netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL.
@ 2025-02-06  8:46 Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 1/6] fib: rules: Don't check net in rule_exists() and rule_find() Kuniyuki Iwashima
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

Patch 1 & 2 are small cleanup, and patch 3 ~ 6 make fib_nl_newrule()
and fib_nl_delrule() hold per-netns RTNL.


Kuniyuki Iwashima (6):
  fib: rules: Don't check net in rule_exists() and rule_find().
  fib: rules: Pass net to fib_nl2rule() instead of skb.
  fib: rules: Split fib_nl2rule().
  fib: rules: Convert RTM_NEWRULE to per-netns RTNL.
  fib: rules: Add error_free label in fib_nl_delrule().
  fib: rules: Convert RTM_DELRULE to per-netns RTNL.

 net/core/fib_rules.c | 131 ++++++++++++++++++++++++++++---------------
 1 file changed, 85 insertions(+), 46 deletions(-)

-- 
2.39.5 (Apple Git-154)


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

* [PATCH v1 net-next 1/6] fib: rules: Don't check net in rule_exists() and rule_find().
  2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
@ 2025-02-06  8:46 ` Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 2/6] fib: rules: Pass net to fib_nl2rule() instead of skb Kuniyuki Iwashima
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

fib_nl_newrule() / fib_nl_delrule() looks up struct fib_rules_ops
in sock_net(skb->sk) and calls rule_exists() / rule_find() respectively.

fib_nl_newrule() creates a new rule and links it to the found ops, so
struct fib_rule never belongs to a different netns's ops->rules_list.

Let's remove redundant netns check in rule_exists() and rule_find().

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/fib_rules.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index e684ba3ebb38..02dfb841ab29 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -459,9 +459,6 @@ static struct fib_rule *rule_find(struct fib_rules_ops *ops,
 		if (rule->tun_id && r->tun_id != rule->tun_id)
 			continue;
 
-		if (r->fr_net != rule->fr_net)
-			continue;
-
 		if (rule->l3mdev && r->l3mdev != rule->l3mdev)
 			continue;
 
@@ -719,9 +716,6 @@ static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
 		if (r->tun_id != rule->tun_id)
 			continue;
 
-		if (r->fr_net != rule->fr_net)
-			continue;
-
 		if (r->l3mdev != rule->l3mdev)
 			continue;
 
-- 
2.39.5 (Apple Git-154)


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

* [PATCH v1 net-next 2/6] fib: rules: Pass net to fib_nl2rule() instead of skb.
  2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 1/6] fib: rules: Don't check net in rule_exists() and rule_find() Kuniyuki Iwashima
@ 2025-02-06  8:46 ` Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 3/6] fib: rules: Split fib_nl2rule() Kuniyuki Iwashima
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

skb is not used in fib_nl2rule() other than sock_net(skb->sk),
which is already available in callers, fib_nl_newrule() and
fib_nl_delrule().

Let's pass net directly to fib_nl2rule().

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/fib_rules.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index 02dfb841ab29..87f731199538 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -512,14 +512,13 @@ static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
 }
 #endif
 
-static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
+static int fib_nl2rule(struct net *net, struct nlmsghdr *nlh,
 		       struct netlink_ext_ack *extack,
 		       struct fib_rules_ops *ops,
 		       struct nlattr *tb[],
 		       struct fib_rule **rule,
 		       bool *user_priority)
 {
-	struct net *net = sock_net(skb->sk);
 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
 	struct fib_rule *nlrule = NULL;
 	int err = -EINVAL;
@@ -798,7 +797,7 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 		goto errout;
 	}
 
-	err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
+	err = fib_nl2rule(net, nlh, extack, ops, tb, &rule, &user_priority);
 	if (err)
 		goto errout;
 
@@ -906,7 +905,7 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 		goto errout;
 	}
 
-	err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
+	err = fib_nl2rule(net, nlh, extack, ops, tb, &nlrule, &user_priority);
 	if (err)
 		goto errout;
 
