Skip to contentSkip to conversation

powerpc: ftrace optimisation and cleanup and more [v1]

PATCH Superseded Christophe Leroy 4 participants 39 messages Mar 24, 2022 – May 6, 2022

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover powerpc: ftrace optimisation and cleanup and more [v1]

This series provides optimisation and cleanup of ftrace on powerpc.

With this series ftrace activation is about 20% faster on an 8xx.

At the end of the series come additional cleanups around ppc-opcode, that would likely conflict with this series if posted separately.

Christophe Leroy (22):
powerpc/ftrace: Refactor prepare_ftrace_return() powerpc/ftrace: Remove redundant create_branch() calls powerpc/code-patching: Inline is_offset_in_{cond}_branch_range() powerpc/ftrace: Use is_offset_in_branch_range() powerpc/code-patching: Inline create_branch() powerpc/ftrace: Inline ftrace_modify_code() powerpc/ftrace: Use patch_instruction() return directly powerpc/ftrace: Make __ftrace_make_{nop/call}() common to PPC32 and

    PPC64

powerpc/ftrace: Don't include ftrace.o for CONFIG_FTRACE_SYSCALLS powerpc/ftrace: Use CONFIG_FUNCTION_TRACER instead of
CONFIG_DYNAMIC_FTRACE
powerpc/ftrace: Remove ftrace_plt_tramps[] powerpc/ftrace: Use BRANCH_SET_LINK instead of value 1 powerpc/ftrace: Use PPC_RAW_xxx() macros instead of opencoding. powerpc/ftrace: Use size macro instead of opencoding powerpc/ftrace: Simplify expected_nop_sequence() powerpc/ftrace: Minimise number of #ifdefs powerpc/inst: Add __copy_inst_from_kernel_nofault() powerpc/ftrace: Don't use copy_from_kernel_nofault() in
module_trampoline_target()
powerpc/inst: Remove PPC_INST_BRANCH
powerpc/modules: Use PPC_INST_BRANCH_MASK instead of opencoding powerpc/inst: Remove PPC_INST_BL
powerpc/opcodes: Remove unused PPC_INST_XXX macros

 arch/powerpc/include/asm/book3s/32/pgtable.h |   2 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h |   2 +-
 arch/powerpc/include/asm/code-patching.h     |  53 ++-
 arch/powerpc/include/asm/inst.h              |  13 +-
 arch/powerpc/include/asm/module.h            |   6 +-
 arch/powerpc/include/asm/nohash/pgtable.h    |   2 +-
 arch/powerpc/include/asm/ppc-opcode.h        |  20 +-
 arch/powerpc/include/asm/sections.h          |  24 +-
 arch/powerpc/kernel/module_32.c              |  29 +-
 arch/powerpc/kernel/module_64.c              |  10 +-
 arch/powerpc/kernel/trace/Makefile           |   5 +-
 arch/powerpc/kernel/trace/ftrace.c           | 390 ++++++-------------
 arch/powerpc/lib/code-patching.c             |  47 ---
 arch/powerpc/lib/feature-fixups.c            |   2 +-
 arch/powerpc/net/bpf_jit.h                   |   2 +-
 15 files changed, 229 insertions(+), 378 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/22 powerpc/ftrace: Refactor prepare_ftrace_return()

When we have CONFIG_DYNAMIC_FTRACE_WITH_ARGS, prepare_ftrace_return() is called by ftrace_graph_func() otherwise prepare_ftrace_return() is called from assembly.

Refactor prepare_ftrace_return() into a static __prepare_ftrace_return() that will be called by both prepare_ftrace_return() and ftrace_graph_func().

It will allow GCC to fold __prepare_ftrace_return() inside ftrace_graph_func().

---
 arch/powerpc/kernel/trace/ftrace.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +9 -3
index 4ee04aacf9f1..7a266fd469b7 100644
@@ -939,8 +939,8 @@ int ftrace_disable_ftrace_graph_caller(void)
  * Hook the return address and push it in the stack of return addrs
  * in current thread info. Return the address we want to divert to.
  */
-unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
-						unsigned long sp)
+static unsigned long
+__prepare_ftrace_return(unsigned long parent, unsigned long ip, unsigned long sp)
 {
 	unsigned long return_hooker;
 	int bit;
@@ -969,7 +969,13 @@ unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
 		       struct ftrace_ops *op, struct ftrace_regs *fregs)
 {
-	fregs->regs.link = prepare_ftrace_return(parent_ip, ip, fregs->regs.gpr[1]);
+	fregs->regs.link = __prepare_ftrace_return(parent_ip, ip, fregs->regs.gpr[1]);
+}
+#else
+unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
+				    unsigned long sp)
+{
+	return __prepare_ftrace_return(parent, ip, sp);
 }
 #endif
 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/22 powerpc/ftrace: Remove redundant create_branch() calls

In reply to Christophe Leroy

Since commit d5937db114e4 ("powerpc/code-patching: Fix patch_branch() return on out-of-range failure") patch_branch() fails with -ERANGE when trying to branch out of range.

No need to perform the test twice. Remove redundant create_branch() calls.

---
 arch/powerpc/kernel/trace/ftrace.c | 20 --------------------
 1 file changed, 20 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +0 -20
index 7a266fd469b7..3ce3697e8a7c 100644
@@ -301,7 +301,6 @@ static int setup_mcount_compiler_tramp(unsigned long tramp)
 	int i;
 	ppc_inst_t op;
 	unsigned long ptr;
-	ppc_inst_t instr;
 	static unsigned long ftrace_plt_tramps[NUM_FTRACE_TRAMPS];
 
 	/* Is this a known long jump tramp? */
@@ -344,12 +343,6 @@ static int setup_mcount_compiler_tramp(unsigned long tramp)
 #else
 	ptr = ppc_global_function_entry((void *)ftrace_caller);
 #endif
-	if (create_branch(&instr, (void *)tramp, ptr, 0)) {
-		pr_debug("%ps is not reachable from existing mcount tramp\n",
-				(void *)ptr);
-		return -1;
-	}
-
 	if (patch_branch((u32 *)tramp, ptr, 0)) {
 		pr_debug("REL24 out of range!\n");
 		return -1;
@@ -490,7 +483,6 @@ static int
 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 {
 	ppc_inst_t op[2];
-	ppc_inst_t instr;
 	void *ip = (void *)rec->ip;
 	unsigned long entry, ptr, tramp;
 	struct module *mod = rec->arch.mod;
@@ -539,12 +531,6 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 		return -EINVAL;
 	}
 
-	/* Ensure branch is within 24 bits */
-	if (create_branch(&instr, ip, tramp, BRANCH_SET_LINK)) {
-		pr_err("Branch out of range\n");
-		return -EINVAL;
-	}
-
 	if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
 		pr_err("REL24 out of range!\n");
 		return -EINVAL;
@@ -770,12 +756,6 @@ __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
 		return -EINVAL;
 	}
 
-	/* Ensure branch is within 24 bits */
-	if (create_branch(&op, (u32 *)ip, tramp, BRANCH_SET_LINK)) {
-		pr_err("Branch out of range\n");
-		return -EINVAL;
-	}
-
 	if (patch_branch((u32 *)ip, tramp, BRANCH_SET_LINK)) {
 		pr_err("REL24 out of range!\n");
 		return -EINVAL;
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/22 powerpc/code-patching: Inline is_offset_in_{cond}_branch_range()

In reply to Christophe Leroy

Test in is_offset_in_branch_range() and is_offset_in_cond_branch_range() are simple tests that are worth inlining.

---
 arch/powerpc/include/asm/code-patching.h | 29 ++++++++++++++++++++++--
 arch/powerpc/lib/code-patching.c         | 27 ----------------------
 2 files changed, 27 insertions(+), 29 deletions(-)
arch/powerpc/include/asm/code-patching.h +27 -2
index 409483b2d0ce..e7c5df50cb4e 100644
@@ -22,8 +22,33 @@
 #define BRANCH_SET_LINK	0x1
 #define BRANCH_ABSOLUTE	0x2
 
-bool is_offset_in_branch_range(long offset);
-bool is_offset_in_cond_branch_range(long offset);
+/*
+ * Powerpc branch instruction is :
+ *
+ *  0         6                 30   31
+ *  +---------+----------------+---+---+
+ *  | opcode  |     LI         |AA |LK |
+ *  +---------+----------------+---+---+
+ *  Where AA = 0 and LK = 0
+ *
+ * LI is a signed 24 bits integer. The real branch offset is computed
+ * by: imm32 = SignExtend(LI:'0b00', 32);
+ *
+ * So the maximum forward branch should be:
+ *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
+ * The maximum backward branch should be:
+ *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
+ */
+static inline bool is_offset_in_branch_range(long offset)
+{
+	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
+}
+
+static inline bool is_offset_in_cond_branch_range(long offset)
+{
+	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
+}
+
 int create_branch(ppc_inst_t *instr, const u32 *addr,
 		  unsigned long target, int flags);
 int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
arch/powerpc/lib/code-patching.c +0 -27
index 00c68e7fb11e..58262c7e447c 100644
@@ -208,33 +208,6 @@ int patch_branch(u32 *addr, unsigned long target, int flags)
 	return patch_instruction(addr, instr);
 }
 
-bool is_offset_in_branch_range(long offset)
-{
-	/*
-	 * Powerpc branch instruction is :
-	 *
-	 *  0         6                 30   31
-	 *  +---------+----------------+---+---+
-	 *  | opcode  |     LI         |AA |LK |
-	 *  +---------+----------------+---+---+
-	 *  Where AA = 0 and LK = 0
-	 *
-	 * LI is a signed 24 bits integer. The real branch offset is computed
-	 * by: imm32 = SignExtend(LI:'0b00', 32);
-	 *
-	 * So the maximum forward branch should be:
-	 *   (0x007fffff << 2) = 0x01fffffc =  0x1fffffc
-	 * The maximum backward branch should be:
-	 *   (0xff800000 << 2) = 0xfe000000 = -0x2000000
-	 */
-	return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
-}
-
-bool is_offset_in_cond_branch_range(long offset)
-{
-	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
-}
-
 /*
  * Helper to check if a given instruction is a conditional branch
  * Derived from the conditional checks in analyse_instr()
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/22 powerpc/ftrace: Use is_offset_in_branch_range()

In reply to Christophe Leroy

Use is_offset_in_branch_range() instead of create_branch() to check if a target is within branch range.

This patch together with the previous one improves ftrace activation time by 7%

---
 arch/powerpc/kernel/trace/ftrace.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +2 -6
index 3ce3697e8a7c..41c45b9c7f39 100644
@@ -89,11 +89,9 @@ ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
  */
 static int test_24bit_addr(unsigned long ip, unsigned long addr)
 {
-	ppc_inst_t op;
 	addr = ppc_function_entry((void *)addr);
 
-	/* use the create_branch to verify that this offset can be branched */
-	return create_branch(&op, (u32 *)ip, addr, 0) == 0;
+	return is_offset_in_branch_range(addr - ip);
 }
 
 static int is_bl_op(ppc_inst_t op)
@@ -261,7 +259,6 @@ __ftrace_make_nop(struct module *mod,
 static unsigned long find_ftrace_tramp(unsigned long ip)
 {
 	int i;
-	ppc_inst_t instr;
 
 	/*
 	 * We have the compiler generated long_branch tramps at the end
@@ -270,8 +267,7 @@ static unsigned long find_ftrace_tramp(unsigned long ip)
 	for (i = NUM_FTRACE_TRAMPS - 1; i >= 0; i--)
 		if (!ftrace_tramps[i])
 			continue;
-		else if (create_branch(&instr, (void *)ip,
-				       ftrace_tramps[i], 0) == 0)
+		else if (is_offset_in_branch_range(ftrace_tramps[i] - ip))
 			return ftrace_tramps[i];
 
 	return 0;
Signature · 1 line
2.35.1
#5 lore raw

PATCH 5/22 powerpc/code-patching: Inline create_branch()

In reply to Christophe Leroy

create_branch() is a good candidate for inlining because:
- Flags can be folded in.
- Range tests are likely to be already done.

Hence reducing the create_branch() to only a set of instructions.

So inline it.

It improves ftrace activation by 10%.

---
 arch/powerpc/include/asm/code-patching.h | 22 ++++++++++++++++++++--
 arch/powerpc/lib/code-patching.c         | 20 --------------------
 2 files changed, 20 insertions(+), 22 deletions(-)
arch/powerpc/include/asm/code-patching.h +20 -2
index e7c5df50cb4e..4260e89f62b1 100644
@@ -49,8 +49,26 @@ static inline bool is_offset_in_cond_branch_range(long offset)
 	return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
 }
 
-int create_branch(ppc_inst_t *instr, const u32 *addr,
-		  unsigned long target, int flags);
+static inline int create_branch(ppc_inst_t *instr, const u32 *addr,
+				unsigned long target, int flags)
+{
+	long offset;
+
+	*instr = ppc_inst(0);
+	offset = target;
+	if (! (flags & BRANCH_ABSOLUTE))
+		offset = offset - (unsigned long)addr;
+
+	/* Check we can represent the target in the instruction format */
+	if (!is_offset_in_branch_range(offset))
+		return 1;
+
+	/* Mask out the flags and target, so they don't step on each other. */
+	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
+
+	return 0;
+}
+
 int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
 		       unsigned long target, int flags);
 int patch_branch(u32 *addr, unsigned long target, int flags);
arch/powerpc/lib/code-patching.c +0 -20
index 58262c7e447c..7adbdb05fee7 100644
@@ -230,26 +230,6 @@ bool is_conditional_branch(ppc_inst_t instr)
 }
 NOKPROBE_SYMBOL(is_conditional_branch);
 
-int create_branch(ppc_inst_t *instr, const u32 *addr,
-		  unsigned long target, int flags)
-{
-	long offset;
-
-	*instr = ppc_inst(0);
-	offset = target;
-	if (! (flags & BRANCH_ABSOLUTE))
-		offset = offset - (unsigned long)addr;
-
-	/* Check we can represent the target in the instruction format */
-	if (!is_offset_in_branch_range(offset))
-		return 1;
-
-	/* Mask out the flags and target, so they don't step on each other. */
-	*instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
-
-	return 0;
-}
-
 int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
 		       unsigned long target, int flags)
 {
Signature · 1 line
2.35.1
#6 lore raw

PATCH 6/22 powerpc/ftrace: Inline ftrace_modify_code()

In reply to Christophe Leroy

Inlining ftrace_modify_code(), it increases a bit the size of ftrace code but brings 5% improvment on ftrace activation.

---
 arch/powerpc/kernel/trace/ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +1 -1
index 41c45b9c7f39..98e82fa4980f 100644
@@ -53,7 +53,7 @@ ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
 	return op;
 }
 
-static int
+static inline int
 ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
 {
 	ppc_inst_t replaced;
Signature · 1 line
2.35.1
#7 lore raw

Re: [PATCH v1 06/22] powerpc/ftrace: Inline ftrace_modify_code()

Quoted 14 lines · Christophe Leroy

Inlining ftrace_modify_code(), it increases a bit the size of ftrace code but brings 5% improvment on ftrace activation.

---
 arch/powerpc/kernel/trace/ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +0 -0
index 41c45b9c7f39..98e82fa4980f 100644

arch/powerpc/kernel/trace/ftrace.c ftrace_call_replace(unsigned long ip, unsigned long addr, int link) · line 59

Quoted 8 lines
arch/powerpc/kernel/trace/ftrace.c +1 -1
@@ -53,7 +53,7 @@ ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
 	return op;
 }
 
