Back to home page

LXR

 
 

    


0001 /*
0002  * Functions to sequence FLUSH and FUA writes.
0003  *
0004  * Copyright (C) 2011       Max Planck Institute for Gravitational Physics
0005  * Copyright (C) 2011       Tejun Heo <tj@kernel.org>
0006  *
0007  * This file is released under the GPLv2.
0008  *
0009  * REQ_{FLUSH|FUA} requests are decomposed to sequences consisted of three
0010  * optional steps - PREFLUSH, DATA and POSTFLUSH - according to the request
0011  * properties and hardware capability.
0012  *
0013  * If a request doesn't have data, only REQ_PREFLUSH makes sense, which
0014  * indicates a simple flush request.  If there is data, REQ_PREFLUSH indicates
0015  * that the device cache should be flushed before the data is executed, and
0016  * REQ_FUA means that the data must be on non-volatile media on request
0017  * completion.
0018  *
0019  * If the device doesn't have writeback cache, FLUSH and FUA don't make any
0020  * difference.  The requests are either completed immediately if there's no
0021  * data or executed as normal requests otherwise.
0022  *
0023  * If the device has writeback cache and supports FUA, REQ_PREFLUSH is
0024  * translated to PREFLUSH but REQ_FUA is passed down directly with DATA.
0025  *
0026  * If the device has writeback cache and doesn't support FUA, REQ_PREFLUSH
0027  * is translated to PREFLUSH and REQ_FUA to POSTFLUSH.
0028  *
0029  * The actual execution of flush is double buffered.  Whenever a request
0030  * needs to execute PRE or POSTFLUSH, it queues at
0031  * fq->flush_queue[fq->flush_pending_idx].  Once certain criteria are met, a
0032  * REQ_OP_FLUSH is issued and the pending_idx is toggled.  When the flush
0033  * completes, all the requests which were pending are proceeded to the next
0034  * step.  This allows arbitrary merging of different types of FLUSH/FUA
0035  * requests.
0036  *
0037  * Currently, the following conditions are used to determine when to issue
0038  * flush.
0039  *
0040  * C1. At any given time, only one flush shall be in progress.  This makes
0041  *     double buffering sufficient.
0042  *
0043  * C2. Flush is deferred if any request is executing DATA of its sequence.
0044  *     This avoids issuing separate POSTFLUSHes for requests which shared
0045  *     PREFLUSH.
0046  *
0047  * C3. The second condition is ignored if there is a request which has
0048  *     waited longer than FLUSH_PENDING_TIMEOUT.  This is to avoid
0049  *     starvation in the unlikely case where there are continuous stream of
0050  *     FUA (without FLUSH) requests.
0051  *
0052  * For devices which support FUA, it isn't clear whether C2 (and thus C3)
0053  * is beneficial.
0054  *
0055  * Note that a sequenced FLUSH/FUA request with DATA is completed twice.
0056  * Once while executing DATA and again after the whole sequence is
0057  * complete.  The first completion updates the contained bio but doesn't
0058  * finish it so that the bio submitter is notified only after the whole
0059  * sequence is complete.  This is implemented by testing RQF_FLUSH_SEQ in
0060  * req_bio_endio().
0061  *
0062  * The above peculiarity requires that each FLUSH/FUA request has only one
0063  * bio attached to it, which is guaranteed as they aren't allowed to be
0064  * merged in the usual way.
0065  */
0066 
0067 #include <linux/kernel.h>
0068 #include <linux/module.h>
0069 #include <linux/bio.h>
0070 #include <linux/blkdev.h>
0071 #include <linux/gfp.h>
0072 #include <linux/blk-mq.h>
0073 
0074 #include "blk.h"
0075 #include "blk-mq.h"
0076 #include "blk-mq-tag.h"
0077 
0078 /* FLUSH/FUA sequences */
0079 enum {
0080     REQ_FSEQ_PREFLUSH   = (1 << 0), /* pre-flushing in progress */
0081     REQ_FSEQ_DATA       = (1 << 1), /* data write in progress */
0082     REQ_FSEQ_POSTFLUSH  = (1 << 2), /* post-flushing in progress */
0083     REQ_FSEQ_DONE       = (1 << 3),
0084 
0085     REQ_FSEQ_ACTIONS    = REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
0086                   REQ_FSEQ_POSTFLUSH,
0087 
0088     /*
0089      * If flush has been pending longer than the following timeout,
0090      * it's issued even if flush_data requests are still in flight.
0091      */
0092     FLUSH_PENDING_TIMEOUT   = 5 * HZ,
0093 };
0094 
0095 static bool blk_kick_flush(struct request_queue *q,
0096                struct blk_flush_queue *fq);
0097 
0098 static unsigned int blk_flush_policy(unsigned long fflags, struct request *rq)
0099 {
0100     unsigned int policy = 0;
0101 
0102     if (blk_rq_sectors(rq))
0103         policy |= REQ_FSEQ_DATA;
0104 
0105     if (fflags & (1UL << QUEUE_FLAG_WC)) {
0106         if (rq->cmd_flags & REQ_PREFLUSH)
0107             policy |= REQ_FSEQ_PREFLUSH;
0108         if (!(fflags & (1UL << QUEUE_FLAG_FUA)) &&
0109             (rq->cmd_flags & REQ_FUA))
0110             policy |= REQ_FSEQ_POSTFLUSH;
0111     }
0112     return policy;
0113 }
0114 
0115 static unsigned int blk_flush_cur_seq(struct request *rq)
0116 {
0117     return 1 << ffz(rq->flush.seq);
0118 }
0119 
0120 static void blk_flush_restore_request(struct request *rq)
0121 {
0122     /*
0123      * After flush data completion, @rq->bio is %NULL but we need to
0124      * complete the bio again.  @rq->biotail is guaranteed to equal the
0125      * original @rq->bio.  Restore it.
0126      */
0127     rq->bio = rq->biotail;
0128 
0129     /* make @rq a normal request */
0130     rq->rq_flags &= ~RQF_FLUSH_SEQ;
0131     rq->end_io = rq->flush.saved_end_io;
0132 }
0133 
0134 static bool blk_flush_queue_rq(struct request *rq, bool add_front)
0135 {
0136     if (rq->q->mq_ops) {
0137         blk_mq_add_to_requeue_list(rq, add_front, true);
0138         return false;
0139     } else {
0140         if (add_front)
0141             list_add(&rq->queuelist, &rq->q->queue_head);
0142         else
0143             list_add_tail(&rq->queuelist, &rq->q->queue_head);
0144         return true;
0145     }
0146 }
0147 
0148 /**
0149  * blk_flush_complete_seq - complete flush sequence
0150  * @rq: FLUSH/FUA request being sequenced
0151  * @fq: flush queue
0152  * @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
0153  * @error: whether an error occurred
0154  *
0155  * @rq just completed @seq part of its flush sequence, record the
0156  * completion and trigger the next step.
0157  *
0158  * CONTEXT:
0159  * spin_lock_irq(q->queue_lock or fq->mq_flush_lock)
0160  *
0161  * RETURNS:
0162  * %true if requests were added to the dispatch queue, %false otherwise.
0163  */
0164 static bool blk_flush_complete_seq(struct request *rq,
0165                    struct blk_flush_queue *fq,
0166                    unsigned int seq, int error)
0167 {
0168     struct request_queue *q = rq->q;
0169     struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
0170     bool queued = false, kicked;
0171 
0172     BUG_ON(rq->flush.seq & seq);
0173     rq->flush.seq |= seq;
0174 
0175     if (likely(!error))
0176         seq = blk_flush_cur_seq(rq);
0177     else
0178         seq = REQ_FSEQ_DONE;
0179 
0180     switch (seq) {
0181     case REQ_FSEQ_PREFLUSH:
0182     case REQ_FSEQ_POSTFLUSH:
0183         /* queue for flush */
0184         if (list_empty(pending))
0185             fq->flush_pending_since = jiffies;
0186         list_move_tail(&rq->flush.list, pending);
0187         break;
0188 
0189     case REQ_FSEQ_DATA:
0190         list_move_tail(&rq->flush.list, &fq->flush_data_in_flight);
0191         queued = blk_flush_queue_rq(rq, true);
0192         break;
0193 
0194     case REQ_FSEQ_DONE:
0195         /*
0196          * @rq was previously adjusted by blk_flush_issue() for
0197          * flush sequencing and may already have gone through the
0198          * flush data request completion path.  Restore @rq for
0199          * normal completion and end it.
0200          */
0201         BUG_ON(!list_empty(&rq->queuelist));
0202         list_del_init(&rq->flush.list);
0203         blk_flush_restore_request(rq);
0204         if (q->mq_ops)
0205             blk_mq_end_request(rq, error);
0206         else
0207             __blk_end_request_all(rq, error);
0208         break;
0209 
0210     default:
0211         BUG();
0212     }
0213 
0214     kicked = blk_kick_flush(q, fq);
0215     return kicked | queued;
0216 }
0217 
0218 static void flush_end_io(struct request *flush_rq, int error)
0219 {
0220     struct request_queue *q = flush_rq->q;
0221     struct list_head *running;
0222     bool queued = false;
0223     struct request *rq, *n;
0224     unsigned long flags = 0;
0225     struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx);
0226 
0227     if (q->mq_ops) {
0228         struct blk_mq_hw_ctx *hctx;
0229 
0230         /* release the tag's ownership to the req cloned from */
0231         spin_lock_irqsave(&fq->mq_flush_lock, flags);
0232         hctx = blk_mq_map_queue(q, flush_rq->mq_ctx->cpu);
0233         blk_mq_tag_set_rq(hctx, flush_rq->tag, fq->orig_rq);
0234         flush_rq->tag = -1;
0235     }
0236 
0237     running = &fq->flush_queue[fq->flush_running_idx];
0238     BUG_ON(fq->flush_pending_idx == fq->flush_running_idx);
0239 
0240     /* account completion of the flush request */
0241     fq->flush_running_idx ^= 1;
0242 
0243     if (!q->mq_ops)
0244         elv_completed_request(q, flush_rq);
0245 
0246     /* and push the waiting requests to the next stage */
0247     list_for_each_entry_safe(rq, n, running, flush.list) {
0248         unsigned int seq = blk_flush_cur_seq(rq);
0249 
0250         BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
0251         queued |= blk_flush_complete_seq(rq, fq, seq, error);
0252     }
0253 
0254     /*
0255      * Kick the queue to avoid stall for two cases:
0256      * 1. Moving a request silently to empty queue_head may stall the
0257      * queue.
0258      * 2. When flush request is running in non-queueable queue, the
0259      * queue is hold. Restart the queue after flush request is finished
0260      * to avoid stall.
0261      * This function is called from request completion path and calling
0262      * directly into request_fn may confuse the driver.  Always use
0263      * kblockd.
0264      */
0265     if (queued || fq->flush_queue_delayed) {
0266         WARN_ON(q->mq_ops);
0267         blk_run_queue_async(q);
0268     }
0269     fq->flush_queue_delayed = 0;
0270     if (q->mq_ops)
0271         spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
0272 }
0273 
0274 /**
0275  * blk_kick_flush - consider issuing flush request
0276  * @q: request_queue being kicked
0277  * @fq: flush queue
0278  *
0279  * Flush related states of @q have changed, consider issuing flush request.
0280  * Please read the comment at the top of this file for more info.
0281  *
0282  * CONTEXT:
0283  * spin_lock_irq(q->queue_lock or fq->mq_flush_lock)
0284  *
0285  * RETURNS:
0286  * %true if flush was issued, %false otherwise.
0287  */
0288 static bool blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq)
0289 {
0290     struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
0291     struct request *first_rq =
0292         list_first_entry(pending, struct request, flush.list);
0293     struct request *flush_rq = fq->flush_rq;
0294 
0295     /* C1 described at the top of this file */
0296     if (fq->flush_pending_idx != fq->flush_running_idx || list_empty(pending))
0297         return false;
0298 
0299     /* C2 and C3 */
0300     if (!list_empty(&fq->flush_data_in_flight) &&
0301         time_before(jiffies,
0302             fq->flush_pending_since + FLUSH_PENDING_TIMEOUT))
0303         return false;
0304 
0305     /*
0306      * Issue flush and toggle pending_idx.  This makes pending_idx
0307      * different from running_idx, which means flush is in flight.
0308      */
0309     fq->flush_pending_idx ^= 1;
0310 
0311     blk_rq_init(q, flush_rq);
0312 
0313     /*
0314      * Borrow tag from the first request since they can't
0315      * be in flight at the same time. And acquire the tag's
0316      * ownership for flush req.
0317      */
0318     if (q->mq_ops) {
0319         struct blk_mq_hw_ctx *hctx;
0320 
0321         flush_rq->mq_ctx = first_rq->mq_ctx;
0322         flush_rq->tag = first_rq->tag;
0323         fq->orig_rq = first_rq;
0324 
0325         hctx = blk_mq_map_queue(q, first_rq->mq_ctx->cpu);
0326         blk_mq_tag_set_rq(hctx, first_rq->tag, flush_rq);
0327     }
0328 
0329     flush_rq->cmd_type = REQ_TYPE_FS;
0330     flush_rq->cmd_flags = REQ_OP_FLUSH | REQ_PREFLUSH;
0331     flush_rq->rq_flags |= RQF_FLUSH_SEQ;
0332     flush_rq->rq_disk = first_rq->rq_disk;
0333     flush_rq->end_io = flush_end_io;
0334 
0335     return blk_flush_queue_rq(flush_rq, false);
0336 }
0337 
0338 static void flush_data_end_io(struct request *rq, int error)
0339 {
0340     struct request_queue *q = rq->q;
0341     struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
0342 
0343     /*
0344      * Updating q->in_flight[] here for making this tag usable
0345      * early. Because in blk_queue_start_tag(),
0346      * q->in_flight[BLK_RW_ASYNC] is used to limit async I/O and
0347      * reserve tags for sync I/O.
0348      *
0349      * More importantly this way can avoid the following I/O
0350      * deadlock:
0351      *
0352      * - suppose there are 40 fua requests comming to flush queue
0353      *   and queue depth is 31
0354      * - 30 rqs are scheduled then blk_queue_start_tag() can't alloc
0355      *   tag for async I/O any more
0356      * - all the 30 rqs are completed before FLUSH_PENDING_TIMEOUT
0357      *   and flush_data_end_io() is called
0358      * - the other rqs still can't go ahead if not updating
0359      *   q->in_flight[BLK_RW_ASYNC] here, meantime these rqs
0360      *   are held in flush data queue and make no progress of
0361      *   handling post flush rq
0362      * - only after the post flush rq is handled, all these rqs
0363      *   can be completed
0364      */
0365 
0366     elv_completed_request(q, rq);
0367 
0368     /* for avoiding double accounting */
0369     rq->rq_flags &= ~RQF_STARTED;
0370 
0371     /*
0372      * After populating an empty queue, kick it to avoid stall.  Read
0373      * the comment in flush_end_io().
0374      */
0375     if (blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error))
0376         blk_run_queue_async(q);
0377 }
0378 
0379 static void mq_flush_data_end_io(struct request *rq, int error)
0380 {
0381     struct request_queue *q = rq->q;
0382     struct blk_mq_hw_ctx *hctx;
0383     struct blk_mq_ctx *ctx = rq->mq_ctx;
0384     unsigned long flags;
0385     struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx);
0386 
0387     hctx = blk_mq_map_queue(q, ctx->cpu);
0388 
0389     /*
0390      * After populating an empty queue, kick it to avoid stall.  Read
0391      * the comment in flush_end_io().
0392      */
0393     spin_lock_irqsave(&fq->mq_flush_lock, flags);
0394     if (blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error))
0395         blk_mq_run_hw_queue(hctx, true);
0396     spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
0397 }
0398 
0399 /**
0400  * blk_insert_flush - insert a new FLUSH/FUA request
0401  * @rq: request to insert
0402  *
0403  * To be called from __elv_add_request() for %ELEVATOR_INSERT_FLUSH insertions.
0404  * or __blk_mq_run_hw_queue() to dispatch request.
0405  * @rq is being submitted.  Analyze what needs to be done and put it on the
0406  * right queue.
0407  *
0408  * CONTEXT:
0409  * spin_lock_irq(q->queue_lock) in !mq case
0410  */
0411 void blk_insert_flush(struct request *rq)
0412 {
0413     struct request_queue *q = rq->q;
0414     unsigned long fflags = q->queue_flags;  /* may change, cache */
0415     unsigned int policy = blk_flush_policy(fflags, rq);
0416     struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
0417 
0418     /*
0419      * @policy now records what operations need to be done.  Adjust
0420      * REQ_PREFLUSH and FUA for the driver.
0421      */
0422     rq->cmd_flags &= ~REQ_PREFLUSH;
0423     if (!(fflags & (1UL << QUEUE_FLAG_FUA)))
0424         rq->cmd_flags &= ~REQ_FUA;
0425 
0426     /*
0427      * REQ_PREFLUSH|REQ_FUA implies REQ_SYNC, so if we clear any
0428      * of those flags, we have to set REQ_SYNC to avoid skewing
0429      * the request accounting.
0430      */
0431     rq->cmd_flags |= REQ_SYNC;
0432 
0433     /*
0434      * An empty flush handed down from a stacking driver may
0435      * translate into nothing if the underlying device does not
0436      * advertise a write-back cache.  In this case, simply
0437      * complete the request.
0438      */
0439     if (!policy) {
0440         if (q->mq_ops)
0441             blk_mq_end_request(rq, 0);
0442         else
0443             __blk_end_bidi_request(rq, 0, 0, 0);
0444         return;
0445     }
0446 
0447     BUG_ON(rq->bio != rq->biotail); /*assumes zero or single bio rq */
0448 
0449     /*
0450      * If there's data but flush is not necessary, the request can be
0451      * processed directly without going through flush machinery.  Queue
0452      * for normal execution.
0453      */
0454     if ((policy & REQ_FSEQ_DATA) &&
0455         !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
0456         if (q->mq_ops) {
0457             blk_mq_insert_request(rq, false, true, false);
0458         } else
0459             list_add_tail(&rq->queuelist, &q->queue_head);
0460         return;
0461     }
0462 
0463     /*
0464      * @rq should go through flush machinery.  Mark it part of flush
0465      * sequence and submit for further processing.
0466      */
0467     memset(&rq->flush, 0, sizeof(rq->flush));
0468     INIT_LIST_HEAD(&rq->flush.list);
0469     rq->rq_flags |= RQF_FLUSH_SEQ;
0470     rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
0471     if (q->mq_ops) {
0472         rq->end_io = mq_flush_data_end_io;
0473 
0474         spin_lock_irq(&fq->mq_flush_lock);
0475         blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
0476         spin_unlock_irq(&fq->mq_flush_lock);
0477         return;
0478     }
0479     rq->end_io = flush_data_end_io;
0480 
0481     blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
0482 }
0483 
0484 /**
0485  * blkdev_issue_flush - queue a flush
0486  * @bdev:   blockdev to issue flush for
0487  * @gfp_mask:   memory allocation flags (for bio_alloc)
0488  * @error_sector:   error sector
0489  *
0490  * Description:
0491  *    Issue a flush for the block device in question. Caller can supply
0492  *    room for storing the error offset in case of a flush error, if they
0493  *    wish to. If WAIT flag is not passed then caller may check only what
0494  *    request was pushed in some internal queue for later handling.
0495  */
0496 int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
0497         sector_t *error_sector)
0498 {
0499     struct request_queue *q;
0500     struct bio *bio;
0501     int ret = 0;
0502 
0503     if (bdev->bd_disk == NULL)
0504         return -ENXIO;
0505 
0506     q = bdev_get_queue(bdev);
0507     if (!q)
0508         return -ENXIO;
0509 
0510     /*
0511      * some block devices may not have their queue correctly set up here
0512      * (e.g. loop device without a backing file) and so issuing a flush
0513      * here will panic. Ensure there is a request function before issuing
0514      * the flush.
0515      */
0516     if (!q->make_request_fn)
0517         return -ENXIO;
0518 
0519     bio = bio_alloc(gfp_mask, 0);
0520     bio->bi_bdev = bdev;
0521     bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
0522 
0523     ret = submit_bio_wait(bio);
0524 
0525     /*
0526      * The driver must store the error location in ->bi_sector, if
0527      * it supports it. For non-stacked drivers, this should be
0528      * copied from blk_rq_pos(rq).
0529      */
0530     if (error_sector)
0531         *error_sector = bio->bi_iter.bi_sector;
0532 
0533     bio_put(bio);
0534     return ret;
0535 }
0536 EXPORT_SYMBOL(blkdev_issue_flush);
0537 
0538 struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
0539         int node, int cmd_size)
0540 {
0541     struct blk_flush_queue *fq;
0542     int rq_sz = sizeof(struct request);
0543 
0544     fq = kzalloc_node(sizeof(*fq), GFP_KERNEL, node);
0545     if (!fq)
0546         goto fail;
0547 
0548     if (q->mq_ops) {
0549         spin_lock_init(&fq->mq_flush_lock);
0550         rq_sz = round_up(rq_sz + cmd_size, cache_line_size());
0551     }
0552 
0553     fq->flush_rq = kzalloc_node(rq_sz, GFP_KERNEL, node);
0554     if (!fq->flush_rq)
0555         goto fail_rq;
0556 
0557     INIT_LIST_HEAD(&fq->flush_queue[0]);
0558     INIT_LIST_HEAD(&fq->flush_queue[1]);
0559     INIT_LIST_HEAD(&fq->flush_data_in_flight);
0560 
0561     return fq;
0562 
0563  fail_rq:
0564     kfree(fq);
0565  fail:
0566     return NULL;
0567 }
0568 
0569 void blk_free_flush_queue(struct blk_flush_queue *fq)
0570 {
0571     /* bio based request queue hasn't flush queue */
0572     if (!fq)
0573         return;
0574 
0575     kfree(fq->flush_rq);
0576     kfree(fq);
0577 }