Skip to content

Commit ae1d48b

Browse files
HSchillummakynes
authored andcommitted
IPVS netns shutdown/startup dead-lock
ip_vs_mutext is used by both netns shutdown code and startup and both implicit uses sk_lock-AF_INET mutex. cleanup CPU-1 startup CPU-2 ip_vs_dst_event() ip_vs_genl_set_cmd() sk_lock-AF_INET __ip_vs_mutex sk_lock-AF_INET __ip_vs_mutex * DEAD LOCK * A new mutex placed in ip_vs netns struct called sync_mutex is added. Comments from Julian and Simon added. This patch has been running for more than 3 month now and it seems to work. Ver. 3 IP_VS_SO_GET_DAEMON in do_ip_vs_get_ctl protected by sync_mutex instead of __ip_vs_mutex as sugested by Julian. Signed-off-by: Hans Schillstrom <hans@schillstrom.com> Acked-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: Simon Horman <horms@verge.net.au> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 98d9ae8 commit ae1d48b

3 files changed

Lines changed: 87 additions & 51 deletions

File tree

‎include/net/ip_vs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ struct netns_ipvs {
900900
volatile int sync_state;
901901
volatile int master_syncid;
902902
volatile int backup_syncid;
903+
struct mutex sync_mutex;
903904
/* multicast interface name */
904905
char master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
905906
char backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];

‎net/netfilter/ipvs/ip_vs_ctl.c‎

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,7 @@ do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
22832283
struct ip_vs_service *svc;
22842284
struct ip_vs_dest_user *udest_compat;
22852285
struct ip_vs_dest_user_kern udest;
2286+
struct netns_ipvs *ipvs = net_ipvs(net);
22862287

22872288
if (!capable(CAP_NET_ADMIN))
22882289
return -EPERM;
@@ -2303,6 +2304,24 @@ do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
23032304
/* increase the module use count */
23042305
ip_vs_use_count_inc();
23052306

2307+
/* Handle daemons since they have another lock */
2308+
if (cmd == IP_VS_SO_SET_STARTDAEMON ||
2309+
cmd == IP_VS_SO_SET_STOPDAEMON) {
2310+
struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2311+
2312+
if (mutex_lock_interruptible(&ipvs->sync_mutex)) {
2313+
ret = -ERESTARTSYS;
2314+
goto out_dec;
2315+
}
2316+
if (cmd == IP_VS_SO_SET_STARTDAEMON)
2317+
ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2318+
dm->syncid);
2319+
else
2320+
ret = stop_sync_thread(net, dm->state);
2321+
mutex_unlock(&ipvs->sync_mutex);
2322+
goto out_dec;
2323+
}
2324+
23062325
if (mutex_lock_interruptible(&__ip_vs_mutex)) {
23072326
ret = -ERESTARTSYS;
23082327
goto out_dec;
@@ -2316,15 +2335,6 @@ do_ip_vs_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
23162335
/* Set timeout values for (tcp tcpfin udp) */
23172336
ret = ip_vs_set_timeout(net, (struct ip_vs_timeout_user *)arg);
23182337
goto out_unlock;
2319-
} else if (cmd == IP_VS_SO_SET_STARTDAEMON) {
2320-
struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2321-
ret = start_sync_thread(net, dm->state, dm->mcast_ifn,
2322-
dm->syncid);
2323-
goto out_unlock;
2324-
} else if (cmd == IP_VS_SO_SET_STOPDAEMON) {
2325-
struct ip_vs_daemon_user *dm = (struct ip_vs_daemon_user *)arg;
2326-
ret = stop_sync_thread(net, dm->state);
2327-
goto out_unlock;
23282338
}
23292339

23302340
usvc_compat = (struct ip_vs_service_user *)arg;
@@ -2584,6 +2594,33 @@ do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
25842594

25852595
if (copy_from_user(arg, user, copylen) != 0)
25862596
return -EFAULT;
2597+
/*
2598+
* Handle daemons first since it has its own locking
2599+
*/
2600+
if (cmd == IP_VS_SO_GET_DAEMON) {
2601+
struct ip_vs_daemon_user d[2];
2602+
2603+
memset(&d, 0, sizeof(d));
2604+
if (mutex_lock_interruptible(&ipvs->sync_mutex))
2605+
return -ERESTARTSYS;
2606+
2607+
if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2608+
d[0].state = IP_VS_STATE_MASTER;
2609+
strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2610+
sizeof(d[0].mcast_ifn));
2611+
d[0].syncid = ipvs->master_syncid;
2612+
}
2613+
if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2614+
d[1].state = IP_VS_STATE_BACKUP;
2615+
strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2616+
sizeof(d[1].mcast_ifn));
2617+
d[1].syncid = ipvs->backup_syncid;
2618+
}
2619+
if (copy_to_user(user, &d, sizeof(d)) != 0)
2620+
ret = -EFAULT;
2621+
mutex_unlock(&ipvs->sync_mutex);
2622+
return ret;
2623+
}
25872624

25882625
if (mutex_lock_interruptible(&__ip_vs_mutex))
25892626
return -ERESTARTSYS;
@@ -2681,28 +2718,6 @@ do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
26812718
}
26822719
break;
26832720

2684-
case IP_VS_SO_GET_DAEMON:
2685-
{
2686-
struct ip_vs_daemon_user d[2];
2687-
2688-
memset(&d, 0, sizeof(d));
2689-
if (ipvs->sync_state & IP_VS_STATE_MASTER) {
2690-
d[0].state = IP_VS_STATE_MASTER;
2691-
strlcpy(d[0].mcast_ifn, ipvs->master_mcast_ifn,
2692-
sizeof(d[0].mcast_ifn));
2693-
d[0].syncid = ipvs->master_syncid;
2694-
}
2695-
if (ipvs->sync_state & IP_VS_STATE_BACKUP) {
2696-
d[1].state = IP_VS_STATE_BACKUP;
2697-
strlcpy(d[1].mcast_ifn, ipvs->backup_mcast_ifn,
2698-
sizeof(d[1].mcast_ifn));
2699-
d[1].syncid = ipvs->backup_syncid;
2700-
}
2701-
if (copy_to_user(user, &d, sizeof(d)) != 0)
2702-
ret = -EFAULT;
2703-
}
2704-
break;
2705-
27062721
default:
27072722
ret = -EINVAL;
27082723
}
@@ -3205,7 +3220,7 @@ static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
32053220
struct net *net = skb_sknet(skb);
32063221
struct netns_ipvs *ipvs = net_ipvs(net);
32073222

