blk-cgroup.c 23 KB

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