Skip to contentSkip to conversation

aio-posix: signal-proof fdmon-io_uring

PATCH Applied Stefan Hajnoczi 3 participants 6 messages Apr 8, 2020 – Apr 9, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aio-posix: signal-proof fdmon-io_uring

The io_uring_enter(2) syscall returns with errno=EINTR when interrupted by a signal. Retry the syscall in this case.

It's essential to do this in the io_uring_submit_and_wait() case. My interpretation of the Linux v5.5 io_uring_enter(2) code is that it shouldn't affect the io_uring_submit() case, but there is no guarantee this will always be the case. Let's check for -EINTR around both APIs.

Note that the liburing APIs have -errno return values.

---
 util/fdmon-io_uring.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
util/fdmon-io_uring.c +8 -2
index b4d6109f20..d5a80ed6fb 100644
@@ -88,7 +88,10 @@ static struct io_uring_sqe *get_sqe(AioContext *ctx)
     }
 
     /* No free sqes left, submit pending sqes first */
-    ret = io_uring_submit(ring);
+    do {
+        ret = io_uring_submit(ring);
+    } while (ret == -EINTR);
+
     assert(ret > 1);
     sqe = io_uring_get_sqe(ring);
     assert(sqe);
@@ -282,7 +285,10 @@ static int fdmon_io_uring_wait(AioContext *ctx, AioHandlerList *ready_list,
 
     fill_sq_ring(ctx);
 
-    ret = io_uring_submit_and_wait(&ctx->fdmon_io_uring, wait_nr);
+    do {
+        ret = io_uring_submit_and_wait(&ctx->fdmon_io_uring, wait_nr);
+    } while (ret == -EINTR);
+
     assert(ret >= 0);
 
     return process_cq_ring(ctx, ready_list);
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Stefan Hajnoczi

The io_uring_enter(2) syscall returns with errno=EINTR when interrupted by a signal. Retry the syscall in this case.

It's essential to do this in the io_uring_submit_and_wait() case. My interpretation of the Linux v5.5 io_uring_enter(2) code is that it shouldn't affect the io_uring_submit() case, but there is no guarantee this will always be the case. Let's check for -EINTR around both APIs.

Quoted 6 lines

Note that the liburing APIs have -errno return values.

---
 util/fdmon-io_uring.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

The patch LGTM:

Not related to this patch, looking at block/io_uring.c, should we retry if the io_uring_submit() fails with EINTR?

I mean something like this:

block/io_uring.c +1 -1
index a3142ca989..9765681f7c 100644
@@ -231,7 +231,7 @@ static int ioq_submit(LuringState *s)
         trace_luring_io_uring_submit(s, ret);
         /* Prevent infinite loop if submission is refused */
         if (ret <= 0) {
-            if (ret == -EAGAIN) {
+            if (ret == -EAGAIN || ret == -EINTR) {
                 continue;
             }
             break;

Thanks,
Stefano

#2 lore raw
Quoted 30 lines · Stefano Garzarella
Quoted 8 lines · Stefan Hajnoczi

The io_uring_enter(2) syscall returns with errno=EINTR when interrupted by a signal. Retry the syscall in this case.

It's essential to do this in the io_uring_submit_and_wait() case. My interpretation of the Linux v5.5 io_uring_enter(2) code is that it shouldn't affect the io_uring_submit() case, but there is no guarantee this will always be the case. Let's check for -EINTR around both APIs.

Quoted 6 lines

Note that the liburing APIs have -errno return values.

---
 util/fdmon-io_uring.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

The patch LGTM:

Not related to this patch, looking at block/io_uring.c, should we retry if the io_uring_submit() fails with EINTR?

I mean something like this:

block/io_uring.c +0 -0
index a3142ca989..9765681f7c 100644

block/io_uring.c static int ioq_submit(LuringState *s) · line 237

Quoted 8 lines
block/io_uring.c +1 -1
@@ -231,7 +231,7 @@ static int ioq_submit(LuringState *s)
         trace_luring_io_uring_submit(s, ret);
         /* Prevent infinite loop if submission is refused */
         if (ret <= 0) {
-            if (ret == -EAGAIN) {
+            if (ret == -EAGAIN || ret == -EINTR) {
                 continue;
             }
             break;

Thanks!

I didn't do that for the reason described in the commit message. Philippe also mentioned that EINTR isn't listed on the io_uring_enter(2) man page, although that is a bug because it does occur with IORING_ENTER_GETEVENTS and min_complete > 0.

Feel free to send a separate patch. It's probably not something that needs to go into QEMU 5.0.

Stefan

#3 lore raw
Quoted 42 lines · Stefan Hajnoczi
Quoted 30 lines · Stefano Garzarella
Quoted 8 lines · Stefan Hajnoczi

The io_uring_enter(2) syscall returns with errno=EINTR when interrupted by a signal. Retry the syscall in this case.

It's essential to do this in the io_uring_submit_and_wait() case. My interpretation of the Linux v5.5 io_uring_enter(2) code is that it shouldn't affect the io_uring_submit() case, but there is no guarantee this will always be the case. Let's check for -EINTR around both APIs.

Quoted 6 lines

Note that the liburing APIs have -errno return values.

---
 util/fdmon-io_uring.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

The patch LGTM:

Not related to this patch, looking at block/io_uring.c, should we retry if the io_uring_submit() fails with EINTR?

I mean something like this:

block/io_uring.c +0 -0
index a3142ca989..9765681f7c 100644

block/io_uring.c static int ioq_submit(LuringState *s) · line 237

Quoted 8 lines
block/io_uring.c +1 -1
@@ -231,7 +231,7 @@ static int ioq_submit(LuringState *s)
         trace_luring_io_uring_submit(s, ret);
         /* Prevent infinite loop if submission is refused */
         if (ret <= 0) {
-            if (ret == -EAGAIN) {
+            if (ret == -EAGAIN || ret == -EINTR) {
                 continue;
             }
             break;

Thanks!

Quoted 1 line

I didn't do that for the reason described in the commit message.

Yeah, I agree and it was not well documented in io_uring_enter(2)!

Quoted 2 lines

Philippe also mentioned that EINTR isn't listed on the io_uring_enter(2) man page,

FYI there was a discussion on io-uring list just few days ago [1] about that and now it is documented [2]. :-)

Quoted 2 lines

although that is a bug because it does occur with
IORING_ENTER_GETEVENTS and min_complete > 0.

I think we are safe for now.
IIUC io_uring_submit() sets min_complete to 0 and IORING_ENTER_GETEVENTS is set only if IOPOLL is enabled (or min_complete > 0), but it is not our case (for now).

Quoted 3 lines

Feel free to send a separate patch. It's probably not something that needs to go into QEMU 5.0.

Sure, I'll send it after the freeze.

Thanks,
Stefano

[1] https://lore.kernel.org/io-uring/[email protected]/t/#u
[2] https://github.com/axboe/liburing/commit/344355ec6619de8f4e64584c9736530b5346e4f4

Reviewed-by on v1 1/1
#4 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

The io_uring_enter(2) syscall returns with errno=EINTR when interrupted by a signal. Retry the syscall in this case.

It's essential to do this in the io_uring_submit_and_wait() case. My interpretation of the Linux v5.5 io_uring_enter(2) code is that it shouldn't affect the io_uring_submit() case, but there is no guarantee this will always be the case.

This is how io_uring_enter() is documented indeed (no EINTR).

Quoted 38 lines

Let's check for -EINTR around both APIs.

Note that the liburing APIs have -errno return values.

---
  util/fdmon-io_uring.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
util/fdmon-io_uring.c +8 -2
index b4d6109f20..d5a80ed6fb 100644
@@ -88,7 +88,10 @@ static struct io_uring_sqe *get_sqe(AioContext *ctx)
      }
 
      /* No free sqes left, submit pending sqes first */
-    ret = io_uring_submit(ring);
+    do {
+        ret = io_uring_submit(ring);
+    } while (ret == -EINTR);
+
      assert(ret > 1);
      sqe = io_uring_get_sqe(ring);
      assert(sqe);
@@ -282,7 +285,10 @@ static int fdmon_io_uring_wait(AioContext *ctx, AioHandlerList *ready_list,
 
      fill_sq_ring(ctx);
 
-    ret = io_uring_submit_and_wait(&ctx->fdmon_io_uring, wait_nr);
+    do {
+        ret = io_uring_submit_and_wait(&ctx->fdmon_io_uring, wait_nr);
+    } while (ret == -EINTR);
+
      assert(ret >= 0);
 
      return process_cq_ring(ctx, ready_list);
author Applied
#5 lore raw
In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi

The io_uring_enter(2) syscall returns with errno=EINTR when interrupted by a signal. Retry the syscall in this case.

It's essential to do this in the io_uring_submit_and_wait() case. My interpretation of the Linux v5.5 io_uring_enter(2) code is that it shouldn't affect the io_uring_submit() case, but there is no guarantee this will always be the case. Let's check for -EINTR around both APIs.

Quoted 6 lines

Note that the liburing APIs have -errno return values.

---
 util/fdmon-io_uring.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan