The server of a target_core_uring device can fetch a command for each tag. It cannot report the result, so the command never completes and the tag is never used again. Add the TCMUR_IO_COMMIT_AND_FETCH io_uring command, which completes the command of a tag with GOOD or CHECK CONDITION status and fetches the next one in the same call. Count references on struct tcmur_cmd and complete the target command when the last one goes, as later patches add users that can outlive the commit. Signed-off-by: Qiliang Yuan --- drivers/target/target_core_uring.c | 47 ++++++++++++++++++++++++++++++++++ include/uapi/linux/target_core_uring.h | 9 +++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c index 85c9a1ae84433..770bb665fbe00 100644 --- a/drivers/target/target_core_uring.c +++ b/drivers/target/target_core_uring.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,8 @@ struct tcmur_dev; * @sgl: Data pages of the command * @sgl_nents: Number of entries in @sgl * @desc: What the server is asked to do + * @ref: Held by the tag or the pending list + * @status: SAM status to complete @se_cmd with */ struct tcmur_cmd { struct list_head node; @@ -49,6 +52,8 @@ struct tcmur_cmd { struct scatterlist *sgl; u32 sgl_nents; struct tcmur_io_desc desc; + refcount_t ref; + u8 status; }; /** @@ -130,6 +135,22 @@ static struct tcmur_queue *tcmur_cmd_queue(struct tcmur_dev *udev, return &udev->queues[(unsigned int)se_cmd->cpuid % udev->nr_queues]; } +static void tcmur_cmd_put(struct tcmur_cmd *tcmd) +{ + if (!refcount_dec_and_test(&tcmd->ref)) + return; + + target_complete_cmd(tcmd->se_cmd, tcmd->status); + kmem_cache_free(tcmur_cmd_cache, tcmd); +} + +/* Drop the reference of the tag or of the pending list and set the result */ +static void tcmur_cmd_end(struct tcmur_cmd *tcmd, u8 status) +{ + tcmd->status = status; + tcmur_cmd_put(tcmd); +} + /* Hand @tcmd to the server through the tag @io */ static void tcmur_io_start(struct tcmur_queue *q, struct tcmur_io *io, struct tcmur_cmd *tcmd) @@ -156,6 +177,8 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd) struct io_uring_cmd *ucmd = NULL; struct tcmur_io *io; + refcount_set(&tcmd->ref, 1); + spin_lock(&q->lock); io = list_first_entry_or_null(&q->idle, struct tcmur_io, node); if (io) { @@ -262,6 +285,24 @@ static int tcmur_fetch(struct tcmur_queue *q, struct tcmur_io *io, return ret; } +static int tcmur_commit(struct tcmur_queue *q, struct tcmur_io *io, int result) +{ + struct tcmur_cmd *tcmd; + + spin_lock(&q->lock); + if (!io->owned) { + spin_unlock(&q->lock); + return -EINVAL; + } + tcmd = io->cmd; + io->cmd = NULL; + io->owned = false; + spin_unlock(&q->lock); + + tcmur_cmd_end(tcmd, result ? SAM_STAT_CHECK_CONDITION : SAM_STAT_GOOD); + return 0; +} + static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd, unsigned int issue_flags) { @@ -272,6 +313,7 @@ static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd, u16 tag = READ_ONCE(ioc->tag); struct tcmur_queue *q; struct tcmur_io *io; + int ret; if (q_id >= udev->nr_queues || tag >= udev->queue_depth) return -EINVAL; @@ -282,6 +324,11 @@ static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd, switch (ucmd->cmd_op) { case TCMUR_IO_FETCH: return tcmur_fetch(q, io, ucmd, issue_flags); + case TCMUR_IO_COMMIT_AND_FETCH: + ret = tcmur_commit(q, io, READ_ONCE(ioc->result)); + if (ret) + return ret; + return tcmur_fetch(q, io, ucmd, issue_flags); default: return -EOPNOTSUPP; } diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h index 9ce871d2e2087..a662a23fca9ab 100644 --- a/include/uapi/linux/target_core_uring.h +++ b/include/uapi/linux/target_core_uring.h @@ -43,21 +43,26 @@ struct tcmur_dev_info { * struct tcmur_io_cmd - Payload of the io_uring commands, placed in sqe->cmd * @q_id: Queue of the tag * @tag: Tag the command is for - * @reserved: Zero + * @result: TCMUR_IO_COMMIT_AND_FETCH: 0, or a negative errno if the + * command failed * @reserved1: Zero */ struct tcmur_io_cmd { __u16 q_id; __u16 tag; - __s32 reserved; + __s32 result; __u64 reserved1; }; /* * TCMUR_IO_FETCH: wait for a command on (q_id, tag). The CQE result is 0 * once the descriptor of the tag holds a command. + * + * TCMUR_IO_COMMIT_AND_FETCH: complete the command of (q_id, tag) with + * @result, then wait for the next one. */ #define TCMUR_IO_FETCH _IOWR(0xBB, 0x08, struct tcmur_io_cmd) +#define TCMUR_IO_COMMIT_AND_FETCH _IOWR(0xBB, 0x09, struct tcmur_io_cmd) enum tcmur_io_op { TCMUR_OP_READ, -- 2.43.0