Skip to contentSkip to conversation

cpufreq: conservative: fix requested_freq reduction issue

PATCH Applied Xiaoguang Chen 5 participants 12 messages Nov 7, 2013 – Nov 11, 2013 1 message not in the archive CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: conservative: fix requested_freq reduction issue

When decreasing frequency, requested_freq may be less than freq_target, So requested_freq minus freq_target may be negative, But reqested_freq's unit is unsigned int, then the negative result will be one larger interger which may be even higher than requested_freq.

This patch is to fix such issue. when result becomes negative, set requested_freq as the min value of policy.

---
 drivers/cpufreq/cpufreq_conservative.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/cpufreq/cpufreq_conservative.c +6 -1
index f62d822..218460f 100644
@@ -80,13 +80,18 @@ static void cs_check_cpu(int cpu, unsigned int load)
 
 	/* Check for frequency decrease */
 	if (load < cs_tuners->down_threshold) {
+		unsigned int freq_target;
 		/*
 		 * if we cannot reduce the frequency anymore, break out early
 		 */
 		if (policy->cur == policy->min)
 			return;
 
-		dbs_info->requested_freq -= get_freq_target(cs_tuners, policy);
+		freq_target = get_freq_target(cs_tuners, policy);
+		if (dbs_info->requested_freq > freq_target)
+			dbs_info->requested_freq -= freq_target;
+		else
+			dbs_info->requested_freq = policy->min;
 
 		__cpufreq_driver_target(policy, dbs_info->requested_freq,
 				CPUFREQ_RELATION_L);
Signature · 1 line
1.8.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Xiaoguang Chen

When decreasing frequency, requested_freq may be less than freq_target, So requested_freq minus freq_target may be negative, But reqested_freq's unit is unsigned int, then the negative result will be one larger interger which may be even higher than requested_freq.

Quoted 7 lines

This patch is to fix such issue. when result becomes negative, set requested_freq as the min value of policy.

---
 drivers/cpufreq/cpufreq_conservative.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Good Catch.

We need another patch for fixing the other part of code where we increase freq..
We need to replace:

if (dbs_info->requested_freq == policy->max)

    return;

with

if (dbs_info->requested_freq >= policy->max)

    return;

So, that we don't run unnecessary code :)

#2 lore raw
Quoted 10 lines · Viresh Kumar
Quoted 9 lines · Xiaoguang Chen

When decreasing frequency, requested_freq may be less than freq_target, So requested_freq minus freq_target may be negative, But reqested_freq's unit is unsigned int, then the negative result will be one larger interger which may be even higher than requested_freq.

This patch is to fix such issue. when result becomes negative, set requested_freq as the min value of policy.

Quoted 8 lines
Quoted 4 lines
---
 drivers/cpufreq/cpufreq_conservative.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Good Catch.

Applied, thanks!

Quoted 5 lines

We need another patch for fixing the other part of code where we increase freq..
We need to replace:

if (dbs_info->requested_freq == policy->max)

Quoted 8 lines
    return;

with

if (dbs_info->requested_freq >= policy->max)

    return;

So, that we don't run unnecessary code :)

Care to prepare a patch?

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
maintainer
#3 lore raw
Quoted 9 lines · Rafael J. Wysocki
Quoted 7 lines · Viresh Kumar

We need another patch for fixing the other part of code where we increase freq..
We need to replace:

if (dbs_info->requested_freq == policy->max)

    return;
Quoted 8 lines
Quoted 6 lines

with

if (dbs_info->requested_freq >= policy->max)

    return;

So, that we don't run unnecessary code :)

Care to prepare a patch?

I wanted to give a chance to Xiaoguang to generate a patch for us :) Lets wait for his reply, otherwise I will do it.

maintainer
#5 lore raw
In reply to a message not in the archive
Quoted 2 lines · Stratos Karafotis

I think the existing code already checks if the requested_freq is greater than policy->max in __cpufreq_driver_target.

Yes it does. But the problem is:
- cs_check_cpu() sets requested_freq above policy->max
- We execute following code because (requested_freq != policy->max)

    dbs_info->requested_freq += get_freq_target(cs_tuners, policy);
    __cpufreq_driver_target(policy, dbs_info->requested_freq,

CPUFREQ_RELATION_H);
- In __cpufreq_driver_target(), we don't do anything and return early..
- Above will keep on repeating all the time..

If we change the code as I have suggested it to be:
- After first loop where requested_freq went over policy->max, we will return early from cs_check_cpu(), but we have already set freq to max..

Quoted 1 line

If we put this check earlier, cpufreq will never reach policy->max.

