Skip to contentSkip to conversation

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

PATCH In review Philippe Mathieu-Daudé 2 participants 6 messages May 10, 2021 – May 21, 2021

lore
Share & tools
lore
#0 lore raw

v3 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 v2:
- clear bitmap to avoid spurious interrupts! (Stefan)
- use 'further' in find_next docstring (Eric)
- added Richard R-b tag

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 | 20 +++++---------------
 2 files changed, 17 insertions(+), 18 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

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

Document the following functions return the bitmap size if no 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..a72f69fea85 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 are no further set bits 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 are no further clear bits 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
#2 lore raw

PATCH v3 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 | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
hw/block/dataplane/virtio-blk.c +5 -15
index e9050c8987e..a31fa94ca33 100644
@@ -60,24 +60,14 @@ 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);
     }
+    bitmap_clear(s->batch_notify_vqs, 0, nvqs);
 }
 
 /* Context: QEMU global mutex held */
Signature · 1 line
2.26.3
#3 lore raw
Quoted 18 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 | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
hw/block/dataplane/virtio-blk.c +0 -0
index e9050c8987e..a31fa94ca33 100644

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

Quoted 10 lines
hw/block/dataplane/virtio-blk.c +2 -4
@@ -60,24 +60,14 @@ 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)) {

It needs to be find_next_bit(s->batch_notify_vqs, nvqs, i + 1).

Reviewed-by on the v3 series
#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 13 lines · Philippe Mathieu-Daudé

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 v2:
- clear bitmap to avoid spurious interrupts! (Stefan)
- use 'further' in find_next docstring (Eric)
- added Richard R-b tag

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

Quoted 12 lines

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 | 20 +++++---------------
 2 files changed, 17 insertions(+), 18 deletions(-)
Signature · 1 line
2.26.3
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 13 lines · Philippe Mathieu-Daudé

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 v2:
- clear bitmap to avoid spurious interrupts! (Stefan)
- use 'further' in find_next docstring (Eric)
- added Richard R-b tag

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

Quoted 7 lines

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 | 20 +++++---------------
 2 files changed, 17 insertions(+), 18 deletions(-)

This series causes "make check" to hang. QEMU processes consume 100% CPU in notify_guest_bh().

I have dropped this from my patch queue.

Stefan