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