[PATCH] tools/xenstored: switch stubdom live update to use file for state

Juergen Gross posted 1 patch 3 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20240731142156.10723-1-jgross@suse.com
tools/xenstored/lu.c        | 75 ++++++++++++++++++++++++++++++++++++-
tools/xenstored/lu.h        | 19 +---------
tools/xenstored/lu_daemon.c | 72 -----------------------------------
tools/xenstored/lu_minios.c | 49 ------------------------
4 files changed, 75 insertions(+), 140 deletions(-)
[PATCH] tools/xenstored: switch stubdom live update to use file for state
Posted by Juergen Gross 3 months, 3 weeks ago
With the introduction of 9pfs for Xenstore-stubdom it is now possible
to use a file for saving the state when doing live update.

This allows to move some environment specific actions back to the
common source file lu.c.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/xenstored/lu.c        | 75 ++++++++++++++++++++++++++++++++++++-
 tools/xenstored/lu.h        | 19 +---------
 tools/xenstored/lu_daemon.c | 72 -----------------------------------
 tools/xenstored/lu_minios.c | 49 ------------------------
 4 files changed, 75 insertions(+), 140 deletions(-)

diff --git a/tools/xenstored/lu.c b/tools/xenstored/lu.c
index 2f41d10c95..bec2a84e10 100644
--- a/tools/xenstored/lu.c
+++ b/tools/xenstored/lu.c
@@ -11,6 +11,8 @@
 #include <stdlib.h>
 #include <syslog.h>
 #include <time.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
 
 #include "talloc.h"
 #include "core.h"
@@ -19,11 +21,18 @@
 #include "watch.h"
 
 #ifndef NO_LIVE_UPDATE
+
+struct lu_dump_state {
+	void *buf;
+	unsigned int size;
+	int fd;
+	char *filename;
+};
+
 struct live_update *lu_status;
 
 static int lu_destroy(void *data)
 {
-	lu_destroy_arch(data);
 	lu_status = NULL;
 
 	return 0;
@@ -70,6 +79,48 @@ bool lu_is_pending(void)
 	return lu_status != NULL;
 }
 
+static void lu_get_dump_state(struct lu_dump_state *state)
+{
+	struct stat statbuf;
+
+	state->size = 0;
+
+	state->filename = talloc_asprintf(NULL, "%s/state_dump",
+					  xenstore_rundir());
+	if (!state->filename)
+		barf("Allocation failure");
+
+	state->fd = open(state->filename, O_RDONLY);
+	if (state->fd < 0)
+		return;
+	if (fstat(state->fd, &statbuf) != 0)
+		goto out_close;
+	state->size = statbuf.st_size;
+
+	state->buf = mmap(NULL, state->size, PROT_READ, MAP_PRIVATE,
+			  state->fd, 0);
+	if (state->buf == MAP_FAILED) {
+		state->size = 0;
+		goto out_close;
+	}
+
+	return;
+
+ out_close:
+	close(state->fd);
+}
+
+static void lu_close_dump_state(struct lu_dump_state *state)
+{
+	assert(state->filename != NULL);
+
+	munmap(state->buf, state->size);
+	close(state->fd);
+
+	unlink(state->filename);
+	talloc_free(state->filename);
+}
+
 void lu_read_state(void)
 {
 	struct lu_dump_state state = {};
@@ -197,6 +248,28 @@ static const char *lu_reject_reason(const void *ctx)
 	return ret ? (const char *)ret : "Overlapping transactions";
 }
 
+static FILE *lu_dump_open(const void *ctx)
+{
+	char *filename;
+	int fd;
+
+	filename = talloc_asprintf(ctx, "%s/state_dump",
+				   xenstore_rundir());
+	if (!filename)
+		return NULL;
+
+	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+	if (fd < 0)
+		return NULL;
+
+	return fdopen(fd, "w");
+}
+
+static void lu_dump_close(FILE *fp)
+{
+	fclose(fp);
+}
+
 static const char *lu_dump_state(const void *ctx, struct connection *conn)
 {
 	FILE *fp;
diff --git a/tools/xenstored/lu.h b/tools/xenstored/lu.h
index ac3c572ca8..dacc9b6e42 100644
--- a/tools/xenstored/lu.h
+++ b/tools/xenstored/lu.h
@@ -18,12 +18,9 @@ struct live_update {
 	unsigned int kernel_size;
 	unsigned int kernel_off;
 
-	void *dump_state;
-	unsigned long dump_size;
-#else
-	char *filename;
 #endif
 
+	char *filename;
 	char *cmdline;
 
 	/* Start parameters. */
@@ -32,15 +29,6 @@ struct live_update {
 	time_t started_at;
 };
 
-struct lu_dump_state {
-	void *buf;
-	unsigned int size;
-#ifndef __MINIOS__
-	int fd;
-	char *filename;
-#endif
-};
-
 extern struct live_update *lu_status;
 
 struct connection *lu_get_connection(void);
@@ -54,15 +42,10 @@ int do_control_lu(const void *ctx, struct connection *conn, const char **vec,
 		  int num);
 
 /* Live update private interfaces. */
-void lu_get_dump_state(struct lu_dump_state *state);
-void lu_close_dump_state(struct lu_dump_state *state);
-FILE *lu_dump_open(const void *ctx);
-void lu_dump_close(FILE *fp);
 char *lu_exec(const void *ctx, int argc, char **argv);
 const char *lu_arch(const void *ctx, struct connection *conn, const char **vec,
 		    int num);
 const char *lu_begin(struct connection *conn);
-void lu_destroy_arch(void *data);
 #else
 static inline struct connection *lu_get_connection(void)
 {
diff --git a/tools/xenstored/lu_daemon.c b/tools/xenstored/lu_daemon.c
index 6351111ab0..6df6c80a2a 100644
--- a/tools/xenstored/lu_daemon.c
+++ b/tools/xenstored/lu_daemon.c
@@ -5,82 +5,14 @@
  * Copyright (C) 2022 Juergen Gross, SUSE LLC
  */
 
-#include <assert.h>
-#include <stdio.h>
 #include <syslog.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
-#include <xen-tools/xenstore-common.h>
 
 #include "talloc.h"
 #include "core.h"
 #include "lu.h"
 
 #ifndef NO_LIVE_UPDATE
-void lu_get_dump_state(struct lu_dump_state *state)
-{
-	struct stat statbuf;
-
-	state->size = 0;
-
-	state->filename = talloc_asprintf(NULL, "%s/state_dump",
-					  xenstore_rundir());
-	if (!state->filename)
-		barf("Allocation failure");
-
-	state->fd = open(state->filename, O_RDONLY);
-	if (state->fd < 0)
-		return;
-	if (fstat(state->fd, &statbuf) != 0)
-		goto out_close;
-	state->size = statbuf.st_size;
-
-	state->buf = mmap(NULL, state->size, PROT_READ, MAP_PRIVATE,
-			  state->fd, 0);
-	if (state->buf == MAP_FAILED) {
-		state->size = 0;
-		goto out_close;
-	}
-
-	return;
-
- out_close:
-	close(state->fd);
-}
-
-void lu_close_dump_state(struct lu_dump_state *state)
-{
-	assert(state->filename != NULL);
-
-	munmap(state->buf, state->size);
-	close(state->fd);
-
-	unlink(state->filename);
-	talloc_free(state->filename);
-}
-
-FILE *lu_dump_open(const void *ctx)
-{
-	char *filename;
-	int fd;
-
-	filename = talloc_asprintf(ctx, "%s/state_dump",
-				   xenstore_rundir());
-	if (!filename)
-		return NULL;
-
-	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
-	if (fd < 0)
-		return NULL;
-
-	return fdopen(fd, "w");
-}
-
-void lu_dump_close(FILE *fp)
-{
-	fclose(fp);
-}
-
 char *lu_exec(const void *ctx, int argc, char **argv)
 {
 	argv[0] = lu_status->filename;
@@ -89,10 +21,6 @@ char *lu_exec(const void *ctx, int argc, char **argv)
 	return "Error activating new binary.";
 }
 
-void lu_destroy_arch(void *data)
-{
-}
-
 static const char *lu_binary(const void *ctx, struct connection *conn,
 			     const char *filename)
 {
diff --git a/tools/xenstored/lu_minios.c b/tools/xenstored/lu_minios.c
index ede8b4dd47..b14a0b29d5 100644
--- a/tools/xenstored/lu_minios.c
+++ b/tools/xenstored/lu_minios.c
@@ -5,67 +5,18 @@
  * Copyright (C) 2022 Juergen Gross, SUSE LLC
  */
 
-#include <stdbool.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <syslog.h>
-#include <sys/mman.h>
-#include <xenctrl.h>
-#include <xen-tools/common-macros.h>
 
 #include "talloc.h"
 #include "lu.h"
 
-/* Mini-OS only knows about MAP_ANON. */
-#ifndef MAP_ANONYMOUS
-#define MAP_ANONYMOUS MAP_ANON
-#endif
-
 #ifndef NO_LIVE_UPDATE
-void lu_get_dump_state(struct lu_dump_state *state)
-{
-}
-
-void lu_close_dump_state(struct lu_dump_state *state)
-{
-}
-
-FILE *lu_dump_open(const void *ctx)
-{
-	lu_status->dump_size = ROUNDUP(talloc_total_size(NULL) * 2,
-				       XC_PAGE_SHIFT);
-	lu_status->dump_state = mmap(NULL, lu_status->dump_size,
-				     PROT_READ | PROT_WRITE,
-				     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-	if (lu_status->dump_state == MAP_FAILED)
-		return NULL;
-
-	return fmemopen(lu_status->dump_state, lu_status->dump_size, "w");
-}
-
-void lu_dump_close(FILE *fp)
-{
-	size_t size;
-
-	size = ftell(fp);
-	size = ROUNDUP(size, XC_PAGE_SHIFT);
-	munmap(lu_status->dump_state + size, lu_status->dump_size - size);
-	lu_status->dump_size = size;
-
-	fclose(fp);
-}
-
 char *lu_exec(const void *ctx, int argc, char **argv)
 {
 	return "NYI";
 }
 
-void lu_destroy_arch(void *data)
-{
-	if (lu_status->dump_state)
-		munmap(lu_status->dump_state, lu_status->dump_size);
-}
-
 static const char *lu_binary_alloc(const void *ctx, struct connection *conn,
 				   unsigned long size)
 {
-- 
2.43.0
Re: [PATCH] tools/xenstored: switch stubdom live update to use file for state
Posted by Julien Grall 3 months, 1 week ago
Hi Juergen,

On 31/07/2024 15:21, Juergen Gross wrote:
> With the introduction of 9pfs for Xenstore-stubdom it is now possible
> to use a file for saving the state when doing live update.
> 
> This allows to move some environment specific actions back to the
> common source file lu.c.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Reviewed-by: Julien Grall <jgrall@amazon.com>

Cheers,

-- 
Julien Grall