Skip to contentSkip to conversation

net: netpoll: fix a memleak and create a selftest

PATCH Superseded Breno Leitao 6 participants 19 messages Sep 5, 2025 – Sep 10, 2025

Subsystems (5)

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Breno Leitao

author maintainer
#0 lore raw

v3 cover net: netpoll: fix a memleak and create a selftest

Fix a memory leak issue on netpoll and create a netconsole test that exposes the problem, when run with kmemleak enabled.

This is a merge of two patches I've sent individually and are merged on the same patchset[1][2].

---
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 (3):
netpoll: fix incorrect refcount handling causing incorrect cleanup
selftest: netcons: refactor target creation
selftest: netcons: create a torture test

 net/core/netpoll.c                                 |   7 +-
 tools/testing/selftests/drivers/net/Makefile       |   1 +
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    |  30 +++--
 .../selftests/drivers/net/netcons_torture.sh       | 127 +++++++++++++++++++++
 4 files changed, 152 insertions(+), 13 deletions(-)

---
base-commit: d69eb204c255c35abd9e8cb621484e8074c75eaa
change-id: 20250902-netconsole_torture-8fc23f0aca99

Best regards,

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

PATCH v3 1/3 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.

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 5f65b62346d4e..19676cd379640 100644
@@ -815,6 +815,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;
 
@@ -824,8 +828,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
reviewer Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 36 lines · Breno Leitao

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.

Quoted 4 lines
Fixes efa95b01da18 ("netpoll: fix use after free")
#3 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

In reply to Breno Leitao
Quoted 31 lines · Breno Leitao

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.

Quoted 4 lines

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.

This makes sense to me.

Just curious, did you try the original OOPS reproducer?
https://lore.kernel.org/lkml/96b940137a50e5c387687bb4f57de8b0435a653f.1404857349.git.decot@googlers.com/

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

The discussion on v1 isn't enlightening either:
https://lore.kernel.org/lkml/0f692012238337f2c40893319830ae042523ce18.1404172155.git.decot@googlers.com/

Thanks,
Calvin

Quoted 37 lines
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 5f65b62346d4e..19676cd379640 100644
@@ -815,6 +815,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;
 
@@ -824,8 +828,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 · 2 lines
-- 
2.47.3
maintainer
#4 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

author maintainer
#5 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 7 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

Do you prefer to have a separated bonding selftest, or, is it better to add some bond operations in the torture selftest?

maintainer
#6 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 11 lines · On Tue, 9 Sep 2025 13:17:27 -0700 Breno Leitao wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

Do you prefer to have a separated bonding selftest, or, is it better to add some bond operations in the torture selftest?

Normal test is preferable, given the flakiness rate and patch volume I'm a bit scared of randomized testing as part of CI.

author maintainer
#7 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 9 Sep 2025 13:17:27 -0700 Breno Leitao wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

Do you prefer to have a separated bonding selftest, or, is it better to add some bond operations in the torture selftest?

Normal test is preferable, given the flakiness rate and patch volume I'm a bit scared of randomized testing as part of CI.

Ok, I will create a selftest to cover the netpoll part of bonding, as soon as my understanding is good enough. I don't think it will be quick, but, it is on my hi-pri todo list.

Do you want to have the selftest done before merging this patch, or, can they go in parallel?

maintainer
#8 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 17 lines · On Wed, 10 Sep 2025 07:12:03 -0700 Breno Leitao wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 9 Sep 2025 13:17:27 -0700 Breno Leitao wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

Do you prefer to have a separated bonding selftest, or, is it better to add some bond operations in the torture selftest?

Normal test is preferable, given the flakiness rate and patch volume I'm a bit scared of randomized testing as part of CI.

Quoted 6 lines

Ok, I will create a selftest to cover the netpoll part of bonding, as soon as my understanding is good enough. I don't think it will be quick, but, it is on my hi-pri todo list.

Do you want to have the selftest done before merging this patch, or, can they go in parallel?

I said "it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding."
"In place" means part of CI when we're merging this fix. Please read emails more carefully.

author maintainer
#9 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 22 lines · Jakub Kicinski
Quoted 21 lines · On Wed, 10 Sep 2025 07:12:03 -0700 Breno Leitao wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 9 Sep 2025 13:17:27 -0700 Breno Leitao wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

