membarrier updates for 4.15
Mathieu Desnoyers
maintainercover membarrier updates for 4.15
Those are the membarrier changes I plan to submit for 4.15.
This series includes selftests improvements for sys_membarrier, improvement of powerpc handling of the memory barrier required by sys_membarrier in switch_mm(), and adds a new core serializing membarrier, currently only implemented on x86. Architectures wishing to provide the core serializing membarrier need to select ARCH_HAS_MEMBARRIER_SYNC_CORE and document how they provide the core serialization required by that command in their architecture code.
I included the x86 core serializing migration fix into that series, given that this fix and membarrier sync_core both depend on the new sync_core_before_usermode() architecture function.
Feedback is welcome,
Thanks,
Mathieu
Mathieu Desnoyers (10):
membarrier: selftest: Test private expedited cmd
membarrier: powerpc: Skip memory barrier in switch_mm() (v7)
membarrier: Document scheduler barrier requirements (v5)
membarrier: Provide core serializing command
x86: Introduce sync_core_before_usermode
Fix: x86: Add missing core serializing instruction on migration
membarrier: x86: Provide core serializing command (v2)
membarrier: selftest: Test private expedited sync core cmd
membarrier: provide SHARED_EXPEDITED command
membarrier: selftest: Test shared expedited cmd
MAINTAINERS | 1 + arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/membarrier.h | 26 +++ arch/powerpc/mm/mmu_context.c | 7 + arch/x86/Kconfig | 2 + arch/x86/entry/entry_32.S | 5 + arch/x86/entry/entry_64.S | 8 + arch/x86/include/asm/processor.h | 10 + arch/x86/mm/tlb.c | 6 + include/linux/processor.h | 6 + include/linux/sched/mm.h | 38 +++- include/uapi/linux/membarrier.h | 66 +++++- init/Kconfig | 6 + kernel/sched/core.c | 60 ++++-- kernel/sched/membarrier.c | 169 +++++++++++++-- kernel/sched/sched.h | 1 + .../testing/selftests/membarrier/membarrier_test.c | 233 +++++++++++++++++++-- 17 files changed, 584 insertions(+), 61 deletions(-) create mode 100644 arch/powerpc/include/asm/membarrier.h
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 1/10 membarrier: selftest: Test private expedited cmd
Test the new MEMBARRIER_CMD_PRIVATE_EXPEDITED and MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED commands.
Add checks expecting specific error values on system calls expected to fail.
--- .../testing/selftests/membarrier/membarrier_test.c | 109 ++++++++++++++++++--- 1 file changed, 94 insertions(+), 15 deletions(-)
tools/testing/selftests/membarrier/membarrier_test.c +94 -15
@@ -16,49 +16,119 @@ static int sys_membarrier(int cmd, int flags)static int test_membarrier_cmd_fail (void ) {int cmd =-1 , flags =0 ; +const char *test_name ="sys membarrier invalid command" ;if (sys_membarrier (cmd, flags) !=-1 ) {ksft_exit_fail_msg ( -"sys membarrier invalid command test: command = %d, flags = %d. Should fail, but passed\n" , - cmd, flags); +"%s test: command = %d, flags = %d. Should fail, but passed\n" , + test_name, cmd, flags); + } +if (errno !=EINVAL ) { +ksft_exit_fail_msg ( +"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n" , + test_name, flags,EINVAL ,strerror (EINVAL ), + errno,strerror (errno)); }ksft_test_result_pass ( -"sys membarrier invalid command test: command = %d, flags = %d. Failed as expected\n" , - cmd, flags); +"%s test: command = %d, flags = %d, errno = %d. Failed as expected\n" , + test_name, cmd, flags, errno);return 0 ; }static int test_membarrier_flags_fail (void ) {int cmd =MEMBARRIER_CMD_QUERY , flags =1 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_QUERY invalid flags" ;if (sys_membarrier (cmd, flags) !=-1 ) {ksft_exit_fail_msg ( -"sys membarrier MEMBARRIER_CMD_QUERY invalid flags test: flags = %d. Should fail, but passed\n" , - flags); +"%s test: flags = %d. Should fail, but passed\n" , + test_name, flags); + } +if (errno !=EINVAL ) { +ksft_exit_fail_msg ( +"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n" , + test_name, flags,EINVAL ,strerror (EINVAL ), + errno,strerror (errno)); }ksft_test_result_pass ( -"sys membarrier MEMBARRIER_CMD_QUERY invalid flags test: flags = %d. Failed as expected\n" , - flags); +"%s test: flags = %d, errno = %d. Failed as expected\n" , + test_name, flags, errno);return 0 ; } -static int test_membarrier_success (void ) +static int test_membarrier_shared_success (void ) {int cmd =MEMBARRIER_CMD_SHARED , flags =0 ; -const char *test_name ="sys membarrier MEMBARRIER_CMD_SHARED\n" ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_SHARED" ; + +if (sys_membarrier (cmd, flags) !=0 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); + } + +ksft_test_result_pass ( +"%s test: flags = %d\n" , test_name, flags); +return 0 ; +} + +static int test_membarrier_private_expedited_fail (void ) +{ +int cmd =MEMBARRIER_CMD_PRIVATE_EXPEDITED , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED not registered failure" ; + +if (sys_membarrier (cmd, flags) !=-1 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d. Should fail, but passed\n" , + test_name, flags); + } +if (errno !=EPERM ) { +ksft_exit_fail_msg ( +"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n" , + test_name, flags,EPERM ,strerror (EPERM ), + errno,strerror (errno)); + } + +ksft_test_result_pass ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); +return 0 ; +} + +static int test_membarrier_register_private_expedited_success (void ) +{ +int cmd =MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED" ;if (sys_membarrier (cmd, flags) !=0 ) {ksft_exit_fail_msg ( -"sys membarrier MEMBARRIER_CMD_SHARED test: flags = %d\n" , - flags); +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); }ksft_test_result_pass ( -"sys membarrier MEMBARRIER_CMD_SHARED test: flags = %d\n" , - flags); +"%s test: flags = %d\n" , + test_name, flags); +return 0 ; +} + +static int test_membarrier_private_expedited_success (void ) +{ +int cmd =MEMBARRIER_CMD_PRIVATE_EXPEDITED , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED" ; + +if (sys_membarrier (cmd, flags) !=0 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); + } + +ksft_test_result_pass ( +"%s test: flags = %d\n" , + test_name, flags);return 0 ; }
@@ -72,7 +142,16 @@ static int test_membarrier(void) status =test_membarrier_flags_fail ();if (status)return status; - status =test_membarrier_success (); + status =test_membarrier_shared_success (); +if (status) +return status; + status =test_membarrier_private_expedited_fail (); +if (status) +return status; + status =test_membarrier_register_private_expedited_success (); +if (status) +return status; + status =test_membarrier_private_expedited_success ();if (status)return status;return 0 ;
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH v7 2/10 membarrier: powerpc: Skip memory barrier in switch_mm()
In reply to Mathieu DesnoyersAllow PowerPC to skip the full memory barrier in switch_mm(), and only issue the barrier when scheduling into a task belonging to a process that has registered to use expedited private.
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 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_PRIVATE_EXPEDITED 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_PRIVATE_EXPEDITED flag.
Changes since v1:
- Use test_ti_thread_flag(next, ...) instead of test_thread_flag() in powerpc membarrier_arch_sched_in(), given that we want to specifically check the next thread state.
- Add missing ARCH_HAS_MEMBARRIER_HOOKS in Kconfig.
- Use task_thread_info() to pass thread_info from task to *_ti_thread_flag().
Changes since v2:
- Move membarrier_arch_sched_in() call to finish_task_switch().
- Check for NULL t->mm in membarrier_arch_fork().
- Use membarrier_sched_in() in generic code, which invokes the arch-specific membarrier_arch_sched_in(). This fixes allnoconfig build on PowerPC.
- Move asm/membarrier.h include under CONFIG_MEMBARRIER, fixing allnoconfig build on PowerPC.
- Build and runtime tested on PowerPC.
Changes since v3:
- Simply rely on copy_mm() to copy the membarrier_private_expedited mm field on fork.
- powerpc: test thread flag instead of reading membarrier_private_expedited in membarrier_arch_fork().
- powerpc: skip memory barrier in membarrier_arch_sched_in() if coming from kernel thread, since mmdrop() implies a full barrier.
- Set membarrier_private_expedited to 1 only after arch registration code, thus eliminating a race where concurrent commands could succeed when they should fail if issued concurrently with process registration.
- Use READ_ONCE() for membarrier_private_expedited field access in membarrier_private_expedited. Matches WRITE_ONCE() performed in process registration.
Changes since v4:
- Move powerpc hook from sched_in() to switch_mm(), based on feedback from Nicholas Piggin.
Changes since v5:
- Rebase on v4.14-rc6.
- Fold "Fix: membarrier: Handle CLONE_VM + !CLONE_THREAD correctly on powerpc (v2)"
Changes since v6:
- Rename MEMBARRIER_STATE_SWITCH_MM to MEMBARRIER_STATE_PRIVATE_EXPEDITED.
--- MAINTAINERS | 1 + arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/membarrier.h | 25 +++++++++++++++++++++++++ arch/powerpc/mm/mmu_context.c | 7 +++++++ include/linux/sched/mm.h | 12 +++++++++++- init/Kconfig | 3 +++ kernel/sched/core.c | 10 ---------- kernel/sched/membarrier.c | 9 +++++++++ 8 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 arch/powerpc/include/asm/membarrier.h
MAINTAINERS +1 -0
@@ -8837,6 +8837,7 @@ L: [email protected] S: Supported F: kernel/sched/membarrier.c F: include/uapi/linux/membarrier.h +F: arch/powerpc/include/asm/membarrier.h MEMORY MANAGEMENT L: [email protected]
arch/powerpc/Kconfig +1 -0
@@ -139,6 +139,7 @@ config PPC select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL + select ARCH_HAS_MEMBARRIER_HOOKS select ARCH_HAS_SCALED_CPUTIME if VIRT_CPU_ACCOUNTING_NATIVE select ARCH_HAS_SG_CHAIN select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
arch/powerpc/include/asm/membarrier.h new file +25 -0
@@ -0,0 +1,25 @@ +#ifndef _ASM_POWERPC_MEMBARRIER_H +#define _ASM_POWERPC_MEMBARRIER_H + +static inline void membarrier_arch_switch_mm (struct mm_struct *prev, +struct mm_struct *next,struct task_struct *tsk) +{ +/* +* Only need the full barrier when switching between processes. +* Barrier when switching from kernel to userspace is not +* required here, given that it is implied by mmdrop(). Barrier +* when switching from userspace to kernel is not needed after +* store to rq->curr. +*/ +if (likely (!(atomic_read (&next->membarrier_state) + &MEMBARRIER_STATE_PRIVATE_EXPEDITED ) || !prev)) +return ; + +/* +* The membarrier system call requires a full memory barrier +* after storing to rq->curr, before going back to user-space. +*/ +smp_mb (); +} + +#endif /* _ASM_POWERPC_MEMBARRIER_H */
arch/powerpc/mm/mmu_context.c +7 -0
@@ -12,6 +12,7 @@#include <linux/mm.h> #include <linux/cpu.h> +#include <linux/sched/mm.h> #include <asm/mmu_context.h>
@@ -67,6 +68,10 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,* * On the read side the barrier is in pte_xchg(), which orders * the store to the PTE vs the load of mm_cpumask. +* +* This full barrier is needed by membarrier when switching +* between processes after store to rq->curr, before user-space +* memory accesses. */ smp_mb ();
@@ -89,6 +94,8 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,if (new_on_cpu)radix_kvm_prefetch_workaround (next); +else +membarrier_arch_switch_mm (prev, next, tsk);/* * The actual HW switching method differs between the various
include/linux/sched/mm.h +11 -1
@@ -215,14 +215,24 @@ 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 ), +MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U <<1 ), }; +#ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS +#include <asm/membarrier.h> +#endif +static inline void membarrier_execve (struct task_struct *t) {atomic_set (&t->mm->membarrier_state,0 ); }#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_execve (struct task_struct *t) { }
init/Kconfig +3 -0
@@ -1400,6 +1400,9 @@ config MEMBARRIER If unsure, say Y. +config ARCH_HAS_MEMBARRIER_HOOKS + bool + config RSEQ bool "Enable rseq() system call" if EXPERT default y
kernel/sched/core.c +0 -10
@@ -2684,16 +2684,6 @@ static struct rq *finish_task_switch(struct task_struct *prev) prev_state = prev->state;vtime_task_switch (prev);perf_event_task_sched_in (prev, current); -/* -* The membarrier system call requires a full memory barrier -* after storing to rq->curr, before going back to user-space. -* -* TODO: This smp_mb__after_unlock_lock can go away if PPC end -* up adding a full barrier to switch_mm(), or we should figure -* out if a smp_mb__after_unlock_lock is really the proper API -* to use. -*/ -smp_mb__after_unlock_lock ();finish_lock_switch (rq, prev);finish_arch_post_lock_switch ();
kernel/sched/membarrier.c +9 -0
@@ -116,6 +116,15 @@ static void membarrier_register_private_expedited(void)if (atomic_read (&mm->membarrier_state) &MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY )return ; +atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED , + &mm->membarrier_state); +if (!(atomic_read (&mm->mm_users) ==1 &&get_nr_threads (p) ==1 )) { +/* +* Ensure all future scheduler executions will observe the +* new thread flag state for this process. +*/ +synchronize_sched (); + }atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY , &mm->membarrier_state); }
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH v5 3/10 membarrier: Document scheduler barrier requirements
In reply to Mathieu DesnoyersDocument the membarrier requirement on having a full memory barrier in __schedule() after coming from user-space, before storing to rq->curr. It is provided by smp_mb__after_spinlock() in __schedule().
Document that membarrier requires a full barrier on transition from kernel thread to userspace thread. We currently have an implicit barrier from atomic_dec_and_test() in mmdrop() that ensures this.
The x86 switch_mm_irqs_off() full barrier is currently provided by many cpumask update operations as well as write_cr3(). Document that write_cr3() provides this barrier.
Changes since v1:
- Update comments to match reality for code paths which are after storing to rq->curr, before returning to user-space, based on feedback from Andrea Parri.
Changes since v2:
- Update changelog (smp_mb__before_spinlock -> smp_mb__after_spinlock). Based on feedback from Andrea Parri.
Changes since v3:
- Clarify comments following feeback from Peter Zijlstra. Changes since v4:
- Update comment regarding powerpc barrier.
--- arch/x86/mm/tlb.c | 5 +++++ include/linux/sched/mm.h | 5 +++++ kernel/sched/core.c | 37 ++++++++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 11 deletions(-)
arch/x86/mm/tlb.c +5 -0
@@ -146,6 +146,11 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,#endif this_cpu_write (cpu_tlbstate.is_lazy, false); +/* +* The membarrier system call requires a full memory barrier +* before returning to user-space, after storing to rq->curr. +* Writing to CR3 provides that full memory barrier. +*/ if (real_prev == next) {VM_WARN_ON (this_cpu_read (cpu_tlbstate.ctxs[prev_asid].ctx_id) != next->context.ctx_id);
include/linux/sched/mm.h +5 -0
@@ -39,6 +39,11 @@ static inline void mmgrab(struct mm_struct *mm)extern void __mmdrop (struct mm_struct *);static inline void mmdrop (struct mm_struct *mm) { +/* +* The implicit full barrier implied by atomic_dec_and_test is +* required by the membarrier system call before returning to +* user-space, after storing to rq->curr. +*/ if (unlikely (atomic_dec_and_test (&mm->mm_count)))__mmdrop (mm); }
kernel/sched/core.c +26 -11
@@ -2688,6 +2688,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)finish_arch_post_lock_switch ();fire_sched_in_preempt_notifiers (current); +/* +* When transitioning from a kernel thread to a userspace +* thread, mmdrop()'s implicit full barrier is required by the +* membarrier system call, because the current active_mm can +* become the current mm without going through switch_mm(). +*/ if (mm)mmdrop (mm);if (unlikely (prev_state ==TASK_DEAD )) {
@@ -2793,6 +2799,13 @@ context_switch(struct rq *rq, struct task_struct *prev,*/ arch_start_context_switch (prev); +/* +* If mm is non-NULL, we pass through switch_mm(). If mm is +* NULL, we will pass through mmdrop() in finish_task_switch(). +* Both of these contain the full memory barrier required by +* membarrier after storing to rq->curr, before returning to +* user-space. +*/ if (!mm) { next->active_mm = oldmm;mmgrab (oldmm);
@@ -3329,6 +3342,9 @@ static void __sched notrace __schedule(bool preempt)* Make sure that signal_pending_state()->signal_pending() below * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) * done by the caller to avoid the race with signal_wake_up(). +* +* The membarrier system call requires a full memory barrier +* after coming from user-space, before storing to rq->curr. */ rq_lock (rq, &rf);smp_mb__after_spinlock ();
@@ -3377,17 +3393,16 @@ static void __sched notrace __schedule(bool preempt)/* * The membarrier system call requires each architecture * to have a full memory barrier after updating -* rq->curr, before returning to user-space. For TSO -* (e.g. x86), the architecture must provide its own -* barrier in switch_mm(). For weakly ordered machines -* for which spin_unlock() acts as a full memory -* barrier, finish_lock_switch() in common code takes -* care of this barrier. For weakly ordered machines for -* which spin_unlock() acts as a RELEASE barrier (only -* arm64 and PowerPC), arm64 has a full barrier in -* switch_to(), and PowerPC has -* smp_mb__after_unlock_lock() before -* finish_lock_switch(). +* rq->curr, before returning to user-space. +* +* Here are the schemes providing that barrier on the +* various architectures: +* - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC. +* switch_mm() rely on membarrier_arch_switch_mm() on PowerPC. +* - finish_lock_switch() for weakly-ordered +* architectures where spin_unlock is a full barrier, +* - switch_to() for arm64 (weakly-ordered, spin_unlock +* is a RELEASE barrier), */ ++*switch_count;
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 4/10 membarrier: Provide core serializing command
In reply to Mathieu DesnoyersProvide core serializing membarrier command to support memory reclaim by JIT.
Each architecture needs to explicitly opt into that support by documenting in their architecture code how they provide the core serializing instructions required when returning from the membarrier IPI, and after the scheduler has updated the curr->mm pointer (before going back to user-space). They should then define ARCH_HAS_MEMBARRIER_SYNC_CORE to enable support for that command on their architecture.
--- include/linux/sched/mm.h | 9 +++++-- include/uapi/linux/membarrier.h | 40 ++++++++++++++++++++++++++---- init/Kconfig | 3 +++ kernel/sched/membarrier.c | 54 ++++++++++++++++++++++++++++++----------- 4 files changed, 85 insertions(+), 21 deletions(-)
include/linux/sched/mm.h +7 -2
@@ -219,8 +219,13 @@ static inline void memalloc_noreclaim_restore(unsigned int flags)#ifdef CONFIG_MEMBARRIER enum { -MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U <<0 ), -MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U <<1 ), +MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U <<0 ), +MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U <<1 ), +MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U <<2 ), +}; + +enum { +MEMBARRIER_FLAG_SYNC_CORE = (1U <<0 ), };#ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS
include/uapi/linux/membarrier.h +35 -5
@@ -51,7 +51,7 @@* 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 +* same process as the caller thread. This * command returns 0 on success. The * "expedited" commands complete faster than * the non-expedited ones, they never block,
@@ -64,18 +64,48 @@* Register the process intent to use * MEMBARRIER_CMD_PRIVATE_EXPEDITED. Always * returns 0. +* @MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE: +* In addition to provide memory ordering +* guarantees described in +* MEMBARRIER_CMD_PRIVATE_EXPEDITED, ensure +* the caller thread, upon return from system +* call, that all its running threads siblings +* have executed a core serializing +* instruction. (architectures are required to +* guarantee that non-running threads issue +* core serializing instructions before they +* resume user-space execution). This only +* covers threads from the same process as the +* caller thread. This command returns 0 on +* success. The "expedited" commands complete +* faster than the non-expedited ones, they +* never block, but have the downside of +* causing extra overhead. If this command is +* not implemented by an architecture, -EINVAL +* is returned. A process needs to register its +* intent to use the private expedited sync +* core command prior to using it, otherwise +* this command returns -EPERM. +* @MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE: +* Register the process intent to use +* MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE. +* If this command is not implemented by an +* architecture, -EINVAL is returned. +* Returns 0 on success. * * 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 ), -MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 <<4 ), +MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 <<3 ), +MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 <<4 ), +MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE = (1 <<5 ), +MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE = (1 <<6 ), };#endif /* _UAPI_LINUX_MEMBARRIER_H */
init/Kconfig +3 -0
@@ -1403,6 +1403,9 @@ config MEMBARRIER config ARCH_HAS_MEMBARRIER_HOOKS bool +config ARCH_HAS_MEMBARRIER_SYNC_CORE + bool + config RSEQ bool "Enable rseq() system call" if EXPERT default y
kernel/sched/membarrier.c +40 -14
101 lines not on this page · raw message
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 5/10 x86: Introduce sync_core_before_usermode
In reply to Mathieu DesnoyersIntroduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "sync_core" command.
Architectures defining the sync_core_before_usermode() static inline need to define ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)
arch/x86/Kconfig +1 -0
@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHG
arch/x86/include/asm/processor.h +10 -0
@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif } +/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +} +extern void select_idle_routine (const struct cpuinfo_x86 *c);extern void amd_e400_c1e_apic_setup (void );
include/linux/processor.h +6 -0
@@ -68,4 +68,10 @@ do { \#endif +#ifndef ARCH_HAS_SYNC_CORE_BEFORE_USERMODE +static inline sync_core_before_usermode (void) +{ +} +#endif +#endif /* _LINUX_PROCESSOR_H */
Signature · 1 line
2.11.0
Andy Lutomirski
maintainerRe: [RFC PATCH for 4.15 05/10] x86: Introduce sync_core_before_usermode
Quoted 57 lines · Mathieu Desnoyers
Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "
sync_core" command.Architectures defining the
sync_core_before_usermode()static inline need to defineARCH_HAS_SYNC_CORE_BEFORE_USERMODE.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Andy Lutomirski <[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 Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andrea Parri <[email protected]>CC Russell King <[email protected]>CC Greg Hackmann <[email protected]>CC Will Deacon <[email protected]>CC David Sehr <[email protected]>CC Linus Torvalds <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHGarch/x86/include/asm/processor.h +0
-0@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif }
arch/x86/include/asm/processor.h static inline void sync_core(void) · line 717
Quoted 10 lines
arch/x86/include/asm/processor.h +9
-0+/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +}
Make this if (!in_interrupt()) sync_core(); please. We can optimize it better later on.
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 4.15 05/10] x86: Introduce sync_core_before_usermode
Quoted 60 lines · ----- On Nov 10, 2017, at 5:02 PM, Andy Lutomirski [email protected] wrote:
Quoted 58 lines · Mathieu Desnoyers
Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "
sync_core" command.Architectures defining the
sync_core_before_usermode()static inline need to defineARCH_HAS_SYNC_CORE_BEFORE_USERMODE.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Andy Lutomirski <[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 Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andrea Parri <[email protected]>CC Russell King <[email protected]>CC Greg Hackmann <[email protected]>CC Will Deacon <[email protected]>CC David Sehr <[email protected]>CC Linus Torvalds <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHGarch/x86/include/asm/processor.h +0
-0@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif }
Quoted 12 lines
arch/x86/include/asm/processor.h static inline void sync_core(void) · line 717
Quoted 9 lines
arch/x86/include/asm/processor.h +9
-0+/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +}Make this if (!in_interrupt())
sync_core(); please. We can optimize it better later on.
Sure, done. It will be part of the next version of that patch.
Thanks!
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 4.15 05/10] x86: Introduce sync_core_before_usermode
Quoted 75 lines · ----- On Nov 10, 2017, at 5:20 PM, Mathieu Desnoyers…
Quoted 60 lines · ----- On Nov 10, 2017, at 5:02 PM, Andy Lutomirski [email protected] wrote:
Quoted 58 lines · Mathieu Desnoyers
Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "
sync_core" command.Architectures defining the
sync_core_before_usermode()static inline need to defineARCH_HAS_SYNC_CORE_BEFORE_USERMODE.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Andy Lutomirski <[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 Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andrea Parri <[email protected]>CC Russell King <[email protected]>CC Greg Hackmann <[email protected]>CC Will Deacon <[email protected]>CC David Sehr <[email protected]>CC Linus Torvalds <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHGarch/x86/include/asm/processor.h +0
-0@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif }Quoted 12 lines
arch/x86/include/asm/processor.h static inline void sync_core(void) · line 717
Quoted 9 lines
arch/x86/include/asm/processor.h +9
-0+/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +}Make this if (!in_interrupt())
sync_core(); please. We can optimize it better later on.
Quoted 1 line
Sure, done. It will be part of the next version of that patch.
Of course, using in_interrupt() from linux/interrupt.h from asm/processor.h is not such a good idea (circular dependency).
Any recommendation on where to move that static inline on x86 ?
Thanks,
Mathieu
Quoted 10 lines
Thanks!
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 4.15 05/10] x86: Introduce sync_core_before_usermode
Quoted 78 lines · ----- On Nov 10, 2017, at 5:32 PM, Mathieu Desnoyers…
----- On Nov 10, 2017, at 5:20 PM, Mathieu Desnoyers
Quoted 75 lines · [email protected] wrote:
Quoted 72 lines · ----- On Nov 10, 2017, at 5:02 PM, Andy Lutomirski [email protected] wrote:
Quoted 57 lines · Mathieu Desnoyers
Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "
sync_core" command.Architectures defining the
sync_core_before_usermode()static inline need to defineARCH_HAS_SYNC_CORE_BEFORE_USERMODE.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Andy Lutomirski <[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 Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andrea Parri <[email protected]>CC Russell King <[email protected]>CC Greg Hackmann <[email protected]>CC Will Deacon <[email protected]>CC David Sehr <[email protected]>CC Linus Torvalds <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHGarch/x86/include/asm/processor.h +0
-0@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif }arch/x86/include/asm/processor.h static inline void sync_core(void) · line 717
Quoted 10 lines
arch/x86/include/asm/processor.h +9
-0+/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +}Make this if (!in_interrupt())
sync_core(); please. We can optimize it better later on.
Quoted 6 lines
Quoted 1 line
Sure, done. It will be part of the next version of that patch.
Of course, using
in_interrupt()fromlinux/interrupt.hfromasm/processor.his not such a good idea (circular dependency).Any recommendation on where to move that static inline on x86 ?
Actually, for the needs of the fix, I plan on not putting the if (!in_interrupt()) check, given that the only caller will be the scheduler, not in an interrupt context.
We can always revisit this optimization-wise if we end up using in interrupt handlers in the future.
Is that ok with you ?
Thanks,
Mathieu
Quoted 21 lines
Thanks,
Mathieu
Quoted 10 lines
Thanks!
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH for 4.15 05/10] x86: Introduce sync_core_before_usermode
In reply to Mathieu DesnoyersQuoted 80 lines · Mathieu Desnoyers
Quoted 60 lines · ----- On Nov 10, 2017, at 5:02 PM, Andy Lutomirski [email protected] wrote:
Quoted 58 lines · Mathieu Desnoyers
Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "
sync_core" command.Architectures defining the
sync_core_before_usermode()static inline need to defineARCH_HAS_SYNC_CORE_BEFORE_USERMODE.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Andy Lutomirski <[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 Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andrea Parri <[email protected]>CC Russell King <[email protected]>CC Greg Hackmann <[email protected]>CC Will Deacon <[email protected]>CC David Sehr <[email protected]>CC Linus Torvalds <[email protected]>---
arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHGarch/x86/include/asm/processor.h +0
-0@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif }Quoted 12 lines
arch/x86/include/asm/processor.h static inline void sync_core(void) · line 717
Quoted 9 lines
arch/x86/include/asm/processor.h +9
-0+/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +}Make this if (!in_interrupt())
sync_core(); please. We can optimize it better later on.
Quoted 3 lines
Sure, done. It will be part of the next version of that patch.
Thanks!
Hmm. Can you also cc stable and make sure it applies by itself?
Quoted 12 lines
Mathieu
Signature · 7 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com -- To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Mathieu Desnoyers
maintainerRe: [RFC PATCH for 4.15 05/10] x86: Introduce sync_core_before_usermode
Quoted 78 lines · ----- On Nov 10, 2017, at 5:36 PM, Andy Lutomirski [email protected] wrote:
Quoted 78 lines
On Nov 10, 2017, at 2:20 PM, Mathieu Desnoyers <[email protected]> wrote:
Quoted 72 lines · ----- On Nov 10, 2017, at 5:02 PM, Andy Lutomirski [email protected] wrote:
Quoted 57 lines · Mathieu Desnoyers
Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.
This is needed to fix an existing core serialization bug on thread migration, and also needed by the membarrier "
sync_core" command.Architectures defining the
sync_core_before_usermode()static inline need to defineARCH_HAS_SYNC_CORE_BEFORE_USERMODE.Signed-off-by Mathieu Desnoyers <[email protected]>CC Peter Zijlstra <[email protected]>CC Andy Lutomirski <[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 Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>CC Andrea Parri <[email protected]>CC Russell King <[email protected]>CC Greg Hackmann <[email protected]>CC Will Deacon <[email protected]>CC David Sehr <[email protected]>CC Linus Torvalds <[email protected]>---
arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ 3 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -62,6 +62,7 @@ config X86 select ARCH_HAS_SG_CHAIN select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX + select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAS_ZONE_DEVICE if X86_64 select ARCH_HAVE_NMI_SAFE_CMPXCHGarch/x86/include/asm/processor.h +0
-0@@ -706,6 +706,16 @@ static inline void sync_core(void)#endif }arch/x86/include/asm/processor.h static inline void sync_core(void) · line 717
Quoted 10 lines
arch/x86/include/asm/processor.h +9
-0+/* +* Ensure that a core serializing instruction is issued before returning +* to user-mode. x86 implements return to user-space through sysexit and +* sysretq, which are not core serializing. +*/ +static inline void sync_core_before_usermode (void ) +{ +sync_core (); +}Make this if (!in_interrupt())
sync_core(); please. We can optimize it better later on.
Quoted 5 lines
Quoted 3 lines
Sure, done. It will be part of the next version of that patch.
Thanks!
Hmm. Can you also cc stable and make sure it applies by itself?
Do you mean merging both this patch and the fix into a single patch ?
Quoted 13 lines
Quoted 13 lines
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
maintainerRFC PATCH 6/10 Fix: x86: Add missing core serializing instruction on migration
In reply to Mathieu Desnoyersx86 has a missing core serializing instruction in migration scenarios.
Given that x86-32 can return to user-space with sysexit, and x86-64 through sysretq and sysretl, which are not core serializing, the following user-space self-modifiying code (JIT) scenario can occur:
CPU 0 CPU 1
User-space self-modify code
Preempted
migrated ->
scheduler selects task Return to user-space (iret or sysexit) User-space issues sync_core()
<- migrated
scheduler selects task
Return to user-space (sysexit)
jump to modified code
Run modified code without sync_core() -> bug.
This migration pattern can return to user-space through sysexit, sysretl, or sysretq, which are not core serializing, and therefore breaks sequential consistency expectations from a single-threaded process.
Fix this issue by invoking sync_core_before_usermode() the first time a runqueue finishes a task switch after receiving a migrated thread.
--- kernel/sched/core.c | 7 +++++++ kernel/sched/sched.h | 1 + 2 files changed, 8 insertions(+)
kernel/sched/core.c +7 -0
@@ -927,6 +927,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,rq_lock (rq, rf);BUG_ON (task_cpu (p) != new_cpu); + rq->need_sync_core =1 ;enqueue_task (rq, p,0 ); p->on_rq =TASK_ON_RQ_QUEUED ;check_preempt_curr (rq, p,0 );
@@ -2684,6 +2685,12 @@ static struct rq *finish_task_switch(struct task_struct *prev) prev_state = prev->state;vtime_task_switch (prev);perf_event_task_sched_in (prev, current); +#ifdef CONFIG_SMP +if (unlikely (rq->need_sync_core)) { +sync_core_before_usermode (); + rq->need_sync_core =0 ; + } +#endif finish_lock_switch (rq, prev);finish_arch_post_lock_switch ();
kernel/sched/sched.h +1 -0
@@ -734,6 +734,7 @@ struct rq {/* For active balancing */ int active_balance;int push_cpu; +int need_sync_core;struct cpu_stop_work active_balance_work;/* cpu of this runqueue: */ int cpu;
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH v2 7/10 membarrier: x86: Provide core serializing command
In reply to Mathieu DesnoyersThere are two places where core serialization is needed by membarrier:
1) When returning from the membarrier IPI,
2) After scheduler updates curr to a thread with a different mm, before going back to user-space, since the curr->mm is used by membarrier to check whether it needs to send an IPI to that CPU.
x86-32 uses iret as return from interrupt, and 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 sysret{l,q} is not 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 adding a sync_core_before_usermode() in that specific case.
---
Changes since v1:
- Use the newly introduced sync_core_before_usermode(). Move all state handling to generic code.
--- arch/x86/Kconfig | 1 + arch/x86/entry/entry_32.S | 5 +++++ arch/x86/entry/entry_64.S | 8 ++++++++ arch/x86/mm/tlb.c | 7 ++++--- include/linux/sched/mm.h | 12 ++++++++++++ kernel/sched/core.c | 6 +++++- kernel/sched/membarrier.c | 4 ++++ 7 files changed, 39 insertions(+), 4 deletions(-)
arch/x86/Kconfig +1 -0
@@ -54,6 +54,7 @@ config X86 select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_KCOV if X86_64 + select ARCH_HAS_MEMBARRIER_SYNC_CORE select ARCH_HAS_PMEM_API if X86_64 # Causing hangs/crashes, see the commit that added this change for details. select ARCH_HAS_REFCOUNT if BROKEN
arch/x86/entry/entry_32.S +5 -0
@@ -553,6 +553,11 @@ restore_all: .Lrestore_nocheck: RESTORE_REGS 4 # skip orig_eax/error_code .Lirq_return: + /* + * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on iret core serialization + * when returning from IPI handler and when returning from + * scheduler to user-space. + */ INTERRUPT_RETURN .section .fixup, "ax"
arch/x86/entry/entry_64.S +8 -0
@@ -642,6 +642,10 @@ GLOBAL(restore_regs_and_iret) restore_c_regs_and_iret: RESTORE_C_REGS REMOVE_PT_GPREGS_FROM_STACK 8 + /* + * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on iret core serialization + * when returning from IPI handler. + */ INTERRUPT_RETURN ENTRY(native_iret)
@@ -1122,6 +1126,10 @@ paranoid_exit_restore: RESTORE_EXTRA_REGS RESTORE_C_REGS REMOVE_PT_GPREGS_FROM_STACK 8 + /* + * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on iret core serialization + * when returning from IPI handler. + */ INTERRUPT_RETURN END(paranoid_exit)
arch/x86/mm/tlb.c +4 -3
@@ -147,9 +147,10 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,this_cpu_write (cpu_tlbstate.is_lazy, false);/* -* The membarrier system call requires a full memory barrier -* before returning to user-space, after storing to rq->curr. -* Writing to CR3 provides that full memory barrier. +* 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. */ if (real_prev == next) { VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) !=
include/linux/sched/mm.h +12 -0
@@ -222,6 +222,7 @@ enum {MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U <<0 ),MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U <<1 ),MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U <<2 ), + MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 3), }; enum {
@@ -232,6 +233,14 @@ enum {#include <asm/membarrier.h> #endif +static inline void membarrier_mm_sync_core_before_usermode (struct mm_struct *mm) +{ +if (likely (!(atomic_read (&mm->membarrier_state) & +MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE ))) +return ; +sync_core_before_usermode (); +} +static inline void membarrier_execve (struct task_struct *t) {atomic_set (&t->mm->membarrier_state,0 );
@@ -246,6 +255,9 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev,static inline void membarrier_execve (struct task_struct *t) { } +static inline void membarrier_mm_sync_core_before_usermode (struct mm_struct *mm) +{ +}#endif #endif /* _LINUX_SCHED_MM_H */
kernel/sched/core.c +5 -1
@@ -2700,9 +2700,13 @@ static struct rq *finish_task_switch(struct task_struct *prev)* thread, mmdrop()'s implicit full barrier is required by the * membarrier system call, because the current active_mm can * become the current mm without going through switch_mm(). +* membarrier also requires a core serializing instruction +* before going back to user-space after storing to rq->curr. */ -if (mm) +if (mm) {mmdrop (mm); +membarrier_mm_sync_core_before_usermode (mm); + }if (unlikely (prev_state ==TASK_DEAD )) {if (prev->sched_class->task_dead) prev->sched_class->task_dead (prev);
kernel/sched/membarrier.c +4 -0
@@ -141,6 +141,10 @@ static int membarrier_register_private_expedited(int flags)return 0 ;atomic_or (MEMBARRIER_STATE_PRIVATE_EXPEDITED , &mm->membarrier_state); +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 )) {/* * Ensure all future scheduler executions will observe the
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 8/10 membarrier: selftest: Test private expedited sync core cmd
In reply to Mathieu DesnoyersTest the new MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE and MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE commands.
--- .../testing/selftests/membarrier/membarrier_test.c | 77 +++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-)
tools/testing/selftests/membarrier/membarrier_test.c +76 -1
@@ -132,6 +132,63 @@ static int test_membarrier_private_expedited_success(void)return 0 ; } +static int test_membarrier_private_expedited_sync_core_fail (void ) +{ +int cmd =MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE not registered failure" ; + +if (sys_membarrier (cmd, flags) !=-1 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d. Should fail, but passed\n" , + test_name, flags); + } +if (errno !=EPERM ) { +ksft_exit_fail_msg ( +"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n" , + test_name, flags,EPERM ,strerror (EPERM ), + errno,strerror (errno)); + } + +ksft_test_result_pass ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); +return 0 ; +} + +static int test_membarrier_register_private_expedited_sync_core_success (void ) +{ +int cmd =MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE" ; + +if (sys_membarrier (cmd, flags) !=0 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); + } + +ksft_test_result_pass ( +"%s test: flags = %d\n" , + test_name, flags); +return 0 ; +} + +static int test_membarrier_private_expedited_sync_core_success (void ) +{ +int cmd =MEMBARRIER_CMD_PRIVATE_EXPEDITED , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE" ; + +if (sys_membarrier (cmd, flags) !=0 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); + } + +ksft_test_result_pass ( +"%s test: flags = %d\n" , + test_name, flags); +return 0 ; +} +static int test_membarrier (void ) {int status;
@@ -154,6 +211,22 @@ static int test_membarrier(void) status =test_membarrier_private_expedited_success ();if (status)return status; + status =sys_membarrier (MEMBARRIER_CMD_QUERY ,0 ); +if (status <0 ) { +ksft_test_result_fail ("sys_membarrier() failed\n" ); +return status; + } +if (status &MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE ) { + status =test_membarrier_private_expedited_sync_core_fail (); +if (status) +return status; + status =test_membarrier_register_private_expedited_sync_core_success (); +if (status) +return status; + status =test_membarrier_private_expedited_sync_core_success (); +if (status) +return status; + }return 0 ; }
@@ -173,8 +246,10 @@ static int test_membarrier_query(void) }ksft_exit_fail_msg ("sys_membarrier() failed\n" ); } -if (!(ret &MEMBARRIER_CMD_SHARED )) +if (!(ret &MEMBARRIER_CMD_SHARED )) { +ksft_test_result_fail ("sys_membarrier() CMD_SHARED query failed\n" );ksft_exit_fail_msg ("sys_membarrier is not supported.\n" ); + }ksft_test_result_pass ("sys_membarrier available\n" );return 0 ;
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 9/10 membarrier: provide SHARED_EXPEDITED command
In reply to Mathieu DesnoyersAllow expedited membarrier to be used for data shared between processes (shared memory).
Processes wishing to receive the membarriers register with MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED. Those which want to issue membarrier invoke MEMBARRIER_CMD_SHARED_EXPEDITED.
This allows extremely simple kernel-level implementation: we have almost everything we need with the PRIVATE_EXPEDITED barrier code. All we need to do is to add a flag in the mm_struct that will be used to check whether we need to send the IPI to the current thread of each CPU.
There is a slight downside of this approach compared to targeting specific shared memory users: when performing a membarrier operation, all registered "shared" receivers will get the barrier, even if they don't share a memory mapping with the "sender" issuing MEMBARRIER_CMD_SHARED_EXPEDITED.
This registration approach seems to fit the requirement of not disturbing processes that really deeply care about real-time: they simply should not register with MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED.
--- arch/powerpc/include/asm/membarrier.h | 3 +- include/linux/sched/mm.h | 2 + include/uapi/linux/membarrier.h | 26 ++++++++- kernel/sched/membarrier.c | 104 +++++++++++++++++++++++++++++++++- 4 files changed, 131 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/membarrier.h +2 -1
@@ -12,7 +12,8 @@ static inline void membarrier_arch_switch_mm(struct mm_struct *prev,* store to rq->curr. */ if (likely (!(atomic_read (&next->membarrier_state) - &MEMBARRIER_STATE_PRIVATE_EXPEDITED ) || !prev)) + & (MEMBARRIER_STATE_PRIVATE_EXPEDITED + |MEMBARRIER_STATE_SHARED_EXPEDITED )) || !prev))return ;/*
include/linux/sched/mm.h +2 -0
@@ -223,6 +223,8 @@ enum {MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U <<1 ),MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U <<2 ),MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U <<3 ), +MEMBARRIER_STATE_SHARED_EXPEDITED_READY = (1U <<4 ), + MEMBARRIER_STATE_SHARED_EXPEDITED = (1U << 5), }; enum {
include/uapi/linux/membarrier.h +24 -2
@@ -40,6 +40,28 @@* (non-running threads are de facto in such a * state). This covers threads from all processes * running on the system. This command returns 0. +* @MEMBARRIER_CMD_SHARED_EXPEDITED: +* Execute a memory barrier on all running threads +* part of a process which previously registered +* with MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED. +* Upon return from system call, the caller thread +* is ensured that all running threads 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 processes +* which registered with +* MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED. +* This command returns 0. Given that +* registration is about the intent to receive +* the barriers, it is valid to invoke +* MEMBARRIER_CMD_SHARED_EXPEDITED from a +* non-registered process. +* @MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED: +* Register the process intent to receive +* MEMBARRIER_CMD_SHARED_EXPEDITED memory +* barriers. Always returns 0. * @MEMBARRIER_CMD_PRIVATE_EXPEDITED: * Execute a memory barrier on each running * thread belonging to the same process as the current
@@ -100,8 +122,8 @@enum membarrier_cmd {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_SHARED_EXPEDITED = (1 <<1 ), +MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED = (1 <<2 ),MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 <<3 ),MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 <<4 ),MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE = (1 <<5 ),
kernel/sched/membarrier.c +103 -1
128 lines not on this page · raw message
Signature · 1 line
2.11.0
Mathieu Desnoyers
maintainerRFC PATCH 10/10 membarrier: selftest: Test shared expedited cmd
In reply to Mathieu DesnoyersTest the new MEMBARRIER_CMD_SHARED_EXPEDITED and MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED commands.
--- .../testing/selftests/membarrier/membarrier_test.c | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+)
tools/testing/selftests/membarrier/membarrier_test.c +47 -0
@@ -189,6 +189,40 @@ static int test_membarrier_private_expedited_sync_core_success(void)return 0 ; } +static int test_membarrier_register_shared_expedited_success (void ) +{ +int cmd =MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_REGISTER_SHARED_EXPEDITED" ; + +if (sys_membarrier (cmd, flags) !=0 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); + } + +ksft_test_result_pass ( +"%s test: flags = %d\n" , + test_name, flags); +return 0 ; +} + +static int test_membarrier_shared_expedited_success (void ) +{ +int cmd =MEMBARRIER_CMD_SHARED_EXPEDITED , flags =0 ; +const char *test_name ="sys membarrier MEMBARRIER_CMD_SHARED_EXPEDITED" ; + +if (sys_membarrier (cmd, flags) !=0 ) { +ksft_exit_fail_msg ( +"%s test: flags = %d, errno = %d\n" , + test_name, flags, errno); + } + +ksft_test_result_pass ( +"%s test: flags = %d\n" , + test_name, flags); +return 0 ; +} +static int test_membarrier (void ) {int status;
@@ -227,6 +261,19 @@ static int test_membarrier(void)if (status)return status; } +/* +* It is valid to send a shared membarrier from a non-registered +* process. +*/ + status =test_membarrier_shared_expedited_success (); +if (status) +return status; + status =test_membarrier_register_shared_expedited_success (); +if (status) +return status; + status =test_membarrier_shared_expedited_success (); +if (status) +return status;return 0 ; }
Signature · 1 line
2.11.0