main.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. * CFQ, or complete fairness queueing, disk scheduler.
  3. *
  4. * Based on ideas from a previously unfinished io
  5. * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
  6. *
  7. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/elevator.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/ioprio.h>
  16. #include <linux/blktrace_api.h>
  17. #include "blk.h"
  18. #include "blk-cgroup.h"
  19. /*
  20. * tunables
  21. */
  22. /* max queue in one round of service */
  23. static const int cfq_quantum = 8;
  24. static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
  25. /* maximum backwards seek, in KiB */
  26. static const int cfq_back_max = 16 * 1024;
  27. /* penalty of a backwards seek */
  28. static const int cfq_back_penalty = 2;
  29. static const int cfq_slice_sync = HZ / 10;
  30. static int cfq_slice_async = HZ / 25;
  31. static const int cfq_slice_async_rq = 2;
  32. static int cfq_slice_idle = HZ / 125;
  33. static int cfq_group_idle = HZ / 125;
  34. static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
  35. static const int cfq_hist_divisor = 4;
  36. /*
  37. * offset from end of service tree
  38. */
  39. #define CFQ_IDLE_DELAY (HZ / 5)
  40. /*
  41. * below this threshold, we consider thinktime immediate
  42. */
  43. #define CFQ_MIN_TT (2)
  44. #define CFQ_SLICE_SCALE (5)
  45. #define CFQ_HW_QUEUE_MIN (5)
  46. #define CFQ_SERVICE_SHIFT 12
  47. #define CFQQ_SEEK_THR (sector_t)(8 * 100)
  48. #define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
  49. #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
  50. #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
  51. #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
  52. #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
  53. #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
  54. static struct kmem_cache *cfq_pool;
  55. #define CFQ_PRIO_LISTS IOPRIO_BE_NR
  56. #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
  57. #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
  58. #define sample_valid(samples) ((samples) > 80)
  59. #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
  60. struct cfq_ttime {
  61. unsigned long last_end_request;
  62. unsigned long ttime_total;
  63. unsigned long ttime_samples;
  64. unsigned long ttime_mean;
  65. };
  66. /*
  67. * Most of our rbtree usage is for sorting with min extraction, so
  68. * if we cache the leftmost node we don't have to walk down the tree
  69. * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
  70. * move this into the elevator for the rq sorting as well.
  71. */
  72. struct cfq_rb_root {
  73. struct rb_root rb;
  74. struct rb_node *left;
  75. unsigned count;
  76. unsigned total_weight;
  77. u64 min_vdisktime;
  78. struct cfq_ttime ttime;
  79. };
  80. #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
  81. .ttime = {.last_end_request = jiffies,},}
  82. /*
  83. * Per process-grouping structure
  84. */
  85. struct cfq_queue {
  86. /* reference count */
  87. int ref;
  88. /* various state flags, see below */
  89. unsigned int flags;
  90. /* parent cfq_data */
  91. struct cfq_data *cfqd;
  92. /* service_tree member */
  93. struct rb_node rb_node;
  94. /* service_tree key */
  95. unsigned long rb_key;
  96. /* prio tree member */
  97. struct rb_node p_node;
  98. /* prio tree root we belong to, if any */
  99. struct rb_root *p_root;
  100. /* sorted list of pending requests */
  101. struct rb_root sort_list;
  102. /* if fifo isn't expired, next request to serve */
  103. struct request *next_rq;
  104. /* requests queued in sort_list */
  105. int queued[2];
  106. /* currently allocated requests */
  107. int allocated[2];
  108. /* fifo list of requests in sort_list */
  109. struct list_head fifo;
  110. /* time when queue got scheduled in to dispatch first request. */
  111. unsigned long dispatch_start;
  112. unsigned int allocated_slice;
  113. unsigned int slice_dispatch;
  114. /* time when first request from queue completed and slice started. */
  115. unsigned long slice_start;
  116. unsigned long slice_end;
  117. long slice_resid;
  118. /* pending priority requests */
  119. int prio_pending;
  120. /* number of requests that are on the dispatch list or inside driver */
  121. int dispatched;
  122. /* io prio of this group */
  123. unsigned short ioprio, org_ioprio;
  124. unsigned short ioprio_class;
  125. pid_t pid;
  126. u32 seek_history;
  127. sector_t last_request_pos;
  128. struct cfq_rb_root *service_tree;
  129. struct cfq_queue *new_cfqq;
  130. struct cfq_group *cfqg;
  131. /* Number of sectors dispatched from queue in single dispatch round */
  132. unsigned long nr_sectors;
  133. };
  134. /*
  135. * First index in the service_trees.
  136. * IDLE is handled separately, so it has negative index
  137. */
  138. enum wl_prio_t {
  139. BE_WORKLOAD = 0,
  140. RT_WORKLOAD = 1,
  141. IDLE_WORKLOAD = 2,
  142. CFQ_PRIO_NR,
  143. };
  144. /*
  145. * Second index in the service_trees.
  146. */
  147. enum wl_type_t {
  148. ASYNC_WORKLOAD = 0,
  149. SYNC_NOIDLE_WORKLOAD = 1,
  150. SYNC_WORKLOAD = 2
  151. };
  152. struct cfqg_stats {
  153. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  154. /* total bytes transferred */
  155. struct blkg_rwstat service_bytes;
  156. /* total IOs serviced, post merge */
  157. struct blkg_rwstat serviced;
  158. /* number of ios merged */
  159. struct blkg_rwstat merged;
  160. /* total time spent on device in ns, may not be accurate w/ queueing */
  161. struct blkg_rwstat service_time;
  162. /* total time spent waiting in scheduler queue in ns */
  163. struct blkg_rwstat wait_time;
  164. /* number of IOs queued up */
  165. struct blkg_rwstat queued;
  166. /* total sectors transferred */
  167. struct blkg_stat sectors;
  168. /* total disk time and nr sectors dispatched by this group */
  169. struct blkg_stat time;
  170. #ifdef CONFIG_DEBUG_BLK_CGROUP
  171. /* time not charged to this cgroup */
  172. struct blkg_stat unaccounted_time;
  173. /* sum of number of ios queued across all samples */
  174. struct blkg_stat avg_queue_size_sum;
  175. /* count of samples taken for average */
  176. struct blkg_stat avg_queue_size_samples;
  177. /* how many times this group has been removed from service tree */
  178. struct blkg_stat dequeue;
  179. /* total time spent waiting for it to be assigned a timeslice. */
  180. struct blkg_stat group_wait_time;
  181. /* time spent idling for this blkcg_gq */
  182. struct blkg_stat idle_time;
  183. /* total time with empty current active q with other requests queued */
  184. struct blkg_stat empty_time;
  185. /* fields after this shouldn't be cleared on stat reset */
  186. uint64_t start_group_wait_time;
  187. uint64_t start_idle_time;
  188. uint64_t start_empty_time;
  189. uint16_t flags;
  190. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  191. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  192. };
  193. /* This is per cgroup per device grouping structure */
  194. struct cfq_group {
  195. /* must be the first member */
  196. struct blkg_policy_data pd;
  197. /* group service_tree member */
  198. struct rb_node rb_node;
  199. /* group service_tree key */
  200. u64 vdisktime;
  201. unsigned int weight;
  202. unsigned int new_weight;
  203. unsigned int dev_weight;
  204. /* number of cfqq currently on this group */
  205. int nr_cfqq;
  206. /*
  207. * Per group busy queues average. Useful for workload slice calc. We
  208. * create the array for each prio class but at run time it is used
  209. * only for RT and BE class and slot for IDLE class remains unused.
  210. * This is primarily done to avoid confusion and a gcc warning.
  211. */
  212. unsigned int busy_queues_avg[CFQ_PRIO_NR];
  213. /*
  214. * rr lists of queues with requests. We maintain service trees for
  215. * RT and BE classes. These trees are subdivided in subclasses
  216. * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
  217. * class there is no subclassification and all the cfq queues go on
  218. * a single tree service_tree_idle.
  219. * Counts are embedded in the cfq_rb_root
  220. */
  221. struct cfq_rb_root service_trees[2][3];
  222. struct cfq_rb_root service_tree_idle;
  223. unsigned long saved_workload_slice;
  224. enum wl_type_t saved_workload;
  225. enum wl_prio_t saved_serving_prio;
  226. /* number of requests that are on the dispatch list or inside driver */
  227. int dispatched;
  228. struct cfq_ttime ttime;
  229. struct cfqg_stats stats;
  230. };
  231. struct cfq_io_cq {
  232. struct io_cq icq; /* must be the first member */
  233. struct cfq_queue *cfqq[2];
  234. struct cfq_ttime ttime;
  235. int ioprio; /* the current ioprio */
  236. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  237. uint64_t blkcg_id; /* the current blkcg ID */
  238. #endif
  239. };
  240. /*
  241. * Per block device queue structure
  242. */
  243. struct cfq_data {
  244. struct request_queue *queue;
  245. /* Root service tree for cfq_groups */
  246. struct cfq_rb_root grp_service_tree;
  247. struct cfq_group *root_group;
  248. /*
  249. * The priority currently being served
  250. */
  251. enum wl_prio_t serving_prio;
  252. enum wl_type_t serving_type;
  253. unsigned long workload_expires;
  254. struct cfq_group *serving_group;
  255. /*
  256. * Each priority tree is sorted by next_request position. These
  257. * trees are used when determining if two or more queues are
  258. * interleaving requests (see cfq_close_cooperator).
  259. */
  260. struct rb_root prio_trees[CFQ_PRIO_LISTS];
  261. unsigned int busy_queues;
  262. unsigned int busy_sync_queues;
  263. int rq_in_driver;
  264. int rq_in_flight[2];
  265. /*
  266. * queue-depth detection
  267. */
  268. int rq_queued;
  269. int hw_tag;
  270. /*
  271. * hw_tag can be
  272. * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
  273. * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
  274. * 0 => no NCQ
  275. */
  276. int hw_tag_est_depth;
  277. unsigned int hw_tag_samples;
  278. /*
  279. * idle window management
  280. */
  281. struct timer_list idle_slice_timer;
  282. struct work_struct unplug_work;
  283. struct cfq_queue *active_queue;
  284. struct cfq_io_cq *active_cic;
  285. /*
  286. * async queue for each priority case
  287. */
  288. struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
  289. struct cfq_queue *async_idle_cfqq;
  290. sector_t last_position;
  291. /*
  292. * tunables, see top of file
  293. */
  294. unsigned int cfq_quantum;
  295. unsigned int cfq_fifo_expire[2];
  296. unsigned int cfq_back_penalty;
  297. unsigned int cfq_back_max;
  298. unsigned int cfq_slice[2];
  299. unsigned int cfq_slice_async_rq;
  300. unsigned int cfq_slice_idle;
  301. unsigned int cfq_group_idle;
  302. unsigned int cfq_latency;
  303. unsigned int cfq_target_latency;
  304. /*
  305. * Fallback dummy cfqq for extreme OOM conditions
  306. */
  307. struct cfq_queue oom_cfqq;
  308. unsigned long last_delayed_sync;
  309. };
  310. static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
  311. static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
  312. enum wl_prio_t prio,
  313. enum wl_type_t type)
  314. {
  315. if (!cfqg)
  316. return NULL;
  317. if (prio == IDLE_WORKLOAD)
  318. return &cfqg->service_tree_idle;
  319. return &cfqg->service_trees[prio][type];
  320. }
  321. enum cfqq_state_flags {
  322. CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
  323. CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
  324. CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
  325. CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
  326. CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
  327. CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
  328. CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
  329. CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
  330. CFQ_CFQQ_FLAG_sync, /* synchronous queue */
  331. CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
  332. CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
  333. CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
  334. CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
  335. };
  336. #define CFQ_CFQQ_FNS(name) \
  337. static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
  338. { \
  339. (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
  340. } \
  341. static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
  342. { \
  343. (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
  344. } \
  345. static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
  346. { \
  347. return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
  348. }
  349. CFQ_CFQQ_FNS(on_rr);
  350. CFQ_CFQQ_FNS(wait_request);
  351. CFQ_CFQQ_FNS(must_dispatch);
  352. CFQ_CFQQ_FNS(must_alloc_slice);
  353. CFQ_CFQQ_FNS(fifo_expire);
  354. CFQ_CFQQ_FNS(idle_window);
  355. CFQ_CFQQ_FNS(prio_changed);
  356. CFQ_CFQQ_FNS(slice_new);
  357. CFQ_CFQQ_FNS(sync);
  358. CFQ_CFQQ_FNS(coop);
  359. CFQ_CFQQ_FNS(split_coop);
  360. CFQ_CFQQ_FNS(deep);
  361. CFQ_CFQQ_FNS(wait_busy);
  362. #undef CFQ_CFQQ_FNS
  363. static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
  364. {
  365. return pd ? container_of(pd, struct cfq_group, pd) : NULL;
  366. }
  367. static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
  368. {
  369. return pd_to_blkg(&cfqg->pd);
  370. }
  371. #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
  372. /* cfqg stats flags */
  373. enum cfqg_stats_flags {
  374. CFQG_stats_waiting = 0,
  375. CFQG_stats_idling,
  376. CFQG_stats_empty,
  377. };
  378. #define CFQG_FLAG_FNS(name) \
  379. static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
  380. { \
  381. stats->flags |= (1 << CFQG_stats_##name); \
  382. } \
  383. static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
  384. { \
  385. stats->flags &= ~(1 << CFQG_stats_##name); \
  386. } \
  387. static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
  388. { \
  389. return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
  390. } \
  391. CFQG_FLAG_FNS(waiting)
  392. CFQG_FLAG_FNS(idling)
  393. CFQG_FLAG_FNS(empty)
  394. #undef CFQG_FLAG_FNS
  395. /* This should be called with the queue_lock held. */
  396. static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
  397. {
  398. unsigned long long now;
  399. if (!cfqg_stats_waiting(stats))
  400. return;
  401. now = sched_clock();
  402. if (time_after64(now, stats->start_group_wait_time))
  403. blkg_stat_add(&stats->group_wait_time,
  404. now - stats->start_group_wait_time);
  405. cfqg_stats_clear_waiting(stats);
  406. }
  407. /* This should be called with the queue_lock held. */
  408. static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
  409. struct cfq_group *curr_cfqg)
  410. {
  411. struct cfqg_stats *stats = &cfqg->stats;
  412. if (cfqg_stats_waiting(stats))
  413. return;
  414. if (cfqg == curr_cfqg)
  415. return;
  416. stats->start_group_wait_time = sched_clock();
  417. cfqg_stats_mark_waiting(stats);
  418. }
  419. /* This should be called with the queue_lock held. */
  420. static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
  421. {
  422. unsigned long long now;
  423. if (!cfqg_stats_empty(stats))
  424. return;
  425. now = sched_clock();
  426. if (time_after64(now, stats->start_empty_time))
  427. blkg_stat_add(&stats->empty_time,
  428. now - stats->start_empty_time);
  429. cfqg_stats_clear_empty(stats);
  430. }
  431. static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
  432. {
  433. blkg_stat_add(&cfqg->stats.dequeue, 1);
  434. }
  435. static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
  436. {
  437. struct cfqg_stats *stats = &cfqg->stats;
  438. if (blkg_rwstat_sum(&stats->queued))
  439. return;
  440. /*
  441. * group is already marked empty. This can happen if cfqq got new
  442. * request in parent group and moved to this group while being added
  443. * to service tree. Just ignore the event and move on.
  444. */
  445. if (cfqg_stats_empty(stats))
  446. return;
  447. stats->start_empty_time = sched_clock();
  448. cfqg_stats_mark_empty(stats);
  449. }
  450. static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
  451. {
  452. struct cfqg_stats *stats = &cfqg->stats;
  453. if (cfqg_stats_idling(stats)) {
  454. unsigned long long now = sched_clock();
  455. if (time_after64(now, stats->start_idle_time))
  456. blkg_stat_add(&stats->idle_time,
  457. now - stats->start_idle_time);
  458. cfqg_stats_clear_idling(stats);
  459. }
  460. }
  461. static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
  462. {
  463. struct cfqg_stats *stats = &cfqg->stats;
  464. BUG_ON(cfqg_stats_idling(stats));
  465. stats->start_idle_time = sched_clock();
  466. cfqg_stats_mark_idling(stats);
  467. }
  468. static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
  469. {
  470. struct cfqg_stats *stats = &cfqg->stats;
  471. blkg_stat_add(&stats->avg_queue_size_sum,
  472. blkg_rwstat_sum(&stats->queued));
  473. blkg_stat_add(&stats->avg_queue_size_samples, 1);
  474. cfqg_stats_update_group_wait_time(stats);
  475. }
  476. #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  477. static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
  478. static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
  479. static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
  480. static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
  481. static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
  482. static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
  483. static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
  484. #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
  485. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  486. static struct blkcg_policy blkcg_policy_cfq;
  487. static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
  488. {
  489. return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
  490. }
  491. static inline void cfqg_get(struct cfq_group *cfqg)
  492. {
  493. return blkg_get(cfqg_to_blkg(cfqg));
  494. }
  495. static inline void cfqg_put(struct cfq_group *cfqg)
  496. {
  497. return blkg_put(cfqg_to_blkg(cfqg));
  498. }
  499. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
  500. char __pbuf[128]; \
  501. \
  502. blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
  503. blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
  504. cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
  505. __pbuf, ##args); \
  506. } while (0)
  507. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
  508. char __pbuf[128]; \
  509. \
  510. blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
  511. blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
  512. } while (0)
  513. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  514. struct cfq_group *curr_cfqg, int rw)
  515. {
  516. blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
  517. cfqg_stats_end_empty_time(&cfqg->stats);
  518. cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
  519. }
  520. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  521. unsigned long time, unsigned long unaccounted_time)
  522. {
  523. blkg_stat_add(&cfqg->stats.time, time);
  524. #ifdef CONFIG_DEBUG_BLK_CGROUP
  525. blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
  526. #endif
  527. }
  528. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
  529. {
  530. blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
  531. }
  532. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
  533. {
  534. blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
  535. }
  536. static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
  537. uint64_t bytes, int rw)
  538. {
  539. blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
  540. blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
  541. blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
  542. }
  543. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  544. uint64_t start_time, uint64_t io_start_time, int rw)
  545. {
  546. struct cfqg_stats *stats = &cfqg->stats;
  547. unsigned long long now = sched_clock();
  548. if (time_after64(now, io_start_time))
  549. blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
  550. if (time_after64(io_start_time, start_time))
  551. blkg_rwstat_add(&stats->wait_time, rw,
  552. io_start_time - start_time);
  553. }
  554. static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
  555. {
  556. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  557. struct cfqg_stats *stats = &cfqg->stats;
  558. /* queued stats shouldn't be cleared */
  559. blkg_rwstat_reset(&stats->service_bytes);
  560. blkg_rwstat_reset(&stats->serviced);
  561. blkg_rwstat_reset(&stats->merged);
  562. blkg_rwstat_reset(&stats->service_time);
  563. blkg_rwstat_reset(&stats->wait_time);
  564. blkg_stat_reset(&stats->time);
  565. #ifdef CONFIG_DEBUG_BLK_CGROUP
  566. blkg_stat_reset(&stats->unaccounted_time);
  567. blkg_stat_reset(&stats->avg_queue_size_sum);
  568. blkg_stat_reset(&stats->avg_queue_size_samples);
  569. blkg_stat_reset(&stats->dequeue);
  570. blkg_stat_reset(&stats->group_wait_time);
  571. blkg_stat_reset(&stats->idle_time);
  572. blkg_stat_reset(&stats->empty_time);
  573. #endif
  574. }
  575. #else /* CONFIG_CFQ_GROUP_IOSCHED */
  576. static inline void cfqg_get(struct cfq_group *cfqg) { }
  577. static inline void cfqg_put(struct cfq_group *cfqg) { }
  578. #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
  579. blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
  580. #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
  581. static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
  582. struct cfq_group *curr_cfqg, int rw) { }
  583. static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
  584. unsigned long time, unsigned long unaccounted_time) { }
  585. static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
  586. static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
  587. static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
  588. uint64_t bytes, int rw) { }
  589. static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
  590. uint64_t start_time, uint64_t io_start_time, int rw) { }
  591. #endif /* CONFIG_CFQ_GROUP_IOSCHED */
  592. #define cfq_log(cfqd, fmt, args...) \
  593. blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
  594. /* Traverses through cfq group service trees */
  595. #define for_each_cfqg_st(cfqg, i, j, st) \
  596. for (i = 0; i <= IDLE_WORKLOAD; i++) \
  597. for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
  598. : &cfqg->service_tree_idle; \
  599. (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
  600. (i == IDLE_WORKLOAD && j == 0); \
  601. j++, st = i < IDLE_WORKLOAD ? \
  602. &cfqg->service_trees[i][j]: NULL) \
  603. static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
  604. struct cfq_ttime *ttime, bool group_idle)
  605. {
  606. unsigned long slice;
  607. if (!sample_valid(ttime->ttime_samples))
  608. return false;
  609. if (group_idle)
  610. slice = cfqd->cfq_group_idle;
  611. else
  612. slice = cfqd->cfq_slice_idle;
  613. return ttime->ttime_mean > slice;
  614. }
  615. static inline bool iops_mode(struct cfq_data *cfqd)
  616. {
  617. /*
  618. * If we are not idling on queues and it is a NCQ drive, parallel
  619. * execution of requests is on and measuring time is not possible
  620. * in most of the cases until and unless we drive shallower queue
  621. * depths and that becomes a performance bottleneck. In such cases
  622. * switch to start providing fairness in terms of number of IOs.
  623. */
  624. if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
  625. return true;
  626. else
  627. return false;
  628. }
  629. static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
  630. {
  631. if (cfq_class_idle(cfqq))
  632. return IDLE_WORKLOAD;
  633. if (cfq_class_rt(cfqq))
  634. return RT_WORKLOAD;
  635. return BE_WORKLOAD;
  636. }
  637. static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
  638. {
  639. if (!cfq_cfqq_sync(cfqq))
  640. return ASYNC_WORKLOAD;
  641. if (!cfq_cfqq_idle_window(cfqq))
  642. return SYNC_NOIDLE_WORKLOAD;
  643. return SYNC_WORKLOAD;
  644. }
  645. static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
  646. struct cfq_data *cfqd,
  647. struct cfq_group *cfqg)
  648. {
  649. if (wl == IDLE_WORKLOAD)
  650. return cfqg->service_tree_idle.count;
  651. return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
  652. + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
  653. + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
  654. }
  655. static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
  656. struct cfq_group *cfqg)
  657. {
  658. return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
  659. + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
  660. }
  661. static void cfq_dispatch_insert(struct request_queue *, struct request *);
  662. static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
  663. struct cfq_io_cq *cic, struct bio *bio,
  664. gfp_t gfp_mask);
  665. static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
  666. {
  667. /* cic->icq is the first member, %NULL will convert to %NULL */
  668. return container_of(icq, struct cfq_io_cq, icq);
  669. }
  670. static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
  671. struct io_context *ioc)
  672. {
  673. if (ioc)
  674. return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
  675. return NULL;
  676. }
  677. static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
  678. {
  679. return cic->cfqq[is_sync];
  680. }
  681. static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
  682. bool is_sync)
  683. {
  684. cic->cfqq[is_sync] = cfqq;
  685. }
  686. static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
  687. {
  688. return cic->icq.q->elevator->elevator_data;
  689. }
  690. /*
  691. * We regard a request as SYNC, if it's either a read or has the SYNC bit
  692. * set (in which case it could also be direct WRITE).
  693. */
  694. static inline bool cfq_bio_sync(struct bio *bio)
  695. {
  696. return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
  697. }
  698. /*
  699. * scheduler run of queue, if there are requests pending and no one in the
  700. * driver that will restart queueing
  701. */
  702. static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
  703. {
  704. if (cfqd->busy_queues) {
  705. cfq_log(cfqd, "schedule dispatch");
  706. kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
  707. }
  708. }
  709. /*
  710. * Scale schedule slice based on io priority. Use the sync time slice only
  711. * if a queue is marked sync and has sync io queued. A sync queue with async
  712. * io only, should not get full sync slice length.
  713. */
  714. static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
  715. unsigned short prio)
  716. {
  717. const int base_slice = cfqd->cfq_slice[sync];
  718. WARN_ON(prio >= IOPRIO_BE_NR);
  719. return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
  720. }
  721. static inline int
  722. cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  723. {
  724. return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
  725. }
  726. static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
  727. {
  728. u64 d = delta << CFQ_SERVICE_SHIFT;
  729. d = d * CFQ_WEIGHT_DEFAULT;
  730. do_div(d, cfqg->weight);
  731. return d;
  732. }
  733. static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
  734. {
  735. s64 delta = (s64)(vdisktime - min_vdisktime);
  736. if (delta > 0)
  737. min_vdisktime = vdisktime;
  738. return min_vdisktime;
  739. }
  740. static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
  741. {
  742. s64 delta = (s64)(vdisktime - min_vdisktime);
  743. if (delta < 0)
  744. min_vdisktime = vdisktime;
  745. return min_vdisktime;
  746. }
  747. static void update_min_vdisktime(struct cfq_rb_root *st)
  748. {
  749. struct cfq_group *cfqg;
  750. if (st->left) {
  751. cfqg = rb_entry_cfqg(st->left);
  752. st->min_vdisktime = max_vdisktime(st->min_vdisktime,
  753. cfqg->vdisktime);
  754. }
  755. }
  756. /*
  757. * get averaged number of queues of RT/BE priority.
  758. * average is updated, with a formula that gives more weight to higher numbers,
  759. * to quickly follows sudden increases and decrease slowly
  760. */
  761. static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
  762. struct cfq_group *cfqg, bool rt)
  763. {
  764. unsigned min_q, max_q;
  765. unsigned mult = cfq_hist_divisor - 1;
  766. unsigned round = cfq_hist_divisor / 2;
  767. unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
  768. min_q = min(cfqg->busy_queues_avg[rt], busy);
  769. max_q = max(cfqg->busy_queues_avg[rt], busy);
  770. cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
  771. cfq_hist_divisor;
  772. return cfqg->busy_queues_avg[rt];
  773. }
  774. static inline unsigned
  775. cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
  776. {
  777. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  778. return cfqd->cfq_target_latency * cfqg->weight / st->total_weight;
  779. }
  780. static inline unsigned
  781. cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  782. {
  783. unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
  784. if (cfqd->cfq_latency) {
  785. /*
  786. * interested queues (we consider only the ones with the same
  787. * priority class in the cfq group)
  788. */
  789. unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
  790. cfq_class_rt(cfqq));
  791. unsigned sync_slice = cfqd->cfq_slice[1];
  792. unsigned expect_latency = sync_slice * iq;
  793. unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
  794. if (expect_latency > group_slice) {
  795. unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
  796. /* scale low_slice according to IO priority
  797. * and sync vs async */
  798. unsigned low_slice =
  799. min(slice, base_low_slice * slice / sync_slice);
  800. /* the adapted slice value is scaled to fit all iqs
  801. * into the target latency */
  802. slice = max(slice * group_slice / expect_latency,
  803. low_slice);
  804. }
  805. }
  806. return slice;
  807. }
  808. static inline void
  809. cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  810. {
  811. unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
  812. cfqq->slice_start = jiffies;
  813. cfqq->slice_end = jiffies + slice;
  814. cfqq->allocated_slice = slice;
  815. cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
  816. }
  817. /*
  818. * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
  819. * isn't valid until the first request from the dispatch is activated
  820. * and the slice time set.
  821. */
  822. static inline bool cfq_slice_used(struct cfq_queue *cfqq)
  823. {
  824. if (cfq_cfqq_slice_new(cfqq))
  825. return false;
  826. if (time_before(jiffies, cfqq->slice_end))
  827. return false;
  828. return true;
  829. }
  830. /*
  831. * Lifted from AS - choose which of rq1 and rq2 that is best served now.
  832. * We choose the request that is closest to the head right now. Distance
  833. * behind the head is penalized and only allowed to a certain extent.
  834. */
  835. static struct request *
  836. cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
  837. {
  838. sector_t s1, s2, d1 = 0, d2 = 0;
  839. unsigned long back_max;
  840. #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
  841. #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
  842. unsigned wrap = 0; /* bit mask: requests behind the disk head? */
  843. if (rq1 == NULL || rq1 == rq2)
  844. return rq2;
  845. if (rq2 == NULL)
  846. return rq1;
  847. if (rq_is_sync(rq1) != rq_is_sync(rq2))
  848. return rq_is_sync(rq1) ? rq1 : rq2;
  849. if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
  850. return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
  851. s1 = blk_rq_pos(rq1);
  852. s2 = blk_rq_pos(rq2);
  853. /*
  854. * by definition, 1KiB is 2 sectors
  855. */
  856. back_max = cfqd->cfq_back_max * 2;
  857. /*
  858. * Strict one way elevator _except_ in the case where we allow
  859. * short backward seeks which are biased as twice the cost of a
  860. * similar forward seek.
  861. */
  862. if (s1 >= last)
  863. d1 = s1 - last;
  864. else if (s1 + back_max >= last)
  865. d1 = (last - s1) * cfqd->cfq_back_penalty;
  866. else
  867. wrap |= CFQ_RQ1_WRAP;
  868. if (s2 >= last)
  869. d2 = s2 - last;
  870. else if (s2 + back_max >= last)
  871. d2 = (last - s2) * cfqd->cfq_back_penalty;
  872. else
  873. wrap |= CFQ_RQ2_WRAP;
  874. /* Found required data */
  875. /*
  876. * By doing switch() on the bit mask "wrap" we avoid having to
  877. * check two variables for all permutations: --> faster!
  878. */
  879. switch (wrap) {
  880. case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
  881. if (d1 < d2)
  882. return rq1;
  883. else if (d2 < d1)
  884. return rq2;
  885. else {
  886. if (s1 >= s2)
  887. return rq1;
  888. else
  889. return rq2;
  890. }
  891. case CFQ_RQ2_WRAP:
  892. return rq1;
  893. case CFQ_RQ1_WRAP:
  894. return rq2;
  895. case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
  896. default:
  897. /*
  898. * Since both rqs are wrapped,
  899. * start with the one that's further behind head
  900. * (--> only *one* back seek required),
  901. * since back seek takes more time than forward.
  902. */
  903. if (s1 <= s2)
  904. return rq1;
  905. else
  906. return rq2;
  907. }
  908. }
  909. /*
  910. * The below is leftmost cache rbtree addon
  911. */
  912. static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
  913. {
  914. /* Service tree is empty */
  915. if (!root->count)
  916. return NULL;
  917. if (!root->left)
  918. root->left = rb_first(&root->rb);
  919. if (root->left)
  920. return rb_entry(root->left, struct cfq_queue, rb_node);
  921. return NULL;
  922. }
  923. static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
  924. {
  925. if (!root->left)
  926. root->left = rb_first(&root->rb);
  927. if (root->left)
  928. return rb_entry_cfqg(root->left);
  929. return NULL;
  930. }
  931. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  932. {
  933. rb_erase(n, root);
  934. RB_CLEAR_NODE(n);
  935. }
  936. static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
  937. {
  938. if (root->left == n)
  939. root->left = NULL;
  940. rb_erase_init(n, &root->rb);
  941. --root->count;
  942. }
  943. /*
  944. * would be nice to take fifo expire time into account as well
  945. */
  946. static struct request *
  947. cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  948. struct request *last)
  949. {
  950. struct rb_node *rbnext = rb_next(&last->rb_node);
  951. struct rb_node *rbprev = rb_prev(&last->rb_node);
  952. struct request *next = NULL, *prev = NULL;
  953. BUG_ON(RB_EMPTY_NODE(&last->rb_node));
  954. if (rbprev)
  955. prev = rb_entry_rq(rbprev);
  956. if (rbnext)
  957. next = rb_entry_rq(rbnext);
  958. else {
  959. rbnext = rb_first(&cfqq->sort_list);
  960. if (rbnext && rbnext != &last->rb_node)
  961. next = rb_entry_rq(rbnext);
  962. }
  963. return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
  964. }
  965. static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
  966. struct cfq_queue *cfqq)
  967. {
  968. /*
  969. * just an approximation, should be ok.
  970. */
  971. return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
  972. cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
  973. }
  974. static inline s64
  975. cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
  976. {
  977. return cfqg->vdisktime - st->min_vdisktime;
  978. }
  979. static void
  980. __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  981. {
  982. struct rb_node **node = &st->rb.rb_node;
  983. struct rb_node *parent = NULL;
  984. struct cfq_group *__cfqg;
  985. s64 key = cfqg_key(st, cfqg);
  986. int left = 1;
  987. while (*node != NULL) {
  988. parent = *node;
  989. __cfqg = rb_entry_cfqg(parent);
  990. if (key < cfqg_key(st, __cfqg))
  991. node = &parent->rb_left;
  992. else {
  993. node = &parent->rb_right;
  994. left = 0;
  995. }
  996. }
  997. if (left)
  998. st->left = &cfqg->rb_node;
  999. rb_link_node(&cfqg->rb_node, parent, node);
  1000. rb_insert_color(&cfqg->rb_node, &st->rb);
  1001. }
  1002. static void
  1003. cfq_update_group_weight(struct cfq_group *cfqg)
  1004. {
  1005. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1006. if (cfqg->new_weight) {
  1007. cfqg->weight = cfqg->new_weight;
  1008. cfqg->new_weight = 0;
  1009. }
  1010. }
  1011. static void
  1012. cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1013. {
  1014. BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
  1015. cfq_update_group_weight(cfqg);
  1016. __cfq_group_service_tree_add(st, cfqg);
  1017. st->total_weight += cfqg->weight;
  1018. }
  1019. static void
  1020. cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1021. {
  1022. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1023. struct cfq_group *__cfqg;
  1024. struct rb_node *n;
  1025. cfqg->nr_cfqq++;
  1026. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1027. return;
  1028. /*
  1029. * Currently put the group at the end. Later implement something
  1030. * so that groups get lesser vtime based on their weights, so that
  1031. * if group does not loose all if it was not continuously backlogged.
  1032. */
  1033. n = rb_last(&st->rb);
  1034. if (n) {
  1035. __cfqg = rb_entry_cfqg(n);
  1036. cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
  1037. } else
  1038. cfqg->vdisktime = st->min_vdisktime;
  1039. cfq_group_service_tree_add(st, cfqg);
  1040. }
  1041. static void
  1042. cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
  1043. {
  1044. st->total_weight -= cfqg->weight;
  1045. if (!RB_EMPTY_NODE(&cfqg->rb_node))
  1046. cfq_rb_erase(&cfqg->rb_node, st);
  1047. }
  1048. static void
  1049. cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
  1050. {
  1051. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1052. BUG_ON(cfqg->nr_cfqq < 1);
  1053. cfqg->nr_cfqq--;
  1054. /* If there are other cfq queues under this group, don't delete it */
  1055. if (cfqg->nr_cfqq)
  1056. return;
  1057. cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
  1058. cfq_group_service_tree_del(st, cfqg);
  1059. cfqg->saved_workload_slice = 0;
  1060. cfqg_stats_update_dequeue(cfqg);
  1061. }
  1062. static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
  1063. unsigned int *unaccounted_time)
  1064. {
  1065. unsigned int slice_used;
  1066. /*
  1067. * Queue got expired before even a single request completed or
  1068. * got expired immediately after first request completion.
  1069. */
  1070. if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
  1071. /*
  1072. * Also charge the seek time incurred to the group, otherwise
  1073. * if there are mutiple queues in the group, each can dispatch
  1074. * a single request on seeky media and cause lots of seek time
  1075. * and group will never know it.
  1076. */
  1077. slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
  1078. 1);
  1079. } else {
  1080. slice_used = jiffies - cfqq->slice_start;
  1081. if (slice_used > cfqq->allocated_slice) {
  1082. *unaccounted_time = slice_used - cfqq->allocated_slice;
  1083. slice_used = cfqq->allocated_slice;
  1084. }
  1085. if (time_after(cfqq->slice_start, cfqq->dispatch_start))
  1086. *unaccounted_time += cfqq->slice_start -
  1087. cfqq->dispatch_start;
  1088. }
  1089. return slice_used;
  1090. }
  1091. static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
  1092. struct cfq_queue *cfqq)
  1093. {
  1094. struct cfq_rb_root *st = &cfqd->grp_service_tree;
  1095. unsigned int used_sl, charge, unaccounted_sl = 0;
  1096. int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
  1097. - cfqg->service_tree_idle.count;
  1098. BUG_ON(nr_sync < 0);
  1099. used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
  1100. if (iops_mode(cfqd))
  1101. charge = cfqq->slice_dispatch;
  1102. else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
  1103. charge = cfqq->allocated_slice;
  1104. /* Can't update vdisktime while group is on service tree */
  1105. cfq_group_service_tree_del(st, cfqg);
  1106. cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
  1107. /* If a new weight was requested, update now, off tree */
  1108. cfq_group_service_tree_add(st, cfqg);
  1109. /* This group is being expired. Save the context */
  1110. if (time_after(cfqd->workload_expires, jiffies)) {
  1111. cfqg->saved_workload_slice = cfqd->workload_expires
  1112. - jiffies;
  1113. cfqg->saved_workload = cfqd->serving_type;
  1114. cfqg->saved_serving_prio = cfqd->serving_prio;
  1115. } else
  1116. cfqg->saved_workload_slice = 0;
  1117. cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
  1118. st->min_vdisktime);
  1119. cfq_log_cfqq(cfqq->cfqd, cfqq,
  1120. "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
  1121. used_sl, cfqq->slice_dispatch, charge,
  1122. iops_mode(cfqd), cfqq->nr_sectors);
  1123. cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
  1124. cfqg_stats_set_start_empty_time(cfqg);
  1125. }
  1126. /**
  1127. * cfq_init_cfqg_base - initialize base part of a cfq_group
  1128. * @cfqg: cfq_group to initialize
  1129. *
  1130. * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
  1131. * is enabled or not.
  1132. */
  1133. static void cfq_init_cfqg_base(struct cfq_group *cfqg)
  1134. {
  1135. struct cfq_rb_root *st;
  1136. int i, j;
  1137. for_each_cfqg_st(cfqg, i, j, st)
  1138. *st = CFQ_RB_ROOT;
  1139. RB_CLEAR_NODE(&cfqg->rb_node);
  1140. cfqg->ttime.last_end_request = jiffies;
  1141. }
  1142. #ifdef CONFIG_CFQ_GROUP_IOSCHED
  1143. static void cfq_pd_init(struct blkcg_gq *blkg)
  1144. {
  1145. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1146. cfq_init_cfqg_base(cfqg);
  1147. cfqg->weight = blkg->blkcg->cfq_weight;
  1148. }
  1149. /*
  1150. * Search for the cfq group current task belongs to. request_queue lock must
  1151. * be held.
  1152. */
  1153. static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
  1154. struct blkcg *blkcg)
  1155. {
  1156. struct request_queue *q = cfqd->queue;
  1157. struct cfq_group *cfqg = NULL;
  1158. /* avoid lookup for the common case where there's no blkcg */
  1159. if (blkcg == &blkcg_root) {
  1160. cfqg = cfqd->root_group;
  1161. } else {
  1162. struct blkcg_gq *blkg;
  1163. blkg = blkg_lookup_create(blkcg, q);
  1164. if (!IS_ERR(blkg))
  1165. cfqg = blkg_to_cfqg(blkg);
  1166. }
  1167. return cfqg;
  1168. }
  1169. static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
  1170. {
  1171. /* Currently, all async queues are mapped to root group */
  1172. if (!cfq_cfqq_sync(cfqq))
  1173. cfqg = cfqq->cfqd->root_group;
  1174. cfqq->cfqg = cfqg;
  1175. /* cfqq reference on cfqg */
  1176. cfqg_get(cfqg);
  1177. }
  1178. static u64 cfqg_prfill_weight_device(struct seq_file *sf,
  1179. struct blkg_policy_data *pd, int off)
  1180. {
  1181. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1182. if (!cfqg->dev_weight)
  1183. return 0;
  1184. return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
  1185. }
  1186. static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
  1187. struct seq_file *sf)
  1188. {
  1189. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
  1190. cfqg_prfill_weight_device, &blkcg_policy_cfq, 0,
  1191. false);
  1192. return 0;
  1193. }
  1194. static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft,
  1195. struct seq_file *sf)
  1196. {
  1197. seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight);
  1198. return 0;
  1199. }
  1200. static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
  1201. const char *buf)
  1202. {
  1203. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1204. struct blkg_conf_ctx ctx;
  1205. struct cfq_group *cfqg;
  1206. int ret;
  1207. ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
  1208. if (ret)
  1209. return ret;
  1210. ret = -EINVAL;
  1211. cfqg = blkg_to_cfqg(ctx.blkg);
  1212. if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
  1213. cfqg->dev_weight = ctx.v;
  1214. cfqg->new_weight = cfqg->dev_weight ?: blkcg->cfq_weight;
  1215. ret = 0;
  1216. }
  1217. blkg_conf_finish(&ctx);
  1218. return ret;
  1219. }
  1220. static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
  1221. {
  1222. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1223. struct blkcg_gq *blkg;
  1224. struct hlist_node *n;
  1225. if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
  1226. return -EINVAL;
  1227. spin_lock_irq(&blkcg->lock);
  1228. blkcg->cfq_weight = (unsigned int)val;
  1229. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  1230. struct cfq_group *cfqg = blkg_to_cfqg(blkg);
  1231. if (cfqg && !cfqg->dev_weight)
  1232. cfqg->new_weight = blkcg->cfq_weight;
  1233. }
  1234. spin_unlock_irq(&blkcg->lock);
  1235. return 0;
  1236. }
  1237. static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft,
  1238. struct seq_file *sf)
  1239. {
  1240. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1241. blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq,
  1242. cft->private, false);
  1243. return 0;
  1244. }
  1245. static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
  1246. struct seq_file *sf)
  1247. {
  1248. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1249. blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq,
  1250. cft->private, true);
  1251. return 0;
  1252. }
  1253. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1254. static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
  1255. struct blkg_policy_data *pd, int off)
  1256. {
  1257. struct cfq_group *cfqg = pd_to_cfqg(pd);
  1258. u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
  1259. u64 v = 0;
  1260. if (samples) {
  1261. v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
  1262. do_div(v, samples);
  1263. }
  1264. __blkg_prfill_u64(sf, pd, v);
  1265. return 0;
  1266. }
  1267. /* print avg_queue_size */
  1268. static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
  1269. struct seq_file *sf)
  1270. {
  1271. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1272. blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size,
  1273. &blkcg_policy_cfq, 0, false);
  1274. return 0;
  1275. }
  1276. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1277. static struct cftype cfq_blkcg_files[] = {
  1278. {
  1279. .name = "weight_device",
  1280. .read_seq_string = cfqg_print_weight_device,
  1281. .write_string = cfqg_set_weight_device,
  1282. .max_write_len = 256,
  1283. },
  1284. {
  1285. .name = "weight",
  1286. .read_seq_string = cfq_print_weight,
  1287. .write_u64 = cfq_set_weight,
  1288. },
  1289. {
  1290. .name = "time",
  1291. .private = offsetof(struct cfq_group, stats.time),
  1292. .read_seq_string = cfqg_print_stat,
  1293. },
  1294. {
  1295. .name = "sectors",
  1296. .private = offsetof(struct cfq_group, stats.sectors),
  1297. .read_seq_string = cfqg_print_stat,
  1298. },
  1299. {
  1300. .name = "io_service_bytes",
  1301. .private = offsetof(struct cfq_group, stats.service_bytes),
  1302. .read_seq_string = cfqg_print_rwstat,
  1303. },
  1304. {
  1305. .name = "io_serviced",
  1306. .private = offsetof(struct cfq_group, stats.serviced),
  1307. .read_seq_string = cfqg_print_rwstat,
  1308. },
  1309. {
  1310. .name = "io_service_time",
  1311. .private = offsetof(struct cfq_group, stats.service_time),
  1312. .read_seq_string = cfqg_print_rwstat,
  1313. },
  1314. {
  1315. .name = "io_wait_time",
  1316. .private = offsetof(struct cfq_group, stats.wait_time),
  1317. .read_seq_string = cfqg_print_rwstat,
  1318. },
  1319. {
  1320. .name = "io_merged",
  1321. .private = offsetof(struct cfq_group, stats.merged),
  1322. .read_seq_string = cfqg_print_rwstat,
  1323. },
  1324. {
  1325. .name = "io_queued",
  1326. .private = offsetof(struct cfq_group, stats.queued),
  1327. .read_seq_string = cfqg_print_rwstat,
  1328. },
  1329. #ifdef CONFIG_DEBUG_BLK_CGROUP
  1330. {
  1331. .name = "avg_queue_size",
  1332. .read_seq_string = cfqg_print_avg_queue_size,
  1333. },
  1334. {
  1335. .name = "group_wait_time",
  1336. .private = offsetof(struct cfq_group, stats.group_wait_time),
  1337. .read_seq_string = cfqg_print_stat,
  1338. },
  1339. {
  1340. .name = "idle_time",
  1341. .private = offsetof(struct cfq_group, stats.idle_time),
  1342. .read_seq_string = cfqg_print_stat,
  1343. },
  1344. {
  1345. .name = "empty_time",
  1346. .private = offsetof(struct cfq_group, stats.empty_time),
  1347. .read_seq_string = cfqg_print_stat,
  1348. },
  1349. {
  1350. .name = "dequeue",
  1351. .private = offsetof(struct cfq_group, stats.dequeue),
  1352. .read_seq_string = cfqg_print_stat,
  1353. },
  1354. {
  1355. .name = "unaccounted_time",
  1356. .private = offsetof(struct cfq_group, stats.unaccounted_time),
  1357. .read_seq_string = cfqg_print_stat,
  1358. },
  1359. #endif /* CONFIG_DEBUG_BLK_CGROUP */
  1360. { } /* terminate */
  1361. };
  1362. #else /* GROUP_IOSCHED */
  1363. static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
  1364. struct blkcg *blkcg)
  1365. {
  1366. return cfqd->root_group;
  1367. }
  1368. static inline void
  1369. cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
  1370. cfqq->cfqg = cfqg;
  1371. }
  1372. #endif /* GROUP_IOSCHED */
  1373. /*
  1374. * The cfqd->service_trees holds all pending cfq_queue's that have
  1375. * requests waiting to be processed. It is sorted in the order that
  1376. * we will service the queues.
  1377. */
  1378. static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
  1379. bool add_front)