sched/fair: Avoid false sharing in nohz struct
PATCH 1/1 sched/fair: Avoid false sharing in nohz struct
There are two potential false sharing issue in nohz struct:
1. idle_cpus_mask is a read-mostly field, but share the same cacheline with frequently updated nr_cpus.
2. Data followed by nohz still share the same cacheline and has potential false sharing issue.
This patch tries to resolve the above two problems by isolating the frequently updated fields in a single cacheline.
--- kernel/sched/fair.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/sched/fair.c +4 -3
@@ -7193,13 +7193,14 @@ static DEFINE_PER_CPU(cpumask_var_t, should_we_balance_tmpmask);#ifdef CONFIG_NO_HZ_COMMON static struct { -cpumask_var_t idle_cpus_mask; -atomic_t nr_cpus; +/* Isolate frequently updated fields in a cacheline to avoid false sharing issue. */ +atomic_t nr_cpus____cacheline_aligned ;int has_blocked;/* Idle CPUS has blocked load */ int needs_update;/* Newly idle CPUs need their next_balance collated */ unsigned long next_balance;/* in jiffy units */ unsigned long next_blocked;/* Next update of blocked load in jiffies */ -} nohz____cacheline_aligned ; +cpumask_var_t idle_cpus_mask____cacheline_aligned ; +} nohz;#endif /* CONFIG_NO_HZ_COMMON */
Signature · 1 line
2.47.3
Hi Wangyang,
Quoted 3 lines · Wangyang Guo
There are two potential false sharing issue in nohz struct:
1.idle_cpus_maskis a read-mostly field, but share the same cacheline with frequently updatednr_cpus.
Updates to idle_cpus_mask is not same cacheline. it is updated alongside nr_cpus.
with CPUMASK_OFFSTACK=y, idle_cpus_mask is a pointer to the actual mask. Updates to it happen in another cacheline.
with CPUMASK_OFFSTACK=n, idle_cpus_mask is on the stack and its length depends on NR_CPUS. typical value being 512/2048/8192 it can span a few cachelines. So updates to it likely in different cacheline compared to nr_cpus.
see https://lore.kernel.org/all/[email protected]/
Likely in your case, nr_cpus updates are the costly ones. Try below and see if it helps to fix your issue too.
https://lore.kernel.org/all/[email protected]/
I Should send out new version soon.
Quoted 3 lines
2. Data followed by nohz still share the same cacheline and has potential false sharing issue.
How does your patch handle this?
I don't see any other struct apart from nohz being changed.
Quoted 21 lines
This patch tries to resolve the above two problems by isolating the frequently updated fields in a single cacheline.
Reported-by Benjamin Lei <[email protected]>Reviewed-by Tim Chen <[email protected]>Reviewed-by Tianyou Li <[email protected]>Signed-off-by Wangyang Guo <[email protected]>--- kernel/sched/fair.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)kernel/sched/fair.c +0
-2@@ -7193,13 +7193,14 @@ static DEFINE_PER_CPU(cpumask_var_t, should_we_balance_tmpmask); #ifdef CONFIG_NO_HZ_COMMON static struct { - cpumask_var_t idle_cpus_mask; - atomic_t nr_cpus;
kernel/sched/fair.c static DEFINE_PER_CPU(cpumask_var_t, should_we_balance_tmpmask); · line 7204
Quoted 10 lines
kernel/sched/fair.c +4
-1+/* Isolate frequently updated fields in a cacheline to avoid false sharing issue. */ +atomic_t nr_cpus____cacheline_aligned ;int has_blocked;/* Idle CPUS has blocked load */ int needs_update;/* Newly idle CPUs need their next_balance collated */ unsigned long next_balance;/* in jiffy units */ unsigned long next_blocked;/* Next update of blocked load in jiffies */ -} nohz____cacheline_aligned ; +cpumask_var_t idle_cpus_mask____cacheline_aligned ; +} nohz;
This can cause a lot of space wastage.
for exp: powerpc has 128 byte cacheline.
Quoted 9 lines · Shrikanth Hegde
Hi Wangyang,
Quoted 3 lines · Wangyang Guo
There are two potential false sharing issue in nohz struct:
1.idle_cpus_maskis a read-mostly field, but share the same cacheline with frequently updatednr_cpus.Updates to
idle_cpus_maskis not same cacheline. it is updated alongsidenr_cpus.
Quoted 9 lines
with CPUMASK_OFFSTACK=y,
idle_cpus_maskis a pointer to the actual mask. Updates to it happen in another cacheline.with CPUMASK_OFFSTACK=n,
idle_cpus_maskis on the stack and its length depends onNR_CPUS. typical value being 512/2048/8192 it can span a few cachelines. So updates to it likely in different cacheline compared tonr_cpus.
This patch is mainly target for idle_cpus_mask as a pointer, which is default for many distro OS.
Quoted 6 lines
Likely in your case,
nr_cpusupdates are the costly ones. Try below and see if it helps to fix your issue too.
https://lore.kernel.org/all/[email protected]/
I Should send out new version soon.
Quoted 6 lines
Quoted 3 lines
2. Data followed by nohz still share the same cacheline and has potential false sharing issue.
How does your patch handle this?
I don't see any other struct apart from nohz being changed.
The data follow by nohz is implicit and determined by compiler. For example, this is the layout from /proc/kallsyms in my machine:ffffffff88600d40 b nohzffffffff88600d68 B arch_needs_tick_broadcast ffffffff88600d6c b __key.264ffffffff88600d6c b __key.265ffffffff88600d70 b dl_generationffffffff88600d78 b sched_clock_irqtime
What we can do is placing read-mostly idle_cpus_mask pointer in a new cacheline, so data followed by nohz would not be affected by nr_cpus.
Quoted 28 lines
Quoted 28 lines
This patch tries to resolve the above two problems by isolating the frequently updated fields in a single cacheline.
Reported-by Benjamin Lei <[email protected]>Reviewed-by Tim Chen <[email protected]>Reviewed-by Tianyou Li <[email protected]>Signed-off-by Wangyang Guo <[email protected]>---
kernel/sched/fair.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)kernel/sched/fair.c +1
-2@@ -7193,13 +7193,14 @@ static DEFINE_PER_CPU(cpumask_var_t, should_we_balance_tmpmask); #ifdef CONFIG_NO_HZ_COMMON static struct { - cpumask_var_t idle_cpus_mask; - atomic_t nr_cpus; + /* Isolate frequently updated fields in a cacheline to avoidfalse sharing issue. */
+atomic_tnr_cpus____cacheline_aligned;
int has_blocked; /* Idle CPUS has blocked load */
int needs_update; /* Newly idle CPUs need theirnext_balancecollated */ unsigned long next_balance; /* in jiffy units */
Quoted 9 lines
kernel/sched/fair.c static DEFINE_PER_CPU(cpumask_var_t, should_we_balance_tmpmask); · line 7203
Quoted 5 lines
unsigned long next_blocked; /* Next update of blocked load in jiffies */
kernel/sched/fair.c +2
-1-} nohz____cacheline_aligned ; + cpumask_var_t idle_cpus_mask ____cacheline_aligned; +} nohz;This can cause a lot of space wastage.
for exp: powerpc has 128 byte cacheline.
nohz is global, only one exists. The size inflating is minimal, less than 1 cacheline.
BR
Wangyang
Quoted 13 lines · Wangyang
Quoted 12 lines · Shrikanth Hegde
Hi Wangyang,
Quoted 3 lines · Wangyang Guo
There are two potential false sharing issue in nohz struct:
1.idle_cpus_maskis a read-mostly field, but share the same cacheline with frequently updatednr_cpus.Updates to
idle_cpus_maskis not same cacheline. it is updated alongsidenr_cpus.with CPUMASK_OFFSTACK=y,
idle_cpus_maskis a pointer to the actual mask. Updates to it happen in another cacheline.
Quoted 8 lines
Quoted 6 lines
with CPUMASK_OFFSTACK=n,
idle_cpus_maskis on the stack and its length depends onNR_CPUS. typical value being 512/2048/8192 it can span a few cachelines. So updates to it likely in different cacheline compared tonr_cpus.This patch is mainly target for
idle_cpus_maskas a pointer, which is default for many distro OS.
Not all archs.
Quoted 22 lines
Quoted 6 lines
Likely in your case,
nr_cpusupdates are the costly ones. Try below and see if it helps to fix your issue too.
https://lore.kernel.org/all/[email protected]/
I Should send out new version soon.Quoted 6 lines
Quoted 3 lines
2. Data followed by nohz still share the same cacheline and has potential false sharing issue.
How does your patch handle this?
I don't see any other struct apart from nohz being changed.The data follow by nohz is implicit and determined by compiler. For example, this is the layout from
/proc/kallsymsin my machine:ffffffff88600d40b nohzffffffff88600d68Barch_needs_tick_broadcastffffffff88600d6cb __key.264ffffffff88600d6cb __key.265ffffffff88600d70bdl_generationffffffff88600d78bsched_clock_irqtime
Quoted 2 lines
What we can do is placing read-mostly
idle_cpus_maskpointer in a new cacheline, so data followed by nohz would not be affected bynr_cpus.
That's a concern. If it is compiler dependent, then sometime it helps, sometime it wont.
It should done other way around rather than changing the nohz. If there is structure which has a lot of read/updates, it should go into its own cacheline rather.
i.e in your case sched_clock_irqtime should go into its own cacheline.
---
kernel/sched/cputime.c +1 -1
@@ -25,7 +25,7 @@*/ DEFINE_PER_CPU(struct irqtime, cpu_irqtime); -int sched_clock_irqtime; +int sched_clock_irqtime __cacheline_aligned;void enable_sched_clock_irqtime (void ) {
Quoted 22 lines · Shrikanth Hegde
Quoted 22 lines
Quoted 6 lines
Likely in your case,
nr_cpusupdates are the costly ones.
Try below and see if it helps to fix your issue too.
https://lore.kernel.org/all/20251201183146.74443-1- [email protected]/ I Should send out new version soon.Quoted 6 lines
Quoted 3 lines
2. Data followed by nohz still share the same cacheline and has potential false sharing issue.
How does your patch handle this?
I don't see any other struct apart from nohz being changed.The data follow by nohz is implicit and determined by compiler. For example, this is the layout from
/proc/kallsymsin my machine:ffffffff88600d40b nohzffffffff88600d68Barch_needs_tick_broadcastffffffff88600d6cb __key.264ffffffff88600d6cb __key.265ffffffff88600d70bdl_generationffffffff88600d78bsched_clock_irqtime
Quoted 4 lines
Quoted 2 lines
What we can do is placing read-mostly
idle_cpus_maskpointer in a new cacheline, so data followed by nohz would not be affected bynr_cpus.That's a concern. If it is compiler dependent, then sometime it helps, sometime it wont.
This patch wants to make it compiler independent. Sometimes it maybe sched_clock_irqtime, sometimes, it maybe baba_vars. Changing on nohz makes sure it would not affect others, no matter whatever data followed.
Quoted 5 lines
It should done other way around rather than changing the nohz. If there is structure which has a lot of read/updates, it should go into its own cacheline rather.
i.e in your case
sched_clock_irqtimeshould go into its own cacheline.
If that is preferred in kernel, I can resubmit the patch which only requires alignment on sched_clock_irqtime.
Quoted 8 lines
---
kernel/sched/cputime.c +0
-0
kernel/sched/cputime.c line 31
Quoted 8 lines
kernel/sched/cputime.c +1
-1@@ -25,7 +25,7 @@*/ DEFINE_PER_CPU(struct irqtime, cpu_irqtime); -int sched_clock_irqtime; +int sched_clock_irqtime __cacheline_aligned; void enable_sched_clock_irqtime(void) {
BR
Wangyang