mirror of
https://github.com/brain-hackers/linux-brain.git
synced 2024-06-09 23:36:23 +09:00
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
This commit is contained in:
parent
130d9c331b
commit
139d025cda
|
@ -3214,12 +3214,8 @@ static __always_inline struct rq *
|
||||||
context_switch(struct rq *rq, struct task_struct *prev,
|
context_switch(struct rq *rq, struct task_struct *prev,
|
||||||
struct task_struct *next, struct rq_flags *rf)
|
struct task_struct *next, struct rq_flags *rf)
|
||||||
{
|
{
|
||||||
struct mm_struct *mm, *oldmm;
|
|
||||||
|
|
||||||
prepare_task_switch(rq, prev, next);
|
prepare_task_switch(rq, prev, next);
|
||||||
|
|
||||||
mm = next->mm;
|
|
||||||
oldmm = prev->active_mm;
|
|
||||||
/*
|
/*
|
||||||
* For paravirt, this is coupled with an exit in switch_to to
|
* For paravirt, this is coupled with an exit in switch_to to
|
||||||
* combine the page table reload and the switch backend into
|
* combine the page table reload and the switch backend into
|
||||||
|
@ -3228,22 +3224,37 @@ context_switch(struct rq *rq, struct task_struct *prev,
|
||||||
arch_start_context_switch(prev);
|
arch_start_context_switch(prev);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If mm is non-NULL, we pass through switch_mm(). If mm is
|
* kernel -> kernel lazy + transfer active
|
||||||
* NULL, we will pass through mmdrop() in finish_task_switch().
|
* user -> kernel lazy + mmgrab() active
|
||||||
* Both of these contain the full memory barrier required by
|
*
|
||||||
* membarrier after storing to rq->curr, before returning to
|
* kernel -> user switch + mmdrop() active
|
||||||
* user-space.
|
* user -> user switch
|
||||||
*/
|
*/
|
||||||
if (!mm) {
|
if (!next->mm) { // to kernel
|
||||||
next->active_mm = oldmm;
|
enter_lazy_tlb(prev->active_mm, next);
|
||||||
mmgrab(oldmm);
|
|
||||||
enter_lazy_tlb(oldmm, next);
|
|
||||||
} else
|
|
||||||
switch_mm_irqs_off(oldmm, mm, next);
|
|
||||||
|
|
||||||
if (!prev->mm) {
|
next->active_mm = prev->active_mm;
|
||||||
prev->active_mm = NULL;
|
if (prev->mm) // from user
|
||||||
rq->prev_mm = oldmm;
|
mmgrab(prev->active_mm);
|
||||||
|
else
|
||||||
|
prev->active_mm = NULL;
|
||||||
|
} else { // to user
|
||||||
|
/*
|
||||||
|
* sys_membarrier() requires an smp_mb() between setting
|
||||||
|
* rq->curr and returning to userspace.
|
||||||
|
*
|
||||||
|
* The below provides this either through switch_mm(), or in
|
||||||
|
* case 'prev->active_mm == next->mm' through
|
||||||
|
* finish_task_switch()'s mmdrop().
|
||||||
|
*/
|
||||||
|
|
||||||
|
switch_mm_irqs_off(prev->active_mm, next->mm, next);
|
||||||
|
|
||||||
|
if (!prev->mm) { // from kernel
|
||||||
|
/* will mmdrop() in finish_task_switch(). */
|
||||||
|
rq->prev_mm = prev->active_mm;
|
||||||
|
prev->active_mm = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
|
rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user