io-uring.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: Jens Axboe <axboe@kernel.dk>,
	io-uring@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Tejun Heo <tj@kernel.org>, Dennis Zhou <dennis@kernel.org>,
	Christoph Lameter <cl@linux.com>
Subject: [PATCH RFC v2 3/3] io_uring: batch get(ctx->ref) across submits
Date: Sat, 21 Dec 2019 19:15:09 +0300	[thread overview]
Message-ID: <925d8fe5406779bbfa108caa3d1f9fd16e3434b5.1576944502.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1576944502.git.asml.silence@gmail.com>

Double account ctx->refs keeping number of taken refs in ctx. As
io_uring gets per-request ctx->refs during submission, while holding
ctx->uring_lock, this allows in most of the time to bypass
percpu_ref_get*() and its overhead.

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

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 5392134f042f..eef09de94609 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -84,6 +84,9 @@
 #define IORING_MAX_ENTRIES	32768
 #define IORING_MAX_CQ_ENTRIES	(2 * IORING_MAX_ENTRIES)
 
+/* Not less than IORING_MAX_ENTRIES, so can grab once per submission loop */
+#define IORING_REFS_THRESHOLD	IORING_MAX_ENTRIES
+
 /*
  * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
  */
@@ -197,6 +200,7 @@ struct fixed_file_data {
 struct io_ring_ctx {
 	struct {
 		struct percpu_ref	refs;
+		unsigned long		taken_refs; /* used under @uring_lock */
 	} ____cacheline_aligned_in_smp;
 
 	struct {
@@ -690,6 +694,13 @@ static void io_ring_ctx_ref_free(struct percpu_ref *ref)
 	complete(&ctx->completions[0]);
 }
 
+static void io_free_taken_refs(struct io_ring_ctx *ctx)
+{
+	if (ctx->taken_refs)
+		percpu_ref_put_many(&ctx->refs, ctx->taken_refs);
+	ctx->taken_refs = 0;
+}
+
 static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
 {
 	struct io_ring_ctx *ctx;
@@ -4388,7 +4399,6 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
 	struct io_submit_state state, *statep = NULL;
 	struct io_kiocb *link = NULL;
 	int i, submitted = 0;
-	unsigned int extra_refs;
 	bool mm_fault = false;
 
 	/* if we have a backlog and couldn't flush it all, return BUSY */
@@ -4398,9 +4408,15 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
 			return -EBUSY;
 	}
 
-	if (!percpu_ref_tryget_many(&ctx->refs, nr))
-		return -EAGAIN;
-	extra_refs = nr;
+	if (ctx->taken_refs < IORING_REFS_THRESHOLD) {
+		if (unlikely(percpu_ref_is_dying(&ctx->refs))) {
+			io_free_taken_refs(ctx);
+			return -ENXIO;
+		}
+		if (!percpu_ref_tryget_many(&ctx->refs, IORING_REFS_THRESHOLD))
+			return -EAGAIN;
+		ctx->taken_refs += IORING_REFS_THRESHOLD;
+	}
 
 	if (nr > IO_PLUG_THRESHOLD) {
 		io_submit_state_start(&state, nr);
@@ -4417,8 +4433,9 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
 				submitted = -EAGAIN;
 			break;
 		}
-		--extra_refs;
 		if (!io_get_sqring(ctx, req, &sqe)) {
+			/* not submitted, but a ref is freed */
+			ctx->taken_refs--;
 			__io_free_req(req);
 			break;
 		}
@@ -4454,8 +4471,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
 		io_queue_link_head(link);
 	if (statep)
 		io_submit_state_end(&state);
-	if (extra_refs)
-		percpu_ref_put_many(&ctx->refs, extra_refs);
+	ctx->taken_refs -= submitted;
 
 	 /* Commit SQ ring head once we've consumed and submitted all SQEs */
 	io_commit_sqring(ctx);
@@ -5731,6 +5747,7 @@ static int io_uring_fasync(int fd, struct file *file, int on)
 static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
 {
 	mutex_lock(&ctx->uring_lock);
+	io_free_taken_refs(ctx);
 	percpu_ref_kill(&ctx->refs);
 	mutex_unlock(&ctx->uring_lock);
 
@@ -6196,6 +6213,7 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
 
 	if (opcode != IORING_UNREGISTER_FILES &&
 	    opcode != IORING_REGISTER_FILES_UPDATE) {
+		io_free_taken_refs(ctx);
 		percpu_ref_kill(&ctx->refs);
 
 		/*
-- 
2.24.0


  parent reply	other threads:[~2019-12-21 16:15 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-17 22:28 [PATCH 0/2] optimise ctx's refs grabbing in io_uring Pavel Begunkov
2019-12-17 22:28 ` [PATCH 1/2] pcpu_ref: add percpu_ref_tryget_many() Pavel Begunkov
2019-12-17 23:42   ` Jens Axboe
2019-12-18 16:26     ` Tejun Heo
2019-12-18 17:49       ` Dennis Zhou
2019-12-21 15:36         ` Pavel Begunkov
2019-12-17 22:28 ` [PATCH 2/2] io_uring: batch getting pcpu references Pavel Begunkov
2019-12-17 23:21   ` Jens Axboe
2019-12-17 23:31     ` Jens Axboe
2019-12-18  9:25       ` Pavel Begunkov
2019-12-18  9:23     ` Pavel Begunkov
2019-12-18  0:02   ` Jens Axboe
2019-12-18 10:41     ` Pavel Begunkov
2019-12-21 16:15   ` [PATCH v2 0/3] optimise ctx's refs grabbing in io_uring Pavel Begunkov
2019-12-21 16:15     ` [PATCH v2 1/3] pcpu_ref: add percpu_ref_tryget_many() Pavel Begunkov
2019-12-21 16:15     ` [PATCH v2 2/3] io_uring: batch getting pcpu references Pavel Begunkov
2019-12-21 16:15     ` Pavel Begunkov [this message]
2019-12-21 16:20       ` [PATCH RFC v2 3/3] io_uring: batch get(ctx->ref) across submits Pavel Begunkov
2019-12-21 16:38         ` Jens Axboe
2019-12-21 16:48           ` Pavel Begunkov
2019-12-21 17:01             ` Jens Axboe
2019-12-21 17:26               ` Pavel Begunkov
2019-12-21 20:12       ` [PATCH v3 0/2] optimise ctx's refs grabbing in io_uring Pavel Begunkov
2019-12-21 20:12         ` [PATCH v3 1/2] pcpu_ref: add percpu_ref_tryget_many() Pavel Begunkov
2019-12-21 20:12         ` [PATCH v3 2/2] io_uring: batch getting pcpu references Pavel Begunkov
2019-12-21 21:56           ` Pavel Begunkov
2019-12-28 11:13         ` [PATCH v4 0/2] optimise ctx's refs grabbing in io_uring Pavel Begunkov
2019-12-28 11:13           ` [PATCH v4 1/2] pcpu_ref: add percpu_ref_tryget_many() Pavel Begunkov
2019-12-28 11:13           ` [PATCH v4 2/2] io_uring: batch getting pcpu references Pavel Begunkov
2019-12-28 11:15             ` Pavel Begunkov
2019-12-28 17:03               ` Jens Axboe
2019-12-28 18:37                 ` Pavel Begunkov
2019-12-30  3:33                   ` Brian Gianforcaro
2019-12-30 18:45                     ` 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=925d8fe5406779bbfa108caa3d1f9fd16e3434b5.1576944502.git.asml.silence@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=cl@linux.com \
    --cc=dennis@kernel.org \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).