From patchwork Fri Jul 5 17:02:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13725404 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 51DE1C3271E for ; Fri, 5 Jul 2024 17:03:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83B916B00A2; Fri, 5 Jul 2024 13:03:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 799256B00A4; Fri, 5 Jul 2024 13:03:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C5D76B00A5; Fri, 5 Jul 2024 13:03:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3B28F6B00A2 for ; Fri, 5 Jul 2024 13:03:13 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ED9D8802A4 for ; Fri, 5 Jul 2024 17:03:12 +0000 (UTC) X-FDA: 82306319424.10.2F8D6EF Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id 21A30C0006 for ; Fri, 5 Jul 2024 17:03:10 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=E1pYph3G; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720198971; a=rsa-sha256; cv=none; b=zNguWj/d04GKMEnJcHUqzYAeOg9irD8wdnBenexQs03cc/w/0m9vOHHHtu6wlWST1RksiJ 0DObUqhnkEYtIopZK978gmOnOBdv1MhB3xF+KpKHeL2XDTEwk2laXE7GZauhE/oUbNaHlr 3ZtdzFNPdGXHs+MQX+glgfPBbgJS2V8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=E1pYph3G; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 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=1720198971; 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=1pd/m7fZXEETNRFlB/7XStY5CGT2AuxekY5FB49CcCM=; b=PXUwT9sMRSO0bPLZfL3zfwJBxYbE0jFT/DhClr0LXGKJZiBXrVIuHacMdU4HcVgKSL0qLM BHMN5fFiEug0gYbM0jowMYuufN0U7gLzN6SX8sinzJcs4Zja09DVITvZSGpWdzfKw/CznQ bz65UDWQmxXMq0TzMY57zfifCnjsd9w= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 4D41762C2E; Fri, 5 Jul 2024 17:03:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8B28C32786; Fri, 5 Jul 2024 17:03:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720198990; bh=Jo9jHESP93Ns2L4cRjs189VoSP34rgHP4WDevXUywUM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=E1pYph3G5cHM90wnA+rcrhKplEYNOLR7Ksl5KmleFIUQzmfyG7jkLtc0DACl/wVcg cGhyu+/HijeYX2jBXA+OPg6Buw8BLzMzi/liYgQEG8Ydgpe0OHb12W0yeIV7mITqco +R/qbBy2mlJnnCj0cuAD+D+O33qnB4Epuh/xTJp821cniJSk0TqmliyXMup7+24xvQ TsRrYPyX2jvwrZtwkwDduvFwTUorm+NBwFDOaeXDIkK6sppsbl7xxZK3jSfdtlSHu6 vJFo5SRSflfSOrk6/jqD+vV+EKDwoBiBL18eduVlH0mIN234EKXGU36kw+lFn9XC3a ZK9Krotb1HwsA== From: Jeff Layton Date: Fri, 05 Jul 2024 13:02:39 -0400 Subject: [PATCH v3 5/9] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20240705-mgtime-v3-5-85b2daa9b335@kernel.org> References: <20240705-mgtime-v3-0-85b2daa9b335@kernel.org> In-Reply-To: <20240705-mgtime-v3-0-85b2daa9b335@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 , 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=Jo9jHESP93Ns2L4cRjs189VoSP34rgHP4WDevXUywUM=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBmiCc96KXbG+1N5n+hAriiu7soeEHZK7nCeSebK b73rjQpQgWJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZognPQAKCRAADmhBGVaC FeWCD/4xwaw0Zu+wyl2eZJk3bgsgpCQvLN8RxReh2ZqY80Wj6IWqmAOK6HAeWxAEHq0NHWG6ned QgTwfDYznBdehBExWUDppv7UBWlAR8kphFotIhSc8sRonxSs9LyanzukWk/93rWBlOHKQ5V8g09 jXXAJSacWxbI7SDjMjz+mHaJ5r2rj3amOmdzOoDJ6i51t2XEsSWNuP0iw95N9/wJGnId79bFxuL QRgS3IMAY6qvN+ZqoNVeiRgYjR+xlJyOrny4M9nc1jUHF6WXO3yW2SZTD9IRY5h5gvYCh09kwX2 6XOI3z+o+ACdDYGfwMiG7ORo8+4AZby7l43ApWVJiu7vs4tbb8vEgz/iIbGsDXzE9NbZHcZFTfV BNF2mNW5bgBvUtK6JZeYn/Ks3glY99cmhp7Q4pkBfuwvEkt5+2tK/dcsjre29dlA1cERmEplqL8 M9f9/BhpE6+1eh2KLctnyu010c4kEBOCRHBmXgCyKun+wqT3qy1w/ACDMGyoXBgeEN/QjQbN1lT re0AjBQ0eDcHa6wRdjD+2VlXM1997QIimFHl+xoHIP5bYkwL3wM1wIvDRp6RwmKYVYIkFFBtN7Y qPn3gcpSwQt6czdZ6qeY2ntqt++WEgQW2wz6/9s0vMEc7cJuwuWJgFOHtsu03KAYoaZSWEA0gWF VwHbct/dgY/7D3w== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Queue-Id: 21A30C0006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: tke56bdbgiit6umzk4bera8c9fjpyte3 X-HE-Tag: 1720198990-125458 X-HE-Meta: U2FsdGVkX1+dUC8+fZnMwnyO8jCD3f0nBxy9uC1QVS70RqJZ3wDGbUL+bfqOgFWZ5whgYLTzzQ/EF7HIzbXKnlnBB1l10YCm8jgF1Hp0s3jEIRbVyGqJ8SHHrk3DyOO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knyOlzxjnSNSvjAE8k56X65w3xarNigApI5Hw== 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..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 +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 +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.