-- 
2.39.5 (Apple Git-154)


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

* [PATCH v1 net-next 3/6] fib: rules: Split fib_nl2rule().
  2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 1/6] fib: rules: Don't check net in rule_exists() and rule_find() Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 2/6] fib: rules: Pass net to fib_nl2rule() instead of skb Kuniyuki Iwashima
@ 2025-02-06  8:46 ` Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 4/6] fib: rules: Convert RTM_NEWRULE to per-netns RTNL Kuniyuki Iwashima
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

We will move RTNL down to fib_nl_newrule() and fib_nl_delrule().

Some operations in fib_nl2rule() require RTNL: fib_default_rule_pref()
and __dev_get_by_name().

Let's split the RTNL parts as fib_nl2rule_rtnl().

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/fib_rules.c | 58 +++++++++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 17 deletions(-)

diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index 87f731199538..694a8c2884a8 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -550,30 +550,18 @@ static int fib_nl2rule(struct net *net, struct nlmsghdr *nlh,
 	if (tb[FRA_PRIORITY]) {
 		nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
 		*user_priority = true;
-	} else {
-		nlrule->pref = fib_default_rule_pref(ops);
 	}
 
 	nlrule->proto = nla_get_u8_default(tb[FRA_PROTOCOL], RTPROT_UNSPEC);
 
 	if (tb[FRA_IIFNAME]) {
-		struct net_device *dev;
-
 		nlrule->iifindex = -1;
 		nla_strscpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
-		dev = __dev_get_by_name(net, nlrule->iifname);
-		if (dev)
-			nlrule->iifindex = dev->ifindex;
 	}
 
 	if (tb[FRA_OIFNAME]) {
-		struct net_device *dev;
-
 		nlrule->oifindex = -1;
 		nla_strscpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
-		dev = __dev_get_by_name(net, nlrule->oifname);
-		if (dev)
-			nlrule->oifindex = dev->ifindex;
 	}
 
 	if (tb[FRA_FWMARK]) {
@@ -615,11 +603,6 @@ static int fib_nl2rule(struct net *net, struct nlmsghdr *nlh,
 		}
 
 		nlrule->target = nla_get_u32(tb[FRA_GOTO]);
-		/* Backward jumps are prohibited to avoid endless loops */
-		if (nlrule->target <= nlrule->pref) {
-			NL_SET_ERR_MSG(extack, "Backward goto not supported");
-			goto errout_free;
-		}
 	} else if (nlrule->action == FR_ACT_GOTO) {
 		NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
 		goto errout_free;
@@ -679,6 +662,39 @@ static int fib_nl2rule(struct net *net, struct nlmsghdr *nlh,
 	return err;
 }
 
+static int fib_nl2rule_rtnl(struct fib_rule *nlrule,
+			    struct fib_rules_ops *ops,
+			    struct nlattr *tb[],
+			    struct netlink_ext_ack *extack)
+{
+	if (!tb[FRA_PRIORITY])
+		nlrule->pref = fib_default_rule_pref(ops);
+
+	/* Backward jumps are prohibited to avoid endless loops */
+	if (tb[FRA_GOTO] && nlrule->target <= nlrule->pref) {
+		NL_SET_ERR_MSG(extack, "Backward goto not supported");
+		return -EINVAL;
+	}
+
+	if (tb[FRA_IIFNAME]) {
+		struct net_device *dev;
+
+		dev = __dev_get_by_name(nlrule->fr_net, nlrule->iifname);
+		if (dev)
+			nlrule->iifindex = dev->ifindex;
+	}
+
+	if (tb[FRA_OIFNAME]) {
+		struct net_device *dev;
+
+		dev = __dev_get_by_name(nlrule->fr_net, nlrule->oifname);
+		if (dev)
+			nlrule->oifindex = dev->ifindex;
+	}
+
+	return 0;
+}
+
 static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
 		       struct nlattr **tb, struct fib_rule *rule)
 {
@@ -801,6 +817,10 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err)
 		goto errout;
 
+	err = fib_nl2rule_rtnl(rule, ops, tb, extack);
+	if (err)
+		goto errout_free;
+
 	if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
 	    rule_exists(ops, frh, tb, rule)) {
 		err = -EEXIST;
@@ -909,6 +929,10 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err)
 		goto errout;
 
+	err = fib_nl2rule_rtnl(nlrule, ops, tb, extack);
+	if (err)
+		goto errout;
+
 	rule = rule_find(ops, frh, tb, nlrule, user_priority);
 	if (!rule) {
 		err = -ENOENT;
-- 
2.39.5 (Apple Git-154)


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

* [PATCH v1 net-next 4/6] fib: rules: Convert RTM_NEWRULE to per-netns RTNL.
  2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
                   ` (2 preceding siblings ...)
  2025-02-06  8:46 ` [PATCH v1 net-next 3/6] fib: rules: Split fib_nl2rule() Kuniyuki Iwashima
@ 2025-02-06  8:46 ` Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 5/6] fib: rules: Add error_free label in fib_nl_delrule() Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
  5 siblings, 0 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

