Skip to contentSkip to conversation

resend: ppc64 LE ABI v2 ftrace-with-regs implementation

PATCH Superseded Torsten Duwe 4 participants 21 messages Jun 11, 2015 – Jun 19, 2015

Subsystems (4)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover resend: ppc64 LE ABI v2 ftrace-with-regs implementation

Hi again,

with the previous submission I was too much into the details to state a few obvious things. Now with nicer explanations and changelog.

Torsten

#1 lore raw

PATCH 1/4 ppc64 FTRACE_WITH_REGS implementation

Implement FTRACE_WITH_REGS for powerpc64, on ELF ABI v2. Initial work started by Vojtech Pavlik, used with permission.

* arch/powerpc/kernel/entry_64.S:
- enhance _mcount with a stub to test (ftrace_trace_function == &ftrace_stub) as suggested in Documentation/trace/ftrace-design.txt
(for reference only, patched out at runtime)
- Implement an effective ftrace_caller that works from within the kernel binary as well as from modules.
* arch/powerpc/kernel/ftrace.c:
- be prepared to deal with ppc64 ELV ABI v2, especially calls to _mcount that result from gcc -mprofile-kernel
* arch/powerpc/kernel/module_64.c:
- do not save the TOC pointer on the trampoline when the destination is ftrace_caller. This trampoline jump happens from a function prologue before a new stack frame is set up, so bad things may happen otherwise...
- relax is_module_trampoline() to recognise the modified trampoline.

--

 arch/powerpc/include/asm/ftrace.h |    5 +
 arch/powerpc/kernel/entry_64.S    |  112 +++++++++++++++++++++++++++++++-------
 arch/powerpc/kernel/ftrace.c      |   67 ++++++++++++++++++++--
 arch/powerpc/kernel/module_64.c   |   33 ++++++++++-
 4 files changed, 186 insertions(+), 31 deletions(-)

--

arch/powerpc/include/asm/ftrace.h +5 -0
index e366187..6111191 100644
@@ -46,6 +46,8 @@
 extern void _mcount(void);
 
 #ifdef CONFIG_DYNAMIC_FTRACE
+# define FTRACE_ADDR ((unsigned long)ftrace_caller+8)
+# define FTRACE_REGS_ADDR FTRACE_ADDR
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
        /* reloction of mcount call site is the same as the address */
@@ -58,6 +60,9 @@ struct dyn_arch_ftrace {
 #endif /*  CONFIG_DYNAMIC_FTRACE */
 #endif /* __ASSEMBLY__ */
 
+#ifdef CONFIG_DYNAMIC_FTRACE
+#define ARCH_SUPPORTS_FTRACE_OPS 1
+#endif
 #endif
 
 #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64) && !defined(__ASSEMBLY__)
arch/powerpc/kernel/entry_64.S +92 -20

146 lines not on this page · raw message

arch/powerpc/kernel/ftrace.c +59 -8

115 lines not on this page · raw message

arch/powerpc/kernel/module_64.c +30 -3
index 6838451..1428ad8 100644
@@ -138,12 +138,22 @@ static u32 ppc64_stub_insns[] = {
 	0x4e800420			/* bctr */
 };
 
+/* In case of _mcount calls or dynamic ftracing, Do not save the
+   current callee's TOC (in R2) again into the original caller's stack
+   frame during this trampoline hop. The stack frame already holds
+   that of the original caller.  _mcount and ftrace_caller will take
+   care of this TOC value themselves.
+*/
+#define SQUASH_TOC_SAVE_INSN(trampoline_addr) \
+	((struct ppc64_stub_entry*)(trampoline_addr))-> \
+	jump[2] = PPC_INST_NOP;
+
 #ifdef CONFIG_DYNAMIC_FTRACE
 
 static u32 ppc64_stub_mask[] = {
 	0xffff0000,
 	0xffff0000,
-	0xffffffff,
+	0x00000000,
 	0xffffffff,
 #if !defined(_CALL_ELF) || _CALL_ELF != 2
 	0xffffffff,
@@ -170,6 +180,9 @@ bool is_module_trampoline(u32 *p)
 		if ((insna & mask) != (insnb & mask))
 			return false;
 	}
+	if (insns[2] != ppc64_stub_insns[2] &&
+	    insns[2] != PPC_INST_NOP )
+		return false;
 
 	return true;
 }
@@ -475,6 +488,14 @@ static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
 static int restore_r2(u32 *instruction, struct module *me)
 {
 	if (*instruction != PPC_INST_NOP) {
+
+		/* -mprofile_kernel sequence starting with mflr r0; std r0, LRSAVE(r1) */
+		if (instruction[-3] == 0x7c0802a6 && instruction[-2] == 0xf8010010) {
+			/* Nothing to be done here, it's a _mcount call location
+			   and r2 will have to be restored in the _mcount function */
+			return 2;
+		};
+
 		pr_err("%s: Expect noop after relocate, got %08x\n",
 		       me->name, *instruction);
 		return 0;
@@ -490,7 +511,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 		       unsigned int relsec,
 		       struct module *me)
 {
-	unsigned int i;
+	unsigned int i, r2;
 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
 	Elf64_Sym *sym;
 	unsigned long *location;
@@ -603,8 +624,11 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 				value = stub_for_addr(sechdrs, value, me);
 				if (!value)
 					return -ENOENT;
-				if (!restore_r2((u32 *)location + 1, me))
+				if (!(r2 = restore_r2((u32 *)location + 1, me)))
 					return -ENOEXEC;
+				/* Squash the TOC saver for profiler calls */
+				if (!strcmp("_mcount", strtab+sym->st_name))
+					SQUASH_TOC_SAVE_INSN(value);
 			} else
 				value += local_entry_offset(sym);
 
@@ -665,6 +689,9 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 	me->arch.tramp = stub_for_addr(sechdrs,
 				       (unsigned long)ftrace_caller,
 				       me);
+	/* ftrace_caller will take care of the TOC;
+	   do not clobber original caller's value. */
+	SQUASH_TOC_SAVE_INSN(me->arch.tramp);
 #endif
 
 	return 0;
#2 lore raw

PATCH 2/4 ppc64 ftrace_with_regs configuration variables

In reply to Torsten Duwe

* Makefile:
- globally use -mprofile-kernel in case it's configured.
* arch/powerpc/Kconfig / kernel/trace/Kconfig:
- declare that ppc64 HAVE_MPROFILE_KERNEL and HAVE_DYNAMIC_FTRACE_WITH_REGS, and use it.

--

 Makefile             |    5 ++++-
 arch/powerpc/Kconfig |    2 ++
 kernel/trace/Kconfig |    5 +++++
 3 files changed, 11 insertions(+), 1 deletion(-)

--

Makefile +4 -1
index 3d16bcc..bbd5e87 100644
@@ -733,7 +733,10 @@ export CC_FLAGS_FTRACE
 ifdef CONFIG_HAVE_FENTRY
 CC_USING_FENTRY	:= $(call cc-option, -mfentry -DCC_USING_FENTRY)
 endif
-KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_USING_FENTRY)
+ifdef CONFIG_HAVE_MPROFILE_KERNEL
+CC_USING_MPROFILE_KERNEL	:= $(call cc-option, -mprofile-kernel -DCC_USING_MPROFILE_KERNEL)
+endif
+KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_USING_FENTRY) $(CC_USING_MPROFILE_KERNEL)
 KBUILD_AFLAGS	+= $(CC_USING_FENTRY)
 ifdef CONFIG_DYNAMIC_FTRACE
 	ifdef CONFIG_HAVE_C_RECORDMCOUNT
arch/powerpc/Kconfig +2 -0
index 22b0940..566f204 100644
@@ -94,8 +94,10 @@ config PPC
 	select OF_RESERVED_MEM
 	select HAVE_FTRACE_MCOUNT_RECORD
 	select HAVE_DYNAMIC_FTRACE
+	select HAVE_DYNAMIC_FTRACE_WITH_REGS
 	select HAVE_FUNCTION_TRACER
 	select HAVE_FUNCTION_GRAPH_TRACER
+	select HAVE_MPROFILE_KERNEL
 	select SYSCTL_EXCEPTION_TRACE
 	select ARCH_WANT_OPTIONAL_GPIOLIB
 	select VIRT_TO_BUS if !PPC64
kernel/trace/Kconfig +5 -0
index a5da09c..dd53f3d 100644
@@ -52,6 +52,11 @@ config HAVE_FENTRY
 	help
 	  Arch supports the gcc options -pg with -mfentry
 
+config HAVE_MPROFILE_KERNEL
+	bool
+	help
+	  Arch supports the gcc options -pg with -mprofile-kernel
+
 config HAVE_C_RECORDMCOUNT
 	bool
 	help
#3 lore raw

PATCH 3/4 ppc64 ftrace_with_regs: spare early boot and low level code

In reply to Torsten Duwe

Using -mprofile-kernel on early boot code not only confuses the checker but is also useless, as the infrastructure is not yet in place. Proceed like with -pg (remove it from CFLAGS), equally with time.o and ftrace itself.

* arch/powerpc/kernel/Makefile:
- remove -mprofile-kernel from low level and boot code objects' CFLAGS for FUNCTION_TRACER configurations.

--

 arch/powerpc/kernel/Makefile |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

--

arch/powerpc/kernel/Makefile +6 -6
index 502cf69..fb33fc5 100644
@@ -17,14 +17,14 @@ endif
 
 ifdef CONFIG_FUNCTION_TRACER
 # Do not trace early boot code
