Re: [PATCH v5 02/11] timekeeping: Add function to convert realtime to base clock

From: John Stultz
Date: Tue Mar 19 2024 - 18:30:15 EST


On Tue, Mar 19, 2024 at 6:06 AM <lakshmi.sowjanya.d@xxxxxxxxx> wrote:
>
> From: Lakshmi Sowjanya D <lakshmi.sowjanya.d@xxxxxxxxx>
>
> PPS(Pulse Per Second) generates signals in realtime, but Timed IO
> hardware understands time in base clock reference. Add an interface,
> ktime_real_to_base_clock() to convert realtime to base clock.
>
> Convert the base clock to the system clock using convert_base_to_cs() in
> get_device_system_crosststamp().
>
> Add the helper function timekeeping_clocksource_has_base(), to check
> whether the current clocksource has the same base clock. This will be
> used by Timed IO device to check if the base clock is X86_ART(Always
> Running Timer).
>
> Co-developed-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Co-developed-by: Christopher S. Hall <christopher.s.hall@xxxxxxxxx>
> Signed-off-by: Christopher S. Hall <christopher.s.hall@xxxxxxxxx>
> Signed-off-by: Lakshmi Sowjanya D <lakshmi.sowjanya.d@xxxxxxxxx>
> ---
> include/linux/timekeeping.h | 6 +++
> kernel/time/timekeeping.c | 105 +++++++++++++++++++++++++++++++++++-
> 2 files changed, 109 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h
> index 7e50cbd97f86..1b2a4a37bf93 100644
> --- a/include/linux/timekeeping.h
> +++ b/include/linux/timekeeping.h
> @@ -275,12 +275,18 @@ struct system_device_crosststamp {
> * timekeeping code to verify comparability of two cycle values.
> * The default ID, CSID_GENERIC, does not identify a specific
> * clocksource.
> + * @nsecs: @cycles is in nanoseconds.
> */
> struct system_counterval_t {
> u64 cycles;
> enum clocksource_ids cs_id;
> + bool nsecs;

Apologies, this is a bit of an annoying bikeshed request, but maybe
use_nsecs here?
There are plenty of places where nsecs fields hold actual nanoseconds,
so what you have might be easy to misread in the future.

Also, at least in this series, I'm not sure I see where this nsecs
value gets set? Maybe something to split out and add in a separate
patch, where its use is more clear?

> +bool timekeeping_clocksource_has_base(enum clocksource_ids id)
> +{
> + unsigned int seq;
> + bool ret;
> +
> + do {
> + seq = read_seqcount_begin(&tk_core.seq);
> + ret = tk_core.timekeeper.tkr_mono.clock->base ?
> + tk_core.timekeeper.tkr_mono.clock->base->id == id : false;

Again, bikeshed nit: I know folks like ternaries for conciseness, but
once you've crossed a single line, I'd often prefer to read an if
statement.

thanks
-john