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