-CFLAGS_REMOVE_cputable.o = -pg -mno-sched-epilog
-CFLAGS_REMOVE_prom_init.o = -pg -mno-sched-epilog
-CFLAGS_REMOVE_btext.o = -pg -mno-sched-epilog
-CFLAGS_REMOVE_prom.o = -pg -mno-sched-epilog
+CFLAGS_REMOVE_cputable.o = -pg -mno-sched-epilog -mprofile-kernel
+CFLAGS_REMOVE_prom_init.o = -pg -mno-sched-epilog -mprofile-kernel
+CFLAGS_REMOVE_btext.o = -pg -mno-sched-epilog -mprofile-kernel
+CFLAGS_REMOVE_prom.o = -pg -mno-sched-epilog -mprofile-kernel
 # do not trace tracer code
-CFLAGS_REMOVE_ftrace.o = -pg -mno-sched-epilog
+CFLAGS_REMOVE_ftrace.o = -pg -mno-sched-epilog -mprofile-kernel
 # timers used by tracing
-CFLAGS_REMOVE_time.o = -pg -mno-sched-epilog
+CFLAGS_REMOVE_time.o = -pg -mno-sched-epilog -mprofile-kernel
 endif
 
 obj-y				:= cputable.o ptrace.o syscalls.o \
#4 lore raw

PATCH 4/4 ppc64 ftrace_with_regs recursion protection

In reply to Torsten Duwe

This is an *emergency* parachute to avoid an endless recursion and consecutively a kernel stack overflow, should any function within some ftrace framework cause an access fault, which calls _mcount / ftrace_caller in return and so on. It might also call an ftrace'd function directly.

As Michael Ellerman pointed out, it is a tedious and error-prone task to maintain a complete list of those functions that _might_ get called from *_access_fault or any dynamic tracer function. So we'll concentrate on the most frequent cases to enhance performance later, while for now sticking with this fill-in. It will later serve as a backup protection.

* arch/powerpc/kernel/entry_64.S:
- test-and-set TRACE_FTRACE_BIT in task_struct's trace_recursion, do not call the actual tracer function if set, clear flag on return.

--

 arch/powerpc/kernel/asm-offsets.c |    1 +
 arch/powerpc/kernel/entry_64.S    |   15 +++++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

--

arch/powerpc/kernel/asm-offsets.c +1 -0
index 4717859..ae10752 100644
@@ -72,6 +72,7 @@ int main(void)
 	DEFINE(THREAD, offsetof(struct task_struct, thread));
 	DEFINE(MM, offsetof(struct task_struct, mm));
 	DEFINE(MMCONTEXTID, offsetof(struct mm_struct, context.id));
+	DEFINE(TASK_TRACEREC, offsetof(struct task_struct, trace_recursion));
 #ifdef CONFIG_PPC64
 	DEFINE(AUDITCONTEXT, offsetof(struct task_struct, audit_context));
 	DEFINE(SIGSEGV, SIGSEGV);
arch/powerpc/kernel/entry_64.S +13 -2
index a4132ef..4768104 100644
@@ -1202,7 +1202,13 @@ _GLOBAL(ftrace_caller)
 	SAVE_8GPRS(16,r1)
 	SAVE_8GPRS(24,r1)
 
-
+	ld	r3, PACACURRENT(r13)
+	ld	r4, TASK_TRACEREC(r3)
+	andi.	r5, r4, 0x0010 // ( 1 << TRACE_FTRACE_BIT )
+	ori	r4, r4, 0x0010
+	std	r4, TASK_TRACEREC(r3)
+	bne-	3f		// ftrace in progress - avoid recursion!
+	
 	LOAD_REG_IMMEDIATE(r3,function_trace_op)
 	ld	r5,0(r3)
 
@@ -1224,9 +1230,14 @@ ftrace_call:
 	bl	ftrace_stub
 	nop
 
+	ld	r3, PACACURRENT(r13)
+	ld	r4, TASK_TRACEREC(r3)
+	andi.	r4, r4, 0xffef // ~( 1 << TRACE_FTRACE_BIT )
+	std	r4, TASK_TRACEREC(r3)
+
 	ld	r3, _NIP(r1)
 	mtlr	r3
-
+3:
 	REST_8GPRS(0,r1)
 	REST_8GPRS(8,r1)
 	REST_8GPRS(16,r1)
#5 lore raw

cover ppc64 LE ABI v2 ftrace-with-regs implementation

In reply to Torsten Duwe

Did I miss anything else? I have the notrace optimisations and kernel live patching in the queue, which depends on this, so I'd really appreciate a comment whether it is acceptable in this form or not. Thanks in advance!

Torsten

#6 lore raw

PATCH RESEND 1/4 ppc64 FTRACE_WITH_REGS implementation

Implement FTRACE_WITH_REGS for powerpc64, on ELF ABI v2. Initial work started by Vojtech Pavlik, used with permission.

* arch/powerpc/kernel/entry_64.S:
- enhance _mcount with a stub to test (ftrace_trace_function == &ftrace_stub) as suggested in Documentation/trace/ftrace-design.txt
(for reference only, patched out at runtime)
- Implement an effective ftrace_caller that works from within the kernel binary as well as from modules.
* arch/powerpc/kernel/ftrace.c:
- be prepared to deal with ppc64 ELV ABI v2, especially calls to _mcount that result from gcc -mprofile-kernel
* arch/powerpc/kernel/module_64.c:
- do not save the TOC pointer on the trampoline when the destination is ftrace_caller. This trampoline jump happens from a function prologue before a new stack frame is set up, so bad things may happen otherwise...
- relax is_module_trampoline() to recognise the modified trampoline.

--

 arch/powerpc/include/asm/ftrace.h |    5 +
 arch/powerpc/kernel/entry_64.S    |  112 +++++++++++++++++++++++++++++++-------
 arch/powerpc/kernel/ftrace.c      |   67 ++++++++++++++++++++--
 arch/powerpc/kernel/module_64.c   |   33 ++++++++++-
 4 files changed, 186 insertions(+), 31 deletions(-)

--

arch/powerpc/include/asm/ftrace.h +5 -0
index e366187..6111191 100644
@@ -46,6 +46,8 @@
 extern void _mcount(void);
 
 #ifdef CONFIG_DYNAMIC_FTRACE
+# define FTRACE_ADDR ((unsigned long)ftrace_caller+8)
+# define FTRACE_REGS_ADDR FTRACE_ADDR
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
        /* reloction of mcount call site is the same as the address */
@@ -58,6 +60,9 @@ struct dyn_arch_ftrace {
 #endif /*  CONFIG_DYNAMIC_FTRACE */
 #endif /* __ASSEMBLY__ */
 
+#ifdef CONFIG_DYNAMIC_FTRACE
+#define ARCH_SUPPORTS_FTRACE_OPS 1
+#endif
 #endif
 
 #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64) && !defined(__ASSEMBLY__)
arch/powerpc/kernel/entry_64.S +92 -20

146 lines not on this page · raw message

arch/powerpc/kernel/ftrace.c +59 -8

115 lines not on this page · raw message

arch/powerpc/kernel/module_64.c +30 -3
index 6838451..1428ad8 100644
@@ -138,12 +138,22 @@ static u32 ppc64_stub_insns[] = {
 	0x4e800420			/* bctr */
 };
 
+/* In case of _mcount calls or dynamic ftracing, Do not save the
+   current callee's TOC (in R2) again into the original caller's stack
+   frame during this trampoline hop. The stack frame already holds
+   that of the original caller.  _mcount and ftrace_caller will take
+   care of this TOC value themselves.
+*/
+#define SQUASH_TOC_SAVE_INSN(trampoline_addr) \
+	((struct ppc64_stub_entry*)(trampoline_addr))-> \
+	jump[2] = PPC_INST_NOP;
+
 #ifdef CONFIG_DYNAMIC_FTRACE
 
 static u32 ppc64_stub_mask[] = {
 	0xffff0000,
 	0xffff0000,
-	0xffffffff,
+	0x00000000,
 	0xffffffff,
 #if !defined(_CALL_ELF) || _CALL_ELF != 2
 	0xffffffff,
@@ -170,6 +180,9 @@ bool is_module_trampoline(u32 *p)
 		if ((insna & mask) != (insnb & mask))
 			return false;
 	}
+	if (insns[2] != ppc64_stub_insns[2] &&
+	    insns[2] != PPC_INST_NOP )
+		return false;
 
 	return true;
 }
