ipv4: use READ_ONCE()/WRITE_ONCE() on net->ipv4.fib_seq
authorEric Dumazet <edumazet@google.com>
Wed, 9 Oct 2024 18:44:02 +0000 (18:44 +0000)
committerJakub Kicinski <kuba@kernel.org>
Fri, 11 Oct 2024 22:35:05 +0000 (15:35 -0700)
Using RTNL to protect ops->fib_rules_seq reads seems a big hammer.

Writes are protected by RTNL.
We can use READ_ONCE() when reading it.

Constify 'struct net' argument of fib4_rules_seq_read()

Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Link: https://patch.msgid.link/20241009184405.3752829-3-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/ip_fib.h
include/net/netns/ipv4.h
net/ipv4/fib_notifier.c
net/ipv4/fib_rules.c

index 06130933542dbe3fb6e1399c70e75b354ed7945b..b6e44f4eaa4c7c46a4103ebd668d192a73fd8e8a 100644 (file)
@@ -347,7 +347,7 @@ static inline int fib4_rules_dump(struct net *net, struct notifier_block *nb,
        return 0;
 }
 
-static inline unsigned int fib4_rules_seq_read(struct net *net)
+static inline unsigned int fib4_rules_seq_read(const struct net *net)
 {
        return 0;
 }
@@ -411,7 +411,7 @@ static inline bool fib4_has_custom_rules(const struct net *net)
 bool fib4_rule_default(const struct fib_rule *rule);
 int fib4_rules_dump(struct net *net, struct notifier_block *nb,
                    struct netlink_ext_ack *extack);
-unsigned int fib4_rules_seq_read(struct net *net);
+unsigned int fib4_rules_seq_read(const struct net *net);
 
 static inline bool fib4_rules_early_flow_dissect(struct net *net,
                                                 struct sk_buff *skb,
index 3b1de80b5c25324c1e43540ebe0f2bcafc4859c6..3c014170e0012818db36d4a7a327025e3fa00dd1 100644 (file)
@@ -262,7 +262,7 @@ struct netns_ipv4 {
 #endif
 
        struct fib_notifier_ops *notifier_ops;
-       unsigned int    fib_seq;        /* protected by rtnl_mutex */
+       unsigned int    fib_seq;        /* writes protected by rtnl_mutex */
 
        struct fib_notifier_ops *ipmr_notifier_ops;
        unsigned int    ipmr_seq;       /* protected by rtnl_mutex */
index 0e23ade74493ce10a3f2572c39e091f132684884..21c85c80de641112d66a28645b1fb17c7071863f 100644 (file)
@@ -22,15 +22,15 @@ int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
        ASSERT_RTNL();
 
        info->family = AF_INET;
-       net->ipv4.fib_seq++;
+       /* Paired with READ_ONCE() in fib4_seq_read() */
+       WRITE_ONCE(net->ipv4.fib_seq, net->ipv4.fib_seq + 1);
        return call_fib_notifiers(net, event_type, info);
 }
 
 static unsigned int fib4_seq_read(struct net *net)
 {
-       ASSERT_RTNL();
-
-       return net->ipv4.fib_seq + fib4_rules_seq_read(net);
+       /* Paired with WRITE_ONCE() in call_fib4_notifiers() */
+       return READ_ONCE(net->ipv4.fib_seq) + fib4_rules_seq_read(net);
 }
 
 static int fib4_dump(struct net *net, struct notifier_block *nb,
index b07292d50ee76603f983c81a55d45abca89266e1..8325224ef07232d05b59c58011625daae847af30 100644 (file)
@@ -74,7 +74,7 @@ int fib4_rules_dump(struct net *net, struct notifier_block *nb,
        return fib_rules_dump(net, nb, AF_INET, extack);
 }
 
-unsigned int fib4_rules_seq_read(struct net *net)
+unsigned int fib4_rules_seq_read(const struct net *net)
 {
        return fib_rules_seq_read(net, AF_INET);
 }