Skip to content

Commit 8b82c18

Browse files
committed
Merge tag 'sched-urgent-2025-02-22' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull rseq fixes from Ingo Molnar: - Fix overly spread-out RSEQ concurrency ID allocation pattern that regressed certain workloads - Fix RSEQ registration syscall behavior on -EFAULT errors when CONFIG_DEBUG_RSEQ=y (This debug option is disabled on most distributions) * tag 'sched-urgent-2025-02-22' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: rseq: Fix rseq registration with CONFIG_DEBUG_RSEQ sched: Compact RSEQ concurrency IDs with reduced threads and affinity
2 parents 1ceffff + dc0a241 commit 8b82c18

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
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/rseq.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,6 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
507507
return -EINVAL;
508508
if (!access_ok(rseq, rseq_len))
509509
return -EFAULT;
510-
current->rseq = rseq;
511-
current->rseq_len = rseq_len;
512-
current->rseq_sig = sig;
513510
#ifdef CONFIG_DEBUG_RSEQ
514511
/*
515512
* Initialize the in-kernel rseq fields copy for validation of
@@ -521,6 +518,14 @@ SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
521518
get_user(rseq_kernel_fields(current)->mm_cid, &rseq->mm_cid))
522519
return -EFAULT;
523520
#endif
521+
/*
522+
* Activate the registration by setting the rseq area address, length
523+
* and signature in the task struct.
524+
*/
525+
current->rseq = rseq;
526+
current->rseq_len = rseq_len;
527+
current->rseq_sig = sig;
528+
524529
/*
525530
* If rseq was previously inactive, and has just been
526531
* registered, ensure the cpu_id_start and cpu_id fields

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)