Skip to contentSkip to conversation

tools/xenstore: do some cleanup and fixes

PATCH Superseded Juergen Gross 4 participants 67 messages Nov 1, 2022 – Dec 13, 2022

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover tools/xenstore: do some cleanup and fixes

This is a first run of post-XSA patches which piled up during the development phase of all the recent Xenstore related XSA patches.

At least the first 7 patches are completely independent from each other. After those the dependencies are starting to be more complex.

This is a mixture of small fixes, enhancements and cleanups. Patch 2 was initially part of the XSA patches, which is the reason for having a "Reviewed-by:" tag already. Even with fixing a previous commit it is not urgent to put this patch in, so I don't think this is 4.17 material.

Juergen Gross (20):
tools/xenstore: remove XEN_LIB_STORED and XENSTORED_ROOTDIR tools/xenstore: call remove_domid_from_perm() for special nodes tools/xenstore: let talloc_free() preserve errno tools/xenstore: let tdb_logger() preserve errno tools/xenstore: preserve errno across corrupt() tools/xenstore: remove all watches when a domain has stopped tools/xenstore: enhance hashtable implementation tools/xenstore: add hashlist for finding struct domain by domid tools/xenstore: introduce dummy nodes for special watch paths tools/xenstore: replace watch->relative_path with a prefix length tools/xenstore: move changed domain handling tools/xenstore: change per-domain node accounting interface tools/xenstore: don't allow creating too many nodes in a transaction tools/xenstore: replace literal domid 0 with dom0_domid tools/xenstore: make domain_is_unprivileged() an inline function tools/xenstore: let chk_domain_generation() return a bool tools/xenstore: switch hashtable to use the talloc framework tools/xenstore: make log macro globally available tools/xenstore: introduce trace classes
tools/xenstore: let check_store() check the accounting data

 config/Paths.mk.in                            |   1 -
 configure                                     |   4 -
 docs/configure                                |   4 -
 docs/misc/xenstore.txt                        |  18 +-
 m4/paths.m4                                   |   3 -
 tools/configure                               |   7 +-
 tools/configure.ac                            |   1 -
 tools/hotplug/FreeBSD/rc.d/xencommons.in      |   6 -
 tools/hotplug/Linux/systemd/Makefile          |   1 -
 .../Linux/systemd/var-lib-xenstored.mount.in  |  12 -
 tools/libs/store/Makefile                     |   1 -
 tools/xenstore/.gdbinit                       |   4 -
 tools/xenstore/Makefile                       |   4 -
 tools/xenstore/Makefile.common                |   1 -
 tools/xenstore/hashtable.c                    | 134 ++--
 tools/xenstore/hashtable.h                    |  38 +-
 tools/xenstore/talloc.c                       |  25 +-
 tools/xenstore/xenstored_control.c            |  44 +-
 tools/xenstore/xenstored_core.c               | 223 ++++---
 tools/xenstore/xenstored_core.h               |  31 +
 tools/xenstore/xenstored_domain.c             | 596 ++++++++++--------
 tools/xenstore/xenstored_domain.h             |  21 +-
 tools/xenstore/xenstored_transaction.c        |  76 +--
 tools/xenstore/xenstored_transaction.h        |   7 +-
 tools/xenstore/xenstored_watch.c              |  43 +-
 tools/xenstore/xs_lib.c                       |  11 +-
 26 files changed, 707 insertions(+), 609 deletions(-)
 delete mode 100644 tools/hotplug/Linux/systemd/var-lib-xenstored.mount.in
 delete mode 100644 tools/xenstore/.gdbinit
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/20 tools/xenstore: remove XEN_LIB_STORED and XENSTORED_ROOTDIR

XEN_LIB_STORED is serving no real purpose, as it is a mount point for a tmpfs, so it can be replaced easily by XEN_RUN_STORED.

XENSTORED_ROOTDIR is basically unused already, there is just a single reference in xs_daemon_rootdir() with a fallback to XEN_LIB_STORED, and a .gdbinit file setting it.

Remove the .gdbinit file, as it is not known having been used since ages, and make xs_daemon_rootdir() an alias of xs_daemon_rundir().

---
 config/Paths.mk.in                                   |  1 -
 configure                                            |  4 ----
 docs/configure                                       |  4 ----
 m4/paths.m4                                          |  3 ---
 tools/configure                                      |  7 +------
 tools/configure.ac                                   |  1 -
 tools/hotplug/FreeBSD/rc.d/xencommons.in             |  6 ------
 tools/hotplug/Linux/systemd/Makefile                 |  1 -
 .../hotplug/Linux/systemd/var-lib-xenstored.mount.in | 12 ------------
 tools/libs/store/Makefile                            |  1 -
 tools/xenstore/.gdbinit                              |  4 ----
 tools/xenstore/Makefile                              |  4 ----
 tools/xenstore/Makefile.common                       |  1 -
 tools/xenstore/xs_lib.c                              | 11 +++++------
 14 files changed, 6 insertions(+), 54 deletions(-)
 delete mode 100644 tools/hotplug/Linux/systemd/var-lib-xenstored.mount.in
 delete mode 100644 tools/xenstore/.gdbinit
config/Paths.mk.in +0 -1
index 416fc7aab9..44bab1d748 100644
@@ -41,7 +41,6 @@ MAN8DIR                  := $(mandir)/man8
 XEN_RUN_DIR              := @XEN_RUN_DIR@
 XEN_LOG_DIR              := @XEN_LOG_DIR@
 XEN_LIB_DIR              := @XEN_LIB_DIR@
-XEN_LIB_STORED           := @XEN_LIB_STORED@
 XEN_RUN_STORED           := @XEN_RUN_STORED@
 
 CONFIG_DIR               := @CONFIG_DIR@
configure +0 -4
index bb7f27ddad..b51174f2ef 100755
@@ -603,7 +603,6 @@ INITD_DIR
 SHAREDIR
 XEN_LIB_DIR
 XEN_RUN_STORED
-XEN_LIB_STORED
 XEN_LOG_DIR
 XEN_RUN_DIR
 XENFIRMWAREDIR
@@ -2081,9 +2080,6 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
-XEN_LIB_STORED=$localstatedir/lib/xenstored
-
-
 XEN_RUN_STORED=$rundir_path/xenstored
 
 
docs/configure +0 -4
index d4fced9858..f008ca0565 100755
@@ -601,7 +601,6 @@ INITD_DIR
 SHAREDIR
 XEN_LIB_DIR
 XEN_RUN_STORED
-XEN_LIB_STORED
 XEN_LOG_DIR
 XEN_RUN_DIR
 XENFIRMWAREDIR
@@ -1984,9 +1983,6 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
-XEN_LIB_STORED=$localstatedir/lib/xenstored
-
-
 XEN_RUN_STORED=$rundir_path/xenstored
 
 
m4/paths.m4 +0 -3
index 826faada45..e4104bcce0 100644
@@ -136,9 +136,6 @@ XEN_LOG_DIR=$localstatedir/log/xen
 AC_SUBST(XEN_LOG_DIR)
 AC_DEFINE_UNQUOTED([XEN_LOG_DIR], ["$XEN_LOG_DIR"], [Xen's log dir])
 
-XEN_LIB_STORED=$localstatedir/lib/xenstored
-AC_SUBST(XEN_LIB_STORED)
-
 XEN_RUN_STORED=$rundir_path/xenstored
 AC_SUBST(XEN_RUN_STORED)
 
tools/configure +1 -6
index 6199823f5a..ffe3f48901 100755
@@ -725,7 +725,6 @@ INITD_DIR
 SHAREDIR
 XEN_LIB_DIR
 XEN_RUN_STORED
-XEN_LIB_STORED
 XEN_LOG_DIR
 XEN_RUN_DIR
 XENFIRMWAREDIR
@@ -4065,9 +4064,6 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
-XEN_LIB_STORED=$localstatedir/lib/xenstored
-
-
 XEN_RUN_STORED=$rundir_path/xenstored
 
 
@@ -10085,7 +10081,7 @@ fi
 
 if test "x$systemd" = "xy"; then :
 
-    ac_config_files="$ac_config_files hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/var-lib-xenstored.mount hotplug/Linux/systemd/xen-init-dom0.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service"
+    ac_config_files="$ac_config_files hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/xen-init-dom0.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service"
 
 
 fi
@@ -10967,7 +10963,6 @@ do
     "ocaml/xenstored/oxenstored.conf") CONFIG_FILES="$CONFIG_FILES ocaml/xenstored/oxenstored.conf" ;;
     "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;;
     "hotplug/Linux/systemd/proc-xen.mount") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/proc-xen.mount" ;;
-    "hotplug/Linux/systemd/var-lib-xenstored.mount") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/var-lib-xenstored.mount" ;;
     "hotplug/Linux/systemd/xen-init-dom0.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xen-init-dom0.service" ;;
     "hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service" ;;
     "hotplug/Linux/systemd/xen-watchdog.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xen-watchdog.service" ;;
tools/configure.ac +0 -1
index 18e481d77e..3a2f6a2da9 100644
@@ -482,7 +482,6 @@ AX_AVAILABLE_SYSTEMD()
 AS_IF([test "x$systemd" = "xy"], [
     AC_CONFIG_FILES([
     hotplug/Linux/systemd/proc-xen.mount
-    hotplug/Linux/systemd/var-lib-xenstored.mount
     hotplug/Linux/systemd/xen-init-dom0.service
     hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service
     hotplug/Linux/systemd/xen-watchdog.service
tools/hotplug/FreeBSD/rc.d/xencommons.in +0 -6
index fddcce314c..7f7cda289f 100644
@@ -14,7 +14,6 @@ export LD_LIBRARY_PATH
 
 name="xencommons"
 rcvar="xencommons_enable"
-start_precmd="xen_precmd"
 start_cmd="xen_startcmd"
 stop_cmd="xen_stop"
 status_cmd="xen_status"
@@ -30,11 +29,6 @@ XENSTORED_PIDFILE="@XEN_RUN_DIR@/xenstored.pid"
 load_rc_config $name
 : ${xencommons_enable:=no}
 
-xen_precmd()
-{
-	mkdir -p @XEN_LIB_STORED@ || exit 1
-}
-
 xen_startcmd()
 {
 	local time=0
tools/hotplug/Linux/systemd/Makefile +0 -1
index 26df2a43b1..e29889156d 100644
@@ -4,7 +4,6 @@ include $(XEN_ROOT)/tools/Rules.mk
 XEN_SYSTEMD_MODULES := xen.conf
 
 XEN_SYSTEMD_MOUNT := proc-xen.mount
-XEN_SYSTEMD_MOUNT += var-lib-xenstored.mount
 
 XEN_SYSTEMD_SERVICE := xenstored.service
 XEN_SYSTEMD_SERVICE += xenconsoled.service
tools/hotplug/Linux/systemd/var-lib-xenstored.mount.in deleted +0 -12
deleted file mode 100644
index 11a7d50edc..0000000000
@@ -1,12 +0,0 @@
-[Unit]
-Description=mount xenstore file system
-Requires=proc-xen.mount
-After=proc-xen.mount
-ConditionPathExists=/proc/xen/capabilities
-RefuseManualStop=true
-
-[Mount]
-What=xenstore
-Where=@XEN_LIB_STORED@
-Type=tmpfs
-Options=mode=755
tools/libs/store/Makefile +0 -1
index 2334c953bb..3557a8c76d 100644
@@ -18,7 +18,6 @@ include ../libs.mk
 # Include configure output (config.h)
 CFLAGS += -include $(XEN_ROOT)/tools/config.h
 CFLAGS += $(CFLAGS_libxentoolcore)
-CFLAGS += -DXEN_LIB_STORED="\"$(XEN_LIB_STORED)\""
 CFLAGS += -DXEN_RUN_STORED="\"$(XEN_RUN_STORED)\""
 
 vpath xs_lib.c $(XEN_ROOT)/tools/xenstore
tools/xenstore/.gdbinit deleted +0 -4
deleted file mode 100644
index 9a71b20ac4..0000000000
@@ -1,4 +0,0 @@
-set environment XENSTORED_RUNDIR=testsuite/tmp
-set environment XENSTORED_ROOTDIR=testsuite/tmp
-handle SIGUSR1 noprint nostop
-handle SIGPIPE noprint nostop
tools/xenstore/Makefile +0 -4
index 1b66190cc5..ce7a68178f 100644
@@ -69,7 +69,6 @@ install: all
 	$(INSTALL_DIR) $(DESTDIR)$(bindir)
 ifeq ($(XENSTORE_XENSTORED),y)
 	$(INSTALL_DIR) $(DESTDIR)$(sbindir)
-	$(INSTALL_DIR) $(DESTDIR)$(XEN_LIB_STORED)
 	$(INSTALL_PROG) xenstored $(DESTDIR)$(sbindir)
 endif
 	$(INSTALL_PROG) xenstore-control $(DESTDIR)$(bindir)
@@ -85,9 +84,6 @@ uninstall:
 	rm -f $(DESTDIR)$(bindir)/xenstore-control
 ifeq ($(XENSTORE_XENSTORED),y)
 	rm -f $(DESTDIR)$(sbindir)/xenstored
-	if [ -d $(DESTDIR)$(XEN_LIB_STORED) ]; then \
-		rmdir --ignore-fail-on-non-empty $(DESTDIR)$(XEN_LIB_STORED); \
-	fi
 endif
 	if [ -d $(DESTDIR)$(includedir)/xenstore-compat ]; then \
 		rmdir --ignore-fail-on-non-empty $(DESTDIR)$(includedir)/xenstore-compat; \
tools/xenstore/Makefile.common +0 -1
index ddbac052ac..b18f95c103 100644
@@ -16,7 +16,6 @@ CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += $(CFLAGS_libxenguest)
 CFLAGS += $(CFLAGS_libxentoolcore)
-CFLAGS += -DXEN_LIB_STORED="\"$(XEN_LIB_STORED)\""
 CFLAGS += -DXEN_RUN_STORED="\"$(XEN_RUN_STORED)\""
 
 ifdef CONFIG_STUBDOM
tools/xenstore/xs_lib.c +5 -6
index 10fa4c3ad0..b9941c567c 100644
@@ -26,18 +26,17 @@
 
 /* Common routines for the Xen store daemon and client library. */
 
-const char *xs_daemon_rootdir(void)
-{
-	char *s = getenv("XENSTORED_ROOTDIR");
-	return (s ? s : XEN_LIB_STORED);
-}
-
 const char *xs_daemon_rundir(void)
 {
 	char *s = getenv("XENSTORED_RUNDIR");
 	return (s ? s : XEN_RUN_STORED);
 }
 
+const char *xs_daemon_rootdir(void)
+{
+	return xs_daemon_rundir();
+}
+
 static const char *xs_daemon_path(void)
 {
 	static char buf[PATH_MAX];
Signature · 1 line
2.35.3
Acked-by on v1 1/20
#2 lore raw

Re: [PATCH 01/20] tools/xenstore: remove XEN_LIB_STORED and XENSTORED_ROOTDIR

Quoted 11 lines · Juergen Gross

XEN_LIB_STORED is serving no real purpose, as it is a mount point for a tmpfs, so it can be replaced easily by XEN_RUN_STORED.

XENSTORED_ROOTDIR is basically unused already, there is just a single reference in xs_daemon_rootdir() with a fallback to XEN_LIB_STORED, and a .gdbinit file setting it.

Remove the .gdbinit file, as it is not known having been used since ages, and make xs_daemon_rootdir() an alias of xs_daemon_rundir().

Do I get a Requested/Reported-by?

CC Henry.  This removes an unneeded mount from dom0 (and associated init infrastructure), and as such will be a blanket improvement (if marginal) for 4.17.  I'd like to to be entered for consideration at this point.

~Andrew

#3 lore raw

RE: [PATCH 01/20] tools/xenstore: remove XEN_LIB_STORED and XENSTORED_ROOTDIR

Hi Andrew,

Quoted 18 lines

-----Original Message-----
From: Andrew Cooper <[email protected]>
Subject: Re: [PATCH 01/20] tools/xenstore: remove XEN_LIB_STORED and XENSTORED_ROOTDIR

Quoted 11 lines · Juergen Gross

XEN_LIB_STORED is serving no real purpose, as it is a mount point for a tmpfs, so it can be replaced easily by XEN_RUN_STORED.

XENSTORED_ROOTDIR is basically unused already, there is just a single reference in xs_daemon_rootdir() with a fallback to XEN_LIB_STORED, and a .gdbinit file setting it.

Remove the .gdbinit file, as it is not known having been used since ages, and make xs_daemon_rootdir() an alias of xs_daemon_rundir().

Quoted 7 lines

Do I get a Requested/Reported-by?

CC Henry.  This removes an unneeded mount from dom0 (and associated init infrastructure), and as such will be a blanket improvement (if marginal) for 4.17.  I'd like to to be entered for consideration at this point.

Thanks for noticing! Sounds good to me.

Kind regards,
Henry

Quoted 2 lines

~Andrew

#4 lore raw

Re: [PATCH 01/20] tools/xenstore: remove XEN_LIB_STORED and XENSTORED_ROOTDIR

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Juergen Gross

XEN_LIB_STORED is serving no real purpose, as it is a mount point for a tmpfs, so it can be replaced easily by XEN_RUN_STORED.

XENSTORED_ROOTDIR is basically unused already, there is just a single reference in xs_daemon_rootdir() with a fallback to XEN_LIB_STORED, and a .gdbinit file setting it.

Quoted 6 lines
Quoted 4 lines

Remove the .gdbinit file, as it is not known having been used since ages, and make xs_daemon_rootdir() an alias of xs_daemon_rundir().

Do I get a Requested/Reported-by?

Oh yes, of course. Do you want me to resend with it? I'd be fine with

added when committing.

Quoted 2 lines

Thanks,

Juergen

#5 lore raw

PATCH 2/20 tools/xenstore: call remove_domid_from_perm() for special nodes

In reply to Juergen Gross

When destroying a domain, any stale permissions of the domain must be removed from the special nodes "@...", too. This was not done in the fix for XSA-322.

Fixes 496306324d8d ("tools/xenstore: revoke access rights for removed domains")
---
 tools/xenstore/xenstored_domain.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
tools/xenstore/xenstored_domain.c +24 -0
index 84b7817cd5..aa86892fed 100644
@@ -227,6 +227,27 @@ static void unmap_interface(void *interface)
 	xengnttab_unmap(*xgt_handle, interface, 1);
 }
 
+static void remove_domid_from_perm(struct node_perms *perms,
+				   struct domain *domain)
+{
+	unsigned int cur, new;
+
+	if (perms->p[0].id == domain->domid)
+		perms->p[0].id = priv_domid;
+
+	for (cur = new = 1; cur < perms->num; cur++) {
+		if (perms->p[cur].id == domain->domid)
+			continue;
+
+		if (new != cur)
+			perms->p[new] = perms->p[cur];
+
+		new++;
+	}
+
+	perms->num = new;
+}
+
 static int domain_tree_remove_sub(const void *ctx, struct connection *conn,
 				  struct node *node, void *arg)
 {
@@ -277,6 +298,9 @@ static void domain_tree_remove(struct domain *domain)
 			syslog(LOG_ERR,
 			       "error when looking for orphaned nodes\n");
 	}
+
+	remove_domid_from_perm(&dom_release_perms, domain);
+	remove_domid_from_perm(&dom_introduce_perms, domain);
 }
 
 static int destroy_domain(void *_domain)
Signature · 1 line
2.35.3
#6 lore raw

Re: [PATCH 02/20] tools/xenstore: call remove_domid_from_perm() for special nodes

Quoted 7 lines · Juergen Gross

When destroying a domain, any stale permissions of the domain must be removed from the special nodes "@...", too. This was not done in the fix for XSA-322.

Fixes 496306324d8d ("tools/xenstore: revoke access rights for removed domains")

Henry, this one also ought to be considered for 4.17 at this point, as it's a bugfix to security fix.

As noted in the cover letter, it is R-by already as it came up in private, but was ultimately not included in the security content.

Thanks,

~Andrew

#7 lore raw

RE: [PATCH 02/20] tools/xenstore: call remove_domid_from_perm() for special nodes

Hi Andrew,

Quoted 10 lines

-----Original Message-----
From: Andrew Cooper <[email protected]>
Subject: Re: [PATCH 02/20] tools/xenstore: call remove_domid_from_perm() for special nodes

Quoted 4 lines · Juergen Gross

When destroying a domain, any stale permissions of the domain must be removed from the special nodes "@...", too. This was not done in the fix for XSA-322.

Quoted 7 lines
Quoted 1 line
Fixes 496306324d8d ("tools/xenstore: revoke access rights for removed

domains")

Quoted 2 lines

Henry, this one also ought to be considered for 4.17 at this point, as it's a bugfix to security fix.

Yes, I was wondering why I didn't have an email in my inbox about this patch :)

Kind regards,
Henry

Quoted 7 lines

As noted in the cover letter, it is R-by already as it came up in private, but was ultimately not included in the security content.

Thanks,

~Andrew

#8 lore raw

PATCH 3/20 tools/xenstore: let talloc_free() preserve errno

In reply to Juergen Gross

Today talloc_free() is not guaranteed to preserve errno, especially in case a custom destructor is being used.

Change that by renaming talloc_free() to _talloc_free() in talloc.c and adding a wrapper to talloc.c.

This allows to remove some errno saving outside of talloc.c.

---
 tools/xenstore/talloc.c         | 25 ++++++++++++++++++-------
 tools/xenstore/xenstored_core.c |  2 --
 2 files changed, 18 insertions(+), 9 deletions(-)
tools/xenstore/talloc.c +18 -7
index d7edcf3a93..5fbefdf091 100644
@@ -103,6 +103,8 @@ struct talloc_chunk {
 	unsigned flags;
 };
 
+static int _talloc_free(void *ptr);
+
 /* 16 byte alignment seems to keep everyone happy */
 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
@@ -245,7 +247,7 @@ static int talloc_reference_destructor(void *ptr)
 		tc1->destructor = NULL;
 	}
 	_TLIST_REMOVE(tc2->refs, handle);
-	talloc_free(handle);
+	_talloc_free(handle);
 	return 0;
 }
 
@@ -311,7 +313,7 @@ static int talloc_unreference(const void *context, const void *ptr)
 
 	talloc_set_destructor(h, NULL);
 	_TLIST_REMOVE(tc->refs, h);
-	talloc_free(h);
+	_talloc_free(h);
 	return 0;
 }
 
@@ -349,7 +351,7 @@ int talloc_unlink(const void *context, void *ptr)
 	tc_p = talloc_chunk_from_ptr(ptr);
 
 	if (tc_p->refs == NULL) {
-		return talloc_free(ptr);
+		return _talloc_free(ptr);
 	}
 
 	new_p = talloc_parent_chunk(tc_p->refs);
@@ -521,7 +523,7 @@ static void talloc_free_children(void *ptr)
 			struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
 			if (p) new_parent = TC_PTR_FROM_CHUNK(p);
 		}
-		if (talloc_free(child) == -1) {
+		if (_talloc_free(child) == -1) {
 			if (new_parent == null_context) {
 				struct talloc_chunk *p = talloc_parent_chunk(ptr);
 				if (p) new_parent = TC_PTR_FROM_CHUNK(p);
@@ -539,7 +541,7 @@ static void talloc_free_children(void *ptr)
    will not be freed if the ref_count is > 1 or the destructor (if
    any) returns non-zero
 */
-int talloc_free(void *ptr)
+static int _talloc_free(void *ptr)
 {
 	struct talloc_chunk *tc;
 
@@ -597,7 +599,16 @@ int talloc_free(void *ptr)
 	return 0;
 }
 
+int talloc_free(void *ptr)
+{
+	int ret;
+	int saved_errno = errno;
 
+	ret = _talloc_free(ptr);
+	errno = saved_errno;
+
+	return ret;
+}
 
 /*
   A talloc version of realloc. The context argument is only used if
@@ -610,7 +621,7 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n
 
 	/* size zero is equivalent to free() */
 	if (size == 0) {
-		talloc_free(ptr);
+		_talloc_free(ptr);
 		return NULL;
 	}
 
@@ -1243,7 +1254,7 @@ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
 
 static void talloc_autofree(void)
 {
-	talloc_free(cleanup_context);
+	_talloc_free(cleanup_context);
 	cleanup_context = NULL;
 }
 
tools/xenstore/xenstored_core.c +0 -2
index 476d5c6d51..5a174b9881 100644
@@ -771,9 +771,7 @@ struct node *read_node(struct connection *conn, const void *ctx,
 	return node;
 
  error:
-	err = errno;
 	talloc_free(node);
-	errno = err;
 	return NULL;
 }
 
Signature · 1 line
2.35.3
#9 lore raw

Re: [PATCH 03/20] tools/xenstore: let talloc_free() preserve errno

Hi Juergen,

Quoted 28 lines · Juergen Gross

Today talloc_free() is not guaranteed to preserve errno, especially in case a custom destructor is being used.

Change that by renaming talloc_free() to _talloc_free() in talloc.c and adding a wrapper to talloc.c.

This allows to remove some errno saving outside of talloc.c.

---
  tools/xenstore/talloc.c         | 25 ++++++++++++++++++-------
  tools/xenstore/xenstored_core.c |  2 --
  2 files changed, 18 insertions(+), 9 deletions(-)
tools/xenstore/talloc.c +2 -0
index d7edcf3a93..5fbefdf091 100644
@@ -103,6 +103,8 @@ struct talloc_chunk {
  	unsigned flags;
  };
 
+static int _talloc_free(void *ptr);
+
  /* 16 byte alignment seems to keep everyone happy */
  #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
  #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))

tools/xenstore/talloc.c static int talloc_reference_destructor(void *ptr) · line 250

Quoted 5 lines
tools/xenstore/talloc.c +1 -1
@@ -245,7 +247,7 @@ static int talloc_reference_destructor(void *ptr)
  		tc1->destructor = NULL;
  	}
  	_TLIST_REMOVE(tc2->refs, handle);
-	talloc_free(handle);
+	_talloc_free(handle);

From the commit message, it is not clear to me why we are calling the underscore version here. Same for the others below.

Quoted 31 lines
tools/xenstore/talloc.c +3 -3
  	return 0;
  }
 
@@ -311,7 +313,7 @@ static int talloc_unreference(const void *context, const void *ptr)
 
  	talloc_set_destructor(h, NULL);
  	_TLIST_REMOVE(tc->refs, h);
-	talloc_free(h);
+	_talloc_free(h);
  	return 0;
  }
 
@@ -349,7 +351,7 @@ int talloc_unlink(const void *context, void *ptr)
  	tc_p = talloc_chunk_from_ptr(ptr);
 
  	if (tc_p->refs == NULL) {
-		return talloc_free(ptr);
+		return _talloc_free(ptr);
  	}
 
  	new_p = talloc_parent_chunk(tc_p->refs);
@@ -521,7 +523,7 @@ static void talloc_free_children(void *ptr)
  			struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
  			if (p) new_parent = TC_PTR_FROM_CHUNK(p);
  		}
