diff mbox series

[v3,net-next,3/4] rtnetlink: Add assertion helpers for per-netns RTNL.

Message ID 20241004221031.77743-4-kuniyu@amazon.com (mailing list archive)
State Accepted
Commit 844e5e7e656d3a7a904fd5607f8491d6fd01db8e
Delegated to: Netdev Maintainers
Headers show
Series rtnetlink: Per-netns RTNL. | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 21 this patch: 21
netdev/build_tools success Errors and warnings before: 0 (+1) this patch: 0 (+1)
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 64 this patch: 64
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 No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 3556 this patch: 3556
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 87 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 7 this patch: 7
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-10-06--15-00 (tests: 775)

Commit Message

Kuniyuki Iwashima Oct. 4, 2024, 10:10 p.m. UTC
Once an RTNL scope is converted with rtnl_net_lock(), we will replace
RTNL helper functions inside the scope with the following per-netns
alternatives:

  ASSERT_RTNL()           -> ASSERT_RTNL_NET(net)
  rcu_dereference_rtnl(p) -> rcu_dereference_rtnl_net(net, p)

Note that the per-netns helpers are equivalent to the conventional
helpers unless CONFIG_DEBUG_NET_SMALL_RTNL is enabled.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 include/linux/rtnetlink.h | 45 +++++++++++++++++++++++++++++++++++----
 net/core/rtnetlink.c      | 12 +++++++++++
 2 files changed, 53 insertions(+), 4 deletions(-)

Comments

Paolo Abeni Oct. 8, 2024, 11:39 a.m. UTC | #1
Hi,

On 10/5/24 00:10, Kuniyuki Iwashima wrote:
> Once an RTNL scope is converted with rtnl_net_lock(), we will replace
> RTNL helper functions inside the scope with the following per-netns
> alternatives:
> 
>    ASSERT_RTNL()           -> ASSERT_RTNL_NET(net)
>    rcu_dereference_rtnl(p) -> rcu_dereference_rtnl_net(net, p)
> 
> Note that the per-netns helpers are equivalent to the conventional
> helpers unless CONFIG_DEBUG_NET_SMALL_RTNL is enabled.
> 
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>

I guess Kuniyuki stripped the ack received on v2 due to the edit here.

@Kuniyuki: in the next iterations, please include a per patch changelog 
to simplify the review.

@Eric: would you mind acking it again? Thanks!

Paolo
Eric Dumazet Oct. 8, 2024, 11:46 a.m. UTC | #2
On Tue, Oct 8, 2024 at 1:39 PM Paolo Abeni <pabeni@redhat.com> wrote:
>
> Hi,
>
> On 10/5/24 00:10, Kuniyuki Iwashima wrote:
> > Once an RTNL scope is converted with rtnl_net_lock(), we will replace
> > RTNL helper functions inside the scope with the following per-netns
> > alternatives:
> >
> >    ASSERT_RTNL()           -> ASSERT_RTNL_NET(net)
> >    rcu_dereference_rtnl(p) -> rcu_dereference_rtnl_net(net, p)
> >
> > Note that the per-netns helpers are equivalent to the conventional
> > helpers unless CONFIG_DEBUG_NET_SMALL_RTNL is enabled.
> >
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
>
> I guess Kuniyuki stripped the ack received on v2 due to the edit here.
>
> @Kuniyuki: in the next iterations, please include a per patch changelog
> to simplify the review.
>
> @Eric: would you mind acking it again? Thanks!

Let me check this new version :)
Eric Dumazet Oct. 8, 2024, 11:48 a.m. UTC | #3
On Sat, Oct 5, 2024 at 12:12 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> Once an RTNL scope is converted with rtnl_net_lock(), we will replace
> RTNL helper functions inside the scope with the following per-netns
> alternatives:
>
>   ASSERT_RTNL()           -> ASSERT_RTNL_NET(net)
>   rcu_dereference_rtnl(p) -> rcu_dereference_rtnl_net(net, p)
>
> Note that the per-netns helpers are equivalent to the conventional
> helpers unless CONFIG_DEBUG_NET_SMALL_RTNL is enabled.
>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>

Reviewed-by: Eric Dumazet <edumazet@google.com>
Kuniyuki Iwashima Oct. 8, 2024, 4:12 p.m. UTC | #4
From: Paolo Abeni <pabeni@redhat.com>
Date: Tue, 8 Oct 2024 13:39:24 +0200
> On 10/5/24 00:10, Kuniyuki Iwashima wrote:
> > Once an RTNL scope is converted with rtnl_net_lock(), we will replace
> > RTNL helper functions inside the scope with the following per-netns
> > alternatives:
> > 
> >    ASSERT_RTNL()           -> ASSERT_RTNL_NET(net)
> >    rcu_dereference_rtnl(p) -> rcu_dereference_rtnl_net(net, p)
> > 
> > Note that the per-netns helpers are equivalent to the conventional
> > helpers unless CONFIG_DEBUG_NET_SMALL_RTNL is enabled.
> > 
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> 
> I guess Kuniyuki stripped the ack received on v2 due to the edit here.
> 
> @Kuniyuki: in the next iterations, please include a per patch changelog 
> to simplify the review.

Sure, will include changelog per-patch basis for future submission.

Thanks!
diff mbox series

Patch

diff --git a/include/linux/rtnetlink.h b/include/linux/rtnetlink.h
index edd840a49989..8468a4ce8510 100644
--- a/include/linux/rtnetlink.h
+++ b/include/linux/rtnetlink.h
@@ -51,6 +51,10 @@  extern atomic_t dev_unreg_count;
 extern struct rw_semaphore pernet_ops_rwsem;
 extern struct rw_semaphore net_rwsem;
 
+#define ASSERT_RTNL() \
+	WARN_ONCE(!rtnl_is_locked(), \
+		  "RTNL: assertion failed at %s (%d)\n", __FILE__,  __LINE__)
+
 #ifdef CONFIG_PROVE_LOCKING
 extern bool lockdep_rtnl_is_held(void);
 #else
@@ -98,6 +102,22 @@  void __rtnl_net_unlock(struct net *net);
 void rtnl_net_lock(struct net *net);
 void rtnl_net_unlock(struct net *net);
 int rtnl_net_lock_cmp_fn(const struct lockdep_map *a, const struct lockdep_map *b);
+
+bool rtnl_net_is_locked(struct net *net);
+
+#define ASSERT_RTNL_NET(net)						\
+	WARN_ONCE(!rtnl_net_is_locked(net),				\
+		  "RTNL_NET: assertion failed at %s (%d)\n",		\
+		  __FILE__,  __LINE__)
+
+bool lockdep_rtnl_net_is_held(struct net *net);
+
+#define rcu_dereference_rtnl_net(net, p)				\
+	rcu_dereference_check(p, lockdep_rtnl_net_is_held(net))
+#define rtnl_net_dereference(net, p)					\
+	rcu_dereference_protected(p, lockdep_rtnl_net_is_held(net))
+#define rcu_replace_pointer_rtnl_net(net, rp, p)			\
+	rcu_replace_pointer(rp, p, lockdep_rtnl_net_is_held(net))
 #else
 static inline void __rtnl_net_lock(struct net *net) {}
 static inline void __rtnl_net_unlock(struct net *net) {}
@@ -111,6 +131,27 @@  static inline void rtnl_net_unlock(struct net *net)
 {
 	rtnl_unlock();
 }
+
+static inline void ASSERT_RTNL_NET(struct net *net)
+{
+	ASSERT_RTNL();
+}
+
+static inline void *rcu_dereference_rtnl_net(struct net *net, void *p)
+{
+	return rcu_dereference_rtnl(p);
+}
+
+static inline void *rtnl_net_dereference(struct net *net, void *p)
+{
+	return rtnl_dereference(p);
+}
+
+static inline void *rcu_replace_pointer_rtnl_net(struct net *net,
+						 void *rp, void *p)
+{
+	return rcu_replace_pointer_rtnl(rp, p);
+}
 #endif
 
 static inline struct netdev_queue *dev_ingress_queue(struct net_device *dev)
@@ -140,10 +181,6 @@  void rtnetlink_init(void);
 void __rtnl_unlock(void);
 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail);
 
-#define ASSERT_RTNL() \
-	WARN_ONCE(!rtnl_is_locked(), \
-		  "RTNL: assertion failed at %s (%d)\n", __FILE__,  __LINE__)
-
 extern int ndo_dflt_fdb_dump(struct sk_buff *skb,
 			     struct netlink_callback *cb,
 			     struct net_device *dev,
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index edf530441b65..2b44ec690780 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -235,6 +235,18 @@  int rtnl_net_lock_cmp_fn(const struct lockdep_map *a, const struct lockdep_map *
 
 	return rtnl_net_cmp_locks(net_a, net_b);
 }
+
+bool rtnl_net_is_locked(struct net *net)
+{
+	return rtnl_is_locked() && mutex_is_locked(&net->rtnl_mutex);
+}
+EXPORT_SYMBOL(rtnl_net_is_locked);
+
+bool lockdep_rtnl_net_is_held(struct net *net)
+{
+	return lockdep_rtnl_is_held() && lockdep_is_held(&net->rtnl_mutex);
+}
+EXPORT_SYMBOL(lockdep_rtnl_net_is_held);
 #endif
 
 static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];