Skip to content

Commit

Permalink
Limit RAM+VRAM to avoid exceeding IPA addressing
Browse files Browse the repository at this point in the history
Automatically adjust VRAM so VRAM+RAM doesn't exceed the space
available in a 36 bit IPA, accounting for the start address and
rounding in libkrun, which sets an upper limit of ~62GiB.

Also, reject a memory configuration larger than 60 GiB to ensure
we leave some room for VRAM.

Fixes: #17

Signed-off-by: Sergio Lopez <[email protected]>
  • Loading branch information
slp authored and tylerfanelli committed Nov 5, 2024
1 parent 8e16224 commit ce48e7c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ impl TryFrom<Args> for KrunContext {

if args.memory == 0 {
return Err(anyhow!("zero MiB RAM inputted (invalid)"));
} else if args.memory > 61440 {
// Limit RAM to 60 GiB of the 62 GiB upper bound to leave room for VRAM.
return Err(anyhow!(
"requested RAM larger than upper limit of 61440 MiB"
));
}

if unsafe { krun_set_vm_config(id, args.cpus, args.memory) } < 0 {
Expand All @@ -69,7 +74,10 @@ impl TryFrom<Args> for KrunContext {
// Temporarily enable GPU by default
let virgl_flags = VIRGLRENDERER_VENUS | VIRGLRENDERER_NO_VIRGL;
let sys = sysinfo::System::new_all();
if unsafe { krun_set_gpu_options2(id, virgl_flags, sys.total_memory()) } < 0 {
// Limit RAM + VRAM to 64 GB (36 bit IPA address limit) minus 2 GB (start address plus rounding).
let rounded_mem = ((args.memory as u64) / 1024 + 1) * 1024;
let vram = std::cmp::min((63488 - rounded_mem) * 1024 * 1024, sys.total_memory());
if unsafe { krun_set_gpu_options2(id, virgl_flags, vram) } < 0 {
return Err(anyhow!("unable to set krun vCPU/RAM configuration"));
}

Expand Down

0 comments on commit ce48e7c

Please sign in to comment.