mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alessio Maroni <alessiomaroni0@gmail.com>
To: Yury Norov <yury.norov@gmail.com>,
	David Hildenbrand <david@kernel.org>,
	"Michael S . Tsirkin" <mst@redhat.com>
Cc: "Jason Wang" <jasowangio@gmail.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
	virtualization@lists.linux.dev, linux-kernel@vger.kernel.org,
	"Alessio Maroni" <alessiomaroni0@gmail.com>
Subject: [PATCH 1/2] bitmap: add bitmap_all_set() and bitmap_all_clear()
Date: Sun,  6 Dec 2026 14:52:19 +0100	[thread overview]
Message-ID: <20261206135220.153130-2-alessiomaroni0@gmail.com> (raw)
In-Reply-To: <20261206135220.153130-1-alessiomaroni0@gmail.com>

Add bitmap_all_set() and bitmap_all_clear() helpers to check whether
all bits in an arbitrary [start, start + nbits) range are set or
clear, generalizing bitmap_full() and bitmap_empty() with the same
(map, start, nbits) signature style as bitmap_set() and bitmap_clear().

Include compile-time fast paths for zero, single-bit, and small
single-word ranges, falling back to find_next_zero_bit() and
find_next_bit() otherwise. Add corresponding test cases in
lib/test_bitmap.c.

Signed-off-by: Alessio Maroni <alessiomaroni0@gmail.com>
---
 include/linux/bitmap.h | 47 ++++++++++++++++++++++++++++++++++++++++++
 lib/test_bitmap.c      | 38 ++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 08de1467367e..bc6eff615f27 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -576,6 +576,53 @@ void bitmap_clear(unsigned long *map, unsigned int start, unsigned int nbits)
 		__bitmap_clear(map, start, nbits);
 }
 
+/**
+ * bitmap_all_set - check if all bits in a region are set
+ * @map: address to the bitmap memory region
+ * @start: bit offset of the region
+ * @nbits: number of bits to check
+ *
+ * Return: true if all @nbits bits starting at @start are set,
+ * false otherwise.
+ */
+static __always_inline
+bool bitmap_all_set(const unsigned long *map, unsigned int start,
+		    unsigned int nbits)
+{
+	if (unlikely(!nbits))
+		return true;
+	if (__builtin_constant_p(nbits) && nbits == 1)
+		return test_bit(start, map);
+	if (small_const_nbits(start + nbits))
+		return (*map & GENMASK(start + nbits - 1, start)) ==
+		       GENMASK(start + nbits - 1, start);
+
+	return find_next_zero_bit(map, start + nbits, start) >= start + nbits;
+}
+
+/**
+ * bitmap_all_clear - check if all bits in a region are clear
+ * @map: address to the bitmap memory region
+ * @start: bit offset of the region
+ * @nbits: number of bits to check
+ *
+ * Return: true if all @nbits bits starting at @start are clear,
+ * false otherwise.
+ */
+static __always_inline
+bool bitmap_all_clear(const unsigned long *map, unsigned int start,
+		      unsigned int nbits)
+{
+	if (unlikely(!nbits))
+		return true;
+	if (__builtin_constant_p(nbits) && nbits == 1)
+		return !test_bit(start, map);
+	if (small_const_nbits(start + nbits))
+		return !(*map & GENMASK(start + nbits - 1, start));
+
+	return find_next_bit(map, start + nbits, start) >= start + nbits;
+}
+
 static __always_inline
 void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
 			unsigned int shift, unsigned int nbits)
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index ba09d057f47f..5b64aa420005 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1541,6 +1541,8 @@ static void __init test_zero_nbits(void)
 	bitmap_xor(NULL, NULL, NULL, 0);
 	bitmap_zero(NULL, 0);
 
+	ret = bitmap_all_clear(NULL, 0, 0);
+	ret = bitmap_all_set(NULL, 0, 0);
 	ret = bitmap_and(NULL, NULL, NULL, 0);
 	ret = bitmap_empty(NULL, 0);
 	ret = bitmap_equal(NULL, NULL, 0);
@@ -1570,6 +1572,40 @@ static void __init test_zero_nbits(void)
 	ret = find_random_bit(NULL, 0);
 }
 
+static void __init test_bitmap_all_set_clear(void)
+{
+	DECLARE_BITMAP(bmap, 256);
+	unsigned int start, nbits;
+	bool expected_set, expected_clear;
+
+	bitmap_zero(bmap, 256);
+	bitmap_set(bmap, 3, 17);
+	bitmap_set(bmap, 60, 12);
+	bitmap_set(bmap, 128, 64);
+
+	expect_eq_uint(true, bitmap_all_set(bmap, 3, 0));
+	expect_eq_uint(true, bitmap_all_clear(bmap, 3, 0));
+	expect_eq_uint(true, bitmap_all_set(bmap, 3, 1));
+	expect_eq_uint(false, bitmap_all_set(bmap, 2, 1));
+	expect_eq_uint(true, bitmap_all_clear(bmap, 2, 1));
+	expect_eq_uint(true, bitmap_all_set(bmap, 3, 17));
+	expect_eq_uint(false, bitmap_all_set(bmap, 3, 18));
+
+	for (start = 0; start < 200; start++) {
+		for (nbits = 0; nbits <= 256 - start; nbits++) {
+			expected_set = find_next_zero_bit(bmap, start + nbits,
+							  start) >= start + nbits;
+			expected_clear = find_next_bit(bmap, start + nbits,
+						       start) >= start + nbits;
+
+			expect_eq_uint(expected_set,
+				       bitmap_all_set(bmap, start, nbits));
+			expect_eq_uint(expected_clear,
+				       bitmap_all_clear(bmap, start, nbits));
+		}
+	}
+}
+
 #undef TEST_BIT_LEN
 
 static void __init selftest(void)
@@ -1607,6 +1643,8 @@ static void __init selftest(void)
 	test_for_each_set_clump8();
 	test_for_each_set_bit_wrap();
 	test_bitmap_find_next_zero_area_off();
+	test_zero_nbits();
+	test_bitmap_all_set_clear();
 }
 
 KSTM_MODULE_LOADERS(test_bitmap);
-- 
2.56.1


  reply	other threads:[~2026-12-06 13:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 13:52 [PATCH 0/2] bitmap: add bitmap_all_set()/bitmap_all_clear() and use in virtio-mem Alessio Maroni
2026-12-06 13:52 ` Alessio Maroni [this message]
2026-12-06 13:52 ` [PATCH 2/2] virtio-mem: use bitmap_all_set() and bitmap_all_clear() Alessio Maroni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206135220.153130-2-alessiomaroni0@gmail.com \
    --to=alessiomaroni0@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=jasowangio@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mst@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®