netconsole: replace target_list_lock by RCU on userdata hot path
Subsystems (4)
Breno Leitao
maintainerv3 cover netconsole: replace target_list_lock by RCU on userdata hot path
I would like to move netconsole to use RCU on the hot path for a while instead of target_list_lock. My goal is to have no lock on the tx side at all and eventually drop CON_NBCON_ATOMIC_UNSAFE, if that is possible [1].
Start removing target_list_lock on certain parts of the code. This patch transforms the userdata array into a RCU-protected pointer, and uses the dynamic mutex as the write lock.
Added a selftest, given we didn't have any netconsole selftest for userdata operations. Feel free to drop it if this is not useful, dear maintainers.
---
Changes in v3:
- Rebased on top of net-next
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- netconsole: free the payload with kfree(rcu_access_pointer()) in the
teardown paths, silencing a sparse __rcu warning (Gustavo Luiz Duarte)
- selftest: reap every torture worker before reporting a failure, so
none is left writing to configfs while the exit trap runs
- Link to v1: https://patch.msgid.link/[email protected]
---
Breno Leitao (2):
netconsole: publish the userdata payload with RCU
selftests: netconsole: add a userdata torture test
drivers/net/netconsole.c | 92 +++++---- .../selftests/drivers/net/netconsole/Makefile | 1 + .../drivers/net/netconsole/netcons_userdata.sh | 229 +++++++++++++++++++++ 3 files changed, 286 insertions(+), 36 deletions(-)
---
base-commit: 001b5d347d8ba39b2dccaefcc57967b18caec8fe
change-id: 20260801-netcons-userdata-rcu-40446e018f86
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Breno Leitao
maintainerPATCH v3 1/2 netconsole: publish the userdata payload with RCU
update_userdata() takes target_list_lock to swap nt->userdata and nt->userdata_length, then frees the old buffer. Since commit 7eab73b18630 ("netconsole: convert to NBCON console infrastructure") that lock is also the console's device_lock, so writing a userdata value from configfs serialises against the printk core emitting messages.
The buffer is immutable once published, which is what RCU is for. Move the string and its length into a single netcons_userdata object and publish it with rcu_replace_pointer(), freeing the old one with kfree_rcu().
New userdata design:
0) Unify the userdata fields into a struct netcons_userdata
1) update_userdata() no longer needs target_list_lock.
2) writers stay serialised by dynamic_netconsole_mutex.
3) reading userdata needs an RCU read lock.
No functional change intended.
--- drivers/net/netconsole.c | 92 +++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 36 deletions(-)
drivers/net/netconsole.c +56 -36
@@ -151,13 +151,27 @@ enum target_state { STATE_DEACTIVATED, }; +/** +* struct netcons_userdata - Formatted userdata payload of a target. +* @rcu: Used to free the payload after a grace period. +* @length: Length of @data, excluding the NUL terminator. +* @data: Formatted " key=value\n" entries, NUL terminated. +* +* Immutable once published, so the transmit path never observes @data and +* @length disagreeing. +*/ +struct netcons_userdata { +struct rcu_head rcu; +size_t length; +char data[]; +}; +/** * struct netconsole_target - Represents a configured netconsole target. * @list: Links this target into the target_list. * @group: Links us into the configfs subsystem hierarchy. * @userdata_group: Links to the userdata configfs hierarchy -* @userdata: Cached, formatted string of append -* @userdata_length: String length of userdata. +* @userdata: Cached, formatted userdata payload. RCU protected. * @sysdata: Cached, formatted string of append * @sysdata_fields: Sysdata features enabled. * @msgcounter: Message sent counter.
@@ -198,8 +212,7 @@ struct netconsole_target {#ifdef CONFIG_NETCONSOLE_DYNAMIC struct config_group group;struct config_group userdata_group; -char *userdata; -size_t userdata_length; +struct netcons_userdata __rcu *userdata;char sysdata[MAX_EXTRADATA_ENTRY_LEN *MAX_SYSDATA_ITEMS ];/* bit-wise with sysdata_feature bits */
@@ -1351,12 +1364,11 @@ static int calc_userdata_len(struct netconsole_target *nt)static int update_userdata (struct netconsole_target *nt) { +struct netcons_userdata *new =NULL ; +struct netcons_userdata *old;struct userdatum *udm_item;struct config_item *item;struct list_head *entry; -char *old_buf =NULL ; -char *new_buf =NULL ; -unsigned long flags;int offset =0 ;int len;
@@ -1368,8 +1380,8 @@ static int update_userdata(struct netconsole_target *nt)/* Allocate new buffer */ if (len) { - new_buf =kmalloc (len +1 ,GFP_KERNEL ); -if (!new_buf) + new =kmalloc_flex (*new, data, len +1 ); +if (!new)return -ENOMEM ; }
@@ -1379,22 +1391,21 @@ static int update_userdata(struct netconsole_target *nt) udm_item =to_userdatum (item);/* Skip userdata with no value set */ if (udm_item->value[0 ]) { - offset +=scnprintf (&new_buf[offset], len +1 - offset, + offset +=scnprintf (&new->data[offset], + len +1 - offset," %s=%s\n" , item->ci_name, udm_item->value); } }WARN_ON_ONCE (offset != len); +if (new) + new->length = offset; -/* Switch to new buffer and free old buffer */ -spin_lock_irqsave (&target_list_lock, flags); - old_buf = nt->userdata; - nt->userdata = new_buf; - nt->userdata_length = offset; -spin_unlock_irqrestore (&target_list_lock, flags); - -kfree (old_buf); +/* Writers are serialized by dynamic_netconsole_mutex. */ + old =rcu_replace_pointer (nt->userdata, new, +lockdep_is_held (&dynamic_netconsole_mutex)); +kfree_rcu (old, rcu);return 0 ; }
@@ -1684,7 +1695,7 @@ static void netconsole_target_release(struct config_item *item) {struct netconsole_target *nt =to_target (item); -kfree (nt->userdata); +kfree (rcu_access_pointer (nt->userdata));kfree (nt); }
@@ -2227,14 +2238,13 @@ static void send_udp(struct netconsole_target *nt, const char *msg, int len)static void send_msg_no_fragmentation (struct netconsole_target *nt,const char *msg,int msg_len, -int release_len) +int release_len, +const struct netcons_userdata *userdata) { -const char *userdata =NULL ;const char *sysdata =NULL ;const char *release;#ifdef CONFIG_NETCONSOLE_DYNAMIC - userdata = nt->userdata; sysdata = nt->sysdata;#endif
@@ -2251,7 +2261,7 @@ static void send_msg_no_fragmentation(struct netconsole_target *nt,if (userdata) msg_len +=scnprintf (&nt->buf[msg_len],sizeof (nt->buf) - msg_len,"%s" , - userdata); + userdata->data);if (sysdata) msg_len += scnprintf(&nt->buf[msg_len],
@@ -2271,7 +2281,8 @@ static void append_release(char *buf)static void send_fragmented_body (struct netconsole_target *nt,const char *msgbody_ptr,int header_len, -int msgbody_len,int sysdata_len) +int msgbody_len,int sysdata_len, +const struct netcons_userdata *userdata) {const char *userdata_ptr =NULL ;const char *sysdata_ptr =NULL ;
@@ -2282,12 +2293,12 @@ static void send_fragmented_body(struct netconsole_target *nt,int userdata_len =0 ;#ifdef CONFIG_NETCONSOLE_DYNAMIC - userdata_ptr = nt->userdata; sysdata_ptr = nt->sysdata; - userdata_len = nt->userdata_length;#endif -if (WARN_ON_ONCE (!userdata_ptr && userdata_len !=0 )) -return ; +if (userdata) { + userdata_ptr = userdata->data; + userdata_len = userdata->length; + }if (WARN_ON_ONCE (!sysdata_ptr && sysdata_len !=0 ))return ;
@@ -2364,7 +2375,8 @@ static void send_msg_fragmented(struct netconsole_target *nt,const char *msg, int msg_len, int release_len, - int sysdata_len) + int sysdata_len, + const struct netcons_userdata *userdata) { int header_len, msgbody_len;const char *msgbody;
@@ -2393,7 +2405,7 @@ static void send_msg_fragmented(struct netconsole_target *nt,* will be replaced */ send_fragmented_body (nt, msgbody, header_len, msgbody_len, - sysdata_len); + sysdata_len, userdata); }/**
@@ -2408,25 +2420,33 @@ static void send_msg_fragmented(struct netconsole_target *nt,static void send_ext_msg_udp (struct netconsole_target *nt,struct nbcon_write_context *wctxt) { +const struct netcons_userdata *userdata =NULL ;int userdata_len =0 ;int release_len =0 ;int sysdata_len =0 ;int len; +/* Keeps the payload picked below alive until the last send_udp(). */ +rcu_read_lock (); +#ifdef CONFIG_NETCONSOLE_DYNAMIC sysdata_len =prepare_sysdata (nt, wctxt); - userdata_len = nt->userdata_length; + userdata =rcu_dereference (nt->userdata); +if (userdata) + userdata_len = userdata->length;#endif if (nt->release) release_len =strlen (init_utsname ()->release) +1 ; len = wctxt->len + release_len + sysdata_len + userdata_len;if (len <=MAX_PRINT_CHUNK ) -return send_msg_no_fragmentation (nt, wctxt->outbuf, - wctxt->len, release_len); +send_msg_no_fragmentation (nt, wctxt->outbuf, wctxt->len, + release_len, userdata); +else +send_msg_fragmented (nt, wctxt->outbuf, wctxt->len, release_len, + sysdata_len, userdata); -return send_msg_fragmented (nt, wctxt->outbuf, wctxt->len, release_len, - sysdata_len); +rcu_read_unlock (); } static void send_msg_udp(struct netconsole_target *nt, const char *msg,
@@ -2669,7 +2689,7 @@ static void free_param_target(struct netconsole_target *nt)netconsole_skb_pool_flush (nt);netpoll_cleanup (&nt->np);#ifdef CONFIG_NETCONSOLE_DYNAMIC -kfree (nt->userdata); +kfree (rcu_access_pointer (nt->userdata));#endif kfree (nt); }
Signature · 1 line
2.53.0-Meta
Re: [PATCH net-next v3 1/2] netconsole: publish the userdata payload with RCU
Quoted 14 lines · Breno Leitao
update_userdata()takestarget_list_lockto swapnt->userdataandnt->userdata_length, then frees the old buffer. Since commit7eab73b18630("netconsole: convert to NBCON console infrastructure") that lock is also the console'sdevice_lock, so writing a userdata value from configfs serialises against the printk core emitting messages.The buffer is immutable once published, which is what RCU is for. Move the string and its length into a single
netcons_userdataobject and publish it withrcu_replace_pointer(), freeing the old one withkfree_rcu().New userdata design:
Quoted 8 lines
0) Unify the userdata fields into a struct
netcons_userdata
1)update_userdata()no longer needstarget_list_lock.
2) writers stay serialised bydynamic_netconsole_mutex.
3) reading userdata needs an RCU read lock.No functional change intended.
Signed-off-by Breno Leitao <[email protected]>
Breno Leitao
maintainerPATCH v3 2/2 selftests: netconsole: add a userdata torture test
In reply to Breno LeitaoThe userdata payload is rebuilt and republished on every configfs write, including while the target is enabled and messages are being sent.
Add netcons_userdata.sh that runs random tests with userdata.
--- .../selftests/drivers/net/netconsole/Makefile | 1 + .../drivers/net/netconsole/netcons_userdata.sh | 229 +++++++++++++++++++++ 2 files changed, 230 insertions(+)
tools/testing/selftests/drivers/net/netconsole/Makefile +1 -0
@@ -13,6 +13,7 @@ TEST_PROGS := \ netcons_resume.sh \ netcons_sysdata.sh \ netcons_torture.sh \ + netcons_userdata.sh \ # end of TEST_PROGS include ../../../lib.mk
tools/testing/selftests/drivers/net/netconsole/netcons_userdata.sh new file +229 -0
229 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Re: [PATCH net-next v3 2/2] selftests: netconsole: add a userdata torture test
Quoted 7 lines · Breno Leitao
The userdata payload is rebuilt and republished on every configfs write, including while the target is enabled and messages are being sent.
Add
netcons_userdata.shthat runs random tests with userdata.Signed-off-by Breno Leitao <[email protected]>
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 10 lines · On Mon, 10 Aug 2026 02:36:10 -0700 you wrote:
I would like to move netconsole to use RCU on the hot path for a while instead of
target_list_lock. My goal is to have no lock on the tx side at all and eventually dropCON_NBCON_ATOMIC_UNSAFE, if that is possible [1].Start removing
target_list_lockon certain parts of the code. This patch transforms the userdata array into a RCU-protected pointer, and uses the dynamic mutex as the write lock.[...]
Here is the summary with links:
- [net-next,v3,1/2] netconsole: publish the userdata payload with RCU
https://git.kernel.org/netdev/net-next/c/a9560343d4e9 - [net-next,v3,2/2] selftests: netconsole: add a userdata torture test https://git.kernel.org/netdev/net-next/c/3d2452c2fb2f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html