Skip to contentSkip to conversation

s390: Add __must_check to set_memory() primitives

PATCH In review Heiko Carstens 3 participants 18 messages Nov 18 – Nov 19

Subsystems (3)
lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

cover s390: Add __must_check to set_memory() primitives

A small series which adds missing return value checks of set_memory() calls to s390 specific call sites. Also add __must_check to set_memory() primitives in order to avoid that new unchecked callers will be added.

This is basically the same what was done for powerpc with [1].

Note that this (trivially) conflicts with [2]. Furthermore the kexec code is the only call site which for now only prints an error message instead of really handling the error, since error handling in common code is missing. That needs to be added with later changes.

While doing this conversion I realized that s390's execmem support also can be improved. Will happen with a follow-on series.

[1] 2abbd6d5fbe0 ("powerpc: Add __must_check to set_memory_...()")
[2] https://lore.kernel.org/all/[email protected]/

Thanks,
Heiko

Heiko Carstens (8):
s390/kfence: Add error handling to arch_kfence_init_pool() s390/kprobes: Add error handling to alloc_insn_page() s390/kexec: Let crash_protect_pages() warn in case of an error s390/module: Add error handling to module_alloc_ftrace_hotpatch_trampolines() s390/mm: Add error handling to arch_setup_zero_pages()
s390/mm: Add error handling to mark_rodata_ro()
s390/mm: Add error handling to vmem_map_init()
s390/set_memory: Add __must_check to set_memory() primitives

 arch/s390/include/asm/kfence.h     |  3 ++-
 arch/s390/include/asm/set_memory.h |  4 ++--
 arch/s390/kernel/kprobes.c         |  5 ++++-
 arch/s390/kernel/machine_kexec.c   |  7 +++++--
 arch/s390/kernel/module.c          |  7 ++++++-
 arch/s390/mm/init.c                | 10 ++++++++--
 arch/s390/mm/vmem.c                | 14 +++++++++-----
 7 files changed, 36 insertions(+), 14 deletions(-)

base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4

Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 1/8 s390/kfence: Add error handling to arch_kfence_init_pool()

If set_memory_4k() fails, return false from arch_kfence_init_pool() instead of silently ignoring.

---
 arch/s390/include/asm/kfence.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/include/asm/kfence.h +2 -1
index e95e35eb8a3f..414c235a91b8 100644
@@ -15,7 +15,8 @@ static __always_inline bool arch_kfence_init_pool(void)
 #ifdef CONFIG_KFENCE
 	unsigned long pool_pages = KFENCE_POOL_SIZE >> PAGE_SHIFT;
 
-	set_memory_4k((unsigned long)__kfence_pool, pool_pages);
+	if (set_memory_4k((unsigned long)__kfence_pool, pool_pages))
+		return false;
 #endif
 	return true;
 }
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH 1/8] s390/kfence: Add error handling to arch_kfence_init_pool()

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

If set_memory_4k() fails, return false from arch_kfence_init_pool() instead of silently ignoring.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/8 s390/kprobes: Add error handling to alloc_insn_page()

In reply to Heiko Carstens

If set_memory_rox() fails, let alloc_insn_page return NULL, instead of a pointer to a not executable page.

---
 arch/s390/kernel/kprobes.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/s390/kernel/kprobes.c +4 -1
index e806b124e4ba..90efe90c609e 100644
@@ -40,7 +40,10 @@ void *alloc_insn_page(void)
 	page = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
 	if (!page)
 		return NULL;
-	set_memory_rox((unsigned long)page, 1);
+	if (set_memory_rox((unsigned long)page, 1)) {
+		execmem_free(page);
+		return NULL;
+	}
 	return page;
 }
 
Signature · 1 line
2.56.1
#4 lore raw

Re: [PATCH 2/8] s390/kprobes: Add error handling to alloc_insn_page()

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

If set_memory_rox() fails, let alloc_insn_page return NULL, instead of a pointer to a not executable page.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

PATCH 3/8 s390/kexec: Let crash_protect_pages() warn in case of an error

