1 // SPDX-License-Identifier: GPL-2.0-only << 2 #include <linux/module.h> 1 #include <linux/module.h> 3 #include <linux/kthread.h> 2 #include <linux/kthread.h> 4 3 5 /* 4 /* 6 * Any file that uses trace points, must inclu 5 * Any file that uses trace points, must include the header. 7 * But only one file, must include the header 6 * But only one file, must include the header by defining 8 * CREATE_TRACE_POINTS first. This will make 7 * CREATE_TRACE_POINTS first. This will make the C code that 9 * creates the handles for the trace points. 8 * creates the handles for the trace points. 10 */ 9 */ 11 #define CREATE_TRACE_POINTS 10 #define CREATE_TRACE_POINTS 12 #include "trace-events-sample.h" 11 #include "trace-events-sample.h" 13 12 14 static const char *random_strings[] = { 13 static const char *random_strings[] = { 15 "Mother Goose", 14 "Mother Goose", 16 "Snoopy", 15 "Snoopy", 17 "Gandalf", 16 "Gandalf", 18 "Frodo", 17 "Frodo", 19 "One ring to rule them all" 18 "One ring to rule them all" 20 }; 19 }; 21 20 22 static void do_simple_thread_func(int cnt, con !! 21 static void simple_thread_func(int cnt) 23 { 22 { 24 unsigned long bitmask[1] = {0xdeadbeef << 25 va_list va; << 26 int array[6]; 23 int array[6]; 27 int len = cnt % 5; 24 int len = cnt % 5; 28 int i; 25 int i; 29 26 30 set_current_state(TASK_INTERRUPTIBLE); 27 set_current_state(TASK_INTERRUPTIBLE); 31 schedule_timeout(HZ); 28 schedule_timeout(HZ); 32 29 33 for (i = 0; i < len; i++) 30 for (i = 0; i < len; i++) 34 array[i] = i + 1; 31 array[i] = i + 1; 35 array[i] = 0; 32 array[i] = 0; 36 33 37 va_start(va, fmt); << 38 << 39 /* Silly tracepoints */ 34 /* Silly tracepoints */ 40 trace_foo_bar("hello", cnt, array, ran 35 trace_foo_bar("hello", cnt, array, random_strings[len], 41 current->cpus_ptr, fmt, !! 36 ¤t->cpus_allowed); 42 << 43 va_end(va); << 44 37 45 trace_foo_with_template_simple("HELLO" 38 trace_foo_with_template_simple("HELLO", cnt); 46 39 47 trace_foo_bar_with_cond("Some times pr 40 trace_foo_bar_with_cond("Some times print", cnt); 48 41 49 trace_foo_with_template_cond("prints o 42 trace_foo_with_template_cond("prints other times", cnt); 50 43 51 trace_foo_with_template_print("I have 44 trace_foo_with_template_print("I have to be different", cnt); 52 << 53 trace_foo_rel_loc("Hello __rel_loc", c << 54 } << 55 << 56 static void simple_thread_func(int cnt) << 57 { << 58 do_simple_thread_func(cnt, "iter=%d", << 59 } 45 } 60 46 61 static int simple_thread(void *arg) 47 static int simple_thread(void *arg) 62 { 48 { 63 int cnt = 0; 49 int cnt = 0; 64 50 65 while (!kthread_should_stop()) 51 while (!kthread_should_stop()) 66 simple_thread_func(cnt++); 52 simple_thread_func(cnt++); 67 53 68 return 0; 54 return 0; 69 } 55 } 70 56 71 static struct task_struct *simple_tsk; 57 static struct task_struct *simple_tsk; 72 static struct task_struct *simple_tsk_fn; 58 static struct task_struct *simple_tsk_fn; 73 59 74 static void simple_thread_func_fn(int cnt) 60 static void simple_thread_func_fn(int cnt) 75 { 61 { 76 set_current_state(TASK_INTERRUPTIBLE); 62 set_current_state(TASK_INTERRUPTIBLE); 77 schedule_timeout(HZ); 63 schedule_timeout(HZ); 78 64 79 /* More silly tracepoints */ 65 /* More silly tracepoints */ 80 trace_foo_bar_with_fn("Look at me", cn 66 trace_foo_bar_with_fn("Look at me", cnt); 81 trace_foo_with_template_fn("Look at me 67 trace_foo_with_template_fn("Look at me too", cnt); 82 } 68 } 83 69 84 static int simple_thread_fn(void *arg) 70 static int simple_thread_fn(void *arg) 85 { 71 { 86 int cnt = 0; 72 int cnt = 0; 87 73 88 while (!kthread_should_stop()) 74 while (!kthread_should_stop()) 89 simple_thread_func_fn(cnt++); 75 simple_thread_func_fn(cnt++); 90 76 91 return 0; 77 return 0; 92 } 78 } 93 79 94 static DEFINE_MUTEX(thread_mutex); 80 static DEFINE_MUTEX(thread_mutex); 95 static int simple_thread_cnt; 81 static int simple_thread_cnt; 96 82 97 int foo_bar_reg(void) 83 int foo_bar_reg(void) 98 { 84 { 99 mutex_lock(&thread_mutex); 85 mutex_lock(&thread_mutex); 100 if (simple_thread_cnt++) 86 if (simple_thread_cnt++) 101 goto out; 87 goto out; 102 88 103 pr_info("Starting thread for foo_bar_f 89 pr_info("Starting thread for foo_bar_fn\n"); 104 /* 90 /* 105 * We shouldn't be able to start a tra 91 * We shouldn't be able to start a trace when the module is 106 * unloading (there's other locks to p 92 * unloading (there's other locks to prevent that). But 107 * for consistency sake, we still take 93 * for consistency sake, we still take the thread_mutex. 108 */ 94 */ 109 simple_tsk_fn = kthread_run(simple_thr 95 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn"); 110 out: 96 out: 111 mutex_unlock(&thread_mutex); 97 mutex_unlock(&thread_mutex); 112 return 0; 98 return 0; 113 } 99 } 114 100 115 void foo_bar_unreg(void) 101 void foo_bar_unreg(void) 116 { 102 { 117 mutex_lock(&thread_mutex); 103 mutex_lock(&thread_mutex); 118 if (--simple_thread_cnt) 104 if (--simple_thread_cnt) 119 goto out; 105 goto out; 120 106 121 pr_info("Killing thread for foo_bar_fn 107 pr_info("Killing thread for foo_bar_fn\n"); 122 if (simple_tsk_fn) 108 if (simple_tsk_fn) 123 kthread_stop(simple_tsk_fn); 109 kthread_stop(simple_tsk_fn); 124 simple_tsk_fn = NULL; 110 simple_tsk_fn = NULL; 125 out: 111 out: 126 mutex_unlock(&thread_mutex); 112 mutex_unlock(&thread_mutex); 127 } 113 } 128 114 129 static int __init trace_event_init(void) 115 static int __init trace_event_init(void) 130 { 116 { 131 simple_tsk = kthread_run(simple_thread 117 simple_tsk = kthread_run(simple_thread, NULL, "event-sample"); 132 if (IS_ERR(simple_tsk)) 118 if (IS_ERR(simple_tsk)) 133 return -1; 119 return -1; 134 120 135 return 0; 121 return 0; 136 } 122 } 137 123 138 static void __exit trace_event_exit(void) 124 static void __exit trace_event_exit(void) 139 { 125 { 140 kthread_stop(simple_tsk); 126 kthread_stop(simple_tsk); 141 mutex_lock(&thread_mutex); 127 mutex_lock(&thread_mutex); 142 if (simple_tsk_fn) 128 if (simple_tsk_fn) 143 kthread_stop(simple_tsk_fn); 129 kthread_stop(simple_tsk_fn); 144 simple_tsk_fn = NULL; 130 simple_tsk_fn = NULL; 145 mutex_unlock(&thread_mutex); 131 mutex_unlock(&thread_mutex); 146 } 132 } 147 133 148 module_init(trace_event_init); 134 module_init(trace_event_init); 149 module_exit(trace_event_exit); 135 module_exit(trace_event_exit); 150 136 151 MODULE_AUTHOR("Steven Rostedt"); 137 MODULE_AUTHOR("Steven Rostedt"); 152 MODULE_DESCRIPTION("trace-events-sample"); 138 MODULE_DESCRIPTION("trace-events-sample"); 153 MODULE_LICENSE("GPL"); 139 MODULE_LICENSE("GPL"); 154 140
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.