Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: introduce vma_flags_count() and vma[_flags]_test_single_mask()

vma_flags_count() determines how many bits are set in VMA flags, using
bitmap_weight().

vma_flags_test_single_mask() determines if a vma_flags_t set of flags
contains a single flag specified as another vma_flags_t value, or if the
sought flag mask is empty, it is defined to return false.

This is useful when we want to declare a VMA flag as optionally a single
flag in a mask or empty depending on kernel configuration.

This allows us to have VM_NONE-like semantics when checking whether the
flag is set.

In a subsequent patch, we introduce the use of VMA_DROPPABLE of type
vma_flags_t using precisely these semantics.

It would be actively confusing to use vma_flags_test_any_single_mask() for
this (and vma_flags_test_all_mask() is not correct to use here, as it
trivially returns true when tested against an empty vma flags mask).

We introduce vma_flags_count() to be able to assert that the compared flag
mask is singular or empty, checked when CONFIG_DEBUG_VM is enabled.

Also update the VMA tests as part of this change.

Link: https://lkml.kernel.org/r/cd778dd02b9f2a01eb54d25a49dea8ec2ddf7753.1774034900.git.ljs@kernel.org
Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Acked-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Anton Ivanov <anton.ivanov@cambridgegreys.com>
Cc: "Borislav Petkov (AMD)" <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Chengming Zhou <chengming.zhou@linux.dev>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Kees Cook <kees@kernel.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Ondrej Mosnacek <omosnace@redhat.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Moore <paul@paul-moore.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: Richard Weinberger <richard@nod.at>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vineet Gupta <vgupta@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>
Cc: Will Deacon <will@kernel.org>
Cc: xu xin <xu.xin16@zte.com.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes (Oracle) and committed by
Andrew Morton
e79d1c50 63cdb667

+73 -6
+46
include/linux/mm.h
··· 1078 1078 #define append_vma_flags(flags, ...) __mk_vma_flags(flags, \ 1079 1079 COUNT_ARGS(__VA_ARGS__), (const vma_flag_t []){__VA_ARGS__}) 1080 1080 1081 + /* Calculates the number of set bits in the specified VMA flags. */ 1082 + static __always_inline int vma_flags_count(const vma_flags_t *flags) 1083 + { 1084 + const unsigned long *bitmap = flags->__vma_flags; 1085 + 1086 + return bitmap_weight(bitmap, NUM_VMA_FLAG_BITS); 1087 + } 1088 + 1081 1089 /* 1082 1090 * Test whether a specific VMA flag is set, e.g.: 1083 1091 * ··· 1160 1152 */ 1161 1153 #define vma_flags_test_all(flags, ...) \ 1162 1154 vma_flags_test_all_mask(flags, mk_vma_flags(__VA_ARGS__)) 1155 + 1156 + /* 1157 + * Helper to test that a flag mask of type vma_flags_t has a SINGLE flag set 1158 + * (returning false if flagmask has no flags set). 1159 + * 1160 + * This is defined to make the semantics clearer when testing an optionally 1161 + * defined VMA flags mask, e.g.: 1162 + * 1163 + * if (vma_flags_test_single_mask(&flags, VMA_DROPPABLE)) { ... } 1164 + * 1165 + * When VMA_DROPPABLE is defined if available, or set to EMPTY_VMA_FLAGS 1166 + * otherwise. 1167 + */ 1168 + static __always_inline bool vma_flags_test_single_mask(const vma_flags_t *flags, 1169 + vma_flags_t flagmask) 1170 + { 1171 + VM_WARN_ON_ONCE(vma_flags_count(&flagmask) > 1); 1172 + 1173 + return vma_flags_test_any_mask(flags, flagmask); 1174 + } 1163 1175 1164 1176 /* Set each of the to_set flags in flags, non-atomically. */ 1165 1177 static __always_inline void vma_flags_set_mask(vma_flags_t *flags, ··· 1308 1280 */ 1309 1281 #define vma_test_all(vma, ...) \ 1310 1282 vma_test_all_mask(vma, mk_vma_flags(__VA_ARGS__)) 1283 + 1284 + /* 1285 + * Helper to test that a flag mask of type vma_flags_t has a SINGLE flag set 1286 + * (returning false if flagmask has no flags set). 1287 + * 1288 + * This is useful when a flag needs to be either defined or not depending upon 1289 + * kernel configuration, e.g.: 1290 + * 1291 + * if (vma_test_single_mask(vma, VMA_DROPPABLE)) { ... } 1292 + * 1293 + * When VMA_DROPPABLE is defined if available, or set to EMPTY_VMA_FLAGS 1294 + * otherwise. 1295 + */ 1296 + static __always_inline bool 1297 + vma_test_single_mask(const struct vm_area_struct *vma, vma_flags_t flagmask) 1298 + { 1299 + return vma_flags_test_single_mask(&vma->flags, flagmask); 1300 + } 1311 1301 1312 1302 /* 1313 1303 * Helper to set all VMA flags in a VMA.
-6
tools/testing/vma/include/custom.h
··· 15 15 #define dac_mmap_min_addr 0UL 16 16 #endif 17 17 18 - #define VM_WARN_ON(_expr) (WARN_ON(_expr)) 19 - #define VM_WARN_ON_ONCE(_expr) (WARN_ON_ONCE(_expr)) 20 - #define VM_WARN_ON_VMG(_expr, _vmg) (WARN_ON(_expr)) 21 - #define VM_BUG_ON(_expr) (BUG_ON(_expr)) 22 - #define VM_BUG_ON_VMA(_expr, _vma) (BUG_ON(_expr)) 23 - 24 18 #define TASK_SIZE ((1ul << 47)-PAGE_SIZE) 25 19 26 20 /*
+21
tools/testing/vma/include/dup.h
··· 905 905 #define append_vma_flags(flags, ...) __mk_vma_flags(flags, \ 906 906 COUNT_ARGS(__VA_ARGS__), (const vma_flag_t []){__VA_ARGS__}) 907 907 908 + static __always_inline int vma_flags_count(const vma_flags_t *flags) 909 + { 910 + const unsigned long *bitmap = flags->__vma_flags; 911 + 912 + return bitmap_weight(bitmap, NUM_VMA_FLAG_BITS); 913 + } 914 + 908 915 static __always_inline bool vma_flags_test(const vma_flags_t *flags, 909 916 vma_flag_t bit) 910 917 { ··· 958 951 959 952 #define vma_flags_test_all(flags, ...) \ 960 953 vma_flags_test_all_mask(flags, mk_vma_flags(__VA_ARGS__)) 954 + 955 + static __always_inline bool vma_flags_test_single_mask(const vma_flags_t *flags, 956 + vma_flags_t flagmask) 957 + { 958 + VM_WARN_ON_ONCE(vma_flags_count(&flagmask) > 1); 959 + 960 + return vma_flags_test_any_mask(flags, flagmask); 961 + } 961 962 962 963 static __always_inline void vma_flags_set_mask(vma_flags_t *flags, vma_flags_t to_set) 963 964 { ··· 1045 1030 1046 1031 #define vma_test_all(vma, ...) \ 1047 1032 vma_test_all_mask(vma, mk_vma_flags(__VA_ARGS__)) 1033 + 1034 + static __always_inline bool 1035 + vma_test_single_mask(const struct vm_area_struct *vma, vma_flags_t flagmask) 1036 + { 1037 + return vma_flags_test_single_mask(&vma->flags, flagmask); 1038 + } 1048 1039 1049 1040 static __always_inline void vma_set_flags_mask(struct vm_area_struct *vma, 1050 1041 vma_flags_t flags)
+6
tools/testing/vma/vma_internal.h
··· 51 51 typedef struct pgprot { pgprotval_t pgprot; } pgprot_t; 52 52 typedef __bitwise unsigned int vm_fault_t; 53 53 54 + #define VM_WARN_ON(_expr) (WARN_ON(_expr)) 55 + #define VM_WARN_ON_ONCE(_expr) (WARN_ON_ONCE(_expr)) 56 + #define VM_WARN_ON_VMG(_expr, _vmg) (WARN_ON(_expr)) 57 + #define VM_BUG_ON(_expr) (BUG_ON(_expr)) 58 + #define VM_BUG_ON_VMA(_expr, _vma) (BUG_ON(_expr)) 59 + 54 60 #include "include/stubs.h" 55 61 #include "include/dup.h" 56 62 #include "include/custom.h"