netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] sfc: fix crash when reading stats while NIC is resetting
@ 2023-06-23 14:34 edward.cree
  2023-06-26  8:30 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: edward.cree @ 2023-06-23 14:34 UTC (permalink / raw)
  To: linux-net-drivers, davem, kuba, edumazet, pabeni
  Cc: Edward Cree, netdev, habetsm.xilinx, Pieter Jansen van Vuuren

From: Edward Cree <ecree.xilinx@gmail.com>

efx_net_stats() (.ndo_get_stats64) can be called during an ethtool
 selftest, during which time nic_data->mc_stats is NULL as the NIC has
 been fini'd.  In this case do not attempt to fetch the latest stats
 from the hardware, else we will crash on a NULL dereference:
    BUG: kernel NULL pointer dereference, address: 0000000000000038
    RIP efx_nic_update_stats
    abridged calltrace:
    efx_ef10_update_stats_pf
    efx_net_stats
    dev_get_stats
    dev_seq_printf_stats
Skipping the read is safe, we will simply give out stale stats.
To ensure that the free in efx_ef10_fini_nic() does not race against
 efx_ef10_update_stats_pf(), which could cause a TOCTTOU bug, take the
 efx->stats_lock in fini_nic (it is already held across update_stats).

Fixes: d3142c193dca ("sfc: refactor EF10 stats handling")
Reviewed-by: Pieter Jansen van Vuuren <pieter.jansen-van-vuuren@amd.com>
Signed-off-by: Edward Cree <ecree.xilinx@gmail.com>
---
 drivers/net/ethernet/sfc/ef10.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/sfc/ef10.c b/drivers/net/ethernet/sfc/ef10.c
index b63e47af6365..8c019f382a7f 100644
--- a/drivers/net/ethernet/sfc/ef10.c
+++ b/drivers/net/ethernet/sfc/ef10.c
@@ -1297,8 +1297,10 @@ static void efx_ef10_fini_nic(struct efx_nic *efx)
 {
 	struct efx_ef10_nic_data *nic_data = efx->nic_data;
 
+	spin_lock_bh(&efx->stats_lock);
 	kfree(nic_data->mc_stats);
 	nic_data->mc_stats = NULL;
+	spin_unlock_bh(&efx->stats_lock);
 }
 
 static int efx_ef10_init_nic(struct efx_nic *efx)
@@ -1852,9 +1854,14 @@ static size_t efx_ef10_update_stats_pf(struct efx_nic *efx, u64 *full_stats,
 
 	efx_ef10_get_stat_mask(efx, mask);
 
-	efx_nic_copy_stats(efx, nic_data->mc_stats);
-	efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
-			     mask, stats, nic_data->mc_stats, false);
+	/* If NIC was fini'd (probably resetting), then we can't read
+	 * updated stats right now.
+	 */
+	if (nic_data->mc_stats) {
+		efx_nic_copy_stats(efx, nic_data->mc_stats);
+		efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
+				     mask, stats, nic_data->mc_stats, false);
+	}
 
 	/* Update derived statistics */
 	efx_nic_fix_nodesc_drop_stat(efx,

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] sfc: fix crash when reading stats while NIC is resetting
  2023-06-23 14:34 [PATCH net] sfc: fix crash when reading stats while NIC is resetting edward.cree
@ 2023-06-26  8:30 ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-06-26  8:30 UTC (permalink / raw)
  To: edward.cree
  Cc: linux-net-drivers, davem, kuba, edumazet, pabeni, ecree.xilinx,
	netdev, habetsm.xilinx, pieter.jansen-van-vuuren

Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <davem@davemloft.net>:

On Fri, 23 Jun 2023 15:34:48 +0100 you wrote:
> From: Edward Cree <ecree.xilinx@gmail.com>
> 
> efx_net_stats() (.ndo_get_stats64) can be called during an ethtool
>  selftest, during which time nic_data->mc_stats is NULL as the NIC has
>  been fini'd.  In this case do not attempt to fetch the latest stats
>  from the hardware, else we will crash on a NULL dereference:
>     BUG: kernel NULL pointer dereference, address: 0000000000000038
>     RIP efx_nic_update_stats
>     abridged calltrace:
>     efx_ef10_update_stats_pf
>     efx_net_stats
>     dev_get_stats
>     dev_seq_printf_stats
> Skipping the read is safe, we will simply give out stale stats.
> To ensure that the free in efx_ef10_fini_nic() does not race against
>  efx_ef10_update_stats_pf(), which could cause a TOCTTOU bug, take the
>  efx->stats_lock in fini_nic (it is already held across update_stats).
> 
> [...]

Here is the summary with links:
  - [net] sfc: fix crash when reading stats while NIC is resetting
    https://git.kernel.org/netdev/net/c/d1b355438b83

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-26  8:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-23 14:34 [PATCH net] sfc: fix crash when reading stats while NIC is resetting edward.cree
2023-06-26  8:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).