1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Block multiqueue core code
4 *
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
7 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/backing-dev.h>
11 #include <linux/bio.h>
12 #include <linux/blkdev.h>
13 #include <linux/kmemleak.h>
14 #include <linux/mm.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/workqueue.h>
18 #include <linux/smp.h>
19 #include <linux/llist.h>
20 #include <linux/list_sort.h>
21 #include <linux/cpu.h>
22 #include <linux/cache.h>
23 #include <linux/sched/sysctl.h>
24 #include <linux/sched/topology.h>
25 #include <linux/sched/signal.h>
26 #include <linux/delay.h>
27 #include <linux/crash_dump.h>
28 #include <linux/prefetch.h>
29 #include <linux/blk-crypto.h>
30
31 #include <trace/events/block.h>
32
33 #include <linux/blk-mq.h>
34 #include <linux/t10-pi.h>
35 #include "blk.h"
36 #include "blk-mq.h"
37 #include "blk-mq-debugfs.h"
38 #include "blk-mq-tag.h"
39 #include "blk-pm.h"
40 #include "blk-stat.h"
41 #include "blk-mq-sched.h"
42 #include "blk-rq-qos.h"
43
44 #include <trace/hooks/block.h>
45
46 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
47
48 static void blk_mq_poll_stats_start(struct request_queue *q);
49 static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
50
blk_mq_poll_stats_bkt(const struct request * rq)51 static int blk_mq_poll_stats_bkt(const struct request *rq)
52 {
53 int ddir, sectors, bucket;
54
55 ddir = rq_data_dir(rq);
56 sectors = blk_rq_stats_sectors(rq);
57
58 bucket = ddir + 2 * ilog2(sectors);
59
60 if (bucket < 0)
61 return -1;
62 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
63 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
64
65 return bucket;
66 }
67
68 /*
69 * Check if any of the ctx, dispatch list or elevator
70 * have pending work in this hardware queue.
71 */
blk_mq_hctx_has_pending(struct blk_mq_hw_ctx * hctx)72 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
73 {
74 return !list_empty_careful(&hctx->dispatch) ||
75 sbitmap_any_bit_set(&hctx->ctx_map) ||
76 blk_mq_sched_has_work(hctx);
77 }
78
79 /*
80 * Mark this ctx as having pending work in this hardware queue
81 */
blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx * hctx,struct blk_mq_ctx * ctx)82 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
83 struct blk_mq_ctx *ctx)
84 {
85 const int bit = ctx->index_hw[hctx->type];
86
87 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
88 sbitmap_set_bit(&hctx->ctx_map, bit);
89 }
90
blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx * hctx,struct blk_mq_ctx * ctx)91 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
92 struct blk_mq_ctx *ctx)
93 {
94 const int bit = ctx->index_hw[hctx->type];
95
96 sbitmap_clear_bit(&hctx->ctx_map, bit);
97 }
98
99 struct mq_inflight {
100 struct hd_struct *part;
101 unsigned int inflight[2];
102 };
103
blk_mq_check_inflight(struct blk_mq_hw_ctx * hctx,struct request * rq,void * priv,bool reserved)104 static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
105 struct request *rq, void *priv,
106 bool reserved)
107 {
108 struct mq_inflight *mi = priv;
109
110 if ((!mi->part->partno || rq->part == mi->part) &&
111 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
112 mi->inflight[rq_data_dir(rq)]++;
113
114 return true;
115 }
116
blk_mq_in_flight(struct request_queue * q,struct hd_struct * part)117 unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part)
118 {
119 struct mq_inflight mi = { .part = part };
120
121 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
122
123 return mi.inflight[0] + mi.inflight[1];
124 }
125
blk_mq_in_flight_rw(struct request_queue * q,struct hd_struct * part,unsigned int inflight[2])126 void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
127 unsigned int inflight[2])
128 {
129 struct mq_inflight mi = { .part = part };
130
131 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
132 inflight[0] = mi.inflight[0];
133 inflight[1] = mi.inflight[1];
134 }
135
blk_freeze_queue_start(struct request_queue * q)136 void blk_freeze_queue_start(struct request_queue *q)
137 {
138 mutex_lock(&q->mq_freeze_lock);
139 if (++q->mq_freeze_depth == 1) {
140 percpu_ref_kill(&q->q_usage_counter);
141 mutex_unlock(&q->mq_freeze_lock);
142 if (queue_is_mq(q))
143 blk_mq_run_hw_queues(q, false);
144 } else {
145 mutex_unlock(&q->mq_freeze_lock);
146 }
147 }
148 EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
149
blk_mq_freeze_queue_wait(struct request_queue * q)150 void blk_mq_freeze_queue_wait(struct request_queue *q)
151 {
152 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
153 }
154 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
155
blk_mq_freeze_queue_wait_timeout(struct request_queue * q,unsigned long timeout)156 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
157 unsigned long timeout)
158 {
159 return wait_event_timeout(q->mq_freeze_wq,
160 percpu_ref_is_zero(&q->q_usage_counter),
161 timeout);
162 }
163 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
164
165 /*
166 * Guarantee no request is in use, so we can change any data structure of
167 * the queue afterward.
168 */
blk_freeze_queue(struct request_queue * q)169 void blk_freeze_queue(struct request_queue *q)
170 {
171 /*
172 * In the !blk_mq case we are only calling this to kill the
173 * q_usage_counter, otherwise this increases the freeze depth
174 * and waits for it to return to zero. For this reason there is
175 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
176 * exported to drivers as the only user for unfreeze is blk_mq.
177 */
178 blk_freeze_queue_start(q);
179 blk_mq_freeze_queue_wait(q);
180 }
181
blk_mq_freeze_queue(struct request_queue * q)182 void blk_mq_freeze_queue(struct request_queue *q)
183 {
184 /*
185 * ...just an alias to keep freeze and unfreeze actions balanced
186 * in the blk_mq_* namespace
187 */
188 blk_freeze_queue(q);
189 }
190 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
191
blk_mq_unfreeze_queue(struct request_queue * q)192 void blk_mq_unfreeze_queue(struct request_queue *q)
193 {
194 mutex_lock(&q->mq_freeze_lock);
195 q->mq_freeze_depth--;
196 WARN_ON_ONCE(q->mq_freeze_depth < 0);
197 if (!q->mq_freeze_depth) {
198 percpu_ref_resurrect(&q->q_usage_counter);
199 wake_up_all(&q->mq_freeze_wq);
200 }
201 mutex_unlock(&q->mq_freeze_lock);
202 }
203 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
204
205 /*
206 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
207 * mpt3sas driver such that this function can be removed.
208 */
blk_mq_quiesce_queue_nowait(struct request_queue * q)209 void blk_mq_quiesce_queue_nowait(struct request_queue *q)
210 {
211 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
212 }
213 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
214
215 /**
216 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
217 * @q: request queue.
218 *
219 * Note: this function does not prevent that the struct request end_io()
220 * callback function is invoked. Once this function is returned, we make
221 * sure no dispatch can happen until the queue is unquiesced via
222 * blk_mq_unquiesce_queue().
223 */
blk_mq_quiesce_queue(struct request_queue * q)224 void blk_mq_quiesce_queue(struct request_queue *q)
225 {
226 struct blk_mq_hw_ctx *hctx;
227 unsigned int i;
228 bool rcu = false;
229
230 blk_mq_quiesce_queue_nowait(q);
231
232 queue_for_each_hw_ctx(q, hctx, i) {
233 if (hctx->flags & BLK_MQ_F_BLOCKING)
234 synchronize_srcu(hctx->srcu);
235 else
236 rcu = true;
237 }
238 if (rcu)
239 synchronize_rcu();
240 }
241 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
242
243 /*
244 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
245 * @q: request queue.
246 *
247 * This function recovers queue into the state before quiescing
248 * which is done by blk_mq_quiesce_queue.
249 */
blk_mq_unquiesce_queue(struct request_queue * q)250 void blk_mq_unquiesce_queue(struct request_queue *q)
251 {
252 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
253
254 /* dispatch requests which are inserted during quiescing */
255 blk_mq_run_hw_queues(q, true);
256 }
257 EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
258
blk_mq_wake_waiters(struct request_queue * q)259 void blk_mq_wake_waiters(struct request_queue *q)
260 {
261 struct blk_mq_hw_ctx *hctx;
262 unsigned int i;
263
264 queue_for_each_hw_ctx(q, hctx, i)
265 if (blk_mq_hw_queue_mapped(hctx))
266 blk_mq_tag_wakeup_all(hctx->tags, true);
267 }
268
269 /*
270 * Only need start/end time stamping if we have iostat or
271 * blk stats enabled, or using an IO scheduler.
272 */
blk_mq_need_time_stamp(struct request * rq)273 static inline bool blk_mq_need_time_stamp(struct request *rq)
274 {
275 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
276 }
277
blk_mq_rq_ctx_init(struct blk_mq_alloc_data * data,unsigned int tag,u64 alloc_time_ns)278 static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
279 unsigned int tag, u64 alloc_time_ns)
280 {
281 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
282 struct request *rq = tags->static_rqs[tag];
283
284 if (data->q->elevator) {
285 rq->tag = BLK_MQ_NO_TAG;
286 rq->internal_tag = tag;
287 } else {
288 rq->tag = tag;
289 rq->internal_tag = BLK_MQ_NO_TAG;
290 }
291
292 /* csd/requeue_work/fifo_time is initialized before use */
293 rq->q = data->q;
294 rq->mq_ctx = data->ctx;
295 rq->mq_hctx = data->hctx;
296 rq->rq_flags = 0;
297 rq->cmd_flags = data->cmd_flags;
298 if (data->flags & BLK_MQ_REQ_PM)
299 rq->rq_flags |= RQF_PM;
300 if (blk_queue_io_stat(data->q))
301 rq->rq_flags |= RQF_IO_STAT;
302 INIT_LIST_HEAD(&rq->queuelist);
303 INIT_HLIST_NODE(&rq->hash);
304 RB_CLEAR_NODE(&rq->rb_node);
305 rq->rq_disk = NULL;
306 rq->part = NULL;
307 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
308 rq->alloc_time_ns = alloc_time_ns;
309 #endif
310 if (blk_mq_need_time_stamp(rq))
311 rq->start_time_ns = ktime_get_ns();
312 else
313 rq->start_time_ns = 0;
314 rq->io_start_time_ns = 0;
315 rq->stats_sectors = 0;
316 rq->nr_phys_segments = 0;
317 #if defined(CONFIG_BLK_DEV_INTEGRITY)
318 rq->nr_integrity_segments = 0;
319 #endif
320 blk_crypto_rq_set_defaults(rq);
321 /* tag was already set */
322 WRITE_ONCE(rq->deadline, 0);
323
324 rq->timeout = 0;
325
326 rq->end_io = NULL;
327 rq->end_io_data = NULL;
328
329 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
330 refcount_set(&rq->ref, 1);
331
332 if (!op_is_flush(data->cmd_flags)) {
333 struct elevator_queue *e = data->q->elevator;
334
335 rq->elv.icq = NULL;
336 if (e && e->type->ops.prepare_request) {
337 if (e->type->icq_cache)
338 blk_mq_sched_assign_ioc(rq);
339
340 e->type->ops.prepare_request(rq);
341 rq->rq_flags |= RQF_ELVPRIV;
342 }
343 }
344
345 data->hctx->queued++;
346 trace_android_vh_blk_rq_ctx_init(rq, tags, data, alloc_time_ns);
347 return rq;
348 }
349
__blk_mq_alloc_request(struct blk_mq_alloc_data * data)350 static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data)
351 {
352 struct request_queue *q = data->q;
353 struct elevator_queue *e = q->elevator;
354 u64 alloc_time_ns = 0;
355 unsigned int tag;
356 bool skip = false;
357
358 /* alloc_time includes depth and tag waits */
359 if (blk_queue_rq_alloc_time(q))
360 alloc_time_ns = ktime_get_ns();
361
362 if (data->cmd_flags & REQ_NOWAIT)
363 data->flags |= BLK_MQ_REQ_NOWAIT;
364
365 if (e) {
366 /*
367 * Flush requests are special and go directly to the
368 * dispatch list. Don't include reserved tags in the
369 * limiting, as it isn't useful.
370 */
371 if (!op_is_flush(data->cmd_flags) &&
372 e->type->ops.limit_depth &&
373 !(data->flags & BLK_MQ_REQ_RESERVED))
374 e->type->ops.limit_depth(data->cmd_flags, data);
375 }
376
377 retry:
378 data->ctx = blk_mq_get_ctx(q);
379 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
380 if (!e)
381 blk_mq_tag_busy(data->hctx);
382
383 /*
384 * Waiting allocations only fail because of an inactive hctx. In that
385 * case just retry the hctx assignment and tag allocation as CPU hotplug
386 * should have migrated us to an online CPU by now.
387 */
388 trace_android_rvh_internal_blk_mq_alloc_request(&skip, &tag, data);
389 if (!skip)
390 tag = blk_mq_get_tag(data);
391 if (tag == BLK_MQ_NO_TAG) {
392 if (data->flags & BLK_MQ_REQ_NOWAIT)
393 return NULL;
394
395 /*
396 * Give up the CPU and sleep for a random short time to ensure
397 * that thread using a realtime scheduling class are migrated
398 * off the CPU, and thus off the hctx that is going away.
399 */
400 msleep(3);
401 goto retry;
402 }
403 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
404 }
405
blk_mq_alloc_request(struct request_queue * q,unsigned int op,blk_mq_req_flags_t flags)406 struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
407 blk_mq_req_flags_t flags)
408 {
409 struct blk_mq_alloc_data data = {
410 .q = q,
411 .flags = flags,
412 .cmd_flags = op,
413 };
414 struct request *rq;
415 int ret;
416
417 ret = blk_queue_enter(q, flags);
418 if (ret)
419 return ERR_PTR(ret);
420
421 rq = __blk_mq_alloc_request(&data);
422 if (!rq)
423 goto out_queue_exit;
424 rq->__data_len = 0;
425 rq->__sector = (sector_t) -1;
426 rq->bio = rq->biotail = NULL;
427 return rq;
428 out_queue_exit:
429 blk_queue_exit(q);
430 return ERR_PTR(-EWOULDBLOCK);
431 }
432 EXPORT_SYMBOL(blk_mq_alloc_request);
433
blk_mq_alloc_request_hctx(struct request_queue * q,unsigned int op,blk_mq_req_flags_t flags,unsigned int hctx_idx)434 struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
435 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
436 {
437 struct blk_mq_alloc_data data = {
438 .q = q,
439 .flags = flags,
440 .cmd_flags = op,
441 };
442 u64 alloc_time_ns = 0;
443 unsigned int cpu;
444 unsigned int tag;
445 int ret;
446
447 /* alloc_time includes depth and tag waits */
448 if (blk_queue_rq_alloc_time(q))
449 alloc_time_ns = ktime_get_ns();
450
451 /*
452 * If the tag allocator sleeps we could get an allocation for a
453 * different hardware context. No need to complicate the low level
454 * allocator for this for the rare use case of a command tied to
455 * a specific queue.
456 */
457 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)) ||
458 WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED)))
459 return ERR_PTR(-EINVAL);
460
461 if (hctx_idx >= q->nr_hw_queues)
462 return ERR_PTR(-EIO);
463
464 ret = blk_queue_enter(q, flags);
465 if (ret)
466 return ERR_PTR(ret);
467
468 /*
469 * Check if the hardware context is actually mapped to anything.
470 * If not tell the caller that it should skip this queue.
471 */
472 ret = -EXDEV;
473 data.hctx = q->queue_hw_ctx[hctx_idx];
474 if (!blk_mq_hw_queue_mapped(data.hctx))
475 goto out_queue_exit;
476 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
477 if (cpu >= nr_cpu_ids)
478 goto out_queue_exit;
479 data.ctx = __blk_mq_get_ctx(q, cpu);
480
481 if (!q->elevator)
482 blk_mq_tag_busy(data.hctx);
483
484 ret = -EWOULDBLOCK;
485 tag = blk_mq_get_tag(&data);
486 if (tag == BLK_MQ_NO_TAG)
487 goto out_queue_exit;
488 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
489
490 out_queue_exit:
491 blk_queue_exit(q);
492 return ERR_PTR(ret);
493 }
494 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
495
__blk_mq_free_request(struct request * rq)496 static void __blk_mq_free_request(struct request *rq)
497 {
498 struct request_queue *q = rq->q;
499 struct blk_mq_ctx *ctx = rq->mq_ctx;
500 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
501 const int sched_tag = rq->internal_tag;
502 bool skip = false;
503
504 blk_crypto_free_request(rq);
505 blk_pm_mark_last_busy(rq);
506 rq->mq_hctx = NULL;
507
508 trace_android_vh_internal_blk_mq_free_request(&skip, rq, hctx);
509 if (!skip) {
510 if (rq->tag != BLK_MQ_NO_TAG)
511 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
512 }
513 if (sched_tag != BLK_MQ_NO_TAG)
514 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
515 blk_mq_sched_restart(hctx);
516 blk_queue_exit(q);
517 }
518
blk_mq_free_request(struct request * rq)519 void blk_mq_free_request(struct request *rq)
520 {
521 struct request_queue *q = rq->q;
522 struct elevator_queue *e = q->elevator;
523 struct blk_mq_ctx *ctx = rq->mq_ctx;
524 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
525
526 if (rq->rq_flags & RQF_ELVPRIV) {
527 if (e && e->type->ops.finish_request)
528 e->type->ops.finish_request(rq);
529 if (rq->elv.icq) {
530 put_io_context(rq->elv.icq->ioc);
531 rq->elv.icq = NULL;
532 }
533 }
534
535 ctx->rq_completed[rq_is_sync(rq)]++;
536 if (rq->rq_flags & RQF_MQ_INFLIGHT)
537 __blk_mq_dec_active_requests(hctx);
538
539 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
540 laptop_io_completion(q->backing_dev_info);
541
542 rq_qos_done(q, rq);
543
544 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
545 if (refcount_dec_and_test(&rq->ref))
546 __blk_mq_free_request(rq);
547 }
548 EXPORT_SYMBOL_GPL(blk_mq_free_request);
549
__blk_mq_end_request(struct request * rq,blk_status_t error)550 inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
551 {
552 u64 now = 0;
553
554 if (blk_mq_need_time_stamp(rq))
555 now = ktime_get_ns();
556
557 if (rq->rq_flags & RQF_STATS) {
558 blk_mq_poll_stats_start(rq->q);
559 blk_stat_add(rq, now);
560 }
561
562 blk_mq_sched_completed_request(rq, now);
563
564 blk_account_io_done(rq, now);
565
566 if (rq->end_io) {
567 rq_qos_done(rq->q, rq);
568 rq->end_io(rq, error);
569 } else {
570 blk_mq_free_request(rq);
571 }
572 }
573 EXPORT_SYMBOL(__blk_mq_end_request);
574
blk_mq_end_request(struct request * rq,blk_status_t error)575 void blk_mq_end_request(struct request *rq, blk_status_t error)
576 {
577 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
578 BUG();
579 __blk_mq_end_request(rq, error);
580 }
581 EXPORT_SYMBOL(blk_mq_end_request);
582
583 /*
584 * Softirq action handler - move entries to local list and loop over them
585 * while passing them to the queue registered handler.
586 */
blk_done_softirq(struct softirq_action * h)587 static __latent_entropy void blk_done_softirq(struct softirq_action *h)
588 {
589 struct list_head *cpu_list, local_list;
590
591 local_irq_disable();
592 cpu_list = this_cpu_ptr(&blk_cpu_done);
593 list_replace_init(cpu_list, &local_list);
594 local_irq_enable();
595
596 while (!list_empty(&local_list)) {
597 struct request *rq;
598
599 rq = list_entry(local_list.next, struct request, ipi_list);
600 list_del_init(&rq->ipi_list);
601 rq->q->mq_ops->complete(rq);
602 }
603 }
604
blk_mq_trigger_softirq(struct request * rq)605 static void blk_mq_trigger_softirq(struct request *rq)
606 {
607 struct list_head *list;
608 unsigned long flags;
609
610 local_irq_save(flags);
611 list = this_cpu_ptr(&blk_cpu_done);
612 list_add_tail(&rq->ipi_list, list);
613
614 /*
615 * If the list only contains our just added request, signal a raise of
616 * the softirq. If there are already entries there, someone already
617 * raised the irq but it hasn't run yet.
618 */
619 if (list->next == &rq->ipi_list)
620 raise_softirq_irqoff(BLOCK_SOFTIRQ);
621 local_irq_restore(flags);
622 }
623
blk_softirq_cpu_dead(unsigned int cpu)624 static int blk_softirq_cpu_dead(unsigned int cpu)
625 {
626 /*
627 * If a CPU goes away, splice its entries to the current CPU
628 * and trigger a run of the softirq
629 */
630 local_irq_disable();
631 list_splice_init(&per_cpu(blk_cpu_done, cpu),
632 this_cpu_ptr(&blk_cpu_done));
633 raise_softirq_irqoff(BLOCK_SOFTIRQ);
634 local_irq_enable();
635
636 return 0;
637 }
638
639
__blk_mq_complete_request_remote(void * data)640 static void __blk_mq_complete_request_remote(void *data)
641 {
642 struct request *rq = data;
643
644 /*
645 * For most of single queue controllers, there is only one irq vector
646 * for handling I/O completion, and the only irq's affinity is set
647 * to all possible CPUs. On most of ARCHs, this affinity means the irq
648 * is handled on one specific CPU.
649 *
650 * So complete I/O requests in softirq context in case of single queue
651 * devices to avoid degrading I/O performance due to irqsoff latency.
652 */
653 if (rq->q->nr_hw_queues == 1)
654 blk_mq_trigger_softirq(rq);
655 else
656 rq->q->mq_ops->complete(rq);
657 }
658
blk_mq_complete_need_ipi(struct request * rq)659 static inline bool blk_mq_complete_need_ipi(struct request *rq)
660 {
661 int cpu = raw_smp_processor_id();
662
663 if (!IS_ENABLED(CONFIG_SMP) ||
664 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
665 return false;
666
667 /* same CPU or cache domain? Complete locally */
668 if (cpu == rq->mq_ctx->cpu ||
669 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
670 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
671 return false;
672
673 /* don't try to IPI to an offline CPU */
674 return cpu_online(rq->mq_ctx->cpu);
675 }
676
blk_mq_complete_request_remote(struct request * rq)677 bool blk_mq_complete_request_remote(struct request *rq)
678 {
679 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
680
681 /*
682 * For a polled request, always complete locallly, it's pointless
683 * to redirect the completion.
684 */
685 if (rq->cmd_flags & REQ_HIPRI)
686 return false;
687
688 if (blk_mq_complete_need_ipi(rq)) {
689 rq->csd.func = __blk_mq_complete_request_remote;
690 rq->csd.info = rq;
691 rq->csd.flags = 0;
692 smp_call_function_single_async(rq->mq_ctx->cpu, &rq->csd);
693 } else {
694 if (rq->q->nr_hw_queues > 1)
695 return false;
696 blk_mq_trigger_softirq(rq);
697 }
698
699 return true;
700 }
701 EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
702
703 /**
704 * blk_mq_complete_request - end I/O on a request
705 * @rq: the request being processed
706 *
707 * Description:
708 * Complete a request by scheduling the ->complete_rq operation.
709 **/
blk_mq_complete_request(struct request * rq)710 void blk_mq_complete_request(struct request *rq)
711 {
712 bool skip = false;
713
714 trace_android_vh_blk_mq_complete_request(&skip, rq);
715 if (skip)
716 return;
717 if (!blk_mq_complete_request_remote(rq))
718 rq->q->mq_ops->complete(rq);
719 }
720 EXPORT_SYMBOL(blk_mq_complete_request);
721
hctx_unlock(struct blk_mq_hw_ctx * hctx,int srcu_idx)722 static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
723 __releases(hctx->srcu)
724 {
725 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
726 rcu_read_unlock();
727 else
728 srcu_read_unlock(hctx->srcu, srcu_idx);
729 }
730
hctx_lock(struct blk_mq_hw_ctx * hctx,int * srcu_idx)731 static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
732 __acquires(hctx->srcu)
733 {
734 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
735 /* shut up gcc false positive */
736 *srcu_idx = 0;
737 rcu_read_lock();
738 } else
739 *srcu_idx = srcu_read_lock(hctx->srcu);
740 }
741
742 /**
743 * blk_mq_start_request - Start processing a request
744 * @rq: Pointer to request to be started
745 *
746 * Function used by device drivers to notify the block layer that a request
747 * is going to be processed now, so blk layer can do proper initializations
748 * such as starting the timeout timer.
749 */
blk_mq_start_request(struct request * rq)750 void blk_mq_start_request(struct request *rq)
751 {
752 struct request_queue *q = rq->q;
753
754 trace_block_rq_issue(q, rq);
755
756 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
757 rq->io_start_time_ns = ktime_get_ns();
758 rq->stats_sectors = blk_rq_sectors(rq);
759 rq->rq_flags |= RQF_STATS;
760 rq_qos_issue(q, rq);
761 }
762
763 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
764
765 blk_add_timer(rq);
766 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
767
768 #ifdef CONFIG_BLK_DEV_INTEGRITY
769 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
770 q->integrity.profile->prepare_fn(rq);
771 #endif
772 }
773 EXPORT_SYMBOL(blk_mq_start_request);
774
__blk_mq_requeue_request(struct request * rq)775 static void __blk_mq_requeue_request(struct request *rq)
776 {
777 struct request_queue *q = rq->q;
778
779 blk_mq_put_driver_tag(rq);
780
781 trace_block_rq_requeue(q, rq);
782 rq_qos_requeue(q, rq);
783
784 if (blk_mq_request_started(rq)) {
785 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
786 rq->rq_flags &= ~RQF_TIMED_OUT;
787 }
788 }
789
blk_mq_requeue_request(struct request * rq,bool kick_requeue_list)790 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
791 {
792 __blk_mq_requeue_request(rq);
793
794 /* this request will be re-inserted to io scheduler queue */
795 blk_mq_sched_requeue_request(rq);
796
797 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
798 }
799 EXPORT_SYMBOL(blk_mq_requeue_request);
800
blk_mq_requeue_work(struct work_struct * work)801 static void blk_mq_requeue_work(struct work_struct *work)
802 {
803 struct request_queue *q =
804 container_of(work, struct request_queue, requeue_work.work);
805 LIST_HEAD(rq_list);
806 struct request *rq, *next;
807
808 spin_lock_irq(&q->requeue_lock);
809 list_splice_init(&q->requeue_list, &rq_list);
810 spin_unlock_irq(&q->requeue_lock);
811
812 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
813 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
814 continue;
815
816 rq->rq_flags &= ~RQF_SOFTBARRIER;
817 list_del_init(&rq->queuelist);
818 /*
819 * If RQF_DONTPREP, rq has contained some driver specific
820 * data, so insert it to hctx dispatch list to avoid any
821 * merge.
822 */
823 if (rq->rq_flags & RQF_DONTPREP)
824 blk_mq_request_bypass_insert(rq, false, false);
825 else
826 blk_mq_sched_insert_request(rq, true, false, false);
827 }
828
829 while (!list_empty(&rq_list)) {
830 rq = list_entry(rq_list.next, struct request, queuelist);
831 list_del_init(&rq->queuelist);
832 blk_mq_sched_insert_request(rq, false, false, false);
833 }
834
835 blk_mq_run_hw_queues(q, false);
836 }
837
blk_mq_add_to_requeue_list(struct request * rq,bool at_head,bool kick_requeue_list)838 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
839 bool kick_requeue_list)
840 {
841 struct request_queue *q = rq->q;
842 unsigned long flags;
843 bool skip = false;
844
845 trace_android_vh_blk_mq_add_to_requeue_list(&skip, rq,
846 kick_requeue_list);
847 if (skip)
848 return;
849 /*
850 * We abuse this flag that is otherwise used by the I/O scheduler to
851 * request head insertion from the workqueue.
852 */
853 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
854
855 spin_lock_irqsave(&q->requeue_lock, flags);
856 if (at_head) {
857 rq->rq_flags |= RQF_SOFTBARRIER;
858 list_add(&rq->queuelist, &q->requeue_list);
859 } else {
860 list_add_tail(&rq->queuelist, &q->requeue_list);
861 }
862 spin_unlock_irqrestore(&q->requeue_lock, flags);
863
864 if (kick_requeue_list)
865 blk_mq_kick_requeue_list(q);
866 }
867
blk_mq_kick_requeue_list(struct request_queue * q)868 void blk_mq_kick_requeue_list(struct request_queue *q)
869 {
870 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
871 }
872 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
873
blk_mq_delay_kick_requeue_list(struct request_queue * q,unsigned long msecs)874 void blk_mq_delay_kick_requeue_list(struct request_queue *q,
875 unsigned long msecs)
876 {
877 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
878 msecs_to_jiffies(msecs));
879 }
880 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
881
blk_mq_tag_to_rq(struct blk_mq_tags * tags,unsigned int tag)882 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
883 {
884 if (tag < tags->nr_tags) {
885 prefetch(tags->rqs[tag]);
886 return tags->rqs[tag];
887 }
888
889 return NULL;
890 }
891 EXPORT_SYMBOL(blk_mq_tag_to_rq);
892
blk_mq_rq_inflight(struct blk_mq_hw_ctx * hctx,struct request * rq,void * priv,bool reserved)893 static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
894 void *priv, bool reserved)
895 {
896 /*
897 * If we find a request that isn't idle and the queue matches,
898 * we know the queue is busy. Return false to stop the iteration.
899 */
900 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
901 bool *busy = priv;
902
903 *busy = true;
904 return false;
905 }
906
907 return true;
908 }
909
blk_mq_queue_inflight(struct request_queue * q)910 bool blk_mq_queue_inflight(struct request_queue *q)
911 {
912 bool busy = false;
913
914 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
915 return busy;
916 }
917 EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
918
blk_mq_rq_timed_out(struct request * req,bool reserved)919 static void blk_mq_rq_timed_out(struct request *req, bool reserved)
920 {
921 req->rq_flags |= RQF_TIMED_OUT;
922 if (req->q->mq_ops->timeout) {
923 enum blk_eh_timer_return ret;
924
925 ret = req->q->mq_ops->timeout(req, reserved);
926 if (ret == BLK_EH_DONE)
927 return;
928 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
929 }
930
931 blk_add_timer(req);
932 }
933
blk_mq_req_expired(struct request * rq,unsigned long * next)934 static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
935 {
936 unsigned long deadline;
937
938 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
939 return false;
940 if (rq->rq_flags & RQF_TIMED_OUT)
941 return false;
942
943 deadline = READ_ONCE(rq->deadline);
944 if (time_after_eq(jiffies, deadline))
945 return true;
946
947 if (*next == 0)
948 *next = deadline;
949 else if (time_after(*next, deadline))
950 *next = deadline;
951 return false;
952 }
953
blk_mq_put_rq_ref(struct request * rq)954 void blk_mq_put_rq_ref(struct request *rq)
955 {
956 if (is_flush_rq(rq))
957 rq->end_io(rq, 0);
958 else if (refcount_dec_and_test(&rq->ref))
959 __blk_mq_free_request(rq);
960 }
961
blk_mq_check_expired(struct blk_mq_hw_ctx * hctx,struct request * rq,void * priv,bool reserved)962 static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
963 struct request *rq, void *priv, bool reserved)
964 {
965 unsigned long *next = priv;
966
967 /*
968 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
969 * be reallocated underneath the timeout handler's processing, then
970 * the expire check is reliable. If the request is not expired, then
971 * it was completed and reallocated as a new request after returning
972 * from blk_mq_check_expired().
973 */
974 if (blk_mq_req_expired(rq, next))
975 blk_mq_rq_timed_out(rq, reserved);
976 return true;
977 }
978
blk_mq_timeout_work(struct work_struct * work)979 static void blk_mq_timeout_work(struct work_struct *work)
980 {
981 struct request_queue *q =
982 container_of(work, struct request_queue, timeout_work);
983 unsigned long next = 0;
984 struct blk_mq_hw_ctx *hctx;
985 int i;
986
987 /* A deadlock might occur if a request is stuck requiring a
988 * timeout at the same time a queue freeze is waiting
989 * completion, since the timeout code would not be able to
990 * acquire the queue reference here.
991 *
992 * That's why we don't use blk_queue_enter here; instead, we use
993 * percpu_ref_tryget directly, because we need to be able to
994 * obtain a reference even in the short window between the queue
995 * starting to freeze, by dropping the first reference in
996 * blk_freeze_queue_start, and the moment the last request is
997 * consumed, marked by the instant q_usage_counter reaches
998 * zero.
999 */
1000 if (!percpu_ref_tryget(&q->q_usage_counter))
1001 return;
1002
1003 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
1004
1005 if (next != 0) {
1006 mod_timer(&q->timeout, next);
1007 } else {
1008 /*
1009 * Request timeouts are handled as a forward rolling timer. If
1010 * we end up here it means that no requests are pending and
1011 * also that no request has been pending for a while. Mark
1012 * each hctx as idle.
1013 */
1014 queue_for_each_hw_ctx(q, hctx, i) {
1015 /* the hctx may be unmapped, so check it here */
1016 if (blk_mq_hw_queue_mapped(hctx))
1017 blk_mq_tag_idle(hctx);
1018 }
1019 }
1020 blk_queue_exit(q);
1021 }
1022
1023 struct flush_busy_ctx_data {
1024 struct blk_mq_hw_ctx *hctx;
1025 struct list_head *list;
1026 };
1027
flush_busy_ctx(struct sbitmap * sb,unsigned int bitnr,void * data)1028 static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1029 {
1030 struct flush_busy_ctx_data *flush_data = data;
1031 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1032 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
1033 enum hctx_type type = hctx->type;
1034
1035 spin_lock(&ctx->lock);
1036 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
1037 sbitmap_clear_bit(sb, bitnr);
1038 spin_unlock(&ctx->lock);
1039 return true;
1040 }
1041
1042 /*
1043 * Process software queues that have been marked busy, splicing them
1044 * to the for-dispatch
1045 */
blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx * hctx,struct list_head * list)1046 void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
1047 {
1048 struct flush_busy_ctx_data data = {
1049 .hctx = hctx,
1050 .list = list,
1051 };
1052
1053 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
1054 }
1055 EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
1056
1057 struct dispatch_rq_data {
1058 struct blk_mq_hw_ctx *hctx;
1059 struct request *rq;
1060 };
1061
dispatch_rq_from_ctx(struct sbitmap * sb,unsigned int bitnr,void * data)1062 static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1063 void *data)
1064 {
1065 struct dispatch_rq_data *dispatch_data = data;
1066 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1067 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
1068 enum hctx_type type = hctx->type;
1069
1070 spin_lock(&ctx->lock);
1071 if (!list_empty(&ctx->rq_lists[type])) {
1072 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
1073 list_del_init(&dispatch_data->rq->queuelist);
1074 if (list_empty(&ctx->rq_lists[type]))
1075 sbitmap_clear_bit(sb, bitnr);
1076 }
1077 spin_unlock(&ctx->lock);
1078
1079 return !dispatch_data->rq;
1080 }
1081
blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx * hctx,struct blk_mq_ctx * start)1082 struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1083 struct blk_mq_ctx *start)
1084 {
1085 unsigned off = start ? start->index_hw[hctx->type] : 0;
1086 struct dispatch_rq_data data = {
1087 .hctx = hctx,
1088 .rq = NULL,
1089 };
1090
1091 __sbitmap_for_each_set(&hctx->ctx_map, off,
1092 dispatch_rq_from_ctx, &data);
1093
1094 return data.rq;
1095 }
1096
queued_to_index(unsigned int queued)1097 static inline unsigned int queued_to_index(unsigned int queued)
1098 {
1099 if (!queued)
1100 return 0;
1101
1102 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
1103 }
1104
__blk_mq_get_driver_tag(struct request * rq)1105 static bool __blk_mq_get_driver_tag(struct request *rq)
1106 {
1107 struct sbitmap_queue *bt = rq->mq_hctx->tags->bitmap_tags;
1108 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
1109 int tag;
1110
1111 blk_mq_tag_busy(rq->mq_hctx);
1112
1113 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
1114 bt = rq->mq_hctx->tags->breserved_tags;
1115 tag_offset = 0;
1116 } else {
1117 if (!hctx_may_queue(rq->mq_hctx, bt))
1118 return false;
1119 }
1120
1121 tag = __sbitmap_queue_get(bt);
1122 if (tag == BLK_MQ_NO_TAG)
1123 return false;
1124
1125 rq->tag = tag + tag_offset;
1126 return true;
1127 }
1128
blk_mq_get_driver_tag(struct request * rq)1129 static bool blk_mq_get_driver_tag(struct request *rq)
1130 {
1131 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1132
1133 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq))
1134 return false;
1135
1136 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
1137 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1138 rq->rq_flags |= RQF_MQ_INFLIGHT;
1139 __blk_mq_inc_active_requests(hctx);
1140 }
1141 hctx->tags->rqs[rq->tag] = rq;
1142 return true;
1143 }
1144
blk_mq_dispatch_wake(wait_queue_entry_t * wait,unsigned mode,int flags,void * key)1145 static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1146 int flags, void *key)
1147 {
1148 struct blk_mq_hw_ctx *hctx;
1149
1150 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1151
1152 spin_lock(&hctx->dispatch_wait_lock);
1153 if (!list_empty(&wait->entry)) {
1154 struct sbitmap_queue *sbq;
1155
1156 list_del_init(&wait->entry);
1157 sbq = hctx->tags->bitmap_tags;
1158 atomic_dec(&sbq->ws_active);
1159 }
1160 spin_unlock(&hctx->dispatch_wait_lock);
1161
1162 blk_mq_run_hw_queue(hctx, true);
1163 return 1;
1164 }
1165
1166 /*
1167 * Mark us waiting for a tag. For shared tags, this involves hooking us into
1168 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1169 * restart. For both cases, take care to check the condition again after
1170 * marking us as waiting.
1171 */
blk_mq_mark_tag_wait(struct blk_mq_hw_ctx * hctx,struct request * rq)1172 static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
1173 struct request *rq)
1174 {
1175 struct sbitmap_queue *sbq = hctx->tags->bitmap_tags;
1176 struct wait_queue_head *wq;
1177 wait_queue_entry_t *wait;
1178 bool ret;
1179
1180 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
1181 blk_mq_sched_mark_restart_hctx(hctx);
1182
1183 /*
1184 * It's possible that a tag was freed in the window between the
1185 * allocation failure and adding the hardware queue to the wait
1186 * queue.
1187 *
1188 * Don't clear RESTART here, someone else could have set it.
1189 * At most this will cost an extra queue run.
1190 */
1191 return blk_mq_get_driver_tag(rq);
1192 }
1193
1194 wait = &hctx->dispatch_wait;
1195 if (!list_empty_careful(&wait->entry))
1196 return false;
1197
1198 wq = &bt_wait_ptr(sbq, hctx)->wait;
1199
1200 spin_lock_irq(&wq->lock);
1201 spin_lock(&hctx->dispatch_wait_lock);
1202 if (!list_empty(&wait->entry)) {
1203 spin_unlock(&hctx->dispatch_wait_lock);
1204 spin_unlock_irq(&wq->lock);
1205 return false;
1206 }
1207
1208 atomic_inc(&sbq->ws_active);
1209 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1210 __add_wait_queue(wq, wait);
1211
1212 /*
1213 * Add one explicit barrier since blk_mq_get_driver_tag() may
1214 * not imply barrier in case of failure.
1215 *
1216 * Order adding us to wait queue and allocating driver tag.
1217 *
1218 * The pair is the one implied in sbitmap_queue_wake_up() which
1219 * orders clearing sbitmap tag bits and waitqueue_active() in
1220 * __sbitmap_queue_wake_up(), since waitqueue_active() is lockless
1221 *
1222 * Otherwise, re-order of adding wait queue and getting driver tag
1223 * may cause __sbitmap_queue_wake_up() to wake up nothing because
1224 * the waitqueue_active() may not observe us in wait queue.
1225 */
1226 smp_mb();
1227
1228 /*
1229 * It's possible that a tag was freed in the window between the
1230 * allocation failure and adding the hardware queue to the wait
1231 * queue.
1232 */
1233 ret = blk_mq_get_driver_tag(rq);
1234 if (!ret) {
1235 spin_unlock(&hctx->dispatch_wait_lock);
1236 spin_unlock_irq(&wq->lock);
1237 return false;
1238 }
1239
1240 /*
1241 * We got a tag, remove ourselves from the wait queue to ensure
1242 * someone else gets the wakeup.
1243 */
1244 list_del_init(&wait->entry);
1245 atomic_dec(&sbq->ws_active);
1246 spin_unlock(&hctx->dispatch_wait_lock);
1247 spin_unlock_irq(&wq->lock);
1248
1249 return true;
1250 }
1251
1252 #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1253 #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1254 /*
1255 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1256 * - EWMA is one simple way to compute running average value
1257 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1258 * - take 4 as factor for avoiding to get too small(0) result, and this
1259 * factor doesn't matter because EWMA decreases exponentially
1260 */
blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx * hctx,bool busy)1261 static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1262 {
1263 unsigned int ewma;
1264
1265 ewma = hctx->dispatch_busy;
1266
1267 if (!ewma && !busy)
1268 return;
1269
1270 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1271 if (busy)
1272 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1273 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1274
1275 hctx->dispatch_busy = ewma;
1276 }
1277
1278 #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1279
blk_mq_handle_dev_resource(struct request * rq,struct list_head * list)1280 static void blk_mq_handle_dev_resource(struct request *rq,
1281 struct list_head *list)
1282 {
1283 struct request *next =
1284 list_first_entry_or_null(list, struct request, queuelist);
1285
1286 /*
1287 * If an I/O scheduler has been configured and we got a driver tag for
1288 * the next request already, free it.
1289 */
1290 if (next)
1291 blk_mq_put_driver_tag(next);
1292
1293 list_add(&rq->queuelist, list);
1294 __blk_mq_requeue_request(rq);
1295 }
1296
blk_mq_handle_zone_resource(struct request * rq,struct list_head * zone_list)1297 static void blk_mq_handle_zone_resource(struct request *rq,
1298 struct list_head *zone_list)
1299 {
1300 /*
1301 * If we end up here it is because we cannot dispatch a request to a
1302 * specific zone due to LLD level zone-write locking or other zone
1303 * related resource not being available. In this case, set the request
1304 * aside in zone_list for retrying it later.
1305 */
1306 list_add(&rq->queuelist, zone_list);
1307 __blk_mq_requeue_request(rq);
1308 }
1309
1310 enum prep_dispatch {
1311 PREP_DISPATCH_OK,
1312 PREP_DISPATCH_NO_TAG,
1313 PREP_DISPATCH_NO_BUDGET,
1314 };
1315
blk_mq_prep_dispatch_rq(struct request * rq,bool need_budget)1316 static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1317 bool need_budget)
1318 {
1319 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1320
1321 if (need_budget && !blk_mq_get_dispatch_budget(rq->q)) {
1322 blk_mq_put_driver_tag(rq);
1323 return PREP_DISPATCH_NO_BUDGET;
1324 }
1325
1326 if (!blk_mq_get_driver_tag(rq)) {
1327 /*
1328 * The initial allocation attempt failed, so we need to
1329 * rerun the hardware queue when a tag is freed. The
1330 * waitqueue takes care of that. If the queue is run
1331 * before we add this entry back on the dispatch list,
1332 * we'll re-run it below.
1333 */
1334 if (!blk_mq_mark_tag_wait(hctx, rq)) {
1335 /*
1336 * All budgets not got from this function will be put
1337 * together during handling partial dispatch
1338 */
1339 if (need_budget)
1340 blk_mq_put_dispatch_budget(rq->q);
1341 return PREP_DISPATCH_NO_TAG;
1342 }
1343 }
1344
1345 return PREP_DISPATCH_OK;
1346 }
1347
1348 /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
blk_mq_release_budgets(struct request_queue * q,unsigned int nr_budgets)1349 static void blk_mq_release_budgets(struct request_queue *q,
1350 unsigned int nr_budgets)
1351 {
1352 int i;
1353
1354 for (i = 0; i < nr_budgets; i++)
1355 blk_mq_put_dispatch_budget(q);
1356 }
1357
1358 /*
1359 * Returns true if we did some work AND can potentially do more.
1360 */
blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx * hctx,struct list_head * list,unsigned int nr_budgets)1361 bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
1362 unsigned int nr_budgets)
1363 {
1364 enum prep_dispatch prep;
1365 struct request_queue *q = hctx->queue;
1366 struct request *rq, *nxt;
1367 int errors, queued;
1368 blk_status_t ret = BLK_STS_OK;
1369 LIST_HEAD(zone_list);
1370 bool needs_resource = false;
1371
1372 if (list_empty(list))
1373 return false;
1374
1375 /*
1376 * Now process all the entries, sending them to the driver.
1377 */
1378 errors = queued = 0;
1379 do {
1380 struct blk_mq_queue_data bd;
1381
1382 rq = list_first_entry(list, struct request, queuelist);
1383
1384 WARN_ON_ONCE(hctx != rq->mq_hctx);
1385 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
1386 if (prep != PREP_DISPATCH_OK)
1387 break;
1388
1389 list_del_init(&rq->queuelist);
1390
1391 bd.rq = rq;
1392
1393 /*
1394 * Flag last if we have no more requests, or if we have more
1395 * but can't assign a driver tag to it.
1396 */
1397 if (list_empty(list))
1398 bd.last = true;
1399 else {
1400 nxt = list_first_entry(list, struct request, queuelist);
1401 bd.last = !blk_mq_get_driver_tag(nxt);
1402 }
1403
1404 /*
1405 * once the request is queued to lld, no need to cover the
1406 * budget any more
1407 */
1408 if (nr_budgets)
1409 nr_budgets--;
1410 ret = q->mq_ops->queue_rq(hctx, &bd);
1411 switch (ret) {
1412 case BLK_STS_OK:
1413 queued++;
1414 break;
1415 case BLK_STS_RESOURCE:
1416 needs_resource = true;
1417 fallthrough;
1418 case BLK_STS_DEV_RESOURCE:
1419 blk_mq_handle_dev_resource(rq, list);
1420 goto out;
1421 case BLK_STS_ZONE_RESOURCE:
1422 /*
1423 * Move the request to zone_list and keep going through
1424 * the dispatch list to find more requests the drive can
1425 * accept.
1426 */
1427 blk_mq_handle_zone_resource(rq, &zone_list);
1428 needs_resource = true;
1429 break;
1430 default:
1431 errors++;
1432 blk_mq_end_request(rq, BLK_STS_IOERR);
1433 }
1434 } while (!list_empty(list));
1435 out:
1436 if (!list_empty(&zone_list))
1437 list_splice_tail_init(&zone_list, list);
1438
1439 hctx->dispatched[queued_to_index(queued)]++;
1440
1441 /* If we didn't flush the entire list, we could have told the driver
1442 * there was more coming, but that turned out to be a lie.
1443 */
1444 if ((!list_empty(list) || errors || needs_resource ||
1445 ret == BLK_STS_DEV_RESOURCE) && q->mq_ops->commit_rqs && queued)
1446 q->mq_ops->commit_rqs(hctx);
1447 /*
1448 * Any items that need requeuing? Stuff them into hctx->dispatch,
1449 * that is where we will continue on next queue run.
1450 */
1451 if (!list_empty(list)) {
1452 bool needs_restart;
1453 /* For non-shared tags, the RESTART check will suffice */
1454 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
1455 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
1456
1457 blk_mq_release_budgets(q, nr_budgets);
1458
1459 spin_lock(&hctx->lock);
1460 list_splice_tail_init(list, &hctx->dispatch);
1461 spin_unlock(&hctx->lock);
1462
1463 /*
1464 * Order adding requests to hctx->dispatch and checking
1465 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1466 * in blk_mq_sched_restart(). Avoid restart code path to
1467 * miss the new added requests to hctx->dispatch, meantime
1468 * SCHED_RESTART is observed here.
1469 */
1470 smp_mb();
1471
1472 /*
1473 * If SCHED_RESTART was set by the caller of this function and
1474 * it is no longer set that means that it was cleared by another
1475 * thread and hence that a queue rerun is needed.
1476 *
1477 * If 'no_tag' is set, that means that we failed getting
1478 * a driver tag with an I/O scheduler attached. If our dispatch
1479 * waitqueue is no longer active, ensure that we run the queue
1480 * AFTER adding our entries back to the list.
1481 *
1482 * If no I/O scheduler has been configured it is possible that
1483 * the hardware queue got stopped and restarted before requests
1484 * were pushed back onto the dispatch list. Rerun the queue to
1485 * avoid starvation. Notes:
1486 * - blk_mq_run_hw_queue() checks whether or not a queue has
1487 * been stopped before rerunning a queue.
1488 * - Some but not all block drivers stop a queue before
1489 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
1490 * and dm-rq.
1491 *
1492 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1493 * bit is set, run queue after a delay to avoid IO stalls
1494 * that could otherwise occur if the queue is idle. We'll do
1495 * similar if we couldn't get budget or couldn't lock a zone
1496 * and SCHED_RESTART is set.
1497 */
1498 needs_restart = blk_mq_sched_needs_restart(hctx);
1499 if (prep == PREP_DISPATCH_NO_BUDGET)
1500 needs_resource = true;
1501 if (!needs_restart ||
1502 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
1503 blk_mq_run_hw_queue(hctx, true);
1504 else if (needs_restart && needs_resource)
1505 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
1506
1507 blk_mq_update_dispatch_busy(hctx, true);
1508 return false;
1509 } else
1510 blk_mq_update_dispatch_busy(hctx, false);
1511
1512 return (queued + errors) != 0;
1513 }
1514
1515 /**
1516 * __blk_mq_run_hw_queue - Run a hardware queue.
1517 * @hctx: Pointer to the hardware queue to run.
1518 *
1519 * Send pending requests to the hardware.
1520 */
__blk_mq_run_hw_queue(struct blk_mq_hw_ctx * hctx)1521 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1522 {
1523 int srcu_idx;
1524
1525 /*
1526 * We should be running this queue from one of the CPUs that
1527 * are mapped to it.
1528 *
1529 * There are at least two related races now between setting
1530 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1531 * __blk_mq_run_hw_queue():
1532 *
1533 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1534 * but later it becomes online, then this warning is harmless
1535 * at all
1536 *
1537 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1538 * but later it becomes offline, then the warning can't be
1539 * triggered, and we depend on blk-mq timeout handler to
1540 * handle dispatched requests to this hctx
1541 */
1542 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1543 cpu_online(hctx->next_cpu)) {
1544 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1545 raw_smp_processor_id(),
1546 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1547 dump_stack();
1548 }
1549
1550 /*
1551 * We can't run the queue inline with ints disabled. Ensure that
1552 * we catch bad users of this early.
1553 */
1554 WARN_ON_ONCE(in_interrupt());
1555
1556 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1557
1558 hctx_lock(hctx, &srcu_idx);
1559 blk_mq_sched_dispatch_requests(hctx);
1560 hctx_unlock(hctx, srcu_idx);
1561 }
1562
blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx * hctx)1563 static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1564 {
1565 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1566
1567 if (cpu >= nr_cpu_ids)
1568 cpu = cpumask_first(hctx->cpumask);
1569 return cpu;
1570 }
1571
1572 /*
1573 * It'd be great if the workqueue API had a way to pass
1574 * in a mask and had some smarts for more clever placement.
1575 * For now we just round-robin here, switching for every
1576 * BLK_MQ_CPU_WORK_BATCH queued items.
1577 */
blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx * hctx)1578 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1579 {
1580 bool tried = false;
1581 int next_cpu = hctx->next_cpu;
1582
1583 if (hctx->queue->nr_hw_queues == 1)
1584 return WORK_CPU_UNBOUND;
1585
1586 if (--hctx->next_cpu_batch <= 0) {
1587 select_cpu:
1588 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
1589 cpu_online_mask);
1590 if (next_cpu >= nr_cpu_ids)
1591 next_cpu = blk_mq_first_mapped_cpu(hctx);
1592 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1593 }
1594
1595 /*
1596 * Do unbound schedule if we can't find a online CPU for this hctx,
1597 * and it should only happen in the path of handling CPU DEAD.
1598 */
1599 if (!cpu_online(next_cpu)) {
1600 if (!tried) {
1601 tried = true;
1602 goto select_cpu;
1603 }
1604
1605 /*
1606 * Make sure to re-select CPU next time once after CPUs
1607 * in hctx->cpumask become online again.
1608 */
1609 hctx->next_cpu = next_cpu;
1610 hctx->next_cpu_batch = 1;
1611 return WORK_CPU_UNBOUND;
1612 }
1613
1614 hctx->next_cpu = next_cpu;
1615 return next_cpu;
1616 }
1617
1618 /**
1619 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1620 * @hctx: Pointer to the hardware queue to run.
1621 * @async: If we want to run the queue asynchronously.
1622 * @msecs: Microseconds of delay to wait before running the queue.
1623 *
1624 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1625 * with a delay of @msecs.
1626 */
__blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx * hctx,bool async,unsigned long msecs)1627 static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1628 unsigned long msecs)
1629 {
1630 bool skip = false;
1631
1632 if (unlikely(blk_mq_hctx_stopped(hctx)))
1633 return;
1634
1635 trace_android_rvh_blk_mq_delay_run_hw_queue(&skip, hctx, async);
1636 if (skip)
1637 return;
1638
1639 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
1640 int cpu = get_cpu();
1641 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
1642 __blk_mq_run_hw_queue(hctx);
1643 put_cpu();
1644 return;
1645 }
1646
1647 put_cpu();
1648 }
1649
1650 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1651 msecs_to_jiffies(msecs));
1652 }
1653
1654 /**
1655 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1656 * @hctx: Pointer to the hardware queue to run.
1657 * @msecs: Microseconds of delay to wait before running the queue.
1658 *
1659 * Run a hardware queue asynchronously with a delay of @msecs.
1660 */
blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx * hctx,unsigned long msecs)1661 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1662 {
1663 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1664 }
1665 EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1666
1667 /**
1668 * blk_mq_run_hw_queue - Start to run a hardware queue.
1669 * @hctx: Pointer to the hardware queue to run.
1670 * @async: If we want to run the queue asynchronously.
1671 *
1672 * Check if the request queue is not in a quiesced state and if there are
1673 * pending requests to be sent. If this is true, run the queue to send requests
1674 * to hardware.
1675 */
blk_mq_run_hw_queue(struct blk_mq_hw_ctx * hctx,bool async)1676 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1677 {
1678 int srcu_idx;
1679 bool need_run;
1680
1681 /*
1682 * When queue is quiesced, we may be switching io scheduler, or
1683 * updating nr_hw_queues, or other things, and we can't run queue
1684 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1685 *
1686 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1687 * quiesced.
1688 */
1689 hctx_lock(hctx, &srcu_idx);
1690 need_run = !blk_queue_quiesced(hctx->queue) &&
1691 blk_mq_hctx_has_pending(hctx);
1692 hctx_unlock(hctx, srcu_idx);
1693
1694 trace_android_vh_blk_mq_run_hw_queue(&need_run, hctx);
1695 if (need_run)
1696 __blk_mq_delay_run_hw_queue(hctx, async, 0);
1697 }
1698 EXPORT_SYMBOL(blk_mq_run_hw_queue);
1699
1700 /*
1701 * Is the request queue handled by an IO scheduler that does not respect
1702 * hardware queues when dispatching?
1703 */
blk_mq_has_sqsched(struct request_queue * q)1704 static bool blk_mq_has_sqsched(struct request_queue *q)
1705 {
1706 struct elevator_queue *e = q->elevator;
1707
1708 if (e && e->type->ops.dispatch_request &&
1709 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1710 return true;
1711 return false;
1712 }
1713
1714 /*
1715 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1716 * scheduler.
1717 */
blk_mq_get_sq_hctx(struct request_queue * q)1718 static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1719 {
1720 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
1721 /*
1722 * If the IO scheduler does not respect hardware queues when
1723 * dispatching, we just don't bother with multiple HW queues and
1724 * dispatch from hctx for the current CPU since running multiple queues
1725 * just causes lock contention inside the scheduler and pointless cache
1726 * bouncing.
1727 */
1728 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, 0, ctx);
1729
1730 if (!blk_mq_hctx_stopped(hctx))
1731 return hctx;
1732 return NULL;
1733 }
1734
1735 /**
1736 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
1737 * @q: Pointer to the request queue to run.
1738 * @async: If we want to run the queue asynchronously.
1739 */
blk_mq_run_hw_queues(struct request_queue * q,bool async)1740 void blk_mq_run_hw_queues(struct request_queue *q, bool async)
1741 {
1742 struct blk_mq_hw_ctx *hctx, *sq_hctx;
1743 int i;
1744
1745 sq_hctx = NULL;
1746 if (blk_mq_has_sqsched(q))
1747 sq_hctx = blk_mq_get_sq_hctx(q);
1748 queue_for_each_hw_ctx(q, hctx, i) {
1749 if (blk_mq_hctx_stopped(hctx))
1750 continue;
1751 /*
1752 * Dispatch from this hctx either if there's no hctx preferred
1753 * by IO scheduler or if it has requests that bypass the
1754 * scheduler.
1755 */
1756 if (!sq_hctx || sq_hctx == hctx ||
1757 !list_empty_careful(&hctx->dispatch))
1758 blk_mq_run_hw_queue(hctx, async);
1759 }
1760 }
1761 EXPORT_SYMBOL(blk_mq_run_hw_queues);
1762
1763 /**
1764 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1765 * @q: Pointer to the request queue to run.
1766 * @msecs: Microseconds of delay to wait before running the queues.
1767 */
blk_mq_delay_run_hw_queues(struct request_queue * q,unsigned long msecs)1768 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1769 {
1770 struct blk_mq_hw_ctx *hctx, *sq_hctx;
1771 int i;
1772
1773 sq_hctx = NULL;
1774 if (blk_mq_has_sqsched(q))
1775 sq_hctx = blk_mq_get_sq_hctx(q);
1776 queue_for_each_hw_ctx(q, hctx, i) {
1777 if (blk_mq_hctx_stopped(hctx))
1778 continue;
1779 /*
1780 * Dispatch from this hctx either if there's no hctx preferred
1781 * by IO scheduler or if it has requests that bypass the
1782 * scheduler.
1783 */
1784 if (!sq_hctx || sq_hctx == hctx ||
1785 !list_empty_careful(&hctx->dispatch))
1786 blk_mq_delay_run_hw_queue(hctx, msecs);
1787 }
1788 }
1789 EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1790
1791 /**
1792 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1793 * @q: request queue.
1794 *
1795 * The caller is responsible for serializing this function against
1796 * blk_mq_{start,stop}_hw_queue().
1797 */
blk_mq_queue_stopped(struct request_queue * q)1798 bool blk_mq_queue_stopped(struct request_queue *q)
1799 {
1800 struct blk_mq_hw_ctx *hctx;
1801 int i;
1802
1803 queue_for_each_hw_ctx(q, hctx, i)
1804 if (blk_mq_hctx_stopped(hctx))
1805 return true;
1806
1807 return false;
1808 }
1809 EXPORT_SYMBOL(blk_mq_queue_stopped);
1810
1811 /*
1812 * This function is often used for pausing .queue_rq() by driver when
1813 * there isn't enough resource or some conditions aren't satisfied, and
1814 * BLK_STS_RESOURCE is usually returned.
1815 *
1816 * We do not guarantee that dispatch can be drained or blocked
1817 * after blk_mq_stop_hw_queue() returns. Please use
1818 * blk_mq_quiesce_queue() for that requirement.
1819 */
blk_mq_stop_hw_queue(struct blk_mq_hw_ctx * hctx)1820 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1821 {
1822 cancel_delayed_work(&hctx->run_work);
1823
1824 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
1825 }
1826 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1827
1828 /*
1829 * This function is often used for pausing .queue_rq() by driver when
1830 * there isn't enough resource or some conditions aren't satisfied, and
1831 * BLK_STS_RESOURCE is usually returned.
1832 *
1833 * We do not guarantee that dispatch can be drained or blocked
1834 * after blk_mq_stop_hw_queues() returns. Please use
1835 * blk_mq_quiesce_queue() for that requirement.
1836 */
blk_mq_stop_hw_queues(struct request_queue * q)1837 void blk_mq_stop_hw_queues(struct request_queue *q)
1838 {
1839 struct blk_mq_hw_ctx *hctx;
1840 int i;
1841
1842 queue_for_each_hw_ctx(q, hctx, i)
1843 blk_mq_stop_hw_queue(hctx);
1844 }
1845 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1846
blk_mq_start_hw_queue(struct blk_mq_hw_ctx * hctx)1847 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1848 {
1849 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1850
1851 blk_mq_run_hw_queue(hctx, false);
1852 }
1853 EXPORT_SYMBOL(blk_mq_start_hw_queue);
1854
blk_mq_start_hw_queues(struct request_queue * q)1855 void blk_mq_start_hw_queues(struct request_queue *q)
1856 {
1857 struct blk_mq_hw_ctx *hctx;
1858 int i;
1859
1860 queue_for_each_hw_ctx(q, hctx, i)
1861 blk_mq_start_hw_queue(hctx);
1862 }
1863 EXPORT_SYMBOL(blk_mq_start_hw_queues);
1864
blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx * hctx,bool async)1865 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1866 {
1867 if (!blk_mq_hctx_stopped(hctx))
1868 return;
1869
1870 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1871 blk_mq_run_hw_queue(hctx, async);
1872 }
1873 EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1874
blk_mq_start_stopped_hw_queues(struct request_queue * q,bool async)1875 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
1876 {
1877 struct blk_mq_hw_ctx *hctx;
1878 int i;
1879
1880 queue_for_each_hw_ctx(q, hctx, i)
1881 blk_mq_start_stopped_hw_queue(hctx, async);
1882 }
1883 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1884
blk_mq_run_work_fn(struct work_struct * work)1885 static void blk_mq_run_work_fn(struct work_struct *work)
1886 {
1887 struct blk_mq_hw_ctx *hctx;
1888
1889 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
1890
1891 /*
1892 * If we are stopped, don't run the queue.
1893 */
1894 if (blk_mq_hctx_stopped(hctx))
1895 return;
1896
1897 __blk_mq_run_hw_queue(hctx);
1898 }
1899
__blk_mq_insert_req_list(struct blk_mq_hw_ctx * hctx,struct request * rq,bool at_head)1900 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
1901 struct request *rq,
1902 bool at_head)
1903 {
1904 struct blk_mq_ctx *ctx = rq->mq_ctx;
1905 enum hctx_type type = hctx->type;
1906
1907 lockdep_assert_held(&ctx->lock);
1908
1909 trace_block_rq_insert(hctx->queue, rq);
1910
1911 if (at_head)
1912 list_add(&rq->queuelist, &ctx->rq_lists[type]);
1913 else
1914 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
1915 }
1916
__blk_mq_insert_request(struct blk_mq_hw_ctx * hctx,struct request * rq,bool at_head)1917 void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1918 bool at_head)
1919 {
1920 struct blk_mq_ctx *ctx = rq->mq_ctx;
1921 bool skip = false;
1922
1923 lockdep_assert_held(&ctx->lock);
1924
1925 trace_android_vh_blk_mq_insert_request(&skip, hctx, rq);
1926 if (skip)
1927 return;
1928
1929 __blk_mq_insert_req_list(hctx, rq, at_head);
1930 blk_mq_hctx_mark_pending(hctx, ctx);
1931 }
1932
1933 /**
1934 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1935 * @rq: Pointer to request to be inserted.
1936 * @at_head: true if the request should be inserted at the head of the list.
1937 * @run_queue: If we should run the hardware queue after inserting the request.
1938 *
1939 * Should only be used carefully, when the caller knows we want to
1940 * bypass a potential IO scheduler on the target device.
1941 */
blk_mq_request_bypass_insert(struct request * rq,bool at_head,bool run_queue)1942 void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1943 bool run_queue)
1944 {
1945 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1946
1947 spin_lock(&hctx->lock);
1948 if (at_head)
1949 list_add(&rq->queuelist, &hctx->dispatch);
1950 else
1951 list_add_tail(&rq->queuelist, &hctx->dispatch);
1952 spin_unlock(&hctx->lock);
1953
1954 if (run_queue)
1955 blk_mq_run_hw_queue(hctx, false);
1956 }
1957
blk_mq_insert_requests(struct blk_mq_hw_ctx * hctx,struct blk_mq_ctx * ctx,struct list_head * list)1958 void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1959 struct list_head *list)
1960
1961 {
1962 struct request *rq;
1963 enum hctx_type type = hctx->type;
1964
1965 /*
1966 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1967 * offline now
1968 */
1969 list_for_each_entry(rq, list, queuelist) {
1970 BUG_ON(rq->mq_ctx != ctx);
1971 trace_block_rq_insert(hctx->queue, rq);
1972 }
1973
1974 spin_lock(&ctx->lock);
1975 list_splice_tail_init(list, &ctx->rq_lists[type]);
1976 blk_mq_hctx_mark_pending(hctx, ctx);
1977 spin_unlock(&ctx->lock);
1978 }
1979
plug_rq_cmp(void * priv,struct list_head * a,struct list_head * b)1980 static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
1981 {
1982 struct request *rqa = container_of(a, struct request, queuelist);
1983 struct request *rqb = container_of(b, struct request, queuelist);
1984
1985 if (rqa->mq_ctx != rqb->mq_ctx)
1986 return rqa->mq_ctx > rqb->mq_ctx;
1987 if (rqa->mq_hctx != rqb->mq_hctx)
1988 return rqa->mq_hctx > rqb->mq_hctx;
1989
1990 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
1991 }
1992
blk_mq_flush_plug_list(struct blk_plug * plug,bool from_schedule)1993 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1994 {
1995 LIST_HEAD(list);
1996
1997 if (list_empty(&plug->mq_list))
1998 return;
1999 list_splice_init(&plug->mq_list, &list);
2000
2001 if (plug->rq_count > 2 && plug->multiple_queues)
2002 list_sort(NULL, &list, plug_rq_cmp);
2003
2004 plug->rq_count = 0;
2005
2006 do {
2007 struct list_head rq_list;
2008 struct request *rq, *head_rq = list_entry_rq(list.next);
2009 struct list_head *pos = &head_rq->queuelist; /* skip first */
2010 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
2011 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
2012 unsigned int depth = 1;
2013
2014 list_for_each_continue(pos, &list) {
2015 rq = list_entry_rq(pos);
2016 BUG_ON(!rq->q);
2017 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
2018 break;
2019 depth++;
2020 }
2021
2022 list_cut_before(&rq_list, &list, pos);
2023 trace_block_unplug(head_rq->q, depth, !from_schedule);
2024 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
2025 from_schedule);
2026 } while(!list_empty(&list));
2027 }
2028
blk_mq_bio_to_request(struct request * rq,struct bio * bio,unsigned int nr_segs)2029 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
2030 unsigned int nr_segs)
2031 {
2032 int err;
2033
2034 if (bio->bi_opf & REQ_RAHEAD)
2035 rq->cmd_flags |= REQ_FAILFAST_MASK;
2036
2037 rq->__sector = bio->bi_iter.bi_sector;
2038 rq->write_hint = bio->bi_write_hint;
2039 blk_rq_bio_prep(rq, bio, nr_segs);
2040
2041 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2042 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2043 WARN_ON_ONCE(err);
2044
2045 blk_account_io_start(rq);
2046 }
2047
__blk_mq_issue_directly(struct blk_mq_hw_ctx * hctx,struct request * rq,blk_qc_t * cookie,bool last)2048 static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
2049 struct request *rq,
2050 blk_qc_t *cookie, bool last)
2051 {
2052 struct request_queue *q = rq->q;
2053 struct blk_mq_queue_data bd = {
2054 .rq = rq,
2055 .last = last,
2056 };
2057 blk_qc_t new_cookie;
2058 blk_status_t ret;
2059
2060 new_cookie = request_to_qc_t(hctx, rq);
2061
2062 /*
2063 * For OK queue, we are done. For error, caller may kill it.
2064 * Any other error (busy), just add it to our list as we
2065 * previously would have done.
2066 */
2067 ret = q->mq_ops->queue_rq(hctx, &bd);
2068 switch (ret) {
2069 case BLK_STS_OK:
2070 blk_mq_update_dispatch_busy(hctx, false);
2071 *cookie = new_cookie;
2072 break;
2073 case BLK_STS_RESOURCE:
2074 case BLK_STS_DEV_RESOURCE:
2075 blk_mq_update_dispatch_busy(hctx, true);
2076 __blk_mq_requeue_request(rq);
2077 break;
2078 default:
2079 blk_mq_update_dispatch_busy(hctx, false);
2080 *cookie = BLK_QC_T_NONE;
2081 break;
2082 }
2083
2084 return ret;
2085 }
2086
__blk_mq_try_issue_directly(struct blk_mq_hw_ctx * hctx,struct request * rq,blk_qc_t * cookie,bool bypass_insert,bool last)2087 static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2088 struct request *rq,
2089 blk_qc_t *cookie,
2090 bool bypass_insert, bool last)
2091 {
2092 struct request_queue *q = rq->q;
2093 bool run_queue = true;
2094
2095 /*
2096 * RCU or SRCU read lock is needed before checking quiesced flag.
2097 *
2098 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2099 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2100 * and avoid driver to try to dispatch again.
2101 */
2102 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
2103 run_queue = false;
2104 bypass_insert = false;
2105 goto insert;
2106 }
2107
2108 if (q->elevator && !bypass_insert)
2109 goto insert;
2110
2111 if (!blk_mq_get_dispatch_budget(q))
2112 goto insert;
2113
2114 if (!blk_mq_get_driver_tag(rq)) {
2115 blk_mq_put_dispatch_budget(q);
2116 goto insert;
2117 }
2118
2119 return __blk_mq_issue_directly(hctx, rq, cookie, last);
2120 insert:
2121 if (bypass_insert)
2122 return BLK_STS_RESOURCE;
2123
2124 blk_mq_sched_insert_request(rq, false, run_queue, false);
2125
2126 return BLK_STS_OK;
2127 }
2128
2129 /**
2130 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2131 * @hctx: Pointer of the associated hardware queue.
2132 * @rq: Pointer to request to be sent.
2133 * @cookie: Request queue cookie.
2134 *
2135 * If the device has enough resources to accept a new request now, send the
2136 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2137 * we can try send it another time in the future. Requests inserted at this
2138 * queue have higher priority.
2139 */
blk_mq_try_issue_directly(struct blk_mq_hw_ctx * hctx,struct request * rq,blk_qc_t * cookie)2140 static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2141 struct request *rq, blk_qc_t *cookie)
2142 {
2143 blk_status_t ret;
2144 int srcu_idx;
2145
2146 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2147
2148 hctx_lock(hctx, &srcu_idx);
2149
2150 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
2151 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
2152 blk_mq_request_bypass_insert(rq, false, true);
2153 else if (ret != BLK_STS_OK)
2154 blk_mq_end_request(rq, ret);
2155
2156 hctx_unlock(hctx, srcu_idx);
2157 }
2158
blk_mq_request_issue_directly(struct request * rq,bool last)2159 blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2160 {
2161 blk_status_t ret;
2162 int srcu_idx;
2163 blk_qc_t unused_cookie;
2164 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2165
2166 hctx_lock(hctx, &srcu_idx);
2167 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2168 hctx_unlock(hctx, srcu_idx);
2169
2170 return ret;
2171 }
2172
blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx * hctx,struct list_head * list)2173 void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2174 struct list_head *list)
2175 {
2176 int queued = 0;
2177 int errors = 0;
2178
2179 while (!list_empty(list)) {
2180 blk_status_t ret;
2181 struct request *rq = list_first_entry(list, struct request,
2182 queuelist);
2183
2184 list_del_init(&rq->queuelist);
2185 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2186 if (ret != BLK_STS_OK) {
2187 errors++;
2188 if (ret == BLK_STS_RESOURCE ||
2189 ret == BLK_STS_DEV_RESOURCE) {
2190 blk_mq_request_bypass_insert(rq, false,
2191 list_empty(list));
2192 break;
2193 }
2194 blk_mq_end_request(rq, ret);
2195 } else
2196 queued++;
2197 }
2198
2199 /*
2200 * If we didn't flush the entire list, we could have told
2201 * the driver there was more coming, but that turned out to
2202 * be a lie.
2203 */
2204 if ((!list_empty(list) || errors) &&
2205 hctx->queue->mq_ops->commit_rqs && queued)
2206 hctx->queue->mq_ops->commit_rqs(hctx);
2207 }
2208
blk_add_rq_to_plug(struct blk_plug * plug,struct request * rq)2209 static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2210 {
2211 list_add_tail(&rq->queuelist, &plug->mq_list);
2212 plug->rq_count++;
2213 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2214 struct request *tmp;
2215
2216 tmp = list_first_entry(&plug->mq_list, struct request,
2217 queuelist);
2218 if (tmp->q != rq->q)
2219 plug->multiple_queues = true;
2220 }
2221 }
2222
2223 /*
2224 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
2225 * queues. This is important for md arrays to benefit from merging
2226 * requests.
2227 */
blk_plug_max_rq_count(struct blk_plug * plug)2228 static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2229 {
2230 if (plug->multiple_queues)
2231 return BLK_MAX_REQUEST_COUNT * 2;
2232 return BLK_MAX_REQUEST_COUNT;
2233 }
2234
2235 /**
2236 * blk_mq_submit_bio - Create and send a request to block device.
2237 * @bio: Bio pointer.
2238 *
2239 * Builds up a request structure from @q and @bio and send to the device. The
2240 * request may not be queued directly to hardware if:
2241 * * This request can be merged with another one
2242 * * We want to place request at plug queue for possible future merging
2243 * * There is an IO scheduler active at this queue
2244 *
2245 * It will not queue the request if there is an error with the bio, or at the
2246 * request creation.
2247 *
2248 * Returns: Request queue cookie.
2249 */
blk_mq_submit_bio(struct bio * bio)2250 blk_qc_t blk_mq_submit_bio(struct bio *bio)
2251 {
2252 struct request_queue *q = bio->bi_disk->queue;
2253 const int is_sync = op_is_sync(bio->bi_opf);
2254 const int is_flush_fua = op_is_flush(bio->bi_opf);
2255 struct blk_mq_alloc_data data = {
2256 .q = q,
2257 };
2258 struct request *rq;
2259 struct blk_plug *plug;
2260 struct request *same_queue_rq = NULL;
2261 unsigned int nr_segs;
2262 blk_qc_t cookie;
2263 blk_status_t ret;
2264
2265 blk_queue_bounce(q, &bio);
2266 __blk_queue_split(&bio, &nr_segs);
2267
2268 if (!bio_integrity_prep(bio))
2269 goto queue_exit;
2270
2271 if (!is_flush_fua && !blk_queue_nomerges(q) &&
2272 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
2273 goto queue_exit;
2274
2275 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
2276 goto queue_exit;
2277
2278 rq_qos_throttle(q, bio);
2279
2280 data.cmd_flags = bio->bi_opf;
2281 rq = __blk_mq_alloc_request(&data);
2282 if (unlikely(!rq)) {
2283 rq_qos_cleanup(q, bio);
2284 if (bio->bi_opf & REQ_NOWAIT)
2285 bio_wouldblock_error(bio);
2286 goto queue_exit;
2287 }
2288
2289 trace_block_getrq(q, bio, bio->bi_opf);
2290
2291 rq_qos_track(q, rq, bio);
2292
2293 cookie = request_to_qc_t(data.hctx, rq);
2294
2295 blk_mq_bio_to_request(rq, bio, nr_segs);
2296
2297 ret = blk_crypto_rq_get_keyslot(rq);
2298 if (ret != BLK_STS_OK) {
2299 bio->bi_status = ret;
2300 bio_endio(bio);
2301 blk_mq_free_request(rq);
2302 return BLK_QC_T_NONE;
2303 }
2304
2305 plug = blk_mq_plug(q, bio);
2306 if (unlikely(is_flush_fua)) {
2307 /* Bypass scheduler for flush requests */
2308 blk_insert_flush(rq);
2309 blk_mq_run_hw_queue(data.hctx, true);
2310 } else if (plug && (q->nr_hw_queues == 1 ||
2311 blk_mq_is_sbitmap_shared(rq->mq_hctx->flags) ||
2312 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
2313 /*
2314 * Use plugging if we have a ->commit_rqs() hook as well, as
2315 * we know the driver uses bd->last in a smart fashion.
2316 *
2317 * Use normal plugging if this disk is slow HDD, as sequential
2318 * IO may benefit a lot from plug merging.
2319 */
2320 unsigned int request_count = plug->rq_count;
2321 struct request *last = NULL;
2322
2323 if (!request_count)
2324 trace_block_plug(q);
2325 else
2326 last = list_entry_rq(plug->mq_list.prev);
2327
2328 if (request_count >= blk_plug_max_rq_count(plug) || (last &&
2329 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
2330 blk_flush_plug_list(plug, false);
2331 trace_block_plug(q);
2332 }
2333
2334 blk_add_rq_to_plug(plug, rq);
2335 } else if (q->elevator) {
2336 /* Insert the request at the IO scheduler queue */
2337 blk_mq_sched_insert_request(rq, false, true, true);
2338 } else if (plug && !blk_queue_nomerges(q)) {
2339 /*
2340 * We do limited plugging. If the bio can be merged, do that.
2341 * Otherwise the existing request in the plug list will be
2342 * issued. So the plug list will have one request at most
2343 * The plug list might get flushed before this. If that happens,
2344 * the plug list is empty, and same_queue_rq is invalid.
2345 */
2346 if (list_empty(&plug->mq_list))
2347 same_queue_rq = NULL;
2348 if (same_queue_rq) {
2349 list_del_init(&same_queue_rq->queuelist);
2350 plug->rq_count--;
2351 }
2352 blk_add_rq_to_plug(plug, rq);
2353 trace_block_plug(q);
2354
2355 if (same_queue_rq) {
2356 data.hctx = same_queue_rq->mq_hctx;
2357 trace_block_unplug(q, 1, true);
2358 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
2359 &cookie);
2360 }
2361 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2362 !data.hctx->dispatch_busy) {
2363 /*
2364 * There is no scheduler and we can try to send directly
2365 * to the hardware.
2366 */
2367 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
2368 } else {
2369 /* Default case. */
2370 blk_mq_sched_insert_request(rq, false, true, true);
2371 }
2372
2373 return cookie;
2374 queue_exit:
2375 blk_queue_exit(q);
2376 return BLK_QC_T_NONE;
2377 }
2378
order_to_size(unsigned int order)2379 static size_t order_to_size(unsigned int order)
2380 {
2381 return (size_t)PAGE_SIZE << order;
2382 }
2383
2384 /* called before freeing request pool in @tags */
blk_mq_clear_rq_mapping(struct blk_mq_tag_set * set,struct blk_mq_tags * tags,unsigned int hctx_idx)2385 static void blk_mq_clear_rq_mapping(struct blk_mq_tag_set *set,
2386 struct blk_mq_tags *tags, unsigned int hctx_idx)
2387 {
2388 struct blk_mq_tags *drv_tags = set->tags[hctx_idx];
2389 struct page *page;
2390 unsigned long flags;
2391
2392 list_for_each_entry(page, &tags->page_list, lru) {
2393 unsigned long start = (unsigned long)page_address(page);
2394 unsigned long end = start + order_to_size(page->private);
2395 int i;
2396
2397 for (i = 0; i < set->queue_depth; i++) {
2398 struct request *rq = drv_tags->rqs[i];
2399 unsigned long rq_addr = (unsigned long)rq;
2400
2401 if (rq_addr >= start && rq_addr < end) {
2402 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2403 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2404 }
2405 }
2406 }
2407
2408 /*
2409 * Wait until all pending iteration is done.
2410 *
2411 * Request reference is cleared and it is guaranteed to be observed
2412 * after the ->lock is released.
2413 */
2414 spin_lock_irqsave(&drv_tags->lock, flags);
2415 spin_unlock_irqrestore(&drv_tags->lock, flags);
2416 }
2417
blk_mq_free_rqs(struct blk_mq_tag_set * set,struct blk_mq_tags * tags,unsigned int hctx_idx)2418 void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2419 unsigned int hctx_idx)
2420 {
2421 struct page *page;
2422
2423 if (tags->rqs && set->ops->exit_request) {
2424 int i;
2425
2426 for (i = 0; i < tags->nr_tags; i++) {
2427 struct request *rq = tags->static_rqs[i];
2428
2429 if (!rq)
2430 continue;
2431 set->ops->exit_request(set, rq, hctx_idx);
2432 tags->static_rqs[i] = NULL;
2433 }
2434 }
2435
2436 blk_mq_clear_rq_mapping(set, tags, hctx_idx);
2437
2438 while (!list_empty(&tags->page_list)) {
2439 page = list_first_entry(&tags->page_list, struct page, lru);
2440 list_del_init(&page->lru);
2441 /*
2442 * Remove kmemleak object previously allocated in
2443 * blk_mq_alloc_rqs().
2444 */
2445 kmemleak_free(page_address(page));
2446 __free_pages(page, page->private);
2447 }
2448 }
2449
blk_mq_free_rq_map(struct blk_mq_tags * tags,unsigned int flags)2450 void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags)
2451 {
2452 kfree(tags->rqs);
2453 tags->rqs = NULL;
2454 kfree(tags->static_rqs);
2455 tags->static_rqs = NULL;
2456
2457 blk_mq_free_tags(tags, flags);
2458 }
2459
blk_mq_alloc_rq_map(struct blk_mq_tag_set * set,unsigned int hctx_idx,unsigned int nr_tags,unsigned int reserved_tags,unsigned int flags)2460 struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2461 unsigned int hctx_idx,
2462 unsigned int nr_tags,
2463 unsigned int reserved_tags,
2464 unsigned int flags)
2465 {
2466 struct blk_mq_tags *tags;
2467 int node;
2468 bool skip = false;
2469
2470 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
2471 if (node == NUMA_NO_NODE)
2472 node = set->numa_node;
2473
2474 trace_android_rvh_blk_mq_alloc_rq_map(&skip, &tags, set, node, flags);
2475 if (!skip)
2476 tags = blk_mq_init_tags(nr_tags, reserved_tags, node, flags);
2477 if (!tags)
2478 return NULL;
2479
2480 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2481 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2482 node);
2483 if (!tags->rqs) {
2484 blk_mq_free_tags(tags, flags);
2485 return NULL;
2486 }
2487
2488 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2489 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2490 node);
2491 if (!tags->static_rqs) {
2492 kfree(tags->rqs);
2493 blk_mq_free_tags(tags, flags);
2494 return NULL;
2495 }
2496
2497 return tags;
2498 }
2499
blk_mq_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,int node)2500 static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2501 unsigned int hctx_idx, int node)
2502 {
2503 int ret;
2504
2505 if (set->ops->init_request) {
2506 ret = set->ops->init_request(set, rq, hctx_idx, node);
2507 if (ret)
2508 return ret;
2509 }
2510
2511 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
2512 return 0;
2513 }
2514
blk_mq_alloc_rqs(struct blk_mq_tag_set * set,struct blk_mq_tags * tags,unsigned int hctx_idx,unsigned int depth)2515 int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2516 unsigned int hctx_idx, unsigned int depth)
2517 {
2518 unsigned int i, j, entries_per_page, max_order = 4;
2519 size_t rq_size, left;
2520 int node;
2521
2522 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
2523 if (node == NUMA_NO_NODE)
2524 node = set->numa_node;
2525
2526 INIT_LIST_HEAD(&tags->page_list);
2527
2528 /*
2529 * rq_size is the size of the request plus driver payload, rounded
2530 * to the cacheline size
2531 */
2532 rq_size = round_up(sizeof(struct request) + set->cmd_size,
2533 cache_line_size());
2534 trace_android_vh_blk_alloc_rqs(&rq_size, set, tags);
2535 left = rq_size * depth;
2536
2537 for (i = 0; i < depth; ) {
2538 int this_order = max_order;
2539 struct page *page;
2540 int to_do;
2541 void *p;
2542
2543 while (this_order && left < order_to_size(this_order - 1))
2544 this_order--;
2545
2546 do {
2547 page = alloc_pages_node(node,
2548 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
2549 this_order);
2550 if (page)
2551 break;
2552 if (!this_order--)
2553 break;
2554 if (order_to_size(this_order) < rq_size)
2555 break;
2556 } while (1);
2557
2558 if (!page)
2559 goto fail;
2560
2561 page->private = this_order;
2562 list_add_tail(&page->lru, &tags->page_list);
2563
2564 p = page_address(page);
2565 /*
2566 * Allow kmemleak to scan these pages as they contain pointers
2567 * to additional allocations like via ops->init_request().
2568 */
2569 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
2570 entries_per_page = order_to_size(this_order) / rq_size;
2571 to_do = min(entries_per_page, depth - i);
2572 left -= to_do * rq_size;
2573 for (j = 0; j < to_do; j++) {
2574 struct request *rq = p;
2575
2576 tags->static_rqs[i] = rq;
2577 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2578 tags->static_rqs[i] = NULL;
2579 goto fail;
2580 }
2581
2582 p += rq_size;
2583 i++;
2584 }
2585 }
2586 return 0;
2587
2588 fail:
2589 blk_mq_free_rqs(set, tags, hctx_idx);
2590 return -ENOMEM;
2591 }
2592
2593 struct rq_iter_data {
2594 struct blk_mq_hw_ctx *hctx;
2595 bool has_rq;
2596 };
2597
blk_mq_has_request(struct request * rq,void * data,bool reserved)2598 static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2599 {
2600 struct rq_iter_data *iter_data = data;
2601
2602 if (rq->mq_hctx != iter_data->hctx)
2603 return true;
2604 iter_data->has_rq = true;
2605 return false;
2606 }
2607
blk_mq_hctx_has_requests(struct blk_mq_hw_ctx * hctx)2608 static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2609 {
2610 struct blk_mq_tags *tags = hctx->sched_tags ?
2611 hctx->sched_tags : hctx->tags;
2612 struct rq_iter_data data = {
2613 .hctx = hctx,
2614 };
2615
2616 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2617 return data.has_rq;
2618 }
2619
blk_mq_last_cpu_in_hctx(unsigned int cpu,struct blk_mq_hw_ctx * hctx)2620 static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2621 struct blk_mq_hw_ctx *hctx)
2622 {
2623 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2624 return false;
2625 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2626 return false;
2627 return true;
2628 }
2629
blk_mq_hctx_notify_offline(unsigned int cpu,struct hlist_node * node)2630 static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2631 {
2632 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2633 struct blk_mq_hw_ctx, cpuhp_online);
2634
2635 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2636 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2637 return 0;
2638
2639 /*
2640 * Prevent new request from being allocated on the current hctx.
2641 *
2642 * The smp_mb__after_atomic() Pairs with the implied barrier in
2643 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2644 * seen once we return from the tag allocator.
2645 */
2646 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2647 smp_mb__after_atomic();
2648
2649 /*
2650 * Try to grab a reference to the queue and wait for any outstanding
2651 * requests. If we could not grab a reference the queue has been
2652 * frozen and there are no requests.
2653 */
2654 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2655 while (blk_mq_hctx_has_requests(hctx))
2656 msleep(5);
2657 percpu_ref_put(&hctx->queue->q_usage_counter);
2658 }
2659
2660 return 0;
2661 }
2662
blk_mq_hctx_notify_online(unsigned int cpu,struct hlist_node * node)2663 static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2664 {
2665 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2666 struct blk_mq_hw_ctx, cpuhp_online);
2667
2668 if (cpumask_test_cpu(cpu, hctx->cpumask))
2669 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2670 return 0;
2671 }
2672
2673 /*
2674 * 'cpu' is going away. splice any existing rq_list entries from this
2675 * software queue to the hw queue dispatch list, and ensure that it
2676 * gets run.
2677 */
blk_mq_hctx_notify_dead(unsigned int cpu,struct hlist_node * node)2678 static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
2679 {
2680 struct blk_mq_hw_ctx *hctx;
2681 struct blk_mq_ctx *ctx;
2682 LIST_HEAD(tmp);
2683 enum hctx_type type;
2684
2685 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
2686 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2687 return 0;
2688
2689 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
2690 type = hctx->type;
2691
2692 spin_lock(&ctx->lock);
2693 if (!list_empty(&ctx->rq_lists[type])) {
2694 list_splice_init(&ctx->rq_lists[type], &tmp);
2695 blk_mq_hctx_clear_pending(hctx, ctx);
2696 }
2697 spin_unlock(&ctx->lock);
2698
2699 if (list_empty(&tmp))
2700 return 0;
2701
2702 spin_lock(&hctx->lock);
2703 list_splice_tail_init(&tmp, &hctx->dispatch);
2704 spin_unlock(&hctx->lock);
2705
2706 blk_mq_run_hw_queue(hctx, true);
2707 return 0;
2708 }
2709
blk_mq_remove_cpuhp(struct blk_mq_hw_ctx * hctx)2710 static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
2711 {
2712 if (!(hctx->flags & BLK_MQ_F_STACKING))
2713 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2714 &hctx->cpuhp_online);
2715 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2716 &hctx->cpuhp_dead);
2717 }
2718
2719 /*
2720 * Before freeing hw queue, clearing the flush request reference in
2721 * tags->rqs[] for avoiding potential UAF.
2722 */
blk_mq_clear_flush_rq_mapping(struct blk_mq_tags * tags,unsigned int queue_depth,struct request * flush_rq)2723 static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
2724 unsigned int queue_depth, struct request *flush_rq)
2725 {
2726 int i;
2727 unsigned long flags;
2728
2729 /* The hw queue may not be mapped yet */
2730 if (!tags)
2731 return;
2732
2733 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
2734
2735 for (i = 0; i < queue_depth; i++)
2736 cmpxchg(&tags->rqs[i], flush_rq, NULL);
2737
2738 /*
2739 * Wait until all pending iteration is done.
2740 *
2741 * Request reference is cleared and it is guaranteed to be observed
2742 * after the ->lock is released.
2743 */
2744 spin_lock_irqsave(&tags->lock, flags);
2745 spin_unlock_irqrestore(&tags->lock, flags);
2746 }
2747
2748 /* hctx->ctxs will be freed in queue's release handler */
blk_mq_exit_hctx(struct request_queue * q,struct blk_mq_tag_set * set,struct blk_mq_hw_ctx * hctx,unsigned int hctx_idx)2749 static void blk_mq_exit_hctx(struct request_queue *q,
2750 struct blk_mq_tag_set *set,
2751 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2752 {
2753 struct request *flush_rq = hctx->fq->flush_rq;
2754
2755 if (blk_mq_hw_queue_mapped(hctx))
2756 blk_mq_tag_idle(hctx);
2757
2758 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
2759 set->queue_depth, flush_rq);
2760 if (set->ops->exit_request)
2761 set->ops->exit_request(set, flush_rq, hctx_idx);
2762
2763 if (set->ops->exit_hctx)
2764 set->ops->exit_hctx(hctx, hctx_idx);
2765
2766 blk_mq_remove_cpuhp(hctx);
2767
2768 spin_lock(&q->unused_hctx_lock);
2769 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2770 spin_unlock(&q->unused_hctx_lock);
2771 }
2772
blk_mq_exit_hw_queues(struct request_queue * q,struct blk_mq_tag_set * set,int nr_queue)2773 static void blk_mq_exit_hw_queues(struct request_queue *q,
2774 struct blk_mq_tag_set *set, int nr_queue)
2775 {
2776 struct blk_mq_hw_ctx *hctx;
2777 unsigned int i;
2778
2779 queue_for_each_hw_ctx(q, hctx, i) {
2780 if (i == nr_queue)
2781 break;
2782 blk_mq_debugfs_unregister_hctx(hctx);
2783 blk_mq_exit_hctx(q, set, hctx, i);
2784 }
2785 }
2786
blk_mq_hw_ctx_size(struct blk_mq_tag_set * tag_set)2787 static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2788 {
2789 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2790
2791 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2792 __alignof__(struct blk_mq_hw_ctx)) !=
2793 sizeof(struct blk_mq_hw_ctx));
2794
2795 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2796 hw_ctx_size += sizeof(struct srcu_struct);
2797
2798 return hw_ctx_size;
2799 }
2800
blk_mq_init_hctx(struct request_queue * q,struct blk_mq_tag_set * set,struct blk_mq_hw_ctx * hctx,unsigned hctx_idx)2801 static int blk_mq_init_hctx(struct request_queue *q,
2802 struct blk_mq_tag_set *set,
2803 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2804 {
2805 hctx->queue_num = hctx_idx;
2806
2807 if (!(hctx->flags & BLK_MQ_F_STACKING))
2808 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2809 &hctx->cpuhp_online);
2810 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2811
2812 hctx->tags = set->tags[hctx_idx];
2813
2814 if (set->ops->init_hctx &&
2815 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2816 goto unregister_cpu_notifier;
2817
2818 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2819 hctx->numa_node))
2820 goto exit_hctx;
2821 return 0;
2822
2823 exit_hctx:
2824 if (set->ops->exit_hctx)
2825 set->ops->exit_hctx(hctx, hctx_idx);
2826 unregister_cpu_notifier:
2827 blk_mq_remove_cpuhp(hctx);
2828 return -1;
2829 }
2830
2831 static struct blk_mq_hw_ctx *
blk_mq_alloc_hctx(struct request_queue * q,struct blk_mq_tag_set * set,int node)2832 blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2833 int node)
2834 {
2835 struct blk_mq_hw_ctx *hctx;
2836 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2837
2838 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2839 if (!hctx)
2840 goto fail_alloc_hctx;
2841
2842 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2843 goto free_hctx;
2844
2845 atomic_set(&hctx->nr_active, 0);
2846 if (node == NUMA_NO_NODE)
2847 node = set->numa_node;
2848 hctx->numa_node = node;
2849
2850 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
2851 spin_lock_init(&hctx->lock);
2852 INIT_LIST_HEAD(&hctx->dispatch);
2853 hctx->queue = q;
2854 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
2855
2856 INIT_LIST_HEAD(&hctx->hctx_list);
2857
2858 /*
2859 * Allocate space for all possible cpus to avoid allocation at
2860 * runtime
2861 */
2862 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
2863 gfp, node);
2864 if (!hctx->ctxs)
2865 goto free_cpumask;
2866
2867 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
2868 gfp, node))
2869 goto free_ctxs;
2870 hctx->nr_ctx = 0;
2871
2872 spin_lock_init(&hctx->dispatch_wait_lock);
2873 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2874 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2875
2876 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
2877 if (!hctx->fq)
2878 goto free_bitmap;
2879
2880 if (hctx->flags & BLK_MQ_F_BLOCKING)
2881 init_srcu_struct(hctx->srcu);
2882 blk_mq_hctx_kobj_init(hctx);
2883
2884 return hctx;
2885
2886 free_bitmap:
2887 sbitmap_free(&hctx->ctx_map);
2888 free_ctxs:
2889 kfree(hctx->ctxs);
2890 free_cpumask:
2891 free_cpumask_var(hctx->cpumask);
2892 free_hctx:
2893 kfree(hctx);
2894 fail_alloc_hctx:
2895 return NULL;
2896 }
2897
blk_mq_init_cpu_queues(struct request_queue * q,unsigned int nr_hw_queues)2898 static void blk_mq_init_cpu_queues(struct request_queue *q,
2899 unsigned int nr_hw_queues)
2900 {
2901 struct blk_mq_tag_set *set = q->tag_set;
2902 unsigned int i, j;
2903
2904 for_each_possible_cpu(i) {
2905 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2906 struct blk_mq_hw_ctx *hctx;
2907 int k;
2908
2909 __ctx->cpu = i;
2910 spin_lock_init(&__ctx->lock);
2911 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2912 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2913
2914 __ctx->queue = q;
2915
2916 /*
2917 * Set local node, IFF we have more than one hw queue. If
2918 * not, we remain on the home node of the device
2919 */
2920 for (j = 0; j < set->nr_maps; j++) {
2921 hctx = blk_mq_map_queue_type(q, j, i);
2922 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2923 hctx->numa_node = cpu_to_node(i);
2924 }
2925 }
2926 }
2927
__blk_mq_alloc_map_and_request(struct blk_mq_tag_set * set,int hctx_idx)2928 static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2929 int hctx_idx)
2930 {
2931 unsigned int flags = set->flags;
2932 int ret = 0;
2933
2934 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2935 set->queue_depth, set->reserved_tags, flags);
2936 if (!set->tags[hctx_idx])
2937 return false;
2938
2939 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2940 set->queue_depth);
2941 if (!ret)
2942 return true;
2943
2944 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
2945 set->tags[hctx_idx] = NULL;
2946 return false;
2947 }
2948
blk_mq_free_map_and_requests(struct blk_mq_tag_set * set,unsigned int hctx_idx)2949 static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2950 unsigned int hctx_idx)
2951 {
2952 unsigned int flags = set->flags;
2953
2954 if (set->tags && set->tags[hctx_idx]) {
2955 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2956 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
2957 set->tags[hctx_idx] = NULL;
2958 }
2959 }
2960
blk_mq_map_swqueue(struct request_queue * q)2961 static void blk_mq_map_swqueue(struct request_queue *q)
2962 {
2963 unsigned int i, j, hctx_idx;
2964 struct blk_mq_hw_ctx *hctx;
2965 struct blk_mq_ctx *ctx;
2966 struct blk_mq_tag_set *set = q->tag_set;
2967
2968 queue_for_each_hw_ctx(q, hctx, i) {
2969 cpumask_clear(hctx->cpumask);
2970 hctx->nr_ctx = 0;
2971 hctx->dispatch_from = NULL;
2972 }
2973
2974 /*
2975 * Map software to hardware queues.
2976 *
2977 * If the cpu isn't present, the cpu is mapped to first hctx.
2978 */
2979 for_each_possible_cpu(i) {
2980
2981 ctx = per_cpu_ptr(q->queue_ctx, i);
2982 for (j = 0; j < set->nr_maps; j++) {
2983 if (!set->map[j].nr_queues) {
2984 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2985 HCTX_TYPE_DEFAULT, i);
2986 continue;
2987 }
2988 hctx_idx = set->map[j].mq_map[i];
2989 /* unmapped hw queue can be remapped after CPU topo changed */
2990 if (!set->tags[hctx_idx] &&
2991 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
2992 /*
2993 * If tags initialization fail for some hctx,
2994 * that hctx won't be brought online. In this
2995 * case, remap the current ctx to hctx[0] which
2996 * is guaranteed to always have tags allocated
2997 */
2998 set->map[j].mq_map[i] = 0;
2999 }
3000
3001 hctx = blk_mq_map_queue_type(q, j, i);
3002 ctx->hctxs[j] = hctx;
3003 /*
3004 * If the CPU is already set in the mask, then we've
3005 * mapped this one already. This can happen if
3006 * devices share queues across queue maps.
3007 */
3008 if (cpumask_test_cpu(i, hctx->cpumask))
3009 continue;
3010
3011 cpumask_set_cpu(i, hctx->cpumask);
3012 hctx->type = j;
3013 ctx->index_hw[hctx->type] = hctx->nr_ctx;
3014 hctx->ctxs[hctx->nr_ctx++] = ctx;
3015
3016 /*
3017 * If the nr_ctx type overflows, we have exceeded the
3018 * amount of sw queues we can support.
3019 */
3020 BUG_ON(!hctx->nr_ctx);
3021 }
3022
3023 for (; j < HCTX_MAX_TYPES; j++)
3024 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3025 HCTX_TYPE_DEFAULT, i);
3026 }
3027
3028 queue_for_each_hw_ctx(q, hctx, i) {
3029 /*
3030 * If no software queues are mapped to this hardware queue,
3031 * disable it and free the request entries.
3032 */
3033 if (!hctx->nr_ctx) {
3034 /* Never unmap queue 0. We need it as a
3035 * fallback in case of a new remap fails
3036 * allocation
3037 */
3038 if (i && set->tags[i])
3039 blk_mq_free_map_and_requests(set, i);
3040
3041 hctx->tags = NULL;
3042 continue;
3043 }
3044
3045 hctx->tags = set->tags[i];
3046 WARN_ON(!hctx->tags);
3047
3048 /*
3049 * Set the map size to the number of mapped software queues.
3050 * This is more accurate and more efficient than looping
3051 * over all possibly mapped software queues.
3052 */
3053 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
3054
3055 /*
3056 * Initialize batch roundrobin counts
3057 */
3058 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
3059 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3060 }
3061 }
3062
3063 /*
3064 * Caller needs to ensure that we're either frozen/quiesced, or that
3065 * the queue isn't live yet.
3066 */
queue_set_hctx_shared(struct request_queue * q,bool shared)3067 static void queue_set_hctx_shared(struct request_queue *q, bool shared)
3068 {
3069 struct blk_mq_hw_ctx *hctx;
3070 int i;
3071
3072 queue_for_each_hw_ctx(q, hctx, i) {
3073 if (shared)
3074 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
3075 else
3076 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
3077 }
3078 }
3079
blk_mq_update_tag_set_shared(struct blk_mq_tag_set * set,bool shared)3080 static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3081 bool shared)
3082 {
3083 struct request_queue *q;
3084
3085 lockdep_assert_held(&set->tag_list_lock);
3086
3087 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3088 blk_mq_freeze_queue(q);
3089 queue_set_hctx_shared(q, shared);
3090 blk_mq_unfreeze_queue(q);
3091 }
3092 }
3093
blk_mq_del_queue_tag_set(struct request_queue * q)3094 static void blk_mq_del_queue_tag_set(struct request_queue *q)
3095 {
3096 struct blk_mq_tag_set *set = q->tag_set;
3097
3098 mutex_lock(&set->tag_list_lock);
3099 list_del(&q->tag_set_list);
3100 if (list_is_singular(&set->tag_list)) {
3101 /* just transitioned to unshared */
3102 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
3103 /* update existing queue */
3104 blk_mq_update_tag_set_shared(set, false);
3105 }
3106 mutex_unlock(&set->tag_list_lock);
3107 INIT_LIST_HEAD(&q->tag_set_list);
3108 }
3109
blk_mq_add_queue_tag_set(struct blk_mq_tag_set * set,struct request_queue * q)3110 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3111 struct request_queue *q)
3112 {
3113 mutex_lock(&set->tag_list_lock);
3114
3115 /*
3116 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3117 */
3118 if (!list_empty(&set->tag_list) &&
3119 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3120 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
3121 /* update existing queue */
3122 blk_mq_update_tag_set_shared(set, true);
3123 }
3124 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
3125 queue_set_hctx_shared(q, true);
3126 list_add_tail(&q->tag_set_list, &set->tag_list);
3127
3128 mutex_unlock(&set->tag_list_lock);
3129 }
3130
3131 /* All allocations will be freed in release handler of q->mq_kobj */
blk_mq_alloc_ctxs(struct request_queue * q)3132 static int blk_mq_alloc_ctxs(struct request_queue *q)
3133 {
3134 struct blk_mq_ctxs *ctxs;
3135 int cpu;
3136
3137 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3138 if (!ctxs)
3139 return -ENOMEM;
3140
3141 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3142 if (!ctxs->queue_ctx)
3143 goto fail;
3144
3145 for_each_possible_cpu(cpu) {
3146 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3147 ctx->ctxs = ctxs;
3148 }
3149
3150 q->mq_kobj = &ctxs->kobj;
3151 q->queue_ctx = ctxs->queue_ctx;
3152
3153 return 0;
3154 fail:
3155 kfree(ctxs);
3156 return -ENOMEM;
3157 }
3158
3159 /*
3160 * It is the actual release handler for mq, but we do it from
3161 * request queue's release handler for avoiding use-after-free
3162 * and headache because q->mq_kobj shouldn't have been introduced,
3163 * but we can't group ctx/kctx kobj without it.
3164 */
blk_mq_release(struct request_queue * q)3165 void blk_mq_release(struct request_queue *q)
3166 {
3167 struct blk_mq_hw_ctx *hctx, *next;
3168 int i;
3169
3170 queue_for_each_hw_ctx(q, hctx, i)
3171 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3172
3173 /* all hctx are in .unused_hctx_list now */
3174 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3175 list_del_init(&hctx->hctx_list);
3176 kobject_put(&hctx->kobj);
3177 }
3178
3179 kfree(q->queue_hw_ctx);
3180
3181 /*
3182 * release .mq_kobj and sw queue's kobject now because
3183 * both share lifetime with request queue.
3184 */
3185 blk_mq_sysfs_deinit(q);
3186 }
3187
blk_mq_init_queue_data(struct blk_mq_tag_set * set,void * queuedata)3188 struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
3189 void *queuedata)
3190 {
3191 struct request_queue *uninit_q, *q;
3192
3193 uninit_q = blk_alloc_queue(set->numa_node);
3194 if (!uninit_q)
3195 return ERR_PTR(-ENOMEM);
3196 uninit_q->queuedata = queuedata;
3197
3198 /*
3199 * Initialize the queue without an elevator. device_add_disk() will do
3200 * the initialization.
3201 */
3202 q = blk_mq_init_allocated_queue(set, uninit_q, false);
3203 if (IS_ERR(q))
3204 blk_cleanup_queue(uninit_q);
3205
3206 return q;
3207 }
3208 EXPORT_SYMBOL_GPL(blk_mq_init_queue_data);
3209
blk_mq_init_queue(struct blk_mq_tag_set * set)3210 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3211 {
3212 return blk_mq_init_queue_data(set, NULL);
3213 }
3214 EXPORT_SYMBOL(blk_mq_init_queue);
3215
3216 /*
3217 * Helper for setting up a queue with mq ops, given queue depth, and
3218 * the passed in mq ops flags.
3219 */
blk_mq_init_sq_queue(struct blk_mq_tag_set * set,const struct blk_mq_ops * ops,unsigned int queue_depth,unsigned int set_flags)3220 struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
3221 const struct blk_mq_ops *ops,
3222 unsigned int queue_depth,
3223 unsigned int set_flags)
3224 {
3225 struct request_queue *q;
3226 int ret;
3227
3228 memset(set, 0, sizeof(*set));
3229 set->ops = ops;
3230 set->nr_hw_queues = 1;
3231 set->nr_maps = 1;
3232 set->queue_depth = queue_depth;
3233 set->numa_node = NUMA_NO_NODE;
3234 set->flags = set_flags;
3235
3236 ret = blk_mq_alloc_tag_set(set);
3237 if (ret)
3238 return ERR_PTR(ret);
3239
3240 q = blk_mq_init_queue(set);
3241 if (IS_ERR(q)) {
3242 blk_mq_free_tag_set(set);
3243 return q;
3244 }
3245
3246 return q;
3247 }
3248 EXPORT_SYMBOL(blk_mq_init_sq_queue);
3249
blk_mq_alloc_and_init_hctx(struct blk_mq_tag_set * set,struct request_queue * q,int hctx_idx,int node)3250 static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3251 struct blk_mq_tag_set *set, struct request_queue *q,
3252 int hctx_idx, int node)
3253 {
3254 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
3255
3256 /* reuse dead hctx first */
3257 spin_lock(&q->unused_hctx_lock);
3258 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3259 if (tmp->numa_node == node) {
3260 hctx = tmp;
3261 break;
3262 }
3263 }
3264 if (hctx)
3265 list_del_init(&hctx->hctx_list);
3266 spin_unlock(&q->unused_hctx_lock);
3267
3268 if (!hctx)
3269 hctx = blk_mq_alloc_hctx(q, set, node);
3270 if (!hctx)
3271 goto fail;
3272
3273 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3274 goto free_hctx;
3275
3276 return hctx;
3277
3278 free_hctx:
3279 kobject_put(&hctx->kobj);
3280 fail:
3281 return NULL;
3282 }
3283
blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set * set,struct request_queue * q)3284 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3285 struct request_queue *q)
3286 {
3287 int i, j, end;
3288 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
3289
3290 if (q->nr_hw_queues < set->nr_hw_queues) {
3291 struct blk_mq_hw_ctx **new_hctxs;
3292
3293 new_hctxs = kcalloc_node(set->nr_hw_queues,
3294 sizeof(*new_hctxs), GFP_KERNEL,
3295 set->numa_node);
3296 if (!new_hctxs)
3297 return;
3298 if (hctxs)
3299 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3300 sizeof(*hctxs));
3301 q->queue_hw_ctx = new_hctxs;
3302 kfree(hctxs);
3303 hctxs = new_hctxs;
3304 }
3305
3306 /* protect against switching io scheduler */
3307 mutex_lock(&q->sysfs_lock);
3308 for (i = 0; i < set->nr_hw_queues; i++) {
3309 int node;
3310 struct blk_mq_hw_ctx *hctx;
3311
3312 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
3313 /*
3314 * If the hw queue has been mapped to another numa node,
3315 * we need to realloc the hctx. If allocation fails, fallback
3316 * to use the previous one.
3317 */
3318 if (hctxs[i] && (hctxs[i]->numa_node == node))
3319 continue;
3320
3321 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3322 if (hctx) {
3323 if (hctxs[i])
3324 blk_mq_exit_hctx(q, set, hctxs[i], i);
3325 hctxs[i] = hctx;
3326 } else {
3327 if (hctxs[i])
3328 pr_warn("Allocate new hctx on node %d fails,\
3329 fallback to previous one on node %d\n",
3330 node, hctxs[i]->numa_node);
3331 else
3332 break;
3333 }
3334 }
3335 /*
3336 * Increasing nr_hw_queues fails. Free the newly allocated
3337 * hctxs and keep the previous q->nr_hw_queues.
3338 */
3339 if (i != set->nr_hw_queues) {
3340 j = q->nr_hw_queues;
3341 end = i;
3342 } else {
3343 j = i;
3344 end = q->nr_hw_queues;
3345 q->nr_hw_queues = set->nr_hw_queues;
3346 }
3347
3348 for (; j < end; j++) {
3349 struct blk_mq_hw_ctx *hctx = hctxs[j];
3350
3351 if (hctx) {
3352 if (hctx->tags)
3353 blk_mq_free_map_and_requests(set, j);
3354 blk_mq_exit_hctx(q, set, hctx, j);
3355 hctxs[j] = NULL;
3356 }
3357 }
3358 mutex_unlock(&q->sysfs_lock);
3359 }
3360
blk_mq_init_allocated_queue(struct blk_mq_tag_set * set,struct request_queue * q,bool elevator_init)3361 struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
3362 struct request_queue *q,
3363 bool elevator_init)
3364 {
3365 /* mark the queue as mq asap */
3366 q->mq_ops = set->ops;
3367
3368 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
3369 blk_mq_poll_stats_bkt,
3370 BLK_MQ_POLL_STATS_BKTS, q);
3371 if (!q->poll_cb)
3372 goto err_exit;
3373
3374 if (blk_mq_alloc_ctxs(q))
3375 goto err_poll;
3376
3377 /* init q->mq_kobj and sw queues' kobjects */
3378 blk_mq_sysfs_init(q);
3379
3380 INIT_LIST_HEAD(&q->unused_hctx_list);
3381 spin_lock_init(&q->unused_hctx_lock);
3382
3383 blk_mq_realloc_hw_ctxs(set, q);
3384 if (!q->nr_hw_queues)
3385 goto err_hctxs;
3386
3387 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
3388 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
3389
3390 q->tag_set = set;
3391
3392 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
3393 if (set->nr_maps > HCTX_TYPE_POLL &&
3394 set->map[HCTX_TYPE_POLL].nr_queues)
3395 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
3396
3397 q->sg_reserved_size = INT_MAX;
3398
3399 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
3400 INIT_LIST_HEAD(&q->requeue_list);
3401 spin_lock_init(&q->requeue_lock);
3402
3403 q->nr_requests = set->queue_depth;
3404
3405 /*
3406 * Default to classic polling
3407 */
3408 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
3409
3410 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
3411 blk_mq_add_queue_tag_set(set, q);
3412 blk_mq_map_swqueue(q);
3413
3414 trace_android_rvh_blk_mq_init_allocated_queue(q);
3415
3416 if (elevator_init)
3417 elevator_init_mq(q);
3418
3419 return q;
3420
3421 err_hctxs:
3422 kfree(q->queue_hw_ctx);
3423 q->nr_hw_queues = 0;
3424 blk_mq_sysfs_deinit(q);
3425 err_poll:
3426 blk_stat_free_callback(q->poll_cb);
3427 q->poll_cb = NULL;
3428 err_exit:
3429 q->mq_ops = NULL;
3430 return ERR_PTR(-ENOMEM);
3431 }
3432 EXPORT_SYMBOL(blk_mq_init_allocated_queue);
3433
3434 /* tags can _not_ be used after returning from blk_mq_exit_queue */
blk_mq_exit_queue(struct request_queue * q)3435 void blk_mq_exit_queue(struct request_queue *q)
3436 {
3437 struct blk_mq_tag_set *set = q->tag_set;
3438
3439 trace_android_vh_blk_mq_exit_queue(q);
3440 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
3441 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
3442 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3443 blk_mq_del_queue_tag_set(q);
3444 }
3445
__blk_mq_alloc_rq_maps(struct blk_mq_tag_set * set)3446 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3447 {
3448 int i;
3449
3450 for (i = 0; i < set->nr_hw_queues; i++) {
3451 if (!__blk_mq_alloc_map_and_request(set, i))
3452 goto out_unwind;
3453 cond_resched();
3454 }
3455
3456 return 0;
3457
3458 out_unwind:
3459 while (--i >= 0)
3460 blk_mq_free_map_and_requests(set, i);
3461
3462 return -ENOMEM;
3463 }
3464
3465 /*
3466 * Allocate the request maps associated with this tag_set. Note that this
3467 * may reduce the depth asked for, if memory is tight. set->queue_depth
3468 * will be updated to reflect the allocated depth.
3469 */
blk_mq_alloc_map_and_requests(struct blk_mq_tag_set * set)3470 static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
3471 {
3472 unsigned int depth;
3473 int err;
3474
3475 depth = set->queue_depth;
3476 do {
3477 err = __blk_mq_alloc_rq_maps(set);
3478 if (!err)
3479 break;
3480
3481 set->queue_depth >>= 1;
3482 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3483 err = -ENOMEM;
3484 break;
3485 }
3486 } while (set->queue_depth);
3487
3488 if (!set->queue_depth || err) {
3489 pr_err("blk-mq: failed to allocate request map\n");
3490 return -ENOMEM;
3491 }
3492
3493 if (depth != set->queue_depth)
3494 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3495 depth, set->queue_depth);
3496
3497 return 0;
3498 }
3499
blk_mq_update_queue_map(struct blk_mq_tag_set * set)3500 static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3501 {
3502 /*
3503 * blk_mq_map_queues() and multiple .map_queues() implementations
3504 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3505 * number of hardware queues.
3506 */
3507 if (set->nr_maps == 1)
3508 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3509
3510 if (set->ops->map_queues && !is_kdump_kernel()) {
3511 int i;
3512
3513 /*
3514 * transport .map_queues is usually done in the following
3515 * way:
3516 *
3517 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3518 * mask = get_cpu_mask(queue)
3519 * for_each_cpu(cpu, mask)
3520 * set->map[x].mq_map[cpu] = queue;
3521 * }
3522 *
3523 * When we need to remap, the table has to be cleared for
3524 * killing stale mapping since one CPU may not be mapped
3525 * to any hw queue.
3526 */
3527 for (i = 0; i < set->nr_maps; i++)
3528 blk_mq_clear_mq_map(&set->map[i]);
3529
3530 return set->ops->map_queues(set);
3531 } else {
3532 BUG_ON(set->nr_maps > 1);
3533 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
3534 }
3535 }
3536
blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set * set,int cur_nr_hw_queues,int new_nr_hw_queues)3537 static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3538 int cur_nr_hw_queues, int new_nr_hw_queues)
3539 {
3540 struct blk_mq_tags **new_tags;
3541
3542 if (cur_nr_hw_queues >= new_nr_hw_queues)
3543 return 0;
3544
3545 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3546 GFP_KERNEL, set->numa_node);
3547 if (!new_tags)
3548 return -ENOMEM;
3549
3550 if (set->tags)
3551 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3552 sizeof(*set->tags));
3553 kfree(set->tags);
3554 set->tags = new_tags;
3555 set->nr_hw_queues = new_nr_hw_queues;
3556
3557 return 0;
3558 }
3559
3560 /*
3561 * Alloc a tag set to be associated with one or more request queues.
3562 * May fail with EINVAL for various error conditions. May adjust the
3563 * requested depth down, if it's too large. In that case, the set
3564 * value will be stored in set->queue_depth.
3565 */
blk_mq_alloc_tag_set(struct blk_mq_tag_set * set)3566 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3567 {
3568 int i, ret;
3569
3570 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3571
3572 if (!set->nr_hw_queues)
3573 return -EINVAL;
3574 if (!set->queue_depth)
3575 return -EINVAL;
3576 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3577 return -EINVAL;
3578
3579 if (!set->ops->queue_rq)
3580 return -EINVAL;
3581
3582 if (!set->ops->get_budget ^ !set->ops->put_budget)
3583 return -EINVAL;
3584
3585 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3586 pr_info("blk-mq: reduced tag depth to %u\n",
3587 BLK_MQ_MAX_DEPTH);
3588 set->queue_depth = BLK_MQ_MAX_DEPTH;
3589 }
3590
3591 if (!set->nr_maps)
3592 set->nr_maps = 1;
3593 else if (set->nr_maps > HCTX_MAX_TYPES)
3594 return -EINVAL;
3595
3596 /*
3597 * If a crashdump is active, then we are potentially in a very
3598 * memory constrained environment. Limit us to 1 queue and
3599 * 64 tags to prevent using too much memory.
3600 */
3601 if (is_kdump_kernel()) {
3602 set->nr_hw_queues = 1;
3603 set->nr_maps = 1;
3604 set->queue_depth = min(64U, set->queue_depth);
3605 }
3606 /*
3607 * There is no use for more h/w queues than cpus if we just have
3608 * a single map
3609 */
3610 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
3611 set->nr_hw_queues = nr_cpu_ids;
3612
3613 if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0)
3614 return -ENOMEM;
3615
3616 ret = -ENOMEM;
3617 for (i = 0; i < set->nr_maps; i++) {
3618 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
3619 sizeof(set->map[i].mq_map[0]),
3620 GFP_KERNEL, set->numa_node);
3621 if (!set->map[i].mq_map)
3622 goto out_free_mq_map;
3623 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
3624 }
3625
3626 ret = blk_mq_update_queue_map(set);
3627 if (ret)
3628 goto out_free_mq_map;
3629
3630 trace_android_vh_blk_mq_alloc_tag_set(set);
3631
3632 ret = blk_mq_alloc_map_and_requests(set);
3633 if (ret)
3634 goto out_free_mq_map;
3635
3636 if (blk_mq_is_sbitmap_shared(set->flags)) {
3637 atomic_set(&set->active_queues_shared_sbitmap, 0);
3638
3639 if (blk_mq_init_shared_sbitmap(set, set->flags)) {
3640 ret = -ENOMEM;
3641 goto out_free_mq_rq_maps;
3642 }
3643 }
3644
3645 mutex_init(&set->tag_list_lock);
3646 INIT_LIST_HEAD(&set->tag_list);
3647
3648 return 0;
3649
3650 out_free_mq_rq_maps:
3651 for (i = 0; i < set->nr_hw_queues; i++)
3652 blk_mq_free_map_and_requests(set, i);
3653 out_free_mq_map:
3654 for (i = 0; i < set->nr_maps; i++) {
3655 kfree(set->map[i].mq_map);
3656 set->map[i].mq_map = NULL;
3657 }
3658 kfree(set->tags);
3659 set->tags = NULL;
3660 return ret;
3661 }
3662 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3663
blk_mq_free_tag_set(struct blk_mq_tag_set * set)3664 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3665 {
3666 int i, j;
3667
3668 for (i = 0; i < set->nr_hw_queues; i++)
3669 blk_mq_free_map_and_requests(set, i);
3670
3671 if (blk_mq_is_sbitmap_shared(set->flags))
3672 blk_mq_exit_shared_sbitmap(set);
3673
3674 for (j = 0; j < set->nr_maps; j++) {
3675 kfree(set->map[j].mq_map);
3676 set->map[j].mq_map = NULL;
3677 }
3678
3679 kfree(set->tags);
3680 set->tags = NULL;
3681 }
3682 EXPORT_SYMBOL(blk_mq_free_tag_set);
3683
blk_mq_update_nr_requests(struct request_queue * q,unsigned int nr)3684 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3685 {
3686 struct blk_mq_tag_set *set = q->tag_set;
3687 struct blk_mq_hw_ctx *hctx;
3688 int i, ret;
3689
3690 if (!set)
3691 return -EINVAL;
3692
3693 if (q->nr_requests == nr)
3694 return 0;
3695
3696 blk_mq_freeze_queue(q);
3697 blk_mq_quiesce_queue(q);
3698
3699 ret = 0;
3700 queue_for_each_hw_ctx(q, hctx, i) {
3701 if (!hctx->tags)
3702 continue;
3703 /*
3704 * If we're using an MQ scheduler, just update the scheduler
3705 * queue depth. This is similar to what the old code would do.
3706 */
3707 if (!hctx->sched_tags) {
3708 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
3709 false);
3710 if (!ret && blk_mq_is_sbitmap_shared(set->flags))
3711 blk_mq_tag_resize_shared_sbitmap(set, nr);
3712 } else {
3713 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3714 nr, true);
3715 }
3716 if (ret)
3717 break;
3718 if (q->elevator && q->elevator->type->ops.depth_updated)
3719 q->elevator->type->ops.depth_updated(hctx);
3720 }
3721
3722 if (!ret)
3723 q->nr_requests = nr;
3724
3725 blk_mq_unquiesce_queue(q);
3726 blk_mq_unfreeze_queue(q);
3727
3728 return ret;
3729 }
3730
3731 /*
3732 * request_queue and elevator_type pair.
3733 * It is just used by __blk_mq_update_nr_hw_queues to cache
3734 * the elevator_type associated with a request_queue.
3735 */
3736 struct blk_mq_qe_pair {
3737 struct list_head node;
3738 struct request_queue *q;
3739 struct elevator_type *type;
3740 };
3741
3742 /*
3743 * Cache the elevator_type in qe pair list and switch the
3744 * io scheduler to 'none'
3745 */
blk_mq_elv_switch_none(struct list_head * head,struct request_queue * q)3746 static bool blk_mq_elv_switch_none(struct list_head *head,
3747 struct request_queue *q)
3748 {
3749 struct blk_mq_qe_pair *qe;
3750
3751 if (!q->elevator)
3752 return true;
3753
3754 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3755 if (!qe)
3756 return false;
3757
3758 INIT_LIST_HEAD(&qe->node);
3759 qe->q = q;
3760 qe->type = q->elevator->type;
3761 list_add(&qe->node, head);
3762
3763 mutex_lock(&q->sysfs_lock);
3764 /*
3765 * After elevator_switch_mq, the previous elevator_queue will be
3766 * released by elevator_release. The reference of the io scheduler
3767 * module get by elevator_get will also be put. So we need to get
3768 * a reference of the io scheduler module here to prevent it to be
3769 * removed.
3770 */
3771 __module_get(qe->type->elevator_owner);
3772 elevator_switch_mq(q, NULL);
3773 mutex_unlock(&q->sysfs_lock);
3774
3775 return true;
3776 }
3777
blk_mq_elv_switch_back(struct list_head * head,struct request_queue * q)3778 static void blk_mq_elv_switch_back(struct list_head *head,
3779 struct request_queue *q)
3780 {
3781 struct blk_mq_qe_pair *qe;
3782 struct elevator_type *t = NULL;
3783
3784 list_for_each_entry(qe, head, node)
3785 if (qe->q == q) {
3786 t = qe->type;
3787 break;
3788 }
3789
3790 if (!t)
3791 return;
3792
3793 list_del(&qe->node);
3794 kfree(qe);
3795
3796 mutex_lock(&q->sysfs_lock);
3797 elevator_switch_mq(q, t);
3798 mutex_unlock(&q->sysfs_lock);
3799 }
3800
__blk_mq_update_nr_hw_queues(struct blk_mq_tag_set * set,int nr_hw_queues)3801 static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3802 int nr_hw_queues)
3803 {
3804 struct request_queue *q;
3805 LIST_HEAD(head);
3806 int prev_nr_hw_queues;
3807
3808 lockdep_assert_held(&set->tag_list_lock);
3809
3810 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
3811 nr_hw_queues = nr_cpu_ids;
3812 if (nr_hw_queues < 1)
3813 return;
3814 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
3815 return;
3816
3817 list_for_each_entry(q, &set->tag_list, tag_set_list)
3818 blk_mq_freeze_queue(q);
3819 /*
3820 * Switch IO scheduler to 'none', cleaning up the data associated
3821 * with the previous scheduler. We will switch back once we are done
3822 * updating the new sw to hw queue mappings.
3823 */
3824 list_for_each_entry(q, &set->tag_list, tag_set_list)
3825 if (!blk_mq_elv_switch_none(&head, q))
3826 goto switch_back;
3827
3828 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3829 blk_mq_debugfs_unregister_hctxs(q);
3830 blk_mq_sysfs_unregister(q);
3831 }
3832
3833 prev_nr_hw_queues = set->nr_hw_queues;
3834 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3835 0)
3836 goto reregister;
3837
3838 set->nr_hw_queues = nr_hw_queues;
3839 fallback:
3840 blk_mq_update_queue_map(set);
3841 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3842 blk_mq_realloc_hw_ctxs(set, q);
3843 if (q->nr_hw_queues != set->nr_hw_queues) {
3844 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3845 nr_hw_queues, prev_nr_hw_queues);
3846 set->nr_hw_queues = prev_nr_hw_queues;
3847 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
3848 goto fallback;
3849 }
3850 blk_mq_map_swqueue(q);
3851 }
3852
3853 reregister:
3854 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3855 blk_mq_sysfs_register(q);
3856 blk_mq_debugfs_register_hctxs(q);
3857 }
3858
3859 switch_back:
3860 list_for_each_entry(q, &set->tag_list, tag_set_list)
3861 blk_mq_elv_switch_back(&head, q);
3862
3863 list_for_each_entry(q, &set->tag_list, tag_set_list)
3864 blk_mq_unfreeze_queue(q);
3865 }
3866
blk_mq_update_nr_hw_queues(struct blk_mq_tag_set * set,int nr_hw_queues)3867 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3868 {
3869 mutex_lock(&set->tag_list_lock);
3870 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3871 mutex_unlock(&set->tag_list_lock);
3872 }
3873 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3874
3875 /* Enable polling stats and return whether they were already enabled. */
blk_poll_stats_enable(struct request_queue * q)3876 static bool blk_poll_stats_enable(struct request_queue *q)
3877 {
3878 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3879 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
3880 return true;
3881 blk_stat_add_callback(q, q->poll_cb);
3882 return false;
3883 }
3884
blk_mq_poll_stats_start(struct request_queue * q)3885 static void blk_mq_poll_stats_start(struct request_queue *q)
3886 {
3887 /*
3888 * We don't arm the callback if polling stats are not enabled or the
3889 * callback is already active.
3890 */
3891 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3892 blk_stat_is_active(q->poll_cb))
3893 return;
3894
3895 blk_stat_activate_msecs(q->poll_cb, 100);
3896 }
3897
blk_mq_poll_stats_fn(struct blk_stat_callback * cb)3898 static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3899 {
3900 struct request_queue *q = cb->data;
3901 int bucket;
3902
3903 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3904 if (cb->stat[bucket].nr_samples)
3905 q->poll_stat[bucket] = cb->stat[bucket];
3906 }
3907 }
3908
blk_mq_poll_nsecs(struct request_queue * q,struct request * rq)3909 static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
3910 struct request *rq)
3911 {
3912 unsigned long ret = 0;
3913 int bucket;
3914
3915 /*
3916 * If stats collection isn't on, don't sleep but turn it on for
3917 * future users
3918 */
3919 if (!blk_poll_stats_enable(q))
3920 return 0;
3921
3922 /*
3923 * As an optimistic guess, use half of the mean service time
3924 * for this type of request. We can (and should) make this smarter.
3925 * For instance, if the completion latencies are tight, we can
3926 * get closer than just half the mean. This is especially
3927 * important on devices where the completion latencies are longer
3928 * than ~10 usec. We do use the stats for the relevant IO size
3929 * if available which does lead to better estimates.
3930 */
3931 bucket = blk_mq_poll_stats_bkt(rq);
3932 if (bucket < 0)
3933 return ret;
3934
3935 if (q->poll_stat[bucket].nr_samples)
3936 ret = (q->poll_stat[bucket].mean + 1) / 2;
3937
3938 return ret;
3939 }
3940
blk_mq_poll_hybrid_sleep(struct request_queue * q,struct request * rq)3941 static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3942 struct request *rq)
3943 {
3944 struct hrtimer_sleeper hs;
3945 enum hrtimer_mode mode;
3946 unsigned int nsecs;
3947 ktime_t kt;
3948
3949 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
3950 return false;
3951
3952 /*
3953 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
3954 *
3955 * 0: use half of prev avg
3956 * >0: use this specific value
3957 */
3958 if (q->poll_nsec > 0)
3959 nsecs = q->poll_nsec;
3960 else
3961 nsecs = blk_mq_poll_nsecs(q, rq);
3962
3963 if (!nsecs)
3964 return false;
3965
3966 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
3967
3968 /*
3969 * This will be replaced with the stats tracking code, using
3970 * 'avg_completion_time / 2' as the pre-sleep target.
3971 */
3972 kt = nsecs;
3973
3974 mode = HRTIMER_MODE_REL;
3975 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
3976 hrtimer_set_expires(&hs.timer, kt);
3977
3978 do {
3979 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
3980 break;
3981 set_current_state(TASK_UNINTERRUPTIBLE);
3982 hrtimer_sleeper_start_expires(&hs, mode);
3983 if (hs.task)
3984 io_schedule();
3985 hrtimer_cancel(&hs.timer);
3986 mode = HRTIMER_MODE_ABS;
3987 } while (hs.task && !signal_pending(current));
3988
3989 __set_current_state(TASK_RUNNING);
3990 destroy_hrtimer_on_stack(&hs.timer);
3991 return true;
3992 }
3993
blk_mq_poll_hybrid(struct request_queue * q,struct blk_mq_hw_ctx * hctx,blk_qc_t cookie)3994 static bool blk_mq_poll_hybrid(struct request_queue *q,
3995 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
3996 {
3997 struct request *rq;
3998
3999 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
4000 return false;
4001
4002 if (!blk_qc_t_is_internal(cookie))
4003 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
4004 else {
4005 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
4006 /*
4007 * With scheduling, if the request has completed, we'll
4008 * get a NULL return here, as we clear the sched tag when
4009 * that happens. The request still remains valid, like always,
4010 * so we should be safe with just the NULL check.
4011 */
4012 if (!rq)
4013 return false;
4014 }
4015
4016 return blk_mq_poll_hybrid_sleep(q, rq);
4017 }
4018
4019 /**
4020 * blk_poll - poll for IO completions
4021 * @q: the queue
4022 * @cookie: cookie passed back at IO submission time
4023 * @spin: whether to spin for completions
4024 *
4025 * Description:
4026 * Poll for completions on the passed in queue. Returns number of
4027 * completed entries found. If @spin is true, then blk_poll will continue
4028 * looping until at least one completion is found, unless the task is
4029 * otherwise marked running (or we need to reschedule).
4030 */
blk_poll(struct request_queue * q,blk_qc_t cookie,bool spin)4031 int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
4032 {
4033 struct blk_mq_hw_ctx *hctx;
4034 long state;
4035
4036 if (!blk_qc_t_valid(cookie) ||
4037 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
4038 return 0;
4039
4040 if (current->plug)
4041 blk_flush_plug_list(current->plug, false);
4042
4043 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
4044
4045 /*
4046 * If we sleep, have the caller restart the poll loop to reset
4047 * the state. Like for the other success return cases, the
4048 * caller is responsible for checking if the IO completed. If
4049 * the IO isn't complete, we'll get called again and will go
4050 * straight to the busy poll loop.
4051 */
4052 if (blk_mq_poll_hybrid(q, hctx, cookie))
4053 return 1;
4054
4055 hctx->poll_considered++;
4056
4057 state = current->state;
4058 do {
4059 int ret;
4060
4061 hctx->poll_invoked++;
4062
4063 ret = q->mq_ops->poll(hctx);
4064 if (ret > 0) {
4065 hctx->poll_success++;
4066 __set_current_state(TASK_RUNNING);
4067 return ret;
4068 }
4069
4070 if (signal_pending_state(state, current))
4071 __set_current_state(TASK_RUNNING);
4072
4073 if (current->state == TASK_RUNNING)
4074 return 1;
4075 if (ret < 0 || !spin)
4076 break;
4077 cpu_relax();
4078 } while (!need_resched());
4079
4080 __set_current_state(TASK_RUNNING);
4081 return 0;
4082 }
4083 EXPORT_SYMBOL_GPL(blk_poll);
4084
blk_mq_rq_cpu(struct request * rq)4085 unsigned int blk_mq_rq_cpu(struct request *rq)
4086 {
4087 return rq->mq_ctx->cpu;
4088 }
4089 EXPORT_SYMBOL(blk_mq_rq_cpu);
4090
blk_mq_init(void)4091 static int __init blk_mq_init(void)
4092 {
4093 int i;
4094
4095 for_each_possible_cpu(i)
4096 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
4097 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4098
4099 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4100 "block/softirq:dead", NULL,
4101 blk_softirq_cpu_dead);
4102 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4103 blk_mq_hctx_notify_dead);
4104 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4105 blk_mq_hctx_notify_online,
4106 blk_mq_hctx_notify_offline);
4107 return 0;
4108 }
4109 subsys_initcall(blk_mq_init);
4110