blob: 6b8aa0d030f76428f9f908cf9e31ad197ce0ede4 [file] [log] [blame]
Ming Lei71f28f32022-07-13 22:07:10 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Userspace block device - block device which IO is handled from userspace
4 *
5 * Take full use of io_uring passthrough command for communicating with
6 * ublk userspace daemon(ublksrvd) for handling basic IO request.
7 *
8 * Copyright 2022 Ming Lei <ming.lei@redhat.com>
9 *
10 * (part of code stolen from loop.c)
11 */
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/sched.h>
15#include <linux/fs.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/stat.h>
19#include <linux/errno.h>
20#include <linux/major.h>
21#include <linux/wait.h>
22#include <linux/blkdev.h>
23#include <linux/init.h>
24#include <linux/swap.h>
25#include <linux/slab.h>
26#include <linux/compat.h>
27#include <linux/mutex.h>
28#include <linux/writeback.h>
29#include <linux/completion.h>
30#include <linux/highmem.h>
31#include <linux/sysfs.h>
32#include <linux/miscdevice.h>
33#include <linux/falloc.h>
34#include <linux/uio.h>
35#include <linux/ioprio.h>
36#include <linux/sched/mm.h>
37#include <linux/uaccess.h>
38#include <linux/cdev.h>
39#include <linux/io_uring.h>
40#include <linux/blk-mq.h>
41#include <linux/delay.h>
42#include <linux/mm.h>
43#include <asm/page.h>
Ming Lei0edb3692022-07-13 22:07:11 +080044#include <linux/task_work.h>
Ming Lei4093cb52023-01-06 12:17:11 +080045#include <linux/namei.h>
Ming Lei71f28f32022-07-13 22:07:10 +080046#include <uapi/linux/ublk_cmd.h>
47
48#define UBLK_MINORS (1U << MINORBITS)
49
Ming Lei6d8c5af2022-07-22 18:38:17 +080050/* All UBLK_F_* have to be included into UBLK_F_ALL */
ZiyangZhangc86019f2022-07-28 20:39:16 +080051#define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \
52 | UBLK_F_URING_CMD_COMP_IN_TASK \
ZiyangZhang77a440e2022-09-23 23:39:14 +080053 | UBLK_F_NEED_GET_DATA \
ZiyangZhanga0d41dc2022-09-23 23:39:17 +080054 | UBLK_F_USER_RECOVERY \
Ming Lei4093cb52023-01-06 12:17:11 +080055 | UBLK_F_USER_RECOVERY_REISSUE \
Ming Lei2d786e62023-04-18 21:18:10 +080056 | UBLK_F_UNPRIVILEGED_DEV \
57 | UBLK_F_CMD_IOCTL_ENCODE)
Ming Lei6d8c5af2022-07-22 18:38:17 +080058
Ming Lei0aa73172022-07-30 17:27:49 +080059/* All UBLK_PARAM_TYPE_* should be included here */
Ming Leiabb864d2023-01-06 12:17:09 +080060#define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | \
61 UBLK_PARAM_TYPE_DISCARD | UBLK_PARAM_TYPE_DEVT)
Ming Lei0aa73172022-07-30 17:27:49 +080062
Ming Lei0edb3692022-07-13 22:07:11 +080063struct ublk_rq_data {
Ming Lei7d4a9312022-11-21 23:56:45 +080064 struct llist_node node;
65 struct callback_head work;
Ming Lei0edb3692022-07-13 22:07:11 +080066};
67
Ming Lei71f28f32022-07-13 22:07:10 +080068struct ublk_uring_cmd_pdu {
Ming Lei3ab6e942022-10-29 09:04:31 +080069 struct ublk_queue *ubq;
Ming Lei71f28f32022-07-13 22:07:10 +080070};
71
72/*
73 * io command is active: sqe cmd is received, and its cqe isn't done
74 *
75 * If the flag is set, the io command is owned by ublk driver, and waited
76 * for incoming blk-mq request from the ublk block device.
77 *
78 * If the flag is cleared, the io command will be completed, and owned by
79 * ublk server.
80 */
81#define UBLK_IO_FLAG_ACTIVE 0x01
82
83/*
84 * IO command is completed via cqe, and it is being handled by ublksrv, and
85 * not committed yet
86 *
87 * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for
88 * cross verification
89 */
90#define UBLK_IO_FLAG_OWNED_BY_SRV 0x02
91
92/*
93 * IO command is aborted, so this flag is set in case of
94 * !UBLK_IO_FLAG_ACTIVE.
95 *
96 * After this flag is observed, any pending or new incoming request
97 * associated with this io command will be failed immediately
98 */
99#define UBLK_IO_FLAG_ABORTED 0x04
100
ZiyangZhangc86019f2022-07-28 20:39:16 +0800101/*
102 * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires
103 * get data buffer address from ublksrv.
104 *
105 * Then, bio data could be copied into this data buffer for a WRITE request
106 * after the IO command is issued again and UBLK_IO_FLAG_NEED_GET_DATA is unset.
107 */
108#define UBLK_IO_FLAG_NEED_GET_DATA 0x08
109
Ming Lei71f28f32022-07-13 22:07:10 +0800110struct ublk_io {
111 /* userspace buffer address from io cmd */
112 __u64 addr;
113 unsigned int flags;
114 int res;
115
116 struct io_uring_cmd *cmd;
117};
118
119struct ublk_queue {
120 int q_id;
121 int q_depth;
122
Ming Lei0edb3692022-07-13 22:07:11 +0800123 unsigned long flags;
Ming Lei71f28f32022-07-13 22:07:10 +0800124 struct task_struct *ubq_daemon;
125 char *io_cmd_buf;
126
Ming Lei3ab6e942022-10-29 09:04:31 +0800127 struct llist_head io_cmds;
128
Ming Lei71f28f32022-07-13 22:07:10 +0800129 unsigned long io_addr; /* mapped vm address */
130 unsigned int max_io_sz;
ZiyangZhangbbae8d12022-09-23 23:39:16 +0800131 bool force_abort;
Ming Lei71f28f32022-07-13 22:07:10 +0800132 unsigned short nr_io_ready; /* how many ios setup */
133 struct ublk_device *dev;
Yushan Zhou72495b52022-10-18 18:01:32 +0800134 struct ublk_io ios[];
Ming Lei71f28f32022-07-13 22:07:10 +0800135};
136
137#define UBLK_DAEMON_MONITOR_PERIOD (5 * HZ)
138
139struct ublk_device {
140 struct gendisk *ub_disk;
Ming Lei71f28f32022-07-13 22:07:10 +0800141
142 char *__queues;
143
Liu Xiaodong29baef72023-01-31 02:05:52 -0500144 unsigned int queue_size;
Ming Lei71f28f32022-07-13 22:07:10 +0800145 struct ublksrv_ctrl_dev_info dev_info;
146
147 struct blk_mq_tag_set tag_set;
148
149 struct cdev cdev;
150 struct device cdev_dev;
151
Dan Carpenter8d9fdb62022-07-26 17:12:23 +0300152#define UB_STATE_OPEN 0
153#define UB_STATE_USED 1
Ming Lei0abe39d2023-02-07 23:07:00 +0800154#define UB_STATE_DELETED 2
Christoph Hellwigfa362042022-07-21 15:09:12 +0200155 unsigned long state;
Ming Lei71f28f32022-07-13 22:07:10 +0800156 int ub_number;
157
158 struct mutex mutex;
159
Ming Leie94eb452022-07-21 23:31:17 +0800160 spinlock_t mm_lock;
Ming Lei71f28f32022-07-13 22:07:10 +0800161 struct mm_struct *mm;
162
Ming Lei0aa73172022-07-30 17:27:49 +0800163 struct ublk_params params;
164
Ming Lei71f28f32022-07-13 22:07:10 +0800165 struct completion completion;
166 unsigned int nr_queues_ready;
Ming Lei73a166d2023-01-06 12:17:07 +0800167 unsigned int nr_privileged_daemon;
Ming Lei71f28f32022-07-13 22:07:10 +0800168
169 /*
170 * Our ubq->daemon may be killed without any notification, so
171 * monitor each queue's daemon periodically
172 */
173 struct delayed_work monitor_work;
ZiyangZhangbbae8d12022-09-23 23:39:16 +0800174 struct work_struct quiesce_work;
Ming Lei71f28f32022-07-13 22:07:10 +0800175 struct work_struct stop_work;
176};
177
Ming Lei0aa73172022-07-30 17:27:49 +0800178/* header of ublk_params */
179struct ublk_params_header {
180 __u32 len;
181 __u32 types;
182};
183
Ming Lei71f28f32022-07-13 22:07:10 +0800184static dev_t ublk_chr_devt;
185static struct class *ublk_chr_class;
186
187static DEFINE_IDR(ublk_index_idr);
188static DEFINE_SPINLOCK(ublk_idr_lock);
189static wait_queue_head_t ublk_idr_wq; /* wait until one idr is freed */
190
191static DEFINE_MUTEX(ublk_ctl_mutex);
192
Ming Lei403ebc82023-01-06 12:17:10 +0800193/*
194 * Max ublk devices allowed to add
195 *
196 * It can be extended to one per-user limit in future or even controlled
197 * by cgroup.
198 */
199static unsigned int ublks_max = 64;
200static unsigned int ublks_added; /* protected by ublk_ctl_mutex */
201
Ming Lei71f28f32022-07-13 22:07:10 +0800202static struct miscdevice ublk_misc;
203
Ming Lei0aa73172022-07-30 17:27:49 +0800204static void ublk_dev_param_basic_apply(struct ublk_device *ub)
205{
206 struct request_queue *q = ub->ub_disk->queue;
207 const struct ublk_param_basic *p = &ub->params.basic;
208
209 blk_queue_logical_block_size(q, 1 << p->logical_bs_shift);
210 blk_queue_physical_block_size(q, 1 << p->physical_bs_shift);
211 blk_queue_io_min(q, 1 << p->io_min_shift);
212 blk_queue_io_opt(q, 1 << p->io_opt_shift);
213
214 blk_queue_write_cache(q, p->attrs & UBLK_ATTR_VOLATILE_CACHE,
215 p->attrs & UBLK_ATTR_FUA);
216 if (p->attrs & UBLK_ATTR_ROTATIONAL)
217 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
218 else
219 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
220
221 blk_queue_max_hw_sectors(q, p->max_sectors);
222 blk_queue_chunk_sectors(q, p->chunk_sectors);
223 blk_queue_virt_boundary(q, p->virt_boundary_mask);
224
225 if (p->attrs & UBLK_ATTR_READ_ONLY)
226 set_disk_ro(ub->ub_disk, true);
227
228 set_capacity(ub->ub_disk, p->dev_sectors);
229}
230
231static void ublk_dev_param_discard_apply(struct ublk_device *ub)
232{
233 struct request_queue *q = ub->ub_disk->queue;
234 const struct ublk_param_discard *p = &ub->params.discard;
235
236 q->limits.discard_alignment = p->discard_alignment;
237 q->limits.discard_granularity = p->discard_granularity;
238 blk_queue_max_discard_sectors(q, p->max_discard_sectors);
239 blk_queue_max_write_zeroes_sectors(q,
240 p->max_write_zeroes_sectors);
241 blk_queue_max_discard_segments(q, p->max_discard_segments);
242}
243
244static int ublk_validate_params(const struct ublk_device *ub)
245{
246 /* basic param is the only one which must be set */
247 if (ub->params.types & UBLK_PARAM_TYPE_BASIC) {
248 const struct ublk_param_basic *p = &ub->params.basic;
249
Ming Lei1d166522023-04-06 20:40:59 +0800250 if (p->logical_bs_shift > PAGE_SHIFT || p->logical_bs_shift < 9)
Ming Lei0aa73172022-07-30 17:27:49 +0800251 return -EINVAL;
252
253 if (p->logical_bs_shift > p->physical_bs_shift)
254 return -EINVAL;
255
Ming Lei4bf9cbf2022-07-30 17:27:50 +0800256 if (p->max_sectors > (ub->dev_info.max_io_buf_bytes >> 9))
Ming Lei0aa73172022-07-30 17:27:49 +0800257 return -EINVAL;
258 } else
259 return -EINVAL;
260
261 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) {
262 const struct ublk_param_discard *p = &ub->params.discard;
263
264 /* So far, only support single segment discard */
265 if (p->max_discard_sectors && p->max_discard_segments != 1)
266 return -EINVAL;
267
268 if (!p->discard_granularity)
269 return -EINVAL;
270 }
271
Ming Leiabb864d2023-01-06 12:17:09 +0800272 /* dev_t is read-only */
273 if (ub->params.types & UBLK_PARAM_TYPE_DEVT)
274 return -EINVAL;
275
Ming Lei0aa73172022-07-30 17:27:49 +0800276 return 0;
277}
278
279static int ublk_apply_params(struct ublk_device *ub)
280{
281 if (!(ub->params.types & UBLK_PARAM_TYPE_BASIC))
282 return -EINVAL;
283
284 ublk_dev_param_basic_apply(ub);
285
286 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD)
287 ublk_dev_param_discard_apply(ub);
288
289 return 0;
290}
291
Ming Lei0edb3692022-07-13 22:07:11 +0800292static inline bool ublk_can_use_task_work(const struct ublk_queue *ubq)
293{
294 if (IS_BUILTIN(CONFIG_BLK_DEV_UBLK) &&
295 !(ubq->flags & UBLK_F_URING_CMD_COMP_IN_TASK))
296 return true;
297 return false;
298}
299
ZiyangZhangc86019f2022-07-28 20:39:16 +0800300static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
301{
Ming Lei96cf2f52023-03-30 19:36:23 +0800302 return ubq->flags & UBLK_F_NEED_GET_DATA;
ZiyangZhangc86019f2022-07-28 20:39:16 +0800303}
304
Ming Lei71f28f32022-07-13 22:07:10 +0800305static struct ublk_device *ublk_get_device(struct ublk_device *ub)
306{
307 if (kobject_get_unless_zero(&ub->cdev_dev.kobj))
308 return ub;
309 return NULL;
310}
311
312static void ublk_put_device(struct ublk_device *ub)
313{
314 put_device(&ub->cdev_dev);
315}
316
317static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev,
318 int qid)
319{
320 return (struct ublk_queue *)&(dev->__queues[qid * dev->queue_size]);
321}
322
323static inline bool ublk_rq_has_data(const struct request *rq)
324{
Ziyang Zhang731e2082023-02-07 15:08:37 +0800325 return bio_has_data(rq->bio);
Ming Lei71f28f32022-07-13 22:07:10 +0800326}
327
328static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
329 int tag)
330{
331 return (struct ublksrv_io_desc *)
332 &(ubq->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]);
333}
334
335static inline char *ublk_queue_cmd_buf(struct ublk_device *ub, int q_id)
336{
337 return ublk_get_queue(ub, q_id)->io_cmd_buf;
338}
339
340static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id)
341{
342 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
343
344 return round_up(ubq->q_depth * sizeof(struct ublksrv_io_desc),
345 PAGE_SIZE);
346}
347
ZiyangZhanga0d41dc2022-09-23 23:39:17 +0800348static inline bool ublk_queue_can_use_recovery_reissue(
349 struct ublk_queue *ubq)
350{
Ming Lei96cf2f52023-03-30 19:36:23 +0800351 return (ubq->flags & UBLK_F_USER_RECOVERY) &&
352 (ubq->flags & UBLK_F_USER_RECOVERY_REISSUE);
ZiyangZhanga0d41dc2022-09-23 23:39:17 +0800353}
354
ZiyangZhang77a440e2022-09-23 23:39:14 +0800355static inline bool ublk_queue_can_use_recovery(
356 struct ublk_queue *ubq)
357{
Ming Lei96cf2f52023-03-30 19:36:23 +0800358 return ubq->flags & UBLK_F_USER_RECOVERY;
ZiyangZhang77a440e2022-09-23 23:39:14 +0800359}
360
361static inline bool ublk_can_use_recovery(struct ublk_device *ub)
362{
Ming Lei96cf2f52023-03-30 19:36:23 +0800363 return ub->dev_info.flags & UBLK_F_USER_RECOVERY;
ZiyangZhang77a440e2022-09-23 23:39:14 +0800364}
365
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +0200366static void ublk_free_disk(struct gendisk *disk)
367{
368 struct ublk_device *ub = disk->private_data;
369
370 clear_bit(UB_STATE_USED, &ub->state);
371 put_device(&ub->cdev_dev);
372}
373
Ming Lei48a90512023-01-31 12:04:46 +0800374static void ublk_store_owner_uid_gid(unsigned int *owner_uid,
375 unsigned int *owner_gid)
376{
377 kuid_t uid;
378 kgid_t gid;
379
380 current_uid_gid(&uid, &gid);
381
382 *owner_uid = from_kuid(&init_user_ns, uid);
383 *owner_gid = from_kgid(&init_user_ns, gid);
384}
385
386static int ublk_open(struct block_device *bdev, fmode_t mode)
387{
388 struct ublk_device *ub = bdev->bd_disk->private_data;
389
390 if (capable(CAP_SYS_ADMIN))
391 return 0;
392
393 /*
394 * If it is one unprivileged device, only owner can open
395 * the disk. Otherwise it could be one trap made by one
396 * evil user who grants this disk's privileges to other
397 * users deliberately.
398 *
399 * This way is reasonable too given anyone can create
400 * unprivileged device, and no need other's grant.
401 */
402 if (ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV) {
403 unsigned int curr_uid, curr_gid;
404
405 ublk_store_owner_uid_gid(&curr_uid, &curr_gid);
406
407 if (curr_uid != ub->dev_info.owner_uid || curr_gid !=
408 ub->dev_info.owner_gid)
409 return -EPERM;
410 }
411
412 return 0;
413}
414
Ming Lei71f28f32022-07-13 22:07:10 +0800415static const struct block_device_operations ub_fops = {
416 .owner = THIS_MODULE,
Ming Lei48a90512023-01-31 12:04:46 +0800417 .open = ublk_open,
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +0200418 .free_disk = ublk_free_disk,
Ming Lei71f28f32022-07-13 22:07:10 +0800419};
420
421#define UBLK_MAX_PIN_PAGES 32
422
423struct ublk_map_data {
Ming Lei71f28f32022-07-13 22:07:10 +0800424 const struct request *rq;
Ming Leiae9f5cc2023-03-30 19:36:24 +0800425 unsigned long ubuf;
426 unsigned int len;
Ming Lei71f28f32022-07-13 22:07:10 +0800427};
428
429struct ublk_io_iter {
430 struct page *pages[UBLK_MAX_PIN_PAGES];
431 unsigned pg_off; /* offset in the 1st page in pages */
432 int nr_pages; /* how many page pointers in pages */
433 struct bio *bio;
434 struct bvec_iter iter;
435};
436
437static inline unsigned ublk_copy_io_pages(struct ublk_io_iter *data,
438 unsigned max_bytes, bool to_vm)
439{
440 const unsigned total = min_t(unsigned, max_bytes,
441 PAGE_SIZE - data->pg_off +
442 ((data->nr_pages - 1) << PAGE_SHIFT));
443 unsigned done = 0;
444 unsigned pg_idx = 0;
445
446 while (done < total) {
447 struct bio_vec bv = bio_iter_iovec(data->bio, data->iter);
448 const unsigned int bytes = min3(bv.bv_len, total - done,
449 (unsigned)(PAGE_SIZE - data->pg_off));
450 void *bv_buf = bvec_kmap_local(&bv);
451 void *pg_buf = kmap_local_page(data->pages[pg_idx]);
452
453 if (to_vm)
454 memcpy(pg_buf + data->pg_off, bv_buf, bytes);
455 else
456 memcpy(bv_buf, pg_buf + data->pg_off, bytes);
457
458 kunmap_local(pg_buf);
459 kunmap_local(bv_buf);
460
461 /* advance page array */
462 data->pg_off += bytes;
463 if (data->pg_off == PAGE_SIZE) {
464 pg_idx += 1;
465 data->pg_off = 0;
466 }
467
468 done += bytes;
469
470 /* advance bio */
471 bio_advance_iter_single(data->bio, &data->iter, bytes);
472 if (!data->iter.bi_size) {
473 data->bio = data->bio->bi_next;
474 if (data->bio == NULL)
475 break;
476 data->iter = data->bio->bi_iter;
477 }
478 }
479
480 return done;
481}
482
Ming Lei2f3af722023-03-30 19:36:22 +0800483static int ublk_copy_user_pages(struct ublk_map_data *data, bool to_vm)
Ming Lei71f28f32022-07-13 22:07:10 +0800484{
485 const unsigned int gup_flags = to_vm ? FOLL_WRITE : 0;
Ming Leiae9f5cc2023-03-30 19:36:24 +0800486 const unsigned long start_vm = data->ubuf;
Ming Lei71f28f32022-07-13 22:07:10 +0800487 unsigned int done = 0;
488 struct ublk_io_iter iter = {
489 .pg_off = start_vm & (PAGE_SIZE - 1),
490 .bio = data->rq->bio,
491 .iter = data->rq->bio->bi_iter,
492 };
Ming Leiae9f5cc2023-03-30 19:36:24 +0800493 const unsigned int nr_pages = round_up(data->len +
Ming Lei71f28f32022-07-13 22:07:10 +0800494 (start_vm & (PAGE_SIZE - 1)), PAGE_SIZE) >> PAGE_SHIFT;
495
496 while (done < nr_pages) {
497 const unsigned to_pin = min_t(unsigned, UBLK_MAX_PIN_PAGES,
498 nr_pages - done);
499 unsigned i, len;
500
501 iter.nr_pages = get_user_pages_fast(start_vm +
502 (done << PAGE_SHIFT), to_pin, gup_flags,
503 iter.pages);
504 if (iter.nr_pages <= 0)
505 return done == 0 ? iter.nr_pages : done;
Ming Leiae9f5cc2023-03-30 19:36:24 +0800506 len = ublk_copy_io_pages(&iter, data->len, to_vm);
Ming Lei71f28f32022-07-13 22:07:10 +0800507 for (i = 0; i < iter.nr_pages; i++) {
508 if (to_vm)
509 set_page_dirty(iter.pages[i]);
510 put_page(iter.pages[i]);
511 }
Ming Leiae9f5cc2023-03-30 19:36:24 +0800512 data->len -= len;
Ming Lei71f28f32022-07-13 22:07:10 +0800513 done += iter.nr_pages;
514 }
515
516 return done;
517}
518
Ming Lei2f3af722023-03-30 19:36:22 +0800519static inline bool ublk_need_map_req(const struct request *req)
520{
521 return ublk_rq_has_data(req) && req_op(req) == REQ_OP_WRITE;
522}
523
524static inline bool ublk_need_unmap_req(const struct request *req)
525{
526 return ublk_rq_has_data(req) && req_op(req) == REQ_OP_READ;
527}
528
Ming Lei71f28f32022-07-13 22:07:10 +0800529static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req,
530 struct ublk_io *io)
531{
532 const unsigned int rq_bytes = blk_rq_bytes(req);
Ming Lei23ef8222023-03-30 19:36:21 +0800533
Ming Lei71f28f32022-07-13 22:07:10 +0800534 /*
535 * no zero copy, we delay copy WRITE request data into ublksrv
536 * context and the big benefit is that pinning pages in current
537 * context is pretty fast, see ublk_pin_user_pages
538 */
Ming Lei2f3af722023-03-30 19:36:22 +0800539 if (ublk_need_map_req(req)) {
Ming Lei71f28f32022-07-13 22:07:10 +0800540 struct ublk_map_data data = {
Ming Lei71f28f32022-07-13 22:07:10 +0800541 .rq = req,
Ming Leiae9f5cc2023-03-30 19:36:24 +0800542 .ubuf = io->addr,
543 .len = rq_bytes,
Ming Lei71f28f32022-07-13 22:07:10 +0800544 };
545
546 ublk_copy_user_pages(&data, true);
547
Ming Leiae9f5cc2023-03-30 19:36:24 +0800548 return rq_bytes - data.len;
Ming Lei71f28f32022-07-13 22:07:10 +0800549 }
550 return rq_bytes;
551}
552
553static int ublk_unmap_io(const struct ublk_queue *ubq,
554 const struct request *req,
555 struct ublk_io *io)
556{
557 const unsigned int rq_bytes = blk_rq_bytes(req);
558
Ming Lei2f3af722023-03-30 19:36:22 +0800559 if (ublk_need_unmap_req(req)) {
Ming Lei71f28f32022-07-13 22:07:10 +0800560 struct ublk_map_data data = {
Ming Lei71f28f32022-07-13 22:07:10 +0800561 .rq = req,
Ming Leiae9f5cc2023-03-30 19:36:24 +0800562 .ubuf = io->addr,
563 .len = io->res,
Ming Lei71f28f32022-07-13 22:07:10 +0800564 };
565
566 WARN_ON_ONCE(io->res > rq_bytes);
567
568 ublk_copy_user_pages(&data, false);
569
Ming Leiae9f5cc2023-03-30 19:36:24 +0800570 return io->res - data.len;
Ming Lei71f28f32022-07-13 22:07:10 +0800571 }
572 return rq_bytes;
573}
574
575static inline unsigned int ublk_req_build_flags(struct request *req)
576{
577 unsigned flags = 0;
578
579 if (req->cmd_flags & REQ_FAILFAST_DEV)
580 flags |= UBLK_IO_F_FAILFAST_DEV;
581
582 if (req->cmd_flags & REQ_FAILFAST_TRANSPORT)
583 flags |= UBLK_IO_F_FAILFAST_TRANSPORT;
584
585 if (req->cmd_flags & REQ_FAILFAST_DRIVER)
586 flags |= UBLK_IO_F_FAILFAST_DRIVER;
587
588 if (req->cmd_flags & REQ_META)
589 flags |= UBLK_IO_F_META;
590
Ming Lei71f28f32022-07-13 22:07:10 +0800591 if (req->cmd_flags & REQ_FUA)
592 flags |= UBLK_IO_F_FUA;
593
Ming Lei71f28f32022-07-13 22:07:10 +0800594 if (req->cmd_flags & REQ_NOUNMAP)
595 flags |= UBLK_IO_F_NOUNMAP;
596
597 if (req->cmd_flags & REQ_SWAP)
598 flags |= UBLK_IO_F_SWAP;
599
600 return flags;
601}
602
Ming Leif2450f82022-07-16 17:53:44 +0800603static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
Ming Lei71f28f32022-07-13 22:07:10 +0800604{
605 struct ublksrv_io_desc *iod = ublk_get_iod(ubq, req->tag);
606 struct ublk_io *io = &ubq->ios[req->tag];
607 u32 ublk_op;
608
609 switch (req_op(req)) {
610 case REQ_OP_READ:
611 ublk_op = UBLK_IO_OP_READ;
612 break;
613 case REQ_OP_WRITE:
614 ublk_op = UBLK_IO_OP_WRITE;
615 break;
616 case REQ_OP_FLUSH:
617 ublk_op = UBLK_IO_OP_FLUSH;
618 break;
619 case REQ_OP_DISCARD:
620 ublk_op = UBLK_IO_OP_DISCARD;
621 break;
622 case REQ_OP_WRITE_ZEROES:
623 ublk_op = UBLK_IO_OP_WRITE_ZEROES;
624 break;
625 default:
626 return BLK_STS_IOERR;
627 }
628
629 /* need to translate since kernel may change */
630 iod->op_flags = ublk_op | ublk_req_build_flags(req);
631 iod->nr_sectors = blk_rq_sectors(req);
632 iod->start_sector = blk_rq_pos(req);
633 iod->addr = io->addr;
634
635 return BLK_STS_OK;
636}
637
638static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu(
639 struct io_uring_cmd *ioucmd)
640{
641 return (struct ublk_uring_cmd_pdu *)&ioucmd->pdu;
642}
643
ZiyangZhang966120b2022-08-15 10:36:31 +0800644static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq)
Ming Lei71f28f32022-07-13 22:07:10 +0800645{
646 return ubq->ubq_daemon->flags & PF_EXITING;
647}
648
649/* todo: handle partial completion */
650static void ublk_complete_rq(struct request *req)
651{
652 struct ublk_queue *ubq = req->mq_hctx->driver_data;
653 struct ublk_io *io = &ubq->ios[req->tag];
654 unsigned int unmapped_bytes;
Ming Lei903f8ae2023-03-30 19:36:20 +0800655 blk_status_t res = BLK_STS_OK;
Ming Lei71f28f32022-07-13 22:07:10 +0800656
657 /* failed read IO if nothing is read */
658 if (!io->res && req_op(req) == REQ_OP_READ)
659 io->res = -EIO;
660
661 if (io->res < 0) {
Ming Lei903f8ae2023-03-30 19:36:20 +0800662 res = errno_to_blk_status(io->res);
663 goto exit;
Ming Lei71f28f32022-07-13 22:07:10 +0800664 }
665
666 /*
Ziyang Zhangb3523892023-02-07 15:08:38 +0800667 * FLUSH, DISCARD or WRITE_ZEROES usually won't return bytes returned, so end them
Ming Lei71f28f32022-07-13 22:07:10 +0800668 * directly.
669 *
670 * Both the two needn't unmap.
671 */
Ming Lei903f8ae2023-03-30 19:36:20 +0800672 if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE)
673 goto exit;
Ming Lei71f28f32022-07-13 22:07:10 +0800674
675 /* for READ request, writing data in iod->addr to rq buffers */
676 unmapped_bytes = ublk_unmap_io(ubq, req, io);
677
678 /*
679 * Extremely impossible since we got data filled in just before
680 *
681 * Re-read simply for this unlikely case.
682 */
683 if (unlikely(unmapped_bytes < io->res))
684 io->res = unmapped_bytes;
685
686 if (blk_update_request(req, BLK_STS_OK, io->res))
687 blk_mq_requeue_request(req, true);
688 else
689 __blk_mq_end_request(req, BLK_STS_OK);
Ming Lei903f8ae2023-03-30 19:36:20 +0800690
691 return;
692exit:
693 blk_mq_end_request(req, res);
Ming Lei71f28f32022-07-13 22:07:10 +0800694}
695
696/*
ZiyangZhangbb241742022-08-15 10:36:32 +0800697 * Since __ublk_rq_task_work always fails requests immediately during
698 * exiting, __ublk_fail_req() is only called from abort context during
699 * exiting. So lock is unnecessary.
Ming Lei71f28f32022-07-13 22:07:10 +0800700 *
701 * Also aborting may not be started yet, keep in mind that one failed
702 * request may be issued by block layer again.
703 */
ZiyangZhanga0d41dc2022-09-23 23:39:17 +0800704static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io,
705 struct request *req)
Ming Lei71f28f32022-07-13 22:07:10 +0800706{
707 WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
708
709 if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
710 io->flags |= UBLK_IO_FLAG_ABORTED;
ZiyangZhanga0d41dc2022-09-23 23:39:17 +0800711 if (ublk_queue_can_use_recovery_reissue(ubq))
712 blk_mq_requeue_request(req, false);
713 else
714 blk_mq_end_request(req, BLK_STS_IOERR);
Ming Lei71f28f32022-07-13 22:07:10 +0800715 }
716}
717
Jens Axboe9d2789a2023-03-20 20:01:25 -0600718static void ubq_complete_io_cmd(struct ublk_io *io, int res,
719 unsigned issue_flags)
ZiyangZhangc86019f2022-07-28 20:39:16 +0800720{
721 /* mark this cmd owned by ublksrv */
722 io->flags |= UBLK_IO_FLAG_OWNED_BY_SRV;
723
724 /*
725 * clear ACTIVE since we are done with this sqe/cmd slot
726 * We can only accept io cmd in case of being not active.
727 */
728 io->flags &= ~UBLK_IO_FLAG_ACTIVE;
729
730 /* tell ublksrv one io request is coming */
Jens Axboe9d2789a2023-03-20 20:01:25 -0600731 io_uring_cmd_done(io->cmd, res, 0, issue_flags);
ZiyangZhangc86019f2022-07-28 20:39:16 +0800732}
733
Ming Lei71f28f32022-07-13 22:07:10 +0800734#define UBLK_REQUEUE_DELAY_MS 3
735
ZiyangZhang42cf5fc2022-09-23 23:39:15 +0800736static inline void __ublk_abort_rq(struct ublk_queue *ubq,
737 struct request *rq)
738{
739 /* We cannot process this rq so just requeue it. */
740 if (ublk_queue_can_use_recovery(ubq))
741 blk_mq_requeue_request(rq, false);
742 else
743 blk_mq_end_request(rq, BLK_STS_IOERR);
744
745 mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0);
746}
747
Jens Axboe9d2789a2023-03-20 20:01:25 -0600748static inline void __ublk_rq_task_work(struct request *req,
749 unsigned issue_flags)
Ming Lei71f28f32022-07-13 22:07:10 +0800750{
Ming Lei71f28f32022-07-13 22:07:10 +0800751 struct ublk_queue *ubq = req->mq_hctx->driver_data;
752 int tag = req->tag;
753 struct ublk_io *io = &ubq->ios[tag];
Ming Lei71f28f32022-07-13 22:07:10 +0800754 unsigned int mapped_bytes;
755
756 pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n",
757 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
758 ublk_get_iod(ubq, req->tag)->addr);
759
ZiyangZhangae3f7192022-09-23 23:39:13 +0800760 /*
761 * Task is exiting if either:
762 *
763 * (1) current != ubq_daemon.
764 * io_uring_cmd_complete_in_task() tries to run task_work
765 * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING.
766 *
767 * (2) current->flags & PF_EXITING.
768 */
769 if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) {
ZiyangZhang42cf5fc2022-09-23 23:39:15 +0800770 __ublk_abort_rq(ubq, req);
Ming Lei71f28f32022-07-13 22:07:10 +0800771 return;
772 }
773
Ming Lei2f3af722023-03-30 19:36:22 +0800774 if (ublk_need_get_data(ubq) && ublk_need_map_req(req)) {
ZiyangZhangc86019f2022-07-28 20:39:16 +0800775 /*
776 * We have not handled UBLK_IO_NEED_GET_DATA command yet,
777 * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv
778 * and notify it.
779 */
780 if (!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA)) {
781 io->flags |= UBLK_IO_FLAG_NEED_GET_DATA;
782 pr_devel("%s: need get data. op %d, qid %d tag %d io_flags %x\n",
783 __func__, io->cmd->cmd_op, ubq->q_id,
784 req->tag, io->flags);
Jens Axboe9d2789a2023-03-20 20:01:25 -0600785 ubq_complete_io_cmd(io, UBLK_IO_RES_NEED_GET_DATA, issue_flags);
ZiyangZhangc86019f2022-07-28 20:39:16 +0800786 return;
787 }
788 /*
789 * We have handled UBLK_IO_NEED_GET_DATA command,
790 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
791 * do the copy work.
792 */
793 io->flags &= ~UBLK_IO_FLAG_NEED_GET_DATA;
ZiyangZhang92cb6e22022-08-10 13:52:12 +0800794 /* update iod->addr because ublksrv may have passed a new io buffer */
795 ublk_get_iod(ubq, req->tag)->addr = io->addr;
796 pr_devel("%s: update iod->addr: op %d, qid %d tag %d io_flags %x addr %llx\n",
797 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
798 ublk_get_iod(ubq, req->tag)->addr);
ZiyangZhangc86019f2022-07-28 20:39:16 +0800799 }
800
Ming Lei71f28f32022-07-13 22:07:10 +0800801 mapped_bytes = ublk_map_io(ubq, req, io);
802
803 /* partially mapped, update io descriptor */
804 if (unlikely(mapped_bytes != blk_rq_bytes(req))) {
805 /*
806 * Nothing mapped, retry until we succeed.
807 *
808 * We may never succeed in mapping any bytes here because
809 * of OOM. TODO: reserve one buffer with single page pinned
810 * for providing forward progress guarantee.
811 */
812 if (unlikely(!mapped_bytes)) {
813 blk_mq_requeue_request(req, false);
814 blk_mq_delay_kick_requeue_list(req->q,
815 UBLK_REQUEUE_DELAY_MS);
816 return;
817 }
818
819 ublk_get_iod(ubq, req->tag)->nr_sectors =
820 mapped_bytes >> 9;
821 }
822
Jens Axboe9d2789a2023-03-20 20:01:25 -0600823 ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
Ming Lei71f28f32022-07-13 22:07:10 +0800824}
825
Jens Axboe9d2789a2023-03-20 20:01:25 -0600826static inline void ublk_forward_io_cmds(struct ublk_queue *ubq,
827 unsigned issue_flags)
Ming Lei7d4a9312022-11-21 23:56:45 +0800828{
829 struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds);
830 struct ublk_rq_data *data, *tmp;
831
832 io_cmds = llist_reverse_order(io_cmds);
833 llist_for_each_entry_safe(data, tmp, io_cmds, node)
Jens Axboe9d2789a2023-03-20 20:01:25 -0600834 __ublk_rq_task_work(blk_mq_rq_from_pdu(data), issue_flags);
Ming Lei7d4a9312022-11-21 23:56:45 +0800835}
836
837static inline void ublk_abort_io_cmds(struct ublk_queue *ubq)
838{
839 struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds);
840 struct ublk_rq_data *data, *tmp;
841
842 llist_for_each_entry_safe(data, tmp, io_cmds, node)
843 __ublk_abort_rq(ubq, blk_mq_rq_from_pdu(data));
844}
845
Jens Axboe9d2789a2023-03-20 20:01:25 -0600846static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd, unsigned issue_flags)
Ming Lei0edb3692022-07-13 22:07:11 +0800847{
848 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
Ming Lei3ab6e942022-10-29 09:04:31 +0800849 struct ublk_queue *ubq = pdu->ubq;
Ming Lei0edb3692022-07-13 22:07:11 +0800850
Jens Axboe9d2789a2023-03-20 20:01:25 -0600851 ublk_forward_io_cmds(ubq, issue_flags);
Ming Lei0edb3692022-07-13 22:07:11 +0800852}
853
854static void ublk_rq_task_work_fn(struct callback_head *work)
855{
856 struct ublk_rq_data *data = container_of(work,
857 struct ublk_rq_data, work);
858 struct request *req = blk_mq_rq_from_pdu(data);
Ming Lei7d4a9312022-11-21 23:56:45 +0800859 struct ublk_queue *ubq = req->mq_hctx->driver_data;
Jens Axboe9d2789a2023-03-20 20:01:25 -0600860 unsigned issue_flags = IO_URING_F_UNLOCKED;
Ming Lei0edb3692022-07-13 22:07:11 +0800861
Jens Axboe9d2789a2023-03-20 20:01:25 -0600862 ublk_forward_io_cmds(ubq, issue_flags);
Ming Lei0edb3692022-07-13 22:07:11 +0800863}
864
Ming Lei7d4a9312022-11-21 23:56:45 +0800865static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
Ming Lei3ab6e942022-10-29 09:04:31 +0800866{
Ming Lei7d4a9312022-11-21 23:56:45 +0800867 struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq);
868 struct ublk_io *io;
Ming Lei3ab6e942022-10-29 09:04:31 +0800869
Ming Lei7d4a9312022-11-21 23:56:45 +0800870 if (!llist_add(&data->node, &ubq->io_cmds))
871 return;
872
873 io = &ubq->ios[rq->tag];
Ming Lei3ab6e942022-10-29 09:04:31 +0800874 /*
875 * If the check pass, we know that this is a re-issued request aborted
876 * previously in monitor_work because the ubq_daemon(cmd's task) is
877 * PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore
878 * because this ioucmd's io_uring context may be freed now if no inflight
879 * ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work.
880 *
881 * Note: monitor_work sets UBLK_IO_FLAG_ABORTED and ends this request(releasing
882 * the tag). Then the request is re-started(allocating the tag) and we are here.
883 * Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED
884 * guarantees that here is a re-issued request aborted previously.
885 */
886 if (unlikely(io->flags & UBLK_IO_FLAG_ABORTED)) {
Ming Lei7d4a9312022-11-21 23:56:45 +0800887 ublk_abort_io_cmds(ubq);
888 } else if (ublk_can_use_task_work(ubq)) {
889 if (task_work_add(ubq->ubq_daemon, &data->work,
890 TWA_SIGNAL_NO_IPI))
891 ublk_abort_io_cmds(ubq);
Ming Lei3ab6e942022-10-29 09:04:31 +0800892 } else {
893 struct io_uring_cmd *cmd = io->cmd;
894 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
895
896 pdu->ubq = ubq;
897 io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
898 }
899}
900
Ming Lei71f28f32022-07-13 22:07:10 +0800901static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
902 const struct blk_mq_queue_data *bd)
903{
904 struct ublk_queue *ubq = hctx->driver_data;
905 struct request *rq = bd->rq;
Ming Lei71f28f32022-07-13 22:07:10 +0800906 blk_status_t res;
907
908 /* fill iod to slot in io cmd buffer */
909 res = ublk_setup_iod(ubq, rq);
910 if (unlikely(res != BLK_STS_OK))
911 return BLK_STS_IOERR;
Ming Lei3ab6e942022-10-29 09:04:31 +0800912
ZiyangZhangbbae8d12022-09-23 23:39:16 +0800913 /* With recovery feature enabled, force_abort is set in
914 * ublk_stop_dev() before calling del_gendisk(). We have to
915 * abort all requeued and new rqs here to let del_gendisk()
916 * move on. Besides, we cannot not call io_uring_cmd_complete_in_task()
917 * to avoid UAF on io_uring ctx.
918 *
919 * Note: force_abort is guaranteed to be seen because it is set
920 * before request queue is unqiuesced.
921 */
922 if (ublk_queue_can_use_recovery(ubq) && unlikely(ubq->force_abort))
923 return BLK_STS_IOERR;
Ming Lei71f28f32022-07-13 22:07:10 +0800924
925 blk_mq_start_request(bd->rq);
926
927 if (unlikely(ubq_daemon_is_dying(ubq))) {
ZiyangZhang42cf5fc2022-09-23 23:39:15 +0800928 __ublk_abort_rq(ubq, rq);
929 return BLK_STS_OK;
Ming Lei71f28f32022-07-13 22:07:10 +0800930 }
931
Ming Lei7d4a9312022-11-21 23:56:45 +0800932 ublk_queue_cmd(ubq, rq);
Ming Lei0edb3692022-07-13 22:07:11 +0800933
Ming Lei71f28f32022-07-13 22:07:10 +0800934 return BLK_STS_OK;
935}
936
Ming Lei71f28f32022-07-13 22:07:10 +0800937static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
938 unsigned int hctx_idx)
939{
Ming Leicebbe572022-07-14 18:32:01 +0800940 struct ublk_device *ub = driver_data;
Ming Lei71f28f32022-07-13 22:07:10 +0800941 struct ublk_queue *ubq = ublk_get_queue(ub, hctx->queue_num);
942
943 hctx->driver_data = ubq;
944 return 0;
945}
946
Ming Lei0edb3692022-07-13 22:07:11 +0800947static int ublk_init_rq(struct blk_mq_tag_set *set, struct request *req,
948 unsigned int hctx_idx, unsigned int numa_node)
949{
950 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
951
952 init_task_work(&data->work, ublk_rq_task_work_fn);
953 return 0;
954}
955
Ming Lei71f28f32022-07-13 22:07:10 +0800956static const struct blk_mq_ops ublk_mq_ops = {
957 .queue_rq = ublk_queue_rq,
958 .init_hctx = ublk_init_hctx,
Ming Lei0edb3692022-07-13 22:07:11 +0800959 .init_request = ublk_init_rq,
Ming Lei71f28f32022-07-13 22:07:10 +0800960};
961
962static int ublk_ch_open(struct inode *inode, struct file *filp)
963{
964 struct ublk_device *ub = container_of(inode->i_cdev,
965 struct ublk_device, cdev);
966
Christoph Hellwigfa362042022-07-21 15:09:12 +0200967 if (test_and_set_bit(UB_STATE_OPEN, &ub->state))
968 return -EBUSY;
969 filp->private_data = ub;
970 return 0;
Ming Lei71f28f32022-07-13 22:07:10 +0800971}
972
973static int ublk_ch_release(struct inode *inode, struct file *filp)
974{
975 struct ublk_device *ub = filp->private_data;
976
Christoph Hellwigfa362042022-07-21 15:09:12 +0200977 clear_bit(UB_STATE_OPEN, &ub->state);
Ming Lei71f28f32022-07-13 22:07:10 +0800978 return 0;
979}
980
981/* map pre-allocated per-queue cmd buffer to ublksrv daemon */
982static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
983{
984 struct ublk_device *ub = filp->private_data;
985 size_t sz = vma->vm_end - vma->vm_start;
986 unsigned max_sz = UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc);
987 unsigned long pfn, end, phys_off = vma->vm_pgoff << PAGE_SHIFT;
988 int q_id, ret = 0;
989
Ming Leie94eb452022-07-21 23:31:17 +0800990 spin_lock(&ub->mm_lock);
Ming Lei71f28f32022-07-13 22:07:10 +0800991 if (!ub->mm)
992 ub->mm = current->mm;
993 if (current->mm != ub->mm)
994 ret = -EINVAL;
Ming Leie94eb452022-07-21 23:31:17 +0800995 spin_unlock(&ub->mm_lock);
Ming Lei71f28f32022-07-13 22:07:10 +0800996
997 if (ret)
998 return ret;
999
1000 if (vma->vm_flags & VM_WRITE)
1001 return -EPERM;
1002
1003 end = UBLKSRV_CMD_BUF_OFFSET + ub->dev_info.nr_hw_queues * max_sz;
1004 if (phys_off < UBLKSRV_CMD_BUF_OFFSET || phys_off >= end)
1005 return -EINVAL;
1006
1007 q_id = (phys_off - UBLKSRV_CMD_BUF_OFFSET) / max_sz;
1008 pr_devel("%s: qid %d, pid %d, addr %lx pg_off %lx sz %lu\n",
1009 __func__, q_id, current->pid, vma->vm_start,
1010 phys_off, (unsigned long)sz);
1011
1012 if (sz != ublk_queue_cmd_buf_size(ub, q_id))
1013 return -EINVAL;
1014
1015 pfn = virt_to_phys(ublk_queue_cmd_buf(ub, q_id)) >> PAGE_SHIFT;
1016 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
1017}
1018
1019static void ublk_commit_completion(struct ublk_device *ub,
1020 struct ublksrv_io_cmd *ub_cmd)
1021{
1022 u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
1023 struct ublk_queue *ubq = ublk_get_queue(ub, qid);
1024 struct ublk_io *io = &ubq->ios[tag];
1025 struct request *req;
1026
1027 /* now this cmd slot is owned by nbd driver */
1028 io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
1029 io->res = ub_cmd->result;
1030
1031 /* find the io request and complete */
1032 req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
1033
1034 if (req && likely(!blk_should_fake_timeout(req->q)))
1035 ublk_complete_rq(req);
1036}
1037
1038/*
1039 * When ->ubq_daemon is exiting, either new request is ended immediately,
1040 * or any queued io command is drained, so it is safe to abort queue
1041 * lockless
1042 */
1043static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
1044{
1045 int i;
1046
1047 if (!ublk_get_device(ub))
1048 return;
1049
1050 for (i = 0; i < ubq->q_depth; i++) {
1051 struct ublk_io *io = &ubq->ios[i];
1052
1053 if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
1054 struct request *rq;
1055
1056 /*
1057 * Either we fail the request or ublk_rq_task_work_fn
1058 * will do it
1059 */
1060 rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
1061 if (rq)
ZiyangZhanga0d41dc2022-09-23 23:39:17 +08001062 __ublk_fail_req(ubq, io, rq);
Ming Lei71f28f32022-07-13 22:07:10 +08001063 }
1064 }
1065 ublk_put_device(ub);
1066}
1067
1068static void ublk_daemon_monitor_work(struct work_struct *work)
1069{
1070 struct ublk_device *ub =
1071 container_of(work, struct ublk_device, monitor_work.work);
1072 int i;
1073
1074 for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
1075 struct ublk_queue *ubq = ublk_get_queue(ub, i);
1076
1077 if (ubq_daemon_is_dying(ubq)) {
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001078 if (ublk_queue_can_use_recovery(ubq))
1079 schedule_work(&ub->quiesce_work);
1080 else
1081 schedule_work(&ub->stop_work);
Ming Lei71f28f32022-07-13 22:07:10 +08001082
1083 /* abort queue is for making forward progress */
1084 ublk_abort_queue(ub, ubq);
1085 }
1086 }
1087
1088 /*
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001089 * We can't schedule monitor work after ub's state is not UBLK_S_DEV_LIVE.
1090 * after ublk_remove() or __ublk_quiesce_dev() is started.
Ming Lei71f28f32022-07-13 22:07:10 +08001091 *
1092 * No need ub->mutex, monitor work are canceled after state is marked
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001093 * as not LIVE, so new state is observed reliably.
Ming Lei71f28f32022-07-13 22:07:10 +08001094 */
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001095 if (ub->dev_info.state == UBLK_S_DEV_LIVE)
Ming Lei71f28f32022-07-13 22:07:10 +08001096 schedule_delayed_work(&ub->monitor_work,
1097 UBLK_DAEMON_MONITOR_PERIOD);
1098}
1099
Ming Leia8ce5f52022-07-30 17:27:47 +08001100static inline bool ublk_queue_ready(struct ublk_queue *ubq)
1101{
1102 return ubq->nr_io_ready == ubq->q_depth;
1103}
1104
Ming Lei71f28f32022-07-13 22:07:10 +08001105static void ublk_cancel_queue(struct ublk_queue *ubq)
1106{
1107 int i;
1108
Ming Leia8ce5f52022-07-30 17:27:47 +08001109 if (!ublk_queue_ready(ubq))
1110 return;
1111
Ming Lei71f28f32022-07-13 22:07:10 +08001112 for (i = 0; i < ubq->q_depth; i++) {
1113 struct ublk_io *io = &ubq->ios[i];
1114
1115 if (io->flags & UBLK_IO_FLAG_ACTIVE)
Jens Axboe9d2789a2023-03-20 20:01:25 -06001116 io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0,
1117 IO_URING_F_UNLOCKED);
Ming Lei71f28f32022-07-13 22:07:10 +08001118 }
Ming Leia8ce5f52022-07-30 17:27:47 +08001119
1120 /* all io commands are canceled */
1121 ubq->nr_io_ready = 0;
Ming Lei71f28f32022-07-13 22:07:10 +08001122}
1123
1124/* Cancel all pending commands, must be called after del_gendisk() returns */
1125static void ublk_cancel_dev(struct ublk_device *ub)
1126{
1127 int i;
1128
1129 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1130 ublk_cancel_queue(ublk_get_queue(ub, i));
1131}
1132
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001133static bool ublk_check_inflight_rq(struct request *rq, void *data)
Ming Lei71f28f32022-07-13 22:07:10 +08001134{
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001135 bool *idle = data;
1136
1137 if (blk_mq_request_started(rq)) {
1138 *idle = false;
1139 return false;
1140 }
1141 return true;
1142}
1143
1144static void ublk_wait_tagset_rqs_idle(struct ublk_device *ub)
1145{
1146 bool idle;
1147
1148 WARN_ON_ONCE(!blk_queue_quiesced(ub->ub_disk->queue));
1149 while (true) {
1150 idle = true;
1151 blk_mq_tagset_busy_iter(&ub->tag_set,
1152 ublk_check_inflight_rq, &idle);
1153 if (idle)
1154 break;
1155 msleep(UBLK_REQUEUE_DELAY_MS);
1156 }
1157}
1158
1159static void __ublk_quiesce_dev(struct ublk_device *ub)
1160{
1161 pr_devel("%s: quiesce ub: dev_id %d state %s\n",
1162 __func__, ub->dev_info.dev_id,
1163 ub->dev_info.state == UBLK_S_DEV_LIVE ?
1164 "LIVE" : "QUIESCED");
1165 blk_mq_quiesce_queue(ub->ub_disk->queue);
1166 ublk_wait_tagset_rqs_idle(ub);
1167 ub->dev_info.state = UBLK_S_DEV_QUIESCED;
1168 ublk_cancel_dev(ub);
1169 /* we are going to release task_struct of ubq_daemon and resets
1170 * ->ubq_daemon to NULL. So in monitor_work, check on ubq_daemon causes UAF.
1171 * Besides, monitor_work is not necessary in QUIESCED state since we have
1172 * already scheduled quiesce_work and quiesced all ubqs.
1173 *
1174 * Do not let monitor_work schedule itself if state it QUIESCED. And we cancel
1175 * it here and re-schedule it in END_USER_RECOVERY to avoid UAF.
1176 */
1177 cancel_delayed_work_sync(&ub->monitor_work);
1178}
1179
1180static void ublk_quiesce_work_fn(struct work_struct *work)
1181{
1182 struct ublk_device *ub =
1183 container_of(work, struct ublk_device, quiesce_work);
1184
Ming Lei71f28f32022-07-13 22:07:10 +08001185 mutex_lock(&ub->mutex);
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001186 if (ub->dev_info.state != UBLK_S_DEV_LIVE)
Ming Lei71f28f32022-07-13 22:07:10 +08001187 goto unlock;
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001188 __ublk_quiesce_dev(ub);
1189 unlock:
1190 mutex_unlock(&ub->mutex);
1191}
Ming Lei71f28f32022-07-13 22:07:10 +08001192
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001193static void ublk_unquiesce_dev(struct ublk_device *ub)
1194{
1195 int i;
1196
1197 pr_devel("%s: unquiesce ub: dev_id %d state %s\n",
1198 __func__, ub->dev_info.dev_id,
1199 ub->dev_info.state == UBLK_S_DEV_LIVE ?
1200 "LIVE" : "QUIESCED");
1201 /* quiesce_work has run. We let requeued rqs be aborted
1202 * before running fallback_wq. "force_abort" must be seen
1203 * after request queue is unqiuesced. Then del_gendisk()
1204 * can move on.
1205 */
1206 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1207 ublk_get_queue(ub, i)->force_abort = true;
1208
1209 blk_mq_unquiesce_queue(ub->ub_disk->queue);
1210 /* We may have requeued some rqs in ublk_quiesce_queue() */
1211 blk_mq_kick_requeue_list(ub->ub_disk->queue);
1212}
1213
1214static void ublk_stop_dev(struct ublk_device *ub)
1215{
1216 mutex_lock(&ub->mutex);
1217 if (ub->dev_info.state == UBLK_S_DEV_DEAD)
1218 goto unlock;
1219 if (ublk_can_use_recovery(ub)) {
1220 if (ub->dev_info.state == UBLK_S_DEV_LIVE)
1221 __ublk_quiesce_dev(ub);
1222 ublk_unquiesce_dev(ub);
1223 }
Ming Lei71f28f32022-07-13 22:07:10 +08001224 del_gendisk(ub->ub_disk);
1225 ub->dev_info.state = UBLK_S_DEV_DEAD;
1226 ub->dev_info.ublksrv_pid = -1;
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001227 put_disk(ub->ub_disk);
1228 ub->ub_disk = NULL;
Ming Lei71f28f32022-07-13 22:07:10 +08001229 unlock:
Ming Leia8ce5f52022-07-30 17:27:47 +08001230 ublk_cancel_dev(ub);
Ming Lei71f28f32022-07-13 22:07:10 +08001231 mutex_unlock(&ub->mutex);
1232 cancel_delayed_work_sync(&ub->monitor_work);
1233}
1234
Ming Lei71f28f32022-07-13 22:07:10 +08001235/* device can only be started after all IOs are ready */
1236static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq)
1237{
1238 mutex_lock(&ub->mutex);
1239 ubq->nr_io_ready++;
1240 if (ublk_queue_ready(ubq)) {
1241 ubq->ubq_daemon = current;
1242 get_task_struct(ubq->ubq_daemon);
1243 ub->nr_queues_ready++;
Ming Lei73a166d2023-01-06 12:17:07 +08001244
1245 if (capable(CAP_SYS_ADMIN))
1246 ub->nr_privileged_daemon++;
Ming Lei71f28f32022-07-13 22:07:10 +08001247 }
1248 if (ub->nr_queues_ready == ub->dev_info.nr_hw_queues)
1249 complete_all(&ub->completion);
1250 mutex_unlock(&ub->mutex);
1251}
1252
ZiyangZhangc86019f2022-07-28 20:39:16 +08001253static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
Ming Leifee32f32022-10-29 09:04:32 +08001254 int tag)
ZiyangZhangc86019f2022-07-28 20:39:16 +08001255{
1256 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1257 struct request *req = blk_mq_tag_to_rq(ub->tag_set.tags[q_id], tag);
1258
Ming Lei7d4a9312022-11-21 23:56:45 +08001259 ublk_queue_cmd(ubq, req);
ZiyangZhangc86019f2022-07-28 20:39:16 +08001260}
1261
Ming Lei2d786e62023-04-18 21:18:10 +08001262static inline int ublk_check_cmd_op(u32 cmd_op)
1263{
1264 u32 ioc_type = _IOC_TYPE(cmd_op);
1265
1266 if (IS_ENABLED(CONFIG_BLKDEV_UBLK_LEGACY_OPCODES) && ioc_type != 'u')
1267 return -EOPNOTSUPP;
1268
1269 if (ioc_type != 'u' && ioc_type != 0)
1270 return -EOPNOTSUPP;
1271
1272 return 0;
1273}
1274
Jens Axboe8c68ae32023-04-05 20:00:46 -06001275static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
1276 unsigned int issue_flags,
1277 struct ublksrv_io_cmd *ub_cmd)
Ming Lei71f28f32022-07-13 22:07:10 +08001278{
Ming Lei71f28f32022-07-13 22:07:10 +08001279 struct ublk_device *ub = cmd->file->private_data;
1280 struct ublk_queue *ubq;
1281 struct ublk_io *io;
1282 u32 cmd_op = cmd->cmd_op;
1283 unsigned tag = ub_cmd->tag;
1284 int ret = -EINVAL;
Liu Xiaodong2f1e07d2023-02-10 09:13:56 -05001285 struct request *req;
Ming Lei71f28f32022-07-13 22:07:10 +08001286
1287 pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
1288 __func__, cmd->cmd_op, ub_cmd->q_id, tag,
1289 ub_cmd->result);
1290
Ming Lei71f28f32022-07-13 22:07:10 +08001291 if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues)
1292 goto out;
1293
1294 ubq = ublk_get_queue(ub, ub_cmd->q_id);
1295 if (!ubq || ub_cmd->q_id != ubq->q_id)
1296 goto out;
1297
1298 if (ubq->ubq_daemon && ubq->ubq_daemon != current)
1299 goto out;
1300
1301 if (tag >= ubq->q_depth)
1302 goto out;
1303
1304 io = &ubq->ios[tag];
1305
1306 /* there is pending io cmd, something must be wrong */
1307 if (io->flags & UBLK_IO_FLAG_ACTIVE) {
1308 ret = -EBUSY;
1309 goto out;
1310 }
1311
ZiyangZhangc86019f2022-07-28 20:39:16 +08001312 /*
1313 * ensure that the user issues UBLK_IO_NEED_GET_DATA
1314 * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
1315 */
1316 if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA))
Ming Lei2d786e62023-04-18 21:18:10 +08001317 ^ (_IOC_NR(cmd_op) == UBLK_IO_NEED_GET_DATA))
ZiyangZhangc86019f2022-07-28 20:39:16 +08001318 goto out;
1319
Ming Lei2d786e62023-04-18 21:18:10 +08001320 ret = ublk_check_cmd_op(cmd_op);
1321 if (ret)
1322 goto out;
1323
Ming Lei7c756612023-04-20 17:11:04 +08001324 ret = -EINVAL;
Ming Lei2d786e62023-04-18 21:18:10 +08001325 switch (_IOC_NR(cmd_op)) {
Ming Lei71f28f32022-07-13 22:07:10 +08001326 case UBLK_IO_FETCH_REQ:
1327 /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */
1328 if (ublk_queue_ready(ubq)) {
1329 ret = -EBUSY;
1330 goto out;
1331 }
1332 /*
1333 * The io is being handled by server, so COMMIT_RQ is expected
1334 * instead of FETCH_REQ
1335 */
1336 if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)
1337 goto out;
Liu Xiaodong2f1e07d2023-02-10 09:13:56 -05001338 /* FETCH_RQ has to provide IO buffer if NEED GET DATA is not enabled */
1339 if (!ub_cmd->addr && !ublk_need_get_data(ubq))
Ming Lei71f28f32022-07-13 22:07:10 +08001340 goto out;
1341 io->cmd = cmd;
1342 io->flags |= UBLK_IO_FLAG_ACTIVE;
1343 io->addr = ub_cmd->addr;
1344
1345 ublk_mark_io_ready(ub, ubq);
1346 break;
1347 case UBLK_IO_COMMIT_AND_FETCH_REQ:
Liu Xiaodong2f1e07d2023-02-10 09:13:56 -05001348 req = blk_mq_tag_to_rq(ub->tag_set.tags[ub_cmd->q_id], tag);
1349 /*
1350 * COMMIT_AND_FETCH_REQ has to provide IO buffer if NEED GET DATA is
1351 * not enabled or it is Read IO.
1352 */
1353 if (!ub_cmd->addr && (!ublk_need_get_data(ubq) || req_op(req) == REQ_OP_READ))
Ming Lei71f28f32022-07-13 22:07:10 +08001354 goto out;
1355 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1356 goto out;
1357 io->addr = ub_cmd->addr;
1358 io->flags |= UBLK_IO_FLAG_ACTIVE;
1359 io->cmd = cmd;
1360 ublk_commit_completion(ub, ub_cmd);
1361 break;
ZiyangZhangc86019f2022-07-28 20:39:16 +08001362 case UBLK_IO_NEED_GET_DATA:
1363 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1364 goto out;
1365 io->addr = ub_cmd->addr;
1366 io->cmd = cmd;
1367 io->flags |= UBLK_IO_FLAG_ACTIVE;
Ming Leifee32f32022-10-29 09:04:32 +08001368 ublk_handle_need_get_data(ub, ub_cmd->q_id, ub_cmd->tag);
ZiyangZhangc86019f2022-07-28 20:39:16 +08001369 break;
Ming Lei71f28f32022-07-13 22:07:10 +08001370 default:
1371 goto out;
1372 }
1373 return -EIOCBQUEUED;
1374
1375 out:
Jens Axboe9d2789a2023-03-20 20:01:25 -06001376 io_uring_cmd_done(cmd, ret, 0, issue_flags);
Ming Lei71f28f32022-07-13 22:07:10 +08001377 pr_devel("%s: complete: cmd op %d, tag %d ret %x io_flags %x\n",
1378 __func__, cmd_op, tag, ret, io->flags);
1379 return -EIOCBQUEUED;
1380}
1381
Jens Axboe8c68ae32023-04-05 20:00:46 -06001382static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
1383{
1384 struct ublksrv_io_cmd *ub_src = (struct ublksrv_io_cmd *) cmd->cmd;
1385 struct ublksrv_io_cmd ub_cmd;
1386
1387 /*
1388 * Not necessary for async retry, but let's keep it simple and always
1389 * copy the values to avoid any potential reuse.
1390 */
1391 ub_cmd.q_id = READ_ONCE(ub_src->q_id);
1392 ub_cmd.tag = READ_ONCE(ub_src->tag);
1393 ub_cmd.result = READ_ONCE(ub_src->result);
1394 ub_cmd.addr = READ_ONCE(ub_src->addr);
1395
1396 return __ublk_ch_uring_cmd(cmd, issue_flags, &ub_cmd);
1397}
1398
Ming Lei71f28f32022-07-13 22:07:10 +08001399static const struct file_operations ublk_ch_fops = {
1400 .owner = THIS_MODULE,
1401 .open = ublk_ch_open,
1402 .release = ublk_ch_release,
1403 .llseek = no_llseek,
1404 .uring_cmd = ublk_ch_uring_cmd,
1405 .mmap = ublk_ch_mmap,
1406};
1407
1408static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
1409{
1410 int size = ublk_queue_cmd_buf_size(ub, q_id);
1411 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1412
1413 if (ubq->ubq_daemon)
1414 put_task_struct(ubq->ubq_daemon);
1415 if (ubq->io_cmd_buf)
1416 free_pages((unsigned long)ubq->io_cmd_buf, get_order(size));
1417}
1418
1419static int ublk_init_queue(struct ublk_device *ub, int q_id)
1420{
1421 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1422 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
1423 void *ptr;
1424 int size;
1425
Ming Lei6d8c5af2022-07-22 18:38:17 +08001426 ubq->flags = ub->dev_info.flags;
Ming Lei71f28f32022-07-13 22:07:10 +08001427 ubq->q_id = q_id;
1428 ubq->q_depth = ub->dev_info.queue_depth;
1429 size = ublk_queue_cmd_buf_size(ub, q_id);
1430
1431 ptr = (void *) __get_free_pages(gfp_flags, get_order(size));
1432 if (!ptr)
1433 return -ENOMEM;
1434
1435 ubq->io_cmd_buf = ptr;
1436 ubq->dev = ub;
1437 return 0;
1438}
1439
1440static void ublk_deinit_queues(struct ublk_device *ub)
1441{
1442 int nr_queues = ub->dev_info.nr_hw_queues;
1443 int i;
1444
1445 if (!ub->__queues)
1446 return;
1447
1448 for (i = 0; i < nr_queues; i++)
1449 ublk_deinit_queue(ub, i);
1450 kfree(ub->__queues);
1451}
1452
1453static int ublk_init_queues(struct ublk_device *ub)
1454{
1455 int nr_queues = ub->dev_info.nr_hw_queues;
1456 int depth = ub->dev_info.queue_depth;
1457 int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io);
1458 int i, ret = -ENOMEM;
1459
1460 ub->queue_size = ubq_size;
1461 ub->__queues = kcalloc(nr_queues, ubq_size, GFP_KERNEL);
1462 if (!ub->__queues)
1463 return ret;
1464
1465 for (i = 0; i < nr_queues; i++) {
1466 if (ublk_init_queue(ub, i))
1467 goto fail;
1468 }
1469
1470 init_completion(&ub->completion);
1471 return 0;
1472
1473 fail:
1474 ublk_deinit_queues(ub);
1475 return ret;
1476}
1477
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001478static int ublk_alloc_dev_number(struct ublk_device *ub, int idx)
Ming Lei71f28f32022-07-13 22:07:10 +08001479{
1480 int i = idx;
1481 int err;
1482
1483 spin_lock(&ublk_idr_lock);
1484 /* allocate id, if @id >= 0, we're requesting that specific id */
1485 if (i >= 0) {
1486 err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT);
1487 if (err == -ENOSPC)
1488 err = -EEXIST;
1489 } else {
1490 err = idr_alloc(&ublk_index_idr, ub, 0, 0, GFP_NOWAIT);
1491 }
1492 spin_unlock(&ublk_idr_lock);
1493
1494 if (err >= 0)
1495 ub->ub_number = err;
1496
1497 return err;
1498}
1499
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001500static void ublk_free_dev_number(struct ublk_device *ub)
Ming Lei71f28f32022-07-13 22:07:10 +08001501{
1502 spin_lock(&ublk_idr_lock);
1503 idr_remove(&ublk_index_idr, ub->ub_number);
1504 wake_up_all(&ublk_idr_wq);
1505 spin_unlock(&ublk_idr_lock);
Ming Lei71f28f32022-07-13 22:07:10 +08001506}
1507
1508static void ublk_cdev_rel(struct device *dev)
1509{
1510 struct ublk_device *ub = container_of(dev, struct ublk_device, cdev_dev);
1511
Ming Lei71f28f32022-07-13 22:07:10 +08001512 blk_mq_free_tag_set(&ub->tag_set);
Ming Lei71f28f32022-07-13 22:07:10 +08001513 ublk_deinit_queues(ub);
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001514 ublk_free_dev_number(ub);
1515 mutex_destroy(&ub->mutex);
1516 kfree(ub);
Ming Lei71f28f32022-07-13 22:07:10 +08001517}
1518
1519static int ublk_add_chdev(struct ublk_device *ub)
1520{
1521 struct device *dev = &ub->cdev_dev;
1522 int minor = ub->ub_number;
1523 int ret;
1524
1525 dev->parent = ublk_misc.this_device;
1526 dev->devt = MKDEV(MAJOR(ublk_chr_devt), minor);
1527 dev->class = ublk_chr_class;
1528 dev->release = ublk_cdev_rel;
1529 device_initialize(dev);
1530
1531 ret = dev_set_name(dev, "ublkc%d", minor);
1532 if (ret)
1533 goto fail;
1534
1535 cdev_init(&ub->cdev, &ublk_ch_fops);
1536 ret = cdev_device_add(&ub->cdev, dev);
1537 if (ret)
1538 goto fail;
Ming Lei403ebc82023-01-06 12:17:10 +08001539
1540 ublks_added++;
Ming Lei71f28f32022-07-13 22:07:10 +08001541 return 0;
1542 fail:
1543 put_device(dev);
1544 return ret;
1545}
1546
1547static void ublk_stop_work_fn(struct work_struct *work)
1548{
1549 struct ublk_device *ub =
1550 container_of(work, struct ublk_device, stop_work);
1551
1552 ublk_stop_dev(ub);
1553}
1554
Ming Lei4bf9cbf2022-07-30 17:27:50 +08001555/* align max io buffer size with PAGE_SIZE */
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001556static void ublk_align_max_io_size(struct ublk_device *ub)
Ming Lei71f28f32022-07-13 22:07:10 +08001557{
Ming Lei4bf9cbf2022-07-30 17:27:50 +08001558 unsigned int max_io_bytes = ub->dev_info.max_io_buf_bytes;
Ming Lei71f28f32022-07-13 22:07:10 +08001559
Ming Lei4bf9cbf2022-07-30 17:27:50 +08001560 ub->dev_info.max_io_buf_bytes =
1561 round_down(max_io_bytes, PAGE_SIZE);
Ming Lei71f28f32022-07-13 22:07:10 +08001562}
1563
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001564static int ublk_add_tag_set(struct ublk_device *ub)
Ming Lei71f28f32022-07-13 22:07:10 +08001565{
Ming Lei71f28f32022-07-13 22:07:10 +08001566 ub->tag_set.ops = &ublk_mq_ops;
1567 ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues;
1568 ub->tag_set.queue_depth = ub->dev_info.queue_depth;
1569 ub->tag_set.numa_node = NUMA_NO_NODE;
Ming Lei0edb3692022-07-13 22:07:11 +08001570 ub->tag_set.cmd_size = sizeof(struct ublk_rq_data);
Ming Lei71f28f32022-07-13 22:07:10 +08001571 ub->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1572 ub->tag_set.driver_data = ub;
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001573 return blk_mq_alloc_tag_set(&ub->tag_set);
Ming Lei71f28f32022-07-13 22:07:10 +08001574}
1575
1576static void ublk_remove(struct ublk_device *ub)
1577{
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001578 ublk_stop_dev(ub);
1579 cancel_work_sync(&ub->stop_work);
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001580 cancel_work_sync(&ub->quiesce_work);
Ming Lei71f28f32022-07-13 22:07:10 +08001581 cdev_device_del(&ub->cdev, &ub->cdev_dev);
1582 put_device(&ub->cdev_dev);
Ming Lei403ebc82023-01-06 12:17:10 +08001583 ublks_added--;
Ming Lei71f28f32022-07-13 22:07:10 +08001584}
1585
1586static struct ublk_device *ublk_get_device_from_id(int idx)
1587{
1588 struct ublk_device *ub = NULL;
1589
1590 if (idx < 0)
1591 return NULL;
1592
1593 spin_lock(&ublk_idr_lock);
1594 ub = idr_find(&ublk_index_idr, idx);
1595 if (ub)
1596 ub = ublk_get_device(ub);
1597 spin_unlock(&ublk_idr_lock);
1598
1599 return ub;
1600}
1601
Ming Leibfbcef02023-01-06 12:17:08 +08001602static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)
Ming Lei71f28f32022-07-13 22:07:10 +08001603{
1604 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
Ming Lei71f28f32022-07-13 22:07:10 +08001605 int ublksrv_pid = (int)header->data[0];
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001606 struct gendisk *disk;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001607 int ret = -EINVAL;
Ming Lei71f28f32022-07-13 22:07:10 +08001608
1609 if (ublksrv_pid <= 0)
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001610 return -EINVAL;
1611
Ming Lei71f28f32022-07-13 22:07:10 +08001612 wait_for_completion_interruptible(&ub->completion);
1613
1614 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
1615
1616 mutex_lock(&ub->mutex);
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001617 if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
1618 test_bit(UB_STATE_USED, &ub->state)) {
Ming Lei71f28f32022-07-13 22:07:10 +08001619 ret = -EEXIST;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001620 goto out_unlock;
Ming Lei71f28f32022-07-13 22:07:10 +08001621 }
Ming Lei71f28f32022-07-13 22:07:10 +08001622
Ziyang Zhang1972d032023-02-07 15:08:39 +08001623 disk = blk_mq_alloc_disk(&ub->tag_set, NULL);
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001624 if (IS_ERR(disk)) {
1625 ret = PTR_ERR(disk);
1626 goto out_unlock;
1627 }
1628 sprintf(disk->disk_name, "ublkb%d", ub->ub_number);
1629 disk->fops = &ub_fops;
1630 disk->private_data = ub;
1631
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001632 ub->dev_info.ublksrv_pid = ublksrv_pid;
1633 ub->ub_disk = disk;
Ming Lei0aa73172022-07-30 17:27:49 +08001634
1635 ret = ublk_apply_params(ub);
1636 if (ret)
1637 goto out_put_disk;
1638
Ming Lei73a166d2023-01-06 12:17:07 +08001639 /* don't probe partitions if any one ubq daemon is un-trusted */
1640 if (ub->nr_privileged_daemon != ub->nr_queues_ready)
1641 set_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
1642
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001643 get_device(&ub->cdev_dev);
Ming Lei4985e7b2023-03-18 22:12:31 +08001644 ub->dev_info.state = UBLK_S_DEV_LIVE;
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001645 ret = add_disk(disk);
1646 if (ret) {
Ming Lei93d71ec2022-07-30 17:27:48 +08001647 /*
1648 * Has to drop the reference since ->free_disk won't be
1649 * called in case of add_disk failure.
1650 */
Ming Lei4985e7b2023-03-18 22:12:31 +08001651 ub->dev_info.state = UBLK_S_DEV_DEAD;
Ming Lei93d71ec2022-07-30 17:27:48 +08001652 ublk_put_device(ub);
Ming Lei0aa73172022-07-30 17:27:49 +08001653 goto out_put_disk;
Christoph Hellwig6d9e6df2022-07-21 15:09:16 +02001654 }
1655 set_bit(UB_STATE_USED, &ub->state);
Ming Lei0aa73172022-07-30 17:27:49 +08001656out_put_disk:
1657 if (ret)
1658 put_disk(disk);
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001659out_unlock:
1660 mutex_unlock(&ub->mutex);
Ming Lei71f28f32022-07-13 22:07:10 +08001661 return ret;
1662}
1663
Ming Leibfbcef02023-01-06 12:17:08 +08001664static int ublk_ctrl_get_queue_affinity(struct ublk_device *ub,
1665 struct io_uring_cmd *cmd)
Ming Lei71f28f32022-07-13 22:07:10 +08001666{
1667 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1668 void __user *argp = (void __user *)(unsigned long)header->addr;
Christoph Hellwigc50061f2022-07-21 15:09:15 +02001669 cpumask_var_t cpumask;
Ming Lei71f28f32022-07-13 22:07:10 +08001670 unsigned long queue;
1671 unsigned int retlen;
Christoph Hellwigc50061f2022-07-21 15:09:15 +02001672 unsigned int i;
Ming Leibfbcef02023-01-06 12:17:08 +08001673 int ret;
1674
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001675 if (header->len * BITS_PER_BYTE < nr_cpu_ids)
1676 return -EINVAL;
1677 if (header->len & (sizeof(unsigned long)-1))
1678 return -EINVAL;
1679 if (!header->addr)
1680 return -EINVAL;
Ming Lei71f28f32022-07-13 22:07:10 +08001681
Ming Lei71f28f32022-07-13 22:07:10 +08001682 queue = header->data[0];
1683 if (queue >= ub->dev_info.nr_hw_queues)
Ming Leibfbcef02023-01-06 12:17:08 +08001684 return -EINVAL;
Ming Lei71f28f32022-07-13 22:07:10 +08001685
Christoph Hellwigc50061f2022-07-21 15:09:15 +02001686 if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
Ming Leibfbcef02023-01-06 12:17:08 +08001687 return -ENOMEM;
Christoph Hellwigc50061f2022-07-21 15:09:15 +02001688
1689 for_each_possible_cpu(i) {
1690 if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue)
1691 cpumask_set_cpu(i, cpumask);
1692 }
1693
1694 ret = -EFAULT;
Ming Lei71f28f32022-07-13 22:07:10 +08001695 retlen = min_t(unsigned short, header->len, cpumask_size());
Christoph Hellwigc50061f2022-07-21 15:09:15 +02001696 if (copy_to_user(argp, cpumask, retlen))
1697 goto out_free_cpumask;
1698 if (retlen != header->len &&
1699 clear_user(argp + retlen, header->len - retlen))
1700 goto out_free_cpumask;
1701
Ming Lei71f28f32022-07-13 22:07:10 +08001702 ret = 0;
Christoph Hellwigc50061f2022-07-21 15:09:15 +02001703out_free_cpumask:
1704 free_cpumask_var(cpumask);
Ming Lei71f28f32022-07-13 22:07:10 +08001705 return ret;
1706}
1707
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001708static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info *info)
Ming Lei71f28f32022-07-13 22:07:10 +08001709{
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001710 pr_devel("%s: dev id %d flags %llx\n", __func__,
Ming Lei6d8c5af2022-07-22 18:38:17 +08001711 info->dev_id, info->flags);
Ming Lei4bf9cbf2022-07-30 17:27:50 +08001712 pr_devel("\t nr_hw_queues %d queue_depth %d\n",
1713 info->nr_hw_queues, info->queue_depth);
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001714}
1715
1716static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
1717{
1718 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1719 void __user *argp = (void __user *)(unsigned long)header->addr;
1720 struct ublksrv_ctrl_dev_info info;
Ming Lei71f28f32022-07-13 22:07:10 +08001721 struct ublk_device *ub;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001722 int ret = -EINVAL;
1723
1724 if (header->len < sizeof(info) || !header->addr)
1725 return -EINVAL;
1726 if (header->queue_id != (u16)-1) {
1727 pr_warn("%s: queue_id is wrong %x\n",
1728 __func__, header->queue_id);
1729 return -EINVAL;
1730 }
Ming Lei4093cb52023-01-06 12:17:11 +08001731
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001732 if (copy_from_user(&info, argp, sizeof(info)))
1733 return -EFAULT;
Ming Lei4093cb52023-01-06 12:17:11 +08001734
1735 if (capable(CAP_SYS_ADMIN))
1736 info.flags &= ~UBLK_F_UNPRIVILEGED_DEV;
1737 else if (!(info.flags & UBLK_F_UNPRIVILEGED_DEV))
1738 return -EPERM;
1739
1740 /* the created device is always owned by current user */
Ming Lei48a90512023-01-31 12:04:46 +08001741 ublk_store_owner_uid_gid(&info.owner_uid, &info.owner_gid);
Ming Lei4093cb52023-01-06 12:17:11 +08001742
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001743 if (header->dev_id != info.dev_id) {
1744 pr_warn("%s: dev id not match %u %u\n",
1745 __func__, header->dev_id, info.dev_id);
1746 return -EINVAL;
1747 }
Ming Lei71f28f32022-07-13 22:07:10 +08001748
Ming Lei4093cb52023-01-06 12:17:11 +08001749 ublk_dump_dev_info(&info);
1750
Ming Lei71f28f32022-07-13 22:07:10 +08001751 ret = mutex_lock_killable(&ublk_ctl_mutex);
1752 if (ret)
1753 return ret;
1754
Ming Lei403ebc82023-01-06 12:17:10 +08001755 ret = -EACCES;
1756 if (ublks_added >= ublks_max)
1757 goto out_unlock;
1758
Christoph Hellwigcfee7e42022-07-21 15:09:14 +02001759 ret = -ENOMEM;
1760 ub = kzalloc(sizeof(*ub), GFP_KERNEL);
1761 if (!ub)
1762 goto out_unlock;
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001763 mutex_init(&ub->mutex);
1764 spin_lock_init(&ub->mm_lock);
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001765 INIT_WORK(&ub->quiesce_work, ublk_quiesce_work_fn);
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001766 INIT_WORK(&ub->stop_work, ublk_stop_work_fn);
1767 INIT_DELAYED_WORK(&ub->monitor_work, ublk_daemon_monitor_work);
Christoph Hellwigcfee7e42022-07-21 15:09:14 +02001768
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001769 ret = ublk_alloc_dev_number(ub, header->dev_id);
1770 if (ret < 0)
1771 goto out_free_ub;
Ming Lei71f28f32022-07-13 22:07:10 +08001772
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001773 memcpy(&ub->dev_info, &info, sizeof(info));
1774
1775 /* update device id */
1776 ub->dev_info.dev_id = ub->ub_number;
1777
Ming Lei6d8c5af2022-07-22 18:38:17 +08001778 /*
1779 * 64bit flags will be copied back to userspace as feature
1780 * negotiation result, so have to clear flags which driver
1781 * doesn't support yet, then userspace can get correct flags
1782 * (features) to handle.
1783 */
1784 ub->dev_info.flags &= UBLK_F_ALL;
1785
Ming Lei224e8582022-10-29 09:04:29 +08001786 if (!IS_BUILTIN(CONFIG_BLK_DEV_UBLK))
1787 ub->dev_info.flags |= UBLK_F_URING_CMD_COMP_IN_TASK;
1788
Ming Lei2d786e62023-04-18 21:18:10 +08001789 ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE;
1790
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001791 /* We are not ready to support zero copy */
Ming Lei6d8c5af2022-07-22 18:38:17 +08001792 ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001793
Christoph Hellwigfa9482e2022-07-22 18:38:16 +08001794 ub->dev_info.nr_hw_queues = min_t(unsigned int,
1795 ub->dev_info.nr_hw_queues, nr_cpu_ids);
1796 ublk_align_max_io_size(ub);
1797
1798 ret = ublk_init_queues(ub);
1799 if (ret)
1800 goto out_free_dev_number;
1801
1802 ret = ublk_add_tag_set(ub);
1803 if (ret)
1804 goto out_deinit_queues;
1805
1806 ret = -EFAULT;
1807 if (copy_to_user(argp, &ub->dev_info, sizeof(info)))
1808 goto out_free_tag_set;
1809
1810 /*
1811 * Add the char dev so that ublksrv daemon can be setup.
1812 * ublk_add_chdev() will cleanup everything if it fails.
1813 */
1814 ret = ublk_add_chdev(ub);
1815 goto out_unlock;
1816
1817out_free_tag_set:
1818 blk_mq_free_tag_set(&ub->tag_set);
1819out_deinit_queues:
1820 ublk_deinit_queues(ub);
1821out_free_dev_number:
1822 ublk_free_dev_number(ub);
1823out_free_ub:
1824 mutex_destroy(&ub->mutex);
1825 kfree(ub);
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001826out_unlock:
1827 mutex_unlock(&ublk_ctl_mutex);
Ming Lei71f28f32022-07-13 22:07:10 +08001828 return ret;
1829}
1830
1831static inline bool ublk_idr_freed(int id)
1832{
1833 void *ptr;
1834
1835 spin_lock(&ublk_idr_lock);
1836 ptr = idr_find(&ublk_index_idr, id);
1837 spin_unlock(&ublk_idr_lock);
1838
1839 return ptr == NULL;
1840}
1841
Ming Leibfbcef02023-01-06 12:17:08 +08001842static int ublk_ctrl_del_dev(struct ublk_device **p_ub)
Ming Lei71f28f32022-07-13 22:07:10 +08001843{
Ming Leibfbcef02023-01-06 12:17:08 +08001844 struct ublk_device *ub = *p_ub;
1845 int idx = ub->ub_number;
Ming Lei71f28f32022-07-13 22:07:10 +08001846 int ret;
1847
1848 ret = mutex_lock_killable(&ublk_ctl_mutex);
1849 if (ret)
1850 return ret;
1851
Ming Lei0abe39d2023-02-07 23:07:00 +08001852 if (!test_bit(UB_STATE_DELETED, &ub->state)) {
Ming Lei71f28f32022-07-13 22:07:10 +08001853 ublk_remove(ub);
Ming Lei0abe39d2023-02-07 23:07:00 +08001854 set_bit(UB_STATE_DELETED, &ub->state);
Ming Lei71f28f32022-07-13 22:07:10 +08001855 }
1856
Ming Leibfbcef02023-01-06 12:17:08 +08001857 /* Mark the reference as consumed */
1858 *p_ub = NULL;
1859 ublk_put_device(ub);
Ming Lei0abe39d2023-02-07 23:07:00 +08001860 mutex_unlock(&ublk_ctl_mutex);
Ming Lei71f28f32022-07-13 22:07:10 +08001861
1862 /*
1863 * Wait until the idr is removed, then it can be reused after
1864 * DEL_DEV command is returned.
Ming Lei0abe39d2023-02-07 23:07:00 +08001865 *
1866 * If we returns because of user interrupt, future delete command
1867 * may come:
1868 *
1869 * - the device number isn't freed, this device won't or needn't
1870 * be deleted again, since UB_STATE_DELETED is set, and device
1871 * will be released after the last reference is dropped
1872 *
1873 * - the device number is freed already, we will not find this
1874 * device via ublk_get_device_from_id()
Ming Lei71f28f32022-07-13 22:07:10 +08001875 */
Ming Lei0abe39d2023-02-07 23:07:00 +08001876 wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx));
Ming Lei71f28f32022-07-13 22:07:10 +08001877
Ming Lei0abe39d2023-02-07 23:07:00 +08001878 return 0;
Ming Lei71f28f32022-07-13 22:07:10 +08001879}
1880
Ming Lei71f28f32022-07-13 22:07:10 +08001881static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
1882{
1883 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1884
1885 pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n",
1886 __func__, cmd->cmd_op, header->dev_id, header->queue_id,
1887 header->data[0], header->addr, header->len);
1888}
1889
Ming Leibfbcef02023-01-06 12:17:08 +08001890static int ublk_ctrl_stop_dev(struct ublk_device *ub)
Ming Lei71f28f32022-07-13 22:07:10 +08001891{
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001892 ublk_stop_dev(ub);
1893 cancel_work_sync(&ub->stop_work);
ZiyangZhangbbae8d12022-09-23 23:39:16 +08001894 cancel_work_sync(&ub->quiesce_work);
Ming Lei71f28f32022-07-13 22:07:10 +08001895
1896 return 0;
1897}
1898
Ming Leibfbcef02023-01-06 12:17:08 +08001899static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
1900 struct io_uring_cmd *cmd)
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001901{
1902 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1903 void __user *argp = (void __user *)(unsigned long)header->addr;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001904
1905 if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr)
1906 return -EINVAL;
1907
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001908 if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info)))
Ming Leibfbcef02023-01-06 12:17:08 +08001909 return -EFAULT;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001910
Ming Leibfbcef02023-01-06 12:17:08 +08001911 return 0;
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02001912}
1913
Ming Leiabb864d2023-01-06 12:17:09 +08001914/* TYPE_DEVT is readonly, so fill it up before returning to userspace */
1915static void ublk_ctrl_fill_params_devt(struct ublk_device *ub)
1916{
1917 ub->params.devt.char_major = MAJOR(ub->cdev_dev.devt);
1918 ub->params.devt.char_minor = MINOR(ub->cdev_dev.devt);
1919
1920 if (ub->ub_disk) {
1921 ub->params.devt.disk_major = MAJOR(disk_devt(ub->ub_disk));
1922 ub->params.devt.disk_minor = MINOR(disk_devt(ub->ub_disk));
1923 } else {
1924 ub->params.devt.disk_major = 0;
1925 ub->params.devt.disk_minor = 0;
1926 }
1927 ub->params.types |= UBLK_PARAM_TYPE_DEVT;
1928}
1929
Ming Leibfbcef02023-01-06 12:17:08 +08001930static int ublk_ctrl_get_params(struct ublk_device *ub,
1931 struct io_uring_cmd *cmd)
Ming Lei0aa73172022-07-30 17:27:49 +08001932{
1933 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1934 void __user *argp = (void __user *)(unsigned long)header->addr;
1935 struct ublk_params_header ph;
Ming Lei0aa73172022-07-30 17:27:49 +08001936 int ret;
1937
1938 if (header->len <= sizeof(ph) || !header->addr)
1939 return -EINVAL;
1940
1941 if (copy_from_user(&ph, argp, sizeof(ph)))
1942 return -EFAULT;
1943
1944 if (ph.len > header->len || !ph.len)
1945 return -EINVAL;
1946
1947 if (ph.len > sizeof(struct ublk_params))
1948 ph.len = sizeof(struct ublk_params);
1949
Ming Lei0aa73172022-07-30 17:27:49 +08001950 mutex_lock(&ub->mutex);
Ming Leiabb864d2023-01-06 12:17:09 +08001951 ublk_ctrl_fill_params_devt(ub);
Ming Lei0aa73172022-07-30 17:27:49 +08001952 if (copy_to_user(argp, &ub->params, ph.len))
1953 ret = -EFAULT;
1954 else
1955 ret = 0;
1956 mutex_unlock(&ub->mutex);
1957
Ming Lei0aa73172022-07-30 17:27:49 +08001958 return ret;
1959}
1960
Ming Leibfbcef02023-01-06 12:17:08 +08001961static int ublk_ctrl_set_params(struct ublk_device *ub,
1962 struct io_uring_cmd *cmd)
Ming Lei0aa73172022-07-30 17:27:49 +08001963{
1964 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1965 void __user *argp = (void __user *)(unsigned long)header->addr;
1966 struct ublk_params_header ph;
Ming Lei0aa73172022-07-30 17:27:49 +08001967 int ret = -EFAULT;
1968
1969 if (header->len <= sizeof(ph) || !header->addr)
1970 return -EINVAL;
1971
1972 if (copy_from_user(&ph, argp, sizeof(ph)))
1973 return -EFAULT;
1974
1975 if (ph.len > header->len || !ph.len || !ph.types)
1976 return -EINVAL;
1977
1978 if (ph.len > sizeof(struct ublk_params))
1979 ph.len = sizeof(struct ublk_params);
1980
Ming Lei0aa73172022-07-30 17:27:49 +08001981 /* parameters can only be changed when device isn't live */
1982 mutex_lock(&ub->mutex);
1983 if (ub->dev_info.state == UBLK_S_DEV_LIVE) {
1984 ret = -EACCES;
1985 } else if (copy_from_user(&ub->params, argp, ph.len)) {
1986 ret = -EFAULT;
1987 } else {
1988 /* clear all we don't support yet */
1989 ub->params.types &= UBLK_PARAM_TYPE_ALL;
1990 ret = ublk_validate_params(ub);
Ming Lei1d166522023-04-06 20:40:59 +08001991 if (ret)
1992 ub->params.types = 0;
Ming Lei0aa73172022-07-30 17:27:49 +08001993 }
1994 mutex_unlock(&ub->mutex);
Ming Lei0aa73172022-07-30 17:27:49 +08001995
1996 return ret;
1997}
1998
ZiyangZhangc732a852022-09-23 23:39:18 +08001999static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
2000{
2001 int i;
2002
2003 WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq)));
2004 /* All old ioucmds have to be completed */
2005 WARN_ON_ONCE(ubq->nr_io_ready);
2006 /* old daemon is PF_EXITING, put it now */
2007 put_task_struct(ubq->ubq_daemon);
2008 /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */
2009 ubq->ubq_daemon = NULL;
2010
2011 for (i = 0; i < ubq->q_depth; i++) {
2012 struct ublk_io *io = &ubq->ios[i];
2013
2014 /* forget everything now and be ready for new FETCH_REQ */
2015 io->flags = 0;
2016 io->cmd = NULL;
2017 io->addr = 0;
2018 }
2019}
2020
Ming Leibfbcef02023-01-06 12:17:08 +08002021static int ublk_ctrl_start_recovery(struct ublk_device *ub,
2022 struct io_uring_cmd *cmd)
ZiyangZhangc732a852022-09-23 23:39:18 +08002023{
2024 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
ZiyangZhangc732a852022-09-23 23:39:18 +08002025 int ret = -EINVAL;
2026 int i;
2027
ZiyangZhangc732a852022-09-23 23:39:18 +08002028 mutex_lock(&ub->mutex);
2029 if (!ublk_can_use_recovery(ub))
2030 goto out_unlock;
2031 /*
2032 * START_RECOVERY is only allowd after:
2033 *
2034 * (1) UB_STATE_OPEN is not set, which means the dying process is exited
2035 * and related io_uring ctx is freed so file struct of /dev/ublkcX is
2036 * released.
2037 *
2038 * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work:
2039 * (a)has quiesced request queue
2040 * (b)has requeued every inflight rqs whose io_flags is ACTIVE
2041 * (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE
2042 * (d)has completed/camceled all ioucmds owned by ther dying process
2043 */
2044 if (test_bit(UB_STATE_OPEN, &ub->state) ||
2045 ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
2046 ret = -EBUSY;
2047 goto out_unlock;
2048 }
2049 pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id);
2050 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
2051 ublk_queue_reinit(ub, ublk_get_queue(ub, i));
2052 /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */
2053 ub->mm = NULL;
2054 ub->nr_queues_ready = 0;
Ming Lei73a166d2023-01-06 12:17:07 +08002055 ub->nr_privileged_daemon = 0;
ZiyangZhangc732a852022-09-23 23:39:18 +08002056 init_completion(&ub->completion);
2057 ret = 0;
2058 out_unlock:
2059 mutex_unlock(&ub->mutex);
ZiyangZhangc732a852022-09-23 23:39:18 +08002060 return ret;
2061}
2062
Ming Leibfbcef02023-01-06 12:17:08 +08002063static int ublk_ctrl_end_recovery(struct ublk_device *ub,
2064 struct io_uring_cmd *cmd)
ZiyangZhangc732a852022-09-23 23:39:18 +08002065{
2066 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
2067 int ublksrv_pid = (int)header->data[0];
ZiyangZhangc732a852022-09-23 23:39:18 +08002068 int ret = -EINVAL;
2069
ZiyangZhangc732a852022-09-23 23:39:18 +08002070 pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
2071 __func__, ub->dev_info.nr_hw_queues, header->dev_id);
2072 /* wait until new ubq_daemon sending all FETCH_REQ */
2073 wait_for_completion_interruptible(&ub->completion);
2074 pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
2075 __func__, ub->dev_info.nr_hw_queues, header->dev_id);
2076
2077 mutex_lock(&ub->mutex);
2078 if (!ublk_can_use_recovery(ub))
2079 goto out_unlock;
2080
2081 if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
2082 ret = -EBUSY;
2083 goto out_unlock;
2084 }
2085 ub->dev_info.ublksrv_pid = ublksrv_pid;
2086 pr_devel("%s: new ublksrv_pid %d, dev id %d\n",
2087 __func__, ublksrv_pid, header->dev_id);
2088 blk_mq_unquiesce_queue(ub->ub_disk->queue);
2089 pr_devel("%s: queue unquiesced, dev id %d.\n",
2090 __func__, header->dev_id);
2091 blk_mq_kick_requeue_list(ub->ub_disk->queue);
2092 ub->dev_info.state = UBLK_S_DEV_LIVE;
2093 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
2094 ret = 0;
2095 out_unlock:
2096 mutex_unlock(&ub->mutex);
ZiyangZhangc732a852022-09-23 23:39:18 +08002097 return ret;
2098}
2099
Ming Lei4093cb52023-01-06 12:17:11 +08002100/*
2101 * All control commands are sent via /dev/ublk-control, so we have to check
2102 * the destination device's permission
2103 */
2104static int ublk_char_dev_permission(struct ublk_device *ub,
2105 const char *dev_path, int mask)
2106{
2107 int err;
2108 struct path path;
2109 struct kstat stat;
2110
2111 err = kern_path(dev_path, LOOKUP_FOLLOW, &path);
2112 if (err)
2113 return err;
2114
2115 err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
2116 if (err)
2117 goto exit;
2118
2119 err = -EPERM;
2120 if (stat.rdev != ub->cdev_dev.devt || !S_ISCHR(stat.mode))
2121 goto exit;
2122
Linus Torvalds5b0ed592023-02-20 14:27:21 -08002123 err = inode_permission(&nop_mnt_idmap,
Ming Lei4093cb52023-01-06 12:17:11 +08002124 d_backing_inode(path.dentry), mask);
2125exit:
2126 path_put(&path);
2127 return err;
2128}
2129
2130static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
2131 struct io_uring_cmd *cmd)
2132{
2133 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
2134 bool unprivileged = ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV;
2135 void __user *argp = (void __user *)(unsigned long)header->addr;
2136 char *dev_path = NULL;
2137 int ret = 0;
2138 int mask;
2139
2140 if (!unprivileged) {
2141 if (!capable(CAP_SYS_ADMIN))
2142 return -EPERM;
2143 /*
2144 * The new added command of UBLK_CMD_GET_DEV_INFO2 includes
2145 * char_dev_path in payload too, since userspace may not
2146 * know if the specified device is created as unprivileged
2147 * mode.
2148 */
Ming Lei2d786e62023-04-18 21:18:10 +08002149 if (_IOC_NR(cmd->cmd_op) != UBLK_CMD_GET_DEV_INFO2)
Ming Lei4093cb52023-01-06 12:17:11 +08002150 return 0;
2151 }
2152
2153 /*
2154 * User has to provide the char device path for unprivileged ublk
2155 *
2156 * header->addr always points to the dev path buffer, and
2157 * header->dev_path_len records length of dev path buffer.
2158 */
2159 if (!header->dev_path_len || header->dev_path_len > PATH_MAX)
2160 return -EINVAL;
2161
2162 if (header->len < header->dev_path_len)
2163 return -EINVAL;
2164
2165 dev_path = kmalloc(header->dev_path_len + 1, GFP_KERNEL);
2166 if (!dev_path)
2167 return -ENOMEM;
2168
2169 ret = -EFAULT;
2170 if (copy_from_user(dev_path, argp, header->dev_path_len))
2171 goto exit;
2172 dev_path[header->dev_path_len] = 0;
2173
2174 ret = -EINVAL;
Ming Lei2d786e62023-04-18 21:18:10 +08002175 switch (_IOC_NR(cmd->cmd_op)) {
Ming Lei4093cb52023-01-06 12:17:11 +08002176 case UBLK_CMD_GET_DEV_INFO:
2177 case UBLK_CMD_GET_DEV_INFO2:
2178 case UBLK_CMD_GET_QUEUE_AFFINITY:
2179 case UBLK_CMD_GET_PARAMS:
2180 mask = MAY_READ;
2181 break;
2182 case UBLK_CMD_START_DEV:
2183 case UBLK_CMD_STOP_DEV:
2184 case UBLK_CMD_ADD_DEV:
2185 case UBLK_CMD_DEL_DEV:
2186 case UBLK_CMD_SET_PARAMS:
2187 case UBLK_CMD_START_USER_RECOVERY:
2188 case UBLK_CMD_END_USER_RECOVERY:
2189 mask = MAY_READ | MAY_WRITE;
2190 break;
2191 default:
2192 goto exit;
2193 }
2194
2195 ret = ublk_char_dev_permission(ub, dev_path, mask);
2196 if (!ret) {
2197 header->len -= header->dev_path_len;
2198 header->addr += header->dev_path_len;
2199 }
2200 pr_devel("%s: dev id %d cmd_op %x uid %d gid %d path %s ret %d\n",
2201 __func__, ub->ub_number, cmd->cmd_op,
2202 ub->dev_info.owner_uid, ub->dev_info.owner_gid,
2203 dev_path, ret);
2204exit:
2205 kfree(dev_path);
Ming Lei71f28f32022-07-13 22:07:10 +08002206 return ret;
2207}
2208
2209static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
2210 unsigned int issue_flags)
2211{
2212 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
Ming Leibfbcef02023-01-06 12:17:08 +08002213 struct ublk_device *ub = NULL;
Ming Lei2d786e62023-04-18 21:18:10 +08002214 u32 cmd_op = cmd->cmd_op;
Ming Lei71f28f32022-07-13 22:07:10 +08002215 int ret = -EINVAL;
2216
Ming Leifa8e442e2023-01-04 21:32:35 +08002217 if (issue_flags & IO_URING_F_NONBLOCK)
2218 return -EAGAIN;
2219
Ming Lei71f28f32022-07-13 22:07:10 +08002220 ublk_ctrl_cmd_dump(cmd);
2221
2222 if (!(issue_flags & IO_URING_F_SQE128))
2223 goto out;
2224
Ming Lei2d786e62023-04-18 21:18:10 +08002225 ret = ublk_check_cmd_op(cmd_op);
2226 if (ret)
2227 goto out;
2228
2229 if (_IOC_NR(cmd_op) != UBLK_CMD_ADD_DEV) {
Ming Leibfbcef02023-01-06 12:17:08 +08002230 ret = -ENODEV;
2231 ub = ublk_get_device_from_id(header->dev_id);
2232 if (!ub)
2233 goto out;
Ming Lei71f28f32022-07-13 22:07:10 +08002234
Ming Lei4093cb52023-01-06 12:17:11 +08002235 ret = ublk_ctrl_uring_cmd_permission(ub, cmd);
Ming Lei2d786e62023-04-18 21:18:10 +08002236 if (ret)
2237 goto put_dev;
Ming Leibfbcef02023-01-06 12:17:08 +08002238 }
2239
Ming Lei2d786e62023-04-18 21:18:10 +08002240 switch (_IOC_NR(cmd_op)) {
Ming Lei71f28f32022-07-13 22:07:10 +08002241 case UBLK_CMD_START_DEV:
Ming Leibfbcef02023-01-06 12:17:08 +08002242 ret = ublk_ctrl_start_dev(ub, cmd);
Ming Lei71f28f32022-07-13 22:07:10 +08002243 break;
2244 case UBLK_CMD_STOP_DEV:
Ming Leibfbcef02023-01-06 12:17:08 +08002245 ret = ublk_ctrl_stop_dev(ub);
Ming Lei71f28f32022-07-13 22:07:10 +08002246 break;
2247 case UBLK_CMD_GET_DEV_INFO:
Ming Lei4093cb52023-01-06 12:17:11 +08002248 case UBLK_CMD_GET_DEV_INFO2:
Ming Leibfbcef02023-01-06 12:17:08 +08002249 ret = ublk_ctrl_get_dev_info(ub, cmd);
Ming Lei71f28f32022-07-13 22:07:10 +08002250 break;
2251 case UBLK_CMD_ADD_DEV:
Christoph Hellwig34d8f2b2022-07-21 15:09:13 +02002252 ret = ublk_ctrl_add_dev(cmd);
Ming Lei71f28f32022-07-13 22:07:10 +08002253 break;
2254 case UBLK_CMD_DEL_DEV:
Ming Leibfbcef02023-01-06 12:17:08 +08002255 ret = ublk_ctrl_del_dev(&ub);
Ming Lei71f28f32022-07-13 22:07:10 +08002256 break;
2257 case UBLK_CMD_GET_QUEUE_AFFINITY:
Ming Leibfbcef02023-01-06 12:17:08 +08002258 ret = ublk_ctrl_get_queue_affinity(ub, cmd);
Ming Lei71f28f32022-07-13 22:07:10 +08002259 break;
Ming Lei0aa73172022-07-30 17:27:49 +08002260 case UBLK_CMD_GET_PARAMS:
Ming Leibfbcef02023-01-06 12:17:08 +08002261 ret = ublk_ctrl_get_params(ub, cmd);
Ming Lei0aa73172022-07-30 17:27:49 +08002262 break;
2263 case UBLK_CMD_SET_PARAMS:
Ming Leibfbcef02023-01-06 12:17:08 +08002264 ret = ublk_ctrl_set_params(ub, cmd);
Ming Lei0aa73172022-07-30 17:27:49 +08002265 break;
ZiyangZhangc732a852022-09-23 23:39:18 +08002266 case UBLK_CMD_START_USER_RECOVERY:
Ming Leibfbcef02023-01-06 12:17:08 +08002267 ret = ublk_ctrl_start_recovery(ub, cmd);
ZiyangZhangc732a852022-09-23 23:39:18 +08002268 break;
2269 case UBLK_CMD_END_USER_RECOVERY:
Ming Leibfbcef02023-01-06 12:17:08 +08002270 ret = ublk_ctrl_end_recovery(ub, cmd);
ZiyangZhangc732a852022-09-23 23:39:18 +08002271 break;
Ming Lei71f28f32022-07-13 22:07:10 +08002272 default:
Ming Leibfbcef02023-01-06 12:17:08 +08002273 ret = -ENOTSUPP;
Ming Lei71f28f32022-07-13 22:07:10 +08002274 break;
Yang Li6b1439d2022-07-18 09:54:31 +08002275 }
Ming Lei4093cb52023-01-06 12:17:11 +08002276
2277 put_dev:
Ming Leibfbcef02023-01-06 12:17:08 +08002278 if (ub)
2279 ublk_put_device(ub);
Ming Lei71f28f32022-07-13 22:07:10 +08002280 out:
Jens Axboe9d2789a2023-03-20 20:01:25 -06002281 io_uring_cmd_done(cmd, ret, 0, issue_flags);
Ming Lei71f28f32022-07-13 22:07:10 +08002282 pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
2283 __func__, ret, cmd->cmd_op, header->dev_id, header->queue_id);
2284 return -EIOCBQUEUED;
2285}
2286
2287static const struct file_operations ublk_ctl_fops = {
2288 .open = nonseekable_open,
2289 .uring_cmd = ublk_ctrl_uring_cmd,
2290 .owner = THIS_MODULE,
2291 .llseek = noop_llseek,
2292};
2293
2294static struct miscdevice ublk_misc = {
2295 .minor = MISC_DYNAMIC_MINOR,
2296 .name = "ublk-control",
2297 .fops = &ublk_ctl_fops,
2298};
2299
2300static int __init ublk_init(void)
2301{
2302 int ret;
2303
2304 init_waitqueue_head(&ublk_idr_wq);
2305
2306 ret = misc_register(&ublk_misc);
2307 if (ret)
2308 return ret;
2309
2310 ret = alloc_chrdev_region(&ublk_chr_devt, 0, UBLK_MINORS, "ublk-char");
2311 if (ret)
2312 goto unregister_mis;
2313
Greg Kroah-Hartman1aaba112023-03-13 19:18:35 +01002314 ublk_chr_class = class_create("ublk-char");
Ming Lei71f28f32022-07-13 22:07:10 +08002315 if (IS_ERR(ublk_chr_class)) {
2316 ret = PTR_ERR(ublk_chr_class);
2317 goto free_chrdev_region;
2318 }
2319 return 0;
2320
2321free_chrdev_region:
2322 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
2323unregister_mis:
2324 misc_deregister(&ublk_misc);
2325 return ret;
2326}
2327
2328static void __exit ublk_exit(void)
2329{
2330 struct ublk_device *ub;
2331 int id;
2332
Ming Lei71f28f32022-07-13 22:07:10 +08002333 idr_for_each_entry(&ublk_index_idr, ub, id)
2334 ublk_remove(ub);
2335
Ming Lei8e4ff682023-01-26 19:53:46 +08002336 class_destroy(ublk_chr_class);
2337 misc_deregister(&ublk_misc);
2338
Ming Lei71f28f32022-07-13 22:07:10 +08002339 idr_destroy(&ublk_index_idr);
2340 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
2341}
2342
2343module_init(ublk_init);
2344module_exit(ublk_exit);
2345
Ming Lei403ebc82023-01-06 12:17:10 +08002346module_param(ublks_max, int, 0444);
2347MODULE_PARM_DESC(ublks_max, "max number of ublk devices allowed to add(default: 64)");
2348
Ming Lei71f28f32022-07-13 22:07:10 +08002349MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");
2350MODULE_LICENSE("GPL");