-static int
+static inline int
 ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
 {
 	ppc_inst_t replaced;

I thought gcc was free to inline functions without the need for 'inline'. Don't you see this being inlined otherwise?

On the flip side, don't we need __always_inline if we want to force inlining?

- Naveen

#8 lore raw

Re: [PATCH v1 06/22] powerpc/ftrace: Inline ftrace_modify_code()

Quoted 18 lines · Naveen N. Rao
Quoted 17 lines · Christophe Leroy

Inlining ftrace_modify_code(), it increases a bit the size of ftrace code but brings 5% improvment on ftrace activation.

---
 arch/powerpc/kernel/trace/ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +0 -0
index 41c45b9c7f39..98e82fa4980f 100644
@@ -53,7 +53,7 @@ ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
 	return op;
 }
 
Quoted 8 lines

arch/powerpc/kernel/trace/ftrace.c ftrace_call_replace(unsigned long ip, unsigned long addr, int link) · line 59

Quoted 5 lines
arch/powerpc/kernel/trace/ftrace.c +1 -1
-static int
+static inline int
 ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
 {
 	ppc_inst_t replaced;

I thought gcc was free to inline functions without the need for 'inline'.

Yes it is.

Quoted 2 lines

On the flip side, don't we need __always_inline if we want to force inlining?

Yes. Since ac7c3e4ff401 ("compiler: enable CONFIG_OPTIMIZE_INLINING forcibly").

cheers

#9 lore raw

Re: [PATCH v1 06/22] powerpc/ftrace: Inline ftrace_modify_code()

In reply to Naveen N. Rao

Le 18/04/2022 à 08:07, Naveen N. Rao a écrit :

Quoted 20 lines
Quoted 19 lines · Christophe Leroy

Inlining ftrace_modify_code(), it increases a bit the size of ftrace code but brings 5% improvment on ftrace activation.

---
 arch/powerpc/kernel/trace/ftrace.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +0 -0
index 41c45b9c7f39..98e82fa4980f 100644
@@ -53,7 +53,7 @@ ftrace_call_replace(unsigned long ip, unsigned long 
 addr, int link)
      return op;
  }
 
Quoted 8 lines
Quoted 5 lines
arch/powerpc/kernel/trace/ftrace.c +1 -1
-static int
+static inline int
  ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
  {

     ppc_inst_t replaced;

I thought gcc was free to inline functions without the need for 'inline'. Don't you see this being inlined otherwise?

Yep, gcc is free to inline, but in that case it doesn't inline unless you suggest it to do so.

Quoted 3 lines

On the flip side, don't we need __always_inline if we want to force inlining?

The question is, do we want to force inlining, even with CONFIG_CC_OPTIMIZE_FOR_SIZE ?

With 'inline', gcc seems to now inline it with CONFIG_CC_OPTIMIZE_FOR_SPEED and still keep it out of line with CONFIG_CC_OPTIMIZE_FOR_SIZE.

Christophe

#10 lore raw

PATCH 7/22 powerpc/ftrace: Use patch_instruction() return directly

In reply to Christophe Leroy

Instead of returning -EPERM when patch_instruction() fails, just return what patch_instruction returns.

That simplifies ftrace_modify_code():

	   0:	94 21 ff c0 	stwu    r1,-64(r1)
	   4:	93 e1 00 3c 	stw     r31,60(r1)
	   8:	7c 7f 1b 79 	mr.     r31,r3
	   c:	40 80 00 30 	bge     3c <ftrace_modify_code+0x3c>
	  10:	93 c1 00 38 	stw     r30,56(r1)
	  14:	7c 9e 23 78 	mr      r30,r4
	  18:	7c a4 2b 78 	mr      r4,r5
	  1c:	80 bf 00 00 	lwz     r5,0(r31)
	  20:	7c 1e 28 40 	cmplw   r30,r5
	  24:	40 82 00 34 	bne     58 <ftrace_modify_code+0x58>
	  28:	83 c1 00 38 	lwz     r30,56(r1)
	  2c:	7f e3 fb 78 	mr      r3,r31
	  30:	83 e1 00 3c 	lwz     r31,60(r1)
	  34:	38 21 00 40 	addi    r1,r1,64
	  38:	48 00 00 00 	b       38 <ftrace_modify_code+0x38>
				38: R_PPC_REL24	patch_instruction

Before:

	   0:	94 21 ff c0 	stwu    r1,-64(r1)
	   4:	93 e1 00 3c 	stw     r31,60(r1)
	   8:	7c 7f 1b 79 	mr.     r31,r3
	   c:	40 80 00 4c 	bge     58 <ftrace_modify_code+0x58>
	  10:	93 c1 00 38 	stw     r30,56(r1)
	  14:	7c 9e 23 78 	mr      r30,r4
	  18:	7c a4 2b 78 	mr      r4,r5
	  1c:	80 bf 00 00 	lwz     r5,0(r31)
	  20:	7c 08 02 a6 	mflr    r0
	  24:	90 01 00 44 	stw     r0,68(r1)
	  28:	7c 1e 28 40 	cmplw   r30,r5
	  2c:	40 82 00 48 	bne     74 <ftrace_modify_code+0x74>
	  30:	7f e3 fb 78 	mr      r3,r31
	  34:	48 00 00 01 	bl      34 <ftrace_modify_code+0x34>
				34: R_PPC_REL24	patch_instruction
	  38:	80 01 00 44 	lwz     r0,68(r1)
	  3c:	20 63 00 00 	subfic  r3,r3,0
	  40:	83 c1 00 38 	lwz     r30,56(r1)
	  44:	7c 63 19 10 	subfe   r3,r3,r3
	  48:	7c 08 03 a6 	mtlr    r0
	  4c:	83 e1 00 3c 	lwz     r31,60(r1)
	  50:	38 21 00 40 	addi    r1,r1,64
	  54:	4e 80 00 20 	blr

It improves ftrace activation/deactivation duration by about 3%.

---
 arch/powerpc/kernel/trace/ftrace.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +1 -4
index 98e82fa4980f..1b05d33f96c6 100644
@@ -78,10 +78,7 @@ ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
 	}
 
 	/* replace the text with the new text */
-	if (patch_instruction((u32 *)ip, new))
-		return -EPERM;
-
-	return 0;
+	return patch_instruction((u32 *)ip, new);
 }
 
 /*
Signature · 1 line
2.35.1
#11 lore raw

Re: [PATCH v1 07/22] powerpc/ftrace: Use patch_instruction() return directly

Quoted 60 lines · Christophe Leroy

Instead of returning -EPERM when patch_instruction() fails, just return what patch_instruction returns.

That simplifies ftrace_modify_code():

	   0:	94 21 ff c0 	stwu    r1,-64(r1)
	   4:	93 e1 00 3c 	stw     r31,60(r1)
	   8:	7c 7f 1b 79 	mr.     r31,r3
	   c:	40 80 00 30 	bge     3c <ftrace_modify_code+0x3c>
	  10:	93 c1 00 38 	stw     r30,56(r1)
	  14:	7c 9e 23 78 	mr      r30,r4
	  18:	7c a4 2b 78 	mr      r4,r5
	  1c:	80 bf 00 00 	lwz     r5,0(r31)
	  20:	7c 1e 28 40 	cmplw   r30,r5
	  24:	40 82 00 34 	bne     58 <ftrace_modify_code+0x58>
	  28:	83 c1 00 38 	lwz     r30,56(r1)
	  2c:	7f e3 fb 78 	mr      r3,r31
	  30:	83 e1 00 3c 	lwz     r31,60(r1)
	  34:	38 21 00 40 	addi    r1,r1,64
	  38:	48 00 00 00 	b       38 <ftrace_modify_code+0x38>
				38: R_PPC_REL24	patch_instruction

Before:

	   0:	94 21 ff c0 	stwu    r1,-64(r1)
	   4:	93 e1 00 3c 	stw     r31,60(r1)
	   8:	7c 7f 1b 79 	mr.     r31,r3
	   c:	40 80 00 4c 	bge     58 <ftrace_modify_code+0x58>
	  10:	93 c1 00 38 	stw     r30,56(r1)
	  14:	7c 9e 23 78 	mr      r30,r4
	  18:	7c a4 2b 78 	mr      r4,r5
	  1c:	80 bf 00 00 	lwz     r5,0(r31)
	  20:	7c 08 02 a6 	mflr    r0
	  24:	90 01 00 44 	stw     r0,68(r1)
	  28:	7c 1e 28 40 	cmplw   r30,r5
	  2c:	40 82 00 48 	bne     74 <ftrace_modify_code+0x74>
	  30:	7f e3 fb 78 	mr      r3,r31
	  34:	48 00 00 01 	bl      34 <ftrace_modify_code+0x34>
				34: R_PPC_REL24	patch_instruction
	  38:	80 01 00 44 	lwz     r0,68(r1)
	  3c:	20 63 00 00 	subfic  r3,r3,0
	  40:	83 c1 00 38 	lwz     r30,56(r1)
	  44:	7c 63 19 10 	subfe   r3,r3,r3
	  48:	7c 08 03 a6 	mtlr    r0
	  4c:	83 e1 00 3c 	lwz     r31,60(r1)
	  50:	38 21 00 40 	addi    r1,r1,64
	  54:	4e 80 00 20 	blr

It improves ftrace activation/deactivation duration by about 3%.

---
 arch/powerpc/kernel/trace/ftrace.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +0 -0
index 98e82fa4980f..1b05d33f96c6 100644

arch/powerpc/kernel/trace/ftrace.c ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new) · line 81

Quoted 8 lines
arch/powerpc/kernel/trace/ftrace.c +1 -4
@@ -78,10 +78,7 @@ ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
 	}
 
 	/* replace the text with the new text */
-	if (patch_instruction((u32 *)ip, new))
-		return -EPERM;
-
-	return 0;
+	return patch_instruction((u32 *)ip, new);

I think the reason we were returning -EPERM is so that ftrace_bug() can throw the right error message. That will change due to this patch, though I'm not sure how much it matters. -EFAULT and -EPERM seem to print almost the same error message.

- Naveen

maintainer
#12 lore raw

Re: [PATCH v1 07/22] powerpc/ftrace: Use patch_instruction() return directly

Quoted 5 lines · On Mon, 18 Apr 2022 11:51:16 +0530 "Naveen N. Rao"…
Quoted 5 lines
arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -78,10 +78,7 @@ ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
 	}
 
Quoted 8 lines

arch/powerpc/kernel/trace/ftrace.c ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new) · line 81

Quoted 6 lines
arch/powerpc/kernel/trace/ftrace.c +1 -4
 	/* replace the text with the new text */
-	if (patch_instruction((u32 *)ip, new))
-		return -EPERM;
-
-	return 0;
+	return patch_instruction((u32 *)ip, new);  

I think the reason we were returning -EPERM is so that ftrace_bug() can

That is correct.

Quoted 3 lines

throw the right error message. That will change due to this patch, though I'm not sure how much it matters. -EFAULT and -EPERM seem to print almost the same error message.

In these cases it helps to know the type of failure, as the way to debug it is different.

-EFAULT: It failed to read it the location. This means that the memory is likely not even mapped in, or the pointer is way off.

-EINVAL: Means that what was read did not match what was expected (the code was already updated, pointing to the wrong location, or simply the calculation of what to expect is incorrect).

-EPERM: Means the write failed. What was read was expected, but the permissions to write have not been updated properly.

Differentiating the three is crucial to looking at where the issue lies when an ftrace_bug() triggers.

-- Steve

#13 lore raw

Re: [PATCH v1 07/22] powerpc/ftrace: Use patch_instruction() return directly

Le 18/04/2022 à 21:44, Steven Rostedt a écrit :

Quoted 33 lines
Quoted 5 lines · On Mon, 18 Apr 2022 11:51:16 +0530 "Naveen N. Rao"…
Quoted 5 lines
arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -78,10 +78,7 @@ ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
  	}
 
Quoted 8 lines

arch/powerpc/kernel/trace/ftrace.c ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new) · line 81

Quoted 6 lines
arch/powerpc/kernel/trace/ftrace.c +1 -4
  	/* replace the text with the new text */
-	if (patch_instruction((u32 *)ip, new))
-		return -EPERM;
-
-	return 0;
+	return patch_instruction((u32 *)ip, new);

I think the reason we were returning -EPERM is so that ftrace_bug() can

That is correct.

Quoted 3 lines

throw the right error message. That will change due to this patch, though I'm not sure how much it matters. -EFAULT and -EPERM seem to print almost the same error message.

In these cases it helps to know the type of failure, as the way to debug it is different.

-EFAULT: It failed to read it the location. This means that the memory is likely not even mapped in, or the pointer is way off.

-EINVAL: Means that what was read did not match what was expected (the code was already updated, pointing to the wrong location, or simply the calculation of what to expect is incorrect).

Quoted 6 lines

-EPERM: Means the write failed. What was read was expected, but the permissions to write have not been updated properly.

Differentiating the three is crucial to looking at where the issue lies when an ftrace_bug() triggers.

Apparently no caller really care about the value returned by patch_instruction(), the ones who check the return value just check that it's not 0.

So the most performant would be to have patch_instruction() return -EPERM instead of -EFAULT in case of failure.

Christophe

#14 lore raw

PATCH 8/22 powerpc/ftrace: Make __ftrace_make_{nop/call}() common to PPC32 and PPC64

In reply to Christophe Leroy

Since c93d4f6ecf4b ("powerpc/ftrace: Add module_trampoline_target() for PPC32"), __ftrace_make_nop() for PPC32 is very similar to the one for PPC64.

Same for __ftrace_make_call().

Make them common.

---
 arch/powerpc/kernel/trace/ftrace.c | 108 +++--------------------------
 1 file changed, 8 insertions(+), 100 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +8 -100
index 1b05d33f96c6..2c7e42e439bb 100644
@@ -114,7 +114,6 @@ static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
 }
 
 #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64
 static int
 __ftrace_make_nop(struct module *mod,
 		  struct dyn_ftrace *rec, unsigned long addr)
@@ -154,10 +153,11 @@ __ftrace_make_nop(struct module *mod,
 		return -EINVAL;
 	}
 
-#ifdef CONFIG_MPROFILE_KERNEL
-	/* When using -mkernel_profile there is no load to jump over */
+	/* When using -mkernel_profile or PPC32 there is no load to jump over */
 	pop = ppc_inst(PPC_RAW_NOP());
 
