Linux-PM Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] cpufreq: cached_resolved_idx can not be negative
@ 2020-07-30  4:08 Viresh Kumar
  2020-07-30  5:59 ` Amit Kucheria
  0 siblings, 1 reply; 6+ messages in thread
From: Viresh Kumar @ 2020-07-30  4:08 UTC (permalink / raw)
  To: Rafael Wysocki, Andy Gross, Bjorn Andersson, Viresh Kumar
  Cc: linux-pm, Vincent Guittot, ionela.voinescu, linux-arm-msm, linux-kernel

It is not possible for cached_resolved_idx to be invalid here as the
cpufreq core always sets index to a positive value.

Change its type to unsigned int and fix qcom usage a bit.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/cpufreq/qcom-cpufreq-hw.c | 5 +----
 include/linux/cpufreq.h           | 2 +-
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/cpufreq/qcom-cpufreq-hw.c b/drivers/cpufreq/qcom-cpufreq-hw.c
index 0a04b6f03b9a..8c0842bd6c5a 100644
--- a/drivers/cpufreq/qcom-cpufreq-hw.c
+++ b/drivers/cpufreq/qcom-cpufreq-hw.c
@@ -66,13 +66,10 @@ static unsigned int qcom_cpufreq_hw_fast_switch(struct cpufreq_policy *policy,
 						unsigned int target_freq)
 {
 	void __iomem *perf_state_reg = policy->driver_data;
-	int index;
+	unsigned int index;
 	unsigned long freq;
 
 	index = policy->cached_resolved_idx;
-	if (index < 0)
-		return 0;
-
 	writel_relaxed(index, perf_state_reg);
 
 	freq = policy->freq_table[index].frequency;
diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
index e62b022cb07e..58687a5bf9c8 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -127,7 +127,7 @@ struct cpufreq_policy {
 
 	 /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
 	unsigned int cached_target_freq;
-	int cached_resolved_idx;
+	unsigned int cached_resolved_idx;
 
 	/* Synchronization for frequency transitions */
 	bool			transition_ongoing; /* Tracks transition status */
-- 
2.14.1


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

* Re: [PATCH] cpufreq: cached_resolved_idx can not be negative
  2020-07-30  4:08 [PATCH] cpufreq: cached_resolved_idx can not be negative Viresh Kumar
@ 2020-07-30  5:59 ` Amit Kucheria
  2020-07-30  6:10   ` Viresh Kumar
  0 siblings, 1 reply; 6+ messages in thread
From: Amit Kucheria @ 2020-07-30  5:59 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael Wysocki, Andy Gross, Bjorn Andersson, Linux PM list,
	Vincent Guittot, ionela.voinescu, linux-arm-msm, LKML

On Thu, Jul 30, 2020 at 9:38 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> It is not possible for cached_resolved_idx to be invalid here as the
> cpufreq core always sets index to a positive value.
>
> Change its type to unsigned int and fix qcom usage a bit.

Shouldn't you fix up idx in cpufreq_driver_resolve_freq() to be
unsigned int too?

> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  drivers/cpufreq/qcom-cpufreq-hw.c | 5 +----
>  include/linux/cpufreq.h           | 2 +-
>  2 files changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/cpufreq/qcom-cpufreq-hw.c b/drivers/cpufreq/qcom-cpufreq-hw.c
> index 0a04b6f03b9a..8c0842bd6c5a 100644
> --- a/drivers/cpufreq/qcom-cpufreq-hw.c
> +++ b/drivers/cpufreq/qcom-cpufreq-hw.c
> @@ -66,13 +66,10 @@ static unsigned int qcom_cpufreq_hw_fast_switch(struct cpufreq_policy *policy,
>                                                 unsigned int target_freq)
>  {
>         void __iomem *perf_state_reg = policy->driver_data;
> -       int index;
> +       unsigned int index;
>         unsigned long freq;
>
>         index = policy->cached_resolved_idx;
> -       if (index < 0)
> -               return 0;
> -
>         writel_relaxed(index, perf_state_reg);
>
>         freq = policy->freq_table[index].frequency;
> diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
> index e62b022cb07e..58687a5bf9c8 100644
> --- a/include/linux/cpufreq.h
> +++ b/include/linux/cpufreq.h
> @@ -127,7 +127,7 @@ struct cpufreq_policy {
>
>          /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
>         unsigned int cached_target_freq;
> -       int cached_resolved_idx;
> +       unsigned int cached_resolved_idx;
>
>         /* Synchronization for frequency transitions */
>         bool                    transition_ongoing; /* Tracks transition status */
> --
> 2.14.1
>

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

* Re: [PATCH] cpufreq: cached_resolved_idx can not be negative
  2020-07-30  5:59 ` Amit Kucheria
@ 2020-07-30  6:10   ` Viresh Kumar
  2020-07-30  6:32     ` Amit Kucheria
  0 siblings, 1 reply; 6+ messages in thread
From: Viresh Kumar @ 2020-07-30  6:10 UTC (permalink / raw)
  To: Amit Kucheria
  Cc: Rafael Wysocki, Andy Gross, Bjorn Andersson, Linux PM list,
	Vincent Guittot, ionela.voinescu, linux-arm-msm, LKML

On 30-07-20, 11:29, Amit Kucheria wrote:
> On Thu, Jul 30, 2020 at 9:38 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> >
> > It is not possible for cached_resolved_idx to be invalid here as the
> > cpufreq core always sets index to a positive value.
> >
> > Change its type to unsigned int and fix qcom usage a bit.
> 
> Shouldn't you fix up idx in cpufreq_driver_resolve_freq() to be
> unsigned int too?

Yes, merged this into the patch.

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 0128de3603df..053d72e52a31 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -538,7 +538,7 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
        policy->cached_target_freq = target_freq;
 
        if (cpufreq_driver->target_index) {
-               int idx;
+               unsigned int idx;
 
                idx = cpufreq_frequency_table_target(policy, target_freq,
                                                     CPUFREQ_RELATION_L);

-- 
viresh

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

* Re: [PATCH] cpufreq: cached_resolved_idx can not be negative
  2020-07-30  6:10   ` Viresh Kumar
@ 2020-07-30  6:32     ` Amit Kucheria
  2020-07-30  6:41       ` Viresh Kumar
  0 siblings, 1 reply; 6+ messages in thread
From: Amit Kucheria @ 2020-07-30  6:32 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael Wysocki, Andy Gross, Bjorn Andersson, Linux PM list,
	Vincent Guittot, ionela.voinescu, linux-arm-msm, LKML

On Thu, Jul 30, 2020 at 11:40 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 30-07-20, 11:29, Amit Kucheria wrote:
> > On Thu, Jul 30, 2020 at 9:38 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> > >
> > > It is not possible for cached_resolved_idx to be invalid here as the
> > > cpufreq core always sets index to a positive value.
> > >
> > > Change its type to unsigned int and fix qcom usage a bit.
> >
> > Shouldn't you fix up idx in cpufreq_driver_resolve_freq() to be
> > unsigned int too?
>
> Yes, merged this into the patch.

Looking at this more closely, I found another call site for
cpufreq_frequency_table_target() in cpufreq.c that needs the index to
be unsigned int.

But then cpufreq_frequency_table_target() returns -EINVAL, so we
should be able to handle int values.

I think you will need to fix the unconditional assignment of
    policy->cached_resolved_idx = idx
in cpufreq_driver_resolve_freq(). It doesn't check for -EINVAL, so the
qcom driver is write in checking for a negative value.

>
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index 0128de3603df..053d72e52a31 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -538,7 +538,7 @@ unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
>         policy->cached_target_freq = target_freq;
>
>         if (cpufreq_driver->target_index) {
> -               int idx;
> +               unsigned int idx;
>
>                 idx = cpufreq_frequency_table_target(policy, target_freq,
>                                                      CPUFREQ_RELATION_L);
>
> --
> viresh

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

* Re: [PATCH] cpufreq: cached_resolved_idx can not be negative
  2020-07-30  6:32     ` Amit Kucheria
@ 2020-07-30  6:41       ` Viresh Kumar
  2020-07-30 16:25         ` Rafael J. Wysocki
  0 siblings, 1 reply; 6+ messages in thread
From: Viresh Kumar @ 2020-07-30  6:41 UTC (permalink / raw)
  To: Amit Kucheria
  Cc: Rafael Wysocki, Andy Gross, Bjorn Andersson, Linux PM list,
	Vincent Guittot, ionela.voinescu, linux-arm-msm, LKML

On 30-07-20, 12:02, Amit Kucheria wrote:
> Looking at this more closely, I found another call site for
> cpufreq_frequency_table_target() in cpufreq.c that needs the index to
> be unsigned int.
> 
> But then cpufreq_frequency_table_target() returns -EINVAL, so we

It returns -EINVAL only in the case where the relation is not valid,
which will never happen. Maybe that should be marked with WARN or BUG
and we should drop return value of -EINVAL.

Rafael ?

> should be able to handle int values.

And so no.

> I think you will need to fix the unconditional assignment of
>     policy->cached_resolved_idx = idx
> in cpufreq_driver_resolve_freq(). It doesn't check for -EINVAL, so the
> qcom driver is write in checking for a negative value.

Right, I don't want it to have that check for the reason stated above.

The point is I don't want code that verifies cached-idx at all, it is
useless.

-- 
viresh

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

* Re: [PATCH] cpufreq: cached_resolved_idx can not be negative
  2020-07-30  6:41       ` Viresh Kumar
@ 2020-07-30 16:25         ` Rafael J. Wysocki
  0 siblings, 0 replies; 6+ messages in thread
From: Rafael J. Wysocki @ 2020-07-30 16:25 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Amit Kucheria, Rafael Wysocki, Andy Gross, Bjorn Andersson,
	Linux PM list, Vincent Guittot, Ionela Voinescu, linux-arm-msm,
	LKML

On Thu, Jul 30, 2020 at 8:41 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 30-07-20, 12:02, Amit Kucheria wrote:
> > Looking at this more closely, I found another call site for
> > cpufreq_frequency_table_target() in cpufreq.c that needs the index to
> > be unsigned int.
> >
> > But then cpufreq_frequency_table_target() returns -EINVAL, so we
>
> It returns -EINVAL only in the case where the relation is not valid,
> which will never happen. Maybe that should be marked with WARN or BUG
> and we should drop return value of -EINVAL.
>
> Rafael ?

Yeah, make it a WARN_ON_ONCE() IMO.

> > should be able to handle int values.
>
> And so no.
>
> > I think you will need to fix the unconditional assignment of
> >     policy->cached_resolved_idx = idx
> > in cpufreq_driver_resolve_freq(). It doesn't check for -EINVAL, so the
> > qcom driver is write in checking for a negative value.
>
> Right, I don't want it to have that check for the reason stated above.
>
> The point is I don't want code that verifies cached-idx at all, it is
> useless.
>
> --
> viresh

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

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-30  4:08 [PATCH] cpufreq: cached_resolved_idx can not be negative Viresh Kumar
2020-07-30  5:59 ` Amit Kucheria
2020-07-30  6:10   ` Viresh Kumar
2020-07-30  6:32     ` Amit Kucheria
2020-07-30  6:41       ` Viresh Kumar
2020-07-30 16:25         ` Rafael J. Wysocki

Linux-PM Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-pm/0 linux-pm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-pm linux-pm/ https://lore.kernel.org/linux-pm \
		linux-pm@vger.kernel.org
	public-inbox-index linux-pm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-pm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git