blob: 96db841bf0fc89d1bea38ee7ccfb8faf909df420 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010027#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010028#include <linux/sched/task.h>
Ingo Molnarc5905af2012-02-24 08:31:31 +010029#include <linux/static_key.h>
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040030
Mathieu Desnoyers65498642011-01-26 17:26:22 -050031extern struct tracepoint * const __start___tracepoints_ptrs[];
32extern struct tracepoint * const __stop___tracepoints_ptrs[];
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040033
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -070034DEFINE_SRCU(tracepoint_srcu);
35EXPORT_SYMBOL_GPL(tracepoint_srcu);
36
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040037/* Set to 1 to enable tracepoint debug output */
38static const int tracepoint_debug;
39
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -040040#ifdef CONFIG_MODULES
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040041/*
42 * Tracepoint module list mutex protects the local module list.
43 */
44static DEFINE_MUTEX(tracepoint_module_list_mutex);
45
46/* Local list of struct tp_module */
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -040047static LIST_HEAD(tracepoint_module_list);
48#endif /* CONFIG_MODULES */
49
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040050/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040051 * tracepoints_mutex protects the builtin and module tracepoints.
52 * tracepoints_mutex nests inside tracepoint_module_list_mutex.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040053 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040054static DEFINE_MUTEX(tracepoints_mutex);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040055
Steven Rostedt (VMware)f8a79d52018-08-10 12:17:50 -040056static struct rcu_head *early_probes;
57static bool ok_to_free_tracepoints;
58
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040059/*
60 * Note about RCU :
Anand Gadiyarfd589a82009-07-16 17:13:03 +020061 * It is used to delay the free of multiple probes array until a quiescent
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040062 * state is reached.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040063 */
Lai Jiangshan19dba332008-10-28 10:51:49 +080064struct tp_probes {
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040065 struct rcu_head rcu;
Steven Rostedt38516ab2010-04-20 17:04:50 -040066 struct tracepoint_func probes[0];
Lai Jiangshan19dba332008-10-28 10:51:49 +080067};
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040068
Lai Jiangshan19dba332008-10-28 10:51:49 +080069static inline void *allocate_probes(int count)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040070{
Steven Rostedt38516ab2010-04-20 17:04:50 -040071 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
Lai Jiangshan19dba332008-10-28 10:51:49 +080072 + sizeof(struct tp_probes), GFP_KERNEL);
73 return p == NULL ? NULL : p->probes;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040074}
75
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -070076static void srcu_free_old_probes(struct rcu_head *head)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040077{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040078 kfree(container_of(head, struct tp_probes, rcu));
Lai Jiangshan19dba332008-10-28 10:51:49 +080079}
80
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -070081static void rcu_free_old_probes(struct rcu_head *head)
82{
83 call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
84}
85
Steven Rostedt (VMware)f8a79d52018-08-10 12:17:50 -040086static __init int release_early_probes(void)
87{
88 struct rcu_head *tmp;
89
90 ok_to_free_tracepoints = true;
91
92 while (early_probes) {
93 tmp = early_probes;
94 early_probes = tmp->next;
95 call_rcu_sched(tmp, rcu_free_old_probes);
96 }
97
98 return 0;
99}
100
101/* SRCU is initialized at core_initcall */
102postcore_initcall(release_early_probes);
103
Steven Rostedt38516ab2010-04-20 17:04:50 -0400104static inline void release_probes(struct tracepoint_func *old)
Lai Jiangshan19dba332008-10-28 10:51:49 +0800105{
106 if (old) {
107 struct tp_probes *tp_probes = container_of(old,
108 struct tp_probes, probes[0]);
Steven Rostedt (VMware)f8a79d52018-08-10 12:17:50 -0400109
110 /*
111 * We can't free probes if SRCU is not initialized yet.
112 * Postpone the freeing till after SRCU is initialized.
113 */
114 if (unlikely(!ok_to_free_tracepoints)) {
115 tp_probes->rcu.next = early_probes;
116 early_probes = &tp_probes->rcu;
117 return;
118 }
119
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -0700120 /*
121 * Tracepoint probes are protected by both sched RCU and SRCU,
122 * by calling the SRCU callback in the sched RCU callback we
123 * cover both cases. So let us chain the SRCU and sched RCU
124 * callbacks to wait for both grace periods.
125 */
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400126 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
Lai Jiangshan19dba332008-10-28 10:51:49 +0800127 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400128}
129
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400130static void debug_print_probes(struct tracepoint_func *funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400131{
132 int i;
133
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400134 if (!tracepoint_debug || !funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400135 return;
136
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400137 for (i = 0; funcs[i].func; i++)
138 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400139}
140
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400141static struct tracepoint_func *
142func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
143 int prio)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400144{
Steven Rostedt38516ab2010-04-20 17:04:50 -0400145 struct tracepoint_func *old, *new;
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400146 int nr_probes = 0;
147 int pos = -1;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400148
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400149 if (WARN_ON(!tp_func->func))
Sahara4c69e6e2013-04-15 11:13:15 +0900150 return ERR_PTR(-EINVAL);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400151
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400152 debug_print_probes(*funcs);
153 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400154 if (old) {
155 /* (N -> N+1), (N != 0, 1) probes */
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400156 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
157 /* Insert before probes of lower priority */
158 if (pos < 0 && old[nr_probes].prio < prio)
159 pos = nr_probes;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400160 if (old[nr_probes].func == tp_func->func &&
161 old[nr_probes].data == tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400162 return ERR_PTR(-EEXIST);
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400163 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400164 }
165 /* + 2 : one for new probe, one for NULL func */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800166 new = allocate_probes(nr_probes + 2);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400167 if (new == NULL)
168 return ERR_PTR(-ENOMEM);
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400169 if (old) {
170 if (pos < 0) {
171 pos = nr_probes;
172 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
173 } else {
174 /* Copy higher priority probes ahead of the new probe */
175 memcpy(new, old, pos * sizeof(struct tracepoint_func));
176 /* Copy the rest after it. */
177 memcpy(new + pos + 1, old + pos,
178 (nr_probes - pos) * sizeof(struct tracepoint_func));
179 }
180 } else
181 pos = 0;
182 new[pos] = *tp_func;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400183 new[nr_probes + 1].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400184 *funcs = new;
185 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400186 return old;
187}
188
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400189static void *func_remove(struct tracepoint_func **funcs,
190 struct tracepoint_func *tp_func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400191{
192 int nr_probes = 0, nr_del = 0, i;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400193 struct tracepoint_func *old, *new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400194
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400195 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400196
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200197 if (!old)
Lai Jiangshan19dba332008-10-28 10:51:49 +0800198 return ERR_PTR(-ENOENT);
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200199
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400200 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400201 /* (N -> M), (N > 1, M >= 0) probes */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400202 if (tp_func->func) {
Sahara4c69e6e2013-04-15 11:13:15 +0900203 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400204 if (old[nr_probes].func == tp_func->func &&
205 old[nr_probes].data == tp_func->data)
Sahara4c69e6e2013-04-15 11:13:15 +0900206 nr_del++;
207 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400208 }
209
Sahara4c69e6e2013-04-15 11:13:15 +0900210 /*
211 * If probe is NULL, then nr_probes = nr_del = 0, and then the
212 * entire entry will be removed.
213 */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400214 if (nr_probes - nr_del == 0) {
215 /* N -> 0, (N > 1) */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400216 *funcs = NULL;
217 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400218 return old;
219 } else {
220 int j = 0;
221 /* N -> M, (N > 1, M > 0) */
222 /* + 1 for NULL */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800223 new = allocate_probes(nr_probes - nr_del + 1);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400224 if (new == NULL)
225 return ERR_PTR(-ENOMEM);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400226 for (i = 0; old[i].func; i++)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400227 if (old[i].func != tp_func->func
228 || old[i].data != tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400229 new[j++] = old[i];
Steven Rostedt38516ab2010-04-20 17:04:50 -0400230 new[nr_probes - nr_del].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400231 *funcs = new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400232 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400233 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400234 return old;
235}
236
237/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400238 * Add the probe function to a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400239 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400240static int tracepoint_add_func(struct tracepoint *tp,
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400241 struct tracepoint_func *func, int prio)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400242{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400243 struct tracepoint_func *old, *tp_funcs;
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500244 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400245
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500246 if (tp->regfunc && !static_key_enabled(&tp->key)) {
247 ret = tp->regfunc();
248 if (ret < 0)
249 return ret;
250 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400251
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400252 tp_funcs = rcu_dereference_protected(tp->funcs,
253 lockdep_is_held(&tracepoints_mutex));
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400254 old = func_add(&tp_funcs, func, prio);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400255 if (IS_ERR(old)) {
Mathieu Desnoyersd66a2702018-03-15 08:44:24 -0400256 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400257 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400258 }
Josh Stone97419872009-08-24 14:43:13 -0700259
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400260 /*
Paul E. McKenney243d1a72017-10-09 11:30:11 -0700261 * rcu_assign_pointer has as smp_store_release() which makes sure
262 * that the new probe callbacks array is consistent before setting
263 * a pointer to it. This array is referenced by __DO_TRACE from
264 * include/linux/tracepoint.h using rcu_dereference_sched().
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400265 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400266 rcu_assign_pointer(tp->funcs, tp_funcs);
267 if (!static_key_enabled(&tp->key))
268 static_key_slow_inc(&tp->key);
Mathieu Desnoyers8058bd02014-05-08 07:47:49 -0400269 release_probes(old);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400270 return 0;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400271}
272
273/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400274 * Remove a probe function from a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400275 * Note: only waiting an RCU period after setting elem->call to the empty
276 * function insures that the original callback is not used anymore. This insured
277 * by preempt_disable around the call site.
278 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400279static int tracepoint_remove_func(struct tracepoint *tp,
280 struct tracepoint_func *func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400281{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400282 struct tracepoint_func *old, *tp_funcs;
Josh Stone97419872009-08-24 14:43:13 -0700283
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400284 tp_funcs = rcu_dereference_protected(tp->funcs,
285 lockdep_is_held(&tracepoints_mutex));
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400286 old = func_remove(&tp_funcs, func);
287 if (IS_ERR(old)) {
Mathieu Desnoyersd66a2702018-03-15 08:44:24 -0400288 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400289 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400290 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400291
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400292 if (!tp_funcs) {
293 /* Removed last function */
294 if (tp->unregfunc && static_key_enabled(&tp->key))
295 tp->unregfunc();
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400296
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400297 if (static_key_enabled(&tp->key))
298 static_key_slow_dec(&tp->key);
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800299 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400300 rcu_assign_pointer(tp->funcs, tp_funcs);
Mathieu Desnoyers8058bd02014-05-08 07:47:49 -0400301 release_probes(old);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400302 return 0;
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800303}
304
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400305/**
Lee, Chun-Yif39e2392017-06-16 16:26:43 +0800306 * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400307 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400308 * @probe: probe handler
Fabian Frederickcac92ba2014-06-04 16:11:23 -0700309 * @data: tracepoint data
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400310 * @prio: priority of this function over other registered functions
311 *
312 * Returns 0 if ok, error value on error.
313 * Note: if @tp is within a module, the caller is responsible for
314 * unregistering the probe before the module is gone. This can be
315 * performed either with a tracepoint module going notifier, or from
316 * within module exit functions.
317 */
318int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
319 void *data, int prio)
320{
321 struct tracepoint_func tp_func;
322 int ret;
323
324 mutex_lock(&tracepoints_mutex);
325 tp_func.func = probe;
326 tp_func.data = data;
327 tp_func.prio = prio;
328 ret = tracepoint_add_func(tp, &tp_func, prio);
329 mutex_unlock(&tracepoints_mutex);
330 return ret;
331}
332EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
333
334/**
335 * tracepoint_probe_register - Connect a probe to a tracepoint
336 * @tp: tracepoint
337 * @probe: probe handler
338 * @data: tracepoint data
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400339 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400340 * Returns 0 if ok, error value on error.
341 * Note: if @tp is within a module, the caller is responsible for
342 * unregistering the probe before the module is gone. This can be
343 * performed either with a tracepoint module going notifier, or from
344 * within module exit functions.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400345 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400346int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400347{
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400348 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400349}
350EXPORT_SYMBOL_GPL(tracepoint_probe_register);
351
352/**
353 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400354 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400355 * @probe: probe function pointer
Fabian Frederickcac92ba2014-06-04 16:11:23 -0700356 * @data: tracepoint data
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400357 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400358 * Returns 0 if ok, error value on error.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400359 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400360int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400361{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400362 struct tracepoint_func tp_func;
363 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400364
365 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400366 tp_func.func = probe;
367 tp_func.data = data;
368 ret = tracepoint_remove_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400369 mutex_unlock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400370 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400371}
372EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
373
Ingo Molnar227a8372008-11-16 09:50:34 +0100374#ifdef CONFIG_MODULES
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500375bool trace_module_has_bad_taint(struct module *mod)
376{
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +1030377 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
378 (1 << TAINT_UNSIGNED_MODULE));
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500379}
380
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400381static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
382
383/**
384 * register_tracepoint_notifier - register tracepoint coming/going notifier
385 * @nb: notifier block
386 *
387 * Notifiers registered with this function are called on module
388 * coming/going with the tracepoint_module_list_mutex held.
389 * The notifier block callback should expect a "struct tp_module" data
390 * pointer.
391 */
392int register_tracepoint_module_notifier(struct notifier_block *nb)
393{
394 struct tp_module *tp_mod;
395 int ret;
396
397 mutex_lock(&tracepoint_module_list_mutex);
398 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
399 if (ret)
400 goto end;
401 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
402 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
403end:
404 mutex_unlock(&tracepoint_module_list_mutex);
405 return ret;
406}
407EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
408
409/**
410 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
411 * @nb: notifier block
412 *
413 * The notifier block callback should expect a "struct tp_module" data
414 * pointer.
415 */
416int unregister_tracepoint_module_notifier(struct notifier_block *nb)
417{
418 struct tp_module *tp_mod;
419 int ret;
420
421 mutex_lock(&tracepoint_module_list_mutex);
422 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
423 if (ret)
424 goto end;
425 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
426 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
427end:
428 mutex_unlock(&tracepoint_module_list_mutex);
429 return ret;
430
431}
432EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
433
434/*
435 * Ensure the tracer unregistered the module's probes before the module
436 * teardown is performed. Prevents leaks of probe and data pointers.
437 */
438static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
439 struct tracepoint * const *end)
440{
441 struct tracepoint * const *iter;
442
443 if (!begin)
444 return;
445 for (iter = begin; iter < end; iter++)
446 WARN_ON_ONCE((*iter)->funcs);
447}
448
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400449static int tracepoint_module_coming(struct module *mod)
450{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400451 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400452 int ret = 0;
453
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500454 if (!mod->num_tracepoints)
455 return 0;
456
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400457 /*
Steven Rostedtc10076c2012-01-13 21:40:59 -0500458 * We skip modules that taint the kernel, especially those with different
459 * module headers (for forced load), to make sure we don't cause a crash.
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +1030460 * Staging, out-of-tree, and unsigned GPL modules are fine.
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400461 */
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500462 if (trace_module_has_bad_taint(mod))
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400463 return 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400464 mutex_lock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400465 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
466 if (!tp_mod) {
467 ret = -ENOMEM;
468 goto end;
469 }
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400470 tp_mod->mod = mod;
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400471 list_add_tail(&tp_mod->list, &tracepoint_module_list);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400472 blocking_notifier_call_chain(&tracepoint_notify_list,
473 MODULE_STATE_COMING, tp_mod);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400474end:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400475 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400476 return ret;
477}
478
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400479static void tracepoint_module_going(struct module *mod)
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400480{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400481 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400482
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500483 if (!mod->num_tracepoints)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400484 return;
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500485
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400486 mutex_lock(&tracepoint_module_list_mutex);
487 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400488 if (tp_mod->mod == mod) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400489 blocking_notifier_call_chain(&tracepoint_notify_list,
490 MODULE_STATE_GOING, tp_mod);
491 list_del(&tp_mod->list);
492 kfree(tp_mod);
493 /*
494 * Called the going notifier before checking for
495 * quiescence.
496 */
497 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
498 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400499 break;
500 }
501 }
502 /*
503 * In the case of modules that were tainted at "coming", we'll simply
504 * walk through the list without finding it. We cannot use the "tainted"
505 * flag on "going", in case a module taints the kernel only after being
506 * loaded.
507 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400508 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400509}
Ingo Molnar227a8372008-11-16 09:50:34 +0100510
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400511static int tracepoint_module_notify(struct notifier_block *self,
512 unsigned long val, void *data)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500513{
514 struct module *mod = data;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400515 int ret = 0;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500516
517 switch (val) {
518 case MODULE_STATE_COMING:
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400519 ret = tracepoint_module_coming(mod);
520 break;
521 case MODULE_STATE_LIVE:
522 break;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500523 case MODULE_STATE_GOING:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400524 tracepoint_module_going(mod);
525 break;
526 case MODULE_STATE_UNFORMED:
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500527 break;
528 }
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400529 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500530}
531
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400532static struct notifier_block tracepoint_module_nb = {
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500533 .notifier_call = tracepoint_module_notify,
534 .priority = 0,
535};
536
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400537static __init int init_tracepoints(void)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500538{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400539 int ret;
540
541 ret = register_module_notifier(&tracepoint_module_nb);
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400542 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700543 pr_warn("Failed to register tracepoint module enter notifier\n");
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400544
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400545 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500546}
547__initcall(init_tracepoints);
Ingo Molnar227a8372008-11-16 09:50:34 +0100548#endif /* CONFIG_MODULES */
Jason Barona871bd32009-08-10 16:52:31 -0400549
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400550static void for_each_tracepoint_range(struct tracepoint * const *begin,
551 struct tracepoint * const *end,
552 void (*fct)(struct tracepoint *tp, void *priv),
553 void *priv)
554{
555 struct tracepoint * const *iter;
556
557 if (!begin)
558 return;
559 for (iter = begin; iter < end; iter++)
560 fct(*iter, priv);
561}
562
563/**
564 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
565 * @fct: callback
566 * @priv: private data
567 */
568void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
569 void *priv)
570{
571 for_each_tracepoint_range(__start___tracepoints_ptrs,
572 __stop___tracepoints_ptrs, fct, priv);
573}
574EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
575
Josh Stone3d27d8cb2009-08-24 14:43:12 -0700576#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
Ingo Molnar60d970c2009-08-13 23:37:26 +0200577
Josh Stone97419872009-08-24 14:43:13 -0700578/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
Jason Barona871bd32009-08-10 16:52:31 -0400579static int sys_tracepoint_refcount;
580
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500581int syscall_regfunc(void)
Jason Barona871bd32009-08-10 16:52:31 -0400582{
Oleg Nesterov8063e412014-04-13 20:59:18 +0200583 struct task_struct *p, *t;
Jason Barona871bd32009-08-10 16:52:31 -0400584
Jason Barona871bd32009-08-10 16:52:31 -0400585 if (!sys_tracepoint_refcount) {
Oleg Nesterov8063e412014-04-13 20:59:18 +0200586 read_lock(&tasklist_lock);
587 for_each_process_thread(p, t) {
Oleg Nesterovea73c792014-04-13 20:59:38 +0200588 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Oleg Nesterov8063e412014-04-13 20:59:18 +0200589 }
590 read_unlock(&tasklist_lock);
Jason Barona871bd32009-08-10 16:52:31 -0400591 }
592 sys_tracepoint_refcount++;
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500593
594 return 0;
Jason Barona871bd32009-08-10 16:52:31 -0400595}
596
597void syscall_unregfunc(void)
598{
Oleg Nesterov8063e412014-04-13 20:59:18 +0200599 struct task_struct *p, *t;
Jason Barona871bd32009-08-10 16:52:31 -0400600
Jason Barona871bd32009-08-10 16:52:31 -0400601 sys_tracepoint_refcount--;
602 if (!sys_tracepoint_refcount) {
Oleg Nesterov8063e412014-04-13 20:59:18 +0200603 read_lock(&tasklist_lock);
604 for_each_process_thread(p, t) {
Josh Stone66700002009-08-24 14:43:11 -0700605 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Oleg Nesterov8063e412014-04-13 20:59:18 +0200606 }
607 read_unlock(&tasklist_lock);
Jason Barona871bd32009-08-10 16:52:31 -0400608 }
Jason Barona871bd32009-08-10 16:52:31 -0400609}
Ingo Molnar60d970c2009-08-13 23:37:26 +0200610#endif