Skip to content

Commit c16361c

Browse files
committed
io_uring: add io_kiocb ref count
We'll use this for the POLL implementation. Regular requests will NOT be using references, so initialize it to 0. Any real use of the io_kiocb ref will initialize it to at least 2. Reviewed-by: Hannes Reinecke <hare@suse.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 6c271ce commit c16361c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

fs/io_uring.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ struct io_kiocb {
184184
struct io_ring_ctx *ctx;
185185
struct list_head list;
186186
unsigned int flags;
187+
refcount_t refs;
187188
#define REQ_F_FORCE_NONBLOCK 1 /* inline submission attempt */
188189
#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
189190
#define REQ_F_FIXED_FILE 4 /* ctx owns file */
@@ -377,6 +378,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
377378

378379
req->ctx = ctx;
379380
req->flags = 0;
381+
refcount_set(&req->refs, 0);
380382
return req;
381383
out:
382384
io_ring_drop_ctx_refs(ctx, 1);
@@ -394,8 +396,10 @@ static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
394396

395397
static void io_free_req(struct io_kiocb *req)
396398
{
397-
io_ring_drop_ctx_refs(req->ctx, 1);
398-
kmem_cache_free(req_cachep, req);
399+
if (!refcount_read(&req->refs) || refcount_dec_and_test(&req->refs)) {
400+
io_ring_drop_ctx_refs(req->ctx, 1);
401+
kmem_cache_free(req_cachep, req);
402+
}
399403
}
400404

401405
/*

0 commit comments

Comments
 (0)