Skip to contentSkip to conversation

tuntap: correctly set SOCKWQ_ASYNC_NOSPACE

PATCH In review Tommi Rantala 2 participants 2 messages Feb 28, 2020 – Mar 3, 2020 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tuntap: correctly set SOCKWQ_ASYNC_NOSPACE

From: Jason Wang <[email protected]>

[ Upstream commit 2f3ab6221e4c87960347d65c7cab9bd917d1f637 ]

When link is down, writes to the device might fail with -EIO. Userspace needs an indication when the status is resolved. As a fix, tun_net_open() attempts to wake up writers - but that is only effective if SOCKWQ_ASYNC_NOSPACE has been set in the past. This is not the case of vhost_net which only poll for EPOLLOUT after it meets errors during sendmsg().

This patch fixes this by making sure SOCKWQ_ASYNC_NOSPACE is set when socket is not writable or device is down to guarantee EPOLLOUT will be raised in either tun_chr_poll() or tun_sock_write_space() after device is up.

Cc Hannes Frederic Sowa <[email protected]>
Cc Eric Dumazet <[email protected]>
Fixes 1bd4978a88ac2 ("tun: honor IFF_UP in tun_get_user()")
---
 drivers/net/tun.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/net/tun.c +15 -4
index 3086211829a7..ba34f61d70de 100644
@@ -1134,6 +1134,13 @@ static void tun_net_init(struct net_device *dev)
 	dev->max_mtu = MAX_MTU - dev->hard_header_len;
 }
 
+static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
+{
+	struct sock *sk = tfile->socket.sk;
+
+	return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
+}
+
 /* Character device part */
 
 /* Poll */
@@ -1156,10 +1163,14 @@ static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
 	if (!skb_array_empty(&tfile->tx_array))
 		mask |= POLLIN | POLLRDNORM;
 
-	if (tun->dev->flags & IFF_UP &&
-	    (sock_writeable(sk) ||
-	     (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
-	      sock_writeable(sk))))
+	/* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
+	 * guarantee EPOLLOUT to be raised by either here or
+	 * tun_sock_write_space(). Then process could get notification
+	 * after it writes to a down device and meets -EIO.
+	 */
+	if (tun_sock_writeable(tun, tfile) ||
+	    (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
+	     tun_sock_writeable(tun, tfile)))
 		mask |= POLLOUT | POLLWRNORM;
 
 	if (tun->dev->reg_state != NETREG_REGISTERED)
Signature · 1 line
2.21.1
maintainer
#1 lore raw
Quoted 22 lines · Tommi Rantala

From: Jason Wang <[email protected]>

[ Upstream commit 2f3ab6221e4c87960347d65c7cab9bd917d1f637 ]

When link is down, writes to the device might fail with -EIO. Userspace needs an indication when the status is resolved. As a fix, tun_net_open() attempts to wake up writers - but that is only effective if SOCKWQ_ASYNC_NOSPACE has been set in the past. This is not the case of vhost_net which only poll for EPOLLOUT after it meets errors during sendmsg().

This patch fixes this by making sure SOCKWQ_ASYNC_NOSPACE is set when socket is not writable or device is down to guarantee EPOLLOUT will be raised in either tun_chr_poll() or tun_sock_write_space() after device is up.

Cc Hannes Frederic Sowa <[email protected]>
Cc Eric Dumazet <[email protected]>
Fixes 1bd4978a88ac2 ("tun: honor IFF_UP in tun_get_user()")
Quoted 3 lines
---
 drivers/net/tun.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

Thanks for the backport, now queued up to 4.9.y and 4.14.y.

greg k-h