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