blk-cgroup.c 9.7 KB

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