kdb: Switch kdb_printf to use safer console poll APIs
RFC 1/1 kdb: Switch kdb_printf to use safer console poll APIs
In kgdb NMI context, polling driver APIs are more safer to use instead of console APIs since the polling drivers know they will execute from all sorts of crazy places. And for the most common use cases this would also result in no console handler ever being called. So switch to use polling driver APIs in case a particular console supports polling mode.
--- kernel/debug/kdb/kdb_io.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)
kernel/debug/kdb/kdb_io.c +33 -6
@@ -24,6 +24,7 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/kallsyms.h> +#include <linux/tty_driver.h> #include "kdb_private.h" #define CMD_BUFLEN 256
@@ -699,11 +700,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ; - ++oops_in_progress; - c->write (c, cp, retlen - (cp - kdb_buffer)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) { + len = retlen - (cp - kdb_buffer); + cp2 = cp; +while (len--) { + p->ops->poll_put_char (p, line, *cp2); + cp2++; + } + }else { + ++oops_in_progress; + c->write (c, cp, retlen - (cp - kdb_buffer)); + --oops_in_progress; + }touch_nmi_watchdog (); } }
@@ -765,11 +779,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ; - ++oops_in_progress; - c->write (c, moreprompt,strlen (moreprompt)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) { + len =strlen (moreprompt); + cp = moreprompt; +while (len--) { + p->ops->poll_put_char (p, line, *cp); + cp++; + } + }else { + ++oops_in_progress; + c->write (c, moreprompt,strlen (moreprompt)); + --oops_in_progress; + }touch_nmi_watchdog (); }
Signature · 1 line
2.7.4
Quoted 5 lines · Sumit Garg
In kgdb NMI context, polling driver APIs are more safer to use instead of console APIs since the polling drivers know they will execute from all sorts of crazy places. And for the most common use cases this would also result in no console handler ever being called. So switch to use polling driver APIs in case a particular console supports polling mode.
This comment seems rather half hearted, not least because it doesn't explain what the current problem is nor why using the polling API is safer.
Compare the above against the advice in
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
and I think it comes up short. Perhaps also consider Ingo Molnar's much more concise suggestion on describing changes:
: Please use the customary changelog style we use in the kernel:
: " Current code does (A), this has a problem when (B).
: We can improve this doing (C), because (D)."
-- http://lkml.iu.edu/hypermail//linux/kernel/1311.1/01157.html
Quoted 22 lines
Suggested-by Daniel Thompson <[email protected]>Signed-off-by Sumit Garg <[email protected]>--- kernel/debug/kdb/kdb_io.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)kernel/debug/kdb/kdb_io.c +1
-0@@ -24,6 +24,7 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/kallsyms.h> +#include <linux/tty_driver.h> #include "kdb_private.h" #define CMD_BUFLEN 256@@ -699,11 +700,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) {
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 709
Quoted 10 lines
kernel/debug/kdb/kdb_io.c +5
-3+int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ; - ++oops_in_progress; - c->write (c, cp, retlen - (cp - kdb_buffer)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) {
What prevents this logic from matching an active console that hasn't been selected as the polling driver?
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 715
Quoted 6 lines
kernel/debug/kdb/kdb_io.c +6
-0+ len = retlen - (cp - kdb_buffer); + cp2 = cp; +while (len--) { + p->ops->poll_put_char (p, line, *cp2); + cp2++; + }
Assuming it is possible to identify the console that matches the currently selected polling driver can't we just drop the is_console test and get rid of this branch entirely.
The only reason for the is_console test is to avoid issuing messages twice so if we are able to suppress the c->write() for the same UART then is_console check becomes pointless and can go.
Quoted 23 lines
kernel/debug/kdb/kdb_io.c +11
-3+ }else { + ++oops_in_progress; + c->write (c, cp, retlen - (cp - kdb_buffer)); + --oops_in_progress; + }touch_nmi_watchdog (); } }@@ -765,11 +779,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ; - ++oops_in_progress; - c->write (c, moreprompt,strlen (moreprompt)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) { + len =strlen (moreprompt);
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 799
Quoted 10 lines
kernel/debug/kdb/kdb_io.c +10
-0+ cp = moreprompt; +while (len--) { + p->ops->poll_put_char (p, line, *cp); + cp++; + } + }else { + ++oops_in_progress; + c->write (c, moreprompt,strlen (moreprompt)); + --oops_in_progress; + }
Maybe also consider pulling the string emit to a separate function.
Daniel.
Quoted 8 lines · Daniel Thompson
Quoted 5 lines · Sumit Garg
In kgdb NMI context, polling driver APIs are more safer to use instead of console APIs since the polling drivers know they will execute from all sorts of crazy places. And for the most common use cases this would also result in no console handler ever being called. So switch to use polling driver APIs in case a particular console supports polling mode.
Quoted 4 lines
This comment seems rather half hearted, not least because it doesn't explain what the current problem is nor why using the polling API is safer.
TBH, some sentences in the above comment were borrowed from your suggestion here [1]. But I agree that it doesn't portray the complete picture. So how about:
====
In kgdb NMI context, calling console handlers isn't safe due to locks used in those handlers which could lead to a deadlock. Although, using oops_in_progress increases the chance to bypass locks in most console handlers but it might not be sufficient enough in case a console uses more locks (VT/TTY is good example).
So instead switch to use lockless polling driver APIs in case a particular console supports polling mode which is common for most kdb use-cases and would result in no console handler ever being called.
====
[1] https://lkml.org/lkml/2020/5/20/356
Quoted 9 lines
Compare the above against the advice in
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
and I think it comes up short. Perhaps also consider Ingo Molnar's much more concise suggestion on describing changes:: Please use the customary changelog style we use in the kernel:
: " Current code does (A), this has a problem when (B).
: We can improve this doing (C), because (D)."
-- http://lkml.iu.edu/hypermail//linux/kernel/1311.1/01157.html
Thanks for the pointers.
Quoted 29 lines
Quoted 29 lines
Suggested-by Daniel Thompson <[email protected]>Signed-off-by Sumit Garg <[email protected]>--- kernel/debug/kdb/kdb_io.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)kernel/debug/kdb/kdb_io.c +4
-0@@ -24,6 +24,7 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/kallsyms.h> +#include <linux/tty_driver.h> #include "kdb_private.h" #define CMD_BUFLEN 256@@ -699,11 +700,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ;
Quoted 8 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 709
Quoted 5 lines
kernel/debug/kdb/kdb_io.c +2
-3- ++oops_in_progress; - c->write (c, cp, retlen - (cp - kdb_buffer)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) {What prevents this logic from matching an active console that hasn't been selected as the polling driver?
Yes you are correct and it could lead to invoking poll_put_char() without poll_init(). And we couldn't invoke poll_init() here as that still comes with locks and could sleep. So one way to overcome this would be to pass selected polling driver via dbg_io_ops and use polling APIs only if the underlying console driver matches that polling driver.
Quoted 9 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 715
Quoted 8 lines
kernel/debug/kdb/kdb_io.c +6
-0+ len = retlen - (cp - kdb_buffer); + cp2 = cp; +while (len--) { + p->ops->poll_put_char (p, line, *cp2); + cp2++; + }
Quoted 3 lines
Assuming it is possible to identify the console that matches the currently selected polling driver can't we just drop the
is_consoletest and get rid of this branch entirely.
Have a look at my suggested approach above.
Quoted 4 lines
The only reason for the
is_consoletest is to avoid issuing messages twice so if we are able to suppress thec->write()for the same UART thenis_consolecheck becomes pointless and can go.
I did consider removing is_console check but it looks like it's not only limited to polling drivers but also used at other places (see [1] [2]) as well.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/early/ehci-dbgp.c#n1061
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/tty/serial/kgdb_nmi.c#n48
Quoted 29 lines
Quoted 29 lines
kernel/debug/kdb/kdb_io.c +15
-3+ }else { + ++oops_in_progress; + c->write (c, cp, retlen - (cp - kdb_buffer)); + --oops_in_progress; + }touch_nmi_watchdog (); } }@@ -765,11 +779,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ; - ++oops_in_progress; - c->write (c, moreprompt,strlen (moreprompt)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) { + len =strlen (moreprompt); + cp = moreprompt; +while (len--) { + p->ops->poll_put_char (p, line, *cp); + cp++;
Quoted 9 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 799
Quoted 6 lines
kernel/debug/kdb/kdb_io.c +6
-0+ } + }else { + ++oops_in_progress; + c->write (c, moreprompt,strlen (moreprompt)); + --oops_in_progress; + }Maybe also consider pulling the string emit to a separate function.
Okay.
-Sumit
Quoted 2 lines
Daniel.
Quoted 26 lines · Sumit Garg
Quoted 8 lines · Daniel Thompson
Quoted 5 lines · Sumit Garg
In kgdb NMI context, polling driver APIs are more safer to use instead of console APIs since the polling drivers know they will execute from all sorts of crazy places. And for the most common use cases this would also result in no console handler ever being called. So switch to use polling driver APIs in case a particular console supports polling mode.
Quoted 4 lines
This comment seems rather half hearted, not least because it doesn't explain what the current problem is nor why using the polling API is safer.
TBH, some sentences in the above comment were borrowed from your suggestion here [1]. But I agree that it doesn't portray the complete picture. So how about:
====
In kgdb NMI context, calling console handlers isn't safe due to locks used in those handlers which could lead to a deadlock. Although, usingoops_in_progressincreases the chance to bypass locks in most console handlers but it might not be sufficient enough in case a console uses more locks (VT/TTY is good example).
Quoted 4 lines
So instead switch to use lockless polling driver APIs in case a particular console supports polling mode which is common for most kdb use-cases and would result in no console handler ever being called.
====
Better, although the later paragraph still seems rather vague to me. Compare to:
Currently when a driver provides both polling I/O and a console then kdb will output using the console. We can increase robustness by using the currently active polling I/O driver (which should be lockless) instead of the corresponding console. For several common cases (e.g. an embedded system with a single serial port that is used both for console output and debugger I/O) this will result in no console handler being used.
Quoted 52 lines
[1] https://lkml.org/lkml/2020/5/20/356
Quoted 9 lines
Compare the above against the advice in
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
and I think it comes up short. Perhaps also consider Ingo Molnar's much more concise suggestion on describing changes:: Please use the customary changelog style we use in the kernel:
: " Current code does (A), this has a problem when (B).
: We can improve this doing (C), because (D)."
-- http://lkml.iu.edu/hypermail//linux/kernel/1311.1/01157.htmlThanks for the pointers.
Quoted 29 lines
Quoted 29 lines
Suggested-by Daniel Thompson <[email protected]>Signed-off-by Sumit Garg <[email protected]>--- kernel/debug/kdb/kdb_io.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)kernel/debug/kdb/kdb_io.c +4
-0@@ -24,6 +24,7 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/kallsyms.h> +#include <linux/tty_driver.h> #include "kdb_private.h" #define CMD_BUFLEN 256@@ -699,11 +700,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ;Quoted 8 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 709
Quoted 5 lines
kernel/debug/kdb/kdb_io.c +2
-3- ++oops_in_progress; - c->write (c, cp, retlen - (cp - kdb_buffer)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) {What prevents this logic from matching an active console that hasn't been selected as the polling driver?
Quoted 6 lines
Yes you are correct and it could lead to invoking
poll_put_char()withoutpoll_init(). And we couldn't invokepoll_init()here as that still comes with locks and could sleep. So one way to overcome this would be to pass selected polling driver viadbg_io_opsand use polling APIs only if the underlying console driver matches that polling driver.
Agree.
Note that this is all I ever expected to look at when I commented about before.
Quoted 18 lines
Quoted 10 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 715
Quoted 6 lines
kernel/debug/kdb/kdb_io.c +6
-0+ len = retlen - (cp - kdb_buffer); + cp2 = cp; +while (len--) { + p->ops->poll_put_char (p, line, *cp2); + cp2++; + }Assuming it is possible to identify the console that matches the currently selected polling driver can't we just drop the
is_consoletest and get rid of this branch entirely.Have a look at my suggested approach above.
Quoted 4 lines
The only reason for the
is_consoletest is to avoid issuing messages twice so if we are able to suppress thec->write()for the same UART thenis_consolecheck becomes pointless and can go.
Quoted 6 lines
I did consider removing
is_consolecheck but it looks like it's not only limited to polling drivers but also used at other places (see [1] [2]) as well.[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/early/ehci-dbgp.c#n1061
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/tty/serial/kgdb_nmi.c#n48
IIUC you mean that the logic to match devices only works for tty drivers and there examples are not tty drivers.
This could probably be solved but no need to get too tied in knots. It's fine to keep the is_console check for now.
However rather than replicate the polled I/O write code a third and fourth time lets get the I/O logic pulled out into proper functions.
Daniel.
Quoted 36 lines · Daniel Thompson
Quoted 26 lines · Sumit Garg
Quoted 8 lines · Daniel Thompson
Quoted 5 lines · Sumit Garg
In kgdb NMI context, polling driver APIs are more safer to use instead of console APIs since the polling drivers know they will execute from all sorts of crazy places. And for the most common use cases this would also result in no console handler ever being called. So switch to use polling driver APIs in case a particular console supports polling mode.
Quoted 4 lines
This comment seems rather half hearted, not least because it doesn't explain what the current problem is nor why using the polling API is safer.
TBH, some sentences in the above comment were borrowed from your suggestion here [1]. But I agree that it doesn't portray the complete picture. So how about:
====
In kgdb NMI context, calling console handlers isn't safe due to locks used in those handlers which could lead to a deadlock. Although, usingoops_in_progressincreases the chance to bypass locks in most console handlers but it might not be sufficient enough in case a console uses more locks (VT/TTY is good example).Quoted 4 lines
So instead switch to use lockless polling driver APIs in case a particular console supports polling mode which is common for most kdb use-cases and would result in no console handler ever being called.
====Better, although the later paragraph still seems rather vague to me. Compare to:
Quoted 8 lines
Currently when a driver provides both polling I/O and a console then kdb will output using the console. We can increase robustness by using the currently active polling I/O driver (which should be lockless) instead of the corresponding console. For several common cases (e.g. an embedded system with a single serial port that is used both for console output and debugger I/O) this will result in no console handler being used.
Looks good, will use it instead.
Quoted 60 lines
Quoted 53 lines
[1] https://lkml.org/lkml/2020/5/20/356
Quoted 9 lines
Compare the above against the advice in
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
and I think it comes up short. Perhaps also consider Ingo Molnar's much more concise suggestion on describing changes:: Please use the customary changelog style we use in the kernel:
: " Current code does (A), this has a problem when (B).
: We can improve this doing (C), because (D)."
-- http://lkml.iu.edu/hypermail//linux/kernel/1311.1/01157.htmlThanks for the pointers.
Quoted 29 lines
Quoted 29 lines
Suggested-by Daniel Thompson <[email protected]>Signed-off-by Sumit Garg <[email protected]>--- kernel/debug/kdb/kdb_io.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-)kernel/debug/kdb/kdb_io.c +4
-0@@ -24,6 +24,7 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/kallsyms.h> +#include <linux/tty_driver.h> #include "kdb_private.h" #define CMD_BUFLEN 256@@ -699,11 +700,24 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) } }for_each_console (c) { +int line; +struct tty_driver *p; +if (!(c->flags &CON_ENABLED ))continue ;Quoted 8 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 709
Quoted 5 lines
kernel/debug/kdb/kdb_io.c +2
-3- ++oops_in_progress; - c->write (c, cp, retlen - (cp - kdb_buffer)); - --oops_in_progress; + p = c->device ? c->device (c, &line) :NULL ; +if (p && p->ops && p->ops->poll_put_char) {What prevents this logic from matching an active console that hasn't been selected as the polling driver?
Quoted 6 lines
Yes you are correct and it could lead to invoking
poll_put_char()withoutpoll_init(). And we couldn't invokepoll_init()here as that still comes with locks and could sleep. So one way to overcome this would be to pass selected polling driver viadbg_io_opsand use polling APIs only if the underlying console driver matches that polling driver.
Quoted 4 lines
Agree.
Note that this is all I ever expected to look at when I commented about before.
Okay.
Quoted 24 lines
Quoted 24 lines
Quoted 9 lines
kernel/debug/kdb/kdb_io.c int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) · line 715
Quoted 8 lines
kernel/debug/kdb/kdb_io.c +6
-0+ len = retlen - (cp - kdb_buffer); + cp2 = cp; +while (len--) { + p->ops->poll_put_char (p, line, *cp2); + cp2++; + }Quoted 3 lines
Assuming it is possible to identify the console that matches the currently selected polling driver can't we just drop the
is_consoletest and get rid of this branch entirely.Have a look at my suggested approach above.
Quoted 4 lines
The only reason for the
is_consoletest is to avoid issuing messages twice so if we are able to suppress thec->write()for the same UART thenis_consolecheck becomes pointless and can go.I did consider removing
is_consolecheck but it looks like it's not only limited to polling drivers but also used at other places (see [1] [2]) as well.
Quoted 9 lines
Quoted 2 lines
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/early/ehci-dbgp.c#n1061
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/tty/serial/kgdb_nmi.c#n48IIUC you mean that the logic to match devices only works for tty drivers and there examples are not tty drivers.
This could probably be solved but no need to get too tied in knots. It's fine to keep the
is_consolecheck for now.
Okay.
Quoted 3 lines
However rather than replicate the polled I/O write code a third and fourth time lets get the I/O logic pulled out into proper functions.
Sure, will do the refactoring.
-Sumit
Quoted 3 lines
Daniel.