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 <hkallwe...@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


Reply via email to