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, "notifier callback %ps already registered",
31 if (n->priority > (*nl)->priority)
36 rcu_assign_pointer(*nl, n);
40 static int notifier_chain_unregister(struct notifier_block **nl,
41 struct notifier_block *n)
43 while ((*nl) != NULL) {
45 rcu_assign_pointer(*nl, n->next);
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.
65 static int notifier_call_chain(struct notifier_block **nl,
66 unsigned long val, void *v,
67 int nr_to_call, int *nr_calls)
69 int ret = NOTIFY_DONE;
70 struct notifier_block *nb, *next_nb;
72 nb = rcu_dereference_raw(*nl);
74 while (nb && nr_to_call) {
75 next_nb = rcu_dereference_raw(nb->next);
77 #ifdef CONFIG_DEBUG_NOTIFIERS
78 if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
79 WARN(1, "Invalid notifier called!");
84 ret = nb->notifier_call(nb, val, v);
89 if (ret & NOTIFY_STOP_MASK)
96 NOKPROBE_SYMBOL(notifier_call_chain);
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
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.
111 * Returns: the return value of the @val_up call.
113 static int notifier_call_chain_robust(struct notifier_block **nl,
114 unsigned long val_up, unsigned long val_down,
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);
127 * Atomic notifier chain routines. Registration and unregistration
128 * use a spinlock, and call_chain is synchronized by RCU (no locks).
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
136 * Adds a notifier to an atomic notifier chain.
138 * Returns 0 on success, %-EEXIST on error.
140 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
141 struct notifier_block *n)
146 spin_lock_irqsave(&nh->lock, flags);
147 ret = notifier_chain_register(&nh->head, n);
148 spin_unlock_irqrestore(&nh->lock, flags);
151 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
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
158 * Removes a notifier from an atomic notifier chain.
160 * Returns zero on success or %-ENOENT on failure.
162 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
163 struct notifier_block *n)
168 spin_lock_irqsave(&nh->lock, flags);
169 ret = notifier_chain_unregister(&nh->head, n);
170 spin_unlock_irqrestore(&nh->lock, flags);
174 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
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
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.
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.
193 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
194 unsigned long val, void *v)
199 ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
204 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
205 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
208 * Blocking notifier chain routines. All access to the chain is
209 * synchronized by an rwsem.
213 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
214 * @nh: Pointer to head of the blocking notifier chain
215 * @n: New entry in notifier chain
217 * Adds a notifier to a blocking notifier chain.
218 * Must be called in process context.
220 * Returns 0 on success, %-EEXIST on error.
222 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
223 struct notifier_block *n)
228 * This code gets used during boot-up, when task switching is
229 * not yet working and interrupts must remain disabled. At
230 * such times we must not call down_write().
232 if (unlikely(system_state == SYSTEM_BOOTING))
233 return notifier_chain_register(&nh->head, n);
235 down_write(&nh->rwsem);
236 ret = notifier_chain_register(&nh->head, n);
237 up_write(&nh->rwsem);
240 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
243 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
244 * @nh: Pointer to head of the blocking notifier chain
245 * @n: Entry to remove from notifier chain
247 * Removes a notifier from a blocking notifier chain.
248 * Must be called from process context.
250 * Returns zero on success or %-ENOENT on failure.
252 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
253 struct notifier_block *n)
258 * This code gets used during boot-up, when task switching is
259 * not yet working and interrupts must remain disabled. At
260 * such times we must not call down_write().
262 if (unlikely(system_state == SYSTEM_BOOTING))
263 return notifier_chain_unregister(&nh->head, n);
265 down_write(&nh->rwsem);
266 ret = notifier_chain_unregister(&nh->head, n);
267 up_write(&nh->rwsem);
270 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
272 int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
273 unsigned long val_up, unsigned long val_down, void *v)
275 int ret = NOTIFY_DONE;
278 * We check the head outside the lock, but if this access is
279 * racy then it does not matter what the result of the test
280 * is, we re-check the list after having taken the lock anyway:
282 if (rcu_access_pointer(nh->head)) {
283 down_read(&nh->rwsem);
284 ret = notifier_call_chain_robust(&nh->head, val_up, val_down, v);
289 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_robust);
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
297 * Calls each function in a notifier chain in turn. The functions
298 * run in a process context, so they are allowed to block.
300 * If the return value of the notifier can be and'ed
301 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
302 * will return immediately, with the return value of
303 * the notifier function which halted execution.
304 * Otherwise the return value is the return value
305 * of the last notifier function called.
307 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
308 unsigned long val, void *v)
310 int ret = NOTIFY_DONE;
313 * We check the head outside the lock, but if this access is
314 * racy then it does not matter what the result of the test
315 * is, we re-check the list after having taken the lock anyway:
317 if (rcu_access_pointer(nh->head)) {
318 down_read(&nh->rwsem);
319 ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
324 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
327 * Raw notifier chain routines. There is no protection;
328 * the caller must provide it. Use at your own risk!
332 * raw_notifier_chain_register - Add notifier to a raw notifier chain
333 * @nh: Pointer to head of the raw notifier chain
334 * @n: New entry in notifier chain
336 * Adds a notifier to a raw notifier chain.
337 * All locking must be provided by the caller.
339 * Returns 0 on success, %-EEXIST on error.
341 int raw_notifier_chain_register(struct raw_notifier_head *nh,
342 struct notifier_block *n)
344 return notifier_chain_register(&nh->head, n);
346 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
349 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
350 * @nh: Pointer to head of the raw notifier chain
351 * @n: Entry to remove from notifier chain
353 * Removes a notifier from a raw notifier chain.
354 * All locking must be provided by the caller.
356 * Returns zero on success or %-ENOENT on failure.
358 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
359 struct notifier_block *n)
361 return notifier_chain_unregister(&nh->head, n);
363 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
365 int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
366 unsigned long val_up, unsigned long val_down, void *v)
368 return notifier_call_chain_robust(&nh->head, val_up, val_down, v);
370 EXPORT_SYMBOL_GPL(raw_notifier_call_chain_robust);
373 * raw_notifier_call_chain - Call functions in a raw notifier chain
374 * @nh: Pointer to head of the raw notifier chain
375 * @val: Value passed unmodified to notifier function
376 * @v: Pointer passed unmodified to notifier function
378 * Calls each function in a notifier chain in turn. The functions
379 * run in an undefined context.
380 * All locking must be provided by the caller.
382 * If the return value of the notifier can be and'ed
383 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
384 * will return immediately, with the return value of
385 * the notifier function which halted execution.
386 * Otherwise the return value is the return value
387 * of the last notifier function called.
389 int raw_notifier_call_chain(struct raw_notifier_head *nh,
390 unsigned long val, void *v)
392 return notifier_call_chain(&nh->head, val, v, -1, NULL);
394 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
398 * SRCU notifier chain routines. Registration and unregistration
399 * use a mutex, and call_chain is synchronized by SRCU (no locks).
403 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
404 * @nh: Pointer to head of the SRCU notifier chain
405 * @n: New entry in notifier chain
407 * Adds a notifier to an SRCU notifier chain.
408 * Must be called in process context.
410 * Returns 0 on success, %-EEXIST on error.
412 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
413 struct notifier_block *n)
418 * This code gets used during boot-up, when task switching is
419 * not yet working and interrupts must remain disabled. At
420 * such times we must not call mutex_lock().
422 if (unlikely(system_state == SYSTEM_BOOTING))
423 return notifier_chain_register(&nh->head, n);
425 mutex_lock(&nh->mutex);
426 ret = notifier_chain_register(&nh->head, n);
427 mutex_unlock(&nh->mutex);
430 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
433 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
434 * @nh: Pointer to head of the SRCU notifier chain
435 * @n: Entry to remove from notifier chain
437 * Removes a notifier from an SRCU notifier chain.
438 * Must be called from process context.
440 * Returns zero on success or %-ENOENT on failure.
442 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
443 struct notifier_block *n)
448 * This code gets used during boot-up, when task switching is
449 * not yet working and interrupts must remain disabled. At
450 * such times we must not call mutex_lock().
452 if (unlikely(system_state == SYSTEM_BOOTING))
453 return notifier_chain_unregister(&nh->head, n);
455 mutex_lock(&nh->mutex);
456 ret = notifier_chain_unregister(&nh->head, n);
457 mutex_unlock(&nh->mutex);
458 synchronize_srcu(&nh->srcu);
461 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
464 * srcu_notifier_call_chain - Call functions in an SRCU notifier chain
465 * @nh: Pointer to head of the SRCU notifier chain
466 * @val: Value passed unmodified to notifier function
467 * @v: Pointer passed unmodified to notifier function
469 * Calls each function in a notifier chain in turn. The functions
470 * run in a process context, so they are allowed to block.
472 * If the return value of the notifier can be and'ed
473 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
474 * will return immediately, with the return value of
475 * the notifier function which halted execution.
476 * Otherwise the return value is the return value
477 * of the last notifier function called.
479 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
480 unsigned long val, void *v)
485 idx = srcu_read_lock(&nh->srcu);
486 ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
487 srcu_read_unlock(&nh->srcu, idx);
490 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
493 * srcu_init_notifier_head - Initialize an SRCU notifier head
494 * @nh: Pointer to head of the srcu notifier chain
496 * Unlike other sorts of notifier heads, SRCU notifier heads require
497 * dynamic initialization. Be sure to call this routine before
498 * calling any of the other SRCU notifier routines for this head.
500 * If an SRCU notifier head is deallocated, it must first be cleaned
501 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
502 * per-cpu data (used by the SRCU mechanism) will leak.
504 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
506 mutex_init(&nh->mutex);
507 if (init_srcu_struct(&nh->srcu) < 0)
511 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
513 #endif /* CONFIG_SRCU */
515 static ATOMIC_NOTIFIER_HEAD(die_chain);
517 int notrace notify_die(enum die_val val, const char *str,
518 struct pt_regs *regs, long err, int trap, int sig)
520 struct die_args args = {
528 RCU_LOCKDEP_WARN(!rcu_is_watching(),
529 "notify_die called but RCU thinks we're quiescent");
530 return atomic_notifier_call_chain(&die_chain, val, &args);
532 NOKPROBE_SYMBOL(notify_die);
534 int register_die_notifier(struct notifier_block *nb)
536 return atomic_notifier_chain_register(&die_chain, nb);
538 EXPORT_SYMBOL_GPL(register_die_notifier);
540 int unregister_die_notifier(struct notifier_block *nb)
542 return atomic_notifier_chain_unregister(&die_chain, nb);
544 EXPORT_SYMBOL_GPL(unregister_die_notifier);