ipv6: use READ_ONCE()/WRITE_ONCE() on fib6_table->fib_seq
authorEric Dumazet <edumazet@google.com>
Wed, 9 Oct 2024 18:44:03 +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 fib6_tables_seq_read() and
fib6_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-4-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/ip6_fib.h
net/ipv6/fib6_rules.c
net/ipv6/ip6_fib.c

index 6cb867ce4878423fbb9049e69445a6dbf8f31ba7..7c87873ae211c5fa80d34e8f3b8df0e813976390 100644 (file)
@@ -394,7 +394,7 @@ struct fib6_table {
        struct fib6_node        tb6_root;
        struct inet_peer_base   tb6_peers;
        unsigned int            flags;
-       unsigned int            fib_seq;
+       unsigned int            fib_seq; /* writes protected by rtnl_mutex */
        struct hlist_head       tb6_gc_hlist;   /* GC candidates */
 #define RT6_TABLE_HAS_DFLT_ROUTER      BIT(0)
 };
@@ -563,7 +563,7 @@ int call_fib6_notifiers(struct net *net, enum fib_event_type event_type,
 int __net_init fib6_notifier_init(struct net *net);
 void __net_exit fib6_notifier_exit(struct net *net);
 
-unsigned int fib6_tables_seq_read(struct net *net);
+unsigned int fib6_tables_seq_read(const struct net *net);
 int fib6_tables_dump(struct net *net, struct notifier_block *nb,
                     struct netlink_ext_ack *extack);
 
@@ -632,7 +632,7 @@ void fib6_rules_cleanup(void);
 bool fib6_rule_default(const struct fib_rule *rule);
 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
                    struct netlink_ext_ack *extack);
-unsigned int fib6_rules_seq_read(struct net *net);
+unsigned int fib6_rules_seq_read(const struct net *net);
 
 static inline bool fib6_rules_early_flow_dissect(struct net *net,
                                                 struct sk_buff *skb,
@@ -676,7 +676,7 @@ static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb,
 {
        return 0;
 }
-static inline unsigned int fib6_rules_seq_read(struct net *net)
+static inline unsigned int fib6_rules_seq_read(const struct net *net)
 {
        return 0;
 }
index 04a9ed5e8310f23cb7d947b732be5dd19916bf39..c85c1627cb16ed0bdfe4c6026bb0132cdd7be6b7 100644 (file)
@@ -56,7 +56,7 @@ int fib6_rules_dump(struct net *net, struct notifier_block *nb,
        return fib_rules_dump(net, nb, AF_INET6, extack);
 }
 
-unsigned int fib6_rules_seq_read(struct net *net)
+unsigned int fib6_rules_seq_read(const struct net *net)
 {
        return fib_rules_seq_read(net, AF_INET6);
 }
index eb111d20615c6274647eeb413d0b9475aaa3ae6c..cea160b249d2d75d03c867d2298da76eb0c7114e 100644 (file)
@@ -345,17 +345,17 @@ static void __net_init fib6_tables_init(struct net *net)
 
 #endif
 
-unsigned int fib6_tables_seq_read(struct net *net)
+unsigned int fib6_tables_seq_read(const struct net *net)
 {
        unsigned int h, fib_seq = 0;
 
        rcu_read_lock();
        for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
-               struct hlist_head *head = &net->ipv6.fib_table_hash[h];
-               struct fib6_table *tb;
+               const struct hlist_head *head = &net->ipv6.fib_table_hash[h];
+               const struct fib6_table *tb;
 
                hlist_for_each_entry_rcu(tb, head, tb6_hlist)
-                       fib_seq += tb->fib_seq;
+                       fib_seq += READ_ONCE(tb->fib_seq);
        }
        rcu_read_unlock();
 
@@ -400,7 +400,7 @@ int call_fib6_entry_notifiers(struct net *net,
                .rt = rt,
        };
 
-       rt->fib6_table->fib_seq++;
+       WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);
        return call_fib6_notifiers(net, event_type, &info.info);
 }
 
@@ -416,7 +416,7 @@ int call_fib6_multipath_entry_notifiers(struct net *net,
                .nsiblings = nsiblings,
        };
 
-       rt->fib6_table->fib_seq++;
+       WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);
        return call_fib6_notifiers(net, event_type, &info.info);
 }
 
@@ -427,7 +427,7 @@ int call_fib6_entry_notifiers_replace(struct net *net, struct fib6_info *rt)
                .nsiblings = rt->fib6_nsiblings,
        };
 
-       rt->fib6_table->fib_seq++;
+       WRITE_ONCE(rt->fib6_table->fib_seq, rt->fib6_table->fib_seq + 1);
        return call_fib6_notifiers(net, FIB_EVENT_ENTRY_REPLACE, &info.info);
 }