blk-cgroup.c 22 KB

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