blob: 0096a05395e380a35fe25a6329e05953eb66ab66 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
11#ifndef _LINUX_NOTIFIER_H
12#define _LINUX_NOTIFIER_H
13#include <linux/errno.h>
Alan Sterne041c682006-03-27 01:16:30 -080014#include <linux/mutex.h>
15#include <linux/rwsem.h>
Alan Sterneabc0692006-10-04 02:17:04 -070016#include <linux/srcu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Alan Sterne041c682006-03-27 01:16:30 -080018/*
Alan Sterneabc0692006-10-04 02:17:04 -070019 * Notifier chains are of four types:
Alan Sterne041c682006-03-27 01:16:30 -080020 *
21 * Atomic notifier chains: Chain callbacks run in interrupt/atomic
22 * context. Callouts are not allowed to block.
23 * Blocking notifier chains: Chain callbacks run in process context.
24 * Callouts are allowed to block.
25 * Raw notifier chains: There are no restrictions on callbacks,
26 * registration, or unregistration. All locking and protection
27 * must be provided by the caller.
Alan Sterneabc0692006-10-04 02:17:04 -070028 * SRCU notifier chains: A variant of blocking notifier chains, with
29 * the same restrictions.
Alan Sterne041c682006-03-27 01:16:30 -080030 *
31 * atomic_notifier_chain_register() may be called from an atomic context,
Alan Sterneabc0692006-10-04 02:17:04 -070032 * but blocking_notifier_chain_register() and srcu_notifier_chain_register()
33 * must be called from a process context. Ditto for the corresponding
34 * _unregister() routines.
Alan Sterne041c682006-03-27 01:16:30 -080035 *
Alan Sterneabc0692006-10-04 02:17:04 -070036 * atomic_notifier_chain_unregister(), blocking_notifier_chain_unregister(),
37 * and srcu_notifier_chain_unregister() _must not_ be called from within
38 * the call chain.
39 *
40 * SRCU notifier chains are an alternative form of blocking notifier chains.
41 * They use SRCU (Sleepable Read-Copy Update) instead of rw-semaphores for
42 * protection of the chain links. This means there is _very_ low overhead
43 * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
44 * As compensation, srcu_notifier_chain_unregister() is rather expensive.
45 * SRCU notifier chains should be used when the chain will be called very
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +020046 * often but notifier_blocks will seldom be removed.
Alan Sterne041c682006-03-27 01:16:30 -080047 */
48
Thomas Gleixner27d50c72016-02-26 18:43:44 +000049struct notifier_block;
50
Andrew Mortonf02c6962013-04-29 15:08:04 -070051typedef int (*notifier_fn_t)(struct notifier_block *nb,
52 unsigned long action, void *data);
53
Alan Sterne041c682006-03-27 01:16:30 -080054struct notifier_block {
Andrew Mortonf02c6962013-04-29 15:08:04 -070055 notifier_fn_t notifier_call;
Arnd Bergmann374a8e02010-02-24 20:00:13 +010056 struct notifier_block __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int priority;
58};
59
Alan Sterne041c682006-03-27 01:16:30 -080060struct atomic_notifier_head {
61 spinlock_t lock;
Arnd Bergmann374a8e02010-02-24 20:00:13 +010062 struct notifier_block __rcu *head;
Alan Sterne041c682006-03-27 01:16:30 -080063};
64
65struct blocking_notifier_head {
66 struct rw_semaphore rwsem;
Arnd Bergmann374a8e02010-02-24 20:00:13 +010067 struct notifier_block __rcu *head;
Alan Sterne041c682006-03-27 01:16:30 -080068};
69
70struct raw_notifier_head {
Arnd Bergmann374a8e02010-02-24 20:00:13 +010071 struct notifier_block __rcu *head;
Alan Sterne041c682006-03-27 01:16:30 -080072};
73
Alan Sterneabc0692006-10-04 02:17:04 -070074struct srcu_notifier_head {
75 struct mutex mutex;
76 struct srcu_struct srcu;
Arnd Bergmann374a8e02010-02-24 20:00:13 +010077 struct notifier_block __rcu *head;
Alan Sterneabc0692006-10-04 02:17:04 -070078};
79
Alan Sterne041c682006-03-27 01:16:30 -080080#define ATOMIC_INIT_NOTIFIER_HEAD(name) do { \
81 spin_lock_init(&(name)->lock); \
82 (name)->head = NULL; \
83 } while (0)
84#define BLOCKING_INIT_NOTIFIER_HEAD(name) do { \
85 init_rwsem(&(name)->rwsem); \
86 (name)->head = NULL; \
87 } while (0)
88#define RAW_INIT_NOTIFIER_HEAD(name) do { \
89 (name)->head = NULL; \
90 } while (0)
91
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +020092/* srcu_notifier_heads must be cleaned up dynamically */
Alan Sterneabc0692006-10-04 02:17:04 -070093extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
94#define srcu_cleanup_notifier_head(name) \
95 cleanup_srcu_struct(&(name)->srcu);
96
Alan Sterne041c682006-03-27 01:16:30 -080097#define ATOMIC_NOTIFIER_INIT(name) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070098 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Alan Sterne041c682006-03-27 01:16:30 -080099 .head = NULL }
100#define BLOCKING_NOTIFIER_INIT(name) { \
101 .rwsem = __RWSEM_INITIALIZER((name).rwsem), \
102 .head = NULL }
103#define RAW_NOTIFIER_INIT(name) { \
104 .head = NULL }
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +0200105
106#define SRCU_NOTIFIER_INIT(name, pcpu) \
107 { \
108 .mutex = __MUTEX_INITIALIZER(name.mutex), \
109 .head = NULL, \
110 .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
111 }
Alan Sterne041c682006-03-27 01:16:30 -0800112
113#define ATOMIC_NOTIFIER_HEAD(name) \
114 struct atomic_notifier_head name = \
115 ATOMIC_NOTIFIER_INIT(name)
116#define BLOCKING_NOTIFIER_HEAD(name) \
117 struct blocking_notifier_head name = \
118 BLOCKING_NOTIFIER_INIT(name)
119#define RAW_NOTIFIER_HEAD(name) \
120 struct raw_notifier_head name = \
121 RAW_NOTIFIER_INIT(name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +0200123#ifdef CONFIG_TREE_SRCU
124#define _SRCU_NOTIFIER_HEAD(name, mod) \
Sam Protsenko94e297c2018-11-02 15:47:53 -0700125 static DEFINE_PER_CPU(struct srcu_data, name##_head_srcu_data); \
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +0200126 mod struct srcu_notifier_head name = \
127 SRCU_NOTIFIER_INIT(name, name##_head_srcu_data)
128
129#else
130#define _SRCU_NOTIFIER_HEAD(name, mod) \
131 mod struct srcu_notifier_head name = \
132 SRCU_NOTIFIER_INIT(name, name)
133
134#endif
135
136#define SRCU_NOTIFIER_HEAD(name) \
137 _SRCU_NOTIFIER_HEAD(name, /* not static */)
138
139#define SRCU_NOTIFIER_HEAD_STATIC(name) \
140 _SRCU_NOTIFIER_HEAD(name, static)
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#ifdef __KERNEL__
143
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700144extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
145 struct notifier_block *nb);
146extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
147 struct notifier_block *nb);
148extern int raw_notifier_chain_register(struct raw_notifier_head *nh,
149 struct notifier_block *nb);
150extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
151 struct notifier_block *nb);
Alan Sterne041c682006-03-27 01:16:30 -0800152
Nadia Derbey6546bc42008-04-29 01:00:45 -0700153extern int blocking_notifier_chain_cond_register(
154 struct blocking_notifier_head *nh,
155 struct notifier_block *nb);
156
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700157extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
158 struct notifier_block *nb);
159extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
160 struct notifier_block *nb);
161extern int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
162 struct notifier_block *nb);
163extern int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
164 struct notifier_block *nb);
Alan Sterne041c682006-03-27 01:16:30 -0800165
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700166extern int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
Alan Sterne041c682006-03-27 01:16:30 -0800167 unsigned long val, void *v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700168extern int __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
169 unsigned long val, void *v, int nr_to_call, int *nr_calls);
170extern int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
Alan Sterne041c682006-03-27 01:16:30 -0800171 unsigned long val, void *v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700172extern int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
173 unsigned long val, void *v, int nr_to_call, int *nr_calls);
174extern int raw_notifier_call_chain(struct raw_notifier_head *nh,
Alan Sterne041c682006-03-27 01:16:30 -0800175 unsigned long val, void *v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700176extern int __raw_notifier_call_chain(struct raw_notifier_head *nh,
177 unsigned long val, void *v, int nr_to_call, int *nr_calls);
178extern int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
Alan Sterneabc0692006-10-04 02:17:04 -0700179 unsigned long val, void *v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700180extern int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
181 unsigned long val, void *v, int nr_to_call, int *nr_calls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183#define NOTIFY_DONE 0x0000 /* Don't care */
184#define NOTIFY_OK 0x0001 /* Suits me */
185#define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
Alan Sterne041c682006-03-27 01:16:30 -0800186#define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
187 /* Bad/Veto action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * Clean way to return from the notifier and stop further calls.
190 */
191#define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
192
Herbert Xufcc5a032007-07-30 17:03:38 -0700193/* Encapsulate (negative) errno value (in particular, NOTIFY_BAD <=> EPERM). */
194static inline int notifier_from_errno(int err)
195{
Akinobu Mitab957e042010-05-26 14:43:29 -0700196 if (err)
197 return NOTIFY_STOP_MASK | (NOTIFY_OK - err);
198
199 return NOTIFY_OK;
Herbert Xufcc5a032007-07-30 17:03:38 -0700200}
201
202/* Restore (negative) errno value from notify return value. */
203static inline int notifier_to_errno(int ret)
204{
205 ret &= ~NOTIFY_STOP_MASK;
206 return ret > NOTIFY_OK ? NOTIFY_OK - ret : 0;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Declared notifiers so far. I can imagine quite a few more chains
211 * over time (eg laptop power reset chains, reboot chain (to clean
212 * device units up), device [un]mount chain, module load/unload chain,
213 * low memory chain, screenblank chain (for plug in modular screenblankers)
214 * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
215 */
216
Amerigo Wang80f1ff92011-07-25 17:13:08 -0700217/* CPU notfiers are defined in include/linux/cpu.h. */
218
Amerigo Wangdcfe1422011-07-25 17:13:09 -0700219/* netdevice notifiers are defined in include/linux/netdevice.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Amerigo Wangc5f41752011-07-25 17:13:10 -0700221/* reboot notifiers are defined in include/linux/reboot.h. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Amerigo Wang35eb6db2011-07-25 17:13:11 -0700223/* Hibernation and suspend events are defined in include/linux/suspend.h. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Amerigo Wanga376d3d2011-07-25 17:13:12 -0700225/* Virtual Terminal events are defined in include/linux/vt.h. */
226
Amerigo Wang35eb6db2011-07-25 17:13:11 -0700227#define NETLINK_URELEASE 0x0001 /* Unicast netlink socket released */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700228
Samuel Thibault41ab4392007-10-18 23:39:12 -0700229/* Console keyboard events.
230 * Note: KBD_KEYCODE is always sent before KBD_UNBOUND_KEYCODE, KBD_UNICODE and
231 * KBD_KEYSYM. */
232#define KBD_KEYCODE 0x0001 /* Keyboard keycode, called before any other */
233#define KBD_UNBOUND_KEYCODE 0x0002 /* Keyboard keycode which is not bound to any other */
234#define KBD_UNICODE 0x0003 /* Keyboard unicode */
235#define KBD_KEYSYM 0x0004 /* Keyboard keysym */
236#define KBD_POST_KEYSYM 0x0005 /* Called after keyboard keysym interpretation */
237
Alexey Dobriyanfe9d4f52007-10-18 23:39:16 -0700238extern struct blocking_notifier_head reboot_notifier_list;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#endif /* __KERNEL__ */
241#endif /* _LINUX_NOTIFIER_H */