Skip to contentSkip to conversation

Don't take semaphore in cpufreq_quick_get()

PATCH In review Andi Kleen 3 participants 3 messages Oct 10, 2007 CPU frequency scaling framework

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 Don't take semaphore in cpufreq_quick_get()

I don't see any reason to take an expensive lock in cpufreq_quick_get() Reading policy->cur is a single atomic operation and after the lock is dropped again the state could change any time anyways.

So don't take the lock in the first place.

This also makes this function interrupt safe which is useful for some code of mine.

drivers/cpufreq/cpufreq.c +0 -5
Index: linux/drivers/cpufreq/cpufreq.c
===================================================================
@@ -1109,12 +1109,7 @@ unsigned int cpufreq_quick_get(unsigned 
 	unsigned int ret_freq = 0;
 
 	if (policy) {
-		if (unlikely(lock_policy_rwsem_read(cpu)))
-			return ret_freq;
-
 		ret_freq = policy->cur;
-
-		unlock_policy_rwsem_read(cpu);
 		cpufreq_cpu_put(policy);
 	}
#1 lore raw
Quoted 17 lines · Andi Kleen

I don't see any reason to take an expensive lock in cpufreq_quick_get() Reading policy->cur is a single atomic operation and after the lock is dropped again the state could change any time anyways.

So don't take the lock in the first place.

This also makes this function interrupt safe which is useful for some code of mine.

Index: linux/drivers/cpufreq/cpufreq.c
===================================================================

drivers/cpufreq/cpufreq.c +0 -0
@@ -1109,12 +1109,7 @@ unsigned int cpufreq_quick_get(unsigned 
 	unsigned int ret_freq = 0;

drivers/cpufreq/cpufreq.c unsigned int cpufreq_quick_get(unsigned · line 1114

Quoted 10 lines
drivers/cpufreq/cpufreq.c +0 -5
 
 	if (policy) {
-		if (unlikely(lock_policy_rwsem_read(cpu)))
-			return ret_freq;
-
 		ret_freq = policy->cur;
-
-		unlock_policy_rwsem_read(cpu);
 		cpufreq_cpu_put(policy);
 	}

I can't dispute this. It does look strange.
Even the initial implementation before the cpufreq-vs-hotplug-cpu refactoring happened looked as odd for the same reasons you outline.

       if (policy) {
               down(&policy->lock);
               ret = policy->cur;
               up(&policy->lock);
               cpufreq_cpu_put(policy);
       }

This isn't really protecting anything, and we grab policy->cur elsewhere without locking, so I'm inclined to merge this patch.

Venki, any comments ?

Dave

Signature · 1 line
http://www.codemonkey.org.uk
#2 lore raw
Quoted 51 lines

-----Original Message-----
From: Dave Jones [mailto:[email protected]]
Sent: Tuesday, October 09, 2007 5:43 PM
To: Andi Kleen
Cc: [email protected]; Pallipadi, Venkatesh
Subject: Re: [PATCH] Don't take semaphore in cpufreq_quick_get()

Quoted 1 line · Andi Kleen

I don't see any reason to take an expensive lock in

cpufreq_quick_get()

Quoted 16 lines

Reading policy->cur is a single atomic operation and after the lock is dropped again the state could change any time anyways.

So don't take the lock in the first place.

This also makes this function interrupt safe which is useful for some code of mine.

Index: linux/drivers/cpufreq/cpufreq.c
===================================================================

drivers/cpufreq/cpufreq.c +0 -0
@@ -1109,12 +1109,7 @@ unsigned int cpufreq_quick_get(unsigned 
 	unsigned int ret_freq = 0;

drivers/cpufreq/cpufreq.c unsigned int cpufreq_quick_get(unsigned · line 1114

Quoted 10 lines
drivers/cpufreq/cpufreq.c +0 -5
 
 	if (policy) {
-		if (unlikely(lock_policy_rwsem_read(cpu)))
-			return ret_freq;
-
 		ret_freq = policy->cur;
-
-		unlock_policy_rwsem_read(cpu);
 		cpufreq_cpu_put(policy);
 	}

I can't dispute this. It does look strange.
Even the initial implementation before the cpufreq-vs-hotplug-cpu refactoring happened looked as odd for the same reasons you outline.

      if (policy) {
              down(&policy->lock);
              ret = policy->cur;
              up(&policy->lock);
              cpufreq_cpu_put(policy);
      }
Quoted 3 lines

This isn't really protecting anything, and we grab policy->cur elsewhere without locking, so I'm inclined to merge this patch.

Agreed. We can take locks out of this.

Thanks,
Venki