From nobody Thu Apr 9 19:20:03 2026 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 4B4673909AF for ; Tue, 3 Mar 2026 13:52:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545968; cv=none; b=SXHCbgVXqiYSbPIANcdJz6Ai2230mMv5IvcguzST5zttzzftAv8PO2RWvT6jd6U63q5UK1rREOjOlsHVbnmxiwOQLR6ZpOSZRIJk3Kwg/Zoikv27Y7K4FZS/MT5u8uCLl1nysgCpZNTP1LxNUK17ngY7Rg+phYcWjk+X9X/xzR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545968; c=relaxed/simple; bh=bXOZyfrjjEJNg2zXkkRsWg+H9U/k/NIMjxFzOVXWV+0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YcqszRvlHt2GpElsXZ+MNnbgWIUm6ixe996RvDzLr9A79bSExjBWRur+a22dIkqi0zwVV6/SCp9FbDJyn9y1Hjj4vIdlVC4J7p7d2BX+Vhv+vzsQD5gXb3oqnAl5n3k9HzBuu/wyNcA7vQVLeUrDslUdZUDbJh3Z5K8pTAwJNmc= 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=izg8TOLB; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="izg8TOLB" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-8274936d2c0so5584211b3a.3 for ; Tue, 03 Mar 2026 05:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772545966; x=1773150766; 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; bh=U5WORjt7SxVQlhYT/6rzYEp8dt5s0cz4VVG01wwIAJ8=; b=izg8TOLB1yNjkO5NJ3g2uyGdEc0fIY3Giyvue25EIY/pftojzqi7WrKFao3DXMwX/8 sX7sUn9Ktm6wa7s0qPDrR3S+A6qqfH02L039X5VFNkTwbwDmNGlOCjy8Ef4t7FdrWb0R YyxFvRw1Pw1Eo3Y98bPYQ8MHBRBoC0cYn5PtAjIIl1fJ3HwjIaL8vin3cybo9GgCCGr9 neWQNP8Bom80TpCfTbcN5TFem4x5MOXA5RkT09J5XTpm343ATOzV52ZnUL72GA7tsEf9 1GeztXCc109EglgOOHhKMiC3FWlF0FiQHFA2szCLXFBSTULq3IspBhcGlsv1DsvM3CXv cgjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772545966; x=1773150766; 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; bh=U5WORjt7SxVQlhYT/6rzYEp8dt5s0cz4VVG01wwIAJ8=; b=H6R5ai2vcY7gi9fivdUtf6vfIt8ajN8UJheKYWGbTzqeEhUWAaG0HtxcopMewyTW7p mMIe4UIGryNhthwf9yP8Yf2vDFHLNyXIHSxFaKyHeaN/Iykn5M77aVpU8BkhxTMssxG1 qVq3UVHJVWToXPWSObiowc5F8+eD3AhaBr/2nI88iwF7X9lT98qZzFVxwJ0DCP5L1hBG 2rmv+q1gI9Wa2BVo5glmC/6Un9RwH2VEdDStqyJCVbFoqkFqVRiNZVwkjUrDaC9kK321 HZYnCot/hKWuo2NGOiDOh3u0juDKcF4bv0KdgijZFn813AXxA+7W1qCn8pFmNZidIKKP od0Q== X-Forwarded-Encrypted: i=1; AJvYcCVqq7FFirMfUhn6WwPicTz2xZ19zrIz6/ILIhF1ZP5cbYtqdyJrOs2wFpXCYtNDHgddP1NWV+3WuzsPMxU=@vger.kernel.org X-Gm-Message-State: AOJu0YwwpyjMmv1ysD7nzK1W4azz0dLvxqpN+UHVOF26DuRMgClaa+Zs +OdwbTyq3jn+5F3OSa+eU9CpkA9HUlkXULrB5/5l69VpGe5BFo5S1bp8 X-Gm-Gg: ATEYQzyXl01yerJ7hmm7ldd3RhWXicY65N5tQMdG2Rc8rl8jWjQmVEhOFtNlaARYqdv EeCyZOpfi94uy3Ct0AIDA6EXEnF62PjmWMaCKfpZg0u3tk/X8kLOd1vpuz0lOjY8InNO9UaXkLd M7Be41rdfHPPab1fsRpEW3h8kmdcEh0PBMM+LzA56wc4PWCEZgG/AHiOr2+a5eeatJmtlsPIh78 fUaR8EHMQg4lYfwDcXKZ/g0Q2vj+33Bl2KdVXLIZCvGaNOyVPtOpT3HLQlyM4i+uW1AVB05RIeR mkjSvZQ40q62Fbalr0orb1njaZ5Yf9Ey3gH/tEYeUdE9OFU9zNZ9C0CFBgpW3V6QXrXoNhe45eF wDChBF/2P9vZjq3BCD49mxrGvDNeYtmlM0HTy5Gfb6yyj6boincLYMEQ2WcDpERKk97o4Jd6q4k LV+pymR2pnmgzegIXN0UeXqJ/7EO45F45rGVHehtC22YSv2rrn X-Received: by 2002:a05:6a21:140a:b0:395:6f58:d4b7 with SMTP id adf61e73a8af0-395c3a0cbafmr15399033637.6.1772545966299; Tue, 03 Mar 2026 05:52:46 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.52.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:52:46 -0800 (PST) From: Chengkaitao To: martin.lau@linux.dev, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, shuah@kernel.org, chengkaitao@kylinos.cn, linux-kselftest@vger.kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/6] bpf: Introduce the bpf_list_del kfunc. Date: Tue, 3 Mar 2026 21:52:14 +0800 Message-ID: <20260303135219.33726-2-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-1-pilgrimtao@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kaitao Cheng If a user holds ownership of a node in the middle of a list, they can directly remove it from the list without strictly adhering to deletion rules from the head or tail. This is typically paired with bpf_refcount. After calling bpf_list_del, it is generally necessary to drop the reference to the list node twice to prevent reference count leaks. Signed-off-by: Kaitao Cheng --- kernel/bpf/helpers.c | 19 +++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 6eb6c82ed2ee..19d88da8e694 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2459,6 +2459,24 @@ __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(= struct bpf_list_head *head) return __bpf_list_del(head, true); } =20 +__bpf_kfunc struct bpf_list_node *bpf_list_del(struct bpf_list_head *head, + struct bpf_list_node *node) +{ + struct bpf_list_node_kern *knode =3D (struct bpf_list_node_kern *)node; + struct list_head *h =3D (void *)head; + + if (unlikely(!knode)) + return NULL; + + if (WARN_ON_ONCE(READ_ONCE(knode->owner) !=3D h)) + return NULL; + + list_del_init(&knode->list_head); + WRITE_ONCE(knode->owner, NULL); + + return node; +} + __bpf_kfunc struct bpf_list_node *bpf_list_front(struct bpf_list_head *hea= d) { struct list_head *h =3D (struct list_head *)head; @@ -4545,6 +4563,7 @@ BTF_ID_FLAGS(func, bpf_list_push_front_impl) BTF_ID_FLAGS(func, bpf_list_push_back_impl) BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_list_del, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_list_front, KF_RET_NULL) BTF_ID_FLAGS(func, bpf_list_back, KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 67c09b43a497..c9557d3fb8dd 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12461,6 +12461,7 @@ enum special_kfunc_type { KF_bpf_list_push_back_impl, KF_bpf_list_pop_front, KF_bpf_list_pop_back, + KF_bpf_list_del, KF_bpf_list_front, KF_bpf_list_back, KF_bpf_cast_to_kern_ctx, @@ -12521,6 +12522,7 @@ BTF_ID(func, bpf_list_push_front_impl) BTF_ID(func, bpf_list_push_back_impl) BTF_ID(func, bpf_list_pop_front) BTF_ID(func, bpf_list_pop_back) +BTF_ID(func, bpf_list_del) BTF_ID(func, bpf_list_front) BTF_ID(func, bpf_list_back) BTF_ID(func, bpf_cast_to_kern_ctx) @@ -12996,6 +12998,7 @@ static bool is_bpf_list_api_kfunc(u32 btf_id) btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl] || btf_id =3D=3D special_kfunc_list[KF_bpf_list_pop_front] || btf_id =3D=3D special_kfunc_list[KF_bpf_list_pop_back] || + btf_id =3D=3D special_kfunc_list[KF_bpf_list_del] || btf_id =3D=3D special_kfunc_list[KF_bpf_list_front] || btf_id =3D=3D special_kfunc_list[KF_bpf_list_back]; } @@ -13118,7 +13121,8 @@ static bool check_kfunc_is_graph_node_api(struct bp= f_verifier_env *env, switch (node_field_type) { case BPF_LIST_NODE: ret =3D (kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_front_i= mpl] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl= ]); + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl= ] || + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_del]); break; case BPF_RB_NODE: ret =3D (kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || --=20 2.50.1 (Apple Git-155)