public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy()
@ 2026-01-20  9:21 David Yang
  2026-01-20  9:21 ` [PATCH net-next 1/4] " David Yang
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: David Yang @ 2026-01-20  9:21 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Sabrina Dubroca, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov,
	Ido Schimmel, Simon Horman, Mark Bloch, Petr Machata,
	Stanislav Fomichev, Carolina Jubran, Breno Leitao,
	Shigeru Yoshida, linux-kernel, bridge

On 64bit arches, struct u64_stats_sync is empty and provides no help
against load/store tearing. memcpy() should not be considered atomic
against u64 values. Use u64_stats_copy() instead.

David Yang (4):
  u64_stats: Introduce u64_stats_copy()
  net: bridge: mcast: fix memcpy with u64_stats
  macsec: fix memcpy with u64_stats
  vxlan: vnifilter: fix memcpy with u64_stats

 drivers/net/macsec.c                |  6 +++---
 drivers/net/vxlan/vxlan_vnifilter.c |  2 +-
 include/linux/u64_stats_sync.h      | 15 +++++++++++++++
 net/bridge/br_multicast.c           |  2 +-
 4 files changed, 20 insertions(+), 5 deletions(-)

-- 
2.51.0


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

* [PATCH net-next 1/4] u64_stats: Introduce u64_stats_copy()
  2026-01-20  9:21 [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() David Yang
@ 2026-01-20  9:21 ` David Yang
  2026-01-21 17:23   ` Sabrina Dubroca
  2026-01-20  9:21 ` [PATCH net-next 2/4] net: bridge: mcast: fix memcpy with u64_stats David Yang
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: David Yang @ 2026-01-20  9:21 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Sabrina Dubroca, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov,
	Ido Schimmel, Simon Horman, Mark Bloch, Petr Machata,
	Stanislav Fomichev, Carolina Jubran, Breno Leitao,
	Shigeru Yoshida, linux-kernel, bridge

The following (anti-)pattern was observed in the code tree:

        do {
                start = u64_stats_fetch_begin(&pstats->syncp);
                memcpy(&temp, &pstats->stats, sizeof(temp));
        } while (u64_stats_fetch_retry(&pstats->syncp, start));

On 64bit arches, struct u64_stats_sync is empty and provides no help
against load/store tearing, especially for memcpy(), for which arches may
provide their highly-optimized implements.

In theory the affected code should convert to u64_stats_t, or use
READ_ONCE()/WRITE_ONCE() properly.

However since there are needs to copy chunks of statistics, instead of
writing loops at random places, we provide a safe memcpy() variant for
u64_stats.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 include/linux/u64_stats_sync.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h
index 457879938fc1..849ff6e159c6 100644
--- a/include/linux/u64_stats_sync.h
+++ b/include/linux/u64_stats_sync.h
@@ -79,6 +79,14 @@ static inline u64 u64_stats_read(const u64_stats_t *p)
 	return local64_read(&p->v);
 }
 
+static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
+{
+	BUILD_BUG_ON(len % sizeof(u64_stats_t));
+	for (size_t i = 0; i < len / sizeof(u64_stats_t); i++)
+		((u64 *)dst)[i] = local64_read(&((local64_t *)src)[i]);
+	return dst;
+}
+
 static inline void u64_stats_set(u64_stats_t *p, u64 val)
 {
 	local64_set(&p->v, val);
@@ -110,6 +118,7 @@ static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
 }
 
 #else /* 64 bit */
+#include <linux/string.h>
 
 typedef struct {
 	u64		v;
@@ -120,6 +129,12 @@ static inline u64 u64_stats_read(const u64_stats_t *p)
 	return p->v;
 }
 