In reply to Heiko Carstens

Let crash_protect_pages() print an error message in case set_memory*() fails.

This doesn't buy too much since a failing set_memory_rw() would result in a crash of the caller as soon as a write to the memory area happens. Fixing that requires larger changes - for now only print a message.

---
 arch/s390/kernel/machine_kexec.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/s390/kernel/machine_kexec.c +5 -2
index baeb3dcfc1c8..bf65bea5fa01 100644
@@ -152,14 +152,17 @@ void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
 static void crash_protect_pages(int protect)
 {
 	unsigned long size;
+	int rc;
 
 	if (!crashk_res.end)
 		return;
 	size = resource_size(&crashk_res);
 	if (protect)
-		set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
+		rc = set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
 	else
-		set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
+		rc = set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
+	if (rc)
+		pr_warn("Failed to %sprotect crashkernel memory: %d\n",	protect ? "" : "un", rc);
 }
 
 void arch_kexec_protect_crashkres(void)
Signature · 1 line
2.56.1
#6 lore raw

Re: [PATCH 3/8] s390/kexec: Let crash_protect_pages() warn in case of an error

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

Let crash_protect_pages() print an error message in case set_memory*() fails.

This doesn't buy too much since a failing set_memory_rw() would result in a crash of the caller as soon as a write to the memory area happens. Fixing that requires larger changes - for now only print a message.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#7 lore raw

PATCH 4/8 s390/module: Add error handling to module_alloc_ftrace_hotpatch_trampolines()

In reply to Heiko Carstens

Let module_alloc_ftrace_hotpatch_trampolines() return NULL, instead of a pointer to a not executable page if set_memory_rox() fails.

---
 arch/s390/kernel/module.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/s390/kernel/module.c +6 -1
index 9d1f8a50f5a4..86ace854e22d 100644
@@ -473,13 +473,18 @@ static int module_alloc_ftrace_hotpatch_trampolines(struct module *me,
 	char *start, *end;
 	int numpages;
 	size_t size;
+	int rc;
 
 	size = FTRACE_HOTPATCH_TRAMPOLINES_SIZE(s->sh_size);
 	numpages = DIV_ROUND_UP(size, PAGE_SIZE);
 	start = execmem_alloc(EXECMEM_FTRACE, numpages * PAGE_SIZE);
 	if (!start)
 		return -ENOMEM;
-	set_memory_rox((unsigned long)start, numpages);
+	rc = set_memory_rox((unsigned long)start, numpages);
+	if (rc) {
+		execmem_free(start);
+		return rc;
+	}
 	end = start + size;
 
 	me->arch.trampolines_start = (struct ftrace_hotpatch_trampoline *)start;
Signature · 1 line
2.56.1
#8 lore raw

Re: [PATCH 4/8] s390/module: Add error handling to module_alloc_ftrace_hotpatch_trampolines()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] s390/module: inaccurate return value in commit message for trampoline alloc --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] s390/module: inaccurate return value in commit message for trampoline alloc
--

commit 1f1b0c01e4a7101a1a14aadfdd4c9c1dbcd1303a
Author: Heiko Carstens <[email protected]>

s390/module: Add error handling to module_alloc_ftrace_hotpatch_trampolines()

This commit checks the return value of set_memory_rox() in module_alloc_ftrace_hotpatch_trampolines() and frees the allocated trampoline memory on failure.

Quoted 2 lines

Let module_alloc_ftrace_hotpatch_trampolines() return NULL, instead of a pointer to a not executable page if set_memory_rox() fails.

[Severity: Low]
This isn't a bug in the code, but module_alloc_ftrace_hotpatch_trampolines() returns an int error code (rc) and leaves me->arch.trampolines_start unset rather than returning a pointer or NULL.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#9 lore raw

PATCH 5/8 s390/mm: Add error handling to arch_setup_zero_pages()

In reply to Heiko Carstens

If set_memory_ro() fails early in arch_setup_zero_pages() just panic. This should never happen.

