Skip to contentSkip to conversation

block/mirror: limit qiov to IOV_MAX elements

PATCH Applied Stefan Hajnoczi 5 participants 9 messages Jul 1, 2015 – Aug 6, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/mirror: limit qiov to IOV_MAX elements

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

---
 block/mirror.c | 4 ++++
 trace-events   | 1 +
 2 files changed, 5 insertions(+)
block/mirror.c +4 -0
index 048e452..985ad00 100644
@@ -241,6 +241,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
             trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
             break;
         }
+        if (IOV_MAX < nb_chunks + added_chunks) {
+            trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
+            break;
+        }
 
         /* We have enough free space to copy these sectors.  */
         bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
trace-events +1 -0
index 52b7efa..943cd0c 100644
@@ -94,6 +94,7 @@ mirror_yield(void *s, int64_t cnt, int buf_free_count, int in_flight) "s %p dirt
 mirror_yield_in_flight(void *s, int64_t sector_num, int in_flight) "s %p sector_num %"PRId64" in_flight %d"
 mirror_yield_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
 mirror_break_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
+mirror_break_iov_max(void *s, int nb_chunks, int added_chunks) "s %p requested chunks %d added_chunks %d"
 
 # block/backup.c
 backup_do_cow_enter(void *job, int64_t start, int64_t sector_num, int nb_sectors) "job %p start %"PRId64" sector_num %"PRId64" nb_sectors %d"
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 30 lines · Stefan Hajnoczi

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

---
 block/mirror.c | 4 ++++
 trace-events   | 1 +
 2 files changed, 5 insertions(+)
block/mirror.c +0 -0
index 048e452..985ad00 100644

block/mirror.c static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) · line 244

Quoted 4 lines
block/mirror.c +1 -0
@@ -241,6 +241,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
             trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
             break;
         }
+        if (IOV_MAX < nb_chunks + added_chunks) {

No Yoda conditions... apart from that,

Quoted 19 lines
block/mirror.c +3 -0
+            trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
+            break;
+        }
 
         /* We have enough free space to copy these sectors.  */
         bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
trace-events +1 -0
index 52b7efa..943cd0c 100644
@@ -94,6 +94,7 @@ mirror_yield(void *s, int64_t cnt, int buf_free_count, int in_flight) "s %p dirt
 mirror_yield_in_flight(void *s, int64_t sector_num, int in_flight) "s %p sector_num %"PRId64" in_flight %d"
 mirror_yield_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
 mirror_break_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
+mirror_break_iov_max(void *s, int nb_chunks, int added_chunks) "s %p requested chunks %d added_chunks %d"
 
 # block/backup.c
 backup_do_cow_enter(void *job, int64_t start, int64_t sector_num, int nb_sectors) "job %p start %"PRId64" sector_num %"PRId64" nb_sectors %d"
#2 lore raw
Quoted 38 lines · Paolo Bonzini
Quoted 30 lines · Stefan Hajnoczi

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

---
 block/mirror.c | 4 ++++
 trace-events   | 1 +
 2 files changed, 5 insertions(+)
block/mirror.c +0 -0
index 048e452..985ad00 100644

block/mirror.c static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) · line 244

Quoted 4 lines
block/mirror.c +1 -0
@@ -241,6 +241,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
             trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
             break;
         }
+        if (IOV_MAX < nb_chunks + added_chunks) {

No Yoda conditions... apart from that,

Quoted 1 line

I found it annoying to write it backwards too, but it's for consistency:

  if (s->buf_free_count < nb_chunks + added_chunks) {
      trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
      break;
  }
  if (IOV_MAX < nb_chunks + added_chunks) {
      trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
      break;
  }

It's the same type of check as s->buf_free_count (which isn't modified by this loop either so it's a yoda conditional).

#3 lore raw
Quoted 11 lines · Stefan Hajnoczi

I found it annoying to write it backwards too, but it's for consistency:

  if (s->buf_free_count < nb_chunks + added_chunks) {
      trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
      break;
  }
  if (IOV_MAX < nb_chunks + added_chunks) {
      trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
      break;
  }
Quoted 2 lines

It's the same type of check as s->buf_free_count (which isn't modified by this loop either so it's a yoda conditional).

Hmm, right. The problem goes back to:

        while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
            trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
            qemu_coroutine_yield();
        }

where s->buf_free_count _is_ modified by the loop. The if below:

        if (s->buf_free_count < nb_chunks + added_chunks) {
            trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
            break;
        }

is written as a < check for consistency, and the one you add exacerbates the problem. If you want you can change the < to > in the "while" loop as well; otherwise the patch is okay as is.

Paolo

#4 lore raw
Quoted 24 lines · Paolo Bonzini
Quoted 11 lines · Stefan Hajnoczi

I found it annoying to write it backwards too, but it's for consistency:

  if (s->buf_free_count < nb_chunks + added_chunks) {
      trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
      break;
  }
  if (IOV_MAX < nb_chunks + added_chunks) {
      trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
      break;
  }
Quoted 2 lines

It's the same type of check as s->buf_free_count (which isn't modified by this loop either so it's a yoda conditional).

Hmm, right. The problem goes back to:

        while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
            trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
            qemu_coroutine_yield();
        }

