io_uring/rsrc: split out io_free_node() helper
PATCH 1/5 io_uring/rsrc: split out io_free_node() helper
Split the freeing of the io_rsrc_node from io_free_rsrc_node(), for use with nodes that haven't been fully initialized.
--- io_uring/rsrc.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
io_uring/rsrc.c +7 -2
@@ -485,10 +485,16 @@ int io_files_update(struct io_kiocb *req, unsigned int issue_flags)req_set_fail (req);io_req_set_res (req, ret,0 );return IOU_OK ; } +static void io_free_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node) +{ +if (!io_alloc_cache_put (&ctx->node_cache, node)) +kvfree (node); +} +void io_free_rsrc_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node) {if (node->tag)io_post_aux_cqe (ctx, node->tag,0 ,0 );
@@ -504,12 +510,11 @@ void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)default :WARN_ON_ONCE (1 );break ; } -if (!io_alloc_cache_put (&ctx->node_cache, node)) -kvfree (node); +io_free_node (ctx, node); }int io_sqe_files_unregister (struct io_ring_ctx *ctx) {if (!ctx->file_table.data.nr)
Signature · 1 line
2.45.2
PATCH 2/5 io_uring/rsrc: free io_rsrc_node using kfree()
io_rsrc_node_alloc() calls io_cache_alloc(), which uses kmalloc() to allocate the node. So it can be freed with kfree() instead of kvfree().
--- io_uring/rsrc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/rsrc.c +1 -1
@@ -488,11 +488,11 @@ int io_files_update(struct io_kiocb *req, unsigned int issue_flags) }static void io_free_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node) {if (!io_alloc_cache_put (&ctx->node_cache, node)) -kvfree (node); +kfree (node); }void io_free_rsrc_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node) {if (node->tag)
Signature · 1 line
2.45.2
PATCH 3/5 io_uring/rsrc: call io_free_node() on io_sqe_buffer_register() failure
In reply to Caleb Sander Mateosio_sqe_buffer_register() currently calls io_put_rsrc_node() if it fails to fully set up the io_rsrc_node. io_put_rsrc_node() is more involved than necessary, since we already know the reference count will reach 0 and no io_mapped_ubuf has been attached to the node yet.
So just call io_free_node() to release the node's memory. This also avoids the need to temporarily set the node's buf pointer to NULL.
--- io_uring/rsrc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
io_uring/rsrc.c +1 -2
@@ -780,11 +780,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,return NULL ; node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!node)return ERR_PTR (-ENOMEM ); - node->buf =NULL ; ret = -ENOMEM ; pages =io_pin_pages ((unsigned long ) iov->iov_base, iov->iov_len, &nr_pages);if (IS_ERR (pages)) {
@@ -837,11 +836,11 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, done:if (ret) {if (imu)io_free_imu (ctx, imu);if (node) -io_put_rsrc_node (ctx, node); +io_free_node (ctx, node); node =ERR_PTR (ret); }kvfree (pages);return node; }
Signature · 1 line
2.45.2
Re: [PATCH 3/5] io_uring/rsrc: call io_free_node() on io_sqe_buffer_register() failure
Quoted 18 lines · Caleb Sander Mateos
io_sqe_buffer_register()currently callsio_put_rsrc_node()if it fails to fully set up theio_rsrc_node.io_put_rsrc_node()is more involved than necessary, since we already know the reference count will reach 0 and noio_mapped_ubufhas been attached to the node yet.So just call
io_free_node()to release the node's memory. This also avoids the need to temporarily set the node's buf pointer to NULL.Signed-off-by Caleb Sander Mateos <[email protected]>--- io_uring/rsrc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)io_uring/rsrc.c +0
-0
io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 785
Quoted 6 lines
io_uring/rsrc.c +0
-1@@ -780,11 +780,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,return NULL ; node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!node)return ERR_PTR (-ENOMEM ); - node->buf =NULL ;
It's better to have it zeroed than set to a freed / invalid value, it's a slow path.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [PATCH 3/5] io_uring/rsrc: call io_free_node() on io_sqe_buffer_register() failure
Quoted 14 lines · Pavel Begunkov
Quoted 13 lines · Caleb Sander Mateos
io_sqe_buffer_register()currently callsio_put_rsrc_node()if it fails to fully set up theio_rsrc_node.io_put_rsrc_node()is more involved than necessary, since we already know the reference count will reach 0 and noio_mapped_ubufhas been attached to the node yet.So just call
io_free_node()to release the node's memory. This also avoids the need to temporarily set the node's buf pointer to NULL.Signed-off-by Caleb Sander Mateos <[email protected]>--- io_uring/rsrc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 13 lines
io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 784
Quoted 10 lines
io_uring/rsrc.c +0
-1@@ -780,11 +780,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,return NULL ; node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!node)return ERR_PTR (-ENOMEM ); - node->buf =NULL ;It's better to have it zeroed than set to a freed / invalid value, it's a slow path.
Agree, let's leave the clear, I don't like passing uninitialized memory around.
Signature · 1 line
Jens Axboe
Re: [PATCH 3/5] io_uring/rsrc: call io_free_node() on io_sqe_buffer_register() failure
Quoted 30 lines · Jens Axboe
Quoted 14 lines · Pavel Begunkov
Quoted 13 lines · Caleb Sander Mateos
io_sqe_buffer_register()currently callsio_put_rsrc_node()if it fails to fully set up theio_rsrc_node.io_put_rsrc_node()is more involved than necessary, since we already know the reference count will reach 0 and noio_mapped_ubufhas been attached to the node yet.So just call
io_free_node()to release the node's memory. This also avoids the need to temporarily set the node's buf pointer to NULL.Signed-off-by Caleb Sander Mateos <[email protected]>--- io_uring/rsrc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)Quoted 13 lines
io_uring/rsrc.c static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, · line 784
Quoted 10 lines
io_uring/rsrc.c +0
-1@@ -780,11 +780,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,return NULL ; node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!node)return ERR_PTR (-ENOMEM ); - node->buf =NULL ;It's better to have it zeroed than set to a freed / invalid value, it's a slow path.
Quoted 2 lines
Agree, let's leave the clear, I don't like passing uninitialized memory around.
io_rsrc_node_alloc() actually does already zero all of io_rsrc_node's fields (file_ptr is in a union with buf):
struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type)
{
struct io_rsrc_node *node;
node = io_cache_alloc(&ctx->node_cache, GFP_KERNEL);
if (node) {
node->type = type;
node->refs = 1;
node->tag = 0;
node->file_ptr = 0;
}
return node;
}How about I remove the redundant node->buf = NULL; in a separate patch, since it's not dependent on switching the error path to io_free_node()?
Thanks,
Caleb
PATCH 4/5 io_uring/rsrc: avoid NULL node check on io_sqe_buffer_register() failure
In reply to Caleb Sander MateosThe done: label is only reachable if node is non-NULL. So don't bother checking, just call io_free_node().
--- io_uring/rsrc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
io_uring/rsrc.c +1 -2
@@ -835,12 +835,11 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } done:if (ret) {if (imu)io_free_imu (ctx, imu); -if (node) -io_free_node (ctx, node); +io_free_node (ctx, node); node =ERR_PTR (ret); }kvfree (pages);return node; }
Signature · 1 line
2.45.2
PATCH 5/5 io_uring/rsrc: skip NULL file/buffer checks in io_free_rsrc_node()
In reply to Caleb Sander Mateosio_rsrc_node's of type IORING_RSRC_FILE always have a file attached immediately after they are allocated. IORING_RSRC_BUFFER nodes won't be returned from io_sqe_buffer_register()/io_buffer_register_bvec() until they have a io_mapped_ubuf attached.
So remove the checks for a NULL file/buffer in io_free_rsrc_node().
--- io_uring/rsrc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
io_uring/rsrc.c +2 -4
@@ -498,16 +498,14 @@ void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)if (node->tag)io_post_aux_cqe (ctx, node->tag,0 ,0 );switch (node->type) {case IORING_RSRC_FILE : -if (io_slot_file (node)) -fput (io_slot_file (node)); +fput (io_slot_file (node));break ;case IORING_RSRC_BUFFER : -if (node->buf) -io_buffer_unmap (ctx, node->buf); +io_buffer_unmap (ctx, node->buf);break ;default :WARN_ON_ONCE (1 );break ; }
Signature · 1 line
2.45.2
Hi,
Quoted 21 lines
-----Original Message-----
From: Caleb Sander Mateos <[email protected]>
Sent: Saturday, March 1, 2025 7:59 AM
To: Jens Axboe <[email protected]>; Pavel Begunkov <[email protected]>
Cc: Caleb Sander Mateos <[email protected]>; io- [email protected]; [email protected]
Subject: [PATCH 1/5]io_uring/rsrc: split outio_free_node()helperSplit the freeing of the
io_rsrc_nodefromio_free_rsrc_node(), for use with nodes that haven't been fully initialized.Signed-off-by Caleb Sander Mateos <[email protected]>--- io_uring/rsrc.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)io_uring/rsrc.c index 45bfb37bca1e..d941256f0d8c renamed from io_uring/rsrc.c +0
-0100644
io_uring/rsrc.c int io_files_update(struct io_kiocb *req, unsigned int · line 492
Quoted 8 lines
io_uring/rsrc.c +2
-0@@ -485,10 +485,16 @@ int io_files_update(struct io_kiocb *req, unsigned int issue_flags)req_set_fail (req );io_req_set_res (req, ret,0 );return IOU_OK ; } +static void io_free_node (struct io_ring_ctx *ctx,struct io_rsrc_node +*node) {
Would it be better to take a io_alloc_cache as parameter and put it into alloc_cache.h as a general function, so other modules can use it too, just like io_uring/futex.
Quoted 23 lines
io_uring/rsrc.c +5
-3+if (!io_alloc_cache_put (&ctx->node_cache, node)) +kvfree (node); +} +void io_free_rsrc_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node) {if (node->tag)io_post_aux_cqe (ctx, node->tag,0 ,0 );@@ -504,12 +510,11 @@ void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)default :WARN_ON_ONCE (1 );break ; } -if (!io_alloc_cache_put (&ctx->node_cache, node)) -kvfree (node); +io_free_node (ctx, node); }int io_sqe_files_unregister (struct io_ring_ctx *ctx) {if (!ctx->file_table.data.nr) --
Quoted 2 lines
2.45.2
---
Li Zetao
Quoted 4 lines · Caleb Sander Mateos
Split the freeing of the
io_rsrc_nodefromio_free_rsrc_node(), for use with nodes that haven't been fully initialized.
Applied, thanks!
[1/5] io_uring/rsrc: split out io_free_node() helper
commit: 6a53541829662c8f1357f522a1d6315179442bf7
[2/5] io_uring/rsrc: free io_rsrc_node using kfree()
commit: a387b96d2a9687201318826d23c770eb794c778e
[3/5] io_uring/rsrc: call io_free_node() on io_sqe_buffer_register() failure
commit: 13f7f9686e928dae352972a1a95b50b2d5e80d42
[4/5] io_uring/rsrc: avoid NULL node check on io_sqe_buffer_register() failure
commit: 6e5d321a08e30f746d63fc56e7ea5c46b06fbe99
[5/5] io_uring/rsrc: skip NULL file/buffer checks in io_free_rsrc_node()
commit: fe21a4532ef2a6852c89b352cb8ded0d37b4745cBest regards,
Signature · 1 line
Jens Axboe