bitmap updates for v6.20

- more rust helpers (Alice);
  - more bitops tests (Ryota);
  - FIND_NTH_BIT() uninitialized variable fix (Lee Yongjun);
  - random cleanups (Andy, H. Peter).
 -----BEGIN PGP SIGNATURE-----
 
 iQGzBAABCgAdFiEEi8GdvG6xMhdgpu/4sUSA/TofvsgFAmmJIqMACgkQsUSA/Tof
 vsj/uwv/WIv9Wa3oTm3qzkIyaxeowIQE+DeDduporSoO8fnVfEYHjPuihMy3J5Gv
 x6iNUq9n8fMhmg/qHHyKwb/Ow8Ob11re9MZWS6a02wlm8Het0W9hKEk1z7qauFsI
 Vqq3rlMO3wYGaSPmlnh7rn/NHByrJ49ZrJcoX3he0Ov5MKp1w+dP3Czee4HGTb3A
 SM0cf4iCR3/Jj3flTsMghZgqZKNhsIe+ejX++dFPhmv82FHbIBcwUfN6CM27OGdy
 M7GAro1xIaoTwQVKCO2AycLAu8j+Rx9s+mcGuIAWynDo6YG1tVetADZQ+nRJG+x9
 WdUDZwBDyEZlx/8BGnS56wevYxXEfC0lMTTgBqsYlCtLToyeE8G6NUVCLzEJAQCJ
 iWhcsenQVIiEHtYBZI/o2PmEp1Puzk1jI0+uFib1jE5Llf3+FrfMQGOiN2TBStEa
 zVIOipr+L9EaBbJdiKCV1jTl/MoDuCdRPLyby8egxPS+rk4RugvDsGc31XN48118
 OHIJO2M/
 =WnLV
 -----END PGP SIGNATURE-----

Merge tag 'bitmap-for-6.20' of https://github.com/norov/linux

Pull bitmap updates from Yury Norov:

 - more rust helpers (Alice)

 - more bitops tests (Ryota)

 - FIND_NTH_BIT() uninitialized variable fix (Lee Yongjun)

 - random cleanups (Andy, H. Peter)

* tag 'bitmap-for-6.20' of https://github.com/norov/linux:
  lib/tests: extend KUnit test for bitops with more cases
  bitops: Add more files to the MAINTAINERS
  lib/find_bit: fix uninitialized variable use in FIND_NTH_BIT
  lib/tests: add KUnit test for bitops
  rust: cpumask: add __rust_helper to helpers
  rust: bitops: add __rust_helper to helpers
  rust: bitmap: add __rust_helper to helpers
  linux/bitfield.h: replace __auto_type with auto
This commit is contained in:
Linus Torvalds 2026-02-10 11:39:45 -08:00
commit f144367d01
9 changed files with 243 additions and 3 deletions

View file

@ -4472,8 +4472,10 @@ F: arch/*/lib/bitops.c
F: include/asm-generic/bitops
F: include/asm-generic/bitops.h
F: include/linux/bitops.h
F: include/linux/count_zeros.h
F: lib/hweight.c
F: lib/test_bitops.c
F: lib/tests/bitops_kunit.c
F: tools/*/bitops*
BITOPS API BINDINGS [RUST]

View file

