Skip to contentSkip to conversation

fprobe: Introduce fprobe function entry/exit probe

RFC PATCH Superseded Masami Hiramatsu 3 participants 21 messages Jan 19, 2022 – Jan 25, 2022

Subsystems (12)
Networking Network drivers x86 BPF +8 more subsystems

v3 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v3 cover fprobe: Introduce fprobe function entry/exit probe

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

Thank you,

---

Jiri Olsa (2):
ftrace: Add ftrace_set_filter_ips function
bpf: Add kprobe link for attaching raw kprobes

Masami Hiramatsu (7):
fprobe: Add ftrace based probe APIs
rethook: Add a generic return hook
rethook: x86: Add rethook x86 implementation
fprobe: Add exit_handler support
fprobe: Add sample program for fprobe
[DO NOT MERGE] Out-of-tree: Support wildcard symbol option to sample [DO NOT MERGE] out-of-tree: kprobes: Use rethook for kretprobe

 arch/x86/Kconfig                |    1 
 arch/x86/include/asm/unwind.h   |    8 +
 arch/x86/kernel/Makefile        |    1 
 arch/x86/kernel/kprobes/core.c  |  106 --------------
 arch/x86/kernel/rethook.c       |  115 +++++++++++++++
 include/linux/bpf_types.h       |    1 
 include/linux/fprobe.h          |   84 +++++++++++
 include/linux/ftrace.h          |    3 
 include/linux/kprobes.h         |   85 +----------
 include/linux/rethook.h         |   99 +++++++++++++
 include/linux/sched.h           |    4 -
 include/uapi/linux/bpf.h        |   12 ++
 kernel/bpf/syscall.c            |  195 +++++++++++++++++++++++++-
 kernel/exit.c                   |    3 
 kernel/fork.c                   |    4 -
 kernel/kallsyms.c               |    1 
 kernel/kprobes.c                |  265 +++++------------------------------
 kernel/trace/Kconfig            |   22 +++
 kernel/trace/Makefile           |    2 
 kernel/trace/fprobe.c           |  179 ++++++++++++++++++++++++
 kernel/trace/ftrace.c           |   54 ++++++-
 kernel/trace/rethook.c          |  295 +++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c     |    4 -
 kernel/trace/trace_output.c     |    2 
 samples/Kconfig                 |    7 +
 samples/Makefile                |    1 
 samples/fprobe/Makefile         |    3 
 samples/fprobe/fprobe_example.c |  154 ++++++++++++++++++++
 tools/include/uapi/linux/bpf.h  |   12 ++
 29 files changed, 1283 insertions(+), 439 deletions(-)
 create mode 100644 arch/x86/kernel/rethook.c
 create mode 100644 include/linux/fprobe.h
 create mode 100644 include/linux/rethook.h
 create mode 100644 kernel/trace/fprobe.c
 create mode 100644 kernel/trace/rethook.c
 create mode 100644 samples/fprobe/Makefile
 create mode 100644 samples/fprobe/fprobe_example.c

--
Masami Hiramatsu (Linaro) <[email protected]>

author maintainer
#1 lore raw

RFC PATCH v3 1/9 ftrace: Add ftrace_set_filter_ips function

From: Jiri Olsa <[email protected]>

Adding ftrace_set_filter_ips function to be able to set filter on multiple ip addresses at once.

With the kprobe multi attach interface we have cases where we need to initialize ftrace_ops object with thousands of functions, so having single function diving into ftrace_hash_move_and_update_ops with ftrace_lock is faster.

The functions ips are passed as unsigned long array with count.

---
 include/linux/ftrace.h |    3 +++
 kernel/trace/ftrace.c  |   53 ++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/ftrace.h +3 -0
index 9999e29187de..60847cbce0da 100644
@@ -512,6 +512,8 @@ struct dyn_ftrace {
 
 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
 			 int remove, int reset);
+int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
+			  unsigned int cnt, int remove, int reset);
 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
 		       int len, int reset);
 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
@@ -802,6 +804,7 @@ static inline unsigned long ftrace_location(unsigned long ip)
 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
+#define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
 #define ftrace_free_filter(ops) do { } while (0)
kernel/trace/ftrace.c +44 -9

95 lines not on this page · raw message

author maintainer
#2 lore raw

RFC PATCH v3 2/9 fprobe: Add ftrace based probe APIs

In reply to Masami Hiramatsu

The fprobe is a wrapper API for ftrace function tracer. Unlike kprobes, this probes only supports the function entry, but it can probe multiple functions by one fprobe. The usage is almost same as the kprobe, user will specify the function names by fprobe::syms, the number of syms by fprobe::nentry,
and the user handler by fprobe::entry_handler.

struct fprobe fp = { 0 };
const char *targets[] = { "func1", "func2", "func3"};

fp.handler = user_handler;
fp.nentry = ARRAY_SIZE(targets);
fp.syms = targets;

ret = register_fprobe(&fp);

---
Changes in v3:
- Update kerneldocs.

---
 include/linux/fprobe.h |   80 +++++++++++++++++++++++++++++++++
 kernel/trace/Kconfig   |   10 ++++
 kernel/trace/Makefile  |    1 
 kernel/trace/fprobe.c  |  117 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 208 insertions(+)
 create mode 100644 include/linux/fprobe.h
 create mode 100644 kernel/trace/fprobe.c
include/linux/fprobe.h new file +80 -0
new file mode 100644
index 000000000000..2fc487d933e3
@@ -0,0 +1,80 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Simple ftrace probe wrapper */
+#ifndef _LINUX_FPROBE_H
+#define _LINUX_FPROBE_H
+
+#include <linux/compiler.h>
+#include <linux/ftrace.h>
+
+/**
+ * struct fprobe - ftrace based probe.
+ * @syms: The array of symbols to probe.
+ * @addrs: The array of address of the symbols.
+ * @nentry: The number of entries of @syms or @addrs.
+ * @ftrace: The ftrace_ops.
+ * @nmissed: The counter for missing events.
+ * @flags: The status flag.
+ * @entry_handler: The callback function for function entry.
+ *
+ * User must set either @syms or @addrs, but not both. If user sets
+ * only @syms, the @addrs are generated when registering the fprobe.
+ * That auto-generated @addrs will be freed when unregistering.
+ */
+struct fprobe {
+	const char		**syms;
+	unsigned long		*addrs;
+	unsigned int		nentry;
+
+	struct ftrace_ops	ftrace;
+	unsigned long		nmissed;
+	unsigned int		flags;
+	void (*entry_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
+};
+
+#define FPROBE_FL_DISABLED	1
+
+static inline bool fprobe_disabled(struct fprobe *fp)
+{
+	return (fp) ? fp->flags & FPROBE_FL_DISABLED : false;
+}
+
+#ifdef CONFIG_FPROBE
+int register_fprobe(struct fprobe *fp);
+int unregister_fprobe(struct fprobe *fp);
+#else
+static inline int register_fprobe(struct fprobe *fp)
+{
+	return -EOPNOTSUPP;
+}
+static inline int unregister_fprobe(struct fprobe *fp)
+{
+	return -EOPNOTSUPP;
+}
+#endif
+
+/**
+ * disable_fprobe() - Disable fprobe
+ * @fp: The fprobe to be disabled.
+ *
+ * This will soft-disable @fp. Note that this doesn't remove the ftrace
+ * hooks from the function entry.
+ */
+static inline void disable_fprobe(struct fprobe *fp)
+{
+	if (fp)
+		fp->flags |= FPROBE_FL_DISABLED;
+}
+
+/**
+ * enable_fprobe() - Enable fprobe
+ * @fp: The fprobe to be enabled.
+ *
+ * This will soft-enable @fp.
+ */
+static inline void enable_fprobe(struct fprobe *fp)
+{
+	if (fp)
+		fp->flags &= ~FPROBE_FL_DISABLED;
+}
+
+#endif
kernel/trace/Kconfig +10 -0
index 420ff4bc67fd..6834b0272798 100644
@@ -223,6 +223,16 @@ config DYNAMIC_FTRACE_WITH_ARGS
 	depends on DYNAMIC_FTRACE
 	depends on HAVE_DYNAMIC_FTRACE_WITH_ARGS
 
+config FPROBE
+	bool "Kernel Function Probe (fprobe)"
+	depends on FUNCTION_TRACER
+	depends on DYNAMIC_FTRACE_WITH_REGS
+	default n
+	help
+	  This option enables kernel function probe feature, which is
+	  similar to kprobes, but probes only for kernel function entries
+	  and it can probe multiple functions by one fprobe.
+
 config FUNCTION_PROFILER
 	bool "Kernel function profiler"
 	depends on FUNCTION_TRACER
kernel/trace/Makefile +1 -0
index bedc5caceec7..79255f9de9a4 100644
@@ -97,6 +97,7 @@ obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
 obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o
 obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o
 obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o
+obj-$(CONFIG_FPROBE) += fprobe.o
 
 obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o
 
kernel/trace/fprobe.c new file +117 -0

117 lines not on this page · raw message

author maintainer
#3 lore raw

RFC PATCH v3 3/9 rethook: Add a generic return hook

In reply to Masami Hiramatsu

Add a return hook framework which hooks the function return. Most of the idea came from the kretprobe, but this is independent from kretprobe.
Note that this is expected to be used with other function entry hooking feature, like ftrace, fprobe, adn kprobes. Eventually this will replace the kretprobe (e.g. kprobe + rethook = kretprobe), but at this moment, this is just a additional hook.

---
Changes in v3:
- Strictly check the rethook::handler.
- Protect the handler callback loop from rcu.
- Add some comments and kernel docs.
- Change some function name.

---
 include/linux/rethook.h |   99 ++++++++++++++++
 include/linux/sched.h   |    3 
 kernel/exit.c           |    2 
 kernel/fork.c           |    3 
 kernel/trace/Kconfig    |   11 ++
 kernel/trace/Makefile   |    1 
 kernel/trace/rethook.c  |  295 +++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 414 insertions(+)
 create mode 100644 include/linux/rethook.h
 create mode 100644 kernel/trace/rethook.c
include/linux/rethook.h new file +99 -0
new file mode 100644
index 000000000000..39cfbff1a03c
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Return hooking with list-based shadow stack.
+ */
+#ifndef _LINUX_RETHOOK_H
+#define _LINUX_RETHOOK_H
+
+#include <linux/compiler.h>
+#include <linux/freelist.h>
+#include <linux/llist.h>
+#include <linux/rcupdate.h>
+#include <linux/refcount.h>
+
+struct rethook_node;
+
+typedef void (*rethook_handler_t) (struct rethook_node *, void *, struct pt_regs *);
+
+/**
+ * struct rethook - The rethook management data structure.
+ * @data: The user-defined data storage.
+ * @handler: The user-defined return hook handler.
+ * @pool: The pool of struct rethook_node.
+ * @ref: The reference counter.
+ * @rcu: The rcu_head for deferred freeing.
+ *
+ * Don't embed to another data structure, because this is a self-destructive
+ * data structure when all rethook_node are freed.
+ */
+struct rethook {
+	void			*data;
+	rethook_handler_t	handler;
+	struct freelist_head	pool;
+	refcount_t		ref;
+	struct rcu_head		rcu;
+};
+
+/**
+ * struct rethook_node - The rethook shadow-stack entry node.
+ * @freelist: The freelist, linked to struct rethook::pool.
+ * @rcu: The rcu_head for deferred freeing.
+ * @llist: The llist, linked to a struct task_struct::rethooks.
+ * @rethook: The pointer to the struct rethook.
+ * @ret_addr: The storage for the real return address.
+ * @frame: The stroage for the frame pointer.
+ *
+ * You can embed this with your extended data structure to store any data
+ * on the entry of shadow stack.
+ */
+struct rethook_node {
+	union {
+		struct freelist_node freelist;
+		struct rcu_head      rcu;
+	};
+	struct llist_node	llist;
+	struct rethook		*rethook;
+	unsigned long		ret_addr;
+	unsigned long		frame;
+};
+
+struct rethook *rethook_alloc(void *data, rethook_handler_t handler);
+void rethook_free(struct rethook *rh);
+void rethook_add_node(struct rethook *rh, struct rethook_node *node);
+struct rethook_node *rethook_try_get(struct rethook *rh);
+void rethook_recycle(struct rethook_node *node);
+void rethook_hook(struct rethook_node *node, struct pt_regs *regs);
+unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame,
+				    struct llist_node **cur);
+
+/* Arch dependent code must implement arch_* and trampoline code */
+void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs);
+void arch_rethook_trampoline(void);
+
+/**
+ * is_rethook_trampoline() - Check whether the address is rethook trampoline
+ * @addr: The address to be checked
+ *
+ * Return true if the @addr is rethook trampoline address.
+ */
+static inline bool is_rethook_trampoline(unsigned long addr)
+{
+	return addr == (unsigned long)arch_rethook_trampoline;
+}
+
+/* If the architecture needs a fixup the return address, implement it. */
+void arch_rethook_fixup_return(struct pt_regs *regs,
+			       unsigned long correct_ret_addr);
+
+/* Generic trampoline handler, arch code must prepare asm stub */
+unsigned long rethook_trampoline_handler(struct pt_regs *regs,
+					 unsigned long frame);
+
+#ifdef CONFIG_RETHOOK
+void rethook_flush_task(struct task_struct *tk);
+#else
+#define rethook_flush_task(tsk)	do { } while (0)
+#endif
+
+#endif
+
include/linux/sched.h +3 -0
index 78c351e35fec..2bfabf5355b7 100644
@@ -1473,6 +1473,9 @@ struct task_struct {
 #ifdef CONFIG_KRETPROBES
 	struct llist_head               kretprobe_instances;
 #endif
+#ifdef CONFIG_RETHOOK
+	struct llist_head               rethooks;
+#endif
 
 #ifdef CONFIG_ARCH_HAS_PARANOID_L1D_FLUSH
 	/*
kernel/exit.c +2 -0
index f702a6a63686..a39a321c1f37 100644
@@ -64,6 +64,7 @@
 #include <linux/compat.h>
 #include <linux/io_uring.h>
 #include <linux/kprobes.h>
+#include <linux/rethook.h>
 
 #include <linux/uaccess.h>
 #include <asm/unistd.h>
@@ -169,6 +170,7 @@ static void delayed_put_task_struct(struct rcu_head *rhp)
 	struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
 
 	kprobe_flush_task(tsk);
+	rethook_flush_task(tsk);
 	perf_event_delayed_put(tsk);
 	trace_sched_process_free(tsk);
 	put_task_struct(tsk);
kernel/fork.c +3 -0
index 3244cc56b697..ffae38be64c4 100644
@@ -2282,6 +2282,9 @@ static __latent_entropy struct task_struct *copy_process(
 #ifdef CONFIG_KRETPROBES
 	p->kretprobe_instances.first = NULL;
 #endif
+#ifdef CONFIG_RETHOOK
+	p->rethooks.first = NULL;
+#endif
 
 	/*
 	 * Ensure that the cgroup subsystem policies allow the new process to be
kernel/trace/Kconfig +11 -0
index 6834b0272798..44c473ad9021 100644
@@ -10,6 +10,17 @@ config USER_STACKTRACE_SUPPORT
 config NOP_TRACER
 	bool
 
+config HAVE_RETHOOK
+	bool
+
+config RETHOOK
+	bool
+	depends on HAVE_RETHOOK
+	help
+	  Enable generic return hooking feature. This is an internal
+	  API, which will be used by other function-entry hooking
+	  feature like fprobe and kprobes.
+
 config HAVE_FUNCTION_TRACER
 	bool
 	help
kernel/trace/Makefile +1 -0
index 79255f9de9a4..c6f11a139eac 100644
@@ -98,6 +98,7 @@ obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o
 obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o
 obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o
 obj-$(CONFIG_FPROBE) += fprobe.o
+obj-$(CONFIG_RETHOOK) += rethook.o
 
 obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o
 
kernel/trace/rethook.c new file +295 -0

295 lines not on this page · raw message

author maintainer
#4 lore raw

RFC PATCH v3 4/9 rethook: x86: Add rethook x86 implementation

In reply to Masami Hiramatsu

Add rethook for x86 implementation. Most of the code has been copied from kretprobes on x86.

---
 arch/x86/Kconfig          |    1 
 arch/x86/kernel/Makefile  |    1 
 arch/x86/kernel/rethook.c |  115 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 arch/x86/kernel/rethook.c
arch/x86/Kconfig +1 -0
index 5c2ccb85f2ef..0a7d48a63787 100644
@@ -219,6 +219,7 @@ config X86
 	select HAVE_KPROBES_ON_FTRACE
 	select HAVE_FUNCTION_ERROR_INJECTION
 	select HAVE_KRETPROBES
+	select HAVE_RETHOOK
 	select HAVE_KVM
 	select HAVE_LIVEPATCH			if X86_64
 	select HAVE_MIXED_BREAKPOINTS_REGS
arch/x86/kernel/Makefile +1 -0
index 2ff3e600f426..66593d8c4d74 100644
@@ -106,6 +106,7 @@ obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
 obj-$(CONFIG_FTRACE_SYSCALLS)	+= ftrace.o
 obj-$(CONFIG_X86_TSC)		+= trace_clock.o
 obj-$(CONFIG_TRACING)		+= trace.o
+obj-$(CONFIG_RETHOOK)		+= rethook.o
 obj-$(CONFIG_CRASH_CORE)	+= crash_core_$(BITS).o
 obj-$(CONFIG_KEXEC_CORE)	+= machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC_CORE)	+= relocate_kernel_$(BITS).o crash.o
arch/x86/kernel/rethook.c new file +115 -0

115 lines not on this page · raw message

author maintainer
#5 lore raw

RFC PATCH v3 5/9 fprobe: Add exit_handler support

In reply to Masami Hiramatsu

Add exit_handler to fprobe. fprobe + rethook allows us to hook the kernel function return without fgraph tracer. Eventually, the fgraph tracer will be generic array based return hooking and fprobe may use it if user requests. Since both array-based approach and list-based approach have Pros and Cons, (e.g. memory consumption v.s. less missing events) it is better to keep both but fprobe will provide the same exit-handler interface.

---
Changes in v3:
- Make sure to clear rethook->data before free.
- Handler checks the data is not NULL.
- Free rethook only if the rethook is using.

---
 include/linux/fprobe.h |    4 +++
 kernel/trace/Kconfig   |    1 +
 kernel/trace/fprobe.c  |   66 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 2 deletions(-)
include/linux/fprobe.h +4 -0
index 2fc487d933e3..dd9d65294552 100644
@@ -5,6 +5,7 @@
 
 #include <linux/compiler.h>
 #include <linux/ftrace.h>
+#include <linux/rethook.h>
 
 /**
  * struct fprobe - ftrace based probe.
@@ -28,7 +29,10 @@ struct fprobe {
 	struct ftrace_ops	ftrace;
 	unsigned long		nmissed;
 	unsigned int		flags;
+	struct rethook		*rethook;
+
 	void (*entry_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
+	void (*exit_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
 };
 
 #define FPROBE_FL_DISABLED	1
kernel/trace/Kconfig +1 -0
index 44c473ad9021..00bdd2a2f417 100644
@@ -238,6 +238,7 @@ config FPROBE
 	bool "Kernel Function Probe (fprobe)"
 	depends on FUNCTION_TRACER
 	depends on DYNAMIC_FTRACE_WITH_REGS
+	select RETHOOK
 	default n
 	help
 	  This option enables kernel function probe feature, which is
kernel/trace/fprobe.c +64 -2

117 lines not on this page · raw message

author maintainer
#6 lore raw

RFC PATCH v3 6/9 fprobe: Add sample program for fprobe

In reply to Masami Hiramatsu

Add a sample program for the fprobe.

---
Changes in v2:
- Fix infinit loop for multiple symbols.
- Fix memory leaks for copied string and entry array.
- Update for new fprobe APIs.
- Fix style issues.

---
 samples/Kconfig                 |    7 +++
 samples/Makefile                |    1 
 samples/fprobe/Makefile         |    3 +
 samples/fprobe/fprobe_example.c |  103 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 114 insertions(+)
 create mode 100644 samples/fprobe/Makefile
 create mode 100644 samples/fprobe/fprobe_example.c
samples/Kconfig +7 -0
index 43d2e9aa557f..e010c2c1256c 100644
@@ -73,6 +73,13 @@ config SAMPLE_HW_BREAKPOINT
 	help
 	  This builds kernel hardware breakpoint example modules.
 
+config SAMPLE_FPROBE
+	tristate "Build fprobe examples -- loadable modules only"
+	depends on FPROBE && m
+	help
+	  This builds a fprobe example module. This module has an option 'symbol'.
+	  You can specify a probed symbol or symbols separated with ','.
+
 config SAMPLE_KFIFO
 	tristate "Build kfifo examples -- loadable modules only"
 	depends on m
samples/Makefile +1 -0
index 4bcd6b93bffa..4f73fe7aa473 100644
@@ -32,3 +32,4 @@ obj-$(CONFIG_SAMPLE_INTEL_MEI)		+= mei/
 subdir-$(CONFIG_SAMPLE_WATCHDOG)	+= watchdog
 subdir-$(CONFIG_SAMPLE_WATCH_QUEUE)	+= watch_queue
 obj-$(CONFIG_DEBUG_KMEMLEAK_TEST)	+= kmemleak/
+obj-$(CONFIG_SAMPLE_FPROBE)		+= fprobe/
samples/fprobe/Makefile new file +3 -0
new file mode 100644
index 000000000000..ecccbfa6e99b
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_SAMPLE_FPROBE) += fprobe_example.o
samples/fprobe/fprobe_example.c new file +103 -0

103 lines not on this page · raw message

author maintainer
#7 lore raw

RFC PATCH v3 7/9 bpf: Add kprobe link for attaching raw kprobes

In reply to Masami Hiramatsu

From: Jiri Olsa <[email protected]>

Adding new link type BPF_LINK_TYPE_KPROBE to attach so called "kprobes" directly through fprobe API. Note that since the using kprobes with multiple same handler is not efficient, this uses the fprobe which natively support multiple probe points for one same handler, but limited on function entry and exit.

Adding new attach type BPF_TRACE_RAW_KPROBE that enables such link for kprobe program.

The new link allows to create multiple kprobes link by using new link_create interface:

  struct {
    __aligned_u64   addrs;
    __u32           cnt;
    __u64           bpf_cookie;
  } kprobe;

Plus new flag BPF_F_KPROBE_RETURN for link_create.flags to create return probe.

---
 include/linux/bpf_types.h      |    1 
 include/uapi/linux/bpf.h       |   12 ++
 kernel/bpf/syscall.c           |  195 +++++++++++++++++++++++++++++++++++++++-
 tools/include/uapi/linux/bpf.h |   12 ++
 4 files changed, 215 insertions(+), 5 deletions(-)
include/linux/bpf_types.h +1 -0
index 48a91c51c015..a9000feab34e 100644
@@ -140,3 +140,4 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp)
 #ifdef CONFIG_PERF_EVENTS
 BPF_LINK_TYPE(BPF_LINK_TYPE_PERF_EVENT, perf)
 #endif
+BPF_LINK_TYPE(BPF_LINK_TYPE_KPROBE, kprobe)
include/uapi/linux/bpf.h +12 -0
index b0383d371b9a..5216b333c688 100644
@@ -995,6 +995,7 @@ enum bpf_attach_type {
 	BPF_SK_REUSEPORT_SELECT,
 	BPF_SK_REUSEPORT_SELECT_OR_MIGRATE,
 	BPF_PERF_EVENT,
+	BPF_TRACE_RAW_KPROBE,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -1009,6 +1010,7 @@ enum bpf_link_type {
 	BPF_LINK_TYPE_NETNS = 5,
 	BPF_LINK_TYPE_XDP = 6,
 	BPF_LINK_TYPE_PERF_EVENT = 7,
+	BPF_LINK_TYPE_KPROBE = 8,
 
 	MAX_BPF_LINK_TYPE,
 };
@@ -1111,6 +1113,11 @@ enum bpf_link_type {
  */
 #define BPF_F_SLEEPABLE		(1U << 4)
 
+/* link_create flags used in LINK_CREATE command for BPF_TRACE_RAW_KPROBE
+ * attach type.
+ */
+#define BPF_F_KPROBE_RETURN	(1U << 0)
+
 /* When BPF ldimm64's insn[0].src_reg != 0 then this can have
  * the following extensions:
  *
@@ -1465,6 +1472,11 @@ union bpf_attr {
 				 */
 				__u64		bpf_cookie;
 			} perf_event;
+			struct {
+				__aligned_u64	addrs;
+				__u32		cnt;
+				__u64		bpf_cookie;
+			} kprobe;
 		};
 	} link_create;
 
kernel/bpf/syscall.c +190 -5

237 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +12 -0
index b0383d371b9a..5216b333c688 100644
@@ -995,6 +995,7 @@ enum bpf_attach_type {
 	BPF_SK_REUSEPORT_SELECT,
 	BPF_SK_REUSEPORT_SELECT_OR_MIGRATE,
 	BPF_PERF_EVENT,
+	BPF_TRACE_RAW_KPROBE,
 	__MAX_BPF_ATTACH_TYPE
 };
 
@@ -1009,6 +1010,7 @@ enum bpf_link_type {
 	BPF_LINK_TYPE_NETNS = 5,
 	BPF_LINK_TYPE_XDP = 6,
 	BPF_LINK_TYPE_PERF_EVENT = 7,
+	BPF_LINK_TYPE_KPROBE = 8,
 
 	MAX_BPF_LINK_TYPE,
 };
@@ -1111,6 +1113,11 @@ enum bpf_link_type {
  */
 #define BPF_F_SLEEPABLE		(1U << 4)
 
+/* link_create flags used in LINK_CREATE command for BPF_TRACE_RAW_KPROBE
+ * attach type.
+ */
+#define BPF_F_KPROBE_RETURN	(1U << 0)
+
 /* When BPF ldimm64's insn[0].src_reg != 0 then this can have
  * the following extensions:
  *
@@ -1465,6 +1472,11 @@ union bpf_attr {
 				 */
 				__u64		bpf_cookie;
 			} perf_event;
