Skip to contentSkip to conversation

s390: Consistent usage of text_mutex

PATCH In review Heiko Carstens 3 participants 35 messages Oct 27 – Oct 29

Subsystems (6)
BPF s390 ftrace s390 MM +2 more subsystems
lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

v2 cover s390: Consistent usage of text_mutex

v2:
- Use text mutex for all callers of s390_kernel_write(), and add lockdep checking. [Based on a review comment from Ilya Leoshkevich, and also Sashiko feedback]

- Note: this takes now the mutex also at places where it is technically not necessary (e.g. ftrace init nop patching). However this is not much of a difference to before, where the spinlock in s390_kernel_write() was always taken instead

v1:
Other architectures serialize kernel text patching with text_mutex. s390 does not: __s390_kernel_write() relies on a private spinlock, and callers partially depend on implicit exclusion (e.g. no kprobe can be set on a jump label).

Make s390 consistent with other architectures:

- Patches 1-3 add text_mutex to the jump_label, ftrace and bpf text patching paths

- Patch 4 removes the redundant spinlock from __s390_kernel_write(). CPU serialization, previously done implicitly, is now explicit via sync_core()

Thanks,
Heiko

Heiko Carstens (10):
s390/jump_label: Serialize text patching with text_mutex s390/ftrace: Serialize text patching with text_mutex s390/bpf: Serialize text patching with text_mutex s390/runtime-const: Serialize text patching with text_mutex s390/module: Serialize text patching with text_mutex s390/stackprotector: Serialize text patching with text_mutex s390/nospec-branch: Serialize text patching with text_mutex s390/alternative: Serialize text patching with text_mutex s390/uaccess: Move s390_kernel_write() to asm/text-patching.h s390/maccess: Remove spinlock from __s390_kernel_write()

 arch/s390/include/asm/runtime-const.h |  4 +++-
 arch/s390/include/asm/text-patching.h | 14 ++++++++++++++
 arch/s390/include/asm/uaccess.h       |  9 ---------
 arch/s390/kernel/alternative.c        |  6 +++++-
 arch/s390/kernel/ftrace.c             | 11 +++++++++++
 arch/s390/kernel/jump_label.c         |  3 ++-
 arch/s390/kernel/kprobes.c            |  4 +++-
 arch/s390/kernel/module.c             |  2 ++
 arch/s390/kernel/nospec-branch.c      |  4 ++++
 arch/s390/kernel/stackprotector.c     |  6 +++++-
 arch/s390/mm/maccess.c                |  9 ++++-----
 arch/s390/net/bpf_jit_comp.c          | 21 +++++++++++++++++----
 12 files changed, 70 insertions(+), 23 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v2 1/10 s390/jump_label: Serialize text patching with text_mutex

Acquire text_mutex around the verify-then-patch sequence in jump_label_transform(). This way s390 uses text_mutex like other architectures and does not need to rely on other implicit semantics (e.g. no kprobe can be set on a jump label).

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
 arch/s390/kernel/jump_label.c | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/kernel/jump_label.c +2 -0
index e808bb8bc0da..548141eaa20b 100644
@@ -5,6 +5,7 @@
  * Copyright IBM Corp. 2011
  * Author(s): Jan Glauber <[email protected]>
  */
+#include <linux/memory.h>
 #include <linux/uaccess.h>
 #include <linux/jump_label.h>
 #include <linux/module.h>
@@ -57,6 +58,7 @@ static void jump_label_transform(struct jump_entry *entry,
 		jump_label_make_branch(entry, &old);
 		jump_label_make_nop(entry, &new);
 	}
+	guard(mutex)(&text_mutex);
 	if (memcmp(code, &old, sizeof(old)))
 		jump_label_bug(entry, &old, &new);
 	s390_kernel_write(code, &new, sizeof(new));
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 01/10] s390/jump_label: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Acquire text_mutex around the verify-then-patch sequence in jump_label_transform(). This way s390 uses text_mutex like other architectures and does not need to rely on other implicit semantics (e.g. no kprobe can be set on a jump label).

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v2 1/10
#3 lore raw

Re: [PATCH v2 01/10] s390/jump_label: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 5 lines · Heiko Carstens

Acquire text_mutex around the verify-then-patch sequence in jump_label_transform(). This way s390 uses text_mutex like other architectures and does not need to rely on other implicit semantics (e.g. no kprobe can be set on a jump label).

Quoted 7 lines

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
  arch/s390/kernel/jump_label.c | 2 ++
  1 file changed, 2 insertions(+)
author maintainer
#4 lore raw

PATCH v2 2/10 s390/ftrace: Serialize text patching with text_mutex

In reply to Heiko Carstens

Add ftrace_arch_code_modify_prepare() and extend ftrace_arch_code_modify_post_process() to acquire and release text_mutex when ftrace code is modified.

Also add locking to ftrace_init_nop(). This way s390 uses text_mutex like e.g. x86 and riscv.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
 arch/s390/kernel/ftrace.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/s390/kernel/ftrace.c +11 -0
index e94bb98f5231..2f0c4ef1b2d6 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/hardirq.h>
+#include <linux/memory.h>
 #include <linux/uaccess.h>
 #include <linux/ftrace.h>
 #include <linux/kernel.h>
@@ -90,6 +91,8 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
 	BUILD_BUG_ON(sizeof(struct ftrace_hotpatch_trampoline) !=
 		     SIZEOF_FTRACE_HOTPATCH_TRAMPOLINE);
 
+	guard(mutex)(&text_mutex);
+
 	next_trampoline = &next_vmlinux_trampoline;
 	trampolines_end = __ftrace_hotpatch_trampolines_end;
 	shared = ftrace_shared_hotpatch_trampoline(NULL);
@@ -247,13 +250,21 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
 	return 0;
 }
 
+void ftrace_arch_code_modify_prepare(void)
+	__acquires(&text_mutex)
+{
+	mutex_lock(&text_mutex);
+}
+
 void arch_ftrace_update_code(int command)
 {
 	ftrace_modify_all_code(command);
 }
 
 void ftrace_arch_code_modify_post_process(void)
+	__releases(&text_mutex)
 {
+	mutex_unlock(&text_mutex);
 	/*
 	 * Flush any pre-fetched instructions on all
 	 * CPUs to make the new code visible.
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH v2 02/10] s390/ftrace: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

Add ftrace_arch_code_modify_prepare() and extend ftrace_arch_code_modify_post_process() to acquire and release text_mutex when ftrace code is modified.

Also add locking to ftrace_init_nop(). This way s390 uses text_mutex like e.g. x86 and riscv.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by on v2 2/10
#6 lore raw

Re: [PATCH v2 02/10] s390/ftrace: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 7 lines · Heiko Carstens

Add ftrace_arch_code_modify_prepare() and extend ftrace_arch_code_modify_post_process() to acquire and release text_mutex when ftrace code is modified.

Also add locking to ftrace_init_nop(). This way s390 uses text_mutex like e.g. x86 and riscv.

Quoted 7 lines

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
  arch/s390/kernel/ftrace.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
author maintainer
#7 lore raw

PATCH v2 3/10 s390/bpf: Serialize text patching with text_mutex

In reply to Heiko Carstens

Acquire text_mutex in bpf_arch_text_poke(). This way s390 uses text_mutex like most other architectures.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
 arch/s390/net/bpf_jit_comp.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
arch/s390/net/bpf_jit_comp.c +17 -4
index 51db92c3329d..b8ff1ca2f598 100644
@@ -22,6 +22,7 @@
 #include <linux/init.h>
 #include <linux/bpf.h>
 #include <linux/cfi.h>
+#include <linux/memory.h>
 #include <linux/mm.h>
 #include <linux/kernel.h>
 #include <asm/cacheflush.h>
@@ -2446,9 +2447,9 @@ bool bpf_jit_supports_far_kfunc_call(void)
 	return true;
 }
 
-int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
-		       enum bpf_text_poke_type new_t, void *old_addr,
-		       void *new_addr)
+static int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
+				enum bpf_text_poke_type new_t, void *old_addr,
+				void *new_addr)
 {
 	struct bpf_plt expected_plt, current_plt, new_plt, *plt;
 	struct {
@@ -2458,6 +2459,8 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
 	char *ret;
 	int err;
 
+	guard(mutex)(&text_mutex);
+
 	/* Verify the branch to be patched. */
 	err = copy_from_kernel_nofault(&insn, ip, sizeof(insn));
 	if (err < 0)
@@ -2491,10 +2494,20 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
 	/* Adjust the mask of the branch. */
 	insn.opc = 0xc004 | (new_addr ? 0xf0 : 0);
 	s390_kernel_write((char *)ip + 1, (char *)&insn.opc + 1, 1);
+	return 0;
+}
 
+int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
+		       enum bpf_text_poke_type new_t, void *old_addr,
+		       void *new_addr)
+{
+	int err;
+
+	err = __bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr);
+	if (err)
+		return err;
 	/* Make the new code visible to the other CPUs. */
 	text_poke_sync_lock();
-
 	return 0;
 }
 
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v2 03/10] s390/bpf: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Acquire text_mutex in bpf_arch_text_poke(). This way s390 uses text_mutex like most other architectures.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v2 3/10
#9 lore raw

Re: [PATCH v2 03/10] s390/bpf: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 10 lines · Heiko Carstens

Acquire text_mutex in bpf_arch_text_poke(). This way s390 uses text_mutex like most other architectures.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
  arch/s390/net/bpf_jit_comp.c | 21 +++++++++++++++++----
  1 file changed, 17 insertions(+), 4 deletions(-)
author maintainer
#10 lore raw

PATCH v2 4/10 s390/runtime-const: Serialize text patching with text_mutex

In reply to Heiko Carstens

Acquire text_mutex inside runtime_const_fixup() around the patching loop.

