Skip to content

Commit 02d954c

Browse files
compudjPeter Zijlstra
authored andcommitted
sched: Compact RSEQ concurrency IDs with reduced threads and affinity
When a process reduces its number of threads or clears bits in its CPU affinity mask, the mm_cid allocation should eventually converge towards smaller values. However, the change introduced by: commit 7e019dc ("sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads") adds a per-mm/CPU recent_cid which is never unset unless a thread migrates. This is a tradeoff between: A) Preserving cache locality after a transition from many threads to few threads, or after reducing the hamming weight of the allowed CPU mask. B) Making the mm_cid upper bounds wrt nr threads and allowed CPU mask easy to document and understand. C) Allowing applications to eventually react to mm_cid compaction after reduction of the nr threads or allowed CPU mask, making the tracking of mm_cid compaction easier by shrinking it back towards 0 or not. D) Making sure applications that periodically reduce and then increase again the nr threads or allowed CPU mask still benefit from good cache locality with mm_cid. Introduce the following changes: * After shrinking the number of threads or reducing the number of allowed CPUs, reduce the value of max_nr_cid so expansion of CID allocation will preserve cache locality if the number of threads or allowed CPUs increase again. * Only re-use a recent_cid if it is within the max_nr_cid upper bound, else find the first available CID. Fixes: 7e019dc ("sched: Improve cache locality of RSEQ concurrency IDs for intermittent workloads") Signed-off-by: Mathieu Desnoyers <[email protected]> Signed-off-by: Gabriele Monaco <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Gabriele Monaco <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0ad2507 commit 02d954c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

include/linux/mm_types.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,11 @@ struct mm_struct {
875875
*/
876876
unsigned int nr_cpus_allowed;
877877
/**
878-
* @max_nr_cid: Maximum number of concurrency IDs allocated.
878+
* @max_nr_cid: Maximum number of allowed concurrency
879+
* IDs allocated.
879880
*
880-
* Track the highest number of concurrency IDs allocated for the
881-
* mm.
881+
* Track the highest number of allowed concurrency IDs
882+
* allocated for the mm.
882883
*/
883884
atomic_t max_nr_cid;
884885
/**

kernel/sched/sched.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,19 +3698,38 @@ static inline int __mm_cid_try_get(struct task_struct *t, struct mm_struct *mm)
36983698
{
36993699
struct cpumask *cidmask = mm_cidmask(mm);
37003700
struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3701-
int cid = __this_cpu_read(pcpu_cid->recent_cid);
3701+
int cid, max_nr_cid, allowed_max_nr_cid;
37023702

3703+
/*
3704+
* After shrinking the number of threads or reducing the number
3705+
* of allowed cpus, reduce the value of max_nr_cid so expansion
3706+
* of cid allocation will preserve cache locality if the number
3707+
* of threads or allowed cpus increase again.
3708+
*/
3709+
max_nr_cid = atomic_read(&mm->max_nr_cid);
3710+
while ((allowed_max_nr_cid = min_t(int, READ_ONCE(mm->nr_cpus_allowed),
3711+
atomic_read(&mm->mm_users))),
3712+
max_nr_cid > allowed_max_nr_cid) {
3713+
/* atomic_try_cmpxchg loads previous mm->max_nr_cid into max_nr_cid. */
3714+
if (atomic_try_cmpxchg(&mm->max_nr_cid, &max_nr_cid, allowed_max_nr_cid)) {
3715+
max_nr_cid = allowed_max_nr_cid;
3716+
break;
3717+
}
3718+
}
37033719
/* Try to re-use recent cid. This improves cache locality. */
3704-
if (!mm_cid_is_unset(cid) && !cpumask_test_and_set_cpu(cid, cidmask))
3720+
cid = __this_cpu_read(pcpu_cid->recent_cid);
3721+
if (!mm_cid_is_unset(cid) && cid < max_nr_cid &&
3722+
!cpumask_test_and_set_cpu(cid, cidmask))
37053723
return cid;
37063724
/*
37073725
* Expand cid allocation if the maximum number of concurrency
37083726
* IDs allocated (max_nr_cid) is below the number cpus allowed
37093727
* and number of threads. Expanding cid allocation as much as
37103728
* possible improves cache locality.
37113729
*/
3712-
cid = atomic_read(&mm->max_nr_cid);
3730+
cid = max_nr_cid;
37133731
while (cid < READ_ONCE(mm->nr_cpus_allowed) && cid < atomic_read(&mm->mm_users)) {
3732+
/* atomic_try_cmpxchg loads previous mm->max_nr_cid into cid. */
37143733
if (!atomic_try_cmpxchg(&mm->max_nr_cid, &cid, cid + 1))
37153734
continue;
37163735
if (!cpumask_test_and_set_cpu(cid, cidmask))

0 commit comments

Comments
 (0)