Skip to contentSkip to conversation

vhost/net: Fix receive queue test in busy poll

PATCH Applied raoxu 4 participants 4 messages Nov 20 – Nov 26

Subsystems (6)
lore
Share & tools
lore

raoxu

author
#0 lore raw

PATCH v2 1/1 vhost/net: Fix receive queue test in busy poll

From: Xu Rao <[email protected]>

sock_has_rx_data() returns a positive value when receive data is available. For sockets without a peek_len() callback, however, its fallback returns skb_queue_empty(), which has the opposite meaning.

An AF_PACKET SOCK_RAW socket is accepted by get_raw_socket() as a vhost-net backend, and its socket operations do not provide peek_len(). With busy polling enabled for this backend, an empty receive queue is treated as ready and a nonempty queue as not ready. This can make vhost_net_busy_poll() stop polling prematurely or fail to stop for queued receive data, and invert its receive-side scheduling decision. TUN and TAP backends provide peek_len() and are unaffected.

Negate skb_queue_empty() so the fallback matches the callback and the readiness checks in vhost_net_busy_poll().

The issue was found during AI-assisted code review. The helper's incorrect result was confirmed on x86_64 running 6.6.0-amd64-desktop using a temporary diagnostic module containing the original and fixed helpers. An AF_PACKET SOCK_RAW socket, without PACKET_RX_RING, was bound to loopback in a private network namespace. Across three cycles of sending and receiving a 60-byte frame, the original helper returned 1 for an empty queue and 0 for a queued frame. The fixed helper returned 0 and 1 respectively, matching socket readability in all nine checks. This tests helper copies on real sockets, not the running vhost-net busy-poll path; the vhost-net module was not replaced.

The patched drivers/vhost/net.o was also build-tested on the net tree with x86_64 GCC 12.3 and W=1 under allmodconfig and allyesconfig. No end-to-end vhost tests or kernel selftests were run.

Fixes 1576d9860599 ("tun: switch to use skb array for tx")

---
Changes in v2:
- Target net and add the introducing commit's Fixes tag.
- Explain the raw packet socket backend and busy-poll decisions.
- State the AI-assisted discovery method and document the diagnostic module test, its results and its helper-level coverage.
- Document the targeted build tests.
- Keep the code change unchanged from v1.

v1: https://lore.kernel.org/virtualization/[email protected]/

AI assistance: OpenCode was used to inspect the history, prepare and run the diagnostic module test, run build checks, and revise this message. The code change is unchanged from v1. The discovery method was confirmed by the author.

 drivers/vhost/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vhost/net.c +1 -1
index 4570a4ffbab2..10ed2d55ff07 100644
@@ -538,7 +538,7 @@ static int sock_has_rx_data(struct socket *sock)
 	if (sock->ops->peek_len)
 		return sock->ops->peek_len(sock);
 
