mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git
synced 2026-10-28 21:02:43 +01:00
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/[email protected] Link: https://lkml.kernel.org/r/53ab55b7da91425775e42c03177498ad6de88ef4.1774034900.git.ljs@kernel.org Signed-off-by: Lorenzo Stoakes (Oracle) <[email protected]> Acked-by: Vlastimil Babka (SUSE) <[email protected]> Cc: Albert Ou <[email protected]> Cc: Alexander Gordeev <[email protected]> Cc: Alexandre Ghiti <[email protected]> Cc: Al Viro <[email protected]> Cc: Anton Ivanov <[email protected]> Cc: "Borislav Petkov (AMD)" <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Chengming Zhou <[email protected]> Cc: Christian Borntraeger <[email protected]> Cc: Christian Brauner <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Dinh Nguyen <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Huacai Chen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jan Kara <[email protected]> Cc: Jann Horn <[email protected]> Cc: Johannes Berg <[email protected]> Cc: Kees Cook <[email protected]> Cc: Liam Howlett <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Nicholas Piggin <[email protected]> Cc: Ondrej Mosnacek <[email protected]> Cc: Palmer Dabbelt <[email protected]> Cc: Paul Moore <[email protected]> Cc: Pedro Falcato <[email protected]> Cc: Richard Weinberger <[email protected]> Cc: Russell King <[email protected]> Cc: Stephen Smalley <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: Sven Schnelle <[email protected]> Cc: Thomas Bogendoerfer <[email protected]> Cc: Vasily Gorbik <[email protected]> Cc: Vineet Gupta <[email protected]> Cc: WANG Xuerui <[email protected]> Cc: Will Deacon <[email protected]> Cc: xu xin <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
182 lines
4.4 KiB
C
182 lines
4.4 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* From lib/bitmap.c
|
|
* Helper functions for bitmap.h.
|
|
*/
|
|
#include <linux/bitmap.h>
|
|
|
|
unsigned int __bitmap_weight(const unsigned long *bitmap, int bits)
|
|
{
|
|
unsigned int k, w = 0, lim = bits/BITS_PER_LONG;
|
|
|
|
for (k = 0; k < lim; k++)
|
|
w += hweight_long(bitmap[k]);
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
|
|
|
|
return w;
|
|
}
|
|
|
|
void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, int bits)
|
|
{
|
|
int k;
|
|
int nr = BITS_TO_LONGS(bits);
|
|
|
|
for (k = 0; k < nr; k++)
|
|
dst[k] = bitmap1[k] | bitmap2[k];
|
|
}
|
|
|
|
size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
|
|
char *buf, size_t size)
|
|
{
|
|
/* current bit is 'cur', most recently seen range is [rbot, rtop] */
|
|
unsigned int cur, rbot, rtop;
|
|
bool first = true;
|
|
size_t ret = 0;
|
|
|
|
rbot = cur = find_first_bit(bitmap, nbits);
|
|
while (cur < nbits) {
|
|
rtop = cur;
|
|
cur = find_next_bit(bitmap, nbits, cur + 1);
|
|
if (cur < nbits && cur <= rtop + 1)
|
|
continue;
|
|
|
|
if (!first)
|
|
ret += scnprintf(buf + ret, size - ret, ",");
|
|
|
|
first = false;
|
|
|
|
ret += scnprintf(buf + ret, size - ret, "%d", rbot);
|
|
if (rbot < rtop)
|
|
ret += scnprintf(buf + ret, size - ret, "-%d", rtop);
|
|
|
|
rbot = cur;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int lim = bits/BITS_PER_LONG;
|
|
unsigned long result = 0;
|
|
|
|
for (k = 0; k < lim; k++)
|
|
result |= (dst[k] = bitmap1[k] & bitmap2[k]);
|
|
if (bits % BITS_PER_LONG)
|
|
result |= (dst[k] = bitmap1[k] & bitmap2[k] &
|
|
BITMAP_LAST_WORD_MASK(bits));
|
|
return result != 0;
|
|
}
|
|
|
|
bool __bitmap_equal(const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
if (bitmap1[k] != bitmap2[k])
|
|
return false;
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool __bitmap_intersects(const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
if (bitmap1[k] & bitmap2[k])
|
|
return true;
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
void __bitmap_set(unsigned long *map, unsigned int start, int len)
|
|
{
|
|
unsigned long *p = map + BIT_WORD(start);
|
|
const unsigned int size = start + len;
|
|
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
|
|
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
|
|
|
|
while (len - bits_to_set >= 0) {
|
|
*p |= mask_to_set;
|
|
len -= bits_to_set;
|
|
bits_to_set = BITS_PER_LONG;
|
|
mask_to_set = ~0UL;
|
|
p++;
|
|
}
|
|
if (len) {
|
|
mask_to_set &= BITMAP_LAST_WORD_MASK(size);
|
|
*p |= mask_to_set;
|
|
}
|
|
}
|
|
|
|
void __bitmap_clear(unsigned long *map, unsigned int start, int len)
|
|
{
|
|
unsigned long *p = map + BIT_WORD(start);
|
|
const unsigned int size = start + len;
|
|
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
|
|
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
|
|
|
|
while (len - bits_to_clear >= 0) {
|
|
*p &= ~mask_to_clear;
|
|
len -= bits_to_clear;
|
|
bits_to_clear = BITS_PER_LONG;
|
|
mask_to_clear = ~0UL;
|
|
p++;
|
|
}
|
|
if (len) {
|
|
mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
|
|
*p &= ~mask_to_clear;
|
|
}
|
|
}
|
|
|
|
bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int lim = bits/BITS_PER_LONG;
|
|
unsigned long result = 0;
|
|
|
|
for (k = 0; k < lim; k++)
|
|
result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
|
|
if (bits % BITS_PER_LONG)
|
|
result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
|
|
BITMAP_LAST_WORD_MASK(bits));
|
|
return result != 0;
|
|
}
|
|
|
|
bool __bitmap_subset(const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k, lim = bits/BITS_PER_LONG;
|
|
for (k = 0; k < lim; ++k)
|
|
if (bitmap1[k] & ~bitmap2[k])
|
|
return false;
|
|
|
|
if (bits % BITS_PER_LONG)
|
|
if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
|
const unsigned long *bitmap2, unsigned int bits)
|
|
{
|
|
unsigned int k;
|
|
unsigned int nr = BITS_TO_LONGS(bits);
|
|
|
|
for (k = 0; k < nr; k++)
|
|
dst[k] = bitmap1[k] ^ bitmap2[k];
|
|
}
|