All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169
@ 2020-10-29 17:28 Heiner Kallweit
  2020-10-29 17:29 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-29 17:28 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

This series adds missing functionality to the net core handling of
byte/packet counters and statistics. The extensions are then used
to remove private rx/tx byte/packet counters in r8169 driver.

Heiner Kallweit (4):
  net: core: add dev_sw_netstats_tx_add
  net: core: add devm_netdev_alloc_pcpu_stats
  r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters
  r8169: remove no longer needed private rx/tx packet/byte counters

 drivers/net/ethernet/realtek/r8169_main.c | 45 ++++-------------------
 include/linux/netdevice.h                 | 26 +++++++++++++
 2 files changed, 34 insertions(+), 37 deletions(-)

-- 
2.29.1


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

* [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add
  2020-10-29 17:28 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
@ 2020-10-29 17:29 ` Heiner Kallweit
  2020-10-29 17:31 ` [PATCH net-next 2/4] net: core: add devm_netdev_alloc_pcpu_stats Heiner Kallweit
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-29 17:29 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

Add dev_sw_netstats_tx_add(), complementing already existing
dev_sw_netstats_rx_add(). Other than dev_sw_netstats_rx_add allow to
pass the number of packets as function argument.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 include/linux/netdevice.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 964b494b0..568fab708 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2557,6 +2557,18 @@ static inline void dev_sw_netstats_rx_add(struct net_device *dev, unsigned int l
 	u64_stats_update_end(&tstats->syncp);
 }
 
+static inline void dev_sw_netstats_tx_add(struct net_device *dev,
+					  unsigned int packets,
+					  unsigned int len)
+{
+	struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
+
+	u64_stats_update_begin(&tstats->syncp);
+	tstats->tx_bytes += len;
+	tstats->tx_packets += packets;
+	u64_stats_update_end(&tstats->syncp);
+}
+
 static inline void dev_lstats_add(struct net_device *dev, unsigned int len)
 {
 	struct pcpu_lstats *lstats = this_cpu_ptr(dev->lstats);
-- 
2.29.1



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

* [PATCH net-next 2/4] net: core: add devm_netdev_alloc_pcpu_stats
  2020-10-29 17:28 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
  2020-10-29 17:29 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
@ 2020-10-29 17:31 ` Heiner Kallweit
  2020-10-29 17:33 ` [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters Heiner Kallweit
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-29 17:31 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

We have netdev_alloc_pcpu_stats(), and we have devm_alloc_percpu().
Add a managed version of netdev_alloc_pcpu_stats, e.g. for allocating
the per-cpu stats in the probe() callback of a driver. It needs to be
a macro for dealing properly with the type argument.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 include/linux/netdevice.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 568fab708..6e06fef32 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2596,6 +2596,20 @@ static inline void dev_lstats_add(struct net_device *dev, unsigned int len)
 #define netdev_alloc_pcpu_stats(type)					\
 	__netdev_alloc_pcpu_stats(type, GFP_KERNEL)
 
+#define devm_netdev_alloc_pcpu_stats(dev, type)				\
+({									\
+	typeof(type) __percpu *pcpu_stats = devm_alloc_percpu(dev, type);\
+	if (pcpu_stats)	{						\
+		int __cpu;						\
+		for_each_possible_cpu(__cpu) {				\
+			typeof(type) *stat;				\
+			stat = per_cpu_ptr(pcpu_stats, __cpu);		\
+			u64_stats_init(&stat->syncp);			\
+		}							\
+	}								\
+	pcpu_stats;							\
+})
+
 enum netdev_lag_tx_type {
 	NETDEV_LAG_TX_TYPE_UNKNOWN,
 	NETDEV_LAG_TX_TYPE_RANDOM,
-- 
2.29.1



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

* [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters
  2020-10-29 17:28 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
  2020-10-29 17:29 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
  2020-10-29 17:31 ` [PATCH net-next 2/4] net: core: add devm_netdev_alloc_pcpu_stats Heiner Kallweit
@ 2020-10-29 17:33 ` Heiner Kallweit
  2020-10-29 17:34 ` [PATCH net-next 4/4] r8169: remove no longer needed private " Heiner Kallweit
  2020-10-31 18:35 ` [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Jakub Kicinski
  4 siblings, 0 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-29 17:33 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

Switch to the net core rx/tx byte/packet counter infrastructure.
This simplifies the code, only small drawback is some memory overhead
because we use just one queue, but allocate the counters per cpu.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/net/ethernet/realtek/r8169_main.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index 00f13805c..0ef30ad8a 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -4417,6 +4417,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
 	if (tp->dirty_tx != dirty_tx) {
 		netdev_completed_queue(dev, pkts_compl, bytes_compl);
 
+		dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl);
 		rtl_inc_priv_stats(&tp->tx_stats, pkts_compl, bytes_compl);
 
 		tp->dirty_tx = dirty_tx;
@@ -4539,6 +4540,7 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget
 
 		napi_gro_receive(&tp->napi, skb);
 
+		dev_sw_netstats_rx_add(dev, pkt_size);
 		rtl_inc_priv_stats(&tp->rx_stats, 1, pkt_size);
 release_descriptor:
 		rtl8169_mark_to_asic(desc);
@@ -4790,9 +4792,7 @@ rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 	pm_runtime_get_noresume(&pdev->dev);
 
 	netdev_stats_to_stats64(stats, &dev->stats);
-
-	rtl_get_priv_stats(&tp->rx_stats, &stats->rx_packets, &stats->rx_bytes);
-	rtl_get_priv_stats(&tp->tx_stats, &stats->tx_packets, &stats->tx_bytes);
+	dev_fetch_sw_netstats(stats, dev->tstats);
 
 	/*
 	 * Fetch additional counter values missing in stats collected by driver
@@ -5263,6 +5263,11 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	tp->eee_adv = -1;
 	tp->ocp_base = OCP_STD_PHY_BASE;
 
+	dev->tstats = devm_netdev_alloc_pcpu_stats(&pdev->dev,
+						   struct pcpu_sw_netstats);
+	if (!dev->tstats)
+		return -ENOMEM;
+
 	/* Get the *optional* external "ether_clk" used on some boards */
 	rc = rtl_get_ether_clk(tp);
 	if (rc)
-- 
2.29.1



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

* [PATCH net-next 4/4] r8169: remove no longer needed private rx/tx packet/byte counters
  2020-10-29 17:28 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
                   ` (2 preceding siblings ...)
  2020-10-29 17:33 ` [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters Heiner Kallweit
@ 2020-10-29 17:34 ` Heiner Kallweit
  2020-10-31 18:35 ` [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Jakub Kicinski
  4 siblings, 0 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-29 17:34 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

After switching to the net core rx/tx byte/packet counters we can
remove the now unused private version.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/net/ethernet/realtek/r8169_main.c | 34 -----------------------
 1 file changed, 34 deletions(-)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index 0ef30ad8a..b6c11aaa5 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -584,12 +584,6 @@ enum rtl_flag {
 	RTL_FLAG_MAX
 };
 
-struct rtl8169_stats {
-	u64			packets;
-	u64			bytes;
-	struct u64_stats_sync	syncp;
-};
-
 struct rtl8169_private {
 	void __iomem *mmio_addr;	/* memory map physical address */
 	struct pci_dev *pci_dev;
@@ -600,8 +594,6 @@ struct rtl8169_private {
 	u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
 	u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
 	u32 dirty_tx;
-	struct rtl8169_stats rx_stats;
-	struct rtl8169_stats tx_stats;
 	struct TxDesc *TxDescArray;	/* 256-aligned Tx descriptor ring */
 	struct RxDesc *RxDescArray;	/* 256-aligned Rx descriptor ring */
 	dma_addr_t TxPhyAddr;
@@ -700,27 +692,6 @@ static bool rtl_supports_eee(struct rtl8169_private *tp)
 	       tp->mac_version != RTL_GIGA_MAC_VER_39;
 }
 
-static void rtl_get_priv_stats(struct rtl8169_stats *stats,
-			       u64 *pkts, u64 *bytes)
-{
-	unsigned int start;
-
-	do {
-		start = u64_stats_fetch_begin_irq(&stats->syncp);
-		*pkts = stats->packets;
-		*bytes = stats->bytes;
-	} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
-}
-
-static void rtl_inc_priv_stats(struct rtl8169_stats *stats,
-			       u64 pkts, u64 bytes)
-{
-	u64_stats_update_begin(&stats->syncp);
-	stats->packets += pkts;
-	stats->bytes += bytes;
-	u64_stats_update_end(&stats->syncp);
-}
-
 static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg)
 {
 	int i;
@@ -4416,9 +4387,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
 
 	if (tp->dirty_tx != dirty_tx) {
 		netdev_completed_queue(dev, pkts_compl, bytes_compl);
-
 		dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl);
-		rtl_inc_priv_stats(&tp->tx_stats, pkts_compl, bytes_compl);
 
 		tp->dirty_tx = dirty_tx;
 		/* Sync with rtl8169_start_xmit:
@@ -4541,7 +4510,6 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget
 		napi_gro_receive(&tp->napi, skb);
 
 		dev_sw_netstats_rx_add(dev, pkt_size);
-		rtl_inc_priv_stats(&tp->rx_stats, 1, pkt_size);
 release_descriptor:
 		rtl8169_mark_to_asic(desc);
 	}
@@ -5345,8 +5313,6 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	}
 
 	INIT_WORK(&tp->wk.work, rtl_task);
-	u64_stats_init(&tp->rx_stats.syncp);
-	u64_stats_init(&tp->tx_stats.syncp);
 
 	rtl_init_mac_address(tp);
 
-- 
2.29.1



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

* Re: [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169
  2020-10-29 17:28 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
                   ` (3 preceding siblings ...)
  2020-10-29 17:34 ` [PATCH net-next 4/4] r8169: remove no longer needed private " Heiner Kallweit
@ 2020-10-31 18:35 ` Jakub Kicinski
  4 siblings, 0 replies; 7+ messages in thread
From: Jakub Kicinski @ 2020-10-31 18:35 UTC (permalink / raw)
  To: Heiner Kallweit; +Cc: David Miller, Realtek linux nic maintainers, netdev

On Thu, 29 Oct 2020 18:28:52 +0100 Heiner Kallweit wrote:
> This series adds missing functionality to the net core handling of
> byte/packet counters and statistics. The extensions are then used
> to remove private rx/tx byte/packet counters in r8169 driver.

Applied, thanks!

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

* [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters
  2020-10-15 15:47 Heiner Kallweit
@ 2020-10-15 15:50 ` Heiner Kallweit
  0 siblings, 0 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-15 15:50 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

Switch to the net core rx/tx byte/packet counter infrastructure.
This simplifies the code, only small drawback is some memory overhead
because we use just one queue, but allocate the counters per cpu.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/net/ethernet/realtek/r8169_main.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index 7d366b036..840543bc8 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -4417,6 +4417,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
 	if (tp->dirty_tx != dirty_tx) {
 		netdev_completed_queue(dev, pkts_compl, bytes_compl);
 
+		dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl);
 		rtl_inc_priv_stats(&tp->tx_stats, pkts_compl, bytes_compl);
 
 		tp->dirty_tx = dirty_tx;
@@ -4539,6 +4540,7 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget
 
 		napi_gro_receive(&tp->napi, skb);
 
+		dev_sw_netstats_rx_add(dev, pkt_size);
 		rtl_inc_priv_stats(&tp->rx_stats, 1, pkt_size);
 release_descriptor:
 		rtl8169_mark_to_asic(desc);
@@ -4790,9 +4792,7 @@ rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
 	pm_runtime_get_noresume(&pdev->dev);
 
 	netdev_stats_to_stats64(stats, &dev->stats);
-
-	rtl_get_priv_stats(&tp->rx_stats, &stats->rx_packets, &stats->rx_bytes);
-	rtl_get_priv_stats(&tp->tx_stats, &stats->tx_packets, &stats->tx_bytes);
+	dev_fetch_sw_netstats(stats, dev->tstats);
 
 	/*
 	 * Fetch additional counter values missing in stats collected by driver
@@ -5263,6 +5263,11 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	tp->eee_adv = -1;
 	tp->ocp_base = OCP_STD_PHY_BASE;
 
+	dev->tstats = devm_netdev_alloc_pcpu_stats(&pdev->dev,
+						   struct pcpu_sw_netstats);
+	if (!dev->tstats)
+		return -ENOMEM;
+
 	/* Get the *optional* external "ether_clk" used on some boards */
 	rc = rtl_get_ether_clk(tp);
 	if (rc)
-- 
2.28.0



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

end of thread, other threads:[~2020-10-31 18:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-29 17:28 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
2020-10-29 17:29 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
2020-10-29 17:31 ` [PATCH net-next 2/4] net: core: add devm_netdev_alloc_pcpu_stats Heiner Kallweit
2020-10-29 17:33 ` [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters Heiner Kallweit
2020-10-29 17:34 ` [PATCH net-next 4/4] r8169: remove no longer needed private " Heiner Kallweit
2020-10-31 18:35 ` [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Jakub Kicinski
  -- strict thread matches above, loose matches on Subject: below --
2020-10-15 15:47 Heiner Kallweit
2020-10-15 15:50 ` [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters Heiner Kallweit

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.