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=1796383690; b=KfKKPfvSamF4RFpVjwVVcKCBCmVZ4g9NK9P2cnoX4ksJPHU9TI3bM+MrHf+RzKFnFHDa Hn/j8M2Iinw+pUNLLHi29wv5AUd0TqANGlyux8R8CycRMXJL2RPypI3WXizx9g40HBAgi UWkjZqE6kGev216tjziQyPzJKz4trH2xONaCQGDI1gP/Ss9GLTZT0X/S73eSr1dedjEzD 58jG4X86SDOxCDOyUrLFO8yh/Rn1nPgP/lfMz+K/8dbVgaJoUJuGX9pazuwzfQDIfIGiy xWE5pPdyqvplj+RSnEaqj1c6ByscfDd5x9K/XZAqqvcAZEjniqzDhTUJzUdWftQBfxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383690; 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=831nLQ4TcBpbgKgZ5sA4kSRwAWaXbu3dM2R9S+yRIUE=; b=zT6RJAALZD5Wkr3mWzgG0/N8YOPvc5dU/gUiUn6gPBS8xHfwoG4ikWqaCDwgDxmesLVu 5GD+BdIgHJRQU7+0/fvVzvcyJxm1ZQgEFESOy/xjNsk+Gnhqv1ljsCpmRcvawSr1oXRhZ y5X8j664TBm/y6oV2suyIACA4QKkY5l6ZRcbVNEI8M/47+v2h2vbt7Z4xuJPpwDo2umPw VN/X+BjhaoywIM7+b2Mm/fLjIZxFRyfntAZO7tc3v+ZOEu133AjTv5qvVkkNoZq1zpg0W AaIy8zhDBE9YIAEP6r7vc6vEKApS5N/C1tCIiVFcSP0zBj9sAUPcc0rh6WLyh8Iv/0A== 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=rSO2TgNe; 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 B2752606A322; Fri, 4 Dec 2026 11:28:10 +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=rSO2TgNe Received: by smtp.kernel.org (Postfix) with ESMTPSA id 057DD1F000C2; Fri, 4 Dec 2026 11:28:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383690; bh=831nLQ4TcBpbgKgZ5sA4kSRwAWaXbu3dM2R9S+yRIUE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=rSO2TgNevby2lmjQuTq3r/c0obKwKTGemXQB+sUMv5t1XzDfcIRFeV9LADzo1hjM9 TKLYcC46Lpz4eK6Wv1HsIbSDaYvVNEwB3Slmi3qBOFWfTfYmap8K8tgikL5qHmffux LDWI5Xh/DRYYhkUXjrTXN9FrYCMrVDhRaGdRiqGc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Kumar Kartikeya Dwivedi , Sasha Levin Subject: [PATCH 6.12 289/555] bpf: Save irq flags on the stack when freeing a list head or rb root Date: Fri, 4 Dec 2026 19:05:32 +0900 Message-ID: <20261204100620.114390432@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit 81c6c8c608b6075846d3bd5b6f57958300a56eda ] bpf_spin_lock() saves the irq flags in the per-cpu variable irqsave_flags, and bpf_spin_unlock() restores them from there. One word per cpu is enough as long as a cpu takes no second bpf_spin_lock before it releases the first. bpf_list_head_free() and bpf_rb_root_free() take a second one. They lock the list or the tree that they free with __bpf_spin_lock_irqsave(), and they can run while a program holds a bpf_spin_lock of its own. bpf_list_add(), bpf_list_push_front(), bpf_list_push_back() and bpf_rbtree_add() drop the node when they fail. The drop frees a node that has no other reference, with its fields, and the node can have a list or a tree itself, like struct foo of the selftests' linked_list.h: bpf_spin_lock(&glock) irqsave_flags = on bpf_list_add(&ghead, foo, prev) prev is not in the list bpf_list_head_free(&foo->head) __bpf_spin_lock_irqsave() irqsave_flags = off bpf_spin_unlock(&glock) restores off The program returns with irqs off. Keep the flags of the two functions in a local variable, and take the lock with __bpf_spin_lock(). Both functions unlock before they drop the nodes, so nothing else runs between the save and the restore. A failed add drops the node since the commit below. Until bpf_list_add() existed, an add failed only for a node that a list or a tree still owned, with a reference of its own, so the drop freed the node only in a race with the release of that reference. bpf_list_add() fails for any node when @prev is not in the list. Fixes: d2dcc67df910 ("bpf: Migrate bpf_rbtree_add and bpf_list_push_{front,back} to possibly fail") Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi Link: https://patch.msgid.link/178849392dfb8ddf82d7ba4259be6113349bdc2e.1795550092.git.memxor@gmail.com Signed-off-by: Sasha Levin --- kernel/bpf/helpers.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 4614442745afd..af00019b53880 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2077,6 +2077,7 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head, struct bpf_spin_lock *spin_lock) { struct list_head *head = list_head, drain, *pos, *n; + unsigned long flags; BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head)); BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head)); @@ -2088,7 +2089,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head, * section, and end up doing map ops that call bpf_list_head_free for * the same map value again. */ - __bpf_spin_lock_irqsave(spin_lock); + local_irq_save(flags); + __bpf_spin_lock(spin_lock); if (!head->next || list_empty(head)) goto unlock; list_for_each_safe(pos, n, head) { @@ -2100,7 +2102,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head, } unlock: INIT_LIST_HEAD(head); - __bpf_spin_unlock_irqrestore(spin_lock); + __bpf_spin_unlock(spin_lock); + local_irq_restore(flags); while (!list_empty(&drain)) { struct bpf_list_node_kern *node; @@ -2137,19 +2140,22 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root, struct rb_root_cached orig_root, *root = rb_root; struct bpf_rb_node_kern *node; struct rb_node *pos, *n; + unsigned long flags; void *obj; BUILD_BUG_ON(sizeof(struct rb_root_cached) > sizeof(struct bpf_rb_root)); BUILD_BUG_ON(__alignof__(struct rb_root_cached) > __alignof__(struct bpf_rb_root)); - __bpf_spin_lock_irqsave(spin_lock); + local_irq_save(flags); + __bpf_spin_lock(spin_lock); orig_root = *root; bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) { node = rb_entry(pos, struct bpf_rb_node_kern, rb_node); WRITE_ONCE(node->owner, BPF_PTR_POISON); } *root = RB_ROOT_CACHED; - __bpf_spin_unlock_irqrestore(spin_lock); + __bpf_spin_unlock(spin_lock); + local_irq_restore(flags); bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) { obj = pos; -- 2.53.0