From 90b6e21f923fb52178dad787fe0597fb1fd99018 Mon Sep 17 00:00:00 2001 From: Li Huachao Date: Thu, 29 Aug 2024 09:42:25 +0800 Subject: [PATCH] Memory: prealloc bugfix If the host pagesize is not obtained, prealloc may be "divided by zero". When host pagesize is not obtained, 4096 Bytes is used for memory preoccupation. --- address_space/src/host_mmap.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/address_space/src/host_mmap.rs b/address_space/src/host_mmap.rs index fa807a80e..a8107ba78 100644 --- a/address_space/src/host_mmap.rs +++ b/address_space/src/host_mmap.rs @@ -207,8 +207,13 @@ fn touch_pages(start: u64, page_size: u64, nr_pages: u64) { /// * `nr_vcpus` - Number of vcpus. fn mem_prealloc(host_addr: u64, size: u64, nr_vcpus: u8) { trace::trace_scope_start!(pre_alloc, args = (size)); - let page_size = host_page_size(); + let mut page_size = host_page_size(); let threads = max_nr_threads(nr_vcpus); + // If the pagesize of the system is not obtained, we use the granularity + // of 4096 Bytes to allocate memory. + if page_size == 0 { + page_size = 4096; + } let nr_pages = (size + page_size - 1) / page_size; let pages_per_thread = nr_pages / u64::from(threads); let left = nr_pages % u64::from(threads); -- Gitee