Skip to contentSkip to conversation

cancel_hash per entry lock

PATCH In review Hao Xu 2 participants 11 messages Jun 6, 2022 io_uring

lore
Share & tools
lore

Hao Xu

author
#0 lore raw

v2 cover cancel_hash per entry lock

From: Hao Xu <[email protected]>

Make per entry lock for cancel_hash array, this reduces usage of completion_lock and contension between cancel_hash entries.

v1->v2:
- Add per entry lock for poll/apoll task work code which was missed in v1
- add an member in io_kiocb to track req's indice in cancel_hash

Hao Xu (3):
io_uring: add hash_index and its logic to track req in cancel_hash
io_uring: add an io_hash_bucket structure for smaller granularity lock
io_uring: switch cancel_hash to use per list spinlock

 io_uring/cancel.c         | 15 +++++++--
 io_uring/cancel.h         |  6 ++++
 io_uring/fdinfo.c         |  9 ++++--
 io_uring/io_uring.c       |  8 +++--
 io_uring/io_uring_types.h |  3 +-
 io_uring/poll.c           | 64 +++++++++++++++++++++------------------
 6 files changed, 67 insertions(+), 38 deletions(-)

base-commit: d8271bf021438f468dab3cd84fe5279b5bbcead8

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/3 io_uring: add hash_index and its logic to track req in cancel_hash

From: Hao Xu <[email protected]>

Add a new member hash_index in struct io_kiocb to track the req index in cancel_hash array. This is needed in later patches.

---
 io_uring/io_uring_types.h | 1 +
 io_uring/poll.c           | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
io_uring/io_uring_types.h +1 -0
index 7c22cf35a7e2..2041ee83467d 100644
@@ -474,6 +474,7 @@ struct io_kiocb {
 			u64		extra2;
 		};
 	};
+	unsigned int			hash_index;
 	/* internal polling, see IORING_FEAT_FAST_POLL */
 	struct async_poll		*apoll;
 	/* opcode allocated if it needs to store data for async defer */
io_uring/poll.c +3 -1
index 0df5eca93b16..95e28f32b49c 100644
@@ -74,8 +74,10 @@ static void io_poll_req_insert(struct io_kiocb *req)
 {
 	struct io_ring_ctx *ctx = req->ctx;
 	struct hlist_head *list;
+	u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
 
-	list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)];
+	req->hash_index = index;
+	list = &ctx->cancel_hash[index];
 	hlist_add_head(&req->hash_node, list);
 }
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/3] io_uring: add hash_index and its logic to track req in cancel_hash

Quoted 16 lines · Hao Xu

From: Hao Xu <[email protected]>

Add a new member hash_index in struct io_kiocb to track the req index in cancel_hash array. This is needed in later patches.

---
  io_uring/io_uring_types.h | 1 +
  io_uring/poll.c           | 4 +++-
  2 files changed, 4 insertions(+), 1 deletion(-)
io_uring/io_uring_types.h +0 -0
index 7c22cf35a7e2..2041ee83467d 100644

io_uring/io_uring_types.h struct io_kiocb { · line 477

Quoted 4 lines
io_uring/io_uring_types.h +1 -0
@@ -474,6 +474,7 @@ struct io_kiocb {
  			u64		extra2;
  		};
  	};
+	unsigned int			hash_index;

Didn't take a closer look, but can we make rid of it? E.g. computing it again when ejecting a request from the hash? or keep it in struct io_poll?

Quoted 19 lines
io_uring/io_uring_types.h +0 -0
  	/* internal polling, see IORING_FEAT_FAST_POLL */
  	struct async_poll		*apoll;
  	/* opcode allocated if it needs to store data for async defer */
