OSDN Git Service

powerpc/fsl: set fsl,i2c-erratum-a004447 flag for P1010 i2c controllers
[android-x86/kernel.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 {
289         struct tracepoint_func *old, *tp_funcs;
290         int ret;
291
292         if (tp->regfunc && !static_key_enabled(&tp->key)) {
293                 ret = tp->regfunc();
294                 if (ret < 0)
295                         return ret;
296         }
297
298         tp_funcs = rcu_dereference_protected(tp->funcs,
299                         lockdep_is_held(&tracepoints_mutex));
300         old = func_add(&tp_funcs, func, prio);
301         if (IS_ERR(old)) {
302                 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
303                 return PTR_ERR(old);
304         }
305
306         /*
307          * rcu_assign_pointer has as smp_store_release() which makes sure
308          * that the new probe callbacks array is consistent before setting
309          * a pointer to it.  This array is referenced by __DO_TRACE from
310          * include/linux/tracepoint.h using rcu_dereference_sched().
311          */
312         rcu_assign_pointer(tp->funcs, tp_funcs);
313         if (!static_key_enabled(&tp->key))
314                 static_key_slow_inc(&tp->key);
315         release_probes(old);
316         return 0;
317 }
318
319 /*
320  * Remove a probe function from a tracepoint.
321  * Note: only waiting an RCU period after setting elem->call to the empty
322  * function insures that the original callback is not used anymore. This insured
323  * by preempt_disable around the call site.
324  */
325 static int tracepoint_remove_func(struct tracepoint *tp,
326                 struct tracepoint_func *func)
327 {
328         struct tracepoint_func *old, *tp_funcs;
329
330         tp_funcs = rcu_dereference_protected(tp->funcs,
331                         lockdep_is_held(&tracepoints_mutex));
332         old = func_remove(&tp_funcs, func);
333         if (WARN_ON_ONCE(IS_ERR(old)))
334                 return PTR_ERR(old);
335
336         if (tp_funcs == old)
337                 /* Failed allocating new tp_funcs, replaced func with stub */
338                 return 0;
339
340         if (!tp_funcs) {
341                 /* Removed last function */
342                 if (tp->unregfunc && static_key_enabled(&tp->key))
343                         tp->unregfunc();
344
345                 if (static_key_enabled(&tp->key))
346                         static_key_slow_dec(&tp->key);
347         }
348         rcu_assign_pointer(tp->funcs, tp_funcs);
349         release_probes(old);
350         return 0;
351 }
352
353 /**
354  * tracepoint_probe_register_prio -  Connect a probe to a tracepoint with priority
355  * @tp: tracepoint
356  * @probe: probe handler
357  * @data: tracepoint data
358  * @prio: priority of this function over other registered functions
359  *
360  * Returns 0 if ok, error value on error.
361  * Note: if @tp is within a module, the caller is responsible for
362  * unregistering the probe before the module is gone. This can be
363  * performed either with a tracepoint module going notifier, or from
364  * within module exit functions.
365  */
366 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
367                                    void *data, int prio)
368 {
369         struct tracepoint_func tp_func;
370         int ret;
371
372         mutex_lock(&tracepoints_mutex);
373         tp_func.func = probe;
374         tp_func.data = data;
375         tp_func.prio = prio;
376         ret = tracepoint_add_func(tp, &tp_func, prio);
377         mutex_unlock(&tracepoints_mutex);
378         return ret;
379 }
380 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
381
382 /**
383  * tracepoint_probe_register -  Connect a probe to a tracepoint
384  * @tp: tracepoint
385  * @probe: probe handler
386  * @data: tracepoint data
387  *
388  * Returns 0 if ok, error value on error.
389  * Note: if @tp is within a module, the caller is responsible for
390  * unregistering the probe before the module is gone. This can be
391  * performed either with a tracepoint module going notifier, or from
392  * within module exit functions.
393  */
394 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
395 {
396         return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
397 }
398 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
399
400 /**
401  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
402  * @tp: tracepoint
403  * @probe: probe function pointer
404  * @data: tracepoint data
405  *
406  * Returns 0 if ok, error value on error.
407  */
408 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
409 {
410         struct tracepoint_func tp_func;
411         int ret;
412
413         mutex_lock(&tracepoints_mutex);
414         tp_func.func = probe;
415         tp_func.data = data;
416         ret = tracepoint_remove_func(tp, &tp_func);
417         mutex_unlock(&tracepoints_mutex);
418         return ret;
419 }
420 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
421
422 static void for_each_tracepoint_range(
423                 tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
424                 void (*fct)(struct tracepoint *tp, void *priv),
425                 void *priv)
426 {
427         tracepoint_ptr_t *iter;
428
429         if (!begin)
430                 return;
431         for (iter = begin; iter < end; iter++)
432                 fct(tracepoint_ptr_deref(iter), priv);
433 }
434
435 #ifdef CONFIG_MODULES
436 bool trace_module_has_bad_taint(struct module *mod)
437 {
438         return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
439                                (1 << TAINT_UNSIGNED_MODULE));
440 }
441
442 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
443
444 /**
445  * register_tracepoint_notifier - register tracepoint coming/going notifier
446  * @nb: notifier block
447  *
448  * Notifiers registered with this function are called on module
449  * coming/going with the tracepoint_module_list_mutex held.
450  * The notifier block callback should expect a "struct tp_module" data
451  * pointer.
452  */
453 int register_tracepoint_module_notifier(struct notifier_block *nb)
454 {
455         struct tp_module *tp_mod;
456         int ret;
457
458         mutex_lock(&tracepoint_module_list_mutex);
459         ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
460         if (ret)
461                 goto end;
462         list_for_each_entry(tp_mod, &tracepoint_module_list, list)
463                 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
464 end:
465         mutex_unlock(&tracepoint_module_list_mutex);
466         return ret;
467 }
468 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
469
470 /**
471  * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
472  * @nb: notifier block
473  *
474  * The notifier block callback should expect a "struct tp_module" data
475  * pointer.
476  */
477 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
478 {
479         struct tp_module *tp_mod;
480         int ret;
481
482         mutex_lock(&tracepoint_module_list_mutex);
483         ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
484         if (ret)
485                 goto end;
486         list_for_each_entry(tp_mod, &tracepoint_module_list, list)
487                 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
488 end:
489         mutex_unlock(&tracepoint_module_list_mutex);
490         return ret;
491
492 }
493 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
494
495 /*
496  * Ensure the tracer unregistered the module's probes before the module
497  * teardown is performed. Prevents leaks of probe and data pointers.
498  */
499 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
500 {
501         WARN_ON_ONCE(tp->funcs);
502 }
503
504 static int tracepoint_module_coming(struct module *mod)
505 {
506         struct tp_module *tp_mod;
507         int ret = 0;
508
509         if (!mod->num_tracepoints)
510                 return 0;
511
512         /*
513          * We skip modules that taint the kernel, especially those with different
514          * module headers (for forced load), to make sure we don't cause a crash.
515          * Staging, out-of-tree, and unsigned GPL modules are fine.
516          */
517         if (trace_module_has_bad_taint(mod))
518                 return 0;
519         mutex_lock(&tracepoint_module_list_mutex);
520         tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
521         if (!tp_mod) {
522                 ret = -ENOMEM;
523                 goto end;
524         }
525         tp_mod->mod = mod;
526         list_add_tail(&tp_mod->list, &tracepoint_module_list);
527         blocking_notifier_call_chain(&tracepoint_notify_list,
528                         MODULE_STATE_COMING, tp_mod);
529 end:
530         mutex_unlock(&tracepoint_module_list_mutex);
531         return ret;
532 }
533
534 static void tracepoint_module_going(struct module *mod)
535 {
536         struct tp_module *tp_mod;
537
538         if (!mod->num_tracepoints)
539                 return;
540
541         mutex_lock(&tracepoint_module_list_mutex);
542         list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
543                 if (tp_mod->mod == mod) {
544                         blocking_notifier_call_chain(&tracepoint_notify_list,
545                                         MODULE_STATE_GOING, tp_mod);
546                         list_del(&tp_mod->list);
547                         kfree(tp_mod);
548                         /*
549                          * Called the going notifier before checking for
550                          * quiescence.
551                          */
552                         for_each_tracepoint_range(mod->tracepoints_ptrs,
553                                 mod->tracepoints_ptrs + mod->num_tracepoints,
554                                 tp_module_going_check_quiescent, NULL);
555                         break;
556                 }
557         }
558         /*
559          * In the case of modules that were tainted at "coming", we'll simply
560          * walk through the list without finding it. We cannot use the "tainted"
561          * flag on "going", in case a module taints the kernel only after being
562          * loaded.
563          */
564         mutex_unlock(&tracepoint_module_list_mutex);
565 }
566
567 static int tracepoint_module_notify(struct notifier_block *self,
568                 unsigned long val, void *data)
569 {
570         struct module *mod = data;
571         int ret = 0;
572
573         switch (val) {
574         case MODULE_STATE_COMING:
575                 ret = tracepoint_module_coming(mod);
576                 break;
577         case MODULE_STATE_LIVE:
578                 break;
579         case MODULE_STATE_GOING:
580                 tracepoint_module_going(mod);
581                 break;
582         case MODULE_STATE_UNFORMED:
583                 break;
584         }
585         return ret;
586 }
587
588 static struct notifier_block tracepoint_module_nb = {
589         .notifier_call = tracepoint_module_notify,
590         .priority = 0,
591 };
592
593 static __init int init_tracepoints(void)
594 {
595         int ret;
596
597         ret = register_module_notifier(&tracepoint_module_nb);
598         if (ret)
599                 pr_warn("Failed to register tracepoint module enter notifier\n");
600
601         return ret;
602 }
603 __initcall(init_tracepoints);
604 #endif /* CONFIG_MODULES */
605
606 /**
607  * for_each_kernel_tracepoint - iteration on all kernel tracepoints
608  * @fct: callback
609  * @priv: private data
610  */
611 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
612                 void *priv)
613 {
614         for_each_tracepoint_range(__start___tracepoints_ptrs,
615                 __stop___tracepoints_ptrs, fct, priv);
616 }
617 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
618
619 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
620
621 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
622 static int sys_tracepoint_refcount;
623
624 int syscall_regfunc(void)
625 {
626         struct task_struct *p, *t;
627
628         if (!sys_tracepoint_refcount) {
629                 read_lock(&tasklist_lock);
630                 for_each_process_thread(p, t) {
631                         set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
632                 }
633                 read_unlock(&tasklist_lock);
634         }
635         sys_tracepoint_refcount++;
636
637         return 0;
638 }
639
640 void syscall_unregfunc(void)
641 {
642         struct task_struct *p, *t;
643
644         sys_tracepoint_refcount--;
645         if (!sys_tracepoint_refcount) {
646                 read_lock(&tasklist_lock);
647                 for_each_process_thread(p, t) {
648                         clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
649                 }
650                 read_unlock(&tasklist_lock);
651         }
652 }
653 #endif