Skip to contentSkip to conversation

membarrier updates for 4.16

PATCH In review Mathieu Desnoyers 2 participants 17 messages Jan 17, 2018

Subsystems (10)
x86 PowerPC Kselftest arm64 +6 more subsystems
lore
Share & tools
lore

Mathieu Desnoyers

author maintainer
#0 lore raw

cover membarrier updates for 4.16

Hi Ingo, Peter, Thomas,

Here is the updated membarrier patch series following review from Thomas.

It would be appreciated of those can go through the scheduler tree for the 4.16 merge window.

Highlights:

"powerpc: membarrier: Skip memory barrier in switch_mm()" takes care of a TODO that was left in the private expedited implementation when merged in 4.14: an extra memory barrier was added on context switch on powerpc. Ensure that the barrier is only performed when scheduling between different processes, only for threads belonging to processes that have registered their intent to use the private expedited command.

"membarrier: provide GLOBAL_EXPEDITED command" adds new commands to membarrier for registration and use of membarrier across processes communicating through shared memory mappings. The non-expedited command has proven to be really too slow (taking 10ms and more to complete) for real-world use. The expedited version completes in a matter of microseconds. This patch renames the pre-existing MEMBARRIER_CMD_SHARED to MEMBARRIER_CMD_GLOBAL for consistency, keeping the old enum label as an alias for backward compatibility.

"membarrier: Provide core serializing command" provides core serialization for JIT reclaim. We received positive feedback from Android developers that the proposed ABI fits their use-case. Only x86 32/64 and arm 64 implement this command so far. This is opt-in per architecture.

The other patches add selftests and documentation.

Thanks,

Mathieu

Mathieu Desnoyers (11):
membarrier: selftest: Test private expedited cmd (v2)
powerpc: membarrier: Skip memory barrier in switch_mm() (v7)
membarrier: Document scheduler barrier requirements (v5)
membarrier: provide GLOBAL_EXPEDITED command (v3)
membarrier: selftest: Test global expedited cmd (v2) Introduce sync_core_before_usermode
x86: Implement sync_core_before_usermode (v3)
membarrier: Provide core serializing command
membarrier: x86: Provide core serializing command (v4)
membarrier: arm64: Provide core serializing command
membarrier: selftest: Test private expedited sync core cmd

 MAINTAINERS                                        |   1 +
 arch/arm64/Kconfig                                 |   1 +
 arch/arm64/kernel/entry.S                          |   4 +
 arch/powerpc/Kconfig                               |   1 +
 arch/powerpc/include/asm/membarrier.h              |  27 +++
 arch/powerpc/mm/mmu_context.c                      |   7 +
 arch/x86/Kconfig                                   |   2 +
 arch/x86/entry/entry_32.S                          |   5 +
 arch/x86/entry/entry_64.S                          |   4 +
 arch/x86/include/asm/processor.h                   |  10 +
 arch/x86/mm/tlb.c                                  |   6 +
 include/linux/processor.h                          |   6 +
 include/linux/sched/mm.h                           |  40 +++-
 include/uapi/linux/membarrier.h                    |  74 ++++++-
 init/Kconfig                                       |   9 +
 kernel/sched/core.c                                |  53 +++--
 kernel/sched/membarrier.c                          | 177 +++++++++++++--
 .../testing/selftests/membarrier/membarrier_test.c | 237 +++++++++++++++++++--
 18 files changed, 596 insertions(+), 68 deletions(-)
 create mode 100644 arch/powerpc/include/asm/membarrier.h
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 1/11 membarrier: selftest: Test private expedited cmd (v2)

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.