@ -8,6 +8,7 @@
#define _LINUX_BITFIELD_H
#include <linux/build_bug.h>
#include <linux/compiler.h>
#include <linux/typecheck.h>
#include <asm/byteorder.h>
@ -243,7 +244,7 @@ __MAKE_OP(64)
#define __field_prep(mask, val) \
({ \
__auto_type __mask = (mask); \
auto __mask = (mask); \
typeof(__mask) __val = (val); \
unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
__ffs(__mask) : __ffs64(__mask); \
@ -252,7 +253,7 @@ __MAKE_OP(64)
#define __field_get(mask, reg) \
({ \
__auto_type __mask = (mask); \
auto __mask = (mask); \
typeof(__mask) __reg = (reg); \
unsigned int __shift = BITS_PER_TYPE(__mask) <= 32 ? \
__ffs(__mask) : __ffs64(__mask); \

View file

@ -2647,6 +2647,19 @@ config TEST_SYSCTL
If unsure, say N.
config BITOPS_KUNIT
tristate "KUnit test for bitops" if !KUNIT_ALL_TESTS
depends on KUNIT
default KUNIT_ALL_TESTS
help
This option enables the KUnit test for the bitops library
which provides functions for bit operations.
Note that this is derived from the original test_bitops module.
For micro-benchmarks and compiler warning checks, enable TEST_BITOPS.
If unsure, say N.
config BITFIELD_KUNIT
tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS
depends on KUNIT

View file

@ -71,7 +71,7 @@ out: \
#define FIND_NTH_BIT(FETCH, size, num) \
({ \
unsigned long sz = (size), nr = (num), idx, w, tmp; \
unsigned long sz = (size), nr = (num), idx, w, tmp = 0; \
\
for (idx = 0; (idx + 1) * BITS_PER_LONG <= sz; idx++) { \
if (idx * BITS_PER_LONG + nr >= sz) \

View file

@ -5,6 +5,7 @@
# KUnit tests
CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN)
obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o
obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o
obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
obj-$(CONFIG_BITS_TEST) += test_bits.o
obj-$(CONFIG_BLACKHOLE_DEV_KUNIT_TEST) += blackhole_dev_kunit.o

205
lib/tests/bitops_kunit.c Normal file
View file

@ -0,0 +1,205 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2020 Intel Corporation
* Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com>
*/
#include <linux/bitops.h>
#include <linux/module.h>
#include <kunit/test.h>
/* use an enum because that's the most common BITMAP usage */
enum bitops_fun {
BITOPS_4 = 4,
BITOPS_7 = 7,
BITOPS_11 = 11,
BITOPS_31 = 31,
BITOPS_88 = 88,
BITOPS_LENGTH = 256
};
struct bitops_test_case {
const char *str;
const long nr;
};
static struct bitops_test_case bitops_cases[] = {
{
.str = "BITOPS_4",
.nr = BITOPS_4,
},
{
.str = "BITOPS_7",
.nr = BITOPS_7,
},
{
.str = "BITOPS_11",
.nr = BITOPS_11,
},
{
.str = "BITOPS_31",
.nr = BITOPS_31,
},
{
.str = "BITOPS_88",
.nr = BITOPS_88,
},
};
KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);
static void test_set_bit_clear_bit(struct kunit *test)
{
const struct bitops_test_case *params = test->param_value;
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
int bit_set;
bitmap_zero(bitmap, BITOPS_LENGTH);
set_bit(params->nr, bitmap);
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
clear_bit(params->nr, bitmap);
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}
static void test_change_bit(struct kunit *test)
{
const struct bitops_test_case *params = test->param_value;
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
int bit_set;
bitmap_zero(bitmap, BITOPS_LENGTH);
change_bit(params->nr, bitmap);
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
change_bit(params->nr, bitmap);
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}
static void test_test_and_set_bit_test_and_clear_bit(struct kunit *test)
{
const struct bitops_test_case *params = test->param_value;
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
int bit_set;
bitmap_zero(bitmap, BITOPS_LENGTH);
KUNIT_EXPECT_FALSE(test, test_and_set_bit(params->nr, bitmap));
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
KUNIT_EXPECT_TRUE(test, test_and_set_bit(params->nr, bitmap));
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
KUNIT_EXPECT_TRUE(test, test_and_clear_bit(params->nr, bitmap));
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
KUNIT_EXPECT_FALSE(test, test_and_clear_bit(params->nr, bitmap));
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}
static void test_test_and_change_bit(struct kunit *test)
{
const struct bitops_test_case *params = test->param_value;
DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
int bit_set;
bitmap_zero(bitmap, BITOPS_LENGTH);
KUNIT_EXPECT_FALSE(test, test_and_change_bit(params->nr, bitmap));
KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));
KUNIT_EXPECT_TRUE(test, test_and_change_bit(params->nr, bitmap));
KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));
bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}
struct order_test_case {
const char *str;
const unsigned int count;
const int expected;
};
static struct order_test_case order_test_cases[] = {
{"0x00000003", 0x00000003, 2},
{"0x00000004", 0x00000004, 2},
{"0x00001fff", 0x00001fff, 13},
{"0x00002000", 0x00002000, 13},
{"0x50000000", 0x50000000, 31},
{"0x80000000", 0x80000000, 31},
{"0x80003000", 0x80003000, 32},
};
KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str);
static void test_get_count_order(struct kunit *test)
{
const struct order_test_case *params = test->param_value;
KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected);
KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
}
#ifdef CONFIG_64BIT
struct order_long_test_case {
const char *str;
const unsigned long count;
const int expected;
};
static struct order_long_test_case order_long_test_cases[] = {
{"0x0000000300000000", 0x0000000300000000, 34},
{"0x0000000400000000", 0x0000000400000000, 34},
{"0x00001fff00000000", 0x00001fff00000000, 45},
{"0x0000200000000000", 0x0000200000000000, 45},
{"0x5000000000000000", 0x5000000000000000, 63},
{"0x8000000000000000", 0x8000000000000000, 63},
{"0x8000300000000000", 0x8000300000000000, 64},
};
KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str);
static void test_get_count_order_long(struct kunit *test)
{
const struct order_long_test_case *params = test->param_value;
KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected);
}
#endif
static struct kunit_case bitops_test_cases[] = {
KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params),
KUNIT_CASE_PARAM(test_change_bit, bitops_gen_params),
KUNIT_CASE_PARAM(test_test_and_set_bit_test_and_clear_bit, bitops_gen_params),
KUNIT_CASE_PARAM(test_test_and_change_bit, bitops_gen_params),
KUNIT_CASE_PARAM(test_get_count_order, order_gen_params),
#ifdef CONFIG_64BIT
KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params),
#endif
{},
};
static struct kunit_suite bitops_test_suite = {
.name = "bitops",
.test_cases = bitops_test_cases,
};
kunit_test_suite(bitops_test_suite);
MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>");
MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>");
MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Bit testing module");

