ionic_clear_pci() clears idev.dev_info_regs and unmaps BAR0 while lockless readers that only NULL-check it can still be running: the firmware status and heartbeat checks and the ethtool register dump. A PCI reset can unmap the BAR between their check and their read. The readers run in atomic context (the watchdog timer, and ionic_adminq_post() under adminq_lock), so protect the pointer with RCU rather than a lock. The pointer is __iomem and sparse allows only one address space, so the RCU accessors are open-coded. Reproduced on an AMD Pensando NIC under KASAN by holding ethtool -d between the dev_info_regs check and its read with a test-only kprobe module while a PCI reset ran, flushing the local TLB so the stale mapping faults: without this patch the read faults, with it the reset waits for the reader. Fixes: a79b559e99be ("ionic: add FLR recovery support") Fixes: 219e183272b4 ("ionic: no fw read when PCI reset failed") Reported-by: sashiko-bot@kernel.org # Running on a local machine Reviewed-by: Brett Creeley Signed-off-by: Nikhil P. Rao --- .../net/ethernet/pensando/ionic/ionic_bus_pci.c | 3 ++- drivers/net/ethernet/pensando/ionic/ionic_dev.c | 28 +++++++++++++--------- .../net/ethernet/pensando/ionic/ionic_ethtool.c | 10 ++++++-- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c b/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c index 49941fc6bbc4..07bc61d6f964 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c @@ -218,12 +218,13 @@ static int ionic_sriov_configure(struct pci_dev *pdev, int num_vfs) static void ionic_clear_pci(struct ionic *ionic) { if (ionic->num_bars) { - ionic->idev.dev_info_regs = NULL; + WRITE_ONCE(ionic->idev.dev_info_regs, NULL); mutex_lock(&ionic->dev_cmd_lock); ionic->idev.dev_cmd_regs = NULL; mutex_unlock(&ionic->dev_cmd_lock); ionic->idev.intr_status = NULL; ionic->idev.intr_ctrl = NULL; + synchronize_rcu(); ionic_unmap_bars(ionic); pci_release_regions(ionic->pdev); diff --git a/drivers/net/ethernet/pensando/ionic/ionic_dev.c b/drivers/net/ethernet/pensando/ionic/ionic_dev.c index bd41bbcd37a0..5ec5af41141a 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_dev.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_dev.c @@ -410,7 +410,9 @@ int ionic_dev_setup(struct ionic *ionic) } mutex_lock(&ionic->dev_cmd_lock); - idev->dev_info_regs = bar->vaddr + IONIC_BAR0_DEV_INFO_REGS_OFFSET; + /* Pairs with READ_ONCE() in the RCU readers of dev_info_regs */ + smp_store_release(&idev->dev_info_regs, + bar->vaddr + IONIC_BAR0_DEV_INFO_REGS_OFFSET); idev->dev_cmd_regs = bar->vaddr + IONIC_BAR0_DEV_CMD_REGS_OFFSET; mutex_unlock(&ionic->dev_cmd_lock); idev->intr_status = bar->vaddr + IONIC_BAR0_INTR_STATUS_OFFSET; @@ -477,21 +479,17 @@ void ionic_dev_teardown(struct ionic *ionic) /* Devcmd Interface */ static bool __ionic_is_fw_running(struct ionic_dev *idev, u8 *status_ptr) { + union ionic_dev_info_regs __iomem *info_regs; u8 fw_status; - if (!idev->dev_info_regs) { - if (status_ptr) - *status_ptr = 0xff; - return false; - } + rcu_read_lock(); + info_regs = READ_ONCE(idev->dev_info_regs); + fw_status = info_regs ? ioread8(&info_regs->fw_status) : 0xff; + rcu_read_unlock(); - fw_status = ioread8(&idev->dev_info_regs->fw_status); if (status_ptr) *status_ptr = fw_status; - /* firmware is useful only if the running bit is set and - * fw_status != 0xff (bad PCI read) - */ return (fw_status != 0xff) && (fw_status & IONIC_FW_STS_F_RUNNING); } @@ -502,6 +500,7 @@ bool ionic_is_fw_running(struct ionic_dev *idev) int ionic_heartbeat_check(struct ionic *ionic) { + union ionic_dev_info_regs __iomem *info_regs; unsigned long check_time, last_check_time; struct ionic_dev *idev = &ionic->idev; struct ionic_lif *lif = ionic->lif; @@ -598,7 +597,14 @@ int ionic_heartbeat_check(struct ionic *ionic) if (time_before(check_time, last_check_time + DEVCMD_TIMEOUT * 2 * HZ)) return 0; - fw_hb = ioread32(&idev->dev_info_regs->fw_heartbeat); + rcu_read_lock(); + info_regs = READ_ONCE(idev->dev_info_regs); + if (!info_regs) { + rcu_read_unlock(); + return -ENXIO; + } + fw_hb = ioread32(&info_regs->fw_heartbeat); + rcu_read_unlock(); fw_hb_ready = fw_hb != idev->last_fw_hb; /* early FW version had no heartbeat, so fake it */ diff --git a/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c b/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c index d899e44c3902..1360a3dfb7c6 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c @@ -91,6 +91,7 @@ static int ionic_get_regs_len(struct net_device *netdev) static void ionic_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p) { + union ionic_dev_info_regs __iomem *info_regs; struct ionic_lif *lif = netdev_priv(netdev); struct ionic_dev *idev; unsigned int offset; @@ -99,12 +100,17 @@ static void ionic_get_regs(struct net_device *netdev, struct ethtool_regs *regs, regs->version = IONIC_DEV_CMD_REG_VERSION; idev = &lif->ionic->idev; - if (!idev->dev_info_regs) + rcu_read_lock(); + info_regs = READ_ONCE(idev->dev_info_regs); + if (!info_regs) { + rcu_read_unlock(); return; + } offset = 0; size = IONIC_DEV_INFO_REG_COUNT * sizeof(u32); - memcpy_fromio(p + offset, lif->ionic->idev.dev_info_regs->words, size); + memcpy_fromio(p + offset, info_regs->words, size); + rcu_read_unlock(); offset += size; size = IONIC_DEV_CMD_REG_COUNT * sizeof(u32); -- 2.43.0