CC Peter Zijlstra <[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 <avi-VrcmuVmyx1hWk0Htik3J/[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <mpe-Gsx/[email protected]>
CC Dave Watson <[email protected]>
CC Alan Stern <stern-nwvwT67g6+6dFdvTe/[email protected]>
CC Will Deacon <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Alice Ferrazzi <[email protected]>
CC Paul Elder <[email protected]>

---
Changes since v1:
- return result of ksft_exit_pass from main(), silencing compiler warning about missing return value.

---
 .../testing/selftests/membarrier/membarrier_test.c | 111 ++++++++++++++++++---
 1 file changed, 95 insertions(+), 16 deletions(-)
tools/testing/selftests/membarrier/membarrier_test.c +95 -16
index 9e674d9514d1..e6ee73d01fa1 100644
@@ -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;
@@ -108,5 +187,5 @@ int main(int argc, char **argv)
 	test_membarrier_query();
 	test_membarrier();
 
-	ksft_exit_pass();
+	return ksft_exit_pass();
 }
Signature · 1 line
2.11.0
author maintainer
#2 lore raw

PATCH 2/11 powerpc: membarrier: Skip memory barrier in switch_mm() (v7)

In reply to Mathieu Desnoyers

Allow 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.

CC Peter Zijlstra <[email protected]>
CC Paul E. McKenney <[email protected]>
CC Boqun Feng <[email protected]>
CC Andrew Hunter <[email protected]>
CC Maged Michael <[email protected]>
CC Avi Kivity <[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <[email protected]>
CC Dave Watson <[email protected]>
CC Alan Stern <[email protected]>
CC Will Deacon <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Ingo Molnar <[email protected]>
CC Alexander Viro <[email protected]>
CC Nicholas Piggin <[email protected]>

---
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 | 26 ++++++++++++++++++++++++++
 arch/powerpc/mm/mmu_context.c         |  7 +++++++
 include/linux/sched/mm.h              | 13 ++++++++++++-
 init/Kconfig                          |  3 +++
 kernel/sched/core.c                   | 10 ----------
 kernel/sched/membarrier.c             |  8 ++++++++
 8 files changed, 58 insertions(+), 11 deletions(-)
 create mode 100644 arch/powerpc/include/asm/membarrier.h
MAINTAINERS +1 -0
index 18994806e441..c2f0d9a48a10 100644
@@ -8931,6 +8931,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
index c51e6ce42e7a..a63adb082c0a 100644
@@ -140,6 +140,7 @@ config PPC
 	select ARCH_HAS_FORTIFY_SOURCE
 	select ARCH_HAS_GCOV_PROFILE_ALL
 	select ARCH_HAS_PMEM_API                if PPC64
+	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 +26 -0
new file mode 100644
index 000000000000..98ff4f1fcf2b
@@ -0,0 +1,26 @@
+#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
index d60a62bf4fc7..0ab297c4cfad 100644
@@ -12,6 +12,7 @@
 
 #include <linux/mm.h>
 #include <linux/cpu.h>
+#include <linux/sched/mm.h>
 
 #include <asm/mmu_context.h>
 
@@ -58,6 +59,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();
 
@@ -80,6 +85,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 +12 -1
index 3d49b91b674d..1754396795f6 100644
@@ -215,14 +215,25 @@ 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
index a9a2e2c86671..2d118b6adee2 100644
@@ -1412,6 +1412,9 @@ config USERFAULTFD
 	  Enable the userfaultfd() system call that allows to intercept and
 	  handle page faults in userland.
 
+config ARCH_HAS_MEMBARRIER_HOOKS
+	bool
+
 config EMBEDDED
 	bool "Embedded system"
 	option allnoconfig_y
kernel/sched/core.c +0 -10
index 644fa2e3d993..524b705892db 100644
@@ -2653,16 +2653,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 +8 -0
index 9bcbacba82a8..678577267a9a 100644
@@ -118,6 +118,14 @@ 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
author maintainer
#3 lore raw

PATCH 3/11 membarrier: Document scheduler barrier requirements (v5)

In reply to Mathieu Desnoyers

Document 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.

CC Peter Zijlstra <[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]>

---
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
index a1561957dccb..c28cd5592b0d 100644
@@ -200,6 +200,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
index 1754396795f6..28aef7051d73 100644
@@ -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
index 524b705892db..62f269980e29 100644
@@ -2657,6 +2657,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)) {
@@ -2762,6 +2768,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);
@@ -3298,6 +3311,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();
@@ -3345,17 +3361,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
author maintainer
#4 lore raw

PATCH 4/11 membarrier: provide GLOBAL_EXPEDITED command (v3)

In reply to Mathieu Desnoyers

Allow expedited membarrier to be used for data shared between processes through shared memory.

Processes wishing to receive the membarriers register with MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED. Those which want to issue membarrier invoke MEMBARRIER_CMD_GLOBAL_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 to this approach compared to targeting specific shared memory users: when performing a membarrier operation, all registered "global" receivers will get the barrier, even if they don't share a memory mapping with the sender issuing MEMBARRIER_CMD_GLOBAL_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_GLOBAL_EXPEDITED.

In order to align the membarrier command names, the "MEMBARRIER_CMD_SHARED" command is renamed to "MEMBARRIER_CMD_GLOBAL", keeping an alias of MEMBARRIER_CMD_SHARED to MEMBARRIER_CMD_GLOBAL for UAPI header backward compatibility.

CC Peter Zijlstra <[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]>

