hugetlb_cgroup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. *
  3. * Copyright IBM Corporation, 2012
  4. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2.1 of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. */
  15. #include <linux/cgroup.h>
  16. #include <linux/slab.h>
  17. #include <linux/hugetlb.h>
  18. #include <linux/hugetlb_cgroup.h>
  19. struct hugetlb_cgroup {
  20. struct cgroup_subsys_state css;
  21. /*
  22. * the counter to account for hugepages from hugetlb.
  23. */
  24. struct res_counter hugepage[HUGE_MAX_HSTATE];
  25. };
  26. #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
  27. #define MEMFILE_IDX(val) (((val) >> 16) & 0xffff)
  28. #define MEMFILE_ATTR(val) ((val) & 0xffff)
  29. struct cgroup_subsys hugetlb_subsys __read_mostly;
  30. static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
  31. static inline
  32. struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
  33. {
  34. return s ? container_of(s, struct hugetlb_cgroup, css) : NULL;
  35. }
  36. static inline
  37. struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task)
  38. {
  39. return hugetlb_cgroup_from_css(task_css(task, hugetlb_subsys_id));
  40. }
  41. static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
  42. {
  43. return (h_cg == root_h_cgroup);
  44. }
  45. static inline struct hugetlb_cgroup *
  46. parent_hugetlb_cgroup(struct hugetlb_cgroup *h_cg)
  47. {
  48. return hugetlb_cgroup_from_css(css_parent(&h_cg->css));
  49. }
  50. static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)
  51. {
  52. int idx;
  53. for (idx = 0; idx < hugetlb_max_hstate; idx++) {
  54. if ((res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE)) > 0)
  55. return true;
  56. }
  57. return false;
  58. }
  59. static struct cgroup_subsys_state *
  60. hugetlb_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  61. {
  62. struct hugetlb_cgroup *parent_h_cgroup = hugetlb_cgroup_from_css(parent_css);
  63. struct hugetlb_cgroup *h_cgroup;
  64. int idx;
  65. h_cgroup = kzalloc(sizeof(*h_cgroup), GFP_KERNEL);
  66. if (!h_cgroup)
  67. return ERR_PTR(-ENOMEM);
  68. if (parent_h_cgroup) {
  69. for (idx = 0; idx < HUGE_MAX_HSTATE; idx++)
  70. res_counter_init(&h_cgroup->hugepage[idx],
  71. &parent_h_cgroup->hugepage[idx]);
  72. } else {
  73. root_h_cgroup = h_cgroup;
  74. for (idx = 0; idx < HUGE_MAX_HSTATE; idx++)
  75. res_counter_init(&h_cgroup->hugepage[idx], NULL);
  76. }
  77. return &h_cgroup->css;
  78. }
  79. static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)
  80. {
  81. struct hugetlb_cgroup *h_cgroup;
  82. h_cgroup = hugetlb_cgroup_from_css(css);
  83. kfree(h_cgroup);
  84. }
  85. /*
  86. * Should be called with hugetlb_lock held.
  87. * Since we are holding hugetlb_lock, pages cannot get moved from
  88. * active list or uncharged from the cgroup, So no need to get
  89. * page reference and test for page active here. This function
  90. * cannot fail.
  91. */
  92. static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,
  93. struct page *page)
  94. {
  95. int csize;
  96. struct res_counter *counter;
  97. struct res_counter *fail_res;
  98. struct hugetlb_cgroup *page_hcg;
  99. struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(h_cg);
  100. page_hcg = hugetlb_cgroup_from_page(page);
  101. /*
  102. * We can have pages in active list without any cgroup
  103. * ie, hugepage with less than 3 pages. We can safely
  104. * ignore those pages.
  105. */
  106. if (!page_hcg || page_hcg != h_cg)
  107. goto out;
  108. csize = PAGE_SIZE << compound_order(page);
  109. if (!parent) {
  110. parent = root_h_cgroup;
  111. /* root has no limit */
  112. res_counter_charge_nofail(&parent->hugepage[idx],
  113. csize, &fail_res);
  114. }
  115. counter = &h_cg->hugepage[idx];
  116. res_counter_uncharge_until(counter, counter->parent, csize);
  117. set_hugetlb_cgroup(page, parent);
  118. out:
  119. return;
  120. }
  121. /*
  122. * Force the hugetlb cgroup to empty the hugetlb resources by moving them to
  123. * the parent cgroup.
  124. */
  125. static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css)
  126. {
  127. struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
  128. struct hstate *h;
  129. struct page *page;
  130. int idx = 0;
  131. do {
  132. for_each_hstate(h) {
  133. spin_lock(&hugetlb_lock);
  134. list_for_each_entry(page, &h->hugepage_activelist, lru)
  135. hugetlb_cgroup_move_parent(idx, h_cg, page);
  136. spin_unlock(&hugetlb_lock);
  137. idx++;
  138. }
  139. cond_resched();
  140. } while (hugetlb_cgroup_have_usage(h_cg));
  141. }
  142. int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
  143. struct hugetlb_cgroup **ptr)
  144. {
  145. int ret = 0;
  146. struct res_counter *fail_res;
  147. struct hugetlb_cgroup *h_cg = NULL;
  148. unsigned long csize = nr_pages * PAGE_SIZE;
  149. if (hugetlb_cgroup_disabled())
  150. goto done;
  151. /*
  152. * We don't charge any cgroup if the compound page have less
  153. * than 3 pages.
  154. */
  155. if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
  156. goto done;
  157. again:
  158. rcu_read_lock();
  159. h_cg = hugetlb_cgroup_from_task(current);
  160. if (!css_tryget(&h_cg->css)) {
  161. rcu_read_unlock();
  162. goto again;
  163. }
  164. rcu_read_unlock();
  165. ret = res_counter_charge(&h_cg->hugepage[idx], csize, &fail_res);
  166. css_put(&h_cg->css);
  167. done:
  168. *ptr = h_cg;
  169. return ret;
  170. }
  171. /* Should be called with hugetlb_lock held */
  172. void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
  173. struct hugetlb_cgroup *h_cg,
  174. struct page *page)
  175. {
  176. if (hugetlb_cgroup_disabled() || !h_cg)
  177. return;
  178. set_hugetlb_cgroup(page, h_cg);
  179. return;
  180. }
  181. /*
  182. * Should be called with hugetlb_lock held
  183. */
  184. void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
  185. struct page *page)
  186. {
  187. struct hugetlb_cgroup *h_cg;
  188. unsigned long csize = nr_pages * PAGE_SIZE;
  189. if (hugetlb_cgroup_disabled())
  190. return;
  191. VM_BUG_ON(!spin_is_locked(&hugetlb_lock));
  192. h_cg = hugetlb_cgroup_from_page(page);
  193. if (unlikely(!h_cg))
  194. return;
  195. set_hugetlb_cgroup(page, NULL);
  196. res_counter_uncharge(&h_cg->hugepage[idx], csize);
  197. return;
  198. }
  199. void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
  200. struct hugetlb_cgroup *h_cg)
  201. {
  202. unsigned long csize = nr_pages * PAGE_SIZE;
  203. if (hugetlb_cgroup_disabled() || !h_cg)
  204. return;
  205. if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
  206. return;
  207. res_counter_uncharge(&h_cg->hugepage[idx], csize);
  208. return;
  209. }
  210. static ssize_t hugetlb_cgroup_read(struct cgroup_subsys_state *css,
  211. struct cftype *cft, struct file *file,
  212. char __user *buf, size_t nbytes,
  213. loff_t *ppos)
  214. {
  215. u64 val;
  216. char str[64];
  217. int idx, name, len;
  218. struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
  219. idx = MEMFILE_IDX(cft->private);
  220. name = MEMFILE_ATTR(cft->private);
  221. val = res_counter_read_u64(&h_cg->hugepage[idx], name);
  222. len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
  223. return simple_read_from_buffer(buf, nbytes, ppos, str, len);
  224. }
  225. static int hugetlb_cgroup_write(struct cgroup_subsys_state *css,
  226. struct cftype *cft, const char *buffer)
  227. {
  228. int idx, name, ret;
  229. unsigned long long val;
  230. struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
  231. idx = MEMFILE_IDX(cft->private);
  232. name = MEMFILE_ATTR(cft->private);
  233. switch (name) {
  234. case RES_LIMIT:
  235. if (hugetlb_cgroup_is_root(h_cg)) {
  236. /* Can't set limit on root */
  237. ret = -EINVAL;
  238. break;
  239. }
  240. /* This function does all necessary parse...reuse it */
  241. ret = res_counter_memparse_write_strategy(buffer, &val);
  242. if (ret)
  243. break;
  244. ret = res_counter_set_limit(&h_cg->hugepage[idx], val);
  245. break;
  246. default:
  247. ret = -EINVAL;
  248. break;
  249. }
  250. return ret;
  251. }
  252. static int hugetlb_cgroup_reset(struct cgroup_subsys_state *css,
  253. unsigned int event)
  254. {
  255. int idx, name, ret = 0;
  256. struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
  257. idx = MEMFILE_IDX(event);
  258. name = MEMFILE_ATTR(event);
  259. switch (name) {
  260. case RES_MAX_USAGE:
  261. res_counter_reset_max(&h_cg->hugepage[idx]);
  262. break;
  263. case RES_FAILCNT:
  264. res_counter_reset_failcnt(&h_cg->hugepage[idx]);
  265. break;
  266. default:
  267. ret = -EINVAL;
  268. break;
  269. }
  270. return ret;
  271. }
  272. static char *mem_fmt(char *buf, int size, unsigned long hsize)
  273. {
  274. if (hsize >= (1UL << 30))
  275. snprintf(buf, size, "%luGB", hsize >> 30);
  276. else if (hsize >= (1UL << 20))
  277. snprintf(buf, size, "%luMB", hsize >> 20);
  278. else
  279. snprintf(buf, size, "%luKB", hsize >> 10);
  280. return buf;
  281. }
  282. static void __init __hugetlb_cgroup_file_init(int idx)
  283. {
  284. char buf[32];
  285. struct cftype *cft;
  286. struct hstate *h = &hstates[idx];
  287. /* format the size */
  288. mem_fmt(buf, 32, huge_page_size(h));
  289. /* Add the limit file */
  290. cft = &h->cgroup_files[0];
  291. snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
  292. cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
  293. cft->read = hugetlb_cgroup_read;
  294. cft->write_string = hugetlb_cgroup_write;
  295. /* Add the usage file */
  296. cft = &h->cgroup_files[1];
  297. snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
  298. cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
  299. cft->read = hugetlb_cgroup_read;
  300. /* Add the MAX usage file */
  301. cft = &h->cgroup_files[2];
  302. snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
  303. cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
  304. cft->trigger = hugetlb_cgroup_reset;
  305. cft->read = hugetlb_cgroup_read;
  306. /* Add the failcntfile */
  307. cft = &h->cgroup_files[3];
  308. snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
  309. cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
  310. cft->trigger = hugetlb_cgroup_reset;
  311. cft->read = hugetlb_cgroup_read;
  312. /* NULL terminate the last cft */
  313. cft = &h->cgroup_files[4];
  314. memset(cft, 0, sizeof(*cft));
  315. WARN_ON(cgroup_add_cftypes(&hugetlb_subsys, h->cgroup_files));
  316. return;
  317. }
  318. void __init hugetlb_cgroup_file_init(void)
  319. {
  320. struct hstate *h;
  321. for_each_hstate(h) {
  322. /*
  323. * Add cgroup control files only if the huge page consists
  324. * of more than two normal pages. This is because we use
  325. * page[2].lru.next for storing cgroup details.
  326. */
  327. if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER)
  328. __hugetlb_cgroup_file_init(hstate_index(h));
  329. }
  330. }
  331. /*
  332. * hugetlb_lock will make sure a parallel cgroup rmdir won't happen
  333. * when we migrate hugepages
  334. */
  335. void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)
  336. {
  337. struct hugetlb_cgroup *h_cg;
  338. struct hstate *h = page_hstate(oldhpage);
  339. if (hugetlb_cgroup_disabled())
  340. return;
  341. VM_BUG_ON(!PageHuge(oldhpage));
  342. spin_lock(&hugetlb_lock);
  343. h_cg = hugetlb_cgroup_from_page(oldhpage);
  344. set_hugetlb_cgroup(oldhpage, NULL);
  345. /* move the h_cg details to new cgroup */
  346. set_hugetlb_cgroup(newhpage, h_cg);
  347. list_move(&newhpage->lru, &h->hugepage_activelist);
  348. spin_unlock(&hugetlb_lock);
  349. return;
  350. }
  351. struct cgroup_subsys hugetlb_subsys = {
  352. .name = "hugetlb",
  353. .css_alloc = hugetlb_cgroup_css_alloc,
  354. .css_offline = hugetlb_cgroup_css_offline,
  355. .css_free = hugetlb_cgroup_css_free,
  356. .subsys_id = hugetlb_subsys_id,
  357. };