From nobody Thu Apr 2 18:59:44 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B091D369216 for ; Mon, 23 Feb 2026 19:07:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771873681; cv=none; b=AGnEliJnVigOgL5of9IyKpBa0luBa6zVsL3bvj8GleW6A89T/AGCbX4p5PhNNrOM62Fo7U68wArUHh+fuYGfz93fIntNJ4qUOV+dDCoik2791ZtRZq8J/sAPkhcFSxguLPFbJIKW2nujdwH3s32CQsFxW71X5o0uKSvAoezJLhY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771873681; c=relaxed/simple; bh=353PHU8UO2Z7Mx4DxBGCc6/1Hgo31WK6JuMpfS43IrQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c1eyQqfxsfOgw4pKo9yMjCVzEyLmWKLzy9uNcISPurK6gDkrrb1izjHZZep/u1LjGBS412x1ZjjbGuANIP7Kx7Eawmn+OB5uJVOUFvHSY0IuYpXECDaM7TFNT9DWxZtosjh+dsLOtOOfthiP9g0UAoYdDeCmLlH4i0/5GC0xMTM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=cqRANo2a; arc=none smtp.client-ip=91.218.175.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="cqRANo2a" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1771873677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y++r09/3hti2gUSHEUw6btcoGNMDBhPsbUv8ShjBP7E=; b=cqRANo2asZT0wP+VaayFGg5fMyaDAYght0WWzMgeG7vSymSZjV8flVZs0XYSPpUTRE0U2u X9kqwIvXOveaF3l7szCoNDD2PxquJerd5q2bYY6ErfeIKBeUb1hrsZYR63al2wx+ygqmFP EpFAeBnm63lYbcxSxC2DLKZDkezYxLY= From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Jiri Olsa , Mykyta Yatsenko , =?UTF-8?q?Alexis=20Lothor=C3=A9?= Cc: Amery Hung , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf v4 01/20] selftests/bpf: Add simple strscpy() implementation Date: Mon, 23 Feb 2026 11:07:17 -0800 Message-ID: <20260223190736.649171-2-ihor.solodrai@linux.dev> In-Reply-To: <20260223190736.649171-1-ihor.solodrai@linux.dev> References: <20260223190736.649171-1-ihor.solodrai@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Replace bpf_strlcpy() in bpf_util.h with a sized_strscpy(), which is a simplified sized_strscpy() from the kernel (lib/string.c [1]). It: * takes a count (destination size) parameter * guarantees NULL-termination * returns the number of characters copied or -E2BIG Re-define strscpy macro similar to in-kernel implementation [2]: allow the count parameter to be optional. Add #ifdef-s to tools/include/linux/args.h, as they may be defined in other system headers (for example, __CONCAT in sys/cdefs.h). Fixup the single existing bpf_strlcpy() call in cgroup_helpers.c [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree= /lib/string.c?h=3Dv6.19#n113 [2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree= /include/linux/string.h?h=3Dv6.19#n91 Signed-off-by: Ihor Solodrai --- tools/include/linux/args.h | 4 ++ tools/testing/selftests/bpf/bpf_util.h | 45 ++++++++++++++------ tools/testing/selftests/bpf/cgroup_helpers.c | 2 +- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/tools/include/linux/args.h b/tools/include/linux/args.h index 2e8e65d975c7..14b268f2389a 100644 --- a/tools/include/linux/args.h +++ b/tools/include/linux/args.h @@ -22,7 +22,11 @@ #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 15, 14, 13, 12, 11, 10, 9, 8,= 7, 6, 5, 4, 3, 2, 1, 0) =20 /* Concatenate two parameters, but allow them to be expanded beforehand. */ +#ifndef __CONCAT #define __CONCAT(a, b) a ## b +#endif +#ifndef CONCATENATE #define CONCATENATE(a, b) __CONCAT(a, b) +#endif =20 #endif /* _LINUX_ARGS_H */ diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftes= ts/bpf/bpf_util.h index 4bc2d25f33e1..6cb56501a505 100644 --- a/tools/testing/selftests/bpf/bpf_util.h +++ b/tools/testing/selftests/bpf/bpf_util.h @@ -8,6 +8,7 @@ #include #include #include /* libbpf_num_possible_cpus */ +#include =20 static inline unsigned int bpf_num_possible_cpus(void) { @@ -21,25 +22,43 @@ static inline unsigned int bpf_num_possible_cpus(void) return possible_cpus; } =20 -/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that= dst - * is zero-terminated string no matter what (unless sz =3D=3D 0, in which = case - * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but diff= ers - * in what is returned. Given this is internal helper, it's trivial to ext= end - * this, when necessary. Use this instead of strncpy inside libbpf source = code. +/* + * Simplified strscpy() implementation. The kernel one is in lib/string.c */ -static inline void bpf_strlcpy(char *dst, const char *src, size_t sz) +static inline ssize_t sized_strscpy(char *dest, const char *src, size_t co= unt) { - size_t i; + long res =3D 0; =20 - if (sz =3D=3D 0) - return; + if (count =3D=3D 0) + return -E2BIG; =20 - sz--; - for (i =3D 0; i < sz && src[i]; i++) - dst[i] =3D src[i]; - dst[i] =3D '\0'; + while (count > 1) { + char c; + + c =3D src[res]; + dest[res] =3D c; + if (!c) + return res; + res++; + count--; + } + + /* Force NUL-termination. */ + dest[res] =3D '\0'; + + /* Return E2BIG if the source didn't stop */ + return src[res] ? -E2BIG : res; } =20 +#define __strscpy0(dst, src, ...) \ + sized_strscpy(dst, src, sizeof(dst)) +#define __strscpy1(dst, src, size) \ + sized_strscpy(dst, src, size) + +#undef strscpy /* Redefine the placeholder from tools/include/linux/string= .h */ +#define strscpy(dst, src, ...) \ + CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__) + #define __bpf_percpu_val_align __attribute__((__aligned__(8))) =20 #define BPF_DECLARE_PERCPU(type, name) \ diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/s= elftests/bpf/cgroup_helpers.c index 20cede4db3ce..45cd0b479fe3 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -86,7 +86,7 @@ static int __enable_controllers(const char *cgroup_path, = const char *controllers enable[len] =3D 0; close(fd); } else { - bpf_strlcpy(enable, controllers, sizeof(enable)); + strscpy(enable, controllers); } =20 snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path); --=20 2.53.0