Skip to content

Commit dc489f8

Browse files
wkzdavem330
authored andcommitted
net: bridge: switchdev: Skip MDB replays of deferred events on offload
Before this change, generation of the list of MDB events to replay would race against the creation of new group memberships, either from the IGMP/MLD snooping logic or from user configuration. While new memberships are immediately visible to walkers of br->mdb_list, the notification of their existence to switchdev event subscribers is deferred until a later point in time. So if a replay list was generated during a time that overlapped with such a window, it would also contain a replay of the not-yet-delivered event. The driver would thus receive two copies of what the bridge internally considered to be one single event. On destruction of the bridge, only a single membership deletion event was therefore sent. As a consequence of this, drivers which reference count memberships (at least DSA), would be left with orphan groups in their hardware database when the bridge was destroyed. This is only an issue when replaying additions. While deletion events may still be pending on the deferred queue, they will already have been removed from br->mdb_list, so no duplicates can be generated in that scenario. To a user this meant that old group memberships, from a bridge in which a port was previously attached, could be reanimated (in hardware) when the port joined a new bridge, without the new bridge's knowledge. For example, on an mv88e6xxx system, create a snooping bridge and immediately add a port to it: root@infix-06-0b-00:~$ ip link add dev br0 up type bridge mcast_snooping 1 && \ > ip link set dev x3 up master br0 And then destroy the bridge: root@infix-06-0b-00:~$ ip link del dev br0 root@infix-06-0b-00:~$ mvls atu ADDRESS FID STATE Q F 0 1 2 3 4 5 6 7 8 9 a DEV:0 Marvell 88E6393X 33:33:00:00:00:6a 1 static - - 0 . . . . . . . . . . 33:33:ff:87:e4:3f 1 static - - 0 . . . . . . . . . . ff:ff:ff:ff:ff:ff 1 static - - 0 1 2 3 4 5 6 7 8 9 a root@infix-06-0b-00:~$ The two IPv6 groups remain in the hardware database because the port (x3) is notified of the host's membership twice: once via the original event and once via a replay. Since only a single delete notification is sent, the count remains at 1 when the bridge is destroyed. Then add the same port (or another port belonging to the same hardware domain) to a new bridge, this time with snooping disabled: root@infix-06-0b-00:~$ ip link add dev br1 up type bridge mcast_snooping 0 && \ > ip link set dev x3 up master br1 All multicast, including the two IPv6 groups from br0, should now be flooded, according to the policy of br1. But instead the old memberships are still active in the hardware database, causing the switch to only forward traffic to those groups towards the CPU (port 0). Eliminate the race in two steps: 1. Grab the write-side lock of the MDB while generating the replay list. This prevents new memberships from showing up while we are generating the replay list. But it leaves the scenario in which a deferred event was already generated, but not delivered, before we grabbed the lock. Therefore: 2. Make sure that no deferred version of a replay event is already enqueued to the switchdev deferred queue, before adding it to the replay list, when replaying additions. Fixes: 4f2673b ("net: bridge: add helper to replay port and host-joined mdb entries") Signed-off-by: Tobias Waldekranz <[email protected]> Reviewed-by: Vladimir Oltean <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b4ea9b6 commit dc489f8

File tree

3 files changed

+122
-28
lines changed

3 files changed

+122
-28
lines changed

include/net/switchdev.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ void switchdev_deferred_process(void);
308308
int switchdev_port_attr_set(struct net_device *dev,
309309
const struct switchdev_attr *attr,
310310
struct netlink_ext_ack *extack);
311+
bool switchdev_port_obj_act_is_deferred(struct net_device *dev,
312+
enum switchdev_notifier_type nt,
313+
const struct switchdev_obj *obj);
311314
int switchdev_port_obj_add(struct net_device *dev,
312315
const struct switchdev_obj *obj,
313316
struct netlink_ext_ack *extack);

net/bridge/br_switchdev.c

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -595,21 +595,40 @@ br_switchdev_mdb_replay_one(struct notifier_block *nb, struct net_device *dev,
595595
}
596596

