Skip to content

Commit 139d025

Browse files
author
Peter Zijlstra
committed
sched: Clean up active_mm reference counting
The current active_mm reference counting is confusing and sub-optimal. Rewrite the code to explicitly consider the 4 separate cases: user -> user When switching between two user tasks, all we need to consider is switch_mm(). user -> kernel When switching from a user task to a kernel task (which doesn't have an associated mm) we retain the last mm in our active_mm. Increment a reference count on active_mm. kernel -> kernel When switching between kernel threads, all we need to do is pass along the active_mm reference. kernel -> user When switching between a kernel and user task, we must switch from the last active_mm to the next mm, hoping of course that these are the same. Decrement a reference on the active_mm. The code keeps a different order, because as you'll note, both 'to user' cases require switch_mm(). And where the old code would increment/decrement for the 'kernel -> kernel' case, the new code observes this is a neutral operation and avoids touching the reference count. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Rik van Riel <riel@surriel.com> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: luto@kernel.org
1 parent 130d9c3 commit 139d025

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

‎kernel/sched/core.c‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,12 +3214,8 @@ static __always_inline struct rq *
32143214
context_switch(struct rq *rq, struct task_struct *prev,
32153215
struct task_struct *next, struct rq_flags *rf)
32163216
{
3217-
struct mm_struct *mm, *oldmm;
3218-
32193217
prepare_task_switch(rq, prev, next);
32203218

3221-
mm = next->mm;
3222-
oldmm = prev->active_mm;
32233219
/*
32243220
* For paravirt, this is coupled with an exit in switch_to to
32253221
* combine the page table reload and the switch backend into
@@ -3228,22 +3224,37 @@ context_switch(struct rq *rq, struct task_struct *prev,
32283224
arch_start_context_switch(prev);
32293225

32303226
/*
3231-
* If mm is non-NULL, we pass through switch_mm(). If mm is
3232-
* NULL, we will pass through mmdrop() in finish_task_switch().
3233-
* Both of these contain the full memory barrier required by
3234-
* membarrier after storing to rq->curr, before returning to
3235-
* user-space.
3227+
* kernel -> kernel lazy + transfer active
3228+
* user -> kernel lazy + mmgrab() active
3229+
*
3230+
* kernel -> user switch + mmdrop() active
3231+
* user -> user switch
32363232
*/
3237-
if (!mm) {
3238-
next->active_mm = oldmm;
3239-
mmgrab(oldmm);
3240-
enter_lazy_tlb(oldmm, next);
3241-
} else
3242-
switch_mm_irqs_off(oldmm, mm, next);
3243-
3244-
if (!prev->mm) {
3245-
prev->active_mm = NULL;
3246-
rq->prev_mm = oldmm;
3233+
if (!next->mm) { // to kernel
3234+
enter_lazy_tlb(prev->active_mm, next);
3235+
3236+
next->active_mm = prev->active_mm;
3237+
if (prev->mm) // from user
3238+
mmgrab(prev->active_mm);
3239+
else
3240+
prev->active_mm = NULL;
3241+
} else { // to user
3242+
/*
3243+
* sys_membarrier() requires an smp_mb() between setting
3244+
* rq->curr and returning to userspace.
3245+
*
3246+
* The below provides this either through switch_mm(), or in
3247+
* case 'prev->active_mm == next->mm' through
3248+
* finish_task_switch()'s mmdrop().
3249+
*/
3250+
3251+
switch_mm_irqs_off(prev->active_mm, next->mm, next);
3252+
3253+
if (!prev->mm) { // from kernel
3254+
/* will mmdrop() in finish_task_switch(). */
3255+
rq->prev_mm = prev->active_mm;
3256+
prev->active_mm = NULL;
3257+
}
32473258
}
32483259

32493260
rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);

0 commit comments

Comments
 (0)