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