View file

@ -2,6 +2,7 @@
#include <linux/bitmap.h>
__rust_helper
void rust_helper_bitmap_copy_and_extend(unsigned long *to, const unsigned long *from,
unsigned int count, unsigned int size)
{

View file

@ -3,21 +3,25 @@
#include <linux/bitops.h>
#include <linux/find.h>
__rust_helper
void rust_helper___set_bit(unsigned long nr, unsigned long *addr)
{
__set_bit(nr, addr);
}
__rust_helper
void rust_helper___clear_bit(unsigned long nr, unsigned long *addr)
{
__clear_bit(nr, addr);
}
__rust_helper
void rust_helper_set_bit(unsigned long nr, volatile unsigned long *addr)
{
set_bit(nr, addr);
}
__rust_helper
void rust_helper_clear_bit(unsigned long nr, volatile unsigned long *addr)
{
clear_bit(nr, addr);

View file

@ -2,67 +2,80 @@
#include <linux/cpumask.h>
__rust_helper
void rust_helper_cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
{
cpumask_set_cpu(cpu, dstp);
}
__rust_helper
void rust_helper___cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
{
__cpumask_set_cpu(cpu, dstp);
}
__rust_helper
void rust_helper_cpumask_clear_cpu(int cpu, struct cpumask *dstp)
{
cpumask_clear_cpu(cpu, dstp);
}
__rust_helper
void rust_helper___cpumask_clear_cpu(int cpu, struct cpumask *dstp)
{
__cpumask_clear_cpu(cpu, dstp);
}
__rust_helper
bool rust_helper_cpumask_test_cpu(int cpu, struct cpumask *srcp)
{
return cpumask_test_cpu(cpu, srcp);
}
__rust_helper
void rust_helper_cpumask_setall(struct cpumask *dstp)
{
cpumask_setall(dstp);
}
__rust_helper
bool rust_helper_cpumask_empty(struct cpumask *srcp)
{
return cpumask_empty(srcp);
}
__rust_helper
bool rust_helper_cpumask_full(struct cpumask *srcp)
{
return cpumask_full(srcp);
}
__rust_helper
unsigned int rust_helper_cpumask_weight(struct cpumask *srcp)
{
return cpumask_weight(srcp);
}
__rust_helper
void rust_helper_cpumask_copy(struct cpumask *dstp, const struct cpumask *srcp)
{
cpumask_copy(dstp, srcp);
}
__rust_helper
bool rust_helper_alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
{
return alloc_cpumask_var(mask, flags);
}
__rust_helper
bool rust_helper_zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
{
return zalloc_cpumask_var(mask, flags);
}
#ifndef CONFIG_CPUMASK_OFFSTACK
__rust_helper
void rust_helper_free_cpumask_var(cpumask_var_t mask)
{
free_cpumask_var(mask);