ftrace.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Dynamic Ftrace based Kprobes Optimization
  4. *
  5. * Copyright (C) Hitachi Ltd., 2012
  6. */
  7. #include <linux/kprobes.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/preempt.h>
  11. #include <linux/ftrace.h>
  12. #include "common.h"
  13. /* Ftrace callback handler for kprobes -- called under preempt disabled */
  14. void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
  15. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  16. {
  17. struct pt_regs *regs = ftrace_get_regs(fregs);
  18. struct kprobe *p;
  19. struct kprobe_ctlblk *kcb;
  20. int bit;
  21. if (unlikely(kprobe_ftrace_disabled))
  22. return;
  23. bit = ftrace_test_recursion_trylock(ip, parent_ip);
  24. if (bit < 0)
  25. return;
  26. p = get_kprobe((kprobe_opcode_t *)ip);
  27. if (unlikely(!p) || kprobe_disabled(p))
  28. goto out;
  29. kcb = get_kprobe_ctlblk();
  30. if (kprobe_running()) {
  31. kprobes_inc_nmissed_count(p);
  32. } else {
  33. unsigned long orig_ip = regs->ip;
  34. /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
  35. regs->ip = ip + sizeof(kprobe_opcode_t);
  36. __this_cpu_write(current_kprobe, p);
  37. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  38. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  39. /*
  40. * Emulate singlestep (and also recover regs->ip)
  41. * as if there is a 5byte nop
  42. */
  43. regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
  44. if (unlikely(p->post_handler)) {
  45. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  46. p->post_handler(p, regs, 0);
  47. }
  48. regs->ip = orig_ip;
  49. }
  50. /*
  51. * If pre_handler returns !0, it changes regs->ip. We have to
  52. * skip emulating post_handler.
  53. */
  54. __this_cpu_write(current_kprobe, NULL);
  55. }
  56. out:
  57. ftrace_test_recursion_unlock(bit);
  58. }
  59. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  60. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  61. {
  62. p->ainsn.insn = NULL;
  63. p->ainsn.boostable = false;
  64. return 0;
  65. }