2020-03-27 15:56:01

by Eugeniy Paltsev

[permalink] [raw]
Subject: [RFC] ARC: initial ftrace support

Add initial ftrace support for ARCv2. We add support only for
function tracer (the simplest, not dynamic one), however it is
prerequisite for dynamic function tracer and other complex
ones.

Signed-off-by: Eugeniy Paltsev <[email protected]>
---
arch/arc/Kconfig | 1 +
arch/arc/include/asm/Kbuild | 1 -
arch/arc/include/asm/ftrace.h | 16 ++++++++++++++++
arch/arc/kernel/Makefile | 10 ++++++++++
arch/arc/kernel/ftrace.c | 27 +++++++++++++++++++++++++++
5 files changed, 54 insertions(+), 1 deletion(-)
create mode 100644 arch/arc/include/asm/ftrace.h
create mode 100644 arch/arc/kernel/ftrace.c

diff --git a/arch/arc/Kconfig b/arch/arc/Kconfig
index ff2a393b635c..4b8f750bd32b 100644
--- a/arch/arc/Kconfig
+++ b/arch/arc/Kconfig
@@ -48,6 +48,7 @@ config ARC
select PCI_SYSCALL if PCI
select PERF_USE_VMALLOC if ARC_CACHE_VIPT_ALIASING
select HAVE_ARCH_JUMP_LABEL if ISA_ARCV2 && !CPU_ENDIAN_BE32
+ select HAVE_FUNCTION_TRACER if ISA_ARCV2

config ARCH_HAS_CACHE_LINE_SIZE
def_bool y
diff --git a/arch/arc/include/asm/Kbuild b/arch/arc/include/asm/Kbuild
index 1b505694691e..4e2f55bdf2ff 100644
--- a/arch/arc/include/asm/Kbuild
+++ b/arch/arc/include/asm/Kbuild
@@ -6,7 +6,6 @@ generic-y += div64.h
generic-y += dma-mapping.h
generic-y += emergency-restart.h
generic-y += extable.h
-generic-y += ftrace.h
generic-y += hardirq.h
generic-y += hw_irq.h
generic-y += irq_regs.h
diff --git a/arch/arc/include/asm/ftrace.h b/arch/arc/include/asm/ftrace.h
new file mode 100644
index 000000000000..92303e506edf
--- /dev/null
+++ b/arch/arc/include/asm/ftrace.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020 Synopsys, Inc. (http://www.synopsys.com)
+ *
+ * Author: Eugeniy Paltsev <[email protected]>
+ */
+
+#ifndef __ASM_ARC_FTRACE_H
+#define __ASM_ARC_FTRACE_H
+
+extern void _mcount(unsigned long parent_ip);
+
+/* 3 instructions 1x 16 bit + 1x 32 bit */
+#define MCOUNT_INSN_SIZE 6
+
+#endif /* __ASM_ARC_FTRACE_H */
diff --git a/arch/arc/kernel/Makefile b/arch/arc/kernel/Makefile
index 75539670431a..42c9c4b1cabd 100644
--- a/arch/arc/kernel/Makefile
+++ b/arch/arc/kernel/Makefile
@@ -22,12 +22,22 @@ obj-$(CONFIG_ARC_METAWARE_HLINK) += arc_hostlink.o
obj-$(CONFIG_PERF_EVENTS) += perf_event.o
obj-$(CONFIG_JUMP_LABEL) += jump_label.o

+
+obj-$(CONFIG_FUNCTION_TRACER) += ftrace.o
+
+ifdef CONFIG_FUNCTION_TRACER
+CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE)
+endif
+
obj-$(CONFIG_ARC_FPU_SAVE_RESTORE) += fpu.o
ifdef CONFIG_ISA_ARCOMPACT
CFLAGS_fpu.o += -mdpfp
endif

