Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  *  Routines to manage notifier chains for passing status changes to any
0004  *  interested routines. We need this instead of hard coded call lists so
0005  *  that modules can poke their nose into the innards. The network devices
0006  *  needed them so here they are for the rest of you.
0007  *
0008  *              Alan Cox <Alan.Cox@linux.org>
0009  */
0010  
0011 #ifndef _LINUX_NOTIFIER_H
0012 #define _LINUX_NOTIFIER_H
0013 #include <linux/errno.h>
0014 #include <linux/mutex.h>
0015 #include <linux/rwsem.h>
0016 #include <linux/srcu.h>
0017 
0018 /*
0019  * Notifier chains are of four types:
0020  *
0021  *  Atomic notifier chains: Chain callbacks run in interrupt/atomic
0022  *      context. Callouts are not allowed to block.
0023  *  Blocking notifier chains: Chain callbacks run in process context.
0024  *      Callouts are allowed to block.
0025  *  Raw notifier chains: There are no restrictions on callbacks,
0026  *      registration, or unregistration.  All locking and protection
0027  *      must be provided by the caller.
0028  *  SRCU notifier chains: A variant of blocking notifier chains, with
0029  *      the same restrictions.
0030  *
0031  * atomic_notifier_chain_register() may be called from an atomic context,
0032  * but blocking_notifier_chain_register() and srcu_notifier_chain_register()
0033  * must be called from a process context.  Ditto for the corresponding
0034  * _unregister() routines.
0035  *
0036  * atomic_notifier_chain_unregister(), blocking_notifier_chain_unregister(),
0037  * and srcu_notifier_chain_unregister() _must not_ be called from within
0038  * the call chain.
0039  *
0040  * SRCU notifier chains are an alternative form of blocking notifier chains.
0041  * They use SRCU (Sleepable Read-Copy Update) instead of rw-semaphores for
0042  * protection of the chain links.  This means there is _very_ low overhead
0043  * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
0044  * As compensation, srcu_notifier_chain_unregister() is rather expensive.
0045  * SRCU notifier chains should be used when the chain will be called very
0046  * often but notifier_blocks will seldom be removed.
0047  */
0048 
0049 struct notifier_block;
0050 
0051 typedef int (*notifier_fn_t)(struct notifier_block *nb,
0052             unsigned long action, void *data);
0053 
0054 struct notifier_block {
0055     notifier_fn_t notifier_call;
0056     struct notifier_block __rcu *next;
0057     int priority;
0058 };
0059 
0060 struct atomic_notifier_head {
0061     spinlock_t lock;
0062     struct notifier_block __rcu *head;
0063 };
0064 
0065 struct blocking_notifier_head {
0066     struct rw_semaphore rwsem;
0067     struct notifier_block __rcu *head;
0068 };
0069 
0070 struct raw_notifier_head {
0071     struct notifier_block __rcu *head;
0072 };
0073 
0074 struct srcu_notifier_head {
0075     struct mutex mutex;
0076     struct srcu_struct srcu;
0077     struct notifier_block __rcu *head;
0078 };
0079 
0080 #define ATOMIC_INIT_NOTIFIER_HEAD(name) do {    \
0081         spin_lock_init(&(name)->lock);  \
0082         (name)->head = NULL;        \
0083     } while (0)
0084 #define BLOCKING_INIT_NOTIFIER_HEAD(name) do {  \
0085         init_rwsem(&(name)->rwsem); \
0086         (name)->head = NULL;        \
0087     } while (0)
0088 #define RAW_INIT_NOTIFIER_HEAD(name) do {   \
0089         (name)->head = NULL;        \
0090     } while (0)
0091 
0092 /* srcu_notifier_heads must be cleaned up dynamically */
0093 extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
0094 #define srcu_cleanup_notifier_head(name)    \
0095         cleanup_srcu_struct(&(name)->srcu);
0096 
0097 #define ATOMIC_NOTIFIER_INIT(name) {                \
0098         .lock = __SPIN_LOCK_UNLOCKED(name.lock),    \
0099         .head = NULL }
0100 #define BLOCKING_NOTIFIER_INIT(name) {              \
0101         .rwsem = __RWSEM_INITIALIZER((name).rwsem), \
0102         .head = NULL }
0103 #define RAW_NOTIFIER_INIT(name) {               \
0104         .head = NULL }
0105 
0106 #define SRCU_NOTIFIER_INIT(name, pcpu)              \
0107     {                           \
0108         .mutex = __MUTEX_INITIALIZER(name.mutex),   \
0109         .head = NULL,                   \
0110         .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu),    \
0111     }
0112 
0113 #define ATOMIC_NOTIFIER_HEAD(name)              \
0114     struct atomic_notifier_head name =          \
0115         ATOMIC_NOTIFIER_INIT(name)
0116 #define BLOCKING_NOTIFIER_HEAD(name)                \
0117     struct blocking_notifier_head name =            \
0118         BLOCKING_NOTIFIER_INIT(name)
0119 #define RAW_NOTIFIER_HEAD(name)                 \
0120     struct raw_notifier_head name =             \
0121         RAW_NOTIFIER_INIT(name)
0122 
0123 #ifdef CONFIG_TREE_SRCU
0124 #define _SRCU_NOTIFIER_HEAD(name, mod)              \
0125     static DEFINE_PER_CPU(struct srcu_data, name##_head_srcu_data); \
0126     mod struct srcu_notifier_head name =            \
0127             SRCU_NOTIFIER_INIT(name, name##_head_srcu_data)
0128 
0129 #else
0130 #define _SRCU_NOTIFIER_HEAD(name, mod)              \
0131     mod struct srcu_notifier_head name =            \
0132             SRCU_NOTIFIER_INIT(name, name)
0133 
0134 #endif
0135 
0136 #define SRCU_NOTIFIER_HEAD(name)                \
0137     _SRCU_NOTIFIER_HEAD(name, /* not static */)
0138 
0139 #define SRCU_NOTIFIER_HEAD_STATIC(name)             \
0140     _SRCU_NOTIFIER_HEAD(name, static)
0141 
0142 #ifdef __KERNEL__
0143 
0144 extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
0145         struct notifier_block *nb);
0146 extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
0147         struct notifier_block *nb);
0148 extern int raw_notifier_chain_register(struct raw_notifier_head *nh,
0149         struct notifier_block *nb);
0150 extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
0151         struct notifier_block *nb);
0152 
0153 extern int atomic_notifier_chain_register_unique_prio(
0154         struct atomic_notifier_head *nh, struct notifier_block *nb);
0155 extern int blocking_notifier_chain_register_unique_prio(
0156         struct blocking_notifier_head *nh, struct notifier_block *nb);
0157 
0158 extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
0159         struct notifier_block *nb);
0160 extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
0161         struct notifier_block *nb);
0162 extern int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
0163         struct notifier_block *nb);
0164 extern int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
0165         struct notifier_block *nb);
0166 
0167 extern int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
0168         unsigned long val, void *v);
0169 extern int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
0170         unsigned long val, void *v);
0171 extern int raw_notifier_call_chain(struct raw_notifier_head *nh,
0172         unsigned long val, void *v);
0173 extern int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
0174         unsigned long val, void *v);
0175 
0176 extern int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
0177         unsigned long val_up, unsigned long val_down, void *v);
0178 extern int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
0179         unsigned long val_up, unsigned long val_down, void *v);
0180 
0181 extern bool atomic_notifier_call_chain_is_empty(struct atomic_notifier_head *nh);
0182 
0183 #define NOTIFY_DONE     0x0000      /* Don't care */
0184 #define NOTIFY_OK       0x0001      /* Suits me */
0185 #define NOTIFY_STOP_MASK    0x8000      /* Don't call further */
0186 #define NOTIFY_BAD      (NOTIFY_STOP_MASK|0x0002)
0187                         /* Bad/Veto action */
0188 /*
0189  * Clean way to return from the notifier and stop further calls.
0190  */
0191 #define NOTIFY_STOP     (NOTIFY_OK|NOTIFY_STOP_MASK)
0192 
0193 /* Encapsulate (negative) errno value (in particular, NOTIFY_BAD <=> EPERM). */
0194 static inline int notifier_from_errno(int err)
0195 {
0196     if (err)
0197         return NOTIFY_STOP_MASK | (NOTIFY_OK - err);
0198 
0199     return NOTIFY_OK;
0200 }
0201 
0202 /* Restore (negative) errno value from notify return value. */
0203 static inline int notifier_to_errno(int ret)
0204 {
0205     ret &= ~NOTIFY_STOP_MASK;
0206     return ret > NOTIFY_OK ? NOTIFY_OK - ret : 0;
0207 }
0208 
0209 /*
0210  *  Declared notifiers so far. I can imagine quite a few more chains
0211  *  over time (eg laptop power reset chains, reboot chain (to clean 
0212  *  device units up), device [un]mount chain, module load/unload chain,
0213  *  low memory chain, screenblank chain (for plug in modular screenblankers) 
0214  *  VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
0215  */
0216  
0217 /* CPU notfiers are defined in include/linux/cpu.h. */
0218 
0219 /* netdevice notifiers are defined in include/linux/netdevice.h */
0220 
0221 /* reboot notifiers are defined in include/linux/reboot.h. */
0222 
0223 /* Hibernation and suspend events are defined in include/linux/suspend.h. */
0224 
0225 /* Virtual Terminal events are defined in include/linux/vt.h. */
0226 
0227 #define NETLINK_URELEASE    0x0001  /* Unicast netlink socket released */
0228 
0229 /* Console keyboard events.
0230  * Note: KBD_KEYCODE is always sent before KBD_UNBOUND_KEYCODE, KBD_UNICODE and
0231  * KBD_KEYSYM. */
0232 #define KBD_KEYCODE     0x0001 /* Keyboard keycode, called before any other */
0233 #define KBD_UNBOUND_KEYCODE 0x0002 /* Keyboard keycode which is not bound to any other */
0234 #define KBD_UNICODE     0x0003 /* Keyboard unicode */
0235 #define KBD_KEYSYM      0x0004 /* Keyboard keysym */
0236 #define KBD_POST_KEYSYM     0x0005 /* Called after keyboard keysym interpretation */
0237 
0238 extern struct blocking_notifier_head reboot_notifier_list;
0239 
0240 #endif /* __KERNEL__ */
0241 #endif /* _LINUX_NOTIFIER_H */