+			struct {
+				__aligned_u64	addrs;
+				__u32		cnt;
+				__u64		bpf_cookie;
+			} kprobe;
 		};
 	} link_create;
 
#8 lore raw

Re: [RFC PATCH v3 7/9] bpf: Add kprobe link for attaching raw kprobes

On Wed, Jan 19, 2022 at 11:57:50PM +0900, Masami Hiramatsu wrote:

SNIP

Quoted 30 lines
kernel/bpf/syscall.c +30 -0
+static int kprobe_link_prog_run(struct bpf_kprobe_link *kprobe_link,
+				struct pt_regs *regs)
+{
+	struct bpf_trace_run_ctx run_ctx;
+	struct bpf_run_ctx *old_run_ctx;
+	int err;
+
+	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
+		err = 0;
+		goto out;
+	}
+
+	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
+	run_ctx.bpf_cookie = kprobe_link->bpf_cookie;
+
+	rcu_read_lock();
+	migrate_disable();
+	err = bpf_prog_run(kprobe_link->link.prog, regs);
+	migrate_enable();
+	rcu_read_unlock();
+
+	bpf_reset_run_ctx(old_run_ctx);
+
+ out:
+	__this_cpu_dec(bpf_prog_active);
+	return err;
+}
+
+static void kprobe_link_entry_handler(struct fprobe *fp, unsigned long entry_ip,
+				      struct pt_regs *regs)

kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3099

Quoted 10 lines
kernel/bpf/syscall.c +10 -0
+{
+	struct bpf_kprobe_link *kprobe_link;
+
+	/*
+	 * Because fprobe's regs->ip is set to the next instruction of
+	 * dynamic-ftrace insturction, correct entry ip must be set, so
+	 * that the bpf program can access entry address via regs as same
+	 * as kprobes.
+	 */
+	instruction_pointer_set(regs, entry_ip);

ok, so this actually does the stall for me.. it changes the return address back to repeat the call again

bu I think it's good idea to carry the original ip in regs (for bpf_get_func_ip helper) so I think we need to save it first and restore after the callback

I'll make the fix and add cookie change Andrii asked for on top of your ftrace changes and let you know

thanks,
jirka

Quoted 4 lines
kernel/bpf/syscall.c +4 -0
+	kprobe_link = container_of(fp, struct bpf_kprobe_link, fp);
+	kprobe_link_prog_run(kprobe_link, regs);
+}
+

SNIP

author maintainer
#9 lore raw

Re: [RFC PATCH v3 7/9] bpf: Add kprobe link for attaching raw kprobes

Quoted 39 lines · On Thu, 20 Jan 2022 11:44:54 +0100 Jiri Olsa <[email protected]> wrote:

On Wed, Jan 19, 2022 at 11:57:50PM +0900, Masami Hiramatsu wrote:

SNIP

Quoted 35 lines
kernel/bpf/syscall.c +35 -0
+static int kprobe_link_prog_run(struct bpf_kprobe_link *kprobe_link,
+				struct pt_regs *regs)
+{
+	struct bpf_trace_run_ctx run_ctx;
+	struct bpf_run_ctx *old_run_ctx;
+	int err;
+
+	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
+		err = 0;
+		goto out;
+	}
+
+	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
+	run_ctx.bpf_cookie = kprobe_link->bpf_cookie;
+
+	rcu_read_lock();
+	migrate_disable();
+	err = bpf_prog_run(kprobe_link->link.prog, regs);
+	migrate_enable();
+	rcu_read_unlock();
+
+	bpf_reset_run_ctx(old_run_ctx);
+
+ out:
+	__this_cpu_dec(bpf_prog_active);
+	return err;
+}
+
+static void kprobe_link_entry_handler(struct fprobe *fp, unsigned long entry_ip,
+				      struct pt_regs *regs)
+{
+	struct bpf_kprobe_link *kprobe_link;
+
+	/*
+	 * Because fprobe's regs->ip is set to the next instruction of
Quoted 8 lines

kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3099

Quoted 5 lines
kernel/bpf/syscall.c +5 -0
+	 * dynamic-ftrace insturction, correct entry ip must be set, so
+	 * that the bpf program can access entry address via regs as same
+	 * as kprobes.
+	 */
+	instruction_pointer_set(regs, entry_ip);

ok, so this actually does the stall for me.. it changes the return address back to repeat the call again

Good catch! and don't mind, this change is introduced me :-P. I thought that if I didn't add the FTRACE_FL_IPMODIFY, ftrace ignores the updated regs->ip, but it doesn't.

Quoted 3 lines

bu I think it's good idea to carry the original ip in regs (for bpf_get_func_ip helper) so I think we need to save it first and restore after the callback

Yes, btw, should I do that fix in fprobe?

Quoted 3 lines

I'll make the fix and add cookie change Andrii asked for on top of your ftrace changes and let you know

OK, thank you!

Quoted 11 lines

thanks,
jirka

Quoted 4 lines
kernel/bpf/syscall.c +4 -0
+	kprobe_link = container_of(fp, struct bpf_kprobe_link, fp);
+	kprobe_link_prog_run(kprobe_link, regs);
+}
+

SNIP

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#10 lore raw

RFC PATCH v3 8/9 Out-of-tree: Support wildcard symbol option to sample

In reply to Masami Hiramatsu

This is not intended to be merged to upstream code (since this expose some kernel internal functions just for an example.)

But this is good to show how the fprobe is time-efficient for registering a probe on thousands of functions.

 # time insmod fprobe_example.ko symbol='btrfs_*'
[   36.130947] fprobe_init: 1028 symbols found
[   36.177901] fprobe_init: Planted fprobe at btrfs_*
real	0m 0.08s
user	0m 0.00s
sys	0m 0.07s
---
 kernel/kallsyms.c               |    1 +
 kernel/trace/ftrace.c           |    1 +
 samples/fprobe/fprobe_example.c |   69 ++++++++++++++++++++++++++++++++++-----
 3 files changed, 62 insertions(+), 9 deletions(-)
kernel/kallsyms.c +1 -0
index 3011bc33a5ba..d0c4073acbfd 100644
@@ -246,6 +246,7 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 	}
 	return 0;
 }
+EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol);
 #endif /* CONFIG_LIVEPATCH */
 
 static unsigned long get_symbol_pos(unsigned long addr,
kernel/trace/ftrace.c +1 -0
index 39350aa38649..7ce604bc9529 100644
@@ -1580,6 +1580,7 @@ unsigned long ftrace_location(unsigned long ip)
 {
 	return ftrace_location_range(ip, ip);
 }
+EXPORT_SYMBOL_GPL(ftrace_location);
 
 /**
  * ftrace_text_reserved - return true if range contains an ftrace location
samples/fprobe/fprobe_example.c +60 -9

113 lines not on this page · raw message

author maintainer
#11 lore raw

RFC PATCH v3 9/9 out-of-tree: kprobes: Use rethook for kretprobe

In reply to Masami Hiramatsu

Replace the kretprobe return trampoline code with rethook APIs. Note that this is still under testing. This will work only on x86, but breaks other architectures.

---
 arch/x86/include/asm/unwind.h  |    8 +
 arch/x86/kernel/kprobes/core.c |  106 ----------------
 include/linux/kprobes.h        |   85 +------------
 include/linux/sched.h          |    3 
 kernel/exit.c                  |    1 
 kernel/fork.c                  |    3 
 kernel/kprobes.c               |  265 ++++++----------------------------------
 kernel/trace/trace_kprobe.c    |    4 -
 kernel/trace/trace_output.c    |    2 
 9 files changed, 50 insertions(+), 427 deletions(-)
arch/x86/include/asm/unwind.h +4 -4
index 2a1f8734416d..9237576f7ab4 100644
@@ -4,7 +4,7 @@
 
 #include <linux/sched.h>
 #include <linux/ftrace.h>
-#include <linux/kprobes.h>
+#include <linux/rethook.h>
 #include <asm/ptrace.h>
 #include <asm/stacktrace.h>
 
@@ -107,9 +107,9 @@ static inline
 unsigned long unwind_recover_kretprobe(struct unwind_state *state,
 				       unsigned long addr, unsigned long *addr_p)
 {
-#ifdef CONFIG_KRETPROBES
-	return is_kretprobe_trampoline(addr) ?
-		kretprobe_find_ret_addr(state->task, addr_p, &state->kr_cur) :
+#ifdef CONFIG_RETHOOK
+	return is_rethook_trampoline(addr) ?
+		rethook_find_ret_addr(state->task, (unsigned long)addr_p, &state->kr_cur) :
 		addr;
 #else
 	return addr;
arch/x86/kernel/kprobes/core.c +0 -106

118 lines not on this page · raw message

include/linux/kprobes.h +4 -81

136 lines not on this page · raw message

include/linux/sched.h +0 -3
index 2bfabf5355b7..b0fa25523a35 100644
@@ -1470,9 +1470,6 @@ struct task_struct {
 	int				mce_count;
 #endif
 
-#ifdef CONFIG_KRETPROBES
-	struct llist_head               kretprobe_instances;
-#endif
 #ifdef CONFIG_RETHOOK
 	struct llist_head               rethooks;
 #endif
kernel/exit.c +0 -1
index a39a321c1f37..e3a05589faa4 100644
@@ -169,7 +169,6 @@ static void delayed_put_task_struct(struct rcu_head *rhp)
 {
 	struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
 
-	kprobe_flush_task(tsk);
 	rethook_flush_task(tsk);
 	perf_event_delayed_put(tsk);
 	trace_sched_process_free(tsk);
kernel/fork.c +0 -3
index ffae38be64c4..bce71536c20a 100644
@@ -2279,9 +2279,6 @@ static __latent_entropy struct task_struct *copy_process(
 	p->task_works = NULL;
 	clear_posix_cputimers_work(p);
 
-#ifdef CONFIG_KRETPROBES
-	p->kretprobe_instances.first = NULL;
-#endif
 #ifdef CONFIG_RETHOOK
 	p->rethooks.first = NULL;
 #endif
kernel/kprobes.c +39 -226

335 lines not on this page · raw message

kernel/trace/trace_kprobe.c +2 -2
index 4e1257f50aa3..3d88cf33f7cc 100644
@@ -1446,7 +1446,7 @@ __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
 	fbuffer.regs = regs;
 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
 	entry->func = (unsigned long)tk->rp.kp.addr;
-	entry->ret_ip = (unsigned long)ri->ret_addr;
+	entry->ret_ip = ri->node.ret_addr;
 	store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
 
 	trace_event_buffer_commit(&fbuffer);
@@ -1641,7 +1641,7 @@ kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
 		return;
 
 	entry->func = (unsigned long)tk->rp.kp.addr;
-	entry->ret_ip = (unsigned long)ri->ret_addr;
+	entry->ret_ip = ri->node.ret_addr;
 	store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
 			      head, NULL);
kernel/trace/trace_output.c +1 -1
index 3547e7176ff7..b1a391f7bc78 100644
@@ -349,7 +349,7 @@ EXPORT_SYMBOL_GPL(trace_output_call);
 
 static inline const char *kretprobed(const char *name, unsigned long addr)
 {
-	if (is_kretprobe_trampoline(addr))
+	if (is_rethook_trampoline(addr))
 		return "[unknown/kretprobe'd]";
 	return name;
 }
#12 lore raw
In reply to Masami Hiramatsu
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Quoted 59 lines

Thank you,

---

Jiri Olsa (2):
ftrace: Add ftrace_set_filter_ips function
bpf: Add kprobe link for attaching raw kprobes

Masami Hiramatsu (7):
fprobe: Add ftrace based probe APIs
rethook: Add a generic return hook
rethook: x86: Add rethook x86 implementation
fprobe: Add exit_handler support
fprobe: Add sample program for fprobe
[DO NOT MERGE] Out-of-tree: Support wildcard symbol option to sample [DO NOT MERGE] out-of-tree: kprobes: Use rethook for kretprobe

 arch/x86/Kconfig                |    1
 arch/x86/include/asm/unwind.h   |    8 +
 arch/x86/kernel/Makefile        |    1
 arch/x86/kernel/kprobes/core.c  |  106 --------------
 arch/x86/kernel/rethook.c       |  115 +++++++++++++++
 include/linux/bpf_types.h       |    1
 include/linux/fprobe.h          |   84 +++++++++++
 include/linux/ftrace.h          |    3
 include/linux/kprobes.h         |   85 +----------
 include/linux/rethook.h         |   99 +++++++++++++
 include/linux/sched.h           |    4 -
 include/uapi/linux/bpf.h        |   12 ++
 kernel/bpf/syscall.c            |  195 +++++++++++++++++++++++++-
 kernel/exit.c                   |    3
 kernel/fork.c                   |    4 -
 kernel/kallsyms.c               |    1
 kernel/kprobes.c                |  265 +++++------------------------------
 kernel/trace/Kconfig            |   22 +++
 kernel/trace/Makefile           |    2
 kernel/trace/fprobe.c           |  179 ++++++++++++++++++++++++
 kernel/trace/ftrace.c           |   54 ++++++-
 kernel/trace/rethook.c          |  295 +++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c     |    4 -
 kernel/trace/trace_output.c     |    2
 samples/Kconfig                 |    7 +
 samples/Makefile                |    1
 samples/fprobe/Makefile         |    3
 samples/fprobe/fprobe_example.c |  154 ++++++++++++++++++++
 tools/include/uapi/linux/bpf.h  |   12 ++
 29 files changed, 1283 insertions(+), 439 deletions(-)
 create mode 100644 arch/x86/kernel/rethook.c
 create mode 100644 include/linux/fprobe.h
 create mode 100644 include/linux/rethook.h
 create mode 100644 kernel/trace/fprobe.c
 create mode 100644 kernel/trace/rethook.c
 create mode 100644 samples/fprobe/Makefile
 create mode 100644 samples/fprobe/fprobe_example.c

--
Masami Hiramatsu (Linaro) <[email protected]>

author maintainer
#13 lore raw
Quoted 50 lines · On Thu, 20 Jan 2022 14:24:15 -0800 Andrii Nakryiko <[email protected]>…
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

Quoted 4 lines

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Hmm, according to Jiri's original patch, it seems to pass an array of addresses. So I thought that has been resolved by libbpf.

+ struct {
+ __aligned_u64 addrs;
+ __u32 cnt;
+ __u64 bpf_cookie;
+ } kprobe;

Anyway, fprobe itself also has same issue. I'll try to fix it.

Thank you!

Quoted 60 lines
Quoted 60 lines

Thank you,

---

Jiri Olsa (2):
ftrace: Add ftrace_set_filter_ips function
bpf: Add kprobe link for attaching raw kprobes

Masami Hiramatsu (7):
fprobe: Add ftrace based probe APIs
rethook: Add a generic return hook
rethook: x86: Add rethook x86 implementation
fprobe: Add exit_handler support
fprobe: Add sample program for fprobe
[DO NOT MERGE] Out-of-tree: Support wildcard symbol option to sample [DO NOT MERGE] out-of-tree: kprobes: Use rethook for kretprobe

 arch/x86/Kconfig                |    1
 arch/x86/include/asm/unwind.h   |    8 +
 arch/x86/kernel/Makefile        |    1
 arch/x86/kernel/kprobes/core.c  |  106 --------------
 arch/x86/kernel/rethook.c       |  115 +++++++++++++++
 include/linux/bpf_types.h       |    1
 include/linux/fprobe.h          |   84 +++++++++++
 include/linux/ftrace.h          |    3
 include/linux/kprobes.h         |   85 +----------
 include/linux/rethook.h         |   99 +++++++++++++
 include/linux/sched.h           |    4 -
 include/uapi/linux/bpf.h        |   12 ++
 kernel/bpf/syscall.c            |  195 +++++++++++++++++++++++++-
 kernel/exit.c                   |    3
 kernel/fork.c                   |    4 -
 kernel/kallsyms.c               |    1
 kernel/kprobes.c                |  265 +++++------------------------------
 kernel/trace/Kconfig            |   22 +++
 kernel/trace/Makefile           |    2
 kernel/trace/fprobe.c           |  179 ++++++++++++++++++++++++
 kernel/trace/ftrace.c           |   54 ++++++-
 kernel/trace/rethook.c          |  295 +++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c     |    4 -
 kernel/trace/trace_output.c     |    2
 samples/Kconfig                 |    7 +
 samples/Makefile                |    1
 samples/fprobe/Makefile         |    3
 samples/fprobe/fprobe_example.c |  154 ++++++++++++++++++++
 tools/include/uapi/linux/bpf.h  |   12 ++
 29 files changed, 1283 insertions(+), 439 deletions(-)
 create mode 100644 arch/x86/kernel/rethook.c
 create mode 100644 include/linux/fprobe.h
 create mode 100644 include/linux/rethook.h
 create mode 100644 kernel/trace/fprobe.c
 create mode 100644 kernel/trace/rethook.c
 create mode 100644 samples/fprobe/Makefile
 create mode 100644 samples/fprobe/fprobe_example.c

--
Masami Hiramatsu (Linaro) <[email protected]>

Signature · 1 line
Masami Hiramatsu <[email protected]>
#14 lore raw
Quoted 62 lines · Masami Hiramatsu
Quoted 50 lines · On Thu, 20 Jan 2022 14:24:15 -0800 Andrii Nakryiko <[email protected]>…
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

Quoted 4 lines

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Hmm, according to Jiri's original patch, it seems to pass an array of addresses. So I thought that has been resolved by libbpf.

Review

Quoted 2 lines
(quoted diff) +2 -0
+                       struct {
+                               __aligned_u64   addrs;

I think this is a pointer to an array of pointers to zero-terminated C strings

Quoted 72 lines
(quoted diff) +3 -0
+                               __u32           cnt;
+                               __u64           bpf_cookie;
+                       } kprobe;

Anyway, fprobe itself also has same issue. I'll try to fix it.

Thank you!

Quoted 58 lines
Quoted 58 lines

Thank you,

---

Jiri Olsa (2):
ftrace: Add ftrace_set_filter_ips function
bpf: Add kprobe link for attaching raw kprobes

Masami Hiramatsu (7):
fprobe: Add ftrace based probe APIs
rethook: Add a generic return hook
rethook: x86: Add rethook x86 implementation
fprobe: Add exit_handler support
fprobe: Add sample program for fprobe
[DO NOT MERGE] Out-of-tree: Support wildcard symbol option to sample [DO NOT MERGE] out-of-tree: kprobes: Use rethook for kretprobe

 arch/x86/Kconfig                |    1
 arch/x86/include/asm/unwind.h   |    8 +
 arch/x86/kernel/Makefile        |    1
 arch/x86/kernel/kprobes/core.c  |  106 --------------
 arch/x86/kernel/rethook.c       |  115 +++++++++++++++
 include/linux/bpf_types.h       |    1
 include/linux/fprobe.h          |   84 +++++++++++
 include/linux/ftrace.h          |    3
 include/linux/kprobes.h         |   85 +----------
 include/linux/rethook.h         |   99 +++++++++++++
 include/linux/sched.h           |    4 -
 include/uapi/linux/bpf.h        |   12 ++
 kernel/bpf/syscall.c            |  195 +++++++++++++++++++++++++-
 kernel/exit.c                   |    3
 kernel/fork.c                   |    4 -
 kernel/kallsyms.c               |    1
 kernel/kprobes.c                |  265 +++++------------------------------
 kernel/trace/Kconfig            |   22 +++
 kernel/trace/Makefile           |    2
 kernel/trace/fprobe.c           |  179 ++++++++++++++++++++++++
 kernel/trace/ftrace.c           |   54 ++++++-
 kernel/trace/rethook.c          |  295 +++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c     |    4 -
 kernel/trace/trace_output.c     |    2
 samples/Kconfig                 |    7 +
 samples/Makefile                |    1
 samples/fprobe/Makefile         |    3
 samples/fprobe/fprobe_example.c |  154 ++++++++++++++++++++
 tools/include/uapi/linux/bpf.h  |   12 ++
 29 files changed, 1283 insertions(+), 439 deletions(-)
 create mode 100644 arch/x86/kernel/rethook.c
 create mode 100644 include/linux/fprobe.h
 create mode 100644 include/linux/rethook.h
 create mode 100644 kernel/trace/fprobe.c
 create mode 100644 kernel/trace/rethook.c
 create mode 100644 samples/fprobe/Makefile
 create mode 100644 samples/fprobe/fprobe_example.c
Quoted 2 lines
Quoted 2 lines

--
Masami Hiramatsu (Linaro) <[email protected]>

--
Masami Hiramatsu <[email protected]>

#15 lore raw
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Masami Hiramatsu
Quoted 44 lines · On Thu, 20 Jan 2022 14:24:15 -0800 Andrii Nakryiko <[email protected]>…
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

hm, I'm probably missing something, but should this be handled by arm specific kernel code? user passes whatever is found in kallsyms, right?

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Quoted 7 lines

Review

Quoted 5 lines

Hmm, according to Jiri's original patch, it seems to pass an array of addresses. So I thought that has been resolved by libbpf.

(quoted diff) +2 -0
+                       struct {
+                               __aligned_u64   addrs;

I think this is a pointer to an array of pointers to zero-terminated C strings

I used direct addresses, because bpftrace already has them, so there was no point passing strings, I cann add support for that

jirka

Quoted 74 lines
Quoted 73 lines
(quoted diff) +3 -0
+                               __u32           cnt;
+                               __u64           bpf_cookie;
+                       } kprobe;

Anyway, fprobe itself also has same issue. I'll try to fix it.

Thank you!

Quoted 58 lines
Quoted 58 lines

Thank you,

---

Jiri Olsa (2):
ftrace: Add ftrace_set_filter_ips function
bpf: Add kprobe link for attaching raw kprobes

Masami Hiramatsu (7):
fprobe: Add ftrace based probe APIs
rethook: Add a generic return hook
rethook: x86: Add rethook x86 implementation
fprobe: Add exit_handler support
fprobe: Add sample program for fprobe
[DO NOT MERGE] Out-of-tree: Support wildcard symbol option to sample [DO NOT MERGE] out-of-tree: kprobes: Use rethook for kretprobe

 arch/x86/Kconfig                |    1
 arch/x86/include/asm/unwind.h   |    8 +
 arch/x86/kernel/Makefile        |    1
 arch/x86/kernel/kprobes/core.c  |  106 --------------
 arch/x86/kernel/rethook.c       |  115 +++++++++++++++
 include/linux/bpf_types.h       |    1
 include/linux/fprobe.h          |   84 +++++++++++
 include/linux/ftrace.h          |    3
 include/linux/kprobes.h         |   85 +----------
 include/linux/rethook.h         |   99 +++++++++++++
 include/linux/sched.h           |    4 -
 include/uapi/linux/bpf.h        |   12 ++
 kernel/bpf/syscall.c            |  195 +++++++++++++++++++++++++-
 kernel/exit.c                   |    3
 kernel/fork.c                   |    4 -
 kernel/kallsyms.c               |    1
 kernel/kprobes.c                |  265 +++++------------------------------
 kernel/trace/Kconfig            |   22 +++
 kernel/trace/Makefile           |    2
 kernel/trace/fprobe.c           |  179 ++++++++++++++++++++++++
 kernel/trace/ftrace.c           |   54 ++++++-
 kernel/trace/rethook.c          |  295 +++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_kprobe.c     |    4 -
 kernel/trace/trace_output.c     |    2
 samples/Kconfig                 |    7 +
 samples/Makefile                |    1
 samples/fprobe/Makefile         |    3
 samples/fprobe/fprobe_example.c |  154 ++++++++++++++++++++
 tools/include/uapi/linux/bpf.h  |   12 ++
 29 files changed, 1283 insertions(+), 439 deletions(-)
 create mode 100644 arch/x86/kernel/rethook.c
 create mode 100644 include/linux/fprobe.h
 create mode 100644 include/linux/rethook.h
 create mode 100644 kernel/trace/fprobe.c
 create mode 100644 kernel/trace/rethook.c
 create mode 100644 samples/fprobe/Makefile
 create mode 100644 samples/fprobe/fprobe_example.c
Quoted 2 lines
Quoted 2 lines

--
Masami Hiramatsu (Linaro) <[email protected]>

--
Masami Hiramatsu <[email protected]>

author maintainer
#16 lore raw
Quoted 50 lines · On Mon, 24 Jan 2022 00:50:13 +0100 Jiri Olsa <[email protected]> wrote:
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Masami Hiramatsu
Quoted 44 lines · On Thu, 20 Jan 2022 14:24:15 -0800 Andrii Nakryiko <[email protected]>…
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

hm, I'm probably missing something, but should this be handled by arm specific kernel code? user passes whatever is found in kallsyms, right?

In x86, fentry nop is always placed at the first instruction of the function, but the other arches couldn't do that if they use LR (link register) for storing return address instead of stack. E.g. arm64 saves lr and call the ftrace. Then ftrace location address of a function is not the symbol address.

Anyway, I updated fprobe to handle those cases. I also found some issues on rethook, so let me update the series again.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Hmm, according to Jiri's original patch, it seems to pass an array of addresses. So I thought that has been resolved by libbpf.

Quoted 7 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                       struct {
+                               __aligned_u64   addrs;

I think this is a pointer to an array of pointers to zero-terminated C strings

I used direct addresses, because bpftrace already has them, so there was no point passing strings, I cann add support for that

So now both direct address array or symbol array are OK.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#17 lore raw
Quoted 62 lines · Masami Hiramatsu
Quoted 50 lines · On Mon, 24 Jan 2022 00:50:13 +0100 Jiri Olsa <[email protected]> wrote:
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Masami Hiramatsu
Quoted 44 lines · On Thu, 20 Jan 2022 14:24:15 -0800 Andrii Nakryiko <[email protected]>…
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

hm, I'm probably missing something, but should this be handled by arm specific kernel code? user passes whatever is found in kallsyms, right?

Quoted 7 lines

In x86, fentry nop is always placed at the first instruction of the function, but the other arches couldn't do that if they use LR (link register) for storing return address instead of stack. E.g. arm64 saves lr and call the ftrace. Then ftrace location address of a function is not the symbol address.

Anyway, I updated fprobe to handle those cases. I also found some issues on rethook, so let me update the series again.

great, I reworked the bpf fprobe link change and need to add the symbols attachment support, so you don't need to include it in new version.. I'll rebase it and send on top of your patchset

thanks,
jirka

Quoted 25 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Hmm, according to Jiri's original patch, it seems to pass an array of addresses. So I thought that has been resolved by libbpf.

Quoted 7 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                       struct {
+                               __aligned_u64   addrs;

I think this is a pointer to an array of pointers to zero-terminated C strings

I used direct addresses, because bpftrace already has them, so there was no point passing strings, I cann add support for that

So now both direct address array or symbol array are OK.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#18 lore raw
Quoted 69 lines · Jiri Olsa
Quoted 67 lines · Masami Hiramatsu
Quoted 50 lines · On Mon, 24 Jan 2022 00:50:13 +0100 Jiri Olsa <[email protected]> wrote:
Quoted 49 lines · Andrii Nakryiko
Quoted 48 lines · Masami Hiramatsu
Quoted 44 lines · On Thu, 20 Jan 2022 14:24:15 -0800 Andrii Nakryiko <[email protected]>…
Quoted 43 lines · Masami Hiramatsu

Hello Jiri,

Here is the 3rd version of fprobe. I added some comments and fixed some issues. But I still saw some problems when I add your selftest patches.

This series introduces the fprobe, the function entry/exit probe with multiple probe point support. This also introduces the rethook for hooking function return as same as kretprobe does. This abstraction will help us to generalize the fgraph tracer, because we can just switch it from rethook in fprobe, depending on the kernel configuration.

The patch [1/9] and [7/9] are from Jiri's series[1]. Other libbpf patches will not be affected by this change.

[1] https://lore.kernel.org/all/[email protected]/T/#u

However, when I applied all other patches on top of this series, I saw the "#8 bpf_cookie" test case has been stacked (maybe related to the bpf_cookie issue which Andrii and Jiri talked?) And when I remove the last selftest patch[2], the selftest stopped at "#112 raw_tp_test_run".

[2] https://lore.kernel.org/all/[email protected]/T/#m242d2b3a3775eeb5baba322424b15901e5e78483

Note that I used tools/testing/selftests/bpf/vmtest.sh to check it.

This added 2 more out-of-tree patches. [8/9] is for adding wildcard support to the sample program, [9/9] is a testing patch for replacing kretprobe trampoline with rethook.
According to this work, I noticed that using rethook in kretprobe needs 2 steps.
1. port the rethook on all architectures which supports kretprobes. (some arch requires CONFIG_KPROBES for rethook)
2. replace kretprobe trampoline with rethook for all archs, at once. This must be done by one treewide patch.

Anyway, I'll do the kretprobe update in the next step as another series. (This testing patch is just for confirming the rethook is correctly
implemented.)

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

hm, I'm probably missing something, but should this be handled by arm specific kernel code? user passes whatever is found in kallsyms, right?

In x86, fentry nop is always placed at the first instruction of the function, but the other arches couldn't do that if they use LR (link register) for storing return address instead of stack. E.g. arm64 saves lr and call the ftrace. Then ftrace location address of a function is not the symbol address.

Quoted 6 lines
Quoted 2 lines

Anyway, I updated fprobe to handle those cases. I also found some issues on rethook, so let me update the series again.

great, I reworked the bpf fprobe link change and need to add the symbols attachment support, so you don't need to include it in new version.. I'll rebase it and send on top of your patchset

Using just addresses (IPs) for retsnoop and bpftrace is fine because such generic tools are already parsing kallsyms and probably building some lookup table. But in general, having IP-based attachment is a regression from current perf_event_open-based kprobe, where user is expected to pass symbolic function name. Using IPs has an advantage of being unambiguous (e.g., when same static function name in kernel belongs to multiple actual functions), so there is that. But I was also wondering wouldn't kernel need to do symbol to IP resolution anyways just to check that we are attaching to function entry?

I'll wait for your patch set to see how did you go about it in a new revision.

Quoted 30 lines

thanks,
jirka

Quoted 25 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

libbpf doesn't do anything there. The interface for kprobe is based on function name and kernel performs name lookups internally to resolve IP. For fentry it's similar (kernel handles IP resolution), but instead of function name we specify BTF ID of a function type.

Hmm, according to Jiri's original patch, it seems to pass an array of addresses. So I thought that has been resolved by libbpf.

Quoted 7 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                       struct {
+                               __aligned_u64   addrs;

I think this is a pointer to an array of pointers to zero-terminated C strings

I used direct addresses, because bpftrace already has them, so there was no point passing strings, I cann add support for that

So now both direct address array or symbol array are OK.

Thank you,

--
Masami Hiramatsu <[email protected]>

#19 lore raw

On Mon, Jan 24, 2022 at 12:22:10PM -0800, Andrii Nakryiko wrote:

SNIP

Quoted 24 lines
Quoted 17 lines
Quoted 17 lines
Quoted 11 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

(This testing patch is just for confirming the rethook is correctly
implemented.)

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

hm, I'm probably missing something, but should this be handled by arm specific kernel code? user passes whatever is found in kallsyms, right?

In x86, fentry nop is always placed at the first instruction of the function, but the other arches couldn't do that if they use LR (link register) for storing return address instead of stack. E.g. arm64 saves lr and call the ftrace. Then ftrace location address of a function is not the symbol address.

Quoted 6 lines
Quoted 2 lines

Anyway, I updated fprobe to handle those cases. I also found some issues on rethook, so let me update the series again.

great, I reworked the bpf fprobe link change and need to add the symbols attachment support, so you don't need to include it in new version.. I'll rebase it and send on top of your patchset

Quoted 9 lines

Using just addresses (IPs) for retsnoop and bpftrace is fine because such generic tools are already parsing kallsyms and probably building some lookup table. But in general, having IP-based attachment is a regression from current perf_event_open-based kprobe, where user is expected to pass symbolic function name. Using IPs has an advantage of being unambiguous (e.g., when same static function name in kernel belongs to multiple actual functions), so there is that. But I was also wondering wouldn't kernel need to do symbol to IP resolution anyways just to check that we are attaching to function entry?

ftrace does its own check for address to attach, it keeps record for every attachable address.. so less work for us ;-)

Quoted 2 lines

I'll wait for your patch set to see how did you go about it in a new revision.

I agree we should have the support to use symbols as well, I'll add it

jirka

#20 lore raw
Quoted 39 lines · Jiri Olsa

On Mon, Jan 24, 2022 at 12:22:10PM -0800, Andrii Nakryiko wrote:

SNIP

Quoted 24 lines
Quoted 17 lines
Quoted 17 lines
Quoted 11 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

(This testing patch is just for confirming the rethook is correctly
implemented.)

BTW, on the x86, ftrace (with fentry) location address is same as symbol address. But on other archs, it will be different (e.g. arm64 will need 2 instructions to save link-register and call ftrace, the 2nd instruction will be the ftrace location.)
Does libbpf correctly handle it?

hm, I'm probably missing something, but should this be handled by arm specific kernel code? user passes whatever is found in kallsyms, right?

In x86, fentry nop is always placed at the first instruction of the function, but the other arches couldn't do that if they use LR (link register) for storing return address instead of stack. E.g. arm64 saves lr and call the ftrace. Then ftrace location address of a function is not the symbol address.

Quoted 6 lines
Quoted 2 lines

Anyway, I updated fprobe to handle those cases. I also found some issues on rethook, so let me update the series again.

great, I reworked the bpf fprobe link change and need to add the symbols attachment support, so you don't need to include it in new version.. I'll rebase it and send on top of your patchset

Quoted 9 lines

Using just addresses (IPs) for retsnoop and bpftrace is fine because such generic tools are already parsing kallsyms and probably building some lookup table. But in general, having IP-based attachment is a regression from current perf_event_open-based kprobe, where user is expected to pass symbolic function name. Using IPs has an advantage of being unambiguous (e.g., when same static function name in kernel belongs to multiple actual functions), so there is that. But I was also wondering wouldn't kernel need to do symbol to IP resolution anyways just to check that we are attaching to function entry?

Quoted 2 lines

ftrace does its own check for address to attach, it keeps record for every attachable address.. so less work for us ;-)

Oh, makes sense, thanks!

Quoted 5 lines
Quoted 3 lines

I'll wait for your patch set to see how did you go about it in a new revision.

I agree we should have the support to use symbols as well, I'll add it

sounds good, thanks

Quoted 3 lines

jirka