public inbox for [email protected]
 help / color / mirror / Atom feed
From: Pavel Begunkov <[email protected]>
To: [email protected]
Cc: Jens Axboe <[email protected]>,
	[email protected], [email protected]
Subject: [PATCH v2 12/13] io_uring/rsrc: optimise io_rsrc_data refcounting
Date: Tue,  4 Apr 2023 13:39:56 +0100	[thread overview]
Message-ID: <e73c3d6820cf679532696d790b5b8fae23537213.1680576071.git.asml.silence@gmail.com> (raw)
In-Reply-To: <[email protected]>

Every struct io_rsrc_node takes a struct io_rsrc_data reference, which
means all rsrc updates do 2 extra atomics. Replace atomics refcounting
with a int as it's all done under ->uring_lock.

Signed-off-by: Pavel Begunkov <[email protected]>
---
 io_uring/rsrc.c | 30 ++++++++++++++++++------------
 io_uring/rsrc.h |  2 +-
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 95edc5f73204..74e13230fa0c 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -31,6 +31,11 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
 #define IORING_MAX_FIXED_FILES	(1U << 20)
 #define IORING_MAX_REG_BUFFERS	(1U << 14)
 
+static inline bool io_put_rsrc_data_ref(struct io_rsrc_data *rsrc_data)
+{
+	return !--rsrc_data->refs;
+}
+
 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
 {
 	unsigned long page_limit, cur_pages, new_pages;
@@ -165,13 +170,13 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
 	}
 
 	io_rsrc_node_destroy(rsrc_data->ctx, ref_node);
-	if (atomic_dec_and_test(&rsrc_data->refs))
+	if (io_put_rsrc_data_ref(rsrc_data))
 		complete(&rsrc_data->done);
 }
 
 void io_wait_rsrc_data(struct io_rsrc_data *data)
 {
-	if (data && !atomic_dec_and_test(&data->refs))
+	if (data && !io_put_rsrc_data_ref(data))
 		wait_for_completion(&data->done);
 }
 
@@ -234,7 +239,7 @@ void io_rsrc_node_switch(struct io_ring_ctx *ctx,
 		rsrc_node->rsrc_data = data_to_kill;
 		list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
 
-		atomic_inc(&data_to_kill->refs);
+		data_to_kill->refs++;
 		/* put master ref */
 		io_put_rsrc_node(ctx, rsrc_node);
 		ctx->rsrc_node = NULL;
@@ -267,8 +272,8 @@ __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
 		return ret;
 	io_rsrc_node_switch(ctx, data);
 
-	/* kill initial ref, already quiesced if zero */
-	if (atomic_dec_and_test(&data->refs))
+	/* kill initial ref */
+	if (io_put_rsrc_data_ref(data))
 		return 0;
 
 	data->quiesce = true;
@@ -276,17 +281,19 @@ __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
 	do {
 		ret = io_run_task_work_sig(ctx);
 		if (ret < 0) {
-			atomic_inc(&data->refs);
-			/* wait for all works potentially completing data->done */
-			reinit_completion(&data->done);
 			mutex_lock(&ctx->uring_lock);
+			if (!data->refs) {
+				ret = 0;
+			} else {
+				/* restore the master reference */
+				data->refs++;
+			}
 			break;
 		}
-
 		ret = wait_for_completion_interruptible(&data->done);
 		if (!ret) {
 			mutex_lock(&ctx->uring_lock);
-			if (atomic_read(&data->refs) <= 0)
+			if (!data->refs)
 				break;
 			/*
 			 * it has been revived by another thread while
@@ -361,6 +368,7 @@ __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
 	data->nr = nr;
 	data->ctx = ctx;
 	data->do_put = do_put;
+	data->refs = 1;
 	if (utags) {
 		ret = -EFAULT;
 		for (i = 0; i < nr; i++) {
@@ -371,8 +379,6 @@ __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
 				goto fail;
 		}
 	}
-
-	atomic_set(&data->refs, 1);
 	init_completion(&data->done);
 	*pdata = data;
 	return 0;
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index cf24c3fd701f..7ab9b2b2e757 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -33,8 +33,8 @@ struct io_rsrc_data {
 	u64				**tags;
 	unsigned int			nr;
 	rsrc_put_fn			*do_put;
-	atomic_t			refs;
 	struct completion		done;
+	int				refs;
 	bool				quiesce;
 };
 
-- 
2.39.1


  parent reply	other threads:[~2023-04-04 12:41 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-04 12:39 [PATCH v2 00/13] optimise registered buffer/file updates Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 01/13] io_uring/rsrc: use non-pcpu refcounts for nodes Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 02/13] io_uring/rsrc: keep cached refs per node Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 03/13] io_uring: don't put nodes under spinlocks Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 04/13] io_uring: io_free_req() via tw Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 05/13] io_uring/rsrc: protect node refs with uring_lock Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 06/13] io_uring/rsrc: kill rsrc_ref_lock Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 07/13] io_uring/rsrc: rename rsrc_list Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 08/13] io_uring/rsrc: optimise io_rsrc_put allocation Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 09/13] io_uring/rsrc: don't offload node free Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 10/13] io_uring/rsrc: cache struct io_rsrc_node Pavel Begunkov
2023-04-04 12:39 ` [PATCH v2 11/13] io_uring/rsrc: add lockdep sanity checks Pavel Begunkov
2023-04-04 12:39 ` Pavel Begunkov [this message]
2023-04-04 12:39 ` [PATCH v2 13/13] io_uring/rsrc: add custom limit for node caching Pavel Begunkov
2023-04-04 15:30 ` [PATCH v2 00/13] optimise registered buffer/file updates Jens Axboe
2023-04-04 15:33 ` Jens Axboe

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=e73c3d6820cf679532696d790b5b8fae23537213.1680576071.git.asml.silence@gmail.com \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /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