From patchwork Sat Sep 14 17:07:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13804447 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 81B91C021A7 for ; Sat, 14 Sep 2024 17:07:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D21636B009A; Sat, 14 Sep 2024 13:07:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CA91C6B009B; Sat, 14 Sep 2024 13:07:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFDF66B009C; Sat, 14 Sep 2024 13:07:44 -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 88EE36B009A for ; Sat, 14 Sep 2024 13:07:44 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 45D65A0463 for ; Sat, 14 Sep 2024 17:07:44 +0000 (UTC) X-FDA: 82563975648.22.FFCA48A Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf17.hostedemail.com (Postfix) with ESMTP id 8C8EE40006 for ; Sat, 14 Sep 2024 17:07:42 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I6jXlekN; spf=pass (imf17.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=1726333555; 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=SiQULGejP9nqJpsq7GnKaT/oTe870RFoP8vBoy4vdC8=; b=Ut3SYgZjkB57Y+UCLBL8IhV66W8GWlZTbhLxt9jQyfy3bEzdiLR/449zVbl6AlcJ7/KVG0 vms85Lhv1ikDQ7ZiGkg8Y79Mi8DgUJYbtUBawSka6riOmDM+9aqSOSvjFwe3briaSpISb8 ZClG+oV/h4+rz0NF+vMzuv98/bafRo0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726333555; a=rsa-sha256; cv=none; b=WbKDuZtCn64zORrhNuqxSOe2FXnMnZad9ZXpWnVvngjVd7Mq+NaauAlN5w2JI8MCaaXjc6 A8bnGL0eK4b5MMN7YkETse3eheFupCHTqnkJVZ3oK0OKWEXT6IqnKJi4dW8TlwtEsl08Jv Da/2vpPRWasrgTiRPrT/mrNXLcOB5WQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=I6jXlekN; spf=pass (imf17.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 1F056A40136; Sat, 14 Sep 2024 17:07:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB481C4CECE; Sat, 14 Sep 2024 17:07:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726333661; bh=/XcZHkK5gYpUwCSVg7s3+A6XUqyCAQD/OQvh4jjSlT8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=I6jXlekNuZGCsp5NBpRYP0R+xcXpVQbTNaK6HMUBUnTRSpt1zMlwP9l28+OQQsA35 qDoXUcSjO+bpgtb+5Fwv7gteU9Nn3R0dTcshB0a9pD2k3fNQQcCNyEnuNQAPrPXCmS 9EPY65VWshQzrPwqy8HHc2coX7lK5lfDWBwZO4fBj5XPwwC/irwcLnoxz9nT+Ceopb obf3x/9tQr5KsfITxF8pANIEFJblk2cNow6RtJQgL5OpANFICZK2eSs5HLhO3srtMS 82TGmM5omjLC8ZyXKmCqqtVN0z4DPt4tz+4+9qZgV/GdqjganAwnPl2l2av7XZxMN5 Emi4PP3ARb5eg== From: Jeff Layton Date: Sat, 14 Sep 2024 13:07:20 -0400 Subject: [PATCH v8 07/11] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20240914-mgtime-v8-7-5bd872330bed@kernel.org> References: <20240914-mgtime-v8-0-5bd872330bed@kernel.org> In-Reply-To: <20240914-mgtime-v8-0-5bd872330bed@kernel.org> To: John Stultz , Thomas Gleixner , Stephen Boyd , Alexander Viro , Christian Brauner , Jan Kara , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , 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: Randy Dunlap , 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=6998; i=jlayton@kernel.org; h=from:subject:message-id; bh=/XcZHkK5gYpUwCSVg7s3+A6XUqyCAQD/OQvh4jjSlT8=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm5cLGJuuSb1xltFHpW0q8a+qitvGMUmowicsfj Rg/e3AZpXiJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZuXCxgAKCRAADmhBGVaC FWVQEADCr1Ju8Vg4w9iEuJQYTT5m4eUvccIiH6Wxe4xu7Bjjl7Esfa5BErRe/5Jzro90FFNT424 rEz4iPtm6aigGIR/RWKMg+HHvguntKiMPVorNF2LSPWDdD323OJYfux0/lhljniTqzGjYNxEJDD AyTSwsd+qaP99+L8AtMBC8VbvyBAM9M2d8I+U82Omh/FxBpuybHvs2KUh8Zqnom/ICx7y3131gx BUbpZN4gCpoP8yyUoB1+I8XnGs3Q4e4xU/3f3VMmHve1xpMr47XZ9aEuEpVQZ/6dyKTSTEkr5pd BW9VllFp5Xq0daBkYHXOVTJVR+5xX+GPkraB/JNTcJBRl8FQXnUXzbXHFottzpRseuCGkLnlhR5 /Oy1FspdD6sUlKvnyALimjvBu6vKZzqJRuYOxHLNI0HYiY3mnBwq8UTouZR515qw64HylfuCHIh tfNxxCNnfr86gANN4QXomTNmAzzBzvWGfD+V48xHvhRE4ESBjfQ1B2NFJyinNOYkBQfrRtVUUXP aCHXIgF0oFoiDt2e0nCQym4YsEt0Wxh5aSNJDdcA0Zs6srKCiIjbA1U7TgGCF+UXQ9QhBLeeAaw iGt7V2f/qfXuyfXskvDFLtFTHw84eXq55hkiuqqgZJ+i4cvgaOQ6/d6+K8qD98nF0fWuHreU9yx EBLPusAlWUknZNQ== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspamd-Queue-Id: 8C8EE40006 X-Stat-Signature: cpe6dogeoizw5nzk6oxauu5ho8g3e694 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1726333662-646241 X-HE-Meta: U2FsdGVkX19N6ticbrHvDTE/cYPNQzp9FqhLy4/wJnOFsfaUzzGTdZOulLQoEtcovgXpEds4Kq9c7hgKpVGsZxEjAz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JzNbo6a/11KZhEYcmxcy8ttHzn1esgOM99LqCE15dve7rW8+m9AYek+rQGP1Mh3JEvdgjc/uQK/mSbgL1H51zxfKlFSeQ1B9Ym62ZwgMydFueoFyE2ZPeG+xvPN45Ye2asqVWTPGbR3SFURQ7Ont0rqQ3YvecwgUyspDgU1a0EjrSIkaIdRi9oJocPs2suB5p2Gg2QcyDajjnAeVaM9VgBFF9BsGCAUjbUXGCruydz/WLZCFkhhwPzdF3PP9vS9OyXPYO2V2+9/Cm0BzkTDsgnqwBiaNPSK0dKQywyBMRxG8jnPoS2HeRQyoQR7ITvw= 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 Signed-off-by: Jeff Layton --- Documentation/filesystems/index.rst | 1 + Documentation/filesystems/multigrain-ts.rst | 121 ++++++++++++++++++++++++++++ 2 files changed, 122 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..97877ab3d933 --- /dev/null +++ b/Documentation/filesystems/multigrain-ts.rst @@ -0,0 +1,121 @@ +.. 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. + +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.