Can you please explain why do you see that happening?

#6 lore raw

Hi, Viresh, Sorry for the late reply.
I'll prepare the patch.
BTW, do you think we should set requeste_freq to policy->max when such condition happens?

Thanks
Xiaoguang

2013/11/8 Viresh Kumar <[email protected]>:

Quoted 21 lines · Stratos Karafotis
Quoted 2 lines

I think the existing code already checks if the requested_freq is greater than policy->max in __cpufreq_driver_target.

Yes it does. But the problem is:
- cs_check_cpu() sets requested_freq above policy->max
- We execute following code because (requested_freq != policy->max)

    dbs_info->requested_freq += get_freq_target(cs_tuners, policy);
    __cpufreq_driver_target(policy, dbs_info->requested_freq,

CPUFREQ_RELATION_H);
- In __cpufreq_driver_target(), we don't do anything and return early..
- Above will keep on repeating all the time..

If we change the code as I have suggested it to be:
- After first loop where requested_freq went over policy->max, we will return early from cs_check_cpu(), but we have already set freq to max..

Quoted 1 line

If we put this check earlier, cpufreq will never reach policy->max.

Can you please explain why do you see that happening?

maintainer
#7 lore raw
Quoted 1 line · Xiaoguang Chen

Hi, Viresh, Sorry for the late reply.

That's fine :)

Quoted 1 line

I'll prepare the patch.

Thanks.

Quoted 2 lines

BTW, do you think we should set requeste_freq to policy->max when such condition happens?

I thought about that earlier, but then thought this would be a cleaner solution. And guess what, I was wrong.. There is one scenario for which we need to set requested_freq correctly..

Suppose we have requested_freq currently greater than policy->max and load decreases. We will start decreasing requested_freq but it will take one iteration
to make it equal to max, which is not we want.. So we actually need to set it to max when it gets over it.

So, don't do the change I asked for, i.e. replacing == with >=. But update existing
code:

dbs_info->requested_freq += get_freq_target(cs_tuners, policy);

this way:

dbs_info->requested_freq += get_freq_target(cs_tuners, policy); if (dbs_info->requested_freq > policy->max)

    dbs_info->requested_freq = policy->max;
#8 lore raw

2013/11/8 Viresh Kumar <[email protected]>:

Quoted 26 lines · Xiaoguang Chen
Quoted 1 line

Hi, Viresh, Sorry for the late reply.

That's fine :)

Quoted 1 line

I'll prepare the patch.

Thanks.

Quoted 2 lines

BTW, do you think we should set requeste_freq to policy->max when such condition happens?

I thought about that earlier, but then thought this would be a cleaner solution. And guess what, I was wrong.. There is one scenario for which we need to set requested_freq correctly..

Suppose we have requested_freq currently greater than policy->max and load decreases. We will start decreasing requested_freq but it will take one iteration
to make it equal to max, which is not we want.. So we actually need to set it to max when it gets over it.

So, don't do the change I asked for, i.e. replacing == with >=. But update existing
code:

Quoted 7 lines

dbs_info->requested_freq += get_freq_target(cs_tuners, policy);

this way:

dbs_info->requested_freq += get_freq_target(cs_tuners, policy); if (dbs_info->requested_freq > policy->max)

    dbs_info->requested_freq = policy->max;

OK, patch will be sent out later

Xiaoguang

#9 lore raw
In reply to Viresh Kumar
Quoted 14 lines · Viresh Kumar
Quoted 2 lines · Stratos Karafotis

I think the existing code already checks if the requested_freq is greater than policy->max in __cpufreq_driver_target.

Yes it does. But the problem is:
- cs_check_cpu() sets requested_freq above policy->max
- We execute following code because (requested_freq != policy->max)

    dbs_info->requested_freq += get_freq_target(cs_tuners, policy);
    __cpufreq_driver_target(policy, dbs_info->requested_freq,

CPUFREQ_RELATION_H);
- In __cpufreq_driver_target(), we don't do anything and return early..
- Above will keep on repeating all the time..

Quoted 7 lines

If we change the code as I have suggested it to be:
- After first loop where requested_freq went over policy->max, we will return early from cs_check_cpu(), but we have already set freq to max..

Quoted 1 line

If we put this check earlier, cpufreq will never reach policy->max.

Can you please explain why do you see that happening?

Please let me rephrase my previous post. In some circumstances (depending on freq_step and freq_table values) CPU frequency will never reach to policy->max.

For example suppose that (for simplicity values in MHz):
policy->max = 1000
policy->cur = 800
requested_freq = 800
freq_target = 300

