From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Fri, 23 Oct 2026 19:38:14 -0400 Subject: [PATCH] refactor(lib): separate random bit search --- lib/Makefile | 5 +++-- lib/find_bit.c | 24 ------------------------ lib/find_random_bit.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 lib/find_random_bit.c diff --git a/lib/Makefile b/lib/Makefile index f9c55745e19c8e19a8bdc20c3de97f869d4f77a5..6d30dd4666661c3167cb442c1f6df26d828f1f38 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -20,6 +20,7 @@ KCOV_INSTRUMENT_debugobjects.o := n KCOV_INSTRUMENT_dynamic_debug.o := n KCOV_INSTRUMENT_fault-inject.o := n KCOV_INSTRUMENT_find_bit.o := n +KCOV_INSTRUMENT_find_random_bit.o := n # string.o implements standard library functions like memset/memcpy etc. # Use -ffreestanding to ensure that the compiler does not try to "optimize" @@ -56,8 +57,8 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \ bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \ scatterlist-mapping.o scatterlist-iov.o \ list_sort.o uuid.o iov_iter.o clz_ctz.o \ - bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \ - percpu-refcount.o rhashtable.o base64.o \ + bsearch.o find_bit.o find_random_bit.o llist.o lwq.o \ + memweight.o kfifo.o percpu-refcount.o rhashtable.o base64.o \ once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \ generic-radix-tree.o bitmap-str.o obj-y += string_helpers.o diff --git a/lib/find_bit.c b/lib/find_bit.c index 5ac52dfce73006660710a6fea5c761745b41ca49..ee6662cf4b191dd5c41f6be727f44e6f1db5546d 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -18,7 +18,6 @@ #include #include #include -#include /* * Common helper for find_bit() function family @@ -285,26 +284,3 @@ EXPORT_SYMBOL(_find_next_bit_le); #endif #endif /* __BIG_ENDIAN */ - -/** - * find_random_bit - find a set bit at random position - * @addr: The address to base the search on - * @size: The bitmap size in bits - * - * Returns: a position of a random set bit; >= @size otherwise - */ -unsigned long find_random_bit(const unsigned long *addr, unsigned long size) -{ - int w = bitmap_weight(addr, size); - - switch (w) { - case 0: - return size; - case 1: - /* Performance trick for single-bit bitmaps */ - return find_first_bit(addr, size); - default: - return find_nth_bit(addr, size, get_random_u32_below(w)); - } -} -EXPORT_SYMBOL(find_random_bit); diff --git a/lib/find_random_bit.c b/lib/find_random_bit.c new file mode 100644 index 0000000000000000000000000000000000000000..6e72f832e1f5ff2821dbeb947dc604ae6528f7e6 --- /dev/null +++ b/lib/find_random_bit.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* bit search implementation + * + * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + * + * Copyright (C) 2008 IBM Corporation + * 'find_last_bit' is written by Rusty Russell + * (Inspired by David Howell's find_next_bit implementation) + * + * Rewritten by Yury Norov to decrease + * size and improve performance, 2015. + */ + +#include +#include +#include + +/** + * find_random_bit - find a set bit at random position + * @addr: The address to base the search on + * @size: The bitmap size in bits + * + * Returns: a position of a random set bit; >= @size otherwise + */ +unsigned long find_random_bit(const unsigned long *addr, unsigned long size) +{ + int w = bitmap_weight(addr, size); + + switch (w) { + case 0: + return size; + case 1: + /* Performance trick for single-bit bitmaps */ + return find_first_bit(addr, size); + default: + return find_nth_bit(addr, size, get_random_u32_below(w)); + } +} +EXPORT_SYMBOL(find_random_bit);