blk-cgroup.c 23 KB

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