blob: 955148d91b7406a59428f29eef238359844d42c9 [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
56/*
57 * Note about RCU :
Anand Gadiyarfd589a82009-07-16 17:13:03 +020058 * It is used to delay the free of multiple probes array until a quiescent
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040059 * state is reached.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040060 */
Lai Jiangshan19dba332008-10-28 10:51:49 +080061struct tp_probes {
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040062 struct rcu_head rcu;
Steven Rostedt38516ab2010-04-20 17:04:50 -040063 struct tracepoint_func probes[0];
Lai Jiangshan19dba332008-10-28 10:51:49 +080064};
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040065
Lai Jiangshan19dba332008-10-28 10:51:49 +080066static inline void *allocate_probes(int count)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040067{
Steven Rostedt38516ab2010-04-20 17:04:50 -040068 struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
Lai Jiangshan19dba332008-10-28 10:51:49 +080069 + sizeof(struct tp_probes), GFP_KERNEL);
70 return p == NULL ? NULL : p->probes;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040071}
72
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -070073static void srcu_free_old_probes(struct rcu_head *head)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040074{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040075 kfree(container_of(head, struct tp_probes, rcu));
Lai Jiangshan19dba332008-10-28 10:51:49 +080076}
77
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -070078static void rcu_free_old_probes(struct rcu_head *head)
79{
80 call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
81}
82
Steven Rostedt38516ab2010-04-20 17:04:50 -040083static inline void release_probes(struct tracepoint_func *old)
Lai Jiangshan19dba332008-10-28 10:51:49 +080084{
85 if (old) {
86 struct tp_probes *tp_probes = container_of(old,
87 struct tp_probes, probes[0]);
Joel Fernandes (Google)e6753f22018-07-30 15:24:22 -070088 /*
89 * Tracepoint probes are protected by both sched RCU and SRCU,
90 * by calling the SRCU callback in the sched RCU callback we
91 * cover both cases. So let us chain the SRCU and sched RCU
92 * callbacks to wait for both grace periods.
93 */
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -040094 call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
Lai Jiangshan19dba332008-10-28 10:51:49 +080095 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040096}
97
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -040098static void debug_print_probes(struct tracepoint_func *funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -040099{
100 int i;
101
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400102 if (!tracepoint_debug || !funcs)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400103 return;
104
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400105 for (i = 0; funcs[i].func; i++)
106 printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400107}
108
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400109static struct tracepoint_func *
110func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
111 int prio)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400112{
Steven Rostedt38516ab2010-04-20 17:04:50 -0400113 struct tracepoint_func *old, *new;
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400114 int nr_probes = 0;
115 int pos = -1;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400116
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400117 if (WARN_ON(!tp_func->func))
Sahara4c69e6e2013-04-15 11:13:15 +0900118 return ERR_PTR(-EINVAL);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400119
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400120 debug_print_probes(*funcs);
121 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400122 if (old) {
123 /* (N -> N+1), (N != 0, 1) probes */
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400124 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
125 /* Insert before probes of lower priority */
126 if (pos < 0 && old[nr_probes].prio < prio)
127 pos = nr_probes;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400128 if (old[nr_probes].func == tp_func->func &&
129 old[nr_probes].data == tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400130 return ERR_PTR(-EEXIST);
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400131 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400132 }
133 /* + 2 : one for new probe, one for NULL func */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800134 new = allocate_probes(nr_probes + 2);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400135 if (new == NULL)
136 return ERR_PTR(-ENOMEM);
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400137 if (old) {
138 if (pos < 0) {
139 pos = nr_probes;
140 memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
141 } else {
142 /* Copy higher priority probes ahead of the new probe */
143 memcpy(new, old, pos * sizeof(struct tracepoint_func));
144 /* Copy the rest after it. */
145 memcpy(new + pos + 1, old + pos,
146 (nr_probes - pos) * sizeof(struct tracepoint_func));
147 }
148 } else
149 pos = 0;
150 new[pos] = *tp_func;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400151 new[nr_probes + 1].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400152 *funcs = new;
153 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400154 return old;
155}
156
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400157static void *func_remove(struct tracepoint_func **funcs,
158 struct tracepoint_func *tp_func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400159{
160 int nr_probes = 0, nr_del = 0, i;
Steven Rostedt38516ab2010-04-20 17:04:50 -0400161 struct tracepoint_func *old, *new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400162
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400163 old = *funcs;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400164
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200165 if (!old)
Lai Jiangshan19dba332008-10-28 10:51:49 +0800166 return ERR_PTR(-ENOENT);
Frederic Weisbeckerf66af452008-10-22 19:14:55 +0200167
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400168 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400169 /* (N -> M), (N > 1, M >= 0) probes */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400170 if (tp_func->func) {
Sahara4c69e6e2013-04-15 11:13:15 +0900171 for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400172 if (old[nr_probes].func == tp_func->func &&
173 old[nr_probes].data == tp_func->data)
Sahara4c69e6e2013-04-15 11:13:15 +0900174 nr_del++;
175 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400176 }
177
Sahara4c69e6e2013-04-15 11:13:15 +0900178 /*
179 * If probe is NULL, then nr_probes = nr_del = 0, and then the
180 * entire entry will be removed.
181 */
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400182 if (nr_probes - nr_del == 0) {
183 /* N -> 0, (N > 1) */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400184 *funcs = NULL;
185 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400186 return old;
187 } else {
188 int j = 0;
189 /* N -> M, (N > 1, M > 0) */
190 /* + 1 for NULL */
Lai Jiangshan19dba332008-10-28 10:51:49 +0800191 new = allocate_probes(nr_probes - nr_del + 1);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400192 if (new == NULL)
193 return ERR_PTR(-ENOMEM);
Steven Rostedt38516ab2010-04-20 17:04:50 -0400194 for (i = 0; old[i].func; i++)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400195 if (old[i].func != tp_func->func
196 || old[i].data != tp_func->data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400197 new[j++] = old[i];
Steven Rostedt38516ab2010-04-20 17:04:50 -0400198 new[nr_probes - nr_del].func = NULL;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400199 *funcs = new;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400200 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400201 debug_print_probes(*funcs);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400202 return old;
203}
204
205/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400206 * Add the probe function to a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400207 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400208static int tracepoint_add_func(struct tracepoint *tp,
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400209 struct tracepoint_func *func, int prio)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400210{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400211 struct tracepoint_func *old, *tp_funcs;
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500212 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400213
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500214 if (tp->regfunc && !static_key_enabled(&tp->key)) {
215 ret = tp->regfunc();
216 if (ret < 0)
217 return ret;
218 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400219
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400220 tp_funcs = rcu_dereference_protected(tp->funcs,
221 lockdep_is_held(&tracepoints_mutex));
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400222 old = func_add(&tp_funcs, func, prio);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400223 if (IS_ERR(old)) {
Mathieu Desnoyersd66a2702018-03-15 08:44:24 -0400224 WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400225 return PTR_ERR(old);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400226 }
Josh Stone97419872009-08-24 14:43:13 -0700227
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400228 /*
Paul E. McKenney243d1a72017-10-09 11:30:11 -0700229 * rcu_assign_pointer has as smp_store_release() which makes sure
230 * that the new probe callbacks array is consistent before setting
231 * a pointer to it. This array is referenced by __DO_TRACE from
232 * include/linux/tracepoint.h using rcu_dereference_sched().
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400233 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400234 rcu_assign_pointer(tp->funcs, tp_funcs);
235 if (!static_key_enabled(&tp->key))
236 static_key_slow_inc(&tp->key);
Mathieu Desnoyers8058bd02014-05-08 07:47:49 -0400237 release_probes(old);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400238 return 0;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400239}
240
241/*
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400242 * Remove a probe function from a tracepoint.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400243 * Note: only waiting an RCU period after setting elem->call to the empty
244 * function insures that the original callback is not used anymore. This insured
245 * by preempt_disable around the call site.
246 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400247static int tracepoint_remove_func(struct tracepoint *tp,
248 struct tracepoint_func *func)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400249{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400250 struct tracepoint_func *old, *tp_funcs;
Josh Stone97419872009-08-24 14:43:13 -0700251
Mathieu Desnoyersb725dfe2014-04-09 09:24:43 -0400252 tp_funcs = rcu_dereference_protected(tp->funcs,
253 lockdep_is_held(&tracepoints_mutex));
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400254 old = func_remove(&tp_funcs, func);
255 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 }
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400259
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400260 if (!tp_funcs) {
261 /* Removed last function */
262 if (tp->unregfunc && static_key_enabled(&tp->key))
263 tp->unregfunc();
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400264
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400265 if (static_key_enabled(&tp->key))
266 static_key_slow_dec(&tp->key);
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800267 }
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400268 rcu_assign_pointer(tp->funcs, tp_funcs);
Mathieu Desnoyers8058bd02014-05-08 07:47:49 -0400269 release_probes(old);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400270 return 0;
Lai Jiangshan127cafb2008-10-28 10:51:53 +0800271}
272
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400273/**
Lee, Chun-Yif39e2392017-06-16 16:26:43 +0800274 * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400275 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400276 * @probe: probe handler
Fabian Frederickcac92ba2014-06-04 16:11:23 -0700277 * @data: tracepoint data
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400278 * @prio: priority of this function over other registered functions
279 *
280 * Returns 0 if ok, error value on error.
281 * Note: if @tp is within a module, the caller is responsible for
282 * unregistering the probe before the module is gone. This can be
283 * performed either with a tracepoint module going notifier, or from
284 * within module exit functions.
285 */
286int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
287 void *data, int prio)
288{
289 struct tracepoint_func tp_func;
290 int ret;
291
292 mutex_lock(&tracepoints_mutex);
293 tp_func.func = probe;
294 tp_func.data = data;
295 tp_func.prio = prio;
296 ret = tracepoint_add_func(tp, &tp_func, prio);
297 mutex_unlock(&tracepoints_mutex);
298 return ret;
299}
300EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
301
302/**
303 * tracepoint_probe_register - Connect a probe to a tracepoint
304 * @tp: tracepoint
305 * @probe: probe handler
306 * @data: tracepoint data
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400307 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400308 * Returns 0 if ok, error value on error.
309 * Note: if @tp is within a module, the caller is responsible for
310 * unregistering the probe before the module is gone. This can be
311 * performed either with a tracepoint module going notifier, or from
312 * within module exit functions.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400313 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400314int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400315{
Steven Rostedt (Red Hat)7904b5c2015-09-22 17:13:19 -0400316 return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400317}
318EXPORT_SYMBOL_GPL(tracepoint_probe_register);
319
320/**
321 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400322 * @tp: tracepoint
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400323 * @probe: probe function pointer
Fabian Frederickcac92ba2014-06-04 16:11:23 -0700324 * @data: tracepoint data
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400325 *
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400326 * Returns 0 if ok, error value on error.
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400327 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400328int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400329{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400330 struct tracepoint_func tp_func;
331 int ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400332
333 mutex_lock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400334 tp_func.func = probe;
335 tp_func.data = data;
336 ret = tracepoint_remove_func(tp, &tp_func);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400337 mutex_unlock(&tracepoints_mutex);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400338 return ret;
Mathieu Desnoyers97e1c182008-07-18 12:16:16 -0400339}
340EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
341
Ingo Molnar227a8372008-11-16 09:50:34 +0100342#ifdef CONFIG_MODULES
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500343bool trace_module_has_bad_taint(struct module *mod)
344{
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +1030345 return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
346 (1 << TAINT_UNSIGNED_MODULE));
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500347}
348
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400349static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
350
351/**
352 * register_tracepoint_notifier - register tracepoint coming/going notifier
353 * @nb: notifier block
354 *
355 * Notifiers registered with this function are called on module
356 * coming/going with the tracepoint_module_list_mutex held.
357 * The notifier block callback should expect a "struct tp_module" data
358 * pointer.
359 */
360int register_tracepoint_module_notifier(struct notifier_block *nb)
361{
362 struct tp_module *tp_mod;
363 int ret;
364
365 mutex_lock(&tracepoint_module_list_mutex);
366 ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
367 if (ret)
368 goto end;
369 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
370 (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
371end:
372 mutex_unlock(&tracepoint_module_list_mutex);
373 return ret;
374}
375EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
376
377/**
378 * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
379 * @nb: notifier block
380 *
381 * The notifier block callback should expect a "struct tp_module" data
382 * pointer.
383 */
384int unregister_tracepoint_module_notifier(struct notifier_block *nb)
385{
386 struct tp_module *tp_mod;
387 int ret;
388
389 mutex_lock(&tracepoint_module_list_mutex);
390 ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
391 if (ret)
392 goto end;
393 list_for_each_entry(tp_mod, &tracepoint_module_list, list)
394 (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
395end:
396 mutex_unlock(&tracepoint_module_list_mutex);
397 return ret;
398
399}
400EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
401
402/*
403 * Ensure the tracer unregistered the module's probes before the module
404 * teardown is performed. Prevents leaks of probe and data pointers.
405 */
406static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
407 struct tracepoint * const *end)
408{
409 struct tracepoint * const *iter;
410
411 if (!begin)
412 return;
413 for (iter = begin; iter < end; iter++)
414 WARN_ON_ONCE((*iter)->funcs);
415}
416
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400417static int tracepoint_module_coming(struct module *mod)
418{
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400419 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400420 int ret = 0;
421
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500422 if (!mod->num_tracepoints)
423 return 0;
424
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400425 /*
Steven Rostedtc10076c2012-01-13 21:40:59 -0500426 * We skip modules that taint the kernel, especially those with different
427 * module headers (for forced load), to make sure we don't cause a crash.
Mathieu Desnoyers66cc69e2014-03-13 12:11:30 +1030428 * Staging, out-of-tree, and unsigned GPL modules are fine.
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400429 */
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -0500430 if (trace_module_has_bad_taint(mod))
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400431 return 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400432 mutex_lock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400433 tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
434 if (!tp_mod) {
435 ret = -ENOMEM;
436 goto end;
437 }
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400438 tp_mod->mod = mod;
Mathieu Desnoyers0dea6d522014-03-21 01:19:01 -0400439 list_add_tail(&tp_mod->list, &tracepoint_module_list);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400440 blocking_notifier_call_chain(&tracepoint_notify_list,
441 MODULE_STATE_COMING, tp_mod);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400442end:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400443 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400444 return ret;
445}
446
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400447static void tracepoint_module_going(struct module *mod)
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400448{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400449 struct tp_module *tp_mod;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400450
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500451 if (!mod->num_tracepoints)
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400452 return;
Steven Rostedt (Red Hat)7dec9352014-02-26 10:54:36 -0500453
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400454 mutex_lock(&tracepoint_module_list_mutex);
455 list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400456 if (tp_mod->mod == mod) {
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400457 blocking_notifier_call_chain(&tracepoint_notify_list,
458 MODULE_STATE_GOING, tp_mod);
459 list_del(&tp_mod->list);
460 kfree(tp_mod);
461 /*
462 * Called the going notifier before checking for
463 * quiescence.
464 */
465 tp_module_going_check_quiescent(mod->tracepoints_ptrs,
466 mod->tracepoints_ptrs + mod->num_tracepoints);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400467 break;
468 }
469 }
470 /*
471 * In the case of modules that were tainted at "coming", we'll simply
472 * walk through the list without finding it. We cannot use the "tainted"
473 * flag on "going", in case a module taints the kernel only after being
474 * loaded.
475 */
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400476 mutex_unlock(&tracepoint_module_list_mutex);
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400477}
Ingo Molnar227a8372008-11-16 09:50:34 +0100478
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400479static int tracepoint_module_notify(struct notifier_block *self,
480 unsigned long val, void *data)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500481{
482 struct module *mod = data;
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400483 int ret = 0;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500484
485 switch (val) {
486 case MODULE_STATE_COMING:
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400487 ret = tracepoint_module_coming(mod);
488 break;
489 case MODULE_STATE_LIVE:
490 break;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500491 case MODULE_STATE_GOING:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400492 tracepoint_module_going(mod);
493 break;
494 case MODULE_STATE_UNFORMED:
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500495 break;
496 }
Mathieu Desnoyersb75ef8b2011-08-10 15:18:39 -0400497 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500498}
499
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400500static struct notifier_block tracepoint_module_nb = {
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500501 .notifier_call = tracepoint_module_notify,
502 .priority = 0,
503};
504
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400505static __init int init_tracepoints(void)
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500506{
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400507 int ret;
508
509 ret = register_module_notifier(&tracepoint_module_nb);
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400510 if (ret)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700511 pr_warn("Failed to register tracepoint module enter notifier\n");
Steven Rostedt (Red Hat)eb7d0352014-04-08 20:09:40 -0400512
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400513 return ret;
Mathieu Desnoyers32f85742008-11-14 17:47:46 -0500514}
515__initcall(init_tracepoints);
Ingo Molnar227a8372008-11-16 09:50:34 +0100516#endif /* CONFIG_MODULES */
Jason Barona871bd32009-08-10 16:52:31 -0400517
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400518static void for_each_tracepoint_range(struct tracepoint * const *begin,
519 struct tracepoint * const *end,
520 void (*fct)(struct tracepoint *tp, void *priv),
521 void *priv)
522{
523 struct tracepoint * const *iter;
524
525 if (!begin)
526 return;
527 for (iter = begin; iter < end; iter++)
528 fct(*iter, priv);
529}
530
531/**
532 * for_each_kernel_tracepoint - iteration on all kernel tracepoints
533 * @fct: callback
534 * @priv: private data
535 */
536void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
537 void *priv)
538{
539 for_each_tracepoint_range(__start___tracepoints_ptrs,
540 __stop___tracepoints_ptrs, fct, priv);
541}
542EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
543
Josh Stone3d27d8cb2009-08-24 14:43:12 -0700544#ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
Ingo Molnar60d970c2009-08-13 23:37:26 +0200545
Josh Stone97419872009-08-24 14:43:13 -0700546/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
Jason Barona871bd32009-08-10 16:52:31 -0400547static int sys_tracepoint_refcount;
548
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500549int syscall_regfunc(void)
Jason Barona871bd32009-08-10 16:52:31 -0400550{
Oleg Nesterov8063e412014-04-13 20:59:18 +0200551 struct task_struct *p, *t;
Jason Barona871bd32009-08-10 16:52:31 -0400552
Jason Barona871bd32009-08-10 16:52:31 -0400553 if (!sys_tracepoint_refcount) {
Oleg Nesterov8063e412014-04-13 20:59:18 +0200554 read_lock(&tasklist_lock);
555 for_each_process_thread(p, t) {
Oleg Nesterovea73c792014-04-13 20:59:38 +0200556 set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Oleg Nesterov8063e412014-04-13 20:59:18 +0200557 }
558 read_unlock(&tasklist_lock);
Jason Barona871bd32009-08-10 16:52:31 -0400559 }
560 sys_tracepoint_refcount++;
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -0500561
562 return 0;
Jason Barona871bd32009-08-10 16:52:31 -0400563}
564
565void syscall_unregfunc(void)
566{
Oleg Nesterov8063e412014-04-13 20:59:18 +0200567 struct task_struct *p, *t;
Jason Barona871bd32009-08-10 16:52:31 -0400568
Jason Barona871bd32009-08-10 16:52:31 -0400569 sys_tracepoint_refcount--;
570 if (!sys_tracepoint_refcount) {
Oleg Nesterov8063e412014-04-13 20:59:18 +0200571 read_lock(&tasklist_lock);
572 for_each_process_thread(p, t) {
Josh Stone66700002009-08-24 14:43:11 -0700573 clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
Oleg Nesterov8063e412014-04-13 20:59:18 +0200574 }
575 read_unlock(&tasklist_lock);
Jason Barona871bd32009-08-10 16:52:31 -0400576 }
Jason Barona871bd32009-08-10 16:52:31 -0400577}
Ingo Molnar60d970c2009-08-13 23:37:26 +0200578#endif