Skip to contentSkip to conversation

fs: io_uring.c: Add skip option for __io_sqe_files_update

PATCH In review noah 2 participants 8 messages Dec 20, 2020 – Jan 27, 2021

Subsystems (2)
lore
Share & tools
lore

noah

author
#0 lore raw

PATCH 1/1 fs: io_uring.c: Add skip option for __io_sqe_files_update

From: noah <[email protected]>

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

---
 fs/io_uring.c | 72 ++++++++++++++++++++++++++-------------------------
 1 file changed, 37 insertions(+), 35 deletions(-)
fs/io_uring.c +37 -35
index 6f9392c35eef..43ab2b7a87d4 100644
@@ -7876,42 +7876,44 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 			err = -EFAULT;
 			break;
 		}
-		i = array_index_nospec(up->offset, ctx->nr_user_files);
-		table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
-		index = i & IORING_FILE_TABLE_MASK;
-		if (table->files[index]) {
-			file = table->files[index];
-			err = io_queue_file_removal(data, file);
-			if (err)
-				break;
-			table->files[index] = NULL;
-			needs_switch = true;
-		}
-		if (fd != -1) {
-			file = fget(fd);
-			if (!file) {
-				err = -EBADF;
-				break;
-			}
-			/*
-			 * Don't allow io_uring instances to be registered. If
-			 * UNIX isn't enabled, then this causes a reference
-			 * cycle and this instance can never get freed. If UNIX
-			 * is enabled we'll handle it just fine, but there's
-			 * still no point in allowing a ring fd as it doesn't
-			 * support regular read/write anyway.
-			 */
-			if (file->f_op == &io_uring_fops) {
-				fput(file);
-				err = -EBADF;
-				break;
-			}
-			table->files[index] = file;
-			err = io_sqe_file_register(ctx, file, i);
-			if (err) {
+		if (fd != -2) {
+			i = array_index_nospec(up->offset, ctx->nr_user_files);
+			table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
+			index = i & IORING_FILE_TABLE_MASK;
+			if (table->files[index]) {
+				file = table->files[index];
+				err = io_queue_file_removal(data, file);
+				if (err)
+					break;
 				table->files[index] = NULL;
-				fput(file);
-				break;
+				needs_switch = true;
+			}
+			if (fd != -1) {
+				file = fget(fd);
+				if (!file) {
+					err = -EBADF;
+					break;
+				}
+				/*
+				 * Don't allow io_uring instances to be registered. If
+				 * UNIX isn't enabled, then this causes a reference
+				 * cycle and this instance can never get freed. If UNIX
+				 * is enabled we'll handle it just fine, but there's
+				 * still no point in allowing a ring fd as it doesn't
+				 * support regular read/write anyway.
+				 */
+				if (file->f_op == &io_uring_fops) {
+					fput(file);
+					err = -EBADF;
+					break;
+				}
+				table->files[index] = file;
+				err = io_sqe_file_register(ctx, file, i);
+				if (err) {
+					table->files[index] = NULL;
+					fput(file);
+					break;
+				}
 			}
 		}
 		nr_args--;
Signature · 1 line
2.29.2
#1 lore raw

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 6 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

fs/io_uring.c +5 -6
index 941fe9b64fd9..b3ae9d5da17e 100644
@@ -7847,9 +7847,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 	if (IS_ERR(ref_node))
 		return PTR_ERR(ref_node);
 
-	done = 0;
 	fds = u64_to_user_ptr(up->fds);
-	while (nr_args) {
+	for (done = 0; done < nr_args; done++) {
 		struct fixed_file_table *table;
 		unsigned index;
 
@@ -7858,7 +7857,10 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 			err = -EFAULT;
 			break;
 		}
-		i = array_index_nospec(up->offset, ctx->nr_user_files);
+		if (fd == IORING_REGISTER_FILES_SKIP)
+			continue;
+
+		i = array_index_nospec(up->offset + done, ctx->nr_user_files);
 		table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
 		index = i & IORING_FILE_TABLE_MASK;
 		if (table->files[index]) {
@@ -7896,9 +7898,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 				break;
 			}
 		}
-		nr_args--;
-		done++;
-		up->offset++;
 	}
 
 	if (needs_switch) {
Signature · 1 line
Pavel Begunkov
#2 lore raw
Quoted 54 lines · Pavel Begunkov

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 6 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

fs/io_uring.c +5 -6
index 941fe9b64fd9..b3ae9d5da17e 100644
@@ -7847,9 +7847,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 	if (IS_ERR(ref_node))
 		return PTR_ERR(ref_node);
 
-	done = 0;
 	fds = u64_to_user_ptr(up->fds);
-	while (nr_args) {
+	for (done = 0; done < nr_args; done++) {
 		struct fixed_file_table *table;
 		unsigned index;
 
@@ -7858,7 +7857,10 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 			err = -EFAULT;
 			break;
 		}
-		i = array_index_nospec(up->offset, ctx->nr_user_files);
+		if (fd == IORING_REGISTER_FILES_SKIP)
+			continue;
+
+		i = array_index_nospec(up->offset + done, ctx->nr_user_files);
 		table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
 		index = i & IORING_FILE_TABLE_MASK;
 		if (table->files[index]) {
@@ -7896,9 +7898,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 				break;
 			}
 		}
-		nr_args--;
-		done++;
-		up->offset++;
 	}
 
 	if (needs_switch) {
Signature · 2 lines
-- 
Pavel Begunkov

Ah. Got it.

#3 lore raw
Quoted 6 lines · Noah Goldstein
Quoted 5 lines · Pavel Begunkov

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 4 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

Noah, want to give it a try? I've just sent a prep patch, with it you can implement it cleaner with one continue.

Quoted 38 lines
Quoted 38 lines
fs/io_uring.c +5 -6
index 941fe9b64fd9..b3ae9d5da17e 100644
@@ -7847,9 +7847,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 	if (IS_ERR(ref_node))
 		return PTR_ERR(ref_node);
 
-	done = 0;
 	fds = u64_to_user_ptr(up->fds);
-	while (nr_args) {
+	for (done = 0; done < nr_args; done++) {
 		struct fixed_file_table *table;
 		unsigned index;
 
@@ -7858,7 +7857,10 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 			err = -EFAULT;
 			break;
 		}
-		i = array_index_nospec(up->offset, ctx->nr_user_files);
+		if (fd == IORING_REGISTER_FILES_SKIP)
+			continue;
+
+		i = array_index_nospec(up->offset + done, ctx->nr_user_files);
 		table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
 		index = i & IORING_FILE_TABLE_MASK;
 		if (table->files[index]) {
@@ -7896,9 +7898,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 				break;
 			}
 		}
-		nr_args--;
-		done++;
-		up->offset++;
 	}
 
 	if (needs_switch) {
Signature · 1 line
Pavel Begunkov
#4 lore raw
Quoted 11 lines · Pavel Begunkov
Quoted 9 lines · Noah Goldstein
Quoted 8 lines · Pavel Begunkov

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 6 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Quoted 9 lines
Quoted 5 lines
Quoted 5 lines

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

Noah, want to give it a try? I've just sent a prep patch, with it you can implement it cleaner with one continue.

Absolutely. Will get on it ASAP.

Quoted 41 lines
Quoted 37 lines
Quoted 37 lines
fs/io_uring.c +5 -6
index 941fe9b64fd9..b3ae9d5da17e 100644
@@ -7847,9 +7847,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
     if (IS_ERR(ref_node))
             return PTR_ERR(ref_node);
 
-    done = 0;
     fds = u64_to_user_ptr(up->fds);
-    while (nr_args) {
+    for (done = 0; done < nr_args; done++) {
             struct fixed_file_table *table;
             unsigned index;
 
@@ -7858,7 +7857,10 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
                     err = -EFAULT;
                     break;
             }
-            i = array_index_nospec(up->offset, ctx->nr_user_files);
+            if (fd == IORING_REGISTER_FILES_SKIP)
+                    continue;
+
+            i = array_index_nospec(up->offset + done, ctx->nr_user_files);
             table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
             index = i & IORING_FILE_TABLE_MASK;
             if (table->files[index]) {
@@ -7896,9 +7898,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
                             break;
                     }
             }
-            nr_args--;
-            done++;
-            up->offset++;
     }
 
Quoted 1 line

fs/io_uring.c static int __io_sqe_files_update(struct io_ring_ctx *ctx, · line 7903

Quoted 1 line
fs/io_uring.c +0 -0
     if (needs_switch) {

--
Pavel Begunkov

#5 lore raw
Quoted 18 lines · Noah Goldstein
Quoted 17 lines · Pavel Begunkov
Quoted 14 lines · Noah Goldstein
Quoted 13 lines · Pavel Begunkov

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 6 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

Quoted 4 lines
Quoted 2 lines

Noah, want to give it a try? I've just sent a prep patch, with it you can implement it cleaner with one continue.

Absolutely. Will get on it ASAP.

Perfect. Even better if you add a liburing test

Signature · 1 line
Pavel Begunkov
#6 lore raw
Quoted 23 lines · Pavel Begunkov
Quoted 21 lines · Noah Goldstein
Quoted 19 lines · Pavel Begunkov
Quoted 6 lines · Noah Goldstein
Quoted 5 lines · Pavel Begunkov

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 4 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

Noah, want to give it a try? I've just sent a prep patch, with it you can implement it cleaner with one continue.

Quoted 6 lines
Quoted 1 line

Absolutely. Will get on it ASAP.

Perfect. Even better if you add a liburing test

--
Pavel Begunkov

Do you think the return value should not include files skipped?

i.e register fds[1, 2, 3, -1] with no errors returns 4. should fds[1, 2, -2, -1] return 3 or 4 do you think?

Personally think the latter makes more sense. Thoughts?

#7 lore raw
Quoted 26 lines · Noah Goldstein
Quoted 25 lines · Pavel Begunkov
Quoted 22 lines · Noah Goldstein
Quoted 11 lines · Pavel Begunkov
Quoted 9 lines · Noah Goldstein
Quoted 8 lines · Pavel Begunkov

On 20/12/2020 06:50, noah wrote:> From: noah <[email protected]>

Quoted 6 lines

This patch makes it so that specify a file descriptor value of -2 will skip updating the corresponding fixed file index.

This will allow for users to reduce the number of syscalls necessary to update a sparse file range when using the fixed file option.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Answering the github thread -- it's indeed a simple change, I had it the same day you posted the issue. See below it's a bit cleaner. However, I want to first review "io_uring: buffer registration enhancements", and if it's good, for easier merging/etc I'd rather prefer to let it go first (even if partially).

Noah, want to give it a try? I've just sent a prep patch, with it you can implement it cleaner with one continue.

Absolutely. Will get on it ASAP.

Quoted 8 lines
Quoted 1 line

Perfect. Even better if you add a liburing test

Do you think the return value should not include files skipped?

i.e register fds[1, 2, 3, -1] with no errors returns 4. should fds[1, 2, -2, -1] return 3 or 4 do you think?

Personally think the latter makes more sense. Thoughts?

Let's just return @done, 4 in your case. Because otherwise locating which index has failed would be hell. And it's consistent with delete (i.e. -1).

Signature · 1 line
Pavel Begunkov