ionic_dev_setup() points idev.hwstamp_regs into the BAR0 mapping, but a PCI reset unmaps BAR0 with the pointer still set. The PHC stays registered across the reset, and its operations test FW_RESET without a lock before reading the counter, so one that passed the test just before the reset reads the unmapped BAR. Make phc->lock protect the pointer. The timecounter reads the counter through a callback that cannot report a failure, so each operation has to check the pointer before it starts the update, and the pointer must not change until the update ends. ionic_dev_setup() no longer sets the pointer, since it also runs during reset done, outside phc->lock. Reproduced on an AMD Pensando NIC under KASAN by holding phc_ctl get after the FW_RESET test in ionic_phc_gettimex64() with a test-only kprobe module while a PCI reset ran, flushing the local TLB so the stale mapping faults: without this patch the counter read faults, with it the read finds hwstamp_regs cleared. Fixes: a79b559e99be ("ionic: add FLR recovery support") Reported-by: sashiko-bot@kernel.org # Running on a local machine Reviewed-by: Brett Creeley Signed-off-by: Nikhil P. Rao --- drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c | 16 ++++++++++++++++ drivers/net/ethernet/pensando/ionic/ionic_dev.c | 2 -- drivers/net/ethernet/pensando/ionic/ionic_lif.h | 2 +- drivers/net/ethernet/pensando/ionic/ionic_phc.c | 20 +++++++++++++++++--- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c b/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c index 07bc61d6f964..1c283280ce83 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c @@ -444,6 +444,19 @@ static void ionic_remove(struct pci_dev *pdev) ionic_devlink_free(ionic); } +static void ionic_phc_set_regs(struct ionic_lif *lif, + struct ionic_hwstamp_regs __iomem *regs) +{ + unsigned long irqflags; + + if (!lif->phc) + return; + + spin_lock_irqsave(&lif->phc->lock, irqflags); + lif->ionic->idev.hwstamp_regs = regs; + spin_unlock_irqrestore(&lif->phc->lock, irqflags); +} + static void ionic_reset_prepare(struct pci_dev *pdev) { struct ionic *ionic = pci_get_drvdata(pdev); @@ -452,6 +465,7 @@ static void ionic_reset_prepare(struct pci_dev *pdev) dev_dbg(ionic->dev, "%s: device stopping\n", __func__); set_bit(IONIC_LIF_F_FW_RESET, lif->state); + ionic_phc_set_regs(lif, NULL); timer_delete_sync(&ionic->watchdog_timer); cancel_work_sync(&lif->deferred.work); @@ -480,6 +494,8 @@ static void ionic_reset_done(struct pci_dev *pdev) if (err) goto err_out; + ionic_phc_set_regs(lif, &ionic->idev.dev_info_regs->hwstamp); + ionic_debugfs_add_sizes(ionic); ionic_debugfs_add_lif(ionic->lif); diff --git a/drivers/net/ethernet/pensando/ionic/ionic_dev.c b/drivers/net/ethernet/pensando/ionic/ionic_dev.c index 5ec5af41141a..ce6944f24734 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_dev.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_dev.c @@ -418,8 +418,6 @@ int ionic_dev_setup(struct ionic *ionic) idev->intr_status = bar->vaddr + IONIC_BAR0_INTR_STATUS_OFFSET; idev->intr_ctrl = bar->vaddr + IONIC_BAR0_INTR_CTRL_OFFSET; - idev->hwstamp_regs = &idev->dev_info_regs->hwstamp; - sig = ioread32(&idev->dev_info_regs->signature); if (sig != IONIC_DEV_INFO_SIGNATURE) { dev_err(dev, "Incompatible firmware signature %x", sig); diff --git a/drivers/net/ethernet/pensando/ionic/ionic_lif.h b/drivers/net/ethernet/pensando/ionic/ionic_lif.h index e790f6b9441e..5def4ae13863 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_lif.h +++ b/drivers/net/ethernet/pensando/ionic/ionic_lif.h @@ -248,7 +248,7 @@ struct ionic_lif { }; struct ionic_phc { - spinlock_t lock; /* lock for state_page, cc and tc */ + spinlock_t lock; /* lock for state_page, cc, tc and idev.hwstamp_regs */ struct cyclecounter cc; struct timecounter tc; diff --git a/drivers/net/ethernet/pensando/ionic/ionic_phc.c b/drivers/net/ethernet/pensando/ionic/ionic_phc.c index 388150cbf7e4..2631bc3e9580 100644 --- a/drivers/net/ethernet/pensando/ionic/ionic_phc.c +++ b/drivers/net/ethernet/pensando/ionic/ionic_phc.c @@ -377,6 +377,10 @@ static int ionic_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm) adj += phc->init_cc_mult; spin_lock_irqsave(&phc->lock, irqflags); + if (!phc->lif->ionic->idev.hwstamp_regs) { + spin_unlock_irqrestore(&phc->lock, irqflags); + return -EBUSY; + } /* update the point-in-time basis to now, before adjusting the rate */ timecounter_read(&phc->tc); @@ -437,6 +441,10 @@ static int ionic_phc_settime64(struct ptp_clock_info *info, ns = timespec64_to_ns(ts); spin_lock_irqsave(&phc->lock, irqflags); + if (!phc->lif->ionic->idev.hwstamp_regs) { + spin_unlock_irqrestore(&phc->lock, irqflags); + return -EBUSY; + } timecounter_init(&phc->tc, &phc->cc, ns); @@ -466,6 +474,10 @@ static int ionic_phc_gettimex64(struct ptp_clock_info *info, return -EBUSY; spin_lock_irqsave(&phc->lock, irqflags); + if (!ionic->idev.hwstamp_regs) { + spin_unlock_irqrestore(&phc->lock, irqflags); + return -EBUSY; + } tick = ionic_hwstamp_read(ionic, sts); @@ -495,6 +507,10 @@ static long ionic_phc_aux_work(struct ptp_clock_info *info) return phc->aux_work_delay; spin_lock_irqsave(&phc->lock, irqflags); + if (!phc->lif->ionic->idev.hwstamp_regs) { + spin_unlock_irqrestore(&phc->lock, irqflags); + return phc->aux_work_delay; + } /* update point-in-time basis to now */ timecounter_read(&phc->tc); @@ -576,9 +592,6 @@ void ionic_lif_alloc_phc(struct ionic_lif *lif) u64 features; u32 shift; - if (!ionic->idev.hwstamp_regs) - return; - features = le64_to_cpu(ionic->ident.lif.eth.config.features); if (!(features & (IONIC_ETH_HW_TIMESTAMP | IONIC_ETH_HW_RDMA_TIMESTAMP))) return; @@ -672,6 +685,7 @@ void ionic_lif_alloc_phc(struct ionic_lif *lif) /* frequency adjustments are relative to the initial multiplier */ phc->init_cc_mult = phc->cc.mult; + ionic->idev.hwstamp_regs = &ionic->idev.dev_info_regs->hwstamp; timecounter_init(&phc->tc, &phc->cc, ktime_get_real_ns()); /* Update cycle_last at 1/4 the wrap period, or IONIC_PHC_UPDATE_NS */ -- 2.43.0