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=1796382072; b=cNWZ9d3pIucRw+Aq75pPT+KM0f+V4v0TG8iOXTuifl2l6HRuOaJijEExbFeb4qseRH7Z bC9LrlImWJgY/6v2TRvAi4QoRyJTEaSDZQSubBBUg4zCMdHpLZMiW/zev5254CtfwzGYl j5OzjfgB9TqG0l2dll2HPTEccma829aKp0HUievEbAERoGSai90m9L9Wf8qFTAppR+NZ8 5pV03D63kqjbmNJ4NoBEPElxXvQayihH1N3jp2ER4jklYO3RSktZitV0CcXS+SETiBOFG gteCSwFCMcBYvVyJQlTnwnRdwScdFVzjC0oo9up1Mfx0mierH7R4xHfhRxAqFYXMGXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382072; 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=bMrcQjqIUAUjjvqKxq1JJrAKZD8GDqd9hEJQ+l08RcQ=; b=qS5i/LCVdVC4b5clnn9YEcFuXOCtaMM3+zcNal6i8yV+tDUjTJXdBV87R4bY1G+mwFxu Y4xZIS9tWEBMPESy8ecHOCOW5CQwGkXRVcdcy1qHYTqCXMthpWv4lmQ1/mUVJzAFlM03Q D/+aZHyRMD3hFjLqESK8uau46owYQr7zziV5nHXcJfzehKRZiNLq5ch7s9Q+JKNCiw6OZ 6PJp0+1bG83yPWJQWU5Ite/hFZcN2sIpAe68u195rghFiSDKfza9fnd/BQYeHtAM4ib3x IstDYC7iizBn7BOxuDs2POfXDUEEiBc6Zrr4+muI96w638Z2DxJSM/TFihrWp3bE4PA== 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=TglJbOrM; 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 A3BC3606A322; Fri, 4 Dec 2026 11:01:12 +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=TglJbOrM Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5C7E1F000CE; Fri, 4 Dec 2026 11:01:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382072; bh=bMrcQjqIUAUjjvqKxq1JJrAKZD8GDqd9hEJQ+l08RcQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TglJbOrM7/Z9+h0KVJzLJOX3wIglJXvf7oIHHGi0I8dikE3MxTOy5IydMOdTIGkHJ PEPC2EJ8XyFUdM2KYQWkG0RlYY+FlgRPPYVs9/NqXRVDqHJvxJQsy+vCttZBLARgk3 /EK5urakWl3APPd/qjW2CYbXYo615lAqwMGekhAg= 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.18 296/703] bpf: Save irq flags on the stack when freeing a list head or rb root Date: Fri, 4 Dec 2026 19:03:15 +0900 Message-ID: <20261204100635.658663779@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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.18-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 6b52c7f87efd2..9057efcf73d01 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2128,6 +2128,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)); @@ -2139,7 +2140,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) { @@ -2151,7 +2153,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; @@ -2188,19 +2191,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