blk-cgroup.c 26 KB

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