Skip to contentSkip to conversation

io_uring: kiocb_done() should *not* trust ->ki_pos if ->{read,write}_iter() failed

PATCH In review Al Viro 3 participants 3 messages Oct 26, 2023

Subsystems (2)
lore
Share & tools
lore

Al Viro

author maintainer
#0 lore raw

PATCH 1/1 io_uring: kiocb_done() should *not* trust ->ki_pos if ->{read,write}_iter() failed

[in viro/vfs.git#fixes at the moment]
->ki_pos value is unreliable in such cases. For an obvious example, consider O_DSYNC write - we feed the data to page cache and start IO, then we make sure it's completed. Update of ->ki_pos is dealt with by the first part; failure in the second ends up with negative value returned _and_ ->ki_pos left advanced as if sync had been successful. In the same situation write(2) does not advance the file position at all.

---
 io_uring/rw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/rw.c +1 -1
index c8c822fa7980..08d94fb972f0 100644
@@ -339,7 +339,7 @@ static int kiocb_done(struct io_kiocb *req, ssize_t ret,
 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
 	unsigned final_ret = io_fixup_rw_res(req, ret);
 
-	if (req->flags & REQ_F_CUR_POS)
+	if (ret >= 0 && req->flags & REQ_F_CUR_POS)
 		req->file->f_pos = rw->kiocb.ki_pos;
 	if (ret >= 0 && (rw->kiocb.ki_complete == io_complete_rw)) {
 		if (!__io_complete_rw_common(req, ret)) {
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Al Viro

[in viro/vfs.git#fixes at the moment]
->ki_pos value is unreliable in such cases. For an obvious example, consider O_DSYNC write - we feed the data to page cache and start IO, then we make sure it's completed. Update of ->ki_pos is dealt with by the first part; failure in the second ends up with negative value returned _and_ ->ki_pos left advanced as if sync had been successful. In the same situation write(2) does not advance the file position at all.

---

Looks good to me,

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Al Viro
Quoted 8 lines · Al Viro

[in viro/vfs.git#fixes at the moment]
->ki_pos value is unreliable in such cases. For an obvious example, consider O_DSYNC write - we feed the data to page cache and start IO, then we make sure it's completed. Update of ->ki_pos is dealt with by the first part; failure in the second ends up with negative value returned _and_ ->ki_pos left advanced as if sync had been successful. In the same situation write(2) does not advance the file position at all.

Looks good, thanks Al:

or let me know if you want me to pick it up.

Signature · 1 line
Jens Axboe