+#ifdef CONFIG_PPC64
+#ifdef CONFIG_MPROFILE_KERNEL
 	if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) {
 		pr_err("Fetching instruction at %lx failed.\n", ip - 4);
 		return -EFAULT;
@@ -201,6 +201,7 @@ __ftrace_make_nop(struct module *mod,
 		return -EINVAL;
 	}
 #endif /* CONFIG_MPROFILE_KERNEL */
+#endif /* PPC64 */
 
 	if (patch_instruction((u32 *)ip, pop)) {
 		pr_err("Patching NOP failed.\n");
@@ -209,48 +210,6 @@ __ftrace_make_nop(struct module *mod,
 
 	return 0;
 }
-
-#else /* !PPC64 */
-static int
-__ftrace_make_nop(struct module *mod,
-		  struct dyn_ftrace *rec, unsigned long addr)
-{
-	ppc_inst_t op;
-	unsigned long ip = rec->ip;
-	unsigned long tramp, ptr;
-
-	if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE))
-		return -EFAULT;
-
-	/* Make sure that that this is still a 24bit jump */
-	if (!is_bl_op(op)) {
-		pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
-		return -EINVAL;
-	}
-
-	/* lets find where the pointer goes */
-	tramp = find_bl_target(ip, op);
-
-	/* Find where the trampoline jumps to */
-	if (module_trampoline_target(mod, tramp, &ptr)) {
-		pr_err("Failed to get trampoline target\n");
-		return -EFAULT;
-	}
-
-	if (ptr != addr) {
-		pr_err("Trampoline location %08lx does not match addr\n",
-		       tramp);
-		return -EINVAL;
-	}
-
-	op = ppc_inst(PPC_RAW_NOP());
-
-	if (patch_instruction((u32 *)ip, op))
-		return -EPERM;
-
-	return 0;
-}
-#endif /* PPC64 */
 #endif /* CONFIG_MODULES */
 
 static unsigned long find_ftrace_tramp(unsigned long ip)
@@ -437,13 +396,12 @@ int ftrace_make_nop(struct module *mod,
 }
 
 #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64
 /*
  * Examine the existing instructions for __ftrace_make_call.
  * They should effectively be a NOP, and follow formal constraints,
  * depending on the ABI. Return false if they don't.
  */
-#ifndef CONFIG_MPROFILE_KERNEL
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
@@ -465,7 +423,7 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	/* look for patched "NOP" on ppc64 with -mprofile-kernel */
+	/* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */
 	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
 		return 0;
 	return 1;
@@ -484,8 +442,10 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 	if (copy_inst_from_kernel_nofault(op, ip))
 		return -EFAULT;
 
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	if (copy_inst_from_kernel_nofault(op + 1, ip + 4))
 		return -EFAULT;
+#endif
 
 	if (!expected_nop_sequence(ip, op[0], op[1])) {
 		pr_err("Unexpected call sequence at %p: %s %s\n",
@@ -531,58 +491,6 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 
 	return 0;
 }
-
-#else  /* !CONFIG_PPC64: */
-static int
-__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
-{
-	int err;
-	ppc_inst_t op;
-	u32 *ip = (u32 *)rec->ip;
-	struct module *mod = rec->arch.mod;
-	unsigned long tramp;
-
-	/* read where this goes */
-	if (copy_inst_from_kernel_nofault(&op, ip))
-		return -EFAULT;
-
-	/* It should be pointing to a nop */
-	if (!ppc_inst_equal(op,  ppc_inst(PPC_RAW_NOP()))) {
-		pr_err("Expected NOP but have %s\n", ppc_inst_as_str(op));
-		return -EINVAL;
-	}
-
-	/* If we never set up a trampoline to ftrace_caller, then bail */
-#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
-	if (!mod->arch.tramp || !mod->arch.tramp_regs) {
-#else
-	if (!mod->arch.tramp) {
-#endif
-		pr_err("No ftrace trampoline\n");
-		return -EINVAL;
-	}
-
-#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
-	if (rec->flags & FTRACE_FL_REGS)
-		tramp = mod->arch.tramp_regs;
-	else
-#endif
-		tramp = mod->arch.tramp;
-	/* create the branch to the trampoline */
-	err = create_branch(&op, ip, tramp, BRANCH_SET_LINK);
-	if (err) {
-		pr_err("REL24 out of range!\n");
-		return -EINVAL;
-	}
-
-	pr_devel("write to %lx\n", rec->ip);
-
-	if (patch_instruction(ip, op))
-		return -EPERM;
-
-	return 0;
-}
-#endif /* CONFIG_PPC64 */
 #endif /* CONFIG_MODULES */
 
 static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)
Signature · 1 line
2.35.1
#15 lore raw

Re: [PATCH v1 08/22] powerpc/ftrace: Make __ftrace_make_{nop/call}() common to PPC32 and PPC64

Quoted 26 lines · Christophe Leroy

Since c93d4f6ecf4b ("powerpc/ftrace: Add module_trampoline_target() for PPC32"), __ftrace_make_nop() for PPC32 is very similar to the one for PPC64.

Same for __ftrace_make_call().

Make them common.

---
 arch/powerpc/kernel/trace/ftrace.c | 108 +++--------------------------
 1 file changed, 8 insertions(+), 100 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +0 -1
index 1b05d33f96c6..2c7e42e439bb 100644
@@ -114,7 +114,6 @@ static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
 }
 
 #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64
 static int
 __ftrace_make_nop(struct module *mod,
 		  struct dyn_ftrace *rec, unsigned long addr)

arch/powerpc/kernel/trace/ftrace.c __ftrace_make_nop(struct module *mod, · line 156

Quoted 6 lines
arch/powerpc/kernel/trace/ftrace.c +1 -2
@@ -154,10 +153,11 @@ __ftrace_make_nop(struct module *mod,
 		return -EINVAL;
 	}
 
-#ifdef CONFIG_MPROFILE_KERNEL
-	/* When using -mkernel_profile there is no load to jump over */
+	/* When using -mkernel_profile or PPC32 there is no load to jump over */

-mprofile-kernel

Since you are modifying that line anyway ^^

Quoted 66 lines
arch/powerpc/kernel/trace/ftrace.c +3 -42
 	pop = ppc_inst(PPC_RAW_NOP());
 
+#ifdef CONFIG_PPC64
+#ifdef CONFIG_MPROFILE_KERNEL
 	if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) {
 		pr_err("Fetching instruction at %lx failed.\n", ip - 4);
 		return -EFAULT;
@@ -201,6 +201,7 @@ __ftrace_make_nop(struct module *mod,
 		return -EINVAL;
 	}
 #endif /* CONFIG_MPROFILE_KERNEL */
+#endif /* PPC64 */
 
 	if (patch_instruction((u32 *)ip, pop)) {
 		pr_err("Patching NOP failed.\n");
@@ -209,48 +210,6 @@ __ftrace_make_nop(struct module *mod,
 
 	return 0;
 }
-
-#else /* !PPC64 */
-static int
-__ftrace_make_nop(struct module *mod,
-		  struct dyn_ftrace *rec, unsigned long addr)
-{
-	ppc_inst_t op;
-	unsigned long ip = rec->ip;
-	unsigned long tramp, ptr;
-
-	if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE))
-		return -EFAULT;
-
-	/* Make sure that that this is still a 24bit jump */
-	if (!is_bl_op(op)) {
-		pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
-		return -EINVAL;
-	}
-
-	/* lets find where the pointer goes */
-	tramp = find_bl_target(ip, op);
-
-	/* Find where the trampoline jumps to */
-	if (module_trampoline_target(mod, tramp, &ptr)) {
-		pr_err("Failed to get trampoline target\n");
-		return -EFAULT;
-	}
-
-	if (ptr != addr) {
-		pr_err("Trampoline location %08lx does not match addr\n",
-		       tramp);
-		return -EINVAL;
-	}
-
-	op = ppc_inst(PPC_RAW_NOP());
-
-	if (patch_instruction((u32 *)ip, op))
-		return -EPERM;
-
-	return 0;
-}
-#endif /* PPC64 */
 #endif /* CONFIG_MODULES */
 
 static unsigned long find_ftrace_tramp(unsigned long ip)
@@ -437,13 +396,12 @@ int ftrace_make_nop(struct module *mod,
 }

arch/powerpc/kernel/trace/ftrace.c int ftrace_make_nop(struct module *mod, · line 404

Quoted 10 lines
arch/powerpc/kernel/trace/ftrace.c +1 -2
 
 #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64
 /*
  * Examine the existing instructions for __ftrace_make_call.
  * They should effectively be a NOP, and follow formal constraints,
  * depending on the ABI. Return false if they don't.
  */
-#ifndef CONFIG_MPROFILE_KERNEL
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS

It is better to gate this on PPC64_ELF_ABI_v1

Quoted 13 lines
arch/powerpc/kernel/trace/ftrace.c +1 -1
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
@@ -465,7 +423,7 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	/* look for patched "NOP" on ppc64 with -mprofile-kernel */
+	/* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */
 	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
 		return 0;
 	return 1;

arch/powerpc/kernel/trace/ftrace.c __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) · line 448

Quoted 7 lines
arch/powerpc/kernel/trace/ftrace.c +2 -0
@@ -484,8 +442,10 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 	if (copy_inst_from_kernel_nofault(op, ip))
 		return -EFAULT;
 
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	if (copy_inst_from_kernel_nofault(op + 1, ip + 4))
 		return -EFAULT;
+#endif

Here too...

- Naveen

#16 lore raw

Re: [PATCH v1 08/22] powerpc/ftrace: Make __ftrace_make_{nop/call}() common to PPC32 and PPC64

Le 18/04/2022 à 08:40, Naveen N. Rao a écrit :

Quoted 32 lines
Quoted 31 lines · Christophe Leroy

Since c93d4f6ecf4b ("powerpc/ftrace: Add module_trampoline_target() for PPC32"), __ftrace_make_nop() for PPC32 is very similar to the one for PPC64.

Same for __ftrace_make_call().

Make them common.

---
 arch/powerpc/kernel/trace/ftrace.c | 108 +++--------------------------

 1 file changed, 8 insertions(+), 100 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +0 -1
index 1b05d33f96c6..2c7e42e439bb 100644
@@ -114,7 +114,6 @@ static unsigned long find_bl_target(unsigned long 
 ip, ppc_inst_t op)
  }
 
  #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64

 static int
 __ftrace_make_nop(struct module *mod,
           struct dyn_ftrace *rec, unsigned long addr)

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -154,10 +153,11 @@ __ftrace_make_nop(struct module *mod,
          return -EINVAL;
      }
 
Quoted 7 lines
Quoted 4 lines
arch/powerpc/kernel/trace/ftrace.c +1 -2
-#ifdef CONFIG_MPROFILE_KERNEL
-    /* When using -mkernel_profile there is no load to jump over */
+    /* When using -mkernel_profile or PPC32 there is no load to jump 

over */

                     -mprofile-kernel

Since you are modifying that line anyway ^^

ok

Quoted 69 lines
Quoted 69 lines

     pop = ppc_inst(PPC_RAW_NOP());

arch/powerpc/kernel/trace/ftrace.c +2 -0
+#ifdef CONFIG_PPC64
+#ifdef CONFIG_MPROFILE_KERNEL

     if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) { pr_err("Fetching instruction at %lx failed.\n", ip - 4); return -EFAULT;

arch/powerpc/kernel/trace/ftrace.c +1 -42
@@ -201,6 +201,7 @@ __ftrace_make_nop(struct module *mod,
          return -EINVAL;
      }
  #endif /* CONFIG_MPROFILE_KERNEL */
+#endif /* PPC64 */
 
      if (patch_instruction((u32 *)ip, pop)) {
          pr_err("Patching NOP failed.\n");
@@ -209,48 +210,6 @@ __ftrace_make_nop(struct module *mod,
 
      return 0;
  }
-
-#else /* !PPC64 */
-static int
-__ftrace_make_nop(struct module *mod,
-          struct dyn_ftrace *rec, unsigned long addr)
-{
-    ppc_inst_t op;
-    unsigned long ip = rec->ip;
-    unsigned long tramp, ptr;
-
-    if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE))
-        return -EFAULT;
-
-    /* Make sure that that this is still a 24bit jump */
-    if (!is_bl_op(op)) {
-        pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
-        return -EINVAL;
-    }
-
-    /* lets find where the pointer goes */
-    tramp = find_bl_target(ip, op);
-
-    /* Find where the trampoline jumps to */
-    if (module_trampoline_target(mod, tramp, &ptr)) {
-        pr_err("Failed to get trampoline target\n");
-        return -EFAULT;
-    }
-
-    if (ptr != addr) {
-        pr_err("Trampoline location %08lx does not match addr\n",
-               tramp);
-        return -EINVAL;
-    }
-
-    op = ppc_inst(PPC_RAW_NOP());
-
-    if (patch_instruction((u32 *)ip, op))
-        return -EPERM;
-
-    return 0;
-}
-#endif /* PPC64 */
  #endif /* CONFIG_MODULES */
 
  static unsigned long find_ftrace_tramp(unsigned long ip)
@@ -437,13 +396,12 @@ int ftrace_make_nop(struct module *mod,
  }
 
Quoted 11 lines

arch/powerpc/kernel/trace/ftrace.c int ftrace_make_nop(struct module *mod, · line 404

Quoted 9 lines
arch/powerpc/kernel/trace/ftrace.c +1 -2
  #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64
  /*
   * Examine the existing instructions for __ftrace_make_call.
   * They should effectively be a NOP, and follow formal constraints,
   * depending on the ABI. Return false if they don't.
   */
-#ifndef CONFIG_MPROFILE_KERNEL
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS

It is better to gate this on PPC64_ELF_ABI_v1

Well ... let then first change this to a CONFIG_ item, it should simplify some stuff here and there.

Quoted 19 lines
Quoted 19 lines

 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1) {

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -465,7 +423,7 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, 
 ppc_inst_t op1)

 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1) {
-    /* look for patched "NOP" on ppc64 with -mprofile-kernel */ +    /* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */ if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP()))) return 0;
     return 1;

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -484,8 +442,10 @@ __ftrace_make_call(struct dyn_ftrace *rec, 
 unsigned long addr)
      if (copy_inst_from_kernel_nofault(op, ip))
          return -EFAULT;
 
Quoted 7 lines

arch/powerpc/kernel/trace/ftrace.c __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) · line 448

Quoted 4 lines
arch/powerpc/kernel/trace/ftrace.c +2 -0
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
      if (copy_inst_from_kernel_nofault(op + 1, ip + 4))
          return -EFAULT;
+#endif

Here too...

Ok

Christophe

#17 lore raw

Re: [PATCH v1 08/22] powerpc/ftrace: Make __ftrace_make_{nop/call}() common to PPC32 and PPC64

In reply to Naveen N. Rao

Le 18/04/2022 à 08:40, Naveen N. Rao a écrit :

Quoted 108 lines
Quoted 34 lines · Christophe Leroy

Since c93d4f6ecf4b ("powerpc/ftrace: Add module_trampoline_target() for PPC32"), __ftrace_make_nop() for PPC32 is very similar to the one for PPC64.

Same for __ftrace_make_call().

Make them common.

---
 arch/powerpc/kernel/trace/ftrace.c | 108 +++--------------------------

 1 file changed, 8 insertions(+), 100 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +0 -1
index 1b05d33f96c6..2c7e42e439bb 100644
@@ -114,7 +114,6 @@ static unsigned long find_bl_target(unsigned long 
 ip, ppc_inst_t op)
  }
 
  #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64

 static int
 __ftrace_make_nop(struct module *mod,
           struct dyn_ftrace *rec, unsigned long addr)

arch/powerpc/kernel/trace/ftrace.c +1 -2
@@ -154,10 +153,11 @@ __ftrace_make_nop(struct module *mod,
          return -EINVAL;
      }
 
-#ifdef CONFIG_MPROFILE_KERNEL
-    /* When using -mkernel_profile there is no load to jump over */
+    /* When using -mkernel_profile or PPC32 there is no load to jump 
Quoted 1 line

over */

                     -mprofile-kernel

Since you are modifying that line anyway ^^

Quoted 67 lines

     pop = ppc_inst(PPC_RAW_NOP());

arch/powerpc/kernel/trace/ftrace.c +2 -0
+#ifdef CONFIG_PPC64
+#ifdef CONFIG_MPROFILE_KERNEL

     if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) { pr_err("Fetching instruction at %lx failed.\n", ip - 4); return -EFAULT;

arch/powerpc/kernel/trace/ftrace.c +1 -42
@@ -201,6 +201,7 @@ __ftrace_make_nop(struct module *mod,
          return -EINVAL;
      }
  #endif /* CONFIG_MPROFILE_KERNEL */
+#endif /* PPC64 */
 
      if (patch_instruction((u32 *)ip, pop)) {
          pr_err("Patching NOP failed.\n");
@@ -209,48 +210,6 @@ __ftrace_make_nop(struct module *mod,
 
      return 0;
  }
-
-#else /* !PPC64 */
-static int
-__ftrace_make_nop(struct module *mod,
-          struct dyn_ftrace *rec, unsigned long addr)
-{
-    ppc_inst_t op;
-    unsigned long ip = rec->ip;
-    unsigned long tramp, ptr;
-
-    if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE))
-        return -EFAULT;
-
-    /* Make sure that that this is still a 24bit jump */
-    if (!is_bl_op(op)) {
-        pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
-        return -EINVAL;
-    }
-
-    /* lets find where the pointer goes */
-    tramp = find_bl_target(ip, op);
-
-    /* Find where the trampoline jumps to */
-    if (module_trampoline_target(mod, tramp, &ptr)) {
-        pr_err("Failed to get trampoline target\n");
-        return -EFAULT;
-    }
-
-    if (ptr != addr) {
-        pr_err("Trampoline location %08lx does not match addr\n",
-               tramp);
-        return -EINVAL;
-    }
-
-    op = ppc_inst(PPC_RAW_NOP());
-
-    if (patch_instruction((u32 *)ip, op))
-        return -EPERM;
-
-    return 0;
-}
-#endif /* PPC64 */
  #endif /* CONFIG_MODULES */
 
  static unsigned long find_ftrace_tramp(unsigned long ip)
@@ -437,13 +396,12 @@ int ftrace_make_nop(struct module *mod,
  }
 
Quoted 11 lines

arch/powerpc/kernel/trace/ftrace.c int ftrace_make_nop(struct module *mod, · line 404

Quoted 9 lines
arch/powerpc/kernel/trace/ftrace.c +1 -2
  #ifdef CONFIG_MODULES
-#ifdef CONFIG_PPC64
  /*
   * Examine the existing instructions for __ftrace_make_call.
   * They should effectively be a NOP, and follow formal constraints,
   * depending on the ABI. Return false if they don't.
   */
-#ifndef CONFIG_MPROFILE_KERNEL
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS

It is better to gate this on PPC64_ELF_ABI_v1

Ok I do that with the new CONFIG_PPC64_ELF_ABI_V1.

Quoted 19 lines
Quoted 19 lines

 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1) {

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -465,7 +423,7 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, 
 ppc_inst_t op1)

 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1) {
-    /* look for patched "NOP" on ppc64 with -mprofile-kernel */ +    /* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */ if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP()))) return 0;
     return 1;

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -484,8 +442,10 @@ __ftrace_make_call(struct dyn_ftrace *rec, 
 unsigned long addr)
      if (copy_inst_from_kernel_nofault(op, ip))
          return -EFAULT;
 
Quoted 7 lines

arch/powerpc/kernel/trace/ftrace.c __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) · line 448

Quoted 4 lines
arch/powerpc/kernel/trace/ftrace.c +2 -0
+#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
      if (copy_inst_from_kernel_nofault(op + 1, ip + 4))
          return -EFAULT;
+#endif

Here too...

Done

Christophe

#18 lore raw

PATCH 9/22 powerpc/ftrace: Don't include ftrace.o for CONFIG_FTRACE_SYSCALLS

In reply to Christophe Leroy

Since commit 7bea7ac0ca01 ("powerpc/syscalls: Fix syscall tracing") ftrace.o is not needed anymore for CONFIG_FTRACE_SYSCALLS.

---
 arch/powerpc/kernel/trace/Makefile | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kernel/trace/Makefile +0 -1
index 542aa7a8b2b4..fc32ec30b297 100644
@@ -17,7 +17,6 @@ endif
 obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace_low.o
 obj-$(CONFIG_DYNAMIC_FTRACE)		+= ftrace.o
 obj-$(CONFIG_FUNCTION_GRAPH_TRACER)	+= ftrace.o
-obj-$(CONFIG_FTRACE_SYSCALLS)		+= ftrace.o
 obj-$(CONFIG_TRACING)			+= trace_clock.o
 
 obj-$(CONFIG_PPC64)			+= $(obj64-y)
Signature · 1 line
2.35.1
#19 lore raw

PATCH 10/22 powerpc/ftrace: Use CONFIG_FUNCTION_TRACER instead of CONFIG_DYNAMIC_FTRACE

In reply to Christophe Leroy

Since commit 0c0c52306f47 ("powerpc: Only support DYNAMIC_FTRACE not static"), CONFIG_DYNAMIC_FTRACE is always selected when CONFIG_FUNCTION_TRACER is selected.

To avoid confusion and have the reader wonder what's happen when CONFIG_FUNCTION_TRACER is selected and CONFIG_DYNAMIC_FTRACE is not, use CONFIG_FUNCTION_TRACER in ifdefs instead of CONFIG_DYNAMIC_FTRACE.

As CONFIG_FUNCTION_GRAPH_TRACER depends on CONFIG_FUNCTION_TRACER, ftrace.o doesn't need to appear for both symbols in Makefile.

Then as ftrace.o is built only when CONFIG_FUNCTION_TRACER is selected ifdef CONFIG_FUNCTION_TRACER is not needed in ftrace.c

---
 arch/powerpc/include/asm/book3s/32/pgtable.h | 2 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h | 2 +-
 arch/powerpc/include/asm/module.h            | 4 ++--
 arch/powerpc/include/asm/nohash/pgtable.h    | 2 +-
 arch/powerpc/kernel/module_32.c              | 4 ++--
 arch/powerpc/kernel/module_64.c              | 6 +++---
 arch/powerpc/kernel/trace/Makefile           | 4 +---
 arch/powerpc/kernel/trace/ftrace.c           | 4 ----
 8 files changed, 11 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +1 -1
index 772e00dc4ef1..992aed626eb4 100644
@@ -124,7 +124,7 @@ static inline bool pte_user(pte_t pte)
  * on platforms where such control is possible.
  */
 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\
-	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+	defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
 #else
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
arch/powerpc/include/asm/book3s/64/pgtable.h +1 -1
index 875730d5af40..cf01b609572f 100644
@@ -169,7 +169,7 @@
  * on platforms where such control is possible.
  */
 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) || \
-	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+	defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
 #else
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
arch/powerpc/include/asm/module.h +2 -2
index 857d9ff24295..e6f5963fd96e 100644
@@ -39,7 +39,7 @@ struct mod_arch_specific {
 	unsigned int init_plt_section;
 #endif /* powerpc64 */
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	unsigned long tramp;
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	unsigned long tramp_regs;
@@ -68,7 +68,7 @@ struct mod_arch_specific {
 #    endif	/* MODULE */
 #endif
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 #    ifdef MODULE
 	asm(".section .ftrace.tramp,\"ax\",@nobits; .align 3; .previous");
 #    endif	/* MODULE */
arch/powerpc/include/asm/nohash/pgtable.h +1 -1
index ac75f4ab0dba..2e8cf217a191 100644
@@ -23,7 +23,7 @@
  * on platforms where such control is possible.
  */
 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\
-	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+	defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
 #else
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
arch/powerpc/kernel/module_32.c +2 -2
index a0432ef46967..2aa368ce21c9 100644
@@ -39,7 +39,7 @@ static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
 			r_addend = rela[i].r_addend;
 		}
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	_count_relocs++;	/* add one for ftrace_caller */
 #endif
 	return _count_relocs;
@@ -288,7 +288,7 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
 	return 0;
 }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 int module_trampoline_target(struct module *mod, unsigned long addr,
 			     unsigned long *target)
 {
arch/powerpc/kernel/module_64.c +3 -3
index 794720530442..b13a72665eee 100644
@@ -207,7 +207,7 @@ static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
 		}
 	}
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	/* make the trampoline to the ftrace_caller */
 	relocs++;
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
@@ -372,7 +372,7 @@ static bool is_mprofile_ftrace_call(const char *name)
 {
 	if (!strcmp("_mcount", name))
 		return true;
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	if (!strcmp("ftrace_caller", name))
 		return true;
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
@@ -740,7 +740,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 	return 0;
 }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 int module_trampoline_target(struct module *mod, unsigned long addr,
 			     unsigned long *target)
 {
arch/powerpc/kernel/trace/Makefile +1 -3
index fc32ec30b297..af8527538fe4 100644
@@ -14,9 +14,7 @@ obj64-$(CONFIG_FUNCTION_TRACER)		+= ftrace_mprofile.o
 else
 obj64-$(CONFIG_FUNCTION_TRACER)		+= ftrace_64_pg.o
 endif
-obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace_low.o
-obj-$(CONFIG_DYNAMIC_FTRACE)		+= ftrace.o
-obj-$(CONFIG_FUNCTION_GRAPH_TRACER)	+= ftrace.o
+obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace_low.o ftrace.o
 obj-$(CONFIG_TRACING)			+= trace_clock.o
 
 obj-$(CONFIG_PPC64)			+= $(obj64-y)
arch/powerpc/kernel/trace/ftrace.c +0 -4
index 2c7e42e439bb..188f59f4ee4a 100644
@@ -28,9 +28,6 @@
 #include <asm/syscall.h>
 #include <asm/inst.h>
 
-
-#ifdef CONFIG_DYNAMIC_FTRACE
-
 /*
  * We generally only have a single long_branch tramp and at most 2 or 3 plt
  * tramps generated. But, we don't use the plt tramps currently. We also allot
@@ -783,7 +780,6 @@ int __init ftrace_dyn_arch_init(void)
 	return 0;
 }
 #endif
-#endif /* CONFIG_DYNAMIC_FTRACE */
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 
Signature · 1 line
2.35.1
#20 lore raw

Re: [PATCH v1 10/22] powerpc/ftrace: Use CONFIG_FUNCTION_TRACER instead of CONFIG_DYNAMIC_FTRACE

Quoted 4 lines · Christophe Leroy

Since commit 0c0c52306f47 ("powerpc: Only support DYNAMIC_FTRACE not static"), CONFIG_DYNAMIC_FTRACE is always selected when CONFIG_FUNCTION_TRACER is selected.

Quoted 8 lines

To avoid confusion and have the reader wonder what's happen when CONFIG_FUNCTION_TRACER is selected and CONFIG_DYNAMIC_FTRACE is not, use CONFIG_FUNCTION_TRACER in ifdefs instead of CONFIG_DYNAMIC_FTRACE.

As CONFIG_FUNCTION_GRAPH_TRACER depends on CONFIG_FUNCTION_TRACER, ftrace.o doesn't need to appear for both symbols in Makefile.

Then as ftrace.o is built only when CONFIG_FUNCTION_TRACER is selected

and since it implies CONFIG_DYNAMIC_FTRACE, CONFIG_DYNAMIC_FTRACE is not needed in ftrace.c

Quoted 120 lines

ifdef CONFIG_FUNCTION_TRACER is not needed in ftrace.c

---
 arch/powerpc/include/asm/book3s/32/pgtable.h | 2 +-
 arch/powerpc/include/asm/book3s/64/pgtable.h | 2 +-
 arch/powerpc/include/asm/module.h            | 4 ++--
 arch/powerpc/include/asm/nohash/pgtable.h    | 2 +-
 arch/powerpc/kernel/module_32.c              | 4 ++--
 arch/powerpc/kernel/module_64.c              | 6 +++---
 arch/powerpc/kernel/trace/Makefile           | 4 +---
 arch/powerpc/kernel/trace/ftrace.c           | 4 ----
 8 files changed, 11 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +1 -1
index 772e00dc4ef1..992aed626eb4 100644
@@ -124,7 +124,7 @@ static inline bool pte_user(pte_t pte)
  * on platforms where such control is possible.
  */
 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\
-	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+	defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
 #else
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
arch/powerpc/include/asm/book3s/64/pgtable.h +1 -1
index 875730d5af40..cf01b609572f 100644
@@ -169,7 +169,7 @@
  * on platforms where such control is possible.
  */
 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) || \