-		if (talloc_free(child) == -1) {
+		if (_talloc_free(child) == -1) {
  			if (new_parent == null_context) {
  				struct talloc_chunk *p = talloc_parent_chunk(ptr);
  				if (p) new_parent = TC_PTR_FROM_CHUNK(p);

tools/xenstore/talloc.c static void talloc_free_children(void *ptr) · line 542

Quoted 2 lines
tools/xenstore/talloc.c +0 -0
@@ -539,7 +541,7 @@ static void talloc_free_children(void *ptr)
     will not be freed if the ref_count is > 1 or the destructor (if
     any) returns non-zero

Can you expand this comment to explain the different between _talloc_free() and talloc_free()?

I agree the code is probably clear enough, but better to be obvious.

Quoted 46 lines
tools/xenstore/talloc.c +12 -3
  */
-int talloc_free(void *ptr)
+static int _talloc_free(void *ptr)
  {
  	struct talloc_chunk *tc;
 
@@ -597,7 +599,16 @@ int talloc_free(void *ptr)
  	return 0;
  }
 
+int talloc_free(void *ptr)
+{
+	int ret;
+	int saved_errno = errno;
 
+	ret = _talloc_free(ptr);
+	errno = saved_errno;
+
+	return ret;
+}
 
  /*
    A talloc version of realloc. The context argument is only used if
@@ -610,7 +621,7 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n
 
  	/* size zero is equivalent to free() */
  	if (size == 0) {
-		talloc_free(ptr);
+		_talloc_free(ptr);
  		return NULL;
  	}
 
@@ -1243,7 +1254,7 @@ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
 
  static void talloc_autofree(void)
  {
-	talloc_free(cleanup_context);
+	_talloc_free(cleanup_context);
  	cleanup_context = NULL;
  }
 
tools/xenstore/xenstored_core.c +0 -0
index 476d5c6d51..5a174b9881 100644

tools/xenstore/xenstored_core.c struct node *read_node(struct connection *conn, const void *ctx, · line 777

Quoted 9 lines
tools/xenstore/xenstored_core.c +0 -2
@@ -771,9 +771,7 @@ struct node *read_node(struct connection *conn, const void *ctx,
  	return node;
 
   error:
-	err = errno;
  	talloc_free(node);
-	errno = err;
  	return NULL;
  }
 

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

Re: [PATCH 03/20] tools/xenstore: let talloc_free() preserve errno

Quoted 30 lines · Julien Grall

Hi Juergen,

Quoted 27 lines · Juergen Gross

Today talloc_free() is not guaranteed to preserve errno, especially in case a custom destructor is being used.

Change that by renaming talloc_free() to _talloc_free() in talloc.c and adding a wrapper to talloc.c.

This allows to remove some errno saving outside of talloc.c.

---
  tools/xenstore/talloc.c         | 25 ++++++++++++++++++------- tools/xenstore/xenstored_core.c |  2 --

  2 files changed, 18 insertions(+), 9 deletions(-)
tools/xenstore/talloc.c +2 -0
index d7edcf3a93..5fbefdf091 100644
@@ -103,6 +103,8 @@ struct talloc_chunk {
       unsigned flags;
   };
+static int _talloc_free(void *ptr);
+
   /* 16 byte alignment seems to keep everyone happy */
   #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
   #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
@@ -245,7 +247,7 @@ static int talloc_reference_destructor(void *ptr)
Quoted 8 lines

tools/xenstore/talloc.c static int talloc_reference_destructor(void *ptr) · line 250

Quoted 5 lines
tools/xenstore/talloc.c +1 -1
           tc1->destructor = NULL;
       }
       _TLIST_REMOVE(tc2->refs, handle);
-    talloc_free(handle);
+    _talloc_free(handle);

From the commit message, it is not clear to me why we are calling the
underscore version here. Same for the others below.

I was targeting only talloc_free() calls from xenstored to preserve errno.

I can see your point that we could just do the same for talloc internal calls, preserving errno in other cases, too.

OTOH the only relevant case would be the call from talloc_unlink() via talloc_unreference(), which is at least currently no problem regarding errno.

Do you have any preferences? I'm leaning towards dropping the wrapper and do the errno preserving just inside talloc_free().

Juergen

#11 lore raw

PATCH 4/20 tools/xenstore: let tdb_logger() preserve errno

In reply to Juergen Gross

tdb_logger() is called by TDB for logging errors. As errno is checked often after doing the logging, tdb_logger() should preserve errno.

---
 tools/xenstore/xenstored_core.c | 3 +++
 1 file changed, 3 insertions(+)
tools/xenstore/xenstored_core.c +3 -0
index 5a174b9881..d48208ecfe 100644
@@ -2345,6 +2345,7 @@ static void tdb_logger(TDB_CONTEXT *tdb, int level, const char * fmt, ...)
 {
 	va_list ap;
 	char *s;
+	int saved_errno = errno;
 
 	va_start(ap, fmt);
 	s = talloc_vasprintf(NULL, fmt, ap);
@@ -2360,6 +2361,8 @@ static void tdb_logger(TDB_CONTEXT *tdb, int level, const char * fmt, ...)
 		trace("talloc failure during logging\n");
 		syslog(LOG_ERR, "talloc failure during logging\n");
 	}
+
+	errno = saved_errno;
 }
 
 void setup_structure(bool live_update)
Signature · 1 line
2.35.3
Reviewed-by on v1 4/20
#12 lore raw

Re: [PATCH 04/20] tools/xenstore: let tdb_logger() preserve errno

Hi Juergen,

Quoted 4 lines · Juergen Gross

tdb_logger() is called by TDB for logging errors. As errno is checked often after doing the logging, tdb_logger() should preserve errno.

Cheers,

Quoted 25 lines
---
  tools/xenstore/xenstored_core.c | 3 +++
  1 file changed, 3 insertions(+)
tools/xenstore/xenstored_core.c +3 -0
index 5a174b9881..d48208ecfe 100644
@@ -2345,6 +2345,7 @@ static void tdb_logger(TDB_CONTEXT *tdb, int level, const char * fmt, ...)
  {
  	va_list ap;
  	char *s;
+	int saved_errno = errno;
 
  	va_start(ap, fmt);
  	s = talloc_vasprintf(NULL, fmt, ap);
@@ -2360,6 +2361,8 @@ static void tdb_logger(TDB_CONTEXT *tdb, int level, const char * fmt, ...)
  		trace("talloc failure during logging\n");
  		syslog(LOG_ERR, "talloc failure during logging\n");
  	}
+
+	errno = saved_errno;
  }
 
  void setup_structure(bool live_update)
Signature · 1 line
Julien Grall
#13 lore raw

PATCH 5/20 tools/xenstore: preserve errno across corrupt()

In reply to Juergen Gross

Let corrupt() preserve errno in order to be able to simplify error handling in future.

This is rather easy as the errno value when entering corrupt() is saved already.

---
 tools/xenstore/xenstored_core.c | 2 ++
 1 file changed, 2 insertions(+)
tools/xenstore/xenstored_core.c +2 -0
index d48208ecfe..8c2cca62b7 100644
@@ -2549,6 +2549,8 @@ void corrupt(struct connection *conn, const char *fmt, ...)
 	talloc_free(str);
 
 	check_store();
+
+	errno = saved_errno;
 }
 
 #ifndef NO_SOCKETS
Signature · 1 line
2.35.3
Reviewed-by on v1 5/20
#14 lore raw

Re: [PATCH 05/20] tools/xenstore: preserve errno across corrupt()

Hi Juergen,

Quoted 7 lines · Juergen Gross

Let corrupt() preserve errno in order to be able to simplify error handling in future.

This is rather easy as the errno value when entering corrupt() is saved already.

Cheers,

Signature · 1 line
Julien Grall
#15 lore raw

PATCH 6/20 tools/xenstore: remove all watches when a domain has stopped

In reply to Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

---
 tools/xenstore/xenstored_domain.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/xenstore/xenstored_domain.c +6 -0
index aa86892fed..1516df71d8 100644
@@ -364,6 +364,12 @@ void check_domains(void)
 			    && !domain->shutdown) {
 				domain->shutdown = true;
 				notify = 1;
+				/*
+				 * Avoid triggering watch events when the
+				 * domain's nodes are being deleted.
+				 */
+				if (domain->conn)
+					conn_delete_all_watches(domain->conn);
 			}
 			if (!dominfo.dying)
 				continue;
Signature · 1 line
2.35.3
#16 lore raw

Re: [PATCH 06/20] tools/xenstore: remove all watches when a domain has stopped

Hi Juergen,

Quoted 3 lines · Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

From my understanding, shutdown doesn't mean dead. It may be used during migration (or snapshotting), where we don't want to touch the state in case of a cancellation (or resume).

For instance, see the command XS_RESUME which will clear domain->shutdown.

Quoted 13 lines
---
  tools/xenstore/xenstored_domain.c | 6 ++++++
  1 file changed, 6 insertions(+)
tools/xenstore/xenstored_domain.c +0 -0
index aa86892fed..1516df71d8 100644
@@ -364,6 +364,12 @@ void check_domains(void)
  			    && !domain->shutdown) {
  				domain->shutdown = true;

tools/xenstore/xenstored_domain.c void check_domains(void) · line 375

Quoted 10 lines
tools/xenstore/xenstored_domain.c +6 -0
  				notify = 1;
+				/*
+				 * Avoid triggering watch events when the
+				 * domain's nodes are being deleted.
+				 */
+				if (domain->conn)
+					conn_delete_all_watches(domain->conn);
  			}
  			if (!dominfo.dying)
  				continue;

Cheers,

Signature · 1 line
Julien Grall
#17 lore raw

Re: [PATCH 06/20] tools/xenstore: remove all watches when a domain has stopped

Quoted 11 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

From my understanding, shutdown doesn't mean dead. It may be used during
migration (or snapshotting), where we don't want to touch the state in case of a cancellation (or resume).

For instance, see the command XS_RESUME which will clear domain->shutdown.

Oh, good catch!

I need to additionally check the "shutdown reason". I can remove the watches only in case of the reason not having been "suspend".

Juergen

#18 lore raw

Re: [PATCH 06/20] tools/xenstore: remove all watches when a domain has stopped

Quoted 8 lines · Juergen Gross
Quoted 7 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

Quoted 8 lines
Quoted 3 lines

 From my understanding, shutdown doesn't mean dead. It may be used during migration (or snapshotting), where we don't want to touch the state in case of a cancellation (or resume).

For instance, see the command XS_RESUME which will clear domain->shutdown.

Oh, good catch!

I need to additionally check the "shutdown reason". I can remove the watches only in case of the reason not having been "suspend".

This is quite fragile because we may add new shutdown code in the future that could resume.

I think it would be better to only delete the watches if the domain is effectively dying (I don't think it can come back from that state)

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [PATCH 06/20] tools/xenstore: remove all watches when a domain has stopped

Quoted 15 lines · Julien Grall
Quoted 12 lines · Juergen Gross
Quoted 10 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

 From my understanding, shutdown doesn't mean dead. It may be used during migration (or snapshotting), where we don't want to touch the state in case of a cancellation (or resume).

For instance, see the command XS_RESUME which will clear domain->shutdown.

Quoted 8 lines
Quoted 4 lines

Oh, good catch!

I need to additionally check the "shutdown reason". I can remove the watches only in case of the reason not having been "suspend".

This is quite fragile because we may add new shutdown code in the future that could resume.

I think it would be better to only delete the watches if the domain is effectively dying (I don't think it can come back from that state)

This is how it is done today.

The domain's Xenstore entries are removed before the domain is being destroyed.

Juergen

#20 lore raw

Re: [PATCH 06/20] tools/xenstore: remove all watches when a domain has stopped

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 17 lines · Julien Grall
Quoted 14 lines · Juergen Gross
Quoted 10 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

 From my understanding, shutdown doesn't mean dead. It may be used during migration (or snapshotting), where we don't want to touch the state in case of a cancellation (or resume).

For instance, see the command XS_RESUME which will clear domain->shutdown.

Oh, good catch!

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I need to additionally check the "shutdown reason". I can remove the watches only in case of the reason not having been "suspend".

This is quite fragile because we may add new shutdown code in the future that could resume.

I think it would be better to only delete the watches if the domain is effectively dying (I don't think it can come back from that state)

This is how it is done today.

Not really. dominfo.dying is only set if the domain is dead (i.e. DOMDYING_dead). This is happening *after* everything has been relinquished. So this is quite late compare to what you seem to want.

Quoted 3 lines

The domain's Xenstore entries are removed before the domain is being destroyed.

Do you mean before domain_kill() is called? If so, maybe we should call domain_kill() before hand.

The other alternative, would be to notify the Xenstored that domain is never going to come back.

Cheers,

Signature · 1 line
Julien Grall
#21 lore raw

Re: [PATCH 06/20] tools/xenstore: remove all watches when a domain has stopped

Quoted 24 lines · Julien Grall

Hi Juergen,

Quoted 21 lines · Juergen Gross
Quoted 20 lines · Julien Grall
Quoted 16 lines · Juergen Gross
Quoted 10 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

When a domain has been recognized to have stopped, remove all its registered watches. This avoids sending watch events to the dead domain when all the nodes related to it are being removed by the Xen tools.

 From my understanding, shutdown doesn't mean dead. It may be used during migration (or snapshotting), where we don't want to touch the state in case of a cancellation (or resume).

For instance, see the command XS_RESUME which will clear domain->shutdown.

Oh, good catch!

I need to additionally check the "shutdown reason". I can remove the watches only in case of the reason not having been "suspend".

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

This is quite fragile because we may add new shutdown code in the future that could resume.

I think it would be better to only delete the watches if the domain is effectively dying (I don't think it can come back from that state)

This is how it is done today.

Not really. dominfo.dying is only set if the domain is dead (i.e. DOMDYING_dead). This is happening *after* everything has been relinquished. So this is quite late compare to what you seem to want.

I meant that the watches are removed today when the domain has been detected to by dying (so when removing the struct domain in xenstored).

Quoted 3 lines
Quoted 2 lines

The domain's Xenstore entries are removed before the domain is being destroyed.

Do you mean before domain_kill() is called? If so, maybe we should call domain_kill() before hand.

This would probably introduce a race: the domid would no longer be reserved in the hypervisor, so a new domain with the same domid could show up and removal of the old domain data and introduction of the new domain data could interfere.

Quoted 1 line

The other alternative, would be to notify the Xenstored that domain is never going to come back.

Yes, this should really work.

xs_release_domain() should probably do the job.

Juergen

#22 lore raw

PATCH 7/20 tools/xenstore: enhance hashtable implementation

In reply to Juergen Gross

Today it is possible to set a flag when calling hashtable_destroy() in order to specify whether the data associated with the hashtable entries should be freed or not. The keys of the entries will always be freed.

Change that by replacing the flag of hashtable_destroy() by two flags for create_hashtable() which will specify whether the data and/or the key of each entry should be freed or not.

This will enable users to have the key e.g. as part of the data.

Add a new function hashtable_iterate() to call a user specified function for each entry in the hashtable.

Add new primes to the primetable in order to support smaller sizes of the hashtable. The primes are selected according to:

https://planetmath.org/goodhashtableprimes

---
 tools/xenstore/hashtable.c      | 66 +++++++++++++++++++++++----------
 tools/xenstore/hashtable.h      | 35 +++++++++++++++--
 tools/xenstore/xenstored_core.c |  7 ++--
 3 files changed, 82 insertions(+), 26 deletions(-)
tools/xenstore/hashtable.c +46 -20
index 6ac336eff1..7a1548c490 100644
@@ -16,6 +16,7 @@ struct entry
 
 struct hashtable {
     unsigned int tablelength;
+    unsigned int flags;
     struct entry **table;
     unsigned int entrycount;
     unsigned int loadlimit;
@@ -25,12 +26,11 @@ struct hashtable {
 };
 
 /*
-Credit for primes table: Aaron Krowne
- http://br.endernet.org/~akrowne/
- http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
-*/
+ * Credit for primes table: Aaron Krowne
+ * https://planetmath.org/goodhashtableprimes
+ */
 static const unsigned int primes[] = {
-53, 97, 193, 389,
+11, 23, 53, 97, 193, 389,
 769, 1543, 3079, 6151,
 12289, 24593, 49157, 98317,
 196613, 393241, 786433, 1572869,
@@ -52,7 +52,8 @@ indexFor(unsigned int tablelength, unsigned int hashvalue) {
 struct hashtable *
 create_hashtable(unsigned int minsize,
                  unsigned int (*hashf) (void*),
-                 int (*eqf) (void*,void*))
+                 int (*eqf) (void*,void*),
+                 unsigned int flags)
 {
     struct hashtable *h;
     unsigned int pindex, size = primes[0];
@@ -73,6 +74,7 @@ create_hashtable(unsigned int minsize,
         goto err1;
 
     h->tablelength  = size;
+    h->flags        = flags;
     h->primeindex   = pindex;
     h->entrycount   = 0;
     h->hashfn       = hashf;
@@ -235,7 +237,8 @@ hashtable_remove(struct hashtable *h, void *k)
             *pE = e->next;
             h->entrycount--;
             v = e->v;
-            free(e->k);
+            if (h->flags & HASHTABLE_FREE_KEY)
+                free(e->k);
             free(e);
             return v;
         }
@@ -246,29 +249,52 @@ hashtable_remove(struct hashtable *h, void *k)
 }
 
 /*****************************************************************************/
-/* destroy */
-void
-hashtable_destroy(struct hashtable *h, int free_values)
+int
+hashtable_iterate(struct hashtable *h,
+                  int (*func)(void *k, void *v, void *arg), void *arg)
 {
+    int ret;
     unsigned int i;
     struct entry *e, *f;
     struct entry **table = h->table;
-    if (free_values)
+
+    for (i = 0; i < h->tablelength; i++)
     {
-        for (i = 0; i < h->tablelength; i++)
+        e = table[i];
+        while (e)
         {
-            e = table[i];
-            while (NULL != e)
-            { f = e; e = e->next; free(f->k); free(f->v); free(f); }
+            f = e;
+            e = e->next;
+            ret = func(f->k, f->v, arg);
+            if (ret)
+                return ret;
         }
     }
-    else
+
+    return 0;
+}
+
+/*****************************************************************************/
+/* destroy */
+void
+hashtable_destroy(struct hashtable *h)
+{
+    unsigned int i;
+    struct entry *e, *f;
+    struct entry **table = h->table;
+
+    for (i = 0; i < h->tablelength; i++)
     {
-        for (i = 0; i < h->tablelength; i++)
+        e = table[i];
+        while (NULL != e)
         {
-            e = table[i];
-            while (NULL != e)
-            { f = e; e = e->next; free(f->k); free(f); }
+            f = e;
+            e = e->next;
+            if (h->flags & HASHTABLE_FREE_KEY)
+                free(f->k);
+            if (h->flags & HASHTABLE_FREE_VALUE)
+                free(f->v);
+            free(f);
         }
     }
     free(h->table);
tools/xenstore/hashtable.h +32 -3
index 62fef6081a..b31eeaea26 100644
@@ -12,13 +12,21 @@ struct hashtable;
  * @param   minsize         minimum initial size of hashtable
  * @param   hashfunction    function for hashing keys
  * @param   key_eq_fn       function for determining key equality
+ * @param   flags           flags HASHTABLE_*
  * @return                  newly created hashtable or NULL on failure
  */
 
+/* Let hashtable_destroy() free the entries' values. */
+#define HASHTABLE_FREE_VALUE (1U << 0)
+/* Let hashtable_remove() and hashtable_destroy() free the entries' keys. */
+#define HASHTABLE_FREE_KEY   (1U << 1)
+
 struct hashtable *
 create_hashtable(unsigned int minsize,
                  unsigned int (*hashfunction) (void*),
-                 int (*key_eq_fn) (void*,void*));
+                 int (*key_eq_fn) (void*,void*),
+                 unsigned int flags
+);
 
 /*****************************************************************************
  * hashtable_insert
@@ -76,16 +84,37 @@ hashtable_remove(struct hashtable *h, void *k);
 unsigned int
 hashtable_count(struct hashtable *h);
 
+/*****************************************************************************
+ * hashtable_iterate
+
+ * @name           hashtable_iterate
+ * @param   h      the hashtable
+ * @param   func   function to call for each entry
+ * @param   arg    user supplied parameter for func
+ * @return         0 if okay, non-zero return value of func (and iteration
+ *                 was aborted)
+ *
+ * Iterates over all entries in the hashtable and calls func with the
+ * key, value, and the user supplied parameter.
+ * func returning a non-zero value will abort the iteration. In case func is
+ * removing an entry other than itself from the hashtable, it must return a
+ * non-zero value in order to abort the iteration. Inserting entries is
+ * allowed, but it is undefined whether func will be called for those new
+ * entries during this iteration.
+ */
+int
+hashtable_iterate(struct hashtable *h,
+                  int (*func)(void *k, void *v, void *arg), void *arg);
+
 /*****************************************************************************
  * hashtable_destroy
    
  * @name        hashtable_destroy
  * @param   h   the hashtable
- * @param       free_values     whether to call 'free' on the remaining values
  */
 
 void
-hashtable_destroy(struct hashtable *h, int free_values);
+hashtable_destroy(struct hashtable *h);
 
 #endif /* __HASHTABLE_CWC22_H__ */
 
tools/xenstore/xenstored_core.c +4 -3
index 8c2cca62b7..1650821922 100644
@@ -2512,7 +2512,9 @@ void check_store(void)
 		.enoent = check_store_enoent,
 	};
 
-	reachable = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
+	/* Don't free values (they are all void *1) */
+	reachable = create_hashtable(16, hash_from_key_fn, keys_equal_fn,
+				     HASHTABLE_FREE_KEY);
 	if (!reachable) {
 		log("check_store: ENOMEM");
 		return;
@@ -2526,8 +2528,7 @@ void check_store(void)
 		clean_store(reachable);
 	log("Checking store complete.");
 
-	hashtable_destroy(reachable, 0 /* Don't free values (they are all
-					  (void *)1) */);
+	hashtable_destroy(reachable);
 }
 
 
Signature · 1 line
2.35.3
Reviewed-by on v1 7/20
#23 lore raw

Re: [PATCH 07/20] tools/xenstore: enhance hashtable implementation

Hi Juergen,

Quoted 38 lines · Juergen Gross

Today it is possible to set a flag when calling hashtable_destroy() in order to specify whether the data associated with the hashtable entries should be freed or not. The keys of the entries will always be freed.

Change that by replacing the flag of hashtable_destroy() by two flags for create_hashtable() which will specify whether the data and/or the key of each entry should be freed or not.

This will enable users to have the key e.g. as part of the data.

Add a new function hashtable_iterate() to call a user specified function for each entry in the hashtable.

Add new primes to the primetable in order to support smaller sizes of the hashtable. The primes are selected according to:

https://planetmath.org/goodhashtableprimes

---
  tools/xenstore/hashtable.c      | 66 +++++++++++++++++++++++----------
  tools/xenstore/hashtable.h      | 35 +++++++++++++++--
  tools/xenstore/xenstored_core.c |  7 ++--
  3 files changed, 82 insertions(+), 26 deletions(-)
tools/xenstore/hashtable.c +1 -0
index 6ac336eff1..7a1548c490 100644
@@ -16,6 +16,7 @@ struct entry
 
  struct hashtable {
      unsigned int tablelength;
+    unsigned int flags;
      struct entry **table;
      unsigned int entrycount;
      unsigned int loadlimit;

tools/xenstore/hashtable.c struct hashtable { · line 31

Quoted 10 lines
tools/xenstore/hashtable.c +3 -4
@@ -25,12 +26,11 @@ struct hashtable {
  };
 
  /*
-Credit for primes table: Aaron Krowne
- http://br.endernet.org/~akrowne/
- http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
-*/
+ * Credit for primes table: Aaron Krowne
+ * https://planetmath.org/goodhashtableprimes
+ */

You don't seem to explain in the commit message why the URLs have been modified. Other than that:

Cheers,

Signature · 1 line
Julien Grall
#24 lore raw

PATCH 8/20 tools/xenstore: add hashlist for finding struct domain by domid

In reply to Juergen Gross

Today finding a struct domain by its domain id requires to scan the list of domains until finding the correct domid.

Add a hashlist for being able to speed this up. This allows to remove the linking of struct domain in a list. Note that the list of changed domains per transaction is kept as a list, as there are no known use cases with more than 4 domains being touched in a single transaction (this would be a device handled by a driver domain and being assigned to a HVM domain with device model in a stubdom, plus the control domain).

Some simple performance tests comparing the scanning and hashlist have shown that the hashlist will win as soon as more than 6 entries need to be scanned.

---
 tools/xenstore/xenstored_domain.c | 112 +++++++++++++++++-------------
 1 file changed, 65 insertions(+), 47 deletions(-)
tools/xenstore/xenstored_domain.c +65 -47
index 1516df71d8..f6797e53c5 100644
@@ -48,8 +48,6 @@ static struct node_perms dom_introduce_perms;
 
 struct domain
 {
-	struct list_head list;
-
 	/* The id of this domain */
 	unsigned int domid;
 
@@ -96,7 +94,7 @@ struct domain
 	bool wrl_delay_logged;
 };
 
-static LIST_HEAD(domains);
+static struct hashtable *domhash;
 
 static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
 {
@@ -309,7 +307,7 @@ static int destroy_domain(void *_domain)
 
 	domain_tree_remove(domain);
 
-	list_del(&domain->list);
+	hashtable_remove(domhash, &domain->domid);
 
 	if (!domain->introduced)
 		return 0;
@@ -341,49 +339,56 @@ static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo)
 	       dominfo->domid == domid;
 }
 
-void check_domains(void)
+static int check_domain(void *k, void *v, void *arg)
 {
 	xc_dominfo_t dominfo;
-	struct domain *domain;
 	struct connection *conn;
-	int notify = 0;
 	bool dom_valid;
+	struct domain *domain = v;
+	bool *notify = arg;
 
- again:
-	list_for_each_entry(domain, &domains, list) {
-		dom_valid = get_domain_info(domain->domid, &dominfo);
-		if (!domain->introduced) {
-			if (!dom_valid) {
-				talloc_free(domain);
-				goto again;
-			}
-			continue;
-		}
-		if (dom_valid) {
-			if ((dominfo.crashed || dominfo.shutdown)
-			    && !domain->shutdown) {
-				domain->shutdown = true;
-				notify = 1;
-				/*
-				 * Avoid triggering watch events when the
-				 * domain's nodes are being deleted.
-				 */
-				if (domain->conn)
-					conn_delete_all_watches(domain->conn);
-			}
-			if (!dominfo.dying)
-				continue;
+	dom_valid = get_domain_info(domain->domid, &dominfo);
+	if (!domain->introduced) {
+		if (!dom_valid) {
+			talloc_free(domain);
+			return 1;
 		}
-		if (domain->conn) {
-			/* domain is a talloc child of domain->conn. */
-			conn = domain->conn;
-			domain->conn = NULL;
-			talloc_unlink(talloc_autofree_context(), conn);
-			notify = 0; /* destroy_domain() fires the watch */
-			goto again;
+		return 0;
+	}
+	if (dom_valid) {
+		if ((dominfo.crashed || dominfo.shutdown)
+		    && !domain->shutdown) {
+			domain->shutdown = true;
+			*notify = true;
+			/*
+			 * Avoid triggering watch events when the domain's
+			 * nodes are being deleted.
+			 */
+			if (domain->conn)
+				conn_delete_all_watches(domain->conn);
 		}
+		if (!dominfo.dying)
+			return 0;
+	}
+	if (domain->conn) {
+		/* domain is a talloc child of domain->conn. */
+		conn = domain->conn;
+		domain->conn = NULL;
+		talloc_unlink(talloc_autofree_context(), conn);
+		*notify = false; /* destroy_domain() fires the watch */
+		return 1;
 	}
 
+	return 0;
+}
+
+void check_domains(void)
+{
+	bool notify = false;
+
+	while (hashtable_iterate(domhash, check_domain, &notify))
+		;
+
 	if (notify)
 		fire_watches(NULL, NULL, "@releaseDomain", NULL, true, NULL);
 }
@@ -421,13 +426,7 @@ static char *talloc_domain_path(const void *context, unsigned int domid)
 
 static struct domain *find_domain_struct(unsigned int domid)
 {
-	struct domain *i;
-
-	list_for_each_entry(i, &domains, list) {
-		if (i->domid == domid)
-			return i;
-	}
-	return NULL;
+	return hashtable_search(domhash, &domid);
 }
 
 int domain_get_quota(const void *ctx, struct connection *conn,
@@ -476,9 +475,13 @@ static struct domain *alloc_domain(const void *context, unsigned int domid)
 	domain->generation = generation;
 	domain->introduced = false;
 
-	talloc_set_destructor(domain, destroy_domain);
+	if (!hashtable_insert(domhash, &domain->domid, domain)) {
+		talloc_free(domain);
+		errno = ENOMEM;
+		return NULL;
+	}
 
-	list_add(&domain->list, &domains);
+	talloc_set_destructor(domain, destroy_domain);
 
 	return domain;
 }
@@ -909,10 +912,25 @@ void dom0_init(void)
 	xenevtchn_notify(xce_handle, dom0->port);
 }
 
+static unsigned int domhash_fn(void *k)
+{
+	return *(unsigned int *)k;
+}
+
+static int domeq_fn(void *key1, void *key2)
+{
+	return *(unsigned int *)key1 == *(unsigned int *)key2;
+}
+
 void domain_init(int evtfd)
 {
 	int rc;
 
+	/* Start with a random rather low domain count for the hashtable. */
+	domhash = create_hashtable(8, domhash_fn, domeq_fn, 0);
+	if (!domhash)
+		barf_perror("Failed to allocate domain hashtable");
+
 	xc_handle = talloc(talloc_autofree_context(), xc_interface*);
 	if (!xc_handle)
 		barf_perror("Failed to allocate domain handle");
Signature · 1 line
2.35.3
#25 lore raw

Re: [PATCH 08/20] tools/xenstore: add hashlist for finding struct domain by domid

Hi Juergen,

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 342

Quoted 6 lines · Juergen Gross
tools/xenstore/xenstored_domain.c +1 -1
@@ -341,49 +339,56 @@ static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo)
  	       dominfo->domid == domid;
  }
 
-void check_domains(void)
+static int check_domain(void *k, void *v, void *arg)

Looking at this callback, shouldn't 'k' be const? If not, wouldn't this mean a caller could potentially mess up with the hashtable?

Quoted 28 lines
tools/xenstore/xenstored_domain.c +2 -21
  {
  	xc_dominfo_t dominfo;
-	struct domain *domain;
  	struct connection *conn;
-	int notify = 0;
  	bool dom_valid;
+	struct domain *domain = v;
+	bool *notify = arg;
 
- again:
-	list_for_each_entry(domain, &domains, list) {
-		dom_valid = get_domain_info(domain->domid, &dominfo);
-		if (!domain->introduced) {
-			if (!dom_valid) {
-				talloc_free(domain);
-				goto again;
-			}
-			continue;
-		}
-		if (dom_valid) {
-			if ((dominfo.crashed || dominfo.shutdown)
-			    && !domain->shutdown) {
-				domain->shutdown = true;
-				notify = 1;
-				/*
-				 * Avoid triggering watch events when the
-				 * domain's nodes are being deleted.
-				 */

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 354

Quoted 10 lines
tools/xenstore/xenstored_domain.c +5 -5
-				if (domain->conn)
-					conn_delete_all_watches(domain->conn);
-			}
-			if (!dominfo.dying)
-				continue;
+	dom_valid = get_domain_info(domain->domid, &dominfo);
+	if (!domain->introduced) {
+		if (!dom_valid) {
+			talloc_free(domain);
+			return 1;

You are only freeing one domain here. So shouldn't we return 0? If not please explain in a comment.

Quoted 22 lines
tools/xenstore/xenstored_domain.c +13 -7
  		}
-		if (domain->conn) {
-			/* domain is a talloc child of domain->conn. */
-			conn = domain->conn;
-			domain->conn = NULL;
-			talloc_unlink(talloc_autofree_context(), conn);
-			notify = 0; /* destroy_domain() fires the watch */
-			goto again;
+		return 0;
+	}
+	if (dom_valid) {
+		if ((dominfo.crashed || dominfo.shutdown)
+		    && !domain->shutdown) {
+			domain->shutdown = true;
+			*notify = true;
+			/*
+			 * Avoid triggering watch events when the domain's
+			 * nodes are being deleted.
+			 */
+			if (domain->conn)
+				conn_delete_all_watches(domain->conn);
  		}

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 379

Quoted 10 lines
tools/xenstore/xenstored_domain.c +10 -0
+		if (!dominfo.dying)
+			return 0;
+	}
+	if (domain->conn) {
+		/* domain is a talloc child of domain->conn. */
+		conn = domain->conn;
+		domain->conn = NULL;
+		talloc_unlink(talloc_autofree_context(), conn);
+		*notify = false; /* destroy_domain() fires the watch */
+		return 1;

Can you add a comment explaining why 1 is returned here? Is this because we may free two domains?

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 357

Quoted 4 lines
tools/xenstore/xenstored_domain.c +2 -0
  	}
 
+	return 0;
+}

Cheers,

Signature · 1 line
Julien Grall
#26 lore raw

Re: [PATCH 08/20] tools/xenstore: add hashlist for finding struct domain by domid

Quoted 11 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, · line 342

Quoted 6 lines · Juergen Gross
tools/xenstore/xenstored_domain.c +1 -1
@@ -341,49 +339,56 @@ static bool get_domain_info(unsigned int domid, 
 xc_dominfo_t *dominfo)
              dominfo->domid == domid;
   }
-void check_domains(void)
+static int check_domain(void *k, void *v, void *arg)

Looking at this callback, shouldn't 'k' be const? If not, wouldn't this mean a caller could potentially mess up with the hashtable?

I have modified the previous patch to make k const. I hope you are fine with me having kept your "Reviewed-by:".

Quoted 39 lines
Quoted 28 lines
tools/xenstore/xenstored_domain.c +2 -21
   {
       xc_dominfo_t dominfo;
-    struct domain *domain;
       struct connection *conn;
-    int notify = 0;
       bool dom_valid;
+    struct domain *domain = v;
+    bool *notify = arg;
- again:
-    list_for_each_entry(domain, &domains, list) {
-        dom_valid = get_domain_info(domain->domid, &dominfo);
-        if (!domain->introduced) {
-            if (!dom_valid) {
-                talloc_free(domain);
-                goto again;
-            }
-            continue;
-        }
-        if (dom_valid) {
-            if ((dominfo.crashed || dominfo.shutdown)
-                && !domain->shutdown) {
-                domain->shutdown = true;
-                notify = 1;
-                /*
-                 * Avoid triggering watch events when the
-                 * domain's nodes are being deleted.
-                 */

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 354

Quoted 10 lines
tools/xenstore/xenstored_domain.c +5 -5
-                if (domain->conn)
-                    conn_delete_all_watches(domain->conn);
-            }
-            if (!dominfo.dying)
-                continue;
+    dom_valid = get_domain_info(domain->domid, &dominfo);
+    if (!domain->introduced) {
+        if (!dom_valid) {
+            talloc_free(domain);
+            return 1;
Quoted 1 line

You are only freeing one domain here. So shouldn't we return 0? If not please explain in a comment.

Oh, good catch. That was a leftover from a previous state where even removing the entry itself wasn't allowed.

Quoted 34 lines
Quoted 23 lines
tools/xenstore/xenstored_domain.c +13 -7
           }
-        if (domain->conn) {
-            /* domain is a talloc child of domain->conn. */
-            conn = domain->conn;
-            domain->conn = NULL;
-            talloc_unlink(talloc_autofree_context(), conn);
-            notify = 0; /* destroy_domain() fires the watch */
-            goto again;
+        return 0;
+    }
+    if (dom_valid) {
+        if ((dominfo.crashed || dominfo.shutdown)
+            && !domain->shutdown) {
+            domain->shutdown = true;
+            *notify = true;
+            /*
+             * Avoid triggering watch events when the domain's
+             * nodes are being deleted.
+             */
+            if (domain->conn)
+                conn_delete_all_watches(domain->conn);
           }

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 379

Quoted 10 lines
tools/xenstore/xenstored_domain.c +10 -0
+        if (!dominfo.dying)
+            return 0;
+    }
+    if (domain->conn) {
+        /* domain is a talloc child of domain->conn. */
+        conn = domain->conn;
+        domain->conn = NULL;
+        talloc_unlink(talloc_autofree_context(), conn);
+        *notify = false; /* destroy_domain() fires the watch */
+        return 1;
Quoted 1 line

Can you add a comment explaining why 1 is returned here? Is this because we may free two domains?

Should be the same case as above.

In the initial version I just mapped the removed "goto again" to a restart of hashtable_iterate().

Juergen

#27 lore raw

Re: [PATCH 08/20] tools/xenstore: add hashlist for finding struct domain by domid

Hi Juergen,

Quoted 13 lines · Juergen Gross
Quoted 11 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, · line 342

Quoted 6 lines · Juergen Gross
tools/xenstore/xenstored_domain.c +1 -1
@@ -341,49 +339,56 @@ static bool get_domain_info(unsigned int domid, 
 xc_dominfo_t *dominfo)
              dominfo->domid == domid;
   }
-void check_domains(void)
+static int check_domain(void *k, void *v, void *arg)

Looking at this callback, shouldn't 'k' be const? If not, wouldn't this mean a caller could potentially mess up with the hashtable?

Quoted 2 lines

I have modified the previous patch to make k const. I hope you are fine with me having kept your "Reviewed-by:".

Yes I am fine with that.

Cheers,

Signature · 1 line
Julien Grall
#28 lore raw

Re: [PATCH 08/20] tools/xenstore: add hashlist for finding struct domain by domid

In reply to Julien Grall
Quoted 85 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, · line 342

Quoted 6 lines · Juergen Gross
tools/xenstore/xenstored_domain.c +1 -1
@@ -341,49 +339,56 @@ static bool get_domain_info(unsigned int domid, 
 xc_dominfo_t *dominfo)
              dominfo->domid == domid;
   }
-void check_domains(void)
+static int check_domain(void *k, void *v, void *arg)

Looking at this callback, shouldn't 'k' be const? If not, wouldn't this mean a caller could potentially mess up with the hashtable?

Quoted 27 lines
tools/xenstore/xenstored_domain.c +2 -21
   {
       xc_dominfo_t dominfo;
-    struct domain *domain;
       struct connection *conn;
-    int notify = 0;
       bool dom_valid;
+    struct domain *domain = v;
+    bool *notify = arg;
- again:
-    list_for_each_entry(domain, &domains, list) {
-        dom_valid = get_domain_info(domain->domid, &dominfo);
-        if (!domain->introduced) {
-            if (!dom_valid) {
-                talloc_free(domain);
-                goto again;
-            }
-            continue;
-        }
-        if (dom_valid) {
-            if ((dominfo.crashed || dominfo.shutdown)
-                && !domain->shutdown) {
-                domain->shutdown = true;
-                notify = 1;
-                /*
-                 * Avoid triggering watch events when the
-                 * domain's nodes are being deleted.
-                 */

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 354

Quoted 10 lines
tools/xenstore/xenstored_domain.c +5 -5
-                if (domain->conn)
-                    conn_delete_all_watches(domain->conn);
-            }
-            if (!dominfo.dying)
-                continue;
+    dom_valid = get_domain_info(domain->domid, &dominfo);
+    if (!domain->introduced) {
+        if (!dom_valid) {
+            talloc_free(domain);
+            return 1;

You are only freeing one domain here. So shouldn't we return 0? If not please explain in a comment.

Quoted 22 lines
tools/xenstore/xenstored_domain.c +13 -7
           }
-        if (domain->conn) {
-            /* domain is a talloc child of domain->conn. */
-            conn = domain->conn;
-            domain->conn = NULL;
-            talloc_unlink(talloc_autofree_context(), conn);
-            notify = 0; /* destroy_domain() fires the watch */
-            goto again;
+        return 0;
+    }
+    if (dom_valid) {
+        if ((dominfo.crashed || dominfo.shutdown)
+            && !domain->shutdown) {
+            domain->shutdown = true;
+            *notify = true;
+            /*
+             * Avoid triggering watch events when the domain's
+             * nodes are being deleted.
+             */
+            if (domain->conn)
+                conn_delete_all_watches(domain->conn);
           }

tools/xenstore/xenstored_domain.c static bool get_domain_info(unsigned int domid, xc_dominfo_t *dominfo) · line 379

Quoted 10 lines
tools/xenstore/xenstored_domain.c +10 -0
+        if (!dominfo.dying)
+            return 0;
+    }
+    if (domain->conn) {
+        /* domain is a talloc child of domain->conn. */
+        conn = domain->conn;
+        domain->conn = NULL;
+        talloc_unlink(talloc_autofree_context(), conn);
+        *notify = false; /* destroy_domain() fires the watch */
+        return 1;
Quoted 1 line

Can you add a comment explaining why 1 is returned here? Is this because we may free two domains?

Oh, just detected you are right: this might free 2 domains, so a return value other than 0 is required. Will add a comment.

Juergen

#29 lore raw

PATCH 9/20 tools/xenstore: introduce dummy nodes for special watch paths

In reply to Juergen Gross

Instead of special casing the permission handling and watch event firing for the special watch paths "@introduceDomain" and "@releaseDomain", use static dummy nodes added to the data base when starting Xenstore.

This allows to simplify quite some code.

Note that this requires to rework the calls of fire_watches() for the special events in order to avoid leaking memory.

---
 tools/xenstore/xenstored_control.c |   3 -
 tools/xenstore/xenstored_core.c    |  67 +++++++-----
 tools/xenstore/xenstored_domain.c  | 162 ++++-------------------------
 tools/xenstore/xenstored_domain.h  |   6 --
 tools/xenstore/xenstored_watch.c   |  17 +--
 5 files changed, 63 insertions(+), 192 deletions(-)
tools/xenstore/xenstored_control.c +0 -3
index d1aaa00bf4..41e6992591 100644
@@ -676,9 +676,6 @@ static const char *lu_dump_state(const void *ctx, struct connection *conn)
 	if (ret)
 		goto out;
 	ret = dump_state_connections(fp);
-	if (ret)
-		goto out;
-	ret = dump_state_special_nodes(fp);
 	if (ret)
 		goto out;
 	ret = dump_state_nodes(fp, ctx);
tools/xenstore/xenstored_core.c +40 -27

105 lines not on this page · raw message

tools/xenstore/xenstored_domain.c +21 -141

216 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +0 -6
index b38c82991d..630641d620 100644
@@ -99,11 +99,6 @@ void domain_outstanding_domid_dec(unsigned int domid);
 int domain_get_quota(const void *ctx, struct connection *conn,
 		     unsigned int domid);
 
-/* Special node permission handling. */
-int set_perms_special(struct connection *conn, const char *name,
-		      struct node_perms *perms);
-bool check_perms_special(const char *name, struct connection *conn);
-
 /* Write rate limiting */
 
 #define WRL_FACTOR   1000 /* for fixed-point arithmetic */
@@ -132,7 +127,6 @@ void wrl_apply_debit_direct(struct connection *conn);
 void wrl_apply_debit_trans_commit(struct connection *conn);
 
 const char *dump_state_connections(FILE *fp);
-const char *dump_state_special_nodes(FILE *fp);
 
 void read_state_connection(const void *ctx, const void *state);
 
tools/xenstore/xenstored_watch.c +2 -15
index 316c08b7f7..75748ac109 100644
@@ -46,13 +46,6 @@ struct watch
 	char *node;
 };
 
-static bool check_special_event(const char *name)
-{
-	assert(name);
-
-	return strstarts(name, "@");
-}
-
 /* Is child a subnode of parent, or equal? */
 static bool is_child(const char *child, const char *parent)
 {
@@ -153,14 +146,8 @@ void fire_watches(struct connection *conn, const void *ctx, const char *name,
 
 	/* Create an event for each watch. */
 	list_for_each_entry(i, &connections, list) {
-		/* introduce/release domain watches */
-		if (check_special_event(name)) {
-			if (!check_perms_special(name, i))
-				continue;
-		} else {
-			if (!watch_permitted(i, ctx, name, node, perms))
-				continue;
-		}
+		if (!watch_permitted(i, ctx, name, node, perms))
+			continue;
 
 		list_for_each_entry(watch, &i->watches, list) {
 			if (exact) {
Signature · 1 line
2.35.3
#30 lore raw

Re: [PATCH 09/20] tools/xenstore: introduce dummy nodes for special watch paths

Hi Juergen,

Quoted 4 lines · Juergen Gross

Instead of special casing the permission handling and watch event firing for the special watch paths "@introduceDomain" and "@releaseDomain", use static dummy nodes added to the data base when starting Xenstore.

A few questions I think needs to be answered in the commit message:
- Does this means we could write data in "@..." node?
- How about creating sub nodes?
- What does it mean for the accounting? Before, it was accounted to
no-one but now it seems to be accounted to the owner (which may not be dom0).

Quoted 2 lines

This allows to simplify quite some code.

The diff is quite nice to have, but I am not entirely convinced this is making the code easier to understand.

Is this patch helping you for another goal?

Quoted 32 lines

Note that this requires to rework the calls of fire_watches() for the special events in order to avoid leaking memory.

---
  tools/xenstore/xenstored_control.c |   3 -
  tools/xenstore/xenstored_core.c    |  67 +++++++-----
  tools/xenstore/xenstored_domain.c  | 162 ++++-------------------------
  tools/xenstore/xenstored_domain.h  |   6 --
  tools/xenstore/xenstored_watch.c   |  17 +--
  5 files changed, 63 insertions(+), 192 deletions(-)
tools/xenstore/xenstored_control.c +0 -3
index d1aaa00bf4..41e6992591 100644
@@ -676,9 +676,6 @@ static const char *lu_dump_state(const void *ctx, struct connection *conn)
  	if (ret)
  		goto out;
  	ret = dump_state_connections(fp);
-	if (ret)
-		goto out;
-	ret = dump_state_special_nodes(fp);
  	if (ret)
  		goto out;
  	ret = dump_state_nodes(fp, ctx);
tools/xenstore/xenstored_core.c +0 -0
index 1650821922..cadb339486 100644

tools/xenstore/xenstored_core.c static void get_acc_data(TDB_DATA *key, struct node_account_data *acc) · line 619

Quoted 5 lines
tools/xenstore/xenstored_core.c +1 -1
@@ -616,7 +616,8 @@ static void get_acc_data(TDB_DATA *key, struct node_account_data *acc)
  static unsigned int get_acc_domid(struct connection *conn, TDB_DATA *key,
  				  unsigned int domid)
  {
-	return (!conn || key->dptr[0] == '/') ? domid : conn->id;
+	return (!conn || key->dptr[0] == '/' || key->dptr[0] == '@')

The comment above says it is sufficient to check for '/'. But now, you are also checking for '@'.

Quoted 5 lines
tools/xenstore/xenstored_core.c +1 -0
+	       ? domid : conn->id;
  }
 
  int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data,

tools/xenstore/xenstored_core.c static int do_set_perms(const void *ctx, struct connection *conn, · line 1789

Quoted 10 lines
tools/xenstore/xenstored_core.c +0 -7
@@ -1780,14 +1781,6 @@ static int do_set_perms(const void *ctx, struct connection *conn,
  	if (perms.p[0].perms & XS_PERM_IGNORE)
  		return ENOENT;
 
-	/* First arg is node name. */
-	if (strstarts(in->buffer, "@")) {
-		if (set_perms_special(conn, in->buffer, &perms))
-			return errno;
-		send_ack(conn, XS_SET_PERMS);
-		return 0;
-	}

So there are a slight change in behavior here. Before, the permission would be directly set even if we are in a transaction. Now, this will only be set if the transaction has been committed.

I am split on whether this would be considered as an ABI breakage. The new behavior seems better, but anyone rely on the (bogus?) previous behavior would have a surprise. At minimum, the change should at least be pointed out in the commit message.

[...]

Quoted 7 lines
tools/xenstore/xenstored_core.c +0 -0
  static int domain_tree_remove_sub(const void *ctx, struct connection *conn,
  				  struct node *node, void *arg)
  {
@@ -297,8 +273,24 @@ static void domain_tree_remove(struct domain *domain)
  			       "error when looking for orphaned nodes\n");
  	}
 

tools/xenstore/xenstored_core.c static void domain_tree_remove(struct domain *domain) · line 283

Quoted 10 lines
tools/xenstore/xenstored_core.c +8 -2
-	remove_domid_from_perm(&dom_release_perms, domain);
-	remove_domid_from_perm(&dom_introduce_perms, domain);
+	walk_node_tree(domain, NULL, "@releaseDomain", &walkfuncs, domain);
+	walk_node_tree(domain, NULL, "@introduceDomain", &walkfuncs, domain);
+}
+
+static void fire_special_watches(const char *name)
+{
+	void *ctx = talloc_new(NULL);
+	struct node *node;

I can foresee how one may want to abuse for this function. So I would add an assert(name[0] == '@') to match clear this should only for watches starting with '@'.

tools/xenstore/xenstored_core.c static void domain_tree_remove(struct domain *domain) · line 291

Quoted 8 lines
tools/xenstore/xenstored_core.c +8 -0
+
+	if (!ctx)
+		return;
+
+	node = read_node(NULL, ctx, name);
+
+	if (node)
+		fire_watches(NULL, ctx, name, node, true, NULL);

Shouldn't we throw a message if we can't retrieve @...?

Cheers,

Signature · 1 line
Julien Grall
#31 lore raw

Re: [PATCH 09/20] tools/xenstore: introduce dummy nodes for special watch paths

Quoted 9 lines · Julien Grall

Hi Juergen,

Quoted 4 lines · Juergen Gross

Instead of special casing the permission handling and watch event firing for the special watch paths "@introduceDomain" and "@releaseDomain", use static dummy nodes added to the data base when starting Xenstore.

A few questions I think needs to be answered in the commit message:

Okay. Will add.

Quoted 2 lines

  - Does this means we could write data in "@..."  node?
  - How about creating sub nodes?

Normal Xenstore operations won't succeed due to path name checks.

But I just realized that at some time a chunk of the patch must have been lost, as I originally had special checks for set/get permissions in place. Those are no longer there and need to be re-added.

Quoted 2 lines

  - What does it mean for the accounting? Before, it was accounted to no-one
but now it seems to be accounted to the owner (which may not be dom0).

Yes. And this is how it should be.

Quoted 5 lines
Quoted 1 line

This allows to simplify quite some code.

The diff is quite nice to have, but I am not entirely convinced this is making the code easier to understand.

Is this patch helping you for another goal?

It will allow to add more fine grained permissions when adding the support for reporting the domid in special events (e.g. allowing a stubdom to watch the @release event of its target domain).

Quoted 34 lines
Quoted 34 lines

Note that this requires to rework the calls of fire_watches() for the special events in order to avoid leaking memory.

---
  tools/xenstore/xenstored_control.c |   3 - tools/xenstore/xenstored_core.c    |  67 +++++++----- tools/xenstore/xenstored_domain.c  | 162 ++++------------------------- tools/xenstore/xenstored_domain.h  |   6 -- tools/xenstore/xenstored_watch.c   |  17 +--

  5 files changed, 63 insertions(+), 192 deletions(-)
tools/xenstore/xenstored_control.c +0 -3
index d1aaa00bf4..41e6992591 100644
@@ -676,9 +676,6 @@ static const char *lu_dump_state(const void *ctx, struct 
 connection *conn)
       if (ret)
           goto out;
       ret = dump_state_connections(fp);
-    if (ret)
-        goto out;
-    ret = dump_state_special_nodes(fp);
       if (ret)
           goto out;

      ret = dump_state_nodes(fp, ctx);

tools/xenstore/xenstored_core.c +0 -0
index 1650821922..cadb339486 100644
@@ -616,7 +616,8 @@ static void get_acc_data(TDB_DATA *key, struct 
Quoted 8 lines

tools/xenstore/xenstored_core.c static void get_acc_data(TDB_DATA *key, struct node_account_data *acc) · line 619

Quoted 6 lines
tools/xenstore/xenstored_core.c +1 -1
 node_account_data *acc)
   static unsigned int get_acc_domid(struct connection *conn, TDB_DATA *key,
                     unsigned int domid)
   {
-    return (!conn || key->dptr[0] == '/') ? domid : conn->id;
+    return (!conn || key->dptr[0] == '/' || key->dptr[0] == '@')

The comment above says it is sufficient to check for '/'. But now, you are also checking for '@'.

Will fix the comment.

Quoted 18 lines
Quoted 5 lines
tools/xenstore/xenstored_core.c +1 -0
+           ? domid : conn->id;
   }
   int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data,

tools/xenstore/xenstored_core.c static int do_set_perms(const void *ctx, struct · line 1789

Quoted 10 lines
tools/xenstore/xenstored_core.c +0 -7
@@ -1780,14 +1781,6 @@ static int do_set_perms(const void *ctx, struct 
 connection *conn,
       if (perms.p[0].perms & XS_PERM_IGNORE)
           return ENOENT;
-    /* First arg is node name. */
-    if (strstarts(in->buffer, "@")) {
-        if (set_perms_special(conn, in->buffer, &perms))
-            return errno;
-        send_ack(conn, XS_SET_PERMS);
-        return 0;
-    }

So there are a slight change in behavior here. Before, the permission would be directly set even if we are in a transaction. Now, this will only be set if the transaction has been committed.

Quoted 1 line

I am split on whether this would be considered as an ABI breakage. The new behavior seems better, but anyone rely on the (bogus?) previous behavior would have a surprise. At minimum, the change should at least be pointed out in the commit message.

I don't think this will be a real problem, as the old behavior was more like a bug. I'll spell it out in the commit message.

Quoted 13 lines

[...]

Quoted 10 lines

  static int domain_tree_remove_sub(const void *ctx, struct connection *conn, struct node *node, void *arg)
  {

tools/xenstore/xenstored_core.c +2 -2
@@ -297,8 +273,24 @@ static void domain_tree_remove(struct domain *domain)
                      "error when looking for orphaned nodes\n");
       }
-    remove_domid_from_perm(&dom_release_perms, domain);
-    remove_domid_from_perm(&dom_introduce_perms, domain);
+    walk_node_tree(domain, NULL, "@releaseDomain", &walkfuncs, domain);
+    walk_node_tree(domain, NULL, "@introduceDomain", &walkfuncs, domain);
Quoted 8 lines

tools/xenstore/xenstored_core.c static void domain_tree_remove(struct domain *domain) · line 282

Quoted 6 lines
tools/xenstore/xenstored_core.c +6 -0
+}
+
+static void fire_special_watches(const char *name)
+{
+    void *ctx = talloc_new(NULL);
+    struct node *node;

I can foresee how one may want to abuse for this function. So I would add an assert(name[0] == '@') to match clear this should only for watches starting with '@'.

Okay.

Quoted 11 lines

tools/xenstore/xenstored_core.c static void domain_tree_remove(struct domain *domain) · line 290

Quoted 9 lines
tools/xenstore/xenstored_core.c +8 -0
+
+    if (!ctx)
+        return;
+
+    node = read_node(NULL, ctx, name);
+
+    if (node)
+        fire_watches(NULL, ctx, name, node, true, NULL);

Shouldn't we throw a message if we can't retrieve @...?

Yes, I can add that.

Juergen

#32 lore raw

PATCH 10/20 tools/xenstore: replace watch->relative_path with a prefix length

In reply to Juergen Gross

Instead of storing a pointer to the path which is prepended to relative paths in struct watch, just use the length of the prepended path.

It should be noted that the now removed special case of the relative path being "" in get_watch_path() can't happen at all.

---
 tools/xenstore/xenstored_watch.c | 26 +++++++-------------------
 1 file changed, 7 insertions(+), 19 deletions(-)
tools/xenstore/xenstored_watch.c +7 -19
index 75748ac109..8bed967c56 100644
@@ -39,8 +39,8 @@ struct watch
 	/* Current outstanding events applying to this watch. */
 	struct list_head events;
 
-	/* Is this relative to connnection's implicit path? */
-	const char *relative_path;
+	/* Offset into path for skipping prefix (used for relative paths). */
+	unsigned int prefix_len;
 
 	char *token;
 	char *node;
@@ -66,15 +66,7 @@ static bool is_child(const char *child, const char *parent)
 
 static const char *get_watch_path(const struct watch *watch, const char *name)
 {
-	const char *path = name;
-
-	if (watch->relative_path) {
-		path += strlen(watch->relative_path);
-		if (*path == '/') /* Could be "" */
-			path++;
-	}
-
-	return path;
+	return name + watch->prefix_len;
 }
 
 /*
@@ -211,10 +203,7 @@ static struct watch *add_watch(struct connection *conn, char *path, char *token,
 			      no_quota_check))
 		goto nomem;
 
-	if (relative)
-		watch->relative_path = get_implicit_path(conn);
-	else
-		watch->relative_path = NULL;
+	watch->prefix_len = relative ? strlen(get_implicit_path(conn)) + 1 : 0;
 
 	INIT_LIST_HEAD(&watch->events);
 
@@ -316,7 +305,6 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 	struct watch *watch;
 	struct xs_state_watch sw;
 	struct xs_state_record_header head;
-	const char *path;
 
 	head.type = XS_STATE_TYPE_WATCH;
 
@@ -324,8 +312,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		head.length = sizeof(sw);
 
 		sw.conn_id = conn_id;
-		path = get_watch_path(watch, watch->node);
-		sw.path_length = strlen(path) + 1;
+		sw.path_length = strlen(watch->node + watch->prefix_len) + 1;
 		sw.token_length = strlen(watch->token) + 1;
 		head.length += sw.path_length + sw.token_length;
 		head.length = ROUNDUP(head.length, 3);
@@ -334,7 +321,8 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		if (fwrite(&sw, sizeof(sw), 1, fp) != 1)
 			return "Dump watch state error";
 
-		if (fwrite(path, sw.path_length, 1, fp) != 1)
+		if (fwrite(watch->node + watch->prefix_len, sw.path_length,
+			   1, fp) != 1)
 			return "Dump watch path error";
 		if (fwrite(watch->token, sw.token_length, 1, fp) != 1)
 			return "Dump watch token error";
Signature · 1 line
2.35.3
#33 lore raw

Re: [PATCH 10/20] tools/xenstore: replace watch->relative_path with a prefix length

Hi Juergen,

tools/xenstore/xenstored_watch.c const char *dump_state_watches(FILE *fp, struct connection *conn, · line 315

Quoted 7 lines · Juergen Gross
tools/xenstore/xenstored_watch.c +1 -2
@@ -324,8 +312,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
  		head.length = sizeof(sw);
 
  		sw.conn_id = conn_id;
-		path = get_watch_path(watch, watch->node);
-		sw.path_length = strlen(path) + 1;
+		sw.path_length = strlen(watch->node + watch->prefix_len) + 1;

Why are you open-coding get_watch_path()?

tools/xenstore/xenstored_watch.c const char *dump_state_watches(FILE *fp, struct connection *conn, · line 328

Quoted 13 lines
tools/xenstore/xenstored_watch.c +2 -1
  		sw.token_length = strlen(watch->token) + 1;
  		head.length += sw.path_length + sw.token_length;
  		head.length = ROUNDUP(head.length, 3);
@@ -334,7 +321,8 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
  		if (fwrite(&sw, sizeof(sw), 1, fp) != 1)
  			return "Dump watch state error";
 
-		if (fwrite(path, sw.path_length, 1, fp) != 1)
+		if (fwrite(watch->node + watch->prefix_len, sw.path_length,
+			   1, fp) != 1)
  			return "Dump watch path error";
  		if (fwrite(watch->token, sw.token_length, 1, fp) != 1)
  			return "Dump watch token error";

Cheers,

Signature · 1 line
Julien Grall
#34 lore raw

Re: [PATCH 10/20] tools/xenstore: replace watch->relative_path with a prefix length

Quoted 12 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_watch.c const char *dump_state_watches(FILE *fp, struct connection · line 315

Quoted 7 lines · Juergen Gross
tools/xenstore/xenstored_watch.c +1 -2
@@ -324,8 +312,7 @@ const char *dump_state_watches(FILE *fp, struct connection 
 *conn,
           head.length = sizeof(sw);
           sw.conn_id = conn_id;
-        path = get_watch_path(watch, watch->node);
-        sw.path_length = strlen(path) + 1;
+        sw.path_length = strlen(watch->node + watch->prefix_len) + 1;

Why are you open-coding get_watch_path()?

Will fix it.

Juergen

#35 lore raw

PATCH 11/20 tools/xenstore: move changed domain handling

In reply to Juergen Gross

Move all code related to struct changed_domain from xenstored_transaction.c to xenstored_domain.c.

This will be needed later in order to simplify the accounting data updates in cases of errors during a request.

Split the code to have a more generic base framework.

---
 tools/xenstore/xenstored_domain.c      | 85 +++++++++++++++++++++++++-
 tools/xenstore/xenstored_domain.h      |  3 +
 tools/xenstore/xenstored_transaction.c | 64 ++-----------------
 3 files changed, 90 insertions(+), 62 deletions(-)
tools/xenstore/xenstored_domain.c +83 -2
index 5756010944..14fd84c288 100644
@@ -91,6 +91,18 @@ struct domain
 	bool wrl_delay_logged;
 };
 
+struct changed_domain
+{
+	/* List of all changed domains. */
+	struct list_head list;
+
+	/* Identifier of the changed domain. */
+	unsigned int domid;
+
+	/* Amount by which this domain's nbentry field has changed. */
+	int nbentry;
+};
+
 static struct hashtable *domhash;
 
 static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
@@ -492,8 +504,12 @@ static struct domain *find_or_alloc_existing_domain(unsigned int domid)
 	xc_dominfo_t dominfo;
 
 	domain = find_domain_struct(domid);
-	if (!domain && get_domain_info(domid, &dominfo))
-		domain = alloc_domain(NULL, domid);
+	if (!domain) {
+		if (!get_domain_info(domid, &dominfo))
+			errno = ENOENT;
+		else
+			domain = alloc_domain(NULL, domid);
+	}
 
 	return domain;
 }
@@ -547,6 +563,71 @@ static struct domain *find_domain_by_domid(unsigned int domid)
 	return (d && d->introduced) ? d : NULL;
 }
 
+int acc_fix_domains(struct list_head *head, bool update)
+{
+	struct changed_domain *cd;
+	int cnt;
+
+	list_for_each_entry(cd, head, list) {
+		cnt = domain_entry_fix(cd->domid, cd->nbentry, update);
+		if (!update) {
+			if (cnt >= quota_nb_entry_per_domain)
+				return ENOSPC;
+			if (cnt < 0)
+				return ENOMEM;
+		}
+	}
+
+	return 0;
+}
+
+static struct changed_domain *acc_find_changed_domain(struct list_head *head,
+						      unsigned int domid)
+{
+	struct changed_domain *cd;
+
+	list_for_each_entry(cd, head, list) {
+		if (cd->domid == domid)
+			return cd;
+	}
+
+	return NULL;
+}
+
+static struct changed_domain *acc_get_changed_domain(const void *ctx,
+						     struct list_head *head,
+						     unsigned int domid)
+{
+	struct changed_domain *cd;
+
+	cd = acc_find_changed_domain(head, domid);
+	if (cd)
+		return cd;
+
+	cd = talloc_zero(ctx, struct changed_domain);
+	if (!cd)
+		return NULL;
+
+	cd->domid = domid;
+	list_add_tail(&cd->list, head);
+
+	return cd;
+}
+
+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+			unsigned int domid)
+{
+	struct changed_domain *cd;
+
+	cd = acc_get_changed_domain(ctx, head, domid);
+	if (!cd)
+		return errno;
+
+	cd->nbentry += val;
+
+	return 0;
+}
+
 static void domain_conn_reset(struct domain *domain)
 {
 	struct connection *conn = domain->conn;
tools/xenstore/xenstored_domain.h +3 -0
index 630641d620..9e20d2b17d 100644
@@ -98,6 +98,9 @@ void domain_outstanding_dec(struct connection *conn);
 void domain_outstanding_domid_dec(unsigned int domid);
 int domain_get_quota(const void *ctx, struct connection *conn,
 		     unsigned int domid);
+int acc_fix_domains(struct list_head *head, bool update);
+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+			unsigned int domid);
 
 /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +4 -60

104 lines not on this page · raw message

Signature · 1 line
2.35.3
#36 lore raw

Re: [PATCH 11/20] tools/xenstore: move changed domain handling

Hi Juergen,

tools/xenstore/xenstored_domain.c static struct domain *find_or_alloc_existing_domain(unsigned int domid) · line 512

Quoted 13 lines · Juergen Gross

static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)

tools/xenstore/xenstored_domain.c +6 -2
@@ -492,8 +504,12 @@ static struct domain *find_or_alloc_existing_domain(unsigned int domid)
  	xc_dominfo_t dominfo;
 
  	domain = find_domain_struct(domid);
-	if (!domain && get_domain_info(domid, &dominfo))
-		domain = alloc_domain(NULL, domid);
+	if (!domain) {
+		if (!get_domain_info(domid, &dominfo))
+			errno = ENOENT;
+		else
+			domain = alloc_domain(NULL, domid);
+	}

I don't understand how this change is related to this commit.

[...]

tools/xenstore/xenstored_domain.c static struct domain *find_domain_by_domid(unsigned int domid) · line 626

Quoted 10 lines
tools/xenstore/xenstored_domain.c +10 -0
+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+			unsigned int domid)
+{
+	struct changed_domain *cd;
+
+	cd = acc_get_changed_domain(ctx, head, domid);
+	if (!cd)
+		return errno;
+
+	cd->nbentry += val;

As a future improvement, it would be worth considering to check for underflow/overflow.

Cheers,

Signature · 1 line
Julien Grall
#37 lore raw

Re: [PATCH 11/20] tools/xenstore: move changed domain handling

Quoted 18 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static struct domain · line 512

Quoted 13 lines · Juergen Gross

  static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)

tools/xenstore/xenstored_domain.c +6 -2
@@ -492,8 +504,12 @@ static struct domain 
 *find_or_alloc_existing_domain(unsigned int domid)
       xc_dominfo_t dominfo;
       domain = find_domain_struct(domid);
-    if (!domain && get_domain_info(domid, &dominfo))
-        domain = alloc_domain(NULL, domid);
+    if (!domain) {
+        if (!get_domain_info(domid, &dominfo))
+            errno = ENOENT;
+        else
+            domain = alloc_domain(NULL, domid);
+    }

I don't understand how this change is related to this commit.

It is directly related to the hunk below. Returning errno in acc_add_dom_nbentry() requires it to be set correctly in find_or_alloc_existing_domain().

I'll add a remark in the commit message.

Quoted 15 lines

[...]

tools/xenstore/xenstored_domain.c static struct domain *find_domain_by_domid(unsigned int domid) · line 626

Quoted 10 lines
tools/xenstore/xenstored_domain.c +10 -0
+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+            unsigned int domid)
+{
+    struct changed_domain *cd;
+
+    cd = acc_get_changed_domain(ctx, head, domid);
+    if (!cd)
+        return errno;
+
+    cd->nbentry += val;

As a future improvement, it would be worth considering to check for underflow/overflow.

Do you really think we need to make sure not to add/remove more than 2 billion nodes owned by a single domain?

Juergen

#38 lore raw

Re: [PATCH 11/20] tools/xenstore: move changed domain handling

Quoted 20 lines · Juergen Gross
Quoted 18 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static struct domain · line 512

Quoted 13 lines · Juergen Gross

  static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)

tools/xenstore/xenstored_domain.c +6 -2
@@ -492,8 +504,12 @@ static struct domain 
 *find_or_alloc_existing_domain(unsigned int domid)
       xc_dominfo_t dominfo;
       domain = find_domain_struct(domid);
-    if (!domain && get_domain_info(domid, &dominfo))
-        domain = alloc_domain(NULL, domid);
+    if (!domain) {
+        if (!get_domain_info(domid, &dominfo))
+            errno = ENOENT;
+        else
+            domain = alloc_domain(NULL, domid);
+    }

I don't understand how this change is related to this commit.

Quoted 3 lines

It is directly related to the hunk below. Returning errno in acc_add_dom_nbentry() requires it to be set correctly in find_or_alloc_existing_domain().

Wait, that was nonsense. This change is probably a leftover of a previous version. Will remove it.

Juergen

#39 lore raw

Re: [PATCH 11/20] tools/xenstore: move changed domain handling

In reply to Juergen Gross

Hi Juergen,

Quoted 43 lines · Juergen Gross
Quoted 18 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static struct domain · line 512

Quoted 13 lines · Juergen Gross

  static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)

tools/xenstore/xenstored_domain.c +6 -2
@@ -492,8 +504,12 @@ static struct domain 
 *find_or_alloc_existing_domain(unsigned int domid)
       xc_dominfo_t dominfo;
       domain = find_domain_struct(domid);
-    if (!domain && get_domain_info(domid, &dominfo))
-        domain = alloc_domain(NULL, domid);
+    if (!domain) {
+        if (!get_domain_info(domid, &dominfo))
+            errno = ENOENT;
+        else
+            domain = alloc_domain(NULL, domid);
+    }

I don't understand how this change is related to this commit.

It is directly related to the hunk below. Returning errno in acc_add_dom_nbentry() requires it to be set correctly in find_or_alloc_existing_domain().

I'll add a remark in the commit message.

Quoted 16 lines

[...]

tools/xenstore/xenstored_domain.c static struct domain *find_domain_by_domid(unsigned int domid) · line 626

Quoted 11 lines

+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+            unsigned int domid)

tools/xenstore/xenstored_domain.c +8 -0
+{
+    struct changed_domain *cd;
+
+    cd = acc_get_changed_domain(ctx, head, domid);
+    if (!cd)
+        return errno;
+
+    cd->nbentry += val;

As a future improvement, it would be worth considering to check for underflow/overflow.

Quoted 2 lines

Do you really think we need to make sure not to add/remove more than 2 billion nodes owned by a single domain?

No and that's not my point. If you look at domain_entry_fix() we have an assert() to check if the sum is still over 0.

This assert() was actually triggered a few times while testing the previous XSAs batch. So I think it would be worth to carry a similar check (maybe not an assert()) just in case we mess up with accounting in the future.

Cheers,

Signature · 1 line
Julien Grall
#40 lore raw

Re: [PATCH 11/20] tools/xenstore: move changed domain handling

Quoted 45 lines · Julien Grall

Hi Juergen,

Quoted 42 lines · Juergen Gross
Quoted 18 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static struct domain · line 512

Quoted 13 lines · Juergen Gross

  static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)

tools/xenstore/xenstored_domain.c +6 -2
@@ -492,8 +504,12 @@ static struct domain 
 *find_or_alloc_existing_domain(unsigned int domid)
       xc_dominfo_t dominfo;
       domain = find_domain_struct(domid);
-    if (!domain && get_domain_info(domid, &dominfo))
-        domain = alloc_domain(NULL, domid);
+    if (!domain) {
+        if (!get_domain_info(domid, &dominfo))
+            errno = ENOENT;
+        else
+            domain = alloc_domain(NULL, domid);
+    }

I don't understand how this change is related to this commit.

It is directly related to the hunk below. Returning errno in acc_add_dom_nbentry() requires it to be set correctly in find_or_alloc_existing_domain().

I'll add a remark in the commit message.

Quoted 15 lines

[...]

tools/xenstore/xenstored_domain.c static struct domain *find_domain_by_domid(unsigned int domid) · line 626

Quoted 10 lines
tools/xenstore/xenstored_domain.c +10 -0
+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+            unsigned int domid)
+{
+    struct changed_domain *cd;
+
+    cd = acc_get_changed_domain(ctx, head, domid);
+    if (!cd)
+        return errno;
+
+    cd->nbentry += val;

As a future improvement, it would be worth considering to check for underflow/overflow.

Quoted 5 lines
Quoted 2 lines

Do you really think we need to make sure not to add/remove more than 2 billion nodes owned by a single domain?

No and that's not my point. If you look at domain_entry_fix() we have an assert() to check if the sum is still over 0.

This assert() was actually triggered a few times while testing the previous XSAs batch. So I think it would be worth to carry a similar check (maybe not an assert()) just in case we mess up with accounting in the future.

Patch 2 of the 2nd series does that already.

Juergen

#41 lore raw

Re: [PATCH 11/20] tools/xenstore: move changed domain handling

Quoted 47 lines · Juergen Gross
Quoted 46 lines · Julien Grall

Hi Juergen,

Quoted 43 lines · Juergen Gross
Quoted 18 lines · Julien Grall

Hi Juergen,

tools/xenstore/xenstored_domain.c static struct domain · line 512

Quoted 13 lines · Juergen Gross

  static bool check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)

tools/xenstore/xenstored_domain.c +6 -2
@@ -492,8 +504,12 @@ static struct domain 
 *find_or_alloc_existing_domain(unsigned int domid)
       xc_dominfo_t dominfo;
       domain = find_domain_struct(domid);
-    if (!domain && get_domain_info(domid, &dominfo))
-        domain = alloc_domain(NULL, domid);
+    if (!domain) {
+        if (!get_domain_info(domid, &dominfo))
+            errno = ENOENT;
+        else
+            domain = alloc_domain(NULL, domid);
+    }

I don't understand how this change is related to this commit.

It is directly related to the hunk below. Returning errno in acc_add_dom_nbentry() requires it to be set correctly in find_or_alloc_existing_domain().

I'll add a remark in the commit message.

Quoted 16 lines

[...]

tools/xenstore/xenstored_domain.c static struct domain *find_domain_by_domid(unsigned int domid) · line 626

Quoted 11 lines

+int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
+            unsigned int domid)

tools/xenstore/xenstored_domain.c +8 -0
+{
+    struct changed_domain *cd;
+
+    cd = acc_get_changed_domain(ctx, head, domid);
+    if (!cd)
+        return errno;
+
+    cd->nbentry += val;

As a future improvement, it would be worth considering to check for underflow/overflow.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Do you really think we need to make sure not to add/remove more than 2 billion nodes owned by a single domain?

No and that's not my point. If you look at domain_entry_fix() we have an assert() to check if the sum is still over 0.

This assert() was actually triggered a few times while testing the previous XSAs batch. So I think it would be worth to carry a similar check (maybe not an assert()) just in case we mess up with accounting in the future.

Patch 2 of the 2nd series does that already.

Ok. I will have a look.

Cheers,

Signature · 1 line
Julien Grall
#42 lore raw

PATCH 12/20 tools/xenstore: change per-domain node accounting interface

In reply to Juergen Gross

Rework the interface and the internals of the per-domain node accounting:

- rename the functions to domain_nbentry_*() in order to better match the related counter name

- switch from node pointer to domid as interface, as all nodes have the owner filled in

- use a common internal function for adding a value to the counter

For the transaction case add a helper function to get the list head of the per-transaction changed domains, enabling to eliminate the transaction_entry_*() functions.

---
 tools/xenstore/xenstored_core.c        |  22 ++---
 tools/xenstore/xenstored_domain.c      | 122 +++++++++++--------------
 tools/xenstore/xenstored_domain.h      |  10 +-
 tools/xenstore/xenstored_transaction.c |  15 +--
 tools/xenstore/xenstored_transaction.h |   7 +-
 5 files changed, 72 insertions(+), 104 deletions(-)
tools/xenstore/xenstored_core.c +11 -11
index cadb339486..5fd4f8e606 100644
@@ -1449,7 +1449,7 @@ static void destroy_node_rm(struct connection *conn, struct node *node)
 static int destroy_node(struct connection *conn, struct node *node)
 {
 	destroy_node_rm(conn, node);
-	domain_entry_dec(conn, node);
+	domain_nbentry_dec(conn, node->perms.p[0].id);
 
 	/*
 	 * It is not possible to easily revert the changes in a transaction.
@@ -1488,7 +1488,7 @@ static struct node *create_node(struct connection *conn, const void *ctx,
 	for (i = node; i; i = i->parent) {
 		/* i->parent is set for each new node, so check quota. */
 		if (i->parent &&
-		    domain_entry(conn) >= quota_nb_entry_per_domain) {
+		    domain_nbentry(conn) >= quota_nb_entry_per_domain) {
 			ret = ENOSPC;
 			goto err;
 		}
@@ -1499,7 +1499,7 @@ static struct node *create_node(struct connection *conn, const void *ctx,
 
 		/* Account for new node */
 		if (i->parent) {
-			if (domain_entry_inc(conn, i)) {
+			if (domain_nbentry_inc(conn, i->perms.p[0].id)) {
 				destroy_node_rm(conn, i);
 				return NULL;
 			}
@@ -1652,7 +1652,7 @@ static int delnode_sub(const void *ctx, struct connection *conn,
 	watch_exact = strcmp(root, node->name);
 	fire_watches(conn, ctx, node->name, node, watch_exact, NULL);
 
-	domain_entry_dec(conn, node);
+	domain_nbentry_dec(conn, node->perms.p[0].id);
 
 	return WALK_TREE_RM_CHILDENTRY;
 }
@@ -1793,25 +1793,25 @@ static int do_set_perms(const void *ctx, struct connection *conn,
 		return EPERM;
 
 	old_perms = node->perms;
-	domain_entry_dec(conn, node);
+	domain_nbentry_dec(conn, node->perms.p[0].id);
 	node->perms = perms;
-	if (domain_entry_inc(conn, node)) {
+	if (domain_nbentry_inc(conn, node->perms.p[0].id)) {
 		node->perms = old_perms;
 		/*
 		 * This should never fail because we had a reference on the
 		 * domain before and Xenstored is single-threaded.
 		 */
-		domain_entry_inc(conn, node);
+		domain_nbentry_inc(conn, node->perms.p[0].id);
 		return ENOMEM;
 	}
 
 	if (write_node(conn, node, false)) {
 		int saved_errno = errno;
 
-		domain_entry_dec(conn, node);
+		domain_nbentry_dec(conn, node->perms.p[0].id);
 		node->perms = old_perms;
 		/* No failure possible as above. */
-		domain_entry_inc(conn, node);
+		domain_nbentry_inc(conn, node->perms.p[0].id);
 
 		errno = saved_errno;
 		return errno;
@@ -2383,7 +2383,7 @@ void setup_structure(bool live_update)
 		manual_node("/tool/xenstored", NULL);
 		manual_node("@releaseDomain", NULL);
 		manual_node("@introduceDomain", NULL);
-		domain_entry_fix(dom0_domid, 5, true);
+		domain_nbentry_fix(dom0_domid, 5, true);
 	}
 
 	check_store();
@@ -3391,7 +3391,7 @@ void read_state_node(const void *ctx, const void *state)
 	if (write_node_raw(NULL, &key, node, true))
 		barf("write node error restoring node");
 
-	if (domain_entry_inc(&conn, node))
+	if (domain_nbentry_inc(&conn, node->perms.p[0].id))
 		barf("node accounting error restoring node");
 
 	talloc_free(node);
tools/xenstore/xenstored_domain.c +52 -70

176 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +4 -6
index 9e20d2b17d..1e402f2609 100644
@@ -66,10 +66,10 @@ int domain_adjust_node_perms(struct node *node);
 int domain_alloc_permrefs(struct node_perms *perms);
 
 /* Quota manipulation */
-int domain_entry_inc(struct connection *conn, struct node *);
-void domain_entry_dec(struct connection *conn, struct node *);
-int domain_entry_fix(unsigned int domid, int num, bool update);
-int domain_entry(struct connection *conn);
+int domain_nbentry_inc(struct connection *conn, unsigned int domid);
+int domain_nbentry_dec(struct connection *conn, unsigned int domid);
+int domain_nbentry_fix(unsigned int domid, int num, bool update);
+int domain_nbentry(struct connection *conn);
 int domain_memory_add(unsigned int domid, int mem, bool no_quota_check);
 
 /*
@@ -99,8 +99,6 @@ void domain_outstanding_domid_dec(unsigned int domid);
 int domain_get_quota(const void *ctx, struct connection *conn,
 		     unsigned int domid);
 int acc_fix_domains(struct list_head *head, bool update);
-int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
-			unsigned int domid);
 
 /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +2 -13
index 89b92f0baf..82e5e66c18 100644
@@ -548,20 +548,9 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 	return 0;
 }
 
-void transaction_entry_inc(struct transaction *trans, unsigned int domid)
+struct list_head *transaction_get_changed_domains(struct transaction *trans)
 {
-	if (acc_add_dom_nbentry(trans, &trans->changed_domains, 1, domid)) {
-		/* Let the transaction fail. */
-		trans->fail = true;
-	}
-}
-
-void transaction_entry_dec(struct transaction *trans, unsigned int domid)
-{
-	if (acc_add_dom_nbentry(trans, &trans->changed_domains, -1, domid)) {
-		/* Let the transaction fail. */
-		trans->fail = true;
-	}
+	return &trans->changed_domains;
 }
 
 void fail_transaction(struct transaction *trans)
tools/xenstore/xenstored_transaction.h +3 -4
index 3417303f94..b6f8cb7d0a 100644
@@ -36,10 +36,6 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 
 struct transaction *transaction_lookup(struct connection *conn, uint32_t id);
 
-/* inc/dec entry number local to trans while changing a node */
-void transaction_entry_inc(struct transaction *trans, unsigned int domid);
-void transaction_entry_dec(struct transaction *trans, unsigned int domid);
-
 /* This node was accessed. */
 int __must_check access_node(struct connection *conn, struct node *node,
                              enum node_access_type type, TDB_DATA *key);
@@ -54,6 +50,9 @@ void transaction_prepend(struct connection *conn, const char *name,
 /* Mark the transaction as failed. This will prevent it to be committed. */
 void fail_transaction(struct transaction *trans);
 
+/* Get the list head of the changed domains. */
+struct list_head *transaction_get_changed_domains(struct transaction *trans);
+
 void conn_delete_all_transactions(struct connection *conn);
 int check_transactions(struct hashtable *hash);
 
Signature · 1 line
2.35.3
#43 lore raw

PATCH 13/20 tools/xenstore: don't allow creating too many nodes in a transaction

In reply to Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

---
 tools/xenstore/xenstored_domain.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/xenstore/xenstored_domain.c +2 -3
index b737a77683..529ffb522a 100644
@@ -1125,9 +1125,8 @@ int domain_nbentry_fix(unsigned int domid, int num, bool update)
 
 int domain_nbentry(struct connection *conn)
 {
-	return (domain_is_unprivileged(conn))
-		? conn->domain->nbentry
-		: 0;
+	return domain_is_unprivileged(conn)
+	       ? domain_nbentry_add(conn, conn->id, 0, true) : 0;
 }
 
 static bool domain_chk_quota(struct domain *domain, int mem)
Signature · 1 line
2.35.3
#44 lore raw

Re: [PATCH 13/20] tools/xenstore: don't allow creating too many nodes in a transaction

Hi Juergen,

Quoted 5 lines · Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

I have already made some of comments on the security ML when this was initially set. The arguments still don't sound right to me.

For a first, since XSA-326, we have a cap in place for the memory a domain can consume. So this surely can't be a "lot of memory". Otherwise we are saying that our limit (there are other way to hit it) were wrong...

In addition to that, this is quite pointless to check the number of entry a domain currently owned because this can change before the transaction is committed by another "external" command. Therefore, this would add some randomness in the command which could make more difficult to diagnose.

Lastly, there are other very easy way to use memory (just create multiple transaction in parallel).

So based on the commit message, the change doesn't help at all to make better Xenstored.

Note that I don't particularly mind the code change (even though it adds more complexity). I just strongly dislike the current justifications.

At the moment, I can't find a justification that would make the change whorthwhile.

Cheers,

Signature · 1 line
Julien Grall
#45 lore raw

Re: [PATCH 13/20] tools/xenstore: don't allow creating too many nodes in a transaction

Quoted 9 lines · Julien Grall

Hi Juergen,

Quoted 5 lines · Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

Quoted 3 lines

I have already made some of comments on the security ML when this was initially set. The arguments still don't sound right to me.

For a first, since XSA-326, we have a cap in place for the memory a domain can consume. So this surely can't be a "lot of memory". Otherwise we are saying that our limit (there are other way to hit it) were wrong...

Yeah, maybe I should rework the commit message.

The reason I still want to keep the patch is that in a transaction without any conflicts and without hitting any transaction specific limits (number of nodes accessed), the errors returned due to a single operation should be the same as with the same operation performed outside a transaction.

Quoted 1 line

In addition to that, this is quite pointless to check the number of entry a domain currently owned because this can change before the transaction is committed by another "external" command. Therefore, this would add some randomness in the command which could make more difficult to diagnose.

In the scope of the transaction the tests are correct.

Quoted 7 lines

Lastly, there are other very easy way to use memory (just create multiple transaction in parallel).

So based on the commit message, the change doesn't help at all to make better Xenstored.

Note that I don't particularly mind the code change (even though it adds more complexity). I just strongly dislike the current justifications.

At the moment, I can't find a justification that would make the change whorthwhile.

See above reasoning.

Juergen

#46 lore raw

Re: [PATCH 13/20] tools/xenstore: don't allow creating too many nodes in a transaction

Quoted 12 lines · Juergen Gross
Quoted 11 lines · Julien Grall

Hi Juergen,

Quoted 5 lines · Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

I have already made some of comments on the security ML when this was initially set. The arguments still don't sound right to me.

Quoted 8 lines
Quoted 1 line

For a first, since XSA-326, we have a cap in place for the memory a domain can consume. So this surely can't be a "lot of memory". Otherwise we are saying that our limit (there are other way to hit it) were wrong...

Yeah, maybe I should rework the commit message.

The reason I still want to keep the patch is that in a transaction without any conflicts and without hitting any transaction specific limits (number of nodes accessed), the errors returned due to a single operation should be the same as with the same operation performed outside a transaction.

This seems to be a very niche use case. So it is not clear to me why this matter and we want to add extra check for everyone.

Cheers,

Signature · 1 line
Julien Grall
#47 lore raw

Re: [PATCH 13/20] tools/xenstore: don't allow creating too many nodes in a transaction

Quoted 17 lines · Julien Grall
Quoted 14 lines · Juergen Gross
Quoted 12 lines · Julien Grall

Hi Juergen,

Quoted 5 lines · Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

I have already made some of comments on the security ML when this was initially set. The arguments still don't sound right to me.

For a first, since XSA-326, we have a cap in place for the memory a domain can consume. So this surely can't be a "lot of memory". Otherwise we are saying that our limit (there are other way to hit it) were wrong...

Quoted 8 lines
Quoted 6 lines

Yeah, maybe I should rework the commit message.

The reason I still want to keep the patch is that in a transaction without any conflicts and without hitting any transaction specific limits (number of nodes accessed), the errors returned due to a single operation should be the same as with the same operation performed outside a transaction.

This seems to be a very niche use case. So it is not clear to me why this matter and we want to add extra check for everyone.

It is a matter of correctness.

Additionally, after the internal accounting rework this makes even more sense, as the maximum values per domain seen are really correct, even when queried while transactions are active.

Juergen

#48 lore raw

Re: [PATCH 13/20] tools/xenstore: don't allow creating too many nodes in a transaction

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 17 lines · Julien Grall
Quoted 16 lines · Juergen Gross
Quoted 9 lines · Julien Grall

Hi Juergen,

Quoted 5 lines · Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

Quoted 3 lines

I have already made some of comments on the security ML when this was initially set. The arguments still don't sound right to me.

For a first, since XSA-326, we have a cap in place for the memory a domain can consume. So this surely can't be a "lot of memory". Otherwise we are saying that our limit (there are other way to hit it) were wrong...

Yeah, maybe I should rework the commit message.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

The reason I still want to keep the patch is that in a transaction without any conflicts and without hitting any transaction specific limits (number of nodes accessed), the errors returned due to a single operation should be the same as with the same operation performed outside a transaction.

This seems to be a very niche use case. So it is not clear to me why this matter and we want to add extra check for everyone.

It is a matter of correctness.

I think this is a matter of perspective. Transactions are inherently racy and I see no point of try to solve issues in the idealistic case (i.e. no conflict). The reasoning below...

Quoted 4 lines

Additionally, after the internal accounting rework this makes even more sense, as the maximum values per domain seen are really correct, even when queried while transactions are active.

... might be a better justification. But I will need to review the other patch in order to forge a more positive opinion. Can you point me to the patch?

Cheers,

Signature · 1 line
Julien Grall
#49 lore raw

Re: [PATCH 13/20] tools/xenstore: don't allow creating too many nodes in a transaction

Quoted 30 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross
Quoted 17 lines · Julien Grall
Quoted 16 lines · Juergen Gross
Quoted 9 lines · Julien Grall

Hi Juergen,

Quoted 5 lines · Juergen Gross

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it allows to create arbitrary number of nodes. The transaction will finally fail due to exceeding the number of nodes quota, but before closing the transaction an unprivileged guest could cause Xenstore to use a lot of memory.

Quoted 3 lines

I have already made some of comments on the security ML when this was initially set. The arguments still don't sound right to me.

For a first, since XSA-326, we have a cap in place for the memory a domain can consume. So this surely can't be a "lot of memory". Otherwise we are saying that our limit (there are other way to hit it) were wrong...

Yeah, maybe I should rework the commit message.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

The reason I still want to keep the patch is that in a transaction without any conflicts and without hitting any transaction specific limits (number of nodes accessed), the errors returned due to a single operation should be the same as with the same operation performed outside a transaction.

This seems to be a very niche use case. So it is not clear to me why this matter and we want to add extra check for everyone.

It is a matter of correctness.

Quoted 8 lines

I think this is a matter of perspective. Transactions are inherently racy and I see no point of try to solve issues in the idealistic case (i.e. no conflict). The reasoning below...

Quoted 4 lines

Additionally, after the internal accounting rework this makes even more sense, as the maximum values per domain seen are really correct, even when queried while transactions are active.

... might be a better justification. But I will need to review the other patch in order to forge a more positive opinion. Can you point me to the patch?

The main changes are in patches 2-4 of the 2nd series [1].

Juergen

[1]: https://lists.xen.org/archives/html/xen-devel/2022-11/msg00050.html

#50 lore raw

PATCH 14/20 tools/xenstore: replace literal domid 0 with dom0_domid

In reply to Juergen Gross

There are some places left where dom0 is associated with domid 0.

Use dom0_domid instead.

---
V2:
- new patch

---
 tools/xenstore/xenstored_core.c   | 5 +++--
 tools/xenstore/xenstored_domain.c | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)
tools/xenstore/xenstored_core.c +3 -2
index 5fd4f8e606..5013f8b909 100644
@@ -2302,9 +2302,10 @@ static void accept_connection(int sock)
 		return;
 
 	conn = new_connection(&socket_funcs);
-	if (conn)
+	if (conn) {
 		conn->fd = fd;
-	else
+		conn->id = dom0_domid;
+	} else
 		close(fd);
 }
 #endif
tools/xenstore/xenstored_domain.c +4 -4
index 529ffb522a..0f1c903ee6 100644
@@ -324,7 +324,7 @@ static int destroy_domain(void *_domain)
 	if (domain->interface) {
 		/* Domain 0 was mapped by dom0_init, so it must be unmapped
 		   using munmap() and not the grant unmap call. */
-		if (domain->domid == 0)
+		if (domain->domid == dom0_domid)
 			unmap_xenbus(domain->interface);
 		else
 			unmap_interface(domain->interface);
@@ -414,7 +414,7 @@ void handle_event(void)
 
 static bool domid_is_unprivileged(unsigned int domid)
 {
-	return domid != 0 && domid != priv_domid;
+	return domid != dom0_domid && domid != priv_domid;
 }
 
 bool domain_is_unprivileged(struct connection *conn)
@@ -806,7 +806,7 @@ static struct domain *onearg_domain(struct connection *conn,
 		return ERR_PTR(-EINVAL);
 
 	domid = atoi(domid_str);
-	if (!domid)
+	if (domid == dom0_domid)
 		return ERR_PTR(-EINVAL);
 
 	return find_connected_domain(domid);
@@ -1009,7 +1009,7 @@ static int chk_domain_generation(unsigned int domid, uint64_t gen)
 {
 	struct domain *d;
 
-	if (!xc_handle && domid == 0)
+	if (!xc_handle && domid == dom0_domid)
 		return 1;
 
 	d = find_domain_struct(domid);
Signature · 1 line
2.35.3
#52 lore raw

PATCH 15/20 tools/xenstore: make domain_is_unprivileged() an inline function

In reply to Juergen Gross

clang is complaining about a NULL dereference for constructs like:

domain_is_unprivileged(conn) ? conn->in : NULL

as it can't know that domain_is_unprivileged(conn) will return false if conn is NULL.

Fix that by making domain_is_unprivileged() an inline function (and related to that domid_is_unprivileged(), too).

In order not having to make struct domain public, use conn->id instead of conn->domain->domid for the test.

---
 tools/xenstore/xenstored_core.h   | 10 ++++++++++
 tools/xenstore/xenstored_domain.c | 11 -----------
 tools/xenstore/xenstored_domain.h |  2 --
 3 files changed, 10 insertions(+), 13 deletions(-)
tools/xenstore/xenstored_core.h +10 -0
index 37006d508d..3c4e27d0dd 100644
@@ -297,6 +297,16 @@ void unmap_xenbus(void *interface);
 
 static inline int xenbus_master_domid(void) { return dom0_domid; }
 
+static inline bool domid_is_unprivileged(unsigned int domid)
+{
+	return domid != dom0_domid && domid != priv_domid;
+}
+
+static inline bool domain_is_unprivileged(const struct connection *conn)
+{
+	return conn && domid_is_unprivileged(conn->id);
+}
+
 /* Return the event channel used by xenbus. */
 evtchn_port_t xenbus_evtchn(void);
 
tools/xenstore/xenstored_domain.c +0 -11
index 0f1c903ee6..368b9bc2b7 100644
@@ -412,17 +412,6 @@ void handle_event(void)
 		barf_perror("Failed to write to event fd");
 }
 
-static bool domid_is_unprivileged(unsigned int domid)
-{
-	return domid != dom0_domid && domid != priv_domid;
-}
-
-bool domain_is_unprivileged(struct connection *conn)
-{
-	return conn && conn->domain &&
-	       domid_is_unprivileged(conn->domain->domid);
-}
-
 static char *talloc_domain_path(const void *context, unsigned int domid)
 {
 	return talloc_asprintf(context, "/local/domain/%u", domid);
tools/xenstore/xenstored_domain.h +0 -2
index 1e402f2609..22996e2576 100644
@@ -59,8 +59,6 @@ void ignore_connection(struct connection *conn, unsigned int err);
 /* Returns the implicit path of a connection (only domains have this) */
 const char *get_implicit_path(const struct connection *conn);
 
-bool domain_is_unprivileged(struct connection *conn);
-
 /* Remove node permissions for no longer existing domains. */
 int domain_adjust_node_perms(struct node *node);
 int domain_alloc_permrefs(struct node_perms *perms);
Signature · 1 line
2.35.3
#53 lore raw

Re: [PATCH 15/20] tools/xenstore: make domain_is_unprivileged() an inline function

Hi Juergen,

Quoted 3 lines · Juergen Gross

clang is complaining about a NULL dereference for constructs like:

   domain_is_unprivileged(conn) ? conn->in : NULL

I have just build xenstored with clang 11 and didn't get a complain. So can you provide more details?

Cheers,

Signature · 1 line
Julien Grall
#54 lore raw

Re: [PATCH 15/20] tools/xenstore: make domain_is_unprivileged() an inline function

Quoted 8 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

clang is complaining about a NULL dereference for constructs like:

   domain_is_unprivileged(conn) ? conn->in : NULL

I have just build xenstored with clang 11 and didn't get a complain. So can you provide more details?

It was reported by Edwin during development of the XSA series:

Quoted 6 lines · Edwin Torok

xenstored_watch.c:152:39: warning: Access to field 'in' results in a dereference of a null pointer (loaded from variable 'conn') [core.NullDereference]

         req = domain_is_unprivileged(conn) ? conn->in : NULL;
                                              ^~~~~~~~

1 warning generated.

clang 14 says this is a NULL dereference ...

You even responded to that report, BTW.

Juergen

Acked-by
#55 lore raw

Re: [PATCH 15/20] tools/xenstore: make domain_is_unprivileged() an inline function

Hi,

Quoted 14 lines · Juergen Gross
Quoted 8 lines · Julien Grall

Hi Juergen,

Quoted 3 lines · Juergen Gross

clang is complaining about a NULL dereference for constructs like:

   domain_is_unprivileged(conn) ? conn->in : NULL

I have just build xenstored with clang 11 and didn't get a complain. So can you provide more details?

It was reported by Edwin during development of the XSA series:

Quoted 1 line · Edwin Torok

xenstored_watch.c:152:39: warning: Access to field 'in' results in a

Quoted 8 lines

dereference of a null pointer (loaded from variable 'conn') [core.NullDereference]

Quoted 5 lines

          req = domain_is_unprivileged(conn) ? conn->in : NULL; ^~~~~~~~ 1 warning generated.

clang 14 says this is a NULL dereference ...

You even responded to that report, BTW.

I respond to a lot of e-mails and don't necessarily remember all of them :). That's why we have commit message.

In this case, you want to mention the compiler version in the commit message.

With that:

Cheers,

Signature · 1 line
Julien Grall
#56 lore raw

PATCH 16/20 tools/xenstore: let chk_domain_generation() return a bool

In reply to Juergen Gross

Instead of returning 0 or 1 let chk_domain_generation() return a boolean value.

Simplify the only caller by removing the ret variable.

---
 tools/xenstore/xenstored_domain.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
tools/xenstore/xenstored_domain.c +8 -10
index 368b9bc2b7..3d460a6fa8 100644
@@ -990,20 +990,20 @@ void domain_deinit(void)
  * count (used for testing whether a node permission is older than a domain).
  *
  * Return values:
- *  0: domain has higher generation count (it is younger than a node with the
- *     given count), or domain isn't existing any longer
- *  1: domain is older than the node
+ *  false: domain has higher generation count (it is younger than a node with
+ *     the given count), or domain isn't existing any longer
+ *  true: domain is older than the node
  */
-static int chk_domain_generation(unsigned int domid, uint64_t gen)
+static bool chk_domain_generation(unsigned int domid, uint64_t gen)
 {
 	struct domain *d;
 
 	if (!xc_handle && domid == dom0_domid)
-		return 1;
+		return true;
 
 	d = find_domain_struct(domid);
 
-	return (d && d->generation <= gen) ? 1 : 0;
+	return d && d->generation <= gen;
 }
 
 /*
@@ -1038,14 +1038,12 @@ int domain_alloc_permrefs(struct node_perms *perms)
 int domain_adjust_node_perms(struct node *node)
 {
 	unsigned int i;
-	int ret;
 
 	for (i = 1; i < node->perms.num; i++) {
 		if (node->perms.p[i].perms & XS_PERM_IGNORE)
 			continue;
-		ret = chk_domain_generation(node->perms.p[i].id,
-					    node->generation);
-		if (!ret)
+		if (!chk_domain_generation(node->perms.p[i].id,
+					   node->generation))
 			node->perms.p[i].perms |= XS_PERM_IGNORE;
 	}
 
Signature · 1 line
2.35.3
Reviewed-by on v1 16/20
#57 lore raw

Re: [PATCH 16/20] tools/xenstore: let chk_domain_generation() return a bool

Hi Juergen,

Quoted 6 lines · Juergen Gross

Instead of returning 0 or 1 let chk_domain_generation() return a boolean value.

Simplify the only caller by removing the ret variable.

Cheers,

Signature · 1 line
Julien Grall
#58 lore raw

PATCH 17/20 tools/xenstore: switch hashtable to use the talloc framework

In reply to Juergen Gross

Instead of using malloc() and friends, let the hashtable implementation use the talloc framework.

---
 tools/xenstore/hashtable.c        | 86 ++++++++++++-------------------
 tools/xenstore/hashtable.h        |  3 +-
 tools/xenstore/xenstored_core.c   |  5 +-
 tools/xenstore/xenstored_domain.c |  2 +-
 4 files changed, 39 insertions(+), 57 deletions(-)
tools/xenstore/hashtable.c +34 -52
index 7a1548c490..1c8656e016 100644
@@ -6,6 +6,8 @@
 #include <string.h>
 #include <math.h>
 #include <stdint.h>
+#include <stdarg.h>
+#include "talloc.h"
 
 struct entry
 {
@@ -50,7 +52,7 @@ indexFor(unsigned int tablelength, unsigned int hashvalue) {
 
 /*****************************************************************************/
 struct hashtable *
-create_hashtable(unsigned int minsize,
+create_hashtable(const void *ctx, unsigned int minsize,
                  unsigned int (*hashf) (void*),
                  int (*eqf) (void*,void*),
                  unsigned int flags)
@@ -66,10 +68,10 @@ create_hashtable(unsigned int minsize,
         if (primes[pindex] > minsize) { size = primes[pindex]; break; }
     }
 
-    h = (struct hashtable *)calloc(1, sizeof(struct hashtable));
+    h = talloc_zero(ctx, struct hashtable);
     if (NULL == h)
         goto err0;
-    h->table = (struct entry **)calloc(size, sizeof(struct entry *));
+    h->table = talloc_zero_array(h, struct entry *, size);
     if (NULL == h->table)
         goto err1;
 
@@ -83,7 +85,7 @@ create_hashtable(unsigned int minsize,
     return h;
 
 err1:
-   free(h);
+   talloc_free(h);
 err0:
    return NULL;
 }
@@ -115,47 +117,32 @@ hashtable_expand(struct hashtable *h)
     if (h->primeindex == (prime_table_length - 1)) return 0;
     newsize = primes[++(h->primeindex)];
 
-    newtable = (struct entry **)calloc(newsize, sizeof(struct entry*));
-    if (NULL != newtable)
+    newtable = talloc_realloc(h, h->table, struct entry *, newsize);
+    if (!newtable)
     {
-        /* This algorithm is not 'stable'. ie. it reverses the list
-         * when it transfers entries between the tables */
-        for (i = 0; i < h->tablelength; i++) {
-            while (NULL != (e = h->table[i])) {
-                h->table[i] = e->next;
-                index = indexFor(newsize,e->h);
+        h->primeindex--;
+        return 0;
+    }
+
+    h->table = newtable;
+    memset(newtable + h->tablelength, 0,
+           (newsize - h->tablelength) * sizeof(*newtable));
+    for (i = 0; i < h->tablelength; i++) {
+        for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) {
+            index = indexFor(newsize,e->h);
+            if (index == i)
+            {
+                pE = &(e->next);
+            }
+            else
+            {
+                *pE = e->next;
                 e->next = newtable[index];
                 newtable[index] = e;
             }
         }
-        free(h->table);
-        h->table = newtable;
-    }
-    /* Plan B: realloc instead */
-    else 
-    {
-        newtable = (struct entry **)
-                   realloc(h->table, newsize * sizeof(struct entry *));
-        if (NULL == newtable) { (h->primeindex)--; return 0; }
-        h->table = newtable;
-        memset(newtable + h->tablelength, 0,
-               (newsize - h->tablelength) * sizeof(*newtable));
-        for (i = 0; i < h->tablelength; i++) {
-            for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) {
-                index = indexFor(newsize,e->h);
-                if (index == i)
-                {
-                    pE = &(e->next);
-                }
-                else
-                {
-                    *pE = e->next;
-                    e->next = newtable[index];
-                    newtable[index] = e;
-                }
-            }
-        }
     }
+
     h->tablelength = newsize;
     h->loadlimit   = (unsigned int)
         (((uint64_t)newsize * max_load_factor) / 100);
@@ -184,7 +171,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
          * element may be ok. Next time we insert, we'll try expanding again.*/
         hashtable_expand(h);
     }
-    e = (struct entry *)calloc(1, sizeof(struct entry));
+    e = talloc_zero(h, struct entry);
     if (NULL == e) { --(h->entrycount); return 0; } /*oom*/
     e->h = hash(h,k);
     index = indexFor(h->tablelength,e->h);
@@ -238,8 +225,8 @@ hashtable_remove(struct hashtable *h, void *k)
             h->entrycount--;
             v = e->v;
             if (h->flags & HASHTABLE_FREE_KEY)
-                free(e->k);
-            free(e);
+                talloc_free(e->k);
+            talloc_free(e);
             return v;
         }
         pE = &(e->next);
@@ -280,25 +267,20 @@ void
 hashtable_destroy(struct hashtable *h)
 {
     unsigned int i;
-    struct entry *e, *f;
+    struct entry *e;
     struct entry **table = h->table;
 
     for (i = 0; i < h->tablelength; i++)
     {
-        e = table[i];
-        while (NULL != e)
+        for (e = table[i]; e; e = e->next)
         {
-            f = e;
-            e = e->next;
             if (h->flags & HASHTABLE_FREE_KEY)
-                free(f->k);
+                talloc_free(e->k);
             if (h->flags & HASHTABLE_FREE_VALUE)
-                free(f->v);
-            free(f);
+                talloc_free(e->v);
         }
     }
-    free(h->table);
-    free(h);
+    talloc_free(h);
 }
 
 /*
tools/xenstore/hashtable.h +2 -1
index b31eeaea26..07fdc1a82b 100644
@@ -9,6 +9,7 @@ struct hashtable;
  * create_hashtable
    
  * @name                    create_hashtable
+ * @param   ctx             talloc context to use for allocations
  * @param   minsize         minimum initial size of hashtable
  * @param   hashfunction    function for hashing keys
  * @param   key_eq_fn       function for determining key equality
@@ -22,7 +23,7 @@ struct hashtable;
 #define HASHTABLE_FREE_KEY   (1U << 1)
 
 struct hashtable *
-create_hashtable(unsigned int minsize,
+create_hashtable(const void *ctx, unsigned int minsize,
                  unsigned int (*hashfunction) (void*),
                  int (*key_eq_fn) (void*,void*),
                  unsigned int flags
tools/xenstore/xenstored_core.c +2 -3
index 5013f8b909..9361ee7696 100644
@@ -2410,11 +2410,10 @@ static int keys_equal_fn(void *key1, void *key2)
 
 int remember_string(struct hashtable *hash, const char *str)
 {
-	char *k = malloc(strlen(str) + 1);
+	char *k = talloc_strdup(NULL, str);
 
 	if (!k)
 		return 0;
-	strcpy(k, str);
 	return hashtable_insert(hash, k, (void *)1);
 }
 
@@ -2509,7 +2508,7 @@ void check_store(void)
 	};
 
 	/* Don't free values (they are all void *1) */
-	reachable = create_hashtable(16, hash_from_key_fn, keys_equal_fn,
+	reachable = create_hashtable(NULL, 16, hash_from_key_fn, keys_equal_fn,
 				     HASHTABLE_FREE_KEY);
 	if (!reachable) {
 		log("check_store: ENOMEM");
tools/xenstore/xenstored_domain.c +1 -1
index 3d460a6fa8..0bf35cd353 100644
@@ -936,7 +936,7 @@ void domain_init(int evtfd)
 	int rc;
 
 	/* Start with a random rather low domain count for the hashtable. */
-	domhash = create_hashtable(8, domhash_fn, domeq_fn, 0);
+	domhash = create_hashtable(NULL, 8, domhash_fn, domeq_fn, 0);
 	if (!domhash)
 		barf_perror("Failed to allocate domain hashtable");
 
Signature · 1 line
2.35.3
#59 lore raw

Re: [PATCH 17/20] tools/xenstore: switch hashtable to use the talloc framework

Hi Juergen,

Quoted 2 lines · Juergen Gross

Instead of using malloc() and friends, let the hashtable implementation use the talloc framework.

Can you outline why switching to talloc() is better?

Cheers,

Signature · 1 line
Julien Grall
#60 lore raw

Re: [PATCH 17/20] tools/xenstore: switch hashtable to use the talloc framework

Quoted 7 lines · Julien Grall

Hi Juergen,

Quoted 2 lines · Juergen Gross

Instead of using malloc() and friends, let the hashtable implementation use the talloc framework.

Can you outline why switching to talloc() is better?

It is more consistent within xenstored, and it allows to track memory usage easier (see "xenstore-control memreport" output).

Juergen

#61 lore raw

PATCH 18/20 tools/xenstore: make log macro globally available

In reply to Juergen Gross

Move the definition of the log() macro to xenstored_core.h in order to make it usable from other source files, too.

While at it preserve errno from being modified.

---
V2:
- preserve errno (Julien Grall)

---
 tools/xenstore/xenstored_core.c | 14 --------------
 tools/xenstore/xenstored_core.h | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 14 deletions(-)
tools/xenstore/xenstored_core.c +0 -14
index 9361ee7696..e79d82cbff 100644
@@ -88,20 +88,6 @@ TDB_CONTEXT *tdb_ctx = NULL;
 
 static const char *sockmsg_string(enum xsd_sockmsg_type type);
 
-#define log(...)							\
-	do {								\
-		char *s = talloc_asprintf(NULL, __VA_ARGS__);		\
-		if (s) {						\
-			trace("%s\n", s);				\
-			syslog(LOG_ERR, "%s\n",  s);			\
-			talloc_free(s);					\
-		} else {						\
-			trace("talloc failure during logging\n");	\
-			syslog(LOG_ERR, "talloc failure during logging\n"); \
-		}							\
-	} while (0)
-
-
 int quota_nb_entry_per_domain = 1000;
 int quota_nb_watch_per_domain = 128;
 int quota_max_entry_size = 2048; /* 2K */
tools/xenstore/xenstored_core.h +15 -0
index 3c4e27d0dd..3b96ecd018 100644
@@ -267,6 +267,21 @@ void trace(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
 void reopen_log(void);
 void close_log(void);
 
+#define log(...)							\
+	do {								\
+		int _saved_errno = errno;				\
+		char *s = talloc_asprintf(NULL, __VA_ARGS__);		\
+		if (s) {						\
+			trace("%s\n", s);				\
+			syslog(LOG_ERR, "%s\n",	s);			\
+			talloc_free(s);					\
+		} else {						\
+			trace("talloc failure during logging\n");	\
+			syslog(LOG_ERR, "talloc failure during logging\n"); \
+		}							\
+		errno = _saved_errno;					\
+	} while (0)
+
 extern int orig_argc;
 extern char **orig_argv;
 
Signature · 1 line
2.35.3
Reviewed-by on v1 18/20
#62 lore raw

Re: [PATCH 18/20] tools/xenstore: make log macro globally available

Hi Juergen,

Quoted 6 lines · Juergen Gross

Move the definition of the log() macro to xenstored_core.h in order to make it usable from other source files, too.

While at it preserve errno from being modified.

Cheers,

Signature · 1 line
Julien Grall
#63 lore raw

PATCH 19/20 tools/xenstore: introduce trace classes

In reply to Juergen Gross

Make the xenstored internal trace configurable by adding classes which can be switched on and off independently from each other.

Define the following classes:

- obj: Creation and deletion of interesting "objects" (watch, transaction, connection)
- io: incoming requests and outgoing responses
- wrl: write limiting

Per default "obj" and "io" are switched on.

Entries written via trace() will always be printed (if tracing is on at all).

Rename the misnamed xenstore-control commands "logfile" to "tracefile" and "log" to "trace".

Add the capability to control the trace settings via the "trace" command and via a new "--trace-control" command line option.

Add a missing trace_create() call for creating a transaction.

---
 docs/misc/xenstore.txt                 | 18 +++++++----
 tools/xenstore/xenstored_control.c     | 41 +++++++++++++++++++-----
 tools/xenstore/xenstored_core.c        | 44 +++++++++++++++++++++++---
 tools/xenstore/xenstored_core.h        |  6 ++++
 tools/xenstore/xenstored_domain.c      | 27 ++++++++--------
 tools/xenstore/xenstored_transaction.c |  1 +
 6 files changed, 105 insertions(+), 32 deletions(-)
docs/misc/xenstore.txt +11 -7
index 44428ae3a7..9db0385120 100644
@@ -409,14 +409,8 @@ CONTROL			<command>|[<parameters>|]
 		error string in case of failure. -s can return "BUSY" in case
 		of an active transaction, a retry of -s can be done in that
 		case.
-	log|on
-		turn xenstore logging on
-	log|off
-		turn xenstore logging off
-	logfile|<file-name>
-		log to specified file
 	memreport|[<file-name>]
-		print memory statistics to logfile (no <file-name>
+		print memory statistics to tracefile (no <file-name>
 		specified) or to specific file
 	print|<string>
 		print <string> to syslog (xenstore runs as daemon) or
@@ -432,6 +426,16 @@ CONTROL			<command>|[<parameters>|]
 		the domain <domid>
 	quota-soft|[set <name> <val>]
 		like the "quota" command, but for soft-quota.
+	trace|[+|-<switch>]
+		without parameters: show possible trace switches
+		+<switch> activates trace entries for <switch>,
+		-<switch> deactivates trace entries for <switch>
+	trace|on
+		turn xenstore tracing on
+	trace|off
+		turn xenstore tracing off
+	tracefile|<file-name>
+		trace to specified file
 	help			<supported-commands>
 		return list of supported commands for CONTROL
 
tools/xenstore/xenstored_control.c +33 -8

72 lines not on this page · raw message

tools/xenstore/xenstored_core.c +40 -4

95 lines not on this page · raw message

tools/xenstore/xenstored_core.h +6 -0
index 3b96ecd018..c85b15515c 100644
@@ -287,6 +287,12 @@ extern char **orig_argv;
 
 extern char *tracefile;
 extern int tracefd;
+extern unsigned int trace_flags;
+#define TRACE_OBJ	0x00000001
+#define TRACE_IO	0x00000002
+#define TRACE_WRL	0x00000004
+extern const char *trace_switches[];
+int set_trace_switch(const char *arg);
 
 extern TDB_CONTEXT *tdb_ctx;
 extern int dom0_domid;
tools/xenstore/xenstored_domain.c +14 -13
index 0bf35cd353..70d9e9572d 100644
@@ -1256,6 +1256,12 @@ static long wrl_ndomains;
 static wrl_creditt wrl_reserve; /* [-wrl_config_newdoms_dburst, +_gburst ] */
 static time_t wrl_log_last_warning; /* 0: no previous warning */
 
+#define trace_wrl(...)				\
+do {						\
+	if (trace_flags & TRACE_WRL)		\
+		trace("wrl: " __VA_ARGS__);	\
+} while (0)
+
 void wrl_gettime_now(struct wrl_timestampt *now_wt)
 {
 	struct timespec now_ts;
@@ -1361,12 +1367,9 @@ void wrl_credit_update(struct domain *domain, struct wrl_timestampt now)
 
 	domain->wrl_timestamp = now;
 
-	trace("wrl: dom %4d %6ld  msec  %9ld credit   %9ld reserve"
-	      "  %9ld discard\n",
-	      domain->domid,
-	      msec,
-	      (long)domain->wrl_credit, (long)wrl_reserve,
-	      (long)surplus);
+	trace_wrl("dom %4d %6ld msec %9ld credit %9ld reserve %9ld discard\n",
+		  domain->domid, msec, (long)domain->wrl_credit,
+		  (long)wrl_reserve, (long)surplus);
 }
 
 void wrl_check_timeout(struct domain *domain,
@@ -1398,10 +1401,9 @@ void wrl_check_timeout(struct domain *domain,
 	if (*ptimeout==-1 || wakeup < *ptimeout)
 		*ptimeout = wakeup;
 
-	trace("wrl: domain %u credit=%ld (reserve=%ld) SLEEPING for %d\n",
-	      domain->domid,
-	      (long)domain->wrl_credit, (long)wrl_reserve,
-	      wakeup);
+	trace_wrl("domain %u credit=%ld (reserve=%ld) SLEEPING for %d\n",
+		  domain->domid, (long)domain->wrl_credit, (long)wrl_reserve,
+		  wakeup);
 }
 
 #define WRL_LOG(now, ...) \
@@ -1419,9 +1421,8 @@ void wrl_apply_debit_actual(struct domain *domain)
 	wrl_credit_update(domain, now);
 
 	domain->wrl_credit -= wrl_config_writecost;
-	trace("wrl: domain %u credit=%ld (reserve=%ld)\n",
-	      domain->domid,
-	      (long)domain->wrl_credit, (long)wrl_reserve);
+	trace_wrl("domain %u credit=%ld (reserve=%ld)\n", domain->domid,
+		  (long)domain->wrl_credit, (long)wrl_reserve);
 
 	if (domain->wrl_credit < 0) {
 		if (!domain->wrl_delay_logged) {
tools/xenstore/xenstored_transaction.c +1 -0
index 82e5e66c18..1aa9d3cb3d 100644
@@ -475,6 +475,7 @@ int do_transaction_start(const void *ctx, struct connection *conn,
 	if (!trans)
 		return ENOMEM;
 
+	trace_create(trans, "transaction");
 	INIT_LIST_HEAD(&trans->accessed);
 	INIT_LIST_HEAD(&trans->changed_domains);
 	trans->conn = conn;
Signature · 1 line
2.35.3
#64 lore raw

Re: [PATCH 19/20] tools/xenstore: introduce trace classes

Hi Juergen,

I haven't yet look at the code itself. I wanted to comment on the external interfaces.

Quoted 10 lines · Juergen Gross

Make the xenstored internal trace configurable by adding classes which can be switched on and off independently from each other.

Define the following classes:

- obj: Creation and deletion of interesting "objects" (watch, transaction, connection)
- io: incoming requests and outgoing responses
- wrl: write limiting

Quoted 7 lines

Per default "obj" and "io" are switched on.

Entries written via trace() will always be printed (if tracing is on at all).

Rename the misnamed xenstore-control commands "logfile" to "tracefile" and "log" to "trace".

While I understand this may be misnamed, this also means that there is an ABI breakage between current Xenstored and the future one.

I am not convinced this is justified. Therefore, I think we should at minimum keep the compatibility.

Quoted 21 lines

Add the capability to control the trace settings via the "trace" command and via a new "--trace-control" command line option.

Add a missing trace_create() call for creating a transaction.

---
  docs/misc/xenstore.txt                 | 18 +++++++----
  tools/xenstore/xenstored_control.c     | 41 +++++++++++++++++++-----
  tools/xenstore/xenstored_core.c        | 44 +++++++++++++++++++++++---
  tools/xenstore/xenstored_core.h        |  6 ++++
  tools/xenstore/xenstored_domain.c      | 27 ++++++++--------
  tools/xenstore/xenstored_transaction.c |  1 +
  6 files changed, 105 insertions(+), 32 deletions(-)
docs/misc/xenstore.txt +0 -0
index 44428ae3a7..9db0385120 100644

docs/misc/xenstore.txt CONTROL <command>|[<parameters>|] · line 417

Quoted 9 lines
docs/misc/xenstore.txt +0 -6
@@ -409,14 +409,8 @@ CONTROL			<command>|[<parameters>|]
  		error string in case of failure. -s can return "BUSY" in case
  		of an active transaction, a retry of -s can be done in that
  		case.
-	log|on
-		turn xenstore logging on
-	log|off
-		turn xenstore logging off
-	logfile|<file-name>
-		log to specified file

Technically xenstore.txt is meant to describe an interface that is implementation agnostics. Can you outline in the documentation why removing them is fine?

docs/misc/xenstore.txt CONTROL <command>|[<parameters>|] · line 429

Quoted 11 lines
docs/misc/xenstore.txt +2 -1
  	memreport|[<file-name>]
-		print memory statistics to logfile (no <file-name>
+		print memory statistics to tracefile (no <file-name>
  		specified) or to specific file
  	print|<string>
  		print <string> to syslog (xenstore runs as daemon) or
@@ -432,6 +426,16 @@ CONTROL			<command>|[<parameters>|]
  		the domain <domid>
  	quota-soft|[set <name> <val>]
  		like the "quota" command, but for soft-quota.
+	trace|[+|-<switch>]

The regex here is a bit ambiguous because it technically means either "+" or "-<switch>". Furthermore, within this docs, there are case where | is included in between [] to indicate the this is terminated by a \0.

So it would be better to define it over 3 lines:

trace
trace|+<switch>
trace|-<switch>

I think it would be fine if there is only one paragraph of explanation for the 3.

docs/misc/xenstore.txt CONTROL <command>|[<parameters>|] · line 441

Quoted 12 lines
docs/misc/xenstore.txt +9 -0
+		without parameters: show possible trace switches
+		+<switch> activates trace entries for <switch>,
+		-<switch> deactivates trace entries for <switch>
+	trace|on
+		turn xenstore tracing on
+	trace|off
+		turn xenstore tracing off
+	tracefile|<file-name>
+		trace to specified file
  	help			<supported-commands>
  		return list of supported commands for CONTROL
 

Cheers,

Signature · 1 line
Julien Grall
#65 lore raw

Re: [PATCH 19/20] tools/xenstore: introduce trace classes

Quoted 21 lines · Julien Grall

Hi Juergen,

I haven't yet look at the code itself. I wanted to comment on the external interfaces.

Quoted 15 lines · Juergen Gross

Make the xenstored internal trace configurable by adding classes which can be switched on and off independently from each other.

Define the following classes:

- obj: Creation and deletion of interesting "objects" (watch, transaction, connection)
- io: incoming requests and outgoing responses
- wrl: write limiting

Per default "obj" and "io" are switched on.

Entries written via trace() will always be printed (if tracing is on at all).

Quoted 6 lines
Quoted 2 lines

Rename the misnamed xenstore-control commands "logfile" to "tracefile" and "log" to "trace".

While I understand this may be misnamed, this also means that there is an ABI breakage between current Xenstored and the future one.

I am not convinced this is justified. Therefore, I think we should at minimum keep the compatibility.

Hmm, I can see your point. Given that this might be one of the most common used xenstore-control commands I'm not opposed to keep the current names.

Quoted 48 lines
Quoted 23 lines

Add the capability to control the trace settings via the "trace" command and via a new "--trace-control" command line option.

Add a missing trace_create() call for creating a transaction.

---
  docs/misc/xenstore.txt                 | 18 +++++++---- tools/xenstore/xenstored_control.c     | 41 +++++++++++++++++++----- tools/xenstore/xenstored_core.c        | 44 +++++++++++++++++++++++--- tools/xenstore/xenstored_core.h        |  6 ++++ tools/xenstore/xenstored_domain.c      | 27 ++++++++-------- tools/xenstore/xenstored_transaction.c |  1 +

  6 files changed, 105 insertions(+), 32 deletions(-)
docs/misc/xenstore.txt +0 -0
index 44428ae3a7..9db0385120 100644
@@ -409,14 +409,8 @@ CONTROL            <command>|[<parameters>|]

          error string in case of failure. -s can return "BUSY" in case

Quoted 8 lines

          of an active transaction, a retry of -s can be done in that case.
-    log|on
-        turn xenstore logging on
-    log|off
-        turn xenstore logging off
-    logfile|<file-name>
-        log to specified file

Technically xenstore.txt is meant to describe an interface that is implementation agnostics. Can you outline in the documentation why removing them is fine?

Quoted 11 lines

      memreport|[<file-name>]
-        print memory statistics to logfile (no <file-name> +        print memory statistics to tracefile (no <file-name> specified) or to specific file print|<string>
          print <string> to syslog (xenstore runs as daemon) or

docs/misc/xenstore.txt +0 -0
@@ -432,6 +426,16 @@ CONTROL            <command>|[<parameters>|]

          the domain <domid>
      quota-soft|[set <name> <val>]
          like the "quota" command, but for soft-quota. +    trace|[+|-<switch>]

The regex here is a bit ambiguous because it technically means either "+" or "-<switch>". Furthermore, within this docs, there are case where | is included in between [] to indicate the this is terminated by a \0.

Quoted 7 lines

So it would be better to define it over 3 lines:

trace
trace|+<switch>
trace|-<switch>

I think it would be fine if there is only one paragraph of explanation for the 3.

Okay.

Juergen

#66 lore raw

PATCH 20/20 tools/xenstore: let check_store() check the accounting data

In reply to Juergen Gross

Today check_store() is only testing the correctness of the node tree.

Add verification of the accounting data (number of nodes) and correct the data if it is wrong.

Do the initial check_store() call only after Xenstore entries of a live update have been read.

---
 tools/xenstore/xenstored_core.c   | 62 ++++++++++++++++------
 tools/xenstore/xenstored_domain.c | 86 +++++++++++++++++++++++++++++++
 tools/xenstore/xenstored_domain.h |  4 ++
 3 files changed, 137 insertions(+), 15 deletions(-)
tools/xenstore/xenstored_core.c +47 -15
index 86ef511ac0..ea0e6292d3 100644
@@ -2375,8 +2375,6 @@ void setup_structure(bool live_update)
 		manual_node("@introduceDomain", NULL);
 		domain_nbentry_fix(dom0_domid, 5, true);
 	}
-
-	check_store();
 }
 
 static unsigned int hash_from_key_fn(void *k)
@@ -2419,20 +2417,28 @@ int remember_string(struct hashtable *hash, const char *str)
  * As we go, we record each node in the given reachable hashtable.  These
  * entries will be used later in clean_store.
  */
+
+struct check_store_data {
+	struct hashtable *reachable;
+	struct hashtable *domains;
+};
+
 static int check_store_step(const void *ctx, struct connection *conn,
 			    struct node *node, void *arg)
 {
-	struct hashtable *reachable = arg;
+	struct check_store_data *data = arg;
 
-	if (hashtable_search(reachable, (void *)node->name)) {
+	if (hashtable_search(data->reachable, (void *)node->name)) {
 		log("check_store: '%s' is duplicated!", node->name);
 		return recovery ? WALK_TREE_RM_CHILDENTRY
 				: WALK_TREE_SKIP_CHILDREN;
 	}
 
-	if (!remember_string(reachable, node->name))
+	if (!remember_string(data->reachable, node->name))
 		return WALK_TREE_ERROR_STOP;
 
+	domain_check_acc_add(node, data->domains);
+
 	return WALK_TREE_OK;
 }
 
@@ -2482,37 +2488,61 @@ static int clean_store_(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA val,
  * Given the list of reachable nodes, iterate over the whole store, and
  * remove any that were not reached.
  */
-static void clean_store(struct hashtable *reachable)
+static void clean_store(struct check_store_data *data)
 {
-	tdb_traverse(tdb_ctx, &clean_store_, reachable);
+	tdb_traverse(tdb_ctx, &clean_store_, data->reachable);
+	domain_check_acc(data->domains);
 }
 
+int check_store_path(const char *name, struct check_store_data *data)
+{
+	struct node *node;
+
+	node = read_node(NULL, NULL, name);
+	if (!node) {
+		log("check_store: error %d reading special node '%s'", errno,
+		    name);
+		return errno;
+	}
+
+	return check_store_step(NULL, NULL, node, data);
+}
 
 void check_store(void)
 {
-	struct hashtable *reachable;
 	struct walk_funcs walkfuncs = {
 		.enter = check_store_step,
 		.enoent = check_store_enoent,
 	};
+	struct check_store_data data;
 
 	/* Don't free values (they are all void *1) */
-	reachable = create_hashtable(NULL, 16, hash_from_key_fn, keys_equal_fn,
-				     HASHTABLE_FREE_KEY);
-	if (!reachable) {
+	data.reachable = create_hashtable(NULL, 16, hash_from_key_fn,
+					  keys_equal_fn, HASHTABLE_FREE_KEY);
+	if (!data.reachable) {
 		log("check_store: ENOMEM");
 		return;
 	}
 
+	data.domains = domain_check_acc_init();
+	if (!data.domains) {
+		log("check_store: ENOMEM");
+		goto out_hash;
+	}
+
 	log("Checking store ...");
-	if (walk_node_tree(NULL, NULL, "/", &walkfuncs, reachable)) {
+	if (walk_node_tree(NULL, NULL, "/", &walkfuncs, &data)) {
 		if (errno == ENOMEM)
 			log("check_store: ENOMEM");
-	} else if (!check_transactions(reachable))
-		clean_store(reachable);
+	} else if (!check_store_path("@introduceDomain", &data) &&
+		   !check_store_path("@releaseDomain", &data) &&
+		   !check_transactions(data.reachable))
+		clean_store(&data);
 	log("Checking store complete.");
 
-	hashtable_destroy(reachable);
+	hashtable_destroy(data.domains);
+ out_hash:
+	hashtable_destroy(data.reachable);
 }
 
 
@@ -2900,6 +2930,8 @@ int main(int argc, char *argv[])
 		lu_read_state();
 #endif
 
+	check_store();
+
 	/* Get ready to listen to the tools. */
 	initialize_fds(&sock_pollfd_idx, &timeout);
 
tools/xenstore/xenstored_domain.c +86 -0

92 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +4 -0
index 22996e2576..dc4660861e 100644
@@ -129,4 +129,8 @@ const char *dump_state_connections(FILE *fp);
 
 void read_state_connection(const void *ctx, const void *state);
 
+struct hashtable *domain_check_acc_init(void);
+void domain_check_acc_add(const struct node *node, struct hashtable *domains);
+void domain_check_acc(struct hashtable *domains);
+
 #endif /* _XENSTORED_DOMAIN_H */
Signature · 1 line
2.35.3