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 | { |
| 437 | if (unlikely(blk_should_fake_timeout(rq->q))) |
| 438 | return; |
| 439 | if (!blk_mark_rq_complete(rq)) |
| 440 | __blk_mq_complete_request(rq); |
| 441 | } |
| 442 | EXPORT_SYMBOL(blk_mq_complete_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 443 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 444 | static void blk_mq_start_request(struct request *rq, bool last) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 445 | { |
| 446 | struct request_queue *q = rq->q; |
| 447 | |
| 448 | trace_block_rq_issue(q, rq); |
| 449 | |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 450 | rq->resid_len = blk_rq_bytes(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 451 | if (unlikely(blk_bidi_rq(rq))) |
| 452 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 453 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 454 | /* |
| 455 | * Just mark start time and set the started bit. Due to memory |
| 456 | * ordering, we know we'll see the correct deadline as long as |
| 457 | * REQ_ATOMIC_STARTED is seen. |
| 458 | */ |
| 459 | rq->deadline = jiffies + q->rq_timeout; |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 460 | |
| 461 | /* |
| 462 | * Mark us as started and clear complete. Complete might have been |
| 463 | * set if requeue raced with timeout, which then marked it as |
| 464 | * complete. So be sure to clear complete again when we start |
| 465 | * the request, otherwise we'll ignore the completion event. |
| 466 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 467 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 468 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 469 | |
| 470 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
| 471 | /* |
| 472 | * Make sure space for the drain appears. We know we can do |
| 473 | * this because max_hw_segments has been adjusted to be one |
| 474 | * fewer than the device can handle. |
| 475 | */ |
| 476 | rq->nr_phys_segments++; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Flag the last request in the series so that drivers know when IO |
| 481 | * should be kicked off, if they don't do it on a per-request basis. |
| 482 | * |
| 483 | * Note: the flag isn't the only condition drivers should do kick off. |
| 484 | * If drive is busy, the last request might not have the bit set. |
| 485 | */ |
| 486 | if (last) |
| 487 | rq->cmd_flags |= REQ_END; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 490 | static void __blk_mq_requeue_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 491 | { |
| 492 | struct request_queue *q = rq->q; |
| 493 | |
| 494 | trace_block_rq_requeue(q, rq); |
| 495 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 496 | |
| 497 | rq->cmd_flags &= ~REQ_END; |
| 498 | |
| 499 | if (q->dma_drain_size && blk_rq_bytes(rq)) |
| 500 | rq->nr_phys_segments--; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 503 | void blk_mq_requeue_request(struct request *rq) |
| 504 | { |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 505 | __blk_mq_requeue_request(rq); |
| 506 | blk_clear_rq_complete(rq); |
| 507 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 508 | BUG_ON(blk_queued_rq(rq)); |
| 509 | blk_mq_insert_request(rq, true, true, false); |
| 510 | } |
| 511 | EXPORT_SYMBOL(blk_mq_requeue_request); |
| 512 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 513 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
| 514 | { |
| 515 | return tags->rqs[tag]; |
| 516 | } |
| 517 | EXPORT_SYMBOL(blk_mq_tag_to_rq); |
| 518 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 519 | struct blk_mq_timeout_data { |
| 520 | struct blk_mq_hw_ctx *hctx; |
| 521 | unsigned long *next; |
| 522 | unsigned int *next_set; |
| 523 | }; |
| 524 | |
| 525 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) |
| 526 | { |
| 527 | struct blk_mq_timeout_data *data = __data; |
| 528 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 529 | unsigned int tag; |
| 530 | |
| 531 | /* It may not be in flight yet (this is where |
| 532 | * the REQ_ATOMIC_STARTED flag comes in). The requests are |
| 533 | * statically allocated, so we know it's always safe to access the |
| 534 | * memory associated with a bit offset into ->rqs[]. |
| 535 | */ |
| 536 | tag = 0; |
| 537 | do { |
| 538 | struct request *rq; |
| 539 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 540 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
| 541 | if (tag >= hctx->tags->nr_tags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 542 | break; |
| 543 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 544 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
| 545 | if (rq->q != hctx->queue) |
| 546 | continue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 547 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 548 | continue; |
| 549 | |
| 550 | blk_rq_check_expired(rq, data->next, data->next_set); |
| 551 | } while (1); |
| 552 | } |
| 553 | |
| 554 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, |
| 555 | unsigned long *next, |
| 556 | unsigned int *next_set) |
| 557 | { |
| 558 | struct blk_mq_timeout_data data = { |
| 559 | .hctx = hctx, |
| 560 | .next = next, |
| 561 | .next_set = next_set, |
| 562 | }; |
| 563 | |
| 564 | /* |
| 565 | * Ask the tagging code to iterate busy requests, so we can |
| 566 | * check them for timeout. |
| 567 | */ |
| 568 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); |
| 569 | } |
| 570 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 571 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
| 572 | { |
| 573 | struct request_queue *q = rq->q; |
| 574 | |
| 575 | /* |
| 576 | * We know that complete is set at this point. If STARTED isn't set |
| 577 | * anymore, then the request isn't active and the "timeout" should |
| 578 | * just be ignored. This can happen due to the bitflag ordering. |
| 579 | * Timeout first checks if STARTED is set, and if it is, assumes |
| 580 | * the request is active. But if we race with completion, then |
| 581 | * we both flags will get cleared. So check here again, and ignore |
| 582 | * a timeout event with a request that isn't active. |
| 583 | */ |
| 584 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 585 | return BLK_EH_NOT_HANDLED; |
| 586 | |
| 587 | if (!q->mq_ops->timeout) |
| 588 | return BLK_EH_RESET_TIMER; |
| 589 | |
| 590 | return q->mq_ops->timeout(rq); |
| 591 | } |
| 592 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 593 | static void blk_mq_rq_timer(unsigned long data) |
| 594 | { |
| 595 | struct request_queue *q = (struct request_queue *) data; |
| 596 | struct blk_mq_hw_ctx *hctx; |
| 597 | unsigned long next = 0; |
| 598 | int i, next_set = 0; |
| 599 | |
| 600 | queue_for_each_hw_ctx(q, hctx, i) |
| 601 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
| 602 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 603 | if (next_set) { |
| 604 | next = blk_rq_timeout(round_jiffies_up(next)); |
| 605 | mod_timer(&q->timeout, next); |
| 606 | } else { |
| 607 | queue_for_each_hw_ctx(q, hctx, i) |
| 608 | blk_mq_tag_idle(hctx); |
| 609 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Reverse check our software queue for entries that we could potentially |
| 614 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 615 | * too much time checking for merges. |
| 616 | */ |
| 617 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 618 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 619 | { |
| 620 | struct request *rq; |
| 621 | int checked = 8; |
| 622 | |
| 623 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 624 | int el_ret; |
| 625 | |
| 626 | if (!checked--) |
| 627 | break; |
| 628 | |
| 629 | if (!blk_rq_merge_ok(rq, bio)) |
| 630 | continue; |
| 631 | |
| 632 | el_ret = blk_try_merge(rq, bio); |
| 633 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 634 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 635 | ctx->rq_merged++; |
| 636 | return true; |
| 637 | } |
| 638 | break; |
| 639 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 640 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 641 | ctx->rq_merged++; |
| 642 | return true; |
| 643 | } |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return false; |
| 649 | } |
| 650 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 651 | /* |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 652 | * Process software queues that have been marked busy, splicing them |
| 653 | * to the for-dispatch |
| 654 | */ |
| 655 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
| 656 | { |
| 657 | struct blk_mq_ctx *ctx; |
| 658 | int i; |
| 659 | |
| 660 | for (i = 0; i < hctx->ctx_map.map_size; i++) { |
| 661 | struct blk_align_bitmap *bm = &hctx->ctx_map.map[i]; |
| 662 | unsigned int off, bit; |
| 663 | |
| 664 | if (!bm->word) |
| 665 | continue; |
| 666 | |
| 667 | bit = 0; |
| 668 | off = i * hctx->ctx_map.bits_per_word; |
| 669 | do { |
| 670 | bit = find_next_bit(&bm->word, bm->depth, bit); |
| 671 | if (bit >= bm->depth) |
| 672 | break; |
| 673 | |
| 674 | ctx = hctx->ctxs[bit + off]; |
| 675 | clear_bit(bit, &bm->word); |
| 676 | spin_lock(&ctx->lock); |
| 677 | list_splice_tail_init(&ctx->rq_list, list); |
| 678 | spin_unlock(&ctx->lock); |
| 679 | |
| 680 | bit++; |
| 681 | } while (1); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 686 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 687 | * Note that this function currently has various problems around ordering |
| 688 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 689 | * items on the hctx->dispatch list. Ignore that for now. |
| 690 | */ |
| 691 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 692 | { |
| 693 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 694 | struct request *rq; |
| 695 | LIST_HEAD(rq_list); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 696 | int queued; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 697 | |
Jens Axboe | fd1270d | 2014-04-16 09:23:48 -0600 | [diff] [blame] | 698 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 699 | |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 700 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 701 | return; |
| 702 | |
| 703 | hctx->run++; |
| 704 | |
| 705 | /* |
| 706 | * Touch any software queue that has pending entries. |
| 707 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 708 | flush_busy_ctxs(hctx, &rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 709 | |
| 710 | /* |
| 711 | * If we have previous entries on our dispatch list, grab them |
| 712 | * and stuff them at the front for more fair dispatch. |
| 713 | */ |
| 714 | if (!list_empty_careful(&hctx->dispatch)) { |
| 715 | spin_lock(&hctx->lock); |
| 716 | if (!list_empty(&hctx->dispatch)) |
| 717 | list_splice_init(&hctx->dispatch, &rq_list); |
| 718 | spin_unlock(&hctx->lock); |
| 719 | } |
| 720 | |
| 721 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 722 | * Now process all the entries, sending them to the driver. |
| 723 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 724 | queued = 0; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 725 | while (!list_empty(&rq_list)) { |
| 726 | int ret; |
| 727 | |
| 728 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 729 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 730 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 731 | blk_mq_start_request(rq, list_empty(&rq_list)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 732 | |
| 733 | ret = q->mq_ops->queue_rq(hctx, rq); |
| 734 | switch (ret) { |
| 735 | case BLK_MQ_RQ_QUEUE_OK: |
| 736 | queued++; |
| 737 | continue; |
| 738 | case BLK_MQ_RQ_QUEUE_BUSY: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 739 | list_add(&rq->queuelist, &rq_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 740 | __blk_mq_requeue_request(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 741 | break; |
| 742 | default: |
| 743 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 744 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 745 | rq->errors = -EIO; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 746 | blk_mq_end_io(rq, rq->errors); |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | if (!queued) |
| 755 | hctx->dispatched[0]++; |
| 756 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) |
| 757 | hctx->dispatched[ilog2(queued) + 1]++; |
| 758 | |
| 759 | /* |
| 760 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 761 | * that is where we will continue on next queue run. |
| 762 | */ |
| 763 | if (!list_empty(&rq_list)) { |
| 764 | spin_lock(&hctx->lock); |
| 765 | list_splice(&rq_list, &hctx->dispatch); |
| 766 | spin_unlock(&hctx->lock); |
| 767 | } |
| 768 | } |
| 769 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 770 | /* |
| 771 | * It'd be great if the workqueue API had a way to pass |
| 772 | * in a mask and had some smarts for more clever placement. |
| 773 | * For now we just round-robin here, switching for every |
| 774 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 775 | */ |
| 776 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 777 | { |
| 778 | int cpu = hctx->next_cpu; |
| 779 | |
| 780 | if (--hctx->next_cpu_batch <= 0) { |
| 781 | int next_cpu; |
| 782 | |
| 783 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); |
| 784 | if (next_cpu >= nr_cpu_ids) |
| 785 | next_cpu = cpumask_first(hctx->cpumask); |
| 786 | |
| 787 | hctx->next_cpu = next_cpu; |
| 788 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 789 | } |
| 790 | |
| 791 | return cpu; |
| 792 | } |
| 793 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 794 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 795 | { |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 796 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 797 | return; |
| 798 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 799 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 800 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 801 | else if (hctx->queue->nr_hw_queues == 1) |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 802 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 803 | else { |
| 804 | unsigned int cpu; |
| 805 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 806 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 807 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 808 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | void blk_mq_run_queues(struct request_queue *q, bool async) |
| 812 | { |
| 813 | struct blk_mq_hw_ctx *hctx; |
| 814 | int i; |
| 815 | |
| 816 | queue_for_each_hw_ctx(q, hctx, i) { |
| 817 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 818 | list_empty_careful(&hctx->dispatch)) || |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 819 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 820 | continue; |
| 821 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 822 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 823 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 824 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | EXPORT_SYMBOL(blk_mq_run_queues); |
| 828 | |
| 829 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 830 | { |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 831 | cancel_delayed_work(&hctx->run_work); |
| 832 | cancel_delayed_work(&hctx->delay_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 833 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 834 | } |
| 835 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 836 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 837 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 838 | { |
| 839 | struct blk_mq_hw_ctx *hctx; |
| 840 | int i; |
| 841 | |
| 842 | queue_for_each_hw_ctx(q, hctx, i) |
| 843 | blk_mq_stop_hw_queue(hctx); |
| 844 | } |
| 845 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 846 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 847 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 848 | { |
| 849 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 850 | |
| 851 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 852 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 853 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 854 | } |
| 855 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 856 | |
Christoph Hellwig | 2f26855 | 2014-04-16 09:44:56 +0200 | [diff] [blame] | 857 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 858 | { |
| 859 | struct blk_mq_hw_ctx *hctx; |
| 860 | int i; |
| 861 | |
| 862 | queue_for_each_hw_ctx(q, hctx, i) |
| 863 | blk_mq_start_hw_queue(hctx); |
| 864 | } |
| 865 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 866 | |
| 867 | |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 868 | 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] | 869 | { |
| 870 | struct blk_mq_hw_ctx *hctx; |
| 871 | int i; |
| 872 | |
| 873 | queue_for_each_hw_ctx(q, hctx, i) { |
| 874 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 875 | continue; |
| 876 | |
| 877 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 878 | preempt_disable(); |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 879 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 880 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 884 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 885 | static void blk_mq_run_work_fn(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 886 | { |
| 887 | struct blk_mq_hw_ctx *hctx; |
| 888 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 889 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 890 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 891 | __blk_mq_run_hw_queue(hctx); |
| 892 | } |
| 893 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 894 | static void blk_mq_delay_work_fn(struct work_struct *work) |
| 895 | { |
| 896 | struct blk_mq_hw_ctx *hctx; |
| 897 | |
| 898 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); |
| 899 | |
| 900 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 901 | __blk_mq_run_hw_queue(hctx); |
| 902 | } |
| 903 | |
| 904 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 905 | { |
| 906 | unsigned long tmo = msecs_to_jiffies(msecs); |
| 907 | |
| 908 | if (hctx->queue->nr_hw_queues == 1) |
| 909 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); |
| 910 | else { |
| 911 | unsigned int cpu; |
| 912 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 913 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 914 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
| 915 | } |
| 916 | } |
| 917 | EXPORT_SYMBOL(blk_mq_delay_queue); |
| 918 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 919 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 920 | struct request *rq, bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 921 | { |
| 922 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 923 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 924 | trace_block_rq_insert(hctx->queue, rq); |
| 925 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 926 | if (at_head) |
| 927 | list_add(&rq->queuelist, &ctx->rq_list); |
| 928 | else |
| 929 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 930 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 931 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 932 | |
| 933 | /* |
| 934 | * We do this early, to ensure we are on the right CPU. |
| 935 | */ |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 936 | blk_add_timer(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 939 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
| 940 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 941 | { |
| 942 | struct request_queue *q = rq->q; |
| 943 | struct blk_mq_hw_ctx *hctx; |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 944 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 945 | |
| 946 | current_ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 947 | if (!cpu_online(ctx->cpu)) |
| 948 | rq->mq_ctx = ctx = current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 949 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 950 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 951 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 952 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
| 953 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { |
| 954 | blk_insert_flush(rq); |
| 955 | } else { |
| 956 | spin_lock(&ctx->lock); |
| 957 | __blk_mq_insert_request(hctx, rq, at_head); |
| 958 | spin_unlock(&ctx->lock); |
| 959 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 960 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 961 | if (run_queue) |
| 962 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 963 | |
| 964 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static void blk_mq_insert_requests(struct request_queue *q, |
| 968 | struct blk_mq_ctx *ctx, |
| 969 | struct list_head *list, |
| 970 | int depth, |
| 971 | bool from_schedule) |
| 972 | |
| 973 | { |
| 974 | struct blk_mq_hw_ctx *hctx; |
| 975 | struct blk_mq_ctx *current_ctx; |
| 976 | |
| 977 | trace_block_unplug(q, depth, !from_schedule); |
| 978 | |
| 979 | current_ctx = blk_mq_get_ctx(q); |
| 980 | |
| 981 | if (!cpu_online(ctx->cpu)) |
| 982 | ctx = current_ctx; |
| 983 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 984 | |
| 985 | /* |
| 986 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 987 | * offline now |
| 988 | */ |
| 989 | spin_lock(&ctx->lock); |
| 990 | while (!list_empty(list)) { |
| 991 | struct request *rq; |
| 992 | |
| 993 | rq = list_first_entry(list, struct request, queuelist); |
| 994 | list_del_init(&rq->queuelist); |
| 995 | rq->mq_ctx = ctx; |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 996 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 997 | } |
| 998 | spin_unlock(&ctx->lock); |
| 999 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1000 | blk_mq_run_hw_queue(hctx, from_schedule); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1001 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1005 | { |
| 1006 | struct request *rqa = container_of(a, struct request, queuelist); |
| 1007 | struct request *rqb = container_of(b, struct request, queuelist); |
| 1008 | |
| 1009 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 1010 | (rqa->mq_ctx == rqb->mq_ctx && |
| 1011 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 1012 | } |
| 1013 | |
| 1014 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 1015 | { |
| 1016 | struct blk_mq_ctx *this_ctx; |
| 1017 | struct request_queue *this_q; |
| 1018 | struct request *rq; |
| 1019 | LIST_HEAD(list); |
| 1020 | LIST_HEAD(ctx_list); |
| 1021 | unsigned int depth; |
| 1022 | |
| 1023 | list_splice_init(&plug->mq_list, &list); |
| 1024 | |
| 1025 | list_sort(NULL, &list, plug_ctx_cmp); |
| 1026 | |
| 1027 | this_q = NULL; |
| 1028 | this_ctx = NULL; |
| 1029 | depth = 0; |
| 1030 | |
| 1031 | while (!list_empty(&list)) { |
| 1032 | rq = list_entry_rq(list.next); |
| 1033 | list_del_init(&rq->queuelist); |
| 1034 | BUG_ON(!rq->q); |
| 1035 | if (rq->mq_ctx != this_ctx) { |
| 1036 | if (this_ctx) { |
| 1037 | blk_mq_insert_requests(this_q, this_ctx, |
| 1038 | &ctx_list, depth, |
| 1039 | from_schedule); |
| 1040 | } |
| 1041 | |
| 1042 | this_ctx = rq->mq_ctx; |
| 1043 | this_q = rq->q; |
| 1044 | depth = 0; |
| 1045 | } |
| 1046 | |
| 1047 | depth++; |
| 1048 | list_add_tail(&rq->queuelist, &ctx_list); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * If 'this_ctx' is set, we know we have entries to complete |
| 1053 | * on 'ctx_list'. Do those. |
| 1054 | */ |
| 1055 | if (this_ctx) { |
| 1056 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 1057 | from_schedule); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 1062 | { |
| 1063 | init_request_from_bio(rq, bio); |
| 1064 | blk_account_io_start(rq, 1); |
| 1065 | } |
| 1066 | |
| 1067 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 1068 | { |
| 1069 | struct blk_mq_hw_ctx *hctx; |
| 1070 | struct blk_mq_ctx *ctx; |
| 1071 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 1072 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 1073 | int rw = bio_data_dir(bio); |
| 1074 | struct request *rq; |
| 1075 | unsigned int use_plug, request_count = 0; |
| 1076 | |
| 1077 | /* |
| 1078 | * If we have multiple hardware queues, just go directly to |
| 1079 | * one of those for sync IO. |
| 1080 | */ |
| 1081 | use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync); |
| 1082 | |
| 1083 | blk_queue_bounce(q, &bio); |
| 1084 | |
Nicholas Bellinger | 14ec77f | 2014-02-07 13:45:39 -0700 | [diff] [blame] | 1085 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 1086 | bio_endio(bio, -EIO); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1090 | if (use_plug && blk_attempt_plug_merge(q, bio, &request_count)) |
| 1091 | return; |
| 1092 | |
| 1093 | if (blk_mq_queue_enter(q)) { |
| 1094 | bio_endio(bio, -EIO); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
| 1098 | ctx = blk_mq_get_ctx(q); |
| 1099 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1100 | |
Shaohua Li | 27fbf4e8 | 2014-02-19 20:20:21 +0800 | [diff] [blame] | 1101 | if (is_sync) |
| 1102 | rw |= REQ_SYNC; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1103 | trace_block_getrq(q, bio, rw); |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 1104 | rq = __blk_mq_alloc_request(hctx, ctx, GFP_ATOMIC, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1105 | if (likely(rq)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1106 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1107 | else { |
| 1108 | blk_mq_put_ctx(ctx); |
| 1109 | trace_block_sleeprq(q, bio, rw); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1110 | rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC, |
| 1111 | false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1112 | ctx = rq->mq_ctx; |
| 1113 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1114 | } |
| 1115 | |
| 1116 | hctx->queued++; |
| 1117 | |
| 1118 | if (unlikely(is_flush_fua)) { |
| 1119 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1120 | blk_insert_flush(rq); |
| 1121 | goto run_queue; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * A task plug currently exists. Since this is completely lockless, |
| 1126 | * utilize that to temporarily store requests until the task is |
| 1127 | * either done or scheduled away. |
| 1128 | */ |
| 1129 | if (use_plug) { |
| 1130 | struct blk_plug *plug = current->plug; |
| 1131 | |
| 1132 | if (plug) { |
| 1133 | blk_mq_bio_to_request(rq, bio); |
Shaohua Li | 92f399c | 2013-10-29 12:01:03 -0600 | [diff] [blame] | 1134 | if (list_empty(&plug->mq_list)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1135 | trace_block_plug(q); |
| 1136 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { |
| 1137 | blk_flush_plug_list(plug, false); |
| 1138 | trace_block_plug(q); |
| 1139 | } |
| 1140 | list_add_tail(&rq->queuelist, &plug->mq_list); |
| 1141 | blk_mq_put_ctx(ctx); |
| 1142 | return; |
| 1143 | } |
| 1144 | } |
| 1145 | |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1146 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
Jens Axboe | cf4b50a | 2014-05-09 14:54:08 -0600 | [diff] [blame] | 1147 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1148 | spin_lock(&ctx->lock); |
| 1149 | insert_rq: |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1150 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1151 | spin_unlock(&ctx->lock); |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1152 | } else { |
| 1153 | spin_lock(&ctx->lock); |
| 1154 | if (!blk_mq_attempt_merge(q, ctx, bio)) { |
Jens Axboe | cf4b50a | 2014-05-09 14:54:08 -0600 | [diff] [blame] | 1155 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1156 | goto insert_rq; |
| 1157 | } |
| 1158 | |
| 1159 | spin_unlock(&ctx->lock); |
| 1160 | __blk_mq_free_request(hctx, ctx, rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1163 | |
| 1164 | /* |
| 1165 | * For a SYNC request, send it to the hardware immediately. For an |
| 1166 | * ASYNC request, just ensure that we run it later on. The latter |
| 1167 | * allows for merging opportunities and more efficient dispatching. |
| 1168 | */ |
| 1169 | run_queue: |
| 1170 | blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1171 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Default mapping to a software queue, since we use one per CPU. |
| 1176 | */ |
| 1177 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) |
| 1178 | { |
| 1179 | return q->queue_hw_ctx[q->mq_map[cpu]]; |
| 1180 | } |
| 1181 | EXPORT_SYMBOL(blk_mq_map_queue); |
| 1182 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1183 | struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1184 | unsigned int hctx_index) |
| 1185 | { |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 1186 | return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL, |
| 1187 | set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1188 | } |
| 1189 | EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); |
| 1190 | |
| 1191 | void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx, |
| 1192 | unsigned int hctx_index) |
| 1193 | { |
| 1194 | kfree(hctx); |
| 1195 | } |
| 1196 | EXPORT_SYMBOL(blk_mq_free_single_hw_queue); |
| 1197 | |
| 1198 | static void blk_mq_hctx_notify(void *data, unsigned long action, |
| 1199 | unsigned int cpu) |
| 1200 | { |
| 1201 | struct blk_mq_hw_ctx *hctx = data; |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1202 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1203 | struct blk_mq_ctx *ctx; |
| 1204 | LIST_HEAD(tmp); |
| 1205 | |
| 1206 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
| 1207 | return; |
| 1208 | |
| 1209 | /* |
| 1210 | * Move ctx entries to new CPU, if this one is going away. |
| 1211 | */ |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1212 | ctx = __blk_mq_get_ctx(q, cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1213 | |
| 1214 | spin_lock(&ctx->lock); |
| 1215 | if (!list_empty(&ctx->rq_list)) { |
| 1216 | list_splice_init(&ctx->rq_list, &tmp); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1217 | blk_mq_hctx_clear_pending(hctx, ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1218 | } |
| 1219 | spin_unlock(&ctx->lock); |
| 1220 | |
| 1221 | if (list_empty(&tmp)) |
| 1222 | return; |
| 1223 | |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1224 | ctx = blk_mq_get_ctx(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1225 | spin_lock(&ctx->lock); |
| 1226 | |
| 1227 | while (!list_empty(&tmp)) { |
| 1228 | struct request *rq; |
| 1229 | |
| 1230 | rq = list_first_entry(&tmp, struct request, queuelist); |
| 1231 | rq->mq_ctx = ctx; |
| 1232 | list_move_tail(&rq->queuelist, &ctx->rq_list); |
| 1233 | } |
| 1234 | |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1235 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1236 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1237 | |
| 1238 | spin_unlock(&ctx->lock); |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1239 | |
| 1240 | blk_mq_run_hw_queue(hctx, true); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1241 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1244 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
| 1245 | struct blk_mq_tags *tags, unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1246 | { |
| 1247 | struct page *page; |
| 1248 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1249 | if (tags->rqs && set->ops->exit_request) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1250 | int i; |
| 1251 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1252 | for (i = 0; i < tags->nr_tags; i++) { |
| 1253 | if (!tags->rqs[i]) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1254 | continue; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1255 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
| 1256 | hctx_idx, i); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1260 | while (!list_empty(&tags->page_list)) { |
| 1261 | page = list_first_entry(&tags->page_list, struct page, lru); |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1262 | list_del_init(&page->lru); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1263 | __free_pages(page, page->private); |
| 1264 | } |
| 1265 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1266 | kfree(tags->rqs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1267 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1268 | blk_mq_free_tags(tags); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | static size_t order_to_size(unsigned int order) |
| 1272 | { |
Ming Lei | 4ca0850 | 2014-04-19 18:00:18 +0800 | [diff] [blame] | 1273 | return (size_t)PAGE_SIZE << order; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1276 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
| 1277 | unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1278 | { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1279 | struct blk_mq_tags *tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1280 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1281 | size_t rq_size, left; |
| 1282 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1283 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
| 1284 | set->numa_node); |
| 1285 | if (!tags) |
| 1286 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1287 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1288 | INIT_LIST_HEAD(&tags->page_list); |
| 1289 | |
| 1290 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), |
| 1291 | GFP_KERNEL, set->numa_node); |
| 1292 | if (!tags->rqs) { |
| 1293 | blk_mq_free_tags(tags); |
| 1294 | return NULL; |
| 1295 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1296 | |
| 1297 | /* |
| 1298 | * rq_size is the size of the request plus driver payload, rounded |
| 1299 | * to the cacheline size |
| 1300 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1301 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1302 | cache_line_size()); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1303 | left = rq_size * set->queue_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1304 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1305 | for (i = 0; i < set->queue_depth; ) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1306 | int this_order = max_order; |
| 1307 | struct page *page; |
| 1308 | int to_do; |
| 1309 | void *p; |
| 1310 | |
| 1311 | while (left < order_to_size(this_order - 1) && this_order) |
| 1312 | this_order--; |
| 1313 | |
| 1314 | do { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1315 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
| 1316 | this_order); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1317 | if (page) |
| 1318 | break; |
| 1319 | if (!this_order--) |
| 1320 | break; |
| 1321 | if (order_to_size(this_order) < rq_size) |
| 1322 | break; |
| 1323 | } while (1); |
| 1324 | |
| 1325 | if (!page) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1326 | goto fail; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1327 | |
| 1328 | page->private = this_order; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1329 | list_add_tail(&page->lru, &tags->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1330 | |
| 1331 | p = page_address(page); |
| 1332 | entries_per_page = order_to_size(this_order) / rq_size; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1333 | to_do = min(entries_per_page, set->queue_depth - i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1334 | left -= to_do * rq_size; |
| 1335 | for (j = 0; j < to_do; j++) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1336 | tags->rqs[i] = p; |
| 1337 | if (set->ops->init_request) { |
| 1338 | if (set->ops->init_request(set->driver_data, |
| 1339 | tags->rqs[i], hctx_idx, i, |
| 1340 | set->numa_node)) |
| 1341 | goto fail; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1342 | } |
| 1343 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1344 | p += rq_size; |
| 1345 | i++; |
| 1346 | } |
| 1347 | } |
| 1348 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1349 | return tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1350 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1351 | fail: |
| 1352 | pr_warn("%s: failed to allocate requests\n", __func__); |
| 1353 | blk_mq_free_rq_map(set, tags, hctx_idx); |
| 1354 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1357 | static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap) |
| 1358 | { |
| 1359 | kfree(bitmap->map); |
| 1360 | } |
| 1361 | |
| 1362 | static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node) |
| 1363 | { |
| 1364 | unsigned int bpw = 8, total, num_maps, i; |
| 1365 | |
| 1366 | bitmap->bits_per_word = bpw; |
| 1367 | |
| 1368 | num_maps = ALIGN(nr_cpu_ids, bpw) / bpw; |
| 1369 | bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap), |
| 1370 | GFP_KERNEL, node); |
| 1371 | if (!bitmap->map) |
| 1372 | return -ENOMEM; |
| 1373 | |
| 1374 | bitmap->map_size = num_maps; |
| 1375 | |
| 1376 | total = nr_cpu_ids; |
| 1377 | for (i = 0; i < num_maps; i++) { |
| 1378 | bitmap->map[i].depth = min(total, bitmap->bits_per_word); |
| 1379 | total -= bitmap->map[i].depth; |
| 1380 | } |
| 1381 | |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1385 | static int blk_mq_init_hw_queues(struct request_queue *q, |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1386 | struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1387 | { |
| 1388 | struct blk_mq_hw_ctx *hctx; |
| 1389 | unsigned int i, j; |
| 1390 | |
| 1391 | /* |
| 1392 | * Initialize hardware queues |
| 1393 | */ |
| 1394 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1395 | int node; |
| 1396 | |
| 1397 | node = hctx->numa_node; |
| 1398 | if (node == NUMA_NO_NODE) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1399 | node = hctx->numa_node = set->numa_node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1400 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1401 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
| 1402 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1403 | spin_lock_init(&hctx->lock); |
| 1404 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1405 | hctx->queue = q; |
| 1406 | hctx->queue_num = i; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1407 | hctx->flags = set->flags; |
| 1408 | hctx->cmd_size = set->cmd_size; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1409 | |
| 1410 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, |
| 1411 | blk_mq_hctx_notify, hctx); |
| 1412 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); |
| 1413 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1414 | hctx->tags = set->tags[i]; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1415 | |
| 1416 | /* |
| 1417 | * Allocate space for all possible cpus to avoid allocation in |
| 1418 | * runtime |
| 1419 | */ |
| 1420 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1421 | GFP_KERNEL, node); |
| 1422 | if (!hctx->ctxs) |
| 1423 | break; |
| 1424 | |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1425 | if (blk_mq_alloc_bitmap(&hctx->ctx_map, node)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1426 | break; |
| 1427 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1428 | hctx->nr_ctx = 0; |
| 1429 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1430 | if (set->ops->init_hctx && |
| 1431 | set->ops->init_hctx(hctx, set->driver_data, i)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | if (i == q->nr_hw_queues) |
| 1436 | return 0; |
| 1437 | |
| 1438 | /* |
| 1439 | * Init failed |
| 1440 | */ |
| 1441 | queue_for_each_hw_ctx(q, hctx, j) { |
| 1442 | if (i == j) |
| 1443 | break; |
| 1444 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1445 | if (set->ops->exit_hctx) |
| 1446 | set->ops->exit_hctx(hctx, j); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1447 | |
| 1448 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1449 | kfree(hctx->ctxs); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 1450 | blk_mq_free_bitmap(&hctx->ctx_map); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | return 1; |
| 1454 | } |
| 1455 | |
| 1456 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1457 | unsigned int nr_hw_queues) |
| 1458 | { |
| 1459 | unsigned int i; |
| 1460 | |
| 1461 | for_each_possible_cpu(i) { |
| 1462 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1463 | struct blk_mq_hw_ctx *hctx; |
| 1464 | |
| 1465 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1466 | __ctx->cpu = i; |
| 1467 | spin_lock_init(&__ctx->lock); |
| 1468 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1469 | __ctx->queue = q; |
| 1470 | |
| 1471 | /* 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] | 1472 | if (!cpu_online(i)) |
| 1473 | continue; |
| 1474 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1475 | hctx = q->mq_ops->map_queue(q, i); |
| 1476 | cpumask_set_cpu(i, hctx->cpumask); |
| 1477 | hctx->nr_ctx++; |
| 1478 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1479 | /* |
| 1480 | * Set local node, IFF we have more than one hw queue. If |
| 1481 | * not, we remain on the home node of the device |
| 1482 | */ |
| 1483 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 1484 | hctx->numa_node = cpu_to_node(i); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 1489 | { |
| 1490 | unsigned int i; |
| 1491 | struct blk_mq_hw_ctx *hctx; |
| 1492 | struct blk_mq_ctx *ctx; |
| 1493 | |
| 1494 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1495 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1496 | hctx->nr_ctx = 0; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * Map software to hardware queues |
| 1501 | */ |
| 1502 | queue_for_each_ctx(q, ctx, i) { |
| 1503 | /* 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] | 1504 | if (!cpu_online(i)) |
| 1505 | continue; |
| 1506 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1507 | hctx = q->mq_ops->map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1508 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1509 | ctx->index_hw = hctx->nr_ctx; |
| 1510 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1511 | } |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1512 | |
| 1513 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1514 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
| 1515 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 1516 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1519 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set) |
| 1520 | { |
| 1521 | struct blk_mq_hw_ctx *hctx; |
| 1522 | struct request_queue *q; |
| 1523 | bool shared; |
| 1524 | int i; |
| 1525 | |
| 1526 | if (set->tag_list.next == set->tag_list.prev) |
| 1527 | shared = false; |
| 1528 | else |
| 1529 | shared = true; |
| 1530 | |
| 1531 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 1532 | blk_mq_freeze_queue(q); |
| 1533 | |
| 1534 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1535 | if (shared) |
| 1536 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
| 1537 | else |
| 1538 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
| 1539 | } |
| 1540 | blk_mq_unfreeze_queue(q); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | static void blk_mq_del_queue_tag_set(struct request_queue *q) |
| 1545 | { |
| 1546 | struct blk_mq_tag_set *set = q->tag_set; |
| 1547 | |
| 1548 | blk_mq_freeze_queue(q); |
| 1549 | |
| 1550 | mutex_lock(&set->tag_list_lock); |
| 1551 | list_del_init(&q->tag_set_list); |
| 1552 | blk_mq_update_tag_set_depth(set); |
| 1553 | mutex_unlock(&set->tag_list_lock); |
| 1554 | |
| 1555 | blk_mq_unfreeze_queue(q); |
| 1556 | } |
| 1557 | |
| 1558 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, |
| 1559 | struct request_queue *q) |
| 1560 | { |
| 1561 | q->tag_set = set; |
| 1562 | |
| 1563 | mutex_lock(&set->tag_list_lock); |
| 1564 | list_add_tail(&q->tag_set_list, &set->tag_list); |
| 1565 | blk_mq_update_tag_set_depth(set); |
| 1566 | mutex_unlock(&set->tag_list_lock); |
| 1567 | } |
| 1568 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1569 | 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] | 1570 | { |
| 1571 | struct blk_mq_hw_ctx **hctxs; |
| 1572 | struct blk_mq_ctx *ctx; |
| 1573 | struct request_queue *q; |
| 1574 | int i; |
| 1575 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1576 | ctx = alloc_percpu(struct blk_mq_ctx); |
| 1577 | if (!ctx) |
| 1578 | return ERR_PTR(-ENOMEM); |
| 1579 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1580 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
| 1581 | set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1582 | |
| 1583 | if (!hctxs) |
| 1584 | goto err_percpu; |
| 1585 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1586 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 1587 | hctxs[i] = set->ops->alloc_hctx(set, i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1588 | if (!hctxs[i]) |
| 1589 | goto err_hctxs; |
| 1590 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1591 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
| 1592 | goto err_hctxs; |
| 1593 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1594 | atomic_set(&hctxs[i]->nr_active, 0); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1595 | hctxs[i]->numa_node = NUMA_NO_NODE; |
| 1596 | hctxs[i]->queue_num = i; |
| 1597 | } |
| 1598 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1599 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1600 | if (!q) |
| 1601 | goto err_hctxs; |
| 1602 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1603 | q->mq_map = blk_mq_make_queue_map(set); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1604 | if (!q->mq_map) |
| 1605 | goto err_map; |
| 1606 | |
| 1607 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
| 1608 | blk_queue_rq_timeout(q, 30000); |
| 1609 | |
| 1610 | q->nr_queues = nr_cpu_ids; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1611 | q->nr_hw_queues = set->nr_hw_queues; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1612 | |
| 1613 | q->queue_ctx = ctx; |
| 1614 | q->queue_hw_ctx = hctxs; |
| 1615 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1616 | q->mq_ops = set->ops; |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 1617 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1618 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 1619 | q->sg_reserved_size = INT_MAX; |
| 1620 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1621 | blk_queue_make_request(q, blk_mq_make_request); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 1622 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1623 | if (set->timeout) |
| 1624 | blk_queue_rq_timeout(q, set->timeout); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1625 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1626 | if (set->ops->complete) |
| 1627 | blk_queue_softirq_done(q, set->ops->complete); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 1628 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1629 | blk_mq_init_flush(q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1630 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1631 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1632 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
| 1633 | set->cmd_size, cache_line_size()), |
| 1634 | GFP_KERNEL); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1635 | if (!q->flush_rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1636 | goto err_hw; |
| 1637 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1638 | if (blk_mq_init_hw_queues(q, set)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1639 | goto err_flush_rq; |
| 1640 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1641 | blk_mq_map_swqueue(q); |
| 1642 | |
| 1643 | mutex_lock(&all_q_mutex); |
| 1644 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1645 | mutex_unlock(&all_q_mutex); |
| 1646 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1647 | blk_mq_add_queue_tag_set(set, q); |
| 1648 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1649 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1650 | |
| 1651 | err_flush_rq: |
| 1652 | kfree(q->flush_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1653 | err_hw: |
| 1654 | kfree(q->mq_map); |
| 1655 | err_map: |
| 1656 | blk_cleanup_queue(q); |
| 1657 | err_hctxs: |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1658 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1659 | if (!hctxs[i]) |
| 1660 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1661 | free_cpumask_var(hctxs[i]->cpumask); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1662 | set->ops->free_hctx(hctxs[i], i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1663 | } |
| 1664 | kfree(hctxs); |
| 1665 | err_percpu: |
| 1666 | free_percpu(ctx); |
| 1667 | return ERR_PTR(-ENOMEM); |
| 1668 | } |
| 1669 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1670 | |
| 1671 | void blk_mq_free_queue(struct request_queue *q) |
| 1672 | { |
| 1673 | struct blk_mq_hw_ctx *hctx; |
| 1674 | int i; |
| 1675 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1676 | blk_mq_del_queue_tag_set(q); |
| 1677 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1678 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1679 | kfree(hctx->ctxs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1680 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
| 1681 | if (q->mq_ops->exit_hctx) |
| 1682 | q->mq_ops->exit_hctx(hctx, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1683 | free_cpumask_var(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1684 | q->mq_ops->free_hctx(hctx, i); |
| 1685 | } |
| 1686 | |
| 1687 | free_percpu(q->queue_ctx); |
| 1688 | kfree(q->queue_hw_ctx); |
| 1689 | kfree(q->mq_map); |
| 1690 | |
| 1691 | q->queue_ctx = NULL; |
| 1692 | q->queue_hw_ctx = NULL; |
| 1693 | q->mq_map = NULL; |
| 1694 | |
| 1695 | mutex_lock(&all_q_mutex); |
| 1696 | list_del_init(&q->all_q_node); |
| 1697 | mutex_unlock(&all_q_mutex); |
| 1698 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1699 | |
| 1700 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1701 | static void blk_mq_queue_reinit(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1702 | { |
| 1703 | blk_mq_freeze_queue(q); |
| 1704 | |
| 1705 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
| 1706 | |
| 1707 | /* |
| 1708 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 1709 | * we should change hctx numa_node according to new topology (this |
| 1710 | * involves free and re-allocate memory, worthy doing?) |
| 1711 | */ |
| 1712 | |
| 1713 | blk_mq_map_swqueue(q); |
| 1714 | |
| 1715 | blk_mq_unfreeze_queue(q); |
| 1716 | } |
| 1717 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1718 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
| 1719 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1720 | { |
| 1721 | struct request_queue *q; |
| 1722 | |
| 1723 | /* |
Jens Axboe | 9fccfed | 2014-05-08 14:50:19 -0600 | [diff] [blame] | 1724 | * Before new mappings are established, hotadded cpu might already |
| 1725 | * start handling requests. This doesn't break anything as we map |
| 1726 | * offline CPUs to first hardware queue. We will re-init the queue |
| 1727 | * below to get optimal settings. |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1728 | */ |
| 1729 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && |
| 1730 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) |
| 1731 | return NOTIFY_OK; |
| 1732 | |
| 1733 | mutex_lock(&all_q_mutex); |
| 1734 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1735 | blk_mq_queue_reinit(q); |
| 1736 | mutex_unlock(&all_q_mutex); |
| 1737 | return NOTIFY_OK; |
| 1738 | } |
| 1739 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1740 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 1741 | { |
| 1742 | int i; |
| 1743 | |
| 1744 | if (!set->nr_hw_queues) |
| 1745 | return -EINVAL; |
| 1746 | if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH) |
| 1747 | return -EINVAL; |
| 1748 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 1749 | return -EINVAL; |
| 1750 | |
| 1751 | if (!set->nr_hw_queues || |
| 1752 | !set->ops->queue_rq || !set->ops->map_queue || |
| 1753 | !set->ops->alloc_hctx || !set->ops->free_hctx) |
| 1754 | return -EINVAL; |
| 1755 | |
| 1756 | |
Ming Lei | 48479005 | 2014-04-19 18:00:17 +0800 | [diff] [blame] | 1757 | set->tags = kmalloc_node(set->nr_hw_queues * |
| 1758 | sizeof(struct blk_mq_tags *), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1759 | GFP_KERNEL, set->numa_node); |
| 1760 | if (!set->tags) |
| 1761 | goto out; |
| 1762 | |
| 1763 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 1764 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 1765 | if (!set->tags[i]) |
| 1766 | goto out_unwind; |
| 1767 | } |
| 1768 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1769 | mutex_init(&set->tag_list_lock); |
| 1770 | INIT_LIST_HEAD(&set->tag_list); |
| 1771 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1772 | return 0; |
| 1773 | |
| 1774 | out_unwind: |
| 1775 | while (--i >= 0) |
| 1776 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 1777 | out: |
| 1778 | return -ENOMEM; |
| 1779 | } |
| 1780 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 1781 | |
| 1782 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 1783 | { |
| 1784 | int i; |
| 1785 | |
| 1786 | for (i = 0; i < set->nr_hw_queues; i++) |
| 1787 | blk_mq_free_rq_map(set, set->tags[i], i); |
Ming Lei | 981bd18 | 2014-04-24 00:07:34 +0800 | [diff] [blame] | 1788 | kfree(set->tags); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1789 | } |
| 1790 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 1791 | |
Jens Axboe | e3a2b3f | 2014-05-20 11:49:02 -0600 | [diff] [blame^] | 1792 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
| 1793 | { |
| 1794 | struct blk_mq_tag_set *set = q->tag_set; |
| 1795 | struct blk_mq_hw_ctx *hctx; |
| 1796 | int i, ret; |
| 1797 | |
| 1798 | if (!set || nr > set->queue_depth) |
| 1799 | return -EINVAL; |
| 1800 | |
| 1801 | ret = 0; |
| 1802 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1803 | ret = blk_mq_tag_update_depth(hctx->tags, nr); |
| 1804 | if (ret) |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | if (!ret) |
| 1809 | q->nr_requests = nr; |
| 1810 | |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 1814 | void blk_mq_disable_hotplug(void) |
| 1815 | { |
| 1816 | mutex_lock(&all_q_mutex); |
| 1817 | } |
| 1818 | |
| 1819 | void blk_mq_enable_hotplug(void) |
| 1820 | { |
| 1821 | mutex_unlock(&all_q_mutex); |
| 1822 | } |
| 1823 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1824 | static int __init blk_mq_init(void) |
| 1825 | { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1826 | blk_mq_cpu_init(); |
| 1827 | |
| 1828 | /* Must be called after percpu_counter_hotcpu_callback() */ |
| 1829 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); |
| 1830 | |
| 1831 | return 0; |
| 1832 | } |
| 1833 | subsys_initcall(blk_mq_init); |