ipv6: move superseded pcpu dsts to the uncached list
Subsystems (4)
cover ipv6: move superseded pcpu dsts to the uncached list
Hi,
When rt6_get_pcpu_route finds that the per-CPU dst of a route using a nexthop object is stale, it calls dst_dev_put on it, so the packets of holders that have not checked it again yet are dropped. IPv4 fixed the same problem by putting such routes on the uncached list in commits 5018c59607a5 ("ipv4: fix race condition between route lookup and invalidation") and ae89f2724db1 ("ipv4: route: don't blackhole in-use routes when retiring fnhe routes").
In IPv6, membership in the uncached list also tells the callers of RT6_LOOKUP_F_DST_NOREF lookups whether they own a reference, so patch 1 bases the ownership on the flag alone, as Ido suggested [1]. Patch 2, from David's series [2], then puts the stale dst on the uncached list and drops the slot's reference after a grace period.
Both are needed by the respin of "ipv6: tie dst validation to the lookup that returned the dst" [3], which replaces the per-CPU dsts of all routes this way.
[1] https://lore.kernel.org/netdev/20261202161942.GA2800642@shredder/
[2] https://lore.kernel.org/netdev/[email protected]/
[3] https://lore.kernel.org/netdev/[email protected]/
Thanks,
Ralf Lici
Mandelbit Srl
---
Changes to patch 2 since David's v3 [2]:
- Drop the slot's reference after a grace period.
- Drop the comment and the Fixes tag. (Ido)
David Ahern (1):
ipv6: keep a superseded pcpu dst working until its holders notice
Ralf Lici (1):
ipv6: base dst ownership on RT6_LOOKUP_F_DST_NOREF only
include/net/ip6_route.h | 7 ++----- net/ipv6/route.c | 36 +++++++++++++++++++++++++----------- net/ipv6/seg6_local.c | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-)
base-commit: 8a3d0368f58e34e34d402718c7655811902a6bd4
Signature · 1 line
2.56.1
PATCH 1/2 ipv6: base dst ownership on RT6_LOOKUP_F_DST_NOREF only
A lookup with RT6_LOOKUP_F_DST_NOREF returns a dst without a reference, except for the uncached RTF_CACHE clone of the FLOWI_FLAG_KNOWN_NH path, whose allocation reference goes to the caller. Callers tell the two apart by the dst's membership in the uncached list:ip6_route_output_flags, ip6_rt_put_flags and seg6_lookup_any_nexthop.
That membership cannot mark ownership once a dst returned without a reference can be moved to the uncached list afterwards, as a replaced per-CPU dst still in use should be. The caller would then assume a reference it does not have.
Base ownership on the flag only. A RT6_LOOKUP_F_DST_NOREF caller never owns a reference, and the allocation reference of an uncached clone is dropped after a grace period instead, so that the dst stays valid for the caller's RCU section.
The deferred release reuses dst->rcu_head. The dst only queues it when its last reference is dropped, which cannot happen while the deferred reference is still held, and by the time the callback drops that reference the head is no longer queued.
This is needed to put replaced per-CPU dsts on the uncached list, which the next patch does for nexthop objects and a later change to dst validation does for all routes.
--- include/net/ip6_route.h | 7 ++----- net/ipv6/route.c | 32 +++++++++++++++++++++++--------- net/ipv6/seg6_local.c | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-)
include/net/ip6_route.h +2 -5
@@ -100,13 +100,10 @@ static inline struct dst_entry *ip6_route_output(struct net *net,return ip6_route_output_flags (net, sk, fl6,0 ); } -/* Only conditionally release dst if flags indicates -* !RT6_LOOKUP_F_DST_NOREF or dst is uncached. -*/ +/* Release the dst unless it was looked up with RT6_LOOKUP_F_DST_NOREF */ static inline void ip6_rt_put_flags (struct rt6_info *rt,int flags) { -if (!(flags &RT6_LOOKUP_F_DST_NOREF ) || - rt->dst.rt_uncached_list) +if (!(flags &RT6_LOOKUP_F_DST_NOREF ))ip6_rt_put (rt); }
net/ipv6/route.c +23 -9
@@ -147,6 +147,21 @@ void rt6_uncached_list_add(struct rt6_info *rt)spin_unlock_bh (&ul->lock); } +static void rt6_dst_release_rcu (struct rcu_head *head) +{ + struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head); + +dst_release (dst); +} + +/* Drop a reference after a grace period, so that RCU readers that found the +* dst without one can still use it and take their own. +*/ +static void rt6_dst_release_deferred (struct rt6_info *rt) +{ +call_rcu (&rt->dst.rcu_head, rt6_dst_release_rcu); +} +void rt6_uncached_list_del (struct rt6_info *rt) {struct uncached_list *ul = rt->dst.rt_uncached_list;
@@ -2310,12 +2325,14 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, rt =ip6_rt_cache_alloc (&res, &fl6->daddr,NULL );if (rt) { -/* 1 refcnt is taken during ip6_rt_cache_alloc(). -* As rt6_uncached_list_add() does not consume refcnt, -* this refcnt is always returned to the caller even -* if caller sets RT6_LOOKUP_F_DST_NOREF flag. -*/ rt6_uncached_list_add (rt); +/* A RT6_LOOKUP_F_DST_NOREF caller does not inherit the +* reference ip6_rt_cache_alloc returned. Dropping it +* after a grace period keeps the dst alive for the +* caller's RCU section. +*/ +if (flags &RT6_LOOKUP_F_DST_NOREF ) +rt6_dst_release_deferred (rt);rcu_read_unlock ();return rt;
@@ -2715,13 +2732,10 @@ struct dst_entry *ip6_route_output_flags(struct net *net, int flags) {struct dst_entry *dst; -struct rt6_info *rt6;rcu_read_lock (); dst =ip6_route_output_flags_noref (net, sk, fl6, flags); - rt6 =dst_rt6_info (dst); -/* For an uncached dst, refcnt is already taken. */ -if (!rt6->dst.rt_uncached_list && !dst_hold_safe (dst)) { +if (!dst_hold_safe (dst)) { dst = &net->ipv6.ip6_null_entry->dst;dst_hold (dst); }
net/ipv6/seg6_local.c +1 -1
@@ -404,7 +404,7 @@ seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr, }skb_dst_drop (skb); -if ((flags &RT6_LOOKUP_F_DST_NOREF ) && !dst->rt_uncached_list) +if (flags &RT6_LOOKUP_F_DST_NOREF )skb_dst_set_noref (skb, dst);else skb_dst_set (skb, dst);
Signature · 1 line
2.56.1
PATCH 2/2 ipv6: keep a superseded pcpu dst working until its holders notice
In reply to Ralf LiciFrom: David Ahern <[email protected]>
When rt6_get_pcpu_route() finds that a pcpu dst of a route using a nexthop object has been invalidated, it takes the dst out of its slot and calls dst_dev_put() on it. That points the dst at the blackhole device and makes its output a discard, although the dst may still be
held: by a socket, a tunnel dst_cache, an xfrm bundle, or an skb that has not reached dst_output() yet (a queued or NFQUEUE reinjected packet, async crypto). Those packets are dropped, where the holder would have noticed that the dst is stale at its next check and looked the route up again.
The dst does have to be detached from its device when the device is
unregistered: it is no longer reachable from the FIB, and a holder such as an idle socket can keep it indefinitely, which would hold the device. Put it on the uncached list instead. It keeps working until its holders let go, and rt6_uncached_list_flush_dev() detaches it on unregister like any other uncached dst.
Drop the slot's reference after a grace period too. A lookup with RT6_LOOKUP_F_DST_NOREF may have taken the dst from the slot without a reference, and when the only other reference is the child reference of an xfrm bundle, dst_destroy releases it with dst_release_immediate and frees the dst under that lookup.
A later change to dst validation replaces the per-CPU dsts of all routes the same way.
[ralf: drop the slot's reference after a grace period, drop the comment
and the Fixes tag]
--- net/ipv6/route.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv6/route.c +2 -2
@@ -1499,8 +1499,8 @@ static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)/* Paired with READ_ONCE() in __fib6_drop_pcpu_from() */ prev =xchg (p,NULL );if (prev) { -dst_dev_put (&prev->dst); -dst_release (&prev->dst); +rt6_uncached_list_add (prev); +rt6_dst_release_deferred (prev); } pcpu_rt =NULL ;
Signature · 1 line
2.56.1