Skip to content

Commit 0d5acba

Browse files
dcuiliuw
authored andcommitted
Drivers: hv: vmbus: Export hv_vmbus_exists() and use it in pci-hyperv
With commit f84b21d ("PCI: hv: Don't load the driver for baremetal root partition"), the bare metal Linux root partition won't use the pci-hyperv driver, but when a Linux VM runs on the Linux root partition, pci-hyperv's module_init function init_hv_pci_drv() can still run, e.g. in the case of CONFIG_PCI_HYPERV=y, even if the VMBus driver is not used in such a VM (i.e. the hv_vmbus driver's init function returns -ENODEV due to vmbus_root_device being NULL). In such a Linux VM, init_hv_pci_drv() runs with a side effect: the 3 hvpci_block_ops callbacks are set to functions that depend on hv_vmbus. Later, when the MLX driver in such a VM invokes the callbacks, e.g. in drivers/net/ethernet/mellanox/mlx5/core/lib/hv.c: mlx5_hv_register_invalidate(), hvpci_block_ops.reg_blk_invalidate() is hv_register_block_invalidate() rather than a NULL function pointer, and hv_register_block_invalidate() assumes that it can find a struct hv_pcibus_device from pdev->bus->sysdata, which is false in such a VM. Consequently, hv_register_block_invalidate() -> get_pcichild_wslot() -> spin_lock_irqsave() may hang since it can be accessing an invalid spinlock pointer. Fix the issue by exporting hv_vmbus_exists() and using it in pci-hyperv: hv_root_partition() is true and hv_nested is false ==> hv_vmbus_exists() is false. hv_root_partition() is true and hv_nested is true ==> hv_vmbus_exists() is true. hv_root_partition() is false ==> hv_vmbus_exists() is true. While at it, rename vmbus_exists() to hv_vmbus_exists() to follow the convention that all public functions have the hv_ prefix; also change the return value's type from int to bool to make the code more readable; also move the two pr_info() calls. Reported-by: Mukesh Rathor <mrathor@linux.microsoft.com> Signed-off-by: Dexuan Cui <decui@microsoft.com> Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent 80acc80 commit 0d5acba

3 files changed

Lines changed: 11 additions & 13 deletions

File tree

drivers/hv/vmbus_drv.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ struct device *hv_get_vmbus_root_device(void)
101101
}
102102
EXPORT_SYMBOL_GPL(hv_get_vmbus_root_device);
103103

104-
static int vmbus_exists(void)
104+
bool hv_vmbus_exists(void)
105105
{
106-
if (vmbus_root_device == NULL)
107-
return -ENODEV;
108-
109-
return 0;
106+
return vmbus_root_device != NULL;
110107
}
108+
EXPORT_SYMBOL_GPL(hv_vmbus_exists);
111109

112110
static u8 channel_monitor_group(const struct vmbus_channel *channel)
113111
{
@@ -1577,11 +1575,10 @@ int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, c
15771575
{
15781576
int ret;
15791577

1580-
pr_info("registering driver %s\n", hv_driver->name);
1578+
if (!hv_vmbus_exists())
1579+
return -ENODEV;
15811580

1582-
ret = vmbus_exists();
1583-
if (ret < 0)
1584-
return ret;
1581+
pr_info("registering driver %s\n", hv_driver->name);
15851582

15861583
hv_driver->driver.name = hv_driver->name;
15871584
hv_driver->driver.owner = owner;
@@ -1607,9 +1604,8 @@ EXPORT_SYMBOL_GPL(__vmbus_driver_register);
16071604
*/
16081605
void vmbus_driver_unregister(struct hv_driver *hv_driver)
16091606
{
1610-
pr_info("unregistering driver %s\n", hv_driver->name);
1611-
1612-
if (!vmbus_exists()) {
1607+
if (hv_vmbus_exists()) {
1608+
pr_info("unregistering driver %s\n", hv_driver->name);
16131609
driver_unregister(&hv_driver->driver);
16141610
vmbus_free_dynids(hv_driver);
16151611
}

drivers/pci/controller/pci-hyperv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4172,7 +4172,7 @@ static int __init init_hv_pci_drv(void)
41724172
if (!hv_is_hyperv_initialized())
41734173
return -ENODEV;
41744174

4175-
if (hv_root_partition() && !hv_nested)
4175+
if (!hv_vmbus_exists())
41764176
return -ENODEV;
41774177

41784178
ret = hv_pci_irqchip_init();

include/linux/hyperv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,8 @@ static inline void *hv_get_drvdata(struct hv_device *dev)
13041304

13051305
struct device *hv_get_vmbus_root_device(void);
13061306

1307+
bool hv_vmbus_exists(void);
1308+
13071309
struct hv_ring_buffer_debug_info {
13081310
u32 current_interrupt_mask;
13091311
u32 current_read_index;

0 commit comments

Comments
 (0)