From nobody Wed Apr 15 23:32:03 2026 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 217DF6F2F2 for ; Wed, 4 Mar 2026 03:16:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594190; cv=none; b=kmi3p/OHeblzciIv8wGv4Bjuok0HtLI/bWBF8NhPMugc+Ur2YJ2EresKHw2eM5m3zKJG1f7699pAnazz5BIdAgYJr3Uj+ANdDInM4zKOpEOfIJoqBVdpObBo16SiCa3SZL7S2iIG0TsUZB80uKOdXRKQvvExWHyIDKWtV+hSCLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594190; c=relaxed/simple; bh=bXOZyfrjjEJNg2zXkkRsWg+H9U/k/NIMjxFzOVXWV+0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PbkyHGF/7eplw/TJn+JiFweAJVfEoLheX6RJJQLPEH0tfedljqPUpNgAu605RHqQK4pzGh5bLAiKKot1ctwuXDCouhN7HA8MVWrSuSZY1rfUv45JMzlwz7MXmr2aTjhzvfy0/xhl8Fitvpjfl1G2DqHxuJEQhh+/ilLzAWigqRw= 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=f//U9OPo; arc=none smtp.client-ip=209.85.216.50 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="f//U9OPo" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-358d80f60ccso2924904a91.3 for ; Tue, 03 Mar 2026 19:16:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772594188; x=1773198988; 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=U5WORjt7SxVQlhYT/6rzYEp8dt5s0cz4VVG01wwIAJ8=; b=f//U9OPo3nKm/ZZxd12kIfzmM7/PN4YhYB+2QXC/DE2SGIvidZxDPIKeHdVsFYpkoL 9D+smbVC9gdSSzUdE6yo/tMaxi8YpP56DPMcvIpdVHXeb1aDvPetchRFjXt6F9KlY4J1 BSozryKs7P2zyl9KTv49mrjs7nv07heZjBR6CSFHEOBXrGYr4UwN/6MM40Aah1IFj0Vm n7QvcacCj/d9wReVTiWnIwToefMvmm7b4Pp8N9qT7G/QeQM+vCKFzsvK1eTwM8Y4eT6S Eq0eTZLQU4m7aWo246wbIS1pB/N8oAwyEBtHgFiiENGVJ2h1zAHxgKPGW6GlphyuzEXi ZnAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772594188; x=1773198988; 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=U5WORjt7SxVQlhYT/6rzYEp8dt5s0cz4VVG01wwIAJ8=; b=G+PdHLR3yRW2U7RgcCqM8Y/pklCTxow5ZuKY1RFm5oVBFJyRvM7mwCViw5qKqekl+X DoTBh/9NSRzQqpK9826zJgW/HwxP5HD7TWjFbPdoDGo9GItCxFVZcLf8xSAvxKDiZLdN c5iahYwvwrf08V3zug6ht0/hPZzMvRdwfvHO2zN+XuG75rR4buFNLCSQVEo+rsJXC2dW H+Yh43lCyGeoDUbdbKqBY1kzGpogajw2km0+H6uhGklYyaf1Fi/jx6KUjM+6mg7R06Sw 3bCZaB5x846rmobuNTzfZG0gzN/XLzP6bDhALsgYxsYTZiKwSEbOvgxlJ0hzPJ0XWE/u musw== X-Forwarded-Encrypted: i=1; AJvYcCWcVyZBRmzQtlzXNdvTwRzFx4SUCD97/iX5RyCs3ZNJTQCi+DJmXMlfZWI1EUti2l6o133Gzb+gTWm7+3I=@vger.kernel.org X-Gm-Message-State: AOJu0YzhvPsMkdyCEGv252N2rPSZCFBTB8i/9YjyiuAC7Mbbg+L1jKmk /JFZlLPxBLqlxdMKNd1tarbVt57pCtPYxXkOAvoRkjCVUaWqXuzNu4s0 X-Gm-Gg: ATEYQzy0as18+ioS8uUVdn7/3lErCZXJO/umdhDRuO8jWh6MXtaU+jmksopwKMjMy/d KMCbd1WYdRTiUkM7VkPTOr9b1Pv74k/NAZK8vjhZPdLG8KScubhJ/Q8u5f/iMUcId7WHXlMDu2Q Gu3DmbUGRfLpLlg+fqyJOQGvhIR97RdpJWAshbQ3FR4os8GmFSsk90ppVJ8inPasGhxU+XRUECE NwgUozZFVYCm+F9oF+LLoM8tXIuVEjtGoSJsWVEOg8T868X5yw5Pq13ct0rAbVMiFCaPiCe7jEw fZt/c0ZGkYnAJrfyuHTbAFa36oIl0hDVj0ev/jeGNelaWwS90ytRdug0bxG6EgE0aBUvpIW6GyB hKBzztzn2xwMSGtx6ED5fHxaKA5I6Wm709Ar16ddxITcXnpOuI/MhTJPpZf3hZfoe7r4csaX9y2 hEROEBvZh39qbZJ05uUGAYeMztT035C38qsu7eXas85sTBKyH0yw== X-Received: by 2002:a17:90b:390a:b0:359:8ef8:7a77 with SMTP id 98e67ed59e1d1-359a69ba1a1mr646963a91.9.1772594188387; Tue, 03 Mar 2026 19:16:28 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3599c3ab946sm3610648a91.13.2026.03.03.19.16.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 19:16: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 v5 1/6] bpf: Introduce the bpf_list_del kfunc. Date: Wed, 4 Mar 2026 11:16:01 +0800 Message-ID: <20260304031606.43884-2-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304031606.43884-1-pilgrimtao@gmail.com> References: <20260304031606.43884-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. 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 --- kernel/bpf/helpers.c | 19 +++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 6eb6c82ed2ee..19d88da8e694 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2459,6 +2459,24 @@ __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 (unlikely(!knode)) + return NULL; + + if (WARN_ON_ONCE(READ_ONCE(knode->owner) !=3D h)) + 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 +4563,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 Wed Apr 15 23:32:03 2026 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 7611034D922 for ; Wed, 4 Mar 2026 03:16:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594197; cv=none; b=GT/GkyhTd7B895hmCSAUUYZ4oLsEAx+Po/b9FFZpJXyQvy1bTkKf6QcHYr77gH5if4K+o/Q2Fe+jQ3Lc1LqVYZeLRPDjDg93Ez5oadmDUA/1s96J/8XKIP2wp7Vv8WIGXKuq7GllMmX8tePjhUfC7PwQYHk1beSuDR4I7Mv3ERo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594197; c=relaxed/simple; bh=5k3B4Ygo1wjsDMA1EPhJXyhhcMiqhAMzgiy+JUuepVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HRF0cNDtvpnpesHxMZOyYZ4zN51BAW+8/obhElna6+NZ0Qq1kn6H/xuWgyBPH+PyfyFJ644Nd8Zh2Wd3a0nWVrZ8lO6j12jd8MrAX7rVKBvZnj9rOoDYsWEZmEp5jxPvwbTCY3McHc+BXlo5wZfOrOICK8w/9z99kPYwOnfHPX0= 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=cXiXYnWq; arc=none smtp.client-ip=209.85.216.51 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="cXiXYnWq" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-354a18c48b5so5624127a91.1 for ; Tue, 03 Mar 2026 19:16:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772594196; x=1773198996; 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=9yrYqqdL3L32jfs4+IGyGROm0lZBENJH7/3+fmtOXxw=; b=cXiXYnWqyt0MpL47jDycvNwUCu6830PNR2bgK+QA59NNxdSFC+vyKzDouKjF6HuiBV nVCXnDNu50UzfiAFiTOSHkq9P7A8xYDR4L9QJg5zbHauE410nlObPXBocoAyspx/lT/W 8DJc0RkFOwwffgq92CwiCncrGoyOquh+wB+1BzXt82OHt5TtckOlTo1xjyUEvD8Y1sob 6v3Ub4JoXfWapPDBYH12dqa5V1GFl5ZVdkryw5H4Oy3xcfzm45K0CBEr2+umCk5NCd5Z xuJpnUar+QK3CpE/Ixyv3r0E04yf9iKaYnqLwZADMfFHrscnIrRRnn+7u2SgPogEjIsF Jf3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772594196; x=1773198996; 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=9yrYqqdL3L32jfs4+IGyGROm0lZBENJH7/3+fmtOXxw=; b=TosRg9BZXzBUO+M/pcSARclG/Zx4dKtL3JTUCpf7nfUpb8ZsEg14uGDAD6bpPx1VZB BHydjtjos91dxHWa3nb4Er2Ka/sbUj+N+21yt8W9Ify56oaT97mtoO+l4JK2B4z6Rf6Z U66/TgyFC8CFmFGLmRTgtNrSpxwW+fxEI+pWTccwecgbBDiyWIC0pKCmPIa0Mt+1Jrem 4mxCnEjX6FkrAHn3wf4hb2wVpIxFhX1PGNgSc5VF5sGQFvhIdB1HcNkGUHdYt7WK35Ej Edx/JD8YkMehMLnqkwqon/gNqP7NQRLPHEXwsko7gaQTNFE7xamjonyNTMBY9/6ZiWvV 6e1Q== X-Forwarded-Encrypted: i=1; AJvYcCWWU9yx2Qguv/Suwl0m0JGlSE3J5VwqjKsR4Y3hB4gEs4vyCUiiwsrvvqXmVYZuccfQ2APFQnxDECUQb+g=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+NKoDL9LME9KlpWUTsj4bMbXyUGg1iByry0vapnZCPuX2Pr1r tO98dvGfV4JkJkoYmzmhiKekcGJduVwDXIG3hmxHzx2ter1kSomaqeyV X-Gm-Gg: ATEYQzxvDykeg14/R9cfH6gzTPeCy0o7tzS6uRw0ZIHoTh/DO7UN8FfV45mP/LMkCIS whxWT2CfXVqkuTdTGgHQT+P1dti/X4FiPfOEJ5Ubv+nhL7mmzjdGf1PRj8lDkjmk4higLdEq96H zGH06Jho/FfqosliEQ8VVDUKuCV0C2EZpf0YTtVtiRP1hDc/fl6/ovi7oXQm3m8PbgE4IvO494N 31lYATo953PNbW0gbQ+wyx/zBrbTo4nYC4e1VSnMypzlxi2Y/KKJxavNksiwSsepwCICKxEISUD 8+sSBqI0RAY2Qanygk6XBMIWVwn4J5X0BCoujszZj+2v3lfyp6CVVWSaTwi/eACsA4+32Lz9dZK /pbJDhhuIOT0RaZYJGF2jg1Lsws/eMyQLsCPaoUl30dKONp+zdF9B7g51wRsXG+YbH6q09UIuns Ic4cX/JG2SNB7sLmwlcbxCbuIRC/g7xJAPJBCuWFo3z5rN6pnJRg== X-Received: by 2002:a17:90b:1643:b0:356:24f0:af0c with SMTP id 98e67ed59e1d1-359a6a4d3famr830934a91.17.1772594195723; Tue, 03 Mar 2026 19:16:35 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3599c3ab946sm3610648a91.13.2026.03.03.19.16.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 19:16:35 -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 v5 2/6] selftests/bpf: Add test cases for bpf_list_del Date: Wed, 4 Mar 2026 11:16:02 +0800 Message-ID: <20260304031606.43884-3-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304031606.43884-1-pilgrimtao@gmail.com> References: <20260304031606.43884-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 node to both an rbtree and a list, retrieve the node from the rbtree, use the obtained node pointer to remove it from the list, and finally free the node. To verify the validity of bpf_list_del, also expect the verifier to reject calls to bpf_list_del made without holding the spin_lock. Signed-off-by: Kaitao Cheng --- .../testing/selftests/bpf/bpf_experimental.h | 11 +++ .../selftests/bpf/progs/refcounted_kptr.c | 71 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing= /selftests/bpf/bpf_experimental.h index 4b7210c318dd..54ec9d307fdc 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -99,6 +99,17 @@ 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 * 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..ac7672cfefb8 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c @@ -367,6 +367,77 @@ long insert_rbtree_and_stash__del_tree_##rem_tree(void= *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 node_data into both rbtree and list, remove from tree, then remo= ve + * from list via bpf_list_del using the node obtained from the tree. + */ +SEC("tc") +__description("test_bpf_list_del: remove an arbitrary node from the list") +__success __retval(0) +long test_bpf_list_del(void *ctx) +{ + long err; + struct bpf_rb_node *rb; + struct bpf_list_node *l; + struct node_data *n; + + 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); + if (!rb) { + bpf_spin_unlock(&lock); + return -5; + } + + n =3D container_of(rb, struct node_data, r); + l =3D bpf_list_del(&head, &n->l); + bpf_spin_unlock(&lock); + 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_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") __success long rbtree_refcounted_node_ref_escapes(void *ctx) --=20 2.50.1 (Apple Git-155) From nobody Wed Apr 15 23:32:03 2026 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.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 A6FEA23EA80 for ; Wed, 4 Mar 2026 03:16:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594205; cv=none; b=mZsCTWHqEzfrg7X5j3jc+SFwoo+tH3RspsWsgQdUnqez+dbY2/uFU1+DrBzMEDBh3nnep1LJhJKezxgCT5qMqp56bR/iFtGSu98q7dfFGXZpKymE6s6+ZXnry7TSk6CygZd/vHoIzBHQHTShg1sInG6dUIOOXoPya6aL6hagcGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594205; c=relaxed/simple; bh=ehdit5EAOOpytD0YsvqDJRya3u0Bbwh8g1Ul7dRN9cQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XhAsp6Dlsbku+ipL8CBSzCGKkM2Fl//JfzGYDM42JO/LFgnfu7c4pwCuw073nBZe9ayb9hKTzf2nJfMzjbPtkqoCGCf4EB1t4kE9vnjCsHKNeZu2PNyIfbnU+Nz3GYqDMiv8a3Lf3GcTMcQkeqX08Y6BJPGB6RRCNpZ4fdQS91c= 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=A+sGx/iY; arc=none smtp.client-ip=209.85.215.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="A+sGx/iY" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-c70f8ffd162so2926435a12.2 for ; Tue, 03 Mar 2026 19:16:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772594204; x=1773199004; 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=VvK4RWAiZa01vBUY+g5ujJJ2sul5o+PDqUXg1exrkgc=; b=A+sGx/iYsIIDNKIJWBTqUjyfic0AQR+/RnoG2F1bFEch9eCT2eiu/8gr2rPuPEVik/ bfRLFGWiNPPjIOTEINQVXcm/9Oax0OHDITXKRK3llRw5nOZJ/TGK0aP2nmem9fdn0TPb eJltVS8hJK3+I7dx/NrBiqaCiwUmcjY0qmg5R5cbtT1q+s7+qHl+cih37/Or5273VhEb 91ZLLJLlRzMU8b1OiWDVu1DOqzMaxyUoPg1MdXouhEHb0pO/sZfQj9RxqQ9af5PvWuem Zqi8vrSa2T5Dvve2eocUPlzA7mLNqPtPY1A377B2tNXFpYuSsnnhppCBwx6FjFwXcnLz A3wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772594204; x=1773199004; 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=VvK4RWAiZa01vBUY+g5ujJJ2sul5o+PDqUXg1exrkgc=; b=EnsWlYrL4HRqhptEcnJhaNXTy+C8PPSr7ouj0A9DSDEDDWHIYDAiIJmR/2WxZBUvoS MvS4Rx/LbkOPzwzAhQ1lQg/ZHDJMng1TG5wcPpMK9hbmtW7jrWMEnLo685OeYWdFoWEi rVQX/vaUSyVDCCKMNdVjjNtkEpURsy/TbVHG7aehr+7HiUzPnai6anD7/3eyFK/kDj5n irt5pDPiH3xF5dtEikmx4AiaoparVwvKKzWHTEJestARDmoqJww4pRSy7B3JYzsQ3t3q C/9gT85Nct51F5ojFStx/oNcrciXCWHaLzXKFINfvsYc9eMuyjomyz4CHZF9O1za+BIM x2uw== X-Forwarded-Encrypted: i=1; AJvYcCUa8UCjNtX57shbx5BNf9XkkqfwlhIePWM8e+lZcFYAtEoUu5yuh0749P8gw1OyasUS2xwLM6L1q/i/qes=@vger.kernel.org X-Gm-Message-State: AOJu0YyLGTsgVyHO5PiLXRf0Rihx0Uyjjm0A6UEYLfsm/uKHrVjPeCip cy/8NnYSjDSDdfcMTiZK9Fbkdc1+Wzrxsy+llnYYuOdH5uxvSm8uruSq X-Gm-Gg: ATEYQzy0gTgPcD4rhQ1VxKCbjithB+BiWV3lmnZRxobHLvu5APSb3VCupnYS6agJgI7 VllPaty93bvKxvbh5Vu6W5rgqH9AFSeTZm4WLXum1Iiw3hUS1If5MRD2UZ/8wKDPTIxtjItP68s bH6IlyKnhLBrJlsvf+hrqW1zdTjvwswBup/wUFmbbpjN5nmKEsvuED9lFChfEWDA5YeESXdSwne KVcamdZs4fjvoAR6afL2VBUYQLDhnjsvI6bC0U9sw4uIxaLOSX5r1whDvnWdWQ20esKNbkCCqIF hts+St4te0YthwZBY+lKa2NcyXl5pcvpgXpRRz918pV40wApNETW5p+T3x5Ms7I4B8JQlyPiDTV 2YTfOlqEB4CDO7Q5ZWQh+0pwWJ7M5AYMfDa2kDG2jCaotFUwcwzoKMHtmhdUFnWO+JctI2L2Z5y 6qWFeB5/p8r6ELB9/NtBU1GZWhRocs4DXW3Ln2zyvVyfo9EWrouA== X-Received: by 2002:a17:90b:48d2:b0:353:e91:9b38 with SMTP id 98e67ed59e1d1-359a6a9f518mr666512a91.34.1772594203918; Tue, 03 Mar 2026 19:16:43 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3599c3ab946sm3610648a91.13.2026.03.03.19.16.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 19:16:43 -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 v5 3/6] bpf: add bpf_list_add_impl to insert node after a given list node Date: Wed, 4 Mar 2026 11:16:03 +0800 Message-ID: <20260304031606.43884-4-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304031606.43884-1-pilgrimtao@gmail.com> References: <20260304031606.43884-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 | 25 +++++++++++++++++++++++++ kernel/bpf/verifier.c | 15 +++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 19d88da8e694..188840057c7d 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2497,6 +2497,30 @@ __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; + + if (unlikely(!head || 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) { @@ -4566,6 +4590,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..197b972e79a2 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,15 @@ 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]) { + /* They has extra arg, so args-to-fixup are in diff 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 Wed Apr 15 23:32:03 2026 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 8255DC2FF for ; Wed, 4 Mar 2026 03:16:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594213; cv=none; b=NXINihvrKS5Jlh8cf9iErxWIY3tRMgmdImJRkYryWONHrQ2reQqS8EXJdLkQD/NQ+GdzzlEIhTMWdDhvhFPm+kEq3NLh8V6q76GNoyXUGzucA/5reRJaqNNOVZgqtSM/e2phBgUU5FIjOnHWwxZL1OhL1nfsOBWpntnyGaAEX0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594213; c=relaxed/simple; bh=X6liwf3Zz68Ln2eYf3LldcWUzkSatYgDKtSRFKTQiUQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qVPTQBt7BVqNzpNJpED/AhIfBBS3RQncxFmEUiaBQg02SlyXswDcoNBG6BtyZOLhhpE0flfs+fetHI3h2oxbzlaPk7ZuTE5eSxZJlVn+GCuTDYi2mMlMIuOSPClwuYwxlxf3Rh+NzXKv7/tPtIRLa0is+pUM7KtGobObjVYTK7w= 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=aRX8YT+O; arc=none smtp.client-ip=209.85.216.50 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="aRX8YT+O" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-3598581ed7bso1225286a91.2 for ; Tue, 03 Mar 2026 19:16:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772594211; x=1773199011; 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=OiJ82F1hpB+Nf2rgWWs29vO4kgncQvhmjK3cNBDk7WA=; b=aRX8YT+OqMenQCNI36fe3gR4B8AEmUh8IAGpJe550dvQYSeefqtemZ/h6z4VzjrHDx LJKB4qPOBtsX+AAEnbDwvzIi2zD6XjFh0Khj1nsk32/wXgt48N6t0udjqK37ZGGanrs4 3kvUUVlJ/d2CLC8vmJ1njUGLYzOWVxnJn/wO7umEG0i4xVMYRgup5hz/Fzj9Yp8h/P6q ZHfEIIc+fYwUKVKVTHT0PcNSV/oLNn1zjA9HLK1a62nEfC7iPaY8qPwB8bDOM1TqoqAb 4gVuSw7kNDcBtLE8UHtKXUEH1lJkqrpiUyyEy77pMh1LYkHxcnvr2w8D13XcTknQb0J9 l7tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772594211; x=1773199011; 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=OiJ82F1hpB+Nf2rgWWs29vO4kgncQvhmjK3cNBDk7WA=; b=wNicVrkSH1rJbvqKiTHBQoOyg6mDx18moeXAAADhX92ZCmQNsOa7PXw1WQZMWtz1tU eOYdBfH7KIlHJVAEA4y5AINw77G9gA55mCk2a68NBKlZTxiwPi4AhS132jraRIYAP6+X 2doMxys0pNqR6AFOL3hA+9gegac20oUM2oPKoXQL9NJlkKLI322ochw8RTm2Ic34rzXz R5+EEuvVZFPXq76HtFoffTzjqLXoaOj6N90v4fU6e3AihmVpLzkMj323jJ+api8RokF8 +ArJ4q626HlCUMFAZLWP34500CBJqA0ZY6vrPs4l0dErnzWhsjtD5kO3lB1hyPFXs1tt FQ6Q== X-Forwarded-Encrypted: i=1; AJvYcCXolWh41L6zvxZM/kPI4RTmev5Z+fo818aMsgNxMs6s0QZGrxN8PN0CNNprCsMXqfTMXKP+AjtDXI3t1Cw=@vger.kernel.org X-Gm-Message-State: AOJu0YxHpOea8u4kxerU9KTsJIk8DQby4kLD4ZNo7LpMEgJvvuBW1A1m GTIYLSIw93XPw0oeU1zv67Bcuy8J/6FmikmiMjcCmIKWCXrPZb3avElq X-Gm-Gg: ATEYQzySmBxUFeCahOOKCPcvPv+oLVsUEqs4ymgXwD4Cxj8jHkiL0GUqF/oEXKEFz4Z lqYCr0sUpJsXG5TT0JPNIMtKCETboLANN04nWEpyXHebKaumXDBa+XmNYzhwwTG360GcfV5bihQ 8S3SRcSv9L6tJ0E/s8HOGGzih0WAoQRnlyc0aZ2FLxVFtyHQAaocYq/m8ZhRv3mInVXlk8ovA/7 B5YlQ4QQP3tEYgOIr6viTWKlWMbEwgFE54MmmvM0S7gcQVEnjrLoxXx/rPoq8EX/Rhh1l1d9jD2 Z08DGzakOYYmZCmIQpRMpzHBZqxf1r64P0dh79Xz0BvycADi06hQr76PZ8IHqEjXUIF9oAbasWK 7lszRy/m9+uAoYeOarD6dXPT2+VICynk2RPOD6nY+t+GN9M9Uad3KuZuGNZqV2qXhLtJBdT5Vhm g7jYbrj7yQ9psKrDL+VOaTFo53xOzIKOgp8Fshol5cB6hxx6EEz9TWEDlDoBd/ X-Received: by 2002:a17:90b:1fc5:b0:359:8c9a:fdf6 with SMTP id 98e67ed59e1d1-359a69ae8d6mr608246a91.6.1772594210922; Tue, 03 Mar 2026 19:16:50 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3599c3ab946sm3610648a91.13.2026.03.03.19.16.44 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 19:16:50 -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 v5 4/6] selftests/bpf: Add test case for bpf_list_add_impl Date: Wed, 4 Mar 2026 11:16:04 +0800 Message-ID: <20260304031606.43884-5-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304031606.43884-1-pilgrimtao@gmail.com> References: <20260304031606.43884-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 refcounted_kptr test to exercise bpf_list_add: add a second node after the first, then bpf_list_del both nodes. To verify the validity of bpf_list_add, also expect the verifier to reject calls to bpf_list_add made without holding the spin_lock. Signed-off-by: Kaitao Cheng --- .../testing/selftests/bpf/bpf_experimental.h | 16 +++ .../selftests/bpf/progs/refcounted_kptr.c | 122 ++++++++++++++++-- 2 files changed, 124 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing= /selftests/bpf/bpf_experimental.h index 54ec9d307fdc..fdcc7a054095 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -110,6 +110,22 @@ extern struct bpf_list_node *bpf_list_pop_back(struct = bpf_list_head *head) __ksy extern struct bpf_list_node *bpf_list_del(struct bpf_list_head *head, struct bpf_list_node *node) __ksym; =20 +/* 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 * 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 ac7672cfefb8..5a83274e1d26 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c @@ -367,18 +367,19 @@ 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 node_data into both rbtree and list, remove from tree, then remo= ve - * from list via bpf_list_del using the node obtained from the tree. +/* Insert one node in tree and list, remove it from tree, add a second + * node after it in list with bpf_list_add, then remove both nodes from + * list via bpf_list_del. */ SEC("tc") -__description("test_bpf_list_del: remove an arbitrary node from the list") +__description("test_list_add_del: test bpf_list_add/del") __success __retval(0) -long test_bpf_list_del(void *ctx) +long test_list_add_del(void *ctx) { - long err; + long err =3D 0; struct bpf_rb_node *rb; - struct bpf_list_node *l; - struct node_data *n; + struct bpf_list_node *l, *l_1; + struct node_data *n, *n_1, *m_1; =20 err =3D __insert_in_tree_and_list(&head, &root, &lock); if (err) @@ -392,20 +393,48 @@ long test_bpf_list_del(void *ctx) } =20 rb =3D bpf_rbtree_remove(&root, rb); - if (!rb) { - bpf_spin_unlock(&lock); + bpf_spin_unlock(&lock); + if (!rb) return -5; - } =20 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; + } + 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); - if (!l) - return -6; + bpf_obj_drop(m_1); =20 - bpf_obj_drop(container_of(l, struct node_data, l)); - return 0; + 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; } =20 SEC("?tc") @@ -438,6 +467,71 @@ long list_del_without_lock_fail(void *ctx) return 0; } =20 +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 Wed Apr 15 23:32:03 2026 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 C1D33C2FF for ; Wed, 4 Mar 2026 03:16:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594221; cv=none; b=XYfAoHZU9wte7Dku2sq/giXrgEh0gSSudmmKPmPZOSubs3/RC14Z1FK5e1rDj7q/PaRzEigCfCeqxpQnA726QXr2RjIhlWuOCD7latWAVFKsp9h0PzSNbfrMjvF/oSgUAr9DcBKm2ie7+vTSbjhrEJAKHC4fhELhR5P/rTiLpOs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594221; c=relaxed/simple; bh=HopDCi0sbWCcdqt4Jw69CbirnGVcxekQLyoHw0pn5RE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TzjMDssuucUgh2D3bJzqTEVg5/VMPAFZA/fVOXfEHVkC4SoipgWD+3QYxmJnZ+fAKw5ffi19CLqnwjdcARV0uj1OSd8epiiRQrMukWZsrDBA0Nu/xl5FSbb90Wsh2NOiLpsulSS8U5Mn+npTRLhXoASbgc9HMmhtewwDlA8IWTg= 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=G+MNQdKV; arc=none smtp.client-ip=209.85.210.172 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="G+MNQdKV" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-8274a3db5e3so3079384b3a.0 for ; Tue, 03 Mar 2026 19:16:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772594219; x=1773199019; 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=hm2SHq3+Zj/eoKZLqHKZUZv1dCI3xGgt9SMEzJ+vyvQ=; b=G+MNQdKVI6841q7E075nTBz2/7n6dMzLG3QMHxNmlaAyzjzhiXrq/eHHvmSbMHvTVd EhU4zzxjlxOp/pNBTflxnjYFTG5GQurGVDskV1WSRxNlHeyadO3SjUU8oBT8fviD0SZ7 IAbJ8KFa4eZ/XUl0VwZKK9HYYDeoNwCIyRXZgB6k+mtmV8uemjyBp/f/VdLyOlf5JeR0 2PRs0wJ9PRXrFGWsICQdzhswGIy6m7u+YtARyLGRF/uxFcYsE5nxvs4cT6Y/2coOgQVR QtckOv0mRSeJsJOJimDTeUY1d4QDrlOW3mNGOFpgzu+1xnJz8boTNNPURIKSiygJ69I2 Zttw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772594219; x=1773199019; 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=hm2SHq3+Zj/eoKZLqHKZUZv1dCI3xGgt9SMEzJ+vyvQ=; b=R3/Q0n1CwCh89mkU3+Jw+YCOhUuf/nsrg2DDz9RTZT73J0WMRI3JETNdCYSo6grGSo 25niYUN6Ehn+VQNzZYsBCobGtJYzLdgmebja5dttsPUQTGn4bgd4SlMLQASLynyswrUe LhPrA5AFcT9M9l0gKLMntofgd81dUT1Et4eswvISvdyhBCH3d94fJXTWmyMYqZ3+gAWf tEjbSSoTqVQrQf/Qaw/wT4+ZC5vG8BVxGteKWz2KKZJeRBFESMeVKbifR74aIdJrNLt2 coXrbgUXEEnT8fOaOSdsRDXakAWOh0XycDTPX2n26Ht5rHDlE5hAwcAD9TmnVNPWJ87Z FaCg== X-Forwarded-Encrypted: i=1; AJvYcCXJhDNy6FuVuTDyFzEio95iFYs9nQEC6dJdh1JaSLK98BIvYziZ5H5q9Ic/E/eYVynpn45oavdyUYW3a5E=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9yMKd4tis3A4CW6yxR/JQJzLUrLDAs8k/1vann+pbA5jxVfyZ TN153TT4FgYUuD+7DvfzXnWWvDdnMiqeHFKM5rPsPYMHrFdXx2dP8t6g X-Gm-Gg: ATEYQzxVLoSwJotj29N+HUzI/kAbUGfMs4FlhWNpjusFOylQhoGJQ+cJJloEroFOsJi yEXu/6aJ6bHxt35ZtE96C3AQ+urMBxb4U8pV5DJ+DEYAjo6+mebtAWLMMquN5ZmaeSidbT6IkSM lc7aXWXmFQifUR+I8oHpdYscH/3JyWQS2dM41Uu2uI/wci99fDsDFBrV0/XsuqAiV8K1eRVm4K9 7uymipM8FZGvNXlUJK48JIJO39UGEg8uqeqnsyQwKz+J9yfUUFTEYC0KasG4UT7tjb6lrbFOPuM pzUPELZYz9hvBAKyURc52DyR29I5HDynZTV8IQ7j/oH+xeLgVcxJRopATGdQC0VvxE7OwJFUBaB 5j/p6+NWdiPPpfzpzC+I9zORISKBaE7A9gxJEhDhqzcuceerMCGHCJMNUGXhG9w+r1dwd4ypsrf vyFmixgq5UwIpcIdJx9e7GQKlDlQO31NJQ7CjC3uv+4hKnp6e7Ug== X-Received: by 2002:a05:6a21:b93:b0:38e:9938:9a80 with SMTP id adf61e73a8af0-3982e19b2d0mr492689637.47.1772594219037; Tue, 03 Mar 2026 19:16:59 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3599c3ab946sm3610648a91.13.2026.03.03.19.16.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 19:16:58 -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 v5 5/6] bpf: add bpf_list_is_first/last/empty kfuncs Date: Wed, 4 Mar 2026 11:16:05 +0800 Message-ID: <20260304031606.43884-6-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304031606.43884-1-pilgrimtao@gmail.com> References: <20260304031606.43884-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 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_he= ad *head, return -EINVAL; } =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) { @@ -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 =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) From nobody Wed Apr 15 23:32:03 2026 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 2C0BE34D922 for ; Wed, 4 Mar 2026 03:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594227; cv=none; b=Ctxtmcrz5NmFJUh9H6b1NT02MG0wgq0x7apHCIPwY9+liu9skIeLdzz8i8iXPveFOZr5Fxwz/kPxqWbpvJJYJjZ0o5xX+k4Rlww5kv0vPwLlt3LpdNV/otIvRimHvG6mZkYBYXhJrXuEuq+Vv3IhTANPmwDWe78ko6+qHLR+rIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772594227; c=relaxed/simple; bh=3C6ttGuLl8L64Q1LnoiZLd+0O95EfQxJe16UcmlcmDE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M69N6dt5ZqDBG9TUNoLHSBieIHSBve8B2hXdC0iGWyQMI2EOgGvekVz7xV+etP5cL1UzhD0VKHWH05IGUboQompsRgAl7eK56MNAAc17yv6xvZRJIgkZC6xabTmC1RL2YvySvP7kUtLNY4dKQ5yrJMx8X0lfAXwPuotmiCbNjr8= 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=P5nFKaYU; arc=none smtp.client-ip=209.85.215.180 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="P5nFKaYU" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-c70ece855e2so2623303a12.0 for ; Tue, 03 Mar 2026 19:17:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772594225; x=1773199025; 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=/c4HiRtc5xaPbrfb39uLULAmMTx7Jx7ULuDedQkQ4YU=; b=P5nFKaYUXZnKDRygbS9+3yClcPvCz7DHQjZXzi5OycrqrU5iB1AJ4NUTXYFwrESchr 7FkjJtAHowjHD4t2eL84nGWVYYaM4Xpra9FCzxbzyU5Z/VPjzbNmHmoO0JlB8cfqFj2C 1ymixHbiJDTj04hw2XcUu3RlxP0SSWCWq+rN64mAkl1KKCBw2bHng9SQd/U0dg0P4aMh hXsAelmIikCbdWI9PUzYFPc0I+tLwP/FO4T3ak92LwyK03JaJ5uO4M6N3Vi4ZSyH63Yw 3vuusVDcoqT/ve/9T701wLf9RGyGUazZkXq9J8iR1awzzQoQ0rfmSNEDADZyVW/r8gYN W1gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772594225; x=1773199025; 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=/c4HiRtc5xaPbrfb39uLULAmMTx7Jx7ULuDedQkQ4YU=; b=gDbAY7hN7OYn8H1ynF5UNNtj7vTsLwC9/+6G9YRDdy1T7+JiGpEo7yBnqck7WTNaSb PZuqV1W4xTZW0g/cbbVOycalyPPhF16IAa/+dHKg7BCAkSAoGYD/zPIYjLrbgA6Wn6Qb okc7p51yb1GBTe29MF2H/vE/w8FimZfu4LG0LtMdHzM8nrrSBRW4FkijRZRW6cxe0Xbe zn7XSc+7ks7TL4Kc6mJ89MRTflfb67pbFnW1iHUofmDHcftpd27gWcbo4sg5tcjMqxmI D0tPuvy8P48ILEfgkci6XtDI9VJID5QCA2rm/N+SMGpwbjpnisali8JzTzFUkLKHG2S8 k16Q== X-Forwarded-Encrypted: i=1; AJvYcCV4EeFSynbCdpuZngDJ4iAosazEulvh8/DdlYFnXoOzVztOj94k9KCA+HqmWxwn2G5Z6N9ORjEOHebzyvI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2jo09hl0o3kx5UqiUsLnDSGzahDS5SAkhRuu03Tr3yJF4O8j+ YuTvtsc/wkYNfIJ5LmZiITP3Eq67vI7GSicQQxAXFw801J3DEQ7X/Ayc X-Gm-Gg: ATEYQzxxQ20d+mqvce9FE81ntC+I3kNzy34sDuAK5ZiIEqW4GBCmLqre1gJ4bO15CCp QlRrYnVPaqHv+aZ8fHV9QMzM0NkCqCzuO9KvCn9Hl3mAM64TNz1jK2JPJCmQE1Kr9qbilKUvikC g3Ub3oT6VnH7s331Grau++6GsF10yhloBzWBiM1SC4Pv2vrcI+tnwwyyotYh4kl2RntETSGVmyQ 5Luqvd+MqYFKOXduL7mzG1tor91foVhEauokjZG6UV2Ht73RVYk/1M16Yovae7Vz/dRtg0FlQ9Y PcRKODjbhYMnhSGkQNw9UtQJXswESIEoTram6CeeqE7hG5noe/7Olos11Iteg42GHy3od/m3qbz nEwGIdY5dZwVa+jNASyK5vwxxC/fB9vFW/6NuGzluCGlvRLZz6G2fykSBLpBTrSJLPKZThzSfFC Eey8CYS+QiX2VQ1zBkI5XH4gKkunpofSYQ9rKhsvkrl6VMbqrGKQ== X-Received: by 2002:a17:90b:2f87:b0:352:d933:5574 with SMTP id 98e67ed59e1d1-3599ceb537dmr3681684a91.9.1772594225603; Tue, 03 Mar 2026 19:17:05 -0800 (PST) Received: from localhost.localdomain ([116.128.244.171]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3599c3ab946sm3610648a91.13.2026.03.03.19.16.59 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 19:17:04 -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 v5 6/6] selftests/bpf: Add test cases for bpf_list_is_first/is_last/empty Date: Wed, 4 Mar 2026 11:16:06 +0800 Message-ID: <20260304031606.43884-7-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260304031606.43884-1-pilgrimtao@gmail.com> References: <20260304031606.43884-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 Rename test_list_add_del to list_add_del_and_check and extend it to cover the new kfuncs: assert list non-empty after insert, assert is_first(n) and is_last(m_1) after bpf_list_add, and assert list empty after removing both nodes. Signed-off-by: Kaitao Cheng --- .../testing/selftests/bpf/bpf_experimental.h | 15 +++++++++++ .../selftests/bpf/progs/refcounted_kptr.c | 27 +++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing= /selftests/bpf/bpf_experimental.h index fdcc7a054095..056b2de19b06 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -126,6 +126,21 @@ extern int bpf_list_add_impl(struct bpf_list_head *hea= d, struct bpf_list_node *n /* 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) =20 +/* Description + * Return true if 'node' is the first node in the list with head 'head'. + */ +extern bool bpf_list_is_first(struct bpf_list_head *head, struct bpf_list_= node *node) __ksym; + +/* Description + * Return true if 'node' is the last node in the list with head 'head'. + */ +extern bool bpf_list_is_last(struct bpf_list_head *head, struct bpf_list_n= ode *node) __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 5a83274e1d26..1b119fe16f5a 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c @@ -367,14 +367,14 @@ 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 in list with bpf_list_add, then remove both nodes from - * list via bpf_list_del. +/* Insert one node in tree and list, remove it from tree, add a second node + * after it with bpf_list_add, check bpf_list_is_first/is_last/empty, then + * remove both nodes from list via bpf_list_del. */ SEC("tc") -__description("test_list_add_del: test bpf_list_add/del") +__description("list_add_del_and_check: test bpf_list_add/del/is_first/is_l= ast/empty") __success __retval(0) -long test_list_add_del(void *ctx) +long list_add_del_and_check(void *ctx) { long err =3D 0; struct bpf_rb_node *rb; @@ -386,6 +386,11 @@ long test_list_add_del(void *ctx) return err; =20 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); @@ -418,6 +423,14 @@ long test_list_add_del(void *ctx) return -8; } =20 + if (!bpf_list_is_first(&head, &n->l) || + !bpf_list_is_last(&head, &m_1->l)) { + 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); @@ -434,6 +447,10 @@ long test_list_add_del(void *ctx) else err =3D -6; =20 + bpf_spin_lock(&lock); + if (!bpf_list_empty(&head)) + err =3D -7; + bpf_spin_unlock(&lock); return err; } =20 --=20 2.50.1 (Apple Git-155)