blk-cgroup.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. #include <linux/ioprio.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/genhd.h>
  20. #include <linux/delay.h>
  21. #include <linux/atomic.h>
  22. #include "blk-cgroup.h"
  23. #include "blk.h"
  24. #define MAX_KEY_LEN 100
  25. static DEFINE_MUTEX(blkcg_pol_mutex);
  26. struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
  27. .cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
  28. EXPORT_SYMBOL_GPL(blkcg_root);
  29. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  30. static struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
  31. struct request_queue *q, bool update_hint);
  32. /**
  33. * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
  34. * @d_blkg: loop cursor pointing to the current descendant
  35. * @pos_cgrp: used for iteration
  36. * @p_blkg: target blkg to walk descendants of
  37. *
  38. * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
  39. * read locked. If called under either blkcg or queue lock, the iteration
  40. * is guaranteed to include all and only online blkgs. The caller may
  41. * update @pos_cgrp by calling cgroup_rightmost_descendant() to skip
  42. * subtree.
  43. */
  44. #define blkg_for_each_descendant_pre(d_blkg, pos_cgrp, p_blkg) \
  45. cgroup_for_each_descendant_pre((pos_cgrp), (p_blkg)->blkcg->css.cgroup) \
  46. if (((d_blkg) = __blkg_lookup(cgroup_to_blkcg(pos_cgrp), \
  47. (p_blkg)->q, false)))
  48. static bool blkcg_policy_enabled(struct request_queue *q,
  49. const struct blkcg_policy *pol)
  50. {
  51. return pol && test_bit(pol->plid, q->blkcg_pols);
  52. }
  53. /**
  54. * blkg_free - free a blkg
  55. * @blkg: blkg to free
  56. *
  57. * Free @blkg which may be partially allocated.
  58. */
  59. static void blkg_free(struct blkcg_gq *blkg)
  60. {
  61. int i;
  62. if (!blkg)
  63. return;
  64. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  65. struct blkcg_policy *pol = blkcg_policy[i];
  66. struct blkg_policy_data *pd = blkg->pd[i];
  67. if (!pd)
  68. continue;
  69. if (pol && pol->pd_exit_fn)
  70. pol->pd_exit_fn(blkg);
  71. kfree(pd);
  72. }
  73. blk_exit_rl(&blkg->rl);
  74. kfree(blkg);
  75. }
  76. /**
  77. * blkg_alloc - allocate a blkg
  78. * @blkcg: block cgroup the new blkg is associated with
  79. * @q: request_queue the new blkg is associated with
  80. * @gfp_mask: allocation mask to use
  81. *
  82. * Allocate a new blkg assocating @blkcg and @q.
  83. */
  84. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
  85. gfp_t gfp_mask)
  86. {
  87. struct blkcg_gq *blkg;
  88. int i;
  89. /* alloc and init base part */
  90. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
  91. if (!blkg)
  92. return NULL;
  93. blkg->q = q;
  94. INIT_LIST_HEAD(&blkg->q_node);
  95. blkg->blkcg = blkcg;
  96. blkg->refcnt = 1;
  97. /* root blkg uses @q->root_rl, init rl only for !root blkgs */
  98. if (blkcg != &blkcg_root) {
  99. if (blk_init_rl(&blkg->rl, q, gfp_mask))
  100. goto err_free;
  101. blkg->rl.blkg = blkg;
  102. }
  103. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  104. struct blkcg_policy *pol = blkcg_policy[i];
  105. struct blkg_policy_data *pd;
  106. if (!blkcg_policy_enabled(q, pol))
  107. continue;
  108. /* alloc per-policy data and attach it to blkg */
  109. pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
  110. if (!pd)
  111. goto err_free;
  112. blkg->pd[i] = pd;
  113. pd->blkg = blkg;
  114. pd->plid = i;
  115. /* invoke per-policy init */
  116. if (pol->pd_init_fn)
  117. pol->pd_init_fn(blkg);
  118. }
  119. return blkg;
  120. err_free:
  121. blkg_free(blkg);
  122. return NULL;
  123. }
  124. /**
  125. * __blkg_lookup - internal version of blkg_lookup()
  126. * @blkcg: blkcg of interest
  127. * @q: request_queue of interest
  128. * @update_hint: whether to update lookup hint with the result or not
  129. *
  130. * This is internal version and shouldn't be used by policy
  131. * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
  132. * @q's bypass state. If @update_hint is %true, the caller should be
  133. * holding @q->queue_lock and lookup hint is updated on success.
  134. */
  135. static struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
  136. struct request_queue *q, bool update_hint)
  137. {
  138. struct blkcg_gq *blkg;
  139. blkg = rcu_dereference(blkcg->blkg_hint);
  140. if (blkg && blkg->q == q)
  141. return blkg;
  142. /*
  143. * Hint didn't match. Look up from the radix tree. Note that the
  144. * hint can only be updated under queue_lock as otherwise @blkg
  145. * could have already been removed from blkg_tree. The caller is
  146. * responsible for grabbing queue_lock if @update_hint.
  147. */
  148. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  149. if (blkg && blkg->q == q) {
  150. if (update_hint) {
  151. lockdep_assert_held(q->queue_lock);
  152. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  153. }
  154. return blkg;
  155. }
  156. return NULL;
  157. }
  158. /**
  159. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  160. * @blkcg: blkcg of interest
  161. * @q: request_queue of interest
  162. *
  163. * Lookup blkg for the @blkcg - @q pair. This function should be called
  164. * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
  165. * - see blk_queue_bypass_start() for details.
  166. */
  167. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
  168. {
  169. WARN_ON_ONCE(!rcu_read_lock_held());
  170. if (unlikely(blk_queue_bypass(q)))
  171. return NULL;
  172. return __blkg_lookup(blkcg, q, false);
  173. }
  174. EXPORT_SYMBOL_GPL(blkg_lookup);
  175. /*
  176. * If @new_blkg is %NULL, this function tries to allocate a new one as
  177. * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
  178. */
  179. static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
  180. struct request_queue *q,
  181. struct blkcg_gq *new_blkg)
  182. {
  183. struct blkcg_gq *blkg;
  184. int i, ret;
  185. WARN_ON_ONCE(!rcu_read_lock_held());
  186. lockdep_assert_held(q->queue_lock);
  187. /* blkg holds a reference to blkcg */
  188. if (!css_tryget(&blkcg->css)) {
  189. ret = -EINVAL;
  190. goto err_free_blkg;
  191. }
  192. /* allocate */
  193. if (!new_blkg) {
  194. new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
  195. if (unlikely(!new_blkg)) {
  196. ret = -ENOMEM;
  197. goto err_put_css;
  198. }
  199. }
  200. blkg = new_blkg;
  201. /* link parent and insert */
  202. if (blkcg_parent(blkcg)) {
  203. blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
  204. if (WARN_ON_ONCE(!blkg->parent)) {
  205. blkg = ERR_PTR(-EINVAL);
  206. goto err_put_css;
  207. }
  208. blkg_get(blkg->parent);
  209. }
  210. spin_lock(&blkcg->lock);
  211. ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
  212. if (likely(!ret)) {
  213. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  214. list_add(&blkg->q_node, &q->blkg_list);
  215. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  216. struct blkcg_policy *pol = blkcg_policy[i];
  217. if (blkg->pd[i] && pol->pd_online_fn)
  218. pol->pd_online_fn(blkg);
  219. }
  220. }
  221. blkg->online = true;
  222. spin_unlock(&blkcg->lock);
  223. if (!ret)
  224. return blkg;
  225. /* @blkg failed fully initialized, use the usual release path */
  226. blkg_put(blkg);
  227. return ERR_PTR(ret);
  228. err_put_css:
  229. css_put(&blkcg->css);
  230. err_free_blkg:
  231. blkg_free(new_blkg);
  232. return ERR_PTR(ret);
  233. }
  234. /**
  235. * blkg_lookup_create - lookup blkg, try to create one if not there
  236. * @blkcg: blkcg of interest
  237. * @q: request_queue of interest
  238. *
  239. * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
  240. * create one. blkg creation is performed recursively from blkcg_root such
  241. * that all non-root blkg's have access to the parent blkg. This function
  242. * should be called under RCU read lock and @q->queue_lock.
  243. *
  244. * Returns pointer to the looked up or created blkg on success, ERR_PTR()
  245. * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
  246. * dead and bypassing, returns ERR_PTR(-EBUSY).
  247. */
  248. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  249. struct request_queue *q)
  250. {
  251. struct blkcg_gq *blkg;
  252. WARN_ON_ONCE(!rcu_read_lock_held());
  253. lockdep_assert_held(q->queue_lock);
  254. /*
  255. * This could be the first entry point of blkcg implementation and
  256. * we shouldn't allow anything to go through for a bypassing queue.
  257. */
  258. if (unlikely(blk_queue_bypass(q)))
  259. return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
  260. blkg = __blkg_lookup(blkcg, q, true);
  261. if (blkg)
  262. return blkg;
  263. /*
  264. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  265. * non-root blkgs have access to their parents.
  266. */
  267. while (true) {
  268. struct blkcg *pos = blkcg;
  269. struct blkcg *parent = blkcg_parent(blkcg);
  270. while (parent && !__blkg_lookup(parent, q, false)) {
  271. pos = parent;
  272. parent = blkcg_parent(parent);
  273. }
  274. blkg = blkg_create(pos, q, NULL);
  275. if (pos == blkcg || IS_ERR(blkg))
  276. return blkg;
  277. }
  278. }
  279. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  280. static void blkg_destroy(struct blkcg_gq *blkg)
  281. {
  282. struct blkcg *blkcg = blkg->blkcg;
  283. int i;
  284. lockdep_assert_held(blkg->q->queue_lock);
  285. lockdep_assert_held(&blkcg->lock);
  286. /* Something wrong if we are trying to remove same group twice */
  287. WARN_ON_ONCE(list_empty(&blkg->q_node));
  288. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  289. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  290. struct blkcg_policy *pol = blkcg_policy[i];
  291. if (blkg->pd[i] && pol->pd_offline_fn)
  292. pol->pd_offline_fn(blkg);
  293. }
  294. blkg->online = false;
  295. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  296. list_del_init(&blkg->q_node);
  297. hlist_del_init_rcu(&blkg->blkcg_node);
  298. /*
  299. * Both setting lookup hint to and clearing it from @blkg are done
  300. * under queue_lock. If it's not pointing to @blkg now, it never
  301. * will. Hint assignment itself can race safely.
  302. */
  303. if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
  304. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  305. /*
  306. * Put the reference taken at the time of creation so that when all
  307. * queues are gone, group can be destroyed.
  308. */
  309. blkg_put(blkg);
  310. }
  311. /**
  312. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  313. * @q: request_queue of interest
  314. *
  315. * Destroy all blkgs associated with @q.
  316. */
  317. static void blkg_destroy_all(struct request_queue *q)
  318. {
  319. struct blkcg_gq *blkg, *n;
  320. lockdep_assert_held(q->queue_lock);
  321. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  322. struct blkcg *blkcg = blkg->blkcg;
  323. spin_lock(&blkcg->lock);
  324. blkg_destroy(blkg);
  325. spin_unlock(&blkcg->lock);
  326. }
  327. /*
  328. * root blkg is destroyed. Just clear the pointer since
  329. * root_rl does not take reference on root blkg.
  330. */
  331. q->root_blkg = NULL;
  332. q->root_rl.blkg = NULL;
  333. }
  334. static void blkg_rcu_free(struct rcu_head *rcu_head)
  335. {
  336. blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
  337. }
  338. void __blkg_release(struct blkcg_gq *blkg)
  339. {
  340. /* release the blkcg and parent blkg refs this blkg has been holding */
  341. css_put(&blkg->blkcg->css);
  342. if (blkg->parent)
  343. blkg_put(blkg->parent);
  344. /*
  345. * A group is freed in rcu manner. But having an rcu lock does not
  346. * mean that one can access all the fields of blkg and assume these
  347. * are valid. For example, don't try to follow throtl_data and
  348. * request queue links.
  349. *
  350. * Having a reference to blkg under an rcu allows acess to only
  351. * values local to groups like group stats and group rate limits
  352. */
  353. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  354. }
  355. EXPORT_SYMBOL_GPL(__blkg_release);
  356. /*
  357. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  358. * because the root blkg uses @q->root_rl instead of its own rl.
  359. */
  360. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  361. struct request_queue *q)
  362. {
  363. struct list_head *ent;
  364. struct blkcg_gq *blkg;
  365. /*
  366. * Determine the current blkg list_head. The first entry is
  367. * root_rl which is off @q->blkg_list and mapped to the head.
  368. */
  369. if (rl == &q->root_rl) {
  370. ent = &q->blkg_list;
  371. /* There are no more block groups, hence no request lists */
  372. if (list_empty(ent))
  373. return NULL;
  374. } else {
  375. blkg = container_of(rl, struct blkcg_gq, rl);
  376. ent = &blkg->q_node;
  377. }
  378. /* walk to the next list_head, skip root blkcg */
  379. ent = ent->next;
  380. if (ent == &q->root_blkg->q_node)
  381. ent = ent->next;
  382. if (ent == &q->blkg_list)
  383. return NULL;
  384. blkg = container_of(ent, struct blkcg_gq, q_node);
  385. return &blkg->rl;
  386. }
  387. static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
  388. u64 val)
  389. {
  390. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  391. struct blkcg_gq *blkg;
  392. struct hlist_node *n;
  393. int i;
  394. mutex_lock(&blkcg_pol_mutex);
  395. spin_lock_irq(&blkcg->lock);
  396. /*
  397. * Note that stat reset is racy - it doesn't synchronize against
  398. * stat updates. This is a debug feature which shouldn't exist
  399. * anyway. If you get hit by a race, retry.
  400. */
  401. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  402. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  403. struct blkcg_policy *pol = blkcg_policy[i];
  404. if (blkcg_policy_enabled(blkg->q, pol) &&
  405. pol->pd_reset_stats_fn)
  406. pol->pd_reset_stats_fn(blkg);
  407. }
  408. }
  409. spin_unlock_irq(&blkcg->lock);
  410. mutex_unlock(&blkcg_pol_mutex);
  411. return 0;
  412. }
  413. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  414. {
  415. /* some drivers (floppy) instantiate a queue w/o disk registered */
  416. if (blkg->q->backing_dev_info.dev)
  417. return dev_name(blkg->q->backing_dev_info.dev);
  418. return NULL;
  419. }
  420. /**
  421. * blkcg_print_blkgs - helper for printing per-blkg data
  422. * @sf: seq_file to print to
  423. * @blkcg: blkcg of interest
  424. * @prfill: fill function to print out a blkg
  425. * @pol: policy in question
  426. * @data: data to be passed to @prfill
  427. * @show_total: to print out sum of prfill return values or not
  428. *
  429. * This function invokes @prfill on each blkg of @blkcg if pd for the
  430. * policy specified by @pol exists. @prfill is invoked with @sf, the
  431. * policy data and @data and the matching queue lock held. If @show_total
  432. * is %true, the sum of the return values from @prfill is printed with
  433. * "Total" label at the end.
  434. *
  435. * This is to be used to construct print functions for
  436. * cftype->read_seq_string method.
  437. */
  438. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  439. u64 (*prfill)(struct seq_file *,
  440. struct blkg_policy_data *, int),
  441. const struct blkcg_policy *pol, int data,
  442. bool show_total)
  443. {
  444. struct blkcg_gq *blkg;
  445. struct hlist_node *n;
  446. u64 total = 0;
  447. rcu_read_lock();
  448. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  449. spin_lock_irq(blkg->q->queue_lock);
  450. if (blkcg_policy_enabled(blkg->q, pol))
  451. total += prfill(sf, blkg->pd[pol->plid], data);
  452. spin_unlock_irq(blkg->q->queue_lock);
  453. }
  454. rcu_read_unlock();
  455. if (show_total)
  456. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  457. }
  458. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  459. /**
  460. * __blkg_prfill_u64 - prfill helper for a single u64 value
  461. * @sf: seq_file to print to
  462. * @pd: policy private data of interest
  463. * @v: value to print
  464. *
  465. * Print @v to @sf for the device assocaited with @pd.
  466. */
  467. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  468. {
  469. const char *dname = blkg_dev_name(pd->blkg);
  470. if (!dname)
  471. return 0;
  472. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  473. return v;
  474. }
  475. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  476. /**
  477. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  478. * @sf: seq_file to print to
  479. * @pd: policy private data of interest
  480. * @rwstat: rwstat to print
  481. *
  482. * Print @rwstat to @sf for the device assocaited with @pd.
  483. */
  484. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  485. const struct blkg_rwstat *rwstat)
  486. {
  487. static const char *rwstr[] = {
  488. [BLKG_RWSTAT_READ] = "Read",
  489. [BLKG_RWSTAT_WRITE] = "Write",
  490. [BLKG_RWSTAT_SYNC] = "Sync",
  491. [BLKG_RWSTAT_ASYNC] = "Async",
  492. };
  493. const char *dname = blkg_dev_name(pd->blkg);
  494. u64 v;
  495. int i;
  496. if (!dname)
  497. return 0;
  498. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  499. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  500. (unsigned long long)rwstat->cnt[i]);
  501. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  502. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  503. return v;
  504. }
  505. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  506. /**
  507. * blkg_prfill_stat - prfill callback for blkg_stat
  508. * @sf: seq_file to print to
  509. * @pd: policy private data of interest
  510. * @off: offset to the blkg_stat in @pd
  511. *
  512. * prfill callback for printing a blkg_stat.
  513. */
  514. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  515. {
  516. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  517. }
  518. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  519. /**
  520. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  521. * @sf: seq_file to print to
  522. * @pd: policy private data of interest
  523. * @off: offset to the blkg_rwstat in @pd
  524. *
  525. * prfill callback for printing a blkg_rwstat.
  526. */
  527. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  528. int off)
  529. {
  530. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  531. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  532. }
  533. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  534. /**
  535. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  536. * @pd: policy private data of interest
  537. * @off: offset to the blkg_stat in @pd
  538. *
  539. * Collect the blkg_stat specified by @off from @pd and all its online
  540. * descendants and return the sum. The caller must be holding the queue
  541. * lock for online tests.
  542. */
  543. u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off)
  544. {
  545. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  546. struct blkcg_gq *pos_blkg;
  547. struct cgroup *pos_cgrp;
  548. u64 sum;
  549. lockdep_assert_held(pd->blkg->q->queue_lock);
  550. sum = blkg_stat_read((void *)pd + off);
  551. rcu_read_lock();
  552. blkg_for_each_descendant_pre(pos_blkg, pos_cgrp, pd_to_blkg(pd)) {
  553. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  554. struct blkg_stat *stat = (void *)pos_pd + off;
  555. if (pos_blkg->online)
  556. sum += blkg_stat_read(stat);
  557. }
  558. rcu_read_unlock();
  559. return sum;
  560. }
  561. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  562. /**
  563. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  564. * @pd: policy private data of interest
  565. * @off: offset to the blkg_stat in @pd
  566. *
  567. * Collect the blkg_rwstat specified by @off from @pd and all its online
  568. * descendants and return the sum. The caller must be holding the queue
  569. * lock for online tests.
  570. */
  571. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
  572. int off)
  573. {
  574. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  575. struct blkcg_gq *pos_blkg;
  576. struct cgroup *pos_cgrp;
  577. struct blkg_rwstat sum;
  578. int i;
  579. lockdep_assert_held(pd->blkg->q->queue_lock);
  580. sum = blkg_rwstat_read((void *)pd + off);
  581. rcu_read_lock();
  582. blkg_for_each_descendant_pre(pos_blkg, pos_cgrp, pd_to_blkg(pd)) {
  583. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  584. struct blkg_rwstat *rwstat = (void *)pos_pd + off;
  585. struct blkg_rwstat tmp;
  586. if (!pos_blkg->online)
  587. continue;
  588. tmp = blkg_rwstat_read(rwstat);
  589. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  590. sum.cnt[i] += tmp.cnt[i];
  591. }
  592. rcu_read_unlock();
  593. return sum;
  594. }
  595. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  596. /**
  597. * blkg_conf_prep - parse and prepare for per-blkg config update
  598. * @blkcg: target block cgroup
  599. * @pol: target policy
  600. * @input: input string
  601. * @ctx: blkg_conf_ctx to be filled
  602. *
  603. * Parse per-blkg config update from @input and initialize @ctx with the
  604. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  605. * value. This function returns with RCU read lock and queue lock held and
  606. * must be paired with blkg_conf_finish().
  607. */
  608. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  609. const char *input, struct blkg_conf_ctx *ctx)
  610. __acquires(rcu) __acquires(disk->queue->queue_lock)
  611. {
  612. struct gendisk *disk;
  613. struct blkcg_gq *blkg;
  614. unsigned int major, minor;
  615. unsigned long long v;
  616. int part, ret;
  617. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  618. return -EINVAL;
  619. disk = get_gendisk(MKDEV(major, minor), &part);
  620. if (!disk || part)
  621. return -EINVAL;
  622. rcu_read_lock();
  623. spin_lock_irq(disk->queue->queue_lock);
  624. if (blkcg_policy_enabled(disk->queue, pol))
  625. blkg = blkg_lookup_create(blkcg, disk->queue);
  626. else
  627. blkg = ERR_PTR(-EINVAL);
  628. if (IS_ERR(blkg)) {
  629. ret = PTR_ERR(blkg);
  630. rcu_read_unlock();
  631. spin_unlock_irq(disk->queue->queue_lock);
  632. put_disk(disk);
  633. /*
  634. * If queue was bypassing, we should retry. Do so after a
  635. * short msleep(). It isn't strictly necessary but queue
  636. * can be bypassing for some time and it's always nice to
  637. * avoid busy looping.
  638. */
  639. if (ret == -EBUSY) {
  640. msleep(10);
  641. ret = restart_syscall();
  642. }
  643. return ret;
  644. }
  645. ctx->disk = disk;
  646. ctx->blkg = blkg;
  647. ctx->v = v;
  648. return 0;
  649. }
  650. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  651. /**
  652. * blkg_conf_finish - finish up per-blkg config update
  653. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  654. *
  655. * Finish up after per-blkg config update. This function must be paired
  656. * with blkg_conf_prep().
  657. */
  658. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  659. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  660. {
  661. spin_unlock_irq(ctx->disk->queue->queue_lock);
  662. rcu_read_unlock();
  663. put_disk(ctx->disk);
  664. }
  665. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  666. struct cftype blkcg_files[] = {
  667. {
  668. .name = "reset_stats",
  669. .write_u64 = blkcg_reset_stats,
  670. },
  671. { } /* terminate */
  672. };
  673. /**
  674. * blkcg_css_offline - cgroup css_offline callback
  675. * @cgroup: cgroup of interest
  676. *
  677. * This function is called when @cgroup is about to go away and responsible
  678. * for shooting down all blkgs associated with @cgroup. blkgs should be
  679. * removed while holding both q and blkcg locks. As blkcg lock is nested
  680. * inside q lock, this function performs reverse double lock dancing.
  681. *
  682. * This is the blkcg counterpart of ioc_release_fn().
  683. */
  684. static void blkcg_css_offline(struct cgroup *cgroup)
  685. {
  686. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  687. spin_lock_irq(&blkcg->lock);
  688. while (!hlist_empty(&blkcg->blkg_list)) {
  689. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  690. struct blkcg_gq, blkcg_node);
  691. struct request_queue *q = blkg->q;
  692. if (spin_trylock(q->queue_lock)) {
  693. blkg_destroy(blkg);
  694. spin_unlock(q->queue_lock);
  695. } else {
  696. spin_unlock_irq(&blkcg->lock);
  697. cpu_relax();
  698. spin_lock_irq(&blkcg->lock);
  699. }
  700. }
  701. spin_unlock_irq(&blkcg->lock);
  702. }
  703. static void blkcg_css_free(struct cgroup *cgroup)
  704. {
  705. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  706. if (blkcg != &blkcg_root)
  707. kfree(blkcg);
  708. }
  709. static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
  710. {
  711. static atomic64_t id_seq = ATOMIC64_INIT(0);
  712. struct blkcg *blkcg;
  713. struct cgroup *parent = cgroup->parent;
  714. if (!parent) {
  715. blkcg = &blkcg_root;
  716. goto done;
  717. }
  718. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  719. if (!blkcg)
  720. return ERR_PTR(-ENOMEM);
  721. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  722. blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
  723. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  724. done:
  725. spin_lock_init(&blkcg->lock);
  726. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  727. INIT_HLIST_HEAD(&blkcg->blkg_list);
  728. return &blkcg->css;
  729. }
  730. /**
  731. * blkcg_init_queue - initialize blkcg part of request queue
  732. * @q: request_queue to initialize
  733. *
  734. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  735. * part of new request_queue @q.
  736. *
  737. * RETURNS:
  738. * 0 on success, -errno on failure.
  739. */
  740. int blkcg_init_queue(struct request_queue *q)
  741. {
  742. might_sleep();
  743. return blk_throtl_init(q);
  744. }
  745. /**
  746. * blkcg_drain_queue - drain blkcg part of request_queue
  747. * @q: request_queue to drain
  748. *
  749. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  750. */
  751. void blkcg_drain_queue(struct request_queue *q)
  752. {
  753. lockdep_assert_held(q->queue_lock);
  754. blk_throtl_drain(q);
  755. }
  756. /**
  757. * blkcg_exit_queue - exit and release blkcg part of request_queue
  758. * @q: request_queue being released
  759. *
  760. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  761. */
  762. void blkcg_exit_queue(struct request_queue *q)
  763. {
  764. spin_lock_irq(q->queue_lock);
  765. blkg_destroy_all(q);
  766. spin_unlock_irq(q->queue_lock);
  767. blk_throtl_exit(q);
  768. }
  769. /*
  770. * We cannot support shared io contexts, as we have no mean to support
  771. * two tasks with the same ioc in two different groups without major rework
  772. * of the main cic data structures. For now we allow a task to change
  773. * its cgroup only if it's the only owner of its ioc.
  774. */
  775. static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  776. {
  777. struct task_struct *task;
  778. struct io_context *ioc;
  779. int ret = 0;
  780. /* task_lock() is needed to avoid races with exit_io_context() */
  781. cgroup_taskset_for_each(task, cgrp, tset) {
  782. task_lock(task);
  783. ioc = task->io_context;
  784. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  785. ret = -EINVAL;
  786. task_unlock(task);
  787. if (ret)
  788. break;
  789. }
  790. return ret;
  791. }
  792. struct cgroup_subsys blkio_subsys = {
  793. .name = "blkio",
  794. .css_alloc = blkcg_css_alloc,
  795. .css_offline = blkcg_css_offline,
  796. .css_free = blkcg_css_free,
  797. .can_attach = blkcg_can_attach,
  798. .subsys_id = blkio_subsys_id,
  799. .base_cftypes = blkcg_files,
  800. .module = THIS_MODULE,
  801. /*
  802. * blkio subsystem is utterly broken in terms of hierarchy support.
  803. * It treats all cgroups equally regardless of where they're
  804. * located in the hierarchy - all cgroups are treated as if they're
  805. * right below the root. Fix it and remove the following.
  806. */
  807. .broken_hierarchy = true,
  808. };
  809. EXPORT_SYMBOL_GPL(blkio_subsys);
  810. /**
  811. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  812. * @q: request_queue of interest
  813. * @pol: blkcg policy to activate
  814. *
  815. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  816. * bypass mode to populate its blkgs with policy_data for @pol.
  817. *
  818. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  819. * from IO path. Update of each blkg is protected by both queue and blkcg
  820. * locks so that holding either lock and testing blkcg_policy_enabled() is
  821. * always enough for dereferencing policy data.
  822. *
  823. * The caller is responsible for synchronizing [de]activations and policy
  824. * [un]registerations. Returns 0 on success, -errno on failure.
  825. */
  826. int blkcg_activate_policy(struct request_queue *q,
  827. const struct blkcg_policy *pol)
  828. {
  829. LIST_HEAD(pds);
  830. struct blkcg_gq *blkg, *new_blkg;
  831. struct blkg_policy_data *pd, *n;
  832. int cnt = 0, ret;
  833. bool preloaded;
  834. if (blkcg_policy_enabled(q, pol))
  835. return 0;
  836. /* preallocations for root blkg */
  837. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  838. if (!new_blkg)
  839. return -ENOMEM;
  840. preloaded = !radix_tree_preload(GFP_KERNEL);
  841. blk_queue_bypass_start(q);
  842. /*
  843. * Make sure the root blkg exists and count the existing blkgs. As
  844. * @q is bypassing at this point, blkg_lookup_create() can't be
  845. * used. Open code it.
  846. */
  847. spin_lock_irq(q->queue_lock);
  848. rcu_read_lock();
  849. blkg = __blkg_lookup(&blkcg_root, q, false);
  850. if (blkg)
  851. blkg_free(new_blkg);
  852. else
  853. blkg = blkg_create(&blkcg_root, q, new_blkg);
  854. rcu_read_unlock();
  855. if (preloaded)
  856. radix_tree_preload_end();
  857. if (IS_ERR(blkg)) {
  858. ret = PTR_ERR(blkg);
  859. goto out_unlock;
  860. }
  861. q->root_blkg = blkg;
  862. q->root_rl.blkg = blkg;
  863. list_for_each_entry(blkg, &q->blkg_list, q_node)
  864. cnt++;
  865. spin_unlock_irq(q->queue_lock);
  866. /* allocate policy_data for all existing blkgs */
  867. while (cnt--) {
  868. pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
  869. if (!pd) {
  870. ret = -ENOMEM;
  871. goto out_free;
  872. }
  873. list_add_tail(&pd->alloc_node, &pds);
  874. }
  875. /*
  876. * Install the allocated pds. With @q bypassing, no new blkg
  877. * should have been created while the queue lock was dropped.
  878. */
  879. spin_lock_irq(q->queue_lock);
  880. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  881. if (WARN_ON(list_empty(&pds))) {
  882. /* umm... this shouldn't happen, just abort */
  883. ret = -ENOMEM;
  884. goto out_unlock;
  885. }
  886. pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
  887. list_del_init(&pd->alloc_node);
  888. /* grab blkcg lock too while installing @pd on @blkg */
  889. spin_lock(&blkg->blkcg->lock);
  890. blkg->pd[pol->plid] = pd;
  891. pd->blkg = blkg;
  892. pd->plid = pol->plid;
  893. pol->pd_init_fn(blkg);
  894. spin_unlock(&blkg->blkcg->lock);
  895. }
  896. __set_bit(pol->plid, q->blkcg_pols);
  897. ret = 0;
  898. out_unlock:
  899. spin_unlock_irq(q->queue_lock);
  900. out_free:
  901. blk_queue_bypass_end(q);
  902. list_for_each_entry_safe(pd, n, &pds, alloc_node)
  903. kfree(pd);
  904. return ret;
  905. }
  906. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  907. /**
  908. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  909. * @q: request_queue of interest
  910. * @pol: blkcg policy to deactivate
  911. *
  912. * Deactivate @pol on @q. Follows the same synchronization rules as
  913. * blkcg_activate_policy().
  914. */
  915. void blkcg_deactivate_policy(struct request_queue *q,
  916. const struct blkcg_policy *pol)
  917. {
  918. struct blkcg_gq *blkg;
  919. if (!blkcg_policy_enabled(q, pol))
  920. return;
  921. blk_queue_bypass_start(q);
  922. spin_lock_irq(q->queue_lock);
  923. __clear_bit(pol->plid, q->blkcg_pols);
  924. /* if no policy is left, no need for blkgs - shoot them down */
  925. if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
  926. blkg_destroy_all(q);
  927. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  928. /* grab blkcg lock too while removing @pd from @blkg */
  929. spin_lock(&blkg->blkcg->lock);
  930. if (pol->pd_offline_fn)
  931. pol->pd_offline_fn(blkg);
  932. if (pol->pd_exit_fn)
  933. pol->pd_exit_fn(blkg);
  934. kfree(blkg->pd[pol->plid]);
  935. blkg->pd[pol->plid] = NULL;
  936. spin_unlock(&blkg->blkcg->lock);
  937. }
  938. spin_unlock_irq(q->queue_lock);
  939. blk_queue_bypass_end(q);
  940. }
  941. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  942. /**
  943. * blkcg_policy_register - register a blkcg policy
  944. * @pol: blkcg policy to register
  945. *
  946. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  947. * successful registration. Returns 0 on success and -errno on failure.
  948. */
  949. int blkcg_policy_register(struct blkcg_policy *pol)
  950. {
  951. int i, ret;
  952. if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
  953. return -EINVAL;
  954. mutex_lock(&blkcg_pol_mutex);
  955. /* find an empty slot */
  956. ret = -ENOSPC;
  957. for (i = 0; i < BLKCG_MAX_POLS; i++)
  958. if (!blkcg_policy[i])
  959. break;
  960. if (i >= BLKCG_MAX_POLS)
  961. goto out_unlock;
  962. /* register and update blkgs */
  963. pol->plid = i;
  964. blkcg_policy[i] = pol;
  965. /* everything is in place, add intf files for the new policy */
  966. if (pol->cftypes)
  967. WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
  968. ret = 0;
  969. out_unlock:
  970. mutex_unlock(&blkcg_pol_mutex);
  971. return ret;
  972. }
  973. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  974. /**
  975. * blkcg_policy_unregister - unregister a blkcg policy
  976. * @pol: blkcg policy to unregister
  977. *
  978. * Undo blkcg_policy_register(@pol). Might sleep.
  979. */
  980. void blkcg_policy_unregister(struct blkcg_policy *pol)
  981. {
  982. mutex_lock(&blkcg_pol_mutex);
  983. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  984. goto out_unlock;
  985. /* kill the intf files first */
  986. if (pol->cftypes)
  987. cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
  988. /* unregister and update blkgs */
  989. blkcg_policy[pol->plid] = NULL;
  990. out_unlock:
  991. mutex_unlock(&blkcg_pol_mutex);
  992. }
  993. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);