Skip to contentSkip to conversation

Let IORING_OP_FILES_UPDATE support to choose fixed file slots

PATCH In review Xiaoguang Wang 2 participants 3 messages May 30, 2022 – May 31, 2022 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Let IORING_OP_FILES_UPDATE support to choose fixed file slots

Allocate available direct descriptors instead of having the application pass free fixed file slots. To use it, pass IORING_FILE_INDEX_ALLOC to io_uring_prep_files_update(), then io_uring in kernel will store picked fixed file slots in fd array and let cqe return the number of slots allocated.

---
V2:
Update io_uring_prep_close(3) and io_uring_prep_files_update(3)'s man. Skip test in case old kernel does not support IORING_FILE_INDEX_ALLOC.

---
 man/io_uring_prep_close.3        |  10 +++
 man/io_uring_prep_files_update.3 |  10 +++
 src/include/liburing.h           |   8 +++
 src/include/liburing/io_uring.h  |   6 ++
 test/Makefile                    |   1 +
 test/file-update-index-alloc.c   | 139 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 174 insertions(+)
 create mode 100644 test/file-update-index-alloc.c
man/io_uring_prep_close.3 +10 -0
index 055e824..9e924a1 100644
@@ -15,6 +15,9 @@ io_uring_prep_close  - prepare a file descriptor close request
 .BI "
 .BI "void io_uring_prep_close_direct(struct io_uring_sqe *" sqe ","
 .BI "                                unsigned " file_index ");"
+.BI "
+.BI "void io_uring_prep_close_all(struct io_uring_sqe *" sqe ","
+.BI "                             int " fd ", unsigned " file_index ");"
 .PP
 .SH DESCRIPTION
 .PP
@@ -32,6 +35,13 @@ argument instead of the
 This is identical to unregistering the direct descriptor, and is provided as
 a convenience.
 
+For a close request which also has a direct descriptor, the
+.BR io_uring_prep_close_all(3)
+function is identical to close file descriptor indicated by
+.I fd
+and unregister the direct descriptor specified by the
+.I file_index.
+
 This function prepares an async
 .BR close (2)
 request. See that man page for details.
man/io_uring_prep_files_update.3 +10 -0
index c9b88b8..95c6c4f 100644
@@ -35,6 +35,16 @@ entry is updated and then removed from the table. This operation is equivalent t
 first unregistering that entry and then inserting a new one, just bundled into
 one combined operation.
 
+If
+.I offset
+is specified as IORING_FILE_INDEX_ALLOC, io_uring will allocate free direct
+descriptors instead of having the application to pass, and store allocated
+direct descriptors into
+.I fds
+array,
+.I cqe->res
+will return the number of direct descriptors allocated.
+
 .SH RETURN VALUE
 None
 .SH ERRORS
src/include/liburing.h +8 -0
index 6429dff..bc82d99 100644
@@ -614,6 +614,14 @@ static inline void io_uring_prep_close_direct(struct io_uring_sqe *sqe,
 	__io_uring_set_target_fixed_file(sqe, file_index);
 }
 
+static inline void io_uring_prep_close_all(struct io_uring_sqe *sqe,
+					   int fd, unsigned file_index)
+{
+	io_uring_prep_close(sqe, fd);
+	__io_uring_set_target_fixed_file(sqe, file_index);
+	sqe->close_flags = IORING_CLOSE_FD_AND_FILE_SLOT;
+}
+
 static inline void io_uring_prep_read(struct io_uring_sqe *sqe, int fd,
 				      void *buf, unsigned nbytes, __u64 offset)
 {
src/include/liburing/io_uring.h +6 -0
index 2f391c9..15d9fbd 100644
@@ -50,6 +50,7 @@ struct io_uring_sqe {
 		__u32		unlink_flags;
 		__u32		hardlink_flags;
 		__u32		xattr_flags;
+		__u32		close_flags;
 	};
 	__u64	user_data;	/* data to be passed back at completion time */
 	/* pack this to avoid bogus arm OABI complaints */
@@ -78,6 +79,11 @@ struct io_uring_sqe {
  */
 #define IORING_FILE_INDEX_ALLOC		(~0U)
 
+/*
+ * close flags, store in sqe->close_flags.
+ */
+#define IORING_CLOSE_FD_AND_FILE_SLOT	(1U << 0)
+
 enum {
 	IOSQE_FIXED_FILE_BIT,
 	IOSQE_IO_DRAIN_BIT,
test/Makefile +1 -0
index 51c35a9..ab031e0 100644
@@ -79,6 +79,7 @@ test_srcs := \
 	files-exit-hang-poll.c \
 	files-exit-hang-timeout.c \
 	file-update.c \
+	file-update-index-alloc.c \
 	file-verify.c \
 	fixed-buf-iter.c \
 	fixed-link.c \
test/file-update-index-alloc.c new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
2.14.4.44.g2045bb6
maintainer
#1 lore raw
Quoted 5 lines · Xiaoguang Wang

Allocate available direct descriptors instead of having the application pass free fixed file slots. To use it, pass IORING_FILE_INDEX_ALLOC to io_uring_prep_files_update(), then io_uring in kernel will store picked fixed file slots in fd array and let cqe return the number of slots allocated.

Thanks, applied and made a few tweaks. Most notably renaming the helper to io_uring_prep_close_direct_unregister() which is pretty long but more descriptive than _all() which doesn't really tell us anything useful I think.

Please check and see if you agree with that, and the man page tweak as well.

Signature · 1 line
Jens Axboe
#2 lore raw

hi,

Quoted 10 lines · Xiaoguang Wang
Quoted 5 lines

Allocate available direct descriptors instead of having the application pass free fixed file slots. To use it, pass IORING_FILE_INDEX_ALLOC to io_uring_prep_files_update(), then io_uring in kernel will store picked fixed file slots in fd array and let cqe return the number of slots allocated.

Thanks, applied and made a few tweaks. Most notably renaming the helper to io_uring_prep_close_direct_unregister() which is pretty long but more descriptive than _all() which doesn't really tell us anything useful I think.

Agree, thanks.

Quoted 3 lines

Please check and see if you agree with that, and the man page tweak as well.

Yeah, I have checked it, thanks.

Regards,
Xiaoguang Wang

Quoted 1 line