From patchwork Fri Nov 15 15:30:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13876439 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9FBCFD68BC6 for ; Fri, 15 Nov 2024 15:31:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36C726B0083; Fri, 15 Nov 2024 10:31:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3182E6B0085; Fri, 15 Nov 2024 10:31:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16A7F6B0088; Fri, 15 Nov 2024 10:31:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F02D76B0083 for ; Fri, 15 Nov 2024 10:31:26 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 736DA141887 for ; Fri, 15 Nov 2024 15:31:26 +0000 (UTC) X-FDA: 82788717144.10.C83918C Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) by imf19.hostedemail.com (Postfix) with ESMTP id 85A531A0008 for ; Fri, 15 Nov 2024 15:30:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=xojlQ6pa; spf=none (imf19.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.170) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731684595; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kR1XeBSj0UKEAyle1L8EQtWLZrkZWI4sB29nFtL0fR8=; b=gKLFozoJfdYJnweh5rWMbnQUvVhPxjTP4WsLsIkcg0HVAviJlaEsuJBalthlmatkt2wEHK Z82cLDSSjasExgwsPC8SwhHXFBzK8I8+1ov192h53XfFR2YvqU7hHjMXeSHYKo1cFZUlJb n/re6jqO6hAh1cBLrwqv2BYldG3Dg2o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731684595; a=rsa-sha256; cv=none; b=KEXC4Nz/nb1sozO16/K0N74kJSYbn7WwIwt+y+5I0XD5K4Wi8y3h67nd77ELNBopfSOCuz EHP+AP+IVMv0HEuuY9hJqiLCz2MW2jznPFK9yXetvRTm8ho8U8INeqaIu/qMLXpIgaIsDs muIPhnebPgm2TqDWWx6MMMTldwWp0Wc= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=xojlQ6pa; spf=none (imf19.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.219.170) smtp.mailfrom=josef@toxicpanda.com; dmarc=none Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-e38277e9477so852810276.0 for ; Fri, 15 Nov 2024 07:31:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731684683; x=1732289483; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=kR1XeBSj0UKEAyle1L8EQtWLZrkZWI4sB29nFtL0fR8=; b=xojlQ6pamyGMqxfLeovxSM8JXH+B2IUUmN542bEFSEcbFS5Pb+Sx4+MH2Jc/KxOViJ M7Q/taFQZJHYApYXLePsE8Vm3aGCrJUmy5XWuqqHGDRKxOsdsXOkT17pFD+vXeUfDwAn PxAHJZ1nPt+eUq2O+gPjGg2V1AUspV94Eln+b8dzEzX3uHM4Gicx8QVchkF8zpdPzTC5 TQVs4goqL/i4i1XPvcXqSapk7NmAdQFbTBuzJ1UD7/Dsz9ofCs6raytlthFZi5faE06Q DIfjQT+dEIFhgE7bOQh4zIIaMOVKDuDmarQF3FjBBv29rJrm+VrvJ6sdRz6ffiSvmgL0 ZlGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731684683; x=1732289483; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kR1XeBSj0UKEAyle1L8EQtWLZrkZWI4sB29nFtL0fR8=; b=J4u+2iewxXNEgfHfeo30aFZmcXuscmUSbdE/Mv0DbBWZRWiqxrX04N3J2En0bMvfJb +6uG9sFWjcs0FVboJ7rf13T+ebaDU+7HEtnRM1X9f5bHCO23JyOxICpkh7bbizXWY9ia Mme0gMvSmg5URUIxtO2MSCDH8YZKAYPB6WiBBmicpFwoaxfTp2aJb3ifLgMAZ48Xg4Ez RJw/pCQIamei89/JdDvh1GGIZFzMvlWN9hhRakK7Ga0H2ahDkayjF9pGp6t3FrcVckEJ oKGy1TK6/48wbiXXae8h/APpcQFmmn3iheMktBLhU6hN9IfYo3TBr2+KvAu+7q8govVJ 5dew== X-Forwarded-Encrypted: i=1; AJvYcCUcAXuFT6dPidapSub7/7y98ij0m0BWEYlCwgnRo602B3444efFYlzb9UaR4nSkScx9dVvx9YlV+A==@kvack.org X-Gm-Message-State: AOJu0YzkzBcZPz6FVYKT82RJz1MdsMAjqy88PTKDcooTOYy60imYPE7X gFkEbhzXPVDqaqoJCXssBW2ps8Wx8tAO7RRcBNHGBjlVzDqWILolidyITiJiu/c= X-Google-Smtp-Source: AGHT+IGBdJCygNHz6DEBCdeiVRgK2IumpYj4jyGOaPbgCKPpYL6CnPz3nA2B5qY78/GCUuIzkqAI7g== X-Received: by 2002:a05:6902:2b86:b0:e30:e1f3:2aab with SMTP id 3f1490d57ef6-e38263a1b87mr2546246276.39.1731684683163; Fri, 15 Nov 2024 07:31:23 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e38152fe65dsm991511276.35.2024.11.15.07.31.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:31:22 -0800 (PST) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-ext4@vger.kernel.org Subject: [PATCH v8 01/19] fs: get rid of __FMODE_NONOTIFY kludge Date: Fri, 15 Nov 2024 10:30:14 -0500 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Stat-Signature: axey1wtit575kp4rsdt1eeiyd3pttxq4 X-Rspamd-Queue-Id: 85A531A0008 X-Rspam-User: X-HE-Tag: 1731684627-925289 X-HE-Meta: U2FsdGVkX19znfvPWk7pIL+C4YhZ0oOhZ/6f6nb1i+TZVsLi3rgjQchNK35qDfBtG2YSQkYrt8+E3cJNnVIJJte2Jyxc14+fIpKPBM1GklWkmBtQ9IidRHW+qhmgk3WRgPDpu/7GaZG/e1ivDk44rpk0lnrI+PBkRmBjHXxcL5FWZ9gINZyG6WXP7nHQ9kGzIOB3mqcAe3O1sD2/O9csMpNtSFichowVlGsDyorPKwXbNyFcIq1bWJDy24XiydbcpRs36sO125l3Y20gkAHDBtb4lSjfhB+SM+gAeXDTFnBY9D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iWfj0LReiv0f2EoKVX73P9TIgEPYa4RlMkEgNe/SnZhf6WRJuyN+yN21ecOnTmJqWNAtwkXv8illk8aPiRLygUQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Al Viro All it takes to get rid of the __FMODE_NONOTIFY kludge is switching fanotify from anon_inode_getfd() to anon_inode_getfile_fmode() and adding a dentry_open_fmode() helper to be used by fanotify on the other path. That's it - no more weird shit in OPEN_FMODE(), etc. Suggested-by: Al Viro Link: https://lore.kernel.org/linux-fsdevel/20241113043003.GH3387508@ZenIV/ Signed-off-by: Amir Goldstein --- fs/fcntl.c | 4 ++-- fs/notify/fanotify/fanotify_user.c | 25 ++++++++++++++++--------- fs/open.c | 23 +++++++++++++++++++---- include/linux/fs.h | 6 +++--- include/uapi/asm-generic/fcntl.h | 1 - 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index ac77dd912412..88db23aa864a 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -1155,10 +1155,10 @@ static int __init fcntl_init(void) * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ - BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ != + BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32( (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) | - __FMODE_EXEC | __FMODE_NONOTIFY)); + __FMODE_EXEC)); fasync_cache = kmem_cache_create("fasync_cache", sizeof(struct fasync_struct), 0, diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 2d85c71717d6..919ff59cb802 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -100,8 +100,7 @@ static void __init fanotify_sysctls_init(void) * * Internal and external open flags are stored together in field f_flags of * struct file. Only external open flags shall be allowed in event_f_flags. - * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be - * excluded. + * Internal flags like FMODE_EXEC shall be excluded. */ #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ O_ACCMODE | O_APPEND | O_NONBLOCK | \ @@ -258,12 +257,11 @@ static int create_fd(struct fsnotify_group *group, const struct path *path, return client_fd; /* - * we need a new file handle for the userspace program so it can read even if it was - * originally opened O_WRONLY. + * We provide an fd for the userspace program, so it could access the + * file without generating fanotify events itself. */ - new_file = dentry_open(path, - group->fanotify_data.f_flags | __FMODE_NONOTIFY, - current_cred()); + new_file = dentry_open_nonotify(path, group->fanotify_data.f_flags, + current_cred()); if (IS_ERR(new_file)) { put_unused_fd(client_fd); client_fd = PTR_ERR(new_file); @@ -1409,6 +1407,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) 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); @@ -1477,7 +1476,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID))) return -EINVAL; - f_flags = O_RDWR | __FMODE_NONOTIFY; + f_flags = O_RDWR; if (flags & FAN_CLOEXEC) f_flags |= O_CLOEXEC; if (flags & FAN_NONBLOCK) @@ -1555,10 +1554,18 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) goto out_destroy_group; } - fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); + 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)) { + fd = PTR_ERR(file); + put_unused_fd(fd); + goto out_destroy_group; + } + fd_install(fd, file); return fd; out_destroy_group: diff --git a/fs/open.c b/fs/open.c index e6911101fe71..c3490286092e 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1105,6 +1105,23 @@ struct file *dentry_open(const struct path *path, int flags, } EXPORT_SYMBOL(dentry_open); +struct file *dentry_open_nonotify(const struct path *path, int flags, + const struct cred *cred) +{ + struct file *f = alloc_empty_file(flags, cred); + if (!IS_ERR(f)) { + int error; + + f->f_mode |= FMODE_NONOTIFY; + error = vfs_open(path, f); + if (error) { + fput(f); + f = ERR_PTR(error); + } + } + return f; +} + /** * dentry_create - Create and open a file * @path: path to create @@ -1202,7 +1219,7 @@ inline struct open_how build_open_how(int flags, umode_t mode) inline int build_open_flags(const struct open_how *how, struct open_flags *op) { u64 flags = how->flags; - u64 strip = __FMODE_NONOTIFY | O_CLOEXEC; + u64 strip = O_CLOEXEC; int lookup_flags = 0; int acc_mode = ACC_MODE(flags); @@ -1210,9 +1227,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) "struct open_flags doesn't yet handle flags > 32 bits"); /* - * Strip flags that either shouldn't be set by userspace like - * FMODE_NONOTIFY or that aren't relevant in determining struct - * open_flags like O_CLOEXEC. + * Strip flags that aren't relevant in determining struct open_flags. */ flags &= ~strip; diff --git a/include/linux/fs.h b/include/linux/fs.h index 9c13222362f5..23bd058576b1 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2750,6 +2750,8 @@ static inline struct file *file_open_root_mnt(struct vfsmount *mnt, } struct file *dentry_open(const struct path *path, int flags, const struct cred *creds); +struct file *dentry_open_nonotify(const struct path *path, int flags, + const struct cred *cred); struct file *dentry_create(const struct path *path, int flags, umode_t mode, const struct cred *cred); struct path *backing_file_user_path(struct file *f); @@ -3706,11 +3708,9 @@ struct ctl_table; int __init list_bdev_fs_names(char *buf, size_t size); #define __FMODE_EXEC ((__force int) FMODE_EXEC) -#define __FMODE_NONOTIFY ((__force int) FMODE_NONOTIFY) #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE]) -#define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \ - (flag & __FMODE_NONOTIFY))) +#define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE))) static inline bool is_sxid(umode_t mode) { diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h index 80f37a0d40d7..613475285643 100644 --- a/include/uapi/asm-generic/fcntl.h +++ b/include/uapi/asm-generic/fcntl.h @@ -6,7 +6,6 @@ /* * FMODE_EXEC is 0x20 - * FMODE_NONOTIFY is 0x4000000 * These cannot be used by userspace O_* until internal and external open * flags are split. * -Eric Paris