Add helpers to determine if two sets of VMA flags are precisely the same,
that is - that every flag set one is set in another, and neither contain
any flags not set in the other.

We also introduce vma_flags_same_pair() for cases where we want to compare
two sets of VMA flags which are both non-const values.

Also update the VMA tests to reflect the change, we already implicitly test
that this functions correctly having used it for testing purposes
previously.

Signed-off-by: Lorenzo Stoakes (Oracle) <[email protected]>
---
 include/linux/mm.h                 | 28 ++++++++++++++++++++++++++++
 tools/testing/vma/include/custom.h | 12 ------------
 tools/testing/vma/include/dup.h    | 21 +++++++++++++++++++++
 3 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 2c16c744d49d..33d0c2af2c75 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1209,6 +1209,34 @@ static __always_inline vma_flags_t 
vma_flags_diff_pair(const vma_flags_t *flags,
        return dst;
 }
 
+/* Determine if flags and flags_other have precisely the same flags set. */
+static __always_inline bool vma_flags_same_pair(const vma_flags_t *flags,
+                                               const vma_flags_t *flags_other)
+{
+       const unsigned long *bitmap = flags->__vma_flags;
+       const unsigned long *bitmap_other = flags_other->__vma_flags;
+
+       return bitmap_equal(bitmap, bitmap_other, NUM_VMA_FLAG_BITS);
+}
+
+/* Determine if flags and flags_other have precisely the same flags set.  */
+static __always_inline bool vma_flags_same_mask(const vma_flags_t *flags,
+                                               vma_flags_t flags_other)
+{
+       const unsigned long *bitmap = flags->__vma_flags;
+       const unsigned long *bitmap_other = flags_other.__vma_flags;
+
+       return bitmap_equal(bitmap, bitmap_other, NUM_VMA_FLAG_BITS);
+}
+
+/*
+ * Helper macro to determine if only the specific flags are set, e.g.:
+ *
+ * if (vma_flags_same(&flags, VMA_WRITE_BIT) { ... }
+ */
+#define vma_flags_same(flags, ...) \
+       vma_flags_same_mask(flags, mk_vma_flags(__VA_ARGS__))
+
 /*
  * Helper to test that ALL specified flags are set in a VMA.
  *
diff --git a/tools/testing/vma/include/custom.h 
b/tools/testing/vma/include/custom.h
index 433b3396c281..92fe156ed7d6 100644
--- a/tools/testing/vma/include/custom.h
+++ b/tools/testing/vma/include/custom.h
@@ -99,17 +99,5 @@ static inline void vma_lock_init(struct vm_area_struct *vma, 
bool reset_refcnt)
        if (reset_refcnt)
                refcount_set(&vma->vm_refcnt, 0);
 }
-
-/* Place here until needed in the kernel code. */
-static __always_inline bool vma_flags_same_mask(vma_flags_t *flags,
-                                               vma_flags_t flags_other)
-{
-       const unsigned long *bitmap = flags->__vma_flags;
-       const unsigned long *bitmap_other = flags_other.__vma_flags;
-
-       return bitmap_equal(bitmap, bitmap_other, NUM_VMA_FLAG_BITS);
-}
-#define vma_flags_same(flags, ...) \
-       vma_flags_same_mask(flags, mk_vma_flags(__VA_ARGS__))
 #define VMA_SPECIAL_FLAGS mk_vma_flags(VMA_IO_BIT, VMA_DONTEXPAND_BIT, \
                                       VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT)
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index c3be8a2381e1..fa2df96f9dee 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -951,6 +951,27 @@ static __always_inline vma_flags_t 
vma_flags_diff_pair(const vma_flags_t *flags,
        return dst;
 }
 
+static __always_inline bool vma_flags_same_pair(const vma_flags_t *flags,
+                                               const vma_flags_t *flags_other)
+{
+       const unsigned long *bitmap = flags->__vma_flags;
+       const unsigned long *bitmap_other = flags_other->__vma_flags;
+
+       return bitmap_equal(bitmap, bitmap_other, NUM_VMA_FLAG_BITS);
+}
+
+static __always_inline bool vma_flags_same_mask(const vma_flags_t *flags,
+                                               vma_flags_t flags_other)
+{
+       const unsigned long *bitmap = flags->__vma_flags;
+       const unsigned long *bitmap_other = flags_other.__vma_flags;
+
+       return bitmap_equal(bitmap, bitmap_other, NUM_VMA_FLAG_BITS);
+}
+
+#define vma_flags_same(flags, ...) \
+       vma_flags_same_mask(flags, mk_vma_flags(__VA_ARGS__))
+
 static inline bool vma_test_all_mask(const struct vm_area_struct *vma,
                                     vma_flags_t flags)
 {
-- 
2.53.0


Reply via email to