From mboxrd@z Thu Jan 1 00:00:00 1970 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 E0D95D75BD8 for ; Thu, 21 Nov 2024 08:54:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73B536B007B; Thu, 21 Nov 2024 03:54:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EC1E6B0082; Thu, 21 Nov 2024 03:54:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 58BC96B0083; Thu, 21 Nov 2024 03:54:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 3A6916B007B for ; Thu, 21 Nov 2024 03:54:30 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AF83F160D24 for ; Thu, 21 Nov 2024 08:54:29 +0000 (UTC) X-FDA: 82809490134.11.C4038D1 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf26.hostedemail.com (Postfix) with ESMTP id 7109614000E for ; Thu, 21 Nov 2024 08:53:47 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uJ9rSuTw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=tKV06pKx; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uJ9rSuTw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=tKV06pKx; dmarc=none; spf=pass (imf26.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=1732179131; a=rsa-sha256; cv=none; b=pJgs+6vVwoG+KgZbRn6XBpcM7S3jvntHfjJ3tl3sGKgY7A31ROjlhX9N4Bj2E89PKh1Q1T dGVpkBX2eZeATiItHNNfcnEUWnZ/SvDFihrxDYIq9i5j4SCAdDwBAEsqzf+nnNxcx/qgO2 BySua5YZbJictIMEpX6YqSgrVTRZ980= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uJ9rSuTw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=tKV06pKx; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=uJ9rSuTw; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=tKV06pKx; dmarc=none; spf=pass (imf26.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=1732179131; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=M9QDYFVgK18ikztR1aFCMrX/3aAhs0bWXjPhxnlRZtE=; b=3GSLWc+HsR3NhHLVgxK7qrZzzi27y8lzMYwJmwUtFa/KdT1+AmBWquJ0YNrKi7abK8a9eb 2WdfN+Xvvqxo30ZsTkzEJe/xIw//Kur2guaoW9CHQA+ARe9qvL82zZJe/tdQR4C5eryLjM FTFazEJKDrsXzz9NNvth5820yl/ZE/k= 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 78A88211BB; Thu, 21 Nov 2024 08:54:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1732179265; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M9QDYFVgK18ikztR1aFCMrX/3aAhs0bWXjPhxnlRZtE=; b=uJ9rSuTwHq42Q9eV0Pm3KewVmcTElVkYABQedvIts4bOr9jE3bLJrPbvz8D5Rd72op769r D7hlLV2vCSExFT7xyg96dZnT7ugJoY+s2sae2a3czx8qSallOh1s+L6JdqkLWv5J/7yngC pJ4kj2KZPPBGrpJrFfgkxPnSCLc0t24= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1732179265; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M9QDYFVgK18ikztR1aFCMrX/3aAhs0bWXjPhxnlRZtE=; b=tKV06pKxDviRzPYcFgiT/BAb1ygh8vMnho6u2rZfPiQVKd8sSUv3Vw29/qRKVTtrUHuJJq kc+z72YKxb8ircAA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1732179265; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M9QDYFVgK18ikztR1aFCMrX/3aAhs0bWXjPhxnlRZtE=; b=uJ9rSuTwHq42Q9eV0Pm3KewVmcTElVkYABQedvIts4bOr9jE3bLJrPbvz8D5Rd72op769r D7hlLV2vCSExFT7xyg96dZnT7ugJoY+s2sae2a3czx8qSallOh1s+L6JdqkLWv5J/7yngC pJ4kj2KZPPBGrpJrFfgkxPnSCLc0t24= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1732179265; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M9QDYFVgK18ikztR1aFCMrX/3aAhs0bWXjPhxnlRZtE=; b=tKV06pKxDviRzPYcFgiT/BAb1ygh8vMnho6u2rZfPiQVKd8sSUv3Vw29/qRKVTtrUHuJJq kc+z72YKxb8ircAA== 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 59C57137CF; Thu, 21 Nov 2024 08:54:25 +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 VuebFUH1PmdBTgAAD6G6ig (envelope-from ); Thu, 21 Nov 2024 08:54:25 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id CEDF3A089E; Thu, 21 Nov 2024 09:54:20 +0100 (CET) Date: Thu, 21 Nov 2024 09:54:20 +0100 From: Jan Kara To: Amir Goldstein Cc: Jan Kara , Josef Bacik , kernel-team@fb.com, linux-fsdevel@vger.kernel.org, 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: Re: [PATCH v8 03/19] fsnotify: add helper to check if file is actually being watched Message-ID: <20241121085420.lpsvkixshtuju23i@quack3> References: <2ddcc9f8d1fde48d085318a6b5a889289d8871d8.1731684329.git.josef@toxicpanda.com> <20241120160247.sdvonyxkpmf4wnt2@quack3> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Action: no action X-Rspamd-Queue-Id: 7109614000E X-Stat-Signature: 4eizjak7977oy1nipuj7qohuuwyf3nsp X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1732179227-101909 X-HE-Meta: U2FsdGVkX18VgaRsVIpwrXpwTw5XFEBqPN/b7PBuV1GDnmNc8yPeGE0wIY+cl2isgHaqdkN0dKTz87Pia4esOWetdEVXFjb8N4eMn6OI/vNqW6qfYqEpDDIE6Xs5JejbcL0qO2pCYGtlq/AYpk+hO1c72OCyCoruhhuJDC1OCNGmkA4tQjfFxhzGasYhS/ZQL9KwtmaVprqxyLbdfzHINqyJmLPa8wwcUmyKf3QtyTmjMYdh0E4FVIXUKEcTH3zzotv1Im+tDhimPjbJ8GlhoXqF3gzppeifz/RnDtUEh7CY7X/Cq3UOzs5EmFvwqmuCq9uvKJFfxGHWyjk6+GKGfYVenZM1E3um631dT9RrsXrnGq2XNqBUwD75AUg+IPMfFU4UHN5wcJaNGgqR5wcEjfamyHmxUN4Z6IMEdzagp0LQuM8cC2De97gUqZIYblC/VBiSao8WQuVzPA2aPi6lJp2DBExYh+z6khQhG18uoPFQ5jzK82q2oHxEjF/EUhu1lvVrO9VQRLHYTcMAhH3H1lO5O7xGFTZOQd5ksK1Wz4FwZgd2vNt5/GfUK6VnFxaJaS3keA70Hnc6VCuXvNPbbwDjOdj5LKBIYE4Ex+mdZLEA5GsB4pW0mj5cixFPfIjwF6pOjNLCp2so0oFcVJprVG6IQJC8D0J+TE/W/CGa6cNzpibYaCGtpvYtiVQFSYp6xEq6rGeg4pxZG5DXZYJZIRN/pVLzDIS4iqJEoAVBZ0IntYQzfPR2zuGK9CmV9AnWear+RthGmRT8eXlPKXDc6difNMoG8Dsmm3bsq4C09/Bc9ijfy0xKpiyhSaOlAKg9nEeE4Ad44EQY26FrLcCCkrRdaM4YYXyr604uZo1ti8eVF5n/CHH8H4Mp/119dXSJZan8KSJMfYtK+nxd0GZeR1opraUVZyh6SyC8fpSojQrFOGgm2chtEWPVgKEmyI736sOvazP2Bqtr2uGoo3l jWpo7DVS Z5Z6sxxHm1AbFceiCkVyKd4n2e11DAOtQ69umSrMbDXLe+RHtxNOrqc8DcpOy8HRiLB49xbopbZEyPsEZkgUpuimLD32TiVyyoNB29MTeqje9Uk3ZqmeabuZpYVT7x3Ccehh8hwCoYqlHeVRFMf6X51BPFr1CiOheuMN3XQ/tAqvflb04u/hKp7rAu0xavUpnmFp0U2hMaiMx1M8a0roIdTaOyiCEP8krMyML0nt3sZ4qTKJ38vUL4ritl/zjM3JXPyy/1LH9fSbZTXbps0rHrRrhv4tMsP+2orpKjr8XGbWl/BnEb37k66rc8Zozq05/7CLrQ4N8a+7lUJTerBs/5Xs9lFlRvWrEMCWpfvZ5E5/i0KtbMnmhErdNbp9p/+0Q2vRGgCaYAMDUTrA= 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: On Wed 20-11-24 17:42:18, Amir Goldstein wrote: > On Wed, Nov 20, 2024 at 5:02 PM Jan Kara wrote: > > > > On Fri 15-11-24 10:30:16, Josef Bacik wrote: > > > 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. > > > > > > To make the flags more accurate we add a helper that checks if the > > > file's inode, mount, sb or parent are being watched for a set of events. > > > > > > This is going to be used for setting FMODE_NONOTIFY_HSM only when the > > > specific file is actually watched for pre-content events. > > > > > > Signed-off-by: Amir Goldstein > > > > I did some changes here as well. See below: > > > > > -/* Are there any inode/mount/sb objects that are interested in this event? */ > > > -static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, > > > - __u32 mask) > > > +/* Are there any inode/mount/sb objects that watch for these events? */ > > > +static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, > > > + __u32 events_mask) > > > { > > > __u32 marks_mask = READ_ONCE(inode->i_fsnotify_mask) | mnt_mask | > > > READ_ONCE(inode->i_sb->s_fsnotify_mask); > > > > > > - return mask & marks_mask & ALL_FSNOTIFY_EVENTS; > > > + return events_mask & marks_mask; > > > } > > > > > > +/* Are there any inode/mount/sb/parent objects that watch for these events? */ > > > +__u32 fsnotify_file_object_watched(struct file *file, __u32 events_mask) > > > +{ > > > + struct dentry *dentry = file->f_path.dentry; > > > + struct dentry *parent; > > > + __u32 marks_mask, mnt_mask = > > > + READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask); > > > + > > > + marks_mask = fsnotify_object_watched(d_inode(dentry), mnt_mask, > > > + events_mask); > > > + > > > + if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))) > > > + return marks_mask; > > > + > > > + parent = dget_parent(dentry); > > > + marks_mask |= fsnotify_inode_watches_children(d_inode(parent)); > > > + dput(parent); > > > + > > > + return marks_mask & events_mask; > > > +} > > > +EXPORT_SYMBOL_GPL(fsnotify_file_object_watched); > > > > I find it confusing that fsnotify_object_watched() does not take parent > > into account while fsnotify_file_object_watched() does. Furthermore the > > naming doesn't very well reflect the fact we are actually returning a mask > > of events. I've ended up dropping this helper (it's used in a single place > > anyway) and instead doing the same directly in file_set_fsnotify_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; > > + } > > } > > > > Nice! > > Note that I had a "hidden motive" for future optimization when I changed > return value of fsnotify_object_watched() to a mask - > > I figured that while we are doing the checks above, we can check for the > same price the mask ALL_FSNOTIFY_PERM_EVENTS > then we get several answers for the same price: > 1. Is the specific file watched by HSM? > 2. Is the specific file watched by open permission events? > 3. Is the specific file watched by post-open FAN_ACCESS_PERM? > > If the answers are No, No, No, we get some extra optimization > in the (uncommon) use case that there are permission event watchers > on some random inodes in the filesystem. > > If the answers are Yes, Yes, No, or No, Yes, No we can return a special > value from file_set_fsnotify_mode() to indicate that permission events > are needed ONLY for fsnotify_open_perm() hook, but not thereafter. > > This would implement the semantic change of "respect FAN_ACCESS_PERM > only if it existed at open time" that can save a lot of unneeded cycles in > the very hot read/write path, for example, when watcher only cares about > FAN_OPEN_EXEC_PERM. > > I wasn't sure that any of this was worth the effort at this time, but > just in case this gives you ideas of other useful optimizations we can do > with the object combined marks_mask if we get it for free. OK, I'm not opposed to returning the combined mask in principle. Just I'd pick somewhat different function name and it didn't quite make sense to me in the context of this series. If we decide to implement the optimizations you describe above, then I have no problem with tweaking the helpers. Honza -- Jan Kara SUSE Labs, CR