blk-cgroup.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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/seq_file.h>
  15. #include <linux/kdev_t.h>
  16. #include "blk-cgroup.h"
  17. extern void cfq_unlink_blkio_group(void *, struct blkio_group *);
  18. extern void cfq_update_blkio_group_weight(struct blkio_group *, unsigned int);
  19. struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
  20. struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
  21. {
  22. return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
  23. struct blkio_cgroup, css);
  24. }
  25. void blkiocg_update_blkio_group_stats(struct blkio_group *blkg,
  26. unsigned long time, unsigned long sectors)
  27. {
  28. blkg->time += time;
  29. blkg->sectors += sectors;
  30. }
  31. void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
  32. struct blkio_group *blkg, void *key, dev_t dev)
  33. {
  34. unsigned long flags;
  35. spin_lock_irqsave(&blkcg->lock, flags);
  36. rcu_assign_pointer(blkg->key, key);
  37. blkg->blkcg_id = css_id(&blkcg->css);
  38. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  39. spin_unlock_irqrestore(&blkcg->lock, flags);
  40. #ifdef CONFIG_DEBUG_BLK_CGROUP
  41. /* Need to take css reference ? */
  42. cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
  43. #endif
  44. blkg->dev = dev;
  45. }
  46. static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
  47. {
  48. hlist_del_init_rcu(&blkg->blkcg_node);
  49. blkg->blkcg_id = 0;
  50. }
  51. /*
  52. * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
  53. * indicating that blk_group was unhashed by the time we got to it.
  54. */
  55. int blkiocg_del_blkio_group(struct blkio_group *blkg)
  56. {
  57. struct blkio_cgroup *blkcg;
  58. unsigned long flags;
  59. struct cgroup_subsys_state *css;
  60. int ret = 1;
  61. rcu_read_lock();
  62. css = css_lookup(&blkio_subsys, blkg->blkcg_id);
  63. if (!css)
  64. goto out;
  65. blkcg = container_of(css, struct blkio_cgroup, css);
  66. spin_lock_irqsave(&blkcg->lock, flags);
  67. if (!hlist_unhashed(&blkg->blkcg_node)) {
  68. __blkiocg_del_blkio_group(blkg);
  69. ret = 0;
  70. }
  71. spin_unlock_irqrestore(&blkcg->lock, flags);
  72. out:
  73. rcu_read_unlock();
  74. return ret;
  75. }
  76. /* called under rcu_read_lock(). */
  77. struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
  78. {
  79. struct blkio_group *blkg;
  80. struct hlist_node *n;
  81. void *__key;
  82. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
  83. __key = blkg->key;
  84. if (__key == key)
  85. return blkg;
  86. }
  87. return NULL;
  88. }
  89. #define SHOW_FUNCTION(__VAR) \
  90. static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
  91. struct cftype *cftype) \
  92. { \
  93. struct blkio_cgroup *blkcg; \
  94. \
  95. blkcg = cgroup_to_blkio_cgroup(cgroup); \
  96. return (u64)blkcg->__VAR; \
  97. }
  98. SHOW_FUNCTION(weight);
  99. #undef SHOW_FUNCTION
  100. static int
  101. blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
  102. {
  103. struct blkio_cgroup *blkcg;
  104. struct blkio_group *blkg;
  105. struct hlist_node *n;
  106. if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
  107. return -EINVAL;
  108. blkcg = cgroup_to_blkio_cgroup(cgroup);
  109. spin_lock_irq(&blkcg->lock);
  110. blkcg->weight = (unsigned int)val;
  111. hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
  112. cfq_update_blkio_group_weight(blkg, blkcg->weight);
  113. spin_unlock_irq(&blkcg->lock);
  114. return 0;
  115. }
  116. #define SHOW_FUNCTION_PER_GROUP(__VAR) \
  117. static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
  118. struct cftype *cftype, struct seq_file *m) \
  119. { \
  120. struct blkio_cgroup *blkcg; \
  121. struct blkio_group *blkg; \
  122. struct hlist_node *n; \
  123. \
  124. if (!cgroup_lock_live_group(cgroup)) \
  125. return -ENODEV; \
  126. \
  127. blkcg = cgroup_to_blkio_cgroup(cgroup); \
  128. rcu_read_lock(); \
  129. hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
  130. if (blkg->dev) \
  131. seq_printf(m, "%u:%u %lu\n", MAJOR(blkg->dev), \
  132. MINOR(blkg->dev), blkg->__VAR); \
  133. } \
  134. rcu_read_unlock(); \
  135. cgroup_unlock(); \
  136. return 0; \
  137. }
  138. SHOW_FUNCTION_PER_GROUP(time);
  139. SHOW_FUNCTION_PER_GROUP(sectors);
  140. #ifdef CONFIG_DEBUG_BLK_CGROUP
  141. SHOW_FUNCTION_PER_GROUP(dequeue);
  142. #endif
  143. #undef SHOW_FUNCTION_PER_GROUP
  144. #ifdef CONFIG_DEBUG_BLK_CGROUP
  145. void blkiocg_update_blkio_group_dequeue_stats(struct blkio_group *blkg,
  146. unsigned long dequeue)
  147. {
  148. blkg->dequeue += dequeue;
  149. }
  150. #endif
  151. struct cftype blkio_files[] = {
  152. {
  153. .name = "weight",
  154. .read_u64 = blkiocg_weight_read,
  155. .write_u64 = blkiocg_weight_write,
  156. },
  157. {
  158. .name = "time",
  159. .read_seq_string = blkiocg_time_read,
  160. },
  161. {
  162. .name = "sectors",
  163. .read_seq_string = blkiocg_sectors_read,
  164. },
  165. #ifdef CONFIG_DEBUG_BLK_CGROUP
  166. {
  167. .name = "dequeue",
  168. .read_seq_string = blkiocg_dequeue_read,
  169. },
  170. #endif
  171. };
  172. static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  173. {
  174. return cgroup_add_files(cgroup, subsys, blkio_files,
  175. ARRAY_SIZE(blkio_files));
  176. }
  177. static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  178. {
  179. struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
  180. unsigned long flags;
  181. struct blkio_group *blkg;
  182. void *key;
  183. rcu_read_lock();
  184. remove_entry:
  185. spin_lock_irqsave(&blkcg->lock, flags);
  186. if (hlist_empty(&blkcg->blkg_list)) {
  187. spin_unlock_irqrestore(&blkcg->lock, flags);
  188. goto done;
  189. }
  190. blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
  191. blkcg_node);
  192. key = rcu_dereference(blkg->key);
  193. __blkiocg_del_blkio_group(blkg);
  194. spin_unlock_irqrestore(&blkcg->lock, flags);
  195. /*
  196. * This blkio_group is being unlinked as associated cgroup is going
  197. * away. Let all the IO controlling policies know about this event.
  198. *
  199. * Currently this is static call to one io controlling policy. Once
  200. * we have more policies in place, we need some dynamic registration
  201. * of callback function.
  202. */
  203. cfq_unlink_blkio_group(key, blkg);
  204. goto remove_entry;
  205. done:
  206. free_css_id(&blkio_subsys, &blkcg->css);
  207. rcu_read_unlock();
  208. kfree(blkcg);
  209. }
  210. static struct cgroup_subsys_state *
  211. blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
  212. {
  213. struct blkio_cgroup *blkcg, *parent_blkcg;
  214. if (!cgroup->parent) {
  215. blkcg = &blkio_root_cgroup;
  216. goto done;
  217. }
  218. /* Currently we do not support hierarchy deeper than two level (0,1) */
  219. parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
  220. if (css_depth(&parent_blkcg->css) > 0)
  221. return ERR_PTR(-EINVAL);
  222. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  223. if (!blkcg)
  224. return ERR_PTR(-ENOMEM);
  225. blkcg->weight = BLKIO_WEIGHT_DEFAULT;
  226. done:
  227. spin_lock_init(&blkcg->lock);
  228. INIT_HLIST_HEAD(&blkcg->blkg_list);
  229. return &blkcg->css;
  230. }
  231. /*
  232. * We cannot support shared io contexts, as we have no mean to support
  233. * two tasks with the same ioc in two different groups without major rework
  234. * of the main cic data structures. For now we allow a task to change
  235. * its cgroup only if it's the only owner of its ioc.
  236. */
  237. static int blkiocg_can_attach(struct cgroup_subsys *subsys,
  238. struct cgroup *cgroup, struct task_struct *tsk,
  239. bool threadgroup)
  240. {
  241. struct io_context *ioc;
  242. int ret = 0;
  243. /* task_lock() is needed to avoid races with exit_io_context() */
  244. task_lock(tsk);
  245. ioc = tsk->io_context;
  246. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  247. ret = -EINVAL;
  248. task_unlock(tsk);
  249. return ret;
  250. }
  251. static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
  252. struct cgroup *prev, struct task_struct *tsk,
  253. bool threadgroup)
  254. {
  255. struct io_context *ioc;
  256. task_lock(tsk);
  257. ioc = tsk->io_context;
  258. if (ioc)
  259. ioc->cgroup_changed = 1;
  260. task_unlock(tsk);
  261. }
  262. struct cgroup_subsys blkio_subsys = {
  263. .name = "blkio",
  264. .create = blkiocg_create,
  265. .can_attach = blkiocg_can_attach,
  266. .attach = blkiocg_attach,
  267. .destroy = blkiocg_destroy,
  268. .populate = blkiocg_populate,
  269. .subsys_id = blkio_subsys_id,
  270. .use_id = 1,
  271. };