blk-cgroup.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /*
  2. * Common Block IO controller cgroup interface
  3. *
  4. * Based on ideas and code from CFQ, CFS and BFQ:
  5. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  6. *
  7. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  8. * Paolo Valente <paolo.valente@unimore.it>
  9. *
  10. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  11. * Nauman Rafique <nauman@google.com>
  12. *
  13. * For policy-specific per-blkcg data:
  14. * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
  15. * Arianna Avanzini <avanzini.arianna@gmail.com>
  16. */
  17. #include <linux/ioprio.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/module.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/err.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/slab.h>
  25. #include <linux/genhd.h>
  26. #include <linux/delay.h>
  27. #include <linux/atomic.h>
  28. #include <linux/ctype.h>
  29. #include <linux/blk-cgroup.h>
  30. #include "blk.h"
  31. #define MAX_KEY_LEN 100
  32. /*
  33. * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
  34. * blkcg_pol_register_mutex nests outside of it and synchronizes entire
  35. * policy [un]register operations including cgroup file additions /
  36. * removals. Putting cgroup file registration outside blkcg_pol_mutex
  37. * allows grabbing it from cgroup callbacks.
  38. */
  39. static DEFINE_MUTEX(blkcg_pol_register_mutex);
  40. static DEFINE_MUTEX(blkcg_pol_mutex);
  41. struct blkcg blkcg_root;
  42. EXPORT_SYMBOL_GPL(blkcg_root);
  43. struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
  44. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  45. static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */
  46. static bool blkcg_policy_enabled(struct request_queue *q,
  47. const struct blkcg_policy *pol)
  48. {
  49. return pol && test_bit(pol->plid, q->blkcg_pols);
  50. }
  51. /**
  52. * blkg_free - free a blkg
  53. * @blkg: blkg to free
  54. *
  55. * Free @blkg which may be partially allocated.
  56. */
  57. static void blkg_free(struct blkcg_gq *blkg)
  58. {
  59. int i;
  60. if (!blkg)
  61. return;
  62. for (i = 0; i < BLKCG_MAX_POLS; i++)
  63. if (blkg->pd[i])
  64. blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
  65. if (blkg->blkcg != &blkcg_root)
  66. blk_exit_rl(blkg->q, &blkg->rl);
  67. blkg_rwstat_exit(&blkg->stat_ios);
  68. blkg_rwstat_exit(&blkg->stat_bytes);
  69. kfree(blkg);
  70. }
  71. /**
  72. * blkg_alloc - allocate a blkg
  73. * @blkcg: block cgroup the new blkg is associated with
  74. * @q: request_queue the new blkg is associated with
  75. * @gfp_mask: allocation mask to use
  76. *
  77. * Allocate a new blkg assocating @blkcg and @q.
  78. */
  79. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
  80. gfp_t gfp_mask)
  81. {
  82. struct blkcg_gq *blkg;
  83. int i;
  84. /* alloc and init base part */
  85. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
  86. if (!blkg)
  87. return NULL;
  88. if (blkg_rwstat_init(&blkg->stat_bytes, gfp_mask) ||
  89. blkg_rwstat_init(&blkg->stat_ios, gfp_mask))
  90. goto err_free;
  91. blkg->q = q;
  92. INIT_LIST_HEAD(&blkg->q_node);
  93. blkg->blkcg = blkcg;
  94. atomic_set(&blkg->refcnt, 1);
  95. /* root blkg uses @q->root_rl, init rl only for !root blkgs */
  96. if (blkcg != &blkcg_root) {
  97. if (blk_init_rl(&blkg->rl, q, gfp_mask))
  98. goto err_free;
  99. blkg->rl.blkg = blkg;
  100. }
  101. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  102. struct blkcg_policy *pol = blkcg_policy[i];
  103. struct blkg_policy_data *pd;
  104. if (!blkcg_policy_enabled(q, pol))
  105. continue;
  106. /* alloc per-policy data and attach it to blkg */
  107. pd = pol->pd_alloc_fn(gfp_mask, q->node);
  108. if (!pd)
  109. goto err_free;
  110. blkg->pd[i] = pd;
  111. pd->blkg = blkg;
  112. pd->plid = i;
  113. }
  114. return blkg;
  115. err_free:
  116. blkg_free(blkg);
  117. return NULL;
  118. }
  119. struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
  120. struct request_queue *q, bool update_hint)
  121. {
  122. struct blkcg_gq *blkg;
  123. /*
  124. * Hint didn't match. Look up from the radix tree. Note that the
  125. * hint can only be updated under queue_lock as otherwise @blkg
  126. * could have already been removed from blkg_tree. The caller is
  127. * responsible for grabbing queue_lock if @update_hint.
  128. */
  129. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  130. if (blkg && blkg->q == q) {
  131. if (update_hint) {
  132. lockdep_assert_held(q->queue_lock);
  133. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  134. }
  135. return blkg;
  136. }
  137. return NULL;
  138. }
  139. EXPORT_SYMBOL_GPL(blkg_lookup_slowpath);
  140. /*
  141. * If @new_blkg is %NULL, this function tries to allocate a new one as
  142. * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
  143. */
  144. static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
  145. struct request_queue *q,
  146. struct blkcg_gq *new_blkg)
  147. {
  148. struct blkcg_gq *blkg;
  149. struct bdi_writeback_congested *wb_congested;
  150. int i, ret;
  151. WARN_ON_ONCE(!rcu_read_lock_held());
  152. lockdep_assert_held(q->queue_lock);
  153. /* blkg holds a reference to blkcg */
  154. if (!css_tryget_online(&blkcg->css)) {
  155. ret = -ENODEV;
  156. goto err_free_blkg;
  157. }
  158. wb_congested = wb_congested_get_create(q->backing_dev_info,
  159. blkcg->css.id,
  160. GFP_NOWAIT | __GFP_NOWARN);
  161. if (!wb_congested) {
  162. ret = -ENOMEM;
  163. goto err_put_css;
  164. }
  165. /* allocate */
  166. if (!new_blkg) {
  167. new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT | __GFP_NOWARN);
  168. if (unlikely(!new_blkg)) {
  169. ret = -ENOMEM;
  170. goto err_put_congested;
  171. }
  172. }
  173. blkg = new_blkg;
  174. blkg->wb_congested = wb_congested;
  175. /* link parent */
  176. if (blkcg_parent(blkcg)) {
  177. blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
  178. if (WARN_ON_ONCE(!blkg->parent)) {
  179. ret = -ENODEV;
  180. goto err_put_congested;
  181. }
  182. blkg_get(blkg->parent);
  183. }
  184. /* invoke per-policy init */
  185. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  186. struct blkcg_policy *pol = blkcg_policy[i];
  187. if (blkg->pd[i] && pol->pd_init_fn)
  188. pol->pd_init_fn(blkg->pd[i]);
  189. }
  190. /* insert */
  191. spin_lock(&blkcg->lock);
  192. ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
  193. if (likely(!ret)) {
  194. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  195. list_add(&blkg->q_node, &q->blkg_list);
  196. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  197. struct blkcg_policy *pol = blkcg_policy[i];
  198. if (blkg->pd[i] && pol->pd_online_fn)
  199. pol->pd_online_fn(blkg->pd[i]);
  200. }
  201. }
  202. blkg->online = true;
  203. spin_unlock(&blkcg->lock);
  204. if (!ret)
  205. return blkg;
  206. /* @blkg failed fully initialized, use the usual release path */
  207. blkg_put(blkg);
  208. return ERR_PTR(ret);
  209. err_put_congested:
  210. wb_congested_put(wb_congested);
  211. err_put_css:
  212. css_put(&blkcg->css);
  213. err_free_blkg:
  214. blkg_free(new_blkg);
  215. return ERR_PTR(ret);
  216. }
  217. /**
  218. * blkg_lookup_create - lookup blkg, try to create one if not there
  219. * @blkcg: blkcg of interest
  220. * @q: request_queue of interest
  221. *
  222. * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
  223. * create one. blkg creation is performed recursively from blkcg_root such
  224. * that all non-root blkg's have access to the parent blkg. This function
  225. * should be called under RCU read lock and @q->queue_lock.
  226. *
  227. * Returns pointer to the looked up or created blkg on success, ERR_PTR()
  228. * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
  229. * dead and bypassing, returns ERR_PTR(-EBUSY).
  230. */
  231. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  232. struct request_queue *q)
  233. {
  234. struct blkcg_gq *blkg;
  235. WARN_ON_ONCE(!rcu_read_lock_held());
  236. lockdep_assert_held(q->queue_lock);
  237. /*
  238. * This could be the first entry point of blkcg implementation and
  239. * we shouldn't allow anything to go through for a bypassing queue.
  240. */
  241. if (unlikely(blk_queue_bypass(q)))
  242. return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
  243. blkg = __blkg_lookup(blkcg, q, true);
  244. if (blkg)
  245. return blkg;
  246. /*
  247. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  248. * non-root blkgs have access to their parents.
  249. */
  250. while (true) {
  251. struct blkcg *pos = blkcg;
  252. struct blkcg *parent = blkcg_parent(blkcg);
  253. while (parent && !__blkg_lookup(parent, q, false)) {
  254. pos = parent;
  255. parent = blkcg_parent(parent);
  256. }
  257. blkg = blkg_create(pos, q, NULL);
  258. if (pos == blkcg || IS_ERR(blkg))
  259. return blkg;
  260. }
  261. }
  262. static void blkg_destroy(struct blkcg_gq *blkg)
  263. {
  264. struct blkcg *blkcg = blkg->blkcg;
  265. struct blkcg_gq *parent = blkg->parent;
  266. int i;
  267. lockdep_assert_held(blkg->q->queue_lock);
  268. lockdep_assert_held(&blkcg->lock);
  269. /* Something wrong if we are trying to remove same group twice */
  270. WARN_ON_ONCE(list_empty(&blkg->q_node));
  271. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  272. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  273. struct blkcg_policy *pol = blkcg_policy[i];
  274. if (blkg->pd[i] && pol->pd_offline_fn)
  275. pol->pd_offline_fn(blkg->pd[i]);
  276. }
  277. if (parent) {
  278. blkg_rwstat_add_aux(&parent->stat_bytes, &blkg->stat_bytes);
  279. blkg_rwstat_add_aux(&parent->stat_ios, &blkg->stat_ios);
  280. }
  281. blkg->online = false;
  282. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  283. list_del_init(&blkg->q_node);
  284. hlist_del_init_rcu(&blkg->blkcg_node);
  285. /*
  286. * Both setting lookup hint to and clearing it from @blkg are done
  287. * under queue_lock. If it's not pointing to @blkg now, it never
  288. * will. Hint assignment itself can race safely.
  289. */
  290. if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
  291. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  292. /*
  293. * Put the reference taken at the time of creation so that when all
  294. * queues are gone, group can be destroyed.
  295. */
  296. blkg_put(blkg);
  297. }
  298. /**
  299. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  300. * @q: request_queue of interest
  301. *
  302. * Destroy all blkgs associated with @q.
  303. */
  304. static void blkg_destroy_all(struct request_queue *q)
  305. {
  306. struct blkcg_gq *blkg, *n;
  307. lockdep_assert_held(q->queue_lock);
  308. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  309. struct blkcg *blkcg = blkg->blkcg;
  310. spin_lock(&blkcg->lock);
  311. blkg_destroy(blkg);
  312. spin_unlock(&blkcg->lock);
  313. }
  314. q->root_blkg = NULL;
  315. q->root_rl.blkg = NULL;
  316. }
  317. /*
  318. * A group is RCU protected, but having an rcu lock does not mean that one
  319. * can access all the fields of blkg and assume these are valid. For
  320. * example, don't try to follow throtl_data and request queue links.
  321. *
  322. * Having a reference to blkg under an rcu allows accesses to only values
  323. * local to groups like group stats and group rate limits.
  324. */
  325. void __blkg_release_rcu(struct rcu_head *rcu_head)
  326. {
  327. struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head);
  328. /* release the blkcg and parent blkg refs this blkg has been holding */
  329. css_put(&blkg->blkcg->css);
  330. if (blkg->parent)
  331. blkg_put(blkg->parent);
  332. wb_congested_put(blkg->wb_congested);
  333. blkg_free(blkg);
  334. }
  335. EXPORT_SYMBOL_GPL(__blkg_release_rcu);
  336. /*
  337. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  338. * because the root blkg uses @q->root_rl instead of its own rl.
  339. */
  340. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  341. struct request_queue *q)
  342. {
  343. struct list_head *ent;
  344. struct blkcg_gq *blkg;
  345. /*
  346. * Determine the current blkg list_head. The first entry is
  347. * root_rl which is off @q->blkg_list and mapped to the head.
  348. */
  349. if (rl == &q->root_rl) {
  350. ent = &q->blkg_list;
  351. /* There are no more block groups, hence no request lists */
  352. if (list_empty(ent))
  353. return NULL;
  354. } else {
  355. blkg = container_of(rl, struct blkcg_gq, rl);
  356. ent = &blkg->q_node;
  357. }
  358. /* walk to the next list_head, skip root blkcg */
  359. ent = ent->next;
  360. if (ent == &q->root_blkg->q_node)
  361. ent = ent->next;
  362. if (ent == &q->blkg_list)
  363. return NULL;
  364. blkg = container_of(ent, struct blkcg_gq, q_node);
  365. return &blkg->rl;
  366. }
  367. static int blkcg_reset_stats(struct cgroup_subsys_state *css,
  368. struct cftype *cftype, u64 val)
  369. {
  370. struct blkcg *blkcg = css_to_blkcg(css);
  371. struct blkcg_gq *blkg;
  372. int i;
  373. mutex_lock(&blkcg_pol_mutex);
  374. spin_lock_irq(&blkcg->lock);
  375. /*
  376. * Note that stat reset is racy - it doesn't synchronize against
  377. * stat updates. This is a debug feature which shouldn't exist
  378. * anyway. If you get hit by a race, retry.
  379. */
  380. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  381. blkg_rwstat_reset(&blkg->stat_bytes);
  382. blkg_rwstat_reset(&blkg->stat_ios);
  383. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  384. struct blkcg_policy *pol = blkcg_policy[i];
  385. if (blkg->pd[i] && pol->pd_reset_stats_fn)
  386. pol->pd_reset_stats_fn(blkg->pd[i]);
  387. }
  388. }
  389. spin_unlock_irq(&blkcg->lock);
  390. mutex_unlock(&blkcg_pol_mutex);
  391. return 0;
  392. }
  393. const char *blkg_dev_name(struct blkcg_gq *blkg)
  394. {
  395. /* some drivers (floppy) instantiate a queue w/o disk registered */
  396. if (blkg->q->backing_dev_info->dev)
  397. return dev_name(blkg->q->backing_dev_info->dev);
  398. return NULL;
  399. }
  400. EXPORT_SYMBOL_GPL(blkg_dev_name);
  401. /**
  402. * blkcg_print_blkgs - helper for printing per-blkg data
  403. * @sf: seq_file to print to
  404. * @blkcg: blkcg of interest
  405. * @prfill: fill function to print out a blkg
  406. * @pol: policy in question
  407. * @data: data to be passed to @prfill
  408. * @show_total: to print out sum of prfill return values or not
  409. *
  410. * This function invokes @prfill on each blkg of @blkcg if pd for the
  411. * policy specified by @pol exists. @prfill is invoked with @sf, the
  412. * policy data and @data and the matching queue lock held. If @show_total
  413. * is %true, the sum of the return values from @prfill is printed with
  414. * "Total" label at the end.
  415. *
  416. * This is to be used to construct print functions for
  417. * cftype->read_seq_string method.
  418. */
  419. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  420. u64 (*prfill)(struct seq_file *,
  421. struct blkg_policy_data *, int),
  422. const struct blkcg_policy *pol, int data,
  423. bool show_total)
  424. {
  425. struct blkcg_gq *blkg;
  426. u64 total = 0;
  427. rcu_read_lock();
  428. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  429. spin_lock_irq(blkg->q->queue_lock);
  430. if (blkcg_policy_enabled(blkg->q, pol))
  431. total += prfill(sf, blkg->pd[pol->plid], data);
  432. spin_unlock_irq(blkg->q->queue_lock);
  433. }
  434. rcu_read_unlock();
  435. if (show_total)
  436. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  437. }
  438. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  439. /**
  440. * __blkg_prfill_u64 - prfill helper for a single u64 value
  441. * @sf: seq_file to print to
  442. * @pd: policy private data of interest
  443. * @v: value to print
  444. *
  445. * Print @v to @sf for the device assocaited with @pd.
  446. */
  447. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  448. {
  449. const char *dname = blkg_dev_name(pd->blkg);
  450. if (!dname)
  451. return 0;
  452. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  453. return v;
  454. }
  455. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  456. /**
  457. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  458. * @sf: seq_file to print to
  459. * @pd: policy private data of interest
  460. * @rwstat: rwstat to print
  461. *
  462. * Print @rwstat to @sf for the device assocaited with @pd.
  463. */
  464. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  465. const struct blkg_rwstat *rwstat)
  466. {
  467. static const char *rwstr[] = {
  468. [BLKG_RWSTAT_READ] = "Read",
  469. [BLKG_RWSTAT_WRITE] = "Write",
  470. [BLKG_RWSTAT_SYNC] = "Sync",
  471. [BLKG_RWSTAT_ASYNC] = "Async",
  472. };
  473. const char *dname = blkg_dev_name(pd->blkg);
  474. u64 v;
  475. int i;
  476. if (!dname)
  477. return 0;
  478. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  479. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  480. (unsigned long long)atomic64_read(&rwstat->aux_cnt[i]));
  481. v = atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_READ]) +
  482. atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_WRITE]);
  483. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  484. return v;
  485. }
  486. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  487. /**
  488. * blkg_prfill_stat - prfill callback for blkg_stat
  489. * @sf: seq_file to print to
  490. * @pd: policy private data of interest
  491. * @off: offset to the blkg_stat in @pd
  492. *
  493. * prfill callback for printing a blkg_stat.
  494. */
  495. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  496. {
  497. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  498. }
  499. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  500. /**
  501. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  502. * @sf: seq_file to print to
  503. * @pd: policy private data of interest
  504. * @off: offset to the blkg_rwstat in @pd
  505. *
  506. * prfill callback for printing a blkg_rwstat.
  507. */
  508. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  509. int off)
  510. {
  511. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  512. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  513. }
  514. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  515. static u64 blkg_prfill_rwstat_field(struct seq_file *sf,
  516. struct blkg_policy_data *pd, int off)
  517. {
  518. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd->blkg + off);
  519. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  520. }
  521. /**
  522. * blkg_print_stat_bytes - seq_show callback for blkg->stat_bytes
  523. * @sf: seq_file to print to
  524. * @v: unused
  525. *
  526. * To be used as cftype->seq_show to print blkg->stat_bytes.
  527. * cftype->private must be set to the blkcg_policy.
  528. */
  529. int blkg_print_stat_bytes(struct seq_file *sf, void *v)
  530. {
  531. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  532. blkg_prfill_rwstat_field, (void *)seq_cft(sf)->private,
  533. offsetof(struct blkcg_gq, stat_bytes), true);
  534. return 0;
  535. }
  536. EXPORT_SYMBOL_GPL(blkg_print_stat_bytes);
  537. /**
  538. * blkg_print_stat_bytes - seq_show callback for blkg->stat_ios
  539. * @sf: seq_file to print to
  540. * @v: unused
  541. *
  542. * To be used as cftype->seq_show to print blkg->stat_ios. cftype->private
  543. * must be set to the blkcg_policy.
  544. */
  545. int blkg_print_stat_ios(struct seq_file *sf, void *v)
  546. {
  547. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  548. blkg_prfill_rwstat_field, (void *)seq_cft(sf)->private,
  549. offsetof(struct blkcg_gq, stat_ios), true);
  550. return 0;
  551. }
  552. EXPORT_SYMBOL_GPL(blkg_print_stat_ios);
  553. static u64 blkg_prfill_rwstat_field_recursive(struct seq_file *sf,
  554. struct blkg_policy_data *pd,
  555. int off)
  556. {
  557. struct blkg_rwstat rwstat = blkg_rwstat_recursive_sum(pd->blkg,
  558. NULL, off);
  559. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  560. }
  561. /**
  562. * blkg_print_stat_bytes_recursive - recursive version of blkg_print_stat_bytes
  563. * @sf: seq_file to print to
  564. * @v: unused
  565. */
  566. int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v)
  567. {
  568. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  569. blkg_prfill_rwstat_field_recursive,
  570. (void *)seq_cft(sf)->private,
  571. offsetof(struct blkcg_gq, stat_bytes), true);
  572. return 0;
  573. }
  574. EXPORT_SYMBOL_GPL(blkg_print_stat_bytes_recursive);
  575. /**
  576. * blkg_print_stat_ios_recursive - recursive version of blkg_print_stat_ios
  577. * @sf: seq_file to print to
  578. * @v: unused
  579. */
  580. int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v)
  581. {
  582. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  583. blkg_prfill_rwstat_field_recursive,
  584. (void *)seq_cft(sf)->private,
  585. offsetof(struct blkcg_gq, stat_ios), true);
  586. return 0;
  587. }
  588. EXPORT_SYMBOL_GPL(blkg_print_stat_ios_recursive);
  589. /**
  590. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  591. * @blkg: blkg of interest
  592. * @pol: blkcg_policy which contains the blkg_stat
  593. * @off: offset to the blkg_stat in blkg_policy_data or @blkg
  594. *
  595. * Collect the blkg_stat specified by @blkg, @pol and @off and all its
  596. * online descendants and their aux counts. The caller must be holding the
  597. * queue lock for online tests.
  598. *
  599. * If @pol is NULL, blkg_stat is at @off bytes into @blkg; otherwise, it is
  600. * at @off bytes into @blkg's blkg_policy_data of the policy.
  601. */
  602. u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
  603. struct blkcg_policy *pol, int off)
  604. {
  605. struct blkcg_gq *pos_blkg;
  606. struct cgroup_subsys_state *pos_css;
  607. u64 sum = 0;
  608. lockdep_assert_held(blkg->q->queue_lock);
  609. rcu_read_lock();
  610. blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
  611. struct blkg_stat *stat;
  612. if (!pos_blkg->online)
  613. continue;
  614. if (pol)
  615. stat = (void *)blkg_to_pd(pos_blkg, pol) + off;
  616. else
  617. stat = (void *)blkg + off;
  618. sum += blkg_stat_read(stat) + atomic64_read(&stat->aux_cnt);
  619. }
  620. rcu_read_unlock();
  621. return sum;
  622. }
  623. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  624. /**
  625. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  626. * @blkg: blkg of interest
  627. * @pol: blkcg_policy which contains the blkg_rwstat
  628. * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg
  629. *
  630. * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its
  631. * online descendants and their aux counts. The caller must be holding the
  632. * queue lock for online tests.
  633. *
  634. * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it
  635. * is at @off bytes into @blkg's blkg_policy_data of the policy.
  636. */
  637. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
  638. struct blkcg_policy *pol, int off)
  639. {
  640. struct blkcg_gq *pos_blkg;
  641. struct cgroup_subsys_state *pos_css;
  642. struct blkg_rwstat sum = { };
  643. int i;
  644. lockdep_assert_held(blkg->q->queue_lock);
  645. rcu_read_lock();
  646. blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
  647. struct blkg_rwstat *rwstat;
  648. if (!pos_blkg->online)
  649. continue;
  650. if (pol)
  651. rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off;
  652. else
  653. rwstat = (void *)pos_blkg + off;
  654. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  655. atomic64_add(atomic64_read(&rwstat->aux_cnt[i]) +
  656. percpu_counter_sum_positive(&rwstat->cpu_cnt[i]),
  657. &sum.aux_cnt[i]);
  658. }
  659. rcu_read_unlock();
  660. return sum;
  661. }
  662. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  663. /* Performs queue bypass and policy enabled checks then looks up blkg. */
  664. static struct blkcg_gq *blkg_lookup_check(struct blkcg *blkcg,
  665. const struct blkcg_policy *pol,
  666. struct request_queue *q)
  667. {
  668. WARN_ON_ONCE(!rcu_read_lock_held());
  669. lockdep_assert_held(q->queue_lock);
  670. if (!blkcg_policy_enabled(q, pol))
  671. return ERR_PTR(-EOPNOTSUPP);
  672. /*
  673. * This could be the first entry point of blkcg implementation and
  674. * we shouldn't allow anything to go through for a bypassing queue.
  675. */
  676. if (unlikely(blk_queue_bypass(q)))
  677. return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
  678. return __blkg_lookup(blkcg, q, true /* update_hint */);
  679. }
  680. /**
  681. * blkg_conf_prep - parse and prepare for per-blkg config update
  682. * @blkcg: target block cgroup
  683. * @pol: target policy
  684. * @input: input string
  685. * @ctx: blkg_conf_ctx to be filled
  686. *
  687. * Parse per-blkg config update from @input and initialize @ctx with the
  688. * result. @ctx->blkg points to the blkg to be updated and @ctx->body the
  689. * part of @input following MAJ:MIN. This function returns with RCU read
  690. * lock and queue lock held and must be paired with blkg_conf_finish().
  691. */
  692. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  693. char *input, struct blkg_conf_ctx *ctx)
  694. __acquires(rcu) __acquires(disk->queue->queue_lock)
  695. {
  696. struct gendisk *disk;
  697. struct request_queue *q;
  698. struct blkcg_gq *blkg;
  699. struct module *owner;
  700. unsigned int major, minor;
  701. int key_len, part, ret;
  702. char *body;
  703. if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
  704. return -EINVAL;
  705. body = input + key_len;
  706. if (!isspace(*body))
  707. return -EINVAL;
  708. body = skip_spaces(body);
  709. disk = get_gendisk(MKDEV(major, minor), &part);
  710. if (!disk)
  711. return -ENODEV;
  712. if (part) {
  713. ret = -ENODEV;
  714. goto fail;
  715. }
  716. q = disk->queue;
  717. rcu_read_lock();
  718. spin_lock_irq(q->queue_lock);
  719. blkg = blkg_lookup_check(blkcg, pol, q);
  720. if (IS_ERR(blkg)) {
  721. ret = PTR_ERR(blkg);
  722. goto fail_unlock;
  723. }
  724. if (blkg)
  725. goto success;
  726. /*
  727. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  728. * non-root blkgs have access to their parents.
  729. */
  730. while (true) {
  731. struct blkcg *pos = blkcg;
  732. struct blkcg *parent;
  733. struct blkcg_gq *new_blkg;
  734. parent = blkcg_parent(blkcg);
  735. while (parent && !__blkg_lookup(parent, q, false)) {
  736. pos = parent;
  737. parent = blkcg_parent(parent);
  738. }
  739. /* Drop locks to do new blkg allocation with GFP_KERNEL. */
  740. spin_unlock_irq(q->queue_lock);
  741. rcu_read_unlock();
  742. new_blkg = blkg_alloc(pos, q, GFP_KERNEL);
  743. if (unlikely(!new_blkg)) {
  744. ret = -ENOMEM;
  745. goto fail;
  746. }
  747. if (radix_tree_preload(GFP_KERNEL)) {
  748. blkg_free(new_blkg);
  749. ret = -ENOMEM;
  750. goto fail;
  751. }
  752. rcu_read_lock();
  753. spin_lock_irq(q->queue_lock);
  754. blkg = blkg_lookup_check(pos, pol, q);
  755. if (IS_ERR(blkg)) {
  756. ret = PTR_ERR(blkg);
  757. blkg_free(new_blkg);
  758. goto fail_preloaded;
  759. }
  760. if (blkg) {
  761. blkg_free(new_blkg);
  762. } else {
  763. blkg = blkg_create(pos, q, new_blkg);
  764. if (unlikely(IS_ERR(blkg))) {
  765. ret = PTR_ERR(blkg);
  766. goto fail_preloaded;
  767. }
  768. }
  769. radix_tree_preload_end();
  770. if (pos == blkcg)
  771. goto success;
  772. }
  773. success:
  774. ctx->disk = disk;
  775. ctx->blkg = blkg;
  776. ctx->body = body;
  777. return 0;
  778. fail_preloaded:
  779. radix_tree_preload_end();
  780. fail_unlock:
  781. spin_unlock_irq(q->queue_lock);
  782. rcu_read_unlock();
  783. fail:
  784. owner = disk->fops->owner;
  785. put_disk(disk);
  786. module_put(owner);
  787. /*
  788. * If queue was bypassing, we should retry. Do so after a
  789. * short msleep(). It isn't strictly necessary but queue
  790. * can be bypassing for some time and it's always nice to
  791. * avoid busy looping.
  792. */
  793. if (ret == -EBUSY) {
  794. msleep(10);
  795. ret = restart_syscall();
  796. }
  797. return ret;
  798. }
  799. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  800. /**
  801. * blkg_conf_finish - finish up per-blkg config update
  802. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  803. *
  804. * Finish up after per-blkg config update. This function must be paired
  805. * with blkg_conf_prep().
  806. */
  807. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  808. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  809. {
  810. struct module *owner;
  811. spin_unlock_irq(ctx->disk->queue->queue_lock);
  812. rcu_read_unlock();
  813. owner = ctx->disk->fops->owner;
  814. put_disk(ctx->disk);
  815. module_put(owner);
  816. }
  817. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  818. static int blkcg_print_stat(struct seq_file *sf, void *v)
  819. {
  820. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  821. struct blkcg_gq *blkg;
  822. rcu_read_lock();
  823. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  824. const char *dname;
  825. struct blkg_rwstat rwstat;
  826. u64 rbytes, wbytes, rios, wios;
  827. dname = blkg_dev_name(blkg);
  828. if (!dname)
  829. continue;
  830. spin_lock_irq(blkg->q->queue_lock);
  831. rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
  832. offsetof(struct blkcg_gq, stat_bytes));
  833. rbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
  834. wbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
  835. rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
  836. offsetof(struct blkcg_gq, stat_ios));
  837. rios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
  838. wios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
  839. spin_unlock_irq(blkg->q->queue_lock);
  840. if (rbytes || wbytes || rios || wios)
  841. seq_printf(sf, "%s rbytes=%llu wbytes=%llu rios=%llu wios=%llu\n",
  842. dname, rbytes, wbytes, rios, wios);
  843. }
  844. rcu_read_unlock();
  845. return 0;
  846. }
  847. static struct cftype blkcg_files[] = {
  848. {
  849. .name = "stat",
  850. .flags = CFTYPE_NOT_ON_ROOT,
  851. .seq_show = blkcg_print_stat,
  852. },
  853. { } /* terminate */
  854. };
  855. static struct cftype blkcg_legacy_files[] = {
  856. {
  857. .name = "reset_stats",
  858. .write_u64 = blkcg_reset_stats,
  859. },
  860. { } /* terminate */
  861. };
  862. /**
  863. * blkcg_css_offline - cgroup css_offline callback
  864. * @css: css of interest
  865. *
  866. * This function is called when @css is about to go away and responsible
  867. * for shooting down all blkgs associated with @css. blkgs should be
  868. * removed while holding both q and blkcg locks. As blkcg lock is nested
  869. * inside q lock, this function performs reverse double lock dancing.
  870. *
  871. * This is the blkcg counterpart of ioc_release_fn().
  872. */
  873. static void blkcg_css_offline(struct cgroup_subsys_state *css)
  874. {
  875. struct blkcg *blkcg = css_to_blkcg(css);
  876. spin_lock_irq(&blkcg->lock);
  877. while (!hlist_empty(&blkcg->blkg_list)) {
  878. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  879. struct blkcg_gq, blkcg_node);
  880. struct request_queue *q = blkg->q;
  881. if (spin_trylock(q->queue_lock)) {
  882. blkg_destroy(blkg);
  883. spin_unlock(q->queue_lock);
  884. } else {
  885. spin_unlock_irq(&blkcg->lock);
  886. cpu_relax();
  887. spin_lock_irq(&blkcg->lock);
  888. }
  889. }
  890. spin_unlock_irq(&blkcg->lock);
  891. wb_blkcg_offline(blkcg);
  892. }
  893. static void blkcg_css_free(struct cgroup_subsys_state *css)
  894. {
  895. struct blkcg *blkcg = css_to_blkcg(css);
  896. int i;
  897. mutex_lock(&blkcg_pol_mutex);
  898. list_del(&blkcg->all_blkcgs_node);
  899. for (i = 0; i < BLKCG_MAX_POLS; i++)
  900. if (blkcg->cpd[i])
  901. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  902. mutex_unlock(&blkcg_pol_mutex);
  903. kfree(blkcg);
  904. }
  905. static struct cgroup_subsys_state *
  906. blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
  907. {
  908. struct blkcg *blkcg;
  909. struct cgroup_subsys_state *ret;
  910. int i;
  911. mutex_lock(&blkcg_pol_mutex);
  912. if (!parent_css) {
  913. blkcg = &blkcg_root;
  914. } else {
  915. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  916. if (!blkcg) {
  917. ret = ERR_PTR(-ENOMEM);
  918. goto unlock;
  919. }
  920. }
  921. for (i = 0; i < BLKCG_MAX_POLS ; i++) {
  922. struct blkcg_policy *pol = blkcg_policy[i];
  923. struct blkcg_policy_data *cpd;
  924. /*
  925. * If the policy hasn't been attached yet, wait for it
  926. * to be attached before doing anything else. Otherwise,
  927. * check if the policy requires any specific per-cgroup
  928. * data: if it does, allocate and initialize it.
  929. */
  930. if (!pol || !pol->cpd_alloc_fn)
  931. continue;
  932. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  933. if (!cpd) {
  934. ret = ERR_PTR(-ENOMEM);
  935. goto free_pd_blkcg;
  936. }
  937. blkcg->cpd[i] = cpd;
  938. cpd->blkcg = blkcg;
  939. cpd->plid = i;
  940. if (pol->cpd_init_fn)
  941. pol->cpd_init_fn(cpd);
  942. }
  943. spin_lock_init(&blkcg->lock);
  944. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT | __GFP_NOWARN);
  945. INIT_HLIST_HEAD(&blkcg->blkg_list);
  946. #ifdef CONFIG_CGROUP_WRITEBACK
  947. INIT_LIST_HEAD(&blkcg->cgwb_list);
  948. #endif
  949. list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
  950. mutex_unlock(&blkcg_pol_mutex);
  951. return &blkcg->css;
  952. free_pd_blkcg:
  953. for (i--; i >= 0; i--)
  954. if (blkcg->cpd[i])
  955. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  956. if (blkcg != &blkcg_root)
  957. kfree(blkcg);
  958. unlock:
  959. mutex_unlock(&blkcg_pol_mutex);
  960. return ret;
  961. }
  962. /**
  963. * blkcg_init_queue - initialize blkcg part of request queue
  964. * @q: request_queue to initialize
  965. *
  966. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  967. * part of new request_queue @q.
  968. *
  969. * RETURNS:
  970. * 0 on success, -errno on failure.
  971. */
  972. int blkcg_init_queue(struct request_queue *q)
  973. {
  974. struct blkcg_gq *new_blkg, *blkg;
  975. bool preloaded;
  976. int ret;
  977. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  978. if (!new_blkg)
  979. return -ENOMEM;
  980. preloaded = !radix_tree_preload(GFP_KERNEL);
  981. /*
  982. * Make sure the root blkg exists and count the existing blkgs. As
  983. * @q is bypassing at this point, blkg_lookup_create() can't be
  984. * used. Open code insertion.
  985. */
  986. rcu_read_lock();
  987. spin_lock_irq(q->queue_lock);
  988. blkg = blkg_create(&blkcg_root, q, new_blkg);
  989. if (IS_ERR(blkg))
  990. goto err_unlock;
  991. q->root_blkg = blkg;
  992. q->root_rl.blkg = blkg;
  993. spin_unlock_irq(q->queue_lock);
  994. rcu_read_unlock();
  995. if (preloaded)
  996. radix_tree_preload_end();
  997. ret = blk_throtl_init(q);
  998. if (ret) {
  999. spin_lock_irq(q->queue_lock);
  1000. blkg_destroy_all(q);
  1001. spin_unlock_irq(q->queue_lock);
  1002. }
  1003. return ret;
  1004. err_unlock:
  1005. spin_unlock_irq(q->queue_lock);
  1006. rcu_read_unlock();
  1007. if (preloaded)
  1008. radix_tree_preload_end();
  1009. return PTR_ERR(blkg);
  1010. }
  1011. /**
  1012. * blkcg_drain_queue - drain blkcg part of request_queue
  1013. * @q: request_queue to drain
  1014. *
  1015. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  1016. */
  1017. void blkcg_drain_queue(struct request_queue *q)
  1018. {
  1019. lockdep_assert_held(q->queue_lock);
  1020. /*
  1021. * @q could be exiting and already have destroyed all blkgs as
  1022. * indicated by NULL root_blkg. If so, don't confuse policies.
  1023. */
  1024. if (!q->root_blkg)
  1025. return;
  1026. blk_throtl_drain(q);
  1027. }
  1028. /**
  1029. * blkcg_exit_queue - exit and release blkcg part of request_queue
  1030. * @q: request_queue being released
  1031. *
  1032. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  1033. */
  1034. void blkcg_exit_queue(struct request_queue *q)
  1035. {
  1036. spin_lock_irq(q->queue_lock);
  1037. blkg_destroy_all(q);
  1038. spin_unlock_irq(q->queue_lock);
  1039. blk_throtl_exit(q);
  1040. }
  1041. /*
  1042. * We cannot support shared io contexts, as we have no mean to support
  1043. * two tasks with the same ioc in two different groups without major rework
  1044. * of the main cic data structures. For now we allow a task to change
  1045. * its cgroup only if it's the only owner of its ioc.
  1046. */
  1047. static int blkcg_can_attach(struct cgroup_taskset *tset)
  1048. {
  1049. struct task_struct *task;
  1050. struct cgroup_subsys_state *dst_css;
  1051. struct io_context *ioc;
  1052. int ret = 0;
  1053. /* task_lock() is needed to avoid races with exit_io_context() */
  1054. cgroup_taskset_for_each(task, dst_css, tset) {
  1055. task_lock(task);
  1056. ioc = task->io_context;
  1057. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  1058. ret = -EINVAL;
  1059. task_unlock(task);
  1060. if (ret)
  1061. break;
  1062. }
  1063. return ret;
  1064. }
  1065. static void blkcg_bind(struct cgroup_subsys_state *root_css)
  1066. {
  1067. int i;
  1068. mutex_lock(&blkcg_pol_mutex);
  1069. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  1070. struct blkcg_policy *pol = blkcg_policy[i];
  1071. struct blkcg *blkcg;
  1072. if (!pol || !pol->cpd_bind_fn)
  1073. continue;
  1074. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node)
  1075. if (blkcg->cpd[pol->plid])
  1076. pol->cpd_bind_fn(blkcg->cpd[pol->plid]);
  1077. }
  1078. mutex_unlock(&blkcg_pol_mutex);
  1079. }
  1080. struct cgroup_subsys io_cgrp_subsys = {
  1081. .css_alloc = blkcg_css_alloc,
  1082. .css_offline = blkcg_css_offline,
  1083. .css_free = blkcg_css_free,
  1084. .can_attach = blkcg_can_attach,
  1085. .bind = blkcg_bind,
  1086. .dfl_cftypes = blkcg_files,
  1087. .legacy_cftypes = blkcg_legacy_files,
  1088. .legacy_name = "blkio",
  1089. #ifdef CONFIG_MEMCG
  1090. /*
  1091. * This ensures that, if available, memcg is automatically enabled
  1092. * together on the default hierarchy so that the owner cgroup can
  1093. * be retrieved from writeback pages.
  1094. */
  1095. .depends_on = 1 << memory_cgrp_id,
  1096. #endif
  1097. };
  1098. EXPORT_SYMBOL_GPL(io_cgrp_subsys);
  1099. /**
  1100. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  1101. * @q: request_queue of interest
  1102. * @pol: blkcg policy to activate
  1103. *
  1104. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  1105. * bypass mode to populate its blkgs with policy_data for @pol.
  1106. *
  1107. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  1108. * from IO path. Update of each blkg is protected by both queue and blkcg
  1109. * locks so that holding either lock and testing blkcg_policy_enabled() is
  1110. * always enough for dereferencing policy data.
  1111. *
  1112. * The caller is responsible for synchronizing [de]activations and policy
  1113. * [un]registerations. Returns 0 on success, -errno on failure.
  1114. */
  1115. int blkcg_activate_policy(struct request_queue *q,
  1116. const struct blkcg_policy *pol)
  1117. {
  1118. struct blkg_policy_data *pd_prealloc = NULL;
  1119. struct blkcg_gq *blkg;
  1120. int ret;
  1121. if (blkcg_policy_enabled(q, pol))
  1122. return 0;
  1123. if (q->mq_ops)
  1124. blk_mq_freeze_queue(q);
  1125. else
  1126. blk_queue_bypass_start(q);
  1127. pd_prealloc:
  1128. if (!pd_prealloc) {
  1129. pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node);
  1130. if (!pd_prealloc) {
  1131. ret = -ENOMEM;
  1132. goto out_bypass_end;
  1133. }
  1134. }
  1135. spin_lock_irq(q->queue_lock);
  1136. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1137. struct blkg_policy_data *pd;
  1138. if (blkg->pd[pol->plid])
  1139. continue;
  1140. pd = pol->pd_alloc_fn(GFP_NOWAIT | __GFP_NOWARN, q->node);
  1141. if (!pd)
  1142. swap(pd, pd_prealloc);
  1143. if (!pd) {
  1144. spin_unlock_irq(q->queue_lock);
  1145. goto pd_prealloc;
  1146. }
  1147. blkg->pd[pol->plid] = pd;
  1148. pd->blkg = blkg;
  1149. pd->plid = pol->plid;
  1150. if (pol->pd_init_fn)
  1151. pol->pd_init_fn(pd);
  1152. }
  1153. __set_bit(pol->plid, q->blkcg_pols);
  1154. ret = 0;
  1155. spin_unlock_irq(q->queue_lock);
  1156. out_bypass_end:
  1157. if (q->mq_ops)
  1158. blk_mq_unfreeze_queue(q);
  1159. else
  1160. blk_queue_bypass_end(q);
  1161. if (pd_prealloc)
  1162. pol->pd_free_fn(pd_prealloc);
  1163. return ret;
  1164. }
  1165. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  1166. /**
  1167. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  1168. * @q: request_queue of interest
  1169. * @pol: blkcg policy to deactivate
  1170. *
  1171. * Deactivate @pol on @q. Follows the same synchronization rules as
  1172. * blkcg_activate_policy().
  1173. */
  1174. void blkcg_deactivate_policy(struct request_queue *q,
  1175. const struct blkcg_policy *pol)
  1176. {
  1177. struct blkcg_gq *blkg;
  1178. if (!blkcg_policy_enabled(q, pol))
  1179. return;
  1180. if (q->mq_ops)
  1181. blk_mq_freeze_queue(q);
  1182. else
  1183. blk_queue_bypass_start(q);
  1184. spin_lock_irq(q->queue_lock);
  1185. __clear_bit(pol->plid, q->blkcg_pols);
  1186. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1187. if (blkg->pd[pol->plid]) {
  1188. if (pol->pd_offline_fn)
  1189. pol->pd_offline_fn(blkg->pd[pol->plid]);
  1190. pol->pd_free_fn(blkg->pd[pol->plid]);
  1191. blkg->pd[pol->plid] = NULL;
  1192. }
  1193. }
  1194. spin_unlock_irq(q->queue_lock);
  1195. if (q->mq_ops)
  1196. blk_mq_unfreeze_queue(q);
  1197. else
  1198. blk_queue_bypass_end(q);
  1199. }
  1200. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  1201. /**
  1202. * blkcg_policy_register - register a blkcg policy
  1203. * @pol: blkcg policy to register
  1204. *
  1205. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  1206. * successful registration. Returns 0 on success and -errno on failure.
  1207. */
  1208. int blkcg_policy_register(struct blkcg_policy *pol)
  1209. {
  1210. struct blkcg *blkcg;
  1211. int i, ret;
  1212. mutex_lock(&blkcg_pol_register_mutex);
  1213. mutex_lock(&blkcg_pol_mutex);
  1214. /* find an empty slot */
  1215. ret = -ENOSPC;
  1216. for (i = 0; i < BLKCG_MAX_POLS; i++)
  1217. if (!blkcg_policy[i])
  1218. break;
  1219. if (i >= BLKCG_MAX_POLS)
  1220. goto err_unlock;
  1221. /* register @pol */
  1222. pol->plid = i;
  1223. blkcg_policy[pol->plid] = pol;
  1224. /* allocate and install cpd's */
  1225. if (pol->cpd_alloc_fn) {
  1226. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1227. struct blkcg_policy_data *cpd;
  1228. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  1229. if (!cpd)
  1230. goto err_free_cpds;
  1231. blkcg->cpd[pol->plid] = cpd;
  1232. cpd->blkcg = blkcg;
  1233. cpd->plid = pol->plid;
  1234. pol->cpd_init_fn(cpd);
  1235. }
  1236. }
  1237. mutex_unlock(&blkcg_pol_mutex);
  1238. /* everything is in place, add intf files for the new policy */
  1239. if (pol->dfl_cftypes)
  1240. WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
  1241. pol->dfl_cftypes));
  1242. if (pol->legacy_cftypes)
  1243. WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
  1244. pol->legacy_cftypes));
  1245. mutex_unlock(&blkcg_pol_register_mutex);
  1246. return 0;
  1247. err_free_cpds:
  1248. if (pol->cpd_alloc_fn) {
  1249. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1250. if (blkcg->cpd[pol->plid]) {
  1251. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1252. blkcg->cpd[pol->plid] = NULL;
  1253. }
  1254. }
  1255. }
  1256. blkcg_policy[pol->plid] = NULL;
  1257. err_unlock:
  1258. mutex_unlock(&blkcg_pol_mutex);
  1259. mutex_unlock(&blkcg_pol_register_mutex);
  1260. return ret;
  1261. }
  1262. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  1263. /**
  1264. * blkcg_policy_unregister - unregister a blkcg policy
  1265. * @pol: blkcg policy to unregister
  1266. *
  1267. * Undo blkcg_policy_register(@pol). Might sleep.
  1268. */
  1269. void blkcg_policy_unregister(struct blkcg_policy *pol)
  1270. {
  1271. struct blkcg *blkcg;
  1272. mutex_lock(&blkcg_pol_register_mutex);
  1273. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  1274. goto out_unlock;
  1275. /* kill the intf files first */
  1276. if (pol->dfl_cftypes)
  1277. cgroup_rm_cftypes(pol->dfl_cftypes);
  1278. if (pol->legacy_cftypes)
  1279. cgroup_rm_cftypes(pol->legacy_cftypes);
  1280. /* remove cpds and unregister */
  1281. mutex_lock(&blkcg_pol_mutex);
  1282. if (pol->cpd_alloc_fn) {
  1283. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1284. if (blkcg->cpd[pol->plid]) {
  1285. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1286. blkcg->cpd[pol->plid] = NULL;
  1287. }
  1288. }
  1289. }
  1290. blkcg_policy[pol->plid] = NULL;
  1291. mutex_unlock(&blkcg_pol_mutex);
  1292. out_unlock:
  1293. mutex_unlock(&blkcg_pol_register_mutex);
  1294. }
  1295. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);