Skip to content

Commit 9a723ed

Browse files
committed
sched/mmcid: Provide new scheduler CID mechanism
The MM CID management has two fundamental requirements: 1) It has to guarantee that at no given point in time the same CID is used by concurrent tasks in userspace. 2) The CID space must not exceed the number of possible CPUs in a system. While most allocators (glibc, tcmalloc, jemalloc) do not care about that, there seems to be at least some LTTng library depending on it. The CID space compaction itself is not a functional correctness requirement, it is only a useful optimization mechanism to reduce the memory foot print in unused user space pools. The optimal CID space is: min(nr_tasks, nr_cpus_allowed); Where @nr_tasks is the number of actual user space threads associated to the mm and @nr_cpus_allowed is the superset of all task affinities. It is growth only as it would be insane to take a racy snapshot of all task affinities when the affinity of one task changes just do redo it 2 milliseconds later when the next task changes it's affinity. That means that as long as the number of tasks is lower or equal than the number of CPUs allowed, each task owns a CID. If the number of tasks exceeds the number of CPUs allowed it switches to per CPU mode, where the CPUs own the CIDs and the tasks borrow them as long as they are scheduled in. For transition periods CIDs can go beyond the optimal space as long as they don't go beyond the number of possible CPUs. The current upstream implementation adds overhead into task migration to keep the CID with the task. It also has to do the CID space consolidation work from a task work in the exit to user space path. As that work is assigned to a random task related to a MM this can inflict unwanted exit latencies. Implement the context switch parts of a strict ownership mechanism to address this. This removes most of the work from the task which schedules out. Only during transitioning from per CPU to per task ownership it is required to drop the CID when leaving the CPU to prevent CID space exhaustion. Other than that scheduling out is just a single check and branch. The task which schedules in has to check whether: 1) The ownership mode changed 2) The CID is within the optimal CID space In stable situations this results in zero work. The only short disruption is when ownership mode changes or when the associated CID is not in the optimal CID space. The latter only happens when tasks exit and therefore the optimal CID space shrinks. That mechanism is strictly optimized for the common case where no change happens. The only case where it actually causes a temporary one time spike is on mode changes when and only when a lot of tasks related to a MM schedule exactly at the same time and have eventually to compete on allocating a CID from the bitmap. In the sysbench test case which triggered the spinlock contention in the initial CID code, __schedule() drops significantly in perf top on a 128 Core (256 threads) machine when running sysbench with 255 threads, which fits into the task mode limit of 256 together with the parent thread: Upstream rseq/perf branch +CID rework 0.42% 0.37% 0.32% [k] __schedule Increasing the number of threads to 256, which puts the test process into per CPU mode looks about the same. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Link: https://patch.msgid.link/20251119172550.023984859@linutronix.de
1 parent 23343b6 commit 9a723ed

File tree

4 files changed

+168
-10
lines changed

4 files changed

+168
-10
lines changed

include/linux/rseq.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ static __always_inline void rseq_sched_switch_event(struct task_struct *t)
7373
}
7474

7575
/*
76-
* Invoked from __set_task_cpu() when a task migrates to enforce an IDs
77-
* update.
76+
* Invoked from __set_task_cpu() when a task migrates or from
77+
* mm_cid_schedin() when the CID changes to enforce an IDs update.
7878
*
7979
* This does not raise TIF_NOTIFY_RESUME as that happens in
8080
* rseq_sched_switch_event().
8181
*/
82-
static __always_inline void rseq_sched_set_task_cpu(struct task_struct *t, unsigned int cpu)
82+
static __always_inline void rseq_sched_set_ids_changed(struct task_struct *t)
8383
{
8484
t->rseq.event.ids_changed = true;
8585
}
@@ -168,7 +168,7 @@ static inline void rseq_fork(struct task_struct *t, u64 clone_flags)
168168
static inline void rseq_handle_slowpath(struct pt_regs *regs) { }
169169
static inline void rseq_signal_deliver(struct ksignal *ksig, struct pt_regs *regs) { }
170170
static inline void rseq_sched_switch_event(struct task_struct *t) { }
171-
static inline void rseq_sched_set_task_cpu(struct task_struct *t, unsigned int cpu) { }
171+
static inline void rseq_sched_set_ids_changed(struct task_struct *t) { }
172172
static inline void rseq_sched_set_task_mm_cid(struct task_struct *t, unsigned int cid) { }
173173
static inline void rseq_force_update(void) { }
174174
static inline void rseq_virt_userspace_exit(void) { }

