as-iosched.c 39 KB

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