All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
@ 2023-03-29 13:36 Yajun Deng
  2023-03-29 14:21 ` Rafael J. Wysocki
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Yajun Deng @ 2023-03-29 13:36 UTC (permalink / raw)
  To: rafael, viresh.kumar; +Cc: linux-pm, linux-kernel, Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is
NULL. In this case, it should return -EINVAL before __target_index().

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
 drivers/cpufreq/cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index c0e5be0fe2d6..308a3df1a940 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
 		return cpufreq_driver->target(policy, target_freq, relation);
 	}
 
-	if (!cpufreq_driver->target_index)
+	if (!cpufreq_driver->target_index || !policy->freq_table)
 		return -EINVAL;
 
 	return __target_index(policy, policy->cached_resolved_idx);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
  2023-03-29 13:36 [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target() Yajun Deng
@ 2023-03-29 14:21 ` Rafael J. Wysocki
  2023-03-30  1:39 ` Yajun Deng
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Rafael J. Wysocki @ 2023-03-29 14:21 UTC (permalink / raw)
  To: Yajun Deng; +Cc: rafael, viresh.kumar, linux-pm, linux-kernel

On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote:
>
> __resolve_freq() may be return target_freq if policy->freq_table is
> NULL. In this case, it should return -EINVAL before __target_index().

Even so, __target_index() itself doesn't dereference freq_table
AFAICS, so arguably the driver should be prepared to deal with a NULL
freq_table which comes from it after all.

Or, if you want to argue that drivers providing ->target_index() must
also provide freq_table that is not NULL, a check to that effect needs
to be done at the driver registration time IMO.

> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
> ---
>  drivers/cpufreq/cpufreq.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index c0e5be0fe2d6..308a3df1a940 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
>                 return cpufreq_driver->target(policy, target_freq, relation);
>         }
>
> -       if (!cpufreq_driver->target_index)
> +       if (!cpufreq_driver->target_index || !policy->freq_table)
>                 return -EINVAL;
>
>         return __target_index(policy, policy->cached_resolved_idx);
> --

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
  2023-03-29 13:36 [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target() Yajun Deng
  2023-03-29 14:21 ` Rafael J. Wysocki
@ 2023-03-30  1:39 ` Yajun Deng
  2023-03-30  3:57   ` Viresh Kumar
  2023-04-03  4:11 ` Viresh Kumar
  2023-04-04  3:08 ` Yajun Deng
  3 siblings, 1 reply; 7+ messages in thread
From: Yajun Deng @ 2023-03-30  1:39 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: viresh.kumar, linux-pm, linux-kernel

March 29, 2023 10:21 PM, "Rafael J. Wysocki" <rafael@kernel.org> wrote:

> On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote:
> 
>> __resolve_freq() may be return target_freq if policy->freq_table is
>> NULL. In this case, it should return -EINVAL before __target_index().
> 
> Even so, __target_index() itself doesn't dereference freq_table
> AFAICS, so arguably the driver should be prepared to deal with a NULL
> freq_table which comes from it after all.
> 

But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;'
in __target_index(), if driver doesn't provide freq_table, __target_index()
will fault before the driver itself.

> Or, if you want to argue that drivers providing ->target_index() must
> also provide freq_table that is not NULL, a check to that effect needs
> to be done at the driver registration time IMO.
> 
>> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
>> ---
>> drivers/cpufreq/cpufreq.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index c0e5be0fe2d6..308a3df1a940 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
>> return cpufreq_driver->target(policy, target_freq, relation);
>> }
>> 
>> - if (!cpufreq_driver->target_index)
>> + if (!cpufreq_driver->target_index || !policy->freq_table)
>> return -EINVAL;
>> 
>> return __target_index(policy, policy->cached_resolved_idx);
>> --

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
  2023-03-30  1:39 ` Yajun Deng
@ 2023-03-30  3:57   ` Viresh Kumar
  2023-03-30 10:10     ` Rafael J. Wysocki
  0 siblings, 1 reply; 7+ messages in thread
From: Viresh Kumar @ 2023-03-30  3:57 UTC (permalink / raw)
  To: Yajun Deng; +Cc: Rafael J. Wysocki, linux-pm, linux-kernel

On 30-03-23, 01:39, Yajun Deng wrote:
> March 29, 2023 10:21 PM, "Rafael J. Wysocki" <rafael@kernel.org> wrote:
> 
> > On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote:
> > 
> >> __resolve_freq() may be return target_freq if policy->freq_table is
> >> NULL. In this case, it should return -EINVAL before __target_index().
> > 
> > Even so, __target_index() itself doesn't dereference freq_table
> > AFAICS, so arguably the driver should be prepared to deal with a NULL
> > freq_table which comes from it after all.
> > 
> 
> But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;'
> in __target_index(), if driver doesn't provide freq_table, __target_index()
> will fault before the driver itself.

Driver must provide a freq table here.

-- 
viresh

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
  2023-03-30  3:57   ` Viresh Kumar
@ 2023-03-30 10:10     ` Rafael J. Wysocki
  0 siblings, 0 replies; 7+ messages in thread
From: Rafael J. Wysocki @ 2023-03-30 10:10 UTC (permalink / raw)
  To: Viresh Kumar; +Cc: Yajun Deng, Rafael J. Wysocki, linux-pm, linux-kernel

On Thu, Mar 30, 2023 at 5:57 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 30-03-23, 01:39, Yajun Deng wrote:
> > March 29, 2023 10:21 PM, "Rafael J. Wysocki" <rafael@kernel.org> wrote:
> >
> > > On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote:
> > >
> > >> __resolve_freq() may be return target_freq if policy->freq_table is
> > >> NULL. In this case, it should return -EINVAL before __target_index().
> > >
> > > Even so, __target_index() itself doesn't dereference freq_table
> > > AFAICS, so arguably the driver should be prepared to deal with a NULL
> > > freq_table which comes from it after all.
> > >
> >
> > But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;'
> > in __target_index(), if driver doesn't provide freq_table, __target_index()
> > will fault before the driver itself.
>
> Driver must provide a freq table here.

OK, so let's do the check when the driver gets registered.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
  2023-03-29 13:36 [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target() Yajun Deng
  2023-03-29 14:21 ` Rafael J. Wysocki
  2023-03-30  1:39 ` Yajun Deng
@ 2023-04-03  4:11 ` Viresh Kumar
  2023-04-04  3:08 ` Yajun Deng
  3 siblings, 0 replies; 7+ messages in thread
From: Viresh Kumar @ 2023-04-03  4:11 UTC (permalink / raw)
  To: Yajun Deng; +Cc: rafael, linux-pm, linux-kernel

On 29-03-23, 21:36, Yajun Deng wrote:
> __resolve_freq() may be return target_freq if policy->freq_table is
> NULL. In this case, it should return -EINVAL before __target_index().
> 
> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
> ---
>  drivers/cpufreq/cpufreq.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index c0e5be0fe2d6..308a3df1a940 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
>  		return cpufreq_driver->target(policy, target_freq, relation);
>  	}
>  
> -	if (!cpufreq_driver->target_index)
> +	if (!cpufreq_driver->target_index || !policy->freq_table)
>  		return -EINVAL;

Hi,

I have sent an alternate patch [1] for this, please try it.

-- 
viresh

[1] https://lore.kernel.org/all/53d4ed4e5b18a59a48790434f8146fb207e11c49.1680494945.git.viresh.kumar@linaro.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
  2023-03-29 13:36 [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target() Yajun Deng
                   ` (2 preceding siblings ...)
  2023-04-03  4:11 ` Viresh Kumar
@ 2023-04-04  3:08 ` Yajun Deng
  3 siblings, 0 replies; 7+ messages in thread
From: Yajun Deng @ 2023-04-04  3:08 UTC (permalink / raw)
  To: Viresh Kumar; +Cc: rafael, linux-pm, linux-kernel

April 3, 2023 12:11 PM, "Viresh Kumar" <viresh.kumar@linaro.org> wrote:

> On 29-03-23, 21:36, Yajun Deng wrote:
> 
>> __resolve_freq() may be return target_freq if policy->freq_table is
>> NULL. In this case, it should return -EINVAL before __target_index().
>> 
>> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
>> ---
>> drivers/cpufreq/cpufreq.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index c0e5be0fe2d6..308a3df1a940 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
>> return cpufreq_driver->target(policy, target_freq, relation);
>> }
>> 
>> - if (!cpufreq_driver->target_index)
>> + if (!cpufreq_driver->target_index || !policy->freq_table)
>> return -EINVAL;
> 
> Hi,
> 
> I have sent an alternate patch [1] for this, please try it.
>
 
Thanks, v2 is fine.

> --
> viresh
> 
> [1]
> https://lore.kernel.org/all/53d4ed4e5b18a59a48790434f8146fb207e11c49.1680494945.git.viresh.kumar@lin
> ro.org

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-04-04  3:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-29 13:36 [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target() Yajun Deng
2023-03-29 14:21 ` Rafael J. Wysocki
2023-03-30  1:39 ` Yajun Deng
2023-03-30  3:57   ` Viresh Kumar
2023-03-30 10:10     ` Rafael J. Wysocki
2023-04-03  4:11 ` Viresh Kumar
2023-04-04  3:08 ` Yajun Deng

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.