@@ -475,6 +488,14 @@ static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
 static int restore_r2(u32 *instruction, struct module *me)
 {
 	if (*instruction != PPC_INST_NOP) {
+
+		/* -mprofile_kernel sequence starting with mflr r0; std r0, LRSAVE(r1) */
+		if (instruction[-3] == 0x7c0802a6 && instruction[-2] == 0xf8010010) {
+			/* Nothing to be done here, it's a _mcount call location
+			   and r2 will have to be restored in the _mcount function */
+			return 2;
+		};
+
 		pr_err("%s: Expect noop after relocate, got %08x\n",
 		       me->name, *instruction);
 		return 0;
@@ -490,7 +511,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 		       unsigned int relsec,
 		       struct module *me)
 {
-	unsigned int i;
+	unsigned int i, r2;
 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
 	Elf64_Sym *sym;
 	unsigned long *location;
@@ -603,8 +624,11 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 				value = stub_for_addr(sechdrs, value, me);
 				if (!value)
 					return -ENOENT;
-				if (!restore_r2((u32 *)location + 1, me))
+				if (!(r2 = restore_r2((u32 *)location + 1, me)))
 					return -ENOEXEC;
+				/* Squash the TOC saver for profiler calls */
+				if (!strcmp("_mcount", strtab+sym->st_name))
+					SQUASH_TOC_SAVE_INSN(value);
 			} else
 				value += local_entry_offset(sym);
 
@@ -665,6 +689,9 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
 	me->arch.tramp = stub_for_addr(sechdrs,
 				       (unsigned long)ftrace_caller,
 				       me);
+	/* ftrace_caller will take care of the TOC;
+	   do not clobber original caller's value. */
+	SQUASH_TOC_SAVE_INSN(me->arch.tramp);
 #endif
 
 	return 0;
maintainer
#7 lore raw

Re: [RESEND PATCH 1/4] ppc64 FTRACE_WITH_REGS implementation

arch/powerpc/kernel/entry_64.S _GLOBAL(enter_prom) · line 1255

Quoted 5 lines · On Thu, 18 Jun 2015 18:21:07 +0200 Torsten Duwe <[email protected]> wrote:
arch/powerpc/kernel/entry_64.S +4 -0
+	
 _GLOBAL(ftrace_stub)
+	nop
+	nop
+.localentry ftrace_stub,.-ftrace_stub	

You might want to run checkpatch and fix your whitespace errors.

-- Steve

Quoted 4 lines
arch/powerpc/kernel/entry_64.S +0 -0
 	blr
 #else
 _GLOBAL_TOC(_mcount)
@@ -1211,12 +1286,12 @@ _GLOBAL(ftrace_stub)
#8 lore raw

Re: [RESEND PATCH 1/4] ppc64 FTRACE_WITH_REGS implementation

Quoted 10 lines · Steven Rostedt

arch/powerpc/kernel/entry_64.S _GLOBAL(enter_prom) · line 1255

Quoted 5 lines · On Thu, 18 Jun 2015 18:21:07 +0200 Torsten Duwe <[email protected]> wrote:
arch/powerpc/kernel/entry_64.S +4 -0
+	
 _GLOBAL(ftrace_stub)
+	nop
+	nop
+.localentry ftrace_stub,.-ftrace_stub	

You might want to run checkpatch and fix your whitespace errors.

Right. Will do.
Thanks,

Torsten

maintainer
#9 lore raw

Re: [RESEND PATCH 1/4] ppc64 FTRACE_WITH_REGS implementation

In reply to Torsten Duwe
Quoted 18 lines · On Thu, 18 Jun 2015 18:21:07 +0200 Torsten Duwe <[email protected]> wrote:

Implement FTRACE_WITH_REGS for powerpc64, on ELF ABI v2. Initial work started by Vojtech Pavlik, used with permission.

* arch/powerpc/kernel/entry_64.S:
- enhance _mcount with a stub to test (ftrace_trace_function == &ftrace_stub) as suggested in Documentation/trace/ftrace-design.txt
(for reference only, patched out at runtime)
- Implement an effective ftrace_caller that works from within the kernel binary as well as from modules.
* arch/powerpc/kernel/ftrace.c:
- be prepared to deal with ppc64 ELV ABI v2, especially calls to _mcount that result from gcc -mprofile-kernel
* arch/powerpc/kernel/module_64.c:
- do not save the TOC pointer on the trampoline when the destination is ftrace_caller. This trampoline jump happens from a function prologue before a new stack frame is set up, so bad things may happen otherwise...
- relax is_module_trampoline() to recognise the modified trampoline.

Quoted 2 lines

--

FYI, the standard notation after the change log is '---' not '--'. That way git will chop off this from the change log. When I did a git am -s on this patch I had:

    Signed-off-by: Torsten Duwe <[email protected]>
    --
     arch/powerpc/include/asm/ftrace.h |    5 +
     arch/powerpc/kernel/entry_64.S    |  112 +++++++++++++++++++++++++++++++-------
     arch/powerpc/kernel/ftrace.c      |   67 ++++++++++++++++++++--
     arch/powerpc/kernel/module_64.c   |   33 ++++++++++-
     4 files changed, 186 insertions(+), 31 deletions(-)
    --
    Signed-off-by: Steven Rostedt <[email protected]>

-- Steve

Quoted 10 lines
 arch/powerpc/include/asm/ftrace.h |    5 +
 arch/powerpc/kernel/entry_64.S    |  112 +++++++++++++++++++++++++++++++-------
 arch/powerpc/kernel/ftrace.c      |   67 ++++++++++++++++++++--
 arch/powerpc/kernel/module_64.c   |   33 ++++++++++-
 4 files changed, 186 insertions(+), 31 deletions(-)

--

arch/powerpc/include/asm/ftrace.h +0 -0
index e366187..6111191 100644
#10 lore raw

PATCH RESEND 2/4 ppc64 ftrace_with_regs configuration variables

In reply to Torsten Duwe

* Makefile:
- globally use -mprofile-kernel in case it's configured.
* arch/powerpc/Kconfig / kernel/trace/Kconfig:
- declare that ppc64 HAVE_MPROFILE_KERNEL and HAVE_DYNAMIC_FTRACE_WITH_REGS, and use it.

--

 Makefile             |    5 ++++-
 arch/powerpc/Kconfig |    2 ++
 kernel/trace/Kconfig |    5 +++++
 3 files changed, 11 insertions(+), 1 deletion(-)

--

Makefile +4 -1
index 3d16bcc..bbd5e87 100644
@@ -733,7 +733,10 @@ export CC_FLAGS_FTRACE
 ifdef CONFIG_HAVE_FENTRY
 CC_USING_FENTRY	:= $(call cc-option, -mfentry -DCC_USING_FENTRY)
 endif
-KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_USING_FENTRY)
+ifdef CONFIG_HAVE_MPROFILE_KERNEL
+CC_USING_MPROFILE_KERNEL	:= $(call cc-option, -mprofile-kernel -DCC_USING_MPROFILE_KERNEL)
+endif
+KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_USING_FENTRY) $(CC_USING_MPROFILE_KERNEL)
 KBUILD_AFLAGS	+= $(CC_USING_FENTRY)
 ifdef CONFIG_DYNAMIC_FTRACE
 	ifdef CONFIG_HAVE_C_RECORDMCOUNT
arch/powerpc/Kconfig +2 -0
index 22b0940..566f204 100644
@@ -94,8 +94,10 @@ config PPC
 	select OF_RESERVED_MEM
 	select HAVE_FTRACE_MCOUNT_RECORD
 	select HAVE_DYNAMIC_FTRACE
+	select HAVE_DYNAMIC_FTRACE_WITH_REGS
 	select HAVE_FUNCTION_TRACER
 	select HAVE_FUNCTION_GRAPH_TRACER
+	select HAVE_MPROFILE_KERNEL
 	select SYSCTL_EXCEPTION_TRACE
 	select ARCH_WANT_OPTIONAL_GPIOLIB
 	select VIRT_TO_BUS if !PPC64
kernel/trace/Kconfig +5 -0
index a5da09c..dd53f3d 100644
@@ -52,6 +52,11 @@ config HAVE_FENTRY
 	help
 	  Arch supports the gcc options -pg with -mfentry
 
+config HAVE_MPROFILE_KERNEL
+	bool
+	help
+	  Arch supports the gcc options -pg with -mprofile-kernel
+
 config HAVE_C_RECORDMCOUNT
 	bool
 	help
#11 lore raw

PATCH RESEND 3/4 ppc64 ftrace_with_regs: spare early boot and low level code

In reply to Torsten Duwe

Using -mprofile-kernel on early boot code not only confuses the checker but is also useless, as the infrastructure is not yet in place. Proceed like with -pg (remove it from CFLAGS), equally with time.o and ftrace itself.

* arch/powerpc/kernel/Makefile:
- remove -mprofile-kernel from low level and boot code objects' CFLAGS for FUNCTION_TRACER configurations.

--

 arch/powerpc/kernel/Makefile |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

--

arch/powerpc/kernel/Makefile +6 -6
index 502cf69..fb33fc5 100644
@@ -17,14 +17,14 @@ endif
 
 ifdef CONFIG_FUNCTION_TRACER
 # Do not trace early boot code
-CFLAGS_REMOVE_cputable.o = -pg -mno-sched-epilog
-CFLAGS_REMOVE_prom_init.o = -pg -mno-sched-epilog
-CFLAGS_REMOVE_btext.o = -pg -mno-sched-epilog
-CFLAGS_REMOVE_prom.o = -pg -mno-sched-epilog
+CFLAGS_REMOVE_cputable.o = -pg -mno-sched-epilog -mprofile-kernel
+CFLAGS_REMOVE_prom_init.o = -pg -mno-sched-epilog -mprofile-kernel
+CFLAGS_REMOVE_btext.o = -pg -mno-sched-epilog -mprofile-kernel
+CFLAGS_REMOVE_prom.o = -pg -mno-sched-epilog -mprofile-kernel
 # do not trace tracer code
-CFLAGS_REMOVE_ftrace.o = -pg -mno-sched-epilog
+CFLAGS_REMOVE_ftrace.o = -pg -mno-sched-epilog -mprofile-kernel
 # timers used by tracing
-CFLAGS_REMOVE_time.o = -pg -mno-sched-epilog
+CFLAGS_REMOVE_time.o = -pg -mno-sched-epilog -mprofile-kernel
 endif
 
 obj-y				:= cputable.o ptrace.o syscalls.o \
#12 lore raw

PATCH RESEND 4/4 ppc64 ftrace_with_regs recursion protection

In reply to Torsten Duwe

This is an *emergency* parachute to avoid an endless recursion and consecutively a kernel stack overflow, should any function within some ftrace framework cause an access fault, which calls _mcount / ftrace_caller in return and so on. It might also call an ftrace'd function directly.

As Michael Ellerman pointed out, it is a tedious and error-prone task to maintain a complete list of those functions that _might_ get called from *_access_fault or any dynamic tracer function. So we'll concentrate on the most frequent cases to enhance performance later, while for now sticking with this fill-in. It will later serve as a backup protection.

* arch/powerpc/kernel/entry_64.S:
- test-and-set TRACE_FTRACE_BIT in task_struct's trace_recursion, do not call the actual tracer function if set, clear flag on return.

--

 arch/powerpc/kernel/asm-offsets.c |    1 +
 arch/powerpc/kernel/entry_64.S    |   15 +++++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

--

arch/powerpc/kernel/asm-offsets.c +1 -0
index 4717859..ae10752 100644
@@ -72,6 +72,7 @@ int main(void)
 	DEFINE(THREAD, offsetof(struct task_struct, thread));
 	DEFINE(MM, offsetof(struct task_struct, mm));
 	DEFINE(MMCONTEXTID, offsetof(struct mm_struct, context.id));
+	DEFINE(TASK_TRACEREC, offsetof(struct task_struct, trace_recursion));
 #ifdef CONFIG_PPC64
 	DEFINE(AUDITCONTEXT, offsetof(struct task_struct, audit_context));
 	DEFINE(SIGSEGV, SIGSEGV);
arch/powerpc/kernel/entry_64.S +13 -2
index a4132ef..4768104 100644
@@ -1202,7 +1202,13 @@ _GLOBAL(ftrace_caller)
 	SAVE_8GPRS(16,r1)
 	SAVE_8GPRS(24,r1)
 
-
+	ld	r3, PACACURRENT(r13)
+	ld	r4, TASK_TRACEREC(r3)
+	andi.	r5, r4, 0x0010 // ( 1 << TRACE_FTRACE_BIT )
+	ori	r4, r4, 0x0010
+	std	r4, TASK_TRACEREC(r3)
+	bne-	3f		// ftrace in progress - avoid recursion!
+	
 	LOAD_REG_IMMEDIATE(r3,function_trace_op)
 	ld	r5,0(r3)
 
@@ -1224,9 +1230,14 @@ ftrace_call:
 	bl	ftrace_stub
 	nop
 
+	ld	r3, PACACURRENT(r13)
+	ld	r4, TASK_TRACEREC(r3)
+	andi.	r4, r4, 0xffef // ~( 1 << TRACE_FTRACE_BIT )
+	std	r4, TASK_TRACEREC(r3)
+
 	ld	r3, _NIP(r1)
 	mtlr	r3
-
+3:
 	REST_8GPRS(0,r1)
 	REST_8GPRS(8,r1)
 	REST_8GPRS(16,r1)
maintainer
#13 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

In reply to Torsten Duwe
Quoted 6 lines · On Thu, 18 Jun 2015 18:17:27 +0200 Torsten Duwe <[email protected]> wrote:

Did I miss anything else? I have the notrace optimisations and kernel live patching in the queue, which depends on this, so I'd really appreciate a comment whether it is acceptable in this form or not. Thanks in advance!

Torsten

I applied these patches against v4.1-rc8 and I get this compiling it for my electra:

/work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S: Assembler messages:
/work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S:1165: Error: unknown pseudo-op: .localentry' /work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S:1189: Error: unknown pseudo-op: .localentry' /work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S:1271: Error: unknown pseudo-op: `.localentry' /work/autotest/nobackup/powerpc-test.git/scripts/Makefile.build:294: recipe for target 'arch/powerpc/kernel/entry_64.o' failed make[2]: *** [arch/powerpc/kernel/entry_64.o] Error 1

Config attached.

-- Steve

#14 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

Quoted 10 lines · Steven Rostedt

I applied these patches against v4.1-rc8 and I get this compiling it for my electra:

/work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S: Assembler messages:
/work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S:1165: Error: unknown pseudo-op: .localentry' /work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S:1189: Error: unknown pseudo-op: .localentry' /work/autotest/nobackup/powerpc-test.git/arch/powerpc/kernel/entry_64.S:1271: Error: unknown pseudo-op: `.localentry' /work/autotest/nobackup/powerpc-test.git/scripts/Makefile.build:294: recipe for target 'arch/powerpc/kernel/entry_64.o' failed make[2]: *** [arch/powerpc/kernel/entry_64.o] Error 1

After removing your cross compile prefix, with your config I get:

| kernel/bounds.c:1:0: error: -mcall-aixdesc incompatible with -mabi=elfv2

Are you building ppc64, little endian, ELF ABIv2 ? Is your cross compiler working properly?

I'm compiling natively on that platform (faster for me than an x86 ;-) | GNU assembler (GNU Binutils; SUSE Linux Enterprise 12) 2.24.0.20140403-7 | gcc (SUSE Linux) 4.8.3 20140627 [gcc-4_8-branch revision 212064]

4.1.0-rc8 config attached.

HTH,
Torsten

maintainer
#15 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

Quoted 3 lines · On Fri, 19 Jun 2015 14:00:44 +0200 Torsten Duwe <[email protected]> wrote:

