2018-05-24 07:01:28

by Tao Wang

[permalink] [raw]
Subject: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

consider such situation, current user_policy.min is 1000000,
current user_policy.max is 1200000, in cpufreq_set_policy,
other driver may update policy.min to 1200000, policy.max to
1300000. After that, If we input "echo 1300000 > scaling_min_freq",
then user_policy.min will be 1300000, and user_policy.max is
still 1200000, because the input value is checked with policy.max
not user_policy.max. if we get all related cpus offline and
online again, it will cause cpufreq_init_policy fail because
user_policy.min is higher than user_policy.max.

The solution is when user space tries to write scaling_(max|min)_freq,
the min/max of new_policy should be reinitialized with min/max
of user_policy, like what cpufreq_update_policy does.

Signed-off-by: Kevin Wangtao <[email protected]>
---
drivers/cpufreq/cpufreq.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index b79c532..8b33e08 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -697,6 +697,8 @@ static ssize_t store_##file_name \
struct cpufreq_policy new_policy; \
\
memcpy(&new_policy, policy, sizeof(*policy)); \
+ new_policy->min = policy->user_policy.min; \
+ new_policy->max = policy->user_policy.max; \
\
ret = sscanf(buf, "%u", &new_policy.object); \
if (ret != 1) \
--
2.8.1



2018-05-24 08:00:45

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
<[email protected]> wrote:
> consider such situation, current user_policy.min is 1000000,
> current user_policy.max is 1200000, in cpufreq_set_policy,
> other driver may update policy.min to 1200000, policy.max to
> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
> then user_policy.min will be 1300000, and user_policy.max is
> still 1200000, because the input value is checked with policy.max
> not user_policy.max. if we get all related cpus offline and
> online again, it will cause cpufreq_init_policy fail because
> user_policy.min is higher than user_policy.max.

How do you reproduce this, exactly?

> The solution is when user space tries to write scaling_(max|min)_freq,
> the min/max of new_policy should be reinitialized with min/max
> of user_policy, like what cpufreq_update_policy does.
>
> Signed-off-by: Kevin Wangtao <[email protected]>
> ---
> drivers/cpufreq/cpufreq.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index b79c532..8b33e08 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -697,6 +697,8 @@ static ssize_t store_##file_name \
> struct cpufreq_policy new_policy; \
> \
> memcpy(&new_policy, policy, sizeof(*policy)); \
> + new_policy->min = policy->user_policy.min; \
> + new_policy->max = policy->user_policy.max; \

It looks like you haven't even tried to build this, have you?

> \
> ret = sscanf(buf, "%u", &new_policy.object); \
> if (ret != 1) \
> --
> 2.8.1
>

2018-05-25 02:57:29

by Tao Wang

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq



在 2018/5/24 15:45, Rafael J. Wysocki 写道:
> On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
> <[email protected]> wrote:
>> consider such situation, current user_policy.min is 1000000,
>> current user_policy.max is 1200000, in cpufreq_set_policy,
>> other driver may update policy.min to 1200000, policy.max to
>> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
>> then user_policy.min will be 1300000, and user_policy.max is
>> still 1200000, because the input value is checked with policy.max
>> not user_policy.max. if we get all related cpus offline and
>> online again, it will cause cpufreq_init_policy fail because
>> user_policy.min is higher than user_policy.max.
>
> How do you reproduce this, exactly?
I write a driver register CPUFREQ_POLICY_NOTIFIER, and when event is CPUFREQ_ADJUST,
it will modify policy's min/max according to some conditions, I test it with writing
scaling_(max|min)_freq to traverse all frequencies repeatly, and also repeat hotplug
as background.
>
>> The solution is when user space tries to write scaling_(max|min)_freq,
>> the min/max of new_policy should be reinitialized with min/max
>> of user_policy, like what cpufreq_update_policy does.
>>
>> Signed-off-by: Kevin Wangtao <[email protected]>
>> ---
>> drivers/cpufreq/cpufreq.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index b79c532..8b33e08 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -697,6 +697,8 @@ static ssize_t store_##file_name \
>> struct cpufreq_policy new_policy; \
>> \
>> memcpy(&new_policy, policy, sizeof(*policy)); \
>> + new_policy->min = policy->user_policy.min; \
new_policy.min = policy->user_policy.min;
>> + new_policy->max = policy->user_policy.max; \
new_policy.max = policy->user_policy.max
>
> It looks like you haven't even tried to build this, have you?
sorry for that, I test it on another branch and write this patch manually without build
>
>> \
>> ret = sscanf(buf, "%u", &new_policy.object); \
>> if (ret != 1) \
>> --
>> 2.8.1
>>
>
> .
>


2018-05-26 06:51:47

by Tao Wang

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq



在 2018/5/24 15:45, Rafael J. Wysocki 写道:
> On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
> <[email protected]> wrote:
>> consider such situation, current user_policy.min is 1000000,
>> current user_policy.max is 1200000, in cpufreq_set_policy,
>> other driver may update policy.min to 1200000, policy.max to
>> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
>> then user_policy.min will be 1300000, and user_policy.max is
>> still 1200000, because the input value is checked with policy.max
>> not user_policy.max. if we get all related cpus offline and
>> online again, it will cause cpufreq_init_policy fail because
>> user_policy.min is higher than user_policy.max.
>
> How do you reproduce this, exactly?
I can also reproduce this issue with upstream code, write max frequency to scaling_max_freq
and scaling_min_freq, run benchmark to let cpu cooling take effect to clip freq, then write
the cliped freq to scaling_max_freq, thus user_policy.min is still max frequency but user_policy.max
is cliped freq which is lower than max frequency.
>
>> The solution is when user space tries to write scaling_(max|min)_freq,
>> the min/max of new_policy should be reinitialized with min/max
>> of user_policy, like what cpufreq_update_policy does.
>>
>> Signed-off-by: Kevin Wangtao <[email protected]>
>> ---
>> drivers/cpufreq/cpufreq.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index b79c532..8b33e08 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -697,6 +697,8 @@ static ssize_t store_##file_name \
>> struct cpufreq_policy new_policy; \
>> \
>> memcpy(&new_policy, policy, sizeof(*policy)); \
>> + new_policy->min = policy->user_policy.min; \
>> + new_policy->max = policy->user_policy.max; \
>
> It looks like you haven't even tried to build this, have you?
>
>> \
>> ret = sscanf(buf, "%u", &new_policy.object); \
>> if (ret != 1) \
>> --
>> 2.8.1
>>
>
> .
>


2018-05-26 07:31:46

by Tao Wang

[permalink] [raw]
Subject: [PATCH V2] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

consider such situation, current user_policy.min is 1000000,
current user_policy.max is 1200000, in cpufreq_set_policy,
other driver may update policy.min to 1200000, policy.max to
1300000. After that, If we input "echo 1300000 > scaling_min_freq",
then user_policy.min will be 1300000, and user_policy.max is
still 1200000, because the input value is checked with policy.max
not user_policy.max. if we get all related cpus offline and
online again, it will cause cpufreq_init_policy fail because
user_policy.min is higher than user_policy.max.

The solution is when user space tries to write scaling_(max|min)_freq,
the min/max of new_policy should be reinitialized with min/max
of user_policy, like what cpufreq_update_policy does.

Signed-off-by: Kevin Wangtao <[email protected]>
---
drivers/cpufreq/cpufreq.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index b79c532..82123a1 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -697,6 +697,8 @@ static ssize_t store_##file_name \
struct cpufreq_policy new_policy; \
\
memcpy(&new_policy, policy, sizeof(*policy)); \
+ new_policy.min = policy->user_policy.min; \
+ new_policy.max = policy->user_policy.max; \
\
ret = sscanf(buf, "%u", &new_policy.object); \
if (ret != 1) \
--
2.8.1


2018-05-27 06:16:30

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

