From: Jiayuan Chen <mrpre@163.com>
To: Hangbin Liu <liuhangbin@gmail.com>,
"David S. Miller" <davem@davemloft.net>,
David Ahern <dsahern@kernel.org>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Jiayuan Chen <jiayuan.chen@linux.dev>
Cc: David Ahern <dsahern@gmail.com>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
Fei Liu <feliu@redhat.com>
Subject: Re: [PATCH net v2] ipv6: fix data race in fib6_metric_set() using cmpxchg
Date: Sat, 28 Mar 2026 19:22:48 +0800 [thread overview]
Message-ID: <7e8c45cb-b5dd-4694-80e9-7c8e5ad9ade7@163.com> (raw)
In-Reply-To: <20260327-b4-fib6_metric_set-kmemleak-v2-1-366b2c78b5c2@gmail.com>
On 3/27/26 10:24 AM, Hangbin Liu wrote:
> fib6_metric_set() may be called concurrently from softirq context without
> holding the FIB table lock. A typical path is:
>
> ndisc_router_discovery()
> spin_unlock_bh(&table->tb6_lock) <- lock released
> fib6_metric_set(rt, RTAX_HOPLIMIT, ...) <- lockless call
>
> When two CPUs process Router Advertisement packets for the same router
> simultaneously, they can both arrive at fib6_metric_set() with the same
> fib6_info pointer whose fib6_metrics still points to dst_default_metrics.
>
> if (f6i->fib6_metrics == &dst_default_metrics) { /* both CPUs: true */
> struct dst_metrics *p = kzalloc_obj(*p, GFP_ATOMIC);
> refcount_set(&p->refcnt, 1);
> f6i->fib6_metrics = p; /* CPU1 overwrites CPU0's p -> p0 leaked */
> }
>
> The dst_metrics allocated by the losing CPU has refcnt=1 but no pointer
> to it anywhere in memory, producing a kmemleak report:
>
> unreferenced object 0xff1100025aca1400 (size 96):
> comm "softirq", pid 0, jiffies 4299271239
> backtrace:
> kmalloc_trace+0x28a/0x380
> fib6_metric_set+0xcd/0x180
> ndisc_router_discovery+0x12dc/0x24b0
> icmpv6_rcv+0xc16/0x1360
>
> Fix this by:
> - Set val for p->metrics before published via cmpxchg() so the metrics
> value is ready before the pointer becomes visible to other CPUs.
> - Replace the plain pointer store with cmpxchg() and free the allocation
> safely when competition failed.
> - Add READ_ONCE()/WRITE_ONCE() for metrics[] setting in the non-default
> metrics path to prevent compiler-based data races.
>
> Fixes: d4ead6b34b67 ("net/ipv6: move metrics from dst to rt6_info")
> Reported-by: Fei Liu <feliu@redhat.com>
> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
https://sashiko.dev/#/patchset/20260327-b4-fib6_metric_set-kmemleak-v2-1-366b2c78b5c2%40gmail.com
The concern about reader paths (e.g., ip_dst_init_metrics, fib6_pmtu)
lacking READ_ONCE()
annotations is valid — if the compiler reloads from->fib6_metrics after
inlining, it could produce
an inconsistent pointer/flags combination in dst->_metrics, potentially
leading to a refcount_dec
on the read-only dst_default_metrics.
However, this is a pre-existing issue that exists before this patch.
The plain store f6i->fib6_metrics = p in the original code has the same
read-side race.
This patch focuses on fixing the writer-side data race that causes
kmemleak, and it
does so correctly.
BTW, please consider moving the declaration of m to the top of the
function if you have a next version
next prev parent reply other threads:[~2026-03-28 11:24 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-27 2:24 Hangbin Liu
2026-03-28 11:22 ` Jiayuan Chen [this message]
2026-03-31 1:34 ` Hangbin Liu
2026-03-31 0:46 ` Jakub Kicinski
2026-03-31 1:12 ` Hangbin Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7e8c45cb-b5dd-4694-80e9-7c8e5ad9ade7@163.com \
--to=mrpre@163.com \
--cc=davem@davemloft.net \
--cc=dsahern@gmail.com \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=feliu@redhat.com \
--cc=horms@kernel.org \
--cc=jiayuan.chen@linux.dev \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liuhangbin@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®