Skip to contentSkip to conversation

tools/xenstore: rework internal accounting

PATCH Superseded Juergen Gross 2 participants 37 messages Apr 5, 2023 – May 4, 2023

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover tools/xenstore: rework internal accounting

This series reworks the Xenstore internal accounting to use a uniform generic framework. It is adding some additional useful diagnostic information, like accounting trace and max. per-domain and global quota values seen.

Changes in V2:
- added patch 1 (leftover from previous series)
- rebase

Changes in V3:
- addressed comments

Changes in V4:
- fixed patch 3

Juergen Gross (13):
tools/xenstore: take transaction internal nodes into account for quota tools/xenstore: manage per-transaction domain accounting data in an
array
tools/xenstore: introduce accounting data array for per-domain values tools/xenstore: add framework to commit accounting data on success
only
tools/xenstore: use accounting buffering for node accounting tools/xenstore: add current connection to domain_memory_add()
parameters
tools/xenstore: use accounting data array for per-domain values tools/xenstore: add accounting trace support tools/xenstore: add TDB access trace support tools/xenstore: switch transaction accounting to generic accounting tools/xenstore: remember global and per domain max accounting values tools/xenstore: use generic accounting for remaining quotas tools/xenstore: switch quota management to be table based

 docs/misc/xenstore.txt                 |   5 +-
 tools/xenstore/xenstored_control.c     |  65 ++--
 tools/xenstore/xenstored_core.c        | 168 +++++-----
 tools/xenstore/xenstored_core.h        |  24 +-
 tools/xenstore/xenstored_domain.c      | 433 ++++++++++++++++++-------
 tools/xenstore/xenstored_domain.h      |  60 +++-
 tools/xenstore/xenstored_transaction.c |  22 +-
 tools/xenstore/xenstored_watch.c       |  15 +-
 8 files changed, 514 insertions(+), 278 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v4 1/13 tools/xenstore: take transaction internal nodes into account for quota

The accounting for the number of nodes of a domain in an active transaction is not working correctly, as it is checking the node quota only against the number of nodes outside the transaction.

This can result in the transaction finally failing, as node quota is checked at the end of the transaction again.

On the other hand even in a transaction deleting many nodes, new nodes might not be creatable, in case the node quota was already reached at the start of the transaction.

---
V3:
- rewrite of commit message (Julien Grall)

---
 tools/xenstore/xenstored_domain.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/xenstore/xenstored_domain.c +2 -3
index f62be2245c..dbbf97accc 100644
@@ -1116,9 +1116,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
#2 lore raw

PATCH v4 2/13 tools/xenstore: manage per-transaction domain accounting data in an array

In reply to Juergen Gross

In order to prepare keeping accounting data in an array instead of using independent fields, switch the struct changed_domain accounting data to that scheme, for now only using an array with one element.

In order to be able to extend this scheme add the needed indexing enum to xenstored_domain.h.

---
V2:
- make "what" parameter of acc_add_changed_dom() an enum type, and assert() that it won't exceed the accounting array (Julien Grall)

---
 tools/xenstore/xenstored_domain.c | 19 +++++++++++--------
 tools/xenstore/xenstored_domain.h | 10 ++++++++++
 2 files changed, 21 insertions(+), 8 deletions(-)
tools/xenstore/xenstored_domain.c +11 -8
index dbbf97accc..609a9a13ab 100644
@@ -99,8 +99,8 @@ struct changed_domain
 	/* Identifier of the changed domain. */
 	unsigned int domid;
 
-	/* Amount by which this domain's nbentry field has changed. */
-	int nbentry;
+	/* Accounting data. */
+	int acc[ACC_TR_N];
 };
 
 static struct hashtable *domhash;
@@ -550,7 +550,7 @@ int acc_fix_domains(struct list_head *head, bool chk_quota, bool update)
 	int cnt;
 
 	list_for_each_entry(cd, head, list) {
-		cnt = domain_nbentry_fix(cd->domid, cd->nbentry, update);
+		cnt = domain_nbentry_fix(cd->domid, cd->acc[ACC_NODES], update);
 		if (!update) {
 			if (chk_quota && cnt >= quota_nb_entry_per_domain)
 				return ENOSPC;
@@ -595,19 +595,21 @@ static struct changed_domain *acc_get_changed_domain(const void *ctx,
 	return cd;
 }
 
-static int acc_add_dom_nbentry(const void *ctx, struct list_head *head, int val,
-			       unsigned int domid)
+static int acc_add_changed_dom(const void *ctx, struct list_head *head,
+			       enum accitem what, int val, unsigned int domid)
 {
 	struct changed_domain *cd;
 
+	assert(what < ARRAY_SIZE(cd->acc));
+
 	cd = acc_get_changed_domain(ctx, head, domid);
 	if (!cd)
 		return 0;
 
 	errno = 0;
-	cd->nbentry += val;
+	cd->acc[what] += val;
 
-	return cd->nbentry;
+	return cd->acc[what];
 }
 
 static void domain_conn_reset(struct domain *domain)
@@ -1071,7 +1073,8 @@ static int domain_nbentry_add(struct connection *conn, unsigned int domid,
 
 	if (conn && conn->transaction) {
 		head = transaction_get_changed_domains(conn->transaction);
-		ret = acc_add_dom_nbentry(conn->transaction, head, add, domid);
+		ret = acc_add_changed_dom(conn->transaction, head, ACC_NODES,
+					  add, domid);
 		if (errno) {
 			fail_transaction(conn->transaction);
 			return -1;
tools/xenstore/xenstored_domain.h +10 -0
index 279cccb3ad..40803574f6 100644
@@ -19,6 +19,16 @@
 #ifndef _XENSTORED_DOMAIN_H
 #define _XENSTORED_DOMAIN_H
 
+/*
+ * All accounting data is stored in a per-domain array.
+ * Depending on the account item there might be other scopes as well, like e.g.
+ * a per transaction array.
+ */
+enum accitem {
+	ACC_NODES,
+	ACC_TR_N,		/* Number of elements per transaction. */
+};
+
 void handle_event(void);
 
 void check_domains(void);
Signature · 1 line
2.35.3
#3 lore raw

PATCH v4 3/13 tools/xenstore: introduce accounting data array for per-domain values

In reply to Juergen Gross

Introduce the scheme of an accounting data array for per-domain accounting data and use it initially for the number of nodes owned by a domain.

Make the accounting data type to be unsigned int, as no data is allowed to be negative at any time.

---
V3:
- remove domid parameter from domain_acc_add_chk() (Julien Grall)
- rename domain_acc_add_chk() (Julien Grall)
- modify overflow check (Julien Grall)
V4:
- fix overflow check

---
 tools/xenstore/xenstored_domain.c | 70 ++++++++++++++++++-------------
 tools/xenstore/xenstored_domain.h |  3 +-
 2 files changed, 43 insertions(+), 30 deletions(-)
tools/xenstore/xenstored_domain.c +41 -29
index 609a9a13ab..30fb9acec6 100644
@@ -69,8 +69,8 @@ struct domain
 	/* Has domain been officially introduced? */
 	bool introduced;
 
-	/* number of entry from this domain in the store */
-	int nbentry;
+	/* Accounting data for this domain. */
+	unsigned int acc[ACC_N];
 
 	/* Amount of memory allocated for this domain. */
 	int memory;
@@ -246,7 +246,7 @@ static int domain_tree_remove_sub(const void *ctx, struct connection *conn,
 
 	if (keep_orphans) {
 		set_tdb_key(node->name, &key);
-		domain->nbentry--;
+		domain_nbentry_dec(NULL, domain->domid);
 		node->perms.p[0].id = priv_domid;
 		node->acc.memory = 0;
 		domain_nbentry_inc(NULL, priv_domid);
@@ -270,7 +270,7 @@ static int domain_tree_remove_sub(const void *ctx, struct connection *conn,
 		ret = WALK_TREE_SKIP_CHILDREN;
 	}
 
-	return domain->nbentry > 0 ? ret : WALK_TREE_SUCCESS_STOP;
+	return domain->acc[ACC_NODES] ? ret : WALK_TREE_SUCCESS_STOP;
 }
 
 static void domain_tree_remove(struct domain *domain)
@@ -278,7 +278,7 @@ static void domain_tree_remove(struct domain *domain)
 	int ret;
 	struct walk_funcs walkfuncs = { .enter = domain_tree_remove_sub };
 
-	if (domain->nbentry > 0) {
+	if (domain->acc[ACC_NODES]) {
 		ret = walk_node_tree(domain, NULL, "/", &walkfuncs, domain);
 		if (ret == WALK_TREE_ERROR_STOP)
 			syslog(LOG_ERR,
@@ -437,7 +437,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 	resp = talloc_asprintf_append(resp, "%-16s: %8d\n", #t, e); \
 	if (!resp) return ENOMEM
 
-	ent(nodes, d->nbentry);
+	ent(nodes, d->acc[ACC_NODES]);
 	ent(watches, d->nbwatch);
 	ent(transactions, ta);
 	ent(outstanding, d->nboutstanding);
@@ -1047,8 +1047,27 @@ int domain_adjust_node_perms(struct node *node)
 	return 0;
 }
 
-static int domain_nbentry_add(struct connection *conn, unsigned int domid,
-			      int add, bool no_dom_alloc)
+static int domain_acc_add_valid(struct domain *d, enum accitem what, int add)
+{
+	assert(what < ARRAY_SIZE(d->acc));
+
+	if ((add < 0 && -add > d->acc[what]) ||
+	    (add > 0 && (INT_MAX - d->acc[what]) < add)) {
+		/*
+		 * In a transaction when a node is being added/removed AND the
+		 * same node has been added/removed outside the transaction in
+		 * parallel, the resulting value will be wrong. This is no
+		 * problem, as the transaction will fail due to the resulting
+		 * conflict.
+		 */
+		return (add < 0) ? 0 : INT_MAX;
+	}
+
+	return d->acc[what] + add;
+}
+
+static int domain_acc_add(struct connection *conn, unsigned int domid,
+			  enum accitem what, int add, bool no_dom_alloc)
 {
 	struct domain *d;
 	struct list_head *head;
@@ -1071,56 +1090,49 @@ static int domain_nbentry_add(struct connection *conn, unsigned int domid,
 		}
 	}
 
-	if (conn && conn->transaction) {
+	if (conn && conn->transaction && what < ACC_TR_N) {
 		head = transaction_get_changed_domains(conn->transaction);
-		ret = acc_add_changed_dom(conn->transaction, head, ACC_NODES,
+		ret = acc_add_changed_dom(conn->transaction, head, what,
 					  add, domid);
 		if (errno) {
 			fail_transaction(conn->transaction);
 			return -1;
 		}
-		/*
-		 * In a transaction when a node is being added/removed AND the
-		 * same node has been added/removed outside the transaction in
-		 * parallel, the resulting number of nodes will be wrong. This
-		 * is no problem, as the transaction will fail due to the
-		 * resulting conflict.
-		 * In the node remove case the resulting number can be even
-		 * negative, which should be avoided.
-		 */
-		return max(d->nbentry + ret, 0);
+		return domain_acc_add_valid(d, what, ret);
 	}
 
-	d->nbentry += add;
+	d->acc[what] = domain_acc_add_valid(d, what, add);
 
-	return d->nbentry;
+	return d->acc[what];
 }
 
 int domain_nbentry_inc(struct connection *conn, unsigned int domid)
 {
-	return (domain_nbentry_add(conn, domid, 1, false) < 0) ? errno : 0;
+	return (domain_acc_add(conn, domid, ACC_NODES, 1, false) < 0)
+	       ? errno : 0;
 }
 
 int domain_nbentry_dec(struct connection *conn, unsigned int domid)
 {
-	return (domain_nbentry_add(conn, domid, -1, true) < 0) ? errno : 0;
+	return (domain_acc_add(conn, domid, ACC_NODES, -1, true) < 0)
+	       ? errno : 0;
 }
 
 int domain_nbentry_fix(unsigned int domid, int num, bool update)
 {
 	int ret;
 
-	ret = domain_nbentry_add(NULL, domid, update ? num : 0, update);
+	ret = domain_acc_add(NULL, domid, ACC_NODES, update ? num : 0, update);
 	if (ret < 0 || update)
 		return ret;
 
 	return domid_is_unprivileged(domid) ? ret + num : 0;
 }
 
-int domain_nbentry(struct connection *conn)
+unsigned int domain_nbentry(struct connection *conn)
 {
 	return domain_is_unprivileged(conn)
-	       ? domain_nbentry_add(conn, conn->id, 0, true) : 0;
+	       ? domain_acc_add(conn, conn->id, ACC_NODES, 0, true) : 0;
 }
 
 static bool domain_chk_quota(struct domain *domain, int mem)
@@ -1597,7 +1609,7 @@ static int domain_check_acc_init_sub(const void *k, void *v, void *arg)
 	 * If everything is correct incrementing the value for each node will
 	 * result in dom->nodes being 0 at the end.
 	 */
-	dom->nodes = -d->nbentry;
+	dom->nodes = -d->acc[ACC_NODES];
 
 	if (!hashtable_insert(domains, &dom->domid, dom)) {
 		talloc_free(dom);
@@ -1652,7 +1664,7 @@ static int domain_check_acc_cb(const void *k, void *v, void *arg)
 	if (!d)
 		return 0;
 
-	d->nbentry += dom->nodes;
+	d->acc[ACC_NODES] += dom->nodes;
 
 	return 0;
 }
tools/xenstore/xenstored_domain.h +2 -1
index 40803574f6..9d05eb01da 100644
@@ -27,6 +27,7 @@
 enum accitem {
 	ACC_NODES,
 	ACC_TR_N,		/* Number of elements per transaction. */
+	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
 };
 
 void handle_event(void);
@@ -77,7 +78,7 @@ int domain_alloc_permrefs(struct node_perms *perms);
 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);
+unsigned int domain_nbentry(struct connection *conn);
 int domain_memory_add(unsigned int domid, int mem, bool no_quota_check);
 
 /*
Signature · 1 line
2.35.3
Reviewed-by on v4 3/13
#4 lore raw

Re: [PATCH v4 03/13] tools/xenstore: introduce accounting data array for per-domain values

Hi Juergen,

Quoted 8 lines · Juergen Gross

Introduce the scheme of an accounting data array for per-domain accounting data and use it initially for the number of nodes owned by a domain.

Make the accounting data type to be unsigned int, as no data is allowed to be negative at any time.

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

PATCH v4 4/13 tools/xenstore: add framework to commit accounting data on success only

In reply to Juergen Gross

Instead of modifying accounting data and undo those modifications in case of an error during further processing, add a framework for collecting the needed changes and commit them only when the whole operation has succeeded.

This scheme can reuse large parts of the per transaction accounting. The changed_domain handling can be reused, but the array size of the accounting data should be possible to be different for both use cases.

---
V3:
- call acc_commit() earlier (Julien Grall)
- add assert() to acc_commit()
- use fixed sized acc array in struct changed_domain (Julien Grall)

---
 tools/xenstore/xenstored_core.c   |  9 ++++--
 tools/xenstore/xenstored_core.h   |  3 ++
 tools/xenstore/xenstored_domain.c | 53 ++++++++++++++++++++++++++++++-
 tools/xenstore/xenstored_domain.h |  5 ++-
 4 files changed, 66 insertions(+), 4 deletions(-)
tools/xenstore/xenstored_core.c +7 -2
index 3ca68681e3..84335f5f3d 100644
@@ -1023,6 +1023,9 @@ static void send_error(struct connection *conn, int error)
 			break;
 		}
 	}
+
+	acc_drop(conn);
+
 	send_reply(conn, XS_ERROR, xsd_errors[i].errstring,
 			  strlen(xsd_errors[i].errstring) + 1);
 }
@@ -1034,6 +1037,9 @@ void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
 
 	assert(type != XS_WATCH_EVENT);
 
+	conn->in = NULL;
+	acc_commit(conn);
+
 	if ( len > XENSTORE_PAYLOAD_MAX ) {
 		send_error(conn, E2BIG);
 		return;
@@ -1059,8 +1065,6 @@ void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
 		}
 	}
 
-	conn->in = NULL;
-
 	/* Update relevant header fields and fill in the message body. */
 	bdata->hdr.msg.type = type;
 	bdata->hdr.msg.len = len;
@@ -2195,6 +2199,7 @@ struct connection *new_connection(const struct interface_funcs *funcs)
 	new->is_stalled = false;
 	new->transaction_started = 0;
 	INIT_LIST_HEAD(&new->out_list);
+	INIT_LIST_HEAD(&new->acc_list);
 	INIT_LIST_HEAD(&new->ref_list);
 	INIT_LIST_HEAD(&new->watches);
 	INIT_LIST_HEAD(&new->transaction_list);
tools/xenstore/xenstored_core.h +3 -0
index c59b06551f..1f811f38cb 100644
@@ -139,6 +139,9 @@ struct connection
 	struct list_head out_list;
 	uint64_t timeout_msec;
 
+	/* Not yet committed accounting data (valid if in != NULL). */
+	struct list_head acc_list;
+
 	/* Referenced requests no longer pending. */
 	struct list_head ref_list;
 
tools/xenstore/xenstored_domain.c +52 -1

83 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +4 -1
index 9d05eb01da..6355ad4f37 100644
@@ -25,7 +25,8 @@
  * a per transaction array.
  */
 enum accitem {
-	ACC_NODES,
+	ACC_REQ_N,		/* Number of elements per request. */
+	ACC_NODES = ACC_REQ_N,
 	ACC_TR_N,		/* Number of elements per transaction. */
 	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
 };
@@ -113,6 +114,8 @@ int domain_get_quota(const void *ctx, struct connection *conn,
  * If "update" is true, "chk_quota" is ignored.
  */
 int acc_fix_domains(struct list_head *head, bool chk_quota, bool update);
+void acc_drop(struct connection *conn);
+void acc_commit(struct connection *conn);
 
 /* Write rate limiting */
 
Signature · 1 line
2.35.3
#6 lore raw

Re: [PATCH v4 04/13] tools/xenstore: add framework to commit accounting data on success only

Hi Juergen,

Quoted 37 lines · Juergen Gross

Instead of modifying accounting data and undo those modifications in case of an error during further processing, add a framework for collecting the needed changes and commit them only when the whole operation has succeeded.

This scheme can reuse large parts of the per transaction accounting. The changed_domain handling can be reused, but the array size of the accounting data should be possible to be different for both use cases.

---
V3:
- call acc_commit() earlier (Julien Grall)
- add assert() to acc_commit()
- use fixed sized acc array in struct changed_domain (Julien Grall)

---
  tools/xenstore/xenstored_core.c   |  9 ++++--
  tools/xenstore/xenstored_core.h   |  3 ++
  tools/xenstore/xenstored_domain.c | 53 ++++++++++++++++++++++++++++++-
  tools/xenstore/xenstored_domain.h |  5 ++-
  4 files changed, 66 insertions(+), 4 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index 3ca68681e3..84335f5f3d 100644
@@ -1023,6 +1023,9 @@ static void send_error(struct connection *conn, int error)
  			break;
  		}
  	}
+
+	acc_drop(conn);
+
  	send_reply(conn, XS_ERROR, xsd_errors[i].errstring,
  			  strlen(xsd_errors[i].errstring) + 1);
  }

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum xsd_sockmsg_type type, · line 1040

Quoted 4 lines
tools/xenstore/xenstored_core.c +1 -0
@@ -1034,6 +1037,9 @@ void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
 
  	assert(type != XS_WATCH_EVENT);
 
+	conn->in = NULL;

AFAIU, you are setting conn->in to NULL in order to please..

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum xsd_sockmsg_type type, · line 1041

Quoted 1 line
tools/xenstore/xenstored_core.c +1 -0
+	acc_commit(conn);

... this call. However in case of an error like...

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum xsd_sockmsg_type type, · line 1043

Quoted 2 lines
tools/xenstore/xenstored_core.c +1 -0
+
  	if ( len > XENSTORE_PAYLOAD_MAX ) { >   		send_error(conn, E2BIG);

... here, send_reply() will be called again. But the error will not be set because conn->in is NULL.

So I think you want to restore conn->in rewrite acc_commit(). The ordering would also deserve an explanation in a comment.

Quoted 37 lines
tools/xenstore/xenstored_core.c +1 -2
  		return;
@@ -1059,8 +1065,6 @@ void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
  		}
  	}
 
-	conn->in = NULL;
-
  	/* Update relevant header fields and fill in the message body. */
  	bdata->hdr.msg.type = type;
  	bdata->hdr.msg.len = len;
@@ -2195,6 +2199,7 @@ struct connection *new_connection(const struct interface_funcs *funcs)
  	new->is_stalled = false;
  	new->transaction_started = 0;
  	INIT_LIST_HEAD(&new->out_list);
+	INIT_LIST_HEAD(&new->acc_list);
  	INIT_LIST_HEAD(&new->ref_list);
  	INIT_LIST_HEAD(&new->watches);
  	INIT_LIST_HEAD(&new->transaction_list);
tools/xenstore/xenstored_core.h +3 -0
index c59b06551f..1f811f38cb 100644
@@ -139,6 +139,9 @@ struct connection
  	struct list_head out_list;
  	uint64_t timeout_msec;
 
+	/* Not yet committed accounting data (valid if in != NULL). */
+	struct list_head acc_list;
+
  	/* Referenced requests no longer pending. */
  	struct list_head ref_list;
 
tools/xenstore/xenstored_domain.c +0 -0
index 30fb9acec6..144cbafb73 100644

tools/xenstore/xenstored_domain.c struct domain · line 94

Quoted 4 lines
tools/xenstore/xenstored_domain.c +1 -0
@@ -91,6 +91,8 @@ struct domain
  	bool wrl_delay_logged;
  };
 
+#define ACC_CHD_N (ACC_TR_N < ACC_REQ_N ? ACC_REQ_N : ACC_TR_N)

Both ACC_TR_N and ACC_REQ_N are fixed. Can you explain why we need this magic?

Related, wouldn't it be better to define it in the enum?

Quoted 98 lines
tools/xenstore/xenstored_domain.c +51 -1
+
  struct changed_domain
  {
  	/* List of all changed domains. */
@@ -100,7 +102,7 @@ struct changed_domain
  	unsigned int domid;
 
  	/* Accounting data. */
-	int acc[ACC_TR_N];
+	int acc[ACC_CHD_N];
  };
 
  static struct hashtable *domhash;
@@ -1070,6 +1072,7 @@ static int domain_acc_add(struct connection *conn, unsigned int domid,
  			  enum accitem what, int add, bool no_dom_alloc)
  {
  	struct domain *d;
+	struct changed_domain *cd;
  	struct list_head *head;
  	int ret;
 
@@ -1090,6 +1093,22 @@ static int domain_acc_add(struct connection *conn, unsigned int domid,
  		}
  	}
 
+	/* Temporary accounting data until final commit? */
+	if (conn && conn->in && what < ACC_REQ_N) {
+		/* Consider transaction local data. */
+		ret = 0;
+		if (conn->transaction && what < ACC_TR_N) {
+			head = transaction_get_changed_domains(
+				conn->transaction);
+			cd = acc_find_changed_domain(head, domid);
+			if (cd)
+				ret = cd->acc[what];
+		}
+		ret += acc_add_changed_dom(conn->in, &conn->acc_list, what,
+					   add, domid);
+		return errno ? -1 : domain_acc_add_valid(d, what, ret);
+	}
+
  	if (conn && conn->transaction && what < ACC_TR_N) {
  		head = transaction_get_changed_domains(conn->transaction);
  		ret = acc_add_changed_dom(conn->transaction, head, what,
@@ -1106,6 +1125,38 @@ static int domain_acc_add(struct connection *conn, unsigned int domid,
  	return d->acc[what];
  }
 
+void acc_drop(struct connection *conn)
+{
+	struct changed_domain *cd;
+
+	while ((cd = list_top(&conn->acc_list, struct changed_domain, list))) {
+		list_del(&cd->list);
+		talloc_free(cd);
+	}
+}
+
+void acc_commit(struct connection *conn)
+{
+	struct changed_domain *cd;
+	enum accitem what;
+
+	/*
+	 * Make sure domain_acc_add() below can't add additional data to
+	 * to be committed accounting records.
+	 */
+	assert(!conn->in);
+
+	while ((cd = list_top(&conn->acc_list, struct changed_domain, list))) {
+		list_del(&cd->list);
+		for (what = 0; what < ACC_REQ_N; what++)
+			if (cd->acc[what])
+				domain_acc_add(conn, cd->domid, what,
+					       cd->acc[what], true);
+
+		talloc_free(cd);
+	}
+}
+
  int domain_nbentry_inc(struct connection *conn, unsigned int domid)
  {
  	return (domain_acc_add(conn, domid, ACC_NODES, 1, false) < 0)
tools/xenstore/xenstored_domain.h +2 -1
index 9d05eb01da..6355ad4f37 100644
@@ -25,7 +25,8 @@
   * a per transaction array.
   */
  enum accitem {
-	ACC_NODES,
+	ACC_REQ_N,		/* Number of elements per request. */
+	ACC_NODES = ACC_REQ_N,
  	ACC_TR_N,		/* Number of elements per transaction. */
  	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
  };

tools/xenstore/xenstored_domain.h int domain_get_quota(const void *ctx, struct connection *conn, · line 121

Quoted 8 lines
tools/xenstore/xenstored_domain.h +2 -0
@@ -113,6 +114,8 @@ int domain_get_quota(const void *ctx, struct connection *conn,
   * If "update" is true, "chk_quota" is ignored.
   */
  int acc_fix_domains(struct list_head *head, bool chk_quota, bool update);
+void acc_drop(struct connection *conn);
+void acc_commit(struct connection *conn);
 
  /* Write rate limiting */
 

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [PATCH v4 04/13] tools/xenstore: add framework to commit accounting data on success only

Quoted 48 lines · Julien Grall

Hi Juergen,

Quoted 37 lines · Juergen Gross

Instead of modifying accounting data and undo those modifications in case of an error during further processing, add a framework for collecting the needed changes and commit them only when the whole operation has succeeded.

This scheme can reuse large parts of the per transaction accounting. The changed_domain handling can be reused, but the array size of the accounting data should be possible to be different for both use cases.

---
V3:
- call acc_commit() earlier (Julien Grall)
- add assert() to acc_commit()
- use fixed sized acc array in struct changed_domain (Julien Grall)
---
  tools/xenstore/xenstored_core.c   |  9 ++++-- tools/xenstore/xenstored_core.h   |  3 ++ tools/xenstore/xenstored_domain.c | 53 ++++++++++++++++++++++++++++++- tools/xenstore/xenstored_domain.h |  5 ++-

  4 files changed, 66 insertions(+), 4 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index 3ca68681e3..84335f5f3d 100644
@@ -1023,6 +1023,9 @@ static void send_error(struct connection *conn, int error)
               break;
           }
       }
+
+    acc_drop(conn);
+
       send_reply(conn, XS_ERROR, xsd_errors[i].errstring,
                 strlen(xsd_errors[i].errstring) + 1);
   }

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum · line 1039

Quoted 3 lines
tools/xenstore/xenstored_core.c +1 -0
@@ -1034,6 +1037,9 @@ void send_reply(struct connection *conn, enum 
 xsd_sockmsg_type type,
       assert(type != XS_WATCH_EVENT);
+    conn->in = NULL;

AFAIU, you are setting conn->in to NULL in order to please..

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum xsd_sockmsg_type type, · line 1041

Quoted 1 line
tools/xenstore/xenstored_core.c +1 -0
+    acc_commit(conn);
Quoted 8 lines

... this call. However in case of an error like...

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum · line 1042

Quoted 2 lines
tools/xenstore/xenstored_core.c +1 -0
+
       if ( len > XENSTORE_PAYLOAD_MAX ) { >           send_error(conn, E2BIG);

... here, send_reply() will be called again. But the error will not be set because conn->in is NULL.

So I think you want to restore conn->in rewrite acc_commit(). The ordering would also deserve an explanation in a comment.

Just to make sure I understand you correctly (I have some difficulties parsing "So I think you want to restore conn->in rewrite acc_commit()." completely):

You are suggesting to move setting conn->in to NULL to acc_commit() and to restore it before returning? I'm fine with that.

Quoted 43 lines
Quoted 37 lines
tools/xenstore/xenstored_core.c +1 -2
           return;
@@ -1059,8 +1065,6 @@ void send_reply(struct connection *conn, enum 
 xsd_sockmsg_type type,
           }
       }
-    conn->in = NULL;
-
       /* Update relevant header fields and fill in the message body. */
       bdata->hdr.msg.type = type;
       bdata->hdr.msg.len = len;
@@ -2195,6 +2199,7 @@ struct connection *new_connection(const struct 
 interface_funcs *funcs)
       new->is_stalled = false;
       new->transaction_started = 0;
       INIT_LIST_HEAD(&new->out_list);
+    INIT_LIST_HEAD(&new->acc_list);
       INIT_LIST_HEAD(&new->ref_list);
       INIT_LIST_HEAD(&new->watches);

      INIT_LIST_HEAD(&new->transaction_list);

tools/xenstore/xenstored_core.h +3 -0
index c59b06551f..1f811f38cb 100644
@@ -139,6 +139,9 @@ struct connection
       struct list_head out_list;
       uint64_t timeout_msec;
+    /* Not yet committed accounting data (valid if in != NULL). */
+    struct list_head acc_list;
+
       /* Referenced requests no longer pending. */
       struct list_head ref_list;
tools/xenstore/xenstored_domain.c +0 -0
index 30fb9acec6..144cbafb73 100644

tools/xenstore/xenstored_domain.c struct domain · line 93

Quoted 3 lines
tools/xenstore/xenstored_domain.c +1 -0
@@ -91,6 +91,8 @@ struct domain
       bool wrl_delay_logged;
   };
+#define ACC_CHD_N (ACC_TR_N < ACC_REQ_N ? ACC_REQ_N : ACC_TR_N)

Both ACC_TR_N and ACC_REQ_N are fixed. Can you explain why we need this magic?

Quoted 1 line

Related, wouldn't it be better to define it in the enum?

I can do that, of course. I just didn't want to make the enum even more complex. :-)

But with a comment this should be okay IMO.

Juergen

#8 lore raw

Re: [PATCH v4 04/13] tools/xenstore: add framework to commit accounting data on success only

Hi,

Quoted 55 lines · Juergen Gross
Quoted 54 lines · Julien Grall

Hi Juergen,

Quoted 38 lines · Juergen Gross

Instead of modifying accounting data and undo those modifications in case of an error during further processing, add a framework for collecting the needed changes and commit them only when the whole operation has succeeded.

This scheme can reuse large parts of the per transaction accounting. The changed_domain handling can be reused, but the array size of the accounting data should be possible to be different for both use cases.

---
V3:
- call acc_commit() earlier (Julien Grall)
- add assert() to acc_commit()
- use fixed sized acc array in struct changed_domain (Julien Grall)
---
  tools/xenstore/xenstored_core.c   |  9 ++++-- tools/xenstore/xenstored_core.h   |  3 ++ tools/xenstore/xenstored_domain.c | 53 ++++++++++++++++++++++++++++++- tools/xenstore/xenstored_domain.h |  5 ++-

  4 files changed, 66 insertions(+), 4 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index 3ca68681e3..84335f5f3d 100644
@@ -1023,6 +1023,9 @@ static void send_error(struct connection *conn, 
 int error)
               break;
           }
       }
+
+    acc_drop(conn);
+
       send_reply(conn, XS_ERROR, xsd_errors[i].errstring,
                 strlen(xsd_errors[i].errstring) + 1);

  }

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum · line 1039

Quoted 3 lines
tools/xenstore/xenstored_core.c +1 -0
@@ -1034,6 +1037,9 @@ void send_reply(struct connection *conn, enum 
 xsd_sockmsg_type type,
       assert(type != XS_WATCH_EVENT);
+    conn->in = NULL;

AFAIU, you are setting conn->in to NULL in order to please..

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum xsd_sockmsg_type type, · line 1041

Quoted 1 line
tools/xenstore/xenstored_core.c +1 -0
+    acc_commit(conn);

... this call. However in case of an error like...

Quoted 2 lines
tools/xenstore/xenstored_core.c +1 -0
+
       if ( len > XENSTORE_PAYLOAD_MAX ) { >           send_error(conn, E2BIG);
Quoted 7 lines
Quoted 3 lines

... here, send_reply() will be called again. But the error will not be set because conn->in is NULL.

So I think you want to restore conn->in rewrite acc_commit(). The ordering would also deserve an explanation in a comment.

Just to make sure I understand you correctly (I have some difficulties parsing "So I think you want to restore conn->in rewrite acc_commit()." completely):

Hmmm... Not sure why I wrote "rewrite". I was meant to say that you want to restore conn->in after acc_commit() is called.

Quoted 3 lines

You are suggesting to move setting conn->in to NULL to acc_commit() and to restore it before returning? I'm fine with that.

Either that or what I wrote above. It depends on whether you expect other caller to be in the same situation.

Quoted 44 lines
Quoted 44 lines
Quoted 38 lines
tools/xenstore/xenstored_core.c +1 -2
           return;
@@ -1059,8 +1065,6 @@ void send_reply(struct connection *conn, enum 
 xsd_sockmsg_type type,
           }
       }
-    conn->in = NULL;
-
       /* Update relevant header fields and fill in the message body. */
       bdata->hdr.msg.type = type;
       bdata->hdr.msg.len = len;
@@ -2195,6 +2199,7 @@ struct connection *new_connection(const struct 
 interface_funcs *funcs)
       new->is_stalled = false;
       new->transaction_started = 0;
       INIT_LIST_HEAD(&new->out_list);
+    INIT_LIST_HEAD(&new->acc_list);
       INIT_LIST_HEAD(&new->ref_list);
       INIT_LIST_HEAD(&new->watches);

      INIT_LIST_HEAD(&new->transaction_list);

tools/xenstore/xenstored_core.h +3 -0
index c59b06551f..1f811f38cb 100644
@@ -139,6 +139,9 @@ struct connection
       struct list_head out_list;
       uint64_t timeout_msec;
+    /* Not yet committed accounting data (valid if in != NULL). */
+    struct list_head acc_list;
+
       /* Referenced requests no longer pending. */
       struct list_head ref_list;
tools/xenstore/xenstored_domain.c +0 -0
index 30fb9acec6..144cbafb73 100644

tools/xenstore/xenstored_domain.c struct domain · line 93

Quoted 3 lines
tools/xenstore/xenstored_domain.c +1 -0
@@ -91,6 +91,8 @@ struct domain
       bool wrl_delay_logged;
   };
+#define ACC_CHD_N (ACC_TR_N < ACC_REQ_N ? ACC_REQ_N : ACC_TR_N)

Both ACC_TR_N and ACC_REQ_N are fixed. Can you explain why we need this magic?

Quoted 4 lines
Quoted 1 line

Related, wouldn't it be better to define it in the enum?

I can do that, of course. I just didn't want to make the enum even more complex. :-)

My concern is there is a disconnect between the enum and this macro. What would happen if we increase the enum past ACC_REQ_N/ACC_TR_N? Would it be necessary to update ACC_CHD_N?

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v4 04/13] tools/xenstore: add framework to commit accounting data on success only

Quoted 66 lines · Julien Grall

Hi,

Quoted 55 lines · Juergen Gross
Quoted 54 lines · Julien Grall

Hi Juergen,

Quoted 38 lines · Juergen Gross

Instead of modifying accounting data and undo those modifications in case of an error during further processing, add a framework for collecting the needed changes and commit them only when the whole operation has succeeded.

This scheme can reuse large parts of the per transaction accounting. The changed_domain handling can be reused, but the array size of the accounting data should be possible to be different for both use cases.

---
V3:
- call acc_commit() earlier (Julien Grall)
- add assert() to acc_commit()
- use fixed sized acc array in struct changed_domain (Julien Grall)
---
  tools/xenstore/xenstored_core.c   |  9 ++++-- tools/xenstore/xenstored_core.h   |  3 ++ tools/xenstore/xenstored_domain.c | 53 ++++++++++++++++++++++++++++++- tools/xenstore/xenstored_domain.h |  5 ++-

  4 files changed, 66 insertions(+), 4 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index 3ca68681e3..84335f5f3d 100644
@@ -1023,6 +1023,9 @@ static void send_error(struct connection *conn, int 
 error)
               break;
           }
       }
+
+    acc_drop(conn);
+
       send_reply(conn, XS_ERROR, xsd_errors[i].errstring,
                 strlen(xsd_errors[i].errstring) + 1);

  }

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum · line 1039

Quoted 3 lines
tools/xenstore/xenstored_core.c +1 -0
@@ -1034,6 +1037,9 @@ void send_reply(struct connection *conn, enum 
 xsd_sockmsg_type type,
       assert(type != XS_WATCH_EVENT);
+    conn->in = NULL;

AFAIU, you are setting conn->in to NULL in order to please..

tools/xenstore/xenstored_core.c void send_reply(struct connection *conn, enum xsd_sockmsg_type type, · line 1041

Quoted 1 line
tools/xenstore/xenstored_core.c +1 -0
+    acc_commit(conn);

... this call. However in case of an error like...

Quoted 2 lines
tools/xenstore/xenstored_core.c +1 -0
+
       if ( len > XENSTORE_PAYLOAD_MAX ) { > send_error(conn, E2BIG);
Quoted 7 lines
Quoted 3 lines

... here, send_reply() will be called again. But the error will not be set because conn->in is NULL.

So I think you want to restore conn->in rewrite acc_commit(). The ordering would also deserve an explanation in a comment.

Just to make sure I understand you correctly (I have some difficulties parsing "So I think you want to restore conn->in rewrite acc_commit()." completely):

Quoted 7 lines

Hmmm... Not sure why I wrote "rewrite". I was meant to say that you want to restore conn->in after acc_commit() is called.

Quoted 3 lines

You are suggesting to move setting conn->in to NULL to acc_commit() and to restore it before returning? I'm fine with that.

Either that or what I wrote above. It depends on whether you expect other caller to be in the same situation.

I think it should be local to acc_commit(), as conn->in being NULL is a requirement of the acc_commit() handling.

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
Quoted 39 lines
tools/xenstore/xenstored_core.c +1 -2
           return;
@@ -1059,8 +1065,6 @@ void send_reply(struct connection *conn, enum 
 xsd_sockmsg_type type,
           }
       }
-    conn->in = NULL;
-
       /* Update relevant header fields and fill in the message body. */
       bdata->hdr.msg.type = type;
       bdata->hdr.msg.len = len;
@@ -2195,6 +2199,7 @@ struct connection *new_connection(const struct 
 interface_funcs *funcs)
       new->is_stalled = false;
       new->transaction_started = 0;
       INIT_LIST_HEAD(&new->out_list);
+    INIT_LIST_HEAD(&new->acc_list);
       INIT_LIST_HEAD(&new->ref_list);
       INIT_LIST_HEAD(&new->watches);

      INIT_LIST_HEAD(&new->transaction_list);

tools/xenstore/xenstored_core.h +3 -0
index c59b06551f..1f811f38cb 100644
@@ -139,6 +139,9 @@ struct connection
       struct list_head out_list;
       uint64_t timeout_msec;
+    /* Not yet committed accounting data (valid if in != NULL). */
+    struct list_head acc_list;
+
       /* Referenced requests no longer pending. */
       struct list_head ref_list;
tools/xenstore/xenstored_domain.c +0 -0
index 30fb9acec6..144cbafb73 100644

tools/xenstore/xenstored_domain.c struct domain · line 93

Quoted 3 lines
tools/xenstore/xenstored_domain.c +1 -0
@@ -91,6 +91,8 @@ struct domain
       bool wrl_delay_logged;
   };
+#define ACC_CHD_N (ACC_TR_N < ACC_REQ_N ? ACC_REQ_N : ACC_TR_N)

Both ACC_TR_N and ACC_REQ_N are fixed. Can you explain why we need this magic?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Related, wouldn't it be better to define it in the enum?

I can do that, of course. I just didn't want to make the enum even more complex. :-)

My concern is there is a disconnect between the enum and this macro. What would happen if we increase the enum past ACC_REQ_N/ACC_TR_N?

This expansion is happening in later patches.

Quoted 1 line

Would it be necessary to update ACC_CHD_N?

With the #define above: no.

With including it in the enum: depends on the values of ACC_REQ_N and ACC_TR_N. ACC_CHD_N must always be equal to the larger one of both.

In my current V6 variant I've added it to the enum with a comment:

+ ACC_CHD_N = ACC_TR_N, /* max(ACC_REQ_N, ACC_TR_N), for changed dom. */

Juergen

#10 lore raw

PATCH v4 5/13 tools/xenstore: use accounting buffering for node accounting

In reply to Juergen Gross

Add the node accounting to the accounting information buffering in order to avoid having to undo it in case of failure.

---
 tools/xenstore/xenstored_core.c   | 21 ++-------------------
 tools/xenstore/xenstored_domain.h |  4 ++--
 2 files changed, 4 insertions(+), 21 deletions(-)
tools/xenstore/xenstored_core.c +2 -19
index 84335f5f3d..92a40ccf3f 100644
@@ -1452,7 +1452,6 @@ 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_nbentry_dec(conn, get_node_owner(node));
 
 	/*
 	 * It is not possible to easily revert the changes in a transaction.
@@ -1797,27 +1796,11 @@ static int do_set_perms(const void *ctx, struct connection *conn,
 	old_perms = node->perms;
 	domain_nbentry_dec(conn, get_node_owner(node));
 	node->perms = perms;
-	if (domain_nbentry_inc(conn, get_node_owner(node))) {
-		node->perms = old_perms;
-		/*
-		 * This should never fail because we had a reference on the
-		 * domain before and Xenstored is single-threaded.
-		 */
-		domain_nbentry_inc(conn, get_node_owner(node));
+	if (domain_nbentry_inc(conn, get_node_owner(node)))
 		return ENOMEM;
-	}
 
-	if (write_node(conn, node, false)) {
-		int saved_errno = errno;
-
-		domain_nbentry_dec(conn, get_node_owner(node));
-		node->perms = old_perms;
-		/* No failure possible as above. */
-		domain_nbentry_inc(conn, get_node_owner(node));
-
-		errno = saved_errno;
+	if (write_node(conn, node, false))
 		return errno;
-	}
 
 	fire_watches(conn, ctx, name, node, false, &old_perms);
 	send_ack(conn, XS_SET_PERMS);
tools/xenstore/xenstored_domain.h +2 -2
index 6355ad4f37..e669f57b80 100644
@@ -25,9 +25,9 @@
  * a per transaction array.
  */
 enum accitem {
+	ACC_NODES,
 	ACC_REQ_N,		/* Number of elements per request. */
-	ACC_NODES = ACC_REQ_N,
-	ACC_TR_N,		/* Number of elements per transaction. */
+	ACC_TR_N = ACC_REQ_N,	/* Number of elements per transaction. */
 	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
 };
 
Signature · 1 line
2.35.3
#11 lore raw

Re: [PATCH v4 05/13] tools/xenstore: use accounting buffering for node accounting

Hi Juergen,

Quoted 22 lines · Juergen Gross

Add the node accounting to the accounting information buffering in order to avoid having to undo it in case of failure.

---
  tools/xenstore/xenstored_core.c   | 21 ++-------------------
  tools/xenstore/xenstored_domain.h |  4 ++--
  2 files changed, 4 insertions(+), 21 deletions(-)
tools/xenstore/xenstored_core.c +0 -1
index 84335f5f3d..92a40ccf3f 100644
@@ -1452,7 +1452,6 @@ 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_nbentry_dec(conn, get_node_owner(node));
 
  	/*
  	 * It is not possible to easily revert the changes in a transaction.

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

Quoted 2 lines
tools/xenstore/xenstored_core.c +0 -0
@@ -1797,27 +1796,11 @@ static int do_set_perms(const void *ctx, struct connection *conn,
  	old_perms = node->perms;
  	domain_nbentry_dec(conn, get_node_owner(node));

IIRC, we originally said that domain_nbentry_dec() could never fail in a non-transaction case. But with your current rework, the function can now fail because of an allocation failure.

Therefore, shouldn't we now check the error? (Possibly in a patch beforehand).

Quoted 33 lines
tools/xenstore/xenstored_core.c +2 -18
  	node->perms = perms;
-	if (domain_nbentry_inc(conn, get_node_owner(node))) {
-		node->perms = old_perms;
-		/*
-		 * This should never fail because we had a reference on the
-		 * domain before and Xenstored is single-threaded.
-		 */
-		domain_nbentry_inc(conn, get_node_owner(node));
+	if (domain_nbentry_inc(conn, get_node_owner(node)))
  		return ENOMEM;
-	}
 
-	if (write_node(conn, node, false)) {
-		int saved_errno = errno;
-
-		domain_nbentry_dec(conn, get_node_owner(node));
-		node->perms = old_perms;
-		/* No failure possible as above. */
-		domain_nbentry_inc(conn, get_node_owner(node));
-
-		errno = saved_errno;
+	if (write_node(conn, node, false))
  		return errno;
-	}
 
  	fire_watches(conn, ctx, name, node, false, &old_perms);
  	send_ack(conn, XS_SET_PERMS);
tools/xenstore/xenstored_domain.h +0 -0
index 6355ad4f37..e669f57b80 100644
@@ -25,9 +25,9 @@
   * a per transaction array.

tools/xenstore/xenstored_domain.h line 33

Quoted 10 lines
tools/xenstore/xenstored_domain.h +2 -2
   */
  enum accitem {
+	ACC_NODES,
  	ACC_REQ_N,		/* Number of elements per request. */
-	ACC_NODES = ACC_REQ_N,
-	ACC_TR_N,		/* Number of elements per transaction. */
+	ACC_TR_N = ACC_REQ_N,	/* Number of elements per transaction. */
  	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
  };
 

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH v4 05/13] tools/xenstore: use accounting buffering for node accounting

Quoted 22 lines · Julien Grall

Hi Juergen,

Quoted 19 lines · Juergen Gross

Add the node accounting to the accounting information buffering in order to avoid having to undo it in case of failure.

---
  tools/xenstore/xenstored_core.c   | 21 ++------------------- tools/xenstore/xenstored_domain.h |  4 ++--

  2 files changed, 4 insertions(+), 21 deletions(-)
tools/xenstore/xenstored_core.c +0 -1
index 84335f5f3d..92a40ccf3f 100644
@@ -1452,7 +1452,6 @@ 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_nbentry_dec(conn, get_node_owner(node));
Quoted 8 lines

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

Quoted 6 lines
tools/xenstore/xenstored_core.c +0 -0
       /*
        * It is not possible to easily revert the changes in a transaction.
@@ -1797,27 +1796,11 @@ static int do_set_perms(const void *ctx, struct 
 connection *conn,
       old_perms = node->perms;
       domain_nbentry_dec(conn, get_node_owner(node));

IIRC, we originally said that domain_nbentry_dec() could never fail in a non-transaction case. But with your current rework, the function can now fail because of an allocation failure.

How would that be possible to happen?

domain_nbentry_dec() can only be called if a node is being owned by an already known domain. So allocation is impossible to happen, as this would be a major error in xenstored.

Quoted 1 line

Therefore, shouldn't we now check the error? (Possibly in a patch beforehand).

I don't think so. I can add a comment if you want.

Juergen

#13 lore raw

Re: [PATCH v4 05/13] tools/xenstore: use accounting buffering for node accounting

Hi Juergen,

Quoted 32 lines · Juergen Gross
Quoted 22 lines · Julien Grall

Hi Juergen,

Quoted 19 lines · Juergen Gross

Add the node accounting to the accounting information buffering in order to avoid having to undo it in case of failure.

---
  tools/xenstore/xenstored_core.c   | 21 ++------------------- tools/xenstore/xenstored_domain.h |  4 ++--

  2 files changed, 4 insertions(+), 21 deletions(-)
tools/xenstore/xenstored_core.c +0 -1
index 84335f5f3d..92a40ccf3f 100644
@@ -1452,7 +1452,6 @@ 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_nbentry_dec(conn, get_node_owner(node));
Quoted 8 lines

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

Quoted 6 lines
tools/xenstore/xenstored_core.c +0 -0
       /*
        * It is not possible to easily revert the changes in a transaction.
@@ -1797,27 +1796,11 @@ static int do_set_perms(const void *ctx, 
 struct connection *conn,
       old_perms = node->perms;
       domain_nbentry_dec(conn, get_node_owner(node));

IIRC, we originally said that domain_nbentry_dec() could never fail in a non-transaction case. But with your current rework, the function can now fail because of an allocation failure.

Quoted 5 lines

How would that be possible to happen?

domain_nbentry_dec() can only be called if a node is being owned by an already known domain. So allocation is impossible to happen, as this would be a major error in xenstored.

From my understanding, the nodes accounting will be temporary and then committed at the end of the request.

So we would call acc_add_changed_dom() which may require allocation to hold the temporary accounting.

Quoted 4 lines
Quoted 2 lines

Therefore, shouldn't we now check the error? (Possibly in a patch beforehand).

I don't think so. I can add a comment if you want.

See above.

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

Re: [PATCH v4 05/13] tools/xenstore: use accounting buffering for node accounting

Quoted 37 lines · Julien Grall

Hi Juergen,

Quoted 34 lines · Juergen Gross
Quoted 22 lines · Julien Grall

Hi Juergen,

Quoted 19 lines · Juergen Gross

Add the node accounting to the accounting information buffering in order to avoid having to undo it in case of failure.

---
  tools/xenstore/xenstored_core.c   | 21 ++------------------- tools/xenstore/xenstored_domain.h |  4 ++--

  2 files changed, 4 insertions(+), 21 deletions(-)
tools/xenstore/xenstored_core.c +0 -1
index 84335f5f3d..92a40ccf3f 100644
@@ -1452,7 +1452,6 @@ 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_nbentry_dec(conn, get_node_owner(node));
Quoted 8 lines

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

Quoted 6 lines
tools/xenstore/xenstored_core.c +0 -0
       /*
        * It is not possible to easily revert the changes in a transaction.
@@ -1797,27 +1796,11 @@ static int do_set_perms(const void *ctx, struct 
 connection *conn,
       old_perms = node->perms;
       domain_nbentry_dec(conn, get_node_owner(node));

IIRC, we originally said that domain_nbentry_dec() could never fail in a non-transaction case. But with your current rework, the function can now fail because of an allocation failure.

How would that be possible to happen?

Quoted 8 lines
Quoted 3 lines

domain_nbentry_dec() can only be called if a node is being owned by an already known domain. So allocation is impossible to happen, as this would be a major error in xenstored.

From my understanding, the nodes accounting will be temporary and then
committed at the end of the request.

So we would call acc_add_changed_dom() which may require allocation to hold the temporary accounting.

Ah, right, good catch!

Will add checking the return value and move the calls ahead of the tdb changes.

Juergen

#15 lore raw

PATCH v4 6/13 tools/xenstore: add current connection to domain_memory_add() parameters

In reply to Juergen Gross

In order to enable switching memory accounting to the generic array based accounting, add the current connection to the parameters of domain_memory_add().

This requires to add the connection to some other functions, too.

---
 tools/xenstore/xenstored_core.c   | 28 ++++++++++++++++------------
 tools/xenstore/xenstored_domain.c |  3 ++-
 tools/xenstore/xenstored_domain.h | 14 +++++++++-----
 tools/xenstore/xenstored_watch.c  | 11 ++++++-----
 4 files changed, 33 insertions(+), 23 deletions(-)
tools/xenstore/xenstored_core.c +16 -12
index 92a40ccf3f..88ae674523 100644
@@ -246,7 +246,8 @@ static void free_buffered_data(struct buffered_data *out,
 		}
 	}
 
-	domain_memory_add_nochk(conn->id, -out->hdr.msg.len - sizeof(out->hdr));
+	domain_memory_add_nochk(conn, conn->id,
+				-out->hdr.msg.len - sizeof(out->hdr));
 
 	if (out->hdr.msg.type == XS_WATCH_EVENT) {
 		req = out->pend.req;
@@ -631,24 +632,25 @@ int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data,
 	 * nodes to new owners.
 	 */
 	if (old_acc.memory)
-		domain_memory_add_nochk(old_domid,
+		domain_memory_add_nochk(conn, old_domid,
 					-old_acc.memory - key->dsize);
-	ret = domain_memory_add(new_domid, data->dsize + key->dsize,
-				no_quota_check);
+	ret = domain_memory_add(conn, new_domid,
+				data->dsize + key->dsize, no_quota_check);
 	if (ret) {
 		/* Error path, so no quota check. */
 		if (old_acc.memory)
-			domain_memory_add_nochk(old_domid,
+			domain_memory_add_nochk(conn, old_domid,
 						old_acc.memory + key->dsize);
 		return ret;
 	}
 
 	/* TDB should set errno, but doesn't even set ecode AFAICT. */
 	if (tdb_store(tdb_ctx, *key, *data, TDB_REPLACE) != 0) {
-		domain_memory_add_nochk(new_domid, -data->dsize - key->dsize);
+		domain_memory_add_nochk(conn, new_domid,
+					-data->dsize - key->dsize);
 		/* Error path, so no quota check. */
 		if (old_acc.memory)
-			domain_memory_add_nochk(old_domid,
+			domain_memory_add_nochk(conn, old_domid,
 						old_acc.memory + key->dsize);
 		errno = EIO;
 		return errno;
@@ -683,7 +685,7 @@ int do_tdb_delete(struct connection *conn, TDB_DATA *key,
 
 	if (acc->memory) {
 		domid = get_acc_domid(conn, key, acc->domid);
-		domain_memory_add_nochk(domid, -acc->memory - key->dsize);
+		domain_memory_add_nochk(conn, domid, -acc->memory - key->dsize);
 	}
 
 	return 0;
@@ -1055,11 +1057,13 @@ void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
 	if (len <= DEFAULT_BUFFER_SIZE) {
 		bdata->buffer = bdata->default_buffer;
 		/* Don't check quota, path might be used for returning error. */
-		domain_memory_add_nochk(conn->id, len + sizeof(bdata->hdr));
+		domain_memory_add_nochk(conn, conn->id,
+					len + sizeof(bdata->hdr));
 	} else {
 		bdata->buffer = talloc_array(bdata, char, len);
 		if (!bdata->buffer ||
-		    domain_memory_add_chk(conn->id, len + sizeof(bdata->hdr))) {
+		    domain_memory_add_chk(conn, conn->id,
+					  len + sizeof(bdata->hdr))) {
 			send_error(conn, ENOMEM);
 			return;
 		}
@@ -1122,7 +1126,7 @@ void send_event(struct buffered_data *req, struct connection *conn,
 		}
 	}
 
-	if (domain_memory_add_chk(conn->id, len + sizeof(bdata->hdr))) {
+	if (domain_memory_add_chk(conn, conn->id, len + sizeof(bdata->hdr))) {
 		talloc_free(bdata);
 		return;
 	}
@@ -3322,7 +3326,7 @@ static void add_buffered_data(struct buffered_data *bdata,
 	 * be smaller. So ignore it. The limit will be applied for any resource
 	 * after the state has been fully restored.
 	 */
-	domain_memory_add_nochk(conn->id, len + sizeof(bdata->hdr));
+	domain_memory_add_nochk(conn, conn->id, len + sizeof(bdata->hdr));
 }
 
 void read_state_buffered_data(const void *ctx, struct connection *conn,
tools/xenstore/xenstored_domain.c +2 -1
index 144cbafb73..f94812610a 100644
@@ -1235,7 +1235,8 @@ static bool domain_chk_quota(struct domain *domain, int mem)
 	return false;
 }
 
-int domain_memory_add(unsigned int domid, int mem, bool no_quota_check)
+int domain_memory_add(struct connection *conn, unsigned int domid, int mem,
+		      bool no_quota_check)
 {
 	struct domain *domain;
 
tools/xenstore/xenstored_domain.h +9 -5
index e669f57b80..5cfd730cf6 100644
@@ -80,25 +80,29 @@ 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);
 unsigned int domain_nbentry(struct connection *conn);
-int domain_memory_add(unsigned int domid, int mem, bool no_quota_check);
+int domain_memory_add(struct connection *conn, unsigned int domid, int mem,
+		      bool no_quota_check);
 
 /*
  * domain_memory_add_chk(): to be used when memory quota should be checked.
  * Not to be used when specifying a negative mem value, as lowering the used
  * memory should always be allowed.
  */
-static inline int domain_memory_add_chk(unsigned int domid, int mem)
+static inline int domain_memory_add_chk(struct connection *conn,
+					unsigned int domid, int mem)
 {
-	return domain_memory_add(domid, mem, false);
+	return domain_memory_add(conn, domid, mem, false);
 }
+
 /*
  * domain_memory_add_nochk(): to be used when memory quota should not be
  * checked, e.g. when lowering memory usage, or in an error case for undoing
  * a previous memory adjustment.
  */
-static inline void domain_memory_add_nochk(unsigned int domid, int mem)
+static inline void domain_memory_add_nochk(struct connection *conn,
+					   unsigned int domid, int mem)
 {
-	domain_memory_add(domid, mem, true);
+	domain_memory_add(conn, domid, mem, true);
 }
 void domain_watch_inc(struct connection *conn);
 void domain_watch_dec(struct connection *conn);
tools/xenstore/xenstored_watch.c +6 -5
index 8ad0229df6..e30cd89be3 100644
@@ -199,7 +199,7 @@ static struct watch *add_watch(struct connection *conn, char *path, char *token,
 	watch->token = talloc_strdup(watch, token);
 	if (!watch->node || !watch->token)
 		goto nomem;
-	if (domain_memory_add(conn->id, strlen(path) + strlen(token),
+	if (domain_memory_add(conn, conn->id, strlen(path) + strlen(token),
 			      no_quota_check))
 		goto nomem;
 
@@ -274,8 +274,9 @@ int do_unwatch(const void *ctx, struct connection *conn,
 	list_for_each_entry(watch, &conn->watches, list) {
 		if (streq(watch->node, node) && streq(watch->token, vec[1])) {
 			list_del(&watch->list);
-			domain_memory_add_nochk(conn->id, -strlen(watch->node) -
-							  strlen(watch->token));
+			domain_memory_add_nochk(conn, conn->id,
+						-strlen(watch->node) -
+						strlen(watch->token));
 			talloc_free(watch);
 			domain_watch_dec(conn);
 			send_ack(conn, XS_UNWATCH);
@@ -291,8 +292,8 @@ void conn_delete_all_watches(struct connection *conn)
 
 	while ((watch = list_top(&conn->watches, struct watch, list))) {
 		list_del(&watch->list);
-		domain_memory_add_nochk(conn->id, -strlen(watch->node) -
-						  strlen(watch->token));
+		domain_memory_add_nochk(conn, conn->id, -strlen(watch->node) -
+							strlen(watch->token));
 		talloc_free(watch);
 		domain_watch_dec(conn);
 	}
Signature · 1 line
2.35.3
Acked-by on v4 6/13
#16 lore raw

Re: [PATCH v4 06/13] tools/xenstore: add current connection to domain_memory_add() parameters

Hi Juergen,

Quoted 7 lines · Juergen Gross

In order to enable switching memory accounting to the generic array based accounting, add the current connection to the parameters of domain_memory_add().

This requires to add the connection to some other functions, too.

Cheers,

Signature · 1 line
Julien Grall
#17 lore raw

PATCH v4 7/13 tools/xenstore: use accounting data array for per-domain values

In reply to Juergen Gross

Add the accounting of per-domain usage of Xenstore memory, watches, and outstanding requests to the array based mechanism.

---
 tools/xenstore/xenstored_core.c   |   8 +--
 tools/xenstore/xenstored_domain.c | 111 +++++++++++-------------------
 tools/xenstore/xenstored_domain.h |  10 +--
 3 files changed, 52 insertions(+), 77 deletions(-)
tools/xenstore/xenstored_core.c +4 -4
index 88ae674523..67aa7c1578 100644
@@ -255,7 +255,7 @@ static void free_buffered_data(struct buffered_data *out,
 			req->pend.ref.event_cnt--;
 			if (!req->pend.ref.event_cnt && !req->on_out_list) {
 				if (req->on_ref_list) {
-					domain_outstanding_domid_dec(
+					domain_outstanding_dec(conn,
 						req->pend.ref.domid);
 					list_del(&req->list);
 				}
@@ -271,7 +271,7 @@ static void free_buffered_data(struct buffered_data *out,
 		out->on_ref_list = true;
 		return;
 	} else
-		domain_outstanding_dec(conn);
+		domain_outstanding_dec(conn, conn->id);
 
 	talloc_free(out);
 }
@@ -1077,7 +1077,7 @@ void send_reply(struct connection *conn, enum xsd_sockmsg_type type,
 	/* Queue for later transmission. */
 	list_add_tail(&bdata->list, &conn->out_list);
 	bdata->on_out_list = true;
-	domain_outstanding_inc(conn);
+	domain_outstanding_inc(conn, conn->id);
 }
 
 /*
@@ -3320,7 +3320,7 @@ static void add_buffered_data(struct buffered_data *bdata,
 	 * request have been delivered.
 	 */
 	if (bdata->hdr.msg.type != XS_WATCH_EVENT)
-		domain_outstanding_inc(conn);
+		domain_outstanding_inc(conn, conn->id);
 	/*
 	 * We are restoring the state after Live-Update and the new quota may
 	 * be smaller. So ignore it. The limit will be applied for any resource
tools/xenstore/xenstored_domain.c +42 -69

179 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +6 -4
index 5cfd730cf6..0d61bf4344 100644
@@ -28,7 +28,10 @@ enum accitem {
 	ACC_NODES,
 	ACC_REQ_N,		/* Number of elements per request. */
 	ACC_TR_N = ACC_REQ_N,	/* Number of elements per transaction. */
-	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
+	ACC_WATCH = ACC_TR_N,
+	ACC_OUTST,
+	ACC_MEM,
+	ACC_N,			/* Number of elements per domain. */
 };
 
 void handle_event(void);
@@ -107,9 +110,8 @@ static inline void domain_memory_add_nochk(struct connection *conn,
 void domain_watch_inc(struct connection *conn);
 void domain_watch_dec(struct connection *conn);
 int domain_watch(struct connection *conn);
-void domain_outstanding_inc(struct connection *conn);
-void domain_outstanding_dec(struct connection *conn);
-void domain_outstanding_domid_dec(unsigned int domid);
+void domain_outstanding_inc(struct connection *conn, unsigned int domid);
+void domain_outstanding_dec(struct connection *conn, unsigned int domid);
 int domain_get_quota(const void *ctx, struct connection *conn,
 		     unsigned int domid);
 
Signature · 1 line
2.35.3
#18 lore raw

Re: [PATCH v4 07/13] tools/xenstore: use accounting data array for per-domain values

Hi Juergen,

Quoted 17 lines · Juergen Gross
tools/xenstore/xenstored_domain.h +4 -1
index 5cfd730cf6..0d61bf4344 100644
@@ -28,7 +28,10 @@ enum accitem {
  	ACC_NODES,
  	ACC_REQ_N,		/* Number of elements per request. */
  	ACC_TR_N = ACC_REQ_N,	/* Number of elements per transaction. */
-	ACC_N = ACC_TR_N,	/* Number of elements per domain. */
+	ACC_WATCH = ACC_TR_N,
+	ACC_OUTST,
+	ACC_MEM,
+	ACC_N,			/* Number of elements per domain. */
  };
 
  void handle_event(void);

tools/xenstore/xenstored_domain.h static inline void domain_memory_add_nochk(struct connection *conn, · line 113

Quoted 7 lines
tools/xenstore/xenstored_domain.h +1 -3
@@ -107,9 +110,8 @@ static inline void domain_memory_add_nochk(struct connection *conn,
  void domain_watch_inc(struct connection *conn);
  void domain_watch_dec(struct connection *conn);
  int domain_watch(struct connection *conn);
-void domain_outstanding_inc(struct connection *conn);
-void domain_outstanding_dec(struct connection *conn);
-void domain_outstanding_domid_dec(unsigned int domid);
+void domain_outstanding_inc(struct connection *conn, unsigned int domid);

AFAICT, all the caller of domain_outstanding_inc() will pass 'conn->id'. So it is not entirely clear what's the benefits to add the extra parameter.

I am not against this change (and same for removing *domid_dec()). But I think this ought to be explained in the commit message as this feels unrelated.

tools/xenstore/xenstored_domain.h static inline void domain_memory_add_nochk(struct connection *conn, · line 117

Quoted 4 lines
tools/xenstore/xenstored_domain.h +1 -0
+void domain_outstanding_dec(struct connection *conn, unsigned int domid);
  int domain_get_quota(const void *ctx, struct connection *conn,
  		     unsigned int domid);
 

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [PATCH v4 07/13] tools/xenstore: use accounting data array for per-domain values

Quoted 21 lines · Julien Grall

Hi Juergen,

Quoted 18 lines · Juergen Gross
tools/xenstore/xenstored_domain.h +0 -0
index 5cfd730cf6..0d61bf4344 100644
@@ -28,7 +28,10 @@ enum accitem {
       ACC_NODES,

      ACC_REQ_N,        /* Number of elements per request. */ ACC_TR_N = ACC_REQ_N,    /* Number of elements per transaction. */ -    ACC_N = ACC_TR_N,    /* Number of elements per domain. */ +    ACC_WATCH = ACC_TR_N,
+    ACC_OUTST,
+    ACC_MEM,
+    ACC_N,            /* Number of elements per domain. */ };
  void handle_event(void);

tools/xenstore/xenstored_domain.h +0 -0
@@ -107,9 +110,8 @@ static inline void domain_memory_add_nochk(struct 
 connection *conn,
   void domain_watch_inc(struct connection *conn);
Quoted 8 lines

tools/xenstore/xenstored_domain.h static inline void domain_memory_add_nochk(struct connection *conn, · line 113

Quoted 6 lines
tools/xenstore/xenstored_domain.h +1 -3
   void domain_watch_dec(struct connection *conn);
   int domain_watch(struct connection *conn);
-void domain_outstanding_inc(struct connection *conn);
-void domain_outstanding_dec(struct connection *conn);
-void domain_outstanding_domid_dec(unsigned int domid);
+void domain_outstanding_inc(struct connection *conn, unsigned int domid);

AFAICT, all the caller of domain_outstanding_inc() will pass 'conn->id'. So it is not entirely clear what's the benefits to add the extra parameter.

domain_acc_add() will need conn. I agree that I should drop the domid parameter.

Juergen

#20 lore raw

PATCH v4 8/13 tools/xenstore: add accounting trace support

In reply to Juergen Gross

Add a new trace switch "acc" and the related trace calls.

The "acc" switch is off per default.

---
 tools/xenstore/xenstored_core.c   |  2 +-
 tools/xenstore/xenstored_core.h   |  1 +
 tools/xenstore/xenstored_domain.c | 10 ++++++++++
 3 files changed, 12 insertions(+), 1 deletion(-)
tools/xenstore/xenstored_core.c +1 -1
index 67aa7c1578..bd816ce709 100644
@@ -2746,7 +2746,7 @@ static void set_quota(const char *arg, bool soft)
 
 /* Sorted by bit values of TRACE_* flags. Flag is (1u << index). */
 const char *const trace_switches[] = {
-	"obj", "io", "wrl",
+	"obj", "io", "wrl", "acc",
 	NULL
 };
 
tools/xenstore/xenstored_core.h +1 -0
index 1f811f38cb..3e0734a6c6 100644
@@ -302,6 +302,7 @@ extern unsigned int trace_flags;
 #define TRACE_OBJ	0x00000001
 #define TRACE_IO	0x00000002
 #define TRACE_WRL	0x00000004
+#define TRACE_ACC	0x00000008
 extern const char *const trace_switches[];
 int set_trace_switch(const char *arg);
 
tools/xenstore/xenstored_domain.c +10 -0
index 22836ae881..1caa60bb14 100644
@@ -540,6 +540,12 @@ static struct domain *find_domain_by_domid(unsigned int domid)
 	return (d && d->introduced) ? d : NULL;
 }
 
+#define trace_acc(...)				\
+do {						\
+	if (trace_flags & TRACE_ACC)		\
+		trace("acc: " __VA_ARGS__);	\
+} while (0)
+
 int acc_fix_domains(struct list_head *head, bool chk_quota, bool update)
 {
 	struct changed_domain *cd;
@@ -603,6 +609,8 @@ static int acc_add_changed_dom(const void *ctx, struct list_head *head,
 		return 0;
 
 	errno = 0;
+	trace_acc("local change domid %u: what=%u %d add %d\n", domid, what,
+		  cd->acc[what], val);
 	cd->acc[what] += val;
 
 	return cd->acc[what];
@@ -1114,6 +1122,8 @@ static int domain_acc_add(struct connection *conn, unsigned int domid,
 		return domain_acc_add_valid(d, what, ret);
 	}
 
+	trace_acc("global change domid %u: what=%u %u add %d\n", domid, what,
+		  d->acc[what], add);
 	d->acc[what] = domain_acc_add_valid(d, what, add);
 
 	return d->acc[what];
Signature · 1 line
2.35.3
#21 lore raw

PATCH v4 9/13 tools/xenstore: add TDB access trace support

In reply to Juergen Gross

Add a new trace switch "tdb" and the related trace calls.

The "tdb" switch is off per default.

---
 tools/xenstore/xenstored_core.c        | 8 +++++++-
 tools/xenstore/xenstored_core.h        | 7 +++++++
 tools/xenstore/xenstored_transaction.c | 7 ++++++-
 3 files changed, 20 insertions(+), 2 deletions(-)
tools/xenstore/xenstored_core.c +7 -1
index bd816ce709..2d481fcad9 100644
@@ -589,6 +589,8 @@ static void get_acc_data(TDB_DATA *key, struct node_account_data *acc)
 		if (old_data.dptr == NULL) {
 			acc->memory = 0;
 		} else {
+			trace_tdb("read %s size %zu\n", key->dptr,
+				  old_data.dsize + key->dsize);
 			hdr = (void *)old_data.dptr;
 			acc->memory = old_data.dsize;
 			acc->domid = hdr->perms[0].id;
@@ -655,6 +657,7 @@ int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data,
 		errno = EIO;
 		return errno;
 	}
+	trace_tdb("store %s size %zu\n", key->dptr, data->dsize + key->dsize);
 
 	if (acc) {
 		/* Don't use new_domid, as it might be a transaction node. */
@@ -682,6 +685,7 @@ int do_tdb_delete(struct connection *conn, TDB_DATA *key,
 		errno = EIO;
 		return errno;
 	}
+	trace_tdb("delete %s\n", key->dptr);
 
 	if (acc->memory) {
 		domid = get_acc_domid(conn, key, acc->domid);
@@ -731,6 +735,8 @@ struct node *read_node(struct connection *conn, const void *ctx,
 		goto error;
 	}
 
+	trace_tdb("read %s size %zu\n", key.dptr, data.dsize + key.dsize);
+
 	node->parent = NULL;
 	talloc_steal(node, data.dptr);
 
@@ -2746,7 +2752,7 @@ static void set_quota(const char *arg, bool soft)
 
 /* Sorted by bit values of TRACE_* flags. Flag is (1u << index). */
 const char *const trace_switches[] = {
-	"obj", "io", "wrl", "acc",
+	"obj", "io", "wrl", "acc", "tdb",
 	NULL
 };
 
tools/xenstore/xenstored_core.h +7 -0
index 3e0734a6c6..5a11dc1231 100644
@@ -303,9 +303,16 @@ extern unsigned int trace_flags;
 #define TRACE_IO	0x00000002
 #define TRACE_WRL	0x00000004
 #define TRACE_ACC	0x00000008
+#define TRACE_TDB	0x00000010
 extern const char *const trace_switches[];
 int set_trace_switch(const char *arg);
 
+#define trace_tdb(...)				\
+do {						\
+	if (trace_flags & TRACE_TDB)		\
+		trace("tdb: " __VA_ARGS__);	\
+} while (0)
+
 extern TDB_CONTEXT *tdb_ctx;
 extern int dom0_domid;
 extern int dom0_event;
tools/xenstore/xenstored_transaction.c +6 -1
index 2b15506953..11c8bcec84 100644
@@ -374,8 +374,11 @@ static int finalize_transaction(struct connection *conn,
 				if (tdb_error(tdb_ctx) != TDB_ERR_NOEXIST)
 					return EIO;
 				gen = NO_GENERATION;
-			} else
+			} else {
+				trace_tdb("read %s size %zu\n", key.dptr,
+					  key.dsize + data.dsize);
 				gen = hdr->generation;
+			}
 			talloc_free(data.dptr);
 			if (i->generation != gen)
 				return EAGAIN;
@@ -399,6 +402,8 @@ static int finalize_transaction(struct connection *conn,
 			set_tdb_key(i->trans_name, &ta_key);
 			data = tdb_fetch(tdb_ctx, ta_key);
 			if (data.dptr) {
+				trace_tdb("read %s size %zu\n", ta_key.dptr,
+					  ta_key.dsize + data.dsize);
 				hdr = (void *)data.dptr;
 				hdr->generation = ++generation;
 				*is_corrupt |= do_tdb_write(conn, &key, &data,
Signature · 1 line
2.35.3
#22 lore raw

PATCH v4 10/13 tools/xenstore: switch transaction accounting to generic accounting

In reply to Juergen Gross

As transaction accounting is active for unprivileged domains only, it can easily be added to the generic per-domain accounting.

---
 tools/xenstore/xenstored_core.c        |  3 +--
 tools/xenstore/xenstored_core.h        |  1 -
 tools/xenstore/xenstored_domain.c      | 21 ++++++++++++++++++---
 tools/xenstore/xenstored_domain.h      |  4 ++++
 tools/xenstore/xenstored_transaction.c | 12 +++++-------
 5 files changed, 28 insertions(+), 13 deletions(-)
tools/xenstore/xenstored_core.c +1 -2
index 2d481fcad9..88c569b7d5 100644
@@ -2083,7 +2083,7 @@ static void consider_message(struct connection *conn)
 	 * stalled. This will ignore new requests until Live-Update happened
 	 * or it was aborted.
 	 */
-	if (lu_is_pending() && conn->transaction_started == 0 &&
+	if (lu_is_pending() && conn->ta_start_time == 0 &&
 	    conn->in->hdr.msg.type == XS_TRANSACTION_START) {
 		trace("Delaying transaction start for connection %p req_id %u\n",
 		      conn, conn->in->hdr.msg.req_id);
@@ -2190,7 +2190,6 @@ struct connection *new_connection(const struct interface_funcs *funcs)
 	new->funcs = funcs;
 	new->is_ignored = false;
 	new->is_stalled = false;
-	new->transaction_started = 0;
 	INIT_LIST_HEAD(&new->out_list);
 	INIT_LIST_HEAD(&new->acc_list);
 	INIT_LIST_HEAD(&new->ref_list);
tools/xenstore/xenstored_core.h +0 -1
index 5a11dc1231..3564d85d7d 100644
@@ -151,7 +151,6 @@ struct connection
 	/* List of in-progress transactions. */
 	struct list_head transaction_list;
 	uint32_t next_transaction_id;
-	unsigned int transaction_started;
 	time_t ta_start_time;
 
 	/* List of delayed requests. */
tools/xenstore/xenstored_domain.c +18 -3
index 1caa60bb14..40bcc1dbfa 100644
@@ -419,12 +419,10 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 {
 	struct domain *d = find_domain_struct(domid);
 	char *resp;
-	int ta;
 
 	if (!d)
 		return ENOENT;
 
-	ta = d->conn ? d->conn->transaction_started : 0;
 	resp = talloc_asprintf(ctx, "Domain %u:\n", domid);
 	if (!resp)
 		return ENOMEM;
@@ -435,7 +433,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 
 	ent(nodes, d->acc[ACC_NODES]);
 	ent(watches, d->acc[ACC_WATCH]);
-	ent(transactions, ta);
+	ent(transactions, d->acc[ACC_TRANS]);
 	ent(outstanding, d->acc[ACC_OUTST]);
 	ent(memory, d->acc[ACC_MEM]);
 
@@ -1297,6 +1295,23 @@ void domain_outstanding_dec(struct connection *conn, unsigned int domid)
 	domain_acc_add(conn, domid, ACC_OUTST, -1, true);
 }
 
+void domain_transaction_inc(struct connection *conn)
+{
+	domain_acc_add(conn, conn->id, ACC_TRANS, 1, true);
+}
+
+void domain_transaction_dec(struct connection *conn)
+{
+	domain_acc_add(conn, conn->id, ACC_TRANS, -1, true);
+}
+
+unsigned int domain_transaction_get(struct connection *conn)
+{
+	return (domain_is_unprivileged(conn))
+		? domain_acc_add(conn, conn->id, ACC_TRANS, 0, true)
+		: 0;
+}
+
 static wrl_creditt wrl_config_writecost      = WRL_FACTOR;
 static wrl_creditt wrl_config_rate           = WRL_RATE   * WRL_FACTOR;
 static wrl_creditt wrl_config_dburst         = WRL_DBURST * WRL_FACTOR;
tools/xenstore/xenstored_domain.h +4 -0
index 0d61bf4344..abc766f343 100644
@@ -31,6 +31,7 @@ enum accitem {
 	ACC_WATCH = ACC_TR_N,
 	ACC_OUTST,
 	ACC_MEM,
+	ACC_TRANS,
 	ACC_N,			/* Number of elements per domain. */
 };
 
@@ -112,6 +113,9 @@ void domain_watch_dec(struct connection *conn);
 int domain_watch(struct connection *conn);
 void domain_outstanding_inc(struct connection *conn, unsigned int domid);
 void domain_outstanding_dec(struct connection *conn, unsigned int domid);
+void domain_transaction_inc(struct connection *conn);
+void domain_transaction_dec(struct connection *conn);
+unsigned int domain_transaction_get(struct connection *conn);
 int domain_get_quota(const void *ctx, struct connection *conn,
 		     unsigned int domid);
 
tools/xenstore/xenstored_transaction.c +5 -7
index 11c8bcec84..1c14b8579a 100644
@@ -479,8 +479,7 @@ int do_transaction_start(const void *ctx, struct connection *conn,
 	if (conn->transaction)
 		return EBUSY;
 
-	if (domain_is_unprivileged(conn) &&
-	    conn->transaction_started > quota_max_transaction)
+	if (domain_transaction_get(conn) > quota_max_transaction)
 		return ENOSPC;
 
 	/* Attach transaction to ctx for autofree until it's complete */
@@ -505,9 +504,9 @@ int do_transaction_start(const void *ctx, struct connection *conn,
 	list_add_tail(&trans->list, &conn->transaction_list);
 	talloc_steal(conn, trans);
 	talloc_set_destructor(trans, destroy_transaction);
-	if (!conn->transaction_started)
+	if (!conn->ta_start_time)
 		conn->ta_start_time = time(NULL);
-	conn->transaction_started++;
+	domain_transaction_inc(conn);
 	wrl_ntransactions++;
 
 	snprintf(id_str, sizeof(id_str), "%u", trans->id);
@@ -533,8 +532,8 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 
 	conn->transaction = NULL;
 	list_del(&trans->list);
-	conn->transaction_started--;
-	if (!conn->transaction_started)
+	domain_transaction_dec(conn);
+	if (list_empty(&conn->transaction_list))
 		conn->ta_start_time = 0;
 
 	chk_quota = trans->node_created && domain_is_unprivileged(conn);
@@ -588,7 +587,6 @@ void conn_delete_all_transactions(struct connection *conn)
 
 	assert(conn->transaction == NULL);
 
-	conn->transaction_started = 0;
 	conn->ta_start_time = 0;
 }
 
Signature · 1 line
2.35.3
#23 lore raw

Re: [PATCH v4 10/13] tools/xenstore: switch transaction accounting to generic accounting

Hi,

Quoted 17 lines · Juergen Gross

As transaction accounting is active for unprivileged domains only, it can easily be added to the generic per-domain accounting.

---
  tools/xenstore/xenstored_core.c        |  3 +--
  tools/xenstore/xenstored_core.h        |  1 -
  tools/xenstore/xenstored_domain.c      | 21 ++++++++++++++++++---
  tools/xenstore/xenstored_domain.h      |  4 ++++
  tools/xenstore/xenstored_transaction.c | 12 +++++-------
  5 files changed, 28 insertions(+), 13 deletions(-)
tools/xenstore/xenstored_core.c +0 -0
index 2d481fcad9..88c569b7d5 100644

tools/xenstore/xenstored_core.c static void consider_message(struct connection *conn) · line 2086

Quoted 5 lines
tools/xenstore/xenstored_core.c +1 -1
@@ -2083,7 +2083,7 @@ static void consider_message(struct connection *conn)
  	 * stalled. This will ignore new requests until Live-Update happened
  	 * or it was aborted.
  	 */
-	if (lu_is_pending() && conn->transaction_started == 0 &&
+	if (lu_is_pending() && conn->ta_start_time == 0 &&

NIT: I know there are some places in the code checking for conn->ta_start_time == 0. But it feels like a better replacement to "conn->transaction_started" is "list_empty(...)".

I agree this is going to be more expensive. But you are switching the transaction accounting to a generic infrastructure which is pretty heavy compare to a simple addition/substraction. So I think a "list_empty()" would be OK here.

Quoted 110 lines
tools/xenstore/xenstored_core.c +0 -1
  	    conn->in->hdr.msg.type == XS_TRANSACTION_START) {
  		trace("Delaying transaction start for connection %p req_id %u\n",
  		      conn, conn->in->hdr.msg.req_id);
@@ -2190,7 +2190,6 @@ struct connection *new_connection(const struct interface_funcs *funcs)
  	new->funcs = funcs;
  	new->is_ignored = false;
  	new->is_stalled = false;
-	new->transaction_started = 0;
  	INIT_LIST_HEAD(&new->out_list);
  	INIT_LIST_HEAD(&new->acc_list);
  	INIT_LIST_HEAD(&new->ref_list);
tools/xenstore/xenstored_core.h +0 -1
index 5a11dc1231..3564d85d7d 100644
@@ -151,7 +151,6 @@ struct connection
  	/* List of in-progress transactions. */
  	struct list_head transaction_list;
  	uint32_t next_transaction_id;
-	unsigned int transaction_started;
  	time_t ta_start_time;
 
  	/* List of delayed requests. */
tools/xenstore/xenstored_domain.c +18 -3
index 1caa60bb14..40bcc1dbfa 100644
@@ -419,12 +419,10 @@ int domain_get_quota(const void *ctx, struct connection *conn,
  {
  	struct domain *d = find_domain_struct(domid);
  	char *resp;
-	int ta;
 
  	if (!d)
  		return ENOENT;
 
-	ta = d->conn ? d->conn->transaction_started : 0;
  	resp = talloc_asprintf(ctx, "Domain %u:\n", domid);
  	if (!resp)
  		return ENOMEM;
@@ -435,7 +433,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 
  	ent(nodes, d->acc[ACC_NODES]);
  	ent(watches, d->acc[ACC_WATCH]);
-	ent(transactions, ta);
+	ent(transactions, d->acc[ACC_TRANS]);
  	ent(outstanding, d->acc[ACC_OUTST]);
  	ent(memory, d->acc[ACC_MEM]);
 
@@ -1297,6 +1295,23 @@ void domain_outstanding_dec(struct connection *conn, unsigned int domid)
  	domain_acc_add(conn, domid, ACC_OUTST, -1, true);
  }
 
+void domain_transaction_inc(struct connection *conn)
+{
+	domain_acc_add(conn, conn->id, ACC_TRANS, 1, true);
+}
+
+void domain_transaction_dec(struct connection *conn)
+{
+	domain_acc_add(conn, conn->id, ACC_TRANS, -1, true);
+}
+
+unsigned int domain_transaction_get(struct connection *conn)
+{
+	return (domain_is_unprivileged(conn))
+		? domain_acc_add(conn, conn->id, ACC_TRANS, 0, true)
+		: 0;
+}
+
  static wrl_creditt wrl_config_writecost      = WRL_FACTOR;
  static wrl_creditt wrl_config_rate           = WRL_RATE   * WRL_FACTOR;
  static wrl_creditt wrl_config_dburst         = WRL_DBURST * WRL_FACTOR;
tools/xenstore/xenstored_domain.h +4 -0
index 0d61bf4344..abc766f343 100644
@@ -31,6 +31,7 @@ enum accitem {
  	ACC_WATCH = ACC_TR_N,
  	ACC_OUTST,
  	ACC_MEM,
+	ACC_TRANS,
  	ACC_N,			/* Number of elements per domain. */
  };
 
@@ -112,6 +113,9 @@ void domain_watch_dec(struct connection *conn);
  int domain_watch(struct connection *conn);
  void domain_outstanding_inc(struct connection *conn, unsigned int domid);
  void domain_outstanding_dec(struct connection *conn, unsigned int domid);
+void domain_transaction_inc(struct connection *conn);
+void domain_transaction_dec(struct connection *conn);
+unsigned int domain_transaction_get(struct connection *conn);
  int domain_get_quota(const void *ctx, struct connection *conn,
  		     unsigned int domid);
 
tools/xenstore/xenstored_transaction.c +1 -2
index 11c8bcec84..1c14b8579a 100644
@@ -479,8 +479,7 @@ int do_transaction_start(const void *ctx, struct connection *conn,
  	if (conn->transaction)
  		return EBUSY;
 
-	if (domain_is_unprivileged(conn) &&
-	    conn->transaction_started > quota_max_transaction)
+	if (domain_transaction_get(conn) > quota_max_transaction)
  		return ENOSPC;
 
  	/* Attach transaction to ctx for autofree until it's complete */

tools/xenstore/xenstored_transaction.c int do_transaction_start(const void *ctx, struct connection *conn, · line 507

Quoted 5 lines
tools/xenstore/xenstored_transaction.c +1 -1
@@ -505,9 +504,9 @@ int do_transaction_start(const void *ctx, struct connection *conn,
  	list_add_tail(&trans->list, &conn->transaction_list);
  	talloc_steal(conn, trans);
  	talloc_set_destructor(trans, destroy_transaction);
-	if (!conn->transaction_started)
+	if (!conn->ta_start_time)

I think it would make more sense to move this if just before the list_add_tail() and use (list_empty()) for the checked. This would make the code more consistent...

Quoted 7 lines
tools/xenstore/xenstored_transaction.c +1 -1
  		conn->ta_start_time = time(NULL);
-	conn->transaction_started++;
+	domain_transaction_inc(conn);
  	wrl_ntransactions++;
 
  	snprintf(id_str, sizeof(id_str), "%u", trans->id);

tools/xenstore/xenstored_transaction.c int do_transaction_end(const void *ctx, struct connection *conn, · line 537

Quoted 8 lines
tools/xenstore/xenstored_transaction.c +2 -2
@@ -533,8 +532,8 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 
  	conn->transaction = NULL;
  	list_del(&trans->list);
-	conn->transaction_started--;
-	if (!conn->transaction_started)
+	domain_transaction_dec(conn);
+	if (list_empty(&conn->transaction_list))
  		conn->ta_start_time = 0;

... with this check and also easier to spot they are related.

tools/xenstore/xenstored_transaction.c void conn_delete_all_transactions(struct connection *conn) · line 592

Quoted 10 lines
tools/xenstore/xenstored_transaction.c +0 -1
  	chk_quota = trans->node_created && domain_is_unprivileged(conn);
@@ -588,7 +587,6 @@ void conn_delete_all_transactions(struct connection *conn)
 
  	assert(conn->transaction == NULL);
 
-	conn->transaction_started = 0;
  	conn->ta_start_time = 0;
  }
 

Cheers,

Signature · 1 line
Julien Grall
#24 lore raw

Re: [PATCH v4 10/13] tools/xenstore: switch transaction accounting to generic accounting

Quoted 15 lines · Julien Grall

Hi,

Quoted 12 lines · Juergen Gross

As transaction accounting is active for unprivileged domains only, it can easily be added to the generic per-domain accounting.

---
  tools/xenstore/xenstored_core.c        |  3 +-- tools/xenstore/xenstored_core.h        |  1 - tools/xenstore/xenstored_domain.c      | 21 ++++++++++++++++++--- tools/xenstore/xenstored_domain.h      |  4 ++++ tools/xenstore/xenstored_transaction.c | 12 +++++-------

  5 files changed, 28 insertions(+), 13 deletions(-)
Quoted 12 lines

tools/xenstore/xenstored_core.c static void consider_message(struct connection *conn) · line 2086

Quoted 10 lines
tools/xenstore/xenstored_core.c +1 -1
index 2d481fcad9..88c569b7d5 100644
@@ -2083,7 +2083,7 @@ static void consider_message(struct connection *conn)
        * stalled. This will ignore new requests until Live-Update happened
        * or it was aborted.
        */
-    if (lu_is_pending() && conn->transaction_started == 0 &&
+    if (lu_is_pending() && conn->ta_start_time == 0 &&

NIT: I know there are some places in the code checking for conn->ta_start_time == 0. But it feels like a better replacement to "conn->transaction_started" is "list_empty(...)".

Fine with me.

Quoted 108 lines

I agree this is going to be more expensive. But you are switching the transaction accounting to a generic infrastructure which is pretty heavy compare to a simple addition/substraction. So I think a "list_empty()" would be OK here.

Quoted 105 lines
tools/xenstore/xenstored_core.c +0 -1
           conn->in->hdr.msg.type == XS_TRANSACTION_START) {
           trace("Delaying transaction start for connection %p req_id %u\n",
                 conn, conn->in->hdr.msg.req_id);
@@ -2190,7 +2190,6 @@ struct connection *new_connection(const struct 
 interface_funcs *funcs)
       new->funcs = funcs;
       new->is_ignored = false;
       new->is_stalled = false;
-    new->transaction_started = 0;
       INIT_LIST_HEAD(&new->out_list);
       INIT_LIST_HEAD(&new->acc_list);

      INIT_LIST_HEAD(&new->ref_list);

tools/xenstore/xenstored_core.h +0 -1
index 5a11dc1231..3564d85d7d 100644
@@ -151,7 +151,6 @@ struct connection
       /* List of in-progress transactions. */
       struct list_head transaction_list;
       uint32_t next_transaction_id;
-    unsigned int transaction_started;
       time_t ta_start_time;
       /* List of delayed requests. */
tools/xenstore/xenstored_domain.c +18 -3
index 1caa60bb14..40bcc1dbfa 100644
@@ -419,12 +419,10 @@ int domain_get_quota(const void *ctx, struct connection 
 *conn,
   {
       struct domain *d = find_domain_struct(domid);
       char *resp;
-    int ta;
       if (!d)
           return ENOENT;
-    ta = d->conn ? d->conn->transaction_started : 0;
       resp = talloc_asprintf(ctx, "Domain %u:\n", domid);
       if (!resp)
           return ENOMEM;
@@ -435,7 +433,7 @@ int domain_get_quota(const void *ctx, struct connection 
 *conn,
       ent(nodes, d->acc[ACC_NODES]);
       ent(watches, d->acc[ACC_WATCH]);
-    ent(transactions, ta);
+    ent(transactions, d->acc[ACC_TRANS]);
       ent(outstanding, d->acc[ACC_OUTST]);
       ent(memory, d->acc[ACC_MEM]);
@@ -1297,6 +1295,23 @@ void domain_outstanding_dec(struct connection *conn, 
 unsigned int domid)
       domain_acc_add(conn, domid, ACC_OUTST, -1, true);
   }
+void domain_transaction_inc(struct connection *conn)
+{
+    domain_acc_add(conn, conn->id, ACC_TRANS, 1, true);
+}
+
+void domain_transaction_dec(struct connection *conn)
+{
+    domain_acc_add(conn, conn->id, ACC_TRANS, -1, true);
+}
+
+unsigned int domain_transaction_get(struct connection *conn)
+{
+    return (domain_is_unprivileged(conn))
+        ? domain_acc_add(conn, conn->id, ACC_TRANS, 0, true)
+        : 0;
+}
+
   static wrl_creditt wrl_config_writecost      = WRL_FACTOR;
   static wrl_creditt wrl_config_rate           = WRL_RATE   * WRL_FACTOR;
   static wrl_creditt wrl_config_dburst         = WRL_DBURST * WRL_FACTOR;
tools/xenstore/xenstored_domain.h +1 -0
index 0d61bf4344..abc766f343 100644
@@ -31,6 +31,7 @@ enum accitem {
       ACC_WATCH = ACC_TR_N,
       ACC_OUTST,
       ACC_MEM,
+    ACC_TRANS,

      ACC_N,            /* Number of elements per domain. */ };

tools/xenstore/xenstored_domain.h +3 -0
@@ -112,6 +113,9 @@ void domain_watch_dec(struct connection *conn);
   int domain_watch(struct connection *conn);
   void domain_outstanding_inc(struct connection *conn, unsigned int domid);
   void domain_outstanding_dec(struct connection *conn, unsigned int domid);
+void domain_transaction_inc(struct connection *conn);
+void domain_transaction_dec(struct connection *conn);
+unsigned int domain_transaction_get(struct connection *conn);
   int domain_get_quota(const void *ctx, struct connection *conn,
                unsigned int domid);
tools/xenstore/xenstored_transaction.c +1 -2
index 11c8bcec84..1c14b8579a 100644
@@ -479,8 +479,7 @@ int do_transaction_start(const void *ctx, struct 
 connection *conn,
       if (conn->transaction)
           return EBUSY;
-    if (domain_is_unprivileged(conn) &&
-        conn->transaction_started > quota_max_transaction)
+    if (domain_transaction_get(conn) > quota_max_transaction)
           return ENOSPC;
       /* Attach transaction to ctx for autofree until it's complete */
@@ -505,9 +504,9 @@ int do_transaction_start(const void *ctx, struct 
Quoted 8 lines

tools/xenstore/xenstored_transaction.c int do_transaction_start(const void *ctx, struct connection *conn, · line 507

Quoted 6 lines
tools/xenstore/xenstored_transaction.c +1 -1
 connection *conn,
       list_add_tail(&trans->list, &conn->transaction_list);
       talloc_steal(conn, trans);
       talloc_set_destructor(trans, destroy_transaction);
-    if (!conn->transaction_started)
+    if (!conn->ta_start_time)

I think it would make more sense to move this if just before the list_add_tail() and use (list_empty()) for the checked. This would make the code more consistent...

Okay.

Juergen

#25 lore raw

PATCH v4 11/13 tools/xenstore: remember global and per domain max accounting values

In reply to Juergen Gross

Add saving the maximum values of the different accounting data seen per domain and (for unprivileged domains) globally, and print those values via the xenstore-control quota command. Add a sub-command for resetting the global maximum values seen.

This should help for a decision how to set the related quotas.

---
 docs/misc/xenstore.txt             |   5 +-
 tools/xenstore/xenstored_control.c |  22 ++++++-
 tools/xenstore/xenstored_domain.c  | 100 +++++++++++++++++++++++------
 tools/xenstore/xenstored_domain.h  |   2 +
 4 files changed, 108 insertions(+), 21 deletions(-)
docs/misc/xenstore.txt +4 -1
index d807ef0709..38015835b1 100644
@@ -426,7 +426,7 @@ CONTROL			<command>|[<parameters>|]
 	print|<string>
 		print <string> to syslog (xenstore runs as daemon) or
 		to console (xenstore runs as stubdom)
-	quota|[set <name> <val>|<domid>]
+	quota|[set <name> <val>|<domid>|max [-r]]
 		without parameters: print the current quota settings
 		with "set <name> <val>": set the quota <name> to new value
 		<val> (The admin should make sure all the domain usage is
@@ -435,6 +435,9 @@ CONTROL			<command>|[<parameters>|]
 		violating the new quota setting isn't increased further)
 		with "<domid>": print quota related accounting data for
 		the domain <domid>
+		with "max [-r]": show global per-domain maximum values of all
+		unprivileged domains, optionally reset the values by adding
+		"-r"
 	quota-soft|[set <name> <val>]
 		like the "quota" command, but for soft-quota.
 	help			<supported-commands>
tools/xenstore/xenstored_control.c +21 -1
index cbd62556c3..a2ba64a15c 100644
@@ -306,6 +306,22 @@ static int quota_get(const void *ctx, struct connection *conn,
 	return domain_get_quota(ctx, conn, atoi(vec[0]));
 }
 
+static int quota_max(const void *ctx, struct connection *conn,
+		     char **vec, int num)
+{
+	if (num > 1)
+		return EINVAL;
+
+	if (num == 1) {
+		if (!strcmp(vec[0], "-r"))
+			domain_reset_global_acc();
+		else
+			return EINVAL;
+	}
+
+	return domain_max_global_acc(ctx, conn);
+}
+
 static int do_control_quota(const void *ctx, struct connection *conn,
 			    char **vec, int num)
 {
@@ -315,6 +331,9 @@ static int do_control_quota(const void *ctx, struct connection *conn,
 	if (!strcmp(vec[0], "set"))
 		return quota_set(ctx, conn, vec + 1, num - 1, hard_quotas);
 
+	if (!strcmp(vec[0], "max"))
+		return quota_max(ctx, conn, vec + 1, num - 1);
+
 	return quota_get(ctx, conn, vec, num);
 }
 
@@ -978,7 +997,8 @@ static struct cmd_s cmds[] = {
 	{ "memreport", do_control_memreport, "[<file>]" },
 #endif
 	{ "print", do_control_print, "<string>" },
-	{ "quota", do_control_quota, "[set <name> <val>|<domid>]" },
+	{ "quota", do_control_quota,
+		"[set <name> <val>|<domid>|max [-r]]" },
 	{ "quota-soft", do_control_quota_s, "[set <name> <val>]" },
 	{ "help", do_control_help, "" },
 };
tools/xenstore/xenstored_domain.c +81 -19

178 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +2 -0
index abc766f343..b55c9bcc2d 100644
@@ -126,6 +126,8 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 int acc_fix_domains(struct list_head *head, bool chk_quota, bool update);
 void acc_drop(struct connection *conn);
 void acc_commit(struct connection *conn);
+int domain_max_global_acc(const void *ctx, struct connection *conn);
+void domain_reset_global_acc(void);
 
 /* Write rate limiting */
 
Signature · 1 line
2.35.3
Reviewed-by on v4 11/13
#26 lore raw

Re: [PATCH v4 11/13] tools/xenstore: remember global and per domain max accounting values

Hi Juergen,

Quoted 8 lines · Juergen Gross

Add saving the maximum values of the different accounting data seen per domain and (for unprivileged domains) globally, and print those values via the xenstore-control quota command. Add a sub-command for resetting the global maximum values seen.

This should help for a decision how to set the related quotas.

Cheers,

Signature · 1 line
Julien Grall
#27 lore raw

PATCH v4 12/13 tools/xenstore: use generic accounting for remaining quotas

In reply to Juergen Gross

The maxrequests, node size, number of node permissions, and path length quota are a little bit special, as they are either active in transactions only (maxrequests), or they are just per item instead of count values. Nevertheless being able to know the maximum number of those quota related values per domain would be beneficial, so add them to the generic accounting.

The per domain value will never show current numbers other than zero, but the maximum number seen can be gathered the same way as the number of nodes during a transaction.

To be able to use the const qualifier for a new function switch domain_is_unprivileged() to take a const pointer, too.

---
 tools/xenstore/xenstored_core.c        | 14 ++++-----
 tools/xenstore/xenstored_core.h        |  2 +-
 tools/xenstore/xenstored_domain.c      | 39 ++++++++++++++++++++------
 tools/xenstore/xenstored_domain.h      |  6 ++++
 tools/xenstore/xenstored_transaction.c |  4 +--
 tools/xenstore/xenstored_watch.c       |  2 +-
 6 files changed, 48 insertions(+), 19 deletions(-)
tools/xenstore/xenstored_core.c +7 -7
index 88c569b7d5..65df2866bf 100644
@@ -799,8 +799,8 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
 		+ node->perms.num * sizeof(node->perms.p[0])
 		+ node->datalen + node->childlen;
 
-	if (!no_quota_check && domain_is_unprivileged(conn) &&
-	    data.dsize >= quota_max_entry_size) {
+	if (domain_max_chk(conn, ACC_NODESZ, data.dsize, quota_max_entry_size)
+	    && !no_quota_check) {
 		errno = ENOSPC;
 		return errno;
 	}
@@ -1168,7 +1168,7 @@ static bool valid_chars(const char *node)
 		       "0123456789-/_@") == strlen(node));
 }
 
-bool is_valid_nodename(const char *node)
+bool is_valid_nodename(const struct connection *conn, const char *node)
 {
 	int local_off = 0;
 	unsigned int domid;
@@ -1188,7 +1188,8 @@ bool is_valid_nodename(const char *node)
 	if (sscanf(node, "/local/domain/%5u/%n", &domid, &local_off) != 1)
 		local_off = 0;
 
-	if (strlen(node) > local_off + quota_max_path_len)
+	if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off,
+			   quota_max_path_len))
 		return false;
 
 	return valid_chars(node);
@@ -1250,7 +1251,7 @@ static struct node *get_node_canonicalized(struct connection *conn,
 	*canonical_name = canonicalize(conn, ctx, name);
 	if (!*canonical_name)
 		return NULL;
-	if (!is_valid_nodename(*canonical_name)) {
+	if (!is_valid_nodename(conn, *canonical_name)) {
 		errno = EINVAL;
 		return NULL;
 	}
@@ -1775,8 +1776,7 @@ static int do_set_perms(const void *ctx, struct connection *conn,
 		return EINVAL;
 
 	perms.num--;
-	if (domain_is_unprivileged(conn) &&
-	    perms.num > quota_nb_perms_per_node)
+	if (domain_max_chk(conn, ACC_NPERM, perms.num, quota_nb_perms_per_node))
 		return ENOSPC;
 
 	permstr = in->buffer + strlen(in->buffer) + 1;
tools/xenstore/xenstored_core.h +1 -1
index 3564d85d7d..9339820156 100644
@@ -258,7 +258,7 @@ void check_store(void);
 void corrupt(struct connection *conn, const char *fmt, ...);
 
 /* Is this a valid node name? */
-bool is_valid_nodename(const char *node);
+bool is_valid_nodename(const struct connection *conn, const char *node);
 
 /* Get name of parent node. */
 char *get_parent(const void *ctx, const char *node);
tools/xenstore/xenstored_domain.c +31 -8
index d21f31da92..49e2c5c82a 100644
@@ -433,7 +433,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 		return ENOMEM;
 
 #define ent(t, e) \
-	resp = talloc_asprintf_append(resp, "%-16s: %8u (max: %8u\n", #t, \
+	resp = talloc_asprintf_append(resp, "%-17s: %8u (max: %8u\n", #t, \
 				      d->acc[e].val, d->acc[e].max); \
 	if (!resp) return ENOMEM
 
@@ -442,6 +442,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
 	ent(transactions, ACC_TRANS);
 	ent(outstanding, ACC_OUTST);
 	ent(memory, ACC_MEM);
+	ent(transaction-nodes, ACC_TRANSNODES);
 
 #undef ent
 
@@ -459,7 +460,7 @@ int domain_max_global_acc(const void *ctx, struct connection *conn)
 		return ENOMEM;
 
 #define ent(t, e) \
-	resp = talloc_asprintf_append(resp, "%-16s: %8u\n", #t,   \
+	resp = talloc_asprintf_append(resp, "%-17s: %8u\n", #t,   \
 				      acc_global_max[e]);         \
 	if (!resp) return ENOMEM
 
@@ -468,6 +469,7 @@ int domain_max_global_acc(const void *ctx, struct connection *conn)
 	ent(transactions, ACC_TRANS);
 	ent(outstanding, ACC_OUTST);
 	ent(memory, ACC_MEM);
+	ent(transaction-nodes, ACC_TRANSNODES);
 
 #undef ent
 
@@ -1081,12 +1083,22 @@ int domain_adjust_node_perms(struct node *node)
 	return 0;
 }
 
+static void domain_acc_valid_max(struct domain *d, enum accitem what,
+				 unsigned int val)
+{
+	assert(what < ARRAY_SIZE(d->acc));
+	assert(what < ARRAY_SIZE(acc_global_max));
+
+	if (val > d->acc[what].max)
+		d->acc[what].max = val;
+	if (val > acc_global_max[what] && domid_is_unprivileged(d->domid))
+		acc_global_max[what] = val;
+}
+
 static int domain_acc_add_valid(struct domain *d, enum accitem what, int add)
 {
 	unsigned int val;
 
-	assert(what < ARRAY_SIZE(d->acc));
-
 	if ((add < 0 && -add > d->acc[what].val) ||
 	    (add > 0 && (INT_MAX - d->acc[what].val) < add)) {
 		/*
@@ -1100,10 +1112,7 @@ static int domain_acc_add_valid(struct domain *d, enum accitem what, int add)
 	}
 
 	val = d->acc[what].val + add;
-	if (val > d->acc[what].max)
-		d->acc[what].max = val;
-	if (val > acc_global_max[what] && domid_is_unprivileged(d->domid))
-		acc_global_max[what] = val;
+	domain_acc_valid_max(d, what, val);
 
 	return val;
 }
@@ -1221,6 +1230,20 @@ void domain_reset_global_acc(void)
 	hashtable_iterate(domhash, domain_reset_global_acc_sub, NULL);
 }
 
+bool domain_max_chk(const struct connection *conn, enum accitem what,
+		    unsigned int val, unsigned int quota)
+{
+	if (!conn || !conn->domain)
+		return false;
+
+	if (domain_is_unprivileged(conn) && val > quota)
+		return true;
+
+	domain_acc_valid_max(conn->domain, what, val);
+
+	return false;
+}
+
 int domain_nbentry_inc(struct connection *conn, unsigned int domid)
 {
 	return (domain_acc_add(conn, domid, ACC_NODES, 1, false) < 0)
tools/xenstore/xenstored_domain.h +6 -0
index b55c9bcc2d..3e17b63659 100644
@@ -32,6 +32,10 @@ enum accitem {
 	ACC_OUTST,
 	ACC_MEM,
 	ACC_TRANS,
+	ACC_TRANSNODES,
+	ACC_NPERM,
+	ACC_PATHLEN,
+	ACC_NODESZ,
 	ACC_N,			/* Number of elements per domain. */
 };
 
@@ -128,6 +132,8 @@ void acc_drop(struct connection *conn);
 void acc_commit(struct connection *conn);
 int domain_max_global_acc(const void *ctx, struct connection *conn);
 void domain_reset_global_acc(void);
+bool domain_max_chk(const struct connection *conn, unsigned int what,
+		    unsigned int val, unsigned int quota);
 
 /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +2 -2
index 1c14b8579a..0b256f9b18 100644
@@ -252,8 +252,8 @@ int access_node(struct connection *conn, struct node *node,
 
 	i = find_accessed_node(trans, node->name);
 	if (!i) {
-		if (trans->nodes >= quota_trans_nodes &&
-		    domain_is_unprivileged(conn)) {
+		if (domain_max_chk(conn, ACC_TRANSNODES, trans->nodes + 1,
+				   quota_trans_nodes)) {
 			ret = ENOSPC;
 			goto err;
 		}
tools/xenstore/xenstored_watch.c +1 -1
index e30cd89be3..61b1e3421e 100644
@@ -176,7 +176,7 @@ static int check_watch_path(struct connection *conn, const void *ctx,
 		*path = canonicalize(conn, ctx, *path);
 		if (!*path)
 			return errno;
-		if (!is_valid_nodename(*path))
+		if (!is_valid_nodename(conn, *path))
 			goto inval;
 	}
 
Signature · 1 line
2.35.3
#28 lore raw

Re: [PATCH v4 12/13] tools/xenstore: use generic accounting for remaining quotas

Quoted 29 lines · Juergen Gross

The maxrequests, node size, number of node permissions, and path length quota are a little bit special, as they are either active in transactions only (maxrequests), or they are just per item instead of count values. Nevertheless being able to know the maximum number of those quota related values per domain would be beneficial, so add them to the generic accounting.

The per domain value will never show current numbers other than zero, but the maximum number seen can be gathered the same way as the number of nodes during a transaction.

To be able to use the const qualifier for a new function switch domain_is_unprivileged() to take a const pointer, too.

---
  tools/xenstore/xenstored_core.c        | 14 ++++-----
  tools/xenstore/xenstored_core.h        |  2 +-
  tools/xenstore/xenstored_domain.c      | 39 ++++++++++++++++++++------
  tools/xenstore/xenstored_domain.h      |  6 ++++
  tools/xenstore/xenstored_transaction.c |  4 +--
  tools/xenstore/xenstored_watch.c       |  2 +-
  6 files changed, 48 insertions(+), 19 deletions(-)
tools/xenstore/xenstored_core.c +0 -0
index 88c569b7d5..65df2866bf 100644

tools/xenstore/xenstored_core.c int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node, · line 803

Quoted 7 lines
tools/xenstore/xenstored_core.c +2 -2
@@ -799,8 +799,8 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
  		+ node->perms.num * sizeof(node->perms.p[0])
  		+ node->datalen + node->childlen;
 
-	if (!no_quota_check && domain_is_unprivileged(conn) &&
-	    data.dsize >= quota_max_entry_size) {
+	if (domain_max_chk(conn, ACC_NODESZ, data.dsize, quota_max_entry_size)
+	    && !no_quota_check) {

It feels a bit odd to move the !no_quota_check right after the actual check. But AFAICT, you are doing it because domain_max_chk() will also update the maximum value seen by the current quota.

Is that correct? If so, it would be worth mentioning it in a comment.

Quoted 59 lines
tools/xenstore/xenstored_core.c +5 -5
  		errno = ENOSPC;
  		return errno;
  	}
@@ -1168,7 +1168,7 @@ static bool valid_chars(const char *node)
  		       "0123456789-/_@") == strlen(node));
  }
 
-bool is_valid_nodename(const char *node)
+bool is_valid_nodename(const struct connection *conn, const char *node)
  {
  	int local_off = 0;
  	unsigned int domid;
@@ -1188,7 +1188,8 @@ bool is_valid_nodename(const char *node)
  	if (sscanf(node, "/local/domain/%5u/%n", &domid, &local_off) != 1)
  		local_off = 0;
 
-	if (strlen(node) > local_off + quota_max_path_len)
+	if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off,
+			   quota_max_path_len))
  		return false;
 
  	return valid_chars(node);
@@ -1250,7 +1251,7 @@ static struct node *get_node_canonicalized(struct connection *conn,
  	*canonical_name = canonicalize(conn, ctx, name);
  	if (!*canonical_name)
  		return NULL;
-	if (!is_valid_nodename(*canonical_name)) {
+	if (!is_valid_nodename(conn, *canonical_name)) {
  		errno = EINVAL;
  		return NULL;
  	}
@@ -1775,8 +1776,7 @@ static int do_set_perms(const void *ctx, struct connection *conn,
  		return EINVAL;
 
  	perms.num--;
-	if (domain_is_unprivileged(conn) &&
-	    perms.num > quota_nb_perms_per_node)
+	if (domain_max_chk(conn, ACC_NPERM, perms.num, quota_nb_perms_per_node))
  		return ENOSPC;
 
  	permstr = in->buffer + strlen(in->buffer) + 1;
tools/xenstore/xenstored_core.h +1 -1
index 3564d85d7d..9339820156 100644
@@ -258,7 +258,7 @@ void check_store(void);
  void corrupt(struct connection *conn, const char *fmt, ...);
 
  /* Is this a valid node name? */
-bool is_valid_nodename(const char *node);
+bool is_valid_nodename(const struct connection *conn, const char *node);
 
  /* Get name of parent node. */
  char *get_parent(const void *ctx, const char *node);
tools/xenstore/xenstored_domain.c +0 -0
index d21f31da92..49e2c5c82a 100644

tools/xenstore/xenstored_domain.c int domain_get_quota(const void *ctx, struct connection *conn, · line 436

Quoted 5 lines
tools/xenstore/xenstored_domain.c +1 -1
@@ -433,7 +433,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
  		return ENOMEM;
 
  #define ent(t, e) \
-	resp = talloc_asprintf_append(resp, "%-16s: %8u (max: %8u\n", #t, \
+	resp = talloc_asprintf_append(resp, "%-17s: %8u (max: %8u\n", #t, \

This changes feels a bit unrelated. Can you mention why this is necessary in the commit message?

tools/xenstore/xenstored_domain.c int domain_get_quota(const void *ctx, struct connection *conn, · line 445

Quoted 8 lines
tools/xenstore/xenstored_domain.c +1 -0
  				      d->acc[e].val, d->acc[e].max); \
  	if (!resp) return ENOMEM
 
@@ -442,6 +442,7 @@ int domain_get_quota(const void *ctx, struct connection *conn,
  	ent(transactions, ACC_TRANS);
  	ent(outstanding, ACC_OUTST);
  	ent(memory, ACC_MEM);
+	ent(transaction-nodes, ACC_TRANSNODES);

You seem to convert multiple quotas but only print one. Why?

tools/xenstore/xenstored_domain.c int domain_max_global_acc(const void *ctx, struct connection *conn) · line 464

Quoted 10 lines
tools/xenstore/xenstored_domain.c +1 -1
  #undef ent
 
@@ -459,7 +460,7 @@ int domain_max_global_acc(const void *ctx, struct connection *conn)
  		return ENOMEM;
 
  #define ent(t, e) \
-	resp = talloc_asprintf_append(resp, "%-16s: %8u\n", #t,   \
+	resp = talloc_asprintf_append(resp, "%-17s: %8u\n", #t,   \
  				      acc_global_max[e]);         \

Ditto.

Quoted 21 lines
tools/xenstore/xenstored_domain.c +8 -0
  	if (!resp) return ENOMEM
 
@@ -468,6 +469,7 @@ int domain_max_global_acc(const void *ctx, struct connection *conn)
  	ent(transactions, ACC_TRANS);
  	ent(outstanding, ACC_OUTST);
  	ent(memory, ACC_MEM);
+	ent(transaction-nodes, ACC_TRANSNODES);
 
  #undef ent
 
@@ -1081,12 +1083,22 @@ int domain_adjust_node_perms(struct node *node)
  	return 0;
  }
 
+static void domain_acc_valid_max(struct domain *d, enum accitem what,
+				 unsigned int val)
+{
+	assert(what < ARRAY_SIZE(d->acc));
+	assert(what < ARRAY_SIZE(acc_global_max));
+
+	if (val > d->acc[what].max)

tools/xenstore/xenstored_domain.c int domain_adjust_node_perms(struct node *node) · line 1088

Quoted 10 lines
tools/xenstore/xenstored_domain.c +5 -1
+		d->acc[what].max = val;
+	if (val > acc_global_max[what] && domid_is_unprivileged(d->domid))
+		acc_global_max[what] = val;
+}
+
  static int domain_acc_add_valid(struct domain *d, enum accitem what, int add)
  {
  	unsigned int val;
 
-	assert(what < ARRAY_SIZE(d->acc));

I think this assert should be kept because...

tools/xenstore/xenstored_domain.c int domain_adjust_node_perms(struct node *node) · line 1102

Quoted 2 lines
tools/xenstore/xenstored_domain.c +0 -1
-
  	if ((add < 0 && -add > d->acc[what].val) ||

... of this check. Otherwise, you would check that 'what' is within the bounds after the use.

Quoted 79 lines
tools/xenstore/xenstored_domain.c +15 -4
  	    (add > 0 && (INT_MAX - d->acc[what].val) < add)) {
  		/*
@@ -1100,10 +1112,7 @@ static int domain_acc_add_valid(struct domain *d, enum accitem what, int add)
  	}
 
  	val = d->acc[what].val + add;
-	if (val > d->acc[what].max)
-		d->acc[what].max = val;
-	if (val > acc_global_max[what] && domid_is_unprivileged(d->domid))
-		acc_global_max[what] = val;
+	domain_acc_valid_max(d, what, val);
 
  	return val;
  }
@@ -1221,6 +1230,20 @@ void domain_reset_global_acc(void)
  	hashtable_iterate(domhash, domain_reset_global_acc_sub, NULL);
  }
 
+bool domain_max_chk(const struct connection *conn, enum accitem what,
+		    unsigned int val, unsigned int quota)
+{
+	if (!conn || !conn->domain)
+		return false;
+
+	if (domain_is_unprivileged(conn) && val > quota)
+		return true;
+
+	domain_acc_valid_max(conn->domain, what, val);
+
+	return false;
+}
+
  int domain_nbentry_inc(struct connection *conn, unsigned int domid)
  {
  	return (domain_acc_add(conn, domid, ACC_NODES, 1, false) < 0)
tools/xenstore/xenstored_domain.h +6 -0
index b55c9bcc2d..3e17b63659 100644
@@ -32,6 +32,10 @@ enum accitem {
  	ACC_OUTST,
  	ACC_MEM,
  	ACC_TRANS,
+	ACC_TRANSNODES,
+	ACC_NPERM,
+	ACC_PATHLEN,
+	ACC_NODESZ,
  	ACC_N,			/* Number of elements per domain. */
  };
 
@@ -128,6 +132,8 @@ void acc_drop(struct connection *conn);
  void acc_commit(struct connection *conn);
  int domain_max_global_acc(const void *ctx, struct connection *conn);
  void domain_reset_global_acc(void);
+bool domain_max_chk(const struct connection *conn, unsigned int what,
+		    unsigned int val, unsigned int quota);
 
  /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +2 -2
index 1c14b8579a..0b256f9b18 100644
@@ -252,8 +252,8 @@ int access_node(struct connection *conn, struct node *node,
 
  	i = find_accessed_node(trans, node->name);
  	if (!i) {
-		if (trans->nodes >= quota_trans_nodes &&
-		    domain_is_unprivileged(conn)) {
+		if (domain_max_chk(conn, ACC_TRANSNODES, trans->nodes + 1,
+				   quota_trans_nodes)) {
  			ret = ENOSPC;
  			goto err;
  		}
tools/xenstore/xenstored_watch.c +0 -0
index e30cd89be3..61b1e3421e 100644

tools/xenstore/xenstored_watch.c static int check_watch_path(struct connection *conn, const void *ctx, · line 182

Quoted 8 lines
tools/xenstore/xenstored_watch.c +1 -1
@@ -176,7 +176,7 @@ static int check_watch_path(struct connection *conn, const void *ctx,
  		*path = canonicalize(conn, ctx, *path);
  		if (!*path)
  			return errno;
-		if (!is_valid_nodename(*path))
+		if (!is_valid_nodename(conn, *path))
  			goto inval;
  	}
 

Cheers,

Signature · 1 line
Julien Grall
#29 lore raw

Re: [PATCH v4 12/13] tools/xenstore: use generic accounting for remaining quotas

Quoted 27 lines · Julien Grall
Quoted 24 lines · Juergen Gross

The maxrequests, node size, number of node permissions, and path length quota are a little bit special, as they are either active in transactions only (maxrequests), or they are just per item instead of count values. Nevertheless being able to know the maximum number of those quota related values per domain would be beneficial, so add them to the generic accounting.

The per domain value will never show current numbers other than zero, but the maximum number seen can be gathered the same way as the number of nodes during a transaction.

To be able to use the const qualifier for a new function switch domain_is_unprivileged() to take a const pointer, too.

---
  tools/xenstore/xenstored_core.c        | 14 ++++----- tools/xenstore/xenstored_core.h        |  2 +- tools/xenstore/xenstored_domain.c      | 39 ++++++++++++++++++++------ tools/xenstore/xenstored_domain.h      |  6 ++++ tools/xenstore/xenstored_transaction.c |  4 +-- tools/xenstore/xenstored_watch.c       |  2 +-

  6 files changed, 48 insertions(+), 19 deletions(-)
Quoted 14 lines

tools/xenstore/xenstored_core.c int write_node_raw(struct connection *conn, TDB_DATA *key, · line 803

Quoted 12 lines
tools/xenstore/xenstored_core.c +2 -2
index 88c569b7d5..65df2866bf 100644
@@ -799,8 +799,8 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, 
 struct node *node,
           + node->perms.num * sizeof(node->perms.p[0])
           + node->datalen + node->childlen;
-    if (!no_quota_check && domain_is_unprivileged(conn) &&
-        data.dsize >= quota_max_entry_size) {
+    if (domain_max_chk(conn, ACC_NODESZ, data.dsize, quota_max_entry_size)
+        && !no_quota_check) {

It feels a bit odd to move the !no_quota_check right after the actual check. But AFAICT, you are doing it because domain_max_chk() will also update the maximum value seen by the current quota.

Correct.

Quoted 2 lines

Is that correct? If so, it would be worth mentioning it in a comment.

Okay.

Quoted 54 lines
Quoted 54 lines
tools/xenstore/xenstored_core.c +4 -3
           errno = ENOSPC;
           return errno;
       }
@@ -1168,7 +1168,7 @@ static bool valid_chars(const char *node)
                  "0123456789-/_@") == strlen(node));
   }
-bool is_valid_nodename(const char *node)
+bool is_valid_nodename(const struct connection *conn, const char *node)
   {
       int local_off = 0;
       unsigned int domid;
@@ -1188,7 +1188,8 @@ bool is_valid_nodename(const char *node)
       if (sscanf(node, "/local/domain/%5u/%n", &domid, &local_off) != 1)
           local_off = 0;
-    if (strlen(node) > local_off + quota_max_path_len)
+    if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off,
+               quota_max_path_len))
           return false;
       return valid_chars(node);
@@ -1250,7 +1251,7 @@ static struct node *get_node_canonicalized(struct 
 connection *conn,
       *canonical_name = canonicalize(conn, ctx, name);
       if (!*canonical_name)
           return NULL;
-    if (!is_valid_nodename(*canonical_name)) {
+    if (!is_valid_nodename(conn, *canonical_name)) {
           errno = EINVAL;
           return NULL;

      }

tools/xenstore/xenstored_core.c +1 -2
@@ -1775,8 +1776,7 @@ static int do_set_perms(const void *ctx, struct 
 connection *conn,
           return EINVAL;
       perms.num--;
-    if (domain_is_unprivileged(conn) &&
-        perms.num > quota_nb_perms_per_node)
+    if (domain_max_chk(conn, ACC_NPERM, perms.num, quota_nb_perms_per_node))
           return ENOSPC;
       permstr = in->buffer + strlen(in->buffer) + 1;
tools/xenstore/xenstored_core.h +1 -1
index 3564d85d7d..9339820156 100644
@@ -258,7 +258,7 @@ void check_store(void);
   void corrupt(struct connection *conn, const char *fmt, ...);
   /* Is this a valid node name? */
-bool is_valid_nodename(const char *node);
+bool is_valid_nodename(const struct connection *conn, const char *node);
   /* Get name of parent node. */
   char *get_parent(const void *ctx, const char *node);
tools/xenstore/xenstored_domain.c +0 -0
index d21f31da92..49e2c5c82a 100644
Quoted 8 lines

tools/xenstore/xenstored_domain.c int domain_get_quota(const void *ctx, struct connection · line 436

Quoted 6 lines
tools/xenstore/xenstored_domain.c +1 -1
@@ -433,7 +433,7 @@ int domain_get_quota(const void *ctx, struct connection 
 *conn,
           return ENOMEM;
   #define ent(t, e) \
-    resp = talloc_asprintf_append(resp, "%-16s: %8u (max: %8u\n", #t, \
+    resp = talloc_asprintf_append(resp, "%-17s: %8u (max: %8u\n", #t, \

This changes feels a bit unrelated. Can you mention why this is necessary in the commit message?

"transaction-nodes" has 17 characters. :-)

Quoted 11 lines

tools/xenstore/xenstored_domain.c int domain_get_quota(const void *ctx, struct connection · line 446

Quoted 9 lines
tools/xenstore/xenstored_domain.c +0 -0
                         d->acc[e].val, d->acc[e].max); \

      if (!resp) return ENOMEM

tools/xenstore/xenstored_domain.c +1 -0
@@ -442,6 +442,7 @@ int domain_get_quota(const void *ctx, struct connection 
 *conn,
       ent(transactions, ACC_TRANS);
       ent(outstanding, ACC_OUTST);
       ent(memory, ACC_MEM);
+    ent(transaction-nodes, ACC_TRANSNODES);

You seem to convert multiple quotas but only print one. Why?

Ah, sorry for omitting the other ones. The following patch is adding them again, so I didn't recognize them missing.

Quoted 42 lines

tools/xenstore/xenstored_domain.c int domain_max_global_acc(const void *ctx, struct · line 464

Quoted 9 lines
tools/xenstore/xenstored_domain.c +1 -1
   #undef ent
@@ -459,7 +460,7 @@ int domain_max_global_acc(const void *ctx, struct 
 connection *conn)
           return ENOMEM;
   #define ent(t, e) \
-    resp = talloc_asprintf_append(resp, "%-16s: %8u\n", #t,   \
+    resp = talloc_asprintf_append(resp, "%-17s: %8u\n", #t,   \
                         acc_global_max[e]);         \

Ditto.

Quoted 17 lines

      if (!resp) return ENOMEM

tools/xenstore/xenstored_domain.c +7 -0
@@ -468,6 +469,7 @@ int domain_max_global_acc(const void *ctx, struct 
 connection *conn)
       ent(transactions, ACC_TRANS);
       ent(outstanding, ACC_OUTST);
       ent(memory, ACC_MEM);
+    ent(transaction-nodes, ACC_TRANSNODES);
   #undef ent
@@ -1081,12 +1083,22 @@ int domain_adjust_node_perms(struct node *node)
       return 0;
   }
+static void domain_acc_valid_max(struct domain *d, enum accitem what,
+                 unsigned int val)
+{
+    assert(what < ARRAY_SIZE(d->acc));
+    assert(what < ARRAY_SIZE(acc_global_max));
+

tools/xenstore/xenstored_domain.c int domain_adjust_node_perms(struct node *node) · line 1086

Quoted 10 lines
tools/xenstore/xenstored_domain.c +6 -1
+    if (val > d->acc[what].max)
+        d->acc[what].max = val;
+    if (val > acc_global_max[what] && domid_is_unprivileged(d->domid))
+        acc_global_max[what] = val;
+}
+
   static int domain_acc_add_valid(struct domain *d, enum accitem what, int add)
   {
       unsigned int val;
-    assert(what < ARRAY_SIZE(d->acc));

I think this assert should be kept because...

Quoted 4 lines
Quoted 2 lines
tools/xenstore/xenstored_domain.c +0 -1
-

      if ((add < 0 && -add > d->acc[what].val) ||

... of this check. Otherwise, you would check that 'what' is within the bounds after the use.

Okay.

Juergen

#30 lore raw

Re: [PATCH v4 12/13] tools/xenstore: use generic accounting for remaining quotas

Quoted 21 lines · Juergen Gross
Quoted 20 lines · Julien Grall
Quoted 12 lines · Juergen Gross
tools/xenstore/xenstored_domain.c +12 -0
+static void domain_acc_valid_max(struct domain *d, enum accitem what,
+                 unsigned int val)
+{
+    assert(what < ARRAY_SIZE(d->acc));
+    assert(what < ARRAY_SIZE(acc_global_max));
+
+    if (val > d->acc[what].max)
+        d->acc[what].max = val;
+    if (val > acc_global_max[what] && domid_is_unprivileged(d->domid))
+        acc_global_max[what] = val;
+}
+
Quoted 4 lines

  static int domain_acc_add_valid(struct domain *d, enum accitem what, int add) {
      unsigned int val;
-    assert(what < ARRAY_SIZE(d->acc));

I think this assert should be kept because...

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

-
      if ((add < 0 && -add > d->acc[what].val) ||

... of this check. Otherwise, you would check that 'what' is within the bounds after the use.

Okay.

Hmm, I'm no longer sure this is a good reason to duplicate the assert().

Following this reasoning I'd need to put it into even more functions. And an assert() triggering a little bit late is no real problem, as it will abort xenstored anyway.

Additionally with the global and the per-domain arrays now covering all possible quotas, it would even be reasonable to drop the assert()s in domain_acc_valid_max() completely.

Juergen

#31 lore raw

PATCH v4 13/13 tools/xenstore: switch quota management to be table based

In reply to Juergen Gross

Instead of having individual quota variables switch to a table based approach like the generic accounting. Include all the related data in the same table and add accessor functions.

This enables to use the command line --quota parameter for setting all possible quota values, keeping the previous parameters for compatibility.

---
V2:
- new patch
One further remark: it would be rather easy to add soft-quota for all the other quotas (similar to the memory one). This could be used as an early warning for the need to raise global quota.

---
 tools/xenstore/xenstored_control.c     |  43 ++------
 tools/xenstore/xenstored_core.c        |  85 ++++++++--------
 tools/xenstore/xenstored_core.h        |  10 --
 tools/xenstore/xenstored_domain.c      | 132 +++++++++++++++++--------
 tools/xenstore/xenstored_domain.h      |  12 ++-
 tools/xenstore/xenstored_transaction.c |   5 +-
 tools/xenstore/xenstored_watch.c       |   2 +-
 7 files changed, 155 insertions(+), 134 deletions(-)
tools/xenstore/xenstored_control.c +8 -35
index a2ba64a15c..75f51a80db 100644
@@ -221,35 +221,6 @@ static int do_control_log(const void *ctx, struct connection *conn,
 	return 0;
 }
 
-struct quota {
-	const char *name;
-	int *quota;
-	const char *descr;
-};
-
-static const struct quota hard_quotas[] = {
-	{ "nodes", &quota_nb_entry_per_domain, "Nodes per domain" },
-	{ "watches", &quota_nb_watch_per_domain, "Watches per domain" },
-	{ "transactions", &quota_max_transaction, "Transactions per domain" },
-	{ "outstanding", &quota_req_outstanding,
-		"Outstanding requests per domain" },
-	{ "transaction-nodes", &quota_trans_nodes,
-		"Max. number of accessed nodes per transaction" },
-	{ "memory", &quota_memory_per_domain_hard,
-		"Total Xenstore memory per domain (error level)" },
-	{ "node-size", &quota_max_entry_size, "Max. size of a node" },
-	{ "path-max", &quota_max_path_len, "Max. length of a node path" },
-	{ "permissions", &quota_nb_perms_per_node,
-		"Max. number of permissions per node" },
-	{ NULL, NULL, NULL }
-};
-
-static const struct quota soft_quotas[] = {
-	{ "memory", &quota_memory_per_domain_soft,
-		"Total Xenstore memory per domain (warning level)" },
-	{ NULL, NULL, NULL }
-};
-
 static int quota_show_current(const void *ctx, struct connection *conn,
 			      const struct quota *quotas)
 {
@@ -260,9 +231,11 @@ static int quota_show_current(const void *ctx, struct connection *conn,
 	if (!resp)
 		return ENOMEM;
 
-	for (i = 0; quotas[i].quota; i++) {
+	for (i = 0; i < ACC_N; i++) {
+		if (!quotas[i].name)
+			continue;
 		resp = talloc_asprintf_append(resp, "%-17s: %8d %s\n",
-					      quotas[i].name, *quotas[i].quota,
+					      quotas[i].name, quotas[i].val,
 					      quotas[i].descr);
 		if (!resp)
 			return ENOMEM;
@@ -274,7 +247,7 @@ static int quota_show_current(const void *ctx, struct connection *conn,
 }
 
 static int quota_set(const void *ctx, struct connection *conn,
-		     char **vec, int num, const struct quota *quotas)
+		     char **vec, int num, struct quota *quotas)
 {
 	unsigned int i;
 	int val;
@@ -286,9 +259,9 @@ static int quota_set(const void *ctx, struct connection *conn,
 	if (val < 1)
 		return EINVAL;
 
-	for (i = 0; quotas[i].quota; i++) {
-		if (!strcmp(vec[0], quotas[i].name)) {
-			*quotas[i].quota = val;
+	for (i = 0; i < ACC_N; i++) {
+		if (quotas[i].name && !strcmp(vec[0], quotas[i].name)) {
+			quotas[i].val = val;
 			send_ack(conn, XS_CONTROL);
 			return 0;
 		}
tools/xenstore/xenstored_core.c +44 -41

178 lines not on this page · raw message

tools/xenstore/xenstored_core.h +0 -10
index 9339820156..92d5b50f3c 100644
@@ -316,16 +316,6 @@ extern TDB_CONTEXT *tdb_ctx;
 extern int dom0_domid;
 extern int dom0_event;
 extern int priv_domid;
-extern int quota_nb_watch_per_domain;
-extern int quota_max_transaction;
-extern int quota_max_entry_size;
-extern int quota_nb_perms_per_node;
-extern int quota_max_path_len;
-extern int quota_nb_entry_per_domain;
-extern int quota_req_outstanding;
-extern int quota_trans_nodes;
-extern int quota_memory_per_domain_soft;
-extern int quota_memory_per_domain_hard;
 extern bool keep_orphans;
 
 extern unsigned int timeout_watch_event_msec;
tools/xenstore/xenstored_domain.c +89 -43

216 lines not on this page · raw message

tools/xenstore/xenstored_domain.h +11 -1
index 3e17b63659..b9e38abff5 100644
@@ -39,6 +39,16 @@ enum accitem {
 	ACC_N,			/* Number of elements per domain. */
 };
 
+struct quota {
+	const char *name;
+	const char *descr;
+	unsigned int val;
+	unsigned int max;
+};
+
+extern struct quota hard_quotas[ACC_N];
+extern struct quota soft_quotas[ACC_N];
+
 void handle_event(void);
 
 void check_domains(void);
@@ -133,7 +143,7 @@ void acc_commit(struct connection *conn);
 int domain_max_global_acc(const void *ctx, struct connection *conn);
 void domain_reset_global_acc(void);
 bool domain_max_chk(const struct connection *conn, unsigned int what,
-		    unsigned int val, unsigned int quota);
+		    unsigned int val);
 
 /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +2 -3
index 0b256f9b18..e8968d7a1d 100644
@@ -252,8 +252,7 @@ int access_node(struct connection *conn, struct node *node,
 
 	i = find_accessed_node(trans, node->name);
 	if (!i) {
-		if (domain_max_chk(conn, ACC_TRANSNODES, trans->nodes + 1,
-				   quota_trans_nodes)) {
+		if (domain_max_chk(conn, ACC_TRANSNODES, trans->nodes + 1)) {
 			ret = ENOSPC;
 			goto err;
 		}
@@ -479,7 +478,7 @@ int do_transaction_start(const void *ctx, struct connection *conn,
 	if (conn->transaction)
 		return EBUSY;
 
-	if (domain_transaction_get(conn) > quota_max_transaction)
+	if (domain_transaction_get(conn) > hard_quotas[ACC_TRANS].val)
 		return ENOSPC;
 
 	/* Attach transaction to ctx for autofree until it's complete */
tools/xenstore/xenstored_watch.c +1 -1
index 61b1e3421e..e8eb35de02 100644
@@ -239,7 +239,7 @@ int do_watch(const void *ctx, struct connection *conn, struct buffered_data *in)
 			return EEXIST;
 	}
 
-	if (domain_watch(conn) > quota_nb_watch_per_domain)
+	if (domain_watch(conn) > hard_quotas[ACC_WATCH].val)
 		return E2BIG;
 
 	watch = add_watch(conn, vec[0], vec[1], relative, false);
Signature · 1 line
2.35.3
#32 lore raw

Re: [PATCH v4 13/13] tools/xenstore: switch quota management to be table based

Hi Juergen,

Quoted 8 lines · Juergen Gross

Instead of having individual quota variables switch to a table based approach like the generic accounting. Include all the related data in the same table and add accessor functions.

This enables to use the command line --quota parameter for setting all possible quota values, keeping the previous parameters for compatibility.

Quoted 7 lines

---
V2:
- new patch
One further remark: it would be rather easy to add soft-quota for all the other quotas (similar to the memory one). This could be used as an early warning for the need to raise global quota.

I don't have a strong opinion on this topic.

Quoted 165 lines
---
  tools/xenstore/xenstored_control.c     |  43 ++------
  tools/xenstore/xenstored_core.c        |  85 ++++++++--------
  tools/xenstore/xenstored_core.h        |  10 --
  tools/xenstore/xenstored_domain.c      | 132 +++++++++++++++++--------
  tools/xenstore/xenstored_domain.h      |  12 ++-
  tools/xenstore/xenstored_transaction.c |   5 +-
  tools/xenstore/xenstored_watch.c       |   2 +-
  7 files changed, 155 insertions(+), 134 deletions(-)
tools/xenstore/xenstored_control.c +8 -35
index a2ba64a15c..75f51a80db 100644
@@ -221,35 +221,6 @@ static int do_control_log(const void *ctx, struct connection *conn,
  	return 0;
  }
 
-struct quota {
-	const char *name;
-	int *quota;
-	const char *descr;
-};
-
-static const struct quota hard_quotas[] = {
-	{ "nodes", &quota_nb_entry_per_domain, "Nodes per domain" },
-	{ "watches", &quota_nb_watch_per_domain, "Watches per domain" },
-	{ "transactions", &quota_max_transaction, "Transactions per domain" },
-	{ "outstanding", &quota_req_outstanding,
-		"Outstanding requests per domain" },
-	{ "transaction-nodes", &quota_trans_nodes,
-		"Max. number of accessed nodes per transaction" },
-	{ "memory", &quota_memory_per_domain_hard,
-		"Total Xenstore memory per domain (error level)" },
-	{ "node-size", &quota_max_entry_size, "Max. size of a node" },
-	{ "path-max", &quota_max_path_len, "Max. length of a node path" },
-	{ "permissions", &quota_nb_perms_per_node,
-		"Max. number of permissions per node" },
-	{ NULL, NULL, NULL }
-};
-
-static const struct quota soft_quotas[] = {
-	{ "memory", &quota_memory_per_domain_soft,
-		"Total Xenstore memory per domain (warning level)" },
-	{ NULL, NULL, NULL }
-};
-
  static int quota_show_current(const void *ctx, struct connection *conn,
  			      const struct quota *quotas)
  {
@@ -260,9 +231,11 @@ static int quota_show_current(const void *ctx, struct connection *conn,
  	if (!resp)
  		return ENOMEM;
 
-	for (i = 0; quotas[i].quota; i++) {
+	for (i = 0; i < ACC_N; i++) {
+		if (!quotas[i].name)
+			continue;
  		resp = talloc_asprintf_append(resp, "%-17s: %8d %s\n",
-					      quotas[i].name, *quotas[i].quota,
+					      quotas[i].name, quotas[i].val,
  					      quotas[i].descr);
  		if (!resp)
  			return ENOMEM;
@@ -274,7 +247,7 @@ static int quota_show_current(const void *ctx, struct connection *conn,
  }
 
  static int quota_set(const void *ctx, struct connection *conn,
-		     char **vec, int num, const struct quota *quotas)
+		     char **vec, int num, struct quota *quotas)
  {
  	unsigned int i;
  	int val;
@@ -286,9 +259,9 @@ static int quota_set(const void *ctx, struct connection *conn,
  	if (val < 1)
  		return EINVAL;
 
-	for (i = 0; quotas[i].quota; i++) {
-		if (!strcmp(vec[0], quotas[i].name)) {
-			*quotas[i].quota = val;
+	for (i = 0; i < ACC_N; i++) {
+		if (quotas[i].name && !strcmp(vec[0], quotas[i].name)) {
+			quotas[i].val = val;
  			send_ack(conn, XS_CONTROL);
  			return 0;
  		}
tools/xenstore/xenstored_core.c +13 -16
index 65df2866bf..6e2fc06840 100644
@@ -89,17 +89,6 @@ unsigned int trace_flags = TRACE_OBJ | TRACE_IO;
 
  static const char *sockmsg_string(enum xsd_sockmsg_type type);
 
-int quota_nb_entry_per_domain = 1000;
-int quota_nb_watch_per_domain = 128;
-int quota_max_entry_size = 2048; /* 2K */
-int quota_max_transaction = 10;
-int quota_nb_perms_per_node = 5;
-int quota_trans_nodes = 1024;
-int quota_max_path_len = XENSTORE_REL_PATH_MAX;
-int quota_req_outstanding = 20;
-int quota_memory_per_domain_soft = 2 * 1024 * 1024; /* 2 MB */
-int quota_memory_per_domain_hard = 2 * 1024 * 1024 + 512 * 1024; /* 2.5 MB */
-
  unsigned int timeout_watch_event_msec = 20000;
 
  void trace(const char *fmt, ...)
@@ -799,7 +788,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
  		+ node->perms.num * sizeof(node->perms.p[0])
  		+ node->datalen + node->childlen;
 
-	if (domain_max_chk(conn, ACC_NODESZ, data.dsize, quota_max_entry_size)
+	if (domain_max_chk(conn, ACC_NODESZ, data.dsize)
  	    && !no_quota_check) {
  		errno = ENOSPC;
  		return errno;
@@ -1188,8 +1177,7 @@ bool is_valid_nodename(const struct connection *conn, const char *node)
  	if (sscanf(node, "/local/domain/%5u/%n", &domid, &local_off) != 1)
  		local_off = 0;
 
-	if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off,
-			   quota_max_path_len))
+	if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off))
  		return false;
 
  	return valid_chars(node);
@@ -1501,7 +1489,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_nbentry(conn) >= quota_nb_entry_per_domain) {
+		    domain_nbentry(conn) >= hard_quotas[ACC_NODES].val) {
  			ret = ENOSPC;
  			goto err;
  		}
@@ -1776,7 +1764,7 @@ static int do_set_perms(const void *ctx, struct connection *conn,
  		return EINVAL;
 
  	perms.num--;
-	if (domain_max_chk(conn, ACC_NPERM, perms.num, quota_nb_perms_per_node))
+	if (domain_max_chk(conn, ACC_NPERM, perms.num))
  		return ENOSPC;
 
  	permstr = in->buffer + strlen(in->buffer) + 1;
@@ -2644,7 +2632,16 @@ static void usage(void)
  "                          memory: total used memory per domain for nodes,\n"
  "                                  transactions, watches and requests, above\n"
  "                                  which Xenstore will stop talking to domain\n"
+"                          nodes: number nodes owned by a domain\n"
+"                          node-permissions: number of access permissions per\n"
+"                                            node\n"
+"                          node-size: total size of a node (permissions +\n"
+"                                     children names + content)\n"
  "                          outstanding: number of outstanding requests\n"
+"                          path-length: length of a node path\n"
+"                          transactions: number of concurrent transactions\n"
+"                                        per domain\n"
+"                          watches: number of watches per domain"
  "  -q, --quota-soft <what>=<nb> set a soft quota <what> to the value <nb>,\n"
  "                          causing a warning to be issued via syslog() if the\n"
  "                          limit is violated, allowed quotas are:\n"
@@ -2695,12 +2692,12 @@ int dom0_domid = 0;
  int dom0_event = 0;
  int priv_domid = 0;

tools/xenstore/xenstored_core.c int dom0_domid = 0; · line 2700

Quoted 10 lines
tools/xenstore/xenstored_core.c +3 -3
 
-static int get_optval_int(const char *arg)
+static unsigned int get_optval_int(const char *arg)
  {
  	char *end;
-	long val;
+	unsigned long val;
 
-	val = strtol(arg, &end, 10);
+	val = strtoul(arg, &end, 10);

The changes in get_optval_int() feels like more a clean-up because the returned value cannot be negative (see check below). I would prefer if they are done in a separate patch.

tools/xenstore/xenstored_core.c int dom0_domid = 0; · line 2701

Quoted 1 line
tools/xenstore/xenstored_core.c +0 -0
  	if (!*arg || *end || val < 0 || val > INT_MAX)

Now that 'val' is unsigned long, then there is no point for checking val is < 0.

Lastly, I would rename the helper to make clear it returns an unsigned value. How about get_optval_uint()?

tools/xenstore/xenstored_core.c static int get_optval_int(const char *arg) · line 2712

Quoted 11 lines
tools/xenstore/xenstored_core.c +4 -1
  		barf("invalid parameter value \"%s\"\n", arg);
 
@@ -2709,15 +2706,19 @@ static int get_optval_int(const char *arg)
 
  static bool what_matches(const char *arg, const char *what)
  {
-	unsigned int what_len = strlen(what);
+	unsigned int what_len;
+
+	if (!what)
+		false;

Shouldn't this be "return false"?

Quoted 49 lines
tools/xenstore/xenstored_core.c +14 -13
+	what_len = strlen(what);
  	return !strncmp(arg, what, what_len) && arg[what_len] == '=';
  }
 
  static void set_timeout(const char *arg)
  {
  	const char *eq = strchr(arg, '=');
-	int val;
+	unsigned int val;
 
  	if (!eq)
  		barf("quotas must be specified via <what>=<seconds>\n");
@@ -2731,22 +2732,22 @@ static void set_timeout(const char *arg)
  static void set_quota(const char *arg, bool soft)
  {
  	const char *eq = strchr(arg, '=');
-	int val;
+	struct quota *q = soft ? soft_quotas : hard_quotas;
+	unsigned int val;
+	unsigned int i;
 
  	if (!eq)
  		barf("quotas must be specified via <what>=<nb>\n");
  	val = get_optval_int(eq + 1);
-	if (what_matches(arg, "outstanding") && !soft)
-		quota_req_outstanding = val;
-	else if (what_matches(arg, "transaction-nodes") && !soft)
-		quota_trans_nodes = val;
-	else if (what_matches(arg, "memory")) {
-		if (soft)
-			quota_memory_per_domain_soft = val;
-		else
-			quota_memory_per_domain_hard = val;
-	} else
-		barf("unknown quota \"%s\"\n", arg);
+
+	for (i = 0; i < ACC_N; i++) {
+		if (what_matches(arg, q[i].name)) {
+			q[i].val = val;
+			return;
+		}
+	}
+
+	barf("unknown quota \"%s\"\n", arg);
  }
 
  /* Sorted by bit values of TRACE_* flags. Flag is (1u << index). */

tools/xenstore/xenstored_core.c int main(int argc, char *argv[]) · line 2812

Quoted 5 lines
tools/xenstore/xenstored_core.c +1 -1
@@ -2808,7 +2809,7 @@ int main(int argc, char *argv[])
  			no_domain_init = true;
  			break;
  		case 'E':
-			quota_nb_entry_per_domain = strtol(optarg, NULL, 10);
+			hard_quotas[ACC_NODES].val = strtoul(optarg, NULL, 10);

I think we should use get_optval_int() here and all the other below.

Quoted 29 lines
tools/xenstore/xenstored_core.c +4 -5
  			break;
  		case 'F':
  			pidfile = optarg;
@@ -2826,10 +2827,10 @@ int main(int argc, char *argv[])
  			recovery = false;
  			break;
  		case 'S':
-			quota_max_entry_size = strtol(optarg, NULL, 10);
+			hard_quotas[ACC_NODESZ].val = strtoul(optarg, NULL, 10);
  			break;
  		case 't':
-			quota_max_transaction = strtol(optarg, NULL, 10);
+			hard_quotas[ACC_TRANS].val = strtoul(optarg, NULL, 10);
  			break;
  		case 'T':
  			tracefile = optarg;
@@ -2849,15 +2850,17 @@ int main(int argc, char *argv[])
  			verbose = true;
  			break;
  		case 'W':
-			quota_nb_watch_per_domain = strtol(optarg, NULL, 10);
+			hard_quotas[ACC_WATCH].val = strtoul(optarg, NULL, 10);
  			break;
  		case 'A':
-			quota_nb_perms_per_node = strtol(optarg, NULL, 10);
+			hard_quotas[ACC_NPERM].val = strtoul(optarg, NULL, 10);
  			break;
  		case 'M':
-			quota_max_path_len = strtol(optarg, NULL, 10);

tools/xenstore/xenstored_core.c int main(int argc, char *argv[]) · line 2866

Quoted 10 lines
tools/xenstore/xenstored_core.c +5 -2
-			quota_max_path_len = min(XENSTORE_REL_PATH_MAX,
-						 quota_max_path_len);
+			hard_quotas[ACC_PATHLEN].val =
+				strtoul(optarg, NULL, 10);
+			hard_quotas[ACC_PATHLEN].val =
+				 min((unsigned int)XENSTORE_REL_PATH_MAX,
+				     hard_quotas[ACC_PATHLEN].val);
  			break;
  		case 'Q':
  			set_quota(optarg, false);

Cheers,

Signature · 1 line
Julien Grall
#33 lore raw

Re: [PATCH v4 13/13] tools/xenstore: switch quota management to be table based

Quoted 19 lines · Julien Grall

Hi Juergen,

Quoted 8 lines · Juergen Gross

Instead of having individual quota variables switch to a table based approach like the generic accounting. Include all the related data in the same table and add accessor functions.

This enables to use the command line --quota parameter for setting all possible quota values, keeping the previous parameters for compatibility.

Quoted 7 lines

---
V2:
- new patch
One further remark: it would be rather easy to add soft-quota for all the other quotas (similar to the memory one). This could be used as an early warning for the need to raise global quota.

Quoted 1 line

I don't have a strong opinion on this topic.

Me neither.

Quoted 163 lines
Quoted 163 lines

---
  tools/xenstore/xenstored_control.c     |  43 ++------ tools/xenstore/xenstored_core.c        |  85 ++++++++-------- tools/xenstore/xenstored_core.h        |  10 -- tools/xenstore/xenstored_domain.c      | 132 +++++++++++++++++-------- tools/xenstore/xenstored_domain.h      |  12 ++- tools/xenstore/xenstored_transaction.c |   5 +- tools/xenstore/xenstored_watch.c       |   2 +-

  7 files changed, 155 insertions(+), 134 deletions(-)
tools/xenstore/xenstored_control.c +8 -35
index a2ba64a15c..75f51a80db 100644
@@ -221,35 +221,6 @@ static int do_control_log(const void *ctx, struct 
 connection *conn,
       return 0;
   }
-struct quota {
-    const char *name;
-    int *quota;
-    const char *descr;
-};
-
-static const struct quota hard_quotas[] = {
-    { "nodes", &quota_nb_entry_per_domain, "Nodes per domain" },
-    { "watches", &quota_nb_watch_per_domain, "Watches per domain" },
-    { "transactions", &quota_max_transaction, "Transactions per domain" },
-    { "outstanding", &quota_req_outstanding,
-        "Outstanding requests per domain" },
-    { "transaction-nodes", &quota_trans_nodes,
-        "Max. number of accessed nodes per transaction" },
-    { "memory", &quota_memory_per_domain_hard,
-        "Total Xenstore memory per domain (error level)" },
-    { "node-size", &quota_max_entry_size, "Max. size of a node" },
-    { "path-max", &quota_max_path_len, "Max. length of a node path" },
-    { "permissions", &quota_nb_perms_per_node,
-        "Max. number of permissions per node" },
-    { NULL, NULL, NULL }
-};
-
-static const struct quota soft_quotas[] = {
-    { "memory", &quota_memory_per_domain_soft,
-        "Total Xenstore memory per domain (warning level)" },
-    { NULL, NULL, NULL }
-};
-
   static int quota_show_current(const void *ctx, struct connection *conn,
                     const struct quota *quotas)
   {
@@ -260,9 +231,11 @@ static int quota_show_current(const void *ctx, struct 
 connection *conn,
       if (!resp)
           return ENOMEM;
-    for (i = 0; quotas[i].quota; i++) {
+    for (i = 0; i < ACC_N; i++) {
+        if (!quotas[i].name)
+            continue;
           resp = talloc_asprintf_append(resp, "%-17s: %8d %s\n",
-                          quotas[i].name, *quotas[i].quota,
+                          quotas[i].name, quotas[i].val,
                             quotas[i].descr);
           if (!resp)
               return ENOMEM;
@@ -274,7 +247,7 @@ static int quota_show_current(const void *ctx, struct 
 connection *conn,
   }
   static int quota_set(const void *ctx, struct connection *conn,
-             char **vec, int num, const struct quota *quotas)
+             char **vec, int num, struct quota *quotas)
   {
       unsigned int i;
       int val;
@@ -286,9 +259,9 @@ static int quota_set(const void *ctx, struct connection 
 *conn,
       if (val < 1)
           return EINVAL;
-    for (i = 0; quotas[i].quota; i++) {
-        if (!strcmp(vec[0], quotas[i].name)) {
-            *quotas[i].quota = val;
+    for (i = 0; i < ACC_N; i++) {
+        if (quotas[i].name && !strcmp(vec[0], quotas[i].name)) {
+            quotas[i].val = val;
               send_ack(conn, XS_CONTROL);
               return 0;
           }
tools/xenstore/xenstored_core.c +2 -14
index 65df2866bf..6e2fc06840 100644
@@ -89,17 +89,6 @@ unsigned int trace_flags = TRACE_OBJ | TRACE_IO;
   static const char *sockmsg_string(enum xsd_sockmsg_type type);
-int quota_nb_entry_per_domain = 1000;
-int quota_nb_watch_per_domain = 128;
-int quota_max_entry_size = 2048; /* 2K */
-int quota_max_transaction = 10;
-int quota_nb_perms_per_node = 5;
-int quota_trans_nodes = 1024;
-int quota_max_path_len = XENSTORE_REL_PATH_MAX;
-int quota_req_outstanding = 20;
-int quota_memory_per_domain_soft = 2 * 1024 * 1024; /* 2 MB */
-int quota_memory_per_domain_hard = 2 * 1024 * 1024 + 512 * 1024; /* 2.5 MB */
-
   unsigned int timeout_watch_event_msec = 20000;
   void trace(const char *fmt, ...)
@@ -799,7 +788,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, 
 struct node *node,
           + node->perms.num * sizeof(node->perms.p[0])
           + node->datalen + node->childlen;
-    if (domain_max_chk(conn, ACC_NODESZ, data.dsize, quota_max_entry_size)
+    if (domain_max_chk(conn, ACC_NODESZ, data.dsize)
           && !no_quota_check) {
           errno = ENOSPC;
           return errno;
@@ -1188,8 +1177,7 @@ bool is_valid_nodename(const struct connection *conn, 
 const char *node)
       if (sscanf(node, "/local/domain/%5u/%n", &domid, &local_off) != 1)
           local_off = 0;
-    if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off,
-               quota_max_path_len))
+    if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off))
           return false;
       return valid_chars(node);
@@ -1501,7 +1489,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_nbentry(conn) >= quota_nb_entry_per_domain) { +            domain_nbentry(conn) >= hard_quotas[ACC_NODES].val) { ret = ENOSPC;
              goto err;
          }

tools/xenstore/xenstored_core.c +1 -1
@@ -1776,7 +1764,7 @@ static int do_set_perms(const void *ctx, struct 
 connection *conn,
           return EINVAL;
       perms.num--;
-    if (domain_max_chk(conn, ACC_NPERM, perms.num, quota_nb_perms_per_node))
+    if (domain_max_chk(conn, ACC_NPERM, perms.num))
           return ENOSPC;
       permstr = in->buffer + strlen(in->buffer) + 1;
@@ -2644,7 +2632,16 @@ static void usage(void)

  "                          memory: total used memory per domain for nodes,\n" "                                  transactions, watches and requests, above\n" "                                  which Xenstore will stop talking to domain\n" +"                          nodes: number nodes owned by a domain\n" +"                          node-permissions: number of access permissions per\n" +"                                            node\n"
+"                          node-size: total size of a node (permissions +\n" +"                                     children names + content)\n"
  "                          outstanding: number of outstanding requests\n" +"                          path-length: length of a node path\n"
+"                          transactions: number of concurrent transactions\n" +"                                        per domain\n"
+"                          watches: number of watches per domain"
  "  -q, --quota-soft <what>=<nb> set a soft quota <what> to the value <nb>,\n" "                          causing a warning to be issued via syslog() if the\n" "                          limit is violated, allowed quotas are:\n"

tools/xenstore/xenstored_core.c +0 -1
@@ -2695,12 +2692,12 @@ int dom0_domid = 0;
   int dom0_event = 0;
   int priv_domid = 0;
-static int get_optval_int(const char *arg)
Quoted 8 lines

tools/xenstore/xenstored_core.c int dom0_domid = 0; · line 2700

Quoted 7 lines
tools/xenstore/xenstored_core.c +3 -2
+static unsigned int get_optval_int(const char *arg)
   {
       char *end;
-    long val;
+    unsigned long val;
-    val = strtol(arg, &end, 10);
+    val = strtoul(arg, &end, 10);

The changes in get_optval_int() feels like more a clean-up because the returned value cannot be negative (see check below). I would prefer if they are done in a separate patch.

Okay.

Quoted 4 lines

tools/xenstore/xenstored_core.c int dom0_domid = 0; · line 2701

Quoted 2 lines
tools/xenstore/xenstored_core.c +0 -0
       if (!*arg || *end || val < 0 || val > INT_MAX)

Now that 'val' is unsigned long, then there is no point for checking val is < 0.

Oh, indeed.

Quoted 2 lines

Lastly, I would rename the helper to make clear it returns an unsigned value. How about get_optval_uint()?

Okay.

Quoted 12 lines

tools/xenstore/xenstored_core.c static int get_optval_int(const char *arg) · line 2711

Quoted 10 lines
tools/xenstore/xenstored_core.c +4 -1
           barf("invalid parameter value \"%s\"\n", arg);
@@ -2709,15 +2706,19 @@ static int get_optval_int(const char *arg)
   static bool what_matches(const char *arg, const char *what)
   {
-    unsigned int what_len = strlen(what);
+    unsigned int what_len;
+
+    if (!what)
+        false;

Shouldn't this be "return false"?

Yes.

Juergen

#34 lore raw
In reply to Juergen Gross
Quoted 9 lines · Juergen Gross

This series reworks the Xenstore internal accounting to use a uniform generic framework. It is adding some additional useful diagnostic information, like accounting trace and max. per-domain and global quota values seen.

Changes in V2:
- added patch 1 (leftover from previous series)
- rebase

Quoted 5 lines

Changes in V3:
- addressed comments

Changes in V4:
- fixed patch 3

Another thought for this series and followup one:

Do we want to keep current coding style in tools/xenstore (basically Linux kernel style), or do we want to switch to Xen style instead?

If a switch to Xen style is preferred (I do prefer that switch), I'd like to suggest that I do a rework of this series and the followup one to use the Xen style for new or moved functions.

A more radical approach would be to do a large style switch series after the two series, but I'm hesitant as this would affect backports rather badly.

Juergen

#35 lore raw

Hi Juergen,

Quoted 13 lines · Juergen Gross
Quoted 12 lines · Juergen Gross

This series reworks the Xenstore internal accounting to use a uniform generic framework. It is adding some additional useful diagnostic information, like accounting trace and max. per-domain and global quota values seen.

Changes in V2:
- added patch 1 (leftover from previous series)
- rebase

Changes in V3:
- addressed comments

Quoted 7 lines
Quoted 2 lines

Changes in V4:
- fixed patch 3

Another thought for this series and followup one:

Do we want to keep current coding style in tools/xenstore (basically Linux kernel style), or do we want to switch to Xen style instead?

I am a bit split on this one. I don't particularly like the Linux coding style, but it has the advantage to be well-documented (if we compare to the Xen one).

May I ask what would be the reason to switch?

Quoted 4 lines

If a switch to Xen style is preferred (I do prefer that switch), I'd like to suggest that I do a rework of this series and the followup one to use the Xen style for new or moved functions.

I think this is a bad idea because it would make difficult for a developer/reviewer to know what is the coding style of a given function.

At least in my workflow, it would also means that I need two open the file twice with different settings (e.g. soft vs hard tab).

Quoted 4 lines

A more radical approach would be to do a large style switch series after the two series, but I'm hesitant as this would affect backports rather badly.

In general, I would agree with that. But, after your work, I am under the impression that Xenstored will become quite different. So I am not convince we will be able to backports a lot of patch without significant rework.

Therefore, converting all the files in one pass may not be too bad (assuming we agree on switching to the new coding style).

Cheers,

Signature · 1 line
Julien Grall
#36 lore raw
Quoted 19 lines · Julien Grall

Hi Juergen,

Quoted 16 lines · Juergen Gross
Quoted 14 lines · Juergen Gross

This series reworks the Xenstore internal accounting to use a uniform generic framework. It is adding some additional useful diagnostic information, like accounting trace and max. per-domain and global quota values seen.

Changes in V2:
- added patch 1 (leftover from previous series)
- rebase

Changes in V3:
- addressed comments

Changes in V4:
- fixed patch 3

Quoted 6 lines
Quoted 4 lines

Another thought for this series and followup one:

Do we want to keep current coding style in tools/xenstore (basically Linux kernel style), or do we want to switch to Xen style instead?

I am a bit split on this one. I don't particularly like the Linux coding style, but it has the advantage to be well-documented (if we compare to the Xen one).

I have raised the idea to switch to the Linux style for that reason, but it was rejected rather firmly.

So we won't get rid of the Xen style.

Quoted 1 line

May I ask what would be the reason to switch?

According to CODING_STYLE it is the style to be used. We could add a local style hint in tools/xenstored, but I'd rather not add another local style.

In the end it is about consistency.

Quoted 7 lines
Quoted 3 lines

If a switch to Xen style is preferred (I do prefer that switch), I'd like to suggest that I do a rework of this series and the followup one to use the Xen style for new or moved functions.

I think this is a bad idea because it would make difficult for a developer/reviewer to know what is the coding style of a given function.

At least in my workflow, it would also means that I need two open the file twice with different settings (e.g. soft vs hard tab).

Okay. This is a rather good reason not to use different styles in one source.

Quoted 7 lines
Quoted 3 lines

A more radical approach would be to do a large style switch series after the two series, but I'm hesitant as this would affect backports rather badly.

In general, I would agree with that. But, after your work, I am under the impression that Xenstored will become quite different. So I am not convince we will be able to backports a lot of patch without significant rework.

Therefore, converting all the files in one pass may not be too bad (assuming we agree on switching to the new coding style).

Fine with me. In any case this should be done in the same Xen release as the major rework. Otherwise the backport problem will be a real one.

Juergen