[PATCH net 2/4] net: sparx5: fix sleep in atomic context in MAC table access

Daniel Machon
2026-05-04T14:44:05Z

sparx5_mact_learn() is called from .ndo_set_rx_mode with
netif_addr_lock_bh held, but takes sparx5->lock which is a mutex.
A mutex may block, which is not allowed from atomic context.

Convert sparx5->lock to a spinlock, switch the hardware completion poll
to readx_poll_timeout_atomic(), and use spin_lock_bh() since no caller
runs in hardirq context.

Observed with CONFIG_PROVE_LOCKING, CONFIG_DEBUG_SPINLOCK,
CONFIG_DEBUG_MUTEXES and CONFIG_DEBUG_ATOMIC_SLEEP enabled:

BUG: sleeping function called from invalid context at kernel/locking/mutex.c:591
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 217, name: ip
preempt_count: 201, expected: 0
Call trace:
__might_resched+0x144/0x248
__might_sleep+0x48/0x7c
__mutex_lock+0x74/0x850
mutex_lock_nested+0x24/0x30
sparx5_mact_learn+0x78/0x100
sparx5_mc_sync+0x40/0x54
__hw_addr_sync_dev+0xc4/0x170
sparx5_set_rx_mode+0x4c/0x58
__dev_set_rx_mode+0x64/0xa4
__dev_open+0x1ec/0x26c

Fixes: b37a1bae742f ("net: sparx5: add mactable support")
Signed-off-by: Daniel Machon <daniel.machon@microchip.com>
---
.../ethernet/microchip/sparx5/sparx5_mactable.c | 24 +++++++++++-----------
.../net/ethernet/microchip/sparx5/sparx5_main.h | 2 +-
2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c b/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c
index 2bf9c5f64151..0797cfa32916 100644
--- a/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c
+++ b/drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c
@@ -50,7 +50,7 @@ static int sparx5_mact_wait_for_completion(struct sparx5 *sparx5)
{
u32 val;

- return readx_poll_timeout(sparx5_mact_get_status,
+ return readx_poll_timeout_atomic(sparx5_mact_get_status,
sparx5, val,
LRN_COMMON_ACCESS_CTRL_MAC_TABLE_ACCESS_SHOT_GET(val) == 0,
TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
@@ -92,7 +92,7 @@ int sparx5_mact_learn(struct sparx5 *sparx5, int pgid,
addr = pgid - consts->n_ports;
}

- mutex_lock(&sparx5->lock);
+ spin_lock_bh(&sparx5->lock);

sparx5_mact_select(sparx5, mac, vid);

@@ -111,7 +111,7 @@ int sparx5_mact_learn(struct sparx5 *sparx5, int pgid,

ret = sparx5_mact_wait_for_completion(sparx5);

- mutex_unlock(&sparx5->lock);
+ spin_unlock_bh(&sparx5->lock);

return ret;
}
@@ -164,7 +164,7 @@ bool sparx5_mact_getnext(struct sparx5 *sparx5,
u32 cfg2;
int ret;

- mutex_lock(&sparx5->lock);
+ spin_lock_bh(&sparx5->lock);

sparx5_mact_select(sparx5, mac, *vid);

@@ -183,7 +183,7 @@ bool sparx5_mact_getnext(struct sparx5 *sparx5,
*pcfg2 = cfg2;
}

- mutex_unlock(&sparx5->lock);
+ spin_unlock_bh(&sparx5->lock);

return ret == 0;
}
@@ -194,7 +194,7 @@ int sparx5_mact_find(struct sparx5 *sparx5,
int ret;
u32 cfg2;

- mutex_lock(&sparx5->lock);
+ spin_lock_bh(&sparx5->lock);

sparx5_mact_select(sparx5, mac, vid);

@@ -212,7 +212,7 @@ int sparx5_mact_find(struct sparx5 *sparx5,
ret = -ENOENT;
}

- mutex_unlock(&sparx5->lock);
+ spin_unlock_bh(&sparx5->lock);

return ret;
}
@@ -222,7 +222,7 @@ int sparx5_mact_forget(struct sparx5 *sparx5,
{
int ret;

- mutex_lock(&sparx5->lock);
+ spin_lock_bh(&sparx5->lock);

sparx5_mact_select(sparx5, mac, vid);

@@ -233,7 +233,7 @@ int sparx5_mact_forget(struct sparx5 *sparx5,

ret = sparx5_mact_wait_for_completion(sparx5);

- mutex_unlock(&sparx5->lock);
+ spin_unlock_bh(&sparx5->lock);

return ret;
}
@@ -440,7 +440,7 @@ static void sparx5_mact_pull_work(struct work_struct *work)
vid = 0;
memset(mac, 0, sizeof(mac));
do {
- mutex_lock(&sparx5->lock);
+ spin_lock_bh(&sparx5->lock);
sparx5_mact_select(sparx5, mac, vid);
spx5_wr(LRN_SCAN_NEXT_CFG_SCAN_NEXT_UNTIL_FOUND_ENA_SET(1),
sparx5, LRN_SCAN_NEXT_CFG);
@@ -451,7 +451,7 @@ static void sparx5_mact_pull_work(struct work_struct *work)
ret = sparx5_mact_wait_for_completion(sparx5);
if (ret == 0)
ret = sparx5_mact_get(sparx5, mac, &vid, &cfg2);
- mutex_unlock(&sparx5->lock);
+ spin_unlock_bh(&sparx5->lock);
if (ret == 0)
sparx5_mact_handle_entry(sparx5, mac, vid, cfg2);
} while (ret == 0);
@@ -493,7 +493,7 @@ int sparx5_mact_init(struct sparx5 *sparx5)
{
char queue_name[32];

- mutex_init(&sparx5->lock);
+ spin_lock_init(&sparx5->lock);

/* Flush MAC table */
spx5_wr(LRN_COMMON_ACCESS_CTRL_CPU_ACCESS_CMD_SET(MAC_CMD_CLEAR_ALL) |
diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h
index d5e6644ff124..8ef4050ceecc 100644
--- a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h
+++ b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h
@@ -373,7 +373,7 @@ struct sparx5 {
u32 features;
void __iomem *regs[NUM_TARGETS];
int port_count;
- struct mutex lock; /* MAC reg lock */
+ spinlock_t lock; /* MAC reg lock */
/* port structures are in net device */
struct sparx5_port *ports[SPX5_PORTS];
enum sparx5_core_clockfreq coreclock;

--
2.34.1