as-iosched.c 52 KB

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