Skip to contentSkip to conversation

block: Reject negative values for throttling options

PATCH Superseded Fam Zheng 2 participants 7 messages Jan 14, 2016 – Jan 15, 2016

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v3 cover block: Reject negative values for throttling options

v3: Address comments:
- Add test for large value; [Berto]
- Fix typos "negative" & "caught"; [Eric, Berto]
- Use "LL" suffix to the upper limit constant. [Berto]

v2: Check the value range and report an appropriate error. [Berto]

Now the negative values are silently converted to a huge positive number because we are doing implicit casting from uint64_t to double. Fix it and add a test case (this was once fixed in 7d81c1413c9 but regressed when the block device option parsing code was changed).

Fam Zheng (2):
blockdev: Error out on negative throttling option values
iotests: Test that negative and large throttle values are rejected

 blockdev.c                    |  3 ++-
 include/qemu/throttle.h       |  2 ++
 tests/qemu-iotests/051        | 12 ++++++++++++
 tests/qemu-iotests/051.out    | 24 ++++++++++++++++++++++++
 tests/qemu-iotests/051.pc.out | 24 ++++++++++++++++++++++++
 util/throttle.c               | 16 ++++++----------
 6 files changed, 70 insertions(+), 11 deletions(-)
Signature · 1 line
2.4.3
author
#1 lore raw

PATCH v3 1/2 blockdev: Error out on negative throttling option values

The implicit casting from unsigned int to double changes negative values into large positive numbers and accepts them. We should instead print an error.

Check the number range so this case is caught and reported.

---
 blockdev.c              |  3 ++-
 include/qemu/throttle.h |  2 ++
 util/throttle.c         | 16 ++++++----------
 3 files changed, 10 insertions(+), 11 deletions(-)
blockdev.c +2 -1
index 2df0c6d..1afef87 100644
@@ -348,7 +348,8 @@ static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
     }
 
     if (!throttle_is_valid(cfg)) {
-        error_setg(errp, "bps/iops/maxs values must be 0 or greater");
+        error_setg(errp, "bps/iops/maxs values must be within [0, %" PRId64
+                         ")", (int64_t)THROTTLE_VALUE_MAX);
         return false;
     }
 
