blob: 88ae873ee6cf3164856de63f2b8e2e15ff07f97e [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
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -050062/* Called in removal of a func but failed to allocate a new tp_funcs */
63static void tp_stub_func(void)
64{
65 return;
66}
67
Lai Jiangshan19dba332008-10-28 10:51:49 +080068static inline void *allocate_probes(int count)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040069{
Steven Rostedt38516ab2010-04-20 17:04:50 -040070 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
Lai Jiangshan19dba332008-10-28 10:51:49 +080071 + sizeof(struct tp_probes), GFP_KERNEL);
72 return p == NULL ? NULL : p->probes;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040073}
74
Lai Jiangshan19dba332008-10-28 10:51:49 +080075static void rcu_free_old_probes(struct rcu_head *head)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040076{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040077 kfree(container_of(head, struct tp_probes, rcu));
Lai Jiangshan19dba332008-10-28 10:51:49 +080078}
79
Steven Rostedt38516ab2010-04-20 17:04:50 -040080static inline void release_probes(struct tracepoint_func *old)
Lai Jiangshan19dba332008-10-28 10:51:49 +080081{
82 if (old) {
83 struct tp_probes *tp_probes = container_of(old,
84 struct tp_probes, probes[0]);
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040085 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
Lai Jiangshan19dba332008-10-28 10:51:49 +080086 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040087}
88
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040089static void debug_print_probes(struct tracepoint_func *funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040090{
91 int i;
92
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040093 if (!tracepoint_debug || !funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040094 return;
95
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040096 for (i = 0; funcs[i].func; i++)
97 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040098}
99
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400100static struct tracepoint_func *
101func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
102 int prio)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400103{
Steven Rostedt38516ab2010-04-20 17:04:50 -0400104 struct tracepoint_func *old, *new;
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400105 int nr_probes = 0;
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500106 int stub_funcs = 0;
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400107 int pos = -1;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400108
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400109 if (WARN_ON(!tp_func->func))
Sahara4c69e6e2013-04-15 11:13:15 +0900110 return ERR_PTR(-EINVAL);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400111
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400112 debug_print_probes(*funcs);
113 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400114 if (old) {
115 /* (N -> N+1), (N != 0, 1) probes */
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400116 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
117 /* Insert before probes of lower priority */
118 if (pos < 0 && old[nr_probes].prio < prio)
119 pos = nr_probes;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400120 if (old[nr_probes].func == tp_func->func &&
121 old[nr_probes].data == tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400122 return ERR_PTR(-EEXIST);
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500123 if (old[nr_probes].func == tp_stub_func)
124 stub_funcs++;
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400125 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400126 }
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500127 /* + 2 : one for new probe, one for NULL func - stub functions */
128 new = allocate_probes(nr_probes + 2 - stub_funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400129 if (new == NULL)
130 return ERR_PTR(-ENOMEM);
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400131 if (old) {
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500132 if (stub_funcs) {
133 /* Need to copy one at a time to remove stubs */
134 int probes = 0;
135
136 pos = -1;
137 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
138 if (old[nr_probes].func == tp_stub_func)
139 continue;
140 if (pos < 0 && old[nr_probes].prio < prio)
141 pos = probes++;
142 new[probes++] = old[nr_probes];
143 }
144 nr_probes = probes;
145 if (pos < 0)
146 pos = probes;
147 else
148 nr_probes--; /* Account for insertion */
149
150 } else if (pos < 0) {
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400151 pos = nr_probes;
152 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
153 } else {
154 /* Copy higher priority probes ahead of the new probe */
155 memcpy(new, old, pos * sizeof(struct tracepoint_func));
156 /* Copy the rest after it. */
157 memcpy(new + pos + 1, old + pos,
158 (nr_probes - pos) * sizeof(struct tracepoint_func));
159 }
160 } else
161 pos = 0;
162 new[pos] = *tp_func;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400163 new[nr_probes + 1].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400164 *funcs = new;
165 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400166 return old;
167}
168
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400169static void *func_remove(struct tracepoint_func **funcs,
170 struct tracepoint_func *tp_func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400171{
172 int nr_probes = 0, nr_del = 0, i;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400173 struct tracepoint_func *old, *new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400174
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400175 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400176
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200177 if (!old)
Lai Jiangshan19dba332008-10-28 10:51:49 +0800178 return ERR_PTR(-ENOENT);
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200179
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400180 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400181 /* (N -> M), (N > 1, M >= 0) probes */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400182 if (tp_func->func) {
Sahara4c69e6e2013-04-15 11:13:15 +0900183 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500184 if ((old[nr_probes].func == tp_func->func &&
185 old[nr_probes].data == tp_func->data) ||
186 old[nr_probes].func == tp_stub_func)
Sahara4c69e6e2013-04-15 11:13:15 +0900187 nr_del++;
188 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400189 }
190
Sahara4c69e6e2013-04-15 11:13:15 +0900191 /*
192 * If probe is NULL, then nr_probes = nr_del = 0, and then the
193 * entire entry will be removed.
194 */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400195 if (nr_probes - nr_del == 0) {
196 /* N -> 0, (N > 1) */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400197 *funcs = NULL;
198 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400199 return old;
200 } else {
201 int j = 0;
202 /* N -> M, (N > 1, M > 0) */
203 /* + 1 for NULL */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800204 new = allocate_probes(nr_probes - nr_del + 1);
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500205 if (new) {
206 for (i = 0; old[i].func; i++)
207 if ((old[i].func != tp_func->func
208 || old[i].data != tp_func->data)
209 && old[i].func != tp_stub_func)
210 new[j++] = old[i];
211 new[nr_probes - nr_del].func = NULL;
212 *funcs = new;
213 } else {
214 /*
215 * Failed to allocate, replace the old function
216 * with calls to tp_stub_func.
217 */
218 for (i = 0; old[i].func; i++)
219 if (old[i].func == tp_func->func &&
220 old[i].data == tp_func->data) {
221 old[i].func = tp_stub_func;
222 /* Set the prio to the next event. */
223 if (old[i + 1].func)
224 old[i].prio =
225 old[i + 1].prio;
226 else
227 old[i].prio = -1;
228 }
229 *funcs = old;
230 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400231 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400232 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400233 return old;
234}
235
236/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400237 * Add the probe function to a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400238 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400239static int tracepoint_add_func(struct tracepoint *tp,
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400240 struct tracepoint_func *func, int prio)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400241{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400242 struct tracepoint_func *old, *tp_funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400243
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400244 if (tp->regfunc && !static_key_enabled(&tp->key))
245 tp->regfunc();
246
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400247 tp_funcs = rcu_dereference_protected(tp->funcs,
248 lockdep_is_held(&tracepoints_mutex));
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400249 old = func_add(&tp_funcs, func, prio);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400250 if (IS_ERR(old)) {
Mathieu Desnoyers0f2c8b32018-03-15 08:44:24 -0400251 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400252 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400253 }
Josh Stone97419872009-08-24 14:43:13 -0700254
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400255 /*
256 * rcu_assign_pointer has a smp_wmb() which makes sure that the new
257 * probe callbacks array is consistent before setting a pointer to it.
258 * This array is referenced by __DO_TRACE from
259 * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
260 * is used.
261 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400262 rcu_assign_pointer(tp->funcs, tp_funcs);
263 if (!static_key_enabled(&tp->key))
264 static_key_slow_inc(&tp->key);
Mathieu Desnoyers8058bd02014-05-08 07:47:49 -0400265 release_probes(old);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400266 return 0;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400267}
268
269/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400270 * Remove a probe function from a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400271 * Note: only waiting an RCU period after setting elem->call to the empty
272 * function insures that the original callback is not used anymore. This insured
273 * by preempt_disable around the call site.
274 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400275static int tracepoint_remove_func(struct tracepoint *tp,
276 struct tracepoint_func *func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400277{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400278 struct tracepoint_func *old, *tp_funcs;
Josh Stone97419872009-08-24 14:43:13 -0700279
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400280 tp_funcs = rcu_dereference_protected(tp->funcs,
281 lockdep_is_held(&tracepoints_mutex));
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400282 old = func_remove(&tp_funcs, func);
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500283 if (WARN_ON_ONCE(IS_ERR(old)))
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400284 return PTR_ERR(old);
Steven Rostedt (VMware)75f4d0f2020-11-18 09:34:05 -0500285
286 if (tp_funcs == old)
287 /* Failed allocating new tp_funcs, replaced func with stub */
288 return 0;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400289
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400290 if (!tp_funcs) {
291 /* Removed last function */
292 if (tp->unregfunc && static_key_enabled(&tp->key))
293 tp->unregfunc();
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400294
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400295 if (static_key_enabled(&tp->key))
296 static_key_slow_dec(&tp->key);
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800297 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400298 rcu_assign_pointer(tp->funcs, tp_funcs);
Mathieu Desnoyers8058bd02014-05-08 07:47:49 -0400299 release_probes(old);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400300 return 0;
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800301}
302
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400303/**
304 * tracepoint_probe_register - Connect a probe to a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400305 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400306 * @probe: probe handler
Fabian Frederickcac92ba2014-06-04 16:11:23 -0700307 * @data: tracepoint data
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400308 * @prio: priority of this function over other registered functions
309 *
310 * Returns 0 if ok, error value on error.
311 * Note: if @tp is within a module, the caller is responsible for
312 * unregistering the probe before the module is gone. This can be
313 * performed either with a tracepoint module going notifier, or from
314 * within module exit functions.
315 */
316int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
317 void *data, int prio)
318{
319 struct tracepoint_func tp_func;
320 int ret;
321
322 mutex_lock(&tracepoints_mutex);
323 tp_func.func = probe;
324 tp_func.data = data;
325 tp_func.prio = prio;
326 ret = tracepoint_add_func(tp, &tp_func, prio);
327 mutex_unlock(&tracepoints_mutex);
328 return ret;
329}
330EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
331
332/**
333 * tracepoint_probe_register - Connect a probe to a tracepoint
334 * @tp: tracepoint
335 * @probe: probe handler
336 * @data: tracepoint data
337 * @prio: priority of this function over other registered functions
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400338 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400339 * Returns 0 if ok, error value on error.
340 * Note: if @tp is within a module, the caller is responsible for
341 * unregistering the probe before the module is gone. This can be
342 * performed either with a tracepoint module going notifier, or from
343 * within module exit functions.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400344 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400345int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400346{
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400347 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400348}
349EXPORT_SYMBOL_GPL(tracepoint_probe_register);
350
351/**
352 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400353 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400354 * @probe: probe function pointer
Fabian Frederickcac92ba2014-06-04 16:11:23 -0700355 * @data: tracepoint data
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400356 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400357 * Returns 0 if ok, error value on error.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400358 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400359int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400360{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400361 struct tracepoint_func tp_func;
362 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400363
364 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400365 tp_func.func = probe;
366 tp_func.data = data;
367 ret = tracepoint_remove_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400368 mutex_unlock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400369 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400370}
371EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
372
Ingo Molnar227a8372008-11-16 09:50:34 +0100373#ifdef CONFIG_MODULES
Steven Rostedt (Red Hat)45ab28132014-02-26 13:37:38 -0500374bool trace_module_has_bad_taint(struct module *mod)
375{
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +1030376 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
377 (1 << TAINT_UNSIGNED_MODULE));
Steven Rostedt (Red Hat)45ab28132014-02-26 13:37:38 -0500378}
379
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400380static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
381
382/**
383 * register_tracepoint_notifier - register tracepoint coming/going notifier
384 * @nb: notifier block
385 *
386 * Notifiers registered with this function are called on module
387 * coming/going with the tracepoint_module_list_mutex held.
388 * The notifier block callback should expect a "struct tp_module" data
389 * pointer.
390 */
391int register_tracepoint_module_notifier(struct notifier_block *nb)
392{
393 struct tp_module *tp_mod;
394 int ret;
395
396 mutex_lock(&tracepoint_module_list_mutex);
397 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
398 if (ret)
399 goto end;
400 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
401 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
402end:
403 mutex_unlock(&tracepoint_module_list_mutex);
404 return ret;
405}
406EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
407
408/**
409 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
410 * @nb: notifier block
411 *
412 * The notifier block callback should expect a "struct tp_module" data
413 * pointer.
414 */
415int unregister_tracepoint_module_notifier(struct notifier_block *nb)
416{
417 struct tp_module *tp_mod;
418 int ret;
419
420 mutex_lock(&tracepoint_module_list_mutex);
421 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
422 if (ret)
423 goto end;
424 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
425 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
426end:
427 mutex_unlock(&tracepoint_module_list_mutex);
428 return ret;
429
430}
431EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
432
433/*
434 * Ensure the tracer unregistered the module's probes before the module
435 * teardown is performed. Prevents leaks of probe and data pointers.
436 */
437static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
438 struct tracepoint * const *end)
439{
440 struct tracepoint * const *iter;
441
442 if (!begin)
443 return;
444 for (iter = begin; iter < end; iter++)
445 WARN_ON_ONCE((*iter)->funcs);
446}
447
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400448static int tracepoint_module_coming(struct module *mod)
449{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400450 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400451 int ret = 0;
452
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500453 if (!mod->num_tracepoints)
454 return 0;
455
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400456 /*
Steven Rostedtc10076c2012-01-13 21:40:59 -0500457 * We skip modules that taint the kernel, especially those with different
458 * module headers (for forced load), to make sure we don't cause a crash.
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +1030459 * Staging, out-of-tree, and unsigned GPL modules are fine.
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400460 */
Steven Rostedt (Red Hat)45ab28132014-02-26 13:37:38 -0500461 if (trace_module_has_bad_taint(mod))
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400462 return 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400463 mutex_lock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400464 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
465 if (!tp_mod) {
466 ret = -ENOMEM;
467 goto end;
468 }
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400469 tp_mod->mod = mod;
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400470 list_add_tail(&tp_mod->list, &tracepoint_module_list);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400471 blocking_notifier_call_chain(&tracepoint_notify_list,
472 MODULE_STATE_COMING, tp_mod);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400473end:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400474 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400475 return ret;
476}
477
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400478static void tracepoint_module_going(struct module *mod)
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400479{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400480 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400481
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500482 if (!mod->num_tracepoints)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400483 return;
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500484
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400485 mutex_lock(&tracepoint_module_list_mutex);
486 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400487 if (tp_mod->mod == mod) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400488 blocking_notifier_call_chain(&tracepoint_notify_list,
489 MODULE_STATE_GOING, tp_mod);
490 list_del(&tp_mod->list);
491 kfree(tp_mod);
492 /*
493 * Called the going notifier before checking for
494 * quiescence.
495 */
496 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
497 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400498 break;
499 }
500 }
501 /*
502 * In the case of modules that were tainted at "coming", we'll simply
503 * walk through the list without finding it. We cannot use the "tainted"
504 * flag on "going", in case a module taints the kernel only after being
505 * loaded.
506 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400507 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400508}
Ingo Molnar227a8372008-11-16 09:50:34 +0100509
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400510static int tracepoint_module_notify(struct notifier_block *self,
511 unsigned long val, void *data)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500512{
513 struct module *mod = data;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400514 int ret = 0;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500515
516 switch (val) {
517 case MODULE_STATE_COMING:
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400518 ret = tracepoint_module_coming(mod);
519 break;
520 case MODULE_STATE_LIVE:
521 break;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500522 case MODULE_STATE_GOING:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400523 tracepoint_module_going(mod);
524 break;
525 case MODULE_STATE_UNFORMED:
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500526 break;
527 }
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400528 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500529}
530
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400531static struct notifier_block tracepoint_module_nb = {
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500532 .notifier_call = tracepoint_module_notify,
533 .priority = 0,
534};
535
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400536static __init int init_tracepoints(void)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500537{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400538 int ret;
539
540 ret = register_module_notifier(&tracepoint_module_nb);
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400541 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700542 pr_warn("Failed to register tracepoint module enter notifier\n");
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400543
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400544 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500545}
546__initcall(init_tracepoints);
Ingo Molnar227a8372008-11-16 09:50:34 +0100547#endif /* CONFIG_MODULES */
Jason Barona871bd32009-08-10 16:52:31 -0400548
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400549static void for_each_tracepoint_range(struct tracepoint * const *begin,
550 struct tracepoint * const *end,
551 void (*fct)(struct tracepoint *tp, void *priv),
552 void *priv)
553{
554 struct tracepoint * const *iter;
555
556 if (!begin)
557 return;
558 for (iter = begin; iter < end; iter++)
559 fct(*iter, priv);
560}
561
562/**
563 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
564 * @fct: callback
565 * @priv: private data
566 */
567void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
568 void *priv)
569{
570 for_each_tracepoint_range(__start___tracepoints_ptrs,
571 __stop___tracepoints_ptrs, fct, priv);
572}
573EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
574
Josh Stone3d27d8c2009-08-24 14:43:12 -0700575#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
Ingo Molnar60d970c2009-08-13 23:37:26 +0200576
Josh Stone97419872009-08-24 14:43:13 -0700577/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
Jason Barona871bd32009-08-10 16:52:31 -0400578static int sys_tracepoint_refcount;
579
580void syscall_regfunc(void)
581{
Oleg Nesterov8063e412014-04-13 20:59:18 +0200582 struct task_struct *p, *t;
Jason Barona871bd32009-08-10 16:52:31 -0400583
Jason Barona871bd32009-08-10 16:52:31 -0400584 if (!sys_tracepoint_refcount) {
Oleg Nesterov8063e412014-04-13 20:59:18 +0200585 read_lock(&tasklist_lock);
586 for_each_process_thread(p, t) {
Oleg Nesterovea73c792014-04-13 20:59:38 +0200587 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Oleg Nesterov8063e412014-04-13 20:59:18 +0200588 }
589 read_unlock(&tasklist_lock);
Jason Barona871bd32009-08-10 16:52:31 -0400590 }
591 sys_tracepoint_refcount++;
Jason Barona871bd32009-08-10 16:52:31 -0400592}
593
594void syscall_unregfunc(void)
595{
Oleg Nesterov8063e412014-04-13 20:59:18 +0200596 struct task_struct *p, *t;
Jason Barona871bd32009-08-10 16:52:31 -0400597
Jason Barona871bd32009-08-10 16:52:31 -0400598 sys_tracepoint_refcount--;
599 if (!sys_tracepoint_refcount) {
Oleg Nesterov8063e412014-04-13 20:59:18 +0200600 read_lock(&tasklist_lock);
601 for_each_process_thread(p, t) {
Josh Stone66700002009-08-24 14:43:11 -0700602 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Oleg Nesterov8063e412014-04-13 20:59:18 +0200603 }
604 read_unlock(&tasklist_lock);
Jason Barona871bd32009-08-10 16:52:31 -0400605 }
Jason Barona871bd32009-08-10 16:52:31 -0400606}
Ingo Molnar60d970c2009-08-13 23:37:26 +0200607#endif