2 * Copyright IBM Corporation, 2012
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
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.
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.
15 #ifndef _LINUX_HUGETLB_CGROUP_H
16 #define _LINUX_HUGETLB_CGROUP_H
18 #include <linux/mmdebug.h>
20 struct hugetlb_cgroup;
24 #ifdef CONFIG_CGROUP_HUGETLB
26 * Minimum page order trackable by hugetlb cgroup.
27 * At least 4 pages are necessary for all the tracking information.
28 * The second tail page (hpage[SUBPAGE_INDEX_CGROUP]) is the fault
29 * usage cgroup. The third tail page (hpage[SUBPAGE_INDEX_CGROUP_RSVD])
30 * is the reservation usage cgroup.
32 #define HUGETLB_CGROUP_MIN_ORDER order_base_2(__MAX_CGROUP_SUBPAGE_INDEX + 1)
34 enum hugetlb_memory_event {
36 HUGETLB_NR_MEMORY_EVENTS,
39 struct hugetlb_cgroup_per_node {
40 /* hugetlb usage in pages over all hstates. */
41 unsigned long usage[HUGE_MAX_HSTATE];
44 struct hugetlb_cgroup {
45 struct cgroup_subsys_state css;
48 * the counter to account for hugepages from hugetlb.
50 struct page_counter hugepage[HUGE_MAX_HSTATE];
53 * the counter to account for hugepage reservations from hugetlb.
55 struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE];
57 atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
58 atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
60 /* Handle for "hugetlb.events" */
61 struct cgroup_file events_file[HUGE_MAX_HSTATE];
63 /* Handle for "hugetlb.events.local" */
64 struct cgroup_file events_local_file[HUGE_MAX_HSTATE];
66 struct hugetlb_cgroup_per_node *nodeinfo[];
69 static inline struct hugetlb_cgroup *
70 __hugetlb_cgroup_from_page(struct page *page, bool rsvd)
72 VM_BUG_ON_PAGE(!PageHuge(page), page);
74 if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
77 return (void *)page_private(page + SUBPAGE_INDEX_CGROUP_RSVD);
79 return (void *)page_private(page + SUBPAGE_INDEX_CGROUP);
82 static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
84 return __hugetlb_cgroup_from_page(page, false);
87 static inline struct hugetlb_cgroup *
88 hugetlb_cgroup_from_page_rsvd(struct page *page)
90 return __hugetlb_cgroup_from_page(page, true);
93 static inline void __set_hugetlb_cgroup(struct page *page,
94 struct hugetlb_cgroup *h_cg, bool rsvd)
96 VM_BUG_ON_PAGE(!PageHuge(page), page);
98 if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
101 set_page_private(page + SUBPAGE_INDEX_CGROUP_RSVD,
102 (unsigned long)h_cg);
104 set_page_private(page + SUBPAGE_INDEX_CGROUP,
105 (unsigned long)h_cg);
108 static inline void set_hugetlb_cgroup(struct page *page,
109 struct hugetlb_cgroup *h_cg)
111 __set_hugetlb_cgroup(page, h_cg, false);
114 static inline void set_hugetlb_cgroup_rsvd(struct page *page,
115 struct hugetlb_cgroup *h_cg)
117 __set_hugetlb_cgroup(page, h_cg, true);
120 static inline bool hugetlb_cgroup_disabled(void)
122 return !cgroup_subsys_enabled(hugetlb_cgrp_subsys);
125 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
130 static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
131 struct resv_map *resv_map)
134 css_get(resv_map->css);
137 static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
138 struct resv_map *resv_map)
141 css_put(resv_map->css);
144 extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
145 struct hugetlb_cgroup **ptr);
146 extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
147 struct hugetlb_cgroup **ptr);
148 extern void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
149 struct hugetlb_cgroup *h_cg,
151 extern void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
152 struct hugetlb_cgroup *h_cg,
154 extern void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
156 extern void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages,
159 extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
160 struct hugetlb_cgroup *h_cg);
161 extern void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
162 struct hugetlb_cgroup *h_cg);
163 extern void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
167 extern void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
168 struct file_region *rg,
169 unsigned long nr_pages,
172 extern void hugetlb_cgroup_file_init(void) __init;
173 extern void hugetlb_cgroup_migrate(struct page *oldhpage,
174 struct page *newhpage);
177 static inline void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
178 struct file_region *rg,
179 unsigned long nr_pages,
184 static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
189 static inline struct hugetlb_cgroup *
190 hugetlb_cgroup_from_page_resv(struct page *page)
195 static inline struct hugetlb_cgroup *
196 hugetlb_cgroup_from_page_rsvd(struct page *page)
201 static inline void set_hugetlb_cgroup(struct page *page,
202 struct hugetlb_cgroup *h_cg)
206 static inline void set_hugetlb_cgroup_rsvd(struct page *page,
207 struct hugetlb_cgroup *h_cg)
211 static inline bool hugetlb_cgroup_disabled(void)
216 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
220 static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
221 struct resv_map *resv_map)
225 static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
226 struct resv_map *resv_map)
230 static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
231 struct hugetlb_cgroup **ptr)
236 static inline int hugetlb_cgroup_charge_cgroup_rsvd(int idx,
237 unsigned long nr_pages,
238 struct hugetlb_cgroup **ptr)
243 static inline void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
244 struct hugetlb_cgroup *h_cg,
250 hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
251 struct hugetlb_cgroup *h_cg,
256 static inline void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
261 static inline void hugetlb_cgroup_uncharge_page_rsvd(int idx,
262 unsigned long nr_pages,
266 static inline void hugetlb_cgroup_uncharge_cgroup(int idx,
267 unsigned long nr_pages,
268 struct hugetlb_cgroup *h_cg)
273 hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
274 struct hugetlb_cgroup *h_cg)
278 static inline void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
284 static inline void hugetlb_cgroup_file_init(void)
288 static inline void hugetlb_cgroup_migrate(struct page *oldhpage,
289 struct page *newhpage)
293 #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */