Re: [PATCH net-next v2] netconsole: register one console per target

From: Petr Mladek

Date: Wed Nov 18 2026 - 10:59:46 EST


On Tue 2026-11-10 05:10:01, Breno Leitao wrote:
> netconsole registers a single console (or two in case someone wants
> basic and extended at the same time), independent of the number of
> targets.
>
> This was not ideal, but, there was no reason to change.
>
> Now that we want to replay some messages [1], we cannot do it per
> target, but, per console [2].
>
> Embed a struct console in netconsole_target, so, each target has its own
> console, named `netcons_<target name>`. struct console holds 15
> characters, so longer names are truncated, and two targets can end up
> with the same console name.
>
> No ->match() callback is added: netconsole consoles set CON_ENABLED and
> register without a console= match. console=netcon0 no longer refers to
> netconsole.
>
> Unregistering a console waits for an SRCU grace period and for its
> printer thread to stop, which the netdev notifier should not do
> under RTNL. A target stopped by an interface event keeps its console
> instead, and the write callback skips the target as it does today.
> Writing to its enabled attribute drops that console.
>
> The console is replaced when the target is enabled again or resumes.
> The user may have switched the format in between, and printk has to
> format the records the way the target sends them. A target whose
> console fails to register is disabled, as it would not print
> anything.
>
> Today, a target switched to the other format while deactivated resumes
> silently if no console of that format is registered.
>
> target_list_lock remains the device_lock of all the consoles. A target
> is enabled before its console is registered, so only disabling it and
> interface events race with the write callback, and both change the
> state under that lock. The printer threads of the targets serialize on
> it, like the two shared consoles do today.
>
> /proc/consoles now lists one entry per enabled target, and per target
> stopped by an interface event, for example:
>
> netcons_foo0 -W- (E N )
>
> This makes the mental model easier to understand, and also simplifies
> the code.
>
> --- a/drivers/net/netconsole.c
> +++ b/drivers/net/netconsole.c
> @@ -1134,19 +1112,6 @@ static ssize_t enabled_store(struct config_item *item,
> goto out_unlock;
> }
>
> - if (nt->extended && !console_is_registered(&netconsole_ext)) {
> - netconsole_ext.flags |= CON_ENABLED;
> - register_console(&netconsole_ext);
> - }
> -
> - /* User might be enabling the basic format target for the very
> - * first time, make sure the console is registered.
> - */
> - if (!nt->extended && !console_is_registered(&netconsole)) {
> - netconsole.flags |= CON_ENABLED;
> - register_console(&netconsole);
> - }

What is the expected behavior when people repeatedly disable/enable
the console, please?

register_console() always initializes con->nbcon_seq, see
get_init_console_seq(). A solution might be to keep the current value
when con->nbcon_seq or con->seq is not zero.

Alternative solution would be to add a flag CON_PAUSED which would be
used to temporary disable the console without unregistering. It might
be handled similar way as CON_SUSPENDED. The point is to make
is_console_usable() return false when the console should stop
emitting the messages.

Note that we could not use CON_ENABLE because it already has another
semantic.

> -
> /*
> * Skip netconsole_parser_cmdline() -- all the attributes are
> * already configured via configfs. Just print them out.
> @@ -1167,6 +1132,9 @@ static ssize_t enabled_store(struct config_item *item,
> goto out_unlock;
> }
>
> + ret = netconsole_register_console(nt, 0);
> + if (ret)
> + goto out_unlock;
> pr_info("network logging started\n");
> } else { /* false */
> /* We need to disable the netconsole before cleaning it up
> @@ -1182,10 +1150,7 @@ static ssize_t enabled_store(struct config_item *item,

The code does this here:

nt->state = STATE_DISABLED;

> list_move(&nt->list, &target_cleanup_list);
> spin_unlock_irqrestore(&target_list_lock, flags);
> mutex_unlock(&target_cleanup_list_lock);
> - /* Unregister consoles, whose the last target of that type got
> - * disabled.
> - */
> - unregister_netcons_consoles();
> + netconsole_unregister_console(nt);

Why do we disable the traget before unregistering, please?
I ask because it might cause loosing messages, see below.

> }
>
> ret = count;
> @@ -2615,53 +2585,40 @@ static void send_msg_udp(struct netconsole_target *nt, const char *msg,
> }
>
> /**
> - * netconsole_write - Generic function to send a msg to all targets
> + * netconsole_write - Send a msg to the target of a console
> + * @con: console of the target
> * @wctxt: nbcon write context
> - * @extended: "true" for extended console mode
> *
> - * Given an nbcon write context, send the message to the netconsole targets
> + * Given an nbcon write context, send the message to the netconsole target
> */
> -static void netconsole_write(struct nbcon_write_context *wctxt, bool extended)
> +static void netconsole_write(struct console *con,
> + struct nbcon_write_context *wctxt)
> {
> struct netconsole_target *nt;
>
> + nt = container_of(con, struct netconsole_target, console);
> +
> if (oops_only && !netconsole_kernel_dying())
> return;
>
> - list_for_each_entry(nt, &target_list, list) {
> - if (nt->extended != extended || nt->state != STATE_ENABLED ||
> - !netif_running(nt->np.dev))
> - continue;
> -
> - if (netconsole_ratelimited(nt))
> - continue;
> -
> - /* If nbcon_enter_unsafe() fails, just return given netconsole
> - * lost the ownership, and iterating over the targets will not
> - * be able to re-acquire.
> - */
> - if (!nbcon_enter_unsafe(wctxt))
> - return;
> + if (nt->state != STATE_ENABLED || !netif_running(nt->np.dev))
> + return;

This is where the messages can get lost. The write() callback does not
pass any error value. As a result, nbcon_emit_next_record() bumps
con->nbcon_seq even when the message was not emitted.

A better solution would be to unregister or disable the console
so that the write() callback is not longer called in the 1st place.
The current con->nbcon_seq could be preserved and used again when
the console is resumed/enabled again.

>
> - if (extended)
> - send_ext_msg_udp(nt, wctxt);
> - else
> - send_msg_udp(nt, wctxt->outbuf, wctxt->len);
> + if (netconsole_ratelimited(nt))
> + return;
>
> - nbcon_exit_unsafe(wctxt);
> - }
> -}
> + /* If nbcon_enter_unsafe() fails, just return given netconsole
> + * lost the ownership.
> + */
> + if (!nbcon_enter_unsafe(wctxt))
> + return;
>
> -static void netconsole_write_ext(struct console *con __always_unused,
> - struct nbcon_write_context *wctxt)
> -{
> - netconsole_write(wctxt, true);
> -}
> + if (nt->extended)
> + send_ext_msg_udp(nt, wctxt);
> + else
> + send_msg_udp(nt, wctxt->outbuf, wctxt->len);
>
> -static void netconsole_write_basic(struct console *con __always_unused,
> - struct nbcon_write_context *wctxt)
> -{
> - netconsole_write(wctxt, false);
> + nbcon_exit_unsafe(wctxt);
> }
>
> static void netconsole_device_lock(struct console *con __always_unused,

Otherwise, the patch looks reasonable from the printk POV.

Note that I haven't tested it. And I am not sure how many scenarios
are possible. It seems that the netconsole user interface is really
rich ;-)

Best Regards,
Petr