Skip to contentSkip to conversation

console: Don't perform test for CON_BRL flag

PATCH In review Andy Shevchenko 3 participants 10 messages Feb 3, 2020 – Feb 11, 2020

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

PATCH v5 3/7 console: Use for_each_console() helper in unregister_console()

In reply to Andy Shevchenko

We have rather open coded single linked list manipulations where we may simple use for_each_console() helper with properly set exit conditions.

Replace open coded single-linked list handling with for_each_console() helper in use.

---
v5: no change

 kernel/printk/printk.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/printk/printk.c +5 -6
index 21b1a5d5e5b0..c63338df76cf 100644
@@ -2816,7 +2816,7 @@ EXPORT_SYMBOL(register_console);
 
 int unregister_console(struct console *console)
 {
-        struct console *a, *b;
+	struct console *con;
 	int res;
 
 	pr_info("%sconsole [%s%d] disabled\n",
@@ -2832,11 +2832,10 @@ int unregister_console(struct console *console)
 	if (console_drivers == console) {
 		console_drivers=console->next;
 		res = 0;
-	} else if (console_drivers) {
-		for (a=console_drivers->next, b=console_drivers ;
-		     a; b=a, a=b->next) {
-			if (a == console) {
-				b->next = a->next;
+	} else {
+		for_each_console(con) {
+			if (con->next == console) {
+				con->next = console->next;
 				res = 0;
 				break;
 			}
Signature · 1 line
2.24.1
#4 lore raw

PATCH v5 5/7 console: Avoid positive return code from unregister_console()

In reply to Andy Shevchenko

There are only two callers that use the returned code from unregister_console():

- unregister_early_console() in arch/m68k/kernel/early_printk.c
- kgdb_unregister_nmi_console() in drivers/tty/serial/kgdb_nmi.c

They both expect to get "0" on success and a non-zero value on error. But the current behavior is confusing and buggy:

- _braille_unregister_console() returns "1" on success
- unregister_console() returns "1" on error

Fix and clean up the behavior:

- Return success when _braille_unregister_console() succeeded
- Return a meaningful error code when the console was not registered before

---
v5: no change

 kernel/printk/printk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/printk/printk.c +4 -2
index 463ed5c5474e..5fdae891a4cd 100644
@@ -2823,10 +2823,12 @@ int unregister_console(struct console *console)
 		console->name, console->index);
 
 	res = _braille_unregister_console(console);
-	if (res)
+	if (res < 0)
 		return res;
+	if (res > 0)
+		return 0;
 
-	res = 1;
+	res = -ENODEV;
 	console_lock();
 	if (console_drivers == console) {
 		console_drivers=console->next;
Signature · 1 line
2.24.1
#3 lore raw

PATCH v5 4/7 console: Drop misleading comment

In reply to Andy Shevchenko
	/* find the last or real console */

This comment is misleading. The purpose of the loop is to check if we are trying to register boot console when one had been registered before.

---
v5: no change

 kernel/printk/printk.c | 1 -
 1 file changed, 1 deletion(-)
kernel/printk/printk.c +0 -1
index c63338df76cf..463ed5c5474e 100644
@@ -2668,7 +2668,6 @@ void register_console(struct console *newcon)
 	 * already have a valid console
 	 */
 	if (newcon->flags & CON_BOOT) {
-		/* find the last or real console */
 		for_each_console(bcon) {
 			if (!(bcon->flags & CON_BOOT)) {
 				pr_info("Too late to register bootconsole %s%d\n",
Signature · 1 line
2.24.1
#6 lore raw

PATCH v5 7/7 console: Introduce ->exit() callback

In reply to Andy Shevchenko

Some consoles might require special operations on unregistering. For instance, serial console, when registered in the kernel, keeps power on for entire time, until it gets unregistered. Example of use:

	->setup(console):
		pm_runtime_get(...);

	->exit(console):
		pm_runtime_put(...);

For such cases to have a balance we would provide ->exit() callback.

---
v5: Return error code from ->exit() (Sergey)

 include/linux/console.h | 1 +
 kernel/printk/printk.c  | 3 +++
 2 files changed, 4 insertions(+)
include/linux/console.h +1 -0
index f33016b3a401..7a140f4e5d0c 100644
@@ -148,6 +148,7 @@ struct console {
 	struct tty_driver *(*device)(struct console *, int *);
 	void	(*unblank)(void);
 	int	(*setup)(struct console *, char *);
+	int	(*exit)(struct console *);
 	int	(*match)(struct console *, char *name, int idx, char *options);
 	short	flags;
 	short	index;
kernel/printk/printk.c +3 -0
index abd9b92ae0e3..43b5cb88c607 100644
@@ -2860,6 +2860,9 @@ int unregister_console(struct console *console)
 	console_unlock();
 	console_sysfs_notify();
 
+	if (console->exit)
+		res = console->exit(console);
+
 	return res;
 
 out_disable_unlock:
Signature · 1 line
2.24.1
#5 lore raw

PATCH v5 6/7 console: Don't notify user space when unregister non-listed console

In reply to Andy Shevchenko

If console is not on the list then there is nothing for us to do and sysfs notify is pointless.

Note, that nr_ext_console_drivers is being changed only for listed consoles.

---
v5: new patch

 kernel/printk/printk.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/printk/printk.c +11 -1
index 5fdae891a4cd..abd9b92ae0e3 100644
@@ -2843,7 +2843,10 @@ int unregister_console(struct console *console)
 		}
 	}
 
-	if (!res && (console->flags & CON_EXTENDED))
+	if (res)
+		goto out_disable_unlock;
+
+	if (console->flags & CON_EXTENDED)
 		nr_ext_console_drivers--;
 
 	/*
@@ -2856,6 +2859,13 @@ int unregister_console(struct console *console)
 	console->flags &= ~CON_ENABLED;
 	console_unlock();
 	console_sysfs_notify();
+
+	return res;
+
+out_disable_unlock:
+	console->flags &= ~CON_ENABLED;
+	console_unlock();
+
 	return res;
 }
 EXPORT_SYMBOL(unregister_console);
Signature · 1 line
2.24.1
#1 lore raw

PATCH v5 2/7 console: Drop double check for console_drivers being non-NULL

In reply to Andy Shevchenko

There is no need to explicitly check for console_drivers to be non-NULL since for_each_console() does this.

---
v5: no change

 kernel/printk/printk.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
kernel/printk/printk.c +6 -10
index 815739eb8406..21b1a5d5e5b0 100644
@@ -1779,9 +1779,6 @@ static void call_console_drivers(const char *ext_text, size_t ext_len,
 
 	trace_console_rcuidle(text, len);
 
-	if (!console_drivers)
-		return;
-
 	for_each_console(con) {
 		if (exclusive_console && con != exclusive_console)
 			continue;
@@ -2660,18 +2657,17 @@ void register_console(struct console *newcon)
 	struct console_cmdline *c;
 	static bool has_preferred;
 
-	if (console_drivers)
-		for_each_console(bcon)
-			if (WARN(bcon == newcon,
-					"console '%s%d' already registered\n",
-					bcon->name, bcon->index))
-				return;
+	for_each_console(bcon) {
+		if (WARN(bcon == newcon, "console '%s%d' already registered\n",
+					 bcon->name, bcon->index))
+			return;
+	}
 
 	/*
 	 * before we register a new CON_BOOT console, make sure we don't
 	 * already have a valid console
 	 */
-	if (console_drivers && newcon->flags & CON_BOOT) {
+	if (newcon->flags & CON_BOOT) {
 		/* find the last or real console */
 		for_each_console(bcon) {
 			if (!(bcon->flags & CON_BOOT)) {
Signature · 1 line
2.24.1
#0 lore raw

PATCH v5 1/7 console: Don't perform test for CON_BRL flag

We don't call braille_register_console() without CON_BRL flag set. And the _braille_unregister_console() already tests for console to have CON_BRL flag. No need to repeat this in braille_unregister_console().

Drop the repetitive checks from Braille console driver.

---
v5: no change

 drivers/accessibility/braille/braille_console.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/accessibility/braille/braille_console.c +0 -4
index 1339c586bf64..a8f7c278b691 100644
@@ -347,8 +347,6 @@ int braille_register_console(struct console *console, int index,
 {
 	int ret;
 
-	if (!(console->flags & CON_BRL))
-		return 0;
 	if (!console_options)
 		/* Only support VisioBraille for now */
 		console_options = "57600o8";
@@ -371,8 +369,6 @@ int braille_unregister_console(struct console *console)
 {
 	if (braille_co != console)
 		return -EINVAL;
-	if (!(console->flags & CON_BRL))
-		return 0;
 	unregister_keyboard_notifier(&keyboard_notifier_block);
 	unregister_vt_notifier(&vt_notifier_block);
 	braille_co = NULL;
Signature · 1 line
2.24.1
Reviewed-by on v5 1/7
#7 lore raw
Quoted 9 lines · Andy Shevchenko

We don't call braille_register_console() without CON_BRL flag set. And the _braille_unregister_console() already tests for console to have CON_BRL flag. No need to repeat this in braille_unregister_console().

Drop the repetitive checks from Braille console driver.

Looks good to me overall

	-ss
maintainer
#8 lore raw
Quoted 6 lines · Sergey Senozhatsky
Quoted 5 lines · Andy Shevchenko

We don't call braille_register_console() without CON_BRL flag set. And the _braille_unregister_console() already tests for console to have CON_BRL flag. No need to repeat this in braille_unregister_console().

Quoted 8 lines
Quoted 4 lines

Drop the repetitive checks from Braille console driver.

Looks good to me overall

The entire patchset has been commited into printk.git, branch for-5.7-console-exit

Best Regards,
Petr

#9 lore raw
Quoted 12 lines · Petr Mladek
Quoted 11 lines · Sergey Senozhatsky
Quoted 9 lines · Andy Shevchenko

We don't call braille_register_console() without CON_BRL flag set. And the _braille_unregister_console() already tests for console to have CON_BRL flag. No need to repeat this in braille_unregister_console().

Drop the repetitive checks from Braille console driver.

Quoted 6 lines
Quoted 3 lines

Looks good to me overall

The entire patchset has been commited into printk.git, branch for-5.7-console-exit

Thank you, Petr!

Signature · 2 lines
With Best Regards,
Andy Shevchenko