From: Alexander Duyck The HW stats are kept as running totals that advance by the difference between each counter and the value last read, so the last values have to be captured before the first read, and rebased whenever the hardware counters are reset. Three paths get the ordering wrong. The fbnic_probe function creates the debugfs directory, including the pcie_stats file, before it calls the fbnic_init_hw_stats function. A read in that window takes the stats lock before it has been initialized, and accumulates deltas against baselines that have not been captured. On resume, the __fbnic_pm_attach function rebases the stats only after it has returned early for a device in init failure mode, though such a device still exposes the PCIe stats through debugfs. And during PCI error recovery it runs from the resume callback, after the __fbnic_pm_resume function has already restored MMIO access from the slot_reset callback, so a debugfs read in between takes its delta against the values from before the reset and the totals jump by close to the full width of each counter. Initialize the stats before the debugfs directory and the devlink instance are registered. Rebase them in the __fbnic_pm_resume function, in the same stats lock section that restores MMIO access, so no read can see the reset counters first, ahead of its init failure check. The issue was found by code review, and the recovery case confirmed by hardware testing with forced AER/DPC recovery. Fixes: 25ba596d137d ("eth: fbnic: add PCIe hardware statistics") Fixes: 9f61eb2d185b ("eth: fbnic: add locking support for hw stats") Fixes: 05ec5504ff69 ("fbnic: Fix service task races with ndo_stop and PM suspend") Signed-off-by: Alexander Duyck --- drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.c | 11 +++++++++-- drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.h | 1 + drivers/net/ethernet/meta/fbnic/fbnic_pci.c | 16 ++++++++++------ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.c b/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.c index 307d5e737848..966c2d0fb27a 100644 --- a/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.c +++ b/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.c @@ -547,9 +547,10 @@ static void fbnic_reset_hw_mac_stats(struct fbnic_dev *fbd, mac->get_rmon_stats(fbd, true, &mac_stats->rmon); } -void fbnic_reset_hw_stats(struct fbnic_dev *fbd) +void __fbnic_reset_hw_stats(struct fbnic_dev *fbd) { - spin_lock(&fbd->hw_stats.lock); + lockdep_assert_held(&fbd->hw_stats.lock); + fbnic_reset_phy_stats(fbd, &fbd->hw_stats.phy); fbnic_reset_tmi_stats(fbd, &fbd->hw_stats.tmi); fbnic_reset_tti_stats(fbd, &fbd->hw_stats.tti); @@ -558,6 +559,12 @@ void fbnic_reset_hw_stats(struct fbnic_dev *fbd) fbnic_reset_hw_rxq_stats(fbd, fbd->hw_stats.hw_q); fbnic_reset_pcie_stats_asic(fbd, &fbd->hw_stats.pcie); fbnic_reset_hw_mac_stats(fbd, &fbd->hw_stats.mac); +} + +void fbnic_reset_hw_stats(struct fbnic_dev *fbd) +{ + spin_lock(&fbd->hw_stats.lock); + __fbnic_reset_hw_stats(fbd); spin_unlock(&fbd->hw_stats.lock); } diff --git a/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.h b/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.h index caea4be46762..5eb1e8b29ef9 100644 --- a/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.h +++ b/drivers/net/ethernet/meta/fbnic/fbnic_hw_stats.h @@ -154,6 +154,7 @@ struct fbnic_hw_stats { u64 fbnic_stat_rd64(struct fbnic_dev *fbd, u32 reg, u32 offset); +void __fbnic_reset_hw_stats(struct fbnic_dev *fbd); void fbnic_reset_hw_stats(struct fbnic_dev *fbd); void fbnic_init_hw_stats(struct fbnic_dev *fbd); void fbnic_get_hw_q_stats(struct fbnic_dev *fbd, diff --git a/drivers/net/ethernet/meta/fbnic/fbnic_pci.c b/drivers/net/ethernet/meta/fbnic/fbnic_pci.c index 09ae797b1f0d..cb0191b65204 100644 --- a/drivers/net/ethernet/meta/fbnic/fbnic_pci.c +++ b/drivers/net/ethernet/meta/fbnic/fbnic_pci.c @@ -350,13 +350,13 @@ static int fbnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) */ fbnic_fw_log_enable(fbd, true); + /* Capture snapshot of hardware stats so netdev can calculate delta */ + fbnic_init_hw_stats(fbd); + fbnic_devlink_register(fbd); fbnic_devlink_otp_check(fbd, "error detected during probe"); fbnic_dbg_fbd_init(fbd); - /* Capture snapshot of hardware stats so netdev can calculate delta */ - fbnic_init_hw_stats(fbd); - fbnic_hwmon_register(fbd); if (!fbd->dsn) { @@ -498,10 +498,16 @@ static int __fbnic_pm_resume(struct device *dev) struct fbnic_net *fbn; int err; - /* Restore MMIO access */ + /* Restore MMIO access and rebase the HW stats in one step, so that + * no stats read can take its delta from counters the reset cleared + * against the values read before it. + */ iomap_table = pcim_iomap_table(to_pci_dev(dev)); + spin_lock(&fbd->hw_stats.lock); fbd->uc_addr0 = iomap_table[0]; fbd->uc_addr4 = iomap_table[4]; + __fbnic_reset_hw_stats(fbd); + spin_unlock(&fbd->hw_stats.lock); /* Rerequest the IRQs */ err = fbnic_alloc_irqs(fbd); @@ -582,8 +588,6 @@ static void __fbnic_pm_attach(struct device *dev) rtnl_lock(); netdev_lock(netdev); - fbnic_reset_hw_stats(fbd); - if (netif_running(netdev)) fbnic_up(fbn);