elevator.c 26 KB

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