Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Userspace block device - block device which IO is handled from userspace |
| 4 | * |
| 5 | * Take full use of io_uring passthrough command for communicating with |
| 6 | * ublk userspace daemon(ublksrvd) for handling basic IO request. |
| 7 | * |
| 8 | * Copyright 2022 Ming Lei <ming.lei@redhat.com> |
| 9 | * |
| 10 | * (part of code stolen from loop.c) |
| 11 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/stat.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/major.h> |
| 21 | #include <linux/wait.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/swap.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/compat.h> |
| 27 | #include <linux/mutex.h> |
| 28 | #include <linux/writeback.h> |
| 29 | #include <linux/completion.h> |
| 30 | #include <linux/highmem.h> |
| 31 | #include <linux/sysfs.h> |
| 32 | #include <linux/miscdevice.h> |
| 33 | #include <linux/falloc.h> |
| 34 | #include <linux/uio.h> |
| 35 | #include <linux/ioprio.h> |
| 36 | #include <linux/sched/mm.h> |
| 37 | #include <linux/uaccess.h> |
| 38 | #include <linux/cdev.h> |
| 39 | #include <linux/io_uring.h> |
| 40 | #include <linux/blk-mq.h> |
| 41 | #include <linux/delay.h> |
| 42 | #include <linux/mm.h> |
| 43 | #include <asm/page.h> |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 44 | #include <linux/task_work.h> |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 45 | #include <linux/namei.h> |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 46 | #include <uapi/linux/ublk_cmd.h> |
| 47 | |
| 48 | #define UBLK_MINORS (1U << MINORBITS) |
| 49 | |
Ming Lei | 6d8c5af | 2022-07-22 18:38:17 +0800 | [diff] [blame] | 50 | /* All UBLK_F_* have to be included into UBLK_F_ALL */ |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 51 | #define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \ |
| 52 | | UBLK_F_URING_CMD_COMP_IN_TASK \ |
ZiyangZhang | 77a440e | 2022-09-23 23:39:14 +0800 | [diff] [blame] | 53 | | UBLK_F_NEED_GET_DATA \ |
ZiyangZhang | a0d41dc | 2022-09-23 23:39:17 +0800 | [diff] [blame] | 54 | | UBLK_F_USER_RECOVERY \ |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 55 | | UBLK_F_USER_RECOVERY_REISSUE \ |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 56 | | UBLK_F_UNPRIVILEGED_DEV \ |
| 57 | | UBLK_F_CMD_IOCTL_ENCODE) |
Ming Lei | 6d8c5af | 2022-07-22 18:38:17 +0800 | [diff] [blame] | 58 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 59 | /* All UBLK_PARAM_TYPE_* should be included here */ |
Ming Lei | abb864d | 2023-01-06 12:17:09 +0800 | [diff] [blame] | 60 | #define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | \ |
| 61 | UBLK_PARAM_TYPE_DISCARD | UBLK_PARAM_TYPE_DEVT) |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 62 | |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 63 | struct ublk_rq_data { |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 64 | struct llist_node node; |
| 65 | struct callback_head work; |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 68 | struct ublk_uring_cmd_pdu { |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 69 | struct ublk_queue *ubq; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * io command is active: sqe cmd is received, and its cqe isn't done |
| 74 | * |
| 75 | * If the flag is set, the io command is owned by ublk driver, and waited |
| 76 | * for incoming blk-mq request from the ublk block device. |
| 77 | * |
| 78 | * If the flag is cleared, the io command will be completed, and owned by |
| 79 | * ublk server. |
| 80 | */ |
| 81 | #define UBLK_IO_FLAG_ACTIVE 0x01 |
| 82 | |
| 83 | /* |
| 84 | * IO command is completed via cqe, and it is being handled by ublksrv, and |
| 85 | * not committed yet |
| 86 | * |
| 87 | * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for |
| 88 | * cross verification |
| 89 | */ |
| 90 | #define UBLK_IO_FLAG_OWNED_BY_SRV 0x02 |
| 91 | |
| 92 | /* |
| 93 | * IO command is aborted, so this flag is set in case of |
| 94 | * !UBLK_IO_FLAG_ACTIVE. |
| 95 | * |
| 96 | * After this flag is observed, any pending or new incoming request |
| 97 | * associated with this io command will be failed immediately |
| 98 | */ |
| 99 | #define UBLK_IO_FLAG_ABORTED 0x04 |
| 100 | |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 101 | /* |
| 102 | * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires |
| 103 | * get data buffer address from ublksrv. |
| 104 | * |
| 105 | * Then, bio data could be copied into this data buffer for a WRITE request |
| 106 | * after the IO command is issued again and UBLK_IO_FLAG_NEED_GET_DATA is unset. |
| 107 | */ |
| 108 | #define UBLK_IO_FLAG_NEED_GET_DATA 0x08 |
| 109 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 110 | struct ublk_io { |
| 111 | /* userspace buffer address from io cmd */ |
| 112 | __u64 addr; |
| 113 | unsigned int flags; |
| 114 | int res; |
| 115 | |
| 116 | struct io_uring_cmd *cmd; |
| 117 | }; |
| 118 | |
| 119 | struct ublk_queue { |
| 120 | int q_id; |
| 121 | int q_depth; |
| 122 | |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 123 | unsigned long flags; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 124 | struct task_struct *ubq_daemon; |
| 125 | char *io_cmd_buf; |
| 126 | |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 127 | struct llist_head io_cmds; |
| 128 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 129 | unsigned long io_addr; /* mapped vm address */ |
| 130 | unsigned int max_io_sz; |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 131 | bool force_abort; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 132 | unsigned short nr_io_ready; /* how many ios setup */ |
| 133 | struct ublk_device *dev; |
Yushan Zhou | 72495b5 | 2022-10-18 18:01:32 +0800 | [diff] [blame] | 134 | struct ublk_io ios[]; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | #define UBLK_DAEMON_MONITOR_PERIOD (5 * HZ) |
| 138 | |
| 139 | struct ublk_device { |
| 140 | struct gendisk *ub_disk; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 141 | |
| 142 | char *__queues; |
| 143 | |
Liu Xiaodong | 29baef7 | 2023-01-31 02:05:52 -0500 | [diff] [blame] | 144 | unsigned int queue_size; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 145 | struct ublksrv_ctrl_dev_info dev_info; |
| 146 | |
| 147 | struct blk_mq_tag_set tag_set; |
| 148 | |
| 149 | struct cdev cdev; |
| 150 | struct device cdev_dev; |
| 151 | |
Dan Carpenter | 8d9fdb6 | 2022-07-26 17:12:23 +0300 | [diff] [blame] | 152 | #define UB_STATE_OPEN 0 |
| 153 | #define UB_STATE_USED 1 |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 154 | #define UB_STATE_DELETED 2 |
Christoph Hellwig | fa36204 | 2022-07-21 15:09:12 +0200 | [diff] [blame] | 155 | unsigned long state; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 156 | int ub_number; |
| 157 | |
| 158 | struct mutex mutex; |
| 159 | |
Ming Lei | e94eb45 | 2022-07-21 23:31:17 +0800 | [diff] [blame] | 160 | spinlock_t mm_lock; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 161 | struct mm_struct *mm; |
| 162 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 163 | struct ublk_params params; |
| 164 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 165 | struct completion completion; |
| 166 | unsigned int nr_queues_ready; |
Ming Lei | 73a166d | 2023-01-06 12:17:07 +0800 | [diff] [blame] | 167 | unsigned int nr_privileged_daemon; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Our ubq->daemon may be killed without any notification, so |
| 171 | * monitor each queue's daemon periodically |
| 172 | */ |
| 173 | struct delayed_work monitor_work; |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 174 | struct work_struct quiesce_work; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 175 | struct work_struct stop_work; |
| 176 | }; |
| 177 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 178 | /* header of ublk_params */ |
| 179 | struct ublk_params_header { |
| 180 | __u32 len; |
| 181 | __u32 types; |
| 182 | }; |
| 183 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 184 | static dev_t ublk_chr_devt; |
| 185 | static struct class *ublk_chr_class; |
| 186 | |
| 187 | static DEFINE_IDR(ublk_index_idr); |
| 188 | static DEFINE_SPINLOCK(ublk_idr_lock); |
| 189 | static wait_queue_head_t ublk_idr_wq; /* wait until one idr is freed */ |
| 190 | |
| 191 | static DEFINE_MUTEX(ublk_ctl_mutex); |
| 192 | |
Ming Lei | 403ebc8 | 2023-01-06 12:17:10 +0800 | [diff] [blame] | 193 | /* |
| 194 | * Max ublk devices allowed to add |
| 195 | * |
| 196 | * It can be extended to one per-user limit in future or even controlled |
| 197 | * by cgroup. |
| 198 | */ |
| 199 | static unsigned int ublks_max = 64; |
| 200 | static unsigned int ublks_added; /* protected by ublk_ctl_mutex */ |
| 201 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 202 | static struct miscdevice ublk_misc; |
| 203 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 204 | static void ublk_dev_param_basic_apply(struct ublk_device *ub) |
| 205 | { |
| 206 | struct request_queue *q = ub->ub_disk->queue; |
| 207 | const struct ublk_param_basic *p = &ub->params.basic; |
| 208 | |
| 209 | blk_queue_logical_block_size(q, 1 << p->logical_bs_shift); |
| 210 | blk_queue_physical_block_size(q, 1 << p->physical_bs_shift); |
| 211 | blk_queue_io_min(q, 1 << p->io_min_shift); |
| 212 | blk_queue_io_opt(q, 1 << p->io_opt_shift); |
| 213 | |
| 214 | blk_queue_write_cache(q, p->attrs & UBLK_ATTR_VOLATILE_CACHE, |
| 215 | p->attrs & UBLK_ATTR_FUA); |
| 216 | if (p->attrs & UBLK_ATTR_ROTATIONAL) |
| 217 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); |
| 218 | else |
| 219 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
| 220 | |
| 221 | blk_queue_max_hw_sectors(q, p->max_sectors); |
| 222 | blk_queue_chunk_sectors(q, p->chunk_sectors); |
| 223 | blk_queue_virt_boundary(q, p->virt_boundary_mask); |
| 224 | |
| 225 | if (p->attrs & UBLK_ATTR_READ_ONLY) |
| 226 | set_disk_ro(ub->ub_disk, true); |
| 227 | |
| 228 | set_capacity(ub->ub_disk, p->dev_sectors); |
| 229 | } |
| 230 | |
| 231 | static void ublk_dev_param_discard_apply(struct ublk_device *ub) |
| 232 | { |
| 233 | struct request_queue *q = ub->ub_disk->queue; |
| 234 | const struct ublk_param_discard *p = &ub->params.discard; |
| 235 | |
| 236 | q->limits.discard_alignment = p->discard_alignment; |
| 237 | q->limits.discard_granularity = p->discard_granularity; |
| 238 | blk_queue_max_discard_sectors(q, p->max_discard_sectors); |
| 239 | blk_queue_max_write_zeroes_sectors(q, |
| 240 | p->max_write_zeroes_sectors); |
| 241 | blk_queue_max_discard_segments(q, p->max_discard_segments); |
| 242 | } |
| 243 | |
| 244 | static int ublk_validate_params(const struct ublk_device *ub) |
| 245 | { |
| 246 | /* basic param is the only one which must be set */ |
| 247 | if (ub->params.types & UBLK_PARAM_TYPE_BASIC) { |
| 248 | const struct ublk_param_basic *p = &ub->params.basic; |
| 249 | |
Ming Lei | 1d16652 | 2023-04-06 20:40:59 +0800 | [diff] [blame] | 250 | if (p->logical_bs_shift > PAGE_SHIFT || p->logical_bs_shift < 9) |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 251 | return -EINVAL; |
| 252 | |
| 253 | if (p->logical_bs_shift > p->physical_bs_shift) |
| 254 | return -EINVAL; |
| 255 | |
Ming Lei | 4bf9cbf | 2022-07-30 17:27:50 +0800 | [diff] [blame] | 256 | if (p->max_sectors > (ub->dev_info.max_io_buf_bytes >> 9)) |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 257 | return -EINVAL; |
| 258 | } else |
| 259 | return -EINVAL; |
| 260 | |
| 261 | if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) { |
| 262 | const struct ublk_param_discard *p = &ub->params.discard; |
| 263 | |
| 264 | /* So far, only support single segment discard */ |
| 265 | if (p->max_discard_sectors && p->max_discard_segments != 1) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | if (!p->discard_granularity) |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
Ming Lei | abb864d | 2023-01-06 12:17:09 +0800 | [diff] [blame] | 272 | /* dev_t is read-only */ |
| 273 | if (ub->params.types & UBLK_PARAM_TYPE_DEVT) |
| 274 | return -EINVAL; |
| 275 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int ublk_apply_params(struct ublk_device *ub) |
| 280 | { |
| 281 | if (!(ub->params.types & UBLK_PARAM_TYPE_BASIC)) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | ublk_dev_param_basic_apply(ub); |
| 285 | |
| 286 | if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) |
| 287 | ublk_dev_param_discard_apply(ub); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 292 | static inline bool ublk_can_use_task_work(const struct ublk_queue *ubq) |
| 293 | { |
| 294 | if (IS_BUILTIN(CONFIG_BLK_DEV_UBLK) && |
| 295 | !(ubq->flags & UBLK_F_URING_CMD_COMP_IN_TASK)) |
| 296 | return true; |
| 297 | return false; |
| 298 | } |
| 299 | |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 300 | static inline bool ublk_need_get_data(const struct ublk_queue *ubq) |
| 301 | { |
Ming Lei | 96cf2f5 | 2023-03-30 19:36:23 +0800 | [diff] [blame] | 302 | return ubq->flags & UBLK_F_NEED_GET_DATA; |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 305 | static struct ublk_device *ublk_get_device(struct ublk_device *ub) |
| 306 | { |
| 307 | if (kobject_get_unless_zero(&ub->cdev_dev.kobj)) |
| 308 | return ub; |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | static void ublk_put_device(struct ublk_device *ub) |
| 313 | { |
| 314 | put_device(&ub->cdev_dev); |
| 315 | } |
| 316 | |
| 317 | static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev, |
| 318 | int qid) |
| 319 | { |
| 320 | return (struct ublk_queue *)&(dev->__queues[qid * dev->queue_size]); |
| 321 | } |
| 322 | |
| 323 | static inline bool ublk_rq_has_data(const struct request *rq) |
| 324 | { |
Ziyang Zhang | 731e208 | 2023-02-07 15:08:37 +0800 | [diff] [blame] | 325 | return bio_has_data(rq->bio); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq, |
| 329 | int tag) |
| 330 | { |
| 331 | return (struct ublksrv_io_desc *) |
| 332 | &(ubq->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]); |
| 333 | } |
| 334 | |
| 335 | static inline char *ublk_queue_cmd_buf(struct ublk_device *ub, int q_id) |
| 336 | { |
| 337 | return ublk_get_queue(ub, q_id)->io_cmd_buf; |
| 338 | } |
| 339 | |
| 340 | static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id) |
| 341 | { |
| 342 | struct ublk_queue *ubq = ublk_get_queue(ub, q_id); |
| 343 | |
| 344 | return round_up(ubq->q_depth * sizeof(struct ublksrv_io_desc), |
| 345 | PAGE_SIZE); |
| 346 | } |
| 347 | |
ZiyangZhang | a0d41dc | 2022-09-23 23:39:17 +0800 | [diff] [blame] | 348 | static inline bool ublk_queue_can_use_recovery_reissue( |
| 349 | struct ublk_queue *ubq) |
| 350 | { |
Ming Lei | 96cf2f5 | 2023-03-30 19:36:23 +0800 | [diff] [blame] | 351 | return (ubq->flags & UBLK_F_USER_RECOVERY) && |
| 352 | (ubq->flags & UBLK_F_USER_RECOVERY_REISSUE); |
ZiyangZhang | a0d41dc | 2022-09-23 23:39:17 +0800 | [diff] [blame] | 353 | } |
| 354 | |
ZiyangZhang | 77a440e | 2022-09-23 23:39:14 +0800 | [diff] [blame] | 355 | static inline bool ublk_queue_can_use_recovery( |
| 356 | struct ublk_queue *ubq) |
| 357 | { |
Ming Lei | 96cf2f5 | 2023-03-30 19:36:23 +0800 | [diff] [blame] | 358 | return ubq->flags & UBLK_F_USER_RECOVERY; |
ZiyangZhang | 77a440e | 2022-09-23 23:39:14 +0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static inline bool ublk_can_use_recovery(struct ublk_device *ub) |
| 362 | { |
Ming Lei | 96cf2f5 | 2023-03-30 19:36:23 +0800 | [diff] [blame] | 363 | return ub->dev_info.flags & UBLK_F_USER_RECOVERY; |
ZiyangZhang | 77a440e | 2022-09-23 23:39:14 +0800 | [diff] [blame] | 364 | } |
| 365 | |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 366 | static void ublk_free_disk(struct gendisk *disk) |
| 367 | { |
| 368 | struct ublk_device *ub = disk->private_data; |
| 369 | |
| 370 | clear_bit(UB_STATE_USED, &ub->state); |
| 371 | put_device(&ub->cdev_dev); |
| 372 | } |
| 373 | |
Ming Lei | 48a9051 | 2023-01-31 12:04:46 +0800 | [diff] [blame] | 374 | static void ublk_store_owner_uid_gid(unsigned int *owner_uid, |
| 375 | unsigned int *owner_gid) |
| 376 | { |
| 377 | kuid_t uid; |
| 378 | kgid_t gid; |
| 379 | |
| 380 | current_uid_gid(&uid, &gid); |
| 381 | |
| 382 | *owner_uid = from_kuid(&init_user_ns, uid); |
| 383 | *owner_gid = from_kgid(&init_user_ns, gid); |
| 384 | } |
| 385 | |
| 386 | static int ublk_open(struct block_device *bdev, fmode_t mode) |
| 387 | { |
| 388 | struct ublk_device *ub = bdev->bd_disk->private_data; |
| 389 | |
| 390 | if (capable(CAP_SYS_ADMIN)) |
| 391 | return 0; |
| 392 | |
| 393 | /* |
| 394 | * If it is one unprivileged device, only owner can open |
| 395 | * the disk. Otherwise it could be one trap made by one |
| 396 | * evil user who grants this disk's privileges to other |
| 397 | * users deliberately. |
| 398 | * |
| 399 | * This way is reasonable too given anyone can create |
| 400 | * unprivileged device, and no need other's grant. |
| 401 | */ |
| 402 | if (ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV) { |
| 403 | unsigned int curr_uid, curr_gid; |
| 404 | |
| 405 | ublk_store_owner_uid_gid(&curr_uid, &curr_gid); |
| 406 | |
| 407 | if (curr_uid != ub->dev_info.owner_uid || curr_gid != |
| 408 | ub->dev_info.owner_gid) |
| 409 | return -EPERM; |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 415 | static const struct block_device_operations ub_fops = { |
| 416 | .owner = THIS_MODULE, |
Ming Lei | 48a9051 | 2023-01-31 12:04:46 +0800 | [diff] [blame] | 417 | .open = ublk_open, |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 418 | .free_disk = ublk_free_disk, |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | #define UBLK_MAX_PIN_PAGES 32 |
| 422 | |
| 423 | struct ublk_map_data { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 424 | const struct request *rq; |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 425 | unsigned long ubuf; |
| 426 | unsigned int len; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | struct ublk_io_iter { |
| 430 | struct page *pages[UBLK_MAX_PIN_PAGES]; |
| 431 | unsigned pg_off; /* offset in the 1st page in pages */ |
| 432 | int nr_pages; /* how many page pointers in pages */ |
| 433 | struct bio *bio; |
| 434 | struct bvec_iter iter; |
| 435 | }; |
| 436 | |
| 437 | static inline unsigned ublk_copy_io_pages(struct ublk_io_iter *data, |
| 438 | unsigned max_bytes, bool to_vm) |
| 439 | { |
| 440 | const unsigned total = min_t(unsigned, max_bytes, |
| 441 | PAGE_SIZE - data->pg_off + |
| 442 | ((data->nr_pages - 1) << PAGE_SHIFT)); |
| 443 | unsigned done = 0; |
| 444 | unsigned pg_idx = 0; |
| 445 | |
| 446 | while (done < total) { |
| 447 | struct bio_vec bv = bio_iter_iovec(data->bio, data->iter); |
| 448 | const unsigned int bytes = min3(bv.bv_len, total - done, |
| 449 | (unsigned)(PAGE_SIZE - data->pg_off)); |
| 450 | void *bv_buf = bvec_kmap_local(&bv); |
| 451 | void *pg_buf = kmap_local_page(data->pages[pg_idx]); |
| 452 | |
| 453 | if (to_vm) |
| 454 | memcpy(pg_buf + data->pg_off, bv_buf, bytes); |
| 455 | else |
| 456 | memcpy(bv_buf, pg_buf + data->pg_off, bytes); |
| 457 | |
| 458 | kunmap_local(pg_buf); |
| 459 | kunmap_local(bv_buf); |
| 460 | |
| 461 | /* advance page array */ |
| 462 | data->pg_off += bytes; |
| 463 | if (data->pg_off == PAGE_SIZE) { |
| 464 | pg_idx += 1; |
| 465 | data->pg_off = 0; |
| 466 | } |
| 467 | |
| 468 | done += bytes; |
| 469 | |
| 470 | /* advance bio */ |
| 471 | bio_advance_iter_single(data->bio, &data->iter, bytes); |
| 472 | if (!data->iter.bi_size) { |
| 473 | data->bio = data->bio->bi_next; |
| 474 | if (data->bio == NULL) |
| 475 | break; |
| 476 | data->iter = data->bio->bi_iter; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return done; |
| 481 | } |
| 482 | |
Ming Lei | 2f3af72 | 2023-03-30 19:36:22 +0800 | [diff] [blame] | 483 | static int ublk_copy_user_pages(struct ublk_map_data *data, bool to_vm) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 484 | { |
| 485 | const unsigned int gup_flags = to_vm ? FOLL_WRITE : 0; |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 486 | const unsigned long start_vm = data->ubuf; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 487 | unsigned int done = 0; |
| 488 | struct ublk_io_iter iter = { |
| 489 | .pg_off = start_vm & (PAGE_SIZE - 1), |
| 490 | .bio = data->rq->bio, |
| 491 | .iter = data->rq->bio->bi_iter, |
| 492 | }; |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 493 | const unsigned int nr_pages = round_up(data->len + |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 494 | (start_vm & (PAGE_SIZE - 1)), PAGE_SIZE) >> PAGE_SHIFT; |
| 495 | |
| 496 | while (done < nr_pages) { |
| 497 | const unsigned to_pin = min_t(unsigned, UBLK_MAX_PIN_PAGES, |
| 498 | nr_pages - done); |
| 499 | unsigned i, len; |
| 500 | |
| 501 | iter.nr_pages = get_user_pages_fast(start_vm + |
| 502 | (done << PAGE_SHIFT), to_pin, gup_flags, |
| 503 | iter.pages); |
| 504 | if (iter.nr_pages <= 0) |
| 505 | return done == 0 ? iter.nr_pages : done; |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 506 | len = ublk_copy_io_pages(&iter, data->len, to_vm); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 507 | for (i = 0; i < iter.nr_pages; i++) { |
| 508 | if (to_vm) |
| 509 | set_page_dirty(iter.pages[i]); |
| 510 | put_page(iter.pages[i]); |
| 511 | } |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 512 | data->len -= len; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 513 | done += iter.nr_pages; |
| 514 | } |
| 515 | |
| 516 | return done; |
| 517 | } |
| 518 | |
Ming Lei | 2f3af72 | 2023-03-30 19:36:22 +0800 | [diff] [blame] | 519 | static inline bool ublk_need_map_req(const struct request *req) |
| 520 | { |
| 521 | return ublk_rq_has_data(req) && req_op(req) == REQ_OP_WRITE; |
| 522 | } |
| 523 | |
| 524 | static inline bool ublk_need_unmap_req(const struct request *req) |
| 525 | { |
| 526 | return ublk_rq_has_data(req) && req_op(req) == REQ_OP_READ; |
| 527 | } |
| 528 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 529 | static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req, |
| 530 | struct ublk_io *io) |
| 531 | { |
| 532 | const unsigned int rq_bytes = blk_rq_bytes(req); |
Ming Lei | 23ef822 | 2023-03-30 19:36:21 +0800 | [diff] [blame] | 533 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 534 | /* |
| 535 | * no zero copy, we delay copy WRITE request data into ublksrv |
| 536 | * context and the big benefit is that pinning pages in current |
| 537 | * context is pretty fast, see ublk_pin_user_pages |
| 538 | */ |
Ming Lei | 2f3af72 | 2023-03-30 19:36:22 +0800 | [diff] [blame] | 539 | if (ublk_need_map_req(req)) { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 540 | struct ublk_map_data data = { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 541 | .rq = req, |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 542 | .ubuf = io->addr, |
| 543 | .len = rq_bytes, |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | ublk_copy_user_pages(&data, true); |
| 547 | |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 548 | return rq_bytes - data.len; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 549 | } |
| 550 | return rq_bytes; |
| 551 | } |
| 552 | |
| 553 | static int ublk_unmap_io(const struct ublk_queue *ubq, |
| 554 | const struct request *req, |
| 555 | struct ublk_io *io) |
| 556 | { |
| 557 | const unsigned int rq_bytes = blk_rq_bytes(req); |
| 558 | |
Ming Lei | 2f3af72 | 2023-03-30 19:36:22 +0800 | [diff] [blame] | 559 | if (ublk_need_unmap_req(req)) { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 560 | struct ublk_map_data data = { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 561 | .rq = req, |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 562 | .ubuf = io->addr, |
| 563 | .len = io->res, |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | WARN_ON_ONCE(io->res > rq_bytes); |
| 567 | |
| 568 | ublk_copy_user_pages(&data, false); |
| 569 | |
Ming Lei | ae9f5cc | 2023-03-30 19:36:24 +0800 | [diff] [blame] | 570 | return io->res - data.len; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 571 | } |
| 572 | return rq_bytes; |
| 573 | } |
| 574 | |
| 575 | static inline unsigned int ublk_req_build_flags(struct request *req) |
| 576 | { |
| 577 | unsigned flags = 0; |
| 578 | |
| 579 | if (req->cmd_flags & REQ_FAILFAST_DEV) |
| 580 | flags |= UBLK_IO_F_FAILFAST_DEV; |
| 581 | |
| 582 | if (req->cmd_flags & REQ_FAILFAST_TRANSPORT) |
| 583 | flags |= UBLK_IO_F_FAILFAST_TRANSPORT; |
| 584 | |
| 585 | if (req->cmd_flags & REQ_FAILFAST_DRIVER) |
| 586 | flags |= UBLK_IO_F_FAILFAST_DRIVER; |
| 587 | |
| 588 | if (req->cmd_flags & REQ_META) |
| 589 | flags |= UBLK_IO_F_META; |
| 590 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 591 | if (req->cmd_flags & REQ_FUA) |
| 592 | flags |= UBLK_IO_F_FUA; |
| 593 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 594 | if (req->cmd_flags & REQ_NOUNMAP) |
| 595 | flags |= UBLK_IO_F_NOUNMAP; |
| 596 | |
| 597 | if (req->cmd_flags & REQ_SWAP) |
| 598 | flags |= UBLK_IO_F_SWAP; |
| 599 | |
| 600 | return flags; |
| 601 | } |
| 602 | |
Ming Lei | f2450f8 | 2022-07-16 17:53:44 +0800 | [diff] [blame] | 603 | static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 604 | { |
| 605 | struct ublksrv_io_desc *iod = ublk_get_iod(ubq, req->tag); |
| 606 | struct ublk_io *io = &ubq->ios[req->tag]; |
| 607 | u32 ublk_op; |
| 608 | |
| 609 | switch (req_op(req)) { |
| 610 | case REQ_OP_READ: |
| 611 | ublk_op = UBLK_IO_OP_READ; |
| 612 | break; |
| 613 | case REQ_OP_WRITE: |
| 614 | ublk_op = UBLK_IO_OP_WRITE; |
| 615 | break; |
| 616 | case REQ_OP_FLUSH: |
| 617 | ublk_op = UBLK_IO_OP_FLUSH; |
| 618 | break; |
| 619 | case REQ_OP_DISCARD: |
| 620 | ublk_op = UBLK_IO_OP_DISCARD; |
| 621 | break; |
| 622 | case REQ_OP_WRITE_ZEROES: |
| 623 | ublk_op = UBLK_IO_OP_WRITE_ZEROES; |
| 624 | break; |
| 625 | default: |
| 626 | return BLK_STS_IOERR; |
| 627 | } |
| 628 | |
| 629 | /* need to translate since kernel may change */ |
| 630 | iod->op_flags = ublk_op | ublk_req_build_flags(req); |
| 631 | iod->nr_sectors = blk_rq_sectors(req); |
| 632 | iod->start_sector = blk_rq_pos(req); |
| 633 | iod->addr = io->addr; |
| 634 | |
| 635 | return BLK_STS_OK; |
| 636 | } |
| 637 | |
| 638 | static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu( |
| 639 | struct io_uring_cmd *ioucmd) |
| 640 | { |
| 641 | return (struct ublk_uring_cmd_pdu *)&ioucmd->pdu; |
| 642 | } |
| 643 | |
ZiyangZhang | 966120b | 2022-08-15 10:36:31 +0800 | [diff] [blame] | 644 | static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 645 | { |
| 646 | return ubq->ubq_daemon->flags & PF_EXITING; |
| 647 | } |
| 648 | |
| 649 | /* todo: handle partial completion */ |
| 650 | static void ublk_complete_rq(struct request *req) |
| 651 | { |
| 652 | struct ublk_queue *ubq = req->mq_hctx->driver_data; |
| 653 | struct ublk_io *io = &ubq->ios[req->tag]; |
| 654 | unsigned int unmapped_bytes; |
Ming Lei | 903f8ae | 2023-03-30 19:36:20 +0800 | [diff] [blame] | 655 | blk_status_t res = BLK_STS_OK; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 656 | |
| 657 | /* failed read IO if nothing is read */ |
| 658 | if (!io->res && req_op(req) == REQ_OP_READ) |
| 659 | io->res = -EIO; |
| 660 | |
| 661 | if (io->res < 0) { |
Ming Lei | 903f8ae | 2023-03-30 19:36:20 +0800 | [diff] [blame] | 662 | res = errno_to_blk_status(io->res); |
| 663 | goto exit; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
Ziyang Zhang | b352389 | 2023-02-07 15:08:38 +0800 | [diff] [blame] | 667 | * FLUSH, DISCARD or WRITE_ZEROES usually won't return bytes returned, so end them |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 668 | * directly. |
| 669 | * |
| 670 | * Both the two needn't unmap. |
| 671 | */ |
Ming Lei | 903f8ae | 2023-03-30 19:36:20 +0800 | [diff] [blame] | 672 | if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE) |
| 673 | goto exit; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 674 | |
| 675 | /* for READ request, writing data in iod->addr to rq buffers */ |
| 676 | unmapped_bytes = ublk_unmap_io(ubq, req, io); |
| 677 | |
| 678 | /* |
| 679 | * Extremely impossible since we got data filled in just before |
| 680 | * |
| 681 | * Re-read simply for this unlikely case. |
| 682 | */ |
| 683 | if (unlikely(unmapped_bytes < io->res)) |
| 684 | io->res = unmapped_bytes; |
| 685 | |
| 686 | if (blk_update_request(req, BLK_STS_OK, io->res)) |
| 687 | blk_mq_requeue_request(req, true); |
| 688 | else |
| 689 | __blk_mq_end_request(req, BLK_STS_OK); |
Ming Lei | 903f8ae | 2023-03-30 19:36:20 +0800 | [diff] [blame] | 690 | |
| 691 | return; |
| 692 | exit: |
| 693 | blk_mq_end_request(req, res); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
ZiyangZhang | bb24174 | 2022-08-15 10:36:32 +0800 | [diff] [blame] | 697 | * Since __ublk_rq_task_work always fails requests immediately during |
| 698 | * exiting, __ublk_fail_req() is only called from abort context during |
| 699 | * exiting. So lock is unnecessary. |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 700 | * |
| 701 | * Also aborting may not be started yet, keep in mind that one failed |
| 702 | * request may be issued by block layer again. |
| 703 | */ |
ZiyangZhang | a0d41dc | 2022-09-23 23:39:17 +0800 | [diff] [blame] | 704 | static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io, |
| 705 | struct request *req) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 706 | { |
| 707 | WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE); |
| 708 | |
| 709 | if (!(io->flags & UBLK_IO_FLAG_ABORTED)) { |
| 710 | io->flags |= UBLK_IO_FLAG_ABORTED; |
ZiyangZhang | a0d41dc | 2022-09-23 23:39:17 +0800 | [diff] [blame] | 711 | if (ublk_queue_can_use_recovery_reissue(ubq)) |
| 712 | blk_mq_requeue_request(req, false); |
| 713 | else |
| 714 | blk_mq_end_request(req, BLK_STS_IOERR); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 718 | static void ubq_complete_io_cmd(struct ublk_io *io, int res, |
| 719 | unsigned issue_flags) |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 720 | { |
| 721 | /* mark this cmd owned by ublksrv */ |
| 722 | io->flags |= UBLK_IO_FLAG_OWNED_BY_SRV; |
| 723 | |
| 724 | /* |
| 725 | * clear ACTIVE since we are done with this sqe/cmd slot |
| 726 | * We can only accept io cmd in case of being not active. |
| 727 | */ |
| 728 | io->flags &= ~UBLK_IO_FLAG_ACTIVE; |
| 729 | |
| 730 | /* tell ublksrv one io request is coming */ |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 731 | io_uring_cmd_done(io->cmd, res, 0, issue_flags); |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 732 | } |
| 733 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 734 | #define UBLK_REQUEUE_DELAY_MS 3 |
| 735 | |
ZiyangZhang | 42cf5fc | 2022-09-23 23:39:15 +0800 | [diff] [blame] | 736 | static inline void __ublk_abort_rq(struct ublk_queue *ubq, |
| 737 | struct request *rq) |
| 738 | { |
| 739 | /* We cannot process this rq so just requeue it. */ |
| 740 | if (ublk_queue_can_use_recovery(ubq)) |
| 741 | blk_mq_requeue_request(rq, false); |
| 742 | else |
| 743 | blk_mq_end_request(rq, BLK_STS_IOERR); |
| 744 | |
| 745 | mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0); |
| 746 | } |
| 747 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 748 | static inline void __ublk_rq_task_work(struct request *req, |
| 749 | unsigned issue_flags) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 750 | { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 751 | struct ublk_queue *ubq = req->mq_hctx->driver_data; |
| 752 | int tag = req->tag; |
| 753 | struct ublk_io *io = &ubq->ios[tag]; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 754 | unsigned int mapped_bytes; |
| 755 | |
| 756 | pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n", |
| 757 | __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags, |
| 758 | ublk_get_iod(ubq, req->tag)->addr); |
| 759 | |
ZiyangZhang | ae3f719 | 2022-09-23 23:39:13 +0800 | [diff] [blame] | 760 | /* |
| 761 | * Task is exiting if either: |
| 762 | * |
| 763 | * (1) current != ubq_daemon. |
| 764 | * io_uring_cmd_complete_in_task() tries to run task_work |
| 765 | * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING. |
| 766 | * |
| 767 | * (2) current->flags & PF_EXITING. |
| 768 | */ |
| 769 | if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) { |
ZiyangZhang | 42cf5fc | 2022-09-23 23:39:15 +0800 | [diff] [blame] | 770 | __ublk_abort_rq(ubq, req); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 771 | return; |
| 772 | } |
| 773 | |
Ming Lei | 2f3af72 | 2023-03-30 19:36:22 +0800 | [diff] [blame] | 774 | if (ublk_need_get_data(ubq) && ublk_need_map_req(req)) { |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 775 | /* |
| 776 | * We have not handled UBLK_IO_NEED_GET_DATA command yet, |
| 777 | * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv |
| 778 | * and notify it. |
| 779 | */ |
| 780 | if (!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA)) { |
| 781 | io->flags |= UBLK_IO_FLAG_NEED_GET_DATA; |
| 782 | pr_devel("%s: need get data. op %d, qid %d tag %d io_flags %x\n", |
| 783 | __func__, io->cmd->cmd_op, ubq->q_id, |
| 784 | req->tag, io->flags); |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 785 | ubq_complete_io_cmd(io, UBLK_IO_RES_NEED_GET_DATA, issue_flags); |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 786 | return; |
| 787 | } |
| 788 | /* |
| 789 | * We have handled UBLK_IO_NEED_GET_DATA command, |
| 790 | * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just |
| 791 | * do the copy work. |
| 792 | */ |
| 793 | io->flags &= ~UBLK_IO_FLAG_NEED_GET_DATA; |
ZiyangZhang | 92cb6e2 | 2022-08-10 13:52:12 +0800 | [diff] [blame] | 794 | /* update iod->addr because ublksrv may have passed a new io buffer */ |
| 795 | ublk_get_iod(ubq, req->tag)->addr = io->addr; |
| 796 | pr_devel("%s: update iod->addr: op %d, qid %d tag %d io_flags %x addr %llx\n", |
| 797 | __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags, |
| 798 | ublk_get_iod(ubq, req->tag)->addr); |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 799 | } |
| 800 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 801 | mapped_bytes = ublk_map_io(ubq, req, io); |
| 802 | |
| 803 | /* partially mapped, update io descriptor */ |
| 804 | if (unlikely(mapped_bytes != blk_rq_bytes(req))) { |
| 805 | /* |
| 806 | * Nothing mapped, retry until we succeed. |
| 807 | * |
| 808 | * We may never succeed in mapping any bytes here because |
| 809 | * of OOM. TODO: reserve one buffer with single page pinned |
| 810 | * for providing forward progress guarantee. |
| 811 | */ |
| 812 | if (unlikely(!mapped_bytes)) { |
| 813 | blk_mq_requeue_request(req, false); |
| 814 | blk_mq_delay_kick_requeue_list(req->q, |
| 815 | UBLK_REQUEUE_DELAY_MS); |
| 816 | return; |
| 817 | } |
| 818 | |
| 819 | ublk_get_iod(ubq, req->tag)->nr_sectors = |
| 820 | mapped_bytes >> 9; |
| 821 | } |
| 822 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 823 | ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 824 | } |
| 825 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 826 | static inline void ublk_forward_io_cmds(struct ublk_queue *ubq, |
| 827 | unsigned issue_flags) |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 828 | { |
| 829 | struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds); |
| 830 | struct ublk_rq_data *data, *tmp; |
| 831 | |
| 832 | io_cmds = llist_reverse_order(io_cmds); |
| 833 | llist_for_each_entry_safe(data, tmp, io_cmds, node) |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 834 | __ublk_rq_task_work(blk_mq_rq_from_pdu(data), issue_flags); |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static inline void ublk_abort_io_cmds(struct ublk_queue *ubq) |
| 838 | { |
| 839 | struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds); |
| 840 | struct ublk_rq_data *data, *tmp; |
| 841 | |
| 842 | llist_for_each_entry_safe(data, tmp, io_cmds, node) |
| 843 | __ublk_abort_rq(ubq, blk_mq_rq_from_pdu(data)); |
| 844 | } |
| 845 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 846 | static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd, unsigned issue_flags) |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 847 | { |
| 848 | struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd); |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 849 | struct ublk_queue *ubq = pdu->ubq; |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 850 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 851 | ublk_forward_io_cmds(ubq, issue_flags); |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static void ublk_rq_task_work_fn(struct callback_head *work) |
| 855 | { |
| 856 | struct ublk_rq_data *data = container_of(work, |
| 857 | struct ublk_rq_data, work); |
| 858 | struct request *req = blk_mq_rq_from_pdu(data); |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 859 | struct ublk_queue *ubq = req->mq_hctx->driver_data; |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 860 | unsigned issue_flags = IO_URING_F_UNLOCKED; |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 861 | |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 862 | ublk_forward_io_cmds(ubq, issue_flags); |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 863 | } |
| 864 | |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 865 | static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq) |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 866 | { |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 867 | struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq); |
| 868 | struct ublk_io *io; |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 869 | |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 870 | if (!llist_add(&data->node, &ubq->io_cmds)) |
| 871 | return; |
| 872 | |
| 873 | io = &ubq->ios[rq->tag]; |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 874 | /* |
| 875 | * If the check pass, we know that this is a re-issued request aborted |
| 876 | * previously in monitor_work because the ubq_daemon(cmd's task) is |
| 877 | * PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore |
| 878 | * because this ioucmd's io_uring context may be freed now if no inflight |
| 879 | * ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work. |
| 880 | * |
| 881 | * Note: monitor_work sets UBLK_IO_FLAG_ABORTED and ends this request(releasing |
| 882 | * the tag). Then the request is re-started(allocating the tag) and we are here. |
| 883 | * Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED |
| 884 | * guarantees that here is a re-issued request aborted previously. |
| 885 | */ |
| 886 | if (unlikely(io->flags & UBLK_IO_FLAG_ABORTED)) { |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 887 | ublk_abort_io_cmds(ubq); |
| 888 | } else if (ublk_can_use_task_work(ubq)) { |
| 889 | if (task_work_add(ubq->ubq_daemon, &data->work, |
| 890 | TWA_SIGNAL_NO_IPI)) |
| 891 | ublk_abort_io_cmds(ubq); |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 892 | } else { |
| 893 | struct io_uring_cmd *cmd = io->cmd; |
| 894 | struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd); |
| 895 | |
| 896 | pdu->ubq = ubq; |
| 897 | io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb); |
| 898 | } |
| 899 | } |
| 900 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 901 | static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 902 | const struct blk_mq_queue_data *bd) |
| 903 | { |
| 904 | struct ublk_queue *ubq = hctx->driver_data; |
| 905 | struct request *rq = bd->rq; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 906 | blk_status_t res; |
| 907 | |
| 908 | /* fill iod to slot in io cmd buffer */ |
| 909 | res = ublk_setup_iod(ubq, rq); |
| 910 | if (unlikely(res != BLK_STS_OK)) |
| 911 | return BLK_STS_IOERR; |
Ming Lei | 3ab6e94 | 2022-10-29 09:04:31 +0800 | [diff] [blame] | 912 | |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 913 | /* With recovery feature enabled, force_abort is set in |
| 914 | * ublk_stop_dev() before calling del_gendisk(). We have to |
| 915 | * abort all requeued and new rqs here to let del_gendisk() |
| 916 | * move on. Besides, we cannot not call io_uring_cmd_complete_in_task() |
| 917 | * to avoid UAF on io_uring ctx. |
| 918 | * |
| 919 | * Note: force_abort is guaranteed to be seen because it is set |
| 920 | * before request queue is unqiuesced. |
| 921 | */ |
| 922 | if (ublk_queue_can_use_recovery(ubq) && unlikely(ubq->force_abort)) |
| 923 | return BLK_STS_IOERR; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 924 | |
| 925 | blk_mq_start_request(bd->rq); |
| 926 | |
| 927 | if (unlikely(ubq_daemon_is_dying(ubq))) { |
ZiyangZhang | 42cf5fc | 2022-09-23 23:39:15 +0800 | [diff] [blame] | 928 | __ublk_abort_rq(ubq, rq); |
| 929 | return BLK_STS_OK; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 930 | } |
| 931 | |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 932 | ublk_queue_cmd(ubq, rq); |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 933 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 934 | return BLK_STS_OK; |
| 935 | } |
| 936 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 937 | static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data, |
| 938 | unsigned int hctx_idx) |
| 939 | { |
Ming Lei | cebbe57 | 2022-07-14 18:32:01 +0800 | [diff] [blame] | 940 | struct ublk_device *ub = driver_data; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 941 | struct ublk_queue *ubq = ublk_get_queue(ub, hctx->queue_num); |
| 942 | |
| 943 | hctx->driver_data = ubq; |
| 944 | return 0; |
| 945 | } |
| 946 | |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 947 | static int ublk_init_rq(struct blk_mq_tag_set *set, struct request *req, |
| 948 | unsigned int hctx_idx, unsigned int numa_node) |
| 949 | { |
| 950 | struct ublk_rq_data *data = blk_mq_rq_to_pdu(req); |
| 951 | |
| 952 | init_task_work(&data->work, ublk_rq_task_work_fn); |
| 953 | return 0; |
| 954 | } |
| 955 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 956 | static const struct blk_mq_ops ublk_mq_ops = { |
| 957 | .queue_rq = ublk_queue_rq, |
| 958 | .init_hctx = ublk_init_hctx, |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 959 | .init_request = ublk_init_rq, |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 960 | }; |
| 961 | |
| 962 | static int ublk_ch_open(struct inode *inode, struct file *filp) |
| 963 | { |
| 964 | struct ublk_device *ub = container_of(inode->i_cdev, |
| 965 | struct ublk_device, cdev); |
| 966 | |
Christoph Hellwig | fa36204 | 2022-07-21 15:09:12 +0200 | [diff] [blame] | 967 | if (test_and_set_bit(UB_STATE_OPEN, &ub->state)) |
| 968 | return -EBUSY; |
| 969 | filp->private_data = ub; |
| 970 | return 0; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static int ublk_ch_release(struct inode *inode, struct file *filp) |
| 974 | { |
| 975 | struct ublk_device *ub = filp->private_data; |
| 976 | |
Christoph Hellwig | fa36204 | 2022-07-21 15:09:12 +0200 | [diff] [blame] | 977 | clear_bit(UB_STATE_OPEN, &ub->state); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | /* map pre-allocated per-queue cmd buffer to ublksrv daemon */ |
| 982 | static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma) |
| 983 | { |
| 984 | struct ublk_device *ub = filp->private_data; |
| 985 | size_t sz = vma->vm_end - vma->vm_start; |
| 986 | unsigned max_sz = UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc); |
| 987 | unsigned long pfn, end, phys_off = vma->vm_pgoff << PAGE_SHIFT; |
| 988 | int q_id, ret = 0; |
| 989 | |
Ming Lei | e94eb45 | 2022-07-21 23:31:17 +0800 | [diff] [blame] | 990 | spin_lock(&ub->mm_lock); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 991 | if (!ub->mm) |
| 992 | ub->mm = current->mm; |
| 993 | if (current->mm != ub->mm) |
| 994 | ret = -EINVAL; |
Ming Lei | e94eb45 | 2022-07-21 23:31:17 +0800 | [diff] [blame] | 995 | spin_unlock(&ub->mm_lock); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 996 | |
| 997 | if (ret) |
| 998 | return ret; |
| 999 | |
| 1000 | if (vma->vm_flags & VM_WRITE) |
| 1001 | return -EPERM; |
| 1002 | |
| 1003 | end = UBLKSRV_CMD_BUF_OFFSET + ub->dev_info.nr_hw_queues * max_sz; |
| 1004 | if (phys_off < UBLKSRV_CMD_BUF_OFFSET || phys_off >= end) |
| 1005 | return -EINVAL; |
| 1006 | |
| 1007 | q_id = (phys_off - UBLKSRV_CMD_BUF_OFFSET) / max_sz; |
| 1008 | pr_devel("%s: qid %d, pid %d, addr %lx pg_off %lx sz %lu\n", |
| 1009 | __func__, q_id, current->pid, vma->vm_start, |
| 1010 | phys_off, (unsigned long)sz); |
| 1011 | |
| 1012 | if (sz != ublk_queue_cmd_buf_size(ub, q_id)) |
| 1013 | return -EINVAL; |
| 1014 | |
| 1015 | pfn = virt_to_phys(ublk_queue_cmd_buf(ub, q_id)) >> PAGE_SHIFT; |
| 1016 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 1017 | } |
| 1018 | |
| 1019 | static void ublk_commit_completion(struct ublk_device *ub, |
| 1020 | struct ublksrv_io_cmd *ub_cmd) |
| 1021 | { |
| 1022 | u32 qid = ub_cmd->q_id, tag = ub_cmd->tag; |
| 1023 | struct ublk_queue *ubq = ublk_get_queue(ub, qid); |
| 1024 | struct ublk_io *io = &ubq->ios[tag]; |
| 1025 | struct request *req; |
| 1026 | |
| 1027 | /* now this cmd slot is owned by nbd driver */ |
| 1028 | io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV; |
| 1029 | io->res = ub_cmd->result; |
| 1030 | |
| 1031 | /* find the io request and complete */ |
| 1032 | req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag); |
| 1033 | |
| 1034 | if (req && likely(!blk_should_fake_timeout(req->q))) |
| 1035 | ublk_complete_rq(req); |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * When ->ubq_daemon is exiting, either new request is ended immediately, |
| 1040 | * or any queued io command is drained, so it is safe to abort queue |
| 1041 | * lockless |
| 1042 | */ |
| 1043 | static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq) |
| 1044 | { |
| 1045 | int i; |
| 1046 | |
| 1047 | if (!ublk_get_device(ub)) |
| 1048 | return; |
| 1049 | |
| 1050 | for (i = 0; i < ubq->q_depth; i++) { |
| 1051 | struct ublk_io *io = &ubq->ios[i]; |
| 1052 | |
| 1053 | if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) { |
| 1054 | struct request *rq; |
| 1055 | |
| 1056 | /* |
| 1057 | * Either we fail the request or ublk_rq_task_work_fn |
| 1058 | * will do it |
| 1059 | */ |
| 1060 | rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i); |
| 1061 | if (rq) |
ZiyangZhang | a0d41dc | 2022-09-23 23:39:17 +0800 | [diff] [blame] | 1062 | __ublk_fail_req(ubq, io, rq); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | ublk_put_device(ub); |
| 1066 | } |
| 1067 | |
| 1068 | static void ublk_daemon_monitor_work(struct work_struct *work) |
| 1069 | { |
| 1070 | struct ublk_device *ub = |
| 1071 | container_of(work, struct ublk_device, monitor_work.work); |
| 1072 | int i; |
| 1073 | |
| 1074 | for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { |
| 1075 | struct ublk_queue *ubq = ublk_get_queue(ub, i); |
| 1076 | |
| 1077 | if (ubq_daemon_is_dying(ubq)) { |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1078 | if (ublk_queue_can_use_recovery(ubq)) |
| 1079 | schedule_work(&ub->quiesce_work); |
| 1080 | else |
| 1081 | schedule_work(&ub->stop_work); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1082 | |
| 1083 | /* abort queue is for making forward progress */ |
| 1084 | ublk_abort_queue(ub, ubq); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /* |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1089 | * We can't schedule monitor work after ub's state is not UBLK_S_DEV_LIVE. |
| 1090 | * after ublk_remove() or __ublk_quiesce_dev() is started. |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1091 | * |
| 1092 | * No need ub->mutex, monitor work are canceled after state is marked |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1093 | * as not LIVE, so new state is observed reliably. |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1094 | */ |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1095 | if (ub->dev_info.state == UBLK_S_DEV_LIVE) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1096 | schedule_delayed_work(&ub->monitor_work, |
| 1097 | UBLK_DAEMON_MONITOR_PERIOD); |
| 1098 | } |
| 1099 | |
Ming Lei | a8ce5f5 | 2022-07-30 17:27:47 +0800 | [diff] [blame] | 1100 | static inline bool ublk_queue_ready(struct ublk_queue *ubq) |
| 1101 | { |
| 1102 | return ubq->nr_io_ready == ubq->q_depth; |
| 1103 | } |
| 1104 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1105 | static void ublk_cancel_queue(struct ublk_queue *ubq) |
| 1106 | { |
| 1107 | int i; |
| 1108 | |
Ming Lei | a8ce5f5 | 2022-07-30 17:27:47 +0800 | [diff] [blame] | 1109 | if (!ublk_queue_ready(ubq)) |
| 1110 | return; |
| 1111 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1112 | for (i = 0; i < ubq->q_depth; i++) { |
| 1113 | struct ublk_io *io = &ubq->ios[i]; |
| 1114 | |
| 1115 | if (io->flags & UBLK_IO_FLAG_ACTIVE) |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 1116 | io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0, |
| 1117 | IO_URING_F_UNLOCKED); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1118 | } |
Ming Lei | a8ce5f5 | 2022-07-30 17:27:47 +0800 | [diff] [blame] | 1119 | |
| 1120 | /* all io commands are canceled */ |
| 1121 | ubq->nr_io_ready = 0; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /* Cancel all pending commands, must be called after del_gendisk() returns */ |
| 1125 | static void ublk_cancel_dev(struct ublk_device *ub) |
| 1126 | { |
| 1127 | int i; |
| 1128 | |
| 1129 | for (i = 0; i < ub->dev_info.nr_hw_queues; i++) |
| 1130 | ublk_cancel_queue(ublk_get_queue(ub, i)); |
| 1131 | } |
| 1132 | |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1133 | static bool ublk_check_inflight_rq(struct request *rq, void *data) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1134 | { |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1135 | bool *idle = data; |
| 1136 | |
| 1137 | if (blk_mq_request_started(rq)) { |
| 1138 | *idle = false; |
| 1139 | return false; |
| 1140 | } |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | static void ublk_wait_tagset_rqs_idle(struct ublk_device *ub) |
| 1145 | { |
| 1146 | bool idle; |
| 1147 | |
| 1148 | WARN_ON_ONCE(!blk_queue_quiesced(ub->ub_disk->queue)); |
| 1149 | while (true) { |
| 1150 | idle = true; |
| 1151 | blk_mq_tagset_busy_iter(&ub->tag_set, |
| 1152 | ublk_check_inflight_rq, &idle); |
| 1153 | if (idle) |
| 1154 | break; |
| 1155 | msleep(UBLK_REQUEUE_DELAY_MS); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | static void __ublk_quiesce_dev(struct ublk_device *ub) |
| 1160 | { |
| 1161 | pr_devel("%s: quiesce ub: dev_id %d state %s\n", |
| 1162 | __func__, ub->dev_info.dev_id, |
| 1163 | ub->dev_info.state == UBLK_S_DEV_LIVE ? |
| 1164 | "LIVE" : "QUIESCED"); |
| 1165 | blk_mq_quiesce_queue(ub->ub_disk->queue); |
| 1166 | ublk_wait_tagset_rqs_idle(ub); |
| 1167 | ub->dev_info.state = UBLK_S_DEV_QUIESCED; |
| 1168 | ublk_cancel_dev(ub); |
| 1169 | /* we are going to release task_struct of ubq_daemon and resets |
| 1170 | * ->ubq_daemon to NULL. So in monitor_work, check on ubq_daemon causes UAF. |
| 1171 | * Besides, monitor_work is not necessary in QUIESCED state since we have |
| 1172 | * already scheduled quiesce_work and quiesced all ubqs. |
| 1173 | * |
| 1174 | * Do not let monitor_work schedule itself if state it QUIESCED. And we cancel |
| 1175 | * it here and re-schedule it in END_USER_RECOVERY to avoid UAF. |
| 1176 | */ |
| 1177 | cancel_delayed_work_sync(&ub->monitor_work); |
| 1178 | } |
| 1179 | |
| 1180 | static void ublk_quiesce_work_fn(struct work_struct *work) |
| 1181 | { |
| 1182 | struct ublk_device *ub = |
| 1183 | container_of(work, struct ublk_device, quiesce_work); |
| 1184 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1185 | mutex_lock(&ub->mutex); |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1186 | if (ub->dev_info.state != UBLK_S_DEV_LIVE) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1187 | goto unlock; |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1188 | __ublk_quiesce_dev(ub); |
| 1189 | unlock: |
| 1190 | mutex_unlock(&ub->mutex); |
| 1191 | } |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1192 | |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1193 | static void ublk_unquiesce_dev(struct ublk_device *ub) |
| 1194 | { |
| 1195 | int i; |
| 1196 | |
| 1197 | pr_devel("%s: unquiesce ub: dev_id %d state %s\n", |
| 1198 | __func__, ub->dev_info.dev_id, |
| 1199 | ub->dev_info.state == UBLK_S_DEV_LIVE ? |
| 1200 | "LIVE" : "QUIESCED"); |
| 1201 | /* quiesce_work has run. We let requeued rqs be aborted |
| 1202 | * before running fallback_wq. "force_abort" must be seen |
| 1203 | * after request queue is unqiuesced. Then del_gendisk() |
| 1204 | * can move on. |
| 1205 | */ |
| 1206 | for (i = 0; i < ub->dev_info.nr_hw_queues; i++) |
| 1207 | ublk_get_queue(ub, i)->force_abort = true; |
| 1208 | |
| 1209 | blk_mq_unquiesce_queue(ub->ub_disk->queue); |
| 1210 | /* We may have requeued some rqs in ublk_quiesce_queue() */ |
| 1211 | blk_mq_kick_requeue_list(ub->ub_disk->queue); |
| 1212 | } |
| 1213 | |
| 1214 | static void ublk_stop_dev(struct ublk_device *ub) |
| 1215 | { |
| 1216 | mutex_lock(&ub->mutex); |
| 1217 | if (ub->dev_info.state == UBLK_S_DEV_DEAD) |
| 1218 | goto unlock; |
| 1219 | if (ublk_can_use_recovery(ub)) { |
| 1220 | if (ub->dev_info.state == UBLK_S_DEV_LIVE) |
| 1221 | __ublk_quiesce_dev(ub); |
| 1222 | ublk_unquiesce_dev(ub); |
| 1223 | } |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1224 | del_gendisk(ub->ub_disk); |
| 1225 | ub->dev_info.state = UBLK_S_DEV_DEAD; |
| 1226 | ub->dev_info.ublksrv_pid = -1; |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1227 | put_disk(ub->ub_disk); |
| 1228 | ub->ub_disk = NULL; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1229 | unlock: |
Ming Lei | a8ce5f5 | 2022-07-30 17:27:47 +0800 | [diff] [blame] | 1230 | ublk_cancel_dev(ub); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1231 | mutex_unlock(&ub->mutex); |
| 1232 | cancel_delayed_work_sync(&ub->monitor_work); |
| 1233 | } |
| 1234 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1235 | /* device can only be started after all IOs are ready */ |
| 1236 | static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq) |
| 1237 | { |
| 1238 | mutex_lock(&ub->mutex); |
| 1239 | ubq->nr_io_ready++; |
| 1240 | if (ublk_queue_ready(ubq)) { |
| 1241 | ubq->ubq_daemon = current; |
| 1242 | get_task_struct(ubq->ubq_daemon); |
| 1243 | ub->nr_queues_ready++; |
Ming Lei | 73a166d | 2023-01-06 12:17:07 +0800 | [diff] [blame] | 1244 | |
| 1245 | if (capable(CAP_SYS_ADMIN)) |
| 1246 | ub->nr_privileged_daemon++; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1247 | } |
| 1248 | if (ub->nr_queues_ready == ub->dev_info.nr_hw_queues) |
| 1249 | complete_all(&ub->completion); |
| 1250 | mutex_unlock(&ub->mutex); |
| 1251 | } |
| 1252 | |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1253 | static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id, |
Ming Lei | fee32f3 | 2022-10-29 09:04:32 +0800 | [diff] [blame] | 1254 | int tag) |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1255 | { |
| 1256 | struct ublk_queue *ubq = ublk_get_queue(ub, q_id); |
| 1257 | struct request *req = blk_mq_tag_to_rq(ub->tag_set.tags[q_id], tag); |
| 1258 | |
Ming Lei | 7d4a931 | 2022-11-21 23:56:45 +0800 | [diff] [blame] | 1259 | ublk_queue_cmd(ubq, req); |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 1262 | static inline int ublk_check_cmd_op(u32 cmd_op) |
| 1263 | { |
| 1264 | u32 ioc_type = _IOC_TYPE(cmd_op); |
| 1265 | |
| 1266 | if (IS_ENABLED(CONFIG_BLKDEV_UBLK_LEGACY_OPCODES) && ioc_type != 'u') |
| 1267 | return -EOPNOTSUPP; |
| 1268 | |
| 1269 | if (ioc_type != 'u' && ioc_type != 0) |
| 1270 | return -EOPNOTSUPP; |
| 1271 | |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Jens Axboe | 8c68ae3 | 2023-04-05 20:00:46 -0600 | [diff] [blame] | 1275 | static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, |
| 1276 | unsigned int issue_flags, |
| 1277 | struct ublksrv_io_cmd *ub_cmd) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1278 | { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1279 | struct ublk_device *ub = cmd->file->private_data; |
| 1280 | struct ublk_queue *ubq; |
| 1281 | struct ublk_io *io; |
| 1282 | u32 cmd_op = cmd->cmd_op; |
| 1283 | unsigned tag = ub_cmd->tag; |
| 1284 | int ret = -EINVAL; |
Liu Xiaodong | 2f1e07d | 2023-02-10 09:13:56 -0500 | [diff] [blame] | 1285 | struct request *req; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1286 | |
| 1287 | pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n", |
| 1288 | __func__, cmd->cmd_op, ub_cmd->q_id, tag, |
| 1289 | ub_cmd->result); |
| 1290 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1291 | if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues) |
| 1292 | goto out; |
| 1293 | |
| 1294 | ubq = ublk_get_queue(ub, ub_cmd->q_id); |
| 1295 | if (!ubq || ub_cmd->q_id != ubq->q_id) |
| 1296 | goto out; |
| 1297 | |
| 1298 | if (ubq->ubq_daemon && ubq->ubq_daemon != current) |
| 1299 | goto out; |
| 1300 | |
| 1301 | if (tag >= ubq->q_depth) |
| 1302 | goto out; |
| 1303 | |
| 1304 | io = &ubq->ios[tag]; |
| 1305 | |
| 1306 | /* there is pending io cmd, something must be wrong */ |
| 1307 | if (io->flags & UBLK_IO_FLAG_ACTIVE) { |
| 1308 | ret = -EBUSY; |
| 1309 | goto out; |
| 1310 | } |
| 1311 | |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1312 | /* |
| 1313 | * ensure that the user issues UBLK_IO_NEED_GET_DATA |
| 1314 | * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA. |
| 1315 | */ |
| 1316 | if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA)) |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 1317 | ^ (_IOC_NR(cmd_op) == UBLK_IO_NEED_GET_DATA)) |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1318 | goto out; |
| 1319 | |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 1320 | ret = ublk_check_cmd_op(cmd_op); |
| 1321 | if (ret) |
| 1322 | goto out; |
| 1323 | |
Ming Lei | 7c75661 | 2023-04-20 17:11:04 +0800 | [diff] [blame] | 1324 | ret = -EINVAL; |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 1325 | switch (_IOC_NR(cmd_op)) { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1326 | case UBLK_IO_FETCH_REQ: |
| 1327 | /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */ |
| 1328 | if (ublk_queue_ready(ubq)) { |
| 1329 | ret = -EBUSY; |
| 1330 | goto out; |
| 1331 | } |
| 1332 | /* |
| 1333 | * The io is being handled by server, so COMMIT_RQ is expected |
| 1334 | * instead of FETCH_REQ |
| 1335 | */ |
| 1336 | if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV) |
| 1337 | goto out; |
Liu Xiaodong | 2f1e07d | 2023-02-10 09:13:56 -0500 | [diff] [blame] | 1338 | /* FETCH_RQ has to provide IO buffer if NEED GET DATA is not enabled */ |
| 1339 | if (!ub_cmd->addr && !ublk_need_get_data(ubq)) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1340 | goto out; |
| 1341 | io->cmd = cmd; |
| 1342 | io->flags |= UBLK_IO_FLAG_ACTIVE; |
| 1343 | io->addr = ub_cmd->addr; |
| 1344 | |
| 1345 | ublk_mark_io_ready(ub, ubq); |
| 1346 | break; |
| 1347 | case UBLK_IO_COMMIT_AND_FETCH_REQ: |
Liu Xiaodong | 2f1e07d | 2023-02-10 09:13:56 -0500 | [diff] [blame] | 1348 | req = blk_mq_tag_to_rq(ub->tag_set.tags[ub_cmd->q_id], tag); |
| 1349 | /* |
| 1350 | * COMMIT_AND_FETCH_REQ has to provide IO buffer if NEED GET DATA is |
| 1351 | * not enabled or it is Read IO. |
| 1352 | */ |
| 1353 | if (!ub_cmd->addr && (!ublk_need_get_data(ubq) || req_op(req) == REQ_OP_READ)) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1354 | goto out; |
| 1355 | if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)) |
| 1356 | goto out; |
| 1357 | io->addr = ub_cmd->addr; |
| 1358 | io->flags |= UBLK_IO_FLAG_ACTIVE; |
| 1359 | io->cmd = cmd; |
| 1360 | ublk_commit_completion(ub, ub_cmd); |
| 1361 | break; |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1362 | case UBLK_IO_NEED_GET_DATA: |
| 1363 | if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)) |
| 1364 | goto out; |
| 1365 | io->addr = ub_cmd->addr; |
| 1366 | io->cmd = cmd; |
| 1367 | io->flags |= UBLK_IO_FLAG_ACTIVE; |
Ming Lei | fee32f3 | 2022-10-29 09:04:32 +0800 | [diff] [blame] | 1368 | ublk_handle_need_get_data(ub, ub_cmd->q_id, ub_cmd->tag); |
ZiyangZhang | c86019f | 2022-07-28 20:39:16 +0800 | [diff] [blame] | 1369 | break; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1370 | default: |
| 1371 | goto out; |
| 1372 | } |
| 1373 | return -EIOCBQUEUED; |
| 1374 | |
| 1375 | out: |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 1376 | io_uring_cmd_done(cmd, ret, 0, issue_flags); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1377 | pr_devel("%s: complete: cmd op %d, tag %d ret %x io_flags %x\n", |
| 1378 | __func__, cmd_op, tag, ret, io->flags); |
| 1379 | return -EIOCBQUEUED; |
| 1380 | } |
| 1381 | |
Jens Axboe | 8c68ae3 | 2023-04-05 20:00:46 -0600 | [diff] [blame] | 1382 | static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) |
| 1383 | { |
| 1384 | struct ublksrv_io_cmd *ub_src = (struct ublksrv_io_cmd *) cmd->cmd; |
| 1385 | struct ublksrv_io_cmd ub_cmd; |
| 1386 | |
| 1387 | /* |
| 1388 | * Not necessary for async retry, but let's keep it simple and always |
| 1389 | * copy the values to avoid any potential reuse. |
| 1390 | */ |
| 1391 | ub_cmd.q_id = READ_ONCE(ub_src->q_id); |
| 1392 | ub_cmd.tag = READ_ONCE(ub_src->tag); |
| 1393 | ub_cmd.result = READ_ONCE(ub_src->result); |
| 1394 | ub_cmd.addr = READ_ONCE(ub_src->addr); |
| 1395 | |
| 1396 | return __ublk_ch_uring_cmd(cmd, issue_flags, &ub_cmd); |
| 1397 | } |
| 1398 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1399 | static const struct file_operations ublk_ch_fops = { |
| 1400 | .owner = THIS_MODULE, |
| 1401 | .open = ublk_ch_open, |
| 1402 | .release = ublk_ch_release, |
| 1403 | .llseek = no_llseek, |
| 1404 | .uring_cmd = ublk_ch_uring_cmd, |
| 1405 | .mmap = ublk_ch_mmap, |
| 1406 | }; |
| 1407 | |
| 1408 | static void ublk_deinit_queue(struct ublk_device *ub, int q_id) |
| 1409 | { |
| 1410 | int size = ublk_queue_cmd_buf_size(ub, q_id); |
| 1411 | struct ublk_queue *ubq = ublk_get_queue(ub, q_id); |
| 1412 | |
| 1413 | if (ubq->ubq_daemon) |
| 1414 | put_task_struct(ubq->ubq_daemon); |
| 1415 | if (ubq->io_cmd_buf) |
| 1416 | free_pages((unsigned long)ubq->io_cmd_buf, get_order(size)); |
| 1417 | } |
| 1418 | |
| 1419 | static int ublk_init_queue(struct ublk_device *ub, int q_id) |
| 1420 | { |
| 1421 | struct ublk_queue *ubq = ublk_get_queue(ub, q_id); |
| 1422 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO; |
| 1423 | void *ptr; |
| 1424 | int size; |
| 1425 | |
Ming Lei | 6d8c5af | 2022-07-22 18:38:17 +0800 | [diff] [blame] | 1426 | ubq->flags = ub->dev_info.flags; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1427 | ubq->q_id = q_id; |
| 1428 | ubq->q_depth = ub->dev_info.queue_depth; |
| 1429 | size = ublk_queue_cmd_buf_size(ub, q_id); |
| 1430 | |
| 1431 | ptr = (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 1432 | if (!ptr) |
| 1433 | return -ENOMEM; |
| 1434 | |
| 1435 | ubq->io_cmd_buf = ptr; |
| 1436 | ubq->dev = ub; |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static void ublk_deinit_queues(struct ublk_device *ub) |
| 1441 | { |
| 1442 | int nr_queues = ub->dev_info.nr_hw_queues; |
| 1443 | int i; |
| 1444 | |
| 1445 | if (!ub->__queues) |
| 1446 | return; |
| 1447 | |
| 1448 | for (i = 0; i < nr_queues; i++) |
| 1449 | ublk_deinit_queue(ub, i); |
| 1450 | kfree(ub->__queues); |
| 1451 | } |
| 1452 | |
| 1453 | static int ublk_init_queues(struct ublk_device *ub) |
| 1454 | { |
| 1455 | int nr_queues = ub->dev_info.nr_hw_queues; |
| 1456 | int depth = ub->dev_info.queue_depth; |
| 1457 | int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io); |
| 1458 | int i, ret = -ENOMEM; |
| 1459 | |
| 1460 | ub->queue_size = ubq_size; |
| 1461 | ub->__queues = kcalloc(nr_queues, ubq_size, GFP_KERNEL); |
| 1462 | if (!ub->__queues) |
| 1463 | return ret; |
| 1464 | |
| 1465 | for (i = 0; i < nr_queues; i++) { |
| 1466 | if (ublk_init_queue(ub, i)) |
| 1467 | goto fail; |
| 1468 | } |
| 1469 | |
| 1470 | init_completion(&ub->completion); |
| 1471 | return 0; |
| 1472 | |
| 1473 | fail: |
| 1474 | ublk_deinit_queues(ub); |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1478 | static int ublk_alloc_dev_number(struct ublk_device *ub, int idx) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1479 | { |
| 1480 | int i = idx; |
| 1481 | int err; |
| 1482 | |
| 1483 | spin_lock(&ublk_idr_lock); |
| 1484 | /* allocate id, if @id >= 0, we're requesting that specific id */ |
| 1485 | if (i >= 0) { |
| 1486 | err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT); |
| 1487 | if (err == -ENOSPC) |
| 1488 | err = -EEXIST; |
| 1489 | } else { |
| 1490 | err = idr_alloc(&ublk_index_idr, ub, 0, 0, GFP_NOWAIT); |
| 1491 | } |
| 1492 | spin_unlock(&ublk_idr_lock); |
| 1493 | |
| 1494 | if (err >= 0) |
| 1495 | ub->ub_number = err; |
| 1496 | |
| 1497 | return err; |
| 1498 | } |
| 1499 | |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1500 | static void ublk_free_dev_number(struct ublk_device *ub) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1501 | { |
| 1502 | spin_lock(&ublk_idr_lock); |
| 1503 | idr_remove(&ublk_index_idr, ub->ub_number); |
| 1504 | wake_up_all(&ublk_idr_wq); |
| 1505 | spin_unlock(&ublk_idr_lock); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static void ublk_cdev_rel(struct device *dev) |
| 1509 | { |
| 1510 | struct ublk_device *ub = container_of(dev, struct ublk_device, cdev_dev); |
| 1511 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1512 | blk_mq_free_tag_set(&ub->tag_set); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1513 | ublk_deinit_queues(ub); |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1514 | ublk_free_dev_number(ub); |
| 1515 | mutex_destroy(&ub->mutex); |
| 1516 | kfree(ub); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | static int ublk_add_chdev(struct ublk_device *ub) |
| 1520 | { |
| 1521 | struct device *dev = &ub->cdev_dev; |
| 1522 | int minor = ub->ub_number; |
| 1523 | int ret; |
| 1524 | |
| 1525 | dev->parent = ublk_misc.this_device; |
| 1526 | dev->devt = MKDEV(MAJOR(ublk_chr_devt), minor); |
| 1527 | dev->class = ublk_chr_class; |
| 1528 | dev->release = ublk_cdev_rel; |
| 1529 | device_initialize(dev); |
| 1530 | |
| 1531 | ret = dev_set_name(dev, "ublkc%d", minor); |
| 1532 | if (ret) |
| 1533 | goto fail; |
| 1534 | |
| 1535 | cdev_init(&ub->cdev, &ublk_ch_fops); |
| 1536 | ret = cdev_device_add(&ub->cdev, dev); |
| 1537 | if (ret) |
| 1538 | goto fail; |
Ming Lei | 403ebc8 | 2023-01-06 12:17:10 +0800 | [diff] [blame] | 1539 | |
| 1540 | ublks_added++; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1541 | return 0; |
| 1542 | fail: |
| 1543 | put_device(dev); |
| 1544 | return ret; |
| 1545 | } |
| 1546 | |
| 1547 | static void ublk_stop_work_fn(struct work_struct *work) |
| 1548 | { |
| 1549 | struct ublk_device *ub = |
| 1550 | container_of(work, struct ublk_device, stop_work); |
| 1551 | |
| 1552 | ublk_stop_dev(ub); |
| 1553 | } |
| 1554 | |
Ming Lei | 4bf9cbf | 2022-07-30 17:27:50 +0800 | [diff] [blame] | 1555 | /* align max io buffer size with PAGE_SIZE */ |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1556 | static void ublk_align_max_io_size(struct ublk_device *ub) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1557 | { |
Ming Lei | 4bf9cbf | 2022-07-30 17:27:50 +0800 | [diff] [blame] | 1558 | unsigned int max_io_bytes = ub->dev_info.max_io_buf_bytes; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1559 | |
Ming Lei | 4bf9cbf | 2022-07-30 17:27:50 +0800 | [diff] [blame] | 1560 | ub->dev_info.max_io_buf_bytes = |
| 1561 | round_down(max_io_bytes, PAGE_SIZE); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1564 | static int ublk_add_tag_set(struct ublk_device *ub) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1565 | { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1566 | ub->tag_set.ops = &ublk_mq_ops; |
| 1567 | ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues; |
| 1568 | ub->tag_set.queue_depth = ub->dev_info.queue_depth; |
| 1569 | ub->tag_set.numa_node = NUMA_NO_NODE; |
Ming Lei | 0edb369 | 2022-07-13 22:07:11 +0800 | [diff] [blame] | 1570 | ub->tag_set.cmd_size = sizeof(struct ublk_rq_data); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1571 | ub->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 1572 | ub->tag_set.driver_data = ub; |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1573 | return blk_mq_alloc_tag_set(&ub->tag_set); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | static void ublk_remove(struct ublk_device *ub) |
| 1577 | { |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1578 | ublk_stop_dev(ub); |
| 1579 | cancel_work_sync(&ub->stop_work); |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1580 | cancel_work_sync(&ub->quiesce_work); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1581 | cdev_device_del(&ub->cdev, &ub->cdev_dev); |
| 1582 | put_device(&ub->cdev_dev); |
Ming Lei | 403ebc8 | 2023-01-06 12:17:10 +0800 | [diff] [blame] | 1583 | ublks_added--; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | static struct ublk_device *ublk_get_device_from_id(int idx) |
| 1587 | { |
| 1588 | struct ublk_device *ub = NULL; |
| 1589 | |
| 1590 | if (idx < 0) |
| 1591 | return NULL; |
| 1592 | |
| 1593 | spin_lock(&ublk_idr_lock); |
| 1594 | ub = idr_find(&ublk_index_idr, idx); |
| 1595 | if (ub) |
| 1596 | ub = ublk_get_device(ub); |
| 1597 | spin_unlock(&ublk_idr_lock); |
| 1598 | |
| 1599 | return ub; |
| 1600 | } |
| 1601 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1602 | static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1603 | { |
| 1604 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1605 | int ublksrv_pid = (int)header->data[0]; |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1606 | struct gendisk *disk; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1607 | int ret = -EINVAL; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1608 | |
| 1609 | if (ublksrv_pid <= 0) |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1610 | return -EINVAL; |
| 1611 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1612 | wait_for_completion_interruptible(&ub->completion); |
| 1613 | |
| 1614 | schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD); |
| 1615 | |
| 1616 | mutex_lock(&ub->mutex); |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1617 | if (ub->dev_info.state == UBLK_S_DEV_LIVE || |
| 1618 | test_bit(UB_STATE_USED, &ub->state)) { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1619 | ret = -EEXIST; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1620 | goto out_unlock; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1621 | } |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1622 | |
Ziyang Zhang | 1972d03 | 2023-02-07 15:08:39 +0800 | [diff] [blame] | 1623 | disk = blk_mq_alloc_disk(&ub->tag_set, NULL); |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1624 | if (IS_ERR(disk)) { |
| 1625 | ret = PTR_ERR(disk); |
| 1626 | goto out_unlock; |
| 1627 | } |
| 1628 | sprintf(disk->disk_name, "ublkb%d", ub->ub_number); |
| 1629 | disk->fops = &ub_fops; |
| 1630 | disk->private_data = ub; |
| 1631 | |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1632 | ub->dev_info.ublksrv_pid = ublksrv_pid; |
| 1633 | ub->ub_disk = disk; |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1634 | |
| 1635 | ret = ublk_apply_params(ub); |
| 1636 | if (ret) |
| 1637 | goto out_put_disk; |
| 1638 | |
Ming Lei | 73a166d | 2023-01-06 12:17:07 +0800 | [diff] [blame] | 1639 | /* don't probe partitions if any one ubq daemon is un-trusted */ |
| 1640 | if (ub->nr_privileged_daemon != ub->nr_queues_ready) |
| 1641 | set_bit(GD_SUPPRESS_PART_SCAN, &disk->state); |
| 1642 | |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1643 | get_device(&ub->cdev_dev); |
Ming Lei | 4985e7b | 2023-03-18 22:12:31 +0800 | [diff] [blame] | 1644 | ub->dev_info.state = UBLK_S_DEV_LIVE; |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1645 | ret = add_disk(disk); |
| 1646 | if (ret) { |
Ming Lei | 93d71ec | 2022-07-30 17:27:48 +0800 | [diff] [blame] | 1647 | /* |
| 1648 | * Has to drop the reference since ->free_disk won't be |
| 1649 | * called in case of add_disk failure. |
| 1650 | */ |
Ming Lei | 4985e7b | 2023-03-18 22:12:31 +0800 | [diff] [blame] | 1651 | ub->dev_info.state = UBLK_S_DEV_DEAD; |
Ming Lei | 93d71ec | 2022-07-30 17:27:48 +0800 | [diff] [blame] | 1652 | ublk_put_device(ub); |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1653 | goto out_put_disk; |
Christoph Hellwig | 6d9e6df | 2022-07-21 15:09:16 +0200 | [diff] [blame] | 1654 | } |
| 1655 | set_bit(UB_STATE_USED, &ub->state); |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1656 | out_put_disk: |
| 1657 | if (ret) |
| 1658 | put_disk(disk); |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1659 | out_unlock: |
| 1660 | mutex_unlock(&ub->mutex); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1661 | return ret; |
| 1662 | } |
| 1663 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1664 | static int ublk_ctrl_get_queue_affinity(struct ublk_device *ub, |
| 1665 | struct io_uring_cmd *cmd) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1666 | { |
| 1667 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 1668 | void __user *argp = (void __user *)(unsigned long)header->addr; |
Christoph Hellwig | c50061f | 2022-07-21 15:09:15 +0200 | [diff] [blame] | 1669 | cpumask_var_t cpumask; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1670 | unsigned long queue; |
| 1671 | unsigned int retlen; |
Christoph Hellwig | c50061f | 2022-07-21 15:09:15 +0200 | [diff] [blame] | 1672 | unsigned int i; |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1673 | int ret; |
| 1674 | |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1675 | if (header->len * BITS_PER_BYTE < nr_cpu_ids) |
| 1676 | return -EINVAL; |
| 1677 | if (header->len & (sizeof(unsigned long)-1)) |
| 1678 | return -EINVAL; |
| 1679 | if (!header->addr) |
| 1680 | return -EINVAL; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1681 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1682 | queue = header->data[0]; |
| 1683 | if (queue >= ub->dev_info.nr_hw_queues) |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1684 | return -EINVAL; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1685 | |
Christoph Hellwig | c50061f | 2022-07-21 15:09:15 +0200 | [diff] [blame] | 1686 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1687 | return -ENOMEM; |
Christoph Hellwig | c50061f | 2022-07-21 15:09:15 +0200 | [diff] [blame] | 1688 | |
| 1689 | for_each_possible_cpu(i) { |
| 1690 | if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue) |
| 1691 | cpumask_set_cpu(i, cpumask); |
| 1692 | } |
| 1693 | |
| 1694 | ret = -EFAULT; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1695 | retlen = min_t(unsigned short, header->len, cpumask_size()); |
Christoph Hellwig | c50061f | 2022-07-21 15:09:15 +0200 | [diff] [blame] | 1696 | if (copy_to_user(argp, cpumask, retlen)) |
| 1697 | goto out_free_cpumask; |
| 1698 | if (retlen != header->len && |
| 1699 | clear_user(argp + retlen, header->len - retlen)) |
| 1700 | goto out_free_cpumask; |
| 1701 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1702 | ret = 0; |
Christoph Hellwig | c50061f | 2022-07-21 15:09:15 +0200 | [diff] [blame] | 1703 | out_free_cpumask: |
| 1704 | free_cpumask_var(cpumask); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1705 | return ret; |
| 1706 | } |
| 1707 | |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1708 | static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info *info) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1709 | { |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1710 | pr_devel("%s: dev id %d flags %llx\n", __func__, |
Ming Lei | 6d8c5af | 2022-07-22 18:38:17 +0800 | [diff] [blame] | 1711 | info->dev_id, info->flags); |
Ming Lei | 4bf9cbf | 2022-07-30 17:27:50 +0800 | [diff] [blame] | 1712 | pr_devel("\t nr_hw_queues %d queue_depth %d\n", |
| 1713 | info->nr_hw_queues, info->queue_depth); |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) |
| 1717 | { |
| 1718 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 1719 | void __user *argp = (void __user *)(unsigned long)header->addr; |
| 1720 | struct ublksrv_ctrl_dev_info info; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1721 | struct ublk_device *ub; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1722 | int ret = -EINVAL; |
| 1723 | |
| 1724 | if (header->len < sizeof(info) || !header->addr) |
| 1725 | return -EINVAL; |
| 1726 | if (header->queue_id != (u16)-1) { |
| 1727 | pr_warn("%s: queue_id is wrong %x\n", |
| 1728 | __func__, header->queue_id); |
| 1729 | return -EINVAL; |
| 1730 | } |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 1731 | |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1732 | if (copy_from_user(&info, argp, sizeof(info))) |
| 1733 | return -EFAULT; |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 1734 | |
| 1735 | if (capable(CAP_SYS_ADMIN)) |
| 1736 | info.flags &= ~UBLK_F_UNPRIVILEGED_DEV; |
| 1737 | else if (!(info.flags & UBLK_F_UNPRIVILEGED_DEV)) |
| 1738 | return -EPERM; |
| 1739 | |
| 1740 | /* the created device is always owned by current user */ |
Ming Lei | 48a9051 | 2023-01-31 12:04:46 +0800 | [diff] [blame] | 1741 | ublk_store_owner_uid_gid(&info.owner_uid, &info.owner_gid); |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 1742 | |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1743 | if (header->dev_id != info.dev_id) { |
| 1744 | pr_warn("%s: dev id not match %u %u\n", |
| 1745 | __func__, header->dev_id, info.dev_id); |
| 1746 | return -EINVAL; |
| 1747 | } |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1748 | |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 1749 | ublk_dump_dev_info(&info); |
| 1750 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1751 | ret = mutex_lock_killable(&ublk_ctl_mutex); |
| 1752 | if (ret) |
| 1753 | return ret; |
| 1754 | |
Ming Lei | 403ebc8 | 2023-01-06 12:17:10 +0800 | [diff] [blame] | 1755 | ret = -EACCES; |
| 1756 | if (ublks_added >= ublks_max) |
| 1757 | goto out_unlock; |
| 1758 | |
Christoph Hellwig | cfee7e4 | 2022-07-21 15:09:14 +0200 | [diff] [blame] | 1759 | ret = -ENOMEM; |
| 1760 | ub = kzalloc(sizeof(*ub), GFP_KERNEL); |
| 1761 | if (!ub) |
| 1762 | goto out_unlock; |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1763 | mutex_init(&ub->mutex); |
| 1764 | spin_lock_init(&ub->mm_lock); |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1765 | INIT_WORK(&ub->quiesce_work, ublk_quiesce_work_fn); |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1766 | INIT_WORK(&ub->stop_work, ublk_stop_work_fn); |
| 1767 | INIT_DELAYED_WORK(&ub->monitor_work, ublk_daemon_monitor_work); |
Christoph Hellwig | cfee7e4 | 2022-07-21 15:09:14 +0200 | [diff] [blame] | 1768 | |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1769 | ret = ublk_alloc_dev_number(ub, header->dev_id); |
| 1770 | if (ret < 0) |
| 1771 | goto out_free_ub; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1772 | |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1773 | memcpy(&ub->dev_info, &info, sizeof(info)); |
| 1774 | |
| 1775 | /* update device id */ |
| 1776 | ub->dev_info.dev_id = ub->ub_number; |
| 1777 | |
Ming Lei | 6d8c5af | 2022-07-22 18:38:17 +0800 | [diff] [blame] | 1778 | /* |
| 1779 | * 64bit flags will be copied back to userspace as feature |
| 1780 | * negotiation result, so have to clear flags which driver |
| 1781 | * doesn't support yet, then userspace can get correct flags |
| 1782 | * (features) to handle. |
| 1783 | */ |
| 1784 | ub->dev_info.flags &= UBLK_F_ALL; |
| 1785 | |
Ming Lei | 224e858 | 2022-10-29 09:04:29 +0800 | [diff] [blame] | 1786 | if (!IS_BUILTIN(CONFIG_BLK_DEV_UBLK)) |
| 1787 | ub->dev_info.flags |= UBLK_F_URING_CMD_COMP_IN_TASK; |
| 1788 | |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 1789 | ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE; |
| 1790 | |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1791 | /* We are not ready to support zero copy */ |
Ming Lei | 6d8c5af | 2022-07-22 18:38:17 +0800 | [diff] [blame] | 1792 | ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1793 | |
Christoph Hellwig | fa9482e | 2022-07-22 18:38:16 +0800 | [diff] [blame] | 1794 | ub->dev_info.nr_hw_queues = min_t(unsigned int, |
| 1795 | ub->dev_info.nr_hw_queues, nr_cpu_ids); |
| 1796 | ublk_align_max_io_size(ub); |
| 1797 | |
| 1798 | ret = ublk_init_queues(ub); |
| 1799 | if (ret) |
| 1800 | goto out_free_dev_number; |
| 1801 | |
| 1802 | ret = ublk_add_tag_set(ub); |
| 1803 | if (ret) |
| 1804 | goto out_deinit_queues; |
| 1805 | |
| 1806 | ret = -EFAULT; |
| 1807 | if (copy_to_user(argp, &ub->dev_info, sizeof(info))) |
| 1808 | goto out_free_tag_set; |
| 1809 | |
| 1810 | /* |
| 1811 | * Add the char dev so that ublksrv daemon can be setup. |
| 1812 | * ublk_add_chdev() will cleanup everything if it fails. |
| 1813 | */ |
| 1814 | ret = ublk_add_chdev(ub); |
| 1815 | goto out_unlock; |
| 1816 | |
| 1817 | out_free_tag_set: |
| 1818 | blk_mq_free_tag_set(&ub->tag_set); |
| 1819 | out_deinit_queues: |
| 1820 | ublk_deinit_queues(ub); |
| 1821 | out_free_dev_number: |
| 1822 | ublk_free_dev_number(ub); |
| 1823 | out_free_ub: |
| 1824 | mutex_destroy(&ub->mutex); |
| 1825 | kfree(ub); |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1826 | out_unlock: |
| 1827 | mutex_unlock(&ublk_ctl_mutex); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1828 | return ret; |
| 1829 | } |
| 1830 | |
| 1831 | static inline bool ublk_idr_freed(int id) |
| 1832 | { |
| 1833 | void *ptr; |
| 1834 | |
| 1835 | spin_lock(&ublk_idr_lock); |
| 1836 | ptr = idr_find(&ublk_index_idr, id); |
| 1837 | spin_unlock(&ublk_idr_lock); |
| 1838 | |
| 1839 | return ptr == NULL; |
| 1840 | } |
| 1841 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1842 | static int ublk_ctrl_del_dev(struct ublk_device **p_ub) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1843 | { |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1844 | struct ublk_device *ub = *p_ub; |
| 1845 | int idx = ub->ub_number; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1846 | int ret; |
| 1847 | |
| 1848 | ret = mutex_lock_killable(&ublk_ctl_mutex); |
| 1849 | if (ret) |
| 1850 | return ret; |
| 1851 | |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 1852 | if (!test_bit(UB_STATE_DELETED, &ub->state)) { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1853 | ublk_remove(ub); |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 1854 | set_bit(UB_STATE_DELETED, &ub->state); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1857 | /* Mark the reference as consumed */ |
| 1858 | *p_ub = NULL; |
| 1859 | ublk_put_device(ub); |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 1860 | mutex_unlock(&ublk_ctl_mutex); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1861 | |
| 1862 | /* |
| 1863 | * Wait until the idr is removed, then it can be reused after |
| 1864 | * DEL_DEV command is returned. |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 1865 | * |
| 1866 | * If we returns because of user interrupt, future delete command |
| 1867 | * may come: |
| 1868 | * |
| 1869 | * - the device number isn't freed, this device won't or needn't |
| 1870 | * be deleted again, since UB_STATE_DELETED is set, and device |
| 1871 | * will be released after the last reference is dropped |
| 1872 | * |
| 1873 | * - the device number is freed already, we will not find this |
| 1874 | * device via ublk_get_device_from_id() |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1875 | */ |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 1876 | wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx)); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1877 | |
Ming Lei | 0abe39d | 2023-02-07 23:07:00 +0800 | [diff] [blame] | 1878 | return 0; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1879 | } |
| 1880 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1881 | static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd) |
| 1882 | { |
| 1883 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 1884 | |
| 1885 | pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n", |
| 1886 | __func__, cmd->cmd_op, header->dev_id, header->queue_id, |
| 1887 | header->data[0], header->addr, header->len); |
| 1888 | } |
| 1889 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1890 | static int ublk_ctrl_stop_dev(struct ublk_device *ub) |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1891 | { |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1892 | ublk_stop_dev(ub); |
| 1893 | cancel_work_sync(&ub->stop_work); |
ZiyangZhang | bbae8d1 | 2022-09-23 23:39:16 +0800 | [diff] [blame] | 1894 | cancel_work_sync(&ub->quiesce_work); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 1895 | |
| 1896 | return 0; |
| 1897 | } |
| 1898 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1899 | static int ublk_ctrl_get_dev_info(struct ublk_device *ub, |
| 1900 | struct io_uring_cmd *cmd) |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1901 | { |
| 1902 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 1903 | void __user *argp = (void __user *)(unsigned long)header->addr; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1904 | |
| 1905 | if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr) |
| 1906 | return -EINVAL; |
| 1907 | |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1908 | if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info))) |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1909 | return -EFAULT; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1910 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1911 | return 0; |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 1912 | } |
| 1913 | |
Ming Lei | abb864d | 2023-01-06 12:17:09 +0800 | [diff] [blame] | 1914 | /* TYPE_DEVT is readonly, so fill it up before returning to userspace */ |
| 1915 | static void ublk_ctrl_fill_params_devt(struct ublk_device *ub) |
| 1916 | { |
| 1917 | ub->params.devt.char_major = MAJOR(ub->cdev_dev.devt); |
| 1918 | ub->params.devt.char_minor = MINOR(ub->cdev_dev.devt); |
| 1919 | |
| 1920 | if (ub->ub_disk) { |
| 1921 | ub->params.devt.disk_major = MAJOR(disk_devt(ub->ub_disk)); |
| 1922 | ub->params.devt.disk_minor = MINOR(disk_devt(ub->ub_disk)); |
| 1923 | } else { |
| 1924 | ub->params.devt.disk_major = 0; |
| 1925 | ub->params.devt.disk_minor = 0; |
| 1926 | } |
| 1927 | ub->params.types |= UBLK_PARAM_TYPE_DEVT; |
| 1928 | } |
| 1929 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1930 | static int ublk_ctrl_get_params(struct ublk_device *ub, |
| 1931 | struct io_uring_cmd *cmd) |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1932 | { |
| 1933 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 1934 | void __user *argp = (void __user *)(unsigned long)header->addr; |
| 1935 | struct ublk_params_header ph; |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1936 | int ret; |
| 1937 | |
| 1938 | if (header->len <= sizeof(ph) || !header->addr) |
| 1939 | return -EINVAL; |
| 1940 | |
| 1941 | if (copy_from_user(&ph, argp, sizeof(ph))) |
| 1942 | return -EFAULT; |
| 1943 | |
| 1944 | if (ph.len > header->len || !ph.len) |
| 1945 | return -EINVAL; |
| 1946 | |
| 1947 | if (ph.len > sizeof(struct ublk_params)) |
| 1948 | ph.len = sizeof(struct ublk_params); |
| 1949 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1950 | mutex_lock(&ub->mutex); |
Ming Lei | abb864d | 2023-01-06 12:17:09 +0800 | [diff] [blame] | 1951 | ublk_ctrl_fill_params_devt(ub); |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1952 | if (copy_to_user(argp, &ub->params, ph.len)) |
| 1953 | ret = -EFAULT; |
| 1954 | else |
| 1955 | ret = 0; |
| 1956 | mutex_unlock(&ub->mutex); |
| 1957 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1958 | return ret; |
| 1959 | } |
| 1960 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 1961 | static int ublk_ctrl_set_params(struct ublk_device *ub, |
| 1962 | struct io_uring_cmd *cmd) |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1963 | { |
| 1964 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 1965 | void __user *argp = (void __user *)(unsigned long)header->addr; |
| 1966 | struct ublk_params_header ph; |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1967 | int ret = -EFAULT; |
| 1968 | |
| 1969 | if (header->len <= sizeof(ph) || !header->addr) |
| 1970 | return -EINVAL; |
| 1971 | |
| 1972 | if (copy_from_user(&ph, argp, sizeof(ph))) |
| 1973 | return -EFAULT; |
| 1974 | |
| 1975 | if (ph.len > header->len || !ph.len || !ph.types) |
| 1976 | return -EINVAL; |
| 1977 | |
| 1978 | if (ph.len > sizeof(struct ublk_params)) |
| 1979 | ph.len = sizeof(struct ublk_params); |
| 1980 | |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1981 | /* parameters can only be changed when device isn't live */ |
| 1982 | mutex_lock(&ub->mutex); |
| 1983 | if (ub->dev_info.state == UBLK_S_DEV_LIVE) { |
| 1984 | ret = -EACCES; |
| 1985 | } else if (copy_from_user(&ub->params, argp, ph.len)) { |
| 1986 | ret = -EFAULT; |
| 1987 | } else { |
| 1988 | /* clear all we don't support yet */ |
| 1989 | ub->params.types &= UBLK_PARAM_TYPE_ALL; |
| 1990 | ret = ublk_validate_params(ub); |
Ming Lei | 1d16652 | 2023-04-06 20:40:59 +0800 | [diff] [blame] | 1991 | if (ret) |
| 1992 | ub->params.types = 0; |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1993 | } |
| 1994 | mutex_unlock(&ub->mutex); |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 1995 | |
| 1996 | return ret; |
| 1997 | } |
| 1998 | |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 1999 | static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq) |
| 2000 | { |
| 2001 | int i; |
| 2002 | |
| 2003 | WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq))); |
| 2004 | /* All old ioucmds have to be completed */ |
| 2005 | WARN_ON_ONCE(ubq->nr_io_ready); |
| 2006 | /* old daemon is PF_EXITING, put it now */ |
| 2007 | put_task_struct(ubq->ubq_daemon); |
| 2008 | /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */ |
| 2009 | ubq->ubq_daemon = NULL; |
| 2010 | |
| 2011 | for (i = 0; i < ubq->q_depth; i++) { |
| 2012 | struct ublk_io *io = &ubq->ios[i]; |
| 2013 | |
| 2014 | /* forget everything now and be ready for new FETCH_REQ */ |
| 2015 | io->flags = 0; |
| 2016 | io->cmd = NULL; |
| 2017 | io->addr = 0; |
| 2018 | } |
| 2019 | } |
| 2020 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2021 | static int ublk_ctrl_start_recovery(struct ublk_device *ub, |
| 2022 | struct io_uring_cmd *cmd) |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2023 | { |
| 2024 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2025 | int ret = -EINVAL; |
| 2026 | int i; |
| 2027 | |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2028 | mutex_lock(&ub->mutex); |
| 2029 | if (!ublk_can_use_recovery(ub)) |
| 2030 | goto out_unlock; |
| 2031 | /* |
| 2032 | * START_RECOVERY is only allowd after: |
| 2033 | * |
| 2034 | * (1) UB_STATE_OPEN is not set, which means the dying process is exited |
| 2035 | * and related io_uring ctx is freed so file struct of /dev/ublkcX is |
| 2036 | * released. |
| 2037 | * |
| 2038 | * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work: |
| 2039 | * (a)has quiesced request queue |
| 2040 | * (b)has requeued every inflight rqs whose io_flags is ACTIVE |
| 2041 | * (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE |
| 2042 | * (d)has completed/camceled all ioucmds owned by ther dying process |
| 2043 | */ |
| 2044 | if (test_bit(UB_STATE_OPEN, &ub->state) || |
| 2045 | ub->dev_info.state != UBLK_S_DEV_QUIESCED) { |
| 2046 | ret = -EBUSY; |
| 2047 | goto out_unlock; |
| 2048 | } |
| 2049 | pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id); |
| 2050 | for (i = 0; i < ub->dev_info.nr_hw_queues; i++) |
| 2051 | ublk_queue_reinit(ub, ublk_get_queue(ub, i)); |
| 2052 | /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */ |
| 2053 | ub->mm = NULL; |
| 2054 | ub->nr_queues_ready = 0; |
Ming Lei | 73a166d | 2023-01-06 12:17:07 +0800 | [diff] [blame] | 2055 | ub->nr_privileged_daemon = 0; |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2056 | init_completion(&ub->completion); |
| 2057 | ret = 0; |
| 2058 | out_unlock: |
| 2059 | mutex_unlock(&ub->mutex); |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2060 | return ret; |
| 2061 | } |
| 2062 | |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2063 | static int ublk_ctrl_end_recovery(struct ublk_device *ub, |
| 2064 | struct io_uring_cmd *cmd) |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2065 | { |
| 2066 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 2067 | int ublksrv_pid = (int)header->data[0]; |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2068 | int ret = -EINVAL; |
| 2069 | |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2070 | pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n", |
| 2071 | __func__, ub->dev_info.nr_hw_queues, header->dev_id); |
| 2072 | /* wait until new ubq_daemon sending all FETCH_REQ */ |
| 2073 | wait_for_completion_interruptible(&ub->completion); |
| 2074 | pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n", |
| 2075 | __func__, ub->dev_info.nr_hw_queues, header->dev_id); |
| 2076 | |
| 2077 | mutex_lock(&ub->mutex); |
| 2078 | if (!ublk_can_use_recovery(ub)) |
| 2079 | goto out_unlock; |
| 2080 | |
| 2081 | if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) { |
| 2082 | ret = -EBUSY; |
| 2083 | goto out_unlock; |
| 2084 | } |
| 2085 | ub->dev_info.ublksrv_pid = ublksrv_pid; |
| 2086 | pr_devel("%s: new ublksrv_pid %d, dev id %d\n", |
| 2087 | __func__, ublksrv_pid, header->dev_id); |
| 2088 | blk_mq_unquiesce_queue(ub->ub_disk->queue); |
| 2089 | pr_devel("%s: queue unquiesced, dev id %d.\n", |
| 2090 | __func__, header->dev_id); |
| 2091 | blk_mq_kick_requeue_list(ub->ub_disk->queue); |
| 2092 | ub->dev_info.state = UBLK_S_DEV_LIVE; |
| 2093 | schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD); |
| 2094 | ret = 0; |
| 2095 | out_unlock: |
| 2096 | mutex_unlock(&ub->mutex); |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2097 | return ret; |
| 2098 | } |
| 2099 | |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2100 | /* |
| 2101 | * All control commands are sent via /dev/ublk-control, so we have to check |
| 2102 | * the destination device's permission |
| 2103 | */ |
| 2104 | static int ublk_char_dev_permission(struct ublk_device *ub, |
| 2105 | const char *dev_path, int mask) |
| 2106 | { |
| 2107 | int err; |
| 2108 | struct path path; |
| 2109 | struct kstat stat; |
| 2110 | |
| 2111 | err = kern_path(dev_path, LOOKUP_FOLLOW, &path); |
| 2112 | if (err) |
| 2113 | return err; |
| 2114 | |
| 2115 | err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT); |
| 2116 | if (err) |
| 2117 | goto exit; |
| 2118 | |
| 2119 | err = -EPERM; |
| 2120 | if (stat.rdev != ub->cdev_dev.devt || !S_ISCHR(stat.mode)) |
| 2121 | goto exit; |
| 2122 | |
Linus Torvalds | 5b0ed59 | 2023-02-20 14:27:21 -0800 | [diff] [blame] | 2123 | err = inode_permission(&nop_mnt_idmap, |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2124 | d_backing_inode(path.dentry), mask); |
| 2125 | exit: |
| 2126 | path_put(&path); |
| 2127 | return err; |
| 2128 | } |
| 2129 | |
| 2130 | static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub, |
| 2131 | struct io_uring_cmd *cmd) |
| 2132 | { |
| 2133 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
| 2134 | bool unprivileged = ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV; |
| 2135 | void __user *argp = (void __user *)(unsigned long)header->addr; |
| 2136 | char *dev_path = NULL; |
| 2137 | int ret = 0; |
| 2138 | int mask; |
| 2139 | |
| 2140 | if (!unprivileged) { |
| 2141 | if (!capable(CAP_SYS_ADMIN)) |
| 2142 | return -EPERM; |
| 2143 | /* |
| 2144 | * The new added command of UBLK_CMD_GET_DEV_INFO2 includes |
| 2145 | * char_dev_path in payload too, since userspace may not |
| 2146 | * know if the specified device is created as unprivileged |
| 2147 | * mode. |
| 2148 | */ |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 2149 | if (_IOC_NR(cmd->cmd_op) != UBLK_CMD_GET_DEV_INFO2) |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * User has to provide the char device path for unprivileged ublk |
| 2155 | * |
| 2156 | * header->addr always points to the dev path buffer, and |
| 2157 | * header->dev_path_len records length of dev path buffer. |
| 2158 | */ |
| 2159 | if (!header->dev_path_len || header->dev_path_len > PATH_MAX) |
| 2160 | return -EINVAL; |
| 2161 | |
| 2162 | if (header->len < header->dev_path_len) |
| 2163 | return -EINVAL; |
| 2164 | |
| 2165 | dev_path = kmalloc(header->dev_path_len + 1, GFP_KERNEL); |
| 2166 | if (!dev_path) |
| 2167 | return -ENOMEM; |
| 2168 | |
| 2169 | ret = -EFAULT; |
| 2170 | if (copy_from_user(dev_path, argp, header->dev_path_len)) |
| 2171 | goto exit; |
| 2172 | dev_path[header->dev_path_len] = 0; |
| 2173 | |
| 2174 | ret = -EINVAL; |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 2175 | switch (_IOC_NR(cmd->cmd_op)) { |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2176 | case UBLK_CMD_GET_DEV_INFO: |
| 2177 | case UBLK_CMD_GET_DEV_INFO2: |
| 2178 | case UBLK_CMD_GET_QUEUE_AFFINITY: |
| 2179 | case UBLK_CMD_GET_PARAMS: |
| 2180 | mask = MAY_READ; |
| 2181 | break; |
| 2182 | case UBLK_CMD_START_DEV: |
| 2183 | case UBLK_CMD_STOP_DEV: |
| 2184 | case UBLK_CMD_ADD_DEV: |
| 2185 | case UBLK_CMD_DEL_DEV: |
| 2186 | case UBLK_CMD_SET_PARAMS: |
| 2187 | case UBLK_CMD_START_USER_RECOVERY: |
| 2188 | case UBLK_CMD_END_USER_RECOVERY: |
| 2189 | mask = MAY_READ | MAY_WRITE; |
| 2190 | break; |
| 2191 | default: |
| 2192 | goto exit; |
| 2193 | } |
| 2194 | |
| 2195 | ret = ublk_char_dev_permission(ub, dev_path, mask); |
| 2196 | if (!ret) { |
| 2197 | header->len -= header->dev_path_len; |
| 2198 | header->addr += header->dev_path_len; |
| 2199 | } |
| 2200 | pr_devel("%s: dev id %d cmd_op %x uid %d gid %d path %s ret %d\n", |
| 2201 | __func__, ub->ub_number, cmd->cmd_op, |
| 2202 | ub->dev_info.owner_uid, ub->dev_info.owner_gid, |
| 2203 | dev_path, ret); |
| 2204 | exit: |
| 2205 | kfree(dev_path); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2206 | return ret; |
| 2207 | } |
| 2208 | |
| 2209 | static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd, |
| 2210 | unsigned int issue_flags) |
| 2211 | { |
| 2212 | struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2213 | struct ublk_device *ub = NULL; |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 2214 | u32 cmd_op = cmd->cmd_op; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2215 | int ret = -EINVAL; |
| 2216 | |
Ming Lei | fa8e442e | 2023-01-04 21:32:35 +0800 | [diff] [blame] | 2217 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 2218 | return -EAGAIN; |
| 2219 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2220 | ublk_ctrl_cmd_dump(cmd); |
| 2221 | |
| 2222 | if (!(issue_flags & IO_URING_F_SQE128)) |
| 2223 | goto out; |
| 2224 | |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 2225 | ret = ublk_check_cmd_op(cmd_op); |
| 2226 | if (ret) |
| 2227 | goto out; |
| 2228 | |
| 2229 | if (_IOC_NR(cmd_op) != UBLK_CMD_ADD_DEV) { |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2230 | ret = -ENODEV; |
| 2231 | ub = ublk_get_device_from_id(header->dev_id); |
| 2232 | if (!ub) |
| 2233 | goto out; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2234 | |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2235 | ret = ublk_ctrl_uring_cmd_permission(ub, cmd); |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 2236 | if (ret) |
| 2237 | goto put_dev; |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2238 | } |
| 2239 | |
Ming Lei | 2d786e6 | 2023-04-18 21:18:10 +0800 | [diff] [blame] | 2240 | switch (_IOC_NR(cmd_op)) { |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2241 | case UBLK_CMD_START_DEV: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2242 | ret = ublk_ctrl_start_dev(ub, cmd); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2243 | break; |
| 2244 | case UBLK_CMD_STOP_DEV: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2245 | ret = ublk_ctrl_stop_dev(ub); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2246 | break; |
| 2247 | case UBLK_CMD_GET_DEV_INFO: |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2248 | case UBLK_CMD_GET_DEV_INFO2: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2249 | ret = ublk_ctrl_get_dev_info(ub, cmd); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2250 | break; |
| 2251 | case UBLK_CMD_ADD_DEV: |
Christoph Hellwig | 34d8f2b | 2022-07-21 15:09:13 +0200 | [diff] [blame] | 2252 | ret = ublk_ctrl_add_dev(cmd); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2253 | break; |
| 2254 | case UBLK_CMD_DEL_DEV: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2255 | ret = ublk_ctrl_del_dev(&ub); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2256 | break; |
| 2257 | case UBLK_CMD_GET_QUEUE_AFFINITY: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2258 | ret = ublk_ctrl_get_queue_affinity(ub, cmd); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2259 | break; |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 2260 | case UBLK_CMD_GET_PARAMS: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2261 | ret = ublk_ctrl_get_params(ub, cmd); |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 2262 | break; |
| 2263 | case UBLK_CMD_SET_PARAMS: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2264 | ret = ublk_ctrl_set_params(ub, cmd); |
Ming Lei | 0aa7317 | 2022-07-30 17:27:49 +0800 | [diff] [blame] | 2265 | break; |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2266 | case UBLK_CMD_START_USER_RECOVERY: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2267 | ret = ublk_ctrl_start_recovery(ub, cmd); |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2268 | break; |
| 2269 | case UBLK_CMD_END_USER_RECOVERY: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2270 | ret = ublk_ctrl_end_recovery(ub, cmd); |
ZiyangZhang | c732a85 | 2022-09-23 23:39:18 +0800 | [diff] [blame] | 2271 | break; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2272 | default: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2273 | ret = -ENOTSUPP; |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2274 | break; |
Yang Li | 6b1439d | 2022-07-18 09:54:31 +0800 | [diff] [blame] | 2275 | } |
Ming Lei | 4093cb5 | 2023-01-06 12:17:11 +0800 | [diff] [blame] | 2276 | |
| 2277 | put_dev: |
Ming Lei | bfbcef0 | 2023-01-06 12:17:08 +0800 | [diff] [blame] | 2278 | if (ub) |
| 2279 | ublk_put_device(ub); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2280 | out: |
Jens Axboe | 9d2789a | 2023-03-20 20:01:25 -0600 | [diff] [blame] | 2281 | io_uring_cmd_done(cmd, ret, 0, issue_flags); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2282 | pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n", |
| 2283 | __func__, ret, cmd->cmd_op, header->dev_id, header->queue_id); |
| 2284 | return -EIOCBQUEUED; |
| 2285 | } |
| 2286 | |
| 2287 | static const struct file_operations ublk_ctl_fops = { |
| 2288 | .open = nonseekable_open, |
| 2289 | .uring_cmd = ublk_ctrl_uring_cmd, |
| 2290 | .owner = THIS_MODULE, |
| 2291 | .llseek = noop_llseek, |
| 2292 | }; |
| 2293 | |
| 2294 | static struct miscdevice ublk_misc = { |
| 2295 | .minor = MISC_DYNAMIC_MINOR, |
| 2296 | .name = "ublk-control", |
| 2297 | .fops = &ublk_ctl_fops, |
| 2298 | }; |
| 2299 | |
| 2300 | static int __init ublk_init(void) |
| 2301 | { |
| 2302 | int ret; |
| 2303 | |
| 2304 | init_waitqueue_head(&ublk_idr_wq); |
| 2305 | |
| 2306 | ret = misc_register(&ublk_misc); |
| 2307 | if (ret) |
| 2308 | return ret; |
| 2309 | |
| 2310 | ret = alloc_chrdev_region(&ublk_chr_devt, 0, UBLK_MINORS, "ublk-char"); |
| 2311 | if (ret) |
| 2312 | goto unregister_mis; |
| 2313 | |
Greg Kroah-Hartman | 1aaba11 | 2023-03-13 19:18:35 +0100 | [diff] [blame] | 2314 | ublk_chr_class = class_create("ublk-char"); |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2315 | if (IS_ERR(ublk_chr_class)) { |
| 2316 | ret = PTR_ERR(ublk_chr_class); |
| 2317 | goto free_chrdev_region; |
| 2318 | } |
| 2319 | return 0; |
| 2320 | |
| 2321 | free_chrdev_region: |
| 2322 | unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS); |
| 2323 | unregister_mis: |
| 2324 | misc_deregister(&ublk_misc); |
| 2325 | return ret; |
| 2326 | } |
| 2327 | |
| 2328 | static void __exit ublk_exit(void) |
| 2329 | { |
| 2330 | struct ublk_device *ub; |
| 2331 | int id; |
| 2332 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2333 | idr_for_each_entry(&ublk_index_idr, ub, id) |
| 2334 | ublk_remove(ub); |
| 2335 | |
Ming Lei | 8e4ff68 | 2023-01-26 19:53:46 +0800 | [diff] [blame] | 2336 | class_destroy(ublk_chr_class); |
| 2337 | misc_deregister(&ublk_misc); |
| 2338 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2339 | idr_destroy(&ublk_index_idr); |
| 2340 | unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS); |
| 2341 | } |
| 2342 | |
| 2343 | module_init(ublk_init); |
| 2344 | module_exit(ublk_exit); |
| 2345 | |
Ming Lei | 403ebc8 | 2023-01-06 12:17:10 +0800 | [diff] [blame] | 2346 | module_param(ublks_max, int, 0444); |
| 2347 | MODULE_PARM_DESC(ublks_max, "max number of ublk devices allowed to add(default: 64)"); |
| 2348 | |
Ming Lei | 71f28f3 | 2022-07-13 22:07:10 +0800 | [diff] [blame] | 2349 | MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>"); |
| 2350 | MODULE_LICENSE("GPL"); |