From nobody Thu Apr 9 19:18:33 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 D96D435C1A9 for ; Tue, 3 Mar 2026 13:53:28 +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=1772546011; cv=none; b=c5ZGjC+9pPj3lvZTVaR7N4YcLw7LsQib8hGdGsKP8dvHW4me3gRDfjCERa1Lc67dGgYMl2ddiIkwN4vM1WNRfKK0vU/v9u8Trl5MXPgI0FAC3fXPOgrKtjL2K3MCGQYBUWXDzNMdZ4ztHyfvu/OFgKVjoBEbmekWDzJLqjfRq1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772546011; c=relaxed/simple; bh=wxWIwJi2U61Y4j1uCGoff+dbH9Ura67jjy6+JYedFtM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YEbzhVG3D/CDQDrznKieY/WwPF2nh4tAUSvtLzfDPq57wbfYmkbu5FJVwxK+nwt4EzUAIbxESjuE9yJWGadoTuckmoP5blnvbO0jW3428HsHldWXMLQeJV3blYkYF7XFjRkJePBlyL50UBZpgYQDGNUZK0JhjQDrkcc4mziT7oM= 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=iuOu1iOK; 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="iuOu1iOK" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-82735a41920so2196811b3a.2 for ; Tue, 03 Mar 2026 05:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772546008; x=1773150808; 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=H27Cg5uH76XKjjIH1pPEtY296Pssk09ysV1ForE3RSo=; b=iuOu1iOK7vL7sQ34bJQXbd9zkJvhrZfftCPOFozsM5alPXaPihU/lVGi44AIviKWK+ E1hE5dZd1Am3XWPntnUbOzLlI/67rqYaQWThPzLcj1PySt9Yw+4HIQ0wNSNDOAh1wLkI m0K4WF2+NedgGNPtbSNbKp/FJmdpoUIZSFQBHvJk068aG4JFHpz4wioc7aENPnVS1g5k df8M7Y14K2RpGq/hxmMxoWmYF67fXlqNxVaocKNvdxAJYt6/cATcS5rJ1RhOwqSvhIxJ sSrAB33xeCQFz/eAhbu3ZxDC/lSnN4kEQNTjDeaJ/v0ZMoe4NUrCbC1yWomwECjxkh17 ZmlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772546008; x=1773150808; 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=H27Cg5uH76XKjjIH1pPEtY296Pssk09ysV1ForE3RSo=; b=tG5XefIzf/dEmc+nI69ebf4mU5vPH1hSadonyntMiQ/ERSYDtezNUxKl4x7/hzY6sn BnQSmdO6hMEwIKB3mLG1sJbCBaGLVi1s0iy4UfOk1KJGwzTdbP0zpqsZLn3ebuoelhcF HFcg08qRSt9lc6RZ47JtPjVfE5/BlqDaov5icsrZLgp4jlwb2dnUOb8DbwljQobJYbLx ogYiAcdKBIAPO18RHYUPCk5PyWxnDRNqr7RCH0kt99h/6Jq0S5IwGAJfFnNLwZZTI3Dy SePlnvYfchTbq8x7nYW4mPfFFufSt3i59EpdAz+IPiWDUloDn2heCV/L4l4Pv6qVEhW9 B2Mg== X-Forwarded-Encrypted: i=1; AJvYcCXuJ7Blo9l/McCvYK6b05WQNMFtPNGjcgQX+vy+zZkUHqhyLqReXFgmSx0WQUaznh4iwVZvJ1zcC18zr/w=@vger.kernel.org X-Gm-Message-State: AOJu0Yxxuwuv9Qg/ugSV6jIwLXAZDElipg1FFbMHpiuYpqiGulqhce7O pGwjztxKrki978MeIkxi5LQhYLbxfRKcaicHwpVGg6dFjGsQY+khnbgo X-Gm-Gg: ATEYQzzYvAlp3hVzw96matFb2WM6njjCCEYany6ck8r9AZ/cAzFxMz4TKbP+wkif3QN P9B6jcSGCIOFC9JI1cpSUOzTm94PU4RRwbewv/JKqU8APTwR6zb/utK7Vh7Rrapj5qjOJUeV3rR sec2slPez/2bewnlPCkFBZbXz+Gw0rjeaAwi/oAhX+ndA7+LU2Wm0K2dNLkW/2nnTMD3I+fhqx1 dQFS2BEfLnELjO3vBpJR0Pr+1RvTi+dDkRqB4irlzNbXaNG7l2/nxv/Xz/U1pjxCIBYhdR7XpR9 Te5J+sJMUnZxGAmugjC0qc4w290YaUc4QjkF8kUd7pc5Ba4Q6+uGtkjDaaPnmtx9rZL1kKbL1Qd i1LAJgYbssMvAErm4UR4CwbqO+boRAFhYBz2aaDT6K/jtP9enL7MeLGyJJZ/XccO9sdeDHApHSr VgxiONxLKpUWu8Neoix3t/7IUo1TSJjpoq6Y0sG4S7Qwtgh/fA X-Received: by 2002:a05:6a20:db09:b0:394:6023:a0fd with SMTP id adf61e73a8af0-395c3a7110amr14894176637.29.1772546008110; Tue, 03 Mar 2026 05:53:28 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.53.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:53:27 -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 5/6] bpf: add bpf_list_is_first/last/empty kfuncs Date: Tue, 3 Mar 2026 21:52:18 +0800 Message-ID: <20260303135219.33726-6-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 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 488810da8f30..3a64a9bab06b 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2523,6 +2523,44 @@ __bpf_kfunc int bpf_list_add_impl(struct bpf_list_he= ad *head, return 0; } =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) { @@ -4593,6 +4631,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 6dfd4afff1cf..78652ccd2a89 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12465,6 +12465,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, @@ -12527,6 +12530,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) @@ -13003,7 +13009,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) @@ -13126,7 +13135,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)