membarrier: Remove unused code for architectures without membarrier hooks
Subsystems (6)
Mathieu Desnoyers
maintainerRFC PATCH 2/2 Fix: membarrier: Handle CLONE_VM + !CLONE_THREAD correctly on powerpc
In reply to Mathieu DesnoyersThreads targeting the same VM but which belong to different thread groups is a tricky case. It has a few consequences:
It turns out that we cannot rely on get_nr_threads(p) to count the number of threads using a VM. We can use (atomic_read(&mm->mm_users) == 1 && get_nr_threads(p) == 1) instead to skip the synchronize_sched() for cases where the VM only has a single user, and that user only has a single thread.
It also turns out that we cannot use for_each_thread() to set thread flags in all threads using a VM, as it only iterates on the thread group.
Therefore, test the membarrier state variable directly rather than relying on thread flags. This means membarrier_register_private_expedited() needs to set the MEMBARRIER_STATE_SWITCH_MM flag, issue synchronize_sched(), and only then set MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY which allows private expedited membarrier commands to succeed. membarrier_arch_switch_mm() now tests for the MEMBARRIER_STATE_SWITCH_MM flag.
--- arch/powerpc/include/asm/membarrier.h | 21 ++------------------- arch/powerpc/kernel/membarrier.c | 17 ++++------------- include/linux/mm_types.h | 2 +- include/linux/sched/mm.h | 28 ++++++---------------------- kernel/fork.c | 2 -- kernel/sched/membarrier.c | 16 +++++++++++++--- 6 files changed, 26 insertions(+), 60 deletions(-)
arch/powerpc/include/asm/membarrier.h +2 -19
@@ -11,8 +11,8 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev,* when switching from userspace to kernel is not needed after * store to rq->curr. */ -if (likely (!test_ti_thread_flag (task_thread_info (tsk), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ) || !prev)) +if (likely (!(atomic_read (&next->membarrier_state) + &MEMBARRIER_STATE_SWITCH_MM ) || !prev))return ;/*
@@ -21,23 +21,6 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev,*/ smp_mb(); } -static inline void membarrier_arch_fork (struct task_struct *t, -unsigned long clone_flags) -{ -/* -* Coherence of TIF_MEMBARRIER_PRIVATE_EXPEDITED against thread -* fork is protected by siglock. membarrier_arch_fork is called -* with siglock held. -*/ -if (test_thread_flag (TIF_MEMBARRIER_PRIVATE_EXPEDITED )) -set_ti_thread_flag (task_thread_info (t), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ); -} -static inline void membarrier_arch_execve (struct task_struct *t) -{ -clear_ti_thread_flag (task_thread_info (t), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ); -}void membarrier_arch_register_private_expedited (struct task_struct *t);#endif /* _ASM_POWERPC_MEMBARRIER_H */
arch/powerpc/kernel/membarrier.c +4 -13
@@ -19,24 +19,15 @@#include <linux/thread_info.h> #include <linux/spinlock.h> #include <linux/rcupdate.h> +#include <linux/atomic.h> void membarrier_arch_register_private_expedited (struct task_struct *p) { -struct task_struct *t; +struct mm_struct *mm = p->mm; -if (get_nr_threads (p) ==1 ) { -set_thread_flag (TIF_MEMBARRIER_PRIVATE_EXPEDITED ); +atomic_or (MEMBARRIER_STATE_SWITCH_MM , &mm->membarrier_state); +if (atomic_read (&mm->mm_users) ==1 &&get_nr_threads (p) ==1 )return ; - } -/* -* Coherence of TIF_MEMBARRIER_PRIVATE_EXPEDITED against thread -* fork is protected by siglock. -*/ -spin_lock (&p->sighand->siglock); -for_each_thread (p, t) -set_ti_thread_flag (task_thread_info (t), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ); -spin_unlock (&p->sighand->siglock);/* * Ensure all future scheduler executions will observe the new * thread flag state for this process.
include/linux/mm_types.h +1 -1
@@ -446,7 +446,7 @@ struct mm_struct {struct core_state *core_state;/* coredumping support */ #ifdef CONFIG_MEMBARRIER -int membarrier_private_expedited; +atomic_t membarrier_state;#endif #ifdef CONFIG_AIO spinlock_t ioctx_lock;
include/linux/sched/mm.h +6 -22
@@ -212,35 +212,23 @@ static inline void memalloc_noreclaim_restore(unsigned int flags)#ifdef CONFIG_MEMBARRIER +enum { +MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U <<0 ), +MEMBARRIER_STATE_SWITCH_MM = (1U <<1 ), +}; +#ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS #include <asm/membarrier.h> #else -static inline void membarrier_arch_fork (struct task_struct *t, -unsigned long clone_flags) -{ -} -static inline void membarrier_arch_execve (struct task_struct *t) -{ -}static inline void membarrier_arch_register_private_expedited (struct task_struct *p) { }#endif -static inline void membarrier_fork (struct task_struct *t, -unsigned long clone_flags) -{ -/* -* Prior copy_mm() copies the membarrier_private_expedited field -* from current->mm to t->mm. -*/ -membarrier_arch_fork (t, clone_flags); -}static inline void membarrier_execve (struct task_struct *t) { - t->mm->membarrier_private_expedited =0 ; -membarrier_arch_execve (t); +atomic_set (&t->mm->membarrier_state,0 ); }#else #ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS
@@ -249,10 +237,6 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev, { }#endif -static inline void membarrier_fork (struct task_struct *t, -unsigned long clone_flags) -{ -}static inline void membarrier_execve (struct task_struct *t) { }
kernel/fork.c +0 -2
@@ -1840,8 +1840,6 @@ static __latent_entropy struct task_struct *copy_process(*/ copy_seccomp (p); -membarrier_fork (p, clone_flags); -/* * Process group and session signals need to be delivered to just the * parent before the fork or both the parent and the child after the
kernel/sched/membarrier.c +13 -3
@@ -18,6 +18,7 @@#include <linux/membarrier.h> #include <linux/tick.h> #include <linux/cpumask.h> +#include <linux/atomic.h> #include "sched.h" /* for cpu_rq(). */
@@ -40,7 +41,8 @@ static int membarrier_private_expedited(void)bool fallback = false;cpumask_var_t tmpmask; -if (!READ_ONCE (current->mm->membarrier_private_expedited)) +if (!(atomic_read (¤t->mm->membarrier_state) + &MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY ))return -EPERM ;if (num_online_cpus () ==1 )
@@ -104,11 +106,19 @@ static int membarrier_private_expedited(void)static void membarrier_register_private_expedited (void ) {struct task_struct *p = current; +struct mm_struct *mm = p->mm; -if (READ_ONCE (p->mm->membarrier_private_expedited)) +/* +* We need to consider threads belonging to different thread +* groups, which use the same mm. (CLONE_VM but not +* CLONE_THREAD). +*/ +if (atomic_read (&mm->membarrier_state) + &MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY )return ;membarrier_arch_register_private_expedited (p); -WRITE_ONCE (p->mm->membarrier_private_expedited,1 ); +atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY , + &mm->membarrier_state); }/**
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 1/2 membarrier: Remove unused code for architectures without membarrier hooks
Architectures without membarrier hooks don't need to emit the empty membarrier_arch_switch_mm() static inline when CONFIG_MEMBARRIER=y.
Adapt the CONFIG_MEMBARRIER=n counterpart to only emit the empty membarrier_arch_switch_mm() for architectures with membarrier hooks.
--- include/linux/sched/mm.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/sched/mm.h +2 -4
@@ -215,10 +215,6 @@ static inline void memalloc_noreclaim_restore(unsigned int flags)#ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS #include <asm/membarrier.h> #else -static inline void membarrier_arch_switch_mm (struct mm_struct *prev, -struct mm_struct *next,struct task_struct *tsk) -{ -}static inline void membarrier_arch_fork (struct task_struct *t,unsigned long clone_flags) {
@@ -247,10 +243,12 @@ static inline void membarrier_execve(struct task_struct *t) membarrier_arch_execve(t); }#else +#ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS static inline void membarrier_arch_switch_mm (struct mm_struct *prev,struct mm_struct *next,struct task_struct *tsk) { } +#endif static inline void membarrier_fork (struct task_struct *t,unsigned long clone_flags) {
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 4.14 2/2] Fix: membarrier: Handle CLONE_VM + !CLONE_THREAD correctly on powerpc
In reply to Mathieu DesnoyersQuoted 13 lines · ----- On Oct 5, 2017, at 5:40 PM, Mathieu Desnoyers…
Threads targeting the same VM but which belong to different thread groups is a tricky case. It has a few consequences:
It turns out that we cannot rely on
get_nr_threads(p)to count the number of threads using a VM. We can use (atomic_read(&mm->mm_users)== 1 &&get_nr_threads(p)== 1) instead to skip thesynchronize_sched()for cases where the VM only has a single user, and that user only has a single thread.It also turns out that we cannot use
for_each_thread()to set thread flags in all threads using a VM, as it only iterates on the thread group.
Quoted 7 lines
Therefore, test the membarrier state variable directly rather than relying on thread flags. This means
membarrier_register_private_expedited()needs to set theMEMBARRIER_STATE_SWITCH_MMflag, issuesynchronize_sched(), and only then setMEMBARRIER_STATE_PRIVATE_EXPEDITED_READYwhich allows private expedited membarrier commands to succeed.membarrier_arch_switch_mm()now tests for theMEMBARRIER_STATE_SWITCH_MMflag.
I forgot to remove the membarrier thread flag on powerpc (now unused with this patch).
I'll send a v2 fixing this.
Thanks,
Mathieu
Quoted 236 lines
Reported-by Peter Zijlstra <[email protected]>Signed-off-by Mathieu Desnoyers <[email protected]>CC Paul E. McKenney <[email protected]>CC Boqun Feng <[email protected]>CC Andrew Hunter <[email protected]>CC Maged Michael <[email protected]>CC Avi Kivity <[email protected]>CC Benjamin Herrenschmidt <[email protected]>CC Paul Mackerras <[email protected]>CC Michael Ellerman <[email protected]>CC Dave Watson <[email protected]>CC Alan Stern <[email protected]>CC Will Deacon <[email protected]>CC Andy Lutomirski <[email protected]>CC Ingo Molnar <[email protected]>CC Alexander Viro <[email protected]>CC Nicholas Piggin <[email protected]>---
arch/powerpc/include/asm/membarrier.h | 21 ++------------------- arch/powerpc/kernel/membarrier.c | 17 ++++------------- include/linux/mm_types.h | 2 +- include/linux/sched/mm.h | 28 ++++++---------------------- kernel/fork.c | 2 -- kernel/sched/membarrier.c | 16 +++++++++++++--- 6 files changed, 26 insertions(+), 60 deletions(-)arch/powerpc/include/asm/membarrier.h +2
-2@@ -11,8 +11,8 @@ static inline void membarrier_arch_switch_mm(struct mm_struct*prev, * when switching from userspace to kernel is not needed after * store to rq->curr. */ -if (likely (!test_ti_thread_flag (task_thread_info (tsk), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ) || !prev)) +if (likely (!(atomic_read (&next->membarrier_state) + &MEMBARRIER_STATE_SWITCH_MM ) || !prev))return ;/*arch/powerpc/include/asm/membarrier.h +0
-17@@ -21,23 +21,6 @@ static inline void membarrier_arch_switch_mm(struct mm_struct*prev, */ smp_mb (); } -static inline void membarrier_arch_fork (struct task_struct *t, -unsigned long clone_flags) -{ -/* -* Coherence of TIF_MEMBARRIER_PRIVATE_EXPEDITED against thread -* fork is protected by siglock. membarrier_arch_fork is called -* with siglock held. -*/ -if (test_thread_flag (TIF_MEMBARRIER_PRIVATE_EXPEDITED )) -set_ti_thread_flag (task_thread_info (t), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ); -} -static inline void membarrier_arch_execve (struct task_struct *t) -{ -clear_ti_thread_flag (task_thread_info (t), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ); -}void membarrier_arch_register_private_expedited (struct task_struct *t);#endif /*
_ASM_POWERPC_MEMBARRIER_H*/arch/powerpc/kernel/membarrier.c +4
-13@@ -19,24 +19,15 @@#include <linux/thread_info.h> #include <linux/spinlock.h> #include <linux/rcupdate.h> +#include <linux/atomic.h> void membarrier_arch_register_private_expedited (struct task_struct *p) { -struct task_struct *t; +struct mm_struct *mm = p->mm; -if (get_nr_threads (p) ==1 ) { -set_thread_flag (TIF_MEMBARRIER_PRIVATE_EXPEDITED ); +atomic_or (MEMBARRIER_STATE_SWITCH_MM , &mm->membarrier_state); +if (atomic_read (&mm->mm_users) ==1 &&get_nr_threads (p) ==1 )return ; - } -/* -* Coherence of TIF_MEMBARRIER_PRIVATE_EXPEDITED against thread -* fork is protected by siglock. -*/ -spin_lock (&p->sighand->siglock); -for_each_thread (p, t) -set_ti_thread_flag (task_thread_info (t), -TIF_MEMBARRIER_PRIVATE_EXPEDITED ); -spin_unlock (&p->sighand->siglock);/* * Ensure all future scheduler executions will observe the new * thread flag state for this process. include/linux/mm_types.h +1
-1@@ -446,7 +446,7 @@ struct mm_struct {struct core_state *core_state;/* coredumping support */ #ifdef CONFIG_MEMBARRIER -int membarrier_private_expedited; +atomic_t membarrier_state;#endif #ifdef CONFIG_AIO spinlock_t ioctx_lock;include/linux/sched/mm.h +5
-7@@ -212,35 +212,23 @@ static inline void memalloc_noreclaim_restore(unsigned int flags)#ifdef CONFIG_MEMBARRIER +enum { +MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U <<0 ), +MEMBARRIER_STATE_SWITCH_MM = (1U <<1 ), +}; +#ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS #include <asm/membarrier.h> #else -static inline void membarrier_arch_fork (struct task_struct *t, -unsigned long clone_flags) -{ -} -static inline void membarrier_arch_execve (struct task_struct *t) -{ -}static inline void membarrier_arch_register_private_expedited(
structtask_struct*p)
{
}
#endifinclude/linux/sched/mm.h +0
-9-static inline void membarrier_fork (struct task_struct *t, -unsigned long clone_flags) -{ -/* -* Prior copy_mm() copies the membarrier_private_expedited field -* from current->mm to t->mm. -*/ -membarrier_arch_fork (t, clone_flags); -}static inline void
membarrier_execve(struct task_struct *t){include/linux/sched/mm.h +1
-2- t->mm->membarrier_private_expedited =0 ; -membarrier_arch_execve (t); +atomic_set (&t->mm->membarrier_state,0 );}
#else
#ifdefCONFIG_ARCH_HAS_MEMBARRIER_HOOKSinclude/linux/sched/mm.h +0
-4@@ -249,10 +237,6 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev, { }#endif -static inline void membarrier_fork (struct task_struct *t, -unsigned long clone_flags) -{ -}static inline void membarrier_execve (struct task_struct *t) {}
kernel/fork.c +0
-2@@ -1840,8 +1840,6 @@ static __latent_entropy struct task_struct *copy_process(*/ copy_seccomp (p); -membarrier_fork (p, clone_flags); -/* * Process group and session signals need to be delivered to just the * parent before the fork or both the parent and the child after the kernel/sched/membarrier.c +13
-4@@ -18,6 +18,7 @@#include <linux/membarrier.h> #include <linux/tick.h> #include <linux/cpumask.h> +#include <linux/atomic.h> #include "sched.h" /* for cpu_rq(). */ @@ -40,7 +41,8 @@ static int membarrier_private_expedited(void)bool fallback = false;cpumask_var_t tmpmask; -if (!READ_ONCE (current->mm->membarrier_private_expedited)) +if (!(atomic_read (¤t->mm->membarrier_state) + &MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY ))return -EPERM ;if (num_online_cpus () ==1 )@@ -104,11 +106,19 @@ static int membarrier_private_expedited(void)static void membarrier_register_private_expedited (void ) {struct task_struct *p = current; +struct mm_struct *mm = p->mm; -if (READ_ONCE (p->mm->membarrier_private_expedited)) +/* +* We need to consider threads belonging to different thread +* groups, which use the same mm. (CLONE_VM but not +* CLONE_THREAD). +*/ +if (atomic_read (&mm->membarrier_state) + &MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY )return ;membarrier_arch_register_private_expedited (p); -WRITE_ONCE (p->mm->membarrier_private_expedited,1 ); +atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY , + &mm->membarrier_state); }/** -- 2.11.0
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com