blk-core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
  5. * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  6. * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
  7. * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
  8. * - July2000
  9. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  10. */
  11. /*
  12. * This handles all read/write requests to block devices
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/bio.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/blk-pm.h>
  19. #include <linux/blk-integrity.h>
  20. #include <linux/highmem.h>
  21. #include <linux/mm.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/string.h>
  25. #include <linux/init.h>
  26. #include <linux/completion.h>
  27. #include <linux/slab.h>
  28. #include <linux/swap.h>
  29. #include <linux/writeback.h>
  30. #include <linux/task_io_accounting_ops.h>
  31. #include <linux/fault-inject.h>
  32. #include <linux/list_sort.h>
  33. #include <linux/delay.h>
  34. #include <linux/ratelimit.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/t10-pi.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/bpf.h>
  39. #include <linux/part_stat.h>
  40. #include <linux/sched/sysctl.h>
  41. #include <linux/blk-crypto.h>
  42. #define CREATE_TRACE_POINTS
  43. #include <trace/events/block.h>
  44. #include "blk.h"
  45. #include "blk-mq-sched.h"
  46. #include "blk-pm.h"
  47. #include "blk-cgroup.h"
  48. #include "blk-throttle.h"
  49. #include "blk-ioprio.h"
  50. struct dentry *blk_debugfs_root;
  51. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
  52. EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
  53. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
  54. EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
  55. EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
  56. EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
  57. static DEFINE_IDA(blk_queue_ida);
  58. /*
  59. * For queue allocation
  60. */
  61. static struct kmem_cache *blk_requestq_cachep;
  62. /*
  63. * Controlling structure to kblockd
  64. */
  65. static struct workqueue_struct *kblockd_workqueue;
  66. /**
  67. * blk_queue_flag_set - atomically set a queue flag
  68. * @flag: flag to be set
  69. * @q: request queue
  70. */
  71. void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
  72. {
  73. set_bit(flag, &q->queue_flags);
  74. }
  75. EXPORT_SYMBOL(blk_queue_flag_set);
  76. /**
  77. * blk_queue_flag_clear - atomically clear a queue flag
  78. * @flag: flag to be cleared
  79. * @q: request queue
  80. */
  81. void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
  82. {
  83. clear_bit(flag, &q->queue_flags);
  84. }
  85. EXPORT_SYMBOL(blk_queue_flag_clear);
  86. #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
  87. static const char *const blk_op_name[] = {
  88. REQ_OP_NAME(READ),
  89. REQ_OP_NAME(WRITE),
  90. REQ_OP_NAME(FLUSH),
  91. REQ_OP_NAME(DISCARD),
  92. REQ_OP_NAME(SECURE_ERASE),
  93. REQ_OP_NAME(ZONE_RESET),
  94. REQ_OP_NAME(ZONE_RESET_ALL),
  95. REQ_OP_NAME(ZONE_OPEN),
  96. REQ_OP_NAME(ZONE_CLOSE),
  97. REQ_OP_NAME(ZONE_FINISH),
  98. REQ_OP_NAME(ZONE_APPEND),
  99. REQ_OP_NAME(WRITE_ZEROES),
  100. REQ_OP_NAME(DRV_IN),
  101. REQ_OP_NAME(DRV_OUT),
  102. };
  103. #undef REQ_OP_NAME
  104. /**
  105. * blk_op_str - Return string XXX in the REQ_OP_XXX.
  106. * @op: REQ_OP_XXX.
  107. *
  108. * Description: Centralize block layer function to convert REQ_OP_XXX into
  109. * string format. Useful in the debugging and tracing bio or request. For
  110. * invalid REQ_OP_XXX it returns string "UNKNOWN".
  111. */
  112. inline const char *blk_op_str(enum req_op op)
  113. {
  114. const char *op_str = "UNKNOWN";
  115. if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
  116. op_str = blk_op_name[op];
  117. return op_str;
  118. }
  119. EXPORT_SYMBOL_GPL(blk_op_str);
  120. static const struct {
  121. int errno;
  122. const char *name;
  123. } blk_errors[] = {
  124. [BLK_STS_OK] = { 0, "" },
  125. [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
  126. [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
  127. [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
  128. [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
  129. [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
  130. [BLK_STS_RESV_CONFLICT] = { -EBADE, "reservation conflict" },
  131. [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
  132. [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
  133. [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
  134. [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
  135. [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
  136. [BLK_STS_OFFLINE] = { -ENODEV, "device offline" },
  137. /* device mapper special case, should not leak out: */
  138. [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
  139. /* zone device specific errors */
  140. [BLK_STS_ZONE_OPEN_RESOURCE] = { -ETOOMANYREFS, "open zones exceeded" },
  141. [BLK_STS_ZONE_ACTIVE_RESOURCE] = { -EOVERFLOW, "active zones exceeded" },
  142. /* Command duration limit device-side timeout */
  143. [BLK_STS_DURATION_LIMIT] = { -ETIME, "duration limit exceeded" },
  144. [BLK_STS_INVAL] = { -EINVAL, "invalid" },
  145. /* everything else not covered above: */
  146. [BLK_STS_IOERR] = { -EIO, "I/O" },
  147. };
  148. blk_status_t errno_to_blk_status(int errno)
  149. {
  150. int i;
  151. for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
  152. if (blk_errors[i].errno == errno)
  153. return (__force blk_status_t)i;
  154. }
  155. return BLK_STS_IOERR;
  156. }
  157. EXPORT_SYMBOL_GPL(errno_to_blk_status);
  158. int blk_status_to_errno(blk_status_t status)
  159. {
  160. int idx = (__force int)status;
  161. if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
  162. return -EIO;
  163. return blk_errors[idx].errno;
  164. }
  165. EXPORT_SYMBOL_GPL(blk_status_to_errno);
  166. const char *blk_status_to_str(blk_status_t status)
  167. {
  168. int idx = (__force int)status;
  169. if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
  170. return "<null>";
  171. return blk_errors[idx].name;
  172. }
  173. EXPORT_SYMBOL_GPL(blk_status_to_str);
  174. /**
  175. * blk_sync_queue - cancel any pending callbacks on a queue
  176. * @q: the queue
  177. *
  178. * Description:
  179. * The block layer may perform asynchronous callback activity
  180. * on a queue, such as calling the unplug function after a timeout.
  181. * A block device may call blk_sync_queue to ensure that any
  182. * such activity is cancelled, thus allowing it to release resources
  183. * that the callbacks might use. The caller must already have made sure
  184. * that its ->submit_bio will not re-add plugging prior to calling
  185. * this function.
  186. *
  187. * This function does not cancel any asynchronous activity arising
  188. * out of elevator or throttling code. That would require elevator_exit()
  189. * and blkcg_exit_queue() to be called with queue lock initialized.
  190. *
  191. */
  192. void blk_sync_queue(struct request_queue *q)
  193. {
  194. del_timer_sync(&q->timeout);
  195. cancel_work_sync(&q->timeout_work);
  196. }
  197. EXPORT_SYMBOL(blk_sync_queue);
  198. /**
  199. * blk_set_pm_only - increment pm_only counter
  200. * @q: request queue pointer
  201. */
  202. void blk_set_pm_only(struct request_queue *q)
  203. {
  204. atomic_inc(&q->pm_only);
  205. }
  206. EXPORT_SYMBOL_GPL(blk_set_pm_only);
  207. void blk_clear_pm_only(struct request_queue *q)
  208. {
  209. int pm_only;
  210. pm_only = atomic_dec_return(&q->pm_only);
  211. WARN_ON_ONCE(pm_only < 0);
  212. if (pm_only == 0)
  213. wake_up_all(&q->mq_freeze_wq);
  214. }
  215. EXPORT_SYMBOL_GPL(blk_clear_pm_only);
  216. static void blk_free_queue_rcu(struct rcu_head *rcu_head)
  217. {
  218. struct request_queue *q = container_of(rcu_head,
  219. struct request_queue, rcu_head);
  220. percpu_ref_exit(&q->q_usage_counter);
  221. kmem_cache_free(blk_requestq_cachep, q);
  222. }
  223. static void blk_free_queue(struct request_queue *q)
  224. {
  225. blk_free_queue_stats(q->stats);
  226. if (queue_is_mq(q))
  227. blk_mq_release(q);
  228. ida_free(&blk_queue_ida, q->id);
  229. lockdep_unregister_key(&q->io_lock_cls_key);
  230. lockdep_unregister_key(&q->q_lock_cls_key);
  231. call_rcu(&q->rcu_head, blk_free_queue_rcu);
  232. }
  233. /**
  234. * blk_put_queue - decrement the request_queue refcount
  235. * @q: the request_queue structure to decrement the refcount for
  236. *
  237. * Decrements the refcount of the request_queue and free it when the refcount
  238. * reaches 0.
  239. */
  240. void blk_put_queue(struct request_queue *q)
  241. {
  242. if (refcount_dec_and_test(&q->refs))
  243. blk_free_queue(q);
  244. }
  245. EXPORT_SYMBOL(blk_put_queue);
  246. bool blk_queue_start_drain(struct request_queue *q)
  247. {
  248. /*
  249. * When queue DYING flag is set, we need to block new req
  250. * entering queue, so we call blk_freeze_queue_start() to
  251. * prevent I/O from crossing blk_queue_enter().
  252. */
  253. bool freeze = __blk_freeze_queue_start(q, current);
  254. if (queue_is_mq(q))
  255. blk_mq_wake_waiters(q);
  256. /* Make blk_queue_enter() reexamine the DYING flag. */
  257. wake_up_all(&q->mq_freeze_wq);
  258. return freeze;
  259. }
  260. /**
  261. * blk_queue_enter() - try to increase q->q_usage_counter
  262. * @q: request queue pointer
  263. * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PM
  264. */
  265. int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
  266. {
  267. const bool pm = flags & BLK_MQ_REQ_PM;
  268. while (!blk_try_enter_queue(q, pm)) {
  269. if (flags & BLK_MQ_REQ_NOWAIT)
  270. return -EAGAIN;
  271. /*
  272. * read pair of barrier in blk_freeze_queue_start(), we need to
  273. * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
  274. * reading .mq_freeze_depth or queue dying flag, otherwise the
  275. * following wait may never return if the two reads are
  276. * reordered.
  277. */
  278. smp_rmb();
  279. wait_event(q->mq_freeze_wq,
  280. (!q->mq_freeze_depth &&
  281. blk_pm_resume_queue(pm, q)) ||
  282. blk_queue_dying(q));
  283. if (blk_queue_dying(q))
  284. return -ENODEV;
  285. }
  286. rwsem_acquire_read(&q->q_lockdep_map, 0, 0, _RET_IP_);
  287. rwsem_release(&q->q_lockdep_map, _RET_IP_);
  288. return 0;
  289. }
  290. int __bio_queue_enter(struct request_queue *q, struct bio *bio)
  291. {
  292. while (!blk_try_enter_queue(q, false)) {
  293. struct gendisk *disk = bio->bi_bdev->bd_disk;
  294. if (bio->bi_opf & REQ_NOWAIT) {
  295. if (test_bit(GD_DEAD, &disk->state))
  296. goto dead;
  297. bio_wouldblock_error(bio);
  298. return -EAGAIN;
  299. }
  300. /*
  301. * read pair of barrier in blk_freeze_queue_start(), we need to
  302. * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
  303. * reading .mq_freeze_depth or queue dying flag, otherwise the
  304. * following wait may never return if the two reads are
  305. * reordered.
  306. */
  307. smp_rmb();
  308. wait_event(q->mq_freeze_wq,
  309. (!q->mq_freeze_depth &&
  310. blk_pm_resume_queue(false, q)) ||
  311. test_bit(GD_DEAD, &disk->state));
  312. if (test_bit(GD_DEAD, &disk->state))
  313. goto dead;
  314. }
  315. rwsem_acquire_read(&q->io_lockdep_map, 0, 0, _RET_IP_);
  316. rwsem_release(&q->io_lockdep_map, _RET_IP_);
  317. return 0;
  318. dead:
  319. bio_io_error(bio);
  320. return -ENODEV;
  321. }
  322. void blk_queue_exit(struct request_queue *q)
  323. {
  324. percpu_ref_put(&q->q_usage_counter);
  325. }
  326. static void blk_queue_usage_counter_release(struct percpu_ref *ref)
  327. {
  328. struct request_queue *q =
  329. container_of(ref, struct request_queue, q_usage_counter);
  330. wake_up_all(&q->mq_freeze_wq);
  331. }
  332. static void blk_rq_timed_out_timer(struct timer_list *t)
  333. {
  334. struct request_queue *q = from_timer(q, t, timeout);
  335. kblockd_schedule_work(&q->timeout_work);
  336. }
  337. static void blk_timeout_work(struct work_struct *work)
  338. {
  339. }
  340. struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id)
  341. {
  342. struct request_queue *q;
  343. int error;
  344. q = kmem_cache_alloc_node(blk_requestq_cachep, GFP_KERNEL | __GFP_ZERO,
  345. node_id);
  346. if (!q)
  347. return ERR_PTR(-ENOMEM);
  348. q->last_merge = NULL;
  349. q->id = ida_alloc(&blk_queue_ida, GFP_KERNEL);
  350. if (q->id < 0) {
  351. error = q->id;
  352. goto fail_q;
  353. }
  354. q->stats = blk_alloc_queue_stats();
  355. if (!q->stats) {
  356. error = -ENOMEM;
  357. goto fail_id;
  358. }
  359. error = blk_set_default_limits(lim);
  360. if (error)
  361. goto fail_stats;
  362. q->limits = *lim;
  363. q->node = node_id;
  364. atomic_set(&q->nr_active_requests_shared_tags, 0);
  365. timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
  366. INIT_WORK(&q->timeout_work, blk_timeout_work);
  367. INIT_LIST_HEAD(&q->icq_list);
  368. refcount_set(&q->refs, 1);
  369. mutex_init(&q->debugfs_mutex);
  370. mutex_init(&q->sysfs_lock);
  371. mutex_init(&q->sysfs_dir_lock);
  372. mutex_init(&q->limits_lock);
  373. mutex_init(&q->rq_qos_mutex);
  374. spin_lock_init(&q->queue_lock);
  375. init_waitqueue_head(&q->mq_freeze_wq);
  376. mutex_init(&q->mq_freeze_lock);
  377. blkg_init_queue(q);
  378. /*
  379. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  380. * See blk_register_queue() for details.
  381. */
  382. error = percpu_ref_init(&q->q_usage_counter,
  383. blk_queue_usage_counter_release,
  384. PERCPU_REF_INIT_ATOMIC, GFP_KERNEL);
  385. if (error)
  386. goto fail_stats;
  387. lockdep_register_key(&q->io_lock_cls_key);
  388. lockdep_register_key(&q->q_lock_cls_key);
  389. lockdep_init_map(&q->io_lockdep_map, "&q->q_usage_counter(io)",
  390. &q->io_lock_cls_key, 0);
  391. lockdep_init_map(&q->q_lockdep_map, "&q->q_usage_counter(queue)",
  392. &q->q_lock_cls_key, 0);
  393. q->nr_requests = BLKDEV_DEFAULT_RQ;
  394. return q;
  395. fail_stats:
  396. blk_free_queue_stats(q->stats);
  397. fail_id:
  398. ida_free(&blk_queue_ida, q->id);
  399. fail_q:
  400. kmem_cache_free(blk_requestq_cachep, q);
  401. return ERR_PTR(error);
  402. }
  403. /**
  404. * blk_get_queue - increment the request_queue refcount
  405. * @q: the request_queue structure to increment the refcount for
  406. *
  407. * Increment the refcount of the request_queue kobject.
  408. *
  409. * Context: Any context.
  410. */
  411. bool blk_get_queue(struct request_queue *q)
  412. {
  413. if (unlikely(blk_queue_dying(q)))
  414. return false;
  415. refcount_inc(&q->refs);
  416. return true;
  417. }
  418. EXPORT_SYMBOL(blk_get_queue);
  419. #ifdef CONFIG_FAIL_MAKE_REQUEST
  420. static DECLARE_FAULT_ATTR(fail_make_request);
  421. static int __init setup_fail_make_request(char *str)
  422. {
  423. return setup_fault_attr(&fail_make_request, str);
  424. }
  425. __setup("fail_make_request=", setup_fail_make_request);
  426. bool should_fail_request(struct block_device *part, unsigned int bytes)
  427. {
  428. return bdev_test_flag(part, BD_MAKE_IT_FAIL) &&
  429. should_fail(&fail_make_request, bytes);
  430. }
  431. static int __init fail_make_request_debugfs(void)
  432. {
  433. struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
  434. NULL, &fail_make_request);
  435. return PTR_ERR_OR_ZERO(dir);
  436. }
  437. late_initcall(fail_make_request_debugfs);
  438. #endif /* CONFIG_FAIL_MAKE_REQUEST */
  439. static inline void bio_check_ro(struct bio *bio)
  440. {
  441. if (op_is_write(bio_op(bio)) && bdev_read_only(bio->bi_bdev)) {
  442. if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
  443. return;
  444. if (bdev_test_flag(bio->bi_bdev, BD_RO_WARNED))
  445. return;
  446. bdev_set_flag(bio->bi_bdev, BD_RO_WARNED);
  447. /*
  448. * Use ioctl to set underlying disk of raid/dm to read-only
  449. * will trigger this.
  450. */
  451. pr_warn("Trying to write to read-only block-device %pg\n",
  452. bio->bi_bdev);
  453. }
  454. }
  455. static noinline int should_fail_bio(struct bio *bio)
  456. {
  457. if (should_fail_request(bdev_whole(bio->bi_bdev), bio->bi_iter.bi_size))
  458. return -EIO;
  459. return 0;
  460. }
  461. ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
  462. /*
  463. * Check whether this bio extends beyond the end of the device or partition.
  464. * This may well happen - the kernel calls bread() without checking the size of
  465. * the device, e.g., when mounting a file system.
  466. */
  467. static inline int bio_check_eod(struct bio *bio)
  468. {
  469. sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
  470. unsigned int nr_sectors = bio_sectors(bio);
  471. if (nr_sectors &&
  472. (nr_sectors > maxsector ||
  473. bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
  474. pr_info_ratelimited("%s: attempt to access beyond end of device\n"
  475. "%pg: rw=%d, sector=%llu, nr_sectors = %u limit=%llu\n",
  476. current->comm, bio->bi_bdev, bio->bi_opf,
  477. bio->bi_iter.bi_sector, nr_sectors, maxsector);
  478. return -EIO;
  479. }
  480. return 0;
  481. }
  482. /*
  483. * Remap block n of partition p to block n+start(p) of the disk.
  484. */
  485. static int blk_partition_remap(struct bio *bio)
  486. {
  487. struct block_device *p = bio->bi_bdev;
  488. if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
  489. return -EIO;
  490. if (bio_sectors(bio)) {
  491. bio->bi_iter.bi_sector += p->bd_start_sect;
  492. trace_block_bio_remap(bio, p->bd_dev,
  493. bio->bi_iter.bi_sector -
  494. p->bd_start_sect);
  495. }
  496. bio_set_flag(bio, BIO_REMAPPED);
  497. return 0;
  498. }
  499. /*
  500. * Check write append to a zoned block device.
  501. */
  502. static inline blk_status_t blk_check_zone_append(struct request_queue *q,
  503. struct bio *bio)
  504. {
  505. int nr_sectors = bio_sectors(bio);
  506. /* Only applicable to zoned block devices */
  507. if (!bdev_is_zoned(bio->bi_bdev))
  508. return BLK_STS_NOTSUPP;
  509. /* The bio sector must point to the start of a sequential zone */
  510. if (!bdev_is_zone_start(bio->bi_bdev, bio->bi_iter.bi_sector))
  511. return BLK_STS_IOERR;
  512. /*
  513. * Not allowed to cross zone boundaries. Otherwise, the BIO will be
  514. * split and could result in non-contiguous sectors being written in
  515. * different zones.
  516. */
  517. if (nr_sectors > q->limits.chunk_sectors)
  518. return BLK_STS_IOERR;
  519. /* Make sure the BIO is small enough and will not get split */
  520. if (nr_sectors > queue_max_zone_append_sectors(q))
  521. return BLK_STS_IOERR;
  522. bio->bi_opf |= REQ_NOMERGE;
  523. return BLK_STS_OK;
  524. }
  525. static void __submit_bio(struct bio *bio)
  526. {
  527. /* If plug is not used, add new plug here to cache nsecs time. */
  528. struct blk_plug plug;
  529. if (unlikely(!blk_crypto_bio_prep(&bio)))
  530. return;
  531. blk_start_plug(&plug);
  532. if (!bdev_test_flag(bio->bi_bdev, BD_HAS_SUBMIT_BIO)) {
  533. blk_mq_submit_bio(bio);
  534. } else if (likely(bio_queue_enter(bio) == 0)) {
  535. struct gendisk *disk = bio->bi_bdev->bd_disk;
  536. if ((bio->bi_opf & REQ_POLLED) &&
  537. !(disk->queue->limits.features & BLK_FEAT_POLL)) {
  538. bio->bi_status = BLK_STS_NOTSUPP;
  539. bio_endio(bio);
  540. } else {
  541. disk->fops->submit_bio(bio);
  542. }
  543. blk_queue_exit(disk->queue);
  544. }
  545. blk_finish_plug(&plug);
  546. }
  547. /*
  548. * The loop in this function may be a bit non-obvious, and so deserves some
  549. * explanation:
  550. *
  551. * - Before entering the loop, bio->bi_next is NULL (as all callers ensure
  552. * that), so we have a list with a single bio.
  553. * - We pretend that we have just taken it off a longer list, so we assign
  554. * bio_list to a pointer to the bio_list_on_stack, thus initialising the
  555. * bio_list of new bios to be added. ->submit_bio() may indeed add some more
  556. * bios through a recursive call to submit_bio_noacct. If it did, we find a
  557. * non-NULL value in bio_list and re-enter the loop from the top.
  558. * - In this case we really did just take the bio of the top of the list (no
  559. * pretending) and so remove it from bio_list, and call into ->submit_bio()
  560. * again.
  561. *
  562. * bio_list_on_stack[0] contains bios submitted by the current ->submit_bio.
  563. * bio_list_on_stack[1] contains bios that were submitted before the current
  564. * ->submit_bio, but that haven't been processed yet.
  565. */
  566. static void __submit_bio_noacct(struct bio *bio)
  567. {
  568. struct bio_list bio_list_on_stack[2];
  569. BUG_ON(bio->bi_next);
  570. bio_list_init(&bio_list_on_stack[0]);
  571. current->bio_list = bio_list_on_stack;
  572. do {
  573. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  574. struct bio_list lower, same;
  575. /*
  576. * Create a fresh bio_list for all subordinate requests.
  577. */
  578. bio_list_on_stack[1] = bio_list_on_stack[0];
  579. bio_list_init(&bio_list_on_stack[0]);
  580. __submit_bio(bio);
  581. /*
  582. * Sort new bios into those for a lower level and those for the
  583. * same level.
  584. */
  585. bio_list_init(&lower);
  586. bio_list_init(&same);
  587. while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
  588. if (q == bdev_get_queue(bio->bi_bdev))
  589. bio_list_add(&same, bio);
  590. else
  591. bio_list_add(&lower, bio);
  592. /*
  593. * Now assemble so we handle the lowest level first.
  594. */
  595. bio_list_merge(&bio_list_on_stack[0], &lower);
  596. bio_list_merge(&bio_list_on_stack[0], &same);
  597. bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
  598. } while ((bio = bio_list_pop(&bio_list_on_stack[0])));
  599. current->bio_list = NULL;
  600. }
  601. static void __submit_bio_noacct_mq(struct bio *bio)
  602. {
  603. struct bio_list bio_list[2] = { };
  604. current->bio_list = bio_list;
  605. do {
  606. __submit_bio(bio);
  607. } while ((bio = bio_list_pop(&bio_list[0])));
  608. current->bio_list = NULL;
  609. }
  610. void submit_bio_noacct_nocheck(struct bio *bio)
  611. {
  612. blk_cgroup_bio_start(bio);
  613. blkcg_bio_issue_init(bio);
  614. if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
  615. trace_block_bio_queue(bio);
  616. /*
  617. * Now that enqueuing has been traced, we need to trace
  618. * completion as well.
  619. */
  620. bio_set_flag(bio, BIO_TRACE_COMPLETION);
  621. }
  622. /*
  623. * We only want one ->submit_bio to be active at a time, else stack
  624. * usage with stacked devices could be a problem. Use current->bio_list
  625. * to collect a list of requests submited by a ->submit_bio method while
  626. * it is active, and then process them after it returned.
  627. */
  628. if (current->bio_list)
  629. bio_list_add(&current->bio_list[0], bio);
  630. else if (!bdev_test_flag(bio->bi_bdev, BD_HAS_SUBMIT_BIO))
  631. __submit_bio_noacct_mq(bio);
  632. else
  633. __submit_bio_noacct(bio);
  634. }
  635. static blk_status_t blk_validate_atomic_write_op_size(struct request_queue *q,
  636. struct bio *bio)
  637. {
  638. if (bio->bi_iter.bi_size > queue_atomic_write_unit_max_bytes(q))
  639. return BLK_STS_INVAL;
  640. if (bio->bi_iter.bi_size % queue_atomic_write_unit_min_bytes(q))
  641. return BLK_STS_INVAL;
  642. return BLK_STS_OK;
  643. }
  644. /**
  645. * submit_bio_noacct - re-submit a bio to the block device layer for I/O
  646. * @bio: The bio describing the location in memory and on the device.
  647. *
  648. * This is a version of submit_bio() that shall only be used for I/O that is
  649. * resubmitted to lower level drivers by stacking block drivers. All file
  650. * systems and other upper level users of the block layer should use
  651. * submit_bio() instead.
  652. */
  653. void submit_bio_noacct(struct bio *bio)
  654. {
  655. struct block_device *bdev = bio->bi_bdev;
  656. struct request_queue *q = bdev_get_queue(bdev);
  657. blk_status_t status = BLK_STS_IOERR;
  658. might_sleep();
  659. /*
  660. * For a REQ_NOWAIT based request, return -EOPNOTSUPP
  661. * if queue does not support NOWAIT.
  662. */
  663. if ((bio->bi_opf & REQ_NOWAIT) && !bdev_nowait(bdev))
  664. goto not_supported;
  665. if (should_fail_bio(bio))
  666. goto end_io;
  667. bio_check_ro(bio);
  668. if (!bio_flagged(bio, BIO_REMAPPED)) {
  669. if (unlikely(bio_check_eod(bio)))
  670. goto end_io;
  671. if (bdev_is_partition(bdev) &&
  672. unlikely(blk_partition_remap(bio)))
  673. goto end_io;
  674. }
  675. /*
  676. * Filter flush bio's early so that bio based drivers without flush
  677. * support don't have to worry about them.
  678. */
  679. if (op_is_flush(bio->bi_opf)) {
  680. if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_WRITE &&
  681. bio_op(bio) != REQ_OP_ZONE_APPEND))
  682. goto end_io;
  683. if (!bdev_write_cache(bdev)) {
  684. bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
  685. if (!bio_sectors(bio)) {
  686. status = BLK_STS_OK;
  687. goto end_io;
  688. }
  689. }
  690. }
  691. switch (bio_op(bio)) {
  692. case REQ_OP_READ:
  693. break;
  694. case REQ_OP_WRITE:
  695. if (bio->bi_opf & REQ_ATOMIC) {
  696. status = blk_validate_atomic_write_op_size(q, bio);
  697. if (status != BLK_STS_OK)
  698. goto end_io;
  699. }
  700. break;
  701. case REQ_OP_FLUSH:
  702. /*
  703. * REQ_OP_FLUSH can't be submitted through bios, it is only
  704. * synthetized in struct request by the flush state machine.
  705. */
  706. goto not_supported;
  707. case REQ_OP_DISCARD:
  708. if (!bdev_max_discard_sectors(bdev))
  709. goto not_supported;
  710. break;
  711. case REQ_OP_SECURE_ERASE:
  712. if (!bdev_max_secure_erase_sectors(bdev))
  713. goto not_supported;
  714. break;
  715. case REQ_OP_ZONE_APPEND:
  716. status = blk_check_zone_append(q, bio);
  717. if (status != BLK_STS_OK)
  718. goto end_io;
  719. break;
  720. case REQ_OP_WRITE_ZEROES:
  721. if (!q->limits.max_write_zeroes_sectors)
  722. goto not_supported;
  723. break;
  724. case REQ_OP_ZONE_RESET:
  725. case REQ_OP_ZONE_OPEN:
  726. case REQ_OP_ZONE_CLOSE:
  727. case REQ_OP_ZONE_FINISH:
  728. case REQ_OP_ZONE_RESET_ALL:
  729. if (!bdev_is_zoned(bio->bi_bdev))
  730. goto not_supported;
  731. break;
  732. case REQ_OP_DRV_IN:
  733. case REQ_OP_DRV_OUT:
  734. /*
  735. * Driver private operations are only used with passthrough
  736. * requests.
  737. */
  738. fallthrough;
  739. default:
  740. goto not_supported;
  741. }
  742. if (blk_throtl_bio(bio))
  743. return;
  744. submit_bio_noacct_nocheck(bio);
  745. return;
  746. not_supported:
  747. status = BLK_STS_NOTSUPP;
  748. end_io:
  749. bio->bi_status = status;
  750. bio_endio(bio);
  751. }
  752. EXPORT_SYMBOL(submit_bio_noacct);
  753. static void bio_set_ioprio(struct bio *bio)
  754. {
  755. /* Nobody set ioprio so far? Initialize it based on task's nice value */
  756. if (IOPRIO_PRIO_CLASS(bio->bi_ioprio) == IOPRIO_CLASS_NONE)
  757. bio->bi_ioprio = get_current_ioprio();
  758. blkcg_set_ioprio(bio);
  759. }
  760. /**
  761. * submit_bio - submit a bio to the block device layer for I/O
  762. * @bio: The &struct bio which describes the I/O
  763. *
  764. * submit_bio() is used to submit I/O requests to block devices. It is passed a
  765. * fully set up &struct bio that describes the I/O that needs to be done. The
  766. * bio will be send to the device described by the bi_bdev field.
  767. *
  768. * The success/failure status of the request, along with notification of
  769. * completion, is delivered asynchronously through the ->bi_end_io() callback
  770. * in @bio. The bio must NOT be touched by the caller until ->bi_end_io() has
  771. * been called.
  772. */
  773. void submit_bio(struct bio *bio)
  774. {
  775. if (bio_op(bio) == REQ_OP_READ) {
  776. task_io_account_read(bio->bi_iter.bi_size);
  777. count_vm_events(PGPGIN, bio_sectors(bio));
  778. } else if (bio_op(bio) == REQ_OP_WRITE) {
  779. count_vm_events(PGPGOUT, bio_sectors(bio));
  780. }
  781. bio_set_ioprio(bio);
  782. submit_bio_noacct(bio);
  783. }
  784. EXPORT_SYMBOL(submit_bio);
  785. /**
  786. * bio_poll - poll for BIO completions
  787. * @bio: bio to poll for
  788. * @iob: batches of IO
  789. * @flags: BLK_POLL_* flags that control the behavior
  790. *
  791. * Poll for completions on queue associated with the bio. Returns number of
  792. * completed entries found.
  793. *
  794. * Note: the caller must either be the context that submitted @bio, or
  795. * be in a RCU critical section to prevent freeing of @bio.
  796. */
  797. int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags)
  798. {
  799. blk_qc_t cookie = READ_ONCE(bio->bi_cookie);
  800. struct block_device *bdev;
  801. struct request_queue *q;
  802. int ret = 0;
  803. bdev = READ_ONCE(bio->bi_bdev);
  804. if (!bdev)
  805. return 0;
  806. q = bdev_get_queue(bdev);
  807. if (cookie == BLK_QC_T_NONE)
  808. return 0;
  809. blk_flush_plug(current->plug, false);
  810. /*
  811. * We need to be able to enter a frozen queue, similar to how
  812. * timeouts also need to do that. If that is blocked, then we can
  813. * have pending IO when a queue freeze is started, and then the
  814. * wait for the freeze to finish will wait for polled requests to
  815. * timeout as the poller is preventer from entering the queue and
  816. * completing them. As long as we prevent new IO from being queued,
  817. * that should be all that matters.
  818. */
  819. if (!percpu_ref_tryget(&q->q_usage_counter))
  820. return 0;
  821. if (queue_is_mq(q)) {
  822. ret = blk_mq_poll(q, cookie, iob, flags);
  823. } else {
  824. struct gendisk *disk = q->disk;
  825. if ((q->limits.features & BLK_FEAT_POLL) && disk &&
  826. disk->fops->poll_bio)
  827. ret = disk->fops->poll_bio(bio, iob, flags);
  828. }
  829. blk_queue_exit(q);
  830. return ret;
  831. }
  832. EXPORT_SYMBOL_GPL(bio_poll);
  833. /*
  834. * Helper to implement file_operations.iopoll. Requires the bio to be stored
  835. * in iocb->private, and cleared before freeing the bio.
  836. */
  837. int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
  838. unsigned int flags)
  839. {
  840. struct bio *bio;
  841. int ret = 0;
  842. /*
  843. * Note: the bio cache only uses SLAB_TYPESAFE_BY_RCU, so bio can
  844. * point to a freshly allocated bio at this point. If that happens
  845. * we have a few cases to consider:
  846. *
  847. * 1) the bio is beeing initialized and bi_bdev is NULL. We can just
  848. * simply nothing in this case
  849. * 2) the bio points to a not poll enabled device. bio_poll will catch
  850. * this and return 0
  851. * 3) the bio points to a poll capable device, including but not
  852. * limited to the one that the original bio pointed to. In this
  853. * case we will call into the actual poll method and poll for I/O,
  854. * even if we don't need to, but it won't cause harm either.
  855. *
  856. * For cases 2) and 3) above the RCU grace period ensures that bi_bdev
  857. * is still allocated. Because partitions hold a reference to the whole
  858. * device bdev and thus disk, the disk is also still valid. Grabbing
  859. * a reference to the queue in bio_poll() ensures the hctxs and requests
  860. * are still valid as well.
  861. */
  862. rcu_read_lock();
  863. bio = READ_ONCE(kiocb->private);
  864. if (bio)
  865. ret = bio_poll(bio, iob, flags);
  866. rcu_read_unlock();
  867. return ret;
  868. }
  869. EXPORT_SYMBOL_GPL(iocb_bio_iopoll);
  870. void update_io_ticks(struct block_device *part, unsigned long now, bool end)
  871. {
  872. unsigned long stamp;
  873. again:
  874. stamp = READ_ONCE(part->bd_stamp);
  875. if (unlikely(time_after(now, stamp)) &&
  876. likely(try_cmpxchg(&part->bd_stamp, &stamp, now)) &&
  877. (end || part_in_flight(part)))
  878. __part_stat_add(part, io_ticks, now - stamp);
  879. if (bdev_is_partition(part)) {
  880. part = bdev_whole(part);
  881. goto again;
  882. }
  883. }
  884. unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
  885. unsigned long start_time)
  886. {
  887. part_stat_lock();
  888. update_io_ticks(bdev, start_time, false);
  889. part_stat_local_inc(bdev, in_flight[op_is_write(op)]);
  890. part_stat_unlock();
  891. return start_time;
  892. }
  893. EXPORT_SYMBOL(bdev_start_io_acct);
  894. /**
  895. * bio_start_io_acct - start I/O accounting for bio based drivers
  896. * @bio: bio to start account for
  897. *
  898. * Returns the start time that should be passed back to bio_end_io_acct().
  899. */
  900. unsigned long bio_start_io_acct(struct bio *bio)
  901. {
  902. return bdev_start_io_acct(bio->bi_bdev, bio_op(bio), jiffies);
  903. }
  904. EXPORT_SYMBOL_GPL(bio_start_io_acct);
  905. void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
  906. unsigned int sectors, unsigned long start_time)
  907. {
  908. const int sgrp = op_stat_group(op);
  909. unsigned long now = READ_ONCE(jiffies);
  910. unsigned long duration = now - start_time;
  911. part_stat_lock();
  912. update_io_ticks(bdev, now, true);
  913. part_stat_inc(bdev, ios[sgrp]);
  914. part_stat_add(bdev, sectors[sgrp], sectors);
  915. part_stat_add(bdev, nsecs[sgrp], jiffies_to_nsecs(duration));
  916. part_stat_local_dec(bdev, in_flight[op_is_write(op)]);
  917. part_stat_unlock();
  918. }
  919. EXPORT_SYMBOL(bdev_end_io_acct);
  920. void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
  921. struct block_device *orig_bdev)
  922. {
  923. bdev_end_io_acct(orig_bdev, bio_op(bio), bio_sectors(bio), start_time);
  924. }
  925. EXPORT_SYMBOL_GPL(bio_end_io_acct_remapped);
  926. /**
  927. * blk_lld_busy - Check if underlying low-level drivers of a device are busy
  928. * @q : the queue of the device being checked
  929. *
  930. * Description:
  931. * Check if underlying low-level drivers of a device are busy.
  932. * If the drivers want to export their busy state, they must set own
  933. * exporting function using blk_queue_lld_busy() first.
  934. *
  935. * Basically, this function is used only by request stacking drivers
  936. * to stop dispatching requests to underlying devices when underlying
  937. * devices are busy. This behavior helps more I/O merging on the queue
  938. * of the request stacking driver and prevents I/O throughput regression
  939. * on burst I/O load.
  940. *
  941. * Return:
  942. * 0 - Not busy (The request stacking driver should dispatch request)
  943. * 1 - Busy (The request stacking driver should stop dispatching request)
  944. */
  945. int blk_lld_busy(struct request_queue *q)
  946. {
  947. if (queue_is_mq(q) && q->mq_ops->busy)
  948. return q->mq_ops->busy(q);
  949. return 0;
  950. }
  951. EXPORT_SYMBOL_GPL(blk_lld_busy);
  952. int kblockd_schedule_work(struct work_struct *work)
  953. {
  954. return queue_work(kblockd_workqueue, work);
  955. }
  956. EXPORT_SYMBOL(kblockd_schedule_work);
  957. int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
  958. unsigned long delay)
  959. {
  960. return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
  961. }
  962. EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
  963. void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
  964. {
  965. struct task_struct *tsk = current;
  966. /*
  967. * If this is a nested plug, don't actually assign it.
  968. */
  969. if (tsk->plug)
  970. return;
  971. plug->cur_ktime = 0;
  972. plug->mq_list = NULL;
  973. plug->cached_rq = NULL;
  974. plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
  975. plug->rq_count = 0;
  976. plug->multiple_queues = false;
  977. plug->has_elevator = false;
  978. INIT_LIST_HEAD(&plug->cb_list);
  979. /*
  980. * Store ordering should not be needed here, since a potential
  981. * preempt will imply a full memory barrier
  982. */
  983. tsk->plug = plug;
  984. }
  985. /**
  986. * blk_start_plug - initialize blk_plug and track it inside the task_struct
  987. * @plug: The &struct blk_plug that needs to be initialized
  988. *
  989. * Description:
  990. * blk_start_plug() indicates to the block layer an intent by the caller
  991. * to submit multiple I/O requests in a batch. The block layer may use
  992. * this hint to defer submitting I/Os from the caller until blk_finish_plug()
  993. * is called. However, the block layer may choose to submit requests
  994. * before a call to blk_finish_plug() if the number of queued I/Os
  995. * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
  996. * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
  997. * the task schedules (see below).
  998. *
  999. * Tracking blk_plug inside the task_struct will help with auto-flushing the
  1000. * pending I/O should the task end up blocking between blk_start_plug() and
  1001. * blk_finish_plug(). This is important from a performance perspective, but
  1002. * also ensures that we don't deadlock. For instance, if the task is blocking
  1003. * for a memory allocation, memory reclaim could end up wanting to free a
  1004. * page belonging to that request that is currently residing in our private
  1005. * plug. By flushing the pending I/O when the process goes to sleep, we avoid
  1006. * this kind of deadlock.
  1007. */
  1008. void blk_start_plug(struct blk_plug *plug)
  1009. {
  1010. blk_start_plug_nr_ios(plug, 1);
  1011. }
  1012. EXPORT_SYMBOL(blk_start_plug);
  1013. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  1014. {
  1015. LIST_HEAD(callbacks);
  1016. while (!list_empty(&plug->cb_list)) {
  1017. list_splice_init(&plug->cb_list, &callbacks);
  1018. while (!list_empty(&callbacks)) {
  1019. struct blk_plug_cb *cb = list_first_entry(&callbacks,
  1020. struct blk_plug_cb,
  1021. list);
  1022. list_del(&cb->list);
  1023. cb->callback(cb, from_schedule);
  1024. }
  1025. }
  1026. }
  1027. struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
  1028. int size)
  1029. {
  1030. struct blk_plug *plug = current->plug;
  1031. struct blk_plug_cb *cb;
  1032. if (!plug)
  1033. return NULL;
  1034. list_for_each_entry(cb, &plug->cb_list, list)
  1035. if (cb->callback == unplug && cb->data == data)
  1036. return cb;
  1037. /* Not currently on the callback list */
  1038. BUG_ON(size < sizeof(*cb));
  1039. cb = kzalloc(size, GFP_ATOMIC);
  1040. if (cb) {
  1041. cb->data = data;
  1042. cb->callback = unplug;
  1043. list_add(&cb->list, &plug->cb_list);
  1044. }
  1045. return cb;
  1046. }
  1047. EXPORT_SYMBOL(blk_check_plugged);
  1048. void __blk_flush_plug(struct blk_plug *plug, bool from_schedule)
  1049. {
  1050. if (!list_empty(&plug->cb_list))
  1051. flush_plug_callbacks(plug, from_schedule);
  1052. blk_mq_flush_plug_list(plug, from_schedule);
  1053. /*
  1054. * Unconditionally flush out cached requests, even if the unplug
  1055. * event came from schedule. Since we know hold references to the
  1056. * queue for cached requests, we don't want a blocked task holding
  1057. * up a queue freeze/quiesce event.
  1058. */
  1059. if (unlikely(!rq_list_empty(plug->cached_rq)))
  1060. blk_mq_free_plug_rqs(plug);
  1061. plug->cur_ktime = 0;
  1062. current->flags &= ~PF_BLOCK_TS;
  1063. }
  1064. /**
  1065. * blk_finish_plug - mark the end of a batch of submitted I/O
  1066. * @plug: The &struct blk_plug passed to blk_start_plug()
  1067. *
  1068. * Description:
  1069. * Indicate that a batch of I/O submissions is complete. This function
  1070. * must be paired with an initial call to blk_start_plug(). The intent
  1071. * is to allow the block layer to optimize I/O submission. See the
  1072. * documentation for blk_start_plug() for more information.
  1073. */
  1074. void blk_finish_plug(struct blk_plug *plug)
  1075. {
  1076. if (plug == current->plug) {
  1077. __blk_flush_plug(plug, false);
  1078. current->plug = NULL;
  1079. }
  1080. }
  1081. EXPORT_SYMBOL(blk_finish_plug);
  1082. void blk_io_schedule(void)
  1083. {
  1084. /* Prevent hang_check timer from firing at us during very long I/O */
  1085. unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
  1086. if (timeout)
  1087. io_schedule_timeout(timeout);
  1088. else
  1089. io_schedule();
  1090. }
  1091. EXPORT_SYMBOL_GPL(blk_io_schedule);
  1092. int __init blk_dev_init(void)
  1093. {
  1094. BUILD_BUG_ON((__force u32)REQ_OP_LAST >= (1 << REQ_OP_BITS));
  1095. BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
  1096. sizeof_field(struct request, cmd_flags));
  1097. BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
  1098. sizeof_field(struct bio, bi_opf));
  1099. /* used for unplugging and affects IO latency/throughput - HIGHPRI */
  1100. kblockd_workqueue = alloc_workqueue("kblockd",
  1101. WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
  1102. if (!kblockd_workqueue)
  1103. panic("Failed to create kblockd\n");
  1104. blk_requestq_cachep = KMEM_CACHE(request_queue, SLAB_PANIC);
  1105. blk_debugfs_root = debugfs_create_dir("block", NULL);
  1106. return 0;
  1107. }