linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC v3 00/47] file: add and convert to FD_PREPARE()
@ 2025-11-21 18:00 Christian Brauner
  2025-11-21 18:00 ` [PATCH RFC v3 01/47] file: add FD_PREPARE() Christian Brauner
                   ` (46 more replies)
  0 siblings, 47 replies; 49+ messages in thread
From: Christian Brauner @ 2025-11-21 18:00 UTC (permalink / raw)
  To: Linus Torvalds, Jeff Layton, Amir Goldstein, Jens Axboe
  Cc: Alexander Viro, Jan Kara, linux-fsdevel, Christian Brauner

Hey,

I've been playing with this to allow for moderately flexible usage of
the get_unused_fd_flags() + create file + fd_install() pattern that's
used quite extensively.

How callers allocate files is really heterogenous so it's not really
convenient to fold them into a single class. It's possibe to split them
into subclasses like for anon inodes. I think that's not necessarily
nice as well.

My take is to add a FD_PREPARE() primitive that integrates tightly with
the new ACQUIRE_ERR() scheme that was recently added by peterz:

FD_PREPARE(fdf, open_flag, file_open_handle(&path, open_flag));
ret = ACQUIRE_ERR(fd_prepare, &fdf);
if (ret)
	return ret;

return fd_publish(fdf);

I've converted all of the easy cases over to it and it gets rid of a lot
of convoluted cleanup logic.

It's centered around struct fd_prepare. FD_PREPARE() encapsulates all of
allocation and cleanup logic and must be followed by a call to
fd_publish() which associates the fd with the file and installs it into
the callers fdtable. If fd_publish() isn't called both are deallocated.

It mandates a specific order namely that first we allocate the fd and
then instantiate the file. But that shouldn't be a problem nearly
everyone I've converted uses this exact pattern anyway.

There's a bunch of additional cases where it would be easy to convert
them to this pattern. For example, the whole sync file stuff in dma
currently retains the containing structure of the file instead of the
file itself even though it's only used to allocate files. Changing that
would make it fall into the FD_PREPARE() pattern easily. I've not done
that work yet.

There's room for extending this in a way that wed'd have subclasses for
some particularly often use patterns but as I said I'm not even sure
that's worth it.

Anyway, I'm not a macro wizard per se so maybe I missed some very
obvious bugs. Expect there to still be rough edges.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
Changes in v3:
- Remove scope-based variant.
- Link to v2: https://patch.msgid.link/20251120-work-fd-prepare-v2-0-fef6ebda05d3@kernel.org

Changes in v2:
- Make FD_PREPARE() use a separate scope.
- Convert most easy cases.
- Link to v1: https://patch.msgid.link/20251118-work-fd-prepare-v1-0-c20504d97375@kernel.org