---
Changes since v1:
- Add missing preempt disable around smp_call_function_many(). Changes since v2:
- UAPI documentation fix based on Thomas Gleixner's feedback.
- Rename SHARED to GLOBAL.

---
 arch/powerpc/include/asm/membarrier.h |   3 +-
 include/linux/sched/mm.h              |   6 +-
 include/uapi/linux/membarrier.h       |  42 ++++++++++--
 kernel/sched/membarrier.c             | 120 +++++++++++++++++++++++++++++++---
 4 files changed, 153 insertions(+), 18 deletions(-)
arch/powerpc/include/asm/membarrier.h +2 -1
index 98ff4f1fcf2b..6e20bb5c74ea 100644
@@ -13,7 +13,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_GLOBAL_EXPEDITED)) || !prev))
 		return;
 
 	/*
include/linux/sched/mm.h +4 -2
index 28aef7051d73..c7ad7a70cfef 100644
@@ -219,8 +219,10 @@ 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_GLOBAL_EXPEDITED_READY			= (1U << 2),
+	MEMBARRIER_STATE_GLOBAL_EXPEDITED			= (1U << 3),
 };
 
 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS
include/uapi/linux/membarrier.h +35 -7

66 lines not on this page · raw message

kernel/sched/membarrier.c +112 -8

166 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#5 lore raw

PATCH 5/11 membarrier: selftest: Test global expedited cmd (v2)

In reply to Mathieu Desnoyers

Test the new MEMBARRIER_CMD_GLOBAL_EXPEDITED and MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED commands.

Adapt to the MEMBARRIER_CMD_SHARED -> MEMBARRIER_CMD_GLOBAL rename.

CC Greg Kroah-Hartman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Paul E. McKenney <[email protected]>
CC Boqun Feng <[email protected]>
CC Andrew Hunter <[email protected]>
CC Maged Michael <[email protected]>
CC Avi Kivity <[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <[email protected]>
CC Dave Watson <[email protected]>
CC Alan Stern <[email protected]>
CC Will Deacon <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Alice Ferrazzi <[email protected]>
CC Paul Elder <[email protected]>

---
Changes since v1:
- Rename SHARED to GLOBAL.

---
 .../testing/selftests/membarrier/membarrier_test.c | 59 ++++++++++++++++++++--
 1 file changed, 54 insertions(+), 5 deletions(-)
tools/testing/selftests/membarrier/membarrier_test.c +54 -5
index e6ee73d01fa1..eec39a0e1f77 100644
@@ -59,10 +59,10 @@ static int test_membarrier_flags_fail(void)
 	return 0;
 }
 
-static int test_membarrier_shared_success(void)
+static int test_membarrier_global_success(void)
 {
-	int cmd = MEMBARRIER_CMD_SHARED, flags = 0;
-	const char *test_name = "sys membarrier MEMBARRIER_CMD_SHARED";
+	int cmd = MEMBARRIER_CMD_GLOBAL, flags = 0;
+	const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL";
 
 	if (sys_membarrier(cmd, flags) != 0) {
 		ksft_exit_fail_msg(
@@ -132,6 +132,40 @@ static int test_membarrier_private_expedited_success(void)
 	return 0;
 }
 
+static int test_membarrier_register_global_expedited_success(void)
+{
+	int cmd = MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, flags = 0;
+	const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_GLOBAL_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_global_expedited_success(void)
+{
+	int cmd = MEMBARRIER_CMD_GLOBAL_EXPEDITED, flags = 0;
+	const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL_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;
@@ -142,7 +176,7 @@ static int test_membarrier(void)
 	status = test_membarrier_flags_fail();
 	if (status)
 		return status;
-	status = test_membarrier_shared_success();
+	status = test_membarrier_global_success();
 	if (status)
 		return status;
 	status = test_membarrier_private_expedited_fail();
@@ -154,6 +188,19 @@ static int test_membarrier(void)
 	status = test_membarrier_private_expedited_success();
 	if (status)
 		return status;
+	/*
+	 * It is valid to send a global membarrier from a non-registered
+	 * process.
+	 */
+	status = test_membarrier_global_expedited_success();
+	if (status)
+		return status;
+	status = test_membarrier_register_global_expedited_success();
+	if (status)
+		return status;
+	status = test_membarrier_global_expedited_success();
+	if (status)
+		return status;
 	return 0;
 }
 
@@ -173,8 +220,10 @@ static int test_membarrier_query(void)
 		}
 		ksft_exit_fail_msg("sys_membarrier() failed\n");
 	}
