From c9e1f7278505fb20b66b3152be664cc6634a6aaa Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 24 Feb 2022 12:53:49 +1100 Subject: [PATCH] agent: Verify that we allocated as many hugepages as we need MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit allocate_hugepages() writes to the kernel sysfs file to allocate hugepages in the Kata VM. However, even if the write succeeds, it's not certain that the kernel will actually be able to allocate as many hugepages as we requested. This patch reads back the file after writing it to check if we were able to allocate all the required hugepages. fixes #3816 Signed-off-by: David Gibson (cherry picked from commit 42e35505b0fb1f207ca041a4d22de05d41106be8) Signed-off-by: Fabiano FidĂȘncio --- src/agent/src/mount.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/agent/src/mount.rs b/src/agent/src/mount.rs index e788e18dff..f4b50af6ec 100644 --- a/src/agent/src/mount.rs +++ b/src/agent/src/mount.rs @@ -337,6 +337,23 @@ fn allocate_hugepages(logger: &Logger, options: &[String]) -> Result<()> { file.write_all(numpages.as_bytes()) .context(format!("write nr_hugepages failed: {:?}", &path))?; + // Even if the write succeeds, the kernel isn't guaranteed to be + // able to allocate all the pages we requested. Verify that it + // did. + let verify = fs::read_to_string(&path).context(format!("reading {:?}", &path))?; + let allocated = verify + .trim_end() + .parse::() + .map_err(|_| anyhow!("Unexpected text {:?} in {:?}", &verify, &path))?; + if allocated != size / pagesize { + return Err(anyhow!( + "Only allocated {} of {} hugepages of size {}", + allocated, + numpages, + pagesize + )); + } + Ok(()) }