Skip to content

Commit 78076bb

Browse files
committed
io_uring: use hash table for poll command lookups
We recently changed this from a single list to an rbtree, but for some real life workloads, the rbtree slows down the submission/insertion case enough so that it's the top cycle consumer on the io_uring side. In testing, using a hash table is a more well rounded compromise. It is fast for insertion, and as long as it's sized appropriately, it works well for the cancellation case as well. Running TAO with a lot of network sockets, this removes io_poll_req_insert() from spending 2% of the CPU cycles. Reported-by: Dan Melnic <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 08bdcc3 commit 78076bb

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

fs/io_uring.c

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ struct io_ring_ctx {
275275
* manipulate the list, hence no extra locking is needed there.
276276
*/
277277
struct list_head poll_list;
278-
struct rb_root cancel_tree;
278+
struct hlist_head *cancel_hash;
279+
unsigned cancel_hash_bits;
279280

280281
spinlock_t inflight_lock;
281282
struct list_head inflight_list;
@@ -355,7 +356,7 @@ struct io_kiocb {
355356
struct io_ring_ctx *ctx;
356357
union {
357358
struct list_head list;
358-
struct rb_node rb_node;
359+
struct hlist_node hash_node;
359360
};
360361
struct list_head link_list;
361362
unsigned int flags;
@@ -444,6 +445,7 @@ static void io_ring_ctx_ref_free(struct percpu_ref *ref)
444445
static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
445446
{
446447
struct io_ring_ctx *ctx;
448+
int hash_bits;
447449

448450
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
449451
if (!ctx)
@@ -457,6 +459,21 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
457459
if (!ctx->completions)
458460
goto err;
459461

462+
/*
463+
* Use 5 bits less than the max cq entries, that should give us around
464+
* 32 entries per hash list if totally full and uniformly spread.
465+
*/
466+
hash_bits = ilog2(p->cq_entries);
467+
hash_bits -= 5;
468+
if (hash_bits <= 0)
469+
hash_bits = 1;
470+
ctx->cancel_hash_bits = hash_bits;
471+
ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
472+
GFP_KERNEL);
473+
if (!ctx->cancel_hash)
474+
goto err;
475+
__hash_init(ctx->cancel_hash, 1U << hash_bits);
476+
460477
if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
461478
PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
462479
goto err;
@@ -470,7 +487,6 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
470487
init_waitqueue_head(&ctx->wait);
471488
spin_lock_init(&ctx->completion_lock);
472489
INIT_LIST_HEAD(&ctx->poll_list);
473-
ctx->cancel_tree = RB_ROOT;
474490
INIT_LIST_HEAD(&ctx->defer_list);
475491
INIT_LIST_HEAD(&ctx->timeout_list);
476492
init_waitqueue_head(&ctx->inflight_wait);
@@ -481,6 +497,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
481497
if (ctx->fallback_req)
482498
kmem_cache_free(req_cachep, ctx->fallback_req);
483499
kfree(ctx->completions);
500+
kfree(ctx->cancel_hash);
484501
kfree(ctx);
485502
return NULL;
486503
}
@@ -2260,14 +2277,6 @@ static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
22602277
#endif
22612278
}
22622279

2263-
static inline void io_poll_remove_req(struct io_kiocb *req)
2264-
{
2265-
if (!RB_EMPTY_NODE(&req->rb_node)) {
2266-
rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2267-
RB_CLEAR_NODE(&req->rb_node);
2268-
}
2269-
}
2270-
22712280
static void io_poll_remove_one(struct io_kiocb *req)
22722281
{
22732282
struct io_poll_iocb *poll = &req->poll;
@@ -2279,36 +2288,34 @@ static void io_poll_remove_one(struct io_kiocb *req)
22792288
io_queue_async_work(req);
22802289
}
22812290
spin_unlock(&poll->head->lock);
2282-
io_poll_remove_req(req);
2291+
hash_del(&req->hash_node);
22832292
}
22842293

