Article View: linux.kernel.netdev
Article #4188[PATCH bpf-next 10/38] bpf: Don't saturate owner_refcnt on the last unpin
From: Alexei Starovoitov
Date: Sat, 17 Oct 2026 01:24
Date: Sat, 17 Oct 2026 01:24
72 lines
11011 bytes
11011 bytes
From: Josef Bacik <josef@toxicpanda.com>
bpf_local_storage_map_free() pins the owner with refcount_inc_not_zero()
while it uncharges an element. The pin counts when
bpf_local_storage_destroy() could not get local_storage->lock: destroy()
then goes on, drops the owner's reference and spins until the count is
zero. So the pin is the last reference whenever it is needed, and
refcount_dec() takes that for a bug: it warns "decrement hit 0; leaking
memory" and saturates the count. Unless destroy() happens to read the
count between the two stores it spins for ever, under rcu_read_lock().
Drop the pin with refcount_dec_and_test().
Fixes: 5d800f87d0a5 ("bpf: Support lockless unlink when freeing map or local storage")
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
include/linux/bpf_local_storage.h | 13 +++++++++++++
kernel/bpf/bpf_local_storage.c | 6 +++---
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
index 9e4f5c45c974..6c643ac51079 100644
--- a/include/linux/bpf_local_storage.h
+++ b/include/linux/bpf_local_storage.h
@@ -104,6 +104,19 @@ struct bpf_local_storage {
refcount_t owner_refcnt;/* Used to pin owner when map_free is uncharging */
};
+/*
+ * Drop a reference got by refcount_inc_not_zero(&local_storage->owner_refcnt).
+ * bpf_local_storage_destroy() may have dropped the owner's reference meanwhile
+ * and wait for the count to reach zero, so this one can be the last.
+ */
+static inline void
+bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage)
+{
+ bool last __maybe_unused;
+
+ last = refcount_dec_and_test(&local_storage->owner_refcnt);
+}
+
/* U16_MAX is much more than enough for sk local storage
* considering a tcp_sock is ~2k.
*/
diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c
index 4642da062f0f..7721834e4bff 100644
--- a/kernel/bpf/bpf_local_storage.c
+++ b/kernel/bpf/bpf_local_storage.c
@@ -199,7 +199,7 @@ static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem *
}
if (pin_owner)
- refcount_dec(&local_storage->owner_refcnt);
+ bpf_local_storage_unpin_owner(local_storage);
}
/* local_storage->lock must be held and selem->local_storage == local_storage.
@@ -753,8 +753,8 @@ u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
while (refcount_read(&local_storage->owner_refcnt))
cpu_relax();
/*
- * Paired with refcount_dec() in bpf_selem_unlink_nofail()
- * to make sure destroy() sees the correct local_storage->mem_charge.
+ * Paired with bpf_local_storage_unpin_owner() to make sure
+ * destroy() sees the correct local_storage->mem_charge.
*/
smp_mb();
}
--
2.55.0
Technical Details
Message-ID:
<20261017012451.364800-2-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-347595-linux-kernel-netdev=dewy.ruReferences:
<20261017012058.355787-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-347595-linux-kernel-netdev=dewy.ru
From: alexei.starovoitov@gmail.com (Alexei Starovoitov)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf-next 10/38] bpf: Don't saturate owner_refcnt on the last unpin
Date: Sat, 17 Oct 2026 01:24:23 +0000
Organization: Usenet Network
Message-ID: <20261017012451.364800-2-alexei.starovoitov@gmail.com>
References: <20261017012058.355787-1-alexei.starovoitov@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="2934167"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:dBmn8YQPfTbrF+dHNBz65r3F4Gc= sha256:cuk/NZA6z7jpAZpv+CqXIYaBEvAX2CiY5VW0OquPalI=
sha1:Ovnf8G+ovzbwATwGcJ03sfn/rO4= sha256:F/0umKUS871g14YdLkFvXasGiOVojX0GAsp6aZLIDzE=
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 DC55211063F; Sat, 17 Oct 2026 04:26:49 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 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] 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 kxLwasJEMnwq;
Sat, 17 Oct 2026 04:26:47 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-347595-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 1BE2711063D; Sat, 17 Oct 2026 04:26:38 +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 C53623C004; Sat, 17 Oct 2026 01:25:19 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 81DAD374A18;
Sat, 17 Oct 2026 01:25:19 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MjYBLN+I"
X-Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182])
(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 52DA736DA15
for <netdev@vger.kernel.org>; Sat, 17 Oct 2026 01:25:18 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1792200319; cv=none; b=JanazElbikh1RxlojnuQH8Y0jJyUPA9qIvDrI8Hi3RJRXp14iVURkzGE8/47rZ2NCEzLxFsMlf09xD8fnZalIIt3lbNTnqIPD0ebMa3JqQsGcnGYDHN4p7i8JqMFK5OLOMiIdqWuH/NrBoJFvs9jIbPZ8p9BPyzF/GLxwb8Z5uk=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1792200319; c=relaxed/simple;
bh=Er/uTSUJqJwQFr/bT7JAFBRgV8FEqRpD4CpEtcK6xpI=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
MIME-Version; b=fWeaYfxdgoAfpKR/pUJ3gLZ/A2H2VX86Jdkv5kqoBWM62NMMuJyk3kL5W43lO1yFk/vX0WpY8AjpqcowS3Sz+KPfs1It+NuNTJPrGc5zUxxnVNS2/UluQ3fjsMoEOiNjF+NthZrIm/YFMW8fqFBqdhHU4WP9M4rKqmQagXJtpM4=
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=MjYBLN+I; arc=none smtp.client-ip=209.85.214.182
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-f182.google.com with SMTP id d9443c01a7336-2e608cd460eso2159895ad.3
for <netdev@vger.kernel.org>; Fri, 16 Oct 2026 18:25:18 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1792200318; x=1792805118; 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=iELm3bTnzk4B+pnNJ4263DpWkNBrv9oDYGlQ98xR3Ik=;
b=MjYBLN+ITE+H5g6nxeJBu5ND9DRTEm4TnFJBw6FbqgdfGLFhBgfip3QQO30jUTi0KE
BoV86dqnENLcU/YC3gNHYZ0yv2pNdS4bNI0Vz2BE1UjaBvECrgcqFXMULtok8pZeEnmi
uxsGFKCiijrCGAIKrSSZL6jsKleBIe8L23Scoll6zaNb5rVZrFL+Jx2qeCj9clKjcEWp
WV+51rLTPNaQXptxJqR0VcqIhEUgrHSL3IPGCBHKplyPz8mlXAxPI93ou0vaNkKeSzuH
p5ppPs8MsF3wOyCUOENCqVuJIq/BHQ6sNlOMghticMhAjkAxszpUpd+m3BTFp4WdCgrv
Pjjg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1792200318; x=1792805118;
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: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UMi23ozz2CvdfZPatB1hkmq3FWqacGoIFzO+TK
MUCP6Bd7xO9hz6uCEnCZslnQOAeK6iD2hNrl2V7RyIsCLLk2dFHOkIyXgBogABFesSx6jhCBKf2
6N4XsFPjDPwKwCp+Yz02dXFGoXvDRCPDzJYOzdiwbQZWsGhlrSP4G4m5w0lJ5bTAoJbxtmO5GFo
2DlZDg2ytTd+SKcIjvtS/4+ihdn4TNaE+eS1orTnHPs=
X-Received: by 2002:a17:902:f70f:b0:2e9:5829:9b3b with SMTP id d9443c01a7336-2e9a6d36836mr13212965ad.35.1792200317544;
Fri, 16 Oct 2026 18:25:17 -0700 (PDT)
X-Received: from coder-ast-ast-dev-ml-0.tail2dda0.ts.net ([153.61.198.252])
by smtp.gmail.com with ESMTPSA id d9443c01a7336-2e97e804ea9sm16928105ad.13.2026.10.16.18.25.16
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 16 Oct 2026 18:25:16 -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: <20261017012058.355787-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>