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> 273f07c014SIngo Molnar #include <linux/sched/signal.h> 2829930025SIngo Molnar #include <linux/sched/task.h> 29c5905afbSIngo Molnar #include <linux/static_key.h> 3097e1c18eSMathieu Desnoyers 3165498646SMathieu Desnoyers extern struct tracepoint * const __start___tracepoints_ptrs[]; 3265498646SMathieu Desnoyers extern struct tracepoint * const __stop___tracepoints_ptrs[]; 3397e1c18eSMathieu Desnoyers 34e6753f23SJoel Fernandes (Google) DEFINE_SRCU(tracepoint_srcu); 35e6753f23SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(tracepoint_srcu); 36e6753f23SJoel Fernandes (Google) 3797e1c18eSMathieu Desnoyers /* Set to 1 to enable tracepoint debug output */ 3897e1c18eSMathieu Desnoyers static const int tracepoint_debug; 3997e1c18eSMathieu Desnoyers 40b75ef8b4SMathieu Desnoyers #ifdef CONFIG_MODULES 41de7b2973SMathieu Desnoyers /* 42de7b2973SMathieu Desnoyers * Tracepoint module list mutex protects the local module list. 43de7b2973SMathieu Desnoyers */ 44de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoint_module_list_mutex); 45de7b2973SMathieu Desnoyers 46de7b2973SMathieu Desnoyers /* Local list of struct tp_module */ 47b75ef8b4SMathieu Desnoyers static LIST_HEAD(tracepoint_module_list); 48b75ef8b4SMathieu Desnoyers #endif /* CONFIG_MODULES */ 49b75ef8b4SMathieu Desnoyers 5097e1c18eSMathieu Desnoyers /* 51de7b2973SMathieu Desnoyers * tracepoints_mutex protects the builtin and module tracepoints. 52de7b2973SMathieu Desnoyers * tracepoints_mutex nests inside tracepoint_module_list_mutex. 5397e1c18eSMathieu Desnoyers */ 54de7b2973SMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex); 5597e1c18eSMathieu Desnoyers 56*f8a79d5cSSteven Rostedt (VMware) static struct rcu_head *early_probes; 57*f8a79d5cSSteven Rostedt (VMware) static bool ok_to_free_tracepoints; 58*f8a79d5cSSteven Rostedt (VMware) 5997e1c18eSMathieu Desnoyers /* 6097e1c18eSMathieu Desnoyers * Note about RCU : 61fd589a8fSAnand Gadiyar * It is used to delay the free of multiple probes array until a quiescent 6297e1c18eSMathieu Desnoyers * state is reached. 6397e1c18eSMathieu Desnoyers */ 6419dba33cSLai Jiangshan struct tp_probes { 6519dba33cSLai Jiangshan struct rcu_head rcu; 6638516ab5SSteven Rostedt struct tracepoint_func probes[0]; 6719dba33cSLai Jiangshan }; 6897e1c18eSMathieu Desnoyers 6919dba33cSLai Jiangshan static inline void *allocate_probes(int count) 7097e1c18eSMathieu Desnoyers { 7138516ab5SSteven Rostedt struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func) 7219dba33cSLai Jiangshan + sizeof(struct tp_probes), GFP_KERNEL); 7319dba33cSLai Jiangshan return p == NULL ? NULL : p->probes; 7497e1c18eSMathieu Desnoyers } 7597e1c18eSMathieu Desnoyers 76e6753f23SJoel Fernandes (Google) static void srcu_free_old_probes(struct rcu_head *head) 7797e1c18eSMathieu Desnoyers { 780dea6d52SMathieu Desnoyers kfree(container_of(head, struct tp_probes, rcu)); 7919dba33cSLai Jiangshan } 8019dba33cSLai Jiangshan 81e6753f23SJoel Fernandes (Google) static void rcu_free_old_probes(struct rcu_head *head) 82e6753f23SJoel Fernandes (Google) { 83e6753f23SJoel Fernandes (Google) call_srcu(&tracepoint_srcu, head, srcu_free_old_probes); 84e6753f23SJoel Fernandes (Google) } 85e6753f23SJoel Fernandes (Google) 86*f8a79d5cSSteven Rostedt (VMware) static __init int release_early_probes(void) 87*f8a79d5cSSteven Rostedt (VMware) { 88*f8a79d5cSSteven Rostedt (VMware) struct rcu_head *tmp; 89*f8a79d5cSSteven Rostedt (VMware) 90*f8a79d5cSSteven Rostedt (VMware) ok_to_free_tracepoints = true; 91*f8a79d5cSSteven Rostedt (VMware) 92*f8a79d5cSSteven Rostedt (VMware) while (early_probes) { 93*f8a79d5cSSteven Rostedt (VMware) tmp = early_probes; 94*f8a79d5cSSteven Rostedt (VMware) early_probes = tmp->next; 95*f8a79d5cSSteven Rostedt (VMware) call_rcu_sched(tmp, rcu_free_old_probes); 96*f8a79d5cSSteven Rostedt (VMware) } 97*f8a79d5cSSteven Rostedt (VMware) 98*f8a79d5cSSteven Rostedt (VMware) return 0; 99*f8a79d5cSSteven Rostedt (VMware) } 100*f8a79d5cSSteven Rostedt (VMware) 101*f8a79d5cSSteven Rostedt (VMware) /* SRCU is initialized at core_initcall */ 102*f8a79d5cSSteven Rostedt (VMware) postcore_initcall(release_early_probes); 103*f8a79d5cSSteven Rostedt (VMware) 10438516ab5SSteven Rostedt static inline void release_probes(struct tracepoint_func *old) 10519dba33cSLai Jiangshan { 10619dba33cSLai Jiangshan if (old) { 10719dba33cSLai Jiangshan struct tp_probes *tp_probes = container_of(old, 10819dba33cSLai Jiangshan struct tp_probes, probes[0]); 109*f8a79d5cSSteven Rostedt (VMware) 110*f8a79d5cSSteven Rostedt (VMware) /* 111*f8a79d5cSSteven Rostedt (VMware) * We can't free probes if SRCU is not initialized yet. 112*f8a79d5cSSteven Rostedt (VMware) * Postpone the freeing till after SRCU is initialized. 113*f8a79d5cSSteven Rostedt (VMware) */ 114*f8a79d5cSSteven Rostedt (VMware) if (unlikely(!ok_to_free_tracepoints)) { 115*f8a79d5cSSteven Rostedt (VMware) tp_probes->rcu.next = early_probes; 116*f8a79d5cSSteven Rostedt (VMware) early_probes = &tp_probes->rcu; 117*f8a79d5cSSteven Rostedt (VMware) return; 118*f8a79d5cSSteven Rostedt (VMware) } 119*f8a79d5cSSteven Rostedt (VMware) 120e6753f23SJoel Fernandes (Google) /* 121e6753f23SJoel Fernandes (Google) * Tracepoint probes are protected by both sched RCU and SRCU, 122e6753f23SJoel Fernandes (Google) * by calling the SRCU callback in the sched RCU callback we 123e6753f23SJoel Fernandes (Google) * cover both cases. So let us chain the SRCU and sched RCU 124e6753f23SJoel Fernandes (Google) * callbacks to wait for both grace periods. 125e6753f23SJoel Fernandes (Google) */ 1260dea6d52SMathieu Desnoyers call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes); 12719dba33cSLai Jiangshan } 12897e1c18eSMathieu Desnoyers } 12997e1c18eSMathieu Desnoyers 130de7b2973SMathieu Desnoyers static void debug_print_probes(struct tracepoint_func *funcs) 13197e1c18eSMathieu Desnoyers { 13297e1c18eSMathieu Desnoyers int i; 13397e1c18eSMathieu Desnoyers 134de7b2973SMathieu Desnoyers if (!tracepoint_debug || !funcs) 13597e1c18eSMathieu Desnoyers return; 13697e1c18eSMathieu Desnoyers 137de7b2973SMathieu Desnoyers for (i = 0; funcs[i].func; i++) 138de7b2973SMathieu Desnoyers printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func); 13997e1c18eSMathieu Desnoyers } 14097e1c18eSMathieu Desnoyers 1417904b5c4SSteven Rostedt (Red Hat) static struct tracepoint_func * 1427904b5c4SSteven Rostedt (Red Hat) func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func, 1437904b5c4SSteven Rostedt (Red Hat) int prio) 14497e1c18eSMathieu Desnoyers { 14538516ab5SSteven Rostedt struct tracepoint_func *old, *new; 1467904b5c4SSteven Rostedt (Red Hat) int nr_probes = 0; 1477904b5c4SSteven Rostedt (Red Hat) int pos = -1; 14897e1c18eSMathieu Desnoyers 149de7b2973SMathieu Desnoyers if (WARN_ON(!tp_func->func)) 1504c69e6eaSSahara return ERR_PTR(-EINVAL); 15197e1c18eSMathieu Desnoyers 152de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 153de7b2973SMathieu Desnoyers old = *funcs; 15497e1c18eSMathieu Desnoyers if (old) { 15597e1c18eSMathieu Desnoyers /* (N -> N+1), (N != 0, 1) probes */ 1567904b5c4SSteven Rostedt (Red Hat) for (nr_probes = 0; old[nr_probes].func; nr_probes++) { 1577904b5c4SSteven Rostedt (Red Hat) /* Insert before probes of lower priority */ 1587904b5c4SSteven Rostedt (Red Hat) if (pos < 0 && old[nr_probes].prio < prio) 1597904b5c4SSteven Rostedt (Red Hat) pos = nr_probes; 160de7b2973SMathieu Desnoyers if (old[nr_probes].func == tp_func->func && 161de7b2973SMathieu Desnoyers old[nr_probes].data == tp_func->data) 16297e1c18eSMathieu Desnoyers return ERR_PTR(-EEXIST); 16397e1c18eSMathieu Desnoyers } 1647904b5c4SSteven Rostedt (Red Hat) } 16597e1c18eSMathieu Desnoyers /* + 2 : one for new probe, one for NULL func */ 16619dba33cSLai Jiangshan new = allocate_probes(nr_probes + 2); 16797e1c18eSMathieu Desnoyers if (new == NULL) 16897e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 1697904b5c4SSteven Rostedt (Red Hat) if (old) { 1707904b5c4SSteven Rostedt (Red Hat) if (pos < 0) { 1717904b5c4SSteven Rostedt (Red Hat) pos = nr_probes; 17238516ab5SSteven Rostedt memcpy(new, old, nr_probes * sizeof(struct tracepoint_func)); 1737904b5c4SSteven Rostedt (Red Hat) } else { 1747904b5c4SSteven Rostedt (Red Hat) /* Copy higher priority probes ahead of the new probe */ 1757904b5c4SSteven Rostedt (Red Hat) memcpy(new, old, pos * sizeof(struct tracepoint_func)); 1767904b5c4SSteven Rostedt (Red Hat) /* Copy the rest after it. */ 1777904b5c4SSteven Rostedt (Red Hat) memcpy(new + pos + 1, old + pos, 1787904b5c4SSteven Rostedt (Red Hat) (nr_probes - pos) * sizeof(struct tracepoint_func)); 1797904b5c4SSteven Rostedt (Red Hat) } 1807904b5c4SSteven Rostedt (Red Hat) } else 1817904b5c4SSteven Rostedt (Red Hat) pos = 0; 1827904b5c4SSteven Rostedt (Red Hat) new[pos] = *tp_func; 18338516ab5SSteven Rostedt new[nr_probes + 1].func = NULL; 184de7b2973SMathieu Desnoyers *funcs = new; 185de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 18697e1c18eSMathieu Desnoyers return old; 18797e1c18eSMathieu Desnoyers } 18897e1c18eSMathieu Desnoyers 189de7b2973SMathieu Desnoyers static void *func_remove(struct tracepoint_func **funcs, 190de7b2973SMathieu Desnoyers struct tracepoint_func *tp_func) 19197e1c18eSMathieu Desnoyers { 19297e1c18eSMathieu Desnoyers int nr_probes = 0, nr_del = 0, i; 19338516ab5SSteven Rostedt struct tracepoint_func *old, *new; 19497e1c18eSMathieu Desnoyers 195de7b2973SMathieu Desnoyers old = *funcs; 19697e1c18eSMathieu Desnoyers 197f66af459SFrederic Weisbecker if (!old) 19819dba33cSLai Jiangshan return ERR_PTR(-ENOENT); 199f66af459SFrederic Weisbecker 200de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 20197e1c18eSMathieu Desnoyers /* (N -> M), (N > 1, M >= 0) probes */ 202de7b2973SMathieu Desnoyers if (tp_func->func) { 20338516ab5SSteven Rostedt for (nr_probes = 0; old[nr_probes].func; nr_probes++) { 204de7b2973SMathieu Desnoyers if (old[nr_probes].func == tp_func->func && 205de7b2973SMathieu Desnoyers old[nr_probes].data == tp_func->data) 20697e1c18eSMathieu Desnoyers nr_del++; 20797e1c18eSMathieu Desnoyers } 2084c69e6eaSSahara } 20997e1c18eSMathieu Desnoyers 2104c69e6eaSSahara /* 2114c69e6eaSSahara * If probe is NULL, then nr_probes = nr_del = 0, and then the 2124c69e6eaSSahara * entire entry will be removed. 2134c69e6eaSSahara */ 21497e1c18eSMathieu Desnoyers if (nr_probes - nr_del == 0) { 21597e1c18eSMathieu Desnoyers /* N -> 0, (N > 1) */ 216de7b2973SMathieu Desnoyers *funcs = NULL; 217de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 21897e1c18eSMathieu Desnoyers return old; 21997e1c18eSMathieu Desnoyers } else { 22097e1c18eSMathieu Desnoyers int j = 0; 22197e1c18eSMathieu Desnoyers /* N -> M, (N > 1, M > 0) */ 22297e1c18eSMathieu Desnoyers /* + 1 for NULL */ 22319dba33cSLai Jiangshan new = allocate_probes(nr_probes - nr_del + 1); 22497e1c18eSMathieu Desnoyers if (new == NULL) 22597e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 22638516ab5SSteven Rostedt for (i = 0; old[i].func; i++) 227de7b2973SMathieu Desnoyers if (old[i].func != tp_func->func 228de7b2973SMathieu Desnoyers || old[i].data != tp_func->data) 22997e1c18eSMathieu Desnoyers new[j++] = old[i]; 23038516ab5SSteven Rostedt new[nr_probes - nr_del].func = NULL; 231de7b2973SMathieu Desnoyers *funcs = new; 23297e1c18eSMathieu Desnoyers } 233de7b2973SMathieu Desnoyers debug_print_probes(*funcs); 23497e1c18eSMathieu Desnoyers return old; 23597e1c18eSMathieu Desnoyers } 23697e1c18eSMathieu Desnoyers 23797e1c18eSMathieu Desnoyers /* 238de7b2973SMathieu Desnoyers * Add the probe function to a tracepoint. 23997e1c18eSMathieu Desnoyers */ 240de7b2973SMathieu Desnoyers static int tracepoint_add_func(struct tracepoint *tp, 2417904b5c4SSteven Rostedt (Red Hat) struct tracepoint_func *func, int prio) 24297e1c18eSMathieu Desnoyers { 243de7b2973SMathieu Desnoyers struct tracepoint_func *old, *tp_funcs; 2448cf868afSSteven Rostedt (Red Hat) int ret; 24597e1c18eSMathieu Desnoyers 2468cf868afSSteven Rostedt (Red Hat) if (tp->regfunc && !static_key_enabled(&tp->key)) { 2478cf868afSSteven Rostedt (Red Hat) ret = tp->regfunc(); 2488cf868afSSteven Rostedt (Red Hat) if (ret < 0) 2498cf868afSSteven Rostedt (Red Hat) return ret; 2508cf868afSSteven Rostedt (Red Hat) } 25197e1c18eSMathieu Desnoyers 252b725dfeaSMathieu Desnoyers tp_funcs = rcu_dereference_protected(tp->funcs, 253b725dfeaSMathieu Desnoyers lockdep_is_held(&tracepoints_mutex)); 2547904b5c4SSteven Rostedt (Red Hat) old = func_add(&tp_funcs, func, prio); 255de7b2973SMathieu Desnoyers if (IS_ERR(old)) { 256d66a270bSMathieu Desnoyers WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); 257de7b2973SMathieu Desnoyers return PTR_ERR(old); 25897e1c18eSMathieu Desnoyers } 25997419875SJosh Stone 26097e1c18eSMathieu Desnoyers /* 261243d1a79SPaul E. McKenney * rcu_assign_pointer has as smp_store_release() which makes sure 262243d1a79SPaul E. McKenney * that the new probe callbacks array is consistent before setting 263243d1a79SPaul E. McKenney * a pointer to it. This array is referenced by __DO_TRACE from 264243d1a79SPaul E. McKenney * include/linux/tracepoint.h using rcu_dereference_sched(). 26597e1c18eSMathieu Desnoyers */ 266de7b2973SMathieu Desnoyers rcu_assign_pointer(tp->funcs, tp_funcs); 267de7b2973SMathieu Desnoyers if (!static_key_enabled(&tp->key)) 268de7b2973SMathieu Desnoyers static_key_slow_inc(&tp->key); 2698058bd0fSMathieu Desnoyers release_probes(old); 270de7b2973SMathieu Desnoyers return 0; 27197e1c18eSMathieu Desnoyers } 27297e1c18eSMathieu Desnoyers 27397e1c18eSMathieu Desnoyers /* 274de7b2973SMathieu Desnoyers * Remove a probe function from a tracepoint. 27597e1c18eSMathieu Desnoyers * Note: only waiting an RCU period after setting elem->call to the empty 27697e1c18eSMathieu Desnoyers * function insures that the original callback is not used anymore. This insured 27797e1c18eSMathieu Desnoyers * by preempt_disable around the call site. 27897e1c18eSMathieu Desnoyers */ 279de7b2973SMathieu Desnoyers static int tracepoint_remove_func(struct tracepoint *tp, 280de7b2973SMathieu Desnoyers struct tracepoint_func *func) 28197e1c18eSMathieu Desnoyers { 282de7b2973SMathieu Desnoyers struct tracepoint_func *old, *tp_funcs; 28397419875SJosh Stone 284b725dfeaSMathieu Desnoyers tp_funcs = rcu_dereference_protected(tp->funcs, 285b725dfeaSMathieu Desnoyers lockdep_is_held(&tracepoints_mutex)); 286de7b2973SMathieu Desnoyers old = func_remove(&tp_funcs, func); 287de7b2973SMathieu Desnoyers if (IS_ERR(old)) { 288d66a270bSMathieu Desnoyers WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM); 289de7b2973SMathieu Desnoyers return PTR_ERR(old); 29097e1c18eSMathieu Desnoyers } 29197e1c18eSMathieu Desnoyers 292de7b2973SMathieu Desnoyers if (!tp_funcs) { 293de7b2973SMathieu Desnoyers /* Removed last function */ 294de7b2973SMathieu Desnoyers if (tp->unregfunc && static_key_enabled(&tp->key)) 295de7b2973SMathieu Desnoyers tp->unregfunc(); 29697e1c18eSMathieu Desnoyers 297de7b2973SMathieu Desnoyers if (static_key_enabled(&tp->key)) 298de7b2973SMathieu Desnoyers static_key_slow_dec(&tp->key); 29997e1c18eSMathieu Desnoyers } 300de7b2973SMathieu Desnoyers rcu_assign_pointer(tp->funcs, tp_funcs); 3018058bd0fSMathieu Desnoyers release_probes(old); 302de7b2973SMathieu Desnoyers return 0; 303127cafbbSLai Jiangshan } 304127cafbbSLai Jiangshan 30597e1c18eSMathieu Desnoyers /** 306f39e2391SLee, Chun-Yi * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority 307de7b2973SMathieu Desnoyers * @tp: tracepoint 30897e1c18eSMathieu Desnoyers * @probe: probe handler 309cac92ba7SFabian Frederick * @data: tracepoint data 3107904b5c4SSteven Rostedt (Red Hat) * @prio: priority of this function over other registered functions 3117904b5c4SSteven Rostedt (Red Hat) * 3127904b5c4SSteven Rostedt (Red Hat) * Returns 0 if ok, error value on error. 3137904b5c4SSteven Rostedt (Red Hat) * Note: if @tp is within a module, the caller is responsible for 3147904b5c4SSteven Rostedt (Red Hat) * unregistering the probe before the module is gone. This can be 3157904b5c4SSteven Rostedt (Red Hat) * performed either with a tracepoint module going notifier, or from 3167904b5c4SSteven Rostedt (Red Hat) * within module exit functions. 3177904b5c4SSteven Rostedt (Red Hat) */ 3187904b5c4SSteven Rostedt (Red Hat) int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, 3197904b5c4SSteven Rostedt (Red Hat) void *data, int prio) 3207904b5c4SSteven Rostedt (Red Hat) { 3217904b5c4SSteven Rostedt (Red Hat) struct tracepoint_func tp_func; 3227904b5c4SSteven Rostedt (Red Hat) int ret; 3237904b5c4SSteven Rostedt (Red Hat) 3247904b5c4SSteven Rostedt (Red Hat) mutex_lock(&tracepoints_mutex); 3257904b5c4SSteven Rostedt (Red Hat) tp_func.func = probe; 3267904b5c4SSteven Rostedt (Red Hat) tp_func.data = data; 3277904b5c4SSteven Rostedt (Red Hat) tp_func.prio = prio; 3287904b5c4SSteven Rostedt (Red Hat) ret = tracepoint_add_func(tp, &tp_func, prio); 3297904b5c4SSteven Rostedt (Red Hat) mutex_unlock(&tracepoints_mutex); 3307904b5c4SSteven Rostedt (Red Hat) return ret; 3317904b5c4SSteven Rostedt (Red Hat) } 3327904b5c4SSteven Rostedt (Red Hat) EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio); 3337904b5c4SSteven Rostedt (Red Hat) 3347904b5c4SSteven Rostedt (Red Hat) /** 3357904b5c4SSteven Rostedt (Red Hat) * tracepoint_probe_register - Connect a probe to a tracepoint 3367904b5c4SSteven Rostedt (Red Hat) * @tp: tracepoint 3377904b5c4SSteven Rostedt (Red Hat) * @probe: probe handler 3387904b5c4SSteven Rostedt (Red Hat) * @data: tracepoint data 33997e1c18eSMathieu Desnoyers * 340de7b2973SMathieu Desnoyers * Returns 0 if ok, error value on error. 341de7b2973SMathieu Desnoyers * Note: if @tp is within a module, the caller is responsible for 342de7b2973SMathieu Desnoyers * unregistering the probe before the module is gone. This can be 343de7b2973SMathieu Desnoyers * performed either with a tracepoint module going notifier, or from 344de7b2973SMathieu Desnoyers * within module exit functions. 34597e1c18eSMathieu Desnoyers */ 346de7b2973SMathieu Desnoyers int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data) 34797e1c18eSMathieu Desnoyers { 3487904b5c4SSteven Rostedt (Red Hat) return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO); 34997e1c18eSMathieu Desnoyers } 35097e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register); 35197e1c18eSMathieu Desnoyers 35297e1c18eSMathieu Desnoyers /** 35397e1c18eSMathieu Desnoyers * tracepoint_probe_unregister - Disconnect a probe from a tracepoint 354de7b2973SMathieu Desnoyers * @tp: tracepoint 35597e1c18eSMathieu Desnoyers * @probe: probe function pointer 356cac92ba7SFabian Frederick * @data: tracepoint data 35797e1c18eSMathieu Desnoyers * 358de7b2973SMathieu Desnoyers * Returns 0 if ok, error value on error. 35997e1c18eSMathieu Desnoyers */ 360de7b2973SMathieu Desnoyers int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data) 36197e1c18eSMathieu Desnoyers { 362de7b2973SMathieu Desnoyers struct tracepoint_func tp_func; 363de7b2973SMathieu Desnoyers int ret; 36497e1c18eSMathieu Desnoyers 36597e1c18eSMathieu Desnoyers mutex_lock(&tracepoints_mutex); 366de7b2973SMathieu Desnoyers tp_func.func = probe; 367de7b2973SMathieu Desnoyers tp_func.data = data; 368de7b2973SMathieu Desnoyers ret = tracepoint_remove_func(tp, &tp_func); 36997e1c18eSMathieu Desnoyers mutex_unlock(&tracepoints_mutex); 370de7b2973SMathieu Desnoyers return ret; 37197e1c18eSMathieu Desnoyers } 37297e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); 37397e1c18eSMathieu Desnoyers 374227a8375SIngo Molnar #ifdef CONFIG_MODULES 37545ab2813SSteven Rostedt (Red Hat) bool trace_module_has_bad_taint(struct module *mod) 37645ab2813SSteven Rostedt (Red Hat) { 37766cc69e3SMathieu Desnoyers return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) | 37866cc69e3SMathieu Desnoyers (1 << TAINT_UNSIGNED_MODULE)); 37945ab2813SSteven Rostedt (Red Hat) } 38045ab2813SSteven Rostedt (Red Hat) 381de7b2973SMathieu Desnoyers static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list); 382de7b2973SMathieu Desnoyers 383de7b2973SMathieu Desnoyers /** 384de7b2973SMathieu Desnoyers * register_tracepoint_notifier - register tracepoint coming/going notifier 385de7b2973SMathieu Desnoyers * @nb: notifier block 386de7b2973SMathieu Desnoyers * 387de7b2973SMathieu Desnoyers * Notifiers registered with this function are called on module 388de7b2973SMathieu Desnoyers * coming/going with the tracepoint_module_list_mutex held. 389de7b2973SMathieu Desnoyers * The notifier block callback should expect a "struct tp_module" data 390de7b2973SMathieu Desnoyers * pointer. 391de7b2973SMathieu Desnoyers */ 392de7b2973SMathieu Desnoyers int register_tracepoint_module_notifier(struct notifier_block *nb) 393de7b2973SMathieu Desnoyers { 394de7b2973SMathieu Desnoyers struct tp_module *tp_mod; 395de7b2973SMathieu Desnoyers int ret; 396de7b2973SMathieu Desnoyers 397de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 398de7b2973SMathieu Desnoyers ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb); 399de7b2973SMathieu Desnoyers if (ret) 400de7b2973SMathieu Desnoyers goto end; 401de7b2973SMathieu Desnoyers list_for_each_entry(tp_mod, &tracepoint_module_list, list) 402de7b2973SMathieu Desnoyers (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod); 403de7b2973SMathieu Desnoyers end: 404de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 405de7b2973SMathieu Desnoyers return ret; 406de7b2973SMathieu Desnoyers } 407de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier); 408de7b2973SMathieu Desnoyers 409de7b2973SMathieu Desnoyers /** 410de7b2973SMathieu Desnoyers * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier 411de7b2973SMathieu Desnoyers * @nb: notifier block 412de7b2973SMathieu Desnoyers * 413de7b2973SMathieu Desnoyers * The notifier block callback should expect a "struct tp_module" data 414de7b2973SMathieu Desnoyers * pointer. 415de7b2973SMathieu Desnoyers */ 416de7b2973SMathieu Desnoyers int unregister_tracepoint_module_notifier(struct notifier_block *nb) 417de7b2973SMathieu Desnoyers { 418de7b2973SMathieu Desnoyers struct tp_module *tp_mod; 419de7b2973SMathieu Desnoyers int ret; 420de7b2973SMathieu Desnoyers 421de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 422de7b2973SMathieu Desnoyers ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb); 423de7b2973SMathieu Desnoyers if (ret) 424de7b2973SMathieu Desnoyers goto end; 425de7b2973SMathieu Desnoyers list_for_each_entry(tp_mod, &tracepoint_module_list, list) 426de7b2973SMathieu Desnoyers (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod); 427de7b2973SMathieu Desnoyers end: 428de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 429de7b2973SMathieu Desnoyers return ret; 430de7b2973SMathieu Desnoyers 431de7b2973SMathieu Desnoyers } 432de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier); 433de7b2973SMathieu Desnoyers 434de7b2973SMathieu Desnoyers /* 435de7b2973SMathieu Desnoyers * Ensure the tracer unregistered the module's probes before the module 436de7b2973SMathieu Desnoyers * teardown is performed. Prevents leaks of probe and data pointers. 437de7b2973SMathieu Desnoyers */ 438de7b2973SMathieu Desnoyers static void tp_module_going_check_quiescent(struct tracepoint * const *begin, 439de7b2973SMathieu Desnoyers struct tracepoint * const *end) 440de7b2973SMathieu Desnoyers { 441de7b2973SMathieu Desnoyers struct tracepoint * const *iter; 442de7b2973SMathieu Desnoyers 443de7b2973SMathieu Desnoyers if (!begin) 444de7b2973SMathieu Desnoyers return; 445de7b2973SMathieu Desnoyers for (iter = begin; iter < end; iter++) 446de7b2973SMathieu Desnoyers WARN_ON_ONCE((*iter)->funcs); 447de7b2973SMathieu Desnoyers } 448de7b2973SMathieu Desnoyers 449b75ef8b4SMathieu Desnoyers static int tracepoint_module_coming(struct module *mod) 450b75ef8b4SMathieu Desnoyers { 4510dea6d52SMathieu Desnoyers struct tp_module *tp_mod; 452b75ef8b4SMathieu Desnoyers int ret = 0; 453b75ef8b4SMathieu Desnoyers 4547dec935aSSteven Rostedt (Red Hat) if (!mod->num_tracepoints) 4557dec935aSSteven Rostedt (Red Hat) return 0; 4567dec935aSSteven Rostedt (Red Hat) 457b75ef8b4SMathieu Desnoyers /* 458c10076c4SSteven Rostedt * We skip modules that taint the kernel, especially those with different 459c10076c4SSteven Rostedt * module headers (for forced load), to make sure we don't cause a crash. 46066cc69e3SMathieu Desnoyers * Staging, out-of-tree, and unsigned GPL modules are fine. 461b75ef8b4SMathieu Desnoyers */ 46245ab2813SSteven Rostedt (Red Hat) if (trace_module_has_bad_taint(mod)) 463b75ef8b4SMathieu Desnoyers return 0; 464de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 465b75ef8b4SMathieu Desnoyers tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL); 466b75ef8b4SMathieu Desnoyers if (!tp_mod) { 467b75ef8b4SMathieu Desnoyers ret = -ENOMEM; 468b75ef8b4SMathieu Desnoyers goto end; 469b75ef8b4SMathieu Desnoyers } 470eb7d035cSSteven Rostedt (Red Hat) tp_mod->mod = mod; 4710dea6d52SMathieu Desnoyers list_add_tail(&tp_mod->list, &tracepoint_module_list); 472de7b2973SMathieu Desnoyers blocking_notifier_call_chain(&tracepoint_notify_list, 473de7b2973SMathieu Desnoyers MODULE_STATE_COMING, tp_mod); 474b75ef8b4SMathieu Desnoyers end: 475de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 476b75ef8b4SMathieu Desnoyers return ret; 477b75ef8b4SMathieu Desnoyers } 478b75ef8b4SMathieu Desnoyers 479de7b2973SMathieu Desnoyers static void tracepoint_module_going(struct module *mod) 480b75ef8b4SMathieu Desnoyers { 481de7b2973SMathieu Desnoyers struct tp_module *tp_mod; 482b75ef8b4SMathieu Desnoyers 4837dec935aSSteven Rostedt (Red Hat) if (!mod->num_tracepoints) 484de7b2973SMathieu Desnoyers return; 4857dec935aSSteven Rostedt (Red Hat) 486de7b2973SMathieu Desnoyers mutex_lock(&tracepoint_module_list_mutex); 487de7b2973SMathieu Desnoyers list_for_each_entry(tp_mod, &tracepoint_module_list, list) { 488eb7d035cSSteven Rostedt (Red Hat) if (tp_mod->mod == mod) { 489de7b2973SMathieu Desnoyers blocking_notifier_call_chain(&tracepoint_notify_list, 490de7b2973SMathieu Desnoyers MODULE_STATE_GOING, tp_mod); 491de7b2973SMathieu Desnoyers list_del(&tp_mod->list); 492de7b2973SMathieu Desnoyers kfree(tp_mod); 493de7b2973SMathieu Desnoyers /* 494de7b2973SMathieu Desnoyers * Called the going notifier before checking for 495de7b2973SMathieu Desnoyers * quiescence. 496de7b2973SMathieu Desnoyers */ 497de7b2973SMathieu Desnoyers tp_module_going_check_quiescent(mod->tracepoints_ptrs, 498b75ef8b4SMathieu Desnoyers mod->tracepoints_ptrs + mod->num_tracepoints); 499b75ef8b4SMathieu Desnoyers break; 500b75ef8b4SMathieu Desnoyers } 501b75ef8b4SMathieu Desnoyers } 502b75ef8b4SMathieu Desnoyers /* 503b75ef8b4SMathieu Desnoyers * In the case of modules that were tainted at "coming", we'll simply 504b75ef8b4SMathieu Desnoyers * walk through the list without finding it. We cannot use the "tainted" 505b75ef8b4SMathieu Desnoyers * flag on "going", in case a module taints the kernel only after being 506b75ef8b4SMathieu Desnoyers * loaded. 507b75ef8b4SMathieu Desnoyers */ 508de7b2973SMathieu Desnoyers mutex_unlock(&tracepoint_module_list_mutex); 509b75ef8b4SMathieu Desnoyers } 510227a8375SIngo Molnar 511de7b2973SMathieu Desnoyers static int tracepoint_module_notify(struct notifier_block *self, 51232f85742SMathieu Desnoyers unsigned long val, void *data) 51332f85742SMathieu Desnoyers { 51432f85742SMathieu Desnoyers struct module *mod = data; 515b75ef8b4SMathieu Desnoyers int ret = 0; 51632f85742SMathieu Desnoyers 51732f85742SMathieu Desnoyers switch (val) { 51832f85742SMathieu Desnoyers case MODULE_STATE_COMING: 519b75ef8b4SMathieu Desnoyers ret = tracepoint_module_coming(mod); 520b75ef8b4SMathieu Desnoyers break; 521b75ef8b4SMathieu Desnoyers case MODULE_STATE_LIVE: 522b75ef8b4SMathieu Desnoyers break; 52332f85742SMathieu Desnoyers case MODULE_STATE_GOING: 524de7b2973SMathieu Desnoyers tracepoint_module_going(mod); 525de7b2973SMathieu Desnoyers break; 526de7b2973SMathieu Desnoyers case MODULE_STATE_UNFORMED: 52732f85742SMathieu Desnoyers break; 52832f85742SMathieu Desnoyers } 529b75ef8b4SMathieu Desnoyers return ret; 53032f85742SMathieu Desnoyers } 53132f85742SMathieu Desnoyers 532de7b2973SMathieu Desnoyers static struct notifier_block tracepoint_module_nb = { 53332f85742SMathieu Desnoyers .notifier_call = tracepoint_module_notify, 53432f85742SMathieu Desnoyers .priority = 0, 53532f85742SMathieu Desnoyers }; 53632f85742SMathieu Desnoyers 537de7b2973SMathieu Desnoyers static __init int init_tracepoints(void) 53832f85742SMathieu Desnoyers { 539de7b2973SMathieu Desnoyers int ret; 540de7b2973SMathieu Desnoyers 541de7b2973SMathieu Desnoyers ret = register_module_notifier(&tracepoint_module_nb); 542eb7d035cSSteven Rostedt (Red Hat) if (ret) 543a395d6a7SJoe Perches pr_warn("Failed to register tracepoint module enter notifier\n"); 544eb7d035cSSteven Rostedt (Red Hat) 545de7b2973SMathieu Desnoyers return ret; 54632f85742SMathieu Desnoyers } 54732f85742SMathieu Desnoyers __initcall(init_tracepoints); 548227a8375SIngo Molnar #endif /* CONFIG_MODULES */ 549a871bd33SJason Baron 550de7b2973SMathieu Desnoyers static void for_each_tracepoint_range(struct tracepoint * const *begin, 551de7b2973SMathieu Desnoyers struct tracepoint * const *end, 552de7b2973SMathieu Desnoyers void (*fct)(struct tracepoint *tp, void *priv), 553de7b2973SMathieu Desnoyers void *priv) 554de7b2973SMathieu Desnoyers { 555de7b2973SMathieu Desnoyers struct tracepoint * const *iter; 556de7b2973SMathieu Desnoyers 557de7b2973SMathieu Desnoyers if (!begin) 558de7b2973SMathieu Desnoyers return; 559de7b2973SMathieu Desnoyers for (iter = begin; iter < end; iter++) 560de7b2973SMathieu Desnoyers fct(*iter, priv); 561de7b2973SMathieu Desnoyers } 562de7b2973SMathieu Desnoyers 563de7b2973SMathieu Desnoyers /** 564de7b2973SMathieu Desnoyers * for_each_kernel_tracepoint - iteration on all kernel tracepoints 565de7b2973SMathieu Desnoyers * @fct: callback 566de7b2973SMathieu Desnoyers * @priv: private data 567de7b2973SMathieu Desnoyers */ 568de7b2973SMathieu Desnoyers void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), 569de7b2973SMathieu Desnoyers void *priv) 570de7b2973SMathieu Desnoyers { 571de7b2973SMathieu Desnoyers for_each_tracepoint_range(__start___tracepoints_ptrs, 572de7b2973SMathieu Desnoyers __stop___tracepoints_ptrs, fct, priv); 573de7b2973SMathieu Desnoyers } 574de7b2973SMathieu Desnoyers EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint); 575de7b2973SMathieu Desnoyers 5763d27d8cbSJosh Stone #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS 57760d970c2SIngo Molnar 57897419875SJosh Stone /* NB: reg/unreg are called while guarded with the tracepoints_mutex */ 579a871bd33SJason Baron static int sys_tracepoint_refcount; 580a871bd33SJason Baron 5818cf868afSSteven Rostedt (Red Hat) int syscall_regfunc(void) 582a871bd33SJason Baron { 5838063e41dSOleg Nesterov struct task_struct *p, *t; 584a871bd33SJason Baron 585a871bd33SJason Baron if (!sys_tracepoint_refcount) { 5868063e41dSOleg Nesterov read_lock(&tasklist_lock); 5878063e41dSOleg Nesterov for_each_process_thread(p, t) { 58866700001SJosh Stone set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); 5898063e41dSOleg Nesterov } 5908063e41dSOleg Nesterov read_unlock(&tasklist_lock); 591a871bd33SJason Baron } 592a871bd33SJason Baron sys_tracepoint_refcount++; 5938cf868afSSteven Rostedt (Red Hat) 5948cf868afSSteven Rostedt (Red Hat) return 0; 595a871bd33SJason Baron } 596a871bd33SJason Baron 597a871bd33SJason Baron void syscall_unregfunc(void) 598a871bd33SJason Baron { 5998063e41dSOleg Nesterov struct task_struct *p, *t; 600a871bd33SJason Baron 601a871bd33SJason Baron sys_tracepoint_refcount--; 602a871bd33SJason Baron if (!sys_tracepoint_refcount) { 6038063e41dSOleg Nesterov read_lock(&tasklist_lock); 6048063e41dSOleg Nesterov for_each_process_thread(p, t) { 60566700001SJosh Stone clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT); 6068063e41dSOleg Nesterov } 6078063e41dSOleg Nesterov read_unlock(&tasklist_lock); 608a871bd33SJason Baron } 609a871bd33SJason Baron } 61060d970c2SIngo Molnar #endif 611