Commit b8fb5b4f authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Jens Axboe

io_uring/rsrc: use non-pcpu refcounts for nodes

One problem with the current rsrc infra is that often updates will
generates lots of rsrc nodes, each carry pcpu refs. That takes quite a
lot of memory, especially if there is a stall, and takes lots of CPU
cycles. Only pcpu allocations takes >50 of CPU with a naive benchmark
updating files in a loop.

Replace pcpu refs with normal refcounting. There is already a hot path
avoiding atomics / refs, but following patches will further improve it.
Signed-off-by: default avatarPavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/e9ed8a9457b331a26555ff9443afc64cdaab7247.1680576071.git.asml.silence@gmail.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent e3ef728f
...@@ -155,7 +155,7 @@ void io_rsrc_refs_refill(struct io_ring_ctx *ctx) ...@@ -155,7 +155,7 @@ void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
__must_hold(&ctx->uring_lock) __must_hold(&ctx->uring_lock)
{ {
ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); refcount_add(IO_RSRC_REF_BATCH, &ctx->rsrc_node->refs);
} }
static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
...@@ -220,13 +220,11 @@ void io_wait_rsrc_data(struct io_rsrc_data *data) ...@@ -220,13 +220,11 @@ void io_wait_rsrc_data(struct io_rsrc_data *data)
void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
{ {
percpu_ref_exit(&ref_node->refs);
kfree(ref_node); kfree(ref_node);
} }
static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) __cold void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
{ {
struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
struct io_ring_ctx *ctx = node->rsrc_data->ctx; struct io_ring_ctx *ctx = node->rsrc_data->ctx;
unsigned long flags; unsigned long flags;
bool first_add = false; bool first_add = false;
...@@ -269,11 +267,7 @@ static struct io_rsrc_node *io_rsrc_node_alloc(void) ...@@ -269,11 +267,7 @@ static struct io_rsrc_node *io_rsrc_node_alloc(void)
if (!ref_node) if (!ref_node)
return NULL; return NULL;
if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, refcount_set(&ref_node->refs, 1);
0, GFP_KERNEL)) {
kfree(ref_node);
return NULL;
}
INIT_LIST_HEAD(&ref_node->node); INIT_LIST_HEAD(&ref_node->node);
INIT_LIST_HEAD(&ref_node->rsrc_list); INIT_LIST_HEAD(&ref_node->rsrc_list);
ref_node->done = false; ref_node->done = false;
...@@ -298,7 +292,8 @@ void io_rsrc_node_switch(struct io_ring_ctx *ctx, ...@@ -298,7 +292,8 @@ void io_rsrc_node_switch(struct io_ring_ctx *ctx,
spin_unlock_irq(&ctx->rsrc_ref_lock); spin_unlock_irq(&ctx->rsrc_ref_lock);
atomic_inc(&data_to_kill->refs); atomic_inc(&data_to_kill->refs);
percpu_ref_kill(&rsrc_node->refs); /* put master ref */
io_rsrc_put_node(rsrc_node, 1);
ctx->rsrc_node = NULL; ctx->rsrc_node = NULL;
} }
......
...@@ -37,7 +37,7 @@ struct io_rsrc_data { ...@@ -37,7 +37,7 @@ struct io_rsrc_data {
}; };
struct io_rsrc_node { struct io_rsrc_node {
struct percpu_ref refs; refcount_t refs;
struct list_head node; struct list_head node;
struct list_head rsrc_list; struct list_head rsrc_list;
struct io_rsrc_data *rsrc_data; struct io_rsrc_data *rsrc_data;
...@@ -54,6 +54,7 @@ struct io_mapped_ubuf { ...@@ -54,6 +54,7 @@ struct io_mapped_ubuf {
}; };
void io_rsrc_put_tw(struct callback_head *cb); void io_rsrc_put_tw(struct callback_head *cb);
void io_rsrc_node_ref_zero(struct io_rsrc_node *node);
void io_rsrc_put_work(struct work_struct *work); void io_rsrc_put_work(struct work_struct *work);
void io_rsrc_refs_refill(struct io_ring_ctx *ctx); void io_rsrc_refs_refill(struct io_ring_ctx *ctx);
void io_wait_rsrc_data(struct io_rsrc_data *data); void io_wait_rsrc_data(struct io_rsrc_data *data);
...@@ -109,7 +110,8 @@ int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, ...@@ -109,7 +110,8 @@ int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
static inline void io_rsrc_put_node(struct io_rsrc_node *node, int nr) static inline void io_rsrc_put_node(struct io_rsrc_node *node, int nr)
{ {
percpu_ref_put_many(&node->refs, nr); if (refcount_sub_and_test(nr, &node->refs))
io_rsrc_node_ref_zero(node);
} }
static inline void io_req_put_rsrc(struct io_kiocb *req) static inline void io_req_put_rsrc(struct io_kiocb *req)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment