Netdev List
 help / color / mirror / Atom feed
* [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal
@ 2026-11-20 23:54 Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani
  0 siblings, 2 replies; 5+ messages in thread
From: Tristan Madani @ 2026-11-20 23:54 UTC (permalink / raw)
  To: Samuel Mendoza-Jonas, Paul Fertser
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

The NCSI package/channel/device removal paths call list_del_rcu()
followed by immediate kfree().  This races with concurrent readers
(primarily the unprivileged NCSI_CMD_PKG_INFO netlink handler) that
walk the same lists with list_for_each_entry_rcu() but never enter
an RCU read-side critical section.

Patch 1 adds the missing rcu_read_lock()/rcu_read_unlock() to the
reader paths so kfree_rcu() on the write side has matching readers
to wait for.

Patch 2 defers the free side: channels use call_rcu() with a
callback that also frees filter sub-allocations, packages and the
device structure use kfree_rcu_mightsleep(), and the work is
quiesced before packages are removed.

Pre-existing issues noted by sashiko-bot review -- dangling
active_channel/hot_channel/preferred_channel pointers, unquiesced
per-request timers, and the ftgmac100 unregister ordering -- are not
addressed here.  They affect the overall NCSI teardown design and
are better handled in a dedicated series.

Tristan Madani (2):
  net/ncsi: add missing rcu_read_lock to netlink and device list walks
  net/ncsi: defer free of removed list entries to an RCU grace period

 net/ncsi/internal.h     |  1 +
 net/ncsi/ncsi-manage.c  | 39 ++++++++++++++++++++++++---------------
 net/ncsi/ncsi-netlink.c | 32 +++++++++++++++++++++-----------
 3 files changed, 46 insertions(+), 26 deletions(-)

---

Changes in v2:
- Rework as a 2-patch series fixing both reader and writer sides
  instead of a single patch that only fixed the writer side
- Add rcu_read_lock() on the reader side: ncsi_find_dev(),
  ncsi_write_package_info(), ncsi_pkg_info_all_nl() (sashiko-bot)
- Defer channel sub-allocation frees (mac_filter.addrs,
  vlan_filter.vids) to the RCU callback so readers cannot follow
  stale pointers during the grace period (sashiko-bot)
- Move disable_work_sync() before package removal in
  ncsi_unregister_dev() so the work function is quiesced before
  any structures are freed (sashiko-bot)
- Fix Fixes: tag hash from 2d283bdd5b20 to 2d283bdd079c
  ("net/ncsi: Resource management") (sashiko-bot)
- Include discovery method per sashiko-bot request

-- 
2.47.3

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks
  2026-11-20 23:54 [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal Tristan Madani
@ 2026-11-20 23:54 ` Tristan Madani
  2026-11-24 23:56   ` netdev-bot+sashiko
  2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani
  1 sibling, 1 reply; 5+ messages in thread
From: Tristan Madani @ 2026-11-20 23:54 UTC (permalink / raw)
  To: Samuel Mendoza-Jonas, Paul Fertser
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

The NCSI_FOR_EACH_DEV, NCSI_FOR_EACH_PACKAGE and NCSI_FOR_EACH_CHANNEL
macros wrap list_for_each_entry_rcu(), but several callers never enter
an RCU read-side critical section.

ncsi_find_dev() walks the global ncsi_dev_list without rcu_read_lock().
ncsi_write_package_info() walks the package and channel lists in process
context from the NCSI_CMD_PKG_INFO netlink handler, which does not
require GENL_ADMIN_PERM -- any unprivileged user can trigger the walk.
The dumpit callback ncsi_pkg_info_all_nl() has the same issue in its
initial package lookup.

Without RCU read-side protection these walks race with
ncsi_unregister_dev() which removes entries via list_del_rcu().
kfree_rcu() on the free side would be ineffective without matching
rcu_read_lock() on the reader side.

Add rcu_read_lock()/rcu_read_unlock() around the list walks in
ncsi_find_dev(), ncsi_write_package_info() (converting the early returns
to a goto to consolidate the unlock), and ncsi_pkg_info_all_nl().

The GENL_ADMIN_PERM handlers (set_interface, clear_interface,
set_channel_mask) also walk these lists without RCU protection; they are
less critical since they require CAP_NET_ADMIN, but should be addressed
in a follow-up series.

Found by manual code inspection; not triggered at runtime.

Fixes: 955dc68cb9b23 ("net/ncsi: Add generic netlink family")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 net/ncsi/ncsi-manage.c  | 11 ++++++++---
 net/ncsi/ncsi-netlink.c | 32 +++++++++++++++++++++-----------
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index 1d63958c4429b..ec7c0d8cf3d9f 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -419,13 +419,18 @@ void ncsi_free_request(struct ncsi_request *nr)
 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
 {
 	struct ncsi_dev_priv *ndp;
+	struct ncsi_dev *nd = NULL;
 
+	rcu_read_lock();
 	NCSI_FOR_EACH_DEV(ndp) {
-		if (ndp->ndev.dev == dev)
-			return &ndp->ndev;
+		if (ndp->ndev.dev == dev) {
+			nd = &ndp->ndev;
+			break;
+		}
 	}
+	rcu_read_unlock();
 
-	return NULL;
+	return nd;
 }
 
 static void ncsi_request_timeout(struct timer_list *t)
diff --git a/net/ncsi/ncsi-netlink.c b/net/ncsi/ncsi-netlink.c
index 8cc538358f6a3..f3b403e1a2f2e 100644
--- a/net/ncsi/ncsi-netlink.c
+++ b/net/ncsi/ncsi-netlink.c
@@ -106,37 +106,42 @@ static int ncsi_write_package_info(struct sk_buff *skb,
 	}
 
 	found = false;
+	rcu_read_lock();
 	NCSI_FOR_EACH_PACKAGE(ndp, np) {
 		if (np->id != id)
 			continue;
 		pnest = nla_nest_start_noflag(skb, NCSI_PKG_ATTR);
-		if (!pnest)
-			return -ENOMEM;
+		if (!pnest) {
+			rc = -ENOMEM;
+			goto out_rcu;
+		}
 		rc = nla_put_u32(skb, NCSI_PKG_ATTR_ID, np->id);
 		if (rc) {
 			nla_nest_cancel(skb, pnest);
-			return rc;
+			goto out_rcu;
 		}
 		if ((0x1 << np->id) == ndp->package_whitelist)
 			nla_put_flag(skb, NCSI_PKG_ATTR_FORCED);
 		cnest = nla_nest_start_noflag(skb, NCSI_PKG_ATTR_CHANNEL_LIST);
 		if (!cnest) {
 			nla_nest_cancel(skb, pnest);
-			return -ENOMEM;
+			rc = -ENOMEM;
+			goto out_rcu;
 		}
 		NCSI_FOR_EACH_CHANNEL(np, nc) {
 			nest = nla_nest_start_noflag(skb, NCSI_CHANNEL_ATTR);
 			if (!nest) {
 				nla_nest_cancel(skb, cnest);
 				nla_nest_cancel(skb, pnest);
-				return -ENOMEM;
+				rc = -ENOMEM;
+				goto out_rcu;
 			}
 			rc = ncsi_write_channel_info(skb, ndp, nc);
 			if (rc) {
 				nla_nest_cancel(skb, nest);
 				nla_nest_cancel(skb, cnest);
 				nla_nest_cancel(skb, pnest);
-				return rc;
+				goto out_rcu;
 			}
 			nla_nest_end(skb, nest);
 		}
@@ -145,10 +150,11 @@ static int ncsi_write_package_info(struct sk_buff *skb,
 		found = true;
 	}
 
-	if (!found)
-		return -ENODEV;
+	rc = found ? 0 : -ENODEV;
 
-	return 0;
+out_rcu:
+	rcu_read_unlock();
+	return rc;
 }
 
 static int ncsi_pkg_info_nl(struct sk_buff *msg, struct genl_info *info)
@@ -236,9 +242,13 @@ static int ncsi_pkg_info_all_nl(struct sk_buff *skb,
 
 	package_id = cb->args[0];
 	package = NULL;
+	rcu_read_lock();
 	NCSI_FOR_EACH_PACKAGE(ndp, np)
-		if (np->id == package_id)
+		if (np->id == package_id) {
 			package = np;
+			break;
+		}
+	rcu_read_unlock();
 
 	if (!package)
 		return 0; /* done */
@@ -255,7 +265,7 @@ static int ncsi_pkg_info_all_nl(struct sk_buff *skb,
 		rc = -EMSGSIZE;
 		goto err;
 	}
-	rc = ncsi_write_package_info(skb, ndp, package->id);
+	rc = ncsi_write_package_info(skb, ndp, package_id);
 	if (rc) {
 		nla_nest_cancel(skb, attr);
 		goto err;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period
  2026-11-20 23:54 [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal Tristan Madani
  2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
@ 2026-11-20 23:54 ` Tristan Madani
  2026-11-24 23:56   ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: Tristan Madani @ 2026-11-20 23:54 UTC (permalink / raw)
  To: Samuel Mendoza-Jonas, Paul Fertser
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, stable, Tristan Madani

From: Tristan Madani <tristan@talencesecurity.com>

ncsi_remove_channel(), ncsi_remove_package() and ncsi_unregister_dev()
each call list_del_rcu() and then immediately kfree() the entry.
Concurrent RCU readers that found the entry before the removal can
still dereference the freed object.

ncsi_remove_channel() also kfree()s the channel filter buffers
(mac_filter.addrs, vlan_filter.vids) while the channel is still on
the list.  An unprivileged NCSI_CMD_PKG_INFO caller walking
ncsi_write_channel_info() can follow vlan_filter.vids into freed
memory.

Additionally, ncsi_unregister_dev() frees all packages and channels
before calling disable_work_sync(), so the work function can run on
already-freed structures.

Add struct rcu_head to ncsi_channel and free channels through a
call_rcu() callback that releases the filter buffers and the channel
together, so the memory remains valid for the full RCU grace period.
Use kfree_rcu_mightsleep() for packages and the device structure
which have no sub-allocations accessed by RCU readers.  Move
disable_work_sync() before the package removal loop so the work
function is quiesced before any structures are unlinked.

Found by manual code inspection; not triggered at runtime.

Fixes: 2d283bdd079c ("net/ncsi: Resource management")
Cc: stable@vger.kernel.org
Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
---
 net/ncsi/internal.h    |  1 +
 net/ncsi/ncsi-manage.c | 28 ++++++++++++++++------------
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/net/ncsi/internal.h b/net/ncsi/internal.h
index 2c9d1f22c16a9..2cb1cf653164c 100644
--- a/net/ncsi/internal.h
+++ b/net/ncsi/internal.h
@@ -239,6 +239,7 @@ struct ncsi_channel {
 	} monitor;
 	struct list_head            node;
 	struct list_head            link;
+	struct rcu_head             rcu;
 };
 
 struct ncsi_package {
diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
index ec7c0d8cf3d9f..d872351d40245 100644
--- a/net/ncsi/ncsi-manage.c
+++ b/net/ncsi/ncsi-manage.c
@@ -242,28 +242,33 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
 	return nc;
 }
 
+static void ncsi_channel_free_rcu(struct rcu_head *head)
+{
+	struct ncsi_channel *nc = container_of(head, struct ncsi_channel, rcu);
+
+	kfree(nc->mac_filter.addrs);
+	kfree(nc->vlan_filter.vids);
+	kfree(nc);
+}
+
 static void ncsi_remove_channel(struct ncsi_channel *nc)
 {
 	struct ncsi_package *np = nc->package;
 	unsigned long flags;
 
-	spin_lock_irqsave(&nc->lock, flags);
-
-	/* Release filters */
-	kfree(nc->mac_filter.addrs);
-	kfree(nc->vlan_filter.vids);
+	ncsi_stop_channel_monitor(nc);
 
+	spin_lock_irqsave(&nc->lock, flags);
 	nc->state = NCSI_CHANNEL_INACTIVE;
 	spin_unlock_irqrestore(&nc->lock, flags);
-	ncsi_stop_channel_monitor(nc);
 
-	/* Remove and free channel */
+	/* Remove from list and defer free until after RCU grace period */
 	spin_lock_irqsave(&np->lock, flags);
 	list_del_rcu(&nc->node);
 	np->channel_num--;
 	spin_unlock_irqrestore(&np->lock, flags);
 
-	kfree(nc);
+	call_rcu(&nc->rcu, ncsi_channel_free_rcu);
 }
 
 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
@@ -326,7 +331,7 @@ void ncsi_remove_package(struct ncsi_package *np)
 	ndp->package_num--;
 	spin_unlock_irqrestore(&ndp->lock, flags);
 
-	kfree(np);
+	kfree_rcu_mightsleep(np);
 }
 
 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
@@ -1964,6 +1969,7 @@ void ncsi_unregister_dev(struct ncsi_dev *nd)
 	unsigned long flags;
 
 	dev_remove_pack(&ndp->ptype);
+	disable_work_sync(&ndp->work);
 
 	list_for_each_entry_safe(np, tmp, &ndp->packages, node)
 		ncsi_remove_package(np);
@@ -1972,8 +1978,6 @@ void ncsi_unregister_dev(struct ncsi_dev *nd)
 	list_del_rcu(&ndp->node);
 	spin_unlock_irqrestore(&ncsi_dev_lock, flags);
 
-	disable_work_sync(&ndp->work);
-
-	kfree(ndp);
+	kfree_rcu_mightsleep(ndp);
 }
 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks
  2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
@ 2026-11-24 23:56   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 23:56 UTC (permalink / raw)
  To: tristmd
  Cc: sam, fercerpav, davem, edumazet, kuba, pabeni, horms, netdev,
	linux-kernel, stable, tristan

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The new RCU section in ncsi_find_dev() ends before the pointer
  it found is used.

--

net/ncsi: add missing rcu_read_lock to netlink and device list walks

This commit adds rcu_read_lock()/rcu_read_unlock() around the
NCSI_FOR_EACH_* list walks in ncsi_find_dev(), ncsi_write_package_info()
and ncsi_pkg_info_all_nl(). It also turns the early returns in
ncsi_write_package_info() into a goto so there is a single unlock point.

> diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
> index 1d63958c4429b..ec7c0d8cf3d9f 100644
> --- a/net/ncsi/ncsi-manage.c
> +++ b/net/ncsi/ncsi-manage.c
> @@ -419,13 +419,18 @@ void ncsi_free_request(struct ncsi_request *nr)
>  struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
>  {
>  	struct ncsi_dev_priv *ndp;
> +	struct ncsi_dev *nd = NULL;
>  
> +	rcu_read_lock();
>  	NCSI_FOR_EACH_DEV(ndp) {
> -		if (ndp->ndev.dev == dev)
> -			return &ndp->ndev;
> +		if (ndp->ndev.dev == dev) {
> +			nd = &ndp->ndev;
> +			break;
> +		}
>  	}
> +	rcu_read_unlock();
>  
> -	return NULL;
> +	return nd;
>  }

[Severity: High]
Is ndp still used after this RCU section ends?

ncsi_find_dev() drops the RCU read lock before it returns &ndp->ndev.
ndp_from_ifindex() then drops the netdev reference too:

net/ncsi/ncsi-netlink.c:ndp_from_ifindex() {
    ...
	nd = ncsi_find_dev(dev);
	ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;

	dev_put(dev);
	return ndp;
}

As a result, the NCSI_CMD_PKG_INFO doit handler, which unprivileged users
can call, uses ndp without holding a reference and outside any RCU
read-side section:

ncsi_pkg_info_nl()
  ndp_from_ifindex()
    ncsi_find_dev()                 <- rcu_read_unlock()
  genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL)   <- may sleep
  ncsi_write_package_info(skb, ndp, package_id)
    reads ndp->package_num / ndp->ndev.dev before rcu_read_lock()
    NCSI_FOR_EACH_PACKAGE(ndp, np)  <- list head embedded in ndp

ncsi_pkg_info_all_nl() follows the same pattern. It gets ndp from
ndp_from_ifindex(), walks ndp->packages, and then calls
ncsi_write_package_info(skb, ndp, package_id).

On the free side, ftgmac100_remove()->ncsi_unregister_dev() does:

	spin_lock_irqsave(&ncsi_dev_lock, flags);
	list_del_rcu(&ndp->node);
	spin_unlock_irqrestore(&ncsi_dev_lock, flags);

	disable_work_sync(&ndp->work);

	kfree(ndp);

Can ndp be freed while ncsi_pkg_info_nl() or ncsi_pkg_info_all_nl() is
still using it?

The follow-up patch "net/ncsi: defer free of removed list entries to an
RCU grace period" changes this to kfree_rcu_mightsleep(ndp). Even so, the
grace period can end between the rcu_read_unlock() in ncsi_find_dev() and
the next rcu_read_lock() in ncsi_write_package_info(). The sleeping
genlmsg_new() call sits inside that gap.

The netdev reference can't pin ndp either. ftgmac100_remove() unregisters
NCSI before it unregisters the netdev:

	if (priv->ndev)
		ncsi_unregister_dev(priv->ndev);
	unregister_netdev(netdev);

The commit message says these walks race with ncsi_unregister_dev(). Is
that race still open through the ndp pointer that ncsi_find_dev()
returns? The follow-up patch doesn't change ncsi_find_dev(),
ndp_from_ifindex() or the netlink handlers, so the issue seems to remain
after the full series.

Would this need either a reference count on ndp, or one RCU section that
covers every use of ndp in the handlers, with the sleeping allocation
moved out of it?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120235407.1911365-1-tristmd%40gmail.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period
  2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani
@ 2026-11-24 23:56   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 23:56 UTC (permalink / raw)
  To: tristmd
  Cc: sam, fercerpav, davem, edumazet, kuba, pabeni, horms, netdev,
	linux-kernel, stable, tristan

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 2 (2) · Medium: 0 (1) · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix is incomplete.
- [High] ncsi_remove_channel() unlinks only nc->node from np->channels
  before call_rcu(&nc->rcu, ncsi_channel_free_rcu) (ncsi-…

Pre-existing issues:
- [High] ncsi_unregister_dev() never cancels the per-request timeout
  timers in ndp->requests[] before it frees ndp with…
- [High] ncsi_remove_channel() relies on the flag-gated
  ncsi_stop_channel_monitor() to quiesce nc->monitor.timer before the…
- [Medium] This problem wasn't introduced by this patch, but
  ncsi_unregister_dev() frees ndp without walking and freeing…

--

net/ncsi: defer free of removed list entries to an RCU grace period

This patch changes the channel, package and ncsi_dev_priv frees in the
NCSI removal paths to RCU-deferred frees: call_rcu() for channels and
kfree_rcu_mightsleep() for packages and ndp. It also moves the channel
filter buffer frees into the RCU callback. In ncsi_unregister_dev() it
moves disable_work_sync() ahead of the package removal loop.

> diff --git a/net/ncsi/ncsi-manage.c b/net/ncsi/ncsi-manage.c
> index ec7c0d8cf3d9f..d872351d40245 100644
> --- a/net/ncsi/ncsi-manage.c
> +++ b/net/ncsi/ncsi-manage.c
> @@ -242,28 +242,33 @@ struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)

[ ... ]

>  static void ncsi_remove_channel(struct ncsi_channel *nc)
>  {
>  	struct ncsi_package *np = nc->package;
>  	unsigned long flags;
>  
> -	spin_lock_irqsave(&nc->lock, flags);
> -
> -	/* Release filters */
> -	kfree(nc->mac_filter.addrs);
> -	kfree(nc->vlan_filter.vids);
> +	ncsi_stop_channel_monitor(nc);

[Severity: High]
This is a pre-existing issue, but can this conditional stop guarantee
that nc->monitor.timer is idle before the channel is passed to
call_rcu()?

ncsi_start_channel_monitor() arms the timer after it drops nc->lock:

	spin_lock_irqsave(&nc->lock, flags);
	WARN_ON_ONCE(nc->monitor.enabled);
	nc->monitor.enabled = true;
	nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
	spin_unlock_irqrestore(&nc->lock, flags);

	mod_timer(&nc->monitor.timer, jiffies + HZ);

Suppose an AEN handler such as ncsi_aen_handler_cr() runs
ncsi_stop_channel_monitor() on another CPU inside that window:

CPU1 (config done, work)          CPU2 (AEN, softirq)
ncsi_start_channel_monitor()
  enabled = true
  spin_unlock_irqrestore()
                                  ncsi_stop_channel_monitor()
                                    enabled = false
                                    timer_delete_sync() /* not pending */
  mod_timer()

The timer is now pending while enabled is false. If ncsi_unregister_dev()
runs within the next second, the stop call here takes the early return:

	if (!nc->monitor.enabled) {
		spin_unlock_irqrestore(&nc->lock, flags);
		return;
	}

That skips timer_delete_sync(). ncsi_channel_free_rcu() then frees the
channel while its timer_list is still queued. When the timer fires,
ncsi_channel_monitor() takes nc->lock in freed memory.

Would an unconditional timer_shutdown_sync(&nc->monitor.timer) in
ncsi_remove_channel() be more robust than relying on the
monitor.enabled flag?

>  
> +	spin_lock_irqsave(&nc->lock, flags);
>  	nc->state = NCSI_CHANNEL_INACTIVE;
>  	spin_unlock_irqrestore(&nc->lock, flags);
> -	ncsi_stop_channel_monitor(nc);
>  
> -	/* Remove and free channel */
> +	/* Remove from list and defer free until after RCU grace period */
>  	spin_lock_irqsave(&np->lock, flags);
>  	list_del_rcu(&nc->node);
>  	np->channel_num--;
>  	spin_unlock_irqrestore(&np->lock, flags);
>  
> -	kfree(nc);
> +	call_rcu(&nc->rcu, ncsi_channel_free_rcu);

[Severity: High]
Can the channel still be reached through ndp->channel_queue when it is
passed to call_rcu() here?

Only nc->node is removed from np->channels. The channel can also be
linked through nc->link on ndp->channel_queue, and that list is walked
as an RCU list. For example, ncsi_channel_is_tx() does:

	list_for_each_entry_rcu(channel, &ndp->channel_queue, link) {

and ncsi_process_next_channel() pops entries with:

	nc = list_first_or_null_rcu(&ndp->channel_queue,
				    struct ncsi_channel, link);
	...
	list_del_init(&nc->link);

ncsi_reset_dev() is the only place that drains channel_queue.
ncsi_unregister_dev() does not drain it before ncsi_remove_package().
disable_work_sync() stops the work item but leaves the queued entries in
place.

A channel can still be queued at teardown, for example during a
multi-channel configuration round or after an AEN or VLAN kick. After
the grace period, these channel_queue users could then write through the
freed channel:

- the monitor timeout branch of a channel the removal loop has not
  reached yet. It does list_add_tail_rcu(&nc->link, &ndp->channel_queue)
  and then calls ncsi_process_next_channel().
- ncsi_vlan_rx_add_vid()->ncsi_kick_channels()->ncsi_process_next_channel()
  under RTNL.
- the ncsi_reset_dev() drain, reached from the netlink handlers.

ndp->active_channel, ndp->hot_channel and np->preferred_channel are also
left pointing at the freed channel.

Should nc->link be removed from channel_queue, and those pointers
cleared, before call_rcu()?

>  }
>  
>  struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,

[ ... ]

> @@ -1964,6 +1969,7 @@ void ncsi_unregister_dev(struct ncsi_dev *nd)
>  	unsigned long flags;
>  
>  	dev_remove_pack(&ndp->ptype);
> +	disable_work_sync(&ndp->work);
>  
>  	list_for_each_entry_safe(np, tmp, &ndp->packages, node)
>  		ncsi_remove_package(np);
> @@ -1972,8 +1978,6 @@ void ncsi_unregister_dev(struct ncsi_dev *nd)
>  	list_del_rcu(&ndp->node);
>  	spin_unlock_irqrestore(&ncsi_dev_lock, flags);
>  
> -	disable_work_sync(&ndp->work);
> -
> -	kfree(ndp);
> +	kfree_rcu_mightsleep(ndp);

[Severity: High]
Does the deferred free actually close the NCSI_CMD_PKG_INFO path named in
the commit message?

The grace period only protects readers that stay in a single RCU
read-side section from lookup to last use. ncsi_find_dev() leaves its
RCU section before it returns:

	rcu_read_lock();
	NCSI_FOR_EACH_DEV(ndp) {
		if (ndp->ndev.dev == dev) {
			nd = &ndp->ndev;
			break;
		}
	}
	rcu_read_unlock();

	return nd;

ndp_from_ifindex() then returns ndp without taking a reference. The
unprivileged path looks like this:

ncsi_pkg_info_nl()
  ndp_from_ifindex()
    ncsi_find_dev()                          /* RCU section ends */
  genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL) /* may sleep */
  ncsi_write_package_info()
    if (id > ndp->package_num - 1)           /* no RCU held */
    rcu_read_lock();
    NCSI_FOR_EACH_PACKAGE(ndp, np)

ncsi_unregister_dev() holds neither genl_mutex nor RTNL. ndp also stays
on ncsi_dev_list until every package has been torn down.
ftgmac100_remove() calls ncsi_unregister_dev() before unregister_netdev():

	if (priv->ndev)
		ncsi_unregister_dev(priv->ndev);
	unregister_netdev(netdev);

As a result, ifindex lookups keep succeeding during teardown.

If the grace period for kfree_rcu_mightsleep(ndp) ends while
ncsi_pkg_info_nl() is sleeping in genlmsg_new(), wouldn't
ndp->package_num be read from freed memory?

The admin handlers ncsi_set_interface_nl(), ncsi_clear_interface_nl()
and ncsi_set_channel_mask_nl() look similar. They walk ndp->packages and
np->channels with NCSI_FOR_EACH_PACKAGE() and NCSI_FOR_EACH_CHANNEL().
Both are list_for_each_entry_rcu(), yet none of these handlers takes
rcu_read_lock(). They then do things like:

	spin_lock_irqsave(&package->lock, flags);
	...
	package->preferred_channel = channel;

and call ncsi_reset_dev(), which walks the same lists.

Could these handlers lock and write a package or channel that
kfree_rcu_mightsleep() or the new call_rcu() callback has already freed?
With CONFIG_PROVE_RCU_LIST, these walks should also trigger RCU-list
lockdep warnings.

ftgmac100_stop() also calls ncsi_stop_dev(priv->ndev) later in the
removal path, which would use ndp after it has been freed. RCU does not
cover that use either.

Would it work better to hold RCU from lookup through last use, take a
reference on ndp, or serialize the genl handlers against
ncsi_unregister_dev()? The last option would also mean removing ndp
from ncsi_dev_list before the packages are torn down.

[Severity: High]
This is a pre-existing issue, but could the request timers in
ndp->requests[] still be armed when ndp is freed here?

ncsi_xmit_cmd() arms a one second timer that is embedded in ndp:

	nr->enabled = true;
	mod_timer(&nr->timer, jiffies + 1 * HZ);

Only the response path (ncsi_rcv_rsp()->ncsi_free_request()) or the
timer itself disarms it. dev_remove_pack() runs first, so outstanding
requests can no longer get a response. The work item and the channel
monitor timers can keep sending commands until they are stopped.

ncsi_unregister_dev() never walks ndp->requests[] to call
timer_delete_sync() or ncsi_free_request(). An RCU grace period
normally ends well before the one second timeout. When the timer then
fires, ncsi_request_timeout() would run on freed memory:

	struct ncsi_dev_priv *ndp = nr->ndp;
	...
	spin_lock_irqsave(&ndp->lock, flags);

The nr->cmd skbs of those requests also appear to be leaked.

[Severity: Medium]
This isn't a bug introduced by this patch, but are the vlan_vid entries
on ndp->vlan_vids leaked here?

ncsi_vlan_rx_add_vid() allocates them and links them with
list_add_rcu(&vlan->list, &ndp->vlan_vids). They are only freed in
ncsi_vlan_rx_kill_vid(), which returns early once ndp has been removed
from ncsi_dev_list:

	nd = ncsi_find_dev(dev);
	if (!nd) {
		netdev_warn(dev, "NCSI: no net_device?\n");
		return 0;
	}

On ftgmac100, unregister_netdev() runs after ncsi_unregister_dev(). Any
VID still registered at unregister time would therefore never be freed.

>  }
>  EXPORT_SYMBOL_GPL(ncsi_unregister_dev);

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120235407.1911365-1-tristmd%40gmail.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-24 23:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 23:54 [PATCH v2 net 0/2] net/ncsi: fix RCU list entry lifetime on package and channel removal Tristan Madani
2026-11-20 23:54 ` [PATCH v2 net 1/2] net/ncsi: add missing rcu_read_lock to netlink and device list walks Tristan Madani
2026-11-24 23:56   ` netdev-bot+sashiko
2026-11-20 23:54 ` [PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period Tristan Madani
2026-11-24 23:56   ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox