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 7A2D2C3DA4B for ; Thu, 11 Jul 2024 19:12:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 003F66B0088; Thu, 11 Jul 2024 15:12:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECFAE6B0089; Thu, 11 Jul 2024 15:12:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6F0D6B008C; Thu, 11 Jul 2024 15:12:48 -0400 (EDT) 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 B6C246B0088 for ; Thu, 11 Jul 2024 15:12:48 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 518D34079B for ; Thu, 11 Jul 2024 19:12:48 +0000 (UTC) X-FDA: 82328418816.11.004F5A0 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf06.hostedemail.com (Postfix) with ESMTP id ED20F18001B for ; Thu, 11 Jul 2024 19:12:45 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=thMYX9Sc; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of djwong@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=djwong@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720725139; a=rsa-sha256; cv=none; b=GlRTKpkafJceat1ng87AXsH4VvjGTZnn2u8dcWAcxVYbzV5k5MW1dMZFOnRxQ/KxAtVZif QETRK43eohaPa9kLnRlQ5VuWbGtg3+Og/vqUUJkEPgaND7aco8WfSnJhiDJ17tVm/Gx0TN 6GLY751AmNGAtIR3BnzE3MYf7FA5ZSo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=thMYX9Sc; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of djwong@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=djwong@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720725139; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9IM+KpwstqPWbUh4rDuGBePPA5Dt0xAavg+H4FrdVVA=; b=bKSnruDWIixhdAiKMYWr8rxs5Q9ScSUXnGJCRviTshiywxUfUBwkqsOSWyrlnepVDNhj0X 8MRuJ3RCmBiqS45vNmo0PB2GEiIMx/xxNkRFwrBlgpKMdPBLRM8AE2yEZy4bZ2EsO6giEs rS1TDx6+Yf5izGl6/SGD2FWZC/to8qE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 032E6CE12A8; Thu, 11 Jul 2024 19:12:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41D65C116B1; Thu, 11 Jul 2024 19:12:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720725160; bh=f79aMT31G+Ladq5Y5IfX7kG5UX38505CTWzlDx7OFOA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=thMYX9ScLkPKicmopXpO7DygDDuXYmppMj0eSPlM2QSEsXddTvwUFcu77vpQKfkb2 IDZLLeSjje+vUizSCqrrtECNXREQuFPHalvlh11WNavlCgjH997Y4DrihAejuQEfRU /8lfggF1vIWoxtrJKnW+4Av4BAaIwPGDssPuk6bhkqY4wt9DJbQj4xefGcK3sx/0rk btrGeglbfsscbFrMSDcgInY4QfzX72692GsW2EPynNI4Rkrt1ONPVbR2td/Ksu9mFo edCln4xR4G49OOYgxQ9euB5jlScgvK2DH0a1FBBbYzI1WkS74Gh+IeloREQJIGuS5I zlbPAZ2/d58Xg== Date: Thu, 11 Jul 2024 12:12:39 -0700 From: "Darrick J. Wong" To: Jeff Layton Cc: Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Chandan Babu R , Theodore Ts'o , Andreas Dilger , Chris Mason , Josef Bacik , David Sterba , Hugh Dickins , Andrew Morton , Jonathan Corbet , Dave Chinner , Andi Kleen , Christoph Hellwig , Uros Bizjak , Kent Overstreet , Arnd Bergmann , Randy Dunlap , kernel-team@fb.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org, linux-doc@vger.kernel.org Subject: Re: [PATCH v5 5/9] Documentation: add a new file documenting multigrain timestamps Message-ID: <20240711191239.GR612460@frogsfrogsfrogs> References: <20240711-mgtime-v5-0-37bb5b465feb@kernel.org> <20240711-mgtime-v5-5-37bb5b465feb@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240711-mgtime-v5-5-37bb5b465feb@kernel.org> X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: ED20F18001B X-Stat-Signature: f5o5sn345xxo75quiocmw3goqonk8kg3 X-Rspam-User: X-HE-Tag: 1720725165-586170 X-HE-Meta: U2FsdGVkX182SbI8iDfbFNMaVm1dMxatahNbo5Tn05L8X11LMMQVA3YC/BTpkXVD3r7j22/HyLa23dD8NtR8DII62Og65NvypcvjD9w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MXSbKmjzptdC0119s87j+nXydOVTrrr4Z29wUsj3bekInvaPvXEOrH0tv2cZxMVhnvobhwFZxrf7k9zqAoINSUQ== 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 Thu, Jul 11, 2024 at 07:08:09AM -0400, Jeff Layton wrote: > Add a high-level document that describes how multigrain timestamps work, > rationale for them, and some info about implementation and tradeoffs. > > Signed-off-by: Jeff Layton > --- > Documentation/filesystems/multigrain-ts.rst | 120 ++++++++++++++++++++++++++++ > 1 file changed, 120 insertions(+) > > diff --git a/Documentation/filesystems/multigrain-ts.rst b/Documentation/filesystems/multigrain-ts.rst > new file mode 100644 > index 000000000000..5cefc204ecec > --- /dev/null > +++ b/Documentation/filesystems/multigrain-ts.rst > @@ -0,0 +1,120 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +===================== > +Multigrain Timestamps > +===================== > + > +Introduction > +============ > +Historically, the kernel has always used coarse time values to stamp > +inodes. This value is updated on every jiffy, so any change that happens > +within that jiffy will end up with the same timestamp. > + > +When the kernel goes to stamp an inode (due to a read or write), it first gets > +the current time and then compares it to the existing timestamp(s) to see > +whether anything will change. If nothing changed, then it can avoid updating > +the inode's metadata. > + > +Coarse timestamps are therefore good from a performance standpoint, since they > +reduce the need for metadata updates, but bad from the standpoint of > +determining whether anything has changed, since a lot of things can happen in a > +jiffy. > + > +They are particularly troublesome with NFSv3, where unchanging timestamps can > +make it difficult to tell whether to invalidate caches. NFSv4 provides a > +dedicated change attribute that should always show a visible change, but not > +all filesystems implement this properly, causing the NFS server to substitute > +the ctime in many cases. > + > +Multigrain timestamps aim to remedy this by selectively using fine-grained > +timestamps when a file has had its timestamps queried recently, and the current > +coarse-grained time does not cause a change. > + > +Inode Timestamps > +================ > +There are currently 3 timestamps in the inode that are updated to the current > +wallclock time on different activity: > + > +ctime: > + The inode change time. This is stamped with the current time whenever > + the inode's metadata is changed. Note that this value is not settable > + from userland. > + > +mtime: > + The inode modification time. This is stamped with the current time > + any time a file's contents change. > + > +atime: > + The inode access time. This is stamped whenever an inode's contents are > + read. Widely considered to be a terrible mistake. Usually avoided with > + options like noatime or relatime. And for btime/crtime (aka creation time) a filesystem can take the coarse timestamp, right? It's not settable by userspace, and I think statx is the only way those are ever exposed. QUERIED is never set when the file is being created. > +Updating the mtime always implies a change to the ctime, but updating the > +atime due to a read request does not. > + > +Multigrain timestamps are only tracked for the ctime and the mtime. atimes are > +not affected and always use the coarse-grained value (subject to the floor). Is it ok if an atime update uses the same timespec as was used for a ctime update? There's a pending update for 6.11 that changes xfs_trans_ichgtime to do: tv = current_time(inode); if (flags & XFS_ICHGTIME_MOD) inode_set_mtime_to_ts(inode, tv); if (flags & XFS_ICHGTIME_CHG) inode_set_ctime_to_ts(inode, tv); if (flags & XFS_ICHGTIME_ACCESS) inode_set_atime_to_ts(inode, tv); if (flags & XFS_ICHGTIME_CREATE) ip->i_crtime = tv; So I guess xfs could do something like this to set @tv: if (flags & XFS_ICHGTIME_CHG) tv = inode_set_ctime_current(inode); else tv = current_time(); ... if (flags & XFS_ICHGTIME_ACCESS) inode_set_atime_to_ts(inode, tv); Thoughts? > +Inode Timestamp Ordering > +======================== > + > +In addition to just providing info about changes to individual files, file > +timestamps also serve an important purpose in applications like "make". These > +programs measure timestamps in order to determine whether source files might be > +newer than cached objects. > + > +Userland applications like make can only determine ordering based on > +operational boundaries. For a syscall those are the syscall entry and exit > +points. For io_uring or nfsd operations, that's the request submission and > +response. In the case of concurrent operations, userland can make no > +determination about the order in which things will occur. > + > +For instance, if a single thread modifies one file, and then another file in > +sequence, the second file must show an equal or later mtime than the first. The > +same is true if two threads are issuing similar operations that do not overlap > +in time. > + > +If however, two threads have racing syscalls that overlap in time, then there > +is no such guarantee, and the second file may appear to have been modified > +before, after or at the same time as the first, regardless of which one was > +submitted first. > + > +Multigrain Timestamps > +===================== > +Multigrain timestamps are aimed at ensuring that changes to a single file are > +always recognizable, without violating the ordering guarantees when multiple > +different files are modified. This affects the mtime and the ctime, but the > +atime will always use coarse-grained timestamps. > + > +It uses an unused bit in the i_ctime_nsec field to indicate whether the mtime > +or ctime has been queried. If either or both have, then the kernel takes > +special care to ensure the next timestamp update will display a visible change. > +This ensures tight cache coherency for use-cases like NFS, without sacrificing > +the benefits of reduced metadata updates when files aren't being watched. > + > +The Ctime Floor Value > +===================== > +It's not sufficient to simply use fine or coarse-grained timestamps based on > +whether the mtime or ctime has been queried. A file could get a fine grained > +timestamp, and then a second file modified later could get a coarse-grained one > +that appears earlier than the first, which would break the kernel's timestamp > +ordering guarantees. > + > +To mitigate this problem, we maintain a global floor value that ensures that > +this can't happen. The two files in the above example may appear to have been > +modified at the same time in such a case, but they will never show the reverse > +order. To avoid problems with realtime clock jumps, the floor is managed as a > +monotonic ktime_t, and the values are converted to realtime clock values as > +needed. monotonic atomic64_t? --D > + > +Implementation Notes > +==================== > +Multigrain timestamps are intended for use by local filesystems that get > +ctime values from the local clock. This is in contrast to network filesystems > +and the like that just mirror timestamp values from a server. > + > +For most filesystems, it's sufficient to just set the FS_MGTIME flag in the > +fstype->fs_flags in order to opt-in, providing the ctime is only ever set via > +inode_set_ctime_current(). If the filesystem has a ->getattr routine that > +doesn't call generic_fillattr, then you should have it call fill_mg_cmtime to > +fill those values. > > -- > 2.45.2 > >