linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Naveen N Rao <naveen@kernel.org>
To: <linuxppc-dev@lists.ozlabs.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Nicholas Piggin <npiggin@gmail.com>
Subject: [RFC PATCH 07/15] powerpc/ftrace: Refactor ftrace_modify_code()
Date: Wed,  7 Jun 2023 22:25:22 +0530	[thread overview]
Message-ID: <f3268298e2ad7998e4a5dfda5484b14a469b5275.1686151854.git.naveen@kernel.org> (raw)
In-Reply-To: <cover.1686151854.git.naveen@kernel.org>

Split up ftrace_modify_code() into a few helpers for future use. Also
update error messages accordingly.

Signed-off-by: Naveen N Rao <naveen@kernel.org>
---
 arch/powerpc/kernel/trace/ftrace.c | 51 +++++++++++++++++-------------
 1 file changed, 29 insertions(+), 22 deletions(-)

diff --git a/arch/powerpc/kernel/trace/ftrace.c b/arch/powerpc/kernel/trace/ftrace.c
index 913c7aa63d3fa3..ef4e49c2c37781 100644
--- a/arch/powerpc/kernel/trace/ftrace.c
+++ b/arch/powerpc/kernel/trace/ftrace.c
@@ -50,32 +50,39 @@ ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
 	return op;
 }
 
-static inline int
-ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
+static inline int ftrace_read_inst(unsigned long ip, ppc_inst_t *op)
 {
-	ppc_inst_t replaced;
-
-	/*
-	 * Note:
-	 * We are paranoid about modifying text, as if a bug was to happen, it
-	 * could cause us to read or write to someplace that could cause harm.
-	 * Carefully read and modify the code with probe_kernel_*(), and make
-	 * sure what we read is what we expected it to be before modifying it.
-	 */
-
-	/* read the text we want to modify */
-	if (copy_inst_from_kernel_nofault(&replaced, (void *)ip))
+	if (copy_inst_from_kernel_nofault(op, (void *)ip)) {
+		pr_err("0x%lx: fetching instruction failed\n", ip);
 		return -EFAULT;
-
-	/* Make sure it is what we expect it to be */
-	if (!ppc_inst_equal(replaced, old)) {
-		pr_err("%p: replaced (%08lx) != old (%08lx)", (void *)ip,
-		       ppc_inst_as_ulong(replaced), ppc_inst_as_ulong(old));
-		return -EINVAL;
 	}
 
-	/* replace the text with the new text */
-	return patch_instruction((u32 *)ip, new);
+	return 0;
+}
+
+static inline int ftrace_validate_inst(unsigned long ip, ppc_inst_t inst)
+{
+	ppc_inst_t op;
+	int ret;
+
+	ret = ftrace_read_inst(ip, &op);
+	if (!ret && !ppc_inst_equal(op, inst)) {
+		pr_err("0x%lx: expected (%08lx) != found (%08lx)\n",
+		       ip, ppc_inst_as_ulong(inst), ppc_inst_as_ulong(op));
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static inline int ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
+{
+	int ret = ftrace_validate_inst(ip, old);
+
+	if (!ret)
+		ret = patch_instruction((u32 *)ip, new);
+
+	return ret;
 }
 
 /*
-- 
2.40.1


  parent reply	other threads:[~2023-06-07 17:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-07 16:55 [RFC PATCH 00/15] powerpc/ftrace: refactoring and support for -fpatchable-function-entry Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 01/15] powerpc/module: Remove unused .ftrace.tramp section Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 02/15] powerpc64/ftrace: Move ELFv1 and -pg support code into a separate file Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 03/15] powerpc/ftrace: Simplify function_graph support in ftrace.c Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 04/15] powerpc/ftrace: Use FTRACE_REGS_ADDR to identify the correct ftrace trampoline Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 05/15] powerpc/ftrace: Extend ftrace support for large kernels to ppc32 Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 06/15] powerpc/ftrace: Consolidate ftrace support into fewer files Naveen N Rao
2023-06-07 16:55 ` Naveen N Rao [this message]
2023-06-07 16:55 ` [RFC PATCH 08/15] powerpc/ftrace: Stop re-purposing linker generated long branches for ftrace Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 09/15] powerpc/ftrace: Add separate ftrace_init_nop() with additional validation Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 10/15] powerpc/ftrace: Simplify ftrace_make_nop() Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 11/15] powerpc/ftrace: Simplify ftrace_make_call() Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 12/15] powerpc/ftrace: Simplify ftrace_modify_call() Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 13/15] powerpc/ftrace: Replace use of ftrace_call_replace() with ftrace_create_branch_inst() Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 14/15] powerpc/ftrace: Implement ftrace_replace_code() Naveen N Rao
2023-06-07 16:55 ` [RFC PATCH 15/15] powerpc/ftrace: Add support for -fpatchable-function-entry Naveen N Rao

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=f3268298e2ad7998e4a5dfda5484b14a469b5275.1686151854.git.naveen@kernel.org \
    --to=naveen@kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=npiggin@gmail.com \
    --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;
as well as URLs for NNTP newsgroup(s).