Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 ==================================
0004 Fprobe - Function entry/exit probe
0005 ==================================
0006 
0007 .. Author: Masami Hiramatsu <mhiramat@kernel.org>
0008 
0009 Introduction
0010 ============
0011 
0012 Fprobe is a function entry/exit probe mechanism based on ftrace.
0013 Instead of using ftrace full feature, if you only want to attach callbacks
0014 on function entry and exit, similar to the kprobes and kretprobes, you can
0015 use fprobe. Compared with kprobes and kretprobes, fprobe gives faster
0016 instrumentation for multiple functions with single handler. This document
0017 describes how to use fprobe.
0018 
0019 The usage of fprobe
0020 ===================
0021 
0022 The fprobe is a wrapper of ftrace (+ kretprobe-like return callback) to
0023 attach callbacks to multiple function entry and exit. User needs to set up
0024 the `struct fprobe` and pass it to `register_fprobe()`.
0025 
0026 Typically, `fprobe` data structure is initialized with the `entry_handler`
0027 and/or `exit_handler` as below.
0028 
0029 .. code-block:: c
0030 
0031  struct fprobe fp = {
0032         .entry_handler  = my_entry_callback,
0033         .exit_handler   = my_exit_callback,
0034  };
0035 
0036 To enable the fprobe, call one of register_fprobe(), register_fprobe_ips(), and
0037 register_fprobe_syms(). These functions register the fprobe with different types
0038 of parameters.
0039 
0040 The register_fprobe() enables a fprobe by function-name filters.
0041 E.g. this enables @fp on "func*()" function except "func2()".::
0042 
0043   register_fprobe(&fp, "func*", "func2");
0044 
0045 The register_fprobe_ips() enables a fprobe by ftrace-location addresses.
0046 E.g.
0047 
0048 .. code-block:: c
0049 
0050   unsigned long ips[] = { 0x.... };
0051 
0052   register_fprobe_ips(&fp, ips, ARRAY_SIZE(ips));
0053 
0054 And the register_fprobe_syms() enables a fprobe by symbol names.
0055 E.g.
0056 
0057 .. code-block:: c
0058 
0059   char syms[] = {"func1", "func2", "func3"};
0060 
0061   register_fprobe_syms(&fp, syms, ARRAY_SIZE(syms));
0062 
0063 To disable (remove from functions) this fprobe, call::
0064 
0065   unregister_fprobe(&fp);
0066 
0067 You can temporally (soft) disable the fprobe by::
0068 
0069   disable_fprobe(&fp);
0070 
0071 and resume by::
0072 
0073   enable_fprobe(&fp);
0074 
0075 The above is defined by including the header::
0076 
0077   #include <linux/fprobe.h>
0078 
0079 Same as ftrace, the registered callbacks will start being called some time
0080 after the register_fprobe() is called and before it returns. See
0081 :file:`Documentation/trace/ftrace.rst`.
0082 
0083 Also, the unregister_fprobe() will guarantee that the both enter and exit
0084 handlers are no longer being called by functions after unregister_fprobe()
0085 returns as same as unregister_ftrace_function().
0086 
0087 The fprobe entry/exit handler
0088 =============================
0089 
0090 The prototype of the entry/exit callback function is as follows:
0091 
0092 .. code-block:: c
0093 
0094  void callback_func(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
0095 
0096 Note that both entry and exit callbacks have same ptototype. The @entry_ip is
0097 saved at function entry and passed to exit handler.
0098 
0099 @fp
0100         This is the address of `fprobe` data structure related to this handler.
0101         You can embed the `fprobe` to your data structure and get it by
0102         container_of() macro from @fp. The @fp must not be NULL.
0103 
0104 @entry_ip
0105         This is the ftrace address of the traced function (both entry and exit).
0106         Note that this may not be the actual entry address of the function but
0107         the address where the ftrace is instrumented.
0108 
0109 @regs
0110         This is the `pt_regs` data structure at the entry and exit. Note that
0111         the instruction pointer of @regs may be different from the @entry_ip
0112         in the entry_handler. If you need traced instruction pointer, you need
0113         to use @entry_ip. On the other hand, in the exit_handler, the instruction
0114         pointer of @regs is set to the currect return address.
0115 
0116 Share the callbacks with kprobes
0117 ================================
0118 
0119 Since the recursion safeness of the fprobe (and ftrace) is a bit different
0120 from the kprobes, this may cause an issue if user wants to run the same
0121 code from the fprobe and the kprobes.
0122 
0123 Kprobes has per-cpu 'current_kprobe' variable which protects the kprobe
0124 handler from recursion in all cases. On the other hand, fprobe uses
0125 only ftrace_test_recursion_trylock(). This allows interrupt context to
0126 call another (or same) fprobe while the fprobe user handler is running.
0127 
0128 This is not a matter if the common callback code has its own recursion
0129 detection, or it can handle the recursion in the different contexts
0130 (normal/interrupt/NMI.)
0131 But if it relies on the 'current_kprobe' recursion lock, it has to check
0132 kprobe_running() and use kprobe_busy_*() APIs.
0133 
0134 Fprobe has FPROBE_FL_KPROBE_SHARED flag to do this. If your common callback
0135 code will be shared with kprobes, please set FPROBE_FL_KPROBE_SHARED
0136 *before* registering the fprobe, like:
0137 
0138 .. code-block:: c
0139 
0140  fprobe.flags = FPROBE_FL_KPROBE_SHARED;
0141 
0142  register_fprobe(&fprobe, "func*", NULL);
0143 
0144 This will protect your common callback from the nested call.
0145 
0146 The missed counter
0147 ==================
0148 
0149 The `fprobe` data structure has `fprobe::nmissed` counter field as same as
0150 kprobes.
0151 This counter counts up when;
0152 
0153  - fprobe fails to take ftrace_recursion lock. This usually means that a function
0154    which is traced by other ftrace users is called from the entry_handler.
0155 
0156  - fprobe fails to setup the function exit because of the shortage of rethook
0157    (the shadow stack for hooking the function return.)
0158 
0159 The `fprobe::nmissed` field counts up in both cases. Therefore, the former
0160 skips both of entry and exit callback and the latter skips the exit
0161 callback, but in both case the counter will increase by 1.
0162 
0163 Note that if you set the FTRACE_OPS_FL_RECURSION and/or FTRACE_OPS_FL_RCU to
0164 `fprobe::ops::flags` (ftrace_ops::flags) when registering the fprobe, this
0165 counter may not work correctly, because ftrace skips the fprobe function which
0166 increase the counter.
0167 
0168 
0169 Functions and structures
0170 ========================
0171 
0172 .. kernel-doc:: include/linux/fprobe.h
0173 .. kernel-doc:: kernel/trace/fprobe.c
0174