Linux 4.19.133
[linux/fpc-iii.git] / drivers / md / dm-rq.c
blob4d36373e1c0f044264af149bfb46efe7aa6c25c5
1 /*
2 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
4 * This file is released under the GPL.
5 */
7 #include "dm-core.h"
8 #include "dm-rq.h"
10 #include <linux/elevator.h> /* for rq_end_sector() */
11 #include <linux/blk-mq.h>
13 #define DM_MSG_PREFIX "core-rq"
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;
21 * Request-based DM's mempools' reserved IOs set by the user.
23 #define RESERVED_REQUEST_BASED_IOS 256
24 static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
26 static bool use_blk_mq = IS_ENABLED(CONFIG_DM_MQ_DEFAULT);
28 bool dm_use_blk_mq_default(void)
30 return use_blk_mq;
33 bool dm_use_blk_mq(struct mapped_device *md)
35 return md->use_blk_mq;
37 EXPORT_SYMBOL_GPL(dm_use_blk_mq);
39 unsigned dm_get_reserved_rq_based_ios(void)
41 return __dm_get_module_param(&reserved_rq_based_ios,
42 RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
44 EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
46 static unsigned dm_get_blk_mq_nr_hw_queues(void)
48 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
51 static unsigned dm_get_blk_mq_queue_depth(void)
53 return __dm_get_module_param(&dm_mq_queue_depth,
54 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
57 int dm_request_based(struct mapped_device *md)
59 return queue_is_rq_based(md->queue);
62 static void dm_old_start_queue(struct request_queue *q)
64 unsigned long flags;
66 spin_lock_irqsave(q->queue_lock, flags);
67 if (blk_queue_stopped(q))
68 blk_start_queue(q);
69 spin_unlock_irqrestore(q->queue_lock, flags);
72 static void dm_mq_start_queue(struct request_queue *q)
74 blk_mq_unquiesce_queue(q);
75 blk_mq_kick_requeue_list(q);
78 void dm_start_queue(struct request_queue *q)
80 if (!q->mq_ops)
81 dm_old_start_queue(q);
82 else
83 dm_mq_start_queue(q);
86 static void dm_old_stop_queue(struct request_queue *q)
88 unsigned long flags;
90 spin_lock_irqsave(q->queue_lock, flags);
91 if (!blk_queue_stopped(q))
92 blk_stop_queue(q);
93 spin_unlock_irqrestore(q->queue_lock, flags);
96 static void dm_mq_stop_queue(struct request_queue *q)
98 if (blk_mq_queue_stopped(q))
99 return;
101 blk_mq_quiesce_queue(q);
104 void dm_stop_queue(struct request_queue *q)
106 if (!q->mq_ops)
107 dm_old_stop_queue(q);
108 else
109 dm_mq_stop_queue(q);
113 * Partial completion handling for request-based dm
115 static void end_clone_bio(struct bio *clone)
117 struct dm_rq_clone_bio_info *info =
118 container_of(clone, struct dm_rq_clone_bio_info, clone);
119 struct dm_rq_target_io *tio = info->tio;
120 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
121 blk_status_t error = clone->bi_status;
122 bool is_last = !clone->bi_next;
124 bio_put(clone);
126 if (tio->error)
128 * An error has already been detected on the request.
129 * Once error occurred, just let clone->end_io() handle
130 * the remainder.
132 return;
133 else if (error) {
135 * Don't notice the error to the upper layer yet.
136 * The error handling decision is made by the target driver,
137 * when the request is completed.
139 tio->error = error;
140 goto exit;
144 * I/O for the bio successfully completed.
145 * Notice the data completion to the upper layer.
147 tio->completed += nr_bytes;
150 * Update the original request.
151 * Do not use blk_end_request() here, because it may complete
152 * the original request before the clone, and break the ordering.
154 if (is_last)
155 exit:
156 blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
159 static struct dm_rq_target_io *tio_from_request(struct request *rq)
161 return blk_mq_rq_to_pdu(rq);
164 static void rq_end_stats(struct mapped_device *md, struct request *orig)
166 if (unlikely(dm_stats_used(&md->stats))) {
167 struct dm_rq_target_io *tio = tio_from_request(orig);
168 tio->duration_jiffies = jiffies - tio->duration_jiffies;
169 dm_stats_account_io(&md->stats, rq_data_dir(orig),
170 blk_rq_pos(orig), tio->n_sectors, true,
171 tio->duration_jiffies, &tio->stats_aux);
176 * Don't touch any member of the md after calling this function because
177 * the md may be freed in dm_put() at the end of this function.
178 * Or do dm_get() before calling this function and dm_put() later.
180 static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
182 struct request_queue *q = md->queue;
183 unsigned long flags;
185 atomic_dec(&md->pending[rw]);
187 /* nudge anyone waiting on suspend queue */
188 if (!md_in_flight(md))
189 wake_up(&md->wait);
192 * Run this off this callpath, as drivers could invoke end_io while
193 * inside their request_fn (and holding the queue lock). Calling
194 * back into ->request_fn() could deadlock attempting to grab the
195 * queue lock again.
197 if (!q->mq_ops && run_queue) {
198 spin_lock_irqsave(q->queue_lock, flags);
199 blk_run_queue_async(q);
200 spin_unlock_irqrestore(q->queue_lock, flags);
204 * dm_put() must be at the end of this function. See the comment above
206 dm_put(md);
210 * Complete the clone and the original request.
211 * Must be called without clone's queue lock held,
212 * see end_clone_request() for more details.
214 static void dm_end_request(struct request *clone, blk_status_t error)
216 int rw = rq_data_dir(clone);
217 struct dm_rq_target_io *tio = clone->end_io_data;
218 struct mapped_device *md = tio->md;
219 struct request *rq = tio->orig;
221 blk_rq_unprep_clone(clone);
222 tio->ti->type->release_clone_rq(clone, NULL);
224 rq_end_stats(md, rq);
225 if (!rq->q->mq_ops)
226 blk_end_request_all(rq, error);
227 else
228 blk_mq_end_request(rq, error);
229 rq_completed(md, rw, true);
233 * Requeue the original request of a clone.
235 static void dm_old_requeue_request(struct request *rq, unsigned long delay_ms)
237 struct request_queue *q = rq->q;
238 unsigned long flags;
240 spin_lock_irqsave(q->queue_lock, flags);
241 blk_requeue_request(q, rq);
242 blk_delay_queue(q, delay_ms);
243 spin_unlock_irqrestore(q->queue_lock, flags);
246 static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
248 blk_mq_delay_kick_requeue_list(q, msecs);
251 void dm_mq_kick_requeue_list(struct mapped_device *md)
253 __dm_mq_kick_requeue_list(dm_get_md_queue(md), 0);
255 EXPORT_SYMBOL(dm_mq_kick_requeue_list);
257 static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
259 blk_mq_requeue_request(rq, false);
260 __dm_mq_kick_requeue_list(rq->q, msecs);
263 static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
265 struct mapped_device *md = tio->md;
266 struct request *rq = tio->orig;
267 int rw = rq_data_dir(rq);
268 unsigned long delay_ms = delay_requeue ? 100 : 0;
270 rq_end_stats(md, rq);
271 if (tio->clone) {
272 blk_rq_unprep_clone(tio->clone);
273 tio->ti->type->release_clone_rq(tio->clone, NULL);
276 if (!rq->q->mq_ops)
277 dm_old_requeue_request(rq, delay_ms);
278 else
279 dm_mq_delay_requeue_request(rq, delay_ms);
281 rq_completed(md, rw, false);
284 static void dm_done(struct request *clone, blk_status_t error, bool mapped)
286 int r = DM_ENDIO_DONE;
287 struct dm_rq_target_io *tio = clone->end_io_data;
288 dm_request_endio_fn rq_end_io = NULL;
290 if (tio->ti) {
291 rq_end_io = tio->ti->type->rq_end_io;
293 if (mapped && rq_end_io)
294 r = rq_end_io(tio->ti, clone, error, &tio->info);
297 if (unlikely(error == BLK_STS_TARGET)) {
298 if (req_op(clone) == REQ_OP_DISCARD &&
299 !clone->q->limits.max_discard_sectors)
300 disable_discard(tio->md);
301 else if (req_op(clone) == REQ_OP_WRITE_SAME &&
302 !clone->q->limits.max_write_same_sectors)
303 disable_write_same(tio->md);
304 else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
305 !clone->q->limits.max_write_zeroes_sectors)
306 disable_write_zeroes(tio->md);
309 switch (r) {
310 case DM_ENDIO_DONE:
311 /* The target wants to complete the I/O */
312 dm_end_request(clone, error);
313 break;
314 case DM_ENDIO_INCOMPLETE:
315 /* The target will handle the I/O */
316 return;
317 case DM_ENDIO_REQUEUE:
318 /* The target wants to requeue the I/O */
319 dm_requeue_original_request(tio, false);
320 break;
321 case DM_ENDIO_DELAY_REQUEUE:
322 /* The target wants to requeue the I/O after a delay */
323 dm_requeue_original_request(tio, true);
324 break;
325 default:
326 DMWARN("unimplemented target endio return value: %d", r);
327 BUG();
332 * Request completion handler for request-based dm
334 static void dm_softirq_done(struct request *rq)
336 bool mapped = true;
337 struct dm_rq_target_io *tio = tio_from_request(rq);
338 struct request *clone = tio->clone;
339 int rw;
341 if (!clone) {
342 struct mapped_device *md = tio->md;
344 rq_end_stats(md, rq);
345 rw = rq_data_dir(rq);
346 if (!rq->q->mq_ops)
347 blk_end_request_all(rq, tio->error);
348 else
349 blk_mq_end_request(rq, tio->error);
350 rq_completed(md, rw, false);
351 return;
354 if (rq->rq_flags & RQF_FAILED)
355 mapped = false;
357 dm_done(clone, tio->error, mapped);
361 * Complete the clone and the original request with the error status
362 * through softirq context.
364 static void dm_complete_request(struct request *rq, blk_status_t error)
366 struct dm_rq_target_io *tio = tio_from_request(rq);
368 tio->error = error;
369 if (!rq->q->mq_ops)
370 blk_complete_request(rq);
371 else
372 blk_mq_complete_request(rq);
376 * Complete the not-mapped clone and the original request with the error status
377 * through softirq context.
378 * Target's rq_end_io() function isn't called.
379 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
381 static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
383 rq->rq_flags |= RQF_FAILED;
384 dm_complete_request(rq, error);
388 * Called with the clone's queue lock held (in the case of .request_fn)
390 static void end_clone_request(struct request *clone, blk_status_t error)
392 struct dm_rq_target_io *tio = clone->end_io_data;
395 * Actual request completion is done in a softirq context which doesn't
396 * hold the clone's queue lock. Otherwise, deadlock could occur because:
397 * - another request may be submitted by the upper level driver
398 * of the stacking during the completion
399 * - the submission which requires queue lock may be done
400 * against this clone's queue
402 dm_complete_request(tio->orig, error);
405 static blk_status_t dm_dispatch_clone_request(struct request *clone, struct request *rq)
407 blk_status_t r;
409 if (blk_queue_io_stat(clone->q))
410 clone->rq_flags |= RQF_IO_STAT;
412 clone->start_time_ns = ktime_get_ns();
413 r = blk_insert_cloned_request(clone->q, clone);
414 if (r != BLK_STS_OK && r != BLK_STS_RESOURCE && r != BLK_STS_DEV_RESOURCE)
415 /* must complete clone in terms of original request */
416 dm_complete_request(rq, r);
417 return r;
420 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
421 void *data)
423 struct dm_rq_target_io *tio = data;
424 struct dm_rq_clone_bio_info *info =
425 container_of(bio, struct dm_rq_clone_bio_info, clone);
427 info->orig = bio_orig;
428 info->tio = tio;
429 bio->bi_end_io = end_clone_bio;
431 return 0;
434 static int setup_clone(struct request *clone, struct request *rq,
435 struct dm_rq_target_io *tio, gfp_t gfp_mask)
437 int r;
439 r = blk_rq_prep_clone(clone, rq, &tio->md->bs, gfp_mask,
440 dm_rq_bio_constructor, tio);
441 if (r)
442 return r;
444 clone->end_io = end_clone_request;
445 clone->end_io_data = tio;
447 tio->clone = clone;
449 return 0;
452 static void map_tio_request(struct kthread_work *work);
454 static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
455 struct mapped_device *md)
457 tio->md = md;
458 tio->ti = NULL;
459 tio->clone = NULL;
460 tio->orig = rq;
461 tio->error = 0;
462 tio->completed = 0;
464 * Avoid initializing info for blk-mq; it passes
465 * target-specific data through info.ptr
466 * (see: dm_mq_init_request)
468 if (!md->init_tio_pdu)
469 memset(&tio->info, 0, sizeof(tio->info));
470 if (md->kworker_task)
471 kthread_init_work(&tio->work, map_tio_request);
475 * Returns:
476 * DM_MAPIO_* : the request has been processed as indicated
477 * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
478 * < 0 : the request was completed due to failure
480 static int map_request(struct dm_rq_target_io *tio)
482 int r;
483 struct dm_target *ti = tio->ti;
484 struct mapped_device *md = tio->md;
485 struct request *rq = tio->orig;
486 struct request *clone = NULL;
487 blk_status_t ret;
489 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
490 check_again:
491 switch (r) {
492 case DM_MAPIO_SUBMITTED:
493 /* The target has taken the I/O to submit by itself later */
494 break;
495 case DM_MAPIO_REMAPPED:
496 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
497 /* -ENOMEM */
498 ti->type->release_clone_rq(clone, &tio->info);
499 return DM_MAPIO_REQUEUE;
502 /* The target has remapped the I/O so dispatch it */
503 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
504 blk_rq_pos(rq));
505 ret = dm_dispatch_clone_request(clone, rq);
506 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
507 blk_rq_unprep_clone(clone);
508 blk_mq_cleanup_rq(clone);
509 tio->ti->type->release_clone_rq(clone, &tio->info);
510 tio->clone = NULL;
511 if (!rq->q->mq_ops)
512 r = DM_MAPIO_DELAY_REQUEUE;
513 else
514 r = DM_MAPIO_REQUEUE;
515 goto check_again;
517 break;
518 case DM_MAPIO_REQUEUE:
519 /* The target wants to requeue the I/O */
520 break;
521 case DM_MAPIO_DELAY_REQUEUE:
522 /* The target wants to requeue the I/O after a delay */
523 dm_requeue_original_request(tio, true);
524 break;
525 case DM_MAPIO_KILL:
526 /* The target wants to complete the I/O */
527 dm_kill_unmapped_request(rq, BLK_STS_IOERR);
528 break;
529 default:
530 DMWARN("unimplemented target map return value: %d", r);
531 BUG();
534 return r;
537 static void dm_start_request(struct mapped_device *md, struct request *orig)
539 if (!orig->q->mq_ops)
540 blk_start_request(orig);
541 else
542 blk_mq_start_request(orig);
543 atomic_inc(&md->pending[rq_data_dir(orig)]);
545 if (md->seq_rq_merge_deadline_usecs) {
546 md->last_rq_pos = rq_end_sector(orig);
547 md->last_rq_rw = rq_data_dir(orig);
548 md->last_rq_start_time = ktime_get();
551 if (unlikely(dm_stats_used(&md->stats))) {
552 struct dm_rq_target_io *tio = tio_from_request(orig);
553 tio->duration_jiffies = jiffies;
554 tio->n_sectors = blk_rq_sectors(orig);
555 dm_stats_account_io(&md->stats, rq_data_dir(orig),
556 blk_rq_pos(orig), tio->n_sectors, false, 0,
557 &tio->stats_aux);
561 * Hold the md reference here for the in-flight I/O.
562 * We can't rely on the reference count by device opener,
563 * because the device may be closed during the request completion
564 * when all bios are completed.
565 * See the comment in rq_completed() too.
567 dm_get(md);
570 static int __dm_rq_init_rq(struct mapped_device *md, struct request *rq)
572 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
575 * Must initialize md member of tio, otherwise it won't
576 * be available in dm_mq_queue_rq.
578 tio->md = md;
580 if (md->init_tio_pdu) {
581 /* target-specific per-io data is immediately after the tio */
582 tio->info.ptr = tio + 1;
585 return 0;
588 static int dm_rq_init_rq(struct request_queue *q, struct request *rq, gfp_t gfp)
590 return __dm_rq_init_rq(q->rq_alloc_data, rq);
593 static void map_tio_request(struct kthread_work *work)
595 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
597 if (map_request(tio) == DM_MAPIO_REQUEUE)
598 dm_requeue_original_request(tio, false);
601 ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
603 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
606 #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
608 ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
609 const char *buf, size_t count)
611 unsigned deadline;
613 if (dm_get_md_type(md) != DM_TYPE_REQUEST_BASED)
614 return count;
616 if (kstrtouint(buf, 10, &deadline))
617 return -EINVAL;
619 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
620 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
622 md->seq_rq_merge_deadline_usecs = deadline;
624 return count;
627 static bool dm_old_request_peeked_before_merge_deadline(struct mapped_device *md)
629 ktime_t kt_deadline;
631 if (!md->seq_rq_merge_deadline_usecs)
632 return false;
634 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
635 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
637 return !ktime_after(ktime_get(), kt_deadline);
641 * q->request_fn for old request-based dm.
642 * Called with the queue lock held.
644 static void dm_old_request_fn(struct request_queue *q)
646 struct mapped_device *md = q->queuedata;
647 struct dm_target *ti = md->immutable_target;
648 struct request *rq;
649 struct dm_rq_target_io *tio;
650 sector_t pos = 0;
652 if (unlikely(!ti)) {
653 int srcu_idx;
654 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
656 if (unlikely(!map)) {
657 dm_put_live_table(md, srcu_idx);
658 return;
660 ti = dm_table_find_target(map, pos);
661 dm_put_live_table(md, srcu_idx);
665 * For suspend, check blk_queue_stopped() and increment
666 * ->pending within a single queue_lock not to increment the
667 * number of in-flight I/Os after the queue is stopped in
668 * dm_suspend().
670 while (!blk_queue_stopped(q)) {
671 rq = blk_peek_request(q);
672 if (!rq)
673 return;
675 /* always use block 0 to find the target for flushes for now */
676 pos = 0;
677 if (req_op(rq) != REQ_OP_FLUSH)
678 pos = blk_rq_pos(rq);
680 if ((dm_old_request_peeked_before_merge_deadline(md) &&
681 md_in_flight(md) && rq->bio && !bio_multiple_segments(rq->bio) &&
682 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
683 (ti->type->busy && ti->type->busy(ti))) {
684 blk_delay_queue(q, 10);
685 return;
688 dm_start_request(md, rq);
690 tio = tio_from_request(rq);
691 init_tio(tio, rq, md);
692 /* Establish tio->ti before queuing work (map_tio_request) */
693 tio->ti = ti;
694 kthread_queue_work(&md->kworker, &tio->work);
695 BUG_ON(!irqs_disabled());
700 * Fully initialize a .request_fn request-based queue.
702 int dm_old_init_request_queue(struct mapped_device *md, struct dm_table *t)
704 struct dm_target *immutable_tgt;
706 /* Fully initialize the queue */
707 md->queue->cmd_size = sizeof(struct dm_rq_target_io);
708 md->queue->rq_alloc_data = md;
709 md->queue->request_fn = dm_old_request_fn;
710 md->queue->init_rq_fn = dm_rq_init_rq;
712 immutable_tgt = dm_table_get_immutable_target(t);
713 if (immutable_tgt && immutable_tgt->per_io_data_size) {
714 /* any target-specific per-io data is immediately after the tio */
715 md->queue->cmd_size += immutable_tgt->per_io_data_size;
716 md->init_tio_pdu = true;
718 if (blk_init_allocated_queue(md->queue) < 0)
719 return -EINVAL;
721 /* disable dm_old_request_fn's merge heuristic by default */
722 md->seq_rq_merge_deadline_usecs = 0;
724 blk_queue_softirq_done(md->queue, dm_softirq_done);
726 /* Initialize the request-based DM worker thread */
727 kthread_init_worker(&md->kworker);
728 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
729 "kdmwork-%s", dm_device_name(md));
730 if (IS_ERR(md->kworker_task)) {
731 int error = PTR_ERR(md->kworker_task);
732 md->kworker_task = NULL;
733 return error;
736 return 0;
739 static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
740 unsigned int hctx_idx, unsigned int numa_node)
742 return __dm_rq_init_rq(set->driver_data, rq);
745 static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
746 const struct blk_mq_queue_data *bd)
748 struct request *rq = bd->rq;
749 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
750 struct mapped_device *md = tio->md;
751 struct dm_target *ti = md->immutable_target;
753 if (unlikely(!ti)) {
754 int srcu_idx;
755 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
757 ti = dm_table_find_target(map, 0);
758 dm_put_live_table(md, srcu_idx);
761 if (ti->type->busy && ti->type->busy(ti))
762 return BLK_STS_RESOURCE;
764 dm_start_request(md, rq);
766 /* Init tio using md established in .init_request */
767 init_tio(tio, rq, md);
770 * Establish tio->ti before calling map_request().
772 tio->ti = ti;
774 /* Direct call is fine since .queue_rq allows allocations */
775 if (map_request(tio) == DM_MAPIO_REQUEUE) {
776 /* Undo dm_start_request() before requeuing */
777 rq_end_stats(md, rq);
778 rq_completed(md, rq_data_dir(rq), false);
779 return BLK_STS_RESOURCE;
782 return BLK_STS_OK;
785 static const struct blk_mq_ops dm_mq_ops = {
786 .queue_rq = dm_mq_queue_rq,
787 .complete = dm_softirq_done,
788 .init_request = dm_mq_init_request,
791 int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
793 struct request_queue *q;
794 struct dm_target *immutable_tgt;
795 int err;
797 if (!dm_table_all_blk_mq_devices(t)) {
798 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
799 return -EINVAL;
802 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
803 if (!md->tag_set)
804 return -ENOMEM;
806 md->tag_set->ops = &dm_mq_ops;
807 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
808 md->tag_set->numa_node = md->numa_node_id;
809 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
810 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
811 md->tag_set->driver_data = md;
813 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
814 immutable_tgt = dm_table_get_immutable_target(t);
815 if (immutable_tgt && immutable_tgt->per_io_data_size) {
816 /* any target-specific per-io data is immediately after the tio */
817 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
818 md->init_tio_pdu = true;
821 err = blk_mq_alloc_tag_set(md->tag_set);
822 if (err)
823 goto out_kfree_tag_set;
825 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
826 if (IS_ERR(q)) {
827 err = PTR_ERR(q);
828 goto out_tag_set;
831 return 0;
833 out_tag_set:
834 blk_mq_free_tag_set(md->tag_set);
835 out_kfree_tag_set:
836 kfree(md->tag_set);
838 return err;
841 void dm_mq_cleanup_mapped_device(struct mapped_device *md)
843 if (md->tag_set) {
844 blk_mq_free_tag_set(md->tag_set);
845 kfree(md->tag_set);
849 module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
850 MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
852 module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
853 MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
855 module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
856 MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
858 module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
859 MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");