Skip to contentSkip to conversation

virtio-blk: Convert QEMUBH callback to "bitops.h" API

PATCH Superseded Philippe Mathieu-Daudé 4 participants 8 messages May 7, 2021 – May 10, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover virtio-blk: Convert QEMUBH callback to "bitops.h" API

This series follow a suggestion from Stefan to use the bitops API in virtio-blk:
https://www.mail-archive.com/[email protected]/msg805139.html

Since v1:
- improved "bitops.h" docstring
- addressed Richard's review comments

Philippe Mathieu-Daudé (2):
bitops.h: Improve find_xxx_bit() documentation
virtio-blk: Convert QEMUBH callback to "bitops.h" API

 include/qemu/bitops.h           | 15 ++++++++++++---
 hw/block/dataplane/virtio-blk.c | 19 ++++---------------
 2 files changed, 16 insertions(+), 18 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH v2 1/2 bitops.h: Improve find_xxx_bit() documentation

Document the following functions return the bitmap size if not matching bit is found:

- find_first_bit
- find_next_bit
- find_last_bit
- find_first_zero_bit
- find_next_zero_bit

---
 include/qemu/bitops.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
include/qemu/bitops.h +12 -3
index 3acbf3384c6..eb0fc5416a9 100644
@@ -140,7 +140,8 @@ static inline int test_bit(long nr, const unsigned long *addr)
  * @addr: The address to start the search at
  * @size: The maximum size to search
  *
- * Returns the bit number of the first set bit, or size.
+ * Returns the bit number of the last set bit,
+ * or @size if there is no set bit in the bitmap.
  */
 unsigned long find_last_bit(const unsigned long *addr,
                             unsigned long size);
