diff options
author | Alan Stern <stern@rowland.harvard.edu> | 2006-03-27 04:16:30 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-03-27 11:44:50 -0500 |
commit | e041c683412d5bf44dc2b109053e3b837b71742d (patch) | |
tree | 9d271066ef379da0c0fb3b8cb4137abd5d2ebba0 /drivers/cpufreq/cpufreq.c | |
parent | 76b81e2b0e2241accebcc68e126bc5ab958661b9 (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 'drivers/cpufreq/cpufreq.c')
-rw-r--r-- | drivers/cpufreq/cpufreq.c | 61 |
1 files changed, 26 insertions, 35 deletions
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index aed80e6aec6d..9b6ae7dc8b8a 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c | |||
@@ -52,9 +52,8 @@ static void handle_update(void *data); | |||
52 | * changes to devices when the CPU clock speed changes. | 52 | * changes to devices when the CPU clock speed changes. |
53 | * The mutex locks both lists. | 53 | * The mutex locks both lists. |
54 | */ | 54 | */ |
55 | static struct notifier_block *cpufreq_policy_notifier_list; | 55 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
56 | static struct notifier_block *cpufreq_transition_notifier_list; | 56 | static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list); |
57 | static DECLARE_RWSEM (cpufreq_notifier_rwsem); | ||
58 | 57 | ||
59 | 58 | ||
60 | static LIST_HEAD(cpufreq_governor_list); | 59 | static LIST_HEAD(cpufreq_governor_list); |
@@ -247,8 +246,6 @@ void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) | |||
247 | dprintk("notification %u of frequency transition to %u kHz\n", | 246 | dprintk("notification %u of frequency transition to %u kHz\n", |
248 | state, freqs->new); | 247 | state, freqs->new); |
249 | 248 | ||
250 | down_read(&cpufreq_notifier_rwsem); | ||
251 | |||
252 | policy = cpufreq_cpu_data[freqs->cpu]; | 249 | policy = cpufreq_cpu_data[freqs->cpu]; |
253 | switch (state) { | 250 | switch (state) { |
254 | 251 | ||
@@ -266,20 +263,19 @@ void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) | |||
266 | freqs->old = policy->cur; | 263 | freqs->old = policy->cur; |
267 | } | 264 | } |
268 | } | 265 | } |
269 | notifier_call_chain(&cpufreq_transition_notifier_list, | 266 | blocking_notifier_call_chain(&cpufreq_transition_notifier_list, |
270 | CPUFREQ_PRECHANGE, freqs); | 267 | CPUFREQ_PRECHANGE, freqs); |
271 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); | 268 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
272 | break; | 269 | break; |
273 | 270 | ||
274 | case CPUFREQ_POSTCHANGE: | 271 | case CPUFREQ_POSTCHANGE: |
275 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); | 272 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); |
276 | notifier_call_chain(&cpufreq_transition_notifier_list, | 273 | blocking_notifier_call_chain(&cpufreq_transition_notifier_list, |
277 | CPUFREQ_POSTCHANGE, freqs); | 274 | CPUFREQ_POSTCHANGE, freqs); |
278 | if (likely(policy) && likely(policy->cpu == freqs->cpu)) | 275 | if (likely(policy) && likely(policy->cpu == freqs->cpu)) |
279 | policy->cur = freqs->new; | 276 | policy->cur = freqs->new; |
280 | break; | 277 | break; |
281 | } | 278 | } |
282 | up_read(&cpufreq_notifier_rwsem); | ||
283 | } | 279 | } |
284 | EXPORT_SYMBOL_GPL(cpufreq_notify_transition); | 280 | EXPORT_SYMBOL_GPL(cpufreq_notify_transition); |
285 | 281 | ||
@@ -1007,7 +1003,7 @@ static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg) | |||
1007 | freqs.old = cpu_policy->cur; | 1003 | freqs.old = cpu_policy->cur; |
1008 | freqs.new = cur_freq; | 1004 | freqs.new = cur_freq; |
1009 | 1005 | ||
1010 | notifier_call_chain(&cpufreq_transition_notifier_list, | 1006 | blocking_notifier_call_chain(&cpufreq_transition_notifier_list, |
1011 | CPUFREQ_SUSPENDCHANGE, &freqs); | 1007 | CPUFREQ_SUSPENDCHANGE, &freqs); |
1012 | adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); | 1008 | adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); |
1013 | 1009 | ||
@@ -1088,7 +1084,8 @@ static int cpufreq_resume(struct sys_device * sysdev) | |||
1088 | freqs.old = cpu_policy->cur; | 1084 | freqs.old = cpu_policy->cur; |
1089 | freqs.new = cur_freq; | 1085 | freqs.new = cur_freq; |
1090 | 1086 | ||
1091 | notifier_call_chain(&cpufreq_transition_notifier_list, | 1087 | blocking_notifier_call_chain( |
1088 | &cpufreq_transition_notifier_list, | ||
1092 | CPUFREQ_RESUMECHANGE, &freqs); | 1089 | CPUFREQ_RESUMECHANGE, &freqs); |
1093 | adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); | 1090 | adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); |
1094 | 1091 | ||
@@ -1125,24 +1122,24 @@ static struct sysdev_driver cpufreq_sysdev_driver = { | |||
1125 | * changes in cpufreq policy. | 1122 | * changes in cpufreq policy. |
1126 | * | 1123 | * |
1127 | * This function may sleep, and has the same return conditions as | 1124 | * This function may sleep, and has the same return conditions as |
1128 | * notifier_chain_register. | 1125 | * blocking_notifier_chain_register. |
1129 | */ | 1126 | */ |
1130 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) | 1127 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) |
1131 | { | 1128 | { |
1132 | int ret; | 1129 | int ret; |
1133 | 1130 | ||
1134 | down_write(&cpufreq_notifier_rwsem); | ||
1135 | switch (list) { | 1131 | switch (list) { |
1136 | case CPUFREQ_TRANSITION_NOTIFIER: | 1132 | case CPUFREQ_TRANSITION_NOTIFIER: |
1137 | ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb); | 1133 | ret = blocking_notifier_chain_register( |
1134 | &cpufreq_transition_notifier_list, nb); | ||
1138 | break; | 1135 | break; |
1139 | case CPUFREQ_POLICY_NOTIFIER: | 1136 | case CPUFREQ_POLICY_NOTIFIER: |
1140 | ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb); | 1137 | ret = blocking_notifier_chain_register( |
1138 | &cpufreq_policy_notifier_list, nb); | ||
1141 | break; | 1139 | break; |
1142 | default: | 1140 | default: |
1143 | ret = -EINVAL; | 1141 | ret = -EINVAL; |
1144 | } | 1142 | } |
1145 | up_write(&cpufreq_notifier_rwsem); | ||
1146 | 1143 | ||
1147 | return ret; | 1144 | return ret; |
1148 | } | 1145 | } |
@@ -1157,24 +1154,24 @@ EXPORT_SYMBOL(cpufreq_register_notifier); | |||
1157 | * Remove a driver from the CPU frequency notifier list. | 1154 | * Remove a driver from the CPU frequency notifier list. |
1158 | * | 1155 | * |
1159 | * This function may sleep, and has the same return conditions as | 1156 | * This function may sleep, and has the same return conditions as |
1160 | * notifier_chain_unregister. | 1157 | * blocking_notifier_chain_unregister. |
1161 | */ | 1158 | */ |
1162 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) | 1159 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) |
1163 | { | 1160 | { |
1164 | int ret; | 1161 | int ret; |
1165 | 1162 | ||
1166 | down_write(&cpufreq_notifier_rwsem); | ||
1167 | switch (list) { | 1163 | switch (list) { |
1168 | case CPUFREQ_TRANSITION_NOTIFIER: | 1164 | case CPUFREQ_TRANSITION_NOTIFIER: |
1169 | ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb); | 1165 | ret = blocking_notifier_chain_unregister( |
1166 | &cpufreq_transition_notifier_list, nb); | ||
1170 | break; | 1167 | break; |
1171 | case CPUFREQ_POLICY_NOTIFIER: | 1168 | case CPUFREQ_POLICY_NOTIFIER: |
1172 | ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb); | 1169 | ret = blocking_notifier_chain_unregister( |
1170 | &cpufreq_policy_notifier_list, nb); | ||
1173 | break; | 1171 | break; |
1174 | default: | 1172 | default: |
1175 | ret = -EINVAL; | 1173 | ret = -EINVAL; |
1176 | } | 1174 | } |
1177 | up_write(&cpufreq_notifier_rwsem); | ||
1178 | 1175 | ||
1179 | return ret; | 1176 | return ret; |
1180 | } | 1177 | } |
@@ -1346,29 +1343,23 @@ static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_poli | |||
1346 | if (ret) | 1343 | if (ret) |
1347 | goto error_out; | 1344 | goto error_out; |
1348 | 1345 | ||
1349 | down_read(&cpufreq_notifier_rwsem); | ||
1350 | |||
1351 | /* adjust if necessary - all reasons */ | 1346 | /* adjust if necessary - all reasons */ |
1352 | notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST, | 1347 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
1353 | policy); | 1348 | CPUFREQ_ADJUST, policy); |
1354 | 1349 | ||
1355 | /* adjust if necessary - hardware incompatibility*/ | 1350 | /* adjust if necessary - hardware incompatibility*/ |
1356 | notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE, | 1351 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
1357 | policy); | 1352 | CPUFREQ_INCOMPATIBLE, policy); |
1358 | 1353 | ||
1359 | /* verify the cpu speed can be set within this limit, | 1354 | /* verify the cpu speed can be set within this limit, |
1360 | which might be different to the first one */ | 1355 | which might be different to the first one */ |
1361 | ret = cpufreq_driver->verify(policy); | 1356 | ret = cpufreq_driver->verify(policy); |
1362 | if (ret) { | 1357 | if (ret) |
1363 | up_read(&cpufreq_notifier_rwsem); | ||
1364 | goto error_out; | 1358 | goto error_out; |
1365 | } | ||
1366 | 1359 | ||
1367 | /* notification of the new policy */ | 1360 | /* notification of the new policy */ |
1368 | notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY, | 1361 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
1369 | policy); | 1362 | CPUFREQ_NOTIFY, policy); |
1370 | |||
1371 | up_read(&cpufreq_notifier_rwsem); | ||
1372 | 1363 | ||
1373 | data->min = policy->min; | 1364 | data->min = policy->min; |
1374 | data->max = policy->max; | 1365 | data->max = policy->max; |