xref: /linux-6.15/include/linux/notifier.h (revision f2fa0fd4)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Routines to manage notifier chains for passing status changes to any
41da177e4SLinus Torvalds  *	interested routines. We need this instead of hard coded call lists so
51da177e4SLinus Torvalds  *	that modules can poke their nose into the innards. The network devices
61da177e4SLinus Torvalds  *	needed them so here they are for the rest of you.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *				Alan Cox <[email protected]>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #ifndef _LINUX_NOTIFIER_H
121da177e4SLinus Torvalds #define _LINUX_NOTIFIER_H
131da177e4SLinus Torvalds #include <linux/errno.h>
14e041c683SAlan Stern #include <linux/mutex.h>
15e041c683SAlan Stern #include <linux/rwsem.h>
16eabc0694SAlan Stern #include <linux/srcu.h>
171da177e4SLinus Torvalds 
18e041c683SAlan Stern /*
19eabc0694SAlan Stern  * Notifier chains are of four types:
20e041c683SAlan Stern  *
21e041c683SAlan Stern  *	Atomic notifier chains: Chain callbacks run in interrupt/atomic
22e041c683SAlan Stern  *		context. Callouts are not allowed to block.
23e041c683SAlan Stern  *	Blocking notifier chains: Chain callbacks run in process context.
24e041c683SAlan Stern  *		Callouts are allowed to block.
25e041c683SAlan Stern  *	Raw notifier chains: There are no restrictions on callbacks,
26e041c683SAlan Stern  *		registration, or unregistration.  All locking and protection
27e041c683SAlan Stern  *		must be provided by the caller.
28eabc0694SAlan Stern  *	SRCU notifier chains: A variant of blocking notifier chains, with
29eabc0694SAlan Stern  *		the same restrictions.
30e041c683SAlan Stern  *
31e041c683SAlan Stern  * atomic_notifier_chain_register() may be called from an atomic context,
32eabc0694SAlan Stern  * but blocking_notifier_chain_register() and srcu_notifier_chain_register()
33eabc0694SAlan Stern  * must be called from a process context.  Ditto for the corresponding
34eabc0694SAlan Stern  * _unregister() routines.
35e041c683SAlan Stern  *
36eabc0694SAlan Stern  * atomic_notifier_chain_unregister(), blocking_notifier_chain_unregister(),
37eabc0694SAlan Stern  * and srcu_notifier_chain_unregister() _must not_ be called from within
38eabc0694SAlan Stern  * the call chain.
39eabc0694SAlan Stern  *
40eabc0694SAlan Stern  * SRCU notifier chains are an alternative form of blocking notifier chains.
41eabc0694SAlan Stern  * They use SRCU (Sleepable Read-Copy Update) instead of rw-semaphores for
42eabc0694SAlan Stern  * protection of the chain links.  This means there is _very_ low overhead
43eabc0694SAlan Stern  * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
44eabc0694SAlan Stern  * As compensation, srcu_notifier_chain_unregister() is rather expensive.
45eabc0694SAlan Stern  * SRCU notifier chains should be used when the chain will be called very
469c80172bSSebastian Andrzej Siewior  * often but notifier_blocks will seldom be removed.
47e041c683SAlan Stern  */
48e041c683SAlan Stern 
4927d50c7eSThomas Gleixner struct notifier_block;
5027d50c7eSThomas Gleixner 
51f02c6968SAndrew Morton typedef	int (*notifier_fn_t)(struct notifier_block *nb,
52f02c6968SAndrew Morton 			unsigned long action, void *data);
53f02c6968SAndrew Morton 
54e041c683SAlan Stern struct notifier_block {
55f02c6968SAndrew Morton 	notifier_fn_t notifier_call;
56374a8e0dSArnd Bergmann 	struct notifier_block __rcu *next;
571da177e4SLinus Torvalds 	int priority;
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
60e041c683SAlan Stern struct atomic_notifier_head {
61e041c683SAlan Stern 	spinlock_t lock;
62374a8e0dSArnd Bergmann 	struct notifier_block __rcu *head;
63e041c683SAlan Stern };
64e041c683SAlan Stern 
65e041c683SAlan Stern struct blocking_notifier_head {
66e041c683SAlan Stern 	struct rw_semaphore rwsem;
67374a8e0dSArnd Bergmann 	struct notifier_block __rcu *head;
68e041c683SAlan Stern };
69e041c683SAlan Stern 
70e041c683SAlan Stern struct raw_notifier_head {
71374a8e0dSArnd Bergmann 	struct notifier_block __rcu *head;
72e041c683SAlan Stern };
73e041c683SAlan Stern 
74eabc0694SAlan Stern struct srcu_notifier_head {
75eabc0694SAlan Stern 	struct mutex mutex;
7695433f72SPaul E. McKenney 	struct srcu_usage srcuu;
77eabc0694SAlan Stern 	struct srcu_struct srcu;
78374a8e0dSArnd Bergmann 	struct notifier_block __rcu *head;
79eabc0694SAlan Stern };
80eabc0694SAlan Stern 
81e041c683SAlan Stern #define ATOMIC_INIT_NOTIFIER_HEAD(name) do {	\
82e041c683SAlan Stern 		spin_lock_init(&(name)->lock);	\
83e041c683SAlan Stern 		(name)->head = NULL;		\
84e041c683SAlan Stern 	} while (0)
85e041c683SAlan Stern #define BLOCKING_INIT_NOTIFIER_HEAD(name) do {	\
86e041c683SAlan Stern 		init_rwsem(&(name)->rwsem);	\
87e041c683SAlan Stern 		(name)->head = NULL;		\
88e041c683SAlan Stern 	} while (0)
89e041c683SAlan Stern #define RAW_INIT_NOTIFIER_HEAD(name) do {	\
90e041c683SAlan Stern 		(name)->head = NULL;		\
91e041c683SAlan Stern 	} while (0)
92e041c683SAlan Stern 
939c80172bSSebastian Andrzej Siewior /* srcu_notifier_heads must be cleaned up dynamically */
94eabc0694SAlan Stern extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
95eabc0694SAlan Stern #define srcu_cleanup_notifier_head(name)	\
96eabc0694SAlan Stern 		cleanup_srcu_struct(&(name)->srcu);
97eabc0694SAlan Stern 
98e041c683SAlan Stern #define ATOMIC_NOTIFIER_INIT(name) {				\
99e4d91918SIngo Molnar 		.lock = __SPIN_LOCK_UNLOCKED(name.lock),	\
100e041c683SAlan Stern 		.head = NULL }
101e041c683SAlan Stern #define BLOCKING_NOTIFIER_INIT(name) {				\
102e041c683SAlan Stern 		.rwsem = __RWSEM_INITIALIZER((name).rwsem),	\
103e041c683SAlan Stern 		.head = NULL }
104e041c683SAlan Stern #define RAW_NOTIFIER_INIT(name)	{				\
105e041c683SAlan Stern 		.head = NULL }
1069c80172bSSebastian Andrzej Siewior 
107*de29a96aSChen-Yu Tsai #define SRCU_NOTIFIER_INIT(name, pcpu)				\
108*de29a96aSChen-Yu Tsai 	{							\
109*de29a96aSChen-Yu Tsai 		.mutex = __MUTEX_INITIALIZER(name.mutex),	\
110*de29a96aSChen-Yu Tsai 		.head = NULL,					\
111*de29a96aSChen-Yu Tsai 		.srcuu = __SRCU_USAGE_INIT(name.srcuu),		\
112*de29a96aSChen-Yu Tsai 		.srcu = __SRCU_STRUCT_INIT(name.srcu, name.srcuu, pcpu), \
113*de29a96aSChen-Yu Tsai 	}
114e041c683SAlan Stern 
115e041c683SAlan Stern #define ATOMIC_NOTIFIER_HEAD(name)				\
116e041c683SAlan Stern 	struct atomic_notifier_head name =			\
117e041c683SAlan Stern 		ATOMIC_NOTIFIER_INIT(name)
118e041c683SAlan Stern #define BLOCKING_NOTIFIER_HEAD(name)				\
119e041c683SAlan Stern 	struct blocking_notifier_head name =			\
120e041c683SAlan Stern 		BLOCKING_NOTIFIER_INIT(name)
121e041c683SAlan Stern #define RAW_NOTIFIER_HEAD(name)					\
122e041c683SAlan Stern 	struct raw_notifier_head name =				\
123e041c683SAlan Stern 		RAW_NOTIFIER_INIT(name)
1241da177e4SLinus Torvalds 
1259c80172bSSebastian Andrzej Siewior #ifdef CONFIG_TREE_SRCU
1269c80172bSSebastian Andrzej Siewior #define _SRCU_NOTIFIER_HEAD(name, mod)				\
12794e297c5SSam Protsenko 	static DEFINE_PER_CPU(struct srcu_data, name##_head_srcu_data); \
1289c80172bSSebastian Andrzej Siewior 	mod struct srcu_notifier_head name =			\
1299c80172bSSebastian Andrzej Siewior 			SRCU_NOTIFIER_INIT(name, name##_head_srcu_data)
1309c80172bSSebastian Andrzej Siewior 
1319c80172bSSebastian Andrzej Siewior #else
1329c80172bSSebastian Andrzej Siewior #define _SRCU_NOTIFIER_HEAD(name, mod)				\
1339c80172bSSebastian Andrzej Siewior 	mod struct srcu_notifier_head name =			\
1349c80172bSSebastian Andrzej Siewior 			SRCU_NOTIFIER_INIT(name, name)
1359c80172bSSebastian Andrzej Siewior 
1369c80172bSSebastian Andrzej Siewior #endif
1379c80172bSSebastian Andrzej Siewior 
1389c80172bSSebastian Andrzej Siewior #define SRCU_NOTIFIER_HEAD(name)				\
1399c80172bSSebastian Andrzej Siewior 	_SRCU_NOTIFIER_HEAD(name, /* not static */)
1409c80172bSSebastian Andrzej Siewior 
1419c80172bSSebastian Andrzej Siewior #define SRCU_NOTIFIER_HEAD_STATIC(name)				\
1429c80172bSSebastian Andrzej Siewior 	_SRCU_NOTIFIER_HEAD(name, static)
1439c80172bSSebastian Andrzej Siewior 
1441da177e4SLinus Torvalds #ifdef __KERNEL__
1451da177e4SLinus Torvalds 
1466f7cc11aSGautham R Shenoy extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
1476f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
1486f7cc11aSGautham R Shenoy extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
1496f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
1506f7cc11aSGautham R Shenoy extern int raw_notifier_chain_register(struct raw_notifier_head *nh,
1516f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
1526f7cc11aSGautham R Shenoy extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
1536f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
154e041c683SAlan Stern 
155c82f898dSDmitry Osipenko extern int atomic_notifier_chain_register_unique_prio(
156c82f898dSDmitry Osipenko 		struct atomic_notifier_head *nh, struct notifier_block *nb);
157c82f898dSDmitry Osipenko extern int blocking_notifier_chain_register_unique_prio(
158c82f898dSDmitry Osipenko 		struct blocking_notifier_head *nh, struct notifier_block *nb);
159c82f898dSDmitry Osipenko 
1606f7cc11aSGautham R Shenoy extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
1616f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
1626f7cc11aSGautham R Shenoy extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
1636f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
1646f7cc11aSGautham R Shenoy extern int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
1656f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
1666f7cc11aSGautham R Shenoy extern int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
1676f7cc11aSGautham R Shenoy 		struct notifier_block *nb);
168e041c683SAlan Stern 
1696f7cc11aSGautham R Shenoy extern int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
170e041c683SAlan Stern 		unsigned long val, void *v);
1716f7cc11aSGautham R Shenoy extern int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
172e041c683SAlan Stern 		unsigned long val, void *v);
1736f7cc11aSGautham R Shenoy extern int raw_notifier_call_chain(struct raw_notifier_head *nh,
174e041c683SAlan Stern 		unsigned long val, void *v);
1756f7cc11aSGautham R Shenoy extern int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
176eabc0694SAlan Stern 		unsigned long val, void *v);
17770d93298SPeter Zijlstra 
17870d93298SPeter Zijlstra extern int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
17970d93298SPeter Zijlstra 		unsigned long val_up, unsigned long val_down, void *v);
18070d93298SPeter Zijlstra extern int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
18170d93298SPeter Zijlstra 		unsigned long val_up, unsigned long val_down, void *v);
1821da177e4SLinus Torvalds 
18313dfd97aSDmitry Osipenko extern bool atomic_notifier_call_chain_is_empty(struct atomic_notifier_head *nh);
18413dfd97aSDmitry Osipenko 
1851da177e4SLinus Torvalds #define NOTIFY_DONE		0x0000		/* Don't care */
1861da177e4SLinus Torvalds #define NOTIFY_OK		0x0001		/* Suits me */
1871da177e4SLinus Torvalds #define NOTIFY_STOP_MASK	0x8000		/* Don't call further */
188e041c683SAlan Stern #define NOTIFY_BAD		(NOTIFY_STOP_MASK|0x0002)
189e041c683SAlan Stern 						/* Bad/Veto action */
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Clean way to return from the notifier and stop further calls.
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds #define NOTIFY_STOP		(NOTIFY_OK|NOTIFY_STOP_MASK)
1941da177e4SLinus Torvalds 
195fcc5a03aSHerbert Xu /* Encapsulate (negative) errno value (in particular, NOTIFY_BAD <=> EPERM). */
notifier_from_errno(int err)196fcc5a03aSHerbert Xu static inline int notifier_from_errno(int err)
197fcc5a03aSHerbert Xu {
198b957e043SAkinobu Mita 	if (err)
199fcc5a03aSHerbert Xu 		return NOTIFY_STOP_MASK | (NOTIFY_OK - err);
200b957e043SAkinobu Mita 
201b957e043SAkinobu Mita 	return NOTIFY_OK;
202fcc5a03aSHerbert Xu }
203fcc5a03aSHerbert Xu 
204fcc5a03aSHerbert Xu /* Restore (negative) errno value from notify return value. */
notifier_to_errno(int ret)205fcc5a03aSHerbert Xu static inline int notifier_to_errno(int ret)
206fcc5a03aSHerbert Xu {
207fcc5a03aSHerbert Xu 	ret &= ~NOTIFY_STOP_MASK;
208fcc5a03aSHerbert Xu 	return ret > NOTIFY_OK ? NOTIFY_OK - ret : 0;
209fcc5a03aSHerbert Xu }
210fcc5a03aSHerbert Xu 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  *	Declared notifiers so far. I can imagine quite a few more chains
2131da177e4SLinus Torvalds  *	over time (eg laptop power reset chains, reboot chain (to clean
2141da177e4SLinus Torvalds  *	device units up), device [un]mount chain, module load/unload chain,
2151da177e4SLinus Torvalds  *	low memory chain, screenblank chain (for plug in modular screenblankers)
2161da177e4SLinus Torvalds  *	VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
2171da177e4SLinus Torvalds  */
2181da177e4SLinus Torvalds 
21980f1ff97SAmerigo Wang /* CPU notfiers are defined in include/linux/cpu.h. */
22080f1ff97SAmerigo Wang 
221dcfe1421SAmerigo Wang /* netdevice notifiers are defined in include/linux/netdevice.h */
2221da177e4SLinus Torvalds 
223c5f41752SAmerigo Wang /* reboot notifiers are defined in include/linux/reboot.h. */
2241da177e4SLinus Torvalds 
22535eb6db1SAmerigo Wang /* Hibernation and suspend events are defined in include/linux/suspend.h. */
2261da177e4SLinus Torvalds 
227a376d3d6SAmerigo Wang /* Virtual Terminal events are defined in include/linux/vt.h. */
228a376d3d6SAmerigo Wang 
22935eb6db1SAmerigo Wang #define NETLINK_URELEASE	0x0001	/* Unicast netlink socket released */
230b10d9117SRafael J. Wysocki 
23141ab4396SSamuel Thibault /* Console keyboard events.
23241ab4396SSamuel Thibault  * Note: KBD_KEYCODE is always sent before KBD_UNBOUND_KEYCODE, KBD_UNICODE and
23341ab4396SSamuel Thibault  * KBD_KEYSYM. */
23441ab4396SSamuel Thibault #define KBD_KEYCODE		0x0001 /* Keyboard keycode, called before any other */
23541ab4396SSamuel Thibault #define KBD_UNBOUND_KEYCODE	0x0002 /* Keyboard keycode which is not bound to any other */
23641ab4396SSamuel Thibault #define KBD_UNICODE		0x0003 /* Keyboard unicode */
23741ab4396SSamuel Thibault #define KBD_KEYSYM		0x0004 /* Keyboard keysym */
23841ab4396SSamuel Thibault #define KBD_POST_KEYSYM		0x0005 /* Called after keyboard keysym interpretation */
23941ab4396SSamuel Thibault 
2401da177e4SLinus Torvalds #endif /* __KERNEL__ */
2411da177e4SLinus Torvalds #endif /* _LINUX_NOTIFIER_H */
242