From nobody Mon Apr 13 21:40:10 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 0983239B946 for ; Wed, 4 Mar 2026 14:35:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634931; cv=none; b=kl7KTXYFqoXz3xxO0d3TTcq6QXySMib7iLT82ED1GhtewbJ3Wuk4i7X2fUvfOjr2oqAQUdTNJUgRyszUtNy88d4BfLWPiZSIW7mId62zoJk/fXwIAT+6y5Hh0qbcW8c3pqYK7mbxpoZ7o4oaLRRazvNPHhZD1iWyFwGLND20zW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634931; c=relaxed/simple; bh=Ull5jkaPX/Z44Wt7wwTBgL+Vii3TF1svoGfJwvWXAXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QGIs3+QRTlDTyKbR0Fb/RF4+P3rh8RE5L1DHjx4NBeF/NSE5b8vjgY1sE4NUTtL3SXsZsZpLt4NALh1S1558sN2GriyrHwDAy7yrmj6LKiZvAAPu8EKyeg9lvdtCQxcRPgyIKvHuidzn/naC+x05ikRVYZc7XRz5D+//lmo/fW0= 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=SGNjQN6t; arc=none smtp.client-ip=209.85.210.176 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="SGNjQN6t" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-824af5e5c81so7028819b3a.0 for ; Wed, 04 Mar 2026 06:35:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772634929; x=1773239729; 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=O4THeY1nNZA8yDYT6Sd0Re4nLCMtXKkz6T4mk8ZpRgk=; b=SGNjQN6tYyTC789dvt6FNJ+P8y4kC//8MftbLk44RJVk+7Eb/KyXVTHghtW0IDGqtx /OUMJ9c+Gat9tC+NUiF6ZUtqj+q+EanQgFpo6Tf+kQrCSisgcsMcz+uaqOTHA5lSBHht BrcM9/86+1khuFCexAVELMq6ynisVxqip7E7gMTYSBsr1qCLxdpl6/3p75vrRT8dBIxr 6yqyboQgd4fFT0CqA/zyeoVs4FpF0HXT6EEYros7DEfrK8nMZvBlCgMHEs2HhtT5XxT/ hbHU9YDAmXJgAYygce7IKlDUlcmUxM7mbEkELAWi6wPYPcG8zhOelCn2v0a2nmVHMEEL ljlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772634929; x=1773239729; 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=O4THeY1nNZA8yDYT6Sd0Re4nLCMtXKkz6T4mk8ZpRgk=; b=QNXVWX3/Svjd3QaHd5BWVMrtAVIbMVYUJlRb1xGgYkO1GtACQEcEzaltjB6Cp5Oer/ 8fay17v3Uzbv1g9DkTliF2KJE+hve1tTmZXl3aQsjCzT1NBI1GVHlqeqn+ON+cBVfRr8 14ItI23cYZLuzFwoRWNEKpYI/rAtL52Of21I5k2b5COagZRw5vus9Ny+ClY9uM5npGZ7 AaeoxPad5IcwwNDUwc4aCaPXrq9Pn4DFtqKkrkOnF9ESPIrVOdFnH0aoRtaQzjs+kwEa E59y1mlqq6ufrREgIxAZtXBTSvAtYJV37qHXVNBeFVDevSckurSv5E3sPJ+i6r1JFjL2 6KaA== X-Forwarded-Encrypted: i=1; AJvYcCUHsogjwTuYmqwfWO5cIAT+XbuUQVFoGQ1HdUCi5BzVqHCDW3GelhoaUC4KxvNrSgat3XNb05vmHJJudCc=@vger.kernel.org X-Gm-Message-State: AOJu0YwANvUCxuHemq72PmZd5jYfGfeKgAFebzq2CJqjbvchKPAe8Din ykD3fQnbbz2NFpuBRuUMFGEg0Cli0Fz+9FyOzLiW/M3PlY2NBimu+tBt X-Gm-Gg: ATEYQzzCuufq/+brQXnSlbuzznzgejesXCljeYB9nKgjYHk2K6O5rivw9Hd80NXp0lT eVOBkJsy5+sqkz48+Bivxa0TBII0A0/ii+n+bjBdpGC5k2OcRig4JLoqf1BYRJJKr7bCOwAi3Ns /k7fRxYx4q9YzNfKeJatyklmdDYebnUOKec3B1KGSx7kpsjUwUpPhmGtscQ4xRbVaI52TDh94qU COvh08Tqkyoh0Mnh3TEoQK+Myqb7WAR39yjI8vi9z3BNmLMWKTU8n2AcliPaAGm8R8ju8TQd4D3 qPbgH/OdttD5gM+Xrc8SGScBhrIGbD1qlKS6t2JlJ2OxLeLMf8vQYHIs912erPjzBWseclXOdsd Zda9J1TE1hLbkGyp7dNQuZEeL37y/pdTNdigoYnHU1TUCcDMoQtjQN1mr5fdur4it6IS4V5HEsQ psRp3Fwy7r976ZQwFRiUm43KKMvBohEzT2IHscmZwyJtWaIiJawCv/tQ== X-Received: by 2002:a05:6a00:3e02:b0:827:2792:e413 with SMTP id d2e1a72fcca58-82972a35181mr2405909b3a.17.1772634929419; Wed, 04 Mar 2026 06:35:29 -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.20 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Mar 2026 06:35:29 -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 1/5] bpf: Introduce the bpf_list_del kfunc. Date: Wed, 4 Mar 2026 22:34:55 +0800 Message-ID: <20260304143459.78059-2-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 If a user holds ownership of a node in the middle of a list, they can directly remove it from the list without strictly adhering to deletion rules from the head or tail. We have added an additional parameter bpf_list_head *head to bpf_list_del, as the verifier requires the head parameter to check whether the lock is being held. This is typically paired with bpf_refcount. After calling bpf_list_del, it is generally necessary to drop the reference to the list node twice to prevent reference count leaks. Signed-off-by: Kaitao Cheng Acked-by: Kumar Kartikeya Dwivedi --- kernel/bpf/helpers.c | 27 +++++++++++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 6eb6c82ed2ee..cc1a096a1f64 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2459,6 +2459,32 @@ __bpf_kfunc struct bpf_list_node *bpf_list_pop_back(= struct bpf_list_head *head) return __bpf_list_del(head, true); } =20 +__bpf_kfunc struct bpf_list_node *bpf_list_del(struct bpf_list_head *head, + struct bpf_list_node *node) +{ + struct bpf_list_node_kern *knode =3D (struct bpf_list_node_kern *)node; + struct list_head *h =3D (void *)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 NULL; + } + + if (unlikely(!knode)) + return NULL; + + if (WARN_ON_ONCE(READ_ONCE(knode->owner) !=3D head)) + return NULL; + + list_del_init(&knode->list_head); + WRITE_ONCE(knode->owner, NULL); + + return node; +} + __bpf_kfunc struct bpf_list_node *bpf_list_front(struct bpf_list_head *hea= d) { struct list_head *h =3D (struct list_head *)head; @@ -4545,6 +4571,7 @@ BTF_ID_FLAGS(func, bpf_list_push_front_impl) BTF_ID_FLAGS(func, bpf_list_push_back_impl) BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL) +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_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 67c09b43a497..c9557d3fb8dd 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12461,6 +12461,7 @@ enum special_kfunc_type { KF_bpf_list_push_back_impl, KF_bpf_list_pop_front, KF_bpf_list_pop_back, + KF_bpf_list_del, KF_bpf_list_front, KF_bpf_list_back, KF_bpf_cast_to_kern_ctx, @@ -12521,6 +12522,7 @@ BTF_ID(func, bpf_list_push_front_impl) BTF_ID(func, bpf_list_push_back_impl) BTF_ID(func, bpf_list_pop_front) 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_cast_to_kern_ctx) @@ -12996,6 +12998,7 @@ static bool is_bpf_list_api_kfunc(u32 btf_id) btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl] || btf_id =3D=3D special_kfunc_list[KF_bpf_list_pop_front] || 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]; } @@ -13118,7 +13121,8 @@ static bool check_kfunc_is_graph_node_api(struct bp= f_verifier_env *env, switch (node_field_type) { case BPF_LIST_NODE: 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_push_back_impl= ] || + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_del]); 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) From nobody Mon Apr 13 21:40:10 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 4F2FA39FCDD for ; Wed, 4 Mar 2026 14:35:37 +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=1772634938; cv=none; b=akheM+mHmCzLo0kKXYN77WVZS60lVvwXPKskXO/xaU96L8iwLz+xtTeL0iYEwLYSPZFce6UyCn53oMH82qImuOCthgscWMt+LY+azKhrKB62jIKnYxOC69aotmzLGhR+bD9SxVyIwDaxRK7Jozsg+7+JUIozpk8uh7yd9RsnjcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634938; c=relaxed/simple; bh=xwZ3UGx9G2sdDI6IE3+6gfQlSrbF46ymwr2NWpQcIMo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VZiz6B7CuYKM0I2IoztdVv5xJehxpZjSjXxm41OD4Livc5mmFGPVK3tL+FTzjxNGqdfsmSpgARhjvaNa4ighPpXnUwirrioXzp4DCgEvtTacVqolPwZXnIs3pVY5YB3mLhBrjUd73L1FAs9+lqB8GH2l1jHsGx9jveJ0Kk/LswU= 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=SUId7j7s; 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="SUId7j7s" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-82746ed8cdcso4432280b3a.3 for ; Wed, 04 Mar 2026 06:35:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772634937; x=1773239737; 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=7egJvwJqBHagMwboSMYPKqhuF2wpmChoASF/sFznWPI=; b=SUId7j7szEKGVEra/ItJxWvmHjLs47A62KwhKSLO4xtmnz1UbuYclNHkTJM4JIZ5ek ML+stupvmjnl3Z9NB0Y7oP6VMIR3hAbN4rQ+Ol8LR4NrhJOHdbZLkkqbRR5AdoFdWCNh 7zO9ugL6JVGB2ttgHrquUY1psRn0G//NLIQ4d72E+91P0LvK6XsYd7oSXyT3xRTUA5xd t85lvr/zDlqJnCGcYIzbGaMeVaIYUVraDHcMgHfvgyQUnzmBGUe4onB+/McqK+lyq3QK jagHcrLm5TXAO/GBjKIqrV+KsJB/5L65VI1tx1ezDpBKvJR/Bq8Ncj/dWb8usRep3PMu b49Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772634937; x=1773239737; 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=7egJvwJqBHagMwboSMYPKqhuF2wpmChoASF/sFznWPI=; b=abEpoFjMXJt0pJ4lJRBg6Q0FwIN5xkOAWrZ1dC2yUY/tjxYRgs3Ir5+NXSgARI+hEZ UwIhMPjwSdzpSIdFRJqczjFcM38RVvrFoUurd/1oRfHgZ/6XEpKJMRWoHFWu/DWzGA58 wL16dZJcoGa0HfW201IKsIsHy7bue/U2fb+mvWSJgYUzKu5TTTLWp4YO/gqvyOi2af59 CTzl8j2WYyMBd5huXHf6F52Uo5dpLKEpwk565f3YMt6WG9ePuxWUpZoGfo2UgqOTN8cN TXKRiZUWgEXPd+LJpq5vtXoiK2SMpYInnaE6hp5/DpW5hsVk2Kg95W9W8R6wWuMVt1iq P8bA== X-Forwarded-Encrypted: i=1; AJvYcCWrux+JaRDRb2tSqpsKs5I/abiDcLi4QXdj2rNTWJAxnzecT0HseBfSW+i9YtOT2qkdXE2aa4YYNRf0Bfg=@vger.kernel.org X-Gm-Message-State: AOJu0Yxx//3Yvrwt0ik8q4hyZjkHXhgcNDhkgFk+Oa6t1b2Eh3Ijk4nx SA7wwq9gj5cic+8hXV92/5pF8DzyriizBWzyPQc1P6L9FODwlUNWr6V/Timnpw== X-Gm-Gg: ATEYQzyu1WCJRLEn3ZfgwjlGLE4xI3z/HtEuWNUKq9W7V28yJArcouTEqf2Tk676N6u S0YbU0D3UKkj7MAZPD10KlGE8AKFg1cv1v6+FxwRUnWlsmqN6VxthB7f7A3n3DuD3qWKIM0W9bn bg2zQeDa6Ni5WO9QjFe0IfndPwPyevfqL+4Z7r0hy/1dtvg9cTgmvVUOz0zROxc7t1LEUiArQFM bhF93GJFGBDXjsp//LK32iuVwW9659u1691eUO5yfUJBQmrCF56LJUEERU/F5kxITp1g/Q3gR52 OkfBw1jwWwbHwHEVsgtrrHO5mbLeYZJAWKZxk41JsO5m2TVoce1bt1NrJsFgxpnEX9O1pURFfAg cYk5ySNLr8KjMhfjNEezCfI4NuOmm6W3yeoYE753InyvRMIDO8XSNQB8aK5xB63knJvea49475R pe4Nzr0WpDfSyr/itFeSvM7DZjuiyn2/xH/qMrBzkujWsiWUWf8U1Z7UoIe1u0mrfS X-Received: by 2002:a05:6a00:4393:b0:827:3e19:574b with SMTP id d2e1a72fcca58-82972c9f980mr1726978b3a.43.1772634936549; Wed, 04 Mar 2026 06:35:36 -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.30 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Mar 2026 06:35:36 -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 2/5] bpf: Add bpf_list_add_impl to insert node after a given list node Date: Wed, 4 Mar 2026 22:34:56 +0800 Message-ID: <20260304143459.78059-3-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 a new kfunc bpf_list_add_impl(head, new, prev, meta, off) that inserts 'new' after 'prev' in the BPF linked list. Both must be in the same list; 'prev' must already be in the list. The new node must be an owning reference (e.g. from bpf_obj_new); the kfunc consumes that reference and the node becomes non-owning once inserted. We have added an additional parameter bpf_list_head *head to bpf_list_add_impl, as the verifier requires the head parameter to check whether the lock is being held. Returns 0 on success, -EINVAL if 'prev' is not in a list or 'new' is already in a list (or duplicate insertion). On failure, the kernel drops the passed-in node. Signed-off-by: Kaitao Cheng --- kernel/bpf/helpers.c | 34 ++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 17 +++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cc1a096a1f64..740b53024283 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2505,6 +2505,39 @@ __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 int bpf_list_add_impl(struct bpf_list_head *head, + struct bpf_list_node *new, + struct bpf_list_node *prev, + void *meta__ign, u64 off) +{ + struct bpf_list_node_kern *kn =3D (void *)new, *kp =3D (void *)prev; + struct btf_struct_meta *meta =3D meta__ign; + struct list_head *n =3D &kn->list_head, *p =3D &kp->list_head; + struct list_head *h =3D (void *)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); + goto fail; + } + + if (WARN_ON_ONCE(READ_ONCE(kp->owner) !=3D head)) + goto fail; + + if (cmpxchg(&kn->owner, NULL, BPF_PTR_POISON)) + goto fail; + + list_add(n, p); + WRITE_ONCE(kn->owner, head); + return 0; + +fail: + __bpf_obj_drop_impl((void *)n - off, meta ? meta->record : NULL, false); + return -EINVAL; +} + __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, struct bpf_rb_node *node) { @@ -4574,6 +4607,7 @@ BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF= _RET_NULL) 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_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 c9557d3fb8dd..e458cf3b1dd1 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12464,6 +12464,7 @@ enum special_kfunc_type { KF_bpf_list_del, KF_bpf_list_front, KF_bpf_list_back, + KF_bpf_list_add_impl, KF_bpf_cast_to_kern_ctx, KF_bpf_rdonly_cast, KF_bpf_rcu_read_lock, @@ -12525,6 +12526,7 @@ 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_add_impl) BTF_ID(func, bpf_cast_to_kern_ctx) BTF_ID(func, bpf_rdonly_cast) BTF_ID(func, bpf_rcu_read_lock) @@ -13000,7 +13002,8 @@ 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_add_impl]; } =20 static bool is_bpf_rbtree_api_kfunc(u32 btf_id) @@ -13122,7 +13125,8 @@ static bool check_kfunc_is_graph_node_api(struct bp= f_verifier_env *env, case BPF_LIST_NODE: 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_del] || + kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl]); break; case BPF_RB_NODE: ret =3D (kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || @@ -14264,6 +14268,7 @@ static int check_kfunc_call(struct bpf_verifier_env= *env, struct bpf_insn *insn, =20 if (meta.func_id =3D=3D special_kfunc_list[KF_bpf_list_push_front_impl] || meta.func_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl] || + meta.func_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || meta.func_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl]) { release_ref_obj_id =3D regs[BPF_REG_2].ref_obj_id; insn_aux->insert_off =3D regs[BPF_REG_2].off; @@ -23230,13 +23235,17 @@ static int fixup_kfunc_call(struct bpf_verifier_e= nv *env, struct bpf_insn *insn, *cnt =3D 3; } else if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_= impl] || desc->func_id =3D=3D special_kfunc_list[KF_bpf_list_push_front_impl] = || + desc->func_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || desc->func_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl]) { struct btf_struct_meta *kptr_struct_meta =3D env->insn_aux_data[insn_idx= ].kptr_struct_meta; int struct_meta_reg =3D BPF_REG_3; int node_offset_reg =3D BPF_REG_4; =20 - /* rbtree_add has extra 'less' arg, so args-to-fixup are in diff regs */ - if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl]) { + /* list/rbtree_add_impl have an extra arg (prev/less), + * so args-to-fixup are in different regs. + */ + if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_list_add_impl] || + desc->func_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl]) { struct_meta_reg =3D BPF_REG_4; node_offset_reg =3D BPF_REG_5; } --=20 2.50.1 (Apple Git-155) From nobody Mon Apr 13 21:40:10 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) From nobody Mon Apr 13 21:40:10 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 2D7243A63E4 for ; Wed, 4 Mar 2026 14:35:58 +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=1772634959; cv=none; b=MITaFUSy/NaOo0HDSR7udrpu/z/a5Vh0ubWx3z52xCwZ2cKNbB+sSJ8XoJH/P00021+2LTaybJyhSFTp6YuINMn4ubtlaAFH9paoC1UwRpMzmDfdvW4vK6YizXpWeAj6llxhAyh6w9Y1uLaEfaSzL5AtWKSV80u8oliqnwEQhd4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634959; c=relaxed/simple; bh=N4SSyL9EehPGbdPfqVp1P6qfUSFqFa1GZFzsACG9sAI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PmHjx30ppjnraArn2YhEgk9WGB4R/CeYjb1Wg5IjR2imwcJIVrKQEOxHjnt6P4g+imKUuDZFm56gsiaK51a3o6CV6T5PAhW0oGxWAxVniiobLJLMN0H2mN6GWpzpE0KBIYfCrBsN8bteDHOVVhXGkygKrlQ5ZZIIy4tclFVwb7A= 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=m4NvlTcJ; 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="m4NvlTcJ" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-826c49b7628so4399632b3a.0 for ; Wed, 04 Mar 2026 06:35:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772634957; x=1773239757; 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=QNOcpRqny/ZGYzfD6jguHPYGCE71ZgAVAdOs/NQGLEk=; b=m4NvlTcJT5/gDnbzYixF3kL7dAyoN/0rUom+ah7D/EdspqiK9XUzNSHMjdw1+DYNY0 MjZf29u0qKbLinF88wVXLoB5Zil8joGo/LShS29RnlJUIzBIzc+3917w2DEiPkelEy9+ yzq88jJHezhXpYJu6sCQURIAVCt4LCdcernZ+dxR8Pg56CQWNUtSmsmDRDs43axNuisn EzTyYpEvpITma2ReyBcmHTL1cmKA6BKUQsnj6B/2qFoXtVF2dH5qqJxNRGBfXjRVCRtY /qDzqL08tf8VGXMmtiVGZ7GtzJkbmIzAsZ7er/bAuJhs59gbJ05bmbLAEuh+MBoIUHNJ ceDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772634957; x=1773239757; 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=QNOcpRqny/ZGYzfD6jguHPYGCE71ZgAVAdOs/NQGLEk=; b=D04B3jYH+n4Z3Huju2HLV3kt9LteaRdXZwccO3CeyCsbaC9HcgenDpv8rFTfFbBqQ1 JFpiskPVp68Nt80Q1zwwZpwxv5wYhaLhHmIoex9DPa1T+F4SQUiDCOwtDy/yZ9Hj41V9 8pGEVgpk4C2C4Dm5auv9PgRrNhskZ8RMmrvK4T/okcaFnZSvgUDn2M4j/1sdOJuTV6dJ /E+Vb9wDeSAOC3qa+QeX096pfeTdv1q56D5Z83lZjawZUG6/H88H6u4vLW/r/IoYTJRu OmLFaHfiBY9Yjquqm6z6cYXYE889W0ENEy8gOMPH5Jo2XNMCCZ2kj8VoKDuBLmxe8sc5 piUQ== X-Forwarded-Encrypted: i=1; AJvYcCXyZ1JgStp4Nv/5cRMFuZXZ5C8wrxMgmcad5Ps6W4Gbtf0DidZFK1a7QXImPX7BEY1H+bTXEU4PHI60i+k=@vger.kernel.org X-Gm-Message-State: AOJu0Yy8t5RSw9VtOv1oUuo4GnPxdSpeCM9XdXTzFBe11eaiOX35bYc/ y/aDuLnCCovtF0BGzMpzFbkpy7iHEFkdDDzgri0Pg1YO4KzG2hGPH9gs X-Gm-Gg: ATEYQzzA3lMGcFeXwLFVb5CLoy830Ler7F3ACkVTZ5ZG5nvQbw13SfaobsDPYl4BHUH z4TqDu+A5wxgomijhIUVzUSWsrSRD1x6ESvB5wmroFJ81sz+W6aozsLv00jOCk+/fjR1nX4Ad/O kJoxmigwSpo97AAwsGgLPYze9QdTYvWcvkMiWHfVcDTNhPrBWQLBzzz4kB0/zrro5gNH/ZR0CcG xGRu3YSgBBRb6e8tO1OjCcVH+w9oPyTE4XQzPkKHzqpPG9yf6UshAk0hwMA57xnQZmsv+mLgv2i 1xdia3yc+HYy0kJ2L4aLg/MGs+0GPUjDCDa3ASDCgDf2kJcCo3seeeQkp+6/SRSX2QNxkFIPfmr yITvOzrCtDOLbgnd5PytPaUBKlbXCTmVMAMnDPPa2i++GQ2VzeCrwaR5fimCkAiaLgUl2/J4Hqs Fb59l+flejeHX6IG4KK47knwD1Q7zFFe6kMbG7LCs2W9xWsYOt8efEIQ== X-Received: by 2002:a05:6a00:39a7:b0:823:12cb:f5d1 with SMTP id d2e1a72fcca58-8297292e77dmr1929411b3a.6.1772634957382; Wed, 04 Mar 2026 06:35:57 -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.47 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Mar 2026 06:35:56 -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 4/5] selftests/bpf: Add test cases for bpf_list_del/add/is_edge/empty Date: Wed, 4 Mar 2026 22:34:58 +0800 Message-ID: <20260304143459.78059-5-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 Extend the refcounted_kptr test: add a node to both an rbtree and a list, retrieve the node from the rbtree to obtain the node pointer, then add a new node after the first in the list, and finally use bpf_list_del to remove both nodes. The test asserts that the list is non-empty after insert, asserts the first and last nodes after bpf_list_add, and asserts that the list is empty after removing both nodes. To verify the validity of bpf_list_del/add, the test also expects the verifier to reject calls to bpf_list_del/add made without holding the spin_lock. Signed-off-by: Kaitao Cheng --- .../testing/selftests/bpf/bpf_experimental.h | 39 ++++ .../selftests/bpf/progs/refcounted_kptr.c | 182 ++++++++++++++++++ 2 files changed, 221 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing= /selftests/bpf/bpf_experimental.h index 4b7210c318dd..d5f42ed69166 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -99,6 +99,45 @@ extern struct bpf_list_node *bpf_list_pop_front(struct b= pf_list_head *head) __ks */ extern struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head)= __ksym; =20 +/* Description + * Remove 'node' from the BPF linked list with head 'head'. + * The node must be in the list. Caller receives ownership of the + * removed node and must release it with bpf_obj_drop. + * Returns + * Pointer to the removed bpf_list_node, or NULL if 'node' is NULL + * or not in the list. + */ +extern struct bpf_list_node *bpf_list_del(struct bpf_list_head *head, + struct bpf_list_node *node) __ksym; + +/* Description + * Insert 'new' after 'prev' in the BPF linked list with head 'head'. + * The bpf_spin_lock protecting the list must be held. 'prev' must already + * be in that list; 'new' must not be in any list. The 'meta' and 'off' + * parameters are rewritten by the verifier, no need for BPF programs to + * set them. + * Returns + * 0 on success, -EINVAL if head is NULL, prev is not in the list with hea= d, + * or new is already in a list. + */ +extern int bpf_list_add_impl(struct bpf_list_head *head, struct bpf_list_n= ode *new, + struct bpf_list_node *prev, void *meta, __u64 off) __ksym; + +/* Convenience macro to wrap over bpf_list_add_impl */ +#define bpf_list_add(head, new, prev) bpf_list_add_impl(head, new, prev, N= ULL, 0) + +/* Description + * Return true if 'node' is the first (when 'is_first' is true) or the last + * (when 'is_first' is false) node in the list with head 'head'. + */ +extern bool bpf_list_node_is_edge(struct bpf_list_head *head, + struct bpf_list_node *node, bool is_first) __ksym; + +/* Description + * Return true if the list with head 'head' has no entries. + */ +extern bool bpf_list_empty(struct bpf_list_head *head) __ksym; + /* Description * Remove 'node' from rbtree with root 'root' * Returns diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr.c b/tools/te= sting/selftests/bpf/progs/refcounted_kptr.c index 1aca85d86aeb..aca201f9fb56 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c @@ -367,6 +367,188 @@ long insert_rbtree_and_stash__del_tree_##rem_tree(voi= d *ctx) \ INSERT_STASH_READ(true, "insert_stash_read: remove from tree"); INSERT_STASH_READ(false, "insert_stash_read: don't remove from tree"); =20 +/* Insert one node in tree and list, remove it from tree, add a second node + * after it with bpf_list_add, check bpf_list_node_is_edge/empty, then + * remove both nodes from list via bpf_list_del. + */ +SEC("tc") +__description("list_add_del_and_check: test bpf_list_add/del/is_first/is_l= ast/empty") +__success __retval(0) +long list_add_del_and_check(void *ctx) +{ + long err =3D 0; + struct bpf_rb_node *rb; + struct bpf_list_node *l, *l_1; + struct node_data *n, *n_1, *m_1; + + err =3D __insert_in_tree_and_list(&head, &root, &lock); + if (err) + return err; + + bpf_spin_lock(&lock); + if (bpf_list_empty(&head)) { + bpf_spin_unlock(&lock); + return -7; + } + + rb =3D bpf_rbtree_first(&root); + if (!rb) { + bpf_spin_unlock(&lock); + return -4; + } + + rb =3D bpf_rbtree_remove(&root, rb); + bpf_spin_unlock(&lock); + if (!rb) + return -5; + + n =3D container_of(rb, struct node_data, r); + n_1 =3D bpf_obj_new(typeof(*n_1)); + if (!n_1) { + bpf_obj_drop(n); + return -1; + } + m_1 =3D bpf_refcount_acquire(n_1); + if (!m_1) { + bpf_obj_drop(n); + bpf_obj_drop(n_1); + return -1; + } + + bpf_spin_lock(&lock); + if (bpf_list_add(&head, &n_1->l, &n->l)) { + bpf_spin_unlock(&lock); + bpf_obj_drop(n); + bpf_obj_drop(m_1); + return -8; + } + + if (!bpf_list_node_is_edge(&head, &n->l, true) || + !bpf_list_node_is_edge(&head, &m_1->l, false)) { + bpf_spin_unlock(&lock); + bpf_obj_drop(n); + bpf_obj_drop(m_1); + return -9; + } + + l =3D bpf_list_del(&head, &n->l); + l_1 =3D bpf_list_del(&head, &m_1->l); + bpf_spin_unlock(&lock); + bpf_obj_drop(n); + bpf_obj_drop(m_1); + + if (l) + bpf_obj_drop(container_of(l, struct node_data, l)); + else + err =3D -6; + + if (l_1) + bpf_obj_drop(container_of(l_1, struct node_data, l)); + else + err =3D -6; + + bpf_spin_lock(&lock); + if (!bpf_list_empty(&head)) + err =3D -7; + bpf_spin_unlock(&lock); + return err; +} + +SEC("?tc") +__failure __msg("bpf_spin_lock at off=3D32 must be held for bpf_list_head") +long list_del_without_lock_fail(void *ctx) +{ + struct bpf_rb_node *rb; + struct bpf_list_node *l; + struct node_data *n; + + bpf_spin_lock(&lock); + rb =3D bpf_rbtree_first(&root); + if (!rb) { + bpf_spin_unlock(&lock); + return -4; + } + + rb =3D bpf_rbtree_remove(&root, rb); + bpf_spin_unlock(&lock); + if (!rb) + return -5; + + n =3D container_of(rb, struct node_data, r); + l =3D bpf_list_del(&head, &n->l); + bpf_obj_drop(n); + if (!l) + return -6; + + bpf_obj_drop(container_of(l, struct node_data, l)); + return 0; +} + +SEC("?tc") +__failure __msg("bpf_spin_lock at off=3D32 must be held for bpf_list_head") +long list_add_without_lock_fail(void *ctx) +{ + long err =3D 0; + struct bpf_rb_node *rb; + struct bpf_list_node *l, *l_1; + struct node_data *n, *n_1, *m_1; + + err =3D __insert_in_tree_and_list(&head, &root, &lock); + if (err) + return err; + + bpf_spin_lock(&lock); + rb =3D bpf_rbtree_first(&root); + if (!rb) { + bpf_spin_unlock(&lock); + return -4; + } + + rb =3D bpf_rbtree_remove(&root, rb); + bpf_spin_unlock(&lock); + if (!rb) + return -5; + + n =3D container_of(rb, struct node_data, r); + n_1 =3D bpf_obj_new(typeof(*n_1)); + if (!n_1) { + bpf_obj_drop(n); + return -1; + } + m_1 =3D bpf_refcount_acquire(n_1); + if (!m_1) { + bpf_obj_drop(n); + bpf_obj_drop(n_1); + return -1; + } + + /* Intentionally no lock: verifier should reject bpf_list_add without loc= k */ + if (bpf_list_add(&head, &n_1->l, &n->l)) { + bpf_obj_drop(n); + bpf_obj_drop(m_1); + return -8; + } + + bpf_spin_lock(&lock); + l =3D bpf_list_del(&head, &n->l); + l_1 =3D bpf_list_del(&head, &m_1->l); + bpf_spin_unlock(&lock); + bpf_obj_drop(n); + bpf_obj_drop(m_1); + + if (l) + bpf_obj_drop(container_of(l, struct node_data, l)); + else + err =3D -6; + + if (l_1) + bpf_obj_drop(container_of(l_1, struct node_data, l)); + else + err =3D -6; + + return err; +} + SEC("tc") __success long rbtree_refcounted_node_ref_escapes(void *ctx) --=20 2.50.1 (Apple Git-155) From nobody Mon Apr 13 21:40:10 2026 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 0EF273B5823 for ; Wed, 4 Mar 2026 14:36:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634968; cv=none; b=g+Bav1OsNl8aH0HvFzMPK1qOvrZl+PmeZreA87WYOgKJWA7FRB05S+Gt+maJpKf/s7OeWPlomz6KH2FA2hN1VE27foOa/xb7lv5LKdC48UM/Ho/yb+WCcnui5d1eP5IQYBPZ8epV0QkkEx0TMNQkTvwgdBEYDSmUxFymA9920PM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772634968; c=relaxed/simple; bh=BqZXOAJGIKKd92V/RAYQDxNC6AlkoRpcSBFvvTkyrIw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lo/mi2AtT8StmdpdwjDOMp1n+IyXtFh3B5N+KByU1lzSMS/vStbxNTc9XjHdqnbuS49jlVO72VjDMhTg/2N3Cv0Ta+WpEInEHL+IoYyos4FWj0rS94L3xchtNBKRSyDI/r5zguOwNFuL31ddFru5yIG/dK6mGuhokVaVHrLEs8w= 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=YFT0B1eC; arc=none smtp.client-ip=209.85.210.177 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="YFT0B1eC" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-824c9da9928so4182624b3a.3 for ; Wed, 04 Mar 2026 06:36:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772634966; x=1773239766; 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=dc9SHoQTccjHJ9ny8JNU6itOYKrSyJ60RaW6+NKZF+0=; b=YFT0B1eCFTamUd2fHrTmd82K0Hg2jWgof/dUlkj1aGV9skjBGMqkGCuXJHSTngo7z6 a+wrR78YQdIeLBJuK03VNpd3nXYZZwf2pnwyMOLCb/TspsdtphpCh6C6sJhyDlizWuHX XM95FTocNPIQ5J3KPsoKzjTAMLpVbhdNKa4KzvLKi7d77B8nLlxc8O9yrbtQLnyCUgKj 8cGmP4WRTxYYbRNfnkSLCAGyCbsTye827oSsLu4VV4R+1VRn2BdKWefoFfJYVYkYVnJG 72DyQlebzp/Ye9Ds9q7CA2Iizmk1jQNBDCA+eoL41gNzADsaEcpAeDZkiEyZTozG/SUO ooaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772634966; x=1773239766; 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=dc9SHoQTccjHJ9ny8JNU6itOYKrSyJ60RaW6+NKZF+0=; b=WyqIBWHfFggnbeKrV8BmzjllEPvfs0ro/MyZUKDg2Qv5xr2WRAl2T3X0tLSPoEMkrI 4pL5bCkOBBGexUTyqLdDuPN0oZAIKavwqkCAsvTIm8MIVEVgWb0/H9D/Wq6ToG/o9Hav aPU4NqaDBbK8YqSeFJkq1jbUk8wBlQvR9PpmbRhbrA6DFwdU7gSujercmT0ZDxC4PPIG 8FCmH7wzYL8NfQ6SzzzeNautoq4oIqqYACUL1hyMTFHMcJe0jvlAbUWj2ahh+7bvdh+J Xe92Yoj4CqQa1OFET9eEi5c+761ILF/aH1ayMnkECxLHEj+VV2iMgu05ZJmER/pptBBA djAg== X-Forwarded-Encrypted: i=1; AJvYcCVKFn7g5UWjSS9fLyuX8MhxSYopF1Uafebq6CZo6WEVvNKQ5wgmKM4qICoDsLQs0jTr59nZxxxmu4R+RnE=@vger.kernel.org X-Gm-Message-State: AOJu0YzRdzlTyJy9JJBP8SLKbyis2bc3KkX8Ng/1V6fmDRQANqMwILT4 7ao356VsTKGyZLmd/LfWIkiLzQ8vjss5DeDPpqNw1XfrlKCwRYESDr2I X-Gm-Gg: ATEYQzwwOswnFKq1DLbIN8zVKEvPfzq/j2wfxOlyKpLq31c7h98dlz8bJxscA3fDn51 14HuiHTRi9YA7EBfSsLmIxJ0DuxtrQcaxvi11FJ8SXz/nGkw4Sfyi4pZ7n9CKE+nsW5BLrLA9DS /9G1lWtMzG/fhnHiS22aBAllYado2Xbg7/H3JJ3gBf/U6mkrLPwT/SmFOKlkEo3u1qHiRkjfXu1 /1wPYmXLy4aFqOYKcOjAuyCOUWk5fqUvm1DLPTADk4mRJYVPi3m+60+jVxoPYhEBQeoe2vnUlXP pWawurkRV3EGkNXBd3P20jNQTswHfw1tyzoSCdrZv6CAKNlIuDlTurKdxc3rngKI3tz2cn0gvyd OaiaKUtwYZQ7g9WIqYS4A/6GTdFnZsfNzkMn10GdQ4x7LtYAHl5ryUr13IsfCiMqFPZyVqpUr3v gwUZU3ZRN6rowPvngTy9EOzO+5dZIsuuPnM9/HUmy5ise2YT5CStYiog== X-Received: by 2002:a05:6a00:218b:b0:824:9451:c1e9 with SMTP id d2e1a72fcca58-82972df1b4amr2144267b3a.62.1772634966159; Wed, 04 Mar 2026 06:36:06 -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.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Mar 2026 06:36:05 -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 5/5] bpf/verifier: refactor kfunc checks to table-driven approach Date: Wed, 4 Mar 2026 22:34:59 +0800 Message-ID: <20260304143459.78059-6-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 Replace per-kfunc btf_id chains in list/rbtree/res_lock and graph node checks with btf_id_in_kfunc_table() and static kfunc tables for easier maintenance. Signed-off-by: Kaitao Cheng --- kernel/bpf/verifier.c | 93 +++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d133d18aa0cc..25961cf83a67 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12600,6 +12600,60 @@ BTF_ID(func, bpf_session_is_return) BTF_ID(func, bpf_stream_vprintk) BTF_ID(func, bpf_stream_print_stack) =20 +static const enum special_kfunc_type bpf_list_api_kfuncs[] =3D { + KF_bpf_list_push_front_impl, + KF_bpf_list_push_back_impl, + KF_bpf_list_pop_front, + KF_bpf_list_pop_back, + KF_bpf_list_del, + KF_bpf_list_front, + KF_bpf_list_back, + KF_bpf_list_add_impl, + KF_bpf_list_node_is_edge, + KF_bpf_list_empty, +}; + +/* Kfuncs that take a list node argument (bpf_list_node *). */ +static const enum special_kfunc_type bpf_list_node_api_kfuncs[] =3D { + KF_bpf_list_push_front_impl, + KF_bpf_list_push_back_impl, + KF_bpf_list_del, + KF_bpf_list_add_impl, + KF_bpf_list_node_is_edge, +}; + +/* Kfuncs that take an rbtree node argument (bpf_rb_node *). */ +static const enum special_kfunc_type bpf_rbtree_node_api_kfuncs[] =3D { + KF_bpf_rbtree_remove, + KF_bpf_rbtree_add_impl, + KF_bpf_rbtree_left, + KF_bpf_rbtree_right, +}; + +static const enum special_kfunc_type bpf_rbtree_api_kfuncs[] =3D { + KF_bpf_rbtree_add_impl, + KF_bpf_rbtree_remove, + KF_bpf_rbtree_first, + KF_bpf_rbtree_root, + KF_bpf_rbtree_left, + KF_bpf_rbtree_right, +}; + +static const enum special_kfunc_type bpf_res_spin_lock_kfuncs[] =3D { + KF_bpf_res_spin_lock, + KF_bpf_res_spin_unlock, + KF_bpf_res_spin_lock_irqsave, + KF_bpf_res_spin_unlock_irqrestore, +}; + +static bool btf_id_in_kfunc_table(u32 btf_id, const enum special_kfunc_typ= e *kfuncs, int n) +{ + for (int i =3D 0; i < n; i++) + if (btf_id =3D=3D special_kfunc_list[kfuncs[i]]) + return true; + return false; +} + static bool is_task_work_add_kfunc(u32 func_id) { return func_id =3D=3D special_kfunc_list[KF_bpf_task_work_schedule_signal= ] || @@ -13000,26 +13054,14 @@ static int check_reg_allocation_locked(struct bpf= _verifier_env *env, struct bpf_ =20 static bool is_bpf_list_api_kfunc(u32 btf_id) { - return btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_front_impl] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_push_back_impl] || - btf_id =3D=3D special_kfunc_list[KF_bpf_list_pop_front] || - 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_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]; + return btf_id_in_kfunc_table(btf_id, bpf_list_api_kfuncs, + ARRAY_SIZE(bpf_list_api_kfuncs)); } =20 static bool is_bpf_rbtree_api_kfunc(u32 btf_id) { - return btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_first] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_root] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_left] || - btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_right]; + return btf_id_in_kfunc_table(btf_id, bpf_rbtree_api_kfuncs, + ARRAY_SIZE(bpf_rbtree_api_kfuncs)); } =20 static bool is_bpf_iter_num_api_kfunc(u32 btf_id) @@ -13037,10 +13079,8 @@ static bool is_bpf_graph_api_kfunc(u32 btf_id) =20 static bool is_bpf_res_spin_lock_kfunc(u32 btf_id) { - return btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_lock] || - btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_unlock] || - btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_lock_irqsave] || - btf_id =3D=3D special_kfunc_list[KF_bpf_res_spin_unlock_irqrestore= ]; + return btf_id_in_kfunc_table(btf_id, bpf_res_spin_lock_kfuncs, + ARRAY_SIZE(bpf_res_spin_lock_kfuncs)); } =20 static bool is_bpf_arena_kfunc(u32 btf_id) @@ -13129,17 +13169,12 @@ static bool check_kfunc_is_graph_node_api(struct = bpf_verifier_env *env, =20 switch (node_field_type) { case BPF_LIST_NODE: - 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_node_is_edge]); + ret =3D btf_id_in_kfunc_table(kfunc_btf_id, bpf_list_node_api_kfuncs, + ARRAY_SIZE(bpf_list_node_api_kfuncs)); break; case BPF_RB_NODE: - ret =3D (kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_remove] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_add_impl] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_left] || - kfunc_btf_id =3D=3D special_kfunc_list[KF_bpf_rbtree_right]); + ret =3D btf_id_in_kfunc_table(kfunc_btf_id, bpf_rbtree_node_api_kfuncs, + ARRAY_SIZE(bpf_rbtree_node_api_kfuncs)); break; default: verbose(env, "verifier internal error: unexpected graph node argument ty= pe %s\n", --=20 2.50.1 (Apple Git-155)