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