as-iosched.c 46 KB

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