blk-cgroup.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. int i;
  393. mutex_lock(&blkcg_pol_mutex);
  394. spin_lock_irq(&blkcg->lock);
  395. /*
  396. * Note that stat reset is racy - it doesn't synchronize against
  397. * stat updates. This is a debug feature which shouldn't exist
  398. * anyway. If you get hit by a race, retry.
  399. */
  400. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  401. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  402. struct blkcg_policy *pol = blkcg_policy[i];
  403. if (blkcg_policy_enabled(blkg->q, pol) &&
  404. pol->pd_reset_stats_fn)
  405. pol->pd_reset_stats_fn(blkg);
  406. }
  407. }
  408. spin_unlock_irq(&blkcg->lock);
  409. mutex_unlock(&blkcg_pol_mutex);
  410. return 0;
  411. }
  412. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  413. {
  414. /* some drivers (floppy) instantiate a queue w/o disk registered */
  415. if (blkg->q->backing_dev_info.dev)
  416. return dev_name(blkg->q->backing_dev_info.dev);
  417. return NULL;
  418. }
  419. /**
  420. * blkcg_print_blkgs - helper for printing per-blkg data
  421. * @sf: seq_file to print to
  422. * @blkcg: blkcg of interest
  423. * @prfill: fill function to print out a blkg
  424. * @pol: policy in question
  425. * @data: data to be passed to @prfill
  426. * @show_total: to print out sum of prfill return values or not
  427. *
  428. * This function invokes @prfill on each blkg of @blkcg if pd for the
  429. * policy specified by @pol exists. @prfill is invoked with @sf, the
  430. * policy data and @data and the matching queue lock held. If @show_total
  431. * is %true, the sum of the return values from @prfill is printed with
  432. * "Total" label at the end.
  433. *
  434. * This is to be used to construct print functions for
  435. * cftype->read_seq_string method.
  436. */
  437. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  438. u64 (*prfill)(struct seq_file *,
  439. struct blkg_policy_data *, int),
  440. const struct blkcg_policy *pol, int data,
  441. bool show_total)
  442. {
  443. struct blkcg_gq *blkg;
  444. u64 total = 0;
  445. rcu_read_lock();
  446. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  447. spin_lock_irq(blkg->q->queue_lock);
  448. if (blkcg_policy_enabled(blkg->q, pol))
  449. total += prfill(sf, blkg->pd[pol->plid], data);
  450. spin_unlock_irq(blkg->q->queue_lock);
  451. }
  452. rcu_read_unlock();
  453. if (show_total)
  454. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  455. }
  456. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  457. /**
  458. * __blkg_prfill_u64 - prfill helper for a single u64 value
  459. * @sf: seq_file to print to
  460. * @pd: policy private data of interest
  461. * @v: value to print
  462. *
  463. * Print @v to @sf for the device assocaited with @pd.
  464. */
  465. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  466. {
  467. const char *dname = blkg_dev_name(pd->blkg);
  468. if (!dname)
  469. return 0;
  470. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  471. return v;
  472. }
  473. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  474. /**
  475. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  476. * @sf: seq_file to print to
  477. * @pd: policy private data of interest
  478. * @rwstat: rwstat to print
  479. *
  480. * Print @rwstat to @sf for the device assocaited with @pd.
  481. */
  482. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  483. const struct blkg_rwstat *rwstat)
  484. {
  485. static const char *rwstr[] = {
  486. [BLKG_RWSTAT_READ] = "Read",
  487. [BLKG_RWSTAT_WRITE] = "Write",
  488. [BLKG_RWSTAT_SYNC] = "Sync",
  489. [BLKG_RWSTAT_ASYNC] = "Async",
  490. };
  491. const char *dname = blkg_dev_name(pd->blkg);
  492. u64 v;
  493. int i;
  494. if (!dname)
  495. return 0;
  496. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  497. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  498. (unsigned long long)rwstat->cnt[i]);
  499. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  500. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  501. return v;
  502. }
  503. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  504. /**
  505. * blkg_prfill_stat - prfill callback for blkg_stat
  506. * @sf: seq_file to print to
  507. * @pd: policy private data of interest
  508. * @off: offset to the blkg_stat in @pd
  509. *
  510. * prfill callback for printing a blkg_stat.
  511. */
  512. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  513. {
  514. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  515. }
  516. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  517. /**
  518. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  519. * @sf: seq_file to print to
  520. * @pd: policy private data of interest
  521. * @off: offset to the blkg_rwstat in @pd
  522. *
  523. * prfill callback for printing a blkg_rwstat.
  524. */
  525. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  526. int off)
  527. {
  528. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  529. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  530. }
  531. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  532. /**
  533. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  534. * @pd: policy private data of interest
  535. * @off: offset to the blkg_stat in @pd
  536. *
  537. * Collect the blkg_stat specified by @off from @pd and all its online
  538. * descendants and return the sum. The caller must be holding the queue
  539. * lock for online tests.
  540. */
  541. u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off)
  542. {
  543. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  544. struct blkcg_gq *pos_blkg;
  545. struct cgroup *pos_cgrp;
  546. u64 sum;
  547. lockdep_assert_held(pd->blkg->q->queue_lock);
  548. sum = blkg_stat_read((void *)pd + off);
  549. rcu_read_lock();
  550. blkg_for_each_descendant_pre(pos_blkg, pos_cgrp, pd_to_blkg(pd)) {
  551. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  552. struct blkg_stat *stat = (void *)pos_pd + off;
  553. if (pos_blkg->online)
  554. sum += blkg_stat_read(stat);
  555. }
  556. rcu_read_unlock();
  557. return sum;
  558. }
  559. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  560. /**
  561. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  562. * @pd: policy private data of interest
  563. * @off: offset to the blkg_stat in @pd
  564. *
  565. * Collect the blkg_rwstat specified by @off from @pd and all its online
  566. * descendants and return the sum. The caller must be holding the queue
  567. * lock for online tests.
  568. */
  569. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
  570. int off)
  571. {
  572. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  573. struct blkcg_gq *pos_blkg;
  574. struct cgroup *pos_cgrp;
  575. struct blkg_rwstat sum;
  576. int i;
  577. lockdep_assert_held(pd->blkg->q->queue_lock);
  578. sum = blkg_rwstat_read((void *)pd + off);
  579. rcu_read_lock();
  580. blkg_for_each_descendant_pre(pos_blkg, pos_cgrp, pd_to_blkg(pd)) {
  581. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  582. struct blkg_rwstat *rwstat = (void *)pos_pd + off;
  583. struct blkg_rwstat tmp;
  584. if (!pos_blkg->online)
  585. continue;
  586. tmp = blkg_rwstat_read(rwstat);
  587. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  588. sum.cnt[i] += tmp.cnt[i];
  589. }
  590. rcu_read_unlock();
  591. return sum;
  592. }
  593. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  594. /**
  595. * blkg_conf_prep - parse and prepare for per-blkg config update
  596. * @blkcg: target block cgroup
  597. * @pol: target policy
  598. * @input: input string
  599. * @ctx: blkg_conf_ctx to be filled
  600. *
  601. * Parse per-blkg config update from @input and initialize @ctx with the
  602. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  603. * value. This function returns with RCU read lock and queue lock held and
  604. * must be paired with blkg_conf_finish().
  605. */
  606. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  607. const char *input, struct blkg_conf_ctx *ctx)
  608. __acquires(rcu) __acquires(disk->queue->queue_lock)
  609. {
  610. struct gendisk *disk;
  611. struct blkcg_gq *blkg;
  612. unsigned int major, minor;
  613. unsigned long long v;
  614. int part, ret;
  615. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  616. return -EINVAL;
  617. disk = get_gendisk(MKDEV(major, minor), &part);
  618. if (!disk || part)
  619. return -EINVAL;
  620. rcu_read_lock();
  621. spin_lock_irq(disk->queue->queue_lock);
  622. if (blkcg_policy_enabled(disk->queue, pol))
  623. blkg = blkg_lookup_create(blkcg, disk->queue);
  624. else
  625. blkg = ERR_PTR(-EINVAL);
  626. if (IS_ERR(blkg)) {
  627. ret = PTR_ERR(blkg);
  628. rcu_read_unlock();
  629. spin_unlock_irq(disk->queue->queue_lock);
  630. put_disk(disk);
  631. /*
  632. * If queue was bypassing, we should retry. Do so after a
  633. * short msleep(). It isn't strictly necessary but queue
  634. * can be bypassing for some time and it's always nice to
  635. * avoid busy looping.
  636. */
  637. if (ret == -EBUSY) {
  638. msleep(10);
  639. ret = restart_syscall();
  640. }
  641. return ret;
  642. }
  643. ctx->disk = disk;
  644. ctx->blkg = blkg;
  645. ctx->v = v;
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  649. /**
  650. * blkg_conf_finish - finish up per-blkg config update
  651. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  652. *
  653. * Finish up after per-blkg config update. This function must be paired
  654. * with blkg_conf_prep().
  655. */
  656. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  657. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  658. {
  659. spin_unlock_irq(ctx->disk->queue->queue_lock);
  660. rcu_read_unlock();
  661. put_disk(ctx->disk);
  662. }
  663. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  664. struct cftype blkcg_files[] = {
  665. {
  666. .name = "reset_stats",
  667. .write_u64 = blkcg_reset_stats,
  668. },
  669. { } /* terminate */
  670. };
  671. /**
  672. * blkcg_css_offline - cgroup css_offline callback
  673. * @cgroup: cgroup of interest
  674. *
  675. * This function is called when @cgroup is about to go away and responsible
  676. * for shooting down all blkgs associated with @cgroup. blkgs should be
  677. * removed while holding both q and blkcg locks. As blkcg lock is nested
  678. * inside q lock, this function performs reverse double lock dancing.
  679. *
  680. * This is the blkcg counterpart of ioc_release_fn().
  681. */
  682. static void blkcg_css_offline(struct cgroup *cgroup)
  683. {
  684. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  685. spin_lock_irq(&blkcg->lock);
  686. while (!hlist_empty(&blkcg->blkg_list)) {
  687. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  688. struct blkcg_gq, blkcg_node);
  689. struct request_queue *q = blkg->q;
  690. if (spin_trylock(q->queue_lock)) {
  691. blkg_destroy(blkg);
  692. spin_unlock(q->queue_lock);
  693. } else {
  694. spin_unlock_irq(&blkcg->lock);
  695. cpu_relax();
  696. spin_lock_irq(&blkcg->lock);
  697. }
  698. }
  699. spin_unlock_irq(&blkcg->lock);
  700. }
  701. static void blkcg_css_free(struct cgroup *cgroup)
  702. {
  703. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  704. if (blkcg != &blkcg_root)
  705. kfree(blkcg);
  706. }
  707. static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
  708. {
  709. static atomic64_t id_seq = ATOMIC64_INIT(0);
  710. struct blkcg *blkcg;
  711. struct cgroup *parent = cgroup->parent;
  712. if (!parent) {
  713. blkcg = &blkcg_root;
  714. goto done;
  715. }
  716. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  717. if (!blkcg)
  718. return ERR_PTR(-ENOMEM);
  719. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  720. blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
  721. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  722. done:
  723. spin_lock_init(&blkcg->lock);
  724. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  725. INIT_HLIST_HEAD(&blkcg->blkg_list);
  726. return &blkcg->css;
  727. }
  728. /**
  729. * blkcg_init_queue - initialize blkcg part of request queue
  730. * @q: request_queue to initialize
  731. *
  732. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  733. * part of new request_queue @q.
  734. *
  735. * RETURNS:
  736. * 0 on success, -errno on failure.
  737. */
  738. int blkcg_init_queue(struct request_queue *q)
  739. {
  740. might_sleep();
  741. return blk_throtl_init(q);
  742. }
  743. /**
  744. * blkcg_drain_queue - drain blkcg part of request_queue
  745. * @q: request_queue to drain
  746. *
  747. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  748. */
  749. void blkcg_drain_queue(struct request_queue *q)
  750. {
  751. lockdep_assert_held(q->queue_lock);
  752. blk_throtl_drain(q);
  753. }
  754. /**
  755. * blkcg_exit_queue - exit and release blkcg part of request_queue
  756. * @q: request_queue being released
  757. *
  758. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  759. */
  760. void blkcg_exit_queue(struct request_queue *q)
  761. {
  762. spin_lock_irq(q->queue_lock);
  763. blkg_destroy_all(q);
  764. spin_unlock_irq(q->queue_lock);
  765. blk_throtl_exit(q);
  766. }
  767. /*
  768. * We cannot support shared io contexts, as we have no mean to support
  769. * two tasks with the same ioc in two different groups without major rework
  770. * of the main cic data structures. For now we allow a task to change
  771. * its cgroup only if it's the only owner of its ioc.
  772. */
  773. static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  774. {
  775. struct task_struct *task;
  776. struct io_context *ioc;
  777. int ret = 0;
  778. /* task_lock() is needed to avoid races with exit_io_context() */
  779. cgroup_taskset_for_each(task, cgrp, tset) {
  780. task_lock(task);
  781. ioc = task->io_context;
  782. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  783. ret = -EINVAL;
  784. task_unlock(task);
  785. if (ret)
  786. break;
  787. }
  788. return ret;
  789. }
  790. struct cgroup_subsys blkio_subsys = {
  791. .name = "blkio",
  792. .css_alloc = blkcg_css_alloc,
  793. .css_offline = blkcg_css_offline,
  794. .css_free = blkcg_css_free,
  795. .can_attach = blkcg_can_attach,
  796. .subsys_id = blkio_subsys_id,
  797. .base_cftypes = blkcg_files,
  798. .module = THIS_MODULE,
  799. /*
  800. * blkio subsystem is utterly broken in terms of hierarchy support.
  801. * It treats all cgroups equally regardless of where they're
  802. * located in the hierarchy - all cgroups are treated as if they're
  803. * right below the root. Fix it and remove the following.
  804. */
  805. .broken_hierarchy = true,
  806. };
  807. EXPORT_SYMBOL_GPL(blkio_subsys);
  808. /**
  809. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  810. * @q: request_queue of interest
  811. * @pol: blkcg policy to activate
  812. *
  813. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  814. * bypass mode to populate its blkgs with policy_data for @pol.
  815. *
  816. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  817. * from IO path. Update of each blkg is protected by both queue and blkcg
  818. * locks so that holding either lock and testing blkcg_policy_enabled() is
  819. * always enough for dereferencing policy data.
  820. *
  821. * The caller is responsible for synchronizing [de]activations and policy
  822. * [un]registerations. Returns 0 on success, -errno on failure.
  823. */
  824. int blkcg_activate_policy(struct request_queue *q,
  825. const struct blkcg_policy *pol)
  826. {
  827. LIST_HEAD(pds);
  828. struct blkcg_gq *blkg, *new_blkg;
  829. struct blkg_policy_data *pd, *n;
  830. int cnt = 0, ret;
  831. bool preloaded;
  832. if (blkcg_policy_enabled(q, pol))
  833. return 0;
  834. /* preallocations for root blkg */
  835. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  836. if (!new_blkg)
  837. return -ENOMEM;
  838. preloaded = !radix_tree_preload(GFP_KERNEL);
  839. blk_queue_bypass_start(q);
  840. /*
  841. * Make sure the root blkg exists and count the existing blkgs. As
  842. * @q is bypassing at this point, blkg_lookup_create() can't be
  843. * used. Open code it.
  844. */
  845. spin_lock_irq(q->queue_lock);
  846. rcu_read_lock();
  847. blkg = __blkg_lookup(&blkcg_root, q, false);
  848. if (blkg)
  849. blkg_free(new_blkg);
  850. else
  851. blkg = blkg_create(&blkcg_root, q, new_blkg);
  852. rcu_read_unlock();
  853. if (preloaded)
  854. radix_tree_preload_end();
  855. if (IS_ERR(blkg)) {
  856. ret = PTR_ERR(blkg);
  857. goto out_unlock;
  858. }
  859. q->root_blkg = blkg;
  860. q->root_rl.blkg = blkg;
  861. list_for_each_entry(blkg, &q->blkg_list, q_node)
  862. cnt++;
  863. spin_unlock_irq(q->queue_lock);
  864. /* allocate policy_data for all existing blkgs */
  865. while (cnt--) {
  866. pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
  867. if (!pd) {
  868. ret = -ENOMEM;
  869. goto out_free;
  870. }
  871. list_add_tail(&pd->alloc_node, &pds);
  872. }
  873. /*
  874. * Install the allocated pds. With @q bypassing, no new blkg
  875. * should have been created while the queue lock was dropped.
  876. */
  877. spin_lock_irq(q->queue_lock);
  878. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  879. if (WARN_ON(list_empty(&pds))) {
  880. /* umm... this shouldn't happen, just abort */
  881. ret = -ENOMEM;
  882. goto out_unlock;
  883. }
  884. pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
  885. list_del_init(&pd->alloc_node);
  886. /* grab blkcg lock too while installing @pd on @blkg */
  887. spin_lock(&blkg->blkcg->lock);
  888. blkg->pd[pol->plid] = pd;
  889. pd->blkg = blkg;
  890. pd->plid = pol->plid;
  891. pol->pd_init_fn(blkg);
  892. spin_unlock(&blkg->blkcg->lock);
  893. }
  894. __set_bit(pol->plid, q->blkcg_pols);
  895. ret = 0;
  896. out_unlock:
  897. spin_unlock_irq(q->queue_lock);
  898. out_free:
  899. blk_queue_bypass_end(q);
  900. list_for_each_entry_safe(pd, n, &pds, alloc_node)
  901. kfree(pd);
  902. return ret;
  903. }
  904. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  905. /**
  906. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  907. * @q: request_queue of interest
  908. * @pol: blkcg policy to deactivate
  909. *
  910. * Deactivate @pol on @q. Follows the same synchronization rules as
  911. * blkcg_activate_policy().
  912. */
  913. void blkcg_deactivate_policy(struct request_queue *q,
  914. const struct blkcg_policy *pol)
  915. {
  916. struct blkcg_gq *blkg;
  917. if (!blkcg_policy_enabled(q, pol))
  918. return;
  919. blk_queue_bypass_start(q);
  920. spin_lock_irq(q->queue_lock);
  921. __clear_bit(pol->plid, q->blkcg_pols);
  922. /* if no policy is left, no need for blkgs - shoot them down */
  923. if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
  924. blkg_destroy_all(q);
  925. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  926. /* grab blkcg lock too while removing @pd from @blkg */
  927. spin_lock(&blkg->blkcg->lock);
  928. if (pol->pd_offline_fn)
  929. pol->pd_offline_fn(blkg);
  930. if (pol->pd_exit_fn)
  931. pol->pd_exit_fn(blkg);
  932. kfree(blkg->pd[pol->plid]);
  933. blkg->pd[pol->plid] = NULL;
  934. spin_unlock(&blkg->blkcg->lock);
  935. }
  936. spin_unlock_irq(q->queue_lock);
  937. blk_queue_bypass_end(q);
  938. }
  939. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  940. /**
  941. * blkcg_policy_register - register a blkcg policy
  942. * @pol: blkcg policy to register
  943. *
  944. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  945. * successful registration. Returns 0 on success and -errno on failure.
  946. */
  947. int blkcg_policy_register(struct blkcg_policy *pol)
  948. {
  949. int i, ret;
  950. if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
  951. return -EINVAL;
  952. mutex_lock(&blkcg_pol_mutex);
  953. /* find an empty slot */
  954. ret = -ENOSPC;
  955. for (i = 0; i < BLKCG_MAX_POLS; i++)
  956. if (!blkcg_policy[i])
  957. break;
  958. if (i >= BLKCG_MAX_POLS)
  959. goto out_unlock;
  960. /* register and update blkgs */
  961. pol->plid = i;
  962. blkcg_policy[i] = pol;
  963. /* everything is in place, add intf files for the new policy */
  964. if (pol->cftypes)
  965. WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
  966. ret = 0;
  967. out_unlock:
  968. mutex_unlock(&blkcg_pol_mutex);
  969. return ret;
  970. }
  971. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  972. /**
  973. * blkcg_policy_unregister - unregister a blkcg policy
  974. * @pol: blkcg policy to unregister
  975. *
  976. * Undo blkcg_policy_register(@pol). Might sleep.
  977. */
  978. void blkcg_policy_unregister(struct blkcg_policy *pol)
  979. {
  980. mutex_lock(&blkcg_pol_mutex);
  981. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  982. goto out_unlock;
  983. /* kill the intf files first */
  984. if (pol->cftypes)
  985. cgroup_rm_cftypes(&blkio_subsys, pol->cftypes);
  986. /* unregister and update blkgs */
  987. blkcg_policy[pol->plid] = NULL;
  988. out_unlock:
  989. mutex_unlock(&blkcg_pol_mutex);
  990. }
  991. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);