Skip to contentSkip to conversation

xen: fix for_each_cpu when NR_CPUS=1

PATCH In review Dario Faggioli 5 participants 8 messages Mar 11, 2021 – Mar 12, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: fix for_each_cpu when NR_CPUS=1

When running an hypervisor build with NR_CPUS=1 for_each_cpu does not take into account whether the bit of the CPU is set or not in the provided mask.

This means that whatever we have in the bodies of these loops is always done once, even if the mask was empty and it should never be done. This is clearly a bug and was in fact causing an assert to trigger in credit2 code.

Removing the special casing of NR_CPUS == 1 makes things work again.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
---
I'm not really sure whether this should be 4.15 material.

It's definitely a bug, IMO. The risk is also pretty low, considering that no one should really run Xen in this configuration (NR_CPUS=1, I mean). Which is also the reason why it's probably not really important that we fix it at this point of the release cycle.

---
 xen/include/xen/cpumask.h |    5 -----
 1 file changed, 5 deletions(-)
xen/include/xen/cpumask.h +0 -5
index 256b60b106..e69589fc08 100644
@@ -368,15 +368,10 @@ static inline void free_cpumask_var(cpumask_var_t mask)
 #define FREE_CPUMASK_VAR(m) free_cpumask_var(m)
 #endif
 
-#if NR_CPUS > 1
 #define for_each_cpu(cpu, mask)			\
 	for ((cpu) = cpumask_first(mask);	\
 	     (cpu) < nr_cpu_ids;		\
 	     (cpu) = cpumask_next(cpu, mask))
-#else /* NR_CPUS == 1 */
-#define for_each_cpu(cpu, mask)			\
-	for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)(mask))
-#endif /* NR_CPUS */
 
 /*
  * The following particular system cpumasks and operations manage
#1 lore raw
Quoted 38 lines · Dario Faggioli

When running an hypervisor build with NR_CPUS=1 for_each_cpu does not take into account whether the bit of the CPU is set or not in the provided mask.

This means that whatever we have in the bodies of these loops is always done once, even if the mask was empty and it should never be done. This is clearly a bug and was in fact causing an assert to trigger in credit2 code.

Removing the special casing of NR_CPUS == 1 makes things work again.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
---
I'm not really sure whether this should be 4.15 material.

It's definitely a bug, IMO. The risk is also pretty low, considering that no one should really run Xen in this configuration (NR_CPUS=1, I mean). Which is also the reason why it's probably not really important that we fix it at this point of the release cycle.

---
  xen/include/xen/cpumask.h |    5 -----
  1 file changed, 5 deletions(-)
xen/include/xen/cpumask.h +0 -0
index 256b60b106..e69589fc08 100644
@@ -368,15 +368,10 @@ static inline void free_cpumask_var(cpumask_var_t mask)
  #define FREE_CPUMASK_VAR(m) free_cpumask_var(m)
  #endif

xen/include/xen/cpumask.h static inline void free_cpumask_var(cpumask_var_t mask) · line 379

Quoted 10 lines
xen/include/xen/cpumask.h +0 -5
 
-#if NR_CPUS > 1
  #define for_each_cpu(cpu, mask)			\
  	for ((cpu) = cpumask_first(mask);	\
  	     (cpu) < nr_cpu_ids;		\
  	     (cpu) = cpumask_next(cpu, mask))
-#else /* NR_CPUS == 1 */
-#define for_each_cpu(cpu, mask)			\
-	for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)(mask))
-#endif /* NR_CPUS */

Wouldn't it make sense to drop the other NR_CPUS == 1 optimization further down, too?

IMO this is adding clutter for no real gain, as NR_CPUS == 1 Xen hypervisor builds aiming at high performance are probably not existing anywhere in the universe.

Juergen

Reviewed-by on v1 1/1
#2 lore raw
In reply to Dario Faggioli
Quoted 4 lines · Dario Faggioli

When running an hypervisor build with NR_CPUS=1 for_each_cpu does not take into account whether the bit of the CPU is set or not in the provided mask.

Quoted 8 lines

This means that whatever we have in the bodies of these loops is always done once, even if the mask was empty and it should never be done. This is clearly a bug and was in fact causing an assert to trigger in credit2 code.

Removing the special casing of NR_CPUS == 1 makes things work again.

Doesn't this want a Reported-by: Roger?

And FTR I don't really mind the other NR_CPUS == 1 piece of logic to remain there.

Quoted 6 lines

I'm not really sure whether this should be 4.15 material.

It's definitely a bug, IMO. The risk is also pretty low, considering that no one should really run Xen in this configuration (NR_CPUS=1, I mean). Which is also the reason why it's probably not really important that we fix it at this point of the release cycle.

I agree; I'll put it in the 4.16 bucket.

Jan

#3 lore raw
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Dario Faggioli

Removing the special casing of NR_CPUS == 1 makes things work again.

