1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Userspace block device - block device which IO is handled from userspace
5 * Take full use of io_uring passthrough command for communicating with
6 * ublk userspace daemon(ublksrvd) for handling basic IO request.
8 * Copyright 2022 Ming Lei <ming.lei@redhat.com>
10 * (part of code stolen from loop.c)
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/sched.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>
44 #include <linux/task_work.h>
45 #include <uapi/linux/ublk_cmd.h>
47 #define UBLK_MINORS (1U << MINORBITS)
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)
56 /* All UBLK_PARAM_TYPE_* should be included here */
57 #define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD)
60 struct callback_head work;
63 struct ublk_uring_cmd_pdu {
68 * io command is active: sqe cmd is received, and its cqe isn't done
70 * If the flag is set, the io command is owned by ublk driver, and waited
71 * for incoming blk-mq request from the ublk block device.
73 * If the flag is cleared, the io command will be completed, and owned by
76 #define UBLK_IO_FLAG_ACTIVE 0x01
79 * IO command is completed via cqe, and it is being handled by ublksrv, and
82 * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for
85 #define UBLK_IO_FLAG_OWNED_BY_SRV 0x02
88 * IO command is aborted, so this flag is set in case of
89 * !UBLK_IO_FLAG_ACTIVE.
91 * After this flag is observed, any pending or new incoming request
92 * associated with this io command will be failed immediately
94 #define UBLK_IO_FLAG_ABORTED 0x04
97 * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires
98 * get data buffer address from ublksrv.
100 * Then, bio data could be copied into this data buffer for a WRITE request
101 * after the IO command is issued again and UBLK_IO_FLAG_NEED_GET_DATA is unset.
103 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
106 /* userspace buffer address from io cmd */
111 struct io_uring_cmd *cmd;
119 struct task_struct *ubq_daemon;
122 unsigned long io_addr; /* mapped vm address */
123 unsigned int max_io_sz;
125 unsigned short nr_io_ready; /* how many ios setup */
126 struct ublk_device *dev;
127 struct ublk_io ios[0];
130 #define UBLK_DAEMON_MONITOR_PERIOD (5 * HZ)
133 struct gendisk *ub_disk;
137 unsigned short queue_size;
138 struct ublksrv_ctrl_dev_info dev_info;
140 struct blk_mq_tag_set tag_set;
143 struct device cdev_dev;
145 #define UB_STATE_OPEN 0
146 #define UB_STATE_USED 1
153 struct mm_struct *mm;
155 struct ublk_params params;
157 struct completion completion;
158 unsigned int nr_queues_ready;
159 atomic_t nr_aborted_queues;
162 * Our ubq->daemon may be killed without any notification, so
163 * monitor each queue's daemon periodically
165 struct delayed_work monitor_work;
166 struct work_struct quiesce_work;
167 struct work_struct stop_work;
170 /* header of ublk_params */
171 struct ublk_params_header {
176 static dev_t ublk_chr_devt;
177 static struct class *ublk_chr_class;
179 static DEFINE_IDR(ublk_index_idr);
180 static DEFINE_SPINLOCK(ublk_idr_lock);
181 static wait_queue_head_t ublk_idr_wq; /* wait until one idr is freed */
183 static DEFINE_MUTEX(ublk_ctl_mutex);
185 static struct miscdevice ublk_misc;
187 static void ublk_dev_param_basic_apply(struct ublk_device *ub)
189 struct request_queue *q = ub->ub_disk->queue;
190 const struct ublk_param_basic *p = &ub->params.basic;
192 blk_queue_logical_block_size(q, 1 << p->logical_bs_shift);
193 blk_queue_physical_block_size(q, 1 << p->physical_bs_shift);
194 blk_queue_io_min(q, 1 << p->io_min_shift);
195 blk_queue_io_opt(q, 1 << p->io_opt_shift);
197 blk_queue_write_cache(q, p->attrs & UBLK_ATTR_VOLATILE_CACHE,
198 p->attrs & UBLK_ATTR_FUA);
199 if (p->attrs & UBLK_ATTR_ROTATIONAL)
200 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
202 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
204 blk_queue_max_hw_sectors(q, p->max_sectors);
205 blk_queue_chunk_sectors(q, p->chunk_sectors);
206 blk_queue_virt_boundary(q, p->virt_boundary_mask);
208 if (p->attrs & UBLK_ATTR_READ_ONLY)
209 set_disk_ro(ub->ub_disk, true);
211 set_capacity(ub->ub_disk, p->dev_sectors);
214 static void ublk_dev_param_discard_apply(struct ublk_device *ub)
216 struct request_queue *q = ub->ub_disk->queue;
217 const struct ublk_param_discard *p = &ub->params.discard;
219 q->limits.discard_alignment = p->discard_alignment;
220 q->limits.discard_granularity = p->discard_granularity;
221 blk_queue_max_discard_sectors(q, p->max_discard_sectors);
222 blk_queue_max_write_zeroes_sectors(q,
223 p->max_write_zeroes_sectors);
224 blk_queue_max_discard_segments(q, p->max_discard_segments);
227 static int ublk_validate_params(const struct ublk_device *ub)
229 /* basic param is the only one which must be set */
230 if (ub->params.types & UBLK_PARAM_TYPE_BASIC) {
231 const struct ublk_param_basic *p = &ub->params.basic;
233 if (p->logical_bs_shift > PAGE_SHIFT)
236 if (p->logical_bs_shift > p->physical_bs_shift)
239 if (p->max_sectors > (ub->dev_info.max_io_buf_bytes >> 9))
244 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) {
245 const struct ublk_param_discard *p = &ub->params.discard;
247 /* So far, only support single segment discard */
248 if (p->max_discard_sectors && p->max_discard_segments != 1)
251 if (!p->discard_granularity)
258 static int ublk_apply_params(struct ublk_device *ub)
260 if (!(ub->params.types & UBLK_PARAM_TYPE_BASIC))
263 ublk_dev_param_basic_apply(ub);
265 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD)
266 ublk_dev_param_discard_apply(ub);
271 static inline bool ublk_can_use_task_work(const struct ublk_queue *ubq)
273 if (IS_BUILTIN(CONFIG_BLK_DEV_UBLK) &&
274 !(ubq->flags & UBLK_F_URING_CMD_COMP_IN_TASK))
279 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
281 if (ubq->flags & UBLK_F_NEED_GET_DATA)
286 static struct ublk_device *ublk_get_device(struct ublk_device *ub)
288 if (kobject_get_unless_zero(&ub->cdev_dev.kobj))
293 static void ublk_put_device(struct ublk_device *ub)
295 put_device(&ub->cdev_dev);
298 static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev,
301 return (struct ublk_queue *)&(dev->__queues[qid * dev->queue_size]);
304 static inline bool ublk_rq_has_data(const struct request *rq)
306 return rq->bio && bio_has_data(rq->bio);
309 static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
312 return (struct ublksrv_io_desc *)
313 &(ubq->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]);
316 static inline char *ublk_queue_cmd_buf(struct ublk_device *ub, int q_id)
318 return ublk_get_queue(ub, q_id)->io_cmd_buf;
321 static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id)
323 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
325 return round_up(ubq->q_depth * sizeof(struct ublksrv_io_desc),
329 static inline bool ublk_queue_can_use_recovery_reissue(
330 struct ublk_queue *ubq)
332 if ((ubq->flags & UBLK_F_USER_RECOVERY) &&
333 (ubq->flags & UBLK_F_USER_RECOVERY_REISSUE))
338 static inline bool ublk_queue_can_use_recovery(
339 struct ublk_queue *ubq)
341 if (ubq->flags & UBLK_F_USER_RECOVERY)
346 static inline bool ublk_can_use_recovery(struct ublk_device *ub)
348 if (ub->dev_info.flags & UBLK_F_USER_RECOVERY)
353 static void ublk_free_disk(struct gendisk *disk)
355 struct ublk_device *ub = disk->private_data;
357 clear_bit(UB_STATE_USED, &ub->state);
358 put_device(&ub->cdev_dev);
361 static const struct block_device_operations ub_fops = {
362 .owner = THIS_MODULE,
363 .free_disk = ublk_free_disk,
366 #define UBLK_MAX_PIN_PAGES 32
368 struct ublk_map_data {
369 const struct ublk_queue *ubq;
370 const struct request *rq;
371 const struct ublk_io *io;
375 struct ublk_io_iter {
376 struct page *pages[UBLK_MAX_PIN_PAGES];
377 unsigned pg_off; /* offset in the 1st page in pages */
378 int nr_pages; /* how many page pointers in pages */
380 struct bvec_iter iter;
383 static inline unsigned ublk_copy_io_pages(struct ublk_io_iter *data,
384 unsigned max_bytes, bool to_vm)
386 const unsigned total = min_t(unsigned, max_bytes,
387 PAGE_SIZE - data->pg_off +
388 ((data->nr_pages - 1) << PAGE_SHIFT));
392 while (done < total) {
393 struct bio_vec bv = bio_iter_iovec(data->bio, data->iter);
394 const unsigned int bytes = min3(bv.bv_len, total - done,
395 (unsigned)(PAGE_SIZE - data->pg_off));
396 void *bv_buf = bvec_kmap_local(&bv);
397 void *pg_buf = kmap_local_page(data->pages[pg_idx]);
400 memcpy(pg_buf + data->pg_off, bv_buf, bytes);
402 memcpy(bv_buf, pg_buf + data->pg_off, bytes);
404 kunmap_local(pg_buf);
405 kunmap_local(bv_buf);
407 /* advance page array */
408 data->pg_off += bytes;
409 if (data->pg_off == PAGE_SIZE) {
417 bio_advance_iter_single(data->bio, &data->iter, bytes);
418 if (!data->iter.bi_size) {
419 data->bio = data->bio->bi_next;
420 if (data->bio == NULL)
422 data->iter = data->bio->bi_iter;
429 static inline int ublk_copy_user_pages(struct ublk_map_data *data,
432 const unsigned int gup_flags = to_vm ? FOLL_WRITE : 0;
433 const unsigned long start_vm = data->io->addr;
434 unsigned int done = 0;
435 struct ublk_io_iter iter = {
436 .pg_off = start_vm & (PAGE_SIZE - 1),
437 .bio = data->rq->bio,
438 .iter = data->rq->bio->bi_iter,
440 const unsigned int nr_pages = round_up(data->max_bytes +
441 (start_vm & (PAGE_SIZE - 1)), PAGE_SIZE) >> PAGE_SHIFT;
443 while (done < nr_pages) {
444 const unsigned to_pin = min_t(unsigned, UBLK_MAX_PIN_PAGES,
448 iter.nr_pages = get_user_pages_fast(start_vm +
449 (done << PAGE_SHIFT), to_pin, gup_flags,
451 if (iter.nr_pages <= 0)
452 return done == 0 ? iter.nr_pages : done;
453 len = ublk_copy_io_pages(&iter, data->max_bytes, to_vm);
454 for (i = 0; i < iter.nr_pages; i++) {
456 set_page_dirty(iter.pages[i]);
457 put_page(iter.pages[i]);
459 data->max_bytes -= len;
460 done += iter.nr_pages;
466 static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req,
469 const unsigned int rq_bytes = blk_rq_bytes(req);
471 * no zero copy, we delay copy WRITE request data into ublksrv
472 * context and the big benefit is that pinning pages in current
473 * context is pretty fast, see ublk_pin_user_pages
475 if (req_op(req) != REQ_OP_WRITE && req_op(req) != REQ_OP_FLUSH)
478 if (ublk_rq_has_data(req)) {
479 struct ublk_map_data data = {
483 .max_bytes = rq_bytes,
486 ublk_copy_user_pages(&data, true);
488 return rq_bytes - data.max_bytes;
493 static int ublk_unmap_io(const struct ublk_queue *ubq,
494 const struct request *req,
497 const unsigned int rq_bytes = blk_rq_bytes(req);
499 if (req_op(req) == REQ_OP_READ && ublk_rq_has_data(req)) {
500 struct ublk_map_data data = {
504 .max_bytes = io->res,
507 WARN_ON_ONCE(io->res > rq_bytes);
509 ublk_copy_user_pages(&data, false);
511 return io->res - data.max_bytes;
516 static inline unsigned int ublk_req_build_flags(struct request *req)
520 if (req->cmd_flags & REQ_FAILFAST_DEV)
521 flags |= UBLK_IO_F_FAILFAST_DEV;
523 if (req->cmd_flags & REQ_FAILFAST_TRANSPORT)
524 flags |= UBLK_IO_F_FAILFAST_TRANSPORT;
526 if (req->cmd_flags & REQ_FAILFAST_DRIVER)
527 flags |= UBLK_IO_F_FAILFAST_DRIVER;
529 if (req->cmd_flags & REQ_META)
530 flags |= UBLK_IO_F_META;
532 if (req->cmd_flags & REQ_FUA)
533 flags |= UBLK_IO_F_FUA;
535 if (req->cmd_flags & REQ_NOUNMAP)
536 flags |= UBLK_IO_F_NOUNMAP;
538 if (req->cmd_flags & REQ_SWAP)
539 flags |= UBLK_IO_F_SWAP;
544 static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
546 struct ublksrv_io_desc *iod = ublk_get_iod(ubq, req->tag);
547 struct ublk_io *io = &ubq->ios[req->tag];
550 switch (req_op(req)) {
552 ublk_op = UBLK_IO_OP_READ;
555 ublk_op = UBLK_IO_OP_WRITE;
558 ublk_op = UBLK_IO_OP_FLUSH;
561 ublk_op = UBLK_IO_OP_DISCARD;
563 case REQ_OP_WRITE_ZEROES:
564 ublk_op = UBLK_IO_OP_WRITE_ZEROES;
567 return BLK_STS_IOERR;
570 /* need to translate since kernel may change */
571 iod->op_flags = ublk_op | ublk_req_build_flags(req);
572 iod->nr_sectors = blk_rq_sectors(req);
573 iod->start_sector = blk_rq_pos(req);
574 iod->addr = io->addr;
579 static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu(
580 struct io_uring_cmd *ioucmd)
582 return (struct ublk_uring_cmd_pdu *)&ioucmd->pdu;
585 static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq)
587 return ubq->ubq_daemon->flags & PF_EXITING;
590 /* todo: handle partial completion */
591 static void ublk_complete_rq(struct request *req)
593 struct ublk_queue *ubq = req->mq_hctx->driver_data;
594 struct ublk_io *io = &ubq->ios[req->tag];
595 unsigned int unmapped_bytes;
597 /* failed read IO if nothing is read */
598 if (!io->res && req_op(req) == REQ_OP_READ)
602 blk_mq_end_request(req, errno_to_blk_status(io->res));
607 * FLUSH or DISCARD usually won't return bytes returned, so end them
610 * Both the two needn't unmap.
612 if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE) {
613 blk_mq_end_request(req, BLK_STS_OK);
617 /* for READ request, writing data in iod->addr to rq buffers */
618 unmapped_bytes = ublk_unmap_io(ubq, req, io);
621 * Extremely impossible since we got data filled in just before
623 * Re-read simply for this unlikely case.
625 if (unlikely(unmapped_bytes < io->res))
626 io->res = unmapped_bytes;
628 if (blk_update_request(req, BLK_STS_OK, io->res))
629 blk_mq_requeue_request(req, true);
631 __blk_mq_end_request(req, BLK_STS_OK);
635 * Since __ublk_rq_task_work always fails requests immediately during
636 * exiting, __ublk_fail_req() is only called from abort context during
637 * exiting. So lock is unnecessary.
639 * Also aborting may not be started yet, keep in mind that one failed
640 * request may be issued by block layer again.
642 static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io,
645 WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
647 if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
648 io->flags |= UBLK_IO_FLAG_ABORTED;
649 if (ublk_queue_can_use_recovery_reissue(ubq))
650 blk_mq_requeue_request(req, false);
652 blk_mq_end_request(req, BLK_STS_IOERR);
656 static void ubq_complete_io_cmd(struct ublk_io *io, int res)
658 /* mark this cmd owned by ublksrv */
659 io->flags |= UBLK_IO_FLAG_OWNED_BY_SRV;
662 * clear ACTIVE since we are done with this sqe/cmd slot
663 * We can only accept io cmd in case of being not active.
665 io->flags &= ~UBLK_IO_FLAG_ACTIVE;
667 /* tell ublksrv one io request is coming */
668 io_uring_cmd_done(io->cmd, res, 0);
671 #define UBLK_REQUEUE_DELAY_MS 3
673 static inline void __ublk_abort_rq(struct ublk_queue *ubq,
676 /* We cannot process this rq so just requeue it. */
677 if (ublk_queue_can_use_recovery(ubq))
678 blk_mq_requeue_request(rq, false);
680 blk_mq_end_request(rq, BLK_STS_IOERR);
682 mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0);
685 static inline void __ublk_rq_task_work(struct request *req)
687 struct ublk_queue *ubq = req->mq_hctx->driver_data;
689 struct ublk_io *io = &ubq->ios[tag];
690 unsigned int mapped_bytes;
692 pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n",
693 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
694 ublk_get_iod(ubq, req->tag)->addr);
697 * Task is exiting if either:
699 * (1) current != ubq_daemon.
700 * io_uring_cmd_complete_in_task() tries to run task_work
701 * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING.
703 * (2) current->flags & PF_EXITING.
705 if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) {
706 __ublk_abort_rq(ubq, req);
710 if (ublk_need_get_data(ubq) &&
711 (req_op(req) == REQ_OP_WRITE ||
712 req_op(req) == REQ_OP_FLUSH)) {
714 * We have not handled UBLK_IO_NEED_GET_DATA command yet,
715 * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv
718 if (!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA)) {
719 io->flags |= UBLK_IO_FLAG_NEED_GET_DATA;
720 pr_devel("%s: need get data. op %d, qid %d tag %d io_flags %x\n",
721 __func__, io->cmd->cmd_op, ubq->q_id,
722 req->tag, io->flags);
723 ubq_complete_io_cmd(io, UBLK_IO_RES_NEED_GET_DATA);
727 * We have handled UBLK_IO_NEED_GET_DATA command,
728 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
731 io->flags &= ~UBLK_IO_FLAG_NEED_GET_DATA;
732 /* update iod->addr because ublksrv may have passed a new io buffer */
733 ublk_get_iod(ubq, req->tag)->addr = io->addr;
734 pr_devel("%s: update iod->addr: op %d, qid %d tag %d io_flags %x addr %llx\n",
735 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
736 ublk_get_iod(ubq, req->tag)->addr);
739 mapped_bytes = ublk_map_io(ubq, req, io);
741 /* partially mapped, update io descriptor */
742 if (unlikely(mapped_bytes != blk_rq_bytes(req))) {
744 * Nothing mapped, retry until we succeed.
746 * We may never succeed in mapping any bytes here because
747 * of OOM. TODO: reserve one buffer with single page pinned
748 * for providing forward progress guarantee.
750 if (unlikely(!mapped_bytes)) {
751 blk_mq_requeue_request(req, false);
752 blk_mq_delay_kick_requeue_list(req->q,
753 UBLK_REQUEUE_DELAY_MS);
757 ublk_get_iod(ubq, req->tag)->nr_sectors =
761 ubq_complete_io_cmd(io, UBLK_IO_RES_OK);
764 static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd)
766 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
768 __ublk_rq_task_work(pdu->req);
771 static void ublk_rq_task_work_fn(struct callback_head *work)
773 struct ublk_rq_data *data = container_of(work,
774 struct ublk_rq_data, work);
775 struct request *req = blk_mq_rq_from_pdu(data);
777 __ublk_rq_task_work(req);
780 static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
781 const struct blk_mq_queue_data *bd)
783 struct ublk_queue *ubq = hctx->driver_data;
784 struct request *rq = bd->rq;
787 /* fill iod to slot in io cmd buffer */
788 res = ublk_setup_iod(ubq, rq);
789 if (unlikely(res != BLK_STS_OK))
790 return BLK_STS_IOERR;
791 /* With recovery feature enabled, force_abort is set in
792 * ublk_stop_dev() before calling del_gendisk(). We have to
793 * abort all requeued and new rqs here to let del_gendisk()
794 * move on. Besides, we cannot not call io_uring_cmd_complete_in_task()
795 * to avoid UAF on io_uring ctx.
797 * Note: force_abort is guaranteed to be seen because it is set
798 * before request queue is unqiuesced.
800 if (ublk_queue_can_use_recovery(ubq) && unlikely(ubq->force_abort))
801 return BLK_STS_IOERR;
803 blk_mq_start_request(bd->rq);
805 if (unlikely(ubq_daemon_is_dying(ubq))) {
807 __ublk_abort_rq(ubq, rq);
811 if (ublk_can_use_task_work(ubq)) {
812 struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq);
813 enum task_work_notify_mode notify_mode = bd->last ?
814 TWA_SIGNAL_NO_IPI : TWA_NONE;
816 if (task_work_add(ubq->ubq_daemon, &data->work, notify_mode))
819 struct ublk_io *io = &ubq->ios[rq->tag];
820 struct io_uring_cmd *cmd = io->cmd;
821 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
824 * If the check pass, we know that this is a re-issued request aborted
825 * previously in monitor_work because the ubq_daemon(cmd's task) is
826 * PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore
827 * because this ioucmd's io_uring context may be freed now if no inflight
828 * ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work.
830 * Note: monitor_work sets UBLK_IO_FLAG_ABORTED and ends this request(releasing
831 * the tag). Then the request is re-started(allocating the tag) and we are here.
832 * Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED
833 * guarantees that here is a re-issued request aborted previously.
835 if ((io->flags & UBLK_IO_FLAG_ABORTED))
839 io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
845 static void ublk_commit_rqs(struct blk_mq_hw_ctx *hctx)
847 struct ublk_queue *ubq = hctx->driver_data;
849 if (ublk_can_use_task_work(ubq))
850 __set_notify_signal(ubq->ubq_daemon);
853 static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
854 unsigned int hctx_idx)
856 struct ublk_device *ub = driver_data;
857 struct ublk_queue *ubq = ublk_get_queue(ub, hctx->queue_num);
859 hctx->driver_data = ubq;
863 static int ublk_init_rq(struct blk_mq_tag_set *set, struct request *req,
864 unsigned int hctx_idx, unsigned int numa_node)
866 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
868 init_task_work(&data->work, ublk_rq_task_work_fn);
872 static const struct blk_mq_ops ublk_mq_ops = {
873 .queue_rq = ublk_queue_rq,
874 .commit_rqs = ublk_commit_rqs,
875 .init_hctx = ublk_init_hctx,
876 .init_request = ublk_init_rq,
879 static int ublk_ch_open(struct inode *inode, struct file *filp)
881 struct ublk_device *ub = container_of(inode->i_cdev,
882 struct ublk_device, cdev);
884 if (test_and_set_bit(UB_STATE_OPEN, &ub->state))
886 filp->private_data = ub;
890 static int ublk_ch_release(struct inode *inode, struct file *filp)
892 struct ublk_device *ub = filp->private_data;
894 clear_bit(UB_STATE_OPEN, &ub->state);
898 /* map pre-allocated per-queue cmd buffer to ublksrv daemon */
899 static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
901 struct ublk_device *ub = filp->private_data;
902 size_t sz = vma->vm_end - vma->vm_start;
903 unsigned max_sz = UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc);
904 unsigned long pfn, end, phys_off = vma->vm_pgoff << PAGE_SHIFT;
907 spin_lock(&ub->mm_lock);
909 ub->mm = current->mm;
910 if (current->mm != ub->mm)
912 spin_unlock(&ub->mm_lock);
917 if (vma->vm_flags & VM_WRITE)
920 end = UBLKSRV_CMD_BUF_OFFSET + ub->dev_info.nr_hw_queues * max_sz;
921 if (phys_off < UBLKSRV_CMD_BUF_OFFSET || phys_off >= end)
924 q_id = (phys_off - UBLKSRV_CMD_BUF_OFFSET) / max_sz;
925 pr_devel("%s: qid %d, pid %d, addr %lx pg_off %lx sz %lu\n",
926 __func__, q_id, current->pid, vma->vm_start,
927 phys_off, (unsigned long)sz);
929 if (sz != ublk_queue_cmd_buf_size(ub, q_id))
932 pfn = virt_to_phys(ublk_queue_cmd_buf(ub, q_id)) >> PAGE_SHIFT;
933 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
936 static void ublk_commit_completion(struct ublk_device *ub,
937 struct ublksrv_io_cmd *ub_cmd)
939 u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
940 struct ublk_queue *ubq = ublk_get_queue(ub, qid);
941 struct ublk_io *io = &ubq->ios[tag];
944 /* now this cmd slot is owned by nbd driver */
945 io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
946 io->res = ub_cmd->result;
948 /* find the io request and complete */
949 req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
951 if (req && likely(!blk_should_fake_timeout(req->q)))
952 ublk_complete_rq(req);
956 * When ->ubq_daemon is exiting, either new request is ended immediately,
957 * or any queued io command is drained, so it is safe to abort queue
960 static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
964 if (!ublk_get_device(ub))
967 for (i = 0; i < ubq->q_depth; i++) {
968 struct ublk_io *io = &ubq->ios[i];
970 if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
974 * Either we fail the request or ublk_rq_task_work_fn
977 rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
979 __ublk_fail_req(ubq, io, rq);
985 static void ublk_daemon_monitor_work(struct work_struct *work)
987 struct ublk_device *ub =
988 container_of(work, struct ublk_device, monitor_work.work);
991 for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
992 struct ublk_queue *ubq = ublk_get_queue(ub, i);
994 if (ubq_daemon_is_dying(ubq)) {
995 if (ublk_queue_can_use_recovery(ubq))
996 schedule_work(&ub->quiesce_work);
998 schedule_work(&ub->stop_work);
1000 /* abort queue is for making forward progress */
1001 ublk_abort_queue(ub, ubq);
1006 * We can't schedule monitor work after ub's state is not UBLK_S_DEV_LIVE.
1007 * after ublk_remove() or __ublk_quiesce_dev() is started.
1009 * No need ub->mutex, monitor work are canceled after state is marked
1010 * as not LIVE, so new state is observed reliably.
1012 if (ub->dev_info.state == UBLK_S_DEV_LIVE)
1013 schedule_delayed_work(&ub->monitor_work,
1014 UBLK_DAEMON_MONITOR_PERIOD);
1017 static inline bool ublk_queue_ready(struct ublk_queue *ubq)
1019 return ubq->nr_io_ready == ubq->q_depth;
1022 static void ublk_cancel_queue(struct ublk_queue *ubq)
1026 if (!ublk_queue_ready(ubq))
1029 for (i = 0; i < ubq->q_depth; i++) {
1030 struct ublk_io *io = &ubq->ios[i];
1032 if (io->flags & UBLK_IO_FLAG_ACTIVE)
1033 io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0);
1036 /* all io commands are canceled */
1037 ubq->nr_io_ready = 0;
1040 /* Cancel all pending commands, must be called after del_gendisk() returns */
1041 static void ublk_cancel_dev(struct ublk_device *ub)
1045 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1046 ublk_cancel_queue(ublk_get_queue(ub, i));
1049 static bool ublk_check_inflight_rq(struct request *rq, void *data)
1053 if (blk_mq_request_started(rq)) {
1060 static void ublk_wait_tagset_rqs_idle(struct ublk_device *ub)
1064 WARN_ON_ONCE(!blk_queue_quiesced(ub->ub_disk->queue));
1067 blk_mq_tagset_busy_iter(&ub->tag_set,
1068 ublk_check_inflight_rq, &idle);
1071 msleep(UBLK_REQUEUE_DELAY_MS);
1075 static void __ublk_quiesce_dev(struct ublk_device *ub)
1077 pr_devel("%s: quiesce ub: dev_id %d state %s\n",
1078 __func__, ub->dev_info.dev_id,
1079 ub->dev_info.state == UBLK_S_DEV_LIVE ?
1080 "LIVE" : "QUIESCED");
1081 blk_mq_quiesce_queue(ub->ub_disk->queue);
1082 ublk_wait_tagset_rqs_idle(ub);
1083 ub->dev_info.state = UBLK_S_DEV_QUIESCED;
1084 ublk_cancel_dev(ub);
1085 /* we are going to release task_struct of ubq_daemon and resets
1086 * ->ubq_daemon to NULL. So in monitor_work, check on ubq_daemon causes UAF.
1087 * Besides, monitor_work is not necessary in QUIESCED state since we have
1088 * already scheduled quiesce_work and quiesced all ubqs.
1090 * Do not let monitor_work schedule itself if state it QUIESCED. And we cancel
1091 * it here and re-schedule it in END_USER_RECOVERY to avoid UAF.
1093 cancel_delayed_work_sync(&ub->monitor_work);
1096 static void ublk_quiesce_work_fn(struct work_struct *work)
1098 struct ublk_device *ub =
1099 container_of(work, struct ublk_device, quiesce_work);
1101 mutex_lock(&ub->mutex);
1102 if (ub->dev_info.state != UBLK_S_DEV_LIVE)
1104 __ublk_quiesce_dev(ub);
1106 mutex_unlock(&ub->mutex);
1109 static void ublk_unquiesce_dev(struct ublk_device *ub)
1113 pr_devel("%s: unquiesce ub: dev_id %d state %s\n",
1114 __func__, ub->dev_info.dev_id,
1115 ub->dev_info.state == UBLK_S_DEV_LIVE ?
1116 "LIVE" : "QUIESCED");
1117 /* quiesce_work has run. We let requeued rqs be aborted
1118 * before running fallback_wq. "force_abort" must be seen
1119 * after request queue is unqiuesced. Then del_gendisk()
1122 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1123 ublk_get_queue(ub, i)->force_abort = true;
1125 blk_mq_unquiesce_queue(ub->ub_disk->queue);
1126 /* We may have requeued some rqs in ublk_quiesce_queue() */
1127 blk_mq_kick_requeue_list(ub->ub_disk->queue);
1130 static void ublk_stop_dev(struct ublk_device *ub)
1132 mutex_lock(&ub->mutex);
1133 if (ub->dev_info.state == UBLK_S_DEV_DEAD)
1135 if (ublk_can_use_recovery(ub)) {
1136 if (ub->dev_info.state == UBLK_S_DEV_LIVE)
1137 __ublk_quiesce_dev(ub);
1138 ublk_unquiesce_dev(ub);
1140 del_gendisk(ub->ub_disk);
1141 ub->dev_info.state = UBLK_S_DEV_DEAD;
1142 ub->dev_info.ublksrv_pid = -1;
1143 put_disk(ub->ub_disk);
1146 ublk_cancel_dev(ub);
1147 mutex_unlock(&ub->mutex);
1148 cancel_delayed_work_sync(&ub->monitor_work);
1151 /* device can only be started after all IOs are ready */
1152 static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq)
1154 mutex_lock(&ub->mutex);
1156 if (ublk_queue_ready(ubq)) {
1157 ubq->ubq_daemon = current;
1158 get_task_struct(ubq->ubq_daemon);
1159 ub->nr_queues_ready++;
1161 if (ub->nr_queues_ready == ub->dev_info.nr_hw_queues)
1162 complete_all(&ub->completion);
1163 mutex_unlock(&ub->mutex);
1166 static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
1167 int tag, struct io_uring_cmd *cmd)
1169 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1170 struct request *req = blk_mq_tag_to_rq(ub->tag_set.tags[q_id], tag);
1172 if (ublk_can_use_task_work(ubq)) {
1173 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
1175 /* should not fail since we call it just in ubq->ubq_daemon */
1176 task_work_add(ubq->ubq_daemon, &data->work, TWA_SIGNAL_NO_IPI);
1178 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
1181 io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
1185 static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
1187 struct ublksrv_io_cmd *ub_cmd = (struct ublksrv_io_cmd *)cmd->cmd;
1188 struct ublk_device *ub = cmd->file->private_data;
1189 struct ublk_queue *ubq;
1191 u32 cmd_op = cmd->cmd_op;
1192 unsigned tag = ub_cmd->tag;
1195 pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
1196 __func__, cmd->cmd_op, ub_cmd->q_id, tag,
1199 if (!(issue_flags & IO_URING_F_SQE128))
1202 if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues)
1205 ubq = ublk_get_queue(ub, ub_cmd->q_id);
1206 if (!ubq || ub_cmd->q_id != ubq->q_id)
1209 if (ubq->ubq_daemon && ubq->ubq_daemon != current)
1212 if (tag >= ubq->q_depth)
1215 io = &ubq->ios[tag];
1217 /* there is pending io cmd, something must be wrong */
1218 if (io->flags & UBLK_IO_FLAG_ACTIVE) {
1224 * ensure that the user issues UBLK_IO_NEED_GET_DATA
1225 * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
1227 if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA))
1228 ^ (cmd_op == UBLK_IO_NEED_GET_DATA))
1232 case UBLK_IO_FETCH_REQ:
1233 /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */
1234 if (ublk_queue_ready(ubq)) {
1239 * The io is being handled by server, so COMMIT_RQ is expected
1240 * instead of FETCH_REQ
1242 if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)
1244 /* FETCH_RQ has to provide IO buffer */
1248 io->flags |= UBLK_IO_FLAG_ACTIVE;
1249 io->addr = ub_cmd->addr;
1251 ublk_mark_io_ready(ub, ubq);
1253 case UBLK_IO_COMMIT_AND_FETCH_REQ:
1254 /* FETCH_RQ has to provide IO buffer */
1257 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1259 io->addr = ub_cmd->addr;
1260 io->flags |= UBLK_IO_FLAG_ACTIVE;
1262 ublk_commit_completion(ub, ub_cmd);
1264 case UBLK_IO_NEED_GET_DATA:
1265 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1267 io->addr = ub_cmd->addr;
1269 io->flags |= UBLK_IO_FLAG_ACTIVE;
1270 ublk_handle_need_get_data(ub, ub_cmd->q_id, ub_cmd->tag, cmd);
1275 return -EIOCBQUEUED;
1278 io_uring_cmd_done(cmd, ret, 0);
1279 pr_devel("%s: complete: cmd op %d, tag %d ret %x io_flags %x\n",
1280 __func__, cmd_op, tag, ret, io->flags);
1281 return -EIOCBQUEUED;
1284 static const struct file_operations ublk_ch_fops = {
1285 .owner = THIS_MODULE,
1286 .open = ublk_ch_open,
1287 .release = ublk_ch_release,
1288 .llseek = no_llseek,
1289 .uring_cmd = ublk_ch_uring_cmd,
1290 .mmap = ublk_ch_mmap,
1293 static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
1295 int size = ublk_queue_cmd_buf_size(ub, q_id);
1296 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1298 if (ubq->ubq_daemon)
1299 put_task_struct(ubq->ubq_daemon);
1300 if (ubq->io_cmd_buf)
1301 free_pages((unsigned long)ubq->io_cmd_buf, get_order(size));
1304 static int ublk_init_queue(struct ublk_device *ub, int q_id)
1306 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1307 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
1311 ubq->flags = ub->dev_info.flags;
1313 ubq->q_depth = ub->dev_info.queue_depth;
1314 size = ublk_queue_cmd_buf_size(ub, q_id);
1316 ptr = (void *) __get_free_pages(gfp_flags, get_order(size));
1320 ubq->io_cmd_buf = ptr;
1325 static void ublk_deinit_queues(struct ublk_device *ub)
1327 int nr_queues = ub->dev_info.nr_hw_queues;
1333 for (i = 0; i < nr_queues; i++)
1334 ublk_deinit_queue(ub, i);
1335 kfree(ub->__queues);
1338 static int ublk_init_queues(struct ublk_device *ub)
1340 int nr_queues = ub->dev_info.nr_hw_queues;
1341 int depth = ub->dev_info.queue_depth;
1342 int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io);
1343 int i, ret = -ENOMEM;
1345 ub->queue_size = ubq_size;
1346 ub->__queues = kcalloc(nr_queues, ubq_size, GFP_KERNEL);
1350 for (i = 0; i < nr_queues; i++) {
1351 if (ublk_init_queue(ub, i))
1355 init_completion(&ub->completion);
1359 ublk_deinit_queues(ub);
1363 static int ublk_alloc_dev_number(struct ublk_device *ub, int idx)
1368 spin_lock(&ublk_idr_lock);
1369 /* allocate id, if @id >= 0, we're requesting that specific id */
1371 err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT);
1375 err = idr_alloc(&ublk_index_idr, ub, 0, 0, GFP_NOWAIT);
1377 spin_unlock(&ublk_idr_lock);
1380 ub->ub_number = err;
1385 static void ublk_free_dev_number(struct ublk_device *ub)
1387 spin_lock(&ublk_idr_lock);
1388 idr_remove(&ublk_index_idr, ub->ub_number);
1389 wake_up_all(&ublk_idr_wq);
1390 spin_unlock(&ublk_idr_lock);
1393 static void ublk_cdev_rel(struct device *dev)
1395 struct ublk_device *ub = container_of(dev, struct ublk_device, cdev_dev);
1397 blk_mq_free_tag_set(&ub->tag_set);
1398 ublk_deinit_queues(ub);
1399 ublk_free_dev_number(ub);
1400 mutex_destroy(&ub->mutex);
1404 static int ublk_add_chdev(struct ublk_device *ub)
1406 struct device *dev = &ub->cdev_dev;
1407 int minor = ub->ub_number;
1410 dev->parent = ublk_misc.this_device;
1411 dev->devt = MKDEV(MAJOR(ublk_chr_devt), minor);
1412 dev->class = ublk_chr_class;
1413 dev->release = ublk_cdev_rel;
1414 device_initialize(dev);
1416 ret = dev_set_name(dev, "ublkc%d", minor);
1420 cdev_init(&ub->cdev, &ublk_ch_fops);
1421 ret = cdev_device_add(&ub->cdev, dev);
1430 static void ublk_stop_work_fn(struct work_struct *work)
1432 struct ublk_device *ub =
1433 container_of(work, struct ublk_device, stop_work);
1438 /* align max io buffer size with PAGE_SIZE */
1439 static void ublk_align_max_io_size(struct ublk_device *ub)
1441 unsigned int max_io_bytes = ub->dev_info.max_io_buf_bytes;
1443 ub->dev_info.max_io_buf_bytes =
1444 round_down(max_io_bytes, PAGE_SIZE);
1447 static int ublk_add_tag_set(struct ublk_device *ub)
1449 ub->tag_set.ops = &ublk_mq_ops;
1450 ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues;
1451 ub->tag_set.queue_depth = ub->dev_info.queue_depth;
1452 ub->tag_set.numa_node = NUMA_NO_NODE;
1453 ub->tag_set.cmd_size = sizeof(struct ublk_rq_data);
1454 ub->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1455 ub->tag_set.driver_data = ub;
1456 return blk_mq_alloc_tag_set(&ub->tag_set);
1459 static void ublk_remove(struct ublk_device *ub)
1462 cancel_work_sync(&ub->stop_work);
1463 cancel_work_sync(&ub->quiesce_work);
1464 cdev_device_del(&ub->cdev, &ub->cdev_dev);
1465 put_device(&ub->cdev_dev);
1468 static struct ublk_device *ublk_get_device_from_id(int idx)
1470 struct ublk_device *ub = NULL;
1475 spin_lock(&ublk_idr_lock);
1476 ub = idr_find(&ublk_index_idr, idx);
1478 ub = ublk_get_device(ub);
1479 spin_unlock(&ublk_idr_lock);
1484 static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd)
1486 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1487 int ublksrv_pid = (int)header->data[0];
1488 struct ublk_device *ub;
1489 struct gendisk *disk;
1492 if (ublksrv_pid <= 0)
1495 ub = ublk_get_device_from_id(header->dev_id);
1499 wait_for_completion_interruptible(&ub->completion);
1501 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
1503 mutex_lock(&ub->mutex);
1504 if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
1505 test_bit(UB_STATE_USED, &ub->state)) {
1510 disk = blk_mq_alloc_disk(&ub->tag_set, ub);
1512 ret = PTR_ERR(disk);
1515 sprintf(disk->disk_name, "ublkb%d", ub->ub_number);
1516 disk->fops = &ub_fops;
1517 disk->private_data = ub;
1519 ub->dev_info.ublksrv_pid = ublksrv_pid;
1522 ret = ublk_apply_params(ub);
1526 get_device(&ub->cdev_dev);
1527 ret = add_disk(disk);
1530 * Has to drop the reference since ->free_disk won't be
1531 * called in case of add_disk failure.
1533 ublk_put_device(ub);
1536 set_bit(UB_STATE_USED, &ub->state);
1537 ub->dev_info.state = UBLK_S_DEV_LIVE;
1542 mutex_unlock(&ub->mutex);
1543 ublk_put_device(ub);
1547 static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd)
1549 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1550 void __user *argp = (void __user *)(unsigned long)header->addr;
1551 struct ublk_device *ub;
1552 cpumask_var_t cpumask;
1553 unsigned long queue;
1554 unsigned int retlen;
1558 if (header->len * BITS_PER_BYTE < nr_cpu_ids)
1560 if (header->len & (sizeof(unsigned long)-1))
1565 ub = ublk_get_device_from_id(header->dev_id);
1569 queue = header->data[0];
1570 if (queue >= ub->dev_info.nr_hw_queues)
1571 goto out_put_device;
1574 if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
1575 goto out_put_device;
1577 for_each_possible_cpu(i) {
1578 if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue)
1579 cpumask_set_cpu(i, cpumask);
1583 retlen = min_t(unsigned short, header->len, cpumask_size());
1584 if (copy_to_user(argp, cpumask, retlen))
1585 goto out_free_cpumask;
1586 if (retlen != header->len &&
1587 clear_user(argp + retlen, header->len - retlen))
1588 goto out_free_cpumask;
1592 free_cpumask_var(cpumask);
1594 ublk_put_device(ub);
1598 static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info *info)
1600 pr_devel("%s: dev id %d flags %llx\n", __func__,
1601 info->dev_id, info->flags);
1602 pr_devel("\t nr_hw_queues %d queue_depth %d\n",
1603 info->nr_hw_queues, info->queue_depth);
1606 static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
1608 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1609 void __user *argp = (void __user *)(unsigned long)header->addr;
1610 struct ublksrv_ctrl_dev_info info;
1611 struct ublk_device *ub;
1614 if (header->len < sizeof(info) || !header->addr)
1616 if (header->queue_id != (u16)-1) {
1617 pr_warn("%s: queue_id is wrong %x\n",
1618 __func__, header->queue_id);
1621 if (copy_from_user(&info, argp, sizeof(info)))
1623 ublk_dump_dev_info(&info);
1624 if (header->dev_id != info.dev_id) {
1625 pr_warn("%s: dev id not match %u %u\n",
1626 __func__, header->dev_id, info.dev_id);
1630 ret = mutex_lock_killable(&ublk_ctl_mutex);
1635 ub = kzalloc(sizeof(*ub), GFP_KERNEL);
1638 mutex_init(&ub->mutex);
1639 spin_lock_init(&ub->mm_lock);
1640 INIT_WORK(&ub->quiesce_work, ublk_quiesce_work_fn);
1641 INIT_WORK(&ub->stop_work, ublk_stop_work_fn);
1642 INIT_DELAYED_WORK(&ub->monitor_work, ublk_daemon_monitor_work);
1644 ret = ublk_alloc_dev_number(ub, header->dev_id);
1648 memcpy(&ub->dev_info, &info, sizeof(info));
1650 /* update device id */
1651 ub->dev_info.dev_id = ub->ub_number;
1654 * 64bit flags will be copied back to userspace as feature
1655 * negotiation result, so have to clear flags which driver
1656 * doesn't support yet, then userspace can get correct flags
1657 * (features) to handle.
1659 ub->dev_info.flags &= UBLK_F_ALL;
1661 /* We are not ready to support zero copy */
1662 ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
1664 ub->dev_info.nr_hw_queues = min_t(unsigned int,
1665 ub->dev_info.nr_hw_queues, nr_cpu_ids);
1666 ublk_align_max_io_size(ub);
1668 ret = ublk_init_queues(ub);
1670 goto out_free_dev_number;
1672 ret = ublk_add_tag_set(ub);
1674 goto out_deinit_queues;
1677 if (copy_to_user(argp, &ub->dev_info, sizeof(info)))
1678 goto out_free_tag_set;
1681 * Add the char dev so that ublksrv daemon can be setup.
1682 * ublk_add_chdev() will cleanup everything if it fails.
1684 ret = ublk_add_chdev(ub);
1688 blk_mq_free_tag_set(&ub->tag_set);
1690 ublk_deinit_queues(ub);
1691 out_free_dev_number:
1692 ublk_free_dev_number(ub);
1694 mutex_destroy(&ub->mutex);
1697 mutex_unlock(&ublk_ctl_mutex);
1701 static inline bool ublk_idr_freed(int id)
1705 spin_lock(&ublk_idr_lock);
1706 ptr = idr_find(&ublk_index_idr, id);
1707 spin_unlock(&ublk_idr_lock);
1712 static int ublk_ctrl_del_dev(int idx)
1714 struct ublk_device *ub;
1717 ret = mutex_lock_killable(&ublk_ctl_mutex);
1721 ub = ublk_get_device_from_id(idx);
1724 ublk_put_device(ub);
1731 * Wait until the idr is removed, then it can be reused after
1732 * DEL_DEV command is returned.
1735 wait_event(ublk_idr_wq, ublk_idr_freed(idx));
1736 mutex_unlock(&ublk_ctl_mutex);
1741 static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
1743 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1745 pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n",
1746 __func__, cmd->cmd_op, header->dev_id, header->queue_id,
1747 header->data[0], header->addr, header->len);
1750 static int ublk_ctrl_stop_dev(struct io_uring_cmd *cmd)
1752 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1753 struct ublk_device *ub;
1755 ub = ublk_get_device_from_id(header->dev_id);
1760 cancel_work_sync(&ub->stop_work);
1761 cancel_work_sync(&ub->quiesce_work);
1763 ublk_put_device(ub);
1767 static int ublk_ctrl_get_dev_info(struct io_uring_cmd *cmd)
1769 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1770 void __user *argp = (void __user *)(unsigned long)header->addr;
1771 struct ublk_device *ub;
1774 if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr)
1777 ub = ublk_get_device_from_id(header->dev_id);
1781 if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info)))
1783 ublk_put_device(ub);
1788 static int ublk_ctrl_get_params(struct io_uring_cmd *cmd)
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_params_header ph;
1793 struct ublk_device *ub;
1796 if (header->len <= sizeof(ph) || !header->addr)
1799 if (copy_from_user(&ph, argp, sizeof(ph)))
1802 if (ph.len > header->len || !ph.len)
1805 if (ph.len > sizeof(struct ublk_params))
1806 ph.len = sizeof(struct ublk_params);
1808 ub = ublk_get_device_from_id(header->dev_id);
1812 mutex_lock(&ub->mutex);
1813 if (copy_to_user(argp, &ub->params, ph.len))
1817 mutex_unlock(&ub->mutex);
1819 ublk_put_device(ub);
1823 static int ublk_ctrl_set_params(struct io_uring_cmd *cmd)
1825 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1826 void __user *argp = (void __user *)(unsigned long)header->addr;
1827 struct ublk_params_header ph;
1828 struct ublk_device *ub;
1831 if (header->len <= sizeof(ph) || !header->addr)
1834 if (copy_from_user(&ph, argp, sizeof(ph)))
1837 if (ph.len > header->len || !ph.len || !ph.types)
1840 if (ph.len > sizeof(struct ublk_params))
1841 ph.len = sizeof(struct ublk_params);
1843 ub = ublk_get_device_from_id(header->dev_id);
1847 /* parameters can only be changed when device isn't live */
1848 mutex_lock(&ub->mutex);
1849 if (ub->dev_info.state == UBLK_S_DEV_LIVE) {
1851 } else if (copy_from_user(&ub->params, argp, ph.len)) {
1854 /* clear all we don't support yet */
1855 ub->params.types &= UBLK_PARAM_TYPE_ALL;
1856 ret = ublk_validate_params(ub);
1858 mutex_unlock(&ub->mutex);
1859 ublk_put_device(ub);
1864 static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
1868 WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq)));
1869 /* All old ioucmds have to be completed */
1870 WARN_ON_ONCE(ubq->nr_io_ready);
1871 /* old daemon is PF_EXITING, put it now */
1872 put_task_struct(ubq->ubq_daemon);
1873 /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */
1874 ubq->ubq_daemon = NULL;
1876 for (i = 0; i < ubq->q_depth; i++) {
1877 struct ublk_io *io = &ubq->ios[i];
1879 /* forget everything now and be ready for new FETCH_REQ */
1886 static int ublk_ctrl_start_recovery(struct io_uring_cmd *cmd)
1888 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1889 struct ublk_device *ub;
1893 ub = ublk_get_device_from_id(header->dev_id);
1897 mutex_lock(&ub->mutex);
1898 if (!ublk_can_use_recovery(ub))
1901 * START_RECOVERY is only allowd after:
1903 * (1) UB_STATE_OPEN is not set, which means the dying process is exited
1904 * and related io_uring ctx is freed so file struct of /dev/ublkcX is
1907 * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work:
1908 * (a)has quiesced request queue
1909 * (b)has requeued every inflight rqs whose io_flags is ACTIVE
1910 * (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE
1911 * (d)has completed/camceled all ioucmds owned by ther dying process
1913 if (test_bit(UB_STATE_OPEN, &ub->state) ||
1914 ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
1918 pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id);
1919 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1920 ublk_queue_reinit(ub, ublk_get_queue(ub, i));
1921 /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */
1923 ub->nr_queues_ready = 0;
1924 init_completion(&ub->completion);
1927 mutex_unlock(&ub->mutex);
1928 ublk_put_device(ub);
1932 static int ublk_ctrl_end_recovery(struct io_uring_cmd *cmd)
1934 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1935 int ublksrv_pid = (int)header->data[0];
1936 struct ublk_device *ub;
1939 ub = ublk_get_device_from_id(header->dev_id);
1943 pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
1944 __func__, ub->dev_info.nr_hw_queues, header->dev_id);
1945 /* wait until new ubq_daemon sending all FETCH_REQ */
1946 wait_for_completion_interruptible(&ub->completion);
1947 pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
1948 __func__, ub->dev_info.nr_hw_queues, header->dev_id);
1950 mutex_lock(&ub->mutex);
1951 if (!ublk_can_use_recovery(ub))
1954 if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
1958 ub->dev_info.ublksrv_pid = ublksrv_pid;
1959 pr_devel("%s: new ublksrv_pid %d, dev id %d\n",
1960 __func__, ublksrv_pid, header->dev_id);
1961 blk_mq_unquiesce_queue(ub->ub_disk->queue);
1962 pr_devel("%s: queue unquiesced, dev id %d.\n",
1963 __func__, header->dev_id);
1964 blk_mq_kick_requeue_list(ub->ub_disk->queue);
1965 ub->dev_info.state = UBLK_S_DEV_LIVE;
1966 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
1969 mutex_unlock(&ub->mutex);
1970 ublk_put_device(ub);
1974 static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
1975 unsigned int issue_flags)
1977 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1980 ublk_ctrl_cmd_dump(cmd);
1982 if (!(issue_flags & IO_URING_F_SQE128))
1986 if (!capable(CAP_SYS_ADMIN))
1990 switch (cmd->cmd_op) {
1991 case UBLK_CMD_START_DEV:
1992 ret = ublk_ctrl_start_dev(cmd);
1994 case UBLK_CMD_STOP_DEV:
1995 ret = ublk_ctrl_stop_dev(cmd);
1997 case UBLK_CMD_GET_DEV_INFO:
1998 ret = ublk_ctrl_get_dev_info(cmd);
2000 case UBLK_CMD_ADD_DEV:
2001 ret = ublk_ctrl_add_dev(cmd);
2003 case UBLK_CMD_DEL_DEV:
2004 ret = ublk_ctrl_del_dev(header->dev_id);
2006 case UBLK_CMD_GET_QUEUE_AFFINITY:
2007 ret = ublk_ctrl_get_queue_affinity(cmd);
2009 case UBLK_CMD_GET_PARAMS:
2010 ret = ublk_ctrl_get_params(cmd);
2012 case UBLK_CMD_SET_PARAMS:
2013 ret = ublk_ctrl_set_params(cmd);
2015 case UBLK_CMD_START_USER_RECOVERY:
2016 ret = ublk_ctrl_start_recovery(cmd);
2018 case UBLK_CMD_END_USER_RECOVERY:
2019 ret = ublk_ctrl_end_recovery(cmd);
2025 io_uring_cmd_done(cmd, ret, 0);
2026 pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
2027 __func__, ret, cmd->cmd_op, header->dev_id, header->queue_id);
2028 return -EIOCBQUEUED;
2031 static const struct file_operations ublk_ctl_fops = {
2032 .open = nonseekable_open,
2033 .uring_cmd = ublk_ctrl_uring_cmd,
2034 .owner = THIS_MODULE,
2035 .llseek = noop_llseek,
2038 static struct miscdevice ublk_misc = {
2039 .minor = MISC_DYNAMIC_MINOR,
2040 .name = "ublk-control",
2041 .fops = &ublk_ctl_fops,
2044 static int __init ublk_init(void)
2048 init_waitqueue_head(&ublk_idr_wq);
2050 ret = misc_register(&ublk_misc);
2054 ret = alloc_chrdev_region(&ublk_chr_devt, 0, UBLK_MINORS, "ublk-char");
2056 goto unregister_mis;
2058 ublk_chr_class = class_create(THIS_MODULE, "ublk-char");
2059 if (IS_ERR(ublk_chr_class)) {
2060 ret = PTR_ERR(ublk_chr_class);
2061 goto free_chrdev_region;
2066 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
2068 misc_deregister(&ublk_misc);
2072 static void __exit ublk_exit(void)
2074 struct ublk_device *ub;
2077 class_destroy(ublk_chr_class);
2079 misc_deregister(&ublk_misc);
2081 idr_for_each_entry(&ublk_index_idr, ub, id)
2084 idr_destroy(&ublk_index_idr);
2085 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
2088 module_init(ublk_init);
2089 module_exit(ublk_exit);
2091 MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");
2092 MODULE_LICENSE("GPL");