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