io_uring/poll.c +3 -1
index 0df5eca93b16..95e28f32b49c 100644
@@ -74,8 +74,10 @@ static void io_poll_req_insert(struct io_kiocb *req)
  {
  	struct io_ring_ctx *ctx = req->ctx;
  	struct hlist_head *list;
+	u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
 
-	list = &ctx->cancel_hash[hash_long(req->cqe.user_data, ctx->cancel_hash_bits)];
+	req->hash_index = index;
+	list = &ctx->cancel_hash[index];
  	hlist_add_head(&req->hash_node, list);
  }
 
Signature · 1 line
Pavel Begunkov
author
#3 lore raw

Re: [PATCH 1/3] io_uring: add hash_index and its logic to track req in cancel_hash

Quoted 12 lines · Pavel Begunkov
Quoted 11 lines · Hao Xu

From: Hao Xu <[email protected]>

Add a new member hash_index in struct io_kiocb to track the req index in cancel_hash array. This is needed in later patches.

---
  io_uring/io_uring_types.h | 1 +
  io_uring/poll.c           | 4 +++-

  2 files changed, 4 insertions(+), 1 deletion(-)
Quoted 13 lines

io_uring/io_uring_types.h struct io_kiocb { · line 477

Quoted 9 lines
io_uring/io_uring_types.h +1 -0
index 7c22cf35a7e2..2041ee83467d 100644
@@ -474,6 +474,7 @@ struct io_kiocb {
               u64        extra2;
           };
       };
+    unsigned int            hash_index;

Didn't take a closer look, but can we make rid of it? E.g. computing it again when ejecting a request from the hash? or keep it in struct io_poll?

Good point, I prefer moving it to io_poll to computing it again since this patchset is to try to make it faster.

author
#4 lore raw

PATCH v2 2/3 io_uring: add an io_hash_bucket structure for smaller granularity lock

In reply to Hao Xu

From: Hao Xu <[email protected]>

Add a new io_hash_bucket structure so that each bucket in cancel_hash has separate spinlock. This is a prep patch for later use.

---
 io_uring/cancel.h | 5 +++++
 1 file changed, 5 insertions(+)
io_uring/cancel.h +5 -0
index 4f35d8696325..b9218310611c 100644
@@ -4,3 +4,8 @@ int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
 int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags);
 
 int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd);
+
+struct io_hash_bucket {
+	spinlock_t		lock;
+	struct hlist_head	list;
+};
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH 2/3] io_uring: add an io_hash_bucket structure for smaller granularity lock

Quoted 15 lines · Hao Xu

From: Hao Xu <[email protected]>

Add a new io_hash_bucket structure so that each bucket in cancel_hash has separate spinlock. This is a prep patch for later use.

---
  io_uring/cancel.h | 5 +++++
  1 file changed, 5 insertions(+)
io_uring/cancel.h +0 -0
index 4f35d8696325..b9218310611c 100644

io_uring/cancel.h int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); · line 11

Quoted 8 lines
io_uring/cancel.h +5 -0
@@ -4,3 +4,8 @@ int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
  int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags);
 
  int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd);
+
+struct io_hash_bucket {
+	spinlock_t		lock;
+	struct hlist_head	list;
+};

please, in future just merge such patches into the next one, separately it doesn't do anything meaningful, the struct is not used here and IMHO only makes reviewing harder.

Signature · 1 line
Pavel Begunkov
author
#6 lore raw

PATCH v2 3/3 io_uring: switch cancel_hash to use per list spinlock

In reply to Hao Xu

From: Hao Xu <[email protected]>

Use per list lock for cancel_hash, this removes some completion lock invocation and remove contension between different cancel_hash entries

---
 io_uring/cancel.c         | 15 ++++++++--
 io_uring/cancel.h         |  1 +
 io_uring/fdinfo.c         |  9 ++++--
 io_uring/io_uring.c       |  8 +++--
 io_uring/io_uring_types.h |  2 +-
 io_uring/poll.c           | 62 +++++++++++++++++++++------------------
 6 files changed, 59 insertions(+), 38 deletions(-)
io_uring/cancel.c +13 -2
index 83cceb52d82d..c3e5b8058b0d 100644
@@ -6,6 +6,7 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/namei.h>
+#include <linux/list.h>
 #include <linux/io_uring.h>
 
 #include <uapi/linux/io_uring.h>
