[PATCH v2 net 2/2] net/ncsi: defer free of removed list entries to an RCU grace period
From: Tristan Madani
Date: Fri Nov 20 2026 - 18:54:44 EST
From: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
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@xxxxxxxxxxxxxxx
Signed-off-by: Tristan Madani <tristan@xxxxxxxxxxxxxxxxxxx>
---
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