Queue-vector teardown clears wx->num_tx_queues and wx->num_rx_queues while the netdev can remain registered. Lockless readers may use these counts as ring-array loop bounds while teardown is clearing the ring slots. wx_xmit_frame() also uses the Tx count to select a ring and must not divide by zero during teardown. Use READ_ONCE() to take a single count snapshot in the lockless readers, and use WRITE_ONCE() for queue-count updates during scheme setup, teardown, and allocation-error handling, including the VF queue setup callbacks. Keep the existing NULL checks so a reader that observed an old non-zero count can safely skip ring slots that have since been cleared. wx_xmit_frame() retains its zero-count guard before queue-index modulo. Fixes: 3f703186113f ("net: libwx: Add irq flow functions") Signed-off-by: Mengyuan Lou --- drivers/net/ethernet/wangxun/libwx/wx_err.c | 7 +++-- drivers/net/ethernet/wangxun/libwx/wx_hw.c | 23 ++++++++++----- drivers/net/ethernet/wangxun/libwx/wx_lib.c | 29 ++++++++++--------- drivers/net/ethernet/wangxun/libwx/wx_ptp.c | 5 ++-- .../net/ethernet/wangxun/ngbevf/ngbevf_main.c | 4 +-- .../ethernet/wangxun/txgbevf/txgbevf_main.c | 10 +++---- 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/drivers/net/ethernet/wangxun/libwx/wx_err.c b/drivers/net/ethernet/wangxun/libwx/wx_err.c index 8eadb4d3abcff..fa0f7c57090f5 100644 --- a/drivers/net/ethernet/wangxun/libwx/wx_err.c +++ b/drivers/net/ethernet/wangxun/libwx/wx_err.c @@ -199,10 +199,11 @@ EXPORT_SYMBOL(wx_init_err_task); static bool wx_ring_tx_pending(struct wx *wx) { + int num_tx_queues = READ_ONCE(wx->num_tx_queues); int i; rcu_read_lock(); - for (i = 0; i < wx->num_tx_queues; i++) { + for (i = 0; i < num_tx_queues; i++) { struct wx_ring *tx_ring = rcu_dereference(wx->tx_ring[i]); if (!tx_ring) @@ -261,6 +262,7 @@ static void wx_watchdog_flush_tx(struct wx *wx) static void wx_detect_tx_hang(struct wx *wx) { + int num_tx_queues; int i; /* If we're down or resetting, just bail */ @@ -270,8 +272,9 @@ static void wx_detect_tx_hang(struct wx *wx) /* Force detection of hung controller */ if (netif_carrier_ok(wx->netdev)) { + num_tx_queues = READ_ONCE(wx->num_tx_queues); rcu_read_lock(); - for (i = 0; i < wx->num_tx_queues; i++) { + for (i = 0; i < num_tx_queues; i++) { struct wx_ring *tx_ring = rcu_dereference(wx->tx_ring[i]); if (tx_ring) diff --git a/drivers/net/ethernet/wangxun/libwx/wx_hw.c b/drivers/net/ethernet/wangxun/libwx/wx_hw.c index 24c539ce6acbe..c285c1de24ba9 100644 --- a/drivers/net/ethernet/wangxun/libwx/wx_hw.c +++ b/drivers/net/ethernet/wangxun/libwx/wx_hw.c @@ -1596,10 +1596,11 @@ EXPORT_SYMBOL(wx_enable_sec_rx_path); static void wx_vlan_strip_control(struct wx *wx, bool enable) { + int num_rx_queues = READ_ONCE(wx->num_rx_queues); int i, j; rcu_read_lock(); - for (i = 0; i < wx->num_rx_queues; i++) { + for (i = 0; i < num_rx_queues; i++) { struct wx_ring *ring = rcu_dereference(wx->rx_ring[i]); if (!ring) @@ -2777,6 +2778,7 @@ static void wx_disable_rx_drop(struct wx *wx, struct wx_ring *ring) int wx_fc_enable(struct wx *wx, bool tx_pause, bool rx_pause) { + int num_rx_queues = READ_ONCE(wx->num_rx_queues); u16 pause_time = WX_DEFAULT_FCPAUSE; u32 mflcn_reg, fccfg_reg, reg; u32 fcrtl, fcrth; @@ -2849,8 +2851,8 @@ int wx_fc_enable(struct wx *wx, bool tx_pause, bool rx_pause) * and performance reasons. */ rcu_read_lock(); - if (wx->num_rx_queues > 1 && !tx_pause) { - for (i = 0; i < wx->num_rx_queues; i++) { + if (num_rx_queues > 1 && !tx_pause) { + for (i = 0; i < num_rx_queues; i++) { struct wx_ring *rx_ring = rcu_dereference(wx->rx_ring[i]); if (!rx_ring) @@ -2858,7 +2860,7 @@ int wx_fc_enable(struct wx *wx, bool tx_pause, bool rx_pause) wx_enable_rx_drop(wx, rx_ring); } } else { - for (i = 0; i < wx->num_rx_queues; i++) { + for (i = 0; i < num_rx_queues; i++) { struct wx_ring *rx_ring = rcu_dereference(wx->rx_ring[i]); if (!rx_ring) @@ -2875,6 +2877,7 @@ EXPORT_SYMBOL(wx_fc_enable); static void wx_update_xoff_rx_lfc(struct wx *wx) { struct wx_hw_stats *hwstats = &wx->stats; + int num_tx_queues; u64 data; int i; @@ -2893,8 +2896,9 @@ static void wx_update_xoff_rx_lfc(struct wx *wx) if (!data) return; + num_tx_queues = READ_ONCE(wx->num_tx_queues); rcu_read_lock(); - for (i = 0; i < wx->num_tx_queues; i++) { + for (i = 0; i < num_tx_queues; i++) { struct wx_ring *tx_ring = rcu_dereference(wx->tx_ring[i]); if (!tx_ring) @@ -2915,6 +2919,7 @@ void wx_update_stats(struct wx *wx) u64 non_eop_descs = 0, alloc_rx_buff_failed = 0; u64 hw_csum_rx_good = 0, hw_csum_rx_error = 0; u64 restart_queue = 0, tx_busy = 0; + int num_rx_queues, num_tx_queues; u32 i; if (test_bit(WX_STATE_DOWN, wx->state) || @@ -2923,9 +2928,11 @@ void wx_update_stats(struct wx *wx) spin_lock(&wx->hw_stats_lock); + num_rx_queues = READ_ONCE(wx->num_rx_queues); + num_tx_queues = READ_ONCE(wx->num_tx_queues); rcu_read_lock(); /* gather some stats to the wx struct that are per queue */ - for (i = 0; i < wx->num_rx_queues; i++) { + for (i = 0; i < num_rx_queues; i++) { struct wx_ring *rx_ring = rcu_dereference(wx->rx_ring[i]); if (!rx_ring) @@ -2944,7 +2951,7 @@ void wx_update_stats(struct wx *wx) u64 rsc_count = 0; u64 rsc_flush = 0; - for (i = 0; i < wx->num_rx_queues; i++) { + for (i = 0; i < num_rx_queues; i++) { struct wx_ring *rx_ring = rcu_dereference(wx->rx_ring[i]); if (!rx_ring) @@ -2957,7 +2964,7 @@ void wx_update_stats(struct wx *wx) wx->rsc_flush = rsc_flush; } - for (i = 0; i < wx->num_tx_queues; i++) { + for (i = 0; i < num_tx_queues; i++) { struct wx_ring *tx_ring = rcu_dereference(wx->tx_ring[i]); if (!tx_ring) diff --git a/drivers/net/ethernet/wangxun/libwx/wx_lib.c b/drivers/net/ethernet/wangxun/libwx/wx_lib.c index 0fbaa585bb854..e931c57837578 100644 --- a/drivers/net/ethernet/wangxun/libwx/wx_lib.c +++ b/drivers/net/ethernet/wangxun/libwx/wx_lib.c @@ -1734,7 +1734,7 @@ netdev_tx_t wx_xmit_frame(struct sk_buff *skb, return NETDEV_TX_OK; } - num_tx_queues = wx->num_tx_queues; + num_tx_queues = READ_ONCE(wx->num_tx_queues); if (unlikely(!num_tx_queues)) goto drop; @@ -1897,8 +1897,8 @@ static bool wx_set_vmdq_queues(struct wx *wx) wx->num_rx_pools = vmdq_i; wx->num_rx_queues_per_pool = rss_i; - wx->num_rx_queues = vmdq_i * rss_i; - wx->num_tx_queues = vmdq_i * rss_i; + WRITE_ONCE(wx->num_rx_queues, vmdq_i * rss_i); + WRITE_ONCE(wx->num_tx_queues, vmdq_i * rss_i); return true; } @@ -1943,15 +1943,15 @@ static void wx_set_rss_queues(struct wx *wx) } out: - wx->num_rx_queues = f->indices; - wx->num_tx_queues = f->indices; + WRITE_ONCE(wx->num_rx_queues, f->indices); + WRITE_ONCE(wx->num_tx_queues, f->indices); } static void wx_set_num_queues(struct wx *wx) { /* Start with base case */ - wx->num_rx_queues = 1; - wx->num_tx_queues = 1; + WRITE_ONCE(wx->num_rx_queues, 1); + WRITE_ONCE(wx->num_tx_queues, 1); wx->queues_per_pool = 1; if (wx_set_vmdq_queues(wx)) @@ -2288,8 +2288,8 @@ static int wx_alloc_q_vectors(struct wx *wx) return 0; err_out: - wx->num_tx_queues = 0; - wx->num_rx_queues = 0; + WRITE_ONCE(wx->num_tx_queues, 0); + WRITE_ONCE(wx->num_rx_queues, 0); wx->num_q_vectors = 0; while (v_idx--) @@ -2310,8 +2310,8 @@ static void wx_free_q_vectors(struct wx *wx) { int v_idx = wx->num_q_vectors; - wx->num_tx_queues = 0; - wx->num_rx_queues = 0; + WRITE_ONCE(wx->num_tx_queues, 0); + WRITE_ONCE(wx->num_rx_queues, 0); wx->num_q_vectors = 0; while (v_idx--) @@ -3085,13 +3085,16 @@ void wx_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats) { struct wx *wx = netdev_priv(netdev); + int num_rx_queues, num_tx_queues; struct wx_hw_stats *hwstats; int i; wx_update_stats(wx); + num_rx_queues = READ_ONCE(wx->num_rx_queues); + num_tx_queues = READ_ONCE(wx->num_tx_queues); rcu_read_lock(); - for (i = 0; i < wx->num_rx_queues; i++) { + for (i = 0; i < num_rx_queues; i++) { struct wx_ring *ring = rcu_dereference(wx->rx_ring[i]); u64 bytes, packets; unsigned int start; @@ -3107,7 +3110,7 @@ void wx_get_stats64(struct net_device *netdev, } } - for (i = 0; i < wx->num_tx_queues; i++) { + for (i = 0; i < num_tx_queues; i++) { struct wx_ring *ring = rcu_dereference(wx->tx_ring[i]); u64 bytes, packets; unsigned int start; diff --git a/drivers/net/ethernet/wangxun/libwx/wx_ptp.c b/drivers/net/ethernet/wangxun/libwx/wx_ptp.c index 3104a4d24474b..9dec1698c6d33 100644 --- a/drivers/net/ethernet/wangxun/libwx/wx_ptp.c +++ b/drivers/net/ethernet/wangxun/libwx/wx_ptp.c @@ -296,8 +296,8 @@ static void wx_ptp_rx_hang(struct wx *wx) { struct wx_ring *rx_ring; unsigned long rx_event; + int num_rx_queues, n; u32 tsyncrxctl; - int n; tsyncrxctl = rd32(wx, WX_PSR_1588_CTL); @@ -311,8 +311,9 @@ static void wx_ptp_rx_hang(struct wx *wx) /* determine the most recent watchdog or rx_timestamp event */ rx_event = wx->last_rx_ptp_check; + num_rx_queues = READ_ONCE(wx->num_rx_queues); rcu_read_lock(); - for (n = 0; n < wx->num_rx_queues; n++) { + for (n = 0; n < num_rx_queues; n++) { rx_ring = rcu_dereference(wx->rx_ring[n]); if (!rx_ring) continue; diff --git a/drivers/net/ethernet/wangxun/ngbevf/ngbevf_main.c b/drivers/net/ethernet/wangxun/ngbevf/ngbevf_main.c index d79cf8d8484f9..c40b64564d41f 100644 --- a/drivers/net/ethernet/wangxun/ngbevf/ngbevf_main.c +++ b/drivers/net/ethernet/wangxun/ngbevf/ngbevf_main.c @@ -53,8 +53,8 @@ static const struct net_device_ops ngbevf_netdev_ops = { static void ngbevf_set_num_queues(struct wx *wx) { /* Start with base case */ - wx->num_rx_queues = 1; - wx->num_tx_queues = 1; + WRITE_ONCE(wx->num_rx_queues, 1); + WRITE_ONCE(wx->num_tx_queues, 1); } static int ngbevf_sw_init(struct wx *wx) diff --git a/drivers/net/ethernet/wangxun/txgbevf/txgbevf_main.c b/drivers/net/ethernet/wangxun/txgbevf/txgbevf_main.c index 8b16b900820aa..f1784c0ed114d 100644 --- a/drivers/net/ethernet/wangxun/txgbevf/txgbevf_main.c +++ b/drivers/net/ethernet/wangxun/txgbevf/txgbevf_main.c @@ -53,8 +53,8 @@ static void txgbevf_set_num_queues(struct wx *wx) int ret = 0; /* Start with base case */ - wx->num_rx_queues = 1; - wx->num_tx_queues = 1; + WRITE_ONCE(wx->num_rx_queues, 1); + WRITE_ONCE(wx->num_tx_queues, 1); spin_lock_bh(&wx->mbx.mbx_lock); /* fetch queue configuration from the PF */ @@ -66,15 +66,15 @@ static void txgbevf_set_num_queues(struct wx *wx) /* we need as many queues as traffic classes */ if (num_tcs > 1) { - wx->num_rx_queues = num_tcs; + WRITE_ONCE(wx->num_rx_queues, num_tcs); } else { rss = min_t(u16, num_online_cpus(), TXGBEVF_MAX_RSS_NUM); queue = min_t(u16, wx->mac.max_rx_queues, wx->mac.max_tx_queues); rss = min_t(u16, queue, rss); if (wx->vfinfo->vf_api >= wx_mbox_api_13) { - wx->num_rx_queues = rss; - wx->num_tx_queues = rss; + WRITE_ONCE(wx->num_rx_queues, rss); + WRITE_ONCE(wx->num_tx_queues, rss); } } } -- 2.30.1