From: Petr Mladek <pmladek@suse.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
Steven Rostedt <rostedt@goodmis.org>,
Thomas Gleixner <tglx@linutronix.de>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH printk v2 09/18] printk: nbcon: Start printing threads
Date: Wed, 19 Jun 2024 17:13:04 +0200 [thread overview]
Message-ID: <ZnL1gGFeW073gm62@pathway.suse.cz> (raw)
In-Reply-To: <ZnGo-7ctV9oidQM8@pathway.suse.cz>
On Tue 2024-06-18 17:34:22, Petr Mladek wrote:
> On Tue 2024-06-04 01:30:44, John Ogness wrote:
> > If there are no boot consoles, the printing threads are started
> > in early_initcall.
> >
> > If there are boot consoles, the printing threads are started
> > after the last boot console has unregistered. The printing
> > threads do not need to be concerned about boot consoles because
> > boot consoles cannot register once a non-boot console has
> > registered.
> >
> > Until a printing thread of a console has started, that console
> > will print using atomic_write() in the printk() caller context.
>
> > --- a/kernel/printk/nbcon.c
> > +++ b/kernel/printk/nbcon.c
> > @@ -1568,6 +1570,19 @@ void nbcon_kthread_create(struct console *con)
> > sched_set_normal(con->kthread, -20);
> > }
> >
> > +static int __init printk_setup_threads(void)
> > +{
> > + struct console *con;
> > +
> > + console_list_lock();
> > + printk_threads_enabled = true;
>
> What is the actual meaning of the variable?
>
> Does it mean that kthreads can be created? (can be forked?)
>
> It does not guarantee that the kthreads will be running.
> They might still get blocked by a boot console.
I have investigated it more. And it looks like the variable
means that the system is initialized enough to support
the kthreads.
It is similar to the variable __printk_percpu_data_ready.
I am not sure if it would have helped to prevent the confusion
but I would suggest to rename it and add a comment:
/*
* The graphics console drivers are going to use workqueues.
* This variable is set to true when they are safe to use.
*/
static bool __printk_threads_ready __ro_after_init;
>
> > + for_each_console(con)
> > + nbcon_kthread_create(con);
Also we should make it more clear when this function is supposed
to succeed. I mean:
if (have_nbcon_console && !have_boot_console)
for_each_console(con)
nbcon_kthread_create(con);
And similar in unregister_console()
/*
* When the last boot console unregisters, start up the
* printing threads.
*/
if (is_boot_con && have_nbcon_console && !have_boot_console) {
for_each_console(c)
nbcon_kthread_create(c);
}
The same condition (have_nbcon_console && !have_boot_console)
is used on many other locations.
In another mail, I suggested to define a macro for this condition:
#define printing_via_context_safe (have_nbcon_console && !have_boot_console)
It might be even better because it would connect the related
code via cscope.
Hmm, the "_safe" suffix might be confusing in this context.
A better name might be "printing_via_nbcon_context" so that
we have:
/* Printing serialized by console_lock dance is needed. */
#define printing_via_unlock (have_legacy_console || have_boot_console)
/* Printing serialized by nbcon console context is needed and safe. */
#define printing_via_nbcon_context (have_nbcon_console && !have_boot_console)
> > + console_list_unlock();
> > + return 0;
> > +}
> > +early_initcall(printk_setup_threads);
> > +
> > /**
> > * nbcon_alloc - Allocate buffers needed by the nbcon console
> > * @con: Console to allocate buffers for
> > --- a/kernel/printk/printk.c
> > +++ b/kernel/printk/printk.c
> > @@ -2397,6 +2400,7 @@ asmlinkage int vprintk_emit(int facility, int level,
> > * consoles cannot print simultaneously with boot consoles.
> > */
> > if (is_panic_context ||
> > + !printk_threads_enabled ||
> > (system_state > SYSTEM_RUNNING)) {
> > nbcon_atomic_flush_pending();
>
> IMHO, this is not safe. nbcon_atomic_flush_pending()
> is synchronized only via the nbcon console context.
> It means that there might be races with boot consoles.
>
> Another problem is the meaning of @printk_threads_enabled variable.
> It does not guarantee that the kthreads are running. They might
> still be blocked by boot consoles.
>
>
> BTW: The same problem might have the system_state > SYSTEM_RUNNING.
> The boot consoles are removed only when the preferred console
> is registered and "keep_bootcon" is not set.
>
> Note that printk_late_init() unregisters the boot consoles
> only when they are using a code in the init sections.
>
> > }
I take it back. We are on the safe side because the above code is
called only when
if (have_nbcon_console && !have_boot_console)
This is why I suggest to use the same condition around the code
starting the kthreads. It might help to create the mental
model.
Best Regards,
Petr
next prev parent reply other threads:[~2024-06-19 15:13 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-03 23:24 [PATCH printk v2 00/18] add threaded printing + the rest John Ogness
2024-06-03 23:24 ` [PATCH printk v2 01/18] printk: Add function to replay kernel log on consoles John Ogness
2024-06-03 23:24 ` [PATCH printk v2 02/18] tty/sysrq: Replay kernel log messages on consoles via sysrq John Ogness
2024-06-03 23:24 ` [PATCH printk v2 03/18] printk: Rename console_replay_all() and update context John Ogness
2024-06-03 23:24 ` [PATCH printk v2 04/18] printk: nbcon: Introduce printing kthreads John Ogness
2024-06-07 13:17 ` Petr Mladek
2024-06-10 12:09 ` John Ogness
2024-06-11 14:51 ` Petr Mladek
2024-06-12 8:51 ` John Ogness
2024-06-12 9:24 ` Petr Mladek
2024-06-12 11:18 ` John Ogness
2024-06-12 11:33 ` Petr Mladek
2024-06-13 15:21 ` John Ogness
2024-06-14 7:40 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 05/18] printk: Atomic print in printk context on shutdown John Ogness
2024-06-13 12:32 ` Petr Mladek
2024-06-13 12:44 ` atomic_flush vs boot consoles - was: " Petr Mladek
2024-06-13 12:52 ` [PATCH] printk: nbcon_atomic_flush_pending() is safe only when there is no boot console Petr Mladek
2024-06-13 15:10 ` Petr Mladek
2024-06-25 20:53 ` John Ogness
2024-06-28 15:51 ` how to flush consoles: was: " Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 06/18] printk: nbcon: Add context to console_is_usable() John Ogness
2024-06-13 13:22 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 07/18] printk: nbcon: Add printer thread wakeups John Ogness
2024-06-13 15:08 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 08/18] printk: nbcon: Stop threads on shutdown/reboot John Ogness
2024-06-17 15:21 ` Petr Mladek
2024-06-25 19:56 ` John Ogness
2024-06-26 12:14 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 09/18] printk: nbcon: Start printing threads John Ogness
2024-06-18 15:34 ` Petr Mladek
2024-06-19 15:13 ` Petr Mladek [this message]
2024-06-03 23:24 ` [PATCH printk v2 10/18] printk: Provide helper for message prepending John Ogness
2024-06-20 10:28 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 11/18] printk: nbcon: Show replay message on takeover John Ogness
2024-06-20 13:02 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 12/18] printk: Add kthread for all legacy consoles John Ogness
2024-06-28 11:46 ` Petr Mladek
2024-06-28 12:22 ` John Ogness
2024-06-28 13:32 ` Petr Mladek
2024-06-28 14:11 ` John Ogness
2024-06-28 15:56 ` John Ogness
2024-07-01 15:33 ` Petr Mladek
2024-07-01 21:01 ` John Ogness
2024-07-02 9:11 ` Petr Mladek
2024-07-01 14:50 ` Petr Mladek
2024-07-02 9:30 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 13/18] proc: consoles: Add notation to c_start/c_stop John Ogness
2024-07-01 15:43 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 14/18] proc: Add nbcon support for /proc/consoles John Ogness
2024-07-01 15:47 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 15/18] tty: sysfs: Add nbcon support for 'active' John Ogness
2024-06-04 11:48 ` Greg Kroah-Hartman
2024-07-01 15:50 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 16/18] printk: Provide threadprintk boot argument John Ogness
2024-07-02 12:12 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 17/18] printk: Avoid false positive lockdep report for legacy printing John Ogness
2024-07-02 13:26 ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 18/18] printk: nbcon: Add function for printers to reacquire ownership John Ogness
2024-07-02 14:31 ` Petr Mladek
2024-06-04 13:31 ` [PATCH printk v2 00/18] add threaded printing + the rest Juri Lelli
2024-06-05 8:09 ` John Ogness
2024-06-05 9:32 ` Juri Lelli
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=ZnL1gGFeW073gm62@pathway.suse.cz \
--to=pmladek@suse.com \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=tglx@linutronix.de \
/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
Powered by JetHome