Skip to contentSkip to conversation

vhost: fix a few skb leaks

PATCH New [email protected] 3 participants 17 messages Dec 1, 2017 – Dec 4, 2017

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 vhost: fix a few skb leaks

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

This is v3.

v3:
- move freeing skb from vhost to tun/tap recvmsg() to not confuse the callers.

v2:
- add Matthew as the reporter, thanks matthew.
- moving zero headcount check ahead instead of defer consuming skb due to jason and mst's comment.
- add freeing skb in favor of recvmsg() fails.

Wei Xu (3):
vhost: fix skb leak in handle_rx()
tun: free skb in early errors
tap: free skb if flags error

 drivers/net/tap.c   |  6 +++++-
 drivers/net/tun.c   | 14 +++++++++++---
 drivers/vhost/net.c | 20 ++++++++++----------
 3 files changed, 26 insertions(+), 14 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 vhost: fix skb leak in handle_rx()

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

Eventually we figured out that it was a skb leak in handle_rx() when sending packets to the VM. This usually happens when a guest can not drain out vq as fast as vhost fills in, afterwards it sets off the traffic jam and leaks skb(s) which occurs as no headcount to send on the vq from vhost side.

This can be avoided by making sure we have got enough headcount before actually consuming a skb from the batched rx array while transmitting, which is simply done by moving checking the zero headcount a bit ahead.

---
 drivers/vhost/net.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vhost/net.c +10 -10
index 8d626d7..c7bdeb6 100644
@@ -778,16 +778,6 @@ static void handle_rx(struct vhost_net *net)
 		/* On error, stop handling until the next kick. */
 		if (unlikely(headcount < 0))
 			goto out;
