Skip to content

Commit e6f89be

Browse files
isilenceaxboe
authored andcommitted
io_uring: introduce a struct for hash table
Instead of passing around a pointer to hash buckets, add a bit of type safety and wrap it into a structure. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/d65bc3faba537ec2aca9eabf334394936d44bd28.1655371007.git.asml.silence@gmail.com Reviewed-by: Hao Xu <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent a2cdd51 commit e6f89be

File tree

6 files changed

+56
-43
lines changed

6 files changed

+56
-43
lines changed

io_uring/cancel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
193193
return IOU_OK;
194194
}
195195

196-
void init_hash_table(struct io_hash_bucket *hash_table, unsigned size)
196+
void init_hash_table(struct io_hash_table *table, unsigned size)
197197
{
198198
unsigned int i;
199199

200200
for (i = 0; i < size; i++) {
201-
spin_lock_init(&hash_table[i].lock);
202-
INIT_HLIST_HEAD(&hash_table[i].list);
201+
spin_lock_init(&table->hbs[i].lock);
202+
INIT_HLIST_HEAD(&table->hbs[i].list);
203203
}
204204
}

io_uring/cancel.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@ int io_async_cancel_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
44
int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags);
55

66
int io_try_cancel(struct io_kiocb *req, struct io_cancel_data *cd);
7-
void init_hash_table(struct io_hash_bucket *hash_table, unsigned size);
8-
9-
struct io_hash_bucket {
10-
spinlock_t lock;
11-
struct hlist_head list;
12-
} ____cacheline_aligned_in_smp;
7+
void init_hash_table(struct io_hash_table *table, unsigned size);

io_uring/fdinfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
158158
mutex_unlock(&ctx->uring_lock);
159159

160160
seq_puts(m, "PollList:\n");
161-
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
162-
struct io_hash_bucket *hb = &ctx->cancel_hash[i];
161+
for (i = 0; i < (1U << ctx->cancel_table.hash_bits); i++) {
162+
struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
163163
struct io_kiocb *req;
164164

165165
spin_lock(&hb->lock);

io_uring/io_uring.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,23 @@ static __cold void io_fallback_req_func(struct work_struct *work)
241241
percpu_ref_put(&ctx->refs);
242242
}
243243

244+
static int io_alloc_hash_table(struct io_hash_table *table, unsigned bits)
245+
{
246+
unsigned hash_buckets = 1U << bits;
247+
size_t hash_size = hash_buckets * sizeof(table->hbs[0]);
248+
249+
table->hbs = kmalloc(hash_size, GFP_KERNEL);
250+
if (!table->hbs)
251+
return -ENOMEM;
252+
253+
table->hash_bits = bits;
254+
init_hash_table(table, hash_buckets);
255+
return 0;
256+
}
257+
244258
static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
245259
{
246260
struct io_ring_ctx *ctx;
247-
unsigned hash_buckets;
248-
size_t hash_size;
249261
int hash_bits;
250262

251263
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
@@ -261,16 +273,9 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
261273
*/
262274
hash_bits = ilog2(p->cq_entries) - 5;
263275
hash_bits = clamp(hash_bits, 1, 8);
264-
hash_buckets = 1U << hash_bits;
265-
hash_size = hash_buckets * sizeof(struct io_hash_bucket);
266-
267-
ctx->cancel_hash_bits = hash_bits;
268-
ctx->cancel_hash = kmalloc(hash_size, GFP_KERNEL);
269-
if (!ctx->cancel_hash)
276+
if (io_alloc_hash_table(&ctx->cancel_table, hash_bits))
270277
goto err;
271278

272-
init_hash_table(ctx->cancel_hash, hash_buckets);
273-
274279
ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
275280
if (!ctx->dummy_ubuf)
276281
goto err;
@@ -311,7 +316,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
311316
return ctx;
312317
err:
313318
kfree(ctx->dummy_ubuf);
314-
kfree(ctx->cancel_hash);
319+
kfree(ctx->cancel_table.hbs);
315320
kfree(ctx->io_bl);
316321
xa_destroy(&ctx->io_bl_xa);
317322
kfree(ctx);
@@ -2487,7 +2492,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
24872492
io_req_caches_free(ctx);
24882493
if (ctx->hash_map)
24892494
io_wq_put_hash(ctx->hash_map);
2490-
kfree(ctx->cancel_hash);
2495+
kfree(ctx->cancel_table.hbs);
24912496
kfree(ctx->dummy_ubuf);
24922497
kfree(ctx->io_bl);
24932498
xa_destroy(&ctx->io_bl_xa);

io_uring/io_uring_types.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
#include "io-wq.h"
1010
#include "filetable.h"
1111

12+
struct io_hash_bucket {
13+
spinlock_t lock;
14+
struct hlist_head list;
15+
} ____cacheline_aligned_in_smp;
16+
17+
struct io_hash_table {
18+
struct io_hash_bucket *hbs;
19+
unsigned hash_bits;
20+
};
21+
1222
struct io_uring {
1323
u32 head ____cacheline_aligned_in_smp;
1424
u32 tail ____cacheline_aligned_in_smp;
@@ -224,8 +234,7 @@ struct io_ring_ctx {
224234
* manipulate the list, hence no extra locking is needed there.
225235
*/
226236
struct io_wq_work_list iopoll_list;
227-
struct io_hash_bucket *cancel_hash;
228-
unsigned cancel_hash_bits;
237+
struct io_hash_table cancel_table;
229238
bool poll_multi_queue;
230239

231240
struct list_head io_buffers_comp;

io_uring/poll.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ static struct io_poll *io_poll_get_single(struct io_kiocb *req)
7373

7474
static void io_poll_req_insert(struct io_kiocb *req)
7575
{
76-
struct io_ring_ctx *ctx = req->ctx;
77-
u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
78-
struct io_hash_bucket *hb = &ctx->cancel_hash[index];
76+
struct io_hash_table *table = &req->ctx->cancel_table;
77+
u32 index = hash_long(req->cqe.user_data, table->hash_bits);
78+
struct io_hash_bucket *hb = &table->hbs[index];
7979

8080
spin_lock(&hb->lock);
8181
hlist_add_head(&req->hash_node, &hb->list);
@@ -84,8 +84,9 @@ static void io_poll_req_insert(struct io_kiocb *req)
8484

8585
static void io_poll_req_delete(struct io_kiocb *req, struct io_ring_ctx *ctx)
8686
{
87-
u32 index = hash_long(req->cqe.user_data, ctx->cancel_hash_bits);
88-
spinlock_t *lock = &ctx->cancel_hash[index].lock;
87+
struct io_hash_table *table = &req->ctx->cancel_table;
88+
u32 index = hash_long(req->cqe.user_data, table->hash_bits);
89+
spinlock_t *lock = &table->hbs[index].lock;
8990

9091
spin_lock(lock);
9192
hash_del(&req->hash_node);
@@ -539,13 +540,15 @@ int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
539540
__cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
540541
bool cancel_all)
541542
{
543+
struct io_hash_table *table = &ctx->cancel_table;
544+
unsigned nr_buckets = 1U << table->hash_bits;
542545
struct hlist_node *tmp;
543546
struct io_kiocb *req;
544547
bool found = false;
545548
int i;
546549

547-
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
548-
struct io_hash_bucket *hb = &ctx->cancel_hash[i];
550+
for (i = 0; i < nr_buckets; i++) {
551+
struct io_hash_bucket *hb = &table->hbs[i];
549552

550553
spin_lock(&hb->lock);
551554
hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
@@ -562,12 +565,12 @@ __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
562565

563566
static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
564567
struct io_cancel_data *cd,
565-
struct io_hash_bucket hash_table[],
568+
struct io_hash_table *table,
566569
struct io_hash_bucket **out_bucket)
567570
{
568571
struct io_kiocb *req;
569-
u32 index = hash_long(cd->data, ctx->cancel_hash_bits);
570-
struct io_hash_bucket *hb = &hash_table[index];
572+
u32 index = hash_long(cd->data, table->hash_bits);
573+
struct io_hash_bucket *hb = &table->hbs[index];
571574

572575
*out_bucket = NULL;
573576

@@ -591,16 +594,17 @@ static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
591594

592595
static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
593596
struct io_cancel_data *cd,
594-
struct io_hash_bucket hash_table[],
597+
struct io_hash_table *table,
595598
struct io_hash_bucket **out_bucket)
596599
{
600+
unsigned nr_buckets = 1U << table->hash_bits;
597601
struct io_kiocb *req;
598602
int i;
599603

600604
*out_bucket = NULL;
601605

602-
for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
603-
struct io_hash_bucket *hb = &hash_table[i];
606+
for (i = 0; i < nr_buckets; i++) {
607+
struct io_hash_bucket *hb = &table->hbs[i];
604608

605609
spin_lock(&hb->lock);
606610
hlist_for_each_entry(req, &hb->list, hash_node) {
@@ -628,15 +632,15 @@ static bool io_poll_disarm(struct io_kiocb *req)
628632
}
629633

630634
static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
631-
struct io_hash_bucket hash_table[])
635+
struct io_hash_table *table)
632636
{
633637
struct io_hash_bucket *bucket;
634638
struct io_kiocb *req;
635639

636640
if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_ANY))
637-
req = io_poll_file_find(ctx, cd, ctx->cancel_hash, &bucket);
641+
req = io_poll_file_find(ctx, cd, table, &bucket);
638642
else
639-
req = io_poll_find(ctx, false, cd, ctx->cancel_hash, &bucket);
643+
req = io_poll_find(ctx, false, cd, table, &bucket);
640644

641645
if (req)
642646
io_poll_cancel_req(req);
@@ -647,7 +651,7 @@ static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
647651

648652
int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
649653
{
650-
return __io_poll_cancel(ctx, cd, ctx->cancel_hash);
654+
return __io_poll_cancel(ctx, cd, &ctx->cancel_table);
651655
}
652656

653657
static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
@@ -745,7 +749,7 @@ int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
745749
int ret2, ret = 0;
746750
bool locked;
747751

748-
preq = io_poll_find(ctx, true, &cd, ctx->cancel_hash, &bucket);
752+
preq = io_poll_find(ctx, true, &cd, &ctx->cancel_table, &bucket);
749753
if (preq)
750754
ret2 = io_poll_disarm(preq);
751755
if (bucket)

0 commit comments

Comments
 (0)