From patchwork Thu Nov 21 11:22:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13881869 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 6BE34D6ED0B for ; Thu, 21 Nov 2024 11:22:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DBC26B008C; Thu, 21 Nov 2024 06:22:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D7F26B0098; Thu, 21 Nov 2024 06:22:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE8046B0095; Thu, 21 Nov 2024 06:22:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7CAC46B0083 for ; Thu, 21 Nov 2024 06:22:28 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 401851C76F7 for ; Thu, 21 Nov 2024 11:22:28 +0000 (UTC) X-FDA: 82809861960.25.B3F8733 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf12.hostedemail.com (Postfix) with ESMTP id 5E60040014 for ; Thu, 21 Nov 2024 11:22:02 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=ldkxUmSn; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=Ho0aKtua; dmarc=none; spf=pass (imf12.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732187960; a=rsa-sha256; cv=none; b=cjCwCF/XDzYwDjZEFsvkiWRUucXTEu50m797jUmZHv6ZFUA6uPIgCu71XFlpEV4jQvtO0/ cYOcxnsnic62w5ucFYII2XqWbWzQVHiWs3RyvuA1hdRxu7C+kgvAlYlRS9XoTxlVqMJlfP 02XANXG4mwQnoaM3h0G81JjEdYnhpic= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=ldkxUmSn; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=Ho0aKtua; dmarc=none; spf=pass (imf12.hostedemail.com: domain of jack@suse.cz designates 195.135.223.130 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732187960; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Q2qxXVL2Jb2lG51DduQ/x+qQIqCY8ru0Auoofcr9Fgo=; b=q2b9fNsbuwQqkyyPC4lCkrXdSieHEdP9Yo0/INz7rJYIaYcM3SgQHawzW+HTX7rcNkqNaa J6Y7pQLHhUq2eGbnRR3gPSv2NWi5MguIU9UKyp/umLJvL3ERTasjV+AQg05E1XkWniTF4o 8fyzswOeqqLGuXn/q4w3a859XSMKLxE= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 47E57219FB; Thu, 21 Nov 2024 11:22:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1732188144; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q2qxXVL2Jb2lG51DduQ/x+qQIqCY8ru0Auoofcr9Fgo=; b=ldkxUmSnk87XS1fQLVTzkv04jHEngOpg2yhKBoL45DbH67sk0wdFHY1XwzPXCm/uNA/rpx Mi/92oZP1SpHJMuD9uVMqgTLUUZ1YDatg3UW90zSlozoIro7ZKZQdLrOPkbQLSmezCyV/3 1ZJEsDMo1eQjWzMKYy5lHHJRgdzz36U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1732188144; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q2qxXVL2Jb2lG51DduQ/x+qQIqCY8ru0Auoofcr9Fgo=; b=Ho0aKtuajp0z3e7EHf/Ka4HJOoUO8ddZeluFft+W+S3VqR8yuJmTnrmZndAHTITAm/1Pod uhx0qPt35mTXcaDg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 2CD9313AC9; Thu, 21 Nov 2024 11:22:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 5WfpCvAXP2cdfwAAD6G6ig (envelope-from ); Thu, 21 Nov 2024 11:22:24 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id D5391A08E1; Thu, 21 Nov 2024 12:22:23 +0100 (CET) From: Jan Kara To: Cc: Amir Goldstein , Josef Bacik , brauner@kernel.org, Linus Torvalds , Al Viro , linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-mm@kvack.org, Jan Kara Subject: [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open Date: Thu, 21 Nov 2024 12:22:02 +0100 Message-Id: <20241121112218.8249-4-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20241121112218.8249-1-jack@suse.cz> References: <20241121112218.8249-1-jack@suse.cz> MIME-Version: 1.0 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Stat-Signature: 8jeq6o3ad1t3k8rez3tyhrgd6m69mfwc X-Rspamd-Queue-Id: 5E60040014 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1732188122-426090 X-HE-Meta: U2FsdGVkX18OiENX5EQdoAuzZti/wKDjU4fRoO7WCDIlroy5yaXvk9TJ2LqP94hheuulWi4qtwSAYjQ8RWNdEdKXXoHPMtpfO5srXbQuyF9TDJcAeMzVLi3fxMlkE3ZvQjwpl/bypavyCzK65L5ypMGdTmmx0gO9qlFVAZzbIRGHNll4MptVVn+npBPBw7NlY2JoVP2OgizqSTU8amyz3ShzC/plNFC1vKBzrZN2KywH0tsW1KmRbz0pPMmCmlQ9EZWmcB+4Ve52fQq5BDf+RHApq0FdcoauDqcoQ5lb0esiTN5BtbokKLmV2a2IDtC1Ibt8k5dGnEuOagJQPf4kqtsJXa/NC2WAb4r+wUV3Mfy/WjCRkVX9Ys98OQoGXu3lKGuZoN92xZ0gLXT70tbx54fUiXk2u0KFZ80sw32SHLuAaEn2Q2DkF/UiJ3q8omAc7ynEAyhR30YOMigiCNZTg+pUXKeJrZoQVsS2NY/7+id5sxrY+MJd+udJZEShf+JQdC2BxxNs3s5kdtjIfOxCvt+lWZROvadrcQxHyKLBMGULLZL6F68NrQT8o7b2Gew8tEQ9Y2Kb7gzoqqyY7lX+sVFK3tcyRDss/oaR1CY/493ccp9YKaNKARexq0Ig/TT0jNk6VWxls0nCAI7571Knkg2CkdZB9qRouXodU7xogHW+Ec0uoWCEJHM4jCJ4/igqQuD2UiLRHPMz4yFwoYazODVA0dsP5maO1VdxzmutXCwZC0VnKEJAEGIBzAfVvjlxiCqz5Uz7F4sBnqkVJfl2Dz/vv/LswuI2/uZnkKyQYZ69+atSdAEkErI7/H4LUaidD8RAlGD8SIoVYHxaKYvdyEvrm8boUtHa09PDx6NTn8xetZ4v/rTKQp4aiMy74d4G1vWY+u7w3fpxl7iw0EsDA85SPn7PyWGfABcLRd/OshQO3S6CzdcCxB7/qozowvrnfUVu/0y88yj39DASs+u NkgShQVA VJ+HOfNoNoRrn6uKRmlFNsYkusf72bn3J4BjG0ErYGh/rjBTELGKTEmY3zKaIVpFp0HAtjQbYf9AHKSYrUyzS1RpZ/w8R0bPPveNULk7BZpZOY3vivb/NlMlU9fBi6KVVW0lS50klkgcWbnvxm0l8xaHSjY4HcP0qrtv3T/BbLE2eoW0II6VSCJE5WCE1S6YD4qyL6nOegbaPHZDQbnOpjuY0Cq/7rHpDfTW0ladVZAPx5cGyB0DSPCp/Gprif9xFGFdDcPWEPJdLDdhm4qrU0ofeonsfFEg70wUTyOuHv2/0QuveETwSFIF4KDwnRetvJC1C3+G3mvfi8olc57PmSjHsocqA6hls8c6GCFX5JBZjmOZbt61sLmGqCib0uxsxU4KHPzbkw3MQj523ZaMulsHaKXJWxIMotl1zfczgLsFZXrq+QkPY2By3oQ== 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: Amir Goldstein So far, we set FMODE_NONOTIFY_ flags at open time if we know that there are no permission event watchers at all on the filesystem, but lack of FMODE_NONOTIFY_ flags does not mean that the file is actually watched. For pre-content events, it is possible to optimize things so that we don't bother trying to send pre-content events if file was not watched (through sb, mnt, parent or inode itself) on open. Set FMODE_NONOTIFY_ flags according to that. Signed-off-by: Amir Goldstein Signed-off-by: Jan Kara Link: https://patch.msgid.link/2ddcc9f8d1fde48d085318a6b5a889289d8871d8.1731684329.git.josef@toxicpanda.com --- fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++-- include/linux/fsnotify_backend.h | 3 +++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 569ec356e4ce..dd1dffd89fd6 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask, return mask & marks_mask; } -/* Are there any inode/mount/sb objects that are interested in this event? */ +/* Are there any inode/mount/sb objects that watch for these events? */ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, __u32 mask) { @@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify); */ void file_set_fsnotify_mode(struct file *file) { - struct super_block *sb = file->f_path.dentry->d_sb; + struct dentry *dentry = file->f_path.dentry, *parent; + struct super_block *sb = dentry->d_sb; + __u32 mnt_mask, p_mask; /* Is it a file opened by fanotify? */ if (FMODE_FSNOTIFY_NONE(file->f_mode)) @@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file) file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM; return; } + + /* + * OK, there are some pre-content watchers. Check if anybody can be + * watching for pre-content events on *this* file. + */ + mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask); + if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) && + !fsnotify_object_watched(d_inode(dentry), mnt_mask, + FSNOTIFY_PRE_CONTENT_EVENTS))) { + file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM; + return; + } + + /* Even parent is not watching for pre-content events on this file? */ + parent = dget_parent(dentry); + p_mask = fsnotify_inode_watches_children(d_inode(parent)); + dput(parent); + if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) { + file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM; + return; + } } #endif diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 3ecf7768e577..9c105244815d 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -77,6 +77,9 @@ */ #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME) +/* Pre-content events can be used to fill file content */ +#define FSNOTIFY_PRE_CONTENT_EVENTS 0 + #define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ FS_OPEN_EXEC_PERM)