]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm: reintroduce vma_flags_test() as a singular flag test
authorLorenzo Stoakes (Oracle) <ljs@kernel.org>
Thu, 5 Mar 2026 10:50:17 +0000 (10:50 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 5 Apr 2026 20:53:18 +0000 (13:53 -0700)
Since we've now renamed vma_flags_test() to vma_flags_test_any() to be
very clear as to what we are in fact testing, we now have the opportunity
to bring vma_flags_test() back, but for explicitly testing a single VMA
flag.

This is useful, as often flag tests are against a single flag, and
vma_flags_test_any(flags, VMA_READ_BIT) reads oddly and potentially causes
confusion.

We use sparse to enforce that users won't accidentally pass vm_flags_t to
this function without it being flagged so this should make it harder to
get this wrong.

Of course, passing vma_flags_t to the function is impossible, as it is a
struct.

Also update the VMA tests to reflect this change.

Link: https://lkml.kernel.org/r/f33f8d7f16c3f3d286a1dc2cba12c23683073134.1772704455.git.ljs@kernel.org
Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Babu Moger <babu.moger@amd.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Chao Yu <chao@kernel.org>
Cc: Chatre, Reinette <reinette.chatre@intel.com>
Cc: Chunhai Guo <guochunhai@vivo.com>
Cc: Damien Le Maol <dlemoal@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Dave Martin <dave.martin@arm.com>
Cc: Gao Xiang <xiang@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Hongbo Li <lihongbo22@huawei.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: James Morse <james.morse@arm.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Jeffle Xu <jefflexu@linux.alibaba.com>
Cc: Johannes Thumshirn <jth@kernel.org>
Cc: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: "Luck, Tony" <tony.luck@intel.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Naohiro Aota <naohiro.aota@wdc.com>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Sandeep Dhavale <dhavale@google.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vishal Verma <vishal.l.verma@intel.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Yue Hu <zbestahu@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/mm.h
mm/hugetlb.c
mm/shmem.c
tools/testing/vma/include/dup.h

index 9dcdf13570fb5eb92d3e67f337c920d29055c391..9392723a5c5024790330e23d28de5d2ea9ebe5a1 100644 (file)
@@ -1050,6 +1050,19 @@ static __always_inline vma_flags_t __mk_vma_flags(size_t count,
        return flags;
 }
 
+/*
+ * Test whether a specific VMA flag is set, e.g.:
+ *
+ * if (vma_flags_test(flags, VMA_READ_BIT)) { ... }
+ */
+static __always_inline bool vma_flags_test(const vma_flags_t *flags,
+               vma_flag_t bit)
+{
+       const unsigned long *bitmap = flags->__vma_flags;
+
+       return test_bit((__force int)bit, bitmap);
+}
+
 /*
  * Helper macro which bitwise-or combines the specified input flags into a
  * vma_flags_t bitmap value. E.g.:
@@ -1956,8 +1969,8 @@ static inline bool vma_desc_is_cow_mapping(struct vm_area_desc *desc)
 {
        const vma_flags_t *flags = &desc->vma_flags;
 
-       return vma_flags_test_any(flags, VMA_MAYWRITE_BIT) &&
-               !vma_flags_test_any(flags, VMA_SHARED_BIT);
+       return vma_flags_test(flags, VMA_MAYWRITE_BIT) &&
+               !vma_flags_test(flags, VMA_SHARED_BIT);
 }
 
 #ifndef CONFIG_MMU
index fbbe74f944261392ae81bf90243344acccdc2430..9363b6072c0a0df6cef942c8d37c375906214164 100644 (file)
@@ -6593,7 +6593,7 @@ long hugetlb_reserve_pages(struct inode *inode,
         * attempt will be made for VM_NORESERVE to allocate a page
         * without using reserves
         */
-       if (vma_flags_test_any(&vma_flags, VMA_NORESERVE_BIT))
+       if (vma_flags_test(&vma_flags, VMA_NORESERVE_BIT))
                return 0;
 
        /*
index 965a8908200b616fa5df8d3c4326e646b286460c..5e7dcf5bc5d3c9a758b067dac442b83d56f51840 100644 (file)
@@ -3086,7 +3086,7 @@ static struct inode *__shmem_get_inode(struct mnt_idmap *idmap,
        spin_lock_init(&info->lock);
        atomic_set(&info->stop_eviction, 0);
        info->seals = F_SEAL_SEAL;
-       info->flags = vma_flags_test_any(&flags, VMA_NORESERVE_BIT)
+       info->flags = vma_flags_test(&flags, VMA_NORESERVE_BIT)
                ? SHMEM_F_NORESERVE : 0;
        info->i_crtime = inode_get_mtime(inode);
        info->fsflags = (dir == NULL) ? 0 :
@@ -5827,7 +5827,7 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name,
                                       unsigned int i_flags)
 {
        const unsigned long shmem_flags =
-               vma_flags_test_any(&flags, VMA_NORESERVE_BIT) ? SHMEM_F_NORESERVE : 0;
+               vma_flags_test(&flags, VMA_NORESERVE_BIT) ? SHMEM_F_NORESERVE : 0;
        struct inode *inode;
        struct file *res;
 
index ef6b9d963acc0e5ec04d88b71119cac61ed0e706..630478f0d5836206e02883cf4532f39202613b7b 100644 (file)
@@ -844,6 +844,14 @@ static inline vma_flags_t __mk_vma_flags(size_t count, const vma_flag_t *bits);
 #define mk_vma_flags(...) __mk_vma_flags(COUNT_ARGS(__VA_ARGS__), \
                                         (const vma_flag_t []){__VA_ARGS__})
 
+static __always_inline bool vma_flags_test(const vma_flags_t *flags,
+               vma_flag_t bit)
+{
+       const unsigned long *bitmap = flags->__vma_flags;
+
+       return test_bit((__force int)bit, bitmap);
+}
+
 static __always_inline bool vma_flags_test_any_mask(const vma_flags_t *flags,
                vma_flags_t to_test)
 {