From patchwork Mon Jul 8 15:53:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13726775 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 458AAC3271E for ; Mon, 8 Jul 2024 15:54:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 549456B009B; Mon, 8 Jul 2024 11:54:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CEF36B009C; Mon, 8 Jul 2024 11:54:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34A566B009D; Mon, 8 Jul 2024 11:54:02 -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 13C8A6B009B for ; Mon, 8 Jul 2024 11:54:02 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8F604A458F for ; Mon, 8 Jul 2024 15:54:01 +0000 (UTC) X-FDA: 82317031482.17.E26EC4A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id 87D6F10001C for ; Mon, 8 Jul 2024 15:53:59 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="AX/xL3fF"; spf=pass (imf14.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=1720454024; 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=XZrdp0bNDFvi8wiEFlMR9pPqfGnh+LwGxLU56c6fuI4=; b=q1r3N8bWza4i2uXIu2DVXIw7q1Hx2NaLIcxWXqMJUhtjYxtZxpkTZiNi4gS+hXcFwYNiNG JB+klmVh9TYNDnIUO8gM6c6NPiMzpKHTqXyVaTgafHPiSSID3Ks+X4+tFk1iEhMnMKybvp 0xSSde/YNxML6WQeNqAw4PM7ZM1tJ2s= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="AX/xL3fF"; spf=pass (imf14.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720454024; a=rsa-sha256; cv=none; b=Mu2pwIJNK4O4Ce2wI9Xg3mwUGCPflx6HFuPr2ZKPXWTOlA/dWUmJ1kHgVAwrJ5syyipYCt queOcRioCDaF18I4dYW0rhnysx11Vtf3kjsSBU0w0Ljr7MVXr3OtQ3R3wbNSu8oQdxNth+ hKaqLGQ8FaOfDo6lHF2soBKS/s0FONY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9C5E960E93; Mon, 8 Jul 2024 15:53:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C387C4AF0C; Mon, 8 Jul 2024 15:53:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720454038; bh=3kjkEi2lp3qDJyH5rtduUEdM10TvHoB9LhQ8/+GSXaA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=AX/xL3fFvU92GisiPs6Zc+adSfi3GyzRCVxVZfJpqtiEvLxdhhF7U10nvmJ4UtyQR fM37bQQPjdiwtSTcbdYn6LMjRFST5q5kd+Ew8F5fFLpiXChImlHCy0ELww42G7Y6RH wAgMYrnoci9J+K2MPrLa3Ic22crAXJNDM+0W9xIzuRdAOgkRJg69GTE27Jij+j1EEB yX7574tzzeUnnYY0aGKe7t0GaAJIpw6+NmA+fQIQl4zWPSileA0i8ZKGlYlonX//zw osAZqb3+Zf2gTgvnVKUJho0qMxzZGfuCK4hOzfoNv3qEKgZVs+fKLTCIeHVSwA2RE5 XtVMN76HRT3Xw== From: Jeff Layton Date: Mon, 08 Jul 2024 11:53:38 -0400 Subject: [PATCH v4 5/9] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20240708-mgtime-v4-5-a0f3c6fb57f3@kernel.org> References: <20240708-mgtime-v4-0-a0f3c6fb57f3@kernel.org> In-Reply-To: <20240708-mgtime-v4-0-a0f3c6fb57f3@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 , 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=6280; i=jlayton@kernel.org; h=from:subject:message-id; bh=3kjkEi2lp3qDJyH5rtduUEdM10TvHoB9LhQ8/+GSXaA=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBmjAuEvMLYjCZyHKB07a0DIX65Yy1lyjMtPYQsB p/ppuqsUWWJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZowLhAAKCRAADmhBGVaC FbyGEACt5Is1KDr6nFAsnXhrK6hwHEq1NOHCSrSYTx0zl7StwaPcSBLXI3etbY+cCyHngFD1G2V wXXrArzYHOXmX2fDCkftgwk9nXB3Exz9eK+6UO2RGTxxYCFxNN8ihq49HApbfn9eFa2+P8x3Rh3 uXzX61hR4Vz4awjOxZixbhwCpV5sK+2e1WmZvH1TpsQgC1e8GW45irlUQmj19a7h41VG2ybmKw8 5JxJxJtnUIHo+7vhwIuLthwH4Mvmt52xiv7J8mJIHyYvdqk5SteVn6EE6tZW0jDN6isXwsVRdwh fWxuaYgXleqLfteDUy6bBwR43NMH8kjN52cXIyG9xNX9a3gWDUdGgwmuXivtr3+ZJ8rnLvlJwnb +efX02rU0puyV+QosluzdLooCovCu64XnDZ6vrxqRPj06EUWjDL68SkPKHQ3ekNwYX1GA+APb0G sxrG54P7Ef8l5JNWVN4rpgMVaoyNGmYLO70Nn/BBt/GoOcxPrR0T8VoFOFZmPe0ECBnmpvsVQCR sdjqsbNhIaNfMgOUwXF/YJzLsA1lzR0V+SDo9v1FyoQFqCXvwEkqDpeZoPZnHz2k+7ejgtkAvh8 L6rxBrlqeM/1xx0J0fCFF54mCTJkZoD0x+DiGdTVzm3bjX6w7AQua19EFBoGTb25W6PQFSqmq3z mdHDbimn5AsAdIw== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 87D6F10001C X-Stat-Signature: weqqw6u1gqqfi67pyywowwxgq4opri47 X-Rspam-User: X-HE-Tag: 1720454039-352803 X-HE-Meta: U2FsdGVkX18LOkWONecKqdGDsfgfQx2TKYJ45GrKHznyWIZSdNOOcUMo8BtUmh50Z62GV+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YdIHfMHLpnw5jrmRDvVVlwxIs7/QtjH+/CQYpvoEIrbYXYZJx6rgD4GKTFINkPbJ/z12GpbTKQDIMDHbL34v1stzDsjMwvGHZVlSL/Fk+C5DglufSdR+LQnRJTIxIuH7eXvioaODeVcvmJxlCMvVdDkZKyg1M9rXcrko3lgqLZUMRLefbewlBoKYjhclJN9DLJOSDR/ZWIHkz1dA== 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..e4f52a9e3c51 --- /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 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.