Skip to contentSkip to conversation

arm64: kprobes: call set_memory_rox() for kprobe page

PATCH In review Yang Shi 4 participants 6 messages Sep 18, 2025 – Sep 23, 2025

Subsystems (2)
lore
Share & tools
lore

Yang Shi

author
#0 lore raw

PATCH v2 1/1 arm64: kprobes: call set_memory_rox() for kprobe page

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
v2: Separated the patch from BBML2 series since it is an orthogonal bug
fix per Ryan.
Fixed the variable name nit per Catalin.
Collected R-bs from Catalin.

 arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +12 -0
index 0c5d408afd95..8ab6104a4883 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
 #include <linux/extable.h>
 #include <linux/kasan.h>
 #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 static void __kprobes
 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
+void *alloc_insn_page(void)
+{
+	void *addr;
+
+	addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!addr)
+		return NULL;
+	set_memory_rox((unsigned long)addr, 1);
+	return addr;
+}
+
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
 	kprobe_opcode_t *addr = p->ainsn.xol_insn;
Signature · 1 line
2.47.0
maintainer
#1 lore raw
Quoted 33 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
v2: Separated the patch from BBML2 series since it is an orthogonal bug
fix per Ryan.
Fixed the variable name nit per Catalin.
Collected R-bs from Catalin.

 arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +1 -0
index 0c5d408afd95..8ab6104a4883 100644
@@ -10,6 +10,7 @@
 
 #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
 #include <linux/extable.h>
 #include <linux/kasan.h>
 #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 static void __kprobes
 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 54

Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +10 -0
+void *alloc_insn_page(void)
+{
+	void *addr;
+
+	addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!addr)
+		return NULL;
+	set_memory_rox((unsigned long)addr, 1);
+	return addr;
+}

Why isn't execmem taking care of this? It looks to me like the execmem_cache_alloc() path calls set_memory_rox() but the execmem_vmalloc() path doesn't?

It feels a bit bizarre to me that we have to provide our own wrapper (which is identical to what s390 does). Also, how does alloc_insn_page() handle the direct map alias on x86?

Will

author
#2 lore raw
Quoted 34 lines · Will Deacon
Quoted 33 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
v2: Separated the patch from BBML2 series since it is an orthogonal bug
fix per Ryan.
Fixed the variable name nit per Catalin.
Collected R-bs from Catalin.

  arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +1 -0
index 0c5d408afd95..8ab6104a4883 100644
@@ -10,6 +10,7 @@
 
  #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
  #include <linux/extable.h>
  #include <linux/kasan.h>
  #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  static void __kprobes
  post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
Quoted 13 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 54

Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +10 -0
+void *alloc_insn_page(void)
+{
+	void *addr;
+
+	addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!addr)
+		return NULL;
+	set_memory_rox((unsigned long)addr, 1);
+	return addr;
+}

Why isn't execmem taking care of this? It looks to me like the execmem_cache_alloc() path calls set_memory_rox() but the execmem_vmalloc() path doesn't?

execmem_cache_alloc() is just called if execmem ROX cache is enabled, but it currently just supported by x86. Included Mike to this thread who is the author of execmem ROX cache.

Quoted 4 lines

It feels a bit bizarre to me that we have to provide our own wrapper (which is identical to what s390 does). Also, how does alloc_insn_page() handle the direct map alias on x86?

x86 handles it via execmem ROX cache.

Thanks,
Yang

Quoted 2 lines

Will

#3 lore raw
Quoted 39 lines · Yang Shi
Quoted 36 lines · Will Deacon
Quoted 35 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
v2: Separated the patch from BBML2 series since it is an orthogonal bug
fix per Ryan.
Fixed the variable name nit per Catalin.
Collected R-bs from Catalin.

  arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +6 -0
index 0c5d408afd95..8ab6104a4883 100644
@@ -10,6 +10,7 @@
  #define pr_fmt(fmt) "kprobes: " fmt
+#include <linux/execmem.h>
  #include <linux/extable.h>
  #include <linux/kasan.h>
  #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  static void __kprobes
  post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
+void *alloc_insn_page(void)
+{
+	void *addr;
+
+	addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
Quoted 8 lines
Quoted 8 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 54

Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +5 -0
+	if (!addr)
+		return NULL;
+	set_memory_rox((unsigned long)addr, 1);
+	return addr;
+}

Why isn't execmem taking care of this? It looks to me like the execmem_cache_alloc() path calls set_memory_rox() but the execmem_vmalloc() path doesn't?

execmem_alloc() -> execmem_vmalloc() consolidated __vmalloc_node_range() for executable allocations. Those also didn't update the linear map alias.

It could be added to execmem_vmalloc(), but as of now we don't have a way for generic code to tell which set_memory method to call based on pgprot, so making execmem_vmalloc() to deal with direct map alias is quite involved.

It would be easier to just remove the direct map alias. It works on x86 so I don't see what can possibly go wrong :)

Quoted 8 lines

