sched: Improve cache locality of RSEQ concurrency IDs
Subsystems (4)
v2 cover sched: Improve cache locality of RSEQ concurrency IDs
Intermittent workloads behaving in bursts spaced by more than 100ms on each CPU exhibit bad cache locality and degraded performance compared to purely per-cpu data indexing, because concurrency IDs are allocated over various CPUs and cores, therefore losing cache locality of the associated data.
This series addresses this shortcoming. I observed speedups up to 16.7x compared to plain mm_cid indexing in benchmarks.
It is based on tag v6.11.1.
This deprecates the prior "sched: NUMA-aware per-memory-map concurrency IDs" patch series with a simpler and more general approach.
Peter, can you pick it up through the tip tree ?
Thanks,
Mathieu
Mathieu Desnoyers (1):
sched: Improve cache locality of RSEQ concurrency IDs for intermittent
workloads
fs/exec.c | 2 +- include/linux/mm_types.h | 72 +++++++++++++++++++++++++++++++++++----- kernel/fork.c | 2 +- kernel/sched/core.c | 22 +++++++----- kernel/sched/sched.h | 47 ++++++++++++++++++-------- 5 files changed, 111 insertions(+), 34 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/1 sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads
commit 223baf9d17f25 ("sched: Fix performance regression introduced by mm_cid") introduced a per-mm/cpu current concurrency id (mm_cid), which keeps a reference to the concurrency id allocated for each CPU. This reference expires shortly after a 100ms delay.
These per-CPU references keep the per-mm-cid data cache-local in situations where threads are running at least once on each CPU within each 100ms window, thus keeping the per-cpu reference alive.
However, intermittent workloads behaving in bursts spaced by more than 100ms on each CPU exhibit bad cache locality and degraded performance compared to purely per-cpu data indexing, because concurrency IDs are allocated over various CPUs and cores, therefore losing cache locality of the associated data.
Introduce the following changes to improve per-mm-cid cache locality:
- Add a "recent_cid" field to the per-mm/cpu mm_cid structure to keep track of which mm_cid value was last used, and use it as a hint to attempt re-allocating the same concurrency ID the next time this mm/cpu needs to allocate a concurrency ID,
- Add a per-mm CPUs allowed mask, which keeps track of the union of CPUs allowed for all threads belonging to this mm. This cpumask is only set during the lifetime of the mm, never cleared, so it represents the union of all the CPUs allowed since the beginning of the mm lifetime. (note that the mm_cpumask() is really arch-specific and tailored to the TLB flush needs, and is thus _not_ a viable approach for this)
- Add a per-mm nr_cpus_allowed to keep track of the weight of the per-mm CPUs allowed mask (for fast access),
- Add a per-mm nr_cids_used to keep track of the highest concurrency ID allocated for the mm. This is used for expanding the concurrency ID allocation within the upper bound defined by:
min(mm->nr_cpus_allowed, mm->mm_users)
When the next unused CID value reaches this threshold, stop trying to expand the cid allocation and use the first available cid value instead.
Spreading allocation to use all the cid values within the range
[ 0, min(mm->nr_cpus_allowed, mm->mm_users) - 1 ]
improves cache locality while preserving mm_cid compactness within the expected user limits.
- In __mm_cid_try_get, only return cid values within the range [ 0, mm->nr_cpus_allowed ] rather than [ 0, nr_cpu_ids ]. This prevents allocating cids above the number of allowed cpus in rare scenarios where cid allocation races with a concurrent remote-clear of the per-mm/cpu cid. This improvement is made possible by the addition of the per-mm CPUs allowed mask.
- In sched_mm_cid_migrate_to, use mm->nr_cpus_allowed rather than t->nr_cpus_allowed. This criterion was really meant to compare the number of mm->mm_users to the number of CPUs allowed for the entire mm. Therefore, the prior comparison worked fine when all threads shared the same CPUs allowed mask, but not so much in scenarios where those threads have different masks (e.g. each thread pinned to a single CPU). This improvement is made possible by the addition of the per-mm CPUs allowed mask.
* Benchmarks
Each thread increments 16kB worth of 8-bit integers in bursts, with a configurable delay between each thread's execution. Each thread run one after the other (no threads run concurrently). The order of thread execution in the sequence is random. The thread execution sequence begins again after all threads have executed. The 16kB areas are allocated with rseq_mempool and indexed by either cpu_id, mm_cid (not cache-local), or cache-local mm_cid. Each thread is pinned to its own core.
Testing configurations:
8-core/1-L3: Use 8 cores within a single L3 24-core/24-L3: Use 24 cores, 1 core per L3 192-core/24-L3: Use 192 cores (all cores in the system) 384-thread/24-L3: Use 384 HW threads (all HW threads in the system)
Intermittent workload delays between threads: 200ms, 10ms.
Hardware:
CPU(s): 384
On-line CPU(s) list: 0-383
Vendor ID: AuthenticAMD
Model name: AMD EPYC 9654 96-Core Processor
Thread(s) per core: 2
Core(s) per socket: 96
Socket(s): 2
Caches (sum of all):
L1d: 6 MiB (192 instances)
L1i: 6 MiB (192 instances)
L2: 192 MiB (192 instances)
L3: 768 MiB (24 instances)Each result is an average of 5 test runs. The cache-local speedup is calculated as: (cache-local mm_cid) / (mm_cid).
Intermittent workload delay: 200ms
per-cpu mm_cid cache-local mm_cid cache-local speedup
(ns) (ns) (ns)
8-core/1-L3 1374 19289 1336 14.4x 24-core/24-L3 2423 26721 1594 16.7x 192-core/24-L3 2291 15826 2153 7.3x 384-thread/24-L3 1874 13234 1907 6.9x
Intermittent workload delay: 10ms
per-cpu mm_cid cache-local mm_cid cache-local speedup
(ns) (ns) (ns)
8-core/1-L3 662 756 686 1.1x 24-core/24-L3 1378 3648 1035 3.5x 192-core/24-L3 1439 10833 1482 7.3x 384-thread/24-L3 1503 10570 1556 6.8x
[ This deprecates the prior "sched: NUMA-aware per-memory-map concurrency IDs"
patch series with a simpler and more general approach. ]
---
Changes since v0:
- On migration, do not move the source cid to the destination cpu if the destination cpu has a recent cid value set.
Changes since v2:
- Rebase on v6.11.1.
--- fs/exec.c | 2 +- include/linux/mm_types.h | 72 +++++++++++++++++++++++++++++++++++----- kernel/fork.c | 2 +- kernel/sched/core.c | 22 +++++++----- kernel/sched/sched.h | 47 ++++++++++++++++++-------- 5 files changed, 111 insertions(+), 34 deletions(-)
fs/exec.c +1 -1
@@ -1065,7 +1065,7 @@ static int exec_mmap(struct mm_struct *mm) active_mm = tsk->active_mm; tsk->active_mm = mm; tsk->mm = mm; -mm_init_cid (mm); +mm_init_cid (mm, tsk);/* * This prevents preemption while active_mm is being loaded and * it and mm are being updated, which could cause problems for
include/linux/mm_types.h +63 -9
126 lines not on this page · raw message
kernel/fork.c +1 -1
@@ -1296,7 +1296,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,if (init_new_context (p, mm)) goto fail_nocontext; -if (mm_alloc_cid (mm)) +if (mm_alloc_cid (mm, p)) goto fail_cid; if (percpu_counter_init_many(mm->rss_stat, 0, GFP_KERNEL_ACCOUNT,
kernel/sched/core.c +13 -9
@@ -2602,6 +2602,7 @@ __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)put_prev_task (rq, p); p->sched_class->set_cpus_allowed (p, ctx); +mm_set_cpus_allowed (p->mm, ctx->new_mask);if (queued)enqueue_task (rq, p,ENQUEUE_RESTORE |ENQUEUE_NOCLOCK );
@@ -10025,6 +10026,7 @@ int __sched_mm_cid_migrate_from_try_steal_cid(struct rq *src_rq,*/ if (!try_cmpxchg (&src_pcpu_cid->cid, &lazy_cid,MM_CID_UNSET ))return -1 ; +WRITE_ONCE (src_pcpu_cid->recent_cid,MM_CID_UNSET );return src_cid; }
@@ -10037,7 +10039,8 @@ void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) {struct mm_cid *src_pcpu_cid, *dst_pcpu_cid;struct mm_struct *mm = t->mm; -int src_cid, dst_cid, src_cpu; +int src_cid, src_cpu; +bool dst_cid_is_set;struct rq *src_rq;lockdep_assert_rq_held (dst_rq);
@@ -10054,9 +10057,9 @@ void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t)* allocation closest to 0 in cases where few threads migrate around * many CPUs. * -* If destination cid is already set, we may have to just clear -* the src cid to ensure compactness in frequent migrations -* scenarios. +* If destination cid or recent cid is already set, we may have +* to just clear the src cid to ensure compactness in frequent +* migrations scenarios. * * It is not useful to clear the src cid when the number of threads is * greater or equal to the number of allowed CPUs, because user-space
@@ -10064,9 +10067,9 @@ void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t)* allowed CPUs. */ dst_pcpu_cid =per_cpu_ptr (mm->pcpu_cid,cpu_of (dst_rq)); - dst_cid =READ_ONCE (dst_pcpu_cid->cid); -if (!mm_cid_is_unset (dst_cid) && -atomic_read (&mm->mm_users) >= t->nr_cpus_allowed) + dst_cid_is_set = !mm_cid_is_unset (READ_ONCE (dst_pcpu_cid->cid)) || + !mm_cid_is_unset (READ_ONCE (dst_pcpu_cid->recent_cid)); +if (dst_cid_is_set &&atomic_read (&mm->mm_users) >=atomic_read (&mm->nr_cpus_allowed))return ; src_pcpu_cid =per_cpu_ptr (mm->pcpu_cid, src_cpu); src_rq =cpu_rq (src_cpu);
@@ -10077,13 +10080,14 @@ void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) src_cid);if (src_cid ==-1 )return ; -if (!mm_cid_is_unset (dst_cid)) { +if (dst_cid_is_set) {__mm_cid_put (mm, src_cid);return ; }/* Move src_cid to dst cpu. */ mm_cid_snapshot_time (dst_rq, mm);WRITE_ONCE (dst_pcpu_cid->cid, src_cid); +WRITE_ONCE (dst_pcpu_cid->recent_cid, src_cid); } static void sched_mm_cid_remote_clear(struct mm_struct *mm, struct mm_cid *pcpu_cid,
@@ -10320,7 +10324,7 @@ void sched_mm_cid_after_execve(struct task_struct *t)* Matches barrier in sched_mm_cid_remote_clear_old(). */ smp_mb (); - t->last_mm_cid = t->mm_cid =mm_cid_get (rq, mm); + t->last_mm_cid = t->mm_cid =mm_cid_get (rq, t, mm); }rseq_set_notify_resume (t); }
kernel/sched/sched.h +33 -14
106 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v2 1/1] sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads
Quoted 141 lines · Mathieu Desnoyers
commit
223baf9d17f25("sched: Fix performance regression introduced bymm_cid") introduced a per-mm/cpu current concurrency id (mm_cid), which keeps a reference to the concurrency id allocated for each CPU. This reference expires shortly after a 100ms delay.These per-CPU references keep the per-mm-cid data cache-local in situations where threads are running at least once on each CPU within each 100ms window, thus keeping the per-cpu reference alive.
However, intermittent workloads behaving in bursts spaced by more than 100ms on each CPU exhibit bad cache locality and degraded performance compared to purely per-cpu data indexing, because concurrency IDs are allocated over various CPUs and cores, therefore losing cache locality of the associated data.
Introduce the following changes to improve per-mm-cid cache locality:
- Add a "
recent_cid" field to the per-mm/cpumm_cidstructure to keep track of whichmm_cidvalue was last used, and use it as a hint to attempt re-allocating the same concurrency ID the next time this mm/cpu needs to allocate a concurrency ID,- Add a per-mm CPUs allowed mask, which keeps track of the union of CPUs allowed for all threads belonging to this mm. This cpumask is only set during the lifetime of the mm, never cleared, so it represents the union of all the CPUs allowed since the beginning of the mm lifetime. (note that the
mm_cpumask()is really arch-specific and tailored to the TLB flush needs, and is thus _not_ a viable approach for this)- Add a per-mm
nr_cpus_allowedto keep track of the weight of the per-mm CPUs allowed mask (for fast access),- Add a per-mm
nr_cids_usedto keep track of the highest concurrency ID allocated for the mm. This is used for expanding the concurrency ID allocation within the upper bound defined by:min(mm->nr_cpus_allowed, mm->mm_users)When the next unused CID value reaches this threshold, stop trying to expand the cid allocation and use the first available cid value instead.
Spreading allocation to use all the cid values within the range
[ 0,
min(mm->nr_cpus_allowed, mm->mm_users)- 1 ]improves cache locality while preserving
mm_cidcompactness within the expected user limits.- In
__mm_cid_try_get, only return cid values within the range [ 0,mm->nr_cpus_allowed] rather than [ 0,nr_cpu_ids]. This prevents allocating cids above the number of allowed cpus in rare scenarios where cid allocation races with a concurrent remote-clear of the per-mm/cpu cid. This improvement is made possible by the addition of the per-mm CPUs allowed mask.- In
sched_mm_cid_migrate_to, usemm->nr_cpus_allowedrather thant->nr_cpus_allowed. This criterion was really meant to compare the number ofmm->mm_usersto the number of CPUs allowed for the entire mm. Therefore, the prior comparison worked fine when all threads shared the same CPUs allowed mask, but not so much in scenarios where those threads have different masks (e.g. each thread pinned to a single CPU). This improvement is made possible by the addition of the per-mm CPUs allowed mask.* Benchmarks
Each thread increments 16kB worth of 8-bit integers in bursts, with a configurable delay between each thread's execution. Each thread run one after the other (no threads run concurrently). The order of thread execution in the sequence is random. The thread execution sequence begins again after all threads have executed. The 16kB areas are allocated with
rseq_mempooland indexed by eithercpu_id,mm_cid(not cache-local), or cache-localmm_cid. Each thread is pinned to its own core.Testing configurations:
8-core/1-L3: Use 8 cores within a single L3 24-core/24-L3: Use 24 cores, 1 core per L3 192-core/24-L3: Use 192 cores (all cores in the system) 384-thread/24-L3: Use 384 HW threads (all HW threads in the system)Intermittent workload delays between threads: 200ms, 10ms.
Hardware:
CPU(s): 384 On-line CPU(s) list: 0-383 Vendor ID: AuthenticAMD Model name: AMD EPYC 9654 96-Core Processor Thread(s) per core: 2 Core(s) per socket: 96 Socket(s): 2 Caches (sum of all): L1d: 6 MiB (192 instances) L1i: 6 MiB (192 instances) L2: 192 MiB (192 instances) L3: 768 MiB (24 instances)Each result is an average of 5 test runs. The cache-local speedup is calculated as: (cache-local
mm_cid) / (mm_cid).Intermittent workload delay: 200ms
per-cpu
mm_cidcache-localmm_cidcache-local speedup
(ns) (ns) (ns)8-core/1-L3 1374 19289 1336 14.4x 24-core/24-L3 2423 26721 1594 16.7x 192-core/24-L3 2291 15826 2153 7.3x 384-thread/24-L3 1874 13234 1907 6.9xIntermittent workload delay: 10ms
per-cpu
mm_cidcache-localmm_cidcache-local speedup
(ns) (ns) (ns)8-core/1-L3 662 756 686 1.1x 24-core/24-L3 1378 3648 1035 3.5x 192-core/24-L3 1439 10833 1482 7.3x 384-thread/24-L3 1503 10570 1556 6.8x[ This deprecates the prior "sched: NUMA-aware per-memory-map concurrency IDs"
patch series with a simpler and more general approach. ]Signed-off-by Mathieu Desnoyers <[email protected]>Acked-by Marco Elver <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Valentin Schneider <[email protected]>Cc Mel Gorman <[email protected]>Cc Steven Rostedt <[email protected]>Cc Vincent Guittot <[email protected]>Cc Dietmar Eggemann <[email protected]>Cc Ben Segall <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Marco Elver <[email protected]>Cc Yury Norov <[email protected]>Cc Rasmus Villemoes <[email protected]>
Quoted 7 lines
---
Changes since v0:
- On migration, do not move the source cid to the destination cpu if the destination cpu has a recent cid value set.Changes since v2:
- Rebase on v6.11.1.
I think the versioning and changelog got confused. I see the changes from [1] which was already v2 are included in this one.
[1] https://lore.kernel.org/all/[email protected]/T/#u
In any case, I'll reiterate my Ack as this looks like an improvement for the common case.
Thanks,
-- Marco
Re: [PATCH v2 1/1] sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads
Quoted 148 lines · Marco Elver
Quoted 146 lines · Mathieu Desnoyers
commit
223baf9d17f25("sched: Fix performance regression introduced bymm_cid") introduced a per-mm/cpu current concurrency id (mm_cid), which keeps a reference to the concurrency id allocated for each CPU. This reference expires shortly after a 100ms delay.These per-CPU references keep the per-mm-cid data cache-local in situations where threads are running at least once on each CPU within each 100ms window, thus keeping the per-cpu reference alive.
However, intermittent workloads behaving in bursts spaced by more than 100ms on each CPU exhibit bad cache locality and degraded performance compared to purely per-cpu data indexing, because concurrency IDs are allocated over various CPUs and cores, therefore losing cache locality of the associated data.
Introduce the following changes to improve per-mm-cid cache locality:
- Add a "
recent_cid" field to the per-mm/cpumm_cidstructure to keep track of whichmm_cidvalue was last used, and use it as a hint to attempt re-allocating the same concurrency ID the next time this mm/cpu needs to allocate a concurrency ID,- Add a per-mm CPUs allowed mask, which keeps track of the union of CPUs allowed for all threads belonging to this mm. This cpumask is only set during the lifetime of the mm, never cleared, so it represents the union of all the CPUs allowed since the beginning of the mm lifetime. (note that the
mm_cpumask()is really arch-specific and tailored to the TLB flush needs, and is thus _not_ a viable approach for this)- Add a per-mm
nr_cpus_allowedto keep track of the weight of the per-mm CPUs allowed mask (for fast access),- Add a per-mm
nr_cids_usedto keep track of the highest concurrency ID allocated for the mm. This is used for expanding the concurrency ID allocation within the upper bound defined by:min(mm->nr_cpus_allowed, mm->mm_users)When the next unused CID value reaches this threshold, stop trying to expand the cid allocation and use the first available cid value instead.
Spreading allocation to use all the cid values within the range
[ 0, min(mm->nr_cpus_allowed, mm->mm_users) - 1 ]improves cache locality while preserving
mm_cidcompactness within the expected user limits.- In
__mm_cid_try_get, only return cid values within the range [ 0,mm->nr_cpus_allowed] rather than [ 0,nr_cpu_ids]. This prevents allocating cids above the number of allowed cpus in rare scenarios where cid allocation races with a concurrent remote-clear of the per-mm/cpu cid. This improvement is made possible by the addition of the per-mm CPUs allowed mask.- In
sched_mm_cid_migrate_to, usemm->nr_cpus_allowedrather thant->nr_cpus_allowed. This criterion was really meant to compare the number ofmm->mm_usersto the number of CPUs allowed for the entire mm. Therefore, the prior comparison worked fine when all threads shared the same CPUs allowed mask, but not so much in scenarios where those threads have different masks (e.g. each thread pinned to a single CPU). This improvement is made possible by the addition of the per-mm CPUs allowed mask.* Benchmarks
Each thread increments 16kB worth of 8-bit integers in bursts, with a configurable delay between each thread's execution. Each thread run one after the other (no threads run concurrently). The order of thread execution in the sequence is random. The thread execution sequence begins again after all threads have executed. The 16kB areas are allocated with
rseq_mempooland indexed by eithercpu_id,mm_cid(not cache-local), or cache-localmm_cid. Each thread is pinned to its own core.Testing configurations:
8-core/1-L3: Use 8 cores within a single L3 24-core/24-L3: Use 24 cores, 1 core per L3 192-core/24-L3: Use 192 cores (all cores in the system) 384-thread/24-L3: Use 384 HW threads (all HW threads in the system)Intermittent workload delays between threads: 200ms, 10ms.
Hardware:
CPU(s): 384
On-line CPU(s) list: 0-383
Vendor ID: AuthenticAMDModel name: AMD EPYC 9654 96-Core Processor Thread(s) per core: 2 Core(s) per socket: 96 Socket(s): 2 Caches (sum of all): L1d: 6 MiB (192 instances) L1i: 6 MiB (192 instances) L2: 192 MiB (192 instances) L3: 768 MiB (24 instances)Each result is an average of 5 test runs. The cache-local speedup is calculated as: (cache-local
mm_cid) / (mm_cid).Intermittent workload delay: 200ms
per-cpu
mm_cidcache-localmm_cidcache-local speedup
(ns) (ns) (ns)8-core/1-L3 1374 19289 1336 14.4x 24-core/24-L3 2423 26721 1594 16.7x 192-core/24-L3 2291 15826 2153 7.3x 384-thread/24-L3 1874 13234 1907 6.9xIntermittent workload delay: 10ms
per-cpu
mm_cidcache-localmm_cidcache-local speedup
(ns) (ns) (ns)8-core/1-L3 662 756 686 1.1x 24-core/24-L3 1378 3648 1035 3.5x 192-core/24-L3 1439 10833 1482 7.3x 384-thread/24-L3 1503 10570 1556 6.8x[ This deprecates the prior "sched: NUMA-aware per-memory-map concurrency IDs"
patch series with a simpler and more general approach. ]Signed-off-by Mathieu Desnoyers <[email protected]>Acked-by Marco Elver <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Valentin Schneider <[email protected]>Cc Mel Gorman <[email protected]>Cc Steven Rostedt <[email protected]>Cc Vincent Guittot <[email protected]>Cc Dietmar Eggemann <[email protected]>Cc Ben Segall <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Marco Elver <[email protected]>Cc Yury Norov <[email protected]>Cc Rasmus Villemoes <[email protected]>---
Changes since v0:
- On migration, do not move the source cid to the destination cpu if the destination cpu has a recent cid value set.
Quoted 7 lines
Quoted 2 lines
Changes since v2:
- Rebase on v6.11.1.I think the versioning and changelog got confused. I see the changes from [1] which was already v2 are included in this one.
Which means I should have tagged this series [PATCH v3]. Sorry about that.
Quoted 5 lines
In any case, I'll reiterate my Ack as this looks like an improvement for the common case.
Acked-by Marco Elver <[email protected]>
Thanks!
Peter, should I re-send as is with a v3 tag, or is it OK for merge ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com