blk-flush.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Functions to sequence FLUSH and FUA writes.
  3. *
  4. * Copyright (C) 2011 Max Planck Institute for Gravitational Physics
  5. * Copyright (C) 2011 Tejun Heo <tj@kernel.org>
  6. *
  7. * This file is released under the GPLv2.
  8. *
  9. * REQ_{FLUSH|FUA} requests are decomposed to sequences consisted of three
  10. * optional steps - PREFLUSH, DATA and POSTFLUSH - according to the request
  11. * properties and hardware capability.
  12. *
  13. * If a request doesn't have data, only REQ_FLUSH makes sense, which
  14. * indicates a simple flush request. If there is data, REQ_FLUSH indicates
  15. * that the device cache should be flushed before the data is executed, and
  16. * REQ_FUA means that the data must be on non-volatile media on request
  17. * completion.
  18. *
  19. * If the device doesn't have writeback cache, FLUSH and FUA don't make any
  20. * difference. The requests are either completed immediately if there's no
  21. * data or executed as normal requests otherwise.
  22. *
  23. * If the device has writeback cache and supports FUA, REQ_FLUSH is
  24. * translated to PREFLUSH but REQ_FUA is passed down directly with DATA.
  25. *
  26. * If the device has writeback cache and doesn't support FUA, REQ_FLUSH is
  27. * translated to PREFLUSH and REQ_FUA to POSTFLUSH.
  28. *
  29. * The actual execution of flush is double buffered. Whenever a request
  30. * needs to execute PRE or POSTFLUSH, it queues at
  31. * q->flush_queue[q->flush_pending_idx]. Once certain criteria are met, a
  32. * flush is issued and the pending_idx is toggled. When the flush
  33. * completes, all the requests which were pending are proceeded to the next
  34. * step. This allows arbitrary merging of different types of FLUSH/FUA
  35. * requests.
  36. *
  37. * Currently, the following conditions are used to determine when to issue
  38. * flush.
  39. *
  40. * C1. At any given time, only one flush shall be in progress. This makes
  41. * double buffering sufficient.
  42. *
  43. * C2. Flush is deferred if any request is executing DATA of its sequence.
  44. * This avoids issuing separate POSTFLUSHes for requests which shared
  45. * PREFLUSH.
  46. *
  47. * C3. The second condition is ignored if there is a request which has
  48. * waited longer than FLUSH_PENDING_TIMEOUT. This is to avoid
  49. * starvation in the unlikely case where there are continuous stream of
  50. * FUA (without FLUSH) requests.
  51. *
  52. * For devices which support FUA, it isn't clear whether C2 (and thus C3)
  53. * is beneficial.
  54. *
  55. * Note that a sequenced FLUSH/FUA request with DATA is completed twice.
  56. * Once while executing DATA and again after the whole sequence is
  57. * complete. The first completion updates the contained bio but doesn't
  58. * finish it so that the bio submitter is notified only after the whole
  59. * sequence is complete. This is implemented by testing REQ_FLUSH_SEQ in
  60. * req_bio_endio().
  61. *
  62. * The above peculiarity requires that each FLUSH/FUA request has only one
  63. * bio attached to it, which is guaranteed as they aren't allowed to be
  64. * merged in the usual way.
  65. */
  66. #include <linux/kernel.h>
  67. #include <linux/module.h>
  68. #include <linux/bio.h>
  69. #include <linux/blkdev.h>
  70. #include <linux/gfp.h>
  71. #include <linux/blk-mq.h>
  72. #include "blk.h"
  73. #include "blk-mq.h"
  74. /* FLUSH/FUA sequences */
  75. enum {
  76. REQ_FSEQ_PREFLUSH = (1 << 0), /* pre-flushing in progress */
  77. REQ_FSEQ_DATA = (1 << 1), /* data write in progress */
  78. REQ_FSEQ_POSTFLUSH = (1 << 2), /* post-flushing in progress */
  79. REQ_FSEQ_DONE = (1 << 3),
  80. REQ_FSEQ_ACTIONS = REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
  81. REQ_FSEQ_POSTFLUSH,
  82. /*
  83. * If flush has been pending longer than the following timeout,
  84. * it's issued even if flush_data requests are still in flight.
  85. */
  86. FLUSH_PENDING_TIMEOUT = 5 * HZ,
  87. };
  88. static bool blk_kick_flush(struct request_queue *q);
  89. static unsigned int blk_flush_policy(unsigned int fflags, struct request *rq)
  90. {
  91. unsigned int policy = 0;
  92. if (blk_rq_sectors(rq))
  93. policy |= REQ_FSEQ_DATA;
  94. if (fflags & REQ_FLUSH) {
  95. if (rq->cmd_flags & REQ_FLUSH)
  96. policy |= REQ_FSEQ_PREFLUSH;
  97. if (!(fflags & REQ_FUA) && (rq->cmd_flags & REQ_FUA))
  98. policy |= REQ_FSEQ_POSTFLUSH;
  99. }
  100. return policy;
  101. }
  102. static unsigned int blk_flush_cur_seq(struct request *rq)
  103. {
  104. return 1 << ffz(rq->flush.seq);
  105. }
  106. static void blk_flush_restore_request(struct request *rq)
  107. {
  108. /*
  109. * After flush data completion, @rq->bio is %NULL but we need to
  110. * complete the bio again. @rq->biotail is guaranteed to equal the
  111. * original @rq->bio. Restore it.
  112. */
  113. rq->bio = rq->biotail;
  114. /* make @rq a normal request */
  115. rq->cmd_flags &= ~REQ_FLUSH_SEQ;
  116. rq->end_io = rq->flush.saved_end_io;
  117. blk_clear_rq_complete(rq);
  118. }
  119. static void mq_flush_data_run(struct work_struct *work)
  120. {
  121. struct request *rq;
  122. rq = container_of(work, struct request, mq_flush_data);
  123. memset(&rq->csd, 0, sizeof(rq->csd));
  124. blk_mq_run_request(rq, true, false);
  125. }
  126. static void blk_mq_flush_data_insert(struct request *rq)
  127. {
  128. INIT_WORK(&rq->mq_flush_data, mq_flush_data_run);
  129. kblockd_schedule_work(rq->q, &rq->mq_flush_data);
  130. }
  131. /**
  132. * blk_flush_complete_seq - complete flush sequence
  133. * @rq: FLUSH/FUA request being sequenced
  134. * @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
  135. * @error: whether an error occurred
  136. *
  137. * @rq just completed @seq part of its flush sequence, record the
  138. * completion and trigger the next step.
  139. *
  140. * CONTEXT:
  141. * spin_lock_irq(q->queue_lock or q->mq_flush_lock)
  142. *
  143. * RETURNS:
  144. * %true if requests were added to the dispatch queue, %false otherwise.
  145. */
  146. static bool blk_flush_complete_seq(struct request *rq, unsigned int seq,
  147. int error)
  148. {
  149. struct request_queue *q = rq->q;
  150. struct list_head *pending = &q->flush_queue[q->flush_pending_idx];
  151. bool queued = false, kicked;
  152. BUG_ON(rq->flush.seq & seq);
  153. rq->flush.seq |= seq;
  154. if (likely(!error))
  155. seq = blk_flush_cur_seq(rq);
  156. else
  157. seq = REQ_FSEQ_DONE;
  158. switch (seq) {
  159. case REQ_FSEQ_PREFLUSH:
  160. case REQ_FSEQ_POSTFLUSH:
  161. /* queue for flush */
  162. if (list_empty(pending))
  163. q->flush_pending_since = jiffies;
  164. list_move_tail(&rq->flush.list, pending);
  165. break;
  166. case REQ_FSEQ_DATA:
  167. list_move_tail(&rq->flush.list, &q->flush_data_in_flight);
  168. if (q->mq_ops)
  169. blk_mq_flush_data_insert(rq);
  170. else {
  171. list_add(&rq->queuelist, &q->queue_head);
  172. queued = true;
  173. }
  174. break;
  175. case REQ_FSEQ_DONE:
  176. /*
  177. * @rq was previously adjusted by blk_flush_issue() for
  178. * flush sequencing and may already have gone through the
  179. * flush data request completion path. Restore @rq for
  180. * normal completion and end it.
  181. */
  182. BUG_ON(!list_empty(&rq->queuelist));
  183. list_del_init(&rq->flush.list);
  184. blk_flush_restore_request(rq);
  185. if (q->mq_ops)
  186. blk_mq_end_io(rq, error);
  187. else
  188. __blk_end_request_all(rq, error);
  189. break;
  190. default:
  191. BUG();
  192. }
  193. kicked = blk_kick_flush(q);
  194. /* blk_mq_run_flush will run queue */
  195. if (q->mq_ops)
  196. return queued;
  197. return kicked | queued;
  198. }
  199. static void flush_end_io(struct request *flush_rq, int error)
  200. {
  201. struct request_queue *q = flush_rq->q;
  202. struct list_head *running;
  203. bool queued = false;
  204. struct request *rq, *n;
  205. unsigned long flags = 0;
  206. if (q->mq_ops) {
  207. blk_mq_free_request(flush_rq);
  208. spin_lock_irqsave(&q->mq_flush_lock, flags);
  209. }
  210. running = &q->flush_queue[q->flush_running_idx];
  211. BUG_ON(q->flush_pending_idx == q->flush_running_idx);
  212. /* account completion of the flush request */
  213. q->flush_running_idx ^= 1;
  214. if (!q->mq_ops)
  215. elv_completed_request(q, flush_rq);
  216. /* and push the waiting requests to the next stage */
  217. list_for_each_entry_safe(rq, n, running, flush.list) {
  218. unsigned int seq = blk_flush_cur_seq(rq);
  219. BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
  220. queued |= blk_flush_complete_seq(rq, seq, error);
  221. }
  222. /*
  223. * Kick the queue to avoid stall for two cases:
  224. * 1. Moving a request silently to empty queue_head may stall the
  225. * queue.
  226. * 2. When flush request is running in non-queueable queue, the
  227. * queue is hold. Restart the queue after flush request is finished
  228. * to avoid stall.
  229. * This function is called from request completion path and calling
  230. * directly into request_fn may confuse the driver. Always use
  231. * kblockd.
  232. */
  233. if (queued || q->flush_queue_delayed) {
  234. if (!q->mq_ops)
  235. blk_run_queue_async(q);
  236. else
  237. /*
  238. * This can be optimized to only run queues with requests
  239. * queued if necessary.
  240. */
  241. blk_mq_run_queues(q, true);
  242. }
  243. q->flush_queue_delayed = 0;
  244. if (q->mq_ops)
  245. spin_unlock_irqrestore(&q->mq_flush_lock, flags);
  246. }
  247. static void mq_flush_work(struct work_struct *work)
  248. {
  249. struct request_queue *q;
  250. struct request *rq;
  251. q = container_of(work, struct request_queue, mq_flush_work);
  252. /* We don't need set REQ_FLUSH_SEQ, it's for consistency */
  253. rq = blk_mq_alloc_request(q, WRITE_FLUSH|REQ_FLUSH_SEQ,
  254. __GFP_WAIT|GFP_ATOMIC, true);
  255. rq->cmd_type = REQ_TYPE_FS;
  256. rq->end_io = flush_end_io;
  257. blk_mq_run_request(rq, true, false);
  258. }
  259. /*
  260. * We can't directly use q->flush_rq, because it doesn't have tag and is not in
  261. * hctx->rqs[]. so we must allocate a new request, since we can't sleep here,
  262. * so offload the work to workqueue.
  263. *
  264. * Note: we assume a flush request finished in any hardware queue will flush
  265. * the whole disk cache.
  266. */
  267. static void mq_run_flush(struct request_queue *q)
  268. {
  269. kblockd_schedule_work(q, &q->mq_flush_work);
  270. }
  271. /**
  272. * blk_kick_flush - consider issuing flush request
  273. * @q: request_queue being kicked
  274. *
  275. * Flush related states of @q have changed, consider issuing flush request.
  276. * Please read the comment at the top of this file for more info.
  277. *
  278. * CONTEXT:
  279. * spin_lock_irq(q->queue_lock or q->mq_flush_lock)
  280. *
  281. * RETURNS:
  282. * %true if flush was issued, %false otherwise.
  283. */
  284. static bool blk_kick_flush(struct request_queue *q)
  285. {
  286. struct list_head *pending = &q->flush_queue[q->flush_pending_idx];
  287. struct request *first_rq =
  288. list_first_entry(pending, struct request, flush.list);
  289. /* C1 described at the top of this file */
  290. if (q->flush_pending_idx != q->flush_running_idx || list_empty(pending))
  291. return false;
  292. /* C2 and C3 */
  293. if (!list_empty(&q->flush_data_in_flight) &&
  294. time_before(jiffies,
  295. q->flush_pending_since + FLUSH_PENDING_TIMEOUT))
  296. return false;
  297. /*
  298. * Issue flush and toggle pending_idx. This makes pending_idx
  299. * different from running_idx, which means flush is in flight.
  300. */
  301. q->flush_pending_idx ^= 1;
  302. if (q->mq_ops) {
  303. mq_run_flush(q);
  304. return true;
  305. }
  306. blk_rq_init(q, &q->flush_rq);
  307. q->flush_rq.cmd_type = REQ_TYPE_FS;
  308. q->flush_rq.cmd_flags = WRITE_FLUSH | REQ_FLUSH_SEQ;
  309. q->flush_rq.rq_disk = first_rq->rq_disk;
  310. q->flush_rq.end_io = flush_end_io;
  311. list_add_tail(&q->flush_rq.queuelist, &q->queue_head);
  312. return true;
  313. }
  314. static void flush_data_end_io(struct request *rq, int error)
  315. {
  316. struct request_queue *q = rq->q;
  317. /*
  318. * After populating an empty queue, kick it to avoid stall. Read
  319. * the comment in flush_end_io().
  320. */
  321. if (blk_flush_complete_seq(rq, REQ_FSEQ_DATA, error))
  322. blk_run_queue_async(q);
  323. }
  324. static void mq_flush_data_end_io(struct request *rq, int error)
  325. {
  326. struct request_queue *q = rq->q;
  327. struct blk_mq_hw_ctx *hctx;
  328. struct blk_mq_ctx *ctx;
  329. unsigned long flags;
  330. ctx = rq->mq_ctx;
  331. hctx = q->mq_ops->map_queue(q, ctx->cpu);
  332. /*
  333. * After populating an empty queue, kick it to avoid stall. Read
  334. * the comment in flush_end_io().
  335. */
  336. spin_lock_irqsave(&q->mq_flush_lock, flags);
  337. if (blk_flush_complete_seq(rq, REQ_FSEQ_DATA, error))
  338. blk_mq_run_hw_queue(hctx, true);
  339. spin_unlock_irqrestore(&q->mq_flush_lock, flags);
  340. }
  341. /**
  342. * blk_insert_flush - insert a new FLUSH/FUA request
  343. * @rq: request to insert
  344. *
  345. * To be called from __elv_add_request() for %ELEVATOR_INSERT_FLUSH insertions.
  346. * or __blk_mq_run_hw_queue() to dispatch request.
  347. * @rq is being submitted. Analyze what needs to be done and put it on the
  348. * right queue.
  349. *
  350. * CONTEXT:
  351. * spin_lock_irq(q->queue_lock) in !mq case
  352. */
  353. void blk_insert_flush(struct request *rq)
  354. {
  355. struct request_queue *q = rq->q;
  356. unsigned int fflags = q->flush_flags; /* may change, cache */
  357. unsigned int policy = blk_flush_policy(fflags, rq);
  358. /*
  359. * @policy now records what operations need to be done. Adjust
  360. * REQ_FLUSH and FUA for the driver.
  361. */
  362. rq->cmd_flags &= ~REQ_FLUSH;
  363. if (!(fflags & REQ_FUA))
  364. rq->cmd_flags &= ~REQ_FUA;
  365. /*
  366. * An empty flush handed down from a stacking driver may
  367. * translate into nothing if the underlying device does not
  368. * advertise a write-back cache. In this case, simply
  369. * complete the request.
  370. */
  371. if (!policy) {
  372. if (q->mq_ops)
  373. blk_mq_end_io(rq, 0);
  374. else
  375. __blk_end_bidi_request(rq, 0, 0, 0);
  376. return;
  377. }
  378. BUG_ON(rq->bio != rq->biotail); /*assumes zero or single bio rq */
  379. /*
  380. * If there's data but flush is not necessary, the request can be
  381. * processed directly without going through flush machinery. Queue
  382. * for normal execution.
  383. */
  384. if ((policy & REQ_FSEQ_DATA) &&
  385. !(policy & (REQ_FSEQ_PREFLUSH | REQ_FSEQ_POSTFLUSH))) {
  386. if (q->mq_ops) {
  387. blk_mq_run_request(rq, false, true);
  388. } else
  389. list_add_tail(&rq->queuelist, &q->queue_head);
  390. return;
  391. }
  392. /*
  393. * @rq should go through flush machinery. Mark it part of flush
  394. * sequence and submit for further processing.
  395. */
  396. memset(&rq->flush, 0, sizeof(rq->flush));
  397. INIT_LIST_HEAD(&rq->flush.list);
  398. rq->cmd_flags |= REQ_FLUSH_SEQ;
  399. rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
  400. if (q->mq_ops) {
  401. rq->end_io = mq_flush_data_end_io;
  402. spin_lock_irq(&q->mq_flush_lock);
  403. blk_flush_complete_seq(rq, REQ_FSEQ_ACTIONS & ~policy, 0);
  404. spin_unlock_irq(&q->mq_flush_lock);
  405. return;
  406. }
  407. rq->end_io = flush_data_end_io;
  408. blk_flush_complete_seq(rq, REQ_FSEQ_ACTIONS & ~policy, 0);
  409. }
  410. /**
  411. * blk_abort_flushes - @q is being aborted, abort flush requests
  412. * @q: request_queue being aborted
  413. *
  414. * To be called from elv_abort_queue(). @q is being aborted. Prepare all
  415. * FLUSH/FUA requests for abortion.
  416. *
  417. * CONTEXT:
  418. * spin_lock_irq(q->queue_lock)
  419. */
  420. void blk_abort_flushes(struct request_queue *q)
  421. {
  422. struct request *rq, *n;
  423. int i;
  424. /*
  425. * Requests in flight for data are already owned by the dispatch
  426. * queue or the device driver. Just restore for normal completion.
  427. */
  428. list_for_each_entry_safe(rq, n, &q->flush_data_in_flight, flush.list) {
  429. list_del_init(&rq->flush.list);
  430. blk_flush_restore_request(rq);
  431. }
  432. /*
  433. * We need to give away requests on flush queues. Restore for
  434. * normal completion and put them on the dispatch queue.
  435. */
  436. for (i = 0; i < ARRAY_SIZE(q->flush_queue); i++) {
  437. list_for_each_entry_safe(rq, n, &q->flush_queue[i],
  438. flush.list) {
  439. list_del_init(&rq->flush.list);
  440. blk_flush_restore_request(rq);
  441. list_add_tail(&rq->queuelist, &q->queue_head);
  442. }
  443. }
  444. }
  445. static void bio_end_flush(struct bio *bio, int err)
  446. {
  447. if (err)
  448. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  449. if (bio->bi_private)
  450. complete(bio->bi_private);
  451. bio_put(bio);
  452. }
  453. /**
  454. * blkdev_issue_flush - queue a flush
  455. * @bdev: blockdev to issue flush for
  456. * @gfp_mask: memory allocation flags (for bio_alloc)
  457. * @error_sector: error sector
  458. *
  459. * Description:
  460. * Issue a flush for the block device in question. Caller can supply
  461. * room for storing the error offset in case of a flush error, if they
  462. * wish to. If WAIT flag is not passed then caller may check only what
  463. * request was pushed in some internal queue for later handling.
  464. */
  465. int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  466. sector_t *error_sector)
  467. {
  468. DECLARE_COMPLETION_ONSTACK(wait);
  469. struct request_queue *q;
  470. struct bio *bio;
  471. int ret = 0;
  472. if (bdev->bd_disk == NULL)
  473. return -ENXIO;
  474. q = bdev_get_queue(bdev);
  475. if (!q)
  476. return -ENXIO;
  477. /*
  478. * some block devices may not have their queue correctly set up here
  479. * (e.g. loop device without a backing file) and so issuing a flush
  480. * here will panic. Ensure there is a request function before issuing
  481. * the flush.
  482. */
  483. if (!q->make_request_fn)
  484. return -ENXIO;
  485. bio = bio_alloc(gfp_mask, 0);
  486. bio->bi_end_io = bio_end_flush;
  487. bio->bi_bdev = bdev;
  488. bio->bi_private = &wait;
  489. bio_get(bio);
  490. submit_bio(WRITE_FLUSH, bio);
  491. wait_for_completion_io(&wait);
  492. /*
  493. * The driver must store the error location in ->bi_sector, if
  494. * it supports it. For non-stacked drivers, this should be
  495. * copied from blk_rq_pos(rq).
  496. */
  497. if (error_sector)
  498. *error_sector = bio->bi_sector;
  499. if (!bio_flagged(bio, BIO_UPTODATE))
  500. ret = -EIO;
  501. bio_put(bio);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(blkdev_issue_flush);
  505. void blk_mq_init_flush(struct request_queue *q)
  506. {
  507. spin_lock_init(&q->mq_flush_lock);
  508. INIT_WORK(&q->mq_flush_work, mq_flush_work);
  509. }