Skip to contentSkip to conversation

fs/io_uring: Hoist ret2 == -EAGAIN check in tail of io_write

PATCH Superseded Noah Goldstein 2 participants 3 messages Oct 18, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/io_uring: Hoist ret2 == -EAGAIN check in tail of io_write

This commit reorganizes the branches in the tail of io_write so that the 'ret2 == -EAGAIN' check is not repeated and done first.

The previous version was duplicating the 'ret2 == -EAGAIN'. As well 'ret2 != -EAGAIN' gurantees the 'done:' path so it makes sense to move that check to the front before the likely more expensive branches which require memory derefences.

---
Generally I would want to rewrite this as:

```
if (ret2 != -EAGAIN
    || (req->flags & REQ_F_NOWAIT)
    || (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)))
        kiocb_done(kiocb, ret2, issue_flags);
else {
    ...
```

But the style of the file seems to be to use gotos. If the above is prefereable, let me know and I'll post a new version.

 fs/io_uring.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
fs/io_uring.c +6 -3
index d1e672e7a2d1..932fc84d70d3 100644
@@ -3648,12 +3648,15 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags)
 	 */
 	if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
 		ret2 = -EAGAIN;
+
+	if (ret2 != -EAGAIN)
+		goto done;
 	/* no retry on NONBLOCK nor RWF_NOWAIT */
-	if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
+	if (req->flags & REQ_F_NOWAIT)
 		goto done;
-	if (!force_nonblock || ret2 != -EAGAIN) {
+	if (!force_nonblock) {
 		/* IOPOLL retry should happen for io-wq threads */
-		if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
+		if (req->ctx->flags & IORING_SETUP_IOPOLL)
 			goto copy_iov;
 done:
 		kiocb_done(kiocb, ret2, issue_flags);
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Noah Goldstein

This commit reorganizes the branches in the tail of io_write so that the 'ret2 == -EAGAIN' check is not repeated and done first.

The previous version was duplicating the 'ret2 == -EAGAIN'. As well 'ret2 != -EAGAIN' gurantees the 'done:' path so it makes sense to move that check to the front before the likely more expensive branches which require memory derefences.

---
Generally I would want to rewrite this as:

Quoted 8 lines
```
if (ret2 != -EAGAIN
     || (req->flags & REQ_F_NOWAIT)
     || (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)))
         kiocb_done(kiocb, ret2, issue_flags);
else {
     ...
```

To me, this one is clear enough and short, but I think better to:
if (ret2 != -EAGAIN || (req->flags & REQ_F_NOWAIT) ||

    (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))

if the first line doesn't exceed the line limit.

Quoted 30 lines

But the style of the file seems to be to use gotos. If the above is prefereable, let me know and I'll post a new version.

  fs/io_uring.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
fs/io_uring.c +6 -3
index d1e672e7a2d1..932fc84d70d3 100644
@@ -3648,12 +3648,15 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags)
  	 */
  	if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
  		ret2 = -EAGAIN;
+
+	if (ret2 != -EAGAIN)
+		goto done;
  	/* no retry on NONBLOCK nor RWF_NOWAIT */
-	if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
+	if (req->flags & REQ_F_NOWAIT)
  		goto done;
-	if (!force_nonblock || ret2 != -EAGAIN) {
+	if (!force_nonblock) {
  		/* IOPOLL retry should happen for io-wq threads */
-		if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
+		if (req->ctx->flags & IORING_SETUP_IOPOLL)
  			goto copy_iov;
  done:
  		kiocb_done(kiocb, ret2, issue_flags);
#2 lore raw

PATCH v2 1/1 fs/io_uring: Hoist ret2 == -EAGAIN check in tail of io_write

In reply to Noah Goldstein

This commit reorganizes the branches in the tail of io_write so that the 'ret2 == -EAGAIN' check is not repeated and done first.

The previous version was duplicating the 'ret2 == -EAGAIN'. As well 'ret2 != -EAGAIN' gurantees the 'done:' path so it makes sense to move that check to the front before the likely more expensive branches which require memory derefences.

---
 fs/io_uring.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
fs/io_uring.c +7 -9
index d1e672e7a2d1..18293407e8bc 100644
@@ -3648,21 +3648,19 @@ static int io_write(struct io_kiocb *req, unsigned int issue_flags)
 	 */
 	if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
 		ret2 = -EAGAIN;
-	/* no retry on NONBLOCK nor RWF_NOWAIT */
-	if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
-		goto done;
-	if (!force_nonblock || ret2 != -EAGAIN) {
-		/* IOPOLL retry should happen for io-wq threads */
-		if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
-			goto copy_iov;
-done:
+
+	if (ret2 != -EAGAIN ||
+	    /* no retry on NONBLOCK nor RWF_NOWAIT */
+	    (req->flags & REQ_F_NOWAIT)
+	    /* IOPOLL retry should happen for io-wq threads */
+	    || (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))) {
 		kiocb_done(kiocb, ret2, issue_flags);
 	} else {
 copy_iov:
 		iov_iter_restore(&s->iter, &s->iter_state);
 		ret = io_setup_async_rw(req, iovec, s, false);
 		return ret ?: -EAGAIN;
-	}
+}
 out_free:
 	/* it's reportedly faster than delegating the null check to kfree() */
 	if (iovec)
Signature · 1 line
2.29.2