1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/cgroup.h>
17#include <linux/page_counter.h>
18#include <linux/slab.h>
19#include <linux/hugetlb.h>
20#include <linux/hugetlb_cgroup.h>
21
22struct hugetlb_cgroup {
23 struct cgroup_subsys_state css;
24
25
26
27 struct page_counter hugepage[HUGE_MAX_HSTATE];
28};
29
30#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
31#define MEMFILE_IDX(val) (((val) >> 16) & 0xffff)
32#define MEMFILE_ATTR(val) ((val) & 0xffff)
33
34struct cgroup_subsys hugetlb_subsys __read_mostly;
35static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
36
37static inline
38struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
39{
40 return container_of(s, struct hugetlb_cgroup, css);
41}
42
43static inline
44struct hugetlb_cgroup *hugetlb_cgroup_from_cgroup(struct cgroup *cgroup)
45{
46 return hugetlb_cgroup_from_css(cgroup_subsys_state(cgroup,
47 hugetlb_subsys_id));
48}
49
50static inline
51struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task)
52{
53 return hugetlb_cgroup_from_css(task_subsys_state(task,
54 hugetlb_subsys_id));
55}
56
57static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
58{
59 return (h_cg == root_h_cgroup);
60}
61
62static inline struct hugetlb_cgroup *parent_hugetlb_cgroup(struct cgroup *cg)
63{
64 if (!cg->parent)
65 return NULL;
66 return hugetlb_cgroup_from_cgroup(cg->parent);
67}
68
69static inline bool hugetlb_cgroup_have_usage(struct cgroup *cg)
70{
71 int idx;
72 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cg);
73
74 for (idx = 0; idx < hugetlb_max_hstate; idx++) {
75 if (page_counter_read(&h_cg->hugepage[idx]))
76 return true;
77 }
78 return false;
79}
80
81static struct cgroup_subsys_state *hugetlb_cgroup_css_alloc(struct cgroup *cgroup)
82{
83 int idx;
84 struct cgroup *parent_cgroup;
85 struct hugetlb_cgroup *h_cgroup, *parent_h_cgroup;
86
87 h_cgroup = kzalloc(sizeof(*h_cgroup), GFP_KERNEL);
88 if (!h_cgroup)
89 return ERR_PTR(-ENOMEM);
90
91 parent_cgroup = cgroup->parent;
92 if (parent_cgroup) {
93 parent_h_cgroup = hugetlb_cgroup_from_cgroup(parent_cgroup);
94 for (idx = 0; idx < HUGE_MAX_HSTATE; idx++)
95 page_counter_init(&h_cgroup->hugepage[idx],
96 &parent_h_cgroup->hugepage[idx]);
97 } else {
98 root_h_cgroup = h_cgroup;
99 for (idx = 0; idx < HUGE_MAX_HSTATE; idx++)
100 page_counter_init(&h_cgroup->hugepage[idx], NULL);
101 }
102 return &h_cgroup->css;
103}
104
105static void hugetlb_cgroup_css_free(struct cgroup *cgroup)
106{
107 struct hugetlb_cgroup *h_cgroup;
108
109 h_cgroup = hugetlb_cgroup_from_cgroup(cgroup);
110 kfree(h_cgroup);
111}
112
113
114
115
116
117
118
119
120
121static void hugetlb_cgroup_move_parent(int idx, struct cgroup *cgroup,
122 struct page *page)
123{
124 unsigned int nr_pages;
125 struct page_counter *counter;
126 struct hugetlb_cgroup *page_hcg;
127 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
128 struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(cgroup);
129
130 page_hcg = hugetlb_cgroup_from_page(page);
131
132
133
134
135
136 if (!page_hcg || page_hcg != h_cg)
137 goto out;
138
139 nr_pages = 1 << compound_order(page);
140 if (!parent) {
141 parent = root_h_cgroup;
142
143 page_counter_charge(&parent->hugepage[idx], nr_pages);
144 }
145 counter = &h_cg->hugepage[idx];
146
147 page_counter_cancel(counter, nr_pages);
148
149 set_hugetlb_cgroup(page, parent);
150out:
151 return;
152}
153
154
155
156
157
158static void hugetlb_cgroup_css_offline(struct cgroup *cgroup)
159{
160 struct hstate *h;
161 struct page *page;
162 int idx = 0;
163
164 do {
165 for_each_hstate(h) {
166 spin_lock(&hugetlb_lock);
167 list_for_each_entry(page, &h->hugepage_activelist, lru)
168 hugetlb_cgroup_move_parent(idx, cgroup, page);
169
170 spin_unlock(&hugetlb_lock);
171 idx++;
172 }
173 cond_resched();
174 } while (hugetlb_cgroup_have_usage(cgroup));
175}
176
177int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
178 struct hugetlb_cgroup **ptr)
179{
180 int ret = 0;
181 struct page_counter *counter;
182 struct hugetlb_cgroup *h_cg = NULL;
183
184 if (hugetlb_cgroup_disabled())
185 goto done;
186
187
188
189
190 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
191 goto done;
192again:
193 rcu_read_lock();
194 h_cg = hugetlb_cgroup_from_task(current);
195 if (!css_tryget(&h_cg->css)) {
196 rcu_read_unlock();
197 goto again;
198 }
199 rcu_read_unlock();
200
201 if (!page_counter_try_charge(&h_cg->hugepage[idx], nr_pages, &counter))
202 ret = -ENOMEM;
203 css_put(&h_cg->css);
204done:
205 *ptr = h_cg;
206 return ret;
207}
208
209
210void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
211 struct hugetlb_cgroup *h_cg,
212 struct page *page)
213{
214 if (hugetlb_cgroup_disabled() || !h_cg)
215 return;
216
217 set_hugetlb_cgroup(page, h_cg);
218 return;
219}
220
221
222
223
224void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
225 struct page *page)
226{
227 struct hugetlb_cgroup *h_cg;
228
229 if (hugetlb_cgroup_disabled())
230 return;
231 VM_BUG_ON(!spin_is_locked(&hugetlb_lock));
232 h_cg = hugetlb_cgroup_from_page(page);
233 if (unlikely(!h_cg))
234 return;
235 set_hugetlb_cgroup(page, NULL);
236 page_counter_uncharge(&h_cg->hugepage[idx], nr_pages);
237 return;
238}
239
240void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
241 struct hugetlb_cgroup *h_cg)
242{
243 if (hugetlb_cgroup_disabled() || !h_cg)
244 return;
245
246 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
247 return;
248
249 page_counter_uncharge(&h_cg->hugepage[idx], nr_pages);
250 return;
251}
252
253enum {
254 RES_USAGE,
255 RES_LIMIT,
256 RES_MAX_USAGE,
257 RES_FAILCNT,
258};
259
260static ssize_t hugetlb_cgroup_read(struct cgroup *cgroup, struct cftype *cft,
261 struct file *file, char __user *buf,
262 size_t nbytes, loff_t *ppos)
263{
264 u64 val;
265 char str[64];
266 int len;
267 struct page_counter *counter;
268 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
269
270 counter = &h_cg->hugepage[MEMFILE_IDX(cft->private)];
271
272 switch (MEMFILE_ATTR(cft->private)) {
273 case RES_USAGE:
274 val = (u64)page_counter_read(counter) * PAGE_SIZE;
275 break;
276 case RES_LIMIT:
277 val = (u64)counter->limit * PAGE_SIZE;
278 break;
279 case RES_MAX_USAGE:
280 val = (u64)counter->watermark * PAGE_SIZE;
281 break;
282 case RES_FAILCNT:
283 val = counter->failcnt;
284 break;
285 default:
286 BUG();
287 }
288
289 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
290 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
291}
292
293static DEFINE_MUTEX(hugetlb_limit_mutex);
294
295static int hugetlb_cgroup_write(struct cgroup *cgroup, struct cftype *cft,
296 const char *buffer)
297{
298 int idx, ret;
299 unsigned long nr_pages;
300 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
301
302 if (hugetlb_cgroup_is_root(h_cg))
303 return -EINVAL;
304
305 ret = page_counter_memparse(buffer, &nr_pages);
306 if (ret)
307 return ret;
308
309 idx = MEMFILE_IDX(cft->private);
310
311 switch (MEMFILE_ATTR(cft->private)) {
312 case RES_LIMIT:
313 mutex_lock(&hugetlb_limit_mutex);
314 ret = page_counter_limit(&h_cg->hugepage[idx], nr_pages);
315 mutex_unlock(&hugetlb_limit_mutex);
316 break;
317 default:
318 ret = -EINVAL;
319 break;
320 }
321 return ret;
322}
323
324static int hugetlb_cgroup_reset(struct cgroup *cgroup, unsigned int event)
325{
326 int ret = 0;
327 struct page_counter *counter;
328 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
329
330 counter = &h_cg->hugepage[MEMFILE_IDX(event)];
331
332 switch (MEMFILE_ATTR(event)) {
333 case RES_MAX_USAGE:
334 page_counter_reset_watermark(counter);
335 break;
336 case RES_FAILCNT:
337 counter->failcnt = 0;
338 break;
339 default:
340 ret = -EINVAL;
341 break;
342 }
343 return ret;
344}
345
346static char *mem_fmt(char *buf, int size, unsigned long hsize)
347{
348 if (hsize >= (1UL << 30))
349 snprintf(buf, size, "%luGB", hsize >> 30);
350 else if (hsize >= (1UL << 20))
351 snprintf(buf, size, "%luMB", hsize >> 20);
352 else
353 snprintf(buf, size, "%luKB", hsize >> 10);
354 return buf;
355}
356
357static void __init __hugetlb_cgroup_file_init(int idx)
358{
359 char buf[32];
360 struct cftype *cft;
361 struct hstate *h = &hstates[idx];
362
363
364 mem_fmt(buf, 32, huge_page_size(h));
365
366
367 cft = &h->cgroup_files[0];
368 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
369 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
370 cft->read = hugetlb_cgroup_read;
371 cft->write_string = hugetlb_cgroup_write;
372
373
374 cft = &h->cgroup_files[1];
375 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
376 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
377 cft->read = hugetlb_cgroup_read;
378
379
380 cft = &h->cgroup_files[2];
381 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
382 cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
383 cft->trigger = hugetlb_cgroup_reset;
384 cft->read = hugetlb_cgroup_read;
385
386
387 cft = &h->cgroup_files[3];
388 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
389 cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
390 cft->trigger = hugetlb_cgroup_reset;
391 cft->read = hugetlb_cgroup_read;
392
393
394 cft = &h->cgroup_files[4];
395 memset(cft, 0, sizeof(*cft));
396
397 WARN_ON(cgroup_add_cftypes(&hugetlb_subsys, h->cgroup_files));
398
399 return;
400}
401
402void __init hugetlb_cgroup_file_init(void)
403{
404 struct hstate *h;
405
406 for_each_hstate(h) {
407
408
409
410
411
412 if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER)
413 __hugetlb_cgroup_file_init(hstate_index(h));
414 }
415}
416
417
418
419
420
421void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)
422{
423 struct hugetlb_cgroup *h_cg;
424 struct hstate *h = page_hstate(oldhpage);
425
426 if (hugetlb_cgroup_disabled())
427 return;
428
429 VM_BUG_ON_PAGE(!PageHuge(oldhpage), oldhpage);
430 spin_lock(&hugetlb_lock);
431 h_cg = hugetlb_cgroup_from_page(oldhpage);
432 set_hugetlb_cgroup(oldhpage, NULL);
433
434
435 set_hugetlb_cgroup(newhpage, h_cg);
436 list_move(&newhpage->lru, &h->hugepage_activelist);
437 spin_unlock(&hugetlb_lock);
438 return;
439}
440
441struct cgroup_subsys hugetlb_subsys = {
442 .name = "hugetlb",
443 .css_alloc = hugetlb_cgroup_css_alloc,
444 .css_offline = hugetlb_cgroup_css_offline,
445 .css_free = hugetlb_cgroup_css_free,
446 .subsys_id = hugetlb_subsys_id,
447};
448