Skip to contentSkip to conversation

x86/earlyprintk: setup serial earlyprintk as early as possible

PATCH Superseded Alexander Kuleshov 3 participants 19 messages Jun 9, 2015 – Jun 10, 2015

Subsystems (2)

v11 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v11 cover x86/earlyprintk: setup serial earlyprintk as early as possible

The early_printk function is usable only after the setup_early_printk will be executed. We pass 'earlyprintk' through the kernel command line. So, it means that earlyprintk will be usable only after the 'parse_early_param' will be executed or in another words earlyprintk is usable only during early boot, kernel decompression and after call of the parse_early_param. But there are many stuff after the kernel decompression and before the parse_early_param will be called as memblock usage, early cpu initialization, early ioremap initialization and etc... So earlyprintk allows us to see what's going on there.

Patchset was tested for both x86 and x86_64 architectures with the qemu and real hadware.

It provides call of the earlyprintk function right after the early initialization of the serial console in the arch/x86/kernel/head{32,64.c}, so it must prints the debug string.

These patchset provides following changes:

Move handling of the builtin command line to the separate function from the setup_arch. Now we can call it from the arch/x86/kernel/head{32,64}.c, and find 'earlyprintk' kernel command line paramter there.

Provide setup_serial_console function to setup serial earlyprintk in the arch/x86/kernel/head{32,64}.c

Changelog:

v11:
* setup_log_buf moved to the arch/x86/kernel/head{32,64.c} from
the arch/x86/kernel/setup.c to setup early log_buf for the earlyprintl
* Update log_buf in the early_printk function
* Added additional patch for testing earlyprintk

v10:
* Removed style issues which are not related to the patchset.

v9:
* Add call of the lockdep_init to the arch/x86/kernel/head{32,64}.c
before the serial console initialization to prevent:

[ 0.000000] WARNING: lockdep init error! lock-(console_sem).lock was acquiredbefore lockdep_init
[ 0.000000] Call stack leading to lockdep invocation was:
[ 0.000000] [] save_stack_trace+0x2f/0x50
[ 0.000000] [] __lock_acquire+0xa2c/0xf00
[ 0.000000] [] lock_acquire+0xdb/0x2b0
[ 0.000000] [] _raw_spin_lock_irqsave+0x53/0x90
[ 0.000000] [] down+0x16/0x50
[ 0.000000] [] console_lock+0x19/0x60
[ 0.000000] [] register_console+0x116/0x350
[ 0.000000] [] setup_early_printk+0x165/0x467
[ 0.000000] [] setup_early_serial_console+0x56/0x58
[ 0.000000] [] x86_64_start_kernel+0xce/0x110
[ 0.000000] [] 0xffffffffffffffff
[ 0.000000] ------------------------

during early serial console initialization.

* Add additional check to the earlyprintk initialization to protect double initialization
of the early_serial_console.
* Fixed comment.

v8:
* Fixed warning with the definition of the setup_early_serial_console in the
arch/x86/include/asm/setup.h

v7:
* Move setup_early_serial_console to the the arch/x86/include/setup.h
* Add ifdefs to prevent setup_serial_console if CONFIG_EARLY_PRINTK is not set.

v6:
* Style fixes.
* Call of the suetp_builtin_cmdline moved to the separate patch.

v5:
* Call setup_builtin_cmdline instead of setup_cmdline

v4:
* Move setup_early_serial_console from the include/linux/printk.h to the
arch/x86/include/asm/serial.h, because this function is only for x86 now.

v3:
* Call setup_cmdline before setup_early_printk;
* setup_early_printk call wrapped with the setup_early_serial_console which
checks that 'serial' given to the earlyprintk command line option. This prevents call of the setup_early_printk with the given pciserial/dbgp/efi, because they are using early_ioremap.

v2:
* Comment added before the setup_early_printk call;
* Added information about testing to the commit message.

Alexander Kuleshov (5):
x86/setup: introduce setup_bultin_cmdline x86/setup: handle builtin command line as early as possible x86/earlyprintk: Allocate log_buf as early as possible x86/earlyprintk: setup earlyprintk as early as possible x86/earlyprintk: Patch for testing earlyprintk

 arch/x86/include/asm/setup.h   |  7 +++++++
 arch/x86/kernel/early_printk.c | 33 ++++++++++++++++++++++++++++++---
 arch/x86/kernel/head32.c       | 10 ++++++++++
 arch/x86/kernel/head64.c       | 11 +++++++++++
 arch/x86/kernel/setup.c        | 33 +++++++++++++++++----------------
 init/main.c                    |  2 +-
 kernel/printk/printk.c         |  5 +++++
 7 files changed, 81 insertions(+), 20 deletions(-)
Signature · 1 line
2.4.0.GIT
#1 lore raw

PATCH v11 1/5 x86/setup: introduce setup_bultin_cmdline

This patch introduces the setup_builtin_cmdline function which appends or overrides boot_command_line with the builtin_cmdline if CONFIG_CMDLINE_BOOL is set.

Previously this functional was in the setup_arch, but we need to move it for getting actual command line as early as possible in the arch/x86/kernel/head{32,64}.c for the earlyprintk setup.

---
 arch/x86/include/asm/setup.h |  1 +
 arch/x86/kernel/setup.c      | 30 +++++++++++++++++-------------
 2 files changed, 18 insertions(+), 13 deletions(-)
arch/x86/include/asm/setup.h +1 -0
index f69e06b..59efd0d 100644
@@ -119,6 +119,7 @@ asmlinkage void __init x86_64_start_kernel(char *real_mode);
 asmlinkage void __init x86_64_start_reservations(char *real_mode_data);
 
 #endif /* __i386__ */
+void __init setup_builtin_cmdline(void);
 #endif /* _SETUP */
 #else
 #define RESERVE_BRK(name,sz)				\
arch/x86/kernel/setup.c +17 -13
index d74ac33..edd4857 100644
@@ -845,6 +845,22 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
 	return 0;
 }
 
+void __init setup_builtin_cmdline(void)
+{
+#ifdef CONFIG_CMDLINE_BOOL
+#ifdef CONFIG_CMDLINE_OVERRIDE
+	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
+#else
+	if (builtin_cmdline[0]) {
+		/* append boot loader cmdline to builtin */
+		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
+		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
+	}
+#endif
+#endif
+}
+
 /*
  * Determine if we were loaded by an EFI loader.  If so, then we have also been
  * passed the efi memmap, systab, etc., so we should use these data structures
@@ -973,19 +989,7 @@ void __init setup_arch(char **cmdline_p)
 	bss_resource.start = __pa_symbol(__bss_start);
 	bss_resource.end = __pa_symbol(__bss_stop)-1;
 
-#ifdef CONFIG_CMDLINE_BOOL
-#ifdef CONFIG_CMDLINE_OVERRIDE
-	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
-#else
-	if (builtin_cmdline[0]) {
-		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
-		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
-	}
-#endif
-#endif
-
+	setup_builtin_cmdline();
 	strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
 	*cmdline_p = command_line;
 
Signature · 1 line
2.4.0.GIT
#2 lore raw

Re: [PATCH v11 1/5] x86/setup: introduce setup_bultin_cmdline

Quoted 8 lines · Alexander Kuleshov

This patch introduces the setup_builtin_cmdline function which appends or overrides boot_command_line with the builtin_cmdline if CONFIG_CMDLINE_BOOL is set.

Previously this functional was in the setup_arch, but we need to move it for getting actual command line as early as possible in the arch/x86/kernel/head{32,64}.c for the earlyprintk setup.

This paragraph not related to the changes in the patch. So, remove it or replace by something like "The patch is a preparation for further improvements".

Quoted 67 lines
---
 arch/x86/include/asm/setup.h |  1 +
 arch/x86/kernel/setup.c      | 30 +++++++++++++++++-------------
 2 files changed, 18 insertions(+), 13 deletions(-)
arch/x86/include/asm/setup.h +1 -0
index f69e06b..59efd0d 100644
@@ -119,6 +119,7 @@ asmlinkage void __init x86_64_start_kernel(char *real_mode);
 asmlinkage void __init x86_64_start_reservations(char *real_mode_data);
 
 #endif /* __i386__ */
+void __init setup_builtin_cmdline(void);
 #endif /* _SETUP */
 #else
 #define RESERVE_BRK(name,sz)				\
arch/x86/kernel/setup.c +17 -13
index d74ac33..edd4857 100644
@@ -845,6 +845,22 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
 	return 0;
 }
 
+void __init setup_builtin_cmdline(void)
+{
+#ifdef CONFIG_CMDLINE_BOOL
+#ifdef CONFIG_CMDLINE_OVERRIDE
+	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
+#else
+	if (builtin_cmdline[0]) {
+		/* append boot loader cmdline to builtin */
+		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
+		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
+	}
+#endif
+#endif
+}
+
 /*
  * Determine if we were loaded by an EFI loader.  If so, then we have also been
  * passed the efi memmap, systab, etc., so we should use these data structures
@@ -973,19 +989,7 @@ void __init setup_arch(char **cmdline_p)
 	bss_resource.start = __pa_symbol(__bss_start);
 	bss_resource.end = __pa_symbol(__bss_stop)-1;
 
-#ifdef CONFIG_CMDLINE_BOOL
-#ifdef CONFIG_CMDLINE_OVERRIDE
-	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
-#else
-	if (builtin_cmdline[0]) {
-		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
-		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
-	}
-#endif
-#endif
-
+	setup_builtin_cmdline();
 	strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
 	*cmdline_p = command_line;
 
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#3 lore raw

PATCH v11 2/5 x86/setup: handle builtin command line as early as possible

In reply to Alexander Kuleshov

This patch adds the call of the setup_builtin_cmdline to handle builtin command line before we will setup earlyprintk.

---
 arch/x86/kernel/head32.c | 1 +
 arch/x86/kernel/head64.c | 2 ++
 arch/x86/kernel/setup.c  | 1 -
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/head32.c +1 -0
index 2911ef3..92e8b5f 100644
@@ -31,6 +31,7 @@ static void __init i386_default_early_setup(void)
 
 asmlinkage __visible void __init i386_start_kernel(void)
 {
+	setup_builtin_cmdline();
 	cr4_init_shadow();
 	sanitize_boot_params(&boot_params);
 
arch/x86/kernel/head64.c +2 -0
index 5a46681..1e5f064 100644
@@ -172,6 +172,8 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
 
 	copy_bootdata(__va(real_mode_data));
 
+	setup_builtin_cmdline();
+
 	/*
 	 * Load microcode early on BSP.
 	 */
arch/x86/kernel/setup.c +0 -0
index edd4857..0aeee0a 100644
@@ -989,0 +989,0 @@ void __init setup_arch(char **cmdline_p)
	bss_resource.start = __pa_symbol(__bss_start);

bss_resource.end = __pa_symbol(__bss_stop)-1;

