From patchwork Wed Oct 2 18:49:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13820259 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 AA842CEB2E2 for ; Wed, 2 Oct 2024 18:50:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE04A6B027F; Wed, 2 Oct 2024 14:50:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B14006B0405; Wed, 2 Oct 2024 14:50:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9694D6B0404; Wed, 2 Oct 2024 14:50:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6601B6B00D0 for ; Wed, 2 Oct 2024 14:50:18 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 19FCCC0E86 for ; Wed, 2 Oct 2024 18:50:18 +0000 (UTC) X-FDA: 82629552516.05.C26789B Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf07.hostedemail.com (Postfix) with ESMTP id 455A54000E for ; Wed, 2 Oct 2024 18:50:16 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XvBH9LB2; spf=pass (imf07.hostedemail.com: domain of jlayton@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727894846; 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=1qeWHE5mHrGFsc1b44PsJIUS6Zyt5VkttsRW7fHuGGk=; b=qgAlOdCurDG1JdxhV2fhB+eFQPbYZgA5AZlY/LLEBKdpq5piMdOsOi5ovM9hNhAFBM2LmS qBJMAHKzBUQTdi0l2levGWhr7agDn8yMPbsjMYkdhNqKz2zWK5YmI0aFVu7E4GIP8gzwYj N0ZTiemOq6d5BeGco9fP821KjC7ISdk= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XvBH9LB2; spf=pass (imf07.hostedemail.com: domain of jlayton@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727894846; a=rsa-sha256; cv=none; b=sUI68qNT8BUR/ySX4kyKYtrF9gfiMZbQt1WLMQn1iFlJBzELUC+AMwABS+vWkz88R46+vk leTDo4H2BhMfiGUuVk5W4BOZFARj6krKHB+JqnezlAyguIoCPJr8sId8n7in1dtWvowAox DU0IbMU4GYgyYDpLDL0cmp6XffTs7iQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id A1BF8A427CA; Wed, 2 Oct 2024 18:50:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 18FC3C4CED5; Wed, 2 Oct 2024 18:50:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727895014; bh=yU3ux0/2Dtj33S4oJe5U/3bWMbsd90MTGAANY4A51kE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=XvBH9LB2fmEjgRxUykO9Mmm5sj8iqfay0+A30RJBERXYirFJUXgSdeB259F90X6PF oXNBts/3rpgqFkpclUGpTpAV7HpwOsfjyE06TclQoeCCL2rIRwAoCQwFxD0Pat0QTG tr12tHICR1C2F2Axhdui82G5Xqa4gCQQEgPbuzemgii7wf302vlF36BY9MEszwBis0 mQR84ZKkMne3WdaUMa+1deCr3t5xJ9Fw/zBxfefkH46o4bzgfEDzLNfl/5fKdogt3F VilUn4NJ1u2+At3B7NV8Xgu8M1/+rmJNBMmaGMIFd2TM6Vekj2whJLFXpYjBHN5qNb 36eQC74yhzrlQ== From: Jeff Layton Date: Wed, 02 Oct 2024 14:49:36 -0400 Subject: [PATCH v9 08/12] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20241002-mgtime-v9-8-77e2baad57ac@kernel.org> References: <20241002-mgtime-v9-0-77e2baad57ac@kernel.org> In-Reply-To: <20241002-mgtime-v9-0-77e2baad57ac@kernel.org> To: John Stultz , Thomas Gleixner , Stephen Boyd , Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Randy Dunlap , Chandan Babu R , "Darrick J. Wong" , Theodore Ts'o , Andreas Dilger , Chris Mason , Josef Bacik , David Sterba , Hugh Dickins , Andrew Morton , Chuck Lever , Vadim Fedorenko Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-mm@kvack.org, Jeff Layton X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=7290; i=jlayton@kernel.org; h=from:subject:message-id; bh=yU3ux0/2Dtj33S4oJe5U/3bWMbsd90MTGAANY4A51kE=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm/ZXMHmQlIM1XltglOKZZMLpcT3TrGf3O4cPbk 2Y7Zl/tM/GJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZv2VzAAKCRAADmhBGVaC FWtmD/4ljmJ5SjJ0YYZIgYOuuC1tG2m9LzbX5IH1G9yVu5KijxWOZWa6jl/6djpXxBIR/hXE+8J oQ15OQ5qJ19h9q7ixp9LTwzbhs0CRzMDQflyXtW2Pbl0fCd5/pKb3uXbwrEN8Bqc7Yz9EQB1sMo wSPhHCdR/h1Dl9W9yytIAlPr1mg6WqV9iynvU80v9VYF3vd/lM5BPd96ddlnnNDRBXvb+7xlOkA mtmzAxtOhcdSDKch71FVHYmJ3PR+uZGG8E5XfIRH3j1MfPW3YZgOelTOZ6DoiV6e83XYAUuMQm1 wuna4OKC0zXlyFUUYiwyet/jt67VRc2ht8Q3QK+68wkQgROjSPTjoSr1iU/ypxH0RnPNBrMQdXJ Nqv99kKd9TmJPfVWt4MYxx0fes4+i/iWTsV9LEKbpU3EXU0KF/HyJnecw64V0rHLLDY+v2RP8HZ jQ5mBuKpRNtezbXYOzCBFri6HwyEUsmBl/6EG/r0n7M8neuxorWx8bkIdIvMWSLTJk8wqmWOyV8 d6KZdZ9NDYkfOatQMbQfkiwIXsZTd9jnp1MUh+hYXcEyU0dtf8TJtTxInzPS1ZwuF83XFstwvCr vJs5e1foaiQdx7tMBIhFWmhzuZPmbqd/EW1C6sWjQg/r0qRDBvZx4Jk7oyesdhUotKyhorBc9pj ttfdObbsH8mEtVQ== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 455A54000E X-Stat-Signature: i5iksn77jt5sc8fae81npmiftaxbfeno X-Rspam-User: X-HE-Tag: 1727895016-177998 X-HE-Meta: U2FsdGVkX1+cn0JI2uEX/D0hgp3+AUtaxst92BQPpOwQBecsEz88OesoSUWxF6htK5zJLGL2yK1SPXjBtqDJO9Ep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JEqvrg8HOYerYKQqCnT4GnVgLQDIVrugmthxMImK8sXKIt3vk4eiYmutvvg7Sh23gm3NCMAyuyoGEUgAwMeFhlUyC+p6eiwX+xHD4RvbRuQ3Qak9+LTq2P1YLk80h8p8rz8WzdETrXoJAXsdtAiPSOw8JFbVVG4VPy2QfJklAiNwb5W8= 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 Reviewed-by: Darrick J. Wong Reviewed-by: Randy Dunlap Reviewed-by: Jan Kara Tested-by: Randy Dunlap # documentation bits Signed-off-by: Jeff Layton Reviewed-by: Bagas Sanjaya --- Documentation/filesystems/index.rst | 1 + Documentation/filesystems/multigrain-ts.rst | 125 ++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) diff --git a/Documentation/filesystems/index.rst b/Documentation/filesystems/index.rst index e8e496d23e1d..44e9e77ffe0d 100644 --- a/Documentation/filesystems/index.rst +++ b/Documentation/filesystems/index.rst @@ -29,6 +29,7 @@ algorithms work. fiemap files locks + multigrain-ts mount_api quota seq_file diff --git a/Documentation/filesystems/multigrain-ts.rst b/Documentation/filesystems/multigrain-ts.rst new file mode 100644 index 000000000000..c779e47284e8 --- /dev/null +++ b/Documentation/filesystems/multigrain-ts.rst @@ -0,0 +1,125 @@ +.. 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 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. + +Note that the above assumes that the system doesn't experience a backward jump +of the realtime clock. If that occurs at an inopportune time, then timestamps +can appear to go backward, even on a properly functioning system. + +Multigrain Timestamp Implementation +=================================== +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, 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 it should call fill_mg_cmtime() to +fill those values. For setattr, it should use setattr_copy() to update the +timestamps, or otherwise mimic its behavior.