blk-cgroup.c 23 KB

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