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