From patchwork Wed Oct 2 21:27:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13820400 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 047B1CF8543 for ; Wed, 2 Oct 2024 21:28:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDC2B6B040F; Wed, 2 Oct 2024 17:27:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D822B6B0412; Wed, 2 Oct 2024 17:27:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFC7B6B0411; Wed, 2 Oct 2024 17:27:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8EA676B00F2 for ; Wed, 2 Oct 2024 17:27:57 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 488101C6E70 for ; Wed, 2 Oct 2024 21:27:57 +0000 (UTC) X-FDA: 82629949794.02.E97DDB9 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf22.hostedemail.com (Postfix) with ESMTP id 7F1EEC0009 for ; Wed, 2 Oct 2024 21:27:55 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="tZdd5t/F"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of jlayton@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727904435; a=rsa-sha256; cv=none; b=xffmMfoUQq/7s9ySCu+w5YQjVcw0mtNv7+h9v3Bq+4z1Yu5coTFCYQUVaQDyf9oUJi6UhS j+Ab9tsZrdoezinaF76cJ2wRnKjjgFUoIINYV+m1w7h2F095lBFv/nFJi1ehGQhaJycjbg 8WlZlCxoyJWasVhi/6qgaWN7/09mx8U= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="tZdd5t/F"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of jlayton@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=jlayton@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727904435; 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=8GThB4sgw8WggfBS/JKrNuZ55/RWPHTqoG1qOHnND80=; b=DXsB8SRvkal17FKQiDtL6/P+2BvP+0RU3feWjGFi2uUx3FyKJogaGSTXKUEd2+TzbUVYRz YIwfv9ZObVjLOjlHCNYhlMLsrcrbySf3CCJOaj/oXVime6oiowLSc0VayuTy0qyyBzlNPg ETVxsnhXvBZnvWufrUTgaiLhg1hJaW0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id DEBD1A40BFE; Wed, 2 Oct 2024 21:27:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDD5AC4CED6; Wed, 2 Oct 2024 21:27:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727904473; bh=qBtzxV6C3gNnpXcNznor+GUfZID0zTF7K3hXwvPKJ3M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=tZdd5t/FDA6BUDMKhuyJUMzgrU7Dj+ArevBTpINQ/kUw0bID2yhWKQpKYebxZG9T4 2HlHzHF/uJLRfFA19ndqG3latGrn/u720RlCJbM13CK23otWdgZtsYGMvwyX8ndXZ/ H8igGJd+Yl0+pDHWi/44IMj7dMBiuhLvFzott0kklgeHKAfK+g3pmdflyDlxJOH9sA y36edvjXMAAWajMoebqCmpJp8QVaBZpMdHVODRyfbSppMXtTxwys10nWxxlpiHuZh2 OjTH5zGsziWEHSHh8x/FV255nV5xQkP6RCuEJqa0VcmQfpX79TKxcTXe8XDQZ4pnx0 qZT1ErIW9PRnQ== From: Jeff Layton Date: Wed, 02 Oct 2024 17:27:23 -0400 Subject: [PATCH v10 08/12] Documentation: add a new file documenting multigrain timestamps MIME-Version: 1.0 Message-Id: <20241002-mgtime-v10-8-d1c4717f5284@kernel.org> References: <20241002-mgtime-v10-0-d1c4717f5284@kernel.org> In-Reply-To: <20241002-mgtime-v10-0-d1c4717f5284@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.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=7402; i=jlayton@kernel.org; h=from:subject:message-id; bh=qBtzxV6C3gNnpXcNznor+GUfZID0zTF7K3hXwvPKJ3M=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBm/brA6I1dI8EG/U2Sb1UisGHQiXpPg4GwgYbr8 hjnuAbXthWJAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZv26wAAKCRAADmhBGVaC FV/tD/9X5pKIVIniCMvGql/J6WLFzqLMJ1KN/xNGXflDxvxSCtFhy3Bk+wgy5xigEQNhlpabOFn LQ6qkRRkug8tMq2+YV3w5KKcCNUzqBJg51UTT2r48f95rUtSl+q3B5N79OsZ1HzK7fkCD99+xaO P5DiED3DEtbfEhbsGCRpRyjrpKWqS5dCYV+3ggmpHv5XXtmAJMicDBzW443AVoks3ua0yvhF7/v dpd86poXepdu9zB2qA1wm17Skp5Paejf+WJXi6ilJOoIjZYVmhmzs9p/u/XmvpWtFuOlnGN4Gou Is4gcsCS1Zbpocaf+FWUf4TZDeQ3tCqb7LlmMFHRcO9MWqKJaKiSeqkdlHcs56K5MPzYnvjq1Gl lXJJjYPdgzD6ZldrFYidpzx3sDvknnjR6lvvo7lbI6dU3QPIMx77R8vd5REqp9nuMZKx4xZXVPg V/vtq7O0C+m6q3YCj+PYh0EYBY4I5Q2YBCG6RlOR2pJUdwPZ731ke2R3gPtm9qp+6WBj4rdEymx oj2xDpllQcSOV4ieVhIL2b7GOsIObiy7LKiLmekwwpazRph8l9tHk0krQvvZw8cK93TUpfYK3VC Ll8T6GXHeXnAko/UaPT9+Xd/1TjOj7hVYYRKN+Id8tDiGX9GQPRy5BPJgcW2zD79/af/mEp1NiZ QYYMHRnTwY+vL7A== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Rspam-User: X-Rspamd-Queue-Id: 7F1EEC0009 X-Rspamd-Server: rspam01 X-Stat-Signature: 545yeey4j7ecidjftxfsq5piuwyno144 X-HE-Tag: 1727904475-784037 X-HE-Meta: U2FsdGVkX1/FC3tl+quHSgH+Y161OCS/LlSQpUV5pDIUW5AQCRTq3PLklniVwkf0G81beQnHrdBiDGZExFkpSM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hxyzZzNF//cTczYI7VhiqLk6hNA84G9/ClBHWtCKUJonHHaCN0ETh7TaWGYtOU522DVqP8QTIXafzwfmotv/XjeB5ZCvvLD99WGMOqscyqyiZiQ8xdJrC/5SUmIyC2qjH9IeKHppahTfgo3iFSsjykg1OJ2cpaKbHt0fFvEMlHDav6uHRRLf+TEbofSePAf8755OCfYLl+CnG26gPLBSucNTbsducRs7azM0qD3OPYimH8Hx2YwMgqjkvdC7V6qa//oks6YjI8CHTAiwHo25K02Qt2bDYa12zhcgzaseHh3nHXN8YJsfTfv 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 e8e496d23e1dd5b523889159b464d7adf5d5c30a..44e9e77ffe0d4b9c85f9921190d33dfd21acff8f 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 0000000000000000000000000000000000000000..c779e47284e80f54ad9fc8a6a0b03228dbbf3d59 --- /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.