Article View: linux.kernel.netdev
Article #4208[PATCH bpf-next 30/38] bpf: Keep the symbols of a program until it has ended
From: Alexei Starovoitov
Date: Sat, 17 Oct 2026 01:24
Date: Sat, 17 Oct 2026 01:24
150 lines
13397 bytes
13397 bytes
From: Josef Bacik <josef@toxicpanda.com>
When the last reference to a program goes, __bpf_prog_put_noref() takes
its symbols out of the kallsyms tree and frees its line info at once, and
waits for the grace periods only with the rest. But the program may still
be running: the link or the array it was called from is gone, the call is
not over.
search_bpf_extables() finds the exception table of a program by its
symbol. A load that faults, and that the table is there to fix up, is
fatal from then on:
Oops: general protection fault, probably for non-canonical address
0xdead0000000006f8: 0000 [#1] SMP NOPTI
RIP: 0010:0xffffffffc06008f6
It takes a program that is detached and closed on one CPU while it runs on
another. Unwinders and bpf_prog_find_from_stack() do not know the program
any more either, and the stack dump of a BPF stream reads the freed line
info.
Free the line info behind the grace periods the program needs, as it is
done with the BTF that its text comes from. Remove the symbols in
bpf_prog_free_deferred(), which runs behind them as well. The symbols are
looked up under rcu_read_lock(), so the images and the program itself are
freed one more grace period later. All that the program refers to is
released when it was before.
Fixes: 3dec541b2e63 ("bpf: Add support for BTF pointers to x86 JIT")
Fixes: d7c431cafcb4 ("bpf: Add dump_stack() analogue to print to BPF stderr")
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
include/linux/bpf.h | 1 +
kernel/bpf/core.c | 50 +++++++++++++++++++++++++++++++-------------
kernel/bpf/syscall.c | 5 ++---
3 files changed, 38 insertions(+), 18 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index ad4ef937e112..c1d92fd10ef1 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1963,6 +1963,7 @@ struct bpf_prog_aux {
union {
struct work_struct work;
struct rcu_head rcu;
+ struct rcu_work rwork;
};
struct bpf_stream *stream[2];
struct mutex st_ops_assoc_mutex;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index e98c79e8009c..956555fc5e9f 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3087,12 +3087,40 @@ static void bpf_free_used_btfs(struct bpf_prog_aux *aux)
kfree(aux->used_btfs);
}
-static void bpf_prog_free_deferred(struct work_struct *work)
+static void bpf_prog_free_images(struct work_struct *work)
{
struct bpf_prog_aux *aux;
int i;
+ aux = container_of(to_rcu_work(work), struct bpf_prog_aux, rwork);
+ for (i = 0; i < aux->real_func_cnt; i++) {
+ /* We can just unlink the subprog poke descriptor table as
+ * it was originally linked to the main program and is also
+ * released along with it.
+ */
+ aux->func[i]->aux->poke_tab = NULL;
+ bpf_jit_free(aux->func[i]);
+ }
+ if (aux->real_func_cnt) {
+ kfree(aux->func);
+ bpf_prog_unlock_free(aux->prog);
+ } else {
+ bpf_jit_free(aux->prog);
+ }
+}
+
+static void bpf_prog_free_deferred(struct work_struct *work)
+{
+ struct bpf_prog_aux *aux;
+ bool ksyms;
+
aux = container_of(work, struct bpf_prog_aux, work);
+ /*
+ * A prog that still runs when its last reference goes needs its symbols,
+ * by which its exception table is found. It has ended by now.
+ */
+ ksyms = aux->real_func_cnt || !list_empty(&aux->ksym.lnode);
+ bpf_prog_kallsyms_del_all(aux->prog);
#ifdef CONFIG_BPF_SYSCALL
bpf_free_kfunc_btf_tab(aux->kfunc_btf_tab);
#endif
@@ -3111,20 +3139,12 @@ static void bpf_prog_free_deferred(struct work_struct *work)
#endif
if (aux->dst_trampoline)
bpf_trampoline_put(aux->dst_trampoline);
- for (i = 0; i < aux->real_func_cnt; i++) {
- /* We can just unlink the subprog poke descriptor table as
- * it was originally linked to the main program and is also
- * released along with it.
- */
- aux->func[i]->aux->poke_tab = NULL;
- bpf_jit_free(aux->func[i]);
- }
- if (aux->real_func_cnt) {
- kfree(aux->func);
- bpf_prog_unlock_free(aux->prog);
- } else {
- bpf_jit_free(aux->prog);
- }
+ /* The symbols are looked up under rcu_read_lock() */
+ INIT_RCU_WORK(&aux->rwork, bpf_prog_free_images);
+ if (ksyms)
+ queue_rcu_work(system_percpu_wq, &aux->rwork);
+ else
+ bpf_prog_free_images(&aux->rwork.work);
}
void bpf_prog_free(struct bpf_prog *fp)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 3f215f2509b2..c8bd92c95ac0 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2461,6 +2461,8 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
btf_put(aux->btf);
+ kvfree(aux->jited_linfo);
+ kvfree(aux->linfo);
kvfree(aux->func_info);
kfree(aux->func_info_aux);
free_uid(aux->user);
@@ -2485,10 +2487,7 @@ static void __bpf_prog_put_rcu_tasks(struct rcu_head *rcu)
static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
{
- bpf_prog_kallsyms_del_all(prog);
module_put(prog->aux->mod);
- kvfree(prog->aux->jited_linfo);
- kvfree(prog->aux->linfo);
kfree(prog->aux->kfunc_tab);
kfree(prog->aux->ctx_arg_info);
if (prog->aux->attach_btf)
--
2.55.0
Technical Details
Message-ID:
<20261017012451.364800-22-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-347616-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-347616-linux-kernel-netdev=dewy.ru
From: alexei.starovoitov@gmail.com (Alexei Starovoitov)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf-next 30/38] bpf: Keep the symbols of a program until it has ended
Date: Sat, 17 Oct 2026 01:24:43 +0000
Organization: Usenet Network
Message-ID: <20261017012451.364800-22-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="2935853"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:wwCz91aToHB42hpRvOUoer9od/0= sha256:baJLIp1lHcmQwNdb+V5WOqHGRZ57b7PGmQtj0HV57h8=
sha1:oteo75OaILP4jKGETQ1XFzYHFwE= sha256:KsDhDwuixfKJ94MiZWXHv82HQ/GnUaRGTReKNnRkGPE=
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 47567110653; Sat, 17 Oct 2026 04:32:42 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -0.748
X-Spam-Status: No, score=-0.748 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, 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 X-qJg_r0JrDo;
Sat, 17 Oct 2026 04:32:39 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-347616-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
by mail.dewy.ru (Postfix) with ESMTPS id 926EA11063F; Sat, 17 Oct 2026 04:32:31 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
by tor.lore.kernel.org (Postfix) with ESMTP id 644413B956; Sat, 17 Oct 2026 01:32:30 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id E8477370AD7;
Sat, 17 Oct 2026 01:32:29 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jaocKlCH"
X-Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50])
(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 9A245231A23
for <netdev@vger.kernel.org>; Sat, 17 Oct 2026 01:32:28 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1792200749; cv=none; b=h3+XiST4mmY9Y68XVde9inccMt8HLg4ySLYYABG/rPZjoTO5ST8IW9HPmFs8Af2fM83Wi/ZlcaWWhOqTCiFcNLN6kGwX4Cq5MppKkQFIEeQBSUZRMEELKY0vWiZSTJYQKZZKmu9GFj1kBT/Y08TZqRbu5ah6BfeActTRiV2/Gj4=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1792200749; c=relaxed/simple;
bh=CbWiyrsUDJ2cbbxFhp1RZzeSZXG5JlBksA4uu/R+Ed4=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
MIME-Version; b=jsg3sDX8XJieOqZlEisLFukwWEy1VyfSOEdx7ivLDAFe5s8pes0bhzJymWBytYOmhjui6GjAG6lfO7QkTVZzoYqjpz3u1GnhgcoX6suZjD584XCiusNj6oPBJy2IBSRbtU5rr0S/26YhcRHws3omxxqLCI4JaIS+J9Yl6+6OFyg=
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=jaocKlCH; arc=none smtp.client-ip=209.85.216.50
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-pj1-f50.google.com with SMTP id 98e67ed59e1d1-3acd21ba21aso468712a91.1
for <netdev@vger.kernel.org>; Fri, 16 Oct 2026 18:32:28 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1792200748; x=1792805548; 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=eA2NZvNprPsSN5L4VbDShVJlzlwVtYc7SeTdmwQtIBw=;
b=jaocKlCHSKLJv15KFgBELGbHqL8tnRwDbHlFMLLVaCiYiHD6o1mXty0kd8WgAwEMv8
Qz4QhXzGONMFVhYK8WnIpyetgdTac+byKRdjZu2xz+VxTi3Ron/mQFL2ojsTQXGS3kgl
Gpd3oS+r8UNSG5Ve1d4k8ztdCRWVlfsTfMO+1iV4ZHBQI4GD+viUf8Mw82XQfxjGRwiM
G/3yVfKEOv+gMb4UmqpK4rjdZ/J9lrqmUHcp88QvCbLcALVfZbA777hndCpdyNEnnZ7c
K4tIF5TzbgfdhadLaOAf8n+tGy329Zj8vUhxG6VS1hO0RarivGfhr9hk1de9Dl0NOZFw
uM+g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1792200748; x=1792805548;
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=eA2NZvNprPsSN5L4VbDShVJlzlwVtYc7SeTdmwQtIBw=;
b=LMH7CSDVE4TGOIOwWw5Z10W+jl36/entFMXdOXsWMIikPilzqBTtm6PcImFt7CIFM9
Y6gHod6OuzbO9RKd6xzeHYhzoSPDxhIJBbZUEKm1XrvCGMktbJn7Z36j4e5jgnjCIZRW
MJlklxoLsAP0ZvogCF/bsEzTqA/iNuPqoGPuHXQV1sRQ/srUGlRpZINhFqjl62xO3AZI
u2tapNSF4DD4GamJSkm7Wv8Qe70D4VNnQv41Q+EEv0lAWnyJWhdzK3maQh0sDsMyYHuk
th7fYexV1e9vxgA4YlhwtxgxDLAOxtLtqmibIHu6KR/6ugs5SxfComeM13QVTlkk4afW
VtNA==
X-Forwarded-Encrypted: i=1; AKwUvBx3H/lvpLyTnGfm1TPQ1YFPPRCC6UikRdwLVZrjIp2z/2ROCHP7rLJy81zzbLMp1tC1KN85tHE=@vger.kernel.org
X-Gm-Message-State: AFq9FYIZ/mxIjSZHSz1ICy8iNOloIWTGhQ4UWy2a5/dtxAePycKJlFSE
6GNbfYoEPXhPxuzZdRA0nchhUMT9Yy6bhXVN1/vyJjBKV8POxEdCx4Xm
X-Gm-Gg: AYBFou0E5PFgLNMXPKart5iDBiGV1hh+RUVjcC2Ou6WT6hEI3+levpdlVTBZYjlCzyw
W2Lt0FxB9aYDA6/9oBuT8TQsSPJq2oOuEYe7Y2qG3N6tIeNwKkPcQ4XmGL3MuOXd72t5Uen7iLL
n4snW2H987I6nzq7XHjDQtUTKBZgh4tRJSpwOvSLszPxY4Xu7MUZELHnQsxU6w6Ti6KSFJ5sfDt
ayXXZlDceVme/3FionqGtjbtzu7HNB8rFlpbpxplLG5nJAFJHeIO0Ggb1f2nVubsFzg/vjyOkUS
OopMFnqVOBRDnNMtUhP5Y58NUhReJNH02KeLjc0Bxb5/f+lNbP83NrxqZfB51WoZ5JY0xUR6EyO
fnQfI2fXXOvK1TXUhDASppRlOSSJ4UVYPeB0qYe31jPg5+DyMLBVa40Hzv0BGoqlotGY0kCAXKy
deyCr8AsdZW+/8e1Drm4hwT6d6x73dExiBoblqif+mjPgqfenzxASmlNpVb4vKeoaU6tVMoapep
oFcsXKQdvUkoUBQMxmx+/uJIgVTwG5TMbXsiRbB80cY3bAgJ+saCl25E108yIoSVw2cehn1fjMh
2B0bSLCu+JllCyrQzLSq4GOAVlJ3+Y3N8lLxzR7Ra3o=
X-Received: by 2002:a17:90b:3148:b0:39e:6c68:c787 with SMTP id 98e67ed59e1d1-3acef60f597mr1392108a91.61.1792200747853;
Fri, 16 Oct 2026 18:32:27 -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 98e67ed59e1d1-3ac943e7548sm11370509a91.1.2026.10.16.18.32.26
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 16 Oct 2026 18:32:27 -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>