ARC: add missing bits for better BPF support
Subsystems (3)
cover ARC: add missing bits for better BPF support
Hi Vineet and all,
This patch set implements the following two features for ARC:
- HAVE_REGS_AND_STACK_ACCESS_API
- HAVE_SYSCALL_TRACEPOINTS
Adding these features enables more BPF programs for ARC kernels, including KPROBE, TRACEPOINT, PERF_EVENT program types.
Regards,
Sergey
Sergey Matyukevich (4):
ARC: bpf: define uapi for BPF_PROG_TYPE_PERF_EVENT program type
ARC: enable HAVE_REGS_AND_STACK_ACCESS_API feature
ARC: entry: fix syscall_trace_exit argument
ARC: implement syscall tracepoints
arch/arc/Kconfig | 2 + arch/arc/include/asm/perf_event.h | 4 + arch/arc/include/asm/ptrace.h | 27 ++++ arch/arc/include/asm/syscall.h | 2 + arch/arc/include/asm/thread_info.h | 5 +- arch/arc/include/uapi/asm/bpf_perf_event.h | 9 ++ arch/arc/kernel/entry.S | 13 +- arch/arc/kernel/ptrace.c | 140 ++++++++++++++++++++- 8 files changed, 192 insertions(+), 10 deletions(-) create mode 100644 arch/arc/include/uapi/asm/bpf_perf_event.h
Signature · 7 lines
2.35.1 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc
PATCH 1/4 ARC: bpf: define uapi for BPF_PROG_TYPE_PERF_EVENT program type
From: Sergey Matyukevich <[email protected]>
Define appropriate uapi for the BPF_PROG_TYPE_PERF_EVENT program type by exporting the user_regs_struct structure instead of the pt_regs structure that is in-kernel only.
--- arch/arc/include/asm/perf_event.h | 4 ++++ arch/arc/include/uapi/asm/bpf_perf_event.h | 9 +++++++++ 2 files changed, 13 insertions(+) create mode 100644 arch/arc/include/uapi/asm/bpf_perf_event.h
arch/arc/include/asm/perf_event.h +4 -0
@@ -63,4 +63,8 @@ struct arc_reg_cc_build {#define PERF_COUNT_ARC_HW_MAX (PERF_COUNT_HW_MAX + 8) +#ifdef CONFIG_PERF_EVENTS +#define perf_arch_bpf_user_pt_regs (regs) (struct user_regs_struct *)regs +#endif +#endif /* __ASM_PERF_EVENT_H */
arch/arc/include/uapi/asm/bpf_perf_event.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI__ASM_BPF_PERF_EVENT_H__ +#define _UAPI__ASM_BPF_PERF_EVENT_H__ + +#include <asm/ptrace.h> + +typedef struct user_regs_struct bpf_user_pt_regs_t ; + +#endif /* _UAPI__ASM_BPF_PERF_EVENT_H__ */
Signature · 7 lines
2.35.1 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc
PATCH 2/4 ARC: enable HAVE_REGS_AND_STACK_ACCESS_API feature
In reply to Sergey MatyukevichFrom: Sergey Matyukevich <[email protected]>
Enable HAVE_REGS_AND_STACK_ACCESS_API feature for ARC architecture, including ARCcompact and ARCv2 flavors. Add supporting functions and defines.
--- arch/arc/Kconfig | 1 + arch/arc/include/asm/ptrace.h | 27 ++++++++ arch/arc/kernel/ptrace.c | 119 ++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+)
arch/arc/Kconfig +1 -0
@@ -36,6 +36,7 @@ config ARC select HAVE_KERNEL_LZMA select HAVE_KPROBES select HAVE_KRETPROBES + select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_MOD_ARCH_SPECIFIC select HAVE_PERF_EVENTS select IRQ_DOMAIN
arch/arc/include/asm/ptrace.h +27 -0
@@ -8,6 +8,7 @@#define __ASM_ARC_PTRACE_H#include <uapi/asm/ptrace.h> +#include <linux/compiler.h> #ifndef __ASSEMBLY__
@@ -54,6 +55,9 @@ struct pt_regs {unsigned long user_r25; }; + +#define MAX_REG_OFFSET offsetof(struct pt_regs, user_r25) +#else struct pt_regs {
@@ -102,6 +106,8 @@ struct pt_regs { unsigned long status32; }; +#define MAX_REG_OFFSET offsetof(struct pt_regs, status32) +#endif /* Callee saved registers - need to be saved only when you are scheduled out */
@@ -154,6 +160,27 @@ static inline void instruction_pointer_set(struct pt_regs *regs, {instruction_pointer (regs) = val; } + +static inline unsigned long kernel_stack_pointer (struct pt_regs *regs) +{ +return regs->sp; +} + +extern int regs_query_register_offset (const char *name); +extern const char *regs_query_register_name (unsigned int offset); +extern bool regs_within_kernel_stack (struct pt_regs *regs,unsigned long addr); +extern unsigned long regs_get_kernel_stack_nth (struct pt_regs *regs, +unsigned int n); + +static inline unsigned long regs_get_register (struct pt_regs *regs, +unsigned int offset) +{ +if (unlikely (offset >MAX_REG_OFFSET )) +return 0 ; + +return *(unsigned long *)((unsigned long )regs + offset); +} +#endif /* !__ASSEMBLY__ */ #endif /* __ASM_PTRACE_H */
arch/arc/kernel/ptrace.c +119 -0
128 lines not on this page · raw message
Signature · 7 lines
2.35.1 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc
PATCH 3/4 ARC: entry: fix syscall_trace_exit argument
In reply to Sergey MatyukevichFrom: Sergey Matyukevich <[email protected]>
Function syscall_trace_exit expects pointer to pt_regs. However r0 is also used to keep syscall return value. Restore pointer to pt_regs before calling syscall_trace_exit.
--- arch/arc/kernel/entry.S | 1 + 1 file changed, 1 insertion(+)
arch/arc/kernel/entry.S +1 -0
@@ -196,6 +196,7 @@ tracesys_exit: st r0, [sp, PT_r0] ; sys call return value in pt_regs ;POST Sys Call Ptrace Hook + mov r0, sp ; pt_regs needed bl @syscall_trace_exit b ret_from_exception ; NOT ret_from_system_call at is saves r0 which ; we'd done before calling post hook above
Signature · 7 lines
2.35.1 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc
PATCH 4/4 ARC: implement syscall tracepoints
In reply to Sergey MatyukevichFrom: Sergey Matyukevich <[email protected]>
Implement all the bits required to support HAVE_SYSCALL_TRACEPOINTS according to Documentation/trace/ftrace-design.rst.
--- arch/arc/Kconfig | 1 + arch/arc/include/asm/syscall.h | 2 ++ arch/arc/include/asm/thread_info.h | 5 ++++- arch/arc/kernel/entry.S | 12 ++++++------ arch/arc/kernel/ptrace.c | 21 ++++++++++++++++++--- 5 files changed, 31 insertions(+), 10 deletions(-)
arch/arc/Kconfig +1 -0
@@ -39,6 +39,7 @@ config ARC select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_MOD_ARCH_SPECIFIC select HAVE_PERF_EVENTS + select HAVE_SYSCALL_TRACEPOINTS select IRQ_DOMAIN select MODULES_USE_ELF_RELA select OF
arch/arc/include/asm/syscall.h +2 -0
@@ -12,6 +12,8 @@#include <asm/unistd.h> #include <asm/ptrace.h> /* in_syscall() */ +extern void *sys_call_table[]; +static inline long syscall_get_nr (struct task_struct *task,struct pt_regs *regs) {
arch/arc/include/asm/thread_info.h +4 -1
@@ -78,9 +78,9 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)#define TIF_SYSCALL_AUDIT 4/* syscall auditing active */ #define TIF_NOTIFY_SIGNAL 5/* signal notifications exist */ #define TIF_SYSCALL_TRACE 15/* syscall trace active */ -/* true if poll_idle() is polling TIF_NEED_RESCHED */ #define TIF_MEMDIE 16 +#define TIF_SYSCALL_TRACEPOINT 17/* syscall tracepoint instrumentation */ #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
@@ -89,11 +89,14 @@ static inline __attribute_const__ struct thread_info *current_thread_info(void)#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)#define _TIF_NOTIFY_SIGNAL (1<<TIF_NOTIFY_SIGNAL)#define _TIF_MEMDIE (1<<TIF_MEMDIE) +#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)/* work to do on interrupt/exception return */ #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \ _TIF_NOTIFY_RESUME | _TIF_NOTIFY_SIGNAL) +#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_TRACEPOINT) +/* * _TIF_ALLWORK_MASK includes SYSCALL_TRACE, but we don't need it. * SYSCALL_TRACE is anyway separately/unconditionally tested right after a
arch/arc/kernel/entry.S +6 -6
@@ -29,8 +29,8 @@ ENTRY(sys_clone_wrapper) DISCARD_CALLEE_SAVED_USER GET_CURR_THR_INFO_FLAGS r10 - btst r10, TIF_SYSCALL_TRACE - bnz tracesys_exit + and.f 0, r10, _TIF_SYSCALL_WORK + bnz tracesys_exit b .Lret_from_system_call END(sys_clone_wrapper)
@@ -41,8 +41,8 @@ ENTRY(sys_clone3_wrapper) DISCARD_CALLEE_SAVED_USER GET_CURR_THR_INFO_FLAGS r10 - btst r10, TIF_SYSCALL_TRACE - bnz tracesys_exit + and.f 0, r10, _TIF_SYSCALL_WORK + bnz tracesys_exit b .Lret_from_system_call END(sys_clone3_wrapper)
@@ -247,8 +247,8 @@ ENTRY(EV_Trap) ; If syscall tracing ongoing, invoke pre-post-hooks GET_CURR_THR_INFO_FLAGS r10 - btst r10, TIF_SYSCALL_TRACE - bnz tracesys ; this never comes back + and.f 0, r10, _TIF_SYSCALL_WORK + bnz tracesys ; this never comes back ;============ Normal syscall case
arch/arc/kernel/ptrace.c +18 -3
@@ -9,6 +9,9 @@#include <linux/unistd.h> #include <linux/elf.h> +#define CREATE_TRACE_POINTS +#include <trace/events/syscalls.h> +struct pt_regs_offset {const char *name;int offset;
@@ -340,15 +343,27 @@ long arch_ptrace(struct task_struct *child, long request, asmlinkage int syscall_trace_entry(struct pt_regs *regs) { -if (ptrace_report_syscall_entry (regs)) -return ULONG_MAX ; +if (test_thread_flag (TIF_SYSCALL_TRACE )) +if (ptrace_report_syscall_entry (regs)) +return ULONG_MAX ; + +#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS +if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) +trace_sys_enter (regs,syscall_get_nr (current, regs)); +#endif return regs->r8; } asmlinkage void syscall_trace_exit(struct pt_regs *regs) { -ptrace_report_syscall_exit (regs,0 ); +if (test_thread_flag (TIF_SYSCALL_TRACE )) +ptrace_report_syscall_exit (regs,0 ); + +#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS +if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) +trace_sys_exit (regs,regs_return_value (regs)); +#endif }int regs_query_register_offset (const char *name)
Signature · 7 lines
2.35.1 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc