From nobody Tue Feb 10 08:31:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1678209561; cv=none; d=zohomail.com; s=zohoarc; b=Lyq2U66uM8/kmUb+MEd4soY3mu/2RSnQHILgLaZZTVSdiZTYXKl6npiJ4vOlyHEKe2OgiwcJD+EjQEjHykTR29EFHzWCPYcZ7A+mwDMg/y/5CoKKuO7OQBQOkdWN6nsI1vRrqGrmHjsT+XFigJzufDHcknjclB70agfWfAYEEQM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678209561; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IgmdOQxZA13cDYE4nim/NP2x+J2PygFHKT5DRcfTY74=; b=mpkP7YdnC0LOfIPCs7GfAcbYrGjdGdBzY9S9n7pxkxVNqWMYpygyeTBRQPGLtGvJs2gLDq+hHHouTSOVZ588+yhnQys2newMau5oI8jLeVljPlCgSNxa/QoTQCABy9pXlLlYN73YqGwUZsAYySYUP1GuXeZbu3Ja7KA8ij/W/Ss= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1678209561380676.4037331266777; Tue, 7 Mar 2023 09:19:21 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.507695.781629 (Exim 4.92) (envelope-from ) id 1pZaxq-0002jD-11; Tue, 07 Mar 2023 17:19:02 +0000 Received: by outflank-mailman (output) from mailman id 507695.781629; Tue, 07 Mar 2023 17:19:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pZaxp-0002j6-TS; Tue, 07 Mar 2023 17:19:01 +0000 Received: by outflank-mailman (input) for mailman id 507695; Tue, 07 Mar 2023 17:19:00 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pZax4-0000bA-BL for xen-devel@lists.xenproject.org; Tue, 07 Mar 2023 17:18:14 +0000 Received: from desiato.infradead.org (desiato.infradead.org [2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 0783ac5d-bd0c-11ed-a550-8520e6686977; Tue, 07 Mar 2023 18:18:08 +0100 (CET) Received: from i7.infradead.org ([2001:8b0:10b:1:21e:67ff:fecb:7a92]) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pZawj-00H5zv-1u; Tue, 07 Mar 2023 17:17:54 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1pZawj-009cgj-1X; Tue, 07 Mar 2023 17:17:53 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list X-Inumbo-ID: 0783ac5d-bd0c-11ed-a550-8520e6686977 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=IgmdOQxZA13cDYE4nim/NP2x+J2PygFHKT5DRcfTY74=; b=GU0U2gn3ju6iPXidagEgqBwIxQ hWmWAq5h1hggCyACBfiQAWAhxbAMO8ERo4HMYsFptrEr7uKJYWyzdml5j9SI3FrOmjkgRwN1FClJ9 a8WhiuZsmJp08j+pDJjYNwB4Fm7hFwaL+cK3/ekhkEtakbUR36/KfgDqMm2px/iG16B1oBWrSf3EL 4ZgtgsVHxeidw61fBNF4m2rSd0LLrywtfidekv8kCAqqZ46ujQrq2WR0Kqep8Somd+NFXNONU0uHY BGNWd3ZwnuNe7ORvVtTmVkOReIK4nFz4GEOOU+Tb5lBQvGN0fG8clvRtZm3kkKBd0R6N3zdwMz6h3 +rDsG0qw==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Paul Durrant , Joao Martins , Ankur Arora , Stefano Stabellini , vikram.garhwal@amd.com, Anthony Perard , xen-devel@lists.xenproject.org, Juan Quintela , "Dr . David Alan Gilbert" , Peter Maydell Subject: [PATCH v2 04/27] hw/xen: Implement XenStore transactions Date: Tue, 7 Mar 2023 17:17:27 +0000 Message-Id: <20230307171750.2293175-5-dwmw2@infradead.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230307171750.2293175-1-dwmw2@infradead.org> References: <20230307171750.2293175-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1678209562285100001 Content-Type: text/plain; charset="utf-8" From: David Woodhouse Given that the whole thing supported copy on write from the beginning, transactions end up being fairly simple. On starting a transaction, just take a ref of the existing root; swap it back in on a successful commit. The main tree has a transaction ID too, and we keep a record of the last transaction ID given out. if the main tree is ever modified when it isn't the latest, it gets a new transaction ID. A commit can only succeed if the main tree hasn't moved on since it was forked. Strictly speaking, the XenStore protocol allows a transaction to succeed as long as nothing *it* read or wrote has changed in the interim, but no implementations do that; *any* change is sufficient to abort a transaction. This does not yet fire watches on the changed nodes on a commit. That bit is more fun and will come in a follow-on commit. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- hw/i386/kvm/xenstore_impl.c | 150 ++++++++++++++++++++++++++++++++++-- tests/unit/test-xs-node.c | 118 ++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+), 6 deletions(-) diff --git a/hw/i386/kvm/xenstore_impl.c b/hw/i386/kvm/xenstore_impl.c index 9c2348835f..0812e367b0 100644 --- a/hw/i386/kvm/xenstore_impl.c +++ b/hw/i386/kvm/xenstore_impl.c @@ -46,13 +46,56 @@ typedef struct XsWatch { int rel_prefix; } XsWatch; =20 +typedef struct XsTransaction { + XsNode *root; + unsigned int nr_nodes; + unsigned int base_tx; + unsigned int tx_id; + unsigned int dom_id; +} XsTransaction; + struct XenstoreImplState { XsNode *root; unsigned int nr_nodes; GHashTable *watches; unsigned int nr_domu_watches; + GHashTable *transactions; + unsigned int nr_domu_transactions; + unsigned int root_tx; + unsigned int last_tx; }; =20 + +static void nobble_tx(gpointer key, gpointer value, gpointer user_data) +{ + unsigned int *new_tx_id =3D user_data; + XsTransaction *tx =3D value; + + if (tx->base_tx =3D=3D *new_tx_id) { + /* Transactions based on XBT_NULL will always fail */ + tx->base_tx =3D XBT_NULL; + } +} + +static inline unsigned int next_tx(struct XenstoreImplState *s) +{ + unsigned int tx_id; + + /* Find the next TX id which isn't either XBT_NULL or in use. */ + do { + tx_id =3D ++s->last_tx; + } while (tx_id =3D=3D XBT_NULL || tx_id =3D=3D s->root_tx || + g_hash_table_lookup(s->transactions, GINT_TO_POINTER(tx_id))); + + /* + * It is vanishingly unlikely, but ensure that no outstanding transact= ion + * is based on the (previous incarnation of the) newly-allocated TX id. + */ + g_hash_table_foreach(s->transactions, nobble_tx, &tx_id); + + return tx_id; +} + static inline XsNode *xs_node_new(void) { XsNode *n =3D g_new0(XsNode, 1); @@ -159,6 +202,7 @@ struct walk_op { =20 GList *watches; unsigned int dom_id; + unsigned int tx_id; =20 /* The number of nodes which will exist in the tree if this op succeed= s. */ unsigned int new_nr_nodes; @@ -176,6 +220,7 @@ struct walk_op { bool inplace; bool mutating; bool create_dirs; + bool in_transaction; }; =20 static void fire_watches(struct walk_op *op, bool parents) @@ -183,7 +228,7 @@ static void fire_watches(struct walk_op *op, bool paren= ts) GList *l =3D NULL; XsWatch *w; =20 - if (!op->mutating) { + if (!op->mutating || op->in_transaction) { return; } =20 @@ -450,10 +495,23 @@ static int xs_node_walk(XsNode **n, struct walk_op *o= p) assert(!op->watches); /* * On completing the recursion back up the path walk and reaching = the - * top, assign the new node count if the operation was successful. + * top, assign the new node count if the operation was successful.= If + * the main tree was changed, bump its tx ID so that outstanding + * transactions correctly fail. But don't bump it every time; only + * if it makes a difference. */ if (!err && op->mutating) { - op->s->nr_nodes =3D op->new_nr_nodes; + if (!op->in_transaction) { + if (op->s->root_tx !=3D op->s->last_tx) { + op->s->root_tx =3D next_tx(op->s); + } + op->s->nr_nodes =3D op->new_nr_nodes; + } else { + XsTransaction *tx =3D g_hash_table_lookup(op->s->transacti= ons, + GINT_TO_POINTER(op= ->tx_id)); + assert(tx); + tx->nr_nodes =3D op->new_nr_nodes; + } } } return err; @@ -535,14 +593,23 @@ static int init_walk_op(XenstoreImplState *s, struct = walk_op *op, op->inplace =3D true; op->mutating =3D false; op->create_dirs =3D false; + op->in_transaction =3D false; op->dom_id =3D dom_id; + op->tx_id =3D tx_id; op->s =3D s; =20 if (tx_id =3D=3D XBT_NULL) { *rootp =3D &s->root; op->new_nr_nodes =3D s->nr_nodes; } else { - return ENOENT; + XsTransaction *tx =3D g_hash_table_lookup(s->transactions, + GINT_TO_POINTER(tx_id)); + if (!tx) { + return ENOENT; + } + *rootp =3D &tx->root; + op->new_nr_nodes =3D tx->nr_nodes; + op->in_transaction =3D true; } =20 return 0; @@ -616,13 +683,71 @@ int xs_impl_directory(XenstoreImplState *s, unsigned = int dom_id, int xs_impl_transaction_start(XenstoreImplState *s, unsigned int dom_id, xs_transaction_t *tx_id) { - return ENOSYS; + XsTransaction *tx; + + if (*tx_id !=3D XBT_NULL) { + return EINVAL; + } + + if (dom_id && s->nr_domu_transactions >=3D XS_MAX_TRANSACTIONS) { + return ENOSPC; + } + + tx =3D g_new0(XsTransaction, 1); + + tx->nr_nodes =3D s->nr_nodes; + tx->tx_id =3D next_tx(s); + tx->base_tx =3D s->root_tx; + tx->root =3D xs_node_ref(s->root); + tx->dom_id =3D dom_id; + + g_hash_table_insert(s->transactions, GINT_TO_POINTER(tx->tx_id), tx); + if (dom_id) { + s->nr_domu_transactions++; + } + *tx_id =3D tx->tx_id; + return 0; +} + +static int transaction_commit(XenstoreImplState *s, XsTransaction *tx) +{ + if (s->root_tx !=3D tx->base_tx) { + return EAGAIN; + } + xs_node_unref(s->root); + s->root =3D tx->root; + tx->root =3D NULL; + s->root_tx =3D tx->tx_id; + s->nr_nodes =3D tx->nr_nodes; + + /* + * XX: Walk the new root and fire watches on any node which has a + * refcount of one (which is therefore unique to this transaction). + */ + return 0; } =20 int xs_impl_transaction_end(XenstoreImplState *s, unsigned int dom_id, xs_transaction_t tx_id, bool commit) { - return ENOSYS; + int ret =3D 0; + XsTransaction *tx =3D g_hash_table_lookup(s->transactions, + GINT_TO_POINTER(tx_id)); + + if (!tx || tx->dom_id !=3D dom_id) { + return ENOENT; + } + + if (commit) { + ret =3D transaction_commit(s, tx); + } + + g_hash_table_remove(s->transactions, GINT_TO_POINTER(tx_id)); + if (dom_id) { + assert(s->nr_domu_transactions); + s->nr_domu_transactions--; + } + return ret; } =20 int xs_impl_rm(XenstoreImplState *s, unsigned int dom_id, @@ -839,15 +964,28 @@ int xs_impl_reset_watches(XenstoreImplState *s, unsig= ned int dom_id) return 0; } =20 +static void xs_tx_free(void *_tx) +{ + XsTransaction *tx =3D _tx; + if (tx->root) { + xs_node_unref(tx->root); + } + g_free(tx); +} + XenstoreImplState *xs_impl_create(void) { XenstoreImplState *s =3D g_new0(XenstoreImplState, 1); =20 s->watches =3D g_hash_table_new_full(g_str_hash, g_str_equal, g_free, = NULL); + s->transactions =3D g_hash_table_new_full(g_direct_hash, g_direct_equa= l, + NULL, xs_tx_free); s->nr_nodes =3D 1; s->root =3D xs_node_new(); #ifdef XS_NODE_UNIT_TEST s->root->name =3D g_strdup("/"); #endif + + s->root_tx =3D s->last_tx =3D 1; return s; } diff --git a/tests/unit/test-xs-node.c b/tests/unit/test-xs-node.c index 19000b64b2..3c3654550a 100644 --- a/tests/unit/test-xs-node.c +++ b/tests/unit/test-xs-node.c @@ -42,6 +42,7 @@ static void xs_impl_delete(XenstoreImplState *s) g_assert(s->nr_nodes =3D=3D 1); =20 g_hash_table_unref(s->watches); + g_hash_table_unref(s->transactions); xs_node_unref(s->root); g_free(s); =20 @@ -271,12 +272,129 @@ static void test_xs_node_simple(void) } =20 =20 +static void do_test_xs_node_tx(bool fail, bool commit) +{ + XenstoreImplState *s =3D setup(); + GString *watches =3D g_string_new(NULL); + GByteArray *data =3D g_byte_array_new(); + unsigned int tx_id =3D XBT_NULL; + int err; + + g_assert(s); + + /* Set a watch */ + err =3D xs_impl_watch(s, DOMID_GUEST, "some", "watch", + watch_cb, watches); + g_assert(!err); + g_assert(watches->len =3D=3D strlen("somewatch")); + g_assert(!strcmp(watches->str, "somewatch")); + g_string_truncate(watches, 0); + + /* Write something */ + err =3D write_str(s, DOMID_GUEST, XBT_NULL, "some/relative/path", + "something"); + g_assert(s->nr_nodes =3D=3D 7); + g_assert(!err); + g_assert(!strcmp(watches->str, + "some/relative/pathwatch")); + g_string_truncate(watches, 0); + + /* Create a transaction */ + err =3D xs_impl_transaction_start(s, DOMID_GUEST, &tx_id); + g_assert(!err); + + if (fail) { + /* Write something else in the root */ + err =3D write_str(s, DOMID_GUEST, XBT_NULL, "some/relative/path", + "another thing"); + g_assert(!err); + g_assert(s->nr_nodes =3D=3D 7); + g_assert(!strcmp(watches->str, + "some/relative/pathwatch")); + g_string_truncate(watches, 0); + } + + g_assert(!watches->len); + + /* Perform a write in the transaction */ + err =3D write_str(s, DOMID_GUEST, tx_id, "some/relative/path", + "something else"); + g_assert(!err); + g_assert(s->nr_nodes =3D=3D 7); + g_assert(!watches->len); + + err =3D xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative/path", d= ata); + g_assert(!err); + if (fail) { + g_assert(data->len =3D=3D strlen("another thing")); + g_assert(!memcmp(data->data, "another thing", data->len)); + } else { + g_assert(data->len =3D=3D strlen("something")); + g_assert(!memcmp(data->data, "something", data->len)); + } + g_byte_array_set_size(data, 0); + + err =3D xs_impl_read(s, DOMID_GUEST, tx_id, "some/relative/path", data= ); + g_assert(!err); + g_assert(data->len =3D=3D strlen("something else")); + g_assert(!memcmp(data->data, "something else", data->len)); + g_byte_array_set_size(data, 0); + + /* Attempt to commit the transaction */ + err =3D xs_impl_transaction_end(s, DOMID_GUEST, tx_id, commit); + if (commit && fail) { + g_assert(err =3D=3D EAGAIN); + } else { + g_assert(!err); + } + g_assert(!watches->len); + g_assert(s->nr_nodes =3D=3D 7); + + err =3D xs_impl_unwatch(s, DOMID_GUEST, "some", "watch", + watch_cb, watches); + g_assert(!err); + + err =3D xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative/path", d= ata); + g_assert(!err); + if (fail) { + g_assert(data->len =3D=3D strlen("another thing")); + g_assert(!memcmp(data->data, "another thing", data->len)); + } else if (commit) { + g_assert(data->len =3D=3D strlen("something else")); + g_assert(!memcmp(data->data, "something else", data->len)); + } else { + g_assert(data->len =3D=3D strlen("something")); + g_assert(!memcmp(data->data, "something", data->len)); + } + g_byte_array_unref(data); + g_string_free(watches, true); + xs_impl_delete(s); +} + +static void test_xs_node_tx_fail(void) +{ + do_test_xs_node_tx(true, true); +} + +static void test_xs_node_tx_abort(void) +{ + do_test_xs_node_tx(false, false); + do_test_xs_node_tx(true, false); +} +static void test_xs_node_tx_succeed(void) +{ + do_test_xs_node_tx(false, true); +} + int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); module_call_init(MODULE_INIT_QOM); =20 g_test_add_func("/xs_node/simple", test_xs_node_simple); + g_test_add_func("/xs_node/tx_abort", test_xs_node_tx_abort); + g_test_add_func("/xs_node/tx_fail", test_xs_node_tx_fail); + g_test_add_func("/xs_node/tx_succeed", test_xs_node_tx_succeed); =20 return g_test_run(); } --=20 2.39.0