In 'first' iteration, if we return early with this code (because requested_freq will be
1100):
if (dbs_info->requested_freq >= policy->max)

     return;

CPU freq will never go over 800MHz.

I think the current code works correctly.
- The requested freq will go to 1100 in first iteration.
- __cpufreq_driver_target will change CPU freq to 1000
- dbs_cpufreq_notifier will adjust the requested_freq to 1000

In next iteration the code:
if (dbs_info->requested_freq == policy->max)

        return;

will keep the freq to max and break out early.

So, I think there is no need for an extra check because of dbs_cpufreq_notifier code.

Thanks,
Stratos Karafotis

maintainer
#10 lore raw
Quoted 10 lines · Stratos Karafotis

Please let me rephrase my previous post. In some circumstances (depending on freq_step and freq_table values) CPU frequency will never reach to policy->max.

For example suppose that (for simplicity values in MHz):
policy->max = 1000
policy->cur = 800
requested_freq = 800
freq_target = 300

Quoted 5 lines

In 'first' iteration, if we return early with this code (because requested_freq will be
1100):
if (dbs_info->requested_freq >= policy->max)

     return;

That's not correct. At this point requested_freq would have been 800 only, and would have increased after this instruction to 1100. So, in the first transition we will go to max freq, but not from the second.

Though this piece of code is more simplified by the new solution I gave.

Quoted 9 lines

CPU freq will never go over 800MHz.

I think the current code works correctly.
- The requested freq will go to 1100 in first iteration.
- __cpufreq_driver_target will change CPU freq to 1000
- dbs_cpufreq_notifier will adjust the requested_freq to 1000

So, I think there is no need for an extra check because of dbs_cpufreq_notifier code.

Now with the new code in place we are correcting requested_freq in cs_check_cpu(), then why do we need dbs_cpufreq_notifier()?

What do you think?

#11 lore raw
Quoted 17 lines · Viresh Kumar
Quoted 10 lines · Stratos Karafotis

Please let me rephrase my previous post. In some circumstances (depending on freq_step and freq_table values) CPU frequency will never reach to policy->max.

For example suppose that (for simplicity values in MHz):
policy->max = 1000
policy->cur = 800
requested_freq = 800
freq_target = 300

Quoted 5 lines

In 'first' iteration, if we return early with this code (because requested_freq will be
1100):
if (dbs_info->requested_freq >= policy->max)

     return;
Quoted 8 lines

That's not correct. At this point requested_freq would have been 800 only, and would have increased after this instruction to 1100. So, in the first transition we will go to max freq, but not from the second.

Though this piece of code is more simplified by the new solution I gave.

Yes, you are right.

Quoted 7 lines
Quoted 6 lines

CPU freq will never go over 800MHz.

I think the current code works correctly.
- The requested freq will go to 1100 in first iteration.
- __cpufreq_driver_target will change CPU freq to 1000
- dbs_cpufreq_notifier will adjust the requested_freq to 1000

Quoted 7 lines
Quoted 2 lines

So, I think there is no need for an extra check because of dbs_cpufreq_notifier code.

Now with the new code in place we are correcting requested_freq in cs_check_cpu(), then why do we need dbs_cpufreq_notifier()?

What do you think?

I removed the check you proposed in this commit 934dac1ea072 to avoid the duplicate check in cs_check_cpu and in dbs_cpufreq_notifier.

I agree that we don't need dbs_cpufreq_notifier if we transfer checks in cs_check_cpu. But I'm not 100% sure if the notifier also covers other cases and if it can be safely removed.

Stratos Karafotis

maintainer
#12 lore raw
Quoted 6 lines · Stratos Karafotis

I removed the check you proposed in this commit 934dac1ea072 to avoid the duplicate check in cs_check_cpu and in dbs_cpufreq_notifier.

I agree that we don't need dbs_cpufreq_notifier if we transfer checks in cs_check_cpu. But I'm not 100% sure if the notifier also covers other cases and if it can be safely removed.

It is there to take care of out-of-sync issues, and was introduced by this commit, so probably it will stay as is:

commit a8d7c3bc2396aff14f9e920677072cb55b016040

Author: Elias Oltmanns <[email protected]>
Date:   Mon Oct 22 09:50:13 2007 +0200

[CPUFREQ] Make cpufreq_conservative handle out-of-sync events properly

Make cpufreq_conservative handle out-of-sync events properly

Currently, the cpufreq_conservative governor doesn't get notified when the actual frequency the cpu is running at differs from what cpufreq thought it was. As a result the cpu may stay at the maximum frequency after a s2ram / resume cycle even though the system is idle.

---
 drivers/cpufreq/cpufreq_conservative.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)