1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/module.h> 3 #include <linux/kthread.h> 4 #include <linux/ftrace.h> 5 #include <asm/asm-offsets.h> 6 7 extern void my_direct_func1(void); 8 extern void my_direct_func2(void); 9 10 void my_direct_func1(void) 11 { 12 trace_printk("my direct func1\n"); 13 } 14 15 void my_direct_func2(void) 16 { 17 trace_printk("my direct func2\n"); 18 } 19 20 extern void my_tramp1(void *); 21 extern void my_tramp2(void *); 22 23 static unsigned long my_ip = (unsigned long)schedule; 24 25 #ifdef CONFIG_X86_64 26 27 asm ( 28 " .pushsection .text, \"ax\", @progbits\n" 29 " .type my_tramp1, @function\n" 30 " .globl my_tramp1\n" 31 " my_tramp1:" 32 " pushq %rbp\n" 33 " movq %rsp, %rbp\n" 34 " call my_direct_func1\n" 35 " leave\n" 36 " .size my_tramp1, .-my_tramp1\n" 37 ASM_RET 38 " .type my_tramp2, @function\n" 39 " .globl my_tramp2\n" 40 " my_tramp2:" 41 " pushq %rbp\n" 42 " movq %rsp, %rbp\n" 43 " call my_direct_func2\n" 44 " leave\n" 45 ASM_RET 46 " .size my_tramp2, .-my_tramp2\n" 47 " .popsection\n" 48 ); 49 50 #endif /* CONFIG_X86_64 */ 51 52 #ifdef CONFIG_S390 53 54 asm ( 55 " .pushsection .text, \"ax\", @progbits\n" 56 " .type my_tramp1, @function\n" 57 " .globl my_tramp1\n" 58 " my_tramp1:" 59 " lgr %r1,%r15\n" 60 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 61 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 62 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n" 63 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n" 64 " brasl %r14,my_direct_func1\n" 65 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n" 66 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 67 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 68 " lgr %r1,%r0\n" 69 " br %r1\n" 70 " .size my_tramp1, .-my_tramp1\n" 71 " .type my_tramp2, @function\n" 72 " .globl my_tramp2\n" 73 " my_tramp2:" 74 " lgr %r1,%r15\n" 75 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 76 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 77 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n" 78 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n" 79 " brasl %r14,my_direct_func2\n" 80 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n" 81 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n" 82 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n" 83 " lgr %r1,%r0\n" 84 " br %r1\n" 85 " .size my_tramp2, .-my_tramp2\n" 86 " .popsection\n" 87 ); 88 89 #endif /* CONFIG_S390 */ 90 91 static unsigned long my_tramp = (unsigned long)my_tramp1; 92 static unsigned long tramps[2] = { 93 (unsigned long)my_tramp1, 94 (unsigned long)my_tramp2, 95 }; 96 97 static int simple_thread(void *arg) 98 { 99 static int t; 100 int ret = 0; 101 102 while (!kthread_should_stop()) { 103 set_current_state(TASK_INTERRUPTIBLE); 104 schedule_timeout(2 * HZ); 105 106 if (ret) 107 continue; 108 t ^= 1; 109 ret = modify_ftrace_direct(my_ip, my_tramp, tramps[t]); 110 if (!ret) 111 my_tramp = tramps[t]; 112 WARN_ON_ONCE(ret); 113 } 114 115 return 0; 116 } 117 118 static struct task_struct *simple_tsk; 119 120 static int __init ftrace_direct_init(void) 121 { 122 int ret; 123 124 ret = register_ftrace_direct(my_ip, my_tramp); 125 if (!ret) 126 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn"); 127 return ret; 128 } 129 130 static void __exit ftrace_direct_exit(void) 131 { 132 kthread_stop(simple_tsk); 133 unregister_ftrace_direct(my_ip, my_tramp); 134 } 135 136 module_init(ftrace_direct_init); 137 module_exit(ftrace_direct_exit); 138 139 MODULE_AUTHOR("Steven Rostedt"); 140 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()"); 141 MODULE_LICENSE("GPL"); 142