where s->buf_free_count _is_ modified by the loop. The if below:

Quoted 8 lines
        if (s->buf_free_count < nb_chunks + added_chunks) {
            trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
            break;
        }

is written as a < check for consistency, and the one you add exacerbates the problem. If you want you can change the < to > in the "while" loop as well; otherwise the patch is okay as is.

Let's leave it.

#5 lore raw
In reply to Stefan Hajnoczi

Am 01.07.2015 um 16:45 hat Stefan Hajnoczi geschrieben:

Quoted 12 lines

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

Quoted 8 lines
              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

This looks like a backend-specific problem with raw-posix. Wouldn't it make more sense to either let raw-posix split requests it can't handle or introduce a bs->iov_max instead of spreading knowledge about specific backends into all callers of the block layer?

I'm not objecting to taking this patch for now to fix the bug in 2.4, but I don't think it's the proper solution.

Kevin

Quoted 35 lines
 block/mirror.c | 4 ++++
 trace-events   | 1 +
 2 files changed, 5 insertions(+)
block/mirror.c +4 -0
index 048e452..985ad00 100644
@@ -241,6 +241,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
             trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
             break;
         }
+        if (IOV_MAX < nb_chunks + added_chunks) {
+            trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
+            break;
+        }
 
         /* We have enough free space to copy these sectors.  */
         bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
trace-events +1 -0
index 52b7efa..943cd0c 100644
@@ -94,6 +94,7 @@ mirror_yield(void *s, int64_t cnt, int buf_free_count, int in_flight) "s %p dirt
 mirror_yield_in_flight(void *s, int64_t sector_num, int in_flight) "s %p sector_num %"PRId64" in_flight %d"
 mirror_yield_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
 mirror_break_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
+mirror_break_iov_max(void *s, int nb_chunks, int added_chunks) "s %p requested chunks %d added_chunks %d"
 
 # block/backup.c
 backup_do_cow_enter(void *job, int64_t start, int64_t sector_num, int nb_sectors) "job %p start %"PRId64" sector_num %"PRId64" nb_sectors %d"
Signature · 1 line
2.4.3
#6 lore raw
Quoted 22 lines · Kevin Wolf

Am 01.07.2015 um 16:45 hat Stefan Hajnoczi geschrieben:

Quoted 12 lines

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

Quoted 8 lines
              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

Quoted 7 lines

This looks like a backend-specific problem with raw-posix. Wouldn't it make more sense to either let raw-posix split requests it can't handle or introduce a bs->iov_max instead of spreading knowledge about specific backends into all callers of the block layer?

I'm not objecting to taking this patch for now to fix the bug in 2.4, but I don't think it's the proper solution.

I will send a patch for QEMU 2.5 that introduces an iov max field in BlockLimits. It will convert the request merging code which currently hardcodes IOV_MAX too.

In practice there are probably only two values that get used: IOV_MAX and INT_MAX, but we already have BlockLimits and it doesn't hurt to add this.

Stefan

#7 lore raw
In reply to Stefan Hajnoczi

Sorry Jeff, my "Cc:" line in the commit description wasn't picked up. Please merge through your block/jobs tree for QEMU 2.5.

Thanks,
Stefan

Quoted 56 lines · Stefan Hajnoczi

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

---
 block/mirror.c | 4 ++++
 trace-events   | 1 +
 2 files changed, 5 insertions(+)
block/mirror.c +4 -0
index 048e452..985ad00 100644
@@ -241,6 +241,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
             trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
             break;
         }
+        if (IOV_MAX < nb_chunks + added_chunks) {
+            trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
+            break;
+        }
 
         /* We have enough free space to copy these sectors.  */
         bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
trace-events +1 -1
index 52b7efa..943cd0c 100644
@@ -94,6 +94,7 @@ mirror_yield(void *s, int64_t cnt, int buf_free_count, int in_flight) "s %p dirt
 mirror_yield_in_flight(void *s, int64_t sector_num, int in_flight) "s %p sector_num %"PRId64" in_flight %d"
 mirror_yield_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
 mirror_break_buf_busy(void *s, int nb_chunks, int in_flight) "s %p requested chunks %d in_flight %d"
+mirror_break_iov_max(void *s, int nb_chunks, int added_chunks) "s %p requested chunks %d added_chunks %d"
 
 # block/backup.c
 backup_do_cow_enter(void *job, int64_t start, int64_t sector_num, int nb_sectors) "job %p start %"PRId64" sector_num %"PRId64" nb_sectors %d"
--

2.4.3

Applied
#8 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

If mirror has more free buffers than IOV_MAX, preadv(2)/pwritev(2) EINVAL failures may be encountered.

It is possible to trigger this by setting granularity to a low value like 8192.

This patch stops appending chunks once IOV_MAX is reached.

The spurious EINVAL failure can be reproduced with a qcow2 image file and the following QMP invocation:

Quoted 7 lines

qmp.command('drive-mirror', device='virtio0', target='/tmp/r7.s1',

              granularity=8192, sync='full', mode='absolute-paths',
              format='raw')

While the guest is running dd if=/dev/zero of=/var/tmp/foo oflag=direct bs=4k.

Thanks,

Applied to my 'block-next' branch:

https://github.com/codyprime/qemu-kvm-jtc/tree/block-next

-Jeff