All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>, Wei Liu <wl@xen.org>,
	Julien Grall <julien@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v3 16/25] tools/xenstore: move copying of node data out of db_fetch()
Date: Mon, 24 Jul 2023 13:02:38 +0200	[thread overview]
Message-ID: <20230724110247.10520-17-jgross@suse.com> (raw)
In-Reply-To: <20230724110247.10520-1-jgross@suse.com>

Today the node data is copied in db_fetch() on each data base read in
order to avoid accidental data base modifications when working on a
node.

read_node() is the only caller of db_fetch() which isn't freeing the
returned data area immediately after using it. The other callers don't
modify the returned data, so they don't need the data to be copied.

Move copying of the data into read_node(), resulting in a speedup of
the other callers due to no memory allocation and no copying being
needed anymore.

This allows to let db_fetch() return a pointer to const data.

As db_fetch() can't return any error other than ENOENT now, error
handling for the callers can be simplified.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch
V3:
- modify return type of db_fetch() to return a pointer to const
  (Julien Grall)
- drop stale comment (Julien Grall)
- fix transaction handling
---
 tools/xenstore/xenstored_core.c        | 45 +++++++++++---------------
 tools/xenstore/xenstored_core.h        |  2 +-
 tools/xenstore/xenstored_transaction.c | 23 +++++++++----
 3 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 9f88914149..1f5f118f1c 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -555,10 +555,9 @@ static void initialize_fds(int *p_sock_pollfd_idx, int *ptimeout)
 	}
 }
 
-struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size)
+const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size)
 {
-	const struct xs_tdb_record_hdr *hdr;
-	struct xs_tdb_record_hdr *p;
+	struct xs_tdb_record_hdr *hdr;
 
 	hdr = hashtable_search(nodes, db_name);
 	if (!hdr) {
@@ -569,22 +568,15 @@ struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size)
 	*size = sizeof(*hdr) + hdr->num_perms * sizeof(hdr->perms[0]) +
 		hdr->datalen + hdr->childlen;
 
-	/* Return a copy, avoiding a potential modification in the DB. */
-	p = talloc_memdup(NULL, hdr, *size);
-	if (!p) {
-		errno = ENOMEM;
-		return NULL;
-	}
-
 	trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name));
 
-	return p;
+	return hdr;
 }
 
 static void get_acc_data(const char *name, struct node_account_data *acc)
 {
 	size_t size;
-	struct xs_tdb_record_hdr *hdr;
+	const struct xs_tdb_record_hdr *hdr;
 
 	if (acc->memory < 0) {
 		hdr = db_fetch(name, &size);
@@ -595,7 +587,6 @@ static void get_acc_data(const char *name, struct node_account_data *acc)
 			acc->memory = size;
 			acc->domid = hdr->perms[0].id;
 		}
-		talloc_free(hdr);
 	}
 }
 
@@ -708,7 +699,7 @@ struct node *read_node(struct connection *conn, const void *ctx,
 		       const char *name)
 {
 	size_t size;
-	struct xs_tdb_record_hdr *hdr;
+	const struct xs_tdb_record_hdr *hdr;
 	struct node *node;
 	const char *db_name;
 	int err;
@@ -729,30 +720,30 @@ struct node *read_node(struct connection *conn, const void *ctx,
 	hdr = db_fetch(db_name, &size);
 
 	if (hdr == NULL) {
-		if (errno == ENOENT) {
-			node->generation = NO_GENERATION;
-			err = access_node(conn, node, NODE_ACCESS_READ, NULL);
-			errno = err ? : ENOENT;
-		} else {
-			log("DB error on read: %s", strerror(errno));
-			errno = EIO;
-		}
+		node->generation = NO_GENERATION;
+		err = access_node(conn, node, NODE_ACCESS_READ, NULL);
+		errno = err ? : ENOENT;
 		goto error;
 	}
 
 	node->parent = NULL;
-	talloc_steal(node, hdr);
 
 	/* Datalen, childlen, number of permissions */
 	node->generation = hdr->generation;
 	node->perms.num = hdr->num_perms;
 	node->datalen = hdr->datalen;
 	node->childlen = hdr->childlen;
-
-	/* Permissions are struct xs_permissions. */
-	node->perms.p = hdr->perms;
-	node->acc.domid = get_node_owner(node);
+	node->acc.domid = hdr->perms[0].id;
 	node->acc.memory = size;
+
+	/* Copy node data to new memory area, starting with permissions. */
+	size -= sizeof(*hdr);
+	node->perms.p = talloc_memdup(node, hdr->perms, size);
+	if (node->perms.p == NULL) {
+		errno = ENOMEM;
+		goto error;
+	}
+
 	if (domain_adjust_node_perms(node))
 		goto error;
 
diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h
index 1aa3cc0936..6d1578ce97 100644
--- a/tools/xenstore/xenstored_core.h
+++ b/tools/xenstore/xenstored_core.h
@@ -362,7 +362,7 @@ extern xengnttab_handle **xgt_handle;
 int remember_string(struct hashtable *hash, const char *str);
 
 /* Data base access functions. */
-struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size);
+const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size);
 int db_write(struct connection *conn, const char *db_name, const void *data,
 	     size_t size, struct node_account_data *acc,
 	     enum write_node_mode mode, bool no_quota_check);
diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xenstored_transaction.c
index fbcea3663e..a90283dcc5 100644
--- a/tools/xenstore/xenstored_transaction.c
+++ b/tools/xenstore/xenstored_transaction.c
@@ -357,20 +357,17 @@ static int finalize_transaction(struct connection *conn,
 {
 	struct accessed_node *i, *n;
 	size_t size;
-	struct xs_tdb_record_hdr *hdr;
+	const struct xs_tdb_record_hdr *hdr;
 	uint64_t gen;
 
 	list_for_each_entry_safe(i, n, &trans->accessed, list) {
 		if (i->check_gen) {
 			hdr = db_fetch(i->node, &size);
 			if (!hdr) {
-				if (errno != ENOENT)
-					return errno;
 				gen = NO_GENERATION;
 			} else {
 				gen = hdr->generation;
 			}
-			talloc_free(hdr);
 			if (i->generation != gen)
 				return EAGAIN;
 		}
@@ -388,14 +385,26 @@ static int finalize_transaction(struct connection *conn,
 		if (i->ta_node) {
 			hdr = db_fetch(i->trans_name, &size);
 			if (hdr) {
+				/*
+				 * Delete transaction entry and write it as
+				 * no-TA entry. As we only hold a reference
+				 * to the data, increment its ref count, then
+				 * delete it from the DB. Now we own it and can
+				 * drop the const attribute for changing the
+				 * generation count.
+				 */
 				enum write_node_mode mode;
+				struct xs_tdb_record_hdr *own;
 
-				hdr->generation = ++generation;
+				talloc_increase_ref_count(hdr);
+				db_delete(conn, i->trans_name, NULL);
+
+				own = (struct xs_tdb_record_hdr *)hdr;
+				own->generation = ++generation;
 				mode = (i->generation == NO_GENERATION)
 				       ? NODE_CREATE : NODE_MODIFY;
-				*is_corrupt |= db_write(conn, i->node, hdr,
+				*is_corrupt |= db_write(conn, i->node, own,
 							size, NULL, mode, true);
-				db_delete(conn, i->trans_name, NULL);
 			} else {
 				*is_corrupt = true;
 			}
-- 
2.35.3



  parent reply	other threads:[~2023-07-24 11:08 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 11:02 [PATCH v3 00/25] tools/xenstore: drop TDB Juergen Gross
2023-07-24 11:02 ` [PATCH v3 01/25] tools/xenstore: explicitly specify create or modify for tdb_store() Juergen Gross
2023-07-25 16:01   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 02/25] tools/xenstore: replace key in struct node with data base name Juergen Gross
2023-07-24 11:02 ` [PATCH v3 03/25] tools/xenstore: let transaction_prepend() return the name for access Juergen Gross
2023-07-24 11:02 ` [PATCH v3 04/25] tools/xenstore: rename do_tdb_delete() and change parameter type Juergen Gross
2023-07-24 11:02 ` [PATCH v3 05/25] tools/xenstore: rename do_tdb_write() " Juergen Gross
2023-07-24 11:02 ` [PATCH v3 06/25] tools/xenstore: switch get_acc_data() to use name instead of key Juergen Gross
2023-07-24 11:02 ` [PATCH v3 07/25] tools/xenstore: add wrapper for tdb_fetch() Juergen Gross
2023-07-24 11:02 ` [PATCH v3 08/25] tools/xenstore: make hashtable key and value parameters const Juergen Gross
2023-07-25 16:08   ` Julien Grall
2023-07-26  6:19     ` Juergen Gross
2023-07-26  8:20       ` Julien Grall
2023-07-26  8:44         ` Juergen Gross
2023-07-26  9:29           ` Julien Grall
2023-07-26 11:07             ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 09/25] tools/xenstore: let hashtable_add() fail in case of existing entry Juergen Gross
2023-07-24 11:02 ` [PATCH v3 10/25] tools/xenstore: add hashtable_replace() function Juergen Gross
2023-07-27 21:00   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 11/25] tools/xenstore: drop use of tdb Juergen Gross
2023-07-27 21:07   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 12/25] tools/xenstore: remove tdb code Juergen Gross
2023-07-24 11:02 ` [PATCH v3 13/25] tools/xenstore: let db_delete() return void Juergen Gross
2023-07-24 11:02 ` [PATCH v3 14/25] tools/xenstore: change talloc_free() to take a const pointer Juergen Gross
2023-07-27 21:21   ` Julien Grall
2023-07-28  6:15     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 15/25] tools/xenstore: make data parameter of db_write() const Juergen Gross
2023-07-24 11:02 ` Juergen Gross [this message]
2023-07-27 21:33   ` [PATCH v3 16/25] tools/xenstore: move copying of node data out of db_fetch() Julien Grall
2023-07-28  6:18     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 17/25] tools/xenstore: rework struct xs_tdb_record_hdr Juergen Gross
2023-07-27 21:53   ` Julien Grall
2023-07-28  6:23     ` Juergen Gross
2023-07-28  8:59       ` Julien Grall
2023-07-28  9:14         ` Juergen Gross
2023-07-28  9:38           ` Julien Grall
2023-07-28  9:45             ` Juergen Gross
2023-07-28 10:34               ` Julien Grall
2023-07-28 10:47                 ` Juergen Gross
2023-07-28 11:19                   ` Julien Grall
2023-07-28 12:06                     ` Juergen Gross
2023-07-28 12:48                       ` Julien Grall
2023-07-28 13:24                         ` Juergen Gross
2023-07-28 14:08                           ` Julien Grall
2023-07-28 14:32                             ` Juergen Gross
2023-07-28 14:59                               ` Julien Grall
2023-07-28 15:08                                 ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 18/25] tools/xenstore: don't use struct node_perms in struct node Juergen Gross
2023-08-01 21:29   ` Julien Grall
2023-08-02  4:47     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 19/25] tools/xenstore: use struct node_hdr " Juergen Gross
2023-08-01 21:34   ` Julien Grall
2023-08-02  4:50     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 20/25] tools/xenstore: alloc new memory in domain_adjust_node_perms() Juergen Gross
2023-08-01 21:46   ` Julien Grall
2023-08-02  4:51     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 21/25] tools/xenstore: introduce read_node_nocopy() Juergen Gross
2023-08-01 22:00   ` Julien Grall
2023-08-02  4:52     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 22/25] tools/xenstore: merge get_spec_node() into get_node_canonicalized() Juergen Gross
2023-08-03 21:36   ` Julien Grall
2023-08-04  9:17     ` Juergen Gross
2023-08-04  9:21       ` Julien Grall
2023-08-04  9:34         ` Juergen Gross
2023-08-04  9:44           ` Julien Grall
2023-08-04  9:56             ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 23/25] tools/xenstore: merge is_valid_nodename() into canonicalize() Juergen Gross
2023-08-03 21:46   ` Julien Grall
2023-08-04  9:35     ` Juergen Gross
2023-08-04 10:00       ` Julien Grall
2023-08-04 10:17         ` Juergen Gross
2023-08-04 10:33           ` Julien Grall
2023-08-04 12:05             ` Juergen Gross
2023-08-04 12:27               ` Julien Grall
2023-08-04 12:43                 ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 24/25] tools/xenstore: rework get_node() Juergen Gross
2023-08-12 11:56   ` Julien Grall
2023-08-14  5:42     ` Juergen Gross
2023-08-12 12:03   ` Julien Grall
2023-08-14  5:48     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 25/25] tools/xenstore: introduce get_node_const() Juergen Gross
2023-08-12 12:05   ` Julien Grall
2023-08-14  5:54     ` Juergen Gross
2023-07-27 21:02 ` [PATCH v3 00/25] tools/xenstore: drop TDB Julien Grall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230724110247.10520-17-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=anthony.perard@citrix.com \
    --cc=julien@xen.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.