Membarrier fixes/cleanups
Subsystems (5)
Mathieu Desnoyers
maintainercover Membarrier fixes/cleanups
This series of fixes/cleanups is submitted for feedback. It takes care of membarrier issues recently discussed.
Thanks,
Mathieu
Mathieu Desnoyers (4):
Fix: sched/membarrier: private expedited registration check
Cleanup: sched/membarrier: remove redundant check
Cleanup: sched/membarrier: only sync_core before usermode for same mm
Fix: sched/membarrier: p->mm->membarrier_state racy load
include/linux/mm_types.h | 7 +- include/linux/sched/mm.h | 8 +-- kernel/sched/core.c | 1 + kernel/sched/membarrier.c | 141 +++++++++++++++++++++++++++----------- kernel/sched/sched.h | 33 +++++++++ 5 files changed, 143 insertions(+), 47 deletions(-)
Signature · 1 line
2.17.1
Mathieu Desnoyers
maintainerRFC PATCH 1/4 Fix: sched/membarrier: private expedited registration check
Fix a logic flaw in the way membarrier_register_private_expedited() handles ready state checks for private expedited sync core and private expedited registrations.
If a private expedited membarrier registration is first performed, and then a private expedited sync_core registration is performed, the ready state check will skip the second registration when it really should not.
--- kernel/sched/membarrier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/membarrier.c +1 -1
@@ -226,7 +226,7 @@ static int membarrier_register_private_expedited(int flags)* groups, which use the same mm. (CLONE_VM but not * CLONE_THREAD). */ -if (atomic_read (&mm->membarrier_state) & state) +if ((atomic_read (&mm->membarrier_state) & state) == state)return 0 ;atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED , &mm->membarrier_state);if (flags &MEMBARRIER_FLAG_SYNC_CORE )
Signature · 1 line
2.17.1
Mathieu Desnoyers
maintainerRFC PATCH 2/4 Cleanup: sched/membarrier: remove redundant check
In reply to Mathieu DesnoyersChecking that the number of threads is 1 is redundant with checking mm_users == 1.
--- kernel/sched/membarrier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/membarrier.c +2 -2
@@ -186,7 +186,7 @@ static int membarrier_register_global_expedited(void) MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY)return 0 ;atomic_or (MEMBARRIER_STATE_GLOBAL_EXPEDITED , &mm->membarrier_state); -if (atomic_read (&mm->mm_users) ==1 &&get_nr_threads (p) ==1 ) { +if (atomic_read (&mm->mm_users) ==1 ) {/* * For single mm user, single threaded process, we can * simply issue a memory barrier after setting
@@ -232,7 +232,7 @@ static int membarrier_register_private_expedited(int flags)if (flags &MEMBARRIER_FLAG_SYNC_CORE )atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE , &mm->membarrier_state); -if (!(atomic_read (&mm->mm_users) ==1 &&get_nr_threads (p) ==1 )) { +if (atomic_read (&mm->mm_users) !=1 ) {/* * Ensure all future scheduler executions will observe the * new thread flag state for this process.
Signature · 1 line
2.17.1
Mathieu Desnoyers
maintainerRFC PATCH 3/4 Cleanup: sched/membarrier: only sync_core before usermode for same mm
In reply to Mathieu DesnoyersWhen the prev and next task's mm change, switch_mm() provides the core serializing guarantees before returning to usermode. The only case where an explicit core serialization is needed is when the scheduler keeps the same mm for prev and next.
--- include/linux/sched/mm.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/sched/mm.h +2 -0
@@ -362,6 +362,8 @@ enum {static inline void membarrier_mm_sync_core_before_usermode (struct mm_struct *mm) { +if (current->mm != mm) +return ;if (likely (!(atomic_read (&mm->membarrier_state) &MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE )))return ;
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [RFC PATCH 3/4] Cleanup: sched/membarrier: only sync_core before usermode for same mm
Quoted 28 lines · Mathieu Desnoyers
When the prev and next task's mm change,
switch_mm()provides the core serializing guarantees before returning to usermode. The only case where an explicit core serialization is needed is when the scheduler keeps the same mm for prev and next.Suggested-by Oleg Nesterov <[email protected]>Signed-off-by Mathieu Desnoyers <[email protected]>Cc "Paul E. McKenney" <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Oleg Nesterov <[email protected]>Cc "Eric W. Biederman" <[email protected]>Cc Linus Torvalds <[email protected]>Cc Russell King - ARM Linux admin <[email protected]>Cc Chris Metcalf <[email protected]>Cc Christoph Lameter <[email protected]>Cc Kirill Tkhai <[email protected]>Cc Mike Galbraith <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>--- include/linux/sched/mm.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/sched/mm.h +0
-0
include/linux/sched/mm.h enum { · line 369
Quoted 8 lines
include/linux/sched/mm.h +2
-0@@ -362,6 +362,8 @@ enum {static inline void membarrier_mm_sync_core_before_usermode (struct mm_struct *mm) { +if (current->mm != mm) +return ;if (likely (!(atomic_read (&mm->membarrier_state) &MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE )))return ;
So SYNC_CORE is about I$ coherency and funny thing like that. Now it seems 'natural' that if we flip the address space, that I$ also gets wiped/updated, because the whole text mapping changes.
But did we just assume that, or did we verify the truth of this? (I'm just being paranoid here)
Mathieu Desnoyers
maintainerRe: [RFC PATCH 3/4] Cleanup: sched/membarrier: only sync_core before usermode for same mm
Quoted 42 lines · ----- On Sep 6, 2019, at 3:41 AM, Peter Zijlstra [email protected] wrote:
Quoted 28 lines · Mathieu Desnoyers
When the prev and next task's mm change,
switch_mm()provides the core serializing guarantees before returning to usermode. The only case where an explicit core serialization is needed is when the scheduler keeps the same mm for prev and next.Suggested-by Oleg Nesterov <[email protected]>Signed-off-by Mathieu Desnoyers <[email protected]>Cc "Paul E. McKenney" <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Oleg Nesterov <[email protected]>Cc "Eric W. Biederman" <[email protected]>Cc Linus Torvalds <[email protected]>Cc Russell King - ARM Linux admin <[email protected]>Cc Chris Metcalf <[email protected]>Cc Christoph Lameter <[email protected]>Cc Kirill Tkhai <[email protected]>Cc Mike Galbraith <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>--- include/linux/sched/mm.h | 2 ++ 1 file changed, 2 insertions(+)include/linux/sched/mm.h +0
-0include/linux/sched/mm.h enum { · line 369
Quoted 8 lines
include/linux/sched/mm.h +2
-0@@ -362,6 +362,8 @@ enum {static inline void membarrier_mm_sync_core_before_usermode (struct mm_struct *mm) { +if (current->mm != mm) +return ;if (likely (!(atomic_read (&mm->membarrier_state) &MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE )))return ;So
SYNC_COREis about I$ coherency and funny thing like that. Now it seems 'natural' that if we flip the address space, that I$ also gets wiped/updated, because the whole text mapping changes.
Quoted 2 lines
But did we just assume that, or did we verify the truth of this? (I'm just being paranoid here)
We have documented those here:
Documentation/features/sched/membarrier-sync-core/arch-support.txt
For instance, x86:
# * x86
#
# x86-32 uses IRET as return from interrupt, which takes care of the IPI.
# However, it uses both IRET and SYSEXIT to go back to user-space. The IRET
# instruction is core serializing, but not SYSEXIT.
#
# x86-64 uses IRET as return from interrupt, which takes care of the IPI.
# However, it can return to user-space through either SYSRETL (compat code),
# SYSRETQ, or IRET.
#
# Given that neither SYSRET{L,Q}, nor SYSEXIT, are core serializing, we rely
# instead on write_cr3() performed by switch_mm() to provide core serialization
# after changing the current mm, and deal with the special case of kthread ->
# uthread (temporarily keeping current mm into active_mm) by issuing a
# sync_core_before_usermode() in that specific case.I've also made sure x86 switch_mm_irqs_off() has the following comment, just in case someone too keen on optimizing away the CR3 write would forget to look at arch-support.txt:
/*
* The membarrier system call requires a full memory barrier and
* core serialization before returning to user-space, after
* storing to rq->curr. Writing to CR3 provides that full
* memory barrier and core serializing instruction. */
In the case of arm/arm64, they have no requirement on switch_mm():
# * arm/arm64
#
# Rely on implicit context synchronization as a result of exception return
# when returning from IPI handler, and when returning to user-space.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRFC PATCH 4/4 Fix: sched/membarrier: p->mm->membarrier_state racy load
In reply to Mathieu DesnoyersThe membarrier_state field is located within the mm_struct, which is not guaranteed to exist when used from runqueue-lock-free iteration on runqueues by the membarrier system call.
Copy the membarrier_state from the mm_struct into the scheduler runqueue in the scheduler prepare task switch when the scheduler switches between mm.
When registering membarrier for mm, after setting the registration bit in the mm membarrier state, issue a synchronize_rcu() to ensure the scheduler observes the change. In order to take care of the case where a runqueue keeps executing the target mm without swapping to other mm, iterate over each runqueue and issue an IPI to copy the membarrier_state from the mm_struct into each runqueue which have the same mm which state has just been modified.
Move the mm membarrier_state field closer to pgd in mm_struct so sched switch prepare hits a cache line which is already touched by the scheduler switch_mm.
membarrier_execve() hook now needs to clear the runqueue's membarrier state in addition to clear the mm membarrier state, so move its implementation into the scheduler membarrier code so it can access the runqueue structure.
As suggested by Linus, move all membarrier.c RCU read-side locks outside of the for each cpu loops. In fact, it turns out that membarrier_global_expedited() does not need the RCU read-side critical section because it does not need to use task_rcu_dereference() anymore, relying instead on the runqueue's membarrier_state.
--- include/linux/mm_types.h | 7 +- include/linux/sched/mm.h | 6 +- kernel/sched/core.c | 1 + kernel/sched/membarrier.c | 141 +++++++++++++++++++++++++++----------- kernel/sched/sched.h | 33 +++++++++ 5 files changed, 141 insertions(+), 47 deletions(-)
include/linux/mm_types.h +4 -3
@@ -382,6 +382,9 @@ struct mm_struct {unsigned long task_size;/* size of task vm space */ unsigned long highest_vm_end;/* highest vma end address */ pgd_t * pgd; +#ifdef CONFIG_MEMBARRIER +atomic_t membarrier_state; +#endif /** * @mm_users: The number of users including userspace.
@@ -452,9 +455,7 @@ struct mm_struct {unsigned long flags;/* Must use atomic bitops to access */ struct core_state *core_state;/* coredumping support */ -#ifdef CONFIG_MEMBARRIER -atomic_t membarrier_state; -#endif +#ifdef CONFIG_AIO spinlock_t ioctx_lock;struct kioctx_table __rcu *ioctx_table;
include/linux/sched/mm.h +2 -4
@@ -370,10 +370,8 @@ static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)sync_core_before_usermode (); } -static inline void membarrier_execve (struct task_struct *t) -{ -atomic_set (&t->mm->membarrier_state,0 ); -} +extern void membarrier_execve (struct task_struct *t); +#else #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
kernel/sched/core.c +1 -0
@@ -3038,6 +3038,7 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev,perf_event_task_sched_out (prev, next);rseq_preempt (prev);fire_sched_out_preempt_notifiers (prev, next); +membarrier_prepare_task_switch (rq, prev, next);prepare_task (next);prepare_arch_switch (next); }
kernel/sched/membarrier.c +101 -40
213 lines not on this page · raw message
kernel/sched/sched.h +33 -0
@@ -907,6 +907,10 @@ struct rq {atomic_t nr_iowait; +#ifdef CONFIG_MEMBARRIER +int membarrier_state; +#endif +#ifdef CONFIG_SMP struct root_domain *rd;struct sched_domain __rcu *sd;
@@ -2423,3 +2427,32 @@ static inline bool sched_energy_enabled(void)static inline bool sched_energy_enabled (void ) {return false; }#endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */ + +#ifdef CONFIG_MEMBARRIER +/* +* The scheduler provides memory barriers required by membarrier between: +* - prior user-space memory accesses and store to rq->membarrier_state, +* - store to rq->membarrier_state and following user-space memory accesses. +* In the same way it provides those guarantees around store to rq->curr. +*/ +static inline void membarrier_prepare_task_switch (struct rq *rq, +struct task_struct *prev, +struct task_struct *next) +{ +int membarrier_state =0 ; +struct mm_struct *next_mm = next->mm; + +if (prev->mm == next_mm) +return ; +if (next_mm) + membarrier_state =atomic_read (&next_mm->membarrier_state); +if (READ_ONCE (rq->membarrier_state) != membarrier_state) +WRITE_ONCE (rq->membarrier_state, membarrier_state); +} +#else +static inline void membarrier_prepare_task_switch (struct rq *rq, +struct task_struct *prev, +struct task_struct *next) +{ +} +#endif
Signature · 1 line
2.17.1
Peter Zijlstra
maintainerRe: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load
include/linux/mm_types.h struct mm_struct { · line 385
Quoted 9 lines · Mathieu Desnoyers
include/linux/mm_types.h +1
-0@@ -382,6 +382,9 @@ struct mm_struct {unsigned long task_size;/* size of task vm space */ unsigned long highest_vm_end;/* highest vma end address */ pgd_t * pgd; +#ifdef CONFIG_MEMBARRIER
Stick in a comment, on why here. To be close to data already used by switch_mm().
Quoted 11 lines
include/linux/mm_types.h +2
-0+atomic_t membarrier_state; +#endif /** * @mm_users: The number of users including userspace. kernel/sched/core.c +0
-0
kernel/sched/core.c prepare_task_switch(struct rq *rq, struct task_struct *prev, · line 3041
Quoted 4 lines
kernel/sched/core.c +1
-0@@ -3038,6 +3038,7 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev,perf_event_task_sched_out (prev, next);rseq_preempt (prev);fire_sched_out_preempt_notifiers (prev, next); +membarrier_prepare_task_switch (rq, prev, next);
This had me confused for a while, because I initially thought we'd only do this for switch_mm(), but you're made it agressive and track kernel threads too.
I think we can do that slightly different. See below...
Quoted 9 lines
kernel/sched/core.c +0
-0prepare_task (next);prepare_arch_switch (next); }kernel/sched/membarrier.c +0
-0
kernel/sched/membarrier.c static void ipi_mb(void *info) · line 34
Quoted 5 lines
kernel/sched/membarrier.c +4
-0@@ -30,6 +30,28 @@ static void ipi_mb(void *info) +void membarrier_execve (struct task_struct *t) +{ +atomic_set (&t->mm->membarrier_state,0 ); +WRITE_ONCE (this_rq ()->membarrier_state,0 );
It is the callsite of this one that had me puzzled and confused. I think it works by accident more than anything else.
You see; I thought the rules were that we'd change it near/before switch_mm(), and this is quite a way _after_.
I think it might be best to place the call in exec_mmap(), right before activate_mm().
But that then had me wonder about the membarrier_prepate_task_switch() thing...
Quoted 10 lines
kernel/sched/membarrier.c +10
-0+/* +* The scheduler provides memory barriers required by membarrier between: +* - prior user-space memory accesses and store to rq->membarrier_state, +* - store to rq->membarrier_state and following user-space memory accesses. +* In the same way it provides those guarantees around store to rq->curr. +*/ +static inline void membarrier_prepare_task_switch (struct rq *rq, +struct task_struct *prev, +struct task_struct *next) +{
kernel/sched/membarrier.c static void ipi_mb(void *info) · line 54
Quoted 10 lines
kernel/sched/membarrier.c +10
-0+int membarrier_state =0 ; +struct mm_struct *next_mm = next->mm; + +if (prev->mm == next_mm) +return ; +if (next_mm) + membarrier_state =atomic_read (&next_mm->membarrier_state); +if (READ_ONCE (rq->membarrier_state) != membarrier_state) +WRITE_ONCE (rq->membarrier_state, membarrier_state); +}
So if you make the above something like:
static inline voidmembarrier_switch_mm(struct rq *rq, struct mm_struct *prev_mm, struct mm_struct *next_mm)
{
int membarrier_state;
if (prev_mm == next_mm)
return;
membarrier_state = atomic_read(&next_mm->membarrier_state);
if (READ_ONCE(rq->membarrier_state) == membarrier_state)
return;
WRITE_ONCE(rq->membarrier_state, membarrier_state);
}And put it right in front of switch_mm() in context_switch() then we'll deal with kernel on the other side, like so:
kernel/sched/membarrier.c static int membarrier_global_expedited(void) · line 94
Quoted 10 lines
kernel/sched/membarrier.c +2
-4@@ -70,16 +90,13 @@ static int membarrier_global_expedited(void)if (cpu ==raw_smp_processor_id ())continue ; -rcu_read_lock (); - p =task_rcu_dereference (&cpu_rq (cpu)->curr); -if (p && p->mm && (atomic_read (&p->mm->membarrier_state) & -MEMBARRIER_STATE_GLOBAL_EXPEDITED )) { +if (READ_ONCE (cpu_rq (cpu)->membarrier_state) & +MEMBARRIER_STATE_GLOBAL_EXPEDITED ) {
p = rcu_dereference(rq->curr); if ((READ_ONCE(cpu_rq(cpu)->membarrier_state) & MEMBARRIER_STATE_GLOBAL_EXPEDITED) && !(p->flags & PF_KTHREAD))
kernel/sched/membarrier.c static int membarrier_global_expedited(void) · line 102
Quoted 9 lines
kernel/sched/membarrier.c +0
-1if (!fallback)__cpumask_set_cpu (cpu, tmpmask);else smp_call_function_single (cpu, ipi_mb,NULL ,1 ); } -rcu_read_unlock (); }if (!fallback) {preempt_disable ();
does that make sense?
(also, I hate how long all these membarrier names are)
Mathieu Desnoyers
maintainerRFC PATCH 4/4 Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
The membarrier_state field is located within the mm_struct, which is not guaranteed to exist when used from runqueue-lock-free iteration on runqueues by the membarrier system call.
Copy the membarrier_state from the mm_struct into the scheduler runqueue when the scheduler switches between mm.
When registering membarrier for mm, after setting the registration bit in the mm membarrier state, issue a synchronize_rcu() to ensure the scheduler observes the change. In order to take care of the case where a runqueue keeps executing the target mm without swapping to other mm, iterate over each runqueue and issue an IPI to copy the membarrier_state from the mm_struct into each runqueue which have the same mm which state has just been modified.
Move the mm membarrier_state field closer to pgd in mm_struct to use a cache line already touched by the scheduler switch_mm.
The membarrier_execve() (now membarrier_exec_mmap) hook now needs to clear the runqueue's membarrier state in addition to clear the mm membarrier state, so move its implementation into the scheduler membarrier code so it can access the runqueue structure.
Add memory barrier in membarrier_exec_mmap() prior to clearing the membarrier state, ensuring memory accesses executed prior to exec are not reordered with the stores clearing the membarrier state.
As suggested by Linus, move all membarrier.c RCU read-side locks outside of the for each cpu loops.
---
Changes since v1:
- Take care of Peter Zijlstra's feedback, moving callsites closer to switch_mm() (scheduler) and activate_mm() (execve).
- Add memory barrier in membarrier_exec_mmap() prior to clearing the membarrier state.
--- fs/exec.c | 2 +- include/linux/mm_types.h | 14 +++- include/linux/sched/mm.h | 8 +- kernel/sched/core.c | 4 +- kernel/sched/membarrier.c | 170 ++++++++++++++++++++++++++++---------- kernel/sched/sched.h | 34 ++++++++ 6 files changed, 180 insertions(+), 52 deletions(-)
fs/exec.c +1 -1
@@ -1035,6 +1035,7 @@ static int exec_mmap(struct mm_struct *mm) active_mm = tsk->active_mm; tsk->mm = mm; tsk->active_mm = mm; +membarrier_exec_mmap (mm);activate_mm (active_mm, mm); tsk->mm->vmacache_seqnum =0 ;vmacache_flush (tsk);
@@ -1825,7 +1826,6 @@ static int __do_execve_file(int fd, struct filename *filename,/* execve succeeded */ current->fs->in_exec =0 ; current->in_execve =0 ; -membarrier_execve (current);rseq_execve (current);acct_update_integrals (current);task_numa_free (current, false);
include/linux/mm_types.h +11 -3
@@ -383,6 +383,16 @@ struct mm_struct {unsigned long highest_vm_end;/* highest vma end address */ pgd_t * pgd; +#ifdef CONFIG_MEMBARRIER +/** +* @membarrier_state: Flags controlling membarrier behavior. +* +* This field is close to @pgd to hopefully fit in the same +* cache-line, which needs to be touched by switch_mm(). +*/ +atomic_t membarrier_state; +#endif +/** * @mm_users: The number of users including userspace. *
@@ -452,9 +462,7 @@ struct mm_struct {unsigned long flags;/* Must use atomic bitops to access */ struct core_state *core_state;/* coredumping support */ -#ifdef CONFIG_MEMBARRIER -atomic_t membarrier_state; -#endif +#ifdef CONFIG_AIO spinlock_t ioctx_lock;struct kioctx_table __rcu *ioctx_table;
include/linux/sched/mm.h +3 -5
@@ -370,10 +370,8 @@ static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)sync_core_before_usermode (); } -static inline void membarrier_execve (struct task_struct *t) -{ -atomic_set (&t->mm->membarrier_state,0 ); -} +extern void membarrier_exec_mmap (struct mm_struct *mm); +#else #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
@@ -382,7 +380,7 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev, { }#endif -static inline void membarrier_execve (struct task_struct *t) +static inline void membarrier_exec_mmap (struct mm_struct *mm) { }static inline void membarrier_mm_sync_core_before_usermode (struct mm_struct *mm)
kernel/sched/core.c +3 -1
@@ -3238,8 +3238,10 @@ context_switch(struct rq *rq, struct task_struct *prev, next->active_mm = oldmm;mmgrab (oldmm);enter_lazy_tlb (oldmm, next); - }else + }else { +membarrier_switch_mm (rq, oldmm, mm);switch_mm_irqs_off (oldmm, mm, next); + }if (!prev->mm) { prev->active_mm =NULL ;
kernel/sched/membarrier.c +128 -42
239 lines not on this page · raw message
kernel/sched/sched.h +34 -0
@@ -907,6 +907,10 @@ struct rq {atomic_t nr_iowait; +#ifdef CONFIG_MEMBARRIER +int membarrier_state; +#endif +#ifdef CONFIG_SMP struct root_domain *rd;struct sched_domain __rcu *sd;
@@ -2423,3 +2427,33 @@ static inline bool sched_energy_enabled(void)static inline bool sched_energy_enabled (void ) {return false; }#endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL */ + +#ifdef CONFIG_MEMBARRIER +/* +* The scheduler provides memory barriers required by membarrier between: +* - prior user-space memory accesses and store to rq->membarrier_state, +* - store to rq->membarrier_state and following user-space memory accesses. +* In the same way it provides those guarantees around store to rq->curr. +*/ +static inline void membarrier_switch_mm (struct rq *rq, +struct mm_struct *prev_mm, +struct mm_struct *next_mm) +{ +int membarrier_state; + +if (prev_mm == next_mm) +return ; + + membarrier_state =atomic_read (&next_mm->membarrier_state); +if (READ_ONCE (rq->membarrier_state) == membarrier_state) +return ; + +WRITE_ONCE (rq->membarrier_state, membarrier_state); +} +#else +static inline void membarrier_switch_mm (struct rq *rq, +struct mm_struct *prev_mm, +struct mm_struct *next_mm) +{ +} +#endif
Signature · 1 line
2.17.1
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 24 lines · Mathieu Desnoyers
kernel/sched/membarrier.c +23
-0+static void sync_runqueues_membarrier_state (struct mm_struct *mm) +{ +int membarrier_state =atomic_read (&mm->membarrier_state); +bool fallback = false; +cpumask_var_t tmpmask; + +if (!zalloc_cpumask_var (&tmpmask,GFP_NOWAIT )) { +/* Fallback for OOM. */ + fallback = true; + } + +/* +* For each cpu runqueue, if the task's mm match @mm, ensure that all +* @mm's membarrier state set bits are also set in in the runqueue's +* membarrier state. This ensures that a runqueue scheduling +* between threads which are users of @mm has its membarrier state +* updated. +*/ +cpus_read_lock (); +rcu_read_lock (); +for_each_online_cpu (cpu) { +struct rq *rq =cpu_rq (cpu); +struct task_struct *p;
kernel/sched/membarrier.c static int membarrier_private_expedited(int flags) · line 276
Quoted 10 lines
kernel/sched/membarrier.c +10
-0+ + p =task_rcu_dereference (&rq->curr); +if (p && p->mm == mm) { +if (!fallback) +__cpumask_set_cpu (cpu, tmpmask); +else +smp_call_function_single (cpu, ipi_sync_rq_state, + mm,1 ); + } + }
I really absolutely detest this whole "fallback" code.
It will never get any real testing, and the code is just broken.
Why don't you just use the mm_cpumask(mm) unconditionally? Yes, it will possibly call too many CPU's, but this fallback code is just completely disgusting.
Do a simple and clean implementation. Then, if you can show real performance issues (which I doubt), maybe do something else, but even then you should never do something that will effectively create cases that have absolutely zero test-coverage.
Linus
Mathieu Desnoyers
maintainerRe: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 41 lines · ----- On Sep 8, 2019, at 5:51 PM, Linus Torvalds [email protected]…
Quoted 24 lines · Mathieu Desnoyers
kernel/sched/membarrier.c +23
-0+static void sync_runqueues_membarrier_state (struct mm_struct *mm) +{ +int membarrier_state =atomic_read (&mm->membarrier_state); +bool fallback = false; +cpumask_var_t tmpmask; + +if (!zalloc_cpumask_var (&tmpmask,GFP_NOWAIT )) { +/* Fallback for OOM. */ + fallback = true; + } + +/* +* For each cpu runqueue, if the task's mm match @mm, ensure that all +* @mm's membarrier state set bits are also set in in the runqueue's +* membarrier state. This ensures that a runqueue scheduling +* between threads which are users of @mm has its membarrier state +* updated. +*/ +cpus_read_lock (); +rcu_read_lock (); +for_each_online_cpu (cpu) { +struct rq *rq =cpu_rq (cpu); +struct task_struct *p;kernel/sched/membarrier.c static int membarrier_private_expedited(int flags) · line 276
Quoted 10 lines
kernel/sched/membarrier.c +10
-0+ + p =task_rcu_dereference (&rq->curr); +if (p && p->mm == mm) { +if (!fallback) +__cpumask_set_cpu (cpu, tmpmask); +else +smp_call_function_single (cpu, ipi_sync_rq_state, + mm,1 ); + } + }I really absolutely detest this whole "fallback" code.
It will never get any real testing, and the code is just broken.
Quoted 8 lines
Why don't you just use the
mm_cpumask(mm)unconditionally? Yes, it will possibly call too many CPU's, but this fallback code is just completely disgusting.Do a simple and clean implementation. Then, if you can show real performance issues (which I doubt), maybe do something else, but even then you should never do something that will effectively create cases that have absolutely zero test-coverage.
A few points worth mentioning here:
1) As I stated earlier, using mm_cpumask in its current form is not an option for membarrier. For two reasons:
A) The mask is not populated on all architectures (e.g. arm64 does not populate it),
B) Even if it was populated on all architectures, we would need to carefully audit and document every spot where this mm_cpumask is set or cleared within each architecture code, and ensure we have the required memory barriers between user-space memory accesses and those stores, documenting those requirements into each architecture code in the process. This seems to be a lot of useless error-prone code churn.
2) I should actually use GFP_KERNEL rather than GFP_NOWAIT in this membarrier registration code. But it can still fail. However, the other membarrier code using the same fallback pattern (private and global expedited) documents that those membarrier commands do not block in the membarrier(2) man page, so GFP_NOWAIT is appropriate in those cases.
3) Testing-wise, I fully agree with your argument of lacking test coverage. One option I'm considering would be to add a selftest based on the fault-injection infrastructure, which would ensure that we have coverage of the failure case in the kernel selftests.
Thoughts ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 74 lines · Mathieu Desnoyers
Quoted 41 lines · ----- On Sep 8, 2019, at 5:51 PM, Linus Torvalds [email protected]…
Quoted 24 lines · Mathieu Desnoyers
kernel/sched/membarrier.c +23
-0+static void sync_runqueues_membarrier_state (struct mm_struct *mm) +{ +int membarrier_state =atomic_read (&mm->membarrier_state); +bool fallback = false; +cpumask_var_t tmpmask; + +if (!zalloc_cpumask_var (&tmpmask,GFP_NOWAIT )) { +/* Fallback for OOM. */ + fallback = true; + } + +/* +* For each cpu runqueue, if the task's mm match @mm, ensure that all +* @mm's membarrier state set bits are also set in in the runqueue's +* membarrier state. This ensures that a runqueue scheduling +* between threads which are users of @mm has its membarrier state +* updated. +*/ +cpus_read_lock (); +rcu_read_lock (); +for_each_online_cpu (cpu) { +struct rq *rq =cpu_rq (cpu); +struct task_struct *p;kernel/sched/membarrier.c static int membarrier_private_expedited(int flags) · line 276
Quoted 10 lines
kernel/sched/membarrier.c +10
-0+ + p =task_rcu_dereference (&rq->curr); +if (p && p->mm == mm) { +if (!fallback) +__cpumask_set_cpu (cpu, tmpmask); +else +smp_call_function_single (cpu, ipi_sync_rq_state, + mm,1 ); + } + }I really absolutely detest this whole "fallback" code.
It will never get any real testing, and the code is just broken.
Quoted 8 lines
Why don't you just use the
mm_cpumask(mm)unconditionally? Yes, it will possibly call too many CPU's, but this fallback code is just completely disgusting.Do a simple and clean implementation. Then, if you can show real performance issues (which I doubt), maybe do something else, but even then you should never do something that will effectively create cases that have absolutely zero test-coverage.
A few points worth mentioning here:
1) As I stated earlier, using
mm_cpumaskin its current form is not an option for membarrier. For two reasons:A) The mask is not populated on all architectures (e.g. arm64 does not populate it),
B) Even if it was populated on all architectures, we would need to carefully audit and document every spot where this
mm_cpumaskis set or cleared within each architecture code, and ensure we have the required memory barriers between user-space memory accesses and those stores, documenting those requirements into each architecture code in the process. This seems to be a lot of useless error-prone code churn.2) I should actually use
GFP_KERNELrather thanGFP_NOWAITin this membarrier registration code. But it can still fail. However, the other membarrier code using the same fallback pattern (private and global expedited) documents that those membarrier commands do not block in the membarrier(2) man page, soGFP_NOWAITis appropriate in those cases.
Quoted 6 lines
3) Testing-wise, I fully agree with your argument of lacking test coverage. One option I'm considering would be to add a selftest based on the fault-injection infrastructure, which would ensure that we have coverage of the failure case in the kernel selftests.
Thoughts ?
So the man page for sys_membarrier states that the expedited variants "never block", which feels pretty strong. Do any other system calls claim to provide this guarantee without a failure path if blocking is necessary? Given that the whole thing is preemptible, I'm also curious as to how exactly userspace relies on this non-blocking guarantee. I'd have thought that you could either just bite the bullet and block in the rare case that you need to when allocating the cpumask, or you could just return -EWOULDBLOCK on allocation failure, given that I suspect there are very few users of this system call right now and it's not yet supported by glibc afaik.
Will
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 4 lines · Will Deacon
So the man page for
sys_membarrierstates that the expedited variants "never block", which feels pretty strong. Do any other system calls claim to provide this guarantee without a failure path if blocking is necessary?
The traditional semantics for "we don't block" is that "we block on memory allocations and locking and user accesses etc, but we don't wait for our own IO".
So there may be new IO started (and waited on) as part of allocating new memory etc, or in just paging in user memory, but the IO that the operation _itself_ explicitly starts is not waited on.
No system call should ever be considered "atomic" in any sense. If you're doing RT, you should maybe expect "getpid()" to not ever block, but that's just about the exclusive list of truly nonblocking system calls, and even that can be preempted.
Linus
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · Will Deacon
So the man page for
sys_membarrierstates that the expedited variants "never block", which feels pretty strong. Do any other system calls claim to provide this guarantee without a failure path if blocking is necessary?
Quoted 7 lines
The traditional semantics for "we don't block" is that "we block on memory allocations and locking and user accesses etc, but we don't wait for our own IO".
So there may be new IO started (and waited on) as part of allocating new memory etc, or in just paging in user memory, but the IO that the operation _itself_ explicitly starts is not waited on.
Thanks, that makes sense, and I'd be inclined to suggest an update to the sys_membarrier manpage to make this more clear since the "never blocks" phrasing doesn't seem to be used like this for other system calls.
Quoted 4 lines
No system call should ever be considered "atomic" in any sense. If you're doing RT, you should maybe expect "
getpid()" to not ever block, but that's just about the exclusive list of truly nonblocking system calls, and even that can be preempted.
In which case, why can't we just use GFP_KERNEL for the cpumask allocation instead of GFP_NOWAIT and then remove the failure path altogether? Mathieu?
Will
Mathieu Desnoyers
maintainerRe: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 11 lines · ----- On Sep 12, 2019, at 11:47 AM, Will Deacon [email protected] wrote:
Quoted 10 lines · Linus Torvalds
Quoted 4 lines · Will Deacon
So the man page for
sys_membarrierstates that the expedited variants "never block", which feels pretty strong. Do any other system calls claim to provide this guarantee without a failure path if blocking is necessary?The traditional semantics for "we don't block" is that "we block on memory allocations and locking and user accesses etc, but we don't wait for our own IO".
Quoted 7 lines
Quoted 3 lines
So there may be new IO started (and waited on) as part of allocating new memory etc, or in just paging in user memory, but the IO that the operation _itself_ explicitly starts is not waited on.
Thanks, that makes sense, and I'd be inclined to suggest an update to the
sys_membarriermanpage to make this more clear since the "never blocks" phrasing doesn't seem to be used like this for other system calls.
The current wording from membarrier(2) is:
The "expedited" commands complete faster than the non-expedited ones; they never block, but have the downside of causing extra overhead.
We could simply remove the "; they never block" part then ?
Quoted 8 lines
Quoted 5 lines
No system call should ever be considered "atomic" in any sense. If you're doing RT, you should maybe expect "
getpid()" to not ever block, but that's just about the exclusive list of truly nonblocking system calls, and even that can be preempted.In which case, why can't we just use
GFP_KERNELfor the cpumask allocation instead ofGFP_NOWAITand then remove the failure path altogether? Mathieu?
Looking at:
#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS)
I notice that it does not include __GFP_NOFAIL. What prevents GFP_KERNEL from failing, and where is this guarantee documented ?
Regarding __GFP_NOFAIL, its use seems to be discouraged in linux/gfp.h:
* %__GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
* cannot handle allocation failures. The allocation could block
* indefinitely but will never return with failure. Testing for
* failure is pointless.
* New users should be evaluated carefully (and the flag should be
* used only when there is no reasonable failure policy) but it is
* definitely preferable to use the flag rather than opencode endless
* loop around allocator.
* Using this flag for costly allocations is _highly_ discouraged.
So I am reluctant to use it.
But if we can agree on the right combination of flags that guarantees there is no failure, I would be perfectly fine with using them to remove the fallback code.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Hi Mathieu,
Sorry for the delay in responding.
Quoted 21 lines · Mathieu Desnoyers
Quoted 11 lines · ----- On Sep 12, 2019, at 11:47 AM, Will Deacon [email protected] wrote:
Quoted 10 lines · Linus Torvalds
Quoted 4 lines · Will Deacon
So the man page for
sys_membarrierstates that the expedited variants "never block", which feels pretty strong. Do any other system calls claim to provide this guarantee without a failure path if blocking is necessary?The traditional semantics for "we don't block" is that "we block on memory allocations and locking and user accesses etc, but we don't wait for our own IO".
Quoted 7 lines
Quoted 3 lines
So there may be new IO started (and waited on) as part of allocating new memory etc, or in just paging in user memory, but the IO that the operation _itself_ explicitly starts is not waited on.
Thanks, that makes sense, and I'd be inclined to suggest an update to the
sys_membarriermanpage to make this more clear since the "never blocks" phrasing doesn't seem to be used like this for other system calls.
Quoted 7 lines
The current wording from membarrier(2) is:
The "expedited" commands complete faster than the non-expedited ones; they never block, but have the downside of causing extra overhead.
We could simply remove the "; they never block" part then ?
I think so, yes. That or, "; they do not voluntarily block" or something like that. Maybe look at other man pages for inspiration ;)
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
No system call should ever be considered "atomic" in any sense. If you're doing RT, you should maybe expect "
getpid()" to not ever block, but that's just about the exclusive list of truly nonblocking system calls, and even that can be preempted.In which case, why can't we just use
GFP_KERNELfor the cpumask allocation instead ofGFP_NOWAITand then remove the failure path altogether? Mathieu?
Quoted 6 lines
Looking at:
#define
GFP_KERNEL(__GFP_RECLAIM|__GFP_IO|__GFP_FS)I notice that it does not include
__GFP_NOFAIL. What preventsGFP_KERNELfrom failing, and where is this guarantee documented ?
There was an lwn article a little while ago about this:
https://lwn.net/Articles/723317/
I'm not sure what (if anything) has changed in this regard since then, however.
Quoted 12 lines
Regarding
__GFP_NOFAIL, its use seems to be discouraged inlinux/gfp.h:* %__GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
* cannot handle allocation failures. The allocation could block
* indefinitely but will never return with failure. Testing for
* failure is pointless.
* New users should be evaluated carefully (and the flag should be
* used only when there is no reasonable failure policy) but it is
* definitely preferable to use the flag rather than opencode endless
* loop around allocator.
* Using this flag for costly allocations is _highly_ discouraged.
Quoted 5 lines
So I am reluctant to use it.
But if we can agree on the right combination of flags that guarantees there is no failure, I would be perfectly fine with using them to remove the fallback code.
I reckon you'll be fine using GFP_KERNEL and returning -ENOMEM on allocation failure. This shouldn't happen in practice and it removes the fallback path.
Will
Mathieu Desnoyers
maintainerRe: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
----- On Sep 19, 2019, at 12:26 PM, Will Deacon [email protected] wrote:
[...]
Quoted 11 lines
Quoted 8 lines
The current wording from membarrier(2) is:
The "expedited" commands complete faster than the non-expedited ones; they never block, but have the downside of causing extra overhead.
We could simply remove the "; they never block" part then ?
I think so, yes. That or, "; they do not voluntarily block" or something like that. Maybe look at other man pages for inspiration ;)
OK, let's tackle the man-pages part after the fix reaches mainline though.
[...]
Quoted 4 lines
I reckon you'll be fine using
GFP_KERNELand returning-ENOMEMon allocation failure. This shouldn't happen in practice and it removes the fallback path.
Works for me! I'll prepare an updated patchset.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
In reply to Mathieu Desnoyerskernel/sched/membarrier.c static int membarrier_private_expedited(int flags) · line 230
Quoted 10 lines · Mathieu Desnoyers
kernel/sched/membarrier.c +9
-0+static void sync_runqueues_membarrier_state (struct mm_struct *mm) +{ +int membarrier_state =atomic_read (&mm->membarrier_state); +bool fallback = false; +cpumask_var_t tmpmask; +int cpu; + +if (atomic_read (&mm->mm_users) ==1 ||num_online_cpus () ==1 ) { +WRITE_ONCE (this_rq ()->membarrier_state, membarrier_state);
This doesn't look safe, this caller can migrate to another CPU after it calculates the per-cpu ptr.
I think you need do disable preemption or simply use this_cpu_write().
Oleg.
Mathieu Desnoyers
maintainerRe: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 15 lines · ----- On Sep 9, 2019, at 7:00 AM, Oleg Nesterov [email protected] wrote:
kernel/sched/membarrier.c static int membarrier_private_expedited(int flags) · line 230
Quoted 10 lines · Mathieu Desnoyers
kernel/sched/membarrier.c +9
-0+static void sync_runqueues_membarrier_state (struct mm_struct *mm) +{ +int membarrier_state =atomic_read (&mm->membarrier_state); +bool fallback = false; +cpumask_var_t tmpmask; +int cpu; + +if (atomic_read (&mm->mm_users) ==1 ||num_online_cpus () ==1 ) { +WRITE_ONCE (this_rq ()->membarrier_state, membarrier_state);This doesn't look safe, this caller can migrate to another CPU after it calculates the per-cpu ptr.
Quoted 1 line
I think you need do disable preemption or simply use
this_cpu_write().
Good point! I'll use this_cpu_write() there and within membarrier_exec_mmap(), which seems to be affected by the same problem.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 2 lines · Mathieu Desnoyers
membarrier_exec_mmap(), which seems to be affected by the same problem.
IIRC, in the last version it is called by exec_mmap() undef task_lock(), so it should fine.
Oleg.
Mathieu Desnoyers
maintainerRe: [RFC PATCH 4/4] Fix: sched/membarrier: p->mm->membarrier_state racy load (v2)
Quoted 6 lines · ----- On Sep 13, 2019, at 12:04 PM, Oleg Nesterov [email protected] wrote:
Quoted 2 lines · Mathieu Desnoyers
membarrier_exec_mmap(), which seems to be affected by the same problem.IIRC, in the last version it is called by
exec_mmap()undeftask_lock(), so it should fine.
Fair point. Although it seems rather cleaner to use this_cpu_write() in all 3 sites updating this variable rather than a mix of this_cpu_write and WRITE_ONCE(), unless anyone objects.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com