diff options
author | Fuad Tabba <tabba@google.com> | 2023-07-17 13:12:32 +0100 |
---|---|---|
committer | Will Deacon <will@kernel.org> | 2023-07-20 15:58:06 +0100 |
commit | 63643b11ce7d7c36d0b4e7636a48ca8451c2aae7 (patch) | |
tree | c286c5efbb92e332baaac5a02bac347a629cdea8 | |
parent | 834e5ed62fb8e4905b31e54d456d4011365b9533 (diff) | |
download | kvmtool-63643b11ce7d7c36d0b4e7636a48ca8451c2aae7.tar.gz |
Apply scaling down the calculated guest ram size to the number of pages
Calculate the guest ram size based a ratio proportional to the
number of pages available, rather than the amount of memory
available in bytes, in the host. This is to ensure that the
result is always page-aligned.
If the result of get_ram_size() isn't aligned to the host page
size, it triggers an error in __kvm_set_memory_region(), called
via the KVM_SET_USER_MEMORY_REGION ioctl, which requires the size
to be page-aligned.
Fixes: 18bd8c3bd2a7 ("kvm tools: Don't use all of host RAM for guests by default")
Signed-off-by: Fuad Tabba <tabba@google.com>
Link: https://lore.kernel.org/r/20230717121232.3559948-4-tabba@google.com
Signed-off-by: Will Deacon <will@kernel.org>
-rw-r--r-- | builtin-run.c | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/builtin-run.c b/builtin-run.c index 44ea6908..21373d41 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -400,17 +400,15 @@ static u64 host_ram_size(void) static u64 get_ram_size(int nr_cpus) { - u64 available; - u64 ram_size; + long nr_pages_available = host_ram_nrpages() * RAM_SIZE_RATIO; + u64 ram_size = (u64)SZ_64M * (nr_cpus + 3); + u64 available = MIN_RAM_SIZE; - ram_size = (u64)SZ_64M * (nr_cpus + 3); - - available = host_ram_size() * RAM_SIZE_RATIO; - if (!available) - available = MIN_RAM_SIZE; + if (nr_pages_available) + available = nr_pages_available * host_page_size(); if (ram_size > available) - ram_size = available; + ram_size = available; return ram_size; } |