@@ -93,14 +94,14 @@ int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd)
 	if (!ret)
 		return 0;
 
-	spin_lock(&ctx->completion_lock);
 	ret = io_poll_cancel(ctx, cd);
 	if (ret != -ENOENT)
 		goto out;
+	spin_lock(&ctx->completion_lock);
 	if (!(cd->flags & IORING_ASYNC_CANCEL_FD))
 		ret = io_timeout_cancel(ctx, cd);
-out:
 	spin_unlock(&ctx->completion_lock);
+out:
 	return ret;
 }
 
@@ -192,3 +193,13 @@ int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
 	io_req_set_res(req, ret, 0);
 	return IOU_OK;
 }
+
+inline void init_hash_table(struct io_hash_bucket *hash_table, unsigned size)
+{
+	unsigned int i;
+
+	for (i = 0; i < size; i++) {
+		spin_lock_init(&hash_table[i].lock);
+		INIT_HLIST_HEAD(&hash_table[i].list);
+	}
+}
io_uring/cancel.h +1 -0
index b9218310611c..f682e9811e68 100644
@@ -4,6 +4,7 @@ int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
 int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags);
 
 int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd);
+inline void init_hash_table(struct io_hash_bucket *hash_table, unsigned size);
 
 struct io_hash_bucket {
 	spinlock_t		lock;
io_uring/fdinfo.c +6 -3
index fcedde4b4b1e..f941c73f5502 100644
@@ -13,6 +13,7 @@
 #include "io_uring.h"
 #include "sqpoll.h"
 #include "fdinfo.h"
+#include "cancel.h"
 
 #ifdef CONFIG_PROC_FS
 static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
@@ -157,17 +158,19 @@ static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
 		mutex_unlock(&ctx->uring_lock);
 
 	seq_puts(m, "PollList:\n");
-	spin_lock(&ctx->completion_lock);
 	for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
-		struct hlist_head *list = &ctx->cancel_hash[i];
+		struct io_hash_bucket *hb = &ctx->cancel_hash[i];
 		struct io_kiocb *req;
 
-		hlist_for_each_entry(req, list, hash_node)
+		spin_lock(&hb->lock);
+		hlist_for_each_entry(req, &hb->list, hash_node)
 			seq_printf(m, "  op=%d, task_works=%d\n", req->opcode,
 					task_work_pending(req->task));
+		spin_unlock(&hb->lock);
 	}
 
 	seq_puts(m, "CqOverflowList:\n");
+	spin_lock(&ctx->completion_lock);
 	list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
 		struct io_uring_cqe *cqe = &ocqe->cqe;
 
io_uring/io_uring.c +5 -3
index 1572ebe3cff1..b67ab76b9e56 100644
@@ -725,11 +725,13 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
 	if (hash_bits <= 0)
 		hash_bits = 1;
 	ctx->cancel_hash_bits = hash_bits;
-	ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
-					GFP_KERNEL);
+	ctx->cancel_hash =
+		kmalloc((1U << hash_bits) * sizeof(struct io_hash_bucket),
+			GFP_KERNEL);
 	if (!ctx->cancel_hash)
 		goto err;
-	__hash_init(ctx->cancel_hash, 1U << hash_bits);
+
+	init_hash_table(ctx->cancel_hash, 1U << hash_bits);
 
 	ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
 	if (!ctx->dummy_ubuf)
io_uring/io_uring_types.h +1 -1
index 2041ee83467d..59231f7345ac 100644
@@ -230,7 +230,7 @@ struct io_ring_ctx {
 		 * manipulate the list, hence no extra locking is needed there.
 		 */
 		struct io_wq_work_list	iopoll_list;
-		struct hlist_head	*cancel_hash;
+		struct io_hash_bucket	*cancel_hash;
 		unsigned		cancel_hash_bits;
 		bool			poll_multi_queue;
 
io_uring/poll.c +33 -29

177 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#7 lore raw
In reply to Hao Xu
Quoted 9 lines · Hao Xu

From: Hao Xu <[email protected]>

Make per entry lock for cancel_hash array, this reduces usage of completion_lock and contension between cancel_hash entries.

v1->v2:
- Add per entry lock for poll/apoll task work code which was missed in v1
- add an member in io_kiocb to track req's indice in cancel_hash

Tried to test it with many poll_add IOSQQE_ASYNC requests but turned out that there is little conpletion_lock contention, so no visible change in data. But I still think this may be good for cancel_hash access in some real cases where completion lock matters.

Regards,
Hao

#8 lore raw
Quoted 11 lines · Hao Xu
Quoted 9 lines · Hao Xu

From: Hao Xu <[email protected]>

Make per entry lock for cancel_hash array, this reduces usage of completion_lock and contension between cancel_hash entries.

v1->v2:
  - Add per entry lock for poll/apoll task work code which was missed in v1
  - add an member in io_kiocb to track req's indice in cancel_hash

Quoted 4 lines

Tried to test it with many poll_add IOSQQE_ASYNC requests but turned out that there is little conpletion_lock contention, so no visible change in data. But I still think this may be good for cancel_hash access in some real cases where completion lock matters.

Conceptually I don't mind it, but let me ask in what circumstances you expect it to make a difference? And what can we do to get favourable numbers? For instance, how many CPUs io-wq was using?

Signature · 1 line
Pavel Begunkov
#9 lore raw
Quoted 17 lines · Pavel Begunkov
Quoted 11 lines · Hao Xu
Quoted 9 lines · Hao Xu

From: Hao Xu <[email protected]>

Make per entry lock for cancel_hash array, this reduces usage of completion_lock and contension between cancel_hash entries.

v1->v2:
  - Add per entry lock for poll/apoll task work code which was missed in v1
  - add an member in io_kiocb to track req's indice in cancel_hash

Quoted 4 lines

Tried to test it with many poll_add IOSQQE_ASYNC requests but turned out that there is little conpletion_lock contention, so no visible change in data. But I still think this may be good for cancel_hash access in some real cases where completion lock matters.

Quoted 4 lines

Conceptually I don't mind it, but let me ask in what circumstances you expect it to make a difference? And what can we do to get favourable numbers? For instance, how many CPUs io-wq was using?

Btw, I couldn't find ____cacheline_aligned_in_smp anywhere, which I expect around those new spinlocks to avoid them sharing cache lines

Signature · 1 line
Pavel Begunkov
author
#10 lore raw
In reply to Pavel Begunkov
Quoted 12 lines · Pavel Begunkov
Quoted 11 lines · Hao Xu
Quoted 9 lines · Hao Xu

From: Hao Xu <[email protected]>

Make per entry lock for cancel_hash array, this reduces usage of completion_lock and contension between cancel_hash entries.

v1->v2:
  - Add per entry lock for poll/apoll task work code which was missed in v1
  - add an member in io_kiocb to track req's indice in cancel_hash

Quoted 7 lines
Quoted 4 lines

Tried to test it with many poll_add IOSQQE_ASYNC requests but turned out that there is little conpletion_lock contention, so no visible change in data. But I still think this may be good for cancel_hash access in some real cases where completion lock matters.

Conceptually I don't mind it, but let me ask in what circumstances you expect it to make a difference? And

I suppose there are cases where a bunch of users trying to access cancel_hash[] at the same time when people use multiple threads to submit sqes or they use IOSQE_ASYNC. And these io-workers or task works run parallel on different CPUs.

Quoted 2 lines

what can we do to get favourable numbers? For instance, how many CPUs io-wq was using?

It is not easy to construct manually since it is related with task scheduling, like if we just issue many IOSQE_ASYNC polls in an idle machine with many CPUs, there won't be much contention because of different thread start time(thus they access cancel_hash at different time

Quoted 1 line