netprio_cgroup: pass around @css instead of @cgroup and kill struct cgroup_netprio_state
[deliverable/linux.git] / mm / hugetlb_cgroup.c
CommitLineData
2bc64a20
AK
1/*
2 *
3 * Copyright IBM Corporation, 2012
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 */
15
16#include <linux/cgroup.h>
17#include <linux/slab.h>
18#include <linux/hugetlb.h>
19#include <linux/hugetlb_cgroup.h>
20
21struct hugetlb_cgroup {
22 struct cgroup_subsys_state css;
23 /*
24 * the counter to account for hugepages from hugetlb.
25 */
26 struct res_counter hugepage[HUGE_MAX_HSTATE];
27};
28
abb8206c
AK
29#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
30#define MEMFILE_IDX(val) (((val) >> 16) & 0xffff)
31#define MEMFILE_ATTR(val) ((val) & 0xffff)
32
2bc64a20
AK
33struct cgroup_subsys hugetlb_subsys __read_mostly;
34static struct hugetlb_cgroup *root_h_cgroup __read_mostly;
35
36static inline
37struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s)
38{
39 return container_of(s, struct hugetlb_cgroup, css);
40}
41
42static inline
43struct hugetlb_cgroup *hugetlb_cgroup_from_cgroup(struct cgroup *cgroup)
44{
8af01f56 45 return hugetlb_cgroup_from_css(cgroup_css(cgroup, hugetlb_subsys_id));
2bc64a20
AK
46}
47
48static inline
49struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task)
50{
8af01f56 51 return hugetlb_cgroup_from_css(task_css(task, hugetlb_subsys_id));
2bc64a20
AK
52}
53
54static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg)
55{
56 return (h_cg == root_h_cgroup);
57}
58
59static inline struct hugetlb_cgroup *parent_hugetlb_cgroup(struct cgroup *cg)
60{
61 if (!cg->parent)
62 return NULL;
63 return hugetlb_cgroup_from_cgroup(cg->parent);
64}
65
66static inline bool hugetlb_cgroup_have_usage(struct cgroup *cg)
67{
68 int idx;
69 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cg);
70
71 for (idx = 0; idx < hugetlb_max_hstate; idx++) {
72 if ((res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE)) > 0)
73 return true;
74 }
75 return false;
76}
77
92fb9748 78static struct cgroup_subsys_state *hugetlb_cgroup_css_alloc(struct cgroup *cgroup)
2bc64a20
AK
79{
80 int idx;
81 struct cgroup *parent_cgroup;
82 struct hugetlb_cgroup *h_cgroup, *parent_h_cgroup;
83
84 h_cgroup = kzalloc(sizeof(*h_cgroup), GFP_KERNEL);
85 if (!h_cgroup)
86 return ERR_PTR(-ENOMEM);
87
88 parent_cgroup = cgroup->parent;
89 if (parent_cgroup) {
90 parent_h_cgroup = hugetlb_cgroup_from_cgroup(parent_cgroup);
91 for (idx = 0; idx < HUGE_MAX_HSTATE; idx++)
92 res_counter_init(&h_cgroup->hugepage[idx],
93 &parent_h_cgroup->hugepage[idx]);
94 } else {
95 root_h_cgroup = h_cgroup;
96 for (idx = 0; idx < HUGE_MAX_HSTATE; idx++)
97 res_counter_init(&h_cgroup->hugepage[idx], NULL);
98 }
99 return &h_cgroup->css;
100}
101
92fb9748 102static void hugetlb_cgroup_css_free(struct cgroup *cgroup)
2bc64a20
AK
103{
104 struct hugetlb_cgroup *h_cgroup;
105
106 h_cgroup = hugetlb_cgroup_from_cgroup(cgroup);
107 kfree(h_cgroup);
108}
109
da1def55
AK
110
111/*
112 * Should be called with hugetlb_lock held.
113 * Since we are holding hugetlb_lock, pages cannot get moved from
114 * active list or uncharged from the cgroup, So no need to get
115 * page reference and test for page active here. This function
116 * cannot fail.
117 */
118static void hugetlb_cgroup_move_parent(int idx, struct cgroup *cgroup,
119 struct page *page)
120{
121 int csize;
122 struct res_counter *counter;
123 struct res_counter *fail_res;
124 struct hugetlb_cgroup *page_hcg;
125 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
126 struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(cgroup);
127
128 page_hcg = hugetlb_cgroup_from_page(page);
129 /*
130 * We can have pages in active list without any cgroup
131 * ie, hugepage with less than 3 pages. We can safely
132 * ignore those pages.
133 */
134 if (!page_hcg || page_hcg != h_cg)
135 goto out;
136
137 csize = PAGE_SIZE << compound_order(page);
138 if (!parent) {
139 parent = root_h_cgroup;
140 /* root has no limit */
141 res_counter_charge_nofail(&parent->hugepage[idx],
142 csize, &fail_res);
143 }
144 counter = &h_cg->hugepage[idx];
145 res_counter_uncharge_until(counter, counter->parent, csize);
146
147 set_hugetlb_cgroup(page, parent);
148out:
149 return;
150}
151
152/*
153 * Force the hugetlb cgroup to empty the hugetlb resources by moving them to
154 * the parent cgroup.
155 */
92fb9748 156static void hugetlb_cgroup_css_offline(struct cgroup *cgroup)
2bc64a20 157{
da1def55
AK
158 struct hstate *h;
159 struct page *page;
9d093cb1 160 int idx = 0;
da1def55
AK
161
162 do {
da1def55
AK
163 for_each_hstate(h) {
164 spin_lock(&hugetlb_lock);
165 list_for_each_entry(page, &h->hugepage_activelist, lru)
166 hugetlb_cgroup_move_parent(idx, cgroup, page);
167
168 spin_unlock(&hugetlb_lock);
169 idx++;
170 }
171 cond_resched();
172 } while (hugetlb_cgroup_have_usage(cgroup));
2bc64a20
AK
173}
174
6d76dcf4
AK
175int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
176 struct hugetlb_cgroup **ptr)
177{
178 int ret = 0;
179 struct res_counter *fail_res;
180 struct hugetlb_cgroup *h_cg = NULL;
181 unsigned long csize = nr_pages * PAGE_SIZE;
182
183 if (hugetlb_cgroup_disabled())
184 goto done;
185 /*
186 * We don't charge any cgroup if the compound page have less
187 * than 3 pages.
188 */
189 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
190 goto done;
191again:
192 rcu_read_lock();
193 h_cg = hugetlb_cgroup_from_task(current);
194 if (!css_tryget(&h_cg->css)) {
195 rcu_read_unlock();
196 goto again;
197 }
198 rcu_read_unlock();
199
200 ret = res_counter_charge(&h_cg->hugepage[idx], csize, &fail_res);
201 css_put(&h_cg->css);
202done:
203 *ptr = h_cg;
204 return ret;
205}
206
94ae8ba7 207/* Should be called with hugetlb_lock held */
6d76dcf4
AK
208void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
209 struct hugetlb_cgroup *h_cg,
210 struct page *page)
211{
212 if (hugetlb_cgroup_disabled() || !h_cg)
213 return;
214
6d76dcf4 215 set_hugetlb_cgroup(page, h_cg);
6d76dcf4
AK
216 return;
217}
218
219/*
220 * Should be called with hugetlb_lock held
221 */
222void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,
223 struct page *page)
224{
225 struct hugetlb_cgroup *h_cg;
226 unsigned long csize = nr_pages * PAGE_SIZE;
227
228 if (hugetlb_cgroup_disabled())
229 return;
230 VM_BUG_ON(!spin_is_locked(&hugetlb_lock));
231 h_cg = hugetlb_cgroup_from_page(page);
232 if (unlikely(!h_cg))
233 return;
234 set_hugetlb_cgroup(page, NULL);
235 res_counter_uncharge(&h_cg->hugepage[idx], csize);
236 return;
237}
238
239void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,
240 struct hugetlb_cgroup *h_cg)
241{
242 unsigned long csize = nr_pages * PAGE_SIZE;
243
244 if (hugetlb_cgroup_disabled() || !h_cg)
245 return;
246
247 if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
248 return;
249
250 res_counter_uncharge(&h_cg->hugepage[idx], csize);
251 return;
252}
253
abb8206c
AK
254static ssize_t hugetlb_cgroup_read(struct cgroup *cgroup, struct cftype *cft,
255 struct file *file, char __user *buf,
256 size_t nbytes, loff_t *ppos)
257{
258 u64 val;
259 char str[64];
260 int idx, name, len;
261 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
262
263 idx = MEMFILE_IDX(cft->private);
264 name = MEMFILE_ATTR(cft->private);
265
266 val = res_counter_read_u64(&h_cg->hugepage[idx], name);
267 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
268 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
269}
270
271static int hugetlb_cgroup_write(struct cgroup *cgroup, struct cftype *cft,
272 const char *buffer)
273{
274 int idx, name, ret;
275 unsigned long long val;
276 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
277
278 idx = MEMFILE_IDX(cft->private);
279 name = MEMFILE_ATTR(cft->private);
280
281 switch (name) {
282 case RES_LIMIT:
283 if (hugetlb_cgroup_is_root(h_cg)) {
284 /* Can't set limit on root */
285 ret = -EINVAL;
286 break;
287 }
288 /* This function does all necessary parse...reuse it */
289 ret = res_counter_memparse_write_strategy(buffer, &val);
290 if (ret)
291 break;
292 ret = res_counter_set_limit(&h_cg->hugepage[idx], val);
293 break;
294 default:
295 ret = -EINVAL;
296 break;
297 }
298 return ret;
299}
300
301static int hugetlb_cgroup_reset(struct cgroup *cgroup, unsigned int event)
302{
303 int idx, name, ret = 0;
304 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
305
306 idx = MEMFILE_IDX(event);
307 name = MEMFILE_ATTR(event);
308
309 switch (name) {
310 case RES_MAX_USAGE:
311 res_counter_reset_max(&h_cg->hugepage[idx]);
312 break;
313 case RES_FAILCNT:
314 res_counter_reset_failcnt(&h_cg->hugepage[idx]);
315 break;
316 default:
317 ret = -EINVAL;
318 break;
319 }
320 return ret;
321}
322
323static char *mem_fmt(char *buf, int size, unsigned long hsize)
324{
325 if (hsize >= (1UL << 30))
326 snprintf(buf, size, "%luGB", hsize >> 30);
327 else if (hsize >= (1UL << 20))
328 snprintf(buf, size, "%luMB", hsize >> 20);
329 else
330 snprintf(buf, size, "%luKB", hsize >> 10);
331 return buf;
332}
333
7179e7bf 334static void __init __hugetlb_cgroup_file_init(int idx)
abb8206c
AK
335{
336 char buf[32];
337 struct cftype *cft;
338 struct hstate *h = &hstates[idx];
339
340 /* format the size */
341 mem_fmt(buf, 32, huge_page_size(h));
342
343 /* Add the limit file */
344 cft = &h->cgroup_files[0];
345 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf);
346 cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT);
347 cft->read = hugetlb_cgroup_read;
348 cft->write_string = hugetlb_cgroup_write;
349
350 /* Add the usage file */
351 cft = &h->cgroup_files[1];
352 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf);
353 cft->private = MEMFILE_PRIVATE(idx, RES_USAGE);
354 cft->read = hugetlb_cgroup_read;
355
356 /* Add the MAX usage file */
357 cft = &h->cgroup_files[2];
358 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf);
359 cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE);
360 cft->trigger = hugetlb_cgroup_reset;
361 cft->read = hugetlb_cgroup_read;
362
363 /* Add the failcntfile */
364 cft = &h->cgroup_files[3];
365 snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf);
366 cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT);
367 cft->trigger = hugetlb_cgroup_reset;
368 cft->read = hugetlb_cgroup_read;
369
370 /* NULL terminate the last cft */
371 cft = &h->cgroup_files[4];
372 memset(cft, 0, sizeof(*cft));
373
374 WARN_ON(cgroup_add_cftypes(&hugetlb_subsys, h->cgroup_files));
375
7179e7bf
JW
376 return;
377}
378
379void __init hugetlb_cgroup_file_init(void)
380{
381 struct hstate *h;
382
383 for_each_hstate(h) {
384 /*
385 * Add cgroup control files only if the huge page consists
386 * of more than two normal pages. This is because we use
387 * page[2].lru.next for storing cgroup details.
388 */
389 if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER)
390 __hugetlb_cgroup_file_init(hstate_index(h));
391 }
abb8206c
AK
392}
393
75754681
AK
394/*
395 * hugetlb_lock will make sure a parallel cgroup rmdir won't happen
396 * when we migrate hugepages
397 */
8e6ac7fa
AK
398void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)
399{
400 struct hugetlb_cgroup *h_cg;
94ae8ba7 401 struct hstate *h = page_hstate(oldhpage);
8e6ac7fa
AK
402
403 if (hugetlb_cgroup_disabled())
404 return;
405
406 VM_BUG_ON(!PageHuge(oldhpage));
407 spin_lock(&hugetlb_lock);
408 h_cg = hugetlb_cgroup_from_page(oldhpage);
409 set_hugetlb_cgroup(oldhpage, NULL);
8e6ac7fa
AK
410
411 /* move the h_cg details to new cgroup */
412 set_hugetlb_cgroup(newhpage, h_cg);
94ae8ba7 413 list_move(&newhpage->lru, &h->hugepage_activelist);
8e6ac7fa 414 spin_unlock(&hugetlb_lock);
8e6ac7fa
AK
415 return;
416}
417
2bc64a20
AK
418struct cgroup_subsys hugetlb_subsys = {
419 .name = "hugetlb",
92fb9748
TH
420 .css_alloc = hugetlb_cgroup_css_alloc,
421 .css_offline = hugetlb_cgroup_css_offline,
422 .css_free = hugetlb_cgroup_css_free,
423 .subsys_id = hugetlb_subsys_id,
2bc64a20 424};
This page took 0.082588 seconds and 5 git commands to generate.