Skip to contentSkip to conversation

net: netpoll: fix memory leak and add comprehensive selftests

PATCH Superseded Breno Leitao 2 participants 8 messages Nov 6, 2025 – Nov 10, 2025

Subsystems (6)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Breno Leitao

author maintainer
#0 lore raw

v9 cover net: netpoll: fix memory leak and add comprehensive selftests

Fix a memory leak in netpoll and introduce netconsole selftests that expose the issue when running with kmemleak detection enabled.

This patchset includes a selftest for netpoll with multiple concurrent users (netconsole + bonding), which simulates the scenario from test[1] that originally demonstrated the issue allegedly fixed by commit efa95b01da18 ("netpoll: fix use after free") - a commit that is now being reverted.

Sending this to "net" branch because this is a fix, and the selftest might help with the backports validation.

---
Changes in v9:
- Reordered the config entries in tools/testing/selftests/drivers/net/bonding/config (NIPA)
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Sending it again, now that commit 1a8fed52f7be1 ("netdevsim: set the
carrier when the device goes up") has landed in net
- Created one namespace for TX and one for RX (Paolo)
- Used additional helpers to create and delete netdevsim (Paolo)
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Rebased on top of net
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Expand the tests even more and some small fixups
- Moved the test to bonding selftests
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Set CONFIG_BONDING=m in selftests/drivers/net/config.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Added an additional selftest to test multiple netpoll users in
parallel
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- This patchset is a merge of the fix and the selftest together as recommended by Jakub.

Changes in v2:
- Reuse the netconsole creation from lib_netcons.sh. Thus, refactoring
the create_dynamic_target() (Jakub)
- Move the "wait" to after all the messages has been sent.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Breno Leitao (4):
net: netpoll: fix incorrect refcount handling causing incorrect cleanup
selftest: netcons: refactor target creation
selftest: netcons: create a torture test
selftest: netcons: add test for netconsole over bonded interfaces

 net/core/netpoll.c                                 |   7 +-
 tools/testing/selftests/drivers/net/Makefile       |   1 +
 .../testing/selftests/drivers/net/bonding/Makefile |   2 +
 tools/testing/selftests/drivers/net/bonding/config |   4 +
 .../drivers/net/bonding/netcons_over_bonding.sh    | 361 +++++++++++++++++++++
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    |  82 ++++-
 .../selftests/drivers/net/netcons_torture.sh       | 130 ++++++++
 7 files changed, 569 insertions(+), 18 deletions(-)

---
base-commit: 7d1988a943850c584e8e2e4bcc7a3b5275024072
change-id: 20250902-netconsole_torture-8fc23f0aca99

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
author maintainer
#1 lore raw

PATCH v9 1/4 net: netpoll: fix incorrect refcount handling causing incorrect cleanup

commit efa95b01da18 ("netpoll: fix use after free") incorrectly ignored the refcount and prematurely set dev->npinfo to NULL during netpoll cleanup, leading to improper behavior and memory leaks.

Scenario causing lack of proper cleanup:

1) A netpoll is associated with a NIC (e.g., eth0) and netdev->npinfo is allocated, and refcnt = 1
- Keep in mind that npinfo is shared among all netpoll instances. In this case, there is just one.

2) Another netpoll is also associated with the same NIC and npinfo->refcnt += 1.
- Now dev->npinfo->refcnt = 2;
- There is just one npinfo associated to the netdev.

3) When the first netpolls goes to clean up:
- The first cleanup succeeds and clears np->dev->npinfo, ignoring refcnt.
- It basically calls RCU_INIT_POINTER(np->dev->npinfo, NULL);
- Set dev->npinfo = NULL, without proper cleanup
- No ->ndo_netpoll_cleanup() is either called

4) Now the second target tries to clean up
- The second cleanup fails because np->dev->npinfo is already NULL.
* In this case, ops->ndo_netpoll_cleanup() was never called, and the skb pool is not cleaned as well (for the second netpoll instance)
- This leaks npinfo and skbpool skbs, which is clearly reported by kmemleak.

Revert commit efa95b01da18 ("netpoll: fix use after free") and adds clarifying comments emphasizing that npinfo cleanup should only happen once the refcount reaches zero, ensuring stable and correct netpoll behavior.

Cc <[email protected]> # 3.17.x
Cc Jay Vosburgh <[email protected]>
Fixes efa95b01da18 ("netpoll: fix use after free")
---
 net/core/netpoll.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/core/netpoll.c +5 -2
index c85f740065fc6..331764845e8fa 100644
@@ -811,6 +811,10 @@ static void __netpoll_cleanup(struct netpoll *np)
 	if (!npinfo)
 		return;
 
+	/* At this point, there is a single npinfo instance per netdevice, and
+	 * its refcnt tracks how many netpoll structures are linked to it. We
+	 * only perform npinfo cleanup when the refcnt decrements to zero.
+	 */
 	if (refcount_dec_and_test(&npinfo->refcnt)) {
 		const struct net_device_ops *ops;
 
@@ -820,8 +824,7 @@ static void __netpoll_cleanup(struct netpoll *np)
 
 		RCU_INIT_POINTER(np->dev->npinfo, NULL);
 		call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
-	} else
-		RCU_INIT_POINTER(np->dev->npinfo, NULL);
+	}
 
 	skb_pool_flush(np);
 }
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v9 2/4 selftest: netcons: refactor target creation

In reply to Breno Leitao

Extract the netconsole target creation from create_dynamic_target(), by moving it from create_dynamic_target() into a new helper function. This enables other tests to use the creation of netconsole targets with arbitrary parameters and no sleep.

The new helper will be utilized by forthcoming torture-type selftests that require dynamic target management.

---
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    | 30 ++++++++++++++--------
 1 file changed, 19 insertions(+), 11 deletions(-)
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +19 -11
index 8e1085e896472..9b5ef8074440c 100644
@@ -113,31 +113,39 @@ function set_network() {
 	configure_ip
 }
 
