Fix: x86: Add missing core serializing instruction on migration
Subsystems (5)
RFC PATCH v2 1/1 Fix: x86: Add missing core serializing instruction on migration
x86 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 introducing sync_core_before_usermode(), invoked the first time a runqueue finishes a task switch after receiving a migrated thread.
Architectures defining the sync_core_before_usermode() static inline need to define ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
---
Changes since v1:
- Fold patch introducing sync_core_before_usermode and the fix into a single patch.
- CC [email protected]
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ kernel/sched/core.c | 7 +++++++ kernel/sched/sched.h | 1 + 5 files changed, 25 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, +* sysretl, 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 */
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 );
@@ -2654,6 +2655,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)* to use. */ smp_mb__after_unlock_lock (); +#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
Fixing the [email protected] CC.
Quoted 159 lines · ----- On Nov 11, 2017, at 10:03 AM, Mathieu Desnoyers…
x86 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 issuessync_core()
<- migrated
scheduler selects task
Return to user-space (sysexit)
jump to modified code
Run modified code withoutsync_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 introducing
sync_core_before_usermode(), invoked the first time a runqueue finishes a task switch after receiving a migrated thread.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]>---
Changes since v1:
- Fold patch introducingsync_core_before_usermodeand the fix into a single patch.
- CC [email protected]
---arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ kernel/sched/core.c | 7 +++++++ kernel/sched/sched.h | 1 + 5 files changed, 25 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 +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, +* sysretl, 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 */ kernel/sched/core.c +1
-0@@ -927,6 +927,7 @@ static struct rq *move_queued_task(struct rq *rq, structrq_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);kernel/sched/core.c +6
-0@@ -2654,6 +2655,12 @@ static struct rq *finish_task_switch(struct task_struct*prev) * to use. */ smp_mb__after_unlock_lock (); +#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
-1@@ -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; --2.11.0
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Quoted 5 lines · Mathieu Desnoyers
x86 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:
Is this about load/store consistency ? In this case, don't you also have problems with get/put_user and not just going to userspace ?
Quoted 149 lines
CPU 0 CPU 1
User-space self-modify code
Preempted
migrated ->
scheduler selects task Return to user-space (iret or sysexit) User-space issuessync_core()
<- migrated
scheduler selects task
Return to user-space (sysexit)
jump to modified code
Run modified code withoutsync_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 introducing
sync_core_before_usermode(), invoked the first time a runqueue finishes a task switch after receiving a migrated thread.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]>---
Changes since v1:
- Fold patch introducingsync_core_before_usermodeand the fix into a single patch.
- CC [email protected]--- arch/x86/Kconfig | 1 + arch/x86/include/asm/processor.h | 10 ++++++++++ include/linux/processor.h | 6 ++++++ kernel/sched/core.c | 7 +++++++ kernel/sched/sched.h | 1 + 5 files changed, 25 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 +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, +* sysretl, 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 */ 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 );@@ -2654,6 +2655,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)* to use. */ smp_mb__after_unlock_lock (); +#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;
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 5 lines · Mathieu Desnoyers
x86 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:
Is this about load/store consistency ? In this case, don't you also have problems with get/put_user and not just going to userspace ?
Ah forget it, I saw Linus earlier messages.
Cheers,
Ben.