597597
static int br_switchdev_mdb_queue_one(struct list_head *mdb_list,
598+
struct net_device *dev,
599+
unsigned long action,
598600
enum switchdev_obj_id id,
599601
const struct net_bridge_mdb_entry *mp,
600602
struct net_device *orig_dev)
601603
{
602-
struct switchdev_obj_port_mdb *mdb;
604+
struct switchdev_obj_port_mdb mdb = {
605+
.obj = {
606+
.id = id,
607+
.orig_dev = orig_dev,
608+
},
609+
};
610+
struct switchdev_obj_port_mdb *pmdb;
603611

604-
mdb = kzalloc(sizeof(*mdb), GFP_ATOMIC);
605-
if (!mdb)
606-
return -ENOMEM;
612+
br_switchdev_mdb_populate(&mdb, mp);
607613

608-
mdb->obj.id = id;
609-
mdb->obj.orig_dev = orig_dev;
610-
br_switchdev_mdb_populate(mdb, mp);
611-
list_add_tail(&mdb->obj.list, mdb_list);
614+
if (action == SWITCHDEV_PORT_OBJ_ADD &&
615+
switchdev_port_obj_act_is_deferred(dev, action, &mdb.obj)) {
616+
/* This event is already in the deferred queue of
617+
* events, so this replay must be elided, lest the
618+
* driver receives duplicate events for it. This can
619+
* only happen when replaying additions, since
620+
* modifications are always immediately visible in
621+
* br->mdb_list, whereas actual event delivery may be
622+
* delayed.
623+
*/
624+
return 0;
625+
}
626+
627+
pmdb = kmemdup(&mdb, sizeof(mdb), GFP_ATOMIC);
628+
if (!pmdb)
629+
return -ENOMEM;
612630

631+
list_add_tail(&pmdb->obj.list, mdb_list);
613632
return 0;
614633
}
615634

@@ -677,51 +696,50 @@ br_switchdev_mdb_replay(struct net_device *br_dev, struct net_device *dev,
677696
if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
678697
return 0;
679698

680-
/* We cannot walk over br->mdb_list protected just by the rtnl_mutex,
681-
* because the write-side protection is br->multicast_lock. But we
682-
* need to emulate the [ blocking ] calling context of a regular
683-
* switchdev event, so since both br->multicast_lock and RCU read side
684-
* critical sections are atomic, we have no choice but to pick the RCU
685-
* read side lock, queue up all our events, leave the critical section
686-
* and notify switchdev from blocking context.
699+
if (adding)
700+
action = SWITCHDEV_PORT_OBJ_ADD;
701+
else
702+
action = SWITCHDEV_PORT_OBJ_DEL;
703+
704+
/* br_switchdev_mdb_queue_one() will take care to not queue a
705+
* replay of an event that is already pending in the switchdev
706+
* deferred queue. In order to safely determine that, there
707+
* must be no new deferred MDB notifications enqueued for the
708+
* duration of the MDB scan. Therefore, grab the write-side
709+
* lock to avoid racing with any concurrent IGMP/MLD snooping.
687710
*/
688-
rcu_read_lock();
711+
spin_lock_bh(&br->multicast_lock);
689712

690-
hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
713+
hlist_for_each_entry(mp, &br->mdb_list, mdb_node) {
691714
struct net_bridge_port_group __rcu * const *pp;
692715
const struct net_bridge_port_group *p;
693716

694717
if (mp->host_joined) {
695-
err = br_switchdev_mdb_queue_one(&mdb_list,
718+
err = br_switchdev_mdb_queue_one(&mdb_list, dev, action,
696719
SWITCHDEV_OBJ_ID_HOST_MDB,
697720
mp, br_dev);
698721
if (err) {
699-
rcu_read_unlock();
722+
spin_unlock_bh(&br->multicast_lock);
700723
goto out_free_mdb;
701724
}
702725
}
703726

704-
for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
727+
for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
705728
pp = &p->next) {
706729
if (p->key.port->dev != dev)
707730
continue;
708731

709-
err = br_switchdev_mdb_queue_one(&mdb_list,
732+
err = br_switchdev_mdb_queue_one(&mdb_list, dev, action,
710733
SWITCHDEV_OBJ_ID_PORT_MDB,
711734
mp, dev);
712735
if (err) {
713-
rcu_read_unlock();
736+
spin_unlock_bh(&br->multicast_lock);
714737
goto out_free_mdb;
715738
}
716739
}
717740
}
718741

719-
rcu_read_unlock();
720-
721-
if (adding)
722-
action = SWITCHDEV_PORT_OBJ_ADD;
723-
else
724-
action = SWITCHDEV_PORT_OBJ_DEL;
742+
spin_unlock_bh(&br->multicast_lock);
725743

726744
list_for_each_entry(obj, &mdb_list, list) {
727745
err = br_switchdev_mdb_replay_one(nb, dev,

net/switchdev/switchdev.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@
1919
#include <linux/rtnetlink.h>
2020
#include <net/switchdev.h>
2121

22+
static bool switchdev_obj_eq(const struct switchdev_obj *a,
23+
const struct switchdev_obj *b)
24+
{
25+
const struct switchdev_obj_port_vlan *va, *vb;
26+
const struct switchdev_obj_port_mdb *ma, *mb;
27+
28+
if (a->id != b->id || a->orig_dev != b->orig_dev)
29+
return false;
30+
31+
switch (a->id) {
32+
case SWITCHDEV_OBJ_ID_PORT_VLAN:
33+
va = SWITCHDEV_OBJ_PORT_VLAN(a);
34+
vb = SWITCHDEV_OBJ_PORT_VLAN(b);
35+
return va->flags == vb->flags &&
36+
va->vid == vb->vid &&
37+
va->changed == vb->changed;
38+
case SWITCHDEV_OBJ_ID_PORT_MDB:
39+
case SWITCHDEV_OBJ_ID_HOST_MDB:
40+
ma = SWITCHDEV_OBJ_PORT_MDB(a);
41+
mb = SWITCHDEV_OBJ_PORT_MDB(b);
42+
return ma->vid == mb->vid &&
43+
ether_addr_equal(ma->addr, mb->addr);
44+
default:
45+
break;
46+
}
47+
48+
BUG();
49+
}
50+
2251
static LIST_HEAD(deferred);
2352
static DEFINE_SPINLOCK(deferred_lock);
2453

@@ -307,6 +336,50 @@ int switchdev_port_obj_del(struct net_device *dev,
307336
}
308337
EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
309338

339+
/**
340+
* switchdev_port_obj_act_is_deferred - Is object action pending?
341+
*
342+
* @dev: port device
343+
* @nt: type of action; add or delete
344+
* @obj: object to test
345+
*
346+
* Returns true if a deferred item is pending, which is
347+
* equivalent to the action @nt on an object @obj.
348+
*
349+
* rtnl_lock must be held.
350+
*/
351+
bool switchdev_port_obj_act_is_deferred(struct net_device *dev,
352+
enum switchdev_notifier_type nt,
353+
const struct switchdev_obj *obj)
354+
{
355+
struct switchdev_deferred_item *dfitem;
356+
bool found = false;
357+
358+
ASSERT_RTNL();
359+
360+
spin_lock_bh(&deferred_lock);
361+
362+
list_for_each_entry(dfitem, &deferred, list) {
363+
if (dfitem->dev != dev)
364+
continue;
365+
366+
if ((dfitem->func == switchdev_port_obj_add_deferred &&
367+
nt == SWITCHDEV_PORT_OBJ_ADD) ||
368+
(dfitem->func == switchdev_port_obj_del_deferred &&
369+
nt == SWITCHDEV_PORT_OBJ_DEL)) {
370+
if (switchdev_obj_eq((const void *)dfitem->data, obj)) {
371+
found = true;
372+
break;
373+
}
374+
}
375+
}
376+
377+
spin_unlock_bh(&deferred_lock);
378+
379+
return found;
380+
}
381+
EXPORT_SYMBOL_GPL(switchdev_port_obj_act_is_deferred);
382+
310383
static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
311384
static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
312385

0 commit comments

Comments
 (0)