Do you prefer to have a separated bonding selftest, or, is it better to add some bond operations in the torture selftest?

Normal test is preferable, given the flakiness rate and patch volume I'm a bit scared of randomized testing as part of CI.

Ok, I will create a selftest to cover the netpoll part of bonding, as soon as my understanding is good enough. I don't think it will be quick, but, it is on my hi-pri todo list.

Quoted 7 lines
Quoted 2 lines

Do you want to have the selftest done before merging this patch, or, can they go in parallel?

I said "it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding."
"In place" means part of CI when we're merging this fix. Please read emails more carefully.

Apologies for the misunderstanding, It was unclear that the bonding selftest should come before the fix. Thanks for the clarification.

I am planning to create a selftest similar to the original reported to cause the issue[1], where I create a bond device with two netdevsim, and bind netconsole to it.

[1] https://lore.kernel.org/lkml/96b940137a50e5c387687bb4f57de8b0435a653f.1404857349.git.decot@googlers.com/

#10 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

In reply to Jakub Kicinski
Quoted 7 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 8 Sep 2025 13:47:24 -0700 Calvin Owens wrote:

I wonder if there might be a demon lurking in bonding+netpoll that this was papering over? Not a reason not to fix the leaks IMO, I'm just curious, I don't want to spend time on it if you already did :)

+1, I also feel like it'd be good to have some bonding tests in place when we're removing a hack added specifically for bonding.

I'll disclaimer this by saying up front that I'm not super
familiar with the innards of netpoll.

That said, I looked at commit efa95b01da18 ("netpoll: fix use
after free") and the relevant upstream discussion, and I'm not sure the assertion that "After a bonding master reclaims the netpoll info struct, slaves could still hold a pointer to the reclaimed data" is correct.

I'm not sure the efa9 patch's reference count math is
correct (more on that below).

Second, I'm a bit unsure what's going on with the struct netpoll
*np parameter of __netpoll_setup for the second and subsequent netpoll instances (i.e., second and later call), as the function will unconditionally do

	npinfo->netpoll = np;

	which it seems like would overwrite the "np" supplied by any

prior calls to __netpoll_setup. In bonding, slave_enable_netpoll() stashes the "np" it allocates as slave->np, and slave_disable_netpoll relies on __netpoll_free to free it, so I don't think it's lost, but it seems like netpoll internally only tracks one of these at a time, regardless of the reference count.

On the reference counting, the upstream example from the prior
discussion includes:

mkdir /sys/kernel/config/netconsole/blah
echo 0 > /sys/kernel/config/netconsole/blah/enabled echo bond0 > /sys/kernel/config/netconsole/blah/dev_name echo 192.168.56.42 > /sys/kernel/config/netconsole/blah/remote_ip echo 1 > /sys/kernel/config/netconsole/blah/enabled
# npinfo refcnt ->1
ifenslave bond0 eth1
# npinfo refcnt ->2
ifenslave bond0 eth0
# (this should be optional, preventing ndo_cleanup_nepoll below) # npinfo refcnt ->3

I'm suspicious of the refcnt values here; both then and now, the
npinfo for each of the relevant interfaces is a separate per-interface allocation in __netpoll_setup, so I'm not sure what exactly is supposed to be getting a refcnt of 3.

If there are two netpoll instances using the slave in question
(either directly or via the bond itself), then clearing the np->dev->npinfo pointer looks like the wrong thing to do until the last reference is released.

	-J

---
-Jay Vosburgh, [email protected]

author maintainer
#11 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Hello Jay,

Quoted 9 lines · Jay Vosburgh

Second, I'm a bit unsure what's going on with the struct netpoll
*np parameter of __netpoll_setup for the second and subsequent netpoll instances (i.e., second and later call), as the function will unconditionally do

	npinfo->netpoll = np;

	which it seems like would overwrite the "np" supplied by any

prior calls to __netpoll_setup.

This is clearly a bug. Trying to understand where this field is used, it seems it is not used at all.

I was not able to find any usage of it, and removing it and compiling with allyesconfig didn't complain about any other users also.

I think we should remove it.

commit 5da5611575ce94ca557c194d4147ae3011cedb6f

Author: Breno Leitao <[email protected]>
Date:   Wed Sep 10 06:32:25 2025 -0700

netpoll: remove unused netpoll pointer from netpoll_info

The netpoll_info structure contained a useless pointer back to its associated netpoll. This field is never used, and the assignment in __netpoll_setup() is does not comtemplate multiple instances, as reported by Jay[1].

Drop both the member and its initialization to simplify the structure.

include/linux/netpoll.h +0 -1
index b5ea9882eda8b..f22eec4660405 100644
@@ -55,7 +55,6 @@ struct netpoll_info {
 
 	struct delayed_work tx_work;
 
-	struct netpoll *netpoll;
 	struct rcu_head rcu;
 };
 
net/core/netpoll.c +0 -1
index 5f65b62346d4e..c58faa7471650 100644
@@ -591,7 +591,6 @@ int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
 
 	np->dev = ndev;
 	strscpy(np->dev_name, ndev->name, IFNAMSIZ);
-	npinfo->netpoll = np;
 
 	/* fill up the skb queue */
 	refill_skbs(np);
author maintainer
#12 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

In reply to Calvin Owens
Quoted 37 lines · Calvin Owens
Quoted 31 lines · Breno Leitao

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.

Quoted 4 lines

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.

Quoted 4 lines

This makes sense to me.

Just curious, did you try the original OOPS reproducer?
https://lore.kernel.org/lkml/96b940137a50e5c387687bb4f57de8b0435a653f.1404857349.git.decot@googlers.com/

Yes, but I have not been able to reproduce the problem at all. I've have tested it using netdevsim, and here is a quick log of what I run:

+ modprobe netconsole
+ modprobe bonding mode=4
[ 86.540950] Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation
[ 86.541617] Forcing miimon to 100msec
[ 86.541893] MII link monitoring set to 100 ms
+ echo +bond0
[ 86.547802] bonding: bond0 is being created...
+ ifconfig bond0 192.168.56.3 up
+ mkdir /sys/kernel/config/netconsole/blah
+ echo 0
[ 86.614772] netconsole: network logging has already stopped
./run.sh: line 19: echo: write error: Invalid argument
+ echo bond0
+ echo 192.168.56.42
+ echo 1
[ 86.622318] netconsole: netconsole: local port 6665
[ 86.622550] netconsole: netconsole: local IPv4 address 0.0.0.0
[ 86.622819] netconsole: netconsole: interface name 'bond0'
[ 86.623038] netconsole: netconsole: local ethernet address '00:00:00:00:00:00'
[ 86.623466] netconsole: netconsole: remote port 6666
[ 86.623675] netconsole: netconsole: remote IPv4 address 192.168.56.42
[ 86.623924] netconsole: netconsole: remote ethernet address ff:ff:ff:ff:ff:ff
[ 86.624264] netpoll: netconsole: local IP 192.168.56.3
[ 86.643174] netconsole: network logging started
+ ifenslave bond0 eth1
[ 86.659899] bond0: (slave eth1): Enslaving as a backup interface with a down link
+ ifenslave bond0 eth2
[ 86.687630] bond0: (slave eth2): Enslaving as a backup interface with a down link
+ sleep 3
+ ifenslave -d bond0 eth1
[ 89.735701] bond0: (slave eth1): Releasing backup interface
[ 89.737239] bond0: (slave eth1): the permanent HWaddr of slave - 06:44:84:94:87:c7 - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts
+ sleep 1
+ echo -bond0
[ 90.798676] bonding: bond0 is being deleted...
[ 90.815595] netconsole: network logging stopped on interface bond0 as it unregistered
[ 90.816416] bond0 (unregistering): (slave eth2): Releasing backup interface
[ 90.863054] bond0 (unregistering): Released all slaves
+ ls -lR /
+ tail -30
<snip>

+ echo +bond0
./run.sh: line 39: /sys/class/net/bonding_masters: Permission denied
+ ifconfig bond0 192.168.56.3 up
SIOCSIFADDR: No such device
bond0: ERROR while getting interface flags: No such device
bond0: ERROR while

#13 lore raw

Re: [PATCH net v3 1/3] netpoll: fix incorrect refcount handling causing incorrect cleanup

Quoted 38 lines · Breno Leitao
Quoted 37 lines · Calvin Owens
Quoted 35 lines · Breno Leitao

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.

Quoted 8 lines
Quoted 4 lines

This makes sense to me.

Just curious, did you try the original OOPS reproducer?
https://lore.kernel.org/lkml/96b940137a50e5c387687bb4f57de8b0435a653f.1404857349.git.decot@googlers.com/

Yes, but I have not been able to reproduce the problem at all. I've have tested it using netdevsim, and here is a quick log of what I run:

Nice, thanks for clarifying.

I also tried reverting a few commits like [1] around the time that smell vaguely related, on top of your fix, but the repro still never triggers anything for me either. I was using virtio interfaces in KVM.

The world may never know :)

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=69b0216ac255

