Skip to contentSkip to conversation

net: netpoll: fix memory leak and add comprehensive selftests

PATCH Superseded Breno Leitao 2 participants 9 messages Oct 3, 2025 – Oct 13, 2025

Subsystems (6)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Breno Leitao

author maintainer
#0 lore raw

v7 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 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    | 221 +++++++++++++++++++++
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    | 188 ++++++++++++++++--
 .../selftests/drivers/net/netcons_torture.sh       | 127 ++++++++++++
 7 files changed, 530 insertions(+), 20 deletions(-)

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

Best regards,

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

PATCH v7 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 60a05d3b7c249..f4a0023428265 100644
@@ -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
author maintainer
#2 lore raw

PATCH v7 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 v7 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       | 127 +++++++++++++++++++++
 2 files changed, 128 insertions(+)
tools/testing/selftests/drivers/net/Makefile +1 -0
index bd3af9a34e2f2..42b3fb049f868 100644
@@ -17,6 +17,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 +127 -0

127 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH v7 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    | 221 +++++++++++++++++++++
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    | 158 ++++++++++++++-
 4 files changed, 378 insertions(+), 7 deletions(-)
tools/testing/selftests/drivers/net/bonding/Makefile +2 -0
index 2f095cf67d9a0..691e0bef4244c 100644
@@ -8,6 +8,7 @@ TEST_PROGS := \
 	dev_addr_lists.sh \
 	mode-1-recovery-updelay.sh \
 	mode-2-recovery-updelay.sh \
+	netcons_over_bonding.sh \
 	bond_options.sh \
 	bond-eth-type-change.sh \
 	bond_macvlan_ipvlan.sh \
@@ -21,6 +22,7 @@ TEST_FILES := \
 	bond_topo_3d1c.sh
 
 TEST_INCLUDES := \
+	../lib/sh/lib_netcons.sh \
 	../../../net/forwarding/lib.sh \
 	../../../net/lib.sh
 
tools/testing/selftests/drivers/net/bonding/config +4 -0
index e5b7a8db4dfa3..5adc77d3808e1 100644
@@ -1,5 +1,6 @@
 CONFIG_BONDING=y
 CONFIG_BRIDGE=y
+CONFIG_CONFIGFS_FS=y
 CONFIG_DUMMY=y
 CONFIG_IPV6=y
 CONFIG_MACVLAN=y
@@ -8,6 +9,9 @@ CONFIG_NET_ACT_GACT=y
 CONFIG_NET_CLS_FLOWER=y
 CONFIG_NET_CLS_MATCHALL=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 +221 -0

221 lines not on this page · raw message

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

206 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#5 lore raw

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

Quoted 19 lines · Breno Leitao
tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh new file +19 -0
+# Test #1 : Create an bonding interface and attach netpoll into
+# the bonding interface. Netconsole/netpoll should work on
+# the bonding interface.
+send_netcons_msg_through_bond_iface
+echo "test #1: netpoll on bonding interface worked. Test passed" >&2
+
+# Test #2: Attach netpoll to an enslaved interface
+# Try to attach netpoll to an enslaved sub-interface (while still being part of
+# a bonding interface), which shouldn't be allowed
+enable_netpoll_on_enslaved_iface
+echo "test #2: netpoll correctly rejected enslaved interface (expected behavior). Test passed." >&2
+
+# Test #3: Unplug the sub-interface from bond and enable netconsole
+# Detach the interface from a bonding interface and attach netpoll again
+delete_bond_and_reenable_target
+echo "test #3: Able to attach to an unbound interface. Test passed." >&2
+
+# Test #4: Enslave a sub-interface that had netconsole enabled
+# Try to enslave an interface that has netconsole/netpoll enabled.

tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh line 217

Quoted 10 lines
tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh new file +10 -0
+# Previous test has netconsole enabled in BOND_TX1_SLAVE_IF, try to enslave it
+enslave_netcons_enabled_iface
+echo "test #4: Enslaving an interface with netpoll attached. Test passed." >&2
+
+# Test #5: Enslave a sub-interface to a bonding interface
+# Enslave an interface to a bond interface that has netpoll attached
+# At this stage, BOND_TX_MAIN_IF is created and BOND_TX1_SLAVE_IF is part of
+# it. Netconsole is currently disabled
+enslave_iface_to_bond
+echo "test #5: Enslaving an interface to bond+netpoll. Test passed." >&2

