[CRYPTO] tcrypt: Shrink speed templates
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / notifier.c
blob643360d1bb144fda223301b348059cd5381eb35e
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_unregister(struct notifier_block **nl,
35 struct notifier_block *n)
37 while ((*nl) != NULL) {
38 if ((*nl) == n) {
39 rcu_assign_pointer(*nl, n->next);
40 return 0;
42 nl = &((*nl)->next);
44 return -ENOENT;
47 /**
48 * notifier_call_chain - Informs the registered notifiers about an event.
49 * @nl: Pointer to head of the blocking notifier chain
50 * @val: Value passed unmodified to notifier function
51 * @v: Pointer passed unmodified to notifier function
52 * @nr_to_call: Number of notifier functions to be called. Don't care
53 * value of this parameter is -1.
54 * @nr_calls: Records the number of notifications sent. Don't care
55 * value of this field is NULL.
56 * @returns: notifier_call_chain returns the value returned by the
57 * last notifier function called.
59 static int __kprobes notifier_call_chain(struct notifier_block **nl,
60 unsigned long val, void *v,
61 int nr_to_call, int *nr_calls)
63 int ret = NOTIFY_DONE;
64 struct notifier_block *nb, *next_nb;
66 nb = rcu_dereference(*nl);
68 while (nb && nr_to_call) {
69 next_nb = rcu_dereference(nb->next);
70 ret = nb->notifier_call(nb, val, v);
72 if (nr_calls)
73 (*nr_calls)++;
75 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
76 break;
77 nb = next_nb;
78 nr_to_call--;
80 return ret;
84 * Atomic notifier chain routines. Registration and unregistration
85 * use a spinlock, and call_chain is synchronized by RCU (no locks).
88 /**
89 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
90 * @nh: Pointer to head of the atomic notifier chain
91 * @n: New entry in notifier chain
93 * Adds a notifier to an atomic notifier chain.
95 * Currently always returns zero.
97 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
98 struct notifier_block *n)
100 unsigned long flags;
101 int ret;
103 spin_lock_irqsave(&nh->lock, flags);
104 ret = notifier_chain_register(&nh->head, n);
105 spin_unlock_irqrestore(&nh->lock, flags);
106 return ret;
108 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
111 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
112 * @nh: Pointer to head of the atomic notifier chain
113 * @n: Entry to remove from notifier chain
115 * Removes a notifier from an atomic notifier chain.
117 * Returns zero on success or %-ENOENT on failure.
119 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
120 struct notifier_block *n)
122 unsigned long flags;
123 int ret;
125 spin_lock_irqsave(&nh->lock, flags);
126 ret = notifier_chain_unregister(&nh->head, n);
127 spin_unlock_irqrestore(&nh->lock, flags);
128 synchronize_rcu();
129 return ret;
131 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
134 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
135 * @nh: Pointer to head of the atomic notifier chain
136 * @val: Value passed unmodified to notifier function
137 * @v: Pointer passed unmodified to notifier function
138 * @nr_to_call: See the comment for notifier_call_chain.
139 * @nr_calls: See the comment for notifier_call_chain.
141 * Calls each function in a notifier chain in turn. The functions
142 * run in an atomic context, so they must not block.
143 * This routine uses RCU to synchronize with changes to the chain.
145 * If the return value of the notifier can be and'ed
146 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
147 * will return immediately, with the return value of
148 * the notifier function which halted execution.
149 * Otherwise the return value is the return value
150 * of the last notifier function called.
152 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
153 unsigned long val, void *v,
154 int nr_to_call, int *nr_calls)
156 int ret;
158 rcu_read_lock();
159 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
160 rcu_read_unlock();
161 return ret;
163 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
165 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
166 unsigned long val, void *v)
168 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
170 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
173 * Blocking notifier chain routines. All access to the chain is
174 * synchronized by an rwsem.
178 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
179 * @nh: Pointer to head of the blocking notifier chain
180 * @n: New entry in notifier chain
182 * Adds a notifier to a blocking notifier chain.
183 * Must be called in process context.
185 * Currently always returns zero.
187 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
188 struct notifier_block *n)
190 int ret;
193 * This code gets used during boot-up, when task switching is
194 * not yet working and interrupts must remain disabled. At
195 * such times we must not call down_write().
197 if (unlikely(system_state == SYSTEM_BOOTING))
198 return notifier_chain_register(&nh->head, n);
200 down_write(&nh->rwsem);
201 ret = notifier_chain_register(&nh->head, n);
202 up_write(&nh->rwsem);
203 return ret;
205 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
208 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
209 * @nh: Pointer to head of the blocking notifier chain
210 * @n: Entry to remove from notifier chain
212 * Removes a notifier from a blocking notifier chain.
213 * Must be called from process context.
215 * Returns zero on success or %-ENOENT on failure.
217 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
218 struct notifier_block *n)
220 int ret;
223 * This code gets used during boot-up, when task switching is
224 * not yet working and interrupts must remain disabled. At
225 * such times we must not call down_write().
227 if (unlikely(system_state == SYSTEM_BOOTING))
228 return notifier_chain_unregister(&nh->head, n);
230 down_write(&nh->rwsem);
231 ret = notifier_chain_unregister(&nh->head, n);
232 up_write(&nh->rwsem);
233 return ret;
235 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
238 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
239 * @nh: Pointer to head of the blocking notifier chain
240 * @val: Value passed unmodified to notifier function
241 * @v: Pointer passed unmodified to notifier function
242 * @nr_to_call: See comment for notifier_call_chain.
243 * @nr_calls: See comment for notifier_call_chain.
245 * Calls each function in a notifier chain in turn. The functions
246 * run in a process context, so they are allowed to block.
248 * If the return value of the notifier can be and'ed
249 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
250 * will return immediately, with the return value of
251 * the notifier function which halted execution.
252 * Otherwise the return value is the return value
253 * of the last notifier function called.
255 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
256 unsigned long val, void *v,
257 int nr_to_call, int *nr_calls)
259 int ret = NOTIFY_DONE;
262 * We check the head outside the lock, but if this access is
263 * racy then it does not matter what the result of the test
264 * is, we re-check the list after having taken the lock anyway:
266 if (rcu_dereference(nh->head)) {
267 down_read(&nh->rwsem);
268 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
269 nr_calls);
270 up_read(&nh->rwsem);
272 return ret;
274 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
276 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
277 unsigned long val, void *v)
279 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
281 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
284 * Raw notifier chain routines. There is no protection;
285 * the caller must provide it. Use at your own risk!
289 * raw_notifier_chain_register - Add notifier to a raw notifier chain
290 * @nh: Pointer to head of the raw notifier chain
291 * @n: New entry in notifier chain
293 * Adds a notifier to a raw notifier chain.
294 * All locking must be provided by the caller.
296 * Currently always returns zero.
298 int raw_notifier_chain_register(struct raw_notifier_head *nh,
299 struct notifier_block *n)
301 return notifier_chain_register(&nh->head, n);
303 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
306 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
307 * @nh: Pointer to head of the raw notifier chain
308 * @n: Entry to remove from notifier chain
310 * Removes a notifier from a raw notifier chain.
311 * All locking must be provided by the caller.
313 * Returns zero on success or %-ENOENT on failure.
315 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
316 struct notifier_block *n)
318 return notifier_chain_unregister(&nh->head, n);
320 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
323 * __raw_notifier_call_chain - Call functions in a raw notifier chain
324 * @nh: Pointer to head of the raw notifier chain
325 * @val: Value passed unmodified to notifier function
326 * @v: Pointer passed unmodified to notifier function
327 * @nr_to_call: See comment for notifier_call_chain.
328 * @nr_calls: See comment for notifier_call_chain
330 * Calls each function in a notifier chain in turn. The functions
331 * run in an undefined context.
332 * All locking must be provided by the caller.
334 * If the return value of the notifier can be and'ed
335 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
336 * will return immediately, with the return value of
337 * the notifier function which halted execution.
338 * Otherwise the return value is the return value
339 * of the last notifier function called.
341 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
342 unsigned long val, void *v,
343 int nr_to_call, int *nr_calls)
345 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
347 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
349 int raw_notifier_call_chain(struct raw_notifier_head *nh,
350 unsigned long val, void *v)
352 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
354 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
357 * SRCU notifier chain routines. Registration and unregistration
358 * use a mutex, and call_chain is synchronized by SRCU (no locks).
362 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
363 * @nh: Pointer to head of the SRCU notifier chain
364 * @n: New entry in notifier chain
366 * Adds a notifier to an SRCU notifier chain.
367 * Must be called in process context.
369 * Currently always returns zero.
371 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
372 struct notifier_block *n)
374 int ret;
377 * This code gets used during boot-up, when task switching is
378 * not yet working and interrupts must remain disabled. At
379 * such times we must not call mutex_lock().
381 if (unlikely(system_state == SYSTEM_BOOTING))
382 return notifier_chain_register(&nh->head, n);
384 mutex_lock(&nh->mutex);
385 ret = notifier_chain_register(&nh->head, n);
386 mutex_unlock(&nh->mutex);
387 return ret;
389 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
392 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
393 * @nh: Pointer to head of the SRCU notifier chain
394 * @n: Entry to remove from notifier chain
396 * Removes a notifier from an SRCU notifier chain.
397 * Must be called from process context.
399 * Returns zero on success or %-ENOENT on failure.
401 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
402 struct notifier_block *n)
404 int ret;
407 * This code gets used during boot-up, when task switching is
408 * not yet working and interrupts must remain disabled. At
409 * such times we must not call mutex_lock().
411 if (unlikely(system_state == SYSTEM_BOOTING))
412 return notifier_chain_unregister(&nh->head, n);
414 mutex_lock(&nh->mutex);
415 ret = notifier_chain_unregister(&nh->head, n);
416 mutex_unlock(&nh->mutex);
417 synchronize_srcu(&nh->srcu);
418 return ret;
420 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
423 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
424 * @nh: Pointer to head of the SRCU notifier chain
425 * @val: Value passed unmodified to notifier function
426 * @v: Pointer passed unmodified to notifier function
427 * @nr_to_call: See comment for notifier_call_chain.
428 * @nr_calls: See comment for notifier_call_chain
430 * Calls each function in a notifier chain in turn. The functions
431 * run in a process context, so they are allowed to block.
433 * If the return value of the notifier can be and'ed
434 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
435 * will return immediately, with the return value of
436 * the notifier function which halted execution.
437 * Otherwise the return value is the return value
438 * of the last notifier function called.
440 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
441 unsigned long val, void *v,
442 int nr_to_call, int *nr_calls)
444 int ret;
445 int idx;
447 idx = srcu_read_lock(&nh->srcu);
448 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
449 srcu_read_unlock(&nh->srcu, idx);
450 return ret;
452 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
454 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
455 unsigned long val, void *v)
457 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
459 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
462 * srcu_init_notifier_head - Initialize an SRCU notifier head
463 * @nh: Pointer to head of the srcu notifier chain
465 * Unlike other sorts of notifier heads, SRCU notifier heads require
466 * dynamic initialization. Be sure to call this routine before
467 * calling any of the other SRCU notifier routines for this head.
469 * If an SRCU notifier head is deallocated, it must first be cleaned
470 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
471 * per-cpu data (used by the SRCU mechanism) will leak.
473 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
475 mutex_init(&nh->mutex);
476 if (init_srcu_struct(&nh->srcu) < 0)
477 BUG();
478 nh->head = NULL;
480 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
483 * register_reboot_notifier - Register function to be called at reboot time
484 * @nb: Info about notifier function to be called
486 * Registers a function with the list of functions
487 * to be called at reboot time.
489 * Currently always returns zero, as blocking_notifier_chain_register()
490 * always returns zero.
492 int register_reboot_notifier(struct notifier_block *nb)
494 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
496 EXPORT_SYMBOL(register_reboot_notifier);
499 * unregister_reboot_notifier - Unregister previously registered reboot notifier
500 * @nb: Hook to be unregistered
502 * Unregisters a previously registered reboot
503 * notifier function.
505 * Returns zero on success, or %-ENOENT on failure.
507 int unregister_reboot_notifier(struct notifier_block *nb)
509 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
511 EXPORT_SYMBOL(unregister_reboot_notifier);
513 static ATOMIC_NOTIFIER_HEAD(die_chain);
515 int notify_die(enum die_val val, const char *str,
516 struct pt_regs *regs, long err, int trap, int sig)
518 struct die_args args = {
519 .regs = regs,
520 .str = str,
521 .err = err,
522 .trapnr = trap,
523 .signr = sig,
526 return atomic_notifier_call_chain(&die_chain, val, &args);
529 int register_die_notifier(struct notifier_block *nb)
531 vmalloc_sync_all();
532 return atomic_notifier_chain_register(&die_chain, nb);
534 EXPORT_SYMBOL_GPL(register_die_notifier);
536 int unregister_die_notifier(struct notifier_block *nb)
538 return atomic_notifier_chain_unregister(&die_chain, nb);
540 EXPORT_SYMBOL_GPL(unregister_die_notifier);