+static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
+{
+	BUILD_BUG_ON(len % sizeof(u64_stats_t));
+	return memcpy(dst, src, len);
+}
+
 static inline void u64_stats_set(u64_stats_t *p, u64 val)
 {
 	p->v = val;
-- 
2.51.0


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

* [PATCH net-next 2/4] net: bridge: mcast: fix memcpy with u64_stats
  2026-01-20  9:21 [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() David Yang
  2026-01-20  9:21 ` [PATCH net-next 1/4] " David Yang
@ 2026-01-20  9:21 ` David Yang
  2026-01-20  9:21 ` [PATCH net-next 3/4] macsec: " David Yang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: David Yang @ 2026-01-20  9:21 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Nikolay Aleksandrov, Ido Schimmel, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, bridge,
	linux-kernel

On 64bit arches, struct u64_stats_sync is empty and provides no help
against load/store tearing. memcpy() should not be considered atomic
against u64 values. Use u64_stats_copy() instead.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 net/bridge/br_multicast.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index d55a4ab87837..dccae08b4f4c 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -5201,7 +5201,7 @@ void br_multicast_get_stats(const struct net_bridge *br,
 
 		do {
 			start = u64_stats_fetch_begin(&cpu_stats->syncp);
-			memcpy(&temp, &cpu_stats->mstats, sizeof(temp));
+			u64_stats_copy(&temp, &cpu_stats->mstats, sizeof(temp));
 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
 
 		mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries);
-- 
2.51.0


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

* [PATCH net-next 3/4] macsec: fix memcpy with u64_stats
  2026-01-20  9:21 [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() David Yang
  2026-01-20  9:21 ` [PATCH net-next 1/4] " David Yang
  2026-01-20  9:21 ` [PATCH net-next 2/4] net: bridge: mcast: fix memcpy with u64_stats David Yang
@ 2026-01-20  9:21 ` David Yang
  2026-01-20  9:21 ` [PATCH net-next 4/4] vxlan: vnifilter: " David Yang
  2026-01-21 11:16 ` [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() Ido Schimmel
  4 siblings, 0 replies; 13+ messages in thread
From: David Yang @ 2026-01-20  9:21 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Sabrina Dubroca, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, linux-kernel

On 64bit arches, struct u64_stats_sync is empty and provides no help
against load/store tearing. memcpy() should not be considered atomic
against u64 values. Use u64_stats_copy() instead.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/macsec.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/net/macsec.c b/drivers/net/macsec.c
index 5200fd5a10e5..c2cb2d20976b 100644
--- a/drivers/net/macsec.c
+++ b/drivers/net/macsec.c
@@ -2806,7 +2806,7 @@ static void get_rx_sc_stats(struct net_device *dev,
 		stats = per_cpu_ptr(rx_sc->stats, cpu);
 		do {
 			start = u64_stats_fetch_begin(&stats->syncp);
-			memcpy(&tmp, &stats->stats, sizeof(tmp));
+			u64_stats_copy(&tmp, &stats->stats, sizeof(tmp));
 		} while (u64_stats_fetch_retry(&stats->syncp, start));
 
 		sum->InOctetsValidated += tmp.InOctetsValidated;
@@ -2887,7 +2887,7 @@ static void get_tx_sc_stats(struct net_device *dev,
 		stats = per_cpu_ptr(macsec_priv(dev)->secy.tx_sc.stats, cpu);
 		do {
 			start = u64_stats_fetch_begin(&stats->syncp);
-			memcpy(&tmp, &stats->stats, sizeof(tmp));
+			u64_stats_copy(&tmp, &stats->stats, sizeof(tmp));
 		} while (u64_stats_fetch_retry(&stats->syncp, start));
 
 		sum->OutPktsProtected   += tmp.OutPktsProtected;
@@ -2943,7 +2943,7 @@ static void get_secy_stats(struct net_device *dev, struct macsec_dev_stats *sum)
 		stats = per_cpu_ptr(macsec_priv(dev)->stats, cpu);
 		do {
 			start = u64_stats_fetch_begin(&stats->syncp);
-			memcpy(&tmp, &stats->stats, sizeof(tmp));
+			u64_stats_copy(&tmp, &stats->stats, sizeof(tmp));
 		} while (u64_stats_fetch_retry(&stats->syncp, start));
 
 		sum->OutPktsUntagged  += tmp.OutPktsUntagged;
-- 
2.51.0


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

* [PATCH net-next 4/4] vxlan: vnifilter: fix memcpy with u64_stats
  2026-01-20  9:21 [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() David Yang
                   ` (2 preceding siblings ...)
  2026-01-20  9:21 ` [PATCH net-next 3/4] macsec: " David Yang
@ 2026-01-20  9:21 ` David Yang
  2026-01-21 11:16 ` [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() Ido Schimmel
  4 siblings, 0 replies; 13+ messages in thread
From: David Yang @ 2026-01-20  9:21 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Ido Schimmel, Nikolay Aleksandrov,
	Mark Bloch, Shigeru Yoshida, Breno Leitao, Stanislav Fomichev,
	Carolina Jubran, linux-kernel

On 64bit arches, struct u64_stats_sync is empty and provides no help
against load/store tearing. memcpy() should not be considered atomic
against u64 values. Use u64_stats_copy() instead.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/vxlan/vxlan_vnifilter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/vxlan/vxlan_vnifilter.c b/drivers/net/vxlan/vxlan_vnifilter.c
index adc89e651e27..cde897d92f24 100644
--- a/drivers/net/vxlan/vxlan_vnifilter.c
+++ b/drivers/net/vxlan/vxlan_vnifilter.c
@@ -126,7 +126,7 @@ static void vxlan_vnifilter_stats_get(const struct vxlan_vni_node *vninode,
 		pstats = per_cpu_ptr(vninode->stats, i);
 		do {
 			start = u64_stats_fetch_begin(&pstats->syncp);
-			memcpy(&temp, &pstats->stats, sizeof(temp));
+			u64_stats_copy(&temp, &pstats->stats, sizeof(temp));
 		} while (u64_stats_fetch_retry(&pstats->syncp, start));
 
 		dest->rx_packets += temp.rx_packets;
-- 
2.51.0


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

* Re: [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy()
  2026-01-20  9:21 [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() David Yang
                   ` (3 preceding siblings ...)
  2026-01-20  9:21 ` [PATCH net-next 4/4] vxlan: vnifilter: " David Yang
@ 2026-01-21 11:16 ` Ido Schimmel
  2026-01-21 17:21   ` Sabrina Dubroca
  4 siblings, 1 reply; 13+ messages in thread
From: Ido Schimmel @ 2026-01-21 11:16 UTC (permalink / raw)
  To: David Yang
  Cc: netdev, Sabrina Dubroca, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov,
	Simon Horman, Mark Bloch, Petr Machata, Stanislav Fomichev,
	Carolina Jubran, Breno Leitao, Shigeru Yoshida, linux-kernel,
	bridge

On Tue, Jan 20, 2026 at 05:21:28PM +0800, David Yang wrote:
> On 64bit arches, struct u64_stats_sync is empty and provides no help
> against load/store tearing. memcpy() should not be considered atomic
> against u64 values. Use u64_stats_copy() instead.

The existing memcpy() does seem problematic (even if in practice it's
not) and the proposed solution in patch #1 seems OK to me given that all
the callers only pass structures containing 64 bit counters. Couldn't
find any more instances of this pattern.

Reviewed-by: Ido Schimmel <idosch@nvidia.com>

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

* Re: [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy()
  2026-01-21 11:16 ` [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() Ido Schimmel
@ 2026-01-21 17:21   ` Sabrina Dubroca
  2026-01-22  8:00     ` Ido Schimmel
  0 siblings, 1 reply; 13+ messages in thread
From: Sabrina Dubroca @ 2026-01-21 17:21 UTC (permalink / raw)
  To: Ido Schimmel
  Cc: David Yang, netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Simon Horman,
	Mark Bloch, Petr Machata, Stanislav Fomichev, Carolina Jubran,
	Breno Leitao, Shigeru Yoshida, linux-kernel, bridge

2026-01-21, 13:16:35 +0200, Ido Schimmel wrote:
> On Tue, Jan 20, 2026 at 05:21:28PM +0800, David Yang wrote:
> > On 64bit arches, struct u64_stats_sync is empty and provides no help
> > against load/store tearing. memcpy() should not be considered atomic
> > against u64 values. Use u64_stats_copy() instead.
> 
> The existing memcpy() does seem problematic (even if in practice it's
> not) and the proposed solution in patch #1 seems OK to me given that all
> the callers only pass structures containing 64 bit counters. Couldn't
> find any more instances of this pattern.

No direct instances using memcpy, but do we need to also full structs
copied within a u64_stats_fetch_begin/u64_stats_fetch_retry loop?


// net/mpls/af_mpls.c
static void mpls_get_stats(struct mpls_dev *mdev,
			   struct mpls_link_stats *stats)
{
[...]
	for_each_possible_cpu(i) {
		struct mpls_link_stats local;
		unsigned int start;

		p = per_cpu_ptr(mdev->stats, i);
		do {
			start = u64_stats_fetch_begin(&p->syncp);
			local = p->stats;
		} while (u64_stats_fetch_retry(&p->syncp, start));
[...]

// net/openvswitch/datapath.c
static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
			 struct ovs_dp_megaflow_stats *mega_stats)
{
[...]
	for_each_possible_cpu(i) {
		const struct dp_stats_percpu *percpu_stats;
		struct dp_stats_percpu local_stats;
		unsigned int start;

		percpu_stats = per_cpu_ptr(dp->stats_percpu, i);

		do {
			start = u64_stats_fetch_begin(&percpu_stats->syncp);
			local_stats = *percpu_stats;
		} while (u64_stats_fetch_retry(&percpu_stats->syncp, start));
[...]


And if not: can't we just use the same pattern for those other cases
that this series is touching?

-- 
Sabrina

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

* Re: [PATCH net-next 1/4] u64_stats: Introduce u64_stats_copy()
  2026-01-20  9:21 ` [PATCH net-next 1/4] " David Yang
@ 2026-01-21 17:23   ` Sabrina Dubroca
  2026-01-21 18:22     ` Yangfl
  0 siblings, 1 reply; 13+ messages in thread
From: Sabrina Dubroca @ 2026-01-21 17:23 UTC (permalink / raw)
  To: David Yang
  Cc: netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Ido Schimmel,
	Simon Horman, Mark Bloch, Petr Machata, Stanislav Fomichev,
	Carolina Jubran, Breno Leitao, Shigeru Yoshida, linux-kernel,
	bridge

2026-01-20, 17:21:29 +0800, David Yang wrote:
> The following (anti-)pattern was observed in the code tree:
> 
>         do {
>                 start = u64_stats_fetch_begin(&pstats->syncp);
>                 memcpy(&temp, &pstats->stats, sizeof(temp));
>         } while (u64_stats_fetch_retry(&pstats->syncp, start));
> 
> On 64bit arches, struct u64_stats_sync is empty and provides no help
> against load/store tearing, especially for memcpy(), for which arches may
> provide their highly-optimized implements.
> 
> In theory the affected code should convert to u64_stats_t, or use
> READ_ONCE()/WRITE_ONCE() properly.
> 
> However since there are needs to copy chunks of statistics, instead of
> writing loops at random places, we provide a safe memcpy() variant for
> u64_stats.
> 
> Signed-off-by: David Yang <mmyangfl@gmail.com>
> ---
>  include/linux/u64_stats_sync.h | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h
> index 457879938fc1..849ff6e159c6 100644
> --- a/include/linux/u64_stats_sync.h
> +++ b/include/linux/u64_stats_sync.h
> @@ -79,6 +79,14 @@ static inline u64 u64_stats_read(const u64_stats_t *p)
>  	return local64_read(&p->v);
>  }
>  
> +static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
> +{
> +	BUILD_BUG_ON(len % sizeof(u64_stats_t));
> +	for (size_t i = 0; i < len / sizeof(u64_stats_t); i++)
> +		((u64 *)dst)[i] = local64_read(&((local64_t *)src)[i]);

Maybe u64_stats_read/u64_stats_t instead of local64_read/local64_t?

> +	return dst;
> +}

Since this new helper is always used within a
u64_stats_fetch_begin/u64_stats_fetch_retry loop, maybe it would be
nicer to push the retry loop into the helper as well?  Not a strong
opinion. It would be a bit "simpler" for the callers, but your current
proposal has the advantage of looking like memcpy(), and of also
looking (for the caller) like other retry loops fetching each counter
explicitly.

Either way, I think extending the "Usage" section of the big comment
at the top of the file with this new helper would be nice.

-- 
Sabrina

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

* Re: [PATCH net-next 1/4] u64_stats: Introduce u64_stats_copy()
  2026-01-21 17:23   ` Sabrina Dubroca
@ 2026-01-21 18:22     ` Yangfl
  2026-01-22 11:20       ` Sabrina Dubroca
  0 siblings, 1 reply; 13+ messages in thread
From: Yangfl @ 2026-01-21 18:22 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Ido Schimmel,
	Simon Horman, Mark Bloch, Petr Machata, Stanislav Fomichev,
	Carolina Jubran, Breno Leitao, Shigeru Yoshida, linux-kernel,
	bridge

On Thu, Jan 22, 2026 at 1:23 AM Sabrina Dubroca <sd@queasysnail.net> wrote:
>
> 2026-01-20, 17:21:29 +0800, David Yang wrote:
> > The following (anti-)pattern was observed in the code tree:
> >
> >         do {
> >                 start = u64_stats_fetch_begin(&pstats->syncp);
> >                 memcpy(&temp, &pstats->stats, sizeof(temp));
> >         } while (u64_stats_fetch_retry(&pstats->syncp, start));
> >
> > On 64bit arches, struct u64_stats_sync is empty and provides no help
> > against load/store tearing, especially for memcpy(), for which arches may
> > provide their highly-optimized implements.
> >
> > In theory the affected code should convert to u64_stats_t, or use
> > READ_ONCE()/WRITE_ONCE() properly.
> >
> > However since there are needs to copy chunks of statistics, instead of
> > writing loops at random places, we provide a safe memcpy() variant for
> > u64_stats.
> >
> > Signed-off-by: David Yang <mmyangfl@gmail.com>
> > ---
> >  include/linux/u64_stats_sync.h | 15 +++++++++++++++
> >  1 file changed, 15 insertions(+)
> >
> > diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h
> > index 457879938fc1..849ff6e159c6 100644
> > --- a/include/linux/u64_stats_sync.h
> > +++ b/include/linux/u64_stats_sync.h
> > @@ -79,6 +79,14 @@ static inline u64 u64_stats_read(const u64_stats_t *p)
> >       return local64_read(&p->v);
> >  }
> >
> > +static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
> > +{
> > +     BUILD_BUG_ON(len % sizeof(u64_stats_t));
> > +     for (size_t i = 0; i < len / sizeof(u64_stats_t); i++)
> > +             ((u64 *)dst)[i] = local64_read(&((local64_t *)src)[i]);
>
> Maybe u64_stats_read/u64_stats_t instead of local64_read/local64_t?
>

I think casting to u64_stats_t is a bit overkill here since we accept
const void * and we are the actual implementation.

Again, they should convert to u64_stats_t, and this solution already
implies that u64_stats_t is binary compatible with u64. I've already
sent several patches related to that, but that's another issue.

> > +     return dst;
> > +}
>
> Since this new helper is always used within a
> u64_stats_fetch_begin/u64_stats_fetch_retry loop, maybe it would be
> nicer to push the retry loop into the helper as well?  Not a strong
> opinion. It would be a bit "simpler" for the callers, but your current
> proposal has the advantage of looking like memcpy(), and of also
> looking (for the caller) like other retry loops fetching each counter
> explicitly.
>

The callers may want to copy other discontinuous data as well, albeit
no one did it then.

         do {
                 start = u64_stats_fetch_begin(&pstats->syncp);
                 memcpy(...);
                 u64_stats_read(...);
         } while (u64_stats_fetch_retry(&pstats->syncp, start));

It would be redundant to provide two variants of the function.
Moreover, callers can (and already) invent their own reader/writer
helpers, for example

         #define SLIC_GET_STATS_COUNTER(newst, st, counter) \
         { \
                  unsigned int start; \
                  do { \
                          start = u64_stats_fetch_begin(&(st)->syncp); \
                          newst = u64_stats_read(&(st)->counter); \
                  } while (u64_stats_fetch_retry(&(st)->syncp, start)); \
         }

> Either way, I think extending the "Usage" section of the big comment
> at the top of the file with this new helper would be nice.
>

I think callers should avoid memcpy() eventually, and they almost
certainly copied more data than what they need. However, I took a look
at some instances, and it would be non trivial to modify those
drivers.

> --
> Sabrina

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

* Re: [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy()
  2026-01-21 17:21   ` Sabrina Dubroca
@ 2026-01-22  8:00     ` Ido Schimmel
  2026-01-22 11:02       ` Sabrina Dubroca
  0 siblings, 1 reply; 13+ messages in thread
From: Ido Schimmel @ 2026-01-22  8:00 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: David Yang, netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Simon Horman,
	Mark Bloch, Petr Machata, Stanislav Fomichev, Carolina Jubran,
	Breno Leitao, Shigeru Yoshida, linux-kernel, bridge

On Wed, Jan 21, 2026 at 06:21:05PM +0100, Sabrina Dubroca wrote:
> No direct instances using memcpy, but do we need to also full structs
> copied within a u64_stats_fetch_begin/u64_stats_fetch_retry loop?

My understanding is that we cannot rely on the compiler to perform the
copy in any particular way. With the suggested helper it is at least
clear how the copy is done.

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

* Re: [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy()
  2026-01-22  8:00     ` Ido Schimmel
@ 2026-01-22 11:02       ` Sabrina Dubroca
  0 siblings, 0 replies; 13+ messages in thread
From: Sabrina Dubroca @ 2026-01-22 11:02 UTC (permalink / raw)
  To: Ido Schimmel
  Cc: David Yang, netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Simon Horman,
	Mark Bloch, Petr Machata, Stanislav Fomichev, Carolina Jubran,
	Breno Leitao, Shigeru Yoshida, linux-kernel, bridge

2026-01-22, 10:00:43 +0200, Ido Schimmel wrote:
> On Wed, Jan 21, 2026 at 06:21:05PM +0100, Sabrina Dubroca wrote:
> > No direct instances using memcpy, but do we need to also full structs
> > copied within a u64_stats_fetch_begin/u64_stats_fetch_retry loop?
> 
> My understanding is that we cannot rely on the compiler to perform the
> copy in any particular way. With the suggested helper it is at least
> clear how the copy is done.

Ok, thanks. So those (and a few more similar things in drivers/net)
should also be switched to this new helper.

-- 
Sabrina

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

* Re: [PATCH net-next 1/4] u64_stats: Introduce u64_stats_copy()
  2026-01-21 18:22     ` Yangfl
@ 2026-01-22 11:20       ` Sabrina Dubroca
  2026-01-22 15:13         ` Yangfl
  0 siblings, 1 reply; 13+ messages in thread
From: Sabrina Dubroca @ 2026-01-22 11:20 UTC (permalink / raw)
  To: Yangfl
  Cc: netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Ido Schimmel,
	Simon Horman, Mark Bloch, Petr Machata, Stanislav Fomichev,
	Carolina Jubran, Breno Leitao, Shigeru Yoshida, linux-kernel,
	bridge

2026-01-22, 02:22:49 +0800, Yangfl wrote:
> On Thu, Jan 22, 2026 at 1:23 AM Sabrina Dubroca <sd@queasysnail.net> wrote:
> >
> > 2026-01-20, 17:21:29 +0800, David Yang wrote:
> > > The following (anti-)pattern was observed in the code tree:
> > >
> > >         do {
> > >                 start = u64_stats_fetch_begin(&pstats->syncp);
> > >                 memcpy(&temp, &pstats->stats, sizeof(temp));
> > >         } while (u64_stats_fetch_retry(&pstats->syncp, start));
> > >
> > > On 64bit arches, struct u64_stats_sync is empty and provides no help
> > > against load/store tearing, especially for memcpy(), for which arches may
> > > provide their highly-optimized implements.
> > >
> > > In theory the affected code should convert to u64_stats_t, or use
> > > READ_ONCE()/WRITE_ONCE() properly.
> > >
> > > However since there are needs to copy chunks of statistics, instead of
> > > writing loops at random places, we provide a safe memcpy() variant for
> > > u64_stats.
> > >
> > > Signed-off-by: David Yang <mmyangfl@gmail.com>
> > > ---
> > >  include/linux/u64_stats_sync.h | 15 +++++++++++++++
> > >  1 file changed, 15 insertions(+)
> > >
> > > diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h
> > > index 457879938fc1..849ff6e159c6 100644
> > > --- a/include/linux/u64_stats_sync.h
> > > +++ b/include/linux/u64_stats_sync.h
> > > @@ -79,6 +79,14 @@ static inline u64 u64_stats_read(const u64_stats_t *p)
> > >       return local64_read(&p->v);
> > >  }
> > >
> > > +static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
> > > +{
> > > +     BUILD_BUG_ON(len % sizeof(u64_stats_t));
> > > +     for (size_t i = 0; i < len / sizeof(u64_stats_t); i++)
> > > +             ((u64 *)dst)[i] = local64_read(&((local64_t *)src)[i]);
> >
> > Maybe u64_stats_read/u64_stats_t instead of local64_read/local64_t?
> >
> 
> I think casting to u64_stats_t is a bit overkill here since we accept
> const void * and we are the actual implementation.

It would be a bit more consistent. Just within this function you have
2 lines using u64_stats_t and the 3rd uses local64_t. And reusing
types/helpers within a similar context doesn't seem overkill.


[...]
> > Since this new helper is always used within a
> > u64_stats_fetch_begin/u64_stats_fetch_retry loop, maybe it would be
> > nicer to push the retry loop into the helper as well?  Not a strong
> > opinion. It would be a bit "simpler" for the callers, but your current
> > proposal has the advantage of looking like memcpy(), and of also
> > looking (for the caller) like other retry loops fetching each counter
> > explicitly.
> >
> 
> The callers may want to copy other discontinuous data as well, albeit
> no one did it then.

I'm not sure why they would. I think the main point of using memcpy is
"I don't want to copy each counter by name one by one", and possibly
"I don't want to have to patch this code as well if we add a new
counter". If you already have a batch copy for a bunch of counters,
it's usually easier to add others in a contiguous block.


> It would be redundant to provide two variants of the function.
> Moreover, callers can (and already) invent their own reader/writer
> helpers, for example
> 
>          #define SLIC_GET_STATS_COUNTER(newst, st, counter) \
>          { \
>                   unsigned int start; \
>                   do { \
>                           start = u64_stats_fetch_begin(&(st)->syncp); \
>                           newst = u64_stats_read(&(st)->counter); \
>                   } while (u64_stats_fetch_retry(&(st)->syncp, start)); \
>          }

Probably because the retry loop is a bit cumbersome and they'd rather
not c/p it everywhere, and see it in the middle of whatever function
needs it.

> > Either way, I think extending the "Usage" section of the big comment
> > at the top of the file with this new helper would be nice.
> >
> 
> I think callers should avoid memcpy() eventually, and they almost
> certainly copied more data than what they need. However, I took a look
> at some instances, and it would be non trivial to modify those
> drivers.

I'm not asking you to fix something else. But for example commit
316580b69d0a ("u64_stats: provide u64_stats_t type") modified the bit
of documentation we have at the top of the file to help developers who
want to use this API. This patch is introducing a new function and
should also describe how to use it, so that new users aren't tempted
to re-introduce a memcpy.

-- 
Sabrina

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

* Re: [PATCH net-next 1/4] u64_stats: Introduce u64_stats_copy()
  2026-01-22 11:20       ` Sabrina Dubroca
@ 2026-01-22 15:13         ` Yangfl
  0 siblings, 0 replies; 13+ messages in thread
From: Yangfl @ 2026-01-22 15:13 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: netdev, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Nikolay Aleksandrov, Ido Schimmel,
	Simon Horman, Mark Bloch, Petr Machata, Stanislav Fomichev,
	Carolina Jubran, Breno Leitao, Shigeru Yoshida, linux-kernel,
	bridge

On Thu, Jan 22, 2026 at 7:20 PM Sabrina Dubroca <sd@queasysnail.net> wrote:
>
> 2026-01-22, 02:22:49 +0800, Yangfl wrote:
> > On Thu, Jan 22, 2026 at 1:23 AM Sabrina Dubroca <sd@queasysnail.net> wrote:
> > >
> > > 2026-01-20, 17:21:29 +0800, David Yang wrote:
> > > > The following (anti-)pattern was observed in the code tree:
> > > >
> > > >         do {
> > > >                 start = u64_stats_fetch_begin(&pstats->syncp);
> > > >                 memcpy(&temp, &pstats->stats, sizeof(temp));
> > > >         } while (u64_stats_fetch_retry(&pstats->syncp, start));
> > > >
> > > > On 64bit arches, struct u64_stats_sync is empty and provides no help
> > > > against load/store tearing, especially for memcpy(), for which arches may
> > > > provide their highly-optimized implements.
> > > >
> > > > In theory the affected code should convert to u64_stats_t, or use
> > > > READ_ONCE()/WRITE_ONCE() properly.
> > > >
> > > > However since there are needs to copy chunks of statistics, instead of
> > > > writing loops at random places, we provide a safe memcpy() variant for
> > > > u64_stats.
> > > >
> > > > Signed-off-by: David Yang <mmyangfl@gmail.com>
> > > > ---
> > > >  include/linux/u64_stats_sync.h | 15 +++++++++++++++
> > > >  1 file changed, 15 insertions(+)
> > > >
> > > > diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h
> > > > index 457879938fc1..849ff6e159c6 100644
> > > > --- a/include/linux/u64_stats_sync.h
> > > > +++ b/include/linux/u64_stats_sync.h
> > > > @@ -79,6 +79,14 @@ static inline u64 u64_stats_read(const u64_stats_t *p)
> > > >       return local64_read(&p->v);
> > > >  }
> > > >
> > > > +static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
> > > > +{
> > > > +     BUILD_BUG_ON(len % sizeof(u64_stats_t));
> > > > +     for (size_t i = 0; i < len / sizeof(u64_stats_t); i++)
> > > > +             ((u64 *)dst)[i] = local64_read(&((local64_t *)src)[i]);
> > >
> > > Maybe u64_stats_read/u64_stats_t instead of local64_read/local64_t?
> > >
> >
> > I think casting to u64_stats_t is a bit overkill here since we accept
> > const void * and we are the actual implementation.
>
> It would be a bit more consistent. Just within this function you have
> 2 lines using u64_stats_t and the 3rd uses local64_t. And reusing
> types/helpers within a similar context doesn't seem overkill.
>
>
> [...]
> > > Since this new helper is always used within a
> > > u64_stats_fetch_begin/u64_stats_fetch_retry loop, maybe it would be
> > > nicer to push the retry loop into the helper as well?  Not a strong
> > > opinion. It would be a bit "simpler" for the callers, but your current
> > > proposal has the advantage of looking like memcpy(), and of also
> > > looking (for the caller) like other retry loops fetching each counter
> > > explicitly.
> > >
> >
> > The callers may want to copy other discontinuous data as well, albeit
> > no one did it then.
>
> I'm not sure why they would. I think the main point of using memcpy is
> "I don't want to copy each counter by name one by one", and possibly
> "I don't want to have to patch this code as well if we add a new
> counter". If you already have a batch copy for a bunch of counters,
> it's usually easier to add others in a contiguous block.
>

While I agree with your statement, I don't think it's a good idea to
push the retry loop into the helper. u64_stats_copy(syncp, dst, src,
len) would be a strange API, while no others accept syncp argument.
Also, it would give a false appearance to those who read the driver
code, since it does not involve fetch_begin()/fetch_retry()
explicitly.

In my opinion, it would be better to introduce a for-like macro
#define with_u64_stats_fetch(syncp), eliminating two function calls as
well as one variable declaration altogether.

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

end of thread, other threads:[~2026-01-22 15:14 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-20  9:21 [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() David Yang
2026-01-20  9:21 ` [PATCH net-next 1/4] " David Yang
2026-01-21 17:23   ` Sabrina Dubroca
2026-01-21 18:22     ` Yangfl
2026-01-22 11:20       ` Sabrina Dubroca
2026-01-22 15:13         ` Yangfl
2026-01-20  9:21 ` [PATCH net-next 2/4] net: bridge: mcast: fix memcpy with u64_stats David Yang
2026-01-20  9:21 ` [PATCH net-next 3/4] macsec: " David Yang
2026-01-20  9:21 ` [PATCH net-next 4/4] vxlan: vnifilter: " David Yang
2026-01-21 11:16 ` [PATCH net-next 0/4] u64_stats: Introduce u64_stats_copy() Ido Schimmel
2026-01-21 17:21   ` Sabrina Dubroca
2026-01-22  8:00     ` Ido Schimmel
2026-01-22 11:02       ` Sabrina Dubroca

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox