elevator.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Block device elevator/IO-scheduler.
  3. *
  4. * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
  5. *
  6. * 30042000 Jens Axboe <axboe@kernel.dk> :
  7. *
  8. * Split the elevator a bit so that it is possible to choose a different
  9. * one or even write a new "plug in". There are three pieces:
  10. * - elevator_fn, inserts a new request in the queue list
  11. * - elevator_merge_fn, decides whether a new buffer can be merged with
  12. * an existing request
  13. * - elevator_dequeue_fn, called when a request is taken off the active list
  14. *
  15. * 20082000 Dave Jones <davej@suse.de> :
  16. * Removed tests for max-bomb-segments, which was breaking elvtune
  17. * when run without -bN
  18. *
  19. * Jens:
  20. * - Rework again to work with bio instead of buffer_heads
  21. * - loose bi_dev comparisons, partition handling is right now
  22. * - completely modularize elevator setup and teardown
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/elevator.h>
  29. #include <linux/bio.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/compiler.h>
  34. #include <linux/blktrace_api.h>
  35. #include <linux/hash.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/blk-cgroup.h>
  39. #include <trace/events/block.h>
  40. #include "blk.h"
  41. #include "blk-mq-sched.h"
  42. #include "blk-wbt.h"
  43. static DEFINE_SPINLOCK(elv_list_lock);
  44. static LIST_HEAD(elv_list);
  45. /*
  46. * Merge hash stuff.
  47. */
  48. #define rq_hash_key(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
  49. /*
  50. * Query io scheduler to see if the current process issuing bio may be
  51. * merged with rq.
  52. */
  53. static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio)
  54. {
  55. struct request_queue *q = rq->q;
  56. struct elevator_queue *e = q->elevator;
  57. if (e->uses_mq && e->type->ops.mq.allow_merge)
  58. return e->type->ops.mq.allow_merge(q, rq, bio);
  59. else if (!e->uses_mq && e->type->ops.sq.elevator_allow_bio_merge_fn)
  60. return e->type->ops.sq.elevator_allow_bio_merge_fn(q, rq, bio);
  61. return 1;
  62. }
  63. /*
  64. * can we safely merge with this request?
  65. */
  66. bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
  67. {
  68. if (!blk_rq_merge_ok(rq, bio))
  69. return false;
  70. if (!elv_iosched_allow_bio_merge(rq, bio))
  71. return false;
  72. return true;
  73. }
  74. EXPORT_SYMBOL(elv_bio_merge_ok);
  75. static bool elevator_match(const struct elevator_type *e, const char *name)
  76. {
  77. if (!strcmp(e->elevator_name, name))
  78. return true;
  79. if (e->elevator_alias && !strcmp(e->elevator_alias, name))
  80. return true;
  81. return false;
  82. }
  83. /*
  84. * Return scheduler with name 'name' and with matching 'mq capability
  85. */
  86. static struct elevator_type *elevator_find(const char *name, bool mq)
  87. {
  88. struct elevator_type *e;
  89. list_for_each_entry(e, &elv_list, list) {
  90. if (elevator_match(e, name) && (mq == e->uses_mq))
  91. return e;
  92. }
  93. return NULL;
  94. }
  95. static void elevator_put(struct elevator_type *e)
  96. {
  97. module_put(e->elevator_owner);
  98. }
  99. static struct elevator_type *elevator_get(struct request_queue *q,
  100. const char *name, bool try_loading)
  101. {
  102. struct elevator_type *e;
  103. spin_lock(&elv_list_lock);
  104. e = elevator_find(name, q->mq_ops != NULL);
  105. if (!e && try_loading) {
  106. spin_unlock(&elv_list_lock);
  107. request_module("%s-iosched", name);
  108. spin_lock(&elv_list_lock);
  109. e = elevator_find(name, q->mq_ops != NULL);
  110. }
  111. if (e && !try_module_get(e->elevator_owner))
  112. e = NULL;
  113. spin_unlock(&elv_list_lock);
  114. return e;
  115. }
  116. static char chosen_elevator[ELV_NAME_MAX];
  117. static int __init elevator_setup(char *str)
  118. {
  119. /*
  120. * Be backwards-compatible with previous kernels, so users
  121. * won't get the wrong elevator.
  122. */
  123. strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);
  124. return 1;
  125. }
  126. __setup("elevator=", elevator_setup);
  127. /* called during boot to load the elevator chosen by the elevator param */
  128. void __init load_default_elevator_module(void)
  129. {
  130. struct elevator_type *e;
  131. if (!chosen_elevator[0])
  132. return;
  133. /*
  134. * Boot parameter is deprecated, we haven't supported that for MQ.
  135. * Only look for non-mq schedulers from here.
  136. */
  137. spin_lock(&elv_list_lock);
  138. e = elevator_find(chosen_elevator, false);
  139. spin_unlock(&elv_list_lock);
  140. if (!e)
  141. request_module("%s-iosched", chosen_elevator);
  142. }
  143. static struct kobj_type elv_ktype;
  144. struct elevator_queue *elevator_alloc(struct request_queue *q,
  145. struct elevator_type *e)
  146. {
  147. struct elevator_queue *eq;
  148. eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);
  149. if (unlikely(!eq))
  150. return NULL;
  151. eq->type = e;
  152. kobject_init(&eq->kobj, &elv_ktype);
  153. mutex_init(&eq->sysfs_lock);
  154. hash_init(eq->hash);
  155. eq->uses_mq = e->uses_mq;
  156. return eq;
  157. }
  158. EXPORT_SYMBOL(elevator_alloc);
  159. static void elevator_release(struct kobject *kobj)
  160. {
  161. struct elevator_queue *e;
  162. e = container_of(kobj, struct elevator_queue, kobj);
  163. elevator_put(e->type);
  164. kfree(e);
  165. }
  166. /*
  167. * Use the default elevator specified by config boot param for non-mq devices,
  168. * or by config option. Don't try to load modules as we could be running off
  169. * async and request_module() isn't allowed from async.
  170. */
  171. int elevator_init(struct request_queue *q)
  172. {
  173. struct elevator_type *e = NULL;
  174. int err = 0;
  175. /*
  176. * q->sysfs_lock must be held to provide mutual exclusion between
  177. * elevator_switch() and here.
  178. */
  179. mutex_lock(&q->sysfs_lock);
  180. if (unlikely(q->elevator))
  181. goto out_unlock;
  182. if (*chosen_elevator) {
  183. e = elevator_get(q, chosen_elevator, false);
  184. if (!e)
  185. printk(KERN_ERR "I/O scheduler %s not found\n",
  186. chosen_elevator);
  187. }
  188. if (!e)
  189. e = elevator_get(q, CONFIG_DEFAULT_IOSCHED, false);
  190. if (!e) {
  191. printk(KERN_ERR
  192. "Default I/O scheduler not found. Using noop.\n");
  193. e = elevator_get(q, "noop", false);
  194. }
  195. err = e->ops.sq.elevator_init_fn(q, e);
  196. if (err)
  197. elevator_put(e);
  198. out_unlock:
  199. mutex_unlock(&q->sysfs_lock);
  200. return err;
  201. }
  202. void elevator_exit(struct request_queue *q, struct elevator_queue *e)
  203. {
  204. mutex_lock(&e->sysfs_lock);
  205. if (e->uses_mq && e->type->ops.mq.exit_sched)
  206. blk_mq_exit_sched(q, e);
  207. else if (!e->uses_mq && e->type->ops.sq.elevator_exit_fn)
  208. e->type->ops.sq.elevator_exit_fn(e);
  209. mutex_unlock(&e->sysfs_lock);
  210. kobject_put(&e->kobj);
  211. }
  212. static inline void __elv_rqhash_del(struct request *rq)
  213. {
  214. hash_del(&rq->hash);
  215. rq->rq_flags &= ~RQF_HASHED;
  216. }
  217. void elv_rqhash_del(struct request_queue *q, struct request *rq)
  218. {
  219. if (ELV_ON_HASH(rq))
  220. __elv_rqhash_del(rq);
  221. }
  222. EXPORT_SYMBOL_GPL(elv_rqhash_del);
  223. void elv_rqhash_add(struct request_queue *q, struct request *rq)
  224. {
  225. struct elevator_queue *e = q->elevator;
  226. BUG_ON(ELV_ON_HASH(rq));
  227. hash_add(e->hash, &rq->hash, rq_hash_key(rq));
  228. rq->rq_flags |= RQF_HASHED;
  229. }
  230. EXPORT_SYMBOL_GPL(elv_rqhash_add);
  231. void elv_rqhash_reposition(struct request_queue *q, struct request *rq)
  232. {
  233. __elv_rqhash_del(rq);
  234. elv_rqhash_add(q, rq);
  235. }
  236. struct request *elv_rqhash_find(struct request_queue *q, sector_t offset)
  237. {
  238. struct elevator_queue *e = q->elevator;
  239. struct hlist_node *next;
  240. struct request *rq;
  241. hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {
  242. BUG_ON(!ELV_ON_HASH(rq));
  243. if (unlikely(!rq_mergeable(rq))) {
  244. __elv_rqhash_del(rq);
  245. continue;
  246. }
  247. if (rq_hash_key(rq) == offset)
  248. return rq;
  249. }
  250. return NULL;
  251. }
  252. /*
  253. * RB-tree support functions for inserting/lookup/removal of requests
  254. * in a sorted RB tree.
  255. */
  256. void elv_rb_add(struct rb_root *root, struct request *rq)
  257. {
  258. struct rb_node **p = &root->rb_node;
  259. struct rb_node *parent = NULL;
  260. struct request *__rq;
  261. while (*p) {
  262. parent = *p;
  263. __rq = rb_entry(parent, struct request, rb_node);
  264. if (blk_rq_pos(rq) < blk_rq_pos(__rq))
  265. p = &(*p)->rb_left;
  266. else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))
  267. p = &(*p)->rb_right;
  268. }
  269. rb_link_node(&rq->rb_node, parent, p);
  270. rb_insert_color(&rq->rb_node, root);
  271. }
  272. EXPORT_SYMBOL(elv_rb_add);
  273. void elv_rb_del(struct rb_root *root, struct request *rq)
  274. {
  275. BUG_ON(RB_EMPTY_NODE(&rq->rb_node));
  276. rb_erase(&rq->rb_node, root);
  277. RB_CLEAR_NODE(&rq->rb_node);
  278. }
  279. EXPORT_SYMBOL(elv_rb_del);
  280. struct request *elv_rb_find(struct rb_root *root, sector_t sector)
  281. {
  282. struct rb_node *n = root->rb_node;
  283. struct request *rq;
  284. while (n) {
  285. rq = rb_entry(n, struct request, rb_node);
  286. if (sector < blk_rq_pos(rq))
  287. n = n->rb_left;
  288. else if (sector > blk_rq_pos(rq))
  289. n = n->rb_right;
  290. else
  291. return rq;
  292. }
  293. return NULL;
  294. }
  295. EXPORT_SYMBOL(elv_rb_find);
  296. /*
  297. * Insert rq into dispatch queue of q. Queue lock must be held on
  298. * entry. rq is sort instead into the dispatch queue. To be used by
  299. * specific elevators.
  300. */
  301. void elv_dispatch_sort(struct request_queue *q, struct request *rq)
  302. {
  303. sector_t boundary;
  304. struct list_head *entry;
  305. if (q->last_merge == rq)
  306. q->last_merge = NULL;
  307. elv_rqhash_del(q, rq);
  308. q->nr_sorted--;
  309. boundary = q->end_sector;
  310. list_for_each_prev(entry, &q->queue_head) {
  311. struct request *pos = list_entry_rq(entry);
  312. if (req_op(rq) != req_op(pos))
  313. break;
  314. if (rq_data_dir(rq) != rq_data_dir(pos))
  315. break;
  316. if (pos->rq_flags & (RQF_STARTED | RQF_SOFTBARRIER))
  317. break;
  318. if (blk_rq_pos(rq) >= boundary) {
  319. if (blk_rq_pos(pos) < boundary)
  320. continue;
  321. } else {
  322. if (blk_rq_pos(pos) >= boundary)
  323. break;
  324. }
  325. if (blk_rq_pos(rq) >= blk_rq_pos(pos))
  326. break;
  327. }
  328. list_add(&rq->queuelist, entry);
  329. }
  330. EXPORT_SYMBOL(elv_dispatch_sort);
  331. /*
  332. * Insert rq into dispatch queue of q. Queue lock must be held on
  333. * entry. rq is added to the back of the dispatch queue. To be used by
  334. * specific elevators.
  335. */
  336. void elv_dispatch_add_tail(struct request_queue *q, struct request *rq)
  337. {
  338. if (q->last_merge == rq)
  339. q->last_merge = NULL;
  340. elv_rqhash_del(q, rq);
  341. q->nr_sorted--;
  342. q->end_sector = rq_end_sector(rq);
  343. q->boundary_rq = rq;
  344. list_add_tail(&rq->queuelist, &q->queue_head);
  345. }
  346. EXPORT_SYMBOL(elv_dispatch_add_tail);
  347. enum elv_merge elv_merge(struct request_queue *q, struct request **req,
  348. struct bio *bio)
  349. {
  350. struct elevator_queue *e = q->elevator;
  351. struct request *__rq;
  352. /*
  353. * Levels of merges:
  354. * nomerges: No merges at all attempted
  355. * noxmerges: Only simple one-hit cache try
  356. * merges: All merge tries attempted
  357. */
  358. if (blk_queue_nomerges(q) || !bio_mergeable(bio))
  359. return ELEVATOR_NO_MERGE;
  360. /*
  361. * First try one-hit cache.
  362. */
  363. if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {
  364. enum elv_merge ret = blk_try_merge(q->last_merge, bio);
  365. if (ret != ELEVATOR_NO_MERGE) {
  366. *req = q->last_merge;
  367. return ret;
  368. }
  369. }
  370. if (blk_queue_noxmerges(q))
  371. return ELEVATOR_NO_MERGE;
  372. /*
  373. * See if our hash lookup can find a potential backmerge.
  374. */
  375. __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
  376. if (__rq && elv_bio_merge_ok(__rq, bio)) {
  377. *req = __rq;
  378. return ELEVATOR_BACK_MERGE;
  379. }
  380. if (e->uses_mq && e->type->ops.mq.request_merge)
  381. return e->type->ops.mq.request_merge(q, req, bio);
  382. else if (!e->uses_mq && e->type->ops.sq.elevator_merge_fn)
  383. return e->type->ops.sq.elevator_merge_fn(q, req, bio);
  384. return ELEVATOR_NO_MERGE;
  385. }
  386. /*
  387. * Attempt to do an insertion back merge. Only check for the case where
  388. * we can append 'rq' to an existing request, so we can throw 'rq' away
  389. * afterwards.
  390. *
  391. * Returns true if we merged, false otherwise
  392. */
  393. bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq)
  394. {
  395. struct request *__rq;
  396. bool ret;
  397. if (blk_queue_nomerges(q))
  398. return false;
  399. /*
  400. * First try one-hit cache.
  401. */
  402. if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
  403. return true;
  404. if (blk_queue_noxmerges(q))
  405. return false;
  406. ret = false;
  407. /*
  408. * See if our hash lookup can find a potential backmerge.
  409. */
  410. while (1) {
  411. __rq = elv_rqhash_find(q, blk_rq_pos(rq));
  412. if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
  413. break;
  414. /* The merged request could be merged with others, try again */
  415. ret = true;
  416. rq = __rq;
  417. }
  418. return ret;
  419. }
  420. void elv_merged_request(struct request_queue *q, struct request *rq,
  421. enum elv_merge type)
  422. {
  423. struct elevator_queue *e = q->elevator;
  424. if (e->uses_mq && e->type->ops.mq.request_merged)
  425. e->type->ops.mq.request_merged(q, rq, type);
  426. else if (!e->uses_mq && e->type->ops.sq.elevator_merged_fn)
  427. e->type->ops.sq.elevator_merged_fn(q, rq, type);
  428. if (type == ELEVATOR_BACK_MERGE)
  429. elv_rqhash_reposition(q, rq);
  430. q->last_merge = rq;
  431. }
  432. void elv_merge_requests(struct request_queue *q, struct request *rq,
  433. struct request *next)
  434. {
  435. struct elevator_queue *e = q->elevator;
  436. bool next_sorted = false;
  437. if (e->uses_mq && e->type->ops.mq.requests_merged)
  438. e->type->ops.mq.requests_merged(q, rq, next);
  439. else if (e->type->ops.sq.elevator_merge_req_fn) {
  440. next_sorted = (__force bool)(next->rq_flags & RQF_SORTED);
  441. if (next_sorted)
  442. e->type->ops.sq.elevator_merge_req_fn(q, rq, next);
  443. }
  444. elv_rqhash_reposition(q, rq);
  445. if (next_sorted) {
  446. elv_rqhash_del(q, next);
  447. q->nr_sorted--;
  448. }
  449. q->last_merge = rq;
  450. }
  451. void elv_bio_merged(struct request_queue *q, struct request *rq,
  452. struct bio *bio)
  453. {
  454. struct elevator_queue *e = q->elevator;
  455. if (WARN_ON_ONCE(e->uses_mq))
  456. return;
  457. if (e->type->ops.sq.elevator_bio_merged_fn)
  458. e->type->ops.sq.elevator_bio_merged_fn(q, rq, bio);
  459. }
  460. #ifdef CONFIG_PM
  461. static void blk_pm_requeue_request(struct request *rq)
  462. {
  463. if (rq->q->dev && !(rq->rq_flags & RQF_PM))
  464. rq->q->nr_pending--;
  465. }
  466. static void blk_pm_add_request(struct request_queue *q, struct request *rq)
  467. {
  468. if (q->dev && !(rq->rq_flags & RQF_PM) && q->nr_pending++ == 0 &&
  469. (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
  470. pm_request_resume(q->dev);
  471. }
  472. #else
  473. static inline void blk_pm_requeue_request(struct request *rq) {}
  474. static inline void blk_pm_add_request(struct request_queue *q,
  475. struct request *rq)
  476. {
  477. }
  478. #endif
  479. void elv_requeue_request(struct request_queue *q, struct request *rq)
  480. {
  481. /*
  482. * it already went through dequeue, we need to decrement the
  483. * in_flight count again
  484. */
  485. if (blk_account_rq(rq)) {
  486. q->in_flight[rq_is_sync(rq)]--;
  487. if (rq->rq_flags & RQF_SORTED)
  488. elv_deactivate_rq(q, rq);
  489. }
  490. rq->rq_flags &= ~RQF_STARTED;
  491. blk_pm_requeue_request(rq);
  492. __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
  493. }
  494. void elv_drain_elevator(struct request_queue *q)
  495. {
  496. struct elevator_queue *e = q->elevator;
  497. static int printed;
  498. if (WARN_ON_ONCE(e->uses_mq))
  499. return;
  500. lockdep_assert_held(q->queue_lock);
  501. while (e->type->ops.sq.elevator_dispatch_fn(q, 1))
  502. ;
  503. if (q->nr_sorted && !blk_queue_is_zoned(q) && printed++ < 10 ) {
  504. printk(KERN_ERR "%s: forced dispatching is broken "
  505. "(nr_sorted=%u), please report this\n",
  506. q->elevator->type->elevator_name, q->nr_sorted);
  507. }
  508. }
  509. void __elv_add_request(struct request_queue *q, struct request *rq, int where)
  510. {
  511. trace_block_rq_insert(q, rq);
  512. blk_pm_add_request(q, rq);
  513. rq->q = q;
  514. if (rq->rq_flags & RQF_SOFTBARRIER) {
  515. /* barriers are scheduling boundary, update end_sector */
  516. if (!blk_rq_is_passthrough(rq)) {
  517. q->end_sector = rq_end_sector(rq);
  518. q->boundary_rq = rq;
  519. }
  520. } else if (!(rq->rq_flags & RQF_ELVPRIV) &&
  521. (where == ELEVATOR_INSERT_SORT ||
  522. where == ELEVATOR_INSERT_SORT_MERGE))
  523. where = ELEVATOR_INSERT_BACK;
  524. switch (where) {
  525. case ELEVATOR_INSERT_REQUEUE:
  526. case ELEVATOR_INSERT_FRONT:
  527. rq->rq_flags |= RQF_SOFTBARRIER;
  528. list_add(&rq->queuelist, &q->queue_head);
  529. break;
  530. case ELEVATOR_INSERT_BACK:
  531. rq->rq_flags |= RQF_SOFTBARRIER;
  532. elv_drain_elevator(q);
  533. list_add_tail(&rq->queuelist, &q->queue_head);
  534. /*
  535. * We kick the queue here for the following reasons.
  536. * - The elevator might have returned NULL previously
  537. * to delay requests and returned them now. As the
  538. * queue wasn't empty before this request, ll_rw_blk
  539. * won't run the queue on return, resulting in hang.
  540. * - Usually, back inserted requests won't be merged
  541. * with anything. There's no point in delaying queue
  542. * processing.
  543. */
  544. __blk_run_queue(q);
  545. break;
  546. case ELEVATOR_INSERT_SORT_MERGE:
  547. /*
  548. * If we succeed in merging this request with one in the
  549. * queue already, we are done - rq has now been freed,
  550. * so no need to do anything further.
  551. */
  552. if (elv_attempt_insert_merge(q, rq))
  553. break;
  554. /* fall through */
  555. case ELEVATOR_INSERT_SORT:
  556. BUG_ON(blk_rq_is_passthrough(rq));
  557. rq->rq_flags |= RQF_SORTED;
  558. q->nr_sorted++;
  559. if (rq_mergeable(rq)) {
  560. elv_rqhash_add(q, rq);
  561. if (!q->last_merge)
  562. q->last_merge = rq;
  563. }
  564. /*
  565. * Some ioscheds (cfq) run q->request_fn directly, so
  566. * rq cannot be accessed after calling
  567. * elevator_add_req_fn.
  568. */
  569. q->elevator->type->ops.sq.elevator_add_req_fn(q, rq);
  570. break;
  571. case ELEVATOR_INSERT_FLUSH:
  572. rq->rq_flags |= RQF_SOFTBARRIER;
  573. blk_insert_flush(rq);
  574. break;
  575. default:
  576. printk(KERN_ERR "%s: bad insertion point %d\n",
  577. __func__, where);
  578. BUG();
  579. }
  580. }
  581. EXPORT_SYMBOL(__elv_add_request);
  582. void elv_add_request(struct request_queue *q, struct request *rq, int where)
  583. {
  584. unsigned long flags;
  585. spin_lock_irqsave(q->queue_lock, flags);
  586. __elv_add_request(q, rq, where);
  587. spin_unlock_irqrestore(q->queue_lock, flags);
  588. }
  589. EXPORT_SYMBOL(elv_add_request);
  590. struct request *elv_latter_request(struct request_queue *q, struct request *rq)
  591. {
  592. struct elevator_queue *e = q->elevator;
  593. if (e->uses_mq && e->type->ops.mq.next_request)
  594. return e->type->ops.mq.next_request(q, rq);
  595. else if (!e->uses_mq && e->type->ops.sq.elevator_latter_req_fn)
  596. return e->type->ops.sq.elevator_latter_req_fn(q, rq);
  597. return NULL;
  598. }
  599. struct request *elv_former_request(struct request_queue *q, struct request *rq)
  600. {
  601. struct elevator_queue *e = q->elevator;
  602. if (e->uses_mq && e->type->ops.mq.former_request)
  603. return e->type->ops.mq.former_request(q, rq);
  604. if (!e->uses_mq && e->type->ops.sq.elevator_former_req_fn)
  605. return e->type->ops.sq.elevator_former_req_fn(q, rq);
  606. return NULL;
  607. }
  608. int elv_set_request(struct request_queue *q, struct request *rq,
  609. struct bio *bio, gfp_t gfp_mask)
  610. {
  611. struct elevator_queue *e = q->elevator;
  612. if (WARN_ON_ONCE(e->uses_mq))
  613. return 0;
  614. if (e->type->ops.sq.elevator_set_req_fn)
  615. return e->type->ops.sq.elevator_set_req_fn(q, rq, bio, gfp_mask);
  616. return 0;
  617. }
  618. void elv_put_request(struct request_queue *q, struct request *rq)
  619. {
  620. struct elevator_queue *e = q->elevator;
  621. if (WARN_ON_ONCE(e->uses_mq))
  622. return;
  623. if (e->type->ops.sq.elevator_put_req_fn)
  624. e->type->ops.sq.elevator_put_req_fn(rq);
  625. }
  626. int elv_may_queue(struct request_queue *q, unsigned int op)
  627. {
  628. struct elevator_queue *e = q->elevator;
  629. if (WARN_ON_ONCE(e->uses_mq))
  630. return 0;
  631. if (e->type->ops.sq.elevator_may_queue_fn)
  632. return e->type->ops.sq.elevator_may_queue_fn(q, op);
  633. return ELV_MQUEUE_MAY;
  634. }
  635. void elv_completed_request(struct request_queue *q, struct request *rq)
  636. {
  637. struct elevator_queue *e = q->elevator;
  638. if (WARN_ON_ONCE(e->uses_mq))
  639. return;
  640. /*
  641. * request is released from the driver, io must be done
  642. */
  643. if (blk_account_rq(rq)) {
  644. q->in_flight[rq_is_sync(rq)]--;
  645. if ((rq->rq_flags & RQF_SORTED) &&
  646. e->type->ops.sq.elevator_completed_req_fn)
  647. e->type->ops.sq.elevator_completed_req_fn(q, rq);
  648. }
  649. }
  650. #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  651. static ssize_t
  652. elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  653. {
  654. struct elv_fs_entry *entry = to_elv(attr);
  655. struct elevator_queue *e;
  656. ssize_t error;
  657. if (!entry->show)
  658. return -EIO;
  659. e = container_of(kobj, struct elevator_queue, kobj);
  660. mutex_lock(&e->sysfs_lock);
  661. error = e->type ? entry->show(e, page) : -ENOENT;
  662. mutex_unlock(&e->sysfs_lock);
  663. return error;
  664. }
  665. static ssize_t
  666. elv_attr_store(struct kobject *kobj, struct attribute *attr,
  667. const char *page, size_t length)
  668. {
  669. struct elv_fs_entry *entry = to_elv(attr);
  670. struct elevator_queue *e;
  671. ssize_t error;
  672. if (!entry->store)
  673. return -EIO;
  674. e = container_of(kobj, struct elevator_queue, kobj);
  675. mutex_lock(&e->sysfs_lock);
  676. error = e->type ? entry->store(e, page, length) : -ENOENT;
  677. mutex_unlock(&e->sysfs_lock);
  678. return error;
  679. }
  680. static const struct sysfs_ops elv_sysfs_ops = {
  681. .show = elv_attr_show,
  682. .store = elv_attr_store,
  683. };
  684. static struct kobj_type elv_ktype = {
  685. .sysfs_ops = &elv_sysfs_ops,
  686. .release = elevator_release,
  687. };
  688. int elv_register_queue(struct request_queue *q)
  689. {
  690. struct elevator_queue *e = q->elevator;
  691. int error;
  692. lockdep_assert_held(&q->sysfs_lock);
  693. error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
  694. if (!error) {
  695. struct elv_fs_entry *attr = e->type->elevator_attrs;
  696. if (attr) {
  697. while (attr->attr.name) {
  698. if (sysfs_create_file(&e->kobj, &attr->attr))
  699. break;
  700. attr++;
  701. }
  702. }
  703. kobject_uevent(&e->kobj, KOBJ_ADD);
  704. e->registered = 1;
  705. if (!e->uses_mq && e->type->ops.sq.elevator_registered_fn)
  706. e->type->ops.sq.elevator_registered_fn(q);
  707. }
  708. return error;
  709. }
  710. void elv_unregister_queue(struct request_queue *q)
  711. {
  712. lockdep_assert_held(&q->sysfs_lock);
  713. if (q) {
  714. struct elevator_queue *e = q->elevator;
  715. kobject_uevent(&e->kobj, KOBJ_REMOVE);
  716. kobject_del(&e->kobj);
  717. e->registered = 0;
  718. /* Re-enable throttling in case elevator disabled it */
  719. wbt_enable_default(q);
  720. }
  721. }
  722. int elv_register(struct elevator_type *e)
  723. {
  724. char *def = "";
  725. /* create icq_cache if requested */
  726. if (e->icq_size) {
  727. if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
  728. WARN_ON(e->icq_align < __alignof__(struct io_cq)))
  729. return -EINVAL;
  730. snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
  731. "%s_io_cq", e->elevator_name);
  732. e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
  733. e->icq_align, 0, NULL);
  734. if (!e->icq_cache)
  735. return -ENOMEM;
  736. }
  737. /* register, don't allow duplicate names */
  738. spin_lock(&elv_list_lock);
  739. if (elevator_find(e->elevator_name, e->uses_mq)) {
  740. spin_unlock(&elv_list_lock);
  741. kmem_cache_destroy(e->icq_cache);
  742. return -EBUSY;
  743. }
  744. list_add_tail(&e->list, &elv_list);
  745. spin_unlock(&elv_list_lock);
  746. /* print pretty message */
  747. if (elevator_match(e, chosen_elevator) ||
  748. (!*chosen_elevator &&
  749. elevator_match(e, CONFIG_DEFAULT_IOSCHED)))
  750. def = " (default)";
  751. printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
  752. def);
  753. return 0;
  754. }
  755. EXPORT_SYMBOL_GPL(elv_register);
  756. void elv_unregister(struct elevator_type *e)
  757. {
  758. /* unregister */
  759. spin_lock(&elv_list_lock);
  760. list_del_init(&e->list);
  761. spin_unlock(&elv_list_lock);
  762. /*
  763. * Destroy icq_cache if it exists. icq's are RCU managed. Make
  764. * sure all RCU operations are complete before proceeding.
  765. */
  766. if (e->icq_cache) {
  767. rcu_barrier();
  768. kmem_cache_destroy(e->icq_cache);
  769. e->icq_cache = NULL;
  770. }
  771. }
  772. EXPORT_SYMBOL_GPL(elv_unregister);
  773. int elevator_switch_mq(struct request_queue *q,
  774. struct elevator_type *new_e)
  775. {
  776. int ret;
  777. lockdep_assert_held(&q->sysfs_lock);
  778. if (q->elevator) {
  779. if (q->elevator->registered)
  780. elv_unregister_queue(q);
  781. ioc_clear_queue(q);
  782. elevator_exit(q, q->elevator);
  783. }
  784. ret = blk_mq_init_sched(q, new_e);
  785. if (ret)
  786. goto out;
  787. if (new_e) {
  788. ret = elv_register_queue(q);
  789. if (ret) {
  790. elevator_exit(q, q->elevator);
  791. goto out;
  792. }
  793. }
  794. if (new_e)
  795. blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
  796. else
  797. blk_add_trace_msg(q, "elv switch: none");
  798. out:
  799. return ret;
  800. }
  801. /*
  802. * For blk-mq devices, we default to using mq-deadline, if available, for single
  803. * queue devices. If deadline isn't available OR we have multiple queues,
  804. * default to "none".
  805. */
  806. int elevator_init_mq(struct request_queue *q)
  807. {
  808. struct elevator_type *e;
  809. int err = 0;
  810. if (q->nr_hw_queues != 1)
  811. return 0;
  812. /*
  813. * q->sysfs_lock must be held to provide mutual exclusion between
  814. * elevator_switch() and here.
  815. */
  816. mutex_lock(&q->sysfs_lock);
  817. if (unlikely(q->elevator))
  818. goto out_unlock;
  819. e = elevator_get(q, "mq-deadline", false);
  820. if (!e)
  821. goto out_unlock;
  822. err = blk_mq_init_sched(q, e);
  823. if (err)
  824. elevator_put(e);
  825. out_unlock:
  826. mutex_unlock(&q->sysfs_lock);
  827. return err;
  828. }
  829. /*
  830. * switch to new_e io scheduler. be careful not to introduce deadlocks -
  831. * we don't free the old io scheduler, before we have allocated what we
  832. * need for the new one. this way we have a chance of going back to the old
  833. * one, if the new one fails init for some reason.
  834. */
  835. static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
  836. {
  837. struct elevator_queue *old = q->elevator;
  838. bool old_registered = false;
  839. int err;
  840. lockdep_assert_held(&q->sysfs_lock);
  841. if (q->mq_ops) {
  842. blk_mq_freeze_queue(q);
  843. blk_mq_quiesce_queue(q);
  844. err = elevator_switch_mq(q, new_e);
  845. blk_mq_unquiesce_queue(q);
  846. blk_mq_unfreeze_queue(q);
  847. return err;
  848. }
  849. /*
  850. * Turn on BYPASS and drain all requests w/ elevator private data.
  851. * Block layer doesn't call into a quiesced elevator - all requests
  852. * are directly put on the dispatch list without elevator data
  853. * using INSERT_BACK. All requests have SOFTBARRIER set and no
  854. * merge happens either.
  855. */
  856. if (old) {
  857. old_registered = old->registered;
  858. blk_queue_bypass_start(q);
  859. /* unregister and clear all auxiliary data of the old elevator */
  860. if (old_registered)
  861. elv_unregister_queue(q);
  862. ioc_clear_queue(q);
  863. }
  864. /* allocate, init and register new elevator */
  865. err = new_e->ops.sq.elevator_init_fn(q, new_e);
  866. if (err)
  867. goto fail_init;
  868. err = elv_register_queue(q);
  869. if (err)
  870. goto fail_register;
  871. /* done, kill the old one and finish */
  872. if (old) {
  873. elevator_exit(q, old);
  874. blk_queue_bypass_end(q);
  875. }
  876. blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
  877. return 0;
  878. fail_register:
  879. elevator_exit(q, q->elevator);
  880. fail_init:
  881. /* switch failed, restore and re-register old elevator */
  882. if (old) {
  883. q->elevator = old;
  884. elv_register_queue(q);
  885. blk_queue_bypass_end(q);
  886. }
  887. return err;
  888. }
  889. /*
  890. * Switch this queue to the given IO scheduler.
  891. */
  892. static int __elevator_change(struct request_queue *q, const char *name)
  893. {
  894. char elevator_name[ELV_NAME_MAX];
  895. struct elevator_type *e;
  896. /* Make sure queue is not in the middle of being removed */
  897. if (!test_bit(QUEUE_FLAG_REGISTERED, &q->queue_flags))
  898. return -ENOENT;
  899. /*
  900. * Special case for mq, turn off scheduling
  901. */
  902. if (q->mq_ops && !strncmp(name, "none", 4))
  903. return elevator_switch(q, NULL);
  904. strlcpy(elevator_name, name, sizeof(elevator_name));
  905. e = elevator_get(q, strstrip(elevator_name), true);
  906. if (!e)
  907. return -EINVAL;
  908. if (q->elevator && elevator_match(q->elevator->type, elevator_name)) {
  909. elevator_put(e);
  910. return 0;
  911. }
  912. return elevator_switch(q, e);
  913. }
  914. static inline bool elv_support_iosched(struct request_queue *q)
  915. {
  916. if (q->mq_ops && q->tag_set && (q->tag_set->flags &
  917. BLK_MQ_F_NO_SCHED))
  918. return false;
  919. return true;
  920. }
  921. ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  922. size_t count)
  923. {
  924. int ret;
  925. if (!(q->mq_ops || q->request_fn) || !elv_support_iosched(q))
  926. return count;
  927. ret = __elevator_change(q, name);
  928. if (!ret)
  929. return count;
  930. return ret;
  931. }
  932. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  933. {
  934. struct elevator_queue *e = q->elevator;
  935. struct elevator_type *elv = NULL;
  936. struct elevator_type *__e;
  937. bool uses_mq = q->mq_ops != NULL;
  938. int len = 0;
  939. if (!queue_is_rq_based(q))
  940. return sprintf(name, "none\n");
  941. if (!q->elevator)
  942. len += sprintf(name+len, "[none] ");
  943. else
  944. elv = e->type;
  945. spin_lock(&elv_list_lock);
  946. list_for_each_entry(__e, &elv_list, list) {
  947. if (elv && elevator_match(elv, __e->elevator_name) &&
  948. (__e->uses_mq == uses_mq)) {
  949. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  950. continue;
  951. }
  952. if (__e->uses_mq && q->mq_ops && elv_support_iosched(q))
  953. len += sprintf(name+len, "%s ", __e->elevator_name);
  954. else if (!__e->uses_mq && !q->mq_ops)
  955. len += sprintf(name+len, "%s ", __e->elevator_name);
  956. }
  957. spin_unlock(&elv_list_lock);
  958. if (q->mq_ops && q->elevator)
  959. len += sprintf(name+len, "none");
  960. len += sprintf(len+name, "\n");
  961. return len;
  962. }
  963. struct request *elv_rb_former_request(struct request_queue *q,
  964. struct request *rq)
  965. {
  966. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  967. if (rbprev)
  968. return rb_entry_rq(rbprev);
  969. return NULL;
  970. }
  971. EXPORT_SYMBOL(elv_rb_former_request);
  972. struct request *elv_rb_latter_request(struct request_queue *q,
  973. struct request *rq)
  974. {
  975. struct rb_node *rbnext = rb_next(&rq->rb_node);
  976. if (rbnext)
  977. return rb_entry_rq(rbnext);
  978. return NULL;
  979. }
  980. EXPORT_SYMBOL(elv_rb_latter_request);