Skip to contentSkip to conversation

-numa option parsing fixes & improvements

PATCH In review Eduardo Habkost 2 participants 15 messages Jan 11, 2013 – Jan 14, 2013

lore
Share & tools
lore
#0 lore raw

cover -numa option parsing fixes & improvements

This series contains the following:

* Patches 1-7 are multiple bug fixes to the current code
* Patch 8 introduce a feature that libvirt requires since a long time, and even tries to use it today (in a way that doesn't work, using the "-numa node,cpus=1,2,3,4" format): having non-contiguous CPU ranges assigned to a NUMA node.

The last 2 patches I am sending as RFCs:

* Patch 9 makes the "-numa" option deprecated and introduces a "-numa-node" command-line and config file option.
* Patch 10 adds a small hack to the (now deprecated) "-numa" option, that makes the "cpus=1,2,3,4" format currently used by libvirt work.

Eduardo Habkost (10):
vl.c: Fix off-by-one bug when handling "-numa node" argument vl.c: Abort on unknown -numa option type
vl.c: Isolate code specific to "-numa node" option type vl.c: Check for NUMA node limit inside numa_node_add() vl.c: Extract -numa "cpus" parsing to separate function vl.c: handle invalid NUMA CPU ranges properly vl.c: numa_add_node(): Validate nodeid before using it vl.c: Support multiple CPU ranges on -numa option vl.c: Introduce QemuOpts-friendly "-numa-node" config option vl.c: Handle legacy "-numa node,cpus=A,B,C,D" format

 qemu-config.c   |  25 +++++++
 qemu-options.hx |  50 +++++++++++++-
 vl.c            | 204 ++++++++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 240 insertions(+), 39 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

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

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 e5da31c..93fde36 100644
@@ -1061,7 +1061,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
#2 lore raw

PATCH 2/10 vl.c: Abort on unknown -numa option type

In reply to Eduardo Habkost

Instead of silently ignoring them, abort in case an invalid -numa option is provided.

---
 vl.c | 3 +++
 1 file changed, 3 insertions(+)
vl.c +3 -0
index 93fde36..042cc7f 100644
@@ -1101,6 +1101,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
#3 lore raw

PATCH 3/10 vl.c: Isolate code specific to "-numa node" option type

In reply to Eduardo Habkost

Extract the code that's specific for the "node" -numa option type (the only one, today) to a separate function.

The extracted code will eventually become a function specific for a "numa-node" config section, independent from the numa_add() code.

---
 vl.c | 75 +++++++++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 41 insertions(+), 34 deletions(-)
vl.c +41 -34
index 042cc7f..94cc6fd 100644
@@ -1052,7 +1052,7 @@ char *get_boot_devices_list(uint32_t *size)
     return list;
 }
 
-static void numa_add(const char *optarg)
+static void numa_node_add(const char *optarg)
 {
     char option[128];
     char *endptr;
@@ -1061,46 +1061,53 @@ static void numa_add(const char *optarg)
 
     value = endvalue = 0ULL;
 
-    optarg = get_opt_name(option, 128, optarg, ',');
-    if (*optarg == ',') {
-        optarg++;
+    if (get_param_value(option, 128, "nodeid", optarg) == 0) {
+        nodenr = nb_numa_nodes;
+    } else {
+        nodenr = strtoull(option, NULL, 10);
     }
-    if (!strcmp(option, "node")) {
-        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
-            nodenr = nb_numa_nodes;
+
+    if (get_param_value(option, 128, "mem", optarg) == 0) {
+        node_mem[nodenr] = 0;
+    } else {
+        int64_t sval;
+        sval = strtosz(option, &endptr);
+        if (sval < 0 || *endptr) {
+            fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
+            exit(1);
+        }
+        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 {
-            nodenr = strtoull(option, NULL, 10);
+            endvalue = value;
         }
 
-        if (get_param_value(option, 128, "mem", optarg) == 0) {
-            node_mem[nodenr] = 0;
-        } else {
-            int64_t sval;
-            sval = strtosz(option, &endptr);
-            if (sval < 0 || *endptr) {
-                fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
-                exit(1);
-            }
-            node_mem[nodenr] = sval;
+        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);
         }
-        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);
+    }
+    nb_numa_nodes++;
+}
 
