[PATCH for-11.2] net: Don't assert when net_socket_send() hits EWOULDBLOCK

Peter Maydell posted 1 patch 3 days, 9 hours ago
net/socket.c      | 4 +++-
net/stream_data.c | 1 +
2 files changed, 4 insertions(+), 1 deletion(-)
[PATCH for-11.2] net: Don't assert when net_socket_send() hits EWOULDBLOCK
Posted by Peter Maydell 3 days, 9 hours ago
In net_socket_send(), if the recv() call returns with an EWOULDBLOCK
error, we continue in the function to call net_fill_rstate() with a
negative size.  Before commit e9e0a5854b6dc, this was harmless, as
the loop in net_fill_rstate() would not execute, so it did nothing
and returned.  However, in that commit we added an assertion that
when the loop has finished it has completed handling of all the data
and the size is now zero.  That assert will fire if we get
EWOULDBLOCK and pass a negative size into net_fill_rstate().

The code as it stands today has this flow of execution because
originally net_fill_rstate() was inline inside net_socket_send(), and
so it was perhaps a bit more obvious that it would do nothing for a
negative size value.  However, now it's out-of-line it's definitely
confusing to call it in the error case.  Return early instead.

This fixes a bug where using -netdev socket for real world workloads
tends to cause QEMU to hit this assertion. This bug has been present
in the code for a decade, but recent changes to io_uring code in QEMU
in the 10.2 timeframe seem to have made it more likely to occur.

The same problem also exists in net_stream_data_send(), though this
one hasn't been observed to cause problems in the wild.

Thanks to Alistair Bayley for tracking down the cause of this bug and
suggesting the fix, and to Travis Frisinger for providing a
standalone test that triggers the assertion.

Cc: qemu-stable@nongnu.org
Fixes: e9e0a5854b6dc ("net/net: properly handle multiple packets in net_fill_rstate()")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3285
Suggested-by: Alistair Bayley <https://gitlab.com/abayley>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
These functions could usefully be simplified to remove the gotos,
but for this bugfix for stable and for 11.2 let's just do the
simple direct change.
---
 net/socket.c      | 4 +++-
 net/stream_data.c | 1 +
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/net/socket.c b/net/socket.c
index 1ad03fc9d45..eb0fc4e404b 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -162,8 +162,10 @@ static void net_socket_send(void *opaque)
 
     size = recv(s->fd, buf1, sizeof(buf1), 0);
     if (size < 0) {
-        if (errno != EWOULDBLOCK)
+        if (errno != EWOULDBLOCK) {
             goto eoc;
+        }
+        return;
     } else if (size == 0) {
         /* end of connection */
     eoc:
diff --git a/net/stream_data.c b/net/stream_data.c
index 73b45da9fe3..5dd766cb8cb 100644
--- a/net/stream_data.c
+++ b/net/stream_data.c
@@ -101,6 +101,7 @@ gboolean net_stream_data_send(QIOChannel *ioc, GIOCondition condition,
         if (errno != EWOULDBLOCK) {
             goto eoc;
         }
+        return G_SOURCE_CONTINUE;
     } else if (size == 0) {
         /* end of connection */
     eoc:
-- 
2.43.0