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