[PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry
Daniel Machon
daniel.machon at microchip.com
Fri Dec 4 04:35:45 PST 2026
sparx5_add_mact_entry() looks up or adds a software MAC table entry
under mact_lock, drops the lock, programs the hardware, and only then
sets MAC_ENT_LOCK in the entry's flags. sparx5_mact_handle_entry() also
drops the lock after its list walk, and then reads MAC_ENT_MOVED from
the entry it found.
In the first gap, the MAC table pull can reap the entry as aged,
because MAC_ENT_LOCK is not set yet. In the second, the FDB work can
delete it through sparx5_del_mact_entry(). For a host entry, whose port
is a PGID, the reap also indexes sparx5->ports[] out of bounds.
Hold mact_lock in sparx5_add_mact_entry() from the lookup until the
flags are set, and in sparx5_mact_handle_entry() until it is done with
the entry. find_mact_entry() now expects the lock to be held.
The lock is now also held while the hardware entry is written and while
the bridge is notified with SWITCHDEV_FDB_ADD_TO_BRIDGE.
sparx5_del_mact_entry() already writes the hardware, and the reap
already notifies the bridge, with the lock held. The lock is only taken
by the FDB work and the MAC table pull, neither of which is on the
datapath.
This is in preparation for LAG support, which follows in a later
series.
Signed-off-by: Daniel Machon <daniel.machon at microchip.com>
---
This does not fix a regression, so it is targeted at net-next.
---
.../ethernet/microchip/sparx5/sparx5_mactable.c | 25 ++++++++++++----------
1 file changed, 14 insertions(+), 11 deletions(-)
diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c b/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c
index 57c58a3544714..023039270e2ba 100644
--- a/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c
+++ b/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c
@@ -261,7 +261,8 @@ static struct sparx5_mact_entry *find_mact_entry(struct sparx5 *sparx5,
struct sparx5_mact_entry *mact_entry;
struct sparx5_mact_entry *res = NULL;
- mutex_lock(&sparx5->mact_lock);
+ lockdep_assert_held(&sparx5->mact_lock);
+
list_for_each_entry(mact_entry, &sparx5->mact_entries, list) {
if (mact_entry->vid == vid &&
ether_addr_equal(mac, mact_entry->mac) &&
@@ -270,7 +271,6 @@ static struct sparx5_mact_entry *find_mact_entry(struct sparx5 *sparx5,
break;
}
}
- mutex_unlock(&sparx5->mact_lock);
return res;
}
@@ -306,6 +306,7 @@ int sparx5_add_mact_entry(struct sparx5 *sparx5,
* mact thread to start the frame will reach CPU and the CPU will
* add the entry but without the extern_learn flag.
*/
+ mutex_lock(&sparx5->mact_lock);
mact_entry = find_mact_entry(sparx5, addr, vid, portno);
if (mact_entry)
goto update_hw;
@@ -314,12 +315,12 @@ int sparx5_add_mact_entry(struct sparx5 *sparx5,
* SW is pulling again
*/
mact_entry = alloc_mact_entry(sparx5, addr, vid, portno);
- if (!mact_entry)
- return -ENOMEM;
+ if (!mact_entry) {
+ ret = -ENOMEM;
+ goto out;
+ }
- mutex_lock(&sparx5->mact_lock);
list_add_tail(&mact_entry->list, &sparx5->mact_entries);
- mutex_unlock(&sparx5->mact_lock);
update_hw:
ret = sparx5_mact_learn(sparx5, portno, addr, vid);
@@ -331,6 +332,9 @@ int sparx5_add_mact_entry(struct sparx5 *sparx5,
dev, true);
}
+out:
+ mutex_unlock(&sparx5->mact_lock);
+
return ret;
}
@@ -394,28 +398,27 @@ static void sparx5_mact_handle_entry(struct sparx5 *sparx5,
break;
}
}
- mutex_unlock(&sparx5->mact_lock);
if (found && !(mact_entry->flags & MAC_ENT_MOVED))
/* Present, not moved */
- return;
+ goto out;
if (!found) {
/* Entry not found - now add */
mact_entry = alloc_mact_entry(sparx5, mac, vid, port);
if (!mact_entry)
- return;
+ goto out;
mact_entry->flags |= MAC_ENT_ALIVE;
- mutex_lock(&sparx5->mact_lock);
list_add_tail(&mact_entry->list, &sparx5->mact_entries);
- mutex_unlock(&sparx5->mact_lock);
}
/* New or moved entry - notify bridge */
sparx5_fdb_call_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
mac, vid, sparx5->ports[port]->ndev,
true);
+out:
+ mutex_unlock(&sparx5->mact_lock);
}
static void sparx5_mact_pull_work(struct work_struct *work)
---
base-commit: ffd713dc373629317dc374f71690dae61060ead0
change-id: 20261203-sparx5-mact-entry-lock-74c403df781e
Best regards,
--
Daniel Machon <daniel.machon at microchip.com>
More information about the linux-arm-kernel
mailing list