All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: Jens Axboe <axboe@kernel.dk>, io-uring@vger.kernel.org
Subject: [PATCH 03/23] io_uring: refactor io_alloc_req
Date: Thu, 29 Jul 2021 16:05:30 +0100	[thread overview]
Message-ID: <7dee2dfb97c69726d5998779cddb76fdf6ccc3c9.1627570633.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1627570633.git.asml.silence@gmail.com>

Replace the main if of io_flush_cached_reqs() with inverted condition +
goto, so all the cases are handled in the same way. And also extract
io_preinit_req() to make it cleaner and easier to refer to.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 fs/io_uring.c | 66 +++++++++++++++++++++++++--------------------------
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 2c4a61153bd1..6dca33fdb012 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -1690,6 +1690,19 @@ static void io_req_complete_failed(struct io_kiocb *req, long res)
 	io_req_complete_post(req, res, 0);
 }
 
+/*
+ * Don't initialise the fields below on every allocation, but do that in
+ * advance and keep them valid across allocations.
+ */
+static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
+{
+	req->ctx = ctx;
+	req->link = NULL;
+	req->async_data = NULL;
+	/* not necessary, but safer to zero */
+	req->result = 0;
+}
+
 static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
 					struct io_comp_state *cs)
 {
@@ -1732,45 +1745,31 @@ static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
 static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
 {
 	struct io_submit_state *state = &ctx->submit_state;
+	gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
+	int ret, i;
 
 	BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
 
-	if (!state->free_reqs) {
-		gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
-		int ret, i;
-
-		if (io_flush_cached_reqs(ctx))
-			goto got_req;
-
-		ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
-					    state->reqs);
-
-		/*
-		 * Bulk alloc is all-or-nothing. If we fail to get a batch,
-		 * retry single alloc to be on the safe side.
-		 */
-		if (unlikely(ret <= 0)) {
-			state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
-			if (!state->reqs[0])
-				return NULL;
-			ret = 1;
-		}
+	if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
+		goto got_req;
 
-		/*
-		 * Don't initialise the fields below on every allocation, but
-		 * do that in advance and keep valid on free.
-		 */
-		for (i = 0; i < ret; i++) {
-			struct io_kiocb *req = state->reqs[i];
+	ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
+				    state->reqs);
 
-			req->ctx = ctx;
-			req->link = NULL;
-			req->async_data = NULL;
-			/* not necessary, but safer to zero */
-			req->result = 0;
-		}
-		state->free_reqs = ret;
+	/*
+	 * Bulk alloc is all-or-nothing. If we fail to get a batch,
+	 * retry single alloc to be on the safe side.
+	 */
+	if (unlikely(ret <= 0)) {
+		state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
+		if (!state->reqs[0])
+			return NULL;
+		ret = 1;
 	}
+
+	for (i = 0; i < ret; i++)
+		io_preinit_req(state->reqs[i], ctx);
+	state->free_reqs = ret;
 got_req:
 	state->free_reqs--;
 	return state->reqs[state->free_reqs];
@@ -6546,6 +6545,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
 	unsigned int sqe_flags;
 	int personality, ret = 0;
 
+	/* req is partially pre-initialised, see io_preinit_req() */
 	req->opcode = READ_ONCE(sqe->opcode);
 	/* same numerical values with corresponding REQ_F_*, safe to copy */
 	req->flags = sqe_flags = READ_ONCE(sqe->flags);
-- 
2.32.0


  parent reply	other threads:[~2021-07-29 15:06 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-29 15:05 [PATCH for-next 00/23] first 5.15 batch Pavel Begunkov
2021-07-29 15:05 ` [PATCH 01/23] io_uring: remove unnecessary PF_EXITING check Pavel Begunkov
2021-07-29 15:05 ` [PATCH 02/23] io-wq: improve wq_list_add_tail() Pavel Begunkov
2021-07-29 15:05 ` Pavel Begunkov [this message]
2021-07-29 15:05 ` [PATCH 04/23] io_uring: don't halt iopoll too early Pavel Begunkov
2021-07-29 15:05 ` [PATCH 05/23] io_uring: add more locking annotations for submit Pavel Begunkov
2021-07-29 15:05 ` [PATCH 06/23] io_uring: optimise io_cqring_wait() hot path Pavel Begunkov
2021-07-29 15:05 ` [PATCH 07/23] io_uring: extract a helper for ctx quiesce Pavel Begunkov
2021-07-29 15:05 ` [PATCH 08/23] io_uring: move io_put_task() definition Pavel Begunkov
2021-07-29 15:05 ` [PATCH 09/23] io_uring: move io_rsrc_node_alloc() definition Pavel Begunkov
2021-07-29 15:05 ` [PATCH 10/23] io_uring: inline io_free_req_deferred Pavel Begunkov
2021-07-29 15:05 ` [PATCH 11/23] io_uring: deduplicate open iopoll check Pavel Begunkov
2021-07-29 15:05 ` [PATCH 12/23] io_uring: improve ctx hang handling Pavel Begunkov
2021-07-29 15:05 ` [PATCH 13/23] io_uring: kill unused IO_IOPOLL_BATCH Pavel Begunkov
2021-07-29 15:05 ` [PATCH 14/23] io_uring: drop exec checks from io_req_task_submit Pavel Begunkov
2021-07-29 15:05 ` [PATCH 15/23] io_uring: optimise putting task struct Pavel Begunkov
2021-07-29 15:05 ` [PATCH 16/23] io_uring: hide async dadta behind flags Pavel Begunkov
2021-07-29 15:05 ` [PATCH 17/23] io_uring: move io_fallback_req_func() Pavel Begunkov
2021-07-29 15:05 ` [PATCH 18/23] io_uring: cache __io_free_req()'d requests Pavel Begunkov
2021-07-29 15:05 ` [PATCH 19/23] io_uring: use inflight_entry instead of compl.list Pavel Begunkov
2021-07-29 15:05 ` [PATCH 20/23] io_uring: remove redundant args from cache_free Pavel Begunkov
2021-07-29 15:05 ` [PATCH 21/23] io_uring: inline struct io_comp_state Pavel Begunkov
2021-07-29 15:05 ` [PATCH 22/23] io_uring: remove extra argument for overflow flush Pavel Begunkov
2021-07-29 15:05 ` [PATCH 23/23] io_uring: inline io_poll_remove_waitqs Pavel Begunkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7dee2dfb97c69726d5998779cddb76fdf6ccc3c9.1627570633.git.asml.silence@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.