Back to home page

OSCL-LXR

 
 

    


0001 =======================
0002 Kernel Probes (Kprobes)
0003 =======================
0004 
0005 :Author: Jim Keniston <jkenisto@us.ibm.com>
0006 :Author: Prasanna S Panchamukhi <prasanna.panchamukhi@gmail.com>
0007 :Author: Masami Hiramatsu <mhiramat@redhat.com>
0008 
0009 .. CONTENTS
0010 
0011   1. Concepts: Kprobes, and Return Probes
0012   2. Architectures Supported
0013   3. Configuring Kprobes
0014   4. API Reference
0015   5. Kprobes Features and Limitations
0016   6. Probe Overhead
0017   7. TODO
0018   8. Kprobes Example
0019   9. Kretprobes Example
0020   10. Deprecated Features
0021   Appendix A: The kprobes debugfs interface
0022   Appendix B: The kprobes sysctl interface
0023   Appendix C: References
0024 
0025 Concepts: Kprobes and Return Probes
0026 =========================================
0027 
0028 Kprobes enables you to dynamically break into any kernel routine and
0029 collect debugging and performance information non-disruptively. You
0030 can trap at almost any kernel code address [1]_, specifying a handler
0031 routine to be invoked when the breakpoint is hit.
0032 
0033 .. [1] some parts of the kernel code can not be trapped, see
0034        :ref:`kprobes_blacklist`)
0035 
0036 There are currently two types of probes: kprobes, and kretprobes
0037 (also called return probes).  A kprobe can be inserted on virtually
0038 any instruction in the kernel.  A return probe fires when a specified
0039 function returns.
0040 
0041 In the typical case, Kprobes-based instrumentation is packaged as
0042 a kernel module.  The module's init function installs ("registers")
0043 one or more probes, and the exit function unregisters them.  A
0044 registration function such as register_kprobe() specifies where
0045 the probe is to be inserted and what handler is to be called when
0046 the probe is hit.
0047 
0048 There are also ``register_/unregister_*probes()`` functions for batch
0049 registration/unregistration of a group of ``*probes``. These functions
0050 can speed up unregistration process when you have to unregister
0051 a lot of probes at once.
0052 
0053 The next four subsections explain how the different types of
0054 probes work and how jump optimization works.  They explain certain
0055 things that you'll need to know in order to make the best use of
0056 Kprobes -- e.g., the difference between a pre_handler and
0057 a post_handler, and how to use the maxactive and nmissed fields of
0058 a kretprobe.  But if you're in a hurry to start using Kprobes, you
0059 can skip ahead to :ref:`kprobes_archs_supported`.
0060 
0061 How Does a Kprobe Work?
0062 -----------------------
0063 
0064 When a kprobe is registered, Kprobes makes a copy of the probed
0065 instruction and replaces the first byte(s) of the probed instruction
0066 with a breakpoint instruction (e.g., int3 on i386 and x86_64).
0067 
0068 When a CPU hits the breakpoint instruction, a trap occurs, the CPU's
0069 registers are saved, and control passes to Kprobes via the
0070 notifier_call_chain mechanism.  Kprobes executes the "pre_handler"
0071 associated with the kprobe, passing the handler the addresses of the
0072 kprobe struct and the saved registers.
0073 
0074 Next, Kprobes single-steps its copy of the probed instruction.
0075 (It would be simpler to single-step the actual instruction in place,
0076 but then Kprobes would have to temporarily remove the breakpoint
0077 instruction.  This would open a small time window when another CPU
0078 could sail right past the probepoint.)
0079 
0080 After the instruction is single-stepped, Kprobes executes the
0081 "post_handler," if any, that is associated with the kprobe.
0082 Execution then continues with the instruction following the probepoint.
0083 
0084 Changing Execution Path
0085 -----------------------
0086 
0087 Since kprobes can probe into a running kernel code, it can change the
0088 register set, including instruction pointer. This operation requires
0089 maximum care, such as keeping the stack frame, recovering the execution
0090 path etc. Since it operates on a running kernel and needs deep knowledge
0091 of computer architecture and concurrent computing, you can easily shoot
0092 your foot.
0093 
0094 If you change the instruction pointer (and set up other related
0095 registers) in pre_handler, you must return !0 so that kprobes stops
0096 single stepping and just returns to the given address.
0097 This also means post_handler should not be called anymore.
0098 
0099 Note that this operation may be harder on some architectures which use
0100 TOC (Table of Contents) for function call, since you have to setup a new
0101 TOC for your function in your module, and recover the old one after
0102 returning from it.
0103 
0104 Return Probes
0105 -------------
0106 
0107 How Does a Return Probe Work?
0108 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0109 
0110 When you call register_kretprobe(), Kprobes establishes a kprobe at
0111 the entry to the function.  When the probed function is called and this
0112 probe is hit, Kprobes saves a copy of the return address, and replaces
0113 the return address with the address of a "trampoline."  The trampoline
0114 is an arbitrary piece of code -- typically just a nop instruction.
0115 At boot time, Kprobes registers a kprobe at the trampoline.
0116 
0117 When the probed function executes its return instruction, control
0118 passes to the trampoline and that probe is hit.  Kprobes' trampoline
0119 handler calls the user-specified return handler associated with the
0120 kretprobe, then sets the saved instruction pointer to the saved return
0121 address, and that's where execution resumes upon return from the trap.
0122 
0123 While the probed function is executing, its return address is
0124 stored in an object of type kretprobe_instance.  Before calling
0125 register_kretprobe(), the user sets the maxactive field of the
0126 kretprobe struct to specify how many instances of the specified
0127 function can be probed simultaneously.  register_kretprobe()
0128 pre-allocates the indicated number of kretprobe_instance objects.
0129 
0130 For example, if the function is non-recursive and is called with a
0131 spinlock held, maxactive = 1 should be enough.  If the function is
0132 non-recursive and can never relinquish the CPU (e.g., via a semaphore
0133 or preemption), NR_CPUS should be enough.  If maxactive <= 0, it is
0134 set to a default value.  If CONFIG_PREEMPT is enabled, the default
0135 is max(10, 2*NR_CPUS).  Otherwise, the default is NR_CPUS.
0136 
0137 It's not a disaster if you set maxactive too low; you'll just miss
0138 some probes.  In the kretprobe struct, the nmissed field is set to
0139 zero when the return probe is registered, and is incremented every
0140 time the probed function is entered but there is no kretprobe_instance
0141 object available for establishing the return probe.
0142 
0143 Kretprobe entry-handler
0144 ^^^^^^^^^^^^^^^^^^^^^^^
0145 
0146 Kretprobes also provides an optional user-specified handler which runs
0147 on function entry. This handler is specified by setting the entry_handler
0148 field of the kretprobe struct. Whenever the kprobe placed by kretprobe at the
0149 function entry is hit, the user-defined entry_handler, if any, is invoked.
0150 If the entry_handler returns 0 (success) then a corresponding return handler
0151 is guaranteed to be called upon function return. If the entry_handler
0152 returns a non-zero error then Kprobes leaves the return address as is, and
0153 the kretprobe has no further effect for that particular function instance.
0154 
0155 Multiple entry and return handler invocations are matched using the unique
0156 kretprobe_instance object associated with them. Additionally, a user
0157 may also specify per return-instance private data to be part of each
0158 kretprobe_instance object. This is especially useful when sharing private
0159 data between corresponding user entry and return handlers. The size of each
0160 private data object can be specified at kretprobe registration time by
0161 setting the data_size field of the kretprobe struct. This data can be
0162 accessed through the data field of each kretprobe_instance object.
0163 
0164 In case probed function is entered but there is no kretprobe_instance
0165 object available, then in addition to incrementing the nmissed count,
0166 the user entry_handler invocation is also skipped.
0167 
0168 .. _kprobes_jump_optimization:
0169 
0170 How Does Jump Optimization Work?
0171 --------------------------------
0172 
0173 If your kernel is built with CONFIG_OPTPROBES=y (currently this flag
0174 is automatically set 'y' on x86/x86-64, non-preemptive kernel) and
0175 the "debug.kprobes_optimization" kernel parameter is set to 1 (see
0176 sysctl(8)), Kprobes tries to reduce probe-hit overhead by using a jump
0177 instruction instead of a breakpoint instruction at each probepoint.
0178 
0179 Init a Kprobe
0180 ^^^^^^^^^^^^^
0181 
0182 When a probe is registered, before attempting this optimization,
0183 Kprobes inserts an ordinary, breakpoint-based kprobe at the specified
0184 address. So, even if it's not possible to optimize this particular
0185 probepoint, there'll be a probe there.
0186 
0187 Safety Check
0188 ^^^^^^^^^^^^
0189 
0190 Before optimizing a probe, Kprobes performs the following safety checks:
0191 
0192 - Kprobes verifies that the region that will be replaced by the jump
0193   instruction (the "optimized region") lies entirely within one function.
0194   (A jump instruction is multiple bytes, and so may overlay multiple
0195   instructions.)
0196 
0197 - Kprobes analyzes the entire function and verifies that there is no
0198   jump into the optimized region.  Specifically:
0199 
0200   - the function contains no indirect jump;
0201   - the function contains no instruction that causes an exception (since
0202     the fixup code triggered by the exception could jump back into the
0203     optimized region -- Kprobes checks the exception tables to verify this);
0204   - there is no near jump to the optimized region (other than to the first
0205     byte).
0206 
0207 - For each instruction in the optimized region, Kprobes verifies that
0208   the instruction can be executed out of line.
0209 
0210 Preparing Detour Buffer
0211 ^^^^^^^^^^^^^^^^^^^^^^^
0212 
0213 Next, Kprobes prepares a "detour" buffer, which contains the following
0214 instruction sequence:
0215 
0216 - code to push the CPU's registers (emulating a breakpoint trap)
0217 - a call to the trampoline code which calls user's probe handlers.
0218 - code to restore registers
0219 - the instructions from the optimized region
0220 - a jump back to the original execution path.
0221 
0222 Pre-optimization
0223 ^^^^^^^^^^^^^^^^
0224 
0225 After preparing the detour buffer, Kprobes verifies that none of the
0226 following situations exist:
0227 
0228 - The probe has a post_handler.
0229 - Other instructions in the optimized region are probed.
0230 - The probe is disabled.
0231 
0232 In any of the above cases, Kprobes won't start optimizing the probe.
0233 Since these are temporary situations, Kprobes tries to start
0234 optimizing it again if the situation is changed.
0235 
0236 If the kprobe can be optimized, Kprobes enqueues the kprobe to an
0237 optimizing list, and kicks the kprobe-optimizer workqueue to optimize
0238 it.  If the to-be-optimized probepoint is hit before being optimized,
0239 Kprobes returns control to the original instruction path by setting
0240 the CPU's instruction pointer to the copied code in the detour buffer
0241 -- thus at least avoiding the single-step.
0242 
0243 Optimization
0244 ^^^^^^^^^^^^
0245 
0246 The Kprobe-optimizer doesn't insert the jump instruction immediately;
0247 rather, it calls synchronize_rcu() for safety first, because it's
0248 possible for a CPU to be interrupted in the middle of executing the
0249 optimized region [3]_.  As you know, synchronize_rcu() can ensure
0250 that all interruptions that were active when synchronize_rcu()
0251 was called are done, but only if CONFIG_PREEMPT=n.  So, this version
0252 of kprobe optimization supports only kernels with CONFIG_PREEMPT=n [4]_.
0253 
0254 After that, the Kprobe-optimizer calls stop_machine() to replace
0255 the optimized region with a jump instruction to the detour buffer,
0256 using text_poke_smp().
0257 
0258 Unoptimization
0259 ^^^^^^^^^^^^^^
0260 
0261 When an optimized kprobe is unregistered, disabled, or blocked by
0262 another kprobe, it will be unoptimized.  If this happens before
0263 the optimization is complete, the kprobe is just dequeued from the
0264 optimized list.  If the optimization has been done, the jump is
0265 replaced with the original code (except for an int3 breakpoint in
0266 the first byte) by using text_poke_smp().
0267 
0268 .. [3] Please imagine that the 2nd instruction is interrupted and then
0269    the optimizer replaces the 2nd instruction with the jump *address*
0270    while the interrupt handler is running. When the interrupt
0271    returns to original address, there is no valid instruction,
0272    and it causes an unexpected result.
0273 
0274 .. [4] This optimization-safety checking may be replaced with the
0275    stop-machine method that ksplice uses for supporting a CONFIG_PREEMPT=y
0276    kernel.
0277 
0278 NOTE for geeks:
0279 The jump optimization changes the kprobe's pre_handler behavior.
0280 Without optimization, the pre_handler can change the kernel's execution
0281 path by changing regs->ip and returning 1.  However, when the probe
0282 is optimized, that modification is ignored.  Thus, if you want to
0283 tweak the kernel's execution path, you need to suppress optimization,
0284 using one of the following techniques:
0285 
0286 - Specify an empty function for the kprobe's post_handler.
0287 
0288 or
0289 
0290 - Execute 'sysctl -w debug.kprobes_optimization=n'
0291 
0292 .. _kprobes_blacklist:
0293 
0294 Blacklist
0295 ---------
0296 
0297 Kprobes can probe most of the kernel except itself. This means
0298 that there are some functions where kprobes cannot probe. Probing
0299 (trapping) such functions can cause a recursive trap (e.g. double
0300 fault) or the nested probe handler may never be called.
0301 Kprobes manages such functions as a blacklist.
0302 If you want to add a function into the blacklist, you just need
0303 to (1) include linux/kprobes.h and (2) use NOKPROBE_SYMBOL() macro
0304 to specify a blacklisted function.
0305 Kprobes checks the given probe address against the blacklist and
0306 rejects registering it, if the given address is in the blacklist.
0307 
0308 .. _kprobes_archs_supported:
0309 
0310 Architectures Supported
0311 =======================
0312 
0313 Kprobes and return probes are implemented on the following
0314 architectures:
0315 
0316 - i386 (Supports jump optimization)
0317 - x86_64 (AMD-64, EM64T) (Supports jump optimization)
0318 - ppc64
0319 - ia64 (Does not support probes on instruction slot1.)
0320 - sparc64 (Return probes not yet implemented.)
0321 - arm
0322 - ppc
0323 - mips
0324 - s390
0325 - parisc
0326 
0327 Configuring Kprobes
0328 ===================
0329 
0330 When configuring the kernel using make menuconfig/xconfig/oldconfig,
0331 ensure that CONFIG_KPROBES is set to "y". Under "General setup", look
0332 for "Kprobes".
0333 
0334 So that you can load and unload Kprobes-based instrumentation modules,
0335 make sure "Loadable module support" (CONFIG_MODULES) and "Module
0336 unloading" (CONFIG_MODULE_UNLOAD) are set to "y".
0337 
0338 Also make sure that CONFIG_KALLSYMS and perhaps even CONFIG_KALLSYMS_ALL
0339 are set to "y", since kallsyms_lookup_name() is used by the in-kernel
0340 kprobe address resolution code.
0341 
0342 If you need to insert a probe in the middle of a function, you may find
0343 it useful to "Compile the kernel with debug info" (CONFIG_DEBUG_INFO),
0344 so you can use "objdump -d -l vmlinux" to see the source-to-object
0345 code mapping.
0346 
0347 API Reference
0348 =============
0349 
0350 The Kprobes API includes a "register" function and an "unregister"
0351 function for each type of probe. The API also includes "register_*probes"
0352 and "unregister_*probes" functions for (un)registering arrays of probes.
0353 Here are terse, mini-man-page specifications for these functions and
0354 the associated probe handlers that you'll write. See the files in the
0355 samples/kprobes/ sub-directory for examples.
0356 
0357 register_kprobe
0358 ---------------
0359 
0360 ::
0361 
0362         #include <linux/kprobes.h>
0363         int register_kprobe(struct kprobe *kp);
0364 
0365 Sets a breakpoint at the address kp->addr.  When the breakpoint is hit, Kprobes
0366 calls kp->pre_handler.  After the probed instruction is single-stepped, Kprobe
0367 calls kp->post_handler.  Any or all handlers can be NULL. If kp->flags is set
0368 KPROBE_FLAG_DISABLED, that kp will be registered but disabled, so, its handlers
0369 aren't hit until calling enable_kprobe(kp).
0370 
0371 .. note::
0372 
0373    1. With the introduction of the "symbol_name" field to struct kprobe,
0374       the probepoint address resolution will now be taken care of by the kernel.
0375       The following will now work::
0376 
0377         kp.symbol_name = "symbol_name";
0378 
0379       (64-bit powerpc intricacies such as function descriptors are handled
0380       transparently)
0381 
0382    2. Use the "offset" field of struct kprobe if the offset into the symbol
0383       to install a probepoint is known. This field is used to calculate the
0384       probepoint.
0385 
0386    3. Specify either the kprobe "symbol_name" OR the "addr". If both are
0387       specified, kprobe registration will fail with -EINVAL.
0388 
0389    4. With CISC architectures (such as i386 and x86_64), the kprobes code
0390       does not validate if the kprobe.addr is at an instruction boundary.
0391       Use "offset" with caution.
0392 
0393 register_kprobe() returns 0 on success, or a negative errno otherwise.
0394 
0395 User's pre-handler (kp->pre_handler)::
0396 
0397         #include <linux/kprobes.h>
0398         #include <linux/ptrace.h>
0399         int pre_handler(struct kprobe *p, struct pt_regs *regs);
0400 
0401 Called with p pointing to the kprobe associated with the breakpoint,
0402 and regs pointing to the struct containing the registers saved when
0403 the breakpoint was hit.  Return 0 here unless you're a Kprobes geek.
0404 
0405 User's post-handler (kp->post_handler)::
0406 
0407         #include <linux/kprobes.h>
0408         #include <linux/ptrace.h>
0409         void post_handler(struct kprobe *p, struct pt_regs *regs,
0410                           unsigned long flags);
0411 
0412 p and regs are as described for the pre_handler.  flags always seems
0413 to be zero.
0414 
0415 register_kretprobe
0416 ------------------
0417 
0418 ::
0419 
0420         #include <linux/kprobes.h>
0421         int register_kretprobe(struct kretprobe *rp);
0422 
0423 Establishes a return probe for the function whose address is
0424 rp->kp.addr.  When that function returns, Kprobes calls rp->handler.
0425 You must set rp->maxactive appropriately before you call
0426 register_kretprobe(); see "How Does a Return Probe Work?" for details.
0427 
0428 register_kretprobe() returns 0 on success, or a negative errno
0429 otherwise.
0430 
0431 User's return-probe handler (rp->handler)::
0432 
0433         #include <linux/kprobes.h>
0434         #include <linux/ptrace.h>
0435         int kretprobe_handler(struct kretprobe_instance *ri,
0436                               struct pt_regs *regs);
0437 
0438 regs is as described for kprobe.pre_handler.  ri points to the
0439 kretprobe_instance object, of which the following fields may be
0440 of interest:
0441 
0442 - ret_addr: the return address
0443 - rp: points to the corresponding kretprobe object
0444 - task: points to the corresponding task struct
0445 - data: points to per return-instance private data; see "Kretprobe
0446         entry-handler" for details.
0447 
0448 The regs_return_value(regs) macro provides a simple abstraction to
0449 extract the return value from the appropriate register as defined by
0450 the architecture's ABI.
0451 
0452 The handler's return value is currently ignored.
0453 
0454 unregister_*probe
0455 ------------------
0456 
0457 ::
0458 
0459         #include <linux/kprobes.h>
0460         void unregister_kprobe(struct kprobe *kp);
0461         void unregister_kretprobe(struct kretprobe *rp);
0462 
0463 Removes the specified probe.  The unregister function can be called
0464 at any time after the probe has been registered.
0465 
0466 .. note::
0467 
0468    If the functions find an incorrect probe (ex. an unregistered probe),
0469    they clear the addr field of the probe.
0470 
0471 register_*probes
0472 ----------------
0473 
0474 ::
0475 
0476         #include <linux/kprobes.h>
0477         int register_kprobes(struct kprobe **kps, int num);
0478         int register_kretprobes(struct kretprobe **rps, int num);
0479 
0480 Registers each of the num probes in the specified array.  If any
0481 error occurs during registration, all probes in the array, up to
0482 the bad probe, are safely unregistered before the register_*probes
0483 function returns.
0484 
0485 - kps/rps: an array of pointers to ``*probe`` data structures
0486 - num: the number of the array entries.
0487 
0488 .. note::
0489 
0490    You have to allocate(or define) an array of pointers and set all
0491    of the array entries before using these functions.
0492 
0493 unregister_*probes
0494 ------------------
0495 
0496 ::
0497 
0498         #include <linux/kprobes.h>
0499         void unregister_kprobes(struct kprobe **kps, int num);
0500         void unregister_kretprobes(struct kretprobe **rps, int num);
0501 
0502 Removes each of the num probes in the specified array at once.
0503 
0504 .. note::
0505 
0506    If the functions find some incorrect probes (ex. unregistered
0507    probes) in the specified array, they clear the addr field of those
0508    incorrect probes. However, other probes in the array are
0509    unregistered correctly.
0510 
0511 disable_*probe
0512 --------------
0513 
0514 ::
0515 
0516         #include <linux/kprobes.h>
0517         int disable_kprobe(struct kprobe *kp);
0518         int disable_kretprobe(struct kretprobe *rp);
0519 
0520 Temporarily disables the specified ``*probe``. You can enable it again by using
0521 enable_*probe(). You must specify the probe which has been registered.
0522 
0523 enable_*probe
0524 -------------
0525 
0526 ::
0527 
0528         #include <linux/kprobes.h>
0529         int enable_kprobe(struct kprobe *kp);
0530         int enable_kretprobe(struct kretprobe *rp);
0531 
0532 Enables ``*probe`` which has been disabled by disable_*probe(). You must specify
0533 the probe which has been registered.
0534 
0535 Kprobes Features and Limitations
0536 ================================
0537 
0538 Kprobes allows multiple probes at the same address. Also,
0539 a probepoint for which there is a post_handler cannot be optimized.
0540 So if you install a kprobe with a post_handler, at an optimized
0541 probepoint, the probepoint will be unoptimized automatically.
0542 
0543 In general, you can install a probe anywhere in the kernel.
0544 In particular, you can probe interrupt handlers.  Known exceptions
0545 are discussed in this section.
0546 
0547 The register_*probe functions will return -EINVAL if you attempt
0548 to install a probe in the code that implements Kprobes (mostly
0549 kernel/kprobes.c and ``arch/*/kernel/kprobes.c``, but also functions such
0550 as do_page_fault and notifier_call_chain).
0551 
0552 If you install a probe in an inline-able function, Kprobes makes
0553 no attempt to chase down all inline instances of the function and
0554 install probes there.  gcc may inline a function without being asked,
0555 so keep this in mind if you're not seeing the probe hits you expect.
0556 
0557 A probe handler can modify the environment of the probed function
0558 -- e.g., by modifying kernel data structures, or by modifying the
0559 contents of the pt_regs struct (which are restored to the registers
0560 upon return from the breakpoint).  So Kprobes can be used, for example,
0561 to install a bug fix or to inject faults for testing.  Kprobes, of
0562 course, has no way to distinguish the deliberately injected faults
0563 from the accidental ones.  Don't drink and probe.
0564 
0565 Kprobes makes no attempt to prevent probe handlers from stepping on
0566 each other -- e.g., probing printk() and then calling printk() from a
0567 probe handler.  If a probe handler hits a probe, that second probe's
0568 handlers won't be run in that instance, and the kprobe.nmissed member
0569 of the second probe will be incremented.
0570 
0571 As of Linux v2.6.15-rc1, multiple handlers (or multiple instances of
0572 the same handler) may run concurrently on different CPUs.
0573 
0574 Kprobes does not use mutexes or allocate memory except during
0575 registration and unregistration.
0576 
0577 Probe handlers are run with preemption disabled or interrupt disabled,
0578 which depends on the architecture and optimization state.  (e.g.,
0579 kretprobe handlers and optimized kprobe handlers run without interrupt
0580 disabled on x86/x86-64).  In any case, your handler should not yield
0581 the CPU (e.g., by attempting to acquire a semaphore, or waiting I/O).
0582 
0583 Since a return probe is implemented by replacing the return
0584 address with the trampoline's address, stack backtraces and calls
0585 to __builtin_return_address() will typically yield the trampoline's
0586 address instead of the real return address for kretprobed functions.
0587 (As far as we can tell, __builtin_return_address() is used only
0588 for instrumentation and error reporting.)
0589 
0590 If the number of times a function is called does not match the number
0591 of times it returns, registering a return probe on that function may
0592 produce undesirable results. In such a case, a line:
0593 kretprobe BUG!: Processing kretprobe d000000000041aa8 @ c00000000004f48c
0594 gets printed. With this information, one will be able to correlate the
0595 exact instance of the kretprobe that caused the problem. We have the
0596 do_exit() case covered. do_execve() and do_fork() are not an issue.
0597 We're unaware of other specific cases where this could be a problem.
0598 
0599 If, upon entry to or exit from a function, the CPU is running on
0600 a stack other than that of the current task, registering a return
0601 probe on that function may produce undesirable results.  For this
0602 reason, Kprobes doesn't support return probes (or kprobes)
0603 on the x86_64 version of __switch_to(); the registration functions
0604 return -EINVAL.
0605 
0606 On x86/x86-64, since the Jump Optimization of Kprobes modifies
0607 instructions widely, there are some limitations to optimization. To
0608 explain it, we introduce some terminology. Imagine a 3-instruction
0609 sequence consisting of a two 2-byte instructions and one 3-byte
0610 instruction.
0611 
0612 ::
0613 
0614                 IA
0615                 |
0616         [-2][-1][0][1][2][3][4][5][6][7]
0617                 [ins1][ins2][  ins3 ]
0618                 [<-     DCR       ->]
0619                 [<- JTPR ->]
0620 
0621         ins1: 1st Instruction
0622         ins2: 2nd Instruction
0623         ins3: 3rd Instruction
0624         IA:  Insertion Address
0625         JTPR: Jump Target Prohibition Region
0626         DCR: Detoured Code Region
0627 
0628 The instructions in DCR are copied to the out-of-line buffer
0629 of the kprobe, because the bytes in DCR are replaced by
0630 a 5-byte jump instruction. So there are several limitations.
0631 
0632 a) The instructions in DCR must be relocatable.
0633 b) The instructions in DCR must not include a call instruction.
0634 c) JTPR must not be targeted by any jump or call instruction.
0635 d) DCR must not straddle the border between functions.
0636 
0637 Anyway, these limitations are checked by the in-kernel instruction
0638 decoder, so you don't need to worry about that.
0639 
0640 Probe Overhead
0641 ==============
0642 
0643 On a typical CPU in use in 2005, a kprobe hit takes 0.5 to 1.0
0644 microseconds to process.  Specifically, a benchmark that hits the same
0645 probepoint repeatedly, firing a simple handler each time, reports 1-2
0646 million hits per second, depending on the architecture.  A return-probe
0647 hit typically takes 50-75% longer than a kprobe hit.
0648 When you have a return probe set on a function, adding a kprobe at
0649 the entry to that function adds essentially no overhead.
0650 
0651 Here are sample overhead figures (in usec) for different architectures::
0652 
0653   k = kprobe; r = return probe; kr = kprobe + return probe
0654   on same function
0655 
0656   i386: Intel Pentium M, 1495 MHz, 2957.31 bogomips
0657   k = 0.57 usec; r = 0.92; kr = 0.99
0658 
0659   x86_64: AMD Opteron 246, 1994 MHz, 3971.48 bogomips
0660   k = 0.49 usec; r = 0.80; kr = 0.82
0661 
0662   ppc64: POWER5 (gr), 1656 MHz (SMT disabled, 1 virtual CPU per physical CPU)
0663   k = 0.77 usec; r = 1.26; kr = 1.45
0664 
0665 Optimized Probe Overhead
0666 ------------------------
0667 
0668 Typically, an optimized kprobe hit takes 0.07 to 0.1 microseconds to
0669 process. Here are sample overhead figures (in usec) for x86 architectures::
0670 
0671   k = unoptimized kprobe, b = boosted (single-step skipped), o = optimized kprobe,
0672   r = unoptimized kretprobe, rb = boosted kretprobe, ro = optimized kretprobe.
0673 
0674   i386: Intel(R) Xeon(R) E5410, 2.33GHz, 4656.90 bogomips
0675   k = 0.80 usec; b = 0.33; o = 0.05; r = 1.10; rb = 0.61; ro = 0.33
0676 
0677   x86-64: Intel(R) Xeon(R) E5410, 2.33GHz, 4656.90 bogomips
0678   k = 0.99 usec; b = 0.43; o = 0.06; r = 1.24; rb = 0.68; ro = 0.30
0679 
0680 TODO
0681 ====
0682 
0683 a. SystemTap (http://sourceware.org/systemtap): Provides a simplified
0684    programming interface for probe-based instrumentation.  Try it out.
0685 b. Kernel return probes for sparc64.
0686 c. Support for other architectures.
0687 d. User-space probes.
0688 e. Watchpoint probes (which fire on data references).
0689 
0690 Kprobes Example
0691 ===============
0692 
0693 See samples/kprobes/kprobe_example.c
0694 
0695 Kretprobes Example
0696 ==================
0697 
0698 See samples/kprobes/kretprobe_example.c
0699 
0700 Deprecated Features
0701 ===================
0702 
0703 Jprobes is now a deprecated feature. People who are depending on it should
0704 migrate to other tracing features or use older kernels. Please consider to
0705 migrate your tool to one of the following options:
0706 
0707 - Use trace-event to trace target function with arguments.
0708 
0709   trace-event is a low-overhead (and almost no visible overhead if it
0710   is off) statically defined event interface. You can define new events
0711   and trace it via ftrace or any other tracing tools.
0712 
0713   See the following urls:
0714 
0715     - https://lwn.net/Articles/379903/
0716     - https://lwn.net/Articles/381064/
0717     - https://lwn.net/Articles/383362/
0718 
0719 - Use ftrace dynamic events (kprobe event) with perf-probe.
0720 
0721   If you build your kernel with debug info (CONFIG_DEBUG_INFO=y), you can
0722   find which register/stack is assigned to which local variable or arguments
0723   by using perf-probe and set up new event to trace it.
0724 
0725   See following documents:
0726 
0727   - Documentation/trace/kprobetrace.rst
0728   - Documentation/trace/events.rst
0729   - tools/perf/Documentation/perf-probe.txt
0730 
0731 
0732 The kprobes debugfs interface
0733 =============================
0734 
0735 
0736 With recent kernels (> 2.6.20) the list of registered kprobes is visible
0737 under the /sys/kernel/debug/kprobes/ directory (assuming debugfs is mounted at //sys/kernel/debug).
0738 
0739 /sys/kernel/debug/kprobes/list: Lists all registered probes on the system::
0740 
0741         c015d71a  k  vfs_read+0x0
0742         c03dedc5  r  tcp_v4_rcv+0x0
0743 
0744 The first column provides the kernel address where the probe is inserted.
0745 The second column identifies the type of probe (k - kprobe and r - kretprobe)
0746 while the third column specifies the symbol+offset of the probe.
0747 If the probed function belongs to a module, the module name is also
0748 specified. Following columns show probe status. If the probe is on
0749 a virtual address that is no longer valid (module init sections, module
0750 virtual addresses that correspond to modules that've been unloaded),
0751 such probes are marked with [GONE]. If the probe is temporarily disabled,
0752 such probes are marked with [DISABLED]. If the probe is optimized, it is
0753 marked with [OPTIMIZED]. If the probe is ftrace-based, it is marked with
0754 [FTRACE].
0755 
0756 /sys/kernel/debug/kprobes/enabled: Turn kprobes ON/OFF forcibly.
0757 
0758 Provides a knob to globally and forcibly turn registered kprobes ON or OFF.
0759 By default, all kprobes are enabled. By echoing "0" to this file, all
0760 registered probes will be disarmed, till such time a "1" is echoed to this
0761 file. Note that this knob just disarms and arms all kprobes and doesn't
0762 change each probe's disabling state. This means that disabled kprobes (marked
0763 [DISABLED]) will be not enabled if you turn ON all kprobes by this knob.
0764 
0765 
0766 The kprobes sysctl interface
0767 ============================
0768 
0769 /proc/sys/debug/kprobes-optimization: Turn kprobes optimization ON/OFF.
0770 
0771 When CONFIG_OPTPROBES=y, this sysctl interface appears and it provides
0772 a knob to globally and forcibly turn jump optimization (see section
0773 :ref:`kprobes_jump_optimization`) ON or OFF. By default, jump optimization
0774 is allowed (ON). If you echo "0" to this file or set
0775 "debug.kprobes_optimization" to 0 via sysctl, all optimized probes will be
0776 unoptimized, and any new probes registered after that will not be optimized.
0777 
0778 Note that this knob *changes* the optimized state. This means that optimized
0779 probes (marked [OPTIMIZED]) will be unoptimized ([OPTIMIZED] tag will be
0780 removed). If the knob is turned on, they will be optimized again.
0781 
0782 References
0783 ==========
0784 
0785 For additional information on Kprobes, refer to the following URLs:
0786 
0787 - https://lwn.net/Articles/132196/
0788 - https://www.kernel.org/doc/ols/2006/ols2006v2-pages-109-124.pdf
0789