xref: /openbmc/linux/block/blk-flush.c (revision 4e49ea4a)
18839a0e0STejun Heo /*
24fed947cSTejun Heo  * Functions to sequence FLUSH and FUA writes.
3ae1b1539STejun Heo  *
4ae1b1539STejun Heo  * Copyright (C) 2011		Max Planck Institute for Gravitational Physics
5ae1b1539STejun Heo  * Copyright (C) 2011		Tejun Heo <tj@kernel.org>
6ae1b1539STejun Heo  *
7ae1b1539STejun Heo  * This file is released under the GPLv2.
8ae1b1539STejun Heo  *
9ae1b1539STejun Heo  * REQ_{FLUSH|FUA} requests are decomposed to sequences consisted of three
10ae1b1539STejun Heo  * optional steps - PREFLUSH, DATA and POSTFLUSH - according to the request
11ae1b1539STejun Heo  * properties and hardware capability.
12ae1b1539STejun Heo  *
13ae1b1539STejun Heo  * If a request doesn't have data, only REQ_FLUSH makes sense, which
14ae1b1539STejun Heo  * indicates a simple flush request.  If there is data, REQ_FLUSH indicates
15ae1b1539STejun Heo  * that the device cache should be flushed before the data is executed, and
16ae1b1539STejun Heo  * REQ_FUA means that the data must be on non-volatile media on request
17ae1b1539STejun Heo  * completion.
18ae1b1539STejun Heo  *
19ae1b1539STejun Heo  * If the device doesn't have writeback cache, FLUSH and FUA don't make any
20ae1b1539STejun Heo  * difference.  The requests are either completed immediately if there's no
21ae1b1539STejun Heo  * data or executed as normal requests otherwise.
22ae1b1539STejun Heo  *
23ae1b1539STejun Heo  * If the device has writeback cache and supports FUA, REQ_FLUSH is
24ae1b1539STejun Heo  * translated to PREFLUSH but REQ_FUA is passed down directly with DATA.
25ae1b1539STejun Heo  *
26ae1b1539STejun Heo  * If the device has writeback cache and doesn't support FUA, REQ_FLUSH is
27ae1b1539STejun Heo  * translated to PREFLUSH and REQ_FUA to POSTFLUSH.
28ae1b1539STejun Heo  *
29ae1b1539STejun Heo  * The actual execution of flush is double buffered.  Whenever a request
30ae1b1539STejun Heo  * needs to execute PRE or POSTFLUSH, it queues at
317c94e1c1SMing Lei  * fq->flush_queue[fq->flush_pending_idx].  Once certain criteria are met, a
32ae1b1539STejun Heo  * flush is issued and the pending_idx is toggled.  When the flush
33ae1b1539STejun Heo  * completes, all the requests which were pending are proceeded to the next
34ae1b1539STejun Heo  * step.  This allows arbitrary merging of different types of FLUSH/FUA
35ae1b1539STejun Heo  * requests.
36ae1b1539STejun Heo  *
37ae1b1539STejun Heo  * Currently, the following conditions are used to determine when to issue
38ae1b1539STejun Heo  * flush.
39ae1b1539STejun Heo  *
40ae1b1539STejun Heo  * C1. At any given time, only one flush shall be in progress.  This makes
41ae1b1539STejun Heo  *     double buffering sufficient.
42ae1b1539STejun Heo  *
43ae1b1539STejun Heo  * C2. Flush is deferred if any request is executing DATA of its sequence.
44ae1b1539STejun Heo  *     This avoids issuing separate POSTFLUSHes for requests which shared
45ae1b1539STejun Heo  *     PREFLUSH.
46ae1b1539STejun Heo  *
47ae1b1539STejun Heo  * C3. The second condition is ignored if there is a request which has
48ae1b1539STejun Heo  *     waited longer than FLUSH_PENDING_TIMEOUT.  This is to avoid
49ae1b1539STejun Heo  *     starvation in the unlikely case where there are continuous stream of
50ae1b1539STejun Heo  *     FUA (without FLUSH) requests.
51ae1b1539STejun Heo  *
52ae1b1539STejun Heo  * For devices which support FUA, it isn't clear whether C2 (and thus C3)
53ae1b1539STejun Heo  * is beneficial.
54ae1b1539STejun Heo  *
55ae1b1539STejun Heo  * Note that a sequenced FLUSH/FUA request with DATA is completed twice.
56ae1b1539STejun Heo  * Once while executing DATA and again after the whole sequence is
57ae1b1539STejun Heo  * complete.  The first completion updates the contained bio but doesn't
58ae1b1539STejun Heo  * finish it so that the bio submitter is notified only after the whole
59ae1b1539STejun Heo  * sequence is complete.  This is implemented by testing REQ_FLUSH_SEQ in
60ae1b1539STejun Heo  * req_bio_endio().
61ae1b1539STejun Heo  *
62ae1b1539STejun Heo  * The above peculiarity requires that each FLUSH/FUA request has only one
63ae1b1539STejun Heo  * bio attached to it, which is guaranteed as they aren't allowed to be
64ae1b1539STejun Heo  * merged in the usual way.
658839a0e0STejun Heo  */
66ae1b1539STejun Heo 
678839a0e0STejun Heo #include <linux/kernel.h>
688839a0e0STejun Heo #include <linux/module.h>
698839a0e0STejun Heo #include <linux/bio.h>
708839a0e0STejun Heo #include <linux/blkdev.h>
718839a0e0STejun Heo #include <linux/gfp.h>
72320ae51fSJens Axboe #include <linux/blk-mq.h>
738839a0e0STejun Heo 
748839a0e0STejun Heo #include "blk.h"
75320ae51fSJens Axboe #include "blk-mq.h"
760048b483SMing Lei #include "blk-mq-tag.h"
778839a0e0STejun Heo 
784fed947cSTejun Heo /* FLUSH/FUA sequences */
794fed947cSTejun Heo enum {
80ae1b1539STejun Heo 	REQ_FSEQ_PREFLUSH	= (1 << 0), /* pre-flushing in progress */
81ae1b1539STejun Heo 	REQ_FSEQ_DATA		= (1 << 1), /* data write in progress */
82ae1b1539STejun Heo 	REQ_FSEQ_POSTFLUSH	= (1 << 2), /* post-flushing in progress */
83ae1b1539STejun Heo 	REQ_FSEQ_DONE		= (1 << 3),
84ae1b1539STejun Heo 
85ae1b1539STejun Heo 	REQ_FSEQ_ACTIONS	= REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
86ae1b1539STejun Heo 				  REQ_FSEQ_POSTFLUSH,
87ae1b1539STejun Heo 
88ae1b1539STejun Heo 	/*
89ae1b1539STejun Heo 	 * If flush has been pending longer than the following timeout,
90ae1b1539STejun Heo 	 * it's issued even if flush_data requests are still in flight.
91ae1b1539STejun Heo 	 */
92ae1b1539STejun Heo 	FLUSH_PENDING_TIMEOUT	= 5 * HZ,
934fed947cSTejun Heo };
944fed947cSTejun Heo 
950bae352dSMing Lei static bool blk_kick_flush(struct request_queue *q,
960bae352dSMing Lei 			   struct blk_flush_queue *fq);
978839a0e0STejun Heo 
98c888a8f9SJens Axboe static unsigned int blk_flush_policy(unsigned long fflags, struct request *rq)
998839a0e0STejun Heo {
100ae1b1539STejun Heo 	unsigned int policy = 0;
101ae1b1539STejun Heo 
102fa1bf42fSJeff Moyer 	if (blk_rq_sectors(rq))
103fa1bf42fSJeff Moyer 		policy |= REQ_FSEQ_DATA;
104fa1bf42fSJeff Moyer 
105c888a8f9SJens Axboe 	if (fflags & (1UL << QUEUE_FLAG_WC)) {
106ae1b1539STejun Heo 		if (rq->cmd_flags & REQ_FLUSH)
107ae1b1539STejun Heo 			policy |= REQ_FSEQ_PREFLUSH;
108c888a8f9SJens Axboe 		if (!(fflags & (1UL << QUEUE_FLAG_FUA)) &&
109c888a8f9SJens Axboe 		    (rq->cmd_flags & REQ_FUA))
110ae1b1539STejun Heo 			policy |= REQ_FSEQ_POSTFLUSH;
111ae1b1539STejun Heo 	}
112ae1b1539STejun Heo 	return policy;
1138839a0e0STejun Heo }
1148839a0e0STejun Heo 
115ae1b1539STejun Heo static unsigned int blk_flush_cur_seq(struct request *rq)
1168839a0e0STejun Heo {
117ae1b1539STejun Heo 	return 1 << ffz(rq->flush.seq);
1188839a0e0STejun Heo }
1198839a0e0STejun Heo 
120ae1b1539STejun Heo static void blk_flush_restore_request(struct request *rq)
12147f70d5aSTejun Heo {
12247f70d5aSTejun Heo 	/*
123ae1b1539STejun Heo 	 * After flush data completion, @rq->bio is %NULL but we need to
124ae1b1539STejun Heo 	 * complete the bio again.  @rq->biotail is guaranteed to equal the
125ae1b1539STejun Heo 	 * original @rq->bio.  Restore it.
12647f70d5aSTejun Heo 	 */
127ae1b1539STejun Heo 	rq->bio = rq->biotail;
128ae1b1539STejun Heo 
129ae1b1539STejun Heo 	/* make @rq a normal request */
130ae1b1539STejun Heo 	rq->cmd_flags &= ~REQ_FLUSH_SEQ;
1314853abaaSJeff Moyer 	rq->end_io = rq->flush.saved_end_io;
132320ae51fSJens Axboe }
133320ae51fSJens Axboe 
13410beafc1SMike Snitzer static bool blk_flush_queue_rq(struct request *rq, bool add_front)
135320ae51fSJens Axboe {
13618741986SChristoph Hellwig 	if (rq->q->mq_ops) {
1376fca6a61SChristoph Hellwig 		struct request_queue *q = rq->q;
1386fca6a61SChristoph Hellwig 
1396fca6a61SChristoph Hellwig 		blk_mq_add_to_requeue_list(rq, add_front);
1406fca6a61SChristoph Hellwig 		blk_mq_kick_requeue_list(q);
14118741986SChristoph Hellwig 		return false;
14218741986SChristoph Hellwig 	} else {
14310beafc1SMike Snitzer 		if (add_front)
14410beafc1SMike Snitzer 			list_add(&rq->queuelist, &rq->q->queue_head);
14510beafc1SMike Snitzer 		else
14618741986SChristoph Hellwig 			list_add_tail(&rq->queuelist, &rq->q->queue_head);
14718741986SChristoph Hellwig 		return true;
14818741986SChristoph Hellwig 	}
14947f70d5aSTejun Heo }
15047f70d5aSTejun Heo 
151ae1b1539STejun Heo /**
152ae1b1539STejun Heo  * blk_flush_complete_seq - complete flush sequence
153ae1b1539STejun Heo  * @rq: FLUSH/FUA request being sequenced
1540bae352dSMing Lei  * @fq: flush queue
155ae1b1539STejun Heo  * @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
156ae1b1539STejun Heo  * @error: whether an error occurred
157ae1b1539STejun Heo  *
158ae1b1539STejun Heo  * @rq just completed @seq part of its flush sequence, record the
159ae1b1539STejun Heo  * completion and trigger the next step.
160ae1b1539STejun Heo  *
161ae1b1539STejun Heo  * CONTEXT:
1627c94e1c1SMing Lei  * spin_lock_irq(q->queue_lock or fq->mq_flush_lock)
163ae1b1539STejun Heo  *
164ae1b1539STejun Heo  * RETURNS:
165ae1b1539STejun Heo  * %true if requests were added to the dispatch queue, %false otherwise.
166ae1b1539STejun Heo  */
1670bae352dSMing Lei static bool blk_flush_complete_seq(struct request *rq,
1680bae352dSMing Lei 				   struct blk_flush_queue *fq,
1690bae352dSMing Lei 				   unsigned int seq, int error)
1708839a0e0STejun Heo {
171ae1b1539STejun Heo 	struct request_queue *q = rq->q;
1727c94e1c1SMing Lei 	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
173320ae51fSJens Axboe 	bool queued = false, kicked;
1748839a0e0STejun Heo 
175ae1b1539STejun Heo 	BUG_ON(rq->flush.seq & seq);
176ae1b1539STejun Heo 	rq->flush.seq |= seq;
1778839a0e0STejun Heo 
178ae1b1539STejun Heo 	if (likely(!error))
179ae1b1539STejun Heo 		seq = blk_flush_cur_seq(rq);
180ae1b1539STejun Heo 	else
181ae1b1539STejun Heo 		seq = REQ_FSEQ_DONE;
1828839a0e0STejun Heo 
183ae1b1539STejun Heo 	switch (seq) {
184ae1b1539STejun Heo 	case REQ_FSEQ_PREFLUSH:
185ae1b1539STejun Heo 	case REQ_FSEQ_POSTFLUSH:
186ae1b1539STejun Heo 		/* queue for flush */
187ae1b1539STejun Heo 		if (list_empty(pending))
1887c94e1c1SMing Lei 			fq->flush_pending_since = jiffies;
189ae1b1539STejun Heo 		list_move_tail(&rq->flush.list, pending);
1908839a0e0STejun Heo 		break;
191ae1b1539STejun Heo 
192ae1b1539STejun Heo 	case REQ_FSEQ_DATA:
1937c94e1c1SMing Lei 		list_move_tail(&rq->flush.list, &fq->flush_data_in_flight);
19410beafc1SMike Snitzer 		queued = blk_flush_queue_rq(rq, true);
195ae1b1539STejun Heo 		break;
196ae1b1539STejun Heo 
197ae1b1539STejun Heo 	case REQ_FSEQ_DONE:
19809d60c70STejun Heo 		/*
199ae1b1539STejun Heo 		 * @rq was previously adjusted by blk_flush_issue() for
200ae1b1539STejun Heo 		 * flush sequencing and may already have gone through the
201ae1b1539STejun Heo 		 * flush data request completion path.  Restore @rq for
202ae1b1539STejun Heo 		 * normal completion and end it.
20309d60c70STejun Heo 		 */
204ae1b1539STejun Heo 		BUG_ON(!list_empty(&rq->queuelist));
205ae1b1539STejun Heo 		list_del_init(&rq->flush.list);
206ae1b1539STejun Heo 		blk_flush_restore_request(rq);
207320ae51fSJens Axboe 		if (q->mq_ops)
208c8a446adSChristoph Hellwig 			blk_mq_end_request(rq, error);
209320ae51fSJens Axboe 		else
210ae1b1539STejun Heo 			__blk_end_request_all(rq, error);
2118839a0e0STejun Heo 		break;
212ae1b1539STejun Heo 
2138839a0e0STejun Heo 	default:
2148839a0e0STejun Heo 		BUG();
2158839a0e0STejun Heo 	}
216cde4c406SChristoph Hellwig 
2170bae352dSMing Lei 	kicked = blk_kick_flush(q, fq);
218320ae51fSJens Axboe 	return kicked | queued;
2198839a0e0STejun Heo }
2208839a0e0STejun Heo 
221ae1b1539STejun Heo static void flush_end_io(struct request *flush_rq, int error)
2228839a0e0STejun Heo {
223ae1b1539STejun Heo 	struct request_queue *q = flush_rq->q;
224320ae51fSJens Axboe 	struct list_head *running;
225ae1b1539STejun Heo 	bool queued = false;
226ae1b1539STejun Heo 	struct request *rq, *n;
227320ae51fSJens Axboe 	unsigned long flags = 0;
228e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx);
2298839a0e0STejun Heo 
23022302375SShaohua Li 	if (q->mq_ops) {
2310048b483SMing Lei 		struct blk_mq_hw_ctx *hctx;
2320048b483SMing Lei 
2330048b483SMing Lei 		/* release the tag's ownership to the req cloned from */
2347c94e1c1SMing Lei 		spin_lock_irqsave(&fq->mq_flush_lock, flags);
2350048b483SMing Lei 		hctx = q->mq_ops->map_queue(q, flush_rq->mq_ctx->cpu);
2360048b483SMing Lei 		blk_mq_tag_set_rq(hctx, flush_rq->tag, fq->orig_rq);
2377ddab5deSMing Lei 		flush_rq->tag = -1;
23822302375SShaohua Li 	}
23918741986SChristoph Hellwig 
2407c94e1c1SMing Lei 	running = &fq->flush_queue[fq->flush_running_idx];
2417c94e1c1SMing Lei 	BUG_ON(fq->flush_pending_idx == fq->flush_running_idx);
242ae1b1539STejun Heo 
243ae1b1539STejun Heo 	/* account completion of the flush request */
2447c94e1c1SMing Lei 	fq->flush_running_idx ^= 1;
245320ae51fSJens Axboe 
246320ae51fSJens Axboe 	if (!q->mq_ops)
247ae1b1539STejun Heo 		elv_completed_request(q, flush_rq);
248ae1b1539STejun Heo 
249ae1b1539STejun Heo 	/* and push the waiting requests to the next stage */
250ae1b1539STejun Heo 	list_for_each_entry_safe(rq, n, running, flush.list) {
251ae1b1539STejun Heo 		unsigned int seq = blk_flush_cur_seq(rq);
252ae1b1539STejun Heo 
253ae1b1539STejun Heo 		BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
2540bae352dSMing Lei 		queued |= blk_flush_complete_seq(rq, fq, seq, error);
255ae1b1539STejun Heo 	}
256ae1b1539STejun Heo 
2578839a0e0STejun Heo 	/*
2583ac0cc45Sshaohua.li@intel.com 	 * Kick the queue to avoid stall for two cases:
2593ac0cc45Sshaohua.li@intel.com 	 * 1. Moving a request silently to empty queue_head may stall the
2603ac0cc45Sshaohua.li@intel.com 	 * queue.
2613ac0cc45Sshaohua.li@intel.com 	 * 2. When flush request is running in non-queueable queue, the
2623ac0cc45Sshaohua.li@intel.com 	 * queue is hold. Restart the queue after flush request is finished
2633ac0cc45Sshaohua.li@intel.com 	 * to avoid stall.
2643ac0cc45Sshaohua.li@intel.com 	 * This function is called from request completion path and calling
2653ac0cc45Sshaohua.li@intel.com 	 * directly into request_fn may confuse the driver.  Always use
2663ac0cc45Sshaohua.li@intel.com 	 * kblockd.
2678839a0e0STejun Heo 	 */
2687c94e1c1SMing Lei 	if (queued || fq->flush_queue_delayed) {
26918741986SChristoph Hellwig 		WARN_ON(q->mq_ops);
27024ecfbe2SChristoph Hellwig 		blk_run_queue_async(q);
271320ae51fSJens Axboe 	}
2727c94e1c1SMing Lei 	fq->flush_queue_delayed = 0;
273320ae51fSJens Axboe 	if (q->mq_ops)
2747c94e1c1SMing Lei 		spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
275320ae51fSJens Axboe }
276320ae51fSJens Axboe 
277ae1b1539STejun Heo /**
278ae1b1539STejun Heo  * blk_kick_flush - consider issuing flush request
279ae1b1539STejun Heo  * @q: request_queue being kicked
2800bae352dSMing Lei  * @fq: flush queue
2814fed947cSTejun Heo  *
282ae1b1539STejun Heo  * Flush related states of @q have changed, consider issuing flush request.
283ae1b1539STejun Heo  * Please read the comment at the top of this file for more info.
284ae1b1539STejun Heo  *
285ae1b1539STejun Heo  * CONTEXT:
2867c94e1c1SMing Lei  * spin_lock_irq(q->queue_lock or fq->mq_flush_lock)
287ae1b1539STejun Heo  *
288ae1b1539STejun Heo  * RETURNS:
289ae1b1539STejun Heo  * %true if flush was issued, %false otherwise.
2908839a0e0STejun Heo  */
2910bae352dSMing Lei static bool blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq)
292ae1b1539STejun Heo {
2937c94e1c1SMing Lei 	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
294ae1b1539STejun Heo 	struct request *first_rq =
295ae1b1539STejun Heo 		list_first_entry(pending, struct request, flush.list);
2967c94e1c1SMing Lei 	struct request *flush_rq = fq->flush_rq;
297ae1b1539STejun Heo 
298ae1b1539STejun Heo 	/* C1 described at the top of this file */
2997c94e1c1SMing Lei 	if (fq->flush_pending_idx != fq->flush_running_idx || list_empty(pending))
300ae1b1539STejun Heo 		return false;
301ae1b1539STejun Heo 
302ae1b1539STejun Heo 	/* C2 and C3 */
3037c94e1c1SMing Lei 	if (!list_empty(&fq->flush_data_in_flight) &&
304ae1b1539STejun Heo 	    time_before(jiffies,
3057c94e1c1SMing Lei 			fq->flush_pending_since + FLUSH_PENDING_TIMEOUT))
306ae1b1539STejun Heo 		return false;
307ae1b1539STejun Heo 
308ae1b1539STejun Heo 	/*
309ae1b1539STejun Heo 	 * Issue flush and toggle pending_idx.  This makes pending_idx
310ae1b1539STejun Heo 	 * different from running_idx, which means flush is in flight.
311ae1b1539STejun Heo 	 */
3127c94e1c1SMing Lei 	fq->flush_pending_idx ^= 1;
31318741986SChristoph Hellwig 
3147ddab5deSMing Lei 	blk_rq_init(q, flush_rq);
315f70ced09SMing Lei 
316f70ced09SMing Lei 	/*
317f70ced09SMing Lei 	 * Borrow tag from the first request since they can't
3180048b483SMing Lei 	 * be in flight at the same time. And acquire the tag's
3190048b483SMing Lei 	 * ownership for flush req.
320f70ced09SMing Lei 	 */
321f70ced09SMing Lei 	if (q->mq_ops) {
3220048b483SMing Lei 		struct blk_mq_hw_ctx *hctx;
3230048b483SMing Lei 
324f70ced09SMing Lei 		flush_rq->mq_ctx = first_rq->mq_ctx;
325f70ced09SMing Lei 		flush_rq->tag = first_rq->tag;
3260048b483SMing Lei 		fq->orig_rq = first_rq;
3270048b483SMing Lei 
3280048b483SMing Lei 		hctx = q->mq_ops->map_queue(q, first_rq->mq_ctx->cpu);
3290048b483SMing Lei 		blk_mq_tag_set_rq(hctx, first_rq->tag, flush_rq);
330f70ced09SMing Lei 	}
331320ae51fSJens Axboe 
3327ddab5deSMing Lei 	flush_rq->cmd_type = REQ_TYPE_FS;
3337ddab5deSMing Lei 	flush_rq->cmd_flags = WRITE_FLUSH | REQ_FLUSH_SEQ;
3347ddab5deSMing Lei 	flush_rq->rq_disk = first_rq->rq_disk;
3357ddab5deSMing Lei 	flush_rq->end_io = flush_end_io;
336ae1b1539STejun Heo 
3377ddab5deSMing Lei 	return blk_flush_queue_rq(flush_rq, false);
338ae1b1539STejun Heo }
339ae1b1539STejun Heo 
340ae1b1539STejun Heo static void flush_data_end_io(struct request *rq, int error)
341ae1b1539STejun Heo {
342ae1b1539STejun Heo 	struct request_queue *q = rq->q;
343e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
344ae1b1539STejun Heo 
3458839a0e0STejun Heo 	/*
346e83a46bbSTejun Heo 	 * After populating an empty queue, kick it to avoid stall.  Read
347e83a46bbSTejun Heo 	 * the comment in flush_end_io().
3488839a0e0STejun Heo 	 */
3490bae352dSMing Lei 	if (blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error))
35024ecfbe2SChristoph Hellwig 		blk_run_queue_async(q);
351ae1b1539STejun Heo }
352ae1b1539STejun Heo 
353320ae51fSJens Axboe static void mq_flush_data_end_io(struct request *rq, int error)
354320ae51fSJens Axboe {
355320ae51fSJens Axboe 	struct request_queue *q = rq->q;
356320ae51fSJens Axboe 	struct blk_mq_hw_ctx *hctx;
357e97c293cSMing Lei 	struct blk_mq_ctx *ctx = rq->mq_ctx;
358320ae51fSJens Axboe 	unsigned long flags;
359e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx);
360320ae51fSJens Axboe 
361320ae51fSJens Axboe 	hctx = q->mq_ops->map_queue(q, ctx->cpu);
362320ae51fSJens Axboe 
363320ae51fSJens Axboe 	/*
364320ae51fSJens Axboe 	 * After populating an empty queue, kick it to avoid stall.  Read
365320ae51fSJens Axboe 	 * the comment in flush_end_io().
366320ae51fSJens Axboe 	 */
3677c94e1c1SMing Lei 	spin_lock_irqsave(&fq->mq_flush_lock, flags);
3680bae352dSMing Lei 	if (blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error))
369320ae51fSJens Axboe 		blk_mq_run_hw_queue(hctx, true);
3707c94e1c1SMing Lei 	spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
371320ae51fSJens Axboe }
372320ae51fSJens Axboe 
373ae1b1539STejun Heo /**
374ae1b1539STejun Heo  * blk_insert_flush - insert a new FLUSH/FUA request
375ae1b1539STejun Heo  * @rq: request to insert
376ae1b1539STejun Heo  *
377b710a480SJens Axboe  * To be called from __elv_add_request() for %ELEVATOR_INSERT_FLUSH insertions.
378320ae51fSJens Axboe  * or __blk_mq_run_hw_queue() to dispatch request.
379ae1b1539STejun Heo  * @rq is being submitted.  Analyze what needs to be done and put it on the
380ae1b1539STejun Heo  * right queue.
381ae1b1539STejun Heo  *
382ae1b1539STejun Heo  * CONTEXT:
383320ae51fSJens Axboe  * spin_lock_irq(q->queue_lock) in !mq case
384ae1b1539STejun Heo  */
385ae1b1539STejun Heo void blk_insert_flush(struct request *rq)
386ae1b1539STejun Heo {
387ae1b1539STejun Heo 	struct request_queue *q = rq->q;
388c888a8f9SJens Axboe 	unsigned long fflags = q->queue_flags;	/* may change, cache */
389ae1b1539STejun Heo 	unsigned int policy = blk_flush_policy(fflags, rq);
390e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
391ae1b1539STejun Heo 
392ae1b1539STejun Heo 	/*
393ae1b1539STejun Heo 	 * @policy now records what operations need to be done.  Adjust
394ae1b1539STejun Heo 	 * REQ_FLUSH and FUA for the driver.
395ae1b1539STejun Heo 	 */
3964fed947cSTejun Heo 	rq->cmd_flags &= ~REQ_FLUSH;
397c888a8f9SJens Axboe 	if (!(fflags & (1UL << QUEUE_FLAG_FUA)))
3984fed947cSTejun Heo 		rq->cmd_flags &= ~REQ_FUA;
399ae1b1539STejun Heo 
400ae1b1539STejun Heo 	/*
4014853abaaSJeff Moyer 	 * An empty flush handed down from a stacking driver may
4024853abaaSJeff Moyer 	 * translate into nothing if the underlying device does not
4034853abaaSJeff Moyer 	 * advertise a write-back cache.  In this case, simply
4044853abaaSJeff Moyer 	 * complete the request.
4054853abaaSJeff Moyer 	 */
4064853abaaSJeff Moyer 	if (!policy) {
407320ae51fSJens Axboe 		if (q->mq_ops)
408c8a446adSChristoph Hellwig 			blk_mq_end_request(rq, 0);
409320ae51fSJens Axboe 		else
4104853abaaSJeff Moyer 			__blk_end_bidi_request(rq, 0, 0, 0);
4114853abaaSJeff Moyer 		return;
4124853abaaSJeff Moyer 	}
4134853abaaSJeff Moyer 
414834f9f61SJeff Moyer 	BUG_ON(rq->bio != rq->biotail); /*assumes zero or single bio rq */
4154853abaaSJeff Moyer 
4164853abaaSJeff Moyer 	/*
417ae1b1539STejun Heo 	 * If there's data but flush is not necessary, the request can be
418ae1b1539STejun Heo 	 * processed directly without going through flush machinery.  Queue
419ae1b1539STejun Heo 	 * for normal execution.
420ae1b1539STejun Heo 	 */
421ae1b1539STejun Heo 	if ((policy & REQ_FSEQ_DATA) &&
422ae1b1539STejun Heo 	    !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
423320ae51fSJens Axboe 		if (q->mq_ops) {
424feb71daeSChristoph Hellwig 			blk_mq_insert_request(rq, false, false, true);
425320ae51fSJens Axboe 		} else
426dcd8376cSJens Axboe 			list_add_tail(&rq->queuelist, &q->queue_head);
427ae1b1539STejun Heo 		return;
4288839a0e0STejun Heo 	}
4298839a0e0STejun Heo 
4308839a0e0STejun Heo 	/*
431ae1b1539STejun Heo 	 * @rq should go through flush machinery.  Mark it part of flush
432ae1b1539STejun Heo 	 * sequence and submit for further processing.
4338839a0e0STejun Heo 	 */
434ae1b1539STejun Heo 	memset(&rq->flush, 0, sizeof(rq->flush));
435ae1b1539STejun Heo 	INIT_LIST_HEAD(&rq->flush.list);
436ae1b1539STejun Heo 	rq->cmd_flags |= REQ_FLUSH_SEQ;
4374853abaaSJeff Moyer 	rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
438320ae51fSJens Axboe 	if (q->mq_ops) {
439320ae51fSJens Axboe 		rq->end_io = mq_flush_data_end_io;
440320ae51fSJens Axboe 
4417c94e1c1SMing Lei 		spin_lock_irq(&fq->mq_flush_lock);
4420bae352dSMing Lei 		blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
4437c94e1c1SMing Lei 		spin_unlock_irq(&fq->mq_flush_lock);
444320ae51fSJens Axboe 		return;
445320ae51fSJens Axboe 	}
446ae1b1539STejun Heo 	rq->end_io = flush_data_end_io;
447ae1b1539STejun Heo 
4480bae352dSMing Lei 	blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
449ae1b1539STejun Heo }
450ae1b1539STejun Heo 
451ae1b1539STejun Heo /**
4528839a0e0STejun Heo  * blkdev_issue_flush - queue a flush
4538839a0e0STejun Heo  * @bdev:	blockdev to issue flush for
4548839a0e0STejun Heo  * @gfp_mask:	memory allocation flags (for bio_alloc)
4558839a0e0STejun Heo  * @error_sector:	error sector
4568839a0e0STejun Heo  *
4578839a0e0STejun Heo  * Description:
4588839a0e0STejun Heo  *    Issue a flush for the block device in question. Caller can supply
4598839a0e0STejun Heo  *    room for storing the error offset in case of a flush error, if they
4608839a0e0STejun Heo  *    wish to. If WAIT flag is not passed then caller may check only what
4618839a0e0STejun Heo  *    request was pushed in some internal queue for later handling.
4628839a0e0STejun Heo  */
4638839a0e0STejun Heo int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
464dd3932edSChristoph Hellwig 		sector_t *error_sector)
4658839a0e0STejun Heo {
4668839a0e0STejun Heo 	struct request_queue *q;
4678839a0e0STejun Heo 	struct bio *bio;
4688839a0e0STejun Heo 	int ret = 0;
4698839a0e0STejun Heo 
4708839a0e0STejun Heo 	if (bdev->bd_disk == NULL)
4718839a0e0STejun Heo 		return -ENXIO;
4728839a0e0STejun Heo 
4738839a0e0STejun Heo 	q = bdev_get_queue(bdev);
4748839a0e0STejun Heo 	if (!q)
4758839a0e0STejun Heo 		return -ENXIO;
4768839a0e0STejun Heo 
4778839a0e0STejun Heo 	/*
4788839a0e0STejun Heo 	 * some block devices may not have their queue correctly set up here
4798839a0e0STejun Heo 	 * (e.g. loop device without a backing file) and so issuing a flush
4808839a0e0STejun Heo 	 * here will panic. Ensure there is a request function before issuing
481d391a2ddSTejun Heo 	 * the flush.
4828839a0e0STejun Heo 	 */
4838839a0e0STejun Heo 	if (!q->make_request_fn)
4848839a0e0STejun Heo 		return -ENXIO;
4858839a0e0STejun Heo 
4868839a0e0STejun Heo 	bio = bio_alloc(gfp_mask, 0);
4878839a0e0STejun Heo 	bio->bi_bdev = bdev;
4884e49ea4aSMike Christie 	bio->bi_rw = WRITE_FLUSH;
4898839a0e0STejun Heo 
4904e49ea4aSMike Christie 	ret = submit_bio_wait(bio);
491dd3932edSChristoph Hellwig 
4928839a0e0STejun Heo 	/*
4938839a0e0STejun Heo 	 * The driver must store the error location in ->bi_sector, if
4948839a0e0STejun Heo 	 * it supports it. For non-stacked drivers, this should be
4958839a0e0STejun Heo 	 * copied from blk_rq_pos(rq).
4968839a0e0STejun Heo 	 */
4978839a0e0STejun Heo 	if (error_sector)
4984f024f37SKent Overstreet 		*error_sector = bio->bi_iter.bi_sector;
4998839a0e0STejun Heo 
5008839a0e0STejun Heo 	bio_put(bio);
5018839a0e0STejun Heo 	return ret;
5028839a0e0STejun Heo }
5038839a0e0STejun Heo EXPORT_SYMBOL(blkdev_issue_flush);
504320ae51fSJens Axboe 
505f70ced09SMing Lei struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
506f70ced09SMing Lei 		int node, int cmd_size)
507320ae51fSJens Axboe {
5087c94e1c1SMing Lei 	struct blk_flush_queue *fq;
5097c94e1c1SMing Lei 	int rq_sz = sizeof(struct request);
5101bcb1eadSMing Lei 
511f70ced09SMing Lei 	fq = kzalloc_node(sizeof(*fq), GFP_KERNEL, node);
5127c94e1c1SMing Lei 	if (!fq)
5137c94e1c1SMing Lei 		goto fail;
5141bcb1eadSMing Lei 
5157c94e1c1SMing Lei 	if (q->mq_ops) {
5167c94e1c1SMing Lei 		spin_lock_init(&fq->mq_flush_lock);
517f70ced09SMing Lei 		rq_sz = round_up(rq_sz + cmd_size, cache_line_size());
5187c94e1c1SMing Lei 	}
5197c94e1c1SMing Lei 
520f70ced09SMing Lei 	fq->flush_rq = kzalloc_node(rq_sz, GFP_KERNEL, node);
5217c94e1c1SMing Lei 	if (!fq->flush_rq)
5227c94e1c1SMing Lei 		goto fail_rq;
5237c94e1c1SMing Lei 
5247c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_queue[0]);
5257c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_queue[1]);
5267c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_data_in_flight);
5277c94e1c1SMing Lei 
5287c94e1c1SMing Lei 	return fq;
5297c94e1c1SMing Lei 
5307c94e1c1SMing Lei  fail_rq:
5317c94e1c1SMing Lei 	kfree(fq);
5327c94e1c1SMing Lei  fail:
5337c94e1c1SMing Lei 	return NULL;
5347c94e1c1SMing Lei }
5357c94e1c1SMing Lei 
536ba483388SMing Lei void blk_free_flush_queue(struct blk_flush_queue *fq)
5377c94e1c1SMing Lei {
5387c94e1c1SMing Lei 	/* bio based request queue hasn't flush queue */
5397c94e1c1SMing Lei 	if (!fq)
5407c94e1c1SMing Lei 		return;
5417c94e1c1SMing Lei 
5427c94e1c1SMing Lei 	kfree(fq->flush_rq);
5437c94e1c1SMing Lei 	kfree(fq);
544320ae51fSJens Axboe }
545