]>
Commit | Line | Data |
---|---|---|
4cc96131 MS |
1 | /* |
2 | * Copyright (C) 2016 Red Hat, Inc. All rights reserved. | |
3 | * | |
4 | * This file is released under the GPL. | |
5 | */ | |
6 | ||
7 | #include "dm-core.h" | |
8 | #include "dm-rq.h" | |
9 | ||
10 | #include <linux/elevator.h> /* for rq_end_sector() */ | |
11 | #include <linux/blk-mq.h> | |
12 | ||
13 | #define DM_MSG_PREFIX "core-rq" | |
14 | ||
15 | #define DM_MQ_NR_HW_QUEUES 1 | |
16 | #define DM_MQ_QUEUE_DEPTH 2048 | |
17 | static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES; | |
18 | static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH; | |
19 | ||
20 | /* | |
21 | * Request-based DM's mempools' reserved IOs set by the user. | |
22 | */ | |
23 | #define RESERVED_REQUEST_BASED_IOS 256 | |
24 | static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS; | |
25 | ||
26 | #ifdef CONFIG_DM_MQ_DEFAULT | |
27 | static bool use_blk_mq = true; | |
28 | #else | |
29 | static bool use_blk_mq = false; | |
30 | #endif | |
31 | ||
32 | bool dm_use_blk_mq_default(void) | |
33 | { | |
34 | return use_blk_mq; | |
35 | } | |
36 | ||
37 | bool dm_use_blk_mq(struct mapped_device *md) | |
38 | { | |
39 | return md->use_blk_mq; | |
40 | } | |
41 | EXPORT_SYMBOL_GPL(dm_use_blk_mq); | |
42 | ||
43 | unsigned dm_get_reserved_rq_based_ios(void) | |
44 | { | |
45 | return __dm_get_module_param(&reserved_rq_based_ios, | |
46 | RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS); | |
47 | } | |
48 | EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios); | |
49 | ||
50 | static unsigned dm_get_blk_mq_nr_hw_queues(void) | |
51 | { | |
52 | return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32); | |
53 | } | |
54 | ||
55 | static unsigned dm_get_blk_mq_queue_depth(void) | |
56 | { | |
57 | return __dm_get_module_param(&dm_mq_queue_depth, | |
58 | DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH); | |
59 | } | |
60 | ||
61 | int dm_request_based(struct mapped_device *md) | |
62 | { | |
63 | return blk_queue_stackable(md->queue); | |
64 | } | |
65 | ||
66 | static void dm_old_start_queue(struct request_queue *q) | |
67 | { | |
68 | unsigned long flags; | |
69 | ||
70 | spin_lock_irqsave(q->queue_lock, flags); | |
71 | if (blk_queue_stopped(q)) | |
72 | blk_start_queue(q); | |
73 | spin_unlock_irqrestore(q->queue_lock, flags); | |
74 | } | |
75 | ||
9dbeaeab MS |
76 | static void dm_mq_start_queue(struct request_queue *q) |
77 | { | |
78 | unsigned long flags; | |
79 | ||
80 | spin_lock_irqsave(q->queue_lock, flags); | |
81 | queue_flag_clear(QUEUE_FLAG_STOPPED, q); | |
82 | spin_unlock_irqrestore(q->queue_lock, flags); | |
83 | ||
84 | blk_mq_start_stopped_hw_queues(q, true); | |
85 | blk_mq_kick_requeue_list(q); | |
86 | } | |
87 | ||
4cc96131 MS |
88 | void dm_start_queue(struct request_queue *q) |
89 | { | |
90 | if (!q->mq_ops) | |
91 | dm_old_start_queue(q); | |
9dbeaeab MS |
92 | else |
93 | dm_mq_start_queue(q); | |
4cc96131 MS |
94 | } |
95 | ||
96 | static void dm_old_stop_queue(struct request_queue *q) | |
97 | { | |
98 | unsigned long flags; | |
99 | ||
100 | spin_lock_irqsave(q->queue_lock, flags); | |
101 | if (blk_queue_stopped(q)) { | |
102 | spin_unlock_irqrestore(q->queue_lock, flags); | |
103 | return; | |
104 | } | |
105 | ||
106 | blk_stop_queue(q); | |
107 | spin_unlock_irqrestore(q->queue_lock, flags); | |
108 | } | |
109 | ||
2397a15a BVA |
110 | static void dm_mq_stop_queue(struct request_queue *q) |
111 | { | |
112 | unsigned long flags; | |
113 | ||
114 | spin_lock_irqsave(q->queue_lock, flags); | |
115 | if (blk_queue_stopped(q)) { | |
116 | spin_unlock_irqrestore(q->queue_lock, flags); | |
117 | return; | |
118 | } | |
119 | ||
120 | queue_flag_set(QUEUE_FLAG_STOPPED, q); | |
121 | spin_unlock_irqrestore(q->queue_lock, flags); | |
122 | ||
123 | /* Avoid that requeuing could restart the queue. */ | |
124 | blk_mq_cancel_requeue_work(q); | |
125 | blk_mq_stop_hw_queues(q); | |
126 | } | |
127 | ||
4cc96131 MS |
128 | void dm_stop_queue(struct request_queue *q) |
129 | { | |
130 | if (!q->mq_ops) | |
131 | dm_old_stop_queue(q); | |
2397a15a BVA |
132 | else |
133 | dm_mq_stop_queue(q); | |
4cc96131 MS |
134 | } |
135 | ||
136 | static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md, | |
137 | gfp_t gfp_mask) | |
138 | { | |
139 | return mempool_alloc(md->io_pool, gfp_mask); | |
140 | } | |
141 | ||
142 | static void free_old_rq_tio(struct dm_rq_target_io *tio) | |
143 | { | |
144 | mempool_free(tio, tio->md->io_pool); | |
145 | } | |
146 | ||
147 | static struct request *alloc_old_clone_request(struct mapped_device *md, | |
148 | gfp_t gfp_mask) | |
149 | { | |
150 | return mempool_alloc(md->rq_pool, gfp_mask); | |
151 | } | |
152 | ||
153 | static void free_old_clone_request(struct mapped_device *md, struct request *rq) | |
154 | { | |
155 | mempool_free(rq, md->rq_pool); | |
156 | } | |
157 | ||
158 | /* | |
159 | * Partial completion handling for request-based dm | |
160 | */ | |
161 | static void end_clone_bio(struct bio *clone) | |
162 | { | |
163 | struct dm_rq_clone_bio_info *info = | |
164 | container_of(clone, struct dm_rq_clone_bio_info, clone); | |
165 | struct dm_rq_target_io *tio = info->tio; | |
166 | struct bio *bio = info->orig; | |
167 | unsigned int nr_bytes = info->orig->bi_iter.bi_size; | |
168 | int error = clone->bi_error; | |
169 | ||
170 | bio_put(clone); | |
171 | ||
172 | if (tio->error) | |
173 | /* | |
174 | * An error has already been detected on the request. | |
175 | * Once error occurred, just let clone->end_io() handle | |
176 | * the remainder. | |
177 | */ | |
178 | return; | |
179 | else if (error) { | |
180 | /* | |
181 | * Don't notice the error to the upper layer yet. | |
182 | * The error handling decision is made by the target driver, | |
183 | * when the request is completed. | |
184 | */ | |
185 | tio->error = error; | |
186 | return; | |
187 | } | |
188 | ||
189 | /* | |
190 | * I/O for the bio successfully completed. | |
191 | * Notice the data completion to the upper layer. | |
192 | */ | |
193 | ||
194 | /* | |
195 | * bios are processed from the head of the list. | |
196 | * So the completing bio should always be rq->bio. | |
197 | * If it's not, something wrong is happening. | |
198 | */ | |
199 | if (tio->orig->bio != bio) | |
200 | DMERR("bio completion is going in the middle of the request"); | |
201 | ||
202 | /* | |
203 | * Update the original request. | |
204 | * Do not use blk_end_request() here, because it may complete | |
205 | * the original request before the clone, and break the ordering. | |
206 | */ | |
207 | blk_update_request(tio->orig, 0, nr_bytes); | |
208 | } | |
209 | ||
210 | static struct dm_rq_target_io *tio_from_request(struct request *rq) | |
211 | { | |
212 | return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special); | |
213 | } | |
214 | ||
215 | static void rq_end_stats(struct mapped_device *md, struct request *orig) | |
216 | { | |
217 | if (unlikely(dm_stats_used(&md->stats))) { | |
218 | struct dm_rq_target_io *tio = tio_from_request(orig); | |
219 | tio->duration_jiffies = jiffies - tio->duration_jiffies; | |
220 | dm_stats_account_io(&md->stats, rq_data_dir(orig), | |
221 | blk_rq_pos(orig), tio->n_sectors, true, | |
222 | tio->duration_jiffies, &tio->stats_aux); | |
223 | } | |
224 | } | |
225 | ||
226 | /* | |
227 | * Don't touch any member of the md after calling this function because | |
228 | * the md may be freed in dm_put() at the end of this function. | |
229 | * Or do dm_get() before calling this function and dm_put() later. | |
230 | */ | |
231 | static void rq_completed(struct mapped_device *md, int rw, bool run_queue) | |
232 | { | |
233 | atomic_dec(&md->pending[rw]); | |
234 | ||
235 | /* nudge anyone waiting on suspend queue */ | |
236 | if (!md_in_flight(md)) | |
237 | wake_up(&md->wait); | |
238 | ||
239 | /* | |
240 | * Run this off this callpath, as drivers could invoke end_io while | |
241 | * inside their request_fn (and holding the queue lock). Calling | |
242 | * back into ->request_fn() could deadlock attempting to grab the | |
243 | * queue lock again. | |
244 | */ | |
245 | if (!md->queue->mq_ops && run_queue) | |
246 | blk_run_queue_async(md->queue); | |
247 | ||
248 | /* | |
249 | * dm_put() must be at the end of this function. See the comment above | |
250 | */ | |
251 | dm_put(md); | |
252 | } | |
253 | ||
254 | static void free_rq_clone(struct request *clone) | |
255 | { | |
256 | struct dm_rq_target_io *tio = clone->end_io_data; | |
257 | struct mapped_device *md = tio->md; | |
258 | ||
259 | blk_rq_unprep_clone(clone); | |
260 | ||
e83068a5 MS |
261 | /* |
262 | * It is possible for a clone_old_rq() allocated clone to | |
263 | * get passed in -- it may not yet have a request_queue. | |
264 | * This is known to occur if the error target replaces | |
265 | * a multipath target that has a request_fn queue stacked | |
266 | * on blk-mq queue(s). | |
267 | */ | |
268 | if (clone->q && clone->q->mq_ops) | |
4cc96131 MS |
269 | /* stacked on blk-mq queue(s) */ |
270 | tio->ti->type->release_clone_rq(clone); | |
271 | else if (!md->queue->mq_ops) | |
272 | /* request_fn queue stacked on request_fn queue(s) */ | |
273 | free_old_clone_request(md, clone); | |
274 | ||
275 | if (!md->queue->mq_ops) | |
276 | free_old_rq_tio(tio); | |
277 | } | |
278 | ||
279 | /* | |
280 | * Complete the clone and the original request. | |
281 | * Must be called without clone's queue lock held, | |
282 | * see end_clone_request() for more details. | |
283 | */ | |
284 | static void dm_end_request(struct request *clone, int error) | |
285 | { | |
286 | int rw = rq_data_dir(clone); | |
287 | struct dm_rq_target_io *tio = clone->end_io_data; | |
288 | struct mapped_device *md = tio->md; | |
289 | struct request *rq = tio->orig; | |
290 | ||
291 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { | |
292 | rq->errors = clone->errors; | |
293 | rq->resid_len = clone->resid_len; | |
294 | ||
295 | if (rq->sense) | |
296 | /* | |
297 | * We are using the sense buffer of the original | |
298 | * request. | |
299 | * So setting the length of the sense data is enough. | |
300 | */ | |
301 | rq->sense_len = clone->sense_len; | |
302 | } | |
303 | ||
304 | free_rq_clone(clone); | |
305 | rq_end_stats(md, rq); | |
306 | if (!rq->q->mq_ops) | |
307 | blk_end_request_all(rq, error); | |
308 | else | |
309 | blk_mq_end_request(rq, error); | |
310 | rq_completed(md, rw, true); | |
311 | } | |
312 | ||
313 | static void dm_unprep_request(struct request *rq) | |
314 | { | |
315 | struct dm_rq_target_io *tio = tio_from_request(rq); | |
316 | struct request *clone = tio->clone; | |
317 | ||
318 | if (!rq->q->mq_ops) { | |
319 | rq->special = NULL; | |
320 | rq->cmd_flags &= ~REQ_DONTPREP; | |
321 | } | |
322 | ||
323 | if (clone) | |
324 | free_rq_clone(clone); | |
325 | else if (!tio->md->queue->mq_ops) | |
326 | free_old_rq_tio(tio); | |
327 | } | |
328 | ||
329 | /* | |
330 | * Requeue the original request of a clone. | |
331 | */ | |
332 | static void dm_old_requeue_request(struct request *rq) | |
333 | { | |
334 | struct request_queue *q = rq->q; | |
335 | unsigned long flags; | |
336 | ||
337 | spin_lock_irqsave(q->queue_lock, flags); | |
338 | blk_requeue_request(q, rq); | |
339 | blk_run_queue_async(q); | |
340 | spin_unlock_irqrestore(q->queue_lock, flags); | |
341 | } | |
342 | ||
343 | static void dm_mq_requeue_request(struct request *rq) | |
344 | { | |
345 | struct request_queue *q = rq->q; | |
346 | unsigned long flags; | |
347 | ||
348 | blk_mq_requeue_request(rq); | |
349 | spin_lock_irqsave(q->queue_lock, flags); | |
350 | if (!blk_queue_stopped(q)) | |
351 | blk_mq_kick_requeue_list(q); | |
352 | spin_unlock_irqrestore(q->queue_lock, flags); | |
353 | } | |
354 | ||
355 | static void dm_requeue_original_request(struct mapped_device *md, | |
356 | struct request *rq) | |
357 | { | |
358 | int rw = rq_data_dir(rq); | |
359 | ||
360 | rq_end_stats(md, rq); | |
361 | dm_unprep_request(rq); | |
362 | ||
363 | if (!rq->q->mq_ops) | |
364 | dm_old_requeue_request(rq); | |
365 | else | |
366 | dm_mq_requeue_request(rq); | |
367 | ||
368 | rq_completed(md, rw, false); | |
369 | } | |
370 | ||
371 | static void dm_done(struct request *clone, int error, bool mapped) | |
372 | { | |
373 | int r = error; | |
374 | struct dm_rq_target_io *tio = clone->end_io_data; | |
375 | dm_request_endio_fn rq_end_io = NULL; | |
376 | ||
377 | if (tio->ti) { | |
378 | rq_end_io = tio->ti->type->rq_end_io; | |
379 | ||
380 | if (mapped && rq_end_io) | |
381 | r = rq_end_io(tio->ti, clone, error, &tio->info); | |
382 | } | |
383 | ||
384 | if (unlikely(r == -EREMOTEIO && (req_op(clone) == REQ_OP_WRITE_SAME) && | |
385 | !clone->q->limits.max_write_same_sectors)) | |
386 | disable_write_same(tio->md); | |
387 | ||
388 | if (r <= 0) | |
389 | /* The target wants to complete the I/O */ | |
390 | dm_end_request(clone, r); | |
391 | else if (r == DM_ENDIO_INCOMPLETE) | |
392 | /* The target will handle the I/O */ | |
393 | return; | |
394 | else if (r == DM_ENDIO_REQUEUE) | |
395 | /* The target wants to requeue the I/O */ | |
396 | dm_requeue_original_request(tio->md, tio->orig); | |
397 | else { | |
398 | DMWARN("unimplemented target endio return value: %d", r); | |
399 | BUG(); | |
400 | } | |
401 | } | |
402 | ||
403 | /* | |
404 | * Request completion handler for request-based dm | |
405 | */ | |
406 | static void dm_softirq_done(struct request *rq) | |
407 | { | |
408 | bool mapped = true; | |
409 | struct dm_rq_target_io *tio = tio_from_request(rq); | |
410 | struct request *clone = tio->clone; | |
411 | int rw; | |
412 | ||
413 | if (!clone) { | |
414 | rq_end_stats(tio->md, rq); | |
415 | rw = rq_data_dir(rq); | |
416 | if (!rq->q->mq_ops) { | |
417 | blk_end_request_all(rq, tio->error); | |
418 | rq_completed(tio->md, rw, false); | |
419 | free_old_rq_tio(tio); | |
420 | } else { | |
421 | blk_mq_end_request(rq, tio->error); | |
422 | rq_completed(tio->md, rw, false); | |
423 | } | |
424 | return; | |
425 | } | |
426 | ||
427 | if (rq->cmd_flags & REQ_FAILED) | |
428 | mapped = false; | |
429 | ||
430 | dm_done(clone, tio->error, mapped); | |
431 | } | |
432 | ||
433 | /* | |
434 | * Complete the clone and the original request with the error status | |
435 | * through softirq context. | |
436 | */ | |
437 | static void dm_complete_request(struct request *rq, int error) | |
438 | { | |
439 | struct dm_rq_target_io *tio = tio_from_request(rq); | |
440 | ||
441 | tio->error = error; | |
442 | if (!rq->q->mq_ops) | |
443 | blk_complete_request(rq); | |
444 | else | |
445 | blk_mq_complete_request(rq, error); | |
446 | } | |
447 | ||
448 | /* | |
449 | * Complete the not-mapped clone and the original request with the error status | |
450 | * through softirq context. | |
451 | * Target's rq_end_io() function isn't called. | |
452 | * This may be used when the target's map_rq() or clone_and_map_rq() functions fail. | |
453 | */ | |
454 | static void dm_kill_unmapped_request(struct request *rq, int error) | |
455 | { | |
456 | rq->cmd_flags |= REQ_FAILED; | |
457 | dm_complete_request(rq, error); | |
458 | } | |
459 | ||
460 | /* | |
461 | * Called with the clone's queue lock held (in the case of .request_fn) | |
462 | */ | |
463 | static void end_clone_request(struct request *clone, int error) | |
464 | { | |
465 | struct dm_rq_target_io *tio = clone->end_io_data; | |
466 | ||
467 | if (!clone->q->mq_ops) { | |
468 | /* | |
469 | * For just cleaning up the information of the queue in which | |
470 | * the clone was dispatched. | |
471 | * The clone is *NOT* freed actually here because it is alloced | |
472 | * from dm own mempool (REQ_ALLOCED isn't set). | |
473 | */ | |
474 | __blk_put_request(clone->q, clone); | |
475 | } | |
476 | ||
477 | /* | |
478 | * Actual request completion is done in a softirq context which doesn't | |
479 | * hold the clone's queue lock. Otherwise, deadlock could occur because: | |
480 | * - another request may be submitted by the upper level driver | |
481 | * of the stacking during the completion | |
482 | * - the submission which requires queue lock may be done | |
483 | * against this clone's queue | |
484 | */ | |
485 | dm_complete_request(tio->orig, error); | |
486 | } | |
487 | ||
488 | static void dm_dispatch_clone_request(struct request *clone, struct request *rq) | |
489 | { | |
490 | int r; | |
491 | ||
492 | if (blk_queue_io_stat(clone->q)) | |
493 | clone->cmd_flags |= REQ_IO_STAT; | |
494 | ||
495 | clone->start_time = jiffies; | |
496 | r = blk_insert_cloned_request(clone->q, clone); | |
497 | if (r) | |
498 | /* must complete clone in terms of original request */ | |
499 | dm_complete_request(rq, r); | |
500 | } | |
501 | ||
502 | static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, | |
503 | void *data) | |
504 | { | |
505 | struct dm_rq_target_io *tio = data; | |
506 | struct dm_rq_clone_bio_info *info = | |
507 | container_of(bio, struct dm_rq_clone_bio_info, clone); | |
508 | ||
509 | info->orig = bio_orig; | |
510 | info->tio = tio; | |
511 | bio->bi_end_io = end_clone_bio; | |
512 | ||
513 | return 0; | |
514 | } | |
515 | ||
516 | static int setup_clone(struct request *clone, struct request *rq, | |
517 | struct dm_rq_target_io *tio, gfp_t gfp_mask) | |
518 | { | |
519 | int r; | |
520 | ||
521 | r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask, | |
522 | dm_rq_bio_constructor, tio); | |
523 | if (r) | |
524 | return r; | |
525 | ||
526 | clone->cmd = rq->cmd; | |
527 | clone->cmd_len = rq->cmd_len; | |
528 | clone->sense = rq->sense; | |
529 | clone->end_io = end_clone_request; | |
530 | clone->end_io_data = tio; | |
531 | ||
532 | tio->clone = clone; | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
537 | static struct request *clone_old_rq(struct request *rq, struct mapped_device *md, | |
538 | struct dm_rq_target_io *tio, gfp_t gfp_mask) | |
539 | { | |
540 | /* | |
541 | * Create clone for use with .request_fn request_queue | |
542 | */ | |
543 | struct request *clone; | |
544 | ||
545 | clone = alloc_old_clone_request(md, gfp_mask); | |
546 | if (!clone) | |
547 | return NULL; | |
548 | ||
549 | blk_rq_init(NULL, clone); | |
550 | if (setup_clone(clone, rq, tio, gfp_mask)) { | |
551 | /* -ENOMEM */ | |
552 | free_old_clone_request(md, clone); | |
553 | return NULL; | |
554 | } | |
555 | ||
556 | return clone; | |
557 | } | |
558 | ||
559 | static void map_tio_request(struct kthread_work *work); | |
560 | ||
561 | static void init_tio(struct dm_rq_target_io *tio, struct request *rq, | |
562 | struct mapped_device *md) | |
563 | { | |
564 | tio->md = md; | |
565 | tio->ti = NULL; | |
566 | tio->clone = NULL; | |
567 | tio->orig = rq; | |
568 | tio->error = 0; | |
569 | /* | |
570 | * Avoid initializing info for blk-mq; it passes | |
571 | * target-specific data through info.ptr | |
572 | * (see: dm_mq_init_request) | |
573 | */ | |
574 | if (!md->init_tio_pdu) | |
575 | memset(&tio->info, 0, sizeof(tio->info)); | |
576 | if (md->kworker_task) | |
577 | init_kthread_work(&tio->work, map_tio_request); | |
578 | } | |
579 | ||
580 | static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq, | |
581 | struct mapped_device *md, | |
582 | gfp_t gfp_mask) | |
583 | { | |
584 | struct dm_rq_target_io *tio; | |
585 | int srcu_idx; | |
586 | struct dm_table *table; | |
587 | ||
588 | tio = alloc_old_rq_tio(md, gfp_mask); | |
589 | if (!tio) | |
590 | return NULL; | |
591 | ||
592 | init_tio(tio, rq, md); | |
593 | ||
594 | table = dm_get_live_table(md, &srcu_idx); | |
595 | /* | |
596 | * Must clone a request if this .request_fn DM device | |
597 | * is stacked on .request_fn device(s). | |
598 | */ | |
e83068a5 | 599 | if (!dm_table_all_blk_mq_devices(table)) { |
4cc96131 MS |
600 | if (!clone_old_rq(rq, md, tio, gfp_mask)) { |
601 | dm_put_live_table(md, srcu_idx); | |
602 | free_old_rq_tio(tio); | |
603 | return NULL; | |
604 | } | |
605 | } | |
606 | dm_put_live_table(md, srcu_idx); | |
607 | ||
608 | return tio; | |
609 | } | |
610 | ||
611 | /* | |
612 | * Called with the queue lock held. | |
613 | */ | |
614 | static int dm_old_prep_fn(struct request_queue *q, struct request *rq) | |
615 | { | |
616 | struct mapped_device *md = q->queuedata; | |
617 | struct dm_rq_target_io *tio; | |
618 | ||
619 | if (unlikely(rq->special)) { | |
620 | DMWARN("Already has something in rq->special."); | |
621 | return BLKPREP_KILL; | |
622 | } | |
623 | ||
624 | tio = dm_old_prep_tio(rq, md, GFP_ATOMIC); | |
625 | if (!tio) | |
626 | return BLKPREP_DEFER; | |
627 | ||
628 | rq->special = tio; | |
629 | rq->cmd_flags |= REQ_DONTPREP; | |
630 | ||
631 | return BLKPREP_OK; | |
632 | } | |
633 | ||
634 | /* | |
635 | * Returns: | |
636 | * 0 : the request has been processed | |
637 | * DM_MAPIO_REQUEUE : the original request needs to be requeued | |
638 | * < 0 : the request was completed due to failure | |
639 | */ | |
640 | static int map_request(struct dm_rq_target_io *tio, struct request *rq, | |
641 | struct mapped_device *md) | |
642 | { | |
643 | int r; | |
644 | struct dm_target *ti = tio->ti; | |
645 | struct request *clone = NULL; | |
646 | ||
647 | if (tio->clone) { | |
648 | clone = tio->clone; | |
649 | r = ti->type->map_rq(ti, clone, &tio->info); | |
650 | } else { | |
651 | r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone); | |
652 | if (r < 0) { | |
653 | /* The target wants to complete the I/O */ | |
654 | dm_kill_unmapped_request(rq, r); | |
655 | return r; | |
656 | } | |
657 | if (r != DM_MAPIO_REMAPPED) | |
658 | return r; | |
659 | if (setup_clone(clone, rq, tio, GFP_ATOMIC)) { | |
660 | /* -ENOMEM */ | |
661 | ti->type->release_clone_rq(clone); | |
662 | return DM_MAPIO_REQUEUE; | |
663 | } | |
664 | } | |
665 | ||
666 | switch (r) { | |
667 | case DM_MAPIO_SUBMITTED: | |
668 | /* The target has taken the I/O to submit by itself later */ | |
669 | break; | |
670 | case DM_MAPIO_REMAPPED: | |
671 | /* The target has remapped the I/O so dispatch it */ | |
672 | trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), | |
673 | blk_rq_pos(rq)); | |
674 | dm_dispatch_clone_request(clone, rq); | |
675 | break; | |
676 | case DM_MAPIO_REQUEUE: | |
677 | /* The target wants to requeue the I/O */ | |
678 | dm_requeue_original_request(md, tio->orig); | |
679 | break; | |
680 | default: | |
681 | if (r > 0) { | |
682 | DMWARN("unimplemented target map return value: %d", r); | |
683 | BUG(); | |
684 | } | |
685 | ||
686 | /* The target wants to complete the I/O */ | |
687 | dm_kill_unmapped_request(rq, r); | |
688 | return r; | |
689 | } | |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
694 | static void dm_start_request(struct mapped_device *md, struct request *orig) | |
695 | { | |
696 | if (!orig->q->mq_ops) | |
697 | blk_start_request(orig); | |
698 | else | |
699 | blk_mq_start_request(orig); | |
700 | atomic_inc(&md->pending[rq_data_dir(orig)]); | |
701 | ||
702 | if (md->seq_rq_merge_deadline_usecs) { | |
703 | md->last_rq_pos = rq_end_sector(orig); | |
704 | md->last_rq_rw = rq_data_dir(orig); | |
705 | md->last_rq_start_time = ktime_get(); | |
706 | } | |
707 | ||
708 | if (unlikely(dm_stats_used(&md->stats))) { | |
709 | struct dm_rq_target_io *tio = tio_from_request(orig); | |
710 | tio->duration_jiffies = jiffies; | |
711 | tio->n_sectors = blk_rq_sectors(orig); | |
712 | dm_stats_account_io(&md->stats, rq_data_dir(orig), | |
713 | blk_rq_pos(orig), tio->n_sectors, false, 0, | |
714 | &tio->stats_aux); | |
715 | } | |
716 | ||
717 | /* | |
718 | * Hold the md reference here for the in-flight I/O. | |
719 | * We can't rely on the reference count by device opener, | |
720 | * because the device may be closed during the request completion | |
721 | * when all bios are completed. | |
722 | * See the comment in rq_completed() too. | |
723 | */ | |
724 | dm_get(md); | |
725 | } | |
726 | ||
727 | static void map_tio_request(struct kthread_work *work) | |
728 | { | |
729 | struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work); | |
730 | struct request *rq = tio->orig; | |
731 | struct mapped_device *md = tio->md; | |
732 | ||
733 | if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) | |
734 | dm_requeue_original_request(md, rq); | |
735 | } | |
736 | ||
737 | ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf) | |
738 | { | |
739 | return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs); | |
740 | } | |
741 | ||
742 | #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000 | |
743 | ||
744 | ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md, | |
745 | const char *buf, size_t count) | |
746 | { | |
747 | unsigned deadline; | |
748 | ||
e83068a5 | 749 | if (dm_get_md_type(md) != DM_TYPE_REQUEST_BASED) |
4cc96131 MS |
750 | return count; |
751 | ||
752 | if (kstrtouint(buf, 10, &deadline)) | |
753 | return -EINVAL; | |
754 | ||
755 | if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS) | |
756 | deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS; | |
757 | ||
758 | md->seq_rq_merge_deadline_usecs = deadline; | |
759 | ||
760 | return count; | |
761 | } | |
762 | ||
763 | static bool dm_old_request_peeked_before_merge_deadline(struct mapped_device *md) | |
764 | { | |
765 | ktime_t kt_deadline; | |
766 | ||
767 | if (!md->seq_rq_merge_deadline_usecs) | |
768 | return false; | |
769 | ||
770 | kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC); | |
771 | kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline); | |
772 | ||
773 | return !ktime_after(ktime_get(), kt_deadline); | |
774 | } | |
775 | ||
776 | /* | |
777 | * q->request_fn for old request-based dm. | |
778 | * Called with the queue lock held. | |
779 | */ | |
780 | static void dm_old_request_fn(struct request_queue *q) | |
781 | { | |
782 | struct mapped_device *md = q->queuedata; | |
783 | struct dm_target *ti = md->immutable_target; | |
784 | struct request *rq; | |
785 | struct dm_rq_target_io *tio; | |
786 | sector_t pos = 0; | |
787 | ||
788 | if (unlikely(!ti)) { | |
789 | int srcu_idx; | |
790 | struct dm_table *map = dm_get_live_table(md, &srcu_idx); | |
791 | ||
792 | ti = dm_table_find_target(map, pos); | |
793 | dm_put_live_table(md, srcu_idx); | |
794 | } | |
795 | ||
796 | /* | |
797 | * For suspend, check blk_queue_stopped() and increment | |
798 | * ->pending within a single queue_lock not to increment the | |
799 | * number of in-flight I/Os after the queue is stopped in | |
800 | * dm_suspend(). | |
801 | */ | |
802 | while (!blk_queue_stopped(q)) { | |
803 | rq = blk_peek_request(q); | |
804 | if (!rq) | |
805 | return; | |
806 | ||
807 | /* always use block 0 to find the target for flushes for now */ | |
808 | pos = 0; | |
809 | if (req_op(rq) != REQ_OP_FLUSH) | |
810 | pos = blk_rq_pos(rq); | |
811 | ||
812 | if ((dm_old_request_peeked_before_merge_deadline(md) && | |
813 | md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 && | |
814 | md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) || | |
815 | (ti->type->busy && ti->type->busy(ti))) { | |
bd9f55ea | 816 | blk_delay_queue(q, 10); |
4cc96131 MS |
817 | return; |
818 | } | |
819 | ||
820 | dm_start_request(md, rq); | |
821 | ||
822 | tio = tio_from_request(rq); | |
823 | /* Establish tio->ti before queuing work (map_tio_request) */ | |
824 | tio->ti = ti; | |
825 | queue_kthread_work(&md->kworker, &tio->work); | |
826 | BUG_ON(!irqs_disabled()); | |
827 | } | |
828 | } | |
829 | ||
830 | /* | |
831 | * Fully initialize a .request_fn request-based queue. | |
832 | */ | |
833 | int dm_old_init_request_queue(struct mapped_device *md) | |
834 | { | |
835 | /* Fully initialize the queue */ | |
836 | if (!blk_init_allocated_queue(md->queue, dm_old_request_fn, NULL)) | |
837 | return -EINVAL; | |
838 | ||
839 | /* disable dm_old_request_fn's merge heuristic by default */ | |
840 | md->seq_rq_merge_deadline_usecs = 0; | |
841 | ||
842 | dm_init_normal_md_queue(md); | |
843 | blk_queue_softirq_done(md->queue, dm_softirq_done); | |
844 | blk_queue_prep_rq(md->queue, dm_old_prep_fn); | |
845 | ||
846 | /* Initialize the request-based DM worker thread */ | |
847 | init_kthread_worker(&md->kworker); | |
848 | md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker, | |
849 | "kdmwork-%s", dm_device_name(md)); | |
7193a9de MS |
850 | if (IS_ERR(md->kworker_task)) |
851 | return PTR_ERR(md->kworker_task); | |
4cc96131 MS |
852 | |
853 | elv_register_queue(md->queue); | |
854 | ||
855 | return 0; | |
856 | } | |
857 | ||
858 | static int dm_mq_init_request(void *data, struct request *rq, | |
859 | unsigned int hctx_idx, unsigned int request_idx, | |
860 | unsigned int numa_node) | |
861 | { | |
862 | struct mapped_device *md = data; | |
863 | struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); | |
864 | ||
865 | /* | |
866 | * Must initialize md member of tio, otherwise it won't | |
867 | * be available in dm_mq_queue_rq. | |
868 | */ | |
869 | tio->md = md; | |
870 | ||
871 | if (md->init_tio_pdu) { | |
872 | /* target-specific per-io data is immediately after the tio */ | |
873 | tio->info.ptr = tio + 1; | |
874 | } | |
875 | ||
876 | return 0; | |
877 | } | |
878 | ||
879 | static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx, | |
880 | const struct blk_mq_queue_data *bd) | |
881 | { | |
882 | struct request *rq = bd->rq; | |
883 | struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); | |
884 | struct mapped_device *md = tio->md; | |
885 | struct dm_target *ti = md->immutable_target; | |
886 | ||
887 | if (unlikely(!ti)) { | |
888 | int srcu_idx; | |
889 | struct dm_table *map = dm_get_live_table(md, &srcu_idx); | |
890 | ||
891 | ti = dm_table_find_target(map, 0); | |
892 | dm_put_live_table(md, srcu_idx); | |
893 | } | |
894 | ||
7d9595d8 MS |
895 | /* |
896 | * On suspend dm_stop_queue() handles stopping the blk-mq | |
897 | * request_queue BUT: even though the hw_queues are marked | |
898 | * BLK_MQ_S_STOPPED at that point there is still a race that | |
899 | * is allowing block/blk-mq.c to call ->queue_rq against a | |
900 | * hctx that it really shouldn't. The following check guards | |
901 | * against this rarity (albeit _not_ race-free). | |
902 | */ | |
903 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) | |
904 | return BLK_MQ_RQ_QUEUE_BUSY; | |
905 | ||
4cc96131 MS |
906 | if (ti->type->busy && ti->type->busy(ti)) |
907 | return BLK_MQ_RQ_QUEUE_BUSY; | |
908 | ||
909 | dm_start_request(md, rq); | |
910 | ||
911 | /* Init tio using md established in .init_request */ | |
912 | init_tio(tio, rq, md); | |
913 | ||
914 | /* | |
915 | * Establish tio->ti before calling map_request(). | |
916 | */ | |
917 | tio->ti = ti; | |
918 | ||
919 | /* Direct call is fine since .queue_rq allows allocations */ | |
920 | if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) { | |
921 | /* Undo dm_start_request() before requeuing */ | |
922 | rq_end_stats(md, rq); | |
923 | rq_completed(md, rq_data_dir(rq), false); | |
924 | return BLK_MQ_RQ_QUEUE_BUSY; | |
925 | } | |
926 | ||
927 | return BLK_MQ_RQ_QUEUE_OK; | |
928 | } | |
929 | ||
930 | static struct blk_mq_ops dm_mq_ops = { | |
931 | .queue_rq = dm_mq_queue_rq, | |
932 | .map_queue = blk_mq_map_queue, | |
933 | .complete = dm_softirq_done, | |
934 | .init_request = dm_mq_init_request, | |
935 | }; | |
936 | ||
e83068a5 | 937 | int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t) |
4cc96131 MS |
938 | { |
939 | struct request_queue *q; | |
e83068a5 | 940 | struct dm_target *immutable_tgt; |
4cc96131 MS |
941 | int err; |
942 | ||
e83068a5 | 943 | if (!dm_table_all_blk_mq_devices(t)) { |
4cc96131 MS |
944 | DMERR("request-based dm-mq may only be stacked on blk-mq device(s)"); |
945 | return -EINVAL; | |
946 | } | |
947 | ||
948 | md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id); | |
949 | if (!md->tag_set) | |
950 | return -ENOMEM; | |
951 | ||
952 | md->tag_set->ops = &dm_mq_ops; | |
953 | md->tag_set->queue_depth = dm_get_blk_mq_queue_depth(); | |
954 | md->tag_set->numa_node = md->numa_node_id; | |
955 | md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; | |
956 | md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues(); | |
957 | md->tag_set->driver_data = md; | |
958 | ||
959 | md->tag_set->cmd_size = sizeof(struct dm_rq_target_io); | |
e83068a5 | 960 | immutable_tgt = dm_table_get_immutable_target(t); |
4cc96131 MS |
961 | if (immutable_tgt && immutable_tgt->per_io_data_size) { |
962 | /* any target-specific per-io data is immediately after the tio */ | |
963 | md->tag_set->cmd_size += immutable_tgt->per_io_data_size; | |
964 | md->init_tio_pdu = true; | |
965 | } | |
966 | ||
967 | err = blk_mq_alloc_tag_set(md->tag_set); | |
968 | if (err) | |
969 | goto out_kfree_tag_set; | |
970 | ||
971 | q = blk_mq_init_allocated_queue(md->tag_set, md->queue); | |
972 | if (IS_ERR(q)) { | |
973 | err = PTR_ERR(q); | |
974 | goto out_tag_set; | |
975 | } | |
976 | dm_init_md_queue(md); | |
977 | ||
978 | /* backfill 'mq' sysfs registration normally done in blk_register_queue */ | |
979 | blk_mq_register_disk(md->disk); | |
980 | ||
981 | return 0; | |
982 | ||
983 | out_tag_set: | |
984 | blk_mq_free_tag_set(md->tag_set); | |
985 | out_kfree_tag_set: | |
986 | kfree(md->tag_set); | |
987 | ||
988 | return err; | |
989 | } | |
990 | ||
991 | void dm_mq_cleanup_mapped_device(struct mapped_device *md) | |
992 | { | |
993 | if (md->tag_set) { | |
994 | blk_mq_free_tag_set(md->tag_set); | |
995 | kfree(md->tag_set); | |
996 | } | |
997 | } | |
998 | ||
999 | module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR); | |
1000 | MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools"); | |
1001 | ||
1002 | module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR); | |
1003 | MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices"); | |
1004 | ||
1005 | module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR); | |
1006 | MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices"); | |
1007 | ||
1008 | module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR); | |
1009 | MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices"); |