include/qemu/throttle.h +2 -0
index 12faaad..d0c98ed 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
+#define THROTTLE_VALUE_MAX 1000000000000000LL
+
 typedef enum {
     THROTTLE_BPS_TOTAL,
     THROTTLE_BPS_READ,
util/throttle.c +6 -10
index 1113671..af4bc95 100644
@@ -282,22 +282,18 @@ bool throttle_conflicting(ThrottleConfig *cfg)
  */
 bool throttle_is_valid(ThrottleConfig *cfg)
 {
-    bool invalid = false;
     int i;
 
     for (i = 0; i < BUCKETS_COUNT; i++) {
-        if (cfg->buckets[i].avg < 0) {
-            invalid = true;
+        if (cfg->buckets[i].avg < 0 ||
+            cfg->buckets[i].max < 0 ||
+            cfg->buckets[i].avg > THROTTLE_VALUE_MAX ||
+            cfg->buckets[i].max > THROTTLE_VALUE_MAX) {
+            return false;
         }
     }
 
-    for (i = 0; i < BUCKETS_COUNT; i++) {
-        if (cfg->buckets[i].max < 0) {
-            invalid = true;
-        }
-    }
-
-    return !invalid;
+    return true;
 }
 
 /* check if bps_max/iops_max is used without bps/iops
Signature · 1 line
2.4.3
Reviewed-by on v3 1/2
#2 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH v3 1/2] blockdev: Error out on negative throttling option values

Quoted 4 lines · Fam Zheng

The implicit casting from unsigned int to double changes negative values into large positive numbers and accepts them. We should instead print an error.

Quoted 8 lines

Check the number range so this case is caught and reported.

---
 blockdev.c              |  3 ++-
 include/qemu/throttle.h |  2 ++
 util/throttle.c         | 16 ++++++----------
 3 files changed, 10 insertions(+), 11 deletions(-)

blockdev.c static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) · line 352

Quoted 11 lines
blockdev.c +2 -1
index 2df0c6d..1afef87 100644
@@ -348,7 +348,8 @@ static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
     }
 
     if (!throttle_is_valid(cfg)) {
-        error_setg(errp, "bps/iops/maxs values must be 0 or greater");
+        error_setg(errp, "bps/iops/maxs values must be within [0, %" PRId64
+                         ")", (int64_t)THROTTLE_VALUE_MAX);

I personally would have liked a simpler %lli and no cast, but I can see why you want an explicit int64_t here.

include/qemu/throttle.h line 32

Quoted 12 lines
blockdev.c +0 -0
         return false;
     }
 
include/qemu/throttle.h +1 -0
index 12faaad..d0c98ed 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
+#define THROTTLE_VALUE_MAX 1000000000000000LL

<pedantic>
But then you could use UINT64_C(1000000000000000) here. </pedantic>

#3 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH v3 1/2] blockdev: Error out on negative throttling option values

Quoted 23 lines · Max Reitz
Quoted 4 lines · Fam Zheng

The implicit casting from unsigned int to double changes negative values into large positive numbers and accepts them. We should instead print an error.

Quoted 8 lines

Check the number range so this case is caught and reported.

---
 blockdev.c              |  3 ++-
 include/qemu/throttle.h |  2 ++
 util/throttle.c         | 16 ++++++----------
 3 files changed, 10 insertions(+), 11 deletions(-)
Quoted 7 lines
blockdev.c +0 -0
index 2df0c6d..1afef87 100644
@@ -348,7 +348,8 @@ static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
     }
 
Quoted 3 lines

blockdev.c static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) · line 351

Quoted 3 lines
blockdev.c +1 -1
     if (!throttle_is_valid(cfg)) {
-        error_setg(errp, "bps/iops/maxs values must be 0 or greater");
+        error_setg(errp, "bps/iops/maxs values must be within [0, %" PRId64

Pre-existing, but you might want to fix this to "bps/iops/max" while touching this line.

Max

Quoted 22 lines

blockdev.c static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) · line 352

Quoted 1 line
blockdev.c +1 -0
+                         ")", (int64_t)THROTTLE_VALUE_MAX);

I personally would have liked a simpler %lli and no cast, but I can see why you want an explicit int64_t here.

include/qemu/throttle.h line 32

Quoted 12 lines
blockdev.c +0 -0
         return false;
     }
 
include/qemu/throttle.h +1 -0
index 12faaad..d0c98ed 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
+#define THROTTLE_VALUE_MAX 1000000000000000LL

<pedantic>
But then you could use UINT64_C(1000000000000000) here. </pedantic>

author
#4 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH v3 1/2] blockdev: Error out on negative throttling option values

Quoted 24 lines · Max Reitz
Quoted 23 lines · Max Reitz
Quoted 4 lines · Fam Zheng

The implicit casting from unsigned int to double changes negative values into large positive numbers and accepts them. We should instead print an error.

Quoted 8 lines

Check the number range so this case is caught and reported.

---
 blockdev.c              |  3 ++-
 include/qemu/throttle.h |  2 ++
 util/throttle.c         | 16 ++++++----------
 3 files changed, 10 insertions(+), 11 deletions(-)
Quoted 7 lines
blockdev.c +0 -0
index 2df0c6d..1afef87 100644
@@ -348,7 +348,8 @@ static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
     }
 
Quoted 6 lines
Quoted 3 lines

blockdev.c static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) · line 351

Quoted 3 lines
blockdev.c +1 -1
     if (!throttle_is_valid(cfg)) {
-        error_setg(errp, "bps/iops/maxs values must be 0 or greater");
+        error_setg(errp, "bps/iops/maxs values must be within [0, %" PRId64

Pre-existing, but you might want to fix this to "bps/iops/max" while touching this line.

Makes sense, I'll apply your rev-by and fix this in v4.

Fam

Quoted 27 lines

Max

Quoted 22 lines

blockdev.c static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) · line 352

Quoted 1 line
blockdev.c +1 -0
+                         ")", (int64_t)THROTTLE_VALUE_MAX);

I personally would have liked a simpler %lli and no cast, but I can see why you want an explicit int64_t here.

include/qemu/throttle.h line 32

Quoted 12 lines
blockdev.c +0 -0
         return false;
     }
 
include/qemu/throttle.h +1 -0
index 12faaad..d0c98ed 100644
@@ -29,6 +29,8 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 
+#define THROTTLE_VALUE_MAX 1000000000000000LL

<pedantic>
But then you could use UINT64_C(1000000000000000) here. </pedantic>

author
#5 lore raw

PATCH v3 2/2 iotests: Test that negative and large throttle values are rejected

In reply to Fam Zheng
---
 tests/qemu-iotests/051        | 12 ++++++++++++
 tests/qemu-iotests/051.out    | 24 ++++++++++++++++++++++++
 tests/qemu-iotests/051.pc.out | 24 ++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
tests/qemu-iotests/051 +12 -0
index d91f80b..cdf72d4 100755
@@ -263,6 +263,18 @@ run_qemu -drive file="$TEST_IMG",iops_size=1234,throttling.iops-size=5678
 run_qemu -drive file="$TEST_IMG",readonly=on,read-only=off
 
 echo
+echo === Catching negative/large throttling values ===
+echo
+
+run_qemu -drive file="$TEST_IMG",iops=-1
+run_qemu -drive file="$TEST_IMG",bps=-2
+run_qemu -drive file="$TEST_IMG",bps_rd=-3
+run_qemu -drive file="$TEST_IMG",bps_rd_max=-3
+run_qemu -drive file="$TEST_IMG",throttling.iops-total=-4
+run_qemu -drive file="$TEST_IMG",throttling.bps-total=-5
+run_qemu -drive file="$TEST_IMG",bps=1000000000000001
+
+echo
 echo === Parsing protocol from file name ===
 echo
 
tests/qemu-iotests/051.out +24 -0
index bf886ce..efbb39c 100644
@@ -285,6 +285,30 @@ Testing: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off
 QEMU_PROG: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off: 'read-only' and its alias 'readonly' can't be used at the same time
 
 
+=== Catching negative/large throttling values ===
+
+Testing: -drive file=TEST_DIR/t.qcow2,iops=-1
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,iops=-1: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps=-2
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps=-2: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps_rd=-3
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps_rd=-3: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps_rd_max=-3
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps_rd_max=-3: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,throttling.iops-total=-4
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,throttling.iops-total=-4: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,throttling.bps-total=-5
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,throttling.bps-total=-5: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps=1000000000000001
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps=1000000000000001: bps/iops/maxs values must be within [0, 1000000000000000)
+
+
 === Parsing protocol from file name ===
 
 Testing: -hda foo:bar
tests/qemu-iotests/051.pc.out +24 -0
index a5dfc33..0cb1506 100644
@@ -379,6 +379,30 @@ Testing: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off
 QEMU_PROG: -drive file=TEST_DIR/t.qcow2,readonly=on,read-only=off: 'read-only' and its alias 'readonly' can't be used at the same time
 
 
+=== Catching negative/large throttling values ===
+
+Testing: -drive file=TEST_DIR/t.qcow2,iops=-1
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,iops=-1: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps=-2
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps=-2: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps_rd=-3
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps_rd=-3: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps_rd_max=-3
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps_rd_max=-3: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,throttling.iops-total=-4
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,throttling.iops-total=-4: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,throttling.bps-total=-5
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,throttling.bps-total=-5: bps/iops/maxs values must be within [0, 1000000000000000)
+
+Testing: -drive file=TEST_DIR/t.qcow2,bps=1000000000000001
+QEMU_PROG: -drive file=TEST_DIR/t.qcow2,bps=1000000000000001: bps/iops/maxs values must be within [0, 1000000000000000)
+
+
 === Parsing protocol from file name ===
 
 Testing: -hda foo:bar
Signature · 1 line
2.4.3
Reviewed-by on v3 2/2
#6 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH v3 2/2] iotests: Test that negative and large throttle values are rejected

Quoted 6 lines · Fam Zheng
---
 tests/qemu-iotests/051        | 12 ++++++++++++
 tests/qemu-iotests/051.out    | 24 ++++++++++++++++++++++++
 tests/qemu-iotests/051.pc.out | 24 ++++++++++++++++++++++++
 3 files changed, 60 insertions(+)