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