Hi Kevin,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on pm/linux-next]
[also build test ERROR on v4.17-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Kevin-Wangtao/cpufreq-reinitialize-new-policy-min-max-when-writing-scaling_-max-min-_freq/20180527-132510
base: https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: i386-randconfig-x079-201821 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0
reproduce:
# save the attached .config to linux build tree
make ARCH=i386

All errors (new ones prefixed by >>):

drivers/cpufreq/cpufreq.c: In function 'store_scaling_min_freq':
>> drivers/cpufreq/cpufreq.c:700:12: error: invalid type argument of '->' (have 'struct cpufreq_policy')
new_policy->min = policy->user_policy.min; \
^
drivers/cpufreq/cpufreq.c:715:1: note: in expansion of macro 'store_one'
store_one(scaling_min_freq, min);
^~~~~~~~~
drivers/cpufreq/cpufreq.c:701:12: error: invalid type argument of '->' (have 'struct cpufreq_policy')
new_policy->max = policy->user_policy.max; \
^
drivers/cpufreq/cpufreq.c:715:1: note: in expansion of macro 'store_one'
store_one(scaling_min_freq, min);
^~~~~~~~~
drivers/cpufreq/cpufreq.c: In function 'store_scaling_max_freq':
>> drivers/cpufreq/cpufreq.c:700:12: error: invalid type argument of '->' (have 'struct cpufreq_policy')
new_policy->min = policy->user_policy.min; \
^
drivers/cpufreq/cpufreq.c:716:1: note: in expansion of macro 'store_one'
store_one(scaling_max_freq, max);
^~~~~~~~~
drivers/cpufreq/cpufreq.c:701:12: error: invalid type argument of '->' (have 'struct cpufreq_policy')
new_policy->max = policy->user_policy.max; \
^
drivers/cpufreq/cpufreq.c:716:1: note: in expansion of macro 'store_one'
store_one(scaling_max_freq, max);
^~~~~~~~~

vim +700 drivers/cpufreq/cpufreq.c

685
686 static int cpufreq_set_policy(struct cpufreq_policy *policy,
687 struct cpufreq_policy *new_policy);
688
689 /**
690 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
691 */
692 #define store_one(file_name, object) \
693 static ssize_t store_##file_name \
694 (struct cpufreq_policy *policy, const char *buf, size_t count) \
695 { \
696 int ret, temp; \
697 struct cpufreq_policy new_policy; \
698 \
699 memcpy(&new_policy, policy, sizeof(*policy)); \
> 700 new_policy->min = policy->user_policy.min; \
701 new_policy->max = policy->user_policy.max; \
702 \
703 ret = sscanf(buf, "%u", &new_policy.object); \
704 if (ret != 1) \
705 return -EINVAL; \
706 \
707 temp = new_policy.object; \
708 ret = cpufreq_set_policy(policy, &new_policy); \
709 if (!ret) \
710 policy->user_policy.object = temp; \
711 \
712 return ret ? ret : count; \
713 }
714

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation


Attachments:
(No filename) (3.34 kB)
.config.gz (33.67 kB)
Download all attachments

2018-05-29 08:47:54

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

On Friday, May 25, 2018 4:54:04 AM CEST Wangtao (Kevin, Kirin) wrote:
>
> 在 2018/5/24 15:45, Rafael J. Wysocki 写道:
> > On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
> > <[email protected]> wrote:
> >> consider such situation, current user_policy.min is 1000000,
> >> current user_policy.max is 1200000, in cpufreq_set_policy,
> >> other driver may update policy.min to 1200000, policy.max to
> >> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
> >> then user_policy.min will be 1300000, and user_policy.max is
> >> still 1200000, because the input value is checked with policy.max
> >> not user_policy.max. if we get all related cpus offline and
> >> online again, it will cause cpufreq_init_policy fail because
> >> user_policy.min is higher than user_policy.max.
> >
> > How do you reproduce this, exactly?
>
> I write a driver register CPUFREQ_POLICY_NOTIFIER, and when event is CPUFREQ_ADJUST,
> it will modify policy's min/max according to some conditions, I test it with writing
> scaling_(max|min)_freq to traverse all frequencies repeatly, and also repeat hotplug
> as background.

You are expected to use cpufreq_update_policy() to update the limits in policy
notifiers. Do you use it in your driver?


2018-05-29 08:50:50

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

On Saturday, May 26, 2018 8:50:46 AM CEST Wangtao (Kevin, Kirin) wrote:
>
> 在 2018/5/24 15:45, Rafael J. Wysocki 写道:
> > On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
> > <[email protected]> wrote:
> >> consider such situation, current user_policy.min is 1000000,
> >> current user_policy.max is 1200000, in cpufreq_set_policy,
> >> other driver may update policy.min to 1200000, policy.max to
> >> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
> >> then user_policy.min will be 1300000, and user_policy.max is
> >> still 1200000, because the input value is checked with policy.max
> >> not user_policy.max. if we get all related cpus offline and
> >> online again, it will cause cpufreq_init_policy fail because
> >> user_policy.min is higher than user_policy.max.
> >
> > How do you reproduce this, exactly?
>
> I can also reproduce this issue with upstream code, write max frequency to scaling_max_freq
> and scaling_min_freq, run benchmark to let cpu cooling take effect to clip freq, then write
> the cliped freq to scaling_max_freq, thus user_policy.min is still max frequency but user_policy.max
> is cliped freq which is lower than max frequency.

OK, this is a bit more convincing.

It looks like bad interaction between cpufreq_update_policy() and updates of
the limits via sysfs.


2018-05-29 10:28:16

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH V2] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

On 26-05-18, 15:16, Kevin Wangtao wrote:
> consider such situation, current user_policy.min is 1000000,
> current user_policy.max is 1200000, in cpufreq_set_policy,
> other driver may update policy.min to 1200000, policy.max to
> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
> then user_policy.min will be 1300000, and user_policy.max is
> still 1200000, because the input value is checked with policy.max
> not user_policy.max. if we get all related cpus offline and
> online again, it will cause cpufreq_init_policy fail because
> user_policy.min is higher than user_policy.max.
>
> The solution is when user space tries to write scaling_(max|min)_freq,
> the min/max of new_policy should be reinitialized with min/max
> of user_policy, like what cpufreq_update_policy does.
>
> Signed-off-by: Kevin Wangtao <[email protected]>
> ---
> drivers/cpufreq/cpufreq.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index b79c532..82123a1 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -697,6 +697,8 @@ static ssize_t store_##file_name \
> struct cpufreq_policy new_policy; \
> \
> memcpy(&new_policy, policy, sizeof(*policy)); \

