Skip to contentSkip to conversation

tuntap: correctly wake up process during uninit

PATCH Superseded Jason Wang 3 participants 4 messages May 18, 2016 – May 19, 2016

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 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 9e641bdcfa4e ("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 9e641bdcfa4e ("net-tun: restructure tun_do_read for better

sleep/wakeup efficiency")

Cc Eric Dumazet <[email protected]>
Cc Xi Wang <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

---
The patch is needed for -stable.

---
 drivers/net/tun.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/tun.c +3 -0
index 425e983..752d849 100644
@@ -580,11 +580,13 @@ static void tun_detach_all(struct net_device *dev)
 	for (i = 0; i < n; i++) {
 		tfile = rtnl_dereference(tun->tfiles[i]);
 		BUG_ON(!tfile);
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
 		RCU_INIT_POINTER(tfile->tun, NULL);
 		--tun->numqueues;
 	}
 	list_for_each_entry(tfile, &tun->disabled, next) {
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
 		RCU_INIT_POINTER(tfile->tun, NULL);
 	}
@@ -641,6 +643,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte
 			goto out;
 	}
 	tfile->queue_index = tun->numqueues;
+	tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
 	rcu_assign_pointer(tfile->tun, tun);
 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
 	tun->numqueues++;
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jason Wang

We used to check dev->reg_state against NETREG_REGISTERED after each time we are woke up. But after commit 9e641bdcfa4e ("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.

Quoted 7 lines
Fixes 9e641bdcfa4e ("net-tun: restructure tun_do_read for better

sleep/wakeup efficiency")

Cc Eric Dumazet <[email protected]>
Cc Xi Wang <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
Quoted 25 lines

---
The patch is needed for -stable.

---
 drivers/net/tun.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/tun.c +2 -0
index 425e983..752d849 100644
@@ -580,11 +580,13 @@ static void tun_detach_all(struct net_device *dev)
 	for (i = 0; i < n; i++) {
 		tfile = rtnl_dereference(tun->tfiles[i]);
 		BUG_ON(!tfile);
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
 		RCU_INIT_POINTER(tfile->tun, NULL);
 		--tun->numqueues;
 	}
 	list_for_each_entry(tfile, &tun->disabled, next) {
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
 		RCU_INIT_POINTER(tfile->tun, NULL);
 	}

drivers/net/tun.c static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte · line 649

Quoted 7 lines
drivers/net/tun.c +1 -0
@@ -641,6 +643,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte
 			goto out;
 	}
 	tfile->queue_index = tun->numqueues;
+	tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
 	rcu_assign_pointer(tfile->tun, tun);
 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
 	tun->numqueues++;

By the way I wonder: at the moment interface goes down each time userspace disconnects, even if it was persistent and brought up manually (as opposed to on file open). Should we maybe track manual link up status and keep persistent device up on userspace disconnect?

Quoted 2 lines
Signature · 1 line
2.7.4
#2 lore raw
In reply to Jason Wang
Quoted 8 lines · Jason Wang

We used to check dev->reg_state against NETREG_REGISTERED after each time we are woke up. But after commit 9e641bdcfa4e ("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.

Quoted 5 lines

Fixes this by using RCV_SHUTDOWN which will be checked during sk_recv_datagram() before trying to wake up the process during uninit.

Fixes 9e641bdcfa4e ("net-tun: restructure tun_do_read for better

sleep/wakeup efficiency")

<nit : no newline before Fixes: and other parts>

Quoted 30 lines
Cc Eric Dumazet <[email protected]>
Cc Xi Wang <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

---
The patch is needed for -stable.

---
 drivers/net/tun.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/tun.c +2 -0
index 425e983..752d849 100644
@@ -580,11 +580,13 @@ static void tun_detach_all(struct net_device *dev)
 	for (i = 0; i < n; i++) {
 		tfile = rtnl_dereference(tun->tfiles[i]);
 		BUG_ON(!tfile);
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
 		RCU_INIT_POINTER(tfile->tun, NULL);
 		--tun->numqueues;
 	}
 	list_for_each_entry(tfile, &tun->disabled, next) {
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
 		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
 		RCU_INIT_POINTER(tfile->tun, NULL);
 	}

drivers/net/tun.c static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte · line 649

Quoted 7 lines
drivers/net/tun.c +1 -0
@@ -641,6 +643,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte
 			goto out;
 	}
 	tfile->queue_index = tun->numqueues;
+	tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
 	rcu_assign_pointer(tfile->tun, tun);
 	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
 	tun->numqueues++;

Is the "if (tun->dev->reg_state != NETREG_REGISTERED) return -EIO;" check still needed then ?

Thanks.

author
#3 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Jason Wang

We used to check dev->reg_state against NETREG_REGISTERED after each time we are woke up. But after commit 9e641bdcfa4e ("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.

Quoted 6 lines
Quoted 5 lines

Fixes this by using RCV_SHUTDOWN which will be checked during sk_recv_datagram() before trying to wake up the process during uninit.

Fixes 9e641bdcfa4e ("net-tun: restructure tun_do_read for better

sleep/wakeup efficiency")

<nit : no newline before Fixes: and other parts>

Ok.

Quoted 39 lines
Cc Eric Dumazet <[email protected]>
Cc Xi Wang <[email protected]>
Cc Michael S. Tsirkin <[email protected]>

---
The patch is needed for -stable.

---
  drivers/net/tun.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/net/tun.c +3 -0
index 425e983..752d849 100644
@@ -580,11 +580,13 @@ static void tun_detach_all(struct net_device *dev)
  	for (i = 0; i < n; i++) {
  		tfile = rtnl_dereference(tun->tfiles[i]);
  		BUG_ON(!tfile);
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  		RCU_INIT_POINTER(tfile->tun, NULL);
  		--tun->numqueues;
  	}
  	list_for_each_entry(tfile, &tun->disabled, next) {
+		tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  		tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  		RCU_INIT_POINTER(tfile->tun, NULL);
  	}
@@ -641,6 +643,7 @@ static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte
  			goto out;
  	}
  	tfile->queue_index = tun->numqueues;
+	tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
  	rcu_assign_pointer(tfile->tun, tun);
  	rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
  	tun->numqueues++;

Is the "if (tun->dev->reg_state != NETREG_REGISTERED) return -EIO;" check still needed then ?

Quoted 3 lines

Thanks.

No need since we've check tun before, will remove this in V2.

Thanks