as-iosched.c 47 KB

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