197e1c18eSMathieu Desnoyers /* 2de7b2973SMathieu Desnoyers * Copyright (C) 2008-2014 Mathieu Desnoyers 397e1c18eSMathieu Desnoyers * 497e1c18eSMathieu Desnoyers * This program is free software; you can redistribute it and/or modify 597e1c18eSMathieu Desnoyers * it under the terms of the GNU General Public License as published by 697e1c18eSMathieu Desnoyers * the Free Software Foundation; either version 2 of the License, or 797e1c18eSMathieu Desnoyers * (at your option) any later version. 897e1c18eSMathieu Desnoyers * 997e1c18eSMathieu Desnoyers * This program is distributed in the hope that it will be useful, 1097e1c18eSMathieu Desnoyers * but WITHOUT ANY WARRANTY; without even the implied warranty of 1197e1c18eSMathieu Desnoyers * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1297e1c18eSMathieu Desnoyers * GNU General Public License for more details. 1397e1c18eSMathieu Desnoyers * 1497e1c18eSMathieu Desnoyers * You should have received a copy of the GNU General Public License 1597e1c18eSMathieu Desnoyers * along with this program; if not, write to the Free Software 1697e1c18eSMathieu Desnoyers * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1797e1c18eSMathieu Desnoyers */ 1897e1c18eSMathieu Desnoyers #include <linux/module.h> 1997e1c18eSMathieu Desnoyers #include <linux/mutex.h> 2097e1c18eSMathieu Desnoyers #include <linux/types.h> 2197e1c18eSMathieu Desnoyers #include <linux/jhash.h> 2297e1c18eSMathieu Desnoyers #include <linux/list.h> 2397e1c18eSMathieu Desnoyers #include <linux/rcupdate.h> 2497e1c18eSMathieu Desnoyers #include <linux/tracepoint.h> 2597e1c18eSMathieu Desnoyers #include <linux/err.h> 2697e1c18eSMathieu Desnoyers #include <linux/slab.h> 27a871bd33SJason Baron #include <linux/sched.h> 28c5905afbSIngo Molnar #include <linux/static_key.h> 2997e1c18eSMathieu Desnoyers 3065498646SMathieu Desnoyers extern struct tracepoint * const __start___tracepoints_ptrs[]; 3165498646SMathieu Desnoyers extern struct tracepoint * const __stop___tracepoints_ptrs[]; 3297e1c18eSMathieu Desnoyers 3397e1c18eSMathieu Desnoyers /* Set to 1 to enable tracepoint debug output */ 3497e1c18eSMathieu Desnoyers static const int tracepoint_debug; 3597e1c18eSMathieu Desnoyers 36b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES 37de7b2973SMathieu Desnoyers /* 38de7b2973SMathieu Desnoyers * Tracepoint module list mutex protects the local module list. 39de7b2973SMathieu Desnoyers */ 40de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoint_module_list_mutex); 41de7b2973SMathieu Desnoyers 42de7b2973SMathieu Desnoyers /* Local list of struct tp_module */ 43b75ef8b4SMathieu Desnoyers static LIST_HEAD(tracepoint_module_list); 44b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */ 45b75ef8b4SMathieu Desnoyers 4697e1c18eSMathieu Desnoyers /* 47de7b2973SMathieu Desnoyers * tracepoints_mutex protects the builtin and module tracepoints. 48de7b2973SMathieu Desnoyers * tracepoints_mutex nests inside tracepoint_module_list_mutex. 4997e1c18eSMathieu Desnoyers */ 50de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex); 5197e1c18eSMathieu Desnoyers 5297e1c18eSMathieu Desnoyers /* 5397e1c18eSMathieu Desnoyers * Note about RCU : 54fd589a8fSAnand Gadiyar * It is used to delay the free of multiple probes array until a quiescent 5597e1c18eSMathieu Desnoyers * state is reached. 5697e1c18eSMathieu Desnoyers */ 5719dba33cSLai Jiangshan struct tp_probes { 5819dba33cSLai Jiangshan struct rcu_head rcu; 5938516ab5SSteven Rostedt struct tracepoint_func probes[0]; 6019dba33cSLai Jiangshan }; 6197e1c18eSMathieu Desnoyers 6219dba33cSLai Jiangshan static inline void *allocate_probes(int count) 6397e1c18eSMathieu Desnoyers { 6438516ab5SSteven Rostedt struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func) 6519dba33cSLai Jiangshan + sizeof(struct tp_probes), GFP_KERNEL); 6619dba33cSLai Jiangshan return p == NULL ? NULL : p->probes; 6797e1c18eSMathieu Desnoyers } 6897e1c18eSMathieu Desnoyers 6919dba33cSLai Jiangshan static void rcu_free_old_probes(struct rcu_head *head) 7097e1c18eSMathieu Desnoyers { 710dea6d52SMathieu Desnoyers kfree(container_of(head, struct tp_probes, rcu)); 7219dba33cSLai Jiangshan } 7319dba33cSLai Jiangshan 7438516ab5SSteven Rostedt static inline void release_probes(struct tracepoint_func *old) 7519dba33cSLai Jiangshan { 7619dba33cSLai Jiangshan if (old) { 7719dba33cSLai Jiangshan struct tp_probes *tp_probes = container_of(old, 7819dba33cSLai Jiangshan struct tp_probes, probes[0]); 790dea6d52SMathieu Desnoyers call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes); 8019dba33cSLai Jiangshan } 8197e1c18eSMathieu Desnoyers } 8297e1c18eSMathieu Desnoyers 83de7b2973SMathieu Desnoyers static void debug_print_probes(struct tracepoint_func *funcs) 8497e1c18eSMathieu Desnoyers { 8597e1c18eSMathieu Desnoyers int i; 8697e1c18eSMathieu Desnoyers 87de7b2973SMathieu Desnoyers if (!tracepoint_debug || !funcs) 8897e1c18eSMathieu Desnoyers return; 8997e1c18eSMathieu Desnoyers 90de7b2973SMathieu Desnoyers for (i = 0; funcs[i].func; i++) 91de7b2973SMathieu Desnoyers printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func); 9297e1c18eSMathieu Desnoyers } 9397e1c18eSMathieu Desnoyers 94de7b2973SMathieu Desnoyers static struct tracepoint_func *func_add(struct tracepoint_func **funcs, 95de7b2973SMathieu Desnoyers struct tracepoint_func *tp_func) 9697e1c18eSMathieu Desnoyers { 9797e1c18eSMathieu Desnoyers int nr_probes = 0; 9838516ab5SSteven Rostedt struct tracepoint_func *old, *new; 9997e1c18eSMathieu Desnoyers 100de7b2973SMathieu Desnoyers if (WARN_ON(!tp_func->func)) 1014c69e6eaSSahara return ERR_PTR(-EINVAL); 10297e1c18eSMathieu Desnoyers 103de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 104de7b2973SMathieu Desnoyers old = *funcs; 10597e1c18eSMathieu Desnoyers if (old) { 10697e1c18eSMathieu Desnoyers /* (N -> N+1), (N != 0, 1) probes */ 10738516ab5SSteven Rostedt for (nr_probes = 0; old[nr_probes].func; nr_probes++) 108de7b2973SMathieu Desnoyers if (old[nr_probes].func == tp_func->func && 109de7b2973SMathieu Desnoyers old[nr_probes].data == tp_func->data) 11097e1c18eSMathieu Desnoyers return ERR_PTR(-EEXIST); 11197e1c18eSMathieu Desnoyers } 11297e1c18eSMathieu Desnoyers /* + 2 : one for new probe, one for NULL func */ 11319dba33cSLai Jiangshan new = allocate_probes(nr_probes + 2); 11497e1c18eSMathieu Desnoyers if (new == NULL) 11597e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 11697e1c18eSMathieu Desnoyers if (old) 11738516ab5SSteven Rostedt memcpy(new, old, nr_probes * sizeof(struct tracepoint_func)); 118de7b2973SMathieu Desnoyers new[nr_probes] = *tp_func; 11938516ab5SSteven Rostedt new[nr_probes + 1].func = NULL; 120de7b2973SMathieu Desnoyers *funcs = new; 121de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 12297e1c18eSMathieu Desnoyers return old; 12397e1c18eSMathieu Desnoyers } 12497e1c18eSMathieu Desnoyers 125de7b2973SMathieu Desnoyers static void *func_remove(struct tracepoint_func **funcs, 126de7b2973SMathieu Desnoyers struct tracepoint_func *tp_func) 12797e1c18eSMathieu Desnoyers { 12897e1c18eSMathieu Desnoyers int nr_probes = 0, nr_del = 0, i; 12938516ab5SSteven Rostedt struct tracepoint_func *old, *new; 13097e1c18eSMathieu Desnoyers 131de7b2973SMathieu Desnoyers old = *funcs; 13297e1c18eSMathieu Desnoyers 133f66af459SFrederic Weisbecker if (!old) 13419dba33cSLai Jiangshan return ERR_PTR(-ENOENT); 135f66af459SFrederic Weisbecker 136de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 13797e1c18eSMathieu Desnoyers /* (N -> M), (N > 1, M >= 0) probes */ 138de7b2973SMathieu Desnoyers if (tp_func->func) { 13938516ab5SSteven Rostedt for (nr_probes = 0; old[nr_probes].func; nr_probes++) { 140de7b2973SMathieu Desnoyers if (old[nr_probes].func == tp_func->func && 141de7b2973SMathieu Desnoyers old[nr_probes].data == tp_func->data) 14297e1c18eSMathieu Desnoyers nr_del++; 14397e1c18eSMathieu Desnoyers } 1444c69e6eaSSahara } 14597e1c18eSMathieu Desnoyers 1464c69e6eaSSahara /* 1474c69e6eaSSahara * If probe is NULL, then nr_probes = nr_del = 0, and then the 1484c69e6eaSSahara * entire entry will be removed. 1494c69e6eaSSahara */ 15097e1c18eSMathieu Desnoyers if (nr_probes - nr_del == 0) { 15197e1c18eSMathieu Desnoyers /* N -> 0, (N > 1) */ 152de7b2973SMathieu Desnoyers *funcs = NULL; 153de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 15497e1c18eSMathieu Desnoyers return old; 15597e1c18eSMathieu Desnoyers } else { 15697e1c18eSMathieu Desnoyers int j = 0; 15797e1c18eSMathieu Desnoyers /* N -> M, (N > 1, M > 0) */ 15897e1c18eSMathieu Desnoyers /* + 1 for NULL */ 15919dba33cSLai Jiangshan new = allocate_probes(nr_probes - nr_del + 1); 16097e1c18eSMathieu Desnoyers if (new == NULL) 16197e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 16238516ab5SSteven Rostedt for (i = 0; old[i].func; i++) 163de7b2973SMathieu Desnoyers if (old[i].func != tp_func->func 164de7b2973SMathieu Desnoyers || old[i].data != tp_func->data) 16597e1c18eSMathieu Desnoyers new[j++] = old[i]; 16638516ab5SSteven Rostedt new[nr_probes - nr_del].func = NULL; 167de7b2973SMathieu Desnoyers *funcs = new; 16897e1c18eSMathieu Desnoyers } 169de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 17097e1c18eSMathieu Desnoyers return old; 17197e1c18eSMathieu Desnoyers } 17297e1c18eSMathieu Desnoyers 17397e1c18eSMathieu Desnoyers /* 174de7b2973SMathieu Desnoyers * Add the probe function to a tracepoint. 17597e1c18eSMathieu Desnoyers */ 176de7b2973SMathieu Desnoyers static int tracepoint_add_func(struct tracepoint *tp, 177de7b2973SMathieu Desnoyers struct tracepoint_func *func) 17897e1c18eSMathieu Desnoyers { 179de7b2973SMathieu Desnoyers struct tracepoint_func *old, *tp_funcs; 18097e1c18eSMathieu Desnoyers 181de7b2973SMathieu Desnoyers if (tp->regfunc && !static_key_enabled(&tp->key)) 182de7b2973SMathieu Desnoyers tp->regfunc(); 18397e1c18eSMathieu Desnoyers 184de7b2973SMathieu Desnoyers tp_funcs = tp->funcs; 185de7b2973SMathieu Desnoyers old = func_add(&tp_funcs, func); 186de7b2973SMathieu Desnoyers if (IS_ERR(old)) { 187de7b2973SMathieu Desnoyers WARN_ON_ONCE(1); 188de7b2973SMathieu Desnoyers return PTR_ERR(old); 18997e1c18eSMathieu Desnoyers } 190de7b2973SMathieu Desnoyers release_probes(old); 19197419875SJosh Stone 19297e1c18eSMathieu Desnoyers /* 19397e1c18eSMathieu Desnoyers * rcu_assign_pointer has a smp_wmb() which makes sure that the new 19497e1c18eSMathieu Desnoyers * probe callbacks array is consistent before setting a pointer to it. 19597e1c18eSMathieu Desnoyers * This array is referenced by __DO_TRACE from 19697e1c18eSMathieu Desnoyers * include/linux/tracepoints.h. A matching smp_read_barrier_depends() 19797e1c18eSMathieu Desnoyers * is used. 19897e1c18eSMathieu Desnoyers */ 199de7b2973SMathieu Desnoyers rcu_assign_pointer(tp->funcs, tp_funcs); 200de7b2973SMathieu Desnoyers if (!static_key_enabled(&tp->key)) 201de7b2973SMathieu Desnoyers static_key_slow_inc(&tp->key); 202de7b2973SMathieu Desnoyers return 0; 20397e1c18eSMathieu Desnoyers } 20497e1c18eSMathieu Desnoyers 20597e1c18eSMathieu Desnoyers /* 206de7b2973SMathieu Desnoyers * Remove a probe function from a tracepoint. 20797e1c18eSMathieu Desnoyers * Note: only waiting an RCU period after setting elem->call to the empty 20897e1c18eSMathieu Desnoyers * function insures that the original callback is not used anymore. This insured 20997e1c18eSMathieu Desnoyers * by preempt_disable around the call site. 21097e1c18eSMathieu Desnoyers */ 211de7b2973SMathieu Desnoyers static int tracepoint_remove_func(struct tracepoint *tp, 212de7b2973SMathieu Desnoyers struct tracepoint_func *func) 21397e1c18eSMathieu Desnoyers { 214de7b2973SMathieu Desnoyers struct tracepoint_func *old, *tp_funcs; 21597419875SJosh Stone 216de7b2973SMathieu Desnoyers tp_funcs = tp->funcs; 217de7b2973SMathieu Desnoyers old = func_remove(&tp_funcs, func); 218de7b2973SMathieu Desnoyers if (IS_ERR(old)) { 219de7b2973SMathieu Desnoyers WARN_ON_ONCE(1); 220de7b2973SMathieu Desnoyers return PTR_ERR(old); 22197e1c18eSMathieu Desnoyers } 222de7b2973SMathieu Desnoyers release_probes(old); 22397e1c18eSMathieu Desnoyers 224de7b2973SMathieu Desnoyers if (!tp_funcs) { 225de7b2973SMathieu Desnoyers /* Removed last function */ 226de7b2973SMathieu Desnoyers if (tp->unregfunc && static_key_enabled(&tp->key)) 227de7b2973SMathieu Desnoyers tp->unregfunc(); 22897e1c18eSMathieu Desnoyers 229de7b2973SMathieu Desnoyers if (static_key_enabled(&tp->key)) 230de7b2973SMathieu Desnoyers static_key_slow_dec(&tp->key); 23197e1c18eSMathieu Desnoyers } 232de7b2973SMathieu Desnoyers rcu_assign_pointer(tp->funcs, tp_funcs); 233de7b2973SMathieu Desnoyers return 0; 234127cafbbSLai Jiangshan } 235127cafbbSLai Jiangshan 23697e1c18eSMathieu Desnoyers /** 23797e1c18eSMathieu Desnoyers * tracepoint_probe_register - Connect a probe to a tracepoint 238de7b2973SMathieu Desnoyers * @tp: tracepoint 23997e1c18eSMathieu Desnoyers * @probe: probe handler 24097e1c18eSMathieu Desnoyers * 241de7b2973SMathieu Desnoyers * Returns 0 if ok, error value on error. 242de7b2973SMathieu Desnoyers * Note: if @tp is within a module, the caller is responsible for 243de7b2973SMathieu Desnoyers * unregistering the probe before the module is gone. This can be 244de7b2973SMathieu Desnoyers * performed either with a tracepoint module going notifier, or from 245de7b2973SMathieu Desnoyers * within module exit functions. 24697e1c18eSMathieu Desnoyers */ 247de7b2973SMathieu Desnoyers int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data) 24897e1c18eSMathieu Desnoyers { 249de7b2973SMathieu Desnoyers struct tracepoint_func tp_func; 250de7b2973SMathieu Desnoyers int ret; 25197e1c18eSMathieu Desnoyers 25297e1c18eSMathieu Desnoyers mutex_lock(&tracepoints_mutex); 253de7b2973SMathieu Desnoyers tp_func.func = probe; 254de7b2973SMathieu Desnoyers tp_func.data = data; 255de7b2973SMathieu Desnoyers ret = tracepoint_add_func(tp, &tp_func); 25697e1c18eSMathieu Desnoyers mutex_unlock(&tracepoints_mutex); 257b196e2b9SSteven Rostedt return ret; 25897e1c18eSMathieu Desnoyers } 25997e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register); 26097e1c18eSMathieu Desnoyers 26197e1c18eSMathieu Desnoyers /** 26297e1c18eSMathieu Desnoyers * tracepoint_probe_unregister - Disconnect a probe from a tracepoint 263de7b2973SMathieu Desnoyers * @tp: tracepoint 26497e1c18eSMathieu Desnoyers * @probe: probe function pointer 26597e1c18eSMathieu Desnoyers * 266de7b2973SMathieu Desnoyers * Returns 0 if ok, error value on error. 26797e1c18eSMathieu Desnoyers */ 268de7b2973SMathieu Desnoyers int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data) 26997e1c18eSMathieu Desnoyers { 270de7b2973SMathieu Desnoyers struct tracepoint_func tp_func; 271de7b2973SMathieu Desnoyers int ret; 27297e1c18eSMathieu Desnoyers 27397e1c18eSMathieu Desnoyers mutex_lock(&tracepoints_mutex); 274de7b2973SMathieu Desnoyers tp_func.func = probe; 275de7b2973SMathieu Desnoyers tp_func.data = data; 276de7b2973SMathieu Desnoyers ret = tracepoint_remove_func(tp, &tp_func); 27797e1c18eSMathieu Desnoyers mutex_unlock(&tracepoints_mutex); 278de7b2973SMathieu Desnoyers return ret; 27997e1c18eSMathieu Desnoyers } 28097e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); 28197e1c18eSMathieu Desnoyers 282227a8375SIngo Molnar #ifdef CONFIG_MODULES 28345ab2813SSteven Rostedt (Red Hat) bool trace_module_has_bad_taint(struct module *mod) 28445ab2813SSteven Rostedt (Red Hat) { 28545ab2813SSteven Rostedt (Red Hat) return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP)); 28645ab2813SSteven Rostedt (Red Hat) } 28745ab2813SSteven Rostedt (Red Hat) 288de7b2973SMathieu Desnoyers static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list); 289de7b2973SMathieu Desnoyers 290de7b2973SMathieu Desnoyers /** 291de7b2973SMathieu Desnoyers * register_tracepoint_notifier - register tracepoint coming/going notifier 292de7b2973SMathieu Desnoyers * @nb: notifier block 293de7b2973SMathieu Desnoyers * 294de7b2973SMathieu Desnoyers * Notifiers registered with this function are called on module 295de7b2973SMathieu Desnoyers * coming/going with the tracepoint_module_list_mutex held. 296de7b2973SMathieu Desnoyers * The notifier block callback should expect a "struct tp_module" data 297de7b2973SMathieu Desnoyers * pointer. 298de7b2973SMathieu Desnoyers */ 299de7b2973SMathieu Desnoyers int register_tracepoint_module_notifier(struct notifier_block *nb) 300de7b2973SMathieu Desnoyers { 301de7b2973SMathieu Desnoyers struct tp_module *tp_mod; 302de7b2973SMathieu Desnoyers int ret; 303de7b2973SMathieu Desnoyers 304de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 305de7b2973SMathieu Desnoyers ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb); 306de7b2973SMathieu Desnoyers if (ret) 307de7b2973SMathieu Desnoyers goto end; 308de7b2973SMathieu Desnoyers list_for_each_entry(tp_mod, &tracepoint_module_list, list) 309de7b2973SMathieu Desnoyers (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod); 310de7b2973SMathieu Desnoyers end: 311de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 312de7b2973SMathieu Desnoyers return ret; 313de7b2973SMathieu Desnoyers } 314de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier); 315de7b2973SMathieu Desnoyers 316de7b2973SMathieu Desnoyers /** 317de7b2973SMathieu Desnoyers * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier 318de7b2973SMathieu Desnoyers * @nb: notifier block 319de7b2973SMathieu Desnoyers * 320de7b2973SMathieu Desnoyers * The notifier block callback should expect a "struct tp_module" data 321de7b2973SMathieu Desnoyers * pointer. 322de7b2973SMathieu Desnoyers */ 323de7b2973SMathieu Desnoyers int unregister_tracepoint_module_notifier(struct notifier_block *nb) 324de7b2973SMathieu Desnoyers { 325de7b2973SMathieu Desnoyers struct tp_module *tp_mod; 326de7b2973SMathieu Desnoyers int ret; 327de7b2973SMathieu Desnoyers 328de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 329de7b2973SMathieu Desnoyers ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb); 330de7b2973SMathieu Desnoyers if (ret) 331de7b2973SMathieu Desnoyers goto end; 332de7b2973SMathieu Desnoyers list_for_each_entry(tp_mod, &tracepoint_module_list, list) 333de7b2973SMathieu Desnoyers (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod); 334de7b2973SMathieu Desnoyers end: 335de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 336de7b2973SMathieu Desnoyers return ret; 337de7b2973SMathieu Desnoyers 338de7b2973SMathieu Desnoyers } 339de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier); 340de7b2973SMathieu Desnoyers 341de7b2973SMathieu Desnoyers /* 342de7b2973SMathieu Desnoyers * Ensure the tracer unregistered the module's probes before the module 343de7b2973SMathieu Desnoyers * teardown is performed. Prevents leaks of probe and data pointers. 344de7b2973SMathieu Desnoyers */ 345de7b2973SMathieu Desnoyers static void tp_module_going_check_quiescent(struct tracepoint * const *begin, 346de7b2973SMathieu Desnoyers struct tracepoint * const *end) 347de7b2973SMathieu Desnoyers { 348de7b2973SMathieu Desnoyers struct tracepoint * const *iter; 349de7b2973SMathieu Desnoyers 350de7b2973SMathieu Desnoyers if (!begin) 351de7b2973SMathieu Desnoyers return; 352de7b2973SMathieu Desnoyers for (iter = begin; iter < end; iter++) 353de7b2973SMathieu Desnoyers WARN_ON_ONCE((*iter)->funcs); 354de7b2973SMathieu Desnoyers } 355de7b2973SMathieu Desnoyers 356b75ef8b4SMathieu Desnoyers static int tracepoint_module_coming(struct module *mod) 357b75ef8b4SMathieu Desnoyers { 3580dea6d52SMathieu Desnoyers struct tp_module *tp_mod; 359b75ef8b4SMathieu Desnoyers int ret = 0; 360b75ef8b4SMathieu Desnoyers 3617dec935aSSteven Rostedt (Red Hat) if (!mod->num_tracepoints) 3627dec935aSSteven Rostedt (Red Hat) return 0; 3637dec935aSSteven Rostedt (Red Hat) 364b75ef8b4SMathieu Desnoyers /* 365c10076c4SSteven Rostedt * We skip modules that taint the kernel, especially those with different 366c10076c4SSteven Rostedt * module headers (for forced load), to make sure we don't cause a crash. 367c10076c4SSteven Rostedt * Staging and out-of-tree GPL modules are fine. 368b75ef8b4SMathieu Desnoyers */ 36945ab2813SSteven Rostedt (Red Hat) if (trace_module_has_bad_taint(mod)) 370b75ef8b4SMathieu Desnoyers return 0; 371de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 372b75ef8b4SMathieu Desnoyers tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL); 373b75ef8b4SMathieu Desnoyers if (!tp_mod) { 374b75ef8b4SMathieu Desnoyers ret = -ENOMEM; 375b75ef8b4SMathieu Desnoyers goto end; 376b75ef8b4SMathieu Desnoyers } 377*eb7d035cSSteven Rostedt (Red Hat) tp_mod->mod = mod; 3780dea6d52SMathieu Desnoyers list_add_tail(&tp_mod->list, &tracepoint_module_list); 379de7b2973SMathieu Desnoyers blocking_notifier_call_chain(&tracepoint_notify_list, 380de7b2973SMathieu Desnoyers MODULE_STATE_COMING, tp_mod); 381b75ef8b4SMathieu Desnoyers end: 382de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 383b75ef8b4SMathieu Desnoyers return ret; 384b75ef8b4SMathieu Desnoyers } 385b75ef8b4SMathieu Desnoyers 386de7b2973SMathieu Desnoyers static void tracepoint_module_going(struct module *mod) 387b75ef8b4SMathieu Desnoyers { 388de7b2973SMathieu Desnoyers struct tp_module *tp_mod; 389b75ef8b4SMathieu Desnoyers 3907dec935aSSteven Rostedt (Red Hat) if (!mod->num_tracepoints) 391de7b2973SMathieu Desnoyers return; 3927dec935aSSteven Rostedt (Red Hat) 393de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 394de7b2973SMathieu Desnoyers list_for_each_entry(tp_mod, &tracepoint_module_list, list) { 395*eb7d035cSSteven Rostedt (Red Hat) if (tp_mod->mod == mod) { 396de7b2973SMathieu Desnoyers blocking_notifier_call_chain(&tracepoint_notify_list, 397de7b2973SMathieu Desnoyers MODULE_STATE_GOING, tp_mod); 398de7b2973SMathieu Desnoyers list_del(&tp_mod->list); 399de7b2973SMathieu Desnoyers kfree(tp_mod); 400de7b2973SMathieu Desnoyers /* 401de7b2973SMathieu Desnoyers * Called the going notifier before checking for 402de7b2973SMathieu Desnoyers * quiescence. 403de7b2973SMathieu Desnoyers */ 404de7b2973SMathieu Desnoyers tp_module_going_check_quiescent(mod->tracepoints_ptrs, 405b75ef8b4SMathieu Desnoyers mod->tracepoints_ptrs + mod->num_tracepoints); 406b75ef8b4SMathieu Desnoyers break; 407b75ef8b4SMathieu Desnoyers } 408b75ef8b4SMathieu Desnoyers } 409b75ef8b4SMathieu Desnoyers /* 410b75ef8b4SMathieu Desnoyers * In the case of modules that were tainted at "coming", we'll simply 411b75ef8b4SMathieu Desnoyers * walk through the list without finding it. We cannot use the "tainted" 412b75ef8b4SMathieu Desnoyers * flag on "going", in case a module taints the kernel only after being 413b75ef8b4SMathieu Desnoyers * loaded. 414b75ef8b4SMathieu Desnoyers */ 415de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 416b75ef8b4SMathieu Desnoyers } 417227a8375SIngo Molnar 418de7b2973SMathieu Desnoyers static int tracepoint_module_notify(struct notifier_block *self, 41932f85742SMathieu Desnoyers unsigned long val, void *data) 42032f85742SMathieu Desnoyers { 42132f85742SMathieu Desnoyers struct module *mod = data; 422b75ef8b4SMathieu Desnoyers int ret = 0; 42332f85742SMathieu Desnoyers 42432f85742SMathieu Desnoyers switch (val) { 42532f85742SMathieu Desnoyers case MODULE_STATE_COMING: 426b75ef8b4SMathieu Desnoyers ret = tracepoint_module_coming(mod); 427b75ef8b4SMathieu Desnoyers break; 428b75ef8b4SMathieu Desnoyers case MODULE_STATE_LIVE: 429b75ef8b4SMathieu Desnoyers break; 43032f85742SMathieu Desnoyers case MODULE_STATE_GOING: 431de7b2973SMathieu Desnoyers tracepoint_module_going(mod); 432de7b2973SMathieu Desnoyers break; 433de7b2973SMathieu Desnoyers case MODULE_STATE_UNFORMED: 43432f85742SMathieu Desnoyers break; 43532f85742SMathieu Desnoyers } 436b75ef8b4SMathieu Desnoyers return ret; 43732f85742SMathieu Desnoyers } 43832f85742SMathieu Desnoyers 439de7b2973SMathieu Desnoyers static struct notifier_block tracepoint_module_nb = { 44032f85742SMathieu Desnoyers .notifier_call = tracepoint_module_notify, 44132f85742SMathieu Desnoyers .priority = 0, 44232f85742SMathieu Desnoyers }; 44332f85742SMathieu Desnoyers 444de7b2973SMathieu Desnoyers static __init int init_tracepoints(void) 44532f85742SMathieu Desnoyers { 446de7b2973SMathieu Desnoyers int ret; 447de7b2973SMathieu Desnoyers 448de7b2973SMathieu Desnoyers ret = register_module_notifier(&tracepoint_module_nb); 449*eb7d035cSSteven Rostedt (Red Hat) if (ret) 450de7b2973SMathieu Desnoyers pr_warning("Failed to register tracepoint module enter notifier\n"); 451*eb7d035cSSteven Rostedt (Red Hat) 452de7b2973SMathieu Desnoyers return ret; 45332f85742SMathieu Desnoyers } 45432f85742SMathieu Desnoyers __initcall(init_tracepoints); 455227a8375SIngo Molnar #endif /* CONFIG_MODULES */ 456a871bd33SJason Baron 457de7b2973SMathieu Desnoyers static void for_each_tracepoint_range(struct tracepoint * const *begin, 458de7b2973SMathieu Desnoyers struct tracepoint * const *end, 459de7b2973SMathieu Desnoyers void (*fct)(struct tracepoint *tp, void *priv), 460de7b2973SMathieu Desnoyers void *priv) 461de7b2973SMathieu Desnoyers { 462de7b2973SMathieu Desnoyers struct tracepoint * const *iter; 463de7b2973SMathieu Desnoyers 464de7b2973SMathieu Desnoyers if (!begin) 465de7b2973SMathieu Desnoyers return; 466de7b2973SMathieu Desnoyers for (iter = begin; iter < end; iter++) 467de7b2973SMathieu Desnoyers fct(*iter, priv); 468de7b2973SMathieu Desnoyers } 469de7b2973SMathieu Desnoyers 470de7b2973SMathieu Desnoyers /** 471de7b2973SMathieu Desnoyers * for_each_kernel_tracepoint - iteration on all kernel tracepoints 472de7b2973SMathieu Desnoyers * @fct: callback 473de7b2973SMathieu Desnoyers * @priv: private data 474de7b2973SMathieu Desnoyers */ 475de7b2973SMathieu Desnoyers void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), 476de7b2973SMathieu Desnoyers void *priv) 477de7b2973SMathieu Desnoyers { 478de7b2973SMathieu Desnoyers for_each_tracepoint_range(__start___tracepoints_ptrs, 479de7b2973SMathieu Desnoyers __stop___tracepoints_ptrs, fct, priv); 480de7b2973SMathieu Desnoyers } 481de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint); 482de7b2973SMathieu Desnoyers 4833d27d8cbSJosh Stone #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS 48460d970c2SIngo Molnar 48597419875SJosh Stone /* NB: reg/unreg are called while guarded with the tracepoints_mutex */ 486a871bd33SJason Baron static int sys_tracepoint_refcount; 487a871bd33SJason Baron 488a871bd33SJason Baron void syscall_regfunc(void) 489a871bd33SJason Baron { 490a871bd33SJason Baron unsigned long flags; 491a871bd33SJason Baron struct task_struct *g, *t; 492a871bd33SJason Baron 493a871bd33SJason Baron if (!sys_tracepoint_refcount) { 494a871bd33SJason Baron read_lock_irqsave(&tasklist_lock, flags); 495a871bd33SJason Baron do_each_thread(g, t) { 496cc3b13c1SHendrik Brueckner /* Skip kernel threads. */ 497cc3b13c1SHendrik Brueckner if (t->mm) 49866700001SJosh Stone set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); 499a871bd33SJason Baron } while_each_thread(g, t); 500a871bd33SJason Baron read_unlock_irqrestore(&tasklist_lock, flags); 501a871bd33SJason Baron } 502a871bd33SJason Baron sys_tracepoint_refcount++; 503a871bd33SJason Baron } 504a871bd33SJason Baron 505a871bd33SJason Baron void syscall_unregfunc(void) 506a871bd33SJason Baron { 507a871bd33SJason Baron unsigned long flags; 508a871bd33SJason Baron struct task_struct *g, *t; 509a871bd33SJason Baron 510a871bd33SJason Baron sys_tracepoint_refcount--; 511a871bd33SJason Baron if (!sys_tracepoint_refcount) { 512a871bd33SJason Baron read_lock_irqsave(&tasklist_lock, flags); 513a871bd33SJason Baron do_each_thread(g, t) { 51466700001SJosh Stone clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); 515a871bd33SJason Baron } while_each_thread(g, t); 516a871bd33SJason Baron read_unlock_irqrestore(&tasklist_lock, flags); 517a871bd33SJason Baron } 518a871bd33SJason Baron } 51960d970c2SIngo Molnar #endif 520