Skip to contentSkip to conversation

fprobe: Introduce fprobe function entry/exit probe

PATCH Superseded Masami Hiramatsu 2 participants 14 messages Jan 24, 2022 – Jan 25, 2022

Subsystems (13)

v4 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v4 cover fprobe: Introduce fprobe function entry/exit probe

Hello Jiri,

Here is the 4th version of fprobe. I fixed some issues on fprobe and rethook. I dropped out-of-tree patches and Jiri's bpf patch and add rethook arm/arm64 support and fprobe documentation. The previous version is here[1];

[1] https://lore.kernel.org/all/164260419349.657731.13913104835063027148.stgit@devnote2/T/#u

The biggest change of this version is supporting ftrace location conversion correctly. Previous version can not find ftrace location if the dynamic ftrace nop is not placed a the symbol address.

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] is from Jiri's series[2].

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

I'll continue porting rethook to other archs, and after that replace the current kretprobe with the rethook. This design change will make the fgraph/kretprobe integration easier because if fgraph is not available, kretprobe can use rethook.

Thank you,

---

Jiri Olsa (1):
ftrace: Add ftrace_set_filter_ips function

Masami Hiramatsu (8):
fprobe: Add ftrace based probe APIs
rethook: Add a generic return hook
rethook: x86: Add rethook x86 implementation
ARM: rethook: Add rethook arm implementation
arm64: rethook: Add arm64 rethook implementation
fprobe: Add exit_handler support
fprobe: Add sample program for fprobe
docs: fprobe: Add fprobe description to ftrace-use.rst

 Documentation/trace/fprobe.rst                |  131 +++++++++++
 Documentation/trace/index.rst                 |    1 
 arch/arm/Kconfig                              |    1 
 arch/arm/kernel/stacktrace.c                  |    4 
 arch/arm/probes/Makefile                      |    1 
 arch/arm/probes/rethook.c                     |   71 ++++++
 arch/arm64/Kconfig                            |    1 
 arch/arm64/kernel/probes/Makefile             |    1 
 arch/arm64/kernel/probes/rethook.c            |   25 ++
 arch/arm64/kernel/probes/rethook_trampoline.S |   87 +++++++
 arch/arm64/kernel/stacktrace.c                |    3 
 arch/x86/Kconfig                              |    1 
 arch/x86/include/asm/unwind.h                 |    4 
 arch/x86/kernel/Makefile                      |    1 
 arch/x86/kernel/rethook.c                     |  115 +++++++++
 include/linux/fprobe.h                        |   86 +++++++
 include/linux/ftrace.h                        |    3 
 include/linux/rethook.h                       |   99 ++++++++
 include/linux/sched.h                         |    3 
 kernel/exit.c                                 |    2 
 kernel/fork.c                                 |    3 
 kernel/trace/Kconfig                          |   24 ++
 kernel/trace/Makefile                         |    2 
 kernel/trace/fprobe.c                         |  198 ++++++++++++++++
 kernel/trace/ftrace.c                         |   53 ++++
 kernel/trace/rethook.c                        |  311 +++++++++++++++++++++++++
 samples/Kconfig                               |    7 +
 samples/Makefile                              |    1 
 samples/fprobe/Makefile                       |    3 
 samples/fprobe/fprobe_example.c               |  103 ++++++++
 30 files changed, 1336 insertions(+), 9 deletions(-)
 create mode 100644 Documentation/trace/fprobe.rst
 create mode 100644 arch/arm/probes/rethook.c
 create mode 100644 arch/arm64/kernel/probes/rethook.c
 create mode 100644 arch/arm64/kernel/probes/rethook_trampoline.S
 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

PATCH v4 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

PATCH v4 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);

CAUTION: if user entry handler changes registers including ip address, it will be applied when returns from the entry handler. So user handler must recover it.

---
Changes in v4:
- Fix a memory leak when symbol lookup failed.
- Use ftrace location address instead of symbol address.
- Convert the given symbol address to ftrace location automatically.
- Rename fprobe::ftrace to fprobe::ops.
- Update the Kconfig description.

---
 include/linux/fprobe.h |   80 ++++++++++++++++++++++++++++
 kernel/trace/Kconfig   |   12 ++++
 kernel/trace/Makefile  |    1 
 kernel/trace/fprobe.c  |  135 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 228 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..f7de332b08c2
@@ -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 ftrace address of the symbols.
+ * @nentry: The number of entries of @syms or @addrs.
+ * @ops: 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	ops;
+	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 +12 -0
index 420ff4bc67fd..23483dd474b0 100644
@@ -223,6 +223,18 @@ 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 (fprobe) based on ftrace,
+	  which is similar to kprobes, but probes only for kernel function
+	  entries and it can probe multiple functions by one fprobe.
+
+	  If unsure, say N.
+
 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 +135 -0

135 lines not on this page · raw message

author maintainer
#3 lore raw

PATCH v4 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 v4:
- Fix rethook_trampoline_handler() loops as same as what currently kretprobe does. This will fix some stacktrace issue in the rethook handler.

---
 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  |  311 +++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 430 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 23483dd474b0..4d27e56c6e76 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 +311 -0

311 lines not on this page · raw message

author maintainer
#4 lore raw

PATCH v4 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.

---
Changes in v4:
- fix stack backtrace as same as kretprobe does.

---
 arch/x86/Kconfig              |    1 
 arch/x86/include/asm/unwind.h |    4 +
 arch/x86/kernel/Makefile      |    1 
 arch/x86/kernel/rethook.c     |  115 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 121 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/include/asm/unwind.h +4 -0
index 2a1f8734416d..9fe5f73f22f1 100644
@@ -5,6 +5,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,6 +108,9 @@ static inline
 unsigned long unwind_recover_kretprobe(struct unwind_state *state,
 				       unsigned long addr, unsigned long *addr_p)
 {
+	if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(addr))
+		return rethook_find_ret_addr(state->task, (unsigned long)addr_p,
+					     &state->kr_cur);
 #ifdef CONFIG_KRETPROBES
 	return is_kretprobe_trampoline(addr) ?
 		kretprobe_find_ret_addr(state->task, addr_p, &state->kr_cur) :
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

#5 lore raw

Re: [PATCH v4 4/9] rethook: x86: Add rethook x86 implementation

I love your patch! Yet something to improve:

Hi Masami,

I love your patch! Yet something to improve:

[auto build test ERROR on rostedt-trace/for-next]
[also build test ERROR on tip/x86/core linus/master v5.17-rc1 next-20220124] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/fprobe-Introduce-fprobe-function-entry-exit-probe/20220125-001253
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git for-next
config: i386-tinyconfig (https://download.01.org/0day-ci/archive/20220125/202201250509.MSbKNePn-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/6366c7f830e71242dd9538fbdb09acdccea6e786
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Masami-Hiramatsu/fprobe-Introduce-fprobe-function-entry-exit-probe/20220125-001253
        git checkout 6366c7f830e71242dd9538fbdb09acdccea6e786
        # save the config file to linux build tree
        mkdir build_dir
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash arch/x86/kernel/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

In file included from arch/x86/kernel/process.c:48:
arch/x86/include/asm/unwind.h: In function 'unwind_recover_kretprobe':

>> arch/x86/include/asm/unwind.h:113:17: error: 'struct unwind_state' has no member named 'kr_cur'
     113 |           &state->kr_cur);
         |                 ^~

arch/x86/kernel/process.c: At top level:
arch/x86/kernel/process.c:887:13: warning: no previous prototype for 'arch_post_acpi_subsys_init' [-Wmissing-prototypes]
887 | void __init arch_post_acpi_subsys_init(void)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
--
In file included from arch/x86/kernel/unwind_guess.c:7:
arch/x86/include/asm/unwind.h: In function 'unwind_recover_kretprobe':

>> arch/x86/include/asm/unwind.h:113:17: error: 'struct unwind_state' has no member named 'kr_cur'
     113 |           &state->kr_cur);
         |                 ^~

vim +113 arch/x86/include/asm/unwind.h

106
107 static inline
108 unsigned long unwind_recover_kretprobe(struct unwind_state *state,
109 unsigned long addr, unsigned long *addr_p)
110 {
111 if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(addr))
112 return rethook_find_ret_addr(state->task, (unsigned long)addr_p,

 > 113						     &state->kr_cur);

114 #ifdef CONFIG_KRETPROBES
115 return is_kretprobe_trampoline(addr) ?
116 kretprobe_find_ret_addr(state->task, addr_p, &state->kr_cur) :
117 addr;
118 #else
119 return addr;
120 #endif
121 }
122

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#6 lore raw

Re: [PATCH v4 4/9] rethook: x86: Add rethook x86 implementation