I think this is missing the negative/fail to add test case asked by Jakub. AFAICS you should be able to trigger such case trying to add a veth device to the netpoll enabled bond (since the latter carries the IFF_DISABLE_NETPOLL priv_flag).

Quoted 50 lines
tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh new file +4 -0
+
+cleanup_bond
+trap - EXIT
+exit "${EXIT_STATUS}"
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +12 -3
index 9b5ef8074440c..30e4f357b47e9 100644
@@ -28,17 +28,24 @@ NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
 # NAMESPACE will be populated by setup_ns with a random value
 NAMESPACE=""
 
-# IDs for netdevsim
+# IDs for netdevsim. We either use NSIM_DEV_{1,2}_ID for standard test
+# or NSIM_BOND_{T,R}X_{1,2} for the bonding tests. Not both at the
+# same time.
 NSIM_DEV_1_ID=$((256 + RANDOM % 256))
 NSIM_DEV_2_ID=$((512 + RANDOM % 256))
+NSIM_BOND_TX_1=$((768 + RANDOM % 256))
+NSIM_BOND_TX_2=$((1024 + RANDOM % 256))
+NSIM_BOND_RX_1=$((1280 + RANDOM % 256))
+NSIM_BOND_RX_2=$((1536 + RANDOM % 256))
 NSIM_DEV_SYS_NEW="/sys/bus/netdevsim/new_device"
+NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"
+NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"
 
 # Used to create and delete namespaces
 source "${LIBDIR}"/../../../../net/lib.sh
 
 # Create netdevsim interfaces
 create_ifaces() {
-
 	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
 	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
 	udevadm settle 2> /dev/null || true
@@ -54,7 +61,6 @@ create_ifaces() {
 }
 
 link_ifaces() {
-	local NSIM_DEV_SYS_LINK="/sys/bus/netdevsim/link_device"
 	local SRCIF_IFIDX=$(cat /sys/class/net/"$SRCIF"/ifindex)
 	local DSTIF_IFIDX=$(cat /sys/class/net/"$DSTIF"/ifindex)
 
@@ -96,6 +102,33 @@ function select_ipv4_or_ipv6()
 	fi
 }
 
+# Create 4 netdevsim interfaces. Two of them will be bound to TX bonding iface
+# and the other two will be bond to the RX interface (on the other namespace)
+function create_ifaces_bond() {

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh function select_ipv4_or_ipv6() · line 117

Quoted 10 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +10 -0
+	echo "$NSIM_BOND_TX_1" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_BOND_TX_2" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_BOND_RX_1" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_BOND_RX_2" > "$NSIM_DEV_SYS_NEW"
+	udevadm settle 2> /dev/null || true
+
+	local BOND_TX1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_TX_1"
+	local BOND_TX2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_TX_2"
+	local BOND_RX1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_RX_1"
+	local BOND_RX2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_RX_2"

Note that with the create_netdevsim() helper from tools/testing/selftests/net/lib.sh you could create the netdevsim device directly in the target namespace and avoid some duplicate code.

It would be probably safer to create both rx and tx devices in child namespaces.

Quoted 102 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +71 -4
+
+	# TX
+	BOND_TX1_SLAVE_IF=$(find "$BOND_TX1"/net -maxdepth 1 -type d ! \
+		-path "$BOND_TX1"/net -exec basename {} \; | grep -v net)
+	BOND_TX2_SLAVE_IF=$(find "$BOND_TX2"/net -maxdepth 1 -type d ! \
+		-path "$BOND_TX2"/net -exec basename {} \; | grep -v net)
+
+	# RX
+	BOND_RX1_SLAVE_IF=$(find "$BOND_RX1"/net -maxdepth 1 -type d ! \
+		-path "$BOND_RX1"/net -exec basename {} \; | grep -v net)
+	BOND_RX2_SLAVE_IF=$(find "$BOND_RX2"/net -maxdepth 1 -type d ! \
+		-path "$BOND_RX2"/net -exec basename {} \; | grep -v net)
+}
+
 function set_network() {
 	local IP_VERSION=${1:-"ipv4"}
 
@@ -180,8 +213,6 @@ function disable_release_append() {
 }
 
 function do_cleanup() {
-	local NSIM_DEV_SYS_DEL="/sys/bus/netdevsim/del_device"
-
 	# Delete netdevsim devices
 	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL"
 	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL"
@@ -193,14 +224,26 @@ function do_cleanup() {
 	echo "${DEFAULT_PRINTK_VALUES}" > /proc/sys/kernel/printk
 }
 
-function cleanup() {
+function cleanup_netcons() {
 	# delete netconsole dynamic reconfiguration
-	echo 0 > "${NETCONS_PATH}"/enabled
+	# do not fail if the target is already disabled
+	if [[ ! -d "${NETCONS_PATH}" ]]
+	then
+		# in some cases this is called before netcons path is created
+		return
+	fi
+	if [[ $(cat "${NETCONS_PATH}"/enabled) != 0 ]]
+	then
+		echo 0 > "${NETCONS_PATH}"/enabled || true
+	fi
 	# Remove all the keys that got created during the selftest
 	find "${NETCONS_PATH}/userdata/" -mindepth 1 -type d -delete
 	# Remove the configfs entry
 	rmdir "${NETCONS_PATH}"
+}
 
+function cleanup() {
+	cleanup_netcons
 	do_cleanup
 }
 
@@ -377,3 +420,104 @@ function wait_for_port() {
 	# more frequently on IPv6
 	sleep 1
 }
+
+# netdevsim link BOND_TX to BOND_RX interfaces
+function link_ifaces_bond() {
+	local BOND_TX1_SLAVE_IFIDX
+	local BOND_TX2_SLAVE_IFIDX
+	local BOND_RX1_SLAVE_IFIDX
+	local BOND_RX2_SLAVE_IFIDX
+
+	BOND_TX1_SLAVE_IFIDX=$(cat /sys/class/net/"$BOND_TX1_SLAVE_IF"/ifindex)
+	BOND_TX2_SLAVE_IFIDX=$(cat /sys/class/net/"$BOND_TX2_SLAVE_IF"/ifindex)
+	BOND_RX1_SLAVE_IFIDX=$(cat /sys/class/net/"$BOND_RX1_SLAVE_IF"/ifindex)
+	BOND_RX2_SLAVE_IFIDX=$(cat /sys/class/net/"$BOND_RX2_SLAVE_IF"/ifindex)
+
+	exec {NAMESPACE_FD}</var/run/netns/"${NAMESPACE}"
+	exec {INITNS_FD}</proc/self/ns/net
+
+	# Bind the dst interfaces to namespace
+	ip link set "${BOND_RX1_SLAVE_IF}" netns "${NAMESPACE}"
+	ip link set "${BOND_RX2_SLAVE_IF}" netns "${NAMESPACE}"
+
+	# Linking TX ifaces to the RX ones (on the other namespace)
+	echo "${INITNS_FD}:$BOND_TX1_SLAVE_IFIDX $NAMESPACE_FD:$BOND_RX1_SLAVE_IFIDX"  \
+		> "$NSIM_DEV_SYS_LINK"
+	echo "${INITNS_FD}:$BOND_TX2_SLAVE_IFIDX $NAMESPACE_FD:$BOND_RX2_SLAVE_IFIDX"  \
+		> "$NSIM_DEV_SYS_LINK"
+}
+
+# Create "bond_tx_XX" and "bond_rx_XX" interfaces, and set DSTIF and SRCIF with
+# the bonding interfaces
+function setup_bonding_ifaces() {
+	local RAND=$(( RANDOM % 100 ))
+	BOND_TX_MAIN_IF="bond_tx_$RAND"
+	BOND_RX_MAIN_IF="bond_rx_$RAND"
+
+	if ! ip link add "${BOND_TX_MAIN_IF}" type bond mode balance-rr
+	then
+		echo "Failed to create bond TX interface. Is CONFIG_BONDING set?" >&2
+		# only clean nsim ifaces and namespace. Nothing else has been
+		# initialized
+		cleanup_bond_nsim
+		trap - EXIT
+		exit "${ksft_skip}"
+	fi

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

Quoted 10 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +10 -0
+	ip link set "${BOND_TX1_SLAVE_IF}" down
+	ip link set "${BOND_TX2_SLAVE_IF}" down
+
+	ip link set "${BOND_TX1_SLAVE_IF}" master "${BOND_TX_MAIN_IF}"
+	ip link set "${BOND_TX2_SLAVE_IF}" master "${BOND_TX_MAIN_IF}"
+	ip link set "${BOND_TX_MAIN_IF}" up
+
+	# now create the RX bonding iface
+	ip netns exec "${NAMESPACE}" \
+		ip link add "${BOND_RX_MAIN_IF}" type bond mode balance-rr

Minor nit:

	ip -n "${NAMESPACE}" link ...

will yield the same result with a little less wording.

Quoted 14 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +14 -0
+	ip netns exec "${NAMESPACE}" \
+		ip link set "${BOND_RX1_SLAVE_IF}" down
+	ip netns exec "${NAMESPACE}" \
+		ip link set "${BOND_RX2_SLAVE_IF}" down
+
+	ip netns exec "${NAMESPACE}" \
+		ip link set "${BOND_RX1_SLAVE_IF}" master "${BOND_RX_MAIN_IF}"
+	ip netns exec "${NAMESPACE}" \
+		ip link set "${BOND_RX2_SLAVE_IF}" master "${BOND_RX_MAIN_IF}"
+	ip netns exec "${NAMESPACE}" \
+		ip link set "${BOND_RX_MAIN_IF}" up
+	ip netns exec "${NAMESPACE}" \
+		ip link set "${BOND_RX1_SLAVE_IF}" up
+	ip netns exec "${NAMESPACE}" \

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

Quoted 10 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +10 -0
+		ip link set "${BOND_RX2_SLAVE_IF}" up
+}
+
+# Clean up netdevsim ifaces created for bonding test
+function cleanup_bond_nsim() {
+	echo "$NSIM_BOND_TX_1" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_TX_2" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_RX_1" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_RX_2" > "$NSIM_DEV_SYS_DEL"
+	cleanup_all_ns

If all devices are created in child netns, you will not need explicit per device cleanup.

/P

author maintainer
#6 lore raw

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

Hello Paolo,

Quoted 13 lines · Paolo Abeni

tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh line 217

Quoted 6 lines · Breno Leitao
tools/testing/selftests/drivers/net/bonding/netcons_over_bonding.sh new file +6 -0
+# Test #5: Enslave a sub-interface to a bonding interface
+# Enslave an interface to a bond interface that has netpoll attached
+# At this stage, BOND_TX_MAIN_IF is created and BOND_TX1_SLAVE_IF is part of
+# it. Netconsole is currently disabled
+enslave_iface_to_bond
+echo "test #5: Enslaving an interface to bond+netpoll. Test passed." >&2

I think this is missing the negative/fail to add test case asked by Jakub. AFAICS you should be able to trigger such case trying to add a veth device to the netpoll enabled bond (since the latter carries the IFF_DISABLE_NETPOLL priv_flag).

Thanks for the review. I misunderstood what Jakub said, sorry about it.

I've tried to enslave a veth interface manually into a bonding interface, and I can see:

	# ip link set veth0 master bond_tx_78
	 aborting
	 RTNETLINK answers: Device or resource busy

and dmesg shows:

netpoll: (null): veth0 doesn't support polling,

If that is the test case that is missing, I will add it as an additional test in the selftest, and send a new version.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +7 -0
+function create_ifaces_bond() {
+	echo "$NSIM_BOND_TX_1" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_BOND_TX_2" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_BOND_RX_1" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_BOND_RX_2" > "$NSIM_DEV_SYS_NEW"
+	udevadm settle 2> /dev/null || true
+
Quoted 8 lines

tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh function select_ipv4_or_ipv6() · line 117

Quoted 4 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +4 -0
+	local BOND_TX1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_TX_1"
+	local BOND_TX2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_TX_2"
+	local BOND_RX1=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_RX_1"
+	local BOND_RX2=/sys/bus/netdevsim/devices/netdevsim"$NSIM_BOND_RX_2"

Note that with the create_netdevsim() helper from tools/testing/selftests/net/lib.sh you could create the netdevsim device directly in the target namespace and avoid some duplicate code.

Awesome. I am more than happy to create_netdevsim() in this selftest, and move the others to use it as well.

Quoted 2 lines

It would be probably safer to create both rx and tx devices in child namespaces.

Sure, that is doable, but, I need to change a few common helpers, to start netconsole from inside the "tx namespace" instead of the default namespace.

Given all the other netconsole selftest uses TX from the default net namespace, I would like to move them at all the same time.

Do you think it is Ok to have this test using TX interfaces from the main net namespace (as is now), and then I submit a follow patch to migrate all the netcons tests (including this one) to use a TX namespace? Then I can change the helpers at the same time, simplifying the code review.

Quoted 9 lines

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

Quoted 3 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +3 -0
+	# now create the RX bonding iface
+	ip netns exec "${NAMESPACE}" \
+		ip link add "${BOND_RX_MAIN_IF}" type bond mode balance-rr

Minor nit:

	ip -n "${NAMESPACE}" link ...

will yield the same result with a little less wording.

Ack. I will update. Thanks

Quoted 10 lines

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

Quoted 7 lines
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +7 -0
+# Clean up netdevsim ifaces created for bonding test
+function cleanup_bond_nsim() {
+	echo "$NSIM_BOND_TX_1" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_TX_2" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_RX_1" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_RX_2" > "$NSIM_DEV_SYS_DEL"
+	cleanup_all_ns

If all devices are created in child netns, you will not need explicit per device cleanup.

Ack!

Thanks for the review,
--breno

author maintainer
#7 lore raw

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

Quoted 14 lines · Breno Leitao
Quoted 3 lines

Note that with the create_netdevsim() helper from tools/testing/selftests/net/lib.sh you could create the netdevsim device directly in the target namespace and avoid some duplicate code.

Awesome. I am more than happy to create_netdevsim() in this selftest, and move the others to use it as well.

Quoted 2 lines

It would be probably safer to create both rx and tx devices in child namespaces.

Sure, that is doable, but, I need to change a few common helpers, to start netconsole from inside the "tx namespace" instead of the default namespace.

Quoted 8 lines

Given all the other netconsole selftest uses TX from the default net namespace, I would like to move them at all the same time.

Do you think it is Ok to have this test using TX interfaces from the main net namespace (as is now), and then I submit a follow patch to migrate all the netcons tests (including this one) to use a TX namespace? Then I can change the helpers at the same time, simplifying the code review.

In fact, I was able to isolate the functions for the isolation in the self test, and now I have a test that relies on create_netdevsim() and have two namespaces, one for TX and one for RX.

The problem is that it hits a bug on netdevsim that doesn't allow it to run properly. Basically create_netdevsim() put the interface up, and I need to get the interface down in order to enslaved it, losing the carrier.

I've propsoed a fix in here:

https://lore.kernel.org/all/[email protected]/

So, I will send a new version for this patchset soon, but, it will probably not pass the CI, given it will not have the fix aboveyet. But I will send it anyway, so, people can further review the patchset.

Thanks
--breno

author maintainer
#8 lore raw

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

In reply to Paolo Abeni
Quoted 11 lines · Paolo Abeni

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

Quoted 7 lines · Breno Leitao
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +7 -0
+# Clean up netdevsim ifaces created for bonding test
+function cleanup_bond_nsim() {
+	echo "$NSIM_BOND_TX_1" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_TX_2" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_RX_1" > "$NSIM_DEV_SYS_DEL"
+	echo "$NSIM_BOND_RX_2" > "$NSIM_DEV_SYS_DEL"
+	cleanup_all_ns

If all devices are created in child netns, you will not need explicit per device cleanup.

Humm, that is what I was expecting as well, but, when I tried it, I found that the interfaces got re-pareted by the main network namespace when the namespace is deleted.

For instance, in the following example, eth1 belongs to namespace ns1, and when I delete it, it then moves to the main network namespace:

	# ip link
	# ip -n ns1 link
	3: eth1: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 1000
	link/ether d2:3d:b3:3b:59:37 brd ff:ff:ff:ff:ff:ff
	altname eni1np1
	# ip netns delete ns1
	# ip link
	1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
	link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
	3: eth1: <BROADCAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
	link/ether d2:3d:b3:3b:59:37 brd ff:ff:ff:ff:ff:ff
	altname eni1np1