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