vmm: config: Switch PmemConfig to use PciDeviceCommonConfig

Switch PmemConfig over to using the newly extracted struct members as
used by all PCI based devices. The use of #[serde(flatten)] means that
this change has no impact on the JSON format that the data is stored as.

Signed-off-by: Rob Bradford <rbradford@meta.com>
This commit is contained in:
Rob Bradford
2026-04-03 12:05:29 -07:00
parent 0d3080e036
commit c9082570b0
3 changed files with 35 additions and 54 deletions

View File

@@ -2061,52 +2061,28 @@ impl PmemConfig {
.add("pci_segment");
parser.parse(pmem).map_err(Error::ParsePersistentMemory)?;
let pci_common = PciDeviceCommonConfig::parse(pmem)?;
let file = PathBuf::from(parser.get("file").ok_or(Error::ParsePmemFileMissing)?);
let size = parser
.convert::<ByteSized>("size")
.map_err(Error::ParsePersistentMemory)?
.map(|v| v.0);
let iommu = parser
.convert::<Toggle>("iommu")
.map_err(Error::ParsePersistentMemory)?
.unwrap_or(Toggle(false))
.0;
let discard_writes = parser
.convert::<Toggle>("discard_writes")
.map_err(Error::ParsePersistentMemory)?
.unwrap_or(Toggle(false))
.0;
let id = parser.get("id");
let pci_segment = parser
.convert("pci_segment")
.map_err(Error::ParsePersistentMemory)?
.unwrap_or_default();
Ok(PmemConfig {
pci_common,
file,
size,
iommu,
discard_writes,
id,
pci_segment,
})
}
pub fn validate(&self, vm_config: &VmConfig) -> ValidationResult<()> {
if let Some(platform_config) = vm_config.platform.as_ref() {
if self.pci_segment >= platform_config.num_pci_segments {
return Err(ValidationError::InvalidPciSegment(self.pci_segment));
}
if let Some(iommu_segments) = platform_config.iommu_segments.as_ref()
&& iommu_segments.contains(&self.pci_segment)
&& !self.iommu
{
return Err(ValidationError::OnIommuSegment(self.pci_segment));
}
}
Ok(())
self.pci_common.validate(vm_config)
}
}
@@ -3046,9 +3022,9 @@ impl VmConfig {
if let Some(pmems) = &self.pmem {
for pmem in pmems {
pmem.validate(self)?;
self.iommu |= pmem.iommu;
self.iommu |= pmem.pci_common.iommu;
Self::validate_identifier(&mut id_list, &pmem.id)?;
Self::validate_identifier(&mut id_list, &pmem.pci_common.id)?;
}
}
@@ -3521,7 +3497,7 @@ impl VmConfig {
// Remove if pmem device
if let Some(pmem) = self.pmem.as_mut() {
let len = pmem.len();
pmem.retain(|dev| dev.id.as_ref().map(|id| id.as_ref()) != Some(id));
pmem.retain(|dev| dev.pci_common.id.as_ref().map(|id| id.as_ref()) != Some(id));
removed |= pmem.len() != len;
}
@@ -4345,12 +4321,10 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
fn pmem_fixture() -> PmemConfig {
PmemConfig {
pci_common: PciDeviceCommonConfig::default(),
file: PathBuf::from("/tmp/pmem"),
size: Some(128 << 20),
iommu: false,
discard_writes: false,
id: None,
pci_segment: 0,
}
}
@@ -4366,15 +4340,21 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
assert_eq!(
PmemConfig::parse("file=/tmp/pmem,size=128M,id=mypmem0")?,
PmemConfig {
id: Some("mypmem0".to_owned()),
pci_common: PciDeviceCommonConfig {
id: Some("mypmem0".to_owned()),
..Default::default()
},
..pmem_fixture()
}
);
assert_eq!(
PmemConfig::parse("file=/tmp/pmem,size=128M,iommu=on,discard_writes=on")?,
PmemConfig {
pci_common: PciDeviceCommonConfig {
iommu: true,
..Default::default()
},
discard_writes: true,
iommu: true,
..pmem_fixture()
}
);
@@ -5313,8 +5293,11 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
..platform_fixture()
});
still_valid_config.pmem = Some(vec![PmemConfig {
iommu: true,
pci_segment: 1,
pci_common: PciDeviceCommonConfig {
iommu: true,
pci_segment: 1,
..Default::default()
},
..pmem_fixture()
}]);
still_valid_config.validate().unwrap();
@@ -5388,8 +5371,10 @@ id=\"{id}\",pci_segment={pci_segment},queue_sizes={queue_sizes}"
..platform_fixture()
});
invalid_config.pmem = Some(vec![PmemConfig {
iommu: false,
pci_segment: 1,
pci_common: PciDeviceCommonConfig {
pci_segment: 1,
..Default::default()
},
..pmem_fixture()
}]);
assert_eq!(

View File

@@ -3282,11 +3282,11 @@ impl DeviceManager {
&mut self,
pmem_cfg: &mut PmemConfig,
) -> DeviceManagerResult<MetaVirtioDevice> {
let id = if let Some(id) = &pmem_cfg.id {
let id = if let Some(id) = &pmem_cfg.pci_common.id {
id.clone()
} else {
let id = self.next_device_name(PMEM_DEVICE_NAME_PREFIX)?;
pmem_cfg.id = Some(id.clone());
pmem_cfg.pci_common.id = Some(id.clone());
id
};
@@ -3358,7 +3358,7 @@ impl DeviceManager {
let (region_base, region_size) = if let Some((base, size)) = region_range {
// The memory needs to be 2MiB aligned in order to support
// hugepages.
self.pci_segments[pmem_cfg.pci_segment as usize]
self.pci_segments[pmem_cfg.pci_common.pci_segment as usize]
.mem64_allocator
.lock()
.unwrap()
@@ -3373,7 +3373,7 @@ impl DeviceManager {
} else {
// The memory needs to be 2MiB aligned in order to support
// hugepages.
let base = self.pci_segments[pmem_cfg.pci_segment as usize]
let base = self.pci_segments[pmem_cfg.pci_common.pci_segment as usize]
.mem64_allocator
.lock()
.unwrap()
@@ -3421,7 +3421,7 @@ impl DeviceManager {
file,
GuestAddress(region_base),
mapping,
self.force_iommu | pmem_cfg.iommu,
self.force_iommu | pmem_cfg.pci_common.iommu,
self.seccomp_action.clone(),
self.exit_evt
.try_clone()
@@ -3444,9 +3444,9 @@ impl DeviceManager {
Ok(MetaVirtioDevice {
virtio_device: Arc::clone(&virtio_pmem_device)
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
iommu: pmem_cfg.iommu,
iommu: pmem_cfg.pci_common.iommu,
id,
pci_segment: pmem_cfg.pci_segment,
pci_segment: pmem_cfg.pci_common.pci_segment,
dma_handler: None,
})
}
@@ -5056,9 +5056,9 @@ impl DeviceManager {
}
pub fn add_pmem(&mut self, pmem_cfg: &mut PmemConfig) -> DeviceManagerResult<PciDeviceInfo> {
self.validate_identifier(&pmem_cfg.id)?;
self.validate_identifier(&pmem_cfg.pci_common.id)?;
if pmem_cfg.iommu && !self.is_iommu_segment(pmem_cfg.pci_segment) {
if pmem_cfg.pci_common.iommu && !self.is_iommu_segment(pmem_cfg.pci_common.pci_segment) {
return Err(DeviceManagerError::InvalidIommuHotplug);
}

View File

@@ -514,17 +514,13 @@ impl ApplyLandlock for GenericVhostUserConfig {
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct PmemConfig {
#[serde(flatten)]
pub pci_common: PciDeviceCommonConfig,
pub file: PathBuf,
#[serde(default)]
pub size: Option<u64>,
#[serde(default)]
pub iommu: bool,
#[serde(default)]
pub discard_writes: bool,
#[serde(default)]
pub id: Option<String>,
#[serde(default)]
pub pci_segment: u16,
}
impl ApplyLandlock for PmemConfig {