* Re: [PATCH] PCI: hv: Don't use container_of() on non-vPCI buses in config block ops
2026-11-19 19:29 [PATCH] PCI: hv: Don't use container_of() on non-vPCI buses in config block ops Dexuan Cui
2026-11-19 19:34 ` sashiko-bot
@ 2026-11-19 23:15 ` Hamza Mahfooz
2026-11-20 17:41 ` Michael Kelley
2 siblings, 0 replies; 4+ messages in thread
From: Hamza Mahfooz @ 2026-11-19 23:15 UTC (permalink / raw)
To: Dexuan Cui
Cc: haiyangz, wei.liu, lpieralisi, kwilczynski, mani, robh, bhelgaas,
davem, jakeo, saeedm, linux-hyperv, linux-pci, linux-kernel,
abhamidipati, stable
On Thu, Nov 19, 2026 at 11:29:37AM -0800, Dexuan Cui wrote:
> From: Aditya Bhamidipati <abhamidipati@microsoft.com>
>
> hv_read_config_block(), hv_write_config_block() and
> hv_register_block_invalidate() are exported through hvpci_block_ops for
> mlx5_core, which calls them for its PCI function whenever it runs in a
> Hyper-V guest. They assume that pdev->bus->sysdata is embedded in a
> struct hv_pcibus_device and use container_of() on it.
>
> That only holds for buses created by pci-hyperv. A Hyper-V guest can also
> have PCI devices behind other host bridges, for example the ACPI-enumerated
> ECAM root ports used for ePCIe (emulated PCIe) device assignment on Azure.
> For such a device, container_of() yields a bogus hv_pcibus_device, and
> mlx5_core crashes on a ConnectX VF. On a 7.0-based kernel it crashes in
> get_pcichild_wslot(), called from hv_register_block_invalidate() while
> the VF probes. On a 7.3-based kernel the VHCA invalidate work sends a
> packet on a bogus channel; UBSAN flags the spinlock, and a paging fault
> follows:
>
> UBSAN: array-index-out-of-bounds in kernel/locking/qspinlock.h:68:9
> index 2129 is out of range for type 'long unsigned int [512]'
> Workqueue: mlx5_hv_vhca mlx5_hv_vhca_invalidate_work [mlx5_core]
> Call trace:
> queued_spin_lock_slowpath+0x644/0x6e0
> _raw_spin_lock_irqsave+0x98/0xf0
> hv_ringbuffer_write+0x98/0x340
> vmbus_sendpacket+0x9c/0x100
> hv_read_config_block+0xf4/0x1b0
> hyperv_read_cfg_blk+0x28/0x80
> mlx5_hv_read_config+0x6c/0x130 [mlx5_core]
> mlx5_hv_vhca_control_agent_invalidate+0x8c/0x280 [mlx5_core]
> mlx5_hv_vhca_invalidate_work+0x88/0x120 [mlx5_core]
> process_one_work+0x1dc/0x5b8
>
> Commit 0d5acba6331c ("Drivers: hv: vmbus: Export hv_vmbus_exists() and
> use it in pci-hyperv") fixed the same crash for guests without VMBus, by
> not installing the ops at all. These guests do have VMBus, and their NIC
> and OS disk are on vPCI buses, so the ops are installed and a per-device
> check is still needed.
>
> Treat the bus as a vPCI bus only if its ops are hv_pcifront_ops, and
> return -EOPNOTSUPP otherwise. mlx5_core ignores a failure to register the
> Hyper-V VHCA invalidate callback, so the VF probes normally.
>
> Tested on an Azure ARM64 VM with 8 ConnectX VFs behind ACPI ePCIe host
> bridges, with a 7.0- and a 7.3-based kernel: all 8 VFs probe and their IB
> ports go Active, and a test module confirms that all three ops
> return -EOPNOTSUPP for a device that is not on a vPCI bus.
>
> Fixes: e5d2f910cfec ("PCI: hv: Add a paravirtual backchannel in software")
> Cc: stable@vger.kernel.org
> Signed-off-by: Aditya Bhamidipati <abhamidipati@microsoft.com>
> Signed-off-by: Dexuan Cui <decui@microsoft.com>
> ---
>
> It's based on the PCI tree's for-linus branch, and can also apply cleanly
> to v7.4-rc3, pci/next, hyperv-next and linux-next.
>
> drivers/pci/controller/pci-hyperv.c | 36 +++++++++++++++++++++--------
> 1 file changed, 27 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
> index 824cf27e85e67..029d66a171216 100644
> --- a/drivers/pci/controller/pci-hyperv.c
> +++ b/drivers/pci/controller/pci-hyperv.c
> @@ -1451,6 +1451,21 @@ struct hv_read_config_compl {
> unsigned int bytes_returned;
> };
>
> +/*
> + * The config block ops are exported through hvpci_block_ops and can be called
> + * for any PCI device, including devices behind a non-Hyper-V host bridge
> + * (for example, ACPI-enumerated ePCIe roots). Only devices on a bus created by
> + * this driver are backed by a struct hv_pcibus_device.
> + */
> +static struct hv_pcibus_device *hv_pci_dev_to_hbus(struct pci_dev *pdev)
nit: seems like this would be a good candidate for inlining.
> +{
> + if (pdev->bus->ops != &hv_pcifront_ops)
It's worth checking for !pci_is_root_bus(pdev->bus) here as well (since
pci-hyperv only creates the root bus).
Hamza
> + return NULL;
> +
> + return container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> + sysdata);
> +}
> +
> /**
> * hv_pci_read_config_compl() - Invoked when a response packet
> * for a read config block operation arrives.
> @@ -1500,9 +1515,7 @@ static int hv_read_config_block(struct pci_dev *pdev, void *buf,
> unsigned int len, unsigned int block_id,
> unsigned int *bytes_returned)
> {
> - struct hv_pcibus_device *hbus =
> - container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> - sysdata);
> + struct hv_pcibus_device *hbus = hv_pci_dev_to_hbus(pdev);
> struct {
> struct pci_packet pkt;
> char buf[sizeof(struct pci_read_block)];
> @@ -1511,6 +1524,9 @@ static int hv_read_config_block(struct pci_dev *pdev, void *buf,
> struct pci_read_block *read_blk;
> int ret;
>
> + if (!hbus)
> + return -EOPNOTSUPP;
> +
> if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
> return -EINVAL;
>
> @@ -1581,9 +1597,7 @@ static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
> static int hv_write_config_block(struct pci_dev *pdev, void *buf,
> unsigned int len, unsigned int block_id)
> {
> - struct hv_pcibus_device *hbus =
> - container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> - sysdata);
> + struct hv_pcibus_device *hbus = hv_pci_dev_to_hbus(pdev);
> struct {
> struct pci_packet pkt;
> char buf[sizeof(struct pci_write_block)];
> @@ -1594,6 +1608,9 @@ static int hv_write_config_block(struct pci_dev *pdev, void *buf,
> u32 pkt_size;
> int ret;
>
> + if (!hbus)
> + return -EOPNOTSUPP;
> +
> if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
> return -EINVAL;
>
> @@ -1652,11 +1669,12 @@ static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
> void (*block_invalidate)(void *context,
> u64 block_mask))
> {
> - struct hv_pcibus_device *hbus =
> - container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> - sysdata);
> + struct hv_pcibus_device *hbus = hv_pci_dev_to_hbus(pdev);
> struct hv_pci_dev *hpdev;
>
> + if (!hbus)
> + return -EOPNOTSUPP;
> +
> hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
> if (!hpdev)
> return -ENODEV;
>
> base-commit: e735de71a2d9403fa1e2d134d1e9aa0a24f4eaff
> --
> 2.55.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread* RE: [PATCH] PCI: hv: Don't use container_of() on non-vPCI buses in config block ops
2026-11-19 19:29 [PATCH] PCI: hv: Don't use container_of() on non-vPCI buses in config block ops Dexuan Cui
2026-11-19 19:34 ` sashiko-bot
2026-11-19 23:15 ` Hamza Mahfooz
@ 2026-11-20 17:41 ` Michael Kelley
2 siblings, 0 replies; 4+ messages in thread
From: Michael Kelley @ 2026-11-20 17:41 UTC (permalink / raw)
To: Dexuan Cui, haiyangz@microsoft.com, wei.liu@kernel.org,
lpieralisi@kernel.org, kwilczynski@kernel.org, mani@kernel.org,
robh@kernel.org, bhelgaas@google.com, davem@davemloft.net,
jakeo@microsoft.com, saeedm@mellanox.com,
linux-hyperv@vger.kernel.org, linux-pci@vger.kernel.org,
linux-kernel@vger.kernel.org, abhamidipati@microsoft.com
Cc: stable@vger.kernel.org
From: Dexuan Cui <decui@microsoft.com> Sent: Thursday, November 19, 2026 11:30 AM
>
> From: Aditya Bhamidipati <abhamidipati@microsoft.com>
>
> hv_read_config_block(), hv_write_config_block() and
> hv_register_block_invalidate() are exported through hvpci_block_ops for
> mlx5_core, which calls them for its PCI function whenever it runs in a
> Hyper-V guest. They assume that pdev->bus->sysdata is embedded in a
> struct hv_pcibus_device and use container_of() on it.
>
> That only holds for buses created by pci-hyperv. A Hyper-V guest can also
> have PCI devices behind other host bridges, for example the ACPI-enumerated
> ECAM root ports used for ePCIe (emulated PCIe) device assignment on Azure.
> For such a device, container_of() yields a bogus hv_pcibus_device, and
> mlx5_core crashes on a ConnectX VF. On a 7.0-based kernel it crashes in
> get_pcichild_wslot(), called from hv_register_block_invalidate() while
> the VF probes. On a 7.3-based kernel the VHCA invalidate work sends a
> packet on a bogus channel; UBSAN flags the spinlock, and a paging fault
> follows:
>
> UBSAN: array-index-out-of-bounds in kernel/locking/qspinlock.h:68:9
> index 2129 is out of range for type 'long unsigned int [512]'
> Workqueue: mlx5_hv_vhca mlx5_hv_vhca_invalidate_work [mlx5_core]
> Call trace:
> queued_spin_lock_slowpath+0x644/0x6e0
> _raw_spin_lock_irqsave+0x98/0xf0
> hv_ringbuffer_write+0x98/0x340
> vmbus_sendpacket+0x9c/0x100
> hv_read_config_block+0xf4/0x1b0
> hyperv_read_cfg_blk+0x28/0x80
> mlx5_hv_read_config+0x6c/0x130 [mlx5_core]
> mlx5_hv_vhca_control_agent_invalidate+0x8c/0x280 [mlx5_core]
> mlx5_hv_vhca_invalidate_work+0x88/0x120 [mlx5_core]
> process_one_work+0x1dc/0x5b8
>
> Commit 0d5acba6331c ("Drivers: hv: vmbus: Export hv_vmbus_exists() and
> use it in pci-hyperv") fixed the same crash for guests without VMBus, by
> not installing the ops at all. These guests do have VMBus, and their NIC
> and OS disk are on vPCI buses, so the ops are installed and a per-device
> check is still needed.
>
> Treat the bus as a vPCI bus only if its ops are hv_pcifront_ops, and
> return -EOPNOTSUPP otherwise. mlx5_core ignores a failure to register the
> Hyper-V VHCA invalidate callback, so the VF probes normally.
>
> Tested on an Azure ARM64 VM with 8 ConnectX VFs behind ACPI ePCIe host
> bridges, with a 7.0- and a 7.3-based kernel: all 8 VFs probe and their IB
> ports go Active, and a test module confirms that all three ops
> return -EOPNOTSUPP for a device that is not on a vPCI bus.
>
> Fixes: e5d2f910cfec ("PCI: hv: Add a paravirtual backchannel in software")
> Cc: stable@vger.kernel.org
> Signed-off-by: Aditya Bhamidipati <abhamidipati@microsoft.com>
> Signed-off-by: Dexuan Cui <decui@microsoft.com>
> ---
>
> It's based on the PCI tree's for-linus branch, and can also apply cleanly
> to v7.4-rc3, pci/next, hyperv-next and linux-next.
>
> drivers/pci/controller/pci-hyperv.c | 36 +++++++++++++++++++++--------
> 1 file changed, 27 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
> index 824cf27e85e67..029d66a171216 100644
> --- a/drivers/pci/controller/pci-hyperv.c
> +++ b/drivers/pci/controller/pci-hyperv.c
> @@ -1451,6 +1451,21 @@ struct hv_read_config_compl {
> unsigned int bytes_returned;
> };
>
> +/*
> + * The config block ops are exported through hvpci_block_ops and can be called
> + * for any PCI device, including devices behind a non-Hyper-V host bridge
> + * (for example, ACPI-enumerated ePCIe roots). Only devices on a bus created by
> + * this driver are backed by a struct hv_pcibus_device.
> + */
> +static struct hv_pcibus_device *hv_pci_dev_to_hbus(struct pci_dev *pdev)
> +{
> + if (pdev->bus->ops != &hv_pcifront_ops)
> + return NULL;
> +
> + return container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> + sysdata);
> +}
In addition to hv_read_config_block(), hv_write_config_block(), and
hv_register_block_invalidate(), there are two other functions in
pci-hyperv.c that do the same mapping of a struct pci_dev to a
struct hv_pcibus_device:
hv_irq_retarget_interrupt()
hv_compose_msi_msg()
Also, hv_pcifront_read_config() and hv_pcifront_write_config()
do the same thing, but starting with a struct pci_bus instead of
a struct pci_dev. Maybe hv_pci_dev_to_hbus() should become
hv_pci_bus_to_hbus(), and callers can pass pdev->bus as the
first argument if need be.
Finally, there's hv_pci_reset_device(), which currently does the
same sysdata container_of() trick, but based on the bridge. Even
so, the struct pci_dev as also available, so hv_pci_reset_device()
could use hv_pci_dev_to_hbus().
This a long way of saying that there's a consistency argument
for using hv_pci_dev_to_hbus() [or hv_pci_bus_to_hbus()]
throughout pci-hyperv.c instead of just on the hvpci_block_ops
functions.
Separately, it seems a bit fragile for this mapping to depend
on the sysdata memory being allocated as part of the struct
hv_pcibus_device (vs. a dynamic allocation, for example). Of
course, that's the current code, and it works. But I'm
trying to think through whether there's a better way to do
the mapping. Just my random musings and not something
that needs to be covered in this patch ....
Michael
> +
> /**
> * hv_pci_read_config_compl() - Invoked when a response packet
> * for a read config block operation arrives.
> @@ -1500,9 +1515,7 @@ static int hv_read_config_block(struct pci_dev *pdev, void *buf,
> unsigned int len, unsigned int block_id,
> unsigned int *bytes_returned)
> {
> - struct hv_pcibus_device *hbus =
> - container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> - sysdata);
> + struct hv_pcibus_device *hbus = hv_pci_dev_to_hbus(pdev);
> struct {
> struct pci_packet pkt;
> char buf[sizeof(struct pci_read_block)];
> @@ -1511,6 +1524,9 @@ static int hv_read_config_block(struct pci_dev *pdev, void *buf,
> struct pci_read_block *read_blk;
> int ret;
>
> + if (!hbus)
> + return -EOPNOTSUPP;
> +
> if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
> return -EINVAL;
>
> @@ -1581,9 +1597,7 @@ static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
> static int hv_write_config_block(struct pci_dev *pdev, void *buf,
> unsigned int len, unsigned int block_id)
> {
> - struct hv_pcibus_device *hbus =
> - container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> - sysdata);
> + struct hv_pcibus_device *hbus = hv_pci_dev_to_hbus(pdev);
> struct {
> struct pci_packet pkt;
> char buf[sizeof(struct pci_write_block)];
> @@ -1594,6 +1608,9 @@ static int hv_write_config_block(struct pci_dev *pdev, void *buf,
> u32 pkt_size;
> int ret;
>
> + if (!hbus)
> + return -EOPNOTSUPP;
> +
> if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
> return -EINVAL;
>
> @@ -1652,11 +1669,12 @@ static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
> void (*block_invalidate)(void *context,
> u64 block_mask))
> {
> - struct hv_pcibus_device *hbus =
> - container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> - sysdata);
> + struct hv_pcibus_device *hbus = hv_pci_dev_to_hbus(pdev);
> struct hv_pci_dev *hpdev;
>
> + if (!hbus)
> + return -EOPNOTSUPP;
> +
> hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
> if (!hpdev)
> return -ENODEV;
>
> base-commit: e735de71a2d9403fa1e2d134d1e9aa0a24f4eaff
> --
> 2.55.0
>
^ permalink raw reply [flat|nested] 4+ messages in thread