From nobody Wed Apr 15 13:16:15 2026 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 D67BB3AE194 for ; Wed, 4 Mar 2026 14:35:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634949; cv=none; b=kzbb8XaLeLpWI1SnApNSoelncyTutY5Qil191Qjva0EFerFiUJAzKwxYCXJJ6joj+O+f4AoCvdL67rf6tJtr2HgwYi2CtA96z4b1IWAfmXq/PsMqWSVOA7TnqXIX8kfmyrsPXdleUmF8o9GkYXEvTdC65iKd/Hj1scgj5P8X1lY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634949; c=relaxed/simple; bh=XL6lEjWFt+XtdR6JFV7Hk/lJSZOMN7fdGjaesaU1jQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S+ErS5V71hQP2UJQZwaOse7TnNnBkjniLDca8yQfuI9QNuneN6gzA8SIEPhjxXPYcjnTeMxplwGHRpbrZRGoOcmSMmdblkGZqeXtYUCnA1WEzaPa/l0pW24ZS8Pjq/pXx2J/tHkT74d/6fTQ6ge3YaKQMV75bgPpcG1gBUk4aIs= 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=CI+IAcat; arc=none smtp.client-ip=209.85.210.181 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="CI+IAcat" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-827390e8a3cso4493563b3a.0 for ; Wed, 04 Mar 2026 06:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772634947; x=1773239747; 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=Hw1wiE26Ls+6Ko48lXsKErvuJGGoK4sfU++ZQGgH2pk=; b=CI+IAcattnl3V7MbO3b8viTJvSgAjFblGBNTuvrjGEpMQIhH5mn0L8ZzfnrTIGZgpX UUigNXxnqvue31JU0V9qDQnkTsd4vieiBGFVVuFacXIFCdqebnkBQYdGEbmb54d/HvOS lCqao9dCKOQefCz9ApUXl6hMsBwoU0E75AY+qYs95A8zTFNk/AaS0Hdy/9/YSzln3k3n yEPgMbSm63B+bYt3VOnrzLjFfR4cKi6u1Pk/voPNryhbb9UJ5BcO3TVgNxhJgdoo/Bvt 0ZWMJopFI/f3QZR+XPmDZjwSjfZVymwQYtg+XK3deE7b5CeuMs/0+f2rw9+cZlzHyUF+ 0tAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772634947; x=1773239747; 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=Hw1wiE26Ls+6Ko48lXsKErvuJGGoK4sfU++ZQGgH2pk=; b=EsrXS03zU4X+2sU3IUwOud3DVcpgAEOJhkIyxlqNVG0zfRTJxT4UAuBi/RD6RGGp+3 wMk8w9tc3eo7QR5tPGtY+9lmDvKkR2XZDLXet3IwotzwVeFV4ZdTdG0ggFwZq4p7oqh7 wtNKLTLYD4tvOjrDnAYG9iFNcb+39aR6tFOKyhgwtTr6USqLMNA20Zk/u+ZgWUFvE6Ma pIJUKWYqR9NoHKzQg4O5iR5drKmLheQJDRXks01EUoi5WYPRnXIgrjhW4yWH1Si4RR2M xQkGHA+dLgjUp+GSZy9dVYLwqnAM64GttgvtTQrq4eHqf0FGTNBp1DjIJGlskjYsaBbA X1EQ== X-Forwarded-Encrypted: i=1; AJvYcCWPJXyNOrElBSaLb9rjYzRAR/4G9Nto68h1oexNSmHqoN/l5VuidkQpnZRYerA7tPkgQFd0x2lOOrgct5Q=@vger.kernel.org X-Gm-Message-State: AOJu0YxfMzhHAKZBW9uE/v7n0fZqwgVDv27DT4s4IshbrLe+dxGZZOmf rqjc0ckSHR/Tscs8d7n338PW3gMeyg/7409oJ6WNauudpG8sRHTk556N X-Gm-Gg: ATEYQzx+OUZuvYxE/jXzeKfzzYb/3U9m7WJonHVReIByc5MjHw5dGLuermA3vWsU+6q pj5FOhgLzNtnZUhylq0k2EnwPo6pO2kpgoPKLawVgCeOZ3LSF1+QylCD0e+0uIadvebOsVl1Hou MT3PMEHFkgqKI/gubE5Z7WND8egvglbB/ZcVQfEABDcUwFRY7oETO1HoBguSfm5a4Y5xqIBC0JT Vjg5VQsRDSD+Drndy70gh/k4e0HFIeEccKElZU9MeVG332e3GcJTBaN0H5hUux/FNybhQ8JB7zP 3eKGyp43Jwqwkj332hAmY9sSo8Y/pcK9Y94WaB+/RRWCZajZjC8HrytM4QoWVP0cAsGRrQMu7xH bhlTiM7JNrkpOFsTfAe5NWF+jWbHH6NhwAE/02Da6gRtnbguwVA2Q7fkxa7T1mOW6gLmGkoPVss ZlE1Gm3t5QThyGDK1ZcZAaWhfsTS4vu43oZzdIjACvow25XJtlzfgcHw== X-Received: by 2002:a05:6a00:9141:b0:829:7a62:6a with SMTP id d2e1a72fcca58-8297a621faamr725346b3a.22.1772634947050; Wed, 04 Mar 2026 06:35:47 -0800 (PST) Received: from localhost.localdomain ([113.218.252.150]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82739d87e4csm18470184b3a.15.2026.03.04.06.35.37 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Mar 2026 06:35: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 v6 3/5] bpf: Add bpf_list_is_edge/empty kfuncs Date: Wed, 4 Mar 2026 22:34:57 +0800 Message-ID: <20260304143459.78059-4-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304143459.78059-1-pilgrimtao@gmail.com> References: <20260304143459.78059-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_node_is_edge(head, node, is_first): true if node is the first (when is_first is true) or the last (when is_first is false) 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_edge/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 | 37 +++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 11 +++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 740b53024283..3d22b6080185 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2538,6 +2538,41 @@ __bpf_kfunc int bpf_list_add_impl(struct bpf_list_he= ad *head, return -EINVAL; } =20 +__bpf_kfunc bool bpf_list_node_is_edge(struct bpf_list_head *head, + struct bpf_list_node *node, bool is_first) +{ + struct list_head *h =3D (struct list_head *)head, *edge; + struct bpf_list_node_kern *n =3D (struct bpf_list_node_kern *)node; + + /* 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); + + if (list_empty(h)) + return false; + + if (READ_ONCE(n->owner) !=3D head) + return false; + + edge =3D is_first ? h->next : h->prev; + return edge =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 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) { @@ -4608,6 +4643,8 @@ 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_node_is_edge) +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 e458cf3b1dd1..d133d18aa0cc 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12465,6 +12465,8 @@ enum special_kfunc_type { KF_bpf_list_front, KF_bpf_list_back, KF_bpf_list_add_impl, + KF_bpf_list_node_is_edge, + KF_bpf_list_empty, KF_bpf_cast_to_kern_ctx, KF_bpf_rdonly_cast, KF_bpf_rcu_read_lock, @@ -12527,6 +12529,8 @@ 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_node_is_edge) +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 +13007,9 @@ 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_node_is_edge] || + btf_id =3D=3D special_kfunc_list[KF_bpf_list_empty]; } =20 static bool is_bpf_rbtree_api_kfunc(u32 btf_id) @@ -13126,7 +13132,8 @@ 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_node_is_edge]); 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)