Skip to content

Commit addf8fc

Browse files
jasowangdavem330
authored andcommitted
tuntap: correctly wake up process during uninit
We used to check dev->reg_state against NETREG_REGISTERED after each time we are woke up. But after commit 9e641bd ("net-tun: restructure tun_do_read for better sleep/wakeup efficiency"), it uses skb_recv_datagram() which does not check dev->reg_state. This will result if we delete a tun/tap device after a process is blocked in the reading. The device will wait for the reference count which was held by that process for ever. Fixes this by using RCV_SHUTDOWN which will be checked during sk_recv_datagram() before trying to wake up the process during uninit. Fixes: 9e641bd ("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") Cc: Eric Dumazet <edumazet@google.com> Cc: Xi Wang <xii@google.com> Cc: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Jason Wang <jasowang@redhat.com> Acked-by: Eric Dumazet <edumazet@google.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7fd3c56 commit addf8fc

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/net/tun.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,13 @@ static void tun_detach_all(struct net_device *dev)
580580
for (i = 0; i < n; i++) {
581581
tfile = rtnl_dereference(tun->tfiles[i]);
582582
BUG_ON(!tfile);
583+
tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
583584
tfile->socket.sk->sk_data_ready(tfile->socket.sk);
584585
RCU_INIT_POINTER(tfile->tun, NULL);
585586
--tun->numqueues;
586587
}
587588
list_for_each_entry(tfile, &tun->disabled, next) {
589+
tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
588590
tfile->socket.sk->sk_data_ready(tfile->socket.sk);
589591
RCU_INIT_POINTER(tfile->tun, NULL);
590592
}
@@ -641,6 +643,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte
641643
goto out;
642644
}
643645
tfile->queue_index = tun->numqueues;
646+
tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
644647
rcu_assign_pointer(tfile->tun, tun);
645648
rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
646649
tun->numqueues++;
@@ -1491,9 +1494,6 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
14911494
if (!iov_iter_count(to))
14921495
return 0;
14931496

1494-
if (tun->dev->reg_state != NETREG_REGISTERED)
1495-
return -EIO;
1496-
14971497
/* Read frames from queue */
14981498
skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
14991499
&peeked, &off, &err);

0 commit comments

Comments
 (0)