Skip to contentSkip to conversation

io_uring: let IORING_OP_FILES_UPDATE support to choose fixed file slot

RFC In review Xiaoguang Wang 3 participants 7 messages May 26, 2022 – May 30, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 io_uring: let IORING_OP_FILES_UPDATE support to choose fixed file slot

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

TODO list:
Need to prepare liburing corresponding helpers.

---
 fs/io_uring.c                 | 50 ++++++++++++++++++++++++++++++++++---------
 include/uapi/linux/io_uring.h |  1 +
 2 files changed, 41 insertions(+), 10 deletions(-)
fs/io_uring.c +40 -10
index 9f1c682d7caf..d77e6bbec81c 100644
@@ -680,6 +680,7 @@ struct io_rsrc_update {
 	u64				arg;
 	u32				nr_args;
 	u32				offset;
+	u32				flags;
 };
 
 struct io_fadvise {
@@ -7970,14 +7971,23 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
 	return 0;
 }
 
+#define IORING_FILES_UPDATE_INDEX_ALLOC 1
+
 static int io_rsrc_update_prep(struct io_kiocb *req,
 				const struct io_uring_sqe *sqe)
 {
+	u32 flags = READ_ONCE(sqe->files_update_flags);
+
 	if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
 		return -EINVAL;
-	if (sqe->rw_flags || sqe->splice_fd_in)
+	if (sqe->splice_fd_in)
+		return -EINVAL;
+	if (flags & ~IORING_FILES_UPDATE_INDEX_ALLOC)
+		return -EINVAL;
+	if ((flags & IORING_FILES_UPDATE_INDEX_ALLOC) && READ_ONCE(sqe->len) != 1)
 		return -EINVAL;
 
+	req->rsrc_update.flags = flags;
 	req->rsrc_update.offset = READ_ONCE(sqe->off);
 	req->rsrc_update.nr_args = READ_ONCE(sqe->len);
 	if (!req->rsrc_update.nr_args)
@@ -7990,18 +8000,38 @@ static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
 {
 	struct io_ring_ctx *ctx = req->ctx;
 	struct io_uring_rsrc_update2 up;
+	struct file *file;
 	int ret;
 
-	up.offset = req->rsrc_update.offset;
-	up.data = req->rsrc_update.arg;
-	up.nr = 0;
-	up.tags = 0;
-	up.resv = 0;
-	up.resv2 = 0;
+	if (req->rsrc_update.flags & IORING_FILES_UPDATE_INDEX_ALLOC) {
+		int fd;
 
-	io_ring_submit_lock(ctx, issue_flags);
-	ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
-					&up, req->rsrc_update.nr_args);
+		if (copy_from_user(&fd, (int *)req->rsrc_update.arg, sizeof(fd))) {
+			ret = -EFAULT;
+			goto out;
+		}
+		file = fget(fd);
+		if (!file) {
+			ret = -EBADF;
+			goto out;
+		}
+		ret = io_fixed_fd_install(req, issue_flags, file,
+					  IORING_FILE_INDEX_ALLOC);
+	} else {
+		up.offset = req->rsrc_update.offset;
+		up.data = req->rsrc_update.arg;
+		up.nr = 0;
+		up.tags = 0;
+		up.resv = 0;
+		up.resv2 = 0;
+
+		io_ring_submit_lock(ctx, issue_flags);
+		ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
+				&up, req->rsrc_update.nr_args);
+		io_ring_submit_unlock(ctx, issue_flags);
+	}
+
+out:
 	io_ring_submit_unlock(ctx, issue_flags);
 
 	if (ret < 0)
include/uapi/linux/io_uring.h +1 -0
index 53e7dae92e42..b4af01d56d7c 100644
@@ -47,6 +47,7 @@ struct io_uring_sqe {
 		__u32		unlink_flags;
 		__u32		hardlink_flags;
 		__u32		xattr_flags;
+		__u32		files_update_flags;
 	};
 	__u64	user_data;	/* data to be passed back at completion time */
 	/* pack this to avoid bogus arm OABI complaints */
Signature · 1 line
2.14.4.44.g2045bb6
#1 lore raw

Hi Xiaoguang,

Quoted 42 lines · Xiaoguang Wang

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

TODO list:
Need to prepare liburing corresponding helpers.

---
  fs/io_uring.c                 | 50 ++++++++++++++++++++++++++++++++++---------
  include/uapi/linux/io_uring.h |  1 +
  2 files changed, 41 insertions(+), 10 deletions(-)
fs/io_uring.c +3 -0
index 9f1c682d7caf..d77e6bbec81c 100644
@@ -680,6 +680,7 @@ struct io_rsrc_update {
  	u64				arg;
  	u32				nr_args;
  	u32				offset;
+	u32				flags;
  };
 
  struct io_fadvise {
@@ -7970,14 +7971,23 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
  	return 0;
  }
 
+#define IORING_FILES_UPDATE_INDEX_ALLOC 1
+
  static int io_rsrc_update_prep(struct io_kiocb *req,
  				const struct io_uring_sqe *sqe)
  {

fs/io_uring.c static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) · line 7987

Quoted 10 lines
fs/io_uring.c +7 -1
+	u32 flags = READ_ONCE(sqe->files_update_flags);
+
  	if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
  		return -EINVAL;
-	if (sqe->rw_flags || sqe->splice_fd_in)
+	if (sqe->splice_fd_in)
+		return -EINVAL;
+	if (flags & ~IORING_FILES_UPDATE_INDEX_ALLOC)
+		return -EINVAL;
+	if ((flags & IORING_FILES_UPDATE_INDEX_ALLOC) && READ_ONCE(sqe->len) != 1)

How about allowing multiple fd update in IORING_FILES_UPDATE_INDEX_ALLOC
case? For example, using the sqe->addr(the fd array) to store the slots we allocated, and let cqe return the number of slots allocated. By the way, another way, we can levarage up->offset == IORING_FILE_INDEX_ALLOC
to do the mode check since seems it is not used in this mode. Though
I'm not sure that is better..

Quoted 16 lines
fs/io_uring.c +2 -3
  		return -EINVAL;
 
+	req->rsrc_update.flags = flags;
  	req->rsrc_update.offset = READ_ONCE(sqe->off);
  	req->rsrc_update.nr_args = READ_ONCE(sqe->len);
  	if (!req->rsrc_update.nr_args)
@@ -7990,18 +8000,38 @@ static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
  {
  	struct io_ring_ctx *ctx = req->ctx;
  	struct io_uring_rsrc_update2 up;
+	struct file *file;
  	int ret;
 
-	up.offset = req->rsrc_update.offset;
-	up.data = req->rsrc_update.arg;
-	up.nr = 0;

fs/io_uring.c static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) · line 8009

