Skip to contentSkip to conversation

fix double printing with earlycon

printk Sergey Senozhatsky 1 participant 1 message Apr 11, 2017 Printk

lore
Share & tools
lore
#0 lore raw

Hello Fengguang, Xiaolong

I've noticed that the serial output you sometimes provide has repeating lines (double lines). Is this intentional? we are about to push a patch set to the linux-next that fixes "double printing" issue and I'm wondering will it break anything on your side. Can you please test it?

1/3  https://marc.info/?l=linux-kernel&m=148957402019729&w=2
2/3  https://marc.info/?l=linux-kernel&m=148957403319736&w=2
3/3  https://marc.info/?l=linux-kernel&m=149142366508431&w=2

I've also attached the patches.

	-ss
Quoted 1 line

From 68a432d0f48243eab14f19aefa93b1a6194121d5 Mon Sep 17 00:00:00 2001

From: Aleksey Makarov <[email protected]>
Date: Wed, 15 Mar 2017 13:28:50 +0300
Subject: [PATCH 1/3] printk: fix name/type/scope of preferred_console var

The variable preferred_console is used only inside register_console() and its semantics is boolean. It is negative when no console has been made preferred.

Make it static bool and rename to has_preferred.

Renaming was suggested by Peter Hurley

---
 kernel/printk/printk.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/printk/printk.c +6 -6
index 2984fb0f0257..80bc1b72d03d 100644
@@ -271,7 +271,6 @@ static struct console *exclusive_console;
 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
 
 static int selected_console = -1;
-static int preferred_console = -1;
 int console_set_on_cmdline;
 EXPORT_SYMBOL(console_set_on_cmdline);
 
@@ -2409,6 +2408,7 @@ void register_console(struct console *newcon)
 	unsigned long flags;
 	struct console *bcon = NULL;
 	struct console_cmdline *c;
+	static bool has_preferred;
 
 	if (console_drivers)
 		for_each_console(bcon)
@@ -2435,15 +2435,15 @@ void register_console(struct console *newcon)
 	if (console_drivers && console_drivers->flags & CON_BOOT)
 		bcon = console_drivers;
 
-	if (preferred_console < 0 || bcon || !console_drivers)
-		preferred_console = selected_console;
+	if (!has_preferred || bcon || !console_drivers)
+		has_preferred = selected_console >= 0;
 
 	/*
 	 *	See if we want to use this console driver. If we
 	 *	didn't select a console we take the first one
 	 *	that registers here.
 	 */
-	if (preferred_console < 0) {
+	if (!has_preferred) {
 		if (newcon->index < 0)
 			newcon->index = 0;
 		if (newcon->setup == NULL ||
@@ -2451,7 +2451,7 @@ void register_console(struct console *newcon)
 			newcon->flags |= CON_ENABLED;
 			if (newcon->device) {
 				newcon->flags |= CON_CONSDEV;
-				preferred_console = 0;
+				has_preferred = true;
 			}
 		}
 	}
@@ -2486,7 +2486,7 @@ void register_console(struct console *newcon)
 		newcon->flags |= CON_ENABLED;
 		if (i == selected_console) {
 			newcon->flags |= CON_CONSDEV;
-			preferred_console = selected_console;
+			has_preferred = true;
 		}
 		break;
 	}
Signature · 1 line
2.12.2
Quoted 1 line

From d2a59b52dd1e2167219a10d0d1e850da72d5e7cf Mon Sep 17 00:00:00 2001

From: Aleksey Makarov <[email protected]>
Date: Wed, 15 Mar 2017 13:28:51 +0300
Subject: [PATCH 2/3] printk: rename selected_console -> preferred_console

The variable selected_console is set in __add_preferred_console() to point to the last console parameter that was added to the console_cmdline array.

Rename it to preferred_console so that the name reflects the usage.

Petr Mladek:
"[..] the selected_console/preferred_console
value is used to keep the console first in the console_drivers list. IMHO, the main effect is that each line will first appear on this console, see call_console_drivers(). But the message will still appear also on all other enabled consoles. From this point, the name "preferred" sounds better to me. More consoles are selected (enabled) and only one is preferred (first)."

---
 kernel/printk/printk.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/printk/printk.c +5 -5
index 80bc1b72d03d..fd752f0c8ef1 100644
@@ -270,7 +270,7 @@ static struct console *exclusive_console;
 
 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
 
-static int selected_console = -1;
+static int preferred_console = -1;
 int console_set_on_cmdline;
 EXPORT_SYMBOL(console_set_on_cmdline);
 
@@ -1910,14 +1910,14 @@ static int __add_preferred_console(char *name, int idx, char *options,
 	     i++, c++) {
 		if (strcmp(c->name, name) == 0 && c->index == idx) {
 			if (!brl_options)
-				selected_console = i;
+				preferred_console = i;
 			return 0;
 		}
 	}
 	if (i == MAX_CMDLINECONSOLES)
 		return -E2BIG;
 	if (!brl_options)
-		selected_console = i;
+		preferred_console = i;
 	strlcpy(c->name, name, sizeof(c->name));
 	c->options = options;
 	braille_set_options(c, brl_options);
@@ -2436,7 +2436,7 @@ void register_console(struct console *newcon)
 		bcon = console_drivers;
 
 	if (!has_preferred || bcon || !console_drivers)
-		has_preferred = selected_console >= 0;
+		has_preferred = preferred_console >= 0;
 
 	/*
 	 *	See if we want to use this console driver. If we
@@ -2484,7 +2484,7 @@ void register_console(struct console *newcon)
 		}
 
 		newcon->flags |= CON_ENABLED;
-		if (i == selected_console) {
+		if (i == preferred_console) {
 			newcon->flags |= CON_CONSDEV;
 			has_preferred = true;
 		}
Signature · 1 line
2.12.2
Quoted 1 line

From d2a0dffbffa18f0022544132637552e5bc69e8e1 Mon Sep 17 00:00:00 2001

From: Aleksey Makarov <[email protected]>
Date: Wed, 5 Apr 2017 23:20:00 +0300
Subject: [PATCH 3/3] printk: fix double printing with earlycon

If a console was specified by ACPI SPCR table _and_ command line parameters like "console=ttyAMA0" _and_ "earlycon" were specified, then log messages appear twice.

The root cause is that the code traverses the list of specified consoles (the console_cmdline array) and stops at the first match. But it may happen that the same console is referred by the elements of this array twice:

	pl011,mmio,0x87e024000000,115200 -- from SPCR
	ttyAMA0 -- from command line

but in this case preferred_console points to the second entry and the flag CON_CONSDEV is not set, so bootconsole is not deregistered.

To fix that, introduce an invariant "The last non-braille console is always the preferred one" on the entries of the console_cmdline array. Then traverse it in reverse order to be sure that if the console is preferred then it will be the first matching entry. Introduce variable console_cmdline_cnt that keeps the number of elements of the console_cmdline array (Petr Mladek). It helps to get rid of the loop that searches for the end of this array.

---
 kernel/printk/printk.c | 48 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 10 deletions(-)
kernel/printk/printk.c +38 -10

74 lines not on this page · raw message

Signature · 1 line
2.12.2