io_uring: define a request type cleanup handler
authorJens Axboe <axboe@kernel.dk>
Tue, 24 May 2022 16:26:28 +0000 (10:26 -0600)
committerJens Axboe <axboe@kernel.dk>
Mon, 25 Jul 2022 00:39:11 +0000 (18:39 -0600)
This can move request type specific cleanup into a private handler,
removing the need for the core io_uring parts to know what types
they are dealing with.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
io_uring/io_uring.c

index 808acb854b660d57bc2d70e648ed751934fcc150..75d8c31a59d50ea82f6ab8d2d0a56374d43180b3 100644 (file)
@@ -1093,6 +1093,7 @@ struct io_op_def {
        int (*prep)(struct io_kiocb *, const struct io_uring_sqe *);
        int (*issue)(struct io_kiocb *, unsigned int);
        int (*prep_async)(struct io_kiocb *);
+       void (*cleanup)(struct io_kiocb *);
 };
 
 static const struct io_op_def io_op_defs[];
@@ -3433,6 +3434,13 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
        return 0;
 }
 
+static void io_readv_writev_cleanup(struct io_kiocb *req)
+{
+       struct io_async_rw *io = req->async_data;
+
+       kfree(io->free_iovec);
+}
+
 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
 {
        switch (ret) {
@@ -4391,7 +4399,15 @@ static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
        return 0;
 }
 
-static inline void __io_xattr_finish(struct io_kiocb *req)
+static void io_renameat_cleanup(struct io_kiocb *req)
+{
+       struct io_rename *ren = io_kiocb_to_cmd(req);
+
+       putname(ren->oldpath);
+       putname(ren->newpath);
+}
+
+static inline void io_xattr_cleanup(struct io_kiocb *req)
 {
        struct io_xattr *ix = io_kiocb_to_cmd(req);
 
@@ -4406,7 +4422,7 @@ static void io_xattr_finish(struct io_kiocb *req, int ret)
 {
        req->flags &= ~REQ_F_NEED_CLEANUP;
 
-       __io_xattr_finish(req);
+       io_xattr_cleanup(req);
        io_req_complete(req, ret);
 }
 
@@ -4675,6 +4691,13 @@ static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
        return 0;
 }
 
+static void io_unlinkat_cleanup(struct io_kiocb *req)
+{
+       struct io_unlink *ul = io_kiocb_to_cmd(req);
+
+       putname(ul->filename);
+}
+
 static int io_mkdirat_prep(struct io_kiocb *req,
                            const struct io_uring_sqe *sqe)
 {
@@ -4713,6 +4736,13 @@ static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags)
        return 0;
 }
 
+static void io_mkdirat_cleanup(struct io_kiocb *req)
+{
+       struct io_mkdir *md = io_kiocb_to_cmd(req);
+
+       putname(md->filename);
+}
+
 static int io_symlinkat_prep(struct io_kiocb *req,
                            const struct io_uring_sqe *sqe)
 {
@@ -4804,6 +4834,14 @@ static int io_linkat(struct io_kiocb *req, unsigned int issue_flags)
        return 0;
 }
 
+static void io_link_cleanup(struct io_kiocb *req)
+{
+       struct io_link *sl = io_kiocb_to_cmd(req);
+
+       putname(sl->oldpath);
+       putname(sl->newpath);
+}
+
 static void io_uring_cmd_work(struct io_kiocb *req, bool *locked)
 {
        struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req);
@@ -5314,6 +5352,14 @@ static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
        return io_openat2(req, issue_flags);
 }
 
+static void io_open_cleanup(struct io_kiocb *req)
+{
+       struct io_open *open = io_kiocb_to_cmd(req);
+
+       if (open->filename)
+               putname(open->filename);
+}
+
 static int io_remove_buffers_prep(struct io_kiocb *req,
                                  const struct io_uring_sqe *sqe)
 {
@@ -5725,6 +5771,14 @@ static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
        return 0;
 }
 
+static void io_statx_cleanup(struct io_kiocb *req)
+{
+       struct io_statx *sx = io_kiocb_to_cmd(req);
+
+       if (sx->filename)
+               putname(sx->filename);
+}
+
 static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        struct io_close *close = io_kiocb_to_cmd(req);
@@ -5897,6 +5951,13 @@ static int io_sendmsg_prep_async(struct io_kiocb *req)
        return ret;
 }
 
+static void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req)
+{
+       struct io_async_msghdr *io = req->async_data;
+
+       kfree(io->free_iov);
+}
+
 static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
        struct io_sr_msg *sr = io_kiocb_to_cmd(req);
@@ -7958,74 +8019,10 @@ static void io_clean_op(struct io_kiocb *req)
        }
 
        if (req->flags & REQ_F_NEED_CLEANUP) {
-               switch (req->opcode) {
-               case IORING_OP_READV:
-               case IORING_OP_READ_FIXED:
-               case IORING_OP_READ:
-               case IORING_OP_WRITEV:
-               case IORING_OP_WRITE_FIXED:
-               case IORING_OP_WRITE: {
-                       struct io_async_rw *io = req->async_data;
-
-                       kfree(io->free_iovec);
-                       break;
-                       }
-               case IORING_OP_RECVMSG:
-               case IORING_OP_SENDMSG: {
-                       struct io_async_msghdr *io = req->async_data;
-
-                       kfree(io->free_iov);
-                       break;
-                       }
-               case IORING_OP_OPENAT:
-               case IORING_OP_OPENAT2: {
-                       struct io_open *open = io_kiocb_to_cmd(req);
-
-                       if (open->filename)
-                               putname(open->filename);
-                       break;
-                       }
-               case IORING_OP_RENAMEAT: {
-                       struct io_rename *ren = io_kiocb_to_cmd(req);
-
-                       putname(ren->oldpath);
-                       putname(ren->newpath);
-                       break;
-                       }
-               case IORING_OP_UNLINKAT: {
-                       struct io_unlink *ul = io_kiocb_to_cmd(req);
-
-                       putname(ul->filename);
-                       break;
-                       }
-               case IORING_OP_MKDIRAT: {
-                       struct io_mkdir *md = io_kiocb_to_cmd(req);
-
-                       putname(md->filename);
-                       break;
-                       }
-               case IORING_OP_SYMLINKAT:
-               case IORING_OP_LINKAT: {
-                       struct io_link *hl = io_kiocb_to_cmd(req);
-
-                       putname(hl->oldpath);
-                       putname(hl->newpath);
-                       break;
-                       }
-               case IORING_OP_STATX: {
-                       struct io_statx *sx = io_kiocb_to_cmd(req);
+               const struct io_op_def *def = &io_op_defs[req->opcode];
 
-                       if (sx->filename)
-                               putname(sx->filename);
-                       break;
-                       }
-               case IORING_OP_SETXATTR:
-               case IORING_OP_FSETXATTR:
-               case IORING_OP_GETXATTR:
-               case IORING_OP_FGETXATTR:
-                       __io_xattr_finish(req);
-                       break;
-               }
+               if (def->cleanup)
+                       def->cleanup(req);
        }
        if ((req->flags & REQ_F_POLLED) && req->apoll) {
                kfree(req->apoll->double_poll);
@@ -12838,6 +12835,7 @@ static const struct io_op_def io_op_defs[] = {
                .prep                   = io_prep_rw,
                .issue                  = io_read,
                .prep_async             = io_readv_prep_async,
+               .cleanup                = io_readv_writev_cleanup,
        },
        [IORING_OP_WRITEV] = {
                .needs_file             = 1,
@@ -12852,6 +12850,7 @@ static const struct io_op_def io_op_defs[] = {
                .prep                   = io_prep_rw,
                .issue                  = io_write,
                .prep_async             = io_writev_prep_async,
+               .cleanup                = io_readv_writev_cleanup,
        },
        [IORING_OP_FSYNC] = {
                .needs_file             = 1,
@@ -12911,6 +12910,9 @@ static const struct io_op_def io_op_defs[] = {
                .prep                   = io_sendmsg_prep,
                .issue                  = io_sendmsg,
                .prep_async             = io_sendmsg_prep_async,
+#if defined(CONFIG_NET)
+               .cleanup                = io_sendmsg_recvmsg_cleanup,
+#endif
        },
        [IORING_OP_RECVMSG] = {
                .needs_file             = 1,
@@ -12922,6 +12924,9 @@ static const struct io_op_def io_op_defs[] = {
                .prep                   = io_recvmsg_prep,
                .issue                  = io_recvmsg,
                .prep_async             = io_recvmsg_prep_async,
+#if defined(CONFIG_NET)
+               .cleanup                = io_sendmsg_recvmsg_cleanup,
+#endif
        },
        [IORING_OP_TIMEOUT] = {
                .audit_skip             = 1,
@@ -12972,6 +12977,7 @@ static const struct io_op_def io_op_defs[] = {
        [IORING_OP_OPENAT] = {
                .prep                   = io_openat_prep,
                .issue                  = io_openat,
+               .cleanup                = io_open_cleanup,
        },
        [IORING_OP_CLOSE] = {
                .prep                   = io_close_prep,
@@ -12987,6 +12993,7 @@ static const struct io_op_def io_op_defs[] = {
                .audit_skip             = 1,
                .prep                   = io_statx_prep,
                .issue                  = io_statx,
+               .cleanup                = io_statx_cleanup,
        },
        [IORING_OP_READ] = {
                .needs_file             = 1,
@@ -13046,6 +13053,7 @@ static const struct io_op_def io_op_defs[] = {
        [IORING_OP_OPENAT2] = {
                .prep                   = io_openat2_prep,
                .issue                  = io_openat2,
+               .cleanup                = io_open_cleanup,
        },
        [IORING_OP_EPOLL_CTL] = {
                .unbound_nonreg_file    = 1,
@@ -13089,22 +13097,27 @@ static const struct io_op_def io_op_defs[] = {
        [IORING_OP_RENAMEAT] = {
                .prep                   = io_renameat_prep,
                .issue                  = io_renameat,
+               .cleanup                = io_renameat_cleanup,
        },
        [IORING_OP_UNLINKAT] = {
                .prep                   = io_unlinkat_prep,
                .issue                  = io_unlinkat,
+               .cleanup                = io_unlinkat_cleanup,
        },
        [IORING_OP_MKDIRAT] = {
                .prep                   = io_mkdirat_prep,
                .issue                  = io_mkdirat,
+               .cleanup                = io_mkdirat_cleanup,
        },
        [IORING_OP_SYMLINKAT] = {
                .prep                   = io_symlinkat_prep,
                .issue                  = io_symlinkat,
+               .cleanup                = io_link_cleanup,
        },
        [IORING_OP_LINKAT] = {
                .prep                   = io_linkat_prep,
                .issue                  = io_linkat,
+               .cleanup                = io_link_cleanup,
        },
        [IORING_OP_MSG_RING] = {
                .needs_file             = 1,
@@ -13116,19 +13129,23 @@ static const struct io_op_def io_op_defs[] = {
                .needs_file = 1,
                .prep                   = io_fsetxattr_prep,
                .issue                  = io_fsetxattr,
+               .cleanup                = io_xattr_cleanup,
        },
        [IORING_OP_SETXATTR] = {
                .prep                   = io_setxattr_prep,
                .issue                  = io_setxattr,
+               .cleanup                = io_xattr_cleanup,
        },
        [IORING_OP_FGETXATTR] = {
                .needs_file = 1,
                .prep                   = io_fgetxattr_prep,
                .issue                  = io_fgetxattr,
+               .cleanup                = io_xattr_cleanup,
        },
        [IORING_OP_GETXATTR] = {
                .prep                   = io_getxattr_prep,
                .issue                  = io_getxattr,
+               .cleanup                = io_xattr_cleanup,
        },
        [IORING_OP_SOCKET] = {
                .audit_skip             = 1,