public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH-cgroup v5 0/2] cgroup/rstat: Further reduce cpu_lock hold time in cgroup_rstat_flush_locked
@ 2023-11-30 20:43 Waiman Long
  2023-11-30 20:43 ` [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list() Waiman Long
  2023-11-30 20:43 ` [PATCH-cgroup v5 2/2] cgroup: Avoid false cacheline sharing of read mostly rstat_cpu Waiman Long
  0 siblings, 2 replies; 6+ messages in thread
From: Waiman Long @ 2023-11-30 20:43 UTC (permalink / raw)
  To: Tejun Heo, Zefan Li, Johannes Weiner
  Cc: cgroups, linux-kernel, Joe Mario, Sebastian Jug, Yosry Ahmed,
	Waiman Long

 v5:
  - Drop patch "cgroup/rstat: Reduce cpu_lock hold time in
    cgroup_rstat_flush_locked()" as it has been merged into the for-6.8
    branch.
  - Rework the recursive cgroup_rstat_push_children() into an iterative
    function to prevent possibility of stack overflow.

 v4:
  - Update patch 2 to fix a minor bug and update some of the comments.

 v3:
  - Minor comment twisting as suggested by Yosry.
  - Add patches 2 and 3 to further reduce lock hold time

The purpose of this patch series is to further reduce the cpu_lock
hold time of cgroup_rstat_flush_locked() so as to reduce the latency
impact when cgroup_rstat_updated() is called as they may contend with
each other on the cpu_lock.

Waiman Long (2):
  cgroup/rstat: Optimize cgroup_rstat_updated_list()
  cgroup: Avoid false cacheline sharing of read mostly rstat_cpu

 include/linux/cgroup-defs.h |   7 ++
 kernel/cgroup/rstat.c       | 153 +++++++++++++++++++++---------------
 2 files changed, 98 insertions(+), 62 deletions(-)

-- 
2.39.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list()
  2023-11-30 20:43 [PATCH-cgroup v5 0/2] cgroup/rstat: Further reduce cpu_lock hold time in cgroup_rstat_flush_locked Waiman Long
@ 2023-11-30 20:43 ` Waiman Long
  2023-12-01 17:37   ` Tejun Heo
  2023-11-30 20:43 ` [PATCH-cgroup v5 2/2] cgroup: Avoid false cacheline sharing of read mostly rstat_cpu Waiman Long
  1 sibling, 1 reply; 6+ messages in thread
From: Waiman Long @ 2023-11-30 20:43 UTC (permalink / raw)
  To: Tejun Heo, Zefan Li, Johannes Weiner
  Cc: cgroups, linux-kernel, Joe Mario, Sebastian Jug, Yosry Ahmed,
	Waiman Long

The current design of cgroup_rstat_cpu_pop_updated() is to traverse
the updated tree in a way to pop out the leaf nodes first before
their parents. This can cause traversal of multiple nodes before a
leaf node can be found and popped out. IOW, a given node in the tree
can be visited multiple times before the whole operation is done. So
it is not very efficient and the code can be hard to read.

With the introduction of cgroup_rstat_updated_list() to build a list
of cgroups to be flushed first before any flushing operation is being
done, we can optimize the way the updated tree nodes are being popped
by pushing the parents first to the tail end of the list before their
children. In this way, most updated tree nodes will be visited only
once with the exception of the subtree root as we still need to go
back to its parent and popped it out of its updated_children list.
This also makes the code easier to read.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 kernel/cgroup/rstat.c | 153 +++++++++++++++++++++++++-----------------
 1 file changed, 91 insertions(+), 62 deletions(-)

diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c
index 1f300bf4dc40..e77f134c6f03 100644
--- a/kernel/cgroup/rstat.c
+++ b/kernel/cgroup/rstat.c
@@ -74,64 +74,109 @@ __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu)
 }
 
 /**
- * cgroup_rstat_cpu_pop_updated - iterate and dismantle rstat_cpu updated tree
- * @pos: current position
- * @root: root of the tree to traversal
+ * cgroup_rstat_push_children - push children cgroups into the given list
+ * @head: current head of the list (= subtree root)
+ * @child: first child of the root
  * @cpu: target cpu
+ * Return: A new singly linked list of cgroups to be flush
  *
- * Walks the updated rstat_cpu tree on @cpu from @root.  %NULL @pos starts
- * the traversal and %NULL return indicates the end.  During traversal,
- * each returned cgroup is unlinked from the tree.  Must be called with the
- * matching cgroup_rstat_cpu_lock held.
+ * Iteratively traverse down the cgroup_rstat_cpu updated tree level by
+ * level and push all the parents first before their next level children
+ * into a singly linked list built from the tail backward like "pushing"
+ * cgroups into a stack. The parent is by the caller.
+ */
+static struct cgroup *cgroup_rstat_push_children(struct cgroup *head,
+						 struct cgroup *child, int cpu)
+{
+	struct cgroup *chead = child;	/* Head of child cgroup level */
+	struct cgroup *ghead = NULL;	/* Head of grandchild cgroup level */
+	struct cgroup *parent, *grandchild;
+	struct cgroup_rstat_cpu *crstatc;
+
+	child->rstat_flush_next = NULL;
+
+next_level:
+	while (chead) {
+		child = chead;
+		chead = child->rstat_flush_next;
+		parent = cgroup_parent(child);
+
+		/* updated_next is parent cgroup terminated */
+		while (child != parent) {
+			child->rstat_flush_next = head;
+			head = child;
+			crstatc = cgroup_rstat_cpu(child, cpu);
+			grandchild = crstatc->updated_children;
+			if (grandchild != child) {
+				/* Push the grand child to the next level */
+				crstatc->updated_children = child;
+				grandchild->rstat_flush_next = ghead;
+				ghead = grandchild;
+			}
+			child = crstatc->updated_next;
+			crstatc->updated_next = NULL;
+		}
+	}
+
+	if (ghead) {
+		chead = ghead;
+		ghead = NULL;
+		goto next_level;
+	}
+	return head;
+}
+
+/**
+ * cgroup_rstat_updated_list - return a list of updated cgroups to be flushed
+ * @root: root of the cgroup subtree to traverse
+ * @cpu: target cpu
+ * Return: A singly linked list of cgroups to be flushed
+ *
+ * Walks the updated rstat_cpu tree on @cpu from @root.  During traversal,
+ * each returned cgroup is unlinked from the updated tree.
  *
  * The only ordering guarantee is that, for a parent and a child pair
- * covered by a given traversal, if a child is visited, its parent is
- * guaranteed to be visited afterwards.
+ * covered by a given traversal, the child is before its parent in
+ * the list.
+ *
+ * Note that updated_children is self terminated and points to a list of
+ * child cgroups if not empty. Whereas updated_next is like a sibling link
+ * within the children list and terminated by the parent cgroup. An exception
+ * here is the cgroup root whose updated_next can be self terminated.
  */
-static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos,
-						   struct cgroup *root, int cpu)
+static struct cgroup *cgroup_rstat_updated_list(struct cgroup *root, int cpu)
 {
-	struct cgroup_rstat_cpu *rstatc;
-	struct cgroup *parent;
-
-	if (pos == root)
-		return NULL;
+	raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu);
+	struct cgroup_rstat_cpu *rstatc = cgroup_rstat_cpu(root, cpu);
+	struct cgroup *head = NULL, *parent, *child;
+	unsigned long flags;
 
 	/*
-	 * We're gonna walk down to the first leaf and visit/remove it.  We
-	 * can pick whatever unvisited node as the starting point.
+	 * The _irqsave() is needed because cgroup_rstat_lock is
+	 * spinlock_t which is a sleeping lock on PREEMPT_RT. Acquiring
+	 * this lock with the _irq() suffix only disables interrupts on
+	 * a non-PREEMPT_RT kernel. The raw_spinlock_t below disables
+	 * interrupts on both configurations. The _irqsave() ensures
+	 * that interrupts are always disabled and later restored.
 	 */
-	if (!pos) {
-		pos = root;
-		/* return NULL if this subtree is not on-list */
-		if (!cgroup_rstat_cpu(pos, cpu)->updated_next)
-			return NULL;
-	} else {
-		pos = cgroup_parent(pos);
-	}
+	raw_spin_lock_irqsave(cpu_lock, flags);
 
-	/* walk down to the first leaf */
-	while (true) {
-		rstatc = cgroup_rstat_cpu(pos, cpu);
-		if (rstatc->updated_children == pos)
-			break;
-		pos = rstatc->updated_children;
-	}
+	/* Return NULL if this subtree is not on-list */
+	if (!rstatc->updated_next)
+		goto unlock_ret;
 
 	/*
-	 * Unlink @pos from the tree.  As the updated_children list is
+	 * Unlink @root from its parent. As the updated_children list is
 	 * singly linked, we have to walk it to find the removal point.
-	 * However, due to the way we traverse, @pos will be the first
-	 * child in most cases. The only exception is @root.
 	 */
-	parent = cgroup_parent(pos);
+	parent = cgroup_parent(root);
 	if (parent) {
 		struct cgroup_rstat_cpu *prstatc;
 		struct cgroup **nextp;
 
 		prstatc = cgroup_rstat_cpu(parent, cpu);
 		nextp = &prstatc->updated_children;
-		while (*nextp != pos) {
+		while (*nextp != root) {
 			struct cgroup_rstat_cpu *nrstatc;
 
 			nrstatc = cgroup_rstat_cpu(*nextp, cpu);
@@ -142,31 +187,15 @@ static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos,
 	}
 
 	rstatc->updated_next = NULL;
-	return pos;
-}
 
-/* Return a list of updated cgroups to be flushed */
-static struct cgroup *cgroup_rstat_updated_list(struct cgroup *root, int cpu)
-{
-	raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu);
-	struct cgroup *head, *tail, *next;
-	unsigned long flags;
-
-	/*
-	 * The _irqsave() is needed because cgroup_rstat_lock is
-	 * spinlock_t which is a sleeping lock on PREEMPT_RT. Acquiring
-	 * this lock with the _irq() suffix only disables interrupts on
-	 * a non-PREEMPT_RT kernel. The raw_spinlock_t below disables
-	 * interrupts on both configurations. The _irqsave() ensures
-	 * that interrupts are always disabled and later restored.
-	 */
-	raw_spin_lock_irqsave(cpu_lock, flags);
-	head = tail = cgroup_rstat_cpu_pop_updated(NULL, root, cpu);
-	while (tail) {
-		next = cgroup_rstat_cpu_pop_updated(tail, root, cpu);
-		tail->rstat_flush_next = next;
-		tail = next;
-	}
+	/* Push @root to the list first before pushing the children */
+	head = root;
+	root->rstat_flush_next = NULL;
+	child = rstatc->updated_children;
+	rstatc->updated_children = root;
+	if (child != root)
+		head = cgroup_rstat_push_children(head, child, cpu);
+unlock_ret:
 	raw_spin_unlock_irqrestore(cpu_lock, flags);
 	return head;
 }
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH-cgroup v5 2/2] cgroup: Avoid false cacheline sharing of read mostly rstat_cpu
  2023-11-30 20:43 [PATCH-cgroup v5 0/2] cgroup/rstat: Further reduce cpu_lock hold time in cgroup_rstat_flush_locked Waiman Long
  2023-11-30 20:43 ` [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list() Waiman Long
@ 2023-11-30 20:43 ` Waiman Long
  2023-12-01 17:38   ` Tejun Heo
  1 sibling, 1 reply; 6+ messages in thread
From: Waiman Long @ 2023-11-30 20:43 UTC (permalink / raw)
  To: Tejun Heo, Zefan Li, Johannes Weiner
  Cc: cgroups, linux-kernel, Joe Mario, Sebastian Jug, Yosry Ahmed,
	Waiman Long

The rstat_cpu and also rstat_css_list of the cgroup structure are read
mostly variables. However, they may share the same cacheline as the
subsequent rstat_flush_next and *bstat variables which can be updated
frequently.  That will slow down the cgroup_rstat_cpu() call which is
called pretty frequently in the rstat code. Add a CACHELINE_PADDING()
line in between them to avoid false cacheline sharing.

A parallel kernel build on a 2-socket x86-64 server is used as the
benchmarking tool for measuring the lock hold time. Below were the lock
hold time frequency distribution before and after the patch:

      Run time        Before patch       After patch
      --------        ------------       -----------
       0-01 us         9,928,562          9,820,428
      01-05 us           110,151             50,935
      05-10 us               270                 93
      10-15 us               273                146
      15-20 us               135                 76
      20-25 us                 0                  2
      25-30 us                 1                  0

It can be seen that the patch further pushes the lock hold time towards
the lower end.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 include/linux/cgroup-defs.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
index 37518436cfe7..5a97ea95b564 100644
--- a/include/linux/cgroup-defs.h
+++ b/include/linux/cgroup-defs.h
@@ -496,6 +496,13 @@ struct cgroup {
 	struct cgroup_rstat_cpu __percpu *rstat_cpu;
 	struct list_head rstat_css_list;
 
+	/*
+	 * Add padding to separate the read mostly rstat_cpu and
+	 * rstat_css_list into a different cacheline from the following
+	 * rstat_flush_next and *bstat fields which can have frequent updates.
+	 */
+	CACHELINE_PADDING(_pad_);
+
 	/*
 	 * A singly-linked list of cgroup structures to be rstat flushed.
 	 * This is a scratch field to be used exclusively by
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list()
  2023-11-30 20:43 ` [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list() Waiman Long
@ 2023-12-01 17:37   ` Tejun Heo
  2023-12-01 18:09     ` Waiman Long
  0 siblings, 1 reply; 6+ messages in thread
From: Tejun Heo @ 2023-12-01 17:37 UTC (permalink / raw)
  To: Waiman Long
  Cc: Zefan Li, Johannes Weiner, cgroups, linux-kernel, Joe Mario,
	Sebastian Jug, Yosry Ahmed

On Thu, Nov 30, 2023 at 03:43:26PM -0500, Waiman Long wrote:
> The current design of cgroup_rstat_cpu_pop_updated() is to traverse
> the updated tree in a way to pop out the leaf nodes first before
> their parents. This can cause traversal of multiple nodes before a
> leaf node can be found and popped out. IOW, a given node in the tree
> can be visited multiple times before the whole operation is done. So
> it is not very efficient and the code can be hard to read.
> 
> With the introduction of cgroup_rstat_updated_list() to build a list
> of cgroups to be flushed first before any flushing operation is being
> done, we can optimize the way the updated tree nodes are being popped
> by pushing the parents first to the tail end of the list before their
> children. In this way, most updated tree nodes will be visited only
> once with the exception of the subtree root as we still need to go
> back to its parent and popped it out of its updated_children list.
> This also makes the code easier to read.
> 
> Signed-off-by: Waiman Long <longman@redhat.com>

Applied to cgroup/for-6.8 with a small comment edit.

...
> + * Iteratively traverse down the cgroup_rstat_cpu updated tree level by
> + * level and push all the parents first before their next level children
> + * into a singly linked list built from the tail backward like "pushing"
> + * cgroups into a stack. The parent is by the caller.

I found the last sentence a bit difficult to understand and changed it to
"The root is pushed by the caller." That's what you meant, right?

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH-cgroup v5 2/2] cgroup: Avoid false cacheline sharing of read mostly rstat_cpu
  2023-11-30 20:43 ` [PATCH-cgroup v5 2/2] cgroup: Avoid false cacheline sharing of read mostly rstat_cpu Waiman Long
@ 2023-12-01 17:38   ` Tejun Heo
  0 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2023-12-01 17:38 UTC (permalink / raw)
  To: Waiman Long
  Cc: Zefan Li, Johannes Weiner, cgroups, linux-kernel, Joe Mario,
	Sebastian Jug, Yosry Ahmed

On Thu, Nov 30, 2023 at 03:43:27PM -0500, Waiman Long wrote:
> The rstat_cpu and also rstat_css_list of the cgroup structure are read
> mostly variables. However, they may share the same cacheline as the
> subsequent rstat_flush_next and *bstat variables which can be updated
> frequently.  That will slow down the cgroup_rstat_cpu() call which is
> called pretty frequently in the rstat code. Add a CACHELINE_PADDING()
> line in between them to avoid false cacheline sharing.
> 
> A parallel kernel build on a 2-socket x86-64 server is used as the
> benchmarking tool for measuring the lock hold time. Below were the lock
> hold time frequency distribution before and after the patch:
> 
>       Run time        Before patch       After patch
>       --------        ------------       -----------
>        0-01 us         9,928,562          9,820,428
>       01-05 us           110,151             50,935
>       05-10 us               270                 93
>       10-15 us               273                146
>       15-20 us               135                 76
>       20-25 us                 0                  2
>       25-30 us                 1                  0
> 
> It can be seen that the patch further pushes the lock hold time towards
> the lower end.
> 
> Signed-off-by: Waiman Long <longman@redhat.com>

Applied to cgroup/for-6.8.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list()
  2023-12-01 17:37   ` Tejun Heo
@ 2023-12-01 18:09     ` Waiman Long
  0 siblings, 0 replies; 6+ messages in thread
From: Waiman Long @ 2023-12-01 18:09 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Zefan Li, Johannes Weiner, cgroups, linux-kernel, Joe Mario,
	Sebastian Jug, Yosry Ahmed


On 12/1/23 12:37, Tejun Heo wrote:
> On Thu, Nov 30, 2023 at 03:43:26PM -0500, Waiman Long wrote:
>> The current design of cgroup_rstat_cpu_pop_updated() is to traverse
>> the updated tree in a way to pop out the leaf nodes first before
>> their parents. This can cause traversal of multiple nodes before a
>> leaf node can be found and popped out. IOW, a given node in the tree
>> can be visited multiple times before the whole operation is done. So
>> it is not very efficient and the code can be hard to read.
>>
>> With the introduction of cgroup_rstat_updated_list() to build a list
>> of cgroups to be flushed first before any flushing operation is being
>> done, we can optimize the way the updated tree nodes are being popped
>> by pushing the parents first to the tail end of the list before their
>> children. In this way, most updated tree nodes will be visited only
>> once with the exception of the subtree root as we still need to go
>> back to its parent and popped it out of its updated_children list.
>> This also makes the code easier to read.
>>
>> Signed-off-by: Waiman Long <longman@redhat.com>
> Applied to cgroup/for-6.8 with a small comment edit.
>
> ...
>> + * Iteratively traverse down the cgroup_rstat_cpu updated tree level by
>> + * level and push all the parents first before their next level children
>> + * into a singly linked list built from the tail backward like "pushing"
>> + * cgroups into a stack. The parent is by the caller.
> I found the last sentence a bit difficult to understand and changed it to
> "The root is pushed by the caller." That's what you meant, right?

Yes, you are right. Thanks for the edit.

Cheers,
Longman


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-01 18:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-30 20:43 [PATCH-cgroup v5 0/2] cgroup/rstat: Further reduce cpu_lock hold time in cgroup_rstat_flush_locked Waiman Long
2023-11-30 20:43 ` [PATCH-cgroup v5 1/2] cgroup/rstat: Optimize cgroup_rstat_updated_list() Waiman Long
2023-12-01 17:37   ` Tejun Heo
2023-12-01 18:09     ` Waiman Long
2023-11-30 20:43 ` [PATCH-cgroup v5 2/2] cgroup: Avoid false cacheline sharing of read mostly rstat_cpu Waiman Long
2023-12-01 17:38   ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox