fprobe.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==================================
  3. Fprobe - Function entry/exit probe
  4. ==================================
  5. .. Author: Masami Hiramatsu <mhiramat@kernel.org>
  6. Introduction
  7. ============
  8. Fprobe is a function entry/exit probe mechanism based on ftrace.
  9. Instead of using ftrace full feature, if you only want to attach callbacks
  10. on function entry and exit, similar to the kprobes and kretprobes, you can
  11. use fprobe. Compared with kprobes and kretprobes, fprobe gives faster
  12. instrumentation for multiple functions with single handler. This document
  13. describes how to use fprobe.
  14. The usage of fprobe
  15. ===================
  16. The fprobe is a wrapper of ftrace (+ kretprobe-like return callback) to
  17. attach callbacks to multiple function entry and exit. User needs to set up
  18. the `struct fprobe` and pass it to `register_fprobe()`.
  19. Typically, `fprobe` data structure is initialized with the `entry_handler`
  20. and/or `exit_handler` as below.
  21. .. code-block:: c
  22. struct fprobe fp = {
  23. .entry_handler = my_entry_callback,
  24. .exit_handler = my_exit_callback,
  25. };
  26. To enable the fprobe, call one of register_fprobe(), register_fprobe_ips(), and
  27. register_fprobe_syms(). These functions register the fprobe with different types
  28. of parameters.
  29. The register_fprobe() enables a fprobe by function-name filters.
  30. E.g. this enables @fp on "func*()" function except "func2()".::
  31. register_fprobe(&fp, "func*", "func2");
  32. The register_fprobe_ips() enables a fprobe by ftrace-location addresses.
  33. E.g.
  34. .. code-block:: c
  35. unsigned long ips[] = { 0x.... };
  36. register_fprobe_ips(&fp, ips, ARRAY_SIZE(ips));
  37. And the register_fprobe_syms() enables a fprobe by symbol names.
  38. E.g.
  39. .. code-block:: c
  40. char syms[] = {"func1", "func2", "func3"};
  41. register_fprobe_syms(&fp, syms, ARRAY_SIZE(syms));
  42. To disable (remove from functions) this fprobe, call::
  43. unregister_fprobe(&fp);
  44. You can temporally (soft) disable the fprobe by::
  45. disable_fprobe(&fp);
  46. and resume by::
  47. enable_fprobe(&fp);
  48. The above is defined by including the header::
  49. #include <linux/fprobe.h>
  50. Same as ftrace, the registered callbacks will start being called some time
  51. after the register_fprobe() is called and before it returns. See
  52. :file:`Documentation/trace/ftrace.rst`.
  53. Also, the unregister_fprobe() will guarantee that the both enter and exit
  54. handlers are no longer being called by functions after unregister_fprobe()
  55. returns as same as unregister_ftrace_function().
  56. The fprobe entry/exit handler
  57. =============================
  58. The prototype of the entry/exit callback function are as follows:
  59. .. code-block:: c
  60. int entry_callback(struct fprobe *fp, unsigned long entry_ip, unsigned long ret_ip, struct pt_regs *regs, void *entry_data);
  61. void exit_callback(struct fprobe *fp, unsigned long entry_ip, unsigned long ret_ip, struct pt_regs *regs, void *entry_data);
  62. Note that the @entry_ip is saved at function entry and passed to exit handler.
  63. If the entry callback function returns !0, the corresponding exit callback will be cancelled.
  64. @fp
  65. This is the address of `fprobe` data structure related to this handler.
  66. You can embed the `fprobe` to your data structure and get it by
  67. container_of() macro from @fp. The @fp must not be NULL.
  68. @entry_ip
  69. This is the ftrace address of the traced function (both entry and exit).
  70. Note that this may not be the actual entry address of the function but
  71. the address where the ftrace is instrumented.
  72. @ret_ip
  73. This is the return address that the traced function will return to,
  74. somewhere in the caller. This can be used at both entry and exit.
  75. @regs
  76. This is the `pt_regs` data structure at the entry and exit. Note that
  77. the instruction pointer of @regs may be different from the @entry_ip
  78. in the entry_handler. If you need traced instruction pointer, you need
  79. to use @entry_ip. On the other hand, in the exit_handler, the instruction
  80. pointer of @regs is set to the current return address.
  81. @entry_data
  82. This is a local storage to share the data between entry and exit handlers.
  83. This storage is NULL by default. If the user specify `exit_handler` field
  84. and `entry_data_size` field when registering the fprobe, the storage is
  85. allocated and passed to both `entry_handler` and `exit_handler`.
  86. Share the callbacks with kprobes
  87. ================================
  88. Since the recursion safeness of the fprobe (and ftrace) is a bit different
  89. from the kprobes, this may cause an issue if user wants to run the same
  90. code from the fprobe and the kprobes.
  91. Kprobes has per-cpu 'current_kprobe' variable which protects the kprobe
  92. handler from recursion in all cases. On the other hand, fprobe uses
  93. only ftrace_test_recursion_trylock(). This allows interrupt context to
  94. call another (or same) fprobe while the fprobe user handler is running.
  95. This is not a matter if the common callback code has its own recursion
  96. detection, or it can handle the recursion in the different contexts
  97. (normal/interrupt/NMI.)
  98. But if it relies on the 'current_kprobe' recursion lock, it has to check
  99. kprobe_running() and use kprobe_busy_*() APIs.
  100. Fprobe has FPROBE_FL_KPROBE_SHARED flag to do this. If your common callback
  101. code will be shared with kprobes, please set FPROBE_FL_KPROBE_SHARED
  102. *before* registering the fprobe, like:
  103. .. code-block:: c
  104. fprobe.flags = FPROBE_FL_KPROBE_SHARED;
  105. register_fprobe(&fprobe, "func*", NULL);
  106. This will protect your common callback from the nested call.
  107. The missed counter
  108. ==================
  109. The `fprobe` data structure has `fprobe::nmissed` counter field as same as
  110. kprobes.
  111. This counter counts up when;
  112. - fprobe fails to take ftrace_recursion lock. This usually means that a function
  113. which is traced by other ftrace users is called from the entry_handler.
  114. - fprobe fails to setup the function exit because of the shortage of rethook
  115. (the shadow stack for hooking the function return.)
  116. The `fprobe::nmissed` field counts up in both cases. Therefore, the former
  117. skips both of entry and exit callback and the latter skips the exit
  118. callback, but in both case the counter will increase by 1.
  119. Note that if you set the FTRACE_OPS_FL_RECURSION and/or FTRACE_OPS_FL_RCU to
  120. `fprobe::ops::flags` (ftrace_ops::flags) when registering the fprobe, this
  121. counter may not work correctly, because ftrace skips the fprobe function which
  122. increase the counter.
  123. Functions and structures
  124. ========================
  125. .. kernel-doc:: include/linux/fprobe.h
  126. .. kernel-doc:: kernel/trace/fprobe.c