blob: 01b3bd84daa192ba5fec7d7ed0893d4944090e4e [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
184 tp_funcs = tp->funcs;
185 old = func_add(&tp_funcs, func);
186 if (IS_ERR(old)) {
187 WARN_ON_ONCE(1);
188 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400189 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400190 release_probes(old);
Josh Stone97419872009-08-24 14:43:13 -0700191
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400192 /*
193 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
194 * probe callbacks array is consistent before setting a pointer to it.
195 * This array is referenced by __DO_TRACE from
196 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
197 * is used.
198 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400199 rcu_assign_pointer(tp->funcs, tp_funcs);
200 if (!static_key_enabled(&tp->key))
201 static_key_slow_inc(&tp->key);
202 return 0;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400203}
204
205/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400206 * Remove a probe function from a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400207 * Note: only waiting an RCU period after setting elem->call to the empty
208 * function insures that the original callback is not used anymore. This insured
209 * by preempt_disable around the call site.
210 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400211static int tracepoint_remove_func(struct tracepoint *tp,
212 struct tracepoint_func *func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400213{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400214 struct tracepoint_func *old, *tp_funcs;
Josh Stone97419872009-08-24 14:43:13 -0700215
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400216 tp_funcs = tp->funcs;
217 old = func_remove(&tp_funcs, func);
218 if (IS_ERR(old)) {
219 WARN_ON_ONCE(1);
220 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400221 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400222 release_probes(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400223
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400224 if (!tp_funcs) {
225 /* Removed last function */
226 if (tp->unregfunc && static_key_enabled(&tp->key))
227 tp->unregfunc();
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400228
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400229 if (static_key_enabled(&tp->key))
230 static_key_slow_dec(&tp->key);
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800231 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400232 rcu_assign_pointer(tp->funcs, tp_funcs);
233 return 0;
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800234}
235
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400236/**
237 * tracepoint_probe_register - Connect a probe to a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400238 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400239 * @probe: probe handler
240 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400241 * Returns 0 if ok, error value on error.
242 * Note: if @tp is within a module, the caller is responsible for
243 * unregistering the probe before the module is gone. This can be
244 * performed either with a tracepoint module going notifier, or from
245 * within module exit functions.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400246 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400247int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400248{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400249 struct tracepoint_func tp_func;
250 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400251
252 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400253 tp_func.func = probe;
254 tp_func.data = data;
255 ret = tracepoint_add_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400256 mutex_unlock(&tracepoints_mutex);
Steven Rostedtb196e2b2014-02-13 15:45:07 -0500257 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400258}
259EXPORT_SYMBOL_GPL(tracepoint_probe_register);
260
261/**
262 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400263 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400264 * @probe: probe function pointer
265 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400266 * Returns 0 if ok, error value on error.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400267 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400268int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400269{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400270 struct tracepoint_func tp_func;
271 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400272
273 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400274 tp_func.func = probe;
275 tp_func.data = data;
276 ret = tracepoint_remove_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400277 mutex_unlock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400278 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400279}
280EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
281
Ingo Molnar227a8372008-11-16 09:50:34 +0100282#ifdef CONFIG_MODULES
Steven Rostedt (Red Hat)45ab28132014-02-26 13:37:38 -0500283bool trace_module_has_bad_taint(struct module *mod)
284{
285 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP));
286}
287
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400288static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
289
290/**
291 * register_tracepoint_notifier - register tracepoint coming/going notifier
292 * @nb: notifier block
293 *
294 * Notifiers registered with this function are called on module
295 * coming/going with the tracepoint_module_list_mutex held.
296 * The notifier block callback should expect a "struct tp_module" data
297 * pointer.
298 */
299int register_tracepoint_module_notifier(struct notifier_block *nb)
300{
301 struct tp_module *tp_mod;
302 int ret;
303
304 mutex_lock(&tracepoint_module_list_mutex);
305 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
306 if (ret)
307 goto end;
308 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
309 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
310end:
311 mutex_unlock(&tracepoint_module_list_mutex);
312 return ret;
313}
314EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
315
316/**
317 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
318 * @nb: notifier block
319 *
320 * The notifier block callback should expect a "struct tp_module" data
321 * pointer.
322 */
323int unregister_tracepoint_module_notifier(struct notifier_block *nb)
324{
325 struct tp_module *tp_mod;
326 int ret;
327
328 mutex_lock(&tracepoint_module_list_mutex);
329 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
330 if (ret)
331 goto end;
332 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
333 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
334end:
335 mutex_unlock(&tracepoint_module_list_mutex);
336 return ret;
337
338}
339EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
340
341/*
342 * Ensure the tracer unregistered the module's probes before the module
343 * teardown is performed. Prevents leaks of probe and data pointers.
344 */
345static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
346 struct tracepoint * const *end)
347{
348 struct tracepoint * const *iter;
349
350 if (!begin)
351 return;
352 for (iter = begin; iter < end; iter++)
353 WARN_ON_ONCE((*iter)->funcs);
354}
355
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400356static int tracepoint_module_coming(struct module *mod)
357{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400358 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400359 int ret = 0;
360
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500361 if (!mod->num_tracepoints)
362 return 0;
363
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400364 /*
Steven Rostedtc10076c2012-01-13 21:40:59 -0500365 * We skip modules that taint the kernel, especially those with different
366 * module headers (for forced load), to make sure we don't cause a crash.
367 * Staging and out-of-tree GPL modules are fine.
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400368 */
Steven Rostedt (Red Hat)45ab28132014-02-26 13:37:38 -0500369 if (trace_module_has_bad_taint(mod))
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400370 return 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400371 mutex_lock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400372 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
373 if (!tp_mod) {
374 ret = -ENOMEM;
375 goto end;
376 }
377 tp_mod->num_tracepoints = mod->num_tracepoints;
378 tp_mod->tracepoints_ptrs = mod->tracepoints_ptrs;
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400379 list_add_tail(&tp_mod->list, &tracepoint_module_list);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400380 blocking_notifier_call_chain(&tracepoint_notify_list,
381 MODULE_STATE_COMING, tp_mod);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400382end:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400383 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400384 return ret;
385}
386
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400387static void tracepoint_module_going(struct module *mod)
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400388{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400389 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400390
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500391 if (!mod->num_tracepoints)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400392 return;
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500393
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400394 mutex_lock(&tracepoint_module_list_mutex);
395 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
396 if (tp_mod->tracepoints_ptrs == mod->tracepoints_ptrs) {
397 blocking_notifier_call_chain(&tracepoint_notify_list,
398 MODULE_STATE_GOING, tp_mod);
399 list_del(&tp_mod->list);
400 kfree(tp_mod);
401 /*
402 * Called the going notifier before checking for
403 * quiescence.
404 */
405 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
406 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400407 break;
408 }
409 }
410 /*
411 * In the case of modules that were tainted at "coming", we'll simply
412 * walk through the list without finding it. We cannot use the "tainted"
413 * flag on "going", in case a module taints the kernel only after being
414 * loaded.
415 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400416 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400417}
Ingo Molnar227a8372008-11-16 09:50:34 +0100418
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400419static int tracepoint_module_notify(struct notifier_block *self,
420 unsigned long val, void *data)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500421{
422 struct module *mod = data;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400423 int ret = 0;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500424
425 switch (val) {
426 case MODULE_STATE_COMING:
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400427 ret = tracepoint_module_coming(mod);
428 break;
429 case MODULE_STATE_LIVE:
430 break;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500431 case MODULE_STATE_GOING:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400432 tracepoint_module_going(mod);
433 break;
434 case MODULE_STATE_UNFORMED:
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500435 break;
436 }
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400437 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500438}
439
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400440static struct notifier_block tracepoint_module_nb = {
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500441 .notifier_call = tracepoint_module_notify,
442 .priority = 0,
443};
444
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400445static __init int init_tracepoints(void)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500446{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400447 int ret;
448
449 ret = register_module_notifier(&tracepoint_module_nb);
450 if (ret) {
451 pr_warning("Failed to register tracepoint module enter notifier\n");
452 }
453 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500454}
455__initcall(init_tracepoints);
Ingo Molnar227a8372008-11-16 09:50:34 +0100456#endif /* CONFIG_MODULES */
Jason Barona871bd32009-08-10 16:52:31 -0400457
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400458static void for_each_tracepoint_range(struct tracepoint * const *begin,
459 struct tracepoint * const *end,
460 void (*fct)(struct tracepoint *tp, void *priv),
461 void *priv)
462{
463 struct tracepoint * const *iter;
464
465 if (!begin)
466 return;
467 for (iter = begin; iter < end; iter++)
468 fct(*iter, priv);
469}
470
471/**
472 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
473 * @fct: callback
474 * @priv: private data
475 */
476void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
477 void *priv)
478{
479 for_each_tracepoint_range(__start___tracepoints_ptrs,
480 __stop___tracepoints_ptrs, fct, priv);
481}
482EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
483
Josh Stone3d27d8c2009-08-24 14:43:12 -0700484#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
Ingo Molnar60d970c2009-08-13 23:37:26 +0200485
Josh Stone97419872009-08-24 14:43:13 -0700486/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
Jason Barona871bd32009-08-10 16:52:31 -0400487static int sys_tracepoint_refcount;
488
489void syscall_regfunc(void)
490{
491 unsigned long flags;
492 struct task_struct *g, *t;
493
Jason Barona871bd32009-08-10 16:52:31 -0400494 if (!sys_tracepoint_refcount) {
495 read_lock_irqsave(&tasklist_lock, flags);
496 do_each_thread(g, t) {
Hendrik Bruecknercc3b13c2009-08-25 18:02:37 +0200497 /* Skip kernel threads. */
498 if (t->mm)
499 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Jason Barona871bd32009-08-10 16:52:31 -0400500 } while_each_thread(g, t);
501 read_unlock_irqrestore(&tasklist_lock, flags);
502 }
503 sys_tracepoint_refcount++;
Jason Barona871bd32009-08-10 16:52:31 -0400504}
505
506void syscall_unregfunc(void)
507{
508 unsigned long flags;
509 struct task_struct *g, *t;
510
Jason Barona871bd32009-08-10 16:52:31 -0400511 sys_tracepoint_refcount--;
512 if (!sys_tracepoint_refcount) {
513 read_lock_irqsave(&tasklist_lock, flags);
514 do_each_thread(g, t) {
Josh Stone66700002009-08-24 14:43:11 -0700515 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Jason Barona871bd32009-08-10 16:52:31 -0400516 } while_each_thread(g, t);
517 read_unlock_irqrestore(&tasklist_lock, flags);
518 }
Jason Barona871bd32009-08-10 16:52:31 -0400519}
Ingo Molnar60d970c2009-08-13 23:37:26 +0200520#endif