Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry
@ 2026-12-04 12:35 Daniel Machon
  2026-12-04 12:37 ` netdev-bot+sinfo
  2026-12-05 13:01 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel Machon @ 2026-12-04 12:35 UTC (permalink / raw)
  To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Steen Hegelund, UNGLinuxDriver
  Cc: netdev, linux-arm-kernel, Kees Cook

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@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@microchip.com>


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

* Re: [PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry
  2026-12-04 12:35 [PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry Daniel Machon
@ 2026-12-04 12:37 ` netdev-bot+sinfo
  2026-12-05 13:01 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sinfo @ 2026-12-04 12:37 UTC (permalink / raw)
  To: Daniel Machon
  Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Steen Hegelund, UNGLinuxDriver, netdev,
	linux-arm-kernel, Kees Cook

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - How the issue was discovered, e.g. hit in production, hit during
   development, syzbot report, manual code inspection, LLM or static
   analysis tool scan.

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

 - What hardware the change was tested on. For driver fixes please
   mention the device (and if relevant firmware version) used for
   testing, or say that the change was not tested on real hardware.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

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

* Re: [PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry
  2026-12-04 12:35 [PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry Daniel Machon
  2026-12-04 12:37 ` netdev-bot+sinfo
@ 2026-12-05 13:01 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 13:01 UTC (permalink / raw)
  To: daniel.machon
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, Steen.Hegelund,
	UNGLinuxDriver, netdev, linux-arm-kernel, kees+treewide

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

Critical: 0 · High: 0 · Medium: 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).

- [Medium] The patch fixes a real memory-safety bug, but its routing
  understates that.

--

net: sparx5/lan969x: hold mact_lock while using a MAC table entry

This patch keeps mact_lock held in sparx5_add_mact_entry() and
sparx5_mact_handle_entry() for as long as they use a software MAC table
entry. find_mact_entry() no longer takes the lock itself and now asserts
that the caller holds it.

> 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.

[ ... ]

> This is in preparation for LAG support, which follows in a later
> series.

[ ... ]

> This does not fix a regression, so it is targeted at net-next.

[Severity: Medium]
The code change itself looks correct. Should this go to net with Fixes:
tags instead of net-next, though?

The gaps described above look like use-after-free bugs in the existing
code, but the commit message doesn't say so.

sparx5_add_mact_entry() is called from the FDB work on sparx5_owq.
sparx5_mact_pull_work() runs on sparx5->mact_queue. These are different
workqueues, so the two can run at the same time. Before this patch, the
add path could race with the reap like this:

CPU1 (FDB work)
sparx5_add_mact_entry()
    list_add_tail(&mact_entry->list, &sparx5->mact_entries);
    mutex_unlock(&sparx5->mact_lock);
    sparx5_mact_learn(...);

CPU2 (MAC table pull)
sparx5_mact_pull_work()
    /* flags == 0, neither MAC_ENT_ALIVE nor MAC_ENT_LOCK set */
    sparx5_fdb_call_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
                              mact_entry->mac, mact_entry->vid,
                              sparx5->ports[mact_entry->port]->ndev,
                              true);
    list_del(&mact_entry->list);
    devm_kfree(sparx5->dev, mact_entry);

CPU1
    if (mact_entry->flags == 0) {
        mact_entry->flags |= MAC_ENT_LOCK;

The final read and write of mact_entry->flags then touch freed memory.

Host entries are added from sparx5_switchdev_bridge_fdb_event_work() with
sparx5_get_pgid(sparx5, PGID_CPU) as the port. For those, the reap also
reads past the end of sparx5->ports[] before the free.

The pull path has the same problem. sparx5_mact_handle_entry() read
mact_entry->flags & MAC_ENT_MOVED after dropping mact_lock. By then,
sparx5_del_mact_entry() in the FDB work could already have done list_del()
and devm_kfree() on that entry.

Normal FDB add and ageing activity can reach both paths. Would it make
sense to describe them as use-after-free and add tags like these, so that
stable kernels get the fix even though the LAG series also builds on it?

Fixes: b37a1bae742f ("net: sparx5: add mactable support")
Fixes: 9f01cfbf2922 ("net: sparx5: Use Switchdev fdb events for managing fdb entries")

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204-sparx5-mact-entry-lock-v1-1-2a438292ba56%40microchip.com

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

end of thread, other threads:[~2026-12-05 13:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 12:35 [PATCH net-next] net: sparx5/lan969x: hold mact_lock while using a MAC table entry Daniel Machon
2026-12-04 12:37 ` netdev-bot+sinfo
2026-12-05 13:01 ` 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