Article View: linux.kernel.netdev
Article #27358[PATCH 6.18.y 1/2] ipv6: flowlabel: take ip6_fl_lock across mem_check and fl_intern
From: Dickson Wu via B4 Relay
Date: Sat, 05 Dec 2026 11:58
Date: Sat, 05 Dec 2026 11:58
158 lines
15234 bytes
15234 bytes
From: Maoyi Xie <maoyi.xie@ntu.edu.sg>
commit 7ce5556f255a680d80daa31b1cedecf7f89e2c22 upstream.
mem_check() in net/ipv6/ip6_flowlabel.c reads fl_size without
holding ip6_fl_lock. fl_intern() takes the lock immediately
afterwards. The two checks therefore race against concurrent
fl_intern, ip6_fl_gc and ip6_fl_purge writers, which makes the
mem_check budget check approximate.
Move spin_lock_bh(&ip6_fl_lock) and the matching unlock from
fl_intern() into its only caller ipv6_flowlabel_get(). The
mem_check() call now runs under the same critical section as the
fl_intern() insert, so the budget check is exact.
With all writers and the read of fl_size under ip6_fl_lock,
convert fl_size from atomic_t to plain int. The four sites that
update or read fl_size are fl_intern (insert path), ip6_fl_gc
(garbage collector, the !sched check and the per-entry decrement),
ip6_fl_purge (per-netns purge), and mem_check (budget check), and
all four now run under ip6_fl_lock.
This is a prerequisite for adding a per-netns budget alongside
fl_size. The follow-up patch adds netns_ipv6::flowlabel_count and
folds it into mem_check().
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Suggested-by: Willem de Bruijn <willemb@google.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Maoyi Xie <maoyi.xie@ntu.edu.sg>
Link: https://patch.msgid.link/20260506082416.2259567-2-maoyixie.tju@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Dickson Wu <dickson.wu@certik.com>
---
Clean pick; prerequisite for patch 2. Tested on v6.18.66 with
patch 2, see the cover letter.
The backport and the test runs were prepared with AI coding tools
(OpenAI Codex agents, with review by Claude). I went over the result
before signing off.
net/ipv6/ip6_flowlabel.c | 34 +++++++++++++++++++++-------------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/net/ipv6/ip6_flowlabel.c b/net/ipv6/ip6_flowlabel.c
index 74de0f344f54..d3c9c06e4c08 100644
--- a/net/ipv6/ip6_flowlabel.c
+++ b/net/ipv6/ip6_flowlabel.c
@@ -40,7 +40,7 @@
#define FL_HASH_MASK 255
#define FL_HASH(l) (ntohl(l)&FL_HASH_MASK)
-static atomic_t fl_size = ATOMIC_INIT(0);
+static int fl_size;
static struct ip6_flowlabel __rcu *fl_ht[FL_HASH_MASK+1];
static void ip6_fl_gc(struct timer_list *unused);
@@ -163,7 +163,7 @@ static void ip6_fl_gc(struct timer_list *unused)
if (time_after_eq(now, ttd)) {
*flp = fl->next;
fl_free(fl);
- atomic_dec(&fl_size);
+ fl_size--;
continue;
}
if (!sched || time_before(ttd, sched))
@@ -172,7 +172,7 @@ static void ip6_fl_gc(struct timer_list *unused)
flp = &fl->next;
}
}
- if (!sched && atomic_read(&fl_size))
+ if (!sched && fl_size)
sched = now + FL_MAX_LINGER;
if (sched) {
mod_timer(&ip6_fl_gc_timer, sched);
@@ -196,7 +196,7 @@ static void __net_exit ip6_fl_purge(struct net *net)
atomic_read(&fl->users) == 0) {
*flp = fl->next;
fl_free(fl);
- atomic_dec(&fl_size);
+ fl_size--;
continue;
}
flp = &fl->next;
@@ -210,10 +210,10 @@ static struct ip6_flowlabel *fl_intern(struct net *net,
{
struct ip6_flowlabel *lfl;
+ lockdep_assert_held(&ip6_fl_lock);
+
fl->label = label & IPV6_FLOWLABEL_MASK;
- rcu_read_lock();
- spin_lock_bh(&ip6_fl_lock);
if (label == 0) {
for (;;) {
fl->label = htonl(get_random_u32())&IPV6_FLOWLABEL_MASK;
@@ -235,8 +235,6 @@ static struct ip6_flowlabel *fl_intern(struct net *net,
lfl = __fl_lookup(net, fl->label);
if (lfl) {
atomic_inc(&lfl->users);
- spin_unlock_bh(&ip6_fl_lock);
- rcu_read_unlock();
return lfl;
}
}
@@ -244,9 +242,7 @@ static struct ip6_flowlabel *fl_intern(struct net *net,
fl->lastuse = jiffies;
fl->next = fl_ht[FL_HASH(fl->label)];
rcu_assign_pointer(fl_ht[FL_HASH(fl->label)], fl);
- atomic_inc(&fl_size);
- spin_unlock_bh(&ip6_fl_lock);
- rcu_read_unlock();
+ fl_size++;
return NULL;
}
@@ -479,10 +475,14 @@ static bool fl_sock_at_lease_limit(const struct sock *sk)
static int mem_check(struct sock *sk)
{
- int room = FL_MAX_SIZE - atomic_read(&fl_size);
+ int room;
struct ipv6_fl_socklist *sfl;
int count = 0;
+ lockdep_assert_held(&ip6_fl_lock);
+
+ room = FL_MAX_SIZE - fl_size;
+
if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
return 0;
@@ -711,11 +711,19 @@ static int ipv6_flowlabel_get(struct sock *sk, struct in6_flowlabel_req *freq,
if (!sfl1)
goto done;
+ rcu_read_lock();
+ spin_lock_bh(&ip6_fl_lock);
err = mem_check(sk);
+ if (err == 0)
+ fl1 = fl_intern(net, fl, freq->flr_label);
+ else
+ fl1 = NULL;
+ spin_unlock_bh(&ip6_fl_lock);
+ rcu_read_unlock();
+
if (err != 0)
goto done;
- fl1 = fl_intern(net, fl, freq->flr_label);
if (fl1)
goto recheck;
--
2.43.0
Technical Details
Message-ID:
<20261205-flowlabel-618b-v1-1-d81e833c0c60@certik.com>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377962-linux-kernel-netdev=dewy.ruReferences:
<2026051901-actress-ambition-70c4@gregkh>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377962-linux-kernel-netdev=dewy.ru From: devnull+dickson.wu.certik.com@kernel.org (Dickson Wu via B4 Relay) Newsgroups: linux.kernel.netdev Subject: [PATCH 6.18.y 1/2] ipv6: flowlabel: take ip6_fl_lock across mem_check and fl_intern Date: Sat, 05 Dec 2026 11:58:21 +0000 Organization: Usenet Network Message-ID: <20261205-flowlabel-618b-v1-1-d81e833c0c60@certik.com> References: <2026051901-actress-ambition-70c4@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="3020049"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:ujNMayzL+pwqfiE06eCgbXqHGtE= sha256:Mysmc8wbYl52ros7/ZxiYQVhgdd3eKwrE4K+duwOJoU= sha1:64JDcD9RHXHHoCLydpXZXKyaLhc= sha256:Q5REJWKDhR7ERlt1Qf/v2euTugm26kB9pZKorKnW8Bc= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id E72DF117E09; Sat, 5 Dec 2026 15:01:42 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.539 X-Spam-Status: No, score=-3.539 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.14, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EML0IVFFDuhy; Sat, 5 Dec 2026 15:01:39 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-377962-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 788C4117DE6; Sat, 5 Dec 2026 15:01:30 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id AAD2E361B6; Sat, 5 Dec 2026 12:01:24 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796472084; b=hUFTsQFTJ6L1gu66auex6BJr39aF/ActxkdNyqV1rPX1t9CDWYkgHG3Kd2XyTJfkivkd w2Sllm5a+Ie24FoD+6fvRPIfTsa4ujGZEdicRFgODjI9J6bdRIIJXlr7fNlu38wwbEx+S V0UpcCp7x+rspu1rp3oJneceQLpE/kV/jDC8nD7S//GKzIlFEXjicqPOaF600GQ2mx1+p X/qxscH0BBJttkB0q+ekEbyHmxPlhPkqBmhyn5zNQZsTtWIxMzIOr2zElOn1E1G3i3SjU LxHqNycd5iQ8U8Lxe+mDXbjUr6xws06NVQKik7bS97OCqbQGNLAWE5/BHcpzEX1fh+g== X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796472084; h=X-Original-To:Received:Received:DKIM-Signature:Received:From:Date: Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:In-Reply-To:References:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=czLQEP147XaSB66FB+YyHIl4vBR9Oh45LF4ZOlybkQ0=; b=fX1RhUjrQBnyAERvnQzXNJfywUlKis4KgOzo3BYRupiQBZr8yKVYvIUucjbqVR1P00pr 97hfoNDE6L644qscUngVK62JS1Qm8qsXVHZ51n8tmgFfKg1l5YzxAyYcWd2/8ImqeZ1lo ACJ45OjZl5UCjtjOzve5AUMCmfGtyRTcY+yUfgYQ0o8UOyO7J5+PZYKlIOZ/QPx+Gm7EX SSSg67dqqDlH663dQKNFWn29dOkCkkU3hw8lHwJoweemEhOwemsIFG6haR47cfo6F+RN6 yXk+4nz+zqdwHH5CFPKR/2qPX+6aWCO1OozCWKFyG0fgnvWLr+e1hq8K+NvoIoy/H7Q== X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Tg6cyYaH; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8206B606A334; Sat, 5 Dec 2026 12:01:24 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Tg6cyYaH X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796472078; b=3i5xM3GSu1AfRTabl2Ho4rOK7Xqy4E+T7SW0btcO8A3iZAcs2e4h/sKD3bKgc2Icy1Ji EMHRUIySmnqr52Fvn49RrFV07fxf3CBOrkBha5StTVCyeMm3eoEDdBqBLEFkfcNAy3YBw /adfmhEvcBvrgA0n2JIXvlKYOUJ47pKuoapqQBZe2xPUBiGL1uMC/NtxYGZcp7I6oxIT7 wAithuBryU1LWZ4Py2DA5hwFHIgHNfLs6oCsThMrrsD9alH4oc94KDYftHW0KxWqnLRuA 0RC92rxqlzZ75cGb7MnMRIBdzdZUsr+ErC5Lez5c5S4kbcjWYowuVvqanuPTWnOlicA== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796472078; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:In-Reply-To: References:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-Endpoint-Received:X-Original-From:Reply-To; bh=czLQEP147XaSB66FB+YyHIl4vBR9Oh45LF4ZOlybkQ0=; b=CBrn/I56Jwo8vtjymy86I3rlde3iByN7FcyxjbYdCGKHjqWYeiUqrUHymXz4KMLrfdMH mXmOPNC+JCtZIGtSHwocNfIOmkr8p2hYKXNE+GRlpIomXtmU2wTAYv1EdtSTVC+D9pzzX PNuq5y4U8Cig+4LnCtyBBzAwu0WrJu7mFKwfqPrgex9vTySxZq5IA4CobyTf7Eftcqnpp LhvUrq+TBftCXHs3Up8qaUhgcZ9fpXWlaUXQWXryOMMmzgDbPojbAtaqY73Qu6y9e6/XU Bz5MuxRwoDdg7YeXSori5SLpzqBTHtEtMWNYwEGXtf3Gg1ZUZIFV1yBqor31BOUWUcg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Tg6cyYaH; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CC5F606A322; Sat, 5 Dec 2026 12:01:18 +0000 (UTC) X-Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796472078; b=2wSt0F/WHfMohDM7ukAKeO11DXf56H4ChRONg+7hKLv7mPztlS+5mVJBOvMzyVthEgw4 B3mIUjYND+pjryOZJhpE0bMH33QoBhNizcGkOja4+RDKF55dc9gCGJKH2BnDevX6TdLko e/rQRedS57Q6Kk0vR8Xuj0z5+FytZzcuBh5tSTdTIwiCk4EJ5mknG/Vj3PK6eA8gofzVw lh8TAB/wt8jb7x60cLYPXHOpUbhOIEegHgn1izfWY7o3gSGIZNnZS2XeMOIRkM0DLY7F8 r801uw6c3+2XEM999w/VV1dMsMyTyqKhCyL2VC+bhVF7YbB56N1lHBoZs9dhakMQ9SA== X-ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796472078; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:In-Reply-To:References:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=czLQEP147XaSB66FB+YyHIl4vBR9Oh45LF4ZOlybkQ0=; b=c3GtE5B3PFvTrff7HOkIud2BijDR6jeiOxdj8NXvFXuZce9hJFcnb1SO94SLwVl62vOo lKsUALV6vZfW5D1PbS0q2ESySa/Elr+X128VCPDnOc35qYbqE1tRO6GC/xVSG6RwslQLX /FfUCQCM4IMyLDVlF+uQOJFPiNx5fZCzmD4nxaDyjH3HTQhGkJOQT/tJhg6Rvmo1+S3us yB1LTG9aG8dXt5ZNXERU1tn79ermW2AATfp/6nOL5MnDQw8lcuK6JgYat6iD7uJitCr0x 5ConDmHxoT3pH6wwFJEsq4DTMlf9YIe5oyVquG6PO1kcZYXZtfJsgi0SneqdqkW1sTQ== X-ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id E35B21F00936; Sat, 5 Dec 2026 12:01:17 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796472078; bh=czLQEP147XaSB66FB+YyHIl4vBR9Oh45LF4ZOlybkQ0=; h=From:Date:Subject:In-Reply-To:References:To:Cc:Reply-To; b=Tg6cyYaH4D+ifoAz7EEcjEGFMNKNXZUhXaQSumjVWmf+WXgB8dICjSrigrwBB8z3s gVPvY4RYm9dxC0Pfi6ysLXWcItGr/MS9aPzwjIrsrd6j+ZdlyPZgC/uY1En5iHJ4em KY3suXmEZB43M7MtOzYjSid3viqCTPK6Sgv3v+yewz7L6jhGYOWeDbO1UL1fzPFYnP sky2LwAoRh5YRHKShMy7q+QvTRGfq+/cTKlapdeIb5hNuW0ctpRQr1FvLh5NjC3RI/ WA8kdmetBIYaaZ08PgrA5xcRa3+a+QOXQ3LAgH6MfE715AX/Ya+ddn5t3+puj+/YvX 5T58sVPxj3BPg== X-Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 78C68502E4; Sat, 5 Dec 2026 12:01:17 +0000 (UTC) X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-In-Reply-To: <2026051901-actress-ambition-70c4@gregkh> X-Cc: maoyi.xie@ntu.edu.sg, kuba@kernel.org, willemb@google.com, netdev@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796471900; l=4878; i=dickson.wu@certik.com; s=dickson-b4; h=from:subject:message-id; bh=kIOvGP6BO5sVV03bnKHpyELjscF+byCGe3gP/DQCpW0=; b=AlkArFv9O/3MDGk6EGe+2vbZy5HcPtPke0qYL5Z/s4eq9BBvRQQyn1ywVly1NvVMQ0cojRCkx rROcAAFOSpdCUTGSr5Wo5AoAMa0lDAqKrO3NPR06rD9gD8879+7M659 X-Developer-Key: i=dickson.wu@certik.com; a=ed25519; pk=fxH7FS4FdojeJSGLJzgKdfO51Rvr4EW30yinj2mc1vg= X-Endpoint-Received: by B4 Relay for dickson.wu@certik.com/dickson-b4 with auth_id=1647 X-Original-From: Dickson Wu <dickson.wu@certik.com> X-Reply-To: dickson.wu@certik.com