console: Don't perform test for CON_BRL flag
PATCH v3 1/5 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.
---
v3: drop checks in Braille console driver (Sergey)
drivers/accessibility/braille/braille_console.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/accessibility/braille/braille_console.c +0 -4
@@ -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
PATCH v3 2/5 console: Drop double check for console_drivers being non-NULL
There is no need to explicitly check for console_drivers to be non-NULL since for_each_console() does this.
---
v3: no changes
kernel/printk/printk.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
kernel/printk/printk.c +6 -10
@@ -1772,9 +1772,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 ;
@@ -2653,18 +2650,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
Re: [PATCH v3 2/5] console: Drop double check for console_drivers being non-NULL
Quoted 4 lines · Andy Shevchenko
There is no need to explicitly check for
console_driversto be non-NULL sincefor_each_console()does this.Signed-off-by Andy Shevchenko <[email protected]>
Nice catch.
There is a candidate for another patch, see the note below.
Quoted 22 lines
---
v3: no changeskernel/printk/printk.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)kernel/printk/printk.c +3
-6@@ -2653,18 +2650,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))
kernel/printk/printk.c void register_console(struct console *newcon) · line 2664
Quoted 10 lines
kernel/printk/printk.c +3
-1+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 */
Note: This comment is misleading. I would just remove it.
kernel/printk/printk.c void register_console(struct console *newcon) · line 2666
Quoted 2 lines
kernel/printk/printk.c +0
-0for_each_console (bcon) {if (!(bcon->flags &CON_BOOT )) {
Best Regards,
Petr
PATCH v3 3/5 console: Use for_each_console() helper in unregister_console()
In reply to Andy ShevchenkoWe 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.
---
v3: no changes
kernel/printk/printk.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/printk/printk.c +5 -6
@@ -2809,7 +2809,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",
@@ -2825,11 +2825,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
Re: [PATCH v3 3/5] console: Use for_each_console() helper in unregister_console()
Quoted 7 lines · 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.Signed-off-by Andy Shevchenko <[email protected]>
Nice simplification.
Best Regards,
Petr
PATCH v3 4/5 console: Avoid positive return code from unregister_console()
In reply to Andy ShevchenkoThere are two callers which use the returned code from unregister_console(). In some cases, i.e. successfully unregistered Braille console or when console has not been enabled the return code is 1. This code is ambiguous and also prevents callers to distinguish successful operation.
Replace this logic to return only negative error codes or 0 when console, either enabled, disabled or Braille has been successfully unregistered.
---
v3: no changes
kernel/printk/printk.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/printk/printk.c +7 -2
@@ -2817,10 +2817,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;
@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--; +if (res && !(console->flags &CON_ENABLED )) + res =0 ; +/* * If this isn't the last console and it has CON_CONSDEV set, we * need to set it on the next preferred console.
Signature · 1 line
2.24.1
Re: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
On (20/01/27 13:47), Andy Shevchenko wrote:
[..]
Quoted 13 lines
kernel/printk/printk.c +4
-2res =_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;
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 5 lines
kernel/printk/printk.c +2
-0@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--; +if (res && !(console->flags &CON_ENABLED )) + res =0 ;
Console is not on the console_drivers list. Why does !ENABLED case require extra handling? What about the case when console is ENABLED but still not registered?
I think that if the console is not on the list (was never registered) then we can just bail out, without console_sysfs_notify(), etc. IOW,
if (res) {
console->flags &= ~CON_ENABLED; /* just in case */
console_unlock();
return res;
}
-ssRe: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
Quoted 18 lines · Sergey Senozhatsky
On (20/01/27 13:47), Andy Shevchenko wrote:
[..]Quoted 16 lines
kernel/printk/printk.c +4
-2res =_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;@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--;
Quoted 5 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 2 lines
kernel/printk/printk.c +2
-0+if (res && !(console->flags &CON_ENABLED )) + res =0 ;Console is not on the
console_driverslist. Why does !ENABLED case require extra handling?
It's mirroring (to some extend) the register_console() abort conditions.
Quoted 2 lines
What about the case when console is ENABLED
but still not registered?
What about when console is ENABLED and we call register_console()? I think you can tell me what to do in these corner cases (however, that's not the point of this series).
Quoted 8 lines
I think that if the console is not on the list (was never registered) then we can just bail out, without
console_sysfs_notify(), etc. IOW,if (res) { console->flags &= ~CON_ENABLED; /* just in case */ console_unlock(); return res; }
Perhaps. But see above. I would rather drop this condition for now for sake of this series being to the point.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
On (20/01/28 11:22), Andy Shevchenko wrote:
[..]
Quoted 9 lines
Quoted 2 lines
Console is not on the
console_driverslist. Why does !ENABLED case require extra handling?It's mirroring (to some extend) the
register_console()abort conditions.Quoted 2 lines
What about the case when console is ENABLED
but still not registered?What about when console is ENABLED and we call
register_console()?
I think that ENABLED bit makes sense only when console is on the list. Otherwise, I suspect, nothing will be able to access the console.
-ss
Re: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
In reply to Andy ShevchenkoQuoted 19 lines · Andy Shevchenko
Quoted 18 lines · Sergey Senozhatsky
On (20/01/27 13:47), Andy Shevchenko wrote:
[..]Quoted 16 lines
kernel/printk/printk.c +4
-2res =_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;@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--;
Quoted 7 lines
Quoted 5 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 2 lines
kernel/printk/printk.c +2
-0+if (res && !(console->flags &CON_ENABLED )) + res =0 ;Console is not on the
console_driverslist. Why does !ENABLED case require extra handling?It's mirroring (to some extend) the
register_console()abort conditions.
Could you please explain?
I see the "newcon->flags & CON_ENABLED" error out path. I'm guessing, that the expectation is that this is how we filter out consoles which were not matched (there is that "newcon->flags |= CON_ENABLED" several lines earlier.) So this looks like the assumption is that consoles don't have CON_ENABLED bit set prior to register_console(), as far as I understand.
Well, look at these
...
drivers/net/netconsole.c: .flags = CON_ENABLED, drivers/tty/ehv_bytechan.c: .flags = CON_PRINTBUFFER | CON_ENABLED,
drivers/tty/serial/mux.c: .flags = CON_ENABLED | CON_PRINTBUFFER,
...
-ss
Re: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
Quoted 28 lines · Sergey Senozhatsky
Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Sergey Senozhatsky
On (20/01/27 13:47), Andy Shevchenko wrote:
[..]Quoted 16 lines
kernel/printk/printk.c +4
-2res =_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;@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--;Quoted 7 lines
Quoted 5 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 2 lines
kernel/printk/printk.c +2
-0+if (res && !(console->flags &CON_ENABLED )) + res =0 ;Console is not on the
console_driverslist. Why does !ENABLED case require extra handling?It's mirroring (to some extend) the
register_console()abort conditions.
Quoted 7 lines
Could you please explain?
I see the "
newcon->flags&CON_ENABLED" error out path. I'm guessing, that the expectation is that this is how we filter out consoles which were not matched (there is that "newcon->flags|=CON_ENABLED" several lines earlier.) So this looks like the assumption is that consoles don't haveCON_ENABLEDbit set prior toregister_console(), as far as I understand.
I put it to cover the case when register_console() fails (since it has no return code caller is not able to say this anyhow) somebody may call unregister_console() on it unconditionally (and I guess many do like this). In such case we shouldn't return an error code.
Quoted 6 lines
Well, look at these
...drivers/net/netconsole.c: .flags = CON_ENABLED, drivers/tty/ehv_bytechan.c: .flags = CON_PRINTBUFFER | CON_ENABLED,
drivers/tty/serial/mux.c: .flags =CON_ENABLED|CON_PRINTBUFFER,
...
The code there (I meant register_console() and unregister_console() and their usage) is quite twisted and probably abused, so, I have definitely miss something.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Petr Mladek
maintainerRe: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
In reply to Andy ShevchenkoQuoted 7 lines · Andy Shevchenko
There are two callers which use the returned code from
unregister_console(). In some cases, i.e. successfully unregistered Braille console or when console has not been enabled the return code is 1. This code is ambiguous and also prevents callers to distinguish successful operation.Replace this logic to return only negative error codes or 0 when console, either enabled, disabled or Braille has been successfully unregistered.
I am quite confused by the above message. It is probably because the patched code is so confusing ;-)
I would start with something like:
<begin>
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. </end>
The above is more or less clear. Now, the question is what behavior is considered as success and what is failure.
I started thinking about this in a paranoid mode. The console registration code is so tricky and it is easy to create regression.
But I think that it is actually not much important. There are only two callers that handle the return code:
+ The 1st one m68k is a late init call and the error code of init calls is ignored.
+ The 2nd one in kdb code is not much important. I wonder if anyone is actually using kdb. If I remember correctly then Linus prosed to remove it completely during the discussion about lockless printk at Plumbers 2019 and nobody was against.
In fact, the kdb code is probably wrong. tty_register_driver() is called before register_console() in kgdb_register_nmi_console() =>
kgdb_unregister_nmi_console() should probably call tty_unregister_driver() even when unregister_console() fails.
unregister_console() is exported symbol but I doubt that the are more users of the error code.
So, I think that we do not need to care about regressions. But it is worth to define some resonable behavior, see below.
kernel/printk/printk.c int unregister_console(struct console *console) · line 2823
Quoted 13 lines
kernel/printk/printk.c +3
-1@@ -2817,10 +2817,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 ;
Sigh, I wish that _braille_unregister_console() did not returned 1 on success but ...
I would describe this as a bugfix. unregister_console() should return success (0) when _braille_unregister_console() succeeds.
kernel/printk/printk.c int unregister_console(struct console *console) · line 2825
Quoted 3 lines
kernel/printk/printk.c +1
-1- res =1 ; + res = -ENODEV ;
I would describe this as using a regular "meaningful" error code.
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 9 lines
kernel/printk/printk.c +2
-0console_lock ();if (console_drivers == console) { console_drivers=console->next;@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--; +if (res && !(console->flags &CON_ENABLED )) + res =0 ;
I personally think that success or failure of unregister_console() should not depend on the state of CON_ENABLED flag:
+ As it was discussed in the other thread. There are few consoles that have set CON_ENABLED by default. unregister_console() should not succeed when register_console() was not called before.
+ This check would open a question if we should return error when the console was in the list but CON_ENABLED was not set. But consoles might be temporary disabled, see console_stop(). unregister_console() should succeed even when the console was temporary stopped.
But I think that this is only theoretical discussion. IMHO, nobody really depends on the return code in reality. Alternative solution would be to make it symetric with register_console() and do not return the error code at all.
Best Regards,
Petr
Re: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
Quoted 11 lines · Petr Mladek
Quoted 7 lines · Andy Shevchenko
There are two callers which use the returned code from
unregister_console(). In some cases, i.e. successfully unregistered Braille console or when console has not been enabled the return code is 1. This code is ambiguous and also prevents callers to distinguish successful operation.Replace this logic to return only negative error codes or 0 when console, either enabled, disabled or Braille has been successfully unregistered.
I am quite confused by the above message. It is probably because the patched code is so confusing ;-)
True, and thanks for the elaboration. Some comments below, nevertheless.
Quoted 11 lines
I would start with something like:
<begin>
There are only two callers that use the returned code fromunregister_console():+
unregister_early_console()inarch/m68k/kernel/early_printk.c
+kgdb_unregister_nmi_console()indrivers/tty/serial/kgdb_nmi.cThey both expect to get "0" on success and a non-zero value on error. </end>
I'll rewrite commit message.
Quoted 7 lines
The above is more or less clear. Now, the question is what behavior is considered as success and what is failure.
I started thinking about this in a paranoid mode. The console registration code is so tricky and it is easy to create regression.
Quoted 5 lines
But I think that it is actually not much important. There are only two callers that handle the return code:
+ The 1st one m68k is a late init call and the error code of init calls is ignored.
That's not fully true. If you pass initcall_debug it will be helpful to see what is failed and what is not.
Quoted 4 lines
+ The 2nd one in kdb code is not much important. I wonder if anyone is actually using kdb. If I remember correctly then Linus prosed to remove it completely during the discussion about lockless printk at Plumbers 2019 and nobody was against.
I agree with Linus, but It's not my area of expertise, for the scope of this series I would rather ignore what it does with returned code and fix it later if anybody complains (probably we won't see any complaint).
Quoted 7 lines
In fact, the kdb code is probably wrong.
tty_register_driver()is called beforeregister_console()inkgdb_register_nmi_console()=>
kgdb_unregister_nmi_console()should probably calltty_unregister_driver()even whenunregister_console()fails.
Quoted 6 lines
unregister_console()is exported symbol but I doubt that the are more users of the error code.So, I think that we do not need to care about regressions. But it is worth to define some resonable behavior, see below.
Agree.
Quoted 17 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2823
Quoted 13 lines
kernel/printk/printk.c +3
-1@@ -2817,10 +2817,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 ;Sigh, I wish that
_braille_unregister_console()did not returned 1 on success but ...
Quoted 2 lines
I would describe this as a bugfix.
unregister_console()should return success (0) when_braille_unregister_console()succeeds.
You mean do a separate patch for it with Fixes tag?
Quoted 4 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2825
Quoted 2 lines
kernel/printk/printk.c +1
-1- res =1 ; + res = -ENODEV ;I would describe this as using a regular "meaningful" error code.
In the commit message? Will do!
Quoted 24 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 9 lines
kernel/printk/printk.c +2
-0console_lock ();if (console_drivers == console) { console_drivers=console->next;@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--; +if (res && !(console->flags &CON_ENABLED )) + res =0 ;I personally think that success or failure of
unregister_console()should not depend on the state ofCON_ENABLEDflag:+ As it was discussed in the other thread. There are few consoles that have set
CON_ENABLEDby default.unregister_console()should not succeed whenregister_console()was not called before.+ This check would open a question if we should return error when the console was in the list but
CON_ENABLEDwas not set. But consoles might be temporary disabled, seeconsole_stop().unregister_console()should succeed even when the console was temporary stopped.
Quoted 4 lines
But I think that this is only theoretical discussion. IMHO, nobody really depends on the return code in reality. Alternative solution would be to make it symetric with
register_console()and do not return the error code at all.
Okay, I understand that for time being it's matter of how eloquent the commit message will be. (And maybe some comments in the code?) Is it correct?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Petr Mladek
maintainerRe: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
Quoted 123 lines · Andy Shevchenko
Quoted 11 lines · Petr Mladek
Quoted 7 lines · Andy Shevchenko
There are two callers which use the returned code from
unregister_console(). In some cases, i.e. successfully unregistered Braille console or when console has not been enabled the return code is 1. This code is ambiguous and also prevents callers to distinguish successful operation.Replace this logic to return only negative error codes or 0 when console, either enabled, disabled or Braille has been successfully unregistered.
I am quite confused by the above message. It is probably because the patched code is so confusing ;-)
True, and thanks for the elaboration. Some comments below, nevertheless.
Quoted 11 lines
I would start with something like:
<begin>
There are only two callers that use the returned code fromunregister_console():+
unregister_early_console()inarch/m68k/kernel/early_printk.c
+kgdb_unregister_nmi_console()indrivers/tty/serial/kgdb_nmi.cThey both expect to get "0" on success and a non-zero value on error. </end>
I'll rewrite commit message.
Quoted 7 lines
The above is more or less clear. Now, the question is what behavior is considered as success and what is failure.
I started thinking about this in a paranoid mode. The console registration code is so tricky and it is easy to create regression.
Quoted 5 lines
But I think that it is actually not much important. There are only two callers that handle the return code:
+ The 1st one m68k is a late init call and the error code of init calls is ignored.
That's not fully true. If you pass
initcall_debugit will be helpful to see what is failed and what is not.Quoted 4 lines
+ The 2nd one in kdb code is not much important. I wonder if anyone is actually using kdb. If I remember correctly then Linus prosed to remove it completely during the discussion about lockless printk at Plumbers 2019 and nobody was against.
I agree with Linus, but It's not my area of expertise, for the scope of this series I would rather ignore what it does with returned code and fix it later if anybody complains (probably we won't see any complaint).
Quoted 7 lines
In fact, the kdb code is probably wrong.
tty_register_driver()is called beforeregister_console()inkgdb_register_nmi_console()=>
kgdb_unregister_nmi_console()should probably calltty_unregister_driver()even whenunregister_console()fails.Quoted 6 lines
unregister_console()is exported symbol but I doubt that the are more users of the error code.So, I think that we do not need to care about regressions. But it is worth to define some resonable behavior, see below.
Agree.
Quoted 17 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2823
Quoted 13 lines
kernel/printk/printk.c +3
-1@@ -2817,10 +2817,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 ;Sigh, I wish that
_braille_unregister_console()did not returned 1 on success but ...Quoted 2 lines
I would describe this as a bugfix.
unregister_console()should return success (0) when_braille_unregister_console()succeeds.You mean do a separate patch for it with Fixes tag?
Quoted 4 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2825
Quoted 2 lines
kernel/printk/printk.c +1
-1- res =1 ; + res = -ENODEV ;I would describe this as using a regular "meaningful" error code.
In the commit message? Will do!
Quoted 24 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2844
Quoted 9 lines
kernel/printk/printk.c +2
-0console_lock ();if (console_drivers == console) { console_drivers=console->next;@@ -2838,6 +2840,9 @@ int unregister_console(struct console *console)if (!res && (console->flags &CON_EXTENDED )) nr_ext_console_drivers--; +if (res && !(console->flags &CON_ENABLED )) + res =0 ;I personally think that success or failure of
unregister_console()should not depend on the state ofCON_ENABLEDflag:+ As it was discussed in the other thread. There are few consoles that have set
CON_ENABLEDby default.unregister_console()should not succeed whenregister_console()was not called before.+ This check would open a question if we should return error when the console was in the list but
CON_ENABLEDwas not set. But consoles might be temporary disabled, seeconsole_stop().unregister_console()should succeed even when the console was temporary stopped.
Quoted 8 lines
Quoted 4 lines
But I think that this is only theoretical discussion. IMHO, nobody really depends on the return code in reality. Alternative solution would be to make it symetric with
register_console()and do not return the error code at all.Okay, I understand that for time being it's matter of how eloquent the commit message will be. (And maybe some comments in the code?) Is it correct?
Good question.
Please, remove the last hunk if Sergey is not against it. I think that the success/error should not depend on the state of CON_ENABLED flag.
The other two changes might stay in the same patch. We just need to make the commit message easier to understand. I would write something like:
<begin>
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.
</end>
Best Regards,
Petr
Re: [PATCH v3 4/5] console: Avoid positive return code from unregister_console()
Quoted 2 lines · Petr Mladek
Quoted 1 line · Andy Shevchenko
On Thu, Jan 30, 2020 at 10:04:29AM +0100, Petr Mladek wrote:
...
Quoted 9 lines
Quoted 3 lines
Okay, I understand that for time being it's matter of how eloquent the commit message will be. (And maybe some comments in the code?) Is it correct?
Good question.
Please, remove the last hunk if Sergey is not against it. I think that the success/error should not depend on the state of
CON_ENABLEDflag.
If I understood his last message correctly, he is exactly in favour of not using it (and thus changing conditional for ->exit() callback to rely only on res value).
Quoted 3 lines
The other two changes might stay in the same patch. We just need to make the commit message easier to understand. I would write something like:
Thanks! Will do this way.
Quoted 19 lines
<begin>
There are only two callers that use the returned code fromunregister_console():+
unregister_early_console()inarch/m68k/kernel/early_printk.c
+kgdb_unregister_nmi_console()indrivers/tty/serial/kgdb_nmi.cThey 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 errorFix and clean up the behavior:
+ Return success when
_braille_unregister_console()succeeded.
+ Return a meaningful error code when the console was not registered before.
</end>
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 5/5 console: Introduce ->exit() callback
In reply to Andy ShevchenkoSome consoles might require special operations on unregistering. For example, serial console, when registered in the kernel, keeps power on for entire time, until it gets unregistered. For such cases to have a balance we would provide ->exit() callback.
---
v3: move callback out from console_lock (Sergey)
include/linux/console.h | 1 + kernel/printk/printk.c | 4 ++++ 2 files changed, 5 insertions(+)
include/linux/console.h +1 -0
@@ -148,6 +148,7 @@ struct console {struct tty_driver *(*device)(struct console *,int *);void (*unblank)(void );int (*setup)(struct console *,char *); +void (*exit)(struct console *);int (*match)(struct console *,char *name,int idx,char *options);short flags;short index;
kernel/printk/printk.c +4 -0
@@ -2853,6 +2853,10 @@ int unregister_console(struct console *console) console->flags &= ~CON_ENABLED ;console_unlock ();console_sysfs_notify (); + +if (console->exit) + console->exit (console); +return res; }EXPORT_SYMBOL (unregister_console);
Signature · 1 line
2.24.1
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
On (20/01/27 13:47), Andy Shevchenko wrote:
[..]
Quoted 14 lines
+++
b/include/linux/console.hinclude/linux/console.h +1
-0@@ -148,6 +148,7 @@ struct console {struct tty_driver *(*device)(struct console *,int *);void (*unblank)(void );int (*setup)(struct console *,char *); +void (*exit)(struct console *);int (*match)(struct console *,char *name,int idx,char *options);short flags;short index;kernel/printk/printk.c +0
-0
kernel/printk/printk.c int unregister_console(struct console *console) · line 2859
Quoted 7 lines
kernel/printk/printk.c +4
-0@@ -2853,6 +2853,10 @@ int unregister_console(struct console *console) console->flags &= ~CON_ENABLED ;console_unlock ();console_sysfs_notify (); + +if (console->exit) + console->exit (console); +
If the console was not registered (hence not enabled) is it still required to call ->exit()? Is there a requirement that ->exit() should handle such cases?
-ss
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 1 line · Sergey Senozhatsky
On (20/01/27 13:47), Andy Shevchenko wrote:
...
Quoted 4 lines
Quoted 4 lines
kernel/printk/printk.c +0
-0@@ -2853,6 +2853,10 @@ int unregister_console(struct console *console) console->flags &= ~CON_ENABLED ;console_unlock ();console_sysfs_notify ();
Quoted 8 lines
kernel/printk/printk.c int unregister_console(struct console *console) · line 2859
Quoted 4 lines
kernel/printk/printk.c +4
-0+ +if (console->exit) + console->exit (console); +If the console was not registered (hence not enabled) is it still required to call ->exit()? Is there a requirement that ->exit() should handle such cases?
This is a good point. The ->exit() purpose is to keep balance for whatever happened at ->setup().
But ->setup() is being called either when we have has_preferred == false or when we got no matching we call it for all such consoles, till it returns an error (can you elaborate the logic behind it?).
In both cases we will get the console to have CON_ENABLED flag set.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 10 lines · Andy Shevchenko
Quoted 3 lines
If the console was not registered (hence not enabled) is it still required to call ->exit()? Is there a requirement that ->exit() should handle such cases?
This is a good point. The ->exit() purpose is to keep balance for whatever happened at ->setup().
But ->setup() is being called either when we have
has_preferred== false or when we got no matching we call it for all such consoles, till it returns an error (can you elaborate the logic behind it?).
->match() does alias matching and ->setup(). If alias matching failed, exact name match takes place. We don't call ->setup() for all consoles, but only for those that have exact name match:
if (strcmp(c->name, newcon->name) != 0) continue;
As to why we don't stop sooner in that loop - I need to to do some archaeology. We need to have CON_CONSDEV at proper place, which is IIRC the last matching console.
Pretty much every time we tried to change the logic we ended up reverting the changes.
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.
And there are sneaky consoles that have CON_ENABLED before we even register them.
-ss
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 19 lines · Sergey Senozhatsky
Quoted 10 lines · Andy Shevchenko
Quoted 3 lines
If the console was not registered (hence not enabled) is it still required to call ->exit()? Is there a requirement that ->exit() should handle such cases?
This is a good point. The ->exit() purpose is to keep balance for whatever happened at ->setup().
But ->setup() is being called either when we have
has_preferred== false or when we got no matching we call it for all such consoles, till it returns an error (can you elaborate the logic behind it?).->match() does alias matching and ->setup(). If alias matching failed, exact name match takes place. We don't call ->setup() for all consoles, but only for those that have exact name match:
if (strcmp(c->name, newcon->name) != 0) continue;
Quoted 6 lines
As to why we don't stop sooner in that loop - I need to to do some archaeology. We need to have
CON_CONSDEVat proper place, which is IIRC the last matching console.Pretty much every time we tried to change the logic we ended up reverting the changes.
I understand. Seems the ->setup() has to be idempotent. We can tell the same for ->exit() in some comment.
Can you describe, btw, struct console in kernel doc format? It will be very helpful!
Quoted 4 lines
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.And there are sneaky consoles that have
CON_ENABLEDbefore we even register them.
So, taking into consideration my comment to the previous patch, what would be suggested guard here?
For a starter something like this?
if ((console->flags & CON_ENABLED) && console->exit)
console->exit(console);
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 5 lines · Andy Shevchenko
I understand. Seems the ->setup() has to be idempotent. We can tell the same for ->exit() in some comment.
Can you describe, btw, struct console in kernel doc format? It will be very helpful!
We probably need some documentation.
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.And there are sneaky consoles that have
CON_ENABLEDbefore we even register them.
Quoted 7 lines
So, taking into consideration my comment to the previous patch, what would be suggested guard here?
For a starter something like this?
if ((
console->flags&CON_ENABLED) &&console->exit)console->exit(console);
This will work if we also add something like this
---
kernel/printk/printk.c +2 -0
@@ -2739,6 +2739,8 @@ void register_console(struct console *newcon) } } + newcon->flags &= ~CON_ENABLED ; +if (console_drivers && console_drivers->flags &CON_BOOT ) bcon = console_drivers;
---
I don't know why some consoles set CON_ENABLED statically.
E.g. drivers/tty/serial/mux.c
static struct console mux_console = {
.name = "ttyB",
.write = mux_console_write,
.device = uart_console_device,
.setup = mux_console_setup,
.flags = CON_ENABLED | CON_PRINTBUFFER,
.index = 0,
.data = &mux_driver,};
No idea...
Such consoles still will have CON_ENABLED even if registration failed and we never ->setup() them.
-ss
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 17 lines · Sergey Senozhatsky
Quoted 5 lines · Andy Shevchenko
I understand. Seems the ->setup() has to be idempotent. We can tell the same for ->exit() in some comment.
Can you describe, btw, struct console in kernel doc format? It will be very helpful!
We probably need some documentation.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.And there are sneaky consoles that have
CON_ENABLEDbefore we even register them.So, taking into consideration my comment to the previous patch, what would be suggested guard here?
Quoted 6 lines
Quoted 4 lines
For a starter something like this?
if ((
console->flags&CON_ENABLED) &&console->exit)console->exit(console);This will work if we also add something like this
No, wait... This will not work, console can be suspended, yet still registered. I think the only criteria is "the console is on the list".
-ss
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 20 lines · Sergey Senozhatsky
Quoted 19 lines · Sergey Senozhatsky
Quoted 5 lines · Andy Shevchenko
I understand. Seems the ->setup() has to be idempotent. We can tell the same for ->exit() in some comment.
Can you describe, btw, struct console in kernel doc format? It will be very helpful!
We probably need some documentation.
Quoted 10 lines
Quoted 4 lines
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.And there are sneaky consoles that have
CON_ENABLEDbefore we even register them.So, taking into consideration my comment to the previous patch, what would be suggested guard here?
For a starter something like this?
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
if ((
console->flags&CON_ENABLED) &&console->exit)console->exit(console);This will work if we also add something like this
No, wait... This will not work, console can be suspended, yet still registered. I think the only criteria is "the console is on the list".
Okay, I guess we need to drop CON_ENABLE bits from this patch and rely on the res value instead.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Petr Mladek
maintainerRe: [PATCH v3 5/5] console: Introduce ->exit() callback
In reply to Andy ShevchenkoQuoted 24 lines · Andy Shevchenko
Quoted 23 lines · Sergey Senozhatsky
Quoted 10 lines · Andy Shevchenko
Quoted 3 lines
If the console was not registered (hence not enabled) is it still required to call ->exit()? Is there a requirement that ->exit() should handle such cases?
This is a good point. The ->exit() purpose is to keep balance for whatever happened at ->setup().
But ->setup() is being called either when we have
has_preferred== false or when we got no matching we call it for all such consoles, till it returns an error (can you elaborate the logic behind it?).->match() does alias matching and ->setup(). If alias matching failed, exact name match takes place. We don't call ->setup() for all consoles, but only for those that have exact name match:
if (strcmp(c->name, newcon->name) != 0) continue;As to why we don't stop sooner in that loop - I need to to do some archaeology. We need to have
CON_CONSDEVat proper place, which is IIRC the last matching console.
Quoted 5 lines
Quoted 2 lines
Pretty much every time we tried to change the logic we ended up reverting the changes.
I understand. Seems the ->setup() has to be idempotent. We can tell the same for ->exit() in some comment.
I believe that ->setup() can succeesfully be called only once. It is tricky like hell:
1st piece:
if (!has_preferred || bcon || !console_drivers) has_preferred = preferred_console >= 0;
note:
+ "has_preferred" is updated here only when it was not "true" before.
+ "has_preferred" is set to "true" here only when "preferred_console" is set in __add_preferred_console()
2nd piece:
+ __add_preferred_console() is called for console defined on the command line. "preferred_console" points to the console defined by the last "console=" parameter.
3rd piece:
+ "has_preferred" is set to "true" later in register_console() when a console with tty binding gets enabled.
4th piece:
+ The code:
/*
* 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 (!has_preferred)
... try to enable the given console
The comment is a bit unclear. The code is used as a fallback when no console was defined on the command line.
Note that "has_preferred" is always true when "preferred_console" was defined via command line, see 2nd piece above.
By other words:
+ The fallback code (4th piece) is called only when "preferred_console" was not defined on the command line.
+ The cycle below matches the given console only when it was defined on the command line.
As a result, I believe that ->setup() could never be called in both paths for the same console. Especially I think that fallback code should not be used when the console was defined on the command line.
I am not 100% sure but I am ready to risk this. Anyway, I think that many ->setup() callbacks are not ready to be successfully called twice.
(Sigh, I have started to clean up this code two years ago. But I have never found time to finish the patchset. It is such a huge mess.)
Quoted 8 lines
Can you describe, btw, struct console in kernel doc format? It will be very helpful!
Quoted 4 lines
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.And there are sneaky consoles that have
CON_ENABLEDbefore we even register them.
Quoted 7 lines
So, taking into consideration my comment to the previous patch, what would be suggested guard here?
For a starter something like this?
if ((
console->flags&CON_ENABLED) &&console->exit)console->exit(console);
I would do:
if (!res && console->exit) console->exit(console);
I mean. I would call ->exit() only when console->setup() succeeded in register_console(). In this case, the console was later added to the console_drivers list.
Best Regards,
Petr
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 3 lines · Petr Mladek
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Sergey Senozhatsky
On (20/01/28 11:44), Andy Shevchenko wrote:
...
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
Quoted 3 lines
If the console was not registered (hence not enabled) is it still required to call ->exit()? Is there a requirement that ->exit() should handle such cases?
This is a good point. The ->exit() purpose is to keep balance for whatever happened at ->setup().
But ->setup() is being called either when we have
has_preferred== false or when we got no matching we call it for all such consoles, till it returns an error (can you elaborate the logic behind it?).->match() does alias matching and ->setup(). If alias matching failed, exact name match takes place. We don't call ->setup() for all consoles, but only for those that have exact name match:
if (strcmp(c->name, newcon->name) != 0) continue;As to why we don't stop sooner in that loop - I need to to do some archaeology. We need to have
CON_CONSDEVat proper place, which is IIRC the last matching console.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Pretty much every time we tried to change the logic we ended up reverting the changes.
I understand. Seems the ->setup() has to be idempotent. We can tell the same for ->exit() in some comment.
I believe that ->setup() can succeesfully be called only once. It is tricky like hell:
Indeed. I think this code is highly starving for comments.
Quoted 55 lines
1st piece:
if (!has_preferred || bcon || !console_drivers) has_preferred = preferred_console >= 0;note:
+ "
has_preferred" is updated here only when it was not "true" before.
+ "has_preferred" is set to "true" here only when "preferred_console" is set in__add_preferred_console()2nd piece:
+
__add_preferred_console()is called for console defined on the command line. "preferred_console" points to the console defined by the last "console=" parameter.3rd piece:
+ "
has_preferred" is set to "true" later inregister_console()when a console with tty binding gets enabled.4th piece:
+ The code:
/*
* 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 (!has_preferred)
... try to enable the given consoleThe comment is a bit unclear. The code is used as a fallback when no console was defined on the command line.
Note that "
has_preferred" is always true when "preferred_console" was defined via command line, see 2nd piece above.By other words:
+ The fallback code (4th piece) is called only when "
preferred_console" was not defined on the command line.+ The cycle below matches the given console only when it was defined on the command line.
As a result, I believe that ->setup() could never be called in both paths for the same console. Especially I think that fallback code should not be used when the console was defined on the command line.
Quoted 7 lines
I am not 100% sure but I am ready to risk this. Anyway, I think that many ->setup() callbacks are not ready to be successfully called twice.
(Sigh, I have started to clean up this code two years ago. But I have never found time to finish the patchset. It is such a huge mess.)
Thanks for the elaboration in such details!
Quoted 16 lines
Quoted 8 lines
Can you describe, btw, struct console in kernel doc format? It will be very helpful!
Quoted 4 lines
Quoted 1 line
In both cases we will get the console to have
CON_ENABLEDflag set.And there are sneaky consoles that have
CON_ENABLEDbefore we even register them.Quoted 7 lines
So, taking into consideration my comment to the previous patch, what would be suggested guard here?
For a starter something like this?
if ((
console->flags&CON_ENABLED) &&console->exit)console->exit(console);
Quoted 8 lines
I would do:
if (!res && console->exit) console->exit(console);I mean. I would call ->exit() only when
console->setup()succeeded inregister_console(). In this case, the console was later added to theconsole_driverslist.
Yes, that is exactly what I meant in previous mails to you.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Petr Mladek
maintainerRe: [PATCH v3 5/5] console: Introduce ->exit() callback
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
Some consoles might require special operations on unregistering. For example, serial console, when registered in the kernel, keeps power on for entire time, until it gets unregistered. For such cases to have a balance we would provide ->exit() callback.
Is there any plan to use this callback, please?
The console init, setup, registration code needs a clean up, definitely. If you plan some rework, I would like to understand the bigger picture before we start adding new callbacks.
Best Regards,
Petr
Re: [PATCH v3 5/5] console: Introduce ->exit() callback
Quoted 11 lines · Petr Mladek
Quoted 4 lines · Andy Shevchenko
Some consoles might require special operations on unregistering. For example, serial console, when registered in the kernel, keeps power on for entire time, until it gets unregistered. For such cases to have a balance we would provide ->exit() callback.
Is there any plan to use this callback, please?
The console init, setup, registration code needs a clean up, definitely. If you plan some rework, I would like to understand the bigger picture before we start adding new callbacks.
Yes, as mentioned in the commit message I would like to use it for balancing runtime PM reference counters in the UART code later on.
It will look like:
->setup(): pm_runtime_get(...); ->exit(): pm_runtime_put(...);
The current operations have no needs to be undone.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
We don't call
braille_register_console()withoutCON_BRLflag set. And the_braille_unregister_console()already tests for console to haveCON_BRLflag. No need to repeat this inbraille_unregister_console().Drop the repetitive checks from Braille console driver.
Signed-off-by Andy Shevchenko <[email protected]>
I wish the braille console support was added a cleaner way.
I am always confused what is the generic code and what is specific for the only supported VisioBraille console. And braile() functions called from _braile() wrappers just add to the confusion.
Anyway, I am fine with this patch.
Best Regards,
Petr