From nobody Fri Sep 25 03:17:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 1EC9C4A4857; Thu, 17 Sep 2026 09:09:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636198; cv=none; b=YbOBUyF8G8V+9nyxglAnh0ZI4xsCIwIyciH7/L8M0hFMw0CiInqBa7R+RLCqcV3NpcjR0TKWMFlHpXK0yFkhYS/xZDg4seFextReTyl1BUgbqrvKopo4RdWYhUprv1W6VAs510SOE2tUc2YF0J8UNDwf58KTV47nL8K2hdJNnpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636198; c=relaxed/simple; bh=DV2T4c+4xkA8K7vcmtRlGodHYsmKM293QENU4hzkJCg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pdEqUlANF0gawzRIpgBJ6DtN3SMehgjvgCIsmrOkDDQQWoTzEujbtVHLxsI8dXCwO4i/BTaFIJwa9F97UHRqYCUvSMZKvgh3StsaN0+im3zK7T652CQWWzsugwxa98+TofZv2mU4BMWP1dVN4uSydeMQflFy0mv3DH/rPQZwaK8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H+c+T6So; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="H+c+T6So" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D9651F00893; Thu, 17 Sep 2026 09:09:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789636197; bh=MyfKyJ2IFpUV90Fd+O/PB8hPgrmFQXEYPQE9FH2U5+0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=H+c+T6SoQ7bi7KDrdywXGdkUQno8IYfgoOVzvBXc3Nel3/0tmU8DEF2bj0ddSxyTA O5Ud3r8id3oPVDTbslazb9ysVE9ON6gawxat9WFfRF4Q+PLx3tYIdqDQvqfm0jTC6m /cgGcLDIbqt+VgC97EYTKWndcVJt15N6YxfgDmj5eu1UG9nTF1RZQgHnsvksdXMQ4J HeZT6FODg0Zu/NjiC+NyOdMvy6958db56K9fLSUbAfl+qnuWWJfqq2A6efvvaTYfVC yB/nZ71w6QgCB7D8AkTClKTn3kksvqPNqAXrGppLTexMhqcBqNHdlHzRY3+U9l7UwV l3kws5Udp65xA== From: Christian Brauner Date: Thu, 17 Sep 2026 11:09:42 +0200 Subject: [PATCH 1/4] file: simplify FD_PREPARE() 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: <20260917-work-file-fd_prepare-v1-1-b87534ca49f3@kernel.org> References: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> In-Reply-To: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Alexander Viro , Jan Kara , linuxppc-dev@lists.ozlabs.org, linux-gpio@vger.kernel.org, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, freedreno@lists.freedesktop.org, linux-media@vger.kernel.org, wine-devel@list.winehq.org, linux-xfs@vger.kernel.org, io-uring@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Christian Brauner (Amutable)" X-Mailer: b4 0.17-dev-db0b7 X-Developer-Signature: v=1; a=openpgp-sha256; l=5584; i=brauner@kernel.org; h=from:subject:message-id; bh=DV2T4c+4xkA8K7vcmtRlGodHYsmKM293QENU4hzkJCg=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWStXhep3M4ftMm/0tX7f+TWpDfvNhuyLq2zqitf971kY uliTU+TjlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgIm43Wf4Z6Buav4mVHvnrLJ/ F/qWul+VYpPL7NnUl7Jl6qoAjkVXrzD803sVsfDCxzW/N0zhWlmzN8d3Q+NUxk1xjkFhk3MLuXJ D+QE= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 It was originally built as an ACQUIRE-style guard but most of the infrastructure was never needed and introduced complexity that we really didn't need. Drop the ACQUIRE machinery and compute the error where the fd and file are set. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- include/linux/file.h | 99 ++++++++++++++++++++++--------------------------= ---- 1 file changed, 41 insertions(+), 58 deletions(-) diff --git a/include/linux/file.h b/include/linux/file.h index 27484b444d31..2b864f457211 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -12,6 +12,7 @@ #include #include #include +#include =20 struct file; =20 @@ -159,87 +160,69 @@ typedef struct fd_prepare class_fd_prepare_t; (_Generic((_fdf), struct fd_prepare: (_fdf).__file)) =20 /* Do not use directly. */ -static inline void class_fd_prepare_destructor(const struct fd_prepare *fd= f) +static __always_inline void class_fd_prepare_destructor(const struct fd_pr= epare *fdf) { - if (unlikely(fdf->__fd >=3D 0)) + if (unlikely(fdf->__fd >=3D 0)) { put_unused_fd(fdf->__fd); - if (unlikely(!IS_ERR_OR_NULL(fdf->__file))) fput(fdf->__file); + } } =20 /* Do not use directly. */ -static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf) +static __always_inline struct fd_prepare __fd_prepare(int fd, struct file = *file) { - if (unlikely(fdf->err)) - return fdf->err; - if (unlikely(fdf->__fd < 0)) - return fdf->__fd; - if (unlikely(IS_ERR(fdf->__file))) - return PTR_ERR(fdf->__file); - if (unlikely(!fdf->__file)) - return -ENOMEM; - return 0; -} + if (fd >=3D 0 && IS_ERR_OR_NULL(file)) { + int err =3D file ? PTR_ERR(file) : -ENOMEM; =20 -/* - * __FD_PREPARE_INIT - Helper to initialize fd_prepare class. - * @_fd_flags: flags for get_unused_fd_flags() - * @_file_owned: expression that returns struct file * - * - * Returns a struct fd_prepare with fd, file, and err set. - * If fd allocation fails, fd will be negative and err will be set. If - * fd succeeds but file_init_expr fails, file will be ERR_PTR and err - * will be set. The err field is the single source of truth for error - * checking. - */ -#define __FD_PREPARE_INIT(_fd_flags, _file_owned) \ - ({ \ - struct fd_prepare fdf =3D { \ - .__fd =3D get_unused_fd_flags((_fd_flags)), \ - }; \ - if (likely(fdf.__fd >=3D 0)) \ - fdf.__file =3D (_file_owned); \ - fdf.err =3D ACQUIRE_ERR(fd_prepare, &fdf); \ - fdf; \ - }) + put_unused_fd(fd); + fd =3D err; + file =3D NULL; + } + return (struct fd_prepare){ + .err =3D fd < 0 ? fd : 0, + .__fd =3D fd, + .__file =3D file, + }; +} =20 /* - * FD_PREPARE - Macro to declare and initialize an fd_prepare variable. + * FD_PREPARE - Declare and initialize an fd_prepare instance. * - * Declares and initializes an fd_prepare variable with automatic - * cleanup. No separate scope required - cleanup happens when variable - * goes out of scope. + * This allocates a new fd and only evaluates @_file_owned if the + * allocation succeeded. Cleanup happens when the variable goes out of + * scope and the guard releases whichever of the descriptor and the file + * was allocated. If fd_publish() was called the fd and file are + * published and cleanup becomes a nop. * * @_fdf: name of struct fd_prepare variable to define * @_fd_flags: flags for get_unused_fd_flags() * @_file_owned: struct file to take ownership of (can be expression) */ -#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \ - CLASS_INIT(fd_prepare, _fdf, __FD_PREPARE_INIT(_fd_flags, _file_owned)) +#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \ + CLASS_INIT(fd_prepare, _fdf, ({ \ + int __fd =3D get_unused_fd_flags(_fd_flags); \ + __fd_prepare(__fd, __fd < 0 ? NULL : (_file_owned)); \ + })) + +/* Do not use directly. */ +static __always_inline int __fd_publish(struct fd_prepare *fdf) +{ + VFS_WARN_ON_ONCE(fdf->__fd < 0); + fd_install(fdf->__fd, fdf->__file); + return take_fd(fdf->__fd); +} =20 /* * fd_publish - Publish prepared fd and file to the fd table. * @_fdf: struct fd_prepare variable */ -#define fd_publish(_fdf) \ - ({ \ - struct fd_prepare *fdp =3D &(_fdf); \ - VFS_WARN_ON_ONCE(fdp->err); \ - VFS_WARN_ON_ONCE(fdp->__fd < 0); \ - VFS_WARN_ON_ONCE(IS_ERR_OR_NULL(fdp->__file)); \ - fd_install(fdp->__fd, fdp->__file); \ - retain_and_null_ptr(fdp->__file); \ - take_fd(fdp->__fd); \ - }) +#define fd_publish(_fdf) __fd_publish(&(_fdf)) =20 /* Do not use directly. */ -#define __FD_ADD(_fdf, _fd_flags, _file_owned) \ - ({ \ - FD_PREPARE(_fdf, _fd_flags, _file_owned); \ - s32 ret =3D _fdf.err; \ - if (likely(!ret)) \ - ret =3D fd_publish(_fdf); \ - ret; \ +#define __FD_ADD(_fdf, _fd_flags, _file_owned) \ + ({ \ + FD_PREPARE(_fdf, _fd_flags, _file_owned); \ + _fdf.err ?: fd_publish(_fdf); \ }) =20 /* --=20 2.53.0 From nobody Fri Sep 25 03:17:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 DF8884A7CAD; Thu, 17 Sep 2026 09:10:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636203; cv=none; b=YWLbDMKOoGYA3gJJKE0m3oNv9vCKE7NUEJUpzJ9uPzVi8g6iVBBZR968e/sDhzAHDyTh0goEOvDj/soE6YlQ/8YZumgS4ZhJRzBjNTKCtmVOu5ddmiBezyt+aZ4XIHvXsQss0AN2cNQuLnfn1Rjj/aiV6hPb3qhfTSyA7iCLLUs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636203; c=relaxed/simple; bh=h8FDYLEJ2wCbTw+8dTetJhDBaeIOwFIz8qAAMf8e6eA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=F36orrdkgX/TnhsQuRk/w+CN6EuTf3QrAasvy5wZugF5QlAosuUsG3UclQNL2OX4fuWUPdAUPteyb3595IjOD7oBR2fEsM9cus/ho5KhqlK/lQT4dRKwzO8iKqDqh63sgGo3xQ5dEF3js998hFwjXmrFvdXlX4scPZnbbhDjfig= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l3gjnXE7; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="l3gjnXE7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 812851F00898; Thu, 17 Sep 2026 09:09:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789636201; bh=7Hss+tphsWYYElyG0udvHfxNWLscjSFYocLTPr6cLgQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=l3gjnXE7hyT5hOLZNPuXtJreEraAnkHGYjZOWrcBMNjrzVCWdvetpyMslrwhSfAkO dRQF4mzZw8Y2oBrhxgj1+KlIDVtCllYMWw6nEV2QDqvjo+PpIsY3F4eSkb7CFXTq5g 6ie6SAioCC9t3pGh2WqYiA1cVlB9RwhtH36z7l8mtgypnmlNz1f6coAtrJoiGo7Ic6 6bsNfAy8z4WRfiuNSoBwsbSg691wK9hc7SgDLYTu17XLnT1aSqTcvtkCnScw6mneEs xNybY3vM3kCYqY6KHxfBrts0RxMC6vlY8bnK4wzq921kn+rwbhbK1xm5mfT6Le9Y7q 2xoHozTmkIevA== From: Christian Brauner Date: Thu, 17 Sep 2026 11:09:43 +0200 Subject: [PATCH 2/4] file: declare the FD_PREPARE() variable with __cleanup() directly 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: <20260917-work-file-fd_prepare-v1-2-b87534ca49f3@kernel.org> References: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> In-Reply-To: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Alexander Viro , Jan Kara , linuxppc-dev@lists.ozlabs.org, linux-gpio@vger.kernel.org, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, freedreno@lists.freedesktop.org, linux-media@vger.kernel.org, wine-devel@list.winehq.org, linux-xfs@vger.kernel.org, io-uring@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Christian Brauner (Amutable)" X-Mailer: b4 0.17-dev-db0b7 X-Developer-Signature: v=1; a=openpgp-sha256; l=1726; i=brauner@kernel.org; h=from:subject:message-id; bh=h8FDYLEJ2wCbTw+8dTetJhDBaeIOwFIz8qAAMf8e6eA=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWStXhe5uVx3TjxPZLCm+imW81Fc3kEKSm+U25QOvTeX1 yj62pDUUcrCIMbFICumyOLQbhIut5ynYrNRpgbMHFYmkCEMXJwCMBEuTYb/Tue6jnQpfGRSycj4 7OZ9mvk0u8L1e66vj/06JZdSrMvxleF/vLJdkGZv25roipfn7P9vT3sQN/f+5M0M00LYou33PPz OCgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 The whole CLASS_INIT() detour isn't needed. Use __cleanup() directly and give the destructor a private name. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- include/linux/file.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/include/linux/file.h b/include/linux/file.h index 2b864f457211..926cc58d0b7f 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -146,9 +146,6 @@ struct fd_prepare { struct file *__file; /* do not access directly */ }; =20 -/* Typedef for fd_prepare cleanup guards. */ -typedef struct fd_prepare class_fd_prepare_t; - /* * Accessors for fd_prepare class members. * _Generic() is used for zero-cost type safety. @@ -160,7 +157,7 @@ typedef struct fd_prepare class_fd_prepare_t; (_Generic((_fdf), struct fd_prepare: (_fdf).__file)) =20 /* Do not use directly. */ -static __always_inline void class_fd_prepare_destructor(const struct fd_pr= epare *fdf) +static __always_inline void __fd_prepare_cleanup(const struct fd_prepare *= fdf) { if (unlikely(fdf->__fd >=3D 0)) { put_unused_fd(fdf->__fd); @@ -199,10 +196,10 @@ static __always_inline struct fd_prepare __fd_prepare= (int fd, struct file *file) * @_file_owned: struct file to take ownership of (can be expression) */ #define FD_PREPARE(_fdf, _fd_flags, _file_owned) \ - CLASS_INIT(fd_prepare, _fdf, ({ \ + struct fd_prepare _fdf __cleanup(__fd_prepare_cleanup) =3D ({ \ int __fd =3D get_unused_fd_flags(_fd_flags); \ __fd_prepare(__fd, __fd < 0 ? NULL : (_file_owned)); \ - })) + }) =20 /* Do not use directly. */ static __always_inline int __fd_publish(struct fd_prepare *fdf) --=20 2.53.0 From nobody Fri Sep 25 03:17:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 ECD93377AB9; Thu, 17 Sep 2026 09:10:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636207; cv=none; b=a8xDfzzojgjS7K25W7Ep7hYjHKlDm5gFdgeMhnrBMbcipS0kHkEiB9HCAK3e9YVNBOGY5qR2ojIEIbE1CG0mhZRueJzREv4BNNOjae+16UJdq3anEQ/7WWhhYhECU4AcvXvPHkH4UvuyMBEFoaFqsxhaXIcvmQvSTrIupmH6UdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636207; c=relaxed/simple; bh=gePQjgNJP3rZLgjxZMbNcyCVB0rhSfWKtw7rWIMHsCU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HoVV4ABPkNQIVIjDUbECBSJlB+YCy4ltEKaJaxN8a/SH1XIFBa2votgLRjUF4hc8VmU93F0LrSS8zLxu66QesX/LTQaoSFOtBCMIo/0OpYb8tKaETZzYDoxji4xcnQqIDiwjQeSu4NgXUC3ciURgbzveH6odWPj3Ydf847Igcy4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gB/wFH2C; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gB/wFH2C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0D271F00893; Thu, 17 Sep 2026 09:10:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789636205; bh=6dWahTpadJ8SVAXJFFy0YTTM47N8xZaWr7TRB490RZI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=gB/wFH2CZNIyTyzKz6rMZb09+PgNTjUmU9H+6Eu6QBXgv+KymxIPpgYbmkzflAQrU 333+7mJ3A2kMGLT23ZySfOlzw7hqX2G8T9dO6f/Qu32WgqkH066+pbE6q3csCBOJhn hVwJvSqenhZsWVto4JK5LUDNWiDFgSRr4SPiFoiBdYSNOulqmHCvK17qzpVrUB9knh DwxjmfSuTDS9Rvyi1IKr+fRBTuThlHhHi2c3yTEEnl8wU779+BxDljLnOBC063luH/ Qeu0whojY0FqyPpX+sBJ7x4WXtUCJ8RhJj4+xD+LK3aPYwpIvuZYf2EKHwPsuXaN/1 6baEMzbhlX7jA== From: Christian Brauner Date: Thu, 17 Sep 2026 11:09:44 +0200 Subject: [PATCH 3/4] cleanup: remove CLASS_INIT() 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: <20260917-work-file-fd_prepare-v1-3-b87534ca49f3@kernel.org> References: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> In-Reply-To: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Alexander Viro , Jan Kara , linuxppc-dev@lists.ozlabs.org, linux-gpio@vger.kernel.org, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, freedreno@lists.freedesktop.org, linux-media@vger.kernel.org, wine-devel@list.winehq.org, linux-xfs@vger.kernel.org, io-uring@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Christian Brauner (Amutable)" X-Mailer: b4 0.17-dev-db0b7 X-Developer-Signature: v=1; a=openpgp-sha256; l=1527; i=brauner@kernel.org; h=from:subject:message-id; bh=gePQjgNJP3rZLgjxZMbNcyCVB0rhSfWKtw7rWIMHsCU=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWStXhcptiHRLFxir8hFQ7cX4T8XKIlF/9FhyhR+65ynl qu6Wcqno5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgFYCJzVzD8lWq5Gco9c839ZU7p jdrmawS83i4NPfWqOvxHVrbIx6usagz/LFur6m/9XDa34GfizllL3I9kJfeIcS3yyg6IPH70c+Z CfgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 CLASS_INIT() was added for FD_PREPARE() in commit 011703a9acd7 ("file: add FD_{ADD,PREPARE}()") and never got another user. FD_PREPARE() now declares its variable with __cleanup() directly. Remove CLASS_INIT(). No functional changes. Signed-off-by: Christian Brauner (Amutable) --- include/linux/cleanup.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index b1b5698cbf1b..1fb8058b897d 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -261,10 +261,6 @@ const volatile void * __must_check_fn(const volatile v= oid *val) * CLASS(name, var)(args...): * declare the variable @var as an instance of the named class * - * CLASS_INIT(name, var, init_expr): - * declare the variable @var as an instance of the named class with - * custom initialization expression. - * * Ex. * * DEFINE_CLASS(fdget, struct fd, fdput(_T), fdget(fd), int fd) @@ -302,9 +298,6 @@ static __always_inline class_##_name##_t class_##_name#= #ext##_constructor(_init_ class_##_name##_t var __cleanup(class_##_name##_destructor) =3D \ class_##_name##_constructor =20 -#define CLASS_INIT(_name, _var, _init_expr) \ - class_##_name##_t _var __cleanup(class_##_name##_destructor) =3D (= _init_expr) - #define __scoped_class(_name, var, _label, args...) \ for (CLASS(_name, var)(args); ; ({ goto _label; })) \ if (0) { \ --=20 2.53.0 From nobody Fri Sep 25 03:17:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 C62CE4A4409; Thu, 17 Sep 2026 09:10:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636212; cv=none; b=lYxCImIBj6T3NgYa6a/Q2+Fpjrtn3cHbNAnr2GutrgCml8a/AffLcJw1gY81UJKBNYXalKoxs89q0jJ2Y7vuAL9Whgni8U4OWTA3mjhFG9vaqom13IIOme+MMMJr6KKYN4/cWxsoG9FS2ecOH1nf+8FPhPy1KsgHMyLyif7QQCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789636212; c=relaxed/simple; bh=4TVATxkIdSojFG8h8ibaa3nhcXlk0rv552POIhYv3M4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QOsA8Hp1OF1Q+KRW8PTYOFK/X+lxzZuSFTckTl6D/g3vUrZwWdF2lPvXcXZOuj5ZNMrPBBas7La1Fx0A12Ozh+BSipVgB24YkH58bVnGKmy4AXQ5KJgcdiNqxIjD47vLevKSl16c3C0Xj0BfnLUNTH3pz4FELVgXpbSo1sF1xUo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TU+vHQdZ; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TU+vHQdZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CBED1F000FF; Thu, 17 Sep 2026 09:10:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789636210; bh=QH2WDKb8N1csYIAwFwWTZJ+veVOResNI8NfDe/HwrVs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=TU+vHQdZQFBVLHxYRqINdkcmjBoejnE/VDffiTWv+Kv0ML+iwy2Y+tKyasd6eZNpq Ck2IUqodncPwUTEXsLZoJzMgTfRfhLR7DsFm2GcUdUjT/2lZWfH7ztoLl1UCDhTbkv gLFkFO1WSeI0495U5lwO8kxpWovsLK+TF9XFdy5XIYfpMXSqEVla/FIXyRPp2k5dAN dFh7T9VkaAP+4ZepAOOHB3VoqESKk/1BclxtaSO4Msqhmo5pYMcCmbbrzjjQJwSdPV Gk1zhpUdM1ksiPoUhJD0naTEyyqMNCXIjnknl8nng0c8tK0y9mdRQIbsKjFbyeTCGA IXqfi8QJ6WYfw== From: Christian Brauner Date: Thu, 17 Sep 2026 11:09:45 +0200 Subject: [PATCH 4/4] file: make struct fd_prepare const and kill its err field 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: <20260917-work-file-fd_prepare-v1-4-b87534ca49f3@kernel.org> References: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> In-Reply-To: <20260917-work-file-fd_prepare-v1-0-b87534ca49f3@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Alexander Viro , Jan Kara , linuxppc-dev@lists.ozlabs.org, linux-gpio@vger.kernel.org, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, freedreno@lists.freedesktop.org, linux-media@vger.kernel.org, wine-devel@list.winehq.org, linux-xfs@vger.kernel.org, io-uring@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, "Christian Brauner (Amutable)" X-Mailer: b4 0.17-dev-db0b7 X-Developer-Signature: v=1; a=openpgp-sha256; l=19031; i=brauner@kernel.org; h=from:subject:message-id; bh=4TVATxkIdSojFG8h8ibaa3nhcXlk0rv552POIhYv3M4=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWStXhdZ41vZpHLF51FP6LTChIeFL7vbmKVjdjH4/TBPs V3PpTq5o5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgFYCIKcgz/0xeJLtmzKHxmOeui 4KDnWxuk57UzZjk43Di8tvZzovPjLob/Ka9m8bpar2fZebD59WXH8jWM0YVMumx7dDb4XNjmcUm aDQA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 FD_PREPARE() releases the fd as soon as the file expression fails. The fd either holds the descriptor or the error. The separate field is redundant. Make fd and file plain members and struct fd_prepare simpler. Callers of FD_PREPARE() get a const pointer to the guard. Simplify all users as they can now easily access the trivial struct. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- arch/powerpc/platforms/cell/spufs/inode.c | 12 +++--- drivers/gpio/gpiolib-cdev.c | 18 ++++----- drivers/gpu/drm/msm/msm_perfcntr.c | 4 +- drivers/media/mc/mc-request.c | 8 ++-- drivers/misc/ntsync.c | 6 +-- fs/eventfd.c | 4 +- fs/eventpoll.c | 6 +-- fs/file.c | 8 ++-- fs/namespace.c | 12 +++--- fs/nsfs.c | 4 +- fs/xfs/xfs_handle.c | 6 +-- include/linux/file.h | 66 +++++++++++++--------------= ---- io_uring/mock_file.c | 8 ++-- kernel/bpf/bpf_iter.c | 6 +-- kernel/bpf/token.c | 6 +-- mm/userfaultfd.c | 6 +-- net/core/scm.c | 8 ++-- net/handshake/netlink.c | 8 ++-- net/kcm/kcmsock.c | 6 +-- 19 files changed, 95 insertions(+), 107 deletions(-) diff --git a/arch/powerpc/platforms/cell/spufs/inode.c b/arch/powerpc/platf= orms/cell/spufs/inode.c index 2b54afb31529..5c06f69c9f7f 100644 --- a/arch/powerpc/platforms/cell/spufs/inode.c +++ b/arch/powerpc/platforms/cell/spufs/inode.c @@ -266,9 +266,9 @@ spufs_mkdir(struct inode *dir, struct dentry *dentry, u= nsigned int flags, static int spufs_context_open(const struct path *path) { FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred())); - if (fdf.err) - return fdf.err; - fd_prepare_file(fdf)->f_op =3D &spufs_context_fops; + if (fdf->fd < 0) + return fdf->fd; + fdf->file->f_op =3D &spufs_context_fops; return fd_publish(fdf); } =20 @@ -499,9 +499,9 @@ static int spufs_gang_open(const struct path *path) * in error path of *_open(). */ FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred())); - if (fdf.err) - return fdf.err; - fd_prepare_file(fdf)->f_op =3D &spufs_gang_fops; + if (fdf->fd < 0) + return fdf->fd; + fdf->file->f_op =3D &spufs_gang_fops; return fd_publish(fdf); } =20 diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 9f3b628d5793..6526ce27ea44 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -377,11 +377,11 @@ static int linehandle_create(struct gpio_device *gdev= , void __user *ip) FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC, anon_inode_getfile("gpio-linehandle", &linehandle_fileops, lh, O_RDONLY | O_CLOEXEC)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; retain_and_null_ptr(lh); =20 - handlereq.fd =3D fd_prepare_fd(fdf); + handlereq.fd =3D fdf->fd; if (copy_to_user(ip, &handlereq, sizeof(handlereq))) return -EFAULT; =20 @@ -1715,11 +1715,11 @@ static int linereq_create(struct gpio_device *gdev,= void __user *ip) FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC, anon_inode_getfile("gpio-line", &line_fileops, lr, O_RDONLY | O_CLOEXEC)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; retain_and_null_ptr(lr); =20 - ulr.fd =3D fd_prepare_fd(fdf); + ulr.fd =3D fdf->fd; if (copy_to_user(ip, &ulr, sizeof(ulr))) return -EFAULT; =20 @@ -2115,11 +2115,11 @@ static int lineevent_create(struct gpio_device *gde= v, void __user *ip) FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC, anon_inode_getfile("gpio-event", &lineevent_fileops, le, O_RDONLY | O_CLOEXEC)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; retain_and_null_ptr(le); =20 - eventreq.fd =3D fd_prepare_fd(fdf); + eventreq.fd =3D fdf->fd; if (copy_to_user(ip, &eventreq, sizeof(eventreq))) return -EFAULT; =20 diff --git a/drivers/gpu/drm/msm/msm_perfcntr.c b/drivers/gpu/drm/msm/msm_p= erfcntr.c index ce65b1160955..7fa2e858bd08 100644 --- a/drivers/gpu/drm/msm/msm_perfcntr.c +++ b/drivers/gpu/drm/msm/msm_perfcntr.c @@ -543,8 +543,8 @@ msm_ioctl_perfcntr_config(struct drm_device *dev, void = *data, struct drm_file *f =20 FD_PREPARE(fdf, O_CLOEXEC, anon_inode_getfile("[msm_perfcntrs]", &stream_fops, stream, 0)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 INIT_WORK(&stream->sel_work, sel_worker); kthread_init_work(&stream->sample_work, sample_worker); diff --git a/drivers/media/mc/mc-request.c b/drivers/media/mc/mc-request.c index 13e77648807c..e1387f039780 100644 --- a/drivers/media/mc/mc-request.c +++ b/drivers/media/mc/mc-request.c @@ -316,15 +316,15 @@ int media_request_alloc(struct media_device *mdev, in= t *alloc_fd) FD_PREPARE(fdf, O_CLOEXEC, anon_inode_getfile("request", &request_fops, NULL, O_CLOEXEC)); - if (fdf.err) { - ret =3D fdf.err; + if (fdf->fd < 0) { + ret =3D fdf->fd; goto err_free_req; } =20 - fd_prepare_file(fdf)->private_data =3D req; + fdf->file->private_data =3D req; =20 snprintf(req->debug_str, sizeof(req->debug_str), "%u:%d", - atomic_inc_return(&mdev->request_id), fd_prepare_fd(fdf)); + atomic_inc_return(&mdev->request_id), fdf->fd); atomic_inc(&mdev->num_requests); dev_dbg(mdev->dev, "request: allocated %s\n", req->debug_str); =20 diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 4a805919bb0c..2857ae37d3c8 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -724,9 +724,9 @@ static int ntsync_obj_get_fd(struct ntsync_obj *obj) { FD_PREPARE(fdf, O_CLOEXEC, anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR)); - if (fdf.err) - return fdf.err; - obj->file =3D fd_prepare_file(fdf); + if (fdf->fd < 0) + return fdf->fd; + obj->file =3D fdf->file; return fd_publish(fdf); } =20 diff --git a/fs/eventfd.c b/fs/eventfd.c index 9d33a02757d5..52426795752e 100644 --- a/fs/eventfd.c +++ b/fs/eventfd.c @@ -403,8 +403,8 @@ static int do_eventfd(unsigned int count, int flags) FD_PREPARE(fdf, flags, anon_inode_getfile_fmode("[eventfd]", &eventfd_fops, ctx, flags, FMODE_NOWAIT)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 ctx->id =3D ida_alloc(&eventfd_ida, GFP_KERNEL); retain_and_null_ptr(ctx); diff --git a/fs/eventpoll.c b/fs/eventpoll.c index e0c4bf88a838..f48b829a710f 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -2514,11 +2514,11 @@ static int do_epoll_create(int flags) FD_PREPARE(fdf, O_RDWR | (flags & O_CLOEXEC), anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep, O_RDWR | (flags & O_CLOEXEC))); - if (fdf.err) { + if (fdf->fd < 0) { ep_clear_and_put(ep); - return fdf.err; + return fdf->fd; } - ep->file =3D fd_prepare_file(fdf); + ep->file =3D fdf->file; return fd_publish(fdf); } =20 diff --git a/fs/file.c b/fs/file.c index 628ca07dc4b1..2a01b22eb65a 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1391,17 +1391,17 @@ int receive_fd(struct file *file, int __user *ufd, = unsigned int o_flags) return error; =20 FD_PREPARE(fdf, o_flags, file); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; get_file(file); =20 if (ufd) { - error =3D put_user(fd_prepare_fd(fdf), ufd); + error =3D put_user(fdf->fd, ufd); if (error) return error; } =20 - __receive_sock(fd_prepare_file(fdf)); + __receive_sock(fdf->file); return fd_publish(fdf); } EXPORT_SYMBOL_GPL(receive_fd); diff --git a/fs/namespace.c b/fs/namespace.c index 1ecd96c918b3..0f5c00816775 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -4544,16 +4544,16 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, = flags, =20 FD_PREPARE(fdf, (flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0, dentry_open(&new_path, O_PATH, fc->cred)); - if (fdf.err) { + if (fdf->fd < 0) { dissolve_on_fput(new_path.mnt); - return fdf.err; + return fdf->fd; } =20 /* * Attach to an apparent O_PATH fd with a note that we * need to unmount it, not just simply put it. */ - fd_prepare_file(fdf)->f_mode |=3D FMODE_NEED_UNMOUNT; + fdf->file->f_mode |=3D FMODE_NEED_UNMOUNT; return fd_publish(fdf); } =20 @@ -5198,12 +5198,12 @@ SYSCALL_DEFINE5(open_tree_attr, int, dfd, const cha= r __user *, filename, return -EINVAL; =20 FD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 if (uattr) { struct mount_kattr kattr =3D {}; - struct file *file =3D fd_prepare_file(fdf); + struct file *file =3D fdf->file; int ret; =20 if (flags & OPEN_TREE_CLONE) diff --git a/fs/nsfs.c b/fs/nsfs.c index c3b6ae76594a..56ea0bb9ef3a 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -348,8 +348,8 @@ static long ns_ioctl(struct file *filp, unsigned int io= ctl, return ret; =20 FD_PREPARE(fdf, O_CLOEXEC, dentry_open(&path, O_RDONLY, current_cred())); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; /* * If @uinfo is passed return all information about the * mount namespace as well. diff --git a/fs/xfs/xfs_handle.c b/fs/xfs/xfs_handle.c index 0689cade8f74..303bcff64459 100644 --- a/fs/xfs/xfs_handle.c +++ b/fs/xfs/xfs_handle.c @@ -272,11 +272,11 @@ xfs_open_by_handle( path.mnt =3D mntget(parfilp->f_path.mnt); =20 FD_PREPARE(fdf, 0, dentry_open(&path, hreq->oflags, cred)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 if (S_ISREG(inode->i_mode)) { - struct file *filp =3D fd_prepare_file(fdf); + struct file *filp =3D fdf->file; =20 filp->f_flags |=3D O_NOATIME; filp->f_mode |=3D FMODE_NOCMTIME; diff --git a/include/linux/file.h b/include/linux/file.h index 926cc58d0b7f..41c3c0be1064 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -130,38 +130,26 @@ extern unsigned int sysctl_nr_open_min, sysctl_nr_ope= n_max; =20 /* * fd_prepare: Combined fd + file allocation cleanup class. - * @err: Error code to indicate if allocation succeeded. - * @__fd: Allocated fd (may not be accessed directly) - * @__file: Allocated struct file pointer (may not be accessed directly) + * @fd: Allocated fd + * @file: Allocated struct file pointer * * Allocates an fd and a file together. On error paths, automatically clea= ns * up whichever resource was successfully allocated. Allows flexible file * allocation with different functions per usage. * - * Do not use directly. + * Do not declare directly, use FD_PREPARE(). */ struct fd_prepare { - s32 err; - s32 __fd; /* do not access directly */ - struct file *__file; /* do not access directly */ + int fd; + struct file *file; }; =20 -/* - * Accessors for fd_prepare class members. - * _Generic() is used for zero-cost type safety. - */ -#define fd_prepare_fd(_fdf) \ - (_Generic((_fdf), struct fd_prepare: (_fdf).__fd)) - -#define fd_prepare_file(_fdf) \ - (_Generic((_fdf), struct fd_prepare: (_fdf).__file)) - /* Do not use directly. */ static __always_inline void __fd_prepare_cleanup(const struct fd_prepare *= fdf) { - if (unlikely(fdf->__fd >=3D 0)) { - put_unused_fd(fdf->__fd); - fput(fdf->__file); + if (unlikely(fdf->fd >=3D 0)) { + put_unused_fd(fdf->fd); + fput(fdf->file); } } =20 @@ -175,11 +163,7 @@ static __always_inline struct fd_prepare __fd_prepare(= int fd, struct file *file) fd =3D err; file =3D NULL; } - return (struct fd_prepare){ - .err =3D fd < 0 ? fd : 0, - .__fd =3D fd, - .__file =3D file, - }; + return (struct fd_prepare){ .fd =3D fd, .file =3D file }; } =20 /* @@ -191,35 +175,39 @@ static __always_inline struct fd_prepare __fd_prepare= (int fd, struct file *file) * was allocated. If fd_publish() was called the fd and file are * published and cleanup becomes a nop. * - * @_fdf: name of struct fd_prepare variable to define + * @_fdf: name of the const struct fd_prepare pointer to define * @_fd_flags: flags for get_unused_fd_flags() * @_file_owned: struct file to take ownership of (can be expression) */ -#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \ - struct fd_prepare _fdf __cleanup(__fd_prepare_cleanup) =3D ({ \ +#define __FD_PREPARE(_guard, _fdf, _fd_flags, _file_owned) \ + struct fd_prepare _guard __cleanup(__fd_prepare_cleanup) =3D ({ \ int __fd =3D get_unused_fd_flags(_fd_flags); \ __fd_prepare(__fd, __fd < 0 ? NULL : (_file_owned)); \ - }) + }); \ + const struct fd_prepare *const _fdf =3D &_guard =20 -/* Do not use directly. */ -static __always_inline int __fd_publish(struct fd_prepare *fdf) -{ - VFS_WARN_ON_ONCE(fdf->__fd < 0); - fd_install(fdf->__fd, fdf->__file); - return take_fd(fdf->__fd); -} +#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \ + __FD_PREPARE(__UNIQUE_ID(fd_prepare), _fdf, _fd_flags, _file_owned) =20 /* * fd_publish - Publish prepared fd and file to the fd table. - * @_fdf: struct fd_prepare variable + * @fdf: struct fd_prepare pointer defined by FD_PREPARE() */ -#define fd_publish(_fdf) __fd_publish(&(_fdf)) +static __always_inline int fd_publish(const struct fd_prepare *fdf) +{ + /* Callers only get a const view, the guard itself is writable. */ + struct fd_prepare *guard =3D (struct fd_prepare *)fdf; + + VFS_WARN_ON_ONCE(guard->fd < 0); + fd_install(guard->fd, guard->file); + return take_fd(guard->fd); +} =20 /* Do not use directly. */ #define __FD_ADD(_fdf, _fd_flags, _file_owned) \ ({ \ FD_PREPARE(_fdf, _fd_flags, _file_owned); \ - _fdf.err ?: fd_publish(_fdf); \ + _fdf->fd < 0 ? _fdf->fd : fd_publish(_fdf); \ }) =20 /* diff --git a/io_uring/mock_file.c b/io_uring/mock_file.c index b318ed697998..9f0b4d850c12 100644 --- a/io_uring/mock_file.c +++ b/io_uring/mock_file.c @@ -257,17 +257,17 @@ static int io_create_mock_file(struct io_uring_cmd *c= md, unsigned int issue_flag FD_PREPARE(fdf, O_RDWR | O_CLOEXEC, anon_inode_create_getfile("[io_uring_mock]", fops, mf, O_RDWR | O_CLOEXEC, NULL)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 retain_and_null_ptr(mf); - file =3D fd_prepare_file(fdf); + file =3D fdf->file; file->f_mode |=3D FMODE_READ | FMODE_CAN_READ | FMODE_WRITE | FMODE_CAN_WRITE | FMODE_LSEEK; if (mc.flags & IORING_MOCK_CREATE_F_SUPPORT_NOWAIT) file->f_mode |=3D FMODE_NOWAIT; =20 - mc.out_fd =3D fd_prepare_fd(fdf); + mc.out_fd =3D fdf->fd; if (copy_to_user(uarg, &mc, uarg_size)) return -EFAULT; =20 diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c index 14a5fdfa0421..5f790d12f72e 100644 --- a/kernel/bpf/bpf_iter.c +++ b/kernel/bpf/bpf_iter.c @@ -643,11 +643,11 @@ int bpf_iter_new_fd(struct bpf_link *link) flags =3D O_RDONLY | O_CLOEXEC; =20 FD_PREPARE(fdf, flags, anon_inode_getfile("bpf_iter", &bpf_iter_fops, NUL= L, flags)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 iter_link =3D container_of(link, struct bpf_iter_link, link); - err =3D prepare_seq_file(fd_prepare_file(fdf), iter_link); + err =3D prepare_seq_file(fdf->file, iter_link); if (err) return err; /* Automatic cleanup handles fput */ =20 diff --git a/kernel/bpf/token.c b/kernel/bpf/token.c index e85a179523f0..da915a4f972b 100644 --- a/kernel/bpf/token.c +++ b/kernel/bpf/token.c @@ -169,8 +169,8 @@ int bpf_token_create(union bpf_attr *attr) FD_PREPARE(fdf, O_CLOEXEC, alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME, O_RDWR, &bpf_token_fops)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 token =3D kzalloc_obj(*token, GFP_USER); if (!token) @@ -190,7 +190,7 @@ int bpf_token_create(union bpf_attr *attr) return err; =20 get_user_ns(token->userns); - fd_prepare_file(fdf)->private_data =3D no_free_ptr(token); + fdf->file->private_data =3D no_free_ptr(token); return fd_publish(fdf); } =20 diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 23fb68fce000..ea382aedfc90 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -4809,12 +4809,12 @@ static int new_userfaultfd(int flags) anon_inode_create_getfile("[userfaultfd]", &userfaultfd_fops, ctx, O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 /* prevent the mm struct to be freed */ mmgrab(ctx->mm); - fd_prepare_file(fdf)->f_mode |=3D FMODE_NOWAIT; + fdf->file->f_mode |=3D FMODE_NOWAIT; retain_and_null_ptr(ctx); return fd_publish(fdf); } diff --git a/net/core/scm.c b/net/core/scm.c index f0d44ecdb11f..15c330784a69 100644 --- a/net/core/scm.c +++ b/net/core/scm.c @@ -364,14 +364,14 @@ int scm_recv_one_fd(struct file *f, int __user *ufd, = unsigned int flags, return notrunc ? put_user(error, ufd) : error; =20 FD_PREPARE(fdf, flags, get_file(f)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 - error =3D put_user(fd_prepare_fd(fdf), ufd); + error =3D put_user(fdf->fd, ufd); if (error) return error; =20 - __receive_sock(fd_prepare_file(fdf)); + __receive_sock(fdf->file); return fd_publish(fdf); } =20 diff --git a/net/handshake/netlink.c b/net/handshake/netlink.c index 3fd4fef9bab1..73b8314d9010 100644 --- a/net/handshake/netlink.c +++ b/net/handshake/netlink.c @@ -107,17 +107,17 @@ int handshake_nl_accept_doit(struct sk_buff *skb, str= uct genl_info *info) req =3D handshake_req_next(hn, class); if (req) { FD_PREPARE(fdf, O_CLOEXEC, req->hr_file); - if (fdf.err) { + if (fdf->fd < 0) { fput(req->hr_file); /* drop ref from handshake_req_next() */ - err =3D fdf.err; + err =3D fdf->fd; goto out_complete; } =20 - err =3D req->hr_proto->hp_accept(req, info, fd_prepare_fd(fdf)); + err =3D req->hr_proto->hp_accept(req, info, fdf->fd); if (err) goto out_complete; /* Automatic cleanup handles fput */ =20 - trace_handshake_cmd_accept(net, req, req->hr_sk, fd_prepare_fd(fdf)); + trace_handshake_cmd_accept(net, req, req->hr_sk, fdf->fd); fd_publish(fdf); return 0; } diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index 71af69d442f2..962ee2c4acd2 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -1580,10 +1580,10 @@ static int kcm_ioctl(struct socket *sock, unsigned = int cmd, unsigned long arg) struct kcm_clone info; =20 FD_PREPARE(fdf, 0, kcm_clone(sock)); - if (fdf.err) - return fdf.err; + if (fdf->fd < 0) + return fdf->fd; =20 - info.fd =3D fd_prepare_fd(fdf); + info.fd =3D fdf->fd; if (copy_to_user((void __user *)arg, &info, sizeof(info))) return -EFAULT; =20 --=20 2.53.0