Skip to contentSkip to conversation

net/core: Export dev_core_stats_rx_dropped_inc sets

PATCH Superseded Yajun Deng 4 participants 16 messages Sep 11, 2023 – Sep 14, 2023 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yajun Deng

author
#0 lore raw

PATCH 1/1 net/core: Export dev_core_stats_rx_dropped_inc sets

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

For the users, people are more concerned about why the dropped in ifconfig is increasing. So we can export dev_core_stats_rx_dropped_inc sets, which users would trace them know why rx_dropped is increasing.

Export dev_core_stats_{rx_dropped, tx_dropped, rx_nohandler, rx_otherhost_dropped}_inc for trace. Also, move dev_core_stats() and netdev_core_stats_alloc() in dev.c, because they are not called externally.

---
 include/linux/netdevice.h | 32 +++++---------------------------
 net/core/dev.c            | 30 ++++++++++++++++++++++++++++--
 2 files changed, 33 insertions(+), 29 deletions(-)
include/linux/netdevice.h +5 -27
index 0896aaa91dd7..879b01c85ba4 100644
@@ -3954,6 +3954,11 @@ int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb);
 bool is_skb_forwardable(const struct net_device *dev,
 			const struct sk_buff *skb);
 
+void dev_core_stats_rx_dropped_inc(struct net_device *dev);
+void dev_core_stats_tx_dropped_inc(struct net_device *dev);
+void dev_core_stats_rx_nohandler_inc(struct net_device *dev);
+void dev_core_stats_rx_otherhost_dropped_inc(struct net_device *dev);
+
 static __always_inline bool __is_skb_forwardable(const struct net_device *dev,
 						 const struct sk_buff *skb,
 						 const bool check_mtu)
@@ -3980,33 +3985,6 @@ static __always_inline bool __is_skb_forwardable(const struct net_device *dev,
 	return false;
 }
 
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev);
-
-static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)
-{
-	/* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
-	struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
-
-	if (likely(p))
-		return p;
-
-	return netdev_core_stats_alloc(dev);
-}
-
-#define DEV_CORE_STATS_INC(FIELD)						\
-static inline void dev_core_stats_##FIELD##_inc(struct net_device *dev)		\
-{										\
-	struct net_device_core_stats __percpu *p;				\
-										\
-	p = dev_core_stats(dev);						\
-	if (p)									\
-		this_cpu_inc(p->FIELD);						\
-}
-DEV_CORE_STATS_INC(rx_dropped)
-DEV_CORE_STATS_INC(tx_dropped)
-DEV_CORE_STATS_INC(rx_nohandler)
-DEV_CORE_STATS_INC(rx_otherhost_dropped)
-
 static __always_inline int ____dev_forward_skb(struct net_device *dev,
 					       struct sk_buff *skb,
 					       const bool check_mtu)
net/core/dev.c +28 -2
index ccff2b6ef958..32ba730405b4 100644
@@ -10475,7 +10475,7 @@ void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
 }
 EXPORT_SYMBOL(netdev_stats_to_stats64);
 
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
 	struct net_device_core_stats __percpu *p;
 
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
 	/* This READ_ONCE() pairs with the cmpxchg() above */
 	return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)
+{
+	/* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+	struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+	if (likely(p))
+		return p;
+
+	return netdev_core_stats_alloc(dev);
+}
+
+#define DEV_CORE_STATS_INC(FIELD)				\
+void dev_core_stats_##FIELD##_inc(struct net_device *dev)	\
+{								\
+	struct net_device_core_stats __percpu *p;		\
+								\
+	p = dev_core_stats(dev);				\
+	if (p)							\
+		this_cpu_inc(p->FIELD);				\
+}								\
+EXPORT_SYMBOL(dev_core_stats_##FIELD##_inc)
+
+DEV_CORE_STATS_INC(rx_dropped);
+DEV_CORE_STATS_INC(tx_dropped);
+DEV_CORE_STATS_INC(rx_nohandler);
+DEV_CORE_STATS_INC(rx_otherhost_dropped);
 
 /**
  *	dev_get_stats	- get network device statistics
Signature · 1 line
2.25.1
#1 lore raw
Quoted 9 lines · On Mon, 11 Sep 2023 16:20:16 +0800 Yajun Deng <[email protected]> wrote:

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

For the users, people are more concerned about why the dropped in ifconfig is increasing. So we can export dev_core_stats_rx_dropped_inc sets, which users would trace them know why rx_dropped is increasing.

ifconfig has been frozen for over 10 years, and is deprecated so there is no point in catering to legacy api's. There are better API's such as ethtool and netlink that can provide more info.

author
#2 lore raw
Quoted 16 lines · Stephen Hemminger
Quoted 11 lines · On Mon, 11 Sep 2023 16:20:16 +0800 Yajun Deng <[email protected]> wrote:

Although there is a kfree_skb_reason() helper function that can be
used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

For the users, people are more concerned about why the dropped in ifconfig is increasing. So we can export dev_core_stats_rx_dropped_inc sets, which users would trace them know why rx_dropped is increasing.

Quoted 4 lines

ifconfig has been frozen for over 10 years, and is deprecated so there is no point in catering to legacy api's. There are better API's such as ethtool and netlink that can provide more info.

Yes, ifconfig is deprecated. but the dropped in ifconfig and ip is the same. We're more concerned about the reason for dropped packets.
ip, ethtool and netlink couldn't show the reason.

#3 lore raw
In reply to Yajun Deng
Quoted 9 lines · Yajun Deng

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

For the users, people are more concerned about why the dropped in ifconfig is increasing. So we can export dev_core_stats_rx_dropped_inc sets, which users would trace them know why rx_dropped is increasing.

Quoted 6 lines

Export dev_core_stats_{rx_dropped, tx_dropped, rx_nohandler, rx_otherhost_dropped}_inc for trace. Also, move dev_core_stats() and netdev_core_stats_alloc() in dev.c, because they are not called externally.

Okay, but it seems you forgot to say which tree was targeted by this patch.

Documentation/process/maintainer-netdev.rst

I would guess net-next, but patch authors are supposed to be explicit.

Quoted 69 lines
---
 include/linux/netdevice.h | 32 +++++---------------------------
 net/core/dev.c            | 30 ++++++++++++++++++++++++++++--
 2 files changed, 33 insertions(+), 29 deletions(-)
include/linux/netdevice.h +5 -27
index 0896aaa91dd7..879b01c85ba4 100644
@@ -3954,6 +3954,11 @@ int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb);
 bool is_skb_forwardable(const struct net_device *dev,
                        const struct sk_buff *skb);
 
+void dev_core_stats_rx_dropped_inc(struct net_device *dev);
+void dev_core_stats_tx_dropped_inc(struct net_device *dev);
+void dev_core_stats_rx_nohandler_inc(struct net_device *dev);
+void dev_core_stats_rx_otherhost_dropped_inc(struct net_device *dev);
+
 static __always_inline bool __is_skb_forwardable(const struct net_device *dev,
                                                 const struct sk_buff *skb,
                                                 const bool check_mtu)
@@ -3980,33 +3985,6 @@ static __always_inline bool __is_skb_forwardable(const struct net_device *dev,
        return false;
 }
 
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev);
-
-static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)
-{
-       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
-       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
-
-       if (likely(p))
-               return p;
-
-       return netdev_core_stats_alloc(dev);
-}
-
-#define DEV_CORE_STATS_INC(FIELD)                                              \
-static inline void dev_core_stats_##FIELD##_inc(struct net_device *dev)                \
-{                                                                              \
-       struct net_device_core_stats __percpu *p;                               \
-                                                                               \
-       p = dev_core_stats(dev);                                                \
-       if (p)                                                                  \
-               this_cpu_inc(p->FIELD);                                         \
-}
-DEV_CORE_STATS_INC(rx_dropped)
-DEV_CORE_STATS_INC(tx_dropped)
-DEV_CORE_STATS_INC(rx_nohandler)
-DEV_CORE_STATS_INC(rx_otherhost_dropped)
-
 static __always_inline int ____dev_forward_skb(struct net_device *dev,
                                               struct sk_buff *skb,
                                               const bool check_mtu)
net/core/dev.c +1 -1
index ccff2b6ef958..32ba730405b4 100644
@@ -10475,7 +10475,7 @@ void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
 }
 EXPORT_SYMBOL(netdev_stats_to_stats64);
 
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
 

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 6 lines
net/core/dev.c +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

Quoted 15 lines
net/core/dev.c +15 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}
+
+#define DEV_CORE_STATS_INC(FIELD)                              \
+void dev_core_stats_##FIELD##_inc(struct net_device *dev)      \
+{                                                              \
+       struct net_device_core_stats __percpu *p;               \
+                                                               \

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10517

Quoted 10 lines
net/core/dev.c +10 -0
+       p = dev_core_stats(dev);                                \
+       if (p)                                                  \
+               this_cpu_inc(p->FIELD);                         \
+}                                                              \
+EXPORT_SYMBOL(dev_core_stats_##FIELD##_inc)
+
+DEV_CORE_STATS_INC(rx_dropped);
+DEV_CORE_STATS_INC(tx_dropped);
+DEV_CORE_STATS_INC(rx_nohandler);
+DEV_CORE_STATS_INC(rx_otherhost_dropped);

#undef DEV_CORE_STATS_INC

Quoted 6 lines
net/core/dev.c +0 -1
 /**
  *     dev_get_stats   - get network device statistics
--

2.25.1

#4 lore raw

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 06:23:24 +0200

Quoted 5 lines · Yajun Deng
Quoted 4 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 7 lines
Quoted 7 lines

EXPORT_SYMBOL(netdev_stats_to_stats64);

net/core/dev.c +1 -1
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
Quoted 9 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 7 lines
net/core/dev.c +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

__cold? O_o I thought the author's inlining it as it's a couple locs/intstructions, while the compilers would most likely keep it non-inlined as it's referenced 4 times. __cold will for sure keep it standalone and place it in .text.cold, i.e. far away from the call sites. I realize dev_core_stats_*() aren't called frequently, but why making only one small helper cold rather than all of them then?

Quoted 10 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10501

Quoted 10 lines
net/core/dev.c +9 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}

[...]

Thanks,
Olek

#5 lore raw
Quoted 29 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 06:23:24 +0200

Quoted 5 lines · Yajun Deng
Quoted 4 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 7 lines
Quoted 7 lines

EXPORT_SYMBOL(netdev_stats_to_stats64);

net/core/dev.c +1 -1
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
Quoted 9 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 7 lines
net/core/dev.c +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

Quoted 7 lines

__cold? O_o I thought the author's inlining it as it's a couple locs/intstructions, while the compilers would most likely keep it non-inlined as it's referenced 4 times. __cold will for sure keep it standalone and place it in .text.cold, i.e. far away from the call sites. I realize dev_core_stats_*() aren't called frequently, but why making only one small helper cold rather than all of them then?

This helper is used at least one time per netdevice lifetime. This is definitely cold.
Forcing an inline makes no sense, this would duplicate the code four times, for absolutely no gain.

Quoted 15 lines
Quoted 10 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10501

Quoted 10 lines
net/core/dev.c +9 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}

[...]

Thanks,
Olek

#6 lore raw

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 18:04:44 +0200

Quoted 39 lines
Quoted 29 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 06:23:24 +0200

Quoted 5 lines · Yajun Deng
Quoted 4 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 7 lines
Quoted 7 lines

EXPORT_SYMBOL(netdev_stats_to_stats64);

net/core/dev.c +1 -1
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
Quoted 9 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 7 lines
net/core/dev.c +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

Quoted 7 lines

__cold? O_o I thought the author's inlining it as it's a couple locs/intstructions, while the compilers would most likely keep it non-inlined as it's referenced 4 times. __cold will for sure keep it standalone and place it in .text.cold, i.e. far away from the call sites. I realize dev_core_stats_*() aren't called frequently, but why making only one small helper cold rather than all of them then?

Quoted 2 lines

This helper is used at least one time per netdevice lifetime. This is definitely cold.

But then each dev_stats_*_inc() (not cold) has to call it from a completely different piece of .text far from their. I either don't understand the idea or dunno. Why not make them cold as well then?

Quoted 2 lines

Forcing an inline makes no sense, this would duplicate the code four times, for absolutely no gain.

I'd love to see bloat-o-meter numbers, I suspect we're talking about 20-30 bytes.

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines
Quoted 11 lines
net/core/dev.c +9 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}

[...]

Quoted 2 lines
Quoted 2 lines

Thanks,
Olek

Thanks,
Olek

#7 lore raw
Quoted 43 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 18:04:44 +0200

Quoted 39 lines
Quoted 36 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 06:23:24 +0200

Quoted 5 lines · Yajun Deng
Quoted 4 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 7 lines
Quoted 7 lines

EXPORT_SYMBOL(netdev_stats_to_stats64);

net/core/dev.c +1 -1
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
Quoted 9 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 7 lines
net/core/dev.c +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

__cold? O_o I thought the author's inlining it as it's a couple locs/intstructions, while the compilers would most likely keep it non-inlined as it's referenced 4 times. __cold will for sure keep it standalone and place it in .text.cold, i.e. far away from the call sites. I realize dev_core_stats_*() aren't called frequently, but why making only one small helper cold rather than all of them then?

Quoted 7 lines
Quoted 2 lines

This helper is used at least one time per netdevice lifetime. This is definitely cold.

But then each dev_stats_*_inc() (not cold) has to call it from a completely different piece of .text far from their. I either don't understand the idea or dunno. Why not make them cold as well then?

The __cold attribute is only applied to the helper _allocating_ the memory, once.

Not on the functions actually incrementing the stats.

There are situations where they can be called thousands/millions of times per second (incast flood).
If this situation happens, the _allocation_ still happens once.

Quoted 25 lines
Quoted 2 lines

Forcing an inline makes no sense, this would duplicate the code four times, for absolutely no gain.

I'd love to see bloat-o-meter numbers, I suspect we're talking about 20-30 bytes.

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines
Quoted 11 lines
net/core/dev.c +9 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}

[...]

Quoted 2 lines
Quoted 2 lines

Thanks,
Olek

Thanks,
Olek

#8 lore raw

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 19:28:50 +0200

Quoted 48 lines
Quoted 46 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 18:04:44 +0200

Quoted 41 lines
Quoted 29 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 06:23:24 +0200

Quoted 5 lines · Yajun Deng
Quoted 4 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 7 lines
Quoted 7 lines

EXPORT_SYMBOL(netdev_stats_to_stats64);

net/core/dev.c +1 -1
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
Quoted 9 lines

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 7 lines
net/core/dev.c +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

Quoted 7 lines

__cold? O_o I thought the author's inlining it as it's a couple locs/intstructions, while the compilers would most likely keep it non-inlined as it's referenced 4 times. __cold will for sure keep it standalone and place it in .text.cold, i.e. far away from the call sites. I realize dev_core_stats_*() aren't called frequently, but why making only one small helper cold rather than all of them then?

This helper is used at least one time per netdevice lifetime. This is definitely cold.

Quoted 7 lines
Quoted 4 lines

But then each dev_stats_*_inc() (not cold) has to call it from a completely different piece of .text far from their. I either don't understand the idea or dunno. Why not make them cold as well then?

The __cold attribute is only applied to the helper _allocating_ the memory, once.

Then it should be applied to netdev_core_stats_alloc(), not dev_core_stats(). The latter only dereferences the already existing pointer or calls the former, which actually does the allocation. That's why I don't get why make one if/else non-inline or even cold.

Quoted 6 lines

Not on the functions actually incrementing the stats.

There are situations where they can be called thousands/millions of times per second (incast flood).
If this situation happens, the _allocation_ still happens once.

Correct, but dev_core_stats() will be called the same millions of times per second, see above. It's called unconditionally each increment.

So seems like I got the idea of .cold correctly, but you were referring to the wrong function.

Quoted 23 lines
Quoted 23 lines
Quoted 5 lines

Forcing an inline makes no sense, this would duplicate the code four times, for absolutely no gain.

I'd love to see bloat-o-meter numbers, I suspect we're talking about 20-30 bytes.

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines
Quoted 11 lines
net/core/dev.c +9 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}

[...]

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Thanks,
Olek

Thanks,
Olek

Thanks,
Olek

#9 lore raw
Quoted 57 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 19:28:50 +0200

Quoted 53 lines
Quoted 50 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 18:04:44 +0200

Quoted 39 lines
Quoted 29 lines · Alexander Lobakin

From: Eric Dumazet <[email protected]>
Date: Tue, 12 Sep 2023 06:23:24 +0200

Quoted 5 lines · Yajun Deng
Quoted 4 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 7 lines
Quoted 7 lines

EXPORT_SYMBOL(netdev_stats_to_stats64);

(quoted diff) +1 -1
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
        struct net_device_core_stats __percpu *p;
Quoted 9 lines

Review struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10492

Quoted 7 lines
(quoted diff) +2 -1
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
        /* This READ_ONCE() pairs with the cmpxchg() above */
        return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

Please remove this inline attritbute. Consider using __cold instead.

Quoted 7 lines

__cold? O_o I thought the author's inlining it as it's a couple locs/intstructions, while the compilers would most likely keep it non-inlined as it's referenced 4 times. __cold will for sure keep it standalone and place it in .text.cold, i.e. far away from the call sites. I realize dev_core_stats_*() aren't called frequently, but why making only one small helper cold rather than all of them then?

Quoted 2 lines

This helper is used at least one time per netdevice lifetime. This is definitely cold.

But then each dev_stats_*_inc() (not cold) has to call it from a completely different piece of .text far from their. I either don't understand the idea or dunno. Why not make them cold as well then?

Quoted 7 lines
Quoted 2 lines

The __cold attribute is only applied to the helper _allocating_ the memory, once.

Then it should be applied to netdev_core_stats_alloc(), not dev_core_stats(). The latter only dereferences the already existing pointer or calls the former, which actually does the allocation. That's why I don't get why make one if/else non-inline or even cold.

Sure, this was what was suggested (perhaps not _very_ precisely, but the general idea was pretty clear).
v2 seems ok, right ?

It seems we are all on the same page.

+static __cold struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)
+{
+ /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+ struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+ if (likely(p))
+ return p;
+
+ return netdev_core_stats_alloc(dev);
+}
+

