[PATCH 2/3] powerpc/kprobes: Fix alloc_optinsn_page() to use all area of optinsn_slot

From: Masami Hiramatsu
Date: Thu Jan 13 2022 - 09:55:53 EST


When the ppc64 uses 4K page size, most part of the optinsn_slot
is not used because alloc_optinsn_page() is expected to return
only one page-size memory.
To use the remaining memories, make insn_page_in_use as array
to manage page-sized slots and return corresponding memory
address in the optinsn_slot.

Fixes: 51c9c0843993 ("powerpc/kprobes: Implement Optprobes")
Reported-by: Jianhua Liu <jianhua.ljh@xxxxxxxxx>
Signed-off-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
Cc: Anju T <anju@xxxxxxxxxxxxxxxxxx>
---
arch/powerpc/include/asm/kprobes.h | 6 ++++++
arch/powerpc/kernel/optprobes.c | 25 +++++++++++++++++++------
arch/powerpc/kernel/optprobes_head.S | 5 ++---
3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/include/asm/kprobes.h b/arch/powerpc/include/asm/kprobes.h
index bab364152b29..e7a5390effa8 100644
--- a/arch/powerpc/include/asm/kprobes.h
+++ b/arch/powerpc/include/asm/kprobes.h
@@ -4,6 +4,8 @@

#include <asm-generic/kprobes.h>

+#ifndef __ASSEMBLY__
+
#ifdef __KERNEL__
/*
* Kernel Probes (KProbes)
@@ -94,4 +96,8 @@ static inline int kprobe_handler(struct pt_regs *regs) { return 0; }
static inline int kprobe_post_handler(struct pt_regs *regs) { return 0; }
#endif /* CONFIG_KPROBES */
#endif /* __KERNEL__ */
+#endif /* __ASSEMBLY__ */
+
+#define KPROBE_OPTINSN_SLOT_SIZE 65536
+
#endif /* _ASM_POWERPC_KPROBES_H */
diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
index ce1903064031..eec2776ad2fd 100644
--- a/arch/powerpc/kernel/optprobes.c
+++ b/arch/powerpc/kernel/optprobes.c
@@ -25,19 +25,32 @@
#define TMPL_INSN_IDX (optprobe_template_insn - optprobe_template_entry)
#define TMPL_END_IDX (optprobe_template_end - optprobe_template_entry)

-static bool insn_page_in_use;
+#define OPTINSN_SLOT_PAGES (KPROBE_OPTINSN_SLOT_SIZE / PAGE_SIZE)
+
+static bool insn_page_in_use[OPTINSN_SLOT_PAGES];

void *alloc_optinsn_page(void)
{
- if (insn_page_in_use)
- return NULL;
- insn_page_in_use = true;
- return &optinsn_slot;
+ int i;
+
+ for (i = 0; i < OPTINSN_SLOT_PAGES; i++) {
+ if (!insn_page_in_use[i]) {
+ insn_page_in_use[i] = true;
+ return (void *)((unsigned long)&optinsn_slot + PAGE_SIZE * i);
+ }
+ }
+ return NULL;
}

void free_optinsn_page(void *page)
{
- insn_page_in_use = false;
+ unsigned long idx = (unsigned long)page - (unsigned long)&optinsn_slot;
+
+ WARN_ON_ONCE(idx & (PAGE_SIZE - 1));
+ idx >>= PAGE_SHIFT;
+ if (WARN_ON_ONCE(idx >= OPTINSN_SLOT_PAGES))
+ return;
+ insn_page_in_use[idx] = false;
}

/*
diff --git a/arch/powerpc/kernel/optprobes_head.S b/arch/powerpc/kernel/optprobes_head.S
index 19ea3312403c..bf2106836cc6 100644
--- a/arch/powerpc/kernel/optprobes_head.S
+++ b/arch/powerpc/kernel/optprobes_head.S
@@ -8,6 +8,7 @@
#include <asm/ppc_asm.h>
#include <asm/ptrace.h>
#include <asm/asm-offsets.h>
+#include <asm/kprobes.h>

#ifdef CONFIG_PPC64
#define SAVE_30GPRS(base) SAVE_10GPRS(2,base); SAVE_10GPRS(12,base); SAVE_10GPRS(22,base)
@@ -19,8 +20,6 @@
#define TEMPLATE_FOR_IMM_LOAD_INSNS nop; nop; nop
#endif

-#define OPT_SLOT_SIZE 65536
-
.balign 4

/*
@@ -30,7 +29,7 @@
*/
.global optinsn_slot
optinsn_slot:
- .space OPT_SLOT_SIZE
+ .space KPROBE_OPTINSN_SLOT_SIZE

/*
* Optprobe template: