From: Laszlo Ersek <lersek@redhat.com>
To: Igor Mammedov <imammedo@redhat.com>
Cc: kwolf@redhat.com, akong@redhat.com, stefanha@redhat.com,
armbru@redhat.com, mjt@tls.msk.ru, qemu-devel@nongnu.org,
mreitz@redhat.com, aliguori@amazon.com, pbonzini@redhat.com,
lcapitulino@redhat.com
Subject: Re: [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts
Date: Mon, 10 Feb 2014 18:38:59 +0100 [thread overview]
Message-ID: <52F90EB3.8030902@redhat.com> (raw)
In-Reply-To: <1391674564-3783-3-git-send-email-imammedo@redhat.com>
comments below
On 02/06/14 09:16, Igor Mammedov wrote:
> 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
>
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> qemu-options.hx | 7 +++++--
> vl.c | 53 ++++++++++++++++++++++++++++++++++++++++++-----------
> 2 files changed, 47 insertions(+), 13 deletions(-)
>
> diff --git a/qemu-options.hx b/qemu-options.hx
> index 56e5fdf..4d7ef52 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -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"
> + " 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.
> + QEMU_ARCH_ALL)
> STEXI
> @item -m @var{megs}
> @findex -m
> diff --git a/vl.c b/vl.c
> index 7f2595c..fe5dae3 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -532,6 +532,20 @@ static QemuOptsList qemu_msg_opts = {
> },
> };
(this could conflict with Alan's series -- modifies the same spot)
> +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.
> + .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.
> + },
> + { /* 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.
>
> 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;
>
> - 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.
> +
> + 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.)
> 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.)
> + sz <<= 20;
> + }
We could check for overflow here, if we wanted.
> +
> + /* 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);
> }
>
> - /* 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?
>
> 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
next prev parent reply other threads:[~2014-02-10 17:39 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-06 8:16 [Qemu-devel] [PATCH 0/2] convert -m to QemuOpts Igor Mammedov
2014-02-06 8:16 ` [Qemu-devel] [PATCH 1/2] QemuOpts: introduce qemu_find_opts_singleton Igor Mammedov
2014-02-10 17:00 ` Laszlo Ersek
2014-02-11 11:52 ` Igor Mammedov
2014-02-06 8:16 ` [Qemu-devel] [PATCH 2/2] vl: convert -m to QemuOpts Igor Mammedov
2014-02-10 17:38 ` Laszlo Ersek [this message]
2014-02-13 12:47 ` Igor Mammedov
2014-02-26 13:29 ` Paolo Bonzini
2014-02-26 13:42 ` Laszlo Ersek
2014-02-26 13:45 ` Paolo Bonzini
2014-02-26 13:50 ` Igor Mammedov
2014-02-26 14:00 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=52F90EB3.8030902@redhat.com \
--to=lersek@redhat.com \
--cc=akong@redhat.com \
--cc=aliguori@amazon.com \
--cc=armbru@redhat.com \
--cc=imammedo@redhat.com \
--cc=kwolf@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=mjt@tls.msk.ru \
--cc=mreitz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.