Skip to contentSkip to conversation

PCI: vmd: Only assign OSC features in bare metal OS

PATCH Applied Nirmal Patel 5 participants 5 messages Jul 13 – Jul 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 PCI: vmd: Only assign OSC features in bare metal OS

Copy valid OSC flags from root bridge only when OS can provide correct values. In virtual machine, all of the control features are disabled as hypervisor will not passthrough root bridge information to VM.

---
v3 : Removed period from commit and added a black line.
v2 : Added descriptive boolean and removed period from commit message.
---

---
 drivers/pci/controller/vmd.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/pci/controller/vmd.c +9 -4
index d4ae250d4bc6..7c7d110290d6 100644
@@ -782,6 +782,7 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 	resource_size_t membar2_offset = 0x2000;
 	struct pci_bus *child;
 	struct pci_dev *dev;
+	bool vmd_in_guest;
 	int ret;
 
 	/*
@@ -862,14 +863,16 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 		.parent = res,
 	};
 
+	/* Non-zero offset means guest/direct assign view. */
+	vmd_in_guest = offset[0] || offset[1];
+
 	/*
 	 * Currently MSI remapping must be enabled in guest passthrough mode
 	 * due to some missing interrupt remapping plumbing. This is probably
 	 * acceptable because the guest is usually CPU-limited and MSI
 	 * remapping doesn't become a performance bottleneck.
 	 */
-	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
-	    offset[0] || offset[1]) {
+	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) || vmd_in_guest) {
 		ret = vmd_alloc_irqs(vmd);
 		if (ret)
 			return ret;
@@ -910,8 +913,10 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 		return -ENODEV;
 	}
 
-	vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
-				   to_pci_host_bridge(vmd->bus->bridge));
+	/* Don't copy _OSC control flags in VM, it disables features. */
+	if (!vmd_in_guest)
+		vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
+					 to_pci_host_bridge(vmd->bus->bridge));
 
 	vmd_attach_resources(vmd);
 	if (vmd->irq_domain)
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Flawed VM detection heuristic breaks intended OSC flag assignment in edge cases.

Pre-existing issues:
- [Medium] Memory leak of dynamically allocated SRCU structures on error paths.
- [Medium] Leak of dynamically allocated resource entries and IRQ domain on early return. --

--- Patch [1]: [PATCH v3] PCI: vmd: Only assign OSC features in bare metal OS --- commit 7b1a0a2085cd70ccc13f43497ed84956b2e0637c
Author: Nirmal Patel <[email protected]>

PCI: vmd: Only assign OSC features in bare metal OS

Copy valid OSC flags from root bridge only when OS can provide correct values. In virtual machine, all of the control features are disabled as hypervisor will not passthrough root bridge information to VM.

Quoted 3 lines
drivers/pci/controller/vmd.c +0 -0

[ ... ]

drivers/pci/controller/vmd.c static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) · line 868

Quoted 7 lines
drivers/pci/controller/vmd.c +3 -0
@@ -862,14 +863,16 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 		.parent = res,
 	};
 
+	/* Non-zero offset means guest/direct assign view. */
+	vmd_in_guest = offset[0] || offset[1];
+

[Severity: High]
Does this heuristic correctly account for bare-metal BAR reassignment or identity-mapped VMs?

If the Linux PCI subsystem reassigns the VMD device's BARs on bare metal, for example due to resource conflicts, the offsets would become non-zero. This would cause the driver to incorrectly identify the system as a guest and skip copying OSC control flags on bare metal.

Conversely, if a VM uses 1:1 identity-mapped direct assignment where the guest physical address equals the host physical address, the offset would be zero. The driver would then treat the VM as bare metal, copying the disabled or invalid OSC flags from the virtual host bridge.

drivers/pci/controller/vmd.c static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) · line 876

Quoted 10 lines
drivers/pci/controller/vmd.c +1 -2
 	/*
 	 * Currently MSI remapping must be enabled in guest passthrough mode
 	 * due to some missing interrupt remapping plumbing. This is probably
 	 * acceptable because the guest is usually CPU-limited and MSI
 	 * remapping doesn't become a performance bottleneck.
 	 */
-	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
-	    offset[0] || offset[1]) {
+	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) || vmd_in_guest) {
 		ret = vmd_alloc_irqs(vmd);

[Severity: Medium]
This is a pre-existing issue, but does vmd_alloc_irqs() leak dynamically allocated SRCU structures on error paths?

If devm_request_irq() fails inside vmd_alloc_irqs(), the function returns the error directly without calling cleanup_srcu_struct() on the SRCU structures initialized in previous loop iterations or the current one.

Additionally, if vmd_enable_domain() fails and propagates an error back to vmd_probe(), the out_release_instance error path in vmd_probe() does not call vmd_cleanup_srcu(), which seems to leak the SRCU per-CPU data.

Quoted 2 lines
drivers/pci/controller/vmd.c +0 -0
 		if (ret)
 			return ret;

[ ... ]

drivers/pci/controller/vmd.c static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) · line 919

Quoted 10 lines
drivers/pci/controller/vmd.c +4 -2
@@ -910,8 +913,10 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 		return -ENODEV;
 	}
 
-	vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
-				   to_pci_host_bridge(vmd->bus->bridge));
+	/* Don't copy _OSC control flags in VM, it disables features. */
+	if (!vmd_in_guest)
+		vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
+					 to_pci_host_bridge(vmd->bus->bridge));

[Severity: Medium]
This is a pre-existing issue, but does vmd_enable_domain() leak dynamically allocated resource entries and the IRQ domain on early return?

When pci_bus_find_emul_domain_nr() is called a few lines before this hunk in vmd_enable_domain():

        sd->domain = pci_bus_find_emul_domain_nr(0, 0x10000, INT_MAX);
        if (sd->domain < 0)
                return sd->domain;

If this fails, it returns early without calling pci_free_resource_list() or vmd_remove_irq_domain(), stranding the memory allocated for the resources list and the IRQ domain created earlier in the function.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Nirmal Patel
Quoted 63 lines · On Mon, 13 Jul 2026 22:08:44 +0000 Nirmal Patel <[email protected]> wrote:

Copy valid OSC flags from root bridge only when OS can provide correct values. In virtual machine, all of the control features are disabled as hypervisor will not passthrough root bridge information to VM.

---
v3 : Removed period from commit and added a black line.
v2 : Added descriptive boolean and removed period from commit message.
---

---
 drivers/pci/controller/vmd.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/pci/controller/vmd.c index d4ae250d4bc6..7c7d110290d6 100644

drivers/pci/controller/vmd.c +4 -0
@@ -782,6 +782,7 @@ static int vmd_enable_domain(struct vmd_dev *vmd,
 unsigned long features) resource_size_t membar2_offset = 0x2000;
 	struct pci_bus *child;
 	struct pci_dev *dev;
+	bool vmd_in_guest;
 	int ret;
 
 	/*
@@ -862,14 +863,16 @@ static int vmd_enable_domain(struct vmd_dev
 *vmd, unsigned long features) .parent = res,
 	};
 
+	/* Non-zero offset means guest/direct assign view. */
+	vmd_in_guest = offset[0] || offset[1];
+
 	/*
 	 * Currently MSI remapping must be enabled in guest

passthrough mode
* due to some missing interrupt remapping plumbing. This is
probably
* acceptable because the guest is usually CPU-limited and
MSI

drivers/pci/controller/vmd.c +1 -2
 	 * remapping doesn't become a performance bottleneck.
 	 */
-	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
-	    offset[0] || offset[1]) {
+	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||

vmd_in_guest) { ret = vmd_alloc_irqs(vmd);

drivers/pci/controller/vmd.c +1 -2
 		if (ret)
 			return ret;
@@ -910,8 +913,10 @@ static int vmd_enable_domain(struct vmd_dev
 *vmd, unsigned long features) return -ENODEV;
 	}
 
-
 vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
-
 to_pci_host_bridge(vmd->bus->bridge));
+	/* Don't copy _OSC control flags in VM, it disables

features. */

drivers/pci/controller/vmd.c +2 -0
+	if (!vmd_in_guest)
+

vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus), +
to_pci_host_bridge(vmd->bus->bridge));

drivers/pci/controller/vmd.c static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) · line 922

Quoted 2 lines
drivers/pci/controller/vmd.c +0 -0
 	vmd_attach_resources(vmd);
 	if (vmd->irq_domain)

Gentle reminder.

Thanks
nirmal

#3 lore raw
In reply to Nirmal Patel
Quoted 3 lines · Nirmal Patel

Copy valid OSC flags from root bridge only when OS can provide correct values. In virtual machine, all of the control features are disabled as hypervisor will not passthrough root bridge information to VM.

Applied, thanks!

[1/1] PCI: vmd: Only assign OSC features in bare metal OS

      commit: 2162572d34c503882e9047a2b07adafd6139dc2d

Best regards,

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#4 lore raw
In reply to Nirmal Patel
Quoted 4 lines · Nirmal Patel

Copy valid OSC flags from root bridge only when OS can provide correct values. In virtual machine, all of the control features are disabled as hypervisor will not passthrough root bridge information to VM.

I changed the subject and description as below while applying:

PCI: vmd: Only copy root bridge _OSC control flags in bare metal OS

Only in a bare metal OS environment, the _OSC control flags like Hotplug, PME, AER, etc... reflect the physical root bridge capabilities. But in a VM environment, these flags reflect the hypervisor policy and in most cases, the hypervisor disables all of these control flags to the Guest.

So copying these flags would needlessly disable these features in the VMD owned Root Ports. Hence, copy the flags only when VMD is running in a bare metal OS environment.

Quoted 42 lines

---
v3 : Removed period from commit and added a black line.
v2 : Added descriptive boolean and removed period from commit message.
---

---
 drivers/pci/controller/vmd.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/pci/controller/vmd.c +5 -2
index d4ae250d4bc6..7c7d110290d6 100644
@@ -782,6 +782,7 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 	resource_size_t membar2_offset = 0x2000;
 	struct pci_bus *child;
 	struct pci_dev *dev;
+	bool vmd_in_guest;
 	int ret;
 
 	/*
@@ -862,14 +863,16 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 		.parent = res,
 	};
 
+	/* Non-zero offset means guest/direct assign view. */
+	vmd_in_guest = offset[0] || offset[1];
+
 	/*
 	 * Currently MSI remapping must be enabled in guest passthrough mode
 	 * due to some missing interrupt remapping plumbing. This is probably
 	 * acceptable because the guest is usually CPU-limited and MSI
 	 * remapping doesn't become a performance bottleneck.
 	 */
-	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
-	    offset[0] || offset[1]) {
+	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) || vmd_in_guest) {
 		ret = vmd_alloc_irqs(vmd);
 		if (ret)
 			return ret;

drivers/pci/controller/vmd.c static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) · line 916

Quoted 6 lines
drivers/pci/controller/vmd.c +1 -2
@@ -910,8 +913,10 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
 		return -ENODEV;
 	}
 
-	vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
-				   to_pci_host_bridge(vmd->bus->bridge));
+	/* Don't copy _OSC control flags in VM, it disables features. */

Also reworded this comment:

/*
* Don't copy _OSC control flags from root bridge if running in a VM, as
* they don't reflect the physical root bridge capabilities. */

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்