Serialization is not required for runtime_const_fixup(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
 arch/s390/include/asm/runtime-const.h | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/include/asm/runtime-const.h +2 -0
index 7b71156031ec..0e2402717e8b 100644
@@ -3,6 +3,7 @@
 #define _ASM_S390_RUNTIME_CONST_H
 
 #include <linux/uaccess.h>
+#include <linux/memory.h>
 
 #define runtime_const_ptr(sym)					\
 ({								\
@@ -88,6 +89,7 @@ static inline void __runtime_fixup_mask(void *where, unsigned long val)
 static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
 				       unsigned long val, s32 *start, s32 *end)
 {
+	guard(mutex)(&text_mutex);
 	while (start < end) {
 		fn(*start + (void *)start, val);
 		start++;
Signature · 1 line
2.53.0
#11 lore raw

Re: [PATCH v2 04/10] s390/runtime-const: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Acquire text_mutex inside runtime_const_fixup() around the patching loop.

Serialization is not required for runtime_const_fixup(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer Reviewed-by on v2 4/10
#12 lore raw

Re: [PATCH v2 04/10] s390/runtime-const: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 25 lines · Heiko Carstens

Acquire text_mutex inside runtime_const_fixup() around the patching loop.

Serialization is not required for runtime_const_fixup(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
  arch/s390/include/asm/runtime-const.h | 2 ++
  1 file changed, 2 insertions(+)
arch/s390/include/asm/runtime-const.h +1 -0
index 7b71156031ec..0e2402717e8b 100644
@@ -3,6 +3,7 @@
  #define _ASM_S390_RUNTIME_CONST_H
 
  #include <linux/uaccess.h>
+#include <linux/memory.h>
 
  #define runtime_const_ptr(sym)					\
  ({								\

arch/s390/include/asm/runtime-const.h static inline void __runtime_fixup_mask(void *where, unsigned long val) · line 95

Quoted 7 lines
arch/s390/include/asm/runtime-const.h +1 -0
@@ -88,6 +89,7 @@ static inline void __runtime_fixup_mask(void *where, unsigned long val)
  static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
  				       unsigned long val, s32 *start, s32 *end)
  {
+	guard(mutex)(&text_mutex);
  	while (start < end) {
  		fn(*start + (void *)start, val);
  		start++;

Interesting, so clang is able to propagate context even through indirect calls without explicit annotations?

author maintainer
#13 lore raw

Re: [PATCH v2 04/10] s390/runtime-const: Serialize text patching with text_mutex

Quoted 11 lines · Ilya Leoshkevich

arch/s390/include/asm/runtime-const.h static inline void __runtime_fixup_mask(void *where, unsigned long val) · line 95

Quoted 8 lines
arch/s390/include/asm/runtime-const.h +1 -0
@@ -88,6 +89,7 @@ static inline void __runtime_fixup_mask(void *where, unsigned long val)
  static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
  				       unsigned long val, s32 *start, s32 *end)
  {
+	guard(mutex)(&text_mutex);
  	while (start < end) {
  		fn(*start + (void *)start, val);
  		start++;

Interesting, so clang is able to propagate context even through indirect calls without explicit annotations?

There is no clang context analysis involved here - this is purely lockdep based (with the last patch of this series, which adds lockdep_assert_held() to s390_kernel_write()).

maintainer
#14 lore raw

Re: [PATCH v2 04/10] s390/runtime-const: Serialize text patching with text_mutex

Quoted 13 lines · Heiko Carstens
Quoted 11 lines · Ilya Leoshkevich

arch/s390/include/asm/runtime-const.h static inline void __runtime_fixup_mask(void *where, unsigned long val) · line 95

Quoted 8 lines
arch/s390/include/asm/runtime-const.h +1 -0
@@ -88,6 +89,7 @@ static inline void __runtime_fixup_mask(void *where, unsigned long val)
   static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
   				       unsigned long val, s32 *start, s32 *end)
   {
+	guard(mutex)(&text_mutex);
   	while (start < end) {
   		fn(*start + (void *)start, val);
   		start++;

Interesting, so clang is able to propagate context even through indirect calls without explicit annotations?

Quoted 3 lines

There is no clang context analysis involved here - this is purely lockdep based (with the last patch of this series, which adds lockdep_assert_held() to s390_kernel_write()).

Ah, I see, it's stated right in the first line of the cover letter. Did you consider using it though?

author maintainer
#15 lore raw

Re: [PATCH v2 04/10] s390/runtime-const: Serialize text patching with text_mutex

Quoted 14 lines · Ilya Leoshkevich
Quoted 13 lines · Heiko Carstens
Quoted 11 lines · Ilya Leoshkevich

arch/s390/include/asm/runtime-const.h static inline void __runtime_fixup_mask(void *where, unsigned long val) · line 95

Quoted 8 lines
arch/s390/include/asm/runtime-const.h +1 -0
@@ -88,6 +89,7 @@ static inline void __runtime_fixup_mask(void *where, unsigned long val)
   static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
   				       unsigned long val, s32 *start, s32 *end)
   {
+	guard(mutex)(&text_mutex);
   	while (start < end) {
   		fn(*start + (void *)start, val);
   		start++;

Interesting, so clang is able to propagate context even through indirect calls without explicit annotations?

Quoted 6 lines
Quoted 3 lines

There is no clang context analysis involved here - this is purely lockdep based (with the last patch of this series, which adds lockdep_assert_held() to s390_kernel_write()).

Ah, I see, it's stated right in the first line of the cover letter. Did you consider using it though?

Yes I did, but if I add __must_hold(text_mutex) to the inlined s390_kernel_write(), then clang cannot prove at all call sites that the lock is actually held - so plenty of additional annotations would be required (if possible at all), and then I decided to drop that approach and rely only on lockdep.

author maintainer
#16 lore raw

PATCH v2 5/10 s390/module: Serialize text patching with text_mutex

In reply to Heiko Carstens

Acquire text_mutex in apply_relocate_add() in order to serialize text patching.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
 arch/s390/kernel/module.c | 1 +
 1 file changed, 1 insertion(+)
arch/s390/kernel/module.c +1 -0
index 9d1f8a50f5a4..79bc80d7bd02 100644
@@ -462,6 +462,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
 	if (!early)
 		write = s390_kernel_write;
 
+	guard(mutex)(&text_mutex);
 	return __apply_relocate_add(sechdrs, strtab, symindex, relsec, me,
 				    write);
 }
Signature · 1 line
2.53.0
#17 lore raw

Re: [PATCH v2 05/10] s390/module: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Acquire text_mutex in apply_relocate_add() in order to serialize text patching.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer Reviewed-by on v2 5/10
#18 lore raw

Re: [PATCH v2 05/10] s390/module: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 10 lines · Heiko Carstens

Acquire text_mutex in apply_relocate_add() in order to serialize text patching.

This is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex for serialization.

---
  arch/s390/kernel/module.c | 1 +
  1 file changed, 1 insertion(+)
author maintainer
#19 lore raw

PATCH v2 6/10 s390/stackprotector: Serialize text patching with text_mutex

In reply to Heiko Carstens

Acquire text_mutex inside __stack_protector_apply() to serialize text patching.

Serialization is not required for __stack_protector_apply(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
 arch/s390/kernel/stackprotector.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/s390/kernel/stackprotector.c +4 -0
index 8bd3ecf9200a..d72aef57a40a 100644
@@ -6,6 +6,7 @@
 
 #include <linux/export.h>
 #include <linux/hex.h>
+#include <linux/memory.h>
 #include <linux/uaccess.h>
 #include <linux/printk.h>
 #include <asm/abs_lowcore.h>
@@ -120,6 +121,9 @@ int __stack_protector_apply(unsigned long *start, unsigned long *end, unsigned l
 	struct insn_ril *insn, new;
 	int rc;
 
+#ifndef __DECOMPRESSOR
+	guard(mutex)(&text_mutex);
+#endif
 	/*
 	 * Convert LARL/LGRL instructions to LLILF so register R1 contains the
 	 * address of the per-cpu / per-process stack canary:
Signature · 1 line
2.53.0
#20 lore raw

Re: [PATCH v2 06/10] s390/stackprotector: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Acquire text_mutex inside __stack_protector_apply() to serialize text patching.

Serialization is not required for __stack_protector_apply(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer Reviewed-by on v2 6/10
#21 lore raw

Re: [PATCH v2 06/10] s390/stackprotector: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 11 lines · Heiko Carstens

Acquire text_mutex inside __stack_protector_apply() to serialize text patching.

Serialization is not required for __stack_protector_apply(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
  arch/s390/kernel/stackprotector.c | 4 ++++
  1 file changed, 4 insertions(+)
author maintainer
#22 lore raw

PATCH v2 7/10 s390/nospec-branch: Serialize text patching with text_mutex

In reply to Heiko Carstens

Acquire text_mutex inside __nospec_revert() around the patching loop.

Serialization is not required for __nospec_revert(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
 arch/s390/kernel/nospec-branch.c | 2 ++
 1 file changed, 2 insertions(+)
arch/s390/kernel/nospec-branch.c +2 -0
index e11ec15960a1..26cd5b278e8a 100644
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <linux/memory.h>
 #include <linux/module.h>
 #include <linux/device.h>
 #include <linux/cpu.h>
@@ -108,6 +109,7 @@ static void __init_or_module __nospec_revert(s32 *start, s32 *end)
 
 	/* Second part of the instruction replace is always a nop */
 	memcpy(insnbuf + 2, branch, sizeof(branch));
+	guard(mutex)(&text_mutex);
 	for (epo = start; epo < end; epo++) {
 		instr = (u8 *) epo + *epo;
 		if (instr[0] == 0xc0 && (instr[1] & 0x0f) == 0x04)
Signature · 1 line
2.53.0
#23 lore raw

Re: [PATCH v2 07/10] s390/nospec-branch: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Acquire text_mutex inside __nospec_revert() around the patching loop.

Serialization is not required for __nospec_revert(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer Reviewed-by on v2 7/10
#24 lore raw

Re: [PATCH v2 07/10] s390/nospec-branch: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 10 lines · Heiko Carstens

Acquire text_mutex inside __nospec_revert() around the patching loop.

Serialization is not required for __nospec_revert(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
  arch/s390/kernel/nospec-branch.c | 2 ++
  1 file changed, 2 insertions(+)
author maintainer
#25 lore raw

PATCH v2 8/10 s390/alternative: Serialize text patching with text_mutex

In reply to Heiko Carstens

Acquire text_mutex inside __apply_alternatives() around the patching loop.

Serialization is not required for __apply_alternatives(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
 arch/s390/kernel/alternative.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/s390/kernel/alternative.c +4 -0
index 02d04ae621ba..6013a75f5241 100644
@@ -5,6 +5,7 @@
 #endif
 
 #include <linux/hex.h>
+#include <linux/memory.h>
 #include <linux/uaccess.h>
 #include <linux/printk.h>
 #include <asm/nospec-branch.h>
@@ -55,6 +56,9 @@ void __apply_alternatives(struct alt_instr *start, struct alt_instr *end, unsign
 	bool debug, replace;
 	u8 *old, *new;
 
+#ifndef __DECOMPRESSOR
+	guard(mutex)(&text_mutex);
+#endif
 	/*
 	 * The scan order should be from start to end. A later scanned
 	 * alternative code can overwrite previously scanned alternative code.
Signature · 1 line
2.53.0
#26 lore raw

Re: [PATCH v2 08/10] s390/alternative: Serialize text patching with text_mutex

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Acquire text_mutex inside __apply_alternatives() around the patching loop.

Serialization is not required for __apply_alternatives(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
maintainer Reviewed-by on v2 8/10
#27 lore raw

Re: [PATCH v2 08/10] s390/alternative: Serialize text patching with text_mutex

In reply to Heiko Carstens
Quoted 11 lines · Heiko Carstens

Acquire text_mutex inside __apply_alternatives() around the patching loop.

Serialization is not required for __apply_alternatives(), however this is part of larger series where at the end s390_kernel_write() requires that callers hold text_mutex.

---
  arch/s390/kernel/alternative.c | 4 ++++
  1 file changed, 4 insertions(+)
author maintainer
#28 lore raw

PATCH v2 9/10 s390/uaccess: Move s390_kernel_write() to asm/text-patching.h

In reply to Heiko Carstens

s390_kernel_write() is a text-patching primitive, not a user access function. Move its declaration and the inline wrapper from uaccess.h to text-patching.h.

---
 arch/s390/include/asm/runtime-const.h |  2 +-
 arch/s390/include/asm/text-patching.h | 11 +++++++++++
 arch/s390/include/asm/uaccess.h       |  9 ---------
 arch/s390/kernel/alternative.c        |  2 +-
 arch/s390/kernel/jump_label.c         |  1 -
 arch/s390/kernel/module.c             |  1 +
 arch/s390/kernel/nospec-branch.c      |  2 ++
 arch/s390/kernel/stackprotector.c     |  2 +-
 arch/s390/mm/maccess.c                |  1 +
 9 files changed, 18 insertions(+), 13 deletions(-)
arch/s390/include/asm/runtime-const.h +1 -1
index 0e2402717e8b..4d73a198f63f 100644
@@ -2,7 +2,7 @@
 #ifndef _ASM_S390_RUNTIME_CONST_H
 #define _ASM_S390_RUNTIME_CONST_H
 
-#include <linux/uaccess.h>
+#include <linux/text-patching.h>
 #include <linux/memory.h>
 
 #define runtime_const_ptr(sym)					\
arch/s390/include/asm/text-patching.h +11 -0
index b219056a8817..c2f87f20ff97 100644
@@ -3,6 +3,8 @@
 #ifndef _ASM_S390_TEXT_PATCHING_H
 #define _ASM_S390_TEXT_PATCHING_H
 
+#include <linux/string.h>
+#include <linux/memory.h>
 #include <asm/barrier.h>
 
 static __always_inline void sync_core(void)
@@ -13,4 +15,13 @@ static __always_inline void sync_core(void)
 void text_poke_sync(void);
 void text_poke_sync_lock(void);
 
+void *__s390_kernel_write(void *dst, const void *src, size_t size);
+
+static inline void *s390_kernel_write(void *dst, const void *src, size_t size)
+{
+	if (__is_defined(__DECOMPRESSOR))
+		return memcpy(dst, src, size);
+	return __s390_kernel_write(dst, src, size);
+}
+
 #endif /* _ASM_S390_TEXT_PATCHING_H */
arch/s390/include/asm/uaccess.h +0 -9
index ac99b32c6ceb..db6acea9e7b8 100644
@@ -384,15 +384,6 @@ static __always_inline unsigned long __must_check clear_user(void __user *to, un
 	return __clear_user(to, n);
 }
 
-void *__s390_kernel_write(void *dst, const void *src, size_t size);
-
-static inline void *s390_kernel_write(void *dst, const void *src, size_t size)
-{
-	if (__is_defined(__DECOMPRESSOR))
-		return memcpy(dst, src, size);
-	return __s390_kernel_write(dst, src, size);
-}
-
 void __noreturn __mvc_kernel_nofault_bad(void);
 
 #if defined(CONFIG_CC_HAS_ASM_GOTO_OUTPUT) && defined(CONFIG_CC_HAS_ASM_AOR_FORMAT_FLAGS)
arch/s390/kernel/alternative.c +1 -1
index 6013a75f5241..8efd2165692b 100644
@@ -4,9 +4,9 @@
 #define pr_fmt(fmt)	"alt: " fmt
 #endif
 
+#include <linux/text-patching.h>
 #include <linux/hex.h>
 #include <linux/memory.h>
-#include <linux/uaccess.h>
 #include <linux/printk.h>
 #include <asm/nospec-branch.h>
 #include <asm/abs_lowcore.h>
arch/s390/kernel/jump_label.c +0 -1
index 548141eaa20b..5c3083c917bb 100644
@@ -6,7 +6,6 @@
  * Author(s): Jan Glauber <[email protected]>
  */
 #include <linux/memory.h>
-#include <linux/uaccess.h>
 #include <linux/jump_label.h>
 #include <linux/module.h>
 #include <asm/text-patching.h>
arch/s390/kernel/module.c +1 -0
index 79bc80d7bd02..4e03f40d2b2e 100644
@@ -10,6 +10,7 @@
  *  based on i386 version
  *    Copyright (C) 2001 Rusty Russell.
  */
+#include <linux/text-patching.h>
 #include <linux/module.h>
 #include <linux/elf.h>
 #include <linux/vmalloc.h>
arch/s390/kernel/nospec-branch.c +2 -0
index 26cd5b278e8a..1d9d05e1def4 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+
+#include <linux/text-patching.h>
 #include <linux/memory.h>
 #include <linux/module.h>
 #include <linux/device.h>
arch/s390/kernel/stackprotector.c +1 -1
index d72aef57a40a..5da113c2c7f6 100644
@@ -4,10 +4,10 @@
 #define pr_fmt(fmt)	"stackprot: " fmt
 #endif
 
+#include <linux/text-patching.h>
 #include <linux/export.h>
 #include <linux/hex.h>
 #include <linux/memory.h>
-#include <linux/uaccess.h>
 #include <linux/printk.h>
 #include <asm/abs_lowcore.h>
 #include <asm/sections.h>
arch/s390/mm/maccess.c +1 -0
index f39968dd8063..6b25203fb05c 100644
@@ -6,6 +6,7 @@
  *
  */
 
+#include <linux/text-patching.h>
 #include <linux/uaccess.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
Signature · 1 line
2.53.0
#29 lore raw

Re: [PATCH v2 09/10] s390/uaccess: Move s390_kernel_write() to asm/text-patching.h

Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

s390_kernel_write() is a text-patching primitive, not a user access function. Move its declaration and the inline wrapper from uaccess.h to text-patching.h.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
maintainer Reviewed-by on v2 9/10
#30 lore raw

Re: [PATCH v2 09/10] s390/uaccess: Move s390_kernel_write() to asm/text-patching.h

In reply to Heiko Carstens
Quoted 5 lines · Heiko Carstens

s390_kernel_write() is a text-patching primitive, not a user access function. Move its declaration and the inline wrapper from uaccess.h to text-patching.h.

Quoted 11 lines
---
  arch/s390/include/asm/runtime-const.h |  2 +-
  arch/s390/include/asm/text-patching.h | 11 +++++++++++
  arch/s390/include/asm/uaccess.h       |  9 ---------
  arch/s390/kernel/alternative.c        |  2 +-
  arch/s390/kernel/jump_label.c         |  1 -
  arch/s390/kernel/module.c             |  1 +
  arch/s390/kernel/nospec-branch.c      |  2 ++
  arch/s390/kernel/stackprotector.c     |  2 +-
  arch/s390/mm/maccess.c                |  1 +
  9 files changed, 18 insertions(+), 13 deletions(-)
author maintainer
#31 lore raw

PATCH v2 10/10 s390/maccess: Remove spinlock from __s390_kernel_write()

In reply to Heiko Carstens

The spinlock in __s390_kernel_write() was introduced to serialize the read-modify-write sequence against concurrent callers. Now that all runtime callers of s390_kernel_write() hold the text_mutex, the spinlock is not required anymore.

Replace it with preempt_disable() / preempt_enable() and sync_core(). Previously, CPU serialization was implicitly done by other instructions. Make this explicit with sync_core() instead of relying on that.

Preemption is disabled so that sync_core() issues the CPU serialization instruction on the same CPU that executed the stores. This completes all previous stores and makes them visible to other CPUs.

Other CPUs need to perform CPU serialization, e.g. with text_poke_sync(), to discard already prefetched instructions.

To verify that text_mutex is held instead of the spinlock, add a lockdep_assert_held() call to s390_kernel_write(). To make that work a change to the kprobes code is required:

swap_instruction(), which calls s390_kernel_write(), may be called in stop_machine() context.
For that case the task which calls s390_kernel_write() is not the task which called stop_machine() - lockdep_assert_held() will report that text_mutex is not held by the current task, even though text_mutex is held by the stop_machine() caller. Exclusive text write access is guaranteed.
Work around that by calling the non-checking __s390_kernel_write(), and add lockdep_assert_held() checks to arch_arm_kprobe() and arch_disarm_kprobe().

---
 arch/s390/include/asm/text-patching.h | 7 +++++--
 arch/s390/kernel/kprobes.c            | 4 +++-
 arch/s390/mm/maccess.c                | 8 +++-----
 3 files changed, 11 insertions(+), 8 deletions(-)
arch/s390/include/asm/text-patching.h +5 -2
index c2f87f20ff97..a382a3e7c2f8 100644
@@ -19,9 +19,12 @@ void *__s390_kernel_write(void *dst, const void *src, size_t size);
 
 static inline void *s390_kernel_write(void *dst, const void *src, size_t size)
 {
-	if (__is_defined(__DECOMPRESSOR))
-		return memcpy(dst, src, size);
+#ifdef __DECOMPRESSOR
+	return memcpy(dst, src, size);
+#else
+	lockdep_assert_held(&text_mutex);
 	return __s390_kernel_write(dst, src, size);
+#endif
 }
 
 #endif /* _ASM_S390_TEXT_PATCHING_H */
arch/s390/kernel/kprobes.c +3 -1
index e806b124e4ba..ca7fb8b91c5a 100644
@@ -148,7 +148,7 @@ static int swap_instruction(void *data)
 	u16 opc;
 
 	opc = args->arm_kprobe ? BREAKPOINT_INSTRUCTION : p->opcode;
-	s390_kernel_write(p->addr, &opc, sizeof(opc));
+	__s390_kernel_write(p->addr, &opc, sizeof(opc));
 	return 0;
 }
 NOKPROBE_SYMBOL(swap_instruction);
@@ -157,6 +157,7 @@ void arch_arm_kprobe(struct kprobe *p)
 {
 	struct swap_insn_args args = {.p = p, .arm_kprobe = 1};
 
+	lockdep_assert_held(&text_mutex);
 	if (cpu_has_seq_insn()) {
 		swap_instruction(&args);
 		text_poke_sync();
@@ -170,6 +171,7 @@ void arch_disarm_kprobe(struct kprobe *p)
 {
 	struct swap_insn_args args = {.p = p, .arm_kprobe = 0};
 
+	lockdep_assert_held(&text_mutex);
 	if (cpu_has_seq_insn()) {
 		swap_instruction(&args);
 		text_poke_sync();
arch/s390/mm/maccess.c +3 -5
index 6b25203fb05c..cda3f81dfbc2 100644
@@ -61,22 +61,20 @@ static notrace long s390_kernel_write_odd(void *dst, const void *src, size_t siz
  * bytes from destination at an eight byte boundary, modifies the bytes
  * requested and writes the result back in a loop.
  */
-static DEFINE_SPINLOCK(s390_kernel_write_lock);
-
 notrace void *__s390_kernel_write(void *dst, const void *src, size_t size)
 {
 	void *tmp = dst;
-	unsigned long flags;
 	long copied;
 
-	spin_lock_irqsave(&s390_kernel_write_lock, flags);
+	preempt_disable();
 	while (size) {
 		copied = s390_kernel_write_odd(tmp, src, size);
 		tmp += copied;
 		src += copied;
 		size -= copied;
 	}
-	spin_unlock_irqrestore(&s390_kernel_write_lock, flags);
+	sync_core();
+	preempt_enable();
 
 	return dst;
 }
Signature · 1 line
2.53.0
#32 lore raw

Re: [PATCH v2 10/10] s390/maccess: Remove spinlock from __s390_kernel_write()

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

The spinlock in __s390_kernel_write() was introduced to serialize the read-modify-write sequence against concurrent callers. Now that all runtime callers of s390_kernel_write() hold the text_mutex, the spinlock is not required anymore.

Replace it with preempt_disable() / preempt_enable() and sync_core(). Previously, CPU serialization was implicitly done by other instructions. Make this explicit with sync_core() instead of relying on that.

Preemption is disabled so that sync_core() issues the CPU serialization instruction on the same CPU that executed the stores. This completes all previous stores and makes them visible to other CPUs.

Quoted 7 lines

Other CPUs need to perform CPU serialization, e.g. with text_poke_sync(), [ ... ]
Work around that by calling the non-checking __s390_kernel_write(), and add lockdep_assert_held() checks to arch_arm_kprobe() and arch_disarm_kprobe().

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
maintainer Reviewed-by on v2 10/10
#33 lore raw

Re: [PATCH v2 10/10] s390/maccess: Remove spinlock from __s390_kernel_write()

In reply to Heiko Carstens
Quoted 5 lines · Heiko Carstens

The spinlock in __s390_kernel_write() was introduced to serialize the read-modify-write sequence against concurrent callers. Now that all runtime callers of s390_kernel_write() hold the text_mutex, the spinlock is not required anymore.

Quoted 8 lines

Replace it with preempt_disable() / preempt_enable() and sync_core(). Previously, CPU serialization was implicitly done by other instructions. Make this explicit with sync_core() instead of relying on that.

Preemption is disabled so that sync_core() issues the CPU serialization instruction on the same CPU that executed the stores. This completes all previous stores and makes them visible to other CPUs.

Doesn't preemption happen only via interrupts, which always serialize?

Quoted 32 lines

Other CPUs need to perform CPU serialization, e.g. with text_poke_sync(), to discard already prefetched instructions.

To verify that text_mutex is held instead of the spinlock, add a lockdep_assert_held() call to s390_kernel_write(). To make that work a change to the kprobes code is required:

swap_instruction(), which calls s390_kernel_write(), may be called in stop_machine() context.
For that case the task which calls s390_kernel_write() is not the task which called stop_machine() - lockdep_assert_held() will report that text_mutex is not held by the current task, even though text_mutex is held by the stop_machine() caller. Exclusive text write access is guaranteed.
Work around that by calling the non-checking __s390_kernel_write(), and add lockdep_assert_held() checks to arch_arm_kprobe() and arch_disarm_kprobe().

---
  arch/s390/include/asm/text-patching.h | 7 +++++--
  arch/s390/kernel/kprobes.c            | 4 +++-
  arch/s390/mm/maccess.c                | 8 +++-----
  3 files changed, 11 insertions(+), 8 deletions(-)
arch/s390/include/asm/text-patching.h +0 -0
index c2f87f20ff97..a382a3e7c2f8 100644
@@ -19,9 +19,12 @@ void *__s390_kernel_write(void *dst, const void *src, size_t size);
 
  static inline void *s390_kernel_write(void *dst, const void *src, size_t size)

arch/s390/include/asm/text-patching.h void *__s390_kernel_write(void *dst, const void *src, size_t size); · line 28

Quoted 10 lines
arch/s390/include/asm/text-patching.h +5 -2
  {
-	if (__is_defined(__DECOMPRESSOR))
-		return memcpy(dst, src, size);
+#ifdef __DECOMPRESSOR
+	return memcpy(dst, src, size);
+#else
+	lockdep_assert_held(&text_mutex);
  	return __s390_kernel_write(dst, src, size);
+#endif
  }

I assume #ifdef is required because lockdep_assert_held() is not available in decompressor and there are no stubs?

Anyhow:

[...]

author maintainer
#34 lore raw

Re: [PATCH v2 10/10] s390/maccess: Remove spinlock from __s390_kernel_write()

Quoted 11 lines · Ilya Leoshkevich
Quoted 10 lines · Heiko Carstens

The spinlock in __s390_kernel_write() was introduced to serialize the read-modify-write sequence against concurrent callers. Now that all runtime callers of s390_kernel_write() hold the text_mutex, the spinlock is not required anymore.

Replace it with preempt_disable() / preempt_enable() and sync_core(). Previously, CPU serialization was implicitly done by other instructions. Make this explicit with sync_core() instead of relying on that.

Quoted 5 lines
Quoted 3 lines

Preemption is disabled so that sync_core() issues the CPU serialization instruction on the same CPU that executed the stores. This completes all previous stores and makes them visible to other CPUs.

Doesn't preemption happen only via interrupts, which always serialize?

Yes, but again, that's an implicit serialization. The idea of this was to make the code not relying on rather subtle architecture details (as written above, the "implicit" part).

Even if interrupts wouldn't serialize: when scheduling the task away there is almost certainly a compare and swap instruction for taking some lock executed on the CPU before the next task runs.

So this part is more about making things explicit, instead of relying on something that is implicitly happening.

But the main point is of course to get rid of our private locking.

Quoted 6 lines
Quoted 6 lines

static inline void *s390_kernel_write(void *dst, const void *src, size_t size) {

arch/s390/include/asm/text-patching.h +2 -2
-	if (__is_defined(__DECOMPRESSOR))
-		return memcpy(dst, src, size);
+#ifdef __DECOMPRESSOR
+	return memcpy(dst, src, size);
Quoted 8 lines

arch/s390/include/asm/text-patching.h void *__s390_kernel_write(void *dst, const void *src, size_t size); · line 28

Quoted 5 lines
arch/s390/include/asm/text-patching.h +3 -0
+#else
+	lockdep_assert_held(&text_mutex);
  	return __s390_kernel_write(dst, src, size);
+#endif
  }

I assume #ifdef is required because lockdep_assert_held() is not available in decompressor and there are no stubs?

To be honest, I didn't even try; I didn't want to rely on even more things for the decompressor.