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
25 enum hugetlb_memory_event {
27 HUGETLB_NR_MEMORY_EVENTS,
30 struct hugetlb_cgroup_per_node {
31 /* hugetlb usage in pages over all hstates. */
32 unsigned long usage[HUGE_MAX_HSTATE];
35 struct hugetlb_cgroup {
36 struct cgroup_subsys_state css;
39 * the counter to account for hugepages from hugetlb.
41 struct page_counter hugepage[HUGE_MAX_HSTATE];
44 * the counter to account for hugepage reservations from hugetlb.
46 struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE];
48 atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
49 atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
51 /* Handle for "hugetlb.events" */
52 struct cgroup_file events_file[HUGE_MAX_HSTATE];
54 /* Handle for "hugetlb.events.local" */
55 struct cgroup_file events_local_file[HUGE_MAX_HSTATE];
57 struct hugetlb_cgroup_per_node *nodeinfo[];
60 static inline struct hugetlb_cgroup *
61 __hugetlb_cgroup_from_folio(struct folio *folio, bool rsvd)
63 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
65 return folio->_hugetlb_cgroup_rsvd;
67 return folio->_hugetlb_cgroup;
70 static inline struct hugetlb_cgroup *hugetlb_cgroup_from_folio(struct folio *folio)
72 return __hugetlb_cgroup_from_folio(folio, false);
75 static inline struct hugetlb_cgroup *
76 hugetlb_cgroup_from_folio_rsvd(struct folio *folio)
78 return __hugetlb_cgroup_from_folio(folio, true);
81 static inline void __set_hugetlb_cgroup(struct folio *folio,
82 struct hugetlb_cgroup *h_cg, bool rsvd)
84 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
86 folio->_hugetlb_cgroup_rsvd = h_cg;
88 folio->_hugetlb_cgroup = h_cg;
91 static inline void set_hugetlb_cgroup(struct folio *folio,
92 struct hugetlb_cgroup *h_cg)
94 __set_hugetlb_cgroup(folio, h_cg, false);
97 static inline void set_hugetlb_cgroup_rsvd(struct folio *folio,
98 struct hugetlb_cgroup *h_cg)
100 __set_hugetlb_cgroup(folio, h_cg, true);
103 static inline bool hugetlb_cgroup_disabled(void)
105 return !cgroup_subsys_enabled(hugetlb_cgrp_subsys);
108 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
113 static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
114 struct resv_map *resv_map)
117 css_get(resv_map->css);
120 static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
121 struct resv_map *resv_map)
124 css_put(resv_map->css);
127 extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
128 struct hugetlb_cgroup **ptr);
129 extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
130 struct hugetlb_cgroup **ptr);
131 extern void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
132 struct hugetlb_cgroup *h_cg,
133 struct folio *folio);
134 extern void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
135 struct hugetlb_cgroup *h_cg,
136 struct folio *folio);
137 extern void hugetlb_cgroup_uncharge_folio(int idx, unsigned long nr_pages,
138 struct folio *folio);
139 extern void hugetlb_cgroup_uncharge_folio_rsvd(int idx, unsigned long nr_pages,
140 struct folio *folio);
142 extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
143 struct hugetlb_cgroup *h_cg);
144 extern void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
145 struct hugetlb_cgroup *h_cg);
146 extern void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
150 extern void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
151 struct file_region *rg,
152 unsigned long nr_pages,
155 extern void hugetlb_cgroup_file_init(void) __init;
156 extern void hugetlb_cgroup_migrate(struct folio *old_folio,
157 struct folio *new_folio);
160 static inline void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
161 struct file_region *rg,
162 unsigned long nr_pages,
167 static inline struct hugetlb_cgroup *hugetlb_cgroup_from_folio(struct folio *folio)
172 static inline struct hugetlb_cgroup *
173 hugetlb_cgroup_from_folio_rsvd(struct folio *folio)
178 static inline void set_hugetlb_cgroup(struct folio *folio,
179 struct hugetlb_cgroup *h_cg)
183 static inline void set_hugetlb_cgroup_rsvd(struct folio *folio,
184 struct hugetlb_cgroup *h_cg)
188 static inline bool hugetlb_cgroup_disabled(void)
193 static inline void hugetlb_cgroup_put_rsvd_cgroup(struct hugetlb_cgroup *h_cg)
197 static inline void resv_map_dup_hugetlb_cgroup_uncharge_info(
198 struct resv_map *resv_map)
202 static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
203 struct resv_map *resv_map)
207 static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
208 struct hugetlb_cgroup **ptr)
213 static inline int hugetlb_cgroup_charge_cgroup_rsvd(int idx,
214 unsigned long nr_pages,
215 struct hugetlb_cgroup **ptr)
220 static inline void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
221 struct hugetlb_cgroup *h_cg,
227 hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
228 struct hugetlb_cgroup *h_cg,
233 static inline void hugetlb_cgroup_uncharge_folio(int idx, unsigned long nr_pages,
238 static inline void hugetlb_cgroup_uncharge_folio_rsvd(int idx,
239 unsigned long nr_pages,
243 static inline void hugetlb_cgroup_uncharge_cgroup(int idx,
244 unsigned long nr_pages,
245 struct hugetlb_cgroup *h_cg)
250 hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
251 struct hugetlb_cgroup *h_cg)
255 static inline void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,
261 static inline void hugetlb_cgroup_file_init(void)
265 static inline void hugetlb_cgroup_migrate(struct folio *old_folio,
266 struct folio *new_folio)
270 #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */