]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - block/mq-deadline.c
Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[mirror_ubuntu-jammy-kernel.git] / block / mq-deadline.c
index 0179e484ec98c432ec2628ba0b25695ac3f3c865..c56f211c84400662f3e18c137b51c3f5406e20fd 100644 (file)
@@ -59,6 +59,7 @@ struct deadline_data {
        int front_merges;
 
        spinlock_t lock;
+       spinlock_t zone_lock;
        struct list_head dispatch;
 };
 
@@ -191,14 +192,84 @@ static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
        return 0;
 }
 
+/*
+ * For the specified data direction, return the next request to
+ * dispatch using arrival ordered lists.
+ */
+static struct request *
+deadline_fifo_request(struct deadline_data *dd, int data_dir)
+{
+       struct request *rq;
+       unsigned long flags;
+
+       if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
+               return NULL;
+
+       if (list_empty(&dd->fifo_list[data_dir]))
+               return NULL;
+
+       rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
+       if (data_dir == READ || !blk_queue_is_zoned(rq->q))
+               return rq;
+
+       /*
+        * Look for a write request that can be dispatched, that is one with
+        * an unlocked target zone.
+        */
+       spin_lock_irqsave(&dd->zone_lock, flags);
+       list_for_each_entry(rq, &dd->fifo_list[WRITE], queuelist) {
+               if (blk_req_can_dispatch_to_zone(rq))
+                       goto out;
+       }
+       rq = NULL;
+out:
+       spin_unlock_irqrestore(&dd->zone_lock, flags);
+
+       return rq;
+}
+
+/*
+ * For the specified data direction, return the next request to
+ * dispatch using sector position sorted lists.
+ */
+static struct request *
+deadline_next_request(struct deadline_data *dd, int data_dir)
+{
+       struct request *rq;
+       unsigned long flags;
+
+       if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
+               return NULL;
+
+       rq = dd->next_rq[data_dir];
+       if (!rq)
+               return NULL;
+
+       if (data_dir == READ || !blk_queue_is_zoned(rq->q))
+               return rq;
+
+       /*
+        * Look for a write request that can be dispatched, that is one with
+        * an unlocked target zone.
+        */
+       spin_lock_irqsave(&dd->zone_lock, flags);
+       while (rq) {
+               if (blk_req_can_dispatch_to_zone(rq))
+                       break;
+               rq = deadline_latter_request(rq);
+       }
+       spin_unlock_irqrestore(&dd->zone_lock, flags);
+
+       return rq;
+}
+
 /*
  * deadline_dispatch_requests selects the best request according to
  * read/write expire, fifo_batch, etc
  */
-static struct request *__dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
+static struct request *__dd_dispatch_request(struct deadline_data *dd)
 {
-       struct deadline_data *dd = hctx->queue->elevator->elevator_data;
-       struct request *rq;
+       struct request *rq, *next_rq;
        bool reads, writes;
        int data_dir;
 
@@ -214,10 +285,9 @@ static struct request *__dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
        /*
         * batches are currently reads XOR writes
         */
-       if (dd->next_rq[WRITE])
-               rq = dd->next_rq[WRITE];
-       else
-               rq = dd->next_rq[READ];
+       rq = deadline_next_request(dd, WRITE);
+       if (!rq)
+               rq = deadline_next_request(dd, READ);
 
        if (rq && dd->batching < dd->fifo_batch)
                /* we have a next request are still entitled to batch */
@@ -231,7 +301,8 @@ static struct request *__dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
        if (reads) {
                BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
 
-               if (writes && (dd->starved++ >= dd->writes_starved))
+               if (deadline_fifo_request(dd, WRITE) &&
+                   (dd->starved++ >= dd->writes_starved))
                        goto dispatch_writes;
 
                data_dir = READ;
@@ -260,21 +331,29 @@ dispatch_find_request:
        /*
         * we are not running a batch, find best request for selected data_dir
         */
-       if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir]) {
+       next_rq = deadline_next_request(dd, data_dir);
+       if (deadline_check_fifo(dd, data_dir) || !next_rq) {
                /*
                 * A deadline has expired, the last request was in the other
                 * direction, or we have run out of higher-sectored requests.
                 * Start again from the request with the earliest expiry time.
                 */
-               rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
+               rq = deadline_fifo_request(dd, data_dir);
        } else {
                /*
                 * The last req was the same dir and we have a next request in
                 * sort order. No expired requests so continue on from here.
                 */
-               rq = dd->next_rq[data_dir];
+               rq = next_rq;
        }
 
+       /*
+        * For a zoned block device, if we only have writes queued and none of
+        * them can be dispatched, rq will be NULL.
+        */
+       if (!rq)
+               return NULL;
+
        dd->batching = 0;
 
 dispatch_request:
@@ -284,17 +363,27 @@ dispatch_request:
        dd->batching++;
        deadline_move_request(dd, rq);
 done:
+       /*
+        * If the request needs its target zone locked, do it.
+        */
+       blk_req_zone_write_lock(rq);
        rq->rq_flags |= RQF_STARTED;
        return rq;
 }
 
+/*
+ * One confusing aspect here is that we get called for a specific
+ * hardware queue, but we return a request that may not be for a
+ * different hardware queue. This is because mq-deadline has shared
+ * state for all hardware queues, in terms of sorting, FIFOs, etc.
+ */
 static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
 {
        struct deadline_data *dd = hctx->queue->elevator->elevator_data;
        struct request *rq;
 
        spin_lock(&dd->lock);
-       rq = __dd_dispatch_request(hctx);
+       rq = __dd_dispatch_request(dd);
        spin_unlock(&dd->lock);
 
        return rq;
@@ -339,6 +428,7 @@ static int dd_init_queue(struct request_queue *q, struct elevator_type *e)
        dd->front_merges = 1;
        dd->fifo_batch = fifo_batch;
        spin_lock_init(&dd->lock);
+       spin_lock_init(&dd->zone_lock);
        INIT_LIST_HEAD(&dd->dispatch);
 
        q->elevator = eq;
@@ -395,6 +485,12 @@ static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
        struct deadline_data *dd = q->elevator->elevator_data;
        const int data_dir = rq_data_dir(rq);
 
+       /*
+        * This may be a requeue of a write request that has locked its
+        * target zone. If it is the case, this releases the zone lock.
+        */
+       blk_req_zone_write_unlock(rq);
+
        if (blk_mq_sched_try_insert_merge(q, rq))
                return;
 
@@ -439,6 +535,26 @@ static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
        spin_unlock(&dd->lock);
 }
 
+/*
+ * For zoned block devices, write unlock the target zone of
+ * completed write requests. Do this while holding the zone lock
+ * spinlock so that the zone is never unlocked while deadline_fifo_request()
+ * while deadline_next_request() are executing.
+ */
+static void dd_completed_request(struct request *rq)
+{
+       struct request_queue *q = rq->q;
+
+       if (blk_queue_is_zoned(q)) {
+               struct deadline_data *dd = q->elevator->elevator_data;
+               unsigned long flags;
+
+               spin_lock_irqsave(&dd->zone_lock, flags);
+               blk_req_zone_write_unlock(rq);
+               spin_unlock_irqrestore(&dd->zone_lock, flags);
+       }
+}
+
 static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
 {
        struct deadline_data *dd = hctx->queue->elevator->elevator_data;
@@ -640,6 +756,7 @@ static struct elevator_type mq_deadline = {
        .ops.mq = {
                .insert_requests        = dd_insert_requests,
                .dispatch_request       = dd_dispatch_request,
+               .completed_request      = dd_completed_request,
                .next_request           = elv_rb_latter_request,
                .former_request         = elv_rb_former_request,
                .bio_merge              = dd_bio_merge,