-	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+	defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
 #else
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
arch/powerpc/include/asm/module.h +2 -2
index 857d9ff24295..e6f5963fd96e 100644
@@ -39,7 +39,7 @@ struct mod_arch_specific {
 	unsigned int init_plt_section;
 #endif /* powerpc64 */
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	unsigned long tramp;
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	unsigned long tramp_regs;
@@ -68,7 +68,7 @@ struct mod_arch_specific {
 #    endif	/* MODULE */
 #endif
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 #    ifdef MODULE
 	asm(".section .ftrace.tramp,\"ax\",@nobits; .align 3; .previous");
 #    endif	/* MODULE */
arch/powerpc/include/asm/nohash/pgtable.h +1 -1
index ac75f4ab0dba..2e8cf217a191 100644
@@ -23,7 +23,7 @@
  * on platforms where such control is possible.
  */
 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\
-	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+	defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
 #else
 #define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
arch/powerpc/kernel/module_32.c +2 -2
index a0432ef46967..2aa368ce21c9 100644
@@ -39,7 +39,7 @@ static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
 			r_addend = rela[i].r_addend;
 		}
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	_count_relocs++;	/* add one for ftrace_caller */
 #endif
 	return _count_relocs;
@@ -288,7 +288,7 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
 	return 0;
 }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 int module_trampoline_target(struct module *mod, unsigned long addr,
 			     unsigned long *target)
 {
arch/powerpc/kernel/module_64.c +2 -2
index 794720530442..b13a72665eee 100644
@@ -207,7 +207,7 @@ static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
 		}
 	}
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	/* make the trampoline to the ftrace_caller */
 	relocs++;
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
@@ -372,7 +372,7 @@ static bool is_mprofile_ftrace_call(const char *name)
 {
 	if (!strcmp("_mcount", name))
 		return true;
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 	if (!strcmp("ftrace_caller", name))
 		return true;
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS

arch/powerpc/kernel/module_64.c int apply_relocate_add(Elf64_Shdr *sechdrs, · line 746

Quoted 8 lines
arch/powerpc/kernel/module_64.c +1 -1
@@ -740,7 +740,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 	return 0;
 }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
 int module_trampoline_target(struct module *mod, unsigned long addr,
 			     unsigned long *target)
 {

The below two changes to trace/Makefile and trace/ftrace.c make sense, but I'm not sure the above changes are necessary.

For generic code (outside arch/powerpc/kernel/trace), I think it is good to retain the actual dependency which is DYNAMIC_FTRACE. In my view, gating some of the above code on FUNCTION_TRACER is also confusing. The primary implication of always selecting DYNAMIC_FTRACE is within the powerpc trace code, and it is good to keep it that way.

Quoted 40 lines
arch/powerpc/kernel/trace/Makefile +1 -3
index fc32ec30b297..af8527538fe4 100644
@@ -14,9 +14,7 @@ obj64-$(CONFIG_FUNCTION_TRACER)		+= ftrace_mprofile.o
 else
 obj64-$(CONFIG_FUNCTION_TRACER)		+= ftrace_64_pg.o
 endif
-obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace_low.o
-obj-$(CONFIG_DYNAMIC_FTRACE)		+= ftrace.o
-obj-$(CONFIG_FUNCTION_GRAPH_TRACER)	+= ftrace.o
+obj-$(CONFIG_FUNCTION_TRACER)		+= ftrace_low.o ftrace.o
 obj-$(CONFIG_TRACING)			+= trace_clock.o
 
 obj-$(CONFIG_PPC64)			+= $(obj64-y)
arch/powerpc/kernel/trace/ftrace.c +0 -4
index 2c7e42e439bb..188f59f4ee4a 100644
@@ -28,9 +28,6 @@
 #include <asm/syscall.h>
 #include <asm/inst.h>
 
-
-#ifdef CONFIG_DYNAMIC_FTRACE
-
 /*
  * We generally only have a single long_branch tramp and at most 2 or 3 plt
  * tramps generated. But, we don't use the plt tramps currently. We also allot
@@ -783,7 +780,6 @@ int __init ftrace_dyn_arch_init(void)
 	return 0;
 }
 #endif
-#endif /* CONFIG_DYNAMIC_FTRACE */
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 
Signature · 1 line
2.35.1

- Naveen

#21 lore raw

Re: [PATCH v1 10/22] powerpc/ftrace: Use CONFIG_FUNCTION_TRACER instead of CONFIG_DYNAMIC_FTRACE

Le 18/04/2022 à 09:00, Naveen N. Rao a écrit :

Quoted 9 lines
Quoted 8 lines · Christophe Leroy

Since commit 0c0c52306f47 ("powerpc: Only support DYNAMIC_FTRACE not static"), CONFIG_DYNAMIC_FTRACE is always selected when CONFIG_FUNCTION_TRACER is selected.

To avoid confusion and have the reader wonder what's happen when CONFIG_FUNCTION_TRACER is selected and CONFIG_DYNAMIC_FTRACE is not, use CONFIG_FUNCTION_TRACER in ifdefs instead of CONFIG_DYNAMIC_FTRACE.

Quoted 7 lines
Quoted 4 lines

As CONFIG_FUNCTION_GRAPH_TRACER depends on CONFIG_FUNCTION_TRACER, ftrace.o doesn't need to appear for both symbols in Makefile.

Then as ftrace.o is built only when CONFIG_FUNCTION_TRACER is selected

and since it implies CONFIG_DYNAMIC_FTRACE, CONFIG_DYNAMIC_FTRACE is not needed in ftrace.c

Ok, added to the commit message

Quoted 144 lines
Quoted 132 lines

ifdef CONFIG_FUNCTION_TRACER is not needed in ftrace.c

---
 arch/powerpc/include/asm/book3s/32/pgtable.h | 2 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 2 +- arch/powerpc/include/asm/module.h            | 4 ++-- arch/powerpc/include/asm/nohash/pgtable.h    | 2 +- arch/powerpc/kernel/module_32.c              | 4 ++-- arch/powerpc/kernel/module_64.c              | 6 +++--- arch/powerpc/kernel/trace/Makefile           | 4 +--- arch/powerpc/kernel/trace/ftrace.c           | 4 ----

 8 files changed, 11 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +1 -1
index 772e00dc4ef1..992aed626eb4 100644
@@ -124,7 +124,7 @@ static inline bool pte_user(pte_t pte)
   * on platforms where such control is possible.
   */
  #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || 
 defined(CONFIG_BDI_SWITCH) ||\
-    defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+    defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
  #define PAGE_KERNEL_TEXT    PAGE_KERNEL_X
  #else

 #define PAGE_KERNEL_TEXT    PAGE_KERNEL_ROX

arch/powerpc/include/asm/book3s/64/pgtable.h +1 -1
index 875730d5af40..cf01b609572f 100644
@@ -169,7 +169,7 @@
   * on platforms where such control is possible.
   */
  #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || 
 defined(CONFIG_BDI_SWITCH) || \
-    defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+    defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
  #define PAGE_KERNEL_TEXT    PAGE_KERNEL_X
  #else

 #define PAGE_KERNEL_TEXT    PAGE_KERNEL_ROX

arch/powerpc/include/asm/module.h +2 -2
index 857d9ff24295..e6f5963fd96e 100644
@@ -39,7 +39,7 @@ struct mod_arch_specific {
      unsigned int init_plt_section;
  #endif /* powerpc64 */
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
      unsigned long tramp;
  #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
      unsigned long tramp_regs;
@@ -68,7 +68,7 @@ struct mod_arch_specific {
  #    endif    /* MODULE */
  #endif
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
  #    ifdef MODULE
      asm(".section .ftrace.tramp,\"ax\",@nobits; .align 3; .previous");
  #    endif    /* MODULE */
arch/powerpc/include/asm/nohash/pgtable.h +1 -1
index ac75f4ab0dba..2e8cf217a191 100644
@@ -23,7 +23,7 @@
   * on platforms where such control is possible.
   */
  #if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || 
 defined(CONFIG_BDI_SWITCH) ||\
-    defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
+    defined(CONFIG_KPROBES) || defined(CONFIG_FUNCTION_TRACER)
  #define PAGE_KERNEL_TEXT    PAGE_KERNEL_X
  #else

 #define PAGE_KERNEL_TEXT    PAGE_KERNEL_ROX

arch/powerpc/kernel/module_32.c +1 -1
index a0432ef46967..2aa368ce21c9 100644
@@ -39,7 +39,7 @@ static unsigned int count_relocs(const Elf32_Rela 
 *rela, unsigned int num)
              r_addend = rela[i].r_addend;
          }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER

     _count_relocs++;    /* add one for ftrace_caller */ #endif
     return _count_relocs;

arch/powerpc/kernel/module_32.c +1 -1
@@ -288,7 +288,7 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
      return 0;
  }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
  int module_trampoline_target(struct module *mod, unsigned long addr,
                   unsigned long *target)
  {
arch/powerpc/kernel/module_64.c +1 -1
index 794720530442..b13a72665eee 100644
@@ -207,7 +207,7 @@ static unsigned long get_stubs_size(const 
 Elf64_Ehdr *hdr,
          }
      }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
      /* make the trampoline to the ftrace_caller */
      relocs++;

 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS

arch/powerpc/kernel/module_64.c +1 -1
@@ -372,7 +372,7 @@ static bool is_mprofile_ftrace_call(const char *name)
  {
      if (!strcmp("_mcount", name))
          return true;
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
      if (!strcmp("ftrace_caller", name))
          return true;
  #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS

arch/powerpc/kernel/module_64.c int apply_relocate_add(Elf64_Shdr *sechdrs, · line 746

Quoted 8 lines
arch/powerpc/kernel/module_64.c +1 -1
@@ -740,7 +740,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
      return 0;
  }
 
-#ifdef CONFIG_DYNAMIC_FTRACE
+#ifdef CONFIG_FUNCTION_TRACER
  int module_trampoline_target(struct module *mod, unsigned long addr,
                   unsigned long *target)
  {

The below two changes to trace/Makefile and trace/ftrace.c make sense, but I'm not sure the above changes are necessary.

Quoted 5 lines

For generic code (outside arch/powerpc/kernel/trace), I think it is good to retain the actual dependency which is DYNAMIC_FTRACE. In my view, gating some of the above code on FUNCTION_TRACER is also confusing. The primary implication of always selecting DYNAMIC_FTRACE is within the powerpc trace code, and it is good to keep it that way.

Ok I keep only the changes to trace directory for now.

Quoted 48 lines
Quoted 46 lines
arch/powerpc/kernel/trace/Makefile +0 -0
index fc32ec30b297..af8527538fe4 100644
@@ -14,9 +14,7 @@ obj64-$(CONFIG_FUNCTION_TRACER)        += 

ftrace_mprofile.o
 else
 obj64-$(CONFIG_FUNCTION_TRACER)        += ftrace_64_pg.o endif

arch/powerpc/kernel/trace/Makefile +1 -3
-obj-$(CONFIG_FUNCTION_TRACER)        += ftrace_low.o
-obj-$(CONFIG_DYNAMIC_FTRACE)        += ftrace.o
-obj-$(CONFIG_FUNCTION_GRAPH_TRACER)    += ftrace.o
+obj-$(CONFIG_FUNCTION_TRACER)        += ftrace_low.o ftrace.o

 obj-$(CONFIG_TRACING)            += trace_clock.o

 obj-$(CONFIG_PPC64)            += $(obj64-y)

arch/powerpc/kernel/trace/ftrace.c +0 -3
index 2c7e42e439bb..188f59f4ee4a 100644
@@ -28,9 +28,6 @@
  #include <asm/syscall.h>
  #include <asm/inst.h>
 
-
-#ifdef CONFIG_DYNAMIC_FTRACE
-
  /*
   * We generally only have a single long_branch tramp and at most 2 or 

3 plt
  * tramps generated. But, we don't use the plt tramps currently. We also allot

arch/powerpc/kernel/trace/ftrace.c +0 -1
@@ -783,7 +780,6 @@ int __init ftrace_dyn_arch_init(void)
      return 0;
  }
  #endif
-#endif /* CONFIG_DYNAMIC_FTRACE */
 
  #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 
Signature · 1 line
2.35.1

- Naveen

#22 lore raw

PATCH 11/22 powerpc/ftrace: Remove ftrace_plt_tramps[]

In reply to Christophe Leroy

ftrace_plt_tramps table is never filled so it is useless.

Remove it.

---
 arch/powerpc/kernel/trace/ftrace.c | 8 --------
 1 file changed, 8 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +0 -8
index 188f59f4ee4a..b6c5223e26b2 100644
@@ -250,7 +250,6 @@ static int setup_mcount_compiler_tramp(unsigned long tramp)
 	int i;
 	ppc_inst_t op;
 	unsigned long ptr;
-	static unsigned long ftrace_plt_tramps[NUM_FTRACE_TRAMPS];
 
 	/* Is this a known long jump tramp? */
 	for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
@@ -259,13 +258,6 @@ static int setup_mcount_compiler_tramp(unsigned long tramp)
 		else if (ftrace_tramps[i] == tramp)
 			return 0;
 
-	/* Is this a known plt tramp? */
-	for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
-		if (!ftrace_plt_tramps[i])
-			break;
-		else if (ftrace_plt_tramps[i] == tramp)
-			return -1;
-
 	/* New trampoline -- read where this goes */
 	if (copy_inst_from_kernel_nofault(&op, (void *)tramp)) {
 		pr_debug("Fetching opcode failed.\n");
Signature · 1 line
2.35.1
#23 lore raw

PATCH 12/22 powerpc/ftrace: Use BRANCH_SET_LINK instead of value 1

In reply to Christophe Leroy

To make it explicit, use BRANCH_SET_LINK instead of value 1 when calling create_branch().

---
 arch/powerpc/kernel/trace/ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +1 -1
index b6c5223e26b2..fdc0412c1d8a 100644
@@ -45,7 +45,7 @@ ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
 	addr = ppc_function_entry((void *)addr);
 
 	/* if (link) set op to 'bl' else 'b' */
-	create_branch(&op, (u32 *)ip, addr, link ? 1 : 0);
+	create_branch(&op, (u32 *)ip, addr, link ? BRANCH_SET_LINK : 0);
 
 	return op;
 }
Signature · 1 line
2.35.1
#24 lore raw

PATCH 13/22 powerpc/ftrace: Use PPC_RAW_xxx() macros instead of opencoding.

In reply to Christophe Leroy

PPC_RAW_xxx() macros are self explanatory and less error prone than open coding.

Use them in ftrace.c

---
 arch/powerpc/include/asm/ppc-opcode.h |  3 +++
 arch/powerpc/kernel/trace/ftrace.c    | 32 +++++++++------------------
 2 files changed, 14 insertions(+), 21 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +3 -0
index 82f1f0041c6f..281754aca0a3 100644
@@ -294,6 +294,8 @@
 #define PPC_INST_BL			0x48000001
 #define PPC_INST_BRANCH_COND		0x40800000
 
+#define PPC_INST_OFFSET24_MASK		0x03fffffc
+
 /* Prefixes */
 #define PPC_INST_LFS			0xc0000000
 #define PPC_INST_STFS			0xd0000000
@@ -572,6 +574,7 @@
 #define PPC_RAW_EIEIO()			(0x7c0006ac)
 
 #define PPC_RAW_BRANCH(addr)		(PPC_INST_BRANCH | ((addr) & 0x03fffffc))
+#define PPC_RAW_BL(offset)		(0x48000001 | ((offset) & PPC_INST_OFFSET24_MASK))
 
 /* Deal with instructions that older assemblers aren't aware of */
 #define	PPC_BCCTR_FLUSH		stringify_in_c(.long PPC_INST_BCCTR_FLUSH)
arch/powerpc/kernel/trace/ftrace.c +11 -21
index fdc0412c1d8a..afb1d12838c9 100644
@@ -90,19 +90,19 @@ static int test_24bit_addr(unsigned long ip, unsigned long addr)
 
 static int is_bl_op(ppc_inst_t op)
 {
-	return (ppc_inst_val(op) & 0xfc000003) == 0x48000001;
+	return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) == PPC_RAW_BL(0);
 }
 
 static int is_b_op(ppc_inst_t op)
 {
-	return (ppc_inst_val(op) & 0xfc000003) == 0x48000000;
+	return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) == PPC_RAW_BRANCH(0);
 }
 
 static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
 {
 	int offset;
 
-	offset = (ppc_inst_val(op) & 0x03fffffc);
+	offset = (ppc_inst_val(op) & PPC_INST_OFFSET24_MASK);
 	/* make it signed */
 	if (offset & 0x02000000)
 		offset |= 0xfe000000;
@@ -182,7 +182,7 @@ __ftrace_make_nop(struct module *mod,
 	 * Use a b +8 to jump over the load.
 	 */
 
-	pop = ppc_inst(PPC_INST_BRANCH | 8);	/* b +8 */
+	pop = ppc_inst(PPC_RAW_BRANCH(8));	/* b +8 */
 
 	/*
 	 * Check what is in the next instruction. We can see ld r2,40(r1), but
@@ -394,17 +394,8 @@ int ftrace_make_nop(struct module *mod,
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	/*
-	 * We expect to see:
-	 *
-	 * b +8
-	 * ld r2,XX(r1)
-	 *
-	 * The load offset is different depending on the ABI. For simplicity
-	 * just mask it out when doing the compare.
-	 */
-	if (!ppc_inst_equal(op0, ppc_inst(0x48000008)) ||
-	    (ppc_inst_val(op1) & 0xffff0000) != 0xe8410000)
+	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) ||
+	    !ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC)))
 		return 0;
 	return 1;
 }
