elevator.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. 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 && elv_bio_merge_ok(q->last_merge, bio)) {
  352. ret = blk_try_merge(q->last_merge, bio);
  353. if (ret != ELEVATOR_NO_MERGE) {
  354. *req = q->last_merge;
  355. return ret;
  356. }
  357. }
  358. if (blk_queue_noxmerges(q))
  359. return ELEVATOR_NO_MERGE;
  360. /*
  361. * See if our hash lookup can find a potential backmerge.
  362. */
  363. __rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
  364. if (__rq && elv_bio_merge_ok(__rq, bio)) {
  365. *req = __rq;
  366. return ELEVATOR_BACK_MERGE;
  367. }
  368. if (e->type->ops.elevator_merge_fn)
  369. return e->type->ops.elevator_merge_fn(q, req, bio);
  370. return ELEVATOR_NO_MERGE;
  371. }
  372. /*
  373. * Attempt to do an insertion back merge. Only check for the case where
  374. * we can append 'rq' to an existing request, so we can throw 'rq' away
  375. * afterwards.
  376. *
  377. * Returns true if we merged, false otherwise
  378. */
  379. static bool elv_attempt_insert_merge(struct request_queue *q,
  380. struct request *rq)
  381. {
  382. struct request *__rq;
  383. bool ret;
  384. if (blk_queue_nomerges(q))
  385. return false;
  386. /*
  387. * First try one-hit cache.
  388. */
  389. if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))
  390. return true;
  391. if (blk_queue_noxmerges(q))
  392. return false;
  393. ret = false;
  394. /*
  395. * See if our hash lookup can find a potential backmerge.
  396. */
  397. while (1) {
  398. __rq = elv_rqhash_find(q, blk_rq_pos(rq));
  399. if (!__rq || !blk_attempt_req_merge(q, __rq, rq))
  400. break;
  401. /* The merged request could be merged with others, try again */
  402. ret = true;
  403. rq = __rq;
  404. }
  405. return ret;
  406. }
  407. void elv_merged_request(struct request_queue *q, struct request *rq, int type)
  408. {
  409. struct elevator_queue *e = q->elevator;
  410. if (e->type->ops.elevator_merged_fn)
  411. e->type->ops.elevator_merged_fn(q, rq, type);
  412. if (type == ELEVATOR_BACK_MERGE)
  413. elv_rqhash_reposition(q, rq);
  414. q->last_merge = rq;
  415. }
  416. void elv_merge_requests(struct request_queue *q, struct request *rq,
  417. struct request *next)
  418. {
  419. struct elevator_queue *e = q->elevator;
  420. const int next_sorted = next->cmd_flags & REQ_SORTED;
  421. if (next_sorted && e->type->ops.elevator_merge_req_fn)
  422. e->type->ops.elevator_merge_req_fn(q, rq, next);
  423. elv_rqhash_reposition(q, rq);
  424. if (next_sorted) {
  425. elv_rqhash_del(q, next);
  426. q->nr_sorted--;
  427. }
  428. q->last_merge = rq;
  429. }
  430. void elv_bio_merged(struct request_queue *q, struct request *rq,
  431. struct bio *bio)
  432. {
  433. struct elevator_queue *e = q->elevator;
  434. if (e->type->ops.elevator_bio_merged_fn)
  435. e->type->ops.elevator_bio_merged_fn(q, rq, bio);
  436. }
  437. #ifdef CONFIG_PM
  438. static void blk_pm_requeue_request(struct request *rq)
  439. {
  440. if (rq->q->dev && !(rq->cmd_flags & REQ_PM))
  441. rq->q->nr_pending--;
  442. }
  443. static void blk_pm_add_request(struct request_queue *q, struct request *rq)
  444. {
  445. if (q->dev && !(rq->cmd_flags & REQ_PM) && q->nr_pending++ == 0 &&
  446. (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
  447. pm_request_resume(q->dev);
  448. }
  449. #else
  450. static inline void blk_pm_requeue_request(struct request *rq) {}
  451. static inline void blk_pm_add_request(struct request_queue *q,
  452. struct request *rq)
  453. {
  454. }
  455. #endif
  456. void elv_requeue_request(struct request_queue *q, struct request *rq)
  457. {
  458. /*
  459. * it already went through dequeue, we need to decrement the
  460. * in_flight count again
  461. */
  462. if (blk_account_rq(rq)) {
  463. q->in_flight[rq_is_sync(rq)]--;
  464. if (rq->cmd_flags & REQ_SORTED)
  465. elv_deactivate_rq(q, rq);
  466. }
  467. rq->cmd_flags &= ~REQ_STARTED;
  468. blk_pm_requeue_request(rq);
  469. __elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
  470. }
  471. void elv_drain_elevator(struct request_queue *q)
  472. {
  473. static int printed;
  474. lockdep_assert_held(q->queue_lock);
  475. while (q->elevator->type->ops.elevator_dispatch_fn(q, 1))
  476. ;
  477. if (q->nr_sorted && printed++ < 10) {
  478. printk(KERN_ERR "%s: forced dispatching is broken "
  479. "(nr_sorted=%u), please report this\n",
  480. q->elevator->type->elevator_name, q->nr_sorted);
  481. }
  482. }
  483. void __elv_add_request(struct request_queue *q, struct request *rq, int where)
  484. {
  485. trace_block_rq_insert(q, rq);
  486. blk_pm_add_request(q, rq);
  487. rq->q = q;
  488. if (rq->cmd_flags & REQ_SOFTBARRIER) {
  489. /* barriers are scheduling boundary, update end_sector */
  490. if (rq->cmd_type == REQ_TYPE_FS) {
  491. q->end_sector = rq_end_sector(rq);
  492. q->boundary_rq = rq;
  493. }
  494. } else if (!(rq->cmd_flags & REQ_ELVPRIV) &&
  495. (where == ELEVATOR_INSERT_SORT ||
  496. where == ELEVATOR_INSERT_SORT_MERGE))
  497. where = ELEVATOR_INSERT_BACK;
  498. switch (where) {
  499. case ELEVATOR_INSERT_REQUEUE:
  500. case ELEVATOR_INSERT_FRONT:
  501. rq->cmd_flags |= REQ_SOFTBARRIER;
  502. list_add(&rq->queuelist, &q->queue_head);
  503. break;
  504. case ELEVATOR_INSERT_BACK:
  505. rq->cmd_flags |= REQ_SOFTBARRIER;
  506. elv_drain_elevator(q);
  507. list_add_tail(&rq->queuelist, &q->queue_head);
  508. /*
  509. * We kick the queue here for the following reasons.
  510. * - The elevator might have returned NULL previously
  511. * to delay requests and returned them now. As the
  512. * queue wasn't empty before this request, ll_rw_blk
  513. * won't run the queue on return, resulting in hang.
  514. * - Usually, back inserted requests won't be merged
  515. * with anything. There's no point in delaying queue
  516. * processing.
  517. */
  518. __blk_run_queue(q);
  519. break;
  520. case ELEVATOR_INSERT_SORT_MERGE:
  521. /*
  522. * If we succeed in merging this request with one in the
  523. * queue already, we are done - rq has now been freed,
  524. * so no need to do anything further.
  525. */
  526. if (elv_attempt_insert_merge(q, rq))
  527. break;
  528. case ELEVATOR_INSERT_SORT:
  529. BUG_ON(rq->cmd_type != REQ_TYPE_FS);
  530. rq->cmd_flags |= REQ_SORTED;
  531. q->nr_sorted++;
  532. if (rq_mergeable(rq)) {
  533. elv_rqhash_add(q, rq);
  534. if (!q->last_merge)
  535. q->last_merge = rq;
  536. }
  537. /*
  538. * Some ioscheds (cfq) run q->request_fn directly, so
  539. * rq cannot be accessed after calling
  540. * elevator_add_req_fn.
  541. */
  542. q->elevator->type->ops.elevator_add_req_fn(q, rq);
  543. break;
  544. case ELEVATOR_INSERT_FLUSH:
  545. rq->cmd_flags |= REQ_SOFTBARRIER;
  546. blk_insert_flush(rq);
  547. break;
  548. default:
  549. printk(KERN_ERR "%s: bad insertion point %d\n",
  550. __func__, where);
  551. BUG();
  552. }
  553. }
  554. EXPORT_SYMBOL(__elv_add_request);
  555. void elv_add_request(struct request_queue *q, struct request *rq, int where)
  556. {
  557. unsigned long flags;
  558. spin_lock_irqsave(q->queue_lock, flags);
  559. __elv_add_request(q, rq, where);
  560. spin_unlock_irqrestore(q->queue_lock, flags);
  561. }
  562. EXPORT_SYMBOL(elv_add_request);
  563. struct request *elv_latter_request(struct request_queue *q, struct request *rq)
  564. {
  565. struct elevator_queue *e = q->elevator;
  566. if (e->type->ops.elevator_latter_req_fn)
  567. return e->type->ops.elevator_latter_req_fn(q, rq);
  568. return NULL;
  569. }
  570. struct request *elv_former_request(struct request_queue *q, struct request *rq)
  571. {
  572. struct elevator_queue *e = q->elevator;
  573. if (e->type->ops.elevator_former_req_fn)
  574. return e->type->ops.elevator_former_req_fn(q, rq);
  575. return NULL;
  576. }
  577. int elv_set_request(struct request_queue *q, struct request *rq,
  578. struct bio *bio, gfp_t gfp_mask)
  579. {
  580. struct elevator_queue *e = q->elevator;
  581. if (e->type->ops.elevator_set_req_fn)
  582. return e->type->ops.elevator_set_req_fn(q, rq, bio, gfp_mask);
  583. return 0;
  584. }
  585. void elv_put_request(struct request_queue *q, struct request *rq)
  586. {
  587. struct elevator_queue *e = q->elevator;
  588. if (e->type->ops.elevator_put_req_fn)
  589. e->type->ops.elevator_put_req_fn(rq);
  590. }
  591. int elv_may_queue(struct request_queue *q, int op, int op_flags)
  592. {
  593. struct elevator_queue *e = q->elevator;
  594. if (e->type->ops.elevator_may_queue_fn)
  595. return e->type->ops.elevator_may_queue_fn(q, op, op_flags);
  596. return ELV_MQUEUE_MAY;
  597. }
  598. void elv_completed_request(struct request_queue *q, struct request *rq)
  599. {
  600. struct elevator_queue *e = q->elevator;
  601. /*
  602. * request is released from the driver, io must be done
  603. */
  604. if (blk_account_rq(rq)) {
  605. q->in_flight[rq_is_sync(rq)]--;
  606. if ((rq->cmd_flags & REQ_SORTED) &&
  607. e->type->ops.elevator_completed_req_fn)
  608. e->type->ops.elevator_completed_req_fn(q, rq);
  609. }
  610. }
  611. #define to_elv(atr) container_of((atr), struct elv_fs_entry, attr)
  612. static ssize_t
  613. elv_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  614. {
  615. struct elv_fs_entry *entry = to_elv(attr);
  616. struct elevator_queue *e;
  617. ssize_t error;
  618. if (!entry->show)
  619. return -EIO;
  620. e = container_of(kobj, struct elevator_queue, kobj);
  621. mutex_lock(&e->sysfs_lock);
  622. error = e->type ? entry->show(e, page) : -ENOENT;
  623. mutex_unlock(&e->sysfs_lock);
  624. return error;
  625. }
  626. static ssize_t
  627. elv_attr_store(struct kobject *kobj, struct attribute *attr,
  628. const char *page, size_t length)
  629. {
  630. struct elv_fs_entry *entry = to_elv(attr);
  631. struct elevator_queue *e;
  632. ssize_t error;
  633. if (!entry->store)
  634. return -EIO;
  635. e = container_of(kobj, struct elevator_queue, kobj);
  636. mutex_lock(&e->sysfs_lock);
  637. error = e->type ? entry->store(e, page, length) : -ENOENT;
  638. mutex_unlock(&e->sysfs_lock);
  639. return error;
  640. }
  641. static const struct sysfs_ops elv_sysfs_ops = {
  642. .show = elv_attr_show,
  643. .store = elv_attr_store,
  644. };
  645. static struct kobj_type elv_ktype = {
  646. .sysfs_ops = &elv_sysfs_ops,
  647. .release = elevator_release,
  648. };
  649. int elv_register_queue(struct request_queue *q)
  650. {
  651. struct elevator_queue *e = q->elevator;
  652. int error;
  653. error = kobject_add(&e->kobj, &q->kobj, "%s", "iosched");
  654. if (!error) {
  655. struct elv_fs_entry *attr = e->type->elevator_attrs;
  656. if (attr) {
  657. while (attr->attr.name) {
  658. if (sysfs_create_file(&e->kobj, &attr->attr))
  659. break;
  660. attr++;
  661. }
  662. }
  663. kobject_uevent(&e->kobj, KOBJ_ADD);
  664. e->registered = 1;
  665. if (e->type->ops.elevator_registered_fn)
  666. e->type->ops.elevator_registered_fn(q);
  667. }
  668. return error;
  669. }
  670. EXPORT_SYMBOL(elv_register_queue);
  671. void elv_unregister_queue(struct request_queue *q)
  672. {
  673. if (q) {
  674. struct elevator_queue *e = q->elevator;
  675. kobject_uevent(&e->kobj, KOBJ_REMOVE);
  676. kobject_del(&e->kobj);
  677. e->registered = 0;
  678. }
  679. }
  680. EXPORT_SYMBOL(elv_unregister_queue);
  681. int elv_register(struct elevator_type *e)
  682. {
  683. char *def = "";
  684. /* create icq_cache if requested */
  685. if (e->icq_size) {
  686. if (WARN_ON(e->icq_size < sizeof(struct io_cq)) ||
  687. WARN_ON(e->icq_align < __alignof__(struct io_cq)))
  688. return -EINVAL;
  689. snprintf(e->icq_cache_name, sizeof(e->icq_cache_name),
  690. "%s_io_cq", e->elevator_name);
  691. e->icq_cache = kmem_cache_create(e->icq_cache_name, e->icq_size,
  692. e->icq_align, 0, NULL);
  693. if (!e->icq_cache)
  694. return -ENOMEM;
  695. }
  696. /* register, don't allow duplicate names */
  697. spin_lock(&elv_list_lock);
  698. if (elevator_find(e->elevator_name)) {
  699. spin_unlock(&elv_list_lock);
  700. if (e->icq_cache)
  701. kmem_cache_destroy(e->icq_cache);
  702. return -EBUSY;
  703. }
  704. list_add_tail(&e->list, &elv_list);
  705. spin_unlock(&elv_list_lock);
  706. /* print pretty message */
  707. if (!strcmp(e->elevator_name, chosen_elevator) ||
  708. (!*chosen_elevator &&
  709. !strcmp(e->elevator_name, CONFIG_DEFAULT_IOSCHED)))
  710. def = " (default)";
  711. printk(KERN_INFO "io scheduler %s registered%s\n", e->elevator_name,
  712. def);
  713. return 0;
  714. }
  715. EXPORT_SYMBOL_GPL(elv_register);
  716. void elv_unregister(struct elevator_type *e)
  717. {
  718. /* unregister */
  719. spin_lock(&elv_list_lock);
  720. list_del_init(&e->list);
  721. spin_unlock(&elv_list_lock);
  722. /*
  723. * Destroy icq_cache if it exists. icq's are RCU managed. Make
  724. * sure all RCU operations are complete before proceeding.
  725. */
  726. if (e->icq_cache) {
  727. rcu_barrier();
  728. kmem_cache_destroy(e->icq_cache);
  729. e->icq_cache = NULL;
  730. }
  731. }
  732. EXPORT_SYMBOL_GPL(elv_unregister);
  733. /*
  734. * switch to new_e io scheduler. be careful not to introduce deadlocks -
  735. * we don't free the old io scheduler, before we have allocated what we
  736. * need for the new one. this way we have a chance of going back to the old
  737. * one, if the new one fails init for some reason.
  738. */
  739. static int elevator_switch(struct request_queue *q, struct elevator_type *new_e)
  740. {
  741. struct elevator_queue *old = q->elevator;
  742. bool registered = old->registered;
  743. int err;
  744. /*
  745. * Turn on BYPASS and drain all requests w/ elevator private data.
  746. * Block layer doesn't call into a quiesced elevator - all requests
  747. * are directly put on the dispatch list without elevator data
  748. * using INSERT_BACK. All requests have SOFTBARRIER set and no
  749. * merge happens either.
  750. */
  751. blk_queue_bypass_start(q);
  752. /* unregister and clear all auxiliary data of the old elevator */
  753. if (registered)
  754. elv_unregister_queue(q);
  755. spin_lock_irq(q->queue_lock);
  756. ioc_clear_queue(q);
  757. spin_unlock_irq(q->queue_lock);
  758. /* allocate, init and register new elevator */
  759. err = new_e->ops.elevator_init_fn(q, new_e);
  760. if (err)
  761. goto fail_init;
  762. if (registered) {
  763. err = elv_register_queue(q);
  764. if (err)
  765. goto fail_register;
  766. }
  767. /* done, kill the old one and finish */
  768. elevator_exit(old);
  769. blk_queue_bypass_end(q);
  770. blk_add_trace_msg(q, "elv switch: %s", new_e->elevator_name);
  771. return 0;
  772. fail_register:
  773. elevator_exit(q->elevator);
  774. fail_init:
  775. /* switch failed, restore and re-register old elevator */
  776. q->elevator = old;
  777. elv_register_queue(q);
  778. blk_queue_bypass_end(q);
  779. return err;
  780. }
  781. /*
  782. * Switch this queue to the given IO scheduler.
  783. */
  784. static int __elevator_change(struct request_queue *q, const char *name)
  785. {
  786. char elevator_name[ELV_NAME_MAX];
  787. struct elevator_type *e;
  788. if (!q->elevator)
  789. return -ENXIO;
  790. strlcpy(elevator_name, name, sizeof(elevator_name));
  791. e = elevator_get(strstrip(elevator_name), true);
  792. if (!e) {
  793. printk(KERN_ERR "elevator: type %s not found\n", elevator_name);
  794. return -EINVAL;
  795. }
  796. if (!strcmp(elevator_name, q->elevator->type->elevator_name)) {
  797. elevator_put(e);
  798. return 0;
  799. }
  800. return elevator_switch(q, e);
  801. }
  802. int elevator_change(struct request_queue *q, const char *name)
  803. {
  804. int ret;
  805. /* Protect q->elevator from elevator_init() */
  806. mutex_lock(&q->sysfs_lock);
  807. ret = __elevator_change(q, name);
  808. mutex_unlock(&q->sysfs_lock);
  809. return ret;
  810. }
  811. EXPORT_SYMBOL(elevator_change);
  812. ssize_t elv_iosched_store(struct request_queue *q, const char *name,
  813. size_t count)
  814. {
  815. int ret;
  816. if (!q->elevator)
  817. return count;
  818. ret = __elevator_change(q, name);
  819. if (!ret)
  820. return count;
  821. printk(KERN_ERR "elevator: switch to %s failed\n", name);
  822. return ret;
  823. }
  824. ssize_t elv_iosched_show(struct request_queue *q, char *name)
  825. {
  826. struct elevator_queue *e = q->elevator;
  827. struct elevator_type *elv;
  828. struct elevator_type *__e;
  829. int len = 0;
  830. if (!q->elevator || !blk_queue_stackable(q))
  831. return sprintf(name, "none\n");
  832. elv = e->type;
  833. spin_lock(&elv_list_lock);
  834. list_for_each_entry(__e, &elv_list, list) {
  835. if (!strcmp(elv->elevator_name, __e->elevator_name))
  836. len += sprintf(name+len, "[%s] ", elv->elevator_name);
  837. else
  838. len += sprintf(name+len, "%s ", __e->elevator_name);
  839. }
  840. spin_unlock(&elv_list_lock);
  841. len += sprintf(len+name, "\n");
  842. return len;
  843. }
  844. struct request *elv_rb_former_request(struct request_queue *q,
  845. struct request *rq)
  846. {
  847. struct rb_node *rbprev = rb_prev(&rq->rb_node);
  848. if (rbprev)
  849. return rb_entry_rq(rbprev);
  850. return NULL;
  851. }
  852. EXPORT_SYMBOL(elv_rb_former_request);
  853. struct request *elv_rb_latter_request(struct request_queue *q,
  854. struct request *rq)
  855. {
  856. struct rb_node *rbnext = rb_next(&rq->rb_node);
  857. if (rbnext)
  858. return rb_entry_rq(rbnext);
  859. return NULL;
  860. }
  861. EXPORT_SYMBOL(elv_rb_latter_request);