From patchwork Mon Jul 15 12:48:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13733443 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 DA49FC3DA59 for ; Mon, 15 Jul 2024 12:49:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DAFD6B009E; Mon, 15 Jul 2024 08:49:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 910C16B00A0; Mon, 15 Jul 2024 08:49:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FA836B009F; Mon, 15 Jul 2024 08:49:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3C6176B009C for ; Mon, 15 Jul 2024 08:49:35 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E382E12161E for ; Mon, 15 Jul 2024 12:49:34 +0000 (UTC) X-FDA: 82341968268.15.A0B003A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf07.hostedemail.com (Postfix) with ESMTP id 850FB4002D for ; Mon, 15 Jul 2024 12:49:32 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z5Orqeh8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of jlayton@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721047731; 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=ofgP9nmBDlCXsH4UIVzu2VUwgNgOG2mQ3+tiyTUSNgs=; b=BIr6DGXkIooYeU4RtJCMHqWDJVmxs58r/TEjuzEVJUg3Rses4AmA3WocOgB+aUBkbgH1Rx BJOXNyt2N1LAtB5QnxyfNzPeqaGZUsY9+GpZKDhCujROYw/AF4hMIvjP/QIJMnOMENyXav h7ZGepReTbOqR5+04uEnT3dQm0Ffd2I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721047731; a=rsa-sha256; cv=none; b=ApXig1SHXT61N+OjZCe6HSfnpJlBHTtkCFV11xt0LHtgjECVhp71P9pJzKwyBCWFPLE3LX LtbpeOZP+TDaaa2np1clM9re6qifzDiiIochhvn7kEB+g1ni0umVRBAETXmuGVXdV2znE2 AVmuWc/Nt9OBgS8+X3FKl1+RneYFyx8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z5Orqeh8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of jlayton@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=jlayton@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 09DA0CE0FAF; Mon, 15 Jul 2024 12:49:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 964E4C4AF0A; Mon, 15 Jul 2024 12:49:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721047765; bh=iWqOXrOqIjnZmnolg+NEkEq3Yoj/1L10uHjOuc/xylw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Z5Orqeh8dgUc1gNrWO3awvIvearNO9jL/GLYnPiI9DfmGf76A0FSclTsiydvsVdLX P9z9VAeoZuqzFgOvQPhpA2BmMpvnmyZsI9Kd2ynTkH+ZcZtfjNVVAEzpA6RgAWof3c o/N6/gadf6s382d4wQ4m1FNp7Xh/uQuQUuzrqwZukVlT8q9iTq+jarW+dGVMu8szFE bisYIjh7qxk/bJfUKxkw3afk+G7/sb/xtgcW3wCgyi8K7lvffpoyAt35E8jC1SxieU 1MsnZzgZhKv9hABIDSRl7acGfYIcVREvZ9L0J3uu5d5S59ku7A7lpsvDWx/2JWOQo/ EEodhM1YL0B6A== From: Jeff Layton Date: Mon, 15 Jul 2024 08:48:56 -0400 Subject: [PATCH v6 5/9] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20240715-mgtime-v6-5-48e5d34bd2ba@kernel.org> References: <20240715-mgtime-v6-0-48e5d34bd2ba@kernel.org> In-Reply-To: <20240715-mgtime-v6-0-48e5d34bd2ba@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 , 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=6332; i=jlayton@kernel.org; h=from:subject:message-id; bh=iWqOXrOqIjnZmnolg+NEkEq3Yoj/1L10uHjOuc/xylw=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBmlRrD1XVGZ1c8cCIJmwfS2YZ77i/n0JY0rvv7g EETniwfzleJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZpUawwAKCRAADmhBGVaC FQDbEACwh6VmE4qLcnqitmCqk2O7mVTzebx37UKl9uS8FvZaSKi7BdWNZFzrWhFsOOo9nzQ2J7P EqhJd5AIWZUsRNsgRnWS+LPusCZkJ9r6EpGPCg8hCzqMJH3YFaOJyt7s2qn3/UxF4zUQ+G2T1r/ VdlV60RJVEb6Qs7Nr/5oxiN0TSy92wa+Ghd3Fj0BNLCrY3LvOft1bl7wd7wRAnTcBT5hhdhkaLo j30lZHe8lEr4UkWtY9pEx/v17SxSJjfm0CB1fxn08pvNYczGPGRgsEQj5jyZZqMOTb50zZINVIw UOt6gLE6BvayN94BfnnRzzCs+EErUWMY/3g8ef1Axz71mdyfAs+xebNeTulyC6ZRqPbJRjBMhgc J7iGxT9m95TeJEmmvX+9/IL21XPs37yDNx35L9jPy2xZy/0TuROcmu+ev5JqSD4NnXJ/Crkp3JL fGi5gHVxrzVS6gCxkWaVYkybLQMz9KL9ni/aVdPdlWVdCps+lNuhIHtwcyQi/5rh1Vzwn8XR7ru 0aOuBIuitYqxg5TMVk4IGrT1gYV11D06hQSs90iPER8bf2rT9bbuahH4SjgL4d8yGNJf3ZyBvh4 cR9+fivitdTWSR5mpTSQ6bSqDDpt+zzYtLglpl+Gg2gHblc26C72aVc6yXyHbfHYomie/n2H+9q a2O0O4lkIL9NCJQ== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 850FB4002D X-Stat-Signature: oe5bctbsca3tuomq61utxtmtiuaw4x4s X-Rspam-User: X-HE-Tag: 1721047772-549838 X-HE-Meta: U2FsdGVkX19BsRtUoRnZLBb2bvCy3ywIR7x5bwgYxRG98b5F0N0CjWzDWpCFSyO/c23hwWVCIt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B7DpuKuNuJ0Zx0IlRU3xYxLwRMPh2kfZvac76lP8+1drC1SVy4IvmBf0gKNbZn+QeVrBIpTP9C3W8164aK8d2f21FNxidCrfNSs1A2fCQgcBTX4+aDLXP5yl17X5s2Q+WQ628qXWM2Q== 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. Reviewed-by: Josef Bacik Signed-off-by: Jeff Layton Reviewed-by: Darrick J. Wong Reviewed-by: Randy Dunlap Reviewed-by: Jan Kara --- 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.