membarrier: expedited private command
Subsystems (2)
Mathieu Desnoyers
maintainerRFC PATCH 1/1 membarrier: expedited private command
Implement MEMBARRIER_CMD_PRIVATE_EXPEDITED with IPIs using cpumask built from all runqueues for which current thread's mm is the same as our own.
Scheduler-wise, it requires that we add a memory barrier after context switching between processes (which have different mm).
It would be interesting to benchmark the overhead of this added barrier on the performance of context switching between processes. If the preexisting overhead of switching between mm is high enough, the overhead of adding this extra barrier may be insignificant.
[ Compile-tested only! ]
--- include/uapi/linux/membarrier.h | 8 +++-- kernel/membarrier.c | 76 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 21 ++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)
include/uapi/linux/membarrier.h +6 -2
@@ -40,14 +40,18 @@* (non-running threads are de facto in such a * state). This covers threads from all processes * running on the system. This command returns 0. +* TODO: documentation. * * Command to be passed to the membarrier system call. The commands need to * be a single bit each, except for MEMBARRIER_CMD_QUERY which is assigned to * the value 0. */ enum membarrier_cmd { -MEMBARRIER_CMD_QUERY =0 , -MEMBARRIER_CMD_SHARED = (1 <<0 ), +MEMBARRIER_CMD_QUERY =0 , +MEMBARRIER_CMD_SHARED = (1 <<0 ), +/* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */ +/* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */ +MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 <<3 ), };#endif /* _UAPI_LINUX_MEMBARRIER_H */
kernel/membarrier.c +75 -1
91 lines not on this page · raw message
kernel/sched/core.c +21 -0
@@ -2724,6 +2724,26 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev)put_user (task_pid_vnr (current), current->set_child_tid); } +#ifdef CONFIG_MEMBARRIER +static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +if (likely (mm == oldmm)) +return ;/* Thread context switch, same mm. */ +/* +* When switching between processes, membarrier expedited +* private requires a memory barrier after we set the current +* task. +*/ +smp_mb (); +} +#else /* #ifdef CONFIG_MEMBARRIER */ +static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +} +#endif /* #else #ifdef CONFIG_MEMBARRIER */ +/* * context_switch - switch to the new MM and the new thread's register state. */
@@ -2737,6 +2757,7 @@ context_switch(struct rq *rq, struct task_struct *prev, mm = next->mm; oldmm = prev->active_mm; +membarrier_expedited_mb_after_set_current (mm, oldmm);/* * For paravirt, this is coupled with an exit in switch_to to * combine the page table reload and the switch backend into
Signature · 1 line
2.11.0
Peter Zijlstra
maintainerkernel/membarrier.c line 24
Quoted 11 lines · Mathieu Desnoyers
kernel/membarrier.c +3
-0@@ -19,10 +19,81 @@#include <linux/tick.h> /* +* XXX For cpu_rq(). Should we rather move +* membarrier_private_expedited() to sched/core.c or create +* sched/membarrier.c ?
The later perhaps.
Quoted 5 lines
kernel/membarrier.c +5
-0+static void membarrier_private_expedited (void ) +{ +int cpu, this_cpu; +cpumask_var_t tmpmask; +
kernel/membarrier.c line 69
Quoted 10 lines
kernel/membarrier.c +10
-0+if (num_online_cpus () ==1 ) +return ; + +/* +* Matches memory barriers around rq->curr modification in +* scheduler. +*/ +smp_mb ();/* system call entry is not a mb. */ + +if (!alloc_cpumask_var (&tmpmask,GFP_NOWAIT )) {
Why GFP_NOWAIT ? and falback. There seems to be a desire to make this a nonblocking syscall. Should we document this somewhere?
kernel/membarrier.c line 75
Quoted 6 lines
kernel/membarrier.c +6
-0+/* Fallback for OOM. */ +membarrier_private_expedited_ipi_each (); + goto end; + } + + this_cpu =raw_smp_processor_id ();
This is a tad dodgy, you might want to put in a comment on how migrating this thread is ok.
kernel/membarrier.c line 46
Quoted 1 line
kernel/membarrier.c +1
-0+for_each_online_cpu (cpu) {
One would also need cpus_read_lock() if you rely on the online mask.
Quoted 22 lines
kernel/membarrier.c +20
-0+struct task_struct *p; + +if (cpu == this_cpu) +continue ; +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +__cpumask_set_cpu (cpu, tmpmask); +rcu_read_unlock (); + } +smp_call_function_many (tmpmask, ipi_mb,NULL ,1 ); +free_cpumask_var (tmpmask); +end: +/* +* Memory barrier on the caller thread _after_ we finished +* waiting for the last IPI. Matches memory barriers around +* rq->curr modification in scheduler. +*/ +smp_mb ();/* exit from system call is not a mb */ +}
kernel/membarrier.c context_switch(struct rq *rq, struct task_struct *prev, · line 2763
Quoted 7 lines
kernel/membarrier.c +1
-0@@ -2737,6 +2757,7 @@ context_switch(struct rq *rq, struct task_struct *prev, mm = next->mm; oldmm = prev->active_mm; +membarrier_expedited_mb_after_set_current (mm, oldmm);/* * For paravirt, this is coupled with an exit in switch_to to * combine the page table reload and the switch backend into
As said on IRC, we have finish_task_switch()->if (mm) mmdrop(mm)->atomic_dec_and_test() providing a smp_mb(). We just need to deal with the !mm case.
Quoted 10 lines · Mathieu Desnoyers
Implement
MEMBARRIER_CMD_PRIVATE_EXPEDITEDwith IPIs using cpumask built from all runqueues for which current thread's mm is the same as our own.Scheduler-wise, it requires that we add a memory barrier after context switching between processes (which have different mm).
It would be interesting to benchmark the overhead of this added barrier on the performance of context switching between processes. If the preexisting overhead of switching between mm is high enough, the overhead of adding this extra barrier may be insignificant.
I sent this along to the people asking for faster sys_membarrier(), CCing you, thank you!
Please see other feedback inline below.
Quoted 17 lines
[ Compile-tested only! ]
CC Peter Zijlstra <[email protected]>CC Paul E. McKenney <[email protected]>CC Boqun Feng <[email protected]>Signed-off-by Mathieu Desnoyers <[email protected]>--- include/uapi/linux/membarrier.h | 8 +++-- kernel/membarrier.c | 76 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 21 ++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)include/uapi/linux/membarrier.h +0
-0
include/uapi/linux/membarrier.h line 43
Quoted 4 lines
include/uapi/linux/membarrier.h +1
-0@@ -40,14 +40,18 @@* (non-running threads are de facto in such a * state). This covers threads from all processes * running on the system. This command returns 0. +* TODO: documentation.
How about something like this?
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED: IPI each CPU running a thread belonging
* to the same process as the current thread, causing * the recipient CPU to execute a full memory-barrier * instruction. The same-process determination is * made using the task_struct ->mm field: If some * other CPU's currently running task has the same * value in its ->mm field as the requesting thread, * that CPU is IPIed.
Quoted 21 lines
include/uapi/linux/membarrier.h +5
-2* * Command to be passed to the membarrier system call. The commands need to * be a single bit each, except for MEMBARRIER_CMD_QUERY which is assigned to * the value 0. */ enum membarrier_cmd { -MEMBARRIER_CMD_QUERY =0 , -MEMBARRIER_CMD_SHARED = (1 <<0 ), +MEMBARRIER_CMD_QUERY =0 , +MEMBARRIER_CMD_SHARED = (1 <<0 ), +/* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */ +/* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */ +MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 <<3 ), };#endif /* _UAPI_LINUX_MEMBARRIER_H */ kernel/membarrier.c +0
-0
kernel/membarrier.c line 24
Quoted 6 lines
kernel/membarrier.c +3
-0@@ -19,10 +19,81 @@#include <linux/tick.h> /* +* XXX For cpu_rq(). Should we rather move +* membarrier_private_expedited() to sched/core.c or create +* sched/membarrier.c ?
Any reason not to move the entirety of membarrier.c there? Assuming that Peter is OK with this, of course.
Quoted 4 lines
kernel/membarrier.c +4
-0+*/ +#include "sched/sched.h" + +/*
kernel/membarrier.c line 37
Quoted 10 lines
kernel/membarrier.c +6
-1* Bitmask made from a "or" of all commands within enum membarrier_cmd, * except MEMBARRIER_CMD_QUERY. */ -#define MEMBARRIER_CMD_BITMASK (MEMBARRIER_CMD_SHARED) +#define MEMBARRIER_CMD_BITMASK \ + (MEMBARRIER_CMD_SHARED | MEMBARRIER_CMD_PRIVATE_EXPEDITED) + +static void ipi_mb (void *info) +{ +smp_mb ();/* IPIs should be serializing but paranoid. */
I am good with paranoia! ;-)
Quoted 89 lines
kernel/membarrier.c +62
-0+} + +static void membarrier_private_expedited_ipi_each (void ) +{ +int cpu; + +for_each_online_cpu (cpu) { +struct task_struct *p; + +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +smp_call_function_single (cpu, ipi_mb,NULL ,1 ); +rcu_read_unlock (); + } +} + +static void membarrier_private_expedited (void ) +{ +int cpu, this_cpu; +cpumask_var_t tmpmask; + +if (num_online_cpus () ==1 ) +return ; + +/* +* Matches memory barriers around rq->curr modification in +* scheduler. +*/ +smp_mb ();/* system call entry is not a mb. */ + +if (!alloc_cpumask_var (&tmpmask,GFP_NOWAIT )) { +/* Fallback for OOM. */ +membarrier_private_expedited_ipi_each (); + goto end; + } + + this_cpu =raw_smp_processor_id (); +for_each_online_cpu (cpu) { +struct task_struct *p; + +if (cpu == this_cpu) +continue ; +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +__cpumask_set_cpu (cpu, tmpmask); +rcu_read_unlock (); + } +smp_call_function_many (tmpmask, ipi_mb,NULL ,1 ); +free_cpumask_var (tmpmask); +end: +/* +* Memory barrier on the caller thread _after_ we finished +* waiting for the last IPI. Matches memory barriers around +* rq->curr modification in scheduler. +*/ +smp_mb ();/* exit from system call is not a mb */ +}/** * sys_membarrier - issue memory barriers on a set of threads @@ -64,6 +135,9 @@ SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)if (num_online_cpus () >1 )synchronize_sched ();return 0 ; +case MEMBARRIER_CMD_PRIVATE_EXPEDITED : +membarrier_private_expedited (); +return 0 ;default :return -EINVAL ; }kernel/sched/core.c +9
-0@@ -2724,6 +2724,26 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev)put_user (task_pid_vnr (current), current->set_child_tid); } +#ifdef CONFIG_MEMBARRIER +static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +if (likely (mm == oldmm)) +return ;/* Thread context switch, same mm. */ +/* +* When switching between processes, membarrier expedited +* private requires a memory barrier after we set the current
kernel/sched/core.c asmlinkage __visible void schedule_tail(struct task_struct *prev) · line 2745
Quoted 10 lines
kernel/sched/core.c +10
-0+* task. +*/ + smp_mb(); +} +#else /* #ifdef CONFIG_MEMBARRIER */ +static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +} +#endif /* #else #ifdef CONFIG_MEMBARRIER */
Why not something like this? Shorter, easier to read, and should generate the same code.
static void membarrier_expedited_mb_after_set_current(struct mm_struct *mm,
struct mm_struct *oldmm)
{
if (!IS_ENABLED(CONFIG_MEMBARRIER))
return;
if (likely(mm == oldmm))
return; /* Thread context switch, same mm. */
/*
* When switching between processes, membarrier expedited
* private requires a memory barrier after we set the current
* task.
*/
smp_mb();
}Quoted 15 lines
kernel/sched/core.c +2
-0+/* * context_switch - switch to the new MM and the new thread's register state. */ @@ -2737,6 +2757,7 @@ context_switch(struct rq *rq, struct task_struct *prev, mm = next->mm; oldmm = prev->active_mm; +membarrier_expedited_mb_after_set_current (mm, oldmm);/* * For paravirt, this is coupled with an exit in switch_to to * combine the page table reload and the switch backend into Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerQuoted 7 lines · ----- On Jul 27, 2017, at 4:06 PM, Paul E. McKenney [email protected]…
Quoted 6 lines · Mathieu Desnoyers
Implement
MEMBARRIER_CMD_PRIVATE_EXPEDITEDwith IPIs using cpumask built from all runqueues for which current thread's mm is the same as our own.Scheduler-wise, it requires that we add a memory barrier after context switching between processes (which have different mm).
Quoted 7 lines
Quoted 4 lines
It would be interesting to benchmark the overhead of this added barrier on the performance of context switching between processes. If the preexisting overhead of switching between mm is high enough, the overhead of adding this extra barrier may be insignificant.
I sent this along to the people asking for faster
sys_membarrier(), CCing you, thank you!
Thanks! More below,
Quoted 27 lines
Please see other feedback inline below.
Quoted 17 lines
[ Compile-tested only! ]
CC Peter Zijlstra <[email protected]>CC Paul E. McKenney <[email protected]>CC Boqun Feng <[email protected]>Signed-off-by Mathieu Desnoyers <[email protected]>--- include/uapi/linux/membarrier.h | 8 +++-- kernel/membarrier.c | 76 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 21 ++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)include/uapi/linux/membarrier.h +0
-0include/uapi/linux/membarrier.h line 43
Quoted 4 lines
include/uapi/linux/membarrier.h +1
-0@@ -40,14 +40,18 @@* (non-running threads are de facto in such a * state). This covers threads from all processes * running on the system. This command returns 0. +* TODO: documentation. How about something like this?
Quoted 9 lines
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED: IPI each CPU running a thread belonging
* to the same process as the current thread, causing * the recipient CPU to execute a full memory-barrier * instruction. The same-process determination is * made using the task_struct ->mm field: If some * other CPU's currently running task has the same * value in its ->mm field as the requesting thread, * that CPU is IPIed.
I would prefer:
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED:
* Execute a memory barrier on each running * thread belonging to the same process as the current * thread. Upon return from system call, the * caller thread is ensured that all its running * threads siblings have passed through a state * where all memory accesses to user-space * addresses match program order between entry * to and return from the system call * (non-running threads are de facto in such a * state). This only covers threads from the * same processes as the caller thread. This * command returns 0.
mainly because this is a uapi/ header, installed into distribution headers. So I want to remove any implementation reference from that text.
Quoted 23 lines
Quoted 23 lines
include/uapi/linux/membarrier.h +5
-2* * Command to be passed to the membarrier system call. The commands need to * be a single bit each, except for MEMBARRIER_CMD_QUERY which is assigned to * the value 0. */ enum membarrier_cmd { -MEMBARRIER_CMD_QUERY =0 , -MEMBARRIER_CMD_SHARED = (1 <<0 ), +MEMBARRIER_CMD_QUERY =0 , +MEMBARRIER_CMD_SHARED = (1 <<0 ), +/* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */ +/* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */ +MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 <<3 ), };#endif /* _UAPI_LINUX_MEMBARRIER_H */ kernel/membarrier.c +0
-0@@ -19,10 +19,81 @@#include <linux/tick.h>
Quoted 7 lines
kernel/membarrier.c line 24
Quoted 4 lines
kernel/membarrier.c +3
-0/* +* XXX For cpu_rq(). Should we rather move +* membarrier_private_expedited() to sched/core.c or create +* sched/membarrier.c ? Any reason not to move the entirety of
membarrier.cthere? Assuming that Peter is OK with this, of course.
I'm planning to do exactly this for v2.
Quoted 113 lines
Quoted 5 lines
kernel/membarrier.c +4
-0+*/ +#include "sched/sched.h" + +/* kernel/membarrier.c line 37
Quoted 10 lines
kernel/membarrier.c +6
-1* Bitmask made from a "or" of all commands within enum membarrier_cmd, * except MEMBARRIER_CMD_QUERY. */ -#define MEMBARRIER_CMD_BITMASK (MEMBARRIER_CMD_SHARED) +#define MEMBARRIER_CMD_BITMASK \ + (MEMBARRIER_CMD_SHARED | MEMBARRIER_CMD_PRIVATE_EXPEDITED) + +static void ipi_mb (void *info) +{ +smp_mb ();/* IPIs should be serializing but paranoid. */ I am good with paranoia! ;-)
Quoted 95 lines
kernel/membarrier.c +62
-0+} + +static void membarrier_private_expedited_ipi_each (void ) +{ +int cpu; + +for_each_online_cpu (cpu) { +struct task_struct *p; + +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +smp_call_function_single (cpu, ipi_mb,NULL ,1 ); +rcu_read_unlock (); + } +} + +static void membarrier_private_expedited (void ) +{ +int cpu, this_cpu; +cpumask_var_t tmpmask; + +if (num_online_cpus () ==1 ) +return ; + +/* +* Matches memory barriers around rq->curr modification in +* scheduler. +*/ +smp_mb ();/* system call entry is not a mb. */ + +if (!alloc_cpumask_var (&tmpmask,GFP_NOWAIT )) { +/* Fallback for OOM. */ +membarrier_private_expedited_ipi_each (); + goto end; + } + + this_cpu =raw_smp_processor_id (); +for_each_online_cpu (cpu) { +struct task_struct *p; + +if (cpu == this_cpu) +continue ; +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +__cpumask_set_cpu (cpu, tmpmask); +rcu_read_unlock (); + } +smp_call_function_many (tmpmask, ipi_mb,NULL ,1 ); +free_cpumask_var (tmpmask); +end: +/* +* Memory barrier on the caller thread _after_ we finished +* waiting for the last IPI. Matches memory barriers around +* rq->curr modification in scheduler. +*/ +smp_mb ();/* exit from system call is not a mb */ +}/** * sys_membarrier - issue memory barriers on a set of threads @@ -64,6 +135,9 @@ SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)if (num_online_cpus () >1 )synchronize_sched ();return 0 ; +case MEMBARRIER_CMD_PRIVATE_EXPEDITED : +membarrier_private_expedited (); +return 0 ;default :return -EINVAL ; }kernel/sched/core.c +14
-0@@ -2724,6 +2724,26 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev) put_user(task_pid_vnr(current), current->set_child_tid); } +#ifdef CONFIG_MEMBARRIER +static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +if (likely (mm == oldmm)) +return ;/* Thread context switch, same mm. */ +/* +* When switching between processes, membarrier expedited +* private requires a memory barrier after we set the current +* task. +*/ +smp_mb (); +} +#else /* #ifdef CONFIG_MEMBARRIER */
Quoted 8 lines
kernel/sched/core.c asmlinkage __visible void schedule_tail(struct task_struct *prev) · line 2745
Quoted 5 lines
kernel/sched/core.c +5
-0+static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +} +#endif /* #else #ifdef CONFIG_MEMBARRIER */ Why not something like this? Shorter, easier to read, and should generate the same code.
Sure, I'll use this in my v2.
Thanks!
Mathieu
Quoted 30 lines
static void membarrier_expedited_mb_after_set_current(struct
mm_struct*mm,
structmm_struct*oldmm){ if (!IS_ENABLED(CONFIG_MEMBARRIER)) return; if (likely(mm == oldmm)) return; /* Thread context switch, same mm. */ /* * When switching between processes, membarrier expedited * private requires a memory barrier after we set the current * task. */ smp_mb(); }Quoted 14 lines
kernel/sched/core.c +1
-0+/* * context_switch - switch to the new MM and the new thread's register state. */
kernel/sched/core.c +1
-1@@ -2737,6 +2757,7 @@ context_switch(struct rq *rq, struct task_struct *prev, mm = next->mm; oldmm = prev->active_mm; +membarrier_expedited_mb_after_set_current (mm, oldmm);/* * For paravirt, this is coupled with an exit in switch_to to * combine the page table reload and the switch backend into -- 2.11.0
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Quoted 15 lines · Mathieu Desnoyers
Quoted 12 lines · ----- On Jul 27, 2017, at 4:06 PM, Paul E. McKenney [email protected]…
Quoted 10 lines · Mathieu Desnoyers
Implement
MEMBARRIER_CMD_PRIVATE_EXPEDITEDwith IPIs using cpumask built from all runqueues for which current thread's mm is the same as our own.Scheduler-wise, it requires that we add a memory barrier after context switching between processes (which have different mm).
It would be interesting to benchmark the overhead of this added barrier on the performance of context switching between processes. If the preexisting overhead of switching between mm is high enough, the overhead of adding this extra barrier may be insignificant.
Quoted 4 lines
Quoted 2 lines
I sent this along to the people asking for faster
sys_membarrier(), CCing you, thank you!Thanks! More below,
At at least one had it pass their initial testing.
Quoted 52 lines
Quoted 26 lines
Please see other feedback inline below.
Quoted 17 lines
[ Compile-tested only! ]
CC Peter Zijlstra <[email protected]>CC Paul E. McKenney <[email protected]>CC Boqun Feng <[email protected]>Signed-off-by Mathieu Desnoyers <[email protected]>--- include/uapi/linux/membarrier.h | 8 +++-- kernel/membarrier.c | 76 ++++++++++++++++++++++++++++++++++++++++- kernel/sched/core.c | 21 ++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-)include/uapi/linux/membarrier.h +0
-0include/uapi/linux/membarrier.h line 43
Quoted 4 lines
include/uapi/linux/membarrier.h +1
-0@@ -40,14 +40,18 @@* (non-running threads are de facto in such a * state). This covers threads from all processes * running on the system. This command returns 0. +* TODO: documentation. How about something like this?
Quoted 9 lines
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED: IPI each CPU running a thread belonging
* to the same process as the current thread, causing * the recipient CPU to execute a full memory-barrier * instruction. The same-process determination is * made using the task_struct ->mm field: If some * other CPU's currently running task has the same * value in its ->mm field as the requesting thread, * that CPU is IPIed.I would prefer:
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED:
* Execute a memory barrier on each running * thread belonging to the same process as the current * thread. Upon return from system call, the * caller thread is ensured that all its running * threads siblings have passed through a state * where all memory accesses to user-space * addresses match program order between entry * to and return from the system call * (non-running threads are de facto in such a * state). This only covers threads from the * same processes as the caller thread. This * command returns 0.
Quoted 2 lines
mainly because this is a uapi/ header, installed into distribution headers. So I want to remove any implementation reference from that text.
Fine by me, as long as there is documentation. ;-)
Quoted 31 lines
Quoted 23 lines
Quoted 23 lines
include/uapi/linux/membarrier.h +5
-2* * Command to be passed to the membarrier system call. The commands need to * be a single bit each, except for MEMBARRIER_CMD_QUERY which is assigned to * the value 0. */ enum membarrier_cmd { -MEMBARRIER_CMD_QUERY =0 , -MEMBARRIER_CMD_SHARED = (1 <<0 ), +MEMBARRIER_CMD_QUERY =0 , +MEMBARRIER_CMD_SHARED = (1 <<0 ), +/* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */ +/* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */ +MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 <<3 ), };#endif /* _UAPI_LINUX_MEMBARRIER_H */ kernel/membarrier.c +0
-0@@ -19,10 +19,81 @@#include <linux/tick.h> Quoted 7 lines
kernel/membarrier.c line 24
Quoted 4 lines
kernel/membarrier.c +3
-0/* +* XXX For cpu_rq(). Should we rather move +* membarrier_private_expedited() to sched/core.c or create +* sched/membarrier.c ? Any reason not to move the entirety of
membarrier.cthere? Assuming that Peter is OK with this, of course.
Quoted 1 line
I'm planning to do exactly this for v2.
Fair enough.
Quoted 121 lines
Quoted 112 lines
Quoted 4 lines
kernel/membarrier.c +4
-0+*/ +#include "sched/sched.h" + +/* kernel/membarrier.c line 37
Quoted 10 lines
kernel/membarrier.c +6
-1* Bitmask made from a "or" of all commands within enum membarrier_cmd, * except MEMBARRIER_CMD_QUERY. */ -#define MEMBARRIER_CMD_BITMASK (MEMBARRIER_CMD_SHARED) +#define MEMBARRIER_CMD_BITMASK \ + (MEMBARRIER_CMD_SHARED | MEMBARRIER_CMD_PRIVATE_EXPEDITED) + +static void ipi_mb (void *info) +{ +smp_mb ();/* IPIs should be serializing but paranoid. */ I am good with paranoia! ;-)
Quoted 95 lines
kernel/membarrier.c +62
-0+} + +static void membarrier_private_expedited_ipi_each (void ) +{ +int cpu; + +for_each_online_cpu (cpu) { +struct task_struct *p; + +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +smp_call_function_single (cpu, ipi_mb,NULL ,1 ); +rcu_read_unlock (); + } +} + +static void membarrier_private_expedited (void ) +{ +int cpu, this_cpu; +cpumask_var_t tmpmask; + +if (num_online_cpus () ==1 ) +return ; + +/* +* Matches memory barriers around rq->curr modification in +* scheduler. +*/ +smp_mb ();/* system call entry is not a mb. */ + +if (!alloc_cpumask_var (&tmpmask,GFP_NOWAIT )) { +/* Fallback for OOM. */ +membarrier_private_expedited_ipi_each (); + goto end; + } + + this_cpu =raw_smp_processor_id (); +for_each_online_cpu (cpu) { +struct task_struct *p; + +if (cpu == this_cpu) +continue ; +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +__cpumask_set_cpu (cpu, tmpmask); +rcu_read_unlock (); + } +smp_call_function_many (tmpmask, ipi_mb,NULL ,1 ); +free_cpumask_var (tmpmask); +end: +/* +* Memory barrier on the caller thread _after_ we finished +* waiting for the last IPI. Matches memory barriers around +* rq->curr modification in scheduler. +*/ +smp_mb ();/* exit from system call is not a mb */ +}/** * sys_membarrier - issue memory barriers on a set of threads @@ -64,6 +135,9 @@ SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)if (num_online_cpus () >1 )synchronize_sched ();return 0 ; +case MEMBARRIER_CMD_PRIVATE_EXPEDITED : +membarrier_private_expedited (); +return 0 ;default :return -EINVAL ; }kernel/sched/core.c +14
-0@@ -2724,6 +2724,26 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev) put_user(task_pid_vnr(current), current->set_child_tid); } +#ifdef CONFIG_MEMBARRIER +static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +if (likely (mm == oldmm)) +return ;/* Thread context switch, same mm. */ +/* +* When switching between processes, membarrier expedited +* private requires a memory barrier after we set the current +* task. +*/ +smp_mb (); +} +#else /* #ifdef CONFIG_MEMBARRIER */ Quoted 8 lines
kernel/sched/core.c asmlinkage __visible void schedule_tail(struct task_struct *prev) · line 2745
Quoted 5 lines
kernel/sched/core.c +5
-0+static void membarrier_expedited_mb_after_set_current (struct mm_struct *mm, +struct mm_struct *oldmm) +{ +} +#endif /* #else #ifdef CONFIG_MEMBARRIER */ Why not something like this? Shorter, easier to read, and should generate the same code.
Quoted 1 line
Sure, I'll use this in my v2.
Sounds good!
Thanx, Paul
Quoted 40 lines
Thanks!
Mathieu
Quoted 30 lines
static void membarrier_expedited_mb_after_set_current(struct
mm_struct*mm,
structmm_struct*oldmm){ if (!IS_ENABLED(CONFIG_MEMBARRIER)) return; if (likely(mm == oldmm)) return; /* Thread context switch, same mm. */ /* * When switching between processes, membarrier expedited * private requires a memory barrier after we set the current * task. */ smp_mb(); }Quoted 14 lines
kernel/sched/core.c +1
-0+/* * context_switch - switch to the new MM and the new thread's register state. */
kernel/sched/core.c +1
-1@@ -2737,6 +2757,7 @@ context_switch(struct rq *rq, struct task_struct *prev, mm = next->mm; oldmm = prev->active_mm; +membarrier_expedited_mb_after_set_current (mm, oldmm);/* * For paravirt, this is coupled with an exit in switch_to to * combine the page table reload and the switch backend into -- 2.11.0
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerQuoted 8 lines · ----- On Jul 27, 2017, at 3:55 PM, Peter Zijlstra [email protected] wrote:
Quoted 7 lines · Mathieu Desnoyers
kernel/membarrier.c +0
-0@@ -19,10 +19,81 @@#include <linux/tick.h>
Quoted 6 lines
kernel/membarrier.c line 24
Quoted 4 lines
kernel/membarrier.c +3
-0/* +* XXX For cpu_rq(). Should we rather move +* membarrier_private_expedited() to sched/core.c or create +* sched/membarrier.c ? The later perhaps.
Allright, will do that in v2.
Quoted 11 lines
Quoted 11 lines
kernel/membarrier.c +10
-0+static void membarrier_private_expedited (void ) +{ +int cpu, this_cpu; +cpumask_var_t tmpmask; + +if (num_online_cpus () ==1 ) +return ; + +/* +* Matches memory barriers around rq->curr modification in
Quoted 8 lines
kernel/membarrier.c line 69
Quoted 5 lines
kernel/membarrier.c +5
-0+* scheduler. +*/ +smp_mb ();/* system call entry is not a mb. */ + +if (!alloc_cpumask_var (&tmpmask,GFP_NOWAIT )) {Why
GFP_NOWAIT? and falback. There seems to be a desire to make this a nonblocking syscall. Should we document this somewhere?
blocking a synchronization system call due to memory allocation pressure seemed like an unwanted effect back in 2010, so I kept the same approach. Perhaps we could state that all the "expedited" commands should be non-blocking ?
Quoted 10 lines
kernel/membarrier.c line 75
Quoted 7 lines
kernel/membarrier.c +6
-0+/* Fallback for OOM. */ +membarrier_private_expedited_ipi_each (); + goto end; + } + + this_cpu =raw_smp_processor_id ();This is a tad dodgy, you might want to put in a comment on how migrating this thread is ok.
How about this ?
/*
* Skipping the current CPU is OK even through we can be
* migrated at any point. The current CPU, at the point where we
* read raw_smp_processor_id(), is ensured to be in program
* order with respect to the caller thread. Therefore, we can
* skip this CPU from the iteration.
*/
Quoted 4 lines
kernel/membarrier.c line 46
Quoted 2 lines
kernel/membarrier.c +1
-0+for_each_online_cpu (cpu) {One would also need
cpus_read_lock()if you rely on the online mask.
OK.
Quoted 24 lines
Quoted 24 lines
kernel/membarrier.c +20
-0+struct task_struct *p; + +if (cpu == this_cpu) +continue ; +rcu_read_lock (); + p =task_rcu_dereference (&cpu_rq (cpu)->curr); +if (p && p->mm == current->mm) +__cpumask_set_cpu (cpu, tmpmask); +rcu_read_unlock (); + } +smp_call_function_many (tmpmask, ipi_mb,NULL ,1 ); +free_cpumask_var (tmpmask); +end: +/* +* Memory barrier on the caller thread _after_ we finished +* waiting for the last IPI. Matches memory barriers around +* rq->curr modification in scheduler. +*/ +smp_mb ();/* exit from system call is not a mb */ +}@@ -2737,6 +2757,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
Quoted 10 lines
kernel/membarrier.c context_switch(struct rq *rq, struct task_struct *prev, · line 2763
Quoted 6 lines
kernel/membarrier.c +1
-0mm = next->mm; oldmm = prev->active_mm; +membarrier_expedited_mb_after_set_current (mm, oldmm);/* * For paravirt, this is coupled with an exit in switch_to to * combine the page table reload and the switch backend into As said on IRC, we have finish_task_switch()->if (mm) mmdrop(mm)->atomic_dec_and_test() providing a
smp_mb(). We just need to deal with the !mm case.
Yes, I have a v2 brewing that includes this change :)
Thanks!
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com