io_uring/rw: fix missing ERESTARTSYS conversion in read paths
Subsystems (2)
PATCH 1/1 io_uring/rw: fix missing ERESTARTSYS conversion in read paths
Both read and write may receive internal restart error codes from the filesystem layer and should be converted to -EINTR. However, when multishot read support was added, the error code normalization was lost for both io_read() and io_read_mshot().
Extract the conversion into io_fixup_restart_res() and apply it in all three locations: io_rw_done(), io_read(), and io_read_mshot().
--- io_uring/rw.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-)
io_uring/rw.c +24 -14
@@ -615,6 +615,24 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)smp_store_release (&req->iopoll_completed,1 ); } +static inline ssize_t io_fixup_restart_res (ssize_t ret) +{ +switch (ret) { +case -ERESTARTSYS : +case -ERESTARTNOINTR : +case -ERESTARTNOHAND : +case -ERESTART_RESTARTBLOCK : +/* +* We can't just restart the syscall, since previously +* submitted sqes may already be in progress. Just fail +* this IO with EINTR. +*/ +return -EINTR ; +default : +return ret; + } +} +static inline void io_rw_done (struct io_kiocb *req,ssize_t ret) { struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
@@ -625,19 +643,7 @@ static inline void io_rw_done(struct io_kiocb *req, ssize_t ret)/* transform internal restart error codes */ if (unlikely (ret <0 )) { -switch (ret) { -case -ERESTARTSYS : -case -ERESTARTNOINTR : -case -ERESTARTNOHAND : -case -ERESTART_RESTARTBLOCK : -/* -* We can't just restart the syscall, since previously -* submitted sqes may already be in progress. Just fail -* this IO with EINTR. -*/ - ret = -EINTR ; -break ; - } + ret =io_fixup_restart_res (ret); }if (req->flags &REQ_F_IOPOLL )
@@ -1034,6 +1040,8 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags)if (req->flags &REQ_F_BUFFERS_COMMIT )io_kbuf_recycle (req, sel.buf_list, issue_flags); + + ret =io_fixup_restart_res (ret);return ret; }
@@ -1068,8 +1076,10 @@ int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)return IOU_RETRY ; }else if (ret <=0 ) {io_kbuf_recycle (req, sel.buf_list, issue_flags); -if (ret <0 ) +if (ret <0 ) { + ret =io_fixup_restart_res (ret);req_set_fail (req); + } }else if (!(req->flags &REQ_F_APOLL_MULTISHOT )) { cflags =io_put_kbuf (req, ret, sel.buf_list); }else {
Signature · 1 line
2.43.0
Jens Axboe
maintainerQuoted 7 lines · Yitang Yang
Both read and write may receive internal restart error codes from the filesystem layer and should be converted to
-EINTR. However, when multishot read support was added, the error code normalization was lost for bothio_read()andio_read_mshot().Extract the conversion into
io_fixup_restart_res()and apply it in all three locations:io_rw_done(),io_read(), andio_read_mshot().
How was this found? Reason I ask is that I wonder if this is even necessary in the first place. If you found this because of an issue you ran into, then that answers that question clearly (yes we need it). But if this some LLM find, then that's an entirely different story.
Signature · 1 line
Jens Axboe
I found this while trying to combine io_uring read multishot with userfaultfd. I wrote a reproducer here:
https://gist.github.com/wokron/7f75026e02792844b0bbf26b7e79c9ef
Running it with "sudo ./userfaultfd-uring -n 16 -p 10000" on my machine consistently produces "io_uring_cqe error: Unknown error 512".
Quoted 18 lines · Jens Axboe <[email protected]> 于2026年7月22日周三 00:32写道:
Quoted 7 lines · Yitang Yang
Both read and write may receive internal restart error codes from the filesystem layer and should be converted to
-EINTR. However, when multishot read support was added, the error code normalization was lost for bothio_read()andio_read_mshot().Extract the conversion into
io_fixup_restart_res()and apply it in all three locations:io_rw_done(),io_read(), andio_read_mshot().How was this found? Reason I ask is that I wonder if this is even necessary in the first place. If you found this because of an issue you ran into, then that answers that question clearly (yes we need it). But if this some LLM find, then that's an entirely different story.
--
Jens Axboe
Jens Axboe
maintainerQuoted 6 lines · Yitang Yang
I found this while trying to combine
io_uringread multishot with userfaultfd. I wrote a reproducer here:
https://gist.github.com/wokron/7f75026e02792844b0bbf26b7e79c9efRunning it with "sudo ./userfaultfd-uring -n 16 -p 10000" on my machine consistently produces "
io_uring_cqeerror: Unknown error 512".
Perfect, that's a much more useful find then! I'll take a closer look at your patch.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 7 lines · Yitang Yang
Both read and write may receive internal restart error codes from the filesystem layer and should be converted to
-EINTR. However, when multishot read support was added, the error code normalization was lost for bothio_read()andio_read_mshot().Extract the conversion into
io_fixup_restart_res()and apply it in all three locations:io_rw_done(),io_read(), andio_read_mshot().
Functionally this looks good, just a few cosmetic nits below.
Quoted 2 lines
Fixes a08d195b586a ("io_uring/rw: split io_read() into a helper")Signed-off-by Yitang Yang <[email protected]>
And this one needs a Cc: [email protected]
Quoted 9 lines
io_uring/rw.c +0
-5@@ -625,19 +643,7 @@ static inline void io_rw_done(struct io_kiocb *req, ssize_t ret)/* transform internal restart error codes */ if (unlikely (ret <0 )) { -switch (ret) { -case -ERESTARTSYS : -case -ERESTARTNOINTR : -case -ERESTARTNOHAND : -case -ERESTART_RESTARTBLOCK :
io_uring/rw.c static inline void io_rw_done(struct io_kiocb *req, ssize_t ret) · line 647
Quoted 10 lines
io_uring/rw.c +1
-8-/* -* We can't just restart the syscall, since previously -* submitted sqes may already be in progress. Just fail -* this IO with EINTR. -*/ - ret = -EINTR ; -break ; - } + ret =io_fixup_restart_res (ret); }
No need for braces anymore on the 'if' statement, just drop those.
io_uring/rw.c int io_read(struct io_kiocb *req, unsigned int issue_flags) · line 1046
Quoted 8 lines
io_uring/rw.c +2
-0@@ -1034,6 +1040,8 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags)if (req->flags &REQ_F_BUFFERS_COMMIT )io_kbuf_recycle (req, sel.buf_list, issue_flags); + + ret =io_fixup_restart_res (ret);return ret; }
return io_fixup_restart_res(ret);
Signature · 1 line
Jens Axboe
Thanks! I'll send a v2.
Jens Axboe
maintainerQuoted 1 line · Yitang Yang
Thanks! I'll send a v2.
Thanks, applied it - can you send your test case as a liburing test/ addition as well?
Signature · 1 line
Jens Axboe
PATCH 1/1 test: add rw-restart-ret regression test
The filesystem layer may return internal restart codes to io_uring, which should be converted to -EINTR before being delivered to userspace. Add a test using userfaultfd with read multishot to expose the issue and prevent regressions.
--- test/Makefile | 1 + test/rw-restart-ret.c | 304 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100644 test/rw-restart-ret.c
test/Makefile +1 -0
@@ -244,6 +244,7 @@ test_srcs := \ ring-leak.c \ ring-query.c \ rsrc_tags.c \ + rw-restart-ret.c \ rw_merge_test.c \ self.c \ recvsend_bundle.c \
test/rw-restart-ret.c new file +304 -0
304 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 1/1 io_uring/rw: fix missing ERESTARTSYS conversion in read paths
In reply to Yitang YangBoth read and write may receive internal restart error codes from the filesystem layer and should be converted to -EINTR. However, when multishot read support was added, the error code normalization was lost for both io_read() and io_read_mshot().
Extract the conversion into io_fixup_restart_res() and apply it in all three locations: io_rw_done(), io_read(), and io_read_mshot().
--- io_uring/rw.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-)
io_uring/rw.c +25 -17
@@ -615,6 +615,24 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)smp_store_release (&req->iopoll_completed,1 ); } +static inline ssize_t io_fixup_restart_res (ssize_t ret) +{ +switch (ret) { +case -ERESTARTSYS : +case -ERESTARTNOINTR : +case -ERESTARTNOHAND : +case -ERESTART_RESTARTBLOCK : +/* +* We can't just restart the syscall, since previously +* submitted sqes may already be in progress. Just fail +* this IO with EINTR. +*/ +return -EINTR ; +default : +return ret; + } +} +static inline void io_rw_done (struct io_kiocb *req,ssize_t ret) { struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
@@ -624,21 +642,8 @@ static inline void io_rw_done(struct io_kiocb *req, ssize_t ret)return ;/* transform internal restart error codes */ -if (unlikely (ret <0 )) { -switch (ret) { -case -ERESTARTSYS : -case -ERESTARTNOINTR : -case -ERESTARTNOHAND : -case -ERESTART_RESTARTBLOCK : -/* -* We can't just restart the syscall, since previously -* submitted sqes may already be in progress. Just fail -* this IO with EINTR. -*/ - ret = -EINTR ; -break ; - } - } +if (unlikely (ret <0 )) + ret =io_fixup_restart_res (ret);if (req->flags &REQ_F_IOPOLL )io_complete_rw_iopoll (&rw->kiocb, ret);
@@ -1034,7 +1039,8 @@ int io_read(struct io_kiocb *req, unsigned int issue_flags)if (req->flags &REQ_F_BUFFERS_COMMIT )io_kbuf_recycle (req, sel.buf_list, issue_flags); -return ret; + +return io_fixup_restart_res (ret); }int io_read_mshot (struct io_kiocb *req,unsigned int issue_flags)
@@ -1068,8 +1074,10 @@ int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)return IOU_RETRY ; }else if (ret <=0 ) {io_kbuf_recycle (req, sel.buf_list, issue_flags); -if (ret <0 ) +if (ret <0 ) { + ret =io_fixup_restart_res (ret);req_set_fail (req); + } }else if (!(req->flags &REQ_F_APOLL_MULTISHOT )) { cflags =io_put_kbuf (req, ret, sel.buf_list); }else {
Signature · 1 line
2.43.0
Quoted 9 lines · Yitang Yang
Both read and write may receive internal restart error codes from the filesystem layer and should be converted to
-EINTR. However, when multishot read support was added, the error code normalization was lost for bothio_read()andio_read_mshot().Extract the conversion into
io_fixup_restart_res()and apply it in all three locations:io_rw_done(),io_read(), andio_read_mshot().[...]
Applied, thanks!
[1/1] io_uring/rw: fix missing ERESTARTSYS conversion in read paths
commit: ab05caca123c6d0b41850b7c05b246e4dca4a770
Best regards,
Signature · 1 line
Jens Axboe