driver core: fix up Kconfig text for CONFIG_SYSFS_DEPRECATED
[sfrench/cifs-2.6.git] / kernel / notifier.c
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>
8
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  */
14 BLOCKING_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
21 static 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
34 static int notifier_chain_unregister(struct notifier_block **nl,
35                 struct notifier_block *n)
36 {
37         while ((*nl) != NULL) {
38                 if ((*nl) == n) {
39                         rcu_assign_pointer(*nl, n->next);
40                         return 0;
41                 }
42                 nl = &((*nl)->next);
43         }
44         return -ENOENT;
45 }
46
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.
58  */
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)
62 {
63         int ret = NOTIFY_DONE;
64         struct notifier_block *nb, *next_nb;
65
66         nb = rcu_dereference(*nl);
67
68         while (nb && nr_to_call) {
69                 next_nb = rcu_dereference(nb->next);
70                 ret = nb->notifier_call(nb, val, v);
71
72                 if (nr_calls)
73                         (*nr_calls)++;
74
75                 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
76                         break;
77                 nb = next_nb;
78                 nr_to_call--;
79         }
80         return ret;
81 }
82
83 /*
84  *      Atomic notifier chain routines.  Registration and unregistration
85  *      use a spinlock, and call_chain is synchronized by RCU (no locks).
86  */
87
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
92  *
93  *      Adds a notifier to an atomic notifier chain.
94  *
95  *      Currently always returns zero.
96  */
97 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
98                 struct notifier_block *n)
99 {
100         unsigned long flags;
101         int ret;
102
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;
107 }
108 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
109
110 /**
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
114  *
115  *      Removes a notifier from an atomic notifier chain.
116  *
117  *      Returns zero on success or %-ENOENT on failure.
118  */
119 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
120                 struct notifier_block *n)
121 {
122         unsigned long flags;
123         int ret;
124
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;
130 }
131 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
132
133 /**
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.
140  *
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.
144  *
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.
151  */
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)
155 {
156         int ret;
157
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;
162 }
163 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
164
165 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
166                 unsigned long val, void *v)
167 {
168         return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
169 }
170 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
171
172 /*
173  *      Blocking notifier chain routines.  All access to the chain is
174  *      synchronized by an rwsem.
175  */
176
177 /**
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
181  *
182  *      Adds a notifier to a blocking notifier chain.
183  *      Must be called in process context.
184  *
185  *      Currently always returns zero.
186  */
187 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
188                 struct notifier_block *n)
189 {
190         int ret;
191
192         /*
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().
196          */
197         if (unlikely(system_state == SYSTEM_BOOTING))
198                 return notifier_chain_register(&nh->head, n);
199
200         down_write(&nh->rwsem);
201         ret = notifier_chain_register(&nh->head, n);
202         up_write(&nh->rwsem);
203         return ret;
204 }
205 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
206
207 /**
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
211  *
212  *      Removes a notifier from a blocking notifier chain.
213  *      Must be called from process context.
214  *
215  *      Returns zero on success or %-ENOENT on failure.
216  */
217 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
218                 struct notifier_block *n)
219 {
220         int ret;
221
222         /*
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().
226          */
227         if (unlikely(system_state == SYSTEM_BOOTING))
228                 return notifier_chain_unregister(&nh->head, n);
229
230         down_write(&nh->rwsem);
231         ret = notifier_chain_unregister(&nh->head, n);
232         up_write(&nh->rwsem);
233         return ret;
234 }
235 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
236
237 /**
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.
244  *
245  *      Calls each function in a notifier chain in turn.  The functions
246  *      run in a process context, so they are allowed to block.
247  *
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.
254  */
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)
258 {
259         int ret = NOTIFY_DONE;
260
261         /*
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:
265          */
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);
271         }
272         return ret;
273 }
274 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
275
276 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
277                 unsigned long val, void *v)
278 {
279         return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
280 }
281 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
282
283 /*
284  *      Raw notifier chain routines.  There is no protection;
285  *      the caller must provide it.  Use at your own risk!
286  */
287
288 /**
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
292  *
293  *      Adds a notifier to a raw notifier chain.
294  *      All locking must be provided by the caller.
295  *
296  *      Currently always returns zero.
297  */
298 int raw_notifier_chain_register(struct raw_notifier_head *nh,
299                 struct notifier_block *n)
300 {
301         return notifier_chain_register(&nh->head, n);
302 }
303 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
304
305 /**
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
309  *
310  *      Removes a notifier from a raw notifier chain.
311  *      All locking must be provided by the caller.
312  *
313  *      Returns zero on success or %-ENOENT on failure.
314  */
315 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
316                 struct notifier_block *n)
317 {
318         return notifier_chain_unregister(&nh->head, n);
319 }
320 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
321
322 /**
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
329  *
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.
333  *
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.
340  */
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)
344 {
345         return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
346 }
347 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
348
349 int raw_notifier_call_chain(struct raw_notifier_head *nh,
350                 unsigned long val, void *v)
351 {
352         return __raw_notifier_call_chain(nh, val, v, -1, NULL);
353 }
354 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
355
356 /*
357  *      SRCU notifier chain routines.    Registration and unregistration
358  *      use a mutex, and call_chain is synchronized by SRCU (no locks).
359  */
360
361 /**
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
365  *
366  *      Adds a notifier to an SRCU notifier chain.
367  *      Must be called in process context.
368  *
369  *      Currently always returns zero.
370  */
371 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
372                 struct notifier_block *n)
373 {
374         int ret;
375
376         /*
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().
380          */
381         if (unlikely(system_state == SYSTEM_BOOTING))
382                 return notifier_chain_register(&nh->head, n);
383
384         mutex_lock(&nh->mutex);
385         ret = notifier_chain_register(&nh->head, n);
386         mutex_unlock(&nh->mutex);
387         return ret;
388 }
389 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
390
391 /**
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
395  *
396  *      Removes a notifier from an SRCU notifier chain.
397  *      Must be called from process context.
398  *
399  *      Returns zero on success or %-ENOENT on failure.
400  */
401 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
402                 struct notifier_block *n)
403 {
404         int ret;
405
406         /*
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().
410          */
411         if (unlikely(system_state == SYSTEM_BOOTING))
412                 return notifier_chain_unregister(&nh->head, n);
413
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;
419 }
420 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
421
422 /**
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
429  *
430  *      Calls each function in a notifier chain in turn.  The functions
431  *      run in a process context, so they are allowed to block.
432  *
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.
439  */
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)
443 {
444         int ret;
445         int idx;
446
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;
451 }
452 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
453
454 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
455                 unsigned long val, void *v)
456 {
457         return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
458 }
459 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
460
461 /**
462  *      srcu_init_notifier_head - Initialize an SRCU notifier head
463  *      @nh: Pointer to head of the srcu notifier chain
464  *
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.
468  *
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.
472  */
473 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
474 {
475         mutex_init(&nh->mutex);
476         if (init_srcu_struct(&nh->srcu) < 0)
477                 BUG();
478         nh->head = NULL;
479 }
480 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
481
482 /**
483  *      register_reboot_notifier - Register function to be called at reboot time
484  *      @nb: Info about notifier function to be called
485  *
486  *      Registers a function with the list of functions
487  *      to be called at reboot time.
488  *
489  *      Currently always returns zero, as blocking_notifier_chain_register()
490  *      always returns zero.
491  */
492 int register_reboot_notifier(struct notifier_block *nb)
493 {
494         return blocking_notifier_chain_register(&reboot_notifier_list, nb);
495 }
496 EXPORT_SYMBOL(register_reboot_notifier);
497
498 /**
499  *      unregister_reboot_notifier - Unregister previously registered reboot notifier
500  *      @nb: Hook to be unregistered
501  *
502  *      Unregisters a previously registered reboot
503  *      notifier function.
504  *
505  *      Returns zero on success, or %-ENOENT on failure.
506  */
507 int unregister_reboot_notifier(struct notifier_block *nb)
508 {
509         return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
510 }
511 EXPORT_SYMBOL(unregister_reboot_notifier);
512
513 static ATOMIC_NOTIFIER_HEAD(die_chain);
514
515 int notify_die(enum die_val val, const char *str,
516                struct pt_regs *regs, long err, int trap, int sig)
517 {
518         struct die_args args = {
519                 .regs   = regs,
520                 .str    = str,
521                 .err    = err,
522                 .trapnr = trap,
523                 .signr  = sig,
524
525         };
526         return atomic_notifier_call_chain(&die_chain, val, &args);
527 }
528
529 int register_die_notifier(struct notifier_block *nb)
530 {
531         vmalloc_sync_all();
532         return atomic_notifier_chain_register(&die_chain, nb);
533 }
534 EXPORT_SYMBOL_GPL(register_die_notifier);
535
536 int unregister_die_notifier(struct notifier_block *nb)
537 {
538         return atomic_notifier_chain_unregister(&die_chain, nb);
539 }
540 EXPORT_SYMBOL_GPL(unregister_die_notifier);