hugetlb_cgroup.c 11 KB

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