Re: [PATCH net-next v3 2/3] ipv6: add ip6_dst_lookup_flow_cookie

From: Xia Yan

Date: Wed Nov 18 2026 - 03:48:59 EST


I traced the race and the fix against current net-next.

On the writer side, xfrm_policy_insert() links the policy and then calls
rt_genid_bump_ipv6(), which resolves to fib6_flush_trees(). A fresh serial
is allocated from the namespace counter first, then every node is
restamped under tb6_lock. A late rt6_get_cookie() can pick up that
post-insertion serial for a dst whose lookup ran before the insertion,
and the pair keeps passing dst_check until the next flush. Sampling the
cookie between two generation reads closes the in-lookup window. A
policy inserted after the helper returns is still caught later, because
the restamp changes fn_sernum itself and the stored pre-insertion cookie
stops matching. fib6_new_sernum() never returns 0, so the fallback
cookie always fails fib6_check().

1/3 checks out as well. The old code kept writing -1 into the node, so a
second failure changed nothing and the namespace generation never moved.
The fresh allocation under tb6_lock matches the discipline the
fib6_ifup()/fib6_ifdown() paths already get from __fib6_clean_all().
The conversion boundary in 3/3 also matches the tree: rawv6 does not
cache the dst, and the accepted-socket and refill stores in tcp_ipv6.c
are listed as follow-up.

One thing I could not fully work out is the ordering behind the
generation checks. When the lookup misses a change, the reader relies on
the two generation reads to notice it. My understanding is that this
needs the change visible before the counter advance, and the counter
advance visible before the node restamps. I could not establish either
hop, though I may well be missing something.

In xfrm_policy_insert(), the policy is linked into the hash and
rt_genid_bump_ipv6() runs within the same critical section. As far as I
can tell, the linking stores and the cmpxchg on fib6_sernum are all
relaxed, with no barrier between them. Could a reader read the advanced
generation in gen0, miss the policy during the lookup, sample a
restamped fn_sernum, and still read the same generation in gen1?

In fib6_flush_trees(), the serial is allocated before __fib6_clean_all()
takes tb6_lock, and fib6_clean_node() stamps the nodes with a plain
WRITE_ONCE(). spin_lock() is an ACQUIRE, and my reading of
memory-barriers.txt is that operations issued before an ACQUIRE may
complete after it. So a reader could sample the restamped fn_sernum
between two reads that both still return the old generation. The only
pairing I found written down here is fib6_get_cookie_safe()'s smp_rmb()
against the smp_wmb() in __fib6_update_sernum_upto_root().

If either gap is real, the reader ends up caching the plain dst with a
post-flush serial that stays valid until the next flush. That is the
state the series sets out to prevent, so I suspect I am missing
something. Your September RFC [1] seems to anticipate this in item 1 of
its writer contract: "a routing-visible update is published before its
serial is allocated and stamped". Could you check whether the two hops
are covered somewhere I did not look?

[1] https://lore.kernel.org/netdev/20260901122501.482920-1-ralf@xxxxxxxxxxxxx/

If there is an answer to the above that I missed, please feel free to
add:

Reviewed-by: Xia Yan <yanxia@xxxxxxxxxxxxxxxxxxxx>