Skip to contentSkip to conversation

cgroup/rstat: Reduce cpu_lock hold time in cgroup_rstat_flush_locked()

PATCH Applied Waiman Long 3 participants 10 messages Nov 4, 2023 – Nov 12, 2023 cgroup

v3 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover cgroup/rstat: Reduce cpu_lock hold time in cgroup_rstat_flush_locked()

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 reduce of the cpu_lock hold time in 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.

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 applying different number of patches:

  Hold time   Before patch   Patch 1   Patches 1-2  Patches 1-3
  ---------   ------------   -------   -----------  -----------
    0-01 us      804,139   13,738,708   14,594,545   15,484,707
   01-05 us    9,772,767    1,177,194      439,926      207,382
   05-10 us    4,595,028        4,984        5,960        3,174
   10-15 us      303,481        3,562        3,543        3,006
   15-20 us       78,971        1,314        1,397        1,066
   20-25 us       24,583           18           25           15
   25-30 us        6,908           12           12           10
   30-40 us        8,015
   40-50 us        2,192
   50-60 us          316
   60-70 us           43
   70-80 us            7
   80-90 us            2
     >90 us            3

Waiman Long (3):
cgroup/rstat: Reduce cpu_lock hold time in cgroup_rstat_flush_locked() cgroup/rstat: Optimize cgroup_rstat_updated_list()
cgroup: Avoid false cacheline sharing of read mostly rstat_cpu

 include/linux/cgroup-defs.h |  14 ++++
 kernel/cgroup/rstat.c       | 129 +++++++++++++++++++++---------------
 2 files changed, 89 insertions(+), 54 deletions(-)
Signature · 1 line
2.39.3
#1 lore raw

PATCH v3 1/3 cgroup/rstat: Reduce cpu_lock hold time in cgroup_rstat_flush_locked()

When cgroup_rstat_updated() isn't being called concurrently with cgroup_rstat_flush_locked(), its run time is pretty short. When both are called concurrently, the cgroup_rstat_updated() run time can spike to a pretty high value due to high cpu_lock hold time in cgroup_rstat_flush_locked(). This can be problematic if the task calling cgroup_rstat_updated() is a realtime task running on an isolated CPU with a strict latency requirement. The cgroup_rstat_updated() call can happen when there is a page fault even though the task is running in user space most of the time.

The percpu cpu_lock is used to protect the update tree - updated_next and updated_children. This protection is only needed when cgroup_rstat_cpu_pop_updated() is being called. The subsequent flushing operation which can take a much longer time does not need that protection as it is already protected by cgroup_rstat_lock.

To reduce the cpu_lock hold time, we need to perform all the cgroup_rstat_cpu_pop_updated() calls up front with the lock released afterward before doing any flushing. This patch adds a new cgroup_rstat_updated_list() function to return a singly linked list of cgroups to be flushed.

Some instrumentation code are added to measure the cpu_lock hold time right after lock acquisition to after releasing the lock. Parallel kernel build on a 2-socket x86-64 server is used as the benchmarking tool for measuring the lock hold time.

The maximum cpu_lock hold time before and after the patch are 100us and 29us respectively. So the worst case time is reduced to about 30% of the original. However, there may be some OS or hardware noises like NMI or SMI in the test system that can worsen the worst case value. Those noises are usually tuned out in a real production environment to get a better result.

OTOH, the lock hold time frequency distribution should give a better idea of the performance benefit of the patch. Below were the frequency distribution before and after the patch:

     Hold time        Before patch       After patch
     ---------        ------------       -----------
       0-01 us           804,139         13,738,708
      01-05 us         9,772,767          1,177,194
      05-10 us         4,595,028              4,984
      10-15 us           303,481              3,562
      15-20 us            78,971              1,314
      20-25 us            24,583                 18
      25-30 us             6,908                 12
      30-40 us             8,015
      40-50 us             2,192
      50-60 us               316
      60-70 us                43
      70-80 us                 7
      80-90 us                 2
        >90 us                 3
---
 include/linux/cgroup-defs.h |  7 ++++++
 kernel/cgroup/rstat.c       | 43 ++++++++++++++++++++++++-------------
 2 files changed, 35 insertions(+), 15 deletions(-)
include/linux/cgroup-defs.h +7 -0
index 265da00a1a8b..ff4b4c590f32 100644
@@ -491,6 +491,13 @@ struct cgroup {
 	struct cgroup_rstat_cpu __percpu *rstat_cpu;
 	struct list_head rstat_css_list;
 
+	/*
+	 * A singly-linked list of cgroup structures to be rstat flushed.
+	 * This is a scratch field to be used exclusively by
+	 * cgroup_rstat_flush_locked() and protected by cgroup_rstat_lock.
+	 */
+	struct cgroup	*rstat_flush_next;
+
 	/* cgroup basic resource statistics */
 	struct cgroup_base_stat last_bstat;
 	struct cgroup_base_stat bstat;
kernel/cgroup/rstat.c +28 -15

62 lines not on this page · raw message

Signature · 1 line
2.39.3
maintainer Applied
#2 lore raw
Quoted 55 lines · Waiman Long

When cgroup_rstat_updated() isn't being called concurrently with cgroup_rstat_flush_locked(), its run time is pretty short. When both are called concurrently, the cgroup_rstat_updated() run time can spike to a pretty high value due to high cpu_lock hold time in cgroup_rstat_flush_locked(). This can be problematic if the task calling cgroup_rstat_updated() is a realtime task running on an isolated CPU with a strict latency requirement. The cgroup_rstat_updated() call can happen when there is a page fault even though the task is running in user space most of the time.

The percpu cpu_lock is used to protect the update tree - updated_next and updated_children. This protection is only needed when cgroup_rstat_cpu_pop_updated() is being called. The subsequent flushing operation which can take a much longer time does not need that protection as it is already protected by cgroup_rstat_lock.

To reduce the cpu_lock hold time, we need to perform all the cgroup_rstat_cpu_pop_updated() calls up front with the lock released afterward before doing any flushing. This patch adds a new cgroup_rstat_updated_list() function to return a singly linked list of cgroups to be flushed.

Some instrumentation code are added to measure the cpu_lock hold time right after lock acquisition to after releasing the lock. Parallel kernel build on a 2-socket x86-64 server is used as the benchmarking tool for measuring the lock hold time.

The maximum cpu_lock hold time before and after the patch are 100us and 29us respectively. So the worst case time is reduced to about 30% of the original. However, there may be some OS or hardware noises like NMI or SMI in the test system that can worsen the worst case value. Those noises are usually tuned out in a real production environment to get a better result.

OTOH, the lock hold time frequency distribution should give a better idea of the performance benefit of the patch. Below were the frequency distribution before and after the patch:

     Hold time        Before patch       After patch
     ---------        ------------       -----------
       0-01 us           804,139         13,738,708
      01-05 us         9,772,767          1,177,194
      05-10 us         4,595,028              4,984
      10-15 us           303,481              3,562
      15-20 us            78,971              1,314
      20-25 us            24,583                 18
      25-30 us             6,908                 12
      30-40 us             8,015
      40-50 us             2,192
      50-60 us               316
      60-70 us                43
      70-80 us                 7
      80-90 us                 2
        >90 us                 3
Quoted 2 lines

Applied this one to cgroup/for-6.8. Will wait for the updated version for the other patches.

Thanks.

Signature · 1 line
tejun
#3 lore raw

PATCH v3 2/3 cgroup/rstat: Optimize cgroup_rstat_updated_list()

In reply to 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.

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:

     Hold time        Before patch       After patch
     ---------        ------------       -----------
       0-01 us        13,738,708         14,594,545
      01-05 us         1,177,194            439,926
      05-10 us             4,984              5,960
      10-15 us             3,562              3,543
      15-20 us             1,314              1,397
      20-25 us                18                 25
      25-30 us                12                 12

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

---
 kernel/cgroup/rstat.c | 132 ++++++++++++++++++++++--------------------
 1 file changed, 70 insertions(+), 62 deletions(-)
kernel/cgroup/rstat.c +70 -62
index 1f300bf4dc40..d2b709cfeb2a 100644
@@ -74,64 +74,90 @@ __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 (= parent cgroup)
+ * @prstatc: cgroup_rstat_cpu of the parent cgroup
  * @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.
+ * Recursively traverse down the cgroup_rstat_cpu updated tree and push
+ * parent first before its children. The parent is pushed by the caller.
+ * The recursion depth is the depth of the current updated tree.
+ */
+static struct cgroup *cgroup_rstat_push_children(struct cgroup *head,
+				struct cgroup_rstat_cpu *prstatc, int cpu)
+{
+	struct cgroup *child, *parent;
+	struct cgroup_rstat_cpu *crstatc;
+
+	parent = head;
+	child = prstatc->updated_children;
+	prstatc->updated_children = parent;
+
+	/* updated_next is parent cgroup terminated */
+	while (child != parent) {
+		child->rstat_flush_next = head;
+		head = child;
+		crstatc = cgroup_rstat_cpu(child, cpu);
+		if (crstatc->updated_children != parent)
+			head = cgroup_rstat_push_children(head, crstatc, cpu);
+		child = crstatc->updated_next;
+		crstatc->updated_next = NULL;
+	}
+	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.  Must be called
+ * with the matching cgroup_rstat_cpu_lock held.
  *
  * 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 while updated_next is
+ * parent cgroup terminated except the cgroup root which 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;
+	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 +168,13 @@ 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;
+	if (rstatc->updated_children != root)
+		head = cgroup_rstat_push_children(head, rstatc, cpu);
+unlock_ret:
 	raw_spin_unlock_irqrestore(cpu_lock, flags);
 	return head;
 }
Signature · 1 line
2.39.3
#4 lore raw

Re: [PATCH v3 2/3] cgroup/rstat: Optimize cgroup_rstat_updated_list()

Quoted 31 lines · 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.

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:

     Hold time        Before patch       After patch
     ---------        ------------       -----------
       0-01 us        13,738,708         14,594,545
      01-05 us         1,177,194            439,926
      05-10 us             4,984              5,960
      10-15 us             3,562              3,543
      15-20 us             1,314              1,397
      20-25 us                18                 25
      25-30 us                12                 12
Quoted 5 lines

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

---

I don't know why git decided to show this diff in the most confusing way possible.

Quoted 16 lines
 kernel/cgroup/rstat.c | 132 ++++++++++++++++++++++--------------------
 1 file changed, 70 insertions(+), 62 deletions(-)
kernel/cgroup/rstat.c +2 -3
index 1f300bf4dc40..d2b709cfeb2a 100644
@@ -74,64 +74,90 @@ __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 (= parent cgroup)

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 84

Quoted 10 lines
kernel/cgroup/rstat.c +4 -4
+ * @prstatc: cgroup_rstat_cpu of the parent cgroup
  * @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.
+ * Recursively traverse down the cgroup_rstat_cpu updated tree and push
+ * parent first before its children. The parent is pushed by the caller.

I think it might be useful here (and elsewhere in the patch) where "push" is being used to elaborate that we push to the beginning in a stack-like fashion.

Quoted 8 lines
kernel/cgroup/rstat.c +8 -0
+ * The recursion depth is the depth of the current updated tree.
+ */
+static struct cgroup *cgroup_rstat_push_children(struct cgroup *head,
+                               struct cgroup_rstat_cpu *prstatc, int cpu)
+{
+       struct cgroup *child, *parent;
+       struct cgroup_rstat_cpu *crstatc;
+

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 102

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+       parent = head;
+       child = prstatc->updated_children;
+       prstatc->updated_children = parent;
+
+       /* updated_next is parent cgroup terminated */
+       while (child != parent) {
+               child->rstat_flush_next = head;
+               head = child;
+               crstatc = cgroup_rstat_cpu(child, cpu);
+               if (crstatc->updated_children != parent)

I think cgroup->updated_children is set to the cgroup itself if it's empty, right? Shouldn't this be crstatc->updated_children != child?

Quoted 6 lines
kernel/cgroup/rstat.c +6 -0
+                       head = cgroup_rstat_push_children(head, crstatc, cpu);
+               child = crstatc->updated_next;
+               crstatc->updated_next = NULL;
+       }
+       return head;
+}

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 118

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+
+/**
+ * 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.  Must be called
+ * with the matching cgroup_rstat_cpu_lock held.

This function takes care of holding the lock actually. I think that sentence should be applied to cgroup_rstat_push_children() above?

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 126

Quoted 10 lines
kernel/cgroup/rstat.c +6 -2
  *
  * 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 while updated_next is
+ * parent cgroup terminated except the cgroup root which can be self
+ * terminated.

IIUC updated_children and updated_next is the same list. updated_children is the head, and updated_next is how the list items are linked. This comment makes it seem like they are two different lists.

I am actually wondering if it's worth using the singly linked list here. We are saving 8 bytes percpu, but the semantics are fairly confusing. Wouldn't this be easier to reason about if you just use list_head?

updated_children would be replaced with LIST_HEAD (or similar), and the list would be NULL terminated instead of terminated by self/parent cgroup. IIUC the reason it's not NULL-terminated now is because we use cgroup->updated_next to check quickly if a cgroup is on the list or not. If we use list_heads, we can just use list_emtpy() IIUC.

We can also simplify the semantics of unlinking @root from the updated tree below, it would just be list_del() IIUC, which is actually more performant as well. It seems like overall we would simplify a lot of things. When forming the updated_list, we can just walk the tree and splice the lists in the correct order.

It seems to me that saving 8 bytes percpu is not worth the complexity of the custom list semantics here. Am I missing something here?

#5 lore raw

Re: [PATCH v3 2/3] cgroup/rstat: Optimize cgroup_rstat_updated_list()

Quoted 31 lines · Yosry Ahmed
Quoted 30 lines · 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.

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:

      Hold time        Before patch       After patch
      ---------        ------------       -----------
        0-01 us        13,738,708         14,594,545
       01-05 us         1,177,194            439,926
       05-10 us             4,984              5,960
       10-15 us             3,562              3,543
       15-20 us             1,314              1,397
       20-25 us                18                 25
       25-30 us                12                 12
Quoted 7 lines
Quoted 5 lines

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

---

I don't know why git decided to show this diff in the most confusing way possible.

I agree. The diff is really hard to look at. It will be easier to apply the patch & looks at the actual rstat.c file.

Quoted 22 lines
Quoted 22 lines
  kernel/cgroup/rstat.c | 132 ++++++++++++++++++++++--------------------
  1 file changed, 70 insertions(+), 62 deletions(-)
kernel/cgroup/rstat.c +4 -4
index 1f300bf4dc40..d2b709cfeb2a 100644
@@ -74,64 +74,90 @@ __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 (= parent cgroup)
+ * @prstatc: cgroup_rstat_cpu of the parent cgroup
   * @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
Quoted 8 lines

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 84

Quoted 5 lines
kernel/cgroup/rstat.c +2 -3
- * 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.
+ * Recursively traverse down the cgroup_rstat_cpu updated tree and push
+ * parent first before its children. The parent is pushed by the caller.

I think it might be useful here (and elsewhere in the patch) where "push" is being used to elaborate that we push to the beginning in a stack-like fashion.

Right, I am thinking about a stack when I use the word "push". I will clarify that in the comment.

Quoted 13 lines
Quoted 13 lines
kernel/cgroup/rstat.c +12 -0
+ * The recursion depth is the depth of the current updated tree.
+ */
+static struct cgroup *cgroup_rstat_push_children(struct cgroup *head,
+                               struct cgroup_rstat_cpu *prstatc, int cpu)
+{
+       struct cgroup *child, *parent;
+       struct cgroup_rstat_cpu *crstatc;
+
+       parent = head;
+       child = prstatc->updated_children;
+       prstatc->updated_children = parent;
+
Quoted 8 lines

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 102

Quoted 6 lines
kernel/cgroup/rstat.c +6 -0
+       /* updated_next is parent cgroup terminated */
+       while (child != parent) {
+               child->rstat_flush_next = head;
+               head = child;
+               crstatc = cgroup_rstat_cpu(child, cpu);
+               if (crstatc->updated_children != parent)

I think cgroup->updated_children is set to the cgroup itself if it's empty, right? Shouldn't this be crstatc->updated_children != child?

My mistake. Will fix it in the next version.

Quoted 11 lines
Quoted 11 lines
kernel/cgroup/rstat.c +10 -0
+                       head = cgroup_rstat_push_children(head, crstatc, cpu);
+               child = crstatc->updated_next;
+               crstatc->updated_next = NULL;
+       }
+       return head;
+}
+
+/**
+ * cgroup_rstat_updated_list - return a list of updated cgroups to be flushed
+ * @root: root of the cgroup subtree to traverse
Quoted 8 lines

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 118

Quoted 6 lines
kernel/cgroup/rstat.c +6 -0
+ * @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.  Must be called
+ * with the matching cgroup_rstat_cpu_lock held.

This function takes care of holding the lock actually. I think that sentence should be applied to cgroup_rstat_push_children() above?

It is left over from before this patch. Will remove that.

Quoted 11 lines

kernel/cgroup/rstat.c __bpf_kfunc void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) · line 126

Quoted 11 lines
kernel/cgroup/rstat.c +6 -2
   *
   * 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 while updated_next is
+ * parent cgroup terminated except the cgroup root which can be self
+ * terminated.
Quoted 4 lines

IIUC updated_children and updated_next is the same list. updated_children is the head, and updated_next is how the list items are linked. This comment makes it seem like they are two different lists.

Thanks for the comment. I will rework the comment to clarify that a bit more.

Quoted 12 lines

I am actually wondering if it's worth using the singly linked list here. We are saving 8 bytes percpu, but the semantics are fairly confusing. Wouldn't this be easier to reason about if you just use list_head?

updated_children would be replaced with LIST_HEAD (or similar), and the list would be NULL terminated instead of terminated by self/parent cgroup. IIUC the reason it's not NULL-terminated now is because we use cgroup->updated_next to check quickly if a cgroup is on the list or not. If we use list_heads, we can just use list_emtpy() IIUC.

Quoted 8 lines

We can also simplify the semantics of unlinking @root from the updated tree below, it would just be list_del() IIUC, which is actually more performant as well. It seems like overall we would simplify a lot of things. When forming the updated_list, we can just walk the tree and splice the lists in the correct order.

It seems to me that saving 8 bytes percpu is not worth the complexity of the custom list semantics here. Am I missing something here?

It will cost an additional 16 bytes of percpu memory if converted to list_heads. Like other lists, there will be sibling and children list_heads. There are also 2 pointers to update instead of one. Anyway, I don't have an objection to convert them to list_heads if agreed by Tejun.

Cheers,
Longman

#6 lore raw

Re: [PATCH v3 2/3] cgroup/rstat: Optimize cgroup_rstat_updated_list()

Quoted 36 lines · Waiman Long
Quoted 34 lines · Yosry Ahmed
Quoted 33 lines · 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.

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:

      Hold time        Before patch       After patch
      ---------        ------------       -----------
        0-01 us        13,738,708         14,594,545
       01-05 us         1,177,194            439,926
       05-10 us             4,984              5,960
       10-15 us             3,562              3,543
       15-20 us             1,314              1,397
       20-25 us                18                 25
       25-30 us                12                 12

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

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

---

I don't know why git decided to show this diff in the most confusing way possible.

I agree. The diff is really hard to look at. It will be easier to apply the patch & looks at the actual rstat.c file.

Would the diff be simpler if patches 1 & 2 were squashed?

[..]

Quoted 35 lines
Quoted 11 lines
Quoted 11 lines

*
* 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 while updated_next is
+ * parent cgroup terminated except the cgroup root which can be self
+ * terminated.

Quoted 4 lines

IIUC updated_children and updated_next is the same list. updated_children is the head, and updated_next is how the list items are linked. This comment makes it seem like they are two different lists.

Thanks for the comment. I will rework the comment to clarify that a bit more.

Quoted 18 lines

I am actually wondering if it's worth using the singly linked list here. We are saving 8 bytes percpu, but the semantics are fairly confusing. Wouldn't this be easier to reason about if you just use list_head?

updated_children would be replaced with LIST_HEAD (or similar), and the list would be NULL terminated instead of terminated by self/parent cgroup. IIUC the reason it's not NULL-terminated now is because we use cgroup->updated_next to check quickly if a cgroup is on the list or not. If we use list_heads, we can just use list_emtpy() IIUC.

We can also simplify the semantics of unlinking @root from the updated tree below, it would just be list_del() IIUC, which is actually more performant as well. It seems like overall we would simplify a lot of things. When forming the updated_list, we can just walk the tree and splice the lists in the correct order.

Quoted 7 lines
Quoted 2 lines

It seems to me that saving 8 bytes percpu is not worth the complexity of the custom list semantics here. Am I missing something here?

It will cost an additional 16 bytes of percpu memory if converted to list_heads. Like other lists, there will be sibling and children list_heads. There are also 2 pointers to update instead of one. Anyway, I don't have an objection to convert them to list_heads if agreed by Tejun.

Yes you are right. It's definitely not free, but it's also not super costly. It's just that every time I look at the rstat code I need to remind myself of how updated_next and updated_children work. I will let Tejun decide.

#7 lore raw

Re: [PATCH v3 2/3] cgroup/rstat: Optimize cgroup_rstat_updated_list()

Quoted 36 lines · Yosry Ahmed
Quoted 35 lines · Waiman Long
Quoted 34 lines · Yosry Ahmed
Quoted 33 lines · 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.

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:

       Hold time        Before patch       After patch
       ---------        ------------       -----------
         0-01 us        13,738,708         14,594,545
        01-05 us         1,177,194            439,926
        05-10 us             4,984              5,960
        10-15 us             3,562              3,543
        15-20 us             1,314              1,397
        20-25 us                18                 25
        25-30 us                12                 12

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

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

---

I don't know why git decided to show this diff in the most confusing way possible.

I agree. The diff is really hard to look at. It will be easier to apply the patch & looks at the actual rstat.c file.

Would the diff be simpler if patches 1 & 2 were squashed?

Maybe, but I prefer to keep them separate for now.

Quoted 37 lines

[..]

Quoted 35 lines
Quoted 10 lines
Quoted 10 lines

*
* 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 while updated_next is
+ * parent cgroup terminated except the cgroup root which can be self
+ * terminated.

Quoted 4 lines

IIUC updated_children and updated_next is the same list. updated_children is the head, and updated_next is how the list items are linked. This comment makes it seem like they are two different lists.

Thanks for the comment. I will rework the comment to clarify that a bit more.

Quoted 19 lines

I am actually wondering if it's worth using the singly linked list here. We are saving 8 bytes percpu, but the semantics are fairly confusing. Wouldn't this be easier to reason about if you just use list_head?

updated_children would be replaced with LIST_HEAD (or similar), and the list would be NULL terminated instead of terminated by self/parent cgroup. IIUC the reason it's not NULL-terminated now is because we use cgroup->updated_next to check quickly if a cgroup is on the list or not. If we use list_heads, we can just use list_emtpy() IIUC.

We can also simplify the semantics of unlinking @root from the updated tree below, it would just be list_del() IIUC, which is actually more performant as well. It seems like overall we would simplify a lot of things. When forming the updated_list, we can just walk the tree and splice the lists in the correct order.

It seems to me that saving 8 bytes percpu is not worth the complexity of the custom list semantics here. Am I missing something here?

Quoted 8 lines
Quoted 4 lines

It will cost an additional 16 bytes of percpu memory if converted to list_heads. Like other lists, there will be sibling and children list_heads. There are also 2 pointers to update instead of one. Anyway, I don't have an objection to convert them to list_heads if agreed by Tejun.

Yes you are right. It's definitely not free, but it's also not super costly. It's just that every time I look at the rstat code I need to remind myself of how updated_next and updated_children work. I will let Tejun decide.

After further thought, changing it to list_head may not be possible with the current design since the actual linkage is like:

update_next -> cgroup + cpu --> update_next --> ...

So unless we change the design to link cgroup_rstat_cpu directly to each other for a given CPU, not via a cgroup intermediary, we will not be able to use list_head and the associated list_add() & list_del() macros. Direct linkage, however, requires a cgroup back pointer. So the real cost will be 24 bytes instead.

Cheers,
Longman

#8 lore raw

Re: [PATCH v3 2/3] cgroup/rstat: Optimize cgroup_rstat_updated_list()

Quoted 47 lines
Quoted 35 lines
Quoted 35 lines
Quoted 10 lines
Quoted 10 lines

*
* 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 while updated_next is
+ * parent cgroup terminated except the cgroup root which can be self
+ * terminated.

Quoted 4 lines

IIUC updated_children and updated_next is the same list. updated_children is the head, and updated_next is how the list items are linked. This comment makes it seem like they are two different lists.

Thanks for the comment. I will rework the comment to clarify that a bit more.

Quoted 19 lines

I am actually wondering if it's worth using the singly linked list here. We are saving 8 bytes percpu, but the semantics are fairly confusing. Wouldn't this be easier to reason about if you just use list_head?

updated_children would be replaced with LIST_HEAD (or similar), and the list would be NULL terminated instead of terminated by self/parent cgroup. IIUC the reason it's not NULL-terminated now is because we use cgroup->updated_next to check quickly if a cgroup is on the list or not. If we use list_heads, we can just use list_emtpy() IIUC.

We can also simplify the semantics of unlinking @root from the updated tree below, it would just be list_del() IIUC, which is actually more performant as well. It seems like overall we would simplify a lot of things. When forming the updated_list, we can just walk the tree and splice the lists in the correct order.

It seems to me that saving 8 bytes percpu is not worth the complexity of the custom list semantics here. Am I missing something here?

Quoted 8 lines
Quoted 4 lines

It will cost an additional 16 bytes of percpu memory if converted to list_heads. Like other lists, there will be sibling and children list_heads. There are also 2 pointers to update instead of one. Anyway, I don't have an objection to convert them to list_heads if agreed by Tejun.

Yes you are right. It's definitely not free, but it's also not super costly. It's just that every time I look at the rstat code I need to remind myself of how updated_next and updated_children work. I will let Tejun decide.

After further thought, changing it to list_head may not be possible with the current design since the actual linkage is like:

Quoted 7 lines

update_next -> cgroup + cpu --> update_next --> ...

So unless we change the design to link cgroup_rstat_cpu directly to each other for a given CPU, not via a cgroup intermediary, we will not be able to use list_head and the associated list_add() & list_del() macros. Direct linkage, however, requires a cgroup back pointer. So the real cost will be 24 bytes instead.

Yes you are right. Perhaps it's not worth it and it may not be as simple as I thought. Please dismiss this suggestion, we'll have to rely on comments for now to keep things clear.

#9 lore raw

PATCH v3 3/3 cgroup: Avoid false cacheline sharing of read mostly rstat_cpu

In reply to 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        14,594,545         15,484,707
      01-05 us           439,926            207,382
      05-10 us             5,960              3,174
      10-15 us             3,543              3,006
      15-20 us             1,397              1,066
      20-25 us                25                 15
      25-30 us                12                 10

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

---
 include/linux/cgroup-defs.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/cgroup-defs.h +7 -0
index ff4b4c590f32..a4adc0580135 100644
@@ -491,6 +491,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
Signature · 1 line
2.39.3