]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/hugetlb_cgroup.c
hugetlb_cgroup: use helper macro NUMA_NO_NODE
[thirdparty/linux.git] / mm / hugetlb_cgroup.c
CommitLineData
2bc64a20
AK
1/*
2 *
3 * Copyright IBM Corporation, 2012
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 *
faced7e0
GS
6 * Cgroup v2
7 * Copyright (C) 2019 Red Hat, Inc.
8 * Author: Giuseppe Scrivano <gscrivan@redhat.com>
9 *
2bc64a20
AK
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of version 2.1 of the GNU Lesser General Public License
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it would be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 */
19
20#include <linux/cgroup.h>
71f87bee 21#include <linux/page_counter.h>
2bc64a20
AK
22#include <linux/slab.h>
23#include <linux/hugetlb.h>
24#include <linux/hugetlb_cgroup.h>
25
abb8206c
AK
26#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
27#define MEMFILE_IDX(val) (((val) >> 16) & 0xffff)
28#define MEMFILE_ATTR(val) ((val) & 0xffff)
29
2bc64a20
AK
30static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
31
cdc2fcfe 32static inline struct page_counter *
1adc4d41
MA
33__hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx,
34 bool rsvd)
cdc2fcfe
MA
35{
36 if (rsvd)
37 return &h_cg->rsvd_hugepage[idx];
38 return &h_cg->hugepage[idx];
39}
40
1adc4d41
MA
41static inline struct page_counter *
42hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx)
43{
44 return __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, false);
45}
46
47static inline struct page_counter *
48hugetlb_cgroup_counter_from_cgroup_rsvd(struct hugetlb_cgroup *h_cg, int idx)
49{
50 return __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, true);
51}
52
2bc64a20
AK
53static inline
54struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
55{
a7c6d554 56 return s ? container_of(s, struct hugetlb_cgroup, css) : NULL;
2bc64a20
AK
57}
58
2bc64a20
AK
59static inline
60struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task)
61{
073219e9 62 return hugetlb_cgroup_from_css(task_css(task, hugetlb_cgrp_id));
2bc64a20
AK
63}
64
65static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
66{
67 return (h_cg == root_h_cgroup);
68}
69
3f798518
TH
70static inline struct hugetlb_cgroup *
71parent_hugetlb_cgroup(struct hugetlb_cgroup *h_cg)
2bc64a20 72{
5c9d535b 73 return hugetlb_cgroup_from_css(h_cg->css.parent);
2bc64a20
AK
74}
75
3f798518 76static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)
2bc64a20
AK
77{
78 int idx;
2bc64a20
AK
79
80 for (idx = 0; idx < hugetlb_max_hstate; idx++) {
1adc4d41 81 if (page_counter_read(
7a5bde37 82 hugetlb_cgroup_counter_from_cgroup(h_cg, idx)))
2bc64a20
AK
83 return true;
84 }
85 return false;
86}
87
297880f4
DR
88static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup,
89 struct hugetlb_cgroup *parent_h_cgroup)
90{
91 int idx;
92
93 for (idx = 0; idx < HUGE_MAX_HSTATE; idx++) {
1adc4d41
MA
94 struct page_counter *fault_parent = NULL;
95 struct page_counter *rsvd_parent = NULL;
297880f4
DR
96 unsigned long limit;
97 int ret;
98
1adc4d41
MA
99 if (parent_h_cgroup) {
100 fault_parent = hugetlb_cgroup_counter_from_cgroup(
101 parent_h_cgroup, idx);
102 rsvd_parent = hugetlb_cgroup_counter_from_cgroup_rsvd(
103 parent_h_cgroup, idx);
104 }
105 page_counter_init(hugetlb_cgroup_counter_from_cgroup(h_cgroup,
106 idx),
107 fault_parent);
108 page_counter_init(
109 hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx),
110 rsvd_parent);
297880f4
DR
111
112 limit = round_down(PAGE_COUNTER_MAX,
8938494c 113 pages_per_huge_page(&hstates[idx]));
1adc4d41
MA
114
115 ret = page_counter_set_max(
116 hugetlb_cgroup_counter_from_cgroup(h_cgroup, idx),
117 limit);
118 VM_BUG_ON(ret);
119 ret = page_counter_set_max(
120 hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx),
121 limit);
297880f4
DR
122 VM_BUG_ON(ret);
123 }
124}
125
f4776199
MA
126static void hugetlb_cgroup_free(struct hugetlb_cgroup *h_cgroup)
127{
128 int node;
129
130 for_each_node(node)
131 kfree(h_cgroup->nodeinfo[node]);
132 kfree(h_cgroup);
133}
134
eb95419b
TH
135static struct cgroup_subsys_state *
136hugetlb_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
2bc64a20 137{
eb95419b
TH
138 struct hugetlb_cgroup *parent_h_cgroup = hugetlb_cgroup_from_css(parent_css);
139 struct hugetlb_cgroup *h_cgroup;
f4776199
MA
140 int node;
141
142 h_cgroup = kzalloc(struct_size(h_cgroup, nodeinfo, nr_node_ids),
143 GFP_KERNEL);
2bc64a20 144
2bc64a20
AK
145 if (!h_cgroup)
146 return ERR_PTR(-ENOMEM);
147
297880f4 148 if (!parent_h_cgroup)
2bc64a20 149 root_h_cgroup = h_cgroup;
297880f4 150
f4776199
MA
151 /*
152 * TODO: this routine can waste much memory for nodes which will
153 * never be onlined. It's better to use memory hotplug callback
154 * function.
155 */
156 for_each_node(node) {
99249387 157 /* Set node_to_alloc to NUMA_NO_NODE for offline nodes. */
f4776199 158 int node_to_alloc =
99249387 159 node_state(node, N_NORMAL_MEMORY) ? node : NUMA_NO_NODE;
f4776199
MA
160 h_cgroup->nodeinfo[node] =
161 kzalloc_node(sizeof(struct hugetlb_cgroup_per_node),
162 GFP_KERNEL, node_to_alloc);
163 if (!h_cgroup->nodeinfo[node])
164 goto fail_alloc_nodeinfo;
165 }
166
297880f4 167 hugetlb_cgroup_init(h_cgroup, parent_h_cgroup);
2bc64a20 168 return &h_cgroup->css;
f4776199
MA
169
170fail_alloc_nodeinfo:
171 hugetlb_cgroup_free(h_cgroup);
172 return ERR_PTR(-ENOMEM);
2bc64a20
AK
173}
174
eb95419b 175static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)
2bc64a20 176{
f4776199 177 hugetlb_cgroup_free(hugetlb_cgroup_from_css(css));
2bc64a20
AK
178}
179
da1def55
AK
180/*
181 * Should be called with hugetlb_lock held.
182 * Since we are holding hugetlb_lock, pages cannot get moved from
183 * active list or uncharged from the cgroup, So no need to get
184 * page reference and test for page active here. This function
185 * cannot fail.
186 */
3f798518 187static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,
da1def55
AK
188 struct page *page)
189{
71f87bee
JW
190 unsigned int nr_pages;
191 struct page_counter *counter;
da1def55 192 struct hugetlb_cgroup *page_hcg;
3f798518 193 struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(h_cg);
da1def55
AK
194
195 page_hcg = hugetlb_cgroup_from_page(page);
196 /*
197 * We can have pages in active list without any cgroup
198 * ie, hugepage with less than 3 pages. We can safely
199 * ignore those pages.
200 */
201 if (!page_hcg || page_hcg != h_cg)
202 goto out;
203
d8c6546b 204 nr_pages = compound_nr(page);
da1def55
AK
205 if (!parent) {
206 parent = root_h_cgroup;
207 /* root has no limit */
71f87bee 208 page_counter_charge(&parent->hugepage[idx], nr_pages);
da1def55
AK
209 }
210 counter = &h_cg->hugepage[idx];
71f87bee
JW
211 /* Take the pages off the local counter */
212 page_counter_cancel(counter, nr_pages);
da1def55
AK
213
214 set_hugetlb_cgroup(page, parent);
215out:
216 return;
217}
218
219/*
220 * Force the hugetlb cgroup to empty the hugetlb resources by moving them to
221 * the parent cgroup.
222 */
eb95419b 223static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css)
2bc64a20 224{
eb95419b 225 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
da1def55
AK
226 struct hstate *h;
227 struct page *page;
7a5bde37 228 int idx;
da1def55
AK
229
230 do {
7a5bde37 231 idx = 0;
da1def55 232 for_each_hstate(h) {
db71ef79 233 spin_lock_irq(&hugetlb_lock);
da1def55 234 list_for_each_entry(page, &h->hugepage_activelist, lru)
3f798518 235 hugetlb_cgroup_move_parent(idx, h_cg, page);
da1def55 236
db71ef79 237 spin_unlock_irq(&hugetlb_lock);
da1def55
AK
238 idx++;
239 }
240 cond_resched();
3f798518 241 } while (hugetlb_cgroup_have_usage(h_cg));
2bc64a20
AK
242}
243
faced7e0
GS
244static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx,
245 enum hugetlb_memory_event event)
246{
247 atomic_long_inc(&hugetlb->events_local[idx][event]);
248 cgroup_file_notify(&hugetlb->events_local_file[idx]);
249
250 do {
251 atomic_long_inc(&hugetlb->events[idx][event]);
252 cgroup_file_notify(&hugetlb->events_file[idx]);
253 } while ((hugetlb = parent_hugetlb_cgroup(hugetlb)) &&
254 !hugetlb_cgroup_is_root(hugetlb));
255}
256
1adc4d41
MA
257static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
258 struct hugetlb_cgroup **ptr,
259 bool rsvd)
6d76dcf4
AK
260{
261 int ret = 0;
71f87bee 262 struct page_counter *counter;
6d76dcf4 263 struct hugetlb_cgroup *h_cg = NULL;
6d76dcf4
AK
264
265 if (hugetlb_cgroup_disabled())
266 goto done;
267 /*
268 * We don't charge any cgroup if the compound page have less
269 * than 3 pages.
270 */
271 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
272 goto done;
273again:
274 rcu_read_lock();
275 h_cg = hugetlb_cgroup_from_task(current);
0362f326 276 if (!css_tryget(&h_cg->css)) {
6d76dcf4
AK
277 rcu_read_unlock();
278 goto again;
279 }
280 rcu_read_unlock();
281
1adc4d41
MA
282 if (!page_counter_try_charge(
283 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
284 nr_pages, &counter)) {
6071ca52 285 ret = -ENOMEM;
726b7bbe 286 hugetlb_event(h_cg, idx, HUGETLB_MAX);
1adc4d41
MA
287 css_put(&h_cg->css);
288 goto done;
faced7e0 289 }
1adc4d41
MA
290 /* Reservations take a reference to the css because they do not get
291 * reparented.
292 */
293 if (!rsvd)
294 css_put(&h_cg->css);
6d76dcf4
AK
295done:
296 *ptr = h_cg;
297 return ret;
298}
299
1adc4d41
MA
300int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
301 struct hugetlb_cgroup **ptr)
302{
303 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, false);
304}
305
306int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
307 struct hugetlb_cgroup **ptr)
308{
309 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, true);
310}
311
94ae8ba7 312/* Should be called with hugetlb_lock held */
1adc4d41
MA
313static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
314 struct hugetlb_cgroup *h_cg,
315 struct page *page, bool rsvd)
6d76dcf4
AK
316{
317 if (hugetlb_cgroup_disabled() || !h_cg)
318 return;
319
1adc4d41 320 __set_hugetlb_cgroup(page, h_cg, rsvd);
f4776199
MA
321 if (!rsvd) {
322 unsigned long usage =
323 h_cg->nodeinfo[page_to_nid(page)]->usage[idx];
324 /*
325 * This write is not atomic due to fetching usage and writing
326 * to it, but that's fine because we call this with
327 * hugetlb_lock held anyway.
328 */
329 WRITE_ONCE(h_cg->nodeinfo[page_to_nid(page)]->usage[idx],
330 usage + nr_pages);
331 }
6d76dcf4
AK
332}
333
1adc4d41
MA
334void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
335 struct hugetlb_cgroup *h_cg,
336 struct page *page)
337{
338 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, false);
339}
340
341void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
342 struct hugetlb_cgroup *h_cg,
343 struct page *page)
344{
345 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, true);
346}
347
6d76dcf4
AK
348/*
349 * Should be called with hugetlb_lock held
350 */
1adc4d41
MA
351static void __hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
352 struct page *page, bool rsvd)
6d76dcf4
AK
353{
354 struct hugetlb_cgroup *h_cg;
6d76dcf4
AK
355
356 if (hugetlb_cgroup_disabled())
357 return;
7ea8574e 358 lockdep_assert_held(&hugetlb_lock);
1adc4d41 359 h_cg = __hugetlb_cgroup_from_page(page, rsvd);
6d76dcf4
AK
360 if (unlikely(!h_cg))
361 return;
1adc4d41
MA
362 __set_hugetlb_cgroup(page, NULL, rsvd);
363
364 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
365 rsvd),
366 nr_pages);
367
368 if (rsvd)
369 css_put(&h_cg->css);
f4776199
MA
370 else {
371 unsigned long usage =
372 h_cg->nodeinfo[page_to_nid(page)]->usage[idx];
373 /*
374 * This write is not atomic due to fetching usage and writing
375 * to it, but that's fine because we call this with
376 * hugetlb_lock held anyway.
377 */
378 WRITE_ONCE(h_cg->nodeinfo[page_to_nid(page)]->usage[idx],
379 usage - nr_pages);
380 }
6d76dcf4
AK
381}
382
1adc4d41
MA
383void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
384 struct page *page)
385{
386 __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, false);
387}
388
389void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages,
390 struct page *page)
391{
392 __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, true);
393}
394
395static void __hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
396 struct hugetlb_cgroup *h_cg,
397 bool rsvd)
6d76dcf4 398{
6d76dcf4
AK
399 if (hugetlb_cgroup_disabled() || !h_cg)
400 return;
401
402 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
403 return;
404
1adc4d41
MA
405 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
406 rsvd),
407 nr_pages);
408
409 if (rsvd)
410 css_put(&h_cg->css);
411}
412
413void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
414 struct hugetlb_cgroup *h_cg)
415{
416 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, false);
417}
418
419void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
420 struct hugetlb_cgroup *h_cg)
421{
422 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, true);
423}
424
e9fe92ae
MA
425void hugetlb_cgroup_uncharge_counter(struct resv_map *resv, unsigned long start,
426 unsigned long end)
1adc4d41 427{
e9fe92ae
MA
428 if (hugetlb_cgroup_disabled() || !resv || !resv->reservation_counter ||
429 !resv->css)
1adc4d41
MA
430 return;
431
e9fe92ae
MA
432 page_counter_uncharge(resv->reservation_counter,
433 (end - start) * resv->pages_per_hpage);
434 css_put(resv->css);
6d76dcf4
AK
435}
436
075a61d0
MA
437void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
438 struct file_region *rg,
d85aecf2
ML
439 unsigned long nr_pages,
440 bool region_del)
075a61d0
MA
441{
442 if (hugetlb_cgroup_disabled() || !resv || !rg || !nr_pages)
443 return;
444
862f7f65 445 if (rg->reservation_counter && resv->pages_per_hpage &&
075a61d0
MA
446 !resv->reservation_counter) {
447 page_counter_uncharge(rg->reservation_counter,
448 nr_pages * resv->pages_per_hpage);
d85aecf2
ML
449 /*
450 * Only do css_put(rg->css) when we delete the entire region
451 * because one file_region must hold exactly one css reference.
452 */
453 if (region_del)
454 css_put(rg->css);
075a61d0
MA
455 }
456}
457
71f87bee
JW
458enum {
459 RES_USAGE,
cdc2fcfe 460 RES_RSVD_USAGE,
71f87bee 461 RES_LIMIT,
cdc2fcfe 462 RES_RSVD_LIMIT,
71f87bee 463 RES_MAX_USAGE,
cdc2fcfe 464 RES_RSVD_MAX_USAGE,
71f87bee 465 RES_FAILCNT,
cdc2fcfe 466 RES_RSVD_FAILCNT,
71f87bee
JW
467};
468
f4776199
MA
469static int hugetlb_cgroup_read_numa_stat(struct seq_file *seq, void *dummy)
470{
471 int nid;
472 struct cftype *cft = seq_cft(seq);
473 int idx = MEMFILE_IDX(cft->private);
474 bool legacy = MEMFILE_ATTR(cft->private);
475 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
476 struct cgroup_subsys_state *css;
477 unsigned long usage;
478
479 if (legacy) {
480 /* Add up usage across all nodes for the non-hierarchical total. */
481 usage = 0;
482 for_each_node_state(nid, N_MEMORY)
483 usage += READ_ONCE(h_cg->nodeinfo[nid]->usage[idx]);
484 seq_printf(seq, "total=%lu", usage * PAGE_SIZE);
485
486 /* Simply print the per-node usage for the non-hierarchical total. */
487 for_each_node_state(nid, N_MEMORY)
488 seq_printf(seq, " N%d=%lu", nid,
489 READ_ONCE(h_cg->nodeinfo[nid]->usage[idx]) *
490 PAGE_SIZE);
491 seq_putc(seq, '\n');
492 }
493
494 /*
495 * The hierarchical total is pretty much the value recorded by the
496 * counter, so use that.
497 */
498 seq_printf(seq, "%stotal=%lu", legacy ? "hierarchical_" : "",
499 page_counter_read(&h_cg->hugepage[idx]) * PAGE_SIZE);
500
501 /*
502 * For each node, transverse the css tree to obtain the hierarchical
503 * node usage.
504 */
505 for_each_node_state(nid, N_MEMORY) {
506 usage = 0;
507 rcu_read_lock();
508 css_for_each_descendant_pre(css, &h_cg->css) {
509 usage += READ_ONCE(hugetlb_cgroup_from_css(css)
510 ->nodeinfo[nid]
511 ->usage[idx]);
512 }
513 rcu_read_unlock();
514 seq_printf(seq, " N%d=%lu", nid, usage * PAGE_SIZE);
515 }
516
517 seq_putc(seq, '\n');
518
519 return 0;
520}
521
716f479d
TH
522static u64 hugetlb_cgroup_read_u64(struct cgroup_subsys_state *css,
523 struct cftype *cft)
abb8206c 524{
71f87bee 525 struct page_counter *counter;
cdc2fcfe 526 struct page_counter *rsvd_counter;
182446d0 527 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
abb8206c 528
71f87bee 529 counter = &h_cg->hugepage[MEMFILE_IDX(cft->private)];
cdc2fcfe 530 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(cft->private)];
abb8206c 531
71f87bee
JW
532 switch (MEMFILE_ATTR(cft->private)) {
533 case RES_USAGE:
534 return (u64)page_counter_read(counter) * PAGE_SIZE;
cdc2fcfe
MA
535 case RES_RSVD_USAGE:
536 return (u64)page_counter_read(rsvd_counter) * PAGE_SIZE;
71f87bee 537 case RES_LIMIT:
bbec2e15 538 return (u64)counter->max * PAGE_SIZE;
cdc2fcfe
MA
539 case RES_RSVD_LIMIT:
540 return (u64)rsvd_counter->max * PAGE_SIZE;
71f87bee
JW
541 case RES_MAX_USAGE:
542 return (u64)counter->watermark * PAGE_SIZE;
cdc2fcfe
MA
543 case RES_RSVD_MAX_USAGE:
544 return (u64)rsvd_counter->watermark * PAGE_SIZE;
71f87bee
JW
545 case RES_FAILCNT:
546 return counter->failcnt;
cdc2fcfe
MA
547 case RES_RSVD_FAILCNT:
548 return rsvd_counter->failcnt;
71f87bee
JW
549 default:
550 BUG();
551 }
abb8206c
AK
552}
553
faced7e0
GS
554static int hugetlb_cgroup_read_u64_max(struct seq_file *seq, void *v)
555{
556 int idx;
557 u64 val;
558 struct cftype *cft = seq_cft(seq);
559 unsigned long limit;
560 struct page_counter *counter;
561 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
562
563 idx = MEMFILE_IDX(cft->private);
564 counter = &h_cg->hugepage[idx];
565
566 limit = round_down(PAGE_COUNTER_MAX,
8938494c 567 pages_per_huge_page(&hstates[idx]));
faced7e0
GS
568
569 switch (MEMFILE_ATTR(cft->private)) {
cdc2fcfe
MA
570 case RES_RSVD_USAGE:
571 counter = &h_cg->rsvd_hugepage[idx];
e4a9bc58 572 fallthrough;
faced7e0
GS
573 case RES_USAGE:
574 val = (u64)page_counter_read(counter);
575 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
576 break;
cdc2fcfe
MA
577 case RES_RSVD_LIMIT:
578 counter = &h_cg->rsvd_hugepage[idx];
e4a9bc58 579 fallthrough;
faced7e0
GS
580 case RES_LIMIT:
581 val = (u64)counter->max;
582 if (val == limit)
583 seq_puts(seq, "max\n");
584 else
585 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
586 break;
587 default:
588 BUG();
589 }
590
591 return 0;
592}
593
71f87bee
JW
594static DEFINE_MUTEX(hugetlb_limit_mutex);
595
451af504 596static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of,
faced7e0
GS
597 char *buf, size_t nbytes, loff_t off,
598 const char *max)
abb8206c 599{
71f87bee
JW
600 int ret, idx;
601 unsigned long nr_pages;
451af504 602 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
cdc2fcfe 603 bool rsvd = false;
abb8206c 604
71f87bee
JW
605 if (hugetlb_cgroup_is_root(h_cg)) /* Can't set limit on root */
606 return -EINVAL;
607
451af504 608 buf = strstrip(buf);
faced7e0 609 ret = page_counter_memparse(buf, max, &nr_pages);
71f87bee
JW
610 if (ret)
611 return ret;
612
451af504 613 idx = MEMFILE_IDX(of_cft(of)->private);
8938494c 614 nr_pages = round_down(nr_pages, pages_per_huge_page(&hstates[idx]));
abb8206c 615
71f87bee 616 switch (MEMFILE_ATTR(of_cft(of)->private)) {
cdc2fcfe
MA
617 case RES_RSVD_LIMIT:
618 rsvd = true;
e4a9bc58 619 fallthrough;
abb8206c 620 case RES_LIMIT:
71f87bee 621 mutex_lock(&hugetlb_limit_mutex);
cdc2fcfe 622 ret = page_counter_set_max(
1adc4d41 623 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
cdc2fcfe 624 nr_pages);
71f87bee 625 mutex_unlock(&hugetlb_limit_mutex);
abb8206c
AK
626 break;
627 default:
628 ret = -EINVAL;
629 break;
630 }
451af504 631 return ret ?: nbytes;
abb8206c
AK
632}
633
faced7e0
GS
634static ssize_t hugetlb_cgroup_write_legacy(struct kernfs_open_file *of,
635 char *buf, size_t nbytes, loff_t off)
636{
637 return hugetlb_cgroup_write(of, buf, nbytes, off, "-1");
638}
639
640static ssize_t hugetlb_cgroup_write_dfl(struct kernfs_open_file *of,
641 char *buf, size_t nbytes, loff_t off)
642{
643 return hugetlb_cgroup_write(of, buf, nbytes, off, "max");
644}
645
6770c64e
TH
646static ssize_t hugetlb_cgroup_reset(struct kernfs_open_file *of,
647 char *buf, size_t nbytes, loff_t off)
abb8206c 648{
71f87bee 649 int ret = 0;
cdc2fcfe 650 struct page_counter *counter, *rsvd_counter;
6770c64e 651 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
abb8206c 652
71f87bee 653 counter = &h_cg->hugepage[MEMFILE_IDX(of_cft(of)->private)];
cdc2fcfe 654 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(of_cft(of)->private)];
abb8206c 655
71f87bee 656 switch (MEMFILE_ATTR(of_cft(of)->private)) {
abb8206c 657 case RES_MAX_USAGE:
71f87bee 658 page_counter_reset_watermark(counter);
abb8206c 659 break;
cdc2fcfe
MA
660 case RES_RSVD_MAX_USAGE:
661 page_counter_reset_watermark(rsvd_counter);
662 break;
abb8206c 663 case RES_FAILCNT:
71f87bee 664 counter->failcnt = 0;
abb8206c 665 break;
cdc2fcfe
MA
666 case RES_RSVD_FAILCNT:
667 rsvd_counter->failcnt = 0;
668 break;
abb8206c
AK
669 default:
670 ret = -EINVAL;
671 break;
672 }
6770c64e 673 return ret ?: nbytes;
abb8206c
AK
674}
675
676static char *mem_fmt(char *buf, int size, unsigned long hsize)
677{
abfb09e2
ML
678 if (hsize >= SZ_1G)
679 snprintf(buf, size, "%luGB", hsize / SZ_1G);
680 else if (hsize >= SZ_1M)
681 snprintf(buf, size, "%luMB", hsize / SZ_1M);
abb8206c 682 else
abfb09e2 683 snprintf(buf, size, "%luKB", hsize / SZ_1K);
abb8206c
AK
684 return buf;
685}
686
faced7e0
GS
687static int __hugetlb_events_show(struct seq_file *seq, bool local)
688{
689 int idx;
690 long max;
691 struct cftype *cft = seq_cft(seq);
692 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
693
694 idx = MEMFILE_IDX(cft->private);
695
696 if (local)
697 max = atomic_long_read(&h_cg->events_local[idx][HUGETLB_MAX]);
698 else
699 max = atomic_long_read(&h_cg->events[idx][HUGETLB_MAX]);
700
701 seq_printf(seq, "max %lu\n", max);
702
703 return 0;
704}
705
706static int hugetlb_events_show(struct seq_file *seq, void *v)
707{
708 return __hugetlb_events_show(seq, false);
709}
710
711static int hugetlb_events_local_show(struct seq_file *seq, void *v)
712{
713 return __hugetlb_events_show(seq, true);
714}
715
716static void __init __hugetlb_cgroup_file_dfl_init(int idx)
abb8206c
AK
717{
718 char buf[32];
719 struct cftype *cft;
720 struct hstate *h = &hstates[idx];
721
722 /* format the size */
cdc2fcfe 723 mem_fmt(buf, sizeof(buf), huge_page_size(h));
abb8206c
AK
724
725 /* Add the limit file */
faced7e0
GS
726 cft = &h->cgroup_files_dfl[0];
727 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max", buf);
728 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
729 cft->seq_show = hugetlb_cgroup_read_u64_max;
730 cft->write = hugetlb_cgroup_write_dfl;
731 cft->flags = CFTYPE_NOT_ON_ROOT;
732
cdc2fcfe 733 /* Add the reservation limit file */
faced7e0 734 cft = &h->cgroup_files_dfl[1];
cdc2fcfe
MA
735 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max", buf);
736 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
737 cft->seq_show = hugetlb_cgroup_read_u64_max;
738 cft->write = hugetlb_cgroup_write_dfl;
739 cft->flags = CFTYPE_NOT_ON_ROOT;
740
741 /* Add the current usage file */
742 cft = &h->cgroup_files_dfl[2];
faced7e0
GS
743 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.current", buf);
744 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
745 cft->seq_show = hugetlb_cgroup_read_u64_max;
746 cft->flags = CFTYPE_NOT_ON_ROOT;
747
cdc2fcfe
MA
748 /* Add the current reservation usage file */
749 cft = &h->cgroup_files_dfl[3];
750 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.current", buf);
751 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
752 cft->seq_show = hugetlb_cgroup_read_u64_max;
753 cft->flags = CFTYPE_NOT_ON_ROOT;
754
faced7e0 755 /* Add the events file */
cdc2fcfe 756 cft = &h->cgroup_files_dfl[4];
faced7e0
GS
757 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events", buf);
758 cft->private = MEMFILE_PRIVATE(idx, 0);
759 cft->seq_show = hugetlb_events_show;
d5a16959 760 cft->file_offset = offsetof(struct hugetlb_cgroup, events_file[idx]);
faced7e0
GS
761 cft->flags = CFTYPE_NOT_ON_ROOT;
762
763 /* Add the events.local file */
cdc2fcfe 764 cft = &h->cgroup_files_dfl[5];
faced7e0
GS
765 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events.local", buf);
766 cft->private = MEMFILE_PRIVATE(idx, 0);
767 cft->seq_show = hugetlb_events_local_show;
768 cft->file_offset = offsetof(struct hugetlb_cgroup,
d5a16959 769 events_local_file[idx]);
faced7e0
GS
770 cft->flags = CFTYPE_NOT_ON_ROOT;
771
f4776199 772 /* Add the numa stat file */
cdc2fcfe 773 cft = &h->cgroup_files_dfl[6];
f4776199 774 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.numa_stat", buf);
2727cfe4 775 cft->private = MEMFILE_PRIVATE(idx, 0);
f4776199
MA
776 cft->seq_show = hugetlb_cgroup_read_numa_stat;
777 cft->flags = CFTYPE_NOT_ON_ROOT;
778
779 /* NULL terminate the last cft */
780 cft = &h->cgroup_files_dfl[7];
faced7e0
GS
781 memset(cft, 0, sizeof(*cft));
782
783 WARN_ON(cgroup_add_dfl_cftypes(&hugetlb_cgrp_subsys,
784 h->cgroup_files_dfl));
785}
786
787static void __init __hugetlb_cgroup_file_legacy_init(int idx)
788{
789 char buf[32];
790 struct cftype *cft;
791 struct hstate *h = &hstates[idx];
792
793 /* format the size */
cdc2fcfe 794 mem_fmt(buf, sizeof(buf), huge_page_size(h));
faced7e0
GS
795
796 /* Add the limit file */
797 cft = &h->cgroup_files_legacy[0];
abb8206c
AK
798 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
799 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
716f479d 800 cft->read_u64 = hugetlb_cgroup_read_u64;
faced7e0 801 cft->write = hugetlb_cgroup_write_legacy;
abb8206c 802
cdc2fcfe 803 /* Add the reservation limit file */
faced7e0 804 cft = &h->cgroup_files_legacy[1];
cdc2fcfe
MA
805 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.limit_in_bytes", buf);
806 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
807 cft->read_u64 = hugetlb_cgroup_read_u64;
808 cft->write = hugetlb_cgroup_write_legacy;
809
810 /* Add the usage file */
811 cft = &h->cgroup_files_legacy[2];
abb8206c
AK
812 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
813 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
716f479d 814 cft->read_u64 = hugetlb_cgroup_read_u64;
abb8206c 815
cdc2fcfe
MA
816 /* Add the reservation usage file */
817 cft = &h->cgroup_files_legacy[3];
818 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.usage_in_bytes", buf);
819 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
820 cft->read_u64 = hugetlb_cgroup_read_u64;
821
abb8206c 822 /* Add the MAX usage file */
cdc2fcfe 823 cft = &h->cgroup_files_legacy[4];
abb8206c
AK
824 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
825 cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
6770c64e 826 cft->write = hugetlb_cgroup_reset;
716f479d 827 cft->read_u64 = hugetlb_cgroup_read_u64;
abb8206c 828
cdc2fcfe
MA
829 /* Add the MAX reservation usage file */
830 cft = &h->cgroup_files_legacy[5];
831 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max_usage_in_bytes", buf);
832 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_MAX_USAGE);
833 cft->write = hugetlb_cgroup_reset;
834 cft->read_u64 = hugetlb_cgroup_read_u64;
835
abb8206c 836 /* Add the failcntfile */
cdc2fcfe 837 cft = &h->cgroup_files_legacy[6];
abb8206c 838 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
cdc2fcfe
MA
839 cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
840 cft->write = hugetlb_cgroup_reset;
841 cft->read_u64 = hugetlb_cgroup_read_u64;
842
843 /* Add the reservation failcntfile */
844 cft = &h->cgroup_files_legacy[7];
845 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.failcnt", buf);
846 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_FAILCNT);
6770c64e 847 cft->write = hugetlb_cgroup_reset;
716f479d 848 cft->read_u64 = hugetlb_cgroup_read_u64;
abb8206c 849
f4776199 850 /* Add the numa stat file */
cdc2fcfe 851 cft = &h->cgroup_files_legacy[8];
f4776199
MA
852 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.numa_stat", buf);
853 cft->private = MEMFILE_PRIVATE(idx, 1);
854 cft->seq_show = hugetlb_cgroup_read_numa_stat;
855
856 /* NULL terminate the last cft */
857 cft = &h->cgroup_files_legacy[9];
abb8206c
AK
858 memset(cft, 0, sizeof(*cft));
859
2cf669a5 860 WARN_ON(cgroup_add_legacy_cftypes(&hugetlb_cgrp_subsys,
faced7e0
GS
861 h->cgroup_files_legacy));
862}
863
864static void __init __hugetlb_cgroup_file_init(int idx)
865{
866 __hugetlb_cgroup_file_dfl_init(idx);
867 __hugetlb_cgroup_file_legacy_init(idx);
7179e7bf
JW
868}
869
870void __init hugetlb_cgroup_file_init(void)
871{
872 struct hstate *h;
873
874 for_each_hstate(h) {
875 /*
876 * Add cgroup control files only if the huge page consists
877 * of more than two normal pages. This is because we use
1d798ca3 878 * page[2].private for storing cgroup details.
7179e7bf
JW
879 */
880 if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER)
881 __hugetlb_cgroup_file_init(hstate_index(h));
882 }
abb8206c
AK
883}
884
75754681
AK
885/*
886 * hugetlb_lock will make sure a parallel cgroup rmdir won't happen
887 * when we migrate hugepages
888 */
8e6ac7fa
AK
889void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)
890{
891 struct hugetlb_cgroup *h_cg;
1adc4d41 892 struct hugetlb_cgroup *h_cg_rsvd;
94ae8ba7 893 struct hstate *h = page_hstate(oldhpage);
8e6ac7fa
AK
894
895 if (hugetlb_cgroup_disabled())
896 return;
897
db71ef79 898 spin_lock_irq(&hugetlb_lock);
8e6ac7fa 899 h_cg = hugetlb_cgroup_from_page(oldhpage);
1adc4d41 900 h_cg_rsvd = hugetlb_cgroup_from_page_rsvd(oldhpage);
8e6ac7fa 901 set_hugetlb_cgroup(oldhpage, NULL);
9808895e 902 set_hugetlb_cgroup_rsvd(oldhpage, NULL);
8e6ac7fa
AK
903
904 /* move the h_cg details to new cgroup */
9808895e 905 set_hugetlb_cgroup(newhpage, h_cg);
1adc4d41 906 set_hugetlb_cgroup_rsvd(newhpage, h_cg_rsvd);
94ae8ba7 907 list_move(&newhpage->lru, &h->hugepage_activelist);
db71ef79 908 spin_unlock_irq(&hugetlb_lock);
8e6ac7fa
AK
909 return;
910}
911
faced7e0
GS
912static struct cftype hugetlb_files[] = {
913 {} /* terminate */
914};
915
073219e9 916struct cgroup_subsys hugetlb_cgrp_subsys = {
92fb9748
TH
917 .css_alloc = hugetlb_cgroup_css_alloc,
918 .css_offline = hugetlb_cgroup_css_offline,
919 .css_free = hugetlb_cgroup_css_free,
faced7e0
GS
920 .dfl_cftypes = hugetlb_files,
921 .legacy_cftypes = hugetlb_files,
2bc64a20 922};