diff mbox series

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

Message ID 20240404075740.30682-1-petr@tesarici.cz (mailing list archive)
State Accepted
Commit 38a15d0a50e0a43778561a5861403851f0b0194c
Delegated to: Netdev Maintainers
Headers show
Series [net] u64_stats: fix u64_stats_init() for lockdep when used repeatedly in one file | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 10937 this patch: 10937
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 2 of 2 maintainers
netdev/build_clang success Errors and warnings before: 1405 this patch: 1405
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 11830 this patch: 11830
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-05--03-00 (tests: 951)

Commit Message

Petr Tesařík April 4, 2024, 7:57 a.m. UTC
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/ea1567d9-ce66-45e6-8168-ac40a47d1821@roeck-us.net/
Signed-off-by: Petr Tesarik <petr@tesarici.cz>
---
 include/linux/u64_stats_sync.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Simon Horman April 5, 2024, 7:51 p.m. UTC | #1
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/ea1567d9-ce66-45e6-8168-ac40a47d1821@roeck-us.net/
> Signed-off-by: Petr Tesarik <petr@tesarici.cz>

Reviewed-by: Simon Horman <horms@kernel.org>

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.

...
Eric Dumazet April 5, 2024, 9:08 p.m. UTC | #2
On Thu, Apr 4, 2024 at 9:58 AM Petr Tesarik <petr@tesarici.cz> 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/ea1567d9-ce66-45e6-8168-ac40a47d1821@roeck-us.net/
> Signed-off-by: Petr Tesarik <petr@tesarici.cz>

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

Reviewed-by: Eric Dumazet <edumazet@google.com>
patchwork-bot+netdevbpf@kernel.org April 6, 2024, 6:10 a.m. UTC | #3
Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

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!
Petr Tesařík April 6, 2024, 7:44 a.m. UTC | #4
On Fri, 5 Apr 2024 23:08:58 +0200
Eric Dumazet <edumazet@google.com> wrote:

> On Thu, Apr 4, 2024 at 9:58 AM Petr Tesarik <petr@tesarici.cz> 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/ea1567d9-ce66-45e6-8168-ac40a47d1821@roeck-us.net/
> > Signed-off-by: Petr Tesarik <petr@tesarici.cz>  
> 
> 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 <edumazet@google.com>
Petr Tesařík April 6, 2024, 8:12 a.m. UTC | #5
On Fri, 5 Apr 2024 20:51:17 +0100
Simon Horman <horms@kernel.org> 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/ea1567d9-ce66-45e6-8168-ac40a47d1821@roeck-us.net/
> > Signed-off-by: Petr Tesarik <petr@tesarici.cz>  
> 
> Reviewed-by: Simon Horman <horms@kernel.org>
> 
> 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
diff mbox series

Patch

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