net: netpoll: fix memory leak and add comprehensive selftests
Subsystems (6)
Breno Leitao
maintainerv8 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 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: e120f46768d98151ece8756ebd688b0e43dc8b29
change-id: 20250902-netconsole_torture-8fc23f0aca99
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Breno Leitao
maintainerPATCH v8 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.
--- net/core/netpoll.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/core/netpoll.c +5 -2
@@ -814,6 +814,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;
@@ -823,8 +827,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
Breno Leitao
maintainerPATCH v8 2/4 selftest: netcons: refactor target creation
In reply to Breno LeitaoExtract 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
@@ -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
Breno Leitao
maintainerPATCH v8 3/4 selftest: netcons: create a torture test
In reply to Breno LeitaoCreate 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
@@ -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
Breno Leitao
maintainerPATCH v8 4/4 selftest: netcons: add test for netconsole over bonded interfaces
In reply to Breno LeitaoThis 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
@@ -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
@@ -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
@@ -11,6 +12,9 @@ CONFIG_NET_CLS_FLOWER=y CONFIG_NET_CLS_MATCHALL=m CONFIG_NETDEVSIM=m CONFIG_NET_SCH_INGRESS=y +CONFIG_NETCONSOLE=m +CONFIG_NETCONSOLE_DYNAMIC=y +CONFIG_NETCONSOLE_EXTENDED_LOG=y CONFIG_NLMON=y CONFIG_VETH=y CONFIG_VLAN_8021Q=m
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
Breno Leitao
maintainerRe: [PATCH net v8 4/4] selftest: netcons: add test for netconsole over bonded interfaces
Quoted 12 lines · Breno Leitao
tools/testing/selftests/drivers/net/bonding/config +1
-0@@ -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
tools/testing/selftests/drivers/net/bonding/config CONFIG_NET_CLS_FLOWER=y · line 17
Quoted 6 lines
tools/testing/selftests/drivers/net/bonding/config +3
-0@@ -11,6 +12,9 @@ CONFIG_NET_CLS_FLOWER=y CONFIG_NET_CLS_MATCHALL=m CONFIG_NETDEVSIM=m CONFIG_NET_SCH_INGRESS=y +CONFIG_NETCONSOLE=m +CONFIG_NETCONSOLE_DYNAMIC=y +CONFIG_NETCONSOLE_EXTENDED_LOG=y
I've just realized that check_selftest discards the '_' when checking for the order. This means NETCONSOLE should come before CONFIG_NET_SCH_INGRESS.
I will wait for additional review, before updating.