Skip to contentSkip to conversation

-numa option parsing fixes (v2)

PATCH In review Eduardo Habkost 2 participants 23 messages Jan 16, 2013

lore
Share & tools
lore
#0 lore raw

cover -numa option parsing fixes (v2)

This series contains only the most important fixes from the previous "-numa option parsing fixes & improvements" series I have submitted.

I have introduced parse_uint*() helpers that can be reused by other code, later. I plan to submit parse_int*() (for signed integers) and parse_double*() functions too, later, and change string-input-visitor.c and opts-visitor.c to use those common functions instead of duplicating the number parsing code.

Eduardo Habkost (8):
cutils: unsigned int parsing functions
vl.c: Fix off-by-one bug when handling "-numa node" argument vl.c: Abort on unknown -numa option type
vl.c: Check for NUMA node limit inside numa_add() vl.c: numa_add(): Validate nodeid before using it vl.c: Use parse_uint_full() for NUMA nodeid
vl.c: Extract -numa "cpus" parsing to separate function vl.c: validate -numa "cpus" parameter properly

 include/qemu-common.h |   3 +
 tests/Makefile        |   3 +
 tests/test-cutils.c   | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++
 util/cutils.c         |  76 +++++++++++++++++++++
 vl.c                  |  93 ++++++++++++++++++-------
 5 files changed, 333 insertions(+), 25 deletions(-)
 create mode 100644 tests/test-cutils.c
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/8 cutils: unsigned int parsing functions

There are lots of duplicate parsing code using strto*() in QEMU, and most of that code is broken in one way or another. Even the visitors code have duplicate integer parsing code[1]. This introduces functions to help parsing unsigned int values: parse_uint() and parse_uint_full().

Parsing functions for signed ints and floats will be submitted later.

parse_uint_full() has all the checks made by opts_type_uint64() at opts-visitor.c:

- Check for NULL (returns -EINVAL)
- Check for negative numbers (returns -ERANGE)
- Check for empty string (returns -EINVAL)
- Check for overflow or other errno values set by strtoll() (returns -errno)
- Check for end of string (reject invalid characters after number) (returns -EINVAL)

parse_uint() does everything above except checking for the end of the string, so callers can continue parsing the remainder of string after the number.

Unit tests included.

[1] string-input-visitor.c:parse_int() could use the same parsing code used by opts-visitor.c:opts_type_int(), instead of duplicating that logic.

---
Cc: Laszlo Ersek <[email protected]>
Cc: Eric Blake <[email protected]>

---
 include/qemu-common.h |   3 +
 tests/Makefile        |   3 +
 tests/test-cutils.c   | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++
 util/cutils.c         |  76 +++++++++++++++++++++
 4 files changed, 265 insertions(+)
 create mode 100644 tests/test-cutils.c
include/qemu-common.h +3 -0
index ca464bb..83f89fd 100644
@@ -170,6 +170,9 @@ int qemu_fdatasync(int fd);
 int fcntl_setfl(int fd, int flag);
 int qemu_parse_fd(const char *param);
 
+int parse_uint(const char *s, unsigned long long *value, char **endptr);
+int parse_uint_full(const char *s, unsigned long long *value);
+
 /*
  * strtosz() suffixes used to specify the default treatment of an
  * argument passed to strtosz() without an explicit suffix.
tests/Makefile +3 -0
index d97a571..2dba3e5 100644
@@ -45,6 +45,8 @@ gcov-files-test-aio-$(CONFIG_WIN32) = aio-win32.c
 gcov-files-test-aio-$(CONFIG_POSIX) = aio-posix.c
 check-unit-y += tests/test-thread-pool$(EXESUF)
 gcov-files-test-thread-pool-y = thread-pool.c
+check-unit-y += tests/test-cutils$(EXESUF)
+gcov-files-test-cutils-y += util/cutils.c
 
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
@@ -84,6 +86,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(block-obj-y) libqemuutil
 tests/test-aio$(EXESUF): tests/test-aio.o $(block-obj-y) libqemuutil.a libqemustub.a
 tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemuutil.a libqemustub.a
 tests/test-iov$(EXESUF): tests/test-iov.o libqemuutil.a
+tests/test-cutils$(EXESUF): tests/test-cutils.o util/cutils.o
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-cutils.c new file +183 -0

183 lines not on this page · raw message

util/cutils.c +76 -0

82 lines not on this page · raw message

Signature · 1 line
1.7.11.7
Reviewed-by on v1 1/8
#2 lore raw

Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions

Quoted 34 lines · Eduardo Habkost

There are lots of duplicate parsing code using strto*() in QEMU, and most of that code is broken in one way or another. Even the visitors code have duplicate integer parsing code[1]. This introduces functions to help parsing unsigned int values: parse_uint() and parse_uint_full().

Parsing functions for signed ints and floats will be submitted later.

parse_uint_full() has all the checks made by opts_type_uint64() at opts-visitor.c:

- Check for NULL (returns -EINVAL)
- Check for negative numbers (returns -ERANGE)
- Check for empty string (returns -EINVAL)
- Check for overflow or other errno values set by strtoll() (returns -errno)
- Check for end of string (reject invalid characters after number) (returns -EINVAL)

parse_uint() does everything above except checking for the end of the string, so callers can continue parsing the remainder of string after the number.

Unit tests included.

[1] string-input-visitor.c:parse_int() could use the same parsing code used by opts-visitor.c:opts_type_int(), instead of duplicating that logic.

---
Cc: Laszlo Ersek <[email protected]>
Cc: Eric Blake <[email protected]>
---

Quoted 4 lines

+++ b/tests/test-cutils.c

+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy

Interesting that you chose a BSD license instead of GPL, but doesn't affect my review.

Your test case lacks test of octal or hexadecimal input strings; is that worth adding?

Quoted 4 lines

+++ b/util/cutils.c

util/cutils.c +0 -0
@@ -270,6 +270,82 @@ int64_t strtosz(const char *nptr, char **end)
     return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
 }

util/cutils.c int64_t strtosz(const char *nptr, char **end) · line 281

Quoted 10 lines
util/cutils.c +9 -0
 
+/* Try to parse an unsigned integer
+ *
+ * Error checks done by the function:
+ * - NULL pointer will return -EINVAL.
+ * - Empty strings will return -EINVAL.
+ * - Overflow errors or other errno values  set by strtoull() will
+ *   return -errno (-ERANGE in case of overflow).
+ * - Differently from strtoull(), values starting with a minus sign are
+ *   rejected (returning -ERANGE).

Interesting that you chose to reject negative numbers, even though strtoull() is required to accept them. But you documented it and tested for it, so I can live with it.

util/cutils.c int64_t strtosz(const char *nptr, char **end) · line 312

Quoted 4 lines
util/cutils.c +4 -0
+    errno = 0;
+    val = strtoull(s, &endp, 0);
+    if (errno) {
+        r =  -errno;

Why two spaces?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions

Quoted 35 lines · Eric Blake
Quoted 33 lines · Eduardo Habkost

There are lots of duplicate parsing code using strto*() in QEMU, and most of that code is broken in one way or another. Even the visitors code have duplicate integer parsing code[1]. This introduces functions to help parsing unsigned int values: parse_uint() and parse_uint_full().

Parsing functions for signed ints and floats will be submitted later.

parse_uint_full() has all the checks made by opts_type_uint64() at opts-visitor.c:

- Check for NULL (returns -EINVAL)
- Check for negative numbers (returns -ERANGE)
- Check for empty string (returns -EINVAL)
- Check for overflow or other errno values set by strtoll() (returns -errno)
- Check for end of string (reject invalid characters after number) (returns -EINVAL)

parse_uint() does everything above except checking for the end of the string, so callers can continue parsing the remainder of string after the number.

Unit tests included.

[1] string-input-visitor.c:parse_int() could use the same parsing code used by opts-visitor.c:opts_type_int(), instead of duplicating that logic.

---
Cc: Laszlo Ersek <[email protected]>
Cc: Eric Blake <[email protected]>
---

Quoted 7 lines
Quoted 4 lines

+++ b/tests/test-cutils.c

+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy

Interesting that you chose a BSD license instead of GPL, but doesn't affect my review.

It's a personal preference, but by coincidence is also the license of cutils.c.

Quoted 3 lines

Your test case lacks test of octal or hexadecimal input strings; is that worth adding?

I believe I trust strtoll() enough to not require tests for those cases. Just like I didn't add tests involving trailing spaces or tabs, except on the negative-number test case.

Quoted 5 lines
Quoted 5 lines

+++ b/util/cutils.c

util/cutils.c +0 -0
@@ -270,6 +270,82 @@ int64_t strtosz(const char *nptr, char **end)
     return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
 }
 
Quoted 13 lines

util/cutils.c int64_t strtosz(const char *nptr, char **end) · line 281

Quoted 9 lines
util/cutils.c +9 -0
+/* Try to parse an unsigned integer
+ *
+ * Error checks done by the function:
+ * - NULL pointer will return -EINVAL.
+ * - Empty strings will return -EINVAL.
+ * - Overflow errors or other errno values  set by strtoull() will
+ *   return -errno (-ERANGE in case of overflow).
+ * - Differently from strtoull(), values starting with a minus sign are
+ *   rejected (returning -ERANGE).

Interesting that you chose to reject negative numbers, even though strtoull() is required to accept them. But you documented it and tested for it, so I can live with it.

I find this behavior of strtoull() confusing for users. If an option/field requires positive numbers, I expect QEMU to reject "-1" instead of silently converting it to ULLONG_MAX.

Quoted 7 lines

util/cutils.c int64_t strtosz(const char *nptr, char **end) · line 312

Quoted 5 lines
util/cutils.c +4 -0
+    errno = 0;
+    val = strtoull(s, &endp, 0);
+    if (errno) {
+        r =  -errno;

Why two spaces?

Typo. I will send a fixed version (keeping your Reviewed-by, if you don't mind).

Quoted 2 lines

Thanks!

Signature · 1 line
Eduardo
#4 lore raw

Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions

Quoted 8 lines · Eduardo Habkost
Quoted 7 lines · Eric Blake
Quoted 5 lines · Eduardo Habkost

There are lots of duplicate parsing code using strto*() in QEMU, and most of that code is broken in one way or another. Even the visitors code have duplicate integer parsing code[1]. This introduces functions to help parsing unsigned int values: parse_uint() and parse_uint_full().

Quoted 4 lines
Quoted 2 lines

Your test case lacks test of octal or hexadecimal input strings; is that worth adding?

I believe I trust strtoll() enough to not require tests for those cases.

But see my comments in 8/8 about whether it makes sense to add a 'base' parameter to let the caller choose whether they want to allow octal or require strict decimal parsing.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ r = -errno;

Why two spaces?

Typo. I will send a fixed version (keeping your Reviewed-by, if you don't mind).

No problem - I wouldn't have left a reviewed-by if I didn't think the changes couldn't be trivially fixed. On the other hand, you may have a non-trivial fix in the form of adding a base parameter...

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH 1/8] cutils: unsigned int parsing functions

Quoted 9 lines · Eric Blake
Quoted 8 lines · Eduardo Habkost
Quoted 7 lines · Eric Blake
Quoted 5 lines · Eduardo Habkost

There are lots of duplicate parsing code using strto*() in QEMU, and most of that code is broken in one way or another. Even the visitors code have duplicate integer parsing code[1]. This introduces functions to help parsing unsigned int values: parse_uint() and parse_uint_full().

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Your test case lacks test of octal or hexadecimal input strings; is that worth adding?

I believe I trust strtoll() enough to not require tests for those cases.

But see my comments in 8/8 about whether it makes sense to add a 'base' parameter to let the caller choose whether they want to allow octal or require strict decimal parsing.

In that case, I will add at least a test cases to check if "010" gives the expected result depending on the 'base' parameter.

Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

+ r = -errno;

Why two spaces?

Typo. I will send a fixed version (keeping your Reviewed-by, if you don't mind).

No problem - I wouldn't have left a reviewed-by if I didn't think the changes couldn't be trivially fixed. On the other hand, you may have a non-trivial fix in the form of adding a base parameter...

I was planning to keep the reviewed-by line only if fixing the trivial whitespace issue above. I won't keep it when adding the 'base' parameter, don't worry. :-)

Signature · 1 line
Eduardo
#6 lore raw

PATCH 2/8 vl.c: Fix off-by-one bug when handling "-numa node" argument

In reply to Eduardo Habkost

The numa_add() code was unconditionally adding 1 to the get_opt_name() return value, making it point after the end of the string if no ',' separator is present.

Example of weird behavior caused by the bug:

  $ qemu-img create -f qcow2 this-file-image-has,cpus=5,mem=1000,in-its-name.qcow2 5G
  Formatting 'this-file-image-has,cpus=5,mem=1000,in-its-name.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536
  $ ./x86_64-softmmu/qemu-system-x86_64 -S -monitor stdio -numa node 'this-file-image-has,cpus=5,mem=1000,in-its-name.qcow2'
  QEMU 1.3.50 monitor - type 'help' for more information
  (qemu) info numa
  1 nodes
  node 0 cpus: 0
  node 0 size: 1000 MB
  (qemu)

This changes the code to nove the pointer only if ',' is found.

---
 vl.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
vl.c +4 -1
index 15e0280..f29d926 100644
@@ -1250,7 +1250,10 @@ static void numa_add(const char *optarg)
 
     value = endvalue = 0ULL;
 
-    optarg = get_opt_name(option, 128, optarg, ',') + 1;
+    optarg = get_opt_name(option, 128, optarg, ',');
+    if (*optarg == ',') {
+        optarg++;
+    }
     if (!strcmp(option, "node")) {
         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
             nodenr = nb_numa_nodes;
Signature · 1 line
1.7.11.7
Reviewed-by on v1 2/8
#7 lore raw

Re: [Qemu-devel] [PATCH 2/8] vl.c: Fix off-by-one bug when handling "-numa node" argument

Quoted 16 lines · Eduardo Habkost

The numa_add() code was unconditionally adding 1 to the get_opt_name() return value, making it point after the end of the string if no ',' separator is present.

Example of weird behavior caused by the bug:

  $ qemu-img create -f qcow2 this-file-image-has,cpus=5,mem=1000,in-its-name.qcow2 5G
  Formatting 'this-file-image-has,cpus=5,mem=1000,in-its-name.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536
  $ ./x86_64-softmmu/qemu-system-x86_64 -S -monitor stdio -numa node 'this-file-image-has,cpus=5,mem=1000,in-its-name.qcow2'
  QEMU 1.3.50 monitor - type 'help' for more information
  (qemu) info numa
  1 nodes
  node 0 cpus: 0
  node 0 size: 1000 MB
  (qemu)
Quoted 1 line

This changes the code to nove the pointer only if ',' is found.

s/nove/move/

Quoted 5 lines
---
 vl.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#8 lore raw

PATCH 3/8 vl.c: Abort on unknown -numa option type

In reply to Eduardo Habkost

Abort in case an invalid -numa option is provided, instead of silently ignoring it.

---
 vl.c | 3 +++
 1 file changed, 3 insertions(+)
vl.c +3 -0
index f29d926..1cf8ba6 100644
@@ -1290,6 +1290,9 @@ static void numa_add(const char *optarg)
             bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
         }
         nb_numa_nodes++;
+    } else {
+        fprintf(stderr, "Invalid -numa option: %s\n", option);
+        exit(1);
     }
 }
 
Signature · 1 line
1.7.11.7
#9 lore raw

PATCH 4/8 vl.c: Check for NUMA node limit inside numa_add()

In reply to Eduardo Habkost

Instead of checking the limit before calling numa_add(), check the limit only when we already know we're going to add a new node.

---
Changes v2:
- Implement the change without adding numa_node_add() function

---
 vl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
vl.c +6 -4
index 1cf8ba6..dabbba1 100644
@@ -1255,6 +1255,12 @@ static void numa_add(const char *optarg)
         optarg++;
     }
     if (!strcmp(option, "node")) {
+
+        if (nb_numa_nodes >= MAX_NODES) {
+            fprintf(stderr, "qemu: too many NUMA nodes\n");
+            exit(1);
+        }
+
         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
             nodenr = nb_numa_nodes;
         } else {
@@ -2960,10 +2966,6 @@ int main(int argc, char **argv, char **envp)
                 }
                 break;
             case QEMU_OPTION_numa:
-                if (nb_numa_nodes >= MAX_NODES) {
-                    fprintf(stderr, "qemu: too many NUMA nodes\n");
-                    exit(1);
-                }
                 numa_add(optarg);
                 break;
             case QEMU_OPTION_display:
Signature · 1 line
1.7.11.7
Reviewed-by on v1 4/8
#10 lore raw

Re: [Qemu-devel] [PATCH 4/8] vl.c: Check for NUMA node limit inside numa_add()

Quoted 10 lines · Eduardo Habkost

Instead of checking the limit before calling numa_add(), check the limit only when we already know we're going to add a new node.

---
Changes v2:
- Implement the change without adding numa_node_add() function

---
 vl.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#11 lore raw

PATCH 5/8 vl.c: numa_add(): Validate nodeid before using it

In reply to Eduardo Habkost

Without this check, QEMU will corrupt memory if a too-large nodeid is provided in the command-line. e.g.:

-numa node,mem=...,cpus=...,nodeid=65

This changes nodenr to unsigned long long, to avoid integer conversion issues when converting the strtoull() result to int.

---
Changes v2:
- Implement change without creation of numa_node_add() function

---
 vl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
vl.c +6 -1
index dabbba1..3637a39 100644
@@ -1246,7 +1246,7 @@ static void numa_add(const char *optarg)
     char option[128];
     char *endptr;
     unsigned long long value, endvalue;
-    int nodenr;
+    unsigned long long nodenr;
 
     value = endvalue = 0ULL;
 
@@ -1267,6 +1267,11 @@ static void numa_add(const char *optarg)
             nodenr = strtoull(option, NULL, 10);
         }
 
+        if (nodenr >= MAX_NODES) {
+            fprintf(stderr, "qemu: invalid NUMA nodeid: %d\n", nodenr);
+            exit(1);
+        }
+
         if (get_param_value(option, 128, "mem", optarg) == 0) {
             node_mem[nodenr] = 0;
         } else {
Signature · 1 line
1.7.11.7
#12 lore raw

Re: [Qemu-devel] [PATCH 5/8] vl.c: numa_add(): Validate nodeid before using it

Quoted 14 lines · Eduardo Habkost

Without this check, QEMU will corrupt memory if a too-large nodeid is provided in the command-line. e.g.:

-numa node,mem=...,cpus=...,nodeid=65

This changes nodenr to unsigned long long, to avoid integer conversion issues when converting the strtoull() result to int.

---
Changes v2:
- Implement change without creation of numa_node_add() function

vl.c static void numa_add(const char *optarg) · line 1271

Quoted 2 lines
vl.c +2 -0
+        if (nodenr >= MAX_NODES) {
+            fprintf(stderr, "qemu: invalid NUMA nodeid: %d\n", nodenr);

%lld (I see you later fixed that in 6/8, but you should rebase that hunk into the patch that first needs it).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#13 lore raw

PATCH 6/8 vl.c: Use parse_uint_full() for NUMA nodeid

In reply to Eduardo Habkost

This should catch many kinds of errors that the current code wasn't checking for:

- Values that can't be parsed as a number
- Negative values
- Overflow
- Empty string

---
Cc: Eric Blake <[email protected]>

---
 vl.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
vl.c +5 -2
index 3637a39..43ce607 100644
@@ -1264,11 +1264,14 @@ static void numa_add(const char *optarg)
         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
             nodenr = nb_numa_nodes;
         } else {
-            nodenr = strtoull(option, NULL, 10);
+            if (parse_uint_full(option, &nodenr) < 0) {
+                fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
+                exit(1);
+            }
         }
 
         if (nodenr >= MAX_NODES) {
-            fprintf(stderr, "qemu: invalid NUMA nodeid: %d\n", nodenr);
+            fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
             exit(1);
         }
 
Signature · 1 line
1.7.11.7
#14 lore raw

Re: [Qemu-devel] [PATCH 6/8] vl.c: Use parse_uint_full() for NUMA nodeid

Quoted 18 lines · Eduardo Habkost

This should catch many kinds of errors that the current code wasn't checking for:

- Values that can't be parsed as a number
- Negative values
- Overflow
- Empty string

---
Cc: Eric Blake <[email protected]>

---
 vl.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

+++ b/vl.c

vl.c static void numa_add(const char *optarg) · line 1267

Quoted 5 lines
vl.c +1 -1
@@ -1264,11 +1264,14 @@ static void numa_add(const char *optarg)
         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
             nodenr = nb_numa_nodes;
         } else {
-            nodenr = strtoull(option, NULL, 10);
+            if (parse_uint_full(option, &nodenr) < 0) {

This allows a user to pass octal or hex numbers, where previously it was forced to be decimal. That means 'nodeid=010' is now '8' instead of '10'; is that intentional?

vl.c static void numa_add(const char *optarg) · line 1274

Quoted 4 lines
vl.c +1 -1
         if (nodenr >= MAX_NODES) {
-            fprintf(stderr, "qemu: invalid NUMA nodeid: %d\n", nodenr);
+            fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);

Already mentions that this part belongs in 5/8.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#15 lore raw

Re: [Qemu-devel] [PATCH 6/8] vl.c: Use parse_uint_full() for NUMA nodeid

Quoted 17 lines · Eric Blake
Quoted 15 lines · Eduardo Habkost

This should catch many kinds of errors that the current code wasn't checking for:

- Values that can't be parsed as a number
- Negative values
- Overflow
- Empty string

---
Cc: Eric Blake <[email protected]>

---
 vl.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 11 lines

vl.c static void numa_add(const char *optarg) · line 1267

Quoted 7 lines

+++ b/vl.c

vl.c +1 -1
@@ -1264,11 +1264,14 @@ static void numa_add(const char *optarg)
         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
             nodenr = nb_numa_nodes;
         } else {
-            nodenr = strtoull(option, NULL, 10);
+            if (parse_uint_full(option, &nodenr) < 0) {

This allows a user to pass octal or hex numbers, where previously it was forced to be decimal. That means 'nodeid=010' is now '8' instead of '10'; is that intentional?

It wasn't intentional, thanks for catching it!

However, the existing option parsing infra-structure (parse_option_number() at qemu-option.c and opts_type_uint64() at opts-visitor.c) use 0 as the 'base' parameter. So when we convert this code to use that infra-structure, it will end up accepting hex and octal numbers as well.

So I believe I will fix it this way:

- Add 'base' parameter to parse_uint*()
- Use base=10 when parsing "-numa" (keeping "nodeid=010" working, just in case)
- Use the QemuOpts number parser when introducing the "numa-node" config section, and manually parse/convert the (then obsolete) "-numa node,..." option to "numa-node"

Or, we could simply change the meaning of "nodeid=010" and document it in the commit message and QEMU ChageLog. Which option do the QEMU maintainers prefer?

Quoted 7 lines

vl.c static void numa_add(const char *optarg) · line 1274

Quoted 5 lines
vl.c +1 -1
         if (nodenr >= MAX_NODES) {
-            fprintf(stderr, "qemu: invalid NUMA nodeid: %d\n", nodenr);
+            fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);

Already mentions that this part belongs in 5/8.

I will fix it. Thanks.

Signature · 1 line
Eduardo
#16 lore raw

Re: [Qemu-devel] [PATCH 6/8] vl.c: Use parse_uint_full() for NUMA nodeid

Quoted 4 lines · Eduardo Habkost

Or, we could simply change the meaning of "nodeid=010" and document it in the commit message and QEMU ChageLog. Which option do the QEMU maintainers prefer?

I'm not a maintainer, so anyone is free to overrule me; but I'm personally fine with just documenting that the semantic change is intentional. It's enough of a corner case that probably no one was relying on leading 0 still giving decimal parsing (libvirt certainly wasn't relying on it], and the flexibility of allowing hex parsing is useful. But do document it in the commit message; a silent change is not fair.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#17 lore raw

Re: [Qemu-devel] [PATCH 6/8] vl.c: Use parse_uint_full() for NUMA nodeid

Quoted 6 lines · Eric Blake
Quoted 4 lines · Eduardo Habkost

Or, we could simply change the meaning of "nodeid=010" and document it in the commit message and QEMU ChageLog. Which option do the QEMU maintainers prefer?

Quoted 7 lines

I'm not a maintainer, so anyone is free to overrule me; but I'm personally fine with just documenting that the semantic change is intentional. It's enough of a corner case that probably no one was relying on leading 0 still giving decimal parsing (libvirt certainly wasn't relying on it], and the flexibility of allowing hex parsing is useful. But do document it in the commit message; a silent change is not fair.

I will add a 'base' parameter to parse_uint*() anyway, as it may be useful somewhere else. Then keeping the existing behavior will be trivial, so I will keep it by now.

Later, when changing the code to use QemuOpts, we may decide to change the behavior (to keep the code simpler) and document the semantic change.

Signature · 1 line
Eduardo
#18 lore raw

PATCH 7/8 vl.c: Extract -numa "cpus" parsing to separate function

In reply to Eduardo Habkost

This will make it easier to refactor that code later.

---
Changes v2:
- Implemented change without creation of numa_node_add() function

---
 vl.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)
vl.c +23 -18
index 43ce607..98cf9f0 100644
@@ -1241,15 +1241,34 @@ char *get_boot_devices_list(uint32_t *size)
     return list;
 }
 
+static void numa_node_parse_cpus(int nodenr, const char *cpus)
+{
+    char *endptr;
+    unsigned long long value, endvalue;
+
+    value = strtoull(cpus, &endptr, 10);
+    if (*endptr == '-') {
+        endvalue = strtoull(endptr+1, &endptr, 10);
+    } else {
+        endvalue = value;
+    }
+
+    if (!(endvalue < MAX_CPUMASK_BITS)) {
+        endvalue = MAX_CPUMASK_BITS - 1;
+        fprintf(stderr,
+            "A max of %d CPUs are supported in a guest\n",
+             MAX_CPUMASK_BITS);
+    }
+
+    bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
+}
+
 static void numa_add(const char *optarg)
 {
     char option[128];
     char *endptr;
-    unsigned long long value, endvalue;
     unsigned long long nodenr;
 
-    value = endvalue = 0ULL;
-
     optarg = get_opt_name(option, 128, optarg, ',');
     if (*optarg == ',') {
         optarg++;
@@ -1287,21 +1306,7 @@ static void numa_add(const char *optarg)
             node_mem[nodenr] = sval;
         }
         if (get_param_value(option, 128, "cpus", optarg) != 0) {
-            value = strtoull(option, &endptr, 10);
-            if (*endptr == '-') {
-                endvalue = strtoull(endptr+1, &endptr, 10);
-            } else {
-                endvalue = value;
-            }
-
-            if (!(endvalue < MAX_CPUMASK_BITS)) {
-                endvalue = MAX_CPUMASK_BITS - 1;
-                fprintf(stderr,
-                    "A max of %d CPUs are supported in a guest\n",
-                     MAX_CPUMASK_BITS);
-            }
-
-            bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
+            numa_node_parse_cpus(nodenr, option);
         }
         nb_numa_nodes++;
     } else {
Signature · 1 line
1.7.11.7
Reviewed-by on v1 7/8
#19 lore raw

Re: [Qemu-devel] [PATCH 7/8] vl.c: Extract -numa "cpus" parsing to separate function

Quoted 10 lines · Eduardo Habkost

This will make it easier to refactor that code later.

---
Changes v2:
- Implemented change without creation of numa_node_add() function

---
 vl.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#20 lore raw

PATCH 8/8 vl.c: validate -numa "cpus" parameter properly

In reply to Eduardo Habkost

- Accept empty strings without aborting
- Use parse_uint*() to parse numbers
- Abort if anything except '-' or end-of-string is found after the first number.
- Check for endvalue < value

Also change the MAX_CPUMASK_BITS warning message from "A max of %d CPUs are supported in a guest" to "qemu: NUMA: A max of %d VCPUs are supported".

---
Cc: Eric Blake <[email protected]>

---
 vl.c | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)
vl.c +27 -5
index 98cf9f0..2d156e2 100644
@@ -1246,21 +1246,43 @@ static void numa_node_parse_cpus(int nodenr, const char *cpus)
     char *endptr;
     unsigned long long value, endvalue;
 
-    value = strtoull(cpus, &endptr, 10);
+    /* Empty CPU range strings will be considered valid, they will simply
+     * not set any bit in the CPU bitmap.
+     */
+    if (!*cpus) {
+        return;
+    }
+
+    if (parse_uint(cpus, &value, &endptr) < 0) {
+        goto error;
+    }
     if (*endptr == '-') {
-        endvalue = strtoull(endptr+1, &endptr, 10);
-    } else {
+        if (parse_uint_full(endptr + 1, &endvalue) < 0) {
+            goto error;
+        }
+    } else if (*endptr == '\0') {
         endvalue = value;
+    } else {
+        goto error;
     }
 
-    if (!(endvalue < MAX_CPUMASK_BITS)) {
+    if (endvalue >= MAX_CPUMASK_BITS) {
         endvalue = MAX_CPUMASK_BITS - 1;
         fprintf(stderr,
-            "A max of %d CPUs are supported in a guest\n",
+            "qemu: NUMA: A max of %d VCPUs are supported\n",
              MAX_CPUMASK_BITS);
     }
 
+    if (endvalue < value) {
+        goto error;
+    }
+
     bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
+    return;
+
+error:
+    fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
+    exit(1);
 }
 
 static void numa_add(const char *optarg)
Signature · 1 line
1.7.11.7
#21 lore raw

Re: [Qemu-devel] [PATCH 8/8] vl.c: validate -numa "cpus" parameter properly

Quoted 19 lines · Eduardo Habkost

- Accept empty strings without aborting
- Use parse_uint*() to parse numbers
- Abort if anything except '-' or end-of-string is found after the first number.
- Check for endvalue < value

Also change the MAX_CPUMASK_BITS warning message from "A max of %d CPUs are supported in a guest" to "qemu: NUMA: A max of %d VCPUs are supported".

---
Cc: Eric Blake <[email protected]>

---
 vl.c | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)

vl.c static void numa_node_parse_cpus(int nodenr, const char *cpus) · line 1253

Quoted 6 lines
vl.c +5 -1
-    value = strtoull(cpus, &endptr, 10);
+    /* Empty CPU range strings will be considered valid, they will simply
+     * not set any bit in the CPU bitmap.
+     */
+    if (!*cpus) {
+        return;

Does the code behave correctly when there are no bits in the CPU bitmap, or do you require that at least one bit be set?

vl.c static void numa_node_parse_cpus(int nodenr, const char *cpus) · line 1257

Quoted 4 lines
vl.c +4 -0
+    }
+
+    if (parse_uint(cpus, &value, &endptr) < 0) {
+        goto error;

Again, another case of accepting octal where you used to only accept binary; if the change of interpretation of 010 is intentional, it would be worth documenting in the commit message. Otherwise, it might be worth refactoring 1/8 to add a 'base' parameter to parse_uint[_full] to allow the caller to control whether they want base 0 or base 10.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#22 lore raw

Re: [Qemu-devel] [PATCH 8/8] vl.c: validate -numa "cpus" parameter properly

Quoted 27 lines · Eric Blake
Quoted 19 lines · Eduardo Habkost

- Accept empty strings without aborting
- Use parse_uint*() to parse numbers
- Abort if anything except '-' or end-of-string is found after the first number.
- Check for endvalue < value

Also change the MAX_CPUMASK_BITS warning message from "A max of %d CPUs are supported in a guest" to "qemu: NUMA: A max of %d VCPUs are supported".

---
Cc: Eric Blake <[email protected]>

---
 vl.c | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)

vl.c static void numa_node_parse_cpus(int nodenr, const char *cpus) · line 1253

Quoted 6 lines
vl.c +5 -1
-    value = strtoull(cpus, &endptr, 10);
+    /* Empty CPU range strings will be considered valid, they will simply
+     * not set any bit in the CPU bitmap.
+     */
+    if (!*cpus) {
+        return;
Quoted 2 lines

Does the code behave correctly when there are no bits in the CPU bitmap, or do you require that at least one bit be set?

If no bits are set on any node, QEMU will divide the VCPUs equally through all nodes. More exactly, each VCPU (i) would go to NUMA node (i % nb_numa_nodes).

It is also possible to build ACPI tables where a node have no CPUs, just RAM regions. It doesn't mean it is a good idea or that guest OSes will be able to handle it properly, but QEMU will do exactly what you asked for.

Quoted 11 lines

vl.c static void numa_node_parse_cpus(int nodenr, const char *cpus) · line 1257

Quoted 5 lines
vl.c +4 -0
+    }
+
+    if (parse_uint(cpus, &value, &endptr) < 0) {
+        goto error;

Again, another case of accepting octal where you used to only accept binary; if the change of interpretation of 010 is intentional, it would be worth documenting in the commit message. Otherwise, it might be worth refactoring 1/8 to add a 'base' parameter to parse_uint[_full] to allow the caller to control whether they want base 0 or base 10.

True. Thanks for catching it, again. I plan to take the same approach proposed on my reply to patch 6/8.

Signature · 1 line
Eduardo