fib_nl_newrule() is the doit() handler for RTM_NEWRULE but also called
from vrf_newlink().

In the latter case, RTNL is already held and the 3rd arg extack is NULL.

Let's hold per-netns RTNL in fib_nl_newrule() if extack is NULL.

Note that we call fib_rule_get() before releasing per-netns RTNL to call
notify_rule_change() without RTNL and prevent freeing the new rule.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/fib_rules.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index 694a8c2884a8..3cdfa3ac8c7c 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -786,13 +786,13 @@ static const struct nla_policy fib_rule_policy[FRA_MAX + 1] = {
 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 		   struct netlink_ext_ack *extack)
 {
-	struct net *net = sock_net(skb->sk);
+	bool user_priority = false, hold_rtnl = !!extack;
 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
+	struct fib_rule *rule, *r, *last = NULL;
+	struct net *net = sock_net(skb->sk);
+	int err = -EINVAL, unresolved = 0;
 	struct fib_rules_ops *ops = NULL;
-	struct fib_rule *rule = NULL, *r, *last = NULL;
 	struct nlattr *tb[FRA_MAX + 1];
-	int err = -EINVAL, unresolved = 0;
-	bool user_priority = false;
 
 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
 		NL_SET_ERR_MSG(extack, "Invalid msg length");
@@ -817,6 +817,9 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err)
 		goto errout;
 
+	if (hold_rtnl)
+		rtnl_net_lock(net);
+
 	err = fib_nl2rule_rtnl(rule, ops, tb, extack);
 	if (err)
 		goto errout_free;
@@ -882,12 +885,20 @@ int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (rule->tun_id)
 		ip_tunnel_need_metadata();
 
+	fib_rule_get(rule);
+
+	if (hold_rtnl)
+		rtnl_net_unlock(net);
+
 	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
+	fib_rule_put(rule);
 	flush_route_cache(ops);
 	rules_ops_put(ops);
 	return 0;
 
 errout_free:
+	if (hold_rtnl)
+		rtnl_net_unlock(net);
 	kfree(rule);
 errout:
 	rules_ops_put(ops);
@@ -1310,7 +1321,8 @@ static struct pernet_operations fib_rules_net_ops = {
 };
 
 static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
