2024-04-04 07:59:21

by Petr Tesařík

[permalink] [raw]
Subject: [PATCH net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file

Fix bogus lockdep warnings if multiple u64_stats_sync variables are
initialized in the same file.

With CONFIG_LOCKDEP, seqcount_init() is a macro which declares:

static struct lock_class_key __key;

Since u64_stats_init() is a function (albeit an inline one), all calls
within the same file end up using the same instance, effectively treating
them all as a single lock-class.

Fixes: 9464ca650008 ("net: make u64_stats_init() a function")
Closes: https://lore.kernel.org/netdev/[email protected]/
Signed-off-by: Petr Tesarik <[email protected]>
---
include/linux/u64_stats_sync.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/include/linux/u64_stats_sync.h b/include/linux/u64_stats_sync.h
index ffe48e69b3f3..457879938fc1 100644
--- a/include/linux/u64_stats_sync.h
+++ b/include/linux/u64_stats_sync.h
@@ -135,10 +135,11 @@ static inline void u64_stats_inc(u64_stats_t *p)
p->v++;
}

-static inline void u64_stats_init(struct u64_stats_sync *syncp)
-{
- seqcount_init(&syncp->seq);
-}
+#define u64_stats_init(syncp) \
+ do { \
+ struct u64_stats_sync *__s = (syncp); \
+ seqcount_init(&__s->seq); \
+ } while (0)