include/linux/rseq_types.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,31 @@ struct mm_cid_pcpu {
119119
/**
120120
* struct mm_mm_cid - Storage for per MM CID data
121121
* @pcpu: Per CPU storage for CIDs associated to a CPU
122+
* @percpu: Set, when CIDs are in per CPU mode
123+
* @transit: Set to MM_CID_TRANSIT during a mode change transition phase
122124
* @max_cids: The exclusive maximum CID value for allocation and convergence
125+
* @lock: Spinlock to protect all fields except @pcpu. It also protects
126+
* the MM cid cpumask and the MM cidmask bitmap.
127+
* @mutex: Mutex to serialize forks and exits related to this mm
123128
* @nr_cpus_allowed: The number of CPUs in the per MM allowed CPUs map. The map
124129
* is growth only.
125130
* @users: The number of tasks sharing this MM. Separate from mm::mm_users
126131
* as that is modified by mmget()/mm_put() by other entities which
127132
* do not actually share the MM.
128-
* @lock: Spinlock to protect all fields except @pcpu. It also protects
129-
* the MM cid cpumask and the MM cidmask bitmap.
130-
* @mutex: Mutex to serialize forks and exits related to this mm
131133
*/
132134
struct mm_mm_cid {
135+
/* Hotpath read mostly members */
133136
struct mm_cid_pcpu __percpu *pcpu;
137+
unsigned int percpu;
138+
unsigned int transit;
134139
unsigned int max_cids;
135-
unsigned int nr_cpus_allowed;
136-
unsigned int users;
140+
137141
raw_spinlock_t lock;
138142
struct mutex mutex;
143+
144+
/* Low frequency modified */
145+
unsigned int nr_cpus_allowed;
146+
unsigned int users;
139147
}____cacheline_aligned_in_smp;
140148
#else /* CONFIG_SCHED_MM_CID */
141149
struct mm_mm_cid { };

kernel/sched/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10495,6 +10495,8 @@ void mm_init_cid(struct mm_struct *mm, struct task_struct *p)
1049510495
per_cpu_ptr(pcpu, cpu)->cid = MM_CID_UNSET;
1049610496

1049710497
mm->mm_cid.max_cids = 0;
10498+
mm->mm_cid.percpu = 0;
10499+
mm->mm_cid.transit = 0;
1049810500
mm->mm_cid.nr_cpus_allowed = p->nr_cpus_allowed;
1049910501
mm->mm_cid.users = 0;
1050010502
raw_spin_lock_init(&mm->mm_cid.lock);

kernel/sched/sched.h

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
22092209
smp_wmb();
22102210
WRITE_ONCE(task_thread_info(p)->cpu, cpu);
22112211
p->wake_cpu = cpu;
2212-
rseq_sched_set_task_cpu(p, cpu);
2212+
rseq_sched_set_ids_changed(p);
22132213
#endif /* CONFIG_SMP */
22142214
}
22152215

@@ -3598,6 +3598,153 @@ static __always_inline void mm_drop_cid_on_cpu(struct mm_struct *mm, struct mm_c
35983598
mm_drop_cid(mm, pcp->cid);
35993599
}
36003600

