Back to home page

LXR

 
 

    


0001 #include <linux/kdebug.h>
0002 #include <linux/kprobes.h>
0003 #include <linux/export.h>
0004 #include <linux/notifier.h>
0005 #include <linux/rcupdate.h>
0006 #include <linux/vmalloc.h>
0007 #include <linux/reboot.h>
0008 
0009 /*
0010  *  Notifier list for kernel code which wants to be called
0011  *  at shutdown. This is used to stop any idling DMA operations
0012  *  and the like.
0013  */
0014 BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
0015 
0016 /*
0017  *  Notifier chain core routines.  The exported routines below
0018  *  are layered on top of these, with appropriate locking added.
0019  */
0020 
0021 static int notifier_chain_register(struct notifier_block **nl,
0022         struct notifier_block *n)
0023 {
0024     while ((*nl) != NULL) {
0025         if (n->priority > (*nl)->priority)
0026             break;
0027         nl = &((*nl)->next);
0028     }
0029     n->next = *nl;
0030     rcu_assign_pointer(*nl, n);
0031     return 0;
0032 }
0033 
0034 static int notifier_chain_cond_register(struct notifier_block **nl,
0035         struct notifier_block *n)
0036 {
0037     while ((*nl) != NULL) {
0038         if ((*nl) == n)
0039             return 0;
0040         if (n->priority > (*nl)->priority)
0041             break;
0042         nl = &((*nl)->next);
0043     }
0044     n->next = *nl;
0045     rcu_assign_pointer(*nl, n);
0046     return 0;
0047 }
0048 
0049 static int notifier_chain_unregister(struct notifier_block **nl,
0050         struct notifier_block *n)
0051 {
0052     while ((*nl) != NULL) {
0053         if ((*nl) == n) {
0054             rcu_assign_pointer(*nl, n->next);
0055             return 0;
0056         }
0057         nl = &((*nl)->next);
0058     }
0059     return -ENOENT;
0060 }
0061 
0062 /**
0063  * notifier_call_chain - Informs the registered notifiers about an event.
0064  *  @nl:        Pointer to head of the blocking notifier chain
0065  *  @val:       Value passed unmodified to notifier function
0066  *  @v:     Pointer passed unmodified to notifier function
0067  *  @nr_to_call:    Number of notifier functions to be called. Don't care
0068  *          value of this parameter is -1.
0069  *  @nr_calls:  Records the number of notifications sent. Don't care
0070  *          value of this field is NULL.
0071  *  @returns:   notifier_call_chain returns the value returned by the
0072  *          last notifier function called.
0073  */
0074 static int notifier_call_chain(struct notifier_block **nl,
0075                    unsigned long val, void *v,
0076                    int nr_to_call, int *nr_calls)
0077 {
0078     int ret = NOTIFY_DONE;
0079     struct notifier_block *nb, *next_nb;
0080 
0081     nb = rcu_dereference_raw(*nl);
0082 
0083     while (nb && nr_to_call) {
0084         next_nb = rcu_dereference_raw(nb->next);
0085 
0086 #ifdef CONFIG_DEBUG_NOTIFIERS
0087         if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
0088             WARN(1, "Invalid notifier called!");
0089             nb = next_nb;
0090             continue;
0091         }
0092 #endif
0093         ret = nb->notifier_call(nb, val, v);
0094 
0095         if (nr_calls)
0096             (*nr_calls)++;
0097 
0098         if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
0099             break;
0100         nb = next_nb;
0101         nr_to_call--;
0102     }
0103     return ret;
0104 }
0105 NOKPROBE_SYMBOL(notifier_call_chain);
0106 
0107 /*
0108  *  Atomic notifier chain routines.  Registration and unregistration
0109  *  use a spinlock, and call_chain is synchronized by RCU (no locks).
0110  */
0111 
0112 /**
0113  *  atomic_notifier_chain_register - Add notifier to an atomic notifier chain
0114  *  @nh: Pointer to head of the atomic notifier chain
0115  *  @n: New entry in notifier chain
0116  *
0117  *  Adds a notifier to an atomic notifier chain.
0118  *
0119  *  Currently always returns zero.
0120  */
0121 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
0122         struct notifier_block *n)
0123 {
0124     unsigned long flags;
0125     int ret;
0126 
0127     spin_lock_irqsave(&nh->lock, flags);
0128     ret = notifier_chain_register(&nh->head, n);
0129     spin_unlock_irqrestore(&nh->lock, flags);
0130     return ret;
0131 }
0132 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
0133 
0134 /**
0135  *  atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
0136  *  @nh: Pointer to head of the atomic notifier chain
0137  *  @n: Entry to remove from notifier chain
0138  *
0139  *  Removes a notifier from an atomic notifier chain.
0140  *
0141  *  Returns zero on success or %-ENOENT on failure.
0142  */
0143 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
0144         struct notifier_block *n)
0145 {
0146     unsigned long flags;
0147     int ret;
0148 
0149     spin_lock_irqsave(&nh->lock, flags);
0150     ret = notifier_chain_unregister(&nh->head, n);
0151     spin_unlock_irqrestore(&nh->lock, flags);
0152     synchronize_rcu();
0153     return ret;
0154 }
0155 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
0156 
0157 /**
0158  *  __atomic_notifier_call_chain - Call functions in an atomic notifier chain
0159  *  @nh: Pointer to head of the atomic notifier chain
0160  *  @val: Value passed unmodified to notifier function
0161  *  @v: Pointer passed unmodified to notifier function
0162  *  @nr_to_call: See the comment for notifier_call_chain.
0163  *  @nr_calls: See the comment for notifier_call_chain.
0164  *
0165  *  Calls each function in a notifier chain in turn.  The functions
0166  *  run in an atomic context, so they must not block.
0167  *  This routine uses RCU to synchronize with changes to the chain.
0168  *
0169  *  If the return value of the notifier can be and'ed
0170  *  with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
0171  *  will return immediately, with the return value of
0172  *  the notifier function which halted execution.
0173  *  Otherwise the return value is the return value
0174  *  of the last notifier function called.
0175  */
0176 int __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
0177                  unsigned long val, void *v,
0178                  int nr_to_call, int *nr_calls)
0179 {
0180     int ret;
0181 
0182     rcu_read_lock();
0183     ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
0184     rcu_read_unlock();
0185     return ret;
0186 }
0187 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
0188 NOKPROBE_SYMBOL(__atomic_notifier_call_chain);
0189 
0190 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
0191                    unsigned long val, void *v)
0192 {
0193     return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
0194 }
0195 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
0196 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
0197 
0198 /*
0199  *  Blocking notifier chain routines.  All access to the chain is
0200  *  synchronized by an rwsem.
0201  */
0202 
0203 /**
0204  *  blocking_notifier_chain_register - Add notifier to a blocking notifier chain
0205  *  @nh: Pointer to head of the blocking notifier chain
0206  *  @n: New entry in notifier chain
0207  *
0208  *  Adds a notifier to a blocking notifier chain.
0209  *  Must be called in process context.
0210  *
0211  *  Currently always returns zero.
0212  */
0213 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
0214         struct notifier_block *n)
0215 {
0216     int ret;
0217 
0218     /*
0219      * This code gets used during boot-up, when task switching is
0220      * not yet working and interrupts must remain disabled.  At
0221      * such times we must not call down_write().
0222      */
0223     if (unlikely(system_state == SYSTEM_BOOTING))
0224         return notifier_chain_register(&nh->head, n);
0225 
0226     down_write(&nh->rwsem);
0227     ret = notifier_chain_register(&nh->head, n);
0228     up_write(&nh->rwsem);
0229     return ret;
0230 }
0231 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
0232 
0233 /**
0234  *  blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
0235  *  @nh: Pointer to head of the blocking notifier chain
0236  *  @n: New entry in notifier chain
0237  *
0238  *  Adds a notifier to a blocking notifier chain, only if not already
0239  *  present in the chain.
0240  *  Must be called in process context.
0241  *
0242  *  Currently always returns zero.
0243  */
0244 int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
0245         struct notifier_block *n)
0246 {
0247     int ret;
0248 
0249     down_write(&nh->rwsem);
0250     ret = notifier_chain_cond_register(&nh->head, n);
0251     up_write(&nh->rwsem);
0252     return ret;
0253 }
0254 EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
0255 
0256 /**
0257  *  blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
0258  *  @nh: Pointer to head of the blocking notifier chain
0259  *  @n: Entry to remove from notifier chain
0260  *
0261  *  Removes a notifier from a blocking notifier chain.
0262  *  Must be called from process context.
0263  *
0264  *  Returns zero on success or %-ENOENT on failure.
0265  */
0266 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
0267         struct notifier_block *n)
0268 {
0269     int ret;
0270 
0271     /*
0272      * This code gets used during boot-up, when task switching is
0273      * not yet working and interrupts must remain disabled.  At
0274      * such times we must not call down_write().
0275      */
0276     if (unlikely(system_state == SYSTEM_BOOTING))
0277         return notifier_chain_unregister(&nh->head, n);
0278 
0279     down_write(&nh->rwsem);
0280     ret = notifier_chain_unregister(&nh->head, n);
0281     up_write(&nh->rwsem);
0282     return ret;
0283 }
0284 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
0285 
0286 /**
0287  *  __blocking_notifier_call_chain - Call functions in a blocking notifier chain
0288  *  @nh: Pointer to head of the blocking notifier chain
0289  *  @val: Value passed unmodified to notifier function
0290  *  @v: Pointer passed unmodified to notifier function
0291  *  @nr_to_call: See comment for notifier_call_chain.
0292  *  @nr_calls: See comment for notifier_call_chain.
0293  *
0294  *  Calls each function in a notifier chain in turn.  The functions
0295  *  run in a process context, so they are allowed to block.
0296  *
0297  *  If the return value of the notifier can be and'ed
0298  *  with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
0299  *  will return immediately, with the return value of
0300  *  the notifier function which halted execution.
0301  *  Otherwise the return value is the return value
0302  *  of the last notifier function called.
0303  */
0304 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
0305                    unsigned long val, void *v,
0306                    int nr_to_call, int *nr_calls)
0307 {
0308     int ret = NOTIFY_DONE;
0309 
0310     /*
0311      * We check the head outside the lock, but if this access is
0312      * racy then it does not matter what the result of the test
0313      * is, we re-check the list after having taken the lock anyway:
0314      */
0315     if (rcu_access_pointer(nh->head)) {
0316         down_read(&nh->rwsem);
0317         ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
0318                     nr_calls);
0319         up_read(&nh->rwsem);
0320     }
0321     return ret;
0322 }
0323 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
0324 
0325 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
0326         unsigned long val, void *v)
0327 {
0328     return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
0329 }
0330 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
0331 
0332 /*
0333  *  Raw notifier chain routines.  There is no protection;
0334  *  the caller must provide it.  Use at your own risk!
0335  */
0336 
0337 /**
0338  *  raw_notifier_chain_register - Add notifier to a raw notifier chain
0339  *  @nh: Pointer to head of the raw notifier chain
0340  *  @n: New entry in notifier chain
0341  *
0342  *  Adds a notifier to a raw notifier chain.
0343  *  All locking must be provided by the caller.
0344  *
0345  *  Currently always returns zero.
0346  */
0347 int raw_notifier_chain_register(struct raw_notifier_head *nh,
0348         struct notifier_block *n)
0349 {
0350     return notifier_chain_register(&nh->head, n);
0351 }
0352 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
0353 
0354 /**
0355  *  raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
0356  *  @nh: Pointer to head of the raw notifier chain
0357  *  @n: Entry to remove from notifier chain
0358  *
0359  *  Removes a notifier from a raw notifier chain.
0360  *  All locking must be provided by the caller.
0361  *
0362  *  Returns zero on success or %-ENOENT on failure.
0363  */
0364 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
0365         struct notifier_block *n)
0366 {
0367     return notifier_chain_unregister(&nh->head, n);
0368 }
0369 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
0370 
0371 /**
0372  *  __raw_notifier_call_chain - Call functions in a raw notifier chain
0373  *  @nh: Pointer to head of the raw notifier chain
0374  *  @val: Value passed unmodified to notifier function
0375  *  @v: Pointer passed unmodified to notifier function
0376  *  @nr_to_call: See comment for notifier_call_chain.
0377  *  @nr_calls: See comment for notifier_call_chain
0378  *
0379  *  Calls each function in a notifier chain in turn.  The functions
0380  *  run in an undefined context.
0381  *  All locking must be provided by the caller.
0382  *
0383  *  If the return value of the notifier can be and'ed
0384  *  with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
0385  *  will return immediately, with the return value of
0386  *  the notifier function which halted execution.
0387  *  Otherwise the return value is the return value
0388  *  of the last notifier function called.
0389  */
0390 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
0391                   unsigned long val, void *v,
0392                   int nr_to_call, int *nr_calls)
0393 {
0394     return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
0395 }
0396 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
0397 
0398 int raw_notifier_call_chain(struct raw_notifier_head *nh,
0399         unsigned long val, void *v)
0400 {
0401     return __raw_notifier_call_chain(nh, val, v, -1, NULL);
0402 }
0403 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
0404 
0405 #ifdef CONFIG_SRCU
0406 /*
0407  *  SRCU notifier chain routines.    Registration and unregistration
0408  *  use a mutex, and call_chain is synchronized by SRCU (no locks).
0409  */
0410 
0411 /**
0412  *  srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
0413  *  @nh: Pointer to head of the SRCU notifier chain
0414  *  @n: New entry in notifier chain
0415  *
0416  *  Adds a notifier to an SRCU notifier chain.
0417  *  Must be called in process context.
0418  *
0419  *  Currently always returns zero.
0420  */
0421 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
0422         struct notifier_block *n)
0423 {
0424     int ret;
0425 
0426     /*
0427      * This code gets used during boot-up, when task switching is
0428      * not yet working and interrupts must remain disabled.  At
0429      * such times we must not call mutex_lock().
0430      */
0431     if (unlikely(system_state == SYSTEM_BOOTING))
0432         return notifier_chain_register(&nh->head, n);
0433 
0434     mutex_lock(&nh->mutex);
0435     ret = notifier_chain_register(&nh->head, n);
0436     mutex_unlock(&nh->mutex);
0437     return ret;
0438 }
0439 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
0440 
0441 /**
0442  *  srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
0443  *  @nh: Pointer to head of the SRCU notifier chain
0444  *  @n: Entry to remove from notifier chain
0445  *
0446  *  Removes a notifier from an SRCU notifier chain.
0447  *  Must be called from process context.
0448  *
0449  *  Returns zero on success or %-ENOENT on failure.
0450  */
0451 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
0452         struct notifier_block *n)
0453 {
0454     int ret;
0455 
0456     /*
0457      * This code gets used during boot-up, when task switching is
0458      * not yet working and interrupts must remain disabled.  At
0459      * such times we must not call mutex_lock().
0460      */
0461     if (unlikely(system_state == SYSTEM_BOOTING))
0462         return notifier_chain_unregister(&nh->head, n);
0463 
0464     mutex_lock(&nh->mutex);
0465     ret = notifier_chain_unregister(&nh->head, n);
0466     mutex_unlock(&nh->mutex);
0467     synchronize_srcu(&nh->srcu);
0468     return ret;
0469 }
0470 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
0471 
0472 /**
0473  *  __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
0474  *  @nh: Pointer to head of the SRCU notifier chain
0475  *  @val: Value passed unmodified to notifier function
0476  *  @v: Pointer passed unmodified to notifier function
0477  *  @nr_to_call: See comment for notifier_call_chain.
0478  *  @nr_calls: See comment for notifier_call_chain
0479  *
0480  *  Calls each function in a notifier chain in turn.  The functions
0481  *  run in a process context, so they are allowed to block.
0482  *
0483  *  If the return value of the notifier can be and'ed
0484  *  with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
0485  *  will return immediately, with the return value of
0486  *  the notifier function which halted execution.
0487  *  Otherwise the return value is the return value
0488  *  of the last notifier function called.
0489  */
0490 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
0491                    unsigned long val, void *v,
0492                    int nr_to_call, int *nr_calls)
0493 {
0494     int ret;
0495     int idx;
0496 
0497     idx = srcu_read_lock(&nh->srcu);
0498     ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
0499     srcu_read_unlock(&nh->srcu, idx);
0500     return ret;
0501 }
0502 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
0503 
0504 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
0505         unsigned long val, void *v)
0506 {
0507     return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
0508 }
0509 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
0510 
0511 /**
0512  *  srcu_init_notifier_head - Initialize an SRCU notifier head
0513  *  @nh: Pointer to head of the srcu notifier chain
0514  *
0515  *  Unlike other sorts of notifier heads, SRCU notifier heads require
0516  *  dynamic initialization.  Be sure to call this routine before
0517  *  calling any of the other SRCU notifier routines for this head.
0518  *
0519  *  If an SRCU notifier head is deallocated, it must first be cleaned
0520  *  up by calling srcu_cleanup_notifier_head().  Otherwise the head's
0521  *  per-cpu data (used by the SRCU mechanism) will leak.
0522  */
0523 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
0524 {
0525     mutex_init(&nh->mutex);
0526     if (init_srcu_struct(&nh->srcu) < 0)
0527         BUG();
0528     nh->head = NULL;
0529 }
0530 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
0531 
0532 #endif /* CONFIG_SRCU */
0533 
0534 static ATOMIC_NOTIFIER_HEAD(die_chain);
0535 
0536 int notrace notify_die(enum die_val val, const char *str,
0537            struct pt_regs *regs, long err, int trap, int sig)
0538 {
0539     struct die_args args = {
0540         .regs   = regs,
0541         .str    = str,
0542         .err    = err,
0543         .trapnr = trap,
0544         .signr  = sig,
0545 
0546     };
0547     RCU_LOCKDEP_WARN(!rcu_is_watching(),
0548                "notify_die called but RCU thinks we're quiescent");
0549     return atomic_notifier_call_chain(&die_chain, val, &args);
0550 }
0551 NOKPROBE_SYMBOL(notify_die);
0552 
0553 int register_die_notifier(struct notifier_block *nb)
0554 {
0555     vmalloc_sync_all();
0556     return atomic_notifier_chain_register(&die_chain, nb);
0557 }
0558 EXPORT_SYMBOL_GPL(register_die_notifier);
0559 
0560 int unregister_die_notifier(struct notifier_block *nb)
0561 {
0562     return atomic_notifier_chain_unregister(&die_chain, nb);
0563 }
0564 EXPORT_SYMBOL_GPL(unregister_die_notifier);