xref: /openbmc/linux/block/blk-flush.c (revision 0281ed3cf44d2a7061ec3c1680e1f86e55ad57b9)
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>
71320ae51fSJens Axboe #include <linux/blk-mq.h>
728839a0e0STejun Heo 
738839a0e0STejun Heo #include "blk.h"
74320ae51fSJens Axboe #include "blk-mq.h"
750048b483SMing Lei #include "blk-mq-tag.h"
76bd166ef1SJens Axboe #include "blk-mq-sched.h"
778839a0e0STejun Heo 
783140c3cfSOmar Sandoval /* PREFLUSH/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 
95404b8f5aSJens Axboe static void blk_kick_flush(struct request_queue *q,
9684fca1b0SHannes Reinecke 			   struct blk_flush_queue *fq, unsigned int flags);
978839a0e0STejun Heo 
98*0281ed3cSChristoph Hellwig static inline struct blk_flush_queue *
99*0281ed3cSChristoph Hellwig blk_get_flush_queue(struct request_queue *q, struct blk_mq_ctx *ctx)
100*0281ed3cSChristoph Hellwig {
101*0281ed3cSChristoph Hellwig 	return blk_mq_map_queue(q, REQ_OP_FLUSH, ctx)->fq;
102*0281ed3cSChristoph Hellwig }
103*0281ed3cSChristoph Hellwig 
104c888a8f9SJens Axboe static unsigned int blk_flush_policy(unsigned long fflags, struct request *rq)
1058839a0e0STejun Heo {
106ae1b1539STejun Heo 	unsigned int policy = 0;
107ae1b1539STejun Heo 
108fa1bf42fSJeff Moyer 	if (blk_rq_sectors(rq))
109fa1bf42fSJeff Moyer 		policy |= REQ_FSEQ_DATA;
110fa1bf42fSJeff Moyer 
111c888a8f9SJens Axboe 	if (fflags & (1UL << QUEUE_FLAG_WC)) {
11228a8f0d3SMike Christie 		if (rq->cmd_flags & REQ_PREFLUSH)
113ae1b1539STejun Heo 			policy |= REQ_FSEQ_PREFLUSH;
114c888a8f9SJens Axboe 		if (!(fflags & (1UL << QUEUE_FLAG_FUA)) &&
115c888a8f9SJens Axboe 		    (rq->cmd_flags & REQ_FUA))
116ae1b1539STejun Heo 			policy |= REQ_FSEQ_POSTFLUSH;
117ae1b1539STejun Heo 	}
118ae1b1539STejun Heo 	return policy;
1198839a0e0STejun Heo }
1208839a0e0STejun Heo 
121ae1b1539STejun Heo static unsigned int blk_flush_cur_seq(struct request *rq)
1228839a0e0STejun Heo {
123ae1b1539STejun Heo 	return 1 << ffz(rq->flush.seq);
1248839a0e0STejun Heo }
1258839a0e0STejun Heo 
126ae1b1539STejun Heo static void blk_flush_restore_request(struct request *rq)
12747f70d5aSTejun Heo {
12847f70d5aSTejun Heo 	/*
129ae1b1539STejun Heo 	 * After flush data completion, @rq->bio is %NULL but we need to
130ae1b1539STejun Heo 	 * complete the bio again.  @rq->biotail is guaranteed to equal the
131ae1b1539STejun Heo 	 * original @rq->bio.  Restore it.
13247f70d5aSTejun Heo 	 */
133ae1b1539STejun Heo 	rq->bio = rq->biotail;
134ae1b1539STejun Heo 
135ae1b1539STejun Heo 	/* make @rq a normal request */
136e8064021SChristoph Hellwig 	rq->rq_flags &= ~RQF_FLUSH_SEQ;
1374853abaaSJeff Moyer 	rq->end_io = rq->flush.saved_end_io;
138320ae51fSJens Axboe }
139320ae51fSJens Axboe 
140404b8f5aSJens Axboe static void blk_flush_queue_rq(struct request *rq, bool add_front)
141320ae51fSJens Axboe {
1422b053acaSBart Van Assche 	blk_mq_add_to_requeue_list(rq, add_front, true);
14347f70d5aSTejun Heo }
14447f70d5aSTejun Heo 
145b6866318SKonstantin Khlebnikov static void blk_account_io_flush(struct request *rq)
146b6866318SKonstantin Khlebnikov {
1478446fe92SChristoph Hellwig 	struct block_device *part = rq->rq_disk->part0;
148b6866318SKonstantin Khlebnikov 
149b6866318SKonstantin Khlebnikov 	part_stat_lock();
150b6866318SKonstantin Khlebnikov 	part_stat_inc(part, ios[STAT_FLUSH]);
151b6866318SKonstantin Khlebnikov 	part_stat_add(part, nsecs[STAT_FLUSH],
152b6866318SKonstantin Khlebnikov 		      ktime_get_ns() - rq->start_time_ns);
153b6866318SKonstantin Khlebnikov 	part_stat_unlock();
154b6866318SKonstantin Khlebnikov }
155b6866318SKonstantin Khlebnikov 
156ae1b1539STejun Heo /**
157ae1b1539STejun Heo  * blk_flush_complete_seq - complete flush sequence
1583140c3cfSOmar Sandoval  * @rq: PREFLUSH/FUA request being sequenced
1590bae352dSMing Lei  * @fq: flush queue
160ae1b1539STejun Heo  * @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
161ae1b1539STejun Heo  * @error: whether an error occurred
162ae1b1539STejun Heo  *
163ae1b1539STejun Heo  * @rq just completed @seq part of its flush sequence, record the
164ae1b1539STejun Heo  * completion and trigger the next step.
165ae1b1539STejun Heo  *
166ae1b1539STejun Heo  * CONTEXT:
1679809b4eeSChristoph Hellwig  * spin_lock_irq(fq->mq_flush_lock)
168ae1b1539STejun Heo  */
169404b8f5aSJens Axboe static void blk_flush_complete_seq(struct request *rq,
1700bae352dSMing Lei 				   struct blk_flush_queue *fq,
1712a842acaSChristoph Hellwig 				   unsigned int seq, blk_status_t error)
1728839a0e0STejun Heo {
173ae1b1539STejun Heo 	struct request_queue *q = rq->q;
1747c94e1c1SMing Lei 	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
175190b02edSJens Axboe 	unsigned int cmd_flags;
1768839a0e0STejun Heo 
177ae1b1539STejun Heo 	BUG_ON(rq->flush.seq & seq);
178ae1b1539STejun Heo 	rq->flush.seq |= seq;
179190b02edSJens Axboe 	cmd_flags = rq->cmd_flags;
1808839a0e0STejun Heo 
181ae1b1539STejun Heo 	if (likely(!error))
182ae1b1539STejun Heo 		seq = blk_flush_cur_seq(rq);
183ae1b1539STejun Heo 	else
184ae1b1539STejun Heo 		seq = REQ_FSEQ_DONE;
1858839a0e0STejun Heo 
186ae1b1539STejun Heo 	switch (seq) {
187ae1b1539STejun Heo 	case REQ_FSEQ_PREFLUSH:
188ae1b1539STejun Heo 	case REQ_FSEQ_POSTFLUSH:
189ae1b1539STejun Heo 		/* queue for flush */
190ae1b1539STejun Heo 		if (list_empty(pending))
1917c94e1c1SMing Lei 			fq->flush_pending_since = jiffies;
192ae1b1539STejun Heo 		list_move_tail(&rq->flush.list, pending);
1938839a0e0STejun Heo 		break;
194ae1b1539STejun Heo 
195ae1b1539STejun Heo 	case REQ_FSEQ_DATA:
1967c94e1c1SMing Lei 		list_move_tail(&rq->flush.list, &fq->flush_data_in_flight);
197404b8f5aSJens Axboe 		blk_flush_queue_rq(rq, true);
198ae1b1539STejun Heo 		break;
199ae1b1539STejun Heo 
200ae1b1539STejun Heo 	case REQ_FSEQ_DONE:
20109d60c70STejun Heo 		/*
202b6866318SKonstantin Khlebnikov 		 * @rq was previously adjusted by blk_insert_flush() for
203ae1b1539STejun Heo 		 * flush sequencing and may already have gone through the
204ae1b1539STejun Heo 		 * flush data request completion path.  Restore @rq for
205ae1b1539STejun Heo 		 * normal completion and end it.
20609d60c70STejun Heo 		 */
207ae1b1539STejun Heo 		BUG_ON(!list_empty(&rq->queuelist));
208ae1b1539STejun Heo 		list_del_init(&rq->flush.list);
209ae1b1539STejun Heo 		blk_flush_restore_request(rq);
210c8a446adSChristoph Hellwig 		blk_mq_end_request(rq, error);
2118839a0e0STejun Heo 		break;
212ae1b1539STejun Heo 
2138839a0e0STejun Heo 	default:
2148839a0e0STejun Heo 		BUG();
2158839a0e0STejun Heo 	}
216cde4c406SChristoph Hellwig 
217404b8f5aSJens Axboe 	blk_kick_flush(q, fq, cmd_flags);
2188839a0e0STejun Heo }
2198839a0e0STejun Heo 
2202a842acaSChristoph Hellwig static void flush_end_io(struct request *flush_rq, blk_status_t error)
2218839a0e0STejun Heo {
222ae1b1539STejun Heo 	struct request_queue *q = flush_rq->q;
223320ae51fSJens Axboe 	struct list_head *running;
224ae1b1539STejun Heo 	struct request *rq, *n;
225320ae51fSJens Axboe 	unsigned long flags = 0;
226e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx);
2270048b483SMing Lei 
2280048b483SMing Lei 	/* release the tag's ownership to the req cloned from */
2297c94e1c1SMing Lei 	spin_lock_irqsave(&fq->mq_flush_lock, flags);
2308d699663SYufen Yu 
2318d699663SYufen Yu 	if (!refcount_dec_and_test(&flush_rq->ref)) {
2328d699663SYufen Yu 		fq->rq_status = error;
2338d699663SYufen Yu 		spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
2348d699663SYufen Yu 		return;
2358d699663SYufen Yu 	}
2368d699663SYufen Yu 
23784da7accSMing Lei 	blk_account_io_flush(flush_rq);
2389f16a667SMing Lei 	/*
2399f16a667SMing Lei 	 * Flush request has to be marked as IDLE when it is really ended
2409f16a667SMing Lei 	 * because its .end_io() is called from timeout code path too for
2419f16a667SMing Lei 	 * avoiding use-after-free.
2429f16a667SMing Lei 	 */
2439f16a667SMing Lei 	WRITE_ONCE(flush_rq->state, MQ_RQ_IDLE);
2448d699663SYufen Yu 	if (fq->rq_status != BLK_STS_OK)
2458d699663SYufen Yu 		error = fq->rq_status;
2468d699663SYufen Yu 
2474e2f62e5SJens Axboe 	if (!q->elevator) {
248568f2700SMing Lei 		flush_rq->tag = BLK_MQ_NO_TAG;
2494e2f62e5SJens Axboe 	} else {
2504e2f62e5SJens Axboe 		blk_mq_put_driver_tag(flush_rq);
251568f2700SMing Lei 		flush_rq->internal_tag = BLK_MQ_NO_TAG;
2524e2f62e5SJens Axboe 	}
25318741986SChristoph Hellwig 
2547c94e1c1SMing Lei 	running = &fq->flush_queue[fq->flush_running_idx];
2557c94e1c1SMing Lei 	BUG_ON(fq->flush_pending_idx == fq->flush_running_idx);
256ae1b1539STejun Heo 
257ae1b1539STejun Heo 	/* account completion of the flush request */
2587c94e1c1SMing Lei 	fq->flush_running_idx ^= 1;
259320ae51fSJens Axboe 
260ae1b1539STejun Heo 	/* and push the waiting requests to the next stage */
261ae1b1539STejun Heo 	list_for_each_entry_safe(rq, n, running, flush.list) {
262ae1b1539STejun Heo 		unsigned int seq = blk_flush_cur_seq(rq);
263ae1b1539STejun Heo 
264ae1b1539STejun Heo 		BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
265404b8f5aSJens Axboe 		blk_flush_complete_seq(rq, fq, seq, error);
266ae1b1539STejun Heo 	}
267ae1b1539STejun Heo 
2687c94e1c1SMing Lei 	spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
269320ae51fSJens Axboe }
270320ae51fSJens Axboe 
271a9ed27a7SMing Lei bool is_flush_rq(struct request *rq)
272a9ed27a7SMing Lei {
273a9ed27a7SMing Lei 	return rq->end_io == flush_end_io;
274a9ed27a7SMing Lei }
275a9ed27a7SMing Lei 
276ae1b1539STejun Heo /**
277ae1b1539STejun Heo  * blk_kick_flush - consider issuing flush request
278ae1b1539STejun Heo  * @q: request_queue being kicked
2790bae352dSMing Lei  * @fq: flush queue
28084fca1b0SHannes Reinecke  * @flags: cmd_flags of the original request
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:
2869809b4eeSChristoph Hellwig  * spin_lock_irq(fq->mq_flush_lock)
287ae1b1539STejun Heo  *
2888839a0e0STejun Heo  */
289404b8f5aSJens Axboe static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
29084fca1b0SHannes Reinecke 			   unsigned int flags)
291ae1b1539STejun Heo {
2927c94e1c1SMing Lei 	struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
293ae1b1539STejun Heo 	struct request *first_rq =
294ae1b1539STejun Heo 		list_first_entry(pending, struct request, flush.list);
2957c94e1c1SMing Lei 	struct request *flush_rq = fq->flush_rq;
296ae1b1539STejun Heo 
297ae1b1539STejun Heo 	/* C1 described at the top of this file */
2987c94e1c1SMing Lei 	if (fq->flush_pending_idx != fq->flush_running_idx || list_empty(pending))
299404b8f5aSJens Axboe 		return;
300ae1b1539STejun Heo 
301b5718d6cSYufen Yu 	/* C2 and C3 */
302b5718d6cSYufen Yu 	if (!list_empty(&fq->flush_data_in_flight) &&
303ae1b1539STejun Heo 	    time_before(jiffies,
3047c94e1c1SMing Lei 			fq->flush_pending_since + FLUSH_PENDING_TIMEOUT))
305404b8f5aSJens Axboe 		return;
306ae1b1539STejun Heo 
307ae1b1539STejun Heo 	/*
308ae1b1539STejun Heo 	 * Issue flush and toggle pending_idx.  This makes pending_idx
309ae1b1539STejun Heo 	 * different from running_idx, which means flush is in flight.
310ae1b1539STejun Heo 	 */
3117c94e1c1SMing Lei 	fq->flush_pending_idx ^= 1;
31218741986SChristoph Hellwig 
3137ddab5deSMing Lei 	blk_rq_init(q, flush_rq);
314f70ced09SMing Lei 
315f70ced09SMing Lei 	/*
316923218f6SMing Lei 	 * In case of none scheduler, borrow tag from the first request
317923218f6SMing Lei 	 * since they can't be in flight at the same time. And acquire
318923218f6SMing Lei 	 * the tag's ownership for flush req.
319923218f6SMing Lei 	 *
320923218f6SMing Lei 	 * In case of IO scheduler, flush rq need to borrow scheduler tag
321923218f6SMing Lei 	 * just for cheating put/get driver tag.
322f70ced09SMing Lei 	 */
323f70ced09SMing Lei 	flush_rq->mq_ctx = first_rq->mq_ctx;
324ea4f995eSJens Axboe 	flush_rq->mq_hctx = first_rq->mq_hctx;
3250048b483SMing Lei 
326c1e2b842SMing Lei 	if (!q->elevator) {
327923218f6SMing Lei 		flush_rq->tag = first_rq->tag;
328c1e2b842SMing Lei 
329c1e2b842SMing Lei 		/*
330c1e2b842SMing Lei 		 * We borrow data request's driver tag, so have to mark
331c1e2b842SMing Lei 		 * this flush request as INFLIGHT for avoiding double
332c1e2b842SMing Lei 		 * account of this driver tag
333c1e2b842SMing Lei 		 */
334c1e2b842SMing Lei 		flush_rq->rq_flags |= RQF_MQ_INFLIGHT;
335c1e2b842SMing Lei 	} else
336923218f6SMing Lei 		flush_rq->internal_tag = first_rq->internal_tag;
337320ae51fSJens Axboe 
33870fd7614SChristoph Hellwig 	flush_rq->cmd_flags = REQ_OP_FLUSH | REQ_PREFLUSH;
33984fca1b0SHannes Reinecke 	flush_rq->cmd_flags |= (flags & REQ_DRV) | (flags & REQ_FAILFAST_MASK);
340e8064021SChristoph Hellwig 	flush_rq->rq_flags |= RQF_FLUSH_SEQ;
3417ddab5deSMing Lei 	flush_rq->rq_disk = first_rq->rq_disk;
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();
350c2da19edSMing Lei 	refcount_set(&flush_rq->ref, 1);
351ae1b1539STejun Heo 
352404b8f5aSJens Axboe 	blk_flush_queue_rq(flush_rq, false);
353ae1b1539STejun Heo }
354ae1b1539STejun Heo 
3552a842acaSChristoph Hellwig static void mq_flush_data_end_io(struct request *rq, blk_status_t error)
356320ae51fSJens Axboe {
357320ae51fSJens Axboe 	struct request_queue *q = rq->q;
358ea4f995eSJens Axboe 	struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
359e97c293cSMing Lei 	struct blk_mq_ctx *ctx = rq->mq_ctx;
360320ae51fSJens Axboe 	unsigned long flags;
361e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx);
362320ae51fSJens Axboe 
3634e2f62e5SJens Axboe 	if (q->elevator) {
3644e2f62e5SJens Axboe 		WARN_ON(rq->tag < 0);
3654e2f62e5SJens Axboe 		blk_mq_put_driver_tag(rq);
3664e2f62e5SJens Axboe 	}
3674e2f62e5SJens Axboe 
368320ae51fSJens Axboe 	/*
369320ae51fSJens Axboe 	 * After populating an empty queue, kick it to avoid stall.  Read
370320ae51fSJens Axboe 	 * the comment in flush_end_io().
371320ae51fSJens Axboe 	 */
3727c94e1c1SMing Lei 	spin_lock_irqsave(&fq->mq_flush_lock, flags);
373bd166ef1SJens Axboe 	blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error);
3747c94e1c1SMing Lei 	spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
375bd166ef1SJens Axboe 
37685bd6e61SJianchao Wang 	blk_mq_sched_restart(hctx);
377320ae51fSJens Axboe }
378320ae51fSJens Axboe 
379ae1b1539STejun Heo /**
3803140c3cfSOmar Sandoval  * blk_insert_flush - insert a new PREFLUSH/FUA request
381ae1b1539STejun Heo  * @rq: request to insert
382ae1b1539STejun Heo  *
383b710a480SJens Axboe  * To be called from __elv_add_request() for %ELEVATOR_INSERT_FLUSH insertions.
384320ae51fSJens Axboe  * or __blk_mq_run_hw_queue() to dispatch request.
385ae1b1539STejun Heo  * @rq is being submitted.  Analyze what needs to be done and put it on the
386ae1b1539STejun Heo  * right queue.
387ae1b1539STejun Heo  */
3882b504bd4SMing Lei void blk_insert_flush(struct request *rq)
389ae1b1539STejun Heo {
390ae1b1539STejun Heo 	struct request_queue *q = rq->q;
391c888a8f9SJens Axboe 	unsigned long fflags = q->queue_flags;	/* may change, cache */
392ae1b1539STejun Heo 	unsigned int policy = blk_flush_policy(fflags, rq);
393e97c293cSMing Lei 	struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
394ae1b1539STejun Heo 
395ae1b1539STejun Heo 	/*
396ae1b1539STejun Heo 	 * @policy now records what operations need to be done.  Adjust
39728a8f0d3SMike Christie 	 * REQ_PREFLUSH and FUA for the driver.
398ae1b1539STejun Heo 	 */
39928a8f0d3SMike Christie 	rq->cmd_flags &= ~REQ_PREFLUSH;
400c888a8f9SJens Axboe 	if (!(fflags & (1UL << QUEUE_FLAG_FUA)))
4014fed947cSTejun Heo 		rq->cmd_flags &= ~REQ_FUA;
402ae1b1539STejun Heo 
403ae1b1539STejun Heo 	/*
404ae5b2ec8SJens Axboe 	 * REQ_PREFLUSH|REQ_FUA implies REQ_SYNC, so if we clear any
405ae5b2ec8SJens Axboe 	 * of those flags, we have to set REQ_SYNC to avoid skewing
406ae5b2ec8SJens Axboe 	 * the request accounting.
407ae5b2ec8SJens Axboe 	 */
408ae5b2ec8SJens Axboe 	rq->cmd_flags |= REQ_SYNC;
409ae5b2ec8SJens Axboe 
410ae5b2ec8SJens Axboe 	/*
4114853abaaSJeff Moyer 	 * An empty flush handed down from a stacking driver may
4124853abaaSJeff Moyer 	 * translate into nothing if the underlying device does not
4134853abaaSJeff Moyer 	 * advertise a write-back cache.  In this case, simply
4144853abaaSJeff Moyer 	 * complete the request.
4154853abaaSJeff Moyer 	 */
4164853abaaSJeff Moyer 	if (!policy) {
417c8a446adSChristoph Hellwig 		blk_mq_end_request(rq, 0);
4182b504bd4SMing Lei 		return;
4194853abaaSJeff Moyer 	}
4204853abaaSJeff Moyer 
421834f9f61SJeff Moyer 	BUG_ON(rq->bio != rq->biotail); /*assumes zero or single bio rq */
4224853abaaSJeff Moyer 
4234853abaaSJeff Moyer 	/*
424ae1b1539STejun Heo 	 * If there's data but flush is not necessary, the request can be
425ae1b1539STejun Heo 	 * processed directly without going through flush machinery.  Queue
426ae1b1539STejun Heo 	 * for normal execution.
427ae1b1539STejun Heo 	 */
428ae1b1539STejun Heo 	if ((policy & REQ_FSEQ_DATA) &&
4292b504bd4SMing Lei 	    !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
4302b504bd4SMing Lei 		blk_mq_request_bypass_insert(rq, false, true);
4312b504bd4SMing Lei 		return;
4322b504bd4SMing Lei 	}
4338839a0e0STejun Heo 
4348839a0e0STejun Heo 	/*
435ae1b1539STejun Heo 	 * @rq should go through flush machinery.  Mark it part of flush
436ae1b1539STejun Heo 	 * sequence and submit for further processing.
4378839a0e0STejun Heo 	 */
438ae1b1539STejun Heo 	memset(&rq->flush, 0, sizeof(rq->flush));
439ae1b1539STejun Heo 	INIT_LIST_HEAD(&rq->flush.list);
440e8064021SChristoph Hellwig 	rq->rq_flags |= RQF_FLUSH_SEQ;
4414853abaaSJeff Moyer 	rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
4427e992f84SJens Axboe 
443320ae51fSJens Axboe 	rq->end_io = mq_flush_data_end_io;
444320ae51fSJens Axboe 
4457c94e1c1SMing Lei 	spin_lock_irq(&fq->mq_flush_lock);
4460bae352dSMing Lei 	blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
4477c94e1c1SMing Lei 	spin_unlock_irq(&fq->mq_flush_lock);
448ae1b1539STejun Heo }
449ae1b1539STejun Heo 
450ae1b1539STejun Heo /**
4518839a0e0STejun Heo  * blkdev_issue_flush - queue a flush
4528839a0e0STejun Heo  * @bdev:	blockdev to issue flush for
4538839a0e0STejun Heo  *
4548839a0e0STejun Heo  * Description:
4559398554fSChristoph Hellwig  *    Issue a flush for the block device in question.
4568839a0e0STejun Heo  */
457c6bf3f0eSChristoph Hellwig int blkdev_issue_flush(struct block_device *bdev)
4588839a0e0STejun Heo {
459c6bf3f0eSChristoph Hellwig 	struct bio bio;
4608839a0e0STejun Heo 
461c6bf3f0eSChristoph Hellwig 	bio_init(&bio, NULL, 0);
462c6bf3f0eSChristoph Hellwig 	bio_set_dev(&bio, bdev);
463c6bf3f0eSChristoph Hellwig 	bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
464c6bf3f0eSChristoph Hellwig 	return submit_bio_wait(&bio);
4658839a0e0STejun Heo }
4668839a0e0STejun Heo EXPORT_SYMBOL(blkdev_issue_flush);
467320ae51fSJens Axboe 
468754a1572SGuoqing Jiang struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,
469754a1572SGuoqing Jiang 					      gfp_t flags)
470320ae51fSJens Axboe {
4717c94e1c1SMing Lei 	struct blk_flush_queue *fq;
4727c94e1c1SMing Lei 	int rq_sz = sizeof(struct request);
4731bcb1eadSMing Lei 
4745b202853SJianchao Wang 	fq = kzalloc_node(sizeof(*fq), flags, node);
4757c94e1c1SMing Lei 	if (!fq)
4767c94e1c1SMing Lei 		goto fail;
4771bcb1eadSMing Lei 
4787c94e1c1SMing Lei 	spin_lock_init(&fq->mq_flush_lock);
4797c94e1c1SMing Lei 
4806d247d7fSChristoph Hellwig 	rq_sz = round_up(rq_sz + cmd_size, cache_line_size());
4815b202853SJianchao Wang 	fq->flush_rq = kzalloc_node(rq_sz, flags, node);
4827c94e1c1SMing Lei 	if (!fq->flush_rq)
4837c94e1c1SMing Lei 		goto fail_rq;
4847c94e1c1SMing Lei 
4857c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_queue[0]);
4867c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_queue[1]);
4877c94e1c1SMing Lei 	INIT_LIST_HEAD(&fq->flush_data_in_flight);
4887c94e1c1SMing Lei 
4897c94e1c1SMing Lei 	return fq;
4907c94e1c1SMing Lei 
4917c94e1c1SMing Lei  fail_rq:
4927c94e1c1SMing Lei 	kfree(fq);
4937c94e1c1SMing Lei  fail:
4947c94e1c1SMing Lei 	return NULL;
4957c94e1c1SMing Lei }
4967c94e1c1SMing Lei 
497ba483388SMing Lei void blk_free_flush_queue(struct blk_flush_queue *fq)
4987c94e1c1SMing Lei {
4997c94e1c1SMing Lei 	/* bio based request queue hasn't flush queue */
5007c94e1c1SMing Lei 	if (!fq)
5017c94e1c1SMing Lei 		return;
5027c94e1c1SMing Lei 
5037c94e1c1SMing Lei 	kfree(fq->flush_rq);
5047c94e1c1SMing Lei 	kfree(fq);
505320ae51fSJens Axboe }
506fb01a293SMing Lei 
507fb01a293SMing Lei /*
508fb01a293SMing Lei  * Allow driver to set its own lock class to fq->mq_flush_lock for
509fb01a293SMing Lei  * avoiding lockdep complaint.
510fb01a293SMing Lei  *
511fb01a293SMing Lei  * flush_end_io() may be called recursively from some driver, such as
512fb01a293SMing Lei  * nvme-loop, so lockdep may complain 'possible recursive locking' because
513fb01a293SMing Lei  * all 'struct blk_flush_queue' instance share same mq_flush_lock lock class
514fb01a293SMing Lei  * key. We need to assign different lock class for these driver's
515fb01a293SMing Lei  * fq->mq_flush_lock for avoiding the lockdep warning.
516fb01a293SMing Lei  *
517fb01a293SMing Lei  * Use dynamically allocated lock class key for each 'blk_flush_queue'
518fb01a293SMing Lei  * instance is over-kill, and more worse it introduces horrible boot delay
519fb01a293SMing Lei  * issue because synchronize_rcu() is implied in lockdep_unregister_key which
520fb01a293SMing Lei  * is called for each hctx release. SCSI probing may synchronously create and
521fb01a293SMing Lei  * destroy lots of MQ request_queues for non-existent devices, and some robot
522fb01a293SMing Lei  * test kernel always enable lockdep option. It is observed that more than half
523fb01a293SMing Lei  * an hour is taken during SCSI MQ probe with per-fq lock class.
524fb01a293SMing Lei  */
525fb01a293SMing Lei void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
526fb01a293SMing Lei 		struct lock_class_key *key)
527fb01a293SMing Lei {
528fb01a293SMing Lei 	lockdep_set_class(&hctx->fq->mq_flush_lock, key);
529fb01a293SMing Lei }
530fb01a293SMing Lei EXPORT_SYMBOL_GPL(blk_mq_hctx_set_fq_lock_class);
531