execmem_cache_alloc() is just called if execmem ROX cache is enabled, but it currently just supported by x86. Included Mike to this thread who is the author of execmem ROX cache.

Quoted 4 lines

It feels a bit bizarre to me that we have to provide our own wrapper (which is identical to what s390 does). Also, how does alloc_insn_page() handle the direct map alias on x86?

s390 had its version of alloc_insn_page() long before execmem so there I just replaced module_alloc() with exemem_alloc().

arm64 version of alloc_insn_page() didn't update the direct map before execmem, so I overlooked this issue when I was converting arm64 to execmem.

Quoted 8 lines

x86 handles it via execmem ROX cache.

Thanks,
Yang

Quoted 2 lines

Will

Signature · 2 lines
Sincerely yours,
Mike.
author
#4 lore raw
Quoted 62 lines · Mike Rapoport
Quoted 38 lines · Yang Shi
Quoted 36 lines · Will Deacon
Quoted 35 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
v2: Separated the patch from BBML2 series since it is an orthogonal bug
fix per Ryan.
Fixed the variable name nit per Catalin.
Collected R-bs from Catalin.

   arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
   1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +6 -0
index 0c5d408afd95..8ab6104a4883 100644
@@ -10,6 +10,7 @@
   #define pr_fmt(fmt) "kprobes: " fmt
+#include <linux/execmem.h>
   #include <linux/extable.h>
   #include <linux/kasan.h>
   #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
   static void __kprobes
   post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
+void *alloc_insn_page(void)
+{
+	void *addr;
+
+	addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
Quoted 8 lines
Quoted 8 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 54

Quoted 5 lines
arch/arm64/kernel/probes/kprobes.c +5 -0
+	if (!addr)
+		return NULL;
+	set_memory_rox((unsigned long)addr, 1);
+	return addr;
+}

Why isn't execmem taking care of this? It looks to me like the execmem_cache_alloc() path calls set_memory_rox() but the execmem_vmalloc() path doesn't?

execmem_alloc() -> execmem_vmalloc() consolidated __vmalloc_node_range() for executable allocations. Those also didn't update the linear map alias.

It could be added to execmem_vmalloc(), but as of now we don't have a way for generic code to tell which set_memory method to call based on pgprot, so making execmem_vmalloc() to deal with direct map alias is quite involved.

It would be easier to just remove the direct map alias. It works on x86 so I don't see what can possibly go wrong :)

Quoted 4 lines

execmem_cache_alloc() is just called if execmem ROX cache is enabled, but it currently just supported by x86. Included Mike to this thread who is the author of execmem ROX cache.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

It feels a bit bizarre to me that we have to provide our own wrapper (which is identical to what s390 does). Also, how does alloc_insn_page() handle the direct map alias on x86?

s390 had its version of alloc_insn_page() long before execmem so there I just replaced module_alloc() with exemem_alloc().

arm64 version of alloc_insn_page() didn't update the direct map before execmem, so I overlooked this issue when I was converting arm64 to execmem.

Thanks, Mike. It is not your fault. The set_memory_rox() was called in arm64's alloc_insn_page() before, but it was mistakenly removed by commit 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page") before execmem.

Yang

Quoted 7 lines
Quoted 7 lines

x86 handles it via execmem ROX cache.

Thanks,
Yang

Quoted 1 line

Will

#5 lore raw
In reply to Will Deacon
Quoted 34 lines · Will Deacon
Quoted 33 lines · Yang Shi

The kprobe page is allocated by execmem allocator with ROX permission. It needs to call set_memory_rox() to set proper permission for the direct map too. It was missed.

Fixes 10d5e97c1bf8 ("arm64: use PAGE_KERNEL_ROX directly in alloc_insn_page")

---
v2: Separated the patch from BBML2 series since it is an orthogonal bug
fix per Ryan.
Fixed the variable name nit per Catalin.
Collected R-bs from Catalin.

  arch/arm64/kernel/probes/kprobes.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
arch/arm64/kernel/probes/kprobes.c +1 -0
index 0c5d408afd95..8ab6104a4883 100644
@@ -10,6 +10,7 @@
 
  #define pr_fmt(fmt) "kprobes: " fmt
 
+#include <linux/execmem.h>
  #include <linux/extable.h>
  #include <linux/kasan.h>
  #include <linux/kernel.h>
@@ -41,6 +42,17 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
  static void __kprobes
  post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 
Quoted 13 lines

arch/arm64/kernel/probes/kprobes.c DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); · line 54

Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +10 -0
+void *alloc_insn_page(void)
+{
+	void *addr;
+
+	addr = execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE);
+	if (!addr)
+		return NULL;
+	set_memory_rox((unsigned long)addr, 1);
+	return addr;
+}

Why isn't execmem taking care of this? It looks to me like the execmem_cache_alloc() path calls set_memory_rox() but the execmem_vmalloc() path doesn't?

Ryan has raised this issue here -
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

It feels a bit bizarre to me that we have to provide our own wrapper (which is identical to what s390 does). Also, how does alloc_insn_page() handle the direct map alias on x86?

Will