ftrace-direct-multi-modify.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/kthread.h>
  4. #include <linux/ftrace.h>
  5. #ifndef CONFIG_ARM64
  6. #include <asm/asm-offsets.h>
  7. #endif
  8. extern void my_direct_func1(unsigned long ip);
  9. extern void my_direct_func2(unsigned long ip);
  10. void my_direct_func1(unsigned long ip)
  11. {
  12. trace_printk("my direct func1 ip %lx\n", ip);
  13. }
  14. void my_direct_func2(unsigned long ip)
  15. {
  16. trace_printk("my direct func2 ip %lx\n", ip);
  17. }
  18. extern void my_tramp1(void *);
  19. extern void my_tramp2(void *);
  20. #ifdef CONFIG_RISCV
  21. #include <asm/asm.h>
  22. asm (
  23. " .pushsection .text, \"ax\", @progbits\n"
  24. " .type my_tramp1, @function\n"
  25. " .globl my_tramp1\n"
  26. " my_tramp1:\n"
  27. " addi sp,sp,-3*"SZREG"\n"
  28. " "REG_S" a0,0*"SZREG"(sp)\n"
  29. " "REG_S" t0,1*"SZREG"(sp)\n"
  30. " "REG_S" ra,2*"SZREG"(sp)\n"
  31. " mv a0,t0\n"
  32. " call my_direct_func1\n"
  33. " "REG_L" a0,0*"SZREG"(sp)\n"
  34. " "REG_L" t0,1*"SZREG"(sp)\n"
  35. " "REG_L" ra,2*"SZREG"(sp)\n"
  36. " addi sp,sp,3*"SZREG"\n"
  37. " jr t0\n"
  38. " .size my_tramp1, .-my_tramp1\n"
  39. " .type my_tramp2, @function\n"
  40. " .globl my_tramp2\n"
  41. " my_tramp2:\n"
  42. " addi sp,sp,-3*"SZREG"\n"
  43. " "REG_S" a0,0*"SZREG"(sp)\n"
  44. " "REG_S" t0,1*"SZREG"(sp)\n"
  45. " "REG_S" ra,2*"SZREG"(sp)\n"
  46. " mv a0,t0\n"
  47. " call my_direct_func2\n"
  48. " "REG_L" a0,0*"SZREG"(sp)\n"
  49. " "REG_L" t0,1*"SZREG"(sp)\n"
  50. " "REG_L" ra,2*"SZREG"(sp)\n"
  51. " addi sp,sp,3*"SZREG"\n"
  52. " jr t0\n"
  53. " .size my_tramp2, .-my_tramp2\n"
  54. " .popsection\n"
  55. );
  56. #endif /* CONFIG_RISCV */
  57. #ifdef CONFIG_X86_64
  58. #include <asm/ibt.h>
  59. #include <asm/nospec-branch.h>
  60. asm (
  61. " .pushsection .text, \"ax\", @progbits\n"
  62. " .type my_tramp1, @function\n"
  63. " .globl my_tramp1\n"
  64. " my_tramp1:"
  65. ASM_ENDBR
  66. " pushq %rbp\n"
  67. " movq %rsp, %rbp\n"
  68. CALL_DEPTH_ACCOUNT
  69. " pushq %rdi\n"
  70. " movq 8(%rbp), %rdi\n"
  71. " call my_direct_func1\n"
  72. " popq %rdi\n"
  73. " leave\n"
  74. ASM_RET
  75. " .size my_tramp1, .-my_tramp1\n"
  76. " .type my_tramp2, @function\n"
  77. " .globl my_tramp2\n"
  78. " my_tramp2:"
  79. ASM_ENDBR
  80. " pushq %rbp\n"
  81. " movq %rsp, %rbp\n"
  82. CALL_DEPTH_ACCOUNT
  83. " pushq %rdi\n"
  84. " movq 8(%rbp), %rdi\n"
  85. " call my_direct_func2\n"
  86. " popq %rdi\n"
  87. " leave\n"
  88. ASM_RET
  89. " .size my_tramp2, .-my_tramp2\n"
  90. " .popsection\n"
  91. );
  92. #endif /* CONFIG_X86_64 */
  93. #ifdef CONFIG_S390
  94. asm (
  95. " .pushsection .text, \"ax\", @progbits\n"
  96. " .type my_tramp1, @function\n"
  97. " .globl my_tramp1\n"
  98. " my_tramp1:"
  99. " lgr %r1,%r15\n"
  100. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  101. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  102. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  103. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  104. " lgr %r2,%r0\n"
  105. " brasl %r14,my_direct_func1\n"
  106. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  107. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  108. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  109. " lgr %r1,%r0\n"
  110. " br %r1\n"
  111. " .size my_tramp1, .-my_tramp1\n"
  112. "\n"
  113. " .type my_tramp2, @function\n"
  114. " .globl my_tramp2\n"
  115. " my_tramp2:"
  116. " lgr %r1,%r15\n"
  117. " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  118. " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  119. " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
  120. " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
  121. " lgr %r2,%r0\n"
  122. " brasl %r14,my_direct_func2\n"
  123. " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
  124. " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
  125. " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
  126. " lgr %r1,%r0\n"
  127. " br %r1\n"
  128. " .size my_tramp2, .-my_tramp2\n"
  129. " .popsection\n"
  130. );
  131. #endif /* CONFIG_S390 */
  132. #ifdef CONFIG_ARM64
  133. asm (
  134. " .pushsection .text, \"ax\", @progbits\n"
  135. " .type my_tramp1, @function\n"
  136. " .globl my_tramp1\n"
  137. " my_tramp1:"
  138. " hint 34\n" // bti c
  139. " sub sp, sp, #32\n"
  140. " stp x9, x30, [sp]\n"
  141. " str x0, [sp, #16]\n"
  142. " mov x0, x30\n"
  143. " bl my_direct_func1\n"
  144. " ldp x30, x9, [sp]\n"
  145. " ldr x0, [sp, #16]\n"
  146. " add sp, sp, #32\n"
  147. " ret x9\n"
  148. " .size my_tramp1, .-my_tramp1\n"
  149. " .type my_tramp2, @function\n"
  150. " .globl my_tramp2\n"
  151. " my_tramp2:"
  152. " hint 34\n" // bti c
  153. " sub sp, sp, #32\n"
  154. " stp x9, x30, [sp]\n"
  155. " str x0, [sp, #16]\n"
  156. " mov x0, x30\n"
  157. " bl my_direct_func2\n"
  158. " ldp x30, x9, [sp]\n"
  159. " ldr x0, [sp, #16]\n"
  160. " add sp, sp, #32\n"
  161. " ret x9\n"
  162. " .size my_tramp2, .-my_tramp2\n"
  163. " .popsection\n"
  164. );
  165. #endif /* CONFIG_ARM64 */
  166. #ifdef CONFIG_LOONGARCH
  167. #include <asm/asm.h>
  168. asm (
  169. " .pushsection .text, \"ax\", @progbits\n"
  170. " .type my_tramp1, @function\n"
  171. " .globl my_tramp1\n"
  172. " my_tramp1:\n"
  173. " addi.d $sp, $sp, -32\n"
  174. " st.d $a0, $sp, 0\n"
  175. " st.d $t0, $sp, 8\n"
  176. " st.d $ra, $sp, 16\n"
  177. " move $a0, $t0\n"
  178. " bl my_direct_func1\n"
  179. " ld.d $a0, $sp, 0\n"
  180. " ld.d $t0, $sp, 8\n"
  181. " ld.d $ra, $sp, 16\n"
  182. " addi.d $sp, $sp, 32\n"
  183. " jr $t0\n"
  184. " .size my_tramp1, .-my_tramp1\n"
  185. " .type my_tramp2, @function\n"
  186. " .globl my_tramp2\n"
  187. " my_tramp2:\n"
  188. " addi.d $sp, $sp, -32\n"
  189. " st.d $a0, $sp, 0\n"
  190. " st.d $t0, $sp, 8\n"
  191. " st.d $ra, $sp, 16\n"
  192. " move $a0, $t0\n"
  193. " bl my_direct_func2\n"
  194. " ld.d $a0, $sp, 0\n"
  195. " ld.d $t0, $sp, 8\n"
  196. " ld.d $ra, $sp, 16\n"
  197. " addi.d $sp, $sp, 32\n"
  198. " jr $t0\n"
  199. " .size my_tramp2, .-my_tramp2\n"
  200. " .popsection\n"
  201. );
  202. #endif /* CONFIG_LOONGARCH */
  203. static unsigned long my_tramp = (unsigned long)my_tramp1;
  204. static unsigned long tramps[2] = {
  205. (unsigned long)my_tramp1,
  206. (unsigned long)my_tramp2,
  207. };
  208. static struct ftrace_ops direct;
  209. static int simple_thread(void *arg)
  210. {
  211. static int t;
  212. int ret = 0;
  213. while (!kthread_should_stop()) {
  214. set_current_state(TASK_INTERRUPTIBLE);
  215. schedule_timeout(2 * HZ);
  216. if (ret)
  217. continue;
  218. t ^= 1;
  219. ret = modify_ftrace_direct(&direct, tramps[t]);
  220. if (!ret)
  221. my_tramp = tramps[t];
  222. WARN_ON_ONCE(ret);
  223. }
  224. return 0;
  225. }
  226. static struct task_struct *simple_tsk;
  227. static int __init ftrace_direct_multi_init(void)
  228. {
  229. int ret;
  230. ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
  231. ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
  232. ret = register_ftrace_direct(&direct, my_tramp);
  233. if (!ret)
  234. simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
  235. return ret;
  236. }
  237. static void __exit ftrace_direct_multi_exit(void)
  238. {
  239. kthread_stop(simple_tsk);
  240. unregister_ftrace_direct(&direct, my_tramp, true);
  241. }
  242. module_init(ftrace_direct_multi_init);
  243. module_exit(ftrace_direct_multi_exit);
  244. MODULE_AUTHOR("Jiri Olsa");
  245. MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
  246. MODULE_LICENSE("GPL");