In reply to Masami Hiramatsu
Quoted 40 lines · On Tue, 25 Jan 2022 01:10:09 +0900 Masami Hiramatsu <[email protected]> wrote:

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

---
Changes in v4:
- fix stack backtrace as same as kretprobe does.

---
 arch/x86/Kconfig              |    1 
 arch/x86/include/asm/unwind.h |    4 +
 arch/x86/kernel/Makefile      |    1 
 arch/x86/kernel/rethook.c     |  115 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 121 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/include/asm/unwind.h +1 -0
index 2a1f8734416d..9fe5f73f22f1 100644
@@ -5,6 +5,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>
 

arch/x86/include/asm/unwind.h static inline · line 113

Quoted 6 lines
arch/x86/include/asm/unwind.h +3 -0
@@ -107,6 +108,9 @@ static inline
 unsigned long unwind_recover_kretprobe(struct unwind_state *state,
 				       unsigned long addr, unsigned long *addr_p)
 {
+	if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(addr))
+		return rethook_find_ret_addr(state->task, (unsigned long)addr_p,
+					     &state->kr_cur);

Hm, I found that this doesn't work since state->kr_cur is not defined when CONFIG_KRETPROBES=n. Even if I define it with CONFIG_RETHOOK=y, if both CONFIG_RETHOOK and CONFIG_KRETPROBES are 'n', the compiler caused a build error. So I decided to use #ifdef here in the next version.

Thank you,

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

PATCH v4 5/9 ARM: rethook: Add rethook arm implementation

In reply to Masami Hiramatsu
---
 arch/arm/Kconfig             |    1 +
 arch/arm/kernel/stacktrace.c |    4 ++
 arch/arm/probes/Makefile     |    1 +
 arch/arm/probes/rethook.c    |   71 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+)
 create mode 100644 arch/arm/probes/rethook.c
arch/arm/Kconfig +1 -0
index c2724d986fa0..2fe24bbca618 100644
@@ -106,6 +106,7 @@ config ARM
 	select HAVE_MOD_ARCH_SPECIFIC
 	select HAVE_NMI
 	select HAVE_OPTPROBES if !THUMB2_KERNEL
+	select HAVE_RETHOOK
 	select HAVE_PERF_EVENTS
 	select HAVE_PERF_REGS
 	select HAVE_PERF_USER_STACK_DUMP
arch/arm/kernel/stacktrace.c +4 -0
index 75e905508f27..3c2a9d7024bc 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <linux/export.h>
 #include <linux/kprobes.h>
+#include <linux/rethook.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
 #include <linux/stacktrace.h>
@@ -66,6 +67,9 @@ int notrace unwind_frame(struct stackframe *frame)
 	frame->sp = *(unsigned long *)(fp - 8);
 	frame->pc = *(unsigned long *)(fp - 4);
 #endif
+	if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(frame->pc))
+		frame->pc = rethook_find_ret_addr(frame->tsk, frame->fp,
+						  &frame->kr_cur);
 #ifdef CONFIG_KRETPROBES
 	if (is_kretprobe_trampoline(frame->pc))
 		frame->pc = kretprobe_find_ret_addr(frame->tsk,
arch/arm/probes/Makefile +1 -0
index 8b0ea5ace100..10c083a22223 100644
@@ -6,3 +6,4 @@ obj-$(CONFIG_KPROBES)		+= decode-thumb.o
 else
 obj-$(CONFIG_KPROBES)		+= decode-arm.o
 endif
+obj-$(CONFIG_RETHOOK)		+= rethook.o
arch/arm/probes/rethook.c new file +71 -0

71 lines not on this page · raw message

#8 lore raw

Re: [PATCH v4 5/9] ARM: rethook: Add rethook arm implementation

I love your patch! Yet something to improve:

Hi Masami,

I love your patch! Yet something to improve:

[auto build test ERROR on rostedt-trace/for-next]
[also build test ERROR on arm64/for-next/core tip/x86/core linus/master v5.17-rc1 next-20220124] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/fprobe-Introduce-fprobe-function-entry-exit-probe/20220125-001253
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git for-next
config: arm-aspeed_g5_defconfig (https://download.01.org/0day-ci/archive/20220125/202201250328.drn6ia3n-lkp(a)intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/df6df88bb474db78d80fc5619d39b25ec15d5d16
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Masami-Hiramatsu/fprobe-Introduce-fprobe-function-entry-exit-probe/20220125-001253 git checkout df6df88bb474db78d80fc5619d39b25ec15d5d16
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arm SHELL=/bin/bash arch/arm/kernel/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

arch/arm/kernel/stacktrace.c: In function 'unwind_frame':

>> arch/arm/kernel/stacktrace.c:71:56: error: 'struct stackframe' has no member named 'tsk'
      71 |                 frame->pc = rethook_find_ret_addr(frame->tsk, frame->fp,
         |                                                        ^~
>> arch/arm/kernel/stacktrace.c:72:57: error: 'struct stackframe' has no member named 'kr_cur'
      72 |                                                   &frame->kr_cur);
         |                                                         ^~

vim +71 arch/arm/kernel/stacktrace.c

    12	
    13	#if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
    14	/*
    15	 * Unwind the current stack frame and store the new register values in the
    16	 * structure passed as argument. Unwinding is equivalent to a function return,
    17	 * hence the new PC value rather than LR should be used for backtrace.
    18	 *
    19	 * With framepointer enabled, a simple function prologue looks like this:
    20	 *	mov	ip, sp
    21	 *	stmdb	sp!, {fp, ip, lr, pc}
    22	 *	sub	fp, ip, #4
    23	 *
    24	 * A simple function epilogue looks like this:
    25	 *	ldm	sp, {fp, sp, pc}
    26	 *
    27	 * When compiled with clang, pc and sp are not pushed. A simple function
    28	 * prologue looks like this when built with clang:
    29	 *
    30	 *	stmdb	{..., fp, lr}
    31	 *	add	fp, sp, #x
    32	 *	sub	sp, sp, #y
    33	 *
    34	 * A simple function epilogue looks like this when built with clang:
    35	 *
    36	 *	sub	sp, fp, #x
    37	 *	ldm	{..., fp, pc}
    38	 *
    39	 *
    40	 * Note that with framepointer enabled, even the leaf functions have the same
    41	 * prologue and epilogue, therefore we can ignore the LR value in this case.
    42	 */
    43	int notrace unwind_frame(struct stackframe *frame)
    44	{
    45		unsigned long high, low;
    46		unsigned long fp = frame->fp;
    47	
    48		/* only go to a higher address on the stack */
    49		low = frame->sp;
    50		high = ALIGN(low, THREAD_SIZE);
    51	
    52	#ifdef CONFIG_CC_IS_CLANG
    53		/* check current frame pointer is within bounds */
    54		if (fp < low + 4 || fp > high - 4)
    55			return -EINVAL;
    56	
    57		frame->sp = frame->fp;
    58		frame->fp = *(unsigned long *)(fp);
    59		frame->pc = *(unsigned long *)(fp + 4);
    60	#else
    61		/* check current frame pointer is within bounds */
    62		if (fp < low + 12 || fp > high - 4)
    63			return -EINVAL;
    64	
    65		/* restore the registers from the stack frame */
    66		frame->fp = *(unsigned long *)(fp - 12);
    67		frame->sp = *(unsigned long *)(fp - 8);
    68		frame->pc = *(unsigned long *)(fp - 4);
    69	#endif
    70		if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(frame->pc))
  > 71			frame->pc = rethook_find_ret_addr(frame->tsk, frame->fp,
  > 72							  &frame->kr_cur);
    73	#ifdef CONFIG_KRETPROBES
    74		if (is_kretprobe_trampoline(frame->pc))
    75			frame->pc = kretprobe_find_ret_addr(frame->tsk,
    76						(void *)frame->fp, &frame->kr_cur);
    77	#endif
    78	
    79		return 0;
    80	}
    81	#endif
    82	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#9 lore raw

PATCH v4 6/9 arm64: rethook: Add arm64 rethook implementation

In reply to Masami Hiramatsu
---
 arch/arm64/Kconfig                            |    1 
 arch/arm64/kernel/probes/Makefile             |    1 
 arch/arm64/kernel/probes/rethook.c            |   25 +++++++
 arch/arm64/kernel/probes/rethook_trampoline.S |   87 +++++++++++++++++++++++++
 arch/arm64/kernel/stacktrace.c                |    3 +
 5 files changed, 117 insertions(+)
 create mode 100644 arch/arm64/kernel/probes/rethook.c
 create mode 100644 arch/arm64/kernel/probes/rethook_trampoline.S
arch/arm64/Kconfig +1 -0
index c4207cf9bb17..c706ed25ea50 100644
@@ -201,6 +201,7 @@ config ARM64
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_KPROBES
 	select HAVE_KRETPROBES
+	select HAVE_RETHOOK
 	select HAVE_GENERIC_VDSO
 	select IOMMU_DMA if IOMMU_SUPPORT
 	select IRQ_DOMAIN
arch/arm64/kernel/probes/Makefile +1 -0
index 8e4be92e25b1..24e689f44c32 100644
@@ -4,3 +4,4 @@ obj-$(CONFIG_KPROBES)		+= kprobes.o decode-insn.o	\
 				   simulate-insn.o
 obj-$(CONFIG_UPROBES)		+= uprobes.o decode-insn.o	\
 				   simulate-insn.o
+obj-$(CONFIG_RETHOOK)		+= rethook.o rethook_trampoline.o
arch/arm64/kernel/probes/rethook.c new file +25 -0
new file mode 100644
index 000000000000..38c33c81438b
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Generic return hook for arm64.
+ * Most of the code is copied from arch/arm64/kernel/probes/kprobes.c
+ */
+
+#include <linux/kprobes.h>
+#include <linux/rethook.h>
+
+/* This is called from arch_rethook_trampoline() */
+unsigned long __used arch_rethook_trampoline_callback(struct pt_regs *regs)
+{
+	return rethook_trampoline_handler(regs, regs->regs[29]);
+}
+NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
+
+void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs)
+{
+	rhn->ret_addr = regs->regs[30];
+	rhn->frame = regs->regs[29];
+
+	/* replace return addr (x30) with trampoline */
+	regs->regs[30] = (u64)arch_rethook_trampoline;
+}
+NOKPROBE_SYMBOL(arch_rethook_prepare);
arch/arm64/kernel/probes/rethook_trampoline.S new file +87 -0

87 lines not on this page · raw message

arch/arm64/kernel/stacktrace.c +3 -0
index b0f21677764d..d0883d0fa61c 100644
@@ -8,6 +8,7 @@
 #include <linux/export.h>
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
+#include <linux/rethook.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
 #include <linux/sched/task_stack.h>
@@ -137,6 +138,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
 	if (is_kretprobe_trampoline(frame->pc))
 		frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
 #endif
+	if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(frame->pc))
+		frame->pc = rethook_find_ret_addr(tsk, frame->fp, &frame->kr_cur);
 
 	return 0;
 }
#10 lore raw

Re: [PATCH v4 6/9] arm64: rethook: Add arm64 rethook implementation

I love your patch! Yet something to improve:

Hi Masami,

I love your patch! Yet something to improve:

[auto build test ERROR on rostedt-trace/for-next]
[also build test ERROR on arm64/for-next/core tip/x86/core linus/master v5.17-rc1 next-20220124] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Masami-Hiramatsu/fprobe-Introduce-fprobe-function-entry-exit-probe/20220125-001253
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git for-next
config: arm64-randconfig-r033-20220124 (https://download.01.org/0day-ci/archive/20220125/202201250403.5YnZvb4K-lkp(a)intel.com/config)
compiler: aarch64-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/981b0378461c912ba2d7b10412dd6fe21c316055
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Masami-Hiramatsu/fprobe-Introduce-fprobe-function-entry-exit-probe/20220125-001253 git checkout 981b0378461c912ba2d7b10412dd6fe21c316055
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/kernel/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

arch/arm64/kernel/stacktrace.c: In function 'unwind_frame':

>> arch/arm64/kernel/stacktrace.c:141:73: error: 'struct stackframe' has no member named 'kr_cur'
     141 |                 frame->pc = rethook_find_ret_addr(tsk, frame->fp, &frame->kr_cur);
         |                                                                         ^~

vim +141 arch/arm64/kernel/stacktrace.c

    59	
    60	/*
    61	 * Unwind from one frame record (A) to the next frame record (B).
    62	 *
    63	 * We terminate early if the location of B indicates a malformed chain of frame
    64	 * records (e.g. a cycle), determined based on the location and fp value of A
    65	 * and the location (but not the fp value) of B.
    66	 */
    67	int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
    68	{
    69		unsigned long fp = frame->fp;
    70		struct stack_info info;
    71	
    72		if (!tsk)
    73			tsk = current;
    74	
    75		/* Final frame; nothing to unwind */
    76		if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
    77			return -ENOENT;
    78	
    79		if (fp & 0x7)
    80			return -EINVAL;
    81	
    82		if (!on_accessible_stack(tsk, fp, 16, &info))
    83			return -EINVAL;
    84	
    85		if (test_bit(info.type, frame->stacks_done))
    86			return -EINVAL;
    87	
    88		/*
    89		 * As stacks grow downward, any valid record on the same stack must be
    90		 * at a strictly higher address than the prior record.
    91		 *
    92		 * Stacks can nest in several valid orders, e.g.
    93		 *
    94		 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
    95		 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
    96		 *
    97		 * ... but the nesting itself is strict. Once we transition from one
    98		 * stack to another, it's never valid to unwind back to that first
    99		 * stack.
   100		 */
   101		if (info.type == frame->prev_type) {
   102			if (fp <= frame->prev_fp)
   103				return -EINVAL;
   104		} else {
   105			set_bit(frame->prev_type, frame->stacks_done);
   106		}
   107	
   108		/*
   109		 * Record this frame record's values and location. The prev_fp and
   110		 * prev_type are only meaningful to the next unwind_frame() invocation.
   111		 */
   112		frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
   113		frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
   114		frame->prev_fp = fp;
   115		frame->prev_type = info.type;
   116	
   117		frame->pc = ptrauth_strip_insn_pac(frame->pc);
   118	
   119	#ifdef CONFIG_FUNCTION_GRAPH_TRACER
   120		if (tsk->ret_stack &&
   121			(frame->pc == (unsigned long)return_to_handler)) {
   122			unsigned long orig_pc;
   123			/*
   124			 * This is a case where function graph tracer has
   125			 * modified a return address (LR) in a stack frame
   126			 * to hook a function return.
   127			 * So replace it to an original value.
   128			 */
   129			orig_pc = ftrace_graph_ret_addr(tsk, NULL, frame->pc,
   130							(void *)frame->fp);
   131			if (WARN_ON_ONCE(frame->pc == orig_pc))
   132				return -EINVAL;
   133			frame->pc = orig_pc;
   134		}
   135	#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
   136	#ifdef CONFIG_KRETPROBES
   137		if (is_kretprobe_trampoline(frame->pc))
   138			frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
   139	#endif
   140		if (IS_ENABLED(CONFIG_RETHOOK) && is_rethook_trampoline(frame->pc))
 > 141			frame->pc = rethook_find_ret_addr(tsk, frame->fp, &frame->kr_cur);

142
143 return 0;
144 }
145 NOKPROBE_SYMBOL(unwind_frame);
146

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#11 lore raw

PATCH v4 7/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 v4:
- Check fprobe is disabled in the exit handler.
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 |    6 ++++
 kernel/trace/Kconfig   |    1 +
 kernel/trace/fprobe.c  |   65 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 71 insertions(+), 1 deletion(-)
include/linux/fprobe.h +6 -0
index f7de332b08c2..4692089b9118 100644
@@ -5,6 +5,7 @@
 
 #include <linux/compiler.h>
 #include <linux/ftrace.h>
+#include <linux/rethook.h>
 
 /**
  * struct fprobe - ftrace based probe.
@@ -14,7 +15,9 @@
  * @ops: The ftrace_ops.
  * @nmissed: The counter for missing events.
  * @flags: The status flag.
+ * @rethook: The rethook data structure. (internal data)
  * @entry_handler: The callback function for function entry.
+ * @exit_handler: The callback function for function exit.
  *
  * User must set either @syms or @addrs, but not both. If user sets
  * only @syms, the @addrs are generated when registering the fprobe.
@@ -28,7 +31,10 @@ struct fprobe {
 	struct ftrace_ops	ops;
 	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 4d27e56c6e76..6176904c4741 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 (fprobe) based on ftrace,
kernel/trace/fprobe.c +64 -1

110 lines not on this page · raw message

author maintainer
#12 lore raw

PATCH v4 8/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
#13 lore raw

PATCH v4 9/9 docs: fprobe: Add fprobe description to ftrace-use.rst

In reply to Masami Hiramatsu

Add a documentation of fprobe for the user who needs this interface.

---
 Documentation/trace/fprobe.rst |  131 ++++++++++++++++++++++++++++++++++++++++
 Documentation/trace/index.rst  |    1 
 2 files changed, 132 insertions(+)
 create mode 100644 Documentation/trace/fprobe.rst
Documentation/trace/fprobe.rst new file +131 -0
new file mode 100644
index 000000000000..c53950a1f91e
@@ -0,0 +1,131 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+Fprobe - Function entry/exit probe
+==================================
+
+.. Author: Masami Hiramatsu <[email protected]>
+
+Introduction
+============
+
+Instead of using ftrace full feature, if you only want to attach callbacks
+on function entry and exit, similar to the kprobes and kretprobes, you can
+use fprobe. Compared with kprobes and kretprobes, fprobe gives faster
+instrumentation for multiple functions with single handler. This document
+describes how to use fprobe.
+
+The usage of fprobe
+===================
+
+The fprobe is a wrapper of ftrace (+ kretprobe-like return callback) to
+attach callbacks to multiple function entry and exit. User needs to set up
+the `struct fprobe` and pass it to `register_fprobe()`.
+
+Typically, `fprobe` data structure is initialized with the `syms`, `nentry`
+and `entry_handler` and/or `exit_handler` as below.
+
+.. code-block:: c
+
+ char targets[] = {"func1", "func2", "func3"};
+ struct fprobe fp = {
+        .syms           = targets,
+        .nentry         = ARRAY_SIZE(targets),
+        .entry_handler  = my_entry_callback,
+        .exit_handler   = my_exit_callback,
+ };
+
+The ftrace_ops in the fprobe is automatically set. The FTRACE_OPS_FL_SAVE_REGS
+and FTRACE_OPS_FL_RECURSION
+flag will be set. If you need other flags, please set it by yourself.
+
+.. code-block:: c
+
+ fp.ops.flags |= FTRACE_OPS_FL_RCU;
+
+To enable this fprobe, call::
+
+  register_fprobe(&fp);
+
+To disable (remove from functions) this fprobe, call::
+
+  unregister_fprobe(&fp);
+
+You can temporally (soft) disable the fprobe by::
+
+  disable_fprobe(&fp);
+
+and resume by::
+
+  enable_fprobe(&fp);
+
+The above is defined by including the header::
+
+  #include <linux/fprobe.h>
+
+Same as ftrace, the registered callback will start being called some time
+after the register_fprobe() is called and before it returns. See
+:file:`Documentation/trace/ftrace.rst`.
+
+
+The fprobe entry/exit handler
+=============================
+
+The prototype of the entry/exit callback function is as follows:
+
+.. code-block:: c
+
+ void callback_func(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
+
+Note that both entry and exit callback has same ptototype. The @entry_ip is
+saved at function entry and passed to exit handler.
+
+@fp
+        This is the address of `fprobe` data structure related to this handler.
+        You can embed the `fprobe` to your data structure and get it by
+        container_of() macro from @fp. The @fp must not be NULL.
+
+@entry_ip
+        This is the entry address of the traced function (both entry and exit).
+
+@regs
+        This is the `pt_regs` data structure at the entry and exit. Note that
+        the instruction pointer of @regs may be different from the @entry_ip
+        in the entry_handler. If you need traced instruction pointer, you need
+        to use @entry_ip. On the other hand, in the exit_handler, the instruction
+        pointer of @regs is set to the currect return address.
+
+
+Use fprobe with raw address list
+================================
+
+Instead of passing the array of symbols, you can pass a array of raw
+function addresses via `fprobe::addrs`. In this case, the value of
+this array will be changed automatically to the dynamic ftrace NOP
+location addresses in the given kernel function. So please take care
+if you share this array with others.
+
+
+The missed counter
+==================
+
+The `fprobe` data structure has `fprobe::nmissed` counter field as same as
+kprobes.
+This counter counts up when;
+
+ - fprobe fails to take ftrace_recursion lock. This usually means that a function
+   which is traced by other ftrace users is called from the entry_handler.
+
+ - fprobe fails to setup the function exit because of the shortage of rethook
+   (the shadow stack for hooking the function return.)
+
+Note that `fprobe::nmissed` field is counted up in both case. The former case
+will skip both of entry and exit callback, and the latter case will skip exit
+callback, but in both case the counter is just increased by 1.
+
+Functions and structures
+========================
+
+.. kernel-doc:: include/linux/fprobe.h
+.. kernel-doc:: kernel/trace/fprobe.c
+
Documentation/trace/index.rst +1 -0
index 3769b9b7aed8..b9f3757f8269 100644
@@ -9,6 +9,7 @@ Linux Tracing Technologies
    tracepoint-analysis
    ftrace
    ftrace-uses
+   fprobe
    kprobes
    kprobetrace
    uprobetracer