Skip to contentSkip to conversation

printk: Add process name information to printk() output.

PATCH Changki Kim 5 participants 19 messages Sep 4, 2020 – Sep 11, 2020 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20200904082449epcas2p4420d5df2083325b328a182c79f5c0948@epcas2p4.samsung.com

#1 lore raw

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor.
If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

Therefore, instead of trying to help buffering, let's try to help reconstructing messages by saving caller information as of calling log_store() and adding it as "[$processor_id: $process_name: $thread_id]" upon printing to consoles.

Some examples for console output:

  [    0.059580] [0:      swapper/0:    1] CPU: All CPU(s) started at EL1i
  [    2.153157] [5:           init:    1] migov: complete to probe migov
  [    7.441317] [4:           init:    1] NET: Registered protocol family 39
  [   22.793389] [5:  kworker/u16:1:  184] SMP: stopping secondary CPUs
---
 kernel/printk/printk.c            | 20 ++++++++++++++++++--
 kernel/printk/printk_ringbuffer.h |  7 +++++++
 lib/Kconfig.debug                 | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)
kernel/printk/printk.c +18 -2
index ad8d1dfe5fbe..b8e7a263b123 100644
@@ -408,7 +408,7 @@ static unsigned long console_dropped;
 /* the next printk record to read after the last 'clear' command */
 static u64 clear_seq;
 
-#ifdef CONFIG_PRINTK_CALLER
+#if defined(CONFIG_PRINTK_CALLER) || defined(CONFIG_PRINTK_PROCESS)
 #define PREFIX_MAX		48
 #else
 #define PREFIX_MAX		32
@@ -530,7 +530,12 @@ static int log_store(u32 caller_id, int facility, int level,
 	else
 		r.info->ts_nsec = local_clock();
 	r.info->caller_id = caller_id;
-
+#ifdef CONFIG_PRINTK_PROCESS
+	get_task_comm(r.info->process, current);
+	r.info->pid = task_pid_nr(current);
+	r.info->cpu_id = raw_smp_processor_id();
+	r.info->in_interrupt = in_interrupt() ? 1 : 0;
+#endif
 	/* insert message */
 	prb_commit(&e);
 
@@ -1298,6 +1303,16 @@ static size_t print_caller(u32 id, char *buf)
 #else
 #define print_caller(id, buf) 0
 #endif
+#ifdef CONFIG_PRINTK_PROCESS
+static size_t print_process(const struct printk_info *info, char *buf)
+{
+	return sprintf(buf, "%c[%1d:%15s:%5d]",
+		       info->in_interrupt ? 'I' : ' ', info->cpu_id,
+		       info->process, info->pid);
+}
+#else
+#define print_process(info, buf) 0
+#endif
 
 static size_t info_print_prefix(const struct printk_info  *info, bool syslog,
 				bool time, char *buf)
@@ -1310,6 +1325,7 @@ static size_t info_print_prefix(const struct printk_info  *info, bool syslog,
 	if (time)
 		len += print_time(info->ts_nsec, buf + len);
 
+	len += print_process(info, buf + len);
 	len += print_caller(info->caller_id, buf + len);
 
 	if (IS_ENABLED(CONFIG_PRINTK_CALLER) || time) {
kernel/printk/printk_ringbuffer.h +7 -0
index e6302da041f9..fcefe9516606 100644
@@ -4,6 +4,7 @@
 #define _KERNEL_PRINTK_RINGBUFFER_H
 
 #include <linux/atomic.h>
+#include <linux/sched.h>
 
 /*
  * Meta information about each stored message.
@@ -21,6 +22,12 @@ struct printk_info {
 	u8	flags:5;	/* internal record flags */
 	u8	level:3;	/* syslog level */
 	u32	caller_id;	/* thread id or processor id */
+#ifdef CONFIG_PRINTK_PROCESS
+	int	pid;			/* process id */
+	u8	cpu_id;			/* processor id */
+	u8	in_interrupt;		/* interrupt conext */
+	char process[TASK_COMM_LEN];	/* process name */
+#endif
 };
 
 /*
lib/Kconfig.debug +16 -0
index a84a41d8fadd..921d9e0eeb1a 100644
@@ -35,6 +35,22 @@ config PRINTK_CALLER
 	  no option to enable/disable at the kernel command line parameter or
 	  sysfs interface.
 
+config PRINTK_PROCESS
+        bool "Show process information on printks"
+        depends on PRINTK && !PRINTK_CALLER
+        help
+          Selecting this option causes printk() to add cpu number, process name
+	  and pid to every message.
+
+	  This option is intended for environments where multiple cores and
+	  multiple processes concurrently call printk() for many times, for it
+	  is difficult to interpret without knowing where these lines came from.
+	  This option is very useful from the point of view of vendor. You can
+	  infer problems of CPU or H/W IP to the cpu number and process name.
+
+	  This option can not use with PRINK_CALLER. Because prefix is too long
+	  and has duplicate information (processor id and thread id).
+
 config CONSOLE_LOGLEVEL_DEFAULT
 	int "Default console loglevel (1-15)"
 	range 1 15
Signature · 1 line
2.14.2
maintainer
#2 lore raw
Quoted 18 lines · Changki Kim

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor.
If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

Therefore, instead of trying to help buffering, let's try to help reconstructing messages by saving caller information as of calling log_store() and adding it as "[$processor_id: $process_name: $thread_id]" upon printing to consoles.

Some examples for console output:

  [    0.059580] [0:      swapper/0:    1] CPU: All CPU(s) started at EL1i
  [    2.153157] [5:           init:    1] migov: complete to probe migov
  [    7.441317] [4:           init:    1] NET: Registered protocol family 39
  [   22.793389] [5:  kworker/u16:1:  184] SMP: stopping secondary CPUs
Quoted 6 lines
---
 kernel/printk/printk.c            | 20 ++++++++++++++++++--
 kernel/printk/printk_ringbuffer.h |  7 +++++++
 lib/Kconfig.debug                 | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

Isn't the CPU number already present if you want it today? Why add it again?

thanks,

greg k-h

author
#3 lore raw
Quoted 7 lines · Changki Kim
Quoted 6 lines

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor. If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

Quoted 9 lines
Quoted 3 lines

Therefore, instead of trying to help buffering, let's try to help reconstructing messages by saving caller information as of calling log_store() and adding it as "[$processor_id: $process_name:

$thread_id]"

Quoted 5 lines

upon printing to consoles.

Some examples for console output:

  [    0.059580] [0:      swapper/0:    1] CPU: All CPU(s) started at

EL1i

Quoted 1 line
Quoted 1 line
  [    2.153157] [5:           init:    1] migov: complete to probe

migov

Quoted 1 line
Quoted 1 line
  [    7.441317] [4:           init:    1] NET: Registered protocol

family 39

Quoted 9 lines
Quoted 8 lines
  [   22.793389] [5:  kworker/u16:1:  184] SMP: stopping secondary CPUs
---
 kernel/printk/printk.c            | 20 ++++++++++++++++++--
 kernel/printk/printk_ringbuffer.h |  7 +++++++
 lib/Kconfig.debug                 | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)
Quoted 6 lines

Isn't the CPU number already present if you want it today? Why add it again?

thanks,

greg k-h

CPU number isn't present now. If I enabled PRINTK_CALLER, then it is present cpu id or thread id.
The config isn't always present cpu id.
We need cpu id each each message.

Thanks.

maintainer
#4 lore raw
Quoted 33 lines · ��â��
Quoted 7 lines · Changki Kim
Quoted 6 lines

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor. If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

Quoted 9 lines
Quoted 3 lines

Therefore, instead of trying to help buffering, let's try to help reconstructing messages by saving caller information as of calling log_store() and adding it as "[$processor_id: $process_name:

$thread_id]"

Quoted 5 lines

upon printing to consoles.

Some examples for console output:

  [    0.059580] [0:      swapper/0:    1] CPU: All CPU(s) started at

EL1i

Quoted 1 line
Quoted 1 line
  [    2.153157] [5:           init:    1] migov: complete to probe

migov

Quoted 1 line
Quoted 1 line
  [    7.441317] [4:           init:    1] NET: Registered protocol

family 39

Quoted 12 lines
Quoted 8 lines
  [   22.793389] [5:  kworker/u16:1:  184] SMP: stopping secondary CPUs
---
 kernel/printk/printk.c            | 20 ++++++++++++++++++--
 kernel/printk/printk_ringbuffer.h |  7 +++++++
 lib/Kconfig.debug                 | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

Isn't the CPU number already present if you want it today? Why add it again?

Quoted 6 lines
Quoted 3 lines

thanks,

greg k-h

CPU number isn't present now. If I enabled PRINTK_CALLER, then it is present cpu id or thread id.

Great, that should give you what you need here, right?

Quoted 1 line

The config isn't always present cpu id.

What do you mean?

Quoted 1 line

We need cpu id each each message.

Why each message? What does that provide you?

And if PRINTK_CALLER does not work properly, please fix it. I don't see how it would not work any different from your patch, you shouldn't duplicate things.

thanks,

greg k-h

author
#5 lore raw
Quoted 40 lines · On Fri, Sep 04, 2020 at 06:31:13PM +0900, â wrote:
Quoted 5 lines
Quoted 5 lines · Changki Kim
Quoted 4 lines

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor. If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or

CPU.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

This is very useful in narrowing down the scope of the problems.

Therefore, instead of trying to help buffering, let's try to help reconstructing messages by saving caller information as of calling log_store() and adding it as "[$processor_id: $process_name:

$thread_id]"

Quoted 2 lines

upon printing to consoles.

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Some examples for console output:

  [    0.059580] [0:      swapper/0:    1] CPU: All CPU(s) started at

EL1i

Quoted 1 line
Quoted 1 line
  [    2.153157] [5:           init:    1] migov: complete to probe

migov

Quoted 1 line
Quoted 1 line
  [    7.441317] [4:           init:    1] NET: Registered protocol

family 39

Quoted 1 line
Quoted 1 line
  [   22.793389] [5:  kworker/u16:1:  184] SMP: stopping secondary

CPUs

Quoted 15 lines
Quoted 14 lines
Quoted 7 lines
---
 kernel/printk/printk.c            | 20 ++++++++++++++++++--
 kernel/printk/printk_ringbuffer.h |  7 +++++++
 lib/Kconfig.debug                 | 16 ++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

Isn't the CPU number already present if you want it today? Why add it again?

thanks,

greg k-h

Quoted 8 lines
Quoted 2 lines

CPU number isn't present now. If I enabled PRINTK_CALLER, then it is present cpu id or thread id.

Great, that should give you what you need here, right?

Quoted 1 line

The config isn't always present cpu id.

What do you mean?

When PRINTK_CALLER is enabled, cpu id or thread id are present. not always cpu id.

Quoted 4 lines
Quoted 2 lines

We need cpu id each each message.

Why each message? What does that provide you?

In mobile project, Sometimes we can meet the problem in the physically cpu. It is important to know cpu id for H/W Issue.

Quoted 4 lines

And if PRINTK_CALLER does not work properly, please fix it. I don't see how it would not work any different from your patch, you shouldn't duplicate things.

PRINTK_CALLER worked well.

Quoted 4 lines

thanks,

greg k-h

maintainer
#6 lore raw
In reply to Changki Kim
Quoted 5 lines · Changki Kim

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor.
If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

[...]

Quoted 5 lines
kernel/printk/printk_ringbuffer.h +0 -0
index e6302da041f9..fcefe9516606 100644

kernel/printk/printk_ringbuffer.h struct printk_info { · line 31

Quoted 10 lines
kernel/printk/printk_ringbuffer.h +6 -0
@@ -21,6 +22,12 @@ struct printk_info {
 	u8	flags:5;	/* internal record flags */
 	u8	level:3;	/* syslog level */
 	u32	caller_id;	/* thread id or processor id */
+#ifdef CONFIG_PRINTK_PROCESS
+	int	pid;			/* process id */
+	u8	cpu_id;			/* processor id */
+	u8	in_interrupt;		/* interrupt conext */
+	char process[TASK_COMM_LEN];	/* process name */
+#endif
 };

I can understand the desire to have more information with messages. But IMHO adding it to the ringbuffer descriptor is the wrong place for it. The descriptor should really be limited to data that the printk subsystem needs for _itself_. With respect to LOG_CONT, I think we can agree that @caller_id is not enough. But there has been discussions [0] of having @caller_id provide a better context representation.

If we want to support adding more meta information to messages, I would prefer that the information is either prepended directly to the message text string or appended to the dictionary text string. We could even go so far as providing a boot argument where a list of information could be specified, what should be automatically added to the text/dict strings of each message. That would not require any ringbuffer changes and would allow new types of information to be added later.

Something like:

printk.format=ts,cpu,comm,pid,in_atomic

John Ogness

[0] https://lkml.kernel.org/r/[email protected]

maintainer
#7 lore raw
Quoted 40 lines · John Ogness
Quoted 5 lines · Changki Kim

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor.
If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

[...]

Quoted 5 lines
kernel/printk/printk_ringbuffer.h +0 -0
index e6302da041f9..fcefe9516606 100644

kernel/printk/printk_ringbuffer.h struct printk_info { · line 31

Quoted 10 lines
kernel/printk/printk_ringbuffer.h +6 -0
@@ -21,6 +22,12 @@ struct printk_info {
 	u8	flags:5;	/* internal record flags */
 	u8	level:3;	/* syslog level */
 	u32	caller_id;	/* thread id or processor id */
+#ifdef CONFIG_PRINTK_PROCESS
+	int	pid;			/* process id */
+	u8	cpu_id;			/* processor id */
+	u8	in_interrupt;		/* interrupt conext */
+	char process[TASK_COMM_LEN];	/* process name */
+#endif
 };

I can understand the desire to have more information with messages. But IMHO adding it to the ringbuffer descriptor is the wrong place for it. The descriptor should really be limited to data that the printk subsystem needs for _itself_. With respect to LOG_CONT, I think we can agree that @caller_id is not enough. But there has been discussions [0] of having @caller_id provide a better context representation.

If we want to support adding more meta information to messages, I would prefer that the information is either prepended directly to the message text string or appended to the dictionary text string. We could even go so far as providing a boot argument where a list of information could be specified, what should be automatically added to the text/dict strings of each message. That would not require any ringbuffer changes and would allow new types of information to be added later.

Quoted 7 lines

Something like:

printk.format=ts,cpu,comm,pid,in_atomic

John Ogness

[0] https://lkml.kernel.org/r/[email protected]

Ah, finally a good use of the "dictionary" that we all can agree makes sense :)

This does seem like a better solution overall, thanks.

greg k-h

#8 lore raw
Quoted 45 lines · Greg KH
Quoted 44 lines · John Ogness
Quoted 5 lines · Changki Kim

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor.
If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

[...]

Quoted 5 lines
kernel/printk/printk_ringbuffer.h +0 -0
index e6302da041f9..fcefe9516606 100644

kernel/printk/printk_ringbuffer.h struct printk_info { · line 31

Quoted 10 lines
kernel/printk/printk_ringbuffer.h +6 -0
@@ -21,6 +22,12 @@ struct printk_info {
 	u8	flags:5;	/* internal record flags */
 	u8	level:3;	/* syslog level */
 	u32	caller_id;	/* thread id or processor id */
+#ifdef CONFIG_PRINTK_PROCESS
+	int	pid;			/* process id */
+	u8	cpu_id;			/* processor id */
+	u8	in_interrupt;		/* interrupt conext */
+	char process[TASK_COMM_LEN];	/* process name */
+#endif
 };

I can understand the desire to have more information with messages. But IMHO adding it to the ringbuffer descriptor is the wrong place for it. The descriptor should really be limited to data that the printk subsystem needs for _itself_. With respect to LOG_CONT, I think we can agree that @caller_id is not enough. But there has been discussions [0] of having @caller_id provide a better context representation.

If we want to support adding more meta information to messages, I would prefer that the information is either prepended directly to the message text string or appended to the dictionary text string. We could even go so far as providing a boot argument where a list of information could be specified, what should be automatically added to the text/dict strings of each message. That would not require any ringbuffer changes and would allow new types of information to be added later.

Something like:

printk.format=ts,cpu,comm,pid,in_atomic

Quoted 8 lines
Quoted 3 lines

John Ogness

[0] https://lkml.kernel.org/r/[email protected]

Ah, finally a good use of the "dictionary" that we all can agree makes sense :)

This does seem like a better solution overall, thanks.

__func__ too please so that it could optionally be enabled per subsystem using %pS, __builtin_return_address(<appropriate_level>)

That way a lot of %s:..., __func__ could be removed from specific printk instances.

maintainer
#9 lore raw
In reply to John Ogness
Quoted 21 lines · John Ogness
Quoted 5 lines · Changki Kim

Printk() meesages are the most basic and useful debug method. However, additional information needs in multi-processor.
If we add messages with processor id and process name, we can find a problem only with messages when the problem occurs with H/W IP or CPU. This is very useful in narrowing down the scope of the problems.

[...]

Quoted 12 lines
kernel/printk/printk_ringbuffer.h +4 -0
index e6302da041f9..fcefe9516606 100644
@@ -21,6 +22,12 @@ struct printk_info {
 	u8	flags:5;	/* internal record flags */
 	u8	level:3;	/* syslog level */
 	u32	caller_id;	/* thread id or processor id */
+#ifdef CONFIG_PRINTK_PROCESS
+	int	pid;			/* process id */
+	u8	cpu_id;			/* processor id */
+	u8	in_interrupt;		/* interrupt conext */
Quoted 8 lines

kernel/printk/printk_ringbuffer.h struct printk_info { · line 31

Quoted 3 lines
kernel/printk/printk_ringbuffer.h +2 -0
+	char process[TASK_COMM_LEN];	/* process name */
+#endif
 };

I can understand the desire to have more information with messages. But IMHO adding it to the ringbuffer descriptor is the wrong place for it. The descriptor should really be limited to data that the printk subsystem needs for _itself_.

I fully agree.

Quoted 3 lines

With respect to LOG_CONT, I think we can agree that @caller_id is not enough. But there has been discussions [0]
of having @caller_id provide a better context representation.

I am not sure if I read the above correctly. Anyway, @caller_id is needed by printk() to handle continuous lines. It would be great to store more precise information about the irq context. But more details, like process name, have to be stored somewhere else.

Quoted 3 lines

If we want to support adding more meta information to messages, I would prefer that the information is either prepended directly to the message text string or appended to the dictionary text string.

I have two problems with dict ring:

+ does not guarantee enough space to always store the metadata
+ waste of space by repeating the same prefixes again and again

I am currently playing with support for all three timestamps based on https://lore.kernel.org/lkml/[email protected]/

And I got the following idea:

1. Storing side:

Create one more ring/array for storing the optional metadata. It might eventually replace dict ring, see below.

struct struct printk_ext_info {
u64 ts_boot; /* timestamp from boot clock */
u64 ts_real; /* timestamp from real clock */
char process[TASK_COMM_LEN]; /* process name */
};

It must be in a separate array so that struct prb_desc stay stable and crashdump tools do not need to be updated so often.

But the number of these structures must be the same as descriptors. So it might be:

   struct prb_desc_ring {
	unsigned int		count_bits;
	struct prb_desc		*descs;
	struct printk_ext_info  *ext_info
	atomic_long_t		head_id;
	atomic_long_t		tail_id;

};

One huge advantage is that these extra information would not block pushing lockless printk buffer upstream.

It might be even possible to get rid of dict ring and just add two more elements into struct printk_ext_info:

char subsystem[16]; /* for SUBSYSTEM= dict value */
char device[48]; /* for DEVICE= dict value */

Pros:

+ the information will always get stored

Cons:

+ the names might get shrinked

Space usage:

+ No need to store "SUBSYSTEM=" and "DEVICE=" repeatedly.
+ Space used only for dev_printk()

Well, I think that it might work in practice.

2. Reading side:

+ /dev/kmsg is easy. It makes perfect sense to pass all extra information in the dictionary format:

+ less effective but /dev/kmsg is fast
+ does not break userspace
+ might be shown even by the current journalctl

+ syslog syscall and /proc/syslog are hard to extend. I would ignore them. Users need to use /dev/kmsg to get the dictionary metadata already now.

+ console is a sensitive beast

The output format would need to be configurable using build/boot/runtime options. It is already the case for the timestamp, see printk_time parameter.

And people would probably want to have this configurable per-console to disable this on the slow ones, sigh.

Best Regards,
Petr

maintainer
#10 lore raw
Quoted 30 lines · Petr Mladek

I am currently playing with support for all three timestamps based on https://lore.kernel.org/lkml/[email protected]/

And I got the following idea:

1. Storing side:

Create one more ring/array for storing the optional metadata. It might eventually replace dict ring, see below.

struct struct printk_ext_info {
u64 ts_boot; /* timestamp from boot clock */
u64 ts_real; /* timestamp from real clock */
char process[TASK_COMM_LEN]; /* process name */
};

It must be in a separate array so that struct prb_desc stay stable and crashdump tools do not need to be updated so often.

But the number of these structures must be the same as descriptors. So it might be:

struct prb_desc_ring {

	unsigned int		count_bits;
	struct prb_desc		*descs;
	struct printk_ext_info  *ext_info
	atomic_long_t		head_id;
	atomic_long_t		tail_id;

};

Quoted 8 lines

One huge advantage is that these extra information would not block pushing lockless printk buffer upstream.

It might be even possible to get rid of dict ring and just add two more elements into struct printk_ext_info:

char subsystem[16]; /* for SUBSYSTEM= dict value */
char device[48]; /* for DEVICE= dict value */

You say "get rid of dict ring", but there is nothing requiring the dict_ring to be strings. It can be binary data. The @data of the prb_data_block struct could be a printk_ext_info struct. This would be trivial to implement in printk.c and would not require any ringbuffer changes. (My ringbuffer test software [0] uses binary structs for the data.)

Using VMCOREINFO we can provide the printk_ext_info size and field offsets for crash tools.

Quoted 3 lines

Pros:

+ the information will always get stored

If the dict_ring is "_DESCS_COUNT() * sizeof(struct printk_ext_info)" then it would also always get stored. Although this does seem like a bit of a waste of space in order to cover the worst case scenario of all records using all fields.

John Ogness

[0] https://github.com/Linutronix/prb-test.git

maintainer
#11 lore raw
Quoted 50 lines · John Ogness
Quoted 30 lines · Petr Mladek

I am currently playing with support for all three timestamps based on https://lore.kernel.org/lkml/[email protected]/

And I got the following idea:

1. Storing side:

Create one more ring/array for storing the optional metadata. It might eventually replace dict ring, see below.

struct struct printk_ext_info {
u64 ts_boot; /* timestamp from boot clock */
u64 ts_real; /* timestamp from real clock */
char process[TASK_COMM_LEN]; /* process name */
};

It must be in a separate array so that struct prb_desc stay stable and crashdump tools do not need to be updated so often.

But the number of these structures must be the same as descriptors. So it might be:

struct prb_desc_ring {

	unsigned int		count_bits;
	struct prb_desc		*descs;
	struct printk_ext_info  *ext_info
	atomic_long_t		head_id;
	atomic_long_t		tail_id;

};

Quoted 8 lines

One huge advantage is that these extra information would not block pushing lockless printk buffer upstream.

It might be even possible to get rid of dict ring and just add two more elements into struct printk_ext_info:

char subsystem[16]; /* for SUBSYSTEM= dict value */
char device[48]; /* for DEVICE= dict value */

You say "get rid of dict ring", but there is nothing requiring the dict_ring to be strings. It can be binary data. The @data of the prb_data_block struct could be a printk_ext_info struct. This would be trivial to implement in printk.c and would not require any ringbuffer changes. (My ringbuffer test software [0] uses binary structs for the data.)

Using VMCOREINFO we can provide the printk_ext_info size and field offsets for crash tools.

Quoted 8 lines
Quoted 3 lines

Pros:

+ the information will always get stored

If the dict_ring is "_DESCS_COUNT() * sizeof(struct printk_ext_info)" then it would also always get stored. Although this does seem like a bit of a waste of space in order to cover the worst case scenario of all records using all fields.

The opposite is that the extended metadata might be missing for some lines. This might cause quite some confusion.

For example, I can't find any reasonable way how to produce missing timestamps.

From my POV, if we support 3 timestamps then they must be stored reliably. And dict ring is out of the game.

And I am not comfortable even with the current dictionary handling. I already wrote this somewhere. The following command is supposed to show all kernel messages printed by "pci" subsystem:

$> journalctl _KERNEL_SUBSYSTEM=pci

It will be incomplete when the dictionary metadata were not saved.

You might argue that the problem already exists when the entire message gets lost. But this situation can be detected by missing sequence numbers and journalctl even add messages about it. But the user currently is not informed about missing dictionary.

Regarding the waste of space. The dict ring currently has the same size as the text ring. It is likely a waste of space as well. Any tuning is complicated because it depends on the use case.

The advantage of the fixed @ext_info[] array is that everything is clear, simple, and predictable (taken space and name length limits). We could easily tell users what they will get for a given cost.

Best Regards,
Petr

maintainer
#12 lore raw
Quoted 33 lines · Petr Mladek
Quoted 33 lines
Quoted 33 lines

I am currently playing with support for all three timestamps based on https://lore.kernel.org/lkml/[email protected]/

And I got the following idea:

1. Storing side:

Create one more ring/array for storing the optional metadata. It might eventually replace dict ring, see below.

struct struct printk_ext_info {
u64 ts_boot; /* timestamp from boot clock */
u64 ts_real; /* timestamp from real clock */
char process[TASK_COMM_LEN]; /* process name */
};

It must be in a separate array so that struct prb_desc stay stable and crashdump tools do not need to be updated so often.

   But the number of these structures must be the same as descriptors.
   So it might be:

   struct prb_desc_ring {
	unsigned int		count_bits;
	struct prb_desc		*descs;
	struct printk_ext_info  *ext_info
	atomic_long_t		head_id;
	atomic_long_t		tail_id;
   };

   One huge advantage is that these extra information would not block
   pushing lockless printk buffer upstream.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

It might be even possible to get rid of dict ring and just add two more elements into struct printk_ext_info:

char subsystem[16]; /* for SUBSYSTEM= dict value */
char device[48]; /* for DEVICE= dict value */

From my POV, if we support 3 timestamps then they must be stored reliably. And dict ring is out of the game.

Agreed. I am just trying to think of how to better manage the strings, which currently are rare and optional. That is where the dict_ring becomes interesting.

Perhaps we should use both the fixed structs with the variable dict_ring. printk_ext_info could look like this:

struct struct printk_ext_info {
    u64 ts_boot;
    u64 ts_real;
    char *process;
    char *subsystem;
    char *device;
};

And @process, @subsystem, @device could all point to null-terminated trings within the dict_ring. So printk.c code looks something like this:

size_t process_sz = strlen(process) + 1;
size_t subsystem_sz = strlen(subsystem) + 1;
size_t device_sz = strlen(device) + 1;
struct prb_reserved_entry e;
struct printk_record r;
char *p;

prb_rec_init_wr(&r, text_len, process_sz + subsystem_sz + device_sz); prb_reserve(&e, prb, &r);

memcpy(r.text_buf, text, text_len);
r.info->text_len = text_len;

/* guaranteed ext data */
r.ext_info->ts_boot = time_boot();
r.ext_info->ts_real = time_real();

/* optional ext data */

if (r.dict_buf) {
    p = r.dict_buf;

    memcpy(p, process, process_sz);
    r.ext_info->process = p;
    p += process_sz;
    
    memcpy(p, subsystem, subsystem_sz);
    r.ext_info->subsystem = p;    
    p += subsystem_sz;
    
    memcpy(p, device, device_sz);
    r.ext_info->device = p;

    r.info->dict_len = process_sz + subsystem_sz + device_sz;
}
Quoted 7 lines

And I am not comfortable even with the current dictionary handling. I already wrote this somewhere. The following command is supposed to show all kernel messages printed by "pci" subsystem:

$> journalctl _KERNEL_SUBSYSTEM=pci

It will be incomplete when the dictionary metadata were not saved.

In that case, perhaps @subsystem should be a static array in printk_ext_info instead.

Quoted 3 lines

Regarding the waste of space. The dict ring currently has the same size as the text ring. It is likely a waste of space as well. Any tuning is complicated because it depends on the use case.

The whole point of the dict_ring is that it allows for variable length _optional_ data to be stored. If we decide there is no optional data, then dict_ring is not needed.

Quoted 3 lines

The advantage of the fixed @ext_info[] array is that everything is clear, simple, and predictable (taken space and name length limits). We could easily tell users what they will get for a given cost.

Agreed. For non-optional data (such as your timestamps), I am in full agreement that a fixed array is the way to go. And it would only require a couple lines of code added to the ringbuffer.

My concern is if we need to guarantee space for all possible dictionary data of all records. I think the dict_ring can be very helpful here.

John Ogness

author
#13 lore raw
Quoted 116 lines · Petr Mladek
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines

I am currently playing with support for all three timestamps based on
https://lore.kernel.org/lkml/[email protected]/

And I got the following idea:

1. Storing side:

Create one more ring/array for storing the optional metadata. It might eventually replace dict ring, see below.

struct struct printk_ext_info {
u64 ts_boot; /* timestamp from boot clock */
u64 ts_real; /* timestamp from real clock */
char process[TASK_COMM_LEN]; /* process name */
};

It must be in a separate array so that struct prb_desc stay stable and crashdump tools do not need to be updated so often.

   But the number of these structures must be the same as descriptors.
   So it might be:

   struct prb_desc_ring {
	unsigned int		count_bits;
	struct prb_desc		*descs;
	struct printk_ext_info  *ext_info
	atomic_long_t		head_id;
	atomic_long_t		tail_id;
   };

   One huge advantage is that these extra information would not block
   pushing lockless printk buffer upstream.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

It might be even possible to get rid of dict ring and just add two more elements into struct printk_ext_info:

char subsystem[16]; /* for SUBSYSTEM= dict value */
char device[48]; /* for DEVICE= dict value */

From my POV, if we support 3 timestamps then they must be stored reliably. And dict ring is out of the game.

Agreed. I am just trying to think of how to better manage the strings, which currently are rare and optional. That is where the dict_ring becomes interesting.

Perhaps we should use both the fixed structs with the variable dict_ring. printk_ext_info could look like this:

struct struct printk_ext_info {
    u64 ts_boot;
    u64 ts_real;
    char *process;
    char *subsystem;
    char *device;
};

And @process, @subsystem, @device could all point to null-terminated trings within the dict_ring. So printk.c code looks something like this:

size_t process_sz = strlen(process) + 1; size_t subsystem_sz = strlen(subsystem) + 1; size_t device_sz = strlen(device) + 1; struct prb_reserved_entry e; struct printk_record r; char *p;

prb_rec_init_wr(&r, text_len, process_sz + subsystem_sz + device_sz); prb_reserve(&e, prb, &r);

memcpy(r.text_buf, text, text_len);
r.info->text_len = text_len;

/* guaranteed ext data */
r.ext_info->ts_boot = time_boot();
r.ext_info->ts_real = time_real();

/* optional ext data */

if (r.dict_buf) {
    p = r.dict_buf;

    memcpy(p, process, process_sz);
    r.ext_info->process = p;
    p += process_sz;

    memcpy(p, subsystem, subsystem_sz);
    r.ext_info->subsystem = p;
    p += subsystem_sz;

    memcpy(p, device, device_sz);
    r.ext_info->device = p;

    r.info->dict_len = process_sz + subsystem_sz + device_sz; }
Quoted 7 lines

And I am not comfortable even with the current dictionary handling. I already wrote this somewhere. The following command is supposed to show all kernel messages printed by "pci" subsystem:

$> journalctl _KERNEL_SUBSYSTEM=pci

It will be incomplete when the dictionary metadata were not saved.

In that case, perhaps @subsystem should be a static array in printk_ext_info instead.

Quoted 3 lines

Regarding the waste of space. The dict ring currently has the same size as the text ring. It is likely a waste of space as well. Any tuning is complicated because it depends on the use case.

The whole point of the dict_ring is that it allows for variable length _optional_ data to be stored. If we decide there is no optional data, then dict_ring is not needed.

Quoted 3 lines

The advantage of the fixed @ext_info[] array is that everything is clear, simple, and predictable (taken space and name length limits). We could easily tell users what they will get for a given cost.

Quoted 8 lines

Agreed. For non-optional data (such as your timestamps), I am in full agreement that a fixed array is the way to go. And it would only require a couple lines of code added to the ringbuffer.

My concern is if we need to guarantee space for all possible dictionary data of all records. I think the dict_ring can be very helpful here.

John Ogness

To be honest, This patch is made based on kernel 5.4. So I haven't fully figured out the printk ring buffer yet. When I see the code, dict_buf didn't print out into prefix. Right? I want to print out the information into prefix.

When I developed the kernel in Android,
we could modify prefix of kernel log format. Because we should be able to obtain a lot of information with just a kernel log.
But now, we can't modify the prefix due to GKI. So We need a printk where we can freely modify the prefix.
The prefix doesn't need to be saved to log_buf. It only needs to be printed out.

It would be great if printk.format is possible.

printk.format=ts,cpu,comm,pid,in_atomic

Changki Kim.

maintainer
#14 lore raw
In reply to John Ogness
Quoted 58 lines · John Ogness
Quoted 33 lines · Petr Mladek
Quoted 33 lines
Quoted 33 lines

I am currently playing with support for all three timestamps based on https://lore.kernel.org/lkml/[email protected]/

And I got the following idea:

1. Storing side:

Create one more ring/array for storing the optional metadata. It might eventually replace dict ring, see below.

struct struct printk_ext_info {
u64 ts_boot; /* timestamp from boot clock */
u64 ts_real; /* timestamp from real clock */
char process[TASK_COMM_LEN]; /* process name */
};

It must be in a separate array so that struct prb_desc stay stable and crashdump tools do not need to be updated so often.

   But the number of these structures must be the same as descriptors.
   So it might be:

   struct prb_desc_ring {
	unsigned int		count_bits;
	struct prb_desc		*descs;
	struct printk_ext_info  *ext_info
	atomic_long_t		head_id;
	atomic_long_t		tail_id;
   };

   One huge advantage is that these extra information would not block
   pushing lockless printk buffer upstream.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

It might be even possible to get rid of dict ring and just add two more elements into struct printk_ext_info:

char subsystem[16]; /* for SUBSYSTEM= dict value */
char device[48]; /* for DEVICE= dict value */

From my POV, if we support 3 timestamps then they must be stored reliably. And dict ring is out of the game.

Agreed. I am just trying to think of how to better manage the strings, which currently are rare and optional. That is where the dict_ring becomes interesting.

Perhaps we should use both the fixed structs with the variable dict_ring. printk_ext_info could look like this:

struct struct printk_ext_info {
    u64 ts_boot;
    u64 ts_real;
    char *process;
    char *subsystem;
    char *device;
};
Quoted 2 lines

And @process, @subsystem, @device could all point to null-terminated trings within the dict_ring. So printk.c code looks something like this:

Yeah, it would be possible. But I would prefer to start with something simple. We could always make it more complicated when people complains.

This extra metadata are not currently read by crashdump tools. It should be much easier to change them at least in the near future.

Quoted 3 lines

The whole point of the dict_ring is that it allows for variable length _optional_ data to be stored. If we decide there is no optional data, then dict_ring is not needed.

I know but they are currently used only for subsystem and device names.

I tried to find what are the longest subsystem and device names. They are limited already now by the buffer for formatting the dictionary in dev_printk_emit():

	char hdr[128];

and create_syslog_header() prints a warning when the buffer is not enoung:

	dev_WARN(dev, "device/subsystem name too long");

Google has shown only one syscaller report with this error message, see
https://lore.kernel.org/lkml/[email protected]/

First, dev_WARN() looks like an overhead. Simple printk() should be enough. Anyway, this type of error is not a reason for panicing the system.

Second, I believe that limit device name to 48 characters should work well in practice. Note that it is not clear how many people, if any, is using the journalctl filtering. And shrinked device name should be better than missing one.

I personally would prefer to replace the dict ring with the array. Bot solutions are not fully reliable. But the array is easier and we would need it for the timestamps, cpu number, and maybe some other metadata anyway.

We could always add the dict ring back again when there is a good use for it.

Best Regards,
Petr

maintainer
#16 lore raw
Quoted 5 lines · John Ogness
Quoted 1 line · Petr Mladek

This extra metadata are not currently read by crashdump tools.

crash [0] prints dictionary data.
[0] https://github.com/crash-utility/crash

Grr, I have missed this. It makes things more complicated.

We need to find a way that would allow to add more metadata without breaking support for the existing ones.

I know, it works with the current dictionary. "crash" just prints it the same way as "cat /dev/kmsg". Dict entries are printed on separate lines. It is better than nothing. But it is not much user friendly.

If we add more metadata, people would want to see it some more userfriendly way, e.g.:

	[2020-08-20T07:58:48][  T136] random: crng init done

instead of

	[    1.394073] random: crng init done
	 PID = 136
	 CPU = 3
	 IRQ_CONTEXT = 0;
	 TS_MONO = 234632423
	 TS_REAL = 1599211091487260162
	 [...]

For this, "crash" would need to understand the different variables stored in dictionary. So, it would become harder to change the format as well.

Anyway, "crash" could get the information about struct printk_ext_info via VMCOREINFO_STRUCT_SIZE(), VMCOREINFO_OFFSET() macros. We could extend the structure if we do not change the format of the existing items.

I am not 100% sure that the array of struct printk_ext_info is the best solution. But I still somehow like it more than dict ring. IMHO, it is more reliable and much easier to work with on the kernel side.

Best Regards,
Petr

maintainer
#17 lore raw

[POC] printk: Convert dict ring into array

In reply to Petr Mladek

This is POC how the printk() code would look when dict ring gets converted into an array of extended info structures.

It applies on top of the patchset ("[PATCH printk v4 0/6]
printk: reimplement LOG_CONT handling"), see
https://lore.kernel.org/r/[email protected]

It compiles and even seems to work. But it would need quite some love and discussion to get ready for merging.

Below is the description of the patch:

The dictionary information is not stored when there is not enough space in the dict ring. It makes perfect sense. The message itself is more important than its extra meta data.

Though, there are requests to store and show even more metadata, for example, timestamp from real clock, CPU, task name.

If these metadata can get dropped then they are are unreliable for use. For example, for filtering, correlation with time stamps from user space logs.

It brings many questions and problems how to:

+ tune the size
+ substitute missing values in output

Another problem is that storing them in the current format, (key=value) is not ideal:

+ repeated key strings for each message
+ storing numbers as text
+ values need to get parsed and numbers converted back

A solution would be to store the dictionary in a binary form. It would be either some universal format or some structure.

There is only small step to replace the dict ring with an array of structures. It would have several advantages:

+ guaranteed space for each message
+ easy access
+ preserving the original type of the value

It has also few drawbacks:

+ limited lengths of stored string values
+ unused space when a string is short or missing; normal printk() vs. dev_printk()
+ any structure changes might break crashdump tools

Well, there is only small step to bundle these values into the existing struct printk_info.

The main motivation to store these extra information separately is to make them optional for crashdump tools. It might be able to show the most important data even when the structure changes.

---
 drivers/base/core.c               |  45 +++----
 include/linux/dev_printk.h        |   8 ++
 include/linux/printk.h            |   6 +-
 kernel/printk/internal.h          |   4 +-
 kernel/printk/printk.c            | 181 ++++++++++++++-------------
 kernel/printk/printk_ringbuffer.c | 201 ++++++++++--------------------
 kernel/printk/printk_ringbuffer.h |  72 +++++------
 kernel/printk/printk_safe.c       |   2 +-
 8 files changed, 222 insertions(+), 297 deletions(-)
drivers/base/core.c +15 -30
index 05d414e9e8a4..4fd81bc06bbc 100644
@@ -3835,22 +3835,21 @@ void device_shutdown(void)
  */
 
 #ifdef CONFIG_PRINTK
-static int
-create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
+static void
+set_dev_info(const struct device *dev, struct dev_printk_info *dev_info)
 {
 	const char *subsys;
-	size_t pos = 0;
+
+	memset(dev_info, 0, sizeof(*dev_info));
 
 	if (dev->class)
 		subsys = dev->class->name;
 	else if (dev->bus)
 		subsys = dev->bus->name;
 	else
-		return 0;
+		return;
 
-	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
-	if (pos >= hdrlen)
-		goto overflow;
+	snprintf(dev_info->subsystem, sizeof(dev_info->subsystem), subsys);
 
 	/*
 	 * Add device identifier DEVICE=:
@@ -3866,41 +3865,27 @@ create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
 			c = 'b';
 		else
 			c = 'c';
-		pos++;
-		pos += snprintf(hdr + pos, hdrlen - pos,
-				"DEVICE=%c%u:%u",
-				c, MAJOR(dev->devt), MINOR(dev->devt));
+		snprintf(dev_info->device, sizeof(dev_info->device),
+			 "%c%u:%u", c, MAJOR(dev->devt), MINOR(dev->devt));
 	} else if (strcmp(subsys, "net") == 0) {
 		struct net_device *net = to_net_dev(dev);
 
-		pos++;
-		pos += snprintf(hdr + pos, hdrlen - pos,
-				"DEVICE=n%u", net->ifindex);
+		snprintf(dev_info->device, sizeof(dev_info->device),
+			"n%u", net->ifindex);
 	} else {
-		pos++;
-		pos += snprintf(hdr + pos, hdrlen - pos,
-				"DEVICE=+%s:%s", subsys, dev_name(dev));
+		snprintf(dev_info->device, sizeof(dev_info->device),
+			"+%s:%s", subsys, dev_name(dev));
 	}
-
-	if (pos >= hdrlen)
-		goto overflow;
-
-	return pos;
-
-overflow:
-	dev_WARN(dev, "device/subsystem name too long");
-	return 0;
 }
 
 int dev_vprintk_emit(int level, const struct device *dev,
 		     const char *fmt, va_list args)
 {
-	char hdr[128];
-	size_t hdrlen;
+	struct dev_printk_info dev_info;
 
-	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
+	set_dev_info(dev, &dev_info);
 
-	return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
+	return vprintk_emit(0, level, &dev_info, fmt, args);
 }
 EXPORT_SYMBOL(dev_vprintk_emit);
 
include/linux/dev_printk.h +8 -0
index 3028b644b4fb..6f009559ee54 100644
@@ -21,6 +21,14 @@
 
 struct device;
 
+#define PRINTK_INFO_SUBSYSTEM_LEN	16
+#define PRINTK_INFO_DEVICE_LEN		48
+
+struct dev_printk_info {
+	char subsystem[PRINTK_INFO_SUBSYSTEM_LEN];
+	char device[PRINTK_INFO_DEVICE_LEN];
+};
+
 #ifdef CONFIG_PRINTK
 
 __printf(3, 0) __cold
include/linux/printk.h +4 -2
index 34c1a7be3e01..55f1bf47b0f8 100644
@@ -159,10 +159,12 @@ static inline void printk_nmi_direct_enter(void) { }
 static inline void printk_nmi_direct_exit(void) { }
 #endif /* PRINTK_NMI */
 
+struct dev_printk_info;
+
 #ifdef CONFIG_PRINTK
-asmlinkage __printf(5, 0)
+asmlinkage __printf(4, 0)
 int vprintk_emit(int facility, int level,
-		 const char *dict, size_t dictlen,
+		 const struct dev_printk_info *dev_info,
 		 const char *fmt, va_list args);
 
 asmlinkage __printf(1, 0)
kernel/printk/internal.h +2 -2
index 660f9a6bf73a..3a8fd491758c 100644
@@ -14,9 +14,9 @@
 
 extern raw_spinlock_t logbuf_lock;
 
-__printf(5, 0)
+__printf(4, 0)
 int vprintk_store(int facility, int level,
-		  const char *dict, size_t dictlen,
+		  const struct dev_printk_info *dev_info,
 		  const char *fmt, va_list args);
 
 __printf(1, 0) int vprintk_default(const char *fmt, va_list args);
kernel/printk/printk.c +96 -85

451 lines not on this page · raw message

kernel/printk/printk_ringbuffer.c +68 -133

439 lines not on this page · raw message

kernel/printk/printk_ringbuffer.h +28 -44

202 lines not on this page · raw message

kernel/printk/printk_safe.c +1 -1
index 50aeae770434..5dbc40160990 100644
@@ -375,7 +375,7 @@ __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
 	    raw_spin_trylock(&logbuf_lock)) {
 		int len;
 
-		len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
+		len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
 		raw_spin_unlock(&logbuf_lock);
 		defer_console_output();
 		return len;
Signature · 1 line
2.26.2
maintainer
#18 lore raw

Re: [POC] printk: Convert dict ring into array

Quoted 9 lines · Petr Mladek

This is POC how the printk() code would look when dict ring gets converted into an array of extended info structures.

It applies on top of the patchset ("[PATCH printk v4 0/6]
printk: reimplement LOG_CONT handling"), see
https://lore.kernel.org/r/[email protected]

It compiles and even seems to work. But it would need quite some love and discussion to get ready for merging.

[...]

Quoted 2 lines

Well, there is only small step to bundle these values into the existing struct printk_info.

My opinion:

I would like to go this way in the long term because it looks like the most easy and reliable solution. But I do _not_ want to block pushing printk rework [0][1] into mainline because of this.

Handling more information for each stored message is tangential to the printk rework. It would make it easier for crashdump related tools when the log buffer format is not modified many times. But it might block the rework indefinitely.

The lockless ringbuffer is almost ready for merging into mainline. It will be a huge change on its own. The printk rework is very important effort. It is cleaning up the printk design and helps with many historical problems, like deadlocks or soft-lockups.

I do not want to complicate it much more with adding more meta information and yet another format change that would deserve more investigation and discussion.

[0] https://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git/log/?h=printk-rework
[1] https://lore.kernel.org/r/[email protected]

Best Regards,
Petr

maintainer
#19 lore raw

Re: [POC] printk: Convert dict ring into array

Quoted 2 lines · Petr Mladek

I would like to go this way in the long term because it looks like the most easy and reliable solution.

This change is technically trivial and I don't have any problems with it. Actually it follows the agreement made at the meeting in Lisbon [0] of "support for printk dictionaries will be discontinued".

So there will be no more dictionaries, but subsystem and device information will continue to exist as extended record data.

John Ogness

[0] https://lkml.kernel.org/r/[email protected]