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>
11 * Notifier list for kernel code which wants to be called
12 * at shutdown. This is used to stop any idling DMA operations
15 BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
18 * Notifier chain core routines. The exported routines below
19 * are layered on top of these, with appropriate locking added.
22 static int notifier_chain_register(struct notifier_block **nl,
23 struct notifier_block *n)
25 while ((*nl) != NULL) {
26 if (unlikely((*nl) == n)) {
27 WARN(1, "double register detected");
30 if (n->priority > (*nl)->priority)
35 rcu_assign_pointer(*nl, n);
39 static int notifier_chain_cond_register(struct notifier_block **nl,
40 struct notifier_block *n)
42 while ((*nl) != NULL) {
45 if (n->priority > (*nl)->priority)
50 rcu_assign_pointer(*nl, n);
54 static int notifier_chain_unregister(struct notifier_block **nl,
55 struct notifier_block *n)
57 while ((*nl) != NULL) {
59 rcu_assign_pointer(*nl, n->next);
68 * notifier_call_chain - Informs the registered notifiers about an event.
69 * @nl: Pointer to head of the blocking notifier chain
70 * @val: Value passed unmodified to notifier function
71 * @v: Pointer passed unmodified to notifier function
72 * @nr_to_call: Number of notifier functions to be called. Don't care
73 * value of this parameter is -1.
74 * @nr_calls: Records the number of notifications sent. Don't care
75 * value of this field is NULL.
76 * @returns: notifier_call_chain returns the value returned by the
77 * last notifier function called.
79 static int notifier_call_chain(struct notifier_block **nl,
80 unsigned long val, void *v,
81 int nr_to_call, int *nr_calls)
83 int ret = NOTIFY_DONE;
84 struct notifier_block *nb, *next_nb;
86 nb = rcu_dereference_raw(*nl);
88 while (nb && nr_to_call) {
89 next_nb = rcu_dereference_raw(nb->next);
91 #ifdef CONFIG_DEBUG_NOTIFIERS
92 if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
93 WARN(1, "Invalid notifier called!");
98 ret = nb->notifier_call(nb, val, v);
103 if (ret & NOTIFY_STOP_MASK)
110 NOKPROBE_SYMBOL(notifier_call_chain);
113 * Atomic notifier chain routines. Registration and unregistration
114 * use a spinlock, and call_chain is synchronized by RCU (no locks).
118 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
119 * @nh: Pointer to head of the atomic notifier chain
120 * @n: New entry in notifier chain
122 * Adds a notifier to an atomic notifier chain.
124 * Currently always returns zero.
126 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
127 struct notifier_block *n)
132 spin_lock_irqsave(&nh->lock, flags);
133 ret = notifier_chain_register(&nh->head, n);
134 spin_unlock_irqrestore(&nh->lock, flags);
137 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
140 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
141 * @nh: Pointer to head of the atomic notifier chain
142 * @n: Entry to remove from notifier chain
144 * Removes a notifier from an atomic notifier chain.
146 * Returns zero on success or %-ENOENT on failure.
148 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
149 struct notifier_block *n)
154 spin_lock_irqsave(&nh->lock, flags);
155 ret = notifier_chain_unregister(&nh->head, n);
156 spin_unlock_irqrestore(&nh->lock, flags);
160 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
163 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
164 * @nh: Pointer to head of the atomic notifier chain
165 * @val: Value passed unmodified to notifier function
166 * @v: Pointer passed unmodified to notifier function
167 * @nr_to_call: See the comment for notifier_call_chain.
168 * @nr_calls: See the comment for notifier_call_chain.
170 * Calls each function in a notifier chain in turn. The functions
171 * run in an atomic context, so they must not block.
172 * This routine uses RCU to synchronize with changes to the chain.
174 * If the return value of the notifier can be and'ed
175 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
176 * will return immediately, with the return value of
177 * the notifier function which halted execution.
178 * Otherwise the return value is the return value
179 * of the last notifier function called.
181 int __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
182 unsigned long val, void *v,
183 int nr_to_call, int *nr_calls)
188 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
192 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
193 NOKPROBE_SYMBOL(__atomic_notifier_call_chain);
195 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
196 unsigned long val, void *v)
198 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
200 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
201 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
204 * Blocking notifier chain routines. All access to the chain is
205 * synchronized by an rwsem.
209 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
210 * @nh: Pointer to head of the blocking notifier chain
211 * @n: New entry in notifier chain
213 * Adds a notifier to a blocking notifier chain.
214 * Must be called in process context.
216 * Currently always returns zero.
218 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
219 struct notifier_block *n)
224 * This code gets used during boot-up, when task switching is
225 * not yet working and interrupts must remain disabled. At
226 * such times we must not call down_write().
228 if (unlikely(system_state == SYSTEM_BOOTING))
229 return notifier_chain_register(&nh->head, n);
231 down_write(&nh->rwsem);
232 ret = notifier_chain_register(&nh->head, n);
233 up_write(&nh->rwsem);
236 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
239 * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
240 * @nh: Pointer to head of the blocking notifier chain
241 * @n: New entry in notifier chain
243 * Adds a notifier to a blocking notifier chain, only if not already
244 * present in the chain.
245 * Must be called in process context.
247 * Currently always returns zero.
249 int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
250 struct notifier_block *n)
254 down_write(&nh->rwsem);
255 ret = notifier_chain_cond_register(&nh->head, n);
256 up_write(&nh->rwsem);
259 EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
262 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
263 * @nh: Pointer to head of the blocking notifier chain
264 * @n: Entry to remove from notifier chain
266 * Removes a notifier from a blocking notifier chain.
267 * Must be called from process context.
269 * Returns zero on success or %-ENOENT on failure.
271 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
272 struct notifier_block *n)
277 * This code gets used during boot-up, when task switching is
278 * not yet working and interrupts must remain disabled. At
279 * such times we must not call down_write().
281 if (unlikely(system_state == SYSTEM_BOOTING))
282 return notifier_chain_unregister(&nh->head, n);
284 down_write(&nh->rwsem);
285 ret = notifier_chain_unregister(&nh->head, n);
286 up_write(&nh->rwsem);
289 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
292 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
293 * @nh: Pointer to head of the blocking notifier chain
294 * @val: Value passed unmodified to notifier function
295 * @v: Pointer passed unmodified to notifier function
296 * @nr_to_call: See comment for notifier_call_chain.
297 * @nr_calls: See comment for notifier_call_chain.
299 * Calls each function in a notifier chain in turn. The functions
300 * run in a process context, so they are allowed to block.
302 * If the return value of the notifier can be and'ed
303 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
304 * will return immediately, with the return value of
305 * the notifier function which halted execution.
306 * Otherwise the return value is the return value
307 * of the last notifier function called.
309 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
310 unsigned long val, void *v,
311 int nr_to_call, int *nr_calls)
313 int ret = NOTIFY_DONE;
316 * We check the head outside the lock, but if this access is
317 * racy then it does not matter what the result of the test
318 * is, we re-check the list after having taken the lock anyway:
320 if (rcu_access_pointer(nh->head)) {
321 down_read(&nh->rwsem);
322 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
328 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
330 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
331 unsigned long val, void *v)
333 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
335 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
338 * Raw notifier chain routines. There is no protection;
339 * the caller must provide it. Use at your own risk!
343 * raw_notifier_chain_register - Add notifier to a raw notifier chain
344 * @nh: Pointer to head of the raw notifier chain
345 * @n: New entry in notifier chain
347 * Adds a notifier to a raw notifier chain.
348 * All locking must be provided by the caller.
350 * Currently always returns zero.
352 int raw_notifier_chain_register(struct raw_notifier_head *nh,
353 struct notifier_block *n)
355 return notifier_chain_register(&nh->head, n);
357 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
360 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
361 * @nh: Pointer to head of the raw notifier chain
362 * @n: Entry to remove from notifier chain
364 * Removes a notifier from a raw notifier chain.
365 * All locking must be provided by the caller.
367 * Returns zero on success or %-ENOENT on failure.
369 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
370 struct notifier_block *n)
372 return notifier_chain_unregister(&nh->head, n);
374 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
377 * __raw_notifier_call_chain - Call functions in a raw notifier chain
378 * @nh: Pointer to head of the raw notifier chain
379 * @val: Value passed unmodified to notifier function
380 * @v: Pointer passed unmodified to notifier function
381 * @nr_to_call: See comment for notifier_call_chain.
382 * @nr_calls: See comment for notifier_call_chain
384 * Calls each function in a notifier chain in turn. The functions
385 * run in an undefined context.
386 * All locking must be provided by the caller.
388 * If the return value of the notifier can be and'ed
389 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
390 * will return immediately, with the return value of
391 * the notifier function which halted execution.
392 * Otherwise the return value is the return value
393 * of the last notifier function called.
395 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
396 unsigned long val, void *v,
397 int nr_to_call, int *nr_calls)
399 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
401 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
403 int raw_notifier_call_chain(struct raw_notifier_head *nh,
404 unsigned long val, void *v)
406 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
408 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
412 * SRCU notifier chain routines. Registration and unregistration
413 * use a mutex, and call_chain is synchronized by SRCU (no locks).
417 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
418 * @nh: Pointer to head of the SRCU notifier chain
419 * @n: New entry in notifier chain
421 * Adds a notifier to an SRCU notifier chain.
422 * Must be called in process context.
424 * Currently always returns zero.
426 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
427 struct notifier_block *n)
432 * This code gets used during boot-up, when task switching is
433 * not yet working and interrupts must remain disabled. At
434 * such times we must not call mutex_lock().
436 if (unlikely(system_state == SYSTEM_BOOTING))
437 return notifier_chain_register(&nh->head, n);
439 mutex_lock(&nh->mutex);
440 ret = notifier_chain_register(&nh->head, n);
441 mutex_unlock(&nh->mutex);
444 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
447 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
448 * @nh: Pointer to head of the SRCU notifier chain
449 * @n: Entry to remove from notifier chain
451 * Removes a notifier from an SRCU notifier chain.
452 * Must be called from process context.
454 * Returns zero on success or %-ENOENT on failure.
456 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
457 struct notifier_block *n)
462 * This code gets used during boot-up, when task switching is
463 * not yet working and interrupts must remain disabled. At
464 * such times we must not call mutex_lock().
466 if (unlikely(system_state == SYSTEM_BOOTING))
467 return notifier_chain_unregister(&nh->head, n);
469 mutex_lock(&nh->mutex);
470 ret = notifier_chain_unregister(&nh->head, n);
471 mutex_unlock(&nh->mutex);
472 synchronize_srcu(&nh->srcu);
475 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
478 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
479 * @nh: Pointer to head of the SRCU notifier chain
480 * @val: Value passed unmodified to notifier function
481 * @v: Pointer passed unmodified to notifier function
482 * @nr_to_call: See comment for notifier_call_chain.
483 * @nr_calls: See comment for notifier_call_chain
485 * Calls each function in a notifier chain in turn. The functions
486 * run in a process context, so they are allowed to block.
488 * If the return value of the notifier can be and'ed
489 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
490 * will return immediately, with the return value of
491 * the notifier function which halted execution.
492 * Otherwise the return value is the return value
493 * of the last notifier function called.
495 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
496 unsigned long val, void *v,
497 int nr_to_call, int *nr_calls)
502 idx = srcu_read_lock(&nh->srcu);
503 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
504 srcu_read_unlock(&nh->srcu, idx);
507 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
509 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
510 unsigned long val, void *v)
512 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
514 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
517 * srcu_init_notifier_head - Initialize an SRCU notifier head
518 * @nh: Pointer to head of the srcu notifier chain
520 * Unlike other sorts of notifier heads, SRCU notifier heads require
521 * dynamic initialization. Be sure to call this routine before
522 * calling any of the other SRCU notifier routines for this head.
524 * If an SRCU notifier head is deallocated, it must first be cleaned
525 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
526 * per-cpu data (used by the SRCU mechanism) will leak.
528 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
530 mutex_init(&nh->mutex);
531 if (init_srcu_struct(&nh->srcu) < 0)
535 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
537 #endif /* CONFIG_SRCU */
539 static ATOMIC_NOTIFIER_HEAD(die_chain);
541 int notrace notify_die(enum die_val val, const char *str,
542 struct pt_regs *regs, long err, int trap, int sig)
544 struct die_args args = {
552 RCU_LOCKDEP_WARN(!rcu_is_watching(),
553 "notify_die called but RCU thinks we're quiescent");
554 return atomic_notifier_call_chain(&die_chain, val, &args);
556 NOKPROBE_SYMBOL(notify_die);
558 int register_die_notifier(struct notifier_block *nb)
560 vmalloc_sync_mappings();
561 return atomic_notifier_chain_register(&die_chain, nb);
563 EXPORT_SYMBOL_GPL(register_die_notifier);
565 int unregister_die_notifier(struct notifier_block *nb)
567 return atomic_notifier_chain_unregister(&die_chain, nb);
569 EXPORT_SYMBOL_GPL(unregister_die_notifier);