1 #include <linux/kdebug.h>
2 #include <linux/kprobes.h>
3 #include <linux/export.h>
4 #include <linux/notifier.h>
5 #include <linux/rcupdate.h>
6 #include <linux/vmalloc.h>
7 #include <linux/reboot.h>
10 * Notifier list for kernel code which wants to be called
11 * at shutdown. This is used to stop any idling DMA operations
14 BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
17 * Notifier chain core routines. The exported routines below
18 * are layered on top of these, with appropriate locking added.
21 static int notifier_chain_register(struct notifier_block **nl,
22 struct notifier_block *n)
24 while ((*nl) != NULL) {
25 WARN_ONCE(((*nl) == n), "double register detected");
26 if (n->priority > (*nl)->priority)
31 rcu_assign_pointer(*nl, n);
35 static int notifier_chain_cond_register(struct notifier_block **nl,
36 struct notifier_block *n)
38 while ((*nl) != NULL) {
41 if (n->priority > (*nl)->priority)
46 rcu_assign_pointer(*nl, n);
50 static int notifier_chain_unregister(struct notifier_block **nl,
51 struct notifier_block *n)
53 while ((*nl) != NULL) {
55 rcu_assign_pointer(*nl, n->next);
64 * notifier_call_chain - Informs the registered notifiers about an event.
65 * @nl: Pointer to head of the blocking notifier chain
66 * @val: Value passed unmodified to notifier function
67 * @v: Pointer passed unmodified to notifier function
68 * @nr_to_call: Number of notifier functions to be called. Don't care
69 * value of this parameter is -1.
70 * @nr_calls: Records the number of notifications sent. Don't care
71 * value of this field is NULL.
72 * @returns: notifier_call_chain returns the value returned by the
73 * last notifier function called.
75 static int notifier_call_chain(struct notifier_block **nl,
76 unsigned long val, void *v,
77 int nr_to_call, int *nr_calls)
79 int ret = NOTIFY_DONE;
80 struct notifier_block *nb, *next_nb;
82 nb = rcu_dereference_raw(*nl);
84 while (nb && nr_to_call) {
85 next_nb = rcu_dereference_raw(nb->next);
87 #ifdef CONFIG_DEBUG_NOTIFIERS
88 if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
89 WARN(1, "Invalid notifier called!");
94 ret = nb->notifier_call(nb, val, v);
99 if (ret & NOTIFY_STOP_MASK)
106 NOKPROBE_SYMBOL(notifier_call_chain);
109 * Atomic notifier chain routines. Registration and unregistration
110 * use a spinlock, and call_chain is synchronized by RCU (no locks).
114 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
115 * @nh: Pointer to head of the atomic notifier chain
116 * @n: New entry in notifier chain
118 * Adds a notifier to an atomic notifier chain.
120 * Currently always returns zero.
122 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
123 struct notifier_block *n)
128 spin_lock_irqsave(&nh->lock, flags);
129 ret = notifier_chain_register(&nh->head, n);
130 spin_unlock_irqrestore(&nh->lock, flags);
133 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
136 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
137 * @nh: Pointer to head of the atomic notifier chain
138 * @n: Entry to remove from notifier chain
140 * Removes a notifier from an atomic notifier chain.
142 * Returns zero on success or %-ENOENT on failure.
144 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
145 struct notifier_block *n)
150 spin_lock_irqsave(&nh->lock, flags);
151 ret = notifier_chain_unregister(&nh->head, n);
152 spin_unlock_irqrestore(&nh->lock, flags);
156 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
159 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
160 * @nh: Pointer to head of the atomic notifier chain
161 * @val: Value passed unmodified to notifier function
162 * @v: Pointer passed unmodified to notifier function
163 * @nr_to_call: See the comment for notifier_call_chain.
164 * @nr_calls: See the comment for notifier_call_chain.
166 * Calls each function in a notifier chain in turn. The functions
167 * run in an atomic context, so they must not block.
168 * This routine uses RCU to synchronize with changes to the chain.
170 * If the return value of the notifier can be and'ed
171 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
172 * will return immediately, with the return value of
173 * the notifier function which halted execution.
174 * Otherwise the return value is the return value
175 * of the last notifier function called.
177 int __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
178 unsigned long val, void *v,
179 int nr_to_call, int *nr_calls)
184 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
188 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
189 NOKPROBE_SYMBOL(__atomic_notifier_call_chain);
191 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
192 unsigned long val, void *v)
194 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
196 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
197 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
200 * Blocking notifier chain routines. All access to the chain is
201 * synchronized by an rwsem.
205 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
206 * @nh: Pointer to head of the blocking notifier chain
207 * @n: New entry in notifier chain
209 * Adds a notifier to a blocking notifier chain.
210 * Must be called in process context.
212 * Currently always returns zero.
214 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
215 struct notifier_block *n)
220 * This code gets used during boot-up, when task switching is
221 * not yet working and interrupts must remain disabled. At
222 * such times we must not call down_write().
224 if (unlikely(system_state == SYSTEM_BOOTING))
225 return notifier_chain_register(&nh->head, n);
227 down_write(&nh->rwsem);
228 ret = notifier_chain_register(&nh->head, n);
229 up_write(&nh->rwsem);
232 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
235 * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
236 * @nh: Pointer to head of the blocking notifier chain
237 * @n: New entry in notifier chain
239 * Adds a notifier to a blocking notifier chain, only if not already
240 * present in the chain.
241 * Must be called in process context.
243 * Currently always returns zero.
245 int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
246 struct notifier_block *n)
250 down_write(&nh->rwsem);
251 ret = notifier_chain_cond_register(&nh->head, n);
252 up_write(&nh->rwsem);
255 EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
258 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
259 * @nh: Pointer to head of the blocking notifier chain
260 * @n: Entry to remove from notifier chain
262 * Removes a notifier from a blocking notifier chain.
263 * Must be called from process context.
265 * Returns zero on success or %-ENOENT on failure.
267 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
268 struct notifier_block *n)
273 * This code gets used during boot-up, when task switching is
274 * not yet working and interrupts must remain disabled. At
275 * such times we must not call down_write().
277 if (unlikely(system_state == SYSTEM_BOOTING))
278 return notifier_chain_unregister(&nh->head, n);
280 down_write(&nh->rwsem);
281 ret = notifier_chain_unregister(&nh->head, n);
282 up_write(&nh->rwsem);
285 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
288 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
289 * @nh: Pointer to head of the blocking notifier chain
290 * @val: Value passed unmodified to notifier function
291 * @v: Pointer passed unmodified to notifier function
292 * @nr_to_call: See comment for notifier_call_chain.
293 * @nr_calls: See comment for notifier_call_chain.
295 * Calls each function in a notifier chain in turn. The functions
296 * run in a process context, so they are allowed to block.
298 * If the return value of the notifier can be and'ed
299 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
300 * will return immediately, with the return value of
301 * the notifier function which halted execution.
302 * Otherwise the return value is the return value
303 * of the last notifier function called.
305 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
306 unsigned long val, void *v,
307 int nr_to_call, int *nr_calls)
309 int ret = NOTIFY_DONE;
312 * We check the head outside the lock, but if this access is
313 * racy then it does not matter what the result of the test
314 * is, we re-check the list after having taken the lock anyway:
316 if (rcu_access_pointer(nh->head)) {
317 down_read(&nh->rwsem);
318 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
324 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
326 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
327 unsigned long val, void *v)
329 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
331 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
334 * Raw notifier chain routines. There is no protection;
335 * the caller must provide it. Use at your own risk!
339 * raw_notifier_chain_register - Add notifier to a raw notifier chain
340 * @nh: Pointer to head of the raw notifier chain
341 * @n: New entry in notifier chain
343 * Adds a notifier to a raw notifier chain.
344 * All locking must be provided by the caller.
346 * Currently always returns zero.
348 int raw_notifier_chain_register(struct raw_notifier_head *nh,
349 struct notifier_block *n)
351 return notifier_chain_register(&nh->head, n);
353 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
356 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
357 * @nh: Pointer to head of the raw notifier chain
358 * @n: Entry to remove from notifier chain
360 * Removes a notifier from a raw notifier chain.
361 * All locking must be provided by the caller.
363 * Returns zero on success or %-ENOENT on failure.
365 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
366 struct notifier_block *n)
368 return notifier_chain_unregister(&nh->head, n);
370 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
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
377 * @nr_to_call: See comment for notifier_call_chain.
378 * @nr_calls: See comment for notifier_call_chain
380 * Calls each function in a notifier chain in turn. The functions
381 * run in an undefined context.
382 * All locking must be provided by the caller.
384 * If the return value of the notifier can be and'ed
385 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
386 * will return immediately, with the return value of
387 * the notifier function which halted execution.
388 * Otherwise the return value is the return value
389 * of the last notifier function called.
391 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
392 unsigned long val, void *v,
393 int nr_to_call, int *nr_calls)
395 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
397 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
399 int raw_notifier_call_chain(struct raw_notifier_head *nh,
400 unsigned long val, void *v)
402 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
404 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
408 * SRCU notifier chain routines. Registration and unregistration
409 * use a mutex, and call_chain is synchronized by SRCU (no locks).
413 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
414 * @nh: Pointer to head of the SRCU notifier chain
415 * @n: New entry in notifier chain
417 * Adds a notifier to an SRCU notifier chain.
418 * Must be called in process context.
420 * Currently always returns zero.
422 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
423 struct notifier_block *n)
428 * This code gets used during boot-up, when task switching is
429 * not yet working and interrupts must remain disabled. At
430 * such times we must not call mutex_lock().
432 if (unlikely(system_state == SYSTEM_BOOTING))
433 return notifier_chain_register(&nh->head, n);
435 mutex_lock(&nh->mutex);
436 ret = notifier_chain_register(&nh->head, n);
437 mutex_unlock(&nh->mutex);
440 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
443 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
444 * @nh: Pointer to head of the SRCU notifier chain
445 * @n: Entry to remove from notifier chain
447 * Removes a notifier from an SRCU notifier chain.
448 * Must be called from process context.
450 * Returns zero on success or %-ENOENT on failure.
452 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
453 struct notifier_block *n)
458 * This code gets used during boot-up, when task switching is
459 * not yet working and interrupts must remain disabled. At
460 * such times we must not call mutex_lock().
462 if (unlikely(system_state == SYSTEM_BOOTING))
463 return notifier_chain_unregister(&nh->head, n);
465 mutex_lock(&nh->mutex);
466 ret = notifier_chain_unregister(&nh->head, n);
467 mutex_unlock(&nh->mutex);
468 synchronize_srcu(&nh->srcu);
471 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
474 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
475 * @nh: Pointer to head of the SRCU notifier chain
476 * @val: Value passed unmodified to notifier function
477 * @v: Pointer passed unmodified to notifier function
478 * @nr_to_call: See comment for notifier_call_chain.
479 * @nr_calls: See comment for notifier_call_chain
481 * Calls each function in a notifier chain in turn. The functions
482 * run in a process context, so they are allowed to block.
484 * If the return value of the notifier can be and'ed
485 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
486 * will return immediately, with the return value of
487 * the notifier function which halted execution.
488 * Otherwise the return value is the return value
489 * of the last notifier function called.
491 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
492 unsigned long val, void *v,
493 int nr_to_call, int *nr_calls)
498 idx = srcu_read_lock(&nh->srcu);
499 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
500 srcu_read_unlock(&nh->srcu, idx);
503 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
505 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
506 unsigned long val, void *v)
508 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
510 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
513 * srcu_init_notifier_head - Initialize an SRCU notifier head
514 * @nh: Pointer to head of the srcu notifier chain
516 * Unlike other sorts of notifier heads, SRCU notifier heads require
517 * dynamic initialization. Be sure to call this routine before
518 * calling any of the other SRCU notifier routines for this head.
520 * If an SRCU notifier head is deallocated, it must first be cleaned
521 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
522 * per-cpu data (used by the SRCU mechanism) will leak.
524 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
526 mutex_init(&nh->mutex);
527 if (init_srcu_struct(&nh->srcu) < 0)
531 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
533 #endif /* CONFIG_SRCU */
535 static ATOMIC_NOTIFIER_HEAD(die_chain);
537 int notrace notify_die(enum die_val val, const char *str,
538 struct pt_regs *regs, long err, int trap, int sig)
540 struct die_args args = {
548 RCU_LOCKDEP_WARN(!rcu_is_watching(),
549 "notify_die called but RCU thinks we're quiescent");
550 return atomic_notifier_call_chain(&die_chain, val, &args);
552 NOKPROBE_SYMBOL(notify_die);
554 int register_die_notifier(struct notifier_block *nb)
557 return atomic_notifier_chain_register(&die_chain, nb);
559 EXPORT_SYMBOL_GPL(register_die_notifier);
561 int unregister_die_notifier(struct notifier_block *nb)
563 return atomic_notifier_chain_unregister(&die_chain, nb);
565 EXPORT_SYMBOL_GPL(unregister_die_notifier);