From: Nicholas Piggin <npiggin@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
linuxppc-dev@lists.ozlabs.org,
Linus Torvalds <torvalds@linux-foundation.org>,
Nicholas Piggin <npiggin@gmail.com>,
linux-mm@kvack.org
Subject: [PATCH 2/2] lazy tlb: consolidate lazy tlb mm switching
Date: Wed, 24 May 2023 16:04:55 +1000 [thread overview]
Message-ID: <20230524060455.147699-2-npiggin@gmail.com> (raw)
In-Reply-To: <20230524060455.147699-1-npiggin@gmail.com>
Switching a kernel thread using a "lazy tlb mm" to init_mm is a
relatively common sequence that is not quite trivial. Consolidate this
into a function.
This fixes a bug in do_shoot_lazy_tlb() for any arch that implements
finish_arch_post_lock_switch(). None select MMU_LAZY_TLB_SHOOTDOWN at
the moment.
Fixes: 2655421ae69fa ("lazy tlb: shoot lazies, non-refcounting lazy tlb mm reference handling scheme")
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
arch/powerpc/mm/book3s64/radix_tlb.c | 6 +----
include/linux/sched/task.h | 2 ++
kernel/fork.c | 7 ++----
kernel/sched/core.c | 34 ++++++++++++++++++++--------
4 files changed, 29 insertions(+), 20 deletions(-)
diff --git a/arch/powerpc/mm/book3s64/radix_tlb.c b/arch/powerpc/mm/book3s64/radix_tlb.c
index ce804b7bf84e..90953cf9f648 100644
--- a/arch/powerpc/mm/book3s64/radix_tlb.c
+++ b/arch/powerpc/mm/book3s64/radix_tlb.c
@@ -795,12 +795,8 @@ void exit_lazy_flush_tlb(struct mm_struct *mm, bool always_flush)
goto out;
if (current->active_mm == mm) {
- WARN_ON_ONCE(current->mm != NULL);
/* Is a kernel thread and is using mm as the lazy tlb */
- mmgrab_lazy_tlb(&init_mm);
- current->active_mm = &init_mm;
- switch_mm_irqs_off(mm, &init_mm, current);
- mmdrop_lazy_tlb(mm);
+ kthread_end_lazy_tlb_mm();
}
/*
diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index 537cbf9a2ade..23693b94a09b 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -61,6 +61,8 @@ extern int lockdep_tasklist_lock_is_held(void);
extern asmlinkage void schedule_tail(struct task_struct *prev);
extern void init_idle(struct task_struct *idle, int cpu);
+extern void kthread_end_lazy_tlb_mm(void);
+
extern int sched_fork(unsigned long clone_flags, struct task_struct *p);
extern void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs);
extern void sched_post_fork(struct task_struct *p);
diff --git a/kernel/fork.c b/kernel/fork.c
index ed4e01daccaa..8b005c2c7c3c 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -854,11 +854,8 @@ static void do_shoot_lazy_tlb(void *arg)
{
struct mm_struct *mm = arg;
- if (current->active_mm == mm) {
- WARN_ON_ONCE(current->mm);
- current->active_mm = &init_mm;
- switch_mm(mm, &init_mm, current);
- }
+ if (current->active_mm == mm)
+ kthread_end_lazy_tlb_mm();
}
static void cleanup_lazy_tlbs(struct mm_struct *mm)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index bc4ef1f3394b..71706df22b41 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5346,6 +5346,29 @@ context_switch(struct rq *rq, struct task_struct *prev,
return finish_task_switch(prev);
}
+/*
+ * If this kthread has a user process's mm for its active_mm (aka lazy tlb mm)
+ * then switch away from it, to init_mm. Must not be called while using an
+ * mm with kthread_use_mm().
+ */
+void kthread_end_lazy_tlb_mm(void)
+{
+ struct mm_struct *mm = current->active_mm;
+
+ WARN_ON_ONCE(!irqs_disabled());
+
+ if (WARN_ON_ONCE(current->mm))
+ return; /* Not a kthread or doing kthread_use_mm */
+
+ if (mm != &init_mm) {
+ mmgrab_lazy_tlb(&init_mm);
+ current->active_mm = &init_mm;
+ switch_mm_irqs_off(mm, &init_mm, current);
+ finish_arch_post_lock_switch();
+ mmdrop_lazy_tlb(mm);
+ }
+}
+
/*
* nr_running and nr_context_switches:
*
@@ -9375,17 +9398,8 @@ void sched_setnuma(struct task_struct *p, int nid)
*/
void idle_task_prepare_exit(void)
{
- struct mm_struct *mm = current->active_mm;
-
WARN_ON(!irqs_disabled());
-
- if (mm != &init_mm) {
- mmgrab_lazy_tlb(&init_mm);
- current->active_mm = &init_mm;
- switch_mm_irqs_off(mm, &init_mm, current);
- finish_arch_post_lock_switch();
- mmdrop_lazy_tlb(mm);
- }
+ kthread_end_lazy_tlb_mm();
/* finish_cpu() will mmdrop the init_mm ref after this CPU stops */
}
--
2.40.1
prev parent reply other threads:[~2023-05-24 6:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-24 6:04 [PATCH 1/2] lazy tlb: fix hotplug exit race with MMU_LAZY_TLB_SHOOTDOWN Nicholas Piggin
2023-05-24 6:04 ` Nicholas Piggin [this message]
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=20230524060455.147699-2-npiggin@gmail.com \
--to=npiggin@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-mm@kvack.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=peterz@infradead.org \
--cc=torvalds@linux-foundation.org \
/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;
as well as URLs for NNTP newsgroup(s).