Align value generated by get_ram_size() to the host's page size
cover Align value generated by get_ram_size() to the host's page size
Hi,
This short patch series ensures that the value returned by get_ram_size() is aligned to the host's page size. Without that, KVM_SET_USER_MEMORY_REGION could fail when passed an unaligned value.
Cheers,
/fuad
Fuad Tabba (2):
Factor out getting the host page size
Align the calculated guest ram size to the host's page size
builtin-run.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
base-commit: bd4ba57156dad39349edfb2338bdc2f4ed3c0bae
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 1/2 Factor out getting the host page size
Factor out getting the page size of the host into a separate function. This will be used in the subsequent patch.
No functional change intended.
--- builtin-run.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
builtin-run.c +13 -7
@@ -360,9 +360,21 @@ static void kernel_usage_with_options(void) KVM_BINARY_NAME); } +static long host_page_size (void ) +{ +long page_size =sysconf (_SC_PAGE_SIZE); + +if (page_size <0 ) { +pr_warning ("sysconf(_SC_PAGE_SIZE) failed" ); +return 0 ; + } + +return page_size; +} +static u64 host_ram_size (void ) { -long page_size; +long page_size =host_page_size ();long nr_pages; nr_pages =sysconf (_SC_PHYS_PAGES);
@@ -371,12 +383,6 @@ static u64 host_ram_size(void)return 0 ; } - page_size =sysconf (_SC_PAGE_SIZE); -if (page_size <0 ) { -pr_warning ("sysconf(_SC_PAGE_SIZE) failed" ); -return 0 ; - } -return (u64 )nr_pages * page_size; }
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 2/2 Align the calculated guest ram size to the host's page size
In reply to Fuad TabbaIf host_ram_size() * RAM_SIZE_RATIO does not result in a value 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.
--- builtin-run.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
builtin-run.c +1 -1
@@ -406,7 +406,7 @@ static u64 get_ram_size(int nr_cpus)if (ram_size > available) ram_size = available; -return ram_size; +return ALIGN (ram_size,host_page_size ()); }static const char *find_kernel (void )
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [PATCH kvmtool v1 2/2] Align the calculated guest ram size to the host's page size
Quoted 17 lines · Fuad Tabba
If
host_ram_size()*RAM_SIZE_RATIOdoes not result in a value aligned to the host page size, it triggers an error in__kvm_set_memory_region(), called via theKVM_SET_USER_MEMORY_REGIONioctl, 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 <[email protected]>--- builtin-run.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)builtin-run.c +0
-0
builtin-run.c static u64 get_ram_size(int nr_cpus) · line 410
Quoted 6 lines
builtin-run.c +1
-1@@ -406,7 +406,7 @@ static u64 get_ram_size(int nr_cpus)if (ram_size > available) ram_size = available; -return ram_size; +return ALIGN (ram_size,host_page_size ()); }
I guess we could avoid querying the page size twice if we also factored out a helper to grab _SC_PHYS_PAGES and then did the multiply by RAM_SIZE_RATIO before converting back to bytes.
e.g. something like:
available = MIN_RAM_SIZE; nrpages = host_ram_nrpages() * RAM_SIZE_RATIO; if (nrpages) available = nrpages * host_page_size();
and then host_ram_size() just calls the two new helpers.
What do you think?
Will
Re: [PATCH kvmtool v1 2/2] Align the calculated guest ram size to the host's page size
Hi Will,
Quoted 38 lines · Will Deacon
Quoted 17 lines · Fuad Tabba
If
host_ram_size()*RAM_SIZE_RATIOdoes not result in a value aligned to the host page size, it triggers an error in__kvm_set_memory_region(), called via theKVM_SET_USER_MEMORY_REGIONioctl, 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 <[email protected]>--- builtin-run.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)builtin-run.c +0
-0builtin-run.c static u64 get_ram_size(int nr_cpus) · line 410
Quoted 6 lines
builtin-run.c +1
-1@@ -406,7 +406,7 @@ static u64 get_ram_size(int nr_cpus)if (ram_size > available) ram_size = available; -return ram_size; +return ALIGN (ram_size,host_page_size ()); }I guess we could avoid querying the page size twice if we also factored out a helper to grab
_SC_PHYS_PAGESand then did the multiply byRAM_SIZE_RATIObefore converting back to bytes.e.g. something like:
available = MIN_RAM_SIZE; nrpages = host_ram_nrpages() * RAM_SIZE_RATIO; if (nrpages) available = nrpages * host_page_size();
Quoted 3 lines
and then
host_ram_size()just calls the two new helpers.What do you think?
Sounds good to me. I'll respin.
Cheers,
/fuad
Quoted 2 lines
Will