elevator.c 24 KB

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