Re: [PATCH printk] printk: Avoid multiple unregistration in unregister_console_locked()
From: Petr Mladek
Date: Fri Oct 30 2026 - 09:53:06 EST
On Thu 2026-10-29 16:48:53, John Ogness wrote:
> On 2026-10-29, Sashiko wrote:
> >> @@ -4471,6 +4493,9 @@ static int __init printk_late_init(void)
> >> }
> >>
> >> unregister_console_locked(con);
> >> +
> >> + /* Changed console list, may require printer threads to start/stop. */
> >> + printk_kthreads_check_locked();
> >> }
> >
> > Should printk_kthreads_check_locked() be called after the
> > hlist_for_each_entry_safe(con, tmp, &console_list, node) loop in
> > printk_late_init() rather than inside the loop body?
> >
> > In kernel/printk/printk.c:printk_late_init():
> >
> > hlist_for_each_entry_safe(con, tmp, &console_list, node) {
> > if (!(con->flags & CON_BOOT))
> > continue;
> > ...
> > unregister_console_locked(con);
> >
> > /* Changed console list, may require printer threads to start/stop. */
> > printk_kthreads_check_locked();
> > }
> > console_list_unlock();
> >
> > Because printk_kthreads_check_locked() is placed inside the loop,
> > unregistering the last boot console sets have_boot_console to false and
> > immediately attempts to start printer kthreads while the loop is still
> > iterating over console_list.
> >
> > If kthread creation in legacy_kthread_create() or nbcon_kthread_create()
> > fails for the console stored in tmp, printk_kthreads_check_locked() calls
> > unregister_console_locked() on that console, removing it with
> > hlist_del_init_rcu(), freeing nbcon resources, and invoking console->exit().
> > On the next iteration of printk_late_init(), con is set to tmp, accessing the
> > already-unregistered console and terminating the walk early because
> > hlist_del_init_rcu() cleared tmp->next.
>
> Geez, sorry. Obviously printk_kthreads_check_locked() needs to be
> outside of the hlist_for_each_entry_safe(console_list) loop. That was
> the whole point of the series!
>
> I will wait a day for v2 in case there is any feedback regarding my
> comments (or lack thereof).
JFYI, the comments look good to me.
It is quite twisted problem. I have got lost again when looking at
this patch. I am not surprised that the 1st version was not right. ;-)
Best Regards,
Petr