aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandru Elisei <alexandru.elisei@arm.com>2022-06-16 14:48:19 +0100
committerWill Deacon <will@kernel.org>2022-07-01 16:08:06 +0100
commit28b96259fbdf602178d209fe93a0eddd3eec07d8 (patch)
tree1cb4e7d0ab9f92a21e9c176ac4c597c500a96900
parent9d655190e466fd6a0b042ba8107ec06570c2e6d5 (diff)
downloadkvmtool-28b96259fbdf602178d209fe93a0eddd3eec07d8.tar.gz
builtin-run: Rework RAM size validation
host_ram_size() uses sysconf() to calculate the available ram, and sysconf() can fail. When that happens, host_ram_size() returns 0. kvmtool warns the user when the configured VM ram size exceeds the size of the host's memory, but doesn't take into account that host_ram_size() can return 0. If the function returns zero, skip the warning. Since this can only happen when the user sets the memory size (via the -m/--mem command line argument), skip the check entirely if the user hasn't set it. Move the check to kvm_run_validate_cfg(), as it checks for valid user configuration. Reviewed-by: Andre Przywara <andre.przywara@arm.com> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com> Reviewed-and-Tested-by: Suzuki K Poulose <suzuki.poulose@arm.com> Link: https://lore.kernel.org/r/20220616134828.129006-4-alexandru.elisei@arm.com Signed-off-by: Will Deacon <will@kernel.org>
-rw-r--r--builtin-run.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/builtin-run.c b/builtin-run.c
index 2bf93fe1..e1770b3c 100644
--- a/builtin-run.c
+++ b/builtin-run.c
@@ -509,6 +509,8 @@ static void kvm_run_set_real_cmdline(struct kvm *kvm)
static void kvm_run_validate_cfg(struct kvm *kvm)
{
+ u64 available_ram;
+
if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename)
die("Only one of --kernel or --firmware can be specified");
@@ -518,6 +520,17 @@ static void kvm_run_validate_cfg(struct kvm *kvm)
if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename)
pr_warning("Ignoring initrd file when loading a firmware image");
+
+ if (kvm->cfg.ram_size) {
+ /* User specifies RAM size in megabytes. */
+ kvm->cfg.ram_size <<= MB_SHIFT;
+ available_ram = host_ram_size();
+ if (available_ram && kvm->cfg.ram_size > available_ram) {
+ pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
+ (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
+ (unsigned long long)available_ram >> MB_SHIFT);
+ }
+ }
}
static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
@@ -596,13 +609,6 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
if (!kvm->cfg.ram_size)
kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
- else
- kvm->cfg.ram_size <<= MB_SHIFT;
-
- if (kvm->cfg.ram_size > host_ram_size())
- pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
- (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
- (unsigned long long)host_ram_size() >> MB_SHIFT);
if (!kvm->cfg.dev)
kvm->cfg.dev = DEFAULT_KVM_DEV;