Maybe add a comment here on why this is required ?

> + new_policy.min = policy->user_policy.min; \
> + new_policy.max = policy->user_policy.max; \
> \
> ret = sscanf(buf, "%u", &new_policy.object); \
> if (ret != 1) \

Acked-by: Viresh Kumar <[email protected]>

--
viresh

2018-05-30 08:05:10

by Tao Wang

[permalink] [raw]
Subject: Re: [PATCH V2] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq



?? 2018/5/29 18:26, Viresh Kumar д??:
> On 26-05-18, 15:16, Kevin Wangtao wrote:
>> consider such situation, current user_policy.min is 1000000,
>> current user_policy.max is 1200000, in cpufreq_set_policy,
>> other driver may update policy.min to 1200000, policy.max to
>> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
>> then user_policy.min will be 1300000, and user_policy.max is
>> still 1200000, because the input value is checked with policy.max
>> not user_policy.max. if we get all related cpus offline and
>> online again, it will cause cpufreq_init_policy fail because
>> user_policy.min is higher than user_policy.max.
>>
>> The solution is when user space tries to write scaling_(max|min)_freq,
>> the min/max of new_policy should be reinitialized with min/max
>> of user_policy, like what cpufreq_update_policy does.
>>
>> Signed-off-by: Kevin Wangtao <[email protected]>
>> ---
>> drivers/cpufreq/cpufreq.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index b79c532..82123a1 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -697,6 +697,8 @@ static ssize_t store_##file_name \
>> struct cpufreq_policy new_policy; \
>> \
>> memcpy(&new_policy, policy, sizeof(*policy)); \
>
> Maybe add a comment here on why this is required ?
OK
>
>> + new_policy.min = policy->user_policy.min; \
>> + new_policy.max = policy->user_policy.max; \
>> \
>> ret = sscanf(buf, "%u", &new_policy.object); \
>> if (ret != 1) \
>
> Acked-by: Viresh Kumar <[email protected]>
>


2018-05-30 08:12:05

by Tao Wang

[permalink] [raw]
Subject: [PATCH V3] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

consider such situation, current user_policy.min is 1000000,
current user_policy.max is 1200000, in cpufreq_set_policy,
other driver may update policy.min to 1200000, policy.max to
1300000. After that, If we input "echo 1300000 > scaling_min_freq",
then user_policy.min will be 1300000, and user_policy.max is
still 1200000, because the input value is checked with policy.max
not user_policy.max. if we get all related cpus offline and
online again, it will cause cpufreq_init_policy fail because
user_policy.min is higher than user_policy.max.

The solution is when user space tries to write scaling_(max|min)_freq,
the min/max of new_policy should be reinitialized with min/max
of user_policy, like what cpufreq_update_policy does.

Signed-off-by: Kevin Wangtao <[email protected]>
---
drivers/cpufreq/cpufreq.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index b79c532..a970113 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -697,6 +697,9 @@ static ssize_t store_##file_name \
struct cpufreq_policy new_policy; \
\
memcpy(&new_policy, policy, sizeof(*policy)); \
+ /* Initialized with user_policy to keep consistency */ \
+ new_policy.min = policy->user_policy.min; \
+ new_policy.max = policy->user_policy.max; \
\
ret = sscanf(buf, "%u", &new_policy.object); \
if (ret != 1) \
--
2.8.1


2018-05-30 11:12:50

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH V3] cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

On Wed, May 30, 2018 at 9:56 AM, Kevin Wangtao
<[email protected]> wrote:
> consider such situation, current user_policy.min is 1000000,
> current user_policy.max is 1200000, in cpufreq_set_policy,
> other driver may update policy.min to 1200000, policy.max to
> 1300000. After that, If we input "echo 1300000 > scaling_min_freq",
> then user_policy.min will be 1300000, and user_policy.max is
> still 1200000, because the input value is checked with policy.max
> not user_policy.max. if we get all related cpus offline and
> online again, it will cause cpufreq_init_policy fail because
> user_policy.min is higher than user_policy.max.
>
> The solution is when user space tries to write scaling_(max|min)_freq,
> the min/max of new_policy should be reinitialized with min/max
> of user_policy, like what cpufreq_update_policy does.
>
> Signed-off-by: Kevin Wangtao <[email protected]>

I've applied the v2 with modified subject and changelog and with the
ACK from Viresh.

Thanks!