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