xref: /linux-6.15/kernel/notifier.c (revision 13dfd97a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kdebug.h>
3 #include <linux/kprobes.h>
4 #include <linux/export.h>
5 #include <linux/notifier.h>
6 #include <linux/rcupdate.h>
7 #include <linux/vmalloc.h>
8 #include <linux/reboot.h>
9 
10 /*
11  *	Notifier list for kernel code which wants to be called
12  *	at shutdown. This is used to stop any idling DMA operations
13  *	and the like.
14  */
15 BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
16 
17 /*
18  *	Notifier chain core routines.  The exported routines below
19  *	are layered on top of these, with appropriate locking added.
20  */
21 
22 static int notifier_chain_register(struct notifier_block **nl,
23 				   struct notifier_block *n)
24 {
25 	while ((*nl) != NULL) {
26 		if (unlikely((*nl) == n)) {
27 			WARN(1, "notifier callback %ps already registered",
28 			     n->notifier_call);
29 			return -EEXIST;
30 		}
31 		if (n->priority > (*nl)->priority)
32 			break;
33 		nl = &((*nl)->next);
34 	}
35 	n->next = *nl;
36 	rcu_assign_pointer(*nl, n);
37 	return 0;
38 }
39 
40 static int notifier_chain_unregister(struct notifier_block **nl,
41 		struct notifier_block *n)
42 {
43 	while ((*nl) != NULL) {
44 		if ((*nl) == n) {
45 			rcu_assign_pointer(*nl, n->next);
46 			return 0;
47 		}
48 		nl = &((*nl)->next);
49 	}
50 	return -ENOENT;
51 }
52 
53 /**
54  * notifier_call_chain - Informs the registered notifiers about an event.
55  *	@nl:		Pointer to head of the blocking notifier chain
56  *	@val:		Value passed unmodified to notifier function
57  *	@v:		Pointer passed unmodified to notifier function
58  *	@nr_to_call:	Number of notifier functions to be called. Don't care
59  *			value of this parameter is -1.
60  *	@nr_calls:	Records the number of notifications sent. Don't care
61  *			value of this field is NULL.
62  *	@returns:	notifier_call_chain returns the value returned by the
63  *			last notifier function called.
64  */
65 static int notifier_call_chain(struct notifier_block **nl,
66 			       unsigned long val, void *v,
67 			       int nr_to_call, int *nr_calls)
68 {
69 	int ret = NOTIFY_DONE;
70 	struct notifier_block *nb, *next_nb;
71 
72 	nb = rcu_dereference_raw(*nl);
73 
74 	while (nb && nr_to_call) {
75 		next_nb = rcu_dereference_raw(nb->next);
76 
77 #ifdef CONFIG_DEBUG_NOTIFIERS
78 		if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
79 			WARN(1, "Invalid notifier called!");
80 			nb = next_nb;
81 			continue;
82 		}
83 #endif
84 		ret = nb->notifier_call(nb, val, v);
85 
86 		if (nr_calls)
87 			(*nr_calls)++;
88 
89 		if (ret & NOTIFY_STOP_MASK)
90 			break;
91 		nb = next_nb;
92 		nr_to_call--;
93 	}
94 	return ret;
95 }
96 NOKPROBE_SYMBOL(notifier_call_chain);
97 
98 /**
99  * notifier_call_chain_robust - Inform the registered notifiers about an event
100  *                              and rollback on error.
101  * @nl:		Pointer to head of the blocking notifier chain
102  * @val_up:	Value passed unmodified to the notifier function
103  * @val_down:	Value passed unmodified to the notifier function when recovering
104  *              from an error on @val_up
105  * @v		Pointer passed unmodified to the notifier function
106  *
107  * NOTE:	It is important the @nl chain doesn't change between the two
108  *		invocations of notifier_call_chain() such that we visit the
109  *		exact same notifier callbacks; this rules out any RCU usage.
110  *
111  * Returns:	the return value of the @val_up call.
112  */
113 static int notifier_call_chain_robust(struct notifier_block **nl,
114 				     unsigned long val_up, unsigned long val_down,
115 				     void *v)
116 {
117 	int ret, nr = 0;
118 
119 	ret = notifier_call_chain(nl, val_up, v, -1, &nr);
120 	if (ret & NOTIFY_STOP_MASK)
121 		notifier_call_chain(nl, val_down, v, nr-1, NULL);
122 
123 	return ret;
124 }
125 
126 /*
127  *	Atomic notifier chain routines.  Registration and unregistration
128  *	use a spinlock, and call_chain is synchronized by RCU (no locks).
129  */
130 
131 /**
132  *	atomic_notifier_chain_register - Add notifier to an atomic notifier chain
133  *	@nh: Pointer to head of the atomic notifier chain
134  *	@n: New entry in notifier chain
135  *
136  *	Adds a notifier to an atomic notifier chain.
137  *
138  *	Returns 0 on success, %-EEXIST on error.
139  */
140 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
141 		struct notifier_block *n)
142 {
143 	unsigned long flags;
144 	int ret;
145 
146 	spin_lock_irqsave(&nh->lock, flags);
147 	ret = notifier_chain_register(&nh->head, n);
148 	spin_unlock_irqrestore(&nh->lock, flags);
149 	return ret;
150 }
151 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
152 
153 /**
154  *	atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
155  *	@nh: Pointer to head of the atomic notifier chain
156  *	@n: Entry to remove from notifier chain
157  *
158  *	Removes a notifier from an atomic notifier chain.
159  *
160  *	Returns zero on success or %-ENOENT on failure.
161  */
162 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
163 		struct notifier_block *n)
164 {
165 	unsigned long flags;
166 	int ret;
167 
168 	spin_lock_irqsave(&nh->lock, flags);
169 	ret = notifier_chain_unregister(&nh->head, n);
170 	spin_unlock_irqrestore(&nh->lock, flags);
171 	synchronize_rcu();
172 	return ret;
173 }
174 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
175 
176 /**
177  *	atomic_notifier_call_chain - Call functions in an atomic notifier chain
178  *	@nh: Pointer to head of the atomic notifier chain
179  *	@val: Value passed unmodified to notifier function
180  *	@v: Pointer passed unmodified to notifier function
181  *
182  *	Calls each function in a notifier chain in turn.  The functions
183  *	run in an atomic context, so they must not block.
184  *	This routine uses RCU to synchronize with changes to the chain.
185  *
186  *	If the return value of the notifier can be and'ed
187  *	with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
188  *	will return immediately, with the return value of
189  *	the notifier function which halted execution.
190  *	Otherwise the return value is the return value
191  *	of the last notifier function called.
192  */
193 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
194 			       unsigned long val, void *v)
195 {
196 	int ret;
197 
198 	rcu_read_lock();
199 	ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
200 	rcu_read_unlock();
201 
202 	return ret;
203 }
204 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
205 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
206 
207 /**
208  *	atomic_notifier_call_chain_is_empty - Check whether notifier chain is empty
209  *	@nh: Pointer to head of the atomic notifier chain
210  *
211  *	Checks whether notifier chain is empty.
212  *
213  *	Returns true is notifier chain is empty, false otherwise.
214  */
215 bool atomic_notifier_call_chain_is_empty(struct atomic_notifier_head *nh)
216 {
217 	return !rcu_access_pointer(nh->head);
218 }
219 
220 /*
221  *	Blocking notifier chain routines.  All access to the chain is
222  *	synchronized by an rwsem.
223  */
224 
225 /**
226  *	blocking_notifier_chain_register - Add notifier to a blocking notifier chain
227  *	@nh: Pointer to head of the blocking notifier chain
228  *	@n: New entry in notifier chain
229  *
230  *	Adds a notifier to a blocking notifier chain.
231  *	Must be called in process context.
232  *
233  *	Returns 0 on success, %-EEXIST on error.
234  */
235 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
236 		struct notifier_block *n)
237 {
238 	int ret;
239 
240 	/*
241 	 * This code gets used during boot-up, when task switching is
242 	 * not yet working and interrupts must remain disabled.  At
243 	 * such times we must not call down_write().
244 	 */
245 	if (unlikely(system_state == SYSTEM_BOOTING))
246 		return notifier_chain_register(&nh->head, n);
247 
248 	down_write(&nh->rwsem);
249 	ret = notifier_chain_register(&nh->head, n);
250 	up_write(&nh->rwsem);
251 	return ret;
252 }
253 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
254 
255 /**
256  *	blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
257  *	@nh: Pointer to head of the blocking notifier chain
258  *	@n: Entry to remove from notifier chain
259  *
260  *	Removes a notifier from a blocking notifier chain.
261  *	Must be called from process context.
262  *
263  *	Returns zero on success or %-ENOENT on failure.
264  */
265 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
266 		struct notifier_block *n)
267 {
268 	int ret;
269 
270 	/*
271 	 * This code gets used during boot-up, when task switching is
272 	 * not yet working and interrupts must remain disabled.  At
273 	 * such times we must not call down_write().
274 	 */
275 	if (unlikely(system_state == SYSTEM_BOOTING))
276 		return notifier_chain_unregister(&nh->head, n);
277 
278 	down_write(&nh->rwsem);
279 	ret = notifier_chain_unregister(&nh->head, n);
280 	up_write(&nh->rwsem);
281 	return ret;
282 }
283 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
284 
285 int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
286 		unsigned long val_up, unsigned long val_down, void *v)
287 {
288 	int ret = NOTIFY_DONE;
289 
290 	/*
291 	 * We check the head outside the lock, but if this access is
292 	 * racy then it does not matter what the result of the test
293 	 * is, we re-check the list after having taken the lock anyway:
294 	 */
295 	if (rcu_access_pointer(nh->head)) {
296 		down_read(&nh->rwsem);
297 		ret = notifier_call_chain_robust(&nh->head, val_up, val_down, v);
298 		up_read(&nh->rwsem);
299 	}
300 	return ret;
301 }
302 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_robust);
303 
304 /**
305  *	blocking_notifier_call_chain - Call functions in a blocking notifier chain
306  *	@nh: Pointer to head of the blocking notifier chain
307  *	@val: Value passed unmodified to notifier function
308  *	@v: Pointer passed unmodified to notifier function
309  *
310  *	Calls each function in a notifier chain in turn.  The functions
311  *	run in a process context, so they are allowed to block.
312  *
313  *	If the return value of the notifier can be and'ed
314  *	with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
315  *	will return immediately, with the return value of
316  *	the notifier function which halted execution.
317  *	Otherwise the return value is the return value
318  *	of the last notifier function called.
319  */
320 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
321 		unsigned long val, void *v)
322 {
323 	int ret = NOTIFY_DONE;
324 
325 	/*
326 	 * We check the head outside the lock, but if this access is
327 	 * racy then it does not matter what the result of the test
328 	 * is, we re-check the list after having taken the lock anyway:
329 	 */
330 	if (rcu_access_pointer(nh->head)) {
331 		down_read(&nh->rwsem);
332 		ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
333 		up_read(&nh->rwsem);
334 	}
335 	return ret;
336 }
337 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
338 
339 /*
340  *	Raw notifier chain routines.  There is no protection;
341  *	the caller must provide it.  Use at your own risk!
342  */
343 
344 /**
345  *	raw_notifier_chain_register - Add notifier to a raw notifier chain
346  *	@nh: Pointer to head of the raw notifier chain
347  *	@n: New entry in notifier chain
348  *
349  *	Adds a notifier to a raw notifier chain.
350  *	All locking must be provided by the caller.
351  *
352  *	Returns 0 on success, %-EEXIST on error.
353  */
354 int raw_notifier_chain_register(struct raw_notifier_head *nh,
355 		struct notifier_block *n)
356 {
357 	return notifier_chain_register(&nh->head, n);
358 }
359 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
360 
361 /**
362  *	raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
363  *	@nh: Pointer to head of the raw notifier chain
364  *	@n: Entry to remove from notifier chain
365  *
366  *	Removes a notifier from a raw notifier chain.
367  *	All locking must be provided by the caller.
368  *
369  *	Returns zero on success or %-ENOENT on failure.
370  */
371 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
372 		struct notifier_block *n)
373 {
374 	return notifier_chain_unregister(&nh->head, n);
375 }
376 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
377 
378 int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
379 		unsigned long val_up, unsigned long val_down, void *v)
380 {
381 	return notifier_call_chain_robust(&nh->head, val_up, val_down, v);
382 }
383 EXPORT_SYMBOL_GPL(raw_notifier_call_chain_robust);
384 
385 /**
386  *	raw_notifier_call_chain - Call functions in a raw notifier chain
387  *	@nh: Pointer to head of the raw notifier chain
388  *	@val: Value passed unmodified to notifier function
389  *	@v: Pointer passed unmodified to notifier function
390  *
391  *	Calls each function in a notifier chain in turn.  The functions
392  *	run in an undefined context.
393  *	All locking must be provided by the caller.
394  *
395  *	If the return value of the notifier can be and'ed
396  *	with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
397  *	will return immediately, with the return value of
398  *	the notifier function which halted execution.
399  *	Otherwise the return value is the return value
400  *	of the last notifier function called.
401  */
402 int raw_notifier_call_chain(struct raw_notifier_head *nh,
403 		unsigned long val, void *v)
404 {
405 	return notifier_call_chain(&nh->head, val, v, -1, NULL);
406 }
407 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
408 
409 #ifdef CONFIG_SRCU
410 /*
411  *	SRCU notifier chain routines.    Registration and unregistration
412  *	use a mutex, and call_chain is synchronized by SRCU (no locks).
413  */
414 
415 /**
416  *	srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
417  *	@nh: Pointer to head of the SRCU notifier chain
418  *	@n: New entry in notifier chain
419  *
420  *	Adds a notifier to an SRCU notifier chain.
421  *	Must be called in process context.
422  *
423  *	Returns 0 on success, %-EEXIST on error.
424  */
425 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
426 		struct notifier_block *n)
427 {
428 	int ret;
429 
430 	/*
431 	 * This code gets used during boot-up, when task switching is
432 	 * not yet working and interrupts must remain disabled.  At
433 	 * such times we must not call mutex_lock().
434 	 */
435 	if (unlikely(system_state == SYSTEM_BOOTING))
436 		return notifier_chain_register(&nh->head, n);
437 
438 	mutex_lock(&nh->mutex);
439 	ret = notifier_chain_register(&nh->head, n);
440 	mutex_unlock(&nh->mutex);
441 	return ret;
442 }
443 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
444 
445 /**
446  *	srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
447  *	@nh: Pointer to head of the SRCU notifier chain
448  *	@n: Entry to remove from notifier chain
449  *
450  *	Removes a notifier from an SRCU notifier chain.
451  *	Must be called from process context.
452  *
453  *	Returns zero on success or %-ENOENT on failure.
454  */
455 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
456 		struct notifier_block *n)
457 {
458 	int ret;
459 
460 	/*
461 	 * This code gets used during boot-up, when task switching is
462 	 * not yet working and interrupts must remain disabled.  At
463 	 * such times we must not call mutex_lock().
464 	 */
465 	if (unlikely(system_state == SYSTEM_BOOTING))
466 		return notifier_chain_unregister(&nh->head, n);
467 
468 	mutex_lock(&nh->mutex);
469 	ret = notifier_chain_unregister(&nh->head, n);
470 	mutex_unlock(&nh->mutex);
471 	synchronize_srcu(&nh->srcu);
472 	return ret;
473 }
474 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
475 
476 /**
477  *	srcu_notifier_call_chain - Call functions in an SRCU notifier chain
478  *	@nh: Pointer to head of the SRCU notifier chain
479  *	@val: Value passed unmodified to notifier function
480  *	@v: Pointer passed unmodified to notifier function
481  *
482  *	Calls each function in a notifier chain in turn.  The functions
483  *	run in a process context, so they are allowed to block.
484  *
485  *	If the return value of the notifier can be and'ed
486  *	with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
487  *	will return immediately, with the return value of
488  *	the notifier function which halted execution.
489  *	Otherwise the return value is the return value
490  *	of the last notifier function called.
491  */
492 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
493 		unsigned long val, void *v)
494 {
495 	int ret;
496 	int idx;
497 
498 	idx = srcu_read_lock(&nh->srcu);
499 	ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
500 	srcu_read_unlock(&nh->srcu, idx);
501 	return ret;
502 }
503 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
504 
505 /**
506  *	srcu_init_notifier_head - Initialize an SRCU notifier head
507  *	@nh: Pointer to head of the srcu notifier chain
508  *
509  *	Unlike other sorts of notifier heads, SRCU notifier heads require
510  *	dynamic initialization.  Be sure to call this routine before
511  *	calling any of the other SRCU notifier routines for this head.
512  *
513  *	If an SRCU notifier head is deallocated, it must first be cleaned
514  *	up by calling srcu_cleanup_notifier_head().  Otherwise the head's
515  *	per-cpu data (used by the SRCU mechanism) will leak.
516  */
517 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
518 {
519 	mutex_init(&nh->mutex);
520 	if (init_srcu_struct(&nh->srcu) < 0)
521 		BUG();
522 	nh->head = NULL;
523 }
524 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
525 
526 #endif /* CONFIG_SRCU */
527 
528 static ATOMIC_NOTIFIER_HEAD(die_chain);
529 
530 int notrace notify_die(enum die_val val, const char *str,
531 	       struct pt_regs *regs, long err, int trap, int sig)
532 {
533 	struct die_args args = {
534 		.regs	= regs,
535 		.str	= str,
536 		.err	= err,
537 		.trapnr	= trap,
538 		.signr	= sig,
539 
540 	};
541 	RCU_LOCKDEP_WARN(!rcu_is_watching(),
542 			   "notify_die called but RCU thinks we're quiescent");
543 	return atomic_notifier_call_chain(&die_chain, val, &args);
544 }
545 NOKPROBE_SYMBOL(notify_die);
546 
547 int register_die_notifier(struct notifier_block *nb)
548 {
549 	return atomic_notifier_chain_register(&die_chain, nb);
550 }
551 EXPORT_SYMBOL_GPL(register_die_notifier);
552 
553 int unregister_die_notifier(struct notifier_block *nb)
554 {
555 	return atomic_notifier_chain_unregister(&die_chain, nb);
556 }
557 EXPORT_SYMBOL_GPL(unregister_die_notifier);
558