blob: 823be11584efef8ef1d344f484cbf8c3d4f9617e [file] [log] [blame]
Alexey Dobriyanfe9d4f52007-10-18 23:39:16 -07001#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>
Adrian Bunkc166f232008-02-06 01:36:46 -08007#include <linux/reboot.h>
Alexey Dobriyanfe9d4f52007-10-18 23:39:16 -07008
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.
13 */
14BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
15
16/*
17 * Notifier chain core routines. The exported routines below
18 * are layered on top of these, with appropriate locking added.
19 */
20
21static int notifier_chain_register(struct notifier_block **nl,
22 struct notifier_block *n)
23{
24 while ((*nl) != NULL) {
25 if (n->priority > (*nl)->priority)
26 break;
27 nl = &((*nl)->next);
28 }
29 n->next = *nl;
30 rcu_assign_pointer(*nl, n);
31 return 0;
32}
33
Nadia Derbey6546bc42008-04-29 01:00:45 -070034static int notifier_chain_cond_register(struct notifier_block **nl,
35 struct notifier_block *n)
36{
37 while ((*nl) != NULL) {
38 if ((*nl) == n)
39 return 0;
40 if (n->priority > (*nl)->priority)
41 break;
42 nl = &((*nl)->next);
43 }
44 n->next = *nl;
45 rcu_assign_pointer(*nl, n);
46 return 0;
47}
48
Alexey Dobriyanfe9d4f52007-10-18 23:39:16 -070049static int notifier_chain_unregister(struct notifier_block **nl,
50 struct notifier_block *n)
51{
52 while ((*nl) != NULL) {
53 if ((*nl) == n) {
54 rcu_assign_pointer(*nl, n->next);
55 return 0;
56 }
57 nl = &((*nl)->next);
58 }
59 return -ENOENT;
60}
61
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.
73 */
74static int __kprobes notifier_call_chain(struct notifier_block **nl,
75 unsigned long val, void *v,
76 int nr_to_call, int *nr_calls)
77{
78 int ret = NOTIFY_DONE;
79 struct notifier_block *nb, *next_nb;
80
81 nb = rcu_dereference(*nl);
82
83 while (nb && nr_to_call) {
84 next_nb = rcu_dereference(nb->next);
85 ret = nb->notifier_call(nb, val, v);
86
87 if (nr_calls)
88 (*nr_calls)++;
89
90 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
91 break;
92 nb = next_nb;
93 nr_to_call--;
94 }
95 return ret;
96}
97
98/*
99 * Atomic notifier chain routines. Registration and unregistration
100 * use a spinlock, and call_chain is synchronized by RCU (no locks).
101 */
102
103/**
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
107 *
108 * Adds a notifier to an atomic notifier chain.
109 *
110 * Currently always returns zero.
111 */
112int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
113 struct notifier_block *n)
114{
115 unsigned long flags;
116 int ret;
117
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;
122}
123EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
124
125/**
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
129 *
130 * Removes a notifier from an atomic notifier chain.
131 *
132 * Returns zero on success or %-ENOENT on failure.
133 */
134int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
135 struct notifier_block *n)
136{
137 unsigned long flags;
138 int ret;
139
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;
145}
146EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
147
148/**
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.
155 *
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.
159 *
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.
166 */
167int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
168 unsigned long val, void *v,
169 int nr_to_call, int *nr_calls)
170{
171 int ret;
172
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;
177}
178EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
179
180int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
181 unsigned long val, void *v)
182{
183 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
184}
185EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
186
187/*
188 * Blocking notifier chain routines. All access to the chain is
189 * synchronized by an rwsem.
190 */
191
192/**
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
196 *
197 * Adds a notifier to a blocking notifier chain.
198 * Must be called in process context.
199 *
200 * Currently always returns zero.
201 */
202int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
203 struct notifier_block *n)
204{
205 int ret;
206
207 /*
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().
211 */
212 if (unlikely(system_state == SYSTEM_BOOTING))
213 return notifier_chain_register(&nh->head, n);
214
215 down_write(&nh->rwsem);
216 ret = notifier_chain_register(&nh->head, n);
217 up_write(&nh->rwsem);
218 return ret;
219}
220EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
221
222/**
Nadia Derbey6546bc42008-04-29 01:00:45 -0700223 * 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
226 *
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.
230 *
231 * Currently always returns zero.
232 */
233int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
234 struct notifier_block *n)
235{
236 int ret;
237
238 down_write(&nh->rwsem);
239 ret = notifier_chain_cond_register(&nh->head, n);
240 up_write(&nh->rwsem);
241 return ret;
242}
243EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
244
245/**
Alexey Dobriyanfe9d4f52007-10-18 23:39:16 -0700246 * 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
249 *
250 * Removes a notifier from a blocking notifier chain.
251 * Must be called from process context.
252 *
253 * Returns zero on success or %-ENOENT on failure.
254 */
255int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
256 struct notifier_block *n)
257{
258 int ret;
259
260 /*
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().
264 */
265 if (unlikely(system_state == SYSTEM_BOOTING))
266 return notifier_chain_unregister(&nh->head, n);
267
268 down_write(&nh->rwsem);
269 ret = notifier_chain_unregister(&nh->head, n);
270 up_write(&nh->rwsem);
271 return ret;
272}
273EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
274
275/**
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.
282 *
283 * Calls each function in a notifier chain in turn. The functions
284 * run in a process context, so they are allowed to block.
285 *
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.
292 */
293int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
294 unsigned long val, void *v,
295 int nr_to_call, int *nr_calls)
296{
297 int ret = NOTIFY_DONE;
298
299 /*
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:
303 */
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);
309 }
310 return ret;
311}
312EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
313
314int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
315 unsigned long val, void *v)
316{
317 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
318}
319EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
320
321/*
322 * Raw notifier chain routines. There is no protection;
323 * the caller must provide it. Use at your own risk!
324 */
325
326/**
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
330 *
331 * Adds a notifier to a raw notifier chain.
332 * All locking must be provided by the caller.
333 *
334 * Currently always returns zero.
335 */
336int raw_notifier_chain_register(struct raw_notifier_head *nh,
337 struct notifier_block *n)
338{
339 return notifier_chain_register(&nh->head, n);
340}
341EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
342
343/**
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
347 *
348 * Removes a notifier from a raw notifier chain.
349 * All locking must be provided by the caller.
350 *
351 * Returns zero on success or %-ENOENT on failure.
352 */
353int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
354 struct notifier_block *n)
355{
356 return notifier_chain_unregister(&nh->head, n);
357}
358EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
359
360/**
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
367 *
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.
371 *
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.
378 */
379int __raw_notifier_call_chain(struct raw_notifier_head *nh,
380 unsigned long val, void *v,
381 int nr_to_call, int *nr_calls)
382{
383 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
384}
385EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
386
387int raw_notifier_call_chain(struct raw_notifier_head *nh,
388 unsigned long val, void *v)
389{
390 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
391}
392EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
393
394/*
395 * SRCU notifier chain routines. Registration and unregistration
396 * use a mutex, and call_chain is synchronized by SRCU (no locks).
397 */
398
399/**
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
403 *
404 * Adds a notifier to an SRCU notifier chain.
405 * Must be called in process context.
406 *
407 * Currently always returns zero.
408 */
409int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
410 struct notifier_block *n)
411{
412 int ret;
413
414 /*
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().
418 */
419 if (unlikely(system_state == SYSTEM_BOOTING))
420 return notifier_chain_register(&nh->head, n);
421
422 mutex_lock(&nh->mutex);
423 ret = notifier_chain_register(&nh->head, n);
424 mutex_unlock(&nh->mutex);
425 return ret;
426}
427EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
428
429/**
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
433 *
434 * Removes a notifier from an SRCU notifier chain.
435 * Must be called from process context.
436 *
437 * Returns zero on success or %-ENOENT on failure.
438 */
439int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
440 struct notifier_block *n)
441{
442 int ret;
443
444 /*
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().
448 */
449 if (unlikely(system_state == SYSTEM_BOOTING))
450 return notifier_chain_unregister(&nh->head, n);
451
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;
457}
458EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
459
460/**
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
467 *
468 * Calls each function in a notifier chain in turn. The functions
469 * run in a process context, so they are allowed to block.
470 *
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.
477 */
478int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
479 unsigned long val, void *v,
480 int nr_to_call, int *nr_calls)
481{
482 int ret;
483 int idx;
484
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;
489}
490EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
491
492int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
493 unsigned long val, void *v)
494{
495 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
496}
497EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
498
499/**
500 * srcu_init_notifier_head - Initialize an SRCU notifier head
501 * @nh: Pointer to head of the srcu notifier chain
502 *
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.
506 *
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.
510 */
511void srcu_init_notifier_head(struct srcu_notifier_head *nh)
512{
513 mutex_init(&nh->mutex);
514 if (init_srcu_struct(&nh->srcu) < 0)
515 BUG();
516 nh->head = NULL;
517}
518EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
519
520/**
521 * register_reboot_notifier - Register function to be called at reboot time
522 * @nb: Info about notifier function to be called
523 *
524 * Registers a function with the list of functions
525 * to be called at reboot time.
526 *
527 * Currently always returns zero, as blocking_notifier_chain_register()
528 * always returns zero.
529 */
530int register_reboot_notifier(struct notifier_block *nb)
531{
532 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
533}
534EXPORT_SYMBOL(register_reboot_notifier);
535
536/**
537 * unregister_reboot_notifier - Unregister previously registered reboot notifier
538 * @nb: Hook to be unregistered
539 *
540 * Unregisters a previously registered reboot
541 * notifier function.
542 *
543 * Returns zero on success, or %-ENOENT on failure.
544 */
545int unregister_reboot_notifier(struct notifier_block *nb)
546{
547 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
548}
549EXPORT_SYMBOL(unregister_reboot_notifier);
550
551static ATOMIC_NOTIFIER_HEAD(die_chain);
552
553int notify_die(enum die_val val, const char *str,
554 struct pt_regs *regs, long err, int trap, int sig)
555{
556 struct die_args args = {
557 .regs = regs,
558 .str = str,
559 .err = err,
560 .trapnr = trap,
561 .signr = sig,
562
563 };
564 return atomic_notifier_call_chain(&die_chain, val, &args);
565}
566
567int register_die_notifier(struct notifier_block *nb)
568{
569 vmalloc_sync_all();
570 return atomic_notifier_chain_register(&die_chain, nb);
571}
572EXPORT_SYMBOL_GPL(register_die_notifier);
573
574int unregister_die_notifier(struct notifier_block *nb)
575{
576 return atomic_notifier_chain_unregister(&die_chain, nb);
577}
578EXPORT_SYMBOL_GPL(unregister_die_notifier);