From: Manuel Janocha <manu…@amperfied.com> cpsw_ale_control_set() updates a control field with an unlocked read-modify-write of a register that holds other fields as well. Two concurrent updates of fields in the same register can lose each other's change. ALE_PORTCTL holds several fields of one port in a single word. The am65-cpsw rx_mode path clears ALE_PORT_MACONLY_CAF and can overwrite an ALE_PORT_STATE write from phylink mac_link_up()/mac_link_down() or from STP, or the other way round. In the same way cpsw_ale_timer() sets ALE_AGEOUT in ALE_CONTROL and can overwrite a concurrent update of the bypass or flood bits in the same register. This does not depend on the RTNL change named in the previous patch. The ageout timer runs in softirq context and phylink mac_link_up() and mac_link_down() are not called under RTNL either, so these updates were never serialized against each other. The problem was not observed on hardware, it was found by review while adding the lock for the table accesses. Take the ALE lock around the read-modify-write in cpsw_ale_control_set(). The lock is only taken with spin_lock_bh() and does not nest with the table functions, none of which calls cpsw_ale_control_set() with the lock held. All callers run in process or softirq context with interrupts enabled. cpsw_ale_control_get() stays unlocked, as it is a single read. The policer index registers are separate and left alone. Testing: compile-tested only (arm64 and arm, W=1). The race was not reproduced. Tooling: this patch and its changelog were prepared with an AI coding assistant (Claude). Fixes: db82173f23c5 ("netdev: driver: ethernet: add cpsw address lookup engine support") Cc: stab…@vger.kernel.org Signed-off-by: Manuel Janocha <manu…@amperfied.com> --- drivers/net/ethernet/ti/cpsw_ale.c | 2 ++ drivers/net/ethernet/ti/cpsw_ale.h | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/ti/cpsw_ale.c b/drivers/net/ethernet/ti/cpsw_ale.c index dc550142ed82..69861d451ce4 100644 --- a/drivers/net/ethernet/ti/cpsw_ale.c +++ b/drivers/net/ethernet/ti/cpsw_ale.c @@ -1205,9 +1205,11 @@ int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control, offset = info->offset + (port * info->port_offset); shift = info->shift + (port * info->port_shift); + spin_lock_bh(&ale->lock); tmp = readl_relaxed(ale->params.ale_regs + offset); tmp = (tmp & ~(mask << shift)) | (value << shift); writel_relaxed(tmp, ale->params.ale_regs + offset); + spin_unlock_bh(&ale->lock); return 0; } diff --git a/drivers/net/ethernet/ti/cpsw_ale.h b/drivers/net/ethernet/ti/cpsw_ale.h index 7276c47ede52..28c1fa8e38a3 100644 --- a/drivers/net/ethernet/ti/cpsw_ale.h +++ b/drivers/net/ethernet/ti/cpsw_ale.h @@ -97,8 +97,9 @@ struct cpsw_ale { unsigned long *p0_untag_vid_mask; const struct ale_entry_fld *vlan_entry_tbl; /* serializes access to the ALE table (index and data registers), - * the VLAN mask mux registers and the writers of p0_untag_vid_mask - * (the RX path reads it without the lock) + * the VLAN mask mux registers, the writers of p0_untag_vid_mask (the + * RX path reads it without the lock) and the control registers + * written by cpsw_ale_control_set() */ spinlock_t lock; };
-- 2.34.1