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