Skip to content

Commit 5a5b45f

Browse files
compudjopsiff
authored andcommitted
sched: Compact RSEQ concurrency IDs with reduced threads and affinity
mainline inclusion from mainline-v6.14-rc4 category: bugfix 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 <mathieu.desnoyers@efficios.com> Signed-off-by: Gabriele Monaco <gmonaco@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Gabriele Monaco <gmonaco@redhat.com> Link: https://lkml.kernel.org/r/20250210153253.460471-2-gmonaco@redhat.com (cherry picked from commit 02d954c) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 10ff06e commit 5a5b45f

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

include/linux/mm_types.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,11 @@ struct mm_struct {
891891
*/
892892
unsigned int nr_cpus_allowed;
893893
/**
894-
* @max_nr_cid: Maximum number of concurrency IDs allocated.
894+
* @max_nr_cid: Maximum number of allowed concurrency
895+
* IDs allocated.
895896
*
896-
* Track the highest number of concurrency IDs allocated for the
897-
* mm.
897+
* Track the highest number of allowed concurrency IDs
898+
* allocated for the mm.
898899
*/
899900
atomic_t max_nr_cid;
900901
/**

kernel/sched/sched.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,19 +3646,38 @@ static inline int __mm_cid_try_get(struct task_struct *t, struct mm_struct *mm)
36463646
{
36473647
struct cpumask *cidmask = mm_cidmask(mm);
36483648
struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
3649-
int cid = __this_cpu_read(pcpu_cid->recent_cid);
3649+
int cid, max_nr_cid, allowed_max_nr_cid;
36503650

3651+
/*
3652+
* After shrinking the number of threads or reducing the number
3653+
* of allowed cpus, reduce the value of max_nr_cid so expansion
3654+
* of cid allocation will preserve cache locality if the number
3655+
* of threads or allowed cpus increase again.
3656+
*/
3657+
max_nr_cid = atomic_read(&mm->max_nr_cid);
3658+
while ((allowed_max_nr_cid = min_t(int, READ_ONCE(mm->nr_cpus_allowed),
3659+
atomic_read(&mm->mm_users))),
3660+
max_nr_cid > allowed_max_nr_cid) {
3661+
/* atomic_try_cmpxchg loads previous mm->max_nr_cid into max_nr_cid. */
3662+
if (atomic_try_cmpxchg(&mm->max_nr_cid, &max_nr_cid, allowed_max_nr_cid)) {
3663+
max_nr_cid = allowed_max_nr_cid;
3664+
break;
3665+
}
3666+
}
36513667
/* Try to re-use recent cid. This improves cache locality. */
3652-
if (!mm_cid_is_unset(cid) && !cpumask_test_and_set_cpu(cid, cidmask))
3668+
cid = __this_cpu_read(pcpu_cid->recent_cid);
3669+
if (!mm_cid_is_unset(cid) && cid < max_nr_cid &&
3670+
!cpumask_test_and_set_cpu(cid, cidmask))
36533671
return cid;
36543672
/*
36553673
* Expand cid allocation if the maximum number of concurrency
36563674
* IDs allocated (max_nr_cid) is below the number cpus allowed
36573675
* and number of threads. Expanding cid allocation as much as
36583676
* possible improves cache locality.
36593677
*/
3660-
cid = atomic_read(&mm->max_nr_cid);
3678+
cid = max_nr_cid;
36613679
while (cid < READ_ONCE(mm->nr_cpus_allowed) && cid < atomic_read(&mm->mm_users)) {
3680+
/* atomic_try_cmpxchg loads previous mm->max_nr_cid into cid. */
36623681
if (!atomic_try_cmpxchg(&mm->max_nr_cid, &cid, cid + 1))
36633682
continue;
36643683
if (!cpumask_test_and_set_cpu(cid, cidmask))

0 commit comments

Comments
 (0)