diff options
-rw-r--r-- | include/linux/hugetlb.h | 5 | ||||
-rw-r--r-- | include/linux/hugetlb_cgroup.h | 6 | ||||
-rw-r--r-- | mm/hugetlb.c | 8 | ||||
-rw-r--r-- | mm/hugetlb_cgroup.c | 129 |
4 files changed, 148 insertions, 0 deletions
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 3d677dd41898..f9db20bfa9fc 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <linux/mm_types.h> | 4 | #include <linux/mm_types.h> |
5 | #include <linux/fs.h> | 5 | #include <linux/fs.h> |
6 | #include <linux/hugetlb_inline.h> | 6 | #include <linux/hugetlb_inline.h> |
7 | #include <linux/cgroup.h> | ||
7 | 8 | ||
8 | struct ctl_table; | 9 | struct ctl_table; |
9 | struct user_struct; | 10 | struct user_struct; |
@@ -221,6 +222,10 @@ struct hstate { | |||
221 | unsigned int nr_huge_pages_node[MAX_NUMNODES]; | 222 | unsigned int nr_huge_pages_node[MAX_NUMNODES]; |
222 | unsigned int free_huge_pages_node[MAX_NUMNODES]; | 223 | unsigned int free_huge_pages_node[MAX_NUMNODES]; |
223 | unsigned int surplus_huge_pages_node[MAX_NUMNODES]; | 224 | unsigned int surplus_huge_pages_node[MAX_NUMNODES]; |
225 | #ifdef CONFIG_CGROUP_HUGETLB | ||
226 | /* cgroup control files */ | ||
227 | struct cftype cgroup_files[5]; | ||
228 | #endif | ||
224 | char name[HSTATE_NAME_LEN]; | 229 | char name[HSTATE_NAME_LEN]; |
225 | }; | 230 | }; |
226 | 231 | ||
diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h index 7d3fde996be3..73f1e600fc12 100644 --- a/include/linux/hugetlb_cgroup.h +++ b/include/linux/hugetlb_cgroup.h | |||
@@ -62,6 +62,7 @@ extern void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages, | |||
62 | struct page *page); | 62 | struct page *page); |
63 | extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, | 63 | extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, |
64 | struct hugetlb_cgroup *h_cg); | 64 | struct hugetlb_cgroup *h_cg); |
65 | extern int hugetlb_cgroup_file_init(int idx) __init; | ||
65 | 66 | ||
66 | #else | 67 | #else |
67 | static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page) | 68 | static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page) |
@@ -108,5 +109,10 @@ hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, | |||
108 | return; | 109 | return; |
109 | } | 110 | } |
110 | 111 | ||
112 | static inline int __init hugetlb_cgroup_file_init(int idx) | ||
113 | { | ||
114 | return 0; | ||
115 | } | ||
116 | |||
111 | #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */ | 117 | #endif /* CONFIG_MEM_RES_CTLR_HUGETLB */ |
112 | #endif | 118 | #endif |
diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 16a0f32c4820..c57740bb203a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c | |||
@@ -30,6 +30,7 @@ | |||
30 | #include <linux/hugetlb.h> | 30 | #include <linux/hugetlb.h> |
31 | #include <linux/hugetlb_cgroup.h> | 31 | #include <linux/hugetlb_cgroup.h> |
32 | #include <linux/node.h> | 32 | #include <linux/node.h> |
33 | #include <linux/hugetlb_cgroup.h> | ||
33 | #include "internal.h" | 34 | #include "internal.h" |
34 | 35 | ||
35 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; | 36 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; |
@@ -1930,6 +1931,13 @@ void __init hugetlb_add_hstate(unsigned order) | |||
1930 | h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]); | 1931 | h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]); |
1931 | snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", | 1932 | snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", |
1932 | huge_page_size(h)/1024); | 1933 | huge_page_size(h)/1024); |
1934 | /* | ||
1935 | * Add cgroup control files only if the huge page consists | ||
1936 | * of more than two normal pages. This is because we use | ||
1937 | * page[2].lru.next for storing cgoup details. | ||
1938 | */ | ||
1939 | if (order >= HUGETLB_CGROUP_MIN_ORDER) | ||
1940 | hugetlb_cgroup_file_init(hugetlb_max_hstate - 1); | ||
1933 | 1941 | ||
1934 | parsed_hstate = h; | 1942 | parsed_hstate = h; |
1935 | } | 1943 | } |
diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c index bc518bedea98..d1ca1196e62f 100644 --- a/mm/hugetlb_cgroup.c +++ b/mm/hugetlb_cgroup.c | |||
@@ -26,6 +26,10 @@ struct hugetlb_cgroup { | |||
26 | struct res_counter hugepage[HUGE_MAX_HSTATE]; | 26 | struct res_counter hugepage[HUGE_MAX_HSTATE]; |
27 | }; | 27 | }; |
28 | 28 | ||
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 | |||
29 | struct cgroup_subsys hugetlb_subsys __read_mostly; | 33 | struct cgroup_subsys hugetlb_subsys __read_mostly; |
30 | static struct hugetlb_cgroup *root_h_cgroup __read_mostly; | 34 | static struct hugetlb_cgroup *root_h_cgroup __read_mostly; |
31 | 35 | ||
@@ -257,6 +261,131 @@ void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, | |||
257 | return; | 261 | return; |
258 | } | 262 | } |
259 | 263 | ||
264 | static ssize_t hugetlb_cgroup_read(struct cgroup *cgroup, struct cftype *cft, | ||
265 | struct file *file, char __user *buf, | ||
266 | size_t nbytes, loff_t *ppos) | ||
267 | { | ||
268 | u64 val; | ||
269 | char str[64]; | ||
270 | int idx, name, len; | ||
271 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup); | ||
272 | |||
273 | idx = MEMFILE_IDX(cft->private); | ||
274 | name = MEMFILE_ATTR(cft->private); | ||
275 | |||
276 | val = res_counter_read_u64(&h_cg->hugepage[idx], name); | ||
277 | len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val); | ||
278 | return simple_read_from_buffer(buf, nbytes, ppos, str, len); | ||
279 | } | ||
280 | |||
281 | static int hugetlb_cgroup_write(struct cgroup *cgroup, struct cftype *cft, | ||
282 | const char *buffer) | ||
283 | { | ||
284 | int idx, name, ret; | ||
285 | unsigned long long val; | ||
286 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup); | ||
287 | |||
288 | idx = MEMFILE_IDX(cft->private); | ||
289 | name = MEMFILE_ATTR(cft->private); | ||
290 | |||
291 | switch (name) { | ||
292 | case RES_LIMIT: | ||
293 | if (hugetlb_cgroup_is_root(h_cg)) { | ||
294 | /* Can't set limit on root */ | ||
295 | ret = -EINVAL; | ||
296 | break; | ||
297 | } | ||
298 | /* This function does all necessary parse...reuse it */ | ||
299 | ret = res_counter_memparse_write_strategy(buffer, &val); | ||
300 | if (ret) | ||
301 | break; | ||
302 | ret = res_counter_set_limit(&h_cg->hugepage[idx], val); | ||
303 | break; | ||
304 | default: | ||
305 | ret = -EINVAL; | ||
306 | break; | ||
307 | } | ||
308 | return ret; | ||
309 | } | ||
310 | |||
311 | static int hugetlb_cgroup_reset(struct cgroup *cgroup, unsigned int event) | ||
312 | { | ||
313 | int idx, name, ret = 0; | ||
314 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup); | ||
315 | |||
316 | idx = MEMFILE_IDX(event); | ||
317 | name = MEMFILE_ATTR(event); | ||
318 | |||
319 | switch (name) { | ||
320 | case RES_MAX_USAGE: | ||
321 | res_counter_reset_max(&h_cg->hugepage[idx]); | ||
322 | break; | ||
323 | case RES_FAILCNT: | ||
324 | res_counter_reset_failcnt(&h_cg->hugepage[idx]); | ||
325 | break; | ||
326 | default: | ||
327 | ret = -EINVAL; | ||
328 | break; | ||
329 | } | ||
330 | return ret; | ||
331 | } | ||
332 | |||
333 | static char *mem_fmt(char *buf, int size, unsigned long hsize) | ||
334 | { | ||
335 | if (hsize >= (1UL << 30)) | ||
336 | snprintf(buf, size, "%luGB", hsize >> 30); | ||
337 | else if (hsize >= (1UL << 20)) | ||
338 | snprintf(buf, size, "%luMB", hsize >> 20); | ||
339 | else | ||
340 | snprintf(buf, size, "%luKB", hsize >> 10); | ||
341 | return buf; | ||
342 | } | ||
343 | |||
344 | int __init hugetlb_cgroup_file_init(int idx) | ||
345 | { | ||
346 | char buf[32]; | ||
347 | struct cftype *cft; | ||
348 | struct hstate *h = &hstates[idx]; | ||
349 | |||
350 | /* format the size */ | ||
351 | mem_fmt(buf, 32, huge_page_size(h)); | ||
352 | |||
353 | /* Add the limit file */ | ||
354 | cft = &h->cgroup_files[0]; | ||
355 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf); | ||
356 | cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT); | ||
357 | cft->read = hugetlb_cgroup_read; | ||
358 | cft->write_string = hugetlb_cgroup_write; | ||
359 | |||
360 | /* Add the usage file */ | ||
361 | cft = &h->cgroup_files[1]; | ||
362 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf); | ||
363 | cft->private = MEMFILE_PRIVATE(idx, RES_USAGE); | ||
364 | cft->read = hugetlb_cgroup_read; | ||
365 | |||
366 | /* Add the MAX usage file */ | ||
367 | cft = &h->cgroup_files[2]; | ||
368 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf); | ||
369 | cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE); | ||
370 | cft->trigger = hugetlb_cgroup_reset; | ||
371 | cft->read = hugetlb_cgroup_read; | ||
372 | |||
373 | /* Add the failcntfile */ | ||
374 | cft = &h->cgroup_files[3]; | ||
375 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf); | ||
376 | cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT); | ||
377 | cft->trigger = hugetlb_cgroup_reset; | ||
378 | cft->read = hugetlb_cgroup_read; | ||
379 | |||
380 | /* NULL terminate the last cft */ | ||
381 | cft = &h->cgroup_files[4]; | ||
382 | memset(cft, 0, sizeof(*cft)); | ||
383 | |||
384 | WARN_ON(cgroup_add_cftypes(&hugetlb_subsys, h->cgroup_files)); | ||
385 | |||
386 | return 0; | ||
387 | } | ||
388 | |||
260 | struct cgroup_subsys hugetlb_subsys = { | 389 | struct cgroup_subsys hugetlb_subsys = { |
261 | .name = "hugetlb", | 390 | .name = "hugetlb", |
262 | .create = hugetlb_cgroup_create, | 391 | .create = hugetlb_cgroup_create, |