aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/notifier.h
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2006-03-27 04:16:30 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-03-27 11:44:50 -0500
commite041c683412d5bf44dc2b109053e3b837b71742d (patch)
tree9d271066ef379da0c0fb3b8cb4137abd5d2ebba0 /include/linux/notifier.h
parent76b81e2b0e2241accebcc68e126bc5ab958661b9 (diff)
[PATCH] Notifier chain update: API changes
The kernel's implementation of notifier chains is unsafe. There is no protection against entries being added to or removed from a chain while the chain is in use. The issues were discussed in this thread: http://marc.theaimsgroup.com/?l=linux-kernel&m=113018709002036&w=2 We noticed that notifier chains in the kernel fall into two basic usage classes: "Blocking" chains are always called from a process context and the callout routines are allowed to sleep; "Atomic" chains can be called from an atomic context and the callout routines are not allowed to sleep. We decided to codify this distinction and make it part of the API. Therefore this set of patches introduces three new, parallel APIs: one for blocking notifiers, one for atomic notifiers, and one for "raw" notifiers (which is really just the old API under a new name). New kinds of data structures are used for the heads of the chains, and new routines are defined for registration, unregistration, and calling a chain. The three APIs are explained in include/linux/notifier.h and their implementation is in kernel/sys.c. With atomic and blocking chains, the implementation guarantees that the chain links will not be corrupted and that chain callers will not get messed up by entries being added or removed. For raw chains the implementation provides no guarantees at all; users of this API must provide their own protections. (The idea was that situations may come up where the assumptions of the atomic and blocking APIs are not appropriate, so it should be possible for users to handle these things in their own way.) There are some limitations, which should not be too hard to live with. For atomic/blocking chains, registration and unregistration must always be done in a process context since the chain is protected by a mutex/rwsem. Also, a callout routine for a non-raw chain must not try to register or unregister entries on its own chain. (This did happen in a couple of places and the code had to be changed to avoid it.) Since atomic chains may be called from within an NMI handler, they cannot use spinlocks for synchronization. Instead we use RCU. The overhead falls almost entirely in the unregister routine, which is okay since unregistration is much less frequent that calling a chain. Here is the list of chains that we adjusted and their classifications. None of them use the raw API, so for the moment it is only a placeholder. ATOMIC CHAINS ------------- arch/i386/kernel/traps.c: i386die_chain arch/ia64/kernel/traps.c: ia64die_chain arch/powerpc/kernel/traps.c: powerpc_die_chain arch/sparc64/kernel/traps.c: sparc64die_chain arch/x86_64/kernel/traps.c: die_chain drivers/char/ipmi/ipmi_si_intf.c: xaction_notifier_list kernel/panic.c: panic_notifier_list kernel/profile.c: task_free_notifier net/bluetooth/hci_core.c: hci_notifier net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_chain net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_expect_chain net/ipv6/addrconf.c: inet6addr_chain net/netfilter/nf_conntrack_core.c: nf_conntrack_chain net/netfilter/nf_conntrack_core.c: nf_conntrack_expect_chain net/netlink/af_netlink.c: netlink_chain BLOCKING CHAINS --------------- arch/powerpc/platforms/pseries/reconfig.c: pSeries_reconfig_chain arch/s390/kernel/process.c: idle_chain arch/x86_64/kernel/process.c idle_notifier drivers/base/memory.c: memory_chain drivers/cpufreq/cpufreq.c cpufreq_policy_notifier_list drivers/cpufreq/cpufreq.c cpufreq_transition_notifier_list drivers/macintosh/adb.c: adb_client_list drivers/macintosh/via-pmu.c sleep_notifier_list drivers/macintosh/via-pmu68k.c sleep_notifier_list drivers/macintosh/windfarm_core.c wf_client_list drivers/usb/core/notify.c usb_notifier_list drivers/video/fbmem.c fb_notifier_list kernel/cpu.c cpu_chain kernel/module.c module_notify_list kernel/profile.c munmap_notifier kernel/profile.c task_exit_notifier kernel/sys.c reboot_notifier_list net/core/dev.c netdev_chain net/decnet/dn_dev.c: dnaddr_chain net/ipv4/devinet.c: inetaddr_chain It's possible that some of these classifications are wrong. If they are, please let us know or submit a patch to fix them. Note that any chain that gets called very frequently should be atomic, because the rwsem read-locking used for blocking chains is very likely to incur cache misses on SMP systems. (However, if the chain's callout routines may sleep then the chain cannot be atomic.) The patch set was written by Alan Stern and Chandra Seetharaman, incorporating material written by Keith Owens and suggestions from Paul McKenney and Andrew Morton. [jes@sgi.com: restructure the notifier chain initialization macros] Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Chandra Seetharaman <sekharan@us.ibm.com> Signed-off-by: Jes Sorensen <jes@sgi.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'include/linux/notifier.h')
-rw-r--r--include/linux/notifier.h96
1 files changed, 89 insertions, 7 deletions
diff --git a/include/linux/notifier.h b/include/linux/notifier.h
index 5937dd6053c3..51dbab9710c7 100644
--- a/include/linux/notifier.h
+++ b/include/linux/notifier.h
@@ -10,25 +10,107 @@
10#ifndef _LINUX_NOTIFIER_H 10#ifndef _LINUX_NOTIFIER_H
11#define _LINUX_NOTIFIER_H 11#define _LINUX_NOTIFIER_H
12#include <linux/errno.h> 12#include <linux/errno.h>
13#include <linux/mutex.h>
14#include <linux/rwsem.h>
13 15
14struct notifier_block 16/*
15{ 17 * Notifier chains are of three types:
16 int (*notifier_call)(struct notifier_block *self, unsigned long, void *); 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 *
27 * atomic_notifier_chain_register() may be called from an atomic context,
28 * but blocking_notifier_chain_register() must be called from a process
29 * context. Ditto for the corresponding _unregister() routines.
30 *
31 * atomic_notifier_chain_unregister() and blocking_notifier_chain_unregister()
32 * _must not_ be called from within the call chain.
33 */
34
35struct notifier_block {
36 int (*notifier_call)(struct notifier_block *, unsigned long, void *);
17 struct notifier_block *next; 37 struct notifier_block *next;
18 int priority; 38 int priority;
19}; 39};
20 40
41struct atomic_notifier_head {
42 spinlock_t lock;
43 struct notifier_block *head;
44};
45
46struct blocking_notifier_head {
47 struct rw_semaphore rwsem;
48 struct notifier_block *head;
49};
50
51struct raw_notifier_head {
52 struct notifier_block *head;
53};
54
55#define ATOMIC_INIT_NOTIFIER_HEAD(name) do { \
56 spin_lock_init(&(name)->lock); \
57 (name)->head = NULL; \
58 } while (0)
59#define BLOCKING_INIT_NOTIFIER_HEAD(name) do { \
60 init_rwsem(&(name)->rwsem); \
61 (name)->head = NULL; \
62 } while (0)
63#define RAW_INIT_NOTIFIER_HEAD(name) do { \
64 (name)->head = NULL; \
65 } while (0)
66
67#define ATOMIC_NOTIFIER_INIT(name) { \
68 .lock = SPIN_LOCK_UNLOCKED, \
69 .head = NULL }
70#define BLOCKING_NOTIFIER_INIT(name) { \
71 .rwsem = __RWSEM_INITIALIZER((name).rwsem), \
72 .head = NULL }
73#define RAW_NOTIFIER_INIT(name) { \
74 .head = NULL }
75
76#define ATOMIC_NOTIFIER_HEAD(name) \
77 struct atomic_notifier_head name = \
78 ATOMIC_NOTIFIER_INIT(name)
79#define BLOCKING_NOTIFIER_HEAD(name) \
80 struct blocking_notifier_head name = \
81 BLOCKING_NOTIFIER_INIT(name)
82#define RAW_NOTIFIER_HEAD(name) \
83 struct raw_notifier_head name = \
84 RAW_NOTIFIER_INIT(name)
21 85
22#ifdef __KERNEL__ 86#ifdef __KERNEL__
23 87
24extern int notifier_chain_register(struct notifier_block **list, struct notifier_block *n); 88extern int atomic_notifier_chain_register(struct atomic_notifier_head *,
25extern int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n); 89 struct notifier_block *);
26extern int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v); 90extern int blocking_notifier_chain_register(struct blocking_notifier_head *,
91 struct notifier_block *);
92extern int raw_notifier_chain_register(struct raw_notifier_head *,
93 struct notifier_block *);
94
95extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *,
96 struct notifier_block *);
97extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *,
98 struct notifier_block *);
99extern int raw_notifier_chain_unregister(struct raw_notifier_head *,
100 struct notifier_block *);
101
102extern int atomic_notifier_call_chain(struct atomic_notifier_head *,
103 unsigned long val, void *v);
104extern int blocking_notifier_call_chain(struct blocking_notifier_head *,
105 unsigned long val, void *v);
106extern int raw_notifier_call_chain(struct raw_notifier_head *,
107 unsigned long val, void *v);
27 108
28#define NOTIFY_DONE 0x0000 /* Don't care */ 109#define NOTIFY_DONE 0x0000 /* Don't care */
29#define NOTIFY_OK 0x0001 /* Suits me */ 110#define NOTIFY_OK 0x0001 /* Suits me */
30#define NOTIFY_STOP_MASK 0x8000 /* Don't call further */ 111#define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
31#define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002) /* Bad/Veto action */ 112#define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
113 /* Bad/Veto action */
32/* 114/*
33 * Clean way to return from the notifier and stop further calls. 115 * Clean way to return from the notifier and stop further calls.
34 */ 116 */