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 {
40 struct cgroup_subsys_state css;
43 * the counter to account for hugepages from hugetlb.
45 struct page_counter hugepage[HUGE_MAX_HSTATE];
48 * the counter to account for hugepage reservations from hugetlb.
50 struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE];
52 atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
53 atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
55 /* Handle for "hugetlb.events" */
56 struct cgroup_file events_file[HUGE_MAX_HSTATE];
58 /* Handle for "hugetlb.events.local" */
59 struct cgroup_file events_local_file[HUGE_MAX_HSTATE];
62 static inline struct hugetlb_cgroup *
63 __hugetlb_cgroup_from_page(struct page *page, bool rsvd)
65 VM_BUG_ON_PAGE(!PageHuge(page), page);
67 if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
70 return (void *)page_private(page + SUBPAGE_INDEX_CGROUP_RSVD);
72 return (void *)page_private(page + SUBPAGE_INDEX_CGROUP);
75 static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
77 return __hugetlb_cgroup_from_page(page, false);
80 static inline struct hugetlb_cgroup *
81 hugetlb_cgroup_from_page_rsvd(struct page *page)
83 return __hugetlb_cgroup_from_page(page, true);
86 static inline int __set_hugetlb_cgroup(struct page *page,
87 struct hugetlb_cgroup *h_cg, bool rsvd)
89 VM_BUG_ON_PAGE(!PageHuge(page), page);
91 if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER)
94 set_page_private(page + SUBPAGE_INDEX_CGROUP_RSVD,
97 set_page_private(page + SUBPAGE_INDEX_CGROUP,
102 static inline int set_hugetlb_cgroup(struct page *page,
103 struct hugetlb_cgroup *h_cg)
105 return __set_hugetlb_cgroup(page, h_cg, false);
108 static inline int set_hugetlb_cgroup_rsvd(struct page *page,
109 struct hugetlb_cgroup *h_cg)
111 return __set_hugetlb_cgroup(page, h_cg, true);
114 static inline bool hugetlb_cgroup_disabled(void)
116 return !cgroup_subsys_enabled(hugetlb_cgrp_subsys);
119 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
124 static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
125 struct resv_map *resv_map)
128 css_get(resv_map->css);
131 extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
132 struct hugetlb_cgroup **ptr);
133 extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
134 struct hugetlb_cgroup **ptr);
135 extern void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
136 struct hugetlb_cgroup *h_cg,
138 extern void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
139 struct hugetlb_cgroup *h_cg,
141 extern void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
143 extern void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages,
146 extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
147 struct hugetlb_cgroup *h_cg);
148 extern void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
149 struct hugetlb_cgroup *h_cg);
150 extern void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
154 extern void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
155 struct file_region *rg,
156 unsigned long nr_pages,
159 extern void hugetlb_cgroup_file_init(void) __init;
160 extern void hugetlb_cgroup_migrate(struct page *oldhpage,
161 struct page *newhpage);
164 static inline void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
165 struct file_region *rg,
166 unsigned long nr_pages,
171 static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)
176 static inline struct hugetlb_cgroup *
177 hugetlb_cgroup_from_page_resv(struct page *page)
182 static inline struct hugetlb_cgroup *
183 hugetlb_cgroup_from_page_rsvd(struct page *page)
188 static inline int set_hugetlb_cgroup(struct page *page,
189 struct hugetlb_cgroup *h_cg)
194 static inline int set_hugetlb_cgroup_rsvd(struct page *page,
195 struct hugetlb_cgroup *h_cg)
200 static inline bool hugetlb_cgroup_disabled(void)
205 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
209 static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
210 struct resv_map *resv_map)
214 static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
215 struct hugetlb_cgroup **ptr)
220 static inline int hugetlb_cgroup_charge_cgroup_rsvd(int idx,
221 unsigned long nr_pages,
222 struct hugetlb_cgroup **ptr)
227 static inline void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
228 struct hugetlb_cgroup *h_cg,
234 hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
235 struct hugetlb_cgroup *h_cg,
240 static inline void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
245 static inline void hugetlb_cgroup_uncharge_page_rsvd(int idx,
246 unsigned long nr_pages,
250 static inline void hugetlb_cgroup_uncharge_cgroup(int idx,
251 unsigned long nr_pages,
252 struct hugetlb_cgroup *h_cg)
257 hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
258 struct hugetlb_cgroup *h_cg)
262 static inline void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
268 static inline void hugetlb_cgroup_file_init(void)
272 static inline void hugetlb_cgroup_migrate(struct page *oldhpage,
273 struct page *newhpage)
277 #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */