Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/backing-dev.h> |
| 4 | #include <linux/bio.h> |
| 5 | #include <linux/blkdev.h> |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/workqueue.h> |
| 10 | #include <linux/smp.h> |
| 11 | #include <linux/llist.h> |
| 12 | #include <linux/list_sort.h> |
| 13 | #include <linux/cpu.h> |
| 14 | #include <linux/cache.h> |
| 15 | #include <linux/sched/sysctl.h> |
| 16 | #include <linux/delay.h> |
| 17 | |
| 18 | #include <trace/events/block.h> |
| 19 | |
| 20 | #include <linux/blk-mq.h> |
| 21 | #include "blk.h" |
| 22 | #include "blk-mq.h" |
| 23 | #include "blk-mq-tag.h" |
| 24 | |
| 25 | static DEFINE_MUTEX(all_q_mutex); |
| 26 | static LIST_HEAD(all_q_list); |
| 27 | |
| 28 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx); |
| 29 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 30 | static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q, |
| 31 | unsigned int cpu) |
| 32 | { |
| 33 | return per_cpu_ptr(q->queue_ctx, cpu); |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * This assumes per-cpu software queueing queues. They could be per-node |
| 38 | * as well, for instance. For now this is hardcoded as-is. Note that we don't |
| 39 | * care about preemption, since we know the ctx's are persistent. This does |
| 40 | * mean that we can't rely on ctx always matching the currently running CPU. |
| 41 | */ |
| 42 | static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q) |
| 43 | { |
| 44 | return __blk_mq_get_ctx(q, get_cpu()); |
| 45 | } |
| 46 | |
| 47 | static void blk_mq_put_ctx(struct blk_mq_ctx *ctx) |
| 48 | { |
| 49 | put_cpu(); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Check if any of the ctx's have pending work in this hardware queue |
| 54 | */ |
| 55 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
| 56 | { |
| 57 | unsigned int i; |
| 58 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 59 | for (i = 0; i < hctx->ctx_map.map_size; i++) |
| 60 | if (hctx->ctx_map.map[i].word) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 61 | return true; |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 66 | static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx, |
| 67 | struct blk_mq_ctx *ctx) |
| 68 | { |
| 69 | return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word]; |
| 70 | } |
| 71 | |
| 72 | #define CTX_TO_BIT(hctx, ctx) \ |
| 73 | ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1)) |
| 74 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 75 | /* |
| 76 | * Mark this ctx as having pending work in this hardware queue |
| 77 | */ |
| 78 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, |
| 79 | struct blk_mq_ctx *ctx) |
| 80 | { |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 81 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); |
| 82 | |
| 83 | if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word)) |
| 84 | set_bit(CTX_TO_BIT(hctx, ctx), &bm->word); |
| 85 | } |
| 86 | |
| 87 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, |
| 88 | struct blk_mq_ctx *ctx) |
| 89 | { |
| 90 | struct blk_align_bitmap *bm = get_bm(hctx, ctx); |
| 91 | |
| 92 | clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Christoph Hellwig | 081241e | 2014-02-20 15:32:36 -0800 | [diff] [blame] | 95 | static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx, |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 96 | struct blk_mq_ctx *ctx, |
Christoph Hellwig | 081241e | 2014-02-20 15:32:36 -0800 | [diff] [blame] | 97 | gfp_t gfp, bool reserved) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 98 | { |
| 99 | struct request *rq; |
| 100 | unsigned int tag; |
| 101 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 102 | tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 103 | if (tag != BLK_MQ_TAG_FAIL) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 104 | rq = hctx->tags->rqs[tag]; |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 105 | |
| 106 | rq->cmd_flags = 0; |
| 107 | if (blk_mq_tag_busy(hctx)) { |
| 108 | rq->cmd_flags = REQ_MQ_INFLIGHT; |
| 109 | atomic_inc(&hctx->nr_active); |
| 110 | } |
| 111 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 112 | rq->tag = tag; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 113 | return rq; |
| 114 | } |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | static int blk_mq_queue_enter(struct request_queue *q) |
| 120 | { |
| 121 | int ret; |
| 122 | |
| 123 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
| 124 | smp_wmb(); |
| 125 | /* we have problems to freeze the queue if it's initializing */ |
| 126 | if (!blk_queue_bypass(q) || !blk_queue_init_done(q)) |
| 127 | return 0; |
| 128 | |
| 129 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 130 | |
| 131 | spin_lock_irq(q->queue_lock); |
| 132 | ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq, |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 133 | !blk_queue_bypass(q) || blk_queue_dying(q), |
| 134 | *q->queue_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 135 | /* inc usage with lock hold to avoid freeze_queue runs here */ |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 136 | if (!ret && !blk_queue_dying(q)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 137 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 138 | else if (blk_queue_dying(q)) |
| 139 | ret = -ENODEV; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 140 | spin_unlock_irq(q->queue_lock); |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static void blk_mq_queue_exit(struct request_queue *q) |
| 146 | { |
| 147 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 148 | } |
| 149 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 150 | static void __blk_mq_drain_queue(struct request_queue *q) |
| 151 | { |
| 152 | while (true) { |
| 153 | s64 count; |
| 154 | |
| 155 | spin_lock_irq(q->queue_lock); |
| 156 | count = percpu_counter_sum(&q->mq_usage_counter); |
| 157 | spin_unlock_irq(q->queue_lock); |
| 158 | |
| 159 | if (count == 0) |
| 160 | break; |
| 161 | blk_mq_run_queues(q, false); |
| 162 | msleep(10); |
| 163 | } |
| 164 | } |
| 165 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 166 | /* |
| 167 | * Guarantee no request is in use, so we can change any data structure of |
| 168 | * the queue afterward. |
| 169 | */ |
| 170 | static void blk_mq_freeze_queue(struct request_queue *q) |
| 171 | { |
| 172 | bool drain; |
| 173 | |
| 174 | spin_lock_irq(q->queue_lock); |
| 175 | drain = !q->bypass_depth++; |
| 176 | queue_flag_set(QUEUE_FLAG_BYPASS, q); |
| 177 | spin_unlock_irq(q->queue_lock); |
| 178 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 179 | if (drain) |
| 180 | __blk_mq_drain_queue(q); |
| 181 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 182 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 183 | void blk_mq_drain_queue(struct request_queue *q) |
| 184 | { |
| 185 | __blk_mq_drain_queue(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void blk_mq_unfreeze_queue(struct request_queue *q) |
| 189 | { |
| 190 | bool wake = false; |
| 191 | |
| 192 | spin_lock_irq(q->queue_lock); |
| 193 | if (!--q->bypass_depth) { |
| 194 | queue_flag_clear(QUEUE_FLAG_BYPASS, q); |
| 195 | wake = true; |
| 196 | } |
| 197 | WARN_ON_ONCE(q->bypass_depth < 0); |
| 198 | spin_unlock_irq(q->queue_lock); |
| 199 | if (wake) |
| 200 | wake_up_all(&q->mq_freeze_wq); |
| 201 | } |
| 202 | |
| 203 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) |
| 204 | { |
| 205 | return blk_mq_has_free_tags(hctx->tags); |
| 206 | } |
| 207 | EXPORT_SYMBOL(blk_mq_can_queue); |
| 208 | |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 209 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
| 210 | struct request *rq, unsigned int rw_flags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 211 | { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 212 | if (blk_queue_io_stat(q)) |
| 213 | rw_flags |= REQ_IO_STAT; |
| 214 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 215 | INIT_LIST_HEAD(&rq->queuelist); |
| 216 | /* csd/requeue_work/fifo_time is initialized before use */ |
| 217 | rq->q = q; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 218 | rq->mq_ctx = ctx; |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 219 | rq->cmd_flags |= rw_flags; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 220 | rq->cmd_type = 0; |
| 221 | /* do not touch atomic flags, it needs atomic ops against the timer */ |
| 222 | rq->cpu = -1; |
| 223 | rq->__data_len = 0; |
| 224 | rq->__sector = (sector_t) -1; |
| 225 | rq->bio = NULL; |
| 226 | rq->biotail = NULL; |
| 227 | INIT_HLIST_NODE(&rq->hash); |
| 228 | RB_CLEAR_NODE(&rq->rb_node); |
| 229 | memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv))); |
| 230 | rq->rq_disk = NULL; |
| 231 | rq->part = NULL; |
Ming Lei | 0fec08b | 2014-01-03 10:00:08 -0700 | [diff] [blame] | 232 | rq->start_time = jiffies; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 233 | #ifdef CONFIG_BLK_CGROUP |
| 234 | rq->rl = NULL; |
Ming Lei | 0fec08b | 2014-01-03 10:00:08 -0700 | [diff] [blame] | 235 | set_start_time_ns(rq); |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 236 | rq->io_start_time_ns = 0; |
| 237 | #endif |
| 238 | rq->nr_phys_segments = 0; |
| 239 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 240 | rq->nr_integrity_segments = 0; |
| 241 | #endif |
| 242 | rq->ioprio = 0; |
| 243 | rq->special = NULL; |
| 244 | /* tag was already set */ |
| 245 | rq->errors = 0; |
| 246 | memset(rq->__cmd, 0, sizeof(rq->__cmd)); |
| 247 | rq->cmd = rq->__cmd; |
| 248 | rq->cmd_len = BLK_MAX_CDB; |
| 249 | |
| 250 | rq->extra_len = 0; |
| 251 | rq->sense_len = 0; |
| 252 | rq->resid_len = 0; |
| 253 | rq->sense = NULL; |
| 254 | |
| 255 | rq->deadline = 0; |
| 256 | INIT_LIST_HEAD(&rq->timeout_list); |
| 257 | rq->timeout = 0; |
| 258 | rq->retries = 0; |
| 259 | rq->end_io = NULL; |
| 260 | rq->end_io_data = NULL; |
| 261 | rq->next_rq = NULL; |
| 262 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 263 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
| 264 | } |
| 265 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 266 | static struct request *blk_mq_alloc_request_pinned(struct request_queue *q, |
| 267 | int rw, gfp_t gfp, |
| 268 | bool reserved) |
| 269 | { |
| 270 | struct request *rq; |
| 271 | |
| 272 | do { |
| 273 | struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); |
| 274 | struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 275 | |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 276 | rq = __blk_mq_alloc_request(hctx, ctx, gfp & ~__GFP_WAIT, |
| 277 | reserved); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 278 | if (rq) { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 279 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 280 | break; |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 281 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 282 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 283 | if (gfp & __GFP_WAIT) { |
| 284 | __blk_mq_run_hw_queue(hctx); |
| 285 | blk_mq_put_ctx(ctx); |
| 286 | } else { |
| 287 | blk_mq_put_ctx(ctx); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 288 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 289 | } |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 290 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 291 | blk_mq_wait_for_tags(hctx, reserved); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 292 | } while (1); |
| 293 | |
| 294 | return rq; |
| 295 | } |
| 296 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 297 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 298 | { |
| 299 | struct request *rq; |
| 300 | |
| 301 | if (blk_mq_queue_enter(q)) |
| 302 | return NULL; |
| 303 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 304 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, false); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 305 | if (rq) |
| 306 | blk_mq_put_ctx(rq->mq_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 307 | return rq; |
| 308 | } |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 309 | EXPORT_SYMBOL(blk_mq_alloc_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 310 | |
| 311 | struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, |
| 312 | gfp_t gfp) |
| 313 | { |
| 314 | struct request *rq; |
| 315 | |
| 316 | if (blk_mq_queue_enter(q)) |
| 317 | return NULL; |
| 318 | |
| 319 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, true); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 320 | if (rq) |
| 321 | blk_mq_put_ctx(rq->mq_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 322 | return rq; |
| 323 | } |
| 324 | EXPORT_SYMBOL(blk_mq_alloc_reserved_request); |
| 325 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 326 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
| 327 | struct blk_mq_ctx *ctx, struct request *rq) |
| 328 | { |
| 329 | const int tag = rq->tag; |
| 330 | struct request_queue *q = rq->q; |
| 331 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 332 | if (rq->cmd_flags & REQ_MQ_INFLIGHT) |
| 333 | atomic_dec(&hctx->nr_active); |
| 334 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 335 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 336 | blk_mq_put_tag(hctx, tag, &ctx->last_tag); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 337 | blk_mq_queue_exit(q); |
| 338 | } |
| 339 | |
| 340 | void blk_mq_free_request(struct request *rq) |
| 341 | { |
| 342 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 343 | struct blk_mq_hw_ctx *hctx; |
| 344 | struct request_queue *q = rq->q; |
| 345 | |
| 346 | ctx->rq_completed[rq_is_sync(rq)]++; |
| 347 | |
| 348 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 349 | __blk_mq_free_request(hctx, ctx, rq); |
| 350 | } |
| 351 | |
Christoph Hellwig | 8727af4 | 2014-04-14 10:30:08 +0200 | [diff] [blame] | 352 | /* |
| 353 | * Clone all relevant state from a request that has been put on hold in |
| 354 | * the flush state machine into the preallocated flush request that hangs |
| 355 | * off the request queue. |
| 356 | * |
| 357 | * For a driver the flush request should be invisible, that's why we are |
| 358 | * impersonating the original request here. |
| 359 | */ |
| 360 | void blk_mq_clone_flush_request(struct request *flush_rq, |
| 361 | struct request *orig_rq) |
| 362 | { |
| 363 | struct blk_mq_hw_ctx *hctx = |
| 364 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); |
| 365 | |
| 366 | flush_rq->mq_ctx = orig_rq->mq_ctx; |
| 367 | flush_rq->tag = orig_rq->tag; |
| 368 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), |
| 369 | hctx->cmd_size); |
| 370 | } |
| 371 | |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 372 | inline void __blk_mq_end_io(struct request *rq, int error) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 373 | { |
Ming Lei | 0d11e6a | 2013-12-05 10:50:39 -0700 | [diff] [blame] | 374 | blk_account_io_done(rq); |
| 375 | |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 376 | if (rq->end_io) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 377 | rq->end_io(rq, error); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 378 | } else { |
| 379 | if (unlikely(blk_bidi_rq(rq))) |
| 380 | blk_mq_free_request(rq->next_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 381 | blk_mq_free_request(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 382 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 383 | } |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 384 | EXPORT_SYMBOL(__blk_mq_end_io); |
| 385 | |
| 386 | void blk_mq_end_io(struct request *rq, int error) |
| 387 | { |
| 388 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
| 389 | BUG(); |
| 390 | __blk_mq_end_io(rq, error); |
| 391 | } |
| 392 | EXPORT_SYMBOL(blk_mq_end_io); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 393 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 394 | static void __blk_mq_complete_request_remote(void *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 395 | { |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 396 | struct request *rq = data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 397 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 398 | rq->q->softirq_done_fn(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 401 | void __blk_mq_complete_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 402 | { |
| 403 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 404 | bool shared = false; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 405 | int cpu; |
| 406 | |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 407 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 408 | rq->q->softirq_done_fn(rq); |
| 409 | return; |
| 410 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 411 | |
| 412 | cpu = get_cpu(); |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 413 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
| 414 | shared = cpus_share_cache(cpu, ctx->cpu); |
| 415 | |
| 416 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 417 | rq->csd.func = __blk_mq_complete_request_remote; |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 418 | rq->csd.info = rq; |
| 419 | rq->csd.flags = 0; |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 420 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 421 | } else { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 422 | rq->q->softirq_done_fn(rq); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 423 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 424 | put_cpu(); |
| 425 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 426 | |
| 427 | /** |
| 428 | * blk_mq_complete_request - end I/O on a request |
| 429 | * @rq: the request being processed |
| 430 | * |
| 431 | * Description: |
| 432 | * Ends all I/O on a request. It does not handle partial completions. |
| 433 | * The actual completion happens out-of-order, through a IPI handler. |
| 434 | **/ |
| 435 | void blk_mq_complete_request(struct request *rq) |
| 436 | { |
Jens Axboe | 95f0968 | 2014-05-27 17:46:48 -0600 | [diff] [blame] | 437 | struct request_queue *q = rq->q; |
| 438 | |
| 439 | if (unlikely(blk_should_fake_timeout(q))) |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 440 | return; |
Jens Axboe | 95f0968 | 2014-05-27 17:46:48 -0600 | [diff] [blame] | 441 | if (!blk_mark_rq_complete(rq)) { |
| 442 | if (q->softirq_done_fn) |
| 443 | __blk_mq_complete_request(rq); |
| 444 | else |
| 445 | blk_mq_end_io(rq, rq->errors); |
| 446 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 447 | } |
| 448 | EXPORT_SYMBOL(blk_mq_complete_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 449 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 450 | static void blk_mq_start_request(struct request *rq, bool last) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 451 | { |
| 452 | struct request_queue *q = rq->q; |
| 453 | |
| 454 | trace_block_rq_issue(q, rq); |
| 455 | |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 456 | rq->resid_len = blk_rq_bytes(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 457 | if (unlikely(blk_bidi_rq(rq))) |
| 458 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 459 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 460 | /* |
| 461 | * Just mark start time and set the started bit. Due to memory |
| 462 | * ordering, we know we'll see the correct deadline as long as |
Jens Axboe | c22d9d8 | 2014-05-23 14:14:57 -0600 | [diff] [blame] | 463 | * REQ_ATOMIC_STARTED is seen. Use the default queue timeout, |
| 464 | * unless one has been set in the request. |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 465 | */ |
Jens Axboe | c22d9d8 | 2014-05-23 14:14:57 -0600 | [diff] [blame] | 466 | if (!rq->timeout) |
| 467 | rq->deadline = jiffies + q->rq_timeout; |
| 468 | else |
| 469 | rq->deadline = jiffies + rq->timeout; |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * Mark us as started and clear complete. Complete might have been |
| 473 | * set if requeue raced with timeout, which then marked it as |
| 474 | * complete. So be sure to clear complete again when we start |
| 475 | * the request, otherwise we'll ignore the completion event. |
| 476 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 477 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 478 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 479 | |
| 480 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
| 481 | /* |
| 482 | * Make sure space for the drain appears. We know we can do |
| 483 | * this because max_hw_segments has been adjusted to be one |
| 484 | * fewer than the device can handle. |
| 485 | */ |
| 486 | rq->nr_phys_segments++; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Flag the last request in the series so that drivers know when IO |
| 491 | * should be kicked off, if they don't do it on a per-request basis. |
| 492 | * |
| 493 | * Note: the flag isn't the only condition drivers should do kick off. |
| 494 | * If drive is busy, the last request might not have the bit set. |
| 495 | */ |
| 496 | if (last) |
| 497 | rq->cmd_flags |= REQ_END; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 500 | static void __blk_mq_requeue_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 501 | { |
| 502 | struct request_queue *q = rq->q; |
| 503 | |
| 504 | trace_block_rq_requeue(q, rq); |
| 505 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 506 | |
| 507 | rq->cmd_flags &= ~REQ_END; |
| 508 | |
| 509 | if (q->dma_drain_size && blk_rq_bytes(rq)) |
| 510 | rq->nr_phys_segments--; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 513 | void blk_mq_requeue_request(struct request *rq) |
| 514 | { |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 515 | __blk_mq_requeue_request(rq); |
| 516 | blk_clear_rq_complete(rq); |
| 517 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 518 | BUG_ON(blk_queued_rq(rq)); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame^] | 519 | blk_mq_add_to_requeue_list(rq, true); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 520 | } |
| 521 | EXPORT_SYMBOL(blk_mq_requeue_request); |
| 522 | |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame^] | 523 | static void blk_mq_requeue_work(struct work_struct *work) |
| 524 | { |
| 525 | struct request_queue *q = |
| 526 | container_of(work, struct request_queue, requeue_work); |
| 527 | LIST_HEAD(rq_list); |
| 528 | struct request *rq, *next; |
| 529 | unsigned long flags; |
| 530 | |
| 531 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 532 | list_splice_init(&q->requeue_list, &rq_list); |
| 533 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 534 | |
| 535 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { |
| 536 | if (!(rq->cmd_flags & REQ_SOFTBARRIER)) |
| 537 | continue; |
| 538 | |
| 539 | rq->cmd_flags &= ~REQ_SOFTBARRIER; |
| 540 | list_del_init(&rq->queuelist); |
| 541 | blk_mq_insert_request(rq, true, false, false); |
| 542 | } |
| 543 | |
| 544 | while (!list_empty(&rq_list)) { |
| 545 | rq = list_entry(rq_list.next, struct request, queuelist); |
| 546 | list_del_init(&rq->queuelist); |
| 547 | blk_mq_insert_request(rq, false, false, false); |
| 548 | } |
| 549 | |
| 550 | blk_mq_run_queues(q, false); |
| 551 | } |
| 552 | |
| 553 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head) |
| 554 | { |
| 555 | struct request_queue *q = rq->q; |
| 556 | unsigned long flags; |
| 557 | |
| 558 | /* |
| 559 | * We abuse this flag that is otherwise used by the I/O scheduler to |
| 560 | * request head insertation from the workqueue. |
| 561 | */ |
| 562 | BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER); |
| 563 | |
| 564 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 565 | if (at_head) { |
| 566 | rq->cmd_flags |= REQ_SOFTBARRIER; |
| 567 | list_add(&rq->queuelist, &q->requeue_list); |
| 568 | } else { |
| 569 | list_add_tail(&rq->queuelist, &q->requeue_list); |
| 570 | } |
| 571 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 572 | } |
| 573 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); |
| 574 | |
| 575 | void blk_mq_kick_requeue_list(struct request_queue *q) |
| 576 | { |
| 577 | kblockd_schedule_work(&q->requeue_work); |
| 578 | } |
| 579 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); |
| 580 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 581 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
| 582 | { |
| 583 | return tags->rqs[tag]; |
| 584 | } |
| 585 | EXPORT_SYMBOL(blk_mq_tag_to_rq); |
| 586 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 587 | struct blk_mq_timeout_data { |
| 588 | struct blk_mq_hw_ctx *hctx; |
| 589 | unsigned long *next; |
| 590 | unsigned int *next_set; |
| 591 | }; |
| 592 | |
| 593 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) |
| 594 | { |
| 595 | struct blk_mq_timeout_data *data = __data; |
| 596 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 597 | unsigned int tag; |
| 598 | |
| 599 | /* It may not be in flight yet (this is where |
| 600 | * the REQ_ATOMIC_STARTED flag comes in). The requests are |
| 601 | * statically allocated, so we know it's always safe to access the |
| 602 | * memory associated with a bit offset into ->rqs[]. |
| 603 | */ |
| 604 | tag = 0; |
| 605 | do { |
| 606 | struct request *rq; |
| 607 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 608 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
| 609 | if (tag >= hctx->tags->nr_tags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 610 | break; |
| 611 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 612 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
| 613 | if (rq->q != hctx->queue) |
| 614 | continue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 615 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 616 | continue; |
| 617 | |
| 618 | blk_rq_check_expired(rq, data->next, data->next_set); |
| 619 | } while (1); |
| 620 | } |
| 621 | |
| 622 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, |
| 623 | unsigned long *next, |
| 624 | unsigned int *next_set) |
| 625 | { |
| 626 | struct blk_mq_timeout_data data = { |
| 627 | .hctx = hctx, |
| 628 | .next = next, |
| 629 | .next_set = next_set, |
| 630 | }; |
| 631 | |
| 632 | /* |
| 633 | * Ask the tagging code to iterate busy requests, so we can |
| 634 | * check them for timeout. |
| 635 | */ |
| 636 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); |
| 637 | } |
| 638 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 639 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
| 640 | { |
| 641 | struct request_queue *q = rq->q; |
| 642 | |
| 643 | /* |
| 644 | * We know that complete is set at this point. If STARTED isn't set |
| 645 | * anymore, then the request isn't active and the "timeout" should |
| 646 | * just be ignored. This can happen due to the bitflag ordering. |
| 647 | * Timeout first checks if STARTED is set, and if it is, assumes |
| 648 | * the request is active. But if we race with completion, then |
| 649 | * we both flags will get cleared. So check here again, and ignore |
| 650 | * a timeout event with a request that isn't active. |
| 651 | */ |
| 652 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 653 | return BLK_EH_NOT_HANDLED; |
| 654 | |
| 655 | if (!q->mq_ops->timeout) |
| 656 | return BLK_EH_RESET_TIMER; |
| 657 | |
| 658 | return q->mq_ops->timeout(rq); |
| 659 | } |
| 660 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 661 | static void blk_mq_rq_timer(unsigned long data) |
| 662 | { |
| 663 | struct request_queue *q = (struct request_queue *) data; |
| 664 | struct blk_mq_hw_ctx *hctx; |
| 665 | unsigned long next = 0; |
| 666 | int i, next_set = 0; |
| 667 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 668 | queue_for_each_hw_ctx(q, hctx, i) { |
| 669 | /* |
| 670 | * If not software queues are currently mapped to this |
| 671 | * hardware queue, there's nothing to check |
| 672 | */ |
| 673 | if (!hctx->nr_ctx || !hctx->tags) |
| 674 | continue; |
| 675 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 676 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 677 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 678 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 679 | if (next_set) { |
| 680 | next = blk_rq_timeout(round_jiffies_up(next)); |
| 681 | mod_timer(&q->timeout, next); |
| 682 | } else { |
| 683 | queue_for_each_hw_ctx(q, hctx, i) |
| 684 | blk_mq_tag_idle(hctx); |
| 685 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Reverse check our software queue for entries that we could potentially |
| 690 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 691 | * too much time checking for merges. |
| 692 | */ |
| 693 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 694 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 695 | { |
| 696 | struct request *rq; |
| 697 | int checked = 8; |
| 698 | |
| 699 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 700 | int el_ret; |
| 701 | |
| 702 | if (!checked--) |
| 703 | break; |
| 704 | |
| 705 | if (!blk_rq_merge_ok(rq, bio)) |
| 706 | continue; |
| 707 | |
| 708 | el_ret = blk_try_merge(rq, bio); |
| 709 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 710 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 711 | ctx->rq_merged++; |
| 712 | return true; |
| 713 | } |
| 714 | break; |
| 715 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 716 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 717 | ctx->rq_merged++; |
| 718 | return true; |
| 719 | } |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return false; |
| 725 | } |
| 726 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 727 | /* |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 728 | * Process software queues that have been marked busy, splicing them |
| 729 | * to the for-dispatch |
| 730 | */ |
| 731 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
| 732 | { |
| 733 | struct blk_mq_ctx *ctx; |
| 734 | int i; |
| 735 | |
| 736 | for (i = 0; i < hctx->ctx_map.map_size; i++) { |
| 737 | struct blk_align_bitmap *bm = &hctx->ctx_map.map[i]; |
| 738 | unsigned int off, bit; |
| 739 | |
| 740 | if (!bm->word) |
| 741 | continue; |
| 742 | |
| 743 | bit = 0; |
| 744 | off = i * hctx->ctx_map.bits_per_word; |
| 745 | do { |
| 746 | bit = find_next_bit(&bm->word, bm->depth, bit); |
| 747 | if (bit >= bm->depth) |
| 748 | break; |
| 749 | |
| 750 | ctx = hctx->ctxs[bit + off]; |
| 751 | clear_bit(bit, &bm->word); |
| 752 | spin_lock(&ctx->lock); |
| 753 | list_splice_tail_init(&ctx->rq_list, list); |
| 754 | spin_unlock(&ctx->lock); |
| 755 | |
| 756 | bit++; |
| 757 | } while (1); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 762 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 763 | * Note that this function currently has various problems around ordering |
| 764 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 765 | * items on the hctx->dispatch list. Ignore that for now. |
| 766 | */ |
| 767 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 768 | { |
| 769 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 770 | struct request *rq; |
| 771 | LIST_HEAD(rq_list); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 772 | int queued; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 773 | |
Jens Axboe | fd1270d | 2014-04-16 09:23:48 -0600 | [diff] [blame] | 774 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 775 | |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 776 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 777 | return; |
| 778 | |
| 779 | hctx->run++; |
| 780 | |
| 781 | /* |
| 782 | * Touch any software queue that has pending entries. |
| 783 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 784 | flush_busy_ctxs(hctx, &rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * If we have previous entries on our dispatch list, grab them |
| 788 | * and stuff them at the front for more fair dispatch. |
| 789 | */ |
| 790 | if (!list_empty_careful(&hctx->dispatch)) { |
| 791 | spin_lock(&hctx->lock); |
| 792 | if (!list_empty(&hctx->dispatch)) |
| 793 | list_splice_init(&hctx->dispatch, &rq_list); |
| 794 | spin_unlock(&hctx->lock); |
| 795 | } |
| 796 | |
| 797 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 798 | * Now process all the entries, sending them to the driver. |
| 799 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 800 | queued = 0; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 801 | while (!list_empty(&rq_list)) { |
| 802 | int ret; |
| 803 | |
| 804 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 805 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 806 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 807 | blk_mq_start_request(rq, list_empty(&rq_list)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 808 | |
| 809 | ret = q->mq_ops->queue_rq(hctx, rq); |
| 810 | switch (ret) { |
| 811 | case BLK_MQ_RQ_QUEUE_OK: |
| 812 | queued++; |
| 813 | continue; |
| 814 | case BLK_MQ_RQ_QUEUE_BUSY: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 815 | list_add(&rq->queuelist, &rq_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 816 | __blk_mq_requeue_request(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 817 | break; |
| 818 | default: |
| 819 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 820 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 821 | rq->errors = -EIO; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 822 | blk_mq_end_io(rq, rq->errors); |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | if (!queued) |
| 831 | hctx->dispatched[0]++; |
| 832 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) |
| 833 | hctx->dispatched[ilog2(queued) + 1]++; |
| 834 | |
| 835 | /* |
| 836 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 837 | * that is where we will continue on next queue run. |
| 838 | */ |
| 839 | if (!list_empty(&rq_list)) { |
| 840 | spin_lock(&hctx->lock); |
| 841 | list_splice(&rq_list, &hctx->dispatch); |
| 842 | spin_unlock(&hctx->lock); |
| 843 | } |
| 844 | } |
| 845 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 846 | /* |
| 847 | * It'd be great if the workqueue API had a way to pass |
| 848 | * in a mask and had some smarts for more clever placement. |
| 849 | * For now we just round-robin here, switching for every |
| 850 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 851 | */ |
| 852 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 853 | { |
| 854 | int cpu = hctx->next_cpu; |
| 855 | |
| 856 | if (--hctx->next_cpu_batch <= 0) { |
| 857 | int next_cpu; |
| 858 | |
| 859 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); |
| 860 | if (next_cpu >= nr_cpu_ids) |
| 861 | next_cpu = cpumask_first(hctx->cpumask); |
| 862 | |
| 863 | hctx->next_cpu = next_cpu; |
| 864 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 865 | } |
| 866 | |
| 867 | return cpu; |
| 868 | } |
| 869 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 870 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 871 | { |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 872 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 873 | return; |
| 874 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 875 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 876 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 877 | else if (hctx->queue->nr_hw_queues == 1) |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 878 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 879 | else { |
| 880 | unsigned int cpu; |
| 881 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 882 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 883 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 884 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | void blk_mq_run_queues(struct request_queue *q, bool async) |
| 888 | { |
| 889 | struct blk_mq_hw_ctx *hctx; |
| 890 | int i; |
| 891 | |
| 892 | queue_for_each_hw_ctx(q, hctx, i) { |
| 893 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 894 | list_empty_careful(&hctx->dispatch)) || |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 895 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 896 | continue; |
| 897 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 898 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 899 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 900 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | EXPORT_SYMBOL(blk_mq_run_queues); |
| 904 | |
| 905 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 906 | { |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 907 | cancel_delayed_work(&hctx->run_work); |
| 908 | cancel_delayed_work(&hctx->delay_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 909 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 910 | } |
| 911 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 912 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 913 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 914 | { |
| 915 | struct blk_mq_hw_ctx *hctx; |
| 916 | int i; |
| 917 | |
| 918 | queue_for_each_hw_ctx(q, hctx, i) |
| 919 | blk_mq_stop_hw_queue(hctx); |
| 920 | } |
| 921 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 922 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 923 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 924 | { |
| 925 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 926 | |
| 927 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 928 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 929 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 930 | } |
| 931 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 932 | |
Christoph Hellwig | 2f26855 | 2014-04-16 09:44:56 +0200 | [diff] [blame] | 933 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 934 | { |
| 935 | struct blk_mq_hw_ctx *hctx; |
| 936 | int i; |
| 937 | |
| 938 | queue_for_each_hw_ctx(q, hctx, i) |
| 939 | blk_mq_start_hw_queue(hctx); |
| 940 | } |
| 941 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 942 | |
| 943 | |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 944 | void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 945 | { |
| 946 | struct blk_mq_hw_ctx *hctx; |
| 947 | int i; |
| 948 | |
| 949 | queue_for_each_hw_ctx(q, hctx, i) { |
| 950 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 951 | continue; |
| 952 | |
| 953 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 954 | preempt_disable(); |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 955 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 956 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 960 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 961 | static void blk_mq_run_work_fn(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 962 | { |
| 963 | struct blk_mq_hw_ctx *hctx; |
| 964 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 965 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 966 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 967 | __blk_mq_run_hw_queue(hctx); |
| 968 | } |
| 969 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 970 | static void blk_mq_delay_work_fn(struct work_struct *work) |
| 971 | { |
| 972 | struct blk_mq_hw_ctx *hctx; |
| 973 | |
| 974 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); |
| 975 | |
| 976 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 977 | __blk_mq_run_hw_queue(hctx); |
| 978 | } |
| 979 | |
| 980 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 981 | { |
| 982 | unsigned long tmo = msecs_to_jiffies(msecs); |
| 983 | |
| 984 | if (hctx->queue->nr_hw_queues == 1) |
| 985 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); |
| 986 | else { |
| 987 | unsigned int cpu; |
| 988 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 989 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 990 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
| 991 | } |
| 992 | } |
| 993 | EXPORT_SYMBOL(blk_mq_delay_queue); |
| 994 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 995 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 996 | struct request *rq, bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 997 | { |
| 998 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 999 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 1000 | trace_block_rq_insert(hctx->queue, rq); |
| 1001 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1002 | if (at_head) |
| 1003 | list_add(&rq->queuelist, &ctx->rq_list); |
| 1004 | else |
| 1005 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 1006 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1007 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1008 | |
| 1009 | /* |
| 1010 | * We do this early, to ensure we are on the right CPU. |
| 1011 | */ |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 1012 | blk_add_timer(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 1015 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
| 1016 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1017 | { |
| 1018 | struct request_queue *q = rq->q; |
| 1019 | struct blk_mq_hw_ctx *hctx; |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 1020 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1021 | |
| 1022 | current_ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 1023 | if (!cpu_online(ctx->cpu)) |
| 1024 | rq->mq_ctx = ctx = current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1025 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1026 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1027 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 1028 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
| 1029 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { |
| 1030 | blk_insert_flush(rq); |
| 1031 | } else { |
| 1032 | spin_lock(&ctx->lock); |
| 1033 | __blk_mq_insert_request(hctx, rq, at_head); |
| 1034 | spin_unlock(&ctx->lock); |
| 1035 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1036 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1037 | if (run_queue) |
| 1038 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1039 | |
| 1040 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | static void blk_mq_insert_requests(struct request_queue *q, |
| 1044 | struct blk_mq_ctx *ctx, |
| 1045 | struct list_head *list, |
| 1046 | int depth, |
| 1047 | bool from_schedule) |
| 1048 | |
| 1049 | { |
| 1050 | struct blk_mq_hw_ctx *hctx; |
| 1051 | struct blk_mq_ctx *current_ctx; |
| 1052 | |
| 1053 | trace_block_unplug(q, depth, !from_schedule); |
| 1054 | |
| 1055 | current_ctx = blk_mq_get_ctx(q); |
| 1056 | |
| 1057 | if (!cpu_online(ctx->cpu)) |
| 1058 | ctx = current_ctx; |
| 1059 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1060 | |
| 1061 | /* |
| 1062 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 1063 | * offline now |
| 1064 | */ |
| 1065 | spin_lock(&ctx->lock); |
| 1066 | while (!list_empty(list)) { |
| 1067 | struct request *rq; |
| 1068 | |
| 1069 | rq = list_first_entry(list, struct request, queuelist); |
| 1070 | list_del_init(&rq->queuelist); |
| 1071 | rq->mq_ctx = ctx; |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1072 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1073 | } |
| 1074 | spin_unlock(&ctx->lock); |
| 1075 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1076 | blk_mq_run_hw_queue(hctx, from_schedule); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1077 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1081 | { |
| 1082 | struct request *rqa = container_of(a, struct request, queuelist); |
| 1083 | struct request *rqb = container_of(b, struct request, queuelist); |
| 1084 | |
| 1085 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 1086 | (rqa->mq_ctx == rqb->mq_ctx && |
| 1087 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 1088 | } |
| 1089 | |
| 1090 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 1091 | { |
| 1092 | struct blk_mq_ctx *this_ctx; |
| 1093 | struct request_queue *this_q; |
| 1094 | struct request *rq; |
| 1095 | LIST_HEAD(list); |
| 1096 | LIST_HEAD(ctx_list); |
| 1097 | unsigned int depth; |
| 1098 | |
| 1099 | list_splice_init(&plug->mq_list, &list); |
| 1100 | |
| 1101 | list_sort(NULL, &list, plug_ctx_cmp); |
| 1102 | |
| 1103 | this_q = NULL; |
| 1104 | this_ctx = NULL; |
| 1105 | depth = 0; |
| 1106 | |
| 1107 | while (!list_empty(&list)) { |
| 1108 | rq = list_entry_rq(list.next); |
| 1109 | list_del_init(&rq->queuelist); |
| 1110 | BUG_ON(!rq->q); |
| 1111 | if (rq->mq_ctx != this_ctx) { |
| 1112 | if (this_ctx) { |
| 1113 | blk_mq_insert_requests(this_q, this_ctx, |
| 1114 | &ctx_list, depth, |
| 1115 | from_schedule); |
| 1116 | } |
| 1117 | |
| 1118 | this_ctx = rq->mq_ctx; |
| 1119 | this_q = rq->q; |
| 1120 | depth = 0; |
| 1121 | } |
| 1122 | |
| 1123 | depth++; |
| 1124 | list_add_tail(&rq->queuelist, &ctx_list); |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * If 'this_ctx' is set, we know we have entries to complete |
| 1129 | * on 'ctx_list'. Do those. |
| 1130 | */ |
| 1131 | if (this_ctx) { |
| 1132 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 1133 | from_schedule); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 1138 | { |
| 1139 | init_request_from_bio(rq, bio); |
| 1140 | blk_account_io_start(rq, 1); |
| 1141 | } |
| 1142 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1143 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, |
| 1144 | struct blk_mq_ctx *ctx, |
| 1145 | struct request *rq, struct bio *bio) |
| 1146 | { |
| 1147 | struct request_queue *q = hctx->queue; |
| 1148 | |
| 1149 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
| 1150 | blk_mq_bio_to_request(rq, bio); |
| 1151 | spin_lock(&ctx->lock); |
| 1152 | insert_rq: |
| 1153 | __blk_mq_insert_request(hctx, rq, false); |
| 1154 | spin_unlock(&ctx->lock); |
| 1155 | return false; |
| 1156 | } else { |
| 1157 | spin_lock(&ctx->lock); |
| 1158 | if (!blk_mq_attempt_merge(q, ctx, bio)) { |
| 1159 | blk_mq_bio_to_request(rq, bio); |
| 1160 | goto insert_rq; |
| 1161 | } |
| 1162 | |
| 1163 | spin_unlock(&ctx->lock); |
| 1164 | __blk_mq_free_request(hctx, ctx, rq); |
| 1165 | return true; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | struct blk_map_ctx { |
| 1170 | struct blk_mq_hw_ctx *hctx; |
| 1171 | struct blk_mq_ctx *ctx; |
| 1172 | }; |
| 1173 | |
| 1174 | static struct request *blk_mq_map_request(struct request_queue *q, |
| 1175 | struct bio *bio, |
| 1176 | struct blk_map_ctx *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1177 | { |
| 1178 | struct blk_mq_hw_ctx *hctx; |
| 1179 | struct blk_mq_ctx *ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1180 | struct request *rq; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1181 | int rw = bio_data_dir(bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1182 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1183 | if (unlikely(blk_mq_queue_enter(q))) { |
Nicholas Bellinger | 14ec77f | 2014-02-07 13:45:39 -0700 | [diff] [blame] | 1184 | bio_endio(bio, -EIO); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1185 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | ctx = blk_mq_get_ctx(q); |
| 1189 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1190 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1191 | if (rw_is_sync(bio->bi_rw)) |
Shaohua Li | 27fbf4e | 2014-02-19 20:20:21 +0800 | [diff] [blame] | 1192 | rw |= REQ_SYNC; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1193 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1194 | trace_block_getrq(q, bio, rw); |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 1195 | rq = __blk_mq_alloc_request(hctx, ctx, GFP_ATOMIC, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1196 | if (likely(rq)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1197 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1198 | else { |
| 1199 | blk_mq_put_ctx(ctx); |
| 1200 | trace_block_sleeprq(q, bio, rw); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1201 | rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC, |
| 1202 | false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1203 | ctx = rq->mq_ctx; |
| 1204 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1205 | } |
| 1206 | |
| 1207 | hctx->queued++; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1208 | data->hctx = hctx; |
| 1209 | data->ctx = ctx; |
| 1210 | return rq; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * Multiple hardware queue variant. This will not use per-process plugs, |
| 1215 | * but will attempt to bypass the hctx queueing if we can go straight to |
| 1216 | * hardware for SYNC IO. |
| 1217 | */ |
| 1218 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 1219 | { |
| 1220 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1221 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1222 | struct blk_map_ctx data; |
| 1223 | struct request *rq; |
| 1224 | |
| 1225 | blk_queue_bounce(q, &bio); |
| 1226 | |
| 1227 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1228 | bio_endio(bio, -EIO); |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | rq = blk_mq_map_request(q, bio, &data); |
| 1233 | if (unlikely(!rq)) |
| 1234 | return; |
| 1235 | |
| 1236 | if (unlikely(is_flush_fua)) { |
| 1237 | blk_mq_bio_to_request(rq, bio); |
| 1238 | blk_insert_flush(rq); |
| 1239 | goto run_queue; |
| 1240 | } |
| 1241 | |
| 1242 | if (is_sync) { |
| 1243 | int ret; |
| 1244 | |
| 1245 | blk_mq_bio_to_request(rq, bio); |
| 1246 | blk_mq_start_request(rq, true); |
| 1247 | |
| 1248 | /* |
| 1249 | * For OK queue, we are done. For error, kill it. Any other |
| 1250 | * error (busy), just add it to our list as we previously |
| 1251 | * would have done |
| 1252 | */ |
| 1253 | ret = q->mq_ops->queue_rq(data.hctx, rq); |
| 1254 | if (ret == BLK_MQ_RQ_QUEUE_OK) |
| 1255 | goto done; |
| 1256 | else { |
| 1257 | __blk_mq_requeue_request(rq); |
| 1258 | |
| 1259 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { |
| 1260 | rq->errors = -EIO; |
| 1261 | blk_mq_end_io(rq, rq->errors); |
| 1262 | goto done; |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1268 | /* |
| 1269 | * For a SYNC request, send it to the hardware immediately. For |
| 1270 | * an ASYNC request, just ensure that we run it later on. The |
| 1271 | * latter allows for merging opportunities and more efficient |
| 1272 | * dispatching. |
| 1273 | */ |
| 1274 | run_queue: |
| 1275 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
| 1276 | } |
| 1277 | done: |
| 1278 | blk_mq_put_ctx(data.ctx); |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * Single hardware queue variant. This will attempt to use any per-process |
| 1283 | * plug for merging and IO deferral. |
| 1284 | */ |
| 1285 | static void blk_sq_make_request(struct request_queue *q, struct bio *bio) |
| 1286 | { |
| 1287 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1288 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1289 | unsigned int use_plug, request_count = 0; |
| 1290 | struct blk_map_ctx data; |
| 1291 | struct request *rq; |
| 1292 | |
| 1293 | /* |
| 1294 | * If we have multiple hardware queues, just go directly to |
| 1295 | * one of those for sync IO. |
| 1296 | */ |
| 1297 | use_plug = !is_flush_fua && !is_sync; |
| 1298 | |
| 1299 | blk_queue_bounce(q, &bio); |
| 1300 | |
| 1301 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1302 | bio_endio(bio, -EIO); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | if (use_plug && !blk_queue_nomerges(q) && |
| 1307 | blk_attempt_plug_merge(q, bio, &request_count)) |
| 1308 | return; |
| 1309 | |
| 1310 | rq = blk_mq_map_request(q, bio, &data); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1311 | |
| 1312 | if (unlikely(is_flush_fua)) { |
| 1313 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1314 | blk_insert_flush(rq); |
| 1315 | goto run_queue; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * A task plug currently exists. Since this is completely lockless, |
| 1320 | * utilize that to temporarily store requests until the task is |
| 1321 | * either done or scheduled away. |
| 1322 | */ |
| 1323 | if (use_plug) { |
| 1324 | struct blk_plug *plug = current->plug; |
| 1325 | |
| 1326 | if (plug) { |
| 1327 | blk_mq_bio_to_request(rq, bio); |
Shaohua Li | 92f399c | 2013-10-29 12:01:03 -0600 | [diff] [blame] | 1328 | if (list_empty(&plug->mq_list)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1329 | trace_block_plug(q); |
| 1330 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { |
| 1331 | blk_flush_plug_list(plug, false); |
| 1332 | trace_block_plug(q); |
| 1333 | } |
| 1334 | list_add_tail(&rq->queuelist, &plug->mq_list); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1335 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1336 | return; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1340 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1341 | /* |
| 1342 | * For a SYNC request, send it to the hardware immediately. For |
| 1343 | * an ASYNC request, just ensure that we run it later on. The |
| 1344 | * latter allows for merging opportunities and more efficient |
| 1345 | * dispatching. |
| 1346 | */ |
| 1347 | run_queue: |
| 1348 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1351 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Default mapping to a software queue, since we use one per CPU. |
| 1356 | */ |
| 1357 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) |
| 1358 | { |
| 1359 | return q->queue_hw_ctx[q->mq_map[cpu]]; |
| 1360 | } |
| 1361 | EXPORT_SYMBOL(blk_mq_map_queue); |
| 1362 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1363 | struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set, |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1364 | unsigned int hctx_index, |
| 1365 | int node) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1366 | { |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1367 | return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL, node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1368 | } |
| 1369 | EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); |
| 1370 | |
| 1371 | void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx, |
| 1372 | unsigned int hctx_index) |
| 1373 | { |
| 1374 | kfree(hctx); |
| 1375 | } |
| 1376 | EXPORT_SYMBOL(blk_mq_free_single_hw_queue); |
| 1377 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1378 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
| 1379 | struct blk_mq_tags *tags, unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1380 | { |
| 1381 | struct page *page; |
| 1382 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1383 | if (tags->rqs && set->ops->exit_request) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1384 | int i; |
| 1385 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1386 | for (i = 0; i < tags->nr_tags; i++) { |
| 1387 | if (!tags->rqs[i]) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1388 | continue; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1389 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
| 1390 | hctx_idx, i); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1394 | while (!list_empty(&tags->page_list)) { |
| 1395 | page = list_first_entry(&tags->page_list, struct page, lru); |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1396 | list_del_init(&page->lru); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1397 | __free_pages(page, page->private); |
| 1398 | } |
| 1399 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1400 | kfree(tags->rqs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1401 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1402 | blk_mq_free_tags(tags); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | static size_t order_to_size(unsigned int order) |
| 1406 | { |
Ming Lei | 4ca0850 | 2014-04-19 18:00:18 +0800 | [diff] [blame] | 1407 | return (size_t)PAGE_SIZE << order; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1410 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
| 1411 | unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1412 | { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1413 | struct blk_mq_tags *tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1414 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1415 | size_t rq_size, left; |
| 1416 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1417 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
| 1418 | set->numa_node); |
| 1419 | if (!tags) |
| 1420 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1421 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1422 | INIT_LIST_HEAD(&tags->page_list); |
| 1423 | |
| 1424 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), |
| 1425 | GFP_KERNEL, set->numa_node); |
| 1426 | if (!tags->rqs) { |
| 1427 | blk_mq_free_tags(tags); |
| 1428 | return NULL; |
| 1429 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1430 | |
| 1431 | /* |
| 1432 | * rq_size is the size of the request plus driver payload, rounded |
| 1433 | * to the cacheline size |
| 1434 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1435 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1436 | cache_line_size()); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1437 | left = rq_size * set->queue_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1438 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1439 | for (i = 0; i < set->queue_depth; ) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1440 | int this_order = max_order; |
| 1441 | struct page *page; |
| 1442 | int to_do; |
| 1443 | void *p; |
| 1444 | |
| 1445 | while (left < order_to_size(this_order - 1) && this_order) |
| 1446 | this_order--; |
| 1447 | |
| 1448 | do { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1449 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
| 1450 | this_order); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1451 | if (page) |
| 1452 | break; |
| 1453 | if (!this_order--) |
| 1454 | break; |
| 1455 | if (order_to_size(this_order) < rq_size) |
| 1456 | break; |
| 1457 | } while (1); |
| 1458 | |
| 1459 | if (!page) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1460 | goto fail; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1461 | |
| 1462 | page->private = this_order; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1463 | list_add_tail(&page->lru, &tags->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1464 | |
| 1465 | p = page_address(page); |
| 1466 | entries_per_page = order_to_size(this_order) / rq_size; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1467 | to_do = min(entries_per_page, set->queue_depth - i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1468 | left -= to_do * rq_size; |
| 1469 | for (j = 0; j < to_do; j++) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1470 | tags->rqs[i] = p; |
| 1471 | if (set->ops->init_request) { |
| 1472 | if (set->ops->init_request(set->driver_data, |
| 1473 | tags->rqs[i], hctx_idx, i, |
| 1474 | set->numa_node)) |
| 1475 | goto fail; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1476 | } |
| 1477 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1478 | p += rq_size; |
| 1479 | i++; |
| 1480 | } |
| 1481 | } |
| 1482 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1483 | return tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1484 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1485 | fail: |
| 1486 | pr_warn("%s: failed to allocate requests\n", __func__); |
| 1487 | blk_mq_free_rq_map(set, tags, hctx_idx); |
| 1488 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1489 | } |
| 1490 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1491 | static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap) |
| 1492 | { |
| 1493 | kfree(bitmap->map); |
| 1494 | } |
| 1495 | |
| 1496 | static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node) |
| 1497 | { |
| 1498 | unsigned int bpw = 8, total, num_maps, i; |
| 1499 | |
| 1500 | bitmap->bits_per_word = bpw; |
| 1501 | |
| 1502 | num_maps = ALIGN(nr_cpu_ids, bpw) / bpw; |
| 1503 | bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap), |
| 1504 | GFP_KERNEL, node); |
| 1505 | if (!bitmap->map) |
| 1506 | return -ENOMEM; |
| 1507 | |
| 1508 | bitmap->map_size = num_maps; |
| 1509 | |
| 1510 | total = nr_cpu_ids; |
| 1511 | for (i = 0; i < num_maps; i++) { |
| 1512 | bitmap->map[i].depth = min(total, bitmap->bits_per_word); |
| 1513 | total -= bitmap->map[i].depth; |
| 1514 | } |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1519 | static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu) |
| 1520 | { |
| 1521 | struct request_queue *q = hctx->queue; |
| 1522 | struct blk_mq_ctx *ctx; |
| 1523 | LIST_HEAD(tmp); |
| 1524 | |
| 1525 | /* |
| 1526 | * Move ctx entries to new CPU, if this one is going away. |
| 1527 | */ |
| 1528 | ctx = __blk_mq_get_ctx(q, cpu); |
| 1529 | |
| 1530 | spin_lock(&ctx->lock); |
| 1531 | if (!list_empty(&ctx->rq_list)) { |
| 1532 | list_splice_init(&ctx->rq_list, &tmp); |
| 1533 | blk_mq_hctx_clear_pending(hctx, ctx); |
| 1534 | } |
| 1535 | spin_unlock(&ctx->lock); |
| 1536 | |
| 1537 | if (list_empty(&tmp)) |
| 1538 | return NOTIFY_OK; |
| 1539 | |
| 1540 | ctx = blk_mq_get_ctx(q); |
| 1541 | spin_lock(&ctx->lock); |
| 1542 | |
| 1543 | while (!list_empty(&tmp)) { |
| 1544 | struct request *rq; |
| 1545 | |
| 1546 | rq = list_first_entry(&tmp, struct request, queuelist); |
| 1547 | rq->mq_ctx = ctx; |
| 1548 | list_move_tail(&rq->queuelist, &ctx->rq_list); |
| 1549 | } |
| 1550 | |
| 1551 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1552 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1553 | |
| 1554 | spin_unlock(&ctx->lock); |
| 1555 | |
| 1556 | blk_mq_run_hw_queue(hctx, true); |
| 1557 | blk_mq_put_ctx(ctx); |
| 1558 | return NOTIFY_OK; |
| 1559 | } |
| 1560 | |
| 1561 | static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu) |
| 1562 | { |
| 1563 | struct request_queue *q = hctx->queue; |
| 1564 | struct blk_mq_tag_set *set = q->tag_set; |
| 1565 | |
| 1566 | if (set->tags[hctx->queue_num]) |
| 1567 | return NOTIFY_OK; |
| 1568 | |
| 1569 | set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num); |
| 1570 | if (!set->tags[hctx->queue_num]) |
| 1571 | return NOTIFY_STOP; |
| 1572 | |
| 1573 | hctx->tags = set->tags[hctx->queue_num]; |
| 1574 | return NOTIFY_OK; |
| 1575 | } |
| 1576 | |
| 1577 | static int blk_mq_hctx_notify(void *data, unsigned long action, |
| 1578 | unsigned int cpu) |
| 1579 | { |
| 1580 | struct blk_mq_hw_ctx *hctx = data; |
| 1581 | |
| 1582 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) |
| 1583 | return blk_mq_hctx_cpu_offline(hctx, cpu); |
| 1584 | else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) |
| 1585 | return blk_mq_hctx_cpu_online(hctx, cpu); |
| 1586 | |
| 1587 | return NOTIFY_OK; |
| 1588 | } |
| 1589 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1590 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
| 1591 | struct blk_mq_tag_set *set, int nr_queue) |
| 1592 | { |
| 1593 | struct blk_mq_hw_ctx *hctx; |
| 1594 | unsigned int i; |
| 1595 | |
| 1596 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1597 | if (i == nr_queue) |
| 1598 | break; |
| 1599 | |
| 1600 | if (set->ops->exit_hctx) |
| 1601 | set->ops->exit_hctx(hctx, i); |
| 1602 | |
| 1603 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
| 1604 | kfree(hctx->ctxs); |
| 1605 | blk_mq_free_bitmap(&hctx->ctx_map); |
| 1606 | } |
| 1607 | |
| 1608 | } |
| 1609 | |
| 1610 | static void blk_mq_free_hw_queues(struct request_queue *q, |
| 1611 | struct blk_mq_tag_set *set) |
| 1612 | { |
| 1613 | struct blk_mq_hw_ctx *hctx; |
| 1614 | unsigned int i; |
| 1615 | |
| 1616 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1617 | free_cpumask_var(hctx->cpumask); |
| 1618 | set->ops->free_hctx(hctx, i); |
| 1619 | } |
| 1620 | } |
| 1621 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1622 | static int blk_mq_init_hw_queues(struct request_queue *q, |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1623 | struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1624 | { |
| 1625 | struct blk_mq_hw_ctx *hctx; |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1626 | unsigned int i; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1627 | |
| 1628 | /* |
| 1629 | * Initialize hardware queues |
| 1630 | */ |
| 1631 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1632 | int node; |
| 1633 | |
| 1634 | node = hctx->numa_node; |
| 1635 | if (node == NUMA_NO_NODE) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1636 | node = hctx->numa_node = set->numa_node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1637 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1638 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
| 1639 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1640 | spin_lock_init(&hctx->lock); |
| 1641 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1642 | hctx->queue = q; |
| 1643 | hctx->queue_num = i; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1644 | hctx->flags = set->flags; |
| 1645 | hctx->cmd_size = set->cmd_size; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1646 | |
| 1647 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, |
| 1648 | blk_mq_hctx_notify, hctx); |
| 1649 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); |
| 1650 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1651 | hctx->tags = set->tags[i]; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* |
| 1654 | * Allocate space for all possible cpus to avoid allocation in |
| 1655 | * runtime |
| 1656 | */ |
| 1657 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1658 | GFP_KERNEL, node); |
| 1659 | if (!hctx->ctxs) |
| 1660 | break; |
| 1661 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1662 | if (blk_mq_alloc_bitmap(&hctx->ctx_map, node)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1663 | break; |
| 1664 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1665 | hctx->nr_ctx = 0; |
| 1666 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1667 | if (set->ops->init_hctx && |
| 1668 | set->ops->init_hctx(hctx, set->driver_data, i)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1669 | break; |
| 1670 | } |
| 1671 | |
| 1672 | if (i == q->nr_hw_queues) |
| 1673 | return 0; |
| 1674 | |
| 1675 | /* |
| 1676 | * Init failed |
| 1677 | */ |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1678 | blk_mq_exit_hw_queues(q, set, i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1679 | |
| 1680 | return 1; |
| 1681 | } |
| 1682 | |
| 1683 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1684 | unsigned int nr_hw_queues) |
| 1685 | { |
| 1686 | unsigned int i; |
| 1687 | |
| 1688 | for_each_possible_cpu(i) { |
| 1689 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1690 | struct blk_mq_hw_ctx *hctx; |
| 1691 | |
| 1692 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1693 | __ctx->cpu = i; |
| 1694 | spin_lock_init(&__ctx->lock); |
| 1695 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1696 | __ctx->queue = q; |
| 1697 | |
| 1698 | /* If the cpu isn't online, the cpu is mapped to first hctx */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1699 | if (!cpu_online(i)) |
| 1700 | continue; |
| 1701 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1702 | hctx = q->mq_ops->map_queue(q, i); |
| 1703 | cpumask_set_cpu(i, hctx->cpumask); |
| 1704 | hctx->nr_ctx++; |
| 1705 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1706 | /* |
| 1707 | * Set local node, IFF we have more than one hw queue. If |
| 1708 | * not, we remain on the home node of the device |
| 1709 | */ |
| 1710 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 1711 | hctx->numa_node = cpu_to_node(i); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 1716 | { |
| 1717 | unsigned int i; |
| 1718 | struct blk_mq_hw_ctx *hctx; |
| 1719 | struct blk_mq_ctx *ctx; |
| 1720 | |
| 1721 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1722 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1723 | hctx->nr_ctx = 0; |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * Map software to hardware queues |
| 1728 | */ |
| 1729 | queue_for_each_ctx(q, ctx, i) { |
| 1730 | /* If the cpu isn't online, the cpu is mapped to first hctx */ |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1731 | if (!cpu_online(i)) |
| 1732 | continue; |
| 1733 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1734 | hctx = q->mq_ops->map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1735 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1736 | ctx->index_hw = hctx->nr_ctx; |
| 1737 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1738 | } |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1739 | |
| 1740 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1741 | /* |
| 1742 | * If not software queues are mapped to this hardware queue, |
| 1743 | * disable it and free the request entries |
| 1744 | */ |
| 1745 | if (!hctx->nr_ctx) { |
| 1746 | struct blk_mq_tag_set *set = q->tag_set; |
| 1747 | |
| 1748 | if (set->tags[i]) { |
| 1749 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 1750 | set->tags[i] = NULL; |
| 1751 | hctx->tags = NULL; |
| 1752 | } |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
| 1756 | /* |
| 1757 | * Initialize batch roundrobin counts |
| 1758 | */ |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1759 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
| 1760 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 1761 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1764 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set) |
| 1765 | { |
| 1766 | struct blk_mq_hw_ctx *hctx; |
| 1767 | struct request_queue *q; |
| 1768 | bool shared; |
| 1769 | int i; |
| 1770 | |
| 1771 | if (set->tag_list.next == set->tag_list.prev) |
| 1772 | shared = false; |
| 1773 | else |
| 1774 | shared = true; |
| 1775 | |
| 1776 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 1777 | blk_mq_freeze_queue(q); |
| 1778 | |
| 1779 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1780 | if (shared) |
| 1781 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
| 1782 | else |
| 1783 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
| 1784 | } |
| 1785 | blk_mq_unfreeze_queue(q); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | static void blk_mq_del_queue_tag_set(struct request_queue *q) |
| 1790 | { |
| 1791 | struct blk_mq_tag_set *set = q->tag_set; |
| 1792 | |
| 1793 | blk_mq_freeze_queue(q); |
| 1794 | |
| 1795 | mutex_lock(&set->tag_list_lock); |
| 1796 | list_del_init(&q->tag_set_list); |
| 1797 | blk_mq_update_tag_set_depth(set); |
| 1798 | mutex_unlock(&set->tag_list_lock); |
| 1799 | |
| 1800 | blk_mq_unfreeze_queue(q); |
| 1801 | } |
| 1802 | |
| 1803 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, |
| 1804 | struct request_queue *q) |
| 1805 | { |
| 1806 | q->tag_set = set; |
| 1807 | |
| 1808 | mutex_lock(&set->tag_list_lock); |
| 1809 | list_add_tail(&q->tag_set_list, &set->tag_list); |
| 1810 | blk_mq_update_tag_set_depth(set); |
| 1811 | mutex_unlock(&set->tag_list_lock); |
| 1812 | } |
| 1813 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1814 | struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1815 | { |
| 1816 | struct blk_mq_hw_ctx **hctxs; |
| 1817 | struct blk_mq_ctx *ctx; |
| 1818 | struct request_queue *q; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1819 | unsigned int *map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1820 | int i; |
| 1821 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1822 | ctx = alloc_percpu(struct blk_mq_ctx); |
| 1823 | if (!ctx) |
| 1824 | return ERR_PTR(-ENOMEM); |
| 1825 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1826 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
| 1827 | set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1828 | |
| 1829 | if (!hctxs) |
| 1830 | goto err_percpu; |
| 1831 | |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1832 | map = blk_mq_make_queue_map(set); |
| 1833 | if (!map) |
| 1834 | goto err_map; |
| 1835 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1836 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1837 | int node = blk_mq_hw_queue_to_node(map, i); |
| 1838 | |
| 1839 | hctxs[i] = set->ops->alloc_hctx(set, i, node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1840 | if (!hctxs[i]) |
| 1841 | goto err_hctxs; |
| 1842 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1843 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
| 1844 | goto err_hctxs; |
| 1845 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1846 | atomic_set(&hctxs[i]->nr_active, 0); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1847 | hctxs[i]->numa_node = node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1848 | hctxs[i]->queue_num = i; |
| 1849 | } |
| 1850 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1851 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1852 | if (!q) |
| 1853 | goto err_hctxs; |
| 1854 | |
Ming Lei | 3d2936f | 2014-05-27 23:35:14 +0800 | [diff] [blame] | 1855 | if (percpu_counter_init(&q->mq_usage_counter, 0)) |
| 1856 | goto err_map; |
| 1857 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1858 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
| 1859 | blk_queue_rq_timeout(q, 30000); |
| 1860 | |
| 1861 | q->nr_queues = nr_cpu_ids; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1862 | q->nr_hw_queues = set->nr_hw_queues; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1863 | q->mq_map = map; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1864 | |
| 1865 | q->queue_ctx = ctx; |
| 1866 | q->queue_hw_ctx = hctxs; |
| 1867 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1868 | q->mq_ops = set->ops; |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 1869 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1870 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 1871 | q->sg_reserved_size = INT_MAX; |
| 1872 | |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame^] | 1873 | INIT_WORK(&q->requeue_work, blk_mq_requeue_work); |
| 1874 | INIT_LIST_HEAD(&q->requeue_list); |
| 1875 | spin_lock_init(&q->requeue_lock); |
| 1876 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1877 | if (q->nr_hw_queues > 1) |
| 1878 | blk_queue_make_request(q, blk_mq_make_request); |
| 1879 | else |
| 1880 | blk_queue_make_request(q, blk_sq_make_request); |
| 1881 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 1882 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1883 | if (set->timeout) |
| 1884 | blk_queue_rq_timeout(q, set->timeout); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1885 | |
Jens Axboe | eba7176 | 2014-05-20 15:17:27 -0600 | [diff] [blame] | 1886 | /* |
| 1887 | * Do this after blk_queue_make_request() overrides it... |
| 1888 | */ |
| 1889 | q->nr_requests = set->queue_depth; |
| 1890 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1891 | if (set->ops->complete) |
| 1892 | blk_queue_softirq_done(q, set->ops->complete); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 1893 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1894 | blk_mq_init_flush(q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1895 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1896 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1897 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
| 1898 | set->cmd_size, cache_line_size()), |
| 1899 | GFP_KERNEL); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1900 | if (!q->flush_rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1901 | goto err_hw; |
| 1902 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1903 | if (blk_mq_init_hw_queues(q, set)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1904 | goto err_flush_rq; |
| 1905 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1906 | mutex_lock(&all_q_mutex); |
| 1907 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1908 | mutex_unlock(&all_q_mutex); |
| 1909 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1910 | blk_mq_add_queue_tag_set(set, q); |
| 1911 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1912 | blk_mq_map_swqueue(q); |
| 1913 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1914 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1915 | |
| 1916 | err_flush_rq: |
| 1917 | kfree(q->flush_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1918 | err_hw: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1919 | blk_cleanup_queue(q); |
| 1920 | err_hctxs: |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1921 | kfree(map); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1922 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1923 | if (!hctxs[i]) |
| 1924 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1925 | free_cpumask_var(hctxs[i]->cpumask); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1926 | set->ops->free_hctx(hctxs[i], i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1927 | } |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1928 | err_map: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1929 | kfree(hctxs); |
| 1930 | err_percpu: |
| 1931 | free_percpu(ctx); |
| 1932 | return ERR_PTR(-ENOMEM); |
| 1933 | } |
| 1934 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1935 | |
| 1936 | void blk_mq_free_queue(struct request_queue *q) |
| 1937 | { |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1938 | struct blk_mq_tag_set *set = q->tag_set; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1939 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1940 | blk_mq_del_queue_tag_set(q); |
| 1941 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1942 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
| 1943 | blk_mq_free_hw_queues(q, set); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1944 | |
Ming Lei | 3d2936f | 2014-05-27 23:35:14 +0800 | [diff] [blame] | 1945 | percpu_counter_destroy(&q->mq_usage_counter); |
| 1946 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1947 | free_percpu(q->queue_ctx); |
| 1948 | kfree(q->queue_hw_ctx); |
| 1949 | kfree(q->mq_map); |
| 1950 | |
| 1951 | q->queue_ctx = NULL; |
| 1952 | q->queue_hw_ctx = NULL; |
| 1953 | q->mq_map = NULL; |
| 1954 | |
| 1955 | mutex_lock(&all_q_mutex); |
| 1956 | list_del_init(&q->all_q_node); |
| 1957 | mutex_unlock(&all_q_mutex); |
| 1958 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1959 | |
| 1960 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1961 | static void blk_mq_queue_reinit(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1962 | { |
| 1963 | blk_mq_freeze_queue(q); |
| 1964 | |
| 1965 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
| 1966 | |
| 1967 | /* |
| 1968 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 1969 | * we should change hctx numa_node according to new topology (this |
| 1970 | * involves free and re-allocate memory, worthy doing?) |
| 1971 | */ |
| 1972 | |
| 1973 | blk_mq_map_swqueue(q); |
| 1974 | |
| 1975 | blk_mq_unfreeze_queue(q); |
| 1976 | } |
| 1977 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1978 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
| 1979 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1980 | { |
| 1981 | struct request_queue *q; |
| 1982 | |
| 1983 | /* |
Jens Axboe | 9fccfed | 2014-05-08 14:50:19 -0600 | [diff] [blame] | 1984 | * Before new mappings are established, hotadded cpu might already |
| 1985 | * start handling requests. This doesn't break anything as we map |
| 1986 | * offline CPUs to first hardware queue. We will re-init the queue |
| 1987 | * below to get optimal settings. |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1988 | */ |
| 1989 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && |
| 1990 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) |
| 1991 | return NOTIFY_OK; |
| 1992 | |
| 1993 | mutex_lock(&all_q_mutex); |
| 1994 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1995 | blk_mq_queue_reinit(q); |
| 1996 | mutex_unlock(&all_q_mutex); |
| 1997 | return NOTIFY_OK; |
| 1998 | } |
| 1999 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2000 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 2001 | { |
| 2002 | int i; |
| 2003 | |
| 2004 | if (!set->nr_hw_queues) |
| 2005 | return -EINVAL; |
| 2006 | if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH) |
| 2007 | return -EINVAL; |
| 2008 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 2009 | return -EINVAL; |
| 2010 | |
| 2011 | if (!set->nr_hw_queues || |
| 2012 | !set->ops->queue_rq || !set->ops->map_queue || |
| 2013 | !set->ops->alloc_hctx || !set->ops->free_hctx) |
| 2014 | return -EINVAL; |
| 2015 | |
| 2016 | |
Ming Lei | 4847900 | 2014-04-19 18:00:17 +0800 | [diff] [blame] | 2017 | set->tags = kmalloc_node(set->nr_hw_queues * |
| 2018 | sizeof(struct blk_mq_tags *), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2019 | GFP_KERNEL, set->numa_node); |
| 2020 | if (!set->tags) |
| 2021 | goto out; |
| 2022 | |
| 2023 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2024 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 2025 | if (!set->tags[i]) |
| 2026 | goto out_unwind; |
| 2027 | } |
| 2028 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2029 | mutex_init(&set->tag_list_lock); |
| 2030 | INIT_LIST_HEAD(&set->tag_list); |
| 2031 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2032 | return 0; |
| 2033 | |
| 2034 | out_unwind: |
| 2035 | while (--i >= 0) |
| 2036 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2037 | out: |
| 2038 | return -ENOMEM; |
| 2039 | } |
| 2040 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 2041 | |
| 2042 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 2043 | { |
| 2044 | int i; |
| 2045 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 2046 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2047 | if (set->tags[i]) |
| 2048 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2049 | } |
| 2050 | |
Ming Lei | 981bd18 | 2014-04-24 00:07:34 +0800 | [diff] [blame] | 2051 | kfree(set->tags); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2052 | } |
| 2053 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 2054 | |
Jens Axboe | e3a2b3f | 2014-05-20 11:49:02 -0600 | [diff] [blame] | 2055 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
| 2056 | { |
| 2057 | struct blk_mq_tag_set *set = q->tag_set; |
| 2058 | struct blk_mq_hw_ctx *hctx; |
| 2059 | int i, ret; |
| 2060 | |
| 2061 | if (!set || nr > set->queue_depth) |
| 2062 | return -EINVAL; |
| 2063 | |
| 2064 | ret = 0; |
| 2065 | queue_for_each_hw_ctx(q, hctx, i) { |
| 2066 | ret = blk_mq_tag_update_depth(hctx->tags, nr); |
| 2067 | if (ret) |
| 2068 | break; |
| 2069 | } |
| 2070 | |
| 2071 | if (!ret) |
| 2072 | q->nr_requests = nr; |
| 2073 | |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 2077 | void blk_mq_disable_hotplug(void) |
| 2078 | { |
| 2079 | mutex_lock(&all_q_mutex); |
| 2080 | } |
| 2081 | |
| 2082 | void blk_mq_enable_hotplug(void) |
| 2083 | { |
| 2084 | mutex_unlock(&all_q_mutex); |
| 2085 | } |
| 2086 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2087 | static int __init blk_mq_init(void) |
| 2088 | { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2089 | blk_mq_cpu_init(); |
| 2090 | |
| 2091 | /* Must be called after percpu_counter_hotcpu_callback() */ |
| 2092 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); |
| 2093 | |
| 2094 | return 0; |
| 2095 | } |
| 2096 | subsys_initcall(blk_mq_init); |