@@ -412,7 +403,6 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	/* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */
 	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
 		return 0;
 	return 1;
@@ -738,11 +728,11 @@ int __init ftrace_dyn_arch_init(void)
 	int i;
 	unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
 	u32 stub_insns[] = {
-		0xe98d0000 | PACATOC,	/* ld      r12,PACATOC(r13)	*/
-		0x3d8c0000,		/* addis   r12,r12,<high>	*/
-		0x398c0000,		/* addi    r12,r12,<low>	*/
-		0x7d8903a6,		/* mtctr   r12			*/
-		0x4e800420,		/* bctr				*/
+		PPC_RAW_LD(_R12, _R13, PACATOC),
+		PPC_RAW_ADDIS(_R12, _R12, 0),
+		PPC_RAW_ADDIS(_R12, _R12, 0),
+		PPC_RAW_MTCTR(_R12),
+		PPC_RAW_BCTR()
 	};
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	unsigned long addr = ppc_global_function_entry((void *)ftrace_regs_caller);
Signature · 1 line
2.35.1
#25 lore raw

Re: [PATCH v1 13/22] powerpc/ftrace: Use PPC_RAW_xxx() macros instead of opencoding.

Quoted 16 lines · Christophe Leroy

PPC_RAW_xxx() macros are self explanatory and less error prone than open coding.

Use them in ftrace.c

---
 arch/powerpc/include/asm/ppc-opcode.h |  3 +++
 arch/powerpc/kernel/trace/ftrace.c    | 32 +++++++++------------------
 2 files changed, 14 insertions(+), 21 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +0 -0
index 82f1f0041c6f..281754aca0a3 100644

arch/powerpc/include/asm/ppc-opcode.h line 297

Quoted 4 lines
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -294,6 +294,8 @@
 #define PPC_INST_BL			0x48000001
 #define PPC_INST_BRANCH_COND		0x40800000
 
+#define PPC_INST_OFFSET24_MASK		0x03fffffc

This corresponds to the LI field, per the ISA. See section 8.1.2/1.7: 'Instruction Fields'. Would it be better to name it PPC_INST_LI_MASK?

Quoted 55 lines
arch/powerpc/include/asm/ppc-opcode.h +2 -0
+
 /* Prefixes */
 #define PPC_INST_LFS			0xc0000000
 #define PPC_INST_STFS			0xd0000000
@@ -572,6 +574,7 @@
 #define PPC_RAW_EIEIO()			(0x7c0006ac)
 
 #define PPC_RAW_BRANCH(addr)		(PPC_INST_BRANCH | ((addr) & 0x03fffffc))
+#define PPC_RAW_BL(offset)		(0x48000001 | ((offset) & PPC_INST_OFFSET24_MASK))
 
 /* Deal with instructions that older assemblers aren't aware of */
 #define	PPC_BCCTR_FLUSH		stringify_in_c(.long PPC_INST_BCCTR_FLUSH)
arch/powerpc/kernel/trace/ftrace.c +4 -7
index fdc0412c1d8a..afb1d12838c9 100644
@@ -90,19 +90,19 @@ static int test_24bit_addr(unsigned long ip, unsigned long addr)
 
 static int is_bl_op(ppc_inst_t op)
 {
-	return (ppc_inst_val(op) & 0xfc000003) == 0x48000001;
+	return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) == PPC_RAW_BL(0);
 }
 
 static int is_b_op(ppc_inst_t op)
 {
-	return (ppc_inst_val(op) & 0xfc000003) == 0x48000000;
+	return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) == PPC_RAW_BRANCH(0);
 }
 
 static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
 {
 	int offset;
 
-	offset = (ppc_inst_val(op) & 0x03fffffc);
+	offset = (ppc_inst_val(op) & PPC_INST_OFFSET24_MASK);
 	/* make it signed */
 	if (offset & 0x02000000)
 		offset |= 0xfe000000;
@@ -182,7 +182,7 @@ __ftrace_make_nop(struct module *mod,
 	 * Use a b +8 to jump over the load.
 	 */
 
-	pop = ppc_inst(PPC_INST_BRANCH | 8);	/* b +8 */
+	pop = ppc_inst(PPC_RAW_BRANCH(8));	/* b +8 */
 
 	/*
 	 * Check what is in the next instruction. We can see ld r2,40(r1), but
@@ -394,17 +394,8 @@ int ftrace_make_nop(struct module *mod,
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	/*
-	 * We expect to see:
-	 *

arch/powerpc/kernel/trace/ftrace.c int ftrace_make_nop(struct module *mod, · line 398

Quoted 10 lines
arch/powerpc/kernel/trace/ftrace.c +2 -8
-	 * b +8
-	 * ld r2,XX(r1)
-	 *
-	 * The load offset is different depending on the ABI. For simplicity
-	 * just mask it out when doing the compare.
-	 */
-	if (!ppc_inst_equal(op0, ppc_inst(0x48000008)) ||
-	    (ppc_inst_val(op1) & 0xffff0000) != 0xe8410000)
+	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) ||
+	    !ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC)))

It would be good to move PPC_INST_LD_TOC to ppc-opcode.h

Quoted 13 lines
arch/powerpc/kernel/trace/ftrace.c +0 -1
 		return 0;
 	return 1;
 }
@@ -412,7 +403,6 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	/* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */
 	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
 		return 0;
 	return 1;
@@ -738,11 +728,11 @@ int __init ftrace_dyn_arch_init(void)
 	int i;

arch/powerpc/kernel/trace/ftrace.c int __init ftrace_dyn_arch_init(void) · line 733

Quoted 10 lines
arch/powerpc/kernel/trace/ftrace.c +3 -5
 	unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
 	u32 stub_insns[] = {
-		0xe98d0000 | PACATOC,	/* ld      r12,PACATOC(r13)	*/
-		0x3d8c0000,		/* addis   r12,r12,<high>	*/
-		0x398c0000,		/* addi    r12,r12,<low>	*/
-		0x7d8903a6,		/* mtctr   r12			*/
-		0x4e800420,		/* bctr				*/
+		PPC_RAW_LD(_R12, _R13, PACATOC),
+		PPC_RAW_ADDIS(_R12, _R12, 0),
+		PPC_RAW_ADDIS(_R12, _R12, 0),

This should be PPC_RAW_ADDI.

- Naveen

#26 lore raw

Re: [PATCH v1 13/22] powerpc/ftrace: Use PPC_RAW_xxx() macros instead of opencoding.

Le 18/04/2022 à 09:38, Naveen N. Rao a écrit :

Quoted 21 lines
Quoted 20 lines · Christophe Leroy

PPC_RAW_xxx() macros are self explanatory and less error prone than open coding.

Use them in ftrace.c

---
 arch/powerpc/include/asm/ppc-opcode.h |  3 +++ arch/powerpc/kernel/trace/ftrace.c    | 32 +++++++++------------------

 2 files changed, 14 insertions(+), 21 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +0 -0
index 82f1f0041c6f..281754aca0a3 100644
@@ -294,6 +294,8 @@
  #define PPC_INST_BL            0x48000001
  #define PPC_INST_BRANCH_COND        0x40800000
 
Quoted 4 lines

arch/powerpc/include/asm/ppc-opcode.h line 297

Quoted 1 line
arch/powerpc/include/asm/ppc-opcode.h +1 -0
+#define PPC_INST_OFFSET24_MASK        0x03fffffc

This corresponds to the LI field, per the ISA. See section 8.1.2/1.7:
'Instruction Fields'. Would it be better to name it PPC_INST_LI_MASK?

Isn't there a risk of confusing with the 'li' instruction ? Like we could have PPC_INST_LI just like we have PPC_INST_ADD ?

Quoted 68 lines
Quoted 68 lines
arch/powerpc/include/asm/ppc-opcode.h +2 -0
+
  /* Prefixes */
  #define PPC_INST_LFS            0xc0000000
  #define PPC_INST_STFS            0xd0000000
@@ -572,6 +574,7 @@
  #define PPC_RAW_EIEIO()            (0x7c0006ac)
 
  #define PPC_RAW_BRANCH(addr)        (PPC_INST_BRANCH | ((addr) & 
 0x03fffffc))
+#define PPC_RAW_BL(offset)        (0x48000001 | ((offset) & 
 PPC_INST_OFFSET24_MASK))
 

 /* Deal with instructions that older assemblers aren't aware of */ #define    PPC_BCCTR_FLUSH        stringify_in_c(.long PPC_INST_BCCTR_FLUSH)

arch/powerpc/kernel/trace/ftrace.c +3 -3
index fdc0412c1d8a..afb1d12838c9 100644
@@ -90,19 +90,19 @@ static int test_24bit_addr(unsigned long ip, 
 unsigned long addr)
 
  static int is_bl_op(ppc_inst_t op)
  {
-    return (ppc_inst_val(op) & 0xfc000003) == 0x48000001;
+    return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) == 
 PPC_RAW_BL(0);
  }
 
  static int is_b_op(ppc_inst_t op)
  {
-    return (ppc_inst_val(op) & 0xfc000003) == 0x48000000;
+    return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) == 
 PPC_RAW_BRANCH(0);
  }
 
  static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
  {
      int offset;
 
-    offset = (ppc_inst_val(op) & 0x03fffffc);
+    offset = (ppc_inst_val(op) & PPC_INST_OFFSET24_MASK);

     /* make it signed */
     if (offset & 0x02000000)
         offset |= 0xfe000000;

arch/powerpc/kernel/trace/ftrace.c +1 -1
@@ -182,7 +182,7 @@ __ftrace_make_nop(struct module *mod,
       * Use a b +8 to jump over the load.
       */
 
-    pop = ppc_inst(PPC_INST_BRANCH | 8);    /* b +8 */
+    pop = ppc_inst(PPC_RAW_BRANCH(8));    /* b +8 */
 
      /*
       * Check what is in the next instruction. We can see ld 

r2,40(r1), but

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -394,17 +394,8 @@ int ftrace_make_nop(struct module *mod,

 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1) {
-    /*
-     * We expect to see:
-     *
-     * b +8
-     * ld r2,XX(r1)
-     *
-     * The load offset is different depending on the ABI. For simplicity

Quoted 8 lines
Quoted 6 lines

-     * just mask it out when doing the compare.
-     */
-    if (!ppc_inst_equal(op0, ppc_inst(0x48000008)) || -        (ppc_inst_val(op1) & 0xffff0000) != 0xe8410000) +    if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) || +        !ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC)))

It would be good to move PPC_INST_LD_TOC to ppc-opcode.h

It's not really just an instruction, it's closely linked to the ABI, so does it really belong to ppc-opcode.h ? Maybe it could be better to have it in ppc_asm.h instead, which already contains ABI related definitions ?

If we move it into ppc-opcode.h, then we also have to move R2_STACK_OFFSET. Or should we use STK_GOT defined in ppc_asm.h and drop R2_STACK_OFFSET ?

Quoted 19 lines
Quoted 19 lines

         return 0;
     return 1;
 }

arch/powerpc/kernel/trace/ftrace.c +0 -0
@@ -412,7 +403,6 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, 
 ppc_inst_t op1)

 static int
 expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1) {
-    /* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */ if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP()))) return 0;
     return 1;

arch/powerpc/kernel/trace/ftrace.c +0 -2
@@ -738,11 +728,11 @@ int __init ftrace_dyn_arch_init(void)
      int i;
      unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
      u32 stub_insns[] = {
-        0xe98d0000 | PACATOC,    /* ld      r12,PACATOC(r13)    */
-        0x3d8c0000,        /* addis   r12,r12,<high>    */
Quoted 9 lines

arch/powerpc/kernel/trace/ftrace.c int __init ftrace_dyn_arch_init(void) · line 733

Quoted 6 lines
arch/powerpc/kernel/trace/ftrace.c +3 -3
-        0x398c0000,        /* addi    r12,r12,<low>    */
-        0x7d8903a6,        /* mtctr   r12            */
-        0x4e800420,        /* bctr                */
+        PPC_RAW_LD(_R12, _R13, PACATOC),
+        PPC_RAW_ADDIS(_R12, _R12, 0),
+        PPC_RAW_ADDIS(_R12, _R12, 0),

This should be PPC_RAW_ADDI.

Oops.

Christophe

#27 lore raw

Re: [PATCH v1 13/22] powerpc/ftrace: Use PPC_RAW_xxx() macros instead of opencoding.

Le 04/05/2022 à 14:39, Christophe Leroy a écrit :

Quoted 24 lines

Le 18/04/2022 à 09:38, Naveen N. Rao a écrit :

Quoted 21 lines
Quoted 20 lines · Christophe Leroy

PPC_RAW_xxx() macros are self explanatory and less error prone than open coding.

Use them in ftrace.c

---
  arch/powerpc/include/asm/ppc-opcode.h |  3 +++
  arch/powerpc/kernel/trace/ftrace.c    | 32 +++++++++------------------
  2 files changed, 14 insertions(+), 21 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +0 -0
index 82f1f0041c6f..281754aca0a3 100644
@@ -294,6 +294,8 @@
  #define PPC_INST_BL            0x48000001
  #define PPC_INST_BRANCH_COND        0x40800000
 
Quoted 7 lines
Quoted 4 lines

arch/powerpc/include/asm/ppc-opcode.h line 297

Quoted 1 line
arch/powerpc/include/asm/ppc-opcode.h +1 -0
+#define PPC_INST_OFFSET24_MASK        0x03fffffc

This corresponds to the LI field, per the ISA. See section 8.1.2/1.7:
'Instruction Fields'. Would it be better to name it PPC_INST_LI_MASK?

Isn't there a risk of confusing with the 'li' instruction ? Like we could have PPC_INST_LI just like we have PPC_INST_ADD ?

I called it PPC_LI() and PPC_LI_MASK, similar to PPC_LO, PPC_HI etc ...

Quoted 80 lines
Quoted 71 lines
Quoted 71 lines
arch/powerpc/include/asm/ppc-opcode.h +2 -0
+
  /* Prefixes */
  #define PPC_INST_LFS            0xc0000000
  #define PPC_INST_STFS            0xd0000000
@@ -572,6 +574,7 @@
  #define PPC_RAW_EIEIO()            (0x7c0006ac)
 
  #define PPC_RAW_BRANCH(addr)        (PPC_INST_BRANCH | ((addr) &
 0x03fffffc))
+#define PPC_RAW_BL(offset)        (0x48000001 | ((offset) &
 PPC_INST_OFFSET24_MASK))
 

 /* Deal with instructions that older assemblers aren't aware of */ #define    PPC_BCCTR_FLUSH        stringify_in_c(.long
PPC_INST_BCCTR_FLUSH)

