From nobody Wed Apr 1 09:43:47 2026 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (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 1C6F619067C for ; Wed, 1 Apr 2026 00:28:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003313; cv=none; b=uCUtQH+bcJ5vG54aBYmHrs7DEVzASBCGQg9a7mkVOSFt2sBSreWVhalE6nOYkYxldKzwkdufM7DltQTranWOb0w/8E9i5AIFOrAFiD8IBsMoDikQfHWTidTSwp976cFN50mbw+tbQlNa8RplkzeusCfTCJJuF23SXv9ObSpb7mQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003313; c=relaxed/simple; bh=/SkZnf9rgHNSdrHLq0MfYdlt06Qrh2Su8iA0QXWXAkA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=D2gFPMGQiOUoMWdbj/lLEHADgS87QiYTWj7XKZBTAv/MOCaPKCRN2BB+Y5dj9CPuohhGN+KW/RQ9JhLSkevft7SiV7Kdh84wDRDiShbykJJUQE7Hn8Zho/ZKodH7K6GEDrhhjPZoj1YzP7s60r6gUMjR+nvxHmhCl5ZQIYX6n9I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com; spf=pass smtp.mailfrom=amutable.com; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b=ECS2W3Px; arc=none smtp.client-ip=209.85.214.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amutable.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b="ECS2W3Px" Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-2ad9516a653so27571205ad.0 for ; Tue, 31 Mar 2026 17:28:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amutable-com.20230601.gappssmtp.com; s=20230601; t=1775003311; x=1775608111; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=R+MmmkffR4AjWlUAh1Y8qnRVcsjhTa+f38j2owrthlg=; b=ECS2W3Pxds9+0eScQYDqj16+IgX/MrqXFR7n0i23oOt4srGFf43gigjTFmw1mVHw4e weh4QjxD97QdUxNq9Ydtd3x4frtJ1AnzoH8uve6GphWB2KKhNlXb1kKTPayqD3PoQT/u PsLVut5L+bilByZrPsdPnGMHzE5cKvOwp9+UpPrL4Fiu4cG0NOpXE6uGVADJZcMgXVXl H560KxV9wjl7ivVIGo1oYGQLrdbjChTJtLWo6JTH8w6yZaN9CAi0U4WGSYfYtdeObDP0 pyqnJvGK46emdlaIfnBVlfV9qLEt5mbuZTRDzeQjMHF33C8mP21A7nRQysIRxLIVx32m T9QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775003311; x=1775608111; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=R+MmmkffR4AjWlUAh1Y8qnRVcsjhTa+f38j2owrthlg=; b=j6SJbcflAHABlaGcTcsGDuJvdsKlyZLcjbJFxpivNIkzvgd391FCFP6ldEvTXCAUP3 Iaw6if2G7+go6Ehj3y79QTS9kee+HdC3C+rL08fNl+hjjIIpdZi7CG+Kq6auXjnM5T3H zpyYocN+pweLO50d4Qyf8bt2iOY/viUTcT38vCOIWgeHdSfXMaUOmKvbpcblQVHog6I4 vZ9kgK4e7k326C5+KkzB+FzWvF1oA6BDc61tYviESbems/xkqhmLVR2yqw8Of1Ib+oBH Lrr5qVom06xW0JXScEcWpkYO5FXGPRLZAgOC9l1zBwInuqpkMc0YScZ/QcMSbC/4LC+F rurw== X-Forwarded-Encrypted: i=1; AJvYcCU/UkM+DdJTEI79tdMCiGknb6gcgxDPxEnI5G+w596ZkMENYiRAqlo70l0Cl627aMmvsOEoefX49N/QSdU=@vger.kernel.org X-Gm-Message-State: AOJu0YzpdJ1JzRX4f14goFhmEQm69vokoYyXzuoFI1BzxNcZR49+2n5r G9WKya4IRTb33GZFhNHRhp7bkcMQGOnbXYNX96GgJyBEYrItjXuQxpDIm++lCwmFA54r X-Gm-Gg: ATEYQzxqze4uS8UOHrSQz//A9YEFua0iYzc6tRIGhP1oOcWB+ZcyS0PNTLpt60tEy6K sgNK054jyUsDfd3+jY/AoR5ugDNoSRawPbcML882RmriYcL6UvKG6yLzQK0+NV3HnAC+/YkCfS5 A9t/p4XSKd367fPpGT9dNeOZm0OlKlNpINKqgXgBdk17ZOYpVGtC784QJWRSL/lOkNF/pl5Nrp3 ca5Y7TfiiCl3NK5xbbQRmwmO+0PAGD1lm0ZrLNeK7lvgMRWpZWAiaGFTrMfgdwLQQwuwczujWv2 cOnRKbnyhnPxo4dxHfdwCgLlIQQZtk8GigRbSb37L9mL7Bv4k5ZOMdNsUzSRDvPTfkbN/QQbCUW qWOULNqtpEScLcW77rhwoxhF8x6MuMjcFMfmWPspU2ATa0SNSph5yJM4JgVBLogF10VRRssO0Ui yt4F3CHPBHAOmavvIox/zEzpYz82xMhEHza1RxT9eylpY4WgK/Y3/7iT1QFhs4wMjMoCnLcYyCU hmVeNaYdcg71RXGAtMc0gQirQ== X-Received: by 2002:a17:903:904:b0:2ae:57e6:616c with SMTP id d9443c01a7336-2b269a80ba6mr12616225ad.3.1775003311440; Tue, 31 Mar 2026 17:28:31 -0700 (PDT) Received: from thesius.dot.cyphar.com (2403-5809-a2fc-2-a90-9fd7-aedd-d405.ip6.aussiebb.net. [2403:5809:a2fc:2:a90:9fd7:aedd:d405]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b24264264fsm157057825ad.2.2026.03.31.17.28.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 17:28:31 -0700 (PDT) From: Aleksa Sarai Date: Wed, 01 Apr 2026 11:28:03 +1100 Subject: [PATCH v2 2/4] selftests: openat2: move helpers to header Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-openat2-selftests-kunit-v2-2-ad153a07da0c@amutable.com> References: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> In-Reply-To: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> To: Shuah Khan Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai , Aleksa Sarai X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=8185; i=aleksa@amutable.com; h=from:subject:message-id; bh=/SkZnf9rgHNSdrHLq0MfYdlt06Qrh2Su8iA0QXWXAkA=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWSeSVv86oFI7KKv1z147GpuBJfLq961X5zVucEyd96j1 N+W69lWdUxkYRDjYrAUU2TZ5ucZumn+4ivJn1aywcxhZQIZIi3SwMDAwMDCwJebmFdqpGOkZ6pt qGdopGOkY8TAxSkAU13FzPDPdv29e3onpm2+FPX7WmNe7dbMxc1WG8vedPevcH3Tb+WxgeGfluf nPTXZW46nJRzJ/PxH6KCumuNRU7sXlR7VApek1XMYAQ== X-Developer-Key: i=aleksa@amutable.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 This is a bit ugly, but in the next patch we will move to using kselftest_harness.h -- which doesn't play well with being included in multiple compilation units due to duplicate function definitions. Not including kselftest_harness.h would let us avoid this patch, but the helpers will need include kselftest_harness.h in order to switch to TH_LOG. Signed-off-by: Aleksa Sarai --- .../testing/selftests/filesystems/openat2/Makefile | 2 - .../selftests/filesystems/openat2/helpers.c | 109 ------------------ .../selftests/filesystems/openat2/helpers.h | 125 +++++++++++++++++= +--- 3 files changed, 107 insertions(+), 129 deletions(-) diff --git a/tools/testing/selftests/filesystems/openat2/Makefile b/tools/t= esting/selftests/filesystems/openat2/Makefile index f36dedccedb6..7736e37b7986 100644 --- a/tools/testing/selftests/filesystems/openat2/Makefile +++ b/tools/testing/selftests/filesystems/openat2/Makefile @@ -15,5 +15,3 @@ endif LOCAL_HDRS +=3D helpers.h =20 include ../../lib.mk - -$(TEST_GEN_PROGS): helpers.c diff --git a/tools/testing/selftests/filesystems/openat2/helpers.c b/tools/= testing/selftests/filesystems/openat2/helpers.c deleted file mode 100644 index 5074681ffdc9..000000000000 --- a/tools/testing/selftests/filesystems/openat2/helpers.c +++ /dev/null @@ -1,109 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* - * Author: Aleksa Sarai - * Copyright (C) 2018-2019 SUSE LLC. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include - -#include "helpers.h" - -bool needs_openat2(const struct open_how *how) -{ - return how->resolve !=3D 0; -} - -int raw_openat2(int dfd, const char *path, void *how, size_t size) -{ - int ret =3D syscall(__NR_openat2, dfd, path, how, size); - return ret >=3D 0 ? ret : -errno; -} - -int sys_openat2(int dfd, const char *path, struct open_how *how) -{ - return raw_openat2(dfd, path, how, sizeof(*how)); -} - -int sys_openat(int dfd, const char *path, struct open_how *how) -{ - int ret =3D openat(dfd, path, how->flags, how->mode); - return ret >=3D 0 ? ret : -errno; -} - -int sys_renameat2(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, unsigned int flags) -{ - int ret =3D syscall(__NR_renameat2, olddirfd, oldpath, - newdirfd, newpath, flags); - return ret >=3D 0 ? ret : -errno; -} - -int touchat(int dfd, const char *path) -{ - int fd =3D openat(dfd, path, O_CREAT, 0700); - if (fd >=3D 0) - close(fd); - return fd; -} - -char *fdreadlink(int fd) -{ - char *target, *tmp; - - E_asprintf(&tmp, "/proc/self/fd/%d", fd); - - target =3D malloc(PATH_MAX); - if (!target) - ksft_exit_fail_msg("fdreadlink: malloc failed\n"); - memset(target, 0, PATH_MAX); - - E_readlink(tmp, target, PATH_MAX); - free(tmp); - return target; -} - -bool fdequal(int fd, int dfd, const char *path) -{ - char *fdpath, *dfdpath, *other; - bool cmp; - - fdpath =3D fdreadlink(fd); - dfdpath =3D fdreadlink(dfd); - - if (!path) - E_asprintf(&other, "%s", dfdpath); - else if (*path =3D=3D '/') - E_asprintf(&other, "%s", path); - else - E_asprintf(&other, "%s/%s", dfdpath, path); - - cmp =3D !strcmp(fdpath, other); - - free(fdpath); - free(dfdpath); - free(other); - return cmp; -} - -bool openat2_supported =3D false; - -void __attribute__((constructor)) init(void) -{ - struct open_how how =3D {}; - int fd; - - BUILD_BUG_ON(sizeof(struct open_how) !=3D OPEN_HOW_SIZE_VER0); - - /* Check openat2(2) support. */ - fd =3D sys_openat2(AT_FDCWD, ".", &how); - openat2_supported =3D (fd >=3D 0); - - if (fd >=3D 0) - close(fd); -} diff --git a/tools/testing/selftests/filesystems/openat2/helpers.h b/tools/= testing/selftests/filesystems/openat2/helpers.h index 510e60602511..975de513af86 100644 --- a/tools/testing/selftests/filesystems/openat2/helpers.h +++ b/tools/testing/selftests/filesystems/openat2/helpers.h @@ -2,6 +2,7 @@ /* * Author: Aleksa Sarai * Copyright (C) 2018-2019 SUSE LLC. + * Copyright (C) 2026 Amutable GmbH */ =20 #ifndef __RESOLVEAT_H__ @@ -11,19 +12,14 @@ #include #include #include +#include #include +#include #include "kselftest.h" =20 #define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X))) #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); }))) =20 -#ifndef SYS_openat2 -#ifndef __NR_openat2 -#define __NR_openat2 437 -#endif /* __NR_openat2 */ -#define SYS_openat2 __NR_openat2 -#endif /* SYS_openat2 */ - /* * Arguments for how openat2(2) should open the target path. If @resolve is * zero, then openat2(2) operates very similarly to openat(2). @@ -45,8 +41,6 @@ struct open_how { #define OPEN_HOW_SIZE_VER0 24 /* sizeof first published struct */ #define OPEN_HOW_SIZE_LATEST OPEN_HOW_SIZE_VER0 =20 -bool needs_openat2(const struct open_how *how); - #ifndef RESOLVE_IN_ROOT /* how->resolve flags for openat2(2). */ #define RESOLVE_NO_XDEV 0x01 /* Block mount-point crossings @@ -93,16 +87,111 @@ bool needs_openat2(const struct open_how *how); __FILE__, __LINE__, #expr, ##__VA_ARGS__); \ } while (0) =20 -int raw_openat2(int dfd, const char *path, void *how, size_t size); -int sys_openat2(int dfd, const char *path, struct open_how *how); -int sys_openat(int dfd, const char *path, struct open_how *how); -int sys_renameat2(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, unsigned int flags); +__maybe_unused +static bool needs_openat2(const struct open_how *how) +{ + return how->resolve !=3D 0; +} + +__maybe_unused +static int raw_openat2(int dfd, const char *path, void *how, size_t size) +{ + int ret =3D syscall(__NR_openat2, dfd, path, how, size); + + return ret >=3D 0 ? ret : -errno; +} + +__maybe_unused +static int sys_openat2(int dfd, const char *path, struct open_how *how) +{ + return raw_openat2(dfd, path, how, sizeof(*how)); +} + +__maybe_unused +static int sys_openat(int dfd, const char *path, struct open_how *how) +{ + int ret =3D openat(dfd, path, how->flags, how->mode); + + return ret >=3D 0 ? ret : -errno; +} + +__maybe_unused +static int sys_renameat2(int olddirfd, const char *oldpath, + int newdirfd, const char *newpath, unsigned int flags) +{ + int ret =3D syscall(__NR_renameat2, olddirfd, oldpath, + newdirfd, newpath, flags); + + return ret >=3D 0 ? ret : -errno; +} + +__maybe_unused +static int touchat(int dfd, const char *path) +{ + int fd =3D openat(dfd, path, O_CREAT, 0700); + + if (fd >=3D 0) + close(fd); + return fd; +} + +__maybe_unused +static char *fdreadlink(int fd) +{ + char *target, *tmp; + + E_asprintf(&tmp, "/proc/self/fd/%d", fd); + + target =3D malloc(PATH_MAX); + if (!target) + ksft_exit_fail_msg("fdreadlink: malloc failed\n"); + memset(target, 0, PATH_MAX); + + E_readlink(tmp, target, PATH_MAX); + free(tmp); + return target; +} + +__maybe_unused +static bool fdequal(int fd, int dfd, const char *path) +{ + char *fdpath, *dfdpath, *other; + bool cmp; + + fdpath =3D fdreadlink(fd); + dfdpath =3D fdreadlink(dfd); + + if (!path) + E_asprintf(&other, "%s", dfdpath); + else if (*path =3D=3D '/') + E_asprintf(&other, "%s", path); + else + E_asprintf(&other, "%s/%s", dfdpath, path); + + cmp =3D !strcmp(fdpath, other); + + free(fdpath); + free(dfdpath); + free(other); + return cmp; +} + +static bool openat2_supported =3D false; + +__attribute__((constructor)) +static void __detect_openat2_supported(void) +{ + struct open_how how =3D {}; + int fd; + + BUILD_BUG_ON(sizeof(struct open_how) !=3D OPEN_HOW_SIZE_VER0); =20 -int touchat(int dfd, const char *path); -char *fdreadlink(int fd); -bool fdequal(int fd, int dfd, const char *path); + /* Check openat2(2) support. */ + fd =3D sys_openat2(AT_FDCWD, ".", &how); + openat2_supported =3D (fd >=3D 0); =20 -extern bool openat2_supported; + if (fd >=3D 0) + close(fd); +} =20 #endif /* __RESOLVEAT_H__ */ --=20 2.53.0