Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3761182rdh; Fri, 29 Sep 2023 01:11:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHNaHaZCoTx0RM/o+mhIcRPexcc+8RdMW6s5IrTK6n4TS14xpIqc/DCGacgvmjNLhgvsmRa X-Received: by 2002:a17:903:22ce:b0:1c3:a814:a12b with SMTP id y14-20020a17090322ce00b001c3a814a12bmr5885028plg.16.1695975112396; Fri, 29 Sep 2023 01:11:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695975112; cv=none; d=google.com; s=arc-20160816; b=hguS0ke/6A8XAjm1n5B/2aaL1QbJWVB0DNbs9PCcB0nKLOvaA9+yu+16Z44j6oIfY9 V4WPe0QWLsECS8Hm3yP85GZsV8+lblfe601XbtNzs9MDorXVOYRsHfYXo6b/62p99nDf wzXP0TTshQH4xJAk0xhzxvSJ+eRrCA0xDH+fxmCD+RSzm09yevVDvMan0SCRxe4ntT2t VRPrwLAzWuQCs1fdc+OM1SV+NQgzusnIbb7QpCLlzROxDss7IUKcfYlhZiSyxG/rYiSJ JHXed1diPK+GwDQ8S+yLRSecFnq3MVWipjR/z537LpS2MV07QuattsuN2M0Ew/NFMp7J PCMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=8DfmXp2T2GKuyyceQ+OhxXHG99X5ZXI8F9X8Rh+6G4E=; fh=hT7Tz+Jlg18zhZY+iUP/igRheEJJrO2wqBIgEii9Erc=; b=t+/AUzkPHTx1OoRNLiaiiKUzNmA58CPm9j+LKJG8G7xx5j+K7cZc49wliSZIHOkaa1 fquzGRyppc/YAUaRT7gCOG0zYOxjhzPLXpbL6F1mkT1VQY/T8ZWKOne4ruzsUGjG9yCi h0Uaucpp6kjLtojl8v6U6HN35ANaJnh6+Ky7jRSt0XXzfcMpdePXOITtKqU19q/X9+2S RxkRmEDr3KPEyZYIsdReg9gc0rL0dY0qkkaNpi5hu0ODheupbCfhGdnwD0tmUwKlvWsm XoYdLMg+vbT4GGDBl9hEHDY+7YH7vQ+CDQQ6DGaUrVD7ZLInC68ZFetF9oE4Ie6y7M+d AKaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BsRbBc8p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id s4-20020a170902ea0400b001c58401354dsi22476752plg.565.2023.09.29.01.11.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 01:11:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BsRbBc8p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 415D7851FE14; Thu, 28 Sep 2023 23:35:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232662AbjI2GfO (ORCPT + 99 others); Fri, 29 Sep 2023 02:35:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232666AbjI2GfH (ORCPT ); Fri, 29 Sep 2023 02:35:07 -0400 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 988881A8 for ; Thu, 28 Sep 2023 23:35:05 -0700 (PDT) Received: by mail-qk1-x732.google.com with SMTP id af79cd13be357-77428510fe7so12963885a.1 for ; Thu, 28 Sep 2023 23:35:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695969304; x=1696574104; darn=vger.kernel.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=8DfmXp2T2GKuyyceQ+OhxXHG99X5ZXI8F9X8Rh+6G4E=; b=BsRbBc8p/fCut5uRlcEHFhTkwa76P0nI968Z0jQk69RrcsBl+Ne5J6yEOqqUQpN02g mAmgSiUv75objR7Ne+gRLP4fS0HCc3SV9YVrA3R+iaPLJ510QTzUGlXl0AD1L+j2/qsw GnwHVoXU+kZgXaejhaKgNyg0EAZNF3fnOx020SwOGWRFiqpyufbWD/iZ5JJ9CocWWgUh i44zK4dfsggk60Lg0zOuOFHn35p28bzDNoHFgSpOj7xpfF3aWkT8OyQT5u6INiTpMwJb XMfehxtRTWmzFM029GcDwhSPwHK6G1v7XpMgfxK5oAcg+C/arXpdDc0guUpYxMBV3kX8 TCag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695969304; x=1696574104; 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=8DfmXp2T2GKuyyceQ+OhxXHG99X5ZXI8F9X8Rh+6G4E=; b=ViF/TsqGl/N9c3Xaf153WjbVHDzv7EzSAnQqkpUO/kXAYpV1AtNlZFmLL+9NvWGPGU WwJfw/AQ9B702Oi0Bdy8aH6iBN/wW/TwOpqVqZjebgxuzdvC7m3Xv0ngdKbNPXARHZUn MNBo0XMWu5izPPs4hN2iUxiJNnbgUtQxHV71Zcx9gy6Wk/zhwDVQdlnaIirS2q0HGsqu AUJtsvqvZzz5SKmYDaMJ5uBAH3WpxEdEDHOV0T6BMat0TBeEXR64fDtTt0Sv5lCNg4Fw IgzTyKFsE22tNSsVGKly+Z1Nvuns1ZPIbZs6O2+1CuC/Oo9/Ay5uVtot7XQ1EamsJtLe SpgQ== X-Gm-Message-State: AOJu0Yxvepx+rJLJd0NtjCvhXE25kHEhWXIzoSdkHEhwnkG10gsuOid8 f2VrAx0Ju23FYMdBWZJfYgDVpXsHgjE8M4a3uWRoxw== X-Received: by 2002:a05:620a:4611:b0:762:3567:a64c with SMTP id br17-20020a05620a461100b007623567a64cmr3338763qkb.11.1695969304568; Thu, 28 Sep 2023 23:35:04 -0700 (PDT) MIME-Version: 1.0 References: <20230929023737.1610865-1-maheshb@google.com> In-Reply-To: From: =?UTF-8?B?TWFoZXNoIEJhbmRld2FyICjgpK7gpLngpYfgpLYg4KSs4KSC4KSh4KWH4KS14KS+4KSwKQ==?= Date: Thu, 28 Sep 2023 23:34:37 -0700 Message-ID: Subject: Re: [PATCH 1/4] time: add ktime_get_cycles64() api To: John Stultz Cc: Netdev , Linux , David Miller , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Jonathan Corbet , Don Hatchett , Yuliang Li , Mahesh Bandewar , Thomas Gleixner , Stephen Boyd , Richard Cochran Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 28 Sep 2023 23:35:29 -0700 (PDT) On Thu, Sep 28, 2023 at 10:15=E2=80=AFPM John Stultz w= rote: > > On Thu, Sep 28, 2023 at 7:37=E2=80=AFPM Mahesh Bandewar wrote: > > > > add a method to retrieve raw cycles in the same fashion as there are > > ktime_get_* methods available for supported time-bases. The method > > continues using the 'struct timespec64' since the UAPI uses 'struct > > ptp_clock_time'. > > > > The caller can perform operation equivalent of timespec64_to_ns() to > > retrieve raw-cycles value. The precision loss because of this conversio= n > > should be none for 64 bit cycle counters and nominal at 96 bit counters > > (considering UAPI of s64 + u32 of 'struct ptp_clock_time). > > > > Signed-off-by: Mahesh Bandewar > > CC: John Stultz > > CC: Thomas Gleixner > > CC: Stephen Boyd > > CC: Richard Cochran > > CC: netdev@vger.kernel.org > > CC: linux-kernel@vger.kernel.org > > --- > > include/linux/timekeeping.h | 1 + > > kernel/time/timekeeping.c | 24 ++++++++++++++++++++++++ > > 2 files changed, 25 insertions(+) > > > > diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h > > index fe1e467ba046..5537700ad113 100644 > > --- a/include/linux/timekeeping.h > > +++ b/include/linux/timekeeping.h > > @@ -43,6 +43,7 @@ extern void ktime_get_ts64(struct timespec64 *ts); > > 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); > > +extern void ktime_get_cycles64(struct timespec64 *ts); > > > > void getboottime64(struct timespec64 *ts); > > > > diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c > > index 266d02809dbb..35d603d21bd5 100644 > > --- a/kernel/time/timekeeping.c > > +++ b/kernel/time/timekeeping.c > > @@ -989,6 +989,30 @@ void ktime_get_ts64(struct timespec64 *ts) > > } > > EXPORT_SYMBOL_GPL(ktime_get_ts64); > > > > +/** > > + * ktime_get_cycles64 - get the raw clock cycles in timespec64 format > > + * @ts: pointer to timespec variable > > + * > > + * This function converts the raw clock cycles into timespce64 format > > + * in the varibale pointed to by @ts > > + */ > > +void ktime_get_cycles64(struct timespec64 *ts) > > +{ > > + struct timekeeper *tk =3D &tk_core.timekeeper; > > + unsigned int seq; > > + u64 now; > > + > > + WARN_ON_ONCE(timekeeping_suspended); > > + > > + do { > > + seq =3D read_seqcount_begin(&tk_core.seq); > > + now =3D tk_clock_read(&tk->tkr_mono); > > + } while (read_seqcount_retry(&tk_core.seq, seq)); > > + > > + *ts =3D ns_to_timespec64(now); > > +} > > Hey Mahesh, > Thanks for sending this out. Unfortunately, I'm a bit confused by > this. It might be helpful to further explain what this would be used > for in more detail? > Thanks for looking at this John. I think my cover-letter wasn't sent to all reviewers and that's my mistake. > Some aspects that are particularly unclear: > 1) You seem to be trying to stuff cycle values into a timespec64, > which is not very intuitive (and a type error of sorts). It's not > clear /why/ that type is useful. > The primary idea is to build a PTP API similar to gettimex64() that gives us a sandwich timestamp of a given timebase instead of just sys-time. Since sys-time is disciplined (adjustment / steps), it's not really suitable for all possible use cases. For the same reasons CLOCK_MONOTONIC is also not suitable in a subset of use cases while some do want to use it. So this API gives user a choice to select the timebase. The ioctl() interface uses 'struct ptp_clock_time' (similar to timespec64) hence the interface. > 2) Depending on your clocksource, this would have very strange > wrapping behavior, so I'm not sure it is generally safe to use. > The uapi does provide other alternatives like sys, mono, mono-raw along with raw-cycles and users can choose. > 3) Nit: The interface is called ktime_get_cycles64 (timespec64 > returning interfaces usually are postfixed with ts64). > Ah, thanks for the explanation. I can change to comply with the convention. Does ktime_get_cycles_ts64() make more sense? > I guess could you clarify why you need this instead of using > CLOCK_MONOTONIC_RAW which tries to abstract raw cycles in a way that > is safe and avoids wrapping across various clocksources? > My impression was that CLOCK_MONOTONIC_RAW (as the same suggests) does provide you the raw / undisciplined cycles. However, code like below does show that monotonic-raw is subjected to certain changes. """ int do_adjtimex(struct __kernel_timex *txc) { [...] /* * The timekeeper keeps its own mult values for the currently * active clocksource. These value will be adjusted via NTP * to counteract clock drifting. */ tk->tkr_mono.mult =3D clock->mult; tk->tkr_raw.mult =3D clock->mult; tk->ntp_err_mult =3D 0; tk->skip_second_overflow =3D 0; } """ and that was the reason why I have added raw-cycles as another option. Of course the user can always choose mono-raw if it satisfies their use-case. > thanks > -john