blob: 79d93534ef1e8a9ad47ec98d883938d7aed0e1e7 [file] [log] [blame]
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -07001/*
2 *
3 * Copyright IBM Corporation, 2012
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 *
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +01006 * Cgroup v2
7 * Copyright (C) 2019 Red Hat, Inc.
8 * Author: Giuseppe Scrivano <gscrivan@redhat.com>
9 *
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070010 * 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>
Johannes Weiner71f87bee2014-12-10 15:42:34 -080021#include <linux/page_counter.h>
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070022#include <linux/slab.h>
23#include <linux/hugetlb.h>
24#include <linux/hugetlb_cgroup.h>
25
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -070026#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
27#define MEMFILE_IDX(val) (((val) >> 16) & 0xffff)
28#define MEMFILE_ATTR(val) ((val) & 0xffff)
29
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070030static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
31
Mina Almasrycdc2fcf2020-04-01 21:11:11 -070032static inline struct page_counter *
Mina Almasry1adc4d42020-04-01 21:11:15 -070033__hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx,
34 bool rsvd)
Mina Almasrycdc2fcf2020-04-01 21:11:11 -070035{
36 if (rsvd)
37 return &h_cg->rsvd_hugepage[idx];
38 return &h_cg->hugepage[idx];
39}
40
Mina Almasry1adc4d42020-04-01 21:11:15 -070041static 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
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070053static inline
54struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
55{
Tejun Heoa7c6d552013-08-08 20:11:23 -040056 return s ? container_of(s, struct hugetlb_cgroup, css) : NULL;
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070057}
58
59static inline
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070060struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task)
61{
Tejun Heo073219e2014-02-08 10:36:58 -050062 return hugetlb_cgroup_from_css(task_css(task, hugetlb_cgrp_id));
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070063}
64
65static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
66{
67 return (h_cg == root_h_cgroup);
68}
69
Tejun Heo3f798512013-08-08 20:11:22 -040070static inline struct hugetlb_cgroup *
71parent_hugetlb_cgroup(struct hugetlb_cgroup *h_cg)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070072{
Tejun Heo5c9d5352014-05-16 13:22:48 -040073 return hugetlb_cgroup_from_css(h_cg->css.parent);
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070074}
75
Tejun Heo3f798512013-08-08 20:11:22 -040076static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070077{
78 int idx;
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070079
80 for (idx = 0; idx < hugetlb_max_hstate; idx++) {
Mina Almasry1adc4d42020-04-01 21:11:15 -070081 if (page_counter_read(
Mike Kravetz7a5bde32020-12-05 22:15:12 -080082 hugetlb_cgroup_counter_from_cgroup(h_cg, idx)))
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070083 return true;
84 }
85 return false;
86}
87
David Rientjes297880f2016-05-20 16:57:50 -070088static 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++) {
Mina Almasry1adc4d42020-04-01 21:11:15 -070094 struct page_counter *fault_parent = NULL;
95 struct page_counter *rsvd_parent = NULL;
David Rientjes297880f2016-05-20 16:57:50 -070096 unsigned long limit;
97 int ret;
98
Mina Almasry1adc4d42020-04-01 21:11:15 -070099 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);
David Rientjes297880f2016-05-20 16:57:50 -0700111
112 limit = round_down(PAGE_COUNTER_MAX,
Miaohe Lin89384942021-02-24 12:07:36 -0800113 pages_per_huge_page(&hstates[idx]));
Mina Almasry1adc4d42020-04-01 21:11:15 -0700114
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);
David Rientjes297880f2016-05-20 16:57:50 -0700122 VM_BUG_ON(ret);
123 }
124}
125
Tejun Heoeb954192013-08-08 20:11:23 -0400126static struct cgroup_subsys_state *
127hugetlb_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700128{
Tejun Heoeb954192013-08-08 20:11:23 -0400129 struct hugetlb_cgroup *parent_h_cgroup = hugetlb_cgroup_from_css(parent_css);
130 struct hugetlb_cgroup *h_cgroup;
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700131
132 h_cgroup = kzalloc(sizeof(*h_cgroup), GFP_KERNEL);
133 if (!h_cgroup)
134 return ERR_PTR(-ENOMEM);
135
David Rientjes297880f2016-05-20 16:57:50 -0700136 if (!parent_h_cgroup)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700137 root_h_cgroup = h_cgroup;
David Rientjes297880f2016-05-20 16:57:50 -0700138
139 hugetlb_cgroup_init(h_cgroup, parent_h_cgroup);
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700140 return &h_cgroup->css;
141}
142
Tejun Heoeb954192013-08-08 20:11:23 -0400143static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700144{
145 struct hugetlb_cgroup *h_cgroup;
146
Tejun Heoeb954192013-08-08 20:11:23 -0400147 h_cgroup = hugetlb_cgroup_from_css(css);
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700148 kfree(h_cgroup);
149}
150
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700151/*
152 * Should be called with hugetlb_lock held.
153 * Since we are holding hugetlb_lock, pages cannot get moved from
154 * active list or uncharged from the cgroup, So no need to get
155 * page reference and test for page active here. This function
156 * cannot fail.
157 */
Tejun Heo3f798512013-08-08 20:11:22 -0400158static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700159 struct page *page)
160{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800161 unsigned int nr_pages;
162 struct page_counter *counter;
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700163 struct hugetlb_cgroup *page_hcg;
Tejun Heo3f798512013-08-08 20:11:22 -0400164 struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(h_cg);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700165
166 page_hcg = hugetlb_cgroup_from_page(page);
167 /*
168 * We can have pages in active list without any cgroup
169 * ie, hugepage with less than 3 pages. We can safely
170 * ignore those pages.
171 */
172 if (!page_hcg || page_hcg != h_cg)
173 goto out;
174
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700175 nr_pages = compound_nr(page);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700176 if (!parent) {
177 parent = root_h_cgroup;
178 /* root has no limit */
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800179 page_counter_charge(&parent->hugepage[idx], nr_pages);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700180 }
181 counter = &h_cg->hugepage[idx];
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800182 /* Take the pages off the local counter */
183 page_counter_cancel(counter, nr_pages);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700184
185 set_hugetlb_cgroup(page, parent);
186out:
187 return;
188}
189
190/*
191 * Force the hugetlb cgroup to empty the hugetlb resources by moving them to
192 * the parent cgroup.
193 */
Tejun Heoeb954192013-08-08 20:11:23 -0400194static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700195{
Tejun Heoeb954192013-08-08 20:11:23 -0400196 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700197 struct hstate *h;
198 struct page *page;
Mike Kravetz7a5bde32020-12-05 22:15:12 -0800199 int idx;
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700200
201 do {
Mike Kravetz7a5bde32020-12-05 22:15:12 -0800202 idx = 0;
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700203 for_each_hstate(h) {
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700204 spin_lock_irq(&hugetlb_lock);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700205 list_for_each_entry(page, &h->hugepage_activelist, lru)
Tejun Heo3f798512013-08-08 20:11:22 -0400206 hugetlb_cgroup_move_parent(idx, h_cg, page);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700207
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700208 spin_unlock_irq(&hugetlb_lock);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700209 idx++;
210 }
211 cond_resched();
Tejun Heo3f798512013-08-08 20:11:22 -0400212 } while (hugetlb_cgroup_have_usage(h_cg));
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700213}
214
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100215static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx,
216 enum hugetlb_memory_event event)
217{
218 atomic_long_inc(&hugetlb->events_local[idx][event]);
219 cgroup_file_notify(&hugetlb->events_local_file[idx]);
220
221 do {
222 atomic_long_inc(&hugetlb->events[idx][event]);
223 cgroup_file_notify(&hugetlb->events_file[idx]);
224 } while ((hugetlb = parent_hugetlb_cgroup(hugetlb)) &&
225 !hugetlb_cgroup_is_root(hugetlb));
226}
227
Mina Almasry1adc4d42020-04-01 21:11:15 -0700228static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
229 struct hugetlb_cgroup **ptr,
230 bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700231{
232 int ret = 0;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800233 struct page_counter *counter;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700234 struct hugetlb_cgroup *h_cg = NULL;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700235
236 if (hugetlb_cgroup_disabled())
237 goto done;
238 /*
239 * We don't charge any cgroup if the compound page have less
240 * than 3 pages.
241 */
242 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
243 goto done;
244again:
245 rcu_read_lock();
246 h_cg = hugetlb_cgroup_from_task(current);
Roman Gushchin0362f322019-11-15 17:34:46 -0800247 if (!css_tryget(&h_cg->css)) {
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700248 rcu_read_unlock();
249 goto again;
250 }
251 rcu_read_unlock();
252
Mina Almasry1adc4d42020-04-01 21:11:15 -0700253 if (!page_counter_try_charge(
254 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
255 nr_pages, &counter)) {
Johannes Weiner6071ca52015-11-05 18:50:26 -0800256 ret = -ENOMEM;
Mina Almasry726b7bb2020-03-28 19:17:22 -0700257 hugetlb_event(h_cg, idx, HUGETLB_MAX);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700258 css_put(&h_cg->css);
259 goto done;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100260 }
Mina Almasry1adc4d42020-04-01 21:11:15 -0700261 /* Reservations take a reference to the css because they do not get
262 * reparented.
263 */
264 if (!rsvd)
265 css_put(&h_cg->css);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700266done:
267 *ptr = h_cg;
268 return ret;
269}
270
Mina Almasry1adc4d42020-04-01 21:11:15 -0700271int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
272 struct hugetlb_cgroup **ptr)
273{
274 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, false);
275}
276
277int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
278 struct hugetlb_cgroup **ptr)
279{
280 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, true);
281}
282
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -0700283/* Should be called with hugetlb_lock held */
Mina Almasry1adc4d42020-04-01 21:11:15 -0700284static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
285 struct hugetlb_cgroup *h_cg,
286 struct page *page, bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700287{
288 if (hugetlb_cgroup_disabled() || !h_cg)
289 return;
290
Mina Almasry1adc4d42020-04-01 21:11:15 -0700291 __set_hugetlb_cgroup(page, h_cg, rsvd);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700292 return;
293}
294
Mina Almasry1adc4d42020-04-01 21:11:15 -0700295void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
296 struct hugetlb_cgroup *h_cg,
297 struct page *page)
298{
299 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, false);
300}
301
302void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
303 struct hugetlb_cgroup *h_cg,
304 struct page *page)
305{
306 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, true);
307}
308
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700309/*
310 * Should be called with hugetlb_lock held
311 */
Mina Almasry1adc4d42020-04-01 21:11:15 -0700312static void __hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
313 struct page *page, bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700314{
315 struct hugetlb_cgroup *h_cg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700316
317 if (hugetlb_cgroup_disabled())
318 return;
Michal Hocko7ea85742014-08-29 15:18:42 -0700319 lockdep_assert_held(&hugetlb_lock);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700320 h_cg = __hugetlb_cgroup_from_page(page, rsvd);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700321 if (unlikely(!h_cg))
322 return;
Mina Almasry1adc4d42020-04-01 21:11:15 -0700323 __set_hugetlb_cgroup(page, NULL, rsvd);
324
325 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
326 rsvd),
327 nr_pages);
328
329 if (rsvd)
330 css_put(&h_cg->css);
331
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700332 return;
333}
334
Mina Almasry1adc4d42020-04-01 21:11:15 -0700335void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
336 struct page *page)
337{
338 __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, false);
339}
340
341void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages,
342 struct page *page)
343{
344 __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, true);
345}
346
347static void __hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
348 struct hugetlb_cgroup *h_cg,
349 bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700350{
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700351 if (hugetlb_cgroup_disabled() || !h_cg)
352 return;
353
354 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
355 return;
356
Mina Almasry1adc4d42020-04-01 21:11:15 -0700357 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
358 rsvd),
359 nr_pages);
360
361 if (rsvd)
362 css_put(&h_cg->css);
363}
364
365void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
366 struct hugetlb_cgroup *h_cg)
367{
368 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, false);
369}
370
371void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
372 struct hugetlb_cgroup *h_cg)
373{
374 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, true);
375}
376
Mina Almasrye9fe92a2020-04-01 21:11:21 -0700377void hugetlb_cgroup_uncharge_counter(struct resv_map *resv, unsigned long start,
378 unsigned long end)
Mina Almasry1adc4d42020-04-01 21:11:15 -0700379{
Mina Almasrye9fe92a2020-04-01 21:11:21 -0700380 if (hugetlb_cgroup_disabled() || !resv || !resv->reservation_counter ||
381 !resv->css)
Mina Almasry1adc4d42020-04-01 21:11:15 -0700382 return;
383
Mina Almasrye9fe92a2020-04-01 21:11:21 -0700384 page_counter_uncharge(resv->reservation_counter,
385 (end - start) * resv->pages_per_hpage);
386 css_put(resv->css);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700387}
388
Mina Almasry075a61d2020-04-01 21:11:28 -0700389void hugetlb_cgroup_uncharge_file_region(struct resv_map *resv,
390 struct file_region *rg,
Miaohe Lind85aecf2021-03-24 21:37:17 -0700391 unsigned long nr_pages,
392 bool region_del)
Mina Almasry075a61d2020-04-01 21:11:28 -0700393{
394 if (hugetlb_cgroup_disabled() || !resv || !rg || !nr_pages)
395 return;
396
397 if (rg->reservation_counter && resv->pages_per_hpage && nr_pages > 0 &&
398 !resv->reservation_counter) {
399 page_counter_uncharge(rg->reservation_counter,
400 nr_pages * resv->pages_per_hpage);
Miaohe Lind85aecf2021-03-24 21:37:17 -0700401 /*
402 * Only do css_put(rg->css) when we delete the entire region
403 * because one file_region must hold exactly one css reference.
404 */
405 if (region_del)
406 css_put(rg->css);
Mina Almasry075a61d2020-04-01 21:11:28 -0700407 }
408}
409
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800410enum {
411 RES_USAGE,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700412 RES_RSVD_USAGE,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800413 RES_LIMIT,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700414 RES_RSVD_LIMIT,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800415 RES_MAX_USAGE,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700416 RES_RSVD_MAX_USAGE,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800417 RES_FAILCNT,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700418 RES_RSVD_FAILCNT,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800419};
420
Tejun Heo716f4792013-12-05 12:28:03 -0500421static u64 hugetlb_cgroup_read_u64(struct cgroup_subsys_state *css,
422 struct cftype *cft)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700423{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800424 struct page_counter *counter;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700425 struct page_counter *rsvd_counter;
Tejun Heo182446d2013-08-08 20:11:24 -0400426 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700427
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800428 counter = &h_cg->hugepage[MEMFILE_IDX(cft->private)];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700429 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(cft->private)];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700430
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800431 switch (MEMFILE_ATTR(cft->private)) {
432 case RES_USAGE:
433 return (u64)page_counter_read(counter) * PAGE_SIZE;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700434 case RES_RSVD_USAGE:
435 return (u64)page_counter_read(rsvd_counter) * PAGE_SIZE;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800436 case RES_LIMIT:
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700437 return (u64)counter->max * PAGE_SIZE;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700438 case RES_RSVD_LIMIT:
439 return (u64)rsvd_counter->max * PAGE_SIZE;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800440 case RES_MAX_USAGE:
441 return (u64)counter->watermark * PAGE_SIZE;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700442 case RES_RSVD_MAX_USAGE:
443 return (u64)rsvd_counter->watermark * PAGE_SIZE;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800444 case RES_FAILCNT:
445 return counter->failcnt;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700446 case RES_RSVD_FAILCNT:
447 return rsvd_counter->failcnt;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800448 default:
449 BUG();
450 }
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700451}
452
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100453static int hugetlb_cgroup_read_u64_max(struct seq_file *seq, void *v)
454{
455 int idx;
456 u64 val;
457 struct cftype *cft = seq_cft(seq);
458 unsigned long limit;
459 struct page_counter *counter;
460 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
461
462 idx = MEMFILE_IDX(cft->private);
463 counter = &h_cg->hugepage[idx];
464
465 limit = round_down(PAGE_COUNTER_MAX,
Miaohe Lin89384942021-02-24 12:07:36 -0800466 pages_per_huge_page(&hstates[idx]));
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100467
468 switch (MEMFILE_ATTR(cft->private)) {
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700469 case RES_RSVD_USAGE:
470 counter = &h_cg->rsvd_hugepage[idx];
Joe Perchese4a9bc52020-04-06 20:08:39 -0700471 fallthrough;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100472 case RES_USAGE:
473 val = (u64)page_counter_read(counter);
474 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
475 break;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700476 case RES_RSVD_LIMIT:
477 counter = &h_cg->rsvd_hugepage[idx];
Joe Perchese4a9bc52020-04-06 20:08:39 -0700478 fallthrough;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100479 case RES_LIMIT:
480 val = (u64)counter->max;
481 if (val == limit)
482 seq_puts(seq, "max\n");
483 else
484 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
485 break;
486 default:
487 BUG();
488 }
489
490 return 0;
491}
492
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800493static DEFINE_MUTEX(hugetlb_limit_mutex);
494
Tejun Heo451af502014-05-13 12:16:21 -0400495static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of,
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100496 char *buf, size_t nbytes, loff_t off,
497 const char *max)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700498{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800499 int ret, idx;
500 unsigned long nr_pages;
Tejun Heo451af502014-05-13 12:16:21 -0400501 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700502 bool rsvd = false;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700503
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800504 if (hugetlb_cgroup_is_root(h_cg)) /* Can't set limit on root */
505 return -EINVAL;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700506
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800507 buf = strstrip(buf);
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100508 ret = page_counter_memparse(buf, max, &nr_pages);
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800509 if (ret)
510 return ret;
511
512 idx = MEMFILE_IDX(of_cft(of)->private);
Miaohe Lin89384942021-02-24 12:07:36 -0800513 nr_pages = round_down(nr_pages, pages_per_huge_page(&hstates[idx]));
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800514
515 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700516 case RES_RSVD_LIMIT:
517 rsvd = true;
Joe Perchese4a9bc52020-04-06 20:08:39 -0700518 fallthrough;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700519 case RES_LIMIT:
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800520 mutex_lock(&hugetlb_limit_mutex);
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700521 ret = page_counter_set_max(
Mina Almasry1adc4d42020-04-01 21:11:15 -0700522 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700523 nr_pages);
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800524 mutex_unlock(&hugetlb_limit_mutex);
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700525 break;
526 default:
527 ret = -EINVAL;
528 break;
529 }
Tejun Heo451af502014-05-13 12:16:21 -0400530 return ret ?: nbytes;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700531}
532
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100533static ssize_t hugetlb_cgroup_write_legacy(struct kernfs_open_file *of,
534 char *buf, size_t nbytes, loff_t off)
535{
536 return hugetlb_cgroup_write(of, buf, nbytes, off, "-1");
537}
538
539static ssize_t hugetlb_cgroup_write_dfl(struct kernfs_open_file *of,
540 char *buf, size_t nbytes, loff_t off)
541{
542 return hugetlb_cgroup_write(of, buf, nbytes, off, "max");
543}
544
Tejun Heo6770c642014-05-13 12:16:21 -0400545static ssize_t hugetlb_cgroup_reset(struct kernfs_open_file *of,
546 char *buf, size_t nbytes, loff_t off)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700547{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800548 int ret = 0;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700549 struct page_counter *counter, *rsvd_counter;
Tejun Heo6770c642014-05-13 12:16:21 -0400550 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700551
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800552 counter = &h_cg->hugepage[MEMFILE_IDX(of_cft(of)->private)];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700553 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(of_cft(of)->private)];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700554
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800555 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700556 case RES_MAX_USAGE:
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800557 page_counter_reset_watermark(counter);
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700558 break;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700559 case RES_RSVD_MAX_USAGE:
560 page_counter_reset_watermark(rsvd_counter);
561 break;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700562 case RES_FAILCNT:
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800563 counter->failcnt = 0;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700564 break;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700565 case RES_RSVD_FAILCNT:
566 rsvd_counter->failcnt = 0;
567 break;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700568 default:
569 ret = -EINVAL;
570 break;
571 }
Tejun Heo6770c642014-05-13 12:16:21 -0400572 return ret ?: nbytes;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700573}
574
575static char *mem_fmt(char *buf, int size, unsigned long hsize)
576{
577 if (hsize >= (1UL << 30))
578 snprintf(buf, size, "%luGB", hsize >> 30);
579 else if (hsize >= (1UL << 20))
580 snprintf(buf, size, "%luMB", hsize >> 20);
581 else
582 snprintf(buf, size, "%luKB", hsize >> 10);
583 return buf;
584}
585
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100586static int __hugetlb_events_show(struct seq_file *seq, bool local)
587{
588 int idx;
589 long max;
590 struct cftype *cft = seq_cft(seq);
591 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
592
593 idx = MEMFILE_IDX(cft->private);
594
595 if (local)
596 max = atomic_long_read(&h_cg->events_local[idx][HUGETLB_MAX]);
597 else
598 max = atomic_long_read(&h_cg->events[idx][HUGETLB_MAX]);
599
600 seq_printf(seq, "max %lu\n", max);
601
602 return 0;
603}
604
605static int hugetlb_events_show(struct seq_file *seq, void *v)
606{
607 return __hugetlb_events_show(seq, false);
608}
609
610static int hugetlb_events_local_show(struct seq_file *seq, void *v)
611{
612 return __hugetlb_events_show(seq, true);
613}
614
615static void __init __hugetlb_cgroup_file_dfl_init(int idx)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700616{
617 char buf[32];
618 struct cftype *cft;
619 struct hstate *h = &hstates[idx];
620
621 /* format the size */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700622 mem_fmt(buf, sizeof(buf), huge_page_size(h));
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700623
624 /* Add the limit file */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100625 cft = &h->cgroup_files_dfl[0];
626 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max", buf);
627 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
628 cft->seq_show = hugetlb_cgroup_read_u64_max;
629 cft->write = hugetlb_cgroup_write_dfl;
630 cft->flags = CFTYPE_NOT_ON_ROOT;
631
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700632 /* Add the reservation limit file */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100633 cft = &h->cgroup_files_dfl[1];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700634 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max", buf);
635 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
636 cft->seq_show = hugetlb_cgroup_read_u64_max;
637 cft->write = hugetlb_cgroup_write_dfl;
638 cft->flags = CFTYPE_NOT_ON_ROOT;
639
640 /* Add the current usage file */
641 cft = &h->cgroup_files_dfl[2];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100642 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.current", buf);
643 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
644 cft->seq_show = hugetlb_cgroup_read_u64_max;
645 cft->flags = CFTYPE_NOT_ON_ROOT;
646
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700647 /* Add the current reservation usage file */
648 cft = &h->cgroup_files_dfl[3];
649 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.current", buf);
650 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
651 cft->seq_show = hugetlb_cgroup_read_u64_max;
652 cft->flags = CFTYPE_NOT_ON_ROOT;
653
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100654 /* Add the events file */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700655 cft = &h->cgroup_files_dfl[4];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100656 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events", buf);
657 cft->private = MEMFILE_PRIVATE(idx, 0);
658 cft->seq_show = hugetlb_events_show;
Xu Wangd5a16952020-08-20 17:41:59 -0700659 cft->file_offset = offsetof(struct hugetlb_cgroup, events_file[idx]);
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100660 cft->flags = CFTYPE_NOT_ON_ROOT;
661
662 /* Add the events.local file */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700663 cft = &h->cgroup_files_dfl[5];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100664 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events.local", buf);
665 cft->private = MEMFILE_PRIVATE(idx, 0);
666 cft->seq_show = hugetlb_events_local_show;
667 cft->file_offset = offsetof(struct hugetlb_cgroup,
Xu Wangd5a16952020-08-20 17:41:59 -0700668 events_local_file[idx]);
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100669 cft->flags = CFTYPE_NOT_ON_ROOT;
670
671 /* NULL terminate the last cft */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700672 cft = &h->cgroup_files_dfl[6];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100673 memset(cft, 0, sizeof(*cft));
674
675 WARN_ON(cgroup_add_dfl_cftypes(&hugetlb_cgrp_subsys,
676 h->cgroup_files_dfl));
677}
678
679static void __init __hugetlb_cgroup_file_legacy_init(int idx)
680{
681 char buf[32];
682 struct cftype *cft;
683 struct hstate *h = &hstates[idx];
684
685 /* format the size */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700686 mem_fmt(buf, sizeof(buf), huge_page_size(h));
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100687
688 /* Add the limit file */
689 cft = &h->cgroup_files_legacy[0];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700690 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
691 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
Tejun Heo716f4792013-12-05 12:28:03 -0500692 cft->read_u64 = hugetlb_cgroup_read_u64;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100693 cft->write = hugetlb_cgroup_write_legacy;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700694
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700695 /* Add the reservation limit file */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100696 cft = &h->cgroup_files_legacy[1];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700697 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.limit_in_bytes", buf);
698 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
699 cft->read_u64 = hugetlb_cgroup_read_u64;
700 cft->write = hugetlb_cgroup_write_legacy;
701
702 /* Add the usage file */
703 cft = &h->cgroup_files_legacy[2];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700704 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
705 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
Tejun Heo716f4792013-12-05 12:28:03 -0500706 cft->read_u64 = hugetlb_cgroup_read_u64;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700707
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700708 /* Add the reservation usage file */
709 cft = &h->cgroup_files_legacy[3];
710 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.usage_in_bytes", buf);
711 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
712 cft->read_u64 = hugetlb_cgroup_read_u64;
713
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700714 /* Add the MAX usage file */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700715 cft = &h->cgroup_files_legacy[4];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700716 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
717 cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
Tejun Heo6770c642014-05-13 12:16:21 -0400718 cft->write = hugetlb_cgroup_reset;
Tejun Heo716f4792013-12-05 12:28:03 -0500719 cft->read_u64 = hugetlb_cgroup_read_u64;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700720
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700721 /* Add the MAX reservation usage file */
722 cft = &h->cgroup_files_legacy[5];
723 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max_usage_in_bytes", buf);
724 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_MAX_USAGE);
725 cft->write = hugetlb_cgroup_reset;
726 cft->read_u64 = hugetlb_cgroup_read_u64;
727
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700728 /* Add the failcntfile */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700729 cft = &h->cgroup_files_legacy[6];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700730 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700731 cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
732 cft->write = hugetlb_cgroup_reset;
733 cft->read_u64 = hugetlb_cgroup_read_u64;
734
735 /* Add the reservation failcntfile */
736 cft = &h->cgroup_files_legacy[7];
737 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.failcnt", buf);
738 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_FAILCNT);
Tejun Heo6770c642014-05-13 12:16:21 -0400739 cft->write = hugetlb_cgroup_reset;
Tejun Heo716f4792013-12-05 12:28:03 -0500740 cft->read_u64 = hugetlb_cgroup_read_u64;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700741
742 /* NULL terminate the last cft */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700743 cft = &h->cgroup_files_legacy[8];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700744 memset(cft, 0, sizeof(*cft));
745
Tejun Heo2cf669a2014-07-15 11:05:09 -0400746 WARN_ON(cgroup_add_legacy_cftypes(&hugetlb_cgrp_subsys,
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100747 h->cgroup_files_legacy));
748}
749
750static void __init __hugetlb_cgroup_file_init(int idx)
751{
752 __hugetlb_cgroup_file_dfl_init(idx);
753 __hugetlb_cgroup_file_legacy_init(idx);
Jianguo Wu7179e7b2012-12-18 14:23:19 -0800754}
755
756void __init hugetlb_cgroup_file_init(void)
757{
758 struct hstate *h;
759
760 for_each_hstate(h) {
761 /*
762 * Add cgroup control files only if the huge page consists
763 * of more than two normal pages. This is because we use
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800764 * page[2].private for storing cgroup details.
Jianguo Wu7179e7b2012-12-18 14:23:19 -0800765 */
766 if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER)
767 __hugetlb_cgroup_file_init(hstate_index(h));
768 }
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700769}
770
Aneesh Kumar K.V75754682012-07-31 16:42:36 -0700771/*
772 * hugetlb_lock will make sure a parallel cgroup rmdir won't happen
773 * when we migrate hugepages
774 */
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700775void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)
776{
777 struct hugetlb_cgroup *h_cg;
Mina Almasry1adc4d42020-04-01 21:11:15 -0700778 struct hugetlb_cgroup *h_cg_rsvd;
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -0700779 struct hstate *h = page_hstate(oldhpage);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700780
781 if (hugetlb_cgroup_disabled())
782 return;
783
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700784 spin_lock_irq(&hugetlb_lock);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700785 h_cg = hugetlb_cgroup_from_page(oldhpage);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700786 h_cg_rsvd = hugetlb_cgroup_from_page_rsvd(oldhpage);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700787 set_hugetlb_cgroup(oldhpage, NULL);
Mina Almasry98088952020-04-01 21:11:18 -0700788 set_hugetlb_cgroup_rsvd(oldhpage, NULL);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700789
790 /* move the h_cg details to new cgroup */
Mina Almasry98088952020-04-01 21:11:18 -0700791 set_hugetlb_cgroup(newhpage, h_cg);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700792 set_hugetlb_cgroup_rsvd(newhpage, h_cg_rsvd);
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -0700793 list_move(&newhpage->lru, &h->hugepage_activelist);
Mike Kravetzdb71ef72021-05-04 18:35:07 -0700794 spin_unlock_irq(&hugetlb_lock);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700795 return;
796}
797
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100798static struct cftype hugetlb_files[] = {
799 {} /* terminate */
800};
801
Tejun Heo073219e2014-02-08 10:36:58 -0500802struct cgroup_subsys hugetlb_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800803 .css_alloc = hugetlb_cgroup_css_alloc,
804 .css_offline = hugetlb_cgroup_css_offline,
805 .css_free = hugetlb_cgroup_css_free,
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100806 .dfl_cftypes = hugetlb_files,
807 .legacy_cftypes = hugetlb_files,
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700808};