-	return skb_queue_empty(&sock->sk->sk_receive_queue);
+	return !skb_queue_empty(&sock->sk->sk_receive_queue);
 }
 
 static void vhost_net_busy_poll_try_queue(struct vhost_net *net,

base-commit: 435cbdaf9b03687498dc16fe4af8d16a977e21d0

Signature · 1 line
2.50.1
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

From: Xu Rao <[email protected]>

sock_has_rx_data() returns a positive value when receive data is available. For sockets without a peek_len() callback, however, its fallback returns skb_queue_empty(), which has the opposite meaning.

An AF_PACKET SOCK_RAW socket is accepted by get_raw_socket() as a vhost-net backend, and its socket operations do not provide peek_len(). With busy polling enabled for this backend, an empty receive queue is treated as ready and a nonempty queue as not ready. This can make vhost_net_busy_poll() stop polling prematurely or fail to stop for queued receive data, and invert its receive-side scheduling decision. TUN and TAP backends provide peek_len() and are unaffected.

Quoted 7 lines

Negate skb_queue_empty() so the fallback matches the callback and the [ ... ]
No end-to-end vhost tests or kernel selftests were run.

Fixes 1576d9860599 ("tun: switch to use skb array for tx")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v2 1/1
#2 lore raw
In reply to raoxu
Quoted 29 lines · raoxu

From: Xu Rao <[email protected]>

sock_has_rx_data() returns a positive value when receive data is available. For sockets without a peek_len() callback, however, its fallback returns skb_queue_empty(), which has the opposite meaning.

An AF_PACKET SOCK_RAW socket is accepted by get_raw_socket() as a vhost-net backend, and its socket operations do not provide peek_len(). With busy polling enabled for this backend, an empty receive queue is treated as ready and a nonempty queue as not ready. This can make vhost_net_busy_poll() stop polling prematurely or fail to stop for queued receive data, and invert its receive-side scheduling decision. TUN and TAP backends provide peek_len() and are unaffected.

Negate skb_queue_empty() so the fallback matches the callback and the readiness checks in vhost_net_busy_poll().

The issue was found during AI-assisted code review. The helper's incorrect result was confirmed on x86_64 running 6.6.0-amd64-desktop using a temporary diagnostic module containing the original and fixed helpers. An AF_PACKET SOCK_RAW socket, without PACKET_RX_RING, was bound to loopback in a private network namespace. Across three cycles of sending and receiving a 60-byte frame, the original helper returned 1 for an empty queue and 0 for a queued frame. The fixed helper returned 0 and 1 respectively, matching socket readability in all nine checks. This tests helper copies on real sockets, not the running vhost-net busy-poll path; the vhost-net module was not replaced.

Quoted 6 lines

The patched drivers/vhost/net.o was also build-tested on the net tree with x86_64 GCC 12.3 and W=1 under allmodconfig and allyesconfig. No end-to-end vhost tests or kernel selftests were run.

Fixes 1576d9860599 ("tun: switch to use skb array for tx")
Quoted 38 lines

---
Changes in v2:
- Target net and add the introducing commit's Fixes tag.
- Explain the raw packet socket backend and busy-poll decisions.
- State the AI-assisted discovery method and document the diagnostic module test, its results and its helper-level coverage.
- Document the targeted build tests.
- Keep the code change unchanged from v1.

v1: https://lore.kernel.org/virtualization/[email protected]/

AI assistance: OpenCode was used to inspect the history, prepare and run the diagnostic module test, run build checks, and revise this message. The code change is unchanged from v1. The discovery method was confirmed by the author.

 drivers/vhost/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vhost/net.c +1 -1
index 4570a4ffbab2..10ed2d55ff07 100644
@@ -538,7 +538,7 @@ static int sock_has_rx_data(struct socket *sock)
        if (sock->ops->peek_len)
                return sock->ops->peek_len(sock);
 
-       return skb_queue_empty(&sock->sk->sk_receive_queue);
+       return !skb_queue_empty(&sock->sk->sk_receive_queue);
 }
 
 static void vhost_net_busy_poll_try_queue(struct vhost_net *net,

base-commit: 435cbdaf9b03687498dc16fe4af8d16a977e21d0 --
2.50.1

#3 lore raw
In reply to raoxu
This patch was applied to netdev/net.git (main) by Willem de Bruijn <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Willem de Bruijn <[email protected]>:

Quoted 14 lines · On Fri, 20 Nov 2026 11:38:24 +0800 you wrote:

From: Xu Rao <[email protected]>

sock_has_rx_data() returns a positive value when receive data is available. For sockets without a peek_len() callback, however, its fallback returns skb_queue_empty(), which has the opposite meaning.

An AF_PACKET SOCK_RAW socket is accepted by get_raw_socket() as a vhost-net backend, and its socket operations do not provide peek_len(). With busy polling enabled for this backend, an empty receive queue is treated as ready and a nonempty queue as not ready. This can make vhost_net_busy_poll() stop polling prematurely or fail to stop for queued receive data, and invert its receive-side scheduling decision. TUN and TAP backends provide peek_len() and are unaffected.

Quoted 1 line

[...]

Here is the summary with links:
- [net,v2] vhost/net: Fix receive queue test in busy poll

    https://git.kernel.org/netdev/net/c/cdf84b850584

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html