-function create_dynamic_target() {
-	local FORMAT=${1:-"extended"}
+function _create_dynamic_target() {
+	local FORMAT="${1:?FORMAT parameter required}"
+	local NCPATH="${2:?NCPATH parameter required}"
 
 	DSTMAC=$(ip netns exec "${NAMESPACE}" \
 		 ip link show "${DSTIF}" | awk '/ether/ {print $2}')
 
 	# Create a dynamic target
-	mkdir "${NETCONS_PATH}"
+	mkdir "${NCPATH}"
 
-	echo "${DSTIP}" > "${NETCONS_PATH}"/remote_ip
-	echo "${SRCIP}" > "${NETCONS_PATH}"/local_ip
-	echo "${DSTMAC}" > "${NETCONS_PATH}"/remote_mac
-	echo "${SRCIF}" > "${NETCONS_PATH}"/dev_name
+	echo "${DSTIP}" > "${NCPATH}"/remote_ip
+	echo "${SRCIP}" > "${NCPATH}"/local_ip
+	echo "${DSTMAC}" > "${NCPATH}"/remote_mac
+	echo "${SRCIF}" > "${NCPATH}"/dev_name
 
 	if [ "${FORMAT}" == "basic" ]
 	then
 		# Basic target does not support release
-		echo 0 > "${NETCONS_PATH}"/release
-		echo 0 > "${NETCONS_PATH}"/extended
+		echo 0 > "${NCPATH}"/release
+		echo 0 > "${NCPATH}"/extended
 	elif [ "${FORMAT}" == "extended" ]
 	then
-		echo 1 > "${NETCONS_PATH}"/extended
+		echo 1 > "${NCPATH}"/extended
 	fi
 
-	echo 1 > "${NETCONS_PATH}"/enabled
+	echo 1 > "${NCPATH}"/enabled
+
+}
+
+function create_dynamic_target() {
+	local FORMAT=${1:-"extended"}
+	local NCPATH=${2:-"$NETCONS_PATH"}
+	_create_dynamic_target "${FORMAT}" "${NCPATH}"
 
 	# This will make sure that the kernel was able to
 	# load the netconsole driver configuration. The console message
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH v9 3/4 selftest: netcons: create a torture test

In reply to Breno Leitao

Create a netconsole test that puts a lot of pressure on the netconsole list manipulation. Do it by creating dynamic targets and deleting targets while messages are being sent. Also put interface down while the messages are being sent, as creating parallel targets.

The code launches three background jobs on distinct schedules:

* Toggle netcons target every 30 iterations
* create and delete random_target every 50 iterations
* toggle iface every 70 iterations

This creates multiple concurrency sources that interact with netconsole states. This is good practice to simulate stress, and exercise netpoll and netconsole locks.

This test already found an issue as reported in [1]

---
 tools/testing/selftests/drivers/net/Makefile       |   1 +
 .../selftests/drivers/net/netcons_torture.sh       | 130 +++++++++++++++++++++
 2 files changed, 131 insertions(+)
tools/testing/selftests/drivers/net/Makefile +1 -0
index 6e41635bd55a4..71ee69e524d77 100644
@@ -18,6 +18,7 @@ TEST_PROGS := \
 	netcons_fragmented_msg.sh \
 	netcons_overflow.sh \
 	netcons_sysdata.sh \
+	netcons_torture.sh \
 	netpoll_basic.py \
 	ping.py \
 	psp.py \
tools/testing/selftests/drivers/net/netcons_torture.sh new file +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v9 4/4 selftest: netcons: add test for netconsole over bonded interfaces

In reply to Breno Leitao

This patch adds a selftest that verifies netconsole functionality over bonded network interfaces using netdevsim. It sets up two bonded interfaces acting as transmit (TX) and receive (RX) ends, placed in separate network namespaces. The test sends kernel log messages and verifies that they are properly received on the bonded RX interfaces with both IPv4 and IPv6, and using basic and extended netconsole formats.

This patchset aims to test a long-standing netpoll subsystem where netpoll has multiple users. (in this case netconsole and bonding). A similar selftest has been discussed in [1] and [2].

This test also tries to enable bonding and netpoll in different order, just to guarantee that all the possibilities are exercised.

---
 .../testing/selftests/drivers/net/bonding/Makefile |   2 +
 tools/testing/selftests/drivers/net/bonding/config |   4 +
 .../drivers/net/bonding/netcons_over_bonding.sh    | 361 +++++++++++++++++++++
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    |  56 +++-
 4 files changed, 416 insertions(+), 7 deletions(-)
tools/testing/selftests/drivers/net/bonding/Makefile +2 -0
index 402d4ee84f2e8..6c5c60adb5e85 100644
@@ -14,6 +14,7 @@ TEST_PROGS := \
 	dev_addr_lists.sh \
 	mode-1-recovery-updelay.sh \
 	mode-2-recovery-updelay.sh \
+	netcons_over_bonding.sh \
 # end of TEST_PROGS
 
 TEST_FILES := \
@@ -24,6 +25,7 @@ TEST_FILES := \
 
 TEST_INCLUDES := \
 	../../../net/lib.sh \
+	../lib/sh/lib_netcons.sh \
 	../../../net/forwarding/lib.sh \
 # end of TEST_INCLUDES
 
tools/testing/selftests/drivers/net/bonding/config +4 -0
index 6bb290abd48bf..9914943762234 100644
@@ -1,5 +1,6 @@
 CONFIG_BONDING=y
 CONFIG_BRIDGE=y
+CONFIG_CONFIGFS_FS=y
 CONFIG_DUMMY=y
 CONFIG_INET_ESP=y
 CONFIG_INET_ESP_OFFLOAD=y
@@ -9,6 +10,9 @@ CONFIG_MACVLAN=y
 CONFIG_NET_ACT_GACT=y
 CONFIG_NET_CLS_FLOWER=y
 CONFIG_NET_CLS_MATCHALL=m
+CONFIG_NETCONSOLE=m
+CONFIG_NETCONSOLE_DYNAMIC=y
+CONFIG_NETCONSOLE_EXTENDED_LOG=y
 CONFIG_NETDEVSIM=m
 CONFIG_NET_SCH_INGRESS=y
 CONFIG_NLMON=y
tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh new file +361 -0

361 lines not on this page · raw message

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +49 -7

114 lines not on this page · raw message

Signature · 1 line
2.47.3
reviewer Reviewed-by on v9 4/4
#5 lore raw

Re: [PATCH net v9 4/4] selftest: netcons: add test for netconsole over bonded interfaces

Quoted 12 lines · Breno Leitao

This patch adds a selftest that verifies netconsole functionality over bonded network interfaces using netdevsim. It sets up two bonded interfaces acting as transmit (TX) and receive (RX) ends, placed in separate network namespaces. The test sends kernel log messages and verifies that they are properly received on the bonded RX interfaces with both IPv4 and IPv6, and using basic and extended netconsole formats.

This patchset aims to test a long-standing netpoll subsystem where netpoll has multiple users. (in this case netconsole and bonding). A similar selftest has been discussed in [1] and [2].

Quoted 6 lines

This test also tries to enable bonding and netpoll in different order, just to guarantee that all the possibilities are exercised.

Thanks, my understanding is that this address Paolo's review of v7.

...

Quoted 1 line
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +0 -0

...

Quoted 7 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +2 -0
@@ -137,15 +145,17 @@ function _create_dynamic_target() {
 	then
 		echo 1 > "${NCPATH}"/extended
 	fi
+}
 
+function create_and_enable_dynamic_target() {

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh function _create_dynamic_target() { · line 158

Quoted 10 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +2 -2
+	_create_dynamic_target "${FORMAT}" "${NCPATH}"
 	echo 1 > "${NCPATH}"/enabled
-
 }
 
 function create_dynamic_target() {
 	local FORMAT=${1:-"extended"}
 	local NCPATH=${2:-"$NETCONS_PATH"}
-	_create_dynamic_target "${FORMAT}" "${NCPATH}"
+	create_and_enable_dynamic_target "${FORMAT}" "${NCPATH}"

Sorry for not noticing this when I looked over v8.
It's not that important and I don't think it should block progress.

create_and_enable_dynamic_target() seems to only be used here. If so, perhaps the 'enabled' line could simply be added to create_dynamic_target() instead of creating adding create_and_enable_dynamic_target().

Quoted 3 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +0 -0
 	# This will make sure that the kernel was able to
 	# load the netconsole driver configuration. The console message

...

author maintainer
#6 lore raw

Re: [PATCH net v9 4/4] selftest: netcons: add test for netconsole over bonded interfaces

Hello Simon,

Quoted 10 lines · Simon Horman

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh function _create_dynamic_target() { · line 158

Quoted 5 lines · Breno Leitao

function create_dynamic_target() {

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +1 -1
 	local FORMAT=${1:-"extended"}
 	local NCPATH=${2:-"$NETCONS_PATH"}
-	_create_dynamic_target "${FORMAT}" "${NCPATH}"
+	create_and_enable_dynamic_target "${FORMAT}" "${NCPATH}"

Sorry for not noticing this when I looked over v8.
It's not that important and I don't think it should block progress.

Quoted 4 lines

create_and_enable_dynamic_target() seems to only be used here. If so, perhaps the 'enabled' line could simply be added to create_dynamic_target() instead of creating adding create_and_enable_dynamic_target().

This is a good catch. I _think_ it is worth fixing, in fact.

I will send a v10 with this additional change.

	diff --git a/tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh b/tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh
	index 09553ecd50e39..3f891bd68d03c 100644
	--- a/tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh
	+++ b/tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh
	@@ -147,15 +147,12 @@ function _create_dynamic_target() {
		fi
	}

	-function create_and_enable_dynamic_target() {
	-       _create_dynamic_target "${FORMAT}" "${NCPATH}"
	-       echo 1 > "${NCPATH}"/enabled
	-}
	-
	function create_dynamic_target() {
		local FORMAT=${1:-"extended"}
		local NCPATH=${2:-"$NETCONS_PATH"}
	-       create_and_enable_dynamic_target "${FORMAT}" "${NCPATH}"
	+
	+       _create_dynamic_target "${FORMAT}" "${NCPATH}"
	+       echo 1 > "${NCPATH}"/enabled

		# This will make sure that the kernel was able to
		# load the netconsole driver configuration. The console message

Thanks for the review!
--breno

reviewer
#7 lore raw

Re: [PATCH net v9 4/4] selftest: netcons: add test for netconsole over bonded interfaces

Quoted 13 lines · Breno Leitao

Hello Simon,

Quoted 10 lines · Simon Horman

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh function _create_dynamic_target() { · line 158

Quoted 5 lines · Breno Leitao

function create_dynamic_target() {

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +1 -1
 	local FORMAT=${1:-"extended"}
 	local NCPATH=${2:-"$NETCONS_PATH"}
-	_create_dynamic_target "${FORMAT}" "${NCPATH}"
+	create_and_enable_dynamic_target "${FORMAT}" "${NCPATH}"

Sorry for not noticing this when I looked over v8.
It's not that important and I don't think it should block progress.

Quoted 8 lines
Quoted 4 lines

create_and_enable_dynamic_target() seems to only be used here. If so, perhaps the 'enabled' line could simply be added to create_dynamic_target() instead of creating adding create_and_enable_dynamic_target().

This is a good catch. I _think_ it is worth fixing, in fact.

I will send a v10 with this additional change.

Thanks, LGTM.