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> 72b3c6a599SBart Van Assche #include <linux/lockdep.h> 738839a0e0STejun Heo 748839a0e0STejun Heo #include "blk.h" 75320ae51fSJens Axboe #include "blk-mq.h" 760048b483SMing Lei #include "blk-mq-tag.h" 77bd166ef1SJens Axboe #include "blk-mq-sched.h" 788839a0e0STejun Heo 793140c3cfSOmar Sandoval /* PREFLUSH/FUA sequences */ 804fed947cSTejun Heo enum { 81ae1b1539STejun Heo REQ_FSEQ_PREFLUSH = (1 << 0), /* pre-flushing in progress */ 82ae1b1539STejun Heo REQ_FSEQ_DATA = (1 << 1), /* data write in progress */ 83ae1b1539STejun Heo REQ_FSEQ_POSTFLUSH = (1 << 2), /* post-flushing in progress */ 84ae1b1539STejun Heo REQ_FSEQ_DONE = (1 << 3), 85ae1b1539STejun Heo 86ae1b1539STejun Heo REQ_FSEQ_ACTIONS = REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA | 87ae1b1539STejun Heo REQ_FSEQ_POSTFLUSH, 88ae1b1539STejun Heo 89ae1b1539STejun Heo /* 90ae1b1539STejun Heo * If flush has been pending longer than the following timeout, 91ae1b1539STejun Heo * it's issued even if flush_data requests are still in flight. 92ae1b1539STejun Heo */ 93ae1b1539STejun Heo FLUSH_PENDING_TIMEOUT = 5 * HZ, 944fed947cSTejun Heo }; 954fed947cSTejun Heo 96404b8f5aSJens Axboe static void blk_kick_flush(struct request_queue *q, 9784fca1b0SHannes Reinecke struct blk_flush_queue *fq, unsigned int flags); 988839a0e0STejun Heo 99c888a8f9SJens Axboe static unsigned int blk_flush_policy(unsigned long fflags, struct request *rq) 1008839a0e0STejun Heo { 101ae1b1539STejun Heo unsigned int policy = 0; 102ae1b1539STejun Heo 103fa1bf42fSJeff Moyer if (blk_rq_sectors(rq)) 104fa1bf42fSJeff Moyer policy |= REQ_FSEQ_DATA; 105fa1bf42fSJeff Moyer 106c888a8f9SJens Axboe if (fflags & (1UL << QUEUE_FLAG_WC)) { 10728a8f0d3SMike Christie if (rq->cmd_flags & REQ_PREFLUSH) 108ae1b1539STejun Heo policy |= REQ_FSEQ_PREFLUSH; 109c888a8f9SJens Axboe if (!(fflags & (1UL << QUEUE_FLAG_FUA)) && 110c888a8f9SJens Axboe (rq->cmd_flags & REQ_FUA)) 111ae1b1539STejun Heo policy |= REQ_FSEQ_POSTFLUSH; 112ae1b1539STejun Heo } 113ae1b1539STejun Heo return policy; 1148839a0e0STejun Heo } 1158839a0e0STejun Heo 116ae1b1539STejun Heo static unsigned int blk_flush_cur_seq(struct request *rq) 1178839a0e0STejun Heo { 118ae1b1539STejun Heo return 1 << ffz(rq->flush.seq); 1198839a0e0STejun Heo } 1208839a0e0STejun Heo 121ae1b1539STejun Heo static void blk_flush_restore_request(struct request *rq) 12247f70d5aSTejun Heo { 12347f70d5aSTejun Heo /* 124ae1b1539STejun Heo * After flush data completion, @rq->bio is %NULL but we need to 125ae1b1539STejun Heo * complete the bio again. @rq->biotail is guaranteed to equal the 126ae1b1539STejun Heo * original @rq->bio. Restore it. 12747f70d5aSTejun Heo */ 128ae1b1539STejun Heo rq->bio = rq->biotail; 129ae1b1539STejun Heo 130ae1b1539STejun Heo /* make @rq a normal request */ 131e8064021SChristoph Hellwig rq->rq_flags &= ~RQF_FLUSH_SEQ; 1324853abaaSJeff Moyer rq->end_io = rq->flush.saved_end_io; 133320ae51fSJens Axboe } 134320ae51fSJens Axboe 135404b8f5aSJens Axboe static void blk_flush_queue_rq(struct request *rq, bool add_front) 136320ae51fSJens Axboe { 1372b053acaSBart Van Assche blk_mq_add_to_requeue_list(rq, add_front, true); 13847f70d5aSTejun Heo } 13947f70d5aSTejun Heo 140b6866318SKonstantin Khlebnikov static void blk_account_io_flush(struct request *rq) 141b6866318SKonstantin Khlebnikov { 142b6866318SKonstantin Khlebnikov struct hd_struct *part = &rq->rq_disk->part0; 143b6866318SKonstantin Khlebnikov 144b6866318SKonstantin Khlebnikov part_stat_lock(); 145b6866318SKonstantin Khlebnikov part_stat_inc(part, ios[STAT_FLUSH]); 146b6866318SKonstantin Khlebnikov part_stat_add(part, nsecs[STAT_FLUSH], 147b6866318SKonstantin Khlebnikov ktime_get_ns() - rq->start_time_ns); 148b6866318SKonstantin Khlebnikov part_stat_unlock(); 149b6866318SKonstantin Khlebnikov } 150b6866318SKonstantin Khlebnikov 151ae1b1539STejun Heo /** 152ae1b1539STejun Heo * blk_flush_complete_seq - complete flush sequence 1533140c3cfSOmar Sandoval * @rq: PREFLUSH/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: 1629809b4eeSChristoph Hellwig * spin_lock_irq(fq->mq_flush_lock) 163ae1b1539STejun Heo */ 164404b8f5aSJens Axboe static void blk_flush_complete_seq(struct request *rq, 1650bae352dSMing Lei struct blk_flush_queue *fq, 1662a842acaSChristoph Hellwig unsigned int seq, blk_status_t error) 1678839a0e0STejun Heo { 168ae1b1539STejun Heo struct request_queue *q = rq->q; 1697c94e1c1SMing Lei struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx]; 170190b02edSJens Axboe unsigned int cmd_flags; 1718839a0e0STejun Heo 172ae1b1539STejun Heo BUG_ON(rq->flush.seq & seq); 173ae1b1539STejun Heo rq->flush.seq |= seq; 174190b02edSJens Axboe cmd_flags = rq->cmd_flags; 1758839a0e0STejun Heo 176ae1b1539STejun Heo if (likely(!error)) 177ae1b1539STejun Heo seq = blk_flush_cur_seq(rq); 178ae1b1539STejun Heo else 179ae1b1539STejun Heo seq = REQ_FSEQ_DONE; 1808839a0e0STejun Heo 181ae1b1539STejun Heo switch (seq) { 182ae1b1539STejun Heo case REQ_FSEQ_PREFLUSH: 183ae1b1539STejun Heo case REQ_FSEQ_POSTFLUSH: 184ae1b1539STejun Heo /* queue for flush */ 185ae1b1539STejun Heo if (list_empty(pending)) 1867c94e1c1SMing Lei fq->flush_pending_since = jiffies; 187ae1b1539STejun Heo list_move_tail(&rq->flush.list, pending); 1888839a0e0STejun Heo break; 189ae1b1539STejun Heo 190ae1b1539STejun Heo case REQ_FSEQ_DATA: 1917c94e1c1SMing Lei list_move_tail(&rq->flush.list, &fq->flush_data_in_flight); 192404b8f5aSJens Axboe blk_flush_queue_rq(rq, true); 193ae1b1539STejun Heo break; 194ae1b1539STejun Heo 195ae1b1539STejun Heo case REQ_FSEQ_DONE: 19609d60c70STejun Heo /* 197b6866318SKonstantin Khlebnikov * @rq was previously adjusted by blk_insert_flush() for 198ae1b1539STejun Heo * flush sequencing and may already have gone through the 199ae1b1539STejun Heo * flush data request completion path. Restore @rq for 200ae1b1539STejun Heo * normal completion and end it. 20109d60c70STejun Heo */ 202ae1b1539STejun Heo BUG_ON(!list_empty(&rq->queuelist)); 203ae1b1539STejun Heo list_del_init(&rq->flush.list); 204ae1b1539STejun Heo blk_flush_restore_request(rq); 205c8a446adSChristoph Hellwig blk_mq_end_request(rq, error); 2068839a0e0STejun Heo break; 207ae1b1539STejun Heo 2088839a0e0STejun Heo default: 2098839a0e0STejun Heo BUG(); 2108839a0e0STejun Heo } 211cde4c406SChristoph Hellwig 212404b8f5aSJens Axboe blk_kick_flush(q, fq, cmd_flags); 2138839a0e0STejun Heo } 2148839a0e0STejun Heo 2152a842acaSChristoph Hellwig static void flush_end_io(struct request *flush_rq, blk_status_t error) 2168839a0e0STejun Heo { 217ae1b1539STejun Heo struct request_queue *q = flush_rq->q; 218320ae51fSJens Axboe struct list_head *running; 219ae1b1539STejun Heo struct request *rq, *n; 220320ae51fSJens Axboe unsigned long flags = 0; 221e97c293cSMing Lei struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx); 2220048b483SMing Lei 223b6866318SKonstantin Khlebnikov blk_account_io_flush(flush_rq); 224b6866318SKonstantin Khlebnikov 2250048b483SMing Lei /* release the tag's ownership to the req cloned from */ 2267c94e1c1SMing Lei spin_lock_irqsave(&fq->mq_flush_lock, flags); 2278d699663SYufen Yu 2288d699663SYufen Yu if (!refcount_dec_and_test(&flush_rq->ref)) { 2298d699663SYufen Yu fq->rq_status = error; 2308d699663SYufen Yu spin_unlock_irqrestore(&fq->mq_flush_lock, flags); 2318d699663SYufen Yu return; 2328d699663SYufen Yu } 2338d699663SYufen Yu 2348d699663SYufen Yu if (fq->rq_status != BLK_STS_OK) 2358d699663SYufen Yu error = fq->rq_status; 2368d699663SYufen Yu 2374e2f62e5SJens Axboe if (!q->elevator) { 238*568f2700SMing Lei flush_rq->tag = BLK_MQ_NO_TAG; 2394e2f62e5SJens Axboe } else { 2404e2f62e5SJens Axboe blk_mq_put_driver_tag(flush_rq); 241*568f2700SMing Lei flush_rq->internal_tag = BLK_MQ_NO_TAG; 2424e2f62e5SJens Axboe } 24318741986SChristoph Hellwig 2447c94e1c1SMing Lei running = &fq->flush_queue[fq->flush_running_idx]; 2457c94e1c1SMing Lei BUG_ON(fq->flush_pending_idx == fq->flush_running_idx); 246ae1b1539STejun Heo 247ae1b1539STejun Heo /* account completion of the flush request */ 2487c94e1c1SMing Lei fq->flush_running_idx ^= 1; 249320ae51fSJens Axboe 250ae1b1539STejun Heo /* and push the waiting requests to the next stage */ 251ae1b1539STejun Heo list_for_each_entry_safe(rq, n, running, flush.list) { 252ae1b1539STejun Heo unsigned int seq = blk_flush_cur_seq(rq); 253ae1b1539STejun Heo 254ae1b1539STejun Heo BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH); 255404b8f5aSJens Axboe blk_flush_complete_seq(rq, fq, seq, error); 256ae1b1539STejun Heo } 257ae1b1539STejun Heo 2587c94e1c1SMing Lei spin_unlock_irqrestore(&fq->mq_flush_lock, flags); 259320ae51fSJens Axboe } 260320ae51fSJens Axboe 261ae1b1539STejun Heo /** 262ae1b1539STejun Heo * blk_kick_flush - consider issuing flush request 263ae1b1539STejun Heo * @q: request_queue being kicked 2640bae352dSMing Lei * @fq: flush queue 26584fca1b0SHannes Reinecke * @flags: cmd_flags of the original request 2664fed947cSTejun Heo * 267ae1b1539STejun Heo * Flush related states of @q have changed, consider issuing flush request. 268ae1b1539STejun Heo * Please read the comment at the top of this file for more info. 269ae1b1539STejun Heo * 270ae1b1539STejun Heo * CONTEXT: 2719809b4eeSChristoph Hellwig * spin_lock_irq(fq->mq_flush_lock) 272ae1b1539STejun Heo * 2738839a0e0STejun Heo */ 274404b8f5aSJens Axboe static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq, 27584fca1b0SHannes Reinecke unsigned int flags) 276ae1b1539STejun Heo { 2777c94e1c1SMing Lei struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx]; 278ae1b1539STejun Heo struct request *first_rq = 279ae1b1539STejun Heo list_first_entry(pending, struct request, flush.list); 2807c94e1c1SMing Lei struct request *flush_rq = fq->flush_rq; 281ae1b1539STejun Heo 282ae1b1539STejun Heo /* C1 described at the top of this file */ 2837c94e1c1SMing Lei if (fq->flush_pending_idx != fq->flush_running_idx || list_empty(pending)) 284404b8f5aSJens Axboe return; 285ae1b1539STejun Heo 2867520872cSJens Axboe /* C2 and C3 2877520872cSJens Axboe * 2887520872cSJens Axboe * For blk-mq + scheduling, we can risk having all driver tags 2897520872cSJens Axboe * assigned to empty flushes, and we deadlock if we are expecting 2907520872cSJens Axboe * other requests to make progress. Don't defer for that case. 2917520872cSJens Axboe */ 292344e9ffcSJens Axboe if (!list_empty(&fq->flush_data_in_flight) && q->elevator && 293ae1b1539STejun Heo time_before(jiffies, 2947c94e1c1SMing Lei fq->flush_pending_since + FLUSH_PENDING_TIMEOUT)) 295404b8f5aSJens Axboe return; 296ae1b1539STejun Heo 297ae1b1539STejun Heo /* 298ae1b1539STejun Heo * Issue flush and toggle pending_idx. This makes pending_idx 299ae1b1539STejun Heo * different from running_idx, which means flush is in flight. 300ae1b1539STejun Heo */ 3017c94e1c1SMing Lei fq->flush_pending_idx ^= 1; 30218741986SChristoph Hellwig 3037ddab5deSMing Lei blk_rq_init(q, flush_rq); 304f70ced09SMing Lei 305f70ced09SMing Lei /* 306923218f6SMing Lei * In case of none scheduler, borrow tag from the first request 307923218f6SMing Lei * since they can't be in flight at the same time. And acquire 308923218f6SMing Lei * the tag's ownership for flush req. 309923218f6SMing Lei * 310923218f6SMing Lei * In case of IO scheduler, flush rq need to borrow scheduler tag 311923218f6SMing Lei * just for cheating put/get driver tag. 312f70ced09SMing Lei */ 313f70ced09SMing Lei flush_rq->mq_ctx = first_rq->mq_ctx; 314ea4f995eSJens Axboe flush_rq->mq_hctx = first_rq->mq_hctx; 3150048b483SMing Lei 316*568f2700SMing Lei if (!q->elevator) 317923218f6SMing Lei flush_rq->tag = first_rq->tag; 318*568f2700SMing Lei else 319923218f6SMing Lei flush_rq->internal_tag = first_rq->internal_tag; 320320ae51fSJens Axboe 32170fd7614SChristoph Hellwig flush_rq->cmd_flags = REQ_OP_FLUSH | REQ_PREFLUSH; 32284fca1b0SHannes Reinecke flush_rq->cmd_flags |= (flags & REQ_DRV) | (flags & REQ_FAILFAST_MASK); 323e8064021SChristoph Hellwig flush_rq->rq_flags |= RQF_FLUSH_SEQ; 3247ddab5deSMing Lei flush_rq->rq_disk = first_rq->rq_disk; 3257ddab5deSMing Lei flush_rq->end_io = flush_end_io; 326ae1b1539STejun Heo 327404b8f5aSJens Axboe blk_flush_queue_rq(flush_rq, false); 328ae1b1539STejun Heo } 329ae1b1539STejun Heo 3302a842acaSChristoph Hellwig static void mq_flush_data_end_io(struct request *rq, blk_status_t error) 331320ae51fSJens Axboe { 332320ae51fSJens Axboe struct request_queue *q = rq->q; 333ea4f995eSJens Axboe struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 334e97c293cSMing Lei struct blk_mq_ctx *ctx = rq->mq_ctx; 335320ae51fSJens Axboe unsigned long flags; 336e97c293cSMing Lei struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx); 337320ae51fSJens Axboe 3384e2f62e5SJens Axboe if (q->elevator) { 3394e2f62e5SJens Axboe WARN_ON(rq->tag < 0); 3404e2f62e5SJens Axboe blk_mq_put_driver_tag(rq); 3414e2f62e5SJens Axboe } 3424e2f62e5SJens Axboe 343320ae51fSJens Axboe /* 344320ae51fSJens Axboe * After populating an empty queue, kick it to avoid stall. Read 345320ae51fSJens Axboe * the comment in flush_end_io(). 346320ae51fSJens Axboe */ 3477c94e1c1SMing Lei spin_lock_irqsave(&fq->mq_flush_lock, flags); 348bd166ef1SJens Axboe blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error); 3497c94e1c1SMing Lei spin_unlock_irqrestore(&fq->mq_flush_lock, flags); 350bd166ef1SJens Axboe 35185bd6e61SJianchao Wang blk_mq_sched_restart(hctx); 352320ae51fSJens Axboe } 353320ae51fSJens Axboe 354ae1b1539STejun Heo /** 3553140c3cfSOmar Sandoval * blk_insert_flush - insert a new PREFLUSH/FUA request 356ae1b1539STejun Heo * @rq: request to insert 357ae1b1539STejun Heo * 358b710a480SJens Axboe * To be called from __elv_add_request() for %ELEVATOR_INSERT_FLUSH insertions. 359320ae51fSJens Axboe * or __blk_mq_run_hw_queue() to dispatch request. 360ae1b1539STejun Heo * @rq is being submitted. Analyze what needs to be done and put it on the 361ae1b1539STejun Heo * right queue. 362ae1b1539STejun Heo */ 363ae1b1539STejun Heo void blk_insert_flush(struct request *rq) 364ae1b1539STejun Heo { 365ae1b1539STejun Heo struct request_queue *q = rq->q; 366c888a8f9SJens Axboe unsigned long fflags = q->queue_flags; /* may change, cache */ 367ae1b1539STejun Heo unsigned int policy = blk_flush_policy(fflags, rq); 368e97c293cSMing Lei struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx); 369ae1b1539STejun Heo 370ae1b1539STejun Heo /* 371ae1b1539STejun Heo * @policy now records what operations need to be done. Adjust 37228a8f0d3SMike Christie * REQ_PREFLUSH and FUA for the driver. 373ae1b1539STejun Heo */ 37428a8f0d3SMike Christie rq->cmd_flags &= ~REQ_PREFLUSH; 375c888a8f9SJens Axboe if (!(fflags & (1UL << QUEUE_FLAG_FUA))) 3764fed947cSTejun Heo rq->cmd_flags &= ~REQ_FUA; 377ae1b1539STejun Heo 378ae1b1539STejun Heo /* 379ae5b2ec8SJens Axboe * REQ_PREFLUSH|REQ_FUA implies REQ_SYNC, so if we clear any 380ae5b2ec8SJens Axboe * of those flags, we have to set REQ_SYNC to avoid skewing 381ae5b2ec8SJens Axboe * the request accounting. 382ae5b2ec8SJens Axboe */ 383ae5b2ec8SJens Axboe rq->cmd_flags |= REQ_SYNC; 384ae5b2ec8SJens Axboe 385ae5b2ec8SJens Axboe /* 3864853abaaSJeff Moyer * An empty flush handed down from a stacking driver may 3874853abaaSJeff Moyer * translate into nothing if the underlying device does not 3884853abaaSJeff Moyer * advertise a write-back cache. In this case, simply 3894853abaaSJeff Moyer * complete the request. 3904853abaaSJeff Moyer */ 3914853abaaSJeff Moyer if (!policy) { 392c8a446adSChristoph Hellwig blk_mq_end_request(rq, 0); 3934853abaaSJeff Moyer return; 3944853abaaSJeff Moyer } 3954853abaaSJeff Moyer 396834f9f61SJeff Moyer BUG_ON(rq->bio != rq->biotail); /*assumes zero or single bio rq */ 3974853abaaSJeff Moyer 3984853abaaSJeff Moyer /* 399ae1b1539STejun Heo * If there's data but flush is not necessary, the request can be 400ae1b1539STejun Heo * processed directly without going through flush machinery. Queue 401ae1b1539STejun Heo * for normal execution. 402ae1b1539STejun Heo */ 403ae1b1539STejun Heo if ((policy & REQ_FSEQ_DATA) && 404ae1b1539STejun Heo !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) { 40501e99aecSMing Lei blk_mq_request_bypass_insert(rq, false, false); 406ae1b1539STejun Heo return; 4078839a0e0STejun Heo } 4088839a0e0STejun Heo 4098839a0e0STejun Heo /* 410ae1b1539STejun Heo * @rq should go through flush machinery. Mark it part of flush 411ae1b1539STejun Heo * sequence and submit for further processing. 4128839a0e0STejun Heo */ 413ae1b1539STejun Heo memset(&rq->flush, 0, sizeof(rq->flush)); 414ae1b1539STejun Heo INIT_LIST_HEAD(&rq->flush.list); 415e8064021SChristoph Hellwig rq->rq_flags |= RQF_FLUSH_SEQ; 4164853abaaSJeff Moyer rq->flush.saved_end_io = rq->end_io; /* Usually NULL */ 4177e992f84SJens Axboe 418320ae51fSJens Axboe rq->end_io = mq_flush_data_end_io; 419320ae51fSJens Axboe 4207c94e1c1SMing Lei spin_lock_irq(&fq->mq_flush_lock); 4210bae352dSMing Lei blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0); 4227c94e1c1SMing Lei spin_unlock_irq(&fq->mq_flush_lock); 423ae1b1539STejun Heo } 424ae1b1539STejun Heo 425ae1b1539STejun Heo /** 4268839a0e0STejun Heo * blkdev_issue_flush - queue a flush 4278839a0e0STejun Heo * @bdev: blockdev to issue flush for 4288839a0e0STejun Heo * @gfp_mask: memory allocation flags (for bio_alloc) 4298839a0e0STejun Heo * 4308839a0e0STejun Heo * Description: 4319398554fSChristoph Hellwig * Issue a flush for the block device in question. 4328839a0e0STejun Heo */ 4339398554fSChristoph Hellwig int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask) 4348839a0e0STejun Heo { 4358839a0e0STejun Heo struct bio *bio; 4368839a0e0STejun Heo int ret = 0; 4378839a0e0STejun Heo 4388839a0e0STejun Heo bio = bio_alloc(gfp_mask, 0); 43974d46992SChristoph Hellwig bio_set_dev(bio, bdev); 44070fd7614SChristoph Hellwig bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; 4418839a0e0STejun Heo 4424e49ea4aSMike Christie ret = submit_bio_wait(bio); 4438839a0e0STejun Heo bio_put(bio); 4448839a0e0STejun Heo return ret; 4458839a0e0STejun Heo } 4468839a0e0STejun Heo EXPORT_SYMBOL(blkdev_issue_flush); 447320ae51fSJens Axboe 448754a1572SGuoqing Jiang struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size, 449754a1572SGuoqing Jiang gfp_t flags) 450320ae51fSJens Axboe { 4517c94e1c1SMing Lei struct blk_flush_queue *fq; 4527c94e1c1SMing Lei int rq_sz = sizeof(struct request); 4531bcb1eadSMing Lei 4545b202853SJianchao Wang fq = kzalloc_node(sizeof(*fq), flags, node); 4557c94e1c1SMing Lei if (!fq) 4567c94e1c1SMing Lei goto fail; 4571bcb1eadSMing Lei 4587c94e1c1SMing Lei spin_lock_init(&fq->mq_flush_lock); 4597c94e1c1SMing Lei 4606d247d7fSChristoph Hellwig rq_sz = round_up(rq_sz + cmd_size, cache_line_size()); 4615b202853SJianchao Wang fq->flush_rq = kzalloc_node(rq_sz, flags, node); 4627c94e1c1SMing Lei if (!fq->flush_rq) 4637c94e1c1SMing Lei goto fail_rq; 4647c94e1c1SMing Lei 4657c94e1c1SMing Lei INIT_LIST_HEAD(&fq->flush_queue[0]); 4667c94e1c1SMing Lei INIT_LIST_HEAD(&fq->flush_queue[1]); 4677c94e1c1SMing Lei INIT_LIST_HEAD(&fq->flush_data_in_flight); 4687c94e1c1SMing Lei 469b3c6a599SBart Van Assche lockdep_register_key(&fq->key); 470b3c6a599SBart Van Assche lockdep_set_class(&fq->mq_flush_lock, &fq->key); 471b3c6a599SBart Van Assche 4727c94e1c1SMing Lei return fq; 4737c94e1c1SMing Lei 4747c94e1c1SMing Lei fail_rq: 4757c94e1c1SMing Lei kfree(fq); 4767c94e1c1SMing Lei fail: 4777c94e1c1SMing Lei return NULL; 4787c94e1c1SMing Lei } 4797c94e1c1SMing Lei 480ba483388SMing Lei void blk_free_flush_queue(struct blk_flush_queue *fq) 4817c94e1c1SMing Lei { 4827c94e1c1SMing Lei /* bio based request queue hasn't flush queue */ 4837c94e1c1SMing Lei if (!fq) 4847c94e1c1SMing Lei return; 4857c94e1c1SMing Lei 486b3c6a599SBart Van Assche lockdep_unregister_key(&fq->key); 4877c94e1c1SMing Lei kfree(fq->flush_rq); 4887c94e1c1SMing Lei kfree(fq); 489320ae51fSJens Axboe } 490