-	{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule},
+	{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule,
+	 .flags = RTNL_FLAG_DOIT_PERNET},
 	{.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
 	{.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
 	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
-- 
2.39.5 (Apple Git-154)


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

* [PATCH v1 net-next 5/6] fib: rules: Add error_free label in fib_nl_delrule().
  2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
                   ` (3 preceding siblings ...)
  2025-02-06  8:46 ` [PATCH v1 net-next 4/6] fib: rules: Convert RTM_NEWRULE to per-netns RTNL Kuniyuki Iwashima
@ 2025-02-06  8:46 ` Kuniyuki Iwashima
  2025-02-06  8:46 ` [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
  5 siblings, 0 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

We will hold RTNL just before calling fib_nl2rule_rtnl() in
fib_nl_delrule() and release it before kfree(nlrule).

Let's add a new rule to make the following change cleaner.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/fib_rules.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index 3cdfa3ac8c7c..cc26c762fa9e 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -942,23 +942,23 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	err = fib_nl2rule_rtnl(nlrule, ops, tb, extack);
 	if (err)
-		goto errout;
+		goto errout_free;
 
 	rule = rule_find(ops, frh, tb, nlrule, user_priority);
 	if (!rule) {
 		err = -ENOENT;
-		goto errout;
+		goto errout_free;
 	}
 
 	if (rule->flags & FIB_RULE_PERMANENT) {
 		err = -EPERM;
-		goto errout;
+		goto errout_free;
 	}
 
 	if (ops->delete) {
 		err = ops->delete(rule);
 		if (err)
-			goto errout;
+			goto errout_free;
 	}
 
 	if (rule->tun_id)
@@ -1004,8 +1004,9 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	kfree(nlrule);
 	return 0;
 
-errout:
+errout_free:
 	kfree(nlrule);
+errout:
 	rules_ops_put(ops);
 	return err;
 }
-- 
2.39.5 (Apple Git-154)


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

* [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.
  2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
                   ` (4 preceding siblings ...)
  2025-02-06  8:46 ` [PATCH v1 net-next 5/6] fib: rules: Add error_free label in fib_nl_delrule() Kuniyuki Iwashima
@ 2025-02-06  8:46 ` Kuniyuki Iwashima
  2025-02-06  9:41   ` Eric Dumazet
  5 siblings, 1 reply; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  8:46 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

fib_nl_delrule() is the doit() handler for RTM_DELRULE but also called
1;95;0cfrom vrf_newlink() in case something fails in vrf_add_fib_rules().

In the latter case, RTNL is already held and the 3rd arg extack is NULL.

Let's hold per-netns RTNL in fib_nl_delrule() if extack is NULL.

Now we can place ASSERT_RTNL_NET() in call_fib_rule_notifiers().

While at it, fib_rule r is moved to the suitable scope.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/core/fib_rules.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index cc26c762fa9e..3430d026134d 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
 		.rule = rule,
 	};
 
-	ASSERT_RTNL();
+	ASSERT_RTNL_NET(net);
+
 	/* Paired with READ_ONCE() in fib_rules_seq() */
 	WRITE_ONCE(ops->fib_rules_seq, ops->fib_rules_seq + 1);
 	return call_fib_notifiers(net, event_type, &info.info);
@@ -909,13 +910,13 @@ EXPORT_SYMBOL_GPL(fib_nl_newrule);
 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 		   struct netlink_ext_ack *extack)
 {
-	struct net *net = sock_net(skb->sk);
+	bool user_priority = false, hold_rtnl = !!extack;
+	struct fib_rule *rule = NULL, *nlrule = NULL;
 	struct fib_rule_hdr *frh = nlmsg_data(nlh);
+	struct net *net = sock_net(skb->sk);
 	struct fib_rules_ops *ops = NULL;
-	struct fib_rule *rule = NULL, *r, *nlrule = NULL;
 	struct nlattr *tb[FRA_MAX+1];
 	int err = -EINVAL;
-	bool user_priority = false;
 
 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
 		NL_SET_ERR_MSG(extack, "Invalid msg length");
@@ -940,6 +941,9 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (err)
 		goto errout;
 
+	if (hold_rtnl)
+		rtnl_net_lock(net);
+
 	err = fib_nl2rule_rtnl(nlrule, ops, tb, extack);
 	if (err)
 		goto errout_free;
@@ -980,7 +984,7 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	 * current if it is goto rule, have actually been added.
 	 */
 	if (ops->nr_goto_rules > 0) {
-		struct fib_rule *n;
+		struct fib_rule *n, *r;
 
 		n = list_next_entry(rule, list);
 		if (&n->list == &ops->rules_list || n->pref != rule->pref)
@@ -994,10 +998,12 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 		}
 	}
 
-	call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
-				NULL);
-	notify_rule_change(RTM_DELRULE, rule, ops, nlh,
-			   NETLINK_CB(skb).portid);
+	call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops, NULL);
+
+	if (hold_rtnl)
+		rtnl_net_unlock(net);
+
+	notify_rule_change(RTM_DELRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
 	fib_rule_put(rule);
 	flush_route_cache(ops);
 	rules_ops_put(ops);
@@ -1005,6 +1011,8 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
 	return 0;
 
 errout_free:
+	if (hold_rtnl)
+		rtnl_net_unlock(net);
 	kfree(nlrule);
 errout:
 	rules_ops_put(ops);
@@ -1324,7 +1332,8 @@ static struct pernet_operations fib_rules_net_ops = {
 static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
 	{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule,
 	 .flags = RTNL_FLAG_DOIT_PERNET},
-	{.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
+	{.msgtype = RTM_DELRULE, .doit = fib_nl_delrule,
+	 .flags = RTNL_FLAG_DOIT_PERNET},
 	{.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
 	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
 };
-- 
2.39.5 (Apple Git-154)


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

* Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.
  2025-02-06  8:46 ` [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
@ 2025-02-06  9:41   ` Eric Dumazet
  2025-02-06  9:52     ` Kuniyuki Iwashima
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Dumazet @ 2025-02-06  9:41 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Kuniyuki Iwashima, netdev

On Thu, Feb 6, 2025 at 9:49 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> fib_nl_delrule() is the doit() handler for RTM_DELRULE but also called
> 1;95;0cfrom vrf_newlink() in case something fails in vrf_add_fib_rules().
>
> In the latter case, RTNL is already held and the 3rd arg extack is NULL.
>
> Let's hold per-netns RTNL in fib_nl_delrule() if extack is NULL.
>
> Now we can place ASSERT_RTNL_NET() in call_fib_rule_notifiers().
>
> While at it, fib_rule r is moved to the suitable scope.
>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
>  net/core/fib_rules.c | 29 +++++++++++++++++++----------
>  1 file changed, 19 insertions(+), 10 deletions(-)
>
> diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
> index cc26c762fa9e..3430d026134d 100644
> --- a/net/core/fib_rules.c
> +++ b/net/core/fib_rules.c
> @@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
>                 .rule = rule,
>         };
>
> -       ASSERT_RTNL();
> +       ASSERT_RTNL_NET(net);

This warning will then fire in the vrf case, because vrf_fib_rule() is
only holding the real RTNL,
but not yet the net->rtnl_mutex ?

> +
>         /* Paired with READ_ONCE() in fib_rules_seq() */
>         WRITE_ONCE(ops->fib_rules_seq, ops->fib_rules_seq + 1);
>         return call_fib_notifiers(net, event_type, &info.info);
> @@ -909,13 +910,13 @@ EXPORT_SYMBOL_GPL(fib_nl_newrule);
>  int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
>                    struct netlink_ext_ack *extack)
>  {
> -       struct net *net = sock_net(skb->sk);
> +       bool user_priority = false, hold_rtnl = !!extack;

I am not pleased with this heuristic hidden here.

At the very least a fat comment in drivers/net/vrf.c would be welcomed.


> +       struct fib_rule *rule = NULL, *nlrule = NULL;
>         struct fib_rule_hdr *frh = nlmsg_data(nlh);
> +       struct net *net = sock_net(skb->sk);
>         struct fib_rules_ops *ops = NULL;
> -       struct fib_rule *rule = NULL, *r, *nlrule = NULL;
>         struct nlattr *tb[FRA_MAX+1];
>         int err = -EINVAL;
> -       bool user_priority = false;
>
>         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
>                 NL_SET_ERR_MSG(extack, "Invalid msg length");
> @@ -940,6 +941,9 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
>         if (err)
>                 goto errout;
>
> +       if (hold_rtnl)
> +               rtnl_net_lock(net);
> +
>         err = fib_nl2rule_rtnl(nlrule, ops, tb, extack);
>         if (err)
>                 goto errout_free;
> @@ -980,7 +984,7 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
>          * current if it is goto rule, have actually been added.
>          */
>         if (ops->nr_goto_rules > 0) {
> -               struct fib_rule *n;
> +               struct fib_rule *n, *r;
>
>                 n = list_next_entry(rule, list);
>                 if (&n->list == &ops->rules_list || n->pref != rule->pref)
> @@ -994,10 +998,12 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
>                 }
>         }
>
> -       call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
> -                               NULL);
> -       notify_rule_change(RTM_DELRULE, rule, ops, nlh,
> -                          NETLINK_CB(skb).portid);
> +       call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops, NULL);
> +
> +       if (hold_rtnl)
> +               rtnl_net_unlock(net);
> +
> +       notify_rule_change(RTM_DELRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
>         fib_rule_put(rule);
>         flush_route_cache(ops);
>         rules_ops_put(ops);
> @@ -1005,6 +1011,8 @@ int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
>         return 0;
>
>  errout_free:
> +       if (hold_rtnl)
> +               rtnl_net_unlock(net);
>         kfree(nlrule);
>  errout:
>         rules_ops_put(ops);
> @@ -1324,7 +1332,8 @@ static struct pernet_operations fib_rules_net_ops = {
>  static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
>         {.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule,
>          .flags = RTNL_FLAG_DOIT_PERNET},
> -       {.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
> +       {.msgtype = RTM_DELRULE, .doit = fib_nl_delrule,
> +        .flags = RTNL_FLAG_DOIT_PERNET},
>         {.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
>          .flags = RTNL_FLAG_DUMP_UNLOCKED},
>  };
> --
> 2.39.5 (Apple Git-154)
>

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

* Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.
  2025-02-06  9:41   ` Eric Dumazet
@ 2025-02-06  9:52     ` Kuniyuki Iwashima
  2025-02-06 11:22       ` Ido Schimmel
  0 siblings, 1 reply; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-06  9:52 UTC (permalink / raw)
  To: edumazet; +Cc: davem, horms, kuba, kuni1840, kuniyu, netdev, pabeni

From: Eric Dumazet <edumazet@google.com>
Date: Thu, 6 Feb 2025 10:41:12 +0100
> On Thu, Feb 6, 2025 at 9:49 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > fib_nl_delrule() is the doit() handler for RTM_DELRULE but also called
> > 1;95;0cfrom vrf_newlink() in case something fails in vrf_add_fib_rules().
> >
> > In the latter case, RTNL is already held and the 3rd arg extack is NULL.
> >
> > Let's hold per-netns RTNL in fib_nl_delrule() if extack is NULL.
> >
> > Now we can place ASSERT_RTNL_NET() in call_fib_rule_notifiers().
> >
> > While at it, fib_rule r is moved to the suitable scope.
> >
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > ---
> >  net/core/fib_rules.c | 29 +++++++++++++++++++----------
> >  1 file changed, 19 insertions(+), 10 deletions(-)
> >
> > diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
> > index cc26c762fa9e..3430d026134d 100644
> > --- a/net/core/fib_rules.c
> > +++ b/net/core/fib_rules.c
> > @@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
> >                 .rule = rule,
> >         };
> >
> > -       ASSERT_RTNL();
> > +       ASSERT_RTNL_NET(net);
> 
> This warning will then fire in the vrf case, because vrf_fib_rule() is
> only holding the real RTNL,
> but not yet the net->rtnl_mutex ?

As it's RTM_NEWLINK, dev_net(net)'s per-netns RTNL is held here and
vrf_fib_rule() sets skb->sk = dev_net(dev)->rtnl, so I think it won't fire.


> 
> > +
> >         /* Paired with READ_ONCE() in fib_rules_seq() */
> >         WRITE_ONCE(ops->fib_rules_seq, ops->fib_rules_seq + 1);
> >         return call_fib_notifiers(net, event_type, &info.info);
> > @@ -909,13 +910,13 @@ EXPORT_SYMBOL_GPL(fib_nl_newrule);
> >  int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
> >                    struct netlink_ext_ack *extack)
> >  {
> > -       struct net *net = sock_net(skb->sk);
> > +       bool user_priority = false, hold_rtnl = !!extack;
> 
> I am not pleased with this heuristic hidden here.
> 
> At the very least a fat comment in drivers/net/vrf.c would be welcomed.

Will add a comment there in v2.

Thanks!

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

* Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.
  2025-02-06  9:52     ` Kuniyuki Iwashima
@ 2025-02-06 11:22       ` Ido Schimmel
  2025-02-07  4:12         ` Kuniyuki Iwashima
  0 siblings, 1 reply; 11+ messages in thread
From: Ido Schimmel @ 2025-02-06 11:22 UTC (permalink / raw)
  To: Kuniyuki Iwashima; +Cc: edumazet, davem, horms, kuba, kuni1840, netdev, pabeni

On Thu, Feb 06, 2025 at 06:52:21PM +0900, Kuniyuki Iwashima wrote:
> From: Eric Dumazet <edumazet@google.com>
> Date: Thu, 6 Feb 2025 10:41:12 +0100
> > On Thu, Feb 6, 2025 at 9:49 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > >
> > > fib_nl_delrule() is the doit() handler for RTM_DELRULE but also called
> > > 1;95;0cfrom vrf_newlink() in case something fails in vrf_add_fib_rules().
> > >
> > > In the latter case, RTNL is already held and the 3rd arg extack is NULL.
> > >
> > > Let's hold per-netns RTNL in fib_nl_delrule() if extack is NULL.
> > >
> > > Now we can place ASSERT_RTNL_NET() in call_fib_rule_notifiers().
> > >
> > > While at it, fib_rule r is moved to the suitable scope.
> > >
> > > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > > ---
> > >  net/core/fib_rules.c | 29 +++++++++++++++++++----------
> > >  1 file changed, 19 insertions(+), 10 deletions(-)
> > >
> > > diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
> > > index cc26c762fa9e..3430d026134d 100644
> > > --- a/net/core/fib_rules.c
> > > +++ b/net/core/fib_rules.c
> > > @@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
> > >                 .rule = rule,
> > >         };
> > >
> > > -       ASSERT_RTNL();
> > > +       ASSERT_RTNL_NET(net);
> > 
> > This warning will then fire in the vrf case, because vrf_fib_rule() is
> > only holding the real RTNL,
> > but not yet the net->rtnl_mutex ?
> 
> As it's RTM_NEWLINK, dev_net(net)'s per-netns RTNL is held here and
> vrf_fib_rule() sets skb->sk = dev_net(dev)->rtnl, so I think it won't fire.

Yes, I believe you're correct. I ran fib_rule_tests.sh with a debug
config and CONFIG_DEBUG_NET_SMALL_RTNL=y and didn't see any splats.

BTW, did you consider adding this config option to
kernel/configs/debug.config under "Networking Debugging"?

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

* Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.
  2025-02-06 11:22       ` Ido Schimmel
@ 2025-02-07  4:12         ` Kuniyuki Iwashima
  0 siblings, 0 replies; 11+ messages in thread
From: Kuniyuki Iwashima @ 2025-02-07  4:12 UTC (permalink / raw)
  To: idosch; +Cc: davem, edumazet, horms, kuba, kuni1840, kuniyu, netdev, pabeni

From: Ido Schimmel <idosch@idosch.org>
Date: Thu, 6 Feb 2025 13:22:28 +0200
> On Thu, Feb 06, 2025 at 06:52:21PM +0900, Kuniyuki Iwashima wrote:
> > From: Eric Dumazet <edumazet@google.com>
> > Date: Thu, 6 Feb 2025 10:41:12 +0100
> > > On Thu, Feb 6, 2025 at 9:49 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > > >
> > > > fib_nl_delrule() is the doit() handler for RTM_DELRULE but also called
> > > > 1;95;0cfrom vrf_newlink() in case something fails in vrf_add_fib_rules().
> > > >
> > > > In the latter case, RTNL is already held and the 3rd arg extack is NULL.
> > > >
> > > > Let's hold per-netns RTNL in fib_nl_delrule() if extack is NULL.
> > > >
> > > > Now we can place ASSERT_RTNL_NET() in call_fib_rule_notifiers().
> > > >
> > > > While at it, fib_rule r is moved to the suitable scope.
> > > >
> > > > Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> > > > ---
> > > >  net/core/fib_rules.c | 29 +++++++++++++++++++----------
> > > >  1 file changed, 19 insertions(+), 10 deletions(-)
> > > >
> > > > diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
> > > > index cc26c762fa9e..3430d026134d 100644
> > > > --- a/net/core/fib_rules.c
> > > > +++ b/net/core/fib_rules.c
> > > > @@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
> > > >                 .rule = rule,
> > > >         };
> > > >
> > > > -       ASSERT_RTNL();
> > > > +       ASSERT_RTNL_NET(net);
> > > 
> > > This warning will then fire in the vrf case, because vrf_fib_rule() is
> > > only holding the real RTNL,
> > > but not yet the net->rtnl_mutex ?
> > 
> > As it's RTM_NEWLINK, dev_net(net)'s per-netns RTNL is held here and
> > vrf_fib_rule() sets skb->sk = dev_net(dev)->rtnl, so I think it won't fire.
> 
> Yes, I believe you're correct. I ran fib_rule_tests.sh with a debug
> config and CONFIG_DEBUG_NET_SMALL_RTNL=y and didn't see any splats.
> 
> BTW, did you consider adding this config option to
> kernel/configs/debug.config under "Networking Debugging"?

I haven't because CONFIG_DEBUG_NET_SMALL_RTNL is not strictly a
debugging config and will not help debugging for real issues like
other DEBUG_NET configs, but I don't have strong preference.

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

end of thread, other threads:[~2025-02-07  4:12 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-06  8:46 [PATCH v1 net-next 0/6] fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
2025-02-06  8:46 ` [PATCH v1 net-next 1/6] fib: rules: Don't check net in rule_exists() and rule_find() Kuniyuki Iwashima
2025-02-06  8:46 ` [PATCH v1 net-next 2/6] fib: rules: Pass net to fib_nl2rule() instead of skb Kuniyuki Iwashima
2025-02-06  8:46 ` [PATCH v1 net-next 3/6] fib: rules: Split fib_nl2rule() Kuniyuki Iwashima
2025-02-06  8:46 ` [PATCH v1 net-next 4/6] fib: rules: Convert RTM_NEWRULE to per-netns RTNL Kuniyuki Iwashima
2025-02-06  8:46 ` [PATCH v1 net-next 5/6] fib: rules: Add error_free label in fib_nl_delrule() Kuniyuki Iwashima
2025-02-06  8:46 ` [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL Kuniyuki Iwashima
2025-02-06  9:41   ` Eric Dumazet
2025-02-06  9:52     ` Kuniyuki Iwashima
2025-02-06 11:22       ` Ido Schimmel
2025-02-07  4:12         ` Kuniyuki Iwashima

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).