| kernel/bounds.c:1:0: error: -mcall-aixdesc incompatible with -mabi=elfv2

Are you building ppc64, little endian, ELF ABIv2 ?

It's big endian.

Quoted 1 line

Is your cross compiler working properly?

Well, just before applying your patches, I built and booted 4.1-rc8 on that box. Then I applied the patches and got the error on build.

I just reverted your patches, and it builds again.

Quoted 2 lines

I'm compiling natively on that platform (faster for me than an x86 ;-)

x86 is bit faster for me to compile.

Quoted 2 lines

| GNU assembler (GNU Binutils; SUSE Linux Enterprise 12) 2.24.0.20140403-7 | gcc (SUSE Linux) 4.8.3 20140627 [gcc-4_8-branch revision 212064]

I'm using the cross tool chains from kernel.org

https://www.kernel.org/pub/tools/crosstool/files/bin/x86_64/4.6.3/

powerpc64-linux-gcc (GCC) 4.6.3

Quoted 2 lines

4.1.0-rc8 config attached.

Um, what do you want me to do with your config? If your patches don't build on my box when vanilla does, it's a regression. In other words, you broke my machine.

-- Steve

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in Please read the FAQ at http://www.tux.org/lkml/

#16 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

Quoted 9 lines · Steven Rostedt
Quoted 3 lines · On Fri, 19 Jun 2015 14:00:44 +0200 Torsten Duwe <[email protected]> wrote:

| kernel/bounds.c:1:0: error: -mcall-aixdesc incompatible with -mabi=elfv2

Are you building ppc64, little endian, ELF ABIv2 ?

It's big endian.

And ABIv1, consequently, and then you probably don't have .localentry. The code inherently assumes ABIv2, v1 does not work.

I'll add a dependency on v2 then, if possible.

Torsten

#17 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

In reply to Torsten Duwe
Quoted 4 lines · Torsten Duwe

Did I miss anything else? I have the notrace optimisations and kernel live patching in the queue, which depends on this, so I'd really appreciate a comment whether it is acceptable in this form or not. Thanks in advance!

Hi Torsten,

Sorry I just haven't had time to look at it this week while I've been doing linux-next.

I'll try and review it next week.

There's no need to resend, we keep track of things in patchwork:

http://patchwork.ozlabs.org/project/linuxppc-dev/list/?submitter=2722

cheers

#18 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

In reply to Torsten Duwe
Quoted 4 lines · Torsten Duwe

Did I miss anything else? I have the notrace optimisations and kernel live patching in the queue, which depends on this, so I'd really appreciate a comment whether it is acceptable in this form or not. Thanks in advance!

Hi Torsten,

could you provide a description how you did test that.

Thanks.

Quoted 7 lines

Torsten

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#19 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

Quoted 4 lines · Denis Kirjanov

Hi Torsten,

could you provide a description how you did test that.

Vanilla kernel, those patches on top, built as described to Steven in the previous mail (.config attached there, built natively on ppc64le ABIv2, mentioned tool versions)

make vmlinux, make modules, build initrd qemu-system-ppc64 -no-reboot -enable-kvm -m 20480 -kernel vmlinux
-initrd initrd.gz -append console=hvc0 -nographic -vga none
inside:
cd /sys/kernel/debug/tracing
mkdir instances/Foo
cd instances/Foo
echo function > current_tracer

current_tracer holds "nop" or "function"; the transition is accompanied by heavy binary patching. When the function tracer is active, almost all calls go through ftrace_caller. This should be enough for a start; the proper stack frame layout details for the tracer functions still remain to be checked...

Torsten

maintainer
#20 lore raw

Re: [RESEND PATCH 0/4] ppc64 LE ABI v2 ftrace-with-regs implementation

Quoted 10 lines · On Fri, 19 Jun 2015 14:17:03 +0200 Torsten Duwe <[email protected]> wrote:
Quoted 4 lines · Denis Kirjanov

Hi Torsten,

could you provide a description how you did test that.

Vanilla kernel, those patches on top, built as described to Steven in the previous mail (.config attached there, built natively on ppc64le ABIv2, mentioned tool versions)

Great that you got it working on your machine. But you need to do it in a way that you don't break builds of existing machines, with existing (still supported) compilers.

-- Steve

Quoted 9 lines

make vmlinux, make modules, build initrd qemu-system-ppc64 -no-reboot -enable-kvm -m 20480 -kernel vmlinux
-initrd initrd.gz -append console=hvc0 -nographic -vga none
inside:
cd /sys/kernel/debug/tracing
mkdir instances/Foo
cd instances/Foo
echo function > current_tracer

Quoted 7 lines

current_tracer holds "nop" or "function"; the transition is accompanied by heavy binary patching. When the function tracer is active, almost all calls go through ftrace_caller. This should be enough for a start; the proper stack frame layout details for the tracer functions still remain to be checked...

Torsten

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in Please read the FAQ at http://www.tux.org/lkml/