ionic_clear_pci() clears idev.dev_cmd_regs and unmaps BAR0 without dev_cmd_lock, so a devcmd from ethtool or devlink that has just tested the pointer can access the registers after the unmap. Make dev_cmd_lock protect dev_cmd_regs. ionic_dev_setup() publishes dev_info_regs inside the same lock, so a devcmd never finds the firmware running without its command registers. The identify and port setup devcmds are left unchanged: they run only from probe and recovery, which do not race a reset. Reproduced on an AMD Pensando NIC under KASAN by holding ethtool -d between the dev_cmd_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: 7662fad348ac ("ionic: check cmd_regs before copying in or out") Reported-by: Sashiko AI review Closes: https://lore.kernel.org/netdev/179539559673.3135.12079878343644861835@kernel.org/ Reviewed-by: Brett Creeley Signed-off-by: Nikhil P. Rao --- drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c | 2 ++ drivers/net/ethernet/pensando/ionic/ionic_dev.c | 4 ++++ drivers/net/ethernet/pensando/ionic/ionic_ethtool.c | 5 ++++- drivers/net/ethernet/pensando/ionic/ionic_fw.c | 6 ++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c b/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c index ddc2f9770df45..49941fc6bbc41 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c @@ -219,7 +219,9 @@ static void ionic_clear_pci(struct ionic *ionic) { if (ionic->num_bars) { 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; diff --git a/drivers/net/ethernet/pensando/ionic/ionic_dev.c b/drivers/net/ethernet/pensando/ionic/ionic_dev.c index 648d9d24be856..bd41bbcd37a02 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_dev.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_dev.c @@ -409,8 +409,10 @@ int ionic_dev_setup(struct ionic *ionic) return -EFAULT; } + mutex_lock(&ionic->dev_cmd_lock); 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; idev->intr_ctrl = bar->vaddr + IONIC_BAR0_INTR_CTRL_OFFSET; @@ -811,8 +813,10 @@ void ionic_vf_start(struct ionic *ionic) if (!(ionic->ident.dev.capabilities & cpu_to_le64(IONIC_DEV_CAP_VF_CTRL))) return; + mutex_lock(&ionic->dev_cmd_lock); ionic_dev_cmd_go(&ionic->idev, &cmd); ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT); + mutex_unlock(&ionic->dev_cmd_lock); } /* LIF commands */ diff --git a/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c b/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c index 24fd34b4cfccc..d899e44c39027 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_ethtool.c @@ -108,7 +108,10 @@ static void ionic_get_regs(struct net_device *netdev, struct ethtool_regs *regs, offset += size; size = IONIC_DEV_CMD_REG_COUNT * sizeof(u32); - memcpy_fromio(p + offset, idev->dev_cmd_regs->words, size); + mutex_lock(&lif->ionic->dev_cmd_lock); + if (idev->dev_cmd_regs) + memcpy_fromio(p + offset, idev->dev_cmd_regs->words, size); + mutex_unlock(&lif->ionic->dev_cmd_lock); } static void ionic_get_link_ext_stats(struct net_device *netdev, diff --git a/drivers/net/ethernet/pensando/ionic/ionic_fw.c b/drivers/net/ethernet/pensando/ionic/ionic_fw.c index 3c209c1a23373..f946c49bc5f6e 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_fw.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_fw.c @@ -131,6 +131,12 @@ int ionic_firmware_update(struct ionic_lif *lif, const struct firmware *fw, copy_sz = min_t(unsigned int, buf_sz, fw->size - offset); mutex_lock(&ionic->dev_cmd_lock); + if (!idev->dev_cmd_regs) { + mutex_unlock(&ionic->dev_cmd_lock); + err = -ENXIO; + NL_SET_ERR_MSG_MOD(extack, "Device reset during flash"); + goto err_out; + } memcpy_toio(&idev->dev_cmd_regs->data, fw->data + offset, copy_sz); ionic_dev_cmd_firmware_download(idev, offsetof(union ionic_dev_cmd_regs, data), -- 2.43.0