blk-cgroup.c 9.1 KB

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