dm: add argument identifier names
[linux-block.git] / drivers / md / dm-rq.c
CommitLineData
3bd94003 1// SPDX-License-Identifier: GPL-2.0-only
4cc96131
MS
2/*
3 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-core.h"
9#include "dm-rq.h"
10
4cc96131
MS
11#include <linux/blk-mq.h>
12
13#define DM_MSG_PREFIX "core-rq"
14
e689fbab
MS
15/*
16 * One of these is allocated per request.
17 */
18struct dm_rq_target_io {
19 struct mapped_device *md;
20 struct dm_target *ti;
21 struct request *orig, *clone;
22 struct kthread_work work;
23 blk_status_t error;
24 union map_info info;
25 struct dm_stats_aux stats_aux;
26 unsigned long duration_jiffies;
86a3238c
HM
27 unsigned int n_sectors;
28 unsigned int completed;
e689fbab
MS
29};
30
4cc96131
MS
31#define DM_MQ_NR_HW_QUEUES 1
32#define DM_MQ_QUEUE_DEPTH 2048
86a3238c
HM
33static unsigned int dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
34static unsigned int dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
4cc96131
MS
35
36/*
37 * Request-based DM's mempools' reserved IOs set by the user.
38 */
39#define RESERVED_REQUEST_BASED_IOS 256
86a3238c 40static unsigned int reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
4cc96131 41
86a3238c 42unsigned int dm_get_reserved_rq_based_ios(void)
4cc96131
MS
43{
44 return __dm_get_module_param(&reserved_rq_based_ios,
45 RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
46}
4cc96131 47
86a3238c 48static unsigned int dm_get_blk_mq_nr_hw_queues(void)
4cc96131
MS
49{
50 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
51}
52
86a3238c 53static unsigned int dm_get_blk_mq_queue_depth(void)
4cc96131
MS
54{
55 return __dm_get_module_param(&dm_mq_queue_depth,
56 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
57}
58
59int dm_request_based(struct mapped_device *md)
60{
344e9ffc 61 return queue_is_mq(md->queue);
4cc96131
MS
62}
63
6a23e05c 64void dm_start_queue(struct request_queue *q)
9dbeaeab 65{
f660174e 66 blk_mq_unquiesce_queue(q);
9dbeaeab
MS
67 blk_mq_kick_requeue_list(q);
68}
69
6a23e05c 70void dm_stop_queue(struct request_queue *q)
2397a15a 71{
7b17c2f7 72 blk_mq_quiesce_queue(q);
4cc96131
MS
73}
74
4cc96131
MS
75/*
76 * Partial completion handling for request-based dm
77 */
78static void end_clone_bio(struct bio *clone)
79{
80 struct dm_rq_clone_bio_info *info =
81 container_of(clone, struct dm_rq_clone_bio_info, clone);
82 struct dm_rq_target_io *tio = info->tio;
4cc96131 83 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
4e4cbee9 84 blk_status_t error = clone->bi_status;
dc6364b5 85 bool is_last = !clone->bi_next;
4cc96131
MS
86
87 bio_put(clone);
88
89 if (tio->error)
90 /*
91 * An error has already been detected on the request.
92 * Once error occurred, just let clone->end_io() handle
93 * the remainder.
94 */
95 return;
96 else if (error) {
97 /*
98 * Don't notice the error to the upper layer yet.
99 * The error handling decision is made by the target driver,
100 * when the request is completed.
101 */
102 tio->error = error;
dc6364b5 103 goto exit;
4cc96131
MS
104 }
105
106 /*
107 * I/O for the bio successfully completed.
108 * Notice the data completion to the upper layer.
109 */
dc6364b5 110 tio->completed += nr_bytes;
4cc96131
MS
111
112 /*
113 * Update the original request.
d370ad23 114 * Do not use blk_mq_end_request() here, because it may complete
4cc96131
MS
115 * the original request before the clone, and break the ordering.
116 */
dc6364b5
ML
117 if (is_last)
118 exit:
119 blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
4cc96131
MS
120}
121
122static struct dm_rq_target_io *tio_from_request(struct request *rq)
123{
eb8db831 124 return blk_mq_rq_to_pdu(rq);
4cc96131
MS
125}
126
127static void rq_end_stats(struct mapped_device *md, struct request *orig)
128{
129 if (unlikely(dm_stats_used(&md->stats))) {
130 struct dm_rq_target_io *tio = tio_from_request(orig);
131 tio->duration_jiffies = jiffies - tio->duration_jiffies;
132 dm_stats_account_io(&md->stats, rq_data_dir(orig),
133 blk_rq_pos(orig), tio->n_sectors, true,
134 tio->duration_jiffies, &tio->stats_aux);
135 }
136}
137
138/*
139 * Don't touch any member of the md after calling this function because
140 * the md may be freed in dm_put() at the end of this function.
141 * Or do dm_get() before calling this function and dm_put() later.
142 */
2adc5c55 143static void rq_completed(struct mapped_device *md)
4cc96131 144{
4cc96131
MS
145 /*
146 * dm_put() must be at the end of this function. See the comment above
147 */
148 dm_put(md);
149}
150
4cc96131
MS
151/*
152 * Complete the clone and the original request.
153 * Must be called without clone's queue lock held,
154 * see end_clone_request() for more details.
155 */
2a842aca 156static void dm_end_request(struct request *clone, blk_status_t error)
4cc96131 157{
4cc96131
MS
158 struct dm_rq_target_io *tio = clone->end_io_data;
159 struct mapped_device *md = tio->md;
160 struct request *rq = tio->orig;
161
eb8db831 162 blk_rq_unprep_clone(clone);
5de719e3 163 tio->ti->type->release_clone_rq(clone, NULL);
4cc96131 164
4cc96131 165 rq_end_stats(md, rq);
6a23e05c 166 blk_mq_end_request(rq, error);
2adc5c55 167 rq_completed(md);
4cc96131
MS
168}
169
e0c10752 170static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
4cc96131 171{
52d7f1b5 172 blk_mq_delay_kick_requeue_list(q, msecs);
4cc96131
MS
173}
174
e0c10752
MS
175void dm_mq_kick_requeue_list(struct mapped_device *md)
176{
33bd6f06 177 __dm_mq_kick_requeue_list(md->queue, 0);
e0c10752
MS
178}
179EXPORT_SYMBOL(dm_mq_kick_requeue_list);
180
181static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
182{
2b053aca 183 blk_mq_requeue_request(rq, false);
e0c10752
MS
184 __dm_mq_kick_requeue_list(rq->q, msecs);
185}
186
fbc39b4c 187static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
4cc96131 188{
fbc39b4c
MS
189 struct mapped_device *md = tio->md;
190 struct request *rq = tio->orig;
d5c27f3f 191 unsigned long delay_ms = delay_requeue ? 100 : 0;
4cc96131
MS
192
193 rq_end_stats(md, rq);
eb8db831
CH
194 if (tio->clone) {
195 blk_rq_unprep_clone(tio->clone);
5de719e3 196 tio->ti->type->release_clone_rq(tio->clone, NULL);
eb8db831 197 }
4cc96131 198
6a23e05c 199 dm_mq_delay_requeue_request(rq, delay_ms);
2adc5c55 200 rq_completed(md);
4cc96131
MS
201}
202
2a842aca 203static void dm_done(struct request *clone, blk_status_t error, bool mapped)
4cc96131 204{
7ed8578a 205 int r = DM_ENDIO_DONE;
4cc96131
MS
206 struct dm_rq_target_io *tio = clone->end_io_data;
207 dm_request_endio_fn rq_end_io = NULL;
208
209 if (tio->ti) {
210 rq_end_io = tio->ti->type->rq_end_io;
211
212 if (mapped && rq_end_io)
213 r = rq_end_io(tio->ti, clone, error, &tio->info);
214 }
215
2a842aca 216 if (unlikely(error == BLK_STS_TARGET)) {
bcb44433
MS
217 if (req_op(clone) == REQ_OP_DISCARD &&
218 !clone->q->limits.max_discard_sectors)
219 disable_discard(tio->md);
bcb44433
MS
220 else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
221 !clone->q->limits.max_write_zeroes_sectors)
ac62d620
CH
222 disable_write_zeroes(tio->md);
223 }
4cc96131 224
7ed8578a
CH
225 switch (r) {
226 case DM_ENDIO_DONE:
4cc96131 227 /* The target wants to complete the I/O */
7ed8578a
CH
228 dm_end_request(clone, error);
229 break;
230 case DM_ENDIO_INCOMPLETE:
4cc96131
MS
231 /* The target will handle the I/O */
232 return;
7ed8578a 233 case DM_ENDIO_REQUEUE:
4cc96131 234 /* The target wants to requeue the I/O */
fbc39b4c 235 dm_requeue_original_request(tio, false);
7ed8578a 236 break;
ac514ffc
MS
237 case DM_ENDIO_DELAY_REQUEUE:
238 /* The target wants to requeue the I/O after a delay */
239 dm_requeue_original_request(tio, true);
240 break;
7ed8578a 241 default:
43e6c111 242 DMCRIT("unimplemented target endio return value: %d", r);
4cc96131
MS
243 BUG();
244 }
245}
246
247/*
248 * Request completion handler for request-based dm
249 */
250static void dm_softirq_done(struct request *rq)
251{
252 bool mapped = true;
253 struct dm_rq_target_io *tio = tio_from_request(rq);
254 struct request *clone = tio->clone;
4cc96131
MS
255
256 if (!clone) {
61febef4
JA
257 struct mapped_device *md = tio->md;
258
259 rq_end_stats(md, rq);
6a23e05c 260 blk_mq_end_request(rq, tio->error);
2adc5c55 261 rq_completed(md);
4cc96131
MS
262 return;
263 }
264
e8064021 265 if (rq->rq_flags & RQF_FAILED)
4cc96131
MS
266 mapped = false;
267
268 dm_done(clone, tio->error, mapped);
269}
270
271/*
272 * Complete the clone and the original request with the error status
273 * through softirq context.
274 */
2a842aca 275static void dm_complete_request(struct request *rq, blk_status_t error)
4cc96131
MS
276{
277 struct dm_rq_target_io *tio = tio_from_request(rq);
278
279 tio->error = error;
15f73f5b
CH
280 if (likely(!blk_should_fake_timeout(rq->q)))
281 blk_mq_complete_request(rq);
4cc96131
MS
282}
283
284/*
285 * Complete the not-mapped clone and the original request with the error status
286 * through softirq context.
287 * Target's rq_end_io() function isn't called.
6a23e05c 288 * This may be used when the target's clone_and_map_rq() function fails.
4cc96131 289 */
2a842aca 290static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
4cc96131 291{
e8064021 292 rq->rq_flags |= RQF_FAILED;
4cc96131
MS
293 dm_complete_request(rq, error);
294}
295
de671d61
JA
296static enum rq_end_io_ret end_clone_request(struct request *clone,
297 blk_status_t error)
4cc96131
MS
298{
299 struct dm_rq_target_io *tio = clone->end_io_data;
300
4cc96131 301 dm_complete_request(tio->orig, error);
de671d61 302 return RQ_END_IO_NONE;
4cc96131
MS
303}
304
4cc96131
MS
305static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
306 void *data)
307{
308 struct dm_rq_target_io *tio = data;
309 struct dm_rq_clone_bio_info *info =
310 container_of(bio, struct dm_rq_clone_bio_info, clone);
311
312 info->orig = bio_orig;
313 info->tio = tio;
314 bio->bi_end_io = end_clone_bio;
315
316 return 0;
317}
318
319static int setup_clone(struct request *clone, struct request *rq,
320 struct dm_rq_target_io *tio, gfp_t gfp_mask)
321{
322 int r;
323
29dec90a 324 r = blk_rq_prep_clone(clone, rq, &tio->md->mempools->bs, gfp_mask,
4cc96131
MS
325 dm_rq_bio_constructor, tio);
326 if (r)
327 return r;
328
4cc96131
MS
329 clone->end_io = end_clone_request;
330 clone->end_io_data = tio;
331
332 tio->clone = clone;
333
334 return 0;
335}
336
4cc96131
MS
337static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
338 struct mapped_device *md)
339{
340 tio->md = md;
341 tio->ti = NULL;
342 tio->clone = NULL;
343 tio->orig = rq;
344 tio->error = 0;
dc6364b5 345 tio->completed = 0;
4cc96131
MS
346 /*
347 * Avoid initializing info for blk-mq; it passes
348 * target-specific data through info.ptr
349 * (see: dm_mq_init_request)
350 */
351 if (!md->init_tio_pdu)
352 memset(&tio->info, 0, sizeof(tio->info));
4cc96131
MS
353}
354
4cc96131
MS
355/*
356 * Returns:
a8ac51e4
MS
357 * DM_MAPIO_* : the request has been processed as indicated
358 * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
4cc96131
MS
359 * < 0 : the request was completed due to failure
360 */
fbc39b4c 361static int map_request(struct dm_rq_target_io *tio)
4cc96131
MS
362{
363 int r;
364 struct dm_target *ti = tio->ti;
fbc39b4c
MS
365 struct mapped_device *md = tio->md;
366 struct request *rq = tio->orig;
4cc96131 367 struct request *clone = NULL;
396eaf21 368 blk_status_t ret;
4cc96131 369
eb8db831 370 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
4cc96131
MS
371 switch (r) {
372 case DM_MAPIO_SUBMITTED:
373 /* The target has taken the I/O to submit by itself later */
374 break;
375 case DM_MAPIO_REMAPPED:
eb8db831
CH
376 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
377 /* -ENOMEM */
5de719e3 378 ti->type->release_clone_rq(clone, &tio->info);
eb8db831
CH
379 return DM_MAPIO_REQUEUE;
380 }
381
4cc96131 382 /* The target has remapped the I/O so dispatch it */
a54895fa 383 trace_block_rq_remap(clone, disk_devt(dm_disk(md)),
4cc96131 384 blk_rq_pos(rq));
9f9adea7
CH
385 ret = blk_insert_cloned_request(clone);
386 switch (ret) {
387 case BLK_STS_OK:
388 break;
389 case BLK_STS_RESOURCE:
390 case BLK_STS_DEV_RESOURCE:
396eaf21 391 blk_rq_unprep_clone(clone);
226b4fc7 392 blk_mq_cleanup_rq(clone);
5de719e3 393 tio->ti->type->release_clone_rq(clone, &tio->info);
396eaf21 394 tio->clone = NULL;
34743bfd 395 return DM_MAPIO_REQUEUE;
9f9adea7
CH
396 default:
397 /* must complete clone in terms of original request */
398 dm_complete_request(rq, ret);
396eaf21 399 }
4cc96131
MS
400 break;
401 case DM_MAPIO_REQUEUE:
402 /* The target wants to requeue the I/O */
a8ac51e4
MS
403 break;
404 case DM_MAPIO_DELAY_REQUEUE:
405 /* The target wants to requeue the I/O after a delay */
fbc39b4c 406 dm_requeue_original_request(tio, true);
4cc96131 407 break;
412445ac 408 case DM_MAPIO_KILL:
4cc96131 409 /* The target wants to complete the I/O */
2a842aca 410 dm_kill_unmapped_request(rq, BLK_STS_IOERR);
ece07280 411 break;
412445ac 412 default:
43e6c111 413 DMCRIT("unimplemented target map return value: %d", r);
412445ac 414 BUG();
4cc96131
MS
415 }
416
a8ac51e4 417 return r;
4cc96131
MS
418}
419
6a23e05c
JA
420/* DEPRECATED: previously used for request-based merge heuristic in dm_request_fn() */
421ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
422{
423 return sprintf(buf, "%u\n", 0);
424}
425
426ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
427 const char *buf, size_t count)
428{
429 return count;
430}
431
4cc96131
MS
432static void dm_start_request(struct mapped_device *md, struct request *orig)
433{
6a23e05c 434 blk_mq_start_request(orig);
4cc96131 435
4cc96131
MS
436 if (unlikely(dm_stats_used(&md->stats))) {
437 struct dm_rq_target_io *tio = tio_from_request(orig);
438 tio->duration_jiffies = jiffies;
439 tio->n_sectors = blk_rq_sectors(orig);
440 dm_stats_account_io(&md->stats, rq_data_dir(orig),
441 blk_rq_pos(orig), tio->n_sectors, false, 0,
442 &tio->stats_aux);
443 }
444
445 /*
446 * Hold the md reference here for the in-flight I/O.
447 * We can't rely on the reference count by device opener,
448 * because the device may be closed during the request completion
449 * when all bios are completed.
450 * See the comment in rq_completed() too.
451 */
452 dm_get(md);
453}
454
6a23e05c
JA
455static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
456 unsigned int hctx_idx, unsigned int numa_node)
eb8db831 457{
6a23e05c 458 struct mapped_device *md = set->driver_data;
eb8db831
CH
459 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
460
461 /*
462 * Must initialize md member of tio, otherwise it won't
463 * be available in dm_mq_queue_rq.
464 */
465 tio->md = md;
466
467 if (md->init_tio_pdu) {
468 /* target-specific per-io data is immediately after the tio */
469 tio->info.ptr = tio + 1;
470 }
471
472 return 0;
473}
474
fc17b653 475static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
4cc96131
MS
476 const struct blk_mq_queue_data *bd)
477{
478 struct request *rq = bd->rq;
479 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
480 struct mapped_device *md = tio->md;
481 struct dm_target *ti = md->immutable_target;
482
b4459b11
ML
483 /*
484 * blk-mq's unquiesce may come from outside events, such as
485 * elevator switch, updating nr_requests or others, and request may
486 * come during suspend, so simply ask for blk-mq to requeue it.
487 */
488 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)))
489 return BLK_STS_RESOURCE;
490
4cc96131
MS
491 if (unlikely(!ti)) {
492 int srcu_idx;
fa247089 493 struct dm_table *map;
4cc96131 494
fa247089
MS
495 map = dm_get_live_table(md, &srcu_idx);
496 if (unlikely(!map)) {
497 dm_put_live_table(md, srcu_idx);
498 return BLK_STS_RESOURCE;
499 }
4cc96131
MS
500 ti = dm_table_find_target(map, 0);
501 dm_put_live_table(md, srcu_idx);
502 }
503
504 if (ti->type->busy && ti->type->busy(ti))
fc17b653 505 return BLK_STS_RESOURCE;
4cc96131
MS
506
507 dm_start_request(md, rq);
508
509 /* Init tio using md established in .init_request */
510 init_tio(tio, rq, md);
511
512 /*
513 * Establish tio->ti before calling map_request().
514 */
515 tio->ti = ti;
516
517 /* Direct call is fine since .queue_rq allows allocations */
fbc39b4c 518 if (map_request(tio) == DM_MAPIO_REQUEUE) {
4cc96131
MS
519 /* Undo dm_start_request() before requeuing */
520 rq_end_stats(md, rq);
2adc5c55 521 rq_completed(md);
fc17b653 522 return BLK_STS_RESOURCE;
4cc96131
MS
523 }
524
fc17b653 525 return BLK_STS_OK;
4cc96131
MS
526}
527
f363b089 528static const struct blk_mq_ops dm_mq_ops = {
4cc96131 529 .queue_rq = dm_mq_queue_rq,
4cc96131
MS
530 .complete = dm_softirq_done,
531 .init_request = dm_mq_init_request,
532};
533
e83068a5 534int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
4cc96131 535{
e83068a5 536 struct dm_target *immutable_tgt;
4cc96131
MS
537 int err;
538
4cc96131
MS
539 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
540 if (!md->tag_set)
541 return -ENOMEM;
542
543 md->tag_set->ops = &dm_mq_ops;
544 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
545 md->tag_set->numa_node = md->numa_node_id;
bf0beec0 546 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING;
4cc96131
MS
547 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
548 md->tag_set->driver_data = md;
549
550 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
e83068a5 551 immutable_tgt = dm_table_get_immutable_target(t);
4cc96131
MS
552 if (immutable_tgt && immutable_tgt->per_io_data_size) {
553 /* any target-specific per-io data is immediately after the tio */
554 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
555 md->init_tio_pdu = true;
556 }
557
558 err = blk_mq_alloc_tag_set(md->tag_set);
559 if (err)
560 goto out_kfree_tag_set;
561
26a9750a
CH
562 err = blk_mq_init_allocated_queue(md->tag_set, md->queue);
563 if (err)
4cc96131 564 goto out_tag_set;
4cc96131
MS
565 return 0;
566
567out_tag_set:
568 blk_mq_free_tag_set(md->tag_set);
569out_kfree_tag_set:
570 kfree(md->tag_set);
8e947c8f 571 md->tag_set = NULL;
4cc96131
MS
572
573 return err;
574}
575
576void dm_mq_cleanup_mapped_device(struct mapped_device *md)
577{
578 if (md->tag_set) {
579 blk_mq_free_tag_set(md->tag_set);
580 kfree(md->tag_set);
8e947c8f 581 md->tag_set = NULL;
4cc96131
MS
582 }
583}
584
585module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
586MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
587
6a23e05c
JA
588/* Unused, but preserved for userspace compatibility */
589static bool use_blk_mq = true;
4cc96131
MS
590module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
591MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
592
593module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
594MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
595
596module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
597MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");