From nobody Thu Apr 9 14:59:19 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.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 E74CB245005 for ; Sun, 8 Mar 2026 13:47:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772977639; cv=none; b=J9bMCY0QWZIwMoMn30fLX0T6o0WzMXnWvMvlELSAemYESh9DWEQckBAtEuPKFYEc4nQRl+COFRMIaQ2C8d87407ZRCd7HmGFQcwE08weGiE5T4wjW9ynfRVWyy6+4C08bc9IuMfGiVW/o4viyUsbEKPl4xFQD7GJMWXzftCeSWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772977639; c=relaxed/simple; bh=1eWxWto6KUk7PXfmWo0yP2+4m0Bt3mH32o0CRbZIsgk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iryHo3hzz8FXCDLnbMu07ONypP7ugi2PiNbtzNN778kIjl0yVRo3qECPl8tjtFHnjzpfE6/qq4hnhlyviAgGMJO3Rd0RJCgPxw6ib8e4sKbZg1Wfb5wFhPWjofvSQUESVjA/cO4pKFJcXRacML8prDU7ePlLzpcCZyHyOUpMrpQ= 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=SizQlioL; arc=none smtp.client-ip=209.85.210.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="SizQlioL" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8297310ce0aso2669739b3a.2 for ; Sun, 08 Mar 2026 06:47:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772977637; x=1773582437; 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=C5ogfvh3LzPgqc+SKxRTFIvNBgOyOocO0BrAiL+IkvQ=; b=SizQlioLetZYYJsXqd9lDAv4ffIprn6V7/fq3EN1oqWu4ZFdWFWW7RxPoeaU1aA0yf 3XmN7iVs7r6P1qG2XnbAVTJZNQ4WxBpuIDMXiIMxNKSQ9oilrpAeu1Jay9PC34pXStLg wDg4IW/lVtnSPbHc56B7Er4aPL9q93PfYepUEBylAvLTbh3xmkvoeAHsxsT6IIVJLeXZ Xgt8IUQm/TS2XPioA2OTTBSCwhL27utzgXeq4pjTF6b0mRrqyZWLybsFWM/xbzTa+cc9 Lwc8L9dYFuucngbAvYsCeBiuAQ6H67cZEGzpT4/33wfrxZ0rZ01eO9FEDF7b55R3se/Y 1n/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772977637; x=1773582437; 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=C5ogfvh3LzPgqc+SKxRTFIvNBgOyOocO0BrAiL+IkvQ=; b=tvcydsW2UowTiuspQo3tYM09hcEHyzC3zXeyYJEPGxpLy/9tMmCoxVztaOLIUWAgzl BiWeMjgzoKFycDuryuh5RrqaoNIfkCaYKJPvdXIAEN8GJcwe+vLW1iNGLExe5hG/yR8S DTpPuXCjGOxPNM7Ol6Cp28PbXZqSdSeJXgZ3WhbRiGeMg9csenbfIK8ZXPoGmZ/RORUM G2XvXoSBNi+x4U+YICljnURPOvUWUDwZ9dOghVwO/BbyTIEaZwHOzLx+BAm+3+W9A1w5 4/0mMUEl2iftL6gnSSw3P8cbsUvmhWfj+Fx93O1PkM1iPXX0BeDqdSev1WAFJYj+rdOV BQpg== X-Forwarded-Encrypted: i=1; AJvYcCXVMfTWvuOlHikN73C7Cl+wCJdMyAX99ACc1hzBhCHuYkmhftxCTrHooSruSGhBlSW8AbJFR4Vc7QsNMn8=@vger.kernel.org X-Gm-Message-State: AOJu0YwmVe62wCo0AwN0yaeUVpmFRuoZWbWeFuA0RIGuJY6Uv3b4k+1Z 2rcv/BqVPVe/cd08VA20ZxVl4sWFhPG9URfzJHKOFyI1+2gUT/uQz9Dv X-Gm-Gg: ATEYQzx2mmm5HjUsJZM06FXZcTn61ZeYMppwtztSEayivXa09kNPhcBss58kX2hekjW OWk/GxZWCh0Jn6uRawduBCIT3uJEXRFu1GjcU6tU33At+wszVzdX+pj/JD3lbP9PJZcO5FjqiPR l0YYj8/v3SU/15xhmZakcWsLilEUhQfMPbmNoS0bUAOiJC40S/otzY1uMvzB2tfEa2v2gspPwyT f5w5Y20KCdJtNyN0aMW3q0YxsG0A+CO1lmaetHbSf8IqVOx6l0Yf+RapetTxWeqr5IIesuM5F/W ilk8gz5xGWgQC4/wB2+Fboo3NnRiGSkBHmn3dpuzHpflKa2yIY4EZMWK6TcO4DgfnqvlUtkmcaQ vbm1UNqrYSAeC2OxsWfnZzcvYzsDIaqNlBBtpsfU6kNU1uPk3/+lTkVkoBcJrwfDt8CaWYGN/Nt 2EeSLCfPJ1WiZ45EK+87jqa00H9bzriG1T5cUl4rwt7SU/OMzh X-Received: by 2002:a05:6a00:3c8d:b0:821:81ef:5de8 with SMTP id d2e1a72fcca58-829a2dc08bbmr7422879b3a.12.1772977637188; Sun, 08 Mar 2026 06:47:17 -0700 (PDT) Received: from localhost.localdomain ([113.218.252.35]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-829a4636968sm7095056b3a.9.2026.03.08.06.47.02 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 08 Mar 2026 06:47:16 -0700 (PDT) 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 bpf-next v7 3/5] bpf: add bpf_list_is_first/last/empty kfuncs Date: Sun, 8 Mar 2026 21:46:12 +0800 Message-ID: <20260308134614.29711-4-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260308134614.29711-1-pilgrimtao@gmail.com> References: <20260308134614.29711-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 | 38 ++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 15 +++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 407520fde668..476f5ad319e2 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2515,6 +2515,41 @@ __bpf_kfunc struct bpf_list_node *bpf_list_back(stru= ct bpf_list_head *head) return (struct bpf_list_node *)h->prev; } =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 *kn =3D (struct bpf_list_node_kern *)node; + + if (READ_ONCE(kn->owner) !=3D head) + return false; + + return list_is_first(&kn->list_head, h); +} + +__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 *kn =3D (struct bpf_list_node_kern *)node; + + if (READ_ONCE(kn->owner) !=3D head) + return false; + + return list_is_last(&kn->list_head, h); +} + +__bpf_kfunc bool bpf_list_empty(struct bpf_list_head *head) +{ + struct list_head *h =3D (struct list_head *)head; + + /* If list_head was 0-initialized by map, bpf_obj_init_field wasn't + * called on its fields, so init here + */ + if (unlikely(!h->next)) + INIT_LIST_HEAD(h); + + return list_empty(h); +} + __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, struct bpf_rb_node *node) { @@ -4585,6 +4620,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 5f55b68ed935..5e32e02429c4 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12465,6 +12465,9 @@ enum special_kfunc_type { KF_bpf_list_del, KF_bpf_list_front, KF_bpf_list_back, + 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_pop_back) BTF_ID(func, bpf_list_del) BTF_ID(func, bpf_list_front) BTF_ID(func, bpf_list_back) +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_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]; + btf_id =3D=3D special_kfunc_list[KF_bpf_list_back] || + 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_add_impl] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_del]); + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_del] || + 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)