Skip to content

Commit 0b222ee

Browse files
isilenceaxboe
authored andcommitted
io_uring/rsrc: remove rsrc_data refs
Instead of waiting for rsrc_data->refs to be downed to zero, check whether there are rsrc nodes queued for completion, that's easier then maintaining references. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/8e33fd143d83e11af3e386aea28eb6d6c6a1be10.1681395792.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 7d481e0 commit 0b222ee

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

include/linux/io_uring_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ struct io_ring_ctx {
334334
struct list_head rsrc_ref_list;
335335
struct io_alloc_cache rsrc_node_cache;
336336
struct wait_queue_head rsrc_quiesce_wq;
337+
unsigned rsrc_quiesce;
337338

338339
struct list_head io_buffers_pages;
339340

io_uring/io_uring.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,8 +2831,8 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
28312831
{
28322832
io_sq_thread_finish(ctx);
28332833
/* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
2834-
io_wait_rsrc_data(ctx->buf_data);
2835-
io_wait_rsrc_data(ctx->file_data);
2834+
if (WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)))
2835+
return;
28362836

28372837
mutex_lock(&ctx->uring_lock);
28382838
if (ctx->buf_data)

io_uring/rsrc.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
3131
#define IORING_MAX_FIXED_FILES (1U << 20)
3232
#define IORING_MAX_REG_BUFFERS (1U << 14)
3333

34-
static inline bool io_put_rsrc_data_ref(struct io_rsrc_data *rsrc_data)
35-
{
36-
return !--rsrc_data->refs;
37-
}
38-
3934
int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
4035
{
4136
unsigned long page_limit, cur_pages, new_pages;
@@ -158,7 +153,6 @@ static void io_rsrc_put_work_one(struct io_rsrc_data *rsrc_data,
158153
static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
159154
{
160155
struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
161-
struct io_ring_ctx *ctx = rsrc_data->ctx;
162156
struct io_rsrc_put *prsrc, *tmp;
163157

164158
if (ref_node->inline_items)
@@ -171,14 +165,6 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
171165
}
172166

173167
io_rsrc_node_destroy(rsrc_data->ctx, ref_node);
174-
if (io_put_rsrc_data_ref(rsrc_data))
175-
wake_up_all(&ctx->rsrc_quiesce_wq);
176-
}
177-
178-
void io_wait_rsrc_data(struct io_rsrc_data *data)
179-
{
180-
if (data)
181-
WARN_ON_ONCE(!io_put_rsrc_data_ref(data));
182168
}
183169

184170
void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
@@ -201,6 +187,8 @@ void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
201187
list_del(&node->node);
202188
__io_rsrc_put_work(node);
203189
}
190+
if (list_empty(&ctx->rsrc_ref_list) && unlikely(ctx->rsrc_quiesce))
191+
wake_up_all(&ctx->rsrc_quiesce_wq);
204192
}
205193

206194
struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
@@ -235,7 +223,6 @@ void io_rsrc_node_switch(struct io_ring_ctx *ctx,
235223
if (WARN_ON_ONCE(!backup))
236224
return;
237225

238-
data_to_kill->refs++;
239226
node->rsrc_data = data_to_kill;
240227
list_add_tail(&node->node, &ctx->rsrc_ref_list);
241228
/* put master ref */
@@ -269,15 +256,15 @@ __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
269256
return ret;
270257
io_rsrc_node_switch(ctx, data);
271258

272-
/* kill initial ref */
273-
if (io_put_rsrc_data_ref(data))
259+
if (list_empty(&ctx->rsrc_ref_list))
274260
return 0;
275261

276262
if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
277263
atomic_set(&ctx->cq_wait_nr, 1);
278264
smp_mb();
279265
}
280266

267+
ctx->rsrc_quiesce++;
281268
data->quiesce = true;
282269
do {
283270
prepare_to_wait(&ctx->rsrc_quiesce_wq, &we, TASK_INTERRUPTIBLE);
@@ -286,23 +273,21 @@ __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
286273
ret = io_run_task_work_sig(ctx);
287274
if (ret < 0) {
288275
mutex_lock(&ctx->uring_lock);
289-
if (!data->refs) {
276+
if (list_empty(&ctx->rsrc_ref_list))
290277
ret = 0;
291-
} else {
292-
/* restore the master reference */
293-
data->refs++;
294-
}
295278
break;
296279
}
297280

298281
schedule();
299282
__set_current_state(TASK_RUNNING);
300283
mutex_lock(&ctx->uring_lock);
301284
ret = 0;
302-
} while (data->refs);
285+
} while (!list_empty(&ctx->rsrc_ref_list));
303286

304287
finish_wait(&ctx->rsrc_quiesce_wq, &we);
305288
data->quiesce = false;
289+
ctx->rsrc_quiesce--;
290+
306291
if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
307292
atomic_set(&ctx->cq_wait_nr, 0);
308293
smp_mb();
@@ -371,7 +356,6 @@ __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
371356
data->nr = nr;
372357
data->ctx = ctx;
373358
data->do_put = do_put;
374-
data->refs = 1;
375359
if (utags) {
376360
ret = -EFAULT;
377361
for (i = 0; i < nr; i++) {

io_uring/rsrc.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ struct io_rsrc_data {
3535
u64 **tags;
3636
unsigned int nr;
3737
rsrc_put_fn *do_put;
38-
int refs;
3938
bool quiesce;
4039
};
4140

@@ -69,7 +68,6 @@ struct io_mapped_ubuf {
6968
void io_rsrc_put_tw(struct callback_head *cb);
7069
void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
7170
void io_rsrc_put_work(struct work_struct *work);
72-
void io_wait_rsrc_data(struct io_rsrc_data *data);
7371
void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node);
7472
int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
7573
struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);

0 commit comments

Comments
 (0)