@@ -6,6 +6,7 @@ use std::path::PathBuf;
6
6
use std:: sync:: { Arc , Mutex , MutexGuard } ;
7
7
8
8
use serde:: { Deserialize , Serialize } ;
9
+ use vm_memory:: GuestAddress ;
9
10
10
11
use crate :: cpu_config:: templates:: CustomCpuTemplate ;
11
12
use crate :: device_manager:: persist:: SharedDeviceType ;
@@ -463,11 +464,14 @@ impl VmResources {
463
464
Ok ( ( ) )
464
465
}
465
466
466
- /// Allocates guest memory in a configuration most appropriate for these [`VmResources`] .
467
+ /// Allocates the given guest memory regions .
467
468
///
468
469
/// If vhost-user-blk devices are in use, allocates memfd-backed shared memory, otherwise
469
470
/// prefers anonymous memory for performance reasons.
470
- pub fn allocate_guest_memory ( & self ) -> Result < Vec < GuestRegionMmap > , MemoryError > {
471
+ fn allocate_memory_regions (
472
+ & self ,
473
+ regions : & [ ( GuestAddress , usize ) ] ,
474
+ ) -> Result < Vec < GuestRegionMmap > , MemoryError > {
471
475
let vhost_user_device_used = self
472
476
. block
473
477
. devices
@@ -483,22 +487,39 @@ impl VmResources {
483
487
// because that would require running a backend process. If in the future we converge to
484
488
// a single way of backing guest memory for vhost-user and non-vhost-user cases,
485
489
// that would not be worth the effort.
486
- let regions =
487
- crate :: arch:: arch_memory_regions ( mib_to_bytes ( self . machine_config . mem_size_mib ) ) ;
488
490
if vhost_user_device_used {
489
491
memory:: memfd_backed (
490
- regions. as_ref ( ) ,
492
+ regions,
491
493
self . machine_config . track_dirty_pages ,
492
494
self . machine_config . huge_pages ,
493
495
)
494
496
} else {
495
497
memory:: anonymous (
496
- regions. into_iter ( ) ,
498
+ regions. iter ( ) . copied ( ) ,
497
499
self . machine_config . track_dirty_pages ,
498
500
self . machine_config . huge_pages ,
499
501
)
500
502
}
501
503
}
504
+
505
+ /// Allocates guest memory in a configuration most appropriate for these [`VmResources`].
506
+ pub fn allocate_guest_memory ( & self ) -> Result < Vec < GuestRegionMmap > , MemoryError > {
507
+ let regions =
508
+ crate :: arch:: arch_memory_regions ( mib_to_bytes ( self . machine_config . mem_size_mib ) ) ;
509
+ self . allocate_memory_regions ( & regions)
510
+ }
511
+
512
+ /// Allocates a single guest memory region.
513
+ pub fn allocate_memory_region (
514
+ & self ,
515
+ start : GuestAddress ,
516
+ size : usize ,
517
+ ) -> Result < GuestRegionMmap , MemoryError > {
518
+ Ok ( self
519
+ . allocate_memory_regions ( & [ ( start, size) ] ) ?
520
+ . pop ( )
521
+ . unwrap ( ) )
522
+ }
502
523
}
503
524
504
525
impl From < & VmResources > for VmmConfig {
0 commit comments