arch/powerpc/kernel/trace/ftrace.c +3 -3
index fdc0412c1d8a..afb1d12838c9 100644
@@ -90,19 +90,19 @@ static int test_24bit_addr(unsigned long ip,
 unsigned long addr)
 
  static int is_bl_op(ppc_inst_t op)
  {
-    return (ppc_inst_val(op) & 0xfc000003) == 0x48000001;
+    return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) ==
 PPC_RAW_BL(0);
  }
 
  static int is_b_op(ppc_inst_t op)
  {
-    return (ppc_inst_val(op) & 0xfc000003) == 0x48000000;
+    return (ppc_inst_val(op) & ~PPC_INST_OFFSET24_MASK) ==
 PPC_RAW_BRANCH(0);
  }
 
  static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
  {
      int offset;
 
-    offset = (ppc_inst_val(op) & 0x03fffffc);
+    offset = (ppc_inst_val(op) & PPC_INST_OFFSET24_MASK);

     /* make it signed */
     if (offset & 0x02000000)
         offset |= 0xfe000000;

arch/powerpc/kernel/trace/ftrace.c +1 -1
@@ -182,7 +182,7 @@ __ftrace_make_nop(struct module *mod,
       * Use a b +8 to jump over the load.
       */
 
-    pop = ppc_inst(PPC_INST_BRANCH | 8);    /* b +8 */
+    pop = ppc_inst(PPC_RAW_BRANCH(8));    /* b +8 */
 
      /*
       * Check what is in the next instruction. We can see ld

r2,40(r1), but

arch/powerpc/kernel/trace/ftrace.c +0 -7
@@ -394,17 +394,8 @@ int ftrace_make_nop(struct module *mod,
  static int
  expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
  {
-    /*
-     * We expect to see:
-     *
-     * b +8
-     * ld r2,XX(r1)
-     *
-     * The load offset is different depending on the ABI. For simplicity
Quoted 8 lines

arch/powerpc/kernel/trace/ftrace.c int ftrace_make_nop(struct module *mod, · line 398

Quoted 6 lines
arch/powerpc/kernel/trace/ftrace.c +2 -4
-     * just mask it out when doing the compare.
-     */
-    if (!ppc_inst_equal(op0, ppc_inst(0x48000008)) ||
-        (ppc_inst_val(op1) & 0xffff0000) != 0xe8410000)
+    if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) ||
+        !ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC)))

It would be good to move PPC_INST_LD_TOC to ppc-opcode.h

Quoted 7 lines

It's not really just an instruction, it's closely linked to the ABI, so does it really belong to ppc-opcode.h ? Maybe it could be better to have it in ppc_asm.h instead, which already contains ABI related definitions ?

If we move it into ppc-opcode.h, then we also have to move R2_STACK_OFFSET. Or should we use STK_GOT defined in ppc_asm.h and drop R2_STACK_OFFSET ?

Looked at it in more details, looks like STK_GOT is an assembly only symbol, and ppc_asm.h is dedicated to ASM allthough it has recently leaked a bit into C.

So I propose to leave it as is and do the change in a followup patch.

Quoted 33 lines
Quoted 20 lines
Quoted 20 lines
arch/powerpc/kernel/trace/ftrace.c +0 -1
          return 0;
      return 1;
  }
@@ -412,7 +403,6 @@ expected_nop_sequence(void *ip, ppc_inst_t op0,
 ppc_inst_t op1)
  static int
  expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
  {
-    /* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */
      if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
          return 0;

     return 1;

arch/powerpc/kernel/trace/ftrace.c +0 -2
@@ -738,11 +728,11 @@ int __init ftrace_dyn_arch_init(void)
      int i;
      unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
      u32 stub_insns[] = {
-        0xe98d0000 | PACATOC,    /* ld      r12,PACATOC(r13)    */
-        0x3d8c0000,        /* addis   r12,r12,<high>    */
Quoted 9 lines

arch/powerpc/kernel/trace/ftrace.c int __init ftrace_dyn_arch_init(void) · line 733

Quoted 6 lines
arch/powerpc/kernel/trace/ftrace.c +3 -3
-        0x398c0000,        /* addi    r12,r12,<low>    */
-        0x7d8903a6,        /* mtctr   r12            */
-        0x4e800420,        /* bctr                */
+        PPC_RAW_LD(_R12, _R13, PACATOC),
+        PPC_RAW_ADDIS(_R12, _R12, 0),
+        PPC_RAW_ADDIS(_R12, _R12, 0),

This should be PPC_RAW_ADDI.

Oops.

Christophe

#28 lore raw

PATCH 14/22 powerpc/ftrace: Use size macro instead of opencoding

In reply to Christophe Leroy

0x80000000 is SZ_2G. Use it.

---
 arch/powerpc/kernel/trace/ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace.c +1 -1
index afb1d12838c9..15e0746806f8 100644
@@ -741,7 +741,7 @@ int __init ftrace_dyn_arch_init(void)
 #endif
 	long reladdr = addr - kernel_toc_addr();
 
-	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
+	if (reladdr >= SZ_2G || reladdr < -SZ_2G) {
 		pr_err("Address of %ps out of range of kernel_toc.\n",
 				(void *)addr);
 		return -1;
Signature · 1 line
2.35.1
#29 lore raw

PATCH 15/22 powerpc/ftrace: Simplify expected_nop_sequence()

In reply to Christophe Leroy

Avoid ifdefs around expected_nop_sequence().

While at it make it a bool.

---
 arch/powerpc/kernel/trace/ftrace.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
arch/powerpc/kernel/trace/ftrace.c +6 -16
index 15e0746806f8..ffedf8c82ea8 100644
@@ -390,24 +390,14 @@ int ftrace_make_nop(struct module *mod,
  * They should effectively be a NOP, and follow formal constraints,
  * depending on the ABI. Return false if they don't.
  */
-#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
-static int
-expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
-{
-	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) ||
-	    !ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC)))
-		return 0;
-	return 1;
-}
-#else
-static int
-expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
+static bool expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
 {
-	if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
-		return 0;
-	return 1;
+	if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
+		return ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP()));
+	else
+		return ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) &&
+		       ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC));
 }
-#endif
 
 static int
 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
Signature · 1 line
2.35.1
#30 lore raw

PATCH 16/22 powerpc/ftrace: Minimise number of #ifdefs

In reply to Christophe Leroy

A lot of #ifdefs can be replaced by IS_ENABLED()

Do so.

This requires to have kernel_toc_addr() defined at all time and PPC_INST_LD_TOC as well.

---
 arch/powerpc/include/asm/code-patching.h |   2 -
 arch/powerpc/include/asm/module.h        |   2 -
 arch/powerpc/include/asm/sections.h      |  24 +--
 arch/powerpc/kernel/trace/ftrace.c       | 201 ++++++++++++-----------
 4 files changed, 113 insertions(+), 116 deletions(-)
arch/powerpc/include/asm/code-patching.h +0 -2
index 4260e89f62b1..071fcbec31c5 100644
@@ -217,7 +217,6 @@ static inline unsigned long ppc_kallsyms_lookup_name(const char *name)
 	return addr;
 }
 
-#ifdef CONFIG_PPC64
 /*
  * Some instruction encodings commonly used in dynamic ftracing
  * and function live patching.
@@ -234,6 +233,5 @@ static inline unsigned long ppc_kallsyms_lookup_name(const char *name)
 
 /* usually preceded by a mflr r0 */
 #define PPC_INST_STD_LR		PPC_RAW_STD(_R0, _R1, PPC_LR_STKOFF)
-#endif /* CONFIG_PPC64 */
 
 #endif /* _ASM_POWERPC_CODE_PATCHING_H */
arch/powerpc/include/asm/module.h +0 -2
index e6f5963fd96e..700d7ecd9012 100644
@@ -41,9 +41,7 @@ struct mod_arch_specific {
 
 #ifdef CONFIG_FUNCTION_TRACER
 	unsigned long tramp;
-#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	unsigned long tramp_regs;
-#endif
 #endif
 
 	/* List of BUG addresses, source line numbers and filenames */
arch/powerpc/include/asm/sections.h +12 -12
index 8be2c491c733..6980eaeb16fe 100644
@@ -29,18 +29,6 @@ extern char start_virt_trampolines[];
 extern char end_virt_trampolines[];
 #endif
 
-/*
- * This assumes the kernel is never compiled -mcmodel=small or
- * the total .toc is always less than 64k.
- */
-static inline unsigned long kernel_toc_addr(void)
-{
-	unsigned long toc_ptr;
-
-	asm volatile("mr %0, 2" : "=r" (toc_ptr));
-	return toc_ptr;
-}
-
 static inline int overlaps_interrupt_vector_text(unsigned long start,
 							unsigned long end)
 {
@@ -60,5 +48,17 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end)
 
 #endif
 
+/*
+ * This assumes the kernel is never compiled -mcmodel=small or
+ * the total .toc is always less than 64k.
+ */
+static inline unsigned long kernel_toc_addr(void)
+{
+	unsigned long toc_ptr;
+
+	asm volatile("mr %0, 2" : "=r" (toc_ptr));
+	return toc_ptr;
+}
+
 #endif /* __KERNEL__ */
 #endif	/* _ASM_POWERPC_SECTIONS_H */
arch/powerpc/kernel/trace/ftrace.c +101 -100

335 lines not on this page · raw message

Signature · 1 line
2.35.1
#31 lore raw

Re: [PATCH v1 16/22] powerpc/ftrace: Minimise number of #ifdefs

Quoted 94 lines · Christophe Leroy

A lot of #ifdefs can be replaced by IS_ENABLED()

Do so.

This requires to have kernel_toc_addr() defined at all time and PPC_INST_LD_TOC as well.

---
 arch/powerpc/include/asm/code-patching.h |   2 -
 arch/powerpc/include/asm/module.h        |   2 -
 arch/powerpc/include/asm/sections.h      |  24 +--
 arch/powerpc/kernel/trace/ftrace.c       | 201 ++++++++++++-----------
 4 files changed, 113 insertions(+), 116 deletions(-)
arch/powerpc/include/asm/code-patching.h +0 -2
index 4260e89f62b1..071fcbec31c5 100644
@@ -217,7 +217,6 @@ static inline unsigned long ppc_kallsyms_lookup_name(const char *name)
 	return addr;
 }
 
-#ifdef CONFIG_PPC64
 /*
  * Some instruction encodings commonly used in dynamic ftracing
  * and function live patching.
@@ -234,6 +233,5 @@ static inline unsigned long ppc_kallsyms_lookup_name(const char *name)
 
 /* usually preceded by a mflr r0 */
 #define PPC_INST_STD_LR		PPC_RAW_STD(_R0, _R1, PPC_LR_STKOFF)
-#endif /* CONFIG_PPC64 */
 
 #endif /* _ASM_POWERPC_CODE_PATCHING_H */
arch/powerpc/include/asm/module.h +0 -2
index e6f5963fd96e..700d7ecd9012 100644
@@ -41,9 +41,7 @@ struct mod_arch_specific {
 
 #ifdef CONFIG_FUNCTION_TRACER
 	unsigned long tramp;
-#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
 	unsigned long tramp_regs;
-#endif
 #endif
 
 	/* List of BUG addresses, source line numbers and filenames */
arch/powerpc/include/asm/sections.h +12 -12
index 8be2c491c733..6980eaeb16fe 100644
@@ -29,18 +29,6 @@ extern char start_virt_trampolines[];
 extern char end_virt_trampolines[];
 #endif
 
-/*
- * This assumes the kernel is never compiled -mcmodel=small or
- * the total .toc is always less than 64k.
- */
-static inline unsigned long kernel_toc_addr(void)
-{
-	unsigned long toc_ptr;
-
-	asm volatile("mr %0, 2" : "=r" (toc_ptr));
-	return toc_ptr;
-}
-
 static inline int overlaps_interrupt_vector_text(unsigned long start,
 							unsigned long end)
 {
@@ -60,5 +48,17 @@ static inline int overlaps_kernel_text(unsigned long start, unsigned long end)
 
 #endif
 
+/*
+ * This assumes the kernel is never compiled -mcmodel=small or
+ * the total .toc is always less than 64k.
+ */
+static inline unsigned long kernel_toc_addr(void)
+{
+	unsigned long toc_ptr;
+
+	asm volatile("mr %0, 2" : "=r" (toc_ptr));
+	return toc_ptr;
+}
+
 #endif /* __KERNEL__ */
 #endif	/* _ASM_POWERPC_SECTIONS_H */
arch/powerpc/kernel/trace/ftrace.c +0 -0
index ffedf8c82ea8..4dd30e396026 100644

arch/powerpc/kernel/trace/ftrace.c __ftrace_make_nop(struct module *mod, · line 155

Quoted 8 lines
arch/powerpc/kernel/trace/ftrace.c +3 -2
@@ -150,55 +150,55 @@ __ftrace_make_nop(struct module *mod,
 		return -EINVAL;
 	}
 
-	/* When using -mkernel_profile or PPC32 there is no load to jump over */
-	pop = ppc_inst(PPC_RAW_NOP());
+	if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) {
+		/* When using -mkernel_profile or PPC32 there is no load to jump over */
+		pop = ppc_inst(PPC_RAW_NOP());

Why move this inside the if() statement? You can keep this out and drop the else clause.

- Naveen

#32 lore raw

Re: [PATCH v1 16/22] powerpc/ftrace: Minimise number of #ifdefs

Le 18/04/2022 à 09:59, Naveen N. Rao a écrit :

Quoted 105 lines
Quoted 104 lines · Christophe Leroy

A lot of #ifdefs can be replaced by IS_ENABLED()

Do so.

This requires to have kernel_toc_addr() defined at all time and PPC_INST_LD_TOC as well.

---
 arch/powerpc/include/asm/code-patching.h |   2 - arch/powerpc/include/asm/module.h        |   2 - arch/powerpc/include/asm/sections.h      |  24 +-- arch/powerpc/kernel/trace/ftrace.c       | 201 ++++++++++++-----------

 4 files changed, 113 insertions(+), 116 deletions(-)
arch/powerpc/include/asm/code-patching.h +0 -1
index 4260e89f62b1..071fcbec31c5 100644
@@ -217,7 +217,6 @@ static inline unsigned long 
 ppc_kallsyms_lookup_name(const char *name)
      return addr;
  }
 
-#ifdef CONFIG_PPC64
  /*
   * Some instruction encodings commonly used in dynamic ftracing

  * and function live patching.

arch/powerpc/include/asm/code-patching.h +0 -1
@@ -234,6 +233,5 @@ static inline unsigned long 
 ppc_kallsyms_lookup_name(const char *name)
 
  /* usually preceded by a mflr r0 */
  #define PPC_INST_STD_LR        PPC_RAW_STD(_R0, _R1, PPC_LR_STKOFF)
-#endif /* CONFIG_PPC64 */
 

 #endif /* _ASM_POWERPC_CODE_PATCHING_H */

arch/powerpc/include/asm/module.h +0 -2
index e6f5963fd96e..700d7ecd9012 100644
@@ -41,9 +41,7 @@ struct mod_arch_specific {
 
  #ifdef CONFIG_FUNCTION_TRACER
      unsigned long tramp;
-#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
      unsigned long tramp_regs;
-#endif
  #endif
 
      /* List of BUG addresses, source line numbers and filenames */
arch/powerpc/include/asm/sections.h +12 -12
index 8be2c491c733..6980eaeb16fe 100644
@@ -29,18 +29,6 @@ extern char start_virt_trampolines[];
  extern char end_virt_trampolines[];
  #endif
 
-/*
- * This assumes the kernel is never compiled -mcmodel=small or
- * the total .toc is always less than 64k.
- */
-static inline unsigned long kernel_toc_addr(void)
-{
-    unsigned long toc_ptr;
-
-    asm volatile("mr %0, 2" : "=r" (toc_ptr));
-    return toc_ptr;
-}
-
  static inline int overlaps_interrupt_vector_text(unsigned long start,
                              unsigned long end)
  {
@@ -60,5 +48,17 @@ static inline int overlaps_kernel_text(unsigned 
 long start, unsigned long end)
 
  #endif
 
+/*
+ * This assumes the kernel is never compiled -mcmodel=small or
+ * the total .toc is always less than 64k.
+ */
+static inline unsigned long kernel_toc_addr(void)
+{
+    unsigned long toc_ptr;
+
+    asm volatile("mr %0, 2" : "=r" (toc_ptr));
+    return toc_ptr;
+}
+
  #endif /* __KERNEL__ */

 #endif    /* _ASM_POWERPC_SECTIONS_H */

arch/powerpc/kernel/trace/ftrace.c +0 -0
index ffedf8c82ea8..4dd30e396026 100644
@@ -150,55 +150,55 @@ __ftrace_make_nop(struct module *mod,
          return -EINVAL;
      }
 
Quoted 10 lines
Quoted 7 lines
arch/powerpc/kernel/trace/ftrace.c +0 -1
-    /* When using -mkernel_profile or PPC32 there is no load to jump 

over */
-    pop = ppc_inst(PPC_RAW_NOP());
+    if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) {
+        /* When using -mkernel_profile or PPC32 there is no load to jump over */
+        pop = ppc_inst(PPC_RAW_NOP());

Why move this inside the if() statement? You can keep this out and drop the else clause.

I find it less error prone that way.

Putting a bad value and then replace it later with the good one can be misleading, and if some day someone removes that second set by error, it will go unnoticed. When you set it only once, GCC will complain in case that setting disappear by error.

Christophe

#33 lore raw

PATCH 17/22 powerpc/inst: Add __copy_inst_from_kernel_nofault()

In reply to Christophe Leroy

On the same model as get_user() versus __get_user(), introduce __copy_inst_from_kernel_nofault() which doesn't check address.

To be used by callers that have already checked that the adress is a kernel address.

---
 arch/powerpc/include/asm/inst.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/inst.h +9 -4
index 80b6d74146c6..b49aae9f6f27 100644
@@ -158,13 +158,10 @@ static inline char *__ppc_inst_as_str(char str[PPC_INST_STR_LEN], ppc_inst_t x)
 	__str;				\
 })
 
-static inline int copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src)
+static inline int __copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src)
 {
 	unsigned int val, suffix;
 
-	if (unlikely(!is_kernel_addr((unsigned long)src)))
-		return -ERANGE;
-
 /* See https://github.com/ClangBuiltLinux/linux/issues/1521 */
 #if defined(CONFIG_CC_IS_CLANG) && CONFIG_CLANG_VERSION < 140000
 	val = suffix = 0;
@@ -181,4 +178,12 @@ static inline int copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src)
 	return -EFAULT;
 }
 
