From mboxrd@z Thu Jan 1 00:00:00 1970 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 64842C021AC for ; Sat, 14 Sep 2024 20:11:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A22D06B007B; Sat, 14 Sep 2024 16:11:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D2C66B0082; Sat, 14 Sep 2024 16:11:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 89A866B0083; Sat, 14 Sep 2024 16:11:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6CD516B007B for ; Sat, 14 Sep 2024 16:11:10 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E64271C5085 for ; Sat, 14 Sep 2024 20:11:09 +0000 (UTC) X-FDA: 82564437858.11.49644C8 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) by imf20.hostedemail.com (Postfix) with ESMTP id 0DE6C1C0008 for ; Sat, 14 Sep 2024 20:11:07 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=twb8Wxob; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of jstultz@google.com designates 209.85.218.52 as permitted sender) smtp.mailfrom=jstultz@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726344638; a=rsa-sha256; cv=none; b=j5GmXE004WyCx2nvOD7dDA60Ll6Me0FnyymZk3JMkOKatVYZYO37r/vDKAtMVJ797MFByF rqbxZP1+gqXFwfiiBzBMErsOzoqivEfIkMzG4yBGuSMT5ca0Zl86a/GUXC7mXwtRzh/fO2 vKxq/gfe/r12A6knI5xcGp28FSWHN48= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=twb8Wxob; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of jstultz@google.com designates 209.85.218.52 as permitted sender) smtp.mailfrom=jstultz@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726344638; 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=/6BEqsMQwTfqIOsLh2idDV9ULwQH/It7+hyl6N9v7o0=; b=BXEQBI4aSoLGYMscXqUZSswhltIcPCu341JkK7LzRE6CQIQ9Sw2fgC+KNogGHl5HCsw4b/ AdwQBUgh7fO5gnnHBGNOcX18Y4aBLcKVRCsebhcrvLvg18tXYM0BczBko7ly6sW3h/Tn3Z ictomgyuAH4ZZ7yE9bOyMfSRaXoPTDI= Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a8d43657255so280582666b.0 for ; Sat, 14 Sep 2024 13:11:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726344666; x=1726949466; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=/6BEqsMQwTfqIOsLh2idDV9ULwQH/It7+hyl6N9v7o0=; b=twb8WxobCEubLsyFvn+VdfuHHVlgLYGg47FOeqyjb9WpUxGnX2W8eXbtoTYGuzbzVg MLs70C1hN/zpxmD5fm/USRGlFFveO+W+d9orihbOICWv8Pl/KcLQL19tL0inqY7mmisy JUwBIQsLmoAB+iQgEVccGFnHqFzkUNBqVjtJHdo8IGN/8GrXtu48f7BBHdYhE1tBtkOg f/SvD/fssk252CiBdgmfoAc3P+/9y1xEYAStEyPxDFGdLkBXmjxXtxfZojde5t2GPsYE 2JTivwT4MwqZcYFq3uRCa4lWjMmC/PgXmt/xk7vPImrTwFWGmsgHNZ6fw5GPQ1BwkoVL LpCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726344666; x=1726949466; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/6BEqsMQwTfqIOsLh2idDV9ULwQH/It7+hyl6N9v7o0=; b=pzE3ezOt5L/YYGEYEVo50YwoVUJY76qvc26i3TDSo8p4pa76Ghu86LvBtUhOTpiDMB jpaAtm1MYrCBXhR4ygtdxGhfbYWhIcFepguV9tSIJgZE6Y4+Ok9YeQ+xgxkEngvbX3tf 44LBvnTui4yMU2Kj2ioDJkrBxdm31zlQprQndMCauYDYAyOUWxt5MSA//QbUjmRy4oJk EnY63GPJwjCS5fOMypk8I+/SLUCgALUmYOP9tekPz66M1VHAyw3Zr6+fqoq5hnT4bcJh x98U+kdLzxbrTIKlEdhqbWU/hsRbmLWx7jTzfZre6uWm0tInFb7/9qx7kHe4N0EMxGY1 Tvmg== X-Forwarded-Encrypted: i=1; AJvYcCUgPkfp/tTDLS8b6e4Lpefq5CtUfGhNCm+fxYzlRx6QS/J/Cwc1ZRh4wwYMCVeMkX6YbkJ/Ho1FTA==@kvack.org X-Gm-Message-State: AOJu0YykI0k4MZjKwROdgl/CjMsx4Tzv7AoohPiWkWpNLY7RtdxqoO/1 kqEDXbEHzeGRH513JnETrW5haRuNjttz2UFJ9SgKzkYTGo0XpnQzmtNrsA0WEml0WKsH41t+u+w fLmFi5W8gVxiSxUqUX8lyVNJPte8L8sG+klk= X-Google-Smtp-Source: AGHT+IFs1sXaKM4VOxehK4mNhVRQjd3/gDCwMPvCwIMtQ05vol5GnMxRAUVkjsh8/C1xzMc9AivKp9raF20+AN2HOxc= X-Received: by 2002:a17:907:7da1:b0:a86:96ca:7f54 with SMTP id a640c23a62f3a-a9047ca3bebmr634617166b.21.1726344665517; Sat, 14 Sep 2024 13:11:05 -0700 (PDT) MIME-Version: 1.0 References: <20240914-mgtime-v8-0-5bd872330bed@kernel.org> <20240914-mgtime-v8-1-5bd872330bed@kernel.org> In-Reply-To: <20240914-mgtime-v8-1-5bd872330bed@kernel.org> From: John Stultz Date: Sat, 14 Sep 2024 13:10:54 -0700 Message-ID: Subject: Re: [PATCH v8 01/11] timekeeping: move multigrain timestamp floor handling into timekeeper To: Jeff Layton Cc: 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 , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: 0DE6C1C0008 X-Rspamd-Server: rspam01 X-Stat-Signature: kfwoxxxne1thawj9kc8gu5uuq9co7oxe X-HE-Tag: 1726344667-865638 X-HE-Meta: U2FsdGVkX19qQpQiBLN0EzRH2mMuP2zUxScNUVOQluey6/MqoMXMgKkfo9V3VJLI03caK1a2CcRVeL6DXMIA9SDn1KpTwN8EEfO0+4T9j2Ny+glSWyX9vNrmbeSmNl2/QlVxI/lIc/qZCMU+DPpA0Jookl/hVrKsQLddkc7WfEa7V1D6/y0J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1LBgMsuZ3+lFrqXwUcBWaK+44LHTQ+XDsFGUPO6+HjF5foESGhIeIljBMYd5Okxgjirk4lJXIuLQdW57qrU4= 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: On Sat, Sep 14, 2024 at 10:07=E2=80=AFAM Jeff Layton w= rote: > > For multigrain timestamps, we must keep track of the latest timestamp > that has ever been handed out, and never hand out a coarse time below > that value. > > Add a static singleton atomic64_t into timekeeper.c that we can use to > keep track of the latest fine-grained time ever handed out. This is > tracked as a monotonic ktime_t value to ensure that it isn't affected by > clock jumps. > > Add two new public interfaces: > > - ktime_get_coarse_real_ts64_mg() fills a timespec64 with the later of th= e > coarse-grained clock and the floor time > > - ktime_get_real_ts64_mg() gets the fine-grained clock value, and tries > to swap it into the floor. A timespec64 is filled with the result. > > Since the floor is global, we take great pains to avoid updating it > unless it's absolutely necessary. If we do the cmpxchg and find that the > value has been updated since we fetched it, then we discard the > fine-grained time that was fetched in favor of the recent update. > > To maximize the window of this occurring when multiple tasks are racing > to update the floor, ktime_get_coarse_real_ts64_mg returns a cookie > value that represents the state of the floor tracking word, and > ktime_get_real_ts64_mg accepts a cookie value that it uses as the "old" > value when calling cmpxchg(). This last bit seems out of date. > --- > include/linux/timekeeping.h | 4 +++ > kernel/time/timekeeping.c | 82 +++++++++++++++++++++++++++++++++++++++= ++++++ > 2 files changed, 86 insertions(+) > > diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h > index fc12a9ba2c88..7aa85246c183 100644 > --- a/include/linux/timekeeping.h > +++ b/include/linux/timekeeping.h > @@ -45,6 +45,10 @@ extern void ktime_get_real_ts64(struct timespec64 *tv)= ; > extern void ktime_get_coarse_ts64(struct timespec64 *ts); > extern void ktime_get_coarse_real_ts64(struct timespec64 *ts); > > +/* Multigrain timestamp interfaces */ > +extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts); > +extern void ktime_get_real_ts64_mg(struct timespec64 *ts); > + > void getboottime64(struct timespec64 *ts); > > /* > diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c > index 5391e4167d60..16937242b904 100644 > --- a/kernel/time/timekeeping.c > +++ b/kernel/time/timekeeping.c > @@ -114,6 +114,13 @@ static struct tk_fast tk_fast_raw ____cacheline_ali= gned =3D { > .base[1] =3D FAST_TK_INIT, > }; > > +/* > + * This represents the latest fine-grained time that we have handed out = as a > + * timestamp on the system. Tracked as a monotonic ktime_t, and converte= d to the > + * realtime clock on an as-needed basis. > + */ > +static __cacheline_aligned_in_smp atomic64_t mg_floor; > + > static inline void tk_normalize_xtime(struct timekeeper *tk) > { > while (tk->tkr_mono.xtime_nsec >=3D ((u64)NSEC_PER_SEC << tk->tkr= _mono.shift)) { > @@ -2394,6 +2401,81 @@ void ktime_get_coarse_real_ts64(struct timespec64 = *ts) > } > EXPORT_SYMBOL(ktime_get_coarse_real_ts64); > > +/** > + * ktime_get_coarse_real_ts64_mg - get later of coarse grained time or f= loor > + * @ts: timespec64 to be filled > + * > + * Adjust floor to realtime and compare it to the coarse time. Fill > + * @ts with the latest one. Note that this is a filesystem-specific > + * interface and should be avoided outside of that context. > + */ > +void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts) > +{ > + struct timekeeper *tk =3D &tk_core.timekeeper; > + u64 floor =3D atomic64_read(&mg_floor); > + ktime_t f_real, offset, coarse; > + unsigned int seq; > + > + WARN_ON(timekeeping_suspended); > + > + do { > + seq =3D read_seqcount_begin(&tk_core.seq); > + *ts =3D tk_xtime(tk); > + offset =3D *offsets[TK_OFFS_REAL]; > + } while (read_seqcount_retry(&tk_core.seq, seq)); > + > + coarse =3D timespec64_to_ktime(*ts); > + f_real =3D ktime_add(floor, offset); > + if (ktime_after(f_real, coarse)) > + *ts =3D ktime_to_timespec64(f_real); > +} > +EXPORT_SYMBOL_GPL(ktime_get_coarse_real_ts64_mg); > + > +/** > + * ktime_get_real_ts64_mg - attempt to update floor value and return res= ult > + * @ts: pointer to the timespec to be set > + * > + * Get a current monotonic fine-grained time value and attempt to swap > + * it into the floor. @ts will be filled with the resulting floor value, > + * regardless of the outcome of the swap. Note that this is a filesystem > + * specific interface and should be avoided outside of that context. > + */ > +void ktime_get_real_ts64_mg(struct timespec64 *ts, u64 cookie) Still passing a cookie. It doesn't match the header definition, so I'm surprised this builds. > +{ > + struct timekeeper *tk =3D &tk_core.timekeeper; > + ktime_t old =3D atomic64_read(&mg_floor); > + ktime_t offset, mono; > + unsigned int seq; > + u64 nsecs; > + > + WARN_ON(timekeeping_suspended); > + > + do { > + seq =3D read_seqcount_begin(&tk_core.seq); > + > + ts->tv_sec =3D tk->xtime_sec; > + mono =3D tk->tkr_mono.base; > + nsecs =3D timekeeping_get_ns(&tk->tkr_mono); > + offset =3D *offsets[TK_OFFS_REAL]; > + } while (read_seqcount_retry(&tk_core.seq, seq)); > + > + mono =3D ktime_add_ns(mono, nsecs); > + > + if (atomic64_try_cmpxchg(&mg_floor, &old, mono)) { > + ts->tv_nsec =3D 0; > + timespec64_add_ns(ts, nsecs); > + } else { > + /* > + * Something has changed mg_floor since "old" was > + * fetched. "old" has now been updated with the > + * current value of mg_floor, so use that to return > + * the current coarse floor value. > + */ > + *ts =3D ktime_to_timespec64(ktime_add(old, offset)); > + } > +} > +EXPORT_SYMBOL_GPL(ktime_get_real_ts64_mg); Other than those issues, I'm ok with it. Thanks again for working through my concerns! Since I'm traveling for LPC soon, to save the next cycle, once the fixes above are sorted: Acked-by: John Stultz thanks -john