1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/module.h>
19#include <linux/mutex.h>
20#include <linux/types.h>
21#include <linux/jhash.h>
22#include <linux/list.h>
23#include <linux/rcupdate.h>
24#include <linux/tracepoint.h>
25#include <linux/err.h>
26#include <linux/slab.h>
27#include <linux/sched/signal.h>
28#include <linux/sched/task.h>
29#include <linux/static_key.h>
30
31extern struct tracepoint * const __start___tracepoints_ptrs[];
32extern struct tracepoint * const __stop___tracepoints_ptrs[];
33
34DEFINE_SRCU(tracepoint_srcu);
35EXPORT_SYMBOL_GPL(tracepoint_srcu);
36
37
38static const int tracepoint_debug;
39
40#ifdef CONFIG_MODULES
41
42
43
44static DEFINE_MUTEX(tracepoint_module_list_mutex);
45
46
47static LIST_HEAD(tracepoint_module_list);
48#endif
49
50
51
52
53
54static DEFINE_MUTEX(tracepoints_mutex);
55
56static struct rcu_head *early_probes;
57static bool ok_to_free_tracepoints;
58
59
60
61
62
63
64struct tp_probes {
65 struct rcu_head rcu;
66 struct tracepoint_func probes[0];
67};
68
69static inline void *allocate_probes(int count)
70{
71 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
72 + sizeof(struct tp_probes), GFP_KERNEL);
73 return p == NULL ? NULL : p->probes;
74}
75
76static void srcu_free_old_probes(struct rcu_head *head)
77{
78 kfree(container_of(head, struct tp_probes, rcu));
79}
80
81static void rcu_free_old_probes(struct rcu_head *head)
82{
83 call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
84}
85
86static __init int release_early_probes(void)
87{
88 struct rcu_head *tmp;
89
90 ok_to_free_tracepoints = true;
91
92 while (early_probes) {
93 tmp = early_probes;
94 early_probes = tmp->next;
95 call_rcu(tmp, rcu_free_old_probes);
96 }
97
98 return 0;
99}
100
101
102postcore_initcall(release_early_probes);
103
104static inline void release_probes(struct tracepoint_func *old)
105{
106 if (old) {
107 struct tp_probes *tp_probes = container_of(old,
108 struct tp_probes, probes[0]);
109
110
111
112
113
114 if (unlikely(!ok_to_free_tracepoints)) {
115 tp_probes->rcu.next = early_probes;
116 early_probes = &tp_probes->rcu;
117 return;
118 }
119
120
121
122
123
124
125
126 call_rcu(&tp_probes->rcu, rcu_free_old_probes);
127 }
128}
129
130static void debug_print_probes(struct tracepoint_func *funcs)
131{
132 int i;
133
134 if (!tracepoint_debug || !funcs)
135 return;
136
137 for (i = 0; funcs[i].func; i++)
138 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
139}
140
141static struct tracepoint_func *
142func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
143 int prio)
144{
145 struct tracepoint_func *old, *new;
146 int nr_probes = 0;
147 int pos = -1;
148
149 if (WARN_ON(!tp_func->func))
150 return ERR_PTR(-EINVAL);
151
152 debug_print_probes(*funcs);
153 old = *funcs;
154 if (old) {
155
156 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
157
158 if (pos < 0 && old[nr_probes].prio < prio)
159 pos = nr_probes;
160 if (old[nr_probes].func == tp_func->func &&
161 old[nr_probes].data == tp_func->data)
162 return ERR_PTR(-EEXIST);
163 }
164 }
165
166 new = allocate_probes(nr_probes + 2);
167 if (new == NULL)
168 return ERR_PTR(-ENOMEM);
169 if (old) {
170 if (pos < 0) {
171 pos = nr_probes;
172 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
173 } else {
174
175 memcpy(new, old, pos * sizeof(struct tracepoint_func));
176
177 memcpy(new + pos + 1, old + pos,
178 (nr_probes - pos) * sizeof(struct tracepoint_func));
179 }
180 } else
181 pos = 0;
182 new[pos] = *tp_func;
183 new[nr_probes + 1].func = NULL;
184 *funcs = new;
185 debug_print_probes(*funcs);
186 return old;
187}
188
189static void *func_remove(struct tracepoint_func **funcs,
190 struct tracepoint_func *tp_func)
191{
192 int nr_probes = 0, nr_del = 0, i;
193 struct tracepoint_func *old, *new;
194
195 old = *funcs;
196
197 if (!old)
198 return ERR_PTR(-ENOENT);
199
200 debug_print_probes(*funcs);
201
202 if (tp_func->func) {
203 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
204 if (old[nr_probes].func == tp_func->func &&
205 old[nr_probes].data == tp_func->data)
206 nr_del++;
207 }
208 }
209
210
211
212
213
214 if (nr_probes - nr_del == 0) {
215
216 *funcs = NULL;
217 debug_print_probes(*funcs);
218 return old;
219 } else {
220 int j = 0;
221
222
223 new = allocate_probes(nr_probes - nr_del + 1);
224 if (new == NULL)
225 return ERR_PTR(-ENOMEM);
226 for (i = 0; old[i].func; i++)
227 if (old[i].func != tp_func->func
228 || old[i].data != tp_func->data)
229 new[j++] = old[i];
230 new[nr_probes - nr_del].func = NULL;
231 *funcs = new;
232 }
233 debug_print_probes(*funcs);
234 return old;
235}
236
237
238
239
240static int tracepoint_add_func(struct tracepoint *tp,
241 struct tracepoint_func *func, int prio)
242{
243 struct tracepoint_func *old, *tp_funcs;
244 int ret;
245
246 if (tp->regfunc && !static_key_enabled(&tp->key)) {
247 ret = tp->regfunc();
248 if (ret < 0)
249 return ret;
250 }
251
252 tp_funcs = rcu_dereference_protected(tp->funcs,
253 lockdep_is_held(&tracepoints_mutex));
254 old = func_add(&tp_funcs, func, prio);
255 if (IS_ERR(old)) {
256 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
257 return PTR_ERR(old);
258 }
259
260
261
262
263
264
265
266 rcu_assign_pointer(tp->funcs, tp_funcs);
267 if (!static_key_enabled(&tp->key))
268 static_key_slow_inc(&tp->key);
269 release_probes(old);
270 return 0;
271}
272
273
274
275
276
277
278
279static int tracepoint_remove_func(struct tracepoint *tp,
280 struct tracepoint_func *func)
281{
282 struct tracepoint_func *old, *tp_funcs;
283
284 tp_funcs = rcu_dereference_protected(tp->funcs,
285 lockdep_is_held(&tracepoints_mutex));
286 old = func_remove(&tp_funcs, func);
287 if (IS_ERR(old)) {
288 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
289 return PTR_ERR(old);
290 }
291
292 if (!tp_funcs) {
293
294 if (tp->unregfunc && static_key_enabled(&tp->key))
295 tp->unregfunc();
296
297 if (static_key_enabled(&tp->key))
298 static_key_slow_dec(&tp->key);
299 }
300 rcu_assign_pointer(tp->funcs, tp_funcs);
301 release_probes(old);
302 return 0;
303}
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
319 void *data, int prio)
320{
321 struct tracepoint_func tp_func;
322 int ret;
323
324 mutex_lock(&tracepoints_mutex);
325 tp_func.func = probe;
326 tp_func.data = data;
327 tp_func.prio = prio;
328 ret = tracepoint_add_func(tp, &tp_func, prio);
329 mutex_unlock(&tracepoints_mutex);
330 return ret;
331}
332EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
333
334
335
336
337
338
339
340
341
342
343
344
345
346int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
347{
348 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
349}
350EXPORT_SYMBOL_GPL(tracepoint_probe_register);
351
352
353
354
355
356
357
358
359
360int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
361{
362 struct tracepoint_func tp_func;
363 int ret;
364
365 mutex_lock(&tracepoints_mutex);
366 tp_func.func = probe;
367 tp_func.data = data;
368 ret = tracepoint_remove_func(tp, &tp_func);
369 mutex_unlock(&tracepoints_mutex);
370 return ret;
371}
372EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
373
374#ifdef CONFIG_MODULES
375bool trace_module_has_bad_taint(struct module *mod)
376{
377 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
378 (1 << TAINT_UNSIGNED_MODULE));
379}
380
381static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
382
383
384
385
386
387
388
389
390
391
392int register_tracepoint_module_notifier(struct notifier_block *nb)
393{
394 struct tp_module *tp_mod;
395 int ret;
396
397 mutex_lock(&tracepoint_module_list_mutex);
398 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
399 if (ret)
400 goto end;
401 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
402 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
403end:
404 mutex_unlock(&tracepoint_module_list_mutex);
405 return ret;
406}
407EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
408
409
410
411
412
413
414
415
416int unregister_tracepoint_module_notifier(struct notifier_block *nb)
417{
418 struct tp_module *tp_mod;
419 int ret;
420
421 mutex_lock(&tracepoint_module_list_mutex);
422 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
423 if (ret)
424 goto end;
425 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
426 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
427end:
428 mutex_unlock(&tracepoint_module_list_mutex);
429 return ret;
430
431}
432EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
433
434
435
436
437
438static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
439 struct tracepoint * const *end)
440{
441 struct tracepoint * const *iter;
442
443 if (!begin)
444 return;
445 for (iter = begin; iter < end; iter++)
446 WARN_ON_ONCE((*iter)->funcs);
447}
448
449static int tracepoint_module_coming(struct module *mod)
450{
451 struct tp_module *tp_mod;
452 int ret = 0;
453
454 if (!mod->num_tracepoints)
455 return 0;
456
457
458
459
460
461
462 if (trace_module_has_bad_taint(mod))
463 return 0;
464 mutex_lock(&tracepoint_module_list_mutex);
465 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
466 if (!tp_mod) {
467 ret = -ENOMEM;
468 goto end;
469 }
470 tp_mod->mod = mod;
471 list_add_tail(&tp_mod->list, &tracepoint_module_list);
472 blocking_notifier_call_chain(&tracepoint_notify_list,
473 MODULE_STATE_COMING, tp_mod);
474end:
475 mutex_unlock(&tracepoint_module_list_mutex);
476 return ret;
477}
478
479static void tracepoint_module_going(struct module *mod)
480{
481 struct tp_module *tp_mod;
482
483 if (!mod->num_tracepoints)
484 return;
485
486 mutex_lock(&tracepoint_module_list_mutex);
487 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
488 if (tp_mod->mod == mod) {
489 blocking_notifier_call_chain(&tracepoint_notify_list,
490 MODULE_STATE_GOING, tp_mod);
491 list_del(&tp_mod->list);
492 kfree(tp_mod);
493
494
495
496
497 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
498 mod->tracepoints_ptrs + mod->num_tracepoints);
499 break;
500 }
501 }
502
503
504
505
506
507
508 mutex_unlock(&tracepoint_module_list_mutex);
509}
510
511static int tracepoint_module_notify(struct notifier_block *self,
512 unsigned long val, void *data)
513{
514 struct module *mod = data;
515 int ret = 0;
516
517 switch (val) {
518 case MODULE_STATE_COMING:
519 ret = tracepoint_module_coming(mod);
520 break;
521 case MODULE_STATE_LIVE:
522 break;
523 case MODULE_STATE_GOING:
524 tracepoint_module_going(mod);
525 break;
526 case MODULE_STATE_UNFORMED:
527 break;
528 }
529 return notifier_from_errno(ret);
530}
531
532static struct notifier_block tracepoint_module_nb = {
533 .notifier_call = tracepoint_module_notify,
534 .priority = 0,
535};
536
537static __init int init_tracepoints(void)
538{
539 int ret;
540
541 ret = register_module_notifier(&tracepoint_module_nb);
542 if (ret)
543 pr_warn("Failed to register tracepoint module enter notifier\n");
544
545 return ret;
546}
547__initcall(init_tracepoints);
548#endif
549
550static void for_each_tracepoint_range(struct tracepoint * const *begin,
551 struct tracepoint * const *end,
552 void (*fct)(struct tracepoint *tp, void *priv),
553 void *priv)
554{
555 struct tracepoint * const *iter;
556
557 if (!begin)
558 return;
559 for (iter = begin; iter < end; iter++)
560 fct(*iter, priv);
561}
562
563
564
565
566
567
568void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
569 void *priv)
570{
571 for_each_tracepoint_range(__start___tracepoints_ptrs,
572 __stop___tracepoints_ptrs, fct, priv);
573}
574EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
575
576#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
577
578
579static int sys_tracepoint_refcount;
580
581int syscall_regfunc(void)
582{
583 struct task_struct *p, *t;
584
585 if (!sys_tracepoint_refcount) {
586 read_lock(&tasklist_lock);
587 for_each_process_thread(p, t) {
588 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
589 }
590 read_unlock(&tasklist_lock);
591 }
592 sys_tracepoint_refcount++;
593
594 return 0;
595}
596
597void syscall_unregfunc(void)
598{
599 struct task_struct *p, *t;
600
601 sys_tracepoint_refcount--;
602 if (!sys_tracepoint_refcount) {
603 read_lock(&tasklist_lock);
604 for_each_process_thread(p, t) {
605 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
606 }
607 read_unlock(&tasklist_lock);
608 }
609}
610#endif
611