static inline void __u64_stats_update_begin(struct u64_stats_sync *syncp)
{
--
2.44.0



2024-04-05 19:51:33

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file

On Thu, Apr 04, 2024 at 09:57:40AM +0200, Petr Tesarik wrote:
> Fix bogus lockdep warnings if multiple u64_stats_sync variables are
> initialized in the same file.
>
> With CONFIG_LOCKDEP, seqcount_init() is a macro which declares:
>
> static struct lock_class_key __key;
>
> Since u64_stats_init() is a function (albeit an inline one), all calls
> within the same file end up using the same instance, effectively treating
> them all as a single lock-class.
>
> Fixes: 9464ca650008 ("net: make u64_stats_init() a function")
> Closes: https://lore.kernel.org/netdev/[email protected]/
> Signed-off-by: Petr Tesarik <[email protected]>

Reviewed-by: Simon Horman <[email protected]>

Interesting bug. I'm wondering if you also looked over other users of
u64_stats_init() to see if any of them can result in unexpected aliasing of
lock keys too.

..

2024-04-05 21:09:20

by Eric Dumazet

[permalink] [raw]
Subject: Re: [PATCH net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file

On Thu, Apr 4, 2024 at 9:58 AM Petr Tesarik <[email protected]> wrote:
>
> Fix bogus lockdep warnings if multiple u64_stats_sync variables are
> initialized in the same file.
>
> With CONFIG_LOCKDEP, seqcount_init() is a macro which declares:
>
> static struct lock_class_key __key;
>
> Since u64_stats_init() is a function (albeit an inline one), all calls
> within the same file end up using the same instance, effectively treating
> them all as a single lock-class.
>
> Fixes: 9464ca650008 ("net: make u64_stats_init() a function")
> Closes: https://lore.kernel.org/netdev/[email protected]/
> Signed-off-by: Petr Tesarik <[email protected]>

I thought I gave a Reviewed-by: tag already...

Reviewed-by: Eric Dumazet <[email protected]>

2024-04-06 06:10:54

by patchwork-bot+netdevbpf

[permalink] [raw]
Subject: Re: [PATCH net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

On Thu, 4 Apr 2024 09:57:40 +0200 you wrote:
> Fix bogus lockdep warnings if multiple u64_stats_sync variables are
> initialized in the same file.
>
> With CONFIG_LOCKDEP, seqcount_init() is a macro which declares:
>
> static struct lock_class_key __key;
>
> [...]

Here is the summary with links:
- [net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file
https://git.kernel.org/netdev/net/c/38a15d0a50e0

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



2024-04-06 07:45:19

by Petr Tesařík

[permalink] [raw]
Subject: Re: [PATCH net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file

On Fri, 5 Apr 2024 23:08:58 +0200
Eric Dumazet <[email protected]> wrote:

> On Thu, Apr 4, 2024 at 9:58 AM Petr Tesarik <[email protected]> wrote:
> >
> > Fix bogus lockdep warnings if multiple u64_stats_sync variables are
> > initialized in the same file.
> >
> > With CONFIG_LOCKDEP, seqcount_init() is a macro which declares:
> >
> > static struct lock_class_key __key;
> >
> > Since u64_stats_init() is a function (albeit an inline one), all calls
> > within the same file end up using the same instance, effectively treating
> > them all as a single lock-class.
> >
> > Fixes: 9464ca650008 ("net: make u64_stats_init() a function")
> > Closes: https://lore.kernel.org/netdev/[email protected]/
> > Signed-off-by: Petr Tesarik <[email protected]>
>
> I thought I gave a Reviewed-by: tag already...

You did, but at that time I didn't know which tree should be used to
merge the patch, so it appeared as a stranded patch on LKML. Once we
agreed it should go through net, I sent it again, but the resend didn't
have it.

Thanks for your assistance!

Petr T

> Reviewed-by: Eric Dumazet <[email protected]>


2024-04-06 08:12:21

by Petr Tesařík

[permalink] [raw]
Subject: Re: [PATCH net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file

On Fri, 5 Apr 2024 20:51:17 +0100
Simon Horman <[email protected]> wrote:

> On Thu, Apr 04, 2024 at 09:57:40AM +0200, Petr Tesarik wrote:
> > Fix bogus lockdep warnings if multiple u64_stats_sync variables are
> > initialized in the same file.
> >
> > With CONFIG_LOCKDEP, seqcount_init() is a macro which declares:
> >
> > static struct lock_class_key __key;
> >
> > Since u64_stats_init() is a function (albeit an inline one), all calls
> > within the same file end up using the same instance, effectively treating
> > them all as a single lock-class.
> >
> > Fixes: 9464ca650008 ("net: make u64_stats_init() a function")
> > Closes: https://lore.kernel.org/netdev/[email protected]/
> > Signed-off-by: Petr Tesarik <[email protected]>
>
> Reviewed-by: Simon Horman <[email protected]>
>
> Interesting bug. I'm wondering if you also looked over other users of
> u64_stats_init() to see if any of them can result in unexpected aliasing of
> lock keys too.

I didn't.

I have now run `git grep u64_stats_init | sed -e 's/:.*//' | uniq -d`
and there indeed appears to be some aliasing in other files.

block/blk-cgroup.c:
- struct blkg_iostat_set.sync
- struct blkg_iostat_set.sync

drivers/net/ethernet/amazon/ena/ena_netdev.c:
- struct ena_ring.syncp
struct ena_adapter.syncp

drivers/net/ethernet/aquantia/atlantic/aq_ring.c:
- struct aq_ring_stats_tx_s.syncp
- struct aq_ring_stats_rx_s.syncp

drivers/net/ethernet/emulex/benet/be_main.c:
- struct be_tx_stats.sync
+ aliasing between this struct in
- struct be_tx_obj
- struct be_rx_obj
- struct be_tx_stats.sync_compl

drivers/net/ethernet/google/gve/gve_main.c:
- struct gve_priv.rx[].statss
- struct gve_priv.tx[].statss

drivers/net/ethernet/intel/fm10k/fm10k_netdev.c:
- tx_ring->syncp
- rx_ring->syncp

drivers/net/ethernet/intel/i40e/i40e_txrx.c:
- tx_ring->syncp
- rx_ring->syncp

drivers/net/ethernet/intel/igb/igb_main.c:
- struct igb_ring.tx_syncp
- struct igb_ring.tx_syncp2
- struct igb_ring.rx_syncp

drivers/net/ethernet/intel/ixgbe/ixgbe_main.c:
- struct ixgbe_adapter.rx_ring[]->syncp
- struct ixgbe_adapter.tx_ring[]->syncp
- struct ixgbe_adapter.xdp_ring[]->syncp

drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c:
- tx_ring->syncp
- rx_ring->syncp

drivers/net/ethernet/marvell/sky2.c:
- struct sky2_port.tx_stats.syncp
- struct sky2_port.rx_stats.syncp

drivers/net/ethernet/microsoft/mana/mana_en.c:
- struct mana_txq.stats.syncp
- struct mana_rxq.stats.syncp

drivers/net/ethernet/netronome/nfp/nfp_net_dp.c:
- struct nfp_net_tx_ring.r_vec->tx_sync
- struct nfp_net_rx_ring.r_vec->rx_sync

drivers/net/ethernet/nvidia/forcedeth.c:
- struct fe_priv.swstats_tx_syncp
- struct fe_priv.swstats_rx_syncp

drivers/net/ethernet/realtek/8139too.c:
- struct rtl8139_private.tx_stats.syncp
- struct rtl8139_private.rx_stats.syncp

drivers/net/ethernet/socionext/sni_ave.c:
- struct ave_private.stats_tx.syncp
- struct ave_private.stats_rx.syncp

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c:
- this was the trigger ;-)

drivers/net/ethernet/ti/netcp_core.c:
- struct netcp_stats.syncp_tx
- struct netcp_stats.syncp_rx

drivers/net/ethernet/via/via-rhine.c:
- struct rhine_private.tx_stats.syncp
- struct rhine_private.rx_stats.syncp

drivers/net/ethernet/xilinx/xilinx_axienet_main.c:
- struct axienet_local.tx_stat_sync
- struct axienet_local.rx_stat_sync

drivers/net/hyperv/netvsc.c:
- struct netvsc_channel.tx_stats.syncp
- struct netvsc_channel.rx_stats.syncp

drivers/net/ifb.c:
- struct ifb_q_private.tx_stats.sync
- struct ifb_q_private.rx_stats.sync

drivers/net/mhi_net.c:
- struct mhi_net_stats.tx_syncp
- struct mhi_net_stats.rx_syncp

drivers/net/virtio_net.c:
- struct virtnet_info.rq[].stats.syncp
- struct virtnet_info.sq[].stats.syncp

drivers/net/wwan/mhi_wwan_mbim.c:
- struct mhi_mbim_link.tx_syncp
- struct mhi_mbim_link.rx_syncp

drivers/vdpa/vdpa_sim/vdpa_sim_net.c:
- struct vdpasim_net.tx_stats.syncp
- struct vdpasim_net.rx_stats.syncp
- struct vdpasim_net.cq_stats.syncp

HTH
Petr T