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/vma: add vma_flags_empty(), vma_flags_and(), vma_flags_diff_pair()

Patch series "mm/vma: convert vm_flags_t to vma_flags_t in vma code", v4.

This series converts a lot of the existing use of the legacy vm_flags_t
data type to the new vma_flags_t type which replaces it.

In order to do so it adds a number of additional helpers:

* vma_flags_empty() - Determines whether a vma_flags_t value has no bits
set.

* vma_flags_and() - Performs a bitwise AND between two vma_flags_t values.

* vma_flags_diff_pair() - Determines which flags are not shared between a
pair of VMA flags (typically non-constant values)

* append_vma_flags() - Similar to mk_vma_flags(), but allows a vma_flags_t
value to be specified (typically a constant value) which will be copied
and appended to to create a new vma_flags_t value, with additional flags
specified to append to it.

* vma_flags_same() - Determines if a vma_flags_t value is exactly equal to
a set of VMA flags.

* vma_flags_same_mask() - Determines if a vma_flags_t value is eactly equal
to another vma_flags_t value (typically constant).

* vma_flags_same_pair() - Determines if a pair of vma_flags_t values are
exactly equal to one another (typically both non-constant).

* vma_flags_to_legacy() - Converts a vma_flags_t value to a vm_flags_t
value, used to enable more iterative introduction of the use of
vma_flags_t.

* legacy_to_vma_flags() - Converts a vm_flags_t value to a vma_flags-t
value, for the same purpose.

* vma_flags_test_single_mask() - Tests whether a vma_flags_t value contain
the single flag specified in an input vma_flags_t flag mask, or if that
flag mask is empty, is defined to return false. Useful for
config-predicated VMA flag mask defines.

* vma_test() - Tests whether a VMA's flags contain a specific singular VMA
flag.

* vma_test_any() - Tests whether a VMA's flags contain any of a set of VMA
flags.

* vma_test_any_mask() - Tests whether a VMA's flags contain any of the
flags specified in another, typically constant, vma_flags_t value.

* vma_test_single_mask() - Tests whether a VMA's flags contain the single
flag specified in an input vma_flags_t flag mask, or if that flag mask is
empty, is defined to return false. Useful for config-predicated VMA flag
mask defines.

* vma_clear_flags() - Clears a specific set of VMA flags from a vma_flags_t
value.

* vma_clear_flags_mask() - Clears those flag set in a vma_flags_t value
(typically constant) from a (typically not constant) vma_flags_t value.

The series mostly focuses on the the VMA specific code, especially that
contained in mm/vma.c and mm/vma.h.

It updates both brk() and mmap() logic to utils vma_flags_t values as much
as is practiaclly possible at this point, changing surrounding logic to be
able to do so.

It also updates the vma_modify_xxx() functions where they interact with VMA
flags directly to use vm_flags_t values where possible.

There is extensive testing added in the VMA userland tests to assert that
all of these new VMA flag functions work correctly.


This patch (of 25):

Firstly, add the ability to determine if VMA flags are empty, that is no
flags are set in a vma_flags_t value.

Next, add the ability to obtain the equivalent of the bitwise and of two
vma_flags_t values, via vma_flags_and_mask().

Next, add the ability to obtain the difference between two sets of VMA
flags, that is the equivalent to the exclusive bitwise OR of the two sets
of flags, via vma_flags_diff_pair().

vma_flags_xxx_mask() typically operates on a pointer to a vma_flags_t
value, which is assumed to be an lvalue of some kind (such as a field in a
struct or a stack variable) and an rvalue of some kind (typically a
constant set of VMA flags obtained e.g. via mk_vma_flags() or
equivalent).

However vma_flags_diff_pair() is intended to operate on two lvalues, so
use the _pair() suffix to make this clear.

Finally, update VMA userland tests to add these helpers.

We also port bitmap_xor() and __bitmap_xor() to the tools/ headers and
source to allow the tests to work with vma_flags_diff_pair().

Link: https://lkml.kernel.org/r/cover.1774034900.git.ljs@kernel.org
Link: https://lkml.kernel.org/r/53ab55b7da91425775e42c03177498ad6de88ef4.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
6bc0987d 5ac9c7c2

+118 -11
+52 -10
include/linux/mm.h
··· 1049 1049 } 1050 1050 1051 1051 /* 1052 + * Helper macro which bitwise-or combines the specified input flags into a 1053 + * vma_flags_t bitmap value. E.g.: 1054 + * 1055 + * vma_flags_t flags = mk_vma_flags(VMA_IO_BIT, VMA_PFNMAP_BIT, 1056 + * VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT); 1057 + * 1058 + * The compiler cleverly optimises away all of the work and this ends up being 1059 + * equivalent to aggregating the values manually. 1060 + */ 1061 + #define mk_vma_flags(...) __mk_vma_flags(COUNT_ARGS(__VA_ARGS__), \ 1062 + (const vma_flag_t []){__VA_ARGS__}) 1063 + 1064 + /* 1052 1065 * Test whether a specific VMA flag is set, e.g.: 1053 1066 * 1054 1067 * if (vma_flags_test(flags, VMA_READ_BIT)) { ... } ··· 1075 1062 } 1076 1063 1077 1064 /* 1078 - * Helper macro which bitwise-or combines the specified input flags into a 1079 - * vma_flags_t bitmap value. E.g.: 1080 - * 1081 - * vma_flags_t flags = mk_vma_flags(VMA_IO_BIT, VMA_PFNMAP_BIT, 1082 - * VMA_DONTEXPAND_BIT, VMA_DONTDUMP_BIT); 1083 - * 1084 - * The compiler cleverly optimises away all of the work and this ends up being 1085 - * equivalent to aggregating the values manually. 1065 + * Obtain a set of VMA flags which contain the overlapping flags contained 1066 + * within flags and to_and. 1086 1067 */ 1087 - #define mk_vma_flags(...) __mk_vma_flags(COUNT_ARGS(__VA_ARGS__), \ 1088 - (const vma_flag_t []){__VA_ARGS__}) 1068 + static __always_inline vma_flags_t vma_flags_and_mask(const vma_flags_t *flags, 1069 + vma_flags_t to_and) 1070 + { 1071 + vma_flags_t dst; 1072 + unsigned long *bitmap_dst = dst.__vma_flags; 1073 + const unsigned long *bitmap = flags->__vma_flags; 1074 + const unsigned long *bitmap_to_and = to_and.__vma_flags; 1075 + 1076 + bitmap_and(bitmap_dst, bitmap, bitmap_to_and, NUM_VMA_FLAG_BITS); 1077 + return dst; 1078 + } 1079 + 1080 + /* 1081 + * Obtain a set of VMA flags which contains the specified overlapping flags, 1082 + * e.g.: 1083 + * 1084 + * vma_flags_t read_flags = vma_flags_and(&flags, VMA_READ_BIT, 1085 + * VMA_MAY_READ_BIT); 1086 + */ 1087 + #define vma_flags_and(flags, ...) \ 1088 + vma_flags_and_mask(flags, mk_vma_flags(__VA_ARGS__)) 1089 1089 1090 1090 /* Test each of to_test flags in flags, non-atomically. */ 1091 1091 static __always_inline bool vma_flags_test_any_mask(const vma_flags_t *flags, ··· 1171 1145 */ 1172 1146 #define vma_flags_clear(flags, ...) \ 1173 1147 vma_flags_clear_mask(flags, mk_vma_flags(__VA_ARGS__)) 1148 + 1149 + /* 1150 + * Obtain a VMA flags value containing those flags that are present in flags or 1151 + * flags_other but not in both. 1152 + */ 1153 + static __always_inline vma_flags_t vma_flags_diff_pair(const vma_flags_t *flags, 1154 + const vma_flags_t *flags_other) 1155 + { 1156 + vma_flags_t dst; 1157 + const unsigned long *bitmap_other = flags_other->__vma_flags; 1158 + const unsigned long *bitmap = flags->__vma_flags; 1159 + unsigned long *bitmap_dst = dst.__vma_flags; 1160 + 1161 + bitmap_xor(bitmap_dst, bitmap, bitmap_other, NUM_VMA_FLAG_BITS); 1162 + return dst; 1163 + } 1174 1164 1175 1165 /* 1176 1166 * Helper to test that ALL specified flags are set in a VMA.
+8
include/linux/mm_types.h
··· 870 870 871 871 #define EMPTY_VMA_FLAGS ((vma_flags_t){ }) 872 872 873 + /* Are no flags set in the specified VMA flags? */ 874 + static __always_inline bool vma_flags_empty(const vma_flags_t *flags) 875 + { 876 + const unsigned long *bitmap = flags->__vma_flags; 877 + 878 + return bitmap_empty(bitmap, NUM_VMA_FLAG_BITS); 879 + } 880 + 873 881 /* 874 882 * Describes a VMA that is about to be mmap()'ed. Drivers may choose to 875 883 * manipulate mutable fields which will cause those fields to be updated in the
+13
tools/include/linux/bitmap.h
··· 28 28 const unsigned long *bitmap2, unsigned int nbits); 29 29 bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, 30 30 const unsigned long *bitmap2, unsigned int nbits); 31 + void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, 32 + const unsigned long *bitmap2, unsigned int nbits); 31 33 32 34 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) 33 35 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) ··· 211 209 else 212 210 __bitmap_clear(map, start, nbits); 213 211 } 212 + 213 + static __always_inline 214 + void bitmap_xor(unsigned long *dst, const unsigned long *src1, 215 + const unsigned long *src2, unsigned int nbits) 216 + { 217 + if (small_const_nbits(nbits)) 218 + *dst = *src1 ^ *src2; 219 + else 220 + __bitmap_xor(dst, src1, src2, nbits); 221 + } 222 + 214 223 #endif /* _TOOLS_LINUX_BITMAP_H */
+10
tools/lib/bitmap.c
··· 169 169 return false; 170 170 return true; 171 171 } 172 + 173 + void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, 174 + const unsigned long *bitmap2, unsigned int bits) 175 + { 176 + unsigned int k; 177 + unsigned int nr = BITS_TO_LONGS(bits); 178 + 179 + for (k = 0; k < nr; k++) 180 + dst[k] = bitmap1[k] ^ bitmap2[k]; 181 + }
+35 -1
tools/testing/vma/include/dup.h
··· 422 422 #define MAPCOUNT_ELF_CORE_MARGIN (5) 423 423 #define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN) 424 424 425 + static __always_inline bool vma_flags_empty(const vma_flags_t *flags) 426 + { 427 + const unsigned long *bitmap = flags->__vma_flags; 428 + 429 + return bitmap_empty(bitmap, NUM_VMA_FLAG_BITS); 430 + } 431 + 425 432 /* What action should be taken after an .mmap_prepare call is complete? */ 426 433 enum mmap_action_type { 427 434 MMAP_NOTHING, /* Mapping is complete, no further action. */ ··· 862 855 return test_bit((__force int)bit, bitmap); 863 856 } 864 857 858 + static __always_inline vma_flags_t vma_flags_and_mask(const vma_flags_t *flags, 859 + vma_flags_t to_and) 860 + { 861 + vma_flags_t dst; 862 + unsigned long *bitmap_dst = dst.__vma_flags; 863 + const unsigned long *bitmap = flags->__vma_flags; 864 + const unsigned long *bitmap_to_and = to_and.__vma_flags; 865 + 866 + bitmap_and(bitmap_dst, bitmap, bitmap_to_and, NUM_VMA_FLAG_BITS); 867 + return dst; 868 + } 869 + 870 + #define vma_flags_and(flags, ...) \ 871 + vma_flags_and_mask(flags, mk_vma_flags(__VA_ARGS__)) 872 + 865 873 static __always_inline bool vma_flags_test_any_mask(const vma_flags_t *flags, 866 874 vma_flags_t to_test) 867 875 { ··· 923 901 #define vma_flags_clear(flags, ...) \ 924 902 vma_flags_clear_mask(flags, mk_vma_flags(__VA_ARGS__)) 925 903 904 + static __always_inline vma_flags_t vma_flags_diff_pair(const vma_flags_t *flags, 905 + const vma_flags_t *flags_other) 906 + { 907 + vma_flags_t dst; 908 + const unsigned long *bitmap_other = flags_other->__vma_flags; 909 + const unsigned long *bitmap = flags->__vma_flags; 910 + unsigned long *bitmap_dst = dst.__vma_flags; 911 + 912 + bitmap_xor(bitmap_dst, bitmap, bitmap_other, NUM_VMA_FLAG_BITS); 913 + return dst; 914 + } 915 + 926 916 static inline bool vma_test_all_mask(const struct vm_area_struct *vma, 927 - vma_flags_t flags) 917 + vma_flags_t flags) 928 918 { 929 919 return vma_flags_test_all_mask(&vma->flags, flags); 930 920 }