-            bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
-        }
-        nb_numa_nodes++;
+static void numa_add(const char *optarg)
+{
+    char option[128];
+
+    optarg = get_opt_name(option, 128, optarg, ',');
+    if (*optarg == ',') {
+        optarg++;
+    }
+    if (!strcmp(option, "node")) {
+        numa_node_add(optarg);
     } else {
         fprintf(stderr, "Invalid -numa option: %s\n", option);
         exit(1);
Signature · 1 line
1.7.11.7
#4 lore raw

Re: [Qemu-devel] [libvirt] [PATCH 03/10] vl.c: Isolate code specific to "-numa node" option type

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

Quoted 11 lines · Eduardo Habkost

Extract the code that's specific for the "node" -numa option type (the only one, today) to a separate function.

The extracted code will eventually become a function specific for a "numa-node" config section, independent from the numa_add() code.

vl.c +4 -0
+    if (get_param_value(option, 128, "nodeid", optarg) == 0) {
+        nodenr = nb_numa_nodes;
+    } else {
+        nodenr = strtoull(option, NULL, 10);
     }

strtoull() needs additional error checking after the fact, to make sure I didn't pass an empty string, trailing garbage, or so many digits I triggered overflow.

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

Quoted 8 lines
vl.c +5 -1
+    if (get_param_value(option, 128, "cpus", optarg) != 0) {
+        value = strtoull(option, &endptr, 10);
+        if (*endptr == '-') {
+            endvalue = strtoull(endptr+1, &endptr, 10);
         } else {
-            nodenr = strtoull(option, NULL, 10);
+            endvalue = value;
         }

More uses of strtoull() that aren't guarding against all possible errors.

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

Re: [Qemu-devel] [libvirt] [PATCH 03/10] vl.c: Isolate code specific to "-numa node" option type

Quoted 27 lines · Eric Blake

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

Quoted 11 lines · Eduardo Habkost

Extract the code that's specific for the "node" -numa option type (the only one, today) to a separate function.

The extracted code will eventually become a function specific for a "numa-node" config section, independent from the numa_add() code.

vl.c +4 -0
+    if (get_param_value(option, 128, "nodeid", optarg) == 0) {
+        nodenr = nb_numa_nodes;
+    } else {
+        nodenr = strtoull(option, NULL, 10);
     }

strtoull() needs additional error checking after the fact, to make sure I didn't pass an empty string, trailing garbage, or so many digits I triggered overflow.

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

Quoted 8 lines
vl.c +5 -1
+    if (get_param_value(option, 128, "cpus", optarg) != 0) {
+        value = strtoull(option, &endptr, 10);
+        if (*endptr == '-') {
+            endvalue = strtoull(endptr+1, &endptr, 10);
         } else {
-            nodenr = strtoull(option, NULL, 10);
+            endvalue = value;
         }
Quoted 1 line

More uses of strtoull() that aren't guarding against all possible errors.

All this code gets replaced in patch 09/10. The only remaining strtoull() call gets error-checking at patch 06/10.

Signature · 1 line
Eduardo
#6 lore raw

PATCH 4/10 vl.c: Check for NUMA node limit inside numa_node_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.

---
 vl.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
vl.c +5 -4
index 94cc6fd..2c3bbb9 100644
@@ -1061,6 +1061,11 @@ static void numa_node_add(const char *optarg)
 
     value = endvalue = 0ULL;
 
+    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 {
@@ -2762,10 +2767,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
#7 lore raw

PATCH 5/10 vl.c: Extract -numa "cpus" parsing to separate function

In reply to Eduardo Habkost

This will make it easier to refactor that code later.

---
 vl.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
vl.c +22 -18
index 2c3bbb9..03a826e 100644
@@ -1052,15 +1052,33 @@ 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_node_add(const char *optarg)
 {
     char option[128];
     char *endptr;
-    unsigned long long value, endvalue;
     int nodenr;
 
-    value = endvalue = 0ULL;
-
     if (nb_numa_nodes >= MAX_NODES) {
         fprintf(stderr, "qemu: too many NUMA nodes\n");
         exit(1);
@@ -1084,21 +1102,7 @@ static void numa_node_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++;
 }
Signature · 1 line
1.7.11.7
#8 lore raw

PATCH 6/10 vl.c: handle invalid NUMA CPU ranges properly

In reply to Eduardo Habkost

Add checks for the following cases:

* Empty string: will be ignored and won't set any CPU bitmap, parser won't abort.
* Missing end value after "-": parser will abort.
* Extra characters after a valid CPU range: parser will abort.
* "N-M" string where M < N: parser will abort.

---
 vl.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
vl.c +23 -1
index 03a826e..19010fa 100644
@@ -1057,13 +1057,30 @@ static void numa_node_parse_cpus(int nodenr, const char *cpus)
     char *endptr;
     unsigned long long value, endvalue;
 
+    /* Empty strings will be ignored, and not considered an error */
+    if (!*cpus) {
+        return;
+    }
+
     value = strtoull(cpus, &endptr, 10);
     if (*endptr == '-') {
-        endvalue = strtoull(endptr+1, &endptr, 10);
+        endptr++;
+        if (!*endptr) {
+            goto error;
+        }
+        endvalue = strtoull(endptr, &endptr, 10);
     } else {
         endvalue = value;
     }
 
+    if (*endptr != '\0')  {
+        goto error;
+    }
+
+    if (endvalue < value) {
+        goto error;
+    }
+
     if (!(endvalue < MAX_CPUMASK_BITS)) {
         endvalue = MAX_CPUMASK_BITS - 1;
         fprintf(stderr, "A max of %d CPUs are supported in a guest\n",
@@ -1071,6 +1088,11 @@ static void numa_node_parse_cpus(int nodenr, const char *cpus)
     }
 
     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_node_add(const char *optarg)
Signature · 1 line
1.7.11.7
#9 lore raw

Re: [Qemu-devel] [libvirt] [PATCH 06/10] vl.c: handle invalid NUMA CPU ranges properly

Quoted 11 lines · Eduardo Habkost

Add checks for the following cases:

* Empty string: will be ignored and won't set any CPU bitmap, parser won't abort.
* Missing end value after "-": parser will abort.
* Extra characters after a valid CPU range: parser will abort.
* "N-M" string where M < N: parser will abort.

     value = strtoull(cpus, &endptr, 10);
     if (*endptr == '-') {

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

Quoted 9 lines
vl.c +5 -1
-        endvalue = strtoull(endptr+1, &endptr, 10);
+        endptr++;
+        if (!*endptr) {
+            goto error;
+        }
+        endvalue = strtoull(endptr, &endptr, 10);
     } else {
         endvalue = value;
     }

Still missing a check for '-numa=-2' with no number on the left of '-', as well as missing a check for overflow for -numa=999999999999999999999999

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

Re: [Qemu-devel] [libvirt] [PATCH 06/10] vl.c: handle invalid NUMA CPU ranges properly

Quoted 22 lines · Eric Blake
Quoted 11 lines · Eduardo Habkost

Add checks for the following cases:

* Empty string: will be ignored and won't set any CPU bitmap, parser won't abort.
* Missing end value after "-": parser will abort.
* Extra characters after a valid CPU range: parser will abort.
* "N-M" string where M < N: parser will abort.

     value = strtoull(cpus, &endptr, 10);
     if (*endptr == '-') {

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

Quoted 9 lines
vl.c +5 -1
-        endvalue = strtoull(endptr+1, &endptr, 10);
+        endptr++;
+        if (!*endptr) {
+            goto error;
+        }
+        endvalue = strtoull(endptr, &endptr, 10);
     } else {
         endvalue = value;
     }
Quoted 2 lines

Still missing a check for '-numa=-2' with no number on the left of '-', as well as missing a check for overflow for -numa=999999999999999999999999

Thanks!

I will fix and submit v2 of this patch.

Signature · 1 line
Eduardo
#11 lore raw

PATCH 7/10 vl.c: numa_add_node(): Validate nodeid before using it

In reply to Eduardo Habkost

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

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

---
 vl.c | 5 +++++
 1 file changed, 5 insertions(+)
vl.c +5 -0
index 19010fa..31175f6 100644
@@ -1112,6 +1112,11 @@ static void numa_node_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

PATCH 8/10 vl.c: Support multiple CPU ranges on -numa option

In reply to Eduardo Habkost

This allows "," or ";" to be used a separator between each CPU range. Note that commas inside key=value command-line options have to be escaped using ",,", so the command-line will look like:

-numa node,cpus=A,,B,,C,,D

or:

-numa node,cpus=A;B;C;D

Note that the following format, currently used by libvirt:

-numa nodes,cpus=A,B,C,D

will _not_ work yet, as "," is the option separator for the command-line option parser, and it will require changing the -numa option parsing code to handle "cpus" as a special case.

---
 vl.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
vl.c +13 -1
index 31175f6..d7337c1 100644
@@ -1052,7 +1052,7 @@ char *get_boot_devices_list(uint32_t *size)
     return list;
 }
 
-static void numa_node_parse_cpus(int nodenr, const char *cpus)
+static void numa_node_parse_cpu_range(int nodenr, const char *cpus)
 {
     char *endptr;
     unsigned long long value, endvalue;
@@ -1095,6 +1095,18 @@ error:
     exit(1);
 }
 
+static void numa_node_parse_cpus(int nodenr, const char *option)
+{
+    char **parts;
+    int i;
+
+    parts = g_strsplit_set(option, ",;", 0);
+    for (i = 0; parts[i]; i++) {
+        numa_node_parse_cpu_range(nodenr, parts[i]);
+    }
+    g_strfreev(parts);
+}
+
 static void numa_node_add(const char *optarg)
 {
     char option[128];
Signature · 1 line
1.7.11.7
#13 lore raw

RFC 9/10 vl.c: Introduce QemuOpts-friendly "-numa-node" config option

In reply to Eduardo Habkost

This introduces both a "-numa-node" command-line option that is parsed using a pure qemu_opts_parse() call, and a "numa-node" config file section.

As the -numa option has a non-standard syntax, parse it manually and translate it "numa-node" config options.

---
 qemu-config.c   |  25 ++++++++++++
 qemu-options.hx |  50 ++++++++++++++++++++++-
 vl.c            | 120 ++++++++++++++++++++++++++++++++++++++++++--------------
 3 files changed, 164 insertions(+), 31 deletions(-)
qemu-config.c +25 -0
index 2188c3e..601dcda 100644
@@ -647,6 +647,30 @@ static QemuOptsList qemu_object_opts = {
     },
 };
 
+static QemuOptsList qemu_numa_node_opts = {
+    .name = "numa-node",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_node_opts.head),
+    .implied_opt_name = "type",
+    .desc = {
+        {
+            .name = "cpus",
+            .type = QEMU_OPT_STRING,
+            .help = "CPU range in the format N[-M]",
+        },
+        {
+            .name = "mem",
+            .type = QEMU_OPT_STRING,
+            .help = "RAM size for node, in MB",
+        },
+        {
+            .name = "nodeid",
+            .type = QEMU_OPT_NUMBER,
+            .help = "Node ID",
+        },
+        { /* end of list */ }
+    },
+};
+
 static QemuOptsList *vm_config_groups[32] = {
     &qemu_drive_opts,
     &qemu_chardev_opts,
@@ -664,6 +688,7 @@ static QemuOptsList *vm_config_groups[32] = {
     &qemu_sandbox_opts,
     &qemu_add_fd_opts,
     &qemu_object_opts,
+    &qemu_numa_node_opts,
     NULL,
 };
 
qemu-options.hx +48 -2

56 lines not on this page · raw message

vl.c +91 -29

179 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#14 lore raw

RFC 10/10 vl.c: Handle legacy "-numa node, cpus=A, B, C, D" format

In reply to Eduardo Habkost

As libvirt already uses this format and expects it to work, add a small hack to the legacy -numa option parsing code to make the "cpus=A,B,C,D" format work.

---
 vl.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
vl.c +11 -0
index 14bf9b6..cf30d44 100644
@@ -1194,6 +1194,17 @@ static void parse_legacy_numa_node(const char *optarg)
             p++;
         }
         qemu_opt_set(opts, option, value);
+
+        /* special case for "cpus", as it can contain "," */
+        if (!strcmp(option, "cpus")) {
+            while (isdigit(*p)) {
+                p = get_opt_value(value, 128, p);
+                if (*p == ',') {
+                    p++;
+                }
+                qemu_opt_set(opts, "cpus", value);
+            }
+        }
     }
 }
 
Signature · 1 line
1.7.11.7