blob: ca2cfe21bb8e22ab02b3cb7a5c3536dcc8309a6f [file] [log] [blame]
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04001/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002 * Copyright (C) 2008-2014 Mathieu Desnoyers
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -04003 *
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>
Jason Barona871bd32009-08-10 16:52:31 -040027#include <linux/sched.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +010028#include <linux/static_key.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040029
Mathieu Desnoyers65498642011-01-26 17:26:22 -050030extern struct tracepoint * const __start___tracepoints_ptrs[];
31extern struct tracepoint * const __stop___tracepoints_ptrs[];
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040032
33/* Set to 1 to enable tracepoint debug output */
34static const int tracepoint_debug;
35
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -040036#ifdef CONFIG_MODULES
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040037/*
38 * Tracepoint module list mutex protects the local module list.
39 */
40static DEFINE_MUTEX(tracepoint_module_list_mutex);
41
42/* Local list of struct tp_module */
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -040043static LIST_HEAD(tracepoint_module_list);
44#endif /* CONFIG_MODULES */
45
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040046/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040047 * tracepoints_mutex protects the builtin and module tracepoints.
48 * tracepoints_mutex nests inside tracepoint_module_list_mutex.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040049 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040050static DEFINE_MUTEX(tracepoints_mutex);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040051
52/*
53 * Note about RCU :
Anand Gadiyarfd589a82009-07-16 17:13:03 +020054 * It is used to delay the free of multiple probes array until a quiescent
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040055 * state is reached.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040056 */
Lai Jiangshan19dba332008-10-28 10:51:49 +080057struct tp_probes {
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040058 struct rcu_head rcu;
Steven Rostedt38516ab2010-04-20 17:04:50 -040059 struct tracepoint_func probes[0];
Lai Jiangshan19dba332008-10-28 10:51:49 +080060};
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040061
Lai Jiangshan19dba332008-10-28 10:51:49 +080062static inline void *allocate_probes(int count)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040063{
Steven Rostedt38516ab2010-04-20 17:04:50 -040064 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
Lai Jiangshan19dba332008-10-28 10:51:49 +080065 + sizeof(struct tp_probes), GFP_KERNEL);
66 return p == NULL ? NULL : p->probes;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040067}
68
Lai Jiangshan19dba332008-10-28 10:51:49 +080069static void rcu_free_old_probes(struct rcu_head *head)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040070{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040071 kfree(container_of(head, struct tp_probes, rcu));
Lai Jiangshan19dba332008-10-28 10:51:49 +080072}
73
Steven Rostedt38516ab2010-04-20 17:04:50 -040074static inline void release_probes(struct tracepoint_func *old)
Lai Jiangshan19dba332008-10-28 10:51:49 +080075{
76 if (old) {
77 struct tp_probes *tp_probes = container_of(old,
78 struct tp_probes, probes[0]);
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040079 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
Lai Jiangshan19dba332008-10-28 10:51:49 +080080 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040081}
82
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040083static void debug_print_probes(struct tracepoint_func *funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040084{
85 int i;
86
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040087 if (!tracepoint_debug || !funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040088 return;
89
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040090 for (i = 0; funcs[i].func; i++)
91 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040092}
93
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040094static struct tracepoint_func *func_add(struct tracepoint_func **funcs,
95 struct tracepoint_func *tp_func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040096{
97 int nr_probes = 0;
Steven Rostedt38516ab2010-04-20 17:04:50 -040098 struct tracepoint_func *old, *new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040099
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400100 if (WARN_ON(!tp_func->func))
Sahara4c69e6e2013-04-15 11:13:15 +0900101 return ERR_PTR(-EINVAL);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400102
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400103 debug_print_probes(*funcs);
104 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400105 if (old) {
106 /* (N -> N+1), (N != 0, 1) probes */
Steven Rostedt38516ab2010-04-20 17:04:50 -0400107 for (nr_probes = 0; old[nr_probes].func; nr_probes++)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400108 if (old[nr_probes].func == tp_func->func &&
109 old[nr_probes].data == tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400110 return ERR_PTR(-EEXIST);
111 }
112 /* + 2 : one for new probe, one for NULL func */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800113 new = allocate_probes(nr_probes + 2);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400114 if (new == NULL)
115 return ERR_PTR(-ENOMEM);
116 if (old)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400117 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400118 new[nr_probes] = *tp_func;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400119 new[nr_probes + 1].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400120 *funcs = new;
121 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400122 return old;
123}
124
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400125static void *func_remove(struct tracepoint_func **funcs,
126 struct tracepoint_func *tp_func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400127{
128 int nr_probes = 0, nr_del = 0, i;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400129 struct tracepoint_func *old, *new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400130
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400131 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400132
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200133 if (!old)
Lai Jiangshan19dba332008-10-28 10:51:49 +0800134 return ERR_PTR(-ENOENT);
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200135
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400136 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400137 /* (N -> M), (N > 1, M >= 0) probes */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400138 if (tp_func->func) {
Sahara4c69e6e2013-04-15 11:13:15 +0900139 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400140 if (old[nr_probes].func == tp_func->func &&
141 old[nr_probes].data == tp_func->data)
Sahara4c69e6e2013-04-15 11:13:15 +0900142 nr_del++;
143 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400144 }
145
Sahara4c69e6e2013-04-15 11:13:15 +0900146 /*
147 * If probe is NULL, then nr_probes = nr_del = 0, and then the
148 * entire entry will be removed.
149 */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400150 if (nr_probes - nr_del == 0) {
151 /* N -> 0, (N > 1) */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400152 *funcs = NULL;
153 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400154 return old;
155 } else {
156 int j = 0;
157 /* N -> M, (N > 1, M > 0) */
158 /* + 1 for NULL */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800159 new = allocate_probes(nr_probes - nr_del + 1);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400160 if (new == NULL)
161 return ERR_PTR(-ENOMEM);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400162 for (i = 0; old[i].func; i++)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400163 if (old[i].func != tp_func->func
164 || old[i].data != tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400165 new[j++] = old[i];
Steven Rostedt38516ab2010-04-20 17:04:50 -0400166 new[nr_probes - nr_del].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400167 *funcs = new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400168 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400169 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400170 return old;
171}
172
173/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400174 * Add the probe function to a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400175 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400176static int tracepoint_add_func(struct tracepoint *tp,
177 struct tracepoint_func *func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400178{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400179 struct tracepoint_func *old, *tp_funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400180
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400181 if (tp->regfunc && !static_key_enabled(&tp->key))
182 tp->regfunc();
183
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400184 tp_funcs = rcu_dereference_protected(tp->funcs,
185 lockdep_is_held(&tracepoints_mutex));
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400186 old = func_add(&tp_funcs, func);
187 if (IS_ERR(old)) {
188 WARN_ON_ONCE(1);
189 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400190 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400191 release_probes(old);
Josh Stone97419872009-08-24 14:43:13 -0700192
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400193 /*
194 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
195 * probe callbacks array is consistent before setting a pointer to it.
196 * This array is referenced by __DO_TRACE from
197 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
198 * is used.
199 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400200 rcu_assign_pointer(tp->funcs, tp_funcs);
201 if (!static_key_enabled(&tp->key))
202 static_key_slow_inc(&tp->key);
203 return 0;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400204}
205
206/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400207 * Remove a probe function from a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400208 * Note: only waiting an RCU period after setting elem->call to the empty
209 * function insures that the original callback is not used anymore. This insured
210 * by preempt_disable around the call site.
211 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400212static int tracepoint_remove_func(struct tracepoint *tp,
213 struct tracepoint_func *func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400214{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400215 struct tracepoint_func *old, *tp_funcs;
Josh Stone97419872009-08-24 14:43:13 -0700216
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400217 tp_funcs = rcu_dereference_protected(tp->funcs,
218 lockdep_is_held(&tracepoints_mutex));
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400219 old = func_remove(&tp_funcs, func);
220 if (IS_ERR(old)) {
221 WARN_ON_ONCE(1);
222 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400223 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400224 release_probes(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400225
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400226 if (!tp_funcs) {
227 /* Removed last function */
228 if (tp->unregfunc && static_key_enabled(&tp->key))
229 tp->unregfunc();
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400230
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400231 if (static_key_enabled(&tp->key))
232 static_key_slow_dec(&tp->key);
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800233 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400234 rcu_assign_pointer(tp->funcs, tp_funcs);
235 return 0;
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800236}
237
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400238/**
239 * tracepoint_probe_register - Connect a probe to a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400240 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400241 * @probe: probe handler
242 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400243 * Returns 0 if ok, error value on error.
244 * Note: if @tp is within a module, the caller is responsible for
245 * unregistering the probe before the module is gone. This can be
246 * performed either with a tracepoint module going notifier, or from
247 * within module exit functions.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400248 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400249int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400250{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400251 struct tracepoint_func tp_func;
252 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400253
254 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400255 tp_func.func = probe;
256 tp_func.data = data;
257 ret = tracepoint_add_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400258 mutex_unlock(&tracepoints_mutex);
Steven Rostedtb196e2b2014-02-13 15:45:07 -0500259 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400260}
261EXPORT_SYMBOL_GPL(tracepoint_probe_register);
262
263/**
264 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400265 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400266 * @probe: probe function pointer
267 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400268 * Returns 0 if ok, error value on error.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400269 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400270int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400271{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400272 struct tracepoint_func tp_func;
273 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400274
275 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400276 tp_func.func = probe;
277 tp_func.data = data;
278 ret = tracepoint_remove_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400279 mutex_unlock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400280 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400281}
282EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
283
Ingo Molnar227a8372008-11-16 09:50:34 +0100284#ifdef CONFIG_MODULES
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500285bool trace_module_has_bad_taint(struct module *mod)
286{
287 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP));
288}
289
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400290static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
291
292/**
293 * register_tracepoint_notifier - register tracepoint coming/going notifier
294 * @nb: notifier block
295 *
296 * Notifiers registered with this function are called on module
297 * coming/going with the tracepoint_module_list_mutex held.
298 * The notifier block callback should expect a "struct tp_module" data
299 * pointer.
300 */
301int register_tracepoint_module_notifier(struct notifier_block *nb)
302{
303 struct tp_module *tp_mod;
304 int ret;
305
306 mutex_lock(&tracepoint_module_list_mutex);
307 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
308 if (ret)
309 goto end;
310 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
311 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
312end:
313 mutex_unlock(&tracepoint_module_list_mutex);
314 return ret;
315}
316EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
317
318/**
319 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
320 * @nb: notifier block
321 *
322 * The notifier block callback should expect a "struct tp_module" data
323 * pointer.
324 */
325int unregister_tracepoint_module_notifier(struct notifier_block *nb)
326{
327 struct tp_module *tp_mod;
328 int ret;
329
330 mutex_lock(&tracepoint_module_list_mutex);
331 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
332 if (ret)
333 goto end;
334 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
335 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
336end:
337 mutex_unlock(&tracepoint_module_list_mutex);
338 return ret;
339
340}
341EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
342
343/*
344 * Ensure the tracer unregistered the module's probes before the module
345 * teardown is performed. Prevents leaks of probe and data pointers.
346 */
347static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
348 struct tracepoint * const *end)
349{
350 struct tracepoint * const *iter;
351
352 if (!begin)
353 return;
354 for (iter = begin; iter < end; iter++)
355 WARN_ON_ONCE((*iter)->funcs);
356}
357
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400358static int tracepoint_module_coming(struct module *mod)
359{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400360 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400361 int ret = 0;
362
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500363 if (!mod->num_tracepoints)
364 return 0;
365
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400366 /*
Steven Rostedtc10076c2012-01-13 21:40:59 -0500367 * We skip modules that taint the kernel, especially those with different
368 * module headers (for forced load), to make sure we don't cause a crash.
369 * Staging and out-of-tree GPL modules are fine.
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400370 */
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500371 if (trace_module_has_bad_taint(mod))
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400372 return 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400373 mutex_lock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400374 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
375 if (!tp_mod) {
376 ret = -ENOMEM;
377 goto end;
378 }
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400379 tp_mod->mod = mod;
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400380 list_add_tail(&tp_mod->list, &tracepoint_module_list);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400381 blocking_notifier_call_chain(&tracepoint_notify_list,
382 MODULE_STATE_COMING, tp_mod);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400383end:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400384 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400385 return ret;
386}
387
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400388static void tracepoint_module_going(struct module *mod)
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400389{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400390 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400391
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500392 if (!mod->num_tracepoints)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400393 return;
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500394
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400395 mutex_lock(&tracepoint_module_list_mutex);
396 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400397 if (tp_mod->mod == mod) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400398 blocking_notifier_call_chain(&tracepoint_notify_list,
399 MODULE_STATE_GOING, tp_mod);
400 list_del(&tp_mod->list);
401 kfree(tp_mod);
402 /*
403 * Called the going notifier before checking for
404 * quiescence.
405 */
406 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
407 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400408 break;
409 }
410 }
411 /*
412 * In the case of modules that were tainted at "coming", we'll simply
413 * walk through the list without finding it. We cannot use the "tainted"
414 * flag on "going", in case a module taints the kernel only after being
415 * loaded.
416 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400417 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400418}
Ingo Molnar227a8372008-11-16 09:50:34 +0100419
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400420static int tracepoint_module_notify(struct notifier_block *self,
421 unsigned long val, void *data)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500422{
423 struct module *mod = data;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400424 int ret = 0;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500425
426 switch (val) {
427 case MODULE_STATE_COMING:
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400428 ret = tracepoint_module_coming(mod);
429 break;
430 case MODULE_STATE_LIVE:
431 break;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500432 case MODULE_STATE_GOING:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400433 tracepoint_module_going(mod);
434 break;
435 case MODULE_STATE_UNFORMED:
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500436 break;
437 }
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400438 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500439}
440
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400441static struct notifier_block tracepoint_module_nb = {
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500442 .notifier_call = tracepoint_module_notify,
443 .priority = 0,
444};
445
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400446static __init int init_tracepoints(void)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500447{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400448 int ret;
449
450 ret = register_module_notifier(&tracepoint_module_nb);
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400451 if (ret)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400452 pr_warning("Failed to register tracepoint module enter notifier\n");
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400453
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400454 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500455}
456__initcall(init_tracepoints);
Ingo Molnar227a8372008-11-16 09:50:34 +0100457#endif /* CONFIG_MODULES */
Jason Barona871bd32009-08-10 16:52:31 -0400458
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400459static void for_each_tracepoint_range(struct tracepoint * const *begin,
460 struct tracepoint * const *end,
461 void (*fct)(struct tracepoint *tp, void *priv),
462 void *priv)
463{
464 struct tracepoint * const *iter;
465
466 if (!begin)
467 return;
468 for (iter = begin; iter < end; iter++)
469 fct(*iter, priv);
470}
471
472/**
473 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
474 * @fct: callback
475 * @priv: private data
476 */
477void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
478 void *priv)
479{
480 for_each_tracepoint_range(__start___tracepoints_ptrs,
481 __stop___tracepoints_ptrs, fct, priv);
482}
483EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
484
Josh Stone3d27d8cb2009-08-24 14:43:12 -0700485#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
Ingo Molnar60d970c2009-08-13 23:37:26 +0200486
Josh Stone97419872009-08-24 14:43:13 -0700487/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
Jason Barona871bd32009-08-10 16:52:31 -0400488static int sys_tracepoint_refcount;
489
490void syscall_regfunc(void)
491{
492 unsigned long flags;
493 struct task_struct *g, *t;
494
Jason Barona871bd32009-08-10 16:52:31 -0400495 if (!sys_tracepoint_refcount) {
496 read_lock_irqsave(&tasklist_lock, flags);
497 do_each_thread(g, t) {
Hendrik Bruecknercc3b13c2009-08-25 18:02:37 +0200498 /* Skip kernel threads. */
499 if (t->mm)
500 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Jason Barona871bd32009-08-10 16:52:31 -0400501 } while_each_thread(g, t);
502 read_unlock_irqrestore(&tasklist_lock, flags);
503 }
504 sys_tracepoint_refcount++;
Jason Barona871bd32009-08-10 16:52:31 -0400505}
506
507void syscall_unregfunc(void)
508{
509 unsigned long flags;
510 struct task_struct *g, *t;
511
Jason Barona871bd32009-08-10 16:52:31 -0400512 sys_tracepoint_refcount--;
513 if (!sys_tracepoint_refcount) {
514 read_lock_irqsave(&tasklist_lock, flags);
515 do_each_thread(g, t) {
Josh Stone66700002009-08-24 14:43:11 -0700516 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Jason Barona871bd32009-08-10 16:52:31 -0400517 } while_each_thread(g, t);
518 read_unlock_irqrestore(&tasklist_lock, flags);
519 }
Jason Barona871bd32009-08-10 16:52:31 -0400520}
Ingo Molnar60d970c2009-08-13 23:37:26 +0200521#endif