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>
7 extern void my_direct_func1(unsigned long ip);
8 extern void my_direct_func2(unsigned long ip);
10 void my_direct_func1(unsigned long ip)
12 trace_printk("my direct func1 ip %lx\n", ip);
15 void my_direct_func2(unsigned long ip)
17 trace_printk("my direct func2 ip %lx\n", ip);
20 extern void my_tramp1(void *);
21 extern void my_tramp2(void *);
28 " .pushsection .text, \"ax\", @progbits\n"
29 " .type my_tramp1, @function\n"
36 " movq 8(%rbp), %rdi\n"
37 " call my_direct_func1\n"
41 " .size my_tramp1, .-my_tramp1\n"
43 " .type my_tramp2, @function\n"
50 " movq 8(%rbp), %rdi\n"
51 " call my_direct_func2\n"
55 " .size my_tramp2, .-my_tramp2\n"
59 #endif /* CONFIG_X86_64 */
64 " .pushsection .text, \"ax\", @progbits\n"
65 " .type my_tramp1, @function\n"
69 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
70 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
71 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
72 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
74 " brasl %r14,my_direct_func1\n"
75 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
76 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
77 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
80 " .size my_tramp1, .-my_tramp1\n"
82 " .type my_tramp2, @function\n"
86 " stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
87 " stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
88 " aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
89 " stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
91 " brasl %r14,my_direct_func2\n"
92 " aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
93 " lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
94 " lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
97 " .size my_tramp2, .-my_tramp2\n"
101 #endif /* CONFIG_S390 */
103 static unsigned long my_tramp = (unsigned long)my_tramp1;
104 static unsigned long tramps[2] = {
105 (unsigned long)my_tramp1,
106 (unsigned long)my_tramp2,
109 static struct ftrace_ops direct;
111 static int simple_thread(void *arg)
116 while (!kthread_should_stop()) {
117 set_current_state(TASK_INTERRUPTIBLE);
118 schedule_timeout(2 * HZ);
123 ret = modify_ftrace_direct_multi(&direct, tramps[t]);
125 my_tramp = tramps[t];
132 static struct task_struct *simple_tsk;
134 static int __init ftrace_direct_multi_init(void)
138 ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
139 ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
141 ret = register_ftrace_direct_multi(&direct, my_tramp);
144 simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
148 static void __exit ftrace_direct_multi_exit(void)
150 kthread_stop(simple_tsk);
151 unregister_ftrace_direct_multi(&direct, my_tramp);
154 module_init(ftrace_direct_multi_init);
155 module_exit(ftrace_direct_multi_exit);
157 MODULE_AUTHOR("Jiri Olsa");
158 MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct_multi()");
159 MODULE_LICENSE("GPL");