Skip to contentSkip to conversation

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

PATCH Superseded Philippe Mathieu-Daudé 2 participants 4 messages May 6, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

By directly using test_and_clear_bit() from the "bitops.h" API, 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 | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
hw/block/dataplane/virtio-blk.c +2 -13
index e9050c8987e..25d9b10c02b 100644
@@ -59,23 +59,12 @@ void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s, VirtQueue *vq)
 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 (j = 0; j < nvqs; j += BITS_PER_LONG) {
-        unsigned long bits = bitmap[j / BITS_PER_LONG];
-
-        while (bits != 0) {
-            unsigned i = j + ctzl(bits);
+    for (unsigned i = 0; i < s->conf->num_queues; i++) {
+        if (test_and_clear_bit(i, s->batch_notify_vqs)) {
             VirtQueue *vq = virtio_get_queue(s->vdev, i);
 
             virtio_notify_irqfd(s->vdev, vq);
-
-            bits &= bits - 1; /* clear right-most bit */
         }
     }
 }
Signature · 1 line
2.26.3
#1 lore raw
Quoted 6 lines · Philippe Mathieu-Daudé
  static void notify_guest_bh(void *opaque)
  {
      VirtIOBlockDataPlane *s = opaque;
hw/block/dataplane/virtio-blk.c +0 -3
-    unsigned nvqs = s->conf->num_queues;
-    unsigned long bitmap[BITS_TO_LONGS(nvqs)];
-    unsigned j;

hw/block/dataplane/virtio-blk.c void virtio_blk_data_plane_notify(VirtIOBlockDataPlane *s, VirtQueue *vq) · line 63

Quoted 10 lines
hw/block/dataplane/virtio-blk.c +1 -8
 
-    memcpy(bitmap, s->batch_notify_vqs, sizeof(bitmap));
-    memset(s->batch_notify_vqs, 0, sizeof(bitmap));
-
-    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);
+    for (unsigned i = 0; i < s->conf->num_queues; i++) {

Is this bitmap dense enough that you want to iterate by index, or is it sparse enough to iterate via find_first_bit/find_next_bit?

In either case, leave the copy of s->conf->num_queues to a local variable.

r~

#2 lore raw
Quoted 10 lines · Richard Henderson
Quoted 9 lines · Philippe Mathieu-Daudé

  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)); -

Quoted 8 lines
Quoted 6 lines

-    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);
+    for (unsigned i = 0; i < s->conf->num_queues; i++) {

Is this bitmap dense enough that you want to iterate by index,

By 'iterate by index' do you mean the actual iteration with 'j'?

Quoted 2 lines

or is it
sparse enough to iterate via find_first_bit/find_next_bit?

I looked at find_first_bit/find_next_bit() but they seemed to do a lot more than test_and_clear_bit(). As Stefan said this is hot path, I thought this would be cheaper, but haven't profiled the performance.

Quoted 1 line

In either case, leave the copy of  s->conf->num_queues to a local variable.

That is sensible to do :)

#3 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Richard Henderson
Quoted 9 lines · Philippe Mathieu-Daudé

  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)); -

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

-    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);
+    for (unsigned i = 0; i < s->conf->num_queues; i++) {

Is this bitmap dense enough that you want to iterate by index,

The max is 1Kb:

#define VIRTIO_QUEUE_MAX 1024

Quoted 15 lines

By 'iterate by index' do you mean the actual iteration with 'j'?

Quoted 2 lines

or is it
sparse enough to iterate via find_first_bit/find_next_bit?

I looked at find_first_bit/find_next_bit() but they seemed to do a lot more than test_and_clear_bit(). As Stefan said this is hot path, I thought this would be cheaper, but haven't profiled the performance.

Quoted 1 line

In either case, leave the copy of  s->conf->num_queues to a local variable.

That is sensible to do :)