diff mbox series

[net-next,2/4] net: core: add devm_netdev_alloc_pcpu_stats

Message ID 1c618e2d-049c-7dfa-0f82-5f32fa4e9322@gmail.com
State New
Headers show
Series net: add functionality to net core byte/packet counters and use it in r8169 | expand

Commit Message

Heiner Kallweit Oct. 15, 2020, 3:49 p.m. UTC
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 mbox series

Patch

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