as-iosched.c 45 KB

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