diff mbox

cpufreq: reinitialize new policy min/max when writing scaling_(max|min)_freq

Message ID 1527144234-96396-1-git-send-email-kevin.wangtao@hisilicon.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Tao Wang May 24, 2018, 6:43 a.m. UTC
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 <kevin.wangtao@hisilicon.com>
---
 drivers/cpufreq/cpufreq.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Rafael J. Wysocki May 24, 2018, 7:45 a.m. UTC | #1
On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
<kevin.wangtao@hisilicon.com> 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 <kevin.wangtao@hisilicon.com>
> ---
>  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
>
Tao Wang May 25, 2018, 2:54 a.m. UTC | #2
在 2018/5/24 15:45, Rafael J. Wysocki 写道:
> On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
> <kevin.wangtao@hisilicon.com> 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 <kevin.wangtao@hisilicon.com>
>> ---
>>   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
>>
> 
> .
>
Tao Wang May 26, 2018, 6:50 a.m. UTC | #3
在 2018/5/24 15:45, Rafael J. Wysocki 写道:
> On Thu, May 24, 2018 at 8:43 AM, Kevin Wangtao
> <kevin.wangtao@hisilicon.com> 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 <kevin.wangtao@hisilicon.com>
>> ---
>>   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
>>
> 
> .
>
kernel test robot May 27, 2018, 6:15 a.m. UTC | #4
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
Rafael J. Wysocki May 29, 2018, 8:45 a.m. UTC | #5
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
> > <kevin.wangtao@hisilicon.com> 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?
Rafael J. Wysocki May 29, 2018, 8:47 a.m. UTC | #6
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
> > <kevin.wangtao@hisilicon.com> 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.
diff mbox

Patch

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)							\