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=1796380532; b=5LQjNvtZLwmphRdFjZyMlMbdxeGM/lUeSKCa2cs12AAmhqt2C/sAHmLkPF6k5yThH7c0 M+V3WCcmtqYNDo5JtvSLWkeboChJeVLTuDUW9DoWH/fGDWd5Y/JLz+ef8w7WBl1o8rs/z BErWtRrGJ9xfAurLoENGZda6WGTLOUL4X1MVNK+8iw7PxqBxo+hAVbunajGAA2LzoupE+ YIrG71TBlEL8VgM5GcYZMb6MZJMe8R1e2+v/OnlY8wrPQMW7qZkhOuyWaG/Ifek4xA3zj Gsn3tTkwF+tY1xuIcFYkDj1y8Ei/d9D9tw64h/NDjeBL8LI3ZJxgssWtMToSuv60Sqg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380532; 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=WiqYCVjHPwL7gYxSNwuuid/yHQnJNDTfJwBiFg06Ras=; b=hhNSH7I+8ilxBYuJ52CaiQA05PuWhvv0c3V4gPVFPrHfElXWkL6UqESXd3ET/EJ0EwLx J1Lw+hEPSetHvD5PcbF+ti3MlZadmpCJbG2D0E1p5JtfuAQsSgWe2aeZBUi5iXrxsBIDe hql6WhiinIYvccau6Yt0G+a2Ke+qK0cJ2GuBwiUf4dgO7V0iPgC6n4Bdgxz9eKM1wK3Yd TxSaQoiKsLc2Uj9hZqToW9Qx+zVSV4bkJiQCuzo5SjYKljeg97Oc7FUrpYfYZj5Rf5kqO O2aD9tnXTkyhBNrkl5018WbY6+7qaOhkSQiNS2J58X9RewEsT2Mx7l6mTZG7AVA+cYw== 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=CsUH1vZf; 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 26092606A682; Fri, 4 Dec 2026 10:35:31 +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=CsUH1vZf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5841D1F000C2; Fri, 4 Dec 2026 10:35:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380531; bh=WiqYCVjHPwL7gYxSNwuuid/yHQnJNDTfJwBiFg06Ras=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CsUH1vZfQ70W/ecTCl7LFOD7aMcyYPektjVIiTrLVlZ4z0PtjSFjx30kf2bUvvT0U XYbvIyN9vxkjZfwRISqgV85LN5+38kgx9vPSwMMJkSkf5CT6JRBUcDdOqARgIadZ8u AMT0hDHHyYQEULHAq1E0grpo+A7M4wbEmsItEnRw= 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 7.3 263/742] bpf: Save irq flags on the stack when freeing a list head or rb root Date: Fri, 4 Dec 2026 19:02:07 +0900 Message-ID: <20261204100642.048433067@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-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 9100283e2935e..13b0ee50254b3 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2254,6 +2254,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)); @@ -2265,7 +2266,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) { @@ -2277,7 +2279,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; @@ -2314,19 +2317,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