xref: /linux/kernel/tracepoint.c (revision 23b0f90ba871f096474e1c27c3d14f455189d2d9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2008-2014 Mathieu Desnoyers
4  */
5 #include <linux/module.h>
6 #include <linux/mutex.h>
7 #include <linux/types.h>
8 #include <linux/jhash.h>
9 #include <linux/list.h>
10 #include <linux/rcupdate.h>
11 #include <linux/tracepoint.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
15 #include <linux/sched/task.h>
16 #include <linux/static_key.h>
17 
18 enum tp_func_state {
19 	TP_FUNC_0,
20 	TP_FUNC_1,
21 	TP_FUNC_2,
22 	TP_FUNC_N,
23 };
24 
25 extern tracepoint_ptr_t __start___tracepoints_ptrs[];
26 extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
27 
28 enum tp_transition_sync {
29 	TP_TRANSITION_SYNC_1_0_1,
30 	TP_TRANSITION_SYNC_N_2_1,
31 
32 	_NR_TP_TRANSITION_SYNC,
33 };
34 
35 struct tp_transition_snapshot {
36 	unsigned long rcu;
37 	unsigned long srcu_gp;
38 	bool ongoing;
39 };
40 
41 DEFINE_SRCU_FAST(tracepoint_srcu);
42 EXPORT_SYMBOL_GPL(tracepoint_srcu);
43 
44 /* Protected by tracepoints_mutex */
45 static struct tp_transition_snapshot tp_transition_snapshot[_NR_TP_TRANSITION_SYNC];
46 
47 static void tp_rcu_get_state(enum tp_transition_sync sync)
48 {
49 	struct tp_transition_snapshot *snapshot = &tp_transition_snapshot[sync];
50 
51 	/* Keep the latest get_state snapshot. */
52 	snapshot->rcu = get_state_synchronize_rcu();
53 	snapshot->srcu_gp = start_poll_synchronize_srcu(&tracepoint_srcu);
54 	snapshot->ongoing = true;
55 }
56 
57 static void tp_rcu_cond_sync(enum tp_transition_sync sync)
58 {
59 	struct tp_transition_snapshot *snapshot = &tp_transition_snapshot[sync];
60 
61 	if (!snapshot->ongoing)
62 		return;
63 	cond_synchronize_rcu(snapshot->rcu);
64 	if (!poll_state_synchronize_srcu(&tracepoint_srcu, snapshot->srcu_gp))
65 		synchronize_srcu(&tracepoint_srcu);
66 	snapshot->ongoing = false;
67 }
68 
69 /* Set to 1 to enable tracepoint debug output */
70 static const int tracepoint_debug;
71 
72 #ifdef CONFIG_MODULES
73 /*
74  * Tracepoint module list mutex protects the local module list.
75  */
76 static DEFINE_MUTEX(tracepoint_module_list_mutex);
77 
78 /* Local list of struct tp_module */
79 static LIST_HEAD(tracepoint_module_list);
80 #endif /* CONFIG_MODULES */
81 
82 /*
83  * tracepoints_mutex protects the builtin and module tracepoints.
84  * tracepoints_mutex nests inside tracepoint_module_list_mutex.
85  */
86 static DEFINE_MUTEX(tracepoints_mutex);
87 
88 /*
89  * Note about RCU :
90  * It is used to delay the free of multiple probes array until a quiescent
91  * state is reached.
92  */
93 struct tp_probes {
94 	struct rcu_head rcu;
95 	struct tracepoint_func probes[];
96 };
97 
98 /* Called in removal of a func but failed to allocate a new tp_funcs */
99 static void tp_stub_func(void)
100 {
101 	return;
102 }
103 
104 static inline void *allocate_probes(int count)
105 {
106 	struct tp_probes *p  = kmalloc(struct_size(p, probes, count),
107 				       GFP_KERNEL);
108 	return p == NULL ? NULL : p->probes;
109 }
110 
111 static void rcu_free_old_probes(struct rcu_head *head)
112 {
113 	kfree(container_of(head, struct tp_probes, rcu));
114 }
115 
116 static inline void release_probes(struct tracepoint *tp, struct tracepoint_func *old)
117 {
118 	if (old) {
119 		struct tp_probes *tp_probes = container_of(old,
120 			struct tp_probes, probes[0]);
121 
122 		if (tracepoint_is_faultable(tp)) {
123 			call_rcu_tasks_trace(&tp_probes->rcu,
124 					     rcu_free_old_probes);
125 		} else {
126 			call_srcu(&tracepoint_srcu, &tp_probes->rcu,
127 				  rcu_free_old_probes);
128 		}
129 	}
130 }
131 
132 static void debug_print_probes(struct tracepoint_func *funcs)
133 {
134 	int i;
135 
136 	if (!tracepoint_debug || !funcs)
137 		return;
138 
139 	for (i = 0; funcs[i].func; i++)
140 		printk(KERN_DEBUG "Probe %d : %pSb\n", i, funcs[i].func);
141 }
142 
143 static struct tracepoint_func *
144 func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
145 	 int prio)
146 {
147 	struct tracepoint_func *old, *new;
148 	int iter_probes;	/* Iterate over old probe array. */
149 	int nr_probes = 0;	/* Counter for probes */
150 	int pos = -1;		/* Insertion position into new array */
151 
152 	if (WARN_ON(!tp_func->func))
153 		return ERR_PTR(-EINVAL);
154 
155 	debug_print_probes(*funcs);
156 	old = *funcs;
157 	if (old) {
158 		/* (N -> N+1), (N != 0, 1) probes */
159 		for (iter_probes = 0; old[iter_probes].func; iter_probes++) {
160 			if (old[iter_probes].func == tp_stub_func)
161 				continue;	/* Skip stub functions. */
162 			if (old[iter_probes].func == tp_func->func &&
163 			    old[iter_probes].data == tp_func->data)
164 				return ERR_PTR(-EEXIST);
165 			nr_probes++;
166 		}
167 	}
168 	/* + 2 : one for new probe, one for NULL func */
169 	new = allocate_probes(nr_probes + 2);
170 	if (new == NULL)
171 		return ERR_PTR(-ENOMEM);
172 	if (old) {
173 		nr_probes = 0;
174 		for (iter_probes = 0; old[iter_probes].func; iter_probes++) {
175 			if (old[iter_probes].func == tp_stub_func)
176 				continue;
177 			/* Insert before probes of lower priority */
178 			if (pos < 0 && old[iter_probes].prio < prio)
179 				pos = nr_probes++;
180 			new[nr_probes++] = old[iter_probes];
181 		}
182 		if (pos < 0)
183 			pos = nr_probes++;
184 		/* nr_probes now points to the end of the new array */
185 	} else {
186 		pos = 0;
187 		nr_probes = 1; /* must point at end of array */
188 	}
189 	new[pos] = *tp_func;
190 	new[nr_probes].func = NULL;
191 	*funcs = new;
192 	debug_print_probes(*funcs);
193 	return old;
194 }
195 
196 static void *func_remove(struct tracepoint_func **funcs,
197 		struct tracepoint_func *tp_func)
198 {
199 	int nr_probes = 0, nr_del = 0, i;
200 	struct tracepoint_func *old, *new;
201 
202 	old = *funcs;
203 
204 	if (!old)
205 		return ERR_PTR(-ENOENT);
206 
207 	debug_print_probes(*funcs);
208 	/* (N -> M), (N > 1, M >= 0) probes */
209 	if (tp_func->func) {
210 		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
211 			if ((old[nr_probes].func == tp_func->func &&
212 			     old[nr_probes].data == tp_func->data) ||
213 			    old[nr_probes].func == tp_stub_func)
214 				nr_del++;
215 		}
216 	}
217 
218 	/*
219 	 * If probe is NULL, then nr_probes = nr_del = 0, and then the
220 	 * entire entry will be removed.
221 	 */
222 	if (nr_probes - nr_del == 0) {
223 		/* N -> 0, (N > 1) */
224 		*funcs = NULL;
225 		debug_print_probes(*funcs);
226 		return old;
227 	} else {
228 		int j = 0;
229 		/* N -> M, (N > 1, M > 0) */
230 		/* + 1 for NULL */
231 		new = allocate_probes(nr_probes - nr_del + 1);
232 		if (new) {
233 			for (i = 0; old[i].func; i++) {
234 				if ((old[i].func != tp_func->func ||
235 				     old[i].data != tp_func->data) &&
236 				    old[i].func != tp_stub_func)
237 					new[j++] = old[i];
238 			}
239 			new[nr_probes - nr_del].func = NULL;
240 			*funcs = new;
241 		} else {
242 			/*
243 			 * Failed to allocate, replace the old function
244 			 * with calls to tp_stub_func.
245 			 */
246 			for (i = 0; old[i].func; i++) {
247 				if (old[i].func == tp_func->func &&
248 				    old[i].data == tp_func->data)
249 					WRITE_ONCE(old[i].func, tp_stub_func);
250 			}
251 			*funcs = old;
252 		}
253 	}
254 	debug_print_probes(*funcs);
255 	return old;
256 }
257 
258 /*
259  * Count the number of functions (enum tp_func_state) in a tp_funcs array.
260  */
261 static enum tp_func_state nr_func_state(const struct tracepoint_func *tp_funcs)
262 {
263 	if (!tp_funcs)
264 		return TP_FUNC_0;
265 	if (!tp_funcs[1].func)
266 		return TP_FUNC_1;
267 	if (!tp_funcs[2].func)
268 		return TP_FUNC_2;
269 	return TP_FUNC_N;	/* 3 or more */
270 }
271 
272 static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs)
273 {
274 	void *func = tp->iterator;
275 
276 	/* Synthetic events do not have static call sites */
277 	if (!tp->static_call_key)
278 		return;
279 	if (nr_func_state(tp_funcs) == TP_FUNC_1)
280 		func = tp_funcs[0].func;
281 	__static_call_update(tp->static_call_key, tp->static_call_tramp, func);
282 }
283 
284 /*
285  * Add the probe function to a tracepoint.
286  */
287 static int tracepoint_add_func(struct tracepoint *tp,
288 			       struct tracepoint_func *func, int prio,
289 			       bool warn)
290 {
291 	struct tracepoint_func *old, *tp_funcs;
292 	int ret;
293 
294 	if (tp->ext && tp->ext->regfunc && !static_key_enabled(&tp->key)) {
295 		ret = tp->ext->regfunc();
296 		if (ret < 0)
297 			return ret;
298 	}
299 
300 	tp_funcs = rcu_dereference_protected(tp->funcs,
301 			lockdep_is_held(&tracepoints_mutex));
302 	old = func_add(&tp_funcs, func, prio);
303 	if (IS_ERR(old)) {
304 		WARN_ON_ONCE(warn && PTR_ERR(old) != -ENOMEM);
305 		return PTR_ERR(old);
306 	}
307 
308 	/*
309 	 * rcu_assign_pointer has as smp_store_release() which makes sure
310 	 * that the new probe callbacks array is consistent before setting
311 	 * a pointer to it.  This array is referenced by __DO_TRACE from
312 	 * include/linux/tracepoint.h using rcu_dereference_sched().
313 	 */
314 	switch (nr_func_state(tp_funcs)) {
315 	case TP_FUNC_1:		/* 0->1 */
316 		/*
317 		 * Make sure new static func never uses old data after a
318 		 * 1->0->1 transition sequence.
319 		 */
320 		tp_rcu_cond_sync(TP_TRANSITION_SYNC_1_0_1);
321 		/* Set static call to first function */
322 		tracepoint_update_call(tp, tp_funcs);
323 		/* Both iterator and static call handle NULL tp->funcs */
324 		rcu_assign_pointer(tp->funcs, tp_funcs);
325 		static_branch_enable(&tp->key);
326 		break;
327 	case TP_FUNC_2:		/* 1->2 */
328 		/* Set iterator static call */
329 		tracepoint_update_call(tp, tp_funcs);
330 		/*
331 		 * Iterator callback installed before updating tp->funcs.
332 		 * Requires ordering between RCU assign/dereference and
333 		 * static call update/call.
334 		 */
335 		fallthrough;
336 	case TP_FUNC_N:		/* N->N+1 (N>1) */
337 		rcu_assign_pointer(tp->funcs, tp_funcs);
338 		/*
339 		 * Make sure static func never uses incorrect data after a
340 		 * N->...->2->1 (N>1) transition sequence.
341 		 */
342 		if (tp_funcs[0].data != old[0].data)
343 			tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1);
344 		break;
345 	default:
346 		WARN_ON_ONCE(1);
347 		break;
348 	}
349 
350 	release_probes(tp, old);
351 	return 0;
352 }
353 
354 /*
355  * Remove a probe function from a tracepoint.
356  * Note: only waiting an RCU period after setting elem->call to the empty
357  * function insures that the original callback is not used anymore. This insured
358  * by preempt_disable around the call site.
359  */
360 static int tracepoint_remove_func(struct tracepoint *tp,
361 		struct tracepoint_func *func)
362 {
363 	struct tracepoint_func *old, *tp_funcs;
364 
365 	tp_funcs = rcu_dereference_protected(tp->funcs,
366 			lockdep_is_held(&tracepoints_mutex));
367 	old = func_remove(&tp_funcs, func);
368 	if (WARN_ON_ONCE(IS_ERR(old)))
369 		return PTR_ERR(old);
370 
371 	if (tp_funcs == old)
372 		/* Failed allocating new tp_funcs, replaced func with stub */
373 		return 0;
374 
375 	switch (nr_func_state(tp_funcs)) {
376 	case TP_FUNC_0:		/* 1->0 */
377 		/* Removed last function */
378 		if (tp->ext && tp->ext->unregfunc && static_key_enabled(&tp->key))
379 			tp->ext->unregfunc();
380 		static_branch_disable(&tp->key);
381 		/* Set iterator static call */
382 		tracepoint_update_call(tp, tp_funcs);
383 		/* Both iterator and static call handle NULL tp->funcs */
384 		rcu_assign_pointer(tp->funcs, NULL);
385 		/*
386 		 * Make sure new static func never uses old data after a
387 		 * 1->0->1 transition sequence.
388 		 */
389 		tp_rcu_get_state(TP_TRANSITION_SYNC_1_0_1);
390 		break;
391 	case TP_FUNC_1:		/* 2->1 */
392 		rcu_assign_pointer(tp->funcs, tp_funcs);
393 		/*
394 		 * Make sure static func never uses incorrect data after a
395 		 * N->...->2->1 (N>2) transition sequence. If the first
396 		 * element's data has changed, then force the synchronization
397 		 * to prevent current readers that have loaded the old data
398 		 * from calling the new function.
399 		 */
400 		if (tp_funcs[0].data != old[0].data)
401 			tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1);
402 		tp_rcu_cond_sync(TP_TRANSITION_SYNC_N_2_1);
403 		/* Set static call to first function */
404 		tracepoint_update_call(tp, tp_funcs);
405 		break;
406 	case TP_FUNC_2:		/* N->N-1 (N>2) */
407 		fallthrough;
408 	case TP_FUNC_N:
409 		rcu_assign_pointer(tp->funcs, tp_funcs);
410 		/*
411 		 * Make sure static func never uses incorrect data after a
412 		 * N->...->2->1 (N>2) transition sequence.
413 		 */
414 		if (tp_funcs[0].data != old[0].data)
415 			tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1);
416 		break;
417 	default:
418 		WARN_ON_ONCE(1);
419 		break;
420 	}
421 	release_probes(tp, old);
422 	return 0;
423 }
424 
425 /**
426  * tracepoint_probe_register_prio_may_exist -  Connect a probe to a tracepoint with priority
427  * @tp: tracepoint
428  * @probe: probe handler
429  * @data: tracepoint data
430  * @prio: priority of this function over other registered functions
431  *
432  * Same as tracepoint_probe_register_prio() except that it will not warn
433  * if the tracepoint is already registered.
434  */
435 int tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe,
436 					     void *data, int prio)
437 {
438 	struct tracepoint_func tp_func;
439 	int ret;
440 
441 	mutex_lock(&tracepoints_mutex);
442 	tp_func.func = probe;
443 	tp_func.data = data;
444 	tp_func.prio = prio;
445 	ret = tracepoint_add_func(tp, &tp_func, prio, false);
446 	mutex_unlock(&tracepoints_mutex);
447 	return ret;
448 }
449 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio_may_exist);
450 
451 /**
452  * tracepoint_probe_register_prio -  Connect a probe to a tracepoint with priority
453  * @tp: tracepoint
454  * @probe: probe handler
455  * @data: tracepoint data
456  * @prio: priority of this function over other registered functions
457  *
458  * Returns 0 if ok, error value on error.
459  * Note: if @tp is within a module, the caller is responsible for
460  * unregistering the probe before the module is gone. This can be
461  * performed either with a tracepoint module going notifier, or from
462  * within module exit functions.
463  */
464 int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
465 				   void *data, int prio)
466 {
467 	struct tracepoint_func tp_func;
468 	int ret;
469 
470 	mutex_lock(&tracepoints_mutex);
471 	tp_func.func = probe;
472 	tp_func.data = data;
473 	tp_func.prio = prio;
474 	ret = tracepoint_add_func(tp, &tp_func, prio, true);
475 	mutex_unlock(&tracepoints_mutex);
476 	return ret;
477 }
478 EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
479 
480 /**
481  * tracepoint_probe_register -  Connect a probe to a tracepoint
482  * @tp: tracepoint
483  * @probe: probe handler
484  * @data: tracepoint data
485  *
486  * Returns 0 if ok, error value on error.
487  * Note: if @tp is within a module, the caller is responsible for
488  * unregistering the probe before the module is gone. This can be
489  * performed either with a tracepoint module going notifier, or from
490  * within module exit functions.
491  */
492 int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
493 {
494 	return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
495 }
496 EXPORT_SYMBOL_GPL(tracepoint_probe_register);
497 
498 /**
499  * tracepoint_probe_unregister -  Disconnect a probe from a tracepoint
500  * @tp: tracepoint
501  * @probe: probe function pointer
502  * @data: tracepoint data
503  *
504  * Returns 0 if ok, error value on error.
505  */
506 int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
507 {
508 	struct tracepoint_func tp_func;
509 	int ret;
510 
511 	mutex_lock(&tracepoints_mutex);
512 	tp_func.func = probe;
513 	tp_func.data = data;
514 	ret = tracepoint_remove_func(tp, &tp_func);
515 	mutex_unlock(&tracepoints_mutex);
516 	return ret;
517 }
518 EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
519 
520 static void for_each_tracepoint_range(
521 		tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
522 		void (*fct)(struct tracepoint *tp, void *priv),
523 		void *priv)
524 {
525 	tracepoint_ptr_t *iter;
526 
527 	if (!begin)
528 		return;
529 	for (iter = begin; iter < end; iter++)
530 		fct(tracepoint_ptr_deref(iter), priv);
531 }
532 
533 #ifdef CONFIG_MODULES
534 bool trace_module_has_bad_taint(struct module *mod)
535 {
536 	return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
537 				(1 << TAINT_UNSIGNED_MODULE) | (1 << TAINT_TEST) |
538 				(1 << TAINT_LIVEPATCH));
539 }
540 
541 static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
542 
543 /**
544  * register_tracepoint_module_notifier - register tracepoint coming/going notifier
545  * @nb: notifier block
546  *
547  * Notifiers registered with this function are called on module
548  * coming/going with the tracepoint_module_list_mutex held.
549  * The notifier block callback should expect a "struct tp_module" data
550  * pointer.
551  */
552 int register_tracepoint_module_notifier(struct notifier_block *nb)
553 {
554 	struct tp_module *tp_mod;
555 	int ret;
556 
557 	mutex_lock(&tracepoint_module_list_mutex);
558 	ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
559 	if (ret)
560 		goto end;
561 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
562 		(void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
563 end:
564 	mutex_unlock(&tracepoint_module_list_mutex);
565 	return ret;
566 }
567 EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
568 
569 /**
570  * unregister_tracepoint_module_notifier - unregister tracepoint coming/going notifier
571  * @nb: notifier block
572  *
573  * The notifier block callback should expect a "struct tp_module" data
574  * pointer.
575  */
576 int unregister_tracepoint_module_notifier(struct notifier_block *nb)
577 {
578 	struct tp_module *tp_mod;
579 	int ret;
580 
581 	mutex_lock(&tracepoint_module_list_mutex);
582 	ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
583 	if (ret)
584 		goto end;
585 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
586 		(void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
587 end:
588 	mutex_unlock(&tracepoint_module_list_mutex);
589 	return ret;
590 
591 }
592 EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
593 
594 /*
595  * Ensure the tracer unregistered the module's probes before the module
596  * teardown is performed. Prevents leaks of probe and data pointers.
597  */
598 static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
599 {
600 	WARN_ON_ONCE(tp->funcs);
601 }
602 
603 static int tracepoint_module_coming(struct module *mod)
604 {
605 	struct tp_module *tp_mod;
606 
607 	if (!mod->num_tracepoints)
608 		return 0;
609 
610 	/*
611 	 * We skip modules that taint the kernel, especially those with different
612 	 * module headers (for forced load), to make sure we don't cause a crash.
613 	 * Staging, out-of-tree, unsigned GPL, and test modules are fine.
614 	 */
615 	if (trace_module_has_bad_taint(mod))
616 		return 0;
617 
618 	tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
619 	if (!tp_mod)
620 		return -ENOMEM;
621 	tp_mod->mod = mod;
622 
623 	mutex_lock(&tracepoint_module_list_mutex);
624 	list_add_tail(&tp_mod->list, &tracepoint_module_list);
625 	blocking_notifier_call_chain(&tracepoint_notify_list,
626 			MODULE_STATE_COMING, tp_mod);
627 	mutex_unlock(&tracepoint_module_list_mutex);
628 	return 0;
629 }
630 
631 static void tracepoint_module_going(struct module *mod)
632 {
633 	struct tp_module *tp_mod;
634 
635 	if (!mod->num_tracepoints)
636 		return;
637 
638 	mutex_lock(&tracepoint_module_list_mutex);
639 	list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
640 		if (tp_mod->mod == mod) {
641 			blocking_notifier_call_chain(&tracepoint_notify_list,
642 					MODULE_STATE_GOING, tp_mod);
643 			list_del(&tp_mod->list);
644 			kfree(tp_mod);
645 			/*
646 			 * Called the going notifier before checking for
647 			 * quiescence.
648 			 */
649 			for_each_tracepoint_range(mod->tracepoints_ptrs,
650 				mod->tracepoints_ptrs + mod->num_tracepoints,
651 				tp_module_going_check_quiescent, NULL);
652 			break;
653 		}
654 	}
655 	/*
656 	 * In the case of modules that were tainted at "coming", we'll simply
657 	 * walk through the list without finding it. We cannot use the "tainted"
658 	 * flag on "going", in case a module taints the kernel only after being
659 	 * loaded.
660 	 */
661 	mutex_unlock(&tracepoint_module_list_mutex);
662 }
663 
664 static int tracepoint_module_notify(struct notifier_block *self,
665 		unsigned long val, void *data)
666 {
667 	struct module *mod = data;
668 	int ret = 0;
669 
670 	switch (val) {
671 	case MODULE_STATE_COMING:
672 		ret = tracepoint_module_coming(mod);
673 		break;
674 	case MODULE_STATE_LIVE:
675 		break;
676 	case MODULE_STATE_GOING:
677 		tracepoint_module_going(mod);
678 		break;
679 	case MODULE_STATE_UNFORMED:
680 		break;
681 	}
682 	return notifier_from_errno(ret);
683 }
684 
685 static struct notifier_block tracepoint_module_nb = {
686 	.notifier_call = tracepoint_module_notify,
687 	.priority = 0,
688 };
689 
690 static __init int init_tracepoints(void)
691 {
692 	int ret;
693 
694 	ret = register_module_notifier(&tracepoint_module_nb);
695 	if (ret)
696 		pr_warn("Failed to register tracepoint module enter notifier\n");
697 
698 	return ret;
699 }
700 __initcall(init_tracepoints);
701 
702 /**
703  * for_each_tracepoint_in_module - iteration on all tracepoints in a module
704  * @mod: module
705  * @fct: callback
706  * @priv: private data
707  */
708 void for_each_tracepoint_in_module(struct module *mod,
709 				   void (*fct)(struct tracepoint *tp,
710 				    struct module *mod, void *priv),
711 				   void *priv)
712 {
713 	tracepoint_ptr_t *begin, *end, *iter;
714 
715 	lockdep_assert_held(&tracepoint_module_list_mutex);
716 
717 	if (!mod)
718 		return;
719 
720 	begin = mod->tracepoints_ptrs;
721 	end = mod->tracepoints_ptrs + mod->num_tracepoints;
722 
723 	for (iter = begin; iter < end; iter++)
724 		fct(tracepoint_ptr_deref(iter), mod, priv);
725 }
726 
727 /**
728  * for_each_module_tracepoint - iteration on all tracepoints in all modules
729  * @fct: callback
730  * @priv: private data
731  */
732 void for_each_module_tracepoint(void (*fct)(struct tracepoint *tp,
733 				 struct module *mod, void *priv),
734 				void *priv)
735 {
736 	struct tp_module *tp_mod;
737 
738 	mutex_lock(&tracepoint_module_list_mutex);
739 	list_for_each_entry(tp_mod, &tracepoint_module_list, list)
740 		for_each_tracepoint_in_module(tp_mod->mod, fct, priv);
741 	mutex_unlock(&tracepoint_module_list_mutex);
742 }
743 #endif /* CONFIG_MODULES */
744 
745 /**
746  * for_each_kernel_tracepoint - iteration on all kernel tracepoints
747  * @fct: callback
748  * @priv: private data
749  */
750 void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
751 		void *priv)
752 {
753 	for_each_tracepoint_range(__start___tracepoints_ptrs,
754 		__stop___tracepoints_ptrs, fct, priv);
755 }
756 EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
757 
758 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
759 
760 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
761 static int sys_tracepoint_refcount;
762 
763 int syscall_regfunc(void)
764 {
765 	struct task_struct *p, *t;
766 
767 	if (!sys_tracepoint_refcount) {
768 		read_lock(&tasklist_lock);
769 		for_each_process_thread(p, t) {
770 			set_task_syscall_work(t, SYSCALL_TRACEPOINT);
771 		}
772 		read_unlock(&tasklist_lock);
773 	}
774 	sys_tracepoint_refcount++;
775 
776 	return 0;
777 }
778 
779 void syscall_unregfunc(void)
780 {
781 	struct task_struct *p, *t;
782 
783 	sys_tracepoint_refcount--;
784 	if (!sys_tracepoint_refcount) {
785 		read_lock(&tasklist_lock);
786 		for_each_process_thread(p, t) {
787 			clear_task_syscall_work(t, SYSCALL_TRACEPOINT);
788 		}
789 		read_unlock(&tasklist_lock);
790 	}
791 }
792 #endif
793