+static inline int copy_inst_from_kernel_nofault(ppc_inst_t *inst, u32 *src)
+{
+	if (unlikely(!is_kernel_addr((unsigned long)src)))
+		return -ERANGE;
+
+	return __copy_inst_from_kernel_nofault(inst, src);
+}
+
 #endif /* _ASM_POWERPC_INST_H */
Signature · 1 line
2.35.1
#34 lore raw

PATCH 18/22 powerpc/ftrace: Don't use copy_from_kernel_nofault() in module_trampoline_target()

In reply to Christophe Leroy

module_trampoline_target() is quite a hot path used when activating/deactivating function tracer.

Avoid the heavy copy_from_kernel_nofault() by doing four calls to copy_inst_from_kernel_nofault().

Use __copy_inst_from_kernel_nofault() for the 3 last calls. First call is done to copy_from_kernel_nofault() to check address is within kernel space. No risk to wrap out the top of kernel space because the last page is never mapped so if address is in last page the first copy will fails and the other ones will never be performed.

And also make it notrace just like all functions that call it.

---
 arch/powerpc/kernel/module_32.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/powerpc/kernel/module_32.c +9 -3
index 2aa368ce21c9..1282cfd672f2 100644
@@ -289,13 +289,19 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
 }
 
 #ifdef CONFIG_FUNCTION_TRACER
-int module_trampoline_target(struct module *mod, unsigned long addr,
-			     unsigned long *target)
+notrace int module_trampoline_target(struct module *mod, unsigned long addr,
+				     unsigned long *target)
 {
 	unsigned int jmp[4];
 
 	/* Find where the trampoline jumps to */
-	if (copy_from_kernel_nofault(jmp, (void *)addr, sizeof(jmp)))
+	if (copy_inst_from_kernel_nofault(jmp, (void *)addr))
+		return -EFAULT;
+	if (__copy_inst_from_kernel_nofault(jmp + 1, (void *)addr + 4))
+		return -EFAULT;
+	if (__copy_inst_from_kernel_nofault(jmp + 2, (void *)addr + 8))
+		return -EFAULT;
+	if (__copy_inst_from_kernel_nofault(jmp + 3, (void *)addr + 12))
 		return -EFAULT;
 
 	/* verify that this is what we expect it to be */
Signature · 1 line
2.35.1
#35 lore raw

PATCH 19/22 powerpc/inst: Remove PPC_INST_BRANCH

In reply to Christophe Leroy

Convert last users of PPC_INST_BRANCH to PPC_RAW_BRANCH()

And remove PPC_INST_BRANCH.

---
 arch/powerpc/include/asm/ppc-opcode.h | 3 +--
 arch/powerpc/lib/feature-fixups.c     | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +1 -2
index 281754aca0a3..ada8fe17b199 100644
@@ -290,7 +290,6 @@
 #define PPC_INST_ADDIS			0x3c000000
 #define PPC_INST_ADD			0x7c000214
 #define PPC_INST_DIVD			0x7c0003d2
-#define PPC_INST_BRANCH			0x48000000
 #define PPC_INST_BL			0x48000001
 #define PPC_INST_BRANCH_COND		0x40800000
 
@@ -573,7 +572,7 @@
 #define PPC_RAW_MTSPR(spr, d)		(0x7c0003a6 | ___PPC_RS(d) | __PPC_SPR(spr))
 #define PPC_RAW_EIEIO()			(0x7c0006ac)
 
-#define PPC_RAW_BRANCH(addr)		(PPC_INST_BRANCH | ((addr) & 0x03fffffc))
+#define PPC_RAW_BRANCH(offset)		(0x48000000 | ((offset) & PPC_INST_OFFSET24_MASK))
 #define PPC_RAW_BL(offset)		(0x48000001 | ((offset) & PPC_INST_OFFSET24_MASK))
 
 /* Deal with instructions that older assemblers aren't aware of */
arch/powerpc/lib/feature-fixups.c +1 -1
index 343a78826035..993d3f31832a 100644
@@ -451,7 +451,7 @@ static int __do_rfi_flush_fixups(void *data)
 
 	if (types & L1D_FLUSH_FALLBACK)
 		/* b .+16 to fallback flush */
-		instrs[0] = PPC_INST_BRANCH | 16;
+		instrs[0] = PPC_RAW_BRANCH(16);
 
 	i = 0;
 	if (types & L1D_FLUSH_ORI) {
Signature · 1 line
2.35.1
#36 lore raw

PATCH 20/22 powerpc/modules: Use PPC_INST_BRANCH_MASK instead of opencoding

In reply to Christophe Leroy

Use PPC_INST_BRANCH_MASK instead of opencoding.

---
 arch/powerpc/kernel/module_32.c | 13 ++++++-------
 arch/powerpc/kernel/module_64.c |  4 ++--
 2 files changed, 8 insertions(+), 9 deletions(-)
arch/powerpc/kernel/module_32.c +6 -7
index 1282cfd672f2..344941855e9e 100644
@@ -256,9 +256,8 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
 			       value, (uint32_t)location);
 			pr_debug("Location before: %08X.\n",
 			       *(uint32_t *)location);
-			value = (*(uint32_t *)location & ~0x03fffffc)
-				| ((value - (uint32_t)location)
-				   & 0x03fffffc);
+			value = (*(uint32_t *)location & ~PPC_INST_OFFSET24_MASK) |
+				((value - (uint32_t)location) & PPC_INST_OFFSET24_MASK);
 
 			if (patch_instruction(location, ppc_inst(value)))
 				return -EFAULT;
@@ -266,10 +265,10 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,
 			pr_debug("Location after: %08X.\n",
 			       *(uint32_t *)location);
 			pr_debug("ie. jump to %08X+%08X = %08X\n",
-			       *(uint32_t *)location & 0x03fffffc,
-			       (uint32_t)location,
-			       (*(uint32_t *)location & 0x03fffffc)
-			       + (uint32_t)location);
+				 *(uint32_t *)location & PPC_INST_OFFSET24_MASK,
+				 (uint32_t)location,
+				 (*(uint32_t *)location & PPC_INST_OFFSET24_MASK) +
+				 (uint32_t)location);
 			break;
 
 		case R_PPC_REL32:
arch/powerpc/kernel/module_64.c +2 -2
index b13a72665eee..8f70a04aac6c 100644
@@ -653,8 +653,8 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 			}
 
 			/* Only replace bits 2 through 26 */
-			value = (*(uint32_t *)location & ~0x03fffffc)
-				| (value & 0x03fffffc);
+			value = (*(uint32_t *)location & ~PPC_INST_OFFSET24_MASK) |
+				(value & PPC_INST_OFFSET24_MASK);
 
 			if (patch_instruction((u32 *)location, ppc_inst(value)))
 				return -EFAULT;
Signature · 1 line
2.35.1
#37 lore raw

PATCH 21/22 powerpc/inst: Remove PPC_INST_BL

In reply to Christophe Leroy

Convert last users of PPC_INST_BL to PPC_RAW_BL()

And remove PPC_INST_BL.

---
 arch/powerpc/include/asm/ppc-opcode.h | 1 -
 arch/powerpc/net/bpf_jit.h            | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +0 -1
index ada8fe17b199..810a28af9dce 100644
@@ -290,7 +290,6 @@
 #define PPC_INST_ADDIS			0x3c000000
 #define PPC_INST_ADD			0x7c000214
 #define PPC_INST_DIVD			0x7c0003d2
-#define PPC_INST_BL			0x48000001
 #define PPC_INST_BRANCH_COND		0x40800000
 
 #define PPC_INST_OFFSET24_MASK		0x03fffffc
arch/powerpc/net/bpf_jit.h +1 -1
index 979701d360da..c63324ff9f04 100644
@@ -35,7 +35,7 @@
 	} while (0)
 
 /* bl (unconditional 'branch' with link) */
-#define PPC_BL(dest)	EMIT(PPC_INST_BL | (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc))
+#define PPC_BL(dest)	EMIT(PPC_RAW_BL((dest) - (unsigned long)(image + ctx->idx)))
 
 /* "cond" here covers BO:BI fields. */
 #define PPC_BCC_SHORT(cond, dest)					      \
Signature · 1 line
2.35.1
#38 lore raw

PATCH 22/22 powerpc/opcodes: Remove unused PPC_INST_XXX macros

In reply to Christophe Leroy

The following PPC_INST_XXX macros are not used anymore outside ppc-opcode.h:
- PPC_INST_LD
- PPC_INST_STD
- PPC_INST_ADDIS
- PPC_INST_ADD
- PPC_INST_DIVD

Remove them.

---
 arch/powerpc/include/asm/ppc-opcode.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +4 -9
index 810a28af9dce..4cdda507e816 100644
@@ -285,11 +285,6 @@
 #define PPC_INST_TRECHKPT		0x7c0007dd
 #define PPC_INST_TRECLAIM		0x7c00075d
 #define PPC_INST_TSR			0x7c0005dd
-#define PPC_INST_LD			0xe8000000
-#define PPC_INST_STD			0xf8000000
-#define PPC_INST_ADDIS			0x3c000000
-#define PPC_INST_ADD			0x7c000214
-#define PPC_INST_DIVD			0x7c0003d2
 #define PPC_INST_BRANCH_COND		0x40800000
 
 #define PPC_INST_OFFSET24_MASK		0x03fffffc
@@ -460,10 +455,10 @@
 	(0x100000c7 | ___PPC_RT(vrt) | ___PPC_RA(vra) | ___PPC_RB(vrb) | __PPC_RC21)
 #define PPC_RAW_VCMPEQUB_RC(vrt, vra, vrb) \
 	(0x10000006 | ___PPC_RT(vrt) | ___PPC_RA(vra) | ___PPC_RB(vrb) | __PPC_RC21)
-#define PPC_RAW_LD(r, base, i)		(PPC_INST_LD | ___PPC_RT(r) | ___PPC_RA(base) | IMM_DS(i))
+#define PPC_RAW_LD(r, base, i)		(0xe8000000 | ___PPC_RT(r) | ___PPC_RA(base) | IMM_DS(i))
 #define PPC_RAW_LWZ(r, base, i)		(0x80000000 | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))
 #define PPC_RAW_LWZX(t, a, b)		(0x7c00002e | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b))
-#define PPC_RAW_STD(r, base, i)		(PPC_INST_STD | ___PPC_RS(r) | ___PPC_RA(base) | IMM_DS(i))
+#define PPC_RAW_STD(r, base, i)		(0xf8000000 | ___PPC_RS(r) | ___PPC_RA(base) | IMM_DS(i))
 #define PPC_RAW_STDCX(s, a, b)		(0x7c0001ad | ___PPC_RS(s) | ___PPC_RA(a) | ___PPC_RB(b))
 #define PPC_RAW_LFSX(t, a, b)		(0x7c00042e | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b))
 #define PPC_RAW_STFSX(s, a, b)		(0x7c00052e | ___PPC_RS(s) | ___PPC_RA(a) | ___PPC_RB(b))
@@ -474,8 +469,8 @@
 #define PPC_RAW_ADDE(t, a, b)		(0x7c000114 | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b))
 #define PPC_RAW_ADDZE(t, a)		(0x7c000194 | ___PPC_RT(t) | ___PPC_RA(a))
 #define PPC_RAW_ADDME(t, a)		(0x7c0001d4 | ___PPC_RT(t) | ___PPC_RA(a))
-#define PPC_RAW_ADD(t, a, b)		(PPC_INST_ADD | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b))
-#define PPC_RAW_ADD_DOT(t, a, b)	(PPC_INST_ADD | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b) | 0x1)
+#define PPC_RAW_ADD(t, a, b)		(0x7c000214 | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b))
+#define PPC_RAW_ADD_DOT(t, a, b)	(0x7c000214 | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b) | 0x1)
 #define PPC_RAW_ADDC(t, a, b)		(0x7c000014 | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b))
 #define PPC_RAW_ADDC_DOT(t, a, b)	(0x7c000014 | ___PPC_RT(t) | ___PPC_RA(a) | ___PPC_RB(b) | 0x1)
 #define PPC_RAW_NOP()			PPC_RAW_ORI(0, 0, 0)
Signature · 1 line
2.35.1