Skip to contentSkip to conversation

printk, Add a printk.clock kernel parameter

PATCH In review Prarit Bhargava 6 participants 20 messages Jan 6, 2016 – Jan 8, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover printk, Add a printk.clock kernel parameter

Over the past years I've seen many reports of bugs that include time-stamped kernel logs (enabled when CONFIG_PRINTK_TIME=y or print.time=1 is specified as a kernel parameter) that do not align with either external time stamped logs or /var/log/messages.

For example,

[root@intel-wildcatpass-06 ~]# date; echo "Hello!" > /dev/kmsg ; date Thu Dec 17 13:58:31 EST 2015
Thu Dec 17 13:58:31 EST 2015

which displays

[83973.768912] Hello!

on the serial console.

Running a script to convert this to "boot time",

[root@intel-wildcatpass-06 ~]# ./human.sh | tail -1

[Thu Dec 17 13:59:57 2015] Hello!

which is already off by 1 minute and 26 seconds off after ~24 hours of uptime.

This occurs because the time stamp is obtained from a call to local_clock() which (on x86) is a direct call to the hardware. These hardware clock reads are not modified by the standard ntp or ptp protocol, while the other timestamps are, and that results in situations where /var/log/messages timestamps are further and further offset from the hardware clock timestamps.

This patch introduces printk.clock=[local|boot|real|tai] allowing a user to specify an adjusted clock to use with printk timestamps. The hardware clock, or the existing functionality, is preserved by default.

Cc John Stultz <[email protected]>
Cc Xunlei Pang <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Peter Hurley <[email protected]>
Cc Vasily Averin <[email protected]>
Cc Joe Perches <[email protected]>

Prarit Bhargava (2):
kernel, timekeeping, add trylock option to ktime_get_with_offset() printk, Add printk.clock kernel parameter

 include/linux/time64.h      |    2 +
 include/linux/timekeeping.h |   50 +++++++++++++++++++--
 kernel/printk/printk.c      |  105 +++++++++++++++++++++++++++++++++++++++----
 kernel/time/timekeeping.c   |   15 ++++++-
 4 files changed, 159 insertions(+), 13 deletions(-)
Signature · 1 line
1.7.9.3
#1 lore raw

PATCH 1/2 kernel, timekeeping, add trylock option to ktime_get_with_offset()

This is a timekeeping staging patch for the printk() timestamp functionality that adds a trylock option for the timekeeping_lock() to ktime_get_with_offset(). When trylock is 1, calls to ktime_get_with_offset() will return return a ktime of 0 if the timekeeping_lock is locked.

This patch adds ktime_try_real(), ktime_try_boot(), and ktime_try_tai() as wrapper functions around ktime_get_with_offset() with trylock = 1, and modifies other callers to call ktime_get_with_offset() with trylock = 0.

Cc John Stultz <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Xunlei Pang <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Arnd Bergmann <[email protected]>
---
 include/linux/timekeeping.h |   50 +++++++++++++++++++++++++++++++++++++++----
 kernel/time/timekeeping.c   |   15 ++++++++++++-
 2 files changed, 60 insertions(+), 5 deletions(-)
include/linux/timekeeping.h +46 -4
index ec89d84..4f47352 100644
@@ -166,7 +166,7 @@ enum tk_offsets {
 };
 
 extern ktime_t ktime_get(void);
-extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
+extern ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock);
 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
 extern ktime_t ktime_get_raw(void);
 extern u32 ktime_get_resolution_ns(void);
@@ -176,7 +176,16 @@ extern u32 ktime_get_resolution_ns(void);
  */
 static inline ktime_t ktime_get_real(void)
 {
-	return ktime_get_with_offset(TK_OFFS_REAL);
+	return ktime_get_with_offset(TK_OFFS_REAL, 0);
+}
+
+/**
+ * ktime_try_real - same as ktime_get_real, except return 0 if timekeeping is
+ * locked.
+ */
+static inline ktime_t ktime_try_real(void)
+{
+	return ktime_get_with_offset(TK_OFFS_REAL, 1);
 }
 
 /**
@@ -187,7 +196,16 @@ static inline ktime_t ktime_get_real(void)
  */
 static inline ktime_t ktime_get_boottime(void)
 {
-	return ktime_get_with_offset(TK_OFFS_BOOT);
+	return ktime_get_with_offset(TK_OFFS_BOOT, 0);
+}
+
+/**
+ * ktime_try_boottime - same as ktime_get_bootime, except return 0 if
+ * timekeeping is locked.
+ */
+static inline ktime_t ktime_try_boottime(void)
+{
+	return ktime_get_with_offset(TK_OFFS_BOOT, 1);
 }
 
 /**
@@ -195,7 +213,16 @@ static inline ktime_t ktime_get_boottime(void)
  */
 static inline ktime_t ktime_get_clocktai(void)
 {
-	return ktime_get_with_offset(TK_OFFS_TAI);
+	return ktime_get_with_offset(TK_OFFS_TAI, 0);
+}
+
+/**
+ * ktime_try_clocktai - same as ktime_get_clocktai, except return 0 if
+ * timekeeping is locked.
+ */
+static inline ktime_t ktime_try_clocktai(void)
+{
+	return ktime_get_with_offset(TK_OFFS_TAI, 1);
 }
 
 /**
@@ -216,16 +243,31 @@ static inline u64 ktime_get_real_ns(void)
 	return ktime_to_ns(ktime_get_real());
 }
 
+static inline u64 ktime_try_real_ns(void)
+{
+	return ktime_to_ns(ktime_try_real());
+}
+
 static inline u64 ktime_get_boot_ns(void)
 {
 	return ktime_to_ns(ktime_get_boottime());
 }
 
+static inline u64 ktime_try_boot_ns(void)
+{
+	return ktime_to_ns(ktime_try_boottime());
+}
+
 static inline u64 ktime_get_tai_ns(void)
 {
 	return ktime_to_ns(ktime_get_clocktai());
 }
 
+static inline u64 ktime_try_tai_ns(void)
+{
+	return ktime_to_ns(ktime_try_clocktai());
+}
+
 static inline u64 ktime_get_raw_ns(void)
 {
 	return ktime_to_ns(ktime_get_raw());
kernel/time/timekeeping.c +14 -1
index d563c19..6e2cbeb 100644
@@ -44,6 +44,8 @@ static struct {
 static DEFINE_RAW_SPINLOCK(timekeeper_lock);
 static struct timekeeper shadow_timekeeper;
 
+/* printk may call ktime_get_with_offset() before timekeeping is initialized. */
+static int timekeeping_initialized;
 /**
  * struct tk_fast - NMI safe timekeeper
  * @seq:	Sequence counter for protecting updates. The lowest bit
@@ -705,15 +707,22 @@ static ktime_t *offsets[TK_OFFS_MAX] = {
 	[TK_OFFS_TAI]	= &tk_core.timekeeper.offs_tai,
 };
 
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 	unsigned int seq;
 	ktime_t base, *offset = offsets[offs];
 	s64 nsecs;
+	unsigned long flags = 0;
+
+	if (unlikely(!timekeeping_initialized))
+		return ktime_set(0, 0);
 
 	WARN_ON(timekeeping_suspended);
 
+	if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+		return ktime_set(KTIME_MAX, 0);
+
 	do {
 		seq = read_seqcount_begin(&tk_core.seq);
 		base = ktime_add(tk->tkr_mono.base, *offset);
@@ -721,6 +730,9 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs)
 
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 
+	if (trylock)
+		raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
+
 	return ktime_add_ns(base, nsecs);
 
 }
@@ -1255,6 +1267,7 @@ void __init timekeeping_init(void)
 
 	write_seqcount_end(&tk_core.seq);
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
+	timekeeping_initialized = 1;
 }
 
 /* time in seconds when suspend began for persistent clock */
