bfq-cgroup.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cgroups support for the BFQ I/O scheduler.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/cgroup.h>
  9. #include <linux/ktime.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/ioprio.h>
  12. #include <linux/sbitmap.h>
  13. #include <linux/delay.h>
  14. #include "elevator.h"
  15. #include "bfq-iosched.h"
  16. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  17. static int bfq_stat_init(struct bfq_stat *stat, gfp_t gfp)
  18. {
  19. int ret;
  20. ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp);
  21. if (ret)
  22. return ret;
  23. atomic64_set(&stat->aux_cnt, 0);
  24. return 0;
  25. }
  26. static void bfq_stat_exit(struct bfq_stat *stat)
  27. {
  28. percpu_counter_destroy(&stat->cpu_cnt);
  29. }
  30. /**
  31. * bfq_stat_add - add a value to a bfq_stat
  32. * @stat: target bfq_stat
  33. * @val: value to add
  34. *
  35. * Add @val to @stat. The caller must ensure that IRQ on the same CPU
  36. * don't re-enter this function for the same counter.
  37. */
  38. static inline void bfq_stat_add(struct bfq_stat *stat, uint64_t val)
  39. {
  40. percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH);
  41. }
  42. /**
  43. * bfq_stat_read - read the current value of a bfq_stat
  44. * @stat: bfq_stat to read
  45. */
  46. static inline uint64_t bfq_stat_read(struct bfq_stat *stat)
  47. {
  48. return percpu_counter_sum_positive(&stat->cpu_cnt);
  49. }
  50. /**
  51. * bfq_stat_reset - reset a bfq_stat
  52. * @stat: bfq_stat to reset
  53. */
  54. static inline void bfq_stat_reset(struct bfq_stat *stat)
  55. {
  56. percpu_counter_set(&stat->cpu_cnt, 0);
  57. atomic64_set(&stat->aux_cnt, 0);
  58. }
  59. /**
  60. * bfq_stat_add_aux - add a bfq_stat into another's aux count
  61. * @to: the destination bfq_stat
  62. * @from: the source
  63. *
  64. * Add @from's count including the aux one to @to's aux count.
  65. */
  66. static inline void bfq_stat_add_aux(struct bfq_stat *to,
  67. struct bfq_stat *from)
  68. {
  69. atomic64_add(bfq_stat_read(from) + atomic64_read(&from->aux_cnt),
  70. &to->aux_cnt);
  71. }
  72. /**
  73. * blkg_prfill_stat - prfill callback for bfq_stat
  74. * @sf: seq_file to print to
  75. * @pd: policy private data of interest
  76. * @off: offset to the bfq_stat in @pd
  77. *
  78. * prfill callback for printing a bfq_stat.
  79. */
  80. static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
  81. int off)
  82. {
  83. return __blkg_prfill_u64(sf, pd, bfq_stat_read((void *)pd + off));
  84. }
  85. /* bfqg stats flags */
  86. enum bfqg_stats_flags {
  87. BFQG_stats_waiting = 0,
  88. BFQG_stats_idling,
  89. BFQG_stats_empty,
  90. };
  91. #define BFQG_FLAG_FNS(name) \
  92. static void bfqg_stats_mark_##name(struct bfqg_stats *stats) \
  93. { \
  94. stats->flags |= (1 << BFQG_stats_##name); \
  95. } \
  96. static void bfqg_stats_clear_##name(struct bfqg_stats *stats) \
  97. { \
  98. stats->flags &= ~(1 << BFQG_stats_##name); \
  99. } \
  100. static int bfqg_stats_##name(struct bfqg_stats *stats) \
  101. { \
  102. return (stats->flags & (1 << BFQG_stats_##name)) != 0; \
  103. } \
  104. BFQG_FLAG_FNS(waiting)
  105. BFQG_FLAG_FNS(idling)
  106. BFQG_FLAG_FNS(empty)
  107. #undef BFQG_FLAG_FNS
  108. /* This should be called with the scheduler lock held. */
  109. static void bfqg_stats_update_group_wait_time(struct bfqg_stats *stats)
  110. {
  111. u64 now;
  112. if (!bfqg_stats_waiting(stats))
  113. return;
  114. now = ktime_get_ns();
  115. if (now > stats->start_group_wait_time)
  116. bfq_stat_add(&stats->group_wait_time,
  117. now - stats->start_group_wait_time);
  118. bfqg_stats_clear_waiting(stats);
  119. }
  120. /* This should be called with the scheduler lock held. */
  121. static void bfqg_stats_set_start_group_wait_time(struct bfq_group *bfqg,
  122. struct bfq_group *curr_bfqg)
  123. {
  124. struct bfqg_stats *stats = &bfqg->stats;
  125. if (bfqg_stats_waiting(stats))
  126. return;
  127. if (bfqg == curr_bfqg)
  128. return;
  129. stats->start_group_wait_time = ktime_get_ns();
  130. bfqg_stats_mark_waiting(stats);
  131. }
  132. /* This should be called with the scheduler lock held. */
  133. static void bfqg_stats_end_empty_time(struct bfqg_stats *stats)
  134. {
  135. u64 now;
  136. if (!bfqg_stats_empty(stats))
  137. return;
  138. now = ktime_get_ns();
  139. if (now > stats->start_empty_time)
  140. bfq_stat_add(&stats->empty_time,
  141. now - stats->start_empty_time);
  142. bfqg_stats_clear_empty(stats);
  143. }
  144. void bfqg_stats_update_dequeue(struct bfq_group *bfqg)
  145. {
  146. bfq_stat_add(&bfqg->stats.dequeue, 1);
  147. }
  148. void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg)
  149. {
  150. struct bfqg_stats *stats = &bfqg->stats;
  151. if (blkg_rwstat_total(&stats->queued))
  152. return;
  153. /*
  154. * group is already marked empty. This can happen if bfqq got new
  155. * request in parent group and moved to this group while being added
  156. * to service tree. Just ignore the event and move on.
  157. */
  158. if (bfqg_stats_empty(stats))
  159. return;
  160. stats->start_empty_time = ktime_get_ns();
  161. bfqg_stats_mark_empty(stats);
  162. }
  163. void bfqg_stats_update_idle_time(struct bfq_group *bfqg)
  164. {
  165. struct bfqg_stats *stats = &bfqg->stats;
  166. if (bfqg_stats_idling(stats)) {
  167. u64 now = ktime_get_ns();
  168. if (now > stats->start_idle_time)
  169. bfq_stat_add(&stats->idle_time,
  170. now - stats->start_idle_time);
  171. bfqg_stats_clear_idling(stats);
  172. }
  173. }
  174. void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg)
  175. {
  176. struct bfqg_stats *stats = &bfqg->stats;
  177. stats->start_idle_time = ktime_get_ns();
  178. bfqg_stats_mark_idling(stats);
  179. }
  180. void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg)
  181. {
  182. struct bfqg_stats *stats = &bfqg->stats;
  183. bfq_stat_add(&stats->avg_queue_size_sum,
  184. blkg_rwstat_total(&stats->queued));
  185. bfq_stat_add(&stats->avg_queue_size_samples, 1);
  186. bfqg_stats_update_group_wait_time(stats);
  187. }
  188. void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
  189. blk_opf_t opf)
  190. {
  191. blkg_rwstat_add(&bfqg->stats.queued, opf, 1);
  192. bfqg_stats_end_empty_time(&bfqg->stats);
  193. if (!(bfqq == ((struct bfq_data *)bfqg->bfqd)->in_service_queue))
  194. bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq));
  195. }
  196. void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf)
  197. {
  198. blkg_rwstat_add(&bfqg->stats.queued, opf, -1);
  199. }
  200. void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf)
  201. {
  202. blkg_rwstat_add(&bfqg->stats.merged, opf, 1);
  203. }
  204. void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
  205. u64 io_start_time_ns, blk_opf_t opf)
  206. {
  207. struct bfqg_stats *stats = &bfqg->stats;
  208. u64 now = ktime_get_ns();
  209. if (now > io_start_time_ns)
  210. blkg_rwstat_add(&stats->service_time, opf,
  211. now - io_start_time_ns);
  212. if (io_start_time_ns > start_time_ns)
  213. blkg_rwstat_add(&stats->wait_time, opf,
  214. io_start_time_ns - start_time_ns);
  215. }
  216. #else /* CONFIG_BFQ_CGROUP_DEBUG */
  217. void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf) { }
  218. void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf) { }
  219. void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
  220. u64 io_start_time_ns, blk_opf_t opf) { }
  221. void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { }
  222. void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { }
  223. #endif /* CONFIG_BFQ_CGROUP_DEBUG */
  224. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  225. /*
  226. * blk-cgroup policy-related handlers
  227. * The following functions help in converting between blk-cgroup
  228. * internal structures and BFQ-specific structures.
  229. */
  230. static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd)
  231. {
  232. return pd ? container_of(pd, struct bfq_group, pd) : NULL;
  233. }
  234. struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg)
  235. {
  236. return pd_to_blkg(&bfqg->pd);
  237. }
  238. static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg)
  239. {
  240. return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq));
  241. }
  242. /*
  243. * bfq_group handlers
  244. * The following functions help in navigating the bfq_group hierarchy
  245. * by allowing to find the parent of a bfq_group or the bfq_group
  246. * associated to a bfq_queue.
  247. */
  248. static struct bfq_group *bfqg_parent(struct bfq_group *bfqg)
  249. {
  250. struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent;
  251. return pblkg ? blkg_to_bfqg(pblkg) : NULL;
  252. }
  253. struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
  254. {
  255. struct bfq_entity *group_entity = bfqq->entity.parent;
  256. return group_entity ? container_of(group_entity, struct bfq_group,
  257. entity) :
  258. bfqq->bfqd->root_group;
  259. }
  260. /*
  261. * The following two functions handle get and put of a bfq_group by
  262. * wrapping the related blk-cgroup hooks.
  263. */
  264. static void bfqg_get(struct bfq_group *bfqg)
  265. {
  266. bfqg->ref++;
  267. }
  268. static void bfqg_put(struct bfq_group *bfqg)
  269. {
  270. bfqg->ref--;
  271. if (bfqg->ref == 0)
  272. kfree(bfqg);
  273. }
  274. static void bfqg_and_blkg_get(struct bfq_group *bfqg)
  275. {
  276. /* see comments in bfq_bic_update_cgroup for why refcounting bfqg */
  277. bfqg_get(bfqg);
  278. blkg_get(bfqg_to_blkg(bfqg));
  279. }
  280. void bfqg_and_blkg_put(struct bfq_group *bfqg)
  281. {
  282. blkg_put(bfqg_to_blkg(bfqg));
  283. bfqg_put(bfqg);
  284. }
  285. void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq)
  286. {
  287. struct bfq_group *bfqg = blkg_to_bfqg(rq->bio->bi_blkg);
  288. if (!bfqg)
  289. return;
  290. blkg_rwstat_add(&bfqg->stats.bytes, rq->cmd_flags, blk_rq_bytes(rq));
  291. blkg_rwstat_add(&bfqg->stats.ios, rq->cmd_flags, 1);
  292. }
  293. /* @stats = 0 */
  294. static void bfqg_stats_reset(struct bfqg_stats *stats)
  295. {
  296. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  297. /* queued stats shouldn't be cleared */
  298. blkg_rwstat_reset(&stats->merged);
  299. blkg_rwstat_reset(&stats->service_time);
  300. blkg_rwstat_reset(&stats->wait_time);
  301. bfq_stat_reset(&stats->time);
  302. bfq_stat_reset(&stats->avg_queue_size_sum);
  303. bfq_stat_reset(&stats->avg_queue_size_samples);
  304. bfq_stat_reset(&stats->dequeue);
  305. bfq_stat_reset(&stats->group_wait_time);
  306. bfq_stat_reset(&stats->idle_time);
  307. bfq_stat_reset(&stats->empty_time);
  308. #endif
  309. }
  310. /* @to += @from */
  311. static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from)
  312. {
  313. if (!to || !from)
  314. return;
  315. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  316. /* queued stats shouldn't be cleared */
  317. blkg_rwstat_add_aux(&to->merged, &from->merged);
  318. blkg_rwstat_add_aux(&to->service_time, &from->service_time);
  319. blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
  320. bfq_stat_add_aux(&from->time, &from->time);
  321. bfq_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
  322. bfq_stat_add_aux(&to->avg_queue_size_samples,
  323. &from->avg_queue_size_samples);
  324. bfq_stat_add_aux(&to->dequeue, &from->dequeue);
  325. bfq_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
  326. bfq_stat_add_aux(&to->idle_time, &from->idle_time);
  327. bfq_stat_add_aux(&to->empty_time, &from->empty_time);
  328. #endif
  329. }
  330. /*
  331. * Transfer @bfqg's stats to its parent's aux counts so that the ancestors'
  332. * recursive stats can still account for the amount used by this bfqg after
  333. * it's gone.
  334. */
  335. static void bfqg_stats_xfer_dead(struct bfq_group *bfqg)
  336. {
  337. struct bfq_group *parent;
  338. if (!bfqg) /* root_group */
  339. return;
  340. parent = bfqg_parent(bfqg);
  341. lockdep_assert_held(&bfqg_to_blkg(bfqg)->q->queue_lock);
  342. if (unlikely(!parent))
  343. return;
  344. bfqg_stats_add_aux(&parent->stats, &bfqg->stats);
  345. bfqg_stats_reset(&bfqg->stats);
  346. }
  347. void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
  348. {
  349. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  350. entity->weight = entity->new_weight;
  351. entity->orig_weight = entity->new_weight;
  352. if (bfqq) {
  353. bfqq->ioprio = bfqq->new_ioprio;
  354. bfqq->ioprio_class = bfqq->new_ioprio_class;
  355. /*
  356. * Make sure that bfqg and its associated blkg do not
  357. * disappear before entity.
  358. */
  359. bfqg_and_blkg_get(bfqg);
  360. }
  361. entity->parent = bfqg->my_entity; /* NULL for root group */
  362. entity->sched_data = &bfqg->sched_data;
  363. }
  364. static void bfqg_stats_exit(struct bfqg_stats *stats)
  365. {
  366. blkg_rwstat_exit(&stats->bytes);
  367. blkg_rwstat_exit(&stats->ios);
  368. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  369. blkg_rwstat_exit(&stats->merged);
  370. blkg_rwstat_exit(&stats->service_time);
  371. blkg_rwstat_exit(&stats->wait_time);
  372. blkg_rwstat_exit(&stats->queued);
  373. bfq_stat_exit(&stats->time);
  374. bfq_stat_exit(&stats->avg_queue_size_sum);
  375. bfq_stat_exit(&stats->avg_queue_size_samples);
  376. bfq_stat_exit(&stats->dequeue);
  377. bfq_stat_exit(&stats->group_wait_time);
  378. bfq_stat_exit(&stats->idle_time);
  379. bfq_stat_exit(&stats->empty_time);
  380. #endif
  381. }
  382. static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp)
  383. {
  384. if (blkg_rwstat_init(&stats->bytes, gfp) ||
  385. blkg_rwstat_init(&stats->ios, gfp))
  386. goto error;
  387. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  388. if (blkg_rwstat_init(&stats->merged, gfp) ||
  389. blkg_rwstat_init(&stats->service_time, gfp) ||
  390. blkg_rwstat_init(&stats->wait_time, gfp) ||
  391. blkg_rwstat_init(&stats->queued, gfp) ||
  392. bfq_stat_init(&stats->time, gfp) ||
  393. bfq_stat_init(&stats->avg_queue_size_sum, gfp) ||
  394. bfq_stat_init(&stats->avg_queue_size_samples, gfp) ||
  395. bfq_stat_init(&stats->dequeue, gfp) ||
  396. bfq_stat_init(&stats->group_wait_time, gfp) ||
  397. bfq_stat_init(&stats->idle_time, gfp) ||
  398. bfq_stat_init(&stats->empty_time, gfp))
  399. goto error;
  400. #endif
  401. return 0;
  402. error:
  403. bfqg_stats_exit(stats);
  404. return -ENOMEM;
  405. }
  406. static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd)
  407. {
  408. return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL;
  409. }
  410. static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg)
  411. {
  412. return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq));
  413. }
  414. static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp)
  415. {
  416. struct bfq_group_data *bgd;
  417. bgd = kzalloc(sizeof(*bgd), gfp);
  418. if (!bgd)
  419. return NULL;
  420. return &bgd->pd;
  421. }
  422. static void bfq_cpd_init(struct blkcg_policy_data *cpd)
  423. {
  424. struct bfq_group_data *d = cpd_to_bfqgd(cpd);
  425. d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
  426. CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL;
  427. }
  428. static void bfq_cpd_free(struct blkcg_policy_data *cpd)
  429. {
  430. kfree(cpd_to_bfqgd(cpd));
  431. }
  432. static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q,
  433. struct blkcg *blkcg)
  434. {
  435. struct bfq_group *bfqg;
  436. bfqg = kzalloc_node(sizeof(*bfqg), gfp, q->node);
  437. if (!bfqg)
  438. return NULL;
  439. if (bfqg_stats_init(&bfqg->stats, gfp)) {
  440. kfree(bfqg);
  441. return NULL;
  442. }
  443. /* see comments in bfq_bic_update_cgroup for why refcounting */
  444. bfqg_get(bfqg);
  445. return &bfqg->pd;
  446. }
  447. static void bfq_pd_init(struct blkg_policy_data *pd)
  448. {
  449. struct blkcg_gq *blkg = pd_to_blkg(pd);
  450. struct bfq_group *bfqg = blkg_to_bfqg(blkg);
  451. struct bfq_data *bfqd = blkg->q->elevator->elevator_data;
  452. struct bfq_entity *entity = &bfqg->entity;
  453. struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg);
  454. entity->orig_weight = entity->weight = entity->new_weight = d->weight;
  455. entity->my_sched_data = &bfqg->sched_data;
  456. entity->last_bfqq_created = NULL;
  457. bfqg->my_entity = entity; /*
  458. * the root_group's will be set to NULL
  459. * in bfq_init_queue()
  460. */
  461. bfqg->bfqd = bfqd;
  462. bfqg->active_entities = 0;
  463. bfqg->online = true;
  464. bfqg->rq_pos_tree = RB_ROOT;
  465. }
  466. static void bfq_pd_free(struct blkg_policy_data *pd)
  467. {
  468. struct bfq_group *bfqg = pd_to_bfqg(pd);
  469. bfqg_stats_exit(&bfqg->stats);
  470. bfqg_put(bfqg);
  471. }
  472. static void bfq_pd_reset_stats(struct blkg_policy_data *pd)
  473. {
  474. struct bfq_group *bfqg = pd_to_bfqg(pd);
  475. bfqg_stats_reset(&bfqg->stats);
  476. }
  477. static void bfq_group_set_parent(struct bfq_group *bfqg,
  478. struct bfq_group *parent)
  479. {
  480. struct bfq_entity *entity;
  481. entity = &bfqg->entity;
  482. entity->parent = parent->my_entity;
  483. entity->sched_data = &parent->sched_data;
  484. }
  485. static void bfq_link_bfqg(struct bfq_data *bfqd, struct bfq_group *bfqg)
  486. {
  487. struct bfq_group *parent;
  488. struct bfq_entity *entity;
  489. /*
  490. * Update chain of bfq_groups as we might be handling a leaf group
  491. * which, along with some of its relatives, has not been hooked yet
  492. * to the private hierarchy of BFQ.
  493. */
  494. entity = &bfqg->entity;
  495. for_each_entity(entity) {
  496. struct bfq_group *curr_bfqg = container_of(entity,
  497. struct bfq_group, entity);
  498. if (curr_bfqg != bfqd->root_group) {
  499. parent = bfqg_parent(curr_bfqg);
  500. if (!parent)
  501. parent = bfqd->root_group;
  502. bfq_group_set_parent(curr_bfqg, parent);
  503. }
  504. }
  505. }
  506. struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)
  507. {
  508. struct blkcg_gq *blkg = bio->bi_blkg;
  509. struct bfq_group *bfqg;
  510. while (blkg) {
  511. if (!blkg->online) {
  512. blkg = blkg->parent;
  513. continue;
  514. }
  515. bfqg = blkg_to_bfqg(blkg);
  516. if (bfqg->online) {
  517. bio_associate_blkg_from_css(bio, &blkg->blkcg->css);
  518. return bfqg;
  519. }
  520. blkg = blkg->parent;
  521. }
  522. bio_associate_blkg_from_css(bio,
  523. &bfqg_to_blkg(bfqd->root_group)->blkcg->css);
  524. return bfqd->root_group;
  525. }
  526. /**
  527. * bfq_bfqq_move - migrate @bfqq to @bfqg.
  528. * @bfqd: queue descriptor.
  529. * @bfqq: the queue to move.
  530. * @bfqg: the group to move to.
  531. *
  532. * Move @bfqq to @bfqg, deactivating it from its old group and reactivating
  533. * it on the new one. Avoid putting the entity on the old group idle tree.
  534. *
  535. * Must be called under the scheduler lock, to make sure that the blkg
  536. * owning @bfqg does not disappear (see comments in
  537. * bfq_bic_update_cgroup on guaranteeing the consistency of blkg
  538. * objects).
  539. */
  540. void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  541. struct bfq_group *bfqg)
  542. {
  543. struct bfq_entity *entity = &bfqq->entity;
  544. struct bfq_group *old_parent = bfqq_group(bfqq);
  545. /*
  546. * No point to move bfqq to the same group, which can happen when
  547. * root group is offlined
  548. */
  549. if (old_parent == bfqg)
  550. return;
  551. /*
  552. * oom_bfqq is not allowed to move, oom_bfqq will hold ref to root_group
  553. * until elevator exit.
  554. */
  555. if (bfqq == &bfqd->oom_bfqq)
  556. return;
  557. /*
  558. * Get extra reference to prevent bfqq from being freed in
  559. * next possible expire or deactivate.
  560. */
  561. bfqq->ref++;
  562. /* If bfqq is empty, then bfq_bfqq_expire also invokes
  563. * bfq_del_bfqq_busy, thereby removing bfqq and its entity
  564. * from data structures related to current group. Otherwise we
  565. * need to remove bfqq explicitly with bfq_deactivate_bfqq, as
  566. * we do below.
  567. */
  568. if (bfqq == bfqd->in_service_queue)
  569. bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
  570. false, BFQQE_PREEMPTED);
  571. if (bfq_bfqq_busy(bfqq))
  572. bfq_deactivate_bfqq(bfqd, bfqq, false, false);
  573. else if (entity->on_st_or_in_serv)
  574. bfq_put_idle_entity(bfq_entity_service_tree(entity), entity);
  575. bfqg_and_blkg_put(old_parent);
  576. if (entity->parent &&
  577. entity->parent->last_bfqq_created == bfqq)
  578. entity->parent->last_bfqq_created = NULL;
  579. else if (bfqd->last_bfqq_created == bfqq)
  580. bfqd->last_bfqq_created = NULL;
  581. entity->parent = bfqg->my_entity;
  582. entity->sched_data = &bfqg->sched_data;
  583. /* pin down bfqg and its associated blkg */
  584. bfqg_and_blkg_get(bfqg);
  585. if (bfq_bfqq_busy(bfqq)) {
  586. if (unlikely(!bfqd->nonrot_with_queueing))
  587. bfq_pos_tree_add_move(bfqd, bfqq);
  588. bfq_activate_bfqq(bfqd, bfqq);
  589. }
  590. if (!bfqd->in_service_queue && !bfqd->rq_in_driver)
  591. bfq_schedule_dispatch(bfqd);
  592. /* release extra ref taken above, bfqq may happen to be freed now */
  593. bfq_put_queue(bfqq);
  594. }
  595. /**
  596. * __bfq_bic_change_cgroup - move @bic to @bfqg.
  597. * @bfqd: the queue descriptor.
  598. * @bic: the bic to move.
  599. * @bfqg: the group to move to.
  600. *
  601. * Move bic to blkcg, assuming that bfqd->lock is held; which makes
  602. * sure that the reference to cgroup is valid across the call (see
  603. * comments in bfq_bic_update_cgroup on this issue)
  604. */
  605. static void *__bfq_bic_change_cgroup(struct bfq_data *bfqd,
  606. struct bfq_io_cq *bic,
  607. struct bfq_group *bfqg)
  608. {
  609. struct bfq_queue *async_bfqq = bic_to_bfqq(bic, false);
  610. struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, true);
  611. struct bfq_entity *entity;
  612. if (async_bfqq) {
  613. entity = &async_bfqq->entity;
  614. if (entity->sched_data != &bfqg->sched_data) {
  615. bic_set_bfqq(bic, NULL, false);
  616. bfq_release_process_ref(bfqd, async_bfqq);
  617. }
  618. }
  619. if (sync_bfqq) {
  620. if (!sync_bfqq->new_bfqq && !bfq_bfqq_coop(sync_bfqq)) {
  621. /* We are the only user of this bfqq, just move it */
  622. if (sync_bfqq->entity.sched_data != &bfqg->sched_data)
  623. bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
  624. } else {
  625. struct bfq_queue *bfqq;
  626. /*
  627. * The queue was merged to a different queue. Check
  628. * that the merge chain still belongs to the same
  629. * cgroup.
  630. */
  631. for (bfqq = sync_bfqq; bfqq; bfqq = bfqq->new_bfqq)
  632. if (bfqq->entity.sched_data !=
  633. &bfqg->sched_data)
  634. break;
  635. if (bfqq) {
  636. /*
  637. * Some queue changed cgroup so the merge is
  638. * not valid anymore. We cannot easily just
  639. * cancel the merge (by clearing new_bfqq) as
  640. * there may be other processes using this
  641. * queue and holding refs to all queues below
  642. * sync_bfqq->new_bfqq. Similarly if the merge
  643. * already happened, we need to detach from
  644. * bfqq now so that we cannot merge bio to a
  645. * request from the old cgroup.
  646. */
  647. bfq_put_cooperator(sync_bfqq);
  648. bic_set_bfqq(bic, NULL, true);
  649. bfq_release_process_ref(bfqd, sync_bfqq);
  650. }
  651. }
  652. }
  653. return bfqg;
  654. }
  655. void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio)
  656. {
  657. struct bfq_data *bfqd = bic_to_bfqd(bic);
  658. struct bfq_group *bfqg = bfq_bio_bfqg(bfqd, bio);
  659. uint64_t serial_nr;
  660. serial_nr = bfqg_to_blkg(bfqg)->blkcg->css.serial_nr;
  661. /*
  662. * Check whether blkcg has changed. The condition may trigger
  663. * spuriously on a newly created cic but there's no harm.
  664. */
  665. if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr))
  666. return;
  667. /*
  668. * New cgroup for this process. Make sure it is linked to bfq internal
  669. * cgroup hierarchy.
  670. */
  671. bfq_link_bfqg(bfqd, bfqg);
  672. __bfq_bic_change_cgroup(bfqd, bic, bfqg);
  673. /*
  674. * Update blkg_path for bfq_log_* functions. We cache this
  675. * path, and update it here, for the following
  676. * reasons. Operations on blkg objects in blk-cgroup are
  677. * protected with the request_queue lock, and not with the
  678. * lock that protects the instances of this scheduler
  679. * (bfqd->lock). This exposes BFQ to the following sort of
  680. * race.
  681. *
  682. * The blkg_lookup performed in bfq_get_queue, protected
  683. * through rcu, may happen to return the address of a copy of
  684. * the original blkg. If this is the case, then the
  685. * bfqg_and_blkg_get performed in bfq_get_queue, to pin down
  686. * the blkg, is useless: it does not prevent blk-cgroup code
  687. * from destroying both the original blkg and all objects
  688. * directly or indirectly referred by the copy of the
  689. * blkg.
  690. *
  691. * On the bright side, destroy operations on a blkg invoke, as
  692. * a first step, hooks of the scheduler associated with the
  693. * blkg. And these hooks are executed with bfqd->lock held for
  694. * BFQ. As a consequence, for any blkg associated with the
  695. * request queue this instance of the scheduler is attached
  696. * to, we are guaranteed that such a blkg is not destroyed, and
  697. * that all the pointers it contains are consistent, while we
  698. * are holding bfqd->lock. A blkg_lookup performed with
  699. * bfqd->lock held then returns a fully consistent blkg, which
  700. * remains consistent until this lock is held.
  701. *
  702. * Thanks to the last fact, and to the fact that: (1) bfqg has
  703. * been obtained through a blkg_lookup in the above
  704. * assignment, and (2) bfqd->lock is being held, here we can
  705. * safely use the policy data for the involved blkg (i.e., the
  706. * field bfqg->pd) to get to the blkg associated with bfqg,
  707. * and then we can safely use any field of blkg. After we
  708. * release bfqd->lock, even just getting blkg through this
  709. * bfqg may cause dangling references to be traversed, as
  710. * bfqg->pd may not exist any more.
  711. *
  712. * In view of the above facts, here we cache, in the bfqg, any
  713. * blkg data we may need for this bic, and for its associated
  714. * bfq_queue. As of now, we need to cache only the path of the
  715. * blkg, which is used in the bfq_log_* functions.
  716. *
  717. * Finally, note that bfqg itself needs to be protected from
  718. * destruction on the blkg_free of the original blkg (which
  719. * invokes bfq_pd_free). We use an additional private
  720. * refcounter for bfqg, to let it disappear only after no
  721. * bfq_queue refers to it any longer.
  722. */
  723. blkg_path(bfqg_to_blkg(bfqg), bfqg->blkg_path, sizeof(bfqg->blkg_path));
  724. bic->blkcg_serial_nr = serial_nr;
  725. }
  726. /**
  727. * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st.
  728. * @st: the service tree being flushed.
  729. */
  730. static void bfq_flush_idle_tree(struct bfq_service_tree *st)
  731. {
  732. struct bfq_entity *entity = st->first_idle;
  733. for (; entity ; entity = st->first_idle)
  734. __bfq_deactivate_entity(entity, false);
  735. }
  736. /**
  737. * bfq_reparent_leaf_entity - move leaf entity to the root_group.
  738. * @bfqd: the device data structure with the root group.
  739. * @entity: the entity to move, if entity is a leaf; or the parent entity
  740. * of an active leaf entity to move, if entity is not a leaf.
  741. * @ioprio_class: I/O priority class to reparent.
  742. */
  743. static void bfq_reparent_leaf_entity(struct bfq_data *bfqd,
  744. struct bfq_entity *entity,
  745. int ioprio_class)
  746. {
  747. struct bfq_queue *bfqq;
  748. struct bfq_entity *child_entity = entity;
  749. while (child_entity->my_sched_data) { /* leaf not reached yet */
  750. struct bfq_sched_data *child_sd = child_entity->my_sched_data;
  751. struct bfq_service_tree *child_st = child_sd->service_tree +
  752. ioprio_class;
  753. struct rb_root *child_active = &child_st->active;
  754. child_entity = bfq_entity_of(rb_first(child_active));
  755. if (!child_entity)
  756. child_entity = child_sd->in_service_entity;
  757. }
  758. bfqq = bfq_entity_to_bfqq(child_entity);
  759. bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
  760. }
  761. /**
  762. * bfq_reparent_active_queues - move to the root group all active queues.
  763. * @bfqd: the device data structure with the root group.
  764. * @bfqg: the group to move from.
  765. * @st: the service tree to start the search from.
  766. * @ioprio_class: I/O priority class to reparent.
  767. */
  768. static void bfq_reparent_active_queues(struct bfq_data *bfqd,
  769. struct bfq_group *bfqg,
  770. struct bfq_service_tree *st,
  771. int ioprio_class)
  772. {
  773. struct rb_root *active = &st->active;
  774. struct bfq_entity *entity;
  775. while ((entity = bfq_entity_of(rb_first(active))))
  776. bfq_reparent_leaf_entity(bfqd, entity, ioprio_class);
  777. if (bfqg->sched_data.in_service_entity)
  778. bfq_reparent_leaf_entity(bfqd,
  779. bfqg->sched_data.in_service_entity,
  780. ioprio_class);
  781. }
  782. /**
  783. * bfq_pd_offline - deactivate the entity associated with @pd,
  784. * and reparent its children entities.
  785. * @pd: descriptor of the policy going offline.
  786. *
  787. * blkio already grabs the queue_lock for us, so no need to use
  788. * RCU-based magic
  789. */
  790. static void bfq_pd_offline(struct blkg_policy_data *pd)
  791. {
  792. struct bfq_service_tree *st;
  793. struct bfq_group *bfqg = pd_to_bfqg(pd);
  794. struct bfq_data *bfqd = bfqg->bfqd;
  795. struct bfq_entity *entity = bfqg->my_entity;
  796. unsigned long flags;
  797. int i;
  798. spin_lock_irqsave(&bfqd->lock, flags);
  799. if (!entity) /* root group */
  800. goto put_async_queues;
  801. /*
  802. * Empty all service_trees belonging to this group before
  803. * deactivating the group itself.
  804. */
  805. for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) {
  806. st = bfqg->sched_data.service_tree + i;
  807. /*
  808. * It may happen that some queues are still active
  809. * (busy) upon group destruction (if the corresponding
  810. * processes have been forced to terminate). We move
  811. * all the leaf entities corresponding to these queues
  812. * to the root_group.
  813. * Also, it may happen that the group has an entity
  814. * in service, which is disconnected from the active
  815. * tree: it must be moved, too.
  816. * There is no need to put the sync queues, as the
  817. * scheduler has taken no reference.
  818. */
  819. bfq_reparent_active_queues(bfqd, bfqg, st, i);
  820. /*
  821. * The idle tree may still contain bfq_queues
  822. * belonging to exited task because they never
  823. * migrated to a different cgroup from the one being
  824. * destroyed now. In addition, even
  825. * bfq_reparent_active_queues() may happen to add some
  826. * entities to the idle tree. It happens if, in some
  827. * of the calls to bfq_bfqq_move() performed by
  828. * bfq_reparent_active_queues(), the queue to move is
  829. * empty and gets expired.
  830. */
  831. bfq_flush_idle_tree(st);
  832. }
  833. __bfq_deactivate_entity(entity, false);
  834. put_async_queues:
  835. bfq_put_async_queues(bfqd, bfqg);
  836. bfqg->online = false;
  837. spin_unlock_irqrestore(&bfqd->lock, flags);
  838. /*
  839. * @blkg is going offline and will be ignored by
  840. * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
  841. * that they don't get lost. If IOs complete after this point, the
  842. * stats for them will be lost. Oh well...
  843. */
  844. bfqg_stats_xfer_dead(bfqg);
  845. }
  846. void bfq_end_wr_async(struct bfq_data *bfqd)
  847. {
  848. struct blkcg_gq *blkg;
  849. list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) {
  850. struct bfq_group *bfqg = blkg_to_bfqg(blkg);
  851. bfq_end_wr_async_queues(bfqd, bfqg);
  852. }
  853. bfq_end_wr_async_queues(bfqd, bfqd->root_group);
  854. }
  855. static int bfq_io_show_weight_legacy(struct seq_file *sf, void *v)
  856. {
  857. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  858. struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
  859. unsigned int val = 0;
  860. if (bfqgd)
  861. val = bfqgd->weight;
  862. seq_printf(sf, "%u\n", val);
  863. return 0;
  864. }
  865. static u64 bfqg_prfill_weight_device(struct seq_file *sf,
  866. struct blkg_policy_data *pd, int off)
  867. {
  868. struct bfq_group *bfqg = pd_to_bfqg(pd);
  869. if (!bfqg->entity.dev_weight)
  870. return 0;
  871. return __blkg_prfill_u64(sf, pd, bfqg->entity.dev_weight);
  872. }
  873. static int bfq_io_show_weight(struct seq_file *sf, void *v)
  874. {
  875. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  876. struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
  877. seq_printf(sf, "default %u\n", bfqgd->weight);
  878. blkcg_print_blkgs(sf, blkcg, bfqg_prfill_weight_device,
  879. &blkcg_policy_bfq, 0, false);
  880. return 0;
  881. }
  882. static void bfq_group_set_weight(struct bfq_group *bfqg, u64 weight, u64 dev_weight)
  883. {
  884. weight = dev_weight ?: weight;
  885. bfqg->entity.dev_weight = dev_weight;
  886. /*
  887. * Setting the prio_changed flag of the entity
  888. * to 1 with new_weight == weight would re-set
  889. * the value of the weight to its ioprio mapping.
  890. * Set the flag only if necessary.
  891. */
  892. if ((unsigned short)weight != bfqg->entity.new_weight) {
  893. bfqg->entity.new_weight = (unsigned short)weight;
  894. /*
  895. * Make sure that the above new value has been
  896. * stored in bfqg->entity.new_weight before
  897. * setting the prio_changed flag. In fact,
  898. * this flag may be read asynchronously (in
  899. * critical sections protected by a different
  900. * lock than that held here), and finding this
  901. * flag set may cause the execution of the code
  902. * for updating parameters whose value may
  903. * depend also on bfqg->entity.new_weight (in
  904. * __bfq_entity_update_weight_prio).
  905. * This barrier makes sure that the new value
  906. * of bfqg->entity.new_weight is correctly
  907. * seen in that code.
  908. */
  909. smp_wmb();
  910. bfqg->entity.prio_changed = 1;
  911. }
  912. }
  913. static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css,
  914. struct cftype *cftype,
  915. u64 val)
  916. {
  917. struct blkcg *blkcg = css_to_blkcg(css);
  918. struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg);
  919. struct blkcg_gq *blkg;
  920. int ret = -ERANGE;
  921. if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT)
  922. return ret;
  923. ret = 0;
  924. spin_lock_irq(&blkcg->lock);
  925. bfqgd->weight = (unsigned short)val;
  926. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  927. struct bfq_group *bfqg = blkg_to_bfqg(blkg);
  928. if (bfqg)
  929. bfq_group_set_weight(bfqg, val, 0);
  930. }
  931. spin_unlock_irq(&blkcg->lock);
  932. return ret;
  933. }
  934. static ssize_t bfq_io_set_device_weight(struct kernfs_open_file *of,
  935. char *buf, size_t nbytes,
  936. loff_t off)
  937. {
  938. int ret;
  939. struct blkg_conf_ctx ctx;
  940. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  941. struct bfq_group *bfqg;
  942. u64 v;
  943. ret = blkg_conf_prep(blkcg, &blkcg_policy_bfq, buf, &ctx);
  944. if (ret)
  945. return ret;
  946. if (sscanf(ctx.body, "%llu", &v) == 1) {
  947. /* require "default" on dfl */
  948. ret = -ERANGE;
  949. if (!v)
  950. goto out;
  951. } else if (!strcmp(strim(ctx.body), "default")) {
  952. v = 0;
  953. } else {
  954. ret = -EINVAL;
  955. goto out;
  956. }
  957. bfqg = blkg_to_bfqg(ctx.blkg);
  958. ret = -ERANGE;
  959. if (!v || (v >= BFQ_MIN_WEIGHT && v <= BFQ_MAX_WEIGHT)) {
  960. bfq_group_set_weight(bfqg, bfqg->entity.weight, v);
  961. ret = 0;
  962. }
  963. out:
  964. blkg_conf_finish(&ctx);
  965. return ret ?: nbytes;
  966. }
  967. static ssize_t bfq_io_set_weight(struct kernfs_open_file *of,
  968. char *buf, size_t nbytes,
  969. loff_t off)
  970. {
  971. char *endp;
  972. int ret;
  973. u64 v;
  974. buf = strim(buf);
  975. /* "WEIGHT" or "default WEIGHT" sets the default weight */
  976. v = simple_strtoull(buf, &endp, 0);
  977. if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
  978. ret = bfq_io_set_weight_legacy(of_css(of), NULL, v);
  979. return ret ?: nbytes;
  980. }
  981. return bfq_io_set_device_weight(of, buf, nbytes, off);
  982. }
  983. static int bfqg_print_rwstat(struct seq_file *sf, void *v)
  984. {
  985. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
  986. &blkcg_policy_bfq, seq_cft(sf)->private, true);
  987. return 0;
  988. }
  989. static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf,
  990. struct blkg_policy_data *pd, int off)
  991. {
  992. struct blkg_rwstat_sample sum;
  993. blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_bfq, off, &sum);
  994. return __blkg_prfill_rwstat(sf, pd, &sum);
  995. }
  996. static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
  997. {
  998. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  999. bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq,
  1000. seq_cft(sf)->private, true);
  1001. return 0;
  1002. }
  1003. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  1004. static int bfqg_print_stat(struct seq_file *sf, void *v)
  1005. {
  1006. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
  1007. &blkcg_policy_bfq, seq_cft(sf)->private, false);
  1008. return 0;
  1009. }
  1010. static u64 bfqg_prfill_stat_recursive(struct seq_file *sf,
  1011. struct blkg_policy_data *pd, int off)
  1012. {
  1013. struct blkcg_gq *blkg = pd_to_blkg(pd);
  1014. struct blkcg_gq *pos_blkg;
  1015. struct cgroup_subsys_state *pos_css;
  1016. u64 sum = 0;
  1017. lockdep_assert_held(&blkg->q->queue_lock);
  1018. rcu_read_lock();
  1019. blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
  1020. struct bfq_stat *stat;
  1021. if (!pos_blkg->online)
  1022. continue;
  1023. stat = (void *)blkg_to_pd(pos_blkg, &blkcg_policy_bfq) + off;
  1024. sum += bfq_stat_read(stat) + atomic64_read(&stat->aux_cnt);
  1025. }
  1026. rcu_read_unlock();
  1027. return __blkg_prfill_u64(sf, pd, sum);
  1028. }
  1029. static int bfqg_print_stat_recursive(struct seq_file *sf, void *v)
  1030. {
  1031. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1032. bfqg_prfill_stat_recursive, &blkcg_policy_bfq,
  1033. seq_cft(sf)->private, false);
  1034. return 0;
  1035. }
  1036. static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
  1037. int off)
  1038. {
  1039. struct bfq_group *bfqg = blkg_to_bfqg(pd->blkg);
  1040. u64 sum = blkg_rwstat_total(&bfqg->stats.bytes);
  1041. return __blkg_prfill_u64(sf, pd, sum >> 9);
  1042. }
  1043. static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)
  1044. {
  1045. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1046. bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false);
  1047. return 0;
  1048. }
  1049. static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf,
  1050. struct blkg_policy_data *pd, int off)
  1051. {
  1052. struct blkg_rwstat_sample tmp;
  1053. blkg_rwstat_recursive_sum(pd->blkg, &blkcg_policy_bfq,
  1054. offsetof(struct bfq_group, stats.bytes), &tmp);
  1055. return __blkg_prfill_u64(sf, pd,
  1056. (tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]) >> 9);
  1057. }
  1058. static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
  1059. {
  1060. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1061. bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0,
  1062. false);
  1063. return 0;
  1064. }
  1065. static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf,
  1066. struct blkg_policy_data *pd, int off)
  1067. {
  1068. struct bfq_group *bfqg = pd_to_bfqg(pd);
  1069. u64 samples = bfq_stat_read(&bfqg->stats.avg_queue_size_samples);
  1070. u64 v = 0;
  1071. if (samples) {
  1072. v = bfq_stat_read(&bfqg->stats.avg_queue_size_sum);
  1073. v = div64_u64(v, samples);
  1074. }
  1075. __blkg_prfill_u64(sf, pd, v);
  1076. return 0;
  1077. }
  1078. /* print avg_queue_size */
  1079. static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v)
  1080. {
  1081. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1082. bfqg_prfill_avg_queue_size, &blkcg_policy_bfq,
  1083. 0, false);
  1084. return 0;
  1085. }
  1086. #endif /* CONFIG_BFQ_CGROUP_DEBUG */
  1087. struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
  1088. {
  1089. int ret;
  1090. ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq);
  1091. if (ret)
  1092. return NULL;
  1093. return blkg_to_bfqg(bfqd->queue->root_blkg);
  1094. }
  1095. struct blkcg_policy blkcg_policy_bfq = {
  1096. .dfl_cftypes = bfq_blkg_files,
  1097. .legacy_cftypes = bfq_blkcg_legacy_files,
  1098. .cpd_alloc_fn = bfq_cpd_alloc,
  1099. .cpd_init_fn = bfq_cpd_init,
  1100. .cpd_bind_fn = bfq_cpd_init,
  1101. .cpd_free_fn = bfq_cpd_free,
  1102. .pd_alloc_fn = bfq_pd_alloc,
  1103. .pd_init_fn = bfq_pd_init,
  1104. .pd_offline_fn = bfq_pd_offline,
  1105. .pd_free_fn = bfq_pd_free,
  1106. .pd_reset_stats_fn = bfq_pd_reset_stats,
  1107. };
  1108. struct cftype bfq_blkcg_legacy_files[] = {
  1109. {
  1110. .name = "bfq.weight",
  1111. .flags = CFTYPE_NOT_ON_ROOT,
  1112. .seq_show = bfq_io_show_weight_legacy,
  1113. .write_u64 = bfq_io_set_weight_legacy,
  1114. },
  1115. {
  1116. .name = "bfq.weight_device",
  1117. .flags = CFTYPE_NOT_ON_ROOT,
  1118. .seq_show = bfq_io_show_weight,
  1119. .write = bfq_io_set_weight,
  1120. },
  1121. /* statistics, covers only the tasks in the bfqg */
  1122. {
  1123. .name = "bfq.io_service_bytes",
  1124. .private = offsetof(struct bfq_group, stats.bytes),
  1125. .seq_show = bfqg_print_rwstat,
  1126. },
  1127. {
  1128. .name = "bfq.io_serviced",
  1129. .private = offsetof(struct bfq_group, stats.ios),
  1130. .seq_show = bfqg_print_rwstat,
  1131. },
  1132. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  1133. {
  1134. .name = "bfq.time",
  1135. .private = offsetof(struct bfq_group, stats.time),
  1136. .seq_show = bfqg_print_stat,
  1137. },
  1138. {
  1139. .name = "bfq.sectors",
  1140. .seq_show = bfqg_print_stat_sectors,
  1141. },
  1142. {
  1143. .name = "bfq.io_service_time",
  1144. .private = offsetof(struct bfq_group, stats.service_time),
  1145. .seq_show = bfqg_print_rwstat,
  1146. },
  1147. {
  1148. .name = "bfq.io_wait_time",
  1149. .private = offsetof(struct bfq_group, stats.wait_time),
  1150. .seq_show = bfqg_print_rwstat,
  1151. },
  1152. {
  1153. .name = "bfq.io_merged",
  1154. .private = offsetof(struct bfq_group, stats.merged),
  1155. .seq_show = bfqg_print_rwstat,
  1156. },
  1157. {
  1158. .name = "bfq.io_queued",
  1159. .private = offsetof(struct bfq_group, stats.queued),
  1160. .seq_show = bfqg_print_rwstat,
  1161. },
  1162. #endif /* CONFIG_BFQ_CGROUP_DEBUG */
  1163. /* the same statistics which cover the bfqg and its descendants */
  1164. {
  1165. .name = "bfq.io_service_bytes_recursive",
  1166. .private = offsetof(struct bfq_group, stats.bytes),
  1167. .seq_show = bfqg_print_rwstat_recursive,
  1168. },
  1169. {
  1170. .name = "bfq.io_serviced_recursive",
  1171. .private = offsetof(struct bfq_group, stats.ios),
  1172. .seq_show = bfqg_print_rwstat_recursive,
  1173. },
  1174. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  1175. {
  1176. .name = "bfq.time_recursive",
  1177. .private = offsetof(struct bfq_group, stats.time),
  1178. .seq_show = bfqg_print_stat_recursive,
  1179. },
  1180. {
  1181. .name = "bfq.sectors_recursive",
  1182. .seq_show = bfqg_print_stat_sectors_recursive,
  1183. },
  1184. {
  1185. .name = "bfq.io_service_time_recursive",
  1186. .private = offsetof(struct bfq_group, stats.service_time),
  1187. .seq_show = bfqg_print_rwstat_recursive,
  1188. },
  1189. {
  1190. .name = "bfq.io_wait_time_recursive",
  1191. .private = offsetof(struct bfq_group, stats.wait_time),
  1192. .seq_show = bfqg_print_rwstat_recursive,
  1193. },
  1194. {
  1195. .name = "bfq.io_merged_recursive",
  1196. .private = offsetof(struct bfq_group, stats.merged),
  1197. .seq_show = bfqg_print_rwstat_recursive,
  1198. },
  1199. {
  1200. .name = "bfq.io_queued_recursive",
  1201. .private = offsetof(struct bfq_group, stats.queued),
  1202. .seq_show = bfqg_print_rwstat_recursive,
  1203. },
  1204. {
  1205. .name = "bfq.avg_queue_size",
  1206. .seq_show = bfqg_print_avg_queue_size,
  1207. },
  1208. {
  1209. .name = "bfq.group_wait_time",
  1210. .private = offsetof(struct bfq_group, stats.group_wait_time),
  1211. .seq_show = bfqg_print_stat,
  1212. },
  1213. {
  1214. .name = "bfq.idle_time",
  1215. .private = offsetof(struct bfq_group, stats.idle_time),
  1216. .seq_show = bfqg_print_stat,
  1217. },
  1218. {
  1219. .name = "bfq.empty_time",
  1220. .private = offsetof(struct bfq_group, stats.empty_time),
  1221. .seq_show = bfqg_print_stat,
  1222. },
  1223. {
  1224. .name = "bfq.dequeue",
  1225. .private = offsetof(struct bfq_group, stats.dequeue),
  1226. .seq_show = bfqg_print_stat,
  1227. },
  1228. #endif /* CONFIG_BFQ_CGROUP_DEBUG */
  1229. { } /* terminate */
  1230. };
  1231. struct cftype bfq_blkg_files[] = {
  1232. {
  1233. .name = "bfq.weight",
  1234. .flags = CFTYPE_NOT_ON_ROOT,
  1235. .seq_show = bfq_io_show_weight,
  1236. .write = bfq_io_set_weight,
  1237. },
  1238. {} /* terminate */
  1239. };
  1240. #else /* CONFIG_BFQ_GROUP_IOSCHED */
  1241. void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1242. struct bfq_group *bfqg) {}
  1243. void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg)
  1244. {
  1245. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  1246. entity->weight = entity->new_weight;
  1247. entity->orig_weight = entity->new_weight;
  1248. if (bfqq) {
  1249. bfqq->ioprio = bfqq->new_ioprio;
  1250. bfqq->ioprio_class = bfqq->new_ioprio_class;
  1251. }
  1252. entity->sched_data = &bfqg->sched_data;
  1253. }
  1254. void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {}
  1255. void bfq_end_wr_async(struct bfq_data *bfqd)
  1256. {
  1257. bfq_end_wr_async_queues(bfqd, bfqd->root_group);
  1258. }
  1259. struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio)
  1260. {
  1261. return bfqd->root_group;
  1262. }
  1263. struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
  1264. {
  1265. return bfqq->bfqd->root_group;
  1266. }
  1267. void bfqg_and_blkg_put(struct bfq_group *bfqg) {}
  1268. struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node)
  1269. {
  1270. struct bfq_group *bfqg;
  1271. int i;
  1272. bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node);
  1273. if (!bfqg)
  1274. return NULL;
  1275. for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
  1276. bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
  1277. return bfqg;
  1278. }
  1279. #endif /* CONFIG_BFQ_GROUP_IOSCHED */