Skip to contentSkip to conversation

fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL.

PATCH Superseded Kuniyuki Iwashima 3 participants 11 messages Feb 6, 2025 – Feb 7, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fib: rules: Convert RTM_NEWRULE and RTM_DELRULE to per-netns RTNL.

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(-)
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH 1/6 fib: rules: Don't check net in rule_exists() and rule_find().

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().

---
 net/core/fib_rules.c | 6 ------
 1 file changed, 6 deletions(-)
net/core/fib_rules.c +0 -6
index e684ba3ebb38..02dfb841ab29 100644
@@ -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;
 
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

PATCH 2/6 fib: rules: Pass net to fib_nl2rule() instead of skb.

In reply to Kuniyuki Iwashima

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().

---
 net/core/fib_rules.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
net/core/fib_rules.c +3 -4
index 02dfb841ab29..87f731199538 100644
@@ -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;
 
Signature · 1 line
2.39.5 (Apple Git-154)
#3 lore raw

PATCH 3/6 fib: rules: Split fib_nl2rule().

In reply to Kuniyuki Iwashima

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().

---
 net/core/fib_rules.c | 58 +++++++++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 17 deletions(-)
net/core/fib_rules.c +41 -17
index 87f731199538..694a8c2884a8 100644
@@ -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;
Signature · 1 line
2.39.5 (Apple Git-154)
#4 lore raw

PATCH 4/6 fib: rules: Convert RTM_NEWRULE to per-netns RTNL.

In reply to Kuniyuki Iwashima

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.

---
 net/core/fib_rules.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
net/core/fib_rules.c +17 -5
index 694a8c2884a8..3cdfa3ac8c7c 100644
@@ -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},
Signature · 1 line
2.39.5 (Apple Git-154)
#5 lore raw

PATCH 5/6 fib: rules: Add error_free label in fib_nl_delrule().

In reply to Kuniyuki Iwashima

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.

---
 net/core/fib_rules.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/core/fib_rules.c +6 -5
index 3cdfa3ac8c7c..cc26c762fa9e 100644
@@ -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;
 }
Signature · 1 line
2.39.5 (Apple Git-154)
#6 lore raw

PATCH 6/6 fib: rules: Convert RTM_DELRULE to per-netns RTNL.

In reply to Kuniyuki Iwashima

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.

---
 net/core/fib_rules.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
net/core/fib_rules.c +19 -10
index cc26c762fa9e..3430d026134d 100644
@@ -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},
 };
Signature · 1 line
2.39.5 (Apple Git-154)
#7 lore raw

Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.

Quoted 22 lines · Kuniyuki Iwashima

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.

---
 net/core/fib_rules.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
net/core/fib_rules.c +0 -0
index cc26c762fa9e..3430d026134d 100644

net/core/fib_rules.c static int call_fib_rule_notifiers(struct net *net, · line 374

Quoted 5 lines
net/core/fib_rules.c +1 -1
@@ -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 ?

net/core/fib_rules.c EXPORT_SYMBOL_GPL(fib_nl_newrule); · line 913

Quoted 10 lines
net/core/fib_rules.c +2 -1
+
        /* 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.

Quoted 69 lines
net/core/fib_rules.c +16 -9
+       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)

#8 lore raw

Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.

From: Eric Dumazet <[email protected]>
Date: Thu, 6 Feb 2025 10:41:12 +0100

Quoted 26 lines · Kuniyuki Iwashima
Quoted 25 lines

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.

---
 net/core/fib_rules.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
net/core/fib_rules.c +0 -0
index cc26c762fa9e..3430d026134d 100644
@@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
                .rule = rule,
        };
 
Quoted 6 lines

net/core/fib_rules.c static int call_fib_rule_notifiers(struct net *net, · line 374

Quoted 2 lines
net/core/fib_rules.c +1 -1
-       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.

Quoted 14 lines

net/core/fib_rules.c EXPORT_SYMBOL_GPL(fib_nl_newrule); · line 913

Quoted 11 lines
net/core/fib_rules.c +2 -1
+
        /* 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.

Quoted 1 line

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

Will add a comment there in v2.

Thanks!

#9 lore raw

Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.

Quoted 35 lines · Kuniyuki Iwashima

From: Eric Dumazet <[email protected]>
Date: Thu, 6 Feb 2025 10:41:12 +0100

Quoted 26 lines · Kuniyuki Iwashima
Quoted 25 lines

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.

---
 net/core/fib_rules.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
net/core/fib_rules.c +0 -0
index cc26c762fa9e..3430d026134d 100644
@@ -371,7 +371,8 @@ static int call_fib_rule_notifiers(struct net *net,
                .rule = rule,
        };
 
Quoted 6 lines

net/core/fib_rules.c static int call_fib_rule_notifiers(struct net *net, · line 374

Quoted 2 lines
net/core/fib_rules.c +1 -1
-       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 ?

Quoted 2 lines

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"?

#10 lore raw

Re: [PATCH v1 net-next 6/6] fib: rules: Convert RTM_DELRULE to per-netns RTNL.

From: Ido Schimmel <[email protected]>
Date: Thu, 6 Feb 2025 13:22:28 +0200

Quoted 36 lines · Kuniyuki Iwashima
Quoted 35 lines

From: Eric Dumazet <[email protected]>
Date: Thu, 6 Feb 2025 10:41:12 +0100

Quoted 32 lines · Kuniyuki Iwashima
Quoted 22 lines

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.

---
 net/core/fib_rules.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
net/core/fib_rules.c +0 -0
index cc26c762fa9e..3430d026134d 100644

net/core/fib_rules.c static int call_fib_rule_notifiers(struct net *net, · line 374

Quoted 5 lines
net/core/fib_rules.c +1 -1
@@ -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 ?

Quoted 8 lines
Quoted 2 lines

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.