Skip to content

Commit 675c807

Browse files
HoratiuVulturkuba-moo
authored andcommitted
net: lan966x: Fix usage of lan966x->mac_lock when used by FDB
When the SW bridge was trying to add/remove entries to/from HW, the access to HW was not protected by any lock. In this way, it was possible to have race conditions. Fix this by using the lan966x->mac_lock to protect parallel access to HW for this cases. Fixes: 25ee956 ("net: lan966x: More MAC table functionality") Signed-off-by: Horatiu Vultur <horatiu.vultur@microchip.com> Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent c192468 commit 675c807

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

drivers/net/ethernet/microchip/lan966x/lan966x_mac.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ static struct lan966x_mac_entry *lan966x_mac_find_entry(struct lan966x *lan966x,
201201
struct lan966x_mac_entry *res = NULL;
202202
struct lan966x_mac_entry *mac_entry;
203203

204-
spin_lock(&lan966x->mac_lock);
205204
list_for_each_entry(mac_entry, &lan966x->mac_entries, list) {
206205
if (mac_entry->vid == vid &&
207206
ether_addr_equal(mac, mac_entry->mac) &&
@@ -210,7 +209,6 @@ static struct lan966x_mac_entry *lan966x_mac_find_entry(struct lan966x *lan966x,
210209
break;
211210
}
212211
}
213-
spin_unlock(&lan966x->mac_lock);
214212

215213
return res;
216214
}
@@ -253,8 +251,11 @@ int lan966x_mac_add_entry(struct lan966x *lan966x, struct lan966x_port *port,
253251
{
254252
struct lan966x_mac_entry *mac_entry;
255253

256-
if (lan966x_mac_lookup(lan966x, addr, vid, ENTRYTYPE_NORMAL))
254+
spin_lock(&lan966x->mac_lock);
255+
if (lan966x_mac_lookup(lan966x, addr, vid, ENTRYTYPE_NORMAL)) {
256+
spin_unlock(&lan966x->mac_lock);
257257
return 0;
258+
}
258259

259260
/* In case the entry already exists, don't add it again to SW,
260261
* just update HW, but we need to look in the actual HW because
@@ -263,21 +264,25 @@ int lan966x_mac_add_entry(struct lan966x *lan966x, struct lan966x_port *port,
263264
* add the entry but without the extern_learn flag.
264265
*/
265266
mac_entry = lan966x_mac_find_entry(lan966x, addr, vid, port->chip_port);
266-
if (mac_entry)
267-
return lan966x_mac_learn(lan966x, port->chip_port,
268-
addr, vid, ENTRYTYPE_LOCKED);
267+
if (mac_entry) {
268+
spin_unlock(&lan966x->mac_lock);
269+
goto mac_learn;
270+
}
269271

270272
mac_entry = lan966x_mac_alloc_entry(addr, vid, port->chip_port);
271-
if (!mac_entry)
273+
if (!mac_entry) {
274+
spin_unlock(&lan966x->mac_lock);
272275
return -ENOMEM;
276+
}
273277

274-
spin_lock(&lan966x->mac_lock);
275278
list_add_tail(&mac_entry->list, &lan966x->mac_entries);
276279
spin_unlock(&lan966x->mac_lock);
277280

278-
lan966x_mac_learn(lan966x, port->chip_port, addr, vid, ENTRYTYPE_LOCKED);
279281
lan966x_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, addr, vid, port->dev);
280282

283+
mac_learn:
284+
lan966x_mac_learn(lan966x, port->chip_port, addr, vid, ENTRYTYPE_LOCKED);
285+
281286
return 0;
282287
}
283288

@@ -291,8 +296,9 @@ int lan966x_mac_del_entry(struct lan966x *lan966x, const unsigned char *addr,
291296
list) {
292297
if (mac_entry->vid == vid &&
293298
ether_addr_equal(addr, mac_entry->mac)) {
294-
lan966x_mac_forget(lan966x, mac_entry->mac, mac_entry->vid,
295-
ENTRYTYPE_LOCKED);
299+
lan966x_mac_forget_locked(lan966x, mac_entry->mac,
300+
mac_entry->vid,
301+
ENTRYTYPE_LOCKED);
296302

297303
list_del(&mac_entry->list);
298304
kfree(mac_entry);
@@ -428,6 +434,12 @@ static void lan966x_mac_irq_process(struct lan966x *lan966x, u32 row,
428434
continue;
429435

430436
spin_lock(&lan966x->mac_lock);
437+
mac_entry = lan966x_mac_find_entry(lan966x, mac, vid, dest_idx);
438+
if (mac_entry) {
439+
spin_unlock(&lan966x->mac_lock);
440+
continue;
441+
}
442+
431443
mac_entry = lan966x_mac_alloc_entry(mac, vid, dest_idx);
432444
if (!mac_entry) {
433445
spin_unlock(&lan966x->mac_lock);

0 commit comments

Comments
 (0)