2022-11-06 10:05:29

by Xim

[permalink] [raw]
Subject: [PATCH v4 2/8] riscv/kprobe: Allocate detour buffer from module area

From: Liao Chang <[email protected]>

From: Liao Chang <[email protected]>

To address the limitation of PC-relative branch instruction on riscv
architecture, detour buffer slot used for optprobes is allocated from
the region, the distance of which from kernel should be less than 4GB.

For the time being, Modules region always live before the kernel.
But Vmalloc region reside far from kernel, the distance is half of the
kernel address space (See Documentation/riscv/vm-layout.rst), hence it
needs to override the alloc_optinsn_page() to make sure allocate detour
buffer from jump-safe region.

Signed-off-by: Liao Chang <[email protected]>
Co-developed-by: Chen Guokai <[email protected]>
Signed-off-by: Chen Guokai <[email protected]>
---
arch/riscv/kernel/probes/kprobes.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index e6e950b7cf32..034eb7b13b3c 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -12,6 +12,7 @@
#include <asm/cacheflush.h>
#include <asm/bug.h>
#include <asm/patch.h>
+#include <asm/set_memory.h>

#include "decode-insn.h"

@@ -84,6 +85,30 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
}

#ifdef CONFIG_MMU
+#if defined(CONFIG_OPTPROBES) && defined(CONFIG_64BIT)
+void *alloc_optinsn_page(void)
+{
+ void *page;
+
+ page = __vmalloc_node_range(PAGE_SIZE, 1, MODULES_VADDR,
+ MODULES_END, GFP_KERNEL,
+ PAGE_KERNEL, 0, NUMA_NO_NODE,
+ __builtin_return_address(0));
+ if (!page)
+ return NULL;
+
+ set_vm_flush_reset_perms(page);
+ /*
+ * First make the page read-only, and only then make it executable to
+ * prevent it from being W+X in between.
+ */
+ set_memory_ro((unsigned long)page, 1);
+ set_memory_x((unsigned long)page, 1);
+
+ return page;
+}
+#endif
+
void *alloc_insn_page(void)
{
return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
--
2.25.1



2022-11-17 01:41:13

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH v4 2/8] riscv/kprobe: Allocate detour buffer from module area

On Sun, 6 Nov 2022 18:03:10 +0800
Chen Guokai <[email protected]> wrote:

> @@ -84,6 +85,30 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
> }
>
> #ifdef CONFIG_MMU
> +#if defined(CONFIG_OPTPROBES) && defined(CONFIG_64BIT)
> +void *alloc_optinsn_page(void)
> +{
> + void *page;
> +
> + page = __vmalloc_node_range(PAGE_SIZE, 1, MODULES_VADDR,
> + MODULES_END, GFP_KERNEL,
> + PAGE_KERNEL, 0, NUMA_NO_NODE,
> + __builtin_return_address(0));
> + if (!page)
> + return NULL;
> +
> + set_vm_flush_reset_perms(page);
> + /*
> + * First make the page read-only, and only then make it executable to
> + * prevent it from being W+X in between.
> + */
> + set_memory_ro((unsigned long)page, 1);
> + set_memory_x((unsigned long)page, 1);

FYI, the above combination is going to be going away:

https://lore.kernel.org/all/[email protected]/

-- Steve


> +
> + return page;
> +}
> +#endif
> +

2022-11-18 01:57:47

by Liao, Chang

[permalink] [raw]
Subject: Re: [PATCH v4 2/8] riscv/kprobe: Allocate detour buffer from module area



在 2022/11/17 9:25, Steven Rostedt 写道:
> On Sun, 6 Nov 2022 18:03:10 +0800
> Chen Guokai <[email protected]> wrote:
>
>> @@ -84,6 +85,30 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
>> }
>>
>> #ifdef CONFIG_MMU
>> +#if defined(CONFIG_OPTPROBES) && defined(CONFIG_64BIT)
>> +void *alloc_optinsn_page(void)
>> +{
>> + void *page;
>> +
>> + page = __vmalloc_node_range(PAGE_SIZE, 1, MODULES_VADDR,
>> + MODULES_END, GFP_KERNEL,
>> + PAGE_KERNEL, 0, NUMA_NO_NODE,
>> + __builtin_return_address(0));
>> + if (!page)
>> + return NULL;
>> +
>> + set_vm_flush_reset_perms(page);
>> + /*
>> + * First make the page read-only, and only then make it executable to
>> + * prevent it from being W+X in between.
>> + */
>> + set_memory_ro((unsigned long)page, 1);
>> + set_memory_x((unsigned long)page, 1);
>
> FYI, the above combination is going to be going away:
>
> https://lore.kernel.org/all/[email protected]/

Thanks for reminding, i will use this API in next revision.
> -- Steve
>
>
>> +
>> + return page;
>> +}
>> +#endif
>> +
>

--
BR,
Liao, Chang