2023-02-16 10:42:17

by John Ogness

[permalink] [raw]
Subject: [PATCH 5.15-rt] printk: ignore consoles without write() callback

The ttynull driver does not provide an implementation for the write()
callback. This leads to a NULL pointer dereference in the related
printing kthread, which assumes it can call that callback.

Do not create kthreads for consoles that do not implement the write()
callback. Also, for pr_flush(), ignore consoles that do not implement
write() or write_atomic() since there is no way those consoles can
flush their output.

Link: https://lore.kernel.org/lkml/[email protected]
Fixes: 8782b1ef4125 ("printk: move console printing to kthreads")
Reported-by: Michael Thalmeier <[email protected]>
Signed-off-by: John Ogness <[email protected]>
---
This is only a problem for the PREEMPT_RT tree. Mainline does not have
this problem.

kernel/printk/printk.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 7f27cfee283e..752afe88b5b4 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2343,6 +2343,10 @@ static int printk_kthread_func(void *data)
/* Must be called within console_lock(). */
static void start_printk_kthread(struct console *con)
{
+ /* No need to start a printing thread if the console cannot print. */
+ if (!con->write)
+ return;
+
con->thread = kthread_run(printk_kthread_func, con,
"pr/%s%d", con->name, con->index);
if (IS_ERR(con->thread)) {
@@ -3737,6 +3741,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
for_each_console(con) {
if (!(con->flags & CON_ENABLED))
continue;
+ if (!con->write && !con->write_atomic)
+ continue;
printk_seq = read_console_seq(con);
if (printk_seq < seq)
diff += seq - printk_seq;
--
2.30.2


Subject: Re: [PATCH 5.15-rt] printk: ignore consoles without write() callback

On 2023-02-16 11:45:58 [+0106], John Ogness wrote:
> This is only a problem for the PREEMPT_RT tree. Mainline does not have
> this problem.

Is this 5.15 and less or current dev RT and less?

Sebastian

2023-02-17 08:46:38

by John Ogness

[permalink] [raw]
Subject: Re: [PATCH 5.15-rt] printk: ignore consoles without write() callback

On 2023-02-16, Sebastian Siewior <[email protected]> wrote:
>> This is only a problem for the PREEMPT_RT tree. Mainline does not have
>> this problem.
>
> Is this 5.15 and less or current dev RT and less?

5.10-rt is also affected. The changes for 5.10-rt are the same, but the
patch does not apply as-is. I will reply with a patch for 5.10-rt.

5.4-rt is not affected.

John Ogness

2023-02-17 08:49:41

by John Ogness

[permalink] [raw]
Subject: [PATCH 5.10-rt] printk: ignore consoles without write() callback

The ttynull driver does not provide an implementation for the write()
callback. This leads to a NULL pointer dereference in the related
printing kthread, which assumes it can call that callback.

Do not create kthreads for consoles that do not implement the write()
callback. Also, for pr_flush(), ignore consoles that do not implement
write() or write_atomic(), since there is no way those consoles can
flush their output.

Link: https://lore.kernel.org/lkml/[email protected]
Reported-by: Michael Thalmeier <[email protected]>
Signed-off-by: John Ogness <[email protected]>
---
kernel/printk/printk.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index d2205872304d..64747c72fbea 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2267,6 +2267,10 @@ static int printk_kthread_func(void *data)
/* Must be called within console_lock(). */
static void start_printk_kthread(struct console *con)
{
+ /* No need to start a printing thread if the console cannot print. */
+ if (!con->write)
+ return;
+
con->thread = kthread_run(printk_kthread_func, con,
"pr/%s%d", con->name, con->index);
if (IS_ERR(con->thread)) {
@@ -3566,6 +3570,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
for_each_console(con) {
if (!(con->flags & CON_ENABLED))
continue;
+ if (!con->write && !con->write_atomic)
+ continue;
printk_seq = atomic64_read(&con->printk_seq);
if (printk_seq < seq)
diff += seq - printk_seq;
--
2.30.2

Subject: Re: [PATCH 5.10-rt] printk: ignore consoles without write() callback

On Fri, Feb 17, 2023 at 09:53:44AM +0106, John Ogness wrote:
> The ttynull driver does not provide an implementation for the write()
> callback. This leads to a NULL pointer dereference in the related
> printing kthread, which assumes it can call that callback.
>
> Do not create kthreads for consoles that do not implement the write()
> callback. Also, for pr_flush(), ignore consoles that do not implement
> write() or write_atomic(), since there is no way those consoles can
> flush their output.
>
> Link: https://lore.kernel.org/lkml/[email protected]
> Reported-by: Michael Thalmeier <[email protected]>
> Signed-off-by: John Ogness <[email protected]>

Thank you! I will apply that to the next 5.10-rt build.

Luis

> ---
> kernel/printk/printk.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index d2205872304d..64747c72fbea 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2267,6 +2267,10 @@ static int printk_kthread_func(void *data)
> /* Must be called within console_lock(). */
> static void start_printk_kthread(struct console *con)
> {
> + /* No need to start a printing thread if the console cannot print. */
> + if (!con->write)
> + return;
> +
> con->thread = kthread_run(printk_kthread_func, con,
> "pr/%s%d", con->name, con->index);
> if (IS_ERR(con->thread)) {
> @@ -3566,6 +3570,8 @@ bool pr_flush(int timeout_ms, bool reset_on_progress)
> for_each_console(con) {
> if (!(con->flags & CON_ENABLED))
> continue;
> + if (!con->write && !con->write_atomic)
> + continue;
> printk_seq = atomic64_read(&con->printk_seq);
> if (printk_seq < seq)
> diff += seq - printk_seq;
> --
> 2.30.2
>
---end quoted text---