Signature · 1 line
1.7.9.3
#2 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 7 lines · Prarit Bhargava
kernel/time/timekeeping.c +1 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 	unsigned int seq;
 	ktime_t base, *offset = offsets[offs];
 	s64 nsecs;

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 725

Quoted 10 lines
kernel/time/timekeeping.c +7 -0
+	unsigned long flags = 0;
+
+	if (unlikely(!timekeeping_initialized))
+		return ktime_set(0, 0);
 
 	WARN_ON(timekeeping_suspended);
 
+	if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+		return ktime_set(KTIME_MAX, 0);
+

Are you trying to avoid a deadlock caused by calling printk() with timekeeper_lock locked?

I believe this is already unsafe, as explained in the commit log of 6d9bcb62 (timekeeping: use printk_deferred when holding timekeeping seqlock).

So directly calling ktime_get() from printk would just turn a rare deadlock into a certain one - perhaps a good thing?

Signature · 2 lines
Jiri Bohac <[email protected]>
SUSE Labs, SUSE CZ
#3 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 13 lines · Jiri Bohac
Quoted 12 lines · Prarit Bhargava
kernel/time/timekeeping.c +5 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 	unsigned int seq;
 	ktime_t base, *offset = offsets[offs];
 	s64 nsecs;
+	unsigned long flags = 0;
+
+	if (unlikely(!timekeeping_initialized))
+		return ktime_set(0, 0);
Quoted 8 lines

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 725

Quoted 5 lines
kernel/time/timekeeping.c +3 -0
 	WARN_ON(timekeeping_suspended);
 
+	if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+		return ktime_set(KTIME_MAX, 0);
+

Are you trying to avoid a deadlock caused by calling printk() with timekeeper_lock locked?

Not exactly. When I initially sent this as a RFE to jstultz he pointed out that if CPU A had acquired the timekeeper_lock (and therefore incremented tk_core.seq for a write), and CPU B panicked, no output would occur because the reads of tk_core.seq would spin indefinitely.

Quoted 4 lines

I believe this is already unsafe, as explained in the commit log of 6d9bcb62 (timekeeping: use printk_deferred when holding timekeeping seqlock).

Hmm ... John Stultz, any suggestions here?

P.

Quoted 5 lines

So directly calling ktime_get() from printk would just turn a rare deadlock into a certain one - perhaps a good thing?

maintainer
#4 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

In reply to Prarit Bhargava
Quoted 5 lines · Prarit Bhargava

This is a timekeeping staging patch for the printk() timestamp functionality that adds a trylock option for the timekeeping_lock() to ktime_get_with_offset(). When trylock is 1, calls to ktime_get_with_offset() will return return a ktime of 0 if the timekeeping_lock is locked.

If I get it correctly, it returns 0 when timekeeping is not initialized. But it returns TIME_MAX_NS when the lock is taken. Where TIME_MAX_NS is defined in the 2nd patch.

Quoted 25 lines

This patch adds ktime_try_real(), ktime_try_boot(), and ktime_try_tai() as wrapper functions around ktime_get_with_offset() with trylock = 1, and modifies other callers to call ktime_get_with_offset() with trylock = 0.

---
 include/linux/timekeeping.h |   50 +++++++++++++++++++++++++++++++++++++++----
 kernel/time/timekeeping.c   |   15 ++++++++++++-
 2 files changed, 60 insertions(+), 5 deletions(-)
include/linux/timekeeping.h +1 -1
index ec89d84..4f47352 100644
@@ -166,7 +166,7 @@ enum tk_offsets {
 };
 
 extern ktime_t ktime_get(void);
-extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
+extern ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock);
 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
 extern ktime_t ktime_get_raw(void);
 extern u32 ktime_get_resolution_ns(void);
@@ -176,7 +176,16 @@ extern u32 ktime_get_resolution_ns(void);
  */
 static inline ktime_t ktime_get_real(void)

include/linux/timekeeping.h extern u32 ktime_get_resolution_ns(void); · line 186

Quoted 10 lines
include/linux/timekeeping.h +8 -1
 {
-	return ktime_get_with_offset(TK_OFFS_REAL);
+	return ktime_get_with_offset(TK_OFFS_REAL, 0);
+}
+
+/**
+ * ktime_try_real - same as ktime_get_real, except return 0 if timekeeping is
+ * locked.
+ */
+static inline ktime_t ktime_try_real(void)

I would call this ktime_try_get_real() to make it clear.

Quoted 4 lines
include/linux/timekeeping.h +2 -0
+{
+	return ktime_get_with_offset(TK_OFFS_REAL, 1);
 }
 

[...]

Quoted 29 lines

