Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-mips/linux-dm7025.git] / kernel / notifier.c
blob823be11584efef8ef1d344f484cbf8c3d4f9617e
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>
9 /*
10 * Notifier list for kernel code which wants to be called
11 * at shutdown. This is used to stop any idling DMA operations
12 * and the like.
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 if (n->priority > (*nl)->priority)
26 break;
27 nl = &((*nl)->next);
29 n->next = *nl;
30 rcu_assign_pointer(*nl, n);
31 return 0;
34 static int notifier_chain_cond_register(struct notifier_block **nl,
35 struct notifier_block *n)
37 while ((*nl) != NULL) {
38 if ((*nl) == n)
39 return 0;
40 if (n->priority > (*nl)->priority)
41 break;
42 nl = &((*nl)->next);
44 n->next = *nl;
45 rcu_assign_pointer(*nl, n);
46 return 0;
49 static int notifier_chain_unregister(struct notifier_block **nl,
50 struct notifier_block *n)
52 while ((*nl) != NULL) {
53 if ((*nl) == n) {
54 rcu_assign_pointer(*nl, n->next);
55 return 0;
57 nl = &((*nl)->next);
59 return -ENOENT;
62 /**
63 * notifier_call_chain - Informs the registered notifiers about an event.
64 * @nl: Pointer to head of the blocking notifier chain
65 * @val: Value passed unmodified to notifier function
66 * @v: Pointer passed unmodified to notifier function
67 * @nr_to_call: Number of notifier functions to be called. Don't care
68 * value of this parameter is -1.
69 * @nr_calls: Records the number of notifications sent. Don't care
70 * value of this field is NULL.
71 * @returns: notifier_call_chain returns the value returned by the
72 * last notifier function called.
74 static int __kprobes notifier_call_chain(struct notifier_block **nl,
75 unsigned long val, void *v,
76 int nr_to_call, int *nr_calls)
78 int ret = NOTIFY_DONE;
79 struct notifier_block *nb, *next_nb;
81 nb = rcu_dereference(*nl);
83 while (nb && nr_to_call) {
84 next_nb = rcu_dereference(nb->next);
85 ret = nb->notifier_call(nb, val, v);
87 if (nr_calls)
88 (*nr_calls)++;
90 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
91 break;
92 nb = next_nb;
93 nr_to_call--;
95 return ret;
99 * Atomic notifier chain routines. Registration and unregistration
100 * use a spinlock, and call_chain is synchronized by RCU (no locks).
104 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
105 * @nh: Pointer to head of the atomic notifier chain
106 * @n: New entry in notifier chain
108 * Adds a notifier to an atomic notifier chain.
110 * Currently always returns zero.
112 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
113 struct notifier_block *n)
115 unsigned long flags;
116 int ret;
118 spin_lock_irqsave(&nh->lock, flags);
119 ret = notifier_chain_register(&nh->head, n);
120 spin_unlock_irqrestore(&nh->lock, flags);
121 return ret;
123 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
126 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
127 * @nh: Pointer to head of the atomic notifier chain
128 * @n: Entry to remove from notifier chain
130 * Removes a notifier from an atomic notifier chain.
132 * Returns zero on success or %-ENOENT on failure.
134 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
135 struct notifier_block *n)
137 unsigned long flags;
138 int ret;
140 spin_lock_irqsave(&nh->lock, flags);
141 ret = notifier_chain_unregister(&nh->head, n);
142 spin_unlock_irqrestore(&nh->lock, flags);
143 synchronize_rcu();
144 return ret;
146 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
149 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
150 * @nh: Pointer to head of the atomic notifier chain
151 * @val: Value passed unmodified to notifier function
152 * @v: Pointer passed unmodified to notifier function
153 * @nr_to_call: See the comment for notifier_call_chain.
154 * @nr_calls: See the comment for notifier_call_chain.
156 * Calls each function in a notifier chain in turn. The functions
157 * run in an atomic context, so they must not block.
158 * This routine uses RCU to synchronize with changes to the chain.
160 * If the return value of the notifier can be and'ed
161 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
162 * will return immediately, with the return value of
163 * the notifier function which halted execution.
164 * Otherwise the return value is the return value
165 * of the last notifier function called.
167 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
168 unsigned long val, void *v,
169 int nr_to_call, int *nr_calls)
171 int ret;
173 rcu_read_lock();
174 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
175 rcu_read_unlock();
176 return ret;
178 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
180 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
181 unsigned long val, void *v)
183 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
185 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
188 * Blocking notifier chain routines. All access to the chain is
189 * synchronized by an rwsem.
193 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
194 * @nh: Pointer to head of the blocking notifier chain
195 * @n: New entry in notifier chain
197 * Adds a notifier to a blocking notifier chain.
198 * Must be called in process context.
200 * Currently always returns zero.
202 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
203 struct notifier_block *n)
205 int ret;
208 * This code gets used during boot-up, when task switching is
209 * not yet working and interrupts must remain disabled. At
210 * such times we must not call down_write().
212 if (unlikely(system_state == SYSTEM_BOOTING))
213 return notifier_chain_register(&nh->head, n);
215 down_write(&nh->rwsem);
216 ret = notifier_chain_register(&nh->head, n);
217 up_write(&nh->rwsem);
218 return ret;
220 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
223 * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
224 * @nh: Pointer to head of the blocking notifier chain
225 * @n: New entry in notifier chain
227 * Adds a notifier to a blocking notifier chain, only if not already
228 * present in the chain.
229 * Must be called in process context.
231 * Currently always returns zero.
233 int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
234 struct notifier_block *n)
236 int ret;
238 down_write(&nh->rwsem);
239 ret = notifier_chain_cond_register(&nh->head, n);
240 up_write(&nh->rwsem);
241 return ret;
243 EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
246 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
247 * @nh: Pointer to head of the blocking notifier chain
248 * @n: Entry to remove from notifier chain
250 * Removes a notifier from a blocking notifier chain.
251 * Must be called from process context.
253 * Returns zero on success or %-ENOENT on failure.
255 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
256 struct notifier_block *n)
258 int ret;
261 * This code gets used during boot-up, when task switching is
262 * not yet working and interrupts must remain disabled. At
263 * such times we must not call down_write().
265 if (unlikely(system_state == SYSTEM_BOOTING))
266 return notifier_chain_unregister(&nh->head, n);
268 down_write(&nh->rwsem);
269 ret = notifier_chain_unregister(&nh->head, n);
270 up_write(&nh->rwsem);
271 return ret;
273 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
276 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
277 * @nh: Pointer to head of the blocking notifier chain
278 * @val: Value passed unmodified to notifier function
279 * @v: Pointer passed unmodified to notifier function
280 * @nr_to_call: See comment for notifier_call_chain.
281 * @nr_calls: See comment for notifier_call_chain.
283 * Calls each function in a notifier chain in turn. The functions
284 * run in a process context, so they are allowed to block.
286 * If the return value of the notifier can be and'ed
287 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
288 * will return immediately, with the return value of
289 * the notifier function which halted execution.
290 * Otherwise the return value is the return value
291 * of the last notifier function called.
293 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
294 unsigned long val, void *v,
295 int nr_to_call, int *nr_calls)
297 int ret = NOTIFY_DONE;
300 * We check the head outside the lock, but if this access is
301 * racy then it does not matter what the result of the test
302 * is, we re-check the list after having taken the lock anyway:
304 if (rcu_dereference(nh->head)) {
305 down_read(&nh->rwsem);
306 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
307 nr_calls);
308 up_read(&nh->rwsem);
310 return ret;
312 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
314 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
315 unsigned long val, void *v)
317 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
319 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
322 * Raw notifier chain routines. There is no protection;
323 * the caller must provide it. Use at your own risk!
327 * raw_notifier_chain_register - Add notifier to a raw notifier chain
328 * @nh: Pointer to head of the raw notifier chain
329 * @n: New entry in notifier chain
331 * Adds a notifier to a raw notifier chain.
332 * All locking must be provided by the caller.
334 * Currently always returns zero.
336 int raw_notifier_chain_register(struct raw_notifier_head *nh,
337 struct notifier_block *n)
339 return notifier_chain_register(&nh->head, n);
341 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
344 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
345 * @nh: Pointer to head of the raw notifier chain
346 * @n: Entry to remove from notifier chain
348 * Removes a notifier from a raw notifier chain.
349 * All locking must be provided by the caller.
351 * Returns zero on success or %-ENOENT on failure.
353 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
354 struct notifier_block *n)
356 return notifier_chain_unregister(&nh->head, n);
358 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
361 * __raw_notifier_call_chain - Call functions in a raw notifier chain
362 * @nh: Pointer to head of the raw notifier chain
363 * @val: Value passed unmodified to notifier function
364 * @v: Pointer passed unmodified to notifier function
365 * @nr_to_call: See comment for notifier_call_chain.
366 * @nr_calls: See comment for notifier_call_chain
368 * Calls each function in a notifier chain in turn. The functions
369 * run in an undefined context.
370 * All locking must be provided by the caller.
372 * If the return value of the notifier can be and'ed
373 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
374 * will return immediately, with the return value of
375 * the notifier function which halted execution.
376 * Otherwise the return value is the return value
377 * of the last notifier function called.
379 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
380 unsigned long val, void *v,
381 int nr_to_call, int *nr_calls)
383 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
385 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
387 int raw_notifier_call_chain(struct raw_notifier_head *nh,
388 unsigned long val, void *v)
390 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
392 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
395 * SRCU notifier chain routines. Registration and unregistration
396 * use a mutex, and call_chain is synchronized by SRCU (no locks).
400 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
401 * @nh: Pointer to head of the SRCU notifier chain
402 * @n: New entry in notifier chain
404 * Adds a notifier to an SRCU notifier chain.
405 * Must be called in process context.
407 * Currently always returns zero.
409 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
410 struct notifier_block *n)
412 int ret;
415 * This code gets used during boot-up, when task switching is
416 * not yet working and interrupts must remain disabled. At
417 * such times we must not call mutex_lock().
419 if (unlikely(system_state == SYSTEM_BOOTING))
420 return notifier_chain_register(&nh->head, n);
422 mutex_lock(&nh->mutex);
423 ret = notifier_chain_register(&nh->head, n);
424 mutex_unlock(&nh->mutex);
425 return ret;
427 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
430 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
431 * @nh: Pointer to head of the SRCU notifier chain
432 * @n: Entry to remove from notifier chain
434 * Removes a notifier from an SRCU notifier chain.
435 * Must be called from process context.
437 * Returns zero on success or %-ENOENT on failure.
439 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
440 struct notifier_block *n)
442 int ret;
445 * This code gets used during boot-up, when task switching is
446 * not yet working and interrupts must remain disabled. At
447 * such times we must not call mutex_lock().
449 if (unlikely(system_state == SYSTEM_BOOTING))
450 return notifier_chain_unregister(&nh->head, n);
452 mutex_lock(&nh->mutex);
453 ret = notifier_chain_unregister(&nh->head, n);
454 mutex_unlock(&nh->mutex);
455 synchronize_srcu(&nh->srcu);
456 return ret;
458 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
461 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
462 * @nh: Pointer to head of the SRCU notifier chain
463 * @val: Value passed unmodified to notifier function
464 * @v: Pointer passed unmodified to notifier function
465 * @nr_to_call: See comment for notifier_call_chain.
466 * @nr_calls: See comment for notifier_call_chain
468 * Calls each function in a notifier chain in turn. The functions
469 * run in a process context, so they are allowed to block.
471 * If the return value of the notifier can be and'ed
472 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
473 * will return immediately, with the return value of
474 * the notifier function which halted execution.
475 * Otherwise the return value is the return value
476 * of the last notifier function called.
478 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
479 unsigned long val, void *v,
480 int nr_to_call, int *nr_calls)
482 int ret;
483 int idx;
485 idx = srcu_read_lock(&nh->srcu);
486 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
487 srcu_read_unlock(&nh->srcu, idx);
488 return ret;
490 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
492 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
493 unsigned long val, void *v)
495 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
497 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
500 * srcu_init_notifier_head - Initialize an SRCU notifier head
501 * @nh: Pointer to head of the srcu notifier chain
503 * Unlike other sorts of notifier heads, SRCU notifier heads require
504 * dynamic initialization. Be sure to call this routine before
505 * calling any of the other SRCU notifier routines for this head.
507 * If an SRCU notifier head is deallocated, it must first be cleaned
508 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
509 * per-cpu data (used by the SRCU mechanism) will leak.
511 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
513 mutex_init(&nh->mutex);
514 if (init_srcu_struct(&nh->srcu) < 0)
515 BUG();
516 nh->head = NULL;
518 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
521 * register_reboot_notifier - Register function to be called at reboot time
522 * @nb: Info about notifier function to be called
524 * Registers a function with the list of functions
525 * to be called at reboot time.
527 * Currently always returns zero, as blocking_notifier_chain_register()
528 * always returns zero.
530 int register_reboot_notifier(struct notifier_block *nb)
532 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
534 EXPORT_SYMBOL(register_reboot_notifier);
537 * unregister_reboot_notifier - Unregister previously registered reboot notifier
538 * @nb: Hook to be unregistered
540 * Unregisters a previously registered reboot
541 * notifier function.
543 * Returns zero on success, or %-ENOENT on failure.
545 int unregister_reboot_notifier(struct notifier_block *nb)
547 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
549 EXPORT_SYMBOL(unregister_reboot_notifier);
551 static ATOMIC_NOTIFIER_HEAD(die_chain);
553 int notify_die(enum die_val val, const char *str,
554 struct pt_regs *regs, long err, int trap, int sig)
556 struct die_args args = {
557 .regs = regs,
558 .str = str,
559 .err = err,
560 .trapnr = trap,
561 .signr = sig,
564 return atomic_notifier_call_chain(&die_chain, val, &args);
567 int register_die_notifier(struct notifier_block *nb)
569 vmalloc_sync_all();
570 return atomic_notifier_chain_register(&die_chain, nb);
572 EXPORT_SYMBOL_GPL(register_die_notifier);
574 int unregister_die_notifier(struct notifier_block *nb)
576 return atomic_notifier_chain_unregister(&die_chain, nb);
578 EXPORT_SYMBOL_GPL(unregister_die_notifier);