From nobody Thu Apr 9 17:58:06 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 4B4673909AF for ; Tue, 3 Mar 2026 13:52:46 +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=1772545968; cv=none; b=SXHCbgVXqiYSbPIANcdJz6Ai2230mMv5IvcguzST5zttzzftAv8PO2RWvT6jd6U63q5UK1rREOjOlsHVbnmxiwOQLR6ZpOSZRIJk3Kwg/Zoikv27Y7K4FZS/MT5u8uCLl1nysgCpZNTP1LxNUK17ngY7Rg+phYcWjk+X9X/xzR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545968; c=relaxed/simple; bh=bXOZyfrjjEJNg2zXkkRsWg+H9U/k/NIMjxFzOVXWV+0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YcqszRvlHt2GpElsXZ+MNnbgWIUm6ixe996RvDzLr9A79bSExjBWRur+a22dIkqi0zwVV6/SCp9FbDJyn9y1Hjj4vIdlVC4J7p7d2BX+Vhv+vzsQD5gXb3oqnAl5n3k9HzBuu/wyNcA7vQVLeUrDslUdZUDbJh3Z5K8pTAwJNmc= 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=izg8TOLB; 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="izg8TOLB" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-8274936d2c0so5584211b3a.3 for ; Tue, 03 Mar 2026 05:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772545966; x=1773150766; 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=izg8TOLB1yNjkO5NJ3g2uyGdEc0fIY3Giyvue25EIY/pftojzqi7WrKFao3DXMwX/8 sX7sUn9Ktm6wa7s0qPDrR3S+A6qqfH02L039X5VFNkTwbwDmNGlOCjy8Ef4t7FdrWb0R YyxFvRw1Pw1Eo3Y98bPYQ8MHBRBoC0cYn5PtAjIIl1fJ3HwjIaL8vin3cybo9GgCCGr9 neWQNP8Bom80TpCfTbcN5TFem4x5MOXA5RkT09J5XTpm343ATOzV52ZnUL72GA7tsEf9 1GeztXCc109EglgOOHhKMiC3FWlF0FiQHFA2szCLXFBSTULq3IspBhcGlsv1DsvM3CXv cgjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772545966; x=1773150766; 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=H6R5ai2vcY7gi9fivdUtf6vfIt8ajN8UJheKYWGbTzqeEhUWAaG0HtxcopMewyTW7p mMIe4UIGryNhthwf9yP8Yf2vDFHLNyXIHSxFaKyHeaN/Iykn5M77aVpU8BkhxTMssxG1 qVq3UVHJVWToXPWSObiowc5F8+eD3AhaBr/2nI88iwF7X9lT98qZzFVxwJ0DCP5L1hBG 2rmv+q1gI9Wa2BVo5glmC/6Un9RwH2VEdDStqyJCVbFoqkFqVRiNZVwkjUrDaC9kK321 HZYnCot/hKWuo2NGOiDOh3u0juDKcF4bv0KdgijZFn813AXxA+7W1qCn8pFmNZidIKKP od0Q== X-Forwarded-Encrypted: i=1; AJvYcCVqq7FFirMfUhn6WwPicTz2xZ19zrIz6/ILIhF1ZP5cbYtqdyJrOs2wFpXCYtNDHgddP1NWV+3WuzsPMxU=@vger.kernel.org X-Gm-Message-State: AOJu0YwwpyjMmv1ysD7nzK1W4azz0dLvxqpN+UHVOF26DuRMgClaa+Zs +OdwbTyq3jn+5F3OSa+eU9CpkA9HUlkXULrB5/5l69VpGe5BFo5S1bp8 X-Gm-Gg: ATEYQzyXl01yerJ7hmm7ldd3RhWXicY65N5tQMdG2Rc8rl8jWjQmVEhOFtNlaARYqdv EeCyZOpfi94uy3Ct0AIDA6EXEnF62PjmWMaCKfpZg0u3tk/X8kLOd1vpuz0lOjY8InNO9UaXkLd M7Be41rdfHPPab1fsRpEW3h8kmdcEh0PBMM+LzA56wc4PWCEZgG/AHiOr2+a5eeatJmtlsPIh78 fUaR8EHMQg4lYfwDcXKZ/g0Q2vj+33Bl2KdVXLIZCvGaNOyVPtOpT3HLQlyM4i+uW1AVB05RIeR mkjSvZQ40q62Fbalr0orb1njaZ5Yf9Ey3gH/tEYeUdE9OFU9zNZ9C0CFBgpW3V6QXrXoNhe45eF wDChBF/2P9vZjq3BCD49mxrGvDNeYtmlM0HTy5Gfb6yyj6boincLYMEQ2WcDpERKk97o4Jd6q4k LV+pymR2pnmgzegIXN0UeXqJ/7EO45F45rGVHehtC22YSv2rrn X-Received: by 2002:a05:6a21:140a:b0:395:6f58:d4b7 with SMTP id adf61e73a8af0-395c3a0cbafmr15399033637.6.1772545966299; Tue, 03 Mar 2026 05:52:46 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.52.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:52: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 v4 1/6] bpf: Introduce the bpf_list_del kfunc. Date: Tue, 3 Mar 2026 21:52:14 +0800 Message-ID: <20260303135219.33726-2-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-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 Thu Apr 9 17:58:06 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 6407B35E947 for ; Tue, 3 Mar 2026 13:52:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545977; cv=none; b=FIQy3d3Wn8KuU0Tb5C/wsTugD2N1gwKNw094zVSnOnsJ21GJPN0G2APtascz6k7s4n82D3y7/dwwS+8NQnv7/E6m02LZ3kI9Em3bd6mHjYEiOLKuxGHQljOEnSbOag+ERmMjxwCwpr+kL6oMktOk2GLKO/haKop1AEK8zc/xN8s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545977; c=relaxed/simple; bh=5k3B4Ygo1wjsDMA1EPhJXyhhcMiqhAMzgiy+JUuepVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZI5dfQnC7SGO16/FU+UmtM+OvivXiimV62X9PmVGEXBuHyrkr08pXXyzKhJ+VWUWZHBdgk7P+CnTDMHdaPSugDifTOUCpNVlnC2687ZsVQfD/vssJ97x0qNGpFBUhwKxpNS13u4EJG5iGOPsDBlC2rOQ4DxsMc19OeS2DDNsvZ0= 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=F1Sb70Tj; arc=none smtp.client-ip=209.85.210.175 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="F1Sb70Tj" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-82418b0178cso3279517b3a.1 for ; Tue, 03 Mar 2026 05:52:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772545976; x=1773150776; 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=F1Sb70Tj345lwTMqmngIa4+emLD7oKmaf7Flttjq3fF5f8VJVrpw0U+7A3KQ5OCqBQ MC4+1/gzWhQgRyHdjXh+xPNl91JYQwDe3a/ye7fSVwaJfYz3B6kdnQrYvd624SRmMWty LkDuuSh6TpxUeDl6KNW1FSTmi8SW61tKjoQlpwHdpjFy8xIdJEEZ1egUuRKPO0QcwbQf 0ND+paymfA495tldA6iPlcBGrAcbo7Loe8lMweqcWU0+sTEgAH1ZofsAd4e9Sey/Glds DbqIZYX/T6ycuL/jnx1NfpUILdGmWCY7+WHs/7fOmNv1EYUdKYYOdZjsyYl6kwNXC1wj 7+HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772545976; x=1773150776; 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=O73XSjkNSbBL9q5kmMJXHa+sBIAFmFlY7dLCJoHTEgFttBGx9+EnAH6OfzuDXcwGka 3wHcq+Gu6r8jSCD4I5qngYP/4pboBKQQlRtv+cA6binvKBq8SvxC7cFkNiVBoR71fh6v 5MbevMbtkTwW1PFjxsq4d67DU3Mwh3u3nIL81Lq+g34cj9INohsSf5DMGp3x1DF4ksCo q7otnVvspl8UtvUineyjozrZYBgaU73QLxReG3kvNunxcKEujr5qP58qOZflYX4HvpYS I0BLeGT1twG84ZqQ5weDHBeegxr3jx89p7vDJ/8v/3hoW1rehIynsAhiI1Egyh2H4H+E R1Tg== X-Forwarded-Encrypted: i=1; AJvYcCWTZKKZ9BiKU4swzIY6wuGTgd1KPriO1Fx1EOLanWbwZabnS8h5JYo5DXky6XyTgzB8zCGrKwzJTj8uwmQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyC3mVeMqpLKg/Efwak7tYOUEmTJKmsM+qos4VpfpB3miBCQ9/3 8/9kX880WBOdAJBHKOakp+iOH621ugbTYrBEj0aOn50UcwvYvnDpwc94 X-Gm-Gg: ATEYQzzrXXSlOd0oyTsIn9RT0TxlBj+JQelxgcB0SgrptRZPM7L2GHm+lxei9lcDfHO F0gRzSCMz5ieOkefz+Ic0XH4MzdHiOuarE7MupEqzNs2KQtY5JGQ0aAKSNQDICSNwKmQg9I5Uev 4KoanTD485utG4zxRRbZtXmOmUcjetsC+VzkSnZ1cbGPDjiwjPChY/Aj3yW42ThspAjsNm2pcN7 AFLHC/AFL/U+jwVE9T8DkCAH5mDEBi4VASDpQtRCQa/Ri+g8E8xRMwJr99ltT+gvgqd8G6WHrfh yWhoIbHSrbLjunDaPabMeT1DehxmC9KMJ2utQt+y+2syFZeGP9sdI/icM5iaGR5ITb+liI8MHoX ZmIXUEMUsm++I86MjIcHf+pX+oJz5biKv65hQJ/sRHTtRKdsml7LUsMgd5uJAwE6BLrfgUNYLqn H42mz0jR0lGBTippzgnRNnnrsBUjuy6pCdrweegqdy6fQiM+oR X-Received: by 2002:a05:6a21:a341:b0:366:1f1f:3299 with SMTP id adf61e73a8af0-395c39f7a2emr14911659637.6.1772545975553; Tue, 03 Mar 2026 05:52:55 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.52.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:52:55 -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 v4 2/6] selftests/bpf: Add test cases for bpf_list_del Date: Tue, 3 Mar 2026 21:52:15 +0800 Message-ID: <20260303135219.33726-3-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-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 Thu Apr 9 17:58:06 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8516935F198 for ; Tue, 3 Mar 2026 13:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545988; cv=none; b=obYuUhJKONeqqqmpuuGpo6Xm5Je/5wY/q3o6DhZOGE3WmY0KfPFTzgbsdzxETPKecjFlovZsVVfueH6C+vscesg4sKX7dIMFyaVMuQbUxOBgw9B2t3gytp49lZOfgaDC/n6/BVdkdOMrj4XWhfrpamuuJ1RNcDBV6cUhX3WGDHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545988; c=relaxed/simple; bh=iSFNWc06H3raqgJgDDYUzTQRs2V7xM01vLG58BhNkmU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lenEXtPGPLKfy4m425DHh3opNrmV6p7ImZJYLG6fLVmN06XSPfVq8H2xURm3UTz2RRJIgV1dLFRURog+weRvDAVIHBvwNwaTvtHY7kJMrls1D3rzkR6WK/5kBcXJdRNi1V3QE3heA4tEP99xBs2pLKQgLlAvRvhbWHCuZUfX4X0= 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=mYG8Vaf6; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mYG8Vaf6" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8272a56b91cso4851023b3a.1 for ; Tue, 03 Mar 2026 05:53:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772545987; x=1773150787; 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=qUQQET4CboZXfpjCg6qExZ2BGX2KyTfT1q7il2Qu4FA=; b=mYG8Vaf6er89Bf13AgtaW1iJ0gMMXqY5eOMQo1O/An5d0oZQTHnIuQ8PHhnngXBWmX 7JWJT+167gRS8h+QwH6DItK4q0LUvPJsREIwxQZh+tvSb/sydt2RY2/oTGIdN/gHgESP uJ7ln5zDGJqku1mxa3oe4/PYmisW6EIKOa1rBSJeUvB+i4ntA06r2zrQjsgKZfGi/4M7 mtCln4kSk7LJ4rX5SoqIiZORqbA8xlqOh+52NfHTMrZW23d+xOpyklVNhj66LsgB5Ztm wznD7fkvhEI49U3cygji2tOKwNBfKifswt3MR8Wau2a7n/KhTiLf0kmKs1vWAp+lvKm+ Nebg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772545987; x=1773150787; 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=qUQQET4CboZXfpjCg6qExZ2BGX2KyTfT1q7il2Qu4FA=; b=QfRGa1lF30jen8eXuI4KhosLLMLRuQH7pN+mUBTufHdtbqrU6nf8ZGsgh5UPnGT740 3GUJhi4w8JDyEUw11XL/jNwClJJgHzdAsGnPLI5TVLhXNqj4s5mCqEV4yWn4aGfUYq68 wSephz/aE5pYUkgH1znldaF3KpaqOrtJzf5Po6jt5lb36A3VzPU24c5iqR/5ZqwKHmGn Myqom73qY+RcSBlFjkbmlU6c23F66lygMIqjJEBn5SPZC7w9JBMQ2XgTFcjNVwo70Hgv t3Fpar1NqvWZy7rZa1gZiTqiY710/4YAaztLa/u1dK36NXdHF0RC3ft7f6BXY9Z7Vkoc Mzog== X-Forwarded-Encrypted: i=1; AJvYcCVho+WrQODSIy42yZIMg2DgkRIQ2vsgsCo5UvxdOXkROjuKbeMdSKiZSK5UFByNF0N9QNyGC09DBwNG/cc=@vger.kernel.org X-Gm-Message-State: AOJu0YxOfhfY9H7ZxGyIvjyjLQti/UXiu9Q1jAzJCI0hArGSKj8sWqR0 1Pa+E4NQfmWUMKAGX+SKmYny749kKcfJ5t7MQ3JgrymsTaWMEBZYfPtz X-Gm-Gg: ATEYQzyBsEyafLrcoq+i9P2xfSAqwXDiHf9ZGl98k9WCSuBxA18WvLoWphMLYu+PuGc dW+6Qt+63QxQQFCQS9yWzEaeJ9sM+HGx3LgHNe+M/cs2cV4vU6l3PMbiy/c056eTwzoG/U/mL4i 64N6VH42wMHVisNC1NLNjnAKui/wrMtJE7Uv9g8pR8PtQXRI1M0lcZWjrdxwMPlvBpDobF4SFUm 4f3AE+rKbXMr4PgVibMynE506J929T/c862Xf+GOyATJhW/NxdqN0rx7Z+xf62dutgOw8Bbo4jI RMS/YYYwxfeoNI2DQdQEFDQF9ISv+oSKOWpigHHs5tH39yEh8BcukvU8/YtrVmFVW19zshSZvgh ADQ1UTXTd3l1TcwoVQhwvxQFy2T9PEaeLgwWXbCxMBxxOJEAHCNqsK7vu8NHoreOKxVU6x3VHFj 6eiVqy11AGbBAkT/UJ5II3RAI+3oaFJj/wYz9yG0LI4rwGFUxd X-Received: by 2002:a05:6a20:c6c1:b0:35d:d477:a7e6 with SMTP id adf61e73a8af0-395c3a0c9acmr16875727637.13.1772545986758; Tue, 03 Mar 2026 05:53:06 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.52.56 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:53:06 -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 v4 3/6] bpf: add bpf_list_add_impl to insert node after a given list node Date: Tue, 3 Mar 2026 21:52:16 +0800 Message-ID: <20260303135219.33726-4-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-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 | 27 +++++++++++++++++++++++++++ kernel/bpf/verifier.c | 14 ++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 19d88da8e694..488810da8f30 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2497,6 +2497,32 @@ __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)) + return -EINVAL; + + if (WARN_ON_ONCE(READ_ONCE(kp->owner) !=3D head)) + return -EINVAL; + + if (cmpxchg(&kn->owner, NULL, BPF_PTR_POISON)) { + __bpf_obj_drop_impl((void *)n - off, + meta ? meta->record : NULL, false); + return -EINVAL; + } + + list_add(n, p); + WRITE_ONCE(kn->owner, head); + return 0; +} + __bpf_kfunc struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root, struct bpf_rb_node *node) { @@ -4566,6 +4592,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..6dfd4afff1cf 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,14 @@ 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]) { + 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 Thu Apr 9 17:58:06 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 20D10358360 for ; Tue, 3 Mar 2026 13:53:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545998; cv=none; b=pBQ6McGF/pE5ae9zXp6+y4t8m/w0cxpywBoqIY4kSOjKTdoIZrR4fD8/VJGgARGWSOwRpQCmOFv/jI+ruNycBkMBpGbOzYE0n+P5dbrNgPF4YTtROwpiQVr9wnXg5Hg3qlxdzQmwV2EMDnuOUPp0LaUkPg1HLb95zkEk6dIIcPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772545998; c=relaxed/simple; bh=X6liwf3Zz68Ln2eYf3LldcWUzkSatYgDKtSRFKTQiUQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BseXzztmSE4qWch6pNAIw4jA34rkzAFn8i+GRRJegVxEyx8a3W1MPCt+rQ4FV2CoXcVCTj3gTBjuKfiJSQdVjTzk9So2qJ3QVecVM83MNCz7+Pi0ziqHnNv4F1KVb5whTZmiV37msLpaTkKJMbcbHIXImAdRsunQJeNv4dALvrw= 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=Hyz6tqNd; arc=none smtp.client-ip=209.85.210.171 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="Hyz6tqNd" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-82748095892so2685251b3a.0 for ; Tue, 03 Mar 2026 05:53:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772545996; x=1773150796; 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=Hyz6tqNdArngbSFXNqIRjTFHNViIPpYFFV+IaILWLba94l6qRq0zMaJa3SSIyPBV07 +AwFXgDxm2e0whmYf4m6hNL+OX3ZZIhLtg/a0dkRYC4soxe63uK9H+f5XNKwkxaJi0Ac X77rt2Ugyb7jUwI488foYoWxQCS6zh4MMjwLcBd1JeICD9RxYPE0boZPyTqB7sNYqjyg 7oMjgTmg7QJLdGLA4ttiXEQEaX3qQnL3aNTBB9qRhKfCV8YaNHdoHZJs54/nQLaQOzCH WohOk1XS6jVSlY5oQV2jWFGe1SIzkNo3GM8lm3UHs9jt3kHTAkAmYqgtg/wPUL1i7KGX 69pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772545996; x=1773150796; 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=OcY0IhABv/toJBrqrRHeOU5XQc/eq4ZRyHKKeSctiTosHbO9Oym/KUcw/mMTgYk3Li /YJjUx+1dmQJ5MulL+kjLtcjAu3jstP2LNN96bxADMLio/zv3Z5Ky1GUgRKyCe0vMUvR WNS/X+nMi7A0BUfMiqsHO72MB0TSjr35G68tJ/BM1oD9vBqkOa7ZZBp2TScoPxHM5aXi HezceyPOi1QZK61Ejw8cVrx+4QTDBAMBzYanNxA9U+jpIZRxNSJ6FlSZeg2v+rA0eH/s 2Mr+Q3Z6FuS47rmSKOu2mDmiJF2U03JIgn3MQiOhnbfFC2PQVXdjDQ40YRPpJIlBVQYT Vx5A== X-Forwarded-Encrypted: i=1; AJvYcCWldOPOi/HPlHaSM9cz95UEZ96h1jeLLumZwH527r5dtkx6bRWLWNKqdDXBlKkx9LriGXp4HMesrlXFraE=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8ONOuG0dbxHX9i7Vrcbur5JNfOI/4bpvBRJQQJhQcRlDNL03G k32LhjVtkgAhOoWP3QpNqVM0dtXCz1n1Zjo+5EPtIjtRkpNFLy0TyRuf X-Gm-Gg: ATEYQzxkSQPecP5/qyTYNO5xdhVJi9B8UVNplCjSQxxNSzikifh0xw+i+dWEj0P/4WP A//kBnFEbX5wRrjAUHqKdbW+1ChD+5hmlUcl04U14l6u2/gx/AaXis1ZUFtaEnoHt8jQ+118pMg 18Jd6oI2OJbG4vYJyQp7t5qsjvm2qOORPHPxUJtE27OrvciBIBjI0AFtS/PvzLkjfFcsP0D2LXG 3lEn0afwQ7zxNayygN38RLtBvdBlm44gG8oS/n+RLM6SLfV5ud7Vr1oGEGHsYcfw2tHDeA2gzDs MYkpD8+bApBg9RZ0y8T8FRW3Jvalk5ULErKNzyz07lXu0Dma/ssMsZqol0cmaEabvtYHE3Wf+gb SiORGx5uDKMYJZsAJ5QZzVjW0mKsK6Xcq6o9sSb15e2F/C9DZIp5sls70sEd05YlLqYOem6Mz8G eC/0NvS6XYk78BJZnygn4k6ZTS5ZsB9ki4HXS+x6CYMoucEFJt X-Received: by 2002:a05:6a21:6e8a:b0:394:5878:e1fa with SMTP id adf61e73a8af0-395c3a1aa3dmr15295153637.6.1772545996456; Tue, 03 Mar 2026 05:53:16 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.53.07 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:53:16 -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 v4 4/6] selftests/bpf: Add test case for bpf_list_add_impl Date: Tue, 3 Mar 2026 21:52:17 +0800 Message-ID: <20260303135219.33726-5-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-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 Thu Apr 9 17:58:06 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 D96D435C1A9 for ; Tue, 3 Mar 2026 13:53:28 +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=1772546011; cv=none; b=c5ZGjC+9pPj3lvZTVaR7N4YcLw7LsQib8hGdGsKP8dvHW4me3gRDfjCERa1Lc67dGgYMl2ddiIkwN4vM1WNRfKK0vU/v9u8Trl5MXPgI0FAC3fXPOgrKtjL2K3MCGQYBUWXDzNMdZ4ztHyfvu/OFgKVjoBEbmekWDzJLqjfRq1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772546011; c=relaxed/simple; bh=wxWIwJi2U61Y4j1uCGoff+dbH9Ura67jjy6+JYedFtM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YEbzhVG3D/CDQDrznKieY/WwPF2nh4tAUSvtLzfDPq57wbfYmkbu5FJVwxK+nwt4EzUAIbxESjuE9yJWGadoTuckmoP5blnvbO0jW3428HsHldWXMLQeJV3blYkYF7XFjRkJePBlyL50UBZpgYQDGNUZK0JhjQDrkcc4mziT7oM= 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=iuOu1iOK; 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="iuOu1iOK" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-82735a41920so2196811b3a.2 for ; Tue, 03 Mar 2026 05:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772546008; x=1773150808; 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=H27Cg5uH76XKjjIH1pPEtY296Pssk09ysV1ForE3RSo=; b=iuOu1iOK7vL7sQ34bJQXbd9zkJvhrZfftCPOFozsM5alPXaPihU/lVGi44AIviKWK+ E1hE5dZd1Am3XWPntnUbOzLlI/67rqYaQWThPzLcj1PySt9Yw+4HIQ0wNSNDOAh1wLkI m0K4WF2+NedgGNPtbSNbKp/FJmdpoUIZSFQBHvJk068aG4JFHpz4wioc7aENPnVS1g5k df8M7Y14K2RpGq/hxmMxoWmYF67fXlqNxVaocKNvdxAJYt6/cATcS5rJ1RhOwqSvhIxJ sSrAB33xeCQFz/eAhbu3ZxDC/lSnN4kEQNTjDeaJ/v0ZMoe4NUrCbC1yWomwECjxkh17 ZmlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772546008; x=1773150808; 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=H27Cg5uH76XKjjIH1pPEtY296Pssk09ysV1ForE3RSo=; b=tG5XefIzf/dEmc+nI69ebf4mU5vPH1hSadonyntMiQ/ERSYDtezNUxKl4x7/hzY6sn BnQSmdO6hMEwIKB3mLG1sJbCBaGLVi1s0iy4UfOk1KJGwzTdbP0zpqsZLn3ebuoelhcF HFcg08qRSt9lc6RZ47JtPjVfE5/BlqDaov5icsrZLgp4jlwb2dnUOb8DbwljQobJYbLx ogYiAcdKBIAPO18RHYUPCk5PyWxnDRNqr7RCH0kt99h/6Jq0S5IwGAJfFnNLwZZTI3Dy SePlnvYfchTbq8x7nYW4mPfFFufSt3i59EpdAz+IPiWDUloDn2heCV/L4l4Pv6qVEhW9 B2Mg== X-Forwarded-Encrypted: i=1; AJvYcCXuJ7Blo9l/McCvYK6b05WQNMFtPNGjcgQX+vy+zZkUHqhyLqReXFgmSx0WQUaznh4iwVZvJ1zcC18zr/w=@vger.kernel.org X-Gm-Message-State: AOJu0Yxxuwuv9Qg/ugSV6jIwLXAZDElipg1FFbMHpiuYpqiGulqhce7O pGwjztxKrki978MeIkxi5LQhYLbxfRKcaicHwpVGg6dFjGsQY+khnbgo X-Gm-Gg: ATEYQzzYvAlp3hVzw96matFb2WM6njjCCEYany6ck8r9AZ/cAzFxMz4TKbP+wkif3QN P9B6jcSGCIOFC9JI1cpSUOzTm94PU4RRwbewv/JKqU8APTwR6zb/utK7Vh7Rrapj5qjOJUeV3rR sec2slPez/2bewnlPCkFBZbXz+Gw0rjeaAwi/oAhX+ndA7+LU2Wm0K2dNLkW/2nnTMD3I+fhqx1 dQFS2BEfLnELjO3vBpJR0Pr+1RvTi+dDkRqB4irlzNbXaNG7l2/nxv/Xz/U1pjxCIBYhdR7XpR9 Te5J+sJMUnZxGAmugjC0qc4w290YaUc4QjkF8kUd7pc5Ba4Q6+uGtkjDaaPnmtx9rZL1kKbL1Qd i1LAJgYbssMvAErm4UR4CwbqO+boRAFhYBz2aaDT6K/jtP9enL7MeLGyJJZ/XccO9sdeDHApHSr VgxiONxLKpUWu8Neoix3t/7IUo1TSJjpoq6Y0sG4S7Qwtgh/fA X-Received: by 2002:a05:6a20:db09:b0:394:6023:a0fd with SMTP id adf61e73a8af0-395c3a7110amr14894176637.29.1772546008110; Tue, 03 Mar 2026 05:53:28 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.53.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:53: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 v4 5/6] bpf: add bpf_list_is_first/last/empty kfuncs Date: Tue, 3 Mar 2026 21:52:18 +0800 Message-ID: <20260303135219.33726-6-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-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 488810da8f30..3a64a9bab06b 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2523,6 +2523,44 @@ __bpf_kfunc int bpf_list_add_impl(struct bpf_list_he= ad *head, return 0; } =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) { @@ -4593,6 +4631,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 6dfd4afff1cf..78652ccd2a89 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 Thu Apr 9 17:58:06 2026 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 2F75135C1A9 for ; Tue, 3 Mar 2026 13:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772546019; cv=none; b=mTn2P7lLxUPE3tRSi+d2W/6jS6BtVq+Zry3NoOnc189KDz7rdCCFBb7oK9anHdkf4JcVp02mgRR3FZDMF3cFECbXXJ5YyikiwNq2HKBzbigkCAi11fSL6h68ybMiSPeQDSlg7HQnk+iFNlShUQZcauenxH8HBlfxx7HHvTTsVSA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772546019; c=relaxed/simple; bh=3C6ttGuLl8L64Q1LnoiZLd+0O95EfQxJe16UcmlcmDE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SjuaJNC58XXqM+AO520VxxUUEl6hSfXxp18eyPNl26pKCMFSKwIJB+zedYqWV+jE+jfpadPjCd+OlfCDc4EjA5sz7HJ90IP4Qk+RcSg0ahuoPpfvCIuHN896+WKX3In6rgMz/nuQSDzAhx6MrEhSaHanXegXD1q9JM7/oY1m6PI= 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=SpfqORez; arc=none smtp.client-ip=209.85.210.178 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="SpfqORez" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-82746ed8cb1so3033757b3a.3 for ; Tue, 03 Mar 2026 05:53:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772546017; x=1773150817; 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=SpfqORezinC1+itMhON7mLIoZjh6QB2tqVtwZ7cLcxllrvfXIxfDJwPjUX7Qvvggn1 S707OjdF541jjdVS52mUl005NJaOxX9zp1gWv2vOKF4aeKhTUF5i/Y01JKmXUfmtqsMK il8d0hs2RkrzL0MGDlVtKHsW3YYxBUIMO/LCirlbOKGiR2Ilayyui1iYhhxL7v0++dax UqYwvuECz+ZSDB9FGi7J1iwKo+z04/1rQ+g+LFxEfnEe3M9zi7Wt3mYHWpsh5nu5PF4y axaFxooO76NwbE4xFy7L1WldbKmkarxyINTXzqAyBEuFh5uJigrukza3jai+I1XwIJft tT4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772546017; x=1773150817; 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=ED5t9W60S62lgs5g+gosjl0yGEHz94kXcMT6EEcr5f8qy829Jym4Q+3Ua8trNG6K1U qURsRJKNMrMlie/V2dpee+kKvFhcVmeFezozOGYyquKghnGSo2G1cZjKQP1jwa/xiQMR 50tjPRrPy3Ql1fsPjEGjzQDT1dcgmww/h/WIXVZ5RJwwutoKfXEOagNzcSXTSHM2VTgD Fie5jb2jUE1AjTwkoZ768iVvuK0efBC88df02cfKpXTpEtvMdQfFEUDIVVb46PIvrBpi 7tS+bDxzC1b+IGXgarvQ1od5cDCgs9YQz0q3qc0USamizw1gp/W3jG/wg+IMrY6OBg1Z +uRA== X-Forwarded-Encrypted: i=1; AJvYcCXgMkVJ18QEu2ENjnIT/tRyZYJ+n22xtKsttsUq2gHGbpPexfM3s05aZXq4VcBUtW2nwTqsOafAOqOEgLQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7d8GzHMVhEM1zNbtK2doyXAUNQnl+vQnveuJyN9ec+aThm+qb waRuwbhQeNr4JrruW2U/vjHcTWwOnOqGvYGJ8a1Kobs6s1yG6zWZMq1ne9NkAQ== X-Gm-Gg: ATEYQzxNVU0euq52RgF231CxY1P8f9tv4GHbjEHrKaRh40mrOkTYOLwFOSGM+ioiY/Q LUdQ3kBP1pBMAyHdS8z6y4LfcG92yg6dfE2sHmZ9bjdwpwxrqCg7gkgzYPg7ndgHO3LfCMTyZq/ goA3Pd5cDQF1wiQ3kVVCQx+6shOFMlM/yAFDi0c7kNIWMICFDJ9iInp1JIuvahLaTMfxjsRUCbL WTcmtrD+kHlPv+eZFFdL2f0H+6bqcldJ7Qd6GHbQBG+fY3XA5lmcA8L05QE40PlBZfIvx3r0btC F34vyy3aS7dW1c3dqRJYQ6WAS3qS4ctC2C3gMLlYrqqVjqd4WM08lKq4VA9vYjL8vRupXWvREFW fpNIzvhRpZGCde1WRTduQqA30gYDJ4APgL8ciHDgS9xvdVHehzCePROHXQz72kghs/icjf4JZkw IeRqYahcuuYGyYbu6zU3HX72RGyaqF9pnlQN5PAwF+IUHeh29c X-Received: by 2002:a05:6a21:1f8a:b0:394:593a:eea4 with SMTP id adf61e73a8af0-395c3b0f750mr14569718637.57.1772546017520; Tue, 03 Mar 2026 05:53:37 -0800 (PST) Received: from localhost.localdomain ([113.218.252.129]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c70fa62147esm14689782a12.12.2026.03.03.05.53.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Mar 2026 05:53:37 -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 v4 6/6] selftests/bpf: Add test cases for bpf_list_is_first/is_last/empty Date: Tue, 3 Mar 2026 21:52:19 +0800 Message-ID: <20260303135219.33726-7-pilgrimtao@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260303135219.33726-1-pilgrimtao@gmail.com> References: <20260303135219.33726-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)