blk-cgroup.c 9.7 KB

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