Re: [PATCH 2/2 v8] printk: Add monotonic, boottime, and realtime timestamps

From: John Stultz
Date: Thu Aug 24 2017 - 14:50:49 EST


On Thu, Aug 24, 2017 at 6:42 AM, Prarit Bhargava <prarit@xxxxxxxxxx> wrote:
> --- a/include/linux/timekeeping.h
> +++ b/include/linux/timekeeping.h
> @@ -239,6 +239,7 @@ static inline u64 ktime_get_raw_ns(void)
> extern u64 ktime_get_mono_fast_ns(void);
> extern u64 ktime_get_raw_fast_ns(void);
> extern u64 ktime_get_boot_fast_ns(void);
> +extern u64 ktime_get_real_offset(void);
>
> /*
> * Timespec interfaces utilizing the ktime based ones
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index fc47863f629c..dd972bc5c88b 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
...
> + * printk_get_real_ns: - Return a realtime timestamp for printk messages
> + * On 32-bit systems selecting the real clock printk timestamp may lead to
> + * unlikely situations where a timestamp is wrong because the real time offset
> + * is read without the protection of a sequence lock.
> + */
> +static u64 printk_get_real_ns(void)
> +{
> + return ktime_get_mono_fast_ns() + ktime_get_real_offset();
> +}
> +
...
> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> index d111039e0245..de07bb5ffef5 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -508,6 +508,11 @@ u64 notrace ktime_get_boot_fast_ns(void)
> }
> EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
>
> +u64 ktime_get_real_offset(void)
> +{
> + return ktime_to_ns(tk_core.timekeeper.offs_real);
> +}
> +

Apologies! One last nit here. So if we're going to export
ktime_get_real_offset(), folks are going to use it, and there is no
documentation about its very sketchy behavioral limits as an
interface, except elsewhere in the printk code.

Instead of doing that, could you export a
__ktime_get_real_fast_ns_unsafe() function, which has its limits
(calculating the realtime w/o locks, thus may return completely bad
values occasionally) clearly documented in the timekeeping code?

You can then use that directly in your printk code, and others who
find it and think "Hey this would be great for my
life-safety-critical-system driver" will be clearly warned.

thanks
-john