---
 arch/s390/mm/init.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/s390/mm/init.c +4 -1
index be7e009e7b59..4d5a13b19dca 100644
@@ -74,6 +74,7 @@ void __init arch_setup_zero_pages(void)
 {
 	unsigned long total_pages = memblock_estimated_nr_free_pages();
 	unsigned int order;
+	int rc;
 
 	/* Latest machines require a mapping granularity of 512KB */
 	order = 7;
@@ -86,7 +87,9 @@ void __init arch_setup_zero_pages(void)
 
 	zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK;
 
-	set_memory_ro(empty_zero_page, 1UL << order);
+	rc = set_memory_ro(empty_zero_page, 1UL << order);
+	if (rc)
+		panic("%s() failed, err = %d\n", __func__, rc);
 }
 
 void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
Signature · 1 line
2.56.1
#10 lore raw

Re: [PATCH 5/8] s390/mm: Add error handling to arch_setup_zero_pages()

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

If set_memory_ro() fails early in arch_setup_zero_pages() just panic. This should never happen.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author maintainer
#11 lore raw

PATCH 6/8 s390/mm: Add error handling to mark_rodata_ro()

In reply to Heiko Carstens

If __set_memory_ro() fails early in mark_rodata_ro() just panic. This should never happen.

---
 arch/s390/mm/init.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/s390/mm/init.c +4 -1
index 4d5a13b19dca..18ce7ea2f653 100644
@@ -110,10 +110,13 @@ void __init paging_init(void)
 void mark_rodata_ro(void)
 {
 	unsigned long size = __end_ro_after_init - __start_ro_after_init;
+	int rc;
 
 	if (cpu_has_nx())
 		system_ctl_set_bit(0, CR0_INSTRUCTION_EXEC_PROTECTION_BIT);
-	__set_memory_ro(__start_ro_after_init, __end_ro_after_init);
+	rc = __set_memory_ro(__start_ro_after_init, __end_ro_after_init);
+	if (rc)
+		panic("%s() failed, err = %d\n", __func__, rc);
 	pr_info("Write protected read-only-after-init data: %luk\n", size >> 10);
 }
 
Signature · 1 line
2.56.1
#12 lore raw

Re: [PATCH 6/8] s390/mm: Add error handling to mark_rodata_ro()

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

If __set_memory_ro() fails early in mark_rodata_ro() just panic. This should never happen.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author maintainer
#13 lore raw

PATCH 7/8 s390/mm: Add error handling to vmem_map_init()

In reply to Heiko Carstens

If any of the set_memory() calls in vmem_map_init() fails just panic. This should never happen, and would lead to a broken kernel mapping.

---
 arch/s390/mm/vmem.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
arch/s390/mm/vmem.c +9 -5
index d2879ce860a1..317bd01dca86 100644
@@ -671,18 +671,22 @@ void vmem_unmap_4k_page(unsigned long addr)
 
 void __init vmem_map_init(void)
 {
-	__set_memory_rox(_stext, _etext);
-	__set_memory_ro(_etext, __end_rodata);
-	__set_memory_rox(__stext_amode31, __etext_amode31);
+	int rc;
+
+	rc  = __set_memory_rox(_stext, _etext);
+	rc |= __set_memory_ro(_etext, __end_rodata);
+	rc |= __set_memory_rox(__stext_amode31, __etext_amode31);
 	/*
 	 * If the BEAR-enhancement facility is not installed the first
 	 * prefix page is used to return to the previous context with
 	 * an LPSWE instruction and therefore must be executable.
 	 */
 	if (!cpu_has_bear())
-		set_memory_x(0, 1);
+		rc |= set_memory_x(0, 1);
 	if (debug_pagealloc_enabled())
-		__set_memory_4k(__va(0), absolute_pointer(__va(0)) + ident_map_size);
+		rc |= __set_memory_4k(__va(0), absolute_pointer(__va(0)) + ident_map_size);
+	if (rc)
+		panic("Failed to set kernel mapping permissions");
 	pr_info("Write protected kernel read-only data: %luk\n",
 		(unsigned long)(__end_rodata - _stext) >> 10);
 }
Signature · 1 line
2.56.1
#14 lore raw

Re: [PATCH 7/8] s390/mm: Add error handling to vmem_map_init()

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

If any of the set_memory() calls in vmem_map_init() fails just panic. This should never happen, and would lead to a broken kernel mapping.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author maintainer
#15 lore raw

PATCH 8/8 s390/set_memory: Add __must_check to set_memory() primitives

In reply to Heiko Carstens

All set_memory() callers in s390 architecture code check the returned value. Add __must_check to all variants to make sure no new callers which don't check the return value are added.

This is the s390 version of commit 2abbd6d5fbe0 ("powerpc: Add __must_check to set_memory_...()")

---
 arch/s390/include/asm/set_memory.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/s390/include/asm/set_memory.h +2 -2
index e3562bf0c1aa..036295215a1a 100644
@@ -39,12 +39,12 @@ int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flags
  * for areas larger than 8TB, which may happen at memory initialization.
  */
 #define __SET_MEMORY_FUNC(fname, flags)					\
-static inline int fname(unsigned long addr, int numpages)		\
+static inline int __must_check fname(unsigned long addr, int numpages)	\
 {									\
 	return __set_memory(addr, numpages, (flags));			\
 }									\
 									\
-static inline int __##fname(void *start, void *end)			\
+static inline int __must_check __##fname(void *start, void *end)	\
 {									\
 	unsigned long numpages;						\
 									\
Signature · 1 line
2.56.1
#16 lore raw

Re: [PATCH 8/8] s390/set_memory: Add __must_check to set_memory() primitives

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

All set_memory() callers in s390 architecture code check the returned value. Add __must_check to all variants to make sure no new callers which don't check the return value are added.

This is the s390 version of commit 2abbd6d5fbe0 ("powerpc: Add __must_check to set_memory_...()")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Acked-by on the v1 series
#17 lore raw
In reply to Heiko Carstens
Quoted 20 lines · Heiko Carstens

A small series which adds missing return value checks of set_memory() calls to s390 specific call sites. Also add __must_check to set_memory() primitives in order to avoid that new unchecked callers will be added.

This is basically the same what was done for powerpc with [1].

Note that this (trivially) conflicts with [2]. Furthermore the kexec code is the only call site which for now only prints an error message instead of really handling the error, since error handling in common code is missing. That needs to be added with later changes.

While doing this conversion I realized that s390's execmem support also can be improved. Will happen with a follow-on series.

[1] 2abbd6d5fbe0 ("powerpc: Add __must_check to set_memory_...()")
[2] https://lore.kernel.org/all/[email protected]/

Thanks,
Heiko

Quoted 9 lines

Heiko Carstens (8):
s390/kfence: Add error handling to arch_kfence_init_pool() s390/kprobes: Add error handling to alloc_insn_page() s390/kexec: Let crash_protect_pages() warn in case of an error s390/module: Add error handling to module_alloc_ftrace_hotpatch_trampolines() s390/mm: Add error handling to arch_setup_zero_pages()
s390/mm: Add error handling to mark_rodata_ro()
s390/mm: Add error handling to vmem_map_init()
s390/set_memory: Add __must_check to set_memory() primitives

For the series:

Quoted 12 lines
 arch/s390/include/asm/kfence.h     |  3 ++-
 arch/s390/include/asm/set_memory.h |  4 ++--
 arch/s390/kernel/kprobes.c         |  5 ++++-
 arch/s390/kernel/machine_kexec.c   |  7 +++++--
 arch/s390/kernel/module.c          |  7 ++++++-
 arch/s390/mm/init.c                | 10 ++++++++--
 arch/s390/mm/vmem.c                | 14 +++++++++-----
 7 files changed, 36 insertions(+), 14 deletions(-)

base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4

Signature · 1 line
2.56.1
Signature · 2 lines
Sincerely yours,
Mike.