static inline u64 ktime_get_raw_ns(void)
{

include/linux/timekeeping.h +0 -0
 	return ktime_to_ns(ktime_get_raw());
kernel/time/timekeeping.c +5 -1
index d563c19..6e2cbeb 100644
@@ -44,6 +44,8 @@ static struct {
 static DEFINE_RAW_SPINLOCK(timekeeper_lock);
 static struct timekeeper shadow_timekeeper;
 
+/* printk may call ktime_get_with_offset() before timekeeping is initialized. */
+static int timekeeping_initialized;
 /**
  * struct tk_fast - NMI safe timekeeper
  * @seq:	Sequence counter for protecting updates. The lowest bit
@@ -705,15 +707,22 @@ static ktime_t *offsets[TK_OFFS_MAX] = {
 	[TK_OFFS_TAI]	= &tk_core.timekeeper.offs_tai,
 };
 
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
 	unsigned int seq;
 	ktime_t base, *offset = offsets[offs];
 	s64 nsecs;
+	unsigned long flags = 0;
+

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 727

Quoted 10 lines
kernel/time/timekeeping.c +5 -0
+	if (unlikely(!timekeeping_initialized))
+		return ktime_set(0, 0);
 
 
 
 	WARN_ON(timekeeping_suspended);
 
+	if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+		return ktime_set(KTIME_MAX, 0);
+

I guess that you want to avoid a deadlock with this. I mean that you want to survive when you call, for example, ktime_try_tai_ns() from inside timekeeping_set_tai_offset(). Am I right?

One problem is that it will fail even when the lock is taken from another CPU and the deadlock is not real. It probably is not a big issue for printk() because currently used local_clock() is far from perfect but...

Another problem is that it will block writers. This might be solved if you try only one while cycle instead of taking the lock. I mean to do something like:

ktime_t ktime_get_with_offset(enum tk_offsets offs, int try_once)

{
	struct timekeeper *tk = &tk_core.timekeeper;
	unsigned int seq;
	int retry;
	ktime_t base, *offset = offsets[offs];
	s64 nsecs;

	WARN_ON(timekeeping_suspended);

	do {
		seq = read_seqcount_begin(&tk_core.seq);
		base = ktime_add(tk->tkr_mono.base, *offset);
		nsecs = timekeeping_get_ns(&tk->tkr_mono);
		retry = read_seqcount_retry(&tk_core.seq, seq));
	} while (retry && !try_once);

	if (try_once && retry)
		return ktime_set(KTIME_MAX, 0);

	return ktime_add_ns(base, nsecs);

}

Another question is if you really need to distinguish between non-initialized and locked state. You might always return zero time if you do not know. It will things easier.

kernel/time/timekeeping.c ktime_t ktime_get_with_offset(enum tk_offsets offs) · line 737

Quoted 12 lines
kernel/time/timekeeping.c +0 -0
 	do {
kernel/time/timekeeping.c +3 -0
 		seq = read_seqcount_begin(&tk_core.seq);
 		base = ktime_add(tk->tkr_mono.base, *offset);
@@ -721,6 +730,9 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs)
 
 	} while (read_seqcount_retry(&tk_core.seq, seq));
 
+	if (trylock)
+		raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
+
 	return ktime_add_ns(base, nsecs);
 

Best Regards,
Petr

#5 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

In reply to Prarit Bhargava
Quoted 6 lines · Prarit Bhargava
kernel/time/timekeeping.c +1 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 10 lines
kernel/time/timekeeping.c +6 -0
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
 
        WARN_ON(timekeeping_suspended);
 
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

kernel/time/timekeeping.c ktime_t ktime_get_with_offset(enum tk_offsets offs) · line 731

Quoted 7 lines
kernel/time/timekeeping.c +1 -0
+
        do {
                seq = read_seqcount_begin(&tk_core.seq);
                base = ktime_add(tk->tkr_mono.base, *offset);
@@ -721,6 +730,9 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs)
 
        } while (read_seqcount_retry(&tk_core.seq, seq));

So instead of the do/while() loop above... Something closer to:

int __ktime_get_with_offset(enum tk_offsets offs, ktime_t* base, s64 *nsec)

{
       unsigned int seq;

       seq = read_seqcount_begin(&tk_core.seq);
       *base = ktime_add(tk->tkr_mono.base, *offset);
       *nsecs = timekeeping_get_ns(&tk->tkr_mono);

       return read_seqcount_retry(&tk_core.seq, seq);
}

Then ktime_get_with_offset() would just call:

      ...
      while(__ktime_get_with_offset(offs, &base, &nsecs))
                          /*spin*/;
      return ktime_add_ns(base,nsecs);

Then you add a simple:
int ktime_try_get_with_offset(enum tk_offsets offs, ktime_t* ret) {
...

    if (__ktime_get_with_offset(offs, &base, &nsecs))
           return -EAGAIN;
    *ret = ktime_add_ns(base,nsecs);
     return 0;
}

thanks
-john

#6 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 13 lines · John Stultz
Quoted 12 lines · Prarit Bhargava
kernel/time/timekeeping.c +5 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
Quoted 6 lines

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 4 lines
        WARN_ON(timekeeping_suspended);
kernel/time/timekeeping.c +2 -0
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

Only for writing.. sorry.. still drinking my coffee.

Quoted 3 lines

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

And more clearly here, if a cpu takes a write on the seqcounter in update_wall_time() and at that point another cpu hits a bug, and IPIs the cpus, the system would deadlock. That's really what I want to avoid.

thanks
-john

#7 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 23 lines · John Stultz
Quoted 13 lines · John Stultz
Quoted 12 lines · Prarit Bhargava
kernel/time/timekeeping.c +5 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
Quoted 6 lines

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 4 lines
        WARN_ON(timekeeping_suspended);
kernel/time/timekeeping.c +2 -0
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

Only for writing.. sorry.. still drinking my coffee.

Quoted 8 lines
Quoted 3 lines

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

And more clearly here, if a cpu takes a write on the seqcounter in update_wall_time() and at that point another cpu hits a bug, and IPIs the cpus, the system would deadlock. That's really what I want to avoid.

Right -- but the only time that the seq_lock is taken for writing is when the timekeeper_lock is acquired (including update_wall_time()). This means that

if (!raw_spin_trylock_irqsave(&timekeeper_lock, flags))

is equivalent to

if (tk_core.seq & 1) // sequence_t is odd when writing

The problem with the latter is that it is possible that there is no protection from a writer setting tk_core.seq odd AFTER I've read it, and the protection for that AFAICT comes from the timekeeper_lock.

That means I need to check to see if the timekeeper_lock is locked. And the patch does exactly that -- checks to see if the lock is available, and if not avoids spinning on the seq_lock.

P.

Quoted 4 lines

thanks
-john

#8 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 42 lines · Prarit Bhargava
Quoted 23 lines · John Stultz
Quoted 13 lines · John Stultz
Quoted 12 lines · Prarit Bhargava
kernel/time/timekeeping.c +5 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
Quoted 6 lines

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 4 lines
        WARN_ON(timekeeping_suspended);
kernel/time/timekeeping.c +2 -0
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

Only for writing.. sorry.. still drinking my coffee.

Quoted 8 lines
Quoted 3 lines

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

And more clearly here, if a cpu takes a write on the seqcounter in update_wall_time() and at that point another cpu hits a bug, and IPIs the cpus, the system would deadlock. That's really what I want to avoid.

Right -- but the only time that the seq_lock is taken for writing is when the timekeeper_lock is acquired (including update_wall_time()). This means that

if (!raw_spin_trylock_irqsave(&timekeeper_lock, flags))

is equivalent to

if (tk_core.seq & 1) // sequence_t is odd when writing

Quoted 7 lines

The problem with the latter is that it is possible that there is no protection from a writer setting tk_core.seq odd AFTER I've read it, and the protection for that AFAICT comes from the timekeeper_lock.

That means I need to check to see if the timekeeper_lock is locked. And the patch does exactly that -- checks to see if the lock is available, and if not avoids spinning on the seq_lock.

And no, we don't want that in every code path.

We already have the concept of the fast timekeeper, which is lockless and NMI safe. It's useable for tracing and perf, so it can be used for printk as well.

It supports clock monotonic today, which is good enough for printk, but it could be extended to other clocks if really required.

Thanks,

tglx

#9 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

In reply to John Stultz
Quoted 15 lines · John Stultz
Quoted 14 lines · Prarit Bhargava
kernel/time/timekeeping.c +5 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
 
        WARN_ON(timekeeping_suspended);
Quoted 8 lines

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 2 lines
kernel/time/timekeeping.c +2 -0
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

We could also extend the fast timekeeper with boot/real/tai extensions and use that for printk. You can use ktime_get_mono_fast_ns() today.

Thanks,

tglx

#10 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 21 lines · Thomas Gleixner
Quoted 20 lines · John Stultz
Quoted 6 lines · Prarit Bhargava
kernel/time/timekeeping.c +1 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 10 lines
kernel/time/timekeeping.c +6 -0
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
 
        WARN_ON(timekeeping_suspended);
 
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

Quoted 7 lines
Quoted 3 lines

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

We could also extend the fast timekeeper with boot/real/tai extensions and use that for printk. You can use ktime_get_mono_fast_ns() today.

Thanks tglx -- I thought about doing that but was put off by the comments in __ktime_get_fast_ns() which point out that we could see backwards time stamps. But I see your point -- I could do the same "last_time_stamp" check and use "??" in the output.

That's a far better approach here, and unless John has any objections I'll go with that.

P.

Quoted 4 lines

Thanks,

tglx

#11 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 26 lines · Prarit Bhargava
Quoted 25 lines · Thomas Gleixner
Quoted 23 lines · John Stultz
Quoted 6 lines · Prarit Bhargava
kernel/time/timekeeping.c +1 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 10 lines
kernel/time/timekeeping.c +6 -0
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
 
        WARN_ON(timekeeping_suspended);
 
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

Quoted 8 lines
Quoted 3 lines

We could also extend the fast timekeeper with boot/real/tai extensions and use that for printk. You can use ktime_get_mono_fast_ns() today.

Thanks tglx -- I thought about doing that but was put off by the comments in __ktime_get_fast_ns() which point out that we could see backwards time stamps. But I see your point -- I could do the same "last_time_stamp" check and use "??" in the output.

We talk about single digit nanoseconds here and in the case of a crash/bug we really do not care about that at all.

Thanks,

tglx

#12 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

In reply to Thomas Gleixner
Quoted 21 lines · Thomas Gleixner
Quoted 20 lines · John Stultz
Quoted 6 lines · Prarit Bhargava
kernel/time/timekeeping.c +1 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 10 lines
kernel/time/timekeeping.c +6 -0
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
 
        WARN_ON(timekeeping_suspended);
 
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

Quoted 6 lines
Quoted 3 lines

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

We could also extend the fast timekeeper with boot/real/tai extensions and use that for printk. You can use ktime_get_mono_fast_ns() today.

Ack. There'd be a chance for odd values around when the time is set, but for debug printks I think its not critical.

thanks
-john

#13 lore raw

Re: [PATCH 1/2] kernel, timekeeping, add trylock option to ktime_get_with_offset()

Quoted 26 lines · John Stultz
Quoted 25 lines · Thomas Gleixner
Quoted 23 lines · John Stultz
Quoted 6 lines · Prarit Bhargava
kernel/time/timekeeping.c +1 -1
-ktime_t ktime_get_with_offset(enum tk_offsets offs)
+ktime_t ktime_get_with_offset(enum tk_offsets offs, int trylock)
 {
        struct timekeeper *tk = &tk_core.timekeeper;
        unsigned int seq;
        ktime_t base, *offset = offsets[offs];

kernel/time/timekeeping.c static ktime_t *offsets[TK_OFFS_MAX] = { · line 724

Quoted 10 lines
kernel/time/timekeeping.c +6 -0
        s64 nsecs;
+       unsigned long flags = 0;
+
+       if (unlikely(!timekeeping_initialized))
+               return ktime_set(0, 0);
 
        WARN_ON(timekeeping_suspended);
 
+       if (trylock && !raw_spin_trylock_irqsave(&timekeeper_lock, flags))
+               return ktime_set(KTIME_MAX, 0);

Wait.. this doesn't make sense. The timekeeper lock is only for reading.

What I was suggesting to you off line is to have something that avoids spinning on the seqcounter should if a bug occurs and we IPI all the cpus, that we don't deadlock or block any printk messages.

Quoted 5 lines
Quoted 2 lines

We could also extend the fast timekeeper with boot/real/tai extensions and use that for printk. You can use ktime_get_mono_fast_ns() today.

Ack. There'd be a chance for odd values around when the time is set, but for debug printks I think its not critical.

I'll convert to this in v2.

Thanks for the input everyone!

P.

Quoted 4 lines

thanks
-john

#14 lore raw

PATCH 2/2 printk, Add printk.clock kernel parameter

In reply to Prarit Bhargava

The script used in the analysis below:

dmesg_with_human_timestamps () {
    $(type -P dmesg) "$@" | perl -w -e 'use strict;
        my ($uptime) = do { local @ARGV="/proc/uptime";<>}; ($uptime) = ($uptime =~ /^(\d+)\./);
        foreach my $line (<>) {
            printf( ($line=~/^\[\s*(\d+)\.\d+\](.+)/) ? ( "[%s]%s\n", scalar localtime(time - $uptime + $1), $2 ) : $line )
        }'
}

dmesg_with_human_timestamps

----8<----

Over the past years I've seen many reports of bugs that include time-stamped kernel logs (enabled when CONFIG_PRINTK_TIME=y or print.time=1 is specified as a kernel parameter) that do not align with either external time stamped logs or /var/log/messages.

For example,

[root@intel-wildcatpass-06 ~]# date; echo "Hello!" > /dev/kmsg ; date Thu Dec 17 13:58:31 EST 2015
Thu Dec 17 13:58:31 EST 2015

which displays

[83973.768912] Hello!

on the serial console.

Running a script to convert this to "boot time",

[root@intel-wildcatpass-06 ~]# ./human.sh | tail -1

[Thu Dec 17 13:59:57 2015] Hello!

which is already off by 1 minute and 26 seconds off after ~24 hours of uptime.

This occurs because the time stamp is obtained from a call to local_clock() which (on x86) is a direct call to the hardware. These hardware clock reads are not modified by the standard ntp or ptp protocol, while the other timestamps are, and that results in situations where /var/log/messages timestamps are further and further offset from the hardware clock timestamps.

This patch introduces printk.clock=[local|boot|real|tai] allowing a user to specify an adjusted clock to use with printk timestamps. The hardware clock, or the existing functionality, is preserved by default.

If the timekeeper_lock (used to protect writes from reads) is under contention, the last known good time is output again and "??" is used as the last two digits of time. This avoids contention issues with a CPU panicking while another CPU is writing time data.

Cc John Stultz <[email protected]>
Cc Xunlei Pang <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Peter Hurley <[email protected]>
Cc Vasily Averin <[email protected]>
Cc Joe Perches <[email protected]>
---
 include/linux/time64.h |    2 +
 kernel/printk/printk.c |  105 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 99 insertions(+), 8 deletions(-)
include/linux/time64.h +2 -0
index 367d5af..f4a35d9 100644
@@ -39,6 +39,8 @@ struct itimerspec64 {
 #define TIME64_MAX			((s64)~((u64)1 << 63))
 #define KTIME_MAX			((s64)~((u64)1 << 63))
 #define KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
+/* Maximum value returned by ktime_to_ns() */
+#define KTIME_MAX_NS 			((u64)((U64_MAX>>1)))
 
 #if __BITS_PER_LONG == 64
 
kernel/printk/printk.c +97 -8

135 lines not on this page · raw message

Signature · 1 line
1.7.9.3
maintainer
#15 lore raw

Re: [PATCH 2/2] printk, Add printk.clock kernel parameter

Quoted 19 lines · Prarit Bhargava

This patch introduces printk.clock=[local|boot|real|tai] allowing a user to specify an adjusted clock to use with printk timestamps. The hardware clock, or the existing functionality, is preserved by default.

If the timekeeper_lock (used to protect writes from reads) is under contention, the last known good time is output again and "??" is used as the last two digits of time. This avoids contention issues with a CPU panicking while another CPU is writing time data.

---
 include/linux/time64.h |    2 +
 kernel/printk/printk.c |  105 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 99 insertions(+), 8 deletions(-)
include/linux/time64.h +0 -0
index 367d5af..f4a35d9 100644

include/linux/time64.h struct itimerspec64 { · line 43

Quoted 5 lines
include/linux/time64.h +2 -0
@@ -39,6 +39,8 @@ struct itimerspec64 {
 #define TIME64_MAX			((s64)~((u64)1 << 63))
 #define KTIME_MAX			((s64)~((u64)1 << 63))
 #define KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
+/* Maximum value returned by ktime_to_ns() */
+#define KTIME_MAX_NS 			((u64)((U64_MAX>>1)))

Please, use KTIME_NS_MAX to use the same naming scheme as KTIME_SEC_MAX.

Well, I would return 0 when the time is not known. See also below.

Quoted 14 lines
include/linux/time64.h +0 -0
 #if __BITS_PER_LONG == 64
 
kernel/printk/printk.c +4 -0
index 2ce8826..ffc05ee 100644
@@ -419,6 +419,83 @@ static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
 	return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
 }
 
+static u64 printk_try_real_ns(void)
+{
+	return ktime_try_real_ns();
+}

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 435

Quoted 10 lines
kernel/printk/printk.c +10 -0
+
+static u64 printk_try_boot_ns(void)
+{
+	return ktime_try_boot_ns();
+}
+
+static u64 printk_try_tai_ns(void)
+{
+	return ktime_try_tai_ns();
+}

Do we need these wrappers? Why not using ktime_try_xxx_ns() directly? You already use local_clock().

Quoted 25 lines
kernel/printk/printk.c +25 -0
+static int printk_clock_param_set(const char *val,
+				  const struct kernel_param *kp)
+{
+	char *printk_clock_new = strstrip((char *)val);
+	int ret;
+
+	/* check if change is needed */
+	if (!strcmp(printk_clock_new, printk_clock))
+		return 0;
+
+	if (!strncmp("local", printk_clock_new, 5)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &local_clock;
+	} else if (!strncmp("real", printk_clock_new, 4)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &printk_try_real_ns;
+	} else if (!strncmp("boot", printk_clock_new, 4)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &printk_try_boot_ns;

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 485

Quoted 10 lines
kernel/printk/printk.c +10 -0
+	} else if (!strncmp("tai", printk_clock_new, 3)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &printk_try_tai_ns;
+	}
+
+	/* Did the clock change ? */
+	if (strcmp(printk_clock_new, printk_clock))
+		return -EINVAL;

I wonder if we need this extra check. It might be enough to print the error message in the final "else" above.

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 497

Quoted 11 lines
kernel/printk/printk.c +11 -0
+	pr_info("printk: timestamp set to %s clock.\n", printk_clock);
+	return 0;
+}
+
+static struct kernel_param_ops printk_clock_param_ops = {
+	.set =		printk_clock_param_set,
+	.get =		param_get_charp,
+};
+
+module_param_cb(clock, &printk_clock_param_ops, &printk_clock, 0644);
+MODULE_PARM_DESC(clock, "Clock used for printk timestamps.  Can be local (hardware/default), boot, real, or tai.\n");

I have problems to parse this. It seems that the most common style used for possible values is to putthem into brackets. I wonder if this is better readable.

MODULE_PARM_DESC(clock, "Clock used for printk timestamps (local = default, boot, real, tai).");

kernel/printk/printk.c static int log_store(int facility, int level, · line 547

Quoted 10 lines
kernel/printk/printk.c +2 -1
+
 /* insert record into the buffer, discard old ones, update heads */
 static int log_store(int facility, int level,
 		     enum log_flags flags, u64 ts_nsec,
@@ -467,7 +544,7 @@ static int log_store(int facility, int level,
 	if (ts_nsec > 0)
 		msg->ts_nsec = ts_nsec;
 	else
-		msg->ts_nsec = local_clock();
+		msg->ts_nsec = printk_get_timestamp();

Hmm, one problem is that each clock returns another type of time. "local" and "boot" clocks returns the number of ns since the boot. While "real" and "tai" clocks returns the number of ns since 1.1.1970 or so.

The tools reading the timestamps are confused by this. For example, I get this:

$> echo boot >/sys/module/printk/parameters/clock $> echo local >/sys/module/printk/parameters/clock $> echo real >/sys/module/printk/parameters/clock $> echo tai >/sys/module/printk/parameters/clock $> dmesg | tail -6

[    6.976593] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[    6.977168] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[   77.500483] printk: timestamp set to boot clock.
[   81.419957] printk: timestamp set to local clock.
[1452177961.544909] printk: timestamp set to real clock.
[1452177965.224824] printk: timestamp set to tai clock.
$> dmesg -T | tail -6
[Thu Jan  7 15:44:41 2016] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[Thu Jan  7 15:44:41 2016] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[Thu Jan  7 15:45:52 2016] printk: timestamp set to boot clock.
[Thu Jan  7 15:45:56 2016] printk: timestamp set to local clock.
[Fri Jan 13 06:30:36 2062] printk: timestamp set to real clock.
[Fri Jan 13 06:30:40 2062] printk: timestamp set to tai clock.

Please, note that the last messages looks as they are printed in a far far future ;-)

Quoted 27 lines
kernel/printk/printk.c +11 -4
 	memset(log_dict(msg) + dict_len, 0, pad_len);
 	msg->len = size;
 
@@ -1039,18 +1116,30 @@ module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
 
 static size_t print_time(u64 ts, char *buf)
 {
-	unsigned long rem_nsec;
+	unsigned long rem;
+	size_t size;
 
 	if (!printk_time)
 		return 0;
 
-	rem_nsec = do_div(ts, 1000000000);
+	if (ts == KTIME_MAX_NS)
+		ts = last_time_stamp;
 
-	if (!buf)
-		return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
+	if (buf) {
+		size = INT_MAX;
+		rem = do_div(ts, 1000000000) / 1000;
+	} else {
+		size = 0;
+		rem = 0;
+	}

kernel/printk/printk.c module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); · line 1142

Quoted 10 lines
kernel/printk/printk.c +7 -2
 
-	return sprintf(buf, "[%5lu.%06lu] ",
-		       (unsigned long)ts, rem_nsec / 1000);
+	if (ts != KTIME_MAX_NS) {
+		return snprintf(buf, size, "[%5lu.%06lu] ",
+			       (unsigned long)ts, rem);
+	}
+	/* return last_time_stamp with ?? warning */
+	return snprintf(buf, size, "[%5lu.%04lu??] ",
+		       (unsigned long)ts, rem);

I am not sure if this is worth the effort. All other tools parsing the log will be confused by this. Also there are tools that read the ring buffer dirrectly, e.g. "crash" reading a crashdump. They will need to get updated to handle KTIME_MAX_NS correctly.

Instead, I would modify printk_get_timestamp() to return the last time stamp if the current time is not known. Then log_buf will included reasonable values.

IMHO, it is not a big deal if two messages have the same time stamp. It already happens for messages that are printed before timekeeping machine is initialized. They all have zero time. Also the time stamp is created under the logbuf_lock(). It means that it is not the time when the printk() was called but it is the time when the printk() got access to the log_buf. By other words, the timestamp is not perfect anyway.

Even better solution would be to always get some reasonable time. Especially, when the message is printed long time after the previous one.

Best Regards,
Petr

#16 lore raw

Re: [PATCH 2/2] printk, Add printk.clock kernel parameter

Quoted 50 lines · Petr Mladek
Quoted 19 lines · Prarit Bhargava

This patch introduces printk.clock=[local|boot|real|tai] allowing a user to specify an adjusted clock to use with printk timestamps. The hardware clock, or the existing functionality, is preserved by default.

If the timekeeper_lock (used to protect writes from reads) is under contention, the last known good time is output again and "??" is used as the last two digits of time. This avoids contention issues with a CPU panicking while another CPU is writing time data.

---
 include/linux/time64.h |    2 +
 kernel/printk/printk.c |  105 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 99 insertions(+), 8 deletions(-)
include/linux/time64.h +0 -0
index 367d5af..f4a35d9 100644

include/linux/time64.h struct itimerspec64 { · line 43

Quoted 5 lines
include/linux/time64.h +2 -0
@@ -39,6 +39,8 @@ struct itimerspec64 {
 #define TIME64_MAX			((s64)~((u64)1 << 63))
 #define KTIME_MAX			((s64)~((u64)1 << 63))
 #define KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
+/* Maximum value returned by ktime_to_ns() */
+#define KTIME_MAX_NS 			((u64)((U64_MAX>>1)))

Please, use KTIME_NS_MAX to use the same naming scheme as KTIME_SEC_MAX.

Well, I would return 0 when the time is not known. See also below.

Quoted 19 lines
include/linux/time64.h +0 -0
 #if __BITS_PER_LONG == 64
 
kernel/printk/printk.c +9 -0
index 2ce8826..ffc05ee 100644
@@ -419,6 +419,83 @@ static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
 	return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
 }
 
+static u64 printk_try_real_ns(void)
+{
+	return ktime_try_real_ns();
+}
+
+static u64 printk_try_boot_ns(void)
+{
+	return ktime_try_boot_ns();
+}
Quoted 8 lines

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 435

Quoted 5 lines
kernel/printk/printk.c +5 -0
+
+static u64 printk_try_tai_ns(void)
+{
+	return ktime_try_tai_ns();
+}

Do we need these wrappers? Why not using ktime_try_xxx_ns() directly? You already use local_clock().

Unfortunately ktime_* are inlined functions, and I've mimicked the currently coding style there.

In any case I'm doing a v2 with different functions. I'll see if making this stuff not-inlined is any better.

Quoted 32 lines
Quoted 32 lines
kernel/printk/printk.c +30 -0
+static int printk_clock_param_set(const char *val,
+				  const struct kernel_param *kp)
+{
+	char *printk_clock_new = strstrip((char *)val);
+	int ret;
+
+	/* check if change is needed */
+	if (!strcmp(printk_clock_new, printk_clock))
+		return 0;
+
+	if (!strncmp("local", printk_clock_new, 5)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &local_clock;
+	} else if (!strncmp("real", printk_clock_new, 4)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &printk_try_real_ns;
+	} else if (!strncmp("boot", printk_clock_new, 4)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &printk_try_boot_ns;
+	} else if (!strncmp("tai", printk_clock_new, 3)) {
+		ret = param_set_charp(printk_clock_new, kp);
+		if (ret)
+			return ret;
+		printk_clock_fn = &printk_try_tai_ns;
Quoted 8 lines

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 485

Quoted 5 lines
kernel/printk/printk.c +5 -0
+	}
+
+	/* Did the clock change ? */
+	if (strcmp(printk_clock_new, printk_clock))
+		return -EINVAL;

I wonder if we need this extra check. It might be enough to print the error message in the final "else" above.

I was trying to catch a local to local change or a real to real change. But yeah ... I could sort of do that in the final else above.

Quoted 17 lines

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 497

Quoted 12 lines
kernel/printk/printk.c +11 -0
+	pr_info("printk: timestamp set to %s clock.\n", printk_clock);
+	return 0;
+}
+
+static struct kernel_param_ops printk_clock_param_ops = {
+	.set =		printk_clock_param_set,
+	.get =		param_get_charp,
+};
+
+module_param_cb(clock, &printk_clock_param_ops, &printk_clock, 0644);
+MODULE_PARM_DESC(clock, "Clock used for printk timestamps.  Can be local (hardware/default), boot, real, or tai.\n");

I have problems to parse this. It seems that the most common style used for possible values is to putthem into brackets. I wonder if this is better readable.

Quoted 2 lines

MODULE_PARM_DESC(clock, "Clock used for printk timestamps (local = default, boot, real, tai).");

Ah, thanks! :) I was wondering if there was a coding style preference there when I wrote this up.

Quoted 39 lines

kernel/printk/printk.c static int log_store(int facility, int level, · line 547

Quoted 11 lines
kernel/printk/printk.c +2 -1
+
 /* insert record into the buffer, discard old ones, update heads */
 static int log_store(int facility, int level,
 		     enum log_flags flags, u64 ts_nsec,
@@ -467,7 +544,7 @@ static int log_store(int facility, int level,
 	if (ts_nsec > 0)
 		msg->ts_nsec = ts_nsec;
 	else
-		msg->ts_nsec = local_clock();
+		msg->ts_nsec = printk_get_timestamp();

Hmm, one problem is that each clock returns another type of time. "local" and "boot" clocks returns the number of ns since the boot. While "real" and "tai" clocks returns the number of ns since 1.1.1970 or so.

The tools reading the timestamps are confused by this. For example, I get this:

$> echo boot >/sys/module/printk/parameters/clock $> echo local >/sys/module/printk/parameters/clock $> echo real >/sys/module/printk/parameters/clock $> echo tai >/sys/module/printk/parameters/clock $> dmesg | tail -6

[    6.976593] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[    6.977168] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[   77.500483] printk: timestamp set to boot clock.
[   81.419957] printk: timestamp set to local clock.
[1452177961.544909] printk: timestamp set to real clock.
[1452177965.224824] printk: timestamp set to tai clock.
$> dmesg -T | tail -6
[Thu Jan  7 15:44:41 2016] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[Thu Jan  7 15:44:41 2016] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[Thu Jan  7 15:45:52 2016] printk: timestamp set to boot clock.
[Thu Jan  7 15:45:56 2016] printk: timestamp set to local clock.
[Fri Jan 13 06:30:36 2062] printk: timestamp set to real clock.
[Fri Jan 13 06:30:40 2062] printk: timestamp set to tai clock.
Quoted 2 lines

Please, note that the last messages looks as they are printed in a far far future ;-)

Heh :) I didn't even think of testing that. I'll have to think about that a bit more. systemd logging must assume boot time/local time. I'll ping those guys with a patch if/when the next version of this gets accepted.

Thanks for catching this.

Quoted 30 lines
Quoted 30 lines
kernel/printk/printk.c +11 -4
 	memset(log_dict(msg) + dict_len, 0, pad_len);
 	msg->len = size;
 
@@ -1039,18 +1116,30 @@ module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
 
 static size_t print_time(u64 ts, char *buf)
 {
-	unsigned long rem_nsec;
+	unsigned long rem;
+	size_t size;
 
 	if (!printk_time)
 		return 0;
 
-	rem_nsec = do_div(ts, 1000000000);
+	if (ts == KTIME_MAX_NS)
+		ts = last_time_stamp;
 
-	if (!buf)
-		return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
+	if (buf) {
+		size = INT_MAX;
+		rem = do_div(ts, 1000000000) / 1000;
+	} else {
+		size = 0;
+		rem = 0;
+	}
 
Quoted 14 lines

kernel/printk/printk.c module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); · line 1142

Quoted 9 lines
kernel/printk/printk.c +7 -2
-	return sprintf(buf, "[%5lu.%06lu] ",
-		       (unsigned long)ts, rem_nsec / 1000);
+	if (ts != KTIME_MAX_NS) {
+		return snprintf(buf, size, "[%5lu.%06lu] ",
+			       (unsigned long)ts, rem);
+	}
+	/* return last_time_stamp with ?? warning */
+	return snprintf(buf, size, "[%5lu.%04lu??] ",
+		       (unsigned long)ts, rem);

I am not sure if this is worth the effort. All other tools parsing the log will be confused by this. Also there are tools that read the ring buffer dirrectly, e.g. "crash" reading a crashdump. They will need to get updated to handle KTIME_MAX_NS correctly.

Yep -- the thread contains suggestions on how to avoid all of this by using the NMI safe ktime_* calls (and implementing new ones). I'm going to do that which should nicely clean all of this up.

Thanks for the review Petr,

P.

maintainer
#17 lore raw

Re: [PATCH 2/2] printk, Add printk.clock kernel parameter

Quoted 23 lines · Prarit Bhargava
Quoted 20 lines · Petr Mladek
Quoted 4 lines · Prarit Bhargava

This patch introduces printk.clock=[local|boot|real|tai] allowing a user to specify an adjusted clock to use with printk timestamps. The hardware clock, or the existing functionality, is preserved by default.

kernel/printk/printk.c +1 -0
+	pr_info("printk: timestamp set to %s clock.\n", printk_clock);

kernel/printk/printk.c static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, · line 497

Quoted 10 lines
kernel/printk/printk.c +10 -0
+	return 0;
+}
+
+static struct kernel_param_ops printk_clock_param_ops = {
+	.set =		printk_clock_param_set,
+	.get =		param_get_charp,
+};
+
+module_param_cb(clock, &printk_clock_param_ops, &printk_clock, 0644);
+MODULE_PARM_DESC(clock, "Clock used for printk timestamps.  Can be local (hardware/default), boot, real, or tai.\n");

I have problems to parse this. It seems that the most common style used for possible values is to putthem into brackets. I wonder if this is better readable.

Quoted 5 lines
Quoted 2 lines

MODULE_PARM_DESC(clock, "Clock used for printk timestamps (local = default, boot, real, tai).");

Ah, thanks! :) I was wondering if there was a coding style preference there when I wrote this up.

Heh, I am not sure if there is any coding style for this. I just went over the output from

git grep MODULE_PARM_DESC

Quoted 39 lines
Quoted 39 lines

kernel/printk/printk.c static int log_store(int facility, int level, · line 547

Quoted 11 lines
kernel/printk/printk.c +2 -1
+
 /* insert record into the buffer, discard old ones, update heads */
 static int log_store(int facility, int level,
 		     enum log_flags flags, u64 ts_nsec,
@@ -467,7 +544,7 @@ static int log_store(int facility, int level,
 	if (ts_nsec > 0)
 		msg->ts_nsec = ts_nsec;
 	else
-		msg->ts_nsec = local_clock();
+		msg->ts_nsec = printk_get_timestamp();

Hmm, one problem is that each clock returns another type of time. "local" and "boot" clocks returns the number of ns since the boot. While "real" and "tai" clocks returns the number of ns since 1.1.1970 or so.

The tools reading the timestamps are confused by this. For example, I get this:

$> echo boot >/sys/module/printk/parameters/clock $> echo local >/sys/module/printk/parameters/clock $> echo real >/sys/module/printk/parameters/clock $> echo tai >/sys/module/printk/parameters/clock $> dmesg | tail -6

[    6.976593] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[    6.977168] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[   77.500483] printk: timestamp set to boot clock.
[   81.419957] printk: timestamp set to local clock.
[1452177961.544909] printk: timestamp set to real clock.
[1452177965.224824] printk: timestamp set to tai clock.
$> dmesg -T | tail -6
[Thu Jan  7 15:44:41 2016] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[Thu Jan  7 15:44:41 2016] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[Thu Jan  7 15:45:52 2016] printk: timestamp set to boot clock.
[Thu Jan  7 15:45:56 2016] printk: timestamp set to local clock.
[Fri Jan 13 06:30:36 2062] printk: timestamp set to real clock.
[Fri Jan 13 06:30:40 2062] printk: timestamp set to tai clock.
Quoted 6 lines
Quoted 2 lines

Please, note that the last messages looks as they are printed in a far far future ;-)

Heh :) I didn't even think of testing that. I'll have to think about that a bit more. systemd logging must assume boot time/local time. I'll ping those guys with a patch if/when the next version of this gets accepted.

I am not sure how many tools would be affected by this. An easier solution would be to normalize all times and always save the relative time since the boot as it is done now.

Best Regards,
Petr

#18 lore raw

Re: [PATCH 2/2] printk, Add printk.clock kernel parameter

Quoted 6 lines · Petr Mladek
Quoted 3 lines · Prarit Bhargava

Heh :) I didn't even think of testing that. I'll have to think about that a bit more. systemd logging must assume boot time/local time. I'll ping those guys with a patch if/when the next version of this gets accepted.

I am not sure how many tools would be affected by this.

I don't see a point in a gazillion of different clocks for printk either.

Quoted 2 lines

An easier solution would be to normalize all times and always save the relative time since the boot as it is done now.

Why? What's wrong with simply using the existing ktime_get_mono_fast_ns() instead of local_clock() and be done with it?

Thanks,

tglx

#19 lore raw
In reply to Prarit Bhargava
Quoted 4 lines · Prarit Bhargava

Over the past years I've seen many reports of bugs that include time-stamped kernel logs (enabled when CONFIG_PRINTK_TIME=y or print.time=1 is specified as a kernel parameter) that do not align with either external time stamped logs or /var/log/messages.

[]

Quoted 3 lines

This patch introduces printk.clock=[local|boot|real|tai] allowing a user to specify an adjusted clock to use with printk timestamps.  The hardware clock, or the existing functionality, is preserved by default.

printk.c is already way too large.

Perhaps it would be reasonable to create a new kernel/printk/timestamp.c file with these changes.