blk-cgroup.c 24 KB

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