+#define DEV_CORE_STATS_INC(FIELD)                              \
+void dev_core_stats_##FIELD##_inc(struct net_device *dev)      \
+{                                                              \
+       struct net_device_core_stats __percpu *p;               \
+                                                               \
+       p = dev_core_stats(dev);                                \
+       if (p)                                                  \
+               this_cpu_inc(p->FIELD);                         \
+}                                                              \

+EXPORT_SYMBOL(dev_core_stats_##FIELD##_inc)

#10 lore raw
Quoted 18 lines · Eric Dumazet

Sure, this was what was suggested (perhaps not _very_ precisely, but the general idea was pretty clear).
v2 seems ok, right ?

It seems we are all on the same page.

+static __cold struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

(quoted diff) +10 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+#define DEV_CORE_STATS_INC(FIELD)                              \
+void dev_core_stats_##FIELD##_inc(struct net_device *dev)      \
+{                                                              \
+       struct net_device_core_stats __percpu *p;               \
+                                                               \
+       p = dev_core_stats(dev);                                \
+       if (p)                                                  \
+               this_cpu_inc(p->FIELD);                         \
+}                                                              \
+EXPORT_SYMBOL(dev_core_stats_##FIELD##_inc)

Oh well, I just read the patch, and it seems wrong indeed.

netdev_core_stats_alloc() is the one that can be cold.

author
#11 lore raw
Quoted 32 lines · Eric Dumazet
Quoted 18 lines · Eric Dumazet

Sure, this was what was suggested (perhaps not _very_ precisely, but the general idea was pretty clear).
v2 seems ok, right ?

It seems we are all on the same page.

+static __cold struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)

(quoted diff) +10 -0
+{
+       /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+       struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+       if (likely(p))
+               return p;
+
+       return netdev_core_stats_alloc(dev);
+}
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+#define DEV_CORE_STATS_INC(FIELD)                              \
+void dev_core_stats_##FIELD##_inc(struct net_device *dev)      \
+{                                                              \
+       struct net_device_core_stats __percpu *p;               \
+                                                               \
+       p = dev_core_stats(dev);                                \
+       if (p)                                                  \
+               this_cpu_inc(p->FIELD);                         \
+}                                                              \
+EXPORT_SYMBOL(dev_core_stats_##FIELD##_inc)

Oh well, I just read the patch, and it seems wrong indeed.

Quoted 1 line

netdev_core_stats_alloc() is the one that can be cold.

Okay, I would add __cold to netdev_core_stats_alloc() in v3.

Olek suggest that define a new dev_core_stats_inc() function.

I hope to see the suggestion in another reply.

#12 lore raw
In reply to Yajun Deng

From: Yajun Deng <[email protected]>
Date: Mon, 11 Sep 2023 16:20:16 +0800

Quoted 3 lines

Although there is a kfree_skb_reason() helper function that can be used to find the reason for dropped packets, but most callers didn't increase one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped.

[...]

Quoted 35 lines
net/core/dev.c +18 -2
index ccff2b6ef958..32ba730405b4 100644
@@ -10475,7 +10475,7 @@ void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
 }
 EXPORT_SYMBOL(netdev_stats_to_stats64);
 
-struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
+static struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device *dev)
 {
 	struct net_device_core_stats __percpu *p;
 
@@ -10488,7 +10488,33 @@ struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device
 	/* This READ_ONCE() pairs with the cmpxchg() above */
 	return READ_ONCE(dev->core_stats);
 }
