From patchwork Mon Jul 1 10:26:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13717790 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 72D5FC2BD09 for ; Mon, 1 Jul 2024 10:27:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BCC46B00A8; Mon, 1 Jul 2024 06:27:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52C996B00A9; Mon, 1 Jul 2024 06:27:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34AEB6B00AA; Mon, 1 Jul 2024 06:27:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0C77A6B00A8 for ; Mon, 1 Jul 2024 06:27:37 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7933A141A96 for ; Mon, 1 Jul 2024 10:27:36 +0000 (UTC) X-FDA: 82290807312.08.CBF16E4 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id 9DC2BC000F for ; Mon, 1 Jul 2024 10:27:34 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z6GNnASo; spf=pass (imf10.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 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=1719829633; 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=kwSCAOyZ+m+4B8m+nFVNQ68+e+AZDelUT9qN4fhek+E=; b=rlX2gozQH1/te8l/46sdo6Hi/+FTn017DFvAi2U5zfWtZUDCgYXDjnxbwyNRH2iZO7bhN3 Gts6dytXWDZqXFaIHTCCzCJOtyfiM6qlq/PYf1DFJ6RcxT81/NAWXEHZ0CLQkqzbuWugfH 9jGV9XS+Iec+4ejkHanetKqwkWyriNA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719829633; a=rsa-sha256; cv=none; b=3l0eCakSyebyJJvobM4MDW6fw7f+GDfQWtAGEapDy4Qc7y3F12SSS0aOpav8IRR6om8o5e CjQ7JofgNuZb7dHb0mvUIA+GX00Jzc4U1Cvdz3CgqwHDQXpmAOzHImvhfEmOeDpP9nFBA7 xIYvnBYzua4nd5jozdw9hmTS6GtmlCQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z6GNnASo; spf=pass (imf10.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D15F861221; Mon, 1 Jul 2024 10:27:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 947F5C116B1; Mon, 1 Jul 2024 10:27:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719829653; bh=yGbFoI0x64Criq5hVXpnf8cTDkEYS+drfXiF9/4UXTc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Z6GNnASo0fot0PtERrcGCWLqCNkhG+ti+9IgcG2z62xbIC1ym5I817maTy4fkKhbY R2soLnBsN9ntBuRXtGUYtbUxBrvdurk/E0KX7EbbDmjOFHzMmSyo90wX0FwClonR8X P4RBkhzh+f0YRGrqIUSoKG7uVfAoeaEL0Ss0NQOw6/1RUJIexOvqBArjV7Pm3HKK+T ELGB782hclQDbjeAOXe38I7SPtwUvE2y977540crj+h57ilap+jopmlWAGnCDy6jcC lu8QPn3FZYpTj4N486u8gYzuOufgDmx/UFNBSoDMzdpGwk8GFl1VmaMqBSCpZXUEsG h//393LSr0nGQ== From: Jeff Layton Date: Mon, 01 Jul 2024 06:26:47 -0400 Subject: [PATCH v2 11/11] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20240701-mgtime-v2-11-19d412a940d9@kernel.org> References: <20240701-mgtime-v2-0-19d412a940d9@kernel.org> In-Reply-To: <20240701-mgtime-v2-0-19d412a940d9@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 Cc: Andi Kleen , 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, Jeff Layton X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6401; i=jlayton@kernel.org; h=from:subject:message-id; bh=yGbFoI0x64Criq5hVXpnf8cTDkEYS+drfXiF9/4UXTc=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBmgoR51H+Vt+Exq9KPckzpWSJI72DyZw39g58HZ 6vgl2QTkO+JAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZoKEeQAKCRAADmhBGVaC FUgRD/9lsIjZK2rTJJTMM0K7QsIaOP3fGuxn3FJzgS/8fA0BxnWEFHyOjfHbW7G8Pk2e7T3FUtF eRaFdjrKm58/m+Pri5Gimbl9u0L3k8bs3OwWdfEHjjd3vY8dBeCHDYsyOOU96h0C8YAeLXYKW7v awrdt160eBzPfrVCLR+PYf0Nr2R5N1w4tTp8UCEaSy8BhOhiWHE5yEU+0dILaoEVaKub1bwYVjV mPVEWbsALVgC3JDWdkI4JTid9yXq+2fKgyT8ZRN9DuQqtfhetl6akwSxiTy2ZhYjQWXn4MwseN+ Sl42qLYCNzrpb+I3GsuPzd4w3YrijPaS2vDQ82BPfRbQqqj+64kR1zd/yjaqoWMcp0YB5VEUnGO l8uyRah0FF1rQNatV4c+a/SpXu1jdNXXtOG6rlSN+0Vih1+1Q/miYBQH6ApBVf+QJxDKzXW10YR ywIi+5WIwTwVnTTUoJdTzKDyW7vSlMCR/n50SSiuX9vYI7wm5/wi5HL/+KT5Idgv29ro6UPca5C tuQZld6B1HnPwUHD0E34Slj4q9H2YbmDz2UYLavQK9GWVOSWvyVApcHSF74iwlV6XMnozuLwVRs iOP1aC1GI5fBD0AJrcgAxYw/xjmU0slyWE7EPk6otXIYcdc4Qrgc4cWAu3TP2Cu6Kw7o6A269Hq VCQ37btdGTtuEEA== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9DC2BC000F X-Stat-Signature: n19hxf5ry6bpfz6e1e1gqqwzy4tf3osd X-HE-Tag: 1719829654-820123 X-HE-Meta: U2FsdGVkX1+pkzGnevoAvDY8aKdRXFuFSE5ZGspjSiIdp1Bhc2r/2bOc81Omoj1gEfuwsvemtBwBYW7WjMCmH3XTfMCr+qdYeBIA/0kjeoyeni8ONHPs0JSN1y3oN7jbElDP1mla/kvf/lp+iq/37B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lvtucSmKWCMgFHOml0LA== 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 | 126 ++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/Documentation/filesystems/multigrain-ts.rst b/Documentation/filesystems/multigrain-ts.rst new file mode 100644 index 000000000000..beef7f79108c --- /dev/null +++ b/Documentation/filesystems/multigrain-ts.rst @@ -0,0 +1,126 @@ +.. 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, and many just populating this with +the ctime. + +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 the lowest-order bit in the timestamp as a flag that indicates whether +the mtime or ctime have 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 per-time_namespace 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. + +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. + +Caveats +======= +The main sacrifice is the lowest bit in the ctime's field, since that's +where the flag is stored. Thus, timestamps presented by multigrain enabled +filesystems will always have an even tv_nsec value (since the lowest bit +is masked off).