xref: /openbmc/linux/block/blk-flush.c (revision fe1e3955)
18c16567dSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
28839a0e0STejun Heo /*
33140c3cfSOmar Sandoval  * Functions to sequence PREFLUSH and FUA writes.
4ae1b1539STejun Heo  *
5ae1b1539STejun Heo  * Copyright (C) 2011		Max Planck Institute for Gravitational Physics
6ae1b1539STejun Heo  * Copyright (C) 2011		Tejun Heo <tj@kernel.org>
7ae1b1539STejun Heo  *
83140c3cfSOmar Sandoval  * REQ_{PREFLUSH|FUA} requests are decomposed to sequences consisted of three
9ae1b1539STejun Heo  * optional steps - PREFLUSH, DATA and POSTFLUSH - according to the request
10ae1b1539STejun Heo  * properties and hardware capability.
11ae1b1539STejun Heo  *
1228a8f0d3SMike Christie  * If a request doesn't have data, only REQ_PREFLUSH makes sense, which
1328a8f0d3SMike Christie  * indicates a simple flush request.  If there is data, REQ_PREFLUSH indicates
14ae1b1539STejun Heo  * that the device cache should be flushed before the data is executed, and
15ae1b1539STejun Heo  * REQ_FUA means that the data must be on non-volatile media on request
16ae1b1539STejun Heo  * completion.
17ae1b1539STejun Heo  *
183140c3cfSOmar Sandoval  * If the device doesn't have writeback cache, PREFLUSH and FUA don't make any
193140c3cfSOmar Sandoval  * difference.  The requests are either completed immediately if there's no data
203140c3cfSOmar Sandoval  * or executed as normal requests otherwise.
21ae1b1539STejun Heo  *
2228a8f0d3SMike Christie  * If the device has writeback cache and supports FUA, REQ_PREFLUSH is
23ae1b1539STejun Heo  * translated to PREFLUSH but REQ_FUA is passed down directly with DATA.
24ae1b1539STejun Heo  *
2528a8f0d3SMike Christie  * If the device has writeback cache and doesn't support FUA, REQ_PREFLUSH
2628a8f0d3SMike Christie  * is translated to PREFLUSH and REQ_FUA to POSTFLUSH.
27ae1b1539STejun Heo  *
28ae1b1539STejun Heo  * The actual execution of flush is double buffered.  Whenever a request
29ae1b1539STejun Heo  * needs to execute PRE or POSTFLUSH, it queues at
307c94e1c1SMing Lei  * fq->flush_queue[fq->flush_pending_idx].  Once certain criteria are met, a
313a5e02ceSMike Christie  * REQ_OP_FLUSH is issued and the pending_idx is toggled.  When the flush
32ae1b1539STejun Heo  * completes, all the requests which were pending are proceeded to the next
333140c3cfSOmar Sandoval  * step.  This allows arbitrary merging of different types of PREFLUSH/FUA
34ae1b1539STejun Heo  * requests.
35ae1b1539STejun Heo  *
36ae1b1539STejun Heo  * Currently, the following conditions are used to determine when to issue
37ae1b1539STejun Heo  * flush.
38ae1b1539STejun Heo  *
39ae1b1539STejun Heo  * C1. At any given time, only one flush shall be in progress.  This makes
40ae1b1539STejun Heo  *     double buffering sufficient.
41ae1b1539STejun Heo  *
42ae1b1539STejun Heo  * C2. Flush is deferred if any request is executing DATA of its sequence.
43ae1b1539STejun Heo  *     This avoids issuing separate POSTFLUSHes for requests which shared
44ae1b1539STejun Heo  *     PREFLUSH.
45ae1b1539STejun Heo  *
46ae1b1539STejun Heo  * C3. The second condition is ignored if there is a request which has
47ae1b1539STejun Heo  *     waited longer than FLUSH_PENDING_TIMEOUT.  This is to avoid
48ae1b1539STejun Heo  *     starvation in the unlikely case where there are continuous stream of
493140c3cfSOmar Sandoval  *     FUA (without PREFLUSH) requests.
50ae1b1539STejun Heo  *
51ae1b1539STejun Heo  * For devices which support FUA, it isn't clear whether C2 (and thus C3)
52ae1b1539STejun Heo  * is beneficial.
53ae1b1539STejun Heo  *
543140c3cfSOmar Sandoval  * Note that a sequenced PREFLUSH/FUA request with DATA is completed twice.
55ae1b1539STejun Heo  * Once while executing DATA and again after the whole sequence is
56ae1b1539STejun Heo  * complete.  The first completion updates the contained bio but doesn't
57ae1b1539STejun Heo  * finish it so that the bio submitter is notified only after the whole
58e8064021SChristoph Hellwig  * sequence is complete.  This is implemented by testing RQF_FLUSH_SEQ in
59ae1b1539STejun Heo  * req_bio_endio().
60ae1b1539STejun Heo  *
613140c3cfSOmar Sandoval  * The above peculiarity requires that each PREFLUSH/FUA request has only one
62ae1b1539STejun Heo  * bio attached to it, which is guaranteed as they aren't allowed to be
63ae1b1539STejun Heo  * merged in the usual way.
648839a0e0STejun Heo  */
65ae1b1539STejun Heo 
668839a0e0STejun Heo #include <linux/kernel.h>
678839a0e0STejun Heo #include <linux/module.h>
688839a0e0STejun Heo #include <linux/bio.h>
698839a0e0STejun Heo #include <linux/blkdev.h>
708839a0e0STejun Heo #include <linux/gfp.h>
7182d981d4SChristoph Hellwig #include <linux/part_stat.h>
728839a0e0STejun Heo 
738839a0e0STejun Heo #include "blk.h"
74320ae51fSJens Axboe #include "blk-mq.h"
75bd166ef1SJens Axboe #include "blk-mq-sched.h"
768839a0e0STejun Heo 
773140c3cfSOmar Sandoval /* PREFLUSH/FUA sequences */
784fed947cSTejun Heo enum {
79ae1b1539STejun Heo 	REQ_FSEQ_PREFLUSH	= (1 << 0), /* pre-flushing in progress */
80ae1b1539STejun Heo 	REQ_FSEQ_DATA		= (1 << 1), /* data write in progress */
81ae1b1539STejun Heo 	REQ_FSEQ_POSTFLUSH	= (1 << 2), /* post-flushing in progress */
82ae1b1539STejun Heo 	REQ_FSEQ_DONE		= (1 << 3),
83ae1b1539STejun Heo 
84ae1b1539STejun Heo 	REQ_FSEQ_ACTIONS	= REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
85ae1b1539STejun Heo 				  REQ_FSEQ_POSTFLUSH,
86ae1b1539STejun Heo 
87ae1b1539STejun Heo 	/*
88ae1b1539STejun Heo 	 * If flush has been pending longer than the following timeout,
89ae1b1539STejun Heo 	 * it's issued even if flush_data requests are still in flight.
90ae1b1539STejun Heo 	 */
91ae1b1539STejun Heo 	FLUSH_PENDING_TIMEOUT	= 5 * HZ,
924fed947cSTejun Heo };
934fed947cSTejun Heo 
94404b8f5aSJens Axboe static void blk_kick_flush(struct request_queue *q,
9516458cf3SBart Van Assche 			   struct blk_flush_queue *fq, blk_opf_t flags);
968839a0e0STejun Heo 
970281ed3cSChristoph Hellwig static inline struct blk_flush_queue *
blk_get_flush_queue(struct request_queue * q,struct blk_mq_ctx * ctx)980281ed3cSChristoph Hellwig blk_get_flush_queue(struct request_queue *q, struct blk_mq_ctx *ctx)
990281ed3cSChristoph Hellwig {
1000281ed3cSChristoph Hellwig 	return blk_mq_map_queue(q, REQ_OP_FLUSH, ctx)->fq;
1010281ed3cSChristoph Hellwig }
1020281ed3cSChristoph Hellwig 
blk_flush_policy(unsigned long fflags,struct request * rq)103c888a8f9SJens Axboe static unsigned int blk_flush_policy(unsigned long fflags, struct request *rq)
1048839a0e0STejun Heo {
105ae1b1539STejun Heo 	unsigned int policy = 0;
106ae1b1539STejun Heo 
107fa1bf42fSJeff Moyer 	if (blk_rq_sectors(rq))
108fa1bf42fSJeff Moyer 		policy |= REQ_FSEQ_DATA;
109fa1bf42fSJeff Moyer 
110c888a8f9SJens Axboe 	if (fflags & (1UL << QUEUE_FLAG_WC)) {
11128a8f0d3SMike Christie 		if (rq->cmd_flags & REQ_PREFLUSH)
112ae1b1539STejun Heo 			policy |= REQ_FSEQ_PREFLUSH;
113c888a8f9SJens Axboe 		if (!(fflags & (1UL << QUEUE_FLAG_FUA)) &&
114c888a8f9SJens Axboe 		    (rq->cmd_flags & REQ_FUA))
115ae1b1539STejun Heo 			policy |= REQ_FSEQ_POSTFLUSH;
116ae1b1539STejun Heo 	}
117ae1b1539STejun Heo 	return policy;
1188839a0e0STejun Heo }
1198839a0e0STejun Heo 
blk_flush_cur_seq(struct request * rq)120ae1b1539STejun Heo static unsigned int blk_flush_cur_seq(struct request *rq)
1218839a0e0STejun Heo {
122ae1b1539STejun Heo 	return 1 << ffz(rq->flush.seq);
1238839a0e0STejun Heo }
1248839a0e0STejun Heo 
blk_flush_restore_request(struct request * rq)125ae1b1539STejun Heo static void blk_flush_restore_request(struct request *rq)
12647f70d5aSTejun Heo {
12747f70d5aSTejun Heo 	/*
128ae1b1539STejun Heo 	 * After flush data completion, @rq->bio is %NULL but we need to
129ae1b1539STejun Heo 	 * complete the bio again.  @rq->biotail is guaranteed to equal the
130ae1b1539STejun Heo 	 * original @rq->bio.  Restore it.
13147f70d5aSTejun Heo 	 */
132ae1b1539STejun Heo 	rq->bio = rq->biotail;
133ae1b1539STejun Heo 
134ae1b1539STejun Heo 	/* make @rq a normal request */
135e8064021SChristoph Hellwig 	rq->rq_flags &= ~RQF_FLUSH_SEQ;
1364853abaaSJeff Moyer 	rq->end_io = rq->flush.saved_end_io;
137320ae51fSJens Axboe }
138320ae51fSJens Axboe 
blk_account_io_flush(struct request * rq)139b6866318SKonstantin Khlebnikov static void blk_account_io_flush(struct request *rq)
140b6866318SKonstantin Khlebnikov {
141f3fa33acSChristoph Hellwig 	struct block_device *part = rq->q->disk->part0;
142b6866318SKonstantin Khlebnikov 
143b6866318SKonstantin Khlebnikov 	part_stat_lock();
144b6866318SKonstantin Khlebnikov 	part_stat_inc(part, ios[STAT_FLUSH]);
145b6866318SKonstantin Khlebnikov 	part_stat_add(part, nsecs[STAT_FLUSH],
146b6866318SKonstantin Khlebnikov 		      ktime_get_ns() - rq->start_time_ns);
147b6866318SKonstantin Khlebnikov 	part_stat_unlock();
148b6866318SKonstantin Khlebnikov }
149b6866318SKonstantin Khlebnikov 
150ae1b1539STejun Heo /**
151ae1b1539STejun Heo  * blk_flush_complete_seq - complete flush sequence
1523140c3cfSOmar Sandoval  * @rq: PREFLUSH/FUA request being sequenced
1530bae352dSMing Lei  * @fq: flush queue
154ae1b1539STejun Heo  * @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
155ae1b1539STejun Heo  * @error: whether an error occurred
156ae1b1539STejun Heo  *
157ae1b1539STejun Heo  * @rq just completed @seq part of its flush sequence, record the
158ae1b1539STejun Heo  * completion and trigger the next step.
159ae1b1539STejun Heo  *
160ae1b1539STejun Heo  * CONTEXT:
1619809b4eeSChristoph Hellwig  * spin_lock_irq(fq->mq_flush_lock)
162ae1b1539STejun Heo  */
blk_flush_complete_seq(struct request * rq,struct blk_flush_queue * fq,unsigned int seq,blk_status_t error)163404b8f5aSJens Axboe static void blk_flush_complete_seq(struct request *rq,
1640bae352dSMing Lei 				   struct blk_flush_queue *fq,
1652a842acaSChristoph Hellwig 				   unsigned int seq, blk_status_t error)
1668839a0e0STejun Heo {
167ae1b1539STejun Heo 	struct request_queue *q = rq->q;
1687c94e1c1SMing Lei 	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
16916458cf3SBart Van Assche 	blk_opf_t cmd_flags;
1708839a0e0STejun Heo 
171ae1b1539STejun Heo 	BUG_ON(rq->flush.seq & seq);
172ae1b1539STejun Heo 	rq->flush.seq |= seq;
173190b02edSJens Axboe 	cmd_flags = rq->cmd_flags;
1748839a0e0STejun Heo 
175ae1b1539STejun Heo 	if (likely(!error))
176ae1b1539STejun Heo 		seq = blk_flush_cur_seq(rq);
177ae1b1539STejun Heo 	else
178ae1b1539STejun Heo 		seq = REQ_FSEQ_DONE;
1798839a0e0STejun Heo 
180ae1b1539STejun Heo 	switch (seq) {
181ae1b1539STejun Heo 	case REQ_FSEQ_PREFLUSH:
182ae1b1539STejun Heo 	case REQ_FSEQ_POSTFLUSH:
183ae1b1539STejun Heo 		/* queue for flush */
184ae1b1539STejun Heo 		if (list_empty(pending))
1857c94e1c1SMing Lei 			fq->flush_pending_since = jiffies;
186fe1e3955SChengming Zhou 		list_add_tail(&rq->queuelist, pending);
1878839a0e0STejun Heo 		break;
188ae1b1539STejun Heo 
189ae1b1539STejun Heo 	case REQ_FSEQ_DATA:
190b175c867SChengming Zhou 		fq->flush_data_in_flight++;
1919a67aa52SChristoph Hellwig 		spin_lock(&q->requeue_lock);
19281ada09cSChengming Zhou 		list_move(&rq->queuelist, &q->requeue_list);
1939a67aa52SChristoph Hellwig 		spin_unlock(&q->requeue_lock);
194214a4418SChristoph Hellwig 		blk_mq_kick_requeue_list(q);
195ae1b1539STejun Heo 		break;
196ae1b1539STejun Heo 
197ae1b1539STejun Heo 	case REQ_FSEQ_DONE:
19809d60c70STejun Heo 		/*
199b6866318SKonstantin Khlebnikov 		 * @rq was previously adjusted by blk_insert_flush() 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 		 */
20481ada09cSChengming Zhou 		list_del_init(&rq->queuelist);
205ae1b1539STejun Heo 		blk_flush_restore_request(rq);
206c8a446adSChristoph Hellwig 		blk_mq_end_request(rq, error);
2078839a0e0STejun Heo 		break;
208ae1b1539STejun Heo 
2098839a0e0STejun Heo 	default:
2108839a0e0STejun Heo 		BUG();
2118839a0e0STejun Heo 	}
212cde4c406SChristoph Hellwig 
213404b8f5aSJens Axboe 	blk_kick_flush(q, fq, cmd_flags);
2148839a0e0STejun Heo }
2158839a0e0STejun Heo 
flush_end_io(struct request * flush_rq,blk_status_t error)216de671d61SJens Axboe static enum rq_end_io_ret flush_end_io(struct request *flush_rq,
217de671d61SJens Axboe 				       blk_status_t error)
2188839a0e0STejun Heo {
219ae1b1539STejun Heo 	struct request_queue *q = flush_rq->q;
220320ae51fSJens Axboe 	struct list_head *running;
221ae1b1539STejun Heo 	struct request *rq, *n;
222320ae51fSJens Axboe 	unsigned long flags = 0;
223e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx);
2240048b483SMing Lei 
2250048b483SMing Lei 	/* release the tag's ownership to the req cloned from */
2267c94e1c1SMing Lei 	spin_lock_irqsave(&fq->mq_flush_lock, flags);
2278d699663SYufen Yu 
2280a467d0fSJens Axboe 	if (!req_ref_put_and_test(flush_rq)) {
2298d699663SYufen Yu 		fq->rq_status = error;
2308d699663SYufen Yu 		spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
231de671d61SJens Axboe 		return RQ_END_IO_NONE;
2328d699663SYufen Yu 	}
2338d699663SYufen Yu 
23484da7accSMing Lei 	blk_account_io_flush(flush_rq);
2359f16a667SMing Lei 	/*
2369f16a667SMing Lei 	 * Flush request has to be marked as IDLE when it is really ended
2379f16a667SMing Lei 	 * because its .end_io() is called from timeout code path too for
2389f16a667SMing Lei 	 * avoiding use-after-free.
2399f16a667SMing Lei 	 */
2409f16a667SMing Lei 	WRITE_ONCE(flush_rq->state, MQ_RQ_IDLE);
2418a751893SYe Bin 	if (fq->rq_status != BLK_STS_OK) {
2428d699663SYufen Yu 		error = fq->rq_status;
2438a751893SYe Bin 		fq->rq_status = BLK_STS_OK;
2448a751893SYe Bin 	}
2458d699663SYufen Yu 
2464e2f62e5SJens Axboe 	if (!q->elevator) {
247568f2700SMing Lei 		flush_rq->tag = BLK_MQ_NO_TAG;
2484e2f62e5SJens Axboe 	} else {
2494e2f62e5SJens Axboe 		blk_mq_put_driver_tag(flush_rq);
250568f2700SMing Lei 		flush_rq->internal_tag = BLK_MQ_NO_TAG;
2514e2f62e5SJens Axboe 	}
25218741986SChristoph Hellwig 
2537c94e1c1SMing Lei 	running = &fq->flush_queue[fq->flush_running_idx];
2547c94e1c1SMing Lei 	BUG_ON(fq->flush_pending_idx == fq->flush_running_idx);
255ae1b1539STejun Heo 
256ae1b1539STejun Heo 	/* account completion of the flush request */
2577c94e1c1SMing Lei 	fq->flush_running_idx ^= 1;
258320ae51fSJens Axboe 
259ae1b1539STejun Heo 	/* and push the waiting requests to the next stage */
26081ada09cSChengming Zhou 	list_for_each_entry_safe(rq, n, running, queuelist) {
261ae1b1539STejun Heo 		unsigned int seq = blk_flush_cur_seq(rq);
262ae1b1539STejun Heo 
263ae1b1539STejun Heo 		BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
264fe1e3955SChengming Zhou 		list_del_init(&rq->queuelist);
265404b8f5aSJens Axboe 		blk_flush_complete_seq(rq, fq, seq, error);
266ae1b1539STejun Heo 	}
267ae1b1539STejun Heo 
2687c94e1c1SMing Lei 	spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
269de671d61SJens Axboe 	return RQ_END_IO_NONE;
270320ae51fSJens Axboe }
271320ae51fSJens Axboe 
is_flush_rq(struct request * rq)272a9ed27a7SMing Lei bool is_flush_rq(struct request *rq)
273a9ed27a7SMing Lei {
274a9ed27a7SMing Lei 	return rq->end_io == flush_end_io;
275a9ed27a7SMing Lei }
276a9ed27a7SMing Lei 
277ae1b1539STejun Heo /**
278ae1b1539STejun Heo  * blk_kick_flush - consider issuing flush request
279ae1b1539STejun Heo  * @q: request_queue being kicked
2800bae352dSMing Lei  * @fq: flush queue
28184fca1b0SHannes Reinecke  * @flags: cmd_flags of the original request
2824fed947cSTejun Heo  *
283ae1b1539STejun Heo  * Flush related states of @q have changed, consider issuing flush request.
284ae1b1539STejun Heo  * Please read the comment at the top of this file for more info.
285ae1b1539STejun Heo  *
286ae1b1539STejun Heo  * CONTEXT:
2879809b4eeSChristoph Hellwig  * spin_lock_irq(fq->mq_flush_lock)
288ae1b1539STejun Heo  *
2898839a0e0STejun Heo  */
blk_kick_flush(struct request_queue * q,struct blk_flush_queue * fq,blk_opf_t flags)290404b8f5aSJens Axboe static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
29116458cf3SBart Van Assche 			   blk_opf_t flags)
292ae1b1539STejun Heo {
2937c94e1c1SMing Lei 	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
294ae1b1539STejun Heo 	struct request *first_rq =
29581ada09cSChengming Zhou 		list_first_entry(pending, struct request, queuelist);
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))
300404b8f5aSJens Axboe 		return;
301ae1b1539STejun Heo 
302b5718d6cSYufen Yu 	/* C2 and C3 */
303b175c867SChengming Zhou 	if (fq->flush_data_in_flight &&
304ae1b1539STejun Heo 	    time_before(jiffies,
3057c94e1c1SMing Lei 			fq->flush_pending_since + FLUSH_PENDING_TIMEOUT))
306404b8f5aSJens Axboe 		return;
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 	/*
317923218f6SMing Lei 	 * In case of none scheduler, borrow tag from the first request
318923218f6SMing Lei 	 * since they can't be in flight at the same time. And acquire
319923218f6SMing Lei 	 * the tag's ownership for flush req.
320923218f6SMing Lei 	 *
321923218f6SMing Lei 	 * In case of IO scheduler, flush rq need to borrow scheduler tag
322923218f6SMing Lei 	 * just for cheating put/get driver tag.
323f70ced09SMing Lei 	 */
324f70ced09SMing Lei 	flush_rq->mq_ctx = first_rq->mq_ctx;
325ea4f995eSJens Axboe 	flush_rq->mq_hctx = first_rq->mq_hctx;
3260048b483SMing Lei 
327c1e2b842SMing Lei 	if (!q->elevator) {
328923218f6SMing Lei 		flush_rq->tag = first_rq->tag;
329c1e2b842SMing Lei 
330c1e2b842SMing Lei 		/*
331c1e2b842SMing Lei 		 * We borrow data request's driver tag, so have to mark
332c1e2b842SMing Lei 		 * this flush request as INFLIGHT for avoiding double
333c1e2b842SMing Lei 		 * account of this driver tag
334c1e2b842SMing Lei 		 */
335c1e2b842SMing Lei 		flush_rq->rq_flags |= RQF_MQ_INFLIGHT;
336c1e2b842SMing Lei 	} else
337923218f6SMing Lei 		flush_rq->internal_tag = first_rq->internal_tag;
338320ae51fSJens Axboe 
33970fd7614SChristoph Hellwig 	flush_rq->cmd_flags = REQ_OP_FLUSH | REQ_PREFLUSH;
34084fca1b0SHannes Reinecke 	flush_rq->cmd_flags |= (flags & REQ_DRV) | (flags & REQ_FAILFAST_MASK);
341e8064021SChristoph Hellwig 	flush_rq->rq_flags |= RQF_FLUSH_SEQ;
3427ddab5deSMing Lei 	flush_rq->end_io = flush_end_io;
343c2da19edSMing Lei 	/*
344c2da19edSMing Lei 	 * Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
345c2da19edSMing Lei 	 * implied in refcount_inc_not_zero() called from
346c2da19edSMing Lei 	 * blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
347c2da19edSMing Lei 	 * and READ flush_rq->end_io
348c2da19edSMing Lei 	 */
349c2da19edSMing Lei 	smp_wmb();
3500a467d0fSJens Axboe 	req_ref_set(flush_rq, 1);
351ae1b1539STejun Heo 
3529a67aa52SChristoph Hellwig 	spin_lock(&q->requeue_lock);
3539a67aa52SChristoph Hellwig 	list_add_tail(&flush_rq->queuelist, &q->flush_list);
3549a67aa52SChristoph Hellwig 	spin_unlock(&q->requeue_lock);
3559a67aa52SChristoph Hellwig 
356214a4418SChristoph Hellwig 	blk_mq_kick_requeue_list(q);
357ae1b1539STejun Heo }
358ae1b1539STejun Heo 
mq_flush_data_end_io(struct request * rq,blk_status_t error)359de671d61SJens Axboe static enum rq_end_io_ret mq_flush_data_end_io(struct request *rq,
360de671d61SJens Axboe 					       blk_status_t error)
361320ae51fSJens Axboe {
362320ae51fSJens Axboe 	struct request_queue *q = rq->q;
363ea4f995eSJens Axboe 	struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
364e97c293cSMing Lei 	struct blk_mq_ctx *ctx = rq->mq_ctx;
365320ae51fSJens Axboe 	unsigned long flags;
366e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx);
367320ae51fSJens Axboe 
3684e2f62e5SJens Axboe 	if (q->elevator) {
3694e2f62e5SJens Axboe 		WARN_ON(rq->tag < 0);
3704e2f62e5SJens Axboe 		blk_mq_put_driver_tag(rq);
3714e2f62e5SJens Axboe 	}
3724e2f62e5SJens Axboe 
373320ae51fSJens Axboe 	/*
374320ae51fSJens Axboe 	 * After populating an empty queue, kick it to avoid stall.  Read
375320ae51fSJens Axboe 	 * the comment in flush_end_io().
376320ae51fSJens Axboe 	 */
3777c94e1c1SMing Lei 	spin_lock_irqsave(&fq->mq_flush_lock, flags);
378b175c867SChengming Zhou 	fq->flush_data_in_flight--;
37981ada09cSChengming Zhou 	/*
38081ada09cSChengming Zhou 	 * May have been corrupted by rq->rq_next reuse, we need to
38181ada09cSChengming Zhou 	 * re-initialize rq->queuelist before reusing it here.
38281ada09cSChengming Zhou 	 */
38381ada09cSChengming Zhou 	INIT_LIST_HEAD(&rq->queuelist);
384bd166ef1SJens Axboe 	blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error);
3857c94e1c1SMing Lei 	spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
386bd166ef1SJens Axboe 
38785bd6e61SJianchao Wang 	blk_mq_sched_restart(hctx);
388de671d61SJens Axboe 	return RQ_END_IO_NONE;
389320ae51fSJens Axboe }
390320ae51fSJens Axboe 
blk_rq_init_flush(struct request * rq)3910b573692SChristoph Hellwig static void blk_rq_init_flush(struct request *rq)
3920b573692SChristoph Hellwig {
3930b573692SChristoph Hellwig 	rq->flush.seq = 0;
3940b573692SChristoph Hellwig 	rq->rq_flags |= RQF_FLUSH_SEQ;
3950b573692SChristoph Hellwig 	rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
3960b573692SChristoph Hellwig 	rq->end_io = mq_flush_data_end_io;
3970b573692SChristoph Hellwig }
3980b573692SChristoph Hellwig 
399360f2648SChristoph Hellwig /*
400360f2648SChristoph Hellwig  * Insert a PREFLUSH/FUA request into the flush state machine.
401360f2648SChristoph Hellwig  * Returns true if the request has been consumed by the flush state machine,
402360f2648SChristoph Hellwig  * or false if the caller should continue to process it.
403ae1b1539STejun Heo  */
blk_insert_flush(struct request * rq)404360f2648SChristoph Hellwig bool blk_insert_flush(struct request *rq)
405ae1b1539STejun Heo {
406ae1b1539STejun Heo 	struct request_queue *q = rq->q;
407c888a8f9SJens Axboe 	unsigned long fflags = q->queue_flags;	/* may change, cache */
408ae1b1539STejun Heo 	unsigned int policy = blk_flush_policy(fflags, rq);
409e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
410ae1b1539STejun Heo 
411c1075e54SChristoph Hellwig 	/* FLUSH/FUA request must never be merged */
412c1075e54SChristoph Hellwig 	WARN_ON_ONCE(rq->bio != rq->biotail);
413c1075e54SChristoph Hellwig 
414ae1b1539STejun Heo 	/*
415ae1b1539STejun Heo 	 * @policy now records what operations need to be done.  Adjust
41628a8f0d3SMike Christie 	 * REQ_PREFLUSH and FUA for the driver.
417ae1b1539STejun Heo 	 */
41828a8f0d3SMike Christie 	rq->cmd_flags &= ~REQ_PREFLUSH;
419c888a8f9SJens Axboe 	if (!(fflags & (1UL << QUEUE_FLAG_FUA)))
4204fed947cSTejun Heo 		rq->cmd_flags &= ~REQ_FUA;
421ae1b1539STejun Heo 
422ae1b1539STejun Heo 	/*
423ae5b2ec8SJens Axboe 	 * REQ_PREFLUSH|REQ_FUA implies REQ_SYNC, so if we clear any
424ae5b2ec8SJens Axboe 	 * of those flags, we have to set REQ_SYNC to avoid skewing
425ae5b2ec8SJens Axboe 	 * the request accounting.
426ae5b2ec8SJens Axboe 	 */
427ae5b2ec8SJens Axboe 	rq->cmd_flags |= REQ_SYNC;
428ae5b2ec8SJens Axboe 
429c1075e54SChristoph Hellwig 	switch (policy) {
430c1075e54SChristoph Hellwig 	case 0:
431ae5b2ec8SJens Axboe 		/*
4324853abaaSJeff Moyer 		 * An empty flush handed down from a stacking driver may
4334853abaaSJeff Moyer 		 * translate into nothing if the underlying device does not
4344853abaaSJeff Moyer 		 * advertise a write-back cache.  In this case, simply
4354853abaaSJeff Moyer 		 * complete the request.
4364853abaaSJeff Moyer 		 */
437c8a446adSChristoph Hellwig 		blk_mq_end_request(rq, 0);
438360f2648SChristoph Hellwig 		return true;
439c1075e54SChristoph Hellwig 	case REQ_FSEQ_DATA:
4404853abaaSJeff Moyer 		/*
441c1075e54SChristoph Hellwig 		 * If there's data, but no flush is necessary, the request can
442c1075e54SChristoph Hellwig 		 * be processed directly without going through flush machinery.
443c1075e54SChristoph Hellwig 		 * Queue for normal execution.
444ae1b1539STejun Heo 		 */
445360f2648SChristoph Hellwig 		return false;
446615939a2SChristoph Hellwig 	case REQ_FSEQ_DATA | REQ_FSEQ_POSTFLUSH:
447615939a2SChristoph Hellwig 		/*
448615939a2SChristoph Hellwig 		 * Initialize the flush fields and completion handler to trigger
449615939a2SChristoph Hellwig 		 * the post flush, and then just pass the command on.
450615939a2SChristoph Hellwig 		 */
451615939a2SChristoph Hellwig 		blk_rq_init_flush(rq);
45228b24123SChengming Zhou 		rq->flush.seq |= REQ_FSEQ_PREFLUSH;
453615939a2SChristoph Hellwig 		spin_lock_irq(&fq->mq_flush_lock);
454b175c867SChengming Zhou 		fq->flush_data_in_flight++;
455615939a2SChristoph Hellwig 		spin_unlock_irq(&fq->mq_flush_lock);
456615939a2SChristoph Hellwig 		return false;
457c1075e54SChristoph Hellwig 	default:
4588839a0e0STejun Heo 		/*
459c1075e54SChristoph Hellwig 		 * Mark the request as part of a flush sequence and submit it
460c1075e54SChristoph Hellwig 		 * for further processing to the flush state machine.
4618839a0e0STejun Heo 		 */
4620b573692SChristoph Hellwig 		blk_rq_init_flush(rq);
4637c94e1c1SMing Lei 		spin_lock_irq(&fq->mq_flush_lock);
4640bae352dSMing Lei 		blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
4657c94e1c1SMing Lei 		spin_unlock_irq(&fq->mq_flush_lock);
466360f2648SChristoph Hellwig 		return true;
467ae1b1539STejun Heo 	}
468c1075e54SChristoph Hellwig }
469ae1b1539STejun Heo 
470ae1b1539STejun Heo /**
4718839a0e0STejun Heo  * blkdev_issue_flush - queue a flush
4728839a0e0STejun Heo  * @bdev:	blockdev to issue flush for
4738839a0e0STejun Heo  *
4748839a0e0STejun Heo  * Description:
4759398554fSChristoph Hellwig  *    Issue a flush for the block device in question.
4768839a0e0STejun Heo  */
blkdev_issue_flush(struct block_device * bdev)477c6bf3f0eSChristoph Hellwig int blkdev_issue_flush(struct block_device *bdev)
4788839a0e0STejun Heo {
479c6bf3f0eSChristoph Hellwig 	struct bio bio;
4808839a0e0STejun Heo 
48149add496SChristoph Hellwig 	bio_init(&bio, bdev, NULL, 0, REQ_OP_WRITE | REQ_PREFLUSH);
482c6bf3f0eSChristoph Hellwig 	return submit_bio_wait(&bio);
4838839a0e0STejun Heo }
4848839a0e0STejun Heo EXPORT_SYMBOL(blkdev_issue_flush);
485320ae51fSJens Axboe 
blk_alloc_flush_queue(int node,int cmd_size,gfp_t flags)486754a1572SGuoqing Jiang struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,
487754a1572SGuoqing Jiang 					      gfp_t flags)
488320ae51fSJens Axboe {
4897c94e1c1SMing Lei 	struct blk_flush_queue *fq;
4907c94e1c1SMing Lei 	int rq_sz = sizeof(struct request);
4911bcb1eadSMing Lei 
4925b202853SJianchao Wang 	fq = kzalloc_node(sizeof(*fq), flags, node);
4937c94e1c1SMing Lei 	if (!fq)
4947c94e1c1SMing Lei 		goto fail;
4951bcb1eadSMing Lei 
4967c94e1c1SMing Lei 	spin_lock_init(&fq->mq_flush_lock);
4977c94e1c1SMing Lei 
4986d247d7fSChristoph Hellwig 	rq_sz = round_up(rq_sz + cmd_size, cache_line_size());
4995b202853SJianchao Wang 	fq->flush_rq = kzalloc_node(rq_sz, flags, node);
5007c94e1c1SMing Lei 	if (!fq->flush_rq)
5017c94e1c1SMing Lei 		goto fail_rq;
5027c94e1c1SMing Lei 
5037c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_queue[0]);
5047c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_queue[1]);
5057c94e1c1SMing Lei 
5067c94e1c1SMing Lei 	return fq;
5077c94e1c1SMing Lei 
5087c94e1c1SMing Lei  fail_rq:
5097c94e1c1SMing Lei 	kfree(fq);
5107c94e1c1SMing Lei  fail:
5117c94e1c1SMing Lei 	return NULL;
5127c94e1c1SMing Lei }
5137c94e1c1SMing Lei 
blk_free_flush_queue(struct blk_flush_queue * fq)514ba483388SMing Lei void blk_free_flush_queue(struct blk_flush_queue *fq)
5157c94e1c1SMing Lei {
5167c94e1c1SMing Lei 	/* bio based request queue hasn't flush queue */
5177c94e1c1SMing Lei 	if (!fq)
5187c94e1c1SMing Lei 		return;
5197c94e1c1SMing Lei 
5207c94e1c1SMing Lei 	kfree(fq->flush_rq);
5217c94e1c1SMing Lei 	kfree(fq);
522320ae51fSJens Axboe }
523fb01a293SMing Lei 
524fb01a293SMing Lei /*
525fb01a293SMing Lei  * Allow driver to set its own lock class to fq->mq_flush_lock for
526fb01a293SMing Lei  * avoiding lockdep complaint.
527fb01a293SMing Lei  *
528fb01a293SMing Lei  * flush_end_io() may be called recursively from some driver, such as
529fb01a293SMing Lei  * nvme-loop, so lockdep may complain 'possible recursive locking' because
530fb01a293SMing Lei  * all 'struct blk_flush_queue' instance share same mq_flush_lock lock class
531fb01a293SMing Lei  * key. We need to assign different lock class for these driver's
532fb01a293SMing Lei  * fq->mq_flush_lock for avoiding the lockdep warning.
533fb01a293SMing Lei  *
534fb01a293SMing Lei  * Use dynamically allocated lock class key for each 'blk_flush_queue'
535fb01a293SMing Lei  * instance is over-kill, and more worse it introduces horrible boot delay
536fb01a293SMing Lei  * issue because synchronize_rcu() is implied in lockdep_unregister_key which
537fb01a293SMing Lei  * is called for each hctx release. SCSI probing may synchronously create and
538fb01a293SMing Lei  * destroy lots of MQ request_queues for non-existent devices, and some robot
539fb01a293SMing Lei  * test kernel always enable lockdep option. It is observed that more than half
540fb01a293SMing Lei  * an hour is taken during SCSI MQ probe with per-fq lock class.
541fb01a293SMing Lei  */
blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx * hctx,struct lock_class_key * key)542fb01a293SMing Lei void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
543fb01a293SMing Lei 		struct lock_class_key *key)
544fb01a293SMing Lei {
545fb01a293SMing Lei 	lockdep_set_class(&hctx->fq->mq_flush_lock, key);
546fb01a293SMing Lei }
547fb01a293SMing Lei EXPORT_SYMBOL_GPL(blk_mq_hctx_set_fq_lock_class);
548