netconsole: NBCON Infrastructure Support
Subsystems (3)
Breno Leitao
maintainercover netconsole: NBCON Infrastructure Support
This RFC proposes enabling netconsole on the NBCON infrastructure.
Context:
=======
Mike[1] reported a netconsole HARDIRQ-safe → HARDIRQ-unsafe lock warning a while ago. The root cause involved IRQ-unsafe locks being called within the console lock context. These IRQ-unsafe locks are on some very specific network drivers TX path (ieee80211 as in Mike's report).
A possible solution is to mark these devices as NOT supported by netpoll (aka IFF_DISABLE_NETPOLL). Another solution is to send "most" of the netconsole messages from non-atomic contexts (aka thread in nbcon parlance), and only rely on atomic context when the host is crashing.
On top of that, nbcon is a much modern console implementation, which brings others benefits to netconsole, so, this patches move netconsole to NBCON.
Until recently, NBCON lacked support for non-atomic consoles (CON_NBCON_ATOMIC_UNSAFE), thus, this port was not possible so far.
John recently implemented CON_NBCON_ATOMIC_UNSAFE in commit 187de7c212e5 ("printk: nbcon: Allow unsafe write_atomic() for panic"), enabling netconsole to use nbcon framework.
The patchset implements NBCON support in 2 phases:
1. Refactoring: Extract message fragmentation logic into a reusable helper function.
2. Extended console support: Introduce CONFIG_NETCONSOLE_NBCON for consoles implementing device lock/unlock callbacks
Backward Compatibility
======================
When CONFIG_NETCONSOLE_NBCON is disabled (the default), both extended and basic consoles continue using the legacy console infrastructure, ensuring full backward compatibility.
Current Limitations
===================
Netconsole continues to call netpoll and network TX helpers with interrupts disabled. The network xmit callbacks are called with IRQ disabled (target_list_lock is an IRQ safe spinlock)
spin_lock_irqsave(&target_list_lock, *flags)
list_for_each_entry(nt, &target_list, list) netpoll_send_udp(); __netpoll_send_skb() lockdep_assert_irqs_disabled()
While this patchset doesn't fully resolve the issue in [1], it removes one layer of the problem and, moves the problem into the network domain, which is a huge win.
Also, the commit 187de7c212e5 ("printk: nbcon: Allow unsafe write_atomic() for panic") is still not on net-next, thus, NIPA will fail for this RFC. Also, this patch is based on linux-next as of 20251121 instead of net-next.
Next steps
==========
1) Move the target_list_lock to RCU
2) Assess if __netpoll_send_skb() can be called with IRQ enabled
3) Mark devices that rely on IRQ unsafe contexts with IFF_DISABLE_NETPOLL
4) Use CON_NBCON_ATOMIC_UNSAFE only if the netpoll device has IFF_DISABLE_NETPOLL, otherwise, unset CON_NBCON_ATOMIC_UNSAFE and be a more normal NBCON user.
[1] https://lore.kernel.org/all/b2qps3uywhmjaym4mht2wpxul4yqtuuayeoq4iv4k3zf5wdgh3@tocu6c7mj4lt/
---
Breno Leitao (2):
netconsole: extract message fragmentation into write_msg_target()
netconsole: add CONFIG_NETCONSOLE_NBCON for nbcon support
drivers/net/Kconfig | 14 ++++++++ drivers/net/netconsole.c | 94 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 10 deletions(-)
---
base-commit: d724c6f85e80a23ed46b7ebc6e38b527c09d64f5
change-id: 20251117-nbcon-f24477ca9f3e
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Breno Leitao
maintainerRFC PATCH 1/2 netconsole: extract message fragmentation into write_msg_target()
Refactor the message fragmentation logic in write_msg() by extracting it into a separate write_msg_target() helper function. This makes the code more maintainable and prepares for future reuse in nbcon support for non-extended consoles.
The helper function takes a target, message, and length, then handles splitting the message into MAX_PRINT_CHUNK-sized fragments for sending via send_udp().
No functional change intended.
--- drivers/net/netconsole.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
drivers/net/netconsole.c +16 -10
@@ -1559,6 +1559,20 @@ static void append_release(char *buf)scnprintf (buf,MAX_PRINT_CHUNK ,"%s," , release); } +static void write_msg_target (struct netconsole_target *nt,const char *msg, +unsigned int len) +{ +const char *tmp = msg; +int frag, left = len; + +while (left >0 ) { + frag =min (left,MAX_PRINT_CHUNK ); +send_udp (nt, tmp, frag); + tmp += frag; + left -= frag; + } +} +static void send_fragmented_body (struct netconsole_target *nt,const char *msgbody,int header_len,int msgbody_len,int extradata_len)
@@ -1728,10 +1742,8 @@ static void write_ext_msg(struct console *con, const char *msg,static void write_msg (struct console *con,const char *msg,unsigned int len) { -int frag, left; -unsigned long flags;struct netconsole_target *nt; -const char *tmp; +unsigned long flags;if (oops_only && !oops_in_progress)return ;
@@ -1748,13 +1760,7 @@ static void write_msg(struct console *con, const char *msg, unsigned int len)* at least one target if we die inside here, instead * of unnecessarily keeping all targets in lock-step. */ - tmp = msg; -for (left = len; left;) { - frag =min (left,MAX_PRINT_CHUNK ); -send_udp (nt, tmp, frag); - tmp += frag; - left -= frag; - } +write_msg_target (nt, msg, len); } }spin_unlock_irqrestore (&target_list_lock, flags);
Signature · 1 line
2.47.3
Re: [PATCH RFC net-next 1/2] netconsole: extract message fragmentation into write_msg_target()
Quoted 36 lines · Breno Leitao
Refactor the message fragmentation logic in
write_msg()by extracting it into a separatewrite_msg_target()helper function. This makes the code more maintainable and prepares for future reuse in nbcon support for non-extended consoles.The helper function takes a target, message, and length, then handles splitting the message into MAX_PRINT_CHUNK-sized fragments for sending via
send_udp().No functional change intended.
drivers/net/netconsole.c +14
-0@@ -1559,6 +1559,20 @@ static void append_release(char *buf)scnprintf (buf,MAX_PRINT_CHUNK ,"%s," , release); } +static void write_msg_target (struct netconsole_target *nt,const char *msg, +unsigned int len) +{ +const char *tmp = msg; +int frag, left = len; + +while (left >0 ) { + frag =min (left,MAX_PRINT_CHUNK ); +send_udp (nt, tmp, frag); + tmp += frag; + left -= frag; + } +} +static void send_fragmented_body (struct netconsole_target *nt,const char *msgbody,int header_len,int msgbody_len,int extradata_len)@@ -1748,13 +1760,7 @@ static void write_msg(struct console *con, const char *msg, unsigned int len)* at least one target if we die inside here, instead
drivers/net/netconsole.c static void write_msg(struct console *con, const char *msg, unsigned int len) · line 1763
Quoted 10 lines
drivers/net/netconsole.c +1
-7* of unnecessarily keeping all targets in lock-step. */ - tmp = msg; -for (left = len; left;) { - frag =min (left,MAX_PRINT_CHUNK ); -send_udp (nt, tmp, frag); - tmp += frag; - left -= frag; - } +write_msg_target (nt, msg, len);
I would call the new function send_msg_udp() to make it symetric with:
static void write_ext_msg(struct console *con, const char *msg,
unsigned int len)
{
[...]
send_ext_msg_udp(nt, msg, len);
[...]
}
By other words, use "write_*()" when struct console * is passed and send_*_udp() when struct netconsole_target * is passed.
The inconsistence confused me... ;-)
Note that write_msg()/write_ext_msg() are cut&pasted. The only difference would be send_msg_udp()/send_ext_msg_udp().
drivers/net/netconsole.c static void write_msg(struct console *con, const char *msg, unsigned int len) · line 1766
Quoted 3 lines
drivers/net/netconsole.c +0
-0} }spin_unlock_irqrestore (&target_list_lock, flags);
Otherwise, I confirm that it is just a refactoring with no functional change.
Feel free to use, ideally after the renaming function:
Best Regards,
Petr
Breno Leitao
maintainerRFC PATCH 2/2 netconsole: add CONFIG_NETCONSOLE_NBCON for nbcon support
In reply to Breno LeitaoAdd optional support for the nbcon infrastructure to netconsole via a new CONFIG_NETCONSOLE_NBCON compile-time option.
The nbcon infrastructure provides a lock-free, priority-based console system that supports atomic printing from any context including NMI, with safe handover mechanisms between different priority levels. This makes it particularly suitable for crash-safe kernel logging.
When disabled (default), netconsole uses the legacy console callbacks, maintaining full backward compatibility.
PS: .write_atomic and .write_thread uses the same callback, given that there is no safe .write_atomic, so .write_atomic is called as the last resource. This is what CON_NBCON_ATOMIC_UNSAFE is telling nbcon.
--- drivers/net/Kconfig | 14 ++++++++++ drivers/net/netconsole.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+)
drivers/net/Kconfig +14 -0
@@ -369,6 +369,20 @@ config NETCONSOLE_PREPEND_RELEASE message. See <file:Documentation/networking/netconsole.rst> for details. +config NETCONSOLE_NBCON + bool "Use nbcon infrastructure (EXPERIMENTAL)" + depends on NETCONSOLE + default n + help + Enable nbcon support for netconsole. This uses the new lock-free + console infrastructure which supports threaded and atomic printing. + Given that netconsole does not support atomic operations, the current + implementation focuses on threaded callbacks, unless the host is + crashing, then it uses an unsafe atomic callbacks. This feature is + available for both extended and non-extended consoles. + + If unsure, say N to use the legacy console infrastructure. + config NETPOLL def_bool NETCONSOLE
drivers/net/netconsole.c +68 -0
94 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH RFC net-next 2/2] netconsole: add CONFIG_NETCONSOLE_NBCON for nbcon support
Quoted 8 lines · Breno Leitao
Add optional support for the nbcon infrastructure to netconsole via a new
CONFIG_NETCONSOLE_NBCONcompile-time option.The nbcon infrastructure provides a lock-free, priority-based console system that supports atomic printing from any context including NMI, with safe handover mechanisms between different priority levels. This makes it particularly suitable for crash-safe kernel logging.
Quoted 6 lines
When disabled (default), netconsole uses the legacy console callbacks, maintaining full backward compatibility.
PS: .write_atomic and .write_thread uses the same callback, given that there is no safe .write_atomic, so .write_atomic is called as the last resource. This is what
CON_NBCON_ATOMIC_UNSAFEis telling nbcon.
Makes sense. CON_NBCON_ATOMIC_UNSAFE also explains why target_list_lock need not be synchronized with nbcon context locking [*]. The _unsafe_ .write_atomic() callback might be called only by the final nbcon_atomic_flush_unsafe() when even the nbcon context synchronization can be ignored.
[*] For example, see how port->lock is synchronized with the nbcon
context by uart_port_lock() wrapper.
drivers/net/Kconfig config NETCONSOLE_PREPEND_RELEASE · line 377
Quoted 12 lines
drivers/net/Kconfig +6
-0@@ -369,6 +369,20 @@ config NETCONSOLE_PREPEND_RELEASE message. See <file:Documentation/networking/netconsole.rst> for details. +config NETCONSOLE_NBCON + bool "Use nbcon infrastructure (EXPERIMENTAL)" + depends on NETCONSOLE + default n + help + Enable nbcon support for netconsole. This uses the new lock-free
Strictly speaking, it is not lock-free. The main feature is that it is threaded so that it does not block the printk() caller.
Nbcon consoles also support synchronous flushing in emergecy situations. But it does not work with netconsoles because they do not support atomic operations. They are flushed only by the final desperate flush in panic() when all locks are ignored.
Quoted 30 lines
drivers/net/Kconfig +8
-0+ console infrastructure which supports threaded and atomic printing. + Given that netconsole does not support atomic operations, the current + implementation focuses on threaded callbacks, unless the host is + crashing, then it uses an unsafe atomic callbacks. This feature is + available for both extended and non-extended consoles. + + If unsure, say N to use the legacy console infrastructure. + config NETPOLL def_bool NETCONSOLEdrivers/net/netconsole.c +11
-0@@ -1724,6 +1724,57 @@ static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg, extradata_len); } +#ifdef CONFIG_NETCONSOLE_NBCON +static void netcon_write_nbcon (struct console *con, +struct nbcon_write_context *wctxt, +bool extended) +{ +struct netconsole_target *nt; + +lockdep_assert_held (&target_list_lock); + +list_for_each_entry (nt, &target_list, list) { + if (nt->extended != extended || !nt->enabled ||
drivers/net/netconsole.c static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg, · line 1747
Quoted 10 lines
drivers/net/netconsole.c +10
-0+ !netif_running(nt->np.dev)) +continue ; + +if (!nbcon_enter_unsafe (wctxt)) +continue ; + +if (extended) +send_ext_msg_udp (nt, wctxt->outbuf, wctxt->len); +else +write_msg_target (nt, wctxt->outbuf, wctxt->len);
If you accepted the rename in the 1st patch then this would be ;-)
if (extended) send_ext_msg_udp(nt, wctxt->outbuf, wctxt->len); else send_msg_udp(nt, wctxt->outbuf, wctxt->len);
drivers/net/netconsole.c static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg, · line 1751
Quoted 4 lines
drivers/net/netconsole.c +4
-0+ +nbcon_exit_unsafe (wctxt); + } +}
Otherwise, it looks good from my POV.
Best Regards,
Petr