From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Waiman Long <longman@redhat.com>, Ingo Molnar <mingo@redhat.com>,
linux-kernel <linux-kernel@vger.kernel.org>,
linux-mm <linux-mm@kvack.org>,
Andrew Morton <akpm@linux-foundation.org>,
Phil Auld <pauld@redhat.com>,
riel@surriel.com, Andy Lutomirski <luto@kernel.org>
Subject: Re: [PATCH] sched: Clean up active_mm reference counting
Date: Mon, 29 Jul 2019 11:01:10 -0400 (EDT) [thread overview]
Message-ID: <1705885422.1640.1564412470005.JavaMail.zimbra@efficios.com> (raw)
In-Reply-To: <20190729142450.GE31425@hirez.programming.kicks-ass.net>
----- On Jul 29, 2019, at 10:24 AM, Peter Zijlstra peterz@infradead.org wrote:
[...]
> ---
> Subject: sched: Clean up active_mm reference counting
> From: Peter Zijlstra <peterz@infradead.org>
> Date: Mon Jul 29 16:05:15 CEST 2019
>
> 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.
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
>
> Cc: riel@surriel.com
> Cc: luto@kernel.org
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> kernel/sched/core.c | 49 ++++++++++++++++++++++++++++++-------------------
> 1 file changed, 30 insertions(+), 19 deletions(-)
>
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3214,12 +3214,8 @@ static __always_inline struct rq *
> context_switch(struct rq *rq, struct task_struct *prev,
> struct task_struct *next, struct rq_flags *rf)
> {
> - struct mm_struct *mm, *oldmm;
> -
> 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
> * combine the page table reload and the switch backend into
> @@ -3228,22 +3224,37 @@ context_switch(struct rq *rq, struct tas
> arch_start_context_switch(prev);
>
> /*
> - * If mm is non-NULL, we pass through switch_mm(). If mm is
> - * NULL, we will pass through mmdrop() in finish_task_switch().
> - * Both of these contain the full memory barrier required by
> - * membarrier after storing to rq->curr, before returning to
> - * user-space.
> + * kernel -> kernel lazy + transfer active
> + * user -> kernel lazy + mmgrab() active
> + *
> + * kernel -> user switch + mmdrop() active
> + * user -> user switch
> */
> - if (!mm) {
> - next->active_mm = oldmm;
> - mmgrab(oldmm);
> - enter_lazy_tlb(oldmm, next);
> - } else
> - switch_mm_irqs_off(oldmm, mm, next);
> -
> - if (!prev->mm) {
> - prev->active_mm = NULL;
> - rq->prev_mm = oldmm;
> + if (!next->mm) { // to kernel
> + enter_lazy_tlb(prev->active_mm, next);
> +
> + next->active_mm = prev->active_mm;
> + if (prev->mm) // from user
> + 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);
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
next prev parent reply other threads:[~2019-07-29 15:01 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-27 17:10 [PATCH v2] sched/core: Don't use dying mm as active_mm of kthreads Waiman Long
2019-07-29 8:18 ` Qais Yousef
2019-07-29 21:06 ` Waiman Long
2019-07-29 21:33 ` Qais Yousef
2019-07-29 8:52 ` Peter Zijlstra
2019-07-29 14:24 ` [PATCH] sched: Clean up active_mm reference counting Peter Zijlstra
2019-07-29 15:01 ` Mathieu Desnoyers [this message]
2019-07-29 15:16 ` Waiman Long
2019-07-29 15:22 ` Peter Zijlstra
2019-07-29 15:29 ` Rik van Riel
2019-07-29 14:27 ` [PATCH v2] sched/core: Don't use dying mm as active_mm of kthreads Peter Zijlstra
2019-07-29 15:22 ` Waiman Long
2019-07-29 15:38 ` Peter Zijlstra
2019-07-29 14:51 ` Waiman Long
2019-07-29 15:03 ` Peter Zijlstra
2019-07-29 15:28 ` Rik van Riel
2019-07-29 15:44 ` Peter Zijlstra
2019-07-29 16:10 ` Rik van Riel
2019-07-29 16:26 ` Peter Zijlstra
2019-07-29 15:37 ` Waiman Long
2019-07-29 16:12 ` Rik van Riel
2019-07-29 16:22 ` Andy Lutomirski
2019-07-29 9:12 ` Michal Hocko
2019-07-29 15:27 ` Waiman Long
2019-07-29 18:58 ` Michal Hocko
2019-07-29 20:41 ` Waiman Long
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1705885422.1640.1564412470005.JavaMail.zimbra@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=longman@redhat.com \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=pauld@redhat.com \
--cc=peterz@infradead.org \
--cc=riel@surriel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox