Joel Stanley | a50627d | 2019-04-08 16:44:10 +0930 | [diff] [blame] | 1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | From: Bart Van Assche <bvanassche@acm.org> |
| 3 | Date: Thu, 4 Apr 2019 10:08:43 -0700 |
| 4 | Subject: [PATCH 2/3] block: Revert v5.0 blk_mq_request_issue_directly() |
| 5 | changes |
| 6 | |
| 7 | blk_mq_try_issue_directly() can return BLK_STS*_RESOURCE for requests that |
| 8 | have been queued. If that happens when blk_mq_try_issue_directly() is called |
| 9 | by the dm-mpath driver then dm-mpath will try to resubmit a request that is |
| 10 | already queued and a kernel crash follows. Since it is nontrivial to fix |
| 11 | blk_mq_request_issue_directly(), revert the blk_mq_request_issue_directly() |
| 12 | changes that went into kernel v5.0. |
| 13 | |
| 14 | This patch reverts the following commits: |
| 15 | * d6a51a97c0b2 ("blk-mq: replace and kill blk_mq_request_issue_directly") # v5.0. |
| 16 | * 5b7a6f128aad ("blk-mq: issue directly with bypass 'false' in blk_mq_sched_insert_requests") # v5.0. |
| 17 | * 7f556a44e61d ("blk-mq: refactor the code of issue request directly") # v5.0. |
| 18 | |
| 19 | Cc: Christoph Hellwig <hch@infradead.org> |
| 20 | Cc: Ming Lei <ming.lei@redhat.com> |
| 21 | Cc: Jianchao Wang <jianchao.w.wang@oracle.com> |
| 22 | Cc: Hannes Reinecke <hare@suse.com> |
| 23 | Cc: Johannes Thumshirn <jthumshirn@suse.de> |
| 24 | Cc: James Smart <james.smart@broadcom.com> |
| 25 | Cc: Dongli Zhang <dongli.zhang@oracle.com> |
| 26 | Cc: Laurence Oberman <loberman@redhat.com> |
| 27 | Cc: <stable@vger.kernel.org> |
| 28 | Reported-by: Laurence Oberman <loberman@redhat.com> |
| 29 | Tested-by: Laurence Oberman <loberman@redhat.com> |
| 30 | Fixes: 7f556a44e61d ("blk-mq: refactor the code of issue request directly") # v5.0. |
| 31 | Signed-off-by: Bart Van Assche <bvanassche@acm.org> |
| 32 | Signed-off-by: Jens Axboe <axboe@kernel.dk> |
| 33 | (cherry picked from commit fd9c40f64c514bdc585a21e2e33fa5f83ca8811b) |
| 34 | Signed-off-by: Joel Stanley <joel@jms.id.au> |
| 35 | --- |
| 36 | block/blk-core.c | 4 +- |
| 37 | block/blk-mq-sched.c | 8 +-- |
| 38 | block/blk-mq.c | 122 ++++++++++++++++++++++--------------------- |
| 39 | block/blk-mq.h | 6 +-- |
| 40 | 4 files changed, 71 insertions(+), 69 deletions(-) |
| 41 | |
| 42 | diff --git a/block/blk-core.c b/block/blk-core.c |
| 43 | index 6b78ec56a4f2..5bde73a49399 100644 |
| 44 | --- a/block/blk-core.c |
| 45 | +++ b/block/blk-core.c |
| 46 | @@ -1246,8 +1246,6 @@ static int blk_cloned_rq_check_limits(struct request_queue *q, |
| 47 | */ |
| 48 | blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq) |
| 49 | { |
| 50 | - blk_qc_t unused; |
| 51 | - |
| 52 | if (blk_cloned_rq_check_limits(q, rq)) |
| 53 | return BLK_STS_IOERR; |
| 54 | |
| 55 | @@ -1263,7 +1261,7 @@ blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request * |
| 56 | * bypass a potential scheduler on the bottom device for |
| 57 | * insert. |
| 58 | */ |
| 59 | - return blk_mq_try_issue_directly(rq->mq_hctx, rq, &unused, true, true); |
| 60 | + return blk_mq_request_issue_directly(rq, true); |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(blk_insert_cloned_request); |
| 63 | |
| 64 | diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c |
| 65 | index 140933e4a7d1..0c98b6c1ca49 100644 |
| 66 | --- a/block/blk-mq-sched.c |
| 67 | +++ b/block/blk-mq-sched.c |
| 68 | @@ -423,10 +423,12 @@ void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx, |
| 69 | * busy in case of 'none' scheduler, and this way may save |
| 70 | * us one extra enqueue & dequeue to sw queue. |
| 71 | */ |
| 72 | - if (!hctx->dispatch_busy && !e && !run_queue_async) |
| 73 | + if (!hctx->dispatch_busy && !e && !run_queue_async) { |
| 74 | blk_mq_try_issue_list_directly(hctx, list); |
| 75 | - else |
| 76 | - blk_mq_insert_requests(hctx, ctx, list); |
| 77 | + if (list_empty(list)) |
| 78 | + return; |
| 79 | + } |
| 80 | + blk_mq_insert_requests(hctx, ctx, list); |
| 81 | } |
| 82 | |
| 83 | blk_mq_run_hw_queue(hctx, run_queue_async); |
| 84 | diff --git a/block/blk-mq.c b/block/blk-mq.c |
| 85 | index b9283b63d116..16f9675c57e6 100644 |
| 86 | --- a/block/blk-mq.c |
| 87 | +++ b/block/blk-mq.c |
| 88 | @@ -1805,74 +1805,76 @@ static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | -blk_status_t blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 93 | +static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 94 | struct request *rq, |
| 95 | blk_qc_t *cookie, |
| 96 | - bool bypass, bool last) |
| 97 | + bool bypass_insert, bool last) |
| 98 | { |
| 99 | struct request_queue *q = rq->q; |
| 100 | bool run_queue = true; |
| 101 | - blk_status_t ret = BLK_STS_RESOURCE; |
| 102 | - int srcu_idx; |
| 103 | - bool force = false; |
| 104 | |
| 105 | - hctx_lock(hctx, &srcu_idx); |
| 106 | /* |
| 107 | - * hctx_lock is needed before checking quiesced flag. |
| 108 | + * RCU or SRCU read lock is needed before checking quiesced flag. |
| 109 | * |
| 110 | - * When queue is stopped or quiesced, ignore 'bypass', insert |
| 111 | - * and return BLK_STS_OK to caller, and avoid driver to try to |
| 112 | - * dispatch again. |
| 113 | + * When queue is stopped or quiesced, ignore 'bypass_insert' from |
| 114 | + * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller, |
| 115 | + * and avoid driver to try to dispatch again. |
| 116 | */ |
| 117 | - if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q))) { |
| 118 | + if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) { |
| 119 | run_queue = false; |
| 120 | - bypass = false; |
| 121 | - goto out_unlock; |
| 122 | + bypass_insert = false; |
| 123 | + goto insert; |
| 124 | } |
| 125 | |
| 126 | - if (unlikely(q->elevator && !bypass)) |
| 127 | - goto out_unlock; |
| 128 | + if (q->elevator && !bypass_insert) |
| 129 | + goto insert; |
| 130 | |
| 131 | if (!blk_mq_get_dispatch_budget(hctx)) |
| 132 | - goto out_unlock; |
| 133 | + goto insert; |
| 134 | |
| 135 | if (!blk_mq_get_driver_tag(rq)) { |
| 136 | blk_mq_put_dispatch_budget(hctx); |
| 137 | - goto out_unlock; |
| 138 | + goto insert; |
| 139 | } |
| 140 | |
| 141 | - /* |
| 142 | - * Always add a request that has been through |
| 143 | - *.queue_rq() to the hardware dispatch list. |
| 144 | - */ |
| 145 | - force = true; |
| 146 | - ret = __blk_mq_issue_directly(hctx, rq, cookie, last); |
| 147 | -out_unlock: |
| 148 | + return __blk_mq_issue_directly(hctx, rq, cookie, last); |
| 149 | +insert: |
| 150 | + if (bypass_insert) |
| 151 | + return BLK_STS_RESOURCE; |
| 152 | + |
| 153 | + blk_mq_request_bypass_insert(rq, run_queue); |
| 154 | + return BLK_STS_OK; |
| 155 | +} |
| 156 | + |
| 157 | +static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 158 | + struct request *rq, blk_qc_t *cookie) |
| 159 | +{ |
| 160 | + blk_status_t ret; |
| 161 | + int srcu_idx; |
| 162 | + |
| 163 | + might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); |
| 164 | + |
| 165 | + hctx_lock(hctx, &srcu_idx); |
| 166 | + |
| 167 | + ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true); |
| 168 | + if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) |
| 169 | + blk_mq_request_bypass_insert(rq, true); |
| 170 | + else if (ret != BLK_STS_OK) |
| 171 | + blk_mq_end_request(rq, ret); |
| 172 | + |
| 173 | + hctx_unlock(hctx, srcu_idx); |
| 174 | +} |
| 175 | + |
| 176 | +blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last) |
| 177 | +{ |
| 178 | + blk_status_t ret; |
| 179 | + int srcu_idx; |
| 180 | + blk_qc_t unused_cookie; |
| 181 | + struct blk_mq_hw_ctx *hctx = rq->mq_hctx; |
| 182 | + |
| 183 | + hctx_lock(hctx, &srcu_idx); |
| 184 | + ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last); |
| 185 | hctx_unlock(hctx, srcu_idx); |
| 186 | - switch (ret) { |
| 187 | - case BLK_STS_OK: |
| 188 | - break; |
| 189 | - case BLK_STS_DEV_RESOURCE: |
| 190 | - case BLK_STS_RESOURCE: |
| 191 | - if (force) { |
| 192 | - blk_mq_request_bypass_insert(rq, run_queue); |
| 193 | - /* |
| 194 | - * We have to return BLK_STS_OK for the DM |
| 195 | - * to avoid livelock. Otherwise, we return |
| 196 | - * the real result to indicate whether the |
| 197 | - * request is direct-issued successfully. |
| 198 | - */ |
| 199 | - ret = bypass ? BLK_STS_OK : ret; |
| 200 | - } else if (!bypass) { |
| 201 | - blk_mq_sched_insert_request(rq, false, |
| 202 | - run_queue, false); |
| 203 | - } |
| 204 | - break; |
| 205 | - default: |
| 206 | - if (!bypass) |
| 207 | - blk_mq_end_request(rq, ret); |
| 208 | - break; |
| 209 | - } |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | @@ -1880,20 +1882,22 @@ blk_status_t blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 214 | void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, |
| 215 | struct list_head *list) |
| 216 | { |
| 217 | - blk_qc_t unused; |
| 218 | - blk_status_t ret = BLK_STS_OK; |
| 219 | - |
| 220 | while (!list_empty(list)) { |
| 221 | + blk_status_t ret; |
| 222 | struct request *rq = list_first_entry(list, struct request, |
| 223 | queuelist); |
| 224 | |
| 225 | list_del_init(&rq->queuelist); |
| 226 | - if (ret == BLK_STS_OK) |
| 227 | - ret = blk_mq_try_issue_directly(hctx, rq, &unused, |
| 228 | - false, |
| 229 | + ret = blk_mq_request_issue_directly(rq, list_empty(list)); |
| 230 | + if (ret != BLK_STS_OK) { |
| 231 | + if (ret == BLK_STS_RESOURCE || |
| 232 | + ret == BLK_STS_DEV_RESOURCE) { |
| 233 | + blk_mq_request_bypass_insert(rq, |
| 234 | list_empty(list)); |
| 235 | - else |
| 236 | - blk_mq_sched_insert_request(rq, false, true, false); |
| 237 | + break; |
| 238 | + } |
| 239 | + blk_mq_end_request(rq, ret); |
| 240 | + } |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | @@ -1901,7 +1905,7 @@ void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, |
| 245 | * the driver there was more coming, but that turned out to |
| 246 | * be a lie. |
| 247 | */ |
| 248 | - if (ret != BLK_STS_OK && hctx->queue->mq_ops->commit_rqs) |
| 249 | + if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs) |
| 250 | hctx->queue->mq_ops->commit_rqs(hctx); |
| 251 | } |
| 252 | |
| 253 | @@ -2014,13 +2018,13 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio) |
| 254 | if (same_queue_rq) { |
| 255 | data.hctx = same_queue_rq->mq_hctx; |
| 256 | blk_mq_try_issue_directly(data.hctx, same_queue_rq, |
| 257 | - &cookie, false, true); |
| 258 | + &cookie); |
| 259 | } |
| 260 | } else if ((q->nr_hw_queues > 1 && is_sync) || (!q->elevator && |
| 261 | !data.hctx->dispatch_busy)) { |
| 262 | blk_mq_put_ctx(data.ctx); |
| 263 | blk_mq_bio_to_request(rq, bio); |
| 264 | - blk_mq_try_issue_directly(data.hctx, rq, &cookie, false, true); |
| 265 | + blk_mq_try_issue_directly(data.hctx, rq, &cookie); |
| 266 | } else { |
| 267 | blk_mq_put_ctx(data.ctx); |
| 268 | blk_mq_bio_to_request(rq, bio); |
| 269 | diff --git a/block/blk-mq.h b/block/blk-mq.h |
| 270 | index d0b3dd54ef8d..a3a684a8c633 100644 |
| 271 | --- a/block/blk-mq.h |
| 272 | +++ b/block/blk-mq.h |
| 273 | @@ -67,10 +67,8 @@ void blk_mq_request_bypass_insert(struct request *rq, bool run_queue); |
| 274 | void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx, |
| 275 | struct list_head *list); |
| 276 | |
| 277 | -blk_status_t blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx, |
| 278 | - struct request *rq, |
| 279 | - blk_qc_t *cookie, |
| 280 | - bool bypass, bool last); |
| 281 | +/* Used by blk_insert_cloned_request() to issue request directly */ |
| 282 | +blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last); |
| 283 | void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx, |
| 284 | struct list_head *list); |
| 285 | |