connectionSignalSlot.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 (blkcg_policy_enabled(blkg->q, pol))
  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)
  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 we
  113. * may not be holding queue_lock and thus are not sure whether
  114. * @blkg from blkg_tree has already been removed or not, so we
  115. * can't update hint to the lookup result. Leave it to the caller.
  116. */
  117. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  118. if (blkg && blkg->q == q)
  119. return blkg;
  120. return NULL;
  121. }
  122. /**
  123. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  124. * @blkcg: blkcg of interest
  125. * @q: request_queue of interest
  126. *
  127. * Lookup blkg for the @blkcg - @q pair. This function should be called
  128. * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
  129. * - see blk_queue_bypass_start() for details.
  130. */
  131. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
  132. {
  133. WARN_ON_ONCE(!rcu_read_lock_held());
  134. if (unlikely(blk_queue_bypass(q)))
  135. return NULL;
  136. return __blkg_lookup(blkcg, q);
  137. }
  138. EXPORT_SYMBOL_GPL(blkg_lookup);
  139. /*
  140. * If @new_blkg is %NULL, this function tries to allocate a new one as
  141. * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
  142. */
  143. static struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
  144. struct request_queue *q,
  145. struct blkcg_gq *new_blkg)
  146. {
  147. struct blkcg_gq *blkg;
  148. int ret;
  149. WARN_ON_ONCE(!rcu_read_lock_held());
  150. lockdep_assert_held(q->queue_lock);
  151. /* lookup and update hint on success, see __blkg_lookup() for details */
  152. blkg = __blkg_lookup(blkcg, q);
  153. if (blkg) {
  154. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  155. goto out_free;
  156. }
  157. /* blkg holds a reference to blkcg */
  158. if (!css_tryget(&blkcg->css)) {
  159. blkg = ERR_PTR(-EINVAL);
  160. goto out_free;
  161. }
  162. /* allocate */
  163. if (!new_blkg) {
  164. new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
  165. if (unlikely(!new_blkg)) {
  166. blkg = ERR_PTR(-ENOMEM);
  167. goto out_put;
  168. }
  169. }
  170. blkg = new_blkg;
  171. /* insert */
  172. spin_lock(&blkcg->lock);
  173. ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
  174. if (likely(!ret)) {
  175. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  176. list_add(&blkg->q_node, &q->blkg_list);
  177. }
  178. spin_unlock(&blkcg->lock);
  179. if (!ret)
  180. return blkg;
  181. blkg = ERR_PTR(ret);
  182. out_put:
  183. css_put(&blkcg->css);
  184. out_free:
  185. blkg_free(new_blkg);
  186. return blkg;
  187. }
  188. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  189. struct request_queue *q)
  190. {
  191. /*
  192. * This could be the first entry point of blkcg implementation and
  193. * we shouldn't allow anything to go through for a bypassing queue.
  194. */
  195. if (unlikely(blk_queue_bypass(q)))
  196. return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
  197. return __blkg_lookup_create(blkcg, q, NULL);
  198. }
  199. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  200. static void blkg_destroy(struct blkcg_gq *blkg)
  201. {
  202. struct blkcg *blkcg = blkg->blkcg;
  203. lockdep_assert_held(blkg->q->queue_lock);
  204. lockdep_assert_held(&blkcg->lock);
  205. /* Something wrong if we are trying to remove same group twice */
  206. WARN_ON_ONCE(list_empty(&blkg->q_node));
  207. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  208. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  209. list_del_init(&blkg->q_node);
  210. hlist_del_init_rcu(&blkg->blkcg_node);
  211. /*
  212. * Both setting lookup hint to and clearing it from @blkg are done
  213. * under queue_lock. If it's not pointing to @blkg now, it never
  214. * will. Hint assignment itself can race safely.
  215. */
  216. if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
  217. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  218. /*
  219. * Put the reference taken at the time of creation so that when all
  220. * queues are gone, group can be destroyed.
  221. */
  222. blkg_put(blkg);
  223. }
  224. /**
  225. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  226. * @q: request_queue of interest
  227. *
  228. * Destroy all blkgs associated with @q.
  229. */
  230. static void blkg_destroy_all(struct request_queue *q)
  231. {
  232. struct blkcg_gq *blkg, *n;
  233. lockdep_assert_held(q->queue_lock);
  234. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  235. struct blkcg *blkcg = blkg->blkcg;
  236. spin_lock(&blkcg->lock);
  237. blkg_destroy(blkg);
  238. spin_unlock(&blkcg->lock);
  239. }
  240. /*
  241. * root blkg is destroyed. Just clear the pointer since
  242. * root_rl does not take reference on root blkg.
  243. */
  244. q->root_blkg = NULL;
  245. q->root_rl.blkg = NULL;
  246. }
  247. static void blkg_rcu_free(struct rcu_head *rcu_head)
  248. {
  249. blkg_free(container_of(rcu_head, struct blkcg_gq, rcu_head));
  250. }
  251. void __blkg_release(struct blkcg_gq *blkg)
  252. {
  253. /* release the extra blkcg reference this blkg has been holding */
  254. css_put(&blkg->blkcg->css);
  255. /*
  256. * A group is freed in rcu manner. But having an rcu lock does not
  257. * mean that one can access all the fields of blkg and assume these
  258. * are valid. For example, don't try to follow throtl_data and
  259. * request queue links.
  260. *
  261. * Having a reference to blkg under an rcu allows acess to only
  262. * values local to groups like group stats and group rate limits
  263. */
  264. call_rcu(&blkg->rcu_head, blkg_rcu_free);
  265. }
  266. EXPORT_SYMBOL_GPL(__blkg_release);
  267. /*
  268. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  269. * because the root blkg uses @q->root_rl instead of its own rl.
  270. */
  271. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  272. struct request_queue *q)
  273. {
  274. struct list_head *ent;
  275. struct blkcg_gq *blkg;
  276. /*
  277. * Determine the current blkg list_head. The first entry is
  278. * root_rl which is off @q->blkg_list and mapped to the head.
  279. */
  280. if (rl == &q->root_rl) {
  281. ent = &q->blkg_list;
  282. /* There are no more block groups, hence no request lists */
  283. if (list_empty(ent))
  284. return NULL;
  285. } else {
  286. blkg = container_of(rl, struct blkcg_gq, rl);
  287. ent = &blkg->q_node;
  288. }
  289. /* walk to the next list_head, skip root blkcg */
  290. ent = ent->next;
  291. if (ent == &q->root_blkg->q_node)
  292. ent = ent->next;
  293. if (ent == &q->blkg_list)
  294. return NULL;
  295. blkg = container_of(ent, struct blkcg_gq, q_node);
  296. return &blkg->rl;
  297. }
  298. static int blkcg_reset_stats(struct cgroup *cgroup, struct cftype *cftype,
  299. u64 val)
  300. {
  301. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  302. struct blkcg_gq *blkg;
  303. struct hlist_node *n;
  304. int i;
  305. mutex_lock(&blkcg_pol_mutex);
  306. spin_lock_irq(&blkcg->lock);
  307. /*
  308. * Note that stat reset is racy - it doesn't synchronize against
  309. * stat updates. This is a debug feature which shouldn't exist
  310. * anyway. If you get hit by a race, retry.
  311. */
  312. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
  313. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  314. struct blkcg_policy *pol = blkcg_policy[i];
  315. if (blkcg_policy_enabled(blkg->q, pol) &&
  316. pol->pd_reset_stats_fn)
  317. pol->pd_reset_stats_fn(blkg);
  318. }
  319. }
  320. spin_unlock_irq(&blkcg->lock);
  321. mutex_unlock(&blkcg_pol_mutex);
  322. return 0;
  323. }
  324. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  325. {
  326. /* some drivers (floppy) instantiate a queue w/o disk registered */
  327. if (blkg->q->backing_dev_info.dev)
  328. return dev_name(blkg->q->backing_dev_info.dev);
  329. return NULL;
  330. }
  331. /**
  332. * blkcg_print_blkgs - helper for printing per-blkg data
  333. * @sf: seq_file to print to
  334. * @blkcg: blkcg of interest
  335. * @prfill: fill function to print out a blkg
  336. * @pol: policy in question
  337. * @data: data to be passed to @prfill
  338. * @show_total: to print out sum of prfill return values or not
  339. *
  340. * This function invokes @prfill on each blkg of @blkcg if pd for the
  341. * policy specified by @pol exists. @prfill is invoked with @sf, the
  342. * policy data and @data. If @show_total is %true, the sum of the return
  343. * values from @prfill is printed with "Total" label at the end.
  344. *
  345. * This is to be used to construct print functions for
  346. * cftype->read_seq_string method.
  347. */
  348. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  349. u64 (*prfill)(struct seq_file *,
  350. struct blkg_policy_data *, int),
  351. const struct blkcg_policy *pol, int data,
  352. bool show_total)
  353. {
  354. struct blkcg_gq *blkg;
  355. struct hlist_node *n;
  356. u64 total = 0;
  357. spin_lock_irq(&blkcg->lock);
  358. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  359. if (blkcg_policy_enabled(blkg->q, pol))
  360. total += prfill(sf, blkg->pd[pol->plid], data);
  361. spin_unlock_irq(&blkcg->lock);
  362. if (show_total)
  363. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  364. }
  365. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  366. /**
  367. * __blkg_prfill_u64 - prfill helper for a single u64 value
  368. * @sf: seq_file to print to
  369. * @pd: policy private data of interest
  370. * @v: value to print
  371. *
  372. * Print @v to @sf for the device assocaited with @pd.
  373. */
  374. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  375. {
  376. const char *dname = blkg_dev_name(pd->blkg);
  377. if (!dname)
  378. return 0;
  379. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  380. return v;
  381. }
  382. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  383. /**
  384. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  385. * @sf: seq_file to print to
  386. * @pd: policy private data of interest
  387. * @rwstat: rwstat to print
  388. *
  389. * Print @rwstat to @sf for the device assocaited with @pd.
  390. */
  391. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  392. const struct blkg_rwstat *rwstat)
  393. {
  394. static const char *rwstr[] = {
  395. [BLKG_RWSTAT_READ] = "Read",
  396. [BLKG_RWSTAT_WRITE] = "Write",
  397. [BLKG_RWSTAT_SYNC] = "Sync",
  398. [BLKG_RWSTAT_ASYNC] = "Async",
  399. };
  400. const char *dname = blkg_dev_name(pd->blkg);
  401. u64 v;
  402. int i;
  403. if (!dname)
  404. return 0;
  405. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  406. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  407. (unsigned long long)rwstat->cnt[i]);
  408. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  409. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  410. return v;
  411. }
  412. /**
  413. * blkg_prfill_stat - prfill callback for blkg_stat
  414. * @sf: seq_file to print to
  415. * @pd: policy private data of interest
  416. * @off: offset to the blkg_stat in @pd
  417. *
  418. * prfill callback for printing a blkg_stat.
  419. */
  420. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  421. {
  422. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  423. }
  424. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  425. /**
  426. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  427. * @sf: seq_file to print to
  428. * @pd: policy private data of interest
  429. * @off: offset to the blkg_rwstat in @pd
  430. *
  431. * prfill callback for printing a blkg_rwstat.
  432. */
  433. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  434. int off)
  435. {
  436. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  437. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  438. }
  439. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  440. /**
  441. * blkg_conf_prep - parse and prepare for per-blkg config update
  442. * @blkcg: target block cgroup
  443. * @pol: target policy
  444. * @input: input string
  445. * @ctx: blkg_conf_ctx to be filled
  446. *
  447. * Parse per-blkg config update from @input and initialize @ctx with the
  448. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  449. * value. This function returns with RCU read lock and queue lock held and
  450. * must be paired with blkg_conf_finish().
  451. */
  452. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  453. const char *input, struct blkg_conf_ctx *ctx)
  454. __acquires(rcu) __acquires(disk->queue->queue_lock)
  455. {
  456. struct gendisk *disk;
  457. struct blkcg_gq *blkg;
  458. unsigned int major, minor;
  459. unsigned long long v;
  460. int part, ret;
  461. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  462. return -EINVAL;
  463. disk = get_gendisk(MKDEV(major, minor), &part);
  464. if (!disk || part)
  465. return -EINVAL;
  466. rcu_read_lock();
  467. spin_lock_irq(disk->queue->queue_lock);
  468. if (blkcg_policy_enabled(disk->queue, pol))
  469. blkg = blkg_lookup_create(blkcg, disk->queue);
  470. else
  471. blkg = ERR_PTR(-EINVAL);
  472. if (IS_ERR(blkg)) {
  473. ret = PTR_ERR(blkg);
  474. rcu_read_unlock();
  475. spin_unlock_irq(disk->queue->queue_lock);
  476. put_disk(disk);
  477. /*
  478. * If queue was bypassing, we should retry. Do so after a
  479. * short msleep(). It isn't strictly necessary but queue
  480. * can be bypassing for some time and it's always nice to
  481. * avoid busy looping.
  482. */
  483. if (ret == -EBUSY) {
  484. msleep(10);
  485. ret = restart_syscall();
  486. }
  487. return ret;
  488. }
  489. ctx->disk = disk;
  490. ctx->blkg = blkg;
  491. ctx->v = v;
  492. return 0;
  493. }
  494. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  495. /**
  496. * blkg_conf_finish - finish up per-blkg config update
  497. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  498. *
  499. * Finish up after per-blkg config update. This function must be paired
  500. * with blkg_conf_prep().
  501. */
  502. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  503. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  504. {
  505. spin_unlock_irq(ctx->disk->queue->queue_lock);
  506. rcu_read_unlock();
  507. put_disk(ctx->disk);
  508. }
  509. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  510. struct cftype blkcg_files[] = {
  511. {
  512. .name = "reset_stats",
  513. .write_u64 = blkcg_reset_stats,
  514. },
  515. { } /* terminate */
  516. };
  517. /**
  518. * blkcg_css_offline - cgroup css_offline callback
  519. * @cgroup: cgroup of interest
  520. *
  521. * This function is called when @cgroup is about to go away and responsible
  522. * for shooting down all blkgs associated with @cgroup. blkgs should be
  523. * removed while holding both q and blkcg locks. As blkcg lock is nested
  524. * inside q lock, this function performs reverse double lock dancing.
  525. *
  526. * This is the blkcg counterpart of ioc_release_fn().
  527. */
  528. static void blkcg_css_offline(struct cgroup *cgroup)
  529. {
  530. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  531. spin_lock_irq(&blkcg->lock);
  532. while (!hlist_empty(&blkcg->blkg_list)) {
  533. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  534. struct blkcg_gq, blkcg_node);
  535. struct request_queue *q = blkg->q;
  536. if (spin_trylock(q->queue_lock)) {
  537. blkg_destroy(blkg);
  538. spin_unlock(q->queue_lock);
  539. } else {
  540. spin_unlock_irq(&blkcg->lock);
  541. cpu_relax();
  542. spin_lock_irq(&blkcg->lock);
  543. }
  544. }
  545. spin_unlock_irq(&blkcg->lock);
  546. }
  547. static void blkcg_css_free(struct cgroup *cgroup)
  548. {
  549. struct blkcg *blkcg = cgroup_to_blkcg(cgroup);
  550. if (blkcg != &blkcg_root)
  551. kfree(blkcg);
  552. }
  553. static struct cgroup_subsys_state *blkcg_css_alloc(struct cgroup *cgroup)
  554. {
  555. static atomic64_t id_seq = ATOMIC64_INIT(0);
  556. struct blkcg *blkcg;
  557. struct cgroup *parent = cgroup->parent;
  558. if (!parent) {
  559. blkcg = &blkcg_root;
  560. goto done;
  561. }
  562. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  563. if (!blkcg)
  564. return ERR_PTR(-ENOMEM);
  565. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  566. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  567. done:
  568. spin_lock_init(&blkcg->lock);
  569. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  570. INIT_HLIST_HEAD(&blkcg->blkg_list);
  571. return &blkcg->css;
  572. }
  573. /**
  574. * blkcg_init_queue - initialize blkcg part of request queue
  575. * @q: request_queue to initialize
  576. *
  577. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  578. * part of new request_queue @q.
  579. *
  580. * RETURNS:
  581. * 0 on success, -errno on failure.
  582. */
  583. int blkcg_init_queue(struct request_queue *q)
  584. {
  585. might_sleep();
  586. return blk_throtl_init(q);
  587. }
  588. /**
  589. * blkcg_drain_queue - drain blkcg part of request_queue
  590. * @q: request_queue to drain
  591. *
  592. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  593. */
  594. void blkcg_drain_queue(struct request_queue *q)
  595. {
  596. lockdep_assert_held(q->queue_lock);
  597. blk_throtl_drain(q);
  598. }
  599. /**
  600. * blkcg_exit_queue - exit and release blkcg part of request_queue
  601. * @q: request_queue being released
  602. *
  603. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  604. */
  605. void blkcg_exit_queue(struct request_queue *q)
  606. {
  607. spin_lock_irq(q->queue_lock);
  608. blkg_destroy_all(q);
  609. spin_unlock_irq(q->queue_lock);
  610. blk_throtl_exit(q);
  611. }
  612. /*
  613. * We cannot support shared io contexts, as we have no mean to support
  614. * two tasks with the same ioc in two different groups without major rework
  615. * of the main cic data structures. For now we allow a task to change
  616. * its cgroup only if it's the only owner of its ioc.
  617. */
  618. static int blkcg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  619. {
  620. struct task_struct *task;
  621. struct io_context *ioc;
  622. int ret = 0;
  623. /* task_lock() is needed to avoid races with exit_io_context() */
  624. cgroup_taskset_for_each(task, cgrp, tset) {
  625. task_lock(task);
  626. ioc = task->io_context;
  627. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  628. ret = -EINVAL;
  629. task_unlock(task);
  630. if (ret)
  631. break;
  632. }
  633. return ret;
  634. }
  635. struct cgroup_subsys blkio_subsys = {
  636. .name = "blkio",
  637. .css_alloc = blkcg_css_alloc,
  638. .css_offline = blkcg_css_offline,
  639. .css_free = blkcg_css_free,
  640. .can_attach = blkcg_can_attach,
  641. .subsys_id = blkio_subsys_id,
  642. .base_cftypes = blkcg_files,
  643. .module = THIS_MODULE,
  644. /*
  645. * blkio subsystem is utterly broken in terms of hierarchy support.
  646. * It treats all cgroups equally regardless of where they're
  647. * located in the hierarchy - all cgroups are treated as if they're
  648. * right below the root. Fix it and remove the following.
  649. */
  650. .broken_hierarchy = true,
  651. };
  652. EXPORT_SYMBOL_GPL(blkio_subsys);
  653. /**
  654. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  655. * @q: request_queue of interest
  656. * @pol: blkcg policy to activate
  657. *
  658. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  659. * bypass mode to populate its blkgs with policy_data for @pol.
  660. *
  661. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  662. * from IO path. Update of each blkg is protected by both queue and blkcg
  663. * locks so that holding either lock and testing blkcg_policy_enabled() is
  664. * always enough for dereferencing policy data.
  665. *
  666. * The caller is responsible for synchronizing [de]activations and policy
  667. * [un]registerations. Returns 0 on success, -errno on failure.
  668. */
  669. int blkcg_activate_policy(struct request_queue *q,
  670. const struct blkcg_policy *pol)
  671. {
  672. LIST_HEAD(pds);
  673. struct blkcg_gq *blkg;
  674. struct blkg_policy_data *pd, *n;
  675. int cnt = 0, ret;
  676. bool preloaded;
  677. if (blkcg_policy_enabled(q, pol))
  678. return 0;
  679. /* preallocations for root blkg */
  680. blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  681. if (!blkg)
  682. return -ENOMEM;
  683. preloaded = !radix_tree_preload(GFP_KERNEL);
  684. blk_queue_bypass_start(q);
  685. /* make sure the root blkg exists and count the existing blkgs */
  686. spin_lock_irq(q->queue_lock);
  687. rcu_read_lock();
  688. blkg = __blkg_lookup_create(&blkcg_root, q, blkg);
  689. rcu_read_unlock();
  690. if (preloaded)
  691. radix_tree_preload_end();