@@ -150,6 +151,9 @@ unsigned long find_last_bit(const unsigned long *addr,
  * @addr: The address to base the search on
  * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ *
+ * Returns the bit number of the next set bit,
+ * or @size if there is no set bit in the bitmap.
  */
 unsigned long find_next_bit(const unsigned long *addr,
                             unsigned long size,
@@ -160,6 +164,9 @@ unsigned long find_next_bit(const unsigned long *addr,
  * @addr: The address to base the search on
  * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ *
+ * Returns the bit number of the next cleared bit,
+ * or @size if there is no clear bit in the bitmap.
  */
 
 unsigned long find_next_zero_bit(const unsigned long *addr,
@@ -171,7 +178,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr,
  * @addr: The address to start the search at
  * @size: The maximum size to search
  *
- * Returns the bit number of the first set bit.
+ * Returns the bit number of the first set bit,
+ * or @size if there is no set bit in the bitmap.
  */
 static inline unsigned long find_first_bit(const unsigned long *addr,
                                            unsigned long size)
@@ -194,7 +202,8 @@ static inline unsigned long find_first_bit(const unsigned long *addr,
  * @addr: The address to start the search at
  * @size: The maximum size to search
  *
- * Returns the bit number of the first cleared bit.
+ * Returns the bit number of the first cleared bit,
+ * or @size if there is no clear bit in the bitmap.
  */
 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
                                                 unsigned long size)
Signature · 1 line
2.26.3
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] bitops.h: Improve find_xxx_bit() documentation

Quoted 9 lines · Philippe Mathieu-Daudé

Document the following functions return the bitmap size if not matching bit is found:

- find_first_bit
- find_next_bit
- find_last_bit
- find_first_zero_bit
- find_next_zero_bit

Quoted 4 lines
---
  include/qemu/bitops.h | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)

r~

#3 lore raw

Re: [PATCH v2 1/2] bitops.h: Improve find_xxx_bit() documentation

In reply to Philippe Mathieu-Daudé
Quoted 2 lines · Philippe Mathieu-Daudé

Document the following functions return the bitmap size if not matching bit is found:

s/not/no/

Quoted 14 lines

- find_first_bit
- find_next_bit
- find_last_bit
- find_first_zero_bit
- find_next_zero_bit

---
 include/qemu/bitops.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

include/qemu/bitops.h unsigned long find_last_bit(const unsigned long *addr, · line 158

Quoted 8 lines
include/qemu/bitops.h +3 -0
@@ -150,6 +151,9 @@ unsigned long find_last_bit(const unsigned long *addr,
  * @addr: The address to base the search on
  * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ *
+ * Returns the bit number of the next set bit,
+ * or @size if there is no set bit in the bitmap.
  */
 unsigned long find_next_bit(const unsigned long *addr,

Misleading - there might be a set bit prior to @offset. Better might be:

or @size if there are no further set bits in the bitmap.

include/qemu/bitops.h unsigned long find_next_bit(const unsigned long *addr, · line 172

Quoted 11 lines
include/qemu/bitops.h +3 -0
                             unsigned long size,
@@ -160,6 +164,9 @@ unsigned long find_next_bit(const unsigned long *addr,
  * @addr: The address to base the search on
  * @offset: The bitnumber to start searching at
  * @size: The bitmap size in bits
+ *
+ * Returns the bit number of the next cleared bit,
+ * or @size if there is no clear bit in the bitmap.
  */
 
 unsigned long find_next_zero_bit(const unsigned long *addr,

similarly,

or @size if there are no further clear bits in the bitmap.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#4 lore raw

PATCH v2 2/2 virtio-blk: Convert QEMUBH callback to "bitops.h" API

In reply to Philippe Mathieu-Daudé

By directly using find_first_bit() and find_next_bit from the "bitops.h" API to iterate over the bitmap, we can remove the bitmap[] variable-length array copy on the stack and the complex manual bit testing/clearing logic.

---
 hw/block/dataplane/virtio-blk.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
hw/block/dataplane/virtio-blk.c +4 -15
index e9050c8987e..a7b5bda06fc 100644
@@ -60,23 +60,12 @@ static void notify_guest_bh(void *opaque)
 {
     VirtIOBlockDataPlane *s = opaque;
     unsigned nvqs = s->conf->num_queues;
-    unsigned long bitmap[BITS_TO_LONGS(nvqs)];
-    unsigned j;
 
-    memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
-    memset(s->batch_notify_vqs, 0, sizeof(bitmap));
+    for (unsigned long i = find_first_bit(s->batch_notify_vqs, nvqs);
+             i < nvqs; i = find_next_bit(s->batch_notify_vqs, nvqs, i)) {
+        VirtQueue *vq = virtio_get_queue(s->vdev, i);
 
-    for (j = 0; j < nvqs; j += BITS_PER_LONG) {
-        unsigned long bits = bitmap[j / BITS_PER_LONG];
-
-        while (bits != 0) {
-            unsigned i = j + ctzl(bits);
-            VirtQueue *vq = virtio_get_queue(s->vdev, i);
-
-            virtio_notify_irqfd(s->vdev, vq);
-
-            bits &= bits - 1; /* clear right-most bit */
-        }
+        virtio_notify_irqfd(s->vdev, vq);
     }
 }
 
Signature · 1 line
2.26.3
Reviewed-by on v2 2/2
#5 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé

By directly using find_first_bit() and find_next_bit from the "bitops.h" API to iterate over the bitmap, we can remove the bitmap[] variable-length array copy on the stack and the complex manual bit testing/clearing logic.

---
  hw/block/dataplane/virtio-blk.c | 19 ++++---------------
  1 file changed, 4 insertions(+), 15 deletions(-)

r~

#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 33 lines · Philippe Mathieu-Daudé

By directly using find_first_bit() and find_next_bit from the "bitops.h" API to iterate over the bitmap, we can remove the bitmap[] variable-length array copy on the stack and the complex manual bit testing/clearing logic.

---
 hw/block/dataplane/virtio-blk.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
hw/block/dataplane/virtio-blk.c +3 -8
index e9050c8987e..a7b5bda06fc 100644
@@ -60,23 +60,12 @@ static void notify_guest_bh(void *opaque)
 {
     VirtIOBlockDataPlane *s = opaque;
     unsigned nvqs = s->conf->num_queues;
-    unsigned long bitmap[BITS_TO_LONGS(nvqs)];
-    unsigned j;
 
-    memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
-    memset(s->batch_notify_vqs, 0, sizeof(bitmap));
+    for (unsigned long i = find_first_bit(s->batch_notify_vqs, nvqs);
+             i < nvqs; i = find_next_bit(s->batch_notify_vqs, nvqs, i)) {
+        VirtQueue *vq = virtio_get_queue(s->vdev, i);
 
-    for (j = 0; j < nvqs; j += BITS_PER_LONG) {
-        unsigned long bits = bitmap[j / BITS_PER_LONG];
-
-        while (bits != 0) {

hw/block/dataplane/virtio-blk.c static void notify_guest_bh(void *opaque) · line 70

Quoted 10 lines
hw/block/dataplane/virtio-blk.c +1 -7
-            unsigned i = j + ctzl(bits);
-            VirtQueue *vq = virtio_get_queue(s->vdev, i);
-
-            virtio_notify_irqfd(s->vdev, vq);
-
-            bits &= bits - 1; /* clear right-most bit */
-        }
+        virtio_notify_irqfd(s->vdev, vq);
     }
 }

Bits in s->batch_notify_vqs[] must be cleared. Otherwise we may raise spurious irqs next time this function is called. The memset() can be moved after the loop.

Stefan

#7 lore raw
Quoted 40 lines · Stefan Hajnoczi
Quoted 39 lines · Philippe Mathieu-Daudé

By directly using find_first_bit() and find_next_bit from the "bitops.h" API to iterate over the bitmap, we can remove the bitmap[] variable-length array copy on the stack and the complex manual bit testing/clearing logic.

---
 hw/block/dataplane/virtio-blk.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
hw/block/dataplane/virtio-blk.c +3 -14
index e9050c8987e..a7b5bda06fc 100644
@@ -60,23 +60,12 @@ static void notify_guest_bh(void *opaque)
 {
     VirtIOBlockDataPlane *s = opaque;
     unsigned nvqs = s->conf->num_queues;
-    unsigned long bitmap[BITS_TO_LONGS(nvqs)];
-    unsigned j;
 
-    memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
-    memset(s->batch_notify_vqs, 0, sizeof(bitmap));
+    for (unsigned long i = find_first_bit(s->batch_notify_vqs, nvqs);
+             i < nvqs; i = find_next_bit(s->batch_notify_vqs, nvqs, i)) {
+        VirtQueue *vq = virtio_get_queue(s->vdev, i);
 
-    for (j = 0; j < nvqs; j += BITS_PER_LONG) {
-        unsigned long bits = bitmap[j / BITS_PER_LONG];
-
-        while (bits != 0) {
-            unsigned i = j + ctzl(bits);
-            VirtQueue *vq = virtio_get_queue(s->vdev, i);
-
-            virtio_notify_irqfd(s->vdev, vq);
-
-            bits &= bits - 1; /* clear right-most bit */
Quoted 8 lines

hw/block/dataplane/virtio-blk.c static void notify_guest_bh(void *opaque) · line 70

Quoted 4 lines
hw/block/dataplane/virtio-blk.c +1 -1
-        }
+        virtio_notify_irqfd(s->vdev, vq);
     }
 }

Bits in s->batch_notify_vqs[] must be cleared. Otherwise we may raise spurious irqs next time this function is called. The memset() can be moved after the loop.

Doh... good catch! I missed it when removing the previous test_and_clear_bit() call (from v1).