- setup_builtin_cmdline();
strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE); *cmdline_p = command_line;

--
2.4.0.GIT

#4 lore raw

PATCH v11 3/5 x86/earlyprintk: Allocate early log_buf as early as possible

In reply to Alexander Kuleshov

This patch moves call of the early setup_log_buf from the arch/x86/kernel/setup.c to the arch/x86/kernel/head{32,64}.c and updates log_buf with the earlyprintk messages.

We need to do it because without it we will not see earlyprintk messages in the kernel log.

---
 arch/x86/kernel/head32.c | 6 ++++++
 arch/x86/kernel/head64.c | 5 +++++
 arch/x86/kernel/setup.c  | 3 ---
 kernel/printk/printk.c   | 5 +++++
 4 files changed, 16 insertions(+), 3 deletions(-)
arch/x86/kernel/head32.c +0 -0
index 92e8b5f..f28d10f 100644
@@ -32,2 +32,2 @@ static void __init i386_default_early_setup(void)
 asmlinkage __visible void __init i386_start_kernel(void)
 {
	setup_builtin_cmdline();

+
+ lockdep_init();
+
+ /* Allocate early log buffer */
+ setup_log_buf(1);
+
cr4_init_shadow();
sanitize_boot_params(&boot_params);

arch/x86/kernel/head64.c +0 -0
index 1e5f064..53662d2 100644
@@ -174,0 +174,0 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)

setup_builtin_cmdline();

+ lockdep_init();
+
+ /* Allocate early log buffer */
+ setup_log_buf(1);
+
/*
* Load microcode early on BSP.
*/

arch/x86/kernel/setup.c +0 -3
index 0aeee0a..edfdb6a 100644
@@ -1146,9 +1146,6 @@ void __init setup_arch(char **cmdline_p)
 	if (init_ohci1394_dma_early)
 		init_ohci1394_dma_on_all_controllers();
 #endif
-	/* Allocate bigger log buffer */
-	setup_log_buf(1);
-
 	reserve_initrd();
 
 #if defined(CONFIG_ACPI) && defined(CONFIG_BLK_DEV_INITRD)
kernel/printk/printk.c +5 -0
index c099b08..d76a032 100644
@@ -1913,6 +1913,7 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 	va_list ap;
 	char buf[512];
 	int n;
+	printk_func_t vprintk_func;
 
 	if (!early_console)
 		return;
@@ -1922,7 +1923,10 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 	va_end(ap);
 
 	early_console->write(early_console, buf, n);
+
+	/* Store earlyprintk messages in the log_buf */
+	vprintk_func = this_cpu_read(printk_func);
+	vprintk_func(fmt, ap);
 }
 #endif
 
Signature · 1 line
2.4.0.GIT
#5 lore raw

Re: [PATCH v11 3/5] x86/earlyprintk: Allocate early log_buf as early as possible

Quoted 4 lines · Alexander Kuleshov

This patch moves call of the early setup_log_buf from the arch/x86/kernel/setup.c to the arch/x86/kernel/head{32,64}.c and updates log_buf with the earlyprintk messages.

Didn't mention lockdep_init() change. Regarding to what Borislav told you how do you handle double call of lockdep_init()?

Quoted 63 lines

We need to do it because without it we will not see earlyprintk messages in the kernel log.

---
 arch/x86/kernel/head32.c | 6 ++++++
 arch/x86/kernel/head64.c | 5 +++++
 arch/x86/kernel/setup.c  | 3 ---
 kernel/printk/printk.c   | 5 +++++
 4 files changed, 16 insertions(+), 3 deletions(-)
arch/x86/kernel/head32.c +0 -0
index 92e8b5f..f28d10f 100644
@@ -32,2 +32,2 @@ static void __init i386_default_early_setup(void)
 asmlinkage __visible void __init i386_start_kernel(void)
 {
	setup_builtin_cmdline();
arch/x86/kernel/head32.c +6 -0
+
+	lockdep_init();
+
+	/* Allocate early log buffer */
+	setup_log_buf(1);
+
 	cr4_init_shadow();
 	sanitize_boot_params(&boot_params);
arch/x86/kernel/head64.c +0 -0
index 1e5f064..53662d2 100644
@@ -174,0 +174,0 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
arch/x86/kernel/head64.c +5 -0
 	setup_builtin_cmdline();
 
+	lockdep_init();
+
+	/* Allocate early log buffer */
+	setup_log_buf(1);
+
 	/*
 	 * Load microcode early on BSP.
 	 */
arch/x86/kernel/setup.c +0 -3
index 0aeee0a..edfdb6a 100644
@@ -1146,9 +1146,6 @@ void __init setup_arch(char **cmdline_p)
 	if (init_ohci1394_dma_early)
 		init_ohci1394_dma_on_all_controllers();
 #endif
-	/* Allocate bigger log buffer */
-	setup_log_buf(1);
-
 	reserve_initrd();
 
 #if defined(CONFIG_ACPI) && defined(CONFIG_BLK_DEV_INITRD)
kernel/printk/printk.c +0 -0
index c099b08..d76a032 100644

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 1916

Quoted 4 lines
kernel/printk/printk.c +1 -0
@@ -1913,6 +1913,7 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 	va_list ap;
 	char buf[512];
 	int n;
+	printk_func_t vprintk_func;

Rearrange this to be upper a bit, for example before char buf[].

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 1929

Quoted 11 lines
kernel/printk/printk.c +4 -0
 	if (!early_console)
 		return;
@@ -1922,7 +1923,10 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 	va_end(ap);
 
 	early_console->write(early_console, buf, n);
+
+	/* Store earlyprintk messages in the log_buf */
+	vprintk_func = this_cpu_read(printk_func);
+	vprintk_func(fmt, ap);

Shouldn't be

va_start(ap, fmt);
…
vprintk_func(…);
va_end(ap);

?

Also, when you print the same message to the serial and to kernel buffer, do you avoid duplication? Your early_printk messages needs flag LOG_NOCONS if I understand correctly.

Quoted 3 lines
kernel/printk/printk.c +0 -0
 }
 #endif
 
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#6 lore raw

