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
;
25 * Minimum page order trackable by hugetlb cgroup.
26 * At least 4 pages are necessary for all the tracking information.
27 * The second tail page (hpage[2]) is the fault usage cgroup.
28 * The third tail page (hpage[3]) is the reservation usage cgroup.
30 #define HUGETLB_CGROUP_MIN_ORDER 2
32 #ifdef CONFIG_CGROUP_HUGETLB
33 enum hugetlb_memory_event
{
35 HUGETLB_NR_MEMORY_EVENTS
,
38 struct hugetlb_cgroup
{
39 struct cgroup_subsys_state css
;
42 * the counter to account for hugepages from hugetlb.
44 struct page_counter hugepage
[HUGE_MAX_HSTATE
];
47 * the counter to account for hugepage reservations from hugetlb.
49 struct page_counter rsvd_hugepage
[HUGE_MAX_HSTATE
];
51 atomic_long_t events
[HUGE_MAX_HSTATE
][HUGETLB_NR_MEMORY_EVENTS
];
52 atomic_long_t events_local
[HUGE_MAX_HSTATE
][HUGETLB_NR_MEMORY_EVENTS
];
54 /* Handle for "hugetlb.events" */
55 struct cgroup_file events_file
[HUGE_MAX_HSTATE
];
57 /* Handle for "hugetlb.events.local" */
58 struct cgroup_file events_local_file
[HUGE_MAX_HSTATE
];
61 static inline struct hugetlb_cgroup
*
62 __hugetlb_cgroup_from_page(struct page
*page
, bool rsvd
)
64 VM_BUG_ON_PAGE(!PageHuge(page
), page
);
66 if (compound_order(page
) < HUGETLB_CGROUP_MIN_ORDER
)
69 return (struct hugetlb_cgroup
*)page
[3].private;
71 return (struct hugetlb_cgroup
*)page
[2].private;
74 static inline struct hugetlb_cgroup
*hugetlb_cgroup_from_page(struct page
*page
)
76 return __hugetlb_cgroup_from_page(page
, false);
79 static inline struct hugetlb_cgroup
*
80 hugetlb_cgroup_from_page_rsvd(struct page
*page
)
82 return __hugetlb_cgroup_from_page(page
, true);
85 static inline int __set_hugetlb_cgroup(struct page
*page
,
86 struct hugetlb_cgroup
*h_cg
, bool rsvd
)
88 VM_BUG_ON_PAGE(!PageHuge(page
), page
);
90 if (compound_order(page
) < HUGETLB_CGROUP_MIN_ORDER
)
93 page
[3].private = (unsigned long)h_cg
;
95 page
[2].private = (unsigned long)h_cg
;
99 static inline int set_hugetlb_cgroup(struct page
*page
,
100 struct hugetlb_cgroup
*h_cg
)
102 return __set_hugetlb_cgroup(page
, h_cg
, false);
105 static inline int set_hugetlb_cgroup_rsvd(struct page
*page
,
106 struct hugetlb_cgroup
*h_cg
)
108 return __set_hugetlb_cgroup(page
, h_cg
, true);
111 static inline bool hugetlb_cgroup_disabled(void)
113 return !cgroup_subsys_enabled(hugetlb_cgrp_subsys
);
116 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup
*h_cg
)
121 extern int hugetlb_cgroup_charge_cgroup(int idx
, unsigned long nr_pages
,
122 struct hugetlb_cgroup
**ptr
);
123 extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx
, unsigned long nr_pages
,
124 struct hugetlb_cgroup
**ptr
);
125 extern void hugetlb_cgroup_commit_charge(int idx
, unsigned long nr_pages
,
126 struct hugetlb_cgroup
*h_cg
,
128 extern void hugetlb_cgroup_commit_charge_rsvd(int idx
, unsigned long nr_pages
,
129 struct hugetlb_cgroup
*h_cg
,
131 extern void hugetlb_cgroup_uncharge_page(int idx
, unsigned long nr_pages
,
133 extern void hugetlb_cgroup_uncharge_page_rsvd(int idx
, unsigned long nr_pages
,
136 extern void hugetlb_cgroup_uncharge_cgroup(int idx
, unsigned long nr_pages
,
137 struct hugetlb_cgroup
*h_cg
);
138 extern void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx
, unsigned long nr_pages
,
139 struct hugetlb_cgroup
*h_cg
);
140 extern void hugetlb_cgroup_uncharge_counter(struct resv_map
*resv
,
144 extern void hugetlb_cgroup_uncharge_file_region(struct resv_map
*resv
,
145 struct file_region
*rg
,
146 unsigned long nr_pages
,
149 extern void hugetlb_cgroup_file_init(void) __init
;
150 extern void hugetlb_cgroup_migrate(struct page
*oldhpage
,
151 struct page
*newhpage
);
154 static inline void hugetlb_cgroup_uncharge_file_region(struct resv_map
*resv
,
155 struct file_region
*rg
,
156 unsigned long nr_pages
,
161 static inline struct hugetlb_cgroup
*hugetlb_cgroup_from_page(struct page
*page
)
166 static inline struct hugetlb_cgroup
*
167 hugetlb_cgroup_from_page_resv(struct page
*page
)
172 static inline struct hugetlb_cgroup
*
173 hugetlb_cgroup_from_page_rsvd(struct page
*page
)
178 static inline int set_hugetlb_cgroup(struct page
*page
,
179 struct hugetlb_cgroup
*h_cg
)
184 static inline int set_hugetlb_cgroup_rsvd(struct page
*page
,
185 struct hugetlb_cgroup
*h_cg
)
190 static inline bool hugetlb_cgroup_disabled(void)
195 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup
*h_cg
)
199 static inline int hugetlb_cgroup_charge_cgroup(int idx
, unsigned long nr_pages
,
200 struct hugetlb_cgroup
**ptr
)
205 static inline int hugetlb_cgroup_charge_cgroup_rsvd(int idx
,
206 unsigned long nr_pages
,
207 struct hugetlb_cgroup
**ptr
)
212 static inline void hugetlb_cgroup_commit_charge(int idx
, unsigned long nr_pages
,
213 struct hugetlb_cgroup
*h_cg
,
219 hugetlb_cgroup_commit_charge_rsvd(int idx
, unsigned long nr_pages
,
220 struct hugetlb_cgroup
*h_cg
,
225 static inline void hugetlb_cgroup_uncharge_page(int idx
, unsigned long nr_pages
,
230 static inline void hugetlb_cgroup_uncharge_page_rsvd(int idx
,
231 unsigned long nr_pages
,
235 static inline void hugetlb_cgroup_uncharge_cgroup(int idx
,
236 unsigned long nr_pages
,
237 struct hugetlb_cgroup
*h_cg
)
242 hugetlb_cgroup_uncharge_cgroup_rsvd(int idx
, unsigned long nr_pages
,
243 struct hugetlb_cgroup
*h_cg
)
247 static inline void hugetlb_cgroup_uncharge_counter(struct resv_map
*resv
,
253 static inline void hugetlb_cgroup_file_init(void)
257 static inline void hugetlb_cgroup_migrate(struct page
*oldhpage
,
258 struct page
*newhpage
)
262 #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */