Skip to contentSkip to conversation

remove cpu hotplug bustification in cpufreq.

PATCH Dave Jones 6 participants 12 messages Jul 22, 2006 – Jul 24, 2006

Subsystems (2)
lore
Share & tools
lore

Dave Jones

author
#0 lore raw

This stuff makes my head hurt. Someone who is motivated enough to fix up hotplug-cpu can fix this up later. In the meantime, this patch should cure the lockdep warnings that seem to trigger very easily.

drivers/cpufreq/cpufreq_ondemand.c renamed from drivers/cpufreq/cpufreq_ondemand.c~ +0 -2
@@ -408,7 +408,6 @@ static int cpufreq_governor_dbs(struct c
 		break;
 
 	case CPUFREQ_GOV_LIMITS:
-		lock_cpu_hotplug();
 		mutex_lock(&dbs_mutex);
 		if (policy->max < this_dbs_info->cur_policy->cur)
 			__cpufreq_driver_target(this_dbs_info->cur_policy,
@@ -419,7 +418,6 @@ static int cpufreq_governor_dbs(struct c
 			                        policy->min,
 			                        CPUFREQ_RELATION_L);
 		mutex_unlock(&dbs_mutex);
-		unlock_cpu_hotplug();
 		break;
 	}
 	return 0;
drivers/cpufreq/cpufreq_stats.c renamed from drivers/cpufreq/cpufreq_stats.c~ +0 -4
@@ -350,12 +350,10 @@ __init cpufreq_stats_init(void)
 	}
 
 	register_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
-	lock_cpu_hotplug();
 	for_each_online_cpu(cpu) {
 		cpufreq_stat_cpu_callback(&cpufreq_stat_cpu_notifier, CPU_ONLINE,
 			(void *)(long)cpu);
 	}
-	unlock_cpu_hotplug();
 	return 0;
 }
 static void
@@ -368,12 +366,10 @@ __exit cpufreq_stats_exit(void)
 	cpufreq_unregister_notifier(&notifier_trans_block,
 			CPUFREQ_TRANSITION_NOTIFIER);
 	unregister_hotcpu_notifier(&cpufreq_stat_cpu_notifier);
-	lock_cpu_hotplug();
 	for_each_online_cpu(cpu) {
 		cpufreq_stat_cpu_callback(&cpufreq_stat_cpu_notifier, CPU_DEAD,
 			(void *)(long)cpu);
 	}
-	unlock_cpu_hotplug();
 }
 
 MODULE_AUTHOR ("Zou Nan hai <[email protected]>");
drivers/cpufreq/cpufreq.c renamed from drivers/cpufreq/cpufreq.c~ +0 -7
@@ -423,8 +423,6 @@ static ssize_t store_scaling_governor (s
 	if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
 		return -EINVAL;
 
-	lock_cpu_hotplug();
-
 	/* Do not use cpufreq_set_policy here or the user_policy.max
 	   will be wrongly overridden */
 	mutex_lock(&policy->lock);
@@ -434,8 +432,6 @@ static ssize_t store_scaling_governor (s
 	policy->user_policy.governor = policy->governor;
 	mutex_unlock(&policy->lock);
 
-	unlock_cpu_hotplug();
-
 	return ret ? ret : count;
 }
 
@@ -1203,14 +1199,11 @@ int __cpufreq_driver_target(struct cpufr
 {
 	int retval = -EINVAL;
 
-	lock_cpu_hotplug();
 	dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
 		target_freq, relation);
 	if (cpu_online(policy->cpu) && cpufreq_driver->target)
 		retval = cpufreq_driver->target(policy, target_freq, relation);
 
-	unlock_cpu_hotplug();
-
 	return retval;
 }
 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
drivers/cpufreq/cpufreq_conservative.c renamed from drivers/cpufreq/cpufreq_conservative.c~ +0 -4
@@ -423,14 +423,12 @@ static void dbs_check_cpu(int cpu)
 static void do_dbs_timer(void *data)
 { 
 	int i;
-	lock_cpu_hotplug();
 	mutex_lock(&dbs_mutex);
 	for_each_online_cpu(i)
 		dbs_check_cpu(i);
 	schedule_delayed_work(&dbs_work, 
 			usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
 	mutex_unlock(&dbs_mutex);
-	unlock_cpu_hotplug();
 } 
 
 static inline void dbs_timer_init(void)
@@ -525,7 +523,6 @@ static int cpufreq_governor_dbs(struct c
 		break;
 
 	case CPUFREQ_GOV_LIMITS:
-		lock_cpu_hotplug();
 		mutex_lock(&dbs_mutex);
 		if (policy->max < this_dbs_info->cur_policy->cur)
 			__cpufreq_driver_target(
@@ -536,7 +533,6 @@ static int cpufreq_governor_dbs(struct c
 					this_dbs_info->cur_policy,
 				       	policy->min, CPUFREQ_RELATION_L);
 		mutex_unlock(&dbs_mutex);
-		unlock_cpu_hotplug();
 		break;
 	}
 	return 0;
Signature · 1 line
http://www.codemonkey.org.uk
#1 lore raw
Quoted 5 lines · Dave Jones

This stuff makes my head hurt. Someone who is motivated enough to fix up hotplug-cpu can fix this up later. In the meantime, this patch should cure the lockdep warnings that seem to trigger very easily.

It doesn't seem to fix all problems. On CPU unplug, I still get deadlocks through some workqueue:

 [<c03af64a>] __mutex_lock_slowpath+0x4d/0x7b
 [<c03af687>] .text.lock.mutex+0xf/0x14
 [<c0137591>] __lock_cpu_hotplug+0x36/0x56
 [<c01375ca>] lock_cpu_hotplug+0xa/0xc
 [<c012f7c2>] flush_workqueue+0x2d/0x61
 [<c012f83b>] flush_scheduled_work+0xd/0xf
 ...

and the nasty part is that this can apparently hit _any_ process that wants to flush workqueues (in one particular case, it was through tty_release() -> release_dev() in drivers/char/tty.c).

The whole CPU hotplug locking seems to be broken.

Linus

#2 lore raw
Quoted 20 lines · On Sat, 22 Jul 2006 17:10:36 -0700 (PDT) Linus Torvalds <[email protected]>…
Quoted 5 lines · Dave Jones

This stuff makes my head hurt. Someone who is motivated enough to fix up hotplug-cpu can fix this up later. In the meantime, this patch should cure the lockdep warnings that seem to trigger very easily.

It doesn't seem to fix all problems. On CPU unplug, I still get deadlocks through some workqueue:

 [<c03af64a>] __mutex_lock_slowpath+0x4d/0x7b
 [<c03af687>] .text.lock.mutex+0xf/0x14
 [<c0137591>] __lock_cpu_hotplug+0x36/0x56
 [<c01375ca>] lock_cpu_hotplug+0xa/0xc
 [<c012f7c2>] flush_workqueue+0x2d/0x61
 [<c012f83b>] flush_scheduled_work+0xd/0xf
 ...
Quoted 3 lines

and the nasty part is that this can apparently hit _any_ process that wants to flush workqueues (in one particular case, it was through tty_release() -> release_dev() in drivers/char/tty.c).

If one was sufficiently morbidly curious, one would ask for the rest of the trace, to identify the deadlock. But it's just not interesting.

Let's just delete the whole thing.

Quoted 1 line

The whole CPU hotplug locking seems to be broken.

It was just wrong in conception. We should not and probably cannot fix it. Let's just delete it all, then implement version 2.

Which is: subsystems are locally responsible for locking their per-cpu resources. No global lock. Subsystems have two options:

a) If the resource can be accessed while running atomically, lock it with preempt_disable(). Because preempt_disable() holds off hotunplug.

b) If the kernel wants to sleep while requiring that the per-cpu resource remain stable, lock it down with mutex_lock() in the normal operating code and also lock it down with mutex_lock() in the subsystem's cpu hotplug notifier callback.

The precise handling of b) needs some thought and will depend upon how the subsystem tells itself about the availability of each CPU's data. A simple implementation would be:

mainline_code()

{
	mutex_lock(lock);
	fiddle_with(data[some_cpu_id]);
	mutex_unlock(lock);
}

static int foo_cpu_callback(struct notifier_block *nfb, unsigned long action,
void *hcpu)

{
	unsigned int cpu = (unsigned long)hcpu;

	switch (action) {
	case CPU_DOWN_PREPARE:
		mutex_lock(lock);
		break;
	case CPU_DOWN_FAILED:
		mutex_unlock(lock);
		break;
	case CPU_DOWN_DEAD:
		mutex_unlock(lock);
		break;
	}
	return NOTIFY_OK;
}

Leaving the lock held in this manner isn't nice IMO, but it should work OK.

Note that there is what appears to be a bug in cpu_down(). This:

	if (cpu_online(cpu))
		goto out_thread;

should do a CPU_DOWN_FAILED callout. Or, better, it should go BUG(), because this shouldn't happen.

#3 lore raw
Quoted 3 lines · Andrew Morton

It was just wrong in conception. We should not and probably cannot fix it. Let's just delete it all, then implement version 2.

Well, I just got Ashok's trial patches which turns the thing into a rwsem as I outlined earlier.

I'll try them out. If they don't work, we should just delete the lock and go totally back to square 1.

Linus

#4 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Andrew Morton

It was just wrong in conception. We should not and probably cannot fix it. Let's just delete it all, then implement version 2.

Well, I just got Ashok's trial patches which turns the thing into a rwsem as I outlined earlier.

with rwsems being 100% fair... how is that going to make a difference? Other than just making the deadlock harder to trigger because the writer needs to come in just at the right time...

#5 lore raw
Quoted 4 lines · Arjan van de Ven

with rwsems being 100% fair... how is that going to make a difference? Other than just making the deadlock harder to trigger because the writer needs to come in just at the right time...

Fair enough, I forgot about that particular braindamage. It would be _soo_ nice to have a proper rwsem like we used to.

Ok, here's a final try.

Its actually simpler than either of the previous ones, and it just does two different locks: a "cpu_add_remove_lock" which is entirely internal to kernel/cpu.c, and is held over the entirety of a cpu_add()/cpu_down() sequence, so that we will only ever add one CPU at a time.

The other lock is the "cpu_bitmask_lock", and the only thing that protects is the actual changing of the present bitmask. It's always nested inside of the "cpu_add_remove_lock" (if that is taken at all, of course).

The latter one is the one that "lock_cpu_hotplug()" actually takes, so anybody who does "lock_cpu_hotplug()" will only protect against the bitmap itself changing, not against the bigger issue of "cpu hotplug events are happening".

Does this work? Hey, it works for me once. It's pretty simple, and had better not have any recursion issues. It guarantees that actual cpu hotplug events are single-threaded wrt each other, and it does not allow any recursive taking of "lock_cpu_hotplug()", but since cpu_add() and cpu_down() no longer hold that particular lock when they to the call-outs for the cpu events, it should hopefully not be needed any more either.

Not very well tested, but it did suspend and resume twice for me.

Does anybody see any problems with this?

Linus

---
 include/linux/cpu.h |    6 ------
 kernel/cpu.c        |   54 ++++++++++++---------------------------------------
 2 files changed, 13 insertions(+), 47 deletions(-)
include/linux/cpu.h +0 -6
index 44a11f1..8fb344a 100644
@@ -48,7 +48,6 @@ static inline void unregister_cpu_notifi
 {
 }
 #endif
-extern int current_in_cpu_hotplug(void);
 
 int cpu_up(unsigned int cpu);
 
@@ -61,10 +60,6 @@ static inline int register_cpu_notifier(
 static inline void unregister_cpu_notifier(struct notifier_block *nb)
 {
 }
-static inline int current_in_cpu_hotplug(void)
-{
-	return 0;
-}
 
 #endif /* CONFIG_SMP */
 extern struct sysdev_class cpu_sysdev_class;
@@ -73,7 +68,6 @@ #ifdef CONFIG_HOTPLUG_CPU
 /* Stop CPUs going up and down. */
 extern void lock_cpu_hotplug(void);
 extern void unlock_cpu_hotplug(void);
-extern int lock_cpu_hotplug_interruptible(void);
 #define hotcpu_notifier(fn, pri) {				\
 	static struct notifier_block fn##_nb =			\
 		{ .notifier_call = fn, .priority = pri };	\
kernel/cpu.c +13 -41

113 lines not on this page · raw message

#6 lore raw
Quoted 3 lines · Linus Torvalds

Does this work? Hey, it works for me once. It's pretty simple, and had better not have any recursion issues.

GAAH!!

What kind of _crap_ is this cpufreq thing?

Lookie here:

	S06cpuspeed   D DD94A324  2180 10241  10215                     (NOTLB)
	Call Trace:
	 [<c03c411d>] __mutex_lock_slowpath+0x4d/0x7b
	 [<c03c415a>] .text.lock.mutex+0xf/0x14
	 [<c0137651>] lock_cpu_hotplug+0xd/0xf
	 [<c012f9df>] __create_workqueue+0x52/0x11f
	 [<df0cd336>] cpufreq_governor_dbs+0x9e/0x2c5 [cpufreq_ondemand]
	 [<c0305d2a>] __cpufreq_governor+0x57/0xd8
	 [<c0305ee8>] __cpufreq_set_policy+0x13d/0x1a9
	 [<c03060e4>] store_scaling_governor+0x12d/0x155
	 [<c03057a5>] store+0x34/0x45
	 [<c0199a6c>] sysfs_write_file+0x99/0xbf
	 [<c0164ac3>] vfs_write+0xab/0x157
	 [<c01650fc>] sys_write+0x3b/0x60
	 [<c0102d41>] sysenter_past_esp+0x56/0x79

where it takes the cpu_hotplug lock in "store_scaling_governor()", and then calls __cpufreq_set_policy(), and then that ondemand thing WILL TAKE IT AGAIN!

What a piece of crap. Why, why, why?

[ Linus bangs his head against the wall until tears of blood course down
his face ]

I will here-by re-introduce the recursion thing for lock_cpu_hotplug, but I will make it say some very rude things about idiots who create code like this.

cpufreq (or at least ondemand) must DIE! And the people who wrote that crap should have red-hot pokers jammed into some very uncomfortable places.

Linus

#7 lore raw
Quoted 3 lines · Linus Torvalds

On Sun, 23 Jul 2006, Linus Torvalds wrote:
[ Linus bangs his head against the wall until tears of blood course down
his face ]

I know how you feel.

Quoted 3 lines

cpufreq (or at least ondemand) must DIE! And the people who wrote that crap should have red-hot pokers jammed into some very uncomfortable places.

You know what else must die? powernowd... which does exactly what the conservative governor does, but takes about a meg of memory to do it, and it doesn't even provide stuff like changing behavior based on ac/battery state or lm_sensors feedback.

Signature · 5 lines
Patrick McFarland || www.AdTerrasPerAspera.com
"Computer games don't affect kids; I mean if Pac-Man affected us as kids,
we'd all be running around in darkened rooms, munching magic pills and
listening to repetitive electronic music." -- Kristian Wilson, Nintendo,
Inc, 1989
#8 lore raw
In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds

Does this work? Hey, it works for me once. It's pretty simple, and had better not have any recursion issues.

GAAH!!

What kind of _crap_ is this cpufreq thing?

this is why lockdep got highly upset with it, and why Davej proposed to remove the locking entirely for now until it can be put back in a correct way....

#9 lore raw
In reply to Linus Torvalds
Quoted 9 lines · On Sat, 22 Jul 2006 18:15:32 -0700 (PDT) Linus Torvalds <[email protected]>…
Quoted 3 lines · Andrew Morton

It was just wrong in conception. We should not and probably cannot fix it. Let's just delete it all, then implement version 2.

Well, I just got Ashok's trial patches which turns the thing into a rwsem as I outlined earlier.

Mark my words ;)

Quoted 2 lines

I'll try them out. If they don't work, we should just delete the lock and go totally back to square 1.

rwsem conversion has the potential to merely hide the problem. Ingo, does lockdep detect recursive down_read()?

#10 lore raw

Hi,

Quoted 10 lines
Quoted 2 lines

Well, I just got Ashok's trial patches which turns the thing into a rwsem as I outlined earlier.

Mark my words ;)

Quoted 2 lines

I'll try them out. If they don't work, we should just delete the lock and go totally back to square 1.

rwsem conversion has the potential to merely hide the problem. Ingo, does lockdep detect recursive down_read()?

lockdep detects and warns about those.

I think we're about equally skeptical about this; I'm extremely hesitant about any place in the kernel that uses rwsems for anything other than a performance tweak. I've ended up with a mental model of rwsems that basically comes down to "you need to be able to replace it with a mutex without breaking correctness". Now of course that model is somewhat of an oversimplification, but not by that much...

Greetings,
Arjan van de Ven

#11 lore raw
In reply to Andrew Morton
Quoted 5 lines · Andrew Morton
Quoted 2 lines

I'll try them out. If they don't work, we should just delete the lock and go totally back to square 1.

rwsem conversion has the potential to merely hide the problem. Ingo, does lockdep detect recursive down_read()?

yeah, it does - there are a couple of testcases for it too, in the testsuite.

Ingo