blk-cgroup.c 23 KB

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