as-iosched.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. /*
  2. * linux/drivers/block/as-iosched.c
  3. *
  4. * Anticipatory & deadline i/o scheduler.
  5. *
  6. * Copyright (C) 2002 Jens Axboe <axboe@suse.de>
  7. * Nick Piggin <piggin@cyberone.com.au>
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/fs.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/elevator.h>
  14. #include <linux/bio.h>
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/compiler.h>
  20. #include <linux/hash.h>
  21. #include <linux/rbtree.h>
  22. #include <linux/interrupt.h>
  23. #define REQ_SYNC 1
  24. #define REQ_ASYNC 0
  25. /*
  26. * See Documentation/block/as-iosched.txt
  27. */
  28. /*
  29. * max time before a read is submitted.
  30. */
  31. #define default_read_expire (HZ / 8)
  32. /*
  33. * ditto for writes, these limits are not hard, even
  34. * if the disk is capable of satisfying them.
  35. */
  36. #define default_write_expire (HZ / 4)
  37. /*
  38. * read_batch_expire describes how long we will allow a stream of reads to
  39. * persist before looking to see whether it is time to switch over to writes.
  40. */
  41. #define default_read_batch_expire (HZ / 2)
  42. /*
  43. * write_batch_expire describes how long we want a stream of writes to run for.
  44. * This is not a hard limit, but a target we set for the auto-tuning thingy.
  45. * See, the problem is: we can send a lot of writes to disk cache / TCQ in
  46. * a short amount of time...
  47. */
  48. #define default_write_batch_expire (HZ / 8)
  49. /*
  50. * max time we may wait to anticipate a read (default around 6ms)
  51. */
  52. #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1)
  53. /*
  54. * Keep track of up to 20ms thinktimes. We can go as big as we like here,
  55. * however huge values tend to interfere and not decay fast enough. A program
  56. * might be in a non-io phase of operation. Waiting on user input for example,
  57. * or doing a lengthy computation. A small penalty can be justified there, and
  58. * will still catch out those processes that constantly have large thinktimes.
  59. */
  60. #define MAX_THINKTIME (HZ/50UL)
  61. /* Bits in as_io_context.state */
  62. enum as_io_states {
  63. AS_TASK_RUNNING=0, /* Process has not exitted */
  64. AS_TASK_IOSTARTED, /* Process has started some IO */
  65. AS_TASK_IORUNNING, /* Process has completed some IO */
  66. };
  67. enum anticipation_status {
  68. ANTIC_OFF=0, /* Not anticipating (normal operation) */
  69. ANTIC_WAIT_REQ, /* The last read has not yet completed */
  70. ANTIC_WAIT_NEXT, /* Currently anticipating a request vs
  71. last read (which has completed) */
  72. ANTIC_FINISHED, /* Anticipating but have found a candidate
  73. * or timed out */
  74. };
  75. struct as_data {
  76. /*
  77. * run time data
  78. */
  79. struct request_queue *q; /* the "owner" queue */
  80. /*
  81. * requests (as_rq s) are present on both sort_list and fifo_list
  82. */
  83. struct rb_root sort_list[2];
  84. struct list_head fifo_list[2];
  85. struct as_rq *next_arq[2]; /* next in sort order */
  86. sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */
  87. struct list_head *hash; /* request hash */
  88. unsigned long exit_prob; /* probability a task will exit while
  89. being waited on */
  90. unsigned long new_ttime_total; /* mean thinktime on new proc */
  91. unsigned long new_ttime_mean;
  92. u64 new_seek_total; /* mean seek on new proc */
  93. sector_t new_seek_mean;
  94. unsigned long current_batch_expires;
  95. unsigned long last_check_fifo[2];
  96. int changed_batch; /* 1: waiting for old batch to end */
  97. int new_batch; /* 1: waiting on first read complete */
  98. int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */
  99. int write_batch_count; /* max # of reqs in a write batch */
  100. int current_write_count; /* how many requests left this batch */
  101. int write_batch_idled; /* has the write batch gone idle? */
  102. mempool_t *arq_pool;
  103. enum anticipation_status antic_status;
  104. unsigned long antic_start; /* jiffies: when it started */
  105. struct timer_list antic_timer; /* anticipatory scheduling timer */
  106. struct work_struct antic_work; /* Deferred unplugging */
  107. struct io_context *io_context; /* Identify the expected process */
  108. int ioc_finished; /* IO associated with io_context is finished */
  109. int nr_dispatched;
  110. /*
  111. * settings that change how the i/o scheduler behaves
  112. */
  113. unsigned long fifo_expire[2];
  114. unsigned long batch_expire[2];
  115. unsigned long antic_expire;
  116. };
  117. #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo)
  118. /*
  119. * per-request data.
  120. */
  121. enum arq_state {
  122. AS_RQ_NEW=0, /* New - not referenced and not on any lists */
  123. AS_RQ_QUEUED, /* In the request queue. It belongs to the
  124. scheduler */
  125. AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the
  126. driver now */
  127. AS_RQ_PRESCHED, /* Debug poisoning for requests being used */
  128. AS_RQ_REMOVED,
  129. AS_RQ_MERGED,
  130. AS_RQ_POSTSCHED, /* when they shouldn't be */
  131. };
  132. struct as_rq {
  133. /*
  134. * rbtree index, key is the starting offset
  135. */
  136. struct rb_node rb_node;
  137. sector_t rb_key;
  138. struct request *request;
  139. struct io_context *io_context; /* The submitting task */
  140. /*
  141. * request hash, key is the ending offset (for back merge lookup)
  142. */
  143. struct list_head hash;
  144. unsigned int on_hash;
  145. /*
  146. * expire fifo
  147. */
  148. struct list_head fifo;
  149. unsigned long expires;
  150. unsigned int is_sync;
  151. enum arq_state state;
  152. };
  153. #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
  154. static kmem_cache_t *arq_pool;
  155. /*
  156. * IO Context helper functions
  157. */
  158. /* Called to deallocate the as_io_context */
  159. static void free_as_io_context(struct as_io_context *aic)
  160. {
  161. kfree(aic);
  162. }
  163. /* Called when the task exits */
  164. static void exit_as_io_context(struct as_io_context *aic)
  165. {
  166. WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
  167. clear_bit(AS_TASK_RUNNING, &aic->state);
  168. }
  169. static struct as_io_context *alloc_as_io_context(void)
  170. {
  171. struct as_io_context *ret;
  172. ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
  173. if (ret) {
  174. ret->dtor = free_as_io_context;
  175. ret->exit = exit_as_io_context;
  176. ret->state = 1 << AS_TASK_RUNNING;
  177. atomic_set(&ret->nr_queued, 0);
  178. atomic_set(&ret->nr_dispatched, 0);
  179. spin_lock_init(&ret->lock);
  180. ret->ttime_total = 0;
  181. ret->ttime_samples = 0;
  182. ret->ttime_mean = 0;
  183. ret->seek_total = 0;
  184. ret->seek_samples = 0;
  185. ret->seek_mean = 0;
  186. }
  187. return ret;
  188. }
  189. /*
  190. * If the current task has no AS IO context then create one and initialise it.
  191. * Then take a ref on the task's io context and return it.
  192. */
  193. static struct io_context *as_get_io_context(void)
  194. {
  195. struct io_context *ioc = get_io_context(GFP_ATOMIC);
  196. if (ioc && !ioc->aic) {
  197. ioc->aic = alloc_as_io_context();
  198. if (!ioc->aic) {
  199. put_io_context(ioc);
  200. ioc = NULL;
  201. }
  202. }
  203. return ioc;
  204. }
  205. static void as_put_io_context(struct as_rq *arq)
  206. {
  207. struct as_io_context *aic;
  208. if (unlikely(!arq->io_context))
  209. return;
  210. aic = arq->io_context->aic;
  211. if (arq->is_sync == REQ_SYNC && aic) {
  212. spin_lock(&aic->lock);
  213. set_bit(AS_TASK_IORUNNING, &aic->state);
  214. aic->last_end_request = jiffies;
  215. spin_unlock(&aic->lock);
  216. }
  217. put_io_context(arq->io_context);
  218. }
  219. /*
  220. * the back merge hash support functions
  221. */
  222. static const int as_hash_shift = 6;
  223. #define AS_HASH_BLOCK(sec) ((sec) >> 3)
  224. #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
  225. #define AS_HASH_ENTRIES (1 << as_hash_shift)
  226. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  227. #define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash)
  228. static inline void __as_del_arq_hash(struct as_rq *arq)
  229. {
  230. arq->on_hash = 0;
  231. list_del_init(&arq->hash);
  232. }
  233. static inline void as_del_arq_hash(struct as_rq *arq)
  234. {
  235. if (arq->on_hash)
  236. __as_del_arq_hash(arq);
  237. }
  238. static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
  239. {
  240. struct request *rq = arq->request;
  241. BUG_ON(arq->on_hash);
  242. arq->on_hash = 1;
  243. list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
  244. }
  245. /*
  246. * move hot entry to front of chain
  247. */
  248. static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
  249. {
  250. struct request *rq = arq->request;
  251. struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
  252. if (!arq->on_hash) {
  253. WARN_ON(1);
  254. return;
  255. }
  256. if (arq->hash.prev != head) {
  257. list_del(&arq->hash);
  258. list_add(&arq->hash, head);
  259. }
  260. }
  261. static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
  262. {
  263. struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
  264. struct list_head *entry, *next = hash_list->next;
  265. while ((entry = next) != hash_list) {
  266. struct as_rq *arq = list_entry_hash(entry);
  267. struct request *__rq = arq->request;
  268. next = entry->next;
  269. BUG_ON(!arq->on_hash);
  270. if (!rq_mergeable(__rq)) {
  271. as_del_arq_hash(arq);
  272. continue;
  273. }
  274. if (rq_hash_key(__rq) == offset)
  275. return __rq;
  276. }
  277. return NULL;
  278. }
  279. /*
  280. * rb tree support functions
  281. */
  282. #define RB_NONE (2)
  283. #define RB_EMPTY(root) ((root)->rb_node == NULL)
  284. #define ON_RB(node) ((node)->rb_color != RB_NONE)
  285. #define RB_CLEAR(node) ((node)->rb_color = RB_NONE)
  286. #define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node)
  287. #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync])
  288. #define rq_rb_key(rq) (rq)->sector
  289. /*
  290. * as_find_first_arq finds the first (lowest sector numbered) request
  291. * for the specified data_dir. Used to sweep back to the start of the disk
  292. * (1-way elevator) after we process the last (highest sector) request.
  293. */
  294. static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir)
  295. {
  296. struct rb_node *n = ad->sort_list[data_dir].rb_node;
  297. if (n == NULL)
  298. return NULL;
  299. for (;;) {
  300. if (n->rb_left == NULL)
  301. return rb_entry_arq(n);
  302. n = n->rb_left;
  303. }
  304. }
  305. /*
  306. * Add the request to the rb tree if it is unique. If there is an alias (an
  307. * existing request against the same sector), which can happen when using
  308. * direct IO, then return the alias.
  309. */
  310. static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq)
  311. {
  312. struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node;
  313. struct rb_node *parent = NULL;
  314. struct as_rq *__arq;
  315. struct request *rq = arq->request;
  316. arq->rb_key = rq_rb_key(rq);
  317. while (*p) {
  318. parent = *p;
  319. __arq = rb_entry_arq(parent);
  320. if (arq->rb_key < __arq->rb_key)
  321. p = &(*p)->rb_left;
  322. else if (arq->rb_key > __arq->rb_key)
  323. p = &(*p)->rb_right;
  324. else
  325. return __arq;
  326. }
  327. rb_link_node(&arq->rb_node, parent, p);
  328. rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
  329. return NULL;
  330. }
  331. static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq)
  332. {
  333. if (!ON_RB(&arq->rb_node)) {
  334. WARN_ON(1);
  335. return;
  336. }
  337. rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq));
  338. RB_CLEAR(&arq->rb_node);
  339. }
  340. static struct request *
  341. as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir)
  342. {
  343. struct rb_node *n = ad->sort_list[data_dir].rb_node;
  344. struct as_rq *arq;
  345. while (n) {
  346. arq = rb_entry_arq(n);
  347. if (sector < arq->rb_key)
  348. n = n->rb_left;
  349. else if (sector > arq->rb_key)
  350. n = n->rb_right;
  351. else
  352. return arq->request;
  353. }
  354. return NULL;
  355. }
  356. /*
  357. * IO Scheduler proper
  358. */
  359. #define MAXBACK (1024 * 1024) /*
  360. * Maximum distance the disk will go backward
  361. * for a request.
  362. */
  363. #define BACK_PENALTY 2
  364. /*
  365. * as_choose_req selects the preferred one of two requests of the same data_dir
  366. * ignoring time - eg. timeouts, which is the job of as_dispatch_request
  367. */
  368. static struct as_rq *
  369. as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2)
  370. {
  371. int data_dir;
  372. sector_t last, s1, s2, d1, d2;
  373. int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */
  374. const sector_t maxback = MAXBACK;
  375. if (arq1 == NULL || arq1 == arq2)
  376. return arq2;
  377. if (arq2 == NULL)
  378. return arq1;
  379. data_dir = arq1->is_sync;
  380. last = ad->last_sector[data_dir];
  381. s1 = arq1->request->sector;
  382. s2 = arq2->request->sector;
  383. BUG_ON(data_dir != arq2->is_sync);
  384. /*
  385. * Strict one way elevator _except_ in the case where we allow
  386. * short backward seeks which are biased as twice the cost of a
  387. * similar forward seek.
  388. */
  389. if (s1 >= last)
  390. d1 = s1 - last;
  391. else if (s1+maxback >= last)
  392. d1 = (last - s1)*BACK_PENALTY;
  393. else {
  394. r1_wrap = 1;
  395. d1 = 0; /* shut up, gcc */
  396. }
  397. if (s2 >= last)
  398. d2 = s2 - last;
  399. else if (s2+maxback >= last)
  400. d2 = (last - s2)*BACK_PENALTY;
  401. else {
  402. r2_wrap = 1;
  403. d2 = 0;
  404. }
  405. /* Found required data */
  406. if (!r1_wrap && r2_wrap)
  407. return arq1;
  408. else if (!r2_wrap && r1_wrap)
  409. return arq2;
  410. else if (r1_wrap && r2_wrap) {
  411. /* both behind the head */
  412. if (s1 <= s2)
  413. return arq1;
  414. else
  415. return arq2;
  416. }
  417. /* Both requests in front of the head */
  418. if (d1 < d2)
  419. return arq1;
  420. else if (d2 < d1)
  421. return arq2;
  422. else {
  423. if (s1 >= s2)
  424. return arq1;
  425. else
  426. return arq2;
  427. }
  428. }
  429. /*
  430. * as_find_next_arq finds the next request after @prev in elevator order.
  431. * this with as_choose_req form the basis for how the scheduler chooses
  432. * what request to process next. Anticipation works on top of this.
  433. */
  434. static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last)
  435. {
  436. const int data_dir = last->is_sync;
  437. struct as_rq *ret;
  438. struct rb_node *rbnext = rb_next(&last->rb_node);
  439. struct rb_node *rbprev = rb_prev(&last->rb_node);
  440. struct as_rq *arq_next, *arq_prev;
  441. BUG_ON(!ON_RB(&last->rb_node));
  442. if (rbprev)
  443. arq_prev = rb_entry_arq(rbprev);
  444. else
  445. arq_prev = NULL;
  446. if (rbnext)
  447. arq_next = rb_entry_arq(rbnext);
  448. else {
  449. arq_next = as_find_first_arq(ad, data_dir);
  450. if (arq_next == last)
  451. arq_next = NULL;
  452. }
  453. ret = as_choose_req(ad, arq_next, arq_prev);
  454. return ret;
  455. }
  456. /*
  457. * anticipatory scheduling functions follow
  458. */
  459. /*
  460. * as_antic_expired tells us when we have anticipated too long.
  461. * The funny "absolute difference" math on the elapsed time is to handle
  462. * jiffy wraps, and disks which have been idle for 0x80000000 jiffies.
  463. */
  464. static int as_antic_expired(struct as_data *ad)
  465. {
  466. long delta_jif;
  467. delta_jif = jiffies - ad->antic_start;
  468. if (unlikely(delta_jif < 0))
  469. delta_jif = -delta_jif;
  470. if (delta_jif < ad->antic_expire)
  471. return 0;
  472. return 1;
  473. }
  474. /*
  475. * as_antic_waitnext starts anticipating that a nice request will soon be
  476. * submitted. See also as_antic_waitreq
  477. */
  478. static void as_antic_waitnext(struct as_data *ad)
  479. {
  480. unsigned long timeout;
  481. BUG_ON(ad->antic_status != ANTIC_OFF
  482. && ad->antic_status != ANTIC_WAIT_REQ);
  483. timeout = ad->antic_start + ad->antic_expire;
  484. mod_timer(&ad->antic_timer, timeout);
  485. ad->antic_status = ANTIC_WAIT_NEXT;
  486. }
  487. /*
  488. * as_antic_waitreq starts anticipating. We don't start timing the anticipation
  489. * until the request that we're anticipating on has finished. This means we
  490. * are timing from when the candidate process wakes up hopefully.
  491. */
  492. static void as_antic_waitreq(struct as_data *ad)
  493. {
  494. BUG_ON(ad->antic_status == ANTIC_FINISHED);
  495. if (ad->antic_status == ANTIC_OFF) {
  496. if (!ad->io_context || ad->ioc_finished)
  497. as_antic_waitnext(ad);
  498. else
  499. ad->antic_status = ANTIC_WAIT_REQ;
  500. }
  501. }
  502. /*
  503. * This is called directly by the functions in this file to stop anticipation.
  504. * We kill the timer and schedule a call to the request_fn asap.
  505. */
  506. static void as_antic_stop(struct as_data *ad)
  507. {
  508. int status = ad->antic_status;
  509. if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) {
  510. if (status == ANTIC_WAIT_NEXT)
  511. del_timer(&ad->antic_timer);
  512. ad->antic_status = ANTIC_FINISHED;
  513. /* see as_work_handler */
  514. kblockd_schedule_work(&ad->antic_work);
  515. }
  516. }
  517. /*
  518. * as_antic_timeout is the timer function set by as_antic_waitnext.
  519. */
  520. static void as_antic_timeout(unsigned long data)
  521. {
  522. struct request_queue *q = (struct request_queue *)data;
  523. struct as_data *ad = q->elevator->elevator_data;
  524. unsigned long flags;
  525. spin_lock_irqsave(q->queue_lock, flags);
  526. if (ad->antic_status == ANTIC_WAIT_REQ
  527. || ad->antic_status == ANTIC_WAIT_NEXT) {
  528. struct as_io_context *aic = ad->io_context->aic;
  529. ad->antic_status = ANTIC_FINISHED;
  530. kblockd_schedule_work(&ad->antic_work);
  531. if (aic->ttime_samples == 0) {
  532. /* process anticipated on has exitted or timed out*/
  533. ad->exit_prob = (7*ad->exit_prob + 256)/8;
  534. }
  535. }
  536. spin_unlock_irqrestore(q->queue_lock, flags);
  537. }
  538. /*
  539. * as_close_req decides if one request is considered "close" to the
  540. * previous one issued.
  541. */
  542. static int as_close_req(struct as_data *ad, struct as_rq *arq)
  543. {
  544. unsigned long delay; /* milliseconds */
  545. sector_t last = ad->last_sector[ad->batch_data_dir];
  546. sector_t next = arq->request->sector;
  547. sector_t delta; /* acceptable close offset (in sectors) */
  548. if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished)
  549. delay = 0;
  550. else
  551. delay = ((jiffies - ad->antic_start) * 1000) / HZ;
  552. if (delay <= 1)
  553. delta = 64;
  554. else if (delay <= 20 && delay <= ad->antic_expire)
  555. delta = 64 << (delay-1);
  556. else
  557. return 1;
  558. return (last - (delta>>1) <= next) && (next <= last + delta);
  559. }
  560. /*
  561. * as_can_break_anticipation returns true if we have been anticipating this
  562. * request.
  563. *
  564. * It also returns true if the process against which we are anticipating
  565. * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to
  566. * dispatch it ASAP, because we know that application will not be submitting
  567. * any new reads.
  568. *
  569. * If the task which has submitted the request has exitted, break anticipation.
  570. *
  571. * If this task has queued some other IO, do not enter enticipation.
  572. */
  573. static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq)
  574. {
  575. struct io_context *ioc;
  576. struct as_io_context *aic;
  577. sector_t s;
  578. ioc = ad->io_context;
  579. BUG_ON(!ioc);
  580. if (arq && ioc == arq->io_context) {
  581. /* request from same process */
  582. return 1;
  583. }
  584. if (ad->ioc_finished && as_antic_expired(ad)) {
  585. /*
  586. * In this situation status should really be FINISHED,
  587. * however the timer hasn't had the chance to run yet.
  588. */
  589. return 1;
  590. }
  591. aic = ioc->aic;
  592. if (!aic)
  593. return 0;
  594. if (!test_bit(AS_TASK_RUNNING, &aic->state)) {
  595. /* process anticipated on has exitted */
  596. if (aic->ttime_samples == 0)
  597. ad->exit_prob = (7*ad->exit_prob + 256)/8;
  598. return 1;
  599. }
  600. if (atomic_read(&aic->nr_queued) > 0) {
  601. /* process has more requests queued */
  602. return 1;
  603. }
  604. if (atomic_read(&aic->nr_dispatched) > 0) {
  605. /* process has more requests dispatched */
  606. return 1;
  607. }
  608. if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, arq)) {
  609. /*
  610. * Found a close request that is not one of ours.
  611. *
  612. * This makes close requests from another process reset
  613. * our thinktime delay. Is generally useful when there are
  614. * two or more cooperating processes working in the same
  615. * area.
  616. */
  617. spin_lock(&aic->lock);
  618. aic->last_end_request = jiffies;
  619. spin_unlock(&aic->lock);
  620. return 1;
  621. }
  622. if (aic->ttime_samples == 0) {
  623. if (ad->new_ttime_mean > ad->antic_expire)
  624. return 1;
  625. if (ad->exit_prob > 128)
  626. return 1;
  627. } else if (aic->ttime_mean > ad->antic_expire) {
  628. /* the process thinks too much between requests */
  629. return 1;
  630. }
  631. if (!arq)
  632. return 0;
  633. if (ad->last_sector[REQ_SYNC] < arq->request->sector)
  634. s = arq->request->sector - ad->last_sector[REQ_SYNC];
  635. else
  636. s = ad->last_sector[REQ_SYNC] - arq->request->sector;
  637. if (aic->seek_samples == 0) {
  638. /*
  639. * Process has just started IO. Use past statistics to
  640. * guage success possibility
  641. */
  642. if (ad->new_seek_mean > s) {
  643. /* this request is better than what we're expecting */
  644. return 1;
  645. }
  646. } else {
  647. if (aic->seek_mean > s) {
  648. /* this request is better than what we're expecting */
  649. return 1;
  650. }
  651. }
  652. return 0;
  653. }
  654. /*
  655. * as_can_anticipate indicates weather we should either run arq
  656. * or keep anticipating a better request.
  657. */
  658. static int as_can_anticipate(struct as_data *ad, struct as_rq *arq)
  659. {
  660. if (!ad->io_context)
  661. /*
  662. * Last request submitted was a write
  663. */
  664. return 0;
  665. if (ad->antic_status == ANTIC_FINISHED)
  666. /*
  667. * Don't restart if we have just finished. Run the next request
  668. */
  669. return 0;
  670. if (as_can_break_anticipation(ad, arq))
  671. /*
  672. * This request is a good candidate. Don't keep anticipating,
  673. * run it.
  674. */
  675. return 0;
  676. /*
  677. * OK from here, we haven't finished, and don't have a decent request!
  678. * Status is either ANTIC_OFF so start waiting,
  679. * ANTIC_WAIT_REQ so continue waiting for request to finish
  680. * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request.
  681. *
  682. */
  683. return 1;
  684. }
  685. static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, unsigned long ttime)
  686. {
  687. /* fixed point: 1.0 == 1<<8 */
  688. if (aic->ttime_samples == 0) {
  689. ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8;
  690. ad->new_ttime_mean = ad->new_ttime_total / 256;
  691. ad->exit_prob = (7*ad->exit_prob)/8;
  692. }
  693. aic->ttime_samples = (7*aic->ttime_samples + 256) / 8;
  694. aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8;
  695. aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples;
  696. }
  697. static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, sector_t sdist)
  698. {
  699. u64 total;
  700. if (aic->seek_samples == 0) {
  701. ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8;
  702. ad->new_seek_mean = ad->new_seek_total / 256;
  703. }
  704. /*
  705. * Don't allow the seek distance to get too large from the
  706. * odd fragment, pagein, etc
  707. */
  708. if (aic->seek_samples <= 60) /* second&third seek */
  709. sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024);
  710. else
  711. sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64);
  712. aic->seek_samples = (7*aic->seek_samples + 256) / 8;
  713. aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8;
  714. total = aic->seek_total + (aic->seek_samples/2);
  715. do_div(total, aic->seek_samples);
  716. aic->seek_mean = (sector_t)total;
  717. }
  718. /*
  719. * as_update_iohist keeps a decaying histogram of IO thinktimes, and
  720. * updates @aic->ttime_mean based on that. It is called when a new
  721. * request is queued.
  722. */
  723. static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, struct request *rq)
  724. {
  725. struct as_rq *arq = RQ_DATA(rq);
  726. int data_dir = arq->is_sync;
  727. unsigned long thinktime;
  728. sector_t seek_dist;
  729. if (aic == NULL)
  730. return;
  731. if (data_dir == REQ_SYNC) {
  732. unsigned long in_flight = atomic_read(&aic->nr_queued)
  733. + atomic_read(&aic->nr_dispatched);
  734. spin_lock(&aic->lock);
  735. if (test_bit(AS_TASK_IORUNNING, &aic->state) ||
  736. test_bit(AS_TASK_IOSTARTED, &aic->state)) {
  737. /* Calculate read -> read thinktime */
  738. if (test_bit(AS_TASK_IORUNNING, &aic->state)
  739. && in_flight == 0) {
  740. thinktime = jiffies - aic->last_end_request;
  741. thinktime = min(thinktime, MAX_THINKTIME-1);
  742. } else
  743. thinktime = 0;
  744. as_update_thinktime(ad, aic, thinktime);
  745. /* Calculate read -> read seek distance */
  746. if (aic->last_request_pos < rq->sector)
  747. seek_dist = rq->sector - aic->last_request_pos;
  748. else
  749. seek_dist = aic->last_request_pos - rq->sector;
  750. as_update_seekdist(ad, aic, seek_dist);
  751. }
  752. aic->last_request_pos = rq->sector + rq->nr_sectors;
  753. set_bit(AS_TASK_IOSTARTED, &aic->state);
  754. spin_unlock(&aic->lock);
  755. }
  756. }
  757. /*
  758. * as_update_arq must be called whenever a request (arq) is added to
  759. * the sort_list. This function keeps caches up to date, and checks if the
  760. * request might be one we are "anticipating"
  761. */
  762. static void as_update_arq(struct as_data *ad, struct as_rq *arq)
  763. {
  764. const int data_dir = arq->is_sync;
  765. /* keep the next_arq cache up to date */
  766. ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]);
  767. /*
  768. * have we been anticipating this request?
  769. * or does it come from the same process as the one we are anticipating
  770. * for?
  771. */
  772. if (ad->antic_status == ANTIC_WAIT_REQ
  773. || ad->antic_status == ANTIC_WAIT_NEXT) {
  774. if (as_can_break_anticipation(ad, arq))
  775. as_antic_stop(ad);
  776. }
  777. }
  778. /*
  779. * Gathers timings and resizes the write batch automatically
  780. */
  781. static void update_write_batch(struct as_data *ad)
  782. {
  783. unsigned long batch = ad->batch_expire[REQ_ASYNC];
  784. long write_time;
  785. write_time = (jiffies - ad->current_batch_expires) + batch;
  786. if (write_time < 0)
  787. write_time = 0;
  788. if (write_time > batch && !ad->write_batch_idled) {
  789. if (write_time > batch * 3)
  790. ad->write_batch_count /= 2;
  791. else
  792. ad->write_batch_count--;
  793. } else if (write_time < batch && ad->current_write_count == 0) {
  794. if (batch > write_time * 3)
  795. ad->write_batch_count *= 2;
  796. else
  797. ad->write_batch_count++;
  798. }
  799. if (ad->write_batch_count < 1)
  800. ad->write_batch_count = 1;
  801. }
  802. /*
  803. * as_completed_request is to be called when a request has completed and
  804. * returned something to the requesting process, be it an error or data.
  805. */
  806. static void as_completed_request(request_queue_t *q, struct request *rq)
  807. {
  808. struct as_data *ad = q->elevator->elevator_data;
  809. struct as_rq *arq = RQ_DATA(rq);
  810. WARN_ON(!list_empty(&rq->queuelist));
  811. if (arq->state != AS_RQ_REMOVED) {
  812. printk("arq->state %d\n", arq->state);
  813. WARN_ON(1);
  814. goto out;
  815. }
  816. if (ad->changed_batch && ad->nr_dispatched == 1) {
  817. kblockd_schedule_work(&ad->antic_work);
  818. ad->changed_batch = 0;
  819. if (ad->batch_data_dir == REQ_SYNC)
  820. ad->new_batch = 1;
  821. }
  822. WARN_ON(ad->nr_dispatched == 0);
  823. ad->nr_dispatched--;
  824. /*
  825. * Start counting the batch from when a request of that direction is
  826. * actually serviced. This should help devices with big TCQ windows
  827. * and writeback caches
  828. */
  829. if (ad->new_batch && ad->batch_data_dir == arq->is_sync) {
  830. update_write_batch(ad);
  831. ad->current_batch_expires = jiffies +
  832. ad->batch_expire[REQ_SYNC];
  833. ad->new_batch = 0;
  834. }
  835. if (ad->io_context == arq->io_context && ad->io_context) {
  836. ad->antic_start = jiffies;
  837. ad->ioc_finished = 1;
  838. if (ad->antic_status == ANTIC_WAIT_REQ) {
  839. /*
  840. * We were waiting on this request, now anticipate
  841. * the next one
  842. */
  843. as_antic_waitnext(ad);
  844. }
  845. }
  846. as_put_io_context(arq);
  847. out:
  848. arq->state = AS_RQ_POSTSCHED;
  849. }
  850. /*
  851. * as_remove_queued_request removes a request from the pre dispatch queue
  852. * without updating refcounts. It is expected the caller will drop the
  853. * reference unless it replaces the request at somepart of the elevator
  854. * (ie. the dispatch queue)
  855. */
  856. static void as_remove_queued_request(request_queue_t *q, struct request *rq)
  857. {
  858. struct as_rq *arq = RQ_DATA(rq);
  859. const int data_dir = arq->is_sync;
  860. struct as_data *ad = q->elevator->elevator_data;
  861. WARN_ON(arq->state != AS_RQ_QUEUED);
  862. if (arq->io_context && arq->io_context->aic) {
  863. BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued));
  864. atomic_dec(&arq->io_context->aic->nr_queued);
  865. }
  866. /*
  867. * Update the "next_arq" cache if we are about to remove its
  868. * entry
  869. */
  870. if (ad->next_arq[data_dir] == arq)
  871. ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
  872. list_del_init(&arq->fifo);
  873. as_del_arq_hash(arq);
  874. as_del_arq_rb(ad, arq);
  875. }
  876. /*
  877. * as_fifo_expired returns 0 if there are no expired reads on the fifo,
  878. * 1 otherwise. It is ratelimited so that we only perform the check once per
  879. * `fifo_expire' interval. Otherwise a large number of expired requests
  880. * would create a hopeless seekstorm.
  881. *
  882. * See as_antic_expired comment.
  883. */
  884. static int as_fifo_expired(struct as_data *ad, int adir)
  885. {
  886. struct as_rq *arq;
  887. long delta_jif;
  888. delta_jif = jiffies - ad->last_check_fifo[adir];
  889. if (unlikely(delta_jif < 0))
  890. delta_jif = -delta_jif;
  891. if (delta_jif < ad->fifo_expire[adir])
  892. return 0;
  893. ad->last_check_fifo[adir] = jiffies;
  894. if (list_empty(&ad->fifo_list[adir]))
  895. return 0;
  896. arq = list_entry_fifo(ad->fifo_list[adir].next);
  897. return time_after(jiffies, arq->expires);
  898. }
  899. /*
  900. * as_batch_expired returns true if the current batch has expired. A batch
  901. * is a set of reads or a set of writes.
  902. */
  903. static inline int as_batch_expired(struct as_data *ad)
  904. {
  905. if (ad->changed_batch || ad->new_batch)
  906. return 0;
  907. if (ad->batch_data_dir == REQ_SYNC)
  908. /* TODO! add a check so a complete fifo gets written? */
  909. return time_after(jiffies, ad->current_batch_expires);
  910. return time_after(jiffies, ad->current_batch_expires)
  911. || ad->current_write_count == 0;
  912. }
  913. /*
  914. * move an entry to dispatch queue
  915. */
  916. static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq)
  917. {
  918. struct request *rq = arq->request;
  919. const int data_dir = arq->is_sync;
  920. BUG_ON(!ON_RB(&arq->rb_node));
  921. as_antic_stop(ad);
  922. ad->antic_status = ANTIC_OFF;
  923. /*
  924. * This has to be set in order to be correctly updated by
  925. * as_find_next_arq
  926. */
  927. ad->last_sector[data_dir] = rq->sector + rq->nr_sectors;
  928. if (data_dir == REQ_SYNC) {
  929. /* In case we have to anticipate after this */
  930. copy_io_context(&ad->io_context, &arq->io_context);
  931. } else {
  932. if (ad->io_context) {
  933. put_io_context(ad->io_context);
  934. ad->io_context = NULL;
  935. }
  936. if (ad->current_write_count != 0)
  937. ad->current_write_count--;
  938. }
  939. ad->ioc_finished = 0;
  940. ad->next_arq[data_dir] = as_find_next_arq(ad, arq);
  941. /*
  942. * take it off the sort and fifo list, add to dispatch queue
  943. */
  944. while (!list_empty(&rq->queuelist)) {
  945. struct request *__rq = list_entry_rq(rq->queuelist.next);
  946. struct as_rq *__arq = RQ_DATA(__rq);
  947. list_del(&__rq->queuelist);
  948. elv_dispatch_add_tail(ad->q, __rq);
  949. if (__arq->io_context && __arq->io_context->aic)
  950. atomic_inc(&__arq->io_context->aic->nr_dispatched);
  951. WARN_ON(__arq->state != AS_RQ_QUEUED);
  952. __arq->state = AS_RQ_DISPATCHED;
  953. ad->nr_dispatched++;
  954. }
  955. as_remove_queued_request(ad->q, rq);
  956. WARN_ON(arq->state != AS_RQ_QUEUED);
  957. elv_dispatch_sort(ad->q, rq);
  958. arq->state = AS_RQ_DISPATCHED;
  959. if (arq->io_context && arq->io_context->aic)
  960. atomic_inc(&arq->io_context->aic->nr_dispatched);
  961. ad->nr_dispatched++;
  962. }
  963. /*
  964. * as_dispatch_request selects the best request according to
  965. * read/write expire, batch expire, etc, and moves it to the dispatch
  966. * queue. Returns 1 if a request was found, 0 otherwise.
  967. */
  968. static int as_dispatch_request(request_queue_t *q, int force)
  969. {
  970. struct as_data *ad = q->elevator->elevator_data;
  971. struct as_rq *arq;
  972. const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]);
  973. const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]);
  974. if (unlikely(force)) {
  975. /*
  976. * Forced dispatch, accounting is useless. Reset
  977. * accounting states and dump fifo_lists. Note that
  978. * batch_data_dir is reset to REQ_SYNC to avoid
  979. * screwing write batch accounting as write batch
  980. * accounting occurs on W->R transition.
  981. */
  982. int dispatched = 0;
  983. ad->batch_data_dir = REQ_SYNC;
  984. ad->changed_batch = 0;
  985. ad->new_batch = 0;
  986. while (ad->next_arq[REQ_SYNC]) {
  987. as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]);
  988. dispatched++;
  989. }
  990. ad->last_check_fifo[REQ_SYNC] = jiffies;
  991. while (ad->next_arq[REQ_ASYNC]) {
  992. as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]);
  993. dispatched++;
  994. }
  995. ad->last_check_fifo[REQ_ASYNC] = jiffies;
  996. return dispatched;
  997. }
  998. /* Signal that the write batch was uncontended, so we can't time it */
  999. if (ad->batch_data_dir == REQ_ASYNC && !reads) {
  1000. if (ad->current_write_count == 0 || !writes)
  1001. ad->write_batch_idled = 1;
  1002. }
  1003. if (!(reads || writes)
  1004. || ad->antic_status == ANTIC_WAIT_REQ
  1005. || ad->antic_status == ANTIC_WAIT_NEXT
  1006. || ad->changed_batch)
  1007. return 0;
  1008. if (!(reads && writes && as_batch_expired(ad)) ) {
  1009. /*
  1010. * batch is still running or no reads or no writes
  1011. */
  1012. arq = ad->next_arq[ad->batch_data_dir];
  1013. if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) {
  1014. if (as_fifo_expired(ad, REQ_SYNC))
  1015. goto fifo_expired;
  1016. if (as_can_anticipate(ad, arq)) {
  1017. as_antic_waitreq(ad);
  1018. return 0;
  1019. }
  1020. }
  1021. if (arq) {
  1022. /* we have a "next request" */
  1023. if (reads && !writes)
  1024. ad->current_batch_expires =
  1025. jiffies + ad->batch_expire[REQ_SYNC];
  1026. goto dispatch_request;
  1027. }
  1028. }
  1029. /*
  1030. * at this point we are not running a batch. select the appropriate
  1031. * data direction (read / write)
  1032. */
  1033. if (reads) {
  1034. BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC]));
  1035. if (writes && ad->batch_data_dir == REQ_SYNC)
  1036. /*
  1037. * Last batch was a read, switch to writes
  1038. */
  1039. goto dispatch_writes;
  1040. if (ad->batch_data_dir == REQ_ASYNC) {
  1041. WARN_ON(ad->new_batch);
  1042. ad->changed_batch = 1;
  1043. }
  1044. ad->batch_data_dir = REQ_SYNC;
  1045. arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
  1046. ad->last_check_fifo[ad->batch_data_dir] = jiffies;
  1047. goto dispatch_request;
  1048. }
  1049. /*
  1050. * the last batch was a read
  1051. */
  1052. if (writes) {
  1053. dispatch_writes:
  1054. BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC]));
  1055. if (ad->batch_data_dir == REQ_SYNC) {
  1056. ad->changed_batch = 1;
  1057. /*
  1058. * new_batch might be 1 when the queue runs out of
  1059. * reads. A subsequent submission of a write might
  1060. * cause a change of batch before the read is finished.
  1061. */
  1062. ad->new_batch = 0;
  1063. }
  1064. ad->batch_data_dir = REQ_ASYNC;
  1065. ad->current_write_count = ad->write_batch_count;
  1066. ad->write_batch_idled = 0;
  1067. arq = ad->next_arq[ad->batch_data_dir];
  1068. goto dispatch_request;
  1069. }
  1070. BUG();
  1071. return 0;
  1072. dispatch_request:
  1073. /*
  1074. * If a request has expired, service it.
  1075. */
  1076. if (as_fifo_expired(ad, ad->batch_data_dir)) {
  1077. fifo_expired:
  1078. arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next);
  1079. BUG_ON(arq == NULL);
  1080. }
  1081. if (ad->changed_batch) {
  1082. WARN_ON(ad->new_batch);
  1083. if (ad->nr_dispatched)
  1084. return 0;
  1085. if (ad->batch_data_dir == REQ_ASYNC)
  1086. ad->current_batch_expires = jiffies +
  1087. ad->batch_expire[REQ_ASYNC];
  1088. else
  1089. ad->new_batch = 1;
  1090. ad->changed_batch = 0;
  1091. }
  1092. /*
  1093. * arq is the selected appropriate request.
  1094. */
  1095. as_move_to_dispatch(ad, arq);
  1096. return 1;
  1097. }
  1098. /*
  1099. * Add arq to a list behind alias
  1100. */
  1101. static inline void
  1102. as_add_aliased_request(struct as_data *ad, struct as_rq *arq, struct as_rq *alias)
  1103. {
  1104. struct request *req = arq->request;
  1105. struct list_head *insert = alias->request->queuelist.prev;
  1106. /*
  1107. * Transfer list of aliases
  1108. */
  1109. while (!list_empty(&req->queuelist)) {
  1110. struct request *__rq = list_entry_rq(req->queuelist.next);
  1111. struct as_rq *__arq = RQ_DATA(__rq);
  1112. list_move_tail(&__rq->queuelist, &alias->request->queuelist);
  1113. WARN_ON(__arq->state != AS_RQ_QUEUED);
  1114. }
  1115. /*
  1116. * Another request with the same start sector on the rbtree.
  1117. * Link this request to that sector. They are untangled in
  1118. * as_move_to_dispatch
  1119. */
  1120. list_add(&arq->request->queuelist, insert);
  1121. /*
  1122. * Don't want to have to handle merges.
  1123. */
  1124. as_del_arq_hash(arq);
  1125. }
  1126. /*
  1127. * add arq to rbtree and fifo
  1128. */
  1129. static void as_add_request(request_queue_t *q, struct request *rq)
  1130. {
  1131. struct as_data *ad = q->elevator->elevator_data;
  1132. struct as_rq *arq = RQ_DATA(rq);
  1133. struct as_rq *alias;
  1134. int data_dir;
  1135. if (arq->state != AS_RQ_PRESCHED) {
  1136. printk("arq->state: %d\n", arq->state);
  1137. WARN_ON(1);
  1138. }
  1139. arq->state = AS_RQ_NEW;
  1140. if (rq_data_dir(arq->request) == READ
  1141. || current->flags&PF_SYNCWRITE)
  1142. arq->is_sync = 1;
  1143. else
  1144. arq->is_sync = 0;
  1145. data_dir = arq->is_sync;
  1146. arq->io_context = as_get_io_context();
  1147. if (arq->io_context) {
  1148. as_update_iohist(ad, arq->io_context->aic, arq->request);
  1149. atomic_inc(&arq->io_context->aic->nr_queued);
  1150. }
  1151. alias = as_add_arq_rb(ad, arq);
  1152. if (!alias) {
  1153. /*
  1154. * set expire time (only used for reads) and add to fifo list
  1155. */
  1156. arq->expires = jiffies + ad->fifo_expire[data_dir];
  1157. list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]);
  1158. if (rq_mergeable(arq->request))
  1159. as_add_arq_hash(ad, arq);
  1160. as_update_arq(ad, arq); /* keep state machine up to date */
  1161. } else {
  1162. as_add_aliased_request(ad, arq, alias);
  1163. /*
  1164. * have we been anticipating this request?
  1165. * or does it come from the same process as the one we are
  1166. * anticipating for?
  1167. */
  1168. if (ad->antic_status == ANTIC_WAIT_REQ
  1169. || ad->antic_status == ANTIC_WAIT_NEXT) {
  1170. if (as_can_break_anticipation(ad, arq))
  1171. as_antic_stop(ad);
  1172. }
  1173. }
  1174. arq->state = AS_RQ_QUEUED;
  1175. }
  1176. static void as_activate_request(request_queue_t *q, struct request *rq)
  1177. {
  1178. struct as_rq *arq = RQ_DATA(rq);
  1179. WARN_ON(arq->state != AS_RQ_DISPATCHED);
  1180. arq->state = AS_RQ_REMOVED;
  1181. if (arq->io_context && arq->io_context->aic)
  1182. atomic_dec(&arq->io_context->aic->nr_dispatched);
  1183. }
  1184. static void as_deactivate_request(request_queue_t *q, struct request *rq)
  1185. {
  1186. struct as_rq *arq = RQ_DATA(rq);
  1187. WARN_ON(arq->state != AS_RQ_REMOVED);
  1188. arq->state = AS_RQ_DISPATCHED;
  1189. if (arq->io_context && arq->io_context->aic)
  1190. atomic_inc(&arq->io_context->aic->nr_dispatched);
  1191. }
  1192. /*
  1193. * as_queue_empty tells us if there are requests left in the device. It may
  1194. * not be the case that a driver can get the next request even if the queue
  1195. * is not empty - it is used in the block layer to check for plugging and
  1196. * merging opportunities
  1197. */
  1198. static int as_queue_empty(request_queue_t *q)
  1199. {
  1200. struct as_data *ad = q->elevator->elevator_data;
  1201. return list_empty(&ad->fifo_list[REQ_ASYNC])
  1202. && list_empty(&ad->fifo_list[REQ_SYNC]);
  1203. }
  1204. static struct request *
  1205. as_former_request(request_queue_t *q, struct request *rq)
  1206. {
  1207. struct as_rq *arq = RQ_DATA(rq);
  1208. struct rb_node *rbprev = rb_prev(&arq->rb_node);
  1209. struct request *ret = NULL;
  1210. if (rbprev)
  1211. ret = rb_entry_arq(rbprev)->request;
  1212. return ret;
  1213. }
  1214. static struct request *
  1215. as_latter_request(request_queue_t *q, struct request *rq)
  1216. {
  1217. struct as_rq *arq = RQ_DATA(rq);
  1218. struct rb_node *rbnext = rb_next(&arq->rb_node);
  1219. struct request *ret = NULL;
  1220. if (rbnext)
  1221. ret = rb_entry_arq(rbnext)->request;
  1222. return ret;
  1223. }
  1224. static int
  1225. as_merge(request_queue_t *q, struct request **req, struct bio *bio)
  1226. {
  1227. struct as_data *ad = q->elevator->elevator_data;
  1228. sector_t rb_key = bio->bi_sector + bio_sectors(bio);
  1229. struct request *__rq;
  1230. int ret;
  1231. /*
  1232. * see if the merge hash can satisfy a back merge
  1233. */
  1234. __rq = as_find_arq_hash(ad, bio->bi_sector);
  1235. if (__rq) {
  1236. BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector);
  1237. if (elv_rq_merge_ok(__rq, bio)) {
  1238. ret = ELEVATOR_BACK_MERGE;
  1239. goto out;
  1240. }
  1241. }
  1242. /*
  1243. * check for front merge
  1244. */
  1245. __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio));
  1246. if (__rq) {
  1247. BUG_ON(rb_key != rq_rb_key(__rq));
  1248. if (elv_rq_merge_ok(__rq, bio)) {
  1249. ret = ELEVATOR_FRONT_MERGE;
  1250. goto out;
  1251. }
  1252. }
  1253. return ELEVATOR_NO_MERGE;
  1254. out:
  1255. if (ret) {
  1256. if (rq_mergeable(__rq))
  1257. as_hot_arq_hash(ad, RQ_DATA(__rq));
  1258. }
  1259. *req = __rq;
  1260. return ret;
  1261. }
  1262. static void as_merged_request(request_queue_t *q, struct request *req)
  1263. {
  1264. struct as_data *ad = q->elevator->elevator_data;
  1265. struct as_rq *arq = RQ_DATA(req);
  1266. /*
  1267. * hash always needs to be repositioned, key is end sector
  1268. */
  1269. as_del_arq_hash(arq);
  1270. as_add_arq_hash(ad, arq);
  1271. /*
  1272. * if the merge was a front merge, we need to reposition request
  1273. */
  1274. if (rq_rb_key(req) != arq->rb_key) {
  1275. struct as_rq *alias, *next_arq = NULL;
  1276. if (ad->next_arq[arq->is_sync] == arq)
  1277. next_arq = as_find_next_arq(ad, arq);
  1278. /*
  1279. * Note! We should really be moving any old aliased requests
  1280. * off this request and try to insert them into the rbtree. We
  1281. * currently don't bother. Ditto the next function.
  1282. */
  1283. as_del_arq_rb(ad, arq);
  1284. if ((alias = as_add_arq_rb(ad, arq)) ) {
  1285. list_del_init(&arq->fifo);
  1286. as_add_aliased_request(ad, arq, alias);
  1287. if (next_arq)
  1288. ad->next_arq[arq->is_sync] = next_arq;
  1289. }
  1290. /*
  1291. * Note! At this stage of this and the next function, our next
  1292. * request may not be optimal - eg the request may have "grown"
  1293. * behind the disk head. We currently don't bother adjusting.
  1294. */
  1295. }
  1296. }
  1297. static void
  1298. as_merged_requests(request_queue_t *q, struct request *req,
  1299. struct request *next)
  1300. {
  1301. struct as_data *ad = q->elevator->elevator_data;
  1302. struct as_rq *arq = RQ_DATA(req);
  1303. struct as_rq *anext = RQ_DATA(next);
  1304. BUG_ON(!arq);
  1305. BUG_ON(!anext);
  1306. /*
  1307. * reposition arq (this is the merged request) in hash, and in rbtree
  1308. * in case of a front merge
  1309. */
  1310. as_del_arq_hash(arq);
  1311. as_add_arq_hash(ad, arq);
  1312. if (rq_rb_key(req) != arq->rb_key) {
  1313. struct as_rq *alias, *next_arq = NULL;
  1314. if (ad->next_arq[arq->is_sync] == arq)
  1315. next_arq = as_find_next_arq(ad, arq);
  1316. as_del_arq_rb(ad, arq);
  1317. if ((alias = as_add_arq_rb(ad, arq)) ) {
  1318. list_del_init(&arq->fifo);
  1319. as_add_aliased_request(ad, arq, alias);
  1320. if (next_arq)
  1321. ad->next_arq[arq->is_sync] = next_arq;
  1322. }
  1323. }
  1324. /*
  1325. * if anext expires before arq, assign its expire time to arq
  1326. * and move into anext position (anext will be deleted) in fifo
  1327. */
  1328. if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) {
  1329. if (time_before(anext->expires, arq->expires)) {
  1330. list_move(&arq->fifo, &anext->fifo);
  1331. arq->expires = anext->expires;
  1332. /*
  1333. * Don't copy here but swap, because when anext is
  1334. * removed below, it must contain the unused context
  1335. */
  1336. swap_io_context(&arq->io_context, &anext->io_context);
  1337. }
  1338. }
  1339. /*
  1340. * Transfer list of aliases
  1341. */
  1342. while (!list_empty(&next->queuelist)) {
  1343. struct request *__rq = list_entry_rq(next->queuelist.next);
  1344. struct as_rq *__arq = RQ_DATA(__rq);
  1345. list_move_tail(&__rq->queuelist, &req->queuelist);
  1346. WARN_ON(__arq->state != AS_RQ_QUEUED);
  1347. }
  1348. /*
  1349. * kill knowledge of next, this one is a goner
  1350. */
  1351. as_remove_queued_request(q, next);
  1352. as_put_io_context(anext);
  1353. anext->state = AS_RQ_MERGED;
  1354. }
  1355. /*
  1356. * This is executed in a "deferred" process context, by kblockd. It calls the
  1357. * driver's request_fn so the driver can submit that request.
  1358. *
  1359. * IMPORTANT! This guy will reenter the elevator, so set up all queue global
  1360. * state before calling, and don't rely on any state over calls.
  1361. *
  1362. * FIXME! dispatch queue is not a queue at all!
  1363. */
  1364. static void as_work_handler(void *data)
  1365. {
  1366. struct request_queue *q = data;
  1367. unsigned long flags;
  1368. spin_lock_irqsave(q->queue_lock, flags);
  1369. if (!as_queue_empty(q))
  1370. q->request_fn(q);
  1371. spin_unlock_irqrestore(q->queue_lock, flags);
  1372. }
  1373. static void as_put_request(request_queue_t *q, struct request *rq)
  1374. {
  1375. struct as_data *ad = q->elevator->elevator_data;
  1376. struct as_rq *arq = RQ_DATA(rq);
  1377. if (!arq) {
  1378. WARN_ON(1);
  1379. return;
  1380. }
  1381. if (unlikely(arq->state != AS_RQ_POSTSCHED &&
  1382. arq->state != AS_RQ_PRESCHED &&
  1383. arq->state != AS_RQ_MERGED)) {
  1384. printk("arq->state %d\n", arq->state);
  1385. WARN_ON(1);
  1386. }
  1387. mempool_free(arq, ad->arq_pool);
  1388. rq->elevator_private = NULL;
  1389. }
  1390. static int as_set_request(request_queue_t *q, struct request *rq,
  1391. struct bio *bio, gfp_t gfp_mask)
  1392. {
  1393. struct as_data *ad = q->elevator->elevator_data;
  1394. struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask);
  1395. if (arq) {
  1396. memset(arq, 0, sizeof(*arq));
  1397. RB_CLEAR(&arq->rb_node);
  1398. arq->request = rq;
  1399. arq->state = AS_RQ_PRESCHED;
  1400. arq->io_context = NULL;
  1401. INIT_LIST_HEAD(&arq->hash);
  1402. arq->on_hash = 0;
  1403. INIT_LIST_HEAD(&arq->fifo);
  1404. rq->elevator_private = arq;
  1405. return 0;
  1406. }
  1407. return 1;
  1408. }
  1409. static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1410. {
  1411. int ret = ELV_MQUEUE_MAY;
  1412. struct as_data *ad = q->elevator->elevator_data;
  1413. struct io_context *ioc;
  1414. if (ad->antic_status == ANTIC_WAIT_REQ ||
  1415. ad->antic_status == ANTIC_WAIT_NEXT) {
  1416. ioc = as_get_io_context();
  1417. if (ad->io_context == ioc)
  1418. ret = ELV_MQUEUE_MUST;
  1419. put_io_context(ioc);
  1420. }
  1421. return ret;
  1422. }
  1423. static void as_exit_queue(elevator_t *e)
  1424. {
  1425. struct as_data *ad = e->elevator_data;
  1426. del_timer_sync(&ad->antic_timer);
  1427. kblockd_flush();
  1428. BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
  1429. BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
  1430. mempool_destroy(ad->arq_pool);
  1431. put_io_context(ad->io_context);
  1432. kfree(ad->hash);
  1433. kfree(ad);
  1434. }
  1435. /*
  1436. * initialize elevator private data (as_data), and alloc a arq for
  1437. * each request on the free lists
  1438. */
  1439. static int as_init_queue(request_queue_t *q, elevator_t *e)
  1440. {
  1441. struct as_data *ad;
  1442. int i;
  1443. if (!arq_pool)
  1444. return -ENOMEM;
  1445. ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
  1446. if (!ad)
  1447. return -ENOMEM;
  1448. memset(ad, 0, sizeof(*ad));
  1449. ad->q = q; /* Identify what queue the data belongs to */
  1450. ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES,
  1451. GFP_KERNEL, q->node);
  1452. if (!ad->hash) {
  1453. kfree(ad);
  1454. return -ENOMEM;
  1455. }
  1456. ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  1457. mempool_free_slab, arq_pool, q->node);
  1458. if (!ad->arq_pool) {
  1459. kfree(ad->hash);
  1460. kfree(ad);
  1461. return -ENOMEM;
  1462. }
  1463. /* anticipatory scheduling helpers */
  1464. ad->antic_timer.function = as_antic_timeout;
  1465. ad->antic_timer.data = (unsigned long)q;
  1466. init_timer(&ad->antic_timer);
  1467. INIT_WORK(&ad->antic_work, as_work_handler, q);
  1468. for (i = 0; i < AS_HASH_ENTRIES; i++)
  1469. INIT_LIST_HEAD(&ad->hash[i]);
  1470. INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
  1471. INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
  1472. ad->sort_list[REQ_SYNC] = RB_ROOT;
  1473. ad->sort_list[REQ_ASYNC] = RB_ROOT;
  1474. ad->fifo_expire[REQ_SYNC] = default_read_expire;
  1475. ad->fifo_expire[REQ_ASYNC] = default_write_expire;
  1476. ad->antic_expire = default_antic_expire;
  1477. ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
  1478. ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
  1479. e->elevator_data = ad;
  1480. ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
  1481. ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
  1482. if (ad->write_batch_count < 2)
  1483. ad->write_batch_count = 2;
  1484. return 0;
  1485. }
  1486. /*
  1487. * sysfs parts below
  1488. */
  1489. struct as_fs_entry {
  1490. struct attribute attr;
  1491. ssize_t (*show)(struct as_data *, char *);
  1492. ssize_t (*store)(struct as_data *, const char *, size_t);
  1493. };
  1494. static ssize_t
  1495. as_var_show(unsigned int var, char *page)
  1496. {
  1497. return sprintf(page, "%d\n", var);
  1498. }
  1499. static ssize_t
  1500. as_var_store(unsigned long *var, const char *page, size_t count)
  1501. {
  1502. char *p = (char *) page;
  1503. *var = simple_strtoul(p, &p, 10);
  1504. return count;
  1505. }
  1506. static ssize_t as_est_show(struct as_data *ad, char *page)
  1507. {
  1508. int pos = 0;
  1509. pos += sprintf(page+pos, "%lu %% exit probability\n", 100*ad->exit_prob/256);
  1510. pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
  1511. pos += sprintf(page+pos, "%llu sectors new seek distance\n", (unsigned long long)ad->new_seek_mean);
  1512. return pos;
  1513. }
  1514. #define SHOW_FUNCTION(__FUNC, __VAR) \
  1515. static ssize_t __FUNC(struct as_data *ad, char *page) \
  1516. { \
  1517. return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
  1518. }
  1519. SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]);
  1520. SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]);
  1521. SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire);
  1522. SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]);
  1523. SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]);
  1524. #undef SHOW_FUNCTION
  1525. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  1526. static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \
  1527. { \
  1528. int ret = as_var_store(__PTR, (page), count); \
  1529. if (*(__PTR) < (MIN)) \
  1530. *(__PTR) = (MIN); \
  1531. else if (*(__PTR) > (MAX)) \
  1532. *(__PTR) = (MAX); \
  1533. *(__PTR) = msecs_to_jiffies(*(__PTR)); \
  1534. return ret; \
  1535. }
  1536. STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
  1537. STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
  1538. STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX);
  1539. STORE_FUNCTION(as_read_batchexpire_store,
  1540. &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
  1541. STORE_FUNCTION(as_write_batchexpire_store,
  1542. &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
  1543. #undef STORE_FUNCTION
  1544. static struct as_fs_entry as_est_entry = {
  1545. .attr = {.name = "est_time", .mode = S_IRUGO },
  1546. .show = as_est_show,
  1547. };
  1548. static struct as_fs_entry as_readexpire_entry = {
  1549. .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR },
  1550. .show = as_readexpire_show,
  1551. .store = as_readexpire_store,
  1552. };
  1553. static struct as_fs_entry as_writeexpire_entry = {
  1554. .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR },
  1555. .show = as_writeexpire_show,
  1556. .store = as_writeexpire_store,
  1557. };
  1558. static struct as_fs_entry as_anticexpire_entry = {
  1559. .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR },
  1560. .show = as_anticexpire_show,
  1561. .store = as_anticexpire_store,
  1562. };
  1563. static struct as_fs_entry as_read_batchexpire_entry = {
  1564. .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR },
  1565. .show = as_read_batchexpire_show,
  1566. .store = as_read_batchexpire_store,
  1567. };
  1568. static struct as_fs_entry as_write_batchexpire_entry = {
  1569. .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR },
  1570. .show = as_write_batchexpire_show,
  1571. .store = as_write_batchexpire_store,
  1572. };
  1573. static struct attribute *default_attrs[] = {
  1574. &as_est_entry.attr,
  1575. &as_readexpire_entry.attr,
  1576. &as_writeexpire_entry.attr,
  1577. &as_anticexpire_entry.attr,
  1578. &as_read_batchexpire_entry.attr,
  1579. &as_write_batchexpire_entry.attr,
  1580. NULL,
  1581. };
  1582. #define to_as(atr) container_of((atr), struct as_fs_entry, attr)
  1583. static ssize_t
  1584. as_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  1585. {
  1586. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1587. struct as_fs_entry *entry = to_as(attr);
  1588. if (!entry->show)
  1589. return -EIO;
  1590. return entry->show(e->elevator_data, page);
  1591. }
  1592. static ssize_t
  1593. as_attr_store(struct kobject *kobj, struct attribute *attr,
  1594. const char *page, size_t length)
  1595. {
  1596. elevator_t *e = container_of(kobj, elevator_t, kobj);
  1597. struct as_fs_entry *entry = to_as(attr);
  1598. if (!entry->store)
  1599. return -EIO;
  1600. return entry->store(e->elevator_data, page, length);
  1601. }
  1602. static struct sysfs_ops as_sysfs_ops = {
  1603. .show = as_attr_show,
  1604. .store = as_attr_store,
  1605. };
  1606. static struct kobj_type as_ktype = {
  1607. .sysfs_ops = &as_sysfs_ops,
  1608. .default_attrs = default_attrs,
  1609. };
  1610. static struct elevator_type iosched_as = {
  1611. .ops = {
  1612. .elevator_merge_fn = as_merge,
  1613. .elevator_merged_fn = as_merged_request,
  1614. .elevator_merge_req_fn = as_merged_requests,
  1615. .elevator_dispatch_fn = as_dispatch_request,
  1616. .elevator_add_req_fn = as_add_request,
  1617. .elevator_activate_req_fn = as_activate_request,
  1618. .elevator_deactivate_req_fn = as_deactivate_request,
  1619. .elevator_queue_empty_fn = as_queue_empty,
  1620. .elevator_completed_req_fn = as_completed_request,
  1621. .elevator_former_req_fn = as_former_request,
  1622. .elevator_latter_req_fn = as_latter_request,
  1623. .elevator_set_req_fn = as_set_request,
  1624. .elevator_put_req_fn = as_put_request,
  1625. .elevator_may_queue_fn = as_may_queue,
  1626. .elevator_init_fn = as_init_queue,
  1627. .elevator_exit_fn = as_exit_queue,
  1628. },
  1629. .elevator_ktype = &as_ktype,
  1630. .elevator_name = "anticipatory",
  1631. .elevator_owner = THIS_MODULE,
  1632. };
  1633. static int __init as_init(void)
  1634. {
  1635. int ret;
  1636. arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
  1637. 0, 0, NULL, NULL);
  1638. if (!arq_pool)
  1639. return -ENOMEM;
  1640. ret = elv_register(&iosched_as);
  1641. if (!ret) {
  1642. /*
  1643. * don't allow AS to get unregistered, since we would have
  1644. * to browse all tasks in the system and release their
  1645. * as_io_context first
  1646. */
  1647. __module_get(THIS_MODULE);
  1648. return 0;
  1649. }
  1650. kmem_cache_destroy(arq_pool);
  1651. return ret;
  1652. }
  1653. static void __exit as_exit(void)
  1654. {
  1655. kmem_cache_destroy(arq_pool);
  1656. elv_unregister(&iosched_as);
  1657. }
  1658. module_init(as_init);
  1659. module_exit(as_exit);
  1660. MODULE_AUTHOR("Nick Piggin");
  1661. MODULE_LICENSE("GPL");
  1662. MODULE_DESCRIPTION("anticipatory IO scheduler");