* [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid()
@ 2025-06-04 21:39 Yury Norov
2025-07-02 18:27 ` Rafael J. Wysocki
0 siblings, 1 reply; 5+ messages in thread
From: Yury Norov @ 2025-06-04 21:39 UTC (permalink / raw)
To: Rafael J. Wysocki, Daniel Lezcano, linux-pm, linux-kernel
Cc: Yury Norov [NVIDIA]
From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
The function opencodes the for_each_cpu_from() by using an open for-loop.
Fix that in sake of readability.
While there, drop the 'valid' variable as it's pretty useless here.
Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
---
drivers/cpuidle/dt_idle_states.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c
index 97feb7d8fb23..558d49838990 100644
--- a/drivers/cpuidle/dt_idle_states.c
+++ b/drivers/cpuidle/dt_idle_states.c
@@ -98,7 +98,6 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
{
int cpu;
struct device_node *cpu_node, *curr_state_node;
- bool valid = true;
/*
* Compare idle state phandles for index idx on all CPUs in the
@@ -107,20 +106,17 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
* retrieved from. If a mismatch is found bail out straight
* away since we certainly hit a firmware misconfiguration.
*/
- for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
- cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
+ cpu = cpumask_first(cpumask) + 1;
+ for_each_cpu_from(cpu, cpumask) {
cpu_node = of_cpu_device_node_get(cpu);
curr_state_node = of_get_cpu_state_node(cpu_node, idx);
- if (state_node != curr_state_node)
- valid = false;
-
of_node_put(curr_state_node);
of_node_put(cpu_node);
- if (!valid)
- break;
+ if (state_node != curr_state_node)
+ return false;
}
- return valid;
+ return true;
}
/**
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid()
2025-06-04 21:39 [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid() Yury Norov
@ 2025-07-02 18:27 ` Rafael J. Wysocki
2025-07-07 15:30 ` Yury Norov
0 siblings, 1 reply; 5+ messages in thread
From: Rafael J. Wysocki @ 2025-07-02 18:27 UTC (permalink / raw)
To: Yury Norov; +Cc: Rafael J. Wysocki, Daniel Lezcano, linux-pm, linux-kernel
On Wed, Jun 4, 2025 at 11:39 PM Yury Norov <yury.norov@gmail.com> wrote:
>
> From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
>
> The function opencodes the for_each_cpu_from() by using an open for-loop.
> Fix that in sake of readability.
>
> While there, drop the 'valid' variable as it's pretty useless here.
>
> Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> ---
> drivers/cpuidle/dt_idle_states.c | 14 +++++---------
> 1 file changed, 5 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c
> index 97feb7d8fb23..558d49838990 100644
> --- a/drivers/cpuidle/dt_idle_states.c
> +++ b/drivers/cpuidle/dt_idle_states.c
> @@ -98,7 +98,6 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> {
> int cpu;
> struct device_node *cpu_node, *curr_state_node;
> - bool valid = true;
>
> /*
> * Compare idle state phandles for index idx on all CPUs in the
> @@ -107,20 +106,17 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> * retrieved from. If a mismatch is found bail out straight
> * away since we certainly hit a firmware misconfiguration.
> */
> - for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
> - cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
> + cpu = cpumask_first(cpumask) + 1;
Doing
cpu = cpumask_next(cpumask_first(cpumask), cpumask);
here might save a few iterations for sparse cpumasks.
> + for_each_cpu_from(cpu, cpumask) {
> cpu_node = of_cpu_device_node_get(cpu);
> curr_state_node = of_get_cpu_state_node(cpu_node, idx);
> - if (state_node != curr_state_node)
> - valid = false;
> -
> of_node_put(curr_state_node);
> of_node_put(cpu_node);
> - if (!valid)
> - break;
> + if (state_node != curr_state_node)
> + return false;
> }
>
> - return valid;
> + return true;
> }
>
> /**
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid()
2025-07-02 18:27 ` Rafael J. Wysocki
@ 2025-07-07 15:30 ` Yury Norov
2025-07-07 16:11 ` Rafael J. Wysocki
0 siblings, 1 reply; 5+ messages in thread
From: Yury Norov @ 2025-07-07 15:30 UTC (permalink / raw)
To: Rafael J. Wysocki; +Cc: Daniel Lezcano, linux-pm, linux-kernel
On Wed, Jul 02, 2025 at 08:27:21PM +0200, Rafael J. Wysocki wrote:
> On Wed, Jun 4, 2025 at 11:39 PM Yury Norov <yury.norov@gmail.com> wrote:
> >
> > From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> >
> > The function opencodes the for_each_cpu_from() by using an open for-loop.
> > Fix that in sake of readability.
> >
> > While there, drop the 'valid' variable as it's pretty useless here.
> >
> > Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > ---
> > drivers/cpuidle/dt_idle_states.c | 14 +++++---------
> > 1 file changed, 5 insertions(+), 9 deletions(-)
> >
> > diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c
> > index 97feb7d8fb23..558d49838990 100644
> > --- a/drivers/cpuidle/dt_idle_states.c
> > +++ b/drivers/cpuidle/dt_idle_states.c
> > @@ -98,7 +98,6 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> > {
> > int cpu;
> > struct device_node *cpu_node, *curr_state_node;
> > - bool valid = true;
> >
> > /*
> > * Compare idle state phandles for index idx on all CPUs in the
> > @@ -107,20 +106,17 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> > * retrieved from. If a mismatch is found bail out straight
> > * away since we certainly hit a firmware misconfiguration.
> > */
> > - for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
> > - cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
> > + cpu = cpumask_first(cpumask) + 1;
>
> Doing
>
> cpu = cpumask_next(cpumask_first(cpumask), cpumask);
>
> here might save a few iterations for sparse cpumasks.
For that it's better to use cpumask_nth(1).
I believe there will be no benefit in calling cpumask_next() before
entering the loop because for_each_cpu_from() is based on it, and it
will be called anyways.
I can send v2 with cpumask_nth() change if you like it better. Please
let me know.
Thanks,
Yury
> > + for_each_cpu_from(cpu, cpumask) {
> > cpu_node = of_cpu_device_node_get(cpu);
> > curr_state_node = of_get_cpu_state_node(cpu_node, idx);
> > - if (state_node != curr_state_node)
> > - valid = false;
> > -
> > of_node_put(curr_state_node);
> > of_node_put(cpu_node);
> > - if (!valid)
> > - break;
> > + if (state_node != curr_state_node)
> > + return false;
> > }
> >
> > - return valid;
> > + return true;
> > }
> >
> > /**
> > --
> > 2.43.0
> >
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid()
2025-07-07 15:30 ` Yury Norov
@ 2025-07-07 16:11 ` Rafael J. Wysocki
2025-07-10 12:54 ` Rafael J. Wysocki
0 siblings, 1 reply; 5+ messages in thread
From: Rafael J. Wysocki @ 2025-07-07 16:11 UTC (permalink / raw)
To: Yury Norov; +Cc: Rafael J. Wysocki, Daniel Lezcano, linux-pm, linux-kernel
On Mon, Jul 7, 2025 at 5:31 PM Yury Norov <yury.norov@gmail.com> wrote:
>
> On Wed, Jul 02, 2025 at 08:27:21PM +0200, Rafael J. Wysocki wrote:
> > On Wed, Jun 4, 2025 at 11:39 PM Yury Norov <yury.norov@gmail.com> wrote:
> > >
> > > From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > >
> > > The function opencodes the for_each_cpu_from() by using an open for-loop.
> > > Fix that in sake of readability.
> > >
> > > While there, drop the 'valid' variable as it's pretty useless here.
> > >
> > > Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > > ---
> > > drivers/cpuidle/dt_idle_states.c | 14 +++++---------
> > > 1 file changed, 5 insertions(+), 9 deletions(-)
> > >
> > > diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c
> > > index 97feb7d8fb23..558d49838990 100644
> > > --- a/drivers/cpuidle/dt_idle_states.c
> > > +++ b/drivers/cpuidle/dt_idle_states.c
> > > @@ -98,7 +98,6 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> > > {
> > > int cpu;
> > > struct device_node *cpu_node, *curr_state_node;
> > > - bool valid = true;
> > >
> > > /*
> > > * Compare idle state phandles for index idx on all CPUs in the
> > > @@ -107,20 +106,17 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> > > * retrieved from. If a mismatch is found bail out straight
> > > * away since we certainly hit a firmware misconfiguration.
> > > */
> > > - for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
> > > - cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
> > > + cpu = cpumask_first(cpumask) + 1;
> >
> > Doing
> >
> > cpu = cpumask_next(cpumask_first(cpumask), cpumask);
> >
> > here might save a few iterations for sparse cpumasks.
>
> For that it's better to use cpumask_nth(1).
>
> I believe there will be no benefit in calling cpumask_next() before
> entering the loop because for_each_cpu_from() is based on it, and it
> will be called anyways.
Fair enough.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid()
2025-07-07 16:11 ` Rafael J. Wysocki
@ 2025-07-10 12:54 ` Rafael J. Wysocki
0 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2025-07-10 12:54 UTC (permalink / raw)
To: Yury Norov; +Cc: Daniel Lezcano, linux-pm, linux-kernel
On Mon, Jul 7, 2025 at 6:11 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Mon, Jul 7, 2025 at 5:31 PM Yury Norov <yury.norov@gmail.com> wrote:
> >
> > On Wed, Jul 02, 2025 at 08:27:21PM +0200, Rafael J. Wysocki wrote:
> > > On Wed, Jun 4, 2025 at 11:39 PM Yury Norov <yury.norov@gmail.com> wrote:
> > > >
> > > > From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > > >
> > > > The function opencodes the for_each_cpu_from() by using an open for-loop.
> > > > Fix that in sake of readability.
> > > >
> > > > While there, drop the 'valid' variable as it's pretty useless here.
> > > >
> > > > Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > > > ---
> > > > drivers/cpuidle/dt_idle_states.c | 14 +++++---------
> > > > 1 file changed, 5 insertions(+), 9 deletions(-)
> > > >
> > > > diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c
> > > > index 97feb7d8fb23..558d49838990 100644
> > > > --- a/drivers/cpuidle/dt_idle_states.c
> > > > +++ b/drivers/cpuidle/dt_idle_states.c
> > > > @@ -98,7 +98,6 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> > > > {
> > > > int cpu;
> > > > struct device_node *cpu_node, *curr_state_node;
> > > > - bool valid = true;
> > > >
> > > > /*
> > > > * Compare idle state phandles for index idx on all CPUs in the
> > > > @@ -107,20 +106,17 @@ static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
> > > > * retrieved from. If a mismatch is found bail out straight
> > > > * away since we certainly hit a firmware misconfiguration.
> > > > */
> > > > - for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
> > > > - cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
> > > > + cpu = cpumask_first(cpumask) + 1;
> > >
> > > Doing
> > >
> > > cpu = cpumask_next(cpumask_first(cpumask), cpumask);
> > >
> > > here might save a few iterations for sparse cpumasks.
> >
> > For that it's better to use cpumask_nth(1).
> >
> > I believe there will be no benefit in calling cpumask_next() before
> > entering the loop because for_each_cpu_from() is based on it, and it
> > will be called anyways.
>
> Fair enough.
And so applied as 6.17 material, thanks!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-07-10 12:54 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-04 21:39 [PATCH] cpuidle: dt: fix opencoded for_each_cpu() in idle_state_valid() Yury Norov
2025-07-02 18:27 ` Rafael J. Wysocki
2025-07-07 15:30 ` Yury Norov
2025-07-07 16:11 ` Rafael J. Wysocki
2025-07-10 12:54 ` Rafael J. Wysocki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).