From: Manuel Janocha <manu…@amperfied.com> The ALE table is accessed through a single index register (ALE_TABLE_CONTROL) and three data registers (ALE_TABLE). Reading an entry writes the index and then reads the data registers; writing an entry fills the data registers and then writes the index with ALE_TABLE_WRITE. Most operations (add/del ucast/mcast/vlan, vlan_add_modify, flush_multicast, set_allmulti, set_unreg_mcast) are read-modify-write sequences over several entries. The driver has no lock around any of this. This did not matter as long as the callers were serialized by someone else. Both ports of a CPSW switch share one ALE, but the port VLAN and address operations run under RTNL, while ndo_set_rx_mode() of am65-cpsw and netcp runs under netif_addr_lock_bh() of one port only and does not take RTNL. Since IPV6_ADD_MEMBERSHIP and friends no longer hold RTNL, joining an IPv6 multicast group (igmp6_group_added() -> dev_mc_add() -> ndo_set_rx_mode()) can run concurrently with a VLAN or address operation on the other port, or on the same port. For example, on two CPUs: CPU0 (eth0 rx_mode, flush) CPU1 (eth1 add_vid) cpsw_ale_read(A): writel(A, TABLE_CONTROL) cpsw_ale_read(B): writel(B, TABLE_CONTROL) readl(ALE_TABLE + 0..2) -> returns entry B cpsw_ale_write(A): entry B is written to index A Entry A is lost and entry B exists twice. Interleaved writes can equally mix the data words of two entries in one table slot. The same happens inside the multi-entry loops, so entries are written back with foreign data or are dropped. Observed on an AM625 board with two am65-cpsw-nuss ports in dual EMAC mode and seven VLANs on the second port: after boot the port VLAN entry (VID 0), the unicast entry of the first port and two VLAN entries were gone, copies of the unicast and of the broadcast entry showed up at two other indexes. The switch then dropped every frame received on the first port (p0_ale_vid_ingress_drop kept rising) while the port could still transmit, and the board was unreachable. Add a spinlock to struct cpsw_ale and take it in all exported functions that access the table. cpsw and cpsw_new are not affected in practice: since commit 0b8c878d1173 ("net: cpsw: Execute ndo_set_rx_mode callback in a work queue") they run rx_mode from a work item under RTNL and are already serialized. The lock is put into the ALE library instead of repeating that approach in the other drivers because the table protocol (index plus data registers) is an invariant of the library itself, one lock covers all users including the ageout timer, and it does not depend on every present and future caller holding RTNL. The lock is taken with spin_lock_irqsave(). netcp calls cpsw_ale_add_vlan() and cpsw_ale_del_vlan() from netcp_rx_add_vid() and netcp_rx_kill_vid() with netcp->lock held and interrupts disabled, so the unlock must not re-enable them. ndo_set_rx_mode() runs with bottom halves disabled and the dev_mc_add()/dev_uc_add() family is not restricted to process context; the other callers run in process context (RTNL, workqueue, ethtool, system suspend). A sleeping lock cannot be used because of the atomic callers. The register accesses under the lock are readl_relaxed()/writel_relaxed() as before; this patch does not change the accessors. The regmap of the ALE is only used for the version, feature and policer fields and is not touched under the lock. cpsw_ale_vlan_add_modify() calls cpsw_ale_add_vlan() with the lock held, so the body of the latter moves into a static helper that does not take the lock. The loops over all entries run with the lock held for the number of table entries; that is the same amount of MMIO as before, just not preemptible by the other port. cpsw_ale_control_set() takes the lock around its read-modify-write as well. ALE_PORTCTL holds several fields of one port in a single word, so two unlocked updates can lose each other's change: the am65-cpsw rx_mode path clearing ALE_PORT_MACONLY_CAF 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() setting ALE_AGEOUT in ALE_CONTROL can overwrite a concurrent update of the bypass or flood bits. cpsw_ale_control_get() stays unlocked. The policer index registers are separate and left alone. The lock is held longest when a VLAN is added: with the 512 entry table of the AM625 a backport of this patch to the TI 6.18.59 vendor kernel measured up to 1.1 ms in cpsw_ale_vlan_add_modify() called from ndo_vlan_rx_add_vid(). That is VLAN configuration, not the data path; the multicast add and delete paths hold it for less. Discovery: observed on a test system and reproduced on the same board. The reproducer joins IPv6 multicast groups in parallel on eth0, eth1 and a VLAN interface of eth1 while adding and deleting a VLAN on eth1. It needs no kernel modification and no error injection. Without this change eth0 stops receiving after about 30 seconds. Testing: hardware test on an AM625 board (two CPSW ports, am65-cpsw-nuss) with net at v7.4-rc3 (ef3e21c61baa) plus an out-of-tree board device tree, built with PROVE_LOCKING, DEBUG_SPINLOCK and DEBUG_ATOMIC_SLEEP. Without this patch the reproducer kills reception within 120 seconds (p0_ale_vid_ingress_drop 0 -> 2366 and rising; link down/up recovers). With this patch it ran for 120 and for 300 seconds (about 21800 joins and 87 VLAN add/delete cycles of three VLANs in the longer run): eth0 and the VLANs kept receiving and lockdep stayed silent. p0_ale_vid_ingress_drop rose by 1 and by 3 during the two runs. Over all 19 runs on net v7.4-rc3 with this patch alone, 2 failed with the VID 0 entry overwritten again. A backport to the TI vendor kernel 6.18.59 passed 28 of 29 runs of 120 to 300 seconds; one 120 second run lost the VID 0 entry again. These remaining failures are not missing serialization: with the lock held, a table read can still return a wrong entry, which the next patch handles. The legacy cpsw, cpsw_new and netcp users of the ALE were only compile-tested (arm64 and arm, W=1). Tooling: the port to net, the locking analysis and this changelog were prepared with an AI coding assistant (Claude). Fixes: 1767bb2d47b7 ("ipv6: mcast: Don't hold RTNL for IPV6_ADD_MEMBERSHIP and MCAST_JOIN_GROUP.") Cc: stab…@vger.kernel.org Signed-off-by: Manuel Janocha <manu…@amperfied.com> --- drivers/net/ethernet/ti/cpsw_ale.c | 86 +++++++++++++++++++++++++++++++++----- drivers/net/ethernet/ti/cpsw_ale.h | 2 + 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/drivers/net/ethernet/ti/cpsw_ale.c b/drivers/net/ethernet/ti/cpsw_ale.c index 9304405c9581..8434803335d5 100644 --- a/drivers/net/ethernet/ti/cpsw_ale.c +++ b/drivers/net/ethernet/ti/cpsw_ale.c @@ -462,8 +462,10 @@ static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry, int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid) { u32 ale_entry[ALE_ENTRY_WORDS]; + unsigned long irq_flags; int ret, idx; + spin_lock_irqsave(&ale->lock, irq_flags); for (idx = 0; idx < ale->params.ale_entries; idx++) { cpsw_ale_read(ale, idx, ale_entry); ret = cpsw_ale_get_entry_type(ale_entry); @@ -491,6 +493,7 @@ int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid) cpsw_ale_write(ale, idx, ale_entry); } + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } EXPORT_SYMBOL_GPL(cpsw_ale_flush_multicast); @@ -510,6 +513,7 @@ int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port, int flags, u16 vid) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; + unsigned long irq_flags; int idx; cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid); @@ -520,15 +524,19 @@ int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port, cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0); cpsw_ale_set_port_num(ale_entry, port, ale->port_num_bits); + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0); if (idx < 0) idx = cpsw_ale_match_free(ale); if (idx < 0) idx = cpsw_ale_find_ageable(ale); - if (idx < 0) + if (idx < 0) { + spin_unlock_irqrestore(&ale->lock, irq_flags); return -ENOMEM; + } cpsw_ale_write(ale, idx, ale_entry); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } EXPORT_SYMBOL_GPL(cpsw_ale_add_ucast); @@ -537,14 +545,19 @@ int cpsw_ale_del_ucast(struct cpsw_ale *ale, const u8 *addr, int port, int flags, u16 vid) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; + unsigned long irq_flags; int idx; + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0); - if (idx < 0) + if (idx < 0) { + spin_unlock_irqrestore(&ale->lock, irq_flags); return -ENOENT; + } cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE); cpsw_ale_write(ale, idx, ale_entry); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } EXPORT_SYMBOL_GPL(cpsw_ale_del_ucast); @@ -553,8 +566,10 @@ int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask, int flags, u16 vid, int mcast_state) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; + unsigned long irq_flags; int idx, mask; + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0); if (idx >= 0) cpsw_ale_read(ale, idx, ale_entry); @@ -575,10 +590,13 @@ int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask, idx = cpsw_ale_match_free(ale); if (idx < 0) idx = cpsw_ale_find_ageable(ale); - if (idx < 0) + if (idx < 0) { + spin_unlock_irqrestore(&ale->lock, irq_flags); return -ENOMEM; + } cpsw_ale_write(ale, idx, ale_entry); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } EXPORT_SYMBOL_GPL(cpsw_ale_add_mcast); @@ -587,12 +605,16 @@ int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask, int flags, u16 vid) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; + unsigned long irq_flags; int mcast_members = 0; int idx; + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0); - if (idx < 0) + if (idx < 0) { + spin_unlock_irqrestore(&ale->lock, irq_flags); return -ENOENT; + } cpsw_ale_read(ale, idx, ale_entry); @@ -609,6 +631,7 @@ int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask, cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE); cpsw_ale_write(ale, idx, ale_entry); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } EXPORT_SYMBOL_GPL(cpsw_ale_del_mcast); @@ -642,8 +665,8 @@ static void cpsw_ale_set_vlan_untag(struct cpsw_ale *ale, u32 *ale_entry, bitmap_clear(ale->p0_untag_vid_mask, vid, 1); } -int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag, - int reg_mcast, int unreg_mcast) +static int __cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, + int untag, int reg_mcast, int unreg_mcast) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; int idx; @@ -681,6 +704,20 @@ int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag, cpsw_ale_write(ale, idx, ale_entry); return 0; } + +int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag, + int reg_mcast, int unreg_mcast) +{ + unsigned long irq_flags; + int ret; + + spin_lock_irqsave(&ale->lock, irq_flags); + ret = __cpsw_ale_add_vlan(ale, vid, port_mask, untag, reg_mcast, + unreg_mcast); + spin_unlock_irqrestore(&ale->lock, irq_flags); + + return ret; +} EXPORT_SYMBOL_GPL(cpsw_ale_add_vlan); static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale, u32 *ale_entry, @@ -726,16 +763,21 @@ static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale, u32 *ale_entry, int cpsw_ale_vlan_del_modify(struct cpsw_ale *ale, u16 vid, int port_mask) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; + unsigned long irq_flags; int idx; + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_vlan(ale, vid); - if (idx < 0) + if (idx < 0) { + spin_unlock_irqrestore(&ale->lock, irq_flags); return -ENOENT; + } cpsw_ale_read(ale, idx, ale_entry); cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask); cpsw_ale_write(ale, idx, ale_entry); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } @@ -744,11 +786,15 @@ EXPORT_SYMBOL_GPL(cpsw_ale_vlan_del_modify); int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask) { u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; + unsigned long irq_flags; int members, idx; + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_vlan(ale, vid); - if (idx < 0) + if (idx < 0) { + spin_unlock_irqrestore(&ale->lock, irq_flags); return -ENOENT; + } cpsw_ale_read(ale, idx, ale_entry); @@ -771,6 +817,7 @@ int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask) } cpsw_ale_write(ale, idx, ale_entry); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } @@ -782,8 +829,10 @@ int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask, u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; int reg_mcast_members, unreg_mcast_members; int vlan_members, untag_members; + unsigned long irq_flags; int idx, ret = 0; + spin_lock_irqsave(&ale->lock, irq_flags); idx = cpsw_ale_match_vlan(ale, vid); if (idx >= 0) cpsw_ale_read(ale, idx, ale_entry); @@ -803,8 +852,9 @@ int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask, reg_mcast_members = (reg_mcast_members & ~port_mask) | reg_mask; unreg_mcast_members = (unreg_mcast_members & ~port_mask) | unreg_mask; - ret = cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members, - reg_mcast_members, unreg_mcast_members); + ret = __cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members, + reg_mcast_members, unreg_mcast_members); + spin_unlock_irqrestore(&ale->lock, irq_flags); if (ret) { dev_err(ale->params.dev, "Unable to add vlan\n"); return ret; @@ -820,9 +870,11 @@ void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask, bool add) { u32 ale_entry[ALE_ENTRY_WORDS]; + unsigned long irq_flags; int unreg_members = 0; int type, idx; + spin_lock_irqsave(&ale->lock, irq_flags); for (idx = 0; idx < ale->params.ale_entries; idx++) { cpsw_ale_read(ale, idx, ale_entry); type = cpsw_ale_get_entry_type(ale_entry); @@ -841,6 +893,7 @@ void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask, unreg_members); cpsw_ale_write(ale, idx, ale_entry); } + spin_unlock_irqrestore(&ale->lock, irq_flags); } EXPORT_SYMBOL_GPL(cpsw_ale_set_unreg_mcast); @@ -883,8 +936,10 @@ cpsw_ale_vlan_set_unreg_mcast_idx(struct cpsw_ale *ale, u32 *ale_entry, void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port) { u32 ale_entry[ALE_ENTRY_WORDS]; + unsigned long irq_flags; int type, idx; + spin_lock_irqsave(&ale->lock, irq_flags); for (idx = 0; idx < ale->params.ale_entries; idx++) { int vlan_members; @@ -907,6 +962,7 @@ void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port) cpsw_ale_write(ale, idx, ale_entry); } + spin_unlock_irqrestore(&ale->lock, irq_flags); } EXPORT_SYMBOL_GPL(cpsw_ale_set_allmulti); @@ -1140,6 +1196,7 @@ int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control, int value) { const struct ale_control_info *info; + unsigned long irq_flags; int offset, shift; u32 tmp, mask; @@ -1160,9 +1217,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_irqsave(&ale->lock, irq_flags); tmp = readl_relaxed(ale->params.ale_regs + offset); tmp = (tmp & ~(mask << shift)) | (value << shift); writel_relaxed(tmp, ale->params.ale_regs + offset); + spin_unlock_irqrestore(&ale->lock, irq_flags); return 0; } @@ -1540,6 +1599,7 @@ struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params) ale = devm_kzalloc_obj(params->dev, *ale); if (!ale) return ERR_PTR(-ENOMEM); + spin_lock_init(&ale->lock); ale->regmap = devm_regmap_init_mmio(params->dev, params->ale_regs, &ale_regmap_cfg); if (IS_ERR(ale->regmap)) { @@ -1639,23 +1699,29 @@ EXPORT_SYMBOL_GPL(cpsw_ale_create); void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data) { + unsigned long irq_flags; int i; + spin_lock_irqsave(&ale->lock, irq_flags); for (i = 0; i < ale->params.ale_entries; i++) { cpsw_ale_read(ale, i, data); data += ALE_ENTRY_WORDS; } + spin_unlock_irqrestore(&ale->lock, irq_flags); } EXPORT_SYMBOL_GPL(cpsw_ale_dump); void cpsw_ale_restore(struct cpsw_ale *ale, u32 *data) { + unsigned long irq_flags; int i; + spin_lock_irqsave(&ale->lock, irq_flags); for (i = 0; i < ale->params.ale_entries; i++) { cpsw_ale_write(ale, i, data); data += ALE_ENTRY_WORDS; } + spin_unlock_irqrestore(&ale->lock, irq_flags); } EXPORT_SYMBOL_GPL(cpsw_ale_restore); diff --git a/drivers/net/ethernet/ti/cpsw_ale.h b/drivers/net/ethernet/ti/cpsw_ale.h index 87b7d1b3a34a..8e47261161fe 100644 --- a/drivers/net/ethernet/ti/cpsw_ale.h +++ b/drivers/net/ethernet/ti/cpsw_ale.h @@ -96,6 +96,8 @@ struct cpsw_ale { u32 vlan_field_bits; unsigned long *p0_untag_vid_mask; const struct ale_entry_fld *vlan_entry_tbl; + /* serializes access to the ALE table registers */ + spinlock_t lock; }; enum cpsw_ale_control {
-- 2.34.1