From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 17710238C1B for ; Wed, 4 Mar 2026 15:13:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772637206; cv=none; b=T1d16pXr8KlI4bfoHWdUIQwUIjK0ebnaG6dCLl7ZuoheC/+n/9Yq6sHEFmA7U+P6x7ANz+TC748fv65PLOmyOP4OqXeqUREKdqE7dsM5l9R/bcGe0enR1kDMTRmxwKXXQUhkVmU9vfOUGW7xJNj2oDLORLrIVR+pZZU1Y/mqaX0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772637206; c=relaxed/simple; bh=1YFcWlz/r9VE3Q0VDgk6jiIND8p5d7gSNbT8wjCMQgA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=KFIVT62OVi3UuHO3cTSQ0wNim6DZwqpNXa6Umfqc6U2Luyr3Jp8yqxnarhVWWpV47hc6uZk/1FAe9gd8ESIqfrOELeCgJPt7ddPGozOrsyGVSRK0hkjKcHOabdcuaZkb+o+UUU3ViF438PS6YPlXUt0mp3SoyjDbW8ZKQNoAT1w= 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=QCXhjOVI; arc=none smtp.client-ip=209.85.128.54 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="QCXhjOVI" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4834826e5a0so79505215e9.2 for ; Wed, 04 Mar 2026 07:13:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772637202; x=1773242002; darn=vger.kernel.org; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=n86dk0KwrsBwI7hT/iQAYW05mvoxAxmaULpJGzCbTNY=; b=QCXhjOVIh7s8I/9kGA8YJ1DgR7Iq3Ll8YQS3L1OUHV8sLBQ+2AZqJQJ0JjpqtxBrik E9hivaYG0ItIHUWaNLhhxL7Ol5dzTbw+4Kkb5uj3ZT1rHE3hlw973tT5w9SEtBhKG+8U fBrV9GidBwkPufxjlulijV/XBKn55uhdyaPyxjvOqyfw6Eym4/IGYB4v+NXJSfJos7Y+ u28W3D5bFuQezg752ucZoWhv6RjEYgTum1VyyQSdtFMENkMKRJUL5odzp/ARkrRN7ck7 me70WezJZy94BGm3JiOCEQiFJh42G5mnkvSI5jukDgBI19yWtXCiCFaaUFwBBm56Q+i7 DLbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772637202; x=1773242002; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=n86dk0KwrsBwI7hT/iQAYW05mvoxAxmaULpJGzCbTNY=; b=iYZfoqJ22BrYnoWi3zujMK7wmYh+P3GUky1aGK2cLuecFZteIaHxlNfjncSFL49u1v 2Y+9Yn4o47E4Olg2bHo0lNMorqAjmiOM/fCyAj631U0qXVLPnRz+6cwJpjb0ZIDQAk3E 0XurLdTv//lHlTKDavGAyOdiSWe+G/rbn4ESJRwFoGH8IXKbKa0GK/Y4BWAwxBON81FA K87nmnV2eqOnql18Q6ioH78IOFkHmC64DknRfeEy3AKq4hh7YpnLhdKHIa5eNPH7A+tn 5xlUPpUP6ZaWs2kJzB9KgRX4Xk4FZDGC/Lqvu3YcoaSrwmorreyYNy3jeQUIVjDfvXfZ cDlw== X-Forwarded-Encrypted: i=1; AJvYcCVeJpgFbyLVPa2oDTQbzspMU8mLGCtngiaiPsCbnCUk3CutgxsQMZsHG+Cj9cuOOSbjIQQIBILHB9WU4gk=@vger.kernel.org X-Gm-Message-State: AOJu0YzbSDJP6zJa7JfSjHMxGajVIY2lwDwP6WMBnUQdaUySz1RxxBkp pYY5SJVlC+RPZ/iDceVdxoOpxjBSCDskcoK34j17nhqQzBLChRdR/qfA X-Gm-Gg: ATEYQzz7BhohiXfJ1fOTsmw96ojnPnc5F8qDWRKDtqp5BT+UZZdY+BA1ESKMiuoOdNs kUqNMrPf6WnadJ0vCx0u5THsovv+GFXHFSA8NChjTnSsQKzwznxaZjYYUC23V2zYaOht5SxQdBe RpeKwGRzosfUaW/eMQQwGUE90BW8c1SjX975TTGQA774oiCS0xIeY0KiTul96orEsM64857lfBB bHnWGEhp23el3+52IRR4ZrGECuyPyIq/xLh7y4ANp+ZHHT4X/n7JmAcXIbuVWPAQUnwhICH/f6H GFS6suG1iXKR6y+bTbPUzRZhH2JmCzCXEZcam1BWj6Jo4uPbAdWt4il/GFximh3Mcc9xCmdZvU9 E4VbhNm3FerJNHhq4cwmLDuUD3Aw08kXocxdLi8UT7VD4/ZfQxnU/Mek4BkA04+/MgL2PgaSs1A FMueZzkyULk7zu/5mBcwJ8HLfj0zUmVLUk030eoQ== X-Received: by 2002:a05:600c:4452:b0:47a:935f:61a0 with SMTP id 5b1f17b1804b1-485197f6ae3mr46162695e9.0.1772637202157; Wed, 04 Mar 2026 07:13:22 -0800 (PST) Received: from localhost ([2a01:4b00:bd1f:f500:f867:fc8a:5174:5755]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-485188080edsm57073725e9.10.2026.03.04.07.13.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2026 07:13:21 -0800 (PST) From: Mykyta Yatsenko To: Chengkaitao , 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: Re: [PATCH v5 5/6] bpf: add bpf_list_is_first/last/empty kfuncs In-Reply-To: <20260304031606.43884-6-pilgrimtao@gmail.com> References: <20260304031606.43884-1-pilgrimtao@gmail.com> <20260304031606.43884-6-pilgrimtao@gmail.com> Date: Wed, 04 Mar 2026 15:13:21 +0000 Message-ID: <875x7bzkz2.fsf@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Chengkaitao writes: > 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 188840057c7d..2b9f30a36c63 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -2521,6 +2521,44 @@ __bpf_kfunc int bpf_list_add_impl(struct bpf_list_head *head, > return -EINVAL; > } > > +__bpf_kfunc bool bpf_list_is_first(struct bpf_list_head *head, struct bpf_list_node *node) > +{ > + struct list_head *h = (struct list_head *)head; > + struct bpf_list_node_kern *n = (struct bpf_list_node_kern *)node; > + > + if (unlikely(!h->next) || list_empty(h)) > + return false; Do we need this block? It sounds like a problem if we end up in the situation where node->owner points to the uninitialized head - the list is corrupted. > + > + if (READ_ONCE(n->owner) != head) > + return false; > + > + return h->next == &n->list_head; Can we reuse list_is_first() here for simplicity? > +} > + > +__bpf_kfunc bool bpf_list_is_last(struct bpf_list_head *head, struct bpf_list_node *node) > +{ > + struct list_head *h = (struct list_head *)head; > + struct bpf_list_node_kern *n = (struct bpf_list_node_kern *)node; > + > + if (unlikely(!h->next) || list_empty(h)) > + return false; > + > + if (READ_ONCE(n->owner) != head) > + return false; > + > + return h->prev == &n->list_head; Here maybe list_is_last() going to work as well? > +} > + > +__bpf_kfunc bool bpf_list_empty(struct bpf_list_head *head) > +{ > + struct list_head *h = (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) > { > @@ -4591,6 +4629,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 197b972e79a2..e97c7e11bf65 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 == special_kfunc_list[KF_bpf_list_del] || > btf_id == special_kfunc_list[KF_bpf_list_front] || > btf_id == special_kfunc_list[KF_bpf_list_back] || > - btf_id == special_kfunc_list[KF_bpf_list_add_impl]; > + btf_id == special_kfunc_list[KF_bpf_list_add_impl] || > + btf_id == special_kfunc_list[KF_bpf_list_is_first] || > + btf_id == special_kfunc_list[KF_bpf_list_is_last] || > + btf_id == special_kfunc_list[KF_bpf_list_empty]; > } > > static bool is_bpf_rbtree_api_kfunc(u32 btf_id) > @@ -13126,7 +13135,9 @@ static bool check_kfunc_is_graph_node_api(struct bpf_verifier_env *env, > ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_front_impl] || > kfunc_btf_id == special_kfunc_list[KF_bpf_list_push_back_impl] || > kfunc_btf_id == special_kfunc_list[KF_bpf_list_del] || > - kfunc_btf_id == special_kfunc_list[KF_bpf_list_add_impl]); > + kfunc_btf_id == special_kfunc_list[KF_bpf_list_add_impl] || > + kfunc_btf_id == special_kfunc_list[KF_bpf_list_is_first] || > + kfunc_btf_id == special_kfunc_list[KF_bpf_list_is_last]); > break; > case BPF_RB_NODE: > ret = (kfunc_btf_id == special_kfunc_list[KF_bpf_rbtree_remove] || > -- > 2.50.1 (Apple Git-155)