as-iosched.c 40 KB

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