Quoted 42 lines

+ modprobe netconsole
+ modprobe bonding mode=4
[ 86.540950] Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation
[ 86.541617] Forcing miimon to 100msec
[ 86.541893] MII link monitoring set to 100 ms
+ echo +bond0
[ 86.547802] bonding: bond0 is being created...
+ ifconfig bond0 192.168.56.3 up
+ mkdir /sys/kernel/config/netconsole/blah
+ echo 0
[ 86.614772] netconsole: network logging has already stopped
./run.sh: line 19: echo: write error: Invalid argument
+ echo bond0
+ echo 192.168.56.42
+ echo 1
[ 86.622318] netconsole: netconsole: local port 6665
[ 86.622550] netconsole: netconsole: local IPv4 address 0.0.0.0
[ 86.622819] netconsole: netconsole: interface name 'bond0'
[ 86.623038] netconsole: netconsole: local ethernet address '00:00:00:00:00:00'
[ 86.623466] netconsole: netconsole: remote port 6666
[ 86.623675] netconsole: netconsole: remote IPv4 address 192.168.56.42
[ 86.623924] netconsole: netconsole: remote ethernet address ff:ff:ff:ff:ff:ff
[ 86.624264] netpoll: netconsole: local IP 192.168.56.3
[ 86.643174] netconsole: network logging started
+ ifenslave bond0 eth1
[ 86.659899] bond0: (slave eth1): Enslaving as a backup interface with a down link
+ ifenslave bond0 eth2
[ 86.687630] bond0: (slave eth2): Enslaving as a backup interface with a down link
+ sleep 3
+ ifenslave -d bond0 eth1
[ 89.735701] bond0: (slave eth1): Releasing backup interface
[ 89.737239] bond0: (slave eth1): the permanent HWaddr of slave - 06:44:84:94:87:c7 - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts
+ sleep 1
+ echo -bond0
[ 90.798676] bonding: bond0 is being deleted...
[ 90.815595] netconsole: network logging stopped on interface bond0 as it unregistered
[ 90.816416] bond0 (unregistering): (slave eth2): Releasing backup interface
[ 90.863054] bond0 (unregistering): Released all slaves
+ ls -lR /
+ tail -30
<snip>

Quoted 2 lines

+ echo +bond0
./run.sh: line 39: /sys/class/net/bonding_masters: Permission denied

I don't get -EACCES here like you seem to, but nothing interesting happens either.

Quoted 4 lines

+ ifconfig bond0 192.168.56.3 up
SIOCSIFADDR: No such device
bond0: ERROR while getting interface flags: No such device
bond0: ERROR while

author maintainer
#14 lore raw

PATCH v3 2/3 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 b6071e80ebbb6..4fc102407e3a6 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
#15 lore raw

Re: [PATCH net v3 2/3] selftest: netcons: refactor target creation

FYI: kernel test robot notices the stable kernel rule is not satisfied.

Hi,

Thanks for your patch.

FYI: kernel test robot notices the stable kernel rule is not satisfied.

The check is based on https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html#option-1

Rule: add the tag "Cc: [email protected]" in the sign-off area to have the patch automatically included in the stable tree.
Subject: [PATCH net v3 2/3] selftest: netcons: refactor target creation

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
reviewer Reviewed-by on v3 2/3
#16 lore raw

Re: [PATCH net v3 2/3] selftest: netcons: refactor target creation

In reply to Breno Leitao
Quoted 9 lines · 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.

author maintainer
#17 lore raw

PATCH v3 3/3 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 984ece05f7f92..2b253b1ff4f38 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 \
 	queues.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
reviewer Reviewed-by on v3 3/3
#18 lore raw

Re: [PATCH net v3 3/3] selftest: netcons: create a torture test

Quoted 15 lines · 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.

Quoted 5 lines

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