From: Petr Mladek <pmladek@suse.com>
To: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>,
Andrew Morton <akpm@linux-foundation.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
"Rafael J . Wysocki" <rjw@rjwysocki.net>,
linux-kernel@vger.kernel.org,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Subject: Re: [RFC][PATCH 2/4] printk: offload printing from wake_up_klogd_work_func()
Date: Fri, 17 Mar 2017 13:19:44 +0100 [thread overview]
Message-ID: <20170317121944.GO3977@pathway.suse.cz> (raw)
In-Reply-To: <20170306124554.828-3-sergey.senozhatsky@gmail.com>
On Mon 2017-03-06 21:45:52, Sergey Senozhatsky wrote:
> Offload printing of printk_deferred() messages from IRQ context
> to a schedulable printing kthread, when possible (the same way
> we do it in vprintk_emit()). Otherwise, console_unlock() can
> force the printing CPU to spend unbound amount of time flushing
> kernel messages from IRQ context.
>
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> ---
> kernel/printk/printk.c | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 1c4232ca2e6a..6e00073a7331 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2735,9 +2735,16 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)
> int pending = __this_cpu_xchg(printk_pending, 0);
>
> if (pending & PRINTK_PENDING_OUTPUT) {
> - /* If trylock fails, someone else is doing the printing */
> - if (console_trylock())
> - console_unlock();
> + if (printk_kthread_enabled()) {
> + wake_up_process(printk_kthread);
I have just noticed a possible race. printk_deferred() does not set
printk_kthread_need_flush_console and there might stay a
pending job:
CPU0 CPU1
printk_kthread_func()
printk_kthread_need_flush_console = false;
console_lock()
console_unlock()
printk_deferred()
vprintk_emit()
irq_work_queue()
<IRQ>
wake_up_klogd_work_func()
if (printk_kthread_enabled())
wake_up_process(printk_kthread);
set_current_state(TASK_INTERRUPTIBLE);
if (!printk_kthread_need_flush_console)
schedule();
Result: printk_kthread goes to sleep even though there is
a pending job.
A solution might be to rename the variable to something like
printk_pending_output, always set it in vprintk_emit() and
clear it in console_unlock() when there are no pending messages.
I think that we have already discussed this in the past.
This solution would also remove one extra cycle if more messages
are handled by one console_unlock() call:
CPU0 CPU1
printk()
vprintk_emit()
printk_kthread_need_flush_console = true;
wake_up_process(printk_kthread)
<printk_kthread>
printk_kthread_need_flush_console
= false;
console_lock()
printk()
vprintk_emit()
printk_kthread_need_flush_console = true;
wake_up_process(printk_kthread)
console_unlock()
set_current_state(TASK_INTERRUPTIBLE);
if (!printk_kthread_need_flush_console)
<fail>
_set_current_state(TASK_RUNNING);
console_lock()
console_unlock()
Result: The second console_unlock() has nothing to do.
If I remember correctly, you were not much happy with this
solution because it did spread the logic. I think that you did not
believe that it was worth fixing the second problem. But fixing
the race might need to spread the logic as well.
I see it the following way. vprintk_emit() is a producer,
console_unlock() is a consumer, and printk_thread is a room
that allows consumer to do its job. The consumer has more
rooms available. The state variable is a flag showing that
there is a pending job, consumer is looking for a room,
and printk_kthread should offer it.
Of course, it is possible that you will find a better
solution.
Best Regards,
Petr
next prev parent reply other threads:[~2017-03-17 12:20 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-06 12:45 [RFC][PATCH 0/4] printk: introduce printing kernel thread Sergey Senozhatsky
2017-03-06 12:45 ` [RFC][PATCH 1/4] " Sergey Senozhatsky
2017-03-22 16:40 ` Petr Mladek
2017-03-23 5:12 ` Sergey Senozhatsky
2017-03-23 10:40 ` Petr Mladek
2017-03-24 5:20 ` Sergey Senozhatsky
2017-03-06 12:45 ` [RFC][PATCH 2/4] printk: offload printing from wake_up_klogd_work_func() Sergey Senozhatsky
2017-03-17 12:19 ` Petr Mladek [this message]
2017-03-18 9:57 ` Sergey Senozhatsky
2017-03-20 16:09 ` Petr Mladek
2017-03-21 4:01 ` Sergey Senozhatsky
2017-03-23 9:00 ` Sergey Senozhatsky
2017-03-23 12:11 ` Petr Mladek
2017-03-06 12:45 ` [RFC][PATCH 3/4] kernel, power: disable printk_kthread in unsafe places Sergey Senozhatsky
2017-03-22 15:38 ` Petr Mladek
2017-03-06 12:45 ` [RFC][PATCH 4/4] printk: enable printk offloading Sergey Senozhatsky
2017-03-22 15:43 ` Petr Mladek
2017-03-22 16:40 ` Sergey Senozhatsky
2017-03-22 17:59 ` [RFC][PATCH 0/4] printk: introduce printing kernel thread Peter Zijlstra
2017-03-23 4:09 ` Sergey Senozhatsky
2017-03-23 8:51 ` Peter Zijlstra
2017-03-24 1:59 ` Sergey Senozhatsky
2017-03-24 4:43 ` Sergey Senozhatsky
2017-03-24 14:43 ` Petr Mladek
2017-03-25 0:18 ` Sergey Senozhatsky
2017-03-23 12:01 ` Petr Mladek
2017-04-03 11:53 ` Sergey Senozhatsky
2017-04-04 12:59 ` Petr Mladek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170317121944.GO3977@pathway.suse.cz \
--to=pmladek@suse.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=rjw@rjwysocki.net \
--cc=rostedt@goodmis.org \
--cc=sergey.senozhatsky.work@gmail.com \
--cc=sergey.senozhatsky@gmail.com \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox