| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Block multiqueue core code |
| 4 | * |
| 5 | * Copyright (C) 2013-2014 Jens Axboe |
| 6 | * Copyright (C) 2013-2014 Christoph Hellwig |
| 7 | */ |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/backing-dev.h> |
| 11 | #include <linux/bio.h> |
| 12 | #include <linux/blkdev.h> |
| 13 | #include <linux/blk-integrity.h> |
| 14 | #include <linux/kmemleak.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/workqueue.h> |
| 19 | #include <linux/smp.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/llist.h> |
| 22 | #include <linux/cpu.h> |
| 23 | #include <linux/cache.h> |
| 24 | #include <linux/sched/sysctl.h> |
| 25 | #include <linux/sched/topology.h> |
| 26 | #include <linux/sched/signal.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/crash_dump.h> |
| 29 | #include <linux/prefetch.h> |
| 30 | #include <linux/blk-crypto.h> |
| 31 | #include <linux/part_stat.h> |
| 32 | |
| 33 | #include <trace/events/block.h> |
| 34 | |
| 35 | #include <linux/blk-mq.h> |
| 36 | #include <linux/t10-pi.h> |
| 37 | #include "blk.h" |
| 38 | #include "blk-mq.h" |
| 39 | #include "blk-mq-debugfs.h" |
| 40 | #include "blk-mq-tag.h" |
| 41 | #include "blk-pm.h" |
| 42 | #include "blk-stat.h" |
| 43 | #include "blk-mq-sched.h" |
| 44 | #include "blk-rq-qos.h" |
| 45 | |
| 46 | static DEFINE_PER_CPU(struct llist_head, blk_cpu_done); |
| 47 | |
| 48 | static void blk_mq_poll_stats_start(struct request_queue *q); |
| 49 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb); |
| 50 | |
| 51 | static int blk_mq_poll_stats_bkt(const struct request *rq) |
| 52 | { |
| 53 | int ddir, sectors, bucket; |
| 54 | |
| 55 | ddir = rq_data_dir(rq); |
| 56 | sectors = blk_rq_stats_sectors(rq); |
| 57 | |
| 58 | bucket = ddir + 2 * ilog2(sectors); |
| 59 | |
| 60 | if (bucket < 0) |
| 61 | return -1; |
| 62 | else if (bucket >= BLK_MQ_POLL_STATS_BKTS) |
| 63 | return ddir + BLK_MQ_POLL_STATS_BKTS - 2; |
| 64 | |
| 65 | return bucket; |
| 66 | } |
| 67 | |
| 68 | #define BLK_QC_T_SHIFT 16 |
| 69 | #define BLK_QC_T_INTERNAL (1U << 31) |
| 70 | |
| 71 | static inline struct blk_mq_hw_ctx *blk_qc_to_hctx(struct request_queue *q, |
| 72 | blk_qc_t qc) |
| 73 | { |
| 74 | return xa_load(&q->hctx_table, |
| 75 | (qc & ~BLK_QC_T_INTERNAL) >> BLK_QC_T_SHIFT); |
| 76 | } |
| 77 | |
| 78 | static inline struct request *blk_qc_to_rq(struct blk_mq_hw_ctx *hctx, |
| 79 | blk_qc_t qc) |
| 80 | { |
| 81 | unsigned int tag = qc & ((1U << BLK_QC_T_SHIFT) - 1); |
| 82 | |
| 83 | if (qc & BLK_QC_T_INTERNAL) |
| 84 | return blk_mq_tag_to_rq(hctx->sched_tags, tag); |
| 85 | return blk_mq_tag_to_rq(hctx->tags, tag); |
| 86 | } |
| 87 | |
| 88 | static inline blk_qc_t blk_rq_to_qc(struct request *rq) |
| 89 | { |
| 90 | return (rq->mq_hctx->queue_num << BLK_QC_T_SHIFT) | |
| 91 | (rq->tag != -1 ? |
| 92 | rq->tag : (rq->internal_tag | BLK_QC_T_INTERNAL)); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Check if any of the ctx, dispatch list or elevator |
| 97 | * have pending work in this hardware queue. |
| 98 | */ |
| 99 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
| 100 | { |
| 101 | return !list_empty_careful(&hctx->dispatch) || |
| 102 | sbitmap_any_bit_set(&hctx->ctx_map) || |
| 103 | blk_mq_sched_has_work(hctx); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Mark this ctx as having pending work in this hardware queue |
| 108 | */ |
| 109 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, |
| 110 | struct blk_mq_ctx *ctx) |
| 111 | { |
| 112 | const int bit = ctx->index_hw[hctx->type]; |
| 113 | |
| 114 | if (!sbitmap_test_bit(&hctx->ctx_map, bit)) |
| 115 | sbitmap_set_bit(&hctx->ctx_map, bit); |
| 116 | } |
| 117 | |
| 118 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, |
| 119 | struct blk_mq_ctx *ctx) |
| 120 | { |
| 121 | const int bit = ctx->index_hw[hctx->type]; |
| 122 | |
| 123 | sbitmap_clear_bit(&hctx->ctx_map, bit); |
| 124 | } |
| 125 | |
| 126 | struct mq_inflight { |
| 127 | struct block_device *part; |
| 128 | unsigned int inflight[2]; |
| 129 | }; |
| 130 | |
| 131 | static bool blk_mq_check_inflight(struct request *rq, void *priv, |
| 132 | bool reserved) |
| 133 | { |
| 134 | struct mq_inflight *mi = priv; |
| 135 | |
| 136 | if ((!mi->part->bd_partno || rq->part == mi->part) && |
| 137 | blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT) |
| 138 | mi->inflight[rq_data_dir(rq)]++; |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | unsigned int blk_mq_in_flight(struct request_queue *q, |
| 144 | struct block_device *part) |
| 145 | { |
| 146 | struct mq_inflight mi = { .part = part }; |
| 147 | |
| 148 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); |
| 149 | |
| 150 | return mi.inflight[0] + mi.inflight[1]; |
| 151 | } |
| 152 | |
| 153 | void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part, |
| 154 | unsigned int inflight[2]) |
| 155 | { |
| 156 | struct mq_inflight mi = { .part = part }; |
| 157 | |
| 158 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi); |
| 159 | inflight[0] = mi.inflight[0]; |
| 160 | inflight[1] = mi.inflight[1]; |
| 161 | } |
| 162 | |
| 163 | void blk_freeze_queue_start(struct request_queue *q) |
| 164 | { |
| 165 | mutex_lock(&q->mq_freeze_lock); |
| 166 | if (++q->mq_freeze_depth == 1) { |
| 167 | percpu_ref_kill(&q->q_usage_counter); |
| 168 | mutex_unlock(&q->mq_freeze_lock); |
| 169 | if (queue_is_mq(q)) |
| 170 | blk_mq_run_hw_queues(q, false); |
| 171 | } else { |
| 172 | mutex_unlock(&q->mq_freeze_lock); |
| 173 | } |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(blk_freeze_queue_start); |
| 176 | |
| 177 | void blk_mq_freeze_queue_wait(struct request_queue *q) |
| 178 | { |
| 179 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); |
| 180 | } |
| 181 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait); |
| 182 | |
| 183 | int blk_mq_freeze_queue_wait_timeout(struct request_queue *q, |
| 184 | unsigned long timeout) |
| 185 | { |
| 186 | return wait_event_timeout(q->mq_freeze_wq, |
| 187 | percpu_ref_is_zero(&q->q_usage_counter), |
| 188 | timeout); |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout); |
| 191 | |
| 192 | /* |
| 193 | * Guarantee no request is in use, so we can change any data structure of |
| 194 | * the queue afterward. |
| 195 | */ |
| 196 | void blk_freeze_queue(struct request_queue *q) |
| 197 | { |
| 198 | /* |
| 199 | * In the !blk_mq case we are only calling this to kill the |
| 200 | * q_usage_counter, otherwise this increases the freeze depth |
| 201 | * and waits for it to return to zero. For this reason there is |
| 202 | * no blk_unfreeze_queue(), and blk_freeze_queue() is not |
| 203 | * exported to drivers as the only user for unfreeze is blk_mq. |
| 204 | */ |
| 205 | blk_freeze_queue_start(q); |
| 206 | blk_mq_freeze_queue_wait(q); |
| 207 | } |
| 208 | |
| 209 | void blk_mq_freeze_queue(struct request_queue *q) |
| 210 | { |
| 211 | /* |
| 212 | * ...just an alias to keep freeze and unfreeze actions balanced |
| 213 | * in the blk_mq_* namespace |
| 214 | */ |
| 215 | blk_freeze_queue(q); |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); |
| 218 | |
| 219 | void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic) |
| 220 | { |
| 221 | mutex_lock(&q->mq_freeze_lock); |
| 222 | if (force_atomic) |
| 223 | q->q_usage_counter.data->force_atomic = true; |
| 224 | q->mq_freeze_depth--; |
| 225 | WARN_ON_ONCE(q->mq_freeze_depth < 0); |
| 226 | if (!q->mq_freeze_depth) { |
| 227 | percpu_ref_resurrect(&q->q_usage_counter); |
| 228 | wake_up_all(&q->mq_freeze_wq); |
| 229 | } |
| 230 | mutex_unlock(&q->mq_freeze_lock); |
| 231 | } |
| 232 | |
| 233 | void blk_mq_unfreeze_queue(struct request_queue *q) |
| 234 | { |
| 235 | __blk_mq_unfreeze_queue(q, false); |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); |
| 238 | |
| 239 | /* |
| 240 | * FIXME: replace the scsi_internal_device_*block_nowait() calls in the |
| 241 | * mpt3sas driver such that this function can be removed. |
| 242 | */ |
| 243 | void blk_mq_quiesce_queue_nowait(struct request_queue *q) |
| 244 | { |
| 245 | unsigned long flags; |
| 246 | |
| 247 | spin_lock_irqsave(&q->queue_lock, flags); |
| 248 | if (!q->quiesce_depth++) |
| 249 | blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q); |
| 250 | spin_unlock_irqrestore(&q->queue_lock, flags); |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait); |
| 253 | |
| 254 | /** |
| 255 | * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done |
| 256 | * @q: request queue. |
| 257 | * |
| 258 | * Note: it is driver's responsibility for making sure that quiesce has |
| 259 | * been started. |
| 260 | */ |
| 261 | void blk_mq_wait_quiesce_done(struct request_queue *q) |
| 262 | { |
| 263 | if (blk_queue_has_srcu(q)) |
| 264 | synchronize_srcu(q->srcu); |
| 265 | else |
| 266 | synchronize_rcu(); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done); |
| 269 | |
| 270 | /** |
| 271 | * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished |
| 272 | * @q: request queue. |
| 273 | * |
| 274 | * Note: this function does not prevent that the struct request end_io() |
| 275 | * callback function is invoked. Once this function is returned, we make |
| 276 | * sure no dispatch can happen until the queue is unquiesced via |
| 277 | * blk_mq_unquiesce_queue(). |
| 278 | */ |
| 279 | void blk_mq_quiesce_queue(struct request_queue *q) |
| 280 | { |
| 281 | blk_mq_quiesce_queue_nowait(q); |
| 282 | blk_mq_wait_quiesce_done(q); |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); |
| 285 | |
| 286 | /* |
| 287 | * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue() |
| 288 | * @q: request queue. |
| 289 | * |
| 290 | * This function recovers queue into the state before quiescing |
| 291 | * which is done by blk_mq_quiesce_queue. |
| 292 | */ |
| 293 | void blk_mq_unquiesce_queue(struct request_queue *q) |
| 294 | { |
| 295 | unsigned long flags; |
| 296 | bool run_queue = false; |
| 297 | |
| 298 | spin_lock_irqsave(&q->queue_lock, flags); |
| 299 | if (WARN_ON_ONCE(q->quiesce_depth <= 0)) { |
| 300 | ; |
| 301 | } else if (!--q->quiesce_depth) { |
| 302 | blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q); |
| 303 | run_queue = true; |
| 304 | } |
| 305 | spin_unlock_irqrestore(&q->queue_lock, flags); |
| 306 | |
| 307 | /* dispatch requests which are inserted during quiescing */ |
| 308 | if (run_queue) |
| 309 | blk_mq_run_hw_queues(q, true); |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue); |
| 312 | |
| 313 | void blk_mq_wake_waiters(struct request_queue *q) |
| 314 | { |
| 315 | struct blk_mq_hw_ctx *hctx; |
| 316 | unsigned long i; |
| 317 | |
| 318 | queue_for_each_hw_ctx(q, hctx, i) |
| 319 | if (blk_mq_hw_queue_mapped(hctx)) |
| 320 | blk_mq_tag_wakeup_all(hctx->tags, true); |
| 321 | } |
| 322 | |
| 323 | void blk_rq_init(struct request_queue *q, struct request *rq) |
| 324 | { |
| 325 | memset(rq, 0, sizeof(*rq)); |
| 326 | |
| 327 | INIT_LIST_HEAD(&rq->queuelist); |
| 328 | rq->q = q; |
| 329 | rq->__sector = (sector_t) -1; |
| 330 | INIT_HLIST_NODE(&rq->hash); |
| 331 | RB_CLEAR_NODE(&rq->rb_node); |
| 332 | rq->tag = BLK_MQ_NO_TAG; |
| 333 | rq->internal_tag = BLK_MQ_NO_TAG; |
| 334 | rq->start_time_ns = ktime_get_ns(); |
| 335 | rq->part = NULL; |
| 336 | blk_crypto_rq_set_defaults(rq); |
| 337 | } |
| 338 | EXPORT_SYMBOL(blk_rq_init); |
| 339 | |
| 340 | static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, |
| 341 | struct blk_mq_tags *tags, unsigned int tag, u64 alloc_time_ns) |
| 342 | { |
| 343 | struct blk_mq_ctx *ctx = data->ctx; |
| 344 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 345 | struct request_queue *q = data->q; |
| 346 | struct request *rq = tags->static_rqs[tag]; |
| 347 | |
| 348 | rq->q = q; |
| 349 | rq->mq_ctx = ctx; |
| 350 | rq->mq_hctx = hctx; |
| 351 | rq->cmd_flags = data->cmd_flags; |
| 352 | |
| 353 | if (data->flags & BLK_MQ_REQ_PM) |
| 354 | data->rq_flags |= RQF_PM; |
| 355 | if (blk_queue_io_stat(q)) |
| 356 | data->rq_flags |= RQF_IO_STAT; |
| 357 | rq->rq_flags = data->rq_flags; |
| 358 | |
| 359 | if (!(data->rq_flags & RQF_ELV)) { |
| 360 | rq->tag = tag; |
| 361 | rq->internal_tag = BLK_MQ_NO_TAG; |
| 362 | } else { |
| 363 | rq->tag = BLK_MQ_NO_TAG; |
| 364 | rq->internal_tag = tag; |
| 365 | } |
| 366 | rq->timeout = 0; |
| 367 | |
| 368 | if (blk_mq_need_time_stamp(rq)) |
| 369 | rq->start_time_ns = ktime_get_ns(); |
| 370 | else |
| 371 | rq->start_time_ns = 0; |
| 372 | rq->part = NULL; |
| 373 | #ifdef CONFIG_BLK_RQ_ALLOC_TIME |
| 374 | rq->alloc_time_ns = alloc_time_ns; |
| 375 | #endif |
| 376 | rq->io_start_time_ns = 0; |
| 377 | rq->stats_sectors = 0; |
| 378 | rq->nr_phys_segments = 0; |
| 379 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 380 | rq->nr_integrity_segments = 0; |
| 381 | #endif |
| 382 | rq->end_io = NULL; |
| 383 | rq->end_io_data = NULL; |
| 384 | |
| 385 | blk_crypto_rq_set_defaults(rq); |
| 386 | INIT_LIST_HEAD(&rq->queuelist); |
| 387 | /* tag was already set */ |
| 388 | WRITE_ONCE(rq->deadline, 0); |
| 389 | req_ref_set(rq, 1); |
| 390 | |
| 391 | if (rq->rq_flags & RQF_ELV) { |
| 392 | struct elevator_queue *e = data->q->elevator; |
| 393 | |
| 394 | INIT_HLIST_NODE(&rq->hash); |
| 395 | RB_CLEAR_NODE(&rq->rb_node); |
| 396 | |
| 397 | if (!op_is_flush(data->cmd_flags) && |
| 398 | e->type->ops.prepare_request) { |
| 399 | e->type->ops.prepare_request(rq); |
| 400 | rq->rq_flags |= RQF_ELVPRIV; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return rq; |
| 405 | } |
| 406 | |
| 407 | static inline struct request * |
| 408 | __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data, |
| 409 | u64 alloc_time_ns) |
| 410 | { |
| 411 | unsigned int tag, tag_offset; |
| 412 | struct blk_mq_tags *tags; |
| 413 | struct request *rq; |
| 414 | unsigned long tag_mask; |
| 415 | int i, nr = 0; |
| 416 | |
| 417 | tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset); |
| 418 | if (unlikely(!tag_mask)) |
| 419 | return NULL; |
| 420 | |
| 421 | tags = blk_mq_tags_from_data(data); |
| 422 | for (i = 0; tag_mask; i++) { |
| 423 | if (!(tag_mask & (1UL << i))) |
| 424 | continue; |
| 425 | tag = tag_offset + i; |
| 426 | prefetch(tags->static_rqs[tag]); |
| 427 | tag_mask &= ~(1UL << i); |
| 428 | rq = blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns); |
| 429 | rq_list_add(data->cached_rq, rq); |
| 430 | nr++; |
| 431 | } |
| 432 | /* caller already holds a reference, add for remainder */ |
| 433 | percpu_ref_get_many(&data->q->q_usage_counter, nr - 1); |
| 434 | data->nr_tags -= nr; |
| 435 | |
| 436 | return rq_list_pop(data->cached_rq); |
| 437 | } |
| 438 | |
| 439 | static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data) |
| 440 | { |
| 441 | struct request_queue *q = data->q; |
| 442 | u64 alloc_time_ns = 0; |
| 443 | struct request *rq; |
| 444 | unsigned int tag; |
| 445 | |
| 446 | /* alloc_time includes depth and tag waits */ |
| 447 | if (blk_queue_rq_alloc_time(q)) |
| 448 | alloc_time_ns = ktime_get_ns(); |
| 449 | |
| 450 | if (data->cmd_flags & REQ_NOWAIT) |
| 451 | data->flags |= BLK_MQ_REQ_NOWAIT; |
| 452 | |
| 453 | if (q->elevator) { |
| 454 | struct elevator_queue *e = q->elevator; |
| 455 | |
| 456 | data->rq_flags |= RQF_ELV; |
| 457 | |
| 458 | /* |
| 459 | * Flush/passthrough requests are special and go directly to the |
| 460 | * dispatch list. Don't include reserved tags in the |
| 461 | * limiting, as it isn't useful. |
| 462 | */ |
| 463 | if (!op_is_flush(data->cmd_flags) && |
| 464 | !blk_op_is_passthrough(data->cmd_flags) && |
| 465 | e->type->ops.limit_depth && |
| 466 | !(data->flags & BLK_MQ_REQ_RESERVED)) |
| 467 | e->type->ops.limit_depth(data->cmd_flags, data); |
| 468 | } |
| 469 | |
| 470 | retry: |
| 471 | data->ctx = blk_mq_get_ctx(q); |
| 472 | data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx); |
| 473 | if (!(data->rq_flags & RQF_ELV)) |
| 474 | blk_mq_tag_busy(data->hctx); |
| 475 | |
| 476 | /* |
| 477 | * Try batched alloc if we want more than 1 tag. |
| 478 | */ |
| 479 | if (data->nr_tags > 1) { |
| 480 | rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns); |
| 481 | if (rq) |
| 482 | return rq; |
| 483 | data->nr_tags = 1; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Waiting allocations only fail because of an inactive hctx. In that |
| 488 | * case just retry the hctx assignment and tag allocation as CPU hotplug |
| 489 | * should have migrated us to an online CPU by now. |
| 490 | */ |
| 491 | tag = blk_mq_get_tag(data); |
| 492 | if (tag == BLK_MQ_NO_TAG) { |
| 493 | if (data->flags & BLK_MQ_REQ_NOWAIT) |
| 494 | return NULL; |
| 495 | /* |
| 496 | * Give up the CPU and sleep for a random short time to |
| 497 | * ensure that thread using a realtime scheduling class |
| 498 | * are migrated off the CPU, and thus off the hctx that |
| 499 | * is going away. |
| 500 | */ |
| 501 | msleep(3); |
| 502 | goto retry; |
| 503 | } |
| 504 | |
| 505 | return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag, |
| 506 | alloc_time_ns); |
| 507 | } |
| 508 | |
| 509 | struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op, |
| 510 | blk_mq_req_flags_t flags) |
| 511 | { |
| 512 | struct blk_mq_alloc_data data = { |
| 513 | .q = q, |
| 514 | .flags = flags, |
| 515 | .cmd_flags = op, |
| 516 | .nr_tags = 1, |
| 517 | }; |
| 518 | struct request *rq; |
| 519 | int ret; |
| 520 | |
| 521 | ret = blk_queue_enter(q, flags); |
| 522 | if (ret) |
| 523 | return ERR_PTR(ret); |
| 524 | |
| 525 | rq = __blk_mq_alloc_requests(&data); |
| 526 | if (!rq) |
| 527 | goto out_queue_exit; |
| 528 | rq->__data_len = 0; |
| 529 | rq->__sector = (sector_t) -1; |
| 530 | rq->bio = rq->biotail = NULL; |
| 531 | return rq; |
| 532 | out_queue_exit: |
| 533 | blk_queue_exit(q); |
| 534 | return ERR_PTR(-EWOULDBLOCK); |
| 535 | } |
| 536 | EXPORT_SYMBOL(blk_mq_alloc_request); |
| 537 | |
| 538 | struct request *blk_mq_alloc_request_hctx(struct request_queue *q, |
| 539 | unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx) |
| 540 | { |
| 541 | struct blk_mq_alloc_data data = { |
| 542 | .q = q, |
| 543 | .flags = flags, |
| 544 | .cmd_flags = op, |
| 545 | .nr_tags = 1, |
| 546 | }; |
| 547 | u64 alloc_time_ns = 0; |
| 548 | unsigned int cpu; |
| 549 | unsigned int tag; |
| 550 | int ret; |
| 551 | |
| 552 | /* alloc_time includes depth and tag waits */ |
| 553 | if (blk_queue_rq_alloc_time(q)) |
| 554 | alloc_time_ns = ktime_get_ns(); |
| 555 | |
| 556 | /* |
| 557 | * If the tag allocator sleeps we could get an allocation for a |
| 558 | * different hardware context. No need to complicate the low level |
| 559 | * allocator for this for the rare use case of a command tied to |
| 560 | * a specific queue. |
| 561 | */ |
| 562 | if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED)))) |
| 563 | return ERR_PTR(-EINVAL); |
| 564 | |
| 565 | if (hctx_idx >= q->nr_hw_queues) |
| 566 | return ERR_PTR(-EIO); |
| 567 | |
| 568 | ret = blk_queue_enter(q, flags); |
| 569 | if (ret) |
| 570 | return ERR_PTR(ret); |
| 571 | |
| 572 | /* |
| 573 | * Check if the hardware context is actually mapped to anything. |
| 574 | * If not tell the caller that it should skip this queue. |
| 575 | */ |
| 576 | ret = -EXDEV; |
| 577 | data.hctx = xa_load(&q->hctx_table, hctx_idx); |
| 578 | if (!blk_mq_hw_queue_mapped(data.hctx)) |
| 579 | goto out_queue_exit; |
| 580 | cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask); |
| 581 | data.ctx = __blk_mq_get_ctx(q, cpu); |
| 582 | |
| 583 | if (!q->elevator) |
| 584 | blk_mq_tag_busy(data.hctx); |
| 585 | else |
| 586 | data.rq_flags |= RQF_ELV; |
| 587 | |
| 588 | ret = -EWOULDBLOCK; |
| 589 | tag = blk_mq_get_tag(&data); |
| 590 | if (tag == BLK_MQ_NO_TAG) |
| 591 | goto out_queue_exit; |
| 592 | return blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag, |
| 593 | alloc_time_ns); |
| 594 | |
| 595 | out_queue_exit: |
| 596 | blk_queue_exit(q); |
| 597 | return ERR_PTR(ret); |
| 598 | } |
| 599 | EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); |
| 600 | |
| 601 | static void __blk_mq_free_request(struct request *rq) |
| 602 | { |
| 603 | struct request_queue *q = rq->q; |
| 604 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 605 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
| 606 | const int sched_tag = rq->internal_tag; |
| 607 | |
| 608 | blk_crypto_free_request(rq); |
| 609 | blk_pm_mark_last_busy(rq); |
| 610 | rq->mq_hctx = NULL; |
| 611 | if (rq->tag != BLK_MQ_NO_TAG) |
| 612 | blk_mq_put_tag(hctx->tags, ctx, rq->tag); |
| 613 | if (sched_tag != BLK_MQ_NO_TAG) |
| 614 | blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag); |
| 615 | blk_mq_sched_restart(hctx); |
| 616 | blk_queue_exit(q); |
| 617 | } |
| 618 | |
| 619 | void blk_mq_free_request(struct request *rq) |
| 620 | { |
| 621 | struct request_queue *q = rq->q; |
| 622 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
| 623 | |
| 624 | if ((rq->rq_flags & RQF_ELVPRIV) && |
| 625 | q->elevator->type->ops.finish_request) |
| 626 | q->elevator->type->ops.finish_request(rq); |
| 627 | |
| 628 | if (rq->rq_flags & RQF_MQ_INFLIGHT) |
| 629 | __blk_mq_dec_active_requests(hctx); |
| 630 | |
| 631 | if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq))) |
| 632 | laptop_io_completion(q->disk->bdi); |
| 633 | |
| 634 | rq_qos_done(q, rq); |
| 635 | |
| 636 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
| 637 | if (req_ref_put_and_test(rq)) |
| 638 | __blk_mq_free_request(rq); |
| 639 | } |
| 640 | EXPORT_SYMBOL_GPL(blk_mq_free_request); |
| 641 | |
| 642 | void blk_mq_free_plug_rqs(struct blk_plug *plug) |
| 643 | { |
| 644 | struct request *rq; |
| 645 | |
| 646 | while ((rq = rq_list_pop(&plug->cached_rq)) != NULL) |
| 647 | blk_mq_free_request(rq); |
| 648 | } |
| 649 | |
| 650 | void blk_dump_rq_flags(struct request *rq, char *msg) |
| 651 | { |
| 652 | printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg, |
| 653 | rq->q->disk ? rq->q->disk->disk_name : "?", |
| 654 | (unsigned long long) rq->cmd_flags); |
| 655 | |
| 656 | printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n", |
| 657 | (unsigned long long)blk_rq_pos(rq), |
| 658 | blk_rq_sectors(rq), blk_rq_cur_sectors(rq)); |
| 659 | printk(KERN_INFO " bio %p, biotail %p, len %u\n", |
| 660 | rq->bio, rq->biotail, blk_rq_bytes(rq)); |
| 661 | } |
| 662 | EXPORT_SYMBOL(blk_dump_rq_flags); |
| 663 | |
| 664 | static void req_bio_endio(struct request *rq, struct bio *bio, |
| 665 | unsigned int nbytes, blk_status_t error) |
| 666 | { |
| 667 | if (unlikely(error)) { |
| 668 | bio->bi_status = error; |
| 669 | } else if (req_op(rq) == REQ_OP_ZONE_APPEND) { |
| 670 | /* |
| 671 | * Partial zone append completions cannot be supported as the |
| 672 | * BIO fragments may end up not being written sequentially. |
| 673 | */ |
| 674 | if (bio->bi_iter.bi_size != nbytes) |
| 675 | bio->bi_status = BLK_STS_IOERR; |
| 676 | else |
| 677 | bio->bi_iter.bi_sector = rq->__sector; |
| 678 | } |
| 679 | |
| 680 | bio_advance(bio, nbytes); |
| 681 | |
| 682 | if (unlikely(rq->rq_flags & RQF_QUIET)) |
| 683 | bio_set_flag(bio, BIO_QUIET); |
| 684 | /* don't actually finish bio if it's part of flush sequence */ |
| 685 | if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ)) |
| 686 | bio_endio(bio); |
| 687 | } |
| 688 | |
| 689 | static void blk_account_io_completion(struct request *req, unsigned int bytes) |
| 690 | { |
| 691 | if (req->part && blk_do_io_stat(req)) { |
| 692 | const int sgrp = op_stat_group(req_op(req)); |
| 693 | |
| 694 | part_stat_lock(); |
| 695 | part_stat_add(req->part, sectors[sgrp], bytes >> 9); |
| 696 | part_stat_unlock(); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static void blk_print_req_error(struct request *req, blk_status_t status) |
| 701 | { |
| 702 | printk_ratelimited(KERN_ERR |
| 703 | "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x " |
| 704 | "phys_seg %u prio class %u\n", |
| 705 | blk_status_to_str(status), |
| 706 | req->q->disk ? req->q->disk->disk_name : "?", |
| 707 | blk_rq_pos(req), req_op(req), blk_op_str(req_op(req)), |
| 708 | req->cmd_flags & ~REQ_OP_MASK, |
| 709 | req->nr_phys_segments, |
| 710 | IOPRIO_PRIO_CLASS(req->ioprio)); |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Fully end IO on a request. Does not support partial completions, or |
| 715 | * errors. |
| 716 | */ |
| 717 | static void blk_complete_request(struct request *req) |
| 718 | { |
| 719 | const bool is_flush = (req->rq_flags & RQF_FLUSH_SEQ) != 0; |
| 720 | int total_bytes = blk_rq_bytes(req); |
| 721 | struct bio *bio = req->bio; |
| 722 | |
| 723 | trace_block_rq_complete(req, BLK_STS_OK, total_bytes); |
| 724 | |
| 725 | if (!bio) |
| 726 | return; |
| 727 | |
| 728 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
| 729 | if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ) |
| 730 | req->q->integrity.profile->complete_fn(req, total_bytes); |
| 731 | #endif |
| 732 | |
| 733 | blk_account_io_completion(req, total_bytes); |
| 734 | |
| 735 | do { |
| 736 | struct bio *next = bio->bi_next; |
| 737 | |
| 738 | /* Completion has already been traced */ |
| 739 | bio_clear_flag(bio, BIO_TRACE_COMPLETION); |
| 740 | if (!is_flush) |
| 741 | bio_endio(bio); |
| 742 | bio = next; |
| 743 | } while (bio); |
| 744 | |
| 745 | /* |
| 746 | * Reset counters so that the request stacking driver |
| 747 | * can find how many bytes remain in the request |
| 748 | * later. |
| 749 | */ |
| 750 | req->bio = NULL; |
| 751 | req->__data_len = 0; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * blk_update_request - Complete multiple bytes without completing the request |
| 756 | * @req: the request being processed |
| 757 | * @error: block status code |
| 758 | * @nr_bytes: number of bytes to complete for @req |
| 759 | * |
| 760 | * Description: |
| 761 | * Ends I/O on a number of bytes attached to @req, but doesn't complete |
| 762 | * the request structure even if @req doesn't have leftover. |
| 763 | * If @req has leftover, sets it up for the next range of segments. |
| 764 | * |
| 765 | * Passing the result of blk_rq_bytes() as @nr_bytes guarantees |
| 766 | * %false return from this function. |
| 767 | * |
| 768 | * Note: |
| 769 | * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function |
| 770 | * except in the consistency check at the end of this function. |
| 771 | * |
| 772 | * Return: |
| 773 | * %false - this request doesn't have any more data |
| 774 | * %true - this request has more data |
| 775 | **/ |
| 776 | bool blk_update_request(struct request *req, blk_status_t error, |
| 777 | unsigned int nr_bytes) |
| 778 | { |
| 779 | int total_bytes; |
| 780 | |
| 781 | trace_block_rq_complete(req, error, nr_bytes); |
| 782 | |
| 783 | if (!req->bio) |
| 784 | return false; |
| 785 | |
| 786 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
| 787 | if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ && |
| 788 | error == BLK_STS_OK) |
| 789 | req->q->integrity.profile->complete_fn(req, nr_bytes); |
| 790 | #endif |
| 791 | |
| 792 | if (unlikely(error && !blk_rq_is_passthrough(req) && |
| 793 | !(req->rq_flags & RQF_QUIET))) { |
| 794 | blk_print_req_error(req, error); |
| 795 | trace_block_rq_error(req, error, nr_bytes); |
| 796 | } |
| 797 | |
| 798 | blk_account_io_completion(req, nr_bytes); |
| 799 | |
| 800 | total_bytes = 0; |
| 801 | while (req->bio) { |
| 802 | struct bio *bio = req->bio; |
| 803 | unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes); |
| 804 | |
| 805 | if (bio_bytes == bio->bi_iter.bi_size) |
| 806 | req->bio = bio->bi_next; |
| 807 | |
| 808 | /* Completion has already been traced */ |
| 809 | bio_clear_flag(bio, BIO_TRACE_COMPLETION); |
| 810 | req_bio_endio(req, bio, bio_bytes, error); |
| 811 | |
| 812 | total_bytes += bio_bytes; |
| 813 | nr_bytes -= bio_bytes; |
| 814 | |
| 815 | if (!nr_bytes) |
| 816 | break; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * completely done |
| 821 | */ |
| 822 | if (!req->bio) { |
| 823 | /* |
| 824 | * Reset counters so that the request stacking driver |
| 825 | * can find how many bytes remain in the request |
| 826 | * later. |
| 827 | */ |
| 828 | req->__data_len = 0; |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | req->__data_len -= total_bytes; |
| 833 | |
| 834 | /* update sector only for requests with clear definition of sector */ |
| 835 | if (!blk_rq_is_passthrough(req)) |
| 836 | req->__sector += total_bytes >> 9; |
| 837 | |
| 838 | /* mixed attributes always follow the first bio */ |
| 839 | if (req->rq_flags & RQF_MIXED_MERGE) { |
| 840 | req->cmd_flags &= ~REQ_FAILFAST_MASK; |
| 841 | req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK; |
| 842 | } |
| 843 | |
| 844 | if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) { |
| 845 | /* |
| 846 | * If total number of sectors is less than the first segment |
| 847 | * size, something has gone terribly wrong. |
| 848 | */ |
| 849 | if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) { |
| 850 | blk_dump_rq_flags(req, "request botched"); |
| 851 | req->__data_len = blk_rq_cur_bytes(req); |
| 852 | } |
| 853 | |
| 854 | /* recalculate the number of segments */ |
| 855 | req->nr_phys_segments = blk_recalc_rq_segments(req); |
| 856 | } |
| 857 | |
| 858 | return true; |
| 859 | } |
| 860 | EXPORT_SYMBOL_GPL(blk_update_request); |
| 861 | |
| 862 | static void __blk_account_io_done(struct request *req, u64 now) |
| 863 | { |
| 864 | const int sgrp = op_stat_group(req_op(req)); |
| 865 | |
| 866 | part_stat_lock(); |
| 867 | update_io_ticks(req->part, jiffies, true); |
| 868 | part_stat_inc(req->part, ios[sgrp]); |
| 869 | part_stat_add(req->part, nsecs[sgrp], now - req->start_time_ns); |
| 870 | part_stat_unlock(); |
| 871 | } |
| 872 | |
| 873 | static inline void blk_account_io_done(struct request *req, u64 now) |
| 874 | { |
| 875 | /* |
| 876 | * Account IO completion. flush_rq isn't accounted as a |
| 877 | * normal IO on queueing nor completion. Accounting the |
| 878 | * containing request is enough. |
| 879 | */ |
| 880 | if (blk_do_io_stat(req) && req->part && |
| 881 | !(req->rq_flags & RQF_FLUSH_SEQ)) |
| 882 | __blk_account_io_done(req, now); |
| 883 | } |
| 884 | |
| 885 | static void __blk_account_io_start(struct request *rq) |
| 886 | { |
| 887 | /* passthrough requests can hold bios that do not have ->bi_bdev set */ |
| 888 | if (rq->bio && rq->bio->bi_bdev) |
| 889 | rq->part = rq->bio->bi_bdev; |
| 890 | else if (rq->q->disk) |
| 891 | rq->part = rq->q->disk->part0; |
| 892 | |
| 893 | part_stat_lock(); |
| 894 | update_io_ticks(rq->part, jiffies, false); |
| 895 | part_stat_unlock(); |
| 896 | } |
| 897 | |
| 898 | static inline void blk_account_io_start(struct request *req) |
| 899 | { |
| 900 | if (blk_do_io_stat(req)) |
| 901 | __blk_account_io_start(req); |
| 902 | } |
| 903 | |
| 904 | static inline void __blk_mq_end_request_acct(struct request *rq, u64 now) |
| 905 | { |
| 906 | if (rq->rq_flags & RQF_STATS) { |
| 907 | blk_mq_poll_stats_start(rq->q); |
| 908 | blk_stat_add(rq, now); |
| 909 | } |
| 910 | |
| 911 | blk_mq_sched_completed_request(rq, now); |
| 912 | blk_account_io_done(rq, now); |
| 913 | } |
| 914 | |
| 915 | inline void __blk_mq_end_request(struct request *rq, blk_status_t error) |
| 916 | { |
| 917 | if (blk_mq_need_time_stamp(rq)) |
| 918 | __blk_mq_end_request_acct(rq, ktime_get_ns()); |
| 919 | |
| 920 | if (rq->end_io) { |
| 921 | rq_qos_done(rq->q, rq); |
| 922 | rq->end_io(rq, error); |
| 923 | } else { |
| 924 | blk_mq_free_request(rq); |
| 925 | } |
| 926 | } |
| 927 | EXPORT_SYMBOL(__blk_mq_end_request); |
| 928 | |
| 929 | void blk_mq_end_request(struct request *rq, blk_status_t error) |
| 930 | { |
| 931 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
| 932 | BUG(); |
| 933 | __blk_mq_end_request(rq, error); |
| 934 | } |
| 935 | EXPORT_SYMBOL(blk_mq_end_request); |
| 936 | |
| 937 | #define TAG_COMP_BATCH 32 |
| 938 | |
| 939 | static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx, |
| 940 | int *tag_array, int nr_tags) |
| 941 | { |
| 942 | struct request_queue *q = hctx->queue; |
| 943 | |
| 944 | /* |
| 945 | * All requests should have been marked as RQF_MQ_INFLIGHT, so |
| 946 | * update hctx->nr_active in batch |
| 947 | */ |
| 948 | if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) |
| 949 | __blk_mq_sub_active_requests(hctx, nr_tags); |
| 950 | |
| 951 | blk_mq_put_tags(hctx->tags, tag_array, nr_tags); |
| 952 | percpu_ref_put_many(&q->q_usage_counter, nr_tags); |
| 953 | } |
| 954 | |
| 955 | void blk_mq_end_request_batch(struct io_comp_batch *iob) |
| 956 | { |
| 957 | int tags[TAG_COMP_BATCH], nr_tags = 0; |
| 958 | struct blk_mq_hw_ctx *cur_hctx = NULL; |
| 959 | struct request *rq; |
| 960 | u64 now = 0; |
| 961 | |
| 962 | if (iob->need_ts) |
| 963 | now = ktime_get_ns(); |
| 964 | |
| 965 | while ((rq = rq_list_pop(&iob->req_list)) != NULL) { |
| 966 | prefetch(rq->bio); |
| 967 | prefetch(rq->rq_next); |
| 968 | |
| 969 | blk_complete_request(rq); |
| 970 | if (iob->need_ts) |
| 971 | __blk_mq_end_request_acct(rq, now); |
| 972 | |
| 973 | rq_qos_done(rq->q, rq); |
| 974 | |
| 975 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
| 976 | if (!req_ref_put_and_test(rq)) |
| 977 | continue; |
| 978 | |
| 979 | blk_crypto_free_request(rq); |
| 980 | blk_pm_mark_last_busy(rq); |
| 981 | |
| 982 | if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) { |
| 983 | if (cur_hctx) |
| 984 | blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags); |
| 985 | nr_tags = 0; |
| 986 | cur_hctx = rq->mq_hctx; |
| 987 | } |
| 988 | tags[nr_tags++] = rq->tag; |
| 989 | } |
| 990 | |
| 991 | if (nr_tags) |
| 992 | blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags); |
| 993 | } |
| 994 | EXPORT_SYMBOL_GPL(blk_mq_end_request_batch); |
| 995 | |
| 996 | static void blk_complete_reqs(struct llist_head *list) |
| 997 | { |
| 998 | struct llist_node *entry = llist_reverse_order(llist_del_all(list)); |
| 999 | struct request *rq, *next; |
| 1000 | |
| 1001 | llist_for_each_entry_safe(rq, next, entry, ipi_list) |
| 1002 | rq->q->mq_ops->complete(rq); |
| 1003 | } |
| 1004 | |
| 1005 | static __latent_entropy void blk_done_softirq(struct softirq_action *h) |
| 1006 | { |
| 1007 | blk_complete_reqs(this_cpu_ptr(&blk_cpu_done)); |
| 1008 | } |
| 1009 | |
| 1010 | static int blk_softirq_cpu_dead(unsigned int cpu) |
| 1011 | { |
| 1012 | blk_complete_reqs(&per_cpu(blk_cpu_done, cpu)); |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static void __blk_mq_complete_request_remote(void *data) |
| 1017 | { |
| 1018 | __raise_softirq_irqoff(BLOCK_SOFTIRQ); |
| 1019 | } |
| 1020 | |
| 1021 | static inline bool blk_mq_complete_need_ipi(struct request *rq) |
| 1022 | { |
| 1023 | int cpu = raw_smp_processor_id(); |
| 1024 | |
| 1025 | if (!IS_ENABLED(CONFIG_SMP) || |
| 1026 | !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) |
| 1027 | return false; |
| 1028 | /* |
| 1029 | * With force threaded interrupts enabled, raising softirq from an SMP |
| 1030 | * function call will always result in waking the ksoftirqd thread. |
| 1031 | * This is probably worse than completing the request on a different |
| 1032 | * cache domain. |
| 1033 | */ |
| 1034 | if (force_irqthreads()) |
| 1035 | return false; |
| 1036 | |
| 1037 | /* same CPU or cache domain? Complete locally */ |
| 1038 | if (cpu == rq->mq_ctx->cpu || |
| 1039 | (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) && |
| 1040 | cpus_share_cache(cpu, rq->mq_ctx->cpu))) |
| 1041 | return false; |
| 1042 | |
| 1043 | /* don't try to IPI to an offline CPU */ |
| 1044 | return cpu_online(rq->mq_ctx->cpu); |
| 1045 | } |
| 1046 | |
| 1047 | static void blk_mq_complete_send_ipi(struct request *rq) |
| 1048 | { |
| 1049 | struct llist_head *list; |
| 1050 | unsigned int cpu; |
| 1051 | |
| 1052 | cpu = rq->mq_ctx->cpu; |
| 1053 | list = &per_cpu(blk_cpu_done, cpu); |
| 1054 | if (llist_add(&rq->ipi_list, list)) { |
| 1055 | INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq); |
| 1056 | smp_call_function_single_async(cpu, &rq->csd); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | static void blk_mq_raise_softirq(struct request *rq) |
| 1061 | { |
| 1062 | struct llist_head *list; |
| 1063 | |
| 1064 | preempt_disable(); |
| 1065 | list = this_cpu_ptr(&blk_cpu_done); |
| 1066 | if (llist_add(&rq->ipi_list, list)) |
| 1067 | raise_softirq(BLOCK_SOFTIRQ); |
| 1068 | preempt_enable(); |
| 1069 | } |
| 1070 | |
| 1071 | bool blk_mq_complete_request_remote(struct request *rq) |
| 1072 | { |
| 1073 | WRITE_ONCE(rq->state, MQ_RQ_COMPLETE); |
| 1074 | |
| 1075 | /* |
| 1076 | * For a polled request, always complete locallly, it's pointless |
| 1077 | * to redirect the completion. |
| 1078 | */ |
| 1079 | if (rq->cmd_flags & REQ_POLLED) |
| 1080 | return false; |
| 1081 | |
| 1082 | if (blk_mq_complete_need_ipi(rq)) { |
| 1083 | blk_mq_complete_send_ipi(rq); |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
| 1087 | if (rq->q->nr_hw_queues == 1) { |
| 1088 | blk_mq_raise_softirq(rq); |
| 1089 | return true; |
| 1090 | } |
| 1091 | return false; |
| 1092 | } |
| 1093 | EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote); |
| 1094 | |
| 1095 | /** |
| 1096 | * blk_mq_complete_request - end I/O on a request |
| 1097 | * @rq: the request being processed |
| 1098 | * |
| 1099 | * Description: |
| 1100 | * Complete a request by scheduling the ->complete_rq operation. |
| 1101 | **/ |
| 1102 | void blk_mq_complete_request(struct request *rq) |
| 1103 | { |
| 1104 | if (!blk_mq_complete_request_remote(rq)) |
| 1105 | rq->q->mq_ops->complete(rq); |
| 1106 | } |
| 1107 | EXPORT_SYMBOL(blk_mq_complete_request); |
| 1108 | |
| 1109 | /** |
| 1110 | * blk_mq_start_request - Start processing a request |
| 1111 | * @rq: Pointer to request to be started |
| 1112 | * |
| 1113 | * Function used by device drivers to notify the block layer that a request |
| 1114 | * is going to be processed now, so blk layer can do proper initializations |
| 1115 | * such as starting the timeout timer. |
| 1116 | */ |
| 1117 | void blk_mq_start_request(struct request *rq) |
| 1118 | { |
| 1119 | struct request_queue *q = rq->q; |
| 1120 | |
| 1121 | trace_block_rq_issue(rq); |
| 1122 | |
| 1123 | if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) { |
| 1124 | u64 start_time; |
| 1125 | #ifdef CONFIG_BLK_CGROUP |
| 1126 | if (rq->bio) |
| 1127 | start_time = bio_issue_time(&rq->bio->bi_issue); |
| 1128 | else |
| 1129 | #endif |
| 1130 | start_time = ktime_get_ns(); |
| 1131 | rq->io_start_time_ns = start_time; |
| 1132 | rq->stats_sectors = blk_rq_sectors(rq); |
| 1133 | rq->rq_flags |= RQF_STATS; |
| 1134 | rq_qos_issue(q, rq); |
| 1135 | } |
| 1136 | |
| 1137 | WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE); |
| 1138 | |
| 1139 | blk_add_timer(rq); |
| 1140 | WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT); |
| 1141 | |
| 1142 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
| 1143 | if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE) |
| 1144 | q->integrity.profile->prepare_fn(rq); |
| 1145 | #endif |
| 1146 | if (rq->bio && rq->bio->bi_opf & REQ_POLLED) |
| 1147 | WRITE_ONCE(rq->bio->bi_cookie, blk_rq_to_qc(rq)); |
| 1148 | } |
| 1149 | EXPORT_SYMBOL(blk_mq_start_request); |
| 1150 | |
| 1151 | /** |
| 1152 | * blk_end_sync_rq - executes a completion event on a request |
| 1153 | * @rq: request to complete |
| 1154 | * @error: end I/O status of the request |
| 1155 | */ |
| 1156 | static void blk_end_sync_rq(struct request *rq, blk_status_t error) |
| 1157 | { |
| 1158 | struct completion *waiting = rq->end_io_data; |
| 1159 | |
| 1160 | rq->end_io_data = (void *)(uintptr_t)error; |
| 1161 | |
| 1162 | /* |
| 1163 | * complete last, if this is a stack request the process (and thus |
| 1164 | * the rq pointer) could be invalid right after this complete() |
| 1165 | */ |
| 1166 | complete(waiting); |
| 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * blk_execute_rq_nowait - insert a request to I/O scheduler for execution |
| 1171 | * @rq: request to insert |
| 1172 | * @at_head: insert request at head or tail of queue |
| 1173 | * @done: I/O completion handler |
| 1174 | * |
| 1175 | * Description: |
| 1176 | * Insert a fully prepared request at the back of the I/O scheduler queue |
| 1177 | * for execution. Don't wait for completion. |
| 1178 | * |
| 1179 | * Note: |
| 1180 | * This function will invoke @done directly if the queue is dead. |
| 1181 | */ |
| 1182 | void blk_execute_rq_nowait(struct request *rq, bool at_head, rq_end_io_fn *done) |
| 1183 | { |
| 1184 | WARN_ON(irqs_disabled()); |
| 1185 | WARN_ON(!blk_rq_is_passthrough(rq)); |
| 1186 | |
| 1187 | rq->end_io = done; |
| 1188 | |
| 1189 | blk_account_io_start(rq); |
| 1190 | |
| 1191 | /* |
| 1192 | * don't check dying flag for MQ because the request won't |
| 1193 | * be reused after dying flag is set |
| 1194 | */ |
| 1195 | blk_mq_sched_insert_request(rq, at_head, true, false); |
| 1196 | } |
| 1197 | EXPORT_SYMBOL_GPL(blk_execute_rq_nowait); |
| 1198 | |
| 1199 | static bool blk_rq_is_poll(struct request *rq) |
| 1200 | { |
| 1201 | if (!rq->mq_hctx) |
| 1202 | return false; |
| 1203 | if (rq->mq_hctx->type != HCTX_TYPE_POLL) |
| 1204 | return false; |
| 1205 | if (WARN_ON_ONCE(!rq->bio)) |
| 1206 | return false; |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
| 1210 | static void blk_rq_poll_completion(struct request *rq, struct completion *wait) |
| 1211 | { |
| 1212 | do { |
| 1213 | bio_poll(rq->bio, NULL, 0); |
| 1214 | cond_resched(); |
| 1215 | } while (!completion_done(wait)); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * blk_execute_rq - insert a request into queue for execution |
| 1220 | * @rq: request to insert |
| 1221 | * @at_head: insert request at head or tail of queue |
| 1222 | * |
| 1223 | * Description: |
| 1224 | * Insert a fully prepared request at the back of the I/O scheduler queue |
| 1225 | * for execution and wait for completion. |
| 1226 | * Return: The blk_status_t result provided to blk_mq_end_request(). |
| 1227 | */ |
| 1228 | blk_status_t blk_execute_rq(struct request *rq, bool at_head) |
| 1229 | { |
| 1230 | DECLARE_COMPLETION_ONSTACK(wait); |
| 1231 | unsigned long hang_check; |
| 1232 | |
| 1233 | rq->end_io_data = &wait; |
| 1234 | blk_execute_rq_nowait(rq, at_head, blk_end_sync_rq); |
| 1235 | |
| 1236 | /* Prevent hang_check timer from firing at us during very long I/O */ |
| 1237 | hang_check = sysctl_hung_task_timeout_secs; |
| 1238 | |
| 1239 | if (blk_rq_is_poll(rq)) |
| 1240 | blk_rq_poll_completion(rq, &wait); |
| 1241 | else if (hang_check) |
| 1242 | while (!wait_for_completion_io_timeout(&wait, |
| 1243 | hang_check * (HZ/2))) |
| 1244 | ; |
| 1245 | else |
| 1246 | wait_for_completion_io(&wait); |
| 1247 | |
| 1248 | return (blk_status_t)(uintptr_t)rq->end_io_data; |
| 1249 | } |
| 1250 | EXPORT_SYMBOL(blk_execute_rq); |
| 1251 | |
| 1252 | static void __blk_mq_requeue_request(struct request *rq) |
| 1253 | { |
| 1254 | struct request_queue *q = rq->q; |
| 1255 | |
| 1256 | blk_mq_put_driver_tag(rq); |
| 1257 | |
| 1258 | trace_block_rq_requeue(rq); |
| 1259 | rq_qos_requeue(q, rq); |
| 1260 | |
| 1261 | if (blk_mq_request_started(rq)) { |
| 1262 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
| 1263 | rq->rq_flags &= ~RQF_TIMED_OUT; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) |
| 1268 | { |
| 1269 | __blk_mq_requeue_request(rq); |
| 1270 | |
| 1271 | /* this request will be re-inserted to io scheduler queue */ |
| 1272 | blk_mq_sched_requeue_request(rq); |
| 1273 | |
| 1274 | blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(blk_mq_requeue_request); |
| 1277 | |
| 1278 | static void blk_mq_requeue_work(struct work_struct *work) |
| 1279 | { |
| 1280 | struct request_queue *q = |
| 1281 | container_of(work, struct request_queue, requeue_work.work); |
| 1282 | LIST_HEAD(rq_list); |
| 1283 | struct request *rq, *next; |
| 1284 | |
| 1285 | spin_lock_irq(&q->requeue_lock); |
| 1286 | list_splice_init(&q->requeue_list, &rq_list); |
| 1287 | spin_unlock_irq(&q->requeue_lock); |
| 1288 | |
| 1289 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { |
| 1290 | if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP))) |
| 1291 | continue; |
| 1292 | |
| 1293 | rq->rq_flags &= ~RQF_SOFTBARRIER; |
| 1294 | list_del_init(&rq->queuelist); |
| 1295 | /* |
| 1296 | * If RQF_DONTPREP, rq has contained some driver specific |
| 1297 | * data, so insert it to hctx dispatch list to avoid any |
| 1298 | * merge. |
| 1299 | */ |
| 1300 | if (rq->rq_flags & RQF_DONTPREP) |
| 1301 | blk_mq_request_bypass_insert(rq, false, false); |
| 1302 | else |
| 1303 | blk_mq_sched_insert_request(rq, true, false, false); |
| 1304 | } |
| 1305 | |
| 1306 | while (!list_empty(&rq_list)) { |
| 1307 | rq = list_entry(rq_list.next, struct request, queuelist); |
| 1308 | list_del_init(&rq->queuelist); |
| 1309 | blk_mq_sched_insert_request(rq, false, false, false); |
| 1310 | } |
| 1311 | |
| 1312 | blk_mq_run_hw_queues(q, false); |
| 1313 | } |
| 1314 | |
| 1315 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, |
| 1316 | bool kick_requeue_list) |
| 1317 | { |
| 1318 | struct request_queue *q = rq->q; |
| 1319 | unsigned long flags; |
| 1320 | |
| 1321 | /* |
| 1322 | * We abuse this flag that is otherwise used by the I/O scheduler to |
| 1323 | * request head insertion from the workqueue. |
| 1324 | */ |
| 1325 | BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); |
| 1326 | |
| 1327 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 1328 | if (at_head) { |
| 1329 | rq->rq_flags |= RQF_SOFTBARRIER; |
| 1330 | list_add(&rq->queuelist, &q->requeue_list); |
| 1331 | } else { |
| 1332 | list_add_tail(&rq->queuelist, &q->requeue_list); |
| 1333 | } |
| 1334 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 1335 | |
| 1336 | if (kick_requeue_list) |
| 1337 | blk_mq_kick_requeue_list(q); |
| 1338 | } |
| 1339 | |
| 1340 | void blk_mq_kick_requeue_list(struct request_queue *q) |
| 1341 | { |
| 1342 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0); |
| 1343 | } |
| 1344 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); |
| 1345 | |
| 1346 | void blk_mq_delay_kick_requeue_list(struct request_queue *q, |
| 1347 | unsigned long msecs) |
| 1348 | { |
| 1349 | kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, |
| 1350 | msecs_to_jiffies(msecs)); |
| 1351 | } |
| 1352 | EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); |
| 1353 | |
| 1354 | static bool blk_mq_rq_inflight(struct request *rq, void *priv, |
| 1355 | bool reserved) |
| 1356 | { |
| 1357 | /* |
| 1358 | * If we find a request that isn't idle we know the queue is busy |
| 1359 | * as it's checked in the iter. |
| 1360 | * Return false to stop the iteration. |
| 1361 | */ |
| 1362 | if (blk_mq_request_started(rq)) { |
| 1363 | bool *busy = priv; |
| 1364 | |
| 1365 | *busy = true; |
| 1366 | return false; |
| 1367 | } |
| 1368 | |
| 1369 | return true; |
| 1370 | } |
| 1371 | |
| 1372 | bool blk_mq_queue_inflight(struct request_queue *q) |
| 1373 | { |
| 1374 | bool busy = false; |
| 1375 | |
| 1376 | blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy); |
| 1377 | return busy; |
| 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(blk_mq_queue_inflight); |
| 1380 | |
| 1381 | static void blk_mq_rq_timed_out(struct request *req, bool reserved) |
| 1382 | { |
| 1383 | req->rq_flags |= RQF_TIMED_OUT; |
| 1384 | if (req->q->mq_ops->timeout) { |
| 1385 | enum blk_eh_timer_return ret; |
| 1386 | |
| 1387 | ret = req->q->mq_ops->timeout(req, reserved); |
| 1388 | if (ret == BLK_EH_DONE) |
| 1389 | return; |
| 1390 | WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER); |
| 1391 | } |
| 1392 | |
| 1393 | blk_add_timer(req); |
| 1394 | } |
| 1395 | |
| 1396 | static bool blk_mq_req_expired(struct request *rq, unsigned long *next) |
| 1397 | { |
| 1398 | unsigned long deadline; |
| 1399 | |
| 1400 | if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT) |
| 1401 | return false; |
| 1402 | if (rq->rq_flags & RQF_TIMED_OUT) |
| 1403 | return false; |
| 1404 | |
| 1405 | deadline = READ_ONCE(rq->deadline); |
| 1406 | if (time_after_eq(jiffies, deadline)) |
| 1407 | return true; |
| 1408 | |
| 1409 | if (*next == 0) |
| 1410 | *next = deadline; |
| 1411 | else if (time_after(*next, deadline)) |
| 1412 | *next = deadline; |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | void blk_mq_put_rq_ref(struct request *rq) |
| 1417 | { |
| 1418 | if (is_flush_rq(rq)) |
| 1419 | rq->end_io(rq, 0); |
| 1420 | else if (req_ref_put_and_test(rq)) |
| 1421 | __blk_mq_free_request(rq); |
| 1422 | } |
| 1423 | |
| 1424 | static bool blk_mq_check_expired(struct request *rq, void *priv, bool reserved) |
| 1425 | { |
| 1426 | unsigned long *next = priv; |
| 1427 | |
| 1428 | /* |
| 1429 | * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot |
| 1430 | * be reallocated underneath the timeout handler's processing, then |
| 1431 | * the expire check is reliable. If the request is not expired, then |
| 1432 | * it was completed and reallocated as a new request after returning |
| 1433 | * from blk_mq_check_expired(). |
| 1434 | */ |
| 1435 | if (blk_mq_req_expired(rq, next)) |
| 1436 | blk_mq_rq_timed_out(rq, reserved); |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
| 1440 | static void blk_mq_timeout_work(struct work_struct *work) |
| 1441 | { |
| 1442 | struct request_queue *q = |
| 1443 | container_of(work, struct request_queue, timeout_work); |
| 1444 | unsigned long next = 0; |
| 1445 | struct blk_mq_hw_ctx *hctx; |
| 1446 | unsigned long i; |
| 1447 | |
| 1448 | /* A deadlock might occur if a request is stuck requiring a |
| 1449 | * timeout at the same time a queue freeze is waiting |
| 1450 | * completion, since the timeout code would not be able to |
| 1451 | * acquire the queue reference here. |
| 1452 | * |
| 1453 | * That's why we don't use blk_queue_enter here; instead, we use |
| 1454 | * percpu_ref_tryget directly, because we need to be able to |
| 1455 | * obtain a reference even in the short window between the queue |
| 1456 | * starting to freeze, by dropping the first reference in |
| 1457 | * blk_freeze_queue_start, and the moment the last request is |
| 1458 | * consumed, marked by the instant q_usage_counter reaches |
| 1459 | * zero. |
| 1460 | */ |
| 1461 | if (!percpu_ref_tryget(&q->q_usage_counter)) |
| 1462 | return; |
| 1463 | |
| 1464 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next); |
| 1465 | |
| 1466 | if (next != 0) { |
| 1467 | mod_timer(&q->timeout, next); |
| 1468 | } else { |
| 1469 | /* |
| 1470 | * Request timeouts are handled as a forward rolling timer. If |
| 1471 | * we end up here it means that no requests are pending and |
| 1472 | * also that no request has been pending for a while. Mark |
| 1473 | * each hctx as idle. |
| 1474 | */ |
| 1475 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1476 | /* the hctx may be unmapped, so check it here */ |
| 1477 | if (blk_mq_hw_queue_mapped(hctx)) |
| 1478 | blk_mq_tag_idle(hctx); |
| 1479 | } |
| 1480 | } |
| 1481 | blk_queue_exit(q); |
| 1482 | } |
| 1483 | |
| 1484 | struct flush_busy_ctx_data { |
| 1485 | struct blk_mq_hw_ctx *hctx; |
| 1486 | struct list_head *list; |
| 1487 | }; |
| 1488 | |
| 1489 | static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) |
| 1490 | { |
| 1491 | struct flush_busy_ctx_data *flush_data = data; |
| 1492 | struct blk_mq_hw_ctx *hctx = flush_data->hctx; |
| 1493 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; |
| 1494 | enum hctx_type type = hctx->type; |
| 1495 | |
| 1496 | spin_lock(&ctx->lock); |
| 1497 | list_splice_tail_init(&ctx->rq_lists[type], flush_data->list); |
| 1498 | sbitmap_clear_bit(sb, bitnr); |
| 1499 | spin_unlock(&ctx->lock); |
| 1500 | return true; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
| 1504 | * Process software queues that have been marked busy, splicing them |
| 1505 | * to the for-dispatch |
| 1506 | */ |
| 1507 | void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
| 1508 | { |
| 1509 | struct flush_busy_ctx_data data = { |
| 1510 | .hctx = hctx, |
| 1511 | .list = list, |
| 1512 | }; |
| 1513 | |
| 1514 | sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); |
| 1515 | } |
| 1516 | EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs); |
| 1517 | |
| 1518 | struct dispatch_rq_data { |
| 1519 | struct blk_mq_hw_ctx *hctx; |
| 1520 | struct request *rq; |
| 1521 | }; |
| 1522 | |
| 1523 | static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr, |
| 1524 | void *data) |
| 1525 | { |
| 1526 | struct dispatch_rq_data *dispatch_data = data; |
| 1527 | struct blk_mq_hw_ctx *hctx = dispatch_data->hctx; |
| 1528 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; |
| 1529 | enum hctx_type type = hctx->type; |
| 1530 | |
| 1531 | spin_lock(&ctx->lock); |
| 1532 | if (!list_empty(&ctx->rq_lists[type])) { |
| 1533 | dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next); |
| 1534 | list_del_init(&dispatch_data->rq->queuelist); |
| 1535 | if (list_empty(&ctx->rq_lists[type])) |
| 1536 | sbitmap_clear_bit(sb, bitnr); |
| 1537 | } |
| 1538 | spin_unlock(&ctx->lock); |
| 1539 | |
| 1540 | return !dispatch_data->rq; |
| 1541 | } |
| 1542 | |
| 1543 | struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx, |
| 1544 | struct blk_mq_ctx *start) |
| 1545 | { |
| 1546 | unsigned off = start ? start->index_hw[hctx->type] : 0; |
| 1547 | struct dispatch_rq_data data = { |
| 1548 | .hctx = hctx, |
| 1549 | .rq = NULL, |
| 1550 | }; |
| 1551 | |
| 1552 | __sbitmap_for_each_set(&hctx->ctx_map, off, |
| 1553 | dispatch_rq_from_ctx, &data); |
| 1554 | |
| 1555 | return data.rq; |
| 1556 | } |
| 1557 | |
| 1558 | static bool __blk_mq_alloc_driver_tag(struct request *rq) |
| 1559 | { |
| 1560 | struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags; |
| 1561 | unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags; |
| 1562 | int tag; |
| 1563 | |
| 1564 | blk_mq_tag_busy(rq->mq_hctx); |
| 1565 | |
| 1566 | if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) { |
| 1567 | bt = &rq->mq_hctx->tags->breserved_tags; |
| 1568 | tag_offset = 0; |
| 1569 | } else { |
| 1570 | if (!hctx_may_queue(rq->mq_hctx, bt)) |
| 1571 | return false; |
| 1572 | } |
| 1573 | |
| 1574 | tag = __sbitmap_queue_get(bt); |
| 1575 | if (tag == BLK_MQ_NO_TAG) |
| 1576 | return false; |
| 1577 | |
| 1578 | rq->tag = tag + tag_offset; |
| 1579 | return true; |
| 1580 | } |
| 1581 | |
| 1582 | bool __blk_mq_get_driver_tag(struct blk_mq_hw_ctx *hctx, struct request *rq) |
| 1583 | { |
| 1584 | if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq)) |
| 1585 | return false; |
| 1586 | |
| 1587 | if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) && |
| 1588 | !(rq->rq_flags & RQF_MQ_INFLIGHT)) { |
| 1589 | rq->rq_flags |= RQF_MQ_INFLIGHT; |
| 1590 | __blk_mq_inc_active_requests(hctx); |
| 1591 | } |
| 1592 | hctx->tags->rqs[rq->tag] = rq; |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
| 1596 | static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, |
| 1597 | int flags, void *key) |
| 1598 | { |
| 1599 | struct blk_mq_hw_ctx *hctx; |
| 1600 | |
| 1601 | hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait); |
| 1602 | |
| 1603 | spin_lock(&hctx->dispatch_wait_lock); |
| 1604 | if (!list_empty(&wait->entry)) { |
| 1605 | struct sbitmap_queue *sbq; |
| 1606 | |
| 1607 | list_del_init(&wait->entry); |
| 1608 | sbq = &hctx->tags->bitmap_tags; |
| 1609 | atomic_dec(&sbq->ws_active); |
| 1610 | } |
| 1611 | spin_unlock(&hctx->dispatch_wait_lock); |
| 1612 | |
| 1613 | blk_mq_run_hw_queue(hctx, true); |
| 1614 | return 1; |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * Mark us waiting for a tag. For shared tags, this involves hooking us into |
| 1619 | * the tag wakeups. For non-shared tags, we can simply mark us needing a |
| 1620 | * restart. For both cases, take care to check the condition again after |
| 1621 | * marking us as waiting. |
| 1622 | */ |
| 1623 | static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx, |
| 1624 | struct request *rq) |
| 1625 | { |
| 1626 | struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags; |
| 1627 | struct wait_queue_head *wq; |
| 1628 | wait_queue_entry_t *wait; |
| 1629 | bool ret; |
| 1630 | |
| 1631 | if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { |
| 1632 | blk_mq_sched_mark_restart_hctx(hctx); |
| 1633 | |
| 1634 | /* |
| 1635 | * It's possible that a tag was freed in the window between the |
| 1636 | * allocation failure and adding the hardware queue to the wait |
| 1637 | * queue. |
| 1638 | * |
| 1639 | * Don't clear RESTART here, someone else could have set it. |
| 1640 | * At most this will cost an extra queue run. |
| 1641 | */ |
| 1642 | return blk_mq_get_driver_tag(rq); |
| 1643 | } |
| 1644 | |
| 1645 | wait = &hctx->dispatch_wait; |
| 1646 | if (!list_empty_careful(&wait->entry)) |
| 1647 | return false; |
| 1648 | |
| 1649 | wq = &bt_wait_ptr(sbq, hctx)->wait; |
| 1650 | |
| 1651 | spin_lock_irq(&wq->lock); |
| 1652 | spin_lock(&hctx->dispatch_wait_lock); |
| 1653 | if (!list_empty(&wait->entry)) { |
| 1654 | spin_unlock(&hctx->dispatch_wait_lock); |
| 1655 | spin_unlock_irq(&wq->lock); |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
| 1659 | atomic_inc(&sbq->ws_active); |
| 1660 | wait->flags &= ~WQ_FLAG_EXCLUSIVE; |
| 1661 | __add_wait_queue(wq, wait); |
| 1662 | |
| 1663 | /* |
| 1664 | * It's possible that a tag was freed in the window between the |
| 1665 | * allocation failure and adding the hardware queue to the wait |
| 1666 | * queue. |
| 1667 | */ |
| 1668 | ret = blk_mq_get_driver_tag(rq); |
| 1669 | if (!ret) { |
| 1670 | spin_unlock(&hctx->dispatch_wait_lock); |
| 1671 | spin_unlock_irq(&wq->lock); |
| 1672 | return false; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * We got a tag, remove ourselves from the wait queue to ensure |
| 1677 | * someone else gets the wakeup. |
| 1678 | */ |
| 1679 | list_del_init(&wait->entry); |
| 1680 | atomic_dec(&sbq->ws_active); |
| 1681 | spin_unlock(&hctx->dispatch_wait_lock); |
| 1682 | spin_unlock_irq(&wq->lock); |
| 1683 | |
| 1684 | return true; |
| 1685 | } |
| 1686 | |
| 1687 | #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8 |
| 1688 | #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4 |
| 1689 | /* |
| 1690 | * Update dispatch busy with the Exponential Weighted Moving Average(EWMA): |
| 1691 | * - EWMA is one simple way to compute running average value |
| 1692 | * - weight(7/8 and 1/8) is applied so that it can decrease exponentially |
| 1693 | * - take 4 as factor for avoiding to get too small(0) result, and this |
| 1694 | * factor doesn't matter because EWMA decreases exponentially |
| 1695 | */ |
| 1696 | static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy) |
| 1697 | { |
| 1698 | unsigned int ewma; |
| 1699 | |
| 1700 | ewma = hctx->dispatch_busy; |
| 1701 | |
| 1702 | if (!ewma && !busy) |
| 1703 | return; |
| 1704 | |
| 1705 | ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1; |
| 1706 | if (busy) |
| 1707 | ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR; |
| 1708 | ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT; |
| 1709 | |
| 1710 | hctx->dispatch_busy = ewma; |
| 1711 | } |
| 1712 | |
| 1713 | #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */ |
| 1714 | |
| 1715 | static void blk_mq_handle_dev_resource(struct request *rq, |
| 1716 | struct list_head *list) |
| 1717 | { |
| 1718 | struct request *next = |
| 1719 | list_first_entry_or_null(list, struct request, queuelist); |
| 1720 | |
| 1721 | /* |
| 1722 | * If an I/O scheduler has been configured and we got a driver tag for |
| 1723 | * the next request already, free it. |
| 1724 | */ |
| 1725 | if (next) |
| 1726 | blk_mq_put_driver_tag(next); |
| 1727 | |
| 1728 | list_add(&rq->queuelist, list); |
| 1729 | __blk_mq_requeue_request(rq); |
| 1730 | } |
| 1731 | |
| 1732 | static void blk_mq_handle_zone_resource(struct request *rq, |
| 1733 | struct list_head *zone_list) |
| 1734 | { |
| 1735 | /* |
| 1736 | * If we end up here it is because we cannot dispatch a request to a |
| 1737 | * specific zone due to LLD level zone-write locking or other zone |
| 1738 | * related resource not being available. In this case, set the request |
| 1739 | * aside in zone_list for retrying it later. |
| 1740 | */ |
| 1741 | list_add(&rq->queuelist, zone_list); |
| 1742 | __blk_mq_requeue_request(rq); |
| 1743 | } |
| 1744 | |
| 1745 | enum prep_dispatch { |
| 1746 | PREP_DISPATCH_OK, |
| 1747 | PREP_DISPATCH_NO_TAG, |
| 1748 | PREP_DISPATCH_NO_BUDGET, |
| 1749 | }; |
| 1750 | |
| 1751 | static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq, |
| 1752 | bool need_budget) |
| 1753 | { |
| 1754 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
| 1755 | int budget_token = -1; |
| 1756 | |
| 1757 | if (need_budget) { |
| 1758 | budget_token = blk_mq_get_dispatch_budget(rq->q); |
| 1759 | if (budget_token < 0) { |
| 1760 | blk_mq_put_driver_tag(rq); |
| 1761 | return PREP_DISPATCH_NO_BUDGET; |
| 1762 | } |
| 1763 | blk_mq_set_rq_budget_token(rq, budget_token); |
| 1764 | } |
| 1765 | |
| 1766 | if (!blk_mq_get_driver_tag(rq)) { |
| 1767 | /* |
| 1768 | * The initial allocation attempt failed, so we need to |
| 1769 | * rerun the hardware queue when a tag is freed. The |
| 1770 | * waitqueue takes care of that. If the queue is run |
| 1771 | * before we add this entry back on the dispatch list, |
| 1772 | * we'll re-run it below. |
| 1773 | */ |
| 1774 | if (!blk_mq_mark_tag_wait(hctx, rq)) { |
| 1775 | /* |
| 1776 | * All budgets not got from this function will be put |
| 1777 | * together during handling partial dispatch |
| 1778 | */ |
| 1779 | if (need_budget) |
| 1780 | blk_mq_put_dispatch_budget(rq->q, budget_token); |
| 1781 | return PREP_DISPATCH_NO_TAG; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | return PREP_DISPATCH_OK; |
| 1786 | } |
| 1787 | |
| 1788 | /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */ |
| 1789 | static void blk_mq_release_budgets(struct request_queue *q, |
| 1790 | struct list_head *list) |
| 1791 | { |
| 1792 | struct request *rq; |
| 1793 | |
| 1794 | list_for_each_entry(rq, list, queuelist) { |
| 1795 | int budget_token = blk_mq_get_rq_budget_token(rq); |
| 1796 | |
| 1797 | if (budget_token >= 0) |
| 1798 | blk_mq_put_dispatch_budget(q, budget_token); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * Returns true if we did some work AND can potentially do more. |
| 1804 | */ |
| 1805 | bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list, |
| 1806 | unsigned int nr_budgets) |
| 1807 | { |
| 1808 | enum prep_dispatch prep; |
| 1809 | struct request_queue *q = hctx->queue; |
| 1810 | struct request *rq, *nxt; |
| 1811 | int errors, queued; |
| 1812 | blk_status_t ret = BLK_STS_OK; |
| 1813 | LIST_HEAD(zone_list); |
| 1814 | bool needs_resource = false; |
| 1815 | |
| 1816 | if (list_empty(list)) |
| 1817 | return false; |
| 1818 | |
| 1819 | /* |
| 1820 | * Now process all the entries, sending them to the driver. |
| 1821 | */ |
| 1822 | errors = queued = 0; |
| 1823 | do { |
| 1824 | struct blk_mq_queue_data bd; |
| 1825 | |
| 1826 | rq = list_first_entry(list, struct request, queuelist); |
| 1827 | |
| 1828 | WARN_ON_ONCE(hctx != rq->mq_hctx); |
| 1829 | prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets); |
| 1830 | if (prep != PREP_DISPATCH_OK) |
| 1831 | break; |
| 1832 | |
| 1833 | list_del_init(&rq->queuelist); |
| 1834 | |
| 1835 | bd.rq = rq; |
| 1836 | |
| 1837 | /* |
| 1838 | * Flag last if we have no more requests, or if we have more |
| 1839 | * but can't assign a driver tag to it. |
| 1840 | */ |
| 1841 | if (list_empty(list)) |
| 1842 | bd.last = true; |
| 1843 | else { |
| 1844 | nxt = list_first_entry(list, struct request, queuelist); |
| 1845 | bd.last = !blk_mq_get_driver_tag(nxt); |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * once the request is queued to lld, no need to cover the |
| 1850 | * budget any more |
| 1851 | */ |
| 1852 | if (nr_budgets) |
| 1853 | nr_budgets--; |
| 1854 | ret = q->mq_ops->queue_rq(hctx, &bd); |
| 1855 | switch (ret) { |
| 1856 | case BLK_STS_OK: |
| 1857 | queued++; |
| 1858 | break; |
| 1859 | case BLK_STS_RESOURCE: |
| 1860 | needs_resource = true; |
| 1861 | fallthrough; |
| 1862 | case BLK_STS_DEV_RESOURCE: |
| 1863 | blk_mq_handle_dev_resource(rq, list); |
| 1864 | goto out; |
| 1865 | case BLK_STS_ZONE_RESOURCE: |
| 1866 | /* |
| 1867 | * Move the request to zone_list and keep going through |
| 1868 | * the dispatch list to find more requests the drive can |
| 1869 | * accept. |
| 1870 | */ |
| 1871 | blk_mq_handle_zone_resource(rq, &zone_list); |
| 1872 | needs_resource = true; |
| 1873 | break; |
| 1874 | default: |
| 1875 | errors++; |
| 1876 | blk_mq_end_request(rq, ret); |
| 1877 | } |
| 1878 | } while (!list_empty(list)); |
| 1879 | out: |
| 1880 | if (!list_empty(&zone_list)) |
| 1881 | list_splice_tail_init(&zone_list, list); |
| 1882 | |
| 1883 | /* If we didn't flush the entire list, we could have told the driver |
| 1884 | * there was more coming, but that turned out to be a lie. |
| 1885 | */ |
| 1886 | if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued) |
| 1887 | q->mq_ops->commit_rqs(hctx); |
| 1888 | /* |
| 1889 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 1890 | * that is where we will continue on next queue run. |
| 1891 | */ |
| 1892 | if (!list_empty(list)) { |
| 1893 | bool needs_restart; |
| 1894 | /* For non-shared tags, the RESTART check will suffice */ |
| 1895 | bool no_tag = prep == PREP_DISPATCH_NO_TAG && |
| 1896 | (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED); |
| 1897 | |
| 1898 | if (nr_budgets) |
| 1899 | blk_mq_release_budgets(q, list); |
| 1900 | |
| 1901 | spin_lock(&hctx->lock); |
| 1902 | list_splice_tail_init(list, &hctx->dispatch); |
| 1903 | spin_unlock(&hctx->lock); |
| 1904 | |
| 1905 | /* |
| 1906 | * Order adding requests to hctx->dispatch and checking |
| 1907 | * SCHED_RESTART flag. The pair of this smp_mb() is the one |
| 1908 | * in blk_mq_sched_restart(). Avoid restart code path to |
| 1909 | * miss the new added requests to hctx->dispatch, meantime |
| 1910 | * SCHED_RESTART is observed here. |
| 1911 | */ |
| 1912 | smp_mb(); |
| 1913 | |
| 1914 | /* |
| 1915 | * If SCHED_RESTART was set by the caller of this function and |
| 1916 | * it is no longer set that means that it was cleared by another |
| 1917 | * thread and hence that a queue rerun is needed. |
| 1918 | * |
| 1919 | * If 'no_tag' is set, that means that we failed getting |
| 1920 | * a driver tag with an I/O scheduler attached. If our dispatch |
| 1921 | * waitqueue is no longer active, ensure that we run the queue |
| 1922 | * AFTER adding our entries back to the list. |
| 1923 | * |
| 1924 | * If no I/O scheduler has been configured it is possible that |
| 1925 | * the hardware queue got stopped and restarted before requests |
| 1926 | * were pushed back onto the dispatch list. Rerun the queue to |
| 1927 | * avoid starvation. Notes: |
| 1928 | * - blk_mq_run_hw_queue() checks whether or not a queue has |
| 1929 | * been stopped before rerunning a queue. |
| 1930 | * - Some but not all block drivers stop a queue before |
| 1931 | * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq |
| 1932 | * and dm-rq. |
| 1933 | * |
| 1934 | * If driver returns BLK_STS_RESOURCE and SCHED_RESTART |
| 1935 | * bit is set, run queue after a delay to avoid IO stalls |
| 1936 | * that could otherwise occur if the queue is idle. We'll do |
| 1937 | * similar if we couldn't get budget or couldn't lock a zone |
| 1938 | * and SCHED_RESTART is set. |
| 1939 | */ |
| 1940 | needs_restart = blk_mq_sched_needs_restart(hctx); |
| 1941 | if (prep == PREP_DISPATCH_NO_BUDGET) |
| 1942 | needs_resource = true; |
| 1943 | if (!needs_restart || |
| 1944 | (no_tag && list_empty_careful(&hctx->dispatch_wait.entry))) |
| 1945 | blk_mq_run_hw_queue(hctx, true); |
| 1946 | else if (needs_restart && needs_resource) |
| 1947 | blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY); |
| 1948 | |
| 1949 | blk_mq_update_dispatch_busy(hctx, true); |
| 1950 | return false; |
| 1951 | } else |
| 1952 | blk_mq_update_dispatch_busy(hctx, false); |
| 1953 | |
| 1954 | return (queued + errors) != 0; |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * __blk_mq_run_hw_queue - Run a hardware queue. |
| 1959 | * @hctx: Pointer to the hardware queue to run. |
| 1960 | * |
| 1961 | * Send pending requests to the hardware. |
| 1962 | */ |
| 1963 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 1964 | { |
| 1965 | /* |
| 1966 | * We can't run the queue inline with ints disabled. Ensure that |
| 1967 | * we catch bad users of this early. |
| 1968 | */ |
| 1969 | WARN_ON_ONCE(in_interrupt()); |
| 1970 | |
| 1971 | blk_mq_run_dispatch_ops(hctx->queue, |
| 1972 | blk_mq_sched_dispatch_requests(hctx)); |
| 1973 | } |
| 1974 | |
| 1975 | static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx) |
| 1976 | { |
| 1977 | int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask); |
| 1978 | |
| 1979 | if (cpu >= nr_cpu_ids) |
| 1980 | cpu = cpumask_first(hctx->cpumask); |
| 1981 | return cpu; |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * It'd be great if the workqueue API had a way to pass |
| 1986 | * in a mask and had some smarts for more clever placement. |
| 1987 | * For now we just round-robin here, switching for every |
| 1988 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 1989 | */ |
| 1990 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 1991 | { |
| 1992 | bool tried = false; |
| 1993 | int next_cpu = hctx->next_cpu; |
| 1994 | |
| 1995 | if (hctx->queue->nr_hw_queues == 1) |
| 1996 | return WORK_CPU_UNBOUND; |
| 1997 | |
| 1998 | if (--hctx->next_cpu_batch <= 0) { |
| 1999 | select_cpu: |
| 2000 | next_cpu = cpumask_next_and(next_cpu, hctx->cpumask, |
| 2001 | cpu_online_mask); |
| 2002 | if (next_cpu >= nr_cpu_ids) |
| 2003 | next_cpu = blk_mq_first_mapped_cpu(hctx); |
| 2004 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 2005 | } |
| 2006 | |
| 2007 | /* |
| 2008 | * Do unbound schedule if we can't find a online CPU for this hctx, |
| 2009 | * and it should only happen in the path of handling CPU DEAD. |
| 2010 | */ |
| 2011 | if (!cpu_online(next_cpu)) { |
| 2012 | if (!tried) { |
| 2013 | tried = true; |
| 2014 | goto select_cpu; |
| 2015 | } |
| 2016 | |
| 2017 | /* |
| 2018 | * Make sure to re-select CPU next time once after CPUs |
| 2019 | * in hctx->cpumask become online again. |
| 2020 | */ |
| 2021 | hctx->next_cpu = next_cpu; |
| 2022 | hctx->next_cpu_batch = 1; |
| 2023 | return WORK_CPU_UNBOUND; |
| 2024 | } |
| 2025 | |
| 2026 | hctx->next_cpu = next_cpu; |
| 2027 | return next_cpu; |
| 2028 | } |
| 2029 | |
| 2030 | /** |
| 2031 | * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue. |
| 2032 | * @hctx: Pointer to the hardware queue to run. |
| 2033 | * @async: If we want to run the queue asynchronously. |
| 2034 | * @msecs: Milliseconds of delay to wait before running the queue. |
| 2035 | * |
| 2036 | * If !@async, try to run the queue now. Else, run the queue asynchronously and |
| 2037 | * with a delay of @msecs. |
| 2038 | */ |
| 2039 | static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async, |
| 2040 | unsigned long msecs) |
| 2041 | { |
| 2042 | if (unlikely(blk_mq_hctx_stopped(hctx))) |
| 2043 | return; |
| 2044 | |
| 2045 | if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { |
| 2046 | int cpu = get_cpu(); |
| 2047 | if (cpumask_test_cpu(cpu, hctx->cpumask)) { |
| 2048 | __blk_mq_run_hw_queue(hctx); |
| 2049 | put_cpu(); |
| 2050 | return; |
| 2051 | } |
| 2052 | |
| 2053 | put_cpu(); |
| 2054 | } |
| 2055 | |
| 2056 | kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work, |
| 2057 | msecs_to_jiffies(msecs)); |
| 2058 | } |
| 2059 | |
| 2060 | /** |
| 2061 | * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously. |
| 2062 | * @hctx: Pointer to the hardware queue to run. |
| 2063 | * @msecs: Milliseconds of delay to wait before running the queue. |
| 2064 | * |
| 2065 | * Run a hardware queue asynchronously with a delay of @msecs. |
| 2066 | */ |
| 2067 | void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 2068 | { |
| 2069 | __blk_mq_delay_run_hw_queue(hctx, true, msecs); |
| 2070 | } |
| 2071 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); |
| 2072 | |
| 2073 | /** |
| 2074 | * blk_mq_run_hw_queue - Start to run a hardware queue. |
| 2075 | * @hctx: Pointer to the hardware queue to run. |
| 2076 | * @async: If we want to run the queue asynchronously. |
| 2077 | * |
| 2078 | * Check if the request queue is not in a quiesced state and if there are |
| 2079 | * pending requests to be sent. If this is true, run the queue to send requests |
| 2080 | * to hardware. |
| 2081 | */ |
| 2082 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 2083 | { |
| 2084 | bool need_run; |
| 2085 | |
| 2086 | /* |
| 2087 | * When queue is quiesced, we may be switching io scheduler, or |
| 2088 | * updating nr_hw_queues, or other things, and we can't run queue |
| 2089 | * any more, even __blk_mq_hctx_has_pending() can't be called safely. |
| 2090 | * |
| 2091 | * And queue will be rerun in blk_mq_unquiesce_queue() if it is |
| 2092 | * quiesced. |
| 2093 | */ |
| 2094 | __blk_mq_run_dispatch_ops(hctx->queue, false, |
| 2095 | need_run = !blk_queue_quiesced(hctx->queue) && |
| 2096 | blk_mq_hctx_has_pending(hctx)); |
| 2097 | |
| 2098 | if (need_run) |
| 2099 | __blk_mq_delay_run_hw_queue(hctx, async, 0); |
| 2100 | } |
| 2101 | EXPORT_SYMBOL(blk_mq_run_hw_queue); |
| 2102 | |
| 2103 | /* |
| 2104 | * Is the request queue handled by an IO scheduler that does not respect |
| 2105 | * hardware queues when dispatching? |
| 2106 | */ |
| 2107 | static bool blk_mq_has_sqsched(struct request_queue *q) |
| 2108 | { |
| 2109 | struct elevator_queue *e = q->elevator; |
| 2110 | |
| 2111 | if (e && e->type->ops.dispatch_request && |
| 2112 | !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE)) |
| 2113 | return true; |
| 2114 | return false; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
| 2118 | * Return prefered queue to dispatch from (if any) for non-mq aware IO |
| 2119 | * scheduler. |
| 2120 | */ |
| 2121 | static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q) |
| 2122 | { |
| 2123 | struct blk_mq_hw_ctx *hctx; |
| 2124 | |
| 2125 | /* |
| 2126 | * If the IO scheduler does not respect hardware queues when |
| 2127 | * dispatching, we just don't bother with multiple HW queues and |
| 2128 | * dispatch from hctx for the current CPU since running multiple queues |
| 2129 | * just causes lock contention inside the scheduler and pointless cache |
| 2130 | * bouncing. |
| 2131 | */ |
| 2132 | hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT, |
| 2133 | raw_smp_processor_id()); |
| 2134 | if (!blk_mq_hctx_stopped(hctx)) |
| 2135 | return hctx; |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | |
| 2139 | /** |
| 2140 | * blk_mq_run_hw_queues - Run all hardware queues in a request queue. |
| 2141 | * @q: Pointer to the request queue to run. |
| 2142 | * @async: If we want to run the queue asynchronously. |
| 2143 | */ |
| 2144 | void blk_mq_run_hw_queues(struct request_queue *q, bool async) |
| 2145 | { |
| 2146 | struct blk_mq_hw_ctx *hctx, *sq_hctx; |
| 2147 | unsigned long i; |
| 2148 | |
| 2149 | sq_hctx = NULL; |
| 2150 | if (blk_mq_has_sqsched(q)) |
| 2151 | sq_hctx = blk_mq_get_sq_hctx(q); |
| 2152 | queue_for_each_hw_ctx(q, hctx, i) { |
| 2153 | if (blk_mq_hctx_stopped(hctx)) |
| 2154 | continue; |
| 2155 | /* |
| 2156 | * Dispatch from this hctx either if there's no hctx preferred |
| 2157 | * by IO scheduler or if it has requests that bypass the |
| 2158 | * scheduler. |
| 2159 | */ |
| 2160 | if (!sq_hctx || sq_hctx == hctx || |
| 2161 | !list_empty_careful(&hctx->dispatch)) |
| 2162 | blk_mq_run_hw_queue(hctx, async); |
| 2163 | } |
| 2164 | } |
| 2165 | EXPORT_SYMBOL(blk_mq_run_hw_queues); |
| 2166 | |
| 2167 | /** |
| 2168 | * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously. |
| 2169 | * @q: Pointer to the request queue to run. |
| 2170 | * @msecs: Milliseconds of delay to wait before running the queues. |
| 2171 | */ |
| 2172 | void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs) |
| 2173 | { |
| 2174 | struct blk_mq_hw_ctx *hctx, *sq_hctx; |
| 2175 | unsigned long i; |
| 2176 | |
| 2177 | sq_hctx = NULL; |
| 2178 | if (blk_mq_has_sqsched(q)) |
| 2179 | sq_hctx = blk_mq_get_sq_hctx(q); |
| 2180 | queue_for_each_hw_ctx(q, hctx, i) { |
| 2181 | if (blk_mq_hctx_stopped(hctx)) |
| 2182 | continue; |
| 2183 | /* |
| 2184 | * If there is already a run_work pending, leave the |
| 2185 | * pending delay untouched. Otherwise, a hctx can stall |
| 2186 | * if another hctx is re-delaying the other's work |
| 2187 | * before the work executes. |
| 2188 | */ |
| 2189 | if (delayed_work_pending(&hctx->run_work)) |
| 2190 | continue; |
| 2191 | /* |
| 2192 | * Dispatch from this hctx either if there's no hctx preferred |
| 2193 | * by IO scheduler or if it has requests that bypass the |
| 2194 | * scheduler. |
| 2195 | */ |
| 2196 | if (!sq_hctx || sq_hctx == hctx || |
| 2197 | !list_empty_careful(&hctx->dispatch)) |
| 2198 | blk_mq_delay_run_hw_queue(hctx, msecs); |
| 2199 | } |
| 2200 | } |
| 2201 | EXPORT_SYMBOL(blk_mq_delay_run_hw_queues); |
| 2202 | |
| 2203 | /** |
| 2204 | * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped |
| 2205 | * @q: request queue. |
| 2206 | * |
| 2207 | * The caller is responsible for serializing this function against |
| 2208 | * blk_mq_{start,stop}_hw_queue(). |
| 2209 | */ |
| 2210 | bool blk_mq_queue_stopped(struct request_queue *q) |
| 2211 | { |
| 2212 | struct blk_mq_hw_ctx *hctx; |
| 2213 | unsigned long i; |
| 2214 | |
| 2215 | queue_for_each_hw_ctx(q, hctx, i) |
| 2216 | if (blk_mq_hctx_stopped(hctx)) |
| 2217 | return true; |
| 2218 | |
| 2219 | return false; |
| 2220 | } |
| 2221 | EXPORT_SYMBOL(blk_mq_queue_stopped); |
| 2222 | |
| 2223 | /* |
| 2224 | * This function is often used for pausing .queue_rq() by driver when |
| 2225 | * there isn't enough resource or some conditions aren't satisfied, and |
| 2226 | * BLK_STS_RESOURCE is usually returned. |
| 2227 | * |
| 2228 | * We do not guarantee that dispatch can be drained or blocked |
| 2229 | * after blk_mq_stop_hw_queue() returns. Please use |
| 2230 | * blk_mq_quiesce_queue() for that requirement. |
| 2231 | */ |
| 2232 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 2233 | { |
| 2234 | cancel_delayed_work(&hctx->run_work); |
| 2235 | |
| 2236 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 2237 | } |
| 2238 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 2239 | |
| 2240 | /* |
| 2241 | * This function is often used for pausing .queue_rq() by driver when |
| 2242 | * there isn't enough resource or some conditions aren't satisfied, and |
| 2243 | * BLK_STS_RESOURCE is usually returned. |
| 2244 | * |
| 2245 | * We do not guarantee that dispatch can be drained or blocked |
| 2246 | * after blk_mq_stop_hw_queues() returns. Please use |
| 2247 | * blk_mq_quiesce_queue() for that requirement. |
| 2248 | */ |
| 2249 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 2250 | { |
| 2251 | struct blk_mq_hw_ctx *hctx; |
| 2252 | unsigned long i; |
| 2253 | |
| 2254 | queue_for_each_hw_ctx(q, hctx, i) |
| 2255 | blk_mq_stop_hw_queue(hctx); |
| 2256 | } |
| 2257 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 2258 | |
| 2259 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 2260 | { |
| 2261 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 2262 | |
| 2263 | blk_mq_run_hw_queue(hctx, false); |
| 2264 | } |
| 2265 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 2266 | |
| 2267 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 2268 | { |
| 2269 | struct blk_mq_hw_ctx *hctx; |
| 2270 | unsigned long i; |
| 2271 | |
| 2272 | queue_for_each_hw_ctx(q, hctx, i) |
| 2273 | blk_mq_start_hw_queue(hctx); |
| 2274 | } |
| 2275 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 2276 | |
| 2277 | void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 2278 | { |
| 2279 | if (!blk_mq_hctx_stopped(hctx)) |
| 2280 | return; |
| 2281 | |
| 2282 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 2283 | blk_mq_run_hw_queue(hctx, async); |
| 2284 | } |
| 2285 | EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); |
| 2286 | |
| 2287 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
| 2288 | { |
| 2289 | struct blk_mq_hw_ctx *hctx; |
| 2290 | unsigned long i; |
| 2291 | |
| 2292 | queue_for_each_hw_ctx(q, hctx, i) |
| 2293 | blk_mq_start_stopped_hw_queue(hctx, async); |
| 2294 | } |
| 2295 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 2296 | |
| 2297 | static void blk_mq_run_work_fn(struct work_struct *work) |
| 2298 | { |
| 2299 | struct blk_mq_hw_ctx *hctx; |
| 2300 | |
| 2301 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
| 2302 | |
| 2303 | /* |
| 2304 | * If we are stopped, don't run the queue. |
| 2305 | */ |
| 2306 | if (blk_mq_hctx_stopped(hctx)) |
| 2307 | return; |
| 2308 | |
| 2309 | __blk_mq_run_hw_queue(hctx); |
| 2310 | } |
| 2311 | |
| 2312 | static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, |
| 2313 | struct request *rq, |
| 2314 | bool at_head) |
| 2315 | { |
| 2316 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 2317 | enum hctx_type type = hctx->type; |
| 2318 | |
| 2319 | lockdep_assert_held(&ctx->lock); |
| 2320 | |
| 2321 | trace_block_rq_insert(rq); |
| 2322 | |
| 2323 | if (at_head) |
| 2324 | list_add(&rq->queuelist, &ctx->rq_lists[type]); |
| 2325 | else |
| 2326 | list_add_tail(&rq->queuelist, &ctx->rq_lists[type]); |
| 2327 | } |
| 2328 | |
| 2329 | void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, |
| 2330 | bool at_head) |
| 2331 | { |
| 2332 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 2333 | |
| 2334 | lockdep_assert_held(&ctx->lock); |
| 2335 | |
| 2336 | __blk_mq_insert_req_list(hctx, rq, at_head); |
| 2337 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * blk_mq_request_bypass_insert - Insert a request at dispatch list. |
| 2342 | * @rq: Pointer to request to be inserted. |
| 2343 | * @at_head: true if the request should be inserted at the head of the list. |
| 2344 | * @run_queue: If we should run the hardware queue after inserting the request. |
| 2345 | * |
| 2346 | * Should only be used carefully, when the caller knows we want to |
| 2347 | * bypass a potential IO scheduler on the target device. |
| 2348 | */ |
| 2349 | void blk_mq_request_bypass_insert(struct request *rq, bool at_head, |
| 2350 | bool run_queue) |
| 2351 | { |
| 2352 | struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
| 2353 | |
| 2354 | spin_lock(&hctx->lock); |
| 2355 | if (at_head) |
| 2356 | list_add(&rq->queuelist, &hctx->dispatch); |
| 2357 | else |
| 2358 | list_add_tail(&rq->queuelist, &hctx->dispatch); |
| 2359 | spin_unlock(&hctx->lock); |
| 2360 | |
| 2361 | if (run_queue) |
| 2362 | blk_mq_run_hw_queue(hctx, false); |
| 2363 | } |
| 2364 | |
| 2365 | void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, |
| 2366 | struct list_head *list) |
| 2367 | |
| 2368 | { |
| 2369 | struct request *rq; |
| 2370 | enum hctx_type type = hctx->type; |
| 2371 | |
| 2372 | /* |
| 2373 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 2374 | * offline now |
| 2375 | */ |
| 2376 | list_for_each_entry(rq, list, queuelist) { |
| 2377 | BUG_ON(rq->mq_ctx != ctx); |
| 2378 | trace_block_rq_insert(rq); |
| 2379 | } |
| 2380 | |
| 2381 | spin_lock(&ctx->lock); |
| 2382 | list_splice_tail_init(list, &ctx->rq_lists[type]); |
| 2383 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 2384 | spin_unlock(&ctx->lock); |
| 2385 | } |
| 2386 | |
| 2387 | static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int *queued, |
| 2388 | bool from_schedule) |
| 2389 | { |
| 2390 | if (hctx->queue->mq_ops->commit_rqs) { |
| 2391 | trace_block_unplug(hctx->queue, *queued, !from_schedule); |
| 2392 | hctx->queue->mq_ops->commit_rqs(hctx); |
| 2393 | } |
| 2394 | *queued = 0; |
| 2395 | } |
| 2396 | |
| 2397 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, |
| 2398 | unsigned int nr_segs) |
| 2399 | { |
| 2400 | int err; |
| 2401 | |
| 2402 | if (bio->bi_opf & REQ_RAHEAD) |
| 2403 | rq->cmd_flags |= REQ_FAILFAST_MASK; |
| 2404 | |
| 2405 | rq->__sector = bio->bi_iter.bi_sector; |
| 2406 | rq->write_hint = bio->bi_write_hint; |
| 2407 | blk_rq_bio_prep(rq, bio, nr_segs); |
| 2408 | |
| 2409 | /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */ |
| 2410 | err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO); |
| 2411 | WARN_ON_ONCE(err); |
| 2412 | |
| 2413 | blk_account_io_start(rq); |
| 2414 | } |
| 2415 | |
| 2416 | static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 2417 | struct request *rq, bool last) |
| 2418 | { |
| 2419 | struct request_queue *q = rq->q; |
| 2420 | struct blk_mq_queue_data bd = { |
| 2421 | .rq = rq, |
| 2422 | .last = last, |
| 2423 | }; |
| 2424 | blk_status_t ret; |
| 2425 | |
| 2426 | /* |
| 2427 | * For OK queue, we are done. For error, caller may kill it. |
| 2428 | * Any other error (busy), just add it to our list as we |
| 2429 | * previously would have done. |
| 2430 | */ |
| 2431 | ret = q->mq_ops->queue_rq(hctx, &bd); |
| 2432 | switch (ret) { |
| 2433 | case BLK_STS_OK: |
| 2434 | blk_mq_update_dispatch_busy(hctx, false); |
| 2435 | break; |
| 2436 | case BLK_STS_RESOURCE: |
| 2437 | case BLK_STS_DEV_RESOURCE: |
| 2438 | blk_mq_update_dispatch_busy(hctx, true); |
| 2439 | __blk_mq_requeue_request(rq); |
| 2440 | break; |
| 2441 | default: |
| 2442 | blk_mq_update_dispatch_busy(hctx, false); |
| 2443 | break; |
| 2444 | } |
| 2445 | |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
| 2449 | static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 2450 | struct request *rq, |
| 2451 | bool bypass_insert, bool last) |
| 2452 | { |
| 2453 | struct request_queue *q = rq->q; |
| 2454 | bool run_queue = true; |
| 2455 | int budget_token; |
| 2456 | |
| 2457 | /* |
| 2458 | * RCU or SRCU read lock is needed before checking quiesced flag. |
| 2459 | * |
| 2460 | * When queue is stopped or quiesced, ignore 'bypass_insert' from |
| 2461 | * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller, |
| 2462 | * and avoid driver to try to dispatch again. |
| 2463 | */ |
| 2464 | if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) { |
| 2465 | run_queue = false; |
| 2466 | bypass_insert = false; |
| 2467 | goto insert; |
| 2468 | } |
| 2469 | |
| 2470 | if ((rq->rq_flags & RQF_ELV) && !bypass_insert) |
| 2471 | goto insert; |
| 2472 | |
| 2473 | budget_token = blk_mq_get_dispatch_budget(q); |
| 2474 | if (budget_token < 0) |
| 2475 | goto insert; |
| 2476 | |
| 2477 | blk_mq_set_rq_budget_token(rq, budget_token); |
| 2478 | |
| 2479 | if (!blk_mq_get_driver_tag(rq)) { |
| 2480 | blk_mq_put_dispatch_budget(q, budget_token); |
| 2481 | goto insert; |
| 2482 | } |
| 2483 | |
| 2484 | return __blk_mq_issue_directly(hctx, rq, last); |
| 2485 | insert: |
| 2486 | if (bypass_insert) |
| 2487 | return BLK_STS_RESOURCE; |
| 2488 | |
| 2489 | blk_mq_sched_insert_request(rq, false, run_queue, false); |
| 2490 | |
| 2491 | return BLK_STS_OK; |
| 2492 | } |
| 2493 | |
| 2494 | /** |
| 2495 | * blk_mq_try_issue_directly - Try to send a request directly to device driver. |
| 2496 | * @hctx: Pointer of the associated hardware queue. |
| 2497 | * @rq: Pointer to request to be sent. |
| 2498 | * |
| 2499 | * If the device has enough resources to accept a new request now, send the |
| 2500 | * request directly to device driver. Else, insert at hctx->dispatch queue, so |
| 2501 | * we can try send it another time in the future. Requests inserted at this |
| 2502 | * queue have higher priority. |
| 2503 | */ |
| 2504 | static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 2505 | struct request *rq) |
| 2506 | { |
| 2507 | blk_status_t ret = |
| 2508 | __blk_mq_try_issue_directly(hctx, rq, false, true); |
| 2509 | |
| 2510 | if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) |
| 2511 | blk_mq_request_bypass_insert(rq, false, true); |
| 2512 | else if (ret != BLK_STS_OK) |
| 2513 | blk_mq_end_request(rq, ret); |
| 2514 | } |
| 2515 | |
| 2516 | static blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last) |
| 2517 | { |
| 2518 | return __blk_mq_try_issue_directly(rq->mq_hctx, rq, true, last); |
| 2519 | } |
| 2520 | |
| 2521 | static void blk_mq_plug_issue_direct(struct blk_plug *plug, bool from_schedule) |
| 2522 | { |
| 2523 | struct blk_mq_hw_ctx *hctx = NULL; |
| 2524 | struct request *rq; |
| 2525 | int queued = 0; |
| 2526 | int errors = 0; |
| 2527 | |
| 2528 | while ((rq = rq_list_pop(&plug->mq_list))) { |
| 2529 | bool last = rq_list_empty(plug->mq_list); |
| 2530 | blk_status_t ret; |
| 2531 | |
| 2532 | if (hctx != rq->mq_hctx) { |
| 2533 | if (hctx) |
| 2534 | blk_mq_commit_rqs(hctx, &queued, from_schedule); |
| 2535 | hctx = rq->mq_hctx; |
| 2536 | } |
| 2537 | |
| 2538 | ret = blk_mq_request_issue_directly(rq, last); |
| 2539 | switch (ret) { |
| 2540 | case BLK_STS_OK: |
| 2541 | queued++; |
| 2542 | break; |
| 2543 | case BLK_STS_RESOURCE: |
| 2544 | case BLK_STS_DEV_RESOURCE: |
| 2545 | blk_mq_request_bypass_insert(rq, false, last); |
| 2546 | blk_mq_commit_rqs(hctx, &queued, from_schedule); |
| 2547 | return; |
| 2548 | default: |
| 2549 | blk_mq_end_request(rq, ret); |
| 2550 | errors++; |
| 2551 | break; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * If we didn't flush the entire list, we could have told the driver |
| 2557 | * there was more coming, but that turned out to be a lie. |
| 2558 | */ |
| 2559 | if (errors) |
| 2560 | blk_mq_commit_rqs(hctx, &queued, from_schedule); |
| 2561 | } |
| 2562 | |
| 2563 | static void __blk_mq_flush_plug_list(struct request_queue *q, |
| 2564 | struct blk_plug *plug) |
| 2565 | { |
| 2566 | if (blk_queue_quiesced(q)) |
| 2567 | return; |
| 2568 | q->mq_ops->queue_rqs(&plug->mq_list); |
| 2569 | } |
| 2570 | |
| 2571 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 2572 | { |
| 2573 | struct blk_mq_hw_ctx *this_hctx; |
| 2574 | struct blk_mq_ctx *this_ctx; |
| 2575 | struct request *rq; |
| 2576 | unsigned int depth; |
| 2577 | LIST_HEAD(list); |
| 2578 | |
| 2579 | if (rq_list_empty(plug->mq_list)) |
| 2580 | return; |
| 2581 | plug->rq_count = 0; |
| 2582 | |
| 2583 | if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) { |
| 2584 | struct request_queue *q; |
| 2585 | |
| 2586 | rq = rq_list_peek(&plug->mq_list); |
| 2587 | q = rq->q; |
| 2588 | |
| 2589 | /* |
| 2590 | * Peek first request and see if we have a ->queue_rqs() hook. |
| 2591 | * If we do, we can dispatch the whole plug list in one go. We |
| 2592 | * already know at this point that all requests belong to the |
| 2593 | * same queue, caller must ensure that's the case. |
| 2594 | * |
| 2595 | * Since we pass off the full list to the driver at this point, |
| 2596 | * we do not increment the active request count for the queue. |
| 2597 | * Bypass shared tags for now because of that. |
| 2598 | */ |
| 2599 | if (q->mq_ops->queue_rqs && |
| 2600 | !(rq->mq_hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { |
| 2601 | blk_mq_run_dispatch_ops(q, |
| 2602 | __blk_mq_flush_plug_list(q, plug)); |
| 2603 | if (rq_list_empty(plug->mq_list)) |
| 2604 | return; |
| 2605 | } |
| 2606 | |
| 2607 | blk_mq_run_dispatch_ops(q, |
| 2608 | blk_mq_plug_issue_direct(plug, false)); |
| 2609 | if (rq_list_empty(plug->mq_list)) |
| 2610 | return; |
| 2611 | } |
| 2612 | |
| 2613 | this_hctx = NULL; |
| 2614 | this_ctx = NULL; |
| 2615 | depth = 0; |
| 2616 | do { |
| 2617 | rq = rq_list_pop(&plug->mq_list); |
| 2618 | |
| 2619 | if (!this_hctx) { |
| 2620 | this_hctx = rq->mq_hctx; |
| 2621 | this_ctx = rq->mq_ctx; |
| 2622 | } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx) { |
| 2623 | trace_block_unplug(this_hctx->queue, depth, |
| 2624 | !from_schedule); |
| 2625 | blk_mq_sched_insert_requests(this_hctx, this_ctx, |
| 2626 | &list, from_schedule); |
| 2627 | depth = 0; |
| 2628 | this_hctx = rq->mq_hctx; |
| 2629 | this_ctx = rq->mq_ctx; |
| 2630 | |
| 2631 | } |
| 2632 | |
| 2633 | list_add(&rq->queuelist, &list); |
| 2634 | depth++; |
| 2635 | } while (!rq_list_empty(plug->mq_list)); |
| 2636 | |
| 2637 | if (!list_empty(&list)) { |
| 2638 | trace_block_unplug(this_hctx->queue, depth, !from_schedule); |
| 2639 | blk_mq_sched_insert_requests(this_hctx, this_ctx, &list, |
| 2640 | from_schedule); |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, |
| 2645 | struct list_head *list) |
| 2646 | { |
| 2647 | int queued = 0; |
| 2648 | int errors = 0; |
| 2649 | |
| 2650 | while (!list_empty(list)) { |
| 2651 | blk_status_t ret; |
| 2652 | struct request *rq = list_first_entry(list, struct request, |
| 2653 | queuelist); |
| 2654 | |
| 2655 | list_del_init(&rq->queuelist); |
| 2656 | ret = blk_mq_request_issue_directly(rq, list_empty(list)); |
| 2657 | if (ret != BLK_STS_OK) { |
| 2658 | if (ret == BLK_STS_RESOURCE || |
| 2659 | ret == BLK_STS_DEV_RESOURCE) { |
| 2660 | blk_mq_request_bypass_insert(rq, false, |
| 2661 | list_empty(list)); |
| 2662 | break; |
| 2663 | } |
| 2664 | blk_mq_end_request(rq, ret); |
| 2665 | errors++; |
| 2666 | } else |
| 2667 | queued++; |
| 2668 | } |
| 2669 | |
| 2670 | /* |
| 2671 | * If we didn't flush the entire list, we could have told |
| 2672 | * the driver there was more coming, but that turned out to |
| 2673 | * be a lie. |
| 2674 | */ |
| 2675 | if ((!list_empty(list) || errors) && |
| 2676 | hctx->queue->mq_ops->commit_rqs && queued) |
| 2677 | hctx->queue->mq_ops->commit_rqs(hctx); |
| 2678 | } |
| 2679 | |
| 2680 | /* |
| 2681 | * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple |
| 2682 | * queues. This is important for md arrays to benefit from merging |
| 2683 | * requests. |
| 2684 | */ |
| 2685 | static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug) |
| 2686 | { |
| 2687 | if (plug->multiple_queues) |
| 2688 | return BLK_MAX_REQUEST_COUNT * 2; |
| 2689 | return BLK_MAX_REQUEST_COUNT; |
| 2690 | } |
| 2691 | |
| 2692 | static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq) |
| 2693 | { |
| 2694 | struct request *last = rq_list_peek(&plug->mq_list); |
| 2695 | |
| 2696 | if (!plug->rq_count) { |
| 2697 | trace_block_plug(rq->q); |
| 2698 | } else if (plug->rq_count >= blk_plug_max_rq_count(plug) || |
| 2699 | (!blk_queue_nomerges(rq->q) && |
| 2700 | blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { |
| 2701 | blk_mq_flush_plug_list(plug, false); |
| 2702 | trace_block_plug(rq->q); |
| 2703 | } |
| 2704 | |
| 2705 | if (!plug->multiple_queues && last && last->q != rq->q) |
| 2706 | plug->multiple_queues = true; |
| 2707 | if (!plug->has_elevator && (rq->rq_flags & RQF_ELV)) |
| 2708 | plug->has_elevator = true; |
| 2709 | rq->rq_next = NULL; |
| 2710 | rq_list_add(&plug->mq_list, rq); |
| 2711 | plug->rq_count++; |
| 2712 | } |
| 2713 | |
| 2714 | static bool blk_mq_attempt_bio_merge(struct request_queue *q, |
| 2715 | struct bio *bio, unsigned int nr_segs) |
| 2716 | { |
| 2717 | if (!blk_queue_nomerges(q) && bio_mergeable(bio)) { |
| 2718 | if (blk_attempt_plug_merge(q, bio, nr_segs)) |
| 2719 | return true; |
| 2720 | if (blk_mq_sched_bio_merge(q, bio, nr_segs)) |
| 2721 | return true; |
| 2722 | } |
| 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | static struct request *blk_mq_get_new_requests(struct request_queue *q, |
| 2727 | struct blk_plug *plug, |
| 2728 | struct bio *bio) |
| 2729 | { |
| 2730 | struct blk_mq_alloc_data data = { |
| 2731 | .q = q, |
| 2732 | .nr_tags = 1, |
| 2733 | .cmd_flags = bio->bi_opf, |
| 2734 | }; |
| 2735 | struct request *rq; |
| 2736 | |
| 2737 | if (unlikely(bio_queue_enter(bio))) |
| 2738 | return NULL; |
| 2739 | |
| 2740 | if (plug) { |
| 2741 | data.nr_tags = plug->nr_ios; |
| 2742 | plug->nr_ios = 1; |
| 2743 | data.cached_rq = &plug->cached_rq; |
| 2744 | } |
| 2745 | |
| 2746 | rq = __blk_mq_alloc_requests(&data); |
| 2747 | if (rq) |
| 2748 | return rq; |
| 2749 | rq_qos_cleanup(q, bio); |
| 2750 | if (bio->bi_opf & REQ_NOWAIT) |
| 2751 | bio_wouldblock_error(bio); |
| 2752 | blk_queue_exit(q); |
| 2753 | return NULL; |
| 2754 | } |
| 2755 | |
| 2756 | static inline struct request *blk_mq_get_cached_request(struct request_queue *q, |
| 2757 | struct blk_plug *plug, struct bio *bio) |
| 2758 | { |
| 2759 | struct request *rq; |
| 2760 | |
| 2761 | if (!plug) |
| 2762 | return NULL; |
| 2763 | rq = rq_list_peek(&plug->cached_rq); |
| 2764 | if (!rq || rq->q != q) |
| 2765 | return NULL; |
| 2766 | |
| 2767 | if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type) |
| 2768 | return NULL; |
| 2769 | if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf)) |
| 2770 | return NULL; |
| 2771 | |
| 2772 | rq->cmd_flags = bio->bi_opf; |
| 2773 | plug->cached_rq = rq_list_next(rq); |
| 2774 | INIT_LIST_HEAD(&rq->queuelist); |
| 2775 | return rq; |
| 2776 | } |
| 2777 | |
| 2778 | /** |
| 2779 | * blk_mq_submit_bio - Create and send a request to block device. |
| 2780 | * @bio: Bio pointer. |
| 2781 | * |
| 2782 | * Builds up a request structure from @q and @bio and send to the device. The |
| 2783 | * request may not be queued directly to hardware if: |
| 2784 | * * This request can be merged with another one |
| 2785 | * * We want to place request at plug queue for possible future merging |
| 2786 | * * There is an IO scheduler active at this queue |
| 2787 | * |
| 2788 | * It will not queue the request if there is an error with the bio, or at the |
| 2789 | * request creation. |
| 2790 | */ |
| 2791 | void blk_mq_submit_bio(struct bio *bio) |
| 2792 | { |
| 2793 | struct request_queue *q = bdev_get_queue(bio->bi_bdev); |
| 2794 | struct blk_plug *plug = blk_mq_plug(q, bio); |
| 2795 | const int is_sync = op_is_sync(bio->bi_opf); |
| 2796 | struct request *rq; |
| 2797 | unsigned int nr_segs = 1; |
| 2798 | blk_status_t ret; |
| 2799 | |
| 2800 | blk_queue_bounce(q, &bio); |
| 2801 | if (blk_may_split(q, bio)) |
| 2802 | __blk_queue_split(q, &bio, &nr_segs); |
| 2803 | |
| 2804 | if (!bio_integrity_prep(bio)) |
| 2805 | return; |
| 2806 | |
| 2807 | if (blk_mq_attempt_bio_merge(q, bio, nr_segs)) |
| 2808 | return; |
| 2809 | |
| 2810 | rq_qos_throttle(q, bio); |
| 2811 | |
| 2812 | rq = blk_mq_get_cached_request(q, plug, bio); |
| 2813 | if (!rq) { |
| 2814 | rq = blk_mq_get_new_requests(q, plug, bio); |
| 2815 | if (unlikely(!rq)) |
| 2816 | return; |
| 2817 | } |
| 2818 | |
| 2819 | trace_block_getrq(bio); |
| 2820 | |
| 2821 | rq_qos_track(q, rq, bio); |
| 2822 | |
| 2823 | blk_mq_bio_to_request(rq, bio, nr_segs); |
| 2824 | |
| 2825 | ret = blk_crypto_init_request(rq); |
| 2826 | if (ret != BLK_STS_OK) { |
| 2827 | bio->bi_status = ret; |
| 2828 | bio_endio(bio); |
| 2829 | blk_mq_free_request(rq); |
| 2830 | return; |
| 2831 | } |
| 2832 | |
| 2833 | if (op_is_flush(bio->bi_opf)) { |
| 2834 | blk_insert_flush(rq); |
| 2835 | return; |
| 2836 | } |
| 2837 | |
| 2838 | if (plug) |
| 2839 | blk_add_rq_to_plug(plug, rq); |
| 2840 | else if ((rq->rq_flags & RQF_ELV) || |
| 2841 | (rq->mq_hctx->dispatch_busy && |
| 2842 | (q->nr_hw_queues == 1 || !is_sync))) |
| 2843 | blk_mq_sched_insert_request(rq, false, true, true); |
| 2844 | else |
| 2845 | blk_mq_run_dispatch_ops(rq->q, |
| 2846 | blk_mq_try_issue_directly(rq->mq_hctx, rq)); |
| 2847 | } |
| 2848 | |
| 2849 | #ifdef CONFIG_BLK_MQ_STACKING |
| 2850 | /** |
| 2851 | * blk_insert_cloned_request - Helper for stacking drivers to submit a request |
| 2852 | * @rq: the request being queued |
| 2853 | */ |
| 2854 | blk_status_t blk_insert_cloned_request(struct request *rq) |
| 2855 | { |
| 2856 | struct request_queue *q = rq->q; |
| 2857 | unsigned int max_sectors = blk_queue_get_max_sectors(q, req_op(rq)); |
| 2858 | blk_status_t ret; |
| 2859 | |
| 2860 | if (blk_rq_sectors(rq) > max_sectors) { |
| 2861 | /* |
| 2862 | * SCSI device does not have a good way to return if |
| 2863 | * Write Same/Zero is actually supported. If a device rejects |
| 2864 | * a non-read/write command (discard, write same,etc.) the |
| 2865 | * low-level device driver will set the relevant queue limit to |
| 2866 | * 0 to prevent blk-lib from issuing more of the offending |
| 2867 | * operations. Commands queued prior to the queue limit being |
| 2868 | * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O |
| 2869 | * errors being propagated to upper layers. |
| 2870 | */ |
| 2871 | if (max_sectors == 0) |
| 2872 | return BLK_STS_NOTSUPP; |
| 2873 | |
| 2874 | printk(KERN_ERR "%s: over max size limit. (%u > %u)\n", |
| 2875 | __func__, blk_rq_sectors(rq), max_sectors); |
| 2876 | return BLK_STS_IOERR; |
| 2877 | } |
| 2878 | |
| 2879 | /* |
| 2880 | * The queue settings related to segment counting may differ from the |
| 2881 | * original queue. |
| 2882 | */ |
| 2883 | rq->nr_phys_segments = blk_recalc_rq_segments(rq); |
| 2884 | if (rq->nr_phys_segments > queue_max_segments(q)) { |
| 2885 | printk(KERN_ERR "%s: over max segments limit. (%hu > %hu)\n", |
| 2886 | __func__, rq->nr_phys_segments, queue_max_segments(q)); |
| 2887 | return BLK_STS_IOERR; |
| 2888 | } |
| 2889 | |
| 2890 | if (q->disk && should_fail_request(q->disk->part0, blk_rq_bytes(rq))) |
| 2891 | return BLK_STS_IOERR; |
| 2892 | |
| 2893 | if (blk_crypto_insert_cloned_request(rq)) |
| 2894 | return BLK_STS_IOERR; |
| 2895 | |
| 2896 | blk_account_io_start(rq); |
| 2897 | |
| 2898 | /* |
| 2899 | * Since we have a scheduler attached on the top device, |
| 2900 | * bypass a potential scheduler on the bottom device for |
| 2901 | * insert. |
| 2902 | */ |
| 2903 | blk_mq_run_dispatch_ops(q, |
| 2904 | ret = blk_mq_request_issue_directly(rq, true)); |
| 2905 | if (ret) |
| 2906 | blk_account_io_done(rq, ktime_get_ns()); |
| 2907 | return ret; |
| 2908 | } |
| 2909 | EXPORT_SYMBOL_GPL(blk_insert_cloned_request); |
| 2910 | |
| 2911 | /** |
| 2912 | * blk_rq_unprep_clone - Helper function to free all bios in a cloned request |
| 2913 | * @rq: the clone request to be cleaned up |
| 2914 | * |
| 2915 | * Description: |
| 2916 | * Free all bios in @rq for a cloned request. |
| 2917 | */ |
| 2918 | void blk_rq_unprep_clone(struct request *rq) |
| 2919 | { |
| 2920 | struct bio *bio; |
| 2921 | |
| 2922 | while ((bio = rq->bio) != NULL) { |
| 2923 | rq->bio = bio->bi_next; |
| 2924 | |
| 2925 | bio_put(bio); |
| 2926 | } |
| 2927 | } |
| 2928 | EXPORT_SYMBOL_GPL(blk_rq_unprep_clone); |
| 2929 | |
| 2930 | /** |
| 2931 | * blk_rq_prep_clone - Helper function to setup clone request |
| 2932 | * @rq: the request to be setup |
| 2933 | * @rq_src: original request to be cloned |
| 2934 | * @bs: bio_set that bios for clone are allocated from |
| 2935 | * @gfp_mask: memory allocation mask for bio |
| 2936 | * @bio_ctr: setup function to be called for each clone bio. |
| 2937 | * Returns %0 for success, non %0 for failure. |
| 2938 | * @data: private data to be passed to @bio_ctr |
| 2939 | * |
| 2940 | * Description: |
| 2941 | * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq. |
| 2942 | * Also, pages which the original bios are pointing to are not copied |
| 2943 | * and the cloned bios just point same pages. |
| 2944 | * So cloned bios must be completed before original bios, which means |
| 2945 | * the caller must complete @rq before @rq_src. |
| 2946 | */ |
| 2947 | int blk_rq_prep_clone(struct request *rq, struct request *rq_src, |
| 2948 | struct bio_set *bs, gfp_t gfp_mask, |
| 2949 | int (*bio_ctr)(struct bio *, struct bio *, void *), |
| 2950 | void *data) |
| 2951 | { |
| 2952 | struct bio *bio, *bio_src; |
| 2953 | |
| 2954 | if (!bs) |
| 2955 | bs = &fs_bio_set; |
| 2956 | |
| 2957 | __rq_for_each_bio(bio_src, rq_src) { |
| 2958 | bio = bio_alloc_clone(rq->q->disk->part0, bio_src, gfp_mask, |
| 2959 | bs); |
| 2960 | if (!bio) |
| 2961 | goto free_and_out; |
| 2962 | |
| 2963 | if (bio_ctr && bio_ctr(bio, bio_src, data)) |
| 2964 | goto free_and_out; |
| 2965 | |
| 2966 | if (rq->bio) { |
| 2967 | rq->biotail->bi_next = bio; |
| 2968 | rq->biotail = bio; |
| 2969 | } else { |
| 2970 | rq->bio = rq->biotail = bio; |
| 2971 | } |
| 2972 | bio = NULL; |
| 2973 | } |
| 2974 | |
| 2975 | /* Copy attributes of the original request to the clone request. */ |
| 2976 | rq->__sector = blk_rq_pos(rq_src); |
| 2977 | rq->__data_len = blk_rq_bytes(rq_src); |
| 2978 | if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) { |
| 2979 | rq->rq_flags |= RQF_SPECIAL_PAYLOAD; |
| 2980 | rq->special_vec = rq_src->special_vec; |
| 2981 | } |
| 2982 | rq->nr_phys_segments = rq_src->nr_phys_segments; |
| 2983 | rq->ioprio = rq_src->ioprio; |
| 2984 | |
| 2985 | if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0) |
| 2986 | goto free_and_out; |
| 2987 | |
| 2988 | return 0; |
| 2989 | |
| 2990 | free_and_out: |
| 2991 | if (bio) |
| 2992 | bio_put(bio); |
| 2993 | blk_rq_unprep_clone(rq); |
| 2994 | |
| 2995 | return -ENOMEM; |
| 2996 | } |
| 2997 | EXPORT_SYMBOL_GPL(blk_rq_prep_clone); |
| 2998 | #endif /* CONFIG_BLK_MQ_STACKING */ |
| 2999 | |
| 3000 | /* |
| 3001 | * Steal bios from a request and add them to a bio list. |
| 3002 | * The request must not have been partially completed before. |
| 3003 | */ |
| 3004 | void blk_steal_bios(struct bio_list *list, struct request *rq) |
| 3005 | { |
| 3006 | if (rq->bio) { |
| 3007 | if (list->tail) |
| 3008 | list->tail->bi_next = rq->bio; |
| 3009 | else |
| 3010 | list->head = rq->bio; |
| 3011 | list->tail = rq->biotail; |
| 3012 | |
| 3013 | rq->bio = NULL; |
| 3014 | rq->biotail = NULL; |
| 3015 | } |
| 3016 | |
| 3017 | rq->__data_len = 0; |
| 3018 | } |
| 3019 | EXPORT_SYMBOL_GPL(blk_steal_bios); |
| 3020 | |
| 3021 | static size_t order_to_size(unsigned int order) |
| 3022 | { |
| 3023 | return (size_t)PAGE_SIZE << order; |
| 3024 | } |
| 3025 | |
| 3026 | /* called before freeing request pool in @tags */ |
| 3027 | static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags, |
| 3028 | struct blk_mq_tags *tags) |
| 3029 | { |
| 3030 | struct page *page; |
| 3031 | unsigned long flags; |
| 3032 | |
| 3033 | /* There is no need to clear a driver tags own mapping */ |
| 3034 | if (drv_tags == tags) |
| 3035 | return; |
| 3036 | |
| 3037 | list_for_each_entry(page, &tags->page_list, lru) { |
| 3038 | unsigned long start = (unsigned long)page_address(page); |
| 3039 | unsigned long end = start + order_to_size(page->private); |
| 3040 | int i; |
| 3041 | |
| 3042 | for (i = 0; i < drv_tags->nr_tags; i++) { |
| 3043 | struct request *rq = drv_tags->rqs[i]; |
| 3044 | unsigned long rq_addr = (unsigned long)rq; |
| 3045 | |
| 3046 | if (rq_addr >= start && rq_addr < end) { |
| 3047 | WARN_ON_ONCE(req_ref_read(rq) != 0); |
| 3048 | cmpxchg(&drv_tags->rqs[i], rq, NULL); |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | /* |
| 3054 | * Wait until all pending iteration is done. |
| 3055 | * |
| 3056 | * Request reference is cleared and it is guaranteed to be observed |
| 3057 | * after the ->lock is released. |
| 3058 | */ |
| 3059 | spin_lock_irqsave(&drv_tags->lock, flags); |
| 3060 | spin_unlock_irqrestore(&drv_tags->lock, flags); |
| 3061 | } |
| 3062 | |
| 3063 | void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags, |
| 3064 | unsigned int hctx_idx) |
| 3065 | { |
| 3066 | struct blk_mq_tags *drv_tags; |
| 3067 | struct page *page; |
| 3068 | |
| 3069 | if (blk_mq_is_shared_tags(set->flags)) |
| 3070 | drv_tags = set->shared_tags; |
| 3071 | else |
| 3072 | drv_tags = set->tags[hctx_idx]; |
| 3073 | |
| 3074 | if (tags->static_rqs && set->ops->exit_request) { |
| 3075 | int i; |
| 3076 | |
| 3077 | for (i = 0; i < tags->nr_tags; i++) { |
| 3078 | struct request *rq = tags->static_rqs[i]; |
| 3079 | |
| 3080 | if (!rq) |
| 3081 | continue; |
| 3082 | set->ops->exit_request(set, rq, hctx_idx); |
| 3083 | tags->static_rqs[i] = NULL; |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | blk_mq_clear_rq_mapping(drv_tags, tags); |
| 3088 | |
| 3089 | while (!list_empty(&tags->page_list)) { |
| 3090 | page = list_first_entry(&tags->page_list, struct page, lru); |
| 3091 | list_del_init(&page->lru); |
| 3092 | /* |
| 3093 | * Remove kmemleak object previously allocated in |
| 3094 | * blk_mq_alloc_rqs(). |
| 3095 | */ |
| 3096 | kmemleak_free(page_address(page)); |
| 3097 | __free_pages(page, page->private); |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | void blk_mq_free_rq_map(struct blk_mq_tags *tags) |
| 3102 | { |
| 3103 | kfree(tags->rqs); |
| 3104 | tags->rqs = NULL; |
| 3105 | kfree(tags->static_rqs); |
| 3106 | tags->static_rqs = NULL; |
| 3107 | |
| 3108 | blk_mq_free_tags(tags); |
| 3109 | } |
| 3110 | |
| 3111 | static enum hctx_type hctx_idx_to_type(struct blk_mq_tag_set *set, |
| 3112 | unsigned int hctx_idx) |
| 3113 | { |
| 3114 | int i; |
| 3115 | |
| 3116 | for (i = 0; i < set->nr_maps; i++) { |
| 3117 | unsigned int start = set->map[i].queue_offset; |
| 3118 | unsigned int end = start + set->map[i].nr_queues; |
| 3119 | |
| 3120 | if (hctx_idx >= start && hctx_idx < end) |
| 3121 | break; |
| 3122 | } |
| 3123 | |
| 3124 | if (i >= set->nr_maps) |
| 3125 | i = HCTX_TYPE_DEFAULT; |
| 3126 | |
| 3127 | return i; |
| 3128 | } |
| 3129 | |
| 3130 | static int blk_mq_get_hctx_node(struct blk_mq_tag_set *set, |
| 3131 | unsigned int hctx_idx) |
| 3132 | { |
| 3133 | enum hctx_type type = hctx_idx_to_type(set, hctx_idx); |
| 3134 | |
| 3135 | return blk_mq_hw_queue_to_node(&set->map[type], hctx_idx); |
| 3136 | } |
| 3137 | |
| 3138 | static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, |
| 3139 | unsigned int hctx_idx, |
| 3140 | unsigned int nr_tags, |
| 3141 | unsigned int reserved_tags) |
| 3142 | { |
| 3143 | int node = blk_mq_get_hctx_node(set, hctx_idx); |
| 3144 | struct blk_mq_tags *tags; |
| 3145 | |
| 3146 | if (node == NUMA_NO_NODE) |
| 3147 | node = set->numa_node; |
| 3148 | |
| 3149 | tags = blk_mq_init_tags(nr_tags, reserved_tags, node, |
| 3150 | BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags)); |
| 3151 | if (!tags) |
| 3152 | return NULL; |
| 3153 | |
| 3154 | tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *), |
| 3155 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, |
| 3156 | node); |
| 3157 | if (!tags->rqs) { |
| 3158 | blk_mq_free_tags(tags); |
| 3159 | return NULL; |
| 3160 | } |
| 3161 | |
| 3162 | tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *), |
| 3163 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, |
| 3164 | node); |
| 3165 | if (!tags->static_rqs) { |
| 3166 | kfree(tags->rqs); |
| 3167 | blk_mq_free_tags(tags); |
| 3168 | return NULL; |
| 3169 | } |
| 3170 | |
| 3171 | return tags; |
| 3172 | } |
| 3173 | |
| 3174 | static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 3175 | unsigned int hctx_idx, int node) |
| 3176 | { |
| 3177 | int ret; |
| 3178 | |
| 3179 | if (set->ops->init_request) { |
| 3180 | ret = set->ops->init_request(set, rq, hctx_idx, node); |
| 3181 | if (ret) |
| 3182 | return ret; |
| 3183 | } |
| 3184 | |
| 3185 | WRITE_ONCE(rq->state, MQ_RQ_IDLE); |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
| 3189 | static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, |
| 3190 | struct blk_mq_tags *tags, |
| 3191 | unsigned int hctx_idx, unsigned int depth) |
| 3192 | { |
| 3193 | unsigned int i, j, entries_per_page, max_order = 4; |
| 3194 | int node = blk_mq_get_hctx_node(set, hctx_idx); |
| 3195 | size_t rq_size, left; |
| 3196 | |
| 3197 | if (node == NUMA_NO_NODE) |
| 3198 | node = set->numa_node; |
| 3199 | |
| 3200 | INIT_LIST_HEAD(&tags->page_list); |
| 3201 | |
| 3202 | /* |
| 3203 | * rq_size is the size of the request plus driver payload, rounded |
| 3204 | * to the cacheline size |
| 3205 | */ |
| 3206 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
| 3207 | cache_line_size()); |
| 3208 | left = rq_size * depth; |
| 3209 | |
| 3210 | for (i = 0; i < depth; ) { |
| 3211 | int this_order = max_order; |
| 3212 | struct page *page; |
| 3213 | int to_do; |
| 3214 | void *p; |
| 3215 | |
| 3216 | while (this_order && left < order_to_size(this_order - 1)) |
| 3217 | this_order--; |
| 3218 | |
| 3219 | do { |
| 3220 | page = alloc_pages_node(node, |
| 3221 | GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, |
| 3222 | this_order); |
| 3223 | if (page) |
| 3224 | break; |
| 3225 | if (!this_order--) |
| 3226 | break; |
| 3227 | if (order_to_size(this_order) < rq_size) |
| 3228 | break; |
| 3229 | } while (1); |
| 3230 | |
| 3231 | if (!page) |
| 3232 | goto fail; |
| 3233 | |
| 3234 | page->private = this_order; |
| 3235 | list_add_tail(&page->lru, &tags->page_list); |
| 3236 | |
| 3237 | p = page_address(page); |
| 3238 | /* |
| 3239 | * Allow kmemleak to scan these pages as they contain pointers |
| 3240 | * to additional allocations like via ops->init_request(). |
| 3241 | */ |
| 3242 | kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO); |
| 3243 | entries_per_page = order_to_size(this_order) / rq_size; |
| 3244 | to_do = min(entries_per_page, depth - i); |
| 3245 | left -= to_do * rq_size; |
| 3246 | for (j = 0; j < to_do; j++) { |
| 3247 | struct request *rq = p; |
| 3248 | |
| 3249 | tags->static_rqs[i] = rq; |
| 3250 | if (blk_mq_init_request(set, rq, hctx_idx, node)) { |
| 3251 | tags->static_rqs[i] = NULL; |
| 3252 | goto fail; |
| 3253 | } |
| 3254 | |
| 3255 | p += rq_size; |
| 3256 | i++; |
| 3257 | } |
| 3258 | } |
| 3259 | return 0; |
| 3260 | |
| 3261 | fail: |
| 3262 | blk_mq_free_rqs(set, tags, hctx_idx); |
| 3263 | return -ENOMEM; |
| 3264 | } |
| 3265 | |
| 3266 | struct rq_iter_data { |
| 3267 | struct blk_mq_hw_ctx *hctx; |
| 3268 | bool has_rq; |
| 3269 | }; |
| 3270 | |
| 3271 | static bool blk_mq_has_request(struct request *rq, void *data, bool reserved) |
| 3272 | { |
| 3273 | struct rq_iter_data *iter_data = data; |
| 3274 | |
| 3275 | if (rq->mq_hctx != iter_data->hctx) |
| 3276 | return true; |
| 3277 | iter_data->has_rq = true; |
| 3278 | return false; |
| 3279 | } |
| 3280 | |
| 3281 | static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) |
| 3282 | { |
| 3283 | struct blk_mq_tags *tags = hctx->sched_tags ? |
| 3284 | hctx->sched_tags : hctx->tags; |
| 3285 | struct rq_iter_data data = { |
| 3286 | .hctx = hctx, |
| 3287 | }; |
| 3288 | |
| 3289 | blk_mq_all_tag_iter(tags, blk_mq_has_request, &data); |
| 3290 | return data.has_rq; |
| 3291 | } |
| 3292 | |
| 3293 | static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu, |
| 3294 | struct blk_mq_hw_ctx *hctx) |
| 3295 | { |
| 3296 | if (cpumask_first_and(hctx->cpumask, cpu_online_mask) != cpu) |
| 3297 | return false; |
| 3298 | if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids) |
| 3299 | return false; |
| 3300 | return true; |
| 3301 | } |
| 3302 | |
| 3303 | static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) |
| 3304 | { |
| 3305 | struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, |
| 3306 | struct blk_mq_hw_ctx, cpuhp_online); |
| 3307 | |
| 3308 | if (!cpumask_test_cpu(cpu, hctx->cpumask) || |
| 3309 | !blk_mq_last_cpu_in_hctx(cpu, hctx)) |
| 3310 | return 0; |
| 3311 | |
| 3312 | /* |
| 3313 | * Prevent new request from being allocated on the current hctx. |
| 3314 | * |
| 3315 | * The smp_mb__after_atomic() Pairs with the implied barrier in |
| 3316 | * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is |
| 3317 | * seen once we return from the tag allocator. |
| 3318 | */ |
| 3319 | set_bit(BLK_MQ_S_INACTIVE, &hctx->state); |
| 3320 | smp_mb__after_atomic(); |
| 3321 | |
| 3322 | /* |
| 3323 | * Try to grab a reference to the queue and wait for any outstanding |
| 3324 | * requests. If we could not grab a reference the queue has been |
| 3325 | * frozen and there are no requests. |
| 3326 | */ |
| 3327 | if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) { |
| 3328 | while (blk_mq_hctx_has_requests(hctx)) |
| 3329 | msleep(5); |
| 3330 | percpu_ref_put(&hctx->queue->q_usage_counter); |
| 3331 | } |
| 3332 | |
| 3333 | return 0; |
| 3334 | } |
| 3335 | |
| 3336 | static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node) |
| 3337 | { |
| 3338 | struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node, |
| 3339 | struct blk_mq_hw_ctx, cpuhp_online); |
| 3340 | |
| 3341 | if (cpumask_test_cpu(cpu, hctx->cpumask)) |
| 3342 | clear_bit(BLK_MQ_S_INACTIVE, &hctx->state); |
| 3343 | return 0; |
| 3344 | } |
| 3345 | |
| 3346 | /* |
| 3347 | * 'cpu' is going away. splice any existing rq_list entries from this |
| 3348 | * software queue to the hw queue dispatch list, and ensure that it |
| 3349 | * gets run. |
| 3350 | */ |
| 3351 | static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) |
| 3352 | { |
| 3353 | struct blk_mq_hw_ctx *hctx; |
| 3354 | struct blk_mq_ctx *ctx; |
| 3355 | LIST_HEAD(tmp); |
| 3356 | enum hctx_type type; |
| 3357 | |
| 3358 | hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); |
| 3359 | if (!cpumask_test_cpu(cpu, hctx->cpumask)) |
| 3360 | return 0; |
| 3361 | |
| 3362 | ctx = __blk_mq_get_ctx(hctx->queue, cpu); |
| 3363 | type = hctx->type; |
| 3364 | |
| 3365 | spin_lock(&ctx->lock); |
| 3366 | if (!list_empty(&ctx->rq_lists[type])) { |
| 3367 | list_splice_init(&ctx->rq_lists[type], &tmp); |
| 3368 | blk_mq_hctx_clear_pending(hctx, ctx); |
| 3369 | } |
| 3370 | spin_unlock(&ctx->lock); |
| 3371 | |
| 3372 | if (list_empty(&tmp)) |
| 3373 | return 0; |
| 3374 | |
| 3375 | spin_lock(&hctx->lock); |
| 3376 | list_splice_tail_init(&tmp, &hctx->dispatch); |
| 3377 | spin_unlock(&hctx->lock); |
| 3378 | |
| 3379 | blk_mq_run_hw_queue(hctx, true); |
| 3380 | return 0; |
| 3381 | } |
| 3382 | |
| 3383 | static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) |
| 3384 | { |
| 3385 | if (!(hctx->flags & BLK_MQ_F_STACKING)) |
| 3386 | cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, |
| 3387 | &hctx->cpuhp_online); |
| 3388 | cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, |
| 3389 | &hctx->cpuhp_dead); |
| 3390 | } |
| 3391 | |
| 3392 | /* |
| 3393 | * Before freeing hw queue, clearing the flush request reference in |
| 3394 | * tags->rqs[] for avoiding potential UAF. |
| 3395 | */ |
| 3396 | static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags, |
| 3397 | unsigned int queue_depth, struct request *flush_rq) |
| 3398 | { |
| 3399 | int i; |
| 3400 | unsigned long flags; |
| 3401 | |
| 3402 | /* The hw queue may not be mapped yet */ |
| 3403 | if (!tags) |
| 3404 | return; |
| 3405 | |
| 3406 | WARN_ON_ONCE(req_ref_read(flush_rq) != 0); |
| 3407 | |
| 3408 | for (i = 0; i < queue_depth; i++) |
| 3409 | cmpxchg(&tags->rqs[i], flush_rq, NULL); |
| 3410 | |
| 3411 | /* |
| 3412 | * Wait until all pending iteration is done. |
| 3413 | * |
| 3414 | * Request reference is cleared and it is guaranteed to be observed |
| 3415 | * after the ->lock is released. |
| 3416 | */ |
| 3417 | spin_lock_irqsave(&tags->lock, flags); |
| 3418 | spin_unlock_irqrestore(&tags->lock, flags); |
| 3419 | } |
| 3420 | |
| 3421 | /* hctx->ctxs will be freed in queue's release handler */ |
| 3422 | static void blk_mq_exit_hctx(struct request_queue *q, |
| 3423 | struct blk_mq_tag_set *set, |
| 3424 | struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) |
| 3425 | { |
| 3426 | struct request *flush_rq = hctx->fq->flush_rq; |
| 3427 | |
| 3428 | if (blk_mq_hw_queue_mapped(hctx)) |
| 3429 | blk_mq_tag_idle(hctx); |
| 3430 | |
| 3431 | blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx], |
| 3432 | set->queue_depth, flush_rq); |
| 3433 | if (set->ops->exit_request) |
| 3434 | set->ops->exit_request(set, flush_rq, hctx_idx); |
| 3435 | |
| 3436 | if (set->ops->exit_hctx) |
| 3437 | set->ops->exit_hctx(hctx, hctx_idx); |
| 3438 | |
| 3439 | blk_mq_remove_cpuhp(hctx); |
| 3440 | |
| 3441 | xa_erase(&q->hctx_table, hctx_idx); |
| 3442 | |
| 3443 | spin_lock(&q->unused_hctx_lock); |
| 3444 | list_add(&hctx->hctx_list, &q->unused_hctx_list); |
| 3445 | spin_unlock(&q->unused_hctx_lock); |
| 3446 | } |
| 3447 | |
| 3448 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
| 3449 | struct blk_mq_tag_set *set, int nr_queue) |
| 3450 | { |
| 3451 | struct blk_mq_hw_ctx *hctx; |
| 3452 | unsigned long i; |
| 3453 | |
| 3454 | queue_for_each_hw_ctx(q, hctx, i) { |
| 3455 | if (i == nr_queue) |
| 3456 | break; |
| 3457 | blk_mq_debugfs_unregister_hctx(hctx); |
| 3458 | blk_mq_exit_hctx(q, set, hctx, i); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | static int blk_mq_init_hctx(struct request_queue *q, |
| 3463 | struct blk_mq_tag_set *set, |
| 3464 | struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) |
| 3465 | { |
| 3466 | hctx->queue_num = hctx_idx; |
| 3467 | |
| 3468 | if (!(hctx->flags & BLK_MQ_F_STACKING)) |
| 3469 | cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE, |
| 3470 | &hctx->cpuhp_online); |
| 3471 | cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); |
| 3472 | |
| 3473 | hctx->tags = set->tags[hctx_idx]; |
| 3474 | |
| 3475 | if (set->ops->init_hctx && |
| 3476 | set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) |
| 3477 | goto unregister_cpu_notifier; |
| 3478 | |
| 3479 | if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, |
| 3480 | hctx->numa_node)) |
| 3481 | goto exit_hctx; |
| 3482 | |
| 3483 | if (xa_insert(&q->hctx_table, hctx_idx, hctx, GFP_KERNEL)) |
| 3484 | goto exit_flush_rq; |
| 3485 | |
| 3486 | return 0; |
| 3487 | |
| 3488 | exit_flush_rq: |
| 3489 | if (set->ops->exit_request) |
| 3490 | set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx); |
| 3491 | exit_hctx: |
| 3492 | if (set->ops->exit_hctx) |
| 3493 | set->ops->exit_hctx(hctx, hctx_idx); |
| 3494 | unregister_cpu_notifier: |
| 3495 | blk_mq_remove_cpuhp(hctx); |
| 3496 | return -1; |
| 3497 | } |
| 3498 | |
| 3499 | static struct blk_mq_hw_ctx * |
| 3500 | blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set, |
| 3501 | int node) |
| 3502 | { |
| 3503 | struct blk_mq_hw_ctx *hctx; |
| 3504 | gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY; |
| 3505 | |
| 3506 | hctx = kzalloc_node(sizeof(struct blk_mq_hw_ctx), gfp, node); |
| 3507 | if (!hctx) |
| 3508 | goto fail_alloc_hctx; |
| 3509 | |
| 3510 | if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node)) |
| 3511 | goto free_hctx; |
| 3512 | |
| 3513 | atomic_set(&hctx->nr_active, 0); |
| 3514 | if (node == NUMA_NO_NODE) |
| 3515 | node = set->numa_node; |
| 3516 | hctx->numa_node = node; |
| 3517 | |
| 3518 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
| 3519 | spin_lock_init(&hctx->lock); |
| 3520 | INIT_LIST_HEAD(&hctx->dispatch); |
| 3521 | hctx->queue = q; |
| 3522 | hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED; |
| 3523 | |
| 3524 | INIT_LIST_HEAD(&hctx->hctx_list); |
| 3525 | |
| 3526 | /* |
| 3527 | * Allocate space for all possible cpus to avoid allocation at |
| 3528 | * runtime |
| 3529 | */ |
| 3530 | hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *), |
| 3531 | gfp, node); |
| 3532 | if (!hctx->ctxs) |
| 3533 | goto free_cpumask; |
| 3534 | |
| 3535 | if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), |
| 3536 | gfp, node, false, false)) |
| 3537 | goto free_ctxs; |
| 3538 | hctx->nr_ctx = 0; |
| 3539 | |
| 3540 | spin_lock_init(&hctx->dispatch_wait_lock); |
| 3541 | init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake); |
| 3542 | INIT_LIST_HEAD(&hctx->dispatch_wait.entry); |
| 3543 | |
| 3544 | hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp); |
| 3545 | if (!hctx->fq) |
| 3546 | goto free_bitmap; |
| 3547 | |
| 3548 | blk_mq_hctx_kobj_init(hctx); |
| 3549 | |
| 3550 | return hctx; |
| 3551 | |
| 3552 | free_bitmap: |
| 3553 | sbitmap_free(&hctx->ctx_map); |
| 3554 | free_ctxs: |
| 3555 | kfree(hctx->ctxs); |
| 3556 | free_cpumask: |
| 3557 | free_cpumask_var(hctx->cpumask); |
| 3558 | free_hctx: |
| 3559 | kfree(hctx); |
| 3560 | fail_alloc_hctx: |
| 3561 | return NULL; |
| 3562 | } |
| 3563 | |
| 3564 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 3565 | unsigned int nr_hw_queues) |
| 3566 | { |
| 3567 | struct blk_mq_tag_set *set = q->tag_set; |
| 3568 | unsigned int i, j; |
| 3569 | |
| 3570 | for_each_possible_cpu(i) { |
| 3571 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 3572 | struct blk_mq_hw_ctx *hctx; |
| 3573 | int k; |
| 3574 | |
| 3575 | __ctx->cpu = i; |
| 3576 | spin_lock_init(&__ctx->lock); |
| 3577 | for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++) |
| 3578 | INIT_LIST_HEAD(&__ctx->rq_lists[k]); |
| 3579 | |
| 3580 | __ctx->queue = q; |
| 3581 | |
| 3582 | /* |
| 3583 | * Set local node, IFF we have more than one hw queue. If |
| 3584 | * not, we remain on the home node of the device |
| 3585 | */ |
| 3586 | for (j = 0; j < set->nr_maps; j++) { |
| 3587 | hctx = blk_mq_map_queue_type(q, j, i); |
| 3588 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 3589 | hctx->numa_node = cpu_to_node(i); |
| 3590 | } |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, |
| 3595 | unsigned int hctx_idx, |
| 3596 | unsigned int depth) |
| 3597 | { |
| 3598 | struct blk_mq_tags *tags; |
| 3599 | int ret; |
| 3600 | |
| 3601 | tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags); |
| 3602 | if (!tags) |
| 3603 | return NULL; |
| 3604 | |
| 3605 | ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth); |
| 3606 | if (ret) { |
| 3607 | blk_mq_free_rq_map(tags); |
| 3608 | return NULL; |
| 3609 | } |
| 3610 | |
| 3611 | return tags; |
| 3612 | } |
| 3613 | |
| 3614 | static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set, |
| 3615 | int hctx_idx) |
| 3616 | { |
| 3617 | if (blk_mq_is_shared_tags(set->flags)) { |
| 3618 | set->tags[hctx_idx] = set->shared_tags; |
| 3619 | |
| 3620 | return true; |
| 3621 | } |
| 3622 | |
| 3623 | set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx, |
| 3624 | set->queue_depth); |
| 3625 | |
| 3626 | return set->tags[hctx_idx]; |
| 3627 | } |
| 3628 | |
| 3629 | void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, |
| 3630 | struct blk_mq_tags *tags, |
| 3631 | unsigned int hctx_idx) |
| 3632 | { |
| 3633 | if (tags) { |
| 3634 | blk_mq_free_rqs(set, tags, hctx_idx); |
| 3635 | blk_mq_free_rq_map(tags); |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set, |
| 3640 | unsigned int hctx_idx) |
| 3641 | { |
| 3642 | if (!blk_mq_is_shared_tags(set->flags)) |
| 3643 | blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx); |
| 3644 | |
| 3645 | set->tags[hctx_idx] = NULL; |
| 3646 | } |
| 3647 | |
| 3648 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 3649 | { |
| 3650 | unsigned int j, hctx_idx; |
| 3651 | unsigned long i; |
| 3652 | struct blk_mq_hw_ctx *hctx; |
| 3653 | struct blk_mq_ctx *ctx; |
| 3654 | struct blk_mq_tag_set *set = q->tag_set; |
| 3655 | |
| 3656 | queue_for_each_hw_ctx(q, hctx, i) { |
| 3657 | cpumask_clear(hctx->cpumask); |
| 3658 | hctx->nr_ctx = 0; |
| 3659 | hctx->dispatch_from = NULL; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
| 3663 | * Map software to hardware queues. |
| 3664 | * |
| 3665 | * If the cpu isn't present, the cpu is mapped to first hctx. |
| 3666 | */ |
| 3667 | for_each_possible_cpu(i) { |
| 3668 | |
| 3669 | ctx = per_cpu_ptr(q->queue_ctx, i); |
| 3670 | for (j = 0; j < set->nr_maps; j++) { |
| 3671 | if (!set->map[j].nr_queues) { |
| 3672 | ctx->hctxs[j] = blk_mq_map_queue_type(q, |
| 3673 | HCTX_TYPE_DEFAULT, i); |
| 3674 | continue; |
| 3675 | } |
| 3676 | hctx_idx = set->map[j].mq_map[i]; |
| 3677 | /* unmapped hw queue can be remapped after CPU topo changed */ |
| 3678 | if (!set->tags[hctx_idx] && |
| 3679 | !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) { |
| 3680 | /* |
| 3681 | * If tags initialization fail for some hctx, |
| 3682 | * that hctx won't be brought online. In this |
| 3683 | * case, remap the current ctx to hctx[0] which |
| 3684 | * is guaranteed to always have tags allocated |
| 3685 | */ |
| 3686 | set->map[j].mq_map[i] = 0; |
| 3687 | } |
| 3688 | |
| 3689 | hctx = blk_mq_map_queue_type(q, j, i); |
| 3690 | ctx->hctxs[j] = hctx; |
| 3691 | /* |
| 3692 | * If the CPU is already set in the mask, then we've |
| 3693 | * mapped this one already. This can happen if |
| 3694 | * devices share queues across queue maps. |
| 3695 | */ |
| 3696 | if (cpumask_test_cpu(i, hctx->cpumask)) |
| 3697 | continue; |
| 3698 | |
| 3699 | cpumask_set_cpu(i, hctx->cpumask); |
| 3700 | hctx->type = j; |
| 3701 | ctx->index_hw[hctx->type] = hctx->nr_ctx; |
| 3702 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 3703 | |
| 3704 | /* |
| 3705 | * If the nr_ctx type overflows, we have exceeded the |
| 3706 | * amount of sw queues we can support. |
| 3707 | */ |
| 3708 | BUG_ON(!hctx->nr_ctx); |
| 3709 | } |
| 3710 | |
| 3711 | for (; j < HCTX_MAX_TYPES; j++) |
| 3712 | ctx->hctxs[j] = blk_mq_map_queue_type(q, |
| 3713 | HCTX_TYPE_DEFAULT, i); |
| 3714 | } |
| 3715 | |
| 3716 | queue_for_each_hw_ctx(q, hctx, i) { |
| 3717 | /* |
| 3718 | * If no software queues are mapped to this hardware queue, |
| 3719 | * disable it and free the request entries. |
| 3720 | */ |
| 3721 | if (!hctx->nr_ctx) { |
| 3722 | /* Never unmap queue 0. We need it as a |
| 3723 | * fallback in case of a new remap fails |
| 3724 | * allocation |
| 3725 | */ |
| 3726 | if (i) |
| 3727 | __blk_mq_free_map_and_rqs(set, i); |
| 3728 | |
| 3729 | hctx->tags = NULL; |
| 3730 | continue; |
| 3731 | } |
| 3732 | |
| 3733 | hctx->tags = set->tags[i]; |
| 3734 | WARN_ON(!hctx->tags); |
| 3735 | |
| 3736 | /* |
| 3737 | * Set the map size to the number of mapped software queues. |
| 3738 | * This is more accurate and more efficient than looping |
| 3739 | * over all possibly mapped software queues. |
| 3740 | */ |
| 3741 | sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); |
| 3742 | |
| 3743 | /* |
| 3744 | * Initialize batch roundrobin counts |
| 3745 | */ |
| 3746 | hctx->next_cpu = blk_mq_first_mapped_cpu(hctx); |
| 3747 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /* |
| 3752 | * Caller needs to ensure that we're either frozen/quiesced, or that |
| 3753 | * the queue isn't live yet. |
| 3754 | */ |
| 3755 | static void queue_set_hctx_shared(struct request_queue *q, bool shared) |
| 3756 | { |
| 3757 | struct blk_mq_hw_ctx *hctx; |
| 3758 | unsigned long i; |
| 3759 | |
| 3760 | queue_for_each_hw_ctx(q, hctx, i) { |
| 3761 | if (shared) { |
| 3762 | hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; |
| 3763 | } else { |
| 3764 | blk_mq_tag_idle(hctx); |
| 3765 | hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; |
| 3766 | } |
| 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set, |
| 3771 | bool shared) |
| 3772 | { |
| 3773 | struct request_queue *q; |
| 3774 | |
| 3775 | lockdep_assert_held(&set->tag_list_lock); |
| 3776 | |
| 3777 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 3778 | blk_mq_freeze_queue(q); |
| 3779 | queue_set_hctx_shared(q, shared); |
| 3780 | blk_mq_unfreeze_queue(q); |
| 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | static void blk_mq_del_queue_tag_set(struct request_queue *q) |
| 3785 | { |
| 3786 | struct blk_mq_tag_set *set = q->tag_set; |
| 3787 | |
| 3788 | mutex_lock(&set->tag_list_lock); |
| 3789 | list_del(&q->tag_set_list); |
| 3790 | if (list_is_singular(&set->tag_list)) { |
| 3791 | /* just transitioned to unshared */ |
| 3792 | set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED; |
| 3793 | /* update existing queue */ |
| 3794 | blk_mq_update_tag_set_shared(set, false); |
| 3795 | } |
| 3796 | mutex_unlock(&set->tag_list_lock); |
| 3797 | INIT_LIST_HEAD(&q->tag_set_list); |
| 3798 | } |
| 3799 | |
| 3800 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, |
| 3801 | struct request_queue *q) |
| 3802 | { |
| 3803 | mutex_lock(&set->tag_list_lock); |
| 3804 | |
| 3805 | /* |
| 3806 | * Check to see if we're transitioning to shared (from 1 to 2 queues). |
| 3807 | */ |
| 3808 | if (!list_empty(&set->tag_list) && |
| 3809 | !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) { |
| 3810 | set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED; |
| 3811 | /* update existing queue */ |
| 3812 | blk_mq_update_tag_set_shared(set, true); |
| 3813 | } |
| 3814 | if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED) |
| 3815 | queue_set_hctx_shared(q, true); |
| 3816 | list_add_tail(&q->tag_set_list, &set->tag_list); |
| 3817 | |
| 3818 | mutex_unlock(&set->tag_list_lock); |
| 3819 | } |
| 3820 | |
| 3821 | /* All allocations will be freed in release handler of q->mq_kobj */ |
| 3822 | static int blk_mq_alloc_ctxs(struct request_queue *q) |
| 3823 | { |
| 3824 | struct blk_mq_ctxs *ctxs; |
| 3825 | int cpu; |
| 3826 | |
| 3827 | ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL); |
| 3828 | if (!ctxs) |
| 3829 | return -ENOMEM; |
| 3830 | |
| 3831 | ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx); |
| 3832 | if (!ctxs->queue_ctx) |
| 3833 | goto fail; |
| 3834 | |
| 3835 | for_each_possible_cpu(cpu) { |
| 3836 | struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu); |
| 3837 | ctx->ctxs = ctxs; |
| 3838 | } |
| 3839 | |
| 3840 | q->mq_kobj = &ctxs->kobj; |
| 3841 | q->queue_ctx = ctxs->queue_ctx; |
| 3842 | |
| 3843 | return 0; |
| 3844 | fail: |
| 3845 | kfree(ctxs); |
| 3846 | return -ENOMEM; |
| 3847 | } |
| 3848 | |
| 3849 | /* |
| 3850 | * It is the actual release handler for mq, but we do it from |
| 3851 | * request queue's release handler for avoiding use-after-free |
| 3852 | * and headache because q->mq_kobj shouldn't have been introduced, |
| 3853 | * but we can't group ctx/kctx kobj without it. |
| 3854 | */ |
| 3855 | void blk_mq_release(struct request_queue *q) |
| 3856 | { |
| 3857 | struct blk_mq_hw_ctx *hctx, *next; |
| 3858 | unsigned long i; |
| 3859 | |
| 3860 | queue_for_each_hw_ctx(q, hctx, i) |
| 3861 | WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list)); |
| 3862 | |
| 3863 | /* all hctx are in .unused_hctx_list now */ |
| 3864 | list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) { |
| 3865 | list_del_init(&hctx->hctx_list); |
| 3866 | kobject_put(&hctx->kobj); |
| 3867 | } |
| 3868 | |
| 3869 | xa_destroy(&q->hctx_table); |
| 3870 | |
| 3871 | /* |
| 3872 | * release .mq_kobj and sw queue's kobject now because |
| 3873 | * both share lifetime with request queue. |
| 3874 | */ |
| 3875 | blk_mq_sysfs_deinit(q); |
| 3876 | } |
| 3877 | |
| 3878 | static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set, |
| 3879 | void *queuedata) |
| 3880 | { |
| 3881 | struct request_queue *q; |
| 3882 | int ret; |
| 3883 | |
| 3884 | q = blk_alloc_queue(set->numa_node, set->flags & BLK_MQ_F_BLOCKING); |
| 3885 | if (!q) |
| 3886 | return ERR_PTR(-ENOMEM); |
| 3887 | q->queuedata = queuedata; |
| 3888 | ret = blk_mq_init_allocated_queue(set, q); |
| 3889 | if (ret) { |
| 3890 | blk_cleanup_queue(q); |
| 3891 | return ERR_PTR(ret); |
| 3892 | } |
| 3893 | return q; |
| 3894 | } |
| 3895 | |
| 3896 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
| 3897 | { |
| 3898 | return blk_mq_init_queue_data(set, NULL); |
| 3899 | } |
| 3900 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 3901 | |
| 3902 | struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata, |
| 3903 | struct lock_class_key *lkclass) |
| 3904 | { |
| 3905 | struct request_queue *q; |
| 3906 | struct gendisk *disk; |
| 3907 | |
| 3908 | q = blk_mq_init_queue_data(set, queuedata); |
| 3909 | if (IS_ERR(q)) |
| 3910 | return ERR_CAST(q); |
| 3911 | |
| 3912 | disk = __alloc_disk_node(q, set->numa_node, lkclass); |
| 3913 | if (!disk) { |
| 3914 | blk_cleanup_queue(q); |
| 3915 | return ERR_PTR(-ENOMEM); |
| 3916 | } |
| 3917 | return disk; |
| 3918 | } |
| 3919 | EXPORT_SYMBOL(__blk_mq_alloc_disk); |
| 3920 | |
| 3921 | static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx( |
| 3922 | struct blk_mq_tag_set *set, struct request_queue *q, |
| 3923 | int hctx_idx, int node) |
| 3924 | { |
| 3925 | struct blk_mq_hw_ctx *hctx = NULL, *tmp; |
| 3926 | |
| 3927 | /* reuse dead hctx first */ |
| 3928 | spin_lock(&q->unused_hctx_lock); |
| 3929 | list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) { |
| 3930 | if (tmp->numa_node == node) { |
| 3931 | hctx = tmp; |
| 3932 | break; |
| 3933 | } |
| 3934 | } |
| 3935 | if (hctx) |
| 3936 | list_del_init(&hctx->hctx_list); |
| 3937 | spin_unlock(&q->unused_hctx_lock); |
| 3938 | |
| 3939 | if (!hctx) |
| 3940 | hctx = blk_mq_alloc_hctx(q, set, node); |
| 3941 | if (!hctx) |
| 3942 | goto fail; |
| 3943 | |
| 3944 | if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) |
| 3945 | goto free_hctx; |
| 3946 | |
| 3947 | return hctx; |
| 3948 | |
| 3949 | free_hctx: |
| 3950 | kobject_put(&hctx->kobj); |
| 3951 | fail: |
| 3952 | return NULL; |
| 3953 | } |
| 3954 | |
| 3955 | static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, |
| 3956 | struct request_queue *q) |
| 3957 | { |
| 3958 | struct blk_mq_hw_ctx *hctx; |
| 3959 | unsigned long i, j; |
| 3960 | |
| 3961 | /* protect against switching io scheduler */ |
| 3962 | mutex_lock(&q->sysfs_lock); |
| 3963 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 3964 | int old_node; |
| 3965 | int node = blk_mq_get_hctx_node(set, i); |
| 3966 | struct blk_mq_hw_ctx *old_hctx = xa_load(&q->hctx_table, i); |
| 3967 | |
| 3968 | if (old_hctx) { |
| 3969 | old_node = old_hctx->numa_node; |
| 3970 | blk_mq_exit_hctx(q, set, old_hctx, i); |
| 3971 | } |
| 3972 | |
| 3973 | if (!blk_mq_alloc_and_init_hctx(set, q, i, node)) { |
| 3974 | if (!old_hctx) |
| 3975 | break; |
| 3976 | pr_warn("Allocate new hctx on node %d fails, fallback to previous one on node %d\n", |
| 3977 | node, old_node); |
| 3978 | hctx = blk_mq_alloc_and_init_hctx(set, q, i, old_node); |
| 3979 | WARN_ON_ONCE(!hctx); |
| 3980 | } |
| 3981 | } |
| 3982 | /* |
| 3983 | * Increasing nr_hw_queues fails. Free the newly allocated |
| 3984 | * hctxs and keep the previous q->nr_hw_queues. |
| 3985 | */ |
| 3986 | if (i != set->nr_hw_queues) { |
| 3987 | j = q->nr_hw_queues; |
| 3988 | } else { |
| 3989 | j = i; |
| 3990 | q->nr_hw_queues = set->nr_hw_queues; |
| 3991 | } |
| 3992 | |
| 3993 | xa_for_each_start(&q->hctx_table, j, hctx, j) |
| 3994 | blk_mq_exit_hctx(q, set, hctx, j); |
| 3995 | mutex_unlock(&q->sysfs_lock); |
| 3996 | } |
| 3997 | |
| 3998 | static void blk_mq_update_poll_flag(struct request_queue *q) |
| 3999 | { |
| 4000 | struct blk_mq_tag_set *set = q->tag_set; |
| 4001 | |
| 4002 | if (set->nr_maps > HCTX_TYPE_POLL && |
| 4003 | set->map[HCTX_TYPE_POLL].nr_queues) |
| 4004 | blk_queue_flag_set(QUEUE_FLAG_POLL, q); |
| 4005 | else |
| 4006 | blk_queue_flag_clear(QUEUE_FLAG_POLL, q); |
| 4007 | } |
| 4008 | |
| 4009 | int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, |
| 4010 | struct request_queue *q) |
| 4011 | { |
| 4012 | WARN_ON_ONCE(blk_queue_has_srcu(q) != |
| 4013 | !!(set->flags & BLK_MQ_F_BLOCKING)); |
| 4014 | |
| 4015 | /* mark the queue as mq asap */ |
| 4016 | q->mq_ops = set->ops; |
| 4017 | |
| 4018 | q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn, |
| 4019 | blk_mq_poll_stats_bkt, |
| 4020 | BLK_MQ_POLL_STATS_BKTS, q); |
| 4021 | if (!q->poll_cb) |
| 4022 | goto err_exit; |
| 4023 | |
| 4024 | if (blk_mq_alloc_ctxs(q)) |
| 4025 | goto err_poll; |
| 4026 | |
| 4027 | /* init q->mq_kobj and sw queues' kobjects */ |
| 4028 | blk_mq_sysfs_init(q); |
| 4029 | |
| 4030 | INIT_LIST_HEAD(&q->unused_hctx_list); |
| 4031 | spin_lock_init(&q->unused_hctx_lock); |
| 4032 | |
| 4033 | xa_init(&q->hctx_table); |
| 4034 | |
| 4035 | blk_mq_realloc_hw_ctxs(set, q); |
| 4036 | if (!q->nr_hw_queues) |
| 4037 | goto err_hctxs; |
| 4038 | |
| 4039 | INIT_WORK(&q->timeout_work, blk_mq_timeout_work); |
| 4040 | blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); |
| 4041 | |
| 4042 | q->tag_set = set; |
| 4043 | |
| 4044 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
| 4045 | blk_mq_update_poll_flag(q); |
| 4046 | |
| 4047 | INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); |
| 4048 | INIT_LIST_HEAD(&q->requeue_list); |
| 4049 | spin_lock_init(&q->requeue_lock); |
| 4050 | |
| 4051 | q->nr_requests = set->queue_depth; |
| 4052 | |
| 4053 | /* |
| 4054 | * Default to classic polling |
| 4055 | */ |
| 4056 | q->poll_nsec = BLK_MQ_POLL_CLASSIC; |
| 4057 | |
| 4058 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
| 4059 | blk_mq_add_queue_tag_set(set, q); |
| 4060 | blk_mq_map_swqueue(q); |
| 4061 | return 0; |
| 4062 | |
| 4063 | err_hctxs: |
| 4064 | xa_destroy(&q->hctx_table); |
| 4065 | q->nr_hw_queues = 0; |
| 4066 | blk_mq_sysfs_deinit(q); |
| 4067 | err_poll: |
| 4068 | blk_stat_free_callback(q->poll_cb); |
| 4069 | q->poll_cb = NULL; |
| 4070 | err_exit: |
| 4071 | q->mq_ops = NULL; |
| 4072 | return -ENOMEM; |
| 4073 | } |
| 4074 | EXPORT_SYMBOL(blk_mq_init_allocated_queue); |
| 4075 | |
| 4076 | /* tags can _not_ be used after returning from blk_mq_exit_queue */ |
| 4077 | void blk_mq_exit_queue(struct request_queue *q) |
| 4078 | { |
| 4079 | struct blk_mq_tag_set *set = q->tag_set; |
| 4080 | |
| 4081 | /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */ |
| 4082 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
| 4083 | /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */ |
| 4084 | blk_mq_del_queue_tag_set(q); |
| 4085 | } |
| 4086 | |
| 4087 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
| 4088 | { |
| 4089 | int i; |
| 4090 | |
| 4091 | if (blk_mq_is_shared_tags(set->flags)) { |
| 4092 | set->shared_tags = blk_mq_alloc_map_and_rqs(set, |
| 4093 | BLK_MQ_NO_HCTX_IDX, |
| 4094 | set->queue_depth); |
| 4095 | if (!set->shared_tags) |
| 4096 | return -ENOMEM; |
| 4097 | } |
| 4098 | |
| 4099 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 4100 | if (!__blk_mq_alloc_map_and_rqs(set, i)) |
| 4101 | goto out_unwind; |
| 4102 | cond_resched(); |
| 4103 | } |
| 4104 | |
| 4105 | return 0; |
| 4106 | |
| 4107 | out_unwind: |
| 4108 | while (--i >= 0) |
| 4109 | __blk_mq_free_map_and_rqs(set, i); |
| 4110 | |
| 4111 | if (blk_mq_is_shared_tags(set->flags)) { |
| 4112 | blk_mq_free_map_and_rqs(set, set->shared_tags, |
| 4113 | BLK_MQ_NO_HCTX_IDX); |
| 4114 | } |
| 4115 | |
| 4116 | return -ENOMEM; |
| 4117 | } |
| 4118 | |
| 4119 | /* |
| 4120 | * Allocate the request maps associated with this tag_set. Note that this |
| 4121 | * may reduce the depth asked for, if memory is tight. set->queue_depth |
| 4122 | * will be updated to reflect the allocated depth. |
| 4123 | */ |
| 4124 | static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set) |
| 4125 | { |
| 4126 | unsigned int depth; |
| 4127 | int err; |
| 4128 | |
| 4129 | depth = set->queue_depth; |
| 4130 | do { |
| 4131 | err = __blk_mq_alloc_rq_maps(set); |
| 4132 | if (!err) |
| 4133 | break; |
| 4134 | |
| 4135 | set->queue_depth >>= 1; |
| 4136 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { |
| 4137 | err = -ENOMEM; |
| 4138 | break; |
| 4139 | } |
| 4140 | } while (set->queue_depth); |
| 4141 | |
| 4142 | if (!set->queue_depth || err) { |
| 4143 | pr_err("blk-mq: failed to allocate request map\n"); |
| 4144 | return -ENOMEM; |
| 4145 | } |
| 4146 | |
| 4147 | if (depth != set->queue_depth) |
| 4148 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", |
| 4149 | depth, set->queue_depth); |
| 4150 | |
| 4151 | return 0; |
| 4152 | } |
| 4153 | |
| 4154 | static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) |
| 4155 | { |
| 4156 | /* |
| 4157 | * blk_mq_map_queues() and multiple .map_queues() implementations |
| 4158 | * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the |
| 4159 | * number of hardware queues. |
| 4160 | */ |
| 4161 | if (set->nr_maps == 1) |
| 4162 | set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues; |
| 4163 | |
| 4164 | if (set->ops->map_queues && !is_kdump_kernel()) { |
| 4165 | int i; |
| 4166 | |
| 4167 | /* |
| 4168 | * transport .map_queues is usually done in the following |
| 4169 | * way: |
| 4170 | * |
| 4171 | * for (queue = 0; queue < set->nr_hw_queues; queue++) { |
| 4172 | * mask = get_cpu_mask(queue) |
| 4173 | * for_each_cpu(cpu, mask) |
| 4174 | * set->map[x].mq_map[cpu] = queue; |
| 4175 | * } |
| 4176 | * |
| 4177 | * When we need to remap, the table has to be cleared for |
| 4178 | * killing stale mapping since one CPU may not be mapped |
| 4179 | * to any hw queue. |
| 4180 | */ |
| 4181 | for (i = 0; i < set->nr_maps; i++) |
| 4182 | blk_mq_clear_mq_map(&set->map[i]); |
| 4183 | |
| 4184 | return set->ops->map_queues(set); |
| 4185 | } else { |
| 4186 | BUG_ON(set->nr_maps > 1); |
| 4187 | return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
| 4188 | } |
| 4189 | } |
| 4190 | |
| 4191 | static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set, |
| 4192 | int cur_nr_hw_queues, int new_nr_hw_queues) |
| 4193 | { |
| 4194 | struct blk_mq_tags **new_tags; |
| 4195 | |
| 4196 | if (cur_nr_hw_queues >= new_nr_hw_queues) |
| 4197 | return 0; |
| 4198 | |
| 4199 | new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *), |
| 4200 | GFP_KERNEL, set->numa_node); |
| 4201 | if (!new_tags) |
| 4202 | return -ENOMEM; |
| 4203 | |
| 4204 | if (set->tags) |
| 4205 | memcpy(new_tags, set->tags, cur_nr_hw_queues * |
| 4206 | sizeof(*set->tags)); |
| 4207 | kfree(set->tags); |
| 4208 | set->tags = new_tags; |
| 4209 | set->nr_hw_queues = new_nr_hw_queues; |
| 4210 | |
| 4211 | return 0; |
| 4212 | } |
| 4213 | |
| 4214 | static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set, |
| 4215 | int new_nr_hw_queues) |
| 4216 | { |
| 4217 | return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues); |
| 4218 | } |
| 4219 | |
| 4220 | /* |
| 4221 | * Alloc a tag set to be associated with one or more request queues. |
| 4222 | * May fail with EINVAL for various error conditions. May adjust the |
| 4223 | * requested depth down, if it's too large. In that case, the set |
| 4224 | * value will be stored in set->queue_depth. |
| 4225 | */ |
| 4226 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 4227 | { |
| 4228 | int i, ret; |
| 4229 | |
| 4230 | BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); |
| 4231 | |
| 4232 | if (!set->nr_hw_queues) |
| 4233 | return -EINVAL; |
| 4234 | if (!set->queue_depth) |
| 4235 | return -EINVAL; |
| 4236 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 4237 | return -EINVAL; |
| 4238 | |
| 4239 | if (!set->ops->queue_rq) |
| 4240 | return -EINVAL; |
| 4241 | |
| 4242 | if (!set->ops->get_budget ^ !set->ops->put_budget) |
| 4243 | return -EINVAL; |
| 4244 | |
| 4245 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
| 4246 | pr_info("blk-mq: reduced tag depth to %u\n", |
| 4247 | BLK_MQ_MAX_DEPTH); |
| 4248 | set->queue_depth = BLK_MQ_MAX_DEPTH; |
| 4249 | } |
| 4250 | |
| 4251 | if (!set->nr_maps) |
| 4252 | set->nr_maps = 1; |
| 4253 | else if (set->nr_maps > HCTX_MAX_TYPES) |
| 4254 | return -EINVAL; |
| 4255 | |
| 4256 | /* |
| 4257 | * If a crashdump is active, then we are potentially in a very |
| 4258 | * memory constrained environment. Limit us to 1 queue and |
| 4259 | * 64 tags to prevent using too much memory. |
| 4260 | */ |
| 4261 | if (is_kdump_kernel()) { |
| 4262 | set->nr_hw_queues = 1; |
| 4263 | set->nr_maps = 1; |
| 4264 | set->queue_depth = min(64U, set->queue_depth); |
| 4265 | } |
| 4266 | /* |
| 4267 | * There is no use for more h/w queues than cpus if we just have |
| 4268 | * a single map |
| 4269 | */ |
| 4270 | if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids) |
| 4271 | set->nr_hw_queues = nr_cpu_ids; |
| 4272 | |
| 4273 | if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0) |
| 4274 | return -ENOMEM; |
| 4275 | |
| 4276 | ret = -ENOMEM; |
| 4277 | for (i = 0; i < set->nr_maps; i++) { |
| 4278 | set->map[i].mq_map = kcalloc_node(nr_cpu_ids, |
| 4279 | sizeof(set->map[i].mq_map[0]), |
| 4280 | GFP_KERNEL, set->numa_node); |
| 4281 | if (!set->map[i].mq_map) |
| 4282 | goto out_free_mq_map; |
| 4283 | set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues; |
| 4284 | } |
| 4285 | |
| 4286 | ret = blk_mq_update_queue_map(set); |
| 4287 | if (ret) |
| 4288 | goto out_free_mq_map; |
| 4289 | |
| 4290 | ret = blk_mq_alloc_set_map_and_rqs(set); |
| 4291 | if (ret) |
| 4292 | goto out_free_mq_map; |
| 4293 | |
| 4294 | mutex_init(&set->tag_list_lock); |
| 4295 | INIT_LIST_HEAD(&set->tag_list); |
| 4296 | |
| 4297 | return 0; |
| 4298 | |
| 4299 | out_free_mq_map: |
| 4300 | for (i = 0; i < set->nr_maps; i++) { |
| 4301 | kfree(set->map[i].mq_map); |
| 4302 | set->map[i].mq_map = NULL; |
| 4303 | } |
| 4304 | kfree(set->tags); |
| 4305 | set->tags = NULL; |
| 4306 | return ret; |
| 4307 | } |
| 4308 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 4309 | |
| 4310 | /* allocate and initialize a tagset for a simple single-queue device */ |
| 4311 | int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set, |
| 4312 | const struct blk_mq_ops *ops, unsigned int queue_depth, |
| 4313 | unsigned int set_flags) |
| 4314 | { |
| 4315 | memset(set, 0, sizeof(*set)); |
| 4316 | set->ops = ops; |
| 4317 | set->nr_hw_queues = 1; |
| 4318 | set->nr_maps = 1; |
| 4319 | set->queue_depth = queue_depth; |
| 4320 | set->numa_node = NUMA_NO_NODE; |
| 4321 | set->flags = set_flags; |
| 4322 | return blk_mq_alloc_tag_set(set); |
| 4323 | } |
| 4324 | EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set); |
| 4325 | |
| 4326 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 4327 | { |
| 4328 | int i, j; |
| 4329 | |
| 4330 | for (i = 0; i < set->nr_hw_queues; i++) |
| 4331 | __blk_mq_free_map_and_rqs(set, i); |
| 4332 | |
| 4333 | if (blk_mq_is_shared_tags(set->flags)) { |
| 4334 | blk_mq_free_map_and_rqs(set, set->shared_tags, |
| 4335 | BLK_MQ_NO_HCTX_IDX); |
| 4336 | } |
| 4337 | |
| 4338 | for (j = 0; j < set->nr_maps; j++) { |
| 4339 | kfree(set->map[j].mq_map); |
| 4340 | set->map[j].mq_map = NULL; |
| 4341 | } |
| 4342 | |
| 4343 | kfree(set->tags); |
| 4344 | set->tags = NULL; |
| 4345 | } |
| 4346 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 4347 | |
| 4348 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
| 4349 | { |
| 4350 | struct blk_mq_tag_set *set = q->tag_set; |
| 4351 | struct blk_mq_hw_ctx *hctx; |
| 4352 | int ret; |
| 4353 | unsigned long i; |
| 4354 | |
| 4355 | if (!set) |
| 4356 | return -EINVAL; |
| 4357 | |
| 4358 | if (q->nr_requests == nr) |
| 4359 | return 0; |
| 4360 | |
| 4361 | blk_mq_freeze_queue(q); |
| 4362 | blk_mq_quiesce_queue(q); |
| 4363 | |
| 4364 | ret = 0; |
| 4365 | queue_for_each_hw_ctx(q, hctx, i) { |
| 4366 | if (!hctx->tags) |
| 4367 | continue; |
| 4368 | /* |
| 4369 | * If we're using an MQ scheduler, just update the scheduler |
| 4370 | * queue depth. This is similar to what the old code would do. |
| 4371 | */ |
| 4372 | if (hctx->sched_tags) { |
| 4373 | ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags, |
| 4374 | nr, true); |
| 4375 | } else { |
| 4376 | ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr, |
| 4377 | false); |
| 4378 | } |
| 4379 | if (ret) |
| 4380 | break; |
| 4381 | if (q->elevator && q->elevator->type->ops.depth_updated) |
| 4382 | q->elevator->type->ops.depth_updated(hctx); |
| 4383 | } |
| 4384 | if (!ret) { |
| 4385 | q->nr_requests = nr; |
| 4386 | if (blk_mq_is_shared_tags(set->flags)) { |
| 4387 | if (q->elevator) |
| 4388 | blk_mq_tag_update_sched_shared_tags(q); |
| 4389 | else |
| 4390 | blk_mq_tag_resize_shared_tags(set, nr); |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | blk_mq_unquiesce_queue(q); |
| 4395 | blk_mq_unfreeze_queue(q); |
| 4396 | |
| 4397 | return ret; |
| 4398 | } |
| 4399 | |
| 4400 | /* |
| 4401 | * request_queue and elevator_type pair. |
| 4402 | * It is just used by __blk_mq_update_nr_hw_queues to cache |
| 4403 | * the elevator_type associated with a request_queue. |
| 4404 | */ |
| 4405 | struct blk_mq_qe_pair { |
| 4406 | struct list_head node; |
| 4407 | struct request_queue *q; |
| 4408 | struct elevator_type *type; |
| 4409 | }; |
| 4410 | |
| 4411 | /* |
| 4412 | * Cache the elevator_type in qe pair list and switch the |
| 4413 | * io scheduler to 'none' |
| 4414 | */ |
| 4415 | static bool blk_mq_elv_switch_none(struct list_head *head, |
| 4416 | struct request_queue *q) |
| 4417 | { |
| 4418 | struct blk_mq_qe_pair *qe; |
| 4419 | |
| 4420 | if (!q->elevator) |
| 4421 | return true; |
| 4422 | |
| 4423 | qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY); |
| 4424 | if (!qe) |
| 4425 | return false; |
| 4426 | |
| 4427 | INIT_LIST_HEAD(&qe->node); |
| 4428 | qe->q = q; |
| 4429 | qe->type = q->elevator->type; |
| 4430 | list_add(&qe->node, head); |
| 4431 | |
| 4432 | mutex_lock(&q->sysfs_lock); |
| 4433 | /* |
| 4434 | * After elevator_switch_mq, the previous elevator_queue will be |
| 4435 | * released by elevator_release. The reference of the io scheduler |
| 4436 | * module get by elevator_get will also be put. So we need to get |
| 4437 | * a reference of the io scheduler module here to prevent it to be |
| 4438 | * removed. |
| 4439 | */ |
| 4440 | __module_get(qe->type->elevator_owner); |
| 4441 | elevator_switch_mq(q, NULL); |
| 4442 | mutex_unlock(&q->sysfs_lock); |
| 4443 | |
| 4444 | return true; |
| 4445 | } |
| 4446 | |
| 4447 | static void blk_mq_elv_switch_back(struct list_head *head, |
| 4448 | struct request_queue *q) |
| 4449 | { |
| 4450 | struct blk_mq_qe_pair *qe; |
| 4451 | struct elevator_type *t = NULL; |
| 4452 | |
| 4453 | list_for_each_entry(qe, head, node) |
| 4454 | if (qe->q == q) { |
| 4455 | t = qe->type; |
| 4456 | break; |
| 4457 | } |
| 4458 | |
| 4459 | if (!t) |
| 4460 | return; |
| 4461 | |
| 4462 | list_del(&qe->node); |
| 4463 | kfree(qe); |
| 4464 | |
| 4465 | mutex_lock(&q->sysfs_lock); |
| 4466 | elevator_switch_mq(q, t); |
| 4467 | mutex_unlock(&q->sysfs_lock); |
| 4468 | } |
| 4469 | |
| 4470 | static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, |
| 4471 | int nr_hw_queues) |
| 4472 | { |
| 4473 | struct request_queue *q; |
| 4474 | LIST_HEAD(head); |
| 4475 | int prev_nr_hw_queues; |
| 4476 | |
| 4477 | lockdep_assert_held(&set->tag_list_lock); |
| 4478 | |
| 4479 | if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids) |
| 4480 | nr_hw_queues = nr_cpu_ids; |
| 4481 | if (nr_hw_queues < 1) |
| 4482 | return; |
| 4483 | if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues) |
| 4484 | return; |
| 4485 | |
| 4486 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
| 4487 | blk_mq_freeze_queue(q); |
| 4488 | /* |
| 4489 | * Switch IO scheduler to 'none', cleaning up the data associated |
| 4490 | * with the previous scheduler. We will switch back once we are done |
| 4491 | * updating the new sw to hw queue mappings. |
| 4492 | */ |
| 4493 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
| 4494 | if (!blk_mq_elv_switch_none(&head, q)) |
| 4495 | goto switch_back; |
| 4496 | |
| 4497 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 4498 | blk_mq_debugfs_unregister_hctxs(q); |
| 4499 | blk_mq_sysfs_unregister(q); |
| 4500 | } |
| 4501 | |
| 4502 | prev_nr_hw_queues = set->nr_hw_queues; |
| 4503 | if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) < |
| 4504 | 0) |
| 4505 | goto reregister; |
| 4506 | |
| 4507 | set->nr_hw_queues = nr_hw_queues; |
| 4508 | fallback: |
| 4509 | blk_mq_update_queue_map(set); |
| 4510 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 4511 | blk_mq_realloc_hw_ctxs(set, q); |
| 4512 | blk_mq_update_poll_flag(q); |
| 4513 | if (q->nr_hw_queues != set->nr_hw_queues) { |
| 4514 | int i = prev_nr_hw_queues; |
| 4515 | |
| 4516 | pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n", |
| 4517 | nr_hw_queues, prev_nr_hw_queues); |
| 4518 | for (; i < set->nr_hw_queues; i++) |
| 4519 | __blk_mq_free_map_and_rqs(set, i); |
| 4520 | |
| 4521 | set->nr_hw_queues = prev_nr_hw_queues; |
| 4522 | blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]); |
| 4523 | goto fallback; |
| 4524 | } |
| 4525 | blk_mq_map_swqueue(q); |
| 4526 | } |
| 4527 | |
| 4528 | reregister: |
| 4529 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 4530 | blk_mq_sysfs_register(q); |
| 4531 | blk_mq_debugfs_register_hctxs(q); |
| 4532 | } |
| 4533 | |
| 4534 | switch_back: |
| 4535 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
| 4536 | blk_mq_elv_switch_back(&head, q); |
| 4537 | |
| 4538 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
| 4539 | blk_mq_unfreeze_queue(q); |
| 4540 | } |
| 4541 | |
| 4542 | void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) |
| 4543 | { |
| 4544 | mutex_lock(&set->tag_list_lock); |
| 4545 | __blk_mq_update_nr_hw_queues(set, nr_hw_queues); |
| 4546 | mutex_unlock(&set->tag_list_lock); |
| 4547 | } |
| 4548 | EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); |
| 4549 | |
| 4550 | /* Enable polling stats and return whether they were already enabled. */ |
| 4551 | static bool blk_poll_stats_enable(struct request_queue *q) |
| 4552 | { |
| 4553 | if (q->poll_stat) |
| 4554 | return true; |
| 4555 | |
| 4556 | return blk_stats_alloc_enable(q); |
| 4557 | } |
| 4558 | |
| 4559 | static void blk_mq_poll_stats_start(struct request_queue *q) |
| 4560 | { |
| 4561 | /* |
| 4562 | * We don't arm the callback if polling stats are not enabled or the |
| 4563 | * callback is already active. |
| 4564 | */ |
| 4565 | if (!q->poll_stat || blk_stat_is_active(q->poll_cb)) |
| 4566 | return; |
| 4567 | |
| 4568 | blk_stat_activate_msecs(q->poll_cb, 100); |
| 4569 | } |
| 4570 | |
| 4571 | static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb) |
| 4572 | { |
| 4573 | struct request_queue *q = cb->data; |
| 4574 | int bucket; |
| 4575 | |
| 4576 | for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) { |
| 4577 | if (cb->stat[bucket].nr_samples) |
| 4578 | q->poll_stat[bucket] = cb->stat[bucket]; |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | static unsigned long blk_mq_poll_nsecs(struct request_queue *q, |
| 4583 | struct request *rq) |
| 4584 | { |
| 4585 | unsigned long ret = 0; |
| 4586 | int bucket; |
| 4587 | |
| 4588 | /* |
| 4589 | * If stats collection isn't on, don't sleep but turn it on for |
| 4590 | * future users |
| 4591 | */ |
| 4592 | if (!blk_poll_stats_enable(q)) |
| 4593 | return 0; |
| 4594 | |
| 4595 | /* |
| 4596 | * As an optimistic guess, use half of the mean service time |
| 4597 | * for this type of request. We can (and should) make this smarter. |
| 4598 | * For instance, if the completion latencies are tight, we can |
| 4599 | * get closer than just half the mean. This is especially |
| 4600 | * important on devices where the completion latencies are longer |
| 4601 | * than ~10 usec. We do use the stats for the relevant IO size |
| 4602 | * if available which does lead to better estimates. |
| 4603 | */ |
| 4604 | bucket = blk_mq_poll_stats_bkt(rq); |
| 4605 | if (bucket < 0) |
| 4606 | return ret; |
| 4607 | |
| 4608 | if (q->poll_stat[bucket].nr_samples) |
| 4609 | ret = (q->poll_stat[bucket].mean + 1) / 2; |
| 4610 | |
| 4611 | return ret; |
| 4612 | } |
| 4613 | |
| 4614 | static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc) |
| 4615 | { |
| 4616 | struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc); |
| 4617 | struct request *rq = blk_qc_to_rq(hctx, qc); |
| 4618 | struct hrtimer_sleeper hs; |
| 4619 | enum hrtimer_mode mode; |
| 4620 | unsigned int nsecs; |
| 4621 | ktime_t kt; |
| 4622 | |
| 4623 | /* |
| 4624 | * If a request has completed on queue that uses an I/O scheduler, we |
| 4625 | * won't get back a request from blk_qc_to_rq. |
| 4626 | */ |
| 4627 | if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT)) |
| 4628 | return false; |
| 4629 | |
| 4630 | /* |
| 4631 | * If we get here, hybrid polling is enabled. Hence poll_nsec can be: |
| 4632 | * |
| 4633 | * 0: use half of prev avg |
| 4634 | * >0: use this specific value |
| 4635 | */ |
| 4636 | if (q->poll_nsec > 0) |
| 4637 | nsecs = q->poll_nsec; |
| 4638 | else |
| 4639 | nsecs = blk_mq_poll_nsecs(q, rq); |
| 4640 | |
| 4641 | if (!nsecs) |
| 4642 | return false; |
| 4643 | |
| 4644 | rq->rq_flags |= RQF_MQ_POLL_SLEPT; |
| 4645 | |
| 4646 | /* |
| 4647 | * This will be replaced with the stats tracking code, using |
| 4648 | * 'avg_completion_time / 2' as the pre-sleep target. |
| 4649 | */ |
| 4650 | kt = nsecs; |
| 4651 | |
| 4652 | mode = HRTIMER_MODE_REL; |
| 4653 | hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode); |
| 4654 | hrtimer_set_expires(&hs.timer, kt); |
| 4655 | |
| 4656 | do { |
| 4657 | if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE) |
| 4658 | break; |
| 4659 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 4660 | hrtimer_sleeper_start_expires(&hs, mode); |
| 4661 | if (hs.task) |
| 4662 | io_schedule(); |
| 4663 | hrtimer_cancel(&hs.timer); |
| 4664 | mode = HRTIMER_MODE_ABS; |
| 4665 | } while (hs.task && !signal_pending(current)); |
| 4666 | |
| 4667 | __set_current_state(TASK_RUNNING); |
| 4668 | destroy_hrtimer_on_stack(&hs.timer); |
| 4669 | |
| 4670 | /* |
| 4671 | * If we sleep, have the caller restart the poll loop to reset the |
| 4672 | * state. Like for the other success return cases, the caller is |
| 4673 | * responsible for checking if the IO completed. If the IO isn't |
| 4674 | * complete, we'll get called again and will go straight to the busy |
| 4675 | * poll loop. |
| 4676 | */ |
| 4677 | return true; |
| 4678 | } |
| 4679 | |
| 4680 | static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie, |
| 4681 | struct io_comp_batch *iob, unsigned int flags) |
| 4682 | { |
| 4683 | struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie); |
| 4684 | long state = get_current_state(); |
| 4685 | int ret; |
| 4686 | |
| 4687 | do { |
| 4688 | ret = q->mq_ops->poll(hctx, iob); |
| 4689 | if (ret > 0) { |
| 4690 | __set_current_state(TASK_RUNNING); |
| 4691 | return ret; |
| 4692 | } |
| 4693 | |
| 4694 | if (signal_pending_state(state, current)) |
| 4695 | __set_current_state(TASK_RUNNING); |
| 4696 | if (task_is_running(current)) |
| 4697 | return 1; |
| 4698 | |
| 4699 | if (ret < 0 || (flags & BLK_POLL_ONESHOT)) |
| 4700 | break; |
| 4701 | cpu_relax(); |
| 4702 | } while (!need_resched()); |
| 4703 | |
| 4704 | __set_current_state(TASK_RUNNING); |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob, |
| 4709 | unsigned int flags) |
| 4710 | { |
| 4711 | if (!(flags & BLK_POLL_NOSLEEP) && |
| 4712 | q->poll_nsec != BLK_MQ_POLL_CLASSIC) { |
| 4713 | if (blk_mq_poll_hybrid(q, cookie)) |
| 4714 | return 1; |
| 4715 | } |
| 4716 | return blk_mq_poll_classic(q, cookie, iob, flags); |
| 4717 | } |
| 4718 | |
| 4719 | unsigned int blk_mq_rq_cpu(struct request *rq) |
| 4720 | { |
| 4721 | return rq->mq_ctx->cpu; |
| 4722 | } |
| 4723 | EXPORT_SYMBOL(blk_mq_rq_cpu); |
| 4724 | |
| 4725 | void blk_mq_cancel_work_sync(struct request_queue *q) |
| 4726 | { |
| 4727 | if (queue_is_mq(q)) { |
| 4728 | struct blk_mq_hw_ctx *hctx; |
| 4729 | unsigned long i; |
| 4730 | |
| 4731 | cancel_delayed_work_sync(&q->requeue_work); |
| 4732 | |
| 4733 | queue_for_each_hw_ctx(q, hctx, i) |
| 4734 | cancel_delayed_work_sync(&hctx->run_work); |
| 4735 | } |
| 4736 | } |
| 4737 | |
| 4738 | static int __init blk_mq_init(void) |
| 4739 | { |
| 4740 | int i; |
| 4741 | |
| 4742 | for_each_possible_cpu(i) |
| 4743 | init_llist_head(&per_cpu(blk_cpu_done, i)); |
| 4744 | open_softirq(BLOCK_SOFTIRQ, blk_done_softirq); |
| 4745 | |
| 4746 | cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD, |
| 4747 | "block/softirq:dead", NULL, |
| 4748 | blk_softirq_cpu_dead); |
| 4749 | cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, |
| 4750 | blk_mq_hctx_notify_dead); |
| 4751 | cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online", |
| 4752 | blk_mq_hctx_notify_online, |
| 4753 | blk_mq_hctx_notify_offline); |
| 4754 | return 0; |
| 4755 | } |
| 4756 | subsys_initcall(blk_mq_init); |