blob: 0aef0f34b80a8b9f465f95924051d60b5c90ff3e [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
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +010026enum hugetlb_memory_event {
27 HUGETLB_MAX,
28 HUGETLB_NR_MEMORY_EVENTS,
29};
30
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070031struct hugetlb_cgroup {
32 struct cgroup_subsys_state css;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +010033
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070034 /*
35 * the counter to account for hugepages from hugetlb.
36 */
Johannes Weiner71f87bee2014-12-10 15:42:34 -080037 struct page_counter hugepage[HUGE_MAX_HSTATE];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +010038
Mina Almasrycdc2fcf2020-04-01 21:11:11 -070039 /*
40 * the counter to account for hugepage reservations from hugetlb.
41 */
42 struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE];
43
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +010044 atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
45 atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
46
47 /* Handle for "hugetlb.events" */
48 struct cgroup_file events_file[HUGE_MAX_HSTATE];
49
50 /* Handle for "hugetlb.events.local" */
51 struct cgroup_file events_local_file[HUGE_MAX_HSTATE];
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070052};
53
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -070054#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
55#define MEMFILE_IDX(val) (((val) >> 16) & 0xffff)
56#define MEMFILE_ATTR(val) ((val) & 0xffff)
57
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +010058#define hugetlb_cgroup_from_counter(counter, idx) \
59 container_of(counter, struct hugetlb_cgroup, hugepage[idx])
60
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070061static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
62
Mina Almasrycdc2fcf2020-04-01 21:11:11 -070063static inline struct page_counter *
Mina Almasry1adc4d42020-04-01 21:11:15 -070064__hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx,
65 bool rsvd)
Mina Almasrycdc2fcf2020-04-01 21:11:11 -070066{
67 if (rsvd)
68 return &h_cg->rsvd_hugepage[idx];
69 return &h_cg->hugepage[idx];
70}
71
Mina Almasry1adc4d42020-04-01 21:11:15 -070072static inline struct page_counter *
73hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx)
74{
75 return __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, false);
76}
77
78static inline struct page_counter *
79hugetlb_cgroup_counter_from_cgroup_rsvd(struct hugetlb_cgroup *h_cg, int idx)
80{
81 return __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, true);
82}
83
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070084static inline
85struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
86{
Tejun Heoa7c6d552013-08-08 20:11:23 -040087 return s ? container_of(s, struct hugetlb_cgroup, css) : NULL;
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070088}
89
90static inline
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070091struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task)
92{
Tejun Heo073219e2014-02-08 10:36:58 -050093 return hugetlb_cgroup_from_css(task_css(task, hugetlb_cgrp_id));
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -070094}
95
96static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
97{
98 return (h_cg == root_h_cgroup);
99}
100
Tejun Heo3f798512013-08-08 20:11:22 -0400101static inline struct hugetlb_cgroup *
102parent_hugetlb_cgroup(struct hugetlb_cgroup *h_cg)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700103{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400104 return hugetlb_cgroup_from_css(h_cg->css.parent);
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700105}
106
Tejun Heo3f798512013-08-08 20:11:22 -0400107static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700108{
109 int idx;
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700110
111 for (idx = 0; idx < hugetlb_max_hstate; idx++) {
Mina Almasry1adc4d42020-04-01 21:11:15 -0700112 if (page_counter_read(
113 hugetlb_cgroup_counter_from_cgroup(h_cg, idx)) ||
114 page_counter_read(hugetlb_cgroup_counter_from_cgroup_rsvd(
115 h_cg, idx))) {
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700116 return true;
Mina Almasry1adc4d42020-04-01 21:11:15 -0700117 }
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700118 }
119 return false;
120}
121
David Rientjes297880f2016-05-20 16:57:50 -0700122static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup,
123 struct hugetlb_cgroup *parent_h_cgroup)
124{
125 int idx;
126
127 for (idx = 0; idx < HUGE_MAX_HSTATE; idx++) {
Mina Almasry1adc4d42020-04-01 21:11:15 -0700128 struct page_counter *fault_parent = NULL;
129 struct page_counter *rsvd_parent = NULL;
David Rientjes297880f2016-05-20 16:57:50 -0700130 unsigned long limit;
131 int ret;
132
Mina Almasry1adc4d42020-04-01 21:11:15 -0700133 if (parent_h_cgroup) {
134 fault_parent = hugetlb_cgroup_counter_from_cgroup(
135 parent_h_cgroup, idx);
136 rsvd_parent = hugetlb_cgroup_counter_from_cgroup_rsvd(
137 parent_h_cgroup, idx);
138 }
139 page_counter_init(hugetlb_cgroup_counter_from_cgroup(h_cgroup,
140 idx),
141 fault_parent);
142 page_counter_init(
143 hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx),
144 rsvd_parent);
David Rientjes297880f2016-05-20 16:57:50 -0700145
146 limit = round_down(PAGE_COUNTER_MAX,
147 1 << huge_page_order(&hstates[idx]));
Mina Almasry1adc4d42020-04-01 21:11:15 -0700148
149 ret = page_counter_set_max(
150 hugetlb_cgroup_counter_from_cgroup(h_cgroup, idx),
151 limit);
152 VM_BUG_ON(ret);
153 ret = page_counter_set_max(
154 hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx),
155 limit);
David Rientjes297880f2016-05-20 16:57:50 -0700156 VM_BUG_ON(ret);
157 }
158}
159
Tejun Heoeb954192013-08-08 20:11:23 -0400160static struct cgroup_subsys_state *
161hugetlb_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700162{
Tejun Heoeb954192013-08-08 20:11:23 -0400163 struct hugetlb_cgroup *parent_h_cgroup = hugetlb_cgroup_from_css(parent_css);
164 struct hugetlb_cgroup *h_cgroup;
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700165
166 h_cgroup = kzalloc(sizeof(*h_cgroup), GFP_KERNEL);
167 if (!h_cgroup)
168 return ERR_PTR(-ENOMEM);
169
David Rientjes297880f2016-05-20 16:57:50 -0700170 if (!parent_h_cgroup)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700171 root_h_cgroup = h_cgroup;
David Rientjes297880f2016-05-20 16:57:50 -0700172
173 hugetlb_cgroup_init(h_cgroup, parent_h_cgroup);
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700174 return &h_cgroup->css;
175}
176
Tejun Heoeb954192013-08-08 20:11:23 -0400177static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700178{
179 struct hugetlb_cgroup *h_cgroup;
180
Tejun Heoeb954192013-08-08 20:11:23 -0400181 h_cgroup = hugetlb_cgroup_from_css(css);
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700182 kfree(h_cgroup);
183}
184
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700185/*
186 * Should be called with hugetlb_lock held.
187 * Since we are holding hugetlb_lock, pages cannot get moved from
188 * active list or uncharged from the cgroup, So no need to get
189 * page reference and test for page active here. This function
190 * cannot fail.
191 */
Tejun Heo3f798512013-08-08 20:11:22 -0400192static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700193 struct page *page)
194{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800195 unsigned int nr_pages;
196 struct page_counter *counter;
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700197 struct hugetlb_cgroup *page_hcg;
Tejun Heo3f798512013-08-08 20:11:22 -0400198 struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(h_cg);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700199
200 page_hcg = hugetlb_cgroup_from_page(page);
201 /*
202 * We can have pages in active list without any cgroup
203 * ie, hugepage with less than 3 pages. We can safely
204 * ignore those pages.
205 */
206 if (!page_hcg || page_hcg != h_cg)
207 goto out;
208
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700209 nr_pages = compound_nr(page);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700210 if (!parent) {
211 parent = root_h_cgroup;
212 /* root has no limit */
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800213 page_counter_charge(&parent->hugepage[idx], nr_pages);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700214 }
215 counter = &h_cg->hugepage[idx];
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800216 /* Take the pages off the local counter */
217 page_counter_cancel(counter, nr_pages);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700218
219 set_hugetlb_cgroup(page, parent);
220out:
221 return;
222}
223
224/*
225 * Force the hugetlb cgroup to empty the hugetlb resources by moving them to
226 * the parent cgroup.
227 */
Tejun Heoeb954192013-08-08 20:11:23 -0400228static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css)
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700229{
Tejun Heoeb954192013-08-08 20:11:23 -0400230 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700231 struct hstate *h;
232 struct page *page;
Michal Hocko9d093cb2012-10-26 13:37:33 +0200233 int idx = 0;
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700234
235 do {
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700236 for_each_hstate(h) {
237 spin_lock(&hugetlb_lock);
238 list_for_each_entry(page, &h->hugepage_activelist, lru)
Tejun Heo3f798512013-08-08 20:11:22 -0400239 hugetlb_cgroup_move_parent(idx, h_cg, page);
Aneesh Kumar K.Vda1def52012-07-31 16:42:21 -0700240
241 spin_unlock(&hugetlb_lock);
242 idx++;
243 }
244 cond_resched();
Tejun Heo3f798512013-08-08 20:11:22 -0400245 } while (hugetlb_cgroup_have_usage(h_cg));
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700246}
247
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100248static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx,
249 enum hugetlb_memory_event event)
250{
251 atomic_long_inc(&hugetlb->events_local[idx][event]);
252 cgroup_file_notify(&hugetlb->events_local_file[idx]);
253
254 do {
255 atomic_long_inc(&hugetlb->events[idx][event]);
256 cgroup_file_notify(&hugetlb->events_file[idx]);
257 } while ((hugetlb = parent_hugetlb_cgroup(hugetlb)) &&
258 !hugetlb_cgroup_is_root(hugetlb));
259}
260
Mina Almasry1adc4d42020-04-01 21:11:15 -0700261static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
262 struct hugetlb_cgroup **ptr,
263 bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700264{
265 int ret = 0;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800266 struct page_counter *counter;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700267 struct hugetlb_cgroup *h_cg = NULL;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700268
269 if (hugetlb_cgroup_disabled())
270 goto done;
271 /*
272 * We don't charge any cgroup if the compound page have less
273 * than 3 pages.
274 */
275 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
276 goto done;
277again:
278 rcu_read_lock();
279 h_cg = hugetlb_cgroup_from_task(current);
Roman Gushchin0362f322019-11-15 17:34:46 -0800280 if (!css_tryget(&h_cg->css)) {
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700281 rcu_read_unlock();
282 goto again;
283 }
284 rcu_read_unlock();
285
Mina Almasry1adc4d42020-04-01 21:11:15 -0700286 if (!page_counter_try_charge(
287 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
288 nr_pages, &counter)) {
Johannes Weiner6071ca52015-11-05 18:50:26 -0800289 ret = -ENOMEM;
Mina Almasry726b7bb2020-03-28 19:17:22 -0700290 hugetlb_event(h_cg, idx, HUGETLB_MAX);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700291 css_put(&h_cg->css);
292 goto done;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100293 }
Mina Almasry1adc4d42020-04-01 21:11:15 -0700294 /* Reservations take a reference to the css because they do not get
295 * reparented.
296 */
297 if (!rsvd)
298 css_put(&h_cg->css);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700299done:
300 *ptr = h_cg;
301 return ret;
302}
303
Mina Almasry1adc4d42020-04-01 21:11:15 -0700304int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
305 struct hugetlb_cgroup **ptr)
306{
307 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, false);
308}
309
310int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
311 struct hugetlb_cgroup **ptr)
312{
313 return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, true);
314}
315
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -0700316/* Should be called with hugetlb_lock held */
Mina Almasry1adc4d42020-04-01 21:11:15 -0700317static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
318 struct hugetlb_cgroup *h_cg,
319 struct page *page, bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700320{
321 if (hugetlb_cgroup_disabled() || !h_cg)
322 return;
323
Mina Almasry1adc4d42020-04-01 21:11:15 -0700324 __set_hugetlb_cgroup(page, h_cg, rsvd);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700325 return;
326}
327
Mina Almasry1adc4d42020-04-01 21:11:15 -0700328void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
329 struct hugetlb_cgroup *h_cg,
330 struct page *page)
331{
332 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, false);
333}
334
335void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages,
336 struct hugetlb_cgroup *h_cg,
337 struct page *page)
338{
339 __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, true);
340}
341
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700342/*
343 * Should be called with hugetlb_lock held
344 */
Mina Almasry1adc4d42020-04-01 21:11:15 -0700345static void __hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
346 struct page *page, bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700347{
348 struct hugetlb_cgroup *h_cg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700349
350 if (hugetlb_cgroup_disabled())
351 return;
Michal Hocko7ea85742014-08-29 15:18:42 -0700352 lockdep_assert_held(&hugetlb_lock);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700353 h_cg = __hugetlb_cgroup_from_page(page, rsvd);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700354 if (unlikely(!h_cg))
355 return;
Mina Almasry1adc4d42020-04-01 21:11:15 -0700356 __set_hugetlb_cgroup(page, NULL, rsvd);
357
358 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
359 rsvd),
360 nr_pages);
361
362 if (rsvd)
363 css_put(&h_cg->css);
364
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700365 return;
366}
367
Mina Almasry1adc4d42020-04-01 21:11:15 -0700368void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
369 struct page *page)
370{
371 __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, false);
372}
373
374void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages,
375 struct page *page)
376{
377 __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, true);
378}
379
380static void __hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
381 struct hugetlb_cgroup *h_cg,
382 bool rsvd)
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700383{
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700384 if (hugetlb_cgroup_disabled() || !h_cg)
385 return;
386
387 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
388 return;
389
Mina Almasry1adc4d42020-04-01 21:11:15 -0700390 page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx,
391 rsvd),
392 nr_pages);
393
394 if (rsvd)
395 css_put(&h_cg->css);
396}
397
398void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
399 struct hugetlb_cgroup *h_cg)
400{
401 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, false);
402}
403
404void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages,
405 struct hugetlb_cgroup *h_cg)
406{
407 __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, true);
408}
409
410void hugetlb_cgroup_uncharge_counter(struct page_counter *p,
411 unsigned long nr_pages,
412 struct cgroup_subsys_state *css)
413{
414 if (hugetlb_cgroup_disabled() || !p || !css)
415 return;
416
417 page_counter_uncharge(p, nr_pages);
418 css_put(css);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700419}
420
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800421enum {
422 RES_USAGE,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700423 RES_RSVD_USAGE,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800424 RES_LIMIT,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700425 RES_RSVD_LIMIT,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800426 RES_MAX_USAGE,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700427 RES_RSVD_MAX_USAGE,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800428 RES_FAILCNT,
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700429 RES_RSVD_FAILCNT,
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800430};
431
Tejun Heo716f4792013-12-05 12:28:03 -0500432static u64 hugetlb_cgroup_read_u64(struct cgroup_subsys_state *css,
433 struct cftype *cft)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700434{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800435 struct page_counter *counter;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700436 struct page_counter *rsvd_counter;
Tejun Heo182446d2013-08-08 20:11:24 -0400437 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css);
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700438
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800439 counter = &h_cg->hugepage[MEMFILE_IDX(cft->private)];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700440 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(cft->private)];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700441
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800442 switch (MEMFILE_ATTR(cft->private)) {
443 case RES_USAGE:
444 return (u64)page_counter_read(counter) * PAGE_SIZE;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700445 case RES_RSVD_USAGE:
446 return (u64)page_counter_read(rsvd_counter) * PAGE_SIZE;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800447 case RES_LIMIT:
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700448 return (u64)counter->max * PAGE_SIZE;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700449 case RES_RSVD_LIMIT:
450 return (u64)rsvd_counter->max * PAGE_SIZE;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800451 case RES_MAX_USAGE:
452 return (u64)counter->watermark * PAGE_SIZE;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700453 case RES_RSVD_MAX_USAGE:
454 return (u64)rsvd_counter->watermark * PAGE_SIZE;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800455 case RES_FAILCNT:
456 return counter->failcnt;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700457 case RES_RSVD_FAILCNT:
458 return rsvd_counter->failcnt;
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800459 default:
460 BUG();
461 }
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700462}
463
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100464static int hugetlb_cgroup_read_u64_max(struct seq_file *seq, void *v)
465{
466 int idx;
467 u64 val;
468 struct cftype *cft = seq_cft(seq);
469 unsigned long limit;
470 struct page_counter *counter;
471 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
472
473 idx = MEMFILE_IDX(cft->private);
474 counter = &h_cg->hugepage[idx];
475
476 limit = round_down(PAGE_COUNTER_MAX,
477 1 << huge_page_order(&hstates[idx]));
478
479 switch (MEMFILE_ATTR(cft->private)) {
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700480 case RES_RSVD_USAGE:
481 counter = &h_cg->rsvd_hugepage[idx];
482 /* Fall through. */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100483 case RES_USAGE:
484 val = (u64)page_counter_read(counter);
485 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
486 break;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700487 case RES_RSVD_LIMIT:
488 counter = &h_cg->rsvd_hugepage[idx];
489 /* Fall through. */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100490 case RES_LIMIT:
491 val = (u64)counter->max;
492 if (val == limit)
493 seq_puts(seq, "max\n");
494 else
495 seq_printf(seq, "%llu\n", val * PAGE_SIZE);
496 break;
497 default:
498 BUG();
499 }
500
501 return 0;
502}
503
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800504static DEFINE_MUTEX(hugetlb_limit_mutex);
505
Tejun Heo451af502014-05-13 12:16:21 -0400506static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of,
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100507 char *buf, size_t nbytes, loff_t off,
508 const char *max)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700509{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800510 int ret, idx;
511 unsigned long nr_pages;
Tejun Heo451af502014-05-13 12:16:21 -0400512 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700513 bool rsvd = false;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700514
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800515 if (hugetlb_cgroup_is_root(h_cg)) /* Can't set limit on root */
516 return -EINVAL;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700517
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800518 buf = strstrip(buf);
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100519 ret = page_counter_memparse(buf, max, &nr_pages);
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800520 if (ret)
521 return ret;
522
523 idx = MEMFILE_IDX(of_cft(of)->private);
David Rientjes297880f2016-05-20 16:57:50 -0700524 nr_pages = round_down(nr_pages, 1 << huge_page_order(&hstates[idx]));
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800525
526 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700527 case RES_RSVD_LIMIT:
528 rsvd = true;
529 /* Fall through. */
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700530 case RES_LIMIT:
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800531 mutex_lock(&hugetlb_limit_mutex);
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700532 ret = page_counter_set_max(
Mina Almasry1adc4d42020-04-01 21:11:15 -0700533 __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd),
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700534 nr_pages);
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800535 mutex_unlock(&hugetlb_limit_mutex);
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700536 break;
537 default:
538 ret = -EINVAL;
539 break;
540 }
Tejun Heo451af502014-05-13 12:16:21 -0400541 return ret ?: nbytes;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700542}
543
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100544static ssize_t hugetlb_cgroup_write_legacy(struct kernfs_open_file *of,
545 char *buf, size_t nbytes, loff_t off)
546{
547 return hugetlb_cgroup_write(of, buf, nbytes, off, "-1");
548}
549
550static ssize_t hugetlb_cgroup_write_dfl(struct kernfs_open_file *of,
551 char *buf, size_t nbytes, loff_t off)
552{
553 return hugetlb_cgroup_write(of, buf, nbytes, off, "max");
554}
555
Tejun Heo6770c642014-05-13 12:16:21 -0400556static ssize_t hugetlb_cgroup_reset(struct kernfs_open_file *of,
557 char *buf, size_t nbytes, loff_t off)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700558{
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800559 int ret = 0;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700560 struct page_counter *counter, *rsvd_counter;
Tejun Heo6770c642014-05-13 12:16:21 -0400561 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of));
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700562
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800563 counter = &h_cg->hugepage[MEMFILE_IDX(of_cft(of)->private)];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700564 rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(of_cft(of)->private)];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700565
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800566 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700567 case RES_MAX_USAGE:
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800568 page_counter_reset_watermark(counter);
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700569 break;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700570 case RES_RSVD_MAX_USAGE:
571 page_counter_reset_watermark(rsvd_counter);
572 break;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700573 case RES_FAILCNT:
Johannes Weiner71f87bee2014-12-10 15:42:34 -0800574 counter->failcnt = 0;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700575 break;
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700576 case RES_RSVD_FAILCNT:
577 rsvd_counter->failcnt = 0;
578 break;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700579 default:
580 ret = -EINVAL;
581 break;
582 }
Tejun Heo6770c642014-05-13 12:16:21 -0400583 return ret ?: nbytes;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700584}
585
586static char *mem_fmt(char *buf, int size, unsigned long hsize)
587{
588 if (hsize >= (1UL << 30))
589 snprintf(buf, size, "%luGB", hsize >> 30);
590 else if (hsize >= (1UL << 20))
591 snprintf(buf, size, "%luMB", hsize >> 20);
592 else
593 snprintf(buf, size, "%luKB", hsize >> 10);
594 return buf;
595}
596
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100597static int __hugetlb_events_show(struct seq_file *seq, bool local)
598{
599 int idx;
600 long max;
601 struct cftype *cft = seq_cft(seq);
602 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
603
604 idx = MEMFILE_IDX(cft->private);
605
606 if (local)
607 max = atomic_long_read(&h_cg->events_local[idx][HUGETLB_MAX]);
608 else
609 max = atomic_long_read(&h_cg->events[idx][HUGETLB_MAX]);
610
611 seq_printf(seq, "max %lu\n", max);
612
613 return 0;
614}
615
616static int hugetlb_events_show(struct seq_file *seq, void *v)
617{
618 return __hugetlb_events_show(seq, false);
619}
620
621static int hugetlb_events_local_show(struct seq_file *seq, void *v)
622{
623 return __hugetlb_events_show(seq, true);
624}
625
626static void __init __hugetlb_cgroup_file_dfl_init(int idx)
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700627{
628 char buf[32];
629 struct cftype *cft;
630 struct hstate *h = &hstates[idx];
631
632 /* format the size */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700633 mem_fmt(buf, sizeof(buf), huge_page_size(h));
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700634
635 /* Add the limit file */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100636 cft = &h->cgroup_files_dfl[0];
637 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max", buf);
638 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
639 cft->seq_show = hugetlb_cgroup_read_u64_max;
640 cft->write = hugetlb_cgroup_write_dfl;
641 cft->flags = CFTYPE_NOT_ON_ROOT;
642
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700643 /* Add the reservation limit file */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100644 cft = &h->cgroup_files_dfl[1];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700645 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max", buf);
646 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
647 cft->seq_show = hugetlb_cgroup_read_u64_max;
648 cft->write = hugetlb_cgroup_write_dfl;
649 cft->flags = CFTYPE_NOT_ON_ROOT;
650
651 /* Add the current usage file */
652 cft = &h->cgroup_files_dfl[2];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100653 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.current", buf);
654 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
655 cft->seq_show = hugetlb_cgroup_read_u64_max;
656 cft->flags = CFTYPE_NOT_ON_ROOT;
657
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700658 /* Add the current reservation usage file */
659 cft = &h->cgroup_files_dfl[3];
660 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.current", buf);
661 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
662 cft->seq_show = hugetlb_cgroup_read_u64_max;
663 cft->flags = CFTYPE_NOT_ON_ROOT;
664
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100665 /* Add the events file */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700666 cft = &h->cgroup_files_dfl[4];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100667 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events", buf);
668 cft->private = MEMFILE_PRIVATE(idx, 0);
669 cft->seq_show = hugetlb_events_show;
670 cft->file_offset = offsetof(struct hugetlb_cgroup, events_file[idx]),
671 cft->flags = CFTYPE_NOT_ON_ROOT;
672
673 /* Add the events.local file */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700674 cft = &h->cgroup_files_dfl[5];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100675 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events.local", buf);
676 cft->private = MEMFILE_PRIVATE(idx, 0);
677 cft->seq_show = hugetlb_events_local_show;
678 cft->file_offset = offsetof(struct hugetlb_cgroup,
679 events_local_file[idx]),
680 cft->flags = CFTYPE_NOT_ON_ROOT;
681
682 /* NULL terminate the last cft */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700683 cft = &h->cgroup_files_dfl[6];
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100684 memset(cft, 0, sizeof(*cft));
685
686 WARN_ON(cgroup_add_dfl_cftypes(&hugetlb_cgrp_subsys,
687 h->cgroup_files_dfl));
688}
689
690static void __init __hugetlb_cgroup_file_legacy_init(int idx)
691{
692 char buf[32];
693 struct cftype *cft;
694 struct hstate *h = &hstates[idx];
695
696 /* format the size */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700697 mem_fmt(buf, sizeof(buf), huge_page_size(h));
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100698
699 /* Add the limit file */
700 cft = &h->cgroup_files_legacy[0];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700701 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
702 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
Tejun Heo716f4792013-12-05 12:28:03 -0500703 cft->read_u64 = hugetlb_cgroup_read_u64;
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100704 cft->write = hugetlb_cgroup_write_legacy;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700705
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700706 /* Add the reservation limit file */
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100707 cft = &h->cgroup_files_legacy[1];
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700708 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.limit_in_bytes", buf);
709 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT);
710 cft->read_u64 = hugetlb_cgroup_read_u64;
711 cft->write = hugetlb_cgroup_write_legacy;
712
713 /* Add the usage file */
714 cft = &h->cgroup_files_legacy[2];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700715 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
716 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
Tejun Heo716f4792013-12-05 12:28:03 -0500717 cft->read_u64 = hugetlb_cgroup_read_u64;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700718
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700719 /* Add the reservation usage file */
720 cft = &h->cgroup_files_legacy[3];
721 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.usage_in_bytes", buf);
722 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE);
723 cft->read_u64 = hugetlb_cgroup_read_u64;
724
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700725 /* Add the MAX usage file */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700726 cft = &h->cgroup_files_legacy[4];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700727 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
728 cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
Tejun Heo6770c642014-05-13 12:16:21 -0400729 cft->write = hugetlb_cgroup_reset;
Tejun Heo716f4792013-12-05 12:28:03 -0500730 cft->read_u64 = hugetlb_cgroup_read_u64;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700731
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700732 /* Add the MAX reservation usage file */
733 cft = &h->cgroup_files_legacy[5];
734 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max_usage_in_bytes", buf);
735 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_MAX_USAGE);
736 cft->write = hugetlb_cgroup_reset;
737 cft->read_u64 = hugetlb_cgroup_read_u64;
738
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700739 /* Add the failcntfile */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700740 cft = &h->cgroup_files_legacy[6];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700741 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700742 cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
743 cft->write = hugetlb_cgroup_reset;
744 cft->read_u64 = hugetlb_cgroup_read_u64;
745
746 /* Add the reservation failcntfile */
747 cft = &h->cgroup_files_legacy[7];
748 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.failcnt", buf);
749 cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_FAILCNT);
Tejun Heo6770c642014-05-13 12:16:21 -0400750 cft->write = hugetlb_cgroup_reset;
Tejun Heo716f4792013-12-05 12:28:03 -0500751 cft->read_u64 = hugetlb_cgroup_read_u64;
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700752
753 /* NULL terminate the last cft */
Mina Almasrycdc2fcf2020-04-01 21:11:11 -0700754 cft = &h->cgroup_files_legacy[8];
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700755 memset(cft, 0, sizeof(*cft));
756
Tejun Heo2cf669a2014-07-15 11:05:09 -0400757 WARN_ON(cgroup_add_legacy_cftypes(&hugetlb_cgrp_subsys,
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100758 h->cgroup_files_legacy));
759}
760
761static void __init __hugetlb_cgroup_file_init(int idx)
762{
763 __hugetlb_cgroup_file_dfl_init(idx);
764 __hugetlb_cgroup_file_legacy_init(idx);
Jianguo Wu7179e7b2012-12-18 14:23:19 -0800765}
766
767void __init hugetlb_cgroup_file_init(void)
768{
769 struct hstate *h;
770
771 for_each_hstate(h) {
772 /*
773 * Add cgroup control files only if the huge page consists
774 * of more than two normal pages. This is because we use
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -0800775 * page[2].private for storing cgroup details.
Jianguo Wu7179e7b2012-12-18 14:23:19 -0800776 */
777 if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER)
778 __hugetlb_cgroup_file_init(hstate_index(h));
779 }
Aneesh Kumar K.Vabb82062012-07-31 16:42:24 -0700780}
781
Aneesh Kumar K.V75754682012-07-31 16:42:36 -0700782/*
783 * hugetlb_lock will make sure a parallel cgroup rmdir won't happen
784 * when we migrate hugepages
785 */
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700786void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)
787{
788 struct hugetlb_cgroup *h_cg;
Mina Almasry1adc4d42020-04-01 21:11:15 -0700789 struct hugetlb_cgroup *h_cg_rsvd;
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -0700790 struct hstate *h = page_hstate(oldhpage);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700791
792 if (hugetlb_cgroup_disabled())
793 return;
794
Sasha Levin309381fea2014-01-23 15:52:54 -0800795 VM_BUG_ON_PAGE(!PageHuge(oldhpage), oldhpage);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700796 spin_lock(&hugetlb_lock);
797 h_cg = hugetlb_cgroup_from_page(oldhpage);
Mina Almasry1adc4d42020-04-01 21:11:15 -0700798 h_cg_rsvd = hugetlb_cgroup_from_page_rsvd(oldhpage);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700799 set_hugetlb_cgroup(oldhpage, NULL);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700800
801 /* move the h_cg details to new cgroup */
Mina Almasry1adc4d42020-04-01 21:11:15 -0700802 set_hugetlb_cgroup_rsvd(newhpage, h_cg_rsvd);
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -0700803 list_move(&newhpage->lru, &h->hugepage_activelist);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700804 spin_unlock(&hugetlb_lock);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -0700805 return;
806}
807
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100808static struct cftype hugetlb_files[] = {
809 {} /* terminate */
810};
811
Tejun Heo073219e2014-02-08 10:36:58 -0500812struct cgroup_subsys hugetlb_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800813 .css_alloc = hugetlb_cgroup_css_alloc,
814 .css_offline = hugetlb_cgroup_css_offline,
815 .css_free = hugetlb_cgroup_css_free,
Giuseppe Scrivanofaced7e2019-12-16 20:38:31 +0100816 .dfl_cftypes = hugetlb_files,
817 .legacy_cftypes = hugetlb_files,
Aneesh Kumar K.V2bc64a22012-07-31 16:42:12 -0700818};