3601+
static inline unsigned int __mm_get_cid(struct mm_struct *mm, unsigned int max_cids)
3602+
{
3603+
unsigned int cid = find_first_zero_bit(mm_cidmask(mm), max_cids);
3604+
3605+
if (cid >= max_cids)
3606+
return MM_CID_UNSET;
3607+
if (test_and_set_bit(cid, mm_cidmask(mm)))
3608+
return MM_CID_UNSET;
3609+
return cid;
3610+
}
3611+
3612+
static inline unsigned int mm_get_cid(struct mm_struct *mm)
3613+
{
3614+
unsigned int cid = __mm_get_cid(mm, READ_ONCE(mm->mm_cid.max_cids));
3615+
3616+
while (cid == MM_CID_UNSET) {
3617+
cpu_relax();
3618+
cid = __mm_get_cid(mm, num_possible_cpus());
3619+
}
3620+
return cid;
3621+
}
3622+
3623+
static inline unsigned int mm_cid_converge(struct mm_struct *mm, unsigned int orig_cid,
3624+
unsigned int max_cids)
3625+
{
3626+
unsigned int new_cid, cid = cpu_cid_to_cid(orig_cid);
3627+
3628+
/* Is it in the optimal CID space? */
3629+
if (likely(cid < max_cids))
3630+
return orig_cid;
3631+
3632+
/* Try to find one in the optimal space. Otherwise keep the provided. */
3633+
new_cid = __mm_get_cid(mm, max_cids);
3634+
if (new_cid != MM_CID_UNSET) {
3635+
mm_drop_cid(mm, cid);
3636+
/* Preserve the ONCPU mode of the original CID */
3637+
return new_cid | (orig_cid & MM_CID_ONCPU);
3638+
}
3639+
return orig_cid;
3640+
}
3641+
3642+
static __always_inline void mm_cid_update_task_cid(struct task_struct *t, unsigned int cid)
3643+
{
3644+
if (t->mm_cid.cid != cid) {
3645+
t->mm_cid.cid = cid;
3646+
rseq_sched_set_ids_changed(t);
3647+
}
3648+
}
3649+
3650+
static __always_inline void mm_cid_update_pcpu_cid(struct mm_struct *mm, unsigned int cid)
3651+
{
3652+
__this_cpu_write(mm->mm_cid.pcpu->cid, cid);
3653+
}
3654+
3655+
static __always_inline void mm_cid_from_cpu(struct task_struct *t, unsigned int cpu_cid)
3656+
{
3657+
unsigned int max_cids, tcid = t->mm_cid.cid;
3658+
struct mm_struct *mm = t->mm;
3659+
3660+
max_cids = READ_ONCE(mm->mm_cid.max_cids);
3661+
/* Optimize for the common case where both have the ONCPU bit set */
3662+
if (likely(cid_on_cpu(cpu_cid & tcid))) {
3663+
if (likely(cpu_cid_to_cid(cpu_cid) < max_cids)) {
3664+
mm_cid_update_task_cid(t, cpu_cid);
3665+
return;
3666+
}
3667+
/* Try to converge into the optimal CID space */
3668+
cpu_cid = mm_cid_converge(mm, cpu_cid, max_cids);
3669+
} else {
3670+
/* Hand over or drop the task owned CID */
3671+
if (cid_on_task(tcid)) {
3672+
if (cid_on_cpu(cpu_cid))
3673+
mm_unset_cid_on_task(t);
3674+
else
3675+
cpu_cid = cid_to_cpu_cid(tcid);
3676+
}
3677+
/* Still nothing, allocate a new one */
3678+
if (!cid_on_cpu(cpu_cid))
3679+
cpu_cid = cid_to_cpu_cid(mm_get_cid(mm));
3680+
}
3681+
mm_cid_update_pcpu_cid(mm, cpu_cid);
3682+
mm_cid_update_task_cid(t, cpu_cid);
3683+
}
3684+
3685+
static __always_inline void mm_cid_from_task(struct task_struct *t, unsigned int cpu_cid)
3686+
{
3687+
unsigned int max_cids, tcid = t->mm_cid.cid;
3688+
struct mm_struct *mm = t->mm;
3689+
3690+
max_cids = READ_ONCE(mm->mm_cid.max_cids);
3691+
/* Optimize for the common case, where both have the ONCPU bit clear */
3692+
if (likely(cid_on_task(tcid | cpu_cid))) {
3693+
if (likely(tcid < max_cids)) {
3694+
mm_cid_update_pcpu_cid(mm, tcid);
3695+
return;
3696+
}
3697+
/* Try to converge into the optimal CID space */
3698+
tcid = mm_cid_converge(mm, tcid, max_cids);
3699+
} else {
3700+
/* Hand over or drop the CPU owned CID */
3701+
if (cid_on_cpu(cpu_cid)) {
3702+
if (cid_on_task(tcid))
3703+
mm_drop_cid_on_cpu(mm, this_cpu_ptr(mm->mm_cid.pcpu));
3704+
else
3705+
tcid = cpu_cid_to_cid(cpu_cid);
3706+
}
3707+
/* Still nothing, allocate a new one */
3708+
if (!cid_on_task(tcid))
3709+
tcid = mm_get_cid(mm);
3710+
/* Set the transition mode flag if required */
3711+
tcid |= READ_ONCE(mm->mm_cid.transit);
3712+
}
3713+
mm_cid_update_pcpu_cid(mm, tcid);
3714+
mm_cid_update_task_cid(t, tcid);
3715+
}
3716+
3717+
static __always_inline void mm_cid_schedin(struct task_struct *next)
3718+
{
3719+
struct mm_struct *mm = next->mm;
3720+
unsigned int cpu_cid;
3721+
3722+
if (!next->mm_cid.active)
3723+
return;
3724+
3725+
cpu_cid = __this_cpu_read(mm->mm_cid.pcpu->cid);
3726+
if (likely(!READ_ONCE(mm->mm_cid.percpu)))
3727+
mm_cid_from_task(next, cpu_cid);
3728+
else
3729+
mm_cid_from_cpu(next, cpu_cid);
3730+
}
3731+
3732+
static __always_inline void mm_cid_schedout(struct task_struct *prev)
3733+
{
3734+
/* During mode transitions CIDs are temporary and need to be dropped */
3735+
if (likely(!cid_in_transit(prev->mm_cid.cid)))
3736+
return;
3737+
3738+
mm_drop_cid(prev->mm, cid_from_transit_cid(prev->mm_cid.cid));
3739+
prev->mm_cid.cid = MM_CID_UNSET;
3740+
}
3741+
3742+
static inline void mm_cid_switch_to(struct task_struct *prev, struct task_struct *next)
3743+
{
3744+
mm_cid_schedout(prev);
3745+
mm_cid_schedin(next);
3746+
}
3747+
36013748
/* Active implementation */
36023749
static inline void init_sched_mm_cid(struct task_struct *t)
36033750
{
@@ -3675,6 +3822,7 @@ static inline void switch_mm_cid(struct task_struct *prev, struct task_struct *n
36753822
#else /* !CONFIG_SCHED_MM_CID: */
36763823
static inline void mm_cid_select(struct task_struct *t) { }
36773824
static inline void switch_mm_cid(struct task_struct *prev, struct task_struct *next) { }
3825+
static inline void mm_cid_switch_to(struct task_struct *prev, struct task_struct *next) { }
36783826
#endif /* !CONFIG_SCHED_MM_CID */
36793827

36803828
extern u64 avg_vruntime(struct cfs_rq *cfs_rq);

0 commit comments

Comments
 (0)