---
Christian Brauner (47):
      file: add FD_PREPARE()
      anon_inodes: convert __anon_inode_getfd() to FD_PREPARE()
      eventfd: convert do_eventfd() to FD_PREPARE()
      fhandle: convert do_handle_open() to FD_PREPARE()
      namespace: convert open_tree() to FD_PREPARE()
      namespace: convert open_tree_attr() to FD_PREPARE()
      namespace: convert fsmount() to FD_PREPARE()
      fanotify: convert fanotify_init() to FD_PREPARE()
      nsfs: convert open_namespace() to FD_PREPARE()
      nsfs: convert ns_ioctl() to FD_PREPARE()
      autofs: convert autofs_dev_ioctl_open_mountpoint() to FD_PREPARE()
      eventpoll: convert do_epoll_create() to FD_PREPARE()
      open: convert do_sys_openat2() to FD_PREPARE()
      signalfd: convert do_signalfd4() to FD_PREPARE()
      timerfd: convert timerfd_create() to FD_PREPARE()
      userfaultfd: convert new_userfaultfd() to FD_PREPARE()
      xfs: convert xfs_open_by_handle() to FD_PREPARE()
      dma: convert dma_buf_fd() to FD_PREPARE()
      af_unix: convert unix_file_open() to FD_PREPARE()
      dma: convert sync_file_ioctl_merge() to FD_PREPARE()
      exec: convert begin_new_exec() to FD_PREPARE()
      ipc: convert do_mq_open() to FD_PREPARE()
      bpf: convert bpf_iter_new_fd() to FD_PREPARE()
      bpf: convert bpf_token_create() to FD_PREPARE()
      memfd: convert memfd_create() to FD_PREPARE()
      secretmem: convert memfd_secret() to FD_PREPARE()
      net/handshake: convert handshake_nl_accept_doit() to FD_PREPARE()
      net/kcm: convert kcm_ioctl() to FD_PREPARE()
      net/sctp: convert sctp_getsockopt_peeloff_common() to FD_PREPARE()
      net/socket: convert sock_map_fd() to FD_PREPARE()
      net/socket: convert __sys_accept4_file() to FD_PREPARE()
      spufs: convert spufs_context_open() to FD_PREPARE()
      papr-hvpipe: convert papr_hvpipe_dev_create_handle() to FD_PREPARE()
      spufs: convert spufs_gang_open() to FD_PREPARE()
      pseries: convert papr_platform_dump_create_handle() to FD_PREPARE()
      pseries: port papr_rtas_setup_file_interface() to FD_PREPARE()
      dma: port sw_sync_ioctl_create_fence() to FD_PREPARE()
      gpio: convert linehandle_create() to FD_PREPARE()
      hv: convert mshv_ioctl_create_partition() to FD_PREPARE()
      media: convert media_request_alloc() to FD_PREPARE()
      ntsync: convert ntsync_obj_get_fd() to FD_PREPARE()
      tty: convert ptm_open_peer() to FD_PREPARE()
      vfio: convert vfio_group_ioctl_get_device_fd() to FD_PREPARE()
      file: convert replace_fd() to FD_PREPARE()
      io_uring: convert io_create_mock_file() to FD_PREPARE()
      kvm: convert kvm_arch_supports_gmem_init_shared() to FD_PREPARE()
      kvm: convert kvm_vcpu_ioctl_get_stats_fd() to FD_PREPARE()

 arch/powerpc/platforms/cell/spufs/inode.c          | 38 +++------
 arch/powerpc/platforms/pseries/papr-hvpipe.c       | 37 +++------
 .../powerpc/platforms/pseries/papr-platform-dump.c | 39 +++------
 arch/powerpc/platforms/pseries/papr-rtas-common.c  | 32 +++-----
 drivers/dma-buf/dma-buf.c                          | 14 ++--
 drivers/dma-buf/sw_sync.c                          | 42 ++++------
 drivers/dma-buf/sync_file.c                        | 54 +++++--------
 drivers/gpio/gpiolib-cdev.c                        | 61 +++++++-------
 drivers/hv/mshv_root_main.c                        | 30 ++-----
 drivers/media/mc/mc-request.c                      | 33 +++-----
 drivers/misc/ntsync.c                              | 22 ++----
 drivers/tty/pty.c                                  | 34 +++-----
 drivers/vfio/group.c                               | 27 ++-----
 fs/anon_inodes.c                                   | 25 ++----
 fs/autofs/dev-ioctl.c                              | 34 +++-----
 fs/eventfd.c                                       | 33 +++-----
 fs/eventpoll.c                                     | 33 +++-----
 fs/exec.c                                          |  8 +-
 fs/fhandle.c                                       | 31 ++++----
 fs/file.c                                          | 20 +++--
 fs/namespace.c                                     | 92 ++++++++--------------
 fs/notify/fanotify/fanotify_user.c                 | 63 ++++++---------
 fs/nsfs.c                                          | 51 +++++-------
 fs/open.c                                          | 21 ++---
 fs/signalfd.c                                      | 29 +++----
 fs/timerfd.c                                       | 30 +++----
 fs/userfaultfd.c                                   | 32 +++-----
 fs/xfs/xfs_handle.c                                | 53 ++++---------
 include/linux/cleanup.h                            |  7 ++
 include/linux/file.h                               | 75 ++++++++++++++++++
 io_uring/mock_file.c                               | 46 ++++-------
 ipc/mqueue.c                                       | 34 +++-----
 kernel/bpf/bpf_iter.c                              | 30 +++----
 kernel/bpf/token.c                                 | 48 ++++-------
 mm/memfd.c                                         | 32 +++-----
 mm/secretmem.c                                     | 23 ++----
 net/handshake/netlink.c                            | 32 ++++----
 net/kcm/kcmsock.c                                  | 24 ++----
 net/sctp/socket.c                                  | 89 ++++++---------------
 net/socket.c                                       | 38 +++------
 net/unix/af_unix.c                                 | 20 ++---
 virt/kvm/guest_memfd.c                             | 37 +++------
 virt/kvm/kvm_main.c                                | 24 +++---
 43 files changed, 597 insertions(+), 980 deletions(-)