-		if (nvq->rx_array)
-			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
-		/* On overrun, truncate and discard */
-		if (unlikely(headcount > UIO_MAXIOV)) {
-			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
-			err = sock->ops->recvmsg(sock, &msg,
-						 1, MSG_DONTWAIT | MSG_TRUNC);
-			pr_debug("Discarded rx packet: len %zd\n", sock_len);
-			continue;
-		}
 		/* OK, now we need to know about added descriptors. */
 		if (!headcount) {
 			if (unlikely(vhost_enable_notify(&net->dev, vq))) {
@@ -800,6 +790,16 @@ static void handle_rx(struct vhost_net *net)
 			 * they refilled. */
 			goto out;
 		}
+		if (nvq->rx_array)
+			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
+		/* On overrun, truncate and discard */
+		if (unlikely(headcount > UIO_MAXIOV)) {
+			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
+			err = sock->ops->recvmsg(sock, &msg,
+						 1, MSG_DONTWAIT | MSG_TRUNC);
+			pr_debug("Discarded rx packet: len %zd\n", sock_len);
+			continue;
+		}
 		/* We don't need to be notified again. */
 		iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
 		fixup = msg.msg_iter;
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH 1/3 vhost: fix skb leak in handle_rx()

In reply to [email protected]

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

Eventually we figured out that it was a skb leak in handle_rx() when sending packets to the VM. This usually happens when a guest can not drain out vq as fast as vhost fills in, afterwards it sets off the traffic jam and leaks skb(s) which occurs as no headcount to send on the vq from vhost side.

This can be avoided by making sure we have got enough headcount before actually consuming a skb from the batched rx array while transmitting, which is simply done by moving checking the zero headcount a bit ahead.

---
 drivers/vhost/net.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vhost/net.c +10 -10
index 8d626d7..c7bdeb6 100644
@@ -778,16 +778,6 @@ static void handle_rx(struct vhost_net *net)
 		/* On error, stop handling until the next kick. */
 		if (unlikely(headcount < 0))
 			goto out;
-		if (nvq->rx_array)
-			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
-		/* On overrun, truncate and discard */
-		if (unlikely(headcount > UIO_MAXIOV)) {
-			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
-			err = sock->ops->recvmsg(sock, &msg,
-						 1, MSG_DONTWAIT | MSG_TRUNC);
-			pr_debug("Discarded rx packet: len %zd\n", sock_len);
-			continue;
-		}
 		/* OK, now we need to know about added descriptors. */
 		if (!headcount) {
 			if (unlikely(vhost_enable_notify(&net->dev, vq))) {
@@ -800,6 +790,16 @@ static void handle_rx(struct vhost_net *net)
 			 * they refilled. */
 			goto out;
 		}
+		if (nvq->rx_array)
+			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
+		/* On overrun, truncate and discard */
+		if (unlikely(headcount > UIO_MAXIOV)) {
+			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
+			err = sock->ops->recvmsg(sock, &msg,
+						 1, MSG_DONTWAIT | MSG_TRUNC);
+			pr_debug("Discarded rx packet: len %zd\n", sock_len);
+			continue;
+		}
 		/* We don't need to be notified again. */
 		iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
 		fixup = msg.msg_iter;
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH 2/3 tun: free skb in early errors

In reply to [email protected]

From: Wei Xu <[email protected]>

tun_recvmsg() supports accepting skb by msg_control after commit ac77cfd4258f ("tun: support receiving skb through msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
 drivers/net/tun.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/tun.c +11 -3
index 6a7bde9..5563430 100644
@@ -2067,14 +2067,17 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
 {
 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
 	struct tun_struct *tun = tun_get(tfile);
+	struct sk_buff *skb = m->msg_control;
 	int ret;
 
-	if (!tun)
-		return -EBADFD;
+	if (!tun) {
+		ret = -EBADFD;
+		goto out_free_skb;
+	}
 
 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
 		ret = -EINVAL;
-		goto out;
+		goto out_free_skb;
 	}
 	if (flags & MSG_ERRQUEUE) {
 		ret = sock_recv_errqueue(sock->sk, m, total_len,
@@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
 		m->msg_flags |= MSG_TRUNC;
 		ret = flags & MSG_TRUNC ? ret : total_len;
 	}
+	goto out;
+
+out_free_skb:
+	if (skb)
+		kfree_skb(skb);
 out:
 	tun_put(tun);
 	return ret;
Signature · 1 line
1.8.3.1
#12 lore raw

PATCH 2/3 tun: free skb in early errors

In reply to [email protected]

From: Wei Xu <[email protected]>

tun_recvmsg() supports accepting skb by msg_control after commit ac77cfd4258f ("tun: support receiving skb through msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
 drivers/net/tun.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/tun.c +11 -3
index 6a7bde9..5563430 100644
@@ -2067,14 +2067,17 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
 {
 	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
 	struct tun_struct *tun = tun_get(tfile);
+	struct sk_buff *skb = m->msg_control;
 	int ret;
 
-	if (!tun)
-		return -EBADFD;
+	if (!tun) {
+		ret = -EBADFD;
+		goto out_free_skb;
+	}
 
 	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
 		ret = -EINVAL;
-		goto out;
+		goto out_free_skb;
 	}
 	if (flags & MSG_ERRQUEUE) {
 		ret = sock_recv_errqueue(sock->sk, m, total_len,
@@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
 		m->msg_flags |= MSG_TRUNC;
 		ret = flags & MSG_TRUNC ? ret : total_len;
 	}
+	goto out;
+
+out_free_skb:
+	if (skb)
+		kfree_skb(skb);
 out:
 	tun_put(tun);
 	return ret;
Signature · 1 line
1.8.3.1
#13 lore raw

PATCH 3/3 tap: free skb if flags error

In reply to [email protected]

From: Wei Xu <[email protected]>

tap_recvmsg() supports accepting skb by msg_control after commit 3b4ba04acca8 ("tap: support receiving skb from msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
 drivers/net/tap.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/tap.c +5 -1
index e9489b8..1c66b75 100644
@@ -1154,9 +1154,13 @@ static int tap_recvmsg(struct socket *sock, struct msghdr *m,
 		       size_t total_len, int flags)
 {
 	struct tap_queue *q = container_of(sock, struct tap_queue, sock);
+	struct sk_buff *skb = m->msg_control;
 	int ret;
-	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
+	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
+		if (skb)
+			kfree_skb(skb);
 		return -EINVAL;
+	}
 	ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
 			  m->msg_control);
 	if (ret > total_len) {
Signature · 1 line
1.8.3.1
#16 lore raw

PATCH 3/3 tap: free skb if flags error

In reply to [email protected]

From: Wei Xu <[email protected]>

tap_recvmsg() supports accepting skb by msg_control after commit 3b4ba04acca8 ("tap: support receiving skb from msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
 drivers/net/tap.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/tap.c +5 -1
index e9489b8..1c66b75 100644
@@ -1154,9 +1154,13 @@ static int tap_recvmsg(struct socket *sock, struct msghdr *m,
 		       size_t total_len, int flags)
 {
 	struct tap_queue *q = container_of(sock, struct tap_queue, sock);
+	struct sk_buff *skb = m->msg_control;
 	int ret;
-	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
+	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
+		if (skb)
+			kfree_skb(skb);
 		return -EINVAL;
+	}
 	ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
 			  m->msg_control);
 	if (ret > total_len) {
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [PATCH 2/3] tun: free skb in early errors

In reply to [email protected]
Quoted 19 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

tun_recvmsg() supports accepting skb by msg_control after commit ac77cfd4258f ("tun: support receiving skb through msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
  drivers/net/tun.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/tun.c +0 -0
index 6a7bde9..5563430 100644
@@ -2067,14 +2067,17 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  {

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2075

Quoted 10 lines
drivers/net/tun.c +4 -2
  	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  	struct tun_struct *tun = tun_get(tfile);
+	struct sk_buff *skb = m->msg_control;
  	int ret;
 
-	if (!tun)
-		return -EBADFD;
+	if (!tun) {
+		ret = -EBADFD;
+		goto out_free_skb;

Unfortunately, you can't to there since tun is NULL.

Quoted 10 lines
drivers/net/tun.c +2 -1
+	}
 
  	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  		ret = -EINVAL;
-		goto out;
+		goto out_free_skb;
  	}
  	if (flags & MSG_ERRQUEUE) {
  		ret = sock_recv_errqueue(sock->sk, m, total_len,

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2093

Quoted 4 lines
drivers/net/tun.c +1 -0
@@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  		m->msg_flags |= MSG_TRUNC;
  		ret = flags & MSG_TRUNC ? ret : total_len;
  	}
+	goto out;

We usually don't use goto in the case of success, and you need deal with the case skb != NULL but iov_iter_count(to) == 0 in tun_do_read().

Thanks

Quoted 7 lines
drivers/net/tun.c +4 -0
+
+out_free_skb:
+	if (skb)
+		kfree_skb(skb);
  out:
  	tun_put(tun);
  	return ret;
#11 lore raw

Re: [PATCH 2/3] tun: free skb in early errors

In reply to [email protected]
Quoted 19 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

tun_recvmsg() supports accepting skb by msg_control after commit ac77cfd4258f ("tun: support receiving skb through msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
  drivers/net/tun.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/tun.c +0 -0
index 6a7bde9..5563430 100644
@@ -2067,14 +2067,17 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  {

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2075

Quoted 10 lines
drivers/net/tun.c +4 -2
  	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  	struct tun_struct *tun = tun_get(tfile);
+	struct sk_buff *skb = m->msg_control;
  	int ret;
 
-	if (!tun)
-		return -EBADFD;
+	if (!tun) {
+		ret = -EBADFD;
+		goto out_free_skb;

Unfortunately, you can't to there since tun is NULL.

Quoted 10 lines
drivers/net/tun.c +2 -1
+	}
 
  	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  		ret = -EINVAL;
-		goto out;
+		goto out_free_skb;
  	}
  	if (flags & MSG_ERRQUEUE) {
  		ret = sock_recv_errqueue(sock->sk, m, total_len,

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2093

Quoted 4 lines
drivers/net/tun.c +1 -0
@@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  		m->msg_flags |= MSG_TRUNC;
  		ret = flags & MSG_TRUNC ? ret : total_len;
  	}
+	goto out;

We usually don't use goto in the case of success, and you need deal with the case skb != NULL but iov_iter_count(to) == 0 in tun_do_read().

Thanks

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2100

Quoted 7 lines
drivers/net/tun.c +4 -0
+
+out_free_skb:
+	if (skb)
+		kfree_skb(skb);
  out:
  	tun_put(tun);
  	return ret;

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#14 lore raw

Re: [PATCH 3/3] tap: free skb if flags error

In reply to [email protected]
Quoted 21 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

tap_recvmsg() supports accepting skb by msg_control after commit 3b4ba04acca8 ("tap: support receiving skb from msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
  drivers/net/tap.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/tap.c +0 -0
index e9489b8..1c66b75 100644
@@ -1154,9 +1154,13 @@ static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  		       size_t total_len, int flags)
  {
  	struct tap_queue *q = container_of(sock, struct tap_queue, sock);

drivers/net/tap.c static int tap_recvmsg(struct socket *sock, struct msghdr *m, · line 1165

Quoted 10 lines
drivers/net/tap.c +5 -1
+	struct sk_buff *skb = m->msg_control;
  	int ret;
-	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
+	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
+		if (skb)
+			kfree_skb(skb);
  		return -EINVAL;
+	}
  	ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
  			  m->msg_control);

Need to deal with iov_iterator_count() == 0.

Thanks

Quoted 1 line
drivers/net/tap.c +0 -0
  	if (ret > total_len) {
#15 lore raw

Re: [PATCH 3/3] tap: free skb if flags error

In reply to [email protected]
Quoted 21 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

tap_recvmsg() supports accepting skb by msg_control after commit 3b4ba04acca8 ("tap: support receiving skb from msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
  drivers/net/tap.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/tap.c +0 -0
index e9489b8..1c66b75 100644
@@ -1154,9 +1154,13 @@ static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  		       size_t total_len, int flags)
  {
  	struct tap_queue *q = container_of(sock, struct tap_queue, sock);

drivers/net/tap.c static int tap_recvmsg(struct socket *sock, struct msghdr *m, · line 1165

Quoted 10 lines
drivers/net/tap.c +5 -1
+	struct sk_buff *skb = m->msg_control;
  	int ret;
-	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
+	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
+		if (skb)
+			kfree_skb(skb);
  		return -EINVAL;
+	}
  	ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
  			  m->msg_control);

Need to deal with iov_iterator_count() == 0.

Thanks

drivers/net/tap.c static int tap_recvmsg(struct socket *sock, struct msghdr *m, · line 1166

Quoted 1 line
drivers/net/tap.c +0 -0
  	if (ret > total_len) {

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw

Re: [PATCH 1/3] vhost: fix skb leak in handle_rx()

In reply to [email protected]
Quoted 52 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

Eventually we figured out that it was a skb leak in handle_rx() when sending packets to the VM. This usually happens when a guest can not drain out vq as fast as vhost fills in, afterwards it sets off the traffic jam and leaks skb(s) which occurs as no headcount to send on the vq from vhost side.

This can be avoided by making sure we have got enough headcount before actually consuming a skb from the batched rx array while transmitting, which is simply done by moving checking the zero headcount a bit ahead.

---
  drivers/vhost/net.c | 20 ++++++++++----------
  1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vhost/net.c +3 -10
index 8d626d7..c7bdeb6 100644
@@ -778,16 +778,6 @@ static void handle_rx(struct vhost_net *net)
  		/* On error, stop handling until the next kick. */
  		if (unlikely(headcount < 0))
  			goto out;
-		if (nvq->rx_array)
-			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
-		/* On overrun, truncate and discard */
-		if (unlikely(headcount > UIO_MAXIOV)) {
-			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
-			err = sock->ops->recvmsg(sock, &msg,
-						 1, MSG_DONTWAIT | MSG_TRUNC);
-			pr_debug("Discarded rx packet: len %zd\n", sock_len);
-			continue;
-		}
  		/* OK, now we need to know about added descriptors. */
  		if (!headcount) {
  			if (unlikely(vhost_enable_notify(&net->dev, vq))) {
@@ -800,6 +790,16 @@ static void handle_rx(struct vhost_net *net)
  			 * they refilled. */
  			goto out;
  		}
+		if (nvq->rx_array)
+			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
+		/* On overrun, truncate and discard */

drivers/vhost/net.c static void handle_rx(struct vhost_net *net) · line 805

Quoted 10 lines
drivers/vhost/net.c +7 -0
+		if (unlikely(headcount > UIO_MAXIOV)) {
+			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
+			err = sock->ops->recvmsg(sock, &msg,
+						 1, MSG_DONTWAIT | MSG_TRUNC);
+			pr_debug("Discarded rx packet: len %zd\n", sock_len);
+			continue;
+		}
  		/* We don't need to be notified again. */
  		iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  		fixup = msg.msg_iter;

I suggest to reorder this patch to 3/3.

Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#9 lore raw

Re: [PATCH 2/3] tun: free skb in early errors

In reply to Jason Wang
Quoted 25 lines · Jason Wang
Quoted 22 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

tun_recvmsg() supports accepting skb by msg_control after commit ac77cfd4258f ("tun: support receiving skb through msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
  drivers/net/tun.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/tun.c +1 -0
index 6a7bde9..5563430 100644
@@ -2067,14 +2067,17 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  {
  	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  	struct tun_struct *tun = tun_get(tfile);
+	struct sk_buff *skb = m->msg_control;
Quoted 8 lines

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2075

Quoted 6 lines
drivers/net/tun.c +3 -2
  	int ret;
-	if (!tun)
-		return -EBADFD;
+	if (!tun) {
+		ret = -EBADFD;
+		goto out_free_skb;

Unfortunately, you can't to there since tun is NULL.

Right, this should just be kfree_skb(skb); return -EBADFD;

Quoted 18 lines
Quoted 10 lines
drivers/net/tun.c +2 -1
+	}
  	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  		ret = -EINVAL;
-		goto out;
+		goto out_free_skb;
  	}
  	if (flags & MSG_ERRQUEUE) {
  		ret = sock_recv_errqueue(sock->sk, m, total_len,

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2093

Quoted 4 lines
drivers/net/tun.c +1 -0
@@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  		m->msg_flags |= MSG_TRUNC;
  		ret = flags & MSG_TRUNC ? ret : total_len;
  	}
+	goto out;

We usually don't use goto in the case of success, and you need deal with the case skb != NULL but iov_iter_count(to) == 0 in tun_do_read().

Quoted 1 line

Thanks

I agree, the way to lay this out is:

	tun_put(tun);
	return ret;

err:

	tun_put(tun);
err_tun:
	if (skb)
		kfree_skb(skb);
	return ret;
Quoted 7 lines
Quoted 7 lines
drivers/net/tun.c +4 -0
+
+out_free_skb:
+	if (skb)
+		kfree_skb(skb);
  out:
  	tun_put(tun);
  	return ret;
#10 lore raw

Re: [PATCH 2/3] tun: free skb in early errors

In reply to Jason Wang
Quoted 25 lines · Jason Wang
Quoted 22 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

tun_recvmsg() supports accepting skb by msg_control after commit ac77cfd4258f ("tun: support receiving skb through msg_control"), the skb if presented should be freed within the function, otherwise it would be leaked.

---
  drivers/net/tun.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/tun.c +1 -0
index 6a7bde9..5563430 100644
@@ -2067,14 +2067,17 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  {
  	struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  	struct tun_struct *tun = tun_get(tfile);
+	struct sk_buff *skb = m->msg_control;
Quoted 8 lines

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2075

Quoted 6 lines
drivers/net/tun.c +3 -2
  	int ret;
-	if (!tun)
-		return -EBADFD;
+	if (!tun) {
+		ret = -EBADFD;
+		goto out_free_skb;

Unfortunately, you can't to there since tun is NULL.

Right, this should just be kfree_skb(skb); return -EBADFD;

Quoted 18 lines
Quoted 10 lines
drivers/net/tun.c +2 -1
+	}
  	if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  		ret = -EINVAL;
-		goto out;
+		goto out_free_skb;
  	}
  	if (flags & MSG_ERRQUEUE) {
  		ret = sock_recv_errqueue(sock->sk, m, total_len,

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2093

Quoted 4 lines
drivers/net/tun.c +1 -0
@@ -2087,6 +2090,11 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  		m->msg_flags |= MSG_TRUNC;
  		ret = flags & MSG_TRUNC ? ret : total_len;
  	}
+	goto out;

We usually don't use goto in the case of success, and you need deal with the case skb != NULL but iov_iter_count(to) == 0 in tun_do_read().

Quoted 1 line

Thanks

I agree, the way to lay this out is:

	tun_put(tun);
	return ret;

err:

	tun_put(tun);
err_tun:
	if (skb)
		kfree_skb(skb);
	return ret;
Quoted 7 lines

drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2100

Quoted 7 lines
drivers/net/tun.c +4 -0
+
+out_free_skb:
+	if (skb)
+		kfree_skb(skb);
  out:
  	tun_put(tun);
  	return ret;

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw

Re: [PATCH 1/3] vhost: fix skb leak in handle_rx()

In reply to Jason Wang
Quoted 68 lines · Jason Wang
Quoted 52 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

Eventually we figured out that it was a skb leak in handle_rx() when sending packets to the VM. This usually happens when a guest can not drain out vq as fast as vhost fills in, afterwards it sets off the traffic jam and leaks skb(s) which occurs as no headcount to send on the vq from vhost side.

This can be avoided by making sure we have got enough headcount before actually consuming a skb from the batched rx array while transmitting, which is simply done by moving checking the zero headcount a bit ahead.

---
  drivers/vhost/net.c | 20 ++++++++++----------
  1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vhost/net.c +3 -10
index 8d626d7..c7bdeb6 100644
@@ -778,16 +778,6 @@ static void handle_rx(struct vhost_net *net)
  		/* On error, stop handling until the next kick. */
  		if (unlikely(headcount < 0))
  			goto out;
-		if (nvq->rx_array)
-			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
-		/* On overrun, truncate and discard */
-		if (unlikely(headcount > UIO_MAXIOV)) {
-			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
-			err = sock->ops->recvmsg(sock, &msg,
-						 1, MSG_DONTWAIT | MSG_TRUNC);
-			pr_debug("Discarded rx packet: len %zd\n", sock_len);
-			continue;
-		}
  		/* OK, now we need to know about added descriptors. */
  		if (!headcount) {
  			if (unlikely(vhost_enable_notify(&net->dev, vq))) {
@@ -800,6 +790,16 @@ static void handle_rx(struct vhost_net *net)
  			 * they refilled. */
  			goto out;
  		}
+		if (nvq->rx_array)
+			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
+		/* On overrun, truncate and discard */

drivers/vhost/net.c static void handle_rx(struct vhost_net *net) · line 805

Quoted 10 lines
drivers/vhost/net.c +7 -0
+		if (unlikely(headcount > UIO_MAXIOV)) {
+			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
+			err = sock->ops->recvmsg(sock, &msg,
+						 1, MSG_DONTWAIT | MSG_TRUNC);
+			pr_debug("Discarded rx packet: len %zd\n", sock_len);
+			continue;
+		}
  		/* We don't need to be notified again. */
  		iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  		fixup = msg.msg_iter;

I suggest to reorder this patch to 3/3.

Quoted 1 line

Thanks

Why? This doesn't cause any new leaks, does it?

Signature · 5 lines
MST
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#4 lore raw

Re: [PATCH 1/3] vhost: fix skb leak in handle_rx()

Quoted 67 lines · Michael S. Tsirkin
Quoted 66 lines · Jason Wang
Quoted 52 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

Eventually we figured out that it was a skb leak in handle_rx() when sending packets to the VM. This usually happens when a guest can not drain out vq as fast as vhost fills in, afterwards it sets off the traffic jam and leaks skb(s) which occurs as no headcount to send on the vq from vhost side.

This can be avoided by making sure we have got enough headcount before actually consuming a skb from the batched rx array while transmitting, which is simply done by moving checking the zero headcount a bit ahead.

---
   drivers/vhost/net.c | 20 ++++++++++----------
   1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vhost/net.c +3 -10
index 8d626d7..c7bdeb6 100644
@@ -778,16 +778,6 @@ static void handle_rx(struct vhost_net *net)
   		/* On error, stop handling until the next kick. */
   		if (unlikely(headcount < 0))
   			goto out;
-		if (nvq->rx_array)
-			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
-		/* On overrun, truncate and discard */
-		if (unlikely(headcount > UIO_MAXIOV)) {
-			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
-			err = sock->ops->recvmsg(sock, &msg,
-						 1, MSG_DONTWAIT | MSG_TRUNC);
-			pr_debug("Discarded rx packet: len %zd\n", sock_len);
-			continue;
-		}
   		/* OK, now we need to know about added descriptors. */
   		if (!headcount) {
   			if (unlikely(vhost_enable_notify(&net->dev, vq))) {
@@ -800,6 +790,16 @@ static void handle_rx(struct vhost_net *net)
   			 * they refilled. */
   			goto out;
   		}
+		if (nvq->rx_array)
+			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
+		/* On overrun, truncate and discard */

drivers/vhost/net.c static void handle_rx(struct vhost_net *net) · line 805

Quoted 10 lines
drivers/vhost/net.c +7 -0
+		if (unlikely(headcount > UIO_MAXIOV)) {
+			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
+			err = sock->ops->recvmsg(sock, &msg,
+						 1, MSG_DONTWAIT | MSG_TRUNC);
+			pr_debug("Discarded rx packet: len %zd\n", sock_len);
+			continue;
+		}
   		/* We don't need to be notified again. */
   		iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
   		fixup = msg.msg_iter;

I suggest to reorder this patch to 3/3.

Quoted 3 lines
Quoted 1 line

Thanks

Why? This doesn't cause any new leaks, does it?

It doesn't, just think it can ease the downstream back porting in case patch 2-3 were missed if somebody did a bisect and just backport patch 1.

Thanks

#5 lore raw

Re: [PATCH 1/3] vhost: fix skb leak in handle_rx()

In reply to Michael S. Tsirkin
Quoted 67 lines · Michael S. Tsirkin
Quoted 66 lines · Jason Wang
Quoted 52 lines · On 2017年12月01日 13:54, [email protected] wrote:

From: Wei Xu <[email protected]>

Matthew found a roughly 40% tcp throughput regression with commit c67df11f(vhost_net: try batch dequing from skb array) as discussed in the following thread:
https://www.mail-archive.com/[email protected]/msg187936.html

Eventually we figured out that it was a skb leak in handle_rx() when sending packets to the VM. This usually happens when a guest can not drain out vq as fast as vhost fills in, afterwards it sets off the traffic jam and leaks skb(s) which occurs as no headcount to send on the vq from vhost side.

This can be avoided by making sure we have got enough headcount before actually consuming a skb from the batched rx array while transmitting, which is simply done by moving checking the zero headcount a bit ahead.

---
   drivers/vhost/net.c | 20 ++++++++++----------
   1 file changed, 10 insertions(+), 10 deletions(-)
drivers/vhost/net.c +3 -10
index 8d626d7..c7bdeb6 100644
@@ -778,16 +778,6 @@ static void handle_rx(struct vhost_net *net)
   		/* On error, stop handling until the next kick. */
   		if (unlikely(headcount < 0))
   			goto out;
-		if (nvq->rx_array)
-			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
-		/* On overrun, truncate and discard */
-		if (unlikely(headcount > UIO_MAXIOV)) {
-			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
-			err = sock->ops->recvmsg(sock, &msg,
-						 1, MSG_DONTWAIT | MSG_TRUNC);
-			pr_debug("Discarded rx packet: len %zd\n", sock_len);
-			continue;
-		}
   		/* OK, now we need to know about added descriptors. */
   		if (!headcount) {
   			if (unlikely(vhost_enable_notify(&net->dev, vq))) {
@@ -800,6 +790,16 @@ static void handle_rx(struct vhost_net *net)
   			 * they refilled. */
   			goto out;
   		}
+		if (nvq->rx_array)
+			msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
+		/* On overrun, truncate and discard */

drivers/vhost/net.c static void handle_rx(struct vhost_net *net) · line 805

Quoted 10 lines
drivers/vhost/net.c +7 -0
+		if (unlikely(headcount > UIO_MAXIOV)) {
+			iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
+			err = sock->ops->recvmsg(sock, &msg,
+						 1, MSG_DONTWAIT | MSG_TRUNC);
+			pr_debug("Discarded rx packet: len %zd\n", sock_len);
+			continue;
+		}
   		/* We don't need to be notified again. */
   		iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
   		fixup = msg.msg_iter;

I suggest to reorder this patch to 3/3.

Quoted 3 lines
Quoted 1 line

Thanks

Why? This doesn't cause any new leaks, does it?

It doesn't, just think it can ease the downstream back porting in case patch 2-3 were missed if somebody did a bisect and just backport patch 1.

Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization