diff mbox series

[2/2] io_uring: defer alloc_hint update to io_file_bitmap_set()

Message ID 20220527165333.55212-3-xiaoguang.wang@linux.alibaba.com (mailing list archive)
State New
Headers show
Series two bug fixes about direct fd install | expand

Commit Message

Xiaoguang Wang May 27, 2022, 4:53 p.m. UTC
io_file_bitmap_get() returns a free bitmap slot, but if it isn't
used later, such as io_queue_rsrc_removal() returns error, in this
case, we should not update alloc_hint at all, which still should
be considered as a valid candidate for next io_file_bitmap_get()
calls.

To fix this issue, only update alloc_hint in io_file_bitmap_set().

Signed-off-by: Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
---
 fs/io_uring.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

Comments

Jens Axboe May 27, 2022, 5:09 p.m. UTC | #1
On 5/27/22 10:53 AM, Xiaoguang Wang wrote:
> @@ -9650,12 +9646,15 @@ static void io_free_file_tables(struct io_file_table *table)
>  	table->bitmap = NULL;
>  }
>  
> -static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
> +static inline void io_file_bitmap_set(struct io_ring_ctx *ctx, int bit)
>  {
> +	struct io_file_table *table = &ctx->file_table;
> +
>  	WARN_ON_ONCE(test_bit(bit, table->bitmap));
>  	__set_bit(bit, table->bitmap);
> -	if (bit == table->alloc_hint)
> -		table->alloc_hint++;
> +	table->alloc_hint = bit + 1;
> +	if (table->alloc_hint >= ctx->nr_user_files)
> +		table->alloc_hint = 0;
>  }

This branch isn't needed, we'll reset it when failing to get one anyway.
Much better than adding a branch to the fast path.
diff mbox series

Patch

diff --git a/fs/io_uring.c b/fs/io_uring.c
index ff50e5f1753d..811007e055c6 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -5419,15 +5419,11 @@  static int io_file_bitmap_get(struct io_ring_ctx *ctx)
 	unsigned long nr = ctx->nr_user_files;
 	int ret;
 
-	if (table->alloc_hint >= nr)
-		table->alloc_hint = 0;
-
 	do {
 		ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);
-		if (ret != nr) {
-			table->alloc_hint = ret + 1;
+		if (ret != nr)
 			return ret;
-		}
+
 		if (!table->alloc_hint)
 			break;
 
@@ -9650,12 +9646,15 @@  static void io_free_file_tables(struct io_file_table *table)
 	table->bitmap = NULL;
 }
 
-static inline void io_file_bitmap_set(struct io_file_table *table, int bit)
+static inline void io_file_bitmap_set(struct io_ring_ctx *ctx, int bit)
 {
+	struct io_file_table *table = &ctx->file_table;
+
 	WARN_ON_ONCE(test_bit(bit, table->bitmap));
 	__set_bit(bit, table->bitmap);
-	if (bit == table->alloc_hint)
-		table->alloc_hint++;
+	table->alloc_hint = bit + 1;
+	if (table->alloc_hint >= ctx->nr_user_files)
+		table->alloc_hint = 0;
 }
 
 static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)
@@ -10080,7 +10079,7 @@  static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
 		}
 		file_slot = io_fixed_file_slot(&ctx->file_table, i);
 		io_fixed_file_set(file_slot, file);
-		io_file_bitmap_set(&ctx->file_table, i);
+		io_file_bitmap_set(ctx, i);
 	}
 
 	io_rsrc_node_switch(ctx, NULL);
@@ -10161,7 +10160,7 @@  static int io_install_fixed_file(struct io_kiocb *req, unsigned int issue_flags,
 	if (!ret) {
 		*io_get_tag_slot(ctx->file_data, slot_index) = 0;
 		io_fixed_file_set(file_slot, file);
-		io_file_bitmap_set(&ctx->file_table, slot_index);
+		io_file_bitmap_set(ctx, slot_index);
 	}
 err:
 	if (needs_switch)
@@ -10284,7 +10283,7 @@  static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 			}
 			*io_get_tag_slot(data, i) = tag;
 			io_fixed_file_set(file_slot, file);
-			io_file_bitmap_set(&ctx->file_table, i);
+			io_file_bitmap_set(ctx, i);
 		}
 	}