blk-cgroup.c 23 KB

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