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 9BBCBC3271E for ; Fri, 5 Jul 2024 20:59:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D2816B009F; Fri, 5 Jul 2024 16:59:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25C396B00A0; Fri, 5 Jul 2024 16:59:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D5BB6B00A2; Fri, 5 Jul 2024 16:59:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id DC4996B009F for ; Fri, 5 Jul 2024 16:59:24 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5F44EC03BE for ; Fri, 5 Jul 2024 20:59:24 +0000 (UTC) X-FDA: 82306914648.17.32D1631 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf02.hostedemail.com (Postfix) with ESMTP id 0124780003 for ; Fri, 5 Jul 2024 20:59:20 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=s1PZHYML; dmarc=none; spf=none (imf02.hostedemail.com: domain of rdunlap@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=rdunlap@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720213143; a=rsa-sha256; cv=none; b=6sj4PpserJnQ4F543qpm4fTQwROSwkBNyK8tfsAJvCYKcEQn3/UxHbJW08JORsaMubK8de xpqyFd8l/NVyR7qCP0PLzENFZ0lx/TLDCZBiQypBVMi2wSCX6QoFhVRn10dao6ydwnC1Lr Ogl7W5QcsBKD5Ygt4KVaXg9de0Aecos= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=s1PZHYML; dmarc=none; spf=none (imf02.hostedemail.com: domain of rdunlap@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=rdunlap@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720213143; 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=/Kb85RBByRKA+7u8OSLey5Gf4F7B6fkoI9PSqKXpd2Q=; b=479UxeXeacArD41fa/JdbsPdnBIhPOiI+l6oMPAXDyAL/5U2aaRfgxmc98wE1b1I4GtFUV Qi1XqTAIrOAt24uJcJp4QwpIlGyd3RmRIf3Nl90BJWSbtaMv8c0FJCThhrfjpPCOCIHAtW ac2/vT0JRIGxkERycol3N05LUArDvIE= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Sender:Reply-To:Content-ID:Content-Description; bh=/Kb85RBByRKA+7u8OSLey5Gf4F7B6fkoI9PSqKXpd2Q=; b=s1PZHYMLbcx7leKxCoOb+zA0Fz WIgyuKpHtDdcKqCLFMKJm8ZdS6QhpDzWXz3psaK/RrEFicjAvC9fRqL5HAT31ux5CfxQVeh4HKD0X Dx24gnQRdn8NofoFKxzTI/jnLJmmwJH1h45FomlHuaYKmQ1yIASsbnk8eT7XGeIaOpr7pax7/9R9L 8mYcqfXnv1pbLDD9qjjTe0NbegNpKCrQ76+gi3XOO3yEIOPGi7NEb6y8eD/bv/s/jgiRHqnWvbz4C lN6+iO5k1jTKZfqtpQ8SSiC4ZitAY/UPwKEm1V9UOzJ7gah70FkWDPocpHML7eJpZHIMqILz2A6iW +Az7UDbg==; Received: from [50.53.4.147] (helo=[192.168.254.15]) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sPq1N-0000000GpGP-3COr; Fri, 05 Jul 2024 20:59:09 +0000 Message-ID: <27400c92-632b-4f58-956a-eb2cbb0cfe26@infradead.org> Date: Fri, 5 Jul 2024 13:59:08 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 5/9] Documentation: add a new file documenting multigrain timestamps To: Jeff Layton , Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Chandan Babu R , "Darrick J. Wong" , Theodore Ts'o , Andreas Dilger , Chris Mason , Josef Bacik , David Sterba , Hugh Dickins , Andrew Morton , Jonathan Corbet Cc: Dave Chinner , Andi Kleen , Christoph Hellwig , 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 References: <20240705-mgtime-v3-0-85b2daa9b335@kernel.org> <20240705-mgtime-v3-5-85b2daa9b335@kernel.org> Content-Language: en-US From: Randy Dunlap In-Reply-To: <20240705-mgtime-v3-5-85b2daa9b335@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 0124780003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: n4uozyhp11tmdw3wnpn63hycsex1ru7y X-HE-Tag: 1720213160-462494 X-HE-Meta: U2FsdGVkX19xRWXvjX/slaTo0bhww7z5z36g/FzgJFQkNX0BWP/N6WMoMwq1d3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GQ0nrrRPIpNAIKWiDjdGL2vMK+snlS0qAN6uj4QbcZg1qeea5a5rrgbGLURxiSAvGzKwtRcINANfsPnrWBEIOYOo5BYKFU9uTSqzODTiYZePjeycht8qsLlad9Mj0GedekR8xmRfoBYQtchhRCyonlEuhQ== 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 7/5/24 10:02 AM, 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..70d36955bb83 > --- /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 a coarse time values to stamp used coarse time values > +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. > + > +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). > + > +Inode Timestamp Ordering > +======================== > + > +In addition 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 recognizeable, without violating the ordering guarantees when multiple recognizable according to what I can find on the web. > +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. > + > +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. > -- ~Randy