Quoted 10 lines
fs/io_uring.c +3 -6
-	up.tags = 0;
-	up.resv = 0;
-	up.resv2 = 0;
+	if (req->rsrc_update.flags & IORING_FILES_UPDATE_INDEX_ALLOC) {
+		int fd;
 
-	io_ring_submit_lock(ctx, issue_flags);
-	ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
-					&up, req->rsrc_update.nr_args);
+		if (copy_from_user(&fd, (int *)req->rsrc_update.arg, sizeof(fd))) {
                                          ^ (void __user *) ?

Regards,
Hao

#2 lore raw

hi Hao,

Quoted 48 lines

Hi Xiaoguang,

Quoted 45 lines · Xiaoguang Wang

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

TODO list:
     Need to prepare liburing corresponding helpers.

---
  fs/io_uring.c                 | 50 ++++++++++++++++++++++++++++++++++--------- include/uapi/linux/io_uring.h |  1 +

  2 files changed, 41 insertions(+), 10 deletions(-)
fs/io_uring.c +1 -0
index 9f1c682d7caf..d77e6bbec81c 100644
@@ -680,6 +680,7 @@ struct io_rsrc_update {
       u64                arg;
       u32                nr_args;
       u32                offset;
+    u32                flags;
   };
     struct io_fadvise {
@@ -7970,14 +7971,23 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
       return 0;
   }

  +#define IORING_FILES_UPDATE_INDEX_ALLOC 1
+
  static int io_rsrc_update_prep(struct io_kiocb *req,
                  const struct io_uring_sqe *sqe)
  {
+    u32 flags = READ_ONCE(sqe->files_update_flags);
+
      if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) return -EINVAL;
-    if (sqe->rw_flags || sqe->splice_fd_in)

Quoted 8 lines
Quoted 5 lines

+    if (sqe->splice_fd_in)
+        return -EINVAL;
+    if (flags & ~IORING_FILES_UPDATE_INDEX_ALLOC)
+        return -EINVAL;
+    if ((flags & IORING_FILES_UPDATE_INDEX_ALLOC) && READ_ONCE(sqe->len) != 1)

How about allowing multiple fd update in IORING_FILES_UPDATE_INDEX_ALLOC
case? For example, using the sqe->addr(the fd array) to store the slots we allocated, and let cqe return the number of slots allocated.

Good idea, I'll try in patch v2, thanks.
Jens, any comments about this patch? At least It's really helpful to our internal apps based on io_uring :)

Regards,
Xiaoguang Wang

Quoted 32 lines

By the way, another way, we can levarage up->offset == IORING_FILE_INDEX_ALLOC to do the mode check since seems it is not used in this mode. Though I'm not sure that is better..

Quoted 13 lines

          return -EINVAL;
  +    req->rsrc_update.flags = flags; req->rsrc_update.offset = READ_ONCE(sqe->off); req->rsrc_update.nr_args = READ_ONCE(sqe->len); if (!req->rsrc_update.nr_args)

fs/io_uring.c +1 -1
@@ -7990,18 +8000,38 @@ static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
   {
       struct io_ring_ctx *ctx = req->ctx;
       struct io_uring_rsrc_update2 up;
+    struct file *file;
       int ret;
   -    up.offset = req->rsrc_update.offset;
-    up.data = req->rsrc_update.arg;

fs/io_uring.c static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) · line 8009

Quoted 10 lines
fs/io_uring.c +3 -6
-    up.nr = 0;
-    up.tags = 0;
-    up.resv = 0;
-    up.resv2 = 0;
+    if (req->rsrc_update.flags & IORING_FILES_UPDATE_INDEX_ALLOC) {
+        int fd;
   -    io_ring_submit_lock(ctx, issue_flags);
-    ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
-                    &up, req->rsrc_update.nr_args);
+        if (copy_from_user(&fd, (int *)req->rsrc_update.arg, sizeof(fd))) {

                                          ^ (void __user *) ?

Regards,
Hao

maintainer
#3 lore raw
Quoted 58 lines · Xiaoguang Wang

hi Hao,

Quoted 48 lines

Hi Xiaoguang,

Quoted 45 lines · Xiaoguang Wang

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

TODO list:
Need to prepare liburing corresponding helpers.

---
  fs/io_uring.c                 | 50 ++++++++++++++++++++++++++++++++++---------
  include/uapi/linux/io_uring.h |  1 +
  2 files changed, 41 insertions(+), 10 deletions(-)
fs/io_uring.c +4 -1
index 9f1c682d7caf..d77e6bbec81c 100644
@@ -680,6 +680,7 @@ struct io_rsrc_update {
      u64                arg;
      u32                nr_args;
      u32                offset;
+    u32                flags;
  };
    struct io_fadvise {
@@ -7970,14 +7971,23 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
      return 0;
  }
  +#define IORING_FILES_UPDATE_INDEX_ALLOC 1
+
  static int io_rsrc_update_prep(struct io_kiocb *req,
                  const struct io_uring_sqe *sqe)
  {
+    u32 flags = READ_ONCE(sqe->files_update_flags);
+
      if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
          return -EINVAL;
-    if (sqe->rw_flags || sqe->splice_fd_in)
Quoted 8 lines

fs/io_uring.c static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) · line 7987

Quoted 5 lines
fs/io_uring.c +5 -0
+    if (sqe->splice_fd_in)
+        return -EINVAL;
+    if (flags & ~IORING_FILES_UPDATE_INDEX_ALLOC)
+        return -EINVAL;
+    if ((flags & IORING_FILES_UPDATE_INDEX_ALLOC) && READ_ONCE(sqe->len) != 1)

How about allowing multiple fd update in IORING_FILES_UPDATE_INDEX_ALLOC
case? For example, using the sqe->addr(the fd array) to store the slots we allocated, and let cqe return the number of slots allocated.

Quoted 3 lines

Good idea, I'll try in patch v2, thanks.
Jens, any comments about this patch? At least It's really helpful to our internal apps based on io_uring :)

I like this suggestion too, other thoughts in reply to the original.

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw
Quoted 59 lines · Jens Axboe
Quoted 58 lines · Xiaoguang Wang

hi Hao,

Quoted 56 lines

Hi Xiaoguang,

Quoted 40 lines · Xiaoguang Wang

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

TODO list:
Need to prepare liburing corresponding helpers.

---
  fs/io_uring.c                 | 50 ++++++++++++++++++++++++++++++++++---------
  include/uapi/linux/io_uring.h |  1 +
  2 files changed, 41 insertions(+), 10 deletions(-)
fs/io_uring.c +2 -0
index 9f1c682d7caf..d77e6bbec81c 100644
@@ -680,6 +680,7 @@ struct io_rsrc_update {
      u64                arg;
      u32                nr_args;
      u32                offset;
+    u32                flags;
  };
    struct io_fadvise {
@@ -7970,14 +7971,23 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
      return 0;
  }
  +#define IORING_FILES_UPDATE_INDEX_ALLOC 1
+
  static int io_rsrc_update_prep(struct io_kiocb *req,
                  const struct io_uring_sqe *sqe)
  {

fs/io_uring.c static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) · line 7986

Quoted 10 lines
fs/io_uring.c +7 -1
+    u32 flags = READ_ONCE(sqe->files_update_flags);
+
      if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
          return -EINVAL;
-    if (sqe->rw_flags || sqe->splice_fd_in)
+    if (sqe->splice_fd_in)
+        return -EINVAL;
+    if (flags & ~IORING_FILES_UPDATE_INDEX_ALLOC)
+        return -EINVAL;
+    if ((flags & IORING_FILES_UPDATE_INDEX_ALLOC) && READ_ONCE(sqe->len) != 1)

How about allowing multiple fd update in IORING_FILES_UPDATE_INDEX_ALLOC
case? For example, using the sqe->addr(the fd array) to store the slots we allocated, and let cqe return the number of slots allocated.

Quoted 5 lines
Quoted 3 lines

Good idea, I'll try in patch v2, thanks.
Jens, any comments about this patch? At least It's really helpful to our internal apps based on io_uring :)

I like this suggestion too, other thoughts in reply to the original.

BTW, if you have time, would be great to get this done for 5.19. It makes the whole thing more consistent and makes it so that 5.19 has (hopefully) all the alloc bits for direct descriptors.

Signature · 1 line
Jens Axboe
#5 lore raw

hi,

Quoted 59 lines · Jens Axboe
Quoted 58 lines · Xiaoguang Wang
Quoted 57 lines

hi Hao,

Quoted 55 lines

Hi Xiaoguang,

Quoted 40 lines · Xiaoguang Wang

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

TODO list:
Need to prepare liburing corresponding helpers.

---
  fs/io_uring.c                 | 50 ++++++++++++++++++++++++++++++++++---------
  include/uapi/linux/io_uring.h |  1 +
  2 files changed, 41 insertions(+), 10 deletions(-)
fs/io_uring.c +2 -0
index 9f1c682d7caf..d77e6bbec81c 100644
@@ -680,6 +680,7 @@ struct io_rsrc_update {
      u64                arg;
      u32                nr_args;
      u32                offset;
+    u32                flags;
  };
    struct io_fadvise {
@@ -7970,14 +7971,23 @@ static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
      return 0;
  }
  +#define IORING_FILES_UPDATE_INDEX_ALLOC 1
+
  static int io_rsrc_update_prep(struct io_kiocb *req,
                  const struct io_uring_sqe *sqe)
  {

fs/io_uring.c static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) · line 7986

Quoted 10 lines
fs/io_uring.c +7 -1
+    u32 flags = READ_ONCE(sqe->files_update_flags);
+
      if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
          return -EINVAL;
-    if (sqe->rw_flags || sqe->splice_fd_in)
+    if (sqe->splice_fd_in)
+        return -EINVAL;
+    if (flags & ~IORING_FILES_UPDATE_INDEX_ALLOC)
+        return -EINVAL;
+    if ((flags & IORING_FILES_UPDATE_INDEX_ALLOC) && READ_ONCE(sqe->len) != 1)

How about allowing multiple fd update in IORING_FILES_UPDATE_INDEX_ALLOC
case? For example, using the sqe->addr(the fd array) to store the slots we allocated, and let cqe return the number of slots allocated.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Good idea, I'll try in patch v2, thanks.
Jens, any comments about this patch? At least It's really helpful to our internal apps based on io_uring :)

I like this suggestion too, other thoughts in reply to the original.

BTW, if you have time, would be great to get this done for 5.19. It makes the whole thing more consistent and makes it so that 5.19 has (hopefully) all the alloc bits for direct descriptors.

Yeah, I have free time now, will prepare new version today.

Regards,
Xiaoguang Wang

Quoted 1 line
maintainer
#6 lore raw
In reply to Xiaoguang Wang
Quoted 11 lines · Xiaoguang Wang

One big issue with file registration feature is that it needs user space apps to maintain free slot info about io_uring's fixed file table, which really is a burden for development. Now since io_uring starts to choose free file slot for user space apps by using IORING_FILE_INDEX_ALLOC flag in accept or open operations, but they need app to uses direct accept or direct open, which as far as I know, some apps are not prepared to use direct accept or open yet.

To support apps, who still need real fds, use registration feature easier, let IORING_OP_FILES_UPDATE support to choose fixed file slot, which will return free file slot in cqe->res.

This looks good. In retrospect, the direct open/accept/etc really should've just returned the direct descriptor in cqe->res, so we would not have this odd "0 for success when you pick a slot, <slot> in cqe->res if io_uring picks it". But this is consistent with the alloc case.

Do you have a liburing test case too? I think we should just get this done for 5.19 rather than spread it over multiple releases, since 5.19 introduced the alloc as well.

Signature · 1 line
Jens Axboe