[v3,5/5] lib/cpumask: reorganize cpumask_local_spread() logic

Message ID 20221208183101.1162006-6-yury.norov@gmail.com
State New
Headers
Series cpumask: improve on cpumask_local_spread() locality |

Commit Message

Yury Norov Dec. 8, 2022, 6:31 p.m. UTC
  Now after moving all NUMA logic into sched_numa_find_nth_cpu(),
else-branch of cpumask_local_spread() is just a function call, and
we can simplify logic by using ternary operator.

While here, replace BUG() with WARN().

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/cpumask.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
  

Comments

Peter Lafreniere Dec. 8, 2022, 8:17 p.m. UTC | #1
> Now after moving all NUMA logic into sched_numa_find_nth_cpu(),
> else-branch of cpumask_local_spread() is just a function call, and
> we can simplify logic by using ternary operator.
>
> While here, replace BUG() with WARN().
Why make this change? It's still as bad to hit the WARN_ON as it was before.

> Signed-off-by: Yury Norov yury.norov@gmail.com
>
> ---
> lib/cpumask.c | 16 ++++++----------
> 1 file changed, 6 insertions(+), 10 deletions(-)
>
> diff --git a/lib/cpumask.c b/lib/cpumask.c
> index 255974cd6734..c7029fb3c372 100644
> --- a/lib/cpumask.c
> +++ b/lib/cpumask.c
> @@ -127,16 +127,12 @@ unsigned int cpumask_local_spread(unsigned int i, int node)
> /* Wrap: we always want a cpu. */
> i %= num_online_cpus();
>
> - if (node == NUMA_NO_NODE) {
> - cpu = cpumask_nth(i, cpu_online_mask);
> - if (cpu < nr_cpu_ids)
> - return cpu;
> - } else {
> - cpu = sched_numa_find_nth_cpu(cpu_online_mask, i, node);
> - if (cpu < nr_cpu_ids)
> - return cpu;
> - }
> - BUG();
> + cpu = node == NUMA_NO_NODE ?
> + cpumask_nth(i, cpu_online_mask) :
> + sched_numa_find_nth_cpu(cpu_online_mask, i, node);
I find the if version clearer, and cleaner too if you drop the brackets.

For the ternary version it would be nice to parenthesize the equality
like you did in cmp() in 3/5.

> +
> + WARN_ON(cpu >= nr_cpu_ids);
>
> + return cpu;
> }
> EXPORT_SYMBOL(cpumask_local_spread);
>
> --
> 2.34.1

Minor nit: cmp() in 3/5 could use a longer name. The file's long, and
cmp() doesn't explain _what_ it's comparing. How about cmp_cpumask() or
something related to the function using it?

Other than the above particularities, the whole series looks good to me.
Reviewed-by: Peter Lafreniere <peter@n8pjl.ca>
  
Yury Norov Dec. 8, 2022, 8:41 p.m. UTC | #2
On Thu, Dec 08, 2022 at 08:17:22PM +0000, Peter Lafreniere wrote:
> > Now after moving all NUMA logic into sched_numa_find_nth_cpu(),
> > else-branch of cpumask_local_spread() is just a function call, and
> > we can simplify logic by using ternary operator.
> >
> > While here, replace BUG() with WARN().
> Why make this change? It's still as bad to hit the WARN_ON as it was before.

For example, because of this:

  > Greg, please don't do this
  > 
  > > ChangeSet@1.614, 2002-09-05 08:33:20-07:00, greg@kroah.com
  > >   USB: storage driver: replace show_trace() with BUG()
  > 
  > that BUG() thing is _way_ out of line, and has killed a few of my machines
  > several times for no good reason. It actively hurts debuggability, because
  > the machine is totally dead after it, and the whole and ONLY point of
  > BUG() messages is to help debugging and make it clear that we can't handle
  > something.
  > 
  > In this case, we _can_ handle it, and we're much better off with a machine
  > that works and that you can look up the messages with than killing it.
  > 
  > Rule of thumb: BUG() is only good for something that never happens and
  > that we really have no other option for (ie state is so corrupt that
  > continuing is deadly).
  > 
  > 		Linus
  
Peter Lafreniere Dec. 8, 2022, 8:57 p.m. UTC | #3
> On Thu, Dec 08, 2022 at 08:17:22PM +0000, Peter Lafreniere wrote:
> > > Now after moving all NUMA logic into sched_numa_find_nth_cpu(),
> > > else-branch of cpumask_local_spread() is just a function call, and
> > > we can simplify logic by using ternary operator.
> > >
> > > While here, replace BUG() with WARN().
> > Why make this change? It's still as bad to hit the WARN_ON as it was before.
>
> For example, because of this:
>
>  > Greg, please don't do this
>  >
>  > > ChangeSet@1.614, 2002-09-05 08:33:20-07:00, greg@kroah.com
>  > >   USB: storage driver: replace show_trace() with BUG()
>  >
>  > that BUG() thing is _way_ out of line, and has killed a few of my machines
>  > several times for no good reason. It actively hurts debuggability, because
>  > the machine is totally dead after it, and the whole and ONLY point of
>  > BUG() messages is to help debugging and make it clear that we can't handle
>  > something.
>  >
>  > In this case, we _can_ handle it, and we're much better off with a machine
>  > that works and that you can look up the messages with than killing it.
>  >
>  > Rule of thumb: BUG() is only good for something that never happens and
>  > that we really have no other option for (ie state is so corrupt that
>  > continuing is deadly).
>  >
>  >            Linus

Fair enough. It's not like it'll be hit anyway. My concern was for if
any of the 23 callers get an invalid result. I guess that if that causes
a crash, then so be it. We have the warning to track down the cause.

Thanks for the explanation,
Peter Lafreniere <peter@n8pjl.ca>
  

Patch

diff --git a/lib/cpumask.c b/lib/cpumask.c
index 255974cd6734..c7029fb3c372 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -127,16 +127,12 @@  unsigned int cpumask_local_spread(unsigned int i, int node)
 	/* Wrap: we always want a cpu. */
 	i %= num_online_cpus();
 
-	if (node == NUMA_NO_NODE) {
-		cpu = cpumask_nth(i, cpu_online_mask);
-		if (cpu < nr_cpu_ids)
-			return cpu;
-	} else {
-		cpu = sched_numa_find_nth_cpu(cpu_online_mask, i, node);
-		if (cpu < nr_cpu_ids)
-			return cpu;
-	}
-	BUG();
+	cpu = node == NUMA_NO_NODE ?
+		cpumask_nth(i, cpu_online_mask) :
+		sched_numa_find_nth_cpu(cpu_online_mask, i, node);
+
+	WARN_ON(cpu >= nr_cpu_ids);
+	return cpu;
 }
 EXPORT_SYMBOL(cpumask_local_spread);