Merge tag 'drm-intel-fixes-2024-01-26' of git://anongit.freedesktop.org/drm/drm-intel...
[linux-block.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 77{
c37213c5 78 struct hstate *h;
2bc64a20 79
c37213c5 80 for_each_hstate(h) {
1adc4d41 81 if (page_counter_read(
c37213c5 82 hugetlb_cgroup_counter_from_cgroup(h_cg, hstate_index(h))))
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);
f074732d 194 struct folio *folio = page_folio(page);
da1def55 195
f074732d 196 page_hcg = hugetlb_cgroup_from_folio(folio);
da1def55
AK
197 /*
198 * We can have pages in active list without any cgroup
199 * ie, hugepage with less than 3 pages. We can safely
200 * ignore those pages.
201 */
202 if (!page_hcg || page_hcg != h_cg)
203 goto out;
204
d8c6546b 205 nr_pages = compound_nr(page);
da1def55
AK
206 if (!parent) {
207 parent = root_h_cgroup;
208 /* root has no limit */
71f87bee 209 page_counter_charge(&parent->hugepage[idx], nr_pages);
da1def55
AK
210 }
211 counter = &h_cg->hugepage[idx];
71f87bee
JW
212 /* Take the pages off the local counter */
213 page_counter_cancel(counter, nr_pages);
da1def55 214
de656ed3 215 set_hugetlb_cgroup(folio, parent);
da1def55
AK
216out:
217 return;
218}
219
220/*
221 * Force the hugetlb cgroup to empty the hugetlb resources by moving them to
222 * the parent cgroup.
223 */
eb95419b 224static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css)
2bc64a20 225{
eb95419b 226 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
da1def55
AK
227 struct hstate *h;
228 struct page *page;
da1def55
AK
229
230 do {
da1def55 231 for_each_hstate(h) {
db71ef79 232 spin_lock_irq(&hugetlb_lock);
da1def55 233 list_for_each_entry(page, &h->hugepage_activelist, lru)
c37213c5 234 hugetlb_cgroup_move_parent(hstate_index(h), h_cg, page);
da1def55 235
db71ef79 236 spin_unlock_irq(&hugetlb_lock);
da1def55
AK
237 }
238 cond_resched();
3f798518 239 } while (hugetlb_cgroup_have_usage(h_cg));
2bc64a20
AK
240}
241
faced7e0
GS
242static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx,
243 enum hugetlb_memory_event event)
244{
245 atomic_long_inc(&hugetlb->events_local[idx][event]);
246 cgroup_file_notify(&hugetlb->events_local_file[idx]);
247
248 do {
249 atomic_long_inc(&hugetlb->events[idx][event]);
250 cgroup_file_notify(&hugetlb->events_file[idx]);
251 } while ((hugetlb = parent_hugetlb_cgroup(hugetlb)) &&
252 !hugetlb_cgroup_is_root(hugetlb));
253}
254
1adc4d41
MA
255static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
256 struct hugetlb_cgroup **ptr,
257 bool rsvd)
6d76dcf4
AK
258{
259 int ret = 0;
71f87bee 260 struct page_counter *counter;
6d76dcf4 261 struct hugetlb_cgroup *h_cg = NULL;
6d76dcf4
AK
262
263 if (hugetlb_cgroup_disabled())
264 goto done;
6d76dcf4
AK
265again:
266 rcu_read_lock();
267 h_cg = hugetlb_cgroup_from_task(current);
0362f326 268 if (!css_tryget(&h_cg->css)) {
6d76dcf4
AK
269 rcu_read_unlock();
270 goto again;
271 }
272 rcu_read_unlock();
273
1adc4d41
MA
274 if (!page_counter_try_charge(
275 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
276 nr_pages, &counter)) {
6071ca52 277 ret = -ENOMEM;
726b7bbe 278 hugetlb_event(h_cg, idx, HUGETLB_MAX);
1adc4d41
MA
279 css_put(&h_cg->css);
280 goto done;
faced7e0 281 }
1adc4d41
MA
282 /* Reservations take a reference to the css because they do not get
283 * reparented.
284 */
285 if (!rsvd)
286 css_put(&h_cg->css);
6d76dcf4
AK
287done:
288 *ptr = h_cg;
289 return ret;
290}
291
1adc4d41
MA
292int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
293 struct hugetlb_cgroup **ptr)
294{
295 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, false);
296}
297
298int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
299 struct hugetlb_cgroup **ptr)
300{
301 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, true);
302}
303
94ae8ba7 304/* Should be called with hugetlb_lock held */
1adc4d41
MA
305static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
306 struct hugetlb_cgroup *h_cg,
541b7c7b 307 struct folio *folio, bool rsvd)
6d76dcf4
AK
308{
309 if (hugetlb_cgroup_disabled() || !h_cg)
310 return;
311
541b7c7b 312 __set_hugetlb_cgroup(folio, h_cg, rsvd);
f4776199
MA
313 if (!rsvd) {
314 unsigned long usage =
541b7c7b 315 h_cg->nodeinfo[folio_nid(folio)]->usage[idx];
f4776199
MA
316 /*
317 * This write is not atomic due to fetching usage and writing
318 * to it, but that's fine because we call this with
319 * hugetlb_lock held anyway.
320 */
541b7c7b 321 WRITE_ONCE(h_cg->nodeinfo[folio_nid(folio)]->usage[idx],
f4776199
MA
322 usage + nr_pages);
323 }
6d76dcf4
AK
324}
325
1adc4d41
MA
326void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
327 struct hugetlb_cgroup *h_cg,
ff7d853b 328 struct folio *folio)
1adc4d41 329{
541b7c7b 330 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, folio, false);
1adc4d41
MA
331}
332
333void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
334 struct hugetlb_cgroup *h_cg,
ff7d853b 335 struct folio *folio)
1adc4d41 336{
541b7c7b 337 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, folio, true);
1adc4d41
MA
338}
339
6d76dcf4
AK
340/*
341 * Should be called with hugetlb_lock held
342 */
d4ab0316
SK
343static void __hugetlb_cgroup_uncharge_folio(int idx, unsigned long nr_pages,
344 struct folio *folio, bool rsvd)
6d76dcf4
AK
345{
346 struct hugetlb_cgroup *h_cg;
6d76dcf4
AK
347
348 if (hugetlb_cgroup_disabled())
349 return;
7ea8574e 350 lockdep_assert_held(&hugetlb_lock);
f074732d 351 h_cg = __hugetlb_cgroup_from_folio(folio, rsvd);
6d76dcf4
AK
352 if (unlikely(!h_cg))
353 return;
f074732d 354 __set_hugetlb_cgroup(folio, NULL, rsvd);
1adc4d41
MA
355
356 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
357 rsvd),
358 nr_pages);
359
360 if (rsvd)
361 css_put(&h_cg->css);
f4776199
MA
362 else {
363 unsigned long usage =
d4ab0316 364 h_cg->nodeinfo[folio_nid(folio)]->usage[idx];
f4776199
MA
365 /*
366 * This write is not atomic due to fetching usage and writing
367 * to it, but that's fine because we call this with
368 * hugetlb_lock held anyway.
369 */
d4ab0316 370 WRITE_ONCE(h_cg->nodeinfo[folio_nid(folio)]->usage[idx],
f4776199
MA
371 usage - nr_pages);
372 }
6d76dcf4
AK
373}
374
d4ab0316
SK
375void hugetlb_cgroup_uncharge_folio(int idx, unsigned long nr_pages,
376 struct folio *folio)
1adc4d41 377{
d4ab0316 378 __hugetlb_cgroup_uncharge_folio(idx, nr_pages, folio, false);
1adc4d41
MA
379}
380
d4ab0316
SK
381void hugetlb_cgroup_uncharge_folio_rsvd(int idx, unsigned long nr_pages,
382 struct folio *folio)
1adc4d41 383{
d4ab0316 384 __hugetlb_cgroup_uncharge_folio(idx, nr_pages, folio, true);
1adc4d41
MA
385}
386
387static void __hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
388 struct hugetlb_cgroup *h_cg,
389 bool rsvd)
6d76dcf4 390{
6d76dcf4
AK
391 if (hugetlb_cgroup_disabled() || !h_cg)
392 return;
393
1adc4d41
MA
394 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
395 rsvd),
396 nr_pages);
397
398 if (rsvd)
399 css_put(&h_cg->css);
400}
401
402void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
403 struct hugetlb_cgroup *h_cg)
404{
405 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, false);
406}
407
408void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
409 struct hugetlb_cgroup *h_cg)
410{
411 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, true);
412}
413
e9fe92ae
MA
414void hugetlb_cgroup_uncharge_counter(struct resv_map *resv, unsigned long start,
415 unsigned long end)
1adc4d41 416{
e9fe92ae
MA
417 if (hugetlb_cgroup_disabled() || !resv || !resv->reservation_counter ||
418 !resv->css)
1adc4d41
MA
419 return;
420
e9fe92ae
MA
421 page_counter_uncharge(resv->reservation_counter,
422 (end - start) * resv->pages_per_hpage);
423 css_put(resv->css);
6d76dcf4
AK
424}
425
075a61d0
MA
426void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
427 struct file_region *rg,
d85aecf2
ML
428 unsigned long nr_pages,
429 bool region_del)
075a61d0
MA
430{
431 if (hugetlb_cgroup_disabled() || !resv || !rg || !nr_pages)
432 return;
433
862f7f65 434 if (rg->reservation_counter && resv->pages_per_hpage &&
075a61d0
MA
435 !resv->reservation_counter) {
436 page_counter_uncharge(rg->reservation_counter,
437 nr_pages * resv->pages_per_hpage);
d85aecf2
ML
438 /*
439 * Only do css_put(rg->css) when we delete the entire region
440 * because one file_region must hold exactly one css reference.
441 */
442 if (region_del)
443 css_put(rg->css);
075a61d0
MA
444 }
445}
446
71f87bee
JW
447enum {
448 RES_USAGE,
cdc2fcfe 449 RES_RSVD_USAGE,
71f87bee 450 RES_LIMIT,
cdc2fcfe 451 RES_RSVD_LIMIT,
71f87bee 452 RES_MAX_USAGE,
cdc2fcfe 453 RES_RSVD_MAX_USAGE,
71f87bee 454 RES_FAILCNT,
cdc2fcfe 455 RES_RSVD_FAILCNT,
71f87bee
JW
456};
457
f4776199
MA
458static int hugetlb_cgroup_read_numa_stat(struct seq_file *seq, void *dummy)
459{
460 int nid;
461 struct cftype *cft = seq_cft(seq);
462 int idx = MEMFILE_IDX(cft->private);
463 bool legacy = MEMFILE_ATTR(cft->private);
464 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
465 struct cgroup_subsys_state *css;
466 unsigned long usage;
467
468 if (legacy) {
469 /* Add up usage across all nodes for the non-hierarchical total. */
470 usage = 0;
471 for_each_node_state(nid, N_MEMORY)
472 usage += READ_ONCE(h_cg->nodeinfo[nid]->usage[idx]);
473 seq_printf(seq, "total=%lu", usage * PAGE_SIZE);
474
475 /* Simply print the per-node usage for the non-hierarchical total. */
476 for_each_node_state(nid, N_MEMORY)
477 seq_printf(seq, " N%d=%lu", nid,
478 READ_ONCE(h_cg->nodeinfo[nid]->usage[idx]) *
479 PAGE_SIZE);
480 seq_putc(seq, '\n');
481 }
482
483 /*
484 * The hierarchical total is pretty much the value recorded by the
485 * counter, so use that.
486 */
487 seq_printf(seq, "%stotal=%lu", legacy ? "hierarchical_" : "",
488 page_counter_read(&h_cg->hugepage[idx]) * PAGE_SIZE);
489
490 /*
491 * For each node, transverse the css tree to obtain the hierarchical
492 * node usage.
493 */
494 for_each_node_state(nid, N_MEMORY) {
495 usage = 0;
496 rcu_read_lock();
497 css_for_each_descendant_pre(css, &h_cg->css) {
498 usage += READ_ONCE(hugetlb_cgroup_from_css(css)
499 ->nodeinfo[nid]
500 ->usage[idx]);
501 }
502 rcu_read_unlock();
503 seq_printf(seq, " N%d=%lu", nid, usage * PAGE_SIZE);
504 }
505
506 seq_putc(seq, '\n');
507
508 return 0;
509}
510
716f479d
TH
511static u64 hugetlb_cgroup_read_u64(struct cgroup_subsys_state *css,
512 struct cftype *cft)
abb8206c 513{
71f87bee 514 struct page_counter *counter;
cdc2fcfe 515 struct page_counter *rsvd_counter;
182446d0 516 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
abb8206c 517
71f87bee 518 counter = &h_cg->hugepage[MEMFILE_IDX(cft->private)];
cdc2fcfe 519 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(cft->private)];
abb8206c 520
71f87bee
JW
521 switch (MEMFILE_ATTR(cft->private)) {
522 case RES_USAGE:
523 return (u64)page_counter_read(counter) * PAGE_SIZE;
cdc2fcfe
MA
524 case RES_RSVD_USAGE:
525 return (u64)page_counter_read(rsvd_counter) * PAGE_SIZE;
71f87bee 526 case RES_LIMIT:
bbec2e15 527 return (u64)counter->max * PAGE_SIZE;
cdc2fcfe
MA
528 case RES_RSVD_LIMIT:
529 return (u64)rsvd_counter->max * PAGE_SIZE;
71f87bee
JW
530 case RES_MAX_USAGE:
531 return (u64)counter->watermark * PAGE_SIZE;
cdc2fcfe
MA
532 case RES_RSVD_MAX_USAGE:
533 return (u64)rsvd_counter->watermark * PAGE_SIZE;
71f87bee
JW
534 case RES_FAILCNT:
535 return counter->failcnt;
cdc2fcfe
MA
536 case RES_RSVD_FAILCNT:
537 return rsvd_counter->failcnt;
71f87bee
JW
538 default:
539 BUG();
540 }
abb8206c
AK
541}
542
faced7e0
GS
543static int hugetlb_cgroup_read_u64_max(struct seq_file *seq, void *v)
544{
545 int idx;
546 u64 val;
547 struct cftype *cft = seq_cft(seq);
548 unsigned long limit;
549 struct page_counter *counter;
550 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
551
552 idx = MEMFILE_IDX(cft->private);
553 counter = &h_cg->hugepage[idx];
554
555 limit = round_down(PAGE_COUNTER_MAX,
8938494c 556 pages_per_huge_page(&hstates[idx]));
faced7e0
GS
557
558 switch (MEMFILE_ATTR(cft->private)) {
cdc2fcfe
MA
559 case RES_RSVD_USAGE:
560 counter = &h_cg->rsvd_hugepage[idx];
e4a9bc58 561 fallthrough;
faced7e0
GS
562 case RES_USAGE:
563 val = (u64)page_counter_read(counter);
564 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
565 break;
cdc2fcfe
MA
566 case RES_RSVD_LIMIT:
567 counter = &h_cg->rsvd_hugepage[idx];
e4a9bc58 568 fallthrough;
faced7e0
GS
569 case RES_LIMIT:
570 val = (u64)counter->max;
571 if (val == limit)
572 seq_puts(seq, "max\n");
573 else
574 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
575 break;
576 default:
577 BUG();
578 }
579
580 return 0;
581}
582
71f87bee
JW
583static DEFINE_MUTEX(hugetlb_limit_mutex);
584
451af504 585static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of,
faced7e0
GS
586 char *buf, size_t nbytes, loff_t off,
587 const char *max)
abb8206c 588{
71f87bee
JW
589 int ret, idx;
590 unsigned long nr_pages;
451af504 591 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
cdc2fcfe 592 bool rsvd = false;
abb8206c 593
71f87bee
JW
594 if (hugetlb_cgroup_is_root(h_cg)) /* Can't set limit on root */
595 return -EINVAL;
596
451af504 597 buf = strstrip(buf);
faced7e0 598 ret = page_counter_memparse(buf, max, &nr_pages);
71f87bee
JW
599 if (ret)
600 return ret;
601
451af504 602 idx = MEMFILE_IDX(of_cft(of)->private);
8938494c 603 nr_pages = round_down(nr_pages, pages_per_huge_page(&hstates[idx]));
abb8206c 604
71f87bee 605 switch (MEMFILE_ATTR(of_cft(of)->private)) {
cdc2fcfe
MA
606 case RES_RSVD_LIMIT:
607 rsvd = true;
e4a9bc58 608 fallthrough;
abb8206c 609 case RES_LIMIT:
71f87bee 610 mutex_lock(&hugetlb_limit_mutex);
cdc2fcfe 611 ret = page_counter_set_max(
1adc4d41 612 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
cdc2fcfe 613 nr_pages);
71f87bee 614 mutex_unlock(&hugetlb_limit_mutex);
abb8206c
AK
615 break;
616 default:
617 ret = -EINVAL;
618 break;
619 }
451af504 620 return ret ?: nbytes;
abb8206c
AK
621}
622
faced7e0
GS
623static ssize_t hugetlb_cgroup_write_legacy(struct kernfs_open_file *of,
624 char *buf, size_t nbytes, loff_t off)
625{
626 return hugetlb_cgroup_write(of, buf, nbytes, off, "-1");
627}
628
629static ssize_t hugetlb_cgroup_write_dfl(struct kernfs_open_file *of,
630 char *buf, size_t nbytes, loff_t off)
631{
632 return hugetlb_cgroup_write(of, buf, nbytes, off, "max");
633}
634
6770c64e
TH
635static ssize_t hugetlb_cgroup_reset(struct kernfs_open_file *of,
636 char *buf, size_t nbytes, loff_t off)
abb8206c 637{
71f87bee 638 int ret = 0;
cdc2fcfe 639 struct page_counter *counter, *rsvd_counter;
6770c64e 640 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
abb8206c 641
71f87bee 642 counter = &h_cg->hugepage[MEMFILE_IDX(of_cft(of)->private)];
cdc2fcfe 643 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(of_cft(of)->private)];
abb8206c 644
71f87bee 645 switch (MEMFILE_ATTR(of_cft(of)->private)) {
abb8206c 646 case RES_MAX_USAGE:
71f87bee 647 page_counter_reset_watermark(counter);
abb8206c 648 break;
cdc2fcfe
MA
649 case RES_RSVD_MAX_USAGE:
650 page_counter_reset_watermark(rsvd_counter);
651 break;
abb8206c 652 case RES_FAILCNT:
71f87bee 653 counter->failcnt = 0;
abb8206c 654 break;
cdc2fcfe
MA
655 case RES_RSVD_FAILCNT:
656 rsvd_counter->failcnt = 0;
657 break;
abb8206c
AK
658 default:
659 ret = -EINVAL;
660 break;
661 }
6770c64e 662 return ret ?: nbytes;
abb8206c
AK
663}
664
665static char *mem_fmt(char *buf, int size, unsigned long hsize)
666{
abfb09e2
ML
667 if (hsize >= SZ_1G)
668 snprintf(buf, size, "%luGB", hsize / SZ_1G);
669 else if (hsize >= SZ_1M)
670 snprintf(buf, size, "%luMB", hsize / SZ_1M);
abb8206c 671 else
abfb09e2 672 snprintf(buf, size, "%luKB", hsize / SZ_1K);
abb8206c
AK
673 return buf;
674}
675
faced7e0
GS
676static int __hugetlb_events_show(struct seq_file *seq, bool local)
677{
678 int idx;
679 long max;
680 struct cftype *cft = seq_cft(seq);
681 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
682
683 idx = MEMFILE_IDX(cft->private);
684
685 if (local)
686 max = atomic_long_read(&h_cg->events_local[idx][HUGETLB_MAX]);
687 else
688 max = atomic_long_read(&h_cg->events[idx][HUGETLB_MAX]);
689
690 seq_printf(seq, "max %lu\n", max);
691
692 return 0;
693}
694
695static int hugetlb_events_show(struct seq_file *seq, void *v)
696{
697 return __hugetlb_events_show(seq, false);
698}
699
700static int hugetlb_events_local_show(struct seq_file *seq, void *v)
701{
702 return __hugetlb_events_show(seq, true);
703}
704
705static void __init __hugetlb_cgroup_file_dfl_init(int idx)
abb8206c
AK
706{
707 char buf[32];
708 struct cftype *cft;
709 struct hstate *h = &hstates[idx];
710
711 /* format the size */
cdc2fcfe 712 mem_fmt(buf, sizeof(buf), huge_page_size(h));
abb8206c
AK
713
714 /* Add the limit file */
faced7e0
GS
715 cft = &h->cgroup_files_dfl[0];
716 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max", buf);
717 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
718 cft->seq_show = hugetlb_cgroup_read_u64_max;
719 cft->write = hugetlb_cgroup_write_dfl;
720 cft->flags = CFTYPE_NOT_ON_ROOT;
721
cdc2fcfe 722 /* Add the reservation limit file */
faced7e0 723 cft = &h->cgroup_files_dfl[1];
cdc2fcfe
MA
724 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max", buf);
725 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
726 cft->seq_show = hugetlb_cgroup_read_u64_max;
727 cft->write = hugetlb_cgroup_write_dfl;
728 cft->flags = CFTYPE_NOT_ON_ROOT;
729
730 /* Add the current usage file */
731 cft = &h->cgroup_files_dfl[2];
faced7e0
GS
732 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.current", buf);
733 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
734 cft->seq_show = hugetlb_cgroup_read_u64_max;
735 cft->flags = CFTYPE_NOT_ON_ROOT;
736
cdc2fcfe
MA
737 /* Add the current reservation usage file */
738 cft = &h->cgroup_files_dfl[3];
739 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.current", buf);
740 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
741 cft->seq_show = hugetlb_cgroup_read_u64_max;
742 cft->flags = CFTYPE_NOT_ON_ROOT;
743
faced7e0 744 /* Add the events file */
cdc2fcfe 745 cft = &h->cgroup_files_dfl[4];
faced7e0
GS
746 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events", buf);
747 cft->private = MEMFILE_PRIVATE(idx, 0);
748 cft->seq_show = hugetlb_events_show;
d5a16959 749 cft->file_offset = offsetof(struct hugetlb_cgroup, events_file[idx]);
faced7e0
GS
750 cft->flags = CFTYPE_NOT_ON_ROOT;
751
752 /* Add the events.local file */
cdc2fcfe 753 cft = &h->cgroup_files_dfl[5];
faced7e0
GS
754 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events.local", buf);
755 cft->private = MEMFILE_PRIVATE(idx, 0);
756 cft->seq_show = hugetlb_events_local_show;
757 cft->file_offset = offsetof(struct hugetlb_cgroup,
d5a16959 758 events_local_file[idx]);
faced7e0
GS
759 cft->flags = CFTYPE_NOT_ON_ROOT;
760
f4776199 761 /* Add the numa stat file */
cdc2fcfe 762 cft = &h->cgroup_files_dfl[6];
f4776199 763 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.numa_stat", buf);
2727cfe4 764 cft->private = MEMFILE_PRIVATE(idx, 0);
f4776199
MA
765 cft->seq_show = hugetlb_cgroup_read_numa_stat;
766 cft->flags = CFTYPE_NOT_ON_ROOT;
767
768 /* NULL terminate the last cft */
769 cft = &h->cgroup_files_dfl[7];
faced7e0
GS
770 memset(cft, 0, sizeof(*cft));
771
772 WARN_ON(cgroup_add_dfl_cftypes(&hugetlb_cgrp_subsys,
773 h->cgroup_files_dfl));
774}
775
776static void __init __hugetlb_cgroup_file_legacy_init(int idx)
777{
778 char buf[32];
779 struct cftype *cft;
780 struct hstate *h = &hstates[idx];
781
782 /* format the size */
cdc2fcfe 783 mem_fmt(buf, sizeof(buf), huge_page_size(h));
faced7e0
GS
784
785 /* Add the limit file */
786 cft = &h->cgroup_files_legacy[0];
abb8206c
AK
787 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
788 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
716f479d 789 cft->read_u64 = hugetlb_cgroup_read_u64;
faced7e0 790 cft->write = hugetlb_cgroup_write_legacy;
abb8206c 791
cdc2fcfe 792 /* Add the reservation limit file */
faced7e0 793 cft = &h->cgroup_files_legacy[1];
cdc2fcfe
MA
794 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.limit_in_bytes", buf);
795 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
796 cft->read_u64 = hugetlb_cgroup_read_u64;
797 cft->write = hugetlb_cgroup_write_legacy;
798
799 /* Add the usage file */
800 cft = &h->cgroup_files_legacy[2];
abb8206c
AK
801 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
802 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
716f479d 803 cft->read_u64 = hugetlb_cgroup_read_u64;
abb8206c 804
cdc2fcfe
MA
805 /* Add the reservation usage file */
806 cft = &h->cgroup_files_legacy[3];
807 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.usage_in_bytes", buf);
808 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
809 cft->read_u64 = hugetlb_cgroup_read_u64;
810
abb8206c 811 /* Add the MAX usage file */
cdc2fcfe 812 cft = &h->cgroup_files_legacy[4];
abb8206c
AK
813 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
814 cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
6770c64e 815 cft->write = hugetlb_cgroup_reset;
716f479d 816 cft->read_u64 = hugetlb_cgroup_read_u64;
abb8206c 817
cdc2fcfe
MA
818 /* Add the MAX reservation usage file */
819 cft = &h->cgroup_files_legacy[5];
820 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max_usage_in_bytes", buf);
821 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_MAX_USAGE);
822 cft->write = hugetlb_cgroup_reset;
823 cft->read_u64 = hugetlb_cgroup_read_u64;
824
abb8206c 825 /* Add the failcntfile */
cdc2fcfe 826 cft = &h->cgroup_files_legacy[6];
abb8206c 827 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
cdc2fcfe
MA
828 cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
829 cft->write = hugetlb_cgroup_reset;
830 cft->read_u64 = hugetlb_cgroup_read_u64;
831
832 /* Add the reservation failcntfile */
833 cft = &h->cgroup_files_legacy[7];
834 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.failcnt", buf);
835 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_FAILCNT);
6770c64e 836 cft->write = hugetlb_cgroup_reset;
716f479d 837 cft->read_u64 = hugetlb_cgroup_read_u64;
abb8206c 838
f4776199 839 /* Add the numa stat file */
cdc2fcfe 840 cft = &h->cgroup_files_legacy[8];
f4776199
MA
841 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.numa_stat", buf);
842 cft->private = MEMFILE_PRIVATE(idx, 1);
843 cft->seq_show = hugetlb_cgroup_read_numa_stat;
844
845 /* NULL terminate the last cft */
846 cft = &h->cgroup_files_legacy[9];
abb8206c
AK
847 memset(cft, 0, sizeof(*cft));
848
2cf669a5 849 WARN_ON(cgroup_add_legacy_cftypes(&hugetlb_cgrp_subsys,
faced7e0
GS
850 h->cgroup_files_legacy));
851}
852
853static void __init __hugetlb_cgroup_file_init(int idx)
854{
855 __hugetlb_cgroup_file_dfl_init(idx);
856 __hugetlb_cgroup_file_legacy_init(idx);
7179e7bf
JW
857}
858
859void __init hugetlb_cgroup_file_init(void)
860{
861 struct hstate *h;
862
59838b25
FL
863 for_each_hstate(h)
864 __hugetlb_cgroup_file_init(hstate_index(h));
abb8206c
AK
865}
866
75754681
AK
867/*
868 * hugetlb_lock will make sure a parallel cgroup rmdir won't happen
869 * when we migrate hugepages
870 */
29f39430 871void hugetlb_cgroup_migrate(struct folio *old_folio, struct folio *new_folio)
8e6ac7fa
AK
872{
873 struct hugetlb_cgroup *h_cg;
1adc4d41 874 struct hugetlb_cgroup *h_cg_rsvd;
29f39430 875 struct hstate *h = folio_hstate(old_folio);
8e6ac7fa
AK
876
877 if (hugetlb_cgroup_disabled())
878 return;
879
db71ef79 880 spin_lock_irq(&hugetlb_lock);
f074732d
SK
881 h_cg = hugetlb_cgroup_from_folio(old_folio);
882 h_cg_rsvd = hugetlb_cgroup_from_folio_rsvd(old_folio);
de656ed3
SK
883 set_hugetlb_cgroup(old_folio, NULL);
884 set_hugetlb_cgroup_rsvd(old_folio, NULL);
8e6ac7fa
AK
885
886 /* move the h_cg details to new cgroup */
de656ed3
SK
887 set_hugetlb_cgroup(new_folio, h_cg);
888 set_hugetlb_cgroup_rsvd(new_folio, h_cg_rsvd);
29f39430 889 list_move(&new_folio->lru, &h->hugepage_activelist);
db71ef79 890 spin_unlock_irq(&hugetlb_lock);
8e6ac7fa
AK
891 return;
892}
893
faced7e0
GS
894static struct cftype hugetlb_files[] = {
895 {} /* terminate */
896};
897
073219e9 898struct cgroup_subsys hugetlb_cgrp_subsys = {
92fb9748
TH
899 .css_alloc = hugetlb_cgroup_css_alloc,
900 .css_offline = hugetlb_cgroup_css_offline,
901 .css_free = hugetlb_cgroup_css_free,
faced7e0
GS
902 .dfl_cftypes = hugetlb_files,
903 .legacy_cftypes = hugetlb_files,
2bc64a20 904};