From patchwork Tue Oct 1 10:59:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13817781 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 DB542CE7D12 for ; Tue, 1 Oct 2024 10:59:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A6E8280078; Tue, 1 Oct 2024 06:59:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 62EA2280068; Tue, 1 Oct 2024 06:59:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 481DF280078; Tue, 1 Oct 2024 06:59:36 -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 1A8EB280068 for ; Tue, 1 Oct 2024 06:59:36 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B40E4405A0 for ; Tue, 1 Oct 2024 10:59:35 +0000 (UTC) X-FDA: 82624737510.01.83A7BED Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id E8F82C000D for ; Tue, 1 Oct 2024 10:59:33 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SqvM7UQu; spf=pass (imf28.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 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=1727780248; 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=Eywsbia8P2KWlMj0rzkujwaUsbnR4BE6MJvg0/1qMj4=; b=HZ7olYsGIWTpeo2p2HFO9cP/4ZBaBkaEnTktxBA2wqnpWWZ+lyslOGevBPjrmjWnwTUOo5 Lc3nI5XY3G0TdkztzDt4RfyGpb1fHCo3Fp0cKNJArcE6v1k85PiouaoLBPqHw/mfQf2RxR 73Z9DMcSkqW6n9u1yMaWM7YHHRON3gw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727780248; a=rsa-sha256; cv=none; b=fNLFGAj0JsTe9InQUxRj+sqM6TLq5zhRlbRSNIj/emmGwTYE2Tvp2pcjyRrByDFVVjBeo2 bx86t1elJqTSOkAPwN9F5Hpq68yvtj1wfoHO1vZ10JD1E5Xpys+6mafbe3cXqpZfNR4QjF Vz59DtUDz+4e4cLJsrx957mjAVaGl5M= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SqvM7UQu; spf=pass (imf28.hostedemail.com: domain of jlayton@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=jlayton@kernel.org; dmarc=pass (policy=quarantine) 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 0300A5C4D08; Tue, 1 Oct 2024 10:59:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3345EC4CED2; Tue, 1 Oct 2024 10:59:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727780372; bh=oPKJkohv3641xKtdl6Qbhb+FHGtw/eR+Am0SFNCb7W8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SqvM7UQu/XzXqr739ZjUMayt09O7fWxBn1UJtQJ8g351BRbl0wANl4R1Yk6sU400E Yr/UGMe49GdD2ebQnt6DzzvUAxSpIZnscsFIdCwabs0Fa4daX+AenIShCE4RLhI3EV w38XZYULiEitHzBZlVn5jhNigejqimT7ax1kuGb14scgnOiO0aLDBzx8dIalhlVG2R L1t/M4IJFUiw+XbLAZNs9OAcRWh+PTLR644uAXZWnJivFkOSp/nSq5IosNG9Vsgix7 81qKUG8BCST6zgyM9YimVCDPFrhZ39NL7WzZEN9swt6eAyU2mbGtVYPD/SpmZp9HQG 2H+AYtehZPAKQ== From: Jeff Layton Date: Tue, 01 Oct 2024 06:59:02 -0400 Subject: [PATCH v8 08/12] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20241001-mgtime-v8-8-903343d91bc3@kernel.org> References: <20241001-mgtime-v8-0-903343d91bc3@kernel.org> In-Reply-To: <20241001-mgtime-v8-0-903343d91bc3@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=7300; i=jlayton@kernel.org; h=from:subject:message-id; bh=oPKJkohv3641xKtdl6Qbhb+FHGtw/eR+Am0SFNCb7W8=; b=kA0DAAgBAA5oQRlWghUByyZiAGb71frI/VZ8IsS6ruTnBs5BWRyAbQA5bsoqMJG8wDD1XAVxb IkCMwQAAQgAHRYhBEvA17JEcbKhhOr10wAOaEEZVoIVBQJm+9X6AAoJEAAOaEEZVoIVsH8P/imV E7s80dTaXxf4ULpYN6X7aZQN/C8GOnFIG+m9PO0VlaOobv+eWULiap2bJb+I/zus8E85zavWgw2 F3cOSJiL0w6d2i/pvNJckkMlroGa9eY9Sn+WDnsyWxDERtp/0VfUgWqmOpWjkKaQprUYXiIKHOT 7892u9SUZxi1sjQXwXRKlBJAsu+2pjJ47/ngqBrAS3XYw6JF6cIfes5YisXQ73TWUDGX33wr1Zv ga4k5NO2Seu+l2F/yOWhgfUgQg9rKh9Vh5fnrmOcVna5fmpk2J3l9sMFO1aLG5VLNvVHKJBdqB5 ky1tvfdWpnMYrpFaQoVtJ96iyMcxAjCk+QIWFjbJGhihWfEQj8OyannWP1hOnXJgQlPiKbdTxtE UUK/KQv1UvZKl6FBtG3jHjRvjkNmqKYc3iOOk9g6RJVTP/tcutVIyMKMR9pP/PxBDmGhbbCCfAM K/xf4E/UXW3Wj6/JWLn6Wie+l7bbJVIcv365ImISGTP2/pUSBSoivUdXMmyEr4Yaw/FbF1aAkto 49mUT8xV/oa8bcnRkEynAZUx5aHQb8VlAsvvQGf2PySLhFQQGOELuVScj4YjskT55adJhrgpSvh n3UCiokQ8gRlaF144PrESWU0berlsMa6+mBSq6fzCT+Y7b9dGyqlRmCG0zV/Qq8vZcpL2iocJhj LcNuR X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Queue-Id: E8F82C000D X-Stat-Signature: xumekcy1dtxwuahaf5pktwc6hrhr14we X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1727780373-969507 X-HE-Meta: U2FsdGVkX188FDUHLI3PbTi4mjA3MeTJGIEEQS71MOSDwOz5QIJIad1etXZB5tRnf0C+LEFgrjenvXp4izOmrkrSNljJgd1FazARfWUSfcWSNqQm+vmi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jsqf2fpsCShArw0fyHsMudQM6nyIsbx3gQVyEQtHdZ9OXuuyX4TVoQoOjPBrVocIhwH66MYhBIII0P/bgMWxmlCtiZCpBzCVPgwtez5upNdudgtS2royHARxDag/6/7oYPUqS47oD6UO3ka0GKFg= 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 --- 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..a150c93471de --- /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, 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. For setattr, it should use setattr_copy() to update the +timestamps, or otherwise mimic its behavior.