1 #include <linux/kdebug.h>
2 #include <linux/kprobes.h>
3 #include <linux/module.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 if (!kernel_text_address((unsigned long)n->notifier_call)) {
25 WARN(1, "Invalid notifier registered!");
28 while ((*nl) != NULL) {
29 if (n->priority > (*nl)->priority)
34 rcu_assign_pointer(*nl, n);
38 static int notifier_chain_cond_register(struct notifier_block **nl,
39 struct notifier_block *n)
41 if (!kernel_text_address((unsigned long)n->notifier_call)) {
42 WARN(1, "Invalid notifier registered!");
45 while ((*nl) != NULL) {
48 if (n->priority > (*nl)->priority)
53 rcu_assign_pointer(*nl, n);
57 static int notifier_chain_unregister(struct notifier_block **nl,
58 struct notifier_block *n)
60 while ((*nl) != NULL) {
62 rcu_assign_pointer(*nl, n->next);
71 * notifier_call_chain - Informs the registered notifiers about an event.
72 * @nl: Pointer to head of the blocking notifier chain
73 * @val: Value passed unmodified to notifier function
74 * @v: Pointer passed unmodified to notifier function
75 * @nr_to_call: Number of notifier functions to be called. Don't care
76 * value of this parameter is -1.
77 * @nr_calls: Records the number of notifications sent. Don't care
78 * value of this field is NULL.
79 * @returns: notifier_call_chain returns the value returned by the
80 * last notifier function called.
82 static int __kprobes notifier_call_chain(struct notifier_block **nl,
83 unsigned long val, void *v,
84 int nr_to_call, int *nr_calls)
86 int ret = NOTIFY_DONE;
87 struct notifier_block *nb, *next_nb;
89 nb = rcu_dereference(*nl);
91 while (nb && nr_to_call) {
92 next_nb = rcu_dereference(nb->next);
94 #ifdef CONFIG_DEBUG_NOTIFIERS
95 if (!kernel_text_address((unsigned long)nb->notifier_call)) {
96 WARN(1, "Invalid notifier called!");
101 ret = nb->notifier_call(nb, val, v);
106 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
115 * Atomic notifier chain routines. Registration and unregistration
116 * use a spinlock, and call_chain is synchronized by RCU (no locks).
120 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
121 * @nh: Pointer to head of the atomic notifier chain
122 * @n: New entry in notifier chain
124 * Adds a notifier to an atomic notifier chain.
126 * Currently always returns zero.
128 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
129 struct notifier_block *n)
134 spin_lock_irqsave(&nh->lock, flags);
135 ret = notifier_chain_register(&nh->head, n);
136 spin_unlock_irqrestore(&nh->lock, flags);
139 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
142 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
143 * @nh: Pointer to head of the atomic notifier chain
144 * @n: Entry to remove from notifier chain
146 * Removes a notifier from an atomic notifier chain.
148 * Returns zero on success or %-ENOENT on failure.
150 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
151 struct notifier_block *n)
156 spin_lock_irqsave(&nh->lock, flags);
157 ret = notifier_chain_unregister(&nh->head, n);
158 spin_unlock_irqrestore(&nh->lock, flags);
162 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
165 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
166 * @nh: Pointer to head of the atomic notifier chain
167 * @val: Value passed unmodified to notifier function
168 * @v: Pointer passed unmodified to notifier function
169 * @nr_to_call: See the comment for notifier_call_chain.
170 * @nr_calls: See the comment for notifier_call_chain.
172 * Calls each function in a notifier chain in turn. The functions
173 * run in an atomic context, so they must not block.
174 * This routine uses RCU to synchronize with changes to the chain.
176 * If the return value of the notifier can be and'ed
177 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
178 * will return immediately, with the return value of
179 * the notifier function which halted execution.
180 * Otherwise the return value is the return value
181 * of the last notifier function called.
183 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
184 unsigned long val, void *v,
185 int nr_to_call, int *nr_calls)
190 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
194 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
196 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
197 unsigned long val, void *v)
199 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
201 EXPORT_SYMBOL_GPL(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_dereference(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);
411 * SRCU notifier chain routines. Registration and unregistration
412 * use a mutex, and call_chain is synchronized by SRCU (no locks).
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
420 * Adds a notifier to an SRCU notifier chain.
421 * Must be called in process context.
423 * Currently always returns zero.
425 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
426 struct notifier_block *n)
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().
435 if (unlikely(system_state == SYSTEM_BOOTING))
436 return notifier_chain_register(&nh->head, n);
438 mutex_lock(&nh->mutex);
439 ret = notifier_chain_register(&nh->head, n);
440 mutex_unlock(&nh->mutex);
443 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
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
450 * Removes a notifier from an SRCU notifier chain.
451 * Must be called from process context.
453 * Returns zero on success or %-ENOENT on failure.
455 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
456 struct notifier_block *n)
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().
465 if (unlikely(system_state == SYSTEM_BOOTING))
466 return notifier_chain_unregister(&nh->head, n);
468 mutex_lock(&nh->mutex);
469 ret = notifier_chain_unregister(&nh->head, n);
470 mutex_unlock(&nh->mutex);
471 synchronize_srcu(&nh->srcu);
474 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
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 * @nr_to_call: See comment for notifier_call_chain.
482 * @nr_calls: See comment for notifier_call_chain
484 * Calls each function in a notifier chain in turn. The functions
485 * run in a process context, so they are allowed to block.
487 * If the return value of the notifier can be and'ed
488 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
489 * will return immediately, with the return value of
490 * the notifier function which halted execution.
491 * Otherwise the return value is the return value
492 * of the last notifier function called.
494 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
495 unsigned long val, void *v,
496 int nr_to_call, int *nr_calls)
501 idx = srcu_read_lock(&nh->srcu);
502 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
503 srcu_read_unlock(&nh->srcu, idx);
506 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
508 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
509 unsigned long val, void *v)
511 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
513 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
516 * srcu_init_notifier_head - Initialize an SRCU notifier head
517 * @nh: Pointer to head of the srcu notifier chain
519 * Unlike other sorts of notifier heads, SRCU notifier heads require
520 * dynamic initialization. Be sure to call this routine before
521 * calling any of the other SRCU notifier routines for this head.
523 * If an SRCU notifier head is deallocated, it must first be cleaned
524 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
525 * per-cpu data (used by the SRCU mechanism) will leak.
527 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
529 mutex_init(&nh->mutex);
530 if (init_srcu_struct(&nh->srcu) < 0)
534 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
537 * register_reboot_notifier - Register function to be called at reboot time
538 * @nb: Info about notifier function to be called
540 * Registers a function with the list of functions
541 * to be called at reboot time.
543 * Currently always returns zero, as blocking_notifier_chain_register()
544 * always returns zero.
546 int register_reboot_notifier(struct notifier_block *nb)
548 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
550 EXPORT_SYMBOL(register_reboot_notifier);
553 * unregister_reboot_notifier - Unregister previously registered reboot notifier
554 * @nb: Hook to be unregistered
556 * Unregisters a previously registered reboot
559 * Returns zero on success, or %-ENOENT on failure.
561 int unregister_reboot_notifier(struct notifier_block *nb)
563 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
565 EXPORT_SYMBOL(unregister_reboot_notifier);
567 static ATOMIC_NOTIFIER_HEAD(die_chain);
569 int notify_die(enum die_val val, const char *str,
570 struct pt_regs *regs, long err, int trap, int sig)
572 struct die_args args = {
580 return atomic_notifier_call_chain(&die_chain, val, &args);
583 int register_die_notifier(struct notifier_block *nb)
586 return atomic_notifier_chain_register(&die_chain, nb);
588 EXPORT_SYMBOL_GPL(register_die_notifier);
590 int unregister_die_notifier(struct notifier_block *nb)
592 return atomic_notifier_chain_unregister(&die_chain, nb);
594 EXPORT_SYMBOL_GPL(unregister_die_notifier);