as-iosched.c 38 KB

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