as-iosched.c 47 KB

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