Message ID | 20230929023737.1610865-1-maheshb@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp3738458vqu; Thu, 28 Sep 2023 19:38:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJeYPN9Stod35DLdSgjN3p9j9u63eCcub15kK35U5JO2rcR+RzS6vvv4kvF5etghk9OKM7 X-Received: by 2002:a17:902:e809:b0:1c5:6d04:75ee with SMTP id u9-20020a170902e80900b001c56d0475eemr2999327plg.21.1695955084268; Thu, 28 Sep 2023 19:38:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695955084; cv=none; d=google.com; s=arc-20160816; b=VeEbH1OX3R/ngGQyhbubuDADYQeZxMM03jFvngERCmHJtJWWkYoNLj2jg4BwpeMdzZ TACe4lWprLUsErzWa/jd/A7EnnTJWZNHQCJJHPMw+oUKr3JAAJr3vGiApUUjij5Irzad Trwt6vNEZVT1eGSsZSfn/oWAFG/KFkep7Hp/YKIz8OnlTHKv2XqjEn1BrsMvRugwmizT OKQK7vzkjqDSUtatiCwmEtwN8iF/C3xKXeRJXXToKKJvLS6id+xYp5TE9Y4+zerqdkfO wYoFMhtZMaRQ7fRANGl9bwHeei2fU+2u1CNrmmh0Ob0YlctcuDeBf15sqHjzNDVmnCp8 buMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=iULIiWyEAnuPU2ttI+NrM06ywk49WbVfQ4RXn/AgPAM=; fh=+k6v5Gd7YkIfpyHkdt8+IKxsXqtJvTtVji+AUX4Rj0M=; b=oPyOg7wd961k+GFRhHExJSzp5PionUVO8HprPACF1cSP4Jui9tThwV8EAUczZEvrlT 1cWbZsr7dS6ucSFay8xCsgp+UMXMV6kdwCcfgOsLYzZl7E8d1fYvcmVPt9l6BTdctD2Z s8HAOtldkyQ2RQk9dUBM1yca5+cRsn9N0cS7xq21f70VEpvGMC4oeqe+inn7xvFDCQcZ kluoIrvFvbwp8L1y8h64Q1KuuXeYM+I4MAWGTju7JMhvY+fesYZWc6U/GfEVauNDceul dY5mvTbHBraoEwQyKAZycWj5WOUF+mQMsxSKaZ0vY/nXksFY1HvyL7lvsOsKFumNeQKi 6O+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vFsFeWoJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id km3-20020a17090327c300b001c35864fdbbsi17927415plb.406.2023.09.28.19.38.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 19:38:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vFsFeWoJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id A0BEB83BE805; Thu, 28 Sep 2023 19:37:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230257AbjI2Chr (ORCPT <rfc822;pwkd43@gmail.com> + 21 others); Thu, 28 Sep 2023 22:37:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbjI2Chm (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 28 Sep 2023 22:37:42 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1FDC1A3 for <linux-kernel@vger.kernel.org>; Thu, 28 Sep 2023 19:37:40 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d85fc6261ffso20430729276.1 for <linux-kernel@vger.kernel.org>; Thu, 28 Sep 2023 19:37:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695955060; x=1696559860; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=iULIiWyEAnuPU2ttI+NrM06ywk49WbVfQ4RXn/AgPAM=; b=vFsFeWoJZreuLuRPnNzfBGFbw7QPBxRitWxuF/J/pmWEcfCLcryiN8AYKZ2uWDBcmm sFmNiF00AU5Zwn6N4pj3fZBz7HIFV9yojk/6iYuwsaE2NwTDhMwAtVPeN3wnXESsJoWH N3GDqXUEh4qHIJBi4YBCo9vKiqkd4Uipzj9CmkAbtNpylMc6VHOdB0dynDnxfpdHtx/L UuzmWP95AUZaZZbvOlxgc4kMogParp9EKIymf9ir+bqyiVi9ObypLr0TxhnEy4+A7gXn sqDjBs5iXZwU8ntaT09NF9NOU8VGyBIyRV+8Br+rbfjcCy03Gfbv2LRyayapTHV2Yf9p vHcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695955060; x=1696559860; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=iULIiWyEAnuPU2ttI+NrM06ywk49WbVfQ4RXn/AgPAM=; b=pvmoVc0nHg8TVWpdih6EfqSTHWvJVil7ER6Bdb4qBMttWoOhm8+0BdeEV5zXnBENnX 14rOQ7I+w64g8aC8EdnATMbYQNNaTRuMF6zuN5eMQkF94sBwK4MPiRoAb8bi3NAxwfvk EwGnsIiCsMxWBDAude+Vx6wRYF7YyB5Nb5Mct1YcsClEyGJ5RFiK2pu+HnZHu7byWMb4 eDOya0AQQ1by20gRaM7CfYgDhwIEhiXVrJla5QitW6RAuUm/sOD2qt0iTd8q1QYvRxoC GehmtSxFXPiU/xQER/6UvsPaiL0zogttfmgaTwj7kTtis7LbPFInHObIyPDih2i4sU53 iE5A== X-Gm-Message-State: AOJu0YylcRL/UGQGhUcMDrYtWIJLWv2eRghmavGP4PoiWsNInaDADdax gIav6NVzDCUmZ8B93hQOP55OSGnHpNi4 X-Received: from coldfire.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2b7a]) (user=maheshb job=sendgmr) by 2002:a25:6812:0:b0:d13:856b:c10a with SMTP id d18-20020a256812000000b00d13856bc10amr38603ybc.3.1695955060095; Thu, 28 Sep 2023 19:37:40 -0700 (PDT) Date: Thu, 28 Sep 2023 19:37:37 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <20230929023737.1610865-1-maheshb@google.com> Subject: [PATCH 1/4] time: add ktime_get_cycles64() api From: Mahesh Bandewar <maheshb@google.com> To: Netdev <netdev@vger.kernel.org>, Linux <linux-kernel@vger.kernel.org>, David Miller <davem@davemloft.net>, Jakub Kicinski <kuba@kernel.org>, Eric Dumazet <edumazet@google.com>, Paolo Abeni <pabeni@redhat.com> Cc: Jonathan Corbet <corbet@lwn.net>, Don Hatchett <hatch@google.com>, Yuliang Li <yuliangli@google.com>, Mahesh Bandewar <mahesh@bandewar.net>, Mahesh Bandewar <maheshb@google.com>, John Stultz <jstultz@google.com>, Thomas Gleixner <tglx@linutronix.de>, Stephen Boyd <sboyd@kernel.org>, Richard Cochran <richardcochran@gmail.com> Content-Type: text/plain; charset="UTF-8" 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 groat.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 28 Sep 2023 19:37:59 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778337798008692109 X-GMAIL-MSGID: 1778337798008692109 |
Series |
[1/4] time: add ktime_get_cycles64() api
|
|
Commit Message
Mahesh Bandewar (महेश बंडेवार)
Sept. 29, 2023, 2:37 a.m. UTC
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 conversion
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 <maheshb@google.com>
CC: John Stultz <jstultz@google.com>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: Stephen Boyd <sboyd@kernel.org>
CC: Richard Cochran <richardcochran@gmail.com>
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(+)
Comments
On Thu, Sep 28, 2023 at 7:37 PM Mahesh Bandewar <maheshb@google.com> 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 conversion > 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 <maheshb@google.com> > CC: John Stultz <jstultz@google.com> > CC: Thomas Gleixner <tglx@linutronix.de> > CC: Stephen Boyd <sboyd@kernel.org> > CC: Richard Cochran <richardcochran@gmail.com> > 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 = &tk_core.timekeeper; > + unsigned int seq; > + u64 now; > + > + WARN_ON_ONCE(timekeeping_suspended); > + > + do { > + seq = read_seqcount_begin(&tk_core.seq); > + now = tk_clock_read(&tk->tkr_mono); > + } while (read_seqcount_retry(&tk_core.seq, seq)); > + > + *ts = 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? 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. 2) Depending on your clocksource, this would have very strange wrapping behavior, so I'm not sure it is generally safe to use. 3) Nit: The interface is called ktime_get_cycles64 (timespec64 returning interfaces usually are postfixed with ts64). 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? thanks -john
On Thu, Sep 28, 2023 at 10:15 PM John Stultz <jstultz@google.com> wrote: > > On Thu, Sep 28, 2023 at 7:37 PM Mahesh Bandewar <maheshb@google.com> 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 conversion > > 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 <maheshb@google.com> > > CC: John Stultz <jstultz@google.com> > > CC: Thomas Gleixner <tglx@linutronix.de> > > CC: Stephen Boyd <sboyd@kernel.org> > > CC: Richard Cochran <richardcochran@gmail.com> > > 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 = &tk_core.timekeeper; > > + unsigned int seq; > > + u64 now; > > + > > + WARN_ON_ONCE(timekeeping_suspended); > > + > > + do { > > + seq = read_seqcount_begin(&tk_core.seq); > > + now = tk_clock_read(&tk->tkr_mono); > > + } while (read_seqcount_retry(&tk_core.seq, seq)); > > + > > + *ts = 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 = clock->mult; tk->tkr_raw.mult = clock->mult; tk->ntp_err_mult = 0; tk->skip_second_overflow = 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
On Thu, Sep 28, 2023 at 11:35 PM Mahesh Bandewar (महेश बंडेवार) <maheshb@google.com> wrote: > > On Thu, Sep 28, 2023 at 10:15 PM John Stultz <jstultz@google.com> wrote: > > > > 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. No worries, I was able to find it on lore. Though it looks like your mail threading isn't quite right? > > 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. Sure, but how does userland know if it's safe to use raw cycles? How do we avoid userland applications written against raw cycles from breaking if they run on a different machine? To me this doesn't feel like the interface has been abstracted enough. > > 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? Maybe a little (it at least looks consistent), but not really if you're sticking raw cycles in the timespec :) > > 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) > { > [...] Err. The bit below is from tk_setup_internals() not do_adjtimex(), no? > /* > * 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 = clock->mult; > tk->tkr_raw.mult = clock->mult; > tk->ntp_err_mult = 0; > tk->skip_second_overflow = 0; So the comment is correct, except for the tkr_raw.mult value (I can see how that is confusing). The raw mult is set to the clocksource mult value and should not modified (unless the clocksource changes). > """ > 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. Having raw monotonic as an option seems reasonable to me (as it was introduced to provide a generic abstraction for logic that was using raw TSC values in an unportable way). But the raw cycles interface still worries me, as I want to make sure we're not creating user visible interfaces that expose raw hardware details (making it very difficult to maintain long term). thanks -john
On Thu, Sep 28, 2023 at 11:56 PM John Stultz <jstultz@google.com> wrote: > On Thu, Sep 28, 2023 at 11:35 PM Mahesh Bandewar (महेश बंडेवार) > <maheshb@google.com> wrote: > > On Thu, Sep 28, 2023 at 10:15 PM John Stultz <jstultz@google.com> wrote: > > > 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? > > Maybe a little (it at least looks consistent), but not really if > you're sticking raw cycles in the timespec :) > Despite my concerns that it's a bad idea, If one was going to expose raw cycles from the timekeeping core, I'd suggest doing so directly as a u64 (`u64 ktime_get_cycles(void)`). That may mean widening (or maybe using a union in) your PTP ioctl data structure to have a explicit cycles field. Or introducing a separate ioctl that deals with cycles instead of timespec64s. Squeezing data into types that are canonically used for something else should always be avoided if possible (there are some cases where you're stuck with an existing interface, but that's not the case here). But I still think we should avoid exporting the raw cycle values unless there is some extremely strong argument for it (and if we can, they should be abstracted into some sort of cookie value to avoid userland using it as a raw clock). thanks -john
On Fri, Sep 29, 2023 at 12:06:46AM -0700, John Stultz wrote: > But I still think we should avoid exporting the raw cycle values > unless there is some extremely strong argument for it Looks like the argument was based on a misunderstanding of what CLOCK_MONOTONIC_RAW actually is. So, please, let's not expose the raw cycle counter value. Thanks, Richard
On Fri, Sep 29, 2023 at 12:07 AM John Stultz <jstultz@google.com> wrote: > > On Thu, Sep 28, 2023 at 11:56 PM John Stultz <jstultz@google.com> wrote: > > On Thu, Sep 28, 2023 at 11:35 PM Mahesh Bandewar (महेश बंडेवार) > > <maheshb@google.com> wrote: > > > On Thu, Sep 28, 2023 at 10:15 PM John Stultz <jstultz@google.com> wrote: > > > > 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? > > > > Maybe a little (it at least looks consistent), but not really if > > you're sticking raw cycles in the timespec :) > > > > Despite my concerns that it's a bad idea, If one was going to expose > raw cycles from the timekeeping core, I'd suggest doing so directly as > a u64 (`u64 ktime_get_cycles(void)`). > > That may mean widening (or maybe using a union in) your PTP ioctl data > structure to have a explicit cycles field. > Or introducing a separate ioctl that deals with cycles instead of timespec64s. > > Squeezing data into types that are canonically used for something else > should always be avoided if possible (there are some cases where > you're stuck with an existing interface, but that's not the case > here). > > But I still think we should avoid exporting the raw cycle values > unless there is some extremely strong argument for it (and if we can, > they should be abstracted into some sort of cookie value to avoid > userland using it as a raw clock). > Thanks for the input John. This change is basically to address the API gap and allow it to give a user-given timebase for the sandwich time. I will remove this RAW-CYCLES option for now. If it's deemed necessary, we can always add it later into the same API. > thanks > -john
On Mon, Oct 2, 2023 at 5:13 PM Mahesh Bandewar (महेश बंडेवार) <maheshb@google.com> wrote: > > On Fri, Sep 29, 2023 at 12:07 AM John Stultz <jstultz@google.com> wrote: > > > > On Thu, Sep 28, 2023 at 11:56 PM John Stultz <jstultz@google.com> wrote: > > > On Thu, Sep 28, 2023 at 11:35 PM Mahesh Bandewar (महेश बंडेवार) > > > <maheshb@google.com> wrote: > > > > On Thu, Sep 28, 2023 at 10:15 PM John Stultz <jstultz@google.com> wrote: > > > > > 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? > > > > > > Maybe a little (it at least looks consistent), but not really if > > > you're sticking raw cycles in the timespec :) > > > > > > > Despite my concerns that it's a bad idea, If one was going to expose > > raw cycles from the timekeeping core, I'd suggest doing so directly as > > a u64 (`u64 ktime_get_cycles(void)`). > > > > That may mean widening (or maybe using a union in) your PTP ioctl data > > structure to have a explicit cycles field. > > Or introducing a separate ioctl that deals with cycles instead of timespec64s. > > > > Squeezing data into types that are canonically used for something else > > should always be avoided if possible (there are some cases where > > you're stuck with an existing interface, but that's not the case > > here). > > > > But I still think we should avoid exporting the raw cycle values > > unless there is some extremely strong argument for it (and if we can, > > they should be abstracted into some sort of cookie value to avoid > > userland using it as a raw clock). > > > Thanks for the input John. This change is basically to address the API > gap and allow it to give a user-given timebase for the sandwich time. > I will remove this RAW-CYCLES option for now. If it's deemed > necessary, we can always add it later into the same API. Sounds reasonable to me. thanks -john
On Sat, Sep 30 2023 at 14:15, Richard Cochran wrote: > On Fri, Sep 29, 2023 at 12:06:46AM -0700, John Stultz wrote: >> But I still think we should avoid exporting the raw cycle values >> unless there is some extremely strong argument for it > > Looks like the argument was based on a misunderstanding of what > CLOCK_MONOTONIC_RAW actually is. So, please, let's not expose the raw > cycle counter value. Correct. Exposing the raw counter value is broken if the counter wraps around on a regular base. Thanks, tglx
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 = &tk_core.timekeeper; + unsigned int seq; + u64 now; + + WARN_ON_ONCE(timekeeping_suspended); + + do { + seq = read_seqcount_begin(&tk_core.seq); + now = tk_clock_read(&tk->tkr_mono); + } while (read_seqcount_retry(&tk_core.seq, seq)); + + *ts = ns_to_timespec64(now); +} +EXPORT_SYMBOL_GPL(ktime_get_cycles64); + /** * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC *