Skip to contentSkip to conversation

qemu-img: add check for zero-length job len

PATCH In review John Snow 4 participants 5 messages Nov 2, 2015 – Nov 4, 2015

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH 1/1 qemu-img: add check for zero-length job len

The mirror job doesn't update its total length until it has already started running, so we should translate a zero-length job-len as meaning 0%.

Otherwise, we may get divide-by-zero faults.

---
 qemu-img.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qemu-img.c +2 -1
index 3025776..38b4888 100644
@@ -656,7 +656,8 @@ static void run_block_job(BlockJob *job, Error **errp)
 
     do {
         aio_poll(aio_context, true);
-        qemu_progress_print((float)job->offset / job->len * 100.f, 0);
+        qemu_progress_print(job->len ?
+                            ((float)job->offset / job->len * 100.f) : 0.00, 0);
     } while (!job->ready);
 
     block_job_complete_sync(job, errp);
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · John Snow

The mirror job doesn't update its total length until it has already started running, so we should translate a zero-length job-len as meaning 0%.

Otherwise, we may get divide-by-zero faults.

---
 qemu-img.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

And indeed, this has tripped up libvirt in the past :)

My only concern is what if you truly have a 0-length job? For example, when doing two block-stream commands with identical arguments in a row, the second block-stream has no work to do, but can complete instantly.

Will this result in such a job never reporting 100% complete? If so, that's bad.

If you can answer my concerns that we don't have a design bug, then the code changes look correct, and you can add:

qemu-img.c static void run_block_job(BlockJob *job, Error **errp) · line 660

Quoted 12 lines
qemu-img.c +2 -1
index 3025776..38b4888 100644
@@ -656,7 +656,8 @@ static void run_block_job(BlockJob *job, Error **errp)
 
     do {
         aio_poll(aio_context, true);
-        qemu_progress_print((float)job->offset / job->len * 100.f, 0);
+        qemu_progress_print(job->len ?
+                            ((float)job->offset / job->len * 100.f) : 0.00, 0);

Also, note that this promotes to double rather than float; maybe you want to use 0.f instead of 0.00 to keep the ternary as a float? But it shouldn't make a difference in practice.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw
Quoted 12 lines · Eric Blake
Quoted 10 lines · John Snow

The mirror job doesn't update its total length until it has already started running, so we should translate a zero-length job-len as meaning 0%.

Otherwise, we may get divide-by-zero faults.

---
 qemu-img.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 9 lines

And indeed, this has tripped up libvirt in the past :)

My only concern is what if you truly have a 0-length job? For example, when doing two block-stream commands with identical arguments in a row, the second block-stream has no work to do, but can complete instantly.

Will this result in such a job never reporting 100% complete? If so, that's bad.

A few lines below the context:

/* A block job may finish instantaneously without publishing any progress,

* so just signal completion here */
qemu_progress_print(100.f, 0);

Quoted 12 lines

If you can answer my concerns that we don't have a design bug, then the code changes look correct, and you can add:

Quoted 7 lines
qemu-img.c +0 -0
index 3025776..38b4888 100644
@@ -656,7 +656,8 @@ static void run_block_job(BlockJob *job, Error **errp)
 
Quoted 10 lines

qemu-img.c static void run_block_job(BlockJob *job, Error **errp) · line 660

Quoted 5 lines
qemu-img.c +2 -1
     do {
         aio_poll(aio_context, true);
-        qemu_progress_print((float)job->offset / job->len * 100.f, 0);
+        qemu_progress_print(job->len ?
+                            ((float)job->offset / job->len * 100.f) : 0.00, 0);

Also, note that this promotes to double rather than float; maybe you want to use 0.f instead of 0.00 to keep the ternary as a float? But it shouldn't make a difference in practice.

Yes, oops -- but harmless.

#3 lore raw

Am 03.11.2015 um 00:45 hat John Snow geschrieben:

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
qemu-img.c +0 -0
index 3025776..38b4888 100644
@@ -656,7 +656,8 @@ static void run_block_job(BlockJob *job, Error **errp)
 
Quoted 9 lines

qemu-img.c static void run_block_job(BlockJob *job, Error **errp) · line 660

Quoted 5 lines
qemu-img.c +2 -1
     do {
         aio_poll(aio_context, true);
-        qemu_progress_print((float)job->offset / job->len * 100.f, 0);
+        qemu_progress_print(job->len ?
+                            ((float)job->offset / job->len * 100.f) : 0.00, 0);

Also, note that this promotes to double rather than float; maybe you want to use 0.f instead of 0.00 to keep the ternary as a float? But it shouldn't make a difference in practice.

Quoted 1 line

Yes, oops -- but harmless.

Thanks, fixed that up and applied to the block branch.

Kevin

Reviewed-by on v1 1/1
#4 lore raw
In reply to John Snow
Quoted 28 lines · John Snow

The mirror job doesn't update its total length until it has already started running, so we should translate a zero-length job-len as meaning 0%.

Otherwise, we may get divide-by-zero faults.

---
 qemu-img.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
qemu-img.c +2 -1
index 3025776..38b4888 100644
@@ -656,7 +656,8 @@ static void run_block_job(BlockJob *job, Error **errp)
 
     do {
         aio_poll(aio_context, true);
-        qemu_progress_print((float)job->offset / job->len * 100.f, 0);
+        qemu_progress_print(job->len ?
+                            ((float)job->offset / job->len * 100.f) : 0.00, 0);
     } while (!job->ready);
 
     block_job_complete_sync(job, errp);
Signature · 1 line
2.4.3