From nobody Tue Apr 7 16:15:39 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAFDD3AA500; Thu, 12 Mar 2026 19:17:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773343029; cv=none; b=JiWZGy+CVuH/hG2wTjWqCOVLeTclQ2poCJ46ZSFJVjWTHPa4FdKT8gA7Wzdcjul1oUtm6OGaqM4CCco08udG3wgeOAaI517/nM9+Kmx8TK21jC2CO4coIBHNUnWZAMD7M/CNscTWPGAcCjKirMtSgJtSHclgdeCGU3yn6Btt0QQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773343029; c=relaxed/simple; bh=i+p3elfatBiwccRrtwrt6PrqJWD9J7hcCLdkdKdPFP0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uFPNLlN9CzdfJ44oCLGbtOVfCPYKYQixf0szzdTK+A4AmI5kokWWDDmjjqVclNtD3TzbIEcRFSoQTgh69LlTKTgYtm7EOzuNT5YaGNRX1Hycj5YXifnE6DmI4ANlIQnVyxyLzrl6dfJr+iM444xxNzKTCD+EUrUNmrZCEcSbgdg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lCzF1EL6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lCzF1EL6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40E8BC2BC9E; Thu, 12 Mar 2026 19:17:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773343028; bh=i+p3elfatBiwccRrtwrt6PrqJWD9J7hcCLdkdKdPFP0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lCzF1EL6UxvNJlquvfqUvlI7j5rI7nUseE8n0RKgXj2V/yhEz2VzNeMlMmxytEe2k MLE4/7YKr7+qAcDrU06qTMz0ScZSVc6hv4K0GtRbvY2fUTvzSWbTc+QGHOd5tiRr1I 0gG/7Y7g5Znu84JOzEx+LYN+K+6NQpA3xTx16pE+X6wlmlPP6jqJSzUEmLoQT2oWYs k+92/igEcOYF5iRBxNAVus3555sLzV/mfqD8VYURk5hi0o8VzWsAmQP7zEWS0fDHiS 3xsezc48bSiMiRz+bMMG+VlJMrP8Y2Hmp0NOic8DObDgCPjNa+qitsFlRod8llDHSG EY+G2NpLC4p2A== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , Pedro Falcato , Mike Rapoport , Suren Baghdasaryan , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Vineet Gupta , Russell King , Catalin Marinas , Will Deacon , Brian Cain , Huacai Chen , WANG Xuerui , Thomas Bogendoerfer , Dinh Nguyen , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Richard Weinberger , Anton Ivanov , Johannes Berg , Alexander Viro , Christian Brauner , Jan Kara , Xu Xin , Chengming Zhou , Michal Hocko , Paul Moore , Stephen Smalley , Ondrej Mosnacek , linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-fsdevel@vger.kernel.org, selinux@vger.kernel.org Subject: [PATCH 14/20] mm/vma: introduce vma_test[_any[_mask]](), and make inlining consistent Date: Thu, 12 Mar 2026 19:16:12 +0000 Message-ID: <7b2ad0c46854dc39ba9111ad0e94770e7e1b9f66.1773342102.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce helper functions and macros to make it convenient to test flags and flag masks for VMAs, specifically: * vma_test() - determine if a single VMA flag is set in a VMA. * vma_test_any_mask() - determine if any flags in a vma_flags_t value are set in a VMA. * vma_test_any() - Helper macro to test if any of specific flags are set. Also, there are a mix of 'inline's and '__always_inline's in VMA helper function declarations, update to consistently use __always_inline. Finally, update the VMA tests to reflect the changes. Signed-off-by: Lorenzo Stoakes (Oracle) --- include/linux/mm.h | 49 +++++++++++++++++++++----- include/linux/mm_types.h | 12 ++++--- tools/testing/vma/include/dup.h | 61 +++++++++++++++++++++------------ 3 files changed, 88 insertions(+), 34 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 84c7f6ac5790..4b574d941ea3 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1013,7 +1013,8 @@ static inline void vm_flags_mod(struct vm_area_struct= *vma, __vm_flags_mod(vma, set, clear); } =20 -static inline bool __vma_atomic_valid_flag(struct vm_area_struct *vma, vma= _flag_t bit) +static __always_inline bool __vma_atomic_valid_flag(struct vm_area_struct = *vma, + vma_flag_t bit) { const vm_flags_t mask =3D BIT((__force int)bit); =20 @@ -1028,7 +1029,8 @@ static inline bool __vma_atomic_valid_flag(struct vm_= area_struct *vma, vma_flag_ * Set VMA flag atomically. Requires only VMA/mmap read lock. Only specific * valid flags are allowed to do this. */ -static inline void vma_set_atomic_flag(struct vm_area_struct *vma, vma_fla= g_t bit) +static __always_inline void vma_set_atomic_flag(struct vm_area_struct *vma, + vma_flag_t bit) { unsigned long *bitmap =3D vma->flags.__vma_flags; =20 @@ -1044,7 +1046,8 @@ static inline void vma_set_atomic_flag(struct vm_area= _struct *vma, vma_flag_t bi * This is necessarily racey, so callers must ensure that serialisation is * achieved through some other means, or that races are permissible. */ -static inline bool vma_test_atomic_flag(struct vm_area_struct *vma, vma_fl= ag_t bit) +static __always_inline bool vma_test_atomic_flag(struct vm_area_struct *vm= a, + vma_flag_t bit) { if (__vma_atomic_valid_flag(vma, bit)) return test_bit((__force int)bit, &vma->vm_flags); @@ -1249,13 +1252,41 @@ static __always_inline bool vma_flags_same_mask(con= st vma_flags_t *flags, #define vma_flags_same(flags, ...) \ vma_flags_same_mask(flags, mk_vma_flags(__VA_ARGS__)) =20 +/* + * Test whether a specific flag in the VMA is set, e.g.: + * + * if (vma_test(vma, VMA_READ_BIT)) { ... } + */ +static __always_inline bool vma_test(const struct vm_area_struct *vma, + vma_flag_t bit) +{ + return vma_flags_test(&vma->flags, bit); +} + +/* Helper to test any VMA flags in a VMA . */ +static __always_inline bool vma_test_any_mask(const struct vm_area_struct = *vma, + vma_flags_t flags) +{ + return vma_flags_test_any_mask(&vma->flags, flags); +} + +/* + * Helper macro for testing whether any VMA flags are set in a VMA, + * e.g.: + * + * if (vma_test_any(vma, VMA_IO_BIT, VMA_PFNMAP_BIT, + * VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT)) { ... } + */ +#define vma_test_any(vma, ...) \ + vma_test_any_mask(vma, mk_vma_flags(__VA_ARGS__)) + /* * Helper to test that ALL specified flags are set in a VMA. * * Note: appropriate locks must be held, this function does not acquire th= em for * you. */ -static inline bool vma_test_all_mask(const struct vm_area_struct *vma, +static __always_inline bool vma_test_all_mask(const struct vm_area_struct = *vma, vma_flags_t flags) { return vma_flags_test_all_mask(&vma->flags, flags); @@ -1275,7 +1306,7 @@ static inline bool vma_test_all_mask(const struct vm_= area_struct *vma, * Note: appropriate locks must be held, this function does not acquire th= em for * you. */ -static inline void vma_set_flags_mask(struct vm_area_struct *vma, +static __always_inline void vma_set_flags_mask(struct vm_area_struct *vma, vma_flags_t flags) { vma_flags_set_mask(&vma->flags, flags); @@ -1305,7 +1336,7 @@ static __always_inline bool vma_desc_test(const struc= t vm_area_desc *desc, } =20 /* Helper to test any VMA flags in a VMA descriptor. */ -static inline bool vma_desc_test_any_mask(const struct vm_area_desc *desc, +static __always_inline bool vma_desc_test_any_mask(const struct vm_area_de= sc *desc, vma_flags_t flags) { return vma_flags_test_any_mask(&desc->vma_flags, flags); @@ -1322,7 +1353,7 @@ static inline bool vma_desc_test_any_mask(const struc= t vm_area_desc *desc, vma_desc_test_any_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 /* Helper to test all VMA flags in a VMA descriptor. */ -static inline bool vma_desc_test_all_mask(const struct vm_area_desc *desc, +static __always_inline bool vma_desc_test_all_mask(const struct vm_area_de= sc *desc, vma_flags_t flags) { return vma_flags_test_all_mask(&desc->vma_flags, flags); @@ -1338,7 +1369,7 @@ static inline bool vma_desc_test_all_mask(const struc= t vm_area_desc *desc, vma_desc_test_all_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 /* Helper to set all VMA flags in a VMA descriptor. */ -static inline void vma_desc_set_flags_mask(struct vm_area_desc *desc, +static __always_inline void vma_desc_set_flags_mask(struct vm_area_desc *d= esc, vma_flags_t flags) { vma_flags_set_mask(&desc->vma_flags, flags); @@ -1355,7 +1386,7 @@ static inline void vma_desc_set_flags_mask(struct vm_= area_desc *desc, vma_desc_set_flags_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 /* Helper to clear all VMA flags in a VMA descriptor. */ -static inline void vma_desc_clear_flags_mask(struct vm_area_desc *desc, +static __always_inline void vma_desc_clear_flags_mask(struct vm_area_desc = *desc, vma_flags_t flags) { vma_flags_clear_mask(&desc->vma_flags, flags); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 63a25f97cd1c..4a229cc0a06b 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1101,7 +1101,8 @@ static __always_inline vma_flags_t legacy_to_vma_flag= s(vm_flags_t flags) * IMPORTANT: This does not overwrite bytes past the first system word. The * caller must account for this. */ -static inline void vma_flags_overwrite_word(vma_flags_t *flags, unsigned l= ong value) +static __always_inline void vma_flags_overwrite_word(vma_flags_t *flags, + unsigned long value) { unsigned long *bitmap =3D flags->__vma_flags; =20 @@ -1114,7 +1115,8 @@ static inline void vma_flags_overwrite_word(vma_flags= _t *flags, unsigned long va * IMPORTANT: This does not overwrite bytes past the first system word. The * caller must account for this. */ -static inline void vma_flags_overwrite_word_once(vma_flags_t *flags, unsig= ned long value) +static __always_inline void vma_flags_overwrite_word_once(vma_flags_t *fla= gs, + unsigned long value) { unsigned long *bitmap =3D flags->__vma_flags; =20 @@ -1122,7 +1124,8 @@ static inline void vma_flags_overwrite_word_once(vma_= flags_t *flags, unsigned lo } =20 /* Update the first system word of VMA flags setting bits, non-atomically.= */ -static inline void vma_flags_set_word(vma_flags_t *flags, unsigned long va= lue) +static __always_inline void vma_flags_set_word(vma_flags_t *flags, + unsigned long value) { unsigned long *bitmap =3D flags->__vma_flags; =20 @@ -1130,7 +1133,8 @@ static inline void vma_flags_set_word(vma_flags_t *fl= ags, unsigned long value) } =20 /* Update the first system word of VMA flags clearing bits, non-atomically= . */ -static inline void vma_flags_clear_word(vma_flags_t *flags, unsigned long = value) +static __always_inline void vma_flags_clear_word(vma_flags_t *flags, + unsigned long value) { unsigned long *bitmap =3D flags->__vma_flags; =20 diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/du= p.h index 9a3fb99416d3..70cabacdb9cc 100644 --- a/tools/testing/vma/include/dup.h +++ b/tools/testing/vma/include/dup.h @@ -765,7 +765,8 @@ static inline bool mm_flags_test(int flag, const struct= mm_struct *mm) * IMPORTANT: This does not overwrite bytes past the first system word. The * caller must account for this. */ -static inline void vma_flags_overwrite_word(vma_flags_t *flags, unsigned l= ong value) +static __always_inline void vma_flags_overwrite_word(vma_flags_t *flags, + unsigned long value) { *ACCESS_PRIVATE(flags, __vma_flags) =3D value; } @@ -776,7 +777,8 @@ static inline void vma_flags_overwrite_word(vma_flags_t= *flags, unsigned long va * IMPORTANT: This does not overwrite bytes past the first system word. The * caller must account for this. */ -static inline void vma_flags_overwrite_word_once(vma_flags_t *flags, unsig= ned long value) +static __always_inline void vma_flags_overwrite_word_once(vma_flags_t *fla= gs, + unsigned long value) { unsigned long *bitmap =3D ACCESS_PRIVATE(flags, __vma_flags); =20 @@ -784,7 +786,8 @@ static inline void vma_flags_overwrite_word_once(vma_fl= ags_t *flags, unsigned lo } =20 /* Update the first system word of VMA flags setting bits, non-atomically.= */ -static inline void vma_flags_set_word(vma_flags_t *flags, unsigned long va= lue) +static __always_inline void vma_flags_set_word(vma_flags_t *flags, + unsigned long value) { unsigned long *bitmap =3D ACCESS_PRIVATE(flags, __vma_flags); =20 @@ -792,7 +795,8 @@ static inline void vma_flags_set_word(vma_flags_t *flag= s, unsigned long value) } =20 /* Update the first system word of VMA flags clearing bits, non-atomically= . */ -static inline void vma_flags_clear_word(vma_flags_t *flags, unsigned long = value) +static __always_inline void vma_flags_clear_word(vma_flags_t *flags, + unsigned long value) { unsigned long *bitmap =3D ACCESS_PRIVATE(flags, __vma_flags); =20 @@ -1002,23 +1006,32 @@ static __always_inline bool vma_flags_same_mask(con= st vma_flags_t *flags, #define vma_flags_same(flags, ...) \ vma_flags_same_mask(flags, mk_vma_flags(__VA_ARGS__)) =20 -static inline bool vma_test_all_mask(const struct vm_area_struct *vma, - vma_flags_t flags) +static __always_inline bool vma_test(const struct vm_area_struct *vma, + vma_flag_t bit) { - return vma_flags_test_all_mask(&vma->flags, flags); + return vma_flags_test(&vma->flags, bit); } =20 -#define vma_test_all(vma, ...) \ - vma_test_all_mask(vma, mk_vma_flags(__VA_ARGS__)) +static __always_inline bool vma_test_any_mask(const struct vm_area_struct = *vma, + vma_flags_t flags) +{ + return vma_flags_test_any_mask(&vma->flags, flags); +} =20 -static inline bool is_shared_maywrite_vm_flags(vm_flags_t vm_flags) +#define vma_test_any(vma, ...) \ + vma_test_any_mask(vma, mk_vma_flags(__VA_ARGS__)) + +static __always_inline bool vma_test_all_mask(const struct vm_area_struct = *vma, + vma_flags_t flags) { - return (vm_flags & (VM_SHARED | VM_MAYWRITE)) =3D=3D - (VM_SHARED | VM_MAYWRITE); + return vma_flags_test_all_mask(&vma->flags, flags); } =20 -static inline void vma_set_flags_mask(struct vm_area_struct *vma, - vma_flags_t flags) +#define vma_test_all(vma, ...) \ + vma_test_all_mask(vma, mk_vma_flags(__VA_ARGS__)) + +static __always_inline void vma_set_flags_mask(struct vm_area_struct *vma, + vma_flags_t flags) { vma_flags_set_mask(&vma->flags, flags); } @@ -1032,8 +1045,8 @@ static __always_inline bool vma_desc_test(const struc= t vm_area_desc *desc, return vma_flags_test(&desc->vma_flags, bit); } =20 -static inline bool vma_desc_test_any_mask(const struct vm_area_desc *desc, - vma_flags_t flags) +static __always_inline bool vma_desc_test_any_mask(const struct vm_area_de= sc *desc, + vma_flags_t flags) { return vma_flags_test_any_mask(&desc->vma_flags, flags); } @@ -1041,7 +1054,7 @@ static inline bool vma_desc_test_any_mask(const struc= t vm_area_desc *desc, #define vma_desc_test_any(desc, ...) \ vma_desc_test_any_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 -static inline bool vma_desc_test_all_mask(const struct vm_area_desc *desc, +static __always_inline bool vma_desc_test_all_mask(const struct vm_area_de= sc *desc, vma_flags_t flags) { return vma_flags_test_all_mask(&desc->vma_flags, flags); @@ -1050,8 +1063,8 @@ static inline bool vma_desc_test_all_mask(const struc= t vm_area_desc *desc, #define vma_desc_test_all(desc, ...) \ vma_desc_test_all_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 -static inline void vma_desc_set_flags_mask(struct vm_area_desc *desc, - vma_flags_t flags) +static __always_inline void vma_desc_set_flags_mask(struct vm_area_desc *d= esc, + vma_flags_t flags) { vma_flags_set_mask(&desc->vma_flags, flags); } @@ -1059,8 +1072,8 @@ static inline void vma_desc_set_flags_mask(struct vm_= area_desc *desc, #define vma_desc_set_flags(desc, ...) \ vma_desc_set_flags_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 -static inline void vma_desc_clear_flags_mask(struct vm_area_desc *desc, - vma_flags_t flags) +static __always_inline void vma_desc_clear_flags_mask(struct vm_area_desc = *desc, + vma_flags_t flags) { vma_flags_clear_mask(&desc->vma_flags, flags); } @@ -1068,6 +1081,12 @@ static inline void vma_desc_clear_flags_mask(struct = vm_area_desc *desc, #define vma_desc_clear_flags(desc, ...) \ vma_desc_clear_flags_mask(desc, mk_vma_flags(__VA_ARGS__)) =20 +static inline bool is_shared_maywrite_vm_flags(vm_flags_t vm_flags) +{ + return (vm_flags & (VM_SHARED | VM_MAYWRITE)) =3D=3D + (VM_SHARED | VM_MAYWRITE); +} + static inline bool is_shared_maywrite(const vma_flags_t *flags) { return vma_flags_test_all(flags, VMA_SHARED_BIT, VMA_MAYWRITE_BIT); --=20 2.53.0