linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC v2 00/48] file: add and convert to FD_PREPARE()
@ 2025-11-20 22:31 Christian Brauner
  2025-11-20 22:31 ` [PATCH RFC v2 01/48] file: add FD_PREPARE() Christian Brauner
                   ` (48 more replies)
  0 siblings, 49 replies; 52+ messages in thread
From: Christian Brauner @ 2025-11-20 22:31 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 scope-based FD_PREPARE() primitive that work as
follows:

FD_PREPARE(fdprep, open_flag, file_open_handle(&path, open_flag)) {
        if (fd_prepare_failed(fdprep))
                return fd_prepare_error(fdprep);

        return fd_publish(fdprep);
}

The scope based variant not just makes the lifetime very clear and
allows allows to easily jump over the guard without issues.

That's somewhat modeled after how we use wait_event() to allow for
arbitrary things to be used as a condition in it. Here we obviously
expect a struct file that we will own once FD_PREPARE was successful.

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.

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

There's a bunch of other 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.

Signed-off-by: Christian Brauner <brauner@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 (48):
      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()
      drm: convert drm_mode_create_lease_ioctl() 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          |  40 ++----
 arch/powerpc/platforms/pseries/papr-hvpipe.c       |  58 ++++-----
 .../powerpc/platforms/pseries/papr-platform-dump.c |  46 +++----
 arch/powerpc/platforms/pseries/papr-rtas-common.c  |  30 ++---
 drivers/dma-buf/dma-buf.c                          |  13 +-
 drivers/dma-buf/sw_sync.c                          |  45 +++----
 drivers/dma-buf/sync_file.c                        |  55 +++-----
 drivers/gpio/gpiolib-cdev.c                        |  48 +++----
 drivers/gpu/drm/drm_lease.c                        |  83 +++++-------
 drivers/hv/mshv_root_main.c                        |  24 ++--
 drivers/media/mc/mc-request.c                      |  35 ++---
 drivers/misc/ntsync.c                              |  19 +--
 drivers/tty/pty.c                                  |  35 ++---
 drivers/vfio/group.c                               |  27 +---
 fs/anon_inodes.c                                   |  24 +---
 fs/autofs/dev-ioctl.c                              |  31 ++---
 fs/eventfd.c                                       |  30 ++---
 fs/eventpoll.c                                     |  34 ++---
 fs/exec.c                                          |  14 +-
 fs/fhandle.c                                       |  31 ++---
 fs/file.c                                          |  24 ++--
 fs/namespace.c                                     | 123 +++++++-----------
 fs/notify/fanotify/fanotify_user.c                 |  18 +--
 fs/nsfs.c                                          |  34 ++---
 fs/open.c                                          |  20 +--
 fs/signalfd.c                                      |  26 ++--
 fs/timerfd.c                                       |  27 ++--
 fs/userfaultfd.c                                   |  32 ++---
 fs/xfs/xfs_handle.c                                |  33 ++---
 include/linux/cleanup.h                            |  32 +++++
 include/linux/file.h                               | 141 +++++++++++++++++++++
 io_uring/mock_file.c                               |  53 +++-----
 ipc/mqueue.c                                       |  40 +++---
 kernel/bpf/bpf_iter.c                              |  32 ++---
 kernel/bpf/token.c                                 |  80 +++++-------
 mm/memfd.c                                         |  30 ++---
 mm/secretmem.c                                     |  21 +--
 net/handshake/netlink.c                            |  27 ++--
 net/kcm/kcmsock.c                                  |  24 ++--
 net/sctp/socket.c                                  |  36 ++----
 net/socket.c                                       |  35 ++---
 net/unix/af_unix.c                                 |  17 +--
 virt/kvm/guest_memfd.c                             |  81 +++++-------
 virt/kvm/kvm_main.c                                |  23 ++--
 44 files changed, 737 insertions(+), 994 deletions(-)
---
base-commit: c8e00cdc7425d5c60fd1ce6e7f71e5fb1b236991
change-id: 20251118-work-fd-prepare-f415a3bf5fda


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

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

Thread overview: 52+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-20 22:31 [PATCH RFC v2 00/48] file: add and convert to FD_PREPARE() Christian Brauner
2025-11-20 22:31 ` [PATCH RFC v2 01/48] file: add FD_PREPARE() Christian Brauner
2025-11-20 22:31 ` [PATCH RFC v2 02/48] anon_inodes: convert __anon_inode_getfd() to FD_PREPARE() 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

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