as-iosched.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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 hlist_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 hlist_node hash;
  145. /*
  146. * expire fifo
  147. */
  148. struct list_head fifo;
  149. unsigned long expires;
  150. unsigned int is_sync;
  151. enum arq_state state;
  152. };
  153. #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private)
  154. static kmem_cache_t *arq_pool;
  155. static atomic_t ioc_count = ATOMIC_INIT(0);
  156. static struct completion *ioc_gone;
  157. static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq);
  158. static void as_antic_stop(struct as_data *ad);
  159. /*
  160. * IO Context helper functions
  161. */
  162. /* Called to deallocate the as_io_context */
  163. static void free_as_io_context(struct as_io_context *aic)
  164. {
  165. kfree(aic);
  166. if (atomic_dec_and_test(&ioc_count) && ioc_gone)
  167. complete(ioc_gone);
  168. }
  169. static void as_trim(struct io_context *ioc)
  170. {
  171. if (ioc->aic)
  172. free_as_io_context(ioc->aic);
  173. ioc->aic = NULL;
  174. }
  175. /* Called when the task exits */
  176. static void exit_as_io_context(struct as_io_context *aic)
  177. {
  178. WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state));
  179. clear_bit(AS_TASK_RUNNING, &aic->state);
  180. }
  181. static struct as_io_context *alloc_as_io_context(void)
  182. {
  183. struct as_io_context *ret;
  184. ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
  185. if (ret) {
  186. ret->dtor = free_as_io_context;
  187. ret->exit = exit_as_io_context;
  188. ret->state = 1 << AS_TASK_RUNNING;
  189. atomic_set(&ret->nr_queued, 0);
  190. atomic_set(&ret->nr_dispatched, 0);
  191. spin_lock_init(&ret->lock);
  192. ret->ttime_total = 0;
  193. ret->ttime_samples = 0;
  194. ret->ttime_mean = 0;
  195. ret->seek_total = 0;
  196. ret->seek_samples = 0;
  197. ret->seek_mean = 0;
  198. atomic_inc(&ioc_count);
  199. }
  200. return ret;
  201. }
  202. /*
  203. * If the current task has no AS IO context then create one and initialise it.
  204. * Then take a ref on the task's io context and return it.
  205. */
  206. static struct io_context *as_get_io_context(void)
  207. {
  208. struct io_context *ioc = get_io_context(GFP_ATOMIC);
  209. if (ioc && !ioc->aic) {
  210. ioc->aic = alloc_as_io_context();
  211. if (!ioc->aic) {
  212. put_io_context(ioc);
  213. ioc = NULL;
  214. }
  215. }
  216. return ioc;
  217. }
  218. static void as_put_io_context(struct as_rq *arq)
  219. {
  220. struct as_io_context *aic;
  221. if (unlikely(!arq->io_context))
  222. return;
  223. aic = arq->io_context->aic;
  224. if (arq->is_sync == REQ_SYNC && aic) {
  225. spin_lock(&aic->lock);
  226. set_bit(AS_TASK_IORUNNING, &aic->state);
  227. aic->last_end_request = jiffies;
  228. spin_unlock(&aic->lock);
  229. }
  230. put_io_context(arq->io_context);
  231. }
  232. /*
  233. * the back merge hash support functions
  234. */
  235. static const int as_hash_shift = 6;
  236. #define AS_HASH_BLOCK(sec) ((sec) >> 3)
  237. #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift))
  238. #define AS_HASH_ENTRIES (1 << as_hash_shift)
  239. #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
  240. static inline void __as_del_arq_hash(struct as_rq *arq)
  241. {
  242. hlist_del_init(&arq->hash);
  243. }
  244. static inline void as_del_arq_hash(struct as_rq *arq)
  245. {
  246. if (!hlist_unhashed(&arq->hash))
  247. __as_del_arq_hash(arq);
  248. }
  249. static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq)
  250. {
  251. struct request *rq = arq->request;
  252. BUG_ON(!hlist_unhashed(&arq->hash));
  253. hlist_add_head(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]);
  254. }
  255. /*
  256. * move hot entry to front of chain
  257. */
  258. static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq)
  259. {
  260. struct request *rq = arq->request;
  261. struct hlist_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))];
  262. if (hlist_unhashed(&arq->hash)) {
  263. WARN_ON(1);
  264. return;
  265. }
  266. if (&arq->hash != head->first) {
  267. hlist_del(&arq->hash);
  268. hlist_add_head(&arq->hash, head);
  269. }
  270. }
  271. static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset)
  272. {
  273. struct hlist_head *hash_list = &ad->hash[AS_HASH_FN(offset)];
  274. struct hlist_node *entry, *next;
  275. struct as_rq *arq;
  276. hlist_for_each_entry_safe(arq, entry, next, hash_list, hash) {
  277. struct request *__rq = arq->request;
  278. BUG_ON(hlist_unhashed(&arq->hash));
  279. if (!rq_mergeable(__rq)) {
  280. as_del_arq_hash(arq);
  281. continue;
  282. }
  283. if (rq_hash_key(__rq) == offset)
  284. return __rq;
  285. }
  286. return NULL;
  287. }
  288. /*
  289. * rb tree support functions
  290. */
  291. #define RB_EMPTY(root) ((root)->rb_node == NULL)
  292. #define ON_RB(node) (rb_parent(node) != node)
  293. #define RB_CLEAR(node) (rb_set_parent(node, node))
  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. || (arq->request->flags & REQ_RW_SYNC))
  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_HLIST_NODE(&arq->hash);
  1339. INIT_LIST_HEAD(&arq->fifo);
  1340. rq->elevator_private = arq;
  1341. return 0;
  1342. }
  1343. return 1;
  1344. }
  1345. static int as_may_queue(request_queue_t *q, int rw, struct bio *bio)
  1346. {
  1347. int ret = ELV_MQUEUE_MAY;
  1348. struct as_data *ad = q->elevator->elevator_data;
  1349. struct io_context *ioc;
  1350. if (ad->antic_status == ANTIC_WAIT_REQ ||
  1351. ad->antic_status == ANTIC_WAIT_NEXT) {
  1352. ioc = as_get_io_context();
  1353. if (ad->io_context == ioc)
  1354. ret = ELV_MQUEUE_MUST;
  1355. put_io_context(ioc);
  1356. }
  1357. return ret;
  1358. }
  1359. static void as_exit_queue(elevator_t *e)
  1360. {
  1361. struct as_data *ad = e->elevator_data;
  1362. del_timer_sync(&ad->antic_timer);
  1363. kblockd_flush();
  1364. BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC]));
  1365. BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC]));
  1366. mempool_destroy(ad->arq_pool);
  1367. put_io_context(ad->io_context);
  1368. kfree(ad->hash);
  1369. kfree(ad);
  1370. }
  1371. /*
  1372. * initialize elevator private data (as_data), and alloc a arq for
  1373. * each request on the free lists
  1374. */
  1375. static void *as_init_queue(request_queue_t *q, elevator_t *e)
  1376. {
  1377. struct as_data *ad;
  1378. int i;
  1379. if (!arq_pool)
  1380. return NULL;
  1381. ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node);
  1382. if (!ad)
  1383. return NULL;
  1384. memset(ad, 0, sizeof(*ad));
  1385. ad->q = q; /* Identify what queue the data belongs to */
  1386. ad->hash = kmalloc_node(sizeof(struct hlist_head)*AS_HASH_ENTRIES,
  1387. GFP_KERNEL, q->node);
  1388. if (!ad->hash) {
  1389. kfree(ad);
  1390. return NULL;
  1391. }
  1392. ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
  1393. mempool_free_slab, arq_pool, q->node);
  1394. if (!ad->arq_pool) {
  1395. kfree(ad->hash);
  1396. kfree(ad);
  1397. return NULL;
  1398. }
  1399. /* anticipatory scheduling helpers */
  1400. ad->antic_timer.function = as_antic_timeout;
  1401. ad->antic_timer.data = (unsigned long)q;
  1402. init_timer(&ad->antic_timer);
  1403. INIT_WORK(&ad->antic_work, as_work_handler, q);
  1404. for (i = 0; i < AS_HASH_ENTRIES; i++)
  1405. INIT_HLIST_HEAD(&ad->hash[i]);
  1406. INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]);
  1407. INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]);
  1408. ad->sort_list[REQ_SYNC] = RB_ROOT;
  1409. ad->sort_list[REQ_ASYNC] = RB_ROOT;
  1410. ad->fifo_expire[REQ_SYNC] = default_read_expire;
  1411. ad->fifo_expire[REQ_ASYNC] = default_write_expire;
  1412. ad->antic_expire = default_antic_expire;
  1413. ad->batch_expire[REQ_SYNC] = default_read_batch_expire;
  1414. ad->batch_expire[REQ_ASYNC] = default_write_batch_expire;
  1415. ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC];
  1416. ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10;
  1417. if (ad->write_batch_count < 2)
  1418. ad->write_batch_count = 2;
  1419. return ad;
  1420. }
  1421. /*
  1422. * sysfs parts below
  1423. */
  1424. static ssize_t
  1425. as_var_show(unsigned int var, char *page)
  1426. {
  1427. return sprintf(page, "%d\n", var);
  1428. }
  1429. static ssize_t
  1430. as_var_store(unsigned long *var, const char *page, size_t count)
  1431. {
  1432. char *p = (char *) page;
  1433. *var = simple_strtoul(p, &p, 10);
  1434. return count;
  1435. }
  1436. static ssize_t est_time_show(elevator_t *e, char *page)
  1437. {
  1438. struct as_data *ad = e->elevator_data;
  1439. int pos = 0;
  1440. pos += sprintf(page+pos, "%lu %% exit probability\n",
  1441. 100*ad->exit_prob/256);
  1442. pos += sprintf(page+pos, "%lu %% probability of exiting without a "
  1443. "cooperating process submitting IO\n",
  1444. 100*ad->exit_no_coop/256);
  1445. pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean);
  1446. pos += sprintf(page+pos, "%llu sectors new seek distance\n",
  1447. (unsigned long long)ad->new_seek_mean);
  1448. return pos;
  1449. }
  1450. #define SHOW_FUNCTION(__FUNC, __VAR) \
  1451. static ssize_t __FUNC(elevator_t *e, char *page) \
  1452. { \
  1453. struct as_data *ad = e->elevator_data; \
  1454. return as_var_show(jiffies_to_msecs((__VAR)), (page)); \
  1455. }
  1456. SHOW_FUNCTION(as_read_expire_show, ad->fifo_expire[REQ_SYNC]);
  1457. SHOW_FUNCTION(as_write_expire_show, ad->fifo_expire[REQ_ASYNC]);
  1458. SHOW_FUNCTION(as_antic_expire_show, ad->antic_expire);
  1459. SHOW_FUNCTION(as_read_batch_expire_show, ad->batch_expire[REQ_SYNC]);
  1460. SHOW_FUNCTION(as_write_batch_expire_show, ad->batch_expire[REQ_ASYNC]);
  1461. #undef SHOW_FUNCTION
  1462. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
  1463. static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
  1464. { \
  1465. struct as_data *ad = e->elevator_data; \
  1466. int ret = as_var_store(__PTR, (page), count); \
  1467. if (*(__PTR) < (MIN)) \
  1468. *(__PTR) = (MIN); \
  1469. else if (*(__PTR) > (MAX)) \
  1470. *(__PTR) = (MAX); \
  1471. *(__PTR) = msecs_to_jiffies(*(__PTR)); \
  1472. return ret; \
  1473. }
  1474. STORE_FUNCTION(as_read_expire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX);
  1475. STORE_FUNCTION(as_write_expire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX);
  1476. STORE_FUNCTION(as_antic_expire_store, &ad->antic_expire, 0, INT_MAX);
  1477. STORE_FUNCTION(as_read_batch_expire_store,
  1478. &ad->batch_expire[REQ_SYNC], 0, INT_MAX);
  1479. STORE_FUNCTION(as_write_batch_expire_store,
  1480. &ad->batch_expire[REQ_ASYNC], 0, INT_MAX);
  1481. #undef STORE_FUNCTION
  1482. #define AS_ATTR(name) \
  1483. __ATTR(name, S_IRUGO|S_IWUSR, as_##name##_show, as_##name##_store)
  1484. static struct elv_fs_entry as_attrs[] = {
  1485. __ATTR_RO(est_time),
  1486. AS_ATTR(read_expire),
  1487. AS_ATTR(write_expire),
  1488. AS_ATTR(antic_expire),
  1489. AS_ATTR(read_batch_expire),
  1490. AS_ATTR(write_batch_expire),
  1491. __ATTR_NULL
  1492. };
  1493. static struct elevator_type iosched_as = {
  1494. .ops = {
  1495. .elevator_merge_fn = as_merge,
  1496. .elevator_merged_fn = as_merged_request,
  1497. .elevator_merge_req_fn = as_merged_requests,
  1498. .elevator_dispatch_fn = as_dispatch_request,
  1499. .elevator_add_req_fn = as_add_request,
  1500. .elevator_activate_req_fn = as_activate_request,
  1501. .elevator_deactivate_req_fn = as_deactivate_request,
  1502. .elevator_queue_empty_fn = as_queue_empty,
  1503. .elevator_completed_req_fn = as_completed_request,
  1504. .elevator_former_req_fn = as_former_request,
  1505. .elevator_latter_req_fn = as_latter_request,
  1506. .elevator_set_req_fn = as_set_request,
  1507. .elevator_put_req_fn = as_put_request,
  1508. .elevator_may_queue_fn = as_may_queue,
  1509. .elevator_init_fn = as_init_queue,
  1510. .elevator_exit_fn = as_exit_queue,
  1511. .trim = as_trim,
  1512. },
  1513. .elevator_attrs = as_attrs,
  1514. .elevator_name = "anticipatory",
  1515. .elevator_owner = THIS_MODULE,
  1516. };
  1517. static int __init as_init(void)
  1518. {
  1519. int ret;
  1520. arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq),
  1521. 0, 0, NULL, NULL);
  1522. if (!arq_pool)
  1523. return -ENOMEM;
  1524. ret = elv_register(&iosched_as);
  1525. if (!ret) {
  1526. /*
  1527. * don't allow AS to get unregistered, since we would have
  1528. * to browse all tasks in the system and release their
  1529. * as_io_context first
  1530. */
  1531. __module_get(THIS_MODULE);
  1532. return 0;
  1533. }
  1534. kmem_cache_destroy(arq_pool);
  1535. return ret;
  1536. }
  1537. static void __exit as_exit(void)
  1538. {
  1539. DECLARE_COMPLETION(all_gone);
  1540. elv_unregister(&iosched_as);
  1541. ioc_gone = &all_gone;
  1542. /* ioc_gone's update must be visible before reading ioc_count */
  1543. smp_wmb();
  1544. if (atomic_read(&ioc_count))
  1545. wait_for_completion(ioc_gone);
  1546. synchronize_rcu();
  1547. kmem_cache_destroy(arq_pool);
  1548. }
  1549. module_init(as_init);
  1550. module_exit(as_exit);
  1551. MODULE_AUTHOR("Nick Piggin");
  1552. MODULE_LICENSE("GPL");
  1553. MODULE_DESCRIPTION("anticipatory IO scheduler");