netdev.vger.kernel.org archive mirror
 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-15 15:47 Heiner Kallweit
  2020-10-15 15:48 ` [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-15 15:47 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 used then
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                 | 27 ++++++++++++++
 net/devres.c                              |  6 +++
 3 files changed, 41 insertions(+), 37 deletions(-)

-- 
2.28.0


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

* [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add
  2020-10-15 15:47 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
@ 2020-10-15 15:48 ` Heiner Kallweit
  2020-10-15 15:49 ` [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-15 15:48 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.28.0



^ 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-15 15:47 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
  2020-10-15 15:48 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
@ 2020-10-15 15:49 ` 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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Heiner Kallweit @ 2020-10-15 15:49 UTC (permalink / raw)
  To: David Miller, Jakub Kicinski, Realtek linux nic maintainers; +Cc: netdev

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 | 15 +++++++++++++++
 net/devres.c              |  6 ++++++
 2 files changed, 21 insertions(+)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 568fab708..f5f41c160 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2596,6 +2596,21 @@ 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)
 
+void devm_free_pcpu_stats(void *data);
+
+#define devm_netdev_alloc_pcpu_stats(dev, type)				\
+({									\
+	typeof(type) __percpu *pcpu_stats = netdev_alloc_pcpu_stats(type); \
+	if (pcpu_stats) {						\
+		int rc = devm_add_action_or_reset(dev,			\
+					devm_free_pcpu_stats,		\
+					(__force void *)pcpu_stats);	\
+		if (rc)							\
+			pcpu_stats = NULL;				\
+	}								\
+	pcpu_stats;							\
+})
+
 enum netdev_lag_tx_type {
 	NETDEV_LAG_TX_TYPE_UNKNOWN,
 	NETDEV_LAG_TX_TYPE_RANDOM,
diff --git a/net/devres.c b/net/devres.c
index 1f9be2133..0d6545946 100644
--- a/net/devres.c
+++ b/net/devres.c
@@ -93,3 +93,9 @@ int devm_register_netdev(struct device *dev, struct net_device *ndev)
 	return 0;
 }
 EXPORT_SYMBOL(devm_register_netdev);
+
+void devm_free_pcpu_stats(void *data)
+{
+	free_percpu((__force void __percpu *)data);
+}
+EXPORT_SYMBOL_GPL(devm_free_pcpu_stats);
-- 
2.28.0



^ 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-15 15:47 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
  2020-10-15 15:48 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
  2020-10-15 15:49 ` [PATCH net-next 2/4] net: core: add devm_netdev_alloc_pcpu_stats Heiner Kallweit
@ 2020-10-15 15:50 ` Heiner Kallweit
  2020-10-15 15:51 ` [PATCH net-next 4/4] r8169: remove no longer needed private " Heiner Kallweit
  2020-10-15 19:56 ` [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-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

* [PATCH net-next 4/4] r8169: remove no longer needed private rx/tx packet/byte counters
  2020-10-15 15:47 [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-15 15:50 ` [PATCH net-next 3/4] r8169: use struct pcpu_sw_netstats for rx/tx packet/byte counters Heiner Kallweit
@ 2020-10-15 15:51 ` Heiner Kallweit
  2020-10-15 19:56 ` [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-15 15:51 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 840543bc8..345cd5f00 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.28.0



^ 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-15 15:47 [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-15 15:51 ` [PATCH net-next 4/4] r8169: remove no longer needed private " Heiner Kallweit
@ 2020-10-15 19:56 ` Jakub Kicinski
  4 siblings, 0 replies; 7+ messages in thread
From: Jakub Kicinski @ 2020-10-15 19:56 UTC (permalink / raw)
  To: Heiner Kallweit; +Cc: David Miller, Realtek linux nic maintainers, netdev

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

As much as I was hoping to apply this, it looks like it's still not
through my build testing :( 

So let's play it safe and come back to it in a week and a half.

^ 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 Heiner Kallweit
@ 2020-10-29 17:29 ` Heiner Kallweit
  0 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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-15 15:47 [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Heiner Kallweit
2020-10-15 15:48 ` [PATCH net-next 1/4] net: core: add dev_sw_netstats_tx_add Heiner Kallweit
2020-10-15 15:49 ` [PATCH net-next 2/4] net: core: add devm_netdev_alloc_pcpu_stats 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
2020-10-15 15:51 ` [PATCH net-next 4/4] r8169: remove no longer needed private " Heiner Kallweit
2020-10-15 19:56 ` [PATCH net-next 0/4] net: add functionality to net core byte/packet counters and use it in r8169 Jakub Kicinski
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

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).