connectionSignalSlot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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);