public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Jianhua Liu <jianhua.ljh@gmail.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	linux-kernel@vger.kernel.org, Anju T <anju@linux.vnet.ibm.com>
Subject: [PATCH 2/3] powerpc/kprobes: Fix alloc_optinsn_page() to use all area of optinsn_slot
Date: Thu, 13 Jan 2022 23:55:45 +0900	[thread overview]
Message-ID: <164208574549.1590449.15254840776784986500.stgit@devnote2> (raw)
In-Reply-To: <164208572899.1590449.14007562142219520042.stgit@devnote2>

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@gmail.com>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Anju T <anju@linux.vnet.ibm.com>
---
 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:


  parent reply	other threads:[~2022-01-13 14:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-13 14:55 [PATCH 0/3] kprobes: Misc bugfixes Masami Hiramatsu
2022-01-13 14:55 ` [PATCH 1/3] ARM: kprobes: Count MAX_OPTINSN_SIZE in kprobe_opcode_t Masami Hiramatsu
2022-01-13 14:55 ` Masami Hiramatsu [this message]
2022-01-13 14:55 ` [PATCH 3/3] ia64: kprobes: Cleanup unused 'template' local variable Masami Hiramatsu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=164208574549.1590449.15254840776784986500.stgit@devnote2 \
    --to=mhiramat@kernel.org \
    --cc=anju@linux.vnet.ibm.com \
    --cc=jianhua.ljh@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox