ftrace.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. bit = ftrace_test_recursion_trylock(ip, parent_ip);
  22. if (bit < 0)
  23. return;
  24. p = get_kprobe((kprobe_opcode_t *)ip);
  25. if (unlikely(!p) || kprobe_disabled(p))
  26. goto out;
  27. kcb = get_kprobe_ctlblk();
  28. if (kprobe_running()) {
  29. kprobes_inc_nmissed_count(p);
  30. } else {
  31. unsigned long orig_ip = regs->ip;
  32. /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
  33. regs->ip = ip + sizeof(kprobe_opcode_t);
  34. __this_cpu_write(current_kprobe, p);
  35. kcb->kprobe_status = KPROBE_HIT_ACTIVE;
  36. if (!p->pre_handler || !p->pre_handler(p, regs)) {
  37. /*
  38. * Emulate singlestep (and also recover regs->ip)
  39. * as if there is a 5byte nop
  40. */
  41. regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
  42. if (unlikely(p->post_handler)) {
  43. kcb->kprobe_status = KPROBE_HIT_SSDONE;
  44. p->post_handler(p, regs, 0);
  45. }
  46. regs->ip = orig_ip;
  47. }
  48. /*
  49. * If pre_handler returns !0, it changes regs->ip. We have to
  50. * skip emulating post_handler.
  51. */
  52. __this_cpu_write(current_kprobe, NULL);
  53. }
  54. out:
  55. ftrace_test_recursion_unlock(bit);
  56. }
  57. NOKPROBE_SYMBOL(kprobe_ftrace_handler);
  58. int arch_prepare_kprobe_ftrace(struct kprobe *p)
  59. {
  60. p->ainsn.insn = NULL;
  61. p->ainsn.boostable = false;
  62. return 0;
  63. }