ifdef CONFIG_ARC_DW2_UNWIND
+ifdef CONFIG_FUNCTION_TRACER
+CFLAGS_REMOVE_ctx_sw.o = $(CC_FLAGS_FTRACE)
+endif
CFLAGS_ctx_sw.o += -fno-omit-frame-pointer
obj-y += ctx_sw.o
else
diff --git a/arch/arc/kernel/ftrace.c b/arch/arc/kernel/ftrace.c
new file mode 100644
index 000000000000..a61edf52bfe2
--- /dev/null
+++ b/arch/arc/kernel/ftrace.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Synopsys, Inc. (http://www.synopsys.com)
+ *
+ * Author: Eugeniy Paltsev <[email protected]>
+ */
+
+#include <linux/ftrace.h>
+
+noinline void ftrace_stub(unsigned long ip, unsigned long parent_ip,
+ struct ftrace_ops *op, struct pt_regs *regs)
+{
+ /* do notning */
+}
+
+extern void (*ftrace_trace_function)(unsigned long, unsigned long,
+ struct ftrace_ops*, struct pt_regs*);
+
+noinline void _mcount(unsigned long parent_ip)
+{
+ unsigned long ip = (unsigned long)__builtin_return_address(0);
+
+ if (unlikely(ftrace_trace_function != ftrace_stub))
+ ftrace_trace_function(ip - MCOUNT_INSN_SIZE, parent_ip,
+ NULL, NULL);
+}
+EXPORT_SYMBOL(_mcount);
--
2.21.1


2020-03-27 17:11:03

by Steven Rostedt

[permalink] [raw]
Subject: Re: [RFC] ARC: initial ftrace support

On Fri, 27 Mar 2020 18:53:55 +0300
Eugeniy Paltsev <[email protected]> wrote:

> +
> +noinline void _mcount(unsigned long parent_ip)
> +{
> + unsigned long ip = (unsigned long)__builtin_return_address(0);
> +
> + if (unlikely(ftrace_trace_function != ftrace_stub))
> + ftrace_trace_function(ip - MCOUNT_INSN_SIZE, parent_ip,
> + NULL, NULL);
> +}
> +EXPORT_SYMBOL(_mcount);

So, ARCv2 allows the _mcount code to be written in C? Nice!

-- Steve

2020-04-02 01:18:54

by Vineet Gupta

[permalink] [raw]
Subject: Re: [RFC] ARC: initial ftrace support

+CC Claudiu

On 3/27/20 10:10 AM, Steven Rostedt wrote:
> On Fri, 27 Mar 2020 18:53:55 +0300
> Eugeniy Paltsev <[email protected]> wrote:

Maybe add a comment that gcc does the heavy lifting: I have following in glibc

+/* this is very simple as gcc does all the heavy lifting at _mcount call site
+ * - sets up caller's blink in r0, so frompc is setup correctly
+ * - preserve argument registers for original call */

>> +noinline void _mcount(unsigned long parent_ip)
>> +{
>> + unsigned long ip = (unsigned long)__builtin_return_address(0);
>> +
>> + if (unlikely(ftrace_trace_function != ftrace_stub))
>> + ftrace_trace_function(ip - MCOUNT_INSN_SIZE, parent_ip,
>> + NULL, NULL);
>> +}
>> +EXPORT_SYMBOL(_mcount);
>
> So, ARCv2 allows the _mcount code to be written in C? Nice!

Yeah, the gcc backend for -pg was overhauled recently so it is a first class "lib
call" meaning we get all the register save/restore for free as well as caller PC
(blink) as explicit argument to _mcount

void bar(int a, int b, int c) {
printf("%d\n", a, b, c);
}

bar:
push_s blink
std.a r14,[sp,-8]
push_s r13
mov_s r14,r1
mov_s r13,r0
mov_s r0,blink
bl.d @_mcount
mov_s r15,r2

mov_s r3,r15 <-- restore args for call
mov_s r2,r14
mov_s r1,r13
mov_s r0,@.LC0
ld blink,[sp,12]
pop_s r13
b.d @printf
ldd.ab r14,[sp,12]

@Eugeniy, this patch looks ok to me, but a word of caution. This won't work with
elf32 toolchain which some of the build systems tend to use (Alexey ?)

The above _mcount semantics is only implemented for the linux tool-chains.
elf32-gcc generates "legacy" __mcount (2 underscores, blink not provided as arg)
likely done by Claudiu to keep newlib stuff unchanged. Perhaps elf32 gcc can add a
toggle to get new _mcount.

And this is conditional to ARCv2 due to future ties into dynamic ftrace and
instruction fudging etc ? We may have to revisit that for BE anyhow given such a
customer lining up.

-Vineet

Subject: Re: [RFC] ARC: initial ftrace support

Hi,

ARC-gcc has two modes to call the mcount routines. When using elf32
configuration, the toolchain is set to use newlib mcount. When
configured for linux, gcc toolchain is using a library call to _mcall
(single underscore) having blink as input argument.
So, using the proper linux toolchain, your patch should work.

//C

On Thu, Apr 2, 2020 at 4:17 AM Vineet Gupta <[email protected]> wrote:
>
> +CC Claudiu
>
> On 3/27/20 10:10 AM, Steven Rostedt wrote:
> > On Fri, 27 Mar 2020 18:53:55 +0300
> > Eugeniy Paltsev <[email protected]> wrote:
>
> Maybe add a comment that gcc does the heavy lifting: I have following in glibc
>
> +/* this is very simple as gcc does all the heavy lifting at _mcount call site
> + * - sets up caller's blink in r0, so frompc is setup correctly
> + * - preserve argument registers for original call */
>
> >> +noinline void _mcount(unsigned long parent_ip)
> >> +{
> >> + unsigned long ip = (unsigned long)__builtin_return_address(0);
> >> +
> >> + if (unlikely(ftrace_trace_function != ftrace_stub))
> >> + ftrace_trace_function(ip - MCOUNT_INSN_SIZE, parent_ip,
> >> + NULL, NULL);
> >> +}
> >> +EXPORT_SYMBOL(_mcount);
> >
> > So, ARCv2 allows the _mcount code to be written in C? Nice!
>
> Yeah, the gcc backend for -pg was overhauled recently so it is a first class "lib
> call" meaning we get all the register save/restore for free as well as caller PC
> (blink) as explicit argument to _mcount
>
> void bar(int a, int b, int c) {
> printf("%d\n", a, b, c);
> }
>
> bar:
> push_s blink
> std.a r14,[sp,-8]
> push_s r13
> mov_s r14,r1
> mov_s r13,r0
> mov_s r0,blink
> bl.d @_mcount
> mov_s r15,r2
>
> mov_s r3,r15 <-- restore args for call
> mov_s r2,r14
> mov_s r1,r13
> mov_s r0,@.LC0
> ld blink,[sp,12]
> pop_s r13
> b.d @printf
> ldd.ab r14,[sp,12]
>
> @Eugeniy, this patch looks ok to me, but a word of caution. This won't work with
> elf32 toolchain which some of the build systems tend to use (Alexey ?)
>
> The above _mcount semantics is only implemented for the linux tool-chains.
> elf32-gcc generates "legacy" __mcount (2 underscores, blink not provided as arg)
> likely done by Claudiu to keep newlib stuff unchanged. Perhaps elf32 gcc can add a
> toggle to get new _mcount.
>
> And this is conditional to ARCv2 due to future ties into dynamic ftrace and
> instruction fudging etc ? We may have to revisit that for BE anyhow given such a
> customer lining up.
>
> -Vineet

2020-04-02 14:21:54

by Alexey Brodkin

[permalink] [raw]
Subject: RE: [RFC] ARC: initial ftrace support

Hi Claus,

> -----Original Message-----
> From: linux-snps-arc <[email protected]> On Behalf Of Claudiu Zissulescu
> Ianculescu
> Sent: Thursday, April 2, 2020 11:10 AM
> To: Vineet Gupta <[email protected]>
> Cc: Alexey Brodkin <[email protected]>; [email protected]; Steven Rostedt
> <[email protected]>; Ingo Molnar <[email protected]>; [email protected]; Eugeniy
> Paltsev <[email protected]>
> Subject: Re: [RFC] ARC: initial ftrace support
>
> Hi,
>
> ARC-gcc has two modes to call the mcount routines. When using elf32
> configuration, the toolchain is set to use newlib mcount. When
> configured for linux, gcc toolchain is using a library call to _mcall
> (single underscore) having blink as input argument.
> So, using the proper linux toolchain, your patch should work.


Is there a chance to switch to Linux-style mcount in Elf32 toolchain with a command-line
option?

Otherwise I guess we'll need to implement some warning which explicitly says why Elf32
toolchain is not usable for building the Linux kernel... at least in case with ftrace enabled.

-Alexey

2020-04-02 19:17:45

by Steven Rostedt

[permalink] [raw]
Subject: Re: [RFC] ARC: initial ftrace support

On Thu, 2 Apr 2020 01:17:01 +0000
Vineet Gupta <[email protected]> wrote:

> +CC Claudiu
>
> On 3/27/20 10:10 AM, Steven Rostedt wrote:
> > On Fri, 27 Mar 2020 18:53:55 +0300
> > Eugeniy Paltsev <[email protected]> wrote:
>
> Maybe add a comment that gcc does the heavy lifting: I have following in glibc
>
> +/* this is very simple as gcc does all the heavy lifting at _mcount call site
> + * - sets up caller's blink in r0, so frompc is setup correctly
> + * - preserve argument registers for original call */
>
> >> +noinline void _mcount(unsigned long parent_ip)
> >> +{
> >> + unsigned long ip = (unsigned long)__builtin_return_address(0);
> >> +
> >> + if (unlikely(ftrace_trace_function != ftrace_stub))
> >> + ftrace_trace_function(ip - MCOUNT_INSN_SIZE, parent_ip,
> >> + NULL, NULL);
> >> +}
> >> +EXPORT_SYMBOL(_mcount);
> >
> > So, ARCv2 allows the _mcount code to be written in C? Nice!
>
> Yeah, the gcc backend for -pg was overhauled recently so it is a first class "lib
> call" meaning we get all the register save/restore for free as well as caller PC
> (blink) as explicit argument to _mcount
>
> void bar(int a, int b, int c) {
> printf("%d\n", a, b, c);
> }
>
> bar:
> push_s blink
> std.a r14,[sp,-8]
> push_s r13
> mov_s r14,r1
> mov_s r13,r0
> mov_s r0,blink
> bl.d @_mcount
> mov_s r15,r2
>
> mov_s r3,r15 <-- restore args for call

We really don't want this. :-/

This will make it really difficult to implement the dynamic ftrace, and
this causes more overhead when tracing is not enabled.

Also, ftrace is much more complex, and this will make it difficult to have
function graph tracing and other features.

Gcc has an "instrument-functions" which people asked me why I didn't go
that route, as it lets you do the same (call C code), and its because of
the overhead it adds to each function that I turned it down.

-- Steve



> mov_s r2,r14
> mov_s r1,r13
> mov_s r0,@.LC0
> ld blink,[sp,12]
> pop_s r13
> b.d @printf
> ldd.ab r14,[sp,12]
>
> @Eugeniy, this patch looks ok to me, but a word of caution. This won't work with
> elf32 toolchain which some of the build systems tend to use (Alexey ?)
>
> The above _mcount semantics is only implemented for the linux tool-chains.
> elf32-gcc generates "legacy" __mcount (2 underscores, blink not provided as arg)
> likely done by Claudiu to keep newlib stuff unchanged. Perhaps elf32 gcc can add a
> toggle to get new _mcount.
>
> And this is conditional to ARCv2 due to future ties into dynamic ftrace and
> instruction fudging etc ? We may have to revisit that for BE anyhow given such a
> customer lining up.
>
> -Vineet

2020-05-14 13:11:54

by Eugeniy Paltsev

[permalink] [raw]
Subject: Re: [RFC] ARC: initial ftrace support

Hi Claudiu,
ping!

Is is possible to switch between mcount modes (Linux-style <-> baremetal-style) via command-line options for ARC GCC?

>From: Alexey Brodkin <[email protected]>
>Sent: Thursday, April 2, 2020 17:15
>To: Claudiu Zissulescu Ianculescu
>Cc: [email protected]; Steven Rostedt; Ingo Molnar; [email protected]; Eugeniy Paltsev; Vineet Gupta
>Subject: RE: [RFC] ARC: initial ftrace support
>
> Hi Claus,
>
>> -----Original Message-----
>> From: linux-snps-arc <[email protected]> On Behalf Of Claudiu Zissulescu
>> Ianculescu
>> Sent: Thursday, April 2, 2020 11:10 AM
>> To: Vineet Gupta <[email protected]>
>> Cc: Alexey Brodkin <[email protected]>; [email protected]; Steven Rostedt
>> <[email protected]>; Ingo Molnar <[email protected]>; [email protected]; Eugeniy
>> Paltsev <[email protected]>
>> Subject: Re: [RFC] ARC: initial ftrace support
>>
>> Hi,
>>
>> ARC-gcc has two modes to call the mcount routines. When using elf32
>> configuration, the toolchain is set to use newlib mcount. When
>> configured for linux, gcc toolchain is using a library call to _mcall
>> (single underscore) having blink as input argument.
>> So, using the proper linux toolchain, your patch should work.
>
>
> Is there a chance to switch to Linux-style mcount in Elf32 toolchain with a command-line
> option?
>
> Otherwise I guess we'll need to implement some warning which explicitly says why Elf32
> toolchain is not usable for building the Linux kernel... at least in case with ftrace enabled.
>
>-Alexey