---
base-commit: c8e00cdc7425d5c60fd1ce6e7f71e5fb1b236991
change-id: 20251118-work-fd-prepare-f415a3bf5fda


^ permalink raw reply	[flat|nested] 49+ messages in thread

end of thread, other threads:[~2025-11-21 18:42 UTC | newest]

Thread overview: 49+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-21 18:00 [PATCH RFC v3 00/47] file: add and convert to FD_PREPARE() Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 01/47] file: add FD_PREPARE() Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 02/47] anon_inodes: convert __anon_inode_getfd() to FD_PREPARE() Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 03/47] eventfd: convert do_eventfd() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 04/47] fhandle: convert do_handle_open() " Christian Brauner
2025-11-21 18:42   ` Linus Torvalds
2025-11-21 18:00 ` [PATCH RFC v3 05/47] namespace: convert open_tree() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 06/47] namespace: convert open_tree_attr() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 07/47] namespace: convert fsmount() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 08/47] fanotify: convert fanotify_init() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 09/47] nsfs: convert open_namespace() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 10/47] nsfs: convert ns_ioctl() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 11/47] autofs: convert autofs_dev_ioctl_open_mountpoint() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 12/47] eventpoll: convert do_epoll_create() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 13/47] open: convert do_sys_openat2() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 14/47] signalfd: convert do_signalfd4() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 15/47] timerfd: convert timerfd_create() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 16/47] userfaultfd: convert new_userfaultfd() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 17/47] xfs: convert xfs_open_by_handle() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 18/47] dma: convert dma_buf_fd() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 19/47] af_unix: convert unix_file_open() " Christian Brauner
2025-11-21 18:00 ` [PATCH RFC v3 20/47] dma: convert sync_file_ioctl_merge() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 21/47] exec: convert begin_new_exec() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 22/47] ipc: convert do_mq_open() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 23/47] bpf: convert bpf_iter_new_fd() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 24/47] bpf: convert bpf_token_create() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 25/47] memfd: convert memfd_create() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 26/47] secretmem: convert memfd_secret() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 27/47] net/handshake: convert handshake_nl_accept_doit() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 28/47] net/kcm: convert kcm_ioctl() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 29/47] net/sctp: convert sctp_getsockopt_peeloff_common() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 30/47] net/socket: convert sock_map_fd() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 31/47] net/socket: convert __sys_accept4_file() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 32/47] spufs: convert spufs_context_open() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 33/47] papr-hvpipe: convert papr_hvpipe_dev_create_handle() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 34/47] spufs: convert spufs_gang_open() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 35/47] pseries: convert papr_platform_dump_create_handle() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 36/47] pseries: port papr_rtas_setup_file_interface() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 37/47] dma: port sw_sync_ioctl_create_fence() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 38/47] gpio: convert linehandle_create() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 39/47] hv: convert mshv_ioctl_create_partition() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 40/47] media: convert media_request_alloc() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 41/47] ntsync: convert ntsync_obj_get_fd() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 42/47] tty: convert ptm_open_peer() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 43/47] vfio: convert vfio_group_ioctl_get_device_fd() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 44/47] file: convert replace_fd() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 45/47] io_uring: convert io_create_mock_file() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 46/47] kvm: convert kvm_arch_supports_gmem_init_shared() " Christian Brauner
2025-11-21 18:01 ` [PATCH RFC v3 47/47] kvm: convert kvm_vcpu_ioctl_get_stats_fd() " Christian Brauner

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).