Re: [PATCH v1 1/2] cpumask: Fix invalid uniprocessor mask assumption

From: Sander Vanheule
Date: Fri Jun 03 2022 - 11:14:01 EST


On Thu, 2022-06-02 at 15:48 -0700, Yury Norov wrote:
> On Thu, Jun 02, 2022 at 11:04:19PM +0200, Sander Vanheule wrote:
> > On uniprocessor builds, any CPU mask is assumed to contain exactly one
> > CPU (cpu0). This ignores the existence of empty masks, resulting in
> > incorrect behaviour for most of the implemented optimisations.
> >
> > Replace the uniprocessor optimisations with implementations that also
> > take into account empty masks. Also drop the incorrectly optimised
> > for_each_cpu implementations and use the generic implementations in all
> > cases.
> >
> > Signed-off-by: Sander Vanheule <sander@xxxxxxxxxxxxx>
> > ---
> >  include/linux/cpumask.h | 35 +++++++++++++++--------------------
> >  1 file changed, 15 insertions(+), 20 deletions(-)
> >
> > diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> > index fe29ac7cc469..ce8c7b27f6c9 100644
> > --- a/include/linux/cpumask.h
> > +++ b/include/linux/cpumask.h
> > @@ -117,51 +117,54 @@ static __always_inline unsigned int
> > cpumask_check(unsigned int cpu)
> >  }
> >  
> >  #if NR_CPUS == 1
> > -/* Uniprocessor.  Assume all masks are "1". */
> > +/* Uniprocessor. Assume all valid masks are "1" or empty. */
> >  static inline unsigned int cpumask_first(const struct cpumask *srcp)
> >  {
> > -       return 0;
> > +       return !(*cpumask_bits(srcp) & 1);
> >  }
>  
> I think, you can just drop this '#if NR_CPUS == 1' part and rely on SMP
> versions. find_first_bit() is optimized for short bitmaps, so I expect
> no overhead comparing to this.

I was initially planning to just drop the UP versions, but that means I have to
include lib/cpumask.o on all builds. I'll have another look at this.

[...]

> >  
> >  static inline unsigned int cpumask_next_wrap(int n, const struct cpumask
> > *mask,
> >                                              int start, bool wrap)
> >  {
> > -       /* cpu0 unless stop condition, wrap and at cpu0, then
> > nr_cpumask_bits */
> > -       return (wrap && n == 0);
> > +       /*
> > +        * nr_cpumask_bits at stop condition, wrap and at cpu0, or empty
> > mask
> > +        * otherwise cpu0
> > +        */
> > +       return (wrap && n == 0) || cpumask_first(mask);
> >  }

My tests contained a copy-paste error, so I missed a case. The results currently
still differ from the SMP implementation though, so this looks like another
reason to just drop the UP optimised version.

> >  
> >  /* cpu must be a valid cpu, ie 0, so there's no other choice. */
> > @@ -186,14 +189,6 @@ static inline int cpumask_any_distribute(const struct
> > cpumask *srcp)
> >         return cpumask_first(srcp);
> >  }
> >  
> > -#define for_each_cpu(cpu, mask)                        \
> > -       for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
> > -#define for_each_cpu_not(cpu, mask)            \
> > -       for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
> > -#define for_each_cpu_wrap(cpu, mask, start)    \
> > -       for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
> > -#define for_each_cpu_and(cpu, mask1, mask2)    \
> > -       for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask1, (void)mask2)
> >  #else
> >  /**
> >   * cpumask_first - get the first cpu in a cpumask
> > @@ -259,11 +254,13 @@ static inline unsigned int cpumask_next_zero(int n,
> > const struct cpumask *srcp)
> >  }
> >  
> >  int __pure cpumask_next_and(int n, const struct cpumask *, const struct
> > cpumask *);
> > +extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start,
> > bool wrap);
>
> is this extern needed?

I don't think it is, but this is a line that I just moved around in the header.
Like for the other functions, I could even add the __pure qualifier.


Thanks for the feedback,
Sander