as-iosched.c 45 KB

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