Skip to content

Commit f8ccac0

Browse files
tomparkindavem330
authored andcommitted
l2tp: put tunnel socket release on a workqueue
To allow l2tp_tunnel_delete to be called from an atomic context, place the tunnel socket release calls on a workqueue for asynchronous execution. Tunnel memory is eventually freed in the tunnel socket destructor. Signed-off-by: Tom Parkin <[email protected]> Signed-off-by: James Chapman <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 188d1f7 commit f8ccac0

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

net/l2tp/l2tp_core.c

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct l2tp_skb_cb {
101101

102102
static atomic_t l2tp_tunnel_count;
103103
static atomic_t l2tp_session_count;
104+
static struct workqueue_struct *l2tp_wq;
104105

105106
/* per-net private data for this module */
106107
static unsigned int l2tp_net_id;
@@ -122,7 +123,6 @@ static inline struct l2tp_net *l2tp_pernet(struct net *net)
122123
return net_generic(net, l2tp_net_id);
123124
}
124125

125-
126126
/* Tunnel reference counts. Incremented per session that is added to
127127
* the tunnel.
128128
*/
@@ -1277,16 +1277,16 @@ EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
12771277
static void l2tp_tunnel_destruct(struct sock *sk)
12781278
{
12791279
struct l2tp_tunnel *tunnel;
1280+
struct l2tp_net *pn;
12801281

12811282
tunnel = sk->sk_user_data;
12821283
if (tunnel == NULL)
12831284
goto end;
12841285

12851286
l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
12861287

1287-
/* Close all sessions */
1288-
l2tp_tunnel_closeall(tunnel);
12891288

1289+
/* Disable udp encapsulation */
12901290
switch (tunnel->encap) {
12911291
case L2TP_ENCAPTYPE_UDP:
12921292
/* No longer an encapsulation socket. See net/ipv4/udp.c */
@@ -1298,17 +1298,23 @@ static void l2tp_tunnel_destruct(struct sock *sk)
12981298
}
12991299

13001300
/* Remove hooks into tunnel socket */
1301-
tunnel->sock = NULL;
13021301
sk->sk_destruct = tunnel->old_sk_destruct;
13031302
sk->sk_user_data = NULL;
1303+
tunnel->sock = NULL;
13041304

1305-
/* Call the original destructor */
1306-
if (sk->sk_destruct)
1307-
(*sk->sk_destruct)(sk);
1305+
/* Remove the tunnel struct from the tunnel list */
1306+
pn = l2tp_pernet(tunnel->l2tp_net);
1307+
spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1308+
list_del_rcu(&tunnel->list);
1309+
spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1310+
atomic_dec(&l2tp_tunnel_count);
13081311

1309-
/* We're finished with the socket */
1312+
l2tp_tunnel_closeall(tunnel);
13101313
l2tp_tunnel_dec_refcount(tunnel);
13111314

1315+
/* Call the original destructor */
1316+
if (sk->sk_destruct)
1317+
(*sk->sk_destruct)(sk);
13121318
end:
13131319
return;
13141320
}
@@ -1382,20 +1388,41 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
13821388
*/
13831389
static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
13841390
{
1385-
struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
1386-
13871391
BUG_ON(atomic_read(&tunnel->ref_count) != 0);
13881392
BUG_ON(tunnel->sock != NULL);
1389-
13901393
l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
1391-
1392-
/* Remove from tunnel list */
1393-
spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1394-
list_del_rcu(&tunnel->list);
13951394
kfree_rcu(tunnel, rcu);
1396-
spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1395+
}
13971396

1398-
atomic_dec(&l2tp_tunnel_count);
1397+
/* Workqueue tunnel deletion function */
1398+
static void l2tp_tunnel_del_work(struct work_struct *work)
1399+
{
1400+
struct l2tp_tunnel *tunnel = NULL;
1401+
struct socket *sock = NULL;
1402+
struct sock *sk = NULL;
1403+
1404+
tunnel = container_of(work, struct l2tp_tunnel, del_work);
1405+
sk = l2tp_tunnel_sock_lookup(tunnel);
1406+
if (!sk)
1407+
return;
1408+
1409+
sock = sk->sk_socket;
1410+
BUG_ON(!sock);
1411+
1412+
/* Force the tunnel socket to close. This will eventually
1413+
* cause the tunnel to be deleted via the normal socket close
1414+
* mechanisms when userspace closes the tunnel socket.
1415+
*/
1416+
inet_shutdown(sock, 2);
1417+
1418+
/* If the tunnel's socket was created by the kernel,
1419+
* close the socket here since the socket was not
1420+
* created by userspace.
1421+
*/
1422+
if (sock->file == NULL)
1423+
inet_release(sock);
1424+
1425+
l2tp_tunnel_sock_put(sk);
13991426
}
14001427

14011428
/* Create a socket for the tunnel, if one isn't set up by
@@ -1657,6 +1684,9 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
16571684

16581685
sk->sk_allocation = GFP_ATOMIC;
16591686

1687+
/* Init delete workqueue struct */
1688+
INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
1689+
16601690
/* Add tunnel to our list */
16611691
INIT_LIST_HEAD(&tunnel->list);
16621692
atomic_inc(&l2tp_tunnel_count);
@@ -1688,33 +1718,7 @@ EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
16881718
*/
16891719
int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
16901720
{
1691-
int err = -EBADF;
1692-
struct socket *sock = NULL;
1693-
struct sock *sk = NULL;
1694-
1695-
sk = l2tp_tunnel_sock_lookup(tunnel);
1696-
if (!sk)
1697-
goto out;
1698-
1699-
sock = sk->sk_socket;
1700-
BUG_ON(!sock);
1701-
1702-
/* Force the tunnel socket to close. This will eventually
1703-
* cause the tunnel to be deleted via the normal socket close
1704-
* mechanisms when userspace closes the tunnel socket.
1705-
*/
1706-
err = inet_shutdown(sock, 2);
1707-
1708-
/* If the tunnel's socket was created by the kernel,
1709-
* close the socket here since the socket was not
1710-
* created by userspace.
1711-
*/
1712-
if (sock->file == NULL)
1713-
err = inet_release(sock);
1714-
1715-
l2tp_tunnel_sock_put(sk);
1716-
out:
1717-
return err;
1721+
return (false == queue_work(l2tp_wq, &tunnel->del_work));
17181722
}
17191723
EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
17201724

@@ -1912,6 +1916,13 @@ static int __init l2tp_init(void)
19121916
if (rc)
19131917
goto out;
19141918

1919+
l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
1920+
if (!l2tp_wq) {
1921+
pr_err("alloc_workqueue failed\n");
1922+
rc = -ENOMEM;
1923+
goto out;
1924+
}
1925+
19151926
pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
19161927

19171928
out:
@@ -1921,6 +1932,10 @@ static int __init l2tp_init(void)
19211932
static void __exit l2tp_exit(void)
19221933
{
19231934
unregister_pernet_device(&l2tp_net_ops);
1935+
if (l2tp_wq) {
1936+
destroy_workqueue(l2tp_wq);
1937+
l2tp_wq = NULL;
1938+
}
19241939
}
19251940

19261941
module_init(l2tp_init);

net/l2tp/l2tp_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ struct l2tp_tunnel {
191191
int fd; /* Parent fd, if tunnel socket
192192
* was created by userspace */
193193

194+
struct work_struct del_work;
195+
194196
uint8_t priv[0]; /* private data */
195197
};
196198

0 commit comments

Comments
 (0)