Jens Axboe | 75bb462 | 2014-05-28 10:15:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Block multiqueue core code |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Jens Axboe |
| 5 | * Copyright (C) 2013-2014 Christoph Hellwig |
| 6 | */ |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/backing-dev.h> |
| 10 | #include <linux/bio.h> |
| 11 | #include <linux/blkdev.h> |
Catalin Marinas | f75782e | 2015-09-14 18:16:02 +0100 | [diff] [blame] | 12 | #include <linux/kmemleak.h> |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/workqueue.h> |
| 17 | #include <linux/smp.h> |
| 18 | #include <linux/llist.h> |
| 19 | #include <linux/list_sort.h> |
| 20 | #include <linux/cpu.h> |
| 21 | #include <linux/cache.h> |
| 22 | #include <linux/sched/sysctl.h> |
| 23 | #include <linux/delay.h> |
Jens Axboe | aedcd72 | 2014-09-17 08:27:03 -0600 | [diff] [blame] | 24 | #include <linux/crash_dump.h> |
Jens Axboe | 88c7b2b | 2016-08-25 08:07:30 -0600 | [diff] [blame] | 25 | #include <linux/prefetch.h> |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 26 | |
| 27 | #include <trace/events/block.h> |
| 28 | |
| 29 | #include <linux/blk-mq.h> |
| 30 | #include "blk.h" |
| 31 | #include "blk-mq.h" |
| 32 | #include "blk-mq-tag.h" |
| 33 | |
| 34 | static DEFINE_MUTEX(all_q_mutex); |
| 35 | static LIST_HEAD(all_q_list); |
| 36 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 37 | /* |
| 38 | * Check if any of the ctx's have pending work in this hardware queue |
| 39 | */ |
| 40 | static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx) |
| 41 | { |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 42 | return sbitmap_any_bit_set(&hctx->ctx_map); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Mark this ctx as having pending work in this hardware queue |
| 47 | */ |
| 48 | static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, |
| 49 | struct blk_mq_ctx *ctx) |
| 50 | { |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 51 | if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw)) |
| 52 | sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx, |
| 56 | struct blk_mq_ctx *ctx) |
| 57 | { |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 58 | sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Keith Busch | b4c6a02 | 2014-12-19 17:54:14 -0700 | [diff] [blame] | 61 | void blk_mq_freeze_queue_start(struct request_queue *q) |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 62 | { |
Christoph Hellwig | 4ecd4fe | 2015-05-07 09:38:13 +0200 | [diff] [blame] | 63 | int freeze_depth; |
Tejun Heo | cddd5d1 | 2014-08-16 08:02:24 -0400 | [diff] [blame] | 64 | |
Christoph Hellwig | 4ecd4fe | 2015-05-07 09:38:13 +0200 | [diff] [blame] | 65 | freeze_depth = atomic_inc_return(&q->mq_freeze_depth); |
| 66 | if (freeze_depth == 1) { |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 67 | percpu_ref_kill(&q->q_usage_counter); |
Mike Snitzer | b94ec29 | 2015-03-11 23:56:38 -0400 | [diff] [blame] | 68 | blk_mq_run_hw_queues(q, false); |
Tejun Heo | cddd5d1 | 2014-08-16 08:02:24 -0400 | [diff] [blame] | 69 | } |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 70 | } |
Keith Busch | b4c6a02 | 2014-12-19 17:54:14 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start); |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 72 | |
| 73 | static void blk_mq_freeze_queue_wait(struct request_queue *q) |
| 74 | { |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 75 | wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter)); |
Ming Lei | 43a5e4e | 2013-12-26 21:31:35 +0800 | [diff] [blame] | 76 | } |
| 77 | |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 78 | /* |
| 79 | * Guarantee no request is in use, so we can change any data structure of |
| 80 | * the queue afterward. |
| 81 | */ |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 82 | void blk_freeze_queue(struct request_queue *q) |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 83 | { |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 84 | /* |
| 85 | * In the !blk_mq case we are only calling this to kill the |
| 86 | * q_usage_counter, otherwise this increases the freeze depth |
| 87 | * and waits for it to return to zero. For this reason there is |
| 88 | * no blk_unfreeze_queue(), and blk_freeze_queue() is not |
| 89 | * exported to drivers as the only user for unfreeze is blk_mq. |
| 90 | */ |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 91 | blk_mq_freeze_queue_start(q); |
| 92 | blk_mq_freeze_queue_wait(q); |
| 93 | } |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 94 | |
| 95 | void blk_mq_freeze_queue(struct request_queue *q) |
| 96 | { |
| 97 | /* |
| 98 | * ...just an alias to keep freeze and unfreeze actions balanced |
| 99 | * in the blk_mq_* namespace |
| 100 | */ |
| 101 | blk_freeze_queue(q); |
| 102 | } |
Jens Axboe | c761d96 | 2015-01-02 15:05:12 -0700 | [diff] [blame] | 103 | EXPORT_SYMBOL_GPL(blk_mq_freeze_queue); |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 104 | |
Keith Busch | b4c6a02 | 2014-12-19 17:54:14 -0700 | [diff] [blame] | 105 | void blk_mq_unfreeze_queue(struct request_queue *q) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 106 | { |
Christoph Hellwig | 4ecd4fe | 2015-05-07 09:38:13 +0200 | [diff] [blame] | 107 | int freeze_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 108 | |
Christoph Hellwig | 4ecd4fe | 2015-05-07 09:38:13 +0200 | [diff] [blame] | 109 | freeze_depth = atomic_dec_return(&q->mq_freeze_depth); |
| 110 | WARN_ON_ONCE(freeze_depth < 0); |
| 111 | if (!freeze_depth) { |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 112 | percpu_ref_reinit(&q->q_usage_counter); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 113 | wake_up_all(&q->mq_freeze_wq); |
Tejun Heo | add703f | 2014-07-01 10:34:38 -0600 | [diff] [blame] | 114 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 115 | } |
Keith Busch | b4c6a02 | 2014-12-19 17:54:14 -0700 | [diff] [blame] | 116 | EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 117 | |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 118 | /** |
| 119 | * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished |
| 120 | * @q: request queue. |
| 121 | * |
| 122 | * Note: this function does not prevent that the struct request end_io() |
| 123 | * callback function is invoked. Additionally, it is not prevented that |
| 124 | * new queue_rq() calls occur unless the queue has been stopped first. |
| 125 | */ |
| 126 | void blk_mq_quiesce_queue(struct request_queue *q) |
| 127 | { |
| 128 | struct blk_mq_hw_ctx *hctx; |
| 129 | unsigned int i; |
| 130 | bool rcu = false; |
| 131 | |
| 132 | blk_mq_stop_hw_queues(q); |
| 133 | |
| 134 | queue_for_each_hw_ctx(q, hctx, i) { |
| 135 | if (hctx->flags & BLK_MQ_F_BLOCKING) |
| 136 | synchronize_srcu(&hctx->queue_rq_srcu); |
| 137 | else |
| 138 | rcu = true; |
| 139 | } |
| 140 | if (rcu) |
| 141 | synchronize_rcu(); |
| 142 | } |
| 143 | EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue); |
| 144 | |
Jens Axboe | aed3ea9 | 2014-12-22 14:04:42 -0700 | [diff] [blame] | 145 | void blk_mq_wake_waiters(struct request_queue *q) |
| 146 | { |
| 147 | struct blk_mq_hw_ctx *hctx; |
| 148 | unsigned int i; |
| 149 | |
| 150 | queue_for_each_hw_ctx(q, hctx, i) |
| 151 | if (blk_mq_hw_queue_mapped(hctx)) |
| 152 | blk_mq_tag_wakeup_all(hctx->tags, true); |
Keith Busch | 3fd5940 | 2015-01-08 08:53:56 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * If we are called because the queue has now been marked as |
| 156 | * dying, we need to ensure that processes currently waiting on |
| 157 | * the queue are notified as well. |
| 158 | */ |
| 159 | wake_up_all(&q->mq_freeze_wq); |
Jens Axboe | aed3ea9 | 2014-12-22 14:04:42 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 162 | bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx) |
| 163 | { |
| 164 | return blk_mq_has_free_tags(hctx->tags); |
| 165 | } |
| 166 | EXPORT_SYMBOL(blk_mq_can_queue); |
| 167 | |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 168 | static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx, |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 169 | struct request *rq, unsigned int op) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 170 | { |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 171 | INIT_LIST_HEAD(&rq->queuelist); |
| 172 | /* csd/requeue_work/fifo_time is initialized before use */ |
| 173 | rq->q = q; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 174 | rq->mq_ctx = ctx; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 175 | rq->cmd_flags = op; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 176 | if (blk_queue_io_stat(q)) |
| 177 | rq->rq_flags |= RQF_IO_STAT; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 178 | /* do not touch atomic flags, it needs atomic ops against the timer */ |
| 179 | rq->cpu = -1; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 180 | INIT_HLIST_NODE(&rq->hash); |
| 181 | RB_CLEAR_NODE(&rq->rb_node); |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 182 | rq->rq_disk = NULL; |
| 183 | rq->part = NULL; |
Jens Axboe | 3ee3237 | 2014-06-09 09:36:53 -0600 | [diff] [blame] | 184 | rq->start_time = jiffies; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 185 | #ifdef CONFIG_BLK_CGROUP |
| 186 | rq->rl = NULL; |
Ming Lei | 0fec08b | 2014-01-03 10:00:08 -0700 | [diff] [blame] | 187 | set_start_time_ns(rq); |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 188 | rq->io_start_time_ns = 0; |
| 189 | #endif |
| 190 | rq->nr_phys_segments = 0; |
| 191 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 192 | rq->nr_integrity_segments = 0; |
| 193 | #endif |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 194 | rq->special = NULL; |
| 195 | /* tag was already set */ |
| 196 | rq->errors = 0; |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 197 | |
Tony Battersby | 6f4a1626 | 2014-08-22 15:53:39 -0400 | [diff] [blame] | 198 | rq->cmd = rq->__cmd; |
| 199 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 200 | rq->extra_len = 0; |
| 201 | rq->sense_len = 0; |
| 202 | rq->resid_len = 0; |
| 203 | rq->sense = NULL; |
| 204 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 205 | INIT_LIST_HEAD(&rq->timeout_list); |
Jens Axboe | f6be4fb | 2014-06-06 11:03:48 -0600 | [diff] [blame] | 206 | rq->timeout = 0; |
| 207 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 208 | rq->end_io = NULL; |
| 209 | rq->end_io_data = NULL; |
| 210 | rq->next_rq = NULL; |
| 211 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 212 | ctx->rq_dispatched[op_is_sync(op)]++; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 215 | static struct request * |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 216 | __blk_mq_alloc_request(struct blk_mq_alloc_data *data, unsigned int op) |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 217 | { |
| 218 | struct request *rq; |
| 219 | unsigned int tag; |
| 220 | |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 221 | tag = blk_mq_get_tag(data); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 222 | if (tag != BLK_MQ_TAG_FAIL) { |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 223 | rq = data->hctx->tags->rqs[tag]; |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 224 | |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 225 | if (blk_mq_tag_busy(data->hctx)) { |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 226 | rq->rq_flags = RQF_MQ_INFLIGHT; |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 227 | atomic_inc(&data->hctx->nr_active); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | rq->tag = tag; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 231 | blk_mq_rq_ctx_init(data->q, data->ctx, rq, op); |
Christoph Hellwig | 5dee857 | 2014-05-27 20:59:47 +0200 | [diff] [blame] | 232 | return rq; |
| 233 | } |
| 234 | |
| 235 | return NULL; |
| 236 | } |
| 237 | |
Christoph Hellwig | 6f3b0e8 | 2015-11-26 09:13:05 +0100 | [diff] [blame] | 238 | struct request *blk_mq_alloc_request(struct request_queue *q, int rw, |
| 239 | unsigned int flags) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 240 | { |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 241 | struct blk_mq_ctx *ctx; |
| 242 | struct blk_mq_hw_ctx *hctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 243 | struct request *rq; |
Ming Lei | cb96a42 | 2014-06-01 00:43:37 +0800 | [diff] [blame] | 244 | struct blk_mq_alloc_data alloc_data; |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 245 | int ret; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 246 | |
Christoph Hellwig | 6f3b0e8 | 2015-11-26 09:13:05 +0100 | [diff] [blame] | 247 | ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT); |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 248 | if (ret) |
| 249 | return ERR_PTR(ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 250 | |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 251 | ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 252 | hctx = blk_mq_map_queue(q, ctx->cpu); |
Christoph Hellwig | 6f3b0e8 | 2015-11-26 09:13:05 +0100 | [diff] [blame] | 253 | blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 254 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
Christoph Hellwig | d852564 | 2014-05-27 20:59:50 +0200 | [diff] [blame] | 255 | blk_mq_put_ctx(ctx); |
Jens Axboe | 841bac2 | 2016-09-21 10:08:43 -0600 | [diff] [blame] | 256 | |
Keith Busch | c76541a | 2014-12-19 17:54:13 -0700 | [diff] [blame] | 257 | if (!rq) { |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 258 | blk_queue_exit(q); |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 259 | return ERR_PTR(-EWOULDBLOCK); |
Keith Busch | c76541a | 2014-12-19 17:54:13 -0700 | [diff] [blame] | 260 | } |
Christoph Hellwig | 0c4de0f | 2016-07-19 11:31:50 +0200 | [diff] [blame] | 261 | |
| 262 | rq->__data_len = 0; |
| 263 | rq->__sector = (sector_t) -1; |
| 264 | rq->bio = rq->biotail = NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 265 | return rq; |
| 266 | } |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 267 | EXPORT_SYMBOL(blk_mq_alloc_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 268 | |
Ming Lin | 1f5bd33 | 2016-06-13 16:45:21 +0200 | [diff] [blame] | 269 | struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw, |
| 270 | unsigned int flags, unsigned int hctx_idx) |
| 271 | { |
| 272 | struct blk_mq_hw_ctx *hctx; |
| 273 | struct blk_mq_ctx *ctx; |
| 274 | struct request *rq; |
| 275 | struct blk_mq_alloc_data alloc_data; |
| 276 | int ret; |
| 277 | |
| 278 | /* |
| 279 | * If the tag allocator sleeps we could get an allocation for a |
| 280 | * different hardware context. No need to complicate the low level |
| 281 | * allocator for this for the rare use case of a command tied to |
| 282 | * a specific queue. |
| 283 | */ |
| 284 | if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT))) |
| 285 | return ERR_PTR(-EINVAL); |
| 286 | |
| 287 | if (hctx_idx >= q->nr_hw_queues) |
| 288 | return ERR_PTR(-EIO); |
| 289 | |
| 290 | ret = blk_queue_enter(q, true); |
| 291 | if (ret) |
| 292 | return ERR_PTR(ret); |
| 293 | |
Christoph Hellwig | c8712c6 | 2016-09-23 10:25:48 -0600 | [diff] [blame] | 294 | /* |
| 295 | * Check if the hardware context is actually mapped to anything. |
| 296 | * If not tell the caller that it should skip this queue. |
| 297 | */ |
Ming Lin | 1f5bd33 | 2016-06-13 16:45:21 +0200 | [diff] [blame] | 298 | hctx = q->queue_hw_ctx[hctx_idx]; |
Christoph Hellwig | c8712c6 | 2016-09-23 10:25:48 -0600 | [diff] [blame] | 299 | if (!blk_mq_hw_queue_mapped(hctx)) { |
| 300 | ret = -EXDEV; |
| 301 | goto out_queue_exit; |
| 302 | } |
Ming Lin | 1f5bd33 | 2016-06-13 16:45:21 +0200 | [diff] [blame] | 303 | ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask)); |
| 304 | |
| 305 | blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 306 | rq = __blk_mq_alloc_request(&alloc_data, rw); |
Ming Lin | 1f5bd33 | 2016-06-13 16:45:21 +0200 | [diff] [blame] | 307 | if (!rq) { |
Christoph Hellwig | c8712c6 | 2016-09-23 10:25:48 -0600 | [diff] [blame] | 308 | ret = -EWOULDBLOCK; |
| 309 | goto out_queue_exit; |
Ming Lin | 1f5bd33 | 2016-06-13 16:45:21 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return rq; |
Christoph Hellwig | c8712c6 | 2016-09-23 10:25:48 -0600 | [diff] [blame] | 313 | |
| 314 | out_queue_exit: |
| 315 | blk_queue_exit(q); |
| 316 | return ERR_PTR(ret); |
Ming Lin | 1f5bd33 | 2016-06-13 16:45:21 +0200 | [diff] [blame] | 317 | } |
| 318 | EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx); |
| 319 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 320 | static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, |
| 321 | struct blk_mq_ctx *ctx, struct request *rq) |
| 322 | { |
| 323 | const int tag = rq->tag; |
| 324 | struct request_queue *q = rq->q; |
| 325 | |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 326 | if (rq->rq_flags & RQF_MQ_INFLIGHT) |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 327 | atomic_dec(&hctx->nr_active); |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 328 | rq->rq_flags = 0; |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 329 | |
Christoph Hellwig | af76e55 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 330 | clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 331 | blk_mq_put_tag(hctx, ctx, tag); |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 332 | blk_queue_exit(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Jens Axboe | 7c7f2f2 | 2014-11-17 10:41:57 -0700 | [diff] [blame] | 335 | void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 336 | { |
| 337 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
Jens Axboe | 7c7f2f2 | 2014-11-17 10:41:57 -0700 | [diff] [blame] | 338 | |
| 339 | ctx->rq_completed[rq_is_sync(rq)]++; |
| 340 | __blk_mq_free_request(hctx, ctx, rq); |
| 341 | |
| 342 | } |
| 343 | EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request); |
| 344 | |
| 345 | void blk_mq_free_request(struct request *rq) |
| 346 | { |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 347 | blk_mq_free_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 348 | } |
Jens Axboe | 1a3b595 | 2014-11-17 10:40:48 -0700 | [diff] [blame] | 349 | EXPORT_SYMBOL_GPL(blk_mq_free_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 350 | |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 351 | inline void __blk_mq_end_request(struct request *rq, int error) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 352 | { |
Ming Lei | 0d11e6a | 2013-12-05 10:50:39 -0700 | [diff] [blame] | 353 | blk_account_io_done(rq); |
| 354 | |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 355 | if (rq->end_io) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 356 | rq->end_io(rq, error); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 357 | } else { |
| 358 | if (unlikely(blk_bidi_rq(rq))) |
| 359 | blk_mq_free_request(rq->next_rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 360 | blk_mq_free_request(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 361 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 362 | } |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 363 | EXPORT_SYMBOL(__blk_mq_end_request); |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 364 | |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 365 | void blk_mq_end_request(struct request *rq, int error) |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 366 | { |
| 367 | if (blk_update_request(rq, error, blk_rq_bytes(rq))) |
| 368 | BUG(); |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 369 | __blk_mq_end_request(rq, error); |
Christoph Hellwig | 63151a4 | 2014-04-16 09:44:52 +0200 | [diff] [blame] | 370 | } |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 371 | EXPORT_SYMBOL(blk_mq_end_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 372 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 373 | static void __blk_mq_complete_request_remote(void *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 374 | { |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 375 | struct request *rq = data; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 376 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 377 | rq->q->softirq_done_fn(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 380 | static void blk_mq_ipi_complete_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 381 | { |
| 382 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 383 | bool shared = false; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 384 | int cpu; |
| 385 | |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 386 | if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 387 | rq->q->softirq_done_fn(rq); |
| 388 | return; |
| 389 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 390 | |
| 391 | cpu = get_cpu(); |
Christoph Hellwig | 3853520 | 2014-04-25 02:32:53 -0700 | [diff] [blame] | 392 | if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags)) |
| 393 | shared = cpus_share_cache(cpu, ctx->cpu); |
| 394 | |
| 395 | if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 396 | rq->csd.func = __blk_mq_complete_request_remote; |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 397 | rq->csd.info = rq; |
| 398 | rq->csd.flags = 0; |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 399 | smp_call_function_single_async(ctx->cpu, &rq->csd); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 400 | } else { |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 401 | rq->q->softirq_done_fn(rq); |
Christoph Hellwig | 3d6efbf | 2014-01-08 09:33:37 -0800 | [diff] [blame] | 402 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 403 | put_cpu(); |
| 404 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 405 | |
Jens Axboe | 1fa8cc5 | 2015-11-05 14:32:55 -0700 | [diff] [blame] | 406 | static void __blk_mq_complete_request(struct request *rq) |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 407 | { |
| 408 | struct request_queue *q = rq->q; |
| 409 | |
| 410 | if (!q->softirq_done_fn) |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 411 | blk_mq_end_request(rq, rq->errors); |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 412 | else |
| 413 | blk_mq_ipi_complete_request(rq); |
| 414 | } |
| 415 | |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 416 | /** |
| 417 | * blk_mq_complete_request - end I/O on a request |
| 418 | * @rq: the request being processed |
| 419 | * |
| 420 | * Description: |
| 421 | * Ends all I/O on a request. It does not handle partial completions. |
| 422 | * The actual completion happens out-of-order, through a IPI handler. |
| 423 | **/ |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 424 | void blk_mq_complete_request(struct request *rq, int error) |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 425 | { |
Jens Axboe | 95f0968 | 2014-05-27 17:46:48 -0600 | [diff] [blame] | 426 | struct request_queue *q = rq->q; |
| 427 | |
| 428 | if (unlikely(blk_should_fake_timeout(q))) |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 429 | return; |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 430 | if (!blk_mark_rq_complete(rq)) { |
| 431 | rq->errors = error; |
Jens Axboe | ed85186 | 2014-05-30 21:20:50 -0600 | [diff] [blame] | 432 | __blk_mq_complete_request(rq); |
Christoph Hellwig | f4829a9 | 2015-09-27 21:01:50 +0200 | [diff] [blame] | 433 | } |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 434 | } |
| 435 | EXPORT_SYMBOL(blk_mq_complete_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 436 | |
Keith Busch | 973c019 | 2015-01-07 18:55:43 -0700 | [diff] [blame] | 437 | int blk_mq_request_started(struct request *rq) |
| 438 | { |
| 439 | return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(blk_mq_request_started); |
| 442 | |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 443 | void blk_mq_start_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 444 | { |
| 445 | struct request_queue *q = rq->q; |
| 446 | |
| 447 | trace_block_rq_issue(q, rq); |
| 448 | |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 449 | rq->resid_len = blk_rq_bytes(rq); |
Christoph Hellwig | 91b6363 | 2014-04-16 09:44:53 +0200 | [diff] [blame] | 450 | if (unlikely(blk_bidi_rq(rq))) |
| 451 | rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq); |
Christoph Hellwig | 742ee69 | 2014-04-14 10:30:06 +0200 | [diff] [blame] | 452 | |
Ming Lei | 2b8393b | 2014-06-10 00:16:41 +0800 | [diff] [blame] | 453 | blk_add_timer(rq); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 454 | |
| 455 | /* |
Jens Axboe | 538b753 | 2014-09-16 10:37:37 -0600 | [diff] [blame] | 456 | * Ensure that ->deadline is visible before set the started |
| 457 | * flag and clear the completed flag. |
| 458 | */ |
| 459 | smp_mb__before_atomic(); |
| 460 | |
| 461 | /* |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 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 | 4b57052 | 2014-05-29 11:00:11 -0600 | [diff] [blame] | 467 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) |
| 468 | set_bit(REQ_ATOM_STARTED, &rq->atomic_flags); |
| 469 | if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags)) |
| 470 | clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 471 | |
| 472 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
| 473 | /* |
| 474 | * Make sure space for the drain appears. We know we can do |
| 475 | * this because max_hw_segments has been adjusted to be one |
| 476 | * fewer than the device can handle. |
| 477 | */ |
| 478 | rq->nr_phys_segments++; |
| 479 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 480 | } |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 481 | EXPORT_SYMBOL(blk_mq_start_request); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 482 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 483 | static void __blk_mq_requeue_request(struct request *rq) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 484 | { |
| 485 | struct request_queue *q = rq->q; |
| 486 | |
| 487 | trace_block_rq_requeue(q, rq); |
Christoph Hellwig | 49f5baa | 2014-02-11 08:27:14 -0800 | [diff] [blame] | 488 | |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 489 | if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { |
| 490 | if (q->dma_drain_size && blk_rq_bytes(rq)) |
| 491 | rq->nr_phys_segments--; |
| 492 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Bart Van Assche | 2b053ac | 2016-10-28 17:21:41 -0700 | [diff] [blame] | 495 | void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list) |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 496 | { |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 497 | __blk_mq_requeue_request(rq); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 498 | |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 499 | BUG_ON(blk_queued_rq(rq)); |
Bart Van Assche | 2b053ac | 2016-10-28 17:21:41 -0700 | [diff] [blame] | 500 | blk_mq_add_to_requeue_list(rq, true, kick_requeue_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 501 | } |
| 502 | EXPORT_SYMBOL(blk_mq_requeue_request); |
| 503 | |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 504 | static void blk_mq_requeue_work(struct work_struct *work) |
| 505 | { |
| 506 | struct request_queue *q = |
Mike Snitzer | 2849450 | 2016-09-14 13:28:30 -0400 | [diff] [blame] | 507 | container_of(work, struct request_queue, requeue_work.work); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 508 | LIST_HEAD(rq_list); |
| 509 | struct request *rq, *next; |
| 510 | unsigned long flags; |
| 511 | |
| 512 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 513 | list_splice_init(&q->requeue_list, &rq_list); |
| 514 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 515 | |
| 516 | list_for_each_entry_safe(rq, next, &rq_list, queuelist) { |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 517 | if (!(rq->rq_flags & RQF_SOFTBARRIER)) |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 518 | continue; |
| 519 | |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 520 | rq->rq_flags &= ~RQF_SOFTBARRIER; |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 521 | list_del_init(&rq->queuelist); |
| 522 | blk_mq_insert_request(rq, true, false, false); |
| 523 | } |
| 524 | |
| 525 | while (!list_empty(&rq_list)) { |
| 526 | rq = list_entry(rq_list.next, struct request, queuelist); |
| 527 | list_del_init(&rq->queuelist); |
| 528 | blk_mq_insert_request(rq, false, false, false); |
| 529 | } |
| 530 | |
Bart Van Assche | 52d7f1b | 2016-10-28 17:20:32 -0700 | [diff] [blame] | 531 | blk_mq_run_hw_queues(q, false); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 532 | } |
| 533 | |
Bart Van Assche | 2b053ac | 2016-10-28 17:21:41 -0700 | [diff] [blame] | 534 | void blk_mq_add_to_requeue_list(struct request *rq, bool at_head, |
| 535 | bool kick_requeue_list) |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 536 | { |
| 537 | struct request_queue *q = rq->q; |
| 538 | unsigned long flags; |
| 539 | |
| 540 | /* |
| 541 | * We abuse this flag that is otherwise used by the I/O scheduler to |
| 542 | * request head insertation from the workqueue. |
| 543 | */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 544 | BUG_ON(rq->rq_flags & RQF_SOFTBARRIER); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 545 | |
| 546 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 547 | if (at_head) { |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 548 | rq->rq_flags |= RQF_SOFTBARRIER; |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 549 | list_add(&rq->queuelist, &q->requeue_list); |
| 550 | } else { |
| 551 | list_add_tail(&rq->queuelist, &q->requeue_list); |
| 552 | } |
| 553 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
Bart Van Assche | 2b053ac | 2016-10-28 17:21:41 -0700 | [diff] [blame] | 554 | |
| 555 | if (kick_requeue_list) |
| 556 | blk_mq_kick_requeue_list(q); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 557 | } |
| 558 | EXPORT_SYMBOL(blk_mq_add_to_requeue_list); |
| 559 | |
| 560 | void blk_mq_kick_requeue_list(struct request_queue *q) |
| 561 | { |
Mike Snitzer | 2849450 | 2016-09-14 13:28:30 -0400 | [diff] [blame] | 562 | kblockd_schedule_delayed_work(&q->requeue_work, 0); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 563 | } |
| 564 | EXPORT_SYMBOL(blk_mq_kick_requeue_list); |
| 565 | |
Mike Snitzer | 2849450 | 2016-09-14 13:28:30 -0400 | [diff] [blame] | 566 | void blk_mq_delay_kick_requeue_list(struct request_queue *q, |
| 567 | unsigned long msecs) |
| 568 | { |
| 569 | kblockd_schedule_delayed_work(&q->requeue_work, |
| 570 | msecs_to_jiffies(msecs)); |
| 571 | } |
| 572 | EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list); |
| 573 | |
Jens Axboe | 1885b24 | 2015-01-07 18:55:45 -0700 | [diff] [blame] | 574 | void blk_mq_abort_requeue_list(struct request_queue *q) |
| 575 | { |
| 576 | unsigned long flags; |
| 577 | LIST_HEAD(rq_list); |
| 578 | |
| 579 | spin_lock_irqsave(&q->requeue_lock, flags); |
| 580 | list_splice_init(&q->requeue_list, &rq_list); |
| 581 | spin_unlock_irqrestore(&q->requeue_lock, flags); |
| 582 | |
| 583 | while (!list_empty(&rq_list)) { |
| 584 | struct request *rq; |
| 585 | |
| 586 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 587 | list_del_init(&rq->queuelist); |
| 588 | rq->errors = -EIO; |
| 589 | blk_mq_end_request(rq, rq->errors); |
| 590 | } |
| 591 | } |
| 592 | EXPORT_SYMBOL(blk_mq_abort_requeue_list); |
| 593 | |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 594 | struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag) |
| 595 | { |
Jens Axboe | 88c7b2b | 2016-08-25 08:07:30 -0600 | [diff] [blame] | 596 | if (tag < tags->nr_tags) { |
| 597 | prefetch(tags->rqs[tag]); |
Hannes Reinecke | 4ee86ba | 2016-03-15 12:03:28 -0700 | [diff] [blame] | 598 | return tags->rqs[tag]; |
Jens Axboe | 88c7b2b | 2016-08-25 08:07:30 -0600 | [diff] [blame] | 599 | } |
Hannes Reinecke | 4ee86ba | 2016-03-15 12:03:28 -0700 | [diff] [blame] | 600 | |
| 601 | return NULL; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 602 | } |
| 603 | EXPORT_SYMBOL(blk_mq_tag_to_rq); |
| 604 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 605 | struct blk_mq_timeout_data { |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 606 | unsigned long next; |
| 607 | unsigned int next_set; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 608 | }; |
| 609 | |
Christoph Hellwig | 9041583 | 2014-09-22 10:21:48 -0600 | [diff] [blame] | 610 | void blk_mq_rq_timed_out(struct request *req, bool reserved) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 611 | { |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 612 | struct blk_mq_ops *ops = req->q->mq_ops; |
| 613 | enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER; |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * We know that complete is set at this point. If STARTED isn't set |
| 617 | * anymore, then the request isn't active and the "timeout" should |
| 618 | * just be ignored. This can happen due to the bitflag ordering. |
| 619 | * Timeout first checks if STARTED is set, and if it is, assumes |
| 620 | * the request is active. But if we race with completion, then |
| 621 | * we both flags will get cleared. So check here again, and ignore |
| 622 | * a timeout event with a request that isn't active. |
| 623 | */ |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 624 | if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags)) |
| 625 | return; |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 626 | |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 627 | if (ops->timeout) |
Christoph Hellwig | 0152fb6 | 2014-09-13 16:40:13 -0700 | [diff] [blame] | 628 | ret = ops->timeout(req, reserved); |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 629 | |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 630 | switch (ret) { |
| 631 | case BLK_EH_HANDLED: |
| 632 | __blk_mq_complete_request(req); |
| 633 | break; |
| 634 | case BLK_EH_RESET_TIMER: |
| 635 | blk_add_timer(req); |
| 636 | blk_clear_rq_complete(req); |
| 637 | break; |
| 638 | case BLK_EH_NOT_HANDLED: |
| 639 | break; |
| 640 | default: |
| 641 | printk(KERN_ERR "block: bad eh return: %d\n", ret); |
| 642 | break; |
| 643 | } |
Jens Axboe | 87ee7b1 | 2014-04-24 08:51:47 -0600 | [diff] [blame] | 644 | } |
Keith Busch | 5b3f25f | 2015-01-07 18:55:46 -0700 | [diff] [blame] | 645 | |
Christoph Hellwig | 81481eb | 2014-09-13 16:40:11 -0700 | [diff] [blame] | 646 | static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, |
| 647 | struct request *rq, void *priv, bool reserved) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 648 | { |
Christoph Hellwig | 81481eb | 2014-09-13 16:40:11 -0700 | [diff] [blame] | 649 | struct blk_mq_timeout_data *data = priv; |
| 650 | |
Keith Busch | eb130db | 2015-01-08 08:59:53 -0700 | [diff] [blame] | 651 | if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) { |
| 652 | /* |
| 653 | * If a request wasn't started before the queue was |
| 654 | * marked dying, kill it here or it'll go unnoticed. |
| 655 | */ |
Keith Busch | a59e0f5 | 2016-02-11 13:05:38 -0700 | [diff] [blame] | 656 | if (unlikely(blk_queue_dying(rq->q))) { |
| 657 | rq->errors = -EIO; |
| 658 | blk_mq_end_request(rq, rq->errors); |
| 659 | } |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 660 | return; |
Keith Busch | eb130db | 2015-01-08 08:59:53 -0700 | [diff] [blame] | 661 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 662 | |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 663 | if (time_after_eq(jiffies, rq->deadline)) { |
| 664 | if (!blk_mark_rq_complete(rq)) |
Christoph Hellwig | 0152fb6 | 2014-09-13 16:40:13 -0700 | [diff] [blame] | 665 | blk_mq_rq_timed_out(rq, reserved); |
Christoph Hellwig | 46f92d4 | 2014-09-13 16:40:12 -0700 | [diff] [blame] | 666 | } else if (!data->next_set || time_after(data->next, rq->deadline)) { |
| 667 | data->next = rq->deadline; |
| 668 | data->next_set = 1; |
| 669 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Christoph Hellwig | 287922e | 2015-10-30 20:57:30 +0800 | [diff] [blame] | 672 | static void blk_mq_timeout_work(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 673 | { |
Christoph Hellwig | 287922e | 2015-10-30 20:57:30 +0800 | [diff] [blame] | 674 | struct request_queue *q = |
| 675 | container_of(work, struct request_queue, timeout_work); |
Christoph Hellwig | 81481eb | 2014-09-13 16:40:11 -0700 | [diff] [blame] | 676 | struct blk_mq_timeout_data data = { |
| 677 | .next = 0, |
| 678 | .next_set = 0, |
| 679 | }; |
Christoph Hellwig | 81481eb | 2014-09-13 16:40:11 -0700 | [diff] [blame] | 680 | int i; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 681 | |
Gabriel Krisman Bertazi | 71f79fb | 2016-08-01 08:23:39 -0600 | [diff] [blame] | 682 | /* A deadlock might occur if a request is stuck requiring a |
| 683 | * timeout at the same time a queue freeze is waiting |
| 684 | * completion, since the timeout code would not be able to |
| 685 | * acquire the queue reference here. |
| 686 | * |
| 687 | * That's why we don't use blk_queue_enter here; instead, we use |
| 688 | * percpu_ref_tryget directly, because we need to be able to |
| 689 | * obtain a reference even in the short window between the queue |
| 690 | * starting to freeze, by dropping the first reference in |
| 691 | * blk_mq_freeze_queue_start, and the moment the last request is |
| 692 | * consumed, marked by the instant q_usage_counter reaches |
| 693 | * zero. |
| 694 | */ |
| 695 | if (!percpu_ref_tryget(&q->q_usage_counter)) |
Christoph Hellwig | 287922e | 2015-10-30 20:57:30 +0800 | [diff] [blame] | 696 | return; |
| 697 | |
Christoph Hellwig | 0bf6cd5 | 2015-09-27 21:01:51 +0200 | [diff] [blame] | 698 | blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 699 | |
Christoph Hellwig | 81481eb | 2014-09-13 16:40:11 -0700 | [diff] [blame] | 700 | if (data.next_set) { |
| 701 | data.next = blk_rq_timeout(round_jiffies_up(data.next)); |
| 702 | mod_timer(&q->timeout, data.next); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 703 | } else { |
Christoph Hellwig | 0bf6cd5 | 2015-09-27 21:01:51 +0200 | [diff] [blame] | 704 | struct blk_mq_hw_ctx *hctx; |
| 705 | |
Ming Lei | f054b56 | 2015-04-21 10:00:19 +0800 | [diff] [blame] | 706 | queue_for_each_hw_ctx(q, hctx, i) { |
| 707 | /* the hctx may be unmapped, so check it here */ |
| 708 | if (blk_mq_hw_queue_mapped(hctx)) |
| 709 | blk_mq_tag_idle(hctx); |
| 710 | } |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 711 | } |
Christoph Hellwig | 287922e | 2015-10-30 20:57:30 +0800 | [diff] [blame] | 712 | blk_queue_exit(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Reverse check our software queue for entries that we could potentially |
| 717 | * merge with. Currently includes a hand-wavy stop count of 8, to not spend |
| 718 | * too much time checking for merges. |
| 719 | */ |
| 720 | static bool blk_mq_attempt_merge(struct request_queue *q, |
| 721 | struct blk_mq_ctx *ctx, struct bio *bio) |
| 722 | { |
| 723 | struct request *rq; |
| 724 | int checked = 8; |
| 725 | |
| 726 | list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) { |
| 727 | int el_ret; |
| 728 | |
| 729 | if (!checked--) |
| 730 | break; |
| 731 | |
| 732 | if (!blk_rq_merge_ok(rq, bio)) |
| 733 | continue; |
| 734 | |
| 735 | el_ret = blk_try_merge(rq, bio); |
| 736 | if (el_ret == ELEVATOR_BACK_MERGE) { |
| 737 | if (bio_attempt_back_merge(q, rq, bio)) { |
| 738 | ctx->rq_merged++; |
| 739 | return true; |
| 740 | } |
| 741 | break; |
| 742 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { |
| 743 | if (bio_attempt_front_merge(q, rq, bio)) { |
| 744 | ctx->rq_merged++; |
| 745 | return true; |
| 746 | } |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | return false; |
| 752 | } |
| 753 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 754 | struct flush_busy_ctx_data { |
| 755 | struct blk_mq_hw_ctx *hctx; |
| 756 | struct list_head *list; |
| 757 | }; |
| 758 | |
| 759 | static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data) |
| 760 | { |
| 761 | struct flush_busy_ctx_data *flush_data = data; |
| 762 | struct blk_mq_hw_ctx *hctx = flush_data->hctx; |
| 763 | struct blk_mq_ctx *ctx = hctx->ctxs[bitnr]; |
| 764 | |
| 765 | sbitmap_clear_bit(sb, bitnr); |
| 766 | spin_lock(&ctx->lock); |
| 767 | list_splice_tail_init(&ctx->rq_list, flush_data->list); |
| 768 | spin_unlock(&ctx->lock); |
| 769 | return true; |
| 770 | } |
| 771 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 772 | /* |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 773 | * Process software queues that have been marked busy, splicing them |
| 774 | * to the for-dispatch |
| 775 | */ |
| 776 | static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list) |
| 777 | { |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 778 | struct flush_busy_ctx_data data = { |
| 779 | .hctx = hctx, |
| 780 | .list = list, |
| 781 | }; |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 782 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 783 | sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 784 | } |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 785 | |
Jens Axboe | 703fd1c | 2016-09-16 13:59:14 -0600 | [diff] [blame] | 786 | static inline unsigned int queued_to_index(unsigned int queued) |
| 787 | { |
| 788 | if (!queued) |
| 789 | return 0; |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 790 | |
Jens Axboe | 703fd1c | 2016-09-16 13:59:14 -0600 | [diff] [blame] | 791 | return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1); |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 795 | * Run this hardware queue, pulling any software queues mapped to it in. |
| 796 | * Note that this function currently has various problems around ordering |
| 797 | * of IO. In particular, we'd like FIFO behaviour on handling existing |
| 798 | * items on the hctx->dispatch list. Ignore that for now. |
| 799 | */ |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 800 | static void blk_mq_process_rq_list(struct blk_mq_hw_ctx *hctx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 801 | { |
| 802 | struct request_queue *q = hctx->queue; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 803 | struct request *rq; |
| 804 | LIST_HEAD(rq_list); |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 805 | LIST_HEAD(driver_list); |
| 806 | struct list_head *dptr; |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 807 | int queued; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 808 | |
Bart Van Assche | 5d1b25c | 2016-10-28 17:19:15 -0700 | [diff] [blame] | 809 | if (unlikely(blk_mq_hctx_stopped(hctx))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 810 | return; |
| 811 | |
| 812 | hctx->run++; |
| 813 | |
| 814 | /* |
| 815 | * Touch any software queue that has pending entries. |
| 816 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 817 | flush_busy_ctxs(hctx, &rq_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 818 | |
| 819 | /* |
| 820 | * If we have previous entries on our dispatch list, grab them |
| 821 | * and stuff them at the front for more fair dispatch. |
| 822 | */ |
| 823 | if (!list_empty_careful(&hctx->dispatch)) { |
| 824 | spin_lock(&hctx->lock); |
| 825 | if (!list_empty(&hctx->dispatch)) |
| 826 | list_splice_init(&hctx->dispatch, &rq_list); |
| 827 | spin_unlock(&hctx->lock); |
| 828 | } |
| 829 | |
| 830 | /* |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 831 | * Start off with dptr being NULL, so we start the first request |
| 832 | * immediately, even if we have more pending. |
| 833 | */ |
| 834 | dptr = NULL; |
| 835 | |
| 836 | /* |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 837 | * Now process all the entries, sending them to the driver. |
| 838 | */ |
Jens Axboe | 1429d7c | 2014-05-19 09:23:55 -0600 | [diff] [blame] | 839 | queued = 0; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 840 | while (!list_empty(&rq_list)) { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 841 | struct blk_mq_queue_data bd; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 842 | int ret; |
| 843 | |
| 844 | rq = list_first_entry(&rq_list, struct request, queuelist); |
| 845 | list_del_init(&rq->queuelist); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 846 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 847 | bd.rq = rq; |
| 848 | bd.list = dptr; |
| 849 | bd.last = list_empty(&rq_list); |
| 850 | |
| 851 | ret = q->mq_ops->queue_rq(hctx, &bd); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 852 | switch (ret) { |
| 853 | case BLK_MQ_RQ_QUEUE_OK: |
| 854 | queued++; |
Omar Sandoval | 52b9c33 | 2016-06-08 18:22:20 -0700 | [diff] [blame] | 855 | break; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 856 | case BLK_MQ_RQ_QUEUE_BUSY: |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 857 | list_add(&rq->queuelist, &rq_list); |
Christoph Hellwig | ed0791b | 2014-04-16 09:44:57 +0200 | [diff] [blame] | 858 | __blk_mq_requeue_request(rq); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 859 | break; |
| 860 | default: |
| 861 | pr_err("blk-mq: bad return on queue: %d\n", ret); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 862 | case BLK_MQ_RQ_QUEUE_ERROR: |
Christoph Hellwig | 1e93b8c | 2014-02-11 08:27:13 -0800 | [diff] [blame] | 863 | rq->errors = -EIO; |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 864 | blk_mq_end_request(rq, rq->errors); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | |
| 868 | if (ret == BLK_MQ_RQ_QUEUE_BUSY) |
| 869 | break; |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 870 | |
| 871 | /* |
| 872 | * We've done the first request. If we have more than 1 |
| 873 | * left in the list, set dptr to defer issue. |
| 874 | */ |
| 875 | if (!dptr && rq_list.next != rq_list.prev) |
| 876 | dptr = &driver_list; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Jens Axboe | 703fd1c | 2016-09-16 13:59:14 -0600 | [diff] [blame] | 879 | hctx->dispatched[queued_to_index(queued)]++; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 880 | |
| 881 | /* |
| 882 | * Any items that need requeuing? Stuff them into hctx->dispatch, |
| 883 | * that is where we will continue on next queue run. |
| 884 | */ |
| 885 | if (!list_empty(&rq_list)) { |
| 886 | spin_lock(&hctx->lock); |
| 887 | list_splice(&rq_list, &hctx->dispatch); |
| 888 | spin_unlock(&hctx->lock); |
Shaohua Li | 9ba52e5 | 2015-05-04 14:32:48 -0600 | [diff] [blame] | 889 | /* |
| 890 | * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but |
| 891 | * it's possible the queue is stopped and restarted again |
| 892 | * before this. Queue restart will dispatch requests. And since |
| 893 | * requests in rq_list aren't added into hctx->dispatch yet, |
| 894 | * the requests in rq_list might get lost. |
| 895 | * |
| 896 | * blk_mq_run_hw_queue() already checks the STOPPED bit |
| 897 | **/ |
| 898 | blk_mq_run_hw_queue(hctx, true); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 902 | static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 903 | { |
| 904 | int srcu_idx; |
| 905 | |
| 906 | WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) && |
| 907 | cpu_online(hctx->next_cpu)); |
| 908 | |
| 909 | if (!(hctx->flags & BLK_MQ_F_BLOCKING)) { |
| 910 | rcu_read_lock(); |
| 911 | blk_mq_process_rq_list(hctx); |
| 912 | rcu_read_unlock(); |
| 913 | } else { |
| 914 | srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu); |
| 915 | blk_mq_process_rq_list(hctx); |
| 916 | srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx); |
| 917 | } |
| 918 | } |
| 919 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 920 | /* |
| 921 | * It'd be great if the workqueue API had a way to pass |
| 922 | * in a mask and had some smarts for more clever placement. |
| 923 | * For now we just round-robin here, switching for every |
| 924 | * BLK_MQ_CPU_WORK_BATCH queued items. |
| 925 | */ |
| 926 | static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx) |
| 927 | { |
Christoph Hellwig | b657d7e | 2014-11-24 09:27:23 +0100 | [diff] [blame] | 928 | if (hctx->queue->nr_hw_queues == 1) |
| 929 | return WORK_CPU_UNBOUND; |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 930 | |
| 931 | if (--hctx->next_cpu_batch <= 0) { |
Christoph Hellwig | b657d7e | 2014-11-24 09:27:23 +0100 | [diff] [blame] | 932 | int cpu = hctx->next_cpu, next_cpu; |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 933 | |
| 934 | next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask); |
| 935 | if (next_cpu >= nr_cpu_ids) |
| 936 | next_cpu = cpumask_first(hctx->cpumask); |
| 937 | |
| 938 | hctx->next_cpu = next_cpu; |
| 939 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
Christoph Hellwig | b657d7e | 2014-11-24 09:27:23 +0100 | [diff] [blame] | 940 | |
| 941 | return cpu; |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 942 | } |
| 943 | |
Christoph Hellwig | b657d7e | 2014-11-24 09:27:23 +0100 | [diff] [blame] | 944 | return hctx->next_cpu; |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 945 | } |
| 946 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 947 | void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async) |
| 948 | { |
Bart Van Assche | 5d1b25c | 2016-10-28 17:19:15 -0700 | [diff] [blame] | 949 | if (unlikely(blk_mq_hctx_stopped(hctx) || |
| 950 | !blk_mq_hw_queue_mapped(hctx))) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 951 | return; |
| 952 | |
Jens Axboe | 1b792f2 | 2016-09-21 10:12:13 -0600 | [diff] [blame] | 953 | if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) { |
Paolo Bonzini | 2a90d4a | 2014-11-07 23:04:00 +0100 | [diff] [blame] | 954 | int cpu = get_cpu(); |
| 955 | if (cpumask_test_cpu(cpu, hctx->cpumask)) { |
Paolo Bonzini | 398205b | 2014-11-07 23:03:59 +0100 | [diff] [blame] | 956 | __blk_mq_run_hw_queue(hctx); |
Paolo Bonzini | 2a90d4a | 2014-11-07 23:04:00 +0100 | [diff] [blame] | 957 | put_cpu(); |
Paolo Bonzini | 398205b | 2014-11-07 23:03:59 +0100 | [diff] [blame] | 958 | return; |
| 959 | } |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 960 | |
Paolo Bonzini | 2a90d4a | 2014-11-07 23:04:00 +0100 | [diff] [blame] | 961 | put_cpu(); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 962 | } |
Paolo Bonzini | 398205b | 2014-11-07 23:03:59 +0100 | [diff] [blame] | 963 | |
Jens Axboe | 27489a3 | 2016-08-24 15:54:25 -0600 | [diff] [blame] | 964 | kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Mike Snitzer | b94ec29 | 2015-03-11 23:56:38 -0400 | [diff] [blame] | 967 | void blk_mq_run_hw_queues(struct request_queue *q, bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 968 | { |
| 969 | struct blk_mq_hw_ctx *hctx; |
| 970 | int i; |
| 971 | |
| 972 | queue_for_each_hw_ctx(q, hctx, i) { |
| 973 | if ((!blk_mq_hctx_has_pending(hctx) && |
| 974 | list_empty_careful(&hctx->dispatch)) || |
Bart Van Assche | 5d1b25c | 2016-10-28 17:19:15 -0700 | [diff] [blame] | 975 | blk_mq_hctx_stopped(hctx)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 976 | continue; |
| 977 | |
Mike Snitzer | b94ec29 | 2015-03-11 23:56:38 -0400 | [diff] [blame] | 978 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 979 | } |
| 980 | } |
Mike Snitzer | b94ec29 | 2015-03-11 23:56:38 -0400 | [diff] [blame] | 981 | EXPORT_SYMBOL(blk_mq_run_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 982 | |
Bart Van Assche | fd00144 | 2016-10-28 17:19:37 -0700 | [diff] [blame] | 983 | /** |
| 984 | * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped |
| 985 | * @q: request queue. |
| 986 | * |
| 987 | * The caller is responsible for serializing this function against |
| 988 | * blk_mq_{start,stop}_hw_queue(). |
| 989 | */ |
| 990 | bool blk_mq_queue_stopped(struct request_queue *q) |
| 991 | { |
| 992 | struct blk_mq_hw_ctx *hctx; |
| 993 | int i; |
| 994 | |
| 995 | queue_for_each_hw_ctx(q, hctx, i) |
| 996 | if (blk_mq_hctx_stopped(hctx)) |
| 997 | return true; |
| 998 | |
| 999 | return false; |
| 1000 | } |
| 1001 | EXPORT_SYMBOL(blk_mq_queue_stopped); |
| 1002 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1003 | void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 1004 | { |
Jens Axboe | 27489a3 | 2016-08-24 15:54:25 -0600 | [diff] [blame] | 1005 | cancel_work(&hctx->run_work); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1006 | cancel_delayed_work(&hctx->delay_work); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1007 | set_bit(BLK_MQ_S_STOPPED, &hctx->state); |
| 1008 | } |
| 1009 | EXPORT_SYMBOL(blk_mq_stop_hw_queue); |
| 1010 | |
Christoph Hellwig | 280d45f | 2013-10-25 14:45:58 +0100 | [diff] [blame] | 1011 | void blk_mq_stop_hw_queues(struct request_queue *q) |
| 1012 | { |
| 1013 | struct blk_mq_hw_ctx *hctx; |
| 1014 | int i; |
| 1015 | |
| 1016 | queue_for_each_hw_ctx(q, hctx, i) |
| 1017 | blk_mq_stop_hw_queue(hctx); |
| 1018 | } |
| 1019 | EXPORT_SYMBOL(blk_mq_stop_hw_queues); |
| 1020 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1021 | void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx) |
| 1022 | { |
| 1023 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1024 | |
Jens Axboe | 0ffbce8 | 2014-06-25 08:22:34 -0600 | [diff] [blame] | 1025 | blk_mq_run_hw_queue(hctx, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1026 | } |
| 1027 | EXPORT_SYMBOL(blk_mq_start_hw_queue); |
| 1028 | |
Christoph Hellwig | 2f26855 | 2014-04-16 09:44:56 +0200 | [diff] [blame] | 1029 | void blk_mq_start_hw_queues(struct request_queue *q) |
| 1030 | { |
| 1031 | struct blk_mq_hw_ctx *hctx; |
| 1032 | int i; |
| 1033 | |
| 1034 | queue_for_each_hw_ctx(q, hctx, i) |
| 1035 | blk_mq_start_hw_queue(hctx); |
| 1036 | } |
| 1037 | EXPORT_SYMBOL(blk_mq_start_hw_queues); |
| 1038 | |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 1039 | 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] | 1040 | { |
| 1041 | struct blk_mq_hw_ctx *hctx; |
| 1042 | int i; |
| 1043 | |
| 1044 | queue_for_each_hw_ctx(q, hctx, i) { |
Bart Van Assche | 5d1b25c | 2016-10-28 17:19:15 -0700 | [diff] [blame] | 1045 | if (!blk_mq_hctx_stopped(hctx)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1046 | continue; |
| 1047 | |
| 1048 | clear_bit(BLK_MQ_S_STOPPED, &hctx->state); |
Christoph Hellwig | 1b4a325 | 2014-04-16 09:44:54 +0200 | [diff] [blame] | 1049 | blk_mq_run_hw_queue(hctx, async); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues); |
| 1053 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1054 | static void blk_mq_run_work_fn(struct work_struct *work) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1055 | { |
| 1056 | struct blk_mq_hw_ctx *hctx; |
| 1057 | |
Jens Axboe | 27489a3 | 2016-08-24 15:54:25 -0600 | [diff] [blame] | 1058 | hctx = container_of(work, struct blk_mq_hw_ctx, run_work); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1059 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1060 | __blk_mq_run_hw_queue(hctx); |
| 1061 | } |
| 1062 | |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1063 | static void blk_mq_delay_work_fn(struct work_struct *work) |
| 1064 | { |
| 1065 | struct blk_mq_hw_ctx *hctx; |
| 1066 | |
| 1067 | hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work); |
| 1068 | |
| 1069 | if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state)) |
| 1070 | __blk_mq_run_hw_queue(hctx); |
| 1071 | } |
| 1072 | |
| 1073 | void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs) |
| 1074 | { |
Ming Lei | 19c66e5 | 2014-12-03 19:38:04 +0800 | [diff] [blame] | 1075 | if (unlikely(!blk_mq_hw_queue_mapped(hctx))) |
| 1076 | return; |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1077 | |
Christoph Hellwig | b657d7e | 2014-11-24 09:27:23 +0100 | [diff] [blame] | 1078 | kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx), |
| 1079 | &hctx->delay_work, msecs_to_jiffies(msecs)); |
Christoph Hellwig | 70f4db6 | 2014-04-16 10:48:08 -0600 | [diff] [blame] | 1080 | } |
| 1081 | EXPORT_SYMBOL(blk_mq_delay_queue); |
| 1082 | |
Ming Lei | cfd0c55 | 2015-10-20 23:13:57 +0800 | [diff] [blame] | 1083 | static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx, |
Ming Lei | cfd0c55 | 2015-10-20 23:13:57 +0800 | [diff] [blame] | 1084 | struct request *rq, |
| 1085 | bool at_head) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1086 | { |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1087 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 1088 | |
Jens Axboe | 01b983c | 2013-11-19 18:59:10 -0700 | [diff] [blame] | 1089 | trace_block_rq_insert(hctx->queue, rq); |
| 1090 | |
Christoph Hellwig | 72a0a36 | 2014-02-07 10:22:36 -0800 | [diff] [blame] | 1091 | if (at_head) |
| 1092 | list_add(&rq->queuelist, &ctx->rq_list); |
| 1093 | else |
| 1094 | list_add_tail(&rq->queuelist, &ctx->rq_list); |
Ming Lei | cfd0c55 | 2015-10-20 23:13:57 +0800 | [diff] [blame] | 1095 | } |
Jens Axboe | 4bb659b | 2014-05-09 09:36:49 -0600 | [diff] [blame] | 1096 | |
Ming Lei | cfd0c55 | 2015-10-20 23:13:57 +0800 | [diff] [blame] | 1097 | static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, |
| 1098 | struct request *rq, bool at_head) |
| 1099 | { |
| 1100 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
| 1101 | |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1102 | __blk_mq_insert_req_list(hctx, rq, at_head); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1103 | blk_mq_hctx_mark_pending(hctx, ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Christoph Hellwig | eeabc85 | 2014-03-21 08:57:37 -0600 | [diff] [blame] | 1106 | void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue, |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1107 | bool async) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1108 | { |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1109 | struct blk_mq_ctx *ctx = rq->mq_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1110 | struct request_queue *q = rq->q; |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 1111 | struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1112 | |
Christoph Hellwig | a57a178 | 2014-09-16 14:44:07 -0700 | [diff] [blame] | 1113 | spin_lock(&ctx->lock); |
| 1114 | __blk_mq_insert_request(hctx, rq, at_head); |
| 1115 | spin_unlock(&ctx->lock); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1116 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1117 | if (run_queue) |
| 1118 | blk_mq_run_hw_queue(hctx, async); |
| 1119 | } |
| 1120 | |
| 1121 | static void blk_mq_insert_requests(struct request_queue *q, |
| 1122 | struct blk_mq_ctx *ctx, |
| 1123 | struct list_head *list, |
| 1124 | int depth, |
| 1125 | bool from_schedule) |
| 1126 | |
| 1127 | { |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 1128 | struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1129 | |
| 1130 | trace_block_unplug(q, depth, !from_schedule); |
| 1131 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1132 | /* |
| 1133 | * preemption doesn't flush plug list, so it's possible ctx->cpu is |
| 1134 | * offline now |
| 1135 | */ |
| 1136 | spin_lock(&ctx->lock); |
| 1137 | while (!list_empty(list)) { |
| 1138 | struct request *rq; |
| 1139 | |
| 1140 | rq = list_first_entry(list, struct request, queuelist); |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1141 | BUG_ON(rq->mq_ctx != ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1142 | list_del_init(&rq->queuelist); |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1143 | __blk_mq_insert_req_list(hctx, rq, false); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1144 | } |
Ming Lei | cfd0c55 | 2015-10-20 23:13:57 +0800 | [diff] [blame] | 1145 | blk_mq_hctx_mark_pending(hctx, ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1146 | spin_unlock(&ctx->lock); |
| 1147 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1148 | blk_mq_run_hw_queue(hctx, from_schedule); |
| 1149 | } |
| 1150 | |
| 1151 | static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1152 | { |
| 1153 | struct request *rqa = container_of(a, struct request, queuelist); |
| 1154 | struct request *rqb = container_of(b, struct request, queuelist); |
| 1155 | |
| 1156 | return !(rqa->mq_ctx < rqb->mq_ctx || |
| 1157 | (rqa->mq_ctx == rqb->mq_ctx && |
| 1158 | blk_rq_pos(rqa) < blk_rq_pos(rqb))); |
| 1159 | } |
| 1160 | |
| 1161 | void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
| 1162 | { |
| 1163 | struct blk_mq_ctx *this_ctx; |
| 1164 | struct request_queue *this_q; |
| 1165 | struct request *rq; |
| 1166 | LIST_HEAD(list); |
| 1167 | LIST_HEAD(ctx_list); |
| 1168 | unsigned int depth; |
| 1169 | |
| 1170 | list_splice_init(&plug->mq_list, &list); |
| 1171 | |
| 1172 | list_sort(NULL, &list, plug_ctx_cmp); |
| 1173 | |
| 1174 | this_q = NULL; |
| 1175 | this_ctx = NULL; |
| 1176 | depth = 0; |
| 1177 | |
| 1178 | while (!list_empty(&list)) { |
| 1179 | rq = list_entry_rq(list.next); |
| 1180 | list_del_init(&rq->queuelist); |
| 1181 | BUG_ON(!rq->q); |
| 1182 | if (rq->mq_ctx != this_ctx) { |
| 1183 | if (this_ctx) { |
| 1184 | blk_mq_insert_requests(this_q, this_ctx, |
| 1185 | &ctx_list, depth, |
| 1186 | from_schedule); |
| 1187 | } |
| 1188 | |
| 1189 | this_ctx = rq->mq_ctx; |
| 1190 | this_q = rq->q; |
| 1191 | depth = 0; |
| 1192 | } |
| 1193 | |
| 1194 | depth++; |
| 1195 | list_add_tail(&rq->queuelist, &ctx_list); |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * If 'this_ctx' is set, we know we have entries to complete |
| 1200 | * on 'ctx_list'. Do those. |
| 1201 | */ |
| 1202 | if (this_ctx) { |
| 1203 | blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth, |
| 1204 | from_schedule); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | static void blk_mq_bio_to_request(struct request *rq, struct bio *bio) |
| 1209 | { |
| 1210 | init_request_from_bio(rq, bio); |
Jens Axboe | 4b57052 | 2014-05-29 11:00:11 -0600 | [diff] [blame] | 1211 | |
Michael Callahan | a21f2a3 | 2016-05-03 11:12:49 -0400 | [diff] [blame] | 1212 | blk_account_io_start(rq, 1); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
Jens Axboe | 274a584 | 2014-08-15 12:44:08 -0600 | [diff] [blame] | 1215 | static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx) |
| 1216 | { |
| 1217 | return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) && |
| 1218 | !blk_queue_nomerges(hctx->queue); |
| 1219 | } |
| 1220 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1221 | static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, |
| 1222 | struct blk_mq_ctx *ctx, |
| 1223 | struct request *rq, struct bio *bio) |
| 1224 | { |
Ming Lei | e18378a | 2015-10-20 23:13:54 +0800 | [diff] [blame] | 1225 | if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) { |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1226 | blk_mq_bio_to_request(rq, bio); |
| 1227 | spin_lock(&ctx->lock); |
| 1228 | insert_rq: |
| 1229 | __blk_mq_insert_request(hctx, rq, false); |
| 1230 | spin_unlock(&ctx->lock); |
| 1231 | return false; |
| 1232 | } else { |
Jens Axboe | 274a584 | 2014-08-15 12:44:08 -0600 | [diff] [blame] | 1233 | struct request_queue *q = hctx->queue; |
| 1234 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1235 | spin_lock(&ctx->lock); |
| 1236 | if (!blk_mq_attempt_merge(q, ctx, bio)) { |
| 1237 | blk_mq_bio_to_request(rq, bio); |
| 1238 | goto insert_rq; |
| 1239 | } |
| 1240 | |
| 1241 | spin_unlock(&ctx->lock); |
| 1242 | __blk_mq_free_request(hctx, ctx, rq); |
| 1243 | return true; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1247 | static struct request *blk_mq_map_request(struct request_queue *q, |
| 1248 | struct bio *bio, |
Jens Axboe | 2552e3f | 2016-10-27 19:03:55 -0600 | [diff] [blame] | 1249 | struct blk_mq_alloc_data *data) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1250 | { |
| 1251 | struct blk_mq_hw_ctx *hctx; |
| 1252 | struct blk_mq_ctx *ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1253 | struct request *rq; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1254 | |
Dan Williams | 3ef28e8 | 2015-10-21 13:20:12 -0400 | [diff] [blame] | 1255 | blk_queue_enter_live(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1256 | ctx = blk_mq_get_ctx(q); |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 1257 | hctx = blk_mq_map_queue(q, ctx->cpu); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1258 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1259 | trace_block_getrq(q, bio, bio->bi_opf); |
Jens Axboe | 2552e3f | 2016-10-27 19:03:55 -0600 | [diff] [blame] | 1260 | blk_mq_set_alloc_data(data, q, 0, ctx, hctx); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1261 | rq = __blk_mq_alloc_request(data, bio->bi_opf); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1262 | |
Jens Axboe | 7dd2fb6 | 2016-10-27 09:49:19 -0600 | [diff] [blame] | 1263 | data->hctx->queued++; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1264 | return rq; |
| 1265 | } |
| 1266 | |
Bart Van Assche | 2253efc | 2016-10-28 17:20:02 -0700 | [diff] [blame] | 1267 | static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 1268 | struct request *rq, blk_qc_t *cookie) |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1269 | { |
| 1270 | int ret; |
| 1271 | struct request_queue *q = rq->q; |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1272 | struct blk_mq_queue_data bd = { |
| 1273 | .rq = rq, |
| 1274 | .list = NULL, |
| 1275 | .last = 1 |
| 1276 | }; |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1277 | blk_qc_t new_cookie = blk_tag_to_qc_t(rq->tag, hctx->queue_num); |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1278 | |
Bart Van Assche | 2253efc | 2016-10-28 17:20:02 -0700 | [diff] [blame] | 1279 | if (blk_mq_hctx_stopped(hctx)) |
| 1280 | goto insert; |
| 1281 | |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1282 | /* |
| 1283 | * For OK queue, we are done. For error, kill it. Any other |
| 1284 | * error (busy), just add it to our list as we previously |
| 1285 | * would have done |
| 1286 | */ |
| 1287 | ret = q->mq_ops->queue_rq(hctx, &bd); |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1288 | if (ret == BLK_MQ_RQ_QUEUE_OK) { |
| 1289 | *cookie = new_cookie; |
Bart Van Assche | 2253efc | 2016-10-28 17:20:02 -0700 | [diff] [blame] | 1290 | return; |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1291 | } |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1292 | |
| 1293 | __blk_mq_requeue_request(rq); |
| 1294 | |
| 1295 | if (ret == BLK_MQ_RQ_QUEUE_ERROR) { |
| 1296 | *cookie = BLK_QC_T_NONE; |
| 1297 | rq->errors = -EIO; |
| 1298 | blk_mq_end_request(rq, rq->errors); |
Bart Van Assche | 2253efc | 2016-10-28 17:20:02 -0700 | [diff] [blame] | 1299 | return; |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Bart Van Assche | 2253efc | 2016-10-28 17:20:02 -0700 | [diff] [blame] | 1302 | insert: |
| 1303 | blk_mq_insert_request(rq, false, true, true); |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1306 | /* |
| 1307 | * Multiple hardware queue variant. This will not use per-process plugs, |
| 1308 | * but will attempt to bypass the hctx queueing if we can go straight to |
| 1309 | * hardware for SYNC IO. |
| 1310 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1311 | static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1312 | { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1313 | const int is_sync = op_is_sync(bio->bi_opf); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1314 | const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA); |
Jens Axboe | 2552e3f | 2016-10-27 19:03:55 -0600 | [diff] [blame] | 1315 | struct blk_mq_alloc_data data; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1316 | struct request *rq; |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 1317 | unsigned int request_count = 0, srcu_idx; |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1318 | struct blk_plug *plug; |
Shaohua Li | 5b3f341 | 2015-05-08 10:51:33 -0700 | [diff] [blame] | 1319 | struct request *same_queue_rq = NULL; |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1320 | blk_qc_t cookie; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1321 | |
| 1322 | blk_queue_bounce(q, &bio); |
| 1323 | |
| 1324 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1325 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1326 | return BLK_QC_T_NONE; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1327 | } |
| 1328 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 1329 | blk_queue_split(q, &bio, q->bio_split); |
| 1330 | |
Omar Sandoval | 87c279e | 2016-06-01 22:18:48 -0700 | [diff] [blame] | 1331 | if (!is_flush_fua && !blk_queue_nomerges(q) && |
| 1332 | blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq)) |
| 1333 | return BLK_QC_T_NONE; |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1334 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1335 | rq = blk_mq_map_request(q, bio, &data); |
| 1336 | if (unlikely(!rq)) |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1337 | return BLK_QC_T_NONE; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1338 | |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1339 | cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1340 | |
| 1341 | if (unlikely(is_flush_fua)) { |
| 1342 | blk_mq_bio_to_request(rq, bio); |
| 1343 | blk_insert_flush(rq); |
| 1344 | goto run_queue; |
| 1345 | } |
| 1346 | |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1347 | plug = current->plug; |
Jens Axboe | e167dfb | 2014-10-29 11:18:26 -0600 | [diff] [blame] | 1348 | /* |
| 1349 | * If the driver supports defer issued based on 'last', then |
| 1350 | * queue it up like normal since we can potentially save some |
| 1351 | * CPU this way. |
| 1352 | */ |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1353 | if (((plug && !blk_queue_nomerges(q)) || is_sync) && |
| 1354 | !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) { |
| 1355 | struct request *old_rq = NULL; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1356 | |
| 1357 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1358 | |
| 1359 | /* |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 1360 | * We do limited plugging. If the bio can be merged, do that. |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1361 | * Otherwise the existing request in the plug list will be |
| 1362 | * issued. So the plug list will have one request at most |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1363 | */ |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1364 | if (plug) { |
Shaohua Li | 5b3f341 | 2015-05-08 10:51:33 -0700 | [diff] [blame] | 1365 | /* |
| 1366 | * The plug list might get flushed before this. If that |
Jens Axboe | b094f89 | 2015-11-20 20:29:45 -0700 | [diff] [blame] | 1367 | * happens, same_queue_rq is invalid and plug list is |
| 1368 | * empty |
| 1369 | */ |
Shaohua Li | 5b3f341 | 2015-05-08 10:51:33 -0700 | [diff] [blame] | 1370 | if (same_queue_rq && !list_empty(&plug->mq_list)) { |
| 1371 | old_rq = same_queue_rq; |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1372 | list_del_init(&old_rq->queuelist); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1373 | } |
Shaohua Li | f984df1 | 2015-05-08 10:51:32 -0700 | [diff] [blame] | 1374 | list_add_tail(&rq->queuelist, &plug->mq_list); |
| 1375 | } else /* is_sync */ |
| 1376 | old_rq = rq; |
| 1377 | blk_mq_put_ctx(data.ctx); |
| 1378 | if (!old_rq) |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1379 | goto done; |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 1380 | |
| 1381 | if (!(data.hctx->flags & BLK_MQ_F_BLOCKING)) { |
| 1382 | rcu_read_lock(); |
| 1383 | blk_mq_try_issue_directly(data.hctx, old_rq, &cookie); |
| 1384 | rcu_read_unlock(); |
| 1385 | } else { |
| 1386 | srcu_idx = srcu_read_lock(&data.hctx->queue_rq_srcu); |
| 1387 | blk_mq_try_issue_directly(data.hctx, old_rq, &cookie); |
| 1388 | srcu_read_unlock(&data.hctx->queue_rq_srcu, srcu_idx); |
| 1389 | } |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1390 | goto done; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1394 | /* |
| 1395 | * For a SYNC request, send it to the hardware immediately. For |
| 1396 | * an ASYNC request, just ensure that we run it later on. The |
| 1397 | * latter allows for merging opportunities and more efficient |
| 1398 | * dispatching. |
| 1399 | */ |
| 1400 | run_queue: |
| 1401 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
| 1402 | } |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1403 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1404 | done: |
| 1405 | return cookie; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | * Single hardware queue variant. This will attempt to use any per-process |
| 1410 | * plug for merging and IO deferral. |
| 1411 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1412 | static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio) |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1413 | { |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1414 | const int is_sync = op_is_sync(bio->bi_opf); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1415 | const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA); |
Jeff Moyer | e6c4438 | 2015-05-08 10:51:30 -0700 | [diff] [blame] | 1416 | struct blk_plug *plug; |
| 1417 | unsigned int request_count = 0; |
Jens Axboe | 2552e3f | 2016-10-27 19:03:55 -0600 | [diff] [blame] | 1418 | struct blk_mq_alloc_data data; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1419 | struct request *rq; |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1420 | blk_qc_t cookie; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1421 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1422 | blk_queue_bounce(q, &bio); |
| 1423 | |
| 1424 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1425 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1426 | return BLK_QC_T_NONE; |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1427 | } |
| 1428 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 1429 | blk_queue_split(q, &bio, q->bio_split); |
| 1430 | |
Omar Sandoval | 87c279e | 2016-06-01 22:18:48 -0700 | [diff] [blame] | 1431 | if (!is_flush_fua && !blk_queue_nomerges(q)) { |
| 1432 | if (blk_attempt_plug_merge(q, bio, &request_count, NULL)) |
| 1433 | return BLK_QC_T_NONE; |
| 1434 | } else |
| 1435 | request_count = blk_plug_queued_count(q); |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1436 | |
| 1437 | rq = blk_mq_map_request(q, bio, &data); |
Jens Axboe | ff87bce | 2014-06-03 11:59:49 -0600 | [diff] [blame] | 1438 | if (unlikely(!rq)) |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1439 | return BLK_QC_T_NONE; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1440 | |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1441 | cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1442 | |
| 1443 | if (unlikely(is_flush_fua)) { |
| 1444 | blk_mq_bio_to_request(rq, bio); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1445 | blk_insert_flush(rq); |
| 1446 | goto run_queue; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
| 1450 | * A task plug currently exists. Since this is completely lockless, |
| 1451 | * utilize that to temporarily store requests until the task is |
| 1452 | * either done or scheduled away. |
| 1453 | */ |
Jeff Moyer | e6c4438 | 2015-05-08 10:51:30 -0700 | [diff] [blame] | 1454 | plug = current->plug; |
| 1455 | if (plug) { |
Shaohua Li | 600271d | 2016-11-03 17:03:54 -0700 | [diff] [blame^] | 1456 | struct request *last = NULL; |
| 1457 | |
Jeff Moyer | e6c4438 | 2015-05-08 10:51:30 -0700 | [diff] [blame] | 1458 | blk_mq_bio_to_request(rq, bio); |
Ming Lei | 676d060 | 2015-10-20 23:13:56 +0800 | [diff] [blame] | 1459 | if (!request_count) |
Jeff Moyer | e6c4438 | 2015-05-08 10:51:30 -0700 | [diff] [blame] | 1460 | trace_block_plug(q); |
Shaohua Li | 600271d | 2016-11-03 17:03:54 -0700 | [diff] [blame^] | 1461 | else |
| 1462 | last = list_entry_rq(plug->mq_list.prev); |
Jens Axboe | b094f89 | 2015-11-20 20:29:45 -0700 | [diff] [blame] | 1463 | |
| 1464 | blk_mq_put_ctx(data.ctx); |
| 1465 | |
Shaohua Li | 600271d | 2016-11-03 17:03:54 -0700 | [diff] [blame^] | 1466 | if (request_count >= BLK_MAX_REQUEST_COUNT || (last && |
| 1467 | blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) { |
Jeff Moyer | e6c4438 | 2015-05-08 10:51:30 -0700 | [diff] [blame] | 1468 | blk_flush_plug_list(plug, false); |
| 1469 | trace_block_plug(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1470 | } |
Jens Axboe | b094f89 | 2015-11-20 20:29:45 -0700 | [diff] [blame] | 1471 | |
Jeff Moyer | e6c4438 | 2015-05-08 10:51:30 -0700 | [diff] [blame] | 1472 | list_add_tail(&rq->queuelist, &plug->mq_list); |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1473 | return cookie; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1476 | if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) { |
| 1477 | /* |
| 1478 | * For a SYNC request, send it to the hardware immediately. For |
| 1479 | * an ASYNC request, just ensure that we run it later on. The |
| 1480 | * latter allows for merging opportunities and more efficient |
| 1481 | * dispatching. |
| 1482 | */ |
| 1483 | run_queue: |
| 1484 | blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1485 | } |
| 1486 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 1487 | blk_mq_put_ctx(data.ctx); |
Jens Axboe | 7b37163 | 2015-11-05 10:41:40 -0700 | [diff] [blame] | 1488 | return cookie; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1489 | } |
| 1490 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1491 | static void blk_mq_free_rq_map(struct blk_mq_tag_set *set, |
| 1492 | struct blk_mq_tags *tags, unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1493 | { |
| 1494 | struct page *page; |
| 1495 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1496 | if (tags->rqs && set->ops->exit_request) { |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1497 | int i; |
| 1498 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1499 | for (i = 0; i < tags->nr_tags; i++) { |
| 1500 | if (!tags->rqs[i]) |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1501 | continue; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1502 | set->ops->exit_request(set->driver_data, tags->rqs[i], |
| 1503 | hctx_idx, i); |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 1504 | tags->rqs[i] = NULL; |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1508 | while (!list_empty(&tags->page_list)) { |
| 1509 | page = list_first_entry(&tags->page_list, struct page, lru); |
Dave Hansen | 6753471 | 2014-01-08 20:17:46 -0700 | [diff] [blame] | 1510 | list_del_init(&page->lru); |
Catalin Marinas | f75782e | 2015-09-14 18:16:02 +0100 | [diff] [blame] | 1511 | /* |
| 1512 | * Remove kmemleak object previously allocated in |
| 1513 | * blk_mq_init_rq_map(). |
| 1514 | */ |
| 1515 | kmemleak_free(page_address(page)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1516 | __free_pages(page, page->private); |
| 1517 | } |
| 1518 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1519 | kfree(tags->rqs); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1520 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1521 | blk_mq_free_tags(tags); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | static size_t order_to_size(unsigned int order) |
| 1525 | { |
Ming Lei | 4ca0850 | 2014-04-19 18:00:18 +0800 | [diff] [blame] | 1526 | return (size_t)PAGE_SIZE << order; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1529 | static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set, |
| 1530 | unsigned int hctx_idx) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1531 | { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1532 | struct blk_mq_tags *tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1533 | unsigned int i, j, entries_per_page, max_order = 4; |
| 1534 | size_t rq_size, left; |
| 1535 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1536 | tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags, |
Shaohua Li | 24391c0 | 2015-01-23 14:18:00 -0700 | [diff] [blame] | 1537 | set->numa_node, |
| 1538 | BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags)); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1539 | if (!tags) |
| 1540 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1541 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1542 | INIT_LIST_HEAD(&tags->page_list); |
| 1543 | |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 1544 | tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *), |
| 1545 | GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY, |
| 1546 | set->numa_node); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1547 | if (!tags->rqs) { |
| 1548 | blk_mq_free_tags(tags); |
| 1549 | return NULL; |
| 1550 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1551 | |
| 1552 | /* |
| 1553 | * rq_size is the size of the request plus driver payload, rounded |
| 1554 | * to the cacheline size |
| 1555 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1556 | rq_size = round_up(sizeof(struct request) + set->cmd_size, |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1557 | cache_line_size()); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1558 | left = rq_size * set->queue_depth; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1559 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1560 | for (i = 0; i < set->queue_depth; ) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1561 | int this_order = max_order; |
| 1562 | struct page *page; |
| 1563 | int to_do; |
| 1564 | void *p; |
| 1565 | |
Bartlomiej Zolnierkiewicz | b3a834b | 2016-05-16 09:54:47 -0600 | [diff] [blame] | 1566 | while (this_order && left < order_to_size(this_order - 1)) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1567 | this_order--; |
| 1568 | |
| 1569 | do { |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 1570 | page = alloc_pages_node(set->numa_node, |
Linus Torvalds | ac21117 | 2015-04-09 14:12:22 -0700 | [diff] [blame] | 1571 | GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO, |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 1572 | this_order); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1573 | if (page) |
| 1574 | break; |
| 1575 | if (!this_order--) |
| 1576 | break; |
| 1577 | if (order_to_size(this_order) < rq_size) |
| 1578 | break; |
| 1579 | } while (1); |
| 1580 | |
| 1581 | if (!page) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1582 | goto fail; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1583 | |
| 1584 | page->private = this_order; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1585 | list_add_tail(&page->lru, &tags->page_list); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1586 | |
| 1587 | p = page_address(page); |
Catalin Marinas | f75782e | 2015-09-14 18:16:02 +0100 | [diff] [blame] | 1588 | /* |
| 1589 | * Allow kmemleak to scan these pages as they contain pointers |
| 1590 | * to additional allocations like via ops->init_request(). |
| 1591 | */ |
| 1592 | kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1593 | entries_per_page = order_to_size(this_order) / rq_size; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1594 | to_do = min(entries_per_page, set->queue_depth - i); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1595 | left -= to_do * rq_size; |
| 1596 | for (j = 0; j < to_do; j++) { |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1597 | tags->rqs[i] = p; |
| 1598 | if (set->ops->init_request) { |
| 1599 | if (set->ops->init_request(set->driver_data, |
| 1600 | tags->rqs[i], hctx_idx, i, |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 1601 | set->numa_node)) { |
| 1602 | tags->rqs[i] = NULL; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1603 | goto fail; |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 1604 | } |
Christoph Hellwig | e9b267d | 2014-04-15 13:59:10 -0600 | [diff] [blame] | 1605 | } |
| 1606 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1607 | p += rq_size; |
| 1608 | i++; |
| 1609 | } |
| 1610 | } |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1611 | return tags; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1612 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1613 | fail: |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1614 | blk_mq_free_rq_map(set, tags, hctx_idx); |
| 1615 | return NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1618 | /* |
| 1619 | * 'cpu' is going away. splice any existing rq_list entries from this |
| 1620 | * software queue to the hw queue dispatch list, and ensure that it |
| 1621 | * gets run. |
| 1622 | */ |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1623 | static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node) |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1624 | { |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1625 | struct blk_mq_hw_ctx *hctx; |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1626 | struct blk_mq_ctx *ctx; |
| 1627 | LIST_HEAD(tmp); |
| 1628 | |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1629 | hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead); |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1630 | ctx = __blk_mq_get_ctx(hctx->queue, cpu); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1631 | |
| 1632 | spin_lock(&ctx->lock); |
| 1633 | if (!list_empty(&ctx->rq_list)) { |
| 1634 | list_splice_init(&ctx->rq_list, &tmp); |
| 1635 | blk_mq_hctx_clear_pending(hctx, ctx); |
| 1636 | } |
| 1637 | spin_unlock(&ctx->lock); |
| 1638 | |
| 1639 | if (list_empty(&tmp)) |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1640 | return 0; |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1641 | |
Jens Axboe | e57690f | 2016-08-24 15:34:35 -0600 | [diff] [blame] | 1642 | spin_lock(&hctx->lock); |
| 1643 | list_splice_tail_init(&tmp, &hctx->dispatch); |
| 1644 | spin_unlock(&hctx->lock); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1645 | |
| 1646 | blk_mq_run_hw_queue(hctx, true); |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1647 | return 0; |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1648 | } |
| 1649 | |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1650 | static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx) |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1651 | { |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1652 | cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD, |
| 1653 | &hctx->cpuhp_dead); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1654 | } |
| 1655 | |
Ming Lei | c3b4afc | 2015-06-04 22:25:04 +0800 | [diff] [blame] | 1656 | /* hctx->ctxs will be freed in queue's release handler */ |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1657 | static void blk_mq_exit_hctx(struct request_queue *q, |
| 1658 | struct blk_mq_tag_set *set, |
| 1659 | struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) |
| 1660 | { |
Ming Lei | f70ced0 | 2014-09-25 23:23:47 +0800 | [diff] [blame] | 1661 | unsigned flush_start_tag = set->queue_depth; |
| 1662 | |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1663 | blk_mq_tag_idle(hctx); |
| 1664 | |
Ming Lei | f70ced0 | 2014-09-25 23:23:47 +0800 | [diff] [blame] | 1665 | if (set->ops->exit_request) |
| 1666 | set->ops->exit_request(set->driver_data, |
| 1667 | hctx->fq->flush_rq, hctx_idx, |
| 1668 | flush_start_tag + hctx_idx); |
| 1669 | |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1670 | if (set->ops->exit_hctx) |
| 1671 | set->ops->exit_hctx(hctx, hctx_idx); |
| 1672 | |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 1673 | if (hctx->flags & BLK_MQ_F_BLOCKING) |
| 1674 | cleanup_srcu_struct(&hctx->queue_rq_srcu); |
| 1675 | |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1676 | blk_mq_remove_cpuhp(hctx); |
Ming Lei | f70ced0 | 2014-09-25 23:23:47 +0800 | [diff] [blame] | 1677 | blk_free_flush_queue(hctx->fq); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 1678 | sbitmap_free(&hctx->ctx_map); |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1681 | static void blk_mq_exit_hw_queues(struct request_queue *q, |
| 1682 | struct blk_mq_tag_set *set, int nr_queue) |
| 1683 | { |
| 1684 | struct blk_mq_hw_ctx *hctx; |
| 1685 | unsigned int i; |
| 1686 | |
| 1687 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1688 | if (i == nr_queue) |
| 1689 | break; |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1690 | blk_mq_exit_hctx(q, set, hctx, i); |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1691 | } |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | static void blk_mq_free_hw_queues(struct request_queue *q, |
| 1695 | struct blk_mq_tag_set *set) |
| 1696 | { |
| 1697 | struct blk_mq_hw_ctx *hctx; |
| 1698 | unsigned int i; |
| 1699 | |
Ming Lei | e09aae7e | 2015-01-29 20:17:27 +0800 | [diff] [blame] | 1700 | queue_for_each_hw_ctx(q, hctx, i) |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1701 | free_cpumask_var(hctx->cpumask); |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1704 | static int blk_mq_init_hctx(struct request_queue *q, |
| 1705 | struct blk_mq_tag_set *set, |
| 1706 | struct blk_mq_hw_ctx *hctx, unsigned hctx_idx) |
| 1707 | { |
| 1708 | int node; |
Ming Lei | f70ced0 | 2014-09-25 23:23:47 +0800 | [diff] [blame] | 1709 | unsigned flush_start_tag = set->queue_depth; |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1710 | |
| 1711 | node = hctx->numa_node; |
| 1712 | if (node == NUMA_NO_NODE) |
| 1713 | node = hctx->numa_node = set->numa_node; |
| 1714 | |
Jens Axboe | 27489a3 | 2016-08-24 15:54:25 -0600 | [diff] [blame] | 1715 | INIT_WORK(&hctx->run_work, blk_mq_run_work_fn); |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1716 | INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn); |
| 1717 | spin_lock_init(&hctx->lock); |
| 1718 | INIT_LIST_HEAD(&hctx->dispatch); |
| 1719 | hctx->queue = q; |
| 1720 | hctx->queue_num = hctx_idx; |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1721 | hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED; |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1722 | |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1723 | cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead); |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1724 | |
| 1725 | hctx->tags = set->tags[hctx_idx]; |
| 1726 | |
| 1727 | /* |
| 1728 | * Allocate space for all possible cpus to avoid allocation at |
| 1729 | * runtime |
| 1730 | */ |
| 1731 | hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *), |
| 1732 | GFP_KERNEL, node); |
| 1733 | if (!hctx->ctxs) |
| 1734 | goto unregister_cpu_notifier; |
| 1735 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 1736 | if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL, |
| 1737 | node)) |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1738 | goto free_ctxs; |
| 1739 | |
| 1740 | hctx->nr_ctx = 0; |
| 1741 | |
| 1742 | if (set->ops->init_hctx && |
| 1743 | set->ops->init_hctx(hctx, set->driver_data, hctx_idx)) |
| 1744 | goto free_bitmap; |
| 1745 | |
Ming Lei | f70ced0 | 2014-09-25 23:23:47 +0800 | [diff] [blame] | 1746 | hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size); |
| 1747 | if (!hctx->fq) |
| 1748 | goto exit_hctx; |
| 1749 | |
| 1750 | if (set->ops->init_request && |
| 1751 | set->ops->init_request(set->driver_data, |
| 1752 | hctx->fq->flush_rq, hctx_idx, |
| 1753 | flush_start_tag + hctx_idx, node)) |
| 1754 | goto free_fq; |
| 1755 | |
Bart Van Assche | 6a83e74 | 2016-11-02 10:09:51 -0600 | [diff] [blame] | 1756 | if (hctx->flags & BLK_MQ_F_BLOCKING) |
| 1757 | init_srcu_struct(&hctx->queue_rq_srcu); |
| 1758 | |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1759 | return 0; |
| 1760 | |
Ming Lei | f70ced0 | 2014-09-25 23:23:47 +0800 | [diff] [blame] | 1761 | free_fq: |
| 1762 | kfree(hctx->fq); |
| 1763 | exit_hctx: |
| 1764 | if (set->ops->exit_hctx) |
| 1765 | set->ops->exit_hctx(hctx, hctx_idx); |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1766 | free_bitmap: |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 1767 | sbitmap_free(&hctx->ctx_map); |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1768 | free_ctxs: |
| 1769 | kfree(hctx->ctxs); |
| 1770 | unregister_cpu_notifier: |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 1771 | blk_mq_remove_cpuhp(hctx); |
Ming Lei | 08e98fc | 2014-09-25 23:23:38 +0800 | [diff] [blame] | 1772 | return -1; |
| 1773 | } |
| 1774 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1775 | static void blk_mq_init_cpu_queues(struct request_queue *q, |
| 1776 | unsigned int nr_hw_queues) |
| 1777 | { |
| 1778 | unsigned int i; |
| 1779 | |
| 1780 | for_each_possible_cpu(i) { |
| 1781 | struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i); |
| 1782 | struct blk_mq_hw_ctx *hctx; |
| 1783 | |
| 1784 | memset(__ctx, 0, sizeof(*__ctx)); |
| 1785 | __ctx->cpu = i; |
| 1786 | spin_lock_init(&__ctx->lock); |
| 1787 | INIT_LIST_HEAD(&__ctx->rq_list); |
| 1788 | __ctx->queue = q; |
| 1789 | |
| 1790 | /* 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] | 1791 | if (!cpu_online(i)) |
| 1792 | continue; |
| 1793 | |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 1794 | hctx = blk_mq_map_queue(q, i); |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1795 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1796 | /* |
| 1797 | * Set local node, IFF we have more than one hw queue. If |
| 1798 | * not, we remain on the home node of the device |
| 1799 | */ |
| 1800 | if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE) |
Raghavendra K T | bffed45 | 2015-12-02 16:59:05 +0530 | [diff] [blame] | 1801 | hctx->numa_node = local_memory_node(cpu_to_node(i)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 1805 | static void blk_mq_map_swqueue(struct request_queue *q, |
| 1806 | const struct cpumask *online_mask) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1807 | { |
| 1808 | unsigned int i; |
| 1809 | struct blk_mq_hw_ctx *hctx; |
| 1810 | struct blk_mq_ctx *ctx; |
Ming Lei | 2a34c08 | 2015-04-21 10:00:20 +0800 | [diff] [blame] | 1811 | struct blk_mq_tag_set *set = q->tag_set; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1812 | |
Akinobu Mita | 60de074 | 2015-09-27 02:09:25 +0900 | [diff] [blame] | 1813 | /* |
| 1814 | * Avoid others reading imcomplete hctx->cpumask through sysfs |
| 1815 | */ |
| 1816 | mutex_lock(&q->sysfs_lock); |
| 1817 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1818 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1819 | cpumask_clear(hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1820 | hctx->nr_ctx = 0; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * Map software to hardware queues |
| 1825 | */ |
Thomas Gleixner | 897bb0c | 2016-03-19 11:30:33 +0100 | [diff] [blame] | 1826 | for_each_possible_cpu(i) { |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1827 | /* If the cpu isn't online, the cpu is mapped to first hctx */ |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 1828 | if (!cpumask_test_cpu(i, online_mask)) |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1829 | continue; |
| 1830 | |
Thomas Gleixner | 897bb0c | 2016-03-19 11:30:33 +0100 | [diff] [blame] | 1831 | ctx = per_cpu_ptr(q->queue_ctx, i); |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 1832 | hctx = blk_mq_map_queue(q, i); |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1833 | |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 1834 | cpumask_set_cpu(i, hctx->cpumask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1835 | ctx->index_hw = hctx->nr_ctx; |
| 1836 | hctx->ctxs[hctx->nr_ctx++] = ctx; |
| 1837 | } |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1838 | |
Akinobu Mita | 60de074 | 2015-09-27 02:09:25 +0900 | [diff] [blame] | 1839 | mutex_unlock(&q->sysfs_lock); |
| 1840 | |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1841 | queue_for_each_hw_ctx(q, hctx, i) { |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1842 | /* |
Jens Axboe | a68aafa | 2014-08-15 13:19:15 -0600 | [diff] [blame] | 1843 | * If no software queues are mapped to this hardware queue, |
| 1844 | * disable it and free the request entries. |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1845 | */ |
| 1846 | if (!hctx->nr_ctx) { |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1847 | if (set->tags[i]) { |
| 1848 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 1849 | set->tags[i] = NULL; |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1850 | } |
Ming Lei | 2a34c08 | 2015-04-21 10:00:20 +0800 | [diff] [blame] | 1851 | hctx->tags = NULL; |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1852 | continue; |
| 1853 | } |
| 1854 | |
Ming Lei | 2a34c08 | 2015-04-21 10:00:20 +0800 | [diff] [blame] | 1855 | /* unmapped hw queue can be remapped after CPU topo changed */ |
| 1856 | if (!set->tags[i]) |
| 1857 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 1858 | hctx->tags = set->tags[i]; |
| 1859 | WARN_ON(!hctx->tags); |
| 1860 | |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1861 | /* |
Chong Yuan | 889fa31 | 2015-04-15 11:39:29 -0600 | [diff] [blame] | 1862 | * Set the map size to the number of mapped software queues. |
| 1863 | * This is more accurate and more efficient than looping |
| 1864 | * over all possibly mapped software queues. |
| 1865 | */ |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 1866 | sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx); |
Chong Yuan | 889fa31 | 2015-04-15 11:39:29 -0600 | [diff] [blame] | 1867 | |
| 1868 | /* |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 1869 | * Initialize batch roundrobin counts |
| 1870 | */ |
Jens Axboe | 506e931 | 2014-05-07 10:26:44 -0600 | [diff] [blame] | 1871 | hctx->next_cpu = cpumask_first(hctx->cpumask); |
| 1872 | hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; |
| 1873 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1874 | } |
| 1875 | |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1876 | static void queue_set_hctx_shared(struct request_queue *q, bool shared) |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1877 | { |
| 1878 | struct blk_mq_hw_ctx *hctx; |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1879 | int i; |
| 1880 | |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1881 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1882 | if (shared) |
| 1883 | hctx->flags |= BLK_MQ_F_TAG_SHARED; |
| 1884 | else |
| 1885 | hctx->flags &= ~BLK_MQ_F_TAG_SHARED; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared) |
| 1890 | { |
| 1891 | struct request_queue *q; |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1892 | |
| 1893 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 1894 | blk_mq_freeze_queue(q); |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1895 | queue_set_hctx_shared(q, shared); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1896 | blk_mq_unfreeze_queue(q); |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | static void blk_mq_del_queue_tag_set(struct request_queue *q) |
| 1901 | { |
| 1902 | struct blk_mq_tag_set *set = q->tag_set; |
| 1903 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1904 | mutex_lock(&set->tag_list_lock); |
| 1905 | list_del_init(&q->tag_set_list); |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1906 | if (list_is_singular(&set->tag_list)) { |
| 1907 | /* just transitioned to unshared */ |
| 1908 | set->flags &= ~BLK_MQ_F_TAG_SHARED; |
| 1909 | /* update existing queue */ |
| 1910 | blk_mq_update_tag_set_depth(set, false); |
| 1911 | } |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1912 | mutex_unlock(&set->tag_list_lock); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set, |
| 1916 | struct request_queue *q) |
| 1917 | { |
| 1918 | q->tag_set = set; |
| 1919 | |
| 1920 | mutex_lock(&set->tag_list_lock); |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1921 | |
| 1922 | /* Check to see if we're transitioning to shared (from 1 to 2 queues). */ |
| 1923 | if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) { |
| 1924 | set->flags |= BLK_MQ_F_TAG_SHARED; |
| 1925 | /* update existing queue */ |
| 1926 | blk_mq_update_tag_set_depth(set, true); |
| 1927 | } |
| 1928 | if (set->flags & BLK_MQ_F_TAG_SHARED) |
| 1929 | queue_set_hctx_shared(q, true); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1930 | list_add_tail(&q->tag_set_list, &set->tag_list); |
Jeff Moyer | 2404e60 | 2015-11-03 10:40:06 -0500 | [diff] [blame] | 1931 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 1932 | mutex_unlock(&set->tag_list_lock); |
| 1933 | } |
| 1934 | |
Ming Lei | e09aae7e | 2015-01-29 20:17:27 +0800 | [diff] [blame] | 1935 | /* |
| 1936 | * It is the actual release handler for mq, but we do it from |
| 1937 | * request queue's release handler for avoiding use-after-free |
| 1938 | * and headache because q->mq_kobj shouldn't have been introduced, |
| 1939 | * but we can't group ctx/kctx kobj without it. |
| 1940 | */ |
| 1941 | void blk_mq_release(struct request_queue *q) |
| 1942 | { |
| 1943 | struct blk_mq_hw_ctx *hctx; |
| 1944 | unsigned int i; |
| 1945 | |
| 1946 | /* hctx kobj stays in hctx */ |
Ming Lei | c3b4afc | 2015-06-04 22:25:04 +0800 | [diff] [blame] | 1947 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1948 | if (!hctx) |
| 1949 | continue; |
| 1950 | kfree(hctx->ctxs); |
Ming Lei | e09aae7e | 2015-01-29 20:17:27 +0800 | [diff] [blame] | 1951 | kfree(hctx); |
Ming Lei | c3b4afc | 2015-06-04 22:25:04 +0800 | [diff] [blame] | 1952 | } |
Ming Lei | e09aae7e | 2015-01-29 20:17:27 +0800 | [diff] [blame] | 1953 | |
Akinobu Mita | a723bab | 2015-09-27 02:09:21 +0900 | [diff] [blame] | 1954 | q->mq_map = NULL; |
| 1955 | |
Ming Lei | e09aae7e | 2015-01-29 20:17:27 +0800 | [diff] [blame] | 1956 | kfree(q->queue_hw_ctx); |
| 1957 | |
| 1958 | /* ctx kobj stays in queue_ctx */ |
| 1959 | free_percpu(q->queue_ctx); |
| 1960 | } |
| 1961 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1962 | 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] | 1963 | { |
Mike Snitzer | b62c21b | 2015-03-12 23:56:02 -0400 | [diff] [blame] | 1964 | struct request_queue *uninit_q, *q; |
| 1965 | |
| 1966 | uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node); |
| 1967 | if (!uninit_q) |
| 1968 | return ERR_PTR(-ENOMEM); |
| 1969 | |
| 1970 | q = blk_mq_init_allocated_queue(set, uninit_q); |
| 1971 | if (IS_ERR(q)) |
| 1972 | blk_cleanup_queue(uninit_q); |
| 1973 | |
| 1974 | return q; |
| 1975 | } |
| 1976 | EXPORT_SYMBOL(blk_mq_init_queue); |
| 1977 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1978 | static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, |
| 1979 | struct request_queue *q) |
Mike Snitzer | b62c21b | 2015-03-12 23:56:02 -0400 | [diff] [blame] | 1980 | { |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1981 | int i, j; |
| 1982 | struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1983 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1984 | blk_mq_sysfs_unregister(q); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1985 | for (i = 0; i < set->nr_hw_queues; i++) { |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1986 | int node; |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 1987 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1988 | if (hctxs[i]) |
| 1989 | continue; |
| 1990 | |
| 1991 | node = blk_mq_hw_queue_to_node(q->mq_map, i); |
Christoph Hellwig | cdef54d | 2014-05-28 18:11:06 +0200 | [diff] [blame] | 1992 | hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx), |
| 1993 | GFP_KERNEL, node); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1994 | if (!hctxs[i]) |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1995 | break; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 1996 | |
Jens Axboe | a86073e | 2014-10-13 15:41:54 -0600 | [diff] [blame] | 1997 | if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL, |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 1998 | node)) { |
| 1999 | kfree(hctxs[i]); |
| 2000 | hctxs[i] = NULL; |
| 2001 | break; |
| 2002 | } |
Jens Axboe | e4043dc | 2014-04-09 10:18:23 -0600 | [diff] [blame] | 2003 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2004 | atomic_set(&hctxs[i]->nr_active, 0); |
Jens Axboe | f14bbe7 | 2014-05-27 12:06:53 -0600 | [diff] [blame] | 2005 | hctxs[i]->numa_node = node; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2006 | hctxs[i]->queue_num = i; |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2007 | |
| 2008 | if (blk_mq_init_hctx(q, set, hctxs[i], i)) { |
| 2009 | free_cpumask_var(hctxs[i]->cpumask); |
| 2010 | kfree(hctxs[i]); |
| 2011 | hctxs[i] = NULL; |
| 2012 | break; |
| 2013 | } |
| 2014 | blk_mq_hctx_kobj_init(hctxs[i]); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2015 | } |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2016 | for (j = i; j < q->nr_hw_queues; j++) { |
| 2017 | struct blk_mq_hw_ctx *hctx = hctxs[j]; |
| 2018 | |
| 2019 | if (hctx) { |
| 2020 | if (hctx->tags) { |
| 2021 | blk_mq_free_rq_map(set, hctx->tags, j); |
| 2022 | set->tags[j] = NULL; |
| 2023 | } |
| 2024 | blk_mq_exit_hctx(q, set, hctx, j); |
| 2025 | free_cpumask_var(hctx->cpumask); |
| 2026 | kobject_put(&hctx->kobj); |
| 2027 | kfree(hctx->ctxs); |
| 2028 | kfree(hctx); |
| 2029 | hctxs[j] = NULL; |
| 2030 | |
| 2031 | } |
| 2032 | } |
| 2033 | q->nr_hw_queues = i; |
| 2034 | blk_mq_sysfs_register(q); |
| 2035 | } |
| 2036 | |
| 2037 | struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, |
| 2038 | struct request_queue *q) |
| 2039 | { |
Ming Lei | 6684167 | 2016-02-12 15:27:00 +0800 | [diff] [blame] | 2040 | /* mark the queue as mq asap */ |
| 2041 | q->mq_ops = set->ops; |
| 2042 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2043 | q->queue_ctx = alloc_percpu(struct blk_mq_ctx); |
| 2044 | if (!q->queue_ctx) |
Ming Lin | c7de572 | 2016-05-25 23:23:27 -0700 | [diff] [blame] | 2045 | goto err_exit; |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2046 | |
| 2047 | q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)), |
| 2048 | GFP_KERNEL, set->numa_node); |
| 2049 | if (!q->queue_hw_ctx) |
| 2050 | goto err_percpu; |
| 2051 | |
Christoph Hellwig | bdd17e7 | 2016-09-14 16:18:53 +0200 | [diff] [blame] | 2052 | q->mq_map = set->mq_map; |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2053 | |
| 2054 | blk_mq_realloc_hw_ctxs(set, q); |
| 2055 | if (!q->nr_hw_queues) |
| 2056 | goto err_hctxs; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2057 | |
Christoph Hellwig | 287922e | 2015-10-30 20:57:30 +0800 | [diff] [blame] | 2058 | INIT_WORK(&q->timeout_work, blk_mq_timeout_work); |
Ming Lei | e56f698 | 2015-07-16 19:53:22 +0800 | [diff] [blame] | 2059 | blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2060 | |
| 2061 | q->nr_queues = nr_cpu_ids; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2062 | |
Jens Axboe | 94eddfb | 2013-11-19 09:25:07 -0700 | [diff] [blame] | 2063 | q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2064 | |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 2065 | if (!(set->flags & BLK_MQ_F_SG_MERGE)) |
| 2066 | q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE; |
| 2067 | |
Christoph Hellwig | 1be036e | 2014-02-07 10:22:39 -0800 | [diff] [blame] | 2068 | q->sg_reserved_size = INT_MAX; |
| 2069 | |
Mike Snitzer | 2849450 | 2016-09-14 13:28:30 -0400 | [diff] [blame] | 2070 | INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work); |
Christoph Hellwig | 6fca6a6 | 2014-05-28 08:08:02 -0600 | [diff] [blame] | 2071 | INIT_LIST_HEAD(&q->requeue_list); |
| 2072 | spin_lock_init(&q->requeue_lock); |
| 2073 | |
Jens Axboe | 07068d5 | 2014-05-22 10:40:51 -0600 | [diff] [blame] | 2074 | if (q->nr_hw_queues > 1) |
| 2075 | blk_queue_make_request(q, blk_mq_make_request); |
| 2076 | else |
| 2077 | blk_queue_make_request(q, blk_sq_make_request); |
| 2078 | |
Jens Axboe | eba7176 | 2014-05-20 15:17:27 -0600 | [diff] [blame] | 2079 | /* |
| 2080 | * Do this after blk_queue_make_request() overrides it... |
| 2081 | */ |
| 2082 | q->nr_requests = set->queue_depth; |
| 2083 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2084 | if (set->ops->complete) |
| 2085 | blk_queue_softirq_done(q, set->ops->complete); |
Christoph Hellwig | 30a91cb | 2014-02-10 03:24:38 -0800 | [diff] [blame] | 2086 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2087 | blk_mq_init_cpu_queues(q, set->nr_hw_queues); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2088 | |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 2089 | get_online_cpus(); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2090 | mutex_lock(&all_q_mutex); |
Akinobu Mita | 4593fdb | 2015-09-27 02:09:20 +0900 | [diff] [blame] | 2091 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2092 | list_add_tail(&q->all_q_node, &all_q_list); |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2093 | blk_mq_add_queue_tag_set(set, q); |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 2094 | blk_mq_map_swqueue(q, cpu_online_mask); |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 2095 | |
Akinobu Mita | 4593fdb | 2015-09-27 02:09:20 +0900 | [diff] [blame] | 2096 | mutex_unlock(&all_q_mutex); |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 2097 | put_online_cpus(); |
Akinobu Mita | 4593fdb | 2015-09-27 02:09:20 +0900 | [diff] [blame] | 2098 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2099 | return q; |
Christoph Hellwig | 1874198 | 2014-02-10 09:29:00 -0700 | [diff] [blame] | 2100 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2101 | err_hctxs: |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2102 | kfree(q->queue_hw_ctx); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2103 | err_percpu: |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2104 | free_percpu(q->queue_ctx); |
Ming Lin | c7de572 | 2016-05-25 23:23:27 -0700 | [diff] [blame] | 2105 | err_exit: |
| 2106 | q->mq_ops = NULL; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2107 | return ERR_PTR(-ENOMEM); |
| 2108 | } |
Mike Snitzer | b62c21b | 2015-03-12 23:56:02 -0400 | [diff] [blame] | 2109 | EXPORT_SYMBOL(blk_mq_init_allocated_queue); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2110 | |
| 2111 | void blk_mq_free_queue(struct request_queue *q) |
| 2112 | { |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 2113 | struct blk_mq_tag_set *set = q->tag_set; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2114 | |
Akinobu Mita | 0e62636 | 2015-09-27 02:09:22 +0900 | [diff] [blame] | 2115 | mutex_lock(&all_q_mutex); |
| 2116 | list_del_init(&q->all_q_node); |
| 2117 | mutex_unlock(&all_q_mutex); |
| 2118 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2119 | blk_mq_del_queue_tag_set(q); |
| 2120 | |
Ming Lei | 624dbe4 | 2014-05-27 23:35:13 +0800 | [diff] [blame] | 2121 | blk_mq_exit_hw_queues(q, set, set->nr_hw_queues); |
| 2122 | blk_mq_free_hw_queues(q, set); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2123 | } |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2124 | |
| 2125 | /* Basically redo blk_mq_init_queue with queue frozen */ |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 2126 | static void blk_mq_queue_reinit(struct request_queue *q, |
| 2127 | const struct cpumask *online_mask) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2128 | { |
Christoph Hellwig | 4ecd4fe | 2015-05-07 09:38:13 +0200 | [diff] [blame] | 2129 | WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth)); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2130 | |
Jens Axboe | 67aec14 | 2014-05-30 08:25:36 -0600 | [diff] [blame] | 2131 | blk_mq_sysfs_unregister(q); |
| 2132 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2133 | /* |
| 2134 | * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe |
| 2135 | * we should change hctx numa_node according to new topology (this |
| 2136 | * involves free and re-allocate memory, worthy doing?) |
| 2137 | */ |
| 2138 | |
Akinobu Mita | 5778322 | 2015-09-27 02:09:23 +0900 | [diff] [blame] | 2139 | blk_mq_map_swqueue(q, online_mask); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2140 | |
Jens Axboe | 67aec14 | 2014-05-30 08:25:36 -0600 | [diff] [blame] | 2141 | blk_mq_sysfs_register(q); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2142 | } |
| 2143 | |
Sebastian Andrzej Siewior | 65d5291 | 2016-09-22 08:05:19 -0600 | [diff] [blame] | 2144 | /* |
| 2145 | * New online cpumask which is going to be set in this hotplug event. |
| 2146 | * Declare this cpumasks as global as cpu-hotplug operation is invoked |
| 2147 | * one-by-one and dynamically allocating this could result in a failure. |
| 2148 | */ |
| 2149 | static struct cpumask cpuhp_online_new; |
| 2150 | |
| 2151 | static void blk_mq_queue_reinit_work(void) |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2152 | { |
| 2153 | struct request_queue *q; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2154 | |
| 2155 | mutex_lock(&all_q_mutex); |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 2156 | /* |
| 2157 | * We need to freeze and reinit all existing queues. Freezing |
| 2158 | * involves synchronous wait for an RCU grace period and doing it |
| 2159 | * one by one may take a long time. Start freezing all queues in |
| 2160 | * one swoop and then wait for the completions so that freezing can |
| 2161 | * take place in parallel. |
| 2162 | */ |
| 2163 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 2164 | blk_mq_freeze_queue_start(q); |
Ming Lei | f054b56 | 2015-04-21 10:00:19 +0800 | [diff] [blame] | 2165 | list_for_each_entry(q, &all_q_list, all_q_node) { |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 2166 | blk_mq_freeze_queue_wait(q); |
| 2167 | |
Ming Lei | f054b56 | 2015-04-21 10:00:19 +0800 | [diff] [blame] | 2168 | /* |
| 2169 | * timeout handler can't touch hw queue during the |
| 2170 | * reinitialization |
| 2171 | */ |
| 2172 | del_timer_sync(&q->timeout); |
| 2173 | } |
| 2174 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2175 | list_for_each_entry(q, &all_q_list, all_q_node) |
Sebastian Andrzej Siewior | 65d5291 | 2016-09-22 08:05:19 -0600 | [diff] [blame] | 2176 | blk_mq_queue_reinit(q, &cpuhp_online_new); |
Tejun Heo | f3af020 | 2014-11-04 13:52:27 -0500 | [diff] [blame] | 2177 | |
| 2178 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 2179 | blk_mq_unfreeze_queue(q); |
| 2180 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2181 | mutex_unlock(&all_q_mutex); |
Sebastian Andrzej Siewior | 65d5291 | 2016-09-22 08:05:19 -0600 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | static int blk_mq_queue_reinit_dead(unsigned int cpu) |
| 2185 | { |
Sebastian Andrzej Siewior | 97a3286 | 2016-09-23 15:02:38 +0200 | [diff] [blame] | 2186 | cpumask_copy(&cpuhp_online_new, cpu_online_mask); |
Sebastian Andrzej Siewior | 65d5291 | 2016-09-22 08:05:19 -0600 | [diff] [blame] | 2187 | blk_mq_queue_reinit_work(); |
| 2188 | return 0; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Before hotadded cpu starts handling requests, new mappings must be |
| 2193 | * established. Otherwise, these requests in hw queue might never be |
| 2194 | * dispatched. |
| 2195 | * |
| 2196 | * For example, there is a single hw queue (hctx) and two CPU queues (ctx0 |
| 2197 | * for CPU0, and ctx1 for CPU1). |
| 2198 | * |
| 2199 | * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list |
| 2200 | * and set bit0 in pending bitmap as ctx1->index_hw is still zero. |
| 2201 | * |
| 2202 | * And then while running hw queue, flush_busy_ctxs() finds bit0 is set in |
| 2203 | * pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list. |
| 2204 | * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list |
| 2205 | * is ignored. |
| 2206 | */ |
| 2207 | static int blk_mq_queue_reinit_prepare(unsigned int cpu) |
| 2208 | { |
| 2209 | cpumask_copy(&cpuhp_online_new, cpu_online_mask); |
| 2210 | cpumask_set_cpu(cpu, &cpuhp_online_new); |
| 2211 | blk_mq_queue_reinit_work(); |
| 2212 | return 0; |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2213 | } |
| 2214 | |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 2215 | static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
| 2216 | { |
| 2217 | int i; |
| 2218 | |
| 2219 | for (i = 0; i < set->nr_hw_queues; i++) { |
| 2220 | set->tags[i] = blk_mq_init_rq_map(set, i); |
| 2221 | if (!set->tags[i]) |
| 2222 | goto out_unwind; |
| 2223 | } |
| 2224 | |
| 2225 | return 0; |
| 2226 | |
| 2227 | out_unwind: |
| 2228 | while (--i >= 0) |
| 2229 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2230 | |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 2231 | return -ENOMEM; |
| 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | * Allocate the request maps associated with this tag_set. Note that this |
| 2236 | * may reduce the depth asked for, if memory is tight. set->queue_depth |
| 2237 | * will be updated to reflect the allocated depth. |
| 2238 | */ |
| 2239 | static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set) |
| 2240 | { |
| 2241 | unsigned int depth; |
| 2242 | int err; |
| 2243 | |
| 2244 | depth = set->queue_depth; |
| 2245 | do { |
| 2246 | err = __blk_mq_alloc_rq_maps(set); |
| 2247 | if (!err) |
| 2248 | break; |
| 2249 | |
| 2250 | set->queue_depth >>= 1; |
| 2251 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) { |
| 2252 | err = -ENOMEM; |
| 2253 | break; |
| 2254 | } |
| 2255 | } while (set->queue_depth); |
| 2256 | |
| 2257 | if (!set->queue_depth || err) { |
| 2258 | pr_err("blk-mq: failed to allocate request map\n"); |
| 2259 | return -ENOMEM; |
| 2260 | } |
| 2261 | |
| 2262 | if (depth != set->queue_depth) |
| 2263 | pr_info("blk-mq: reduced tag depth (%u -> %u)\n", |
| 2264 | depth, set->queue_depth); |
| 2265 | |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame] | 2269 | /* |
| 2270 | * Alloc a tag set to be associated with one or more request queues. |
| 2271 | * May fail with EINVAL for various error conditions. May adjust the |
| 2272 | * requested depth down, if if it too large. In that case, the set |
| 2273 | * value will be stored in set->queue_depth. |
| 2274 | */ |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2275 | int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) |
| 2276 | { |
Christoph Hellwig | da695ba | 2016-09-14 16:18:55 +0200 | [diff] [blame] | 2277 | int ret; |
| 2278 | |
Bart Van Assche | 205fb5f | 2014-10-30 14:45:11 +0100 | [diff] [blame] | 2279 | BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS); |
| 2280 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2281 | if (!set->nr_hw_queues) |
| 2282 | return -EINVAL; |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame] | 2283 | if (!set->queue_depth) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2284 | return -EINVAL; |
| 2285 | if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) |
| 2286 | return -EINVAL; |
| 2287 | |
Christoph Hellwig | 7d7e0f9 | 2016-09-14 16:18:54 +0200 | [diff] [blame] | 2288 | if (!set->ops->queue_rq) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2289 | return -EINVAL; |
| 2290 | |
Jens Axboe | a4391c6 | 2014-06-05 15:21:56 -0600 | [diff] [blame] | 2291 | if (set->queue_depth > BLK_MQ_MAX_DEPTH) { |
| 2292 | pr_info("blk-mq: reduced tag depth to %u\n", |
| 2293 | BLK_MQ_MAX_DEPTH); |
| 2294 | set->queue_depth = BLK_MQ_MAX_DEPTH; |
| 2295 | } |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2296 | |
Shaohua Li | 6637fad | 2014-11-30 16:00:58 -0800 | [diff] [blame] | 2297 | /* |
| 2298 | * If a crashdump is active, then we are potentially in a very |
| 2299 | * memory constrained environment. Limit us to 1 queue and |
| 2300 | * 64 tags to prevent using too much memory. |
| 2301 | */ |
| 2302 | if (is_kdump_kernel()) { |
| 2303 | set->nr_hw_queues = 1; |
| 2304 | set->queue_depth = min(64U, set->queue_depth); |
| 2305 | } |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2306 | /* |
| 2307 | * There is no use for more h/w queues than cpus. |
| 2308 | */ |
| 2309 | if (set->nr_hw_queues > nr_cpu_ids) |
| 2310 | set->nr_hw_queues = nr_cpu_ids; |
Shaohua Li | 6637fad | 2014-11-30 16:00:58 -0800 | [diff] [blame] | 2311 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2312 | set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *), |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2313 | GFP_KERNEL, set->numa_node); |
| 2314 | if (!set->tags) |
Jens Axboe | a516440 | 2014-09-10 09:02:03 -0600 | [diff] [blame] | 2315 | return -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2316 | |
Christoph Hellwig | da695ba | 2016-09-14 16:18:55 +0200 | [diff] [blame] | 2317 | ret = -ENOMEM; |
| 2318 | set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids, |
| 2319 | GFP_KERNEL, set->numa_node); |
Christoph Hellwig | bdd17e7 | 2016-09-14 16:18:53 +0200 | [diff] [blame] | 2320 | if (!set->mq_map) |
| 2321 | goto out_free_tags; |
| 2322 | |
Christoph Hellwig | da695ba | 2016-09-14 16:18:55 +0200 | [diff] [blame] | 2323 | if (set->ops->map_queues) |
| 2324 | ret = set->ops->map_queues(set); |
| 2325 | else |
| 2326 | ret = blk_mq_map_queues(set); |
| 2327 | if (ret) |
| 2328 | goto out_free_mq_map; |
| 2329 | |
| 2330 | ret = blk_mq_alloc_rq_maps(set); |
| 2331 | if (ret) |
Christoph Hellwig | bdd17e7 | 2016-09-14 16:18:53 +0200 | [diff] [blame] | 2332 | goto out_free_mq_map; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2333 | |
Jens Axboe | 0d2602c | 2014-05-13 15:10:52 -0600 | [diff] [blame] | 2334 | mutex_init(&set->tag_list_lock); |
| 2335 | INIT_LIST_HEAD(&set->tag_list); |
| 2336 | |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2337 | return 0; |
Christoph Hellwig | bdd17e7 | 2016-09-14 16:18:53 +0200 | [diff] [blame] | 2338 | |
| 2339 | out_free_mq_map: |
| 2340 | kfree(set->mq_map); |
| 2341 | set->mq_map = NULL; |
| 2342 | out_free_tags: |
Robert Elliott | 5676e7b | 2014-09-02 11:38:44 -0500 | [diff] [blame] | 2343 | kfree(set->tags); |
| 2344 | set->tags = NULL; |
Christoph Hellwig | da695ba | 2016-09-14 16:18:55 +0200 | [diff] [blame] | 2345 | return ret; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2346 | } |
| 2347 | EXPORT_SYMBOL(blk_mq_alloc_tag_set); |
| 2348 | |
| 2349 | void blk_mq_free_tag_set(struct blk_mq_tag_set *set) |
| 2350 | { |
| 2351 | int i; |
| 2352 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2353 | for (i = 0; i < nr_cpu_ids; i++) { |
Junichi Nomura | f42d79a | 2015-10-14 05:02:15 +0000 | [diff] [blame] | 2354 | if (set->tags[i]) |
Jens Axboe | 484b406 | 2014-05-21 14:01:15 -0600 | [diff] [blame] | 2355 | blk_mq_free_rq_map(set, set->tags[i], i); |
| 2356 | } |
| 2357 | |
Christoph Hellwig | bdd17e7 | 2016-09-14 16:18:53 +0200 | [diff] [blame] | 2358 | kfree(set->mq_map); |
| 2359 | set->mq_map = NULL; |
| 2360 | |
Ming Lei | 981bd18 | 2014-04-24 00:07:34 +0800 | [diff] [blame] | 2361 | kfree(set->tags); |
Robert Elliott | 5676e7b | 2014-09-02 11:38:44 -0500 | [diff] [blame] | 2362 | set->tags = NULL; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 2363 | } |
| 2364 | EXPORT_SYMBOL(blk_mq_free_tag_set); |
| 2365 | |
Jens Axboe | e3a2b3f | 2014-05-20 11:49:02 -0600 | [diff] [blame] | 2366 | int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr) |
| 2367 | { |
| 2368 | struct blk_mq_tag_set *set = q->tag_set; |
| 2369 | struct blk_mq_hw_ctx *hctx; |
| 2370 | int i, ret; |
| 2371 | |
| 2372 | if (!set || nr > set->queue_depth) |
| 2373 | return -EINVAL; |
| 2374 | |
| 2375 | ret = 0; |
| 2376 | queue_for_each_hw_ctx(q, hctx, i) { |
Keith Busch | e9137d4 | 2016-02-18 14:56:35 -0700 | [diff] [blame] | 2377 | if (!hctx->tags) |
| 2378 | continue; |
Jens Axboe | e3a2b3f | 2014-05-20 11:49:02 -0600 | [diff] [blame] | 2379 | ret = blk_mq_tag_update_depth(hctx->tags, nr); |
| 2380 | if (ret) |
| 2381 | break; |
| 2382 | } |
| 2383 | |
| 2384 | if (!ret) |
| 2385 | q->nr_requests = nr; |
| 2386 | |
| 2387 | return ret; |
| 2388 | } |
| 2389 | |
Keith Busch | 868f2f0 | 2015-12-17 17:08:14 -0700 | [diff] [blame] | 2390 | void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues) |
| 2391 | { |
| 2392 | struct request_queue *q; |
| 2393 | |
| 2394 | if (nr_hw_queues > nr_cpu_ids) |
| 2395 | nr_hw_queues = nr_cpu_ids; |
| 2396 | if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues) |
| 2397 | return; |
| 2398 | |
| 2399 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
| 2400 | blk_mq_freeze_queue(q); |
| 2401 | |
| 2402 | set->nr_hw_queues = nr_hw_queues; |
| 2403 | list_for_each_entry(q, &set->tag_list, tag_set_list) { |
| 2404 | blk_mq_realloc_hw_ctxs(set, q); |
| 2405 | |
| 2406 | if (q->nr_hw_queues > 1) |
| 2407 | blk_queue_make_request(q, blk_mq_make_request); |
| 2408 | else |
| 2409 | blk_queue_make_request(q, blk_sq_make_request); |
| 2410 | |
| 2411 | blk_mq_queue_reinit(q, cpu_online_mask); |
| 2412 | } |
| 2413 | |
| 2414 | list_for_each_entry(q, &set->tag_list, tag_set_list) |
| 2415 | blk_mq_unfreeze_queue(q); |
| 2416 | } |
| 2417 | EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues); |
| 2418 | |
Jens Axboe | 676141e | 2014-03-20 13:29:18 -0600 | [diff] [blame] | 2419 | void blk_mq_disable_hotplug(void) |
| 2420 | { |
| 2421 | mutex_lock(&all_q_mutex); |
| 2422 | } |
| 2423 | |
| 2424 | void blk_mq_enable_hotplug(void) |
| 2425 | { |
| 2426 | mutex_unlock(&all_q_mutex); |
| 2427 | } |
| 2428 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2429 | static int __init blk_mq_init(void) |
| 2430 | { |
Thomas Gleixner | 9467f85 | 2016-09-22 08:05:17 -0600 | [diff] [blame] | 2431 | cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL, |
| 2432 | blk_mq_hctx_notify_dead); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2433 | |
Sebastian Andrzej Siewior | 65d5291 | 2016-09-22 08:05:19 -0600 | [diff] [blame] | 2434 | cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE, "block/mq:prepare", |
| 2435 | blk_mq_queue_reinit_prepare, |
| 2436 | blk_mq_queue_reinit_dead); |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 2437 | return 0; |
| 2438 | } |
| 2439 | subsys_initcall(blk_mq_init); |