Skip to contentSkip to conversation

convert -m to QemuOpts

PATCH Superseded Igor Mammedov 3 participants 12 messages Feb 6, 2014 – Feb 26, 2014

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover convert -m to QemuOpts

changes since RFC:
- resolved conflict with error_abort change
in qemu_get_machine_opts()

Igor Mammedov (1):
vl: convert -m to QemuOpts

Paolo Bonzini (1):
QemuOpts: introduce qemu_find_opts_singleton

 include/qemu/config-file.h |    2 +
 qemu-options.hx            |    7 +++-
 util/qemu-config.c         |   14 +++++++++
 vl.c                       |   64 +++++++++++++++++++++++++++++--------------
 4 files changed, 64 insertions(+), 23 deletions(-)
#1 lore raw

PATCH 1/2 QemuOpts: introduce qemu_find_opts_singleton

From: Paolo Bonzini <[email protected]>

---
 include/qemu/config-file.h |    2 ++
 util/qemu-config.c         |   14 ++++++++++++++
 vl.c                       |   11 +----------
 3 files changed, 17 insertions(+), 10 deletions(-)
include/qemu/config-file.h +2 -0
index dbd97c4..d4ba20e 100644
@@ -8,6 +8,8 @@
 
 QemuOptsList *qemu_find_opts(const char *group);
 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp);
+QemuOpts *qemu_find_opts_singleton(const char *group);
+
 void qemu_add_opts(QemuOptsList *list);
 void qemu_add_drive_opts(QemuOptsList *list);
 int qemu_set_option(const char *str);
util/qemu-config.c +14 -0
index 9298f55..9dfacbc 100644
@@ -39,6 +39,20 @@ QemuOptsList *qemu_find_opts(const char *group)
     return ret;
 }
 
+QemuOpts *qemu_find_opts_singleton(const char *group)
+{
+    QemuOptsList *list;
+    QemuOpts *opts;
+
+    list = qemu_find_opts(group);
+    assert(list);
+    opts = qemu_opts_find(list, NULL);
+    if (!opts) {
+        opts = qemu_opts_create(list, NULL, 0, &error_abort);
+    }
+    return opts;
+}
+
 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
 {
     CommandLineParameterInfoList *param_list = NULL, *entry;
vl.c +1 -10
index 383be1b..7f2595c 100644
@@ -539,16 +539,7 @@ static QemuOptsList qemu_msg_opts = {
  */
 QemuOpts *qemu_get_machine_opts(void)
 {
-    QemuOptsList *list;
-    QemuOpts *opts;
-
-    list = qemu_find_opts("machine");
-    assert(list);
-    opts = qemu_opts_find(list, NULL);
-    if (!opts) {
-        opts = qemu_opts_create(list, NULL, 0, &error_abort);
-    }
-    return opts;
+    return qemu_find_opts_singleton("machine");
 }
 
 const char *qemu_get_vm_name(void)
Signature · 1 line
1.7.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] QemuOpts: introduce qemu_find_opts_singleton

comments below

Quoted 34 lines · Igor Mammedov

From: Paolo Bonzini <[email protected]>

---
 include/qemu/config-file.h |    2 ++
 util/qemu-config.c         |   14 ++++++++++++++
 vl.c                       |   11 +----------
 3 files changed, 17 insertions(+), 10 deletions(-)
include/qemu/config-file.h +2 -0
index dbd97c4..d4ba20e 100644
@@ -8,6 +8,8 @@
 
 QemuOptsList *qemu_find_opts(const char *group);
 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp);
+QemuOpts *qemu_find_opts_singleton(const char *group);
+
 void qemu_add_opts(QemuOptsList *list);
 void qemu_add_drive_opts(QemuOptsList *list);
 int qemu_set_option(const char *str);
util/qemu-config.c +3 -0
index 9298f55..9dfacbc 100644
@@ -39,6 +39,20 @@ QemuOptsList *qemu_find_opts(const char *group)
     return ret;
 }
 
+QemuOpts *qemu_find_opts_singleton(const char *group)
+{
+    QemuOptsList *list;

util/qemu-config.c QemuOptsList *qemu_find_opts(const char *group) · line 54

Quoted 10 lines
util/qemu-config.c +10 -0
+    QemuOpts *opts;
+
+    list = qemu_find_opts(group);
+    assert(list);
+    opts = qemu_opts_find(list, NULL);
+    if (!opts) {
+        opts = qemu_opts_create(list, NULL, 0, &error_abort);
+    }
+    return opts;
+}

First of all, the commit message body is empty, and the subject line doesn't really say much, plus there are no comments, so I have no clue what we're trying to implement here *in general*.

For example, if you look into qemu_opts_create(), you'll see that when (id==NULL), then you don't actually need the

qemu_opts_find(list, NULL)

call, because qemu_opts_create() calls that itself anyway. Of course, this behavior *also* depends on merge_lists being "true", but in case of "machine", that *is* a given.

Hence a specification for the function would help deciding if we can take merge_lists for granted, or if we want something more general. In the former case, the code above is unnecessarily pessimistic.

(Basically the qemu_find_opts_singleton() should be a *very* thin wrapper around qemu_opts_create(), because the latter already has this "O_CREAT without O_EXCL" semantics.)

Quoted 17 lines
util/qemu-config.c +1 -0
+
 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
 {
     CommandLineParameterInfoList *param_list = NULL, *entry;
vl.c +0 -4
index 383be1b..7f2595c 100644
@@ -539,16 +539,7 @@ static QemuOptsList qemu_msg_opts = {
  */
 QemuOpts *qemu_get_machine_opts(void)
 {
-    QemuOptsList *list;
-    QemuOpts *opts;
-
-    list = qemu_find_opts("machine");

vl.c static QemuOptsList qemu_msg_opts = { · line 545

Quoted 10 lines
vl.c +1 -6
-    assert(list);
-    opts = qemu_opts_find(list, NULL);
-    if (!opts) {
-        opts = qemu_opts_create(list, NULL, 0, &error_abort);
-    }
-    return opts;
+    return qemu_find_opts_singleton("machine");
 }
 
 const char *qemu_get_vm_name(void)

I also kinda hate that "error_abort" -- while used in several option-specific parser functions (balloon_parse, virtcon_parse, QEMU_OPTION_virtfs, QEMU_OPTION_virtfs_synth) -- now makes it into a generic-looking options function. qemu_find_opts_singleton() should take an errp.

Anyway I don't care enough to insist on a respin.

#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] QemuOpts: introduce qemu_find_opts_singleton

Quoted 61 lines · On Mon, 10 Feb 2014 18:00:39 +0100 Laszlo Ersek <[email protected]> wrote:

comments below

Quoted 34 lines · Igor Mammedov

From: Paolo Bonzini <[email protected]>

---
 include/qemu/config-file.h |    2 ++
 util/qemu-config.c         |   14 ++++++++++++++
 vl.c                       |   11 +----------
 3 files changed, 17 insertions(+), 10 deletions(-)
include/qemu/config-file.h +2 -0
index dbd97c4..d4ba20e 100644
@@ -8,6 +8,8 @@
 
 QemuOptsList *qemu_find_opts(const char *group);
 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp);
+QemuOpts *qemu_find_opts_singleton(const char *group);
+
 void qemu_add_opts(QemuOptsList *list);
 void qemu_add_drive_opts(QemuOptsList *list);
 int qemu_set_option(const char *str);
util/qemu-config.c +3 -0
index 9298f55..9dfacbc 100644
@@ -39,6 +39,20 @@ QemuOptsList *qemu_find_opts(const char *group)
     return ret;
 }
 
+QemuOpts *qemu_find_opts_singleton(const char *group)
+{
+    QemuOptsList *list;

util/qemu-config.c QemuOptsList *qemu_find_opts(const char *group) · line 54

Quoted 10 lines
util/qemu-config.c +10 -0
+    QemuOpts *opts;
+
+    list = qemu_find_opts(group);
+    assert(list);
+    opts = qemu_opts_find(list, NULL);
+    if (!opts) {
+        opts = qemu_opts_create(list, NULL, 0, &error_abort);
+    }
+    return opts;
+}

First of all, the commit message body is empty, and the subject line doesn't really say much, plus there are no comments, so I have no clue what we're trying to implement here *in general*.

For example, if you look into qemu_opts_create(), you'll see that when (id==NULL), then you don't actually need the

qemu_opts_find(list, NULL)

call, because qemu_opts_create() calls that itself anyway. Of course, this behavior *also* depends on merge_lists being "true", but in case of "machine", that *is* a given.

Quoted 7 lines

Hence a specification for the function would help deciding if we can take merge_lists for granted, or if we want something more general. In the former case, the code above is unnecessarily pessimistic.

(Basically the qemu_find_opts_singleton() should be a *very* thin wrapper around qemu_opts_create(), because the latter already has this "O_CREAT without O_EXCL" semantics.)

I'd guess function qemu_find_opts_singleton() is called so to express common idiom and shouldn't have an expectation for merge_lists == true being granted. So it could be reused with other options as well.

Quoted 26 lines
Quoted 26 lines
util/qemu-config.c +1 -0
+
 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
 {
     CommandLineParameterInfoList *param_list = NULL, *entry;
vl.c +1 -10
index 383be1b..7f2595c 100644
@@ -539,16 +539,7 @@ static QemuOptsList qemu_msg_opts = {
  */
 QemuOpts *qemu_get_machine_opts(void)
 {
-    QemuOptsList *list;
-    QemuOpts *opts;
-
-    list = qemu_find_opts("machine");
-    assert(list);
-    opts = qemu_opts_find(list, NULL);
-    if (!opts) {
-        opts = qemu_opts_create(list, NULL, 0, &error_abort);
-    }
-    return opts;
+    return qemu_find_opts_singleton("machine");
 }
 
Quoted 8 lines

vl.c static QemuOptsList qemu_msg_opts = { · line 545

Quoted 2 lines
vl.c +0 -0
 const char *qemu_get_vm_name(void)

I also kinda hate that "error_abort" -- while used in several option-specific parser functions (balloon_parse, virtcon_parse, QEMU_OPTION_virtfs, QEMU_OPTION_virtfs_synth) -- now makes it into a generic-looking options function. qemu_find_opts_singleton() should take an errp.

looking at possible call paths qemu_opts_create() with given arguments should never fail so assert like behavior here seems to be just fine, so we would notice for certain if/when it breaks.

Quoted 5 lines

Anyway I don't care enough to insist on a respin.

#4 lore raw

PATCH 2/2 vl: convert -m to QemuOpts

In reply to Igor Mammedov

Adds option to -m
"mem" - startup memory amount

For compatibility with legacy CLI if suffix-less number is passed, it assumes amount in Mb.

Otherwise user is free to use suffixed number using suffixes b,k/K,M,G

---
 qemu-options.hx |    7 +++++--
 vl.c            |   53 ++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 13 deletions(-)
qemu-options.hx +5 -2
index 56e5fdf..4d7ef52 100644
@@ -210,8 +210,11 @@ use is discouraged as it may be removed from future versions.
 ETEXI
 
 DEF("m", HAS_ARG, QEMU_OPTION_m,
-    "-m megs         set virtual RAM size to megs MB [default="
-    stringify(DEFAULT_RAM_SIZE) "]\n", QEMU_ARCH_ALL)
+    "-m [mem=]megs\n"
+    "                configure guest RAM\n"
+    "                mem: initial amount of guest memory (default: "
+    stringify(DEFAULT_RAM_SIZE) "Mb)\n",
+    QEMU_ARCH_ALL)
 STEXI
 @item -m @var{megs}
 @findex -m
vl.c +42 -11

93 lines not on this page · raw message

Signature · 1 line
1.7.1
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts

comments below

Quoted 20 lines · Igor Mammedov

Adds option to -m
"mem" - startup memory amount

For compatibility with legacy CLI if suffix-less number is passed, it assumes amount in Mb.

Otherwise user is free to use suffixed number using suffixes b,k/K,M,G

---
 qemu-options.hx |    7 +++++--
 vl.c            |   53 ++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 13 deletions(-)
qemu-options.hx +0 -0
index 56e5fdf..4d7ef52 100644

qemu-options.hx use is discouraged as it may be removed from future versions. · line 214

Quoted 7 lines
qemu-options.hx +2 -2
@@ -210,8 +210,11 @@ use is discouraged as it may be removed from future versions.
 ETEXI
 
 DEF("m", HAS_ARG, QEMU_OPTION_m,
-    "-m megs         set virtual RAM size to megs MB [default="
-    stringify(DEFAULT_RAM_SIZE) "]\n", QEMU_ARCH_ALL)
+    "-m [mem=]megs\n"
+    "                configure guest RAM\n"

"configure guest RAM size"

qemu-options.hx use is discouraged as it may be removed from future versions. · line 216

Quoted 2 lines
qemu-options.hx +2 -0
+    "                mem: initial amount of guest memory (default: "
+    stringify(DEFAULT_RAM_SIZE) "Mb)\n",

I wonder if it should rather say "MB" -- small "b" has this "bits" connotation for me. But I could be wrong.

Also, again, I believe explaining the default used to mean something else, but I'm OK with that part as-is.

vl.c static QemuOptsList qemu_msg_opts = { · line 533

Quoted 11 lines
qemu-options.hx +1 -0
+    QEMU_ARCH_ALL)
 STEXI
 @item -m @var{megs}
 @findex -m
vl.c +0 -0
index 7f2595c..fe5dae3 100644
@@ -532,6 +532,20 @@ static QemuOptsList qemu_msg_opts = {
     },
 };

(this could conflict with Alan's series -- modifies the same spot)

vl.c static QemuOptsList qemu_msg_opts = { · line 539

Quoted 5 lines
vl.c +5 -0
+static QemuOptsList qemu_mem_opts = {
+    .name = "memory-opts",
+    .implied_opt_name = "mem",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
+    .merge_lists = true,

OK, so we've set merge_list to true here as well, same as for "machine". Further support for simplifying qemu_find_opts_singleton(); see patch #1.

vl.c static QemuOptsList qemu_msg_opts = { · line 543

Quoted 4 lines
vl.c +4 -0
+    .desc = {
+        {
+            .name = "mem",
+            .type = QEMU_OPT_SIZE,

QEMU_OPT_SIZE implies (in parse_option_size()) that "no suffix" means "unit==byte" (); I'll check lower down how that's solved.

vl.c int main(int argc, char **argv, char **envp) · line 2885

Quoted 13 lines
vl.c +6 -0
+        },
+        { /* end of list */ }
+    },
+};
+
 /**
  * Get machine options
  *
@@ -2868,6 +2882,7 @@ int main(int argc, char **argv, char **envp)
     };
     const char *trace_events = NULL;
     const char *trace_file = NULL;
+    const ram_addr_t default_ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;

I'd feel safer if the multiplications were done in ram_addr_t. Currently they are done in "int". It's unlikely that we'll make 2GB+ the default ram size, but still.

Quoted 24 lines
vl.c +2 -1
     atexit(qemu_run_exit_notifiers);
     error_set_progname(argv[0]);
@@ -2906,6 +2921,7 @@ int main(int argc, char **argv, char **envp)
     qemu_add_opts(&qemu_tpmdev_opts);
     qemu_add_opts(&qemu_realtime_opts);
     qemu_add_opts(&qemu_msg_opts);
+    qemu_add_opts(&qemu_mem_opts);
 
     runstate_init();
 
@@ -2921,7 +2937,7 @@ int main(int argc, char **argv, char **envp)
     module_call_init(MODULE_INIT_MACHINE);
     machine = find_default_machine();
     cpu_model = NULL;
-    ram_size = 0;
+    ram_size = default_ram_size;
     snapshot = 0;
     cyls = heads = secs = 0;
     translation = BIOS_ATA_TRANSLATION_AUTO;
@@ -3198,16 +3214,32 @@ int main(int argc, char **argv, char **envp)
                 exit(0);
                 break;
             case QEMU_OPTION_m: {

vl.c int main(int argc, char **argv, char **envp) · line 3221

Quoted 10 lines
vl.c +3 -5
-                int64_t value;
                 uint64_t sz;
-                char *end;
+                const char *mem_str;
 
-                value = strtosz(optarg, &end);
-                if (value < 0 || *end) {
-                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
+                opts = qemu_opts_parse(qemu_find_opts("memory-opts"),
+                                       optarg, 1);

This can set "opts" to NULL if parsing fails, and then the qemu_opt_get() just below will SIGSEGV. You need to check if "opts" becomes NULL here, and exit if so (see other calls to qemu_opts_parse() in main()).

In particular, see commit f46e720a.

Also, unfortunately, this conversion kind of relaxes the error checking that happens during parsing. The pre-patch version ends up in strtosz_suffix_unit(), which rejects the empty string, for example. The new version, which ends up in parse_option_size(), is not that smart about strtod(). I think it will simply return zero for

-m mem=""

However it's not the fault of this patch.

vl.c int main(int argc, char **argv, char **envp) · line 3226

Quoted 5 lines
vl.c +5 -0
+
+                mem_str = qemu_opt_get(opts, "mem");
+                if (!mem_str) {
+                    fprintf(stderr, "qemu: invalid -m option, missing "
+                            " 'mem' option\n");

Double space. (There's one at the end of the first string literal, and another at the beginning of the second literal.)

vl.c int main(int argc, char **argv, char **envp) · line 3233

Quoted 8 lines
vl.c +5 -1
                     exit(1);
                 }
-                sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
+
+                sz = qemu_opt_get_size(opts, "mem", ram_size);
+
+                /* Fix up legacy suffix-less format */
+                if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {

Undefined behavior if mem_str is the emptry string. (I think it is possible, but I didn't test it.)

vl.c int main(int argc, char **argv, char **envp) · line 3235

Quoted 2 lines
vl.c +2 -0
+                    sz <<= 20;
+                }

We could check for overflow here, if we wanted.

Quoted 11 lines
vl.c +7 -0
+
+                /* backward compatibility behaviour for case "-m 0" */
+                if (sz == 0) {
+                    sz = default_ram_size;
+                }
+
+                sz = QEMU_ALIGN_UP(sz, 8192);
                 ram_size = sz;
                 if (ram_size != sz) {
                     fprintf(stderr, "qemu: ram size too large\n");

vl.c int main(int argc, char **argv, char **envp) · line 4093

Quoted 10 lines
vl.c +3 -4
@@ -4056,10 +4088,9 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
-    /* init the memory */
-    if (ram_size == 0) {
-        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
-    }
+    /* store value for the future use */
+    qemu_opt_set_number(qemu_find_opts_singleton("memory-opts"),
+                        "mem", ram_size);

Slight possibility here to overflow the int64_t "val" parameter with the potentially uint64_t "ram_size" argument. I guess we don't care.

Also, I wonder what happens when we have passed a non-default memory size on the command line. In that case, qemu_opt_set_number() seems to create a second QemuOpt here. I guess that's maybe expected though?

vl.c int main(int argc, char **argv, char **envp)

Quoted 4 lines
vl.c +0 -0
     if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
         != 0) {
 

It's your call what you'd like to address from the above.

Thanks
Laszlo

#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts

Quoted 25 lines · On Mon, 10 Feb 2014 18:38:59 +0100 Laszlo Ersek <[email protected]> wrote:

comments below

Quoted 22 lines · Igor Mammedov

Adds option to -m
"mem" - startup memory amount

For compatibility with legacy CLI if suffix-less number is passed, it assumes amount in Mb.

Otherwise user is free to use suffixed number using suffixes b,k/K,M,G

---
 qemu-options.hx |    7 +++++--
 vl.c            |   53 ++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 13 deletions(-)
qemu-options.hx +0 -0
index 56e5fdf..4d7ef52 100644
@@ -210,8 +210,11 @@ use is discouraged as it may be removed from future versions.
 ETEXI
 
Quoted 7 lines

qemu-options.hx use is discouraged as it may be removed from future versions. · line 214

Quoted 5 lines
qemu-options.hx +2 -2
 DEF("m", HAS_ARG, QEMU_OPTION_m,
-    "-m megs         set virtual RAM size to megs MB [default="
-    stringify(DEFAULT_RAM_SIZE) "]\n", QEMU_ARCH_ALL)
+    "-m [mem=]megs\n"
+    "                configure guest RAM\n"

"configure guest RAM size"

fixed

Quoted 6 lines

qemu-options.hx use is discouraged as it may be removed from future versions. · line 216

Quoted 3 lines
qemu-options.hx +2 -0
+    "                mem: initial amount of guest memory (default: "
+    stringify(DEFAULT_RAM_SIZE) "Mb)\n",

I wonder if it should rather say "MB" -- small "b" has this "bits" connotation for me. But I could be wrong.

fixed

Quoted 4 lines

Also, again, I believe explaining the default used to mean something else, but I'm OK with that part as-is.

Quoted 13 lines

vl.c static QemuOptsList qemu_msg_opts = { · line 533

Quoted 11 lines
qemu-options.hx +1 -0
+    QEMU_ARCH_ALL)
 STEXI
 @item -m @var{megs}
 @findex -m
vl.c +0 -0
index 7f2595c..fe5dae3 100644
@@ -532,6 +532,20 @@ static QemuOptsList qemu_msg_opts = {
     },
 };

(this could conflict with Alan's series -- modifies the same spot)

I'll put it in the beginning of list to avoid conflict.

Quoted 8 lines

vl.c static QemuOptsList qemu_msg_opts = { · line 539

Quoted 5 lines
vl.c +5 -0
+static QemuOptsList qemu_mem_opts = {
+    .name = "memory-opts",
+    .implied_opt_name = "mem",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
+    .merge_lists = true,

OK, so we've set merge_list to true here as well, same as for "machine". Further support for simplifying qemu_find_opts_singleton(); see patch #1.

I believe it's not good for generic function to depend on merge_lists.

Quoted 8 lines

vl.c static QemuOptsList qemu_msg_opts = { · line 543

Quoted 5 lines
vl.c +4 -0
+    .desc = {
+        {
+            .name = "mem",
+            .type = QEMU_OPT_SIZE,

QEMU_OPT_SIZE implies (in parse_option_size()) that "no suffix" means "unit==byte" (); I'll check lower down how that's solved.

it's(i.e. legacy behavior) taken care of later at -m parsing time

Quoted 10 lines
Quoted 10 lines
vl.c +5 -0
+        },
+        { /* end of list */ }
+    },
+};
+
 /**
  * Get machine options
  *
@@ -2868,6 +2882,7 @@ int main(int argc, char **argv, char **envp)
Quoted 8 lines

vl.c int main(int argc, char **argv, char **envp) · line 2885

Quoted 4 lines
vl.c +1 -0
     };
     const char *trace_events = NULL;
     const char *trace_file = NULL;
+    const ram_addr_t default_ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;

I'd feel safer if the multiplications were done in ram_addr_t. Currently they are done in "int". It's unlikely that we'll make 2GB+ the default ram size, but still.

fixed

Quoted 30 lines
Quoted 30 lines
vl.c +3 -3
     atexit(qemu_run_exit_notifiers);
     error_set_progname(argv[0]);
@@ -2906,6 +2921,7 @@ int main(int argc, char **argv, char **envp)
     qemu_add_opts(&qemu_tpmdev_opts);
     qemu_add_opts(&qemu_realtime_opts);
     qemu_add_opts(&qemu_msg_opts);
+    qemu_add_opts(&qemu_mem_opts);
 
     runstate_init();
 
@@ -2921,7 +2937,7 @@ int main(int argc, char **argv, char **envp)
     module_call_init(MODULE_INIT_MACHINE);
     machine = find_default_machine();
     cpu_model = NULL;
-    ram_size = 0;
+    ram_size = default_ram_size;
     snapshot = 0;
     cyls = heads = secs = 0;
     translation = BIOS_ATA_TRANSLATION_AUTO;
@@ -3198,16 +3214,32 @@ int main(int argc, char **argv, char **envp)
                 exit(0);
                 break;
             case QEMU_OPTION_m: {
-                int64_t value;
                 uint64_t sz;
-                char *end;
+                const char *mem_str;
 
Quoted 10 lines

vl.c int main(int argc, char **argv, char **envp) · line 3221

Quoted 5 lines
vl.c +2 -3
-                value = strtosz(optarg, &end);
-                if (value < 0 || *end) {
-                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
+                opts = qemu_opts_parse(qemu_find_opts("memory-opts"),
+                                       optarg, 1);

This can set "opts" to NULL if parsing fails, and then the qemu_opt_get() just below will SIGSEGV. You need to check if "opts" becomes NULL here, and exit if so (see other calls to qemu_opts_parse() in main()).

fixed

Quoted 9 lines

In particular, see commit f46e720a.

Also, unfortunately, this conversion kind of relaxes the error checking that happens during parsing. The pre-patch version ends up in strtosz_suffix_unit(), which rejects the empty string, for example. The new version, which ends up in parse_option_size(), is not that smart about strtod(). I think it will simply return zero for

Quoted 3 lines

-m mem=""

However it's not the fault of this patch.

parse_option_size() does return 0, but following code fixes it, look for comment: /* backward compatibility behavior for case "-m 0" */ but as you pointed below empty mem option causes undefined behavior later so fix by checking that string is not empty.

Quoted 9 lines

vl.c int main(int argc, char **argv, char **envp) · line 3226

Quoted 6 lines
vl.c +5 -0
+
+                mem_str = qemu_opt_get(opts, "mem");
+                if (!mem_str) {
+                    fprintf(stderr, "qemu: invalid -m option, missing "
+                            " 'mem' option\n");

Double space. (There's one at the end of the first string literal, and another at the beginning of the second literal.)

fixed

Quoted 12 lines

vl.c int main(int argc, char **argv, char **envp) · line 3233

Quoted 9 lines
vl.c +5 -1
                     exit(1);
                 }
-                sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
+
+                sz = qemu_opt_get_size(opts, "mem", ram_size);
+
+                /* Fix up legacy suffix-less format */
+                if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {

Undefined behavior if mem_str is the emptry string. (I think it is possible, but I didn't test it.)

indeed, fixed.

Quoted 5 lines

vl.c int main(int argc, char **argv, char **envp) · line 3235

Quoted 3 lines
vl.c +2 -0
+                    sz <<= 20;
+                }

We could check for overflow here, if we wanted.

fixed

Quoted 15 lines
Quoted 15 lines
vl.c +7 -0
+
+                /* backward compatibility behaviour for case "-m 0" */
+                if (sz == 0) {
+                    sz = default_ram_size;
+                }
+
+                sz = QEMU_ALIGN_UP(sz, 8192);
                 ram_size = sz;
                 if (ram_size != sz) {
                     fprintf(stderr, "qemu: ram size too large\n");
@@ -4056,10 +4088,9 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
Quoted 10 lines

vl.c int main(int argc, char **argv, char **envp) · line 4093

Quoted 7 lines
vl.c +3 -4
-    /* init the memory */
-    if (ram_size == 0) {
-        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
-    }
+    /* store value for the future use */
+    qemu_opt_set_number(qemu_find_opts_singleton("memory-opts"),
+                        "mem", ram_size);

Slight possibility here to overflow the int64_t "val" parameter with the potentially uint64_t "ram_size" argument. I guess we don't care.

yep, everywhere in API *_number is treated as uint64_t and only qemu_opt_set_number() treats it as int64_t, perhaps this function should be fixed.

Quoted 4 lines

Also, I wonder what happens when we have passed a non-default memory size on the command line. In that case, qemu_opt_set_number() seems to create a second QemuOpt here. I guess that's maybe expected though?

which appends it to the tail, and following lookup finds it first.

Quoted 10 lines

vl.c int main(int argc, char **argv, char **envp)

Quoted 5 lines
vl.c +0 -0
     if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
         != 0) {
 

It's your call what you'd like to address from the above.

Thanks
Laszlo

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts

In reply to Laszlo Ersek

Il 10/02/2014 18:38, Laszlo Ersek ha scritto:

Quoted 4 lines
Quoted 2 lines

+ " mem: initial amount of guest memory (default: "

Quoted 1 line

+ stringify(DEFAULT_RAM_SIZE) "Mb)\n",

I wonder if it should rather say "MB" -- small "b" has this "bits" connotation for me. But I could be wrong.

Thanks, will fix this.

Quoted 2 lines

Also, again, I believe explaining the default used to mean something else, but I'm OK with that part as-is.

What do you mean exactly? I cannot parse this.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts

Quoted 6 lines · Paolo Bonzini

Il 10/02/2014 18:38, Laszlo Ersek ha scritto:

Quoted 4 lines
Quoted 2 lines

+ " mem: initial amount of guest memory (default: "

Quoted 1 line

+ stringify(DEFAULT_RAM_SIZE) "Mb)\n",

I wonder if it should rather say "MB" -- small "b" has this "bits" connotation for me. But I could be wrong.

Quoted 6 lines

Thanks, will fix this.

Quoted 2 lines

Also, again, I believe explaining the default used to mean something else, but I'm OK with that part as-is.

What do you mean exactly? I cannot parse this.

Sorry, I was ambiguous.

When you have a QemuOpt that takes an argument, then the documentation of the default behavior usually explains the case when the QemuOpt is present, and the argument is absent. The documentation of the default behavior usually doesn't concern the case when the QemuOpt*s* itself is absent. Cf.

(1) -foo bar
(2) -foo bar=baz
(3) [nothing]

The "default" in the docs tends to explain case (1), not case (3).

In this case we have: -m [mem=]megs

(1) -m mem -- makes no sense
(2) -m mem=megs -- works, and well documented
(3) [nothing] -- is what the proposed docs describe as "default", but it doesn't match "historical practice".

(1) in general can make sense, eg. for booleans.

Anyway I don't feel strongly about this in the least -- I just thought I'd point it out. Feel free to ignore it; I have no good suggestion as to how to make it consistent across all options.

Thanks
Laszlo

#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts

Il 26/02/2014 14:42, Laszlo Ersek ha scritto:

Quoted 19 lines

When you have a QemuOpt that takes an argument, then the documentation of the default behavior usually explains the case when the QemuOpt is present, and the argument is absent. The documentation of the default behavior usually doesn't concern the case when the QemuOpt*s* itself is absent. Cf.

(1) -foo bar
(2) -foo bar=baz
(3) [nothing]

The "default" in the docs tends to explain case (1), not case (3).

In this case we have: -m [mem=]megs

(1) -m mem -- makes no sense
(2) -m mem=megs -- works, and well documented
(3) [nothing] -- is what the proposed docs describe as "default", but it doesn't match "historical practice".

Quoted 4 lines

(1) in general can make sense, eg. for booleans.

Anyway I don't feel strongly about this in the least -- I just thought I'd point it out.

Yeah, you're right.

Quoted 2 lines

Feel free to ignore it; I have no good suggestion as to how to make it consistent across all options.

Neither do I, but at least "megs" is not marked as an optional element, so I think it's not that ambiguous.

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts

In reply to Paolo Bonzini
Quoted 6 lines · On Wed, 26 Feb 2014 14:29:54 +0100 Paolo Bonzini <[email protected]> wrote:

Il 10/02/2014 18:38, Laszlo Ersek ha scritto:

Quoted 4 lines
Quoted 2 lines

+ " mem: initial amount of guest memory (default: "

Quoted 1 line

+ stringify(DEFAULT_RAM_SIZE) "Mb)\n",

I wonder if it should rather say "MB" -- small "b" has this "bits" connotation for me. But I could be wrong.

Quoted 10 lines

Thanks, will fix this.

Quoted 2 lines

Also, again, I believe explaining the default used to mean something else, but I'm OK with that part as-is.

What do you mean exactly? I cannot parse this.

Paolo

It should be fixed in v3 that you were going to apply to numa branch
https://lists.gnu.org/archive/html/qemu-devel/2014-02/msg02321.html