tun: Utilize the normal socket network namespace refcounting.
authorEric W. Biederman <ebiederm@xmission.com>
Sat, 9 May 2015 02:07:08 +0000 (21:07 -0500)
committerDavid S. Miller <davem@davemloft.net>
Mon, 11 May 2015 14:50:16 +0000 (10:50 -0400)
There is no need for tun to do the weird network namespace refcounting.
The existing network namespace refcounting in tfile has almost exactly
the same lifetime.  So rewrite the code to use the struct sock network
namespace refcounting and remove the unnecessary hand rolled network
namespace refcounting and the unncesary tfile->net.

This change allows the tun code to directly call sock_put bypassing
sock_release and making SOCK_EXTERNALLY_ALLOCATED unnecessary.

Remove the now unncessary tun_release so that if anything tries to use
the sock_release code path the kernel will oops, and let us know about
the bug.

The macvtap code already uses it's internal socket this way.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/tun.c
include/linux/net.h
net/socket.c

index e470ae59d40536fe7530774cb473ebe57000f9a8..3262f3e2b8b2c2a4acc1b107d1af007048f3c51f 100644 (file)
@@ -146,7 +146,6 @@ struct tun_file {
        struct socket socket;
        struct socket_wq wq;
        struct tun_struct __rcu *tun;
-       struct net *net;
        struct fasync_struct *fasync;
        /* only used for fasnyc */
        unsigned int flags;
@@ -493,10 +492,7 @@ static void __tun_detach(struct tun_file *tfile, bool clean)
                            tun->dev->reg_state == NETREG_REGISTERED)
                                unregister_netdevice(tun->dev);
                }
-
-               BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
-                                &tfile->socket.flags));
-               sk_release_kernel(&tfile->sk);
+               sock_put(&tfile->sk);
        }
 }
 
@@ -1492,18 +1488,10 @@ out:
        return ret;
 }
 
-static int tun_release(struct socket *sock)
-{
-       if (sock->sk)
-               sock_put(sock->sk);
-       return 0;
-}
-
 /* Ops structure to mimic raw sockets with tun */
 static const struct proto_ops tun_socket_ops = {
        .sendmsg = tun_sendmsg,
        .recvmsg = tun_recvmsg,
-       .release = tun_release,
 };
 
 static struct proto tun_proto = {
@@ -1865,7 +1853,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
        if (cmd == TUNSETIFF && !tun) {
                ifr.ifr_name[IFNAMSIZ-1] = '\0';
 
-               ret = tun_set_iff(tfile->net, file, &ifr);
+               ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr);
 
                if (ret)
                        goto unlock;
@@ -2154,16 +2142,16 @@ out:
 
 static int tun_chr_open(struct inode *inode, struct file * file)
 {
+       struct net *net = current->nsproxy->net_ns;
        struct tun_file *tfile;
 
        DBG1(KERN_INFO, "tunX: tun_chr_open\n");
 
-       tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
+       tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
                                            &tun_proto);
        if (!tfile)
                return -ENOMEM;
        RCU_INIT_POINTER(tfile->tun, NULL);
-       tfile->net = get_net(current->nsproxy->net_ns);
        tfile->flags = 0;
        tfile->ifindex = 0;
 
@@ -2174,13 +2162,11 @@ static int tun_chr_open(struct inode *inode, struct file * file)
        tfile->socket.ops = &tun_socket_ops;
 
        sock_init_data(&tfile->socket, &tfile->sk);
-       sk_change_net(&tfile->sk, tfile->net);
 
        tfile->sk.sk_write_space = tun_sock_write_space;
        tfile->sk.sk_sndbuf = INT_MAX;
 
        file->private_data = tfile;
-       set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
        INIT_LIST_HEAD(&tfile->next);
 
        sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
@@ -2191,10 +2177,8 @@ static int tun_chr_open(struct inode *inode, struct file * file)
 static int tun_chr_close(struct inode *inode, struct file *file)
 {
        struct tun_file *tfile = file->private_data;
-       struct net *net = tfile->net;
 
        tun_detach(tfile, true);
-       put_net(net);
 
        return 0;
 }
index 738ea48be889e670275616bae5063259ca3d61d9..8a5e81d2bdf7e53be3f3b7b8035dc98bd0ad8b4b 100644 (file)
@@ -38,7 +38,6 @@ struct net;
 #define SOCK_NOSPACE           2
 #define SOCK_PASSCRED          3
 #define SOCK_PASSSEC           4
-#define SOCK_EXTERNALLY_ALLOCATED 5
 
 #ifndef ARCH_HAS_SOCKET_TYPES
 /**
index 884e3299769840c3e05c43f1320ba849a10ad73c..b5f1f43ed8f486846ad66d78d1b3211579405b1a 100644 (file)
@@ -576,9 +576,6 @@ void sock_release(struct socket *sock)
        if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
                pr_err("%s: fasync list not empty!\n", __func__);
 
-       if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
-               return;
-
        this_cpu_sub(sockets_in_use, 1);
        if (!sock->file) {
                iput(SOCK_INODE(sock));
This page took 0.044809 seconds and 5 git commands to generate.