GNU Linux-libre 4.19.286-gnu1
[releases.git] / kernel / tracepoint.c
1 /*
2  * Copyright (C) 2008-2014 Mathieu Desnoyers
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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
31 extern tracepoint_ptr_t __start___tracepoints_ptrs[];
32 extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
33
34 DEFINE_SRCU(tracepoint_srcu);
35 EXPORT_SYMBOL_GPL(tracepoint_srcu);
36
37 /* Set to 1 to enable tracepoint debug output */
38 static const int tracepoint_debug;
39
40 #ifdef CONFIG_MODULES
41 /*
42  * Tracepoint module list mutex protects the local module list.
43  */
44 static DEFINE_MUTEX(tracepoint_module_list_mutex);
45
46 /* Local list of struct tp_module */
47 static LIST_HEAD(tracepoint_module_list);
48 #endif /* CONFIG_MODULES */
49
50 /*
51  * tracepoints_mutex protects the builtin and module tracepoints.
52  * tracepoints_mutex nests inside tracepoint_module_list_mutex.
53  */
54 static DEFINE_MUTEX(tracepoints_mutex);
55
56 static struct rcu_head *early_probes;
57 static bool ok_to_free_tracepoints;
58
59 /*
60  * Note about RCU :
61  * It is used to delay the free of multiple probes array until a quiescent
62  * state is reached.
63  */
64 struct tp_probes {
65         struct rcu_head rcu;
66         struct tracepoint_func probes[0];
67 };
68
69 /* Called in removal of a func but failed to allocate a new tp_funcs */
70 static void tp_stub_func(void)
71 {
72         return;
73 }
74
75 static inline void *allocate_probes(int count)
76 {
77         struct tp_probes *p  = kmalloc(count * sizeof(struct tracepoint_func)
78                         + sizeof(struct tp_probes), GFP_KERNEL);
79         return p == NULL ? NULL : p->probes;
80 }
81
82 static void srcu_free_old_probes(struct rcu_head *head)
83 {
84         kfree(container_of(head, struct tp_probes, rcu));
85 }
86
87 static void rcu_free_old_probes(struct rcu_head *head)
88 {
89         call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
90 }
91
92 static __init int release_early_probes(void)
93 {
94         struct rcu_head *tmp;
95
96         ok_to_free_tracepoints = true;
97
98         while (early_probes) {
99                 tmp = early_probes;
100                 early_probes = tmp->next;
101                 call_rcu_sched(tmp, rcu_free_old_probes);
102         }
103
104         return 0;
105 }
106
107 /* SRCU is initialized at core_initcall */
108 postcore_initcall(release_early_probes);
109
110 static inline void release_probes(struct tracepoint_func *old)
111 {
112         if (old) {
113                 struct tp_probes *tp_probes = container_of(old,
114                         struct tp_probes, probes[0]);
115
116                 /*
117                  * We can't free probes if SRCU is not initialized yet.
118                  * Postpone the freeing till after SRCU is initialized.
119                  */
120                 if (unlikely(!ok_to_free_tracepoints)) {
121                         tp_probes->rcu.next = early_probes;
122                         early_probes = &tp_probes->rcu;
123                         return;
124                 }
125
126                 /*
127                  * Tracepoint probes are protected by both sched RCU and SRCU,
128                  * by calling the SRCU callback in the sched RCU callback we
129                  * cover both cases. So let us chain the SRCU and sched RCU
130                  * callbacks to wait for both grace periods.
131                  */
132                 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
133         }
134 }
135
136 static void debug_print_probes(struct tracepoint_func *funcs)
137 {
138         int i;
139
140         if (!tracepoint_debug || !funcs)
141                 return;
142
143         for (i = 0; funcs[i].func; i++)
144                 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
145 }
146
147 static struct tracepoint_func *
148 func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
149          int prio)
150 {
151         struct tracepoint_func *old, *new;
152         int nr_probes = 0;
153         int stub_funcs = 0;
154         int pos = -1;
155
156         if (WARN_ON(!tp_func->func))
157                 return ERR_PTR(-EINVAL);
158
159         debug_print_probes(*funcs);
160         old = *funcs;
161         if (old) {
162                 /* (N -> N+1), (N != 0, 1) probes */
163                 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
164                         /* Insert before probes of lower priority */
165                         if (pos < 0 && old[nr_probes].prio < prio)
166                                 pos = nr_probes;
167                         if (old[nr_probes].func == tp_func->func &&
168                             old[nr_probes].data == tp_func->data)
169                                 return ERR_PTR(-EEXIST);
170                         if (old[nr_probes].func == tp_stub_func)
171                                 stub_funcs++;
172                 }
173         }
174         /* + 2 : one for new probe, one for NULL func - stub functions */
175         new = allocate_probes(nr_probes + 2 - stub_funcs);
176         if (new == NULL)
177                 return ERR_PTR(-ENOMEM);
178         if (old) {
179                 if (stub_funcs) {
180                         /* Need to copy one at a time to remove stubs */
181                         int probes = 0;
182
183                         pos = -1;
184                         for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
185                                 if (old[nr_probes].func == tp_stub_func)
186                                         continue;
187                                 if (pos < 0 && old[nr_probes].prio < prio)
188                                         pos = probes++;
189                                 new[probes++] = old[nr_probes];
190                         }
191                         nr_probes = probes;
192                         if (pos < 0)
193                                 pos = probes;
194                         else
195                                 nr_probes--; /* Account for insertion */
196
197                 } else if (pos < 0) {
198                         pos = nr_probes;
199                         memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
200                 } else {
201                         /* Copy higher priority probes ahead of the new probe */
202                         memcpy(new, old, pos * sizeof(struct tracepoint_func));
203                         /* Copy the rest after it. */
204                         memcpy(new + pos + 1, old + pos,
205                                (nr_probes - pos) * sizeof(struct tracepoint_func));
206                 }
207         } else
208                 pos = 0;
209         new[pos] = *tp_func;
210         new[nr_probes + 1].func = NULL;
211         *funcs = new;
212         debug_print_probes(*funcs);
213         return old;
214 }
215
216 static void *func_remove(struct tracepoint_func **funcs,
217                 struct tracepoint_func *tp_func)
218 {
219         int nr_probes = 0, nr_del = 0, i;
220         struct tracepoint_func *old, *new;
221
222         old = *funcs;
223
224         if (!old)
225                 return ERR_PTR(-ENOENT);
226
227         debug_print_probes(*funcs);
228         /* (N -> M), (N > 1, M >= 0) probes */
229         if (tp_func->func) {
230                 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
231                         if ((old[nr_probes].func == tp_func->func &&
232                              old[nr_probes].data == tp_func->data) ||
233                             old[nr_probes].func == tp_stub_func)
234                                 nr_del++;
235                 }
236         }
237
238         /*
239          * If probe is NULL, then nr_probes = nr_del = 0, and then the
240          * entire entry will be removed.
241          */
242         if (nr_probes - nr_del == 0) {
243                 /* N -> 0, (N > 1) */
244                 *funcs = NULL;
245                 debug_print_probes(*funcs);
246                 return old;
247         } else {
248                 int j = 0;
249                 /* N -> M, (N > 1, M > 0) */
250                 /* + 1 for NULL */
251                 new = allocate_probes(nr_probes - nr_del + 1);
252                 if (new) {
253                         for (i = 0; old[i].func; i++)
254                                 if ((old[i].func != tp_func->func
255                                      || old[i].data != tp_func->data)
256                                     && old[i].func != tp_stub_func)
257                                         new[j++] = old[i];
258                         new[nr_probes - nr_del].func = NULL;
259                         *funcs = new;
260                 } else {
261                         /*
262                          * Failed to allocate, replace the old function
263                          * with calls to tp_stub_func.
264                          */
265                         for (i = 0; old[i].func; i++)
266                                 if (old[i].func == tp_func->func &&
267                                     old[i].data == tp_func->data) {
268                                         old[i].func = tp_stub_func;
269                                         /* Set the prio to the next event. */
270                                         if (old[i + 1].func)
271                                                 old[i].prio =
272                                                         old[i + 1].prio;
273                                         else
274                                                 old[i].prio = -1;
275                                 }
276                         *funcs = old;
277                 }
278         }
279         debug_print_probes(*funcs);
280         return old;
281 }
282
283 /*
284  * Add the probe function to a tracepoint.
285  */
286 static int tracepoint_add_func(struct tracepoint *tp,
287                                struct tracepoint_func *func, int prio,
288                                bool warn)
289 {
290         struct tracepoint_func *old, *tp_funcs;
291         int ret;
292
293         if (tp->regfunc && !static_key_enabled(&tp->key)) {
294                 ret = tp->regfunc();
295                 if (ret < 0)
296                         return ret;
297         }
298
299         tp_funcs = rcu_dereference_protected(tp->funcs,
300                         lockdep_is_held(&tracepoints_mutex));
301         old = func_add(&tp_funcs, func, prio);
302         if (IS_ERR(old)) {
303                 WARN_ON_ONCE(warn && PTR_ERR(old) != -ENOMEM);
304                 return PTR_ERR(old);
305         }
306
307         /*
308          * rcu_assign_pointer has as smp_store_release() which makes sure
309          * that the new probe callbacks array is consistent before setting
310          * a pointer to it.  This array is referenced by __DO_TRACE from
311          * include/linux/tracepoint.h using rcu_dereference_sched().
312          */
313         rcu_assign_pointer(tp->funcs, tp_funcs);
314         if (!static_key_enabled(&tp->key))
315                 static_key_slow_inc(&tp->key);
316         release_probes(old);
317         return 0;
318 }
319
320 /*
321  * Remove a probe function from a tracepoint.
322  * Note: only waiting an RCU period after setting elem->call to the empty
323  * function insures that the original callback is not used anymore. This insured
324  * by preempt_disable around the call site.
325  */
326 static int tracepoint_remove_func(struct tracepoint *tp,
327                 struct tracepoint_func *func)
328 {
329         struct tracepoint_func *old, *tp_funcs;
330
331         tp_funcs = rcu_dereference_protected(tp->funcs,
332                         lockdep_is_held(&tracepoints_mutex));
333         old = func_remove(&tp_funcs, func);
334         if (WARN_ON_ONCE(IS_ERR(old)))
335                 return PTR_ERR(old);
336
337         if (tp_funcs == old)
338                 /* Failed allocating new tp_funcs, replaced func with stub */
339                 return 0;
340
341         if (!tp_funcs) {
342                 /* Removed last function */
343                 if (tp->unregfunc && static_key_enabled(&tp->key))
344                         tp->unregfunc();
345
346                 if (static_key_enabled(&tp->key))
347                         static_key_slow_dec(&tp->key);
348         }
349         rcu_assign_pointer(tp->funcs, tp_funcs);
350         release_probes(old);
351         return 0;
352 }
353
354 /**
355  * tracepoint_probe_register_prio_may_exist -  Connect a probe to a tracepoint with priority
356  * @tp: tracepoint
357  * @probe: probe handler
358  * @data: tracepoint data
359  * @prio: priority of this function over other registered functions
360  *
361  * Same as tracepoint_probe_register_prio() except that it will not warn
362  * if the tracepoint is already registered.
363  */
364 int tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe,
365                                              void *data, int prio)
366 {
367         struct tracepoint_func tp_func;
368         int ret;
369
370         mutex_lock(&tracepoints_mutex);
371         tp_func.func = probe;
372         tp_func.data = data;
373         tp_func.prio = prio;
374         ret = tracepoint_add_func(tp, &tp_func, prio, false);
375         mutex_unlock(&tracepoints_mutex);
376         return ret;
377 }
378 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio_may_exist);
379
380 /**
381  * tracepoint_probe_register_prio -  Connect a probe to a tracepoint with priority
382  * @tp: tracepoint
383  * @probe: probe handler
384  * @data: tracepoint data
385  * @prio: priority of this function over other registered functions
386  *
387  * Returns 0 if ok, error value on error.
388  * Note: if @tp is within a module, the caller is responsible for
389  * unregistering the probe before the module is gone. This can be
390  * performed either with a tracepoint module going notifier, or from
391  * within module exit functions.
392  */
393 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
394                                    void *data, int prio)
395 {
396         struct tracepoint_func tp_func;
397         int ret;
398
399         mutex_lock(&tracepoints_mutex);
400         tp_func.func = probe;
401         tp_func.data = data;
402         tp_func.prio = prio;
403         ret = tracepoint_add_func(tp, &tp_func, prio, true);
404         mutex_unlock(&tracepoints_mutex);
405         return ret;
406 }
407 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
408
409 /**
410  * tracepoint_probe_register -  Connect a probe to a tracepoint
411  * @tp: tracepoint
412  * @probe: probe handler
413  * @data: tracepoint data
414  *
415  * Returns 0 if ok, error value on error.
416  * Note: if @tp is within a module, the caller is responsible for
417  * unregistering the probe before the module is gone. This can be
418  * performed either with a tracepoint module going notifier, or from
419  * within module exit functions.
420  */
421 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
422 {
423         return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
424 }
425 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
426
427 /**
428  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
429  * @tp: tracepoint
430  * @probe: probe function pointer
431  * @data: tracepoint data
432  *
433  * Returns 0 if ok, error value on error.
434  */
435 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
436 {
437         struct tracepoint_func tp_func;
438         int ret;
439
440         mutex_lock(&tracepoints_mutex);
441         tp_func.func = probe;
442         tp_func.data = data;
443         ret = tracepoint_remove_func(tp, &tp_func);
444         mutex_unlock(&tracepoints_mutex);
445         return ret;
446 }
447 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
448
449 static void for_each_tracepoint_range(
450                 tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
451                 void (*fct)(struct tracepoint *tp, void *priv),
452                 void *priv)
453 {
454         tracepoint_ptr_t *iter;
455
456         if (!begin)
457                 return;
458         for (iter = begin; iter < end; iter++)
459                 fct(tracepoint_ptr_deref(iter), priv);
460 }
461
462 #ifdef CONFIG_MODULES
463 bool trace_module_has_bad_taint(struct module *mod)
464 {
465         return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
466                                (1 << TAINT_UNSIGNED_MODULE));
467 }
468
469 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
470
471 /**
472  * register_tracepoint_notifier - register tracepoint coming/going notifier
473  * @nb: notifier block
474  *
475  * Notifiers registered with this function are called on module
476  * coming/going with the tracepoint_module_list_mutex held.
477  * The notifier block callback should expect a "struct tp_module" data
478  * pointer.
479  */
480 int register_tracepoint_module_notifier(struct notifier_block *nb)
481 {
482         struct tp_module *tp_mod;
483         int ret;
484
485         mutex_lock(&tracepoint_module_list_mutex);
486         ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
487         if (ret)
488                 goto end;
489         list_for_each_entry(tp_mod, &tracepoint_module_list, list)
490                 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
491 end:
492         mutex_unlock(&tracepoint_module_list_mutex);
493         return ret;
494 }
495 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
496
497 /**
498  * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
499  * @nb: notifier block
500  *
501  * The notifier block callback should expect a "struct tp_module" data
502  * pointer.
503  */
504 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
505 {
506         struct tp_module *tp_mod;
507         int ret;
508
509         mutex_lock(&tracepoint_module_list_mutex);
510         ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
511         if (ret)
512                 goto end;
513         list_for_each_entry(tp_mod, &tracepoint_module_list, list)
514                 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
515 end:
516         mutex_unlock(&tracepoint_module_list_mutex);
517         return ret;
518
519 }
520 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
521
522 /*
523  * Ensure the tracer unregistered the module's probes before the module
524  * teardown is performed. Prevents leaks of probe and data pointers.
525  */
526 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
527 {
528         WARN_ON_ONCE(tp->funcs);
529 }
530
531 static int tracepoint_module_coming(struct module *mod)
532 {
533         struct tp_module *tp_mod;
534         int ret = 0;
535
536         if (!mod->num_tracepoints)
537                 return 0;
538
539         /*
540          * We skip modules that taint the kernel, especially those with different
541          * module headers (for forced load), to make sure we don't cause a crash.
542          * Staging, out-of-tree, and unsigned GPL modules are fine.
543          */
544         if (trace_module_has_bad_taint(mod))
545                 return 0;
546         mutex_lock(&tracepoint_module_list_mutex);
547         tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
548         if (!tp_mod) {
549                 ret = -ENOMEM;
550                 goto end;
551         }
552         tp_mod->mod = mod;
553         list_add_tail(&tp_mod->list, &tracepoint_module_list);
554         blocking_notifier_call_chain(&tracepoint_notify_list,
555                         MODULE_STATE_COMING, tp_mod);
556 end:
557         mutex_unlock(&tracepoint_module_list_mutex);
558         return ret;
559 }
560
561 static void tracepoint_module_going(struct module *mod)
562 {
563         struct tp_module *tp_mod;
564
565         if (!mod->num_tracepoints)
566                 return;
567
568         mutex_lock(&tracepoint_module_list_mutex);
569         list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
570                 if (tp_mod->mod == mod) {
571                         blocking_notifier_call_chain(&tracepoint_notify_list,
572                                         MODULE_STATE_GOING, tp_mod);
573                         list_del(&tp_mod->list);
574                         kfree(tp_mod);
575                         /*
576                          * Called the going notifier before checking for
577                          * quiescence.
578                          */
579                         for_each_tracepoint_range(mod->tracepoints_ptrs,
580                                 mod->tracepoints_ptrs + mod->num_tracepoints,
581                                 tp_module_going_check_quiescent, NULL);
582                         break;
583                 }
584         }
585         /*
586          * In the case of modules that were tainted at "coming", we'll simply
587          * walk through the list without finding it. We cannot use the "tainted"
588          * flag on "going", in case a module taints the kernel only after being
589          * loaded.
590          */
591         mutex_unlock(&tracepoint_module_list_mutex);
592 }
593
594 static int tracepoint_module_notify(struct notifier_block *self,
595                 unsigned long val, void *data)
596 {
597         struct module *mod = data;
598         int ret = 0;
599
600         switch (val) {
601         case MODULE_STATE_COMING:
602                 ret = tracepoint_module_coming(mod);
603                 break;
604         case MODULE_STATE_LIVE:
605                 break;
606         case MODULE_STATE_GOING:
607                 tracepoint_module_going(mod);
608                 break;
609         case MODULE_STATE_UNFORMED:
610                 break;
611         }
612         return ret;
613 }
614
615 static struct notifier_block tracepoint_module_nb = {
616         .notifier_call = tracepoint_module_notify,
617         .priority = 0,
618 };
619
620 static __init int init_tracepoints(void)
621 {
622         int ret;
623
624         ret = register_module_notifier(&tracepoint_module_nb);
625         if (ret)
626                 pr_warn("Failed to register tracepoint module enter notifier\n");
627
628         return ret;
629 }
630 __initcall(init_tracepoints);
631 #endif /* CONFIG_MODULES */
632
633 /**
634  * for_each_kernel_tracepoint - iteration on all kernel tracepoints
635  * @fct: callback
636  * @priv: private data
637  */
638 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
639                 void *priv)
640 {
641         for_each_tracepoint_range(__start___tracepoints_ptrs,
642                 __stop___tracepoints_ptrs, fct, priv);
643 }
644 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
645
646 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
647
648 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
649 static int sys_tracepoint_refcount;
650
651 int syscall_regfunc(void)
652 {
653         struct task_struct *p, *t;
654
655         if (!sys_tracepoint_refcount) {
656                 read_lock(&tasklist_lock);
657                 for_each_process_thread(p, t) {
658                         set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
659                 }
660                 read_unlock(&tasklist_lock);
661         }
662         sys_tracepoint_refcount++;
663
664         return 0;
665 }
666
667 void syscall_unregfunc(void)
668 {
669         struct task_struct *p, *t;
670
671         sys_tracepoint_refcount--;
672         if (!sys_tracepoint_refcount) {
673                 read_lock(&tasklist_lock);
674                 for_each_process_thread(p, t) {
675                         clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
676                 }
677                 read_unlock(&tasklist_lock);
678         }
679 }
680 #endif