hugetlb_cgroup.c 11 KB

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