From nobody Thu Apr 9 13:32:15 2026 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 E53D437755B for ; Mon, 2 Mar 2026 12:41:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772455281; cv=none; b=qFFa6BVVnDeB4j7uB9pi+BcABnUn6RBqKrCmlJWhAj8ce3wHXEIsGc2WqEmQuIBGdX+eyVdIWOt/RVzJURo3gQtOqnuLaQre+AcBNPRuIxob6H1YVtWJ7wliTCgysa+TEWEKxm2X1Zy/XC+mYic4RzK+VyOwvGCa7N0RQ+bP1Fk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772455281; c=relaxed/simple; bh=2pc+l+8zW6c1fnoyVeTRp6x7p+qsm1qsJPaPbldYX2w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OEyu3j+/FAnt0hWenF0lMdNtdF94JEUDA4mWygO9mFrE8K7XBHs69MdSAfdp5cx+NY/LgrLLbjnF/wiPQh8aPSeYGhv85KGyeccRsbx5wWUwoIHNlmRFIpP6qmhoyEmkPUeDxoBjNkgp2w5gNEJ++2ey0/7LkCAsoHKLJlSOVn4= 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=jR9tBpKj; arc=none smtp.client-ip=209.85.215.179 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="jR9tBpKj" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-c6e1f417918so1243185a12.3 for ; Mon, 02 Mar 2026 04:41:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772455279; x=1773060079; 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=SQj9PZXDyoiFhstulTRfahJNDAtfPbHGLUSOWnLuMe0=; b=jR9tBpKj8cRU6K9RpXtRAuQEyhSV9jqj1exq8LFo4CkRZREXMMEdMU30MhOpF9Ht0z zdI8LC5yeYrOBHX9wsi3FsuAu8xIw5WHtzIZWzDlwWCmYBAwtyIukzrWsjTWipHf/ksL BYiNXVFF1AklWLUWwNOYPCqPTMA0OzYRGKENI2rvaucmiLaqjhDazhEW4IktCLAyJly/ KKyrq0DVzuyMA5Em+P8uwcbDndL+j17B1EjMvUaDsTzWcPfrtD+onmUv7kvbl++dLPXR demAhesCeF3Z3cDVWZILuWKaEjrgfdKgASPcMA2Ko7t5TyFcUpo0eKIsAv4XSyUQ+FR+ ww+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772455279; x=1773060079; 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=SQj9PZXDyoiFhstulTRfahJNDAtfPbHGLUSOWnLuMe0=; b=k9V83ec+bbxUyGMgO3rb7BU9zeNT8A76UIObyN4Qx42Yh3L4ApBnGUPwd5FX9pg8Mh 8WhwtZ11nBwdy96kcIjTh3+Zru44o9T15x0jWwIQlIba8X+mUlNIdiJ+6Rd1KRWQRfdy kikPEhWm742D/TwBhX5v9/29VIyL/hFTCHm19UfLyWgXJM4JmLliOlzSY6R8tlYxk4dV BWK199I0NCER4oum2OtYyHgdp4dAho901PWxpxCEpyW2CbYH4wpmtvwejR9fGuRJHFeP QE2ZuZhKbyhM3E0FwTq7JHRCoJjWhG2kR5bIQOJFcMPFZglIdspZhEv3B2IY1/ZAggku JuXA== X-Forwarded-Encrypted: i=1; AJvYcCWlm84iCMY37G0t+uGP2TR1qqBJYp0lAx/NdB4txVt79K8rmWfLAYX3UrMNziGrFnrM8iu1/H4e57zfW60=@vger.kernel.org X-Gm-Message-State: AOJu0YzpZDe38d8kE7XF/o3RqlegXyhzR7DGe3cFrpQ1lwWB8r9LWhit FIQvzwSReZtC2IgFd/rEpK4sN43FJx0HMbkGOHMW/EgFxaUTVc9/HFBC X-Gm-Gg: ATEYQzzcRQg6Qe7wChUQ7ptZ0/DrAovTBr1sZn2UtedQsWIb//DSCPFr8I0MbB3LP4C y9jUjsLUgx3sRdzYSvyAHyepl8MaC9XuKVnwZ2yW1p9YPqdYTw980v+hVq8/KrKPlVzRdH1tz6H HPjBwBOTfE/dfFdoY/JQtqwW/VvLgXv4WlHIJEvKORUq1s3iEui+39OgT/sIh50x9o1X6sAPV1i MQPtyce0DdVzezzdj25dHVUH4ZQHfXyhXolPSS9CWkDbitfe3R6vZ11lyKOcbjG6XNsACk7sB0K AAXom4564jQzupEgkMS2T9fTvXV51jSznFhlepaUvL7ZGlBJP6TPbyhu3QwSjbj1WudBTSPp9YF VLQSU25+70pdBweGTEiXNkyYi/BY1T46azem5VNFbZOlhUaBbm1yeW7t0UiX7Ko5KIbSaJVK2GR 2e1TpMoAkRFLOTV7msScg6AJeCMw823eG6oJZpAE7uUibPlRQC4e60jg== X-Received: by 2002:a17:903:35c6:b0:2ae:54b2:27d1 with SMTP id d9443c01a7336-2ae54b24ab8mr21528065ad.44.1772455279208; Mon, 02 Mar 2026 04:41:19 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ae3d1956absm67910225ad.14.2026.03.02.04.41.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 02 Mar 2026 04:41:18 -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 v3 5/6] bpf: add bpf_list_is_first/last/empty kfuncs Date: Mon, 2 Mar 2026 20:40:27 +0800 Message-ID: <20260302124028.82420-6-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260302124028.82420-1-pilgrimtao@gmail.com> References: <20260302124028.82420-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 Add three kfuncs for BPF linked list queries: - bpf_list_is_first(head, node): true if node is the first in the list. - bpf_list_is_last(head, node): true if node is the last in the list. - bpf_list_empty(head): true if the list has no entries. In previous versions, to implement the above functionality, it was necessary to first call bpf_list_pop_front/back to retrieve the first or last node before checking whether the passed-in node was the first or last one. After the check, the node had to be pushed back into the list using bpf_list_push_front/back, which was very inefficient. Now, with the bpf_list_is_first/last/empty kfuncs, we can directly check whether a node is the first, last, or whether the list is empty, without having to first retrieve the node. Signed-off-by: Kaitao Cheng --- kernel/bpf/helpers.c | 41 +++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 15 +++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index d212962d4ed6..ada14eca58ab 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2528,6 +2528,44 @@ __bpf_kfunc int bpf_list_add_impl(struct bpf_list_no= de *prev, return __bpf_list_add_after(p, n, meta ? meta->record : NULL, off); } =20 +__bpf_kfunc bool bpf_list_is_first(struct bpf_list_head *head, struct bpf_= list_node *node) +{ + struct list_head *h =3D (struct list_head *)head; + struct bpf_list_node_kern *n =3D (struct bpf_list_node_kern *)node; + + if (unlikely(!h->next) || list_empty(h)) + return false; + + if (READ_ONCE(n->owner) !=3D head) + return false; + + return h->next =3D=3D &n->list_head; +} + +__bpf_kfunc bool bpf_list_is_last(struct bpf_list_head *head, struct bpf_l= ist_node *node) +{ + struct list_head *h =3D (struct list_head *)head; + struct bpf_list_node_kern *n =3D (struct bpf_list_node_kern *)node; + + if (unlikely(!h->next) || list_empty(h)) + return false; + + if (READ_ONCE(n->owner) !=3D head) + return false; + + return h->prev =3D=3D &n->list_head; +} + +__bpf_kfunc bool bpf_list_empty(struct bpf_list_head *head) +{ + struct list_head *h =3D (struct list_head *)head; + + if (unlikely(!h->next)) + return true; + + return list_empty(h); +} + __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, struct bpf_rb_node *node) { @@ -4598,6 +4636,9 @@ 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_list_add_impl) +BTF_ID_FLAGS(func, bpf_list_is_first) +BTF_ID_FLAGS(func, bpf_list_is_last) +BTF_ID_FLAGS(func, bpf_list_empty) BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f5ee11779a5c..1c36b0938da7 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12454,6 +12454,9 @@ enum special_kfunc_type { KF_bpf_list_front, KF_bpf_list_back, KF_bpf_list_add_impl, + KF_bpf_list_is_first, + KF_bpf_list_is_last, + KF_bpf_list_empty, KF_bpf_cast_to_kern_ctx, KF_bpf_rdonly_cast, KF_bpf_rcu_read_lock, @@ -12516,6 +12519,9 @@ BTF_ID(func, bpf_list_del) BTF_ID(func, bpf_list_front) BTF_ID(func, bpf_list_back) BTF_ID(func, bpf_list_add_impl) +BTF_ID(func, bpf_list_is_first) +BTF_ID(func, bpf_list_is_last) +BTF_ID(func, bpf_list_empty) BTF_ID(func, bpf_cast_to_kern_ctx) BTF_ID(func, bpf_rdonly_cast) BTF_ID(func, bpf_rcu_read_lock) @@ -12992,7 +12998,10 @@ static bool is_bpf_list_api_kfunc(u32 btf_id) 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] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl]; + btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || + btf_id =3D=3D special_kfunc_list[KF_bpf_list_is_first] || + btf_id =3D=3D special_kfunc_list[KF_bpf_list_is_last] || + btf_id =3D=3D special_kfunc_list[KF_bpf_list_empty]; } =20 static bool is_bpf_rbtree_api_kfunc(u32 btf_id) @@ -13115,7 +13124,9 @@ static bool check_kfunc_is_graph_node_api(struct bp= f_verifier_env *env, 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_del] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl]); + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_is_first] || + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_is_last]); 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)