3208-
mutex_lock(&__ip_vs_mutex);
3223+
mutex_lock(&ipvs->sync_mutex);
32093224
if ((ipvs->sync_state & IP_VS_STATE_MASTER) && !cb->args[0]) {
32103225
if (ip_vs_genl_dump_daemon(skb, IP_VS_STATE_MASTER,
32113226
ipvs->master_mcast_ifn,
@@ -3225,7 +3240,7 @@ static int ip_vs_genl_dump_daemons(struct sk_buff *skb,
32253240
}
32263241

32273242
nla_put_failure:
3228-
mutex_unlock(&__ip_vs_mutex);
3243+
mutex_unlock(&ipvs->sync_mutex);
32293244

32303245
return skb->len;
32313246
}
@@ -3271,33 +3286,20 @@ static int ip_vs_genl_set_config(struct net *net, struct nlattr **attrs)
32713286
return ip_vs_set_timeout(net, &t);
32723287
}
32733288

3274-
static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3289+
static int ip_vs_genl_set_daemon(struct sk_buff *skb, struct genl_info *info)
32753290
{
3276-
struct ip_vs_service *svc = NULL;
3277-
struct ip_vs_service_user_kern usvc;
3278-
struct ip_vs_dest_user_kern udest;
32793291
int ret = 0, cmd;
3280-
int need_full_svc = 0, need_full_dest = 0;
32813292
struct net *net;
32823293
struct netns_ipvs *ipvs;
32833294

32843295
net = skb_sknet(skb);
32853296
ipvs = net_ipvs(net);
32863297
cmd = info->genlhdr->cmd;
32873298

3288-
mutex_lock(&__ip_vs_mutex);
3289-
3290-
if (cmd == IPVS_CMD_FLUSH) {
3291-
ret = ip_vs_flush(net);
3292-
goto out;
3293-
} else if (cmd == IPVS_CMD_SET_CONFIG) {
3294-
ret = ip_vs_genl_set_config(net, info->attrs);
3295-
goto out;
3296-
} else if (cmd == IPVS_CMD_NEW_DAEMON ||
3297-
cmd == IPVS_CMD_DEL_DAEMON) {
3298-
3299+
if (cmd == IPVS_CMD_NEW_DAEMON || cmd == IPVS_CMD_DEL_DAEMON) {
32993300
struct nlattr *daemon_attrs[IPVS_DAEMON_ATTR_MAX + 1];
33003301

3302+
mutex_lock(&ipvs->sync_mutex);
33013303
if (!info->attrs[IPVS_CMD_ATTR_DAEMON] ||
33023304
nla_parse_nested(daemon_attrs, IPVS_DAEMON_ATTR_MAX,
33033305
info->attrs[IPVS_CMD_ATTR_DAEMON],
@@ -3310,6 +3312,33 @@ static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
33103312
ret = ip_vs_genl_new_daemon(net, daemon_attrs);
33113313
else
33123314
ret = ip_vs_genl_del_daemon(net, daemon_attrs);
3315+
out:
3316+
mutex_unlock(&ipvs->sync_mutex);
3317+
}
3318+
return ret;
3319+
}
3320+
3321+
static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)
3322+
{
3323+
struct ip_vs_service *svc = NULL;
3324+
struct ip_vs_service_user_kern usvc;
3325+
struct ip_vs_dest_user_kern udest;
3326+
int ret = 0, cmd;
3327+
int need_full_svc = 0, need_full_dest = 0;
3328+
struct net *net;
3329+
struct netns_ipvs *ipvs;
3330+
3331+
net = skb_sknet(skb);
3332+
ipvs = net_ipvs(net);
3333+
cmd = info->genlhdr->cmd;
3334+
3335+
mutex_lock(&__ip_vs_mutex);
3336+
3337+
if (cmd == IPVS_CMD_FLUSH) {
3338+
ret = ip_vs_flush(net);
3339+
goto out;
3340+
} else if (cmd == IPVS_CMD_SET_CONFIG) {
3341+
ret = ip_vs_genl_set_config(net, info->attrs);
33133342
goto out;
33143343
} else if (cmd == IPVS_CMD_ZERO &&
33153344
!info->attrs[IPVS_CMD_ATTR_SERVICE]) {
@@ -3536,13 +3565,13 @@ static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
35363565
.cmd = IPVS_CMD_NEW_DAEMON,
35373566
.flags = GENL_ADMIN_PERM,
35383567
.policy = ip_vs_cmd_policy,
3539-
.doit = ip_vs_genl_set_cmd,
3568+
.doit = ip_vs_genl_set_daemon,
35403569
},
35413570
{
35423571
.cmd = IPVS_CMD_DEL_DAEMON,
35433572
.flags = GENL_ADMIN_PERM,
35443573
.policy = ip_vs_cmd_policy,
3545-
.doit = ip_vs_genl_set_cmd,
3574+
.doit = ip_vs_genl_set_daemon,
35463575
},
35473576
{
35483577
.cmd = IPVS_CMD_GET_DAEMON,

‎net/netfilter/ipvs/ip_vs_sync.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
#define SYNC_PROTO_VER 1 /* Protocol version in header */
6363

64+
static struct lock_class_key __ipvs_sync_key;
6465
/*
6566
* IPVS sync connection entry
6667
* Version 0, i.e. original version.
@@ -1545,6 +1546,7 @@ int start_sync_thread(struct net *net, int state, char *mcast_ifn, __u8 syncid)
15451546
IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
15461547
sizeof(struct ip_vs_sync_conn_v0));
15471548

1549+
15481550
if (state == IP_VS_STATE_MASTER) {
15491551
if (ipvs->master_thread)
15501552
return -EEXIST;
@@ -1667,6 +1669,7 @@ int __net_init ip_vs_sync_net_init(struct net *net)
16671669
{
16681670
struct netns_ipvs *ipvs = net_ipvs(net);
16691671

1672+
__mutex_init(&ipvs->sync_mutex, "ipvs->sync_mutex", &__ipvs_sync_key);
16701673
INIT_LIST_HEAD(&ipvs->sync_queue);
16711674
spin_lock_init(&ipvs->sync_lock);
16721675
spin_lock_init(&ipvs->sync_buff_lock);
@@ -1680,12 +1683,15 @@ int __net_init ip_vs_sync_net_init(struct net *net)
16801683
void ip_vs_sync_net_cleanup(struct net *net)
16811684
{
16821685
int retc;
1686+
struct netns_ipvs *ipvs = net_ipvs(net);
16831687

1688+
mutex_lock(&ipvs->sync_mutex);
16841689
retc = stop_sync_thread(net, IP_VS_STATE_MASTER);
16851690
if (retc && retc != -ESRCH)
16861691
pr_err("Failed to stop Master Daemon\n");
16871692

16881693
retc = stop_sync_thread(net, IP_VS_STATE_BACKUP);
16891694
if (retc && retc != -ESRCH)
16901695
pr_err("Failed to stop Backup Daemon\n");
1696+
mutex_unlock(&ipvs->sync_mutex);
16911697
}

0 commit comments

Comments
 (0)