Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * blk-mq scheduling framework |
| 3 | * |
| 4 | * Copyright (C) 2016 Jens Axboe |
| 5 | */ |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/blk-mq.h> |
| 9 | |
| 10 | #include <trace/events/block.h> |
| 11 | |
| 12 | #include "blk.h" |
| 13 | #include "blk-mq.h" |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 14 | #include "blk-mq-debugfs.h" |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 15 | #include "blk-mq-sched.h" |
| 16 | #include "blk-mq-tag.h" |
| 17 | #include "blk-wbt.h" |
| 18 | |
| 19 | void blk_mq_sched_free_hctx_data(struct request_queue *q, |
| 20 | void (*exit)(struct blk_mq_hw_ctx *)) |
| 21 | { |
| 22 | struct blk_mq_hw_ctx *hctx; |
| 23 | int i; |
| 24 | |
| 25 | queue_for_each_hw_ctx(q, hctx, i) { |
| 26 | if (exit && hctx->sched_data) |
| 27 | exit(hctx); |
| 28 | kfree(hctx->sched_data); |
| 29 | hctx->sched_data = NULL; |
| 30 | } |
| 31 | } |
| 32 | EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data); |
| 33 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 34 | static void __blk_mq_sched_assign_ioc(struct request_queue *q, |
Paolo Valente | f1ba826 | 2017-02-07 18:24:43 +0100 | [diff] [blame] | 35 | struct request *rq, |
| 36 | struct bio *bio, |
| 37 | struct io_context *ioc) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 38 | { |
Christoph Hellwig | ea511e3 | 2017-06-16 18:15:20 +0200 | [diff] [blame^] | 39 | struct elevator_queue *e = q->elevator; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 40 | struct io_cq *icq; |
| 41 | |
| 42 | spin_lock_irq(q->queue_lock); |
| 43 | icq = ioc_lookup_icq(ioc, q); |
| 44 | spin_unlock_irq(q->queue_lock); |
| 45 | |
| 46 | if (!icq) { |
| 47 | icq = ioc_create_icq(ioc, q, GFP_ATOMIC); |
| 48 | if (!icq) |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | rq->elv.icq = icq; |
Christoph Hellwig | ea511e3 | 2017-06-16 18:15:20 +0200 | [diff] [blame^] | 53 | if (e && e->type->ops.mq.get_rq_priv && |
| 54 | e->type->ops.mq.get_rq_priv(q, rq, bio)) { |
| 55 | rq->elv.icq = NULL; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
Christoph Hellwig | ea511e3 | 2017-06-16 18:15:20 +0200 | [diff] [blame^] | 59 | rq->rq_flags |= RQF_ELVPRIV; |
| 60 | get_io_context(icq->ioc); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Christoph Hellwig | d2c0d38 | 2017-06-16 18:15:19 +0200 | [diff] [blame] | 63 | void blk_mq_sched_assign_ioc(struct request_queue *q, struct request *rq, |
| 64 | struct bio *bio) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 65 | { |
| 66 | struct io_context *ioc; |
| 67 | |
| 68 | ioc = rq_ioc(bio); |
| 69 | if (ioc) |
Paolo Valente | f1ba826 | 2017-02-07 18:24:43 +0100 | [diff] [blame] | 70 | __blk_mq_sched_assign_ioc(q, rq, bio, ioc); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 73 | void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) |
| 74 | { |
Omar Sandoval | 81380ca | 2017-04-07 08:56:26 -0600 | [diff] [blame] | 75 | struct request_queue *q = hctx->queue; |
| 76 | struct elevator_queue *e = q->elevator; |
Jens Axboe | 64765a7 | 2017-02-17 11:39:26 -0700 | [diff] [blame] | 77 | const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request; |
| 78 | bool did_work = false; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 79 | LIST_HEAD(rq_list); |
| 80 | |
| 81 | if (unlikely(blk_mq_hctx_stopped(hctx))) |
| 82 | return; |
| 83 | |
| 84 | hctx->run++; |
| 85 | |
| 86 | /* |
| 87 | * If we have previous entries on our dispatch list, grab them first for |
| 88 | * more fair dispatch. |
| 89 | */ |
| 90 | if (!list_empty_careful(&hctx->dispatch)) { |
| 91 | spin_lock(&hctx->lock); |
| 92 | if (!list_empty(&hctx->dispatch)) |
| 93 | list_splice_init(&hctx->dispatch, &rq_list); |
| 94 | spin_unlock(&hctx->lock); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Only ask the scheduler for requests, if we didn't have residual |
| 99 | * requests from the dispatch list. This is to avoid the case where |
| 100 | * we only ever dispatch a fraction of the requests available because |
| 101 | * of low device queue depth. Once we pull requests out of the IO |
| 102 | * scheduler, we can no longer merge or sort them. So it's best to |
| 103 | * leave them there for as long as we can. Mark the hw queue as |
| 104 | * needing a restart in that case. |
| 105 | */ |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 106 | if (!list_empty(&rq_list)) { |
Omar Sandoval | d38d351 | 2017-02-22 10:58:30 -0800 | [diff] [blame] | 107 | blk_mq_sched_mark_restart_hctx(hctx); |
Omar Sandoval | 81380ca | 2017-04-07 08:56:26 -0600 | [diff] [blame] | 108 | did_work = blk_mq_dispatch_rq_list(q, &rq_list); |
Jens Axboe | 64765a7 | 2017-02-17 11:39:26 -0700 | [diff] [blame] | 109 | } else if (!has_sched_dispatch) { |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 110 | blk_mq_flush_busy_ctxs(hctx, &rq_list); |
Omar Sandoval | 81380ca | 2017-04-07 08:56:26 -0600 | [diff] [blame] | 111 | blk_mq_dispatch_rq_list(q, &rq_list); |
Jens Axboe | 64765a7 | 2017-02-17 11:39:26 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * We want to dispatch from the scheduler if we had no work left |
| 116 | * on the dispatch list, OR if we did have work but weren't able |
| 117 | * to make progress. |
| 118 | */ |
| 119 | if (!did_work && has_sched_dispatch) { |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 120 | do { |
| 121 | struct request *rq; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 122 | |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 123 | rq = e->type->ops.mq.dispatch_request(hctx); |
| 124 | if (!rq) |
| 125 | break; |
| 126 | list_add(&rq->queuelist, &rq_list); |
Omar Sandoval | 81380ca | 2017-04-07 08:56:26 -0600 | [diff] [blame] | 127 | } while (blk_mq_dispatch_rq_list(q, &rq_list)); |
Jens Axboe | c13660a | 2017-01-26 12:40:07 -0700 | [diff] [blame] | 128 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jens Axboe | e4d750c | 2017-02-03 09:48:28 -0700 | [diff] [blame] | 131 | bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, |
| 132 | struct request **merged_request) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 133 | { |
| 134 | struct request *rq; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 135 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 136 | switch (elv_merge(q, &rq, bio)) { |
| 137 | case ELEVATOR_BACK_MERGE: |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 138 | if (!blk_mq_sched_allow_merge(q, rq, bio)) |
| 139 | return false; |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 140 | if (!bio_attempt_back_merge(q, rq, bio)) |
| 141 | return false; |
| 142 | *merged_request = attempt_back_merge(q, rq); |
| 143 | if (!*merged_request) |
| 144 | elv_merged_request(q, rq, ELEVATOR_BACK_MERGE); |
| 145 | return true; |
| 146 | case ELEVATOR_FRONT_MERGE: |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 147 | if (!blk_mq_sched_allow_merge(q, rq, bio)) |
| 148 | return false; |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 149 | if (!bio_attempt_front_merge(q, rq, bio)) |
| 150 | return false; |
| 151 | *merged_request = attempt_front_merge(q, rq); |
| 152 | if (!*merged_request) |
| 153 | elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE); |
| 154 | return true; |
| 155 | default: |
| 156 | return false; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 157 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 158 | } |
| 159 | EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge); |
| 160 | |
Ming Lei | 9bddeb2 | 2017-05-26 19:53:20 +0800 | [diff] [blame] | 161 | /* |
| 162 | * Reverse check our software queue for entries that we could potentially |
| 163 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 164 | * too much time checking for merges. |
| 165 | */ |
| 166 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 167 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 168 | { |
| 169 | struct request *rq; |
| 170 | int checked = 8; |
| 171 | |
| 172 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 173 | bool merged = false; |
| 174 | |
| 175 | if (!checked--) |
| 176 | break; |
| 177 | |
| 178 | if (!blk_rq_merge_ok(rq, bio)) |
| 179 | continue; |
| 180 | |
| 181 | switch (blk_try_merge(rq, bio)) { |
| 182 | case ELEVATOR_BACK_MERGE: |
| 183 | if (blk_mq_sched_allow_merge(q, rq, bio)) |
| 184 | merged = bio_attempt_back_merge(q, rq, bio); |
| 185 | break; |
| 186 | case ELEVATOR_FRONT_MERGE: |
| 187 | if (blk_mq_sched_allow_merge(q, rq, bio)) |
| 188 | merged = bio_attempt_front_merge(q, rq, bio); |
| 189 | break; |
| 190 | case ELEVATOR_DISCARD_MERGE: |
| 191 | merged = bio_attempt_discard_merge(q, rq, bio); |
| 192 | break; |
| 193 | default: |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | if (merged) |
| 198 | ctx->rq_merged++; |
| 199 | return merged; |
| 200 | } |
| 201 | |
| 202 | return false; |
| 203 | } |
| 204 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 205 | bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio) |
| 206 | { |
| 207 | struct elevator_queue *e = q->elevator; |
Ming Lei | 9bddeb2 | 2017-05-26 19:53:20 +0800 | [diff] [blame] | 208 | struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); |
| 209 | struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu); |
| 210 | bool ret = false; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 211 | |
Ming Lei | 9bddeb2 | 2017-05-26 19:53:20 +0800 | [diff] [blame] | 212 | if (e && e->type->ops.mq.bio_merge) { |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 213 | blk_mq_put_ctx(ctx); |
| 214 | return e->type->ops.mq.bio_merge(hctx, bio); |
| 215 | } |
| 216 | |
Ming Lei | 9bddeb2 | 2017-05-26 19:53:20 +0800 | [diff] [blame] | 217 | if (hctx->flags & BLK_MQ_F_SHOULD_MERGE) { |
| 218 | /* default per sw-queue merge */ |
| 219 | spin_lock(&ctx->lock); |
| 220 | ret = blk_mq_attempt_merge(q, ctx, bio); |
| 221 | spin_unlock(&ctx->lock); |
| 222 | } |
| 223 | |
| 224 | blk_mq_put_ctx(ctx); |
| 225 | return ret; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq) |
| 229 | { |
| 230 | return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq); |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge); |
| 233 | |
| 234 | void blk_mq_sched_request_inserted(struct request *rq) |
| 235 | { |
| 236 | trace_block_rq_insert(rq->q, rq); |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted); |
| 239 | |
Omar Sandoval | 0cacba6 | 2017-02-02 15:42:39 -0800 | [diff] [blame] | 240 | static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx, |
| 241 | struct request *rq) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 242 | { |
| 243 | if (rq->tag == -1) { |
| 244 | rq->rq_flags |= RQF_SORTED; |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * If we already have a real request tag, send directly to |
| 250 | * the dispatch list. |
| 251 | */ |
| 252 | spin_lock(&hctx->lock); |
| 253 | list_add(&rq->queuelist, &hctx->dispatch); |
| 254 | spin_unlock(&hctx->lock); |
| 255 | return true; |
| 256 | } |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 257 | |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 258 | static bool blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx *hctx) |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 259 | { |
| 260 | if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) { |
| 261 | clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 262 | if (blk_mq_hctx_has_pending(hctx)) { |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 263 | blk_mq_run_hw_queue(hctx, true); |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 264 | return true; |
| 265 | } |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 266 | } |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 267 | return false; |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 270 | /** |
| 271 | * list_for_each_entry_rcu_rr - iterate in a round-robin fashion over rcu list |
| 272 | * @pos: loop cursor. |
| 273 | * @skip: the list element that will not be examined. Iteration starts at |
| 274 | * @skip->next. |
| 275 | * @head: head of the list to examine. This list must have at least one |
| 276 | * element, namely @skip. |
| 277 | * @member: name of the list_head structure within typeof(*pos). |
| 278 | */ |
| 279 | #define list_for_each_entry_rcu_rr(pos, skip, head, member) \ |
| 280 | for ((pos) = (skip); \ |
| 281 | (pos = (pos)->member.next != (head) ? list_entry_rcu( \ |
| 282 | (pos)->member.next, typeof(*pos), member) : \ |
| 283 | list_entry_rcu((pos)->member.next->next, typeof(*pos), member)), \ |
| 284 | (pos) != (skip); ) |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 285 | |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 286 | /* |
| 287 | * Called after a driver tag has been freed to check whether a hctx needs to |
| 288 | * be restarted. Restarts @hctx if its tag set is not shared. Restarts hardware |
| 289 | * queues in a round-robin fashion if the tag set of @hctx is shared with other |
| 290 | * hardware queues. |
| 291 | */ |
| 292 | void blk_mq_sched_restart(struct blk_mq_hw_ctx *const hctx) |
| 293 | { |
| 294 | struct blk_mq_tags *const tags = hctx->tags; |
| 295 | struct blk_mq_tag_set *const set = hctx->queue->tag_set; |
| 296 | struct request_queue *const queue = hctx->queue, *q; |
| 297 | struct blk_mq_hw_ctx *hctx2; |
| 298 | unsigned int i, j; |
| 299 | |
| 300 | if (set->flags & BLK_MQ_F_TAG_SHARED) { |
| 301 | rcu_read_lock(); |
| 302 | list_for_each_entry_rcu_rr(q, queue, &set->tag_list, |
| 303 | tag_set_list) { |
| 304 | queue_for_each_hw_ctx(q, hctx2, i) |
| 305 | if (hctx2->tags == tags && |
| 306 | blk_mq_sched_restart_hctx(hctx2)) |
| 307 | goto done; |
Omar Sandoval | d38d351 | 2017-02-22 10:58:30 -0800 | [diff] [blame] | 308 | } |
Bart Van Assche | 6d8c6c0 | 2017-04-07 12:40:09 -0600 | [diff] [blame] | 309 | j = hctx->queue_num + 1; |
| 310 | for (i = 0; i < queue->nr_hw_queues; i++, j++) { |
| 311 | if (j == queue->nr_hw_queues) |
| 312 | j = 0; |
| 313 | hctx2 = queue->queue_hw_ctx[j]; |
| 314 | if (hctx2->tags == tags && |
| 315 | blk_mq_sched_restart_hctx(hctx2)) |
| 316 | break; |
| 317 | } |
| 318 | done: |
| 319 | rcu_read_unlock(); |
Omar Sandoval | d38d351 | 2017-02-22 10:58:30 -0800 | [diff] [blame] | 320 | } else { |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 321 | blk_mq_sched_restart_hctx(hctx); |
Jens Axboe | 50e1dab | 2017-01-26 14:42:34 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Jens Axboe | bd6737f | 2017-01-27 01:00:47 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Add flush/fua to the queue. If we fail getting a driver tag, then |
| 327 | * punt to the requeue list. Requeue will re-invoke us from a context |
| 328 | * that's safe to block from. |
| 329 | */ |
| 330 | static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx *hctx, |
| 331 | struct request *rq, bool can_block) |
| 332 | { |
| 333 | if (blk_mq_get_driver_tag(rq, &hctx, can_block)) { |
| 334 | blk_insert_flush(rq); |
| 335 | blk_mq_run_hw_queue(hctx, true); |
| 336 | } else |
Jens Axboe | c7a571b | 2017-02-17 11:37:14 -0700 | [diff] [blame] | 337 | blk_mq_add_to_requeue_list(rq, false, true); |
Jens Axboe | bd6737f | 2017-01-27 01:00:47 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void blk_mq_sched_insert_request(struct request *rq, bool at_head, |
| 341 | bool run_queue, bool async, bool can_block) |
| 342 | { |
| 343 | struct request_queue *q = rq->q; |
| 344 | struct elevator_queue *e = q->elevator; |
| 345 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 346 | struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu); |
| 347 | |
Jens Axboe | f3a8ab7 | 2017-01-27 09:08:23 -0700 | [diff] [blame] | 348 | if (rq->tag == -1 && op_is_flush(rq->cmd_flags)) { |
Jens Axboe | bd6737f | 2017-01-27 01:00:47 -0700 | [diff] [blame] | 349 | blk_mq_sched_insert_flush(hctx, rq, can_block); |
| 350 | return; |
| 351 | } |
| 352 | |
Omar Sandoval | 0cacba6 | 2017-02-02 15:42:39 -0800 | [diff] [blame] | 353 | if (e && blk_mq_sched_bypass_insert(hctx, rq)) |
| 354 | goto run; |
| 355 | |
Jens Axboe | bd6737f | 2017-01-27 01:00:47 -0700 | [diff] [blame] | 356 | if (e && e->type->ops.mq.insert_requests) { |
| 357 | LIST_HEAD(list); |
| 358 | |
| 359 | list_add(&rq->queuelist, &list); |
| 360 | e->type->ops.mq.insert_requests(hctx, &list, at_head); |
| 361 | } else { |
| 362 | spin_lock(&ctx->lock); |
| 363 | __blk_mq_insert_request(hctx, rq, at_head); |
| 364 | spin_unlock(&ctx->lock); |
| 365 | } |
| 366 | |
Omar Sandoval | 0cacba6 | 2017-02-02 15:42:39 -0800 | [diff] [blame] | 367 | run: |
Jens Axboe | bd6737f | 2017-01-27 01:00:47 -0700 | [diff] [blame] | 368 | if (run_queue) |
| 369 | blk_mq_run_hw_queue(hctx, async); |
| 370 | } |
| 371 | |
| 372 | void blk_mq_sched_insert_requests(struct request_queue *q, |
| 373 | struct blk_mq_ctx *ctx, |
| 374 | struct list_head *list, bool run_queue_async) |
| 375 | { |
| 376 | struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu); |
| 377 | struct elevator_queue *e = hctx->queue->elevator; |
| 378 | |
Omar Sandoval | 0cacba6 | 2017-02-02 15:42:39 -0800 | [diff] [blame] | 379 | if (e) { |
| 380 | struct request *rq, *next; |
| 381 | |
| 382 | /* |
| 383 | * We bypass requests that already have a driver tag assigned, |
| 384 | * which should only be flushes. Flushes are only ever inserted |
| 385 | * as single requests, so we shouldn't ever hit the |
| 386 | * WARN_ON_ONCE() below (but let's handle it just in case). |
| 387 | */ |
| 388 | list_for_each_entry_safe(rq, next, list, queuelist) { |
| 389 | if (WARN_ON_ONCE(rq->tag != -1)) { |
| 390 | list_del_init(&rq->queuelist); |
| 391 | blk_mq_sched_bypass_insert(hctx, rq); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Jens Axboe | bd6737f | 2017-01-27 01:00:47 -0700 | [diff] [blame] | 396 | if (e && e->type->ops.mq.insert_requests) |
| 397 | e->type->ops.mq.insert_requests(hctx, list, false); |
| 398 | else |
| 399 | blk_mq_insert_requests(hctx, ctx, list); |
| 400 | |
| 401 | blk_mq_run_hw_queue(hctx, run_queue_async); |
| 402 | } |
| 403 | |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 404 | static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set, |
| 405 | struct blk_mq_hw_ctx *hctx, |
| 406 | unsigned int hctx_idx) |
| 407 | { |
| 408 | if (hctx->sched_tags) { |
| 409 | blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx); |
| 410 | blk_mq_free_rq_map(hctx->sched_tags); |
| 411 | hctx->sched_tags = NULL; |
| 412 | } |
| 413 | } |
| 414 | |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 415 | static int blk_mq_sched_alloc_tags(struct request_queue *q, |
| 416 | struct blk_mq_hw_ctx *hctx, |
| 417 | unsigned int hctx_idx) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 418 | { |
| 419 | struct blk_mq_tag_set *set = q->tag_set; |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 420 | int ret; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 421 | |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 422 | hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests, |
| 423 | set->reserved_tags); |
| 424 | if (!hctx->sched_tags) |
| 425 | return -ENOMEM; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 426 | |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 427 | ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests); |
| 428 | if (ret) |
| 429 | blk_mq_sched_free_tags(set, hctx, hctx_idx); |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 430 | |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 431 | return ret; |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 434 | static void blk_mq_sched_tags_teardown(struct request_queue *q) |
Jens Axboe | bd166ef | 2017-01-17 06:03:22 -0700 | [diff] [blame] | 435 | { |
| 436 | struct blk_mq_tag_set *set = q->tag_set; |
| 437 | struct blk_mq_hw_ctx *hctx; |
| 438 | int i; |
| 439 | |
| 440 | queue_for_each_hw_ctx(q, hctx, i) |
| 441 | blk_mq_sched_free_tags(set, hctx, i); |
| 442 | } |
Jens Axboe | d348499 | 2017-01-13 14:43:58 -0700 | [diff] [blame] | 443 | |
Omar Sandoval | 9325263 | 2017-04-05 12:01:31 -0700 | [diff] [blame] | 444 | int blk_mq_sched_init_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx, |
| 445 | unsigned int hctx_idx) |
| 446 | { |
| 447 | struct elevator_queue *e = q->elevator; |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 448 | int ret; |
Omar Sandoval | 9325263 | 2017-04-05 12:01:31 -0700 | [diff] [blame] | 449 | |
| 450 | if (!e) |
| 451 | return 0; |
| 452 | |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 453 | ret = blk_mq_sched_alloc_tags(q, hctx, hctx_idx); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | |
| 457 | if (e->type->ops.mq.init_hctx) { |
| 458 | ret = e->type->ops.mq.init_hctx(hctx, hctx_idx); |
| 459 | if (ret) { |
| 460 | blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx); |
| 461 | return ret; |
| 462 | } |
| 463 | } |
| 464 | |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 465 | blk_mq_debugfs_register_sched_hctx(q, hctx); |
| 466 | |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 467 | return 0; |
Omar Sandoval | 9325263 | 2017-04-05 12:01:31 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void blk_mq_sched_exit_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx, |
| 471 | unsigned int hctx_idx) |
| 472 | { |
| 473 | struct elevator_queue *e = q->elevator; |
| 474 | |
| 475 | if (!e) |
| 476 | return; |
| 477 | |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 478 | blk_mq_debugfs_unregister_sched_hctx(hctx); |
| 479 | |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 480 | if (e->type->ops.mq.exit_hctx && hctx->sched_data) { |
| 481 | e->type->ops.mq.exit_hctx(hctx, hctx_idx); |
| 482 | hctx->sched_data = NULL; |
| 483 | } |
| 484 | |
Omar Sandoval | 9325263 | 2017-04-05 12:01:31 -0700 | [diff] [blame] | 485 | blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx); |
| 486 | } |
| 487 | |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 488 | int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e) |
| 489 | { |
| 490 | struct blk_mq_hw_ctx *hctx; |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 491 | struct elevator_queue *eq; |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 492 | unsigned int i; |
| 493 | int ret; |
| 494 | |
| 495 | if (!e) { |
| 496 | q->elevator = NULL; |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Default to 256, since we don't split into sync/async like the |
| 502 | * old code did. Additionally, this is a per-hw queue depth. |
| 503 | */ |
| 504 | q->nr_requests = 2 * BLKDEV_MAX_RQ; |
| 505 | |
| 506 | queue_for_each_hw_ctx(q, hctx, i) { |
| 507 | ret = blk_mq_sched_alloc_tags(q, hctx, i); |
| 508 | if (ret) |
| 509 | goto err; |
| 510 | } |
| 511 | |
| 512 | ret = e->ops.mq.init_sched(q, e); |
| 513 | if (ret) |
| 514 | goto err; |
| 515 | |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 516 | blk_mq_debugfs_register_sched(q); |
| 517 | |
| 518 | queue_for_each_hw_ctx(q, hctx, i) { |
| 519 | if (e->ops.mq.init_hctx) { |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 520 | ret = e->ops.mq.init_hctx(hctx, i); |
| 521 | if (ret) { |
| 522 | eq = q->elevator; |
| 523 | blk_mq_exit_sched(q, eq); |
| 524 | kobject_put(&eq->kobj); |
| 525 | return ret; |
| 526 | } |
| 527 | } |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 528 | blk_mq_debugfs_register_sched_hctx(q, hctx); |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 531 | return 0; |
| 532 | |
| 533 | err: |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 534 | blk_mq_sched_tags_teardown(q); |
| 535 | q->elevator = NULL; |
Omar Sandoval | 6917ff0 | 2017-04-05 12:01:30 -0700 | [diff] [blame] | 536 | return ret; |
| 537 | } |
| 538 | |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 539 | void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e) |
| 540 | { |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 541 | struct blk_mq_hw_ctx *hctx; |
| 542 | unsigned int i; |
| 543 | |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 544 | queue_for_each_hw_ctx(q, hctx, i) { |
| 545 | blk_mq_debugfs_unregister_sched_hctx(hctx); |
| 546 | if (e->type->ops.mq.exit_hctx && hctx->sched_data) { |
| 547 | e->type->ops.mq.exit_hctx(hctx, i); |
| 548 | hctx->sched_data = NULL; |
Omar Sandoval | ee056f9 | 2017-04-05 12:01:34 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
Omar Sandoval | d332ce0 | 2017-05-04 08:24:40 -0600 | [diff] [blame] | 551 | blk_mq_debugfs_unregister_sched(q); |
Omar Sandoval | 54d5329 | 2017-04-07 08:52:27 -0600 | [diff] [blame] | 552 | if (e->type->ops.mq.exit_sched) |
| 553 | e->type->ops.mq.exit_sched(e); |
| 554 | blk_mq_sched_tags_teardown(q); |
| 555 | q->elevator = NULL; |
| 556 | } |
| 557 | |
Jens Axboe | d348499 | 2017-01-13 14:43:58 -0700 | [diff] [blame] | 558 | int blk_mq_sched_init(struct request_queue *q) |
| 559 | { |
| 560 | int ret; |
| 561 | |
Jens Axboe | d348499 | 2017-01-13 14:43:58 -0700 | [diff] [blame] | 562 | mutex_lock(&q->sysfs_lock); |
| 563 | ret = elevator_init(q, NULL); |
| 564 | mutex_unlock(&q->sysfs_lock); |
| 565 | |
| 566 | return ret; |
| 567 | } |