blk-cgroup.c 9.8 KB

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