preliminaryDataProcessing.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
  4. * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  5. * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
  6. * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
  7. * - July2000
  8. * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
  9. */
  10. /*
  11. * This handles all read/write requests to block devices
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/bio.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/highmem.h>
  19. #include <linux/mm.h>
  20. #include <linux/kernel_stat.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/completion.h>
  24. #include <linux/slab.h>
  25. #include <linux/swap.h>
  26. #include <linux/writeback.h>
  27. #include <linux/task_io_accounting_ops.h>
  28. #include <linux/fault-inject.h>
  29. #include <linux/list_sort.h>
  30. #include <linux/delay.h>
  31. #include <linux/ratelimit.h>
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/block.h>
  34. #include "blk.h"
  35. #include "blk-cgroup.h"
  36. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
  37. EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
  38. EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
  39. EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
  40. DEFINE_IDA(blk_queue_ida);
  41. /*
  42. * For the allocated request tables
  43. */
  44. static struct kmem_cache *request_cachep;
  45. /*
  46. * For queue allocation
  47. */
  48. struct kmem_cache *blk_requestq_cachep;
  49. /*
  50. * Controlling structure to kblockd
  51. */
  52. static struct workqueue_struct *kblockd_workqueue;
  53. static void drive_stat_acct(struct request *rq, int new_io)
  54. {
  55. struct hd_struct *part;
  56. int rw = rq_data_dir(rq);
  57. int cpu;
  58. if (!blk_do_io_stat(rq))
  59. return;
  60. cpu = part_stat_lock();
  61. if (!new_io) {
  62. part = rq->part;
  63. part_stat_inc(cpu, part, merges[rw]);
  64. } else {
  65. part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
  66. if (!hd_struct_try_get(part)) {
  67. /*
  68. * The partition is already being removed,
  69. * the request will be accounted on the disk only
  70. *
  71. * We take a reference on disk->part0 although that
  72. * partition will never be deleted, so we can treat
  73. * it as any other partition.
  74. */
  75. part = &rq->rq_disk->part0;
  76. hd_struct_get(part);
  77. }
  78. part_round_stats(cpu, part);
  79. part_inc_in_flight(part, rw);
  80. rq->part = part;
  81. }
  82. part_stat_unlock();
  83. }
  84. void blk_queue_congestion_threshold(struct request_queue *q)
  85. {
  86. int nr;
  87. nr = q->nr_requests - (q->nr_requests / 8) + 1;
  88. if (nr > q->nr_requests)
  89. nr = q->nr_requests;
  90. q->nr_congestion_on = nr;
  91. nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
  92. if (nr < 1)
  93. nr = 1;
  94. q->nr_congestion_off = nr;
  95. }
  96. /**
  97. * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
  98. * @bdev: device
  99. *
  100. * Locates the passed device's request queue and returns the address of its
  101. * backing_dev_info
  102. *
  103. * Will return NULL if the request queue cannot be located.
  104. */
  105. struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
  106. {
  107. struct backing_dev_info *ret = NULL;
  108. struct request_queue *q = bdev_get_queue(bdev);
  109. if (q)
  110. ret = &q->backing_dev_info;
  111. return ret;
  112. }
  113. EXPORT_SYMBOL(blk_get_backing_dev_info);
  114. void blk_rq_init(struct request_queue *q, struct request *rq)
  115. {
  116. memset(rq, 0, sizeof(*rq));
  117. INIT_LIST_HEAD(&rq->queuelist);
  118. INIT_LIST_HEAD(&rq->timeout_list);
  119. rq->cpu = -1;
  120. rq->q = q;
  121. rq->__sector = (sector_t) -1;
  122. INIT_HLIST_NODE(&rq->hash);
  123. RB_CLEAR_NODE(&rq->rb_node);
  124. rq->cmd = rq->__cmd;
  125. rq->cmd_len = BLK_MAX_CDB;
  126. rq->tag = -1;
  127. rq->ref_count = 1;
  128. rq->start_time = jiffies;
  129. set_start_time_ns(rq);
  130. rq->part = NULL;
  131. }
  132. EXPORT_SYMBOL(blk_rq_init);
  133. static void req_bio_endio(struct request *rq, struct bio *bio,
  134. unsigned int nbytes, int error)
  135. {
  136. if (error)
  137. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  138. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  139. error = -EIO;
  140. if (unlikely(nbytes > bio->bi_size)) {
  141. printk(KERN_ERR "%s: want %u bytes done, %u left\n",
  142. __func__, nbytes, bio->bi_size);
  143. nbytes = bio->bi_size;
  144. }
  145. if (unlikely(rq->cmd_flags & REQ_QUIET))
  146. set_bit(BIO_QUIET, &bio->bi_flags);
  147. bio->bi_size -= nbytes;
  148. bio->bi_sector += (nbytes >> 9);
  149. if (bio_integrity(bio))
  150. bio_integrity_advance(bio, nbytes);
  151. /* don't actually finish bio if it's part of flush sequence */
  152. if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
  153. bio_endio(bio, error);
  154. }
  155. void blk_dump_rq_flags(struct request *rq, char *msg)
  156. {
  157. int bit;
  158. printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
  159. rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
  160. rq->cmd_flags);
  161. printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
  162. (unsigned long long)blk_rq_pos(rq),
  163. blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
  164. printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
  165. rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
  166. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  167. printk(KERN_INFO " cdb: ");
  168. for (bit = 0; bit < BLK_MAX_CDB; bit++)
  169. printk("%02x ", rq->cmd[bit]);
  170. printk("\n");
  171. }
  172. }
  173. EXPORT_SYMBOL(blk_dump_rq_flags);
  174. static void blk_delay_work(struct work_struct *work)
  175. {
  176. struct request_queue *q;
  177. q = container_of(work, struct request_queue, delay_work.work);
  178. spin_lock_irq(q->queue_lock);
  179. __blk_run_queue(q);
  180. spin_unlock_irq(q->queue_lock);
  181. }
  182. /**
  183. * blk_delay_queue - restart queueing after defined interval
  184. * @q: The &struct request_queue in question
  185. * @msecs: Delay in msecs
  186. *
  187. * Description:
  188. * Sometimes queueing needs to be postponed for a little while, to allow
  189. * resources to come back. This function will make sure that queueing is
  190. * restarted around the specified time. Queue lock must be held.
  191. */
  192. void blk_delay_queue(struct request_queue *q, unsigned long msecs)
  193. {
  194. if (likely(!blk_queue_dead(q)))
  195. queue_delayed_work(kblockd_workqueue, &q->delay_work,
  196. msecs_to_jiffies(msecs));
  197. }
  198. EXPORT_SYMBOL(blk_delay_queue);
  199. /**
  200. * blk_start_queue - restart a previously stopped queue
  201. * @q: The &struct request_queue in question
  202. *
  203. * Description:
  204. * blk_start_queue() will clear the stop flag on the queue, and call
  205. * the request_fn for the queue if it was in a stopped state when
  206. * entered. Also see blk_stop_queue(). Queue lock must be held.
  207. **/
  208. void blk_start_queue(struct request_queue *q)
  209. {
  210. WARN_ON(!irqs_disabled());
  211. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  212. __blk_run_queue(q);
  213. }
  214. EXPORT_SYMBOL(blk_start_queue);
  215. /**
  216. * blk_stop_queue - stop a queue
  217. * @q: The &struct request_queue in question
  218. *
  219. * Description:
  220. * The Linux block layer assumes that a block driver will consume all
  221. * entries on the request queue when the request_fn strategy is called.
  222. * Often this will not happen, because of hardware limitations (queue
  223. * depth settings). If a device driver gets a 'queue full' response,
  224. * or if it simply chooses not to queue more I/O at one point, it can
  225. * call this function to prevent the request_fn from being called until
  226. * the driver has signalled it's ready to go again. This happens by calling
  227. * blk_start_queue() to restart queue operations. Queue lock must be held.
  228. **/
  229. void blk_stop_queue(struct request_queue *q)
  230. {
  231. cancel_delayed_work(&q->delay_work);
  232. queue_flag_set(QUEUE_FLAG_STOPPED, q);
  233. }
  234. EXPORT_SYMBOL(blk_stop_queue);
  235. /**
  236. * blk_sync_queue - cancel any pending callbacks on a queue
  237. * @q: the queue
  238. *
  239. * Description:
  240. * The block layer may perform asynchronous callback activity
  241. * on a queue, such as calling the unplug function after a timeout.
  242. * A block device may call blk_sync_queue to ensure that any
  243. * such activity is cancelled, thus allowing it to release resources
  244. * that the callbacks might use. The caller must already have made sure
  245. * that its ->make_request_fn will not re-add plugging prior to calling
  246. * this function.
  247. *
  248. * This function does not cancel any asynchronous activity arising
  249. * out of elevator or throttling code. That would require elevaotor_exit()
  250. * and blkcg_exit_queue() to be called with queue lock initialized.
  251. *
  252. */
  253. void blk_sync_queue(struct request_queue *q)
  254. {
  255. del_timer_sync(&q->timeout);
  256. cancel_delayed_work_sync(&q->delay_work);
  257. }
  258. EXPORT_SYMBOL(blk_sync_queue);
  259. /**
  260. * __blk_run_queue_uncond - run a queue whether or not it has been stopped
  261. * @q: The queue to run
  262. *
  263. * Description:
  264. * Invoke request handling on a queue if there are any pending requests.
  265. * May be used to restart request handling after a request has completed.
  266. * This variant runs the queue whether or not the queue has been
  267. * stopped. Must be called with the queue lock held and interrupts
  268. * disabled. See also @blk_run_queue.
  269. */
  270. inline void __blk_run_queue_uncond(struct request_queue *q)
  271. {
  272. if (unlikely(blk_queue_dead(q)))
  273. return;
  274. /*
  275. * Some request_fn implementations, e.g. scsi_request_fn(), unlock
  276. * the queue lock internally. As a result multiple threads may be
  277. * running such a request function concurrently. Keep track of the
  278. * number of active request_fn invocations such that blk_drain_queue()
  279. * can wait until all these request_fn calls have finished.
  280. */
  281. q->request_fn_active++;
  282. q->request_fn(q);
  283. q->request_fn_active--;
  284. }
  285. /**
  286. * __blk_run_queue - run a single device queue
  287. * @q: The queue to run
  288. *
  289. * Description:
  290. * See @blk_run_queue. This variant must be called with the queue lock
  291. * held and interrupts disabled.
  292. */
  293. void __blk_run_queue(struct request_queue *q)
  294. {
  295. if (unlikely(blk_queue_stopped(q)))
  296. return;
  297. __blk_run_queue_uncond(q);
  298. }
  299. EXPORT_SYMBOL(__blk_run_queue);
  300. /**
  301. * blk_run_queue_async - run a single device queue in workqueue context
  302. * @q: The queue to run
  303. *
  304. * Description:
  305. * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
  306. * of us. The caller must hold the queue lock.
  307. */
  308. void blk_run_queue_async(struct request_queue *q)
  309. {
  310. if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
  311. mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
  312. }
  313. EXPORT_SYMBOL(blk_run_queue_async);
  314. /**
  315. * blk_run_queue - run a single device queue
  316. * @q: The queue to run
  317. *
  318. * Description:
  319. * Invoke request handling on this queue, if it has pending work to do.
  320. * May be used to restart queueing when a request has completed.
  321. */
  322. void blk_run_queue(struct request_queue *q)
  323. {
  324. unsigned long flags;
  325. spin_lock_irqsave(q->queue_lock, flags);
  326. __blk_run_queue(q);
  327. spin_unlock_irqrestore(q->queue_lock, flags);
  328. }
  329. EXPORT_SYMBOL(blk_run_queue);
  330. void blk_put_queue(struct request_queue *q)
  331. {
  332. kobject_put(&q->kobj);
  333. }
  334. EXPORT_SYMBOL(blk_put_queue);
  335. /**
  336. * __blk_drain_queue - drain requests from request_queue
  337. * @q: queue to drain
  338. * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
  339. *
  340. * Drain requests from @q. If @drain_all is set, all requests are drained.
  341. * If not, only ELVPRIV requests are drained. The caller is responsible
  342. * for ensuring that no new requests which need to be drained are queued.
  343. */
  344. static void __blk_drain_queue(struct request_queue *q, bool drain_all)
  345. __releases(q->queue_lock)
  346. __acquires(q->queue_lock)
  347. {
  348. int i;
  349. lockdep_assert_held(q->queue_lock);
  350. while (true) {
  351. bool drain = false;
  352. /*
  353. * The caller might be trying to drain @q before its
  354. * elevator is initialized.
  355. */
  356. if (q->elevator)
  357. elv_drain_elevator(q);
  358. blkcg_drain_queue(q);
  359. /*
  360. * This function might be called on a queue which failed
  361. * driver init after queue creation or is not yet fully
  362. * active yet. Some drivers (e.g. fd and loop) get unhappy
  363. * in such cases. Kick queue iff dispatch queue has
  364. * something on it and @q has request_fn set.
  365. */
  366. if (!list_empty(&q->queue_head) && q->request_fn)
  367. __blk_run_queue(q);
  368. drain |= q->nr_rqs_elvpriv;
  369. drain |= q->request_fn_active;
  370. /*
  371. * Unfortunately, requests are queued at and tracked from
  372. * multiple places and there's no single counter which can
  373. * be drained. Check all the queues and counters.
  374. */
  375. if (drain_all) {
  376. drain |= !list_empty(&q->queue_head);
  377. for (i = 0; i < 2; i++) {
  378. drain |= q->nr_rqs[i];
  379. drain |= q->in_flight[i];
  380. drain |= !list_empty(&q->flush_queue[i]);
  381. }
  382. }
  383. if (!drain)
  384. break;
  385. spin_unlock_irq(q->queue_lock);
  386. msleep(10);
  387. spin_lock_irq(q->queue_lock);
  388. }
  389. /*
  390. * With queue marked dead, any woken up waiter will fail the
  391. * allocation path, so the wakeup chaining is lost and we're
  392. * left with hung waiters. We need to wake up those waiters.
  393. */
  394. if (q->request_fn) {
  395. struct request_list *rl;
  396. blk_queue_for_each_rl(rl, q)
  397. for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
  398. wake_up_all(&rl->wait[i]);
  399. }
  400. }
  401. /**
  402. * blk_queue_bypass_start - enter queue bypass mode
  403. * @q: queue of interest
  404. *
  405. * In bypass mode, only the dispatch FIFO queue of @q is used. This
  406. * function makes @q enter bypass mode and drains all requests which were
  407. * throttled or issued before. On return, it's guaranteed that no request
  408. * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
  409. * inside queue or RCU read lock.
  410. */
  411. void blk_queue_bypass_start(struct request_queue *q)
  412. {
  413. bool drain;
  414. spin_lock_irq(q->queue_lock);
  415. drain = !q->bypass_depth++;
  416. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  417. spin_unlock_irq(q->queue_lock);
  418. if (drain) {
  419. spin_lock_irq(q->queue_lock);
  420. __blk_drain_queue(q, false);
  421. spin_unlock_irq(q->queue_lock);
  422. /* ensure blk_queue_bypass() is %true inside RCU read lock */
  423. synchronize_rcu();
  424. }
  425. }
  426. EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
  427. /**
  428. * blk_queue_bypass_end - leave queue bypass mode
  429. * @q: queue of interest
  430. *
  431. * Leave bypass mode and restore the normal queueing behavior.
  432. */
  433. void blk_queue_bypass_end(struct request_queue *q)
  434. {
  435. spin_lock_irq(q->queue_lock);
  436. if (!--q->bypass_depth)
  437. queue_flag_clear(QUEUE_FLAG_BYPASS, q);
  438. WARN_ON_ONCE(q->bypass_depth < 0);
  439. spin_unlock_irq(q->queue_lock);
  440. }
  441. EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
  442. /**
  443. * blk_cleanup_queue - shutdown a request queue
  444. * @q: request queue to shutdown
  445. *
  446. * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
  447. * put it. All future requests will be failed immediately with -ENODEV.
  448. */
  449. void blk_cleanup_queue(struct request_queue *q)
  450. {
  451. spinlock_t *lock = q->queue_lock;
  452. /* mark @q DYING, no new request or merges will be allowed afterwards */
  453. mutex_lock(&q->sysfs_lock);
  454. queue_flag_set_unlocked(QUEUE_FLAG_DYING, q);
  455. spin_lock_irq(lock);
  456. /*
  457. * A dying queue is permanently in bypass mode till released. Note
  458. * that, unlike blk_queue_bypass_start(), we aren't performing
  459. * synchronize_rcu() after entering bypass mode to avoid the delay
  460. * as some drivers create and destroy a lot of queues while
  461. * probing. This is still safe because blk_release_queue() will be
  462. * called only after the queue refcnt drops to zero and nothing,
  463. * RCU or not, would be traversing the queue by then.
  464. */
  465. q->bypass_depth++;
  466. queue_flag_set(QUEUE_FLAG_BYPASS, q);
  467. queue_flag_set(QUEUE_FLAG_NOMERGES, q);
  468. queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
  469. queue_flag_set(QUEUE_FLAG_DYING, q);
  470. spin_unlock_irq(lock);
  471. mutex_unlock(&q->sysfs_lock);
  472. /*
  473. * Drain all requests queued before DYING marking. Set DEAD flag to
  474. * prevent that q->request_fn() gets invoked after draining finished.
  475. */
  476. spin_lock_irq(lock);
  477. __blk_drain_queue(q, true);
  478. queue_flag_set(QUEUE_FLAG_DEAD, q);
  479. spin_unlock_irq(lock);
  480. /* @q won't process any more request, flush async actions */
  481. del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
  482. blk_sync_queue(q);
  483. spin_lock_irq(lock);
  484. if (q->queue_lock != &q->__queue_lock)
  485. q->queue_lock = &q->__queue_lock;
  486. spin_unlock_irq(lock);
  487. /* @q is and will stay empty, shutdown and put */
  488. blk_put_queue(q);
  489. }
  490. EXPORT_SYMBOL(blk_cleanup_queue);
  491. int blk_init_rl(struct request_list *rl, struct request_queue *q,
  492. gfp_t gfp_mask)
  493. {
  494. if (unlikely(rl->rq_pool))
  495. return 0;
  496. rl->q = q;
  497. rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
  498. rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
  499. init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
  500. init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
  501. rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  502. mempool_free_slab, request_cachep,
  503. gfp_mask, q->node);
  504. if (!rl->rq_pool)
  505. return -ENOMEM;
  506. return 0;
  507. }
  508. void blk_exit_rl(struct request_list *rl)
  509. {
  510. if (rl->rq_pool)
  511. mempool_destroy(rl->rq_pool);
  512. }
  513. struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
  514. {
  515. return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
  516. }
  517. EXPORT_SYMBOL(blk_alloc_queue);
  518. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  519. {
  520. struct request_queue *q;
  521. int err;
  522. q = kmem_cache_alloc_node(blk_requestq_cachep,
  523. gfp_mask | __GFP_ZERO, node_id);
  524. if (!q)
  525. return NULL;
  526. q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
  527. if (q->id < 0)
  528. goto fail_q;
  529. q->backing_dev_info.ra_pages =
  530. (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
  531. q->backing_dev_info.state = 0;
  532. q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
  533. q->backing_dev_info.name = "block";
  534. q->node = node_id;
  535. err = bdi_init(&q->backing_dev_info);
  536. if (err)
  537. goto fail_id;
  538. setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
  539. laptop_mode_timer_fn, (unsigned long) q);
  540. setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
  541. INIT_LIST_HEAD(&q->queue_head);
  542. INIT_LIST_HEAD(&q->timeout_list);
  543. INIT_LIST_HEAD(&q->icq_list);
  544. #ifdef CONFIG_BLK_CGROUP
  545. INIT_LIST_HEAD(&q->blkg_list);
  546. #endif
  547. INIT_LIST_HEAD(&q->flush_queue[0]);
  548. INIT_LIST_HEAD(&q->flush_queue[1]);
  549. INIT_LIST_HEAD(&q->flush_data_in_flight);
  550. INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
  551. kobject_init(&q->kobj, &blk_queue_ktype);
  552. mutex_init(&q->sysfs_lock);
  553. spin_lock_init(&q->__queue_lock);
  554. /*
  555. * By default initialize queue_lock to internal lock and driver can
  556. * override it later if need be.
  557. */
  558. q->queue_lock = &q->__queue_lock;
  559. /*
  560. * A queue starts its life with bypass turned on to avoid
  561. * unnecessary bypass on/off overhead and nasty surprises during
  562. * init. The initial bypass will be finished when the queue is
  563. * registered by blk_register_queue().
  564. */
  565. q->bypass_depth = 1;
  566. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  567. if (blkcg_init_queue(q))
  568. goto fail_id;
  569. return q;
  570. fail_id:
  571. ida_simple_remove(&blk_queue_ida, q->id);
  572. fail_q:
  573. kmem_cache_free(blk_requestq_cachep, q);
  574. return NULL;
  575. }
  576. EXPORT_SYMBOL(blk_alloc_queue_node);
  577. /**
  578. * blk_init_queue - prepare a request queue for use with a block device
  579. * @rfn: The function to be called to process requests that have been
  580. * placed on the queue.
  581. * @lock: Request queue spin lock
  582. *
  583. * Description:
  584. * If a block device wishes to use the standard request handling procedures,
  585. * which sorts requests and coalesces adjacent requests, then it must
  586. * call blk_init_queue(). The function @rfn will be called when there
  587. * are requests on the queue that need to be processed. If the device
  588. * supports plugging, then @rfn may not be called immediately when requests
  589. * are available on the queue, but may be called at some time later instead.
  590. * Plugged queues are generally unplugged when a buffer belonging to one
  591. * of the requests on the queue is needed, or due to memory pressure.
  592. *
  593. * @rfn is not required, or even expected, to remove all requests off the
  594. * queue, but only as many as it can handle at a time. If it does leave
  595. * requests on the queue, it is responsible for arranging that the requests
  596. * get dealt with eventually.
  597. *
  598. * The queue spin lock must be held while manipulating the requests on the
  599. * request queue; this lock will be taken also from interrupt context, so irq
  600. * disabling is needed for it.
  601. *
  602. * Function returns a pointer to the initialized request queue, or %NULL if
  603. * it didn't succeed.
  604. *
  605. * Note:
  606. * blk_init_queue() must be paired with a blk_cleanup_queue() call
  607. * when the block device is deactivated (such as at module unload).
  608. **/
  609. struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
  610. {
  611. return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
  612. }
  613. EXPORT_SYMBOL(blk_init_queue);
  614. struct request_queue *
  615. blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
  616. {
  617. struct request_queue *uninit_q, *q;
  618. uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
  619. if (!uninit_q)
  620. return NULL;
  621. q = blk_init_allocated_queue(uninit_q, rfn, lock);
  622. if (!q)
  623. blk_cleanup_queue(uninit_q);
  624. return q;
  625. }
  626. EXPORT_SYMBOL(blk_init_queue_node);
  627. struct request_queue *
  628. blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
  629. spinlock_t *lock)
  630. {
  631. if (!q)
  632. return NULL;
  633. if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
  634. return NULL;
  635. q->request_fn = rfn;
  636. q->prep_rq_fn = NULL;
  637. q->unprep_rq_fn = NULL;
  638. q->queue_flags |= QUEUE_FLAG_DEFAULT;
  639. /* Override internal queue lock with supplied lock pointer */
  640. if (lock)
  641. q->queue_lock = lock;
  642. /*
  643. * This also sets hw/phys segments, boundary and size
  644. */
  645. blk_queue_make_request(q, blk_queue_bio);
  646. q->sg_reserved_size = INT_MAX;
  647. /* init elevator */
  648. if (elevator_init(q, NULL))
  649. return NULL;
  650. return q;
  651. }
  652. EXPORT_SYMBOL(blk_init_allocated_queue);
  653. bool blk_get_queue(struct request_queue *q)
  654. {
  655. if (likely(!blk_queue_dying(q))) {
  656. __blk_get_queue(q);
  657. return true;
  658. }
  659. return false;
  660. }
  661. EXPORT_SYMBOL(blk_get_queue);
  662. static inline void blk_free_request(struct request_list *rl, struct request *rq)
  663. {
  664. if (rq->cmd_flags & REQ_ELVPRIV) {
  665. elv_put_request(rl->q, rq);
  666. if (rq->elv.icq)
  667. put_io_context(rq->elv.icq->ioc);
  668. }
  669. mempool_free(rq, rl->rq_pool);
  670. }
  671. /*
  672. * ioc_batching returns true if the ioc is a valid batching request and
  673. * should be given priority access to a request.
  674. */
  675. static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
  676. {
  677. if (!ioc)
  678. return 0;
  679. /*
  680. * Make sure the process is able to allocate at least 1 request
  681. * even if the batch times out, otherwise we could theoretically
  682. * lose wakeups.
  683. */
  684. return ioc->nr_batch_requests == q->nr_batching ||
  685. (ioc->nr_batch_requests > 0
  686. && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
  687. }
  688. /*
  689. * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
  690. * will cause the process to be a "batcher" on all queues in the system. This
  691. * is the behaviour we want though - once it gets a wakeup it should be given
  692. * a nice run.
  693. */
  694. static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
  695. {
  696. if (!ioc || ioc_batching(q, ioc))
  697. return;
  698. ioc->nr_batch_requests = q->nr_batching;
  699. ioc->last_waited = jiffies;
  700. }
  701. static void __freed_request(struct request_list *rl, int sync)
  702. {
  703. struct request_queue *q = rl->q;
  704. /*
  705. * bdi isn't aware of blkcg yet. As all async IOs end up root
  706. * blkcg anyway, just use root blkcg state.
  707. */
  708. if (rl == &q->root_rl &&
  709. rl->count[sync] < queue_congestion_off_threshold(q))
  710. blk_clear_queue_congested(q, sync);
  711. if (rl->count[sync] + 1 <= q->nr_requests) {
  712. if (waitqueue_active(&rl->wait[sync]))
  713. wake_up(&rl->wait[sync]);
  714. blk_clear_rl_full(rl, sync);
  715. }
  716. }
  717. /*
  718. * A request has just been released. Account for it, update the full and
  719. * congestion status, wake up any waiters. Called under q->queue_lock.
  720. */
  721. static void freed_request(struct request_list *rl, unsigned int flags)
  722. {
  723. struct request_queue *q = rl->q;
  724. int sync = rw_is_sync(flags);
  725. q->nr_rqs[sync]--;
  726. rl->count[sync]--;
  727. if (flags & REQ_ELVPRIV)
  728. q->nr_rqs_elvpriv--;
  729. __freed_request(rl, sync);
  730. if (unlikely(rl->starved[sync ^ 1]))
  731. __freed_request(rl, sync ^ 1);
  732. }
  733. /*
  734. * Determine if elevator data should be initialized when allocating the
  735. * request associated with @bio.
  736. */
  737. static bool blk_rq_should_init_elevator(struct bio *bio)
  738. {
  739. if (!bio)
  740. return true;
  741. /*
  742. * Flush requests do not use the elevator so skip initialization.
  743. * This allows a request to share the flush and elevator data.
  744. */
  745. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
  746. return false;
  747. return true;
  748. }
  749. /**
  750. * rq_ioc - determine io_context for request allocation
  751. * @bio: request being allocated is for this bio (can be %NULL)
  752. *
  753. * Determine io_context to use for request allocation for @bio. May return
  754. * %NULL if %current->io_context doesn't exist.
  755. */
  756. static struct io_context *rq_ioc(struct bio *bio)
  757. {
  758. #ifdef CONFIG_BLK_CGROUP
  759. if (bio && bio->bi_ioc)
  760. return bio->bi_ioc;
  761. #endif
  762. return current->io_context;
  763. }
  764. /**
  765. * __get_request - get a free request
  766. * @rl: request list to allocate from
  767. * @rw_flags: RW and SYNC flags
  768. * @bio: bio to allocate request for (can be %NULL)
  769. * @gfp_mask: allocation mask
  770. *
  771. * Get a free request from @q. This function may fail under memory
  772. * pressure or if @q is dead.
  773. *
  774. * Must be callled with @q->queue_lock held and,
  775. * Returns %NULL on failure, with @q->queue_lock held.
  776. * Returns !%NULL on success, with @q->queue_lock *not held*.
  777. */
  778. static struct request *__get_request(struct request_list *rl, int rw_flags,
  779. struct bio *bio, gfp_t gfp_mask)
  780. {
  781. struct request_queue *q = rl->q;
  782. struct request *rq;
  783. struct elevator_type *et = q->elevator->type;
  784. struct io_context *ioc = rq_ioc(bio);
  785. struct io_cq *icq = NULL;
  786. const bool is_sync = rw_is_sync(rw_flags) != 0;
  787. int may_queue;
  788. if (unlikely(blk_queue_dying(q)))
  789. return NULL;
  790. may_queue = elv_may_queue(q, rw_flags);
  791. if (may_queue == ELV_MQUEUE_NO)
  792. goto rq_starved;
  793. if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
  794. if (rl->count[is_sync]+1 >= q->nr_requests) {
  795. /*
  796. * The queue will fill after this allocation, so set
  797. * it as full, and mark this process as "batching".
  798. * This process will be allowed to complete a batch of
  799. * requests, others will be blocked.
  800. */
  801. if (!blk_rl_full(rl, is_sync)) {
  802. ioc_set_batching(q, ioc);
  803. blk_set_rl_full(rl, is_sync);
  804. } else {
  805. if (may_queue != ELV_MQUEUE_MUST
  806. && !ioc_batching(q, ioc)) {
  807. /*
  808. * The queue is full and the allocating
  809. * process is not a "batcher", and not
  810. * exempted by the IO scheduler
  811. */
  812. return NULL;
  813. }
  814. }
  815. }
  816. /*
  817. * bdi isn't aware of blkcg yet. As all async IOs end up
  818. * root blkcg anyway, just use root blkcg state.
  819. */
  820. if (rl == &q->root_rl)
  821. blk_set_queue_congested(q, is_sync);
  822. }
  823. /*
  824. * Only allow batching queuers to allocate up to 50% over the defined
  825. * limit of requests, otherwise we could have thousands of requests
  826. * allocated with any setting of ->nr_requests
  827. */
  828. if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
  829. return NULL;
  830. q->nr_rqs[is_sync]++;
  831. rl->count[is_sync]++;
  832. rl->starved[is_sync] = 0;
  833. /*
  834. * Decide whether the new request will be managed by elevator. If
  835. * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
  836. * prevent the current elevator from being destroyed until the new
  837. * request is freed. This guarantees icq's won't be destroyed and
  838. * makes creating new ones safe.
  839. *
  840. * Also, lookup icq while holding queue_lock. If it doesn't exist,
  841. * it will be created after releasing queue_lock.
  842. */
  843. if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
  844. rw_flags |= REQ_ELVPRIV;
  845. q->nr_rqs_elvpriv++;
  846. if (et->icq_cache && ioc)
  847. icq = ioc_lookup_icq(ioc, q);
  848. }