notifier.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Routines to manage notifier chains for passing status changes to any
  4. * interested routines. We need this instead of hard coded call lists so
  5. * that modules can poke their nose into the innards. The network devices
  6. * needed them so here they are for the rest of you.
  7. *
  8. * Alan Cox <Alan.Cox@linux.org>
  9. */
  10. #ifndef _LINUX_NOTIFIER_H
  11. #define _LINUX_NOTIFIER_H
  12. #include <linux/errno.h>
  13. #include <linux/mutex.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/srcu.h>
  16. /*
  17. * Notifier chains are of four types:
  18. *
  19. * Atomic notifier chains: Chain callbacks run in interrupt/atomic
  20. * context. Callouts are not allowed to block.
  21. * Blocking notifier chains: Chain callbacks run in process context.
  22. * Callouts are allowed to block.
  23. * Raw notifier chains: There are no restrictions on callbacks,
  24. * registration, or unregistration. All locking and protection
  25. * must be provided by the caller.
  26. * SRCU notifier chains: A variant of blocking notifier chains, with
  27. * the same restrictions.
  28. *
  29. * atomic_notifier_chain_register() may be called from an atomic context,
  30. * but blocking_notifier_chain_register() and srcu_notifier_chain_register()
  31. * must be called from a process context. Ditto for the corresponding
  32. * _unregister() routines.
  33. *
  34. * atomic_notifier_chain_unregister(), blocking_notifier_chain_unregister(),
  35. * and srcu_notifier_chain_unregister() _must not_ be called from within
  36. * the call chain.
  37. *
  38. * SRCU notifier chains are an alternative form of blocking notifier chains.
  39. * They use SRCU (Sleepable Read-Copy Update) instead of rw-semaphores for
  40. * protection of the chain links. This means there is _very_ low overhead
  41. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  42. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  43. * SRCU notifier chains should be used when the chain will be called very
  44. * often but notifier_blocks will seldom be removed.
  45. */
  46. struct notifier_block;
  47. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  48. unsigned long action, void *data);
  49. struct notifier_block {
  50. notifier_fn_t notifier_call;
  51. struct notifier_block __rcu *next;
  52. int priority;
  53. };
  54. struct atomic_notifier_head {
  55. spinlock_t lock;
  56. struct notifier_block __rcu *head;
  57. };
  58. struct blocking_notifier_head {
  59. struct rw_semaphore rwsem;
  60. struct notifier_block __rcu *head;
  61. };
  62. struct raw_notifier_head {
  63. struct notifier_block __rcu *head;
  64. };
  65. struct srcu_notifier_head {
  66. struct mutex mutex;
  67. struct srcu_struct srcu;
  68. struct notifier_block __rcu *head;
  69. };
  70. #define ATOMIC_INIT_NOTIFIER_HEAD(name) do { \
  71. spin_lock_init(&(name)->lock); \
  72. (name)->head = NULL; \
  73. } while (0)
  74. #define BLOCKING_INIT_NOTIFIER_HEAD(name) do { \
  75. init_rwsem(&(name)->rwsem); \
  76. (name)->head = NULL; \
  77. } while (0)
  78. #define RAW_INIT_NOTIFIER_HEAD(name) do { \
  79. (name)->head = NULL; \
  80. } while (0)
  81. /* srcu_notifier_heads must be cleaned up dynamically */
  82. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  83. #define srcu_cleanup_notifier_head(name) \
  84. cleanup_srcu_struct(&(name)->srcu);
  85. #define ATOMIC_NOTIFIER_INIT(name) { \
  86. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  87. .head = NULL }
  88. #define BLOCKING_NOTIFIER_INIT(name) { \
  89. .rwsem = __RWSEM_INITIALIZER((name).rwsem), \
  90. .head = NULL }
  91. #define RAW_NOTIFIER_INIT(name) { \
  92. .head = NULL }
  93. #define SRCU_NOTIFIER_INIT(name, pcpu) \
  94. { \
  95. .mutex = __MUTEX_INITIALIZER(name.mutex), \
  96. .head = NULL, \
  97. .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  98. }
  99. #define ATOMIC_NOTIFIER_HEAD(name) \
  100. struct atomic_notifier_head name = \
  101. ATOMIC_NOTIFIER_INIT(name)
  102. #define BLOCKING_NOTIFIER_HEAD(name) \
  103. struct blocking_notifier_head name = \
  104. BLOCKING_NOTIFIER_INIT(name)
  105. #define RAW_NOTIFIER_HEAD(name) \
  106. struct raw_notifier_head name = \
  107. RAW_NOTIFIER_INIT(name)
  108. #ifdef CONFIG_TREE_SRCU
  109. #define _SRCU_NOTIFIER_HEAD(name, mod) \
  110. static DEFINE_PER_CPU(struct srcu_data, name##_head_srcu_data); \
  111. mod struct srcu_notifier_head name = \
  112. SRCU_NOTIFIER_INIT(name, name##_head_srcu_data)
  113. #else
  114. #define _SRCU_NOTIFIER_HEAD(name, mod) \
  115. mod struct srcu_notifier_head name = \
  116. SRCU_NOTIFIER_INIT(name, name)
  117. #endif
  118. #define SRCU_NOTIFIER_HEAD(name) \
  119. _SRCU_NOTIFIER_HEAD(name, /* not static */)
  120. #define SRCU_NOTIFIER_HEAD_STATIC(name) \
  121. _SRCU_NOTIFIER_HEAD(name, static)
  122. #ifdef __KERNEL__
  123. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  124. struct notifier_block *nb);
  125. extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
  126. struct notifier_block *nb);
  127. extern int raw_notifier_chain_register(struct raw_notifier_head *nh,
  128. struct notifier_block *nb);
  129. extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
  130. struct notifier_block *nb);
  131. extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
  132. struct notifier_block *nb);
  133. extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
  134. struct notifier_block *nb);
  135. extern int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
  136. struct notifier_block *nb);
  137. extern int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
  138. struct notifier_block *nb);
  139. extern int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  140. unsigned long val, void *v);
  141. extern int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  142. unsigned long val, void *v);
  143. extern int raw_notifier_call_chain(struct raw_notifier_head *nh,
  144. unsigned long val, void *v);
  145. extern int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
  146. unsigned long val, void *v);
  147. extern int atomic_notifier_call_chain_robust(struct atomic_notifier_head *nh,
  148. unsigned long val_up, unsigned long val_down, void *v);
  149. extern int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
  150. unsigned long val_up, unsigned long val_down, void *v);
  151. extern int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
  152. unsigned long val_up, unsigned long val_down, void *v);
  153. #define NOTIFY_DONE 0x0000 /* Don't care */
  154. #define NOTIFY_OK 0x0001 /* Suits me */
  155. #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
  156. #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
  157. /* Bad/Veto action */
  158. /*
  159. * Clean way to return from the notifier and stop further calls.
  160. */
  161. #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
  162. /* Encapsulate (negative) errno value (in particular, NOTIFY_BAD <=> EPERM). */
  163. static inline int notifier_from_errno(int err)
  164. {
  165. if (err)
  166. return NOTIFY_STOP_MASK | (NOTIFY_OK - err);
  167. return NOTIFY_OK;
  168. }
  169. /* Restore (negative) errno value from notify return value. */
  170. static inline int notifier_to_errno(int ret)
  171. {
  172. ret &= ~NOTIFY_STOP_MASK;
  173. return ret > NOTIFY_OK ? NOTIFY_OK - ret : 0;
  174. }
  175. /*
  176. * Declared notifiers so far. I can imagine quite a few more chains
  177. * over time (eg laptop power reset chains, reboot chain (to clean
  178. * device units up), device [un]mount chain, module load/unload chain,
  179. * low memory chain, screenblank chain (for plug in modular screenblankers)
  180. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  181. */
  182. /* CPU notfiers are defined in include/linux/cpu.h. */
  183. /* netdevice notifiers are defined in include/linux/netdevice.h */
  184. /* reboot notifiers are defined in include/linux/reboot.h. */
  185. /* Hibernation and suspend events are defined in include/linux/suspend.h. */
  186. /* Virtual Terminal events are defined in include/linux/vt.h. */
  187. #define NETLINK_URELEASE 0x0001 /* Unicast netlink socket released */
  188. /* Console keyboard events.
  189. * Note: KBD_KEYCODE is always sent before KBD_UNBOUND_KEYCODE, KBD_UNICODE and
  190. * KBD_KEYSYM. */
  191. #define KBD_KEYCODE 0x0001 /* Keyboard keycode, called before any other */
  192. #define KBD_UNBOUND_KEYCODE 0x0002 /* Keyboard keycode which is not bound to any other */
  193. #define KBD_UNICODE 0x0003 /* Keyboard unicode */
  194. #define KBD_KEYSYM 0x0004 /* Keyboard keysym */
  195. #define KBD_POST_KEYSYM 0x0005 /* Called after keyboard keysym interpretation */
  196. extern struct blocking_notifier_head reboot_notifier_list;
  197. #endif /* __KERNEL__ */
  198. #endif /* _LINUX_NOTIFIER_H */