From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796385136; b=DTU/sE+JBK8/OM/f5tpxbCTisvL7ssQmh9EIFSjpHl8UWpBLvIgH7ZM5c+mf3xAqz2Nz 0CefQsWZCt4j2xUSQhej1+90aXBpDiT2qjK7rtMqJZ4DJVmMWMW0DXueRsQVOC8rmbrJR gtPoX0ef8te37u7uaLGwE2nX3Mz776aHv8PifMvTWkXP6/fPZ4CMv+rb3AHhdHcjiMzkR SCVgLGqkTY/zQNvxaAJcyENxb/Gjqvp1or/23soIZav9f1YhITUymvVwl6GXTj3OntxE9 xOqxW0a8FY7LP7sMAVI5ZoVMkG82hyxTsSpGMti8HCdULRN4w05UhfKDFMK1OLiSUmg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385136; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=5lH5WDT6qCvHO0AObvSSRN6Sj98SYRcQQ43coFDAmUs=; b=ICZ94uEdJHko/8V0jCIynd4laevoZEbgUwCAXtwZYzhp69+A+IHsB/6lInPu6VfCir5W DUNNTOqkImN0PLE1NAmnpAZ54GpE8JEAGC/Hc9YFoSM7yANCcNQhgVpn2KWn2whGYyYs8 geVFdpPlYdKHDlKP8GFFomgwMImB0r7w594Q3BmAkVzcVfEos7qmslbCWU/emo/sclpHq acZxU3OFVTRlubckm5MjtqvQJpGsU2saqZGR6APti+OltPsz6TqFhAsAOm2fkmgYyixp3 LRRihH5iKZXDrT4Qfncr8OnzYD3jMg1NyUP96pEnQCfSrsz36xV7DU3VdRljXnX4wjw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=0Zpv240q; arc=none smtp.remote-ip=100.103.45.18 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 81FFE606A330; Fri, 4 Dec 2026 11:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=0Zpv240q Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B1E91F00A06; Fri, 4 Dec 2026 11:52:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385136; bh=5lH5WDT6qCvHO0AObvSSRN6Sj98SYRcQQ43coFDAmUs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=0Zpv240qOxewEK5tPhqeMQnPhzeUfR2AsNQgkqI8zdVYcrTu2CG597X3eWzdwF9z6 eO948E2l+cMuDctGqqF4iGf+eRi3WPTmsGotwkDF8H5RaUU9jX/Ue8vskH5rXFwkoW Bl6GcoyN1bqJMElBWJWJfU3gmwIB0poLYAFCwUiE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Josef Bacik , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.6 0433/1167] bpf: Keep the symbols of a program until it has ended Date: Fri, 4 Dec 2026 18:57:40 +0900 Message-ID: <20261204100713.294522367@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Josef Bacik [ Upstream commit 4eaf4d71b91a983489227336adcab33d8eae58a5 ] 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 Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261017230950.802052-6-alexei.starovoitov@gmail.com [ sashal: Renamed real_func_cnt to func_cnt in the new helper and the ksyms line, and resolved the free-loop conflict by taking the patch's INIT_RCU_WORK/queue_rcu_work() tail. ] Signed-off-by: Sasha Levin --- 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 d0f7dc46d43f7..1283266e02aee 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1512,6 +1512,7 @@ struct bpf_prog_aux { union { struct work_struct work; struct rcu_head rcu; + struct rcu_work rwork; }; }; diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 4ad9435c915d7..fb22830ddcee0 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2828,12 +2828,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->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->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->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 @@ -2851,20 +2879,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->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->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 9cf95263a0213..cd34ce0faa388 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2145,6 +2145,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); @@ -2154,10 +2156,7 @@ static void __bpf_prog_put_rcu(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); if (prog->aux->attach_btf) btf_put(prog->aux->attach_btf); -- 2.53.0