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