blk-cgroup.c 25 KB

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