hugetlb_cgroup.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright IBM Corporation, 2012
  3. * Author Aneesh Kumar K.V <[email protected]>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2.1 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. #ifndef _LINUX_HUGETLB_CGROUP_H
  15. #define _LINUX_HUGETLB_CGROUP_H
  16. #include <linux/mmdebug.h>
  17. struct hugetlb_cgroup;
  18. struct resv_map;
  19. struct file_region;
  20. #ifdef CONFIG_CGROUP_HUGETLB
  21. /*
  22. * Minimum page order trackable by hugetlb cgroup.
  23. * At least 4 pages are necessary for all the tracking information.
  24. * The second tail page (hpage[SUBPAGE_INDEX_CGROUP]) is the fault
  25. * usage cgroup. The third tail page (hpage[SUBPAGE_INDEX_CGROUP_RSVD])
  26. * is the reservation usage cgroup.
  27. */
  28. #define HUGETLB_CGROUP_MIN_ORDER order_base_2(__MAX_CGROUP_SUBPAGE_INDEX + 1)
  29. enum hugetlb_memory_event {
  30. HUGETLB_MAX,
  31. HUGETLB_NR_MEMORY_EVENTS,
  32. };
  33. struct hugetlb_cgroup_per_node {
  34. /* hugetlb usage in pages over all hstates. */
  35. unsigned long usage[HUGE_MAX_HSTATE];
  36. };
  37. struct hugetlb_cgroup {
  38. struct cgroup_subsys_state css;
  39. /*
  40. * the counter to account for hugepages from hugetlb.
  41. */
  42. struct page_counter hugepage[HUGE_MAX_HSTATE];
  43. /*
  44. * the counter to account for hugepage reservations from hugetlb.
  45. */
  46. struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE];
  47. atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
  48. atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
  49. /* Handle for "hugetlb.events" */
  50. struct cgroup_file events_file[HUGE_MAX_HSTATE];
  51. /* Handle for "hugetlb.events.local" */
  52. struct cgroup_file events_local_file[HUGE_MAX_HSTATE];
  53. struct hugetlb_cgroup_per_node *nodeinfo[];
  54. };
  55. static inline struct hugetlb_cgroup *
  56. __hugetlb_cgroup_from_page(struct page *page, bool rsvd)
  57. {
  58. VM_BUG_ON_PAGE(!PageHuge(page), page);
  59. if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
  60. return NULL;
  61. if (rsvd)
  62. return (void *)page_private(page + SUBPAGE_INDEX_CGROUP_RSVD);
  63. else
  64. return (void *)page_private(page + SUBPAGE_INDEX_CGROUP);
  65. }
  66. static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
  67. {
  68. return __hugetlb_cgroup_from_page(page, false);
  69. }
  70. static inline struct hugetlb_cgroup *
  71. hugetlb_cgroup_from_page_rsvd(struct page *page)
  72. {
  73. return __hugetlb_cgroup_from_page(page, true);
  74. }
  75. static inline void __set_hugetlb_cgroup(struct page *page,
  76. struct hugetlb_cgroup *h_cg, bool rsvd)
  77. {
  78. VM_BUG_ON_PAGE(!PageHuge(page), page);
  79. if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
  80. return;
  81. if (rsvd)
  82. set_page_private(page + SUBPAGE_INDEX_CGROUP_RSVD,
  83. (unsigned long)h_cg);
  84. else
  85. set_page_private(page + SUBPAGE_INDEX_CGROUP,
  86. (unsigned long)h_cg);
  87. }
  88. static inline void set_hugetlb_cgroup(struct page *page,
  89. struct hugetlb_cgroup *h_cg)
  90. {
  91. __set_hugetlb_cgroup(page, h_cg, false);
  92. }
  93. static inline void set_hugetlb_cgroup_rsvd(struct page *page,
  94. struct hugetlb_cgroup *h_cg)
  95. {
  96. __set_hugetlb_cgroup(page, h_cg, true);
  97. }
  98. static inline bool hugetlb_cgroup_disabled(void)
  99. {
  100. return !cgroup_subsys_enabled(hugetlb_cgrp_subsys);
  101. }
  102. static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
  103. {
  104. css_put(&h_cg->css);
  105. }
  106. static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
  107. struct resv_map *resv_map)
  108. {
  109. if (resv_map->css)
  110. css_get(resv_map->css);
  111. }
  112. static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
  113. struct resv_map *resv_map)
  114. {
  115. if (resv_map->css)
  116. css_put(resv_map->css);
  117. }
  118. extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
  119. struct hugetlb_cgroup **ptr);
  120. extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
  121. struct hugetlb_cgroup **ptr);
  122. extern void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
  123. struct hugetlb_cgroup *h_cg,
  124. struct page *page);
  125. extern void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
  126. struct hugetlb_cgroup *h_cg,
  127. struct page *page);
  128. extern void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
  129. struct page *page);
  130. extern void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages,
  131. struct page *page);
  132. extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
  133. struct hugetlb_cgroup *h_cg);
  134. extern void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
  135. struct hugetlb_cgroup *h_cg);
  136. extern void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
  137. unsigned long start,
  138. unsigned long end);
  139. extern void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
  140. struct file_region *rg,
  141. unsigned long nr_pages,
  142. bool region_del);
  143. extern void hugetlb_cgroup_file_init(void) __init;
  144. extern void hugetlb_cgroup_migrate(struct page *oldhpage,
  145. struct page *newhpage);
  146. #else
  147. static inline void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
  148. struct file_region *rg,
  149. unsigned long nr_pages,
  150. bool region_del)
  151. {
  152. }
  153. static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
  154. {
  155. return NULL;
  156. }
  157. static inline struct hugetlb_cgroup *
  158. hugetlb_cgroup_from_page_resv(struct page *page)
  159. {
  160. return NULL;
  161. }
  162. static inline struct hugetlb_cgroup *
  163. hugetlb_cgroup_from_page_rsvd(struct page *page)
  164. {
  165. return NULL;
  166. }
  167. static inline void set_hugetlb_cgroup(struct page *page,
  168. struct hugetlb_cgroup *h_cg)
  169. {
  170. }
  171. static inline void set_hugetlb_cgroup_rsvd(struct page *page,
  172. struct hugetlb_cgroup *h_cg)
  173. {
  174. }
  175. static inline bool hugetlb_cgroup_disabled(void)
  176. {
  177. return true;
  178. }
  179. static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
  180. {
  181. }
  182. static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
  183. struct resv_map *resv_map)
  184. {
  185. }
  186. static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
  187. struct resv_map *resv_map)
  188. {
  189. }
  190. static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
  191. struct hugetlb_cgroup **ptr)
  192. {
  193. return 0;
  194. }
  195. static inline int hugetlb_cgroup_charge_cgroup_rsvd(int idx,
  196. unsigned long nr_pages,
  197. struct hugetlb_cgroup **ptr)
  198. {
  199. return 0;
  200. }
  201. static inline void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
  202. struct hugetlb_cgroup *h_cg,
  203. struct page *page)
  204. {
  205. }
  206. static inline void
  207. hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
  208. struct hugetlb_cgroup *h_cg,
  209. struct page *page)
  210. {
  211. }
  212. static inline void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
  213. struct page *page)
  214. {
  215. }
  216. static inline void hugetlb_cgroup_uncharge_page_rsvd(int idx,
  217. unsigned long nr_pages,
  218. struct page *page)
  219. {
  220. }
  221. static inline void hugetlb_cgroup_uncharge_cgroup(int idx,
  222. unsigned long nr_pages,
  223. struct hugetlb_cgroup *h_cg)
  224. {
  225. }
  226. static inline void
  227. hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
  228. struct hugetlb_cgroup *h_cg)
  229. {
  230. }
  231. static inline void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
  232. unsigned long start,
  233. unsigned long end)
  234. {
  235. }
  236. static inline void hugetlb_cgroup_file_init(void)
  237. {
  238. }
  239. static inline void hugetlb_cgroup_migrate(struct page *oldhpage,
  240. struct page *newhpage)
  241. {
  242. }
  243. #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */
  244. #endif