Skip to content

Commit cb45a8b

Browse files
robhancocksedkuba-moo
authored andcommitted
net: axienet: Switch to 64-bit RX/TX statistics
The RX and TX byte/packet statistics in this driver could be overflowed relatively quickly on a 32-bit platform. Switch these stats to use the u64_stats infrastructure to avoid this. Signed-off-by: Robert Hancock <robert.hancock@calian.com> Link: https://lore.kernel.org/r/20220829233901.3429419-1-robert.hancock@calian.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent a60511c commit cb45a8b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

drivers/net/ethernet/xilinx/xilinx_axienet.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ struct axidma_bd {
402402
* @rx_bd_num: Size of RX buffer descriptor ring
403403
* @rx_bd_ci: Stores the index of the Rx buffer descriptor in the ring being
404404
* accessed currently.
405+
* @rx_packets: RX packet count for statistics
406+
* @rx_bytes: RX byte count for statistics
407+
* @rx_stat_sync: Synchronization object for RX stats
405408
* @napi_tx: NAPI TX control structure
406409
* @tx_dma_cr: Nominal content of TX DMA control register
407410
* @tx_bd_v: Virtual address of the TX buffer descriptor ring
@@ -411,6 +414,9 @@ struct axidma_bd {
411414
* complete. Only updated at runtime by TX NAPI poll.
412415
* @tx_bd_tail: Stores the index of the next Tx buffer descriptor in the ring
413416
* to be populated.
417+
* @tx_packets: TX packet count for statistics
418+
* @tx_bytes: TX byte count for statistics
419+
* @tx_stat_sync: Synchronization object for TX stats
414420
* @dma_err_task: Work structure to process Axi DMA errors
415421
* @tx_irq: Axidma TX IRQ number
416422
* @rx_irq: Axidma RX IRQ number
@@ -458,6 +464,9 @@ struct axienet_local {
458464
dma_addr_t rx_bd_p;
459465
u32 rx_bd_num;
460466
u32 rx_bd_ci;
467+
u64_stats_t rx_packets;
468+
u64_stats_t rx_bytes;
469+
struct u64_stats_sync rx_stat_sync;
461470

462471
struct napi_struct napi_tx;
463472
u32 tx_dma_cr;
@@ -466,6 +475,9 @@ struct axienet_local {
466475
u32 tx_bd_num;
467476
u32 tx_bd_ci;
468477
u32 tx_bd_tail;
478+
u64_stats_t tx_packets;
479+
u64_stats_t tx_bytes;
480+
struct u64_stats_sync tx_stat_sync;
469481

470482
struct work_struct dma_err_task;
471483

drivers/net/ethernet/xilinx/xilinx_axienet_main.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,10 @@ static int axienet_tx_poll(struct napi_struct *napi, int budget)
752752
if (lp->tx_bd_ci >= lp->tx_bd_num)
753753
lp->tx_bd_ci %= lp->tx_bd_num;
754754

755-
ndev->stats.tx_packets += packets;
756-
ndev->stats.tx_bytes += size;
755+
u64_stats_update_begin(&lp->tx_stat_sync);
756+
u64_stats_add(&lp->tx_packets, packets);
757+
u64_stats_add(&lp->tx_bytes, size);
758+
u64_stats_update_end(&lp->tx_stat_sync);
757759

758760
/* Matches barrier in axienet_start_xmit */
759761
smp_mb();
@@ -984,8 +986,10 @@ static int axienet_rx_poll(struct napi_struct *napi, int budget)
984986
cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
985987
}
986988

987-
lp->ndev->stats.rx_packets += packets;
988-
lp->ndev->stats.rx_bytes += size;
989+
u64_stats_update_begin(&lp->rx_stat_sync);
990+
u64_stats_add(&lp->rx_packets, packets);
991+
u64_stats_add(&lp->rx_bytes, size);
992+
u64_stats_update_end(&lp->rx_stat_sync);
989993

990994
if (tail_p)
991995
axienet_dma_out_addr(lp, XAXIDMA_RX_TDESC_OFFSET, tail_p);
@@ -1292,10 +1296,32 @@ static int axienet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
12921296
return phylink_mii_ioctl(lp->phylink, rq, cmd);
12931297
}
12941298

1299+
static void
1300+
axienet_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1301+
{
1302+
struct axienet_local *lp = netdev_priv(dev);
1303+
unsigned int start;
1304+
1305+
netdev_stats_to_stats64(stats, &dev->stats);
1306+
1307+
do {
1308+
start = u64_stats_fetch_begin_irq(&lp->rx_stat_sync);
1309+
stats->rx_packets = u64_stats_read(&lp->rx_packets);
1310+
stats->rx_bytes = u64_stats_read(&lp->rx_bytes);
1311+
} while (u64_stats_fetch_retry_irq(&lp->rx_stat_sync, start));
1312+
1313+
do {
1314+
start = u64_stats_fetch_begin_irq(&lp->tx_stat_sync);
1315+
stats->tx_packets = u64_stats_read(&lp->tx_packets);
1316+
stats->tx_bytes = u64_stats_read(&lp->tx_bytes);
1317+
} while (u64_stats_fetch_retry_irq(&lp->tx_stat_sync, start));
1318+
}
1319+
12951320
static const struct net_device_ops axienet_netdev_ops = {
12961321
.ndo_open = axienet_open,
12971322
.ndo_stop = axienet_stop,
12981323
.ndo_start_xmit = axienet_start_xmit,
1324+
.ndo_get_stats64 = axienet_get_stats64,
12991325
.ndo_change_mtu = axienet_change_mtu,
13001326
.ndo_set_mac_address = netdev_set_mac_address,
13011327
.ndo_validate_addr = eth_validate_addr,
@@ -1850,6 +1876,9 @@ static int axienet_probe(struct platform_device *pdev)
18501876
lp->rx_bd_num = RX_BD_NUM_DEFAULT;
18511877
lp->tx_bd_num = TX_BD_NUM_DEFAULT;
18521878

1879+
u64_stats_init(&lp->rx_stat_sync);
1880+
u64_stats_init(&lp->tx_stat_sync);
1881+
18531882
netif_napi_add(ndev, &lp->napi_rx, axienet_rx_poll, NAPI_POLL_WEIGHT);
18541883
netif_napi_add(ndev, &lp->napi_tx, axienet_tx_poll, NAPI_POLL_WEIGHT);
18551884

0 commit comments

Comments
 (0)