LCOV - code coverage report
Current view: top level - block - blk-mq.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 580 0.0 %
Date: 2014-02-18 Functions: 0 53 0.0 %
Branches: 0 464 0.0 %

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

Generated by: LCOV version 1.9