22852294
static void io_poll_remove_all(struct io_ring_ctx *ctx)
22862295
{
2287-
struct rb_node *node;
2296+
struct hlist_node *tmp;
22882297
struct io_kiocb *req;
2298+
int i;
22892299

22902300
spin_lock_irq(&ctx->completion_lock);
2291-
while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2292-
req = rb_entry(node, struct io_kiocb, rb_node);
2293-
io_poll_remove_one(req);
2301+
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2302+
struct hlist_head *list;
2303+
2304+
list = &ctx->cancel_hash[i];
2305+
hlist_for_each_entry_safe(req, tmp, list, hash_node)
2306+
io_poll_remove_one(req);
22942307
}
22952308
spin_unlock_irq(&ctx->completion_lock);
22962309
}
22972310

22982311
static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
22992312
{
2300-
struct rb_node *p, *parent = NULL;
2313+
struct hlist_head *list;
23012314
struct io_kiocb *req;
23022315

2303-
p = ctx->cancel_tree.rb_node;
2304-
while (p) {
2305-
parent = p;
2306-
req = rb_entry(parent, struct io_kiocb, rb_node);
2307-
if (sqe_addr < req->user_data) {
2308-
p = p->rb_left;
2309-
} else if (sqe_addr > req->user_data) {
2310-
p = p->rb_right;
2311-
} else {
2316+
list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2317+
hlist_for_each_entry(req, list, hash_node) {
2318+
if (sqe_addr == req->user_data) {
23122319
io_poll_remove_one(req);
23132320
return 0;
23142321
}
@@ -2390,7 +2397,7 @@ static void io_poll_complete_work(struct io_wq_work **workptr)
23902397
spin_unlock_irq(&ctx->completion_lock);
23912398
return;
23922399
}
2393-
io_poll_remove_req(req);
2400+
hash_del(&req->hash_node);
23942401
io_poll_complete(req, mask, ret);
23952402
spin_unlock_irq(&ctx->completion_lock);
23962403

@@ -2425,7 +2432,7 @@ static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
24252432
* for finalizing the request, mark us as having grabbed that already.
24262433
*/
24272434
if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2428-
io_poll_remove_req(req);
2435+
hash_del(&req->hash_node);
24292436
io_poll_complete(req, mask, 0);
24302437
req->flags |= REQ_F_COMP_LOCKED;
24312438
io_put_req(req);
@@ -2463,20 +2470,10 @@ static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
24632470
static void io_poll_req_insert(struct io_kiocb *req)
24642471
{
24652472
struct io_ring_ctx *ctx = req->ctx;
2466-
struct rb_node **p = &ctx->cancel_tree.rb_node;
2467-
struct rb_node *parent = NULL;
2468-
struct io_kiocb *tmp;
2469-
2470-
while (*p) {
2471-
parent = *p;
2472-
tmp = rb_entry(parent, struct io_kiocb, rb_node);
2473-
if (req->user_data < tmp->user_data)
2474-
p = &(*p)->rb_left;
2475-
else
2476-
p = &(*p)->rb_right;
2477-
}
2478-
rb_link_node(&req->rb_node, parent, p);
2479-
rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2473+
struct hlist_head *list;
2474+
2475+
list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2476+
hlist_add_head(&req->hash_node, list);
24802477
}
24812478

24822479
static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
@@ -2504,7 +2501,7 @@ static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
25042501
INIT_IO_WORK(&req->work, io_poll_complete_work);
25052502
events = READ_ONCE(sqe->poll_events);
25062503
poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2507-
RB_CLEAR_NODE(&req->rb_node);
2504+
INIT_HLIST_NODE(&req->hash_node);
25082505

25092506
poll->head = NULL;
25102507
poll->done = false;
@@ -4644,6 +4641,7 @@ static void io_ring_ctx_free(struct io_ring_ctx *ctx)
46444641
free_uid(ctx->user);
46454642
put_cred(ctx->creds);
46464643
kfree(ctx->completions);
4644+
kfree(ctx->cancel_hash);
46474645
kmem_cache_free(req_cachep, ctx->fallback_req);
46484646
kfree(ctx);
46494647
}

0 commit comments

Comments
 (0)