-EXPORT_SYMBOL(netdev_core_stats_alloc);
+
+static inline struct net_device_core_stats __percpu *dev_core_stats(struct net_device *dev)
+{
+	/* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
+	struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
+
+	if (likely(p))
+		return p;
+
+	return netdev_core_stats_alloc(dev);
+}
+
+#define DEV_CORE_STATS_INC(FIELD)				\
+void dev_core_stats_##FIELD##_inc(struct net_device *dev)	\
+{								\
+	struct net_device_core_stats __percpu *p;		\
+								\

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10517

Quoted 10 lines
net/core/dev.c +10 -0
+	p = dev_core_stats(dev);				\
+	if (p)							\
+		this_cpu_inc(p->FIELD);				\
+}								\
+EXPORT_SYMBOL(dev_core_stats_##FIELD##_inc)
+
+DEV_CORE_STATS_INC(rx_dropped);
+DEV_CORE_STATS_INC(tx_dropped);
+DEV_CORE_STATS_INC(rx_nohandler);
+DEV_CORE_STATS_INC(rx_otherhost_dropped);

I realize you need to have an external function to be able to trace it, but why don't you make it just 1 function instead of 4+ (will only be increasing)?

Define 1 function

void dev_core_stats_inc(struct net_device *dev, u32 offset)

{
	struct net_device_core_stats __percpu *p;

	p = dev_core_stats(dev);
	if (p)
		this_cpu_inc(*(unsigned long *)(void *)p + offset);
}

EXPORT_SYMBOL_GPL(dev_core_stats_inc); // Why not GPL BTW?

And then build inlines:

#define DEV_CORE_STATS_INC(FIELD) \
static inline void \
dev_core_stats_##FIELD##_inc(struct net_device *dev) \

{								\
	dev_core_stats_inc(dev,					\
		offsetof(struct net_device_core_stats, FIELD));	\
}

DEV_CORE_STATS_INC(rx_dropped);
...

OR even just make them macros

#define __DEV_CORE_STATS_INC(dev, field)			\
	dev_core_stats_inc(dev,					\
		offsetof(struct net_device_core_stats, field))

#define dev_core_stats_rx_dropped_inc(dev) \
__DEV_CORE_STATS_INC(dev, rx_dropped)
...

Just don't copy that awful Thunderbird's line wrap and don't assume this code builds and works and that is something finished/polished.

You'll be able to trace functions and you'll be able to understand which counter has been incremented by checking the second argument, i.e. the field offset (IIRC tracing shows you arguments).
And that way you wouldn't geometrically increase the number of symbol exports and deal with its consequences.

net/core/dev.c struct net_device_core_stats __percpu *netdev_core_stats_alloc(struct net_device · line 10520

Quoted 3 lines
net/core/dev.c +0 -0
 /**
  *	dev_get_stats	- get network device statistics

Thanks,
Olek

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

From: Yajun Deng <[email protected]>
Date: Wed, 13 Sep 2023 10:08:08 +0800

Quoted 4 lines · Alexander Lobakin
Quoted 2 lines

From: Yajun Deng <[email protected]>
Date: Mon, 11 Sep 2023 16:20:16 +0800

[...]

Quoted 7 lines
Quoted 1 line

EXPORT_SYMBOL_GPL(dev_core_stats_inc); // Why not GPL BTW?

This may be a better option.

Just because EXPORT_SYMBOL(netdev_core_stats_alloc) before,  but I think

EXPORT_SYMBOL_GPL is better.

Ah I see. BTW, if you will still define increment functions as externals, there will be no reason to export netdev_core_stats_alloc() or even make it non-static at all.

Quoted 21 lines

Quoted 19 lines

And then build inlines:

#define DEV_CORE_STATS_INC(FIELD) \
static inline void \
dev_core_stats_##FIELD##_inc(struct net_device *dev) \

{								\
	dev_core_stats_inc(dev,					\
		offsetof(struct net_device_core_stats, FIELD));	\
}

DEV_CORE_STATS_INC(rx_dropped);
...

OR even just make them macros

#define __DEV_CORE_STATS_INC(dev, field)			\
	dev_core_stats_inc(dev,					\
		offsetof(struct net_device_core_stats, field))
Quoted 5 lines
Quoted 3 lines

#define dev_core_stats_rx_dropped_inc(dev) \
__DEV_CORE_STATS_INC(dev, rx_dropped)
...

I would like the former.  Keep it the same as before.

By "the former" you mean to build static inlines or externals? Seems like the first one, but I got confused by your "the same as before" :D

Quoted 11 lines
Quoted 10 lines

Just don't copy that awful Thunderbird's line wrap and don't assume this code builds and works and that is something finished/polished.

You'll be able to trace functions and you'll be able to understand which counter has been incremented by checking the second argument, i.e. the field offset (IIRC tracing shows you arguments).
And that way you wouldn't geometrically increase the number of symbol exports and deal with its consequences.

I agree that.

Ok, after this one I guess you meant "I'd like to use your approach with static inlines".

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

/**
* dev_get_stats - get network device statistics

Thanks,
Olek

Thanks,
Olek

author
#15 lore raw
Quoted 49 lines · Alexander Lobakin

From: Yajun Deng <[email protected]>
Date: Wed, 13 Sep 2023 10:08:08 +0800

Quoted 3 lines · Alexander Lobakin
Quoted 2 lines

From: Yajun Deng <[email protected]>
Date: Mon, 11 Sep 2023 16:20:16 +0800

[...]

Quoted 6 lines
Quoted 1 line

EXPORT_SYMBOL_GPL(dev_core_stats_inc); // Why not GPL BTW?

This may be a better option.

Just because EXPORT_SYMBOL(netdev_core_stats_alloc) before,  but I think

EXPORT_SYMBOL_GPL is better.

Ah I see. BTW, if you will still define increment functions as externals, there will be no reason to export netdev_core_stats_alloc() or even make it non-static at all.

Quoted 16 lines
Quoted 16 lines

And then build inlines:

#define DEV_CORE_STATS_INC(FIELD) \
static inline void \
dev_core_stats_##FIELD##_inc(struct net_device *dev) \

{								\
	dev_core_stats_inc(dev,					\
		offsetof(struct net_device_core_stats, FIELD));	\
}

DEV_CORE_STATS_INC(rx_dropped);
...

OR even just make them macros

Quoted 8 lines
Quoted 7 lines
#define __DEV_CORE_STATS_INC(dev, field)			\
	dev_core_stats_inc(dev,					\
		offsetof(struct net_device_core_stats, field))

#define dev_core_stats_rx_dropped_inc(dev) \
__DEV_CORE_STATS_INC(dev, rx_dropped)
...

I would like the former.  Keep it the same as before.

By "the former" you mean to build static inlines or externals? Seems like the first one, but I got confused by your "the same as before" :D

Quoted 4 lines
Quoted 4 lines

Just don't copy that awful Thunderbird's line wrap and don't assume this code builds and works and that is something finished/polished.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines

You'll be able to trace functions and you'll be able to understand which counter has been incremented by checking the second argument, i.e. the field offset (IIRC tracing shows you arguments).
And that way you wouldn't geometrically increase the number of symbol exports and deal with its consequences.

I agree that.

Ok, after this one I guess you meant "I'd like to use your approach with static inlines".

Finally, I give up this approach.

The new function dev_core_stats_inc() didn't called by external modules directly.

So EXPORT_SYMBOL_GPL(dev_core_stats_inc) can be removed by anyone.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

/**
* dev_get_stats - get network device statistics

Thanks,
Olek

Thanks,
Olek

#16 lore raw

From: Yajun Deng <[email protected]>
Date: Thu, 14 Sep 2023 10:44:14 +0800

Quoted 58 lines · Alexander Lobakin
Quoted 56 lines

From: Yajun Deng <[email protected]>
Date: Wed, 13 Sep 2023 10:08:08 +0800

Quoted 3 lines · Alexander Lobakin
Quoted 2 lines

From: Yajun Deng <[email protected]>
Date: Mon, 11 Sep 2023 16:20:16 +0800

[...]

Quoted 6 lines
Quoted 1 line

EXPORT_SYMBOL_GPL(dev_core_stats_inc); // Why not GPL BTW?

This may be a better option.

Just because EXPORT_SYMBOL(netdev_core_stats_alloc) before,  but I think

EXPORT_SYMBOL_GPL is better.

Ah I see. BTW, if you will still define increment functions as externals, there will be no reason to export netdev_core_stats_alloc() or even make it non-static at all.

Quoted 16 lines

Quoted 15 lines

And then build inlines:

#define DEV_CORE_STATS_INC(FIELD)                \
static inline void                        \
dev_core_stats_##FIELD##_inc(struct net_device *dev)        \
{                                \
    dev_core_stats_inc(dev,                    \
        offsetof(struct net_device_core_stats, FIELD));    \
}

DEV_CORE_STATS_INC(rx_dropped);
...

OR even just make them macros

Quoted 8 lines
Quoted 7 lines

#define __DEV_CORE_STATS_INC(dev, field)            \
    dev_core_stats_inc(dev,                    \
        offsetof(struct net_device_core_stats, field))

#define dev_core_stats_rx_dropped_inc(dev)            \
    __DEV_CORE_STATS_INC(dev, rx_dropped)
...

I would like the former.  Keep it the same as before.

By "the former" you mean to build static inlines or externals? Seems like the first one, but I got confused by your "the same as before" :D

Quoted 11 lines
Quoted 11 lines

Just don't copy that awful Thunderbird's line wrap and don't assume this
code builds and works and that is something finished/polished.

You'll be able to trace functions and you'll be able to understand which
counter has been incremented by checking the second argument, i.e. the field offset (IIRC tracing shows you arguments).
And that way you wouldn't geometrically increase the number of symbol exports and deal with its consequences.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I agree that.

Ok, after this one I guess you meant "I'd like to use your approach with static inlines".

Finally, I give up this approach.

The new function dev_core_stats_inc() didn't called by external modules directly.

If it's called via an inline or macro or whatever, it still needs to be exported.
Double-check that modpost doesn't complain on allmodconfig build.

Quoted 2 lines

So EXPORT_SYMBOL_GPL(dev_core_stats_inc) can be removed by anyone.

That doesn't mean it won't be needed tomorrow.
And I don't feel like it's a good excuse to define 1 external function per counter instead of 1 external + static inlines for the rest. It's not only about the exports.
Esp. given that I wrote almost the whole code needed for it to work in one of my previous replies.

If you don't want to do that, I could take it over xD

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

    /**
   *    dev_get_stats    - get network device statistics

Thanks,
Olek

Thanks,
Olek

Thanks,
Olek