Linux virtualization list
 help / color / mirror / Atom feed
From: Luan Haickel Araujo <luanharaujo@gmail.com>
To: david@kernel.org, mst@redhat.com, jasowang@redhat.com
Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com,
	virtualization@lists.linux.dev, linux-kernel@vger.kernel.org,
	Luan Haickel Araujo <luanharaujo@gmail.com>,
	David Hildenbrand <david@redhat.com>
Subject: [PATCH] virtio_mem: add bitmap_test_range_all_set/zero helpers
Date: Sun,  5 Apr 2026 18:32:10 -0300	[thread overview]
Message-ID: <20260405213210.108533-1-luanharaujo@gmail.com> (raw)

Encapsulate find_next_bit and find_next_zero_bit calls into helpers
to verify if a range of subblocks is entirely plugged or unplugged.

This improves readability as requested by the TODO comments in the
source code.

Validation confirms the change is binary-neutral on ARM64, with the
generated instructions remaining identical.

Suggested-by: David Hildenbrand <david@redhat.com>

Signed-off-by: Luan Haickel Araujo <luanharaujo@gmail.com>
---
Hi David,

This is my first patch contribution to the Linux kernel, following up on
your TODO notes in virtio_mem.c.

As part of my learning process (LFX 103), I verified the change in a
QEMU (KVM) environment with test_bitmap. I also compared the generated
assembly on ARM64 and it remains identical, confirming the helpers are
binary-neutral as expected.

I noticed these helpers could also be useful in include/linux/bitmap.h in
the future. If you think it makes sense to move them there, I'd be happy
to work on that in a next step.

Looking forward to your feedback.

Luan.
 drivers/virtio/virtio_mem.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 48051e9e98ab..e94e30af3a5d 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -538,6 +538,22 @@ static void virtio_mem_sbm_set_sb_unplugged(struct virtio_mem *vm,
 	__bitmap_clear(vm->sbm.sb_states, bit, count);
 }
 
+static inline bool bitmap_test_range_all_set(const unsigned long *map,
+					     unsigned int start, unsigned int nbits)
+{
+	unsigned int next_zero_bit = find_next_zero_bit(map, start + nbits, start);
+
+	return next_zero_bit >= start + nbits;
+}
+
+static inline bool bitmap_test_range_all_zero(const unsigned long *map,
+					      unsigned int start, unsigned int nbits)
+{
+	unsigned int next_set_bit = find_next_bit(map, start + nbits, start);
+
+	return next_set_bit >= start + nbits;
+}
+
 /*
  * Test if all selected subblocks are plugged.
  */
@@ -550,9 +566,7 @@ static bool virtio_mem_sbm_test_sb_plugged(struct virtio_mem *vm,
 	if (count == 1)
 		return test_bit(bit, vm->sbm.sb_states);
 
-	/* TODO: Helper similar to bitmap_set() */
-	return find_next_zero_bit(vm->sbm.sb_states, bit + count, bit) >=
-	       bit + count;
+	return bitmap_test_range_all_set(vm->sbm.sb_states, bit, count);
 }
 
 /*
@@ -564,9 +578,7 @@ static bool virtio_mem_sbm_test_sb_unplugged(struct virtio_mem *vm,
 {
 	const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id);
 
-	/* TODO: Helper similar to bitmap_set() */
-	return find_next_bit(vm->sbm.sb_states, bit + count, bit) >=
-	       bit + count;
+	return bitmap_test_range_all_zero(vm->sbm.sb_states, bit, count);
 }
 
 /*
-- 
2.53.0


             reply	other threads:[~2026-04-05 21:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-05 21:32 Luan Haickel Araujo [this message]
2026-04-08 13:49 ` [PATCH] virtio_mem: add bitmap_test_range_all_set/zero helpers David Hildenbrand (Arm)

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=20260405213210.108533-1-luanharaujo@gmail.com \
    --to=luanharaujo@gmail.com \
    --cc=david@kernel.org \
    --cc=david@redhat.com \
    --cc=eperezma@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.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