blk-cgroup.c 29 KB

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