ublk: don't get ublk device reference in ublk_abort_queue()
[linux-block.git] / drivers / block / ublk_drv.c
index 630ddfe6657bc9076b3853f8e100b3ca2fd8e70d..59d3652699f596f9461c1fac03fa34d90f4444cb 100644 (file)
@@ -470,6 +470,7 @@ static DEFINE_MUTEX(ublk_ctl_mutex);
  * It can be extended to one per-user limit in future or even controlled
  * by cgroup.
  */
+#define UBLK_MAX_UBLKS UBLK_MINORS
 static unsigned int ublks_max = 64;
 static unsigned int ublks_added;       /* protected by ublk_ctl_mutex */
 
@@ -1419,9 +1420,6 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
 {
        int i;
 
-       if (!ublk_get_device(ub))
-               return;
-
        for (i = 0; i < ubq->q_depth; i++) {
                struct ublk_io *io = &ubq->ios[i];
 
@@ -1437,7 +1435,6 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
                                __ublk_fail_req(ubq, io, rq);
                }
        }
-       ublk_put_device(ub);
 }
 
 static void ublk_daemon_monitor_work(struct work_struct *work)
@@ -2026,7 +2023,8 @@ static int ublk_alloc_dev_number(struct ublk_device *ub, int idx)
                if (err == -ENOSPC)
                        err = -EEXIST;
        } else {
-               err = idr_alloc(&ublk_index_idr, ub, 0, 0, GFP_NOWAIT);
+               err = idr_alloc(&ublk_index_idr, ub, 0, UBLK_MAX_UBLKS,
+                               GFP_NOWAIT);
        }
        spin_unlock(&ublk_idr_lock);
 
@@ -2305,6 +2303,12 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
                return -EINVAL;
        }
 
+       if (header->dev_id != U32_MAX && header->dev_id >= UBLK_MAX_UBLKS) {
+               pr_warn("%s: dev id is too large. Max supported is %d\n",
+                       __func__, UBLK_MAX_UBLKS - 1);
+               return -EINVAL;
+       }
+
        ublk_dump_dev_info(&info);
 
        ret = mutex_lock_killable(&ublk_ctl_mutex);
@@ -2932,7 +2936,22 @@ static void __exit ublk_exit(void)
 module_init(ublk_init);
 module_exit(ublk_exit);
 
-module_param(ublks_max, int, 0444);
+static int ublk_set_max_ublks(const char *buf, const struct kernel_param *kp)
+{
+       return param_set_uint_minmax(buf, kp, 0, UBLK_MAX_UBLKS);
+}
+
+static int ublk_get_max_ublks(char *buf, const struct kernel_param *kp)
+{
+       return sysfs_emit(buf, "%u\n", ublks_max);
+}
+
+static const struct kernel_param_ops ublk_max_ublks_ops = {
+       .set = ublk_set_max_ublks,
+       .get = ublk_get_max_ublks,
+};
+
+module_param_cb(ublks_max, &ublk_max_ublks_ops, &ublks_max, 0644);
 MODULE_PARM_DESC(ublks_max, "max number of ublk devices allowed to add(default: 64)");
 
 MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");