mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-03 17:20:05 +00:00
Merge pull request #3766 from dgibson/hugepages
Improve error checking of hugepage allocation
This commit is contained in:
commit
293e61dc6e
@ -321,26 +321,39 @@ fn allocate_hugepages(logger: &Logger, options: &[String]) -> Result<()> {
|
|||||||
|
|
||||||
// sysfs entry is always of the form hugepages-${pagesize}kB
|
// sysfs entry is always of the form hugepages-${pagesize}kB
|
||||||
// Ref: https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
|
// Ref: https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
|
||||||
let path = Path::new(SYS_FS_HUGEPAGES_PREFIX).join(format!("hugepages-{}kB", pagesize / 1024));
|
let path = Path::new(SYS_FS_HUGEPAGES_PREFIX)
|
||||||
|
.join(format!("hugepages-{}kB", pagesize / 1024))
|
||||||
if !path.exists() {
|
.join("nr_hugepages");
|
||||||
fs::create_dir_all(&path).context("create hugepages-size directory")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// write numpages to nr_hugepages file.
|
// write numpages to nr_hugepages file.
|
||||||
let path = path.join("nr_hugepages");
|
|
||||||
let numpages = format!("{}", size / pagesize);
|
let numpages = format!("{}", size / pagesize);
|
||||||
info!(logger, "write {} pages to {:?}", &numpages, &path);
|
info!(logger, "write {} pages to {:?}", &numpages, &path);
|
||||||
|
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
|
||||||
.open(&path)
|
.open(&path)
|
||||||
.context(format!("open nr_hugepages directory {:?}", &path))?;
|
.context(format!("open nr_hugepages directory {:?}", &path))?;
|
||||||
|
|
||||||
file.write_all(numpages.as_bytes())
|
file.write_all(numpages.as_bytes())
|
||||||
.context(format!("write nr_hugepages failed: {:?}", &path))?;
|
.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::<u64>()
|
||||||
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user