Article View: linux.kernel.netdev
Article #4389[PATCH bpf-next v2 17/43] bpf: Hold the socket in the sk_storage map iterator
From: Alexei Starovoitov
Date: Sat, 17 Oct 2026 16:37
Date: Sat, 17 Oct 2026 16:37
161 lines
14131 bytes
14131 bytes
From: Josef Bacik <josef@toxicpanda.com>
The iterator over a sk_storage map passes the owner of an element to its
program as ctx->sk, without a reference. All that it holds is the RCU read
lock, which keeps the element and the storage, not the socket.
An element is unlinked by bpf_sk_storage_free(), which __sk_destruct()
calls right before it frees the socket. That is no better for a socket
with SOCK_RCU_FREE: the grace period comes in front of __sk_destruct(),
so a reader which starts behind call_rcu() still finds the element. The
program and the helpers it hands ctx->sk to, such as
bpf_sk_storage_get() or bpf_sk_storage_delete(), use a socket which is
being freed on another CPU. It takes nothing but reading the iterator
while sockets with an element in the map are closed, by whomever.
Take a reference for the time of the program, and skip the element if
there is none to be had. To look at the owner at all, do what
bpf_selem_unlink_nofail() does for bpf_local_storage_map_free(): as long
as the element is linked under the lock of the storage,
bpf_local_storage_destroy() has not got past it, unless it has given up
on the lock, and then it waits for owner_refcnt.
The last reference may now be dropped by the iterator. The end of a socket
may want process context: rfcomm_sock_destruct() takes a mutex. So only
drop the reference under RCU, and free a socket whose last one it was when
the RCU read lock has been released, which is at the end of a bucket and
of every read() at the latest.
Fixes: c69d2ddb2072 ("bpf: Using rcu_read_lock for bpf_sk_storage_map iterator")
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
net/core/bpf_sk_storage.c | 73 ++++++++++++++++++++++++++++++++++++---
1 file changed, 68 insertions(+), 5 deletions(-)
diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
index aea67b446c7c..ea22ec5ef5b1 100644
--- a/net/core/bpf_sk_storage.c
+++ b/net/core/bpf_sk_storage.c
@@ -699,8 +699,37 @@ struct bpf_iter_seq_sk_storage_map_info {
struct bpf_map *map;
unsigned int bucket_id;
unsigned skip_elems;
+ struct rcu_head *dead_sks;
};
+/*
+ * sock_put() in two halves. The end of a socket may want process context:
+ * rfcomm_sock_destruct() takes a mutex. If the iterator drops the last
+ * reference, it keeps the socket until it has left the RCU read side critical
+ * section. Nobody uses sk_rcu before sk_destruct().
+ */
+static void
+bpf_sk_storage_map_seq_put_sk(struct bpf_iter_seq_sk_storage_map_info *info,
+ struct sock *sk)
+{
+ if (refcount_dec_and_test(&sk->sk_refcnt)) {
+ sk->sk_rcu.next = info->dead_sks;
+ info->dead_sks = &sk->sk_rcu;
+ }
+}
+
+static void
+bpf_sk_storage_map_seq_free_sks(struct bpf_iter_seq_sk_storage_map_info *info)
+{
+ struct sock *sk;
+
+ while (info->dead_sks) {
+ sk = container_of(info->dead_sks, struct sock, sk_rcu);
+ info->dead_sks = sk->sk_rcu.next;
+ sk_free(sk);
+ }
+}
+
static struct bpf_local_storage_elem *
bpf_sk_storage_map_seq_find_next(struct bpf_iter_seq_sk_storage_map_info *info,
struct bpf_local_storage_elem *prev_selem)
@@ -729,6 +758,7 @@ bpf_sk_storage_map_seq_find_next(struct bpf_iter_seq_sk_storage_map_info *info,
/* not found, unlock and go to the next bucket */
b = &smap->buckets[bucket_id++];
rcu_read_unlock();
+ bpf_sk_storage_map_seq_free_sks(info);
skip_elems = 0;
break;
}
@@ -796,12 +826,44 @@ DEFINE_BPF_ITER_FUNC(bpf_sk_storage_map, struct bpf_iter_meta *meta,
struct bpf_map *map, struct sock *sk,
void *value)
+/*
+ * Take a reference on the socket which owns @selem. RCU keeps the element, not
+ * its owner. As long as the element is linked under the lock of the storage,
+ * the owner has not got past bpf_sk_storage_free(), unless that has given up
+ * on the lock. Then it still waits for owner_refcnt.
+ */
+static struct sock *
+bpf_sk_storage_selem_get_sk(struct bpf_local_storage_elem *selem)
+{
+ struct bpf_local_storage *sk_storage;
+ struct sock *sk = NULL;
+ unsigned long flags;
+
+ sk_storage = rcu_dereference(selem->local_storage);
+ /* reset since ->next() looked: the socket goes away */
+ if (!sk_storage)
+ return NULL;
+
+ if (raw_res_spin_lock_irqsave(&sk_storage->lock, flags))
+ return NULL;
+
+ if (!hlist_unhashed(&selem->snode) &&
+ refcount_inc_not_zero(&sk_storage->owner_refcnt)) {
+ sk = sk_storage->owner;
+ if (!refcount_inc_not_zero(&sk->sk_refcnt))
+ sk = NULL;
+ bpf_local_storage_unpin_owner(sk_storage);
+ }
+ raw_res_spin_unlock_irqrestore(&sk_storage->lock, flags);
+
+ return sk;
+}
+
static int __bpf_sk_storage_map_seq_show(struct seq_file *seq,
struct bpf_local_storage_elem *selem)
{
struct bpf_iter_seq_sk_storage_map_info *info = seq->private;
struct bpf_iter__bpf_sk_storage_map ctx = {};
- struct bpf_local_storage *sk_storage;
struct bpf_iter_meta meta;
struct bpf_prog *prog;
int ret = 0;
@@ -812,14 +874,14 @@ static int __bpf_sk_storage_map_seq_show(struct seq_file *seq,
ctx.meta = &meta;
ctx.map = info->map;
if (selem) {
- sk_storage = rcu_dereference(selem->local_storage);
- /* reset since ->next() looked: the socket goes away */
- if (!sk_storage)
+ ctx.sk = bpf_sk_storage_selem_get_sk(selem);
+ if (!ctx.sk)
return SEQ_SKIP;
- ctx.sk = sk_storage->owner;
ctx.value = SDATA(selem)->data;
}
ret = bpf_iter_run_prog(prog, &ctx);
+ if (ctx.sk)
+ bpf_sk_storage_map_seq_put_sk(info, ctx.sk);
}
return ret;
@@ -837,6 +899,7 @@ static void bpf_sk_storage_map_seq_stop(struct seq_file *seq, void *v)
(void)__bpf_sk_storage_map_seq_show(seq, v);
else
rcu_read_unlock();
+ bpf_sk_storage_map_seq_free_sks(seq->private);
}
static int bpf_iter_init_sk_storage_map(void *priv_data,
--
2.55.0
Technical Details
Message-ID:
<20261017163745.3341787-18-alexei.starovoitov@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-347792-linux-kernel-netdev=dewy.ruReferences:
<20261017163745.3341787-1-alexei.starovoitov@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-347792-linux-kernel-netdev=dewy.ru
From: alexei.starovoitov@gmail.com (Alexei Starovoitov)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf-next v2 17/43] bpf: Hold the socket in the sk_storage map iterator
Date: Sat, 17 Oct 2026 16:37:19 +0000
Organization: Usenet Network
Message-ID: <20261017163745.3341787-18-alexei.starovoitov@gmail.com>
References: <20261017163745.3341787-1-alexei.starovoitov@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="3092764"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:5gfez0V8VMnpqN2O95bYRibrIR8= sha256:AkgKr7tMqyV8+OzCSeph8rA4kaMfz2xAVvaTKsfIlRM=
sha1:4vpdu8Co2Uaqi5bIY8ERlFv/g4U= sha256:mtTNY/63thPSlHdHQTNijiWpbcbW4tezyqh7qu420/I=
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 E03C3110737; Sat, 17 Oct 2026 20:12:45 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.047
X-Spam-Status: No, score=-3.047 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
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=gmail.com
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 L-UPZuDvxGlU;
Sat, 17 Oct 2026 20:12: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-347792-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id D171F110708; Sat, 17 Oct 2026 20:12:29 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
by sea.lore.kernel.org (Postfix) with ESMTP id 2F4206391C; Sat, 17 Oct 2026 16:39:48 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id C2A983AFB03;
Sat, 17 Oct 2026 16:39:34 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="T8Nu7cdU"
X-Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2DB33A0E85
for <netdev@vger.kernel.org>; Sat, 17 Oct 2026 16:39:32 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1792255174; cv=none; b=dohjkvI4OW5Ne61/UjkwNDAYb+we1jsOvCBsyEwpsuIvkAoWUJ5WBUTTOgwKf9Zk2FMv0zbH+9/7WtUqikmBaY49utM15M1/r82MMJXBJQq0219/waAOVHYCQz+NfRaPr5bPWxscQaJ86QG6t1AWh0s9Sz1TvXaGUku8uC+UXxU=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1792255174; c=relaxed/simple;
bh=M8ES0kL0OPES6hbTArznFgO3NVcCTS5zVjhh6KSPQtg=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
MIME-Version; b=H1AYKJEnonsfcNC+a5bR5AYYRPRS13JBxqIANODA5/qaIgTJy2jsUDPeAydmkjtDALXlOFCadUPpw9yiiCwg6v3XHETuUzrPTyVaATvaLNGp43qm5MT1PdU3nIqDfdYvwkJ9hHZKgF2OxTgCXVd0Jc0wush7Ig0yK377VLxfXes=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=T8Nu7cdU; arc=none smtp.client-ip=209.85.214.181
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2e9995ce866so6116415ad.0
for <netdev@vger.kernel.org>; Sat, 17 Oct 2026 09:39:32 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1792255172; x=1792859972; darn=vger.kernel.org;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=4LioG1TfRbQbbPeFWnF473ae+LrllOtiRpu5PoIzCrE=;
b=T8Nu7cdUN5RI2YNr4lbNB7ftELHjufitmqI+1qUCQwsYUCb0VtGtnyfLNrraUCz6+C
AW1qzY4doxpQXZ6nZgUtkzG4OP6lyEshDbzxBNR30ljKWge6koL3VoN1LY2dxA0T/Gvh
sG58wWefNUmiTm5wmr4w7uwF5Q1y15yVK4/yMUKMxzmDGYRfER910ABFGdYlEnFofQYt
b10TtKJadYKoZSGbq2VJGcG3udGWBz+qoJrLCGLw6wJ6hJYePSlij/tBAttaKvMFwGWx
fdmBfI14cW5WKX4PlvHdslt3rSeKUcEPhFz8rU7O1aK7YyktAW3fXd3QQy3LDQQPA4TR
6ZvA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1792255172; x=1792859972;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to:content-type;
bh=4LioG1TfRbQbbPeFWnF473ae+LrllOtiRpu5PoIzCrE=;
b=jDjeH+aerf+HhgsGsWPy/2enh2SVd1fk8DzGgFT0vDoAPEVtdFPDzsfa7YfgIQMq8+
LMeq2FQ5q8CVzagbPR/GEfrgpANkUABMb6A8vYRWeivNlb6S+ZIkXvmx/SVQe/KwdPA8
4xa4Pvf55mJrTfcUe+9ftMwVHtWl7McNmFV+jLnwmuWkVaqWtrvx1nx0FQerv8dvnQJf
g4LLExC7e5rh9yATMrwRpysfANzv7J1xKSUUn9mSq6xQBNvu6HRW8bDOSktUrihhbJtP
+dNRJ9M1s7V1kTcJS9rOqlaXO6q1R4J8XUsD7oUM2E7j3h0BqWEQ9JgynOE/DkyIIKo/
Eqtg==
X-Forwarded-Encrypted: i=1; AKwUvBx1p1/jkzrD7oBjdQeEkn3NMmiQfeWGjeowPf8TDtQNf3nfqTRE/MOc6aBorFVujWC+BIsTRG8=@vger.kernel.org
X-Gm-Message-State: AFq9FYKZaJVPJyh9RNOi8oegWTUDzvO80VKB3majRc2R0U1gNIWHKMaO
//Sey58++B2PBNPn9pGHj7+laYrQfP0RdldTE4k+lS/JQEqZA1mLV3sg
X-Gm-Gg: AYBFou1nKSKqHITmpb1Qgw4HhKrji6PfSL6isIMTzbqVYbO6s7FT6WKkJhM63j9y77L
IBQoTNf+1bs8Hi5Gf27Gf5kqv4+TetUpMMYBjRaikSipNmPIJZe6tXQI2PH3F55Q/vmncKxQ0MK
Vi5ONKaNYhUv0NGkdpGEijLTje9B9s/ENU4R2mG3fLxBGRlpbEaLHY6pO4K1nAxkT4pmI5dMR4/
WYvDLPBGacM0sBT4KnrDXpzCSit6fuo60kShDP2RMc07BlFhtArO9lnpKNublptAwDWrc9/hlFq
VPArHzLiNftc1neOFdKDJp7RJ3em0nnnRtA9/g6sSSMSJCJ51H5dhuar+FcoroRkN2MK3INhbD9
6oWvEV+ANH0iXuvCMbD7f15OTgb91+Iy5BPigxXY4p6TjV0vfogBvbye5TWlui8pRxLoxi50ur3
0megphYUT5kChFbEFWIvmJgwEOcqyhAJDnP0PRdXBZ27hheScU8/mXMwAX+qfBYP06AxEcRiKcw
K0SAhkcpAbHw/CkfVj9pJYkqvQtD92Yw2DPyEWMuWQsmGWDrkTfgbiInMyyJrswS9V50BTCUfVn
IcMbuyYdwYm+jDQ1Cihbj9nRJq1fNwqfNnNiQmvtE58=
X-Received: by 2002:a17:902:dac8:b0:2e9:5829:9b41 with SMTP id d9443c01a7336-2e9a6c02ce4mr32214835ad.37.1792255172188;
Sat, 17 Oct 2026 09:39:32 -0700 (PDT)
X-Received: from coder-ast-ast-dev-ml-0.tail2dda0.ts.net ([153.61.198.255])
by smtp.gmail.com with ESMTPSA id d9443c01a7336-2e97e8b1dd8sm22180545ad.69.2026.10.17.09.39.31
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Sat, 17 Oct 2026 09:39:31 -0700 (PDT)
X-Cc: daniel@iogearbox.net,
andrii@kernel.org,
eddyz87@gmail.com,
memxor@gmail.com,
netdev@vger.kernel.org,
josef@toxicpanda.com,
mason@kernel.org
X-Mailer: git-send-email 2.55.0
X-In-Reply-To: <20261017163745.3341787-1-alexei.starovoitov@gmail.com>
X-Precedence: bulk
X-Mailing-List: netdev@vger.kernel.org
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>