Re: [PATCH v11 3/5] x86/earlyprintk: Allocate early log_buf as early as possible

Quoted 99 lines · Andy Shevchenko
Quoted 4 lines · Alexander Kuleshov

This patch moves call of the early setup_log_buf from the arch/x86/kernel/setup.c to the arch/x86/kernel/head{32,64}.c and updates log_buf with the earlyprintk messages.

Didn't mention lockdep_init() change. Regarding to what Borislav told you how do you handle double call of lockdep_init()?

Quoted 63 lines

We need to do it because without it we will not see earlyprintk messages in the kernel log.

---
 arch/x86/kernel/head32.c | 6 ++++++
 arch/x86/kernel/head64.c | 5 +++++
 arch/x86/kernel/setup.c  | 3 ---
 kernel/printk/printk.c   | 5 +++++
 4 files changed, 16 insertions(+), 3 deletions(-)
arch/x86/kernel/head32.c +0 -0
index 92e8b5f..f28d10f 100644
@@ -32,2 +32,2 @@ static void __init i386_default_early_setup(void)
 asmlinkage __visible void __init i386_start_kernel(void)
 {
	setup_builtin_cmdline();
arch/x86/kernel/head32.c +6 -0
+
+	lockdep_init();
+
+	/* Allocate early log buffer */
+	setup_log_buf(1);
+
 	cr4_init_shadow();
 	sanitize_boot_params(&boot_params);
arch/x86/kernel/head64.c +0 -0
index 1e5f064..53662d2 100644
@@ -174,0 +174,0 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
arch/x86/kernel/head64.c +5 -0
 	setup_builtin_cmdline();
 
+	lockdep_init();
+
+	/* Allocate early log buffer */
+	setup_log_buf(1);
+
 	/*
 	 * Load microcode early on BSP.
 	 */
arch/x86/kernel/setup.c +0 -3
index 0aeee0a..edfdb6a 100644
@@ -1146,9 +1146,6 @@ void __init setup_arch(char **cmdline_p)
 	if (init_ohci1394_dma_early)
 		init_ohci1394_dma_on_all_controllers();
 #endif
-	/* Allocate bigger log buffer */
-	setup_log_buf(1);
-
 	reserve_initrd();
 
 #if defined(CONFIG_ACPI) && defined(CONFIG_BLK_DEV_INITRD)
kernel/printk/printk.c +0 -0
index c099b08..d76a032 100644

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 1916

Quoted 4 lines
kernel/printk/printk.c +1 -0
@@ -1913,6 +1913,7 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 	va_list ap;
 	char buf[512];
 	int n;
+	printk_func_t vprintk_func;

Rearrange this to be upper a bit, for example before char buf[].

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 1929

Quoted 11 lines
kernel/printk/printk.c +4 -0
 	if (!early_console)
 		return;
@@ -1922,7 +1923,10 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 	va_end(ap);
 
 	early_console->write(early_console, buf, n);
+
+	/* Store earlyprintk messages in the log_buf */
+	vprintk_func = this_cpu_read(printk_func);
+	vprintk_func(fmt, ap);

Shouldn't be

va_start(ap, fmt);
…
vprintk_func(…);
va_end(ap);

Quoted 5 lines

?

Also, when you print the same message to the serial and to kernel buffer, do you avoid duplication? Your early_printk messages needs flag LOG_NOCONS if I understand correctly.

Calling printk_func that early - which basically is vprintk_default
- I'm not sure, TBH. He pulls setup_log_buf() up but I'm not sure everything would be kosher that early in the boot. And I don't know the whole "fun" of printk() to make an informed decision here.

Let me add akpm. Andrew, see above (not snipping it). Do you see any problems with snatching that printk_func per-cpu pointer that early and and using it to print to log_buf so that early_printk() calls very early in the boot are visible in dmesg?

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#7 lore raw

Re: [PATCH v11 3/5] x86/earlyprintk: Allocate early log_buf as early as possible

Quoted 9 lines · Andy Shevchenko
Quoted 9 lines · Alexander Kuleshov
Quoted 4 lines

This patch moves call of the early setup_log_buf from the arch/x86/kernel/setup.c to the arch/x86/kernel/head{32,64}.c and updates log_buf with the earlyprintk messages.

Didn't mention lockdep_init() change. Regarding to what Borislav told you how do you handle double call of lockdep_init()?

Double call of the lockdep_init is safe, since it has

if (lockdep_initialized)

                return;

at the beginning. Will update commit message with it.

Quoted 3 lines
Quoted 3 lines

Also, when you print the same message to the serial and to kernel buffer, do you avoid duplication? Your early_printk messages needs flag LOG_NOCONS if I understand correctly.

Yes, just checked. They will be duplicated. Thanks for advice about LOG_NOCONS.

Borislav, I'm really not sure too, that using of printk to update log_buf with the earlyprintk
is a right correct here. So, we can update log_buf with earlyprintk messages with the call
of the

log_store(0, LOGLEVEL_DEFAULT, LOG_NOCONS|LOG_CONT, 0, NULL, 0,

              buf, strlen(buf));

in the early_printk function. What do you think about it?

Do we need to see earlyprintk messages in the dmesg output? Maybe there need to create yet another option, something like:

CONFIG_EARLY_PRINTK_KERNEL_LOG

or something like this?

#8 lore raw

Re: [PATCH v11 3/5] x86/earlyprintk: Allocate early log_buf as early as possible

Quoted 2 lines · Alexander Kuleshov

Borislav, I'm really not sure too, that using of printk to update log_buf with the earlyprintk is a right correct here.

Yes, so this whole approach and what you're trying to achieve seems kinda confusing and wrong. First of all, the early_printk() machinery prints to a special console driver, i.e., I'm looking at the registration fun in setup_early_printk().

So using early_printk() to print to dmesg is the wrong tool for the job. Actually, if you want to do that, you can just as well use plain printk() and try to make it work much earlier. Which is basically what you did by using the printk_func per_cpu ptr, but that was hacky and ugly.

In order to do that right, you need to slow down first, think hard and look hard and long at printk(), log_buf, the statically allocated smaller __log_buf and the whole machinery behind it. Whether it can be used that early or not. And to explain why it can or why it cannot in your commit messages. Then test your stuff a *lot* on the hw you have access to because printk() is not a joke. It needs to be very reliable and to work.

If you don't do your homework and expect other people to do it, you will have a lot less success with your patches.

I sincerely hope that helps.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#9 lore raw

Re: [PATCH v11 3/5] x86/earlyprintk: Allocate early log_buf as early as possible

2015-06-10 15:04 GMT+06:00 Borislav Petkov <[email protected]>:

Quoted 11 lines

Yes, so this whole approach and what you're trying to achieve seems kinda confusing and wrong. First of all, the early_printk() machinery prints to a special console driver, i.e., I'm looking at the registration fun in setup_early_printk().

So using early_printk() to print to dmesg is the wrong tool for the job. Actually, if you want to do that, you can just as well use plain printk() and try to make it work much earlier. Which is basically what you did by using the printk_func per_cpu ptr, but that was hacky and ugly.

Quoted 8 lines

In order to do that right, you need to slow down first, think hard and look hard and long at printk(), log_buf, the statically allocated smaller __log_buf and the whole machinery behind it. Whether it can be used that early or not. And to explain why it can or why it cannot in your commit messages. Then test your stuff a *lot* on the hw you have access to because printk() is not a joke. It needs to be very reliable and to work.

That's right, I thought this too. But when you answered on 10th
(https://lkml.org/lkml/2015/6/8/157) revision that you do not see earlyprintk messages in the dmesg output and I thought that he should be there, but didint research printk/log_buf and related stuff properly.

I will try to learn all earlyprintk related things in more clear way and resend the patch.

Thank you.

#10 lore raw

PATCH v11 4/5 x86/earlyprintk: setup earlyprintk as early as possible

In reply to Alexander Kuleshov

The early_printk function is usable only after the setup_early_printk will be executed. We pass 'earlyprintk' through the kernel command line, so it will be usable only after the 'parse_early_param' will be executed. This means that we have usable earlyprintk only during early boot, kernel decompression and after call of the 'parse_early_param'. This patchset makes earlyprintk usable before the call of the 'parse_early_param'.

This patch makes the setup_early_printk visible for head{32,64}.c. So the 'early_printk' function will be usabable after decompression of the kernel and before parse_early_param will be called. It also must be safe if CONFIG_CMDLINE_BOOL and CONFIG_CMDLINE_OVERRIDE are set, because setup_cmdline function will be called before setup_early_printk.

It provides earlyprintk only for serial console, because other needs in ioremap which is not initialized yet.

Tested it with qemu and real hardware, so early_printk() is usable and prints to serial console right after setup_early_printk function called.

---
 arch/x86/include/asm/setup.h   |  6 ++++++
 arch/x86/kernel/early_printk.c | 33 ++++++++++++++++++++++++++++++---
 arch/x86/kernel/head32.c       |  3 +++
 arch/x86/kernel/head64.c       |  5 ++++-
 4 files changed, 43 insertions(+), 4 deletions(-)
arch/x86/include/asm/setup.h +0 -0
index 59efd0d..180bda4 100644
@@ -120,0 +120,0 @@ asmlinkage void __init x86_64_start_reservations(char *real_mode_data);

#endif /* __i386__ */
void __init setup_builtin_cmdline(void);
+#ifdef CONFIG_EARLY_PRINTK
+/* used by arch/x86/kernel/head{32,64}.c */
+extern int __init setup_early_serial_console(void);
+#else
+static inline int __init setup_early_serial_console(void) { return 0; } +#endif /* CONFIG_EARLY_PRINTK */
#endif /* _SETUP */
#else
#define RESERVE_BRK(name,sz) \

arch/x86/kernel/early_printk.c +30 -3
index 89427d8..6442cd2 100644
@@ -342,19 +342,22 @@ static int __init setup_early_printk(char *buf)
 	keep = (strstr(buf, "keep") != NULL);
 
 	while (*buf != '\0') {
-		if (!strncmp(buf, "serial", 6)) {
+		if (!strncmp(buf, "serial", 6) &&
+			early_serial_console.index == -1) {
 			buf += 6;
 			early_serial_init(buf);
 			early_console_register(&early_serial_console, keep);
 			if (!strncmp(buf, ",ttyS", 5))
 				buf += 5;
 		}
-		if (!strncmp(buf, "ttyS", 4)) {
+		if (!strncmp(buf, "ttyS", 4) &&
+			early_serial_console.index == -1) {
 			early_serial_init(buf + 4);
 			early_console_register(&early_serial_console, keep);
 		}
 #ifdef CONFIG_PCI
-		if (!strncmp(buf, "pciserial", 9)) {
+		if (!strncmp(buf, "pciserial", 9) &&
+			early_serial_console.index == -1) {
 			early_pci_serial_init(buf + 9);
 			early_console_register(&early_serial_console, keep);
 			buf += 9; /* Keep from match the above "serial" */
@@ -385,4 +388,28 @@ static int __init setup_early_printk(char *buf)
 	return 0;
 }
 
+int __init setup_early_serial_console(void)
+{
+	char *arg;
+
+	/*
+	 * make sure that we have:
+	 *      "serial,0x3f8,115200"
+	 *      "serial,ttyS0,115200"
+	 *      "ttyS0,115200"
+	 */
+	arg = strstr(boot_command_line, "earlyprintk=serial");
+	if (!arg)
+		arg = strstr(boot_command_line, "earlyprintk=ttyS");
+	if (!arg)
+		return -1;
+
+	arg = strstr(boot_command_line, "earlyprintk=");
+
+	/* += strlen("earlyprintk="); */
+	arg += 12;
+
+       return setup_early_printk(arg);
+}
+
 early_param("earlyprintk", setup_early_printk);
arch/x86/kernel/head32.c +0 -0
index f28d10f..908ee47 100644
@@ -38,0 +38,0 @@ asmlinkage __visible void __init i386_start_kernel(void)
	/* Allocate early log buffer */

setup_log_buf(1);

+ setup_early_serial_console();
+ early_printk("Early printk is initialized\n");
+
cr4_init_shadow();
sanitize_boot_params(&boot_params);

arch/x86/kernel/head64.c +0 -0
index 53662d2..041be2c 100644
@@ -176,0 +176,0 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
	/* Allocate early log buffer */

setup_log_buf(1);

+ setup_early_serial_console();
+ early_printk("Early printk is initialized\n");
+
/*
* Load microcode early on BSP.
*/

Signature · 1 line
2.4.0.GIT
#11 lore raw

Re: [PATCH v11 4/5] x86/earlyprintk: setup earlyprintk as early as possible

Quoted 13 lines · Alexander Kuleshov

The early_printk function is usable only after the setup_early_printk will be executed. We pass 'earlyprintk' through the kernel command line, so it will be usable only after the 'parse_early_param' will be executed. This means that we have usable earlyprintk only during early boot, kernel decompression and after call of the 'parse_early_param'. This patchset makes earlyprintk usable before the call of the 'parse_early_param'.

This patch makes the setup_early_printk visible for head{32,64}.c. So the 'early_printk' function will be usabable after decompression of the kernel and before parse_early_param will be called. It also must be safe if CONFIG_CMDLINE_BOOL and CONFIG_CMDLINE_OVERRIDE are set, because setup_cmdline function will be called before setup_early_printk.

Quoted 6 lines

It provides earlyprintk only for serial console, because other needs in ioremap which is not initialized yet.

Tested it with qemu and real hardware, so early_printk() is usable and prints to serial console right after setup_early_printk function called.

One topic to discuss below.

Quoted 72 lines
---
 arch/x86/include/asm/setup.h   |  6 ++++++
 arch/x86/kernel/early_printk.c | 33 ++++++++++++++++++++++++++++++---
 arch/x86/kernel/head32.c       |  3 +++
 arch/x86/kernel/head64.c       |  5 ++++-
 4 files changed, 43 insertions(+), 4 deletions(-)
arch/x86/include/asm/setup.h +0 -0
index 59efd0d..180bda4 100644
@@ -120,0 +120,0 @@ asmlinkage void __init x86_64_start_reservations(char *real_mode_data);

#endif /* __i386__ */
void __init setup_builtin_cmdline(void);

arch/x86/include/asm/setup.h +6 -0
+#ifdef CONFIG_EARLY_PRINTK
+/* used by arch/x86/kernel/head{32,64}.c */
+extern int __init setup_early_serial_console(void);
+#else
+static inline int __init setup_early_serial_console(void) { return 0; }
+#endif /* CONFIG_EARLY_PRINTK */
 #endif /* _SETUP */
 #else
 #define RESERVE_BRK(name,sz)				\
arch/x86/kernel/early_printk.c +19 -3
index 89427d8..6442cd2 100644
@@ -342,19 +342,22 @@ static int __init setup_early_printk(char *buf)
 	keep = (strstr(buf, "keep") != NULL);
 
 	while (*buf != '\0') {
-		if (!strncmp(buf, "serial", 6)) {
+		if (!strncmp(buf, "serial", 6) &&
+			early_serial_console.index == -1) {
 			buf += 6;
 			early_serial_init(buf);
 			early_console_register(&early_serial_console, keep);
 			if (!strncmp(buf, ",ttyS", 5))
 				buf += 5;
 		}
-		if (!strncmp(buf, "ttyS", 4)) {
+		if (!strncmp(buf, "ttyS", 4) &&
+			early_serial_console.index == -1) {
 			early_serial_init(buf + 4);
 			early_console_register(&early_serial_console, keep);
 		}
 #ifdef CONFIG_PCI
-		if (!strncmp(buf, "pciserial", 9)) {
+		if (!strncmp(buf, "pciserial", 9) &&
+			early_serial_console.index == -1) {
 			early_pci_serial_init(buf + 9);
 			early_console_register(&early_serial_console, keep);
 			buf += 9; /* Keep from match the above "serial" */
@@ -385,4 +388,28 @@ static int __init setup_early_printk(char *buf)
 	return 0;
 }
 
+int __init setup_early_serial_console(void)
+{
+	char *arg;
+
+	/*
+	 * make sure that we have:
+	 *      "serial,0x3f8,115200"
+	 *      "serial,ttyS0,115200"
+	 *      "ttyS0,115200"
+	 */
+	arg = strstr(boot_command_line, "earlyprintk=serial");
+	if (!arg)
+		arg = strstr(boot_command_line, "earlyprintk=ttyS");

arch/x86/kernel/early_printk.c static int __init setup_early_printk(char *buf) · line 413

Quoted 10 lines
arch/x86/kernel/early_printk.c +10 -0
+	if (!arg)
+		return -1;
+
+	arg = strstr(boot_command_line, "earlyprintk=");
+
+	/* += strlen("earlyprintk="); */
+	arg += 12;
+
+       return setup_early_printk(arg);
+}

I'm still not convincing by this code to be in that form and here. What about to refactor setup_early_printk() to helper which will do parse parameters to a let say structure where one of the flag will be struct early_printk_param {
…
const char *arg;
bool serial;
}

Your function will be something like this

struct early_printk_param epp;

parse_early_printk_param(&epp);

if (!epp->serial)
return /* whatever error code */;

return setup_early_printk(epp.arg);

Quoted 31 lines
arch/x86/kernel/early_printk.c +1 -0
+
 early_param("earlyprintk", setup_early_printk);
arch/x86/kernel/head32.c +0 -0
index f28d10f..908ee47 100644
@@ -38,0 +38,0 @@ asmlinkage __visible void __init i386_start_kernel(void)
	/* Allocate early log buffer */
arch/x86/kernel/head32.c +3 -0
 	setup_log_buf(1);
 
+	setup_early_serial_console();
+	early_printk("Early printk is initialized\n");
+
 	cr4_init_shadow();
 	sanitize_boot_params(&boot_params);
arch/x86/kernel/head64.c +0 -0
index 53662d2..041be2c 100644
@@ -176,0 +176,0 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
	/* Allocate early log buffer */
arch/x86/kernel/head64.c +3 -0
 	setup_log_buf(1);
 
+	setup_early_serial_console();
+	early_printk("Early printk is initialized\n");
+
 	/*
 	 * Load microcode early on BSP.
 	 */
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#12 lore raw

Re: [PATCH v11 4/5] x86/earlyprintk: setup earlyprintk as early as possible

2015-06-09 23:00 GMT+06:00 Andy Shevchenko <[email protected]>:

Quoted 12 lines

I'm still not convincing by this code to be in that form and here. What about to refactor setup_early_printk() to helper which will do parse parameters to a let say structure where one of the flag will be struct early_printk_param {
…
const char *arg;
bool serial;
}

Your function will be something like this

Quoted 9 lines

struct early_printk_param epp;

parse_early_printk_param(&epp);

if (!epp->serial)
return /* whatever error code */;

return setup_early_printk(epp.arg);

Hello Andy,

But what is difference between parsing to string and passing it and parsing to structure and pass its field?

Thank you.

#13 lore raw

Re: [PATCH v11 4/5] x86/earlyprintk: setup earlyprintk as early as possible

Quoted 20 lines · Alexander Kuleshov

2015-06-09 23:00 GMT+06:00 Andy Shevchenko <[email protected]>:

Quoted 19 lines

I'm still not convincing by this code to be in that form and here. What about to refactor setup_early_printk() to helper which will do parse parameters to a let say structure where one of the flag will be struct early_printk_param {
…
const char *arg;
bool serial;
}

Your function will be something like this

struct early_printk_param epp;

parse_early_printk_param(&epp);

if (!epp->serial)
return /* whatever error code */;

Quoted 7 lines
Quoted 2 lines

return setup_early_printk(epp.arg);

Hello Andy,

But what is difference between parsing to string and passing it and parsing to structure and pass its field?

You do parsing twice (still original code and your piece here), and honestly I don't like your approach in this form.

Quoted 2 lines

Thank you.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#14 lore raw

Re: [PATCH v11 4/5] x86/earlyprintk: setup earlyprintk as early as possible

2015-06-10 15:44 GMT+06:00 Andy Shevchenko <[email protected]>:

Quoted 2 lines

You do parsing twice (still original code and your piece here), and honestly I don't like your approach in this form.

I just researched earlyprintk and we can use not only serial, but vga and pciserial. What if I'll rename setup_early_serial_console to the setup_earlyprintk_console, will add variable, something like this

static unsigned int early = 1;

to the arch/x86/kernel/earlyprintk.c and refactor the setup_early_serial_console as:

int __init setup_earlyprintk_console(void)

{
    char *arg;

    arg = strstr(boot_command_line, "earlyprintk=");
    if (!arg)
        return -1;

    early = 0;
    return setup_early_printk(arg);
}

After this we can check in the setup_earlyprintk
is it early to set other consoles or not,

while (*buf != '\0') {
    ....

    #ifdef CONFIG_EARLY_PRINTK_EFI
            if (!strncmp(buf, "efi", 3) && early)
                early_console_register(&early_efi_console, keep);
    #endif
    ....
}

early = 1;

So, when we will call setup_earlyprintk_console from the arch/x86/kernel/head{32,64}.c, early variable will be 0 and it allows us to setup only 'real early' consoles and when setup_earlyprintk will be called by the do_early_param early variable will be one and we will be able to setup efi console and etc....

What do you think about it?

#15 lore raw

Re: [PATCH v11 4/5] x86/earlyprintk: setup earlyprintk as early as possible

2015-06-10 16:36 GMT+06:00 Alexander Kuleshov <[email protected]>:

Quoted 9 lines

2015-06-10 15:44 GMT+06:00 Andy Shevchenko <[email protected]>:

Quoted 2 lines

You do parsing twice (still original code and your piece here), and honestly I don't like your approach in this form.

I just researched earlyprintk and we can use not only serial, but vga and pciserial. What if I'll rename setup_early_serial_console to the setup_earlyprintk_console, will add variable, something like this

We can't use pciserial, accidently hit from cut&paste,

sorry for the noise.

#16 lore raw

PATCH v11 5/5 x86/earlyprintk: Patch for testing earlyprintk

In reply to Alexander Kuleshov
---
 arch/x86/kernel/head64.c | 1 +
 arch/x86/kernel/setup.c  | 1 +
 init/main.c              | 2 +-
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/head64.c +1 -0
index 041be2c..1d1d167 100644
@@ -204,5 +204,6 @@ void __init x86_64_start_reservations(char *real_mode_data)
 
 	reserve_ebda_region();
 
+	early_printk("Go on the start_kernel\n");
 	start_kernel();
 }
arch/x86/kernel/setup.c +1 -0
index edfdb6a..22e8f39 100644
@@ -876,6 +876,7 @@ void __init setup_builtin_cmdline(void)
 
 void __init setup_arch(char **cmdline_p)
 {
+	early_printk("Now setup arch is working\n");
 	memblock_reserve(__pa_symbol(_text),
 			 (unsigned long)__bss_stop - (unsigned long)_text);
 
init/main.c +1 -1
index 2115055..79087b8 100644
@@ -493,7 +493,7 @@ asmlinkage __visible void __init start_kernel(void)
 {
 	char *command_line;
 	char *after_dashes;
-
+	early_printk("start_kernel function started to work\n");
 	/*
 	 * Need to run as early as possible, to initialize the
 	 * lockdep hash:
Signature · 1 line
2.4.0.GIT
#17 lore raw

Re: [PATCH v11 5/5] x86/earlyprintk: Patch for testing earlyprintk

Quoted 1 line · Alexander Kuleshov

Please, mark those patches as something like "[NOT TO APPLY]".

Or you may (if others have no objections) create a configuration option like CONFIG_TEST_EARLY_PRINTK and make this patch upstreamable. See below.

Quoted 11 lines
---
 arch/x86/kernel/head64.c | 1 +
 arch/x86/kernel/setup.c  | 1 +
 init/main.c              | 2 +-
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/head64.c +0 -0
index 041be2c..1d1d167 100644

arch/x86/kernel/head64.c void __init x86_64_start_reservations(char *real_mode_data) · line 207

Quoted 4 lines
arch/x86/kernel/head64.c +1 -0
@@ -204,5 +204,6 @@ void __init x86_64_start_reservations(char *real_mode_data)
 
 	reserve_ebda_region();
 
+	early_printk("Go on the start_kernel\n");

#ifdef CONFIG_TEST_EARLY_PRINTK early_printk("%s: Starting kernel...\n", __func__); #endif

arch/x86/kernel/setup.c void __init setup_builtin_cmdline(void) · line 879

Quoted 11 lines
arch/x86/kernel/head64.c +0 -0
 	start_kernel();
 }
arch/x86/kernel/setup.c +1 -0
index edfdb6a..22e8f39 100644
@@ -876,6 +876,7 @@ void __init setup_builtin_cmdline(void)
 
 void __init setup_arch(char **cmdline_p)
 {
+	early_printk("Now setup arch is working\n");

#ifdef CONFIG_TEST_EARLY_PRINTK
early_printk("%s: Enter\n", __func__);
#endif

init/main.c asmlinkage __visible void __init start_kernel(void) · line 496

Quoted 12 lines
arch/x86/kernel/setup.c +0 -0
 	memblock_reserve(__pa_symbol(_text),
 			 (unsigned long)__bss_stop - (unsigned long)_text);
 
init/main.c +0 -1
index 2115055..79087b8 100644
@@ -493,7 +493,7 @@ asmlinkage __visible void __init start_kernel(void)
 {
 	char *command_line;
 	char *after_dashes;
-

This removal is wrong. Leave empty string after definition block.

init/main.c asmlinkage __visible void __init start_kernel(void) · line 496

Quoted 1 line
init/main.c +1 -0
+	early_printk("start_kernel function started to work\n");

#ifdef CONFIG_TEST_EARLY_PRINTK
early_printk("%s: Enter\n", __func__);
#endif

Quoted 3 lines
init/main.c +0 -0
 	/*
 	 * Need to run as early as possible, to initialize the
 	 * lockdep hash:
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#18 lore raw

Re: [PATCH v11 5/5] x86/earlyprintk: Patch for testing earlyprintk

Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Alexander Kuleshov

Please, mark those patches as something like "[NOT TO APPLY]".

Or you may (if others have no objections) create a configuration option like CONFIG_TEST_EARLY_PRINTK and make this patch upstreamable.

I think this is too much. No need for it - it's not like you can automatically test early printk - you need to go stare at dmesg. And if you want to do that, you can sprinkle some early_printk() calls yourself.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--