Skip to content

Commit 7c47ced

Browse files
committed
vxlan: move IGMP join/leave to work queue
Do join/leave from work queue to avoid lock inversion problems between normal socket and RTNL. The code comes out cleaner as well. Uses Cong Wang's suggestion to turn refcnt into a real atomic since now need to handle case where last use of socket is IGMP worker. Signed-off-by: Stephen Hemminger <[email protected]>
1 parent 758c57d commit 7c47ced

File tree

1 file changed

+40
-63
lines changed

1 file changed

+40
-63
lines changed

drivers/net/vxlan.c

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct vxlan_sock {
8585
struct hlist_node hlist;
8686
struct rcu_head rcu;
8787
struct work_struct del_work;
88-
unsigned int refcnt;
88+
atomic_t refcnt;
8989
struct socket *sock;
9090
struct hlist_head vni_list[VNI_HASH_SIZE];
9191
};
@@ -131,6 +131,7 @@ struct vxlan_dev {
131131
__u8 ttl;
132132
u32 flags; /* VXLAN_F_* below */
133133

134+
struct work_struct igmp_work;
134135
unsigned long age_interval;
135136
struct timer_list age_timer;
136137
spinlock_t hash_lock;
@@ -648,76 +649,58 @@ static bool vxlan_snoop(struct net_device *dev,
648649

649650

650651
/* See if multicast group is already in use by other ID */
651-
static bool vxlan_group_used(struct vxlan_net *vn,
652-
const struct vxlan_dev *this)
652+
static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
653653
{
654654
struct vxlan_dev *vxlan;
655655

656656
list_for_each_entry(vxlan, &vn->vxlan_list, next) {
657-
if (vxlan == this)
658-
continue;
659-
660657
if (!netif_running(vxlan->dev))
661658
continue;
662659

663-
if (vxlan->default_dst.remote_ip == this->default_dst.remote_ip)
660+
if (vxlan->default_dst.remote_ip == remote_ip)
664661
return true;
665662
}
666663

667664
return false;
668665
}
669666

670-
/* kernel equivalent to IP_ADD_MEMBERSHIP */
671-
static int vxlan_join_group(struct net_device *dev)
667+
static void vxlan_sock_hold(struct vxlan_sock *vs)
672668
{
673-
struct vxlan_dev *vxlan = netdev_priv(dev);
674-
struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
675-
struct sock *sk = vxlan->vn_sock->sock->sk;
676-
struct ip_mreqn mreq = {
677-
.imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
678-
.imr_ifindex = vxlan->default_dst.remote_ifindex,
679-
};
680-
int err;
681-
682-
/* Already a member of group */
683-
if (vxlan_group_used(vn, vxlan))
684-
return 0;
669+
atomic_inc(&vs->refcnt);
670+
}
685671

686-
/* Need to drop RTNL to call multicast join */
687-
rtnl_unlock();
688-
lock_sock(sk);
689-
err = ip_mc_join_group(sk, &mreq);
690-
release_sock(sk);
691-
rtnl_lock();
672+
static void vxlan_sock_release(struct vxlan_sock *vs)
673+
{
674+
if (!atomic_dec_and_test(&vs->refcnt))
675+
return;
692676

693-
return err;
677+
hlist_del_rcu(&vs->hlist);
678+
queue_work(vxlan_wq, &vs->del_work);
694679
}
695680

696-
697-
/* kernel equivalent to IP_DROP_MEMBERSHIP */
698-
static int vxlan_leave_group(struct net_device *dev)
681+
/* Callback to update multicast group membership.
682+
* Scheduled when vxlan goes up/down.
683+
*/
684+
static void vxlan_igmp_work(struct work_struct *work)
699685
{
700-
struct vxlan_dev *vxlan = netdev_priv(dev);
701-
struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
702-
int err = 0;
703-
struct sock *sk = vxlan->vn_sock->sock->sk;
686+
struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
687+
struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
688+
struct vxlan_sock *vs = vxlan->vn_sock;
689+
struct sock *sk = vs->sock->sk;
704690
struct ip_mreqn mreq = {
705691
.imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
706692
.imr_ifindex = vxlan->default_dst.remote_ifindex,
707693
};
708694

709-
/* Only leave group when last vxlan is done. */
710-
if (vxlan_group_used(vn, vxlan))
711-
return 0;
712-
713-
/* Need to drop RTNL to call multicast leave */
714-
rtnl_unlock();
715695
lock_sock(sk);
716-
err = ip_mc_leave_group(sk, &mreq);
696+
if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
697+
ip_mc_join_group(sk, &mreq);
698+
else
699+
ip_mc_leave_group(sk, &mreq);
717700
release_sock(sk);
718-
rtnl_lock();
719701

720-
return err;
702+
vxlan_sock_release(vs);
703+
dev_put(vxlan->dev);
721704
}
722705

723706
/* Callback from net/ipv4/udp.c to receive packets */
@@ -1249,12 +1232,11 @@ static int vxlan_init(struct net_device *dev)
12491232
static int vxlan_open(struct net_device *dev)
12501233
{
12511234
struct vxlan_dev *vxlan = netdev_priv(dev);
1252-
int err;
12531235

12541236
if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1255-
err = vxlan_join_group(dev);
1256-
if (err)
1257-
return err;
1237+
vxlan_sock_hold(vxlan->vn_sock);
1238+
dev_hold(dev);
1239+
queue_work(vxlan_wq, &vxlan->igmp_work);
12581240
}
12591241

12601242
if (vxlan->age_interval)
@@ -1285,8 +1267,11 @@ static int vxlan_stop(struct net_device *dev)
12851267
{
12861268
struct vxlan_dev *vxlan = netdev_priv(dev);
12871269

1288-
if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)))
1289-
vxlan_leave_group(dev);
1270+
if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1271+
vxlan_sock_hold(vxlan->vn_sock);
1272+
dev_hold(dev);
1273+
queue_work(vxlan_wq, &vxlan->igmp_work);
1274+
}
12901275

12911276
del_timer_sync(&vxlan->age_timer);
12921277

@@ -1355,6 +1340,7 @@ static void vxlan_setup(struct net_device *dev)
13551340

13561341
INIT_LIST_HEAD(&vxlan->next);
13571342
spin_lock_init(&vxlan->hash_lock);
1343+
INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
13581344

13591345
init_timer_deferrable(&vxlan->age_timer);
13601346
vxlan->age_timer.function = vxlan_cleanup;
@@ -1498,8 +1484,8 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
14981484
udp_sk(sk)->encap_type = 1;
14991485
udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
15001486
udp_encap_enable();
1487+
atomic_set(&vs->refcnt, 1);
15011488

1502-
vs->refcnt = 1;
15031489
return vs;
15041490
}
15051491

@@ -1589,7 +1575,7 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,
15891575

15901576
vs = vxlan_find_port(net, vxlan->dst_port);
15911577
if (vs)
1592-
++vs->refcnt;
1578+
atomic_inc(&vs->refcnt);
15931579
else {
15941580
/* Drop lock because socket create acquires RTNL lock */
15951581
rtnl_unlock();
@@ -1606,12 +1592,7 @@ static int vxlan_newlink(struct net *net, struct net_device *dev,
16061592

16071593
err = register_netdevice(dev);
16081594
if (err) {
1609-
if (--vs->refcnt == 0) {
1610-
rtnl_unlock();
1611-
sk_release_kernel(vs->sock->sk);
1612-
kfree(vs);
1613-
rtnl_lock();
1614-
}
1595+
vxlan_sock_release(vs);
16151596
return err;
16161597
}
16171598

@@ -1629,11 +1610,7 @@ static void vxlan_dellink(struct net_device *dev, struct list_head *head)
16291610
hlist_del_rcu(&vxlan->hlist);
16301611
list_del(&vxlan->next);
16311612
unregister_netdevice_queue(dev, head);
1632-
1633-
if (--vs->refcnt == 0) {
1634-
hlist_del_rcu(&vs->hlist);
1635-
queue_work(vxlan_wq, &vs->del_work);
1636-
}
1613+
vxlan_sock_release(vs);
16371614
}
16381615

16391616
static size_t vxlan_get_size(const struct net_device *dev)

0 commit comments

Comments
 (0)