2022-10-19 15:11:42

by John Ogness

[permalink] [raw]
Subject: [PATCH printk v2 26/38] kdb: use srcu console list iterator

Guarantee safe iteration of the console list by using SRCU.

Signed-off-by: John Ogness <[email protected]>
---
kernel/debug/kdb/kdb_io.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index 550fe8b456ec..5c0bd93c3574 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -545,6 +545,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
{
struct console *c;
const char *cp;
+ int cookie;
int len;

if (msg_len == 0)
@@ -558,7 +559,8 @@ static void kdb_msg_write(const char *msg, int msg_len)
cp++;
}

- for_each_console(c) {
+ cookie = console_srcu_read_lock();
+ for_each_console_srcu(c) {
if (!console_is_enabled(c))
continue;
if (c == dbg_io_ops->cons)
@@ -577,6 +579,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
--oops_in_progress;
touch_nmi_watchdog();
}
+ console_srcu_read_unlock(cookie);
}

int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
--
2.30.2


2022-10-25 03:08:40

by Doug Anderson

[permalink] [raw]
Subject: Re: [PATCH printk v2 26/38] kdb: use srcu console list iterator

Hi,

On Wed, Oct 19, 2022 at 7:56 AM John Ogness <[email protected]> wrote:
>
> Guarantee safe iteration of the console list by using SRCU.
>
> Signed-off-by: John Ogness <[email protected]>
> ---
> kernel/debug/kdb/kdb_io.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
> index 550fe8b456ec..5c0bd93c3574 100644
> --- a/kernel/debug/kdb/kdb_io.c
> +++ b/kernel/debug/kdb/kdb_io.c
> @@ -545,6 +545,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
> {
> struct console *c;
> const char *cp;
> + int cookie;
> int len;
>
> if (msg_len == 0)
> @@ -558,7 +559,8 @@ static void kdb_msg_write(const char *msg, int msg_len)
> cp++;
> }
>
> - for_each_console(c) {
> + cookie = console_srcu_read_lock();
> + for_each_console_srcu(c) {

Maybe it wouldn't hurt to also have a comment saying that normally the
console_srcu_read_lock() wouldn't be enough given that we're poking
into each individual console and calling ->write() but that we're
relying on the fact that all the other CPUs are stopped at the moment
and thus we should be safe.

-Doug

2022-10-25 16:20:12

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH printk v2 26/38] kdb: use srcu console list iterator

On Mon 2022-10-24 17:47:25, Doug Anderson wrote:
> Hi,
>
> On Wed, Oct 19, 2022 at 7:56 AM John Ogness <[email protected]> wrote:
> >
> > Guarantee safe iteration of the console list by using SRCU.
> >
> > Signed-off-by: John Ogness <[email protected]>
> > ---
> > kernel/debug/kdb/kdb_io.c | 5 ++++-
> > 1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
> > index 550fe8b456ec..5c0bd93c3574 100644
> > --- a/kernel/debug/kdb/kdb_io.c
> > +++ b/kernel/debug/kdb/kdb_io.c
> > @@ -545,6 +545,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
> > {
> > struct console *c;
> > const char *cp;
> > + int cookie;
> > int len;
> >
> > if (msg_len == 0)
> > @@ -558,7 +559,8 @@ static void kdb_msg_write(const char *msg, int msg_len)
> > cp++;
> > }
> >
> > - for_each_console(c) {
> > + cookie = console_srcu_read_lock();
> > + for_each_console_srcu(c) {
>
> Maybe it wouldn't hurt to also have a comment saying that normally the
> console_srcu_read_lock() wouldn't be enough given that we're poking
> into each individual console and calling ->write() but that we're
> relying on the fact that all the other CPUs are stopped at the moment
> and thus we should be safe.

True. I guess that the SRCU lock is not really needed from the same
reason.

Well, the SRCU walk makes sense. It makes sure that the list can be
safely traversed. I mean that pointers are updated and read in
the right order with the right barriers.

Yes, it would be nice to add a comment.

Best Regards,
Petr