From patchwork Thu Jul 11 11:08:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13730390 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 6FD99C3DA4A for ; Thu, 11 Jul 2024 11:08:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E25286B009A; Thu, 11 Jul 2024 07:08:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD4FA6B0099; Thu, 11 Jul 2024 07:08:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4ED46B009A; Thu, 11 Jul 2024 07:08:46 -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 9FAF96B0098 for ; Thu, 11 Jul 2024 07:08:46 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5A01C1A053C for ; Thu, 11 Jul 2024 11:08:46 +0000 (UTC) X-FDA: 82327199052.22.561E976 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf18.hostedemail.com (Postfix) with ESMTP id C95F81C001F for ; Thu, 11 Jul 2024 11:08:43 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WCzTZU0J; spf=pass (imf18.hostedemail.com: domain of jlayton@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720696099; 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=R9ZSF8Ea0l/p209368BdF6Z+Bw1WhPjc55g0E7Z79qY=; b=UYOc8SOMfpfLBzSE+pymSQ0wme25R/BzGZZrlYqzwLm+9hE7bh8uTB7hf2KOaUmPnI5bWl hhN4ivMF5TZzTuk1mGU9gRetlHr9qKCSRFodXcG8OJOkssK2m1zajk0FYRMQHbOGvw4NSa k5ESERMFTt8OESLvhsnLvQX534uEiM8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WCzTZU0J; spf=pass (imf18.hostedemail.com: domain of jlayton@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720696099; a=rsa-sha256; cv=none; b=Nwa/aGwXap0qMM8x3kv0sXYVjy9PHUeRDaGOb7scYjCg79f3St+vbYpLww/zwTBQwdzSeU TV+ZV8ju9PCMuYZT92ajDt2oxzLFN7OcDaD3nCEc8xNovZ1zwTtlRySZ/nKcVxNrh6Cu3M Zh2qiKUlg/5xFY3lGRCYbaE68o5OyR0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7ACF3CE18B0; Thu, 11 Jul 2024 11:08:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49D2CC4AF19; Thu, 11 Jul 2024 11:08:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720696120; bh=pW/jFkAPiF/HbV8e5Z5baq9Y5PvUD3bzN6ubPEHLIXQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=WCzTZU0Jr3v43eG8Kx5I8d4s5hM4Nl+4vwkWWwhE05eXLo7V1b4+GWNCE/DgQpjrC I4wY2fE1b8a9AzJf6PqRGaVfDxSQpO6S3ESRLeYAxDf4sN3ZcBDuErjicZUkW1FKoY M4c//kk3Wv8DChNgGR1TK/cPTm4pLi0RY/GC28H1rN03MPSHbtbTWmshnlF/2ojlLH rYQX9W079Lr36VIK9vZSc31GuUnOLeQo/eC1jCmnLGOSp5kNr1x7yNIFj1fOPSkcq6 Z4fG0FH8w9I2wetEiPhdXL5lmwxvUNZCS8wfBrR8OE4yw4GerUFNinTLpSfH65X8Oa 1EeUzRbEQKY8w== From: Jeff Layton Date: Thu, 11 Jul 2024 07:08:09 -0400 Subject: [PATCH v5 5/9] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20240711-mgtime-v5-5-37bb5b465feb@kernel.org> References: <20240711-mgtime-v5-0-37bb5b465feb@kernel.org> In-Reply-To: <20240711-mgtime-v5-0-37bb5b465feb@kernel.org> To: 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 , 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, Jeff Layton X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6283; i=jlayton@kernel.org; h=from:subject:message-id; bh=pW/jFkAPiF/HbV8e5Z5baq9Y5PvUD3bzN6ubPEHLIXQ=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBmj70lBBUxl022ofr76yZVy6+CpmCaiGsVxhD3/ T3eOiMHVuuJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZo+9JQAKCRAADmhBGVaC FWpmD/9kRQrNGkdVPNBW8W7BmKOZt1X+58Y3KY4xzbfBqyo3Eg/XG6dQT717nBirvgi8SfDwtIO JW1PZ6ynlRUujLXq/frAtLhMkEZfbgiyqHQnwRnpwL7utKuPHgze9QZBYbEfp9T9AzxH0qOpcEN J8Suy3DPo+btdubYZPs5rkpxRXyI0SnmsTDoYU/JmIFvUeRguPCDNMGNHRDx9uOt/z0wcOUti1p e8g+jaKiQYw+NnOx24ynuurPOmfg97ad6Z1w7YkiVWSuvnSoLhssKAGQcdKzllDCDpJjwCHWvjQ voU3krCPd+4HXIxC+/pUvFcYTDRYODr1DY96YL4P0whozRIxX4OsSmWKYofSbPVyZww1jYja+Wj HgRGVktAXTXmlXCSb34KeQ7gpBiOTl+ggwqidK86S5ubl8bm3bQwqC/rYjxxRU5OpzjBtQphNeJ Rvqb+vtV9V94Fu1fEaMD5HgGvywWWhN3+VmQqRMsExvrwAlfZc+/AHeNvP1gQJf3JpFR5KirITv NMkZR/IhO1xaXfNoD3JyqkoT0+VE+PrnNsdNI5O7o6ahm2BozM9GBqQTlCA4NR6laYYmy9teQT7 hWgSpi5wdzG60pn4z5+cqXs3cEZu34bW3K/UDzxjHeqeHe04KHdAbmZ2Ykz1oJPd91MfaB3E9WD E+Lb3+lWAbLCPsg== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Stat-Signature: k7oe6iszuirzw5r1eezifam5duzsm4qu X-Rspam-User: X-Rspamd-Queue-Id: C95F81C001F X-Rspamd-Server: rspam02 X-HE-Tag: 1720696123-674941 X-HE-Meta: U2FsdGVkX18jJYu8zkUomkow/jvd/Ve3CguDr5TL/9jS5ZFhQFEE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4oQco7P4e07L6u61TnSwWzsUgfIZid8/2KAKzrTyi2yX+p6jAwcGqNTIQmthmcce0uq6yKOS3mhxqooBwBldFuVFBOMeLMoWP7GWPuAlwAEu0yL9Hs4JoR5d3Dim9LthSOeifAcvWaGd7BFsUaCdxsNJnrdytjO5zB/Un5cabSCiyu3puVFhvjjvHUf3D/kaCON/DSZYW6SvHe8oNj+9Nv+qKKUVs= 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: 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. + +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 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. + +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.