[PATCH] powerpc/kprobes: Define arch specific optinsn memory allocation

From: Masami Hiramatsu
Date: Wed May 12 2021 - 10:33:10 EST


Define arch specific optinsn memory allocate functions
instead of using arch specific ppc_optinsn_slot.

This fixes the unused function warning on is_kprobe_ppc_optinsn_slot()
when building with W=1 and also this makes kernel_text_address()
works correctly on optinsn page.

Signed-off-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
---
arch/powerpc/kernel/optprobes.c | 24 +++++++-----------------
kernel/kprobes.c | 15 +++++++++++++--
2 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobes.c
index cdf87086fa33..aa017ccbd66e 100644
--- a/arch/powerpc/kernel/optprobes.c
+++ b/arch/powerpc/kernel/optprobes.c
@@ -31,11 +31,10 @@
#define TMPL_END_IDX \
(optprobe_template_end - optprobe_template_entry)

-DEFINE_INSN_CACHE_OPS(ppc_optinsn);
-
static bool insn_page_in_use;

-static void *__ppc_alloc_insn_page(void)
+/* Override optinsn page allocation */
+void *alloc_optinsn_page(void)
{
if (insn_page_in_use)
return NULL;
@@ -43,20 +42,11 @@ static void *__ppc_alloc_insn_page(void)
return &optinsn_slot;
}

-static void __ppc_free_insn_page(void *page __maybe_unused)
+void free_optinsn_page(void *page __maybe_unused)
{
insn_page_in_use = false;
}

-struct kprobe_insn_cache kprobe_ppc_optinsn_slots = {
- .mutex = __MUTEX_INITIALIZER(kprobe_ppc_optinsn_slots.mutex),
- .pages = LIST_HEAD_INIT(kprobe_ppc_optinsn_slots.pages),
- /* insn_size initialized later */
- .alloc = __ppc_alloc_insn_page,
- .free = __ppc_free_insn_page,
- .nr_garbage = 0,
-};
-
/*
* Check if we can optimize this probe. Returns NIP post-emulation if this can
* be optimized and 0 otherwise.
@@ -136,7 +126,7 @@ NOKPROBE_SYMBOL(optimized_callback);
void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
{
if (op->optinsn.insn) {
- free_ppc_optinsn_slot(op->optinsn.insn, 1);
+ free_optinsn_slot(op->optinsn.insn, 1);
op->optinsn.insn = NULL;
}
}
@@ -203,14 +193,14 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
unsigned long nip, size;
int rc, i;

- kprobe_ppc_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
+ kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;

nip = can_optimize(p);
if (!nip)
return -EILSEQ;

/* Allocate instruction slot for detour buffer */
- buff = get_ppc_optinsn_slot();
+ buff = get_optinsn_slot();
if (!buff)
return -ENOMEM;

@@ -297,7 +287,7 @@ int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
return 0;

error:
- free_ppc_optinsn_slot(buff, 0);
+ free_optinsn_slot(buff, 0);
return -ERANGE;

}
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 745f08fdd7a6..251fcb9d6c4c 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -321,11 +321,22 @@ int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
}

#ifdef CONFIG_OPTPROBES
+
+void * __weak alloc_optinsn_page(void)
+{
+ return alloc_insn_page();
+}
+
+void __weak free_optinsn_page(void *page)
+{
+ free_insn_page(page);
+}
+
/* For optimized_kprobe buffer */
struct kprobe_insn_cache kprobe_optinsn_slots = {
.mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
- .alloc = alloc_insn_page,
- .free = free_insn_page,
+ .alloc = alloc_optinsn_page,
+ .free = free_optinsn_page,
.sym = KPROBE_OPTINSN_PAGE_SYM,
.pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
/* .insn_size is initialized later */
--
2.25.1