ublk: add timeout handler
[linux-block.git] / drivers / block / ublk_drv.c
index 1223fcbfc6c9ccb686a5dcadbc5fe7250264483d..e96309f2e1ada14e1aab06afc5231c36cdb903ff 100644 (file)
@@ -53,7 +53,8 @@
                | UBLK_F_NEED_GET_DATA \
                | UBLK_F_USER_RECOVERY \
                | UBLK_F_USER_RECOVERY_REISSUE \
-               | UBLK_F_UNPRIVILEGED_DEV)
+               | UBLK_F_UNPRIVILEGED_DEV \
+               | UBLK_F_CMD_IOCTL_ENCODE)
 
 /* All UBLK_PARAM_TYPE_* should be included here */
 #define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | \
@@ -128,6 +129,7 @@ struct ublk_queue {
        unsigned long io_addr;  /* mapped vm address */
        unsigned int max_io_sz;
        bool force_abort;
+       bool timeout;
        unsigned short nr_io_ready;     /* how many ios setup */
        struct ublk_device *dev;
        struct ublk_io ios[];
@@ -893,6 +895,22 @@ static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
        }
 }
 
+static enum blk_eh_timer_return ublk_timeout(struct request *rq)
+{
+       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+
+       if (ubq->flags & UBLK_F_UNPRIVILEGED_DEV) {
+               if (!ubq->timeout) {
+                       send_sig(SIGKILL, ubq->ubq_daemon, 0);
+                       ubq->timeout = true;
+               }
+
+               return BLK_EH_DONE;
+       }
+
+       return BLK_EH_RESET_TIMER;
+}
+
 static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
                const struct blk_mq_queue_data *bd)
 {
@@ -952,6 +970,7 @@ static const struct blk_mq_ops ublk_mq_ops = {
        .queue_rq       = ublk_queue_rq,
        .init_hctx      = ublk_init_hctx,
        .init_request   = ublk_init_rq,
+       .timeout        = ublk_timeout,
 };
 
 static int ublk_ch_open(struct inode *inode, struct file *filp)
@@ -1253,6 +1272,19 @@ static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
        ublk_queue_cmd(ubq, req);
 }
 
+static inline int ublk_check_cmd_op(u32 cmd_op)
+{
+       u32 ioc_type = _IOC_TYPE(cmd_op);
+
+       if (IS_ENABLED(CONFIG_BLKDEV_UBLK_LEGACY_OPCODES) && ioc_type != 'u')
+               return -EOPNOTSUPP;
+
+       if (ioc_type != 'u' && ioc_type != 0)
+               return -EOPNOTSUPP;
+
+       return 0;
+}
+
 static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
 {
        struct ublksrv_io_cmd *ub_cmd = (struct ublksrv_io_cmd *)cmd->cmd;
@@ -1294,10 +1326,15 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
         * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
         */
        if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA))
-                       ^ (cmd_op == UBLK_IO_NEED_GET_DATA))
+                       ^ (_IOC_NR(cmd_op) == UBLK_IO_NEED_GET_DATA))
+               goto out;
+
+       ret = ublk_check_cmd_op(cmd_op);
+       if (ret)
                goto out;
 
-       switch (cmd_op) {
+       ret = -EINVAL;
+       switch (_IOC_NR(cmd_op)) {
        case UBLK_IO_FETCH_REQ:
                /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */
                if (ublk_queue_ready(ubq)) {
@@ -1694,6 +1731,18 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
        else if (!(info.flags & UBLK_F_UNPRIVILEGED_DEV))
                return -EPERM;
 
+       /*
+        * unprivileged device can't be trusted, but RECOVERY and
+        * RECOVERY_REISSUE still may hang error handling, so can't
+        * support recovery features for unprivileged ublk now
+        *
+        * TODO: provide forward progress for RECOVERY handler, so that
+        * unprivileged device can benefit from it
+        */
+       if (info.flags & UBLK_F_UNPRIVILEGED_DEV)
+               info.flags &= ~(UBLK_F_USER_RECOVERY_REISSUE |
+                               UBLK_F_USER_RECOVERY);
+
        /* the created device is always owned by current user */
        ublk_store_owner_uid_gid(&info.owner_uid, &info.owner_gid);
 
@@ -1743,6 +1792,8 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
        if (!IS_BUILTIN(CONFIG_BLK_DEV_UBLK))
                ub->dev_info.flags |= UBLK_F_URING_CMD_COMP_IN_TASK;
 
+       ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE;
+
        /* We are not ready to support zero copy */
        ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
 
@@ -1960,6 +2011,7 @@ static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
        put_task_struct(ubq->ubq_daemon);
        /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */
        ubq->ubq_daemon = NULL;
+       ubq->timeout = false;
 
        for (i = 0; i < ubq->q_depth; i++) {
                struct ublk_io *io = &ubq->ios[i];
@@ -2099,7 +2151,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
                 * know if the specified device is created as unprivileged
                 * mode.
                 */
-               if (cmd->cmd_op != UBLK_CMD_GET_DEV_INFO2)
+               if (_IOC_NR(cmd->cmd_op) != UBLK_CMD_GET_DEV_INFO2)
                        return 0;
        }
 
@@ -2125,7 +2177,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
        dev_path[header->dev_path_len] = 0;
 
        ret = -EINVAL;
-       switch (cmd->cmd_op) {
+       switch (_IOC_NR(cmd->cmd_op)) {
        case UBLK_CMD_GET_DEV_INFO:
        case UBLK_CMD_GET_DEV_INFO2:
        case UBLK_CMD_GET_QUEUE_AFFINITY:
@@ -2164,6 +2216,7 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
 {
        struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
        struct ublk_device *ub = NULL;
+       u32 cmd_op = cmd->cmd_op;
        int ret = -EINVAL;
 
        if (issue_flags & IO_URING_F_NONBLOCK)
@@ -2174,22 +2227,22 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
        if (!(issue_flags & IO_URING_F_SQE128))
                goto out;
 
-       if (cmd->cmd_op != UBLK_CMD_ADD_DEV) {
+       ret = ublk_check_cmd_op(cmd_op);
+       if (ret)
+               goto out;
+
+       if (_IOC_NR(cmd_op) != UBLK_CMD_ADD_DEV) {
                ret = -ENODEV;
                ub = ublk_get_device_from_id(header->dev_id);
                if (!ub)
                        goto out;
 
                ret = ublk_ctrl_uring_cmd_permission(ub, cmd);
-       } else {
-               /* ADD_DEV permission check is done in command handler */
-               ret = 0;
+               if (ret)
+                       goto put_dev;
        }
 
-       if (ret)
-               goto put_dev;
-
-       switch (cmd->cmd_op) {
+       switch (_IOC_NR(cmd_op)) {
        case UBLK_CMD_START_DEV:
                ret = ublk_ctrl_start_dev(ub, cmd);
                break;