From: Christian Brauner <brauner@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>,
Jeff Layton <jlayton@kernel.org>,
Amir Goldstein <amir73il@gmail.com>,
Jens Axboe <axboe@kernel.dk>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org,
Christian Brauner <brauner@kernel.org>
Subject: [PATCH RFC v2 01/48] file: add FD_PREPARE()
Date: Thu, 20 Nov 2025 23:31:58 +0100 [thread overview]
Message-ID: <20251120-work-fd-prepare-v2-1-fef6ebda05d3@kernel.org> (raw)
In-Reply-To: <20251120-work-fd-prepare-v2-0-fef6ebda05d3@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
include/linux/cleanup.h | 32 +++++++++++
include/linux/file.h | 141 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 173 insertions(+)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 19c7e475d3a4..4b5b41bafd47 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -255,12 +255,20 @@ const volatile void * __must_check_fn(const volatile void *val)
* @exit is an expression using '_T' -- similar to FREE above.
* @init is an expression in @init_args resulting in @type
*
+ * DEFINE_CLASS_TYPE(name, type, exit):
+ * Like DEFINE_CLASS but without a constructor. Use with CLASS_INIT()
+ * for classes that need custom initialization expressions per usage.
+ *
* EXTEND_CLASS(name, ext, init, init_args...):
* extends class @name to @name@ext with the new constructor
*
* 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. Use with DEFINE_CLASS_TYPE().
+ *
* Ex.
*
* DEFINE_CLASS(fdget, struct fd, fdput(_T), fdget(fd), int fd)
@@ -270,6 +278,12 @@ const volatile void * __must_check_fn(const volatile void *val)
* return -EBADF;
*
* // use 'f' without concern
+ *
+ * DEFINE_CLASS_TYPE(fd_file, struct { int fd; struct file *file; }, ...)
+ *
+ * CLASS_INIT(fd_file, ff, custom_init_expression);
+ * if (ff.fd < 0)
+ * return ff.fd;
*/
#define DEFINE_CLASS(_name, _type, _exit, _init, _init_args...) \
@@ -279,6 +293,11 @@ static inline void class_##_name##_destructor(_type *p) \
static inline _type class_##_name##_constructor(_init_args) \
{ _type t = _init; return t; }
+#define DEFINE_CLASS_TYPE(_name, _type, _exit) \
+typedef _type class_##_name##_t; \
+static inline void class_##_name##_destructor(_type *p) \
+{ _type _T = *p; _exit; }
+
#define EXTEND_CLASS(_name, ext, _init, _init_args...) \
typedef class_##_name##_t class_##_name##ext##_t; \
static inline void class_##_name##ext##_destructor(class_##_name##_t *p)\
@@ -290,6 +309,9 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
class_##_name##_t var __cleanup(class_##_name##_destructor) = \
class_##_name##_constructor
+#define CLASS_INIT(_name, _var, _init_expr) \
+ class_##_name##_t _var __cleanup(class_##_name##_destructor) = (_init_expr)
+
#define __scoped_class(_name, var, _label, args...) \
for (CLASS(_name, var)(args); ; ({ goto _label; })) \
if (0) { \
@@ -297,9 +319,19 @@ _label: \
break; \
} else
+#define __scoped_class_init(_name, var, _init_expr, _label) \
+ for (CLASS_INIT(_name, var, _init_expr); ; ({ goto _label; })) \
+ if (0) { \
+_label: \
+ break; \
+ } else
+
#define scoped_class(_name, var, args...) \
__scoped_class(_name, var, __UNIQUE_ID(label), args)
+#define scoped_class_init(_name, var, _init_expr) \
+ __scoped_class_init(_name, var, _init_expr, __UNIQUE_ID(label))
+
/*
* DEFINE_GUARD(name, type, lock, unlock):
* trivial wrapper around DEFINE_CLASS() above specifically
diff --git a/include/linux/file.h b/include/linux/file.h
index af1768d934a0..67dbc6b704f7 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -12,6 +12,7 @@
#include <linux/errno.h>
#include <linux/cleanup.h>
#include <linux/err.h>
+#include <linux/vfsdebug.h>
struct file;
@@ -127,4 +128,144 @@ extern void __fput_sync(struct file *);
extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
+/*
+ * fd_prepare class: Combined fd + file allocation with automatic cleanup.
+ *
+ * Allocates an fd and a file together. On error paths, automatically cleans
+ * up whichever resource was successfully allocated. Allows flexible file
+ * allocation with different functions per usage.
+ */
+
+struct fd_prepare {
+ struct file *file;
+ int fd;
+};
+
+/*
+ * fd_prepare_fd() - Get fd from fd_prepare structure
+ * @fd_prepare: struct fd_prepare to extract fd from
+ *
+ * Returns the file descriptor from an fd_prepare structure.
+ *
+ * Return: The file descriptor
+ */
+static inline int fd_prepare_fd(struct fd_prepare fdp)
+{
+ return fdp.fd;
+}
+
+/*
+ * fd_prepare_file() - Get file from fd_prepare structure
+ * @fd_prepare: struct fd_prepare to extract file from
+ *
+ * Returns the file pointer from an fd_prepare structure.
+ *
+ * Return: The file pointer
+ */
+static inline struct file *fd_prepare_file(struct fd_prepare fdp)
+{
+ return fdp.file;
+}
+
+/*
+ * fd_prepare_failed() - Check if fd_prepare allocation failed
+ * @fd_prepare: struct fd_prepare to check
+ *
+ * Checks whether either the fd allocation or file allocation failed.
+ *
+ * Return: true if either allocation failed, false otherwise
+ */
+static inline bool fd_prepare_failed(struct fd_prepare fdp)
+{
+ VFS_WARN_ON_ONCE(fdp.fd < 0 && IS_ERR(fdp.file));
+ return fdp.fd < 0 || IS_ERR(fdp.file);
+}
+
+/*
+ * fd_prepare_error() - Get error from failed fd_prepare
+ * @fd_prepare: struct fd_prepare to extract error from
+ *
+ * Returns the error code from the first allocation that failed.
+ * Should only be called after fd_prepare_failed() returns true.
+ *
+ * Return: Negative error code
+ */
+static inline int fd_prepare_error(struct fd_prepare fdp)
+{
+ if (fdp.fd < 0) {
+ VFS_WARN_ON_ONCE(fdp.file);
+ return fdp.fd;
+ }
+ if (!fdp.file)
+ return -ENOMEM;
+ return PTR_ERR(fdp.file);
+}
+
+static __always_inline void __fd_prepare_put(struct fd_prepare fdp)
+{
+ if (unlikely(fdp.fd >= 0))
+ put_unused_fd(fdp.fd);
+ if (unlikely(!IS_ERR_OR_NULL(fdp.file)))
+ fput(fdp.file);
+}
+
+DEFINE_CLASS_TYPE(fd_prepare, struct fd_prepare, __fd_prepare_put(_T))
+
+/*
+ * __FD_PREPARE_INIT(fd_flags, file_init_expr):
+ * Helper to initialize fd_prepare class.
+ * @fd_flags: flags for get_unused_fd_flags()
+ * @file_init_expr: expression that returns struct file *
+ *
+ * Returns a struct fd_prepare with fd and file set.
+ * If fd allocation fails, file will be NULL.
+ * If fd succeeds but file_init_expr fails, fd will be cleaned up.
+ */
+#define __FD_PREPARE_INIT(_fd_flags, _file_init_owned) \
+ ({ \
+ struct fd_prepare _fd_prepare = { \
+ .fd = get_unused_fd_flags((_fd_flags)), \
+ }; \
+ if (likely(_fd_prepare.fd >= 0)) \
+ _fd_prepare.file = (_file_init_owned); \
+ _fd_prepare; \
+ })
+
+/*
+ * FD_PREPARE(var, fd_flags, file_init_owned):
+ * Convenience wrapper for CLASS_INIT(fd_prepare, ...).
+ *
+ * @_var: name of struct fd_prepare variable to define
+ * @_fd_flags: flags for get_unused_fd_flags()
+ * @_file_init_owned: struct file to take ownership of (can be expression)
+ *
+ * Ex.
+ * FD_PREPARE(fdf, O_RDWR | O_CLOEXEC,
+ * anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep, O_RDWR));
+ * if (fd_prepare_failed(fdf))
+ * return fd_prepare_error(fdf);
+ *
+ * ep->file = fd_prepare_file(fdf);
+ * return fd_publish(fdf);
+ *
+ * Or with different file init function:
+ *
+ * FD_PREPARE(fdf, flags,
+ * anon_inode_getfile_fmode("[eventfd]", &eventfd_fops, ctx, flags, FMODE_NOWAIT));
+ * if (fd_prepare_failed(fdf))
+ * return fd_prepare_error(fdf);
+ *
+ * return fd_publish(fdf);
+ */
+#define FD_PREPARE(_var, _fd_flags, _file_init_owned) \
+ scoped_class_init(fd_prepare, _var, __FD_PREPARE_INIT(_fd_flags, _file_init_owned))
+
+#define fd_publish(_fd_prepare) \
+ ({ \
+ struct fd_prepare *__p = &(_fd_prepare); \
+ fd_install(__p->fd, __p->file); \
+ retain_and_null_ptr(__p->file); \
+ take_fd(__p->fd); \
+ })
+
#endif /* __LINUX_FILE_H */
--
2.47.3
next prev parent reply other threads:[~2025-11-20 22:32 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-20 22:31 [PATCH RFC v2 00/48] file: add and convert to FD_PREPARE() Christian Brauner
2025-11-20 22:31 ` Christian Brauner [this message]
2025-11-20 22:31 ` [PATCH RFC v2 02/48] anon_inodes: convert __anon_inode_getfd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 03/48] eventfd: convert do_eventfd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 04/48] fhandle: convert do_handle_open() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 05/48] namespace: convert open_tree() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 06/48] namespace: convert open_tree_attr() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 07/48] namespace: convert fsmount() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 08/48] fanotify: convert fanotify_init() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 09/48] nsfs: convert open_namespace() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 10/48] nsfs: convert ns_ioctl() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 11/48] autofs: convert autofs_dev_ioctl_open_mountpoint() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 12/48] eventpoll: convert do_epoll_create() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 13/48] open: convert do_sys_openat2() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 14/48] signalfd: convert do_signalfd4() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 15/48] timerfd: convert timerfd_create() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 16/48] userfaultfd: convert new_userfaultfd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 17/48] xfs: convert xfs_open_by_handle() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 18/48] drm: convert drm_mode_create_lease_ioctl() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 19/48] dma: convert dma_buf_fd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 20/48] af_unix: convert unix_file_open() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 21/48] dma: convert sync_file_ioctl_merge() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 22/48] exec: convert begin_new_exec() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 23/48] ipc: convert do_mq_open() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 24/48] bpf: convert bpf_iter_new_fd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 25/48] bpf: convert bpf_token_create() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 26/48] memfd: convert memfd_create() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 27/48] secretmem: convert memfd_secret() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 28/48] net/handshake: convert handshake_nl_accept_doit() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 29/48] net/kcm: convert kcm_ioctl() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 30/48] net/sctp: convert sctp_getsockopt_peeloff_common() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 31/48] net/socket: convert sock_map_fd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 32/48] net/socket: convert __sys_accept4_file() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 33/48] spufs: convert spufs_context_open() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 34/48] papr-hvpipe: convert papr_hvpipe_dev_create_handle() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 35/48] spufs: convert spufs_gang_open() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 36/48] pseries: convert papr_platform_dump_create_handle() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 37/48] pseries: port papr_rtas_setup_file_interface() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 38/48] dma: port sw_sync_ioctl_create_fence() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 39/48] gpio: convert linehandle_create() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 40/48] hv: convert mshv_ioctl_create_partition() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 41/48] media: convert media_request_alloc() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 42/48] ntsync: convert ntsync_obj_get_fd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 43/48] tty: convert ptm_open_peer() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 44/48] vfio: convert vfio_group_ioctl_get_device_fd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 45/48] file: convert replace_fd() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 46/48] io_uring: convert io_create_mock_file() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 47/48] kvm: convert kvm_arch_supports_gmem_init_shared() " Christian Brauner
2025-11-20 22:32 ` [PATCH RFC v2 48/48] kvm: convert kvm_vcpu_ioctl_get_stats_fd() " Christian Brauner
2025-11-20 23:08 ` [PATCH RFC v2 00/48] file: add and convert " Linus Torvalds
2025-11-20 23:28 ` Linus Torvalds
2025-11-21 17:55 ` Christian Brauner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251120-work-fd-prepare-v2-1-fef6ebda05d3@kernel.org \
--to=brauner@kernel.org \
--cc=amir73il@gmail.com \
--cc=axboe@kernel.dk \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).