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 | |
| 59 | for (i = 0; i < hctx->nr_ctx_map; i++) |
| 60 | if (hctx->ctx_map[i]) |
| 61 | return true; |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Mark this ctx as having pending work in this hardware queue |
| 68 | */ |
| 69 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, |
| 70 | struct blk_mq_ctx *ctx) |
| 71 | { |
| 72 | if (!test_bit(ctx->index_hw, hctx->ctx_map)) |
| 73 | set_bit(ctx->index_hw, hctx->ctx_map); |
| 74 | } |
| 75 | |
Christoph Hellwig | 081241e | 2014-02-20 15:32:36 -0800 | [diff] [blame] | 76 | static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx, |
| 77 | gfp_t gfp, bool reserved) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 78 | { |
| 79 | struct request *rq; |
| 80 | unsigned int tag; |
| 81 | |
| 82 | tag = blk_mq_get_tag(hctx->tags, gfp, reserved); |
| 83 | if (tag != BLK_MQ_TAG_FAIL) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 84 | rq = hctx->tags->rqs[tag]; |
Jens Axboe | 70ab0b2 | 2014-04-24 08:50:38 -0600 | [diff] [blame] | 85 | blk_rq_init(hctx->queue, rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 86 | rq->tag = tag; |
| 87 | |
| 88 | return rq; |
| 89 | } |
| 90 | |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | static int blk_mq_queue_enter(struct request_queue *q) |
| 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
| 99 | smp_wmb(); |
| 100 | /* we have problems to freeze the queue if it's initializing */ |
| 101 | if (!blk_queue_bypass(q) || !blk_queue_init_done(q)) |
| 102 | return 0; |
| 103 | |
| 104 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 105 | |
| 106 | spin_lock_irq(q->queue_lock); |
| 107 | ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq, |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 108 | !blk_queue_bypass(q) || blk_queue_dying(q), |
| 109 | *q->queue_lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 110 | /* inc usage with lock hold to avoid freeze_queue runs here */ |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 111 | if (!ret && !blk_queue_dying(q)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 112 | __percpu_counter_add(&q->mq_usage_counter, 1, 1000000); |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 113 | else if (blk_queue_dying(q)) |
| 114 | ret = -ENODEV; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 115 | spin_unlock_irq(q->queue_lock); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static void blk_mq_queue_exit(struct request_queue *q) |
| 121 | { |
| 122 | __percpu_counter_add(&q->mq_usage_counter, -1, 1000000); |
| 123 | } |
| 124 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 125 | static void __blk_mq_drain_queue(struct request_queue *q) |
| 126 | { |
| 127 | while (true) { |
| 128 | s64 count; |
| 129 | |
| 130 | spin_lock_irq(q->queue_lock); |
| 131 | count = percpu_counter_sum(&q->mq_usage_counter); |
| 132 | spin_unlock_irq(q->queue_lock); |
| 133 | |
| 134 | if (count == 0) |
| 135 | break; |
| 136 | blk_mq_run_queues(q, false); |
| 137 | msleep(10); |
| 138 | } |
| 139 | } |
| 140 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 141 | /* |
| 142 | * Guarantee no request is in use, so we can change any data structure of |
| 143 | * the queue afterward. |
| 144 | */ |
| 145 | static void blk_mq_freeze_queue(struct request_queue *q) |
| 146 | { |
| 147 | bool drain; |
| 148 | |
| 149 | spin_lock_irq(q->queue_lock); |
| 150 | drain = !q->bypass_depth++; |
| 151 | queue_flag_set(QUEUE_FLAG_BYPASS, q); |
| 152 | spin_unlock_irq(q->queue_lock); |
| 153 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 154 | if (drain) |
| 155 | __blk_mq_drain_queue(q); |
| 156 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 157 | |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 158 | void blk_mq_drain_queue(struct request_queue *q) |
| 159 | { |
| 160 | __blk_mq_drain_queue(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void blk_mq_unfreeze_queue(struct request_queue *q) |
| 164 | { |
| 165 | bool wake = false; |
| 166 | |
| 167 | spin_lock_irq(q->queue_lock); |
| 168 | if (!--q->bypass_depth) { |
| 169 | queue_flag_clear(QUEUE_FLAG_BYPASS, q); |
| 170 | wake = true; |
| 171 | } |
| 172 | WARN_ON_ONCE(q->bypass_depth < 0); |
| 173 | spin_unlock_irq(q->queue_lock); |
| 174 | if (wake) |
| 175 | wake_up_all(&q->mq_freeze_wq); |
| 176 | } |
| 177 | |
| 178 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) |
| 179 | { |
| 180 | return blk_mq_has_free_tags(hctx->tags); |
| 181 | } |
| 182 | EXPORT_SYMBOL(blk_mq_can_queue); |
| 183 | |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 184 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
| 185 | struct request *rq, unsigned int rw_flags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 186 | { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 187 | if (blk_queue_io_stat(q)) |
| 188 | rw_flags |= REQ_IO_STAT; |
| 189 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 190 | rq->mq_ctx = ctx; |
| 191 | rq->cmd_flags = rw_flags; |
Ming Lei | 0fec08b | 2014-01-03 10:00:08 -0700 | [diff] [blame] | 192 | rq->start_time = jiffies; |
| 193 | set_start_time_ns(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 194 | ctx->rq_dispatched[rw_is_sync(rw_flags)]++; |
| 195 | } |
| 196 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 197 | static struct request *blk_mq_alloc_request_pinned(struct request_queue *q, |
| 198 | int rw, gfp_t gfp, |
| 199 | bool reserved) |
| 200 | { |
| 201 | struct request *rq; |
| 202 | |
| 203 | do { |
| 204 | struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); |
| 205 | struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 206 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 207 | rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 208 | if (rq) { |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 209 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 210 | break; |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 211 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 212 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 213 | if (gfp & __GFP_WAIT) { |
| 214 | __blk_mq_run_hw_queue(hctx); |
| 215 | blk_mq_put_ctx(ctx); |
| 216 | } else { |
| 217 | blk_mq_put_ctx(ctx); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 218 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 219 | } |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 220 | |
Jens Axboe | 5810d90 | 2014-04-29 20:49:48 -0600 | [diff] [blame] | 221 | blk_mq_wait_for_tags(hctx->tags, reserved); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 222 | } while (1); |
| 223 | |
| 224 | return rq; |
| 225 | } |
| 226 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 227 | 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] | 228 | { |
| 229 | struct request *rq; |
| 230 | |
| 231 | if (blk_mq_queue_enter(q)) |
| 232 | return NULL; |
| 233 | |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 234 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, false); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 235 | if (rq) |
| 236 | blk_mq_put_ctx(rq->mq_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 237 | return rq; |
| 238 | } |
| 239 | |
| 240 | struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, |
| 241 | gfp_t gfp) |
| 242 | { |
| 243 | struct request *rq; |
| 244 | |
| 245 | if (blk_mq_queue_enter(q)) |
| 246 | return NULL; |
| 247 | |
| 248 | rq = blk_mq_alloc_request_pinned(q, rw, gfp, true); |
Jeff Moyer | 959a35f | 2013-12-03 14:23:00 -0700 | [diff] [blame] | 249 | if (rq) |
| 250 | blk_mq_put_ctx(rq->mq_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 251 | return rq; |
| 252 | } |
| 253 | EXPORT_SYMBOL(blk_mq_alloc_reserved_request); |
| 254 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 255 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
| 256 | struct blk_mq_ctx *ctx, struct request *rq) |
| 257 | { |
| 258 | const int tag = rq->tag; |
| 259 | struct request_queue *q = rq->q; |
| 260 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 261 | blk_mq_put_tag(hctx->tags, tag); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 262 | blk_mq_queue_exit(q); |
| 263 | } |
| 264 | |
| 265 | void blk_mq_free_request(struct request *rq) |
| 266 | { |
| 267 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 268 | struct blk_mq_hw_ctx *hctx; |
| 269 | struct request_queue *q = rq->q; |
| 270 | |
| 271 | ctx->rq_completed[rq_is_sync(rq)]++; |
| 272 | |
| 273 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 274 | __blk_mq_free_request(hctx, ctx, rq); |
| 275 | } |
| 276 | |
Christoph Hellwig | 8727af4 | 2014-04-14 10:30:08 +0200 | [diff] [blame] | 277 | /* |
| 278 | * Clone all relevant state from a request that has been put on hold in |
| 279 | * the flush state machine into the preallocated flush request that hangs |
| 280 | * off the request queue. |
| 281 | * |
| 282 | * For a driver the flush request should be invisible, that's why we are |
| 283 | * impersonating the original request here. |
| 284 | */ |
| 285 | void blk_mq_clone_flush_request(struct request *flush_rq, |
| 286 | struct request *orig_rq) |
| 287 | { |
| 288 | struct blk_mq_hw_ctx *hctx = |
| 289 | orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu); |
| 290 | |
| 291 | flush_rq->mq_ctx = orig_rq->mq_ctx; |
| 292 | flush_rq->tag = orig_rq->tag; |
| 293 | memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq), |
| 294 | hctx->cmd_size); |
| 295 | } |
| 296 | |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 297 | inline void __blk_mq_end_io(struct request *rq, int error) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 298 | { |
Ming Lei | 0d11e6a | 2013-12-05 10:50:39 -0700 | [diff] [blame] | 299 | blk_account_io_done(rq); |
| 300 | |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 301 | if (rq->end_io) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 302 | rq->end_io(rq, error); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 303 | } else { |
| 304 | if (unlikely(blk_bidi_rq(rq))) |
| 305 | blk_mq_free_request(rq->next_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 306 | blk_mq_free_request(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 307 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 308 | } |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 309 | EXPORT_SYMBOL(__blk_mq_end_io); |
| 310 | |
| 311 | void blk_mq_end_io(struct request *rq, int error) |
| 312 | { |
| 313 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
| 314 | BUG(); |
| 315 | __blk_mq_end_io(rq, error); |
| 316 | } |
| 317 | EXPORT_SYMBOL(blk_mq_end_io); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 318 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 319 | static void __blk_mq_complete_request_remote(void *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 320 | { |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 321 | struct request *rq = data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 322 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 323 | rq->q->softirq_done_fn(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 326 | void __blk_mq_complete_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 327 | { |
| 328 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 329 | bool shared = false; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 330 | int cpu; |
| 331 | |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 332 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 333 | rq->q->softirq_done_fn(rq); |
| 334 | return; |
| 335 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 336 | |
| 337 | cpu = get_cpu(); |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 338 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
| 339 | shared = cpus_share_cache(cpu, ctx->cpu); |
| 340 | |
| 341 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 342 | rq->csd.func = __blk_mq_complete_request_remote; |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 343 | rq->csd.info = rq; |
| 344 | rq->csd.flags = 0; |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 345 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 346 | } else { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 347 | rq->q->softirq_done_fn(rq); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 348 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 349 | put_cpu(); |
| 350 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * blk_mq_complete_request - end I/O on a request |
| 354 | * @rq: the request being processed |
| 355 | * |
| 356 | * Description: |
| 357 | * Ends all I/O on a request. It does not handle partial completions. |
| 358 | * The actual completion happens out-of-order, through a IPI handler. |
| 359 | **/ |
| 360 | void blk_mq_complete_request(struct request *rq) |
| 361 | { |
| 362 | if (unlikely(blk_should_fake_timeout(rq->q))) |
| 363 | return; |
| 364 | if (!blk_mark_rq_complete(rq)) |
| 365 | __blk_mq_complete_request(rq); |
| 366 | } |
| 367 | EXPORT_SYMBOL(blk_mq_complete_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 368 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 369 | static void blk_mq_start_request(struct request *rq, bool last) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 370 | { |
| 371 | struct request_queue *q = rq->q; |
| 372 | |
| 373 | trace_block_rq_issue(q, rq); |
| 374 | |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 375 | rq->resid_len = blk_rq_bytes(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 376 | if (unlikely(blk_bidi_rq(rq))) |
| 377 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 378 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 379 | /* |
| 380 | * Just mark start time and set the started bit. Due to memory |
| 381 | * ordering, we know we'll see the correct deadline as long as |
| 382 | * REQ_ATOMIC_STARTED is seen. |
| 383 | */ |
| 384 | rq->deadline = jiffies + q->rq_timeout; |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 385 | |
| 386 | /* |
| 387 | * Mark us as started and clear complete. Complete might have been |
| 388 | * set if requeue raced with timeout, which then marked it as |
| 389 | * complete. So be sure to clear complete again when we start |
| 390 | * the request, otherwise we'll ignore the completion event. |
| 391 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 392 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 393 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 394 | |
| 395 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
| 396 | /* |
| 397 | * Make sure space for the drain appears. We know we can do |
| 398 | * this because max_hw_segments has been adjusted to be one |
| 399 | * fewer than the device can handle. |
| 400 | */ |
| 401 | rq->nr_phys_segments++; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Flag the last request in the series so that drivers know when IO |
| 406 | * should be kicked off, if they don't do it on a per-request basis. |
| 407 | * |
| 408 | * Note: the flag isn't the only condition drivers should do kick off. |
| 409 | * If drive is busy, the last request might not have the bit set. |
| 410 | */ |
| 411 | if (last) |
| 412 | rq->cmd_flags |= REQ_END; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 415 | static void __blk_mq_requeue_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 416 | { |
| 417 | struct request_queue *q = rq->q; |
| 418 | |
| 419 | trace_block_rq_requeue(q, rq); |
| 420 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 421 | |
| 422 | rq->cmd_flags &= ~REQ_END; |
| 423 | |
| 424 | if (q->dma_drain_size && blk_rq_bytes(rq)) |
| 425 | rq->nr_phys_segments--; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 428 | void blk_mq_requeue_request(struct request *rq) |
| 429 | { |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 430 | __blk_mq_requeue_request(rq); |
| 431 | blk_clear_rq_complete(rq); |
| 432 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 433 | BUG_ON(blk_queued_rq(rq)); |
| 434 | blk_mq_insert_request(rq, true, true, false); |
| 435 | } |
| 436 | EXPORT_SYMBOL(blk_mq_requeue_request); |
| 437 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 438 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
| 439 | { |
| 440 | return tags->rqs[tag]; |
| 441 | } |
| 442 | EXPORT_SYMBOL(blk_mq_tag_to_rq); |
| 443 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 444 | struct blk_mq_timeout_data { |
| 445 | struct blk_mq_hw_ctx *hctx; |
| 446 | unsigned long *next; |
| 447 | unsigned int *next_set; |
| 448 | }; |
| 449 | |
| 450 | static void blk_mq_timeout_check(void *__data, unsigned long *free_tags) |
| 451 | { |
| 452 | struct blk_mq_timeout_data *data = __data; |
| 453 | struct blk_mq_hw_ctx *hctx = data->hctx; |
| 454 | unsigned int tag; |
| 455 | |
| 456 | /* It may not be in flight yet (this is where |
| 457 | * the REQ_ATOMIC_STARTED flag comes in). The requests are |
| 458 | * statically allocated, so we know it's always safe to access the |
| 459 | * memory associated with a bit offset into ->rqs[]. |
| 460 | */ |
| 461 | tag = 0; |
| 462 | do { |
| 463 | struct request *rq; |
| 464 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 465 | tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag); |
| 466 | if (tag >= hctx->tags->nr_tags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 467 | break; |
| 468 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 469 | rq = blk_mq_tag_to_rq(hctx->tags, tag++); |
| 470 | if (rq->q != hctx->queue) |
| 471 | continue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 472 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 473 | continue; |
| 474 | |
| 475 | blk_rq_check_expired(rq, data->next, data->next_set); |
| 476 | } while (1); |
| 477 | } |
| 478 | |
| 479 | static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx, |
| 480 | unsigned long *next, |
| 481 | unsigned int *next_set) |
| 482 | { |
| 483 | struct blk_mq_timeout_data data = { |
| 484 | .hctx = hctx, |
| 485 | .next = next, |
| 486 | .next_set = next_set, |
| 487 | }; |
| 488 | |
| 489 | /* |
| 490 | * Ask the tagging code to iterate busy requests, so we can |
| 491 | * check them for timeout. |
| 492 | */ |
| 493 | blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data); |
| 494 | } |
| 495 | |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 496 | static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq) |
| 497 | { |
| 498 | struct request_queue *q = rq->q; |
| 499 | |
| 500 | /* |
| 501 | * We know that complete is set at this point. If STARTED isn't set |
| 502 | * anymore, then the request isn't active and the "timeout" should |
| 503 | * just be ignored. This can happen due to the bitflag ordering. |
| 504 | * Timeout first checks if STARTED is set, and if it is, assumes |
| 505 | * the request is active. But if we race with completion, then |
| 506 | * we both flags will get cleared. So check here again, and ignore |
| 507 | * a timeout event with a request that isn't active. |
| 508 | */ |
| 509 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 510 | return BLK_EH_NOT_HANDLED; |
| 511 | |
| 512 | if (!q->mq_ops->timeout) |
| 513 | return BLK_EH_RESET_TIMER; |
| 514 | |
| 515 | return q->mq_ops->timeout(rq); |
| 516 | } |
| 517 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 518 | static void blk_mq_rq_timer(unsigned long data) |
| 519 | { |
| 520 | struct request_queue *q = (struct request_queue *) data; |
| 521 | struct blk_mq_hw_ctx *hctx; |
| 522 | unsigned long next = 0; |
| 523 | int i, next_set = 0; |
| 524 | |
| 525 | queue_for_each_hw_ctx(q, hctx, i) |
| 526 | blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set); |
| 527 | |
| 528 | if (next_set) |
| 529 | mod_timer(&q->timeout, round_jiffies_up(next)); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Reverse check our software queue for entries that we could potentially |
| 534 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 535 | * too much time checking for merges. |
| 536 | */ |
| 537 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 538 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 539 | { |
| 540 | struct request *rq; |
| 541 | int checked = 8; |
| 542 | |
| 543 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 544 | int el_ret; |
| 545 | |
| 546 | if (!checked--) |
| 547 | break; |
| 548 | |
| 549 | if (!blk_rq_merge_ok(rq, bio)) |
| 550 | continue; |
| 551 | |
| 552 | el_ret = blk_try_merge(rq, bio); |
| 553 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 554 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 555 | ctx->rq_merged++; |
| 556 | return true; |
| 557 | } |
| 558 | break; |
| 559 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 560 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 561 | ctx->rq_merged++; |
| 562 | return true; |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | return false; |
| 569 | } |
| 570 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 571 | /* |
| 572 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 573 | * Note that this function currently has various problems around ordering |
| 574 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 575 | * items on the hctx->dispatch list. Ignore that for now. |
| 576 | */ |
| 577 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 578 | { |
| 579 | struct request_queue *q = hctx->queue; |
| 580 | struct blk_mq_ctx *ctx; |
| 581 | struct request *rq; |
| 582 | LIST_HEAD(rq_list); |
| 583 | int bit, queued; |
| 584 | |
Jens Axboe | fd1270d | 2014-04-16 09:23:48 -0600 | [diff] [blame] | 585 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 586 | |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 587 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 588 | return; |
| 589 | |
| 590 | hctx->run++; |
| 591 | |
| 592 | /* |
| 593 | * Touch any software queue that has pending entries. |
| 594 | */ |
| 595 | for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) { |
| 596 | clear_bit(bit, hctx->ctx_map); |
| 597 | ctx = hctx->ctxs[bit]; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 598 | |
| 599 | spin_lock(&ctx->lock); |
| 600 | list_splice_tail_init(&ctx->rq_list, &rq_list); |
| 601 | spin_unlock(&ctx->lock); |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * If we have previous entries on our dispatch list, grab them |
| 606 | * and stuff them at the front for more fair dispatch. |
| 607 | */ |
| 608 | if (!list_empty_careful(&hctx->dispatch)) { |
| 609 | spin_lock(&hctx->lock); |
| 610 | if (!list_empty(&hctx->dispatch)) |
| 611 | list_splice_init(&hctx->dispatch, &rq_list); |
| 612 | spin_unlock(&hctx->lock); |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Delete and return all entries from our dispatch list |
| 617 | */ |
| 618 | queued = 0; |
| 619 | |
| 620 | /* |
| 621 | * Now process all the entries, sending them to the driver. |
| 622 | */ |
| 623 | while (!list_empty(&rq_list)) { |
| 624 | int ret; |
| 625 | |
| 626 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 627 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 628 | |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 629 | blk_mq_start_request(rq, list_empty(&rq_list)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 630 | |
| 631 | ret = q->mq_ops->queue_rq(hctx, rq); |
| 632 | switch (ret) { |
| 633 | case BLK_MQ_RQ_QUEUE_OK: |
| 634 | queued++; |
| 635 | continue; |
| 636 | case BLK_MQ_RQ_QUEUE_BUSY: |
| 637 | /* |
| 638 | * FIXME: we should have a mechanism to stop the queue |
| 639 | * like blk_stop_queue, otherwise we will waste cpu |
| 640 | * time |
| 641 | */ |
| 642 | list_add(&rq->queuelist, &rq_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 643 | __blk_mq_requeue_request(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 644 | break; |
| 645 | default: |
| 646 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 647 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 648 | rq->errors = -EIO; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 649 | blk_mq_end_io(rq, rq->errors); |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | if (!queued) |
| 658 | hctx->dispatched[0]++; |
| 659 | else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1))) |
| 660 | hctx->dispatched[ilog2(queued) + 1]++; |
| 661 | |
| 662 | /* |
| 663 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 664 | * that is where we will continue on next queue run. |
| 665 | */ |
| 666 | if (!list_empty(&rq_list)) { |
| 667 | spin_lock(&hctx->lock); |
| 668 | list_splice(&rq_list, &hctx->dispatch); |
| 669 | spin_unlock(&hctx->lock); |
| 670 | } |
| 671 | } |
| 672 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame^] | 673 | /* |
| 674 | * It'd be great if the workqueue API had a way to pass |
| 675 | * in a mask and had some smarts for more clever placement. |
| 676 | * For now we just round-robin here, switching for every |
| 677 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 678 | */ |
| 679 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 680 | { |
| 681 | int cpu = hctx->next_cpu; |
| 682 | |
| 683 | if (--hctx->next_cpu_batch <= 0) { |
| 684 | int next_cpu; |
| 685 | |
| 686 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); |
| 687 | if (next_cpu >= nr_cpu_ids) |
| 688 | next_cpu = cpumask_first(hctx->cpumask); |
| 689 | |
| 690 | hctx->next_cpu = next_cpu; |
| 691 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 692 | } |
| 693 | |
| 694 | return cpu; |
| 695 | } |
| 696 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 697 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 698 | { |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 699 | if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 700 | return; |
| 701 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 702 | if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 703 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 704 | else if (hctx->queue->nr_hw_queues == 1) |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 705 | kblockd_schedule_delayed_work(&hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 706 | else { |
| 707 | unsigned int cpu; |
| 708 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame^] | 709 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 710 | kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 711 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | void blk_mq_run_queues(struct request_queue *q, bool async) |
| 715 | { |
| 716 | struct blk_mq_hw_ctx *hctx; |
| 717 | int i; |
| 718 | |
| 719 | queue_for_each_hw_ctx(q, hctx, i) { |
| 720 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 721 | list_empty_careful(&hctx->dispatch)) || |
Jens Axboe | 5d12f90 | 2014-03-19 15:25:02 -0600 | [diff] [blame] | 722 | test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 723 | continue; |
| 724 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 725 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 726 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 727 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | EXPORT_SYMBOL(blk_mq_run_queues); |
| 731 | |
| 732 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 733 | { |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 734 | cancel_delayed_work(&hctx->run_work); |
| 735 | cancel_delayed_work(&hctx->delay_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 736 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 737 | } |
| 738 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 739 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 740 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 741 | { |
| 742 | struct blk_mq_hw_ctx *hctx; |
| 743 | int i; |
| 744 | |
| 745 | queue_for_each_hw_ctx(q, hctx, i) |
| 746 | blk_mq_stop_hw_queue(hctx); |
| 747 | } |
| 748 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 749 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 750 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 751 | { |
| 752 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 753 | |
| 754 | preempt_disable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 755 | __blk_mq_run_hw_queue(hctx); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 756 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 757 | } |
| 758 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 759 | |
Christoph Hellwig | 2f26855 | 2014-04-16 09:44:56 +0200 | [diff] [blame] | 760 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 761 | { |
| 762 | struct blk_mq_hw_ctx *hctx; |
| 763 | int i; |
| 764 | |
| 765 | queue_for_each_hw_ctx(q, hctx, i) |
| 766 | blk_mq_start_hw_queue(hctx); |
| 767 | } |
| 768 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 769 | |
| 770 | |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 771 | 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] | 772 | { |
| 773 | struct blk_mq_hw_ctx *hctx; |
| 774 | int i; |
| 775 | |
| 776 | queue_for_each_hw_ctx(q, hctx, i) { |
| 777 | if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 778 | continue; |
| 779 | |
| 780 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 781 | preempt_disable(); |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 782 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 783 | preempt_enable(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 787 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 788 | static void blk_mq_run_work_fn(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 789 | { |
| 790 | struct blk_mq_hw_ctx *hctx; |
| 791 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 792 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 793 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 794 | __blk_mq_run_hw_queue(hctx); |
| 795 | } |
| 796 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 797 | static void blk_mq_delay_work_fn(struct work_struct *work) |
| 798 | { |
| 799 | struct blk_mq_hw_ctx *hctx; |
| 800 | |
| 801 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); |
| 802 | |
| 803 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 804 | __blk_mq_run_hw_queue(hctx); |
| 805 | } |
| 806 | |
| 807 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 808 | { |
| 809 | unsigned long tmo = msecs_to_jiffies(msecs); |
| 810 | |
| 811 | if (hctx->queue->nr_hw_queues == 1) |
| 812 | kblockd_schedule_delayed_work(&hctx->delay_work, tmo); |
| 813 | else { |
| 814 | unsigned int cpu; |
| 815 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame^] | 816 | cpu = blk_mq_hctx_next_cpu(hctx); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 817 | kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo); |
| 818 | } |
| 819 | } |
| 820 | EXPORT_SYMBOL(blk_mq_delay_queue); |
| 821 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 822 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 823 | struct request *rq, bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 824 | { |
| 825 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 826 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 827 | trace_block_rq_insert(hctx->queue, rq); |
| 828 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 829 | if (at_head) |
| 830 | list_add(&rq->queuelist, &ctx->rq_list); |
| 831 | else |
| 832 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 833 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 834 | |
| 835 | /* |
| 836 | * We do this early, to ensure we are on the right CPU. |
| 837 | */ |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 838 | blk_add_timer(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 839 | } |
| 840 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 841 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
| 842 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 843 | { |
| 844 | struct request_queue *q = rq->q; |
| 845 | struct blk_mq_hw_ctx *hctx; |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 846 | struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 847 | |
| 848 | current_ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 849 | if (!cpu_online(ctx->cpu)) |
| 850 | rq->mq_ctx = ctx = current_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 851 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 852 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 853 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 854 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) && |
| 855 | !(rq->cmd_flags & (REQ_FLUSH_SEQ))) { |
| 856 | blk_insert_flush(rq); |
| 857 | } else { |
| 858 | spin_lock(&ctx->lock); |
| 859 | __blk_mq_insert_request(hctx, rq, at_head); |
| 860 | spin_unlock(&ctx->lock); |
| 861 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 862 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 863 | if (run_queue) |
| 864 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 865 | |
| 866 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static void blk_mq_insert_requests(struct request_queue *q, |
| 870 | struct blk_mq_ctx *ctx, |
| 871 | struct list_head *list, |
| 872 | int depth, |
| 873 | bool from_schedule) |
| 874 | |
| 875 | { |
| 876 | struct blk_mq_hw_ctx *hctx; |
| 877 | struct blk_mq_ctx *current_ctx; |
| 878 | |
| 879 | trace_block_unplug(q, depth, !from_schedule); |
| 880 | |
| 881 | current_ctx = blk_mq_get_ctx(q); |
| 882 | |
| 883 | if (!cpu_online(ctx->cpu)) |
| 884 | ctx = current_ctx; |
| 885 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 886 | |
| 887 | /* |
| 888 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 889 | * offline now |
| 890 | */ |
| 891 | spin_lock(&ctx->lock); |
| 892 | while (!list_empty(list)) { |
| 893 | struct request *rq; |
| 894 | |
| 895 | rq = list_first_entry(list, struct request, queuelist); |
| 896 | list_del_init(&rq->queuelist); |
| 897 | rq->mq_ctx = ctx; |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 898 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 899 | } |
| 900 | spin_unlock(&ctx->lock); |
| 901 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 902 | blk_mq_run_hw_queue(hctx, from_schedule); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 903 | blk_mq_put_ctx(current_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 907 | { |
| 908 | struct request *rqa = container_of(a, struct request, queuelist); |
| 909 | struct request *rqb = container_of(b, struct request, queuelist); |
| 910 | |
| 911 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 912 | (rqa->mq_ctx == rqb->mq_ctx && |
| 913 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 914 | } |
| 915 | |
| 916 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 917 | { |
| 918 | struct blk_mq_ctx *this_ctx; |
| 919 | struct request_queue *this_q; |
| 920 | struct request *rq; |
| 921 | LIST_HEAD(list); |
| 922 | LIST_HEAD(ctx_list); |
| 923 | unsigned int depth; |
| 924 | |
| 925 | list_splice_init(&plug->mq_list, &list); |
| 926 | |
| 927 | list_sort(NULL, &list, plug_ctx_cmp); |
| 928 | |
| 929 | this_q = NULL; |
| 930 | this_ctx = NULL; |
| 931 | depth = 0; |
| 932 | |
| 933 | while (!list_empty(&list)) { |
| 934 | rq = list_entry_rq(list.next); |
| 935 | list_del_init(&rq->queuelist); |
| 936 | BUG_ON(!rq->q); |
| 937 | if (rq->mq_ctx != this_ctx) { |
| 938 | if (this_ctx) { |
| 939 | blk_mq_insert_requests(this_q, this_ctx, |
| 940 | &ctx_list, depth, |
| 941 | from_schedule); |
| 942 | } |
| 943 | |
| 944 | this_ctx = rq->mq_ctx; |
| 945 | this_q = rq->q; |
| 946 | depth = 0; |
| 947 | } |
| 948 | |
| 949 | depth++; |
| 950 | list_add_tail(&rq->queuelist, &ctx_list); |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * If 'this_ctx' is set, we know we have entries to complete |
| 955 | * on 'ctx_list'. Do those. |
| 956 | */ |
| 957 | if (this_ctx) { |
| 958 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 959 | from_schedule); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 964 | { |
| 965 | init_request_from_bio(rq, bio); |
| 966 | blk_account_io_start(rq, 1); |
| 967 | } |
| 968 | |
| 969 | static void blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 970 | { |
| 971 | struct blk_mq_hw_ctx *hctx; |
| 972 | struct blk_mq_ctx *ctx; |
| 973 | const int is_sync = rw_is_sync(bio->bi_rw); |
| 974 | const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA); |
| 975 | int rw = bio_data_dir(bio); |
| 976 | struct request *rq; |
| 977 | unsigned int use_plug, request_count = 0; |
| 978 | |
| 979 | /* |
| 980 | * If we have multiple hardware queues, just go directly to |
| 981 | * one of those for sync IO. |
| 982 | */ |
| 983 | use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync); |
| 984 | |
| 985 | blk_queue_bounce(q, &bio); |
| 986 | |
Nicholas Bellinger | 14ec77f | 2014-02-07 13:45:39 -0700 | [diff] [blame] | 987 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
| 988 | bio_endio(bio, -EIO); |
| 989 | return; |
| 990 | } |
| 991 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 992 | if (use_plug && blk_attempt_plug_merge(q, bio, &request_count)) |
| 993 | return; |
| 994 | |
| 995 | if (blk_mq_queue_enter(q)) { |
| 996 | bio_endio(bio, -EIO); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | ctx = blk_mq_get_ctx(q); |
| 1001 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1002 | |
Shaohua Li | 27fbf4e | 2014-02-19 20:20:21 +0800 | [diff] [blame] | 1003 | if (is_sync) |
| 1004 | rw |= REQ_SYNC; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1005 | trace_block_getrq(q, bio, rw); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1006 | rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1007 | if (likely(rq)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1008 | blk_mq_rq_ctx_init(q, ctx, rq, rw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1009 | else { |
| 1010 | blk_mq_put_ctx(ctx); |
| 1011 | trace_block_sleeprq(q, bio, rw); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1012 | rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC, |
| 1013 | false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1014 | ctx = rq->mq_ctx; |
| 1015 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
| 1016 | } |
| 1017 | |
| 1018 | hctx->queued++; |
| 1019 | |
| 1020 | if (unlikely(is_flush_fua)) { |
| 1021 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1022 | blk_insert_flush(rq); |
| 1023 | goto run_queue; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * A task plug currently exists. Since this is completely lockless, |
| 1028 | * utilize that to temporarily store requests until the task is |
| 1029 | * either done or scheduled away. |
| 1030 | */ |
| 1031 | if (use_plug) { |
| 1032 | struct blk_plug *plug = current->plug; |
| 1033 | |
| 1034 | if (plug) { |
| 1035 | blk_mq_bio_to_request(rq, bio); |
Shaohua Li | 92f399c | 2013-10-29 12:01:03 -0600 | [diff] [blame] | 1036 | if (list_empty(&plug->mq_list)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1037 | trace_block_plug(q); |
| 1038 | else if (request_count >= BLK_MAX_REQUEST_COUNT) { |
| 1039 | blk_flush_plug_list(plug, false); |
| 1040 | trace_block_plug(q); |
| 1041 | } |
| 1042 | list_add_tail(&rq->queuelist, &plug->mq_list); |
| 1043 | blk_mq_put_ctx(ctx); |
| 1044 | return; |
| 1045 | } |
| 1046 | } |
| 1047 | |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1048 | if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) { |
| 1049 | init_request_from_bio(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1050 | |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1051 | spin_lock(&ctx->lock); |
| 1052 | insert_rq: |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1053 | __blk_mq_insert_request(hctx, rq, false); |
Jens Axboe | c6d600c | 2014-04-30 13:43:56 -0600 | [diff] [blame] | 1054 | spin_unlock(&ctx->lock); |
| 1055 | blk_account_io_start(rq, 1); |
| 1056 | } else { |
| 1057 | spin_lock(&ctx->lock); |
| 1058 | if (!blk_mq_attempt_merge(q, ctx, bio)) { |
| 1059 | init_request_from_bio(rq, bio); |
| 1060 | goto insert_rq; |
| 1061 | } |
| 1062 | |
| 1063 | spin_unlock(&ctx->lock); |
| 1064 | __blk_mq_free_request(hctx, ctx, rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* |
| 1069 | * For a SYNC request, send it to the hardware immediately. For an |
| 1070 | * ASYNC request, just ensure that we run it later on. The latter |
| 1071 | * allows for merging opportunities and more efficient dispatching. |
| 1072 | */ |
| 1073 | run_queue: |
| 1074 | blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1075 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Default mapping to a software queue, since we use one per CPU. |
| 1080 | */ |
| 1081 | struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu) |
| 1082 | { |
| 1083 | return q->queue_hw_ctx[q->mq_map[cpu]]; |
| 1084 | } |
| 1085 | EXPORT_SYMBOL(blk_mq_map_queue); |
| 1086 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1087 | 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] | 1088 | unsigned int hctx_index) |
| 1089 | { |
| 1090 | return kmalloc_node(sizeof(struct blk_mq_hw_ctx), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1091 | GFP_KERNEL | __GFP_ZERO, set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1092 | } |
| 1093 | EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); |
| 1094 | |
| 1095 | void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx, |
| 1096 | unsigned int hctx_index) |
| 1097 | { |
| 1098 | kfree(hctx); |
| 1099 | } |
| 1100 | EXPORT_SYMBOL(blk_mq_free_single_hw_queue); |
| 1101 | |
| 1102 | static void blk_mq_hctx_notify(void *data, unsigned long action, |
| 1103 | unsigned int cpu) |
| 1104 | { |
| 1105 | struct blk_mq_hw_ctx *hctx = data; |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1106 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1107 | struct blk_mq_ctx *ctx; |
| 1108 | LIST_HEAD(tmp); |
| 1109 | |
| 1110 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
| 1111 | return; |
| 1112 | |
| 1113 | /* |
| 1114 | * Move ctx entries to new CPU, if this one is going away. |
| 1115 | */ |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1116 | ctx = __blk_mq_get_ctx(q, cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1117 | |
| 1118 | spin_lock(&ctx->lock); |
| 1119 | if (!list_empty(&ctx->rq_list)) { |
| 1120 | list_splice_init(&ctx->rq_list, &tmp); |
| 1121 | clear_bit(ctx->index_hw, hctx->ctx_map); |
| 1122 | } |
| 1123 | spin_unlock(&ctx->lock); |
| 1124 | |
| 1125 | if (list_empty(&tmp)) |
| 1126 | return; |
| 1127 | |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1128 | ctx = blk_mq_get_ctx(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1129 | spin_lock(&ctx->lock); |
| 1130 | |
| 1131 | while (!list_empty(&tmp)) { |
| 1132 | struct request *rq; |
| 1133 | |
| 1134 | rq = list_first_entry(&tmp, struct request, queuelist); |
| 1135 | rq->mq_ctx = ctx; |
| 1136 | list_move_tail(&rq->queuelist, &ctx->rq_list); |
| 1137 | } |
| 1138 | |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1139 | hctx = q->mq_ops->map_queue(q, ctx->cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1140 | blk_mq_hctx_mark_pending(hctx, ctx); |
| 1141 | |
| 1142 | spin_unlock(&ctx->lock); |
Jens Axboe | bccb5f7 | 2014-04-04 21:34:48 -0600 | [diff] [blame] | 1143 | |
| 1144 | blk_mq_run_hw_queue(hctx, true); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1145 | blk_mq_put_ctx(ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1148 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
| 1149 | struct blk_mq_tags *tags, unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1150 | { |
| 1151 | struct page *page; |
| 1152 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1153 | if (tags->rqs && set->ops->exit_request) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1154 | int i; |
| 1155 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1156 | for (i = 0; i < tags->nr_tags; i++) { |
| 1157 | if (!tags->rqs[i]) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1158 | continue; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1159 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
| 1160 | hctx_idx, i); |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1164 | while (!list_empty(&tags->page_list)) { |
| 1165 | page = list_first_entry(&tags->page_list, struct page, lru); |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1166 | list_del_init(&page->lru); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1167 | __free_pages(page, page->private); |
| 1168 | } |
| 1169 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1170 | kfree(tags->rqs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1171 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1172 | blk_mq_free_tags(tags); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | static size_t order_to_size(unsigned int order) |
| 1176 | { |
Ming Lei | 4ca0850 | 2014-04-19 18:00:18 +0800 | [diff] [blame] | 1177 | return (size_t)PAGE_SIZE << order; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1180 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
| 1181 | unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1182 | { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1183 | struct blk_mq_tags *tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1184 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1185 | size_t rq_size, left; |
| 1186 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1187 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
| 1188 | set->numa_node); |
| 1189 | if (!tags) |
| 1190 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1191 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1192 | INIT_LIST_HEAD(&tags->page_list); |
| 1193 | |
| 1194 | tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *), |
| 1195 | GFP_KERNEL, set->numa_node); |
| 1196 | if (!tags->rqs) { |
| 1197 | blk_mq_free_tags(tags); |
| 1198 | return NULL; |
| 1199 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* |
| 1202 | * rq_size is the size of the request plus driver payload, rounded |
| 1203 | * to the cacheline size |
| 1204 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1205 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1206 | cache_line_size()); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1207 | left = rq_size * set->queue_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1208 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1209 | for (i = 0; i < set->queue_depth; ) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1210 | int this_order = max_order; |
| 1211 | struct page *page; |
| 1212 | int to_do; |
| 1213 | void *p; |
| 1214 | |
| 1215 | while (left < order_to_size(this_order - 1) && this_order) |
| 1216 | this_order--; |
| 1217 | |
| 1218 | do { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1219 | page = alloc_pages_node(set->numa_node, GFP_KERNEL, |
| 1220 | this_order); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1221 | if (page) |
| 1222 | break; |
| 1223 | if (!this_order--) |
| 1224 | break; |
| 1225 | if (order_to_size(this_order) < rq_size) |
| 1226 | break; |
| 1227 | } while (1); |
| 1228 | |
| 1229 | if (!page) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1230 | goto fail; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1231 | |
| 1232 | page->private = this_order; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1233 | list_add_tail(&page->lru, &tags->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1234 | |
| 1235 | p = page_address(page); |
| 1236 | entries_per_page = order_to_size(this_order) / rq_size; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1237 | to_do = min(entries_per_page, set->queue_depth - i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1238 | left -= to_do * rq_size; |
| 1239 | for (j = 0; j < to_do; j++) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1240 | tags->rqs[i] = p; |
| 1241 | if (set->ops->init_request) { |
| 1242 | if (set->ops->init_request(set->driver_data, |
| 1243 | tags->rqs[i], hctx_idx, i, |
| 1244 | set->numa_node)) |
| 1245 | goto fail; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1246 | } |
| 1247 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1248 | p += rq_size; |
| 1249 | i++; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1253 | return tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1254 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1255 | fail: |
| 1256 | pr_warn("%s: failed to allocate requests\n", __func__); |
| 1257 | blk_mq_free_rq_map(set, tags, hctx_idx); |
| 1258 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static int blk_mq_init_hw_queues(struct request_queue *q, |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1262 | struct blk_mq_tag_set *set) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1263 | { |
| 1264 | struct blk_mq_hw_ctx *hctx; |
| 1265 | unsigned int i, j; |
| 1266 | |
| 1267 | /* |
| 1268 | * Initialize hardware queues |
| 1269 | */ |
| 1270 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1271 | unsigned int num_maps; |
| 1272 | int node; |
| 1273 | |
| 1274 | node = hctx->numa_node; |
| 1275 | if (node == NUMA_NO_NODE) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1276 | node = hctx->numa_node = set->numa_node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1277 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1278 | INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn); |
| 1279 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1280 | spin_lock_init(&hctx->lock); |
| 1281 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1282 | hctx->queue = q; |
| 1283 | hctx->queue_num = i; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1284 | hctx->flags = set->flags; |
| 1285 | hctx->cmd_size = set->cmd_size; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1286 | |
| 1287 | blk_mq_init_cpu_notifier(&hctx->cpu_notifier, |
| 1288 | blk_mq_hctx_notify, hctx); |
| 1289 | blk_mq_register_cpu_notifier(&hctx->cpu_notifier); |
| 1290 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1291 | hctx->tags = set->tags[i]; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1292 | |
| 1293 | /* |
| 1294 | * Allocate space for all possible cpus to avoid allocation in |
| 1295 | * runtime |
| 1296 | */ |
| 1297 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1298 | GFP_KERNEL, node); |
| 1299 | if (!hctx->ctxs) |
| 1300 | break; |
| 1301 | |
| 1302 | num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG; |
| 1303 | hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long), |
| 1304 | GFP_KERNEL, node); |
| 1305 | if (!hctx->ctx_map) |
| 1306 | break; |
| 1307 | |
| 1308 | hctx->nr_ctx_map = num_maps; |
| 1309 | hctx->nr_ctx = 0; |
| 1310 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1311 | if (set->ops->init_hctx && |
| 1312 | set->ops->init_hctx(hctx, set->driver_data, i)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1313 | break; |
| 1314 | } |
| 1315 | |
| 1316 | if (i == q->nr_hw_queues) |
| 1317 | return 0; |
| 1318 | |
| 1319 | /* |
| 1320 | * Init failed |
| 1321 | */ |
| 1322 | queue_for_each_hw_ctx(q, hctx, j) { |
| 1323 | if (i == j) |
| 1324 | break; |
| 1325 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1326 | if (set->ops->exit_hctx) |
| 1327 | set->ops->exit_hctx(hctx, j); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1328 | |
| 1329 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1330 | kfree(hctx->ctxs); |
Ming Lei | 11471e0 | 2014-04-19 18:00:16 +0800 | [diff] [blame] | 1331 | kfree(hctx->ctx_map); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | return 1; |
| 1335 | } |
| 1336 | |
| 1337 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1338 | unsigned int nr_hw_queues) |
| 1339 | { |
| 1340 | unsigned int i; |
| 1341 | |
| 1342 | for_each_possible_cpu(i) { |
| 1343 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1344 | struct blk_mq_hw_ctx *hctx; |
| 1345 | |
| 1346 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1347 | __ctx->cpu = i; |
| 1348 | spin_lock_init(&__ctx->lock); |
| 1349 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1350 | __ctx->queue = q; |
| 1351 | |
| 1352 | /* 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] | 1353 | if (!cpu_online(i)) |
| 1354 | continue; |
| 1355 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1356 | hctx = q->mq_ops->map_queue(q, i); |
| 1357 | cpumask_set_cpu(i, hctx->cpumask); |
| 1358 | hctx->nr_ctx++; |
| 1359 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1360 | /* |
| 1361 | * Set local node, IFF we have more than one hw queue. If |
| 1362 | * not, we remain on the home node of the device |
| 1363 | */ |
| 1364 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
| 1365 | hctx->numa_node = cpu_to_node(i); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | static void blk_mq_map_swqueue(struct request_queue *q) |
| 1370 | { |
| 1371 | unsigned int i; |
| 1372 | struct blk_mq_hw_ctx *hctx; |
| 1373 | struct blk_mq_ctx *ctx; |
| 1374 | |
| 1375 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1376 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1377 | hctx->nr_ctx = 0; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * Map software to hardware queues |
| 1382 | */ |
| 1383 | queue_for_each_ctx(q, ctx, i) { |
| 1384 | /* 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] | 1385 | if (!cpu_online(i)) |
| 1386 | continue; |
| 1387 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1388 | hctx = q->mq_ops->map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1389 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1390 | ctx->index_hw = hctx->nr_ctx; |
| 1391 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1392 | } |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame^] | 1393 | |
| 1394 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1395 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
| 1396 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 1397 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1398 | } |
| 1399 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1400 | 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] | 1401 | { |
| 1402 | struct blk_mq_hw_ctx **hctxs; |
| 1403 | struct blk_mq_ctx *ctx; |
| 1404 | struct request_queue *q; |
| 1405 | int i; |
| 1406 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1407 | ctx = alloc_percpu(struct blk_mq_ctx); |
| 1408 | if (!ctx) |
| 1409 | return ERR_PTR(-ENOMEM); |
| 1410 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1411 | hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL, |
| 1412 | set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1413 | |
| 1414 | if (!hctxs) |
| 1415 | goto err_percpu; |
| 1416 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1417 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 1418 | hctxs[i] = set->ops->alloc_hctx(set, i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1419 | if (!hctxs[i]) |
| 1420 | goto err_hctxs; |
| 1421 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1422 | if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL)) |
| 1423 | goto err_hctxs; |
| 1424 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1425 | hctxs[i]->numa_node = NUMA_NO_NODE; |
| 1426 | hctxs[i]->queue_num = i; |
| 1427 | } |
| 1428 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1429 | q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1430 | if (!q) |
| 1431 | goto err_hctxs; |
| 1432 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1433 | q->mq_map = blk_mq_make_queue_map(set); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1434 | if (!q->mq_map) |
| 1435 | goto err_map; |
| 1436 | |
| 1437 | setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q); |
| 1438 | blk_queue_rq_timeout(q, 30000); |
| 1439 | |
| 1440 | q->nr_queues = nr_cpu_ids; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1441 | q->nr_hw_queues = set->nr_hw_queues; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1442 | |
| 1443 | q->queue_ctx = ctx; |
| 1444 | q->queue_hw_ctx = hctxs; |
| 1445 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1446 | q->mq_ops = set->ops; |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 1447 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1448 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 1449 | q->sg_reserved_size = INT_MAX; |
| 1450 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1451 | blk_queue_make_request(q, blk_mq_make_request); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 1452 | blk_queue_rq_timed_out(q, blk_mq_rq_timed_out); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1453 | if (set->timeout) |
| 1454 | blk_queue_rq_timeout(q, set->timeout); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1455 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1456 | if (set->ops->complete) |
| 1457 | blk_queue_softirq_done(q, set->ops->complete); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 1458 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1459 | blk_mq_init_flush(q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1460 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1461 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1462 | q->flush_rq = kzalloc(round_up(sizeof(struct request) + |
| 1463 | set->cmd_size, cache_line_size()), |
| 1464 | GFP_KERNEL); |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1465 | if (!q->flush_rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1466 | goto err_hw; |
| 1467 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1468 | if (blk_mq_init_hw_queues(q, set)) |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1469 | goto err_flush_rq; |
| 1470 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1471 | blk_mq_map_swqueue(q); |
| 1472 | |
| 1473 | mutex_lock(&all_q_mutex); |
| 1474 | list_add_tail(&q->all_q_node, &all_q_list); |
| 1475 | mutex_unlock(&all_q_mutex); |
| 1476 | |
| 1477 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 1478 | |
| 1479 | err_flush_rq: |
| 1480 | kfree(q->flush_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1481 | err_hw: |
| 1482 | kfree(q->mq_map); |
| 1483 | err_map: |
| 1484 | blk_cleanup_queue(q); |
| 1485 | err_hctxs: |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1486 | for (i = 0; i < set->nr_hw_queues; i++) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1487 | if (!hctxs[i]) |
| 1488 | break; |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1489 | free_cpumask_var(hctxs[i]->cpumask); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1490 | set->ops->free_hctx(hctxs[i], i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1491 | } |
| 1492 | kfree(hctxs); |
| 1493 | err_percpu: |
| 1494 | free_percpu(ctx); |
| 1495 | return ERR_PTR(-ENOMEM); |
| 1496 | } |
| 1497 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1498 | |
| 1499 | void blk_mq_free_queue(struct request_queue *q) |
| 1500 | { |
| 1501 | struct blk_mq_hw_ctx *hctx; |
| 1502 | int i; |
| 1503 | |
| 1504 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1505 | kfree(hctx->ctx_map); |
| 1506 | kfree(hctx->ctxs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1507 | blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier); |
| 1508 | if (q->mq_ops->exit_hctx) |
| 1509 | q->mq_ops->exit_hctx(hctx, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1510 | free_cpumask_var(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1511 | q->mq_ops->free_hctx(hctx, i); |
| 1512 | } |
| 1513 | |
| 1514 | free_percpu(q->queue_ctx); |
| 1515 | kfree(q->queue_hw_ctx); |
| 1516 | kfree(q->mq_map); |
| 1517 | |
| 1518 | q->queue_ctx = NULL; |
| 1519 | q->queue_hw_ctx = NULL; |
| 1520 | q->mq_map = NULL; |
| 1521 | |
| 1522 | mutex_lock(&all_q_mutex); |
| 1523 | list_del_init(&q->all_q_node); |
| 1524 | mutex_unlock(&all_q_mutex); |
| 1525 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1526 | |
| 1527 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1528 | static void blk_mq_queue_reinit(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1529 | { |
| 1530 | blk_mq_freeze_queue(q); |
| 1531 | |
| 1532 | blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues); |
| 1533 | |
| 1534 | /* |
| 1535 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 1536 | * we should change hctx numa_node according to new topology (this |
| 1537 | * involves free and re-allocate memory, worthy doing?) |
| 1538 | */ |
| 1539 | |
| 1540 | blk_mq_map_swqueue(q); |
| 1541 | |
| 1542 | blk_mq_unfreeze_queue(q); |
| 1543 | } |
| 1544 | |
Paul Gortmaker | f618ef7 | 2013-11-14 08:26:02 -0700 | [diff] [blame] | 1545 | static int blk_mq_queue_reinit_notify(struct notifier_block *nb, |
| 1546 | unsigned long action, void *hcpu) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1547 | { |
| 1548 | struct request_queue *q; |
| 1549 | |
| 1550 | /* |
| 1551 | * Before new mapping is established, hotadded cpu might already start |
| 1552 | * handling requests. This doesn't break anything as we map offline |
| 1553 | * CPUs to first hardware queue. We will re-init queue below to get |
| 1554 | * optimal settings. |
| 1555 | */ |
| 1556 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN && |
| 1557 | action != CPU_ONLINE && action != CPU_ONLINE_FROZEN) |
| 1558 | return NOTIFY_OK; |
| 1559 | |
| 1560 | mutex_lock(&all_q_mutex); |
| 1561 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 1562 | blk_mq_queue_reinit(q); |
| 1563 | mutex_unlock(&all_q_mutex); |
| 1564 | return NOTIFY_OK; |
| 1565 | } |
| 1566 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1567 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 1568 | { |
| 1569 | int i; |
| 1570 | |
| 1571 | if (!set->nr_hw_queues) |
| 1572 | return -EINVAL; |
| 1573 | if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH) |
| 1574 | return -EINVAL; |
| 1575 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 1576 | return -EINVAL; |
| 1577 | |
| 1578 | if (!set->nr_hw_queues || |
| 1579 | !set->ops->queue_rq || !set->ops->map_queue || |
| 1580 | !set->ops->alloc_hctx || !set->ops->free_hctx) |
| 1581 | return -EINVAL; |
| 1582 | |
| 1583 | |
Ming Lei | 4847900 | 2014-04-19 18:00:17 +0800 | [diff] [blame] | 1584 | set->tags = kmalloc_node(set->nr_hw_queues * |
| 1585 | sizeof(struct blk_mq_tags *), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1586 | GFP_KERNEL, set->numa_node); |
| 1587 | if (!set->tags) |
| 1588 | goto out; |
| 1589 | |
| 1590 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 1591 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 1592 | if (!set->tags[i]) |
| 1593 | goto out_unwind; |
| 1594 | } |
| 1595 | |
| 1596 | return 0; |
| 1597 | |
| 1598 | out_unwind: |
| 1599 | while (--i >= 0) |
| 1600 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 1601 | out: |
| 1602 | return -ENOMEM; |
| 1603 | } |
| 1604 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 1605 | |
| 1606 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 1607 | { |
| 1608 | int i; |
| 1609 | |
| 1610 | for (i = 0; i < set->nr_hw_queues; i++) |
| 1611 | blk_mq_free_rq_map(set, set->tags[i], i); |
Ming Lei | 981bd18 | 2014-04-24 00:07:34 +0800 | [diff] [blame] | 1612 | kfree(set->tags); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1613 | } |
| 1614 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 1615 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 1616 | void blk_mq_disable_hotplug(void) |
| 1617 | { |
| 1618 | mutex_lock(&all_q_mutex); |
| 1619 | } |
| 1620 | |
| 1621 | void blk_mq_enable_hotplug(void) |
| 1622 | { |
| 1623 | mutex_unlock(&all_q_mutex); |
| 1624 | } |
| 1625 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1626 | static int __init blk_mq_init(void) |
| 1627 | { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1628 | blk_mq_cpu_init(); |
| 1629 | |
| 1630 | /* Must be called after percpu_counter_hotcpu_callback() */ |
| 1631 | hotcpu_notifier(blk_mq_queue_reinit_notify, -10); |
| 1632 | |
| 1633 | return 0; |
| 1634 | } |
| 1635 | subsys_initcall(blk_mq_init); |