cpumask: Implement cpumask_or_equal()
authorThomas Gleixner <tglx@linutronix.de>
Mon, 22 Jul 2019 18:47:24 +0000 (20:47 +0200)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 25 Jul 2019 13:47:37 +0000 (15:47 +0200)
The IPI code of x86 needs to evaluate whether the target cpumask is equal
to the cpu_online_mask or equal except for the calling CPU.

To replace the current implementation which requires the usage of a
temporary cpumask, which might involve allocations, add a new function
which compares a cpumask to the result of two other cpumasks which are
or'ed together before comparison.

This allows to make the required decision in one go and the calling code
then can check for the calling CPU being set in the target mask with
cpumask_test_cpu().

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20190722105220.585449120@linutronix.de
include/linux/bitmap.h
include/linux/cpumask.h
lib/bitmap.c

index f58e97446abcf5e3d75f2896af9d0acb7a6a9b5e..90528f12bdfa6a3a28ed1e00e5ab7e68b104535c 100644 (file)
@@ -120,6 +120,10 @@ extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
 extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
 extern int __bitmap_equal(const unsigned long *bitmap1,
                          const unsigned long *bitmap2, unsigned int nbits);
+extern bool __pure __bitmap_or_equal(const unsigned long *src1,
+                                    const unsigned long *src2,
+                                    const unsigned long *src3,
+                                    unsigned int nbits);
 extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
                        unsigned int nbits);
 extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
@@ -321,6 +325,25 @@ static inline int bitmap_equal(const unsigned long *src1,
        return __bitmap_equal(src1, src2, nbits);
 }
 
+/**
+ * bitmap_or_equal - Check whether the or of two bitnaps is equal to a third
+ * @src1:      Pointer to bitmap 1
+ * @src2:      Pointer to bitmap 2 will be or'ed with bitmap 1
+ * @src3:      Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
+ *
+ * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
+ */
+static inline bool bitmap_or_equal(const unsigned long *src1,
+                                  const unsigned long *src2,
+                                  const unsigned long *src3,
+                                  unsigned int nbits)
+{
+       if (!small_const_nbits(nbits))
+               return __bitmap_or_equal(src1, src2, src3, nbits);
+
+       return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
+}
+
 static inline int bitmap_intersects(const unsigned long *src1,
                        const unsigned long *src2, unsigned int nbits)
 {
index 693124900f0a8b0126e0a692be71d81429d5c930..0c7db5efe66c739f8f79975554153928b8606c17 100644 (file)
@@ -475,6 +475,20 @@ static inline bool cpumask_equal(const struct cpumask *src1p,
                                                 nr_cpumask_bits);
 }
 
+/**
+ * cpumask_or_equal - *src1p | *src2p == *src3p
+ * @src1p: the first input
+ * @src2p: the second input
+ * @src3p: the third input
+ */
+static inline bool cpumask_or_equal(const struct cpumask *src1p,
+                                   const struct cpumask *src2p,
+                                   const struct cpumask *src3p)
+{
+       return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
+                              cpumask_bits(src3p), nr_cpumask_bits);
+}
+
 /**
  * cpumask_intersects - (*src1p & *src2p) != 0
  * @src1p: the first input
index bbe2589e8497d147447d3528f2787e8ac748bdcc..f9e834841e941826392ebc6ccf5650186bf657fd 100644 (file)
@@ -59,6 +59,26 @@ int __bitmap_equal(const unsigned long *bitmap1,
 }
 EXPORT_SYMBOL(__bitmap_equal);
 
+bool __bitmap_or_equal(const unsigned long *bitmap1,
+                      const unsigned long *bitmap2,
+                      const unsigned long *bitmap3,
+                      unsigned int bits)
+{
+       unsigned int k, lim = bits / BITS_PER_LONG;
+       unsigned long tmp;
+
+       for (k = 0; k < lim; ++k) {
+               if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
+                       return false;
+       }
+
+       if (!(bits % BITS_PER_LONG))
+               return true;
+
+       tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
+       return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
+}
+
 void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
 {
        unsigned int k, lim = BITS_TO_LONGS(bits);