-	if (!(ret & MEMBARRIER_CMD_SHARED))
+	if (!(ret & MEMBARRIER_CMD_GLOBAL)) {
+		ksft_test_result_fail("sys_membarrier() CMD_GLOBAL 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
author maintainer
#6 lore raw

PATCH 6/11 Introduce sync_core_before_usermode

In reply to Mathieu Desnoyers

Introduce an architecture function that ensures the current CPU issues a core serializing instruction before returning to usermode.

This is needed for the membarrier "sync_core" command.

Architectures defining the sync_core_before_usermode() static inline need to select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.

CC Thomas Gleixner <[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 <avi-VrcmuVmyx1hWk0Htik3J/[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <mpe-Gsx/[email protected]>
CC Dave Watson <[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 <torvalds-de/[email protected]>
---
 include/linux/processor.h | 6 ++++++
 init/Kconfig              | 3 +++
 2 files changed, 9 insertions(+)
include/linux/processor.h +6 -0
index dbc952eec869..866de5326d34 100644
@@ -68,4 +68,10 @@ do {								\
 
 #endif
 
+#ifndef CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
+static inline void sync_core_before_usermode(void)
+{
+}
+#endif
+
 #endif /* _LINUX_PROCESSOR_H */
init/Kconfig +3 -0
index 2d118b6adee2..30208da2221f 100644
@@ -1918,3 +1918,6 @@ config ASN1
 	  functions to call on what tags.
 
 source "kernel/Kconfig.locks"
+
+config ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
+	bool
Signature · 1 line
2.11.0
author maintainer
#7 lore raw

PATCH 7/11 x86: Implement sync_core_before_usermode (v3)

In reply to Mathieu Desnoyers

Ensure that a core serializing instruction is issued before returning to user-mode. x86 implements return to user-space through sysexit, sysrel, and sysretq, which are not core serializing.

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 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:
- Fix prototype of sync_core_before_usermode in generic code (missing return type).
- Add linux/processor.h include to sched/core.c.
- Add ARCH_HAS_SYNC_CORE_BEFORE_USERMODE to init/Kconfig.
- Fix linux/processor.h ifdef to target CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE rather than ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
- Move empty static inline in processor.h to generic patch.

---
 arch/x86/Kconfig                 |  1 +
 arch/x86/include/asm/processor.h | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/Kconfig +1 -0
index 20da391b5f32..0b44c8dd0e95 100644
@@ -61,6 +61,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
index d3a67fba200a..3257d34dbb40 100644
@@ -722,6 +722,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,
+ * sysrel, 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);
 
Signature · 1 line
2.11.0
maintainer
#8 lore raw

Re: [PATCH for 4.16 07/11] x86: Implement sync_core_before_usermode (v3)

Quoted 59 lines · Mathieu Desnoyers

Ensure that a core serializing instruction is issued before returning to user-mode. x86 implements return to user-space through sysexit, sysrel, and sysretq, which are not core serializing.

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 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:
- Fix prototype of sync_core_before_usermode in generic code (missing return type).
- Add linux/processor.h include to sched/core.c.
- Add ARCH_HAS_SYNC_CORE_BEFORE_USERMODE to init/Kconfig.
- Fix linux/processor.h ifdef to target CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE rather than ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
- Move empty static inline in processor.h to generic patch.

---
 arch/x86/Kconfig                 |  1 +
 arch/x86/include/asm/processor.h | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/Kconfig +1 -0
index 20da391b5f32..0b44c8dd0e95 100644
@@ -61,6 +61,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 +0 -0
index d3a67fba200a..3257d34dbb40 100644

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 731

Quoted 10 lines
arch/x86/include/asm/processor.h +7 -0
@@ -722,6 +722,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,
+ * sysrel, and sysretq, which are not core serializing.
+ */
+static inline void sync_core_before_usermode(void)
+{

/* With PTI, we unconditionally serialize before running user code. */ if (static_cpu_has(X86_FEATURE_PTI))
return;

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 733

Quoted 2 lines
arch/x86/include/asm/processor.h +2 -0
+       sync_core();
+}

--Andy

author maintainer
#9 lore raw

Re: [PATCH for 4.16 07/11] x86: Implement sync_core_before_usermode (v3)

Quoted 64 lines · ----- On Jan 17, 2018, at 12:53 PM, Andy Lutomirski [email protected] wrote:
Quoted 62 lines · Mathieu Desnoyers

Ensure that a core serializing instruction is issued before returning to user-mode. x86 implements return to user-space through sysexit, sysrel, and sysretq, which are not core serializing.

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 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:
- Fix prototype of sync_core_before_usermode in generic code (missing return type).
- Add linux/processor.h include to sched/core.c.
- Add ARCH_HAS_SYNC_CORE_BEFORE_USERMODE to init/Kconfig.
- Fix linux/processor.h ifdef to target CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE rather than ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
- Move empty static inline in processor.h to generic patch.

---
 arch/x86/Kconfig                 |  1 +
 arch/x86/include/asm/processor.h | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/Kconfig +1 -0
index 20da391b5f32..0b44c8dd0e95 100644
@@ -61,6 +61,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 +0 -0
index d3a67fba200a..3257d34dbb40 100644
@@ -722,6 +722,16 @@ static inline void sync_core(void)
 #endif
 }
 
Quoted 11 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 731

Quoted 7 lines
arch/x86/include/asm/processor.h +7 -0
+/*
+ * Ensure that a core serializing instruction is issued before returning
+ * to user-mode. x86 implements return to user-space through sysexit,
+ * sysrel, and sysretq, which are not core serializing.
+ */
+static inline void sync_core_before_usermode(void)
+{

/* With PTI, we unconditionally serialize before running user code. */ if (static_cpu_has(X86_FEATURE_PTI))
return;

One issue I'm facing with this change is header dependency:
sync_core_before_usermode() is currently implemented in arch/x86/include/asm/processor.h, but arch/x86/include/asm/cpufeature.h is needed for static_cpu_has, and it happens to include asm/processor.h.

I'm facing a similar issue for adding a (in_irq() || in_nmi()) check.

Should we move sync_core_before_usermode() to a different header, and if so, any suggestion ?

Thanks,

Mathieu

Quoted 5 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 733

Quoted 3 lines
arch/x86/include/asm/processor.h +2 -0
+       sync_core();
+}

--Andy

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
maintainer
#10 lore raw

Re: [PATCH for 4.16 07/11] x86: Implement sync_core_before_usermode (v3)

Quoted 84 lines · Mathieu Desnoyers
Quoted 64 lines · ----- On Jan 17, 2018, at 12:53 PM, Andy Lutomirski…
Quoted 62 lines · Mathieu Desnoyers

Ensure that a core serializing instruction is issued before returning to user-mode. x86 implements return to user-space through sysexit, sysrel, and sysretq, which are not core serializing.

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 <avi-VrcmuVmyx1hWk0Htik3J/[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <mpe-Gsx/[email protected]>
CC Dave Watson <[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 <torvalds-de/[email protected]>

---
Changes since v1:
- Fix prototype of sync_core_before_usermode in generic code (missing return type).
- Add linux/processor.h include to sched/core.c.
- Add ARCH_HAS_SYNC_CORE_BEFORE_USERMODE to init/Kconfig.
- Fix linux/processor.h ifdef to target CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE rather than ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
- Move empty static inline in processor.h to generic patch.

---
 arch/x86/Kconfig                 |  1 +
 arch/x86/include/asm/processor.h | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/Kconfig +1 -0
index 20da391b5f32..0b44c8dd0e95 100644
@@ -61,6 +61,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 +0 -0
index d3a67fba200a..3257d34dbb40 100644
@@ -722,6 +722,16 @@ static inline void sync_core(void)
 #endif
 }
 
Quoted 11 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 731

Quoted 7 lines
arch/x86/include/asm/processor.h +7 -0
+/*
+ * Ensure that a core serializing instruction is issued before returning
+ * to user-mode. x86 implements return to user-space through sysexit,
+ * sysrel, and sysretq, which are not core serializing.
+ */
+static inline void sync_core_before_usermode(void)
+{

/* With PTI, we unconditionally serialize before running user code. */ if (static_cpu_has(X86_FEATURE_PTI))
return;

One issue I'm facing with this change is header dependency:
sync_core_before_usermode() is currently implemented in arch/x86/include/asm/processor.h, but arch/x86/include/asm/cpufeature.h is needed for static_cpu_has, and it happens to include asm/processor.h.

Quoted 4 lines

I'm facing a similar issue for adding a (in_irq() || in_nmi()) check.

Should we move sync_core_before_usermode() to a different header, and if so, any suggestion ?

tlbflush.h, maybe?

Quoted 17 lines

Thanks,

Mathieu

Quoted 5 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 733

Quoted 3 lines
arch/x86/include/asm/processor.h +2 -0
+       sync_core();
+}

--Andy

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

author maintainer
#11 lore raw

Re: [PATCH for 4.16 07/11] x86: Implement sync_core_before_usermode (v3)

Quoted 86 lines · ----- On Jan 17, 2018, at 1:13 PM, Andy Lutomirski [email protected] wrote:
Quoted 84 lines · Mathieu Desnoyers
Quoted 64 lines · ----- On Jan 17, 2018, at 12:53 PM, Andy Lutomirski [email protected] wrote:
Quoted 62 lines · Mathieu Desnoyers

Ensure that a core serializing instruction is issued before returning to user-mode. x86 implements return to user-space through sysexit, sysrel, and sysretq, which are not core serializing.

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 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:
- Fix prototype of sync_core_before_usermode in generic code (missing return type).
- Add linux/processor.h include to sched/core.c.
- Add ARCH_HAS_SYNC_CORE_BEFORE_USERMODE to init/Kconfig.
- Fix linux/processor.h ifdef to target CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE rather than ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
- Move empty static inline in processor.h to generic patch.

---
 arch/x86/Kconfig                 |  1 +
 arch/x86/include/asm/processor.h | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/Kconfig +1 -0
index 20da391b5f32..0b44c8dd0e95 100644
@@ -61,6 +61,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 +0 -0
index d3a67fba200a..3257d34dbb40 100644
@@ -722,6 +722,16 @@ static inline void sync_core(void)
 #endif
 }
 
Quoted 11 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 731

Quoted 7 lines
arch/x86/include/asm/processor.h +7 -0
+/*
+ * Ensure that a core serializing instruction is issued before returning
+ * to user-mode. x86 implements return to user-space through sysexit,
+ * sysrel, and sysretq, which are not core serializing.
+ */
+static inline void sync_core_before_usermode(void)
+{

/* With PTI, we unconditionally serialize before running user code. */ if (static_cpu_has(X86_FEATURE_PTI))
return;

One issue I'm facing with this change is header dependency:
sync_core_before_usermode() is currently implemented in arch/x86/include/asm/processor.h, but arch/x86/include/asm/cpufeature.h is needed for static_cpu_has, and it happens to include asm/processor.h.

Quoted 6 lines
Quoted 4 lines

I'm facing a similar issue for adding a (in_irq() || in_nmi()) check.

Should we move sync_core_before_usermode() to a different header, and if so, any suggestion ?

tlbflush.h, maybe?

Core serialization seems to be unrelated to TLB flushing though.

I'm considering to create those new header files:

include/asm-generic/sync_core.h (empty function)
arch/x86/include/asm/sync_core.h

for the sync_core_before_usermode() static inlines. Any better idea ?

Thanks,

Mathieu

Quoted 18 lines
Quoted 18 lines

Thanks,

Mathieu

Quoted 5 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 733

Quoted 3 lines
arch/x86/include/asm/processor.h +2 -0
+       sync_core();
+}

--Andy

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
maintainer
#12 lore raw

Re: [PATCH for 4.16 07/11] x86: Implement sync_core_before_usermode (v3)

Quoted 95 lines · Mathieu Desnoyers
Quoted 86 lines · ----- On Jan 17, 2018, at 1:13 PM, Andy Lutomirski [email protected] wrote:
Quoted 84 lines · Mathieu Desnoyers
Quoted 64 lines · ----- On Jan 17, 2018, at 12:53 PM, Andy Lutomirski [email protected] wrote:
Quoted 62 lines · Mathieu Desnoyers

Ensure that a core serializing instruction is issued before returning to user-mode. x86 implements return to user-space through sysexit, sysrel, and sysretq, which are not core serializing.

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 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:
- Fix prototype of sync_core_before_usermode in generic code (missing return type).
- Add linux/processor.h include to sched/core.c.
- Add ARCH_HAS_SYNC_CORE_BEFORE_USERMODE to init/Kconfig.
- Fix linux/processor.h ifdef to target CONFIG_ARCH_HAS_SYNC_CORE_BEFORE_USERMODE rather than ARCH_HAS_SYNC_CORE_BEFORE_USERMODE.
- Move empty static inline in processor.h to generic patch.

---
 arch/x86/Kconfig                 |  1 +
 arch/x86/include/asm/processor.h | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/Kconfig +1 -0
index 20da391b5f32..0b44c8dd0e95 100644
@@ -61,6 +61,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 +0 -0
index d3a67fba200a..3257d34dbb40 100644
@@ -722,6 +722,16 @@ static inline void sync_core(void)
 #endif
 }
 
Quoted 11 lines

arch/x86/include/asm/processor.h static inline void sync_core(void) · line 731

Quoted 7 lines
arch/x86/include/asm/processor.h +7 -0
+/*
+ * Ensure that a core serializing instruction is issued before returning
+ * to user-mode. x86 implements return to user-space through sysexit,
+ * sysrel, and sysretq, which are not core serializing.
+ */
+static inline void sync_core_before_usermode(void)
+{

/* With PTI, we unconditionally serialize before running user code. */ if (static_cpu_has(X86_FEATURE_PTI))
return;

One issue I'm facing with this change is header dependency:
sync_core_before_usermode() is currently implemented in arch/x86/include/asm/processor.h, but arch/x86/include/asm/cpufeature.h is needed for static_cpu_has, and it happens to include asm/processor.h.

Quoted 6 lines
Quoted 4 lines

I'm facing a similar issue for adding a (in_irq() || in_nmi()) check.

Should we move sync_core_before_usermode() to a different header, and if so, any suggestion ?

tlbflush.h, maybe?

Quoted 8 lines

Core serialization seems to be unrelated to TLB flushing though.

I'm considering to create those new header files:

include/asm-generic/sync_core.h (empty function)
arch/x86/include/asm/sync_core.h

for the sync_core_before_usermode() static inlines. Any better idea ?

Seems reasonable to me. Or maybe switch_to.h.

author maintainer
#13 lore raw

PATCH 8/11 membarrier: Provide core serializing command

In reply to Mathieu Desnoyers

Provide 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 select ARCH_HAS_MEMBARRIER_SYNC_CORE to enable support for that command on their architecture.

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]>
---
 include/linux/sched/mm.h        | 18 ++++++++++++++
 include/uapi/linux/membarrier.h | 32 ++++++++++++++++++++++++-
 init/Kconfig                    |  3 +++
 kernel/sched/core.c             |  6 ++++-
 kernel/sched/membarrier.c       | 53 +++++++++++++++++++++++++++++++----------
 5 files changed, 98 insertions(+), 14 deletions(-)
include/linux/sched/mm.h +18 -0
index c7ad7a70cfef..14553f8cc666 100644
@@ -7,6 +7,7 @@
 #include <linux/sched.h>
 #include <linux/mm_types.h>
 #include <linux/gfp.h>
+#include <linux/processor.h>
 
 /*
  * Routines for handling mm_structs
@@ -223,12 +224,26 @@ enum {
 	MEMBARRIER_STATE_PRIVATE_EXPEDITED			= (1U << 1),
 	MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY			= (1U << 2),
 	MEMBARRIER_STATE_GLOBAL_EXPEDITED			= (1U << 3),
+	MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY	= (1U << 4),
+	MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE		= (1U << 5),
+};
+
+enum {
+	MEMBARRIER_FLAG_SYNC_CORE	= (1U << 0),
 };
 
 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_HOOKS
 #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);
@@ -244,6 +259,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 */
include/uapi/linux/membarrier.h +31 -1
index d252506e1b5e..5891d7614c8c 100644
@@ -73,7 +73,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,
@@ -86,6 +86,34 @@
  *                          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.
  * @MEMBARRIER_CMD_SHARED:
  *                          Alias to MEMBARRIER_CMD_GLOBAL. Provided for
  *                          header backward compatibility.
@@ -101,6 +129,8 @@ enum membarrier_cmd {
 	MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED		= (1 << 2),
 	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),
 
 	/* Alias for header backward compatibility. */
 	MEMBARRIER_CMD_SHARED			= MEMBARRIER_CMD_GLOBAL,
init/Kconfig +3 -0
index 30208da2221f..30b65febeb23 100644
@@ -1415,6 +1415,9 @@ config USERFAULTFD
 config ARCH_HAS_MEMBARRIER_HOOKS
 	bool
 
+config ARCH_HAS_MEMBARRIER_SYNC_CORE
+	bool
+
 config EMBEDDED
 	bool "Embedded system"
 	option allnoconfig_y
kernel/sched/core.c +5 -1
index 62f269980e29..f86cbba038b9 100644
@@ -2662,9 +2662,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) {
+		membarrier_mm_sync_core_before_usermode(mm);
 		mmdrop(mm);
+	}
 	if (unlikely(prev_state == TASK_DEAD)) {
 		if (prev->sched_class->task_dead)
 			prev->sched_class->task_dead(prev);
kernel/sched/membarrier.c +41 -12

104 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#14 lore raw

PATCH 9/11 membarrier: x86: Provide core serializing command (v4)

In reply to Mathieu Desnoyers

There 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() in that specific case.

Use the new sync_core_before_usermode() to guarantee this.

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]>

---
Changes since v1:
- Use the newly introduced sync_core_before_usermode(). Move all state handling to generic code.
- Add linux/processor.h include to include/linux/sched/mm.h. Changes since v2:
- Fix use-after-free in membarrier_mm_sync_core_before_usermode. Changes since v3:
- Move generic code into separate patch.

---
 arch/x86/Kconfig          | 1 +
 arch/x86/entry/entry_32.S | 5 +++++
 arch/x86/entry/entry_64.S | 4 ++++
 arch/x86/mm/tlb.c         | 7 ++++---
 4 files changed, 14 insertions(+), 3 deletions(-)
arch/x86/Kconfig +1 -0
index 0b44c8dd0e95..b5324f2e3162 100644
@@ -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
 	select ARCH_HAS_REFCOUNT
 	select ARCH_HAS_UACCESS_FLUSHCACHE	if X86_64
arch/x86/entry/entry_32.S +5 -0
index a1f28a54f23a..0c89cef690cf 100644
@@ -554,6 +554,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 +4 -0
index 4f8e1d35a97c..8a32390240f1 100644
@@ -792,6 +792,10 @@ GLOBAL(restore_regs_and_return_to_kernel)
 	POP_EXTRA_REGS
 	POP_C_REGS
 	addq	$8, %rsp	/* skip regs->orig_ax */
+	/*
+	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on iret core serialization
+	 * when returning from IPI handler.
+	 */
 	INTERRUPT_RETURN
 
 ENTRY(native_iret)
arch/x86/mm/tlb.c +4 -3
index c28cd5592b0d..df4e21371c89 100644
@@ -201,9 +201,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) !=
Signature · 1 line
2.11.0
author maintainer
#15 lore raw

PATCH 10/11 membarrier: arm64: Provide core serializing command

In reply to Mathieu Desnoyers
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]>
---
 arch/arm64/Kconfig        | 1 +
 arch/arm64/kernel/entry.S | 4 ++++
 2 files changed, 5 insertions(+)