Doesn't this want a Reported-by: Roger?

It definitely does! And I even forgot to Cc him... Sorry Roger :-(

Will you add it, or do you want me to resubmit with it?

Quoted 2 lines

Thanks

Quoted 3 lines

And FTR I don't really mind the other NR_CPUS == 1 piece of logic to remain there.

Ok. I agree with Juergen that they're totally useless, but at least they're not wrong.

Oh, BTW, since you mentioned in your other email the fact that this comes from Linux, I've had a look there and there's a comment in their cpumask.h file, under the NR_CPUS==1 define, looking like this:

/* Uniprocessor. Assume all masks are "1". */

https://elixir.bootlin.com/linux/latest/source/include/linux/cpumask.h#L149

Of course, that does not make the fact that for_each_cpu and for_each_cpu_not are identical less weird, and the whole thing still does not make sense, at least not to me.

I'm wondering whether or not it is worth to report this to them too, as I have the impression that they just don't care.

Thanks and Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)
#4 lore raw
Quoted 10 lines · Dario Faggioli
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Dario Faggioli

Removing the special casing of NR_CPUS == 1 makes things work again.

Doesn't this want a Reported-by: Roger?

It definitely does! And I even forgot to Cc him... Sorry Roger :-(

No problem! Thanks for sending the patch.

Quoted 18 lines

Will you add it, or do you want me to resubmit with it?

Quoted 2 lines

Thanks

Quoted 3 lines

And FTR I don't really mind the other NR_CPUS == 1 piece of logic to remain there.

Ok. I agree with Juergen that they're totally useless, but at least they're not wrong.

Oh, BTW, since you mentioned in your other email the fact that this comes from Linux, I've had a look there and there's a comment in their cpumask.h file, under the NR_CPUS==1 define, looking like this:

/* Uniprocessor. Assume all masks are "1". */

Quoted 8 lines

https://elixir.bootlin.com/linux/latest/source/include/linux/cpumask.h#L149

Of course, that does not make the fact that for_each_cpu and for_each_cpu_not are identical less weird, and the whole thing still does not make sense, at least not to me.

I'm wondering whether or not it is worth to report this to them too, as I have the impression that they just don't care.

I would report it, worse case they will just ignore, but it would be nice to get it fixed there also, so that someone else doesn't have to discover the same brokenness.

Roger.

#5 lore raw
In reply to Dario Faggioli
Quoted 5 lines · Dario Faggioli
Quoted 4 lines · Jan Beulich
Quoted 3 lines · Dario Faggioli

Removing the special casing of NR_CPUS == 1 makes things work again.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Doesn't this want a Reported-by: Roger?

It definitely does! And I even forgot to Cc him... Sorry Roger :-(

Will you add it, or do you want me to resubmit with it?

No need to, I've taken note.

Jan

#6 lore raw
In reply to Dario Faggioli

Dario Faggioli writes ("[PATCH] xen: fix for_each_cpu when NR_CPUS=1"):

Quoted 10 lines

When running an hypervisor build with NR_CPUS=1 for_each_cpu does not take into account whether the bit of the CPU is set or not in the provided mask.

This means that whatever we have in the bodies of these loops is always done once, even if the mask was empty and it should never be done. This is clearly a bug and was in fact causing an assert to trigger in credit2 code.

Removing the special casing of NR_CPUS == 1 makes things work again.

Quoted 6 lines

I'm not really sure whether this should be 4.15 material.

It's definitely a bug, IMO. The risk is also pretty low, considering that no one should really run Xen in this configuration (NR_CPUS=1, I mean). Which is also the reason why it's probably not really important that we fix it at this point of the release cycle.

Given that it clearly only affects NR_CPUS==1, I think the risk/reward tradeoff is unambiguously positive.

xen/include/xen/cpumask.h static inline void free_cpumask_var(cpumask_var_t mask) · line 372

Quoted 3 lines
xen/include/xen/cpumask.h +0 -1
-#if NR_CPUS > 1
 #define for_each_cpu(cpu, mask)			\
 	for ((cpu) = cpumask_first(mask);	\

Just a thought: does cpumask_first work on an empty mask ?

Ian.

#7 lore raw
Quoted 6 lines · Ian Jackson

Dario Faggioli writes ("[PATCH] xen: fix for_each_cpu when NR_CPUS=1"):

xen/include/xen/cpumask.h static inline void free_cpumask_var(cpumask_var_t mask) · line 372

Quoted 3 lines
xen/include/xen/cpumask.h +0 -1
-#if NR_CPUS > 1
 #define for_each_cpu(cpu, mask)			\
 	for ((cpu) = cpumask_first(mask);	\

Just a thought: does cpumask_first work on an empty mask ?

I'm sure it does, yes - it'll return a value >= nr_cpu_ids in this case. If it didn't work, NR_CPUS > 1 would also be badly affected.

Jan