linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
	 linux-fsdevel@vger.kernel.org, Jeff Layton <jlayton@kernel.org>,
	 Amir Goldstein <amir73il@gmail.com>,
	Jens Axboe <axboe@kernel.dk>,
	 Christian Brauner <brauner@kernel.org>
Subject: [PATCH v4 08/47] fanotify: convert fanotify_init() to FD_PREPARE()
Date: Sun, 23 Nov 2025 17:33:26 +0100	[thread overview]
Message-ID: <20251123-work-fd-prepare-v4-8-b6efa1706cfd@kernel.org> (raw)
In-Reply-To: <20251123-work-fd-prepare-v4-0-b6efa1706cfd@kernel.org>

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/notify/fanotify/fanotify_user.c | 60 ++++++++++++++------------------------
 1 file changed, 22 insertions(+), 38 deletions(-)

diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 1dadda82cae5..be0a96ad4316 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -1597,16 +1597,20 @@ static struct hlist_head *fanotify_alloc_merge_hash(void)
 	return hash;
 }
 
+DEFINE_CLASS(fsnotify_group,
+	      struct fsnotify_group *,
+	      if (_T) fsnotify_destroy_group(_T),
+	      fsnotify_alloc_group(ops, flags),
+	      const struct fsnotify_ops *ops, int flags)
+
 /* fanotify syscalls */
 SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 {
 	struct user_namespace *user_ns = current_user_ns();
-	struct fsnotify_group *group;
 	int f_flags, fd;
 	unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
 	unsigned int class = flags & FANOTIFY_CLASS_BITS;
 	unsigned int internal_flags = 0;
-	struct file *file;
 
 	pr_debug("%s: flags=%x event_f_flags=%x\n",
 		 __func__, flags, event_f_flags);
@@ -1690,36 +1694,29 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 	if (flags & FAN_NONBLOCK)
 		f_flags |= O_NONBLOCK;
 
-	/* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
-	group = fsnotify_alloc_group(&fanotify_fsnotify_ops,
+	CLASS(fsnotify_group, group)(&fanotify_fsnotify_ops,
 				     FSNOTIFY_GROUP_USER);
-	if (IS_ERR(group)) {
+	/* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
+	if (IS_ERR(group))
 		return PTR_ERR(group);
-	}
 
 	/* Enforce groups limits per user in all containing user ns */
 	group->fanotify_data.ucounts = inc_ucount(user_ns, current_euid(),
 						  UCOUNT_FANOTIFY_GROUPS);
-	if (!group->fanotify_data.ucounts) {
-		fd = -EMFILE;
-		goto out_destroy_group;
-	}
+	if (!group->fanotify_data.ucounts)
+		return -EMFILE;
 
 	group->fanotify_data.flags = flags | internal_flags;
 	group->memcg = get_mem_cgroup_from_mm(current->mm);
 	group->user_ns = get_user_ns(user_ns);
 
 	group->fanotify_data.merge_hash = fanotify_alloc_merge_hash();
-	if (!group->fanotify_data.merge_hash) {
-		fd = -ENOMEM;
-		goto out_destroy_group;
-	}
+	if (!group->fanotify_data.merge_hash)
+		return -ENOMEM;
 
 	group->overflow_event = fanotify_alloc_overflow_event();
-	if (unlikely(!group->overflow_event)) {
-		fd = -ENOMEM;
-		goto out_destroy_group;
-	}
+	if (unlikely(!group->overflow_event))
+		return -ENOMEM;
 
 	if (force_o_largefile())
 		event_f_flags |= O_LARGEFILE;
@@ -1738,8 +1735,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 		group->priority = FSNOTIFY_PRIO_PRE_CONTENT;
 		break;
 	default:
-		fd = -EINVAL;
-		goto out_destroy_group;
+		return -EINVAL;
 	}
 
 	BUILD_BUG_ON(!(FANOTIFY_ADMIN_INIT_FLAGS & FAN_UNLIMITED_QUEUE));
@@ -1750,27 +1746,15 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 	}
 
 	if (flags & FAN_ENABLE_AUDIT) {
-		fd = -EPERM;
 		if (!capable(CAP_AUDIT_WRITE))
-			goto out_destroy_group;
-	}
-
-	fd = get_unused_fd_flags(f_flags);
-	if (fd < 0)
-		goto out_destroy_group;
-
-	file = anon_inode_getfile_fmode("[fanotify]", &fanotify_fops, group,
-					f_flags, FMODE_NONOTIFY);
-	if (IS_ERR(file)) {
-		put_unused_fd(fd);
-		fd = PTR_ERR(file);
-		goto out_destroy_group;
+			return -EPERM;
 	}
-	fd_install(fd, file);
-	return fd;
 
-out_destroy_group:
-	fsnotify_destroy_group(group);
+	fd = FD_ADD(f_flags,
+		    anon_inode_getfile_fmode("[fanotify]", &fanotify_fops,
+					     group, f_flags, FMODE_NONOTIFY));
+	if (fd >= 0)
+		retain_and_null_ptr(group);
 	return fd;
 }
 

-- 
2.47.3


  parent reply	other threads:[~2025-11-23 16:33 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-23 16:33 [PATCH v4 00/47] file: FD_{ADD,PREPARE}() Christian Brauner
2025-11-23 16:33 ` [PATCH v4 01/47] file: add FD_{ADD,PREPARE}() Christian Brauner
2025-11-24 15:03   ` Jason Gunthorpe
2025-11-23 16:33 ` [PATCH v4 02/47] anon_inodes: convert to FD_PREPARE() Christian Brauner
2025-11-25 12:16   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 03/47] eventfd: convert do_eventfd() " Christian Brauner
2025-11-25 12:18   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 04/47] fhandle: convert do_handle_open() " Christian Brauner
2025-11-25 12:15   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 05/47] namespace: convert open_tree() " Christian Brauner
2025-11-25 12:32   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 06/47] namespace: convert open_tree_attr() " Christian Brauner
2025-11-25 12:33   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 07/47] namespace: convert fsmount() " Christian Brauner
2025-11-25 12:42   ` Jan Kara
2025-11-23 16:33 ` Christian Brauner [this message]
2025-11-24 17:13   ` [PATCH v4 08/47] fanotify: convert fanotify_init() " Amir Goldstein
2025-11-25 12:13   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 09/47] nsfs: convert open_namespace() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 10/47] nsfs: convert ns_ioctl() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 11/47] autofs: convert autofs_dev_ioctl_open_mountpoint() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 12/47] eventpoll: convert do_epoll_create() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 13/47] open: convert do_sys_openat2() " Christian Brauner
2025-11-25 12:20   ` Jan Kara
2025-11-23 16:33 ` [PATCH v4 14/47] signalfd: convert do_signalfd4() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 15/47] timerfd: convert timerfd_create() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 16/47] userfaultfd: convert new_userfaultfd() " Christian Brauner
2025-11-25 11:24   ` Mike Rapoport
2025-11-23 16:33 ` [PATCH v4 17/47] xfs: convert xfs_open_by_handle() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 18/47] dma: convert dma_buf_fd() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 19/47] af_unix: convert unix_file_open() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 20/47] dma: convert sync_file_ioctl_merge() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 21/47] exec: convert begin_new_exec() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 22/47] ipc: convert do_mq_open() " Christian Brauner
2025-11-25 22:30   ` Mark Brown
2025-11-26  0:05     ` Linus Torvalds
2025-11-26 13:20       ` Christian Brauner
2025-11-26 16:10         ` Linus Torvalds
2025-11-23 16:33 ` [PATCH v4 23/47] bpf: convert bpf_iter_new_fd() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 24/47] bpf: convert bpf_token_create() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 25/47] memfd: convert memfd_create() " Christian Brauner
2025-11-25 11:30   ` Mike Rapoport
2025-11-23 16:33 ` [PATCH v4 26/47] secretmem: convert memfd_secret() " Christian Brauner
2025-11-25 11:21   ` Mike Rapoport
2025-11-23 16:33 ` [PATCH v4 27/47] net/handshake: convert handshake_nl_accept_doit() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 28/47] net/kcm: convert kcm_ioctl() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 29/47] net/sctp: convert sctp_getsockopt_peeloff_common() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 30/47] net/socket: convert sock_map_fd() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 31/47] net/socket: convert __sys_accept4_file() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 32/47] spufs: convert spufs_context_open() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 33/47] papr-hvpipe: convert papr_hvpipe_dev_create_handle() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 34/47] spufs: convert spufs_gang_open() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 35/47] pseries: convert papr_platform_dump_create_handle() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 36/47] pseries: port papr_rtas_setup_file_interface() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 37/47] dma: port sw_sync_ioctl_create_fence() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 38/47] gpio: convert linehandle_create() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 39/47] hv: convert mshv_ioctl_create_partition() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 40/47] media: convert media_request_alloc() " Christian Brauner
2025-11-23 16:33 ` [PATCH v4 41/47] ntsync: convert ntsync_obj_get_fd() " Christian Brauner
2025-11-23 16:34 ` [PATCH v4 42/47] tty: convert ptm_open_peer() " Christian Brauner
2025-11-25 22:39   ` Mark Brown
2025-11-26 13:35     ` Christian Brauner
2025-11-26 14:22       ` Mark Brown
2025-11-23 16:34 ` [PATCH v4 43/47] vfio: convert vfio_group_ioctl_get_device_fd() " Christian Brauner
2025-11-24 14:52   ` Jason Gunthorpe
2025-11-23 16:34 ` [PATCH v4 44/47] file: convert replace_fd() " Christian Brauner
2025-11-25 12:29   ` Jan Kara
2025-11-23 16:34 ` [PATCH v4 45/47] io_uring: convert io_create_mock_file() " Christian Brauner
2025-11-25 16:38   ` Jens Axboe
2025-11-23 16:34 ` [PATCH v4 46/47] kvm: convert kvm_arch_supports_gmem_init_shared() " Christian Brauner
2025-11-23 16:34 ` [PATCH v4 47/47] kvm: convert kvm_vcpu_ioctl_get_stats_fd() " Christian Brauner
2025-11-23 16:43 ` [PATCH v4 00/47] file: FD_{ADD,PREPARE}() Linus Torvalds
2025-11-23 16:46   ` Linus Torvalds
2025-11-23 22:53   ` 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=20251123-work-fd-prepare-v4-8-b6efa1706cfd@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).