arch/arm64/Kconfig +1 -0
index c9a7e9e1414f..5b0c06d8dbbe 100644
@@ -16,6 +16,7 @@ config ARM64
 	select ARCH_HAS_GCOV_PROFILE_ALL
 	select ARCH_HAS_GIGANTIC_PAGE if (MEMORY_ISOLATION && COMPACTION) || CMA
 	select ARCH_HAS_KCOV
+	select ARCH_HAS_MEMBARRIER_SYNC_CORE
 	select ARCH_HAS_SET_MEMORY
 	select ARCH_HAS_SG_CHAIN
 	select ARCH_HAS_STRICT_KERNEL_RWX
arch/arm64/kernel/entry.S +4 -0
index 6d14b8f29b5f..5edde1c2e93e 100644
@@ -302,6 +302,10 @@ alternative_else_nop_endif
 	ldp	x28, x29, [sp, #16 * 14]
 	ldr	lr, [sp, #S_LR]
 	add	sp, sp, #S_FRAME_SIZE		// restore sp
+	/*
+	 * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on eret context synchronization
+	 * when returning from IPI handler, and when returning to user-space.
+	 */
 	eret					// return to kernel
 	.endm
 
Signature · 1 line
2.11.0
author maintainer
#16 lore raw

PATCH 11/11 membarrier: selftest: Test private expedited sync core cmd

In reply to Mathieu Desnoyers

Test the new MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE and MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE commands.

CC Greg Kroah-Hartman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Paul E. McKenney <[email protected]>
CC Boqun Feng <[email protected]>
CC Andrew Hunter <[email protected]>
CC Maged Michael <[email protected]>
CC Avi Kivity <[email protected]>
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <[email protected]>
CC Dave Watson <[email protected]>
CC Alan Stern <[email protected]>
CC Will Deacon <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Alice Ferrazzi <[email protected]>
CC Paul Elder <[email protected]>
---
 .../testing/selftests/membarrier/membarrier_test.c | 73 ++++++++++++++++++++++
 1 file changed, 73 insertions(+)
tools/testing/selftests/membarrier/membarrier_test.c +73 -0
index eec39a0e1f77..22bffd55a523 100644
@@ -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_register_global_expedited_success(void)
 {
 	int cmd = MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, flags = 0;
@@ -188,6 +245,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;
+	}
 	/*
 	 * It is valid to send a global membarrier from a non-registered
 	 * process.
Signature · 1 line
2.11.0