hugetlbfs: Add cgroup resource controller for hugetlbfs
Subsystems (4)
cover hugetlbfs: Add cgroup resource controller for hugetlbfs
Hi,
This patchset implements a cgroup resource controller for HugeTLB pages. It is similar to the existing hugetlb quota support in that the limit is enforced at mmap(2) time and not at fault time. HugeTLB quota limit the number of huge pages that can allocated per superblock.
For shared mapping we track the region mapped by a task along with the hugetlb cgroup. We keep the hugetlb cgroup charged even after the task that did mmap(2) exit. The uncharge happens during truncate. For Private mapping we charge and uncharge from the current task cgroup.
A sample strace output for an application doing malloc with hugectl is given below. libhugetlbfs will fallback to normal pagesize if the HugeTLB mmap fails.
open("/mnt/libhugetlbfs.tmp.uhLMgy", O_RDWR|O_CREAT|O_EXCL, 0600) = 3 unlink("/mnt/libhugetlbfs.tmp.uhLMgy") = 0
.........
mmap(0x20000000000, 50331648, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = -1 ENOMEM (Cannot allocate memory)write(2, "libhugetlbfs", 12libhugetlbfs) = 12
write(2, ": WARNING: New heap segment map" ....mmap(NULL, 42008576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xfff946c0000
....
Changes from RFC post:
* Added support for HugeTLB cgroup hierarchy
* Added support for task migration
* Added documentation patch
* Other Bug fixes
-aneesh
PATCH 1/9 hugetlbfs: Add new HugeTLB cgroup
From: "Aneesh Kumar K.V" <[email protected]>
hugetlb controller helps in controlling the number of hugepages a cgroup can allocate. We enforce the limit during mmap time and NOT during fault time. The behaviour is similar to hugetlb quota support but quota enforce the limit per hugetlb mount point.
--- fs/hugetlbfs/Makefile | 1 + fs/hugetlbfs/hugetlb_cgroup.c | 196 ++++++++++++++++++++++++++++++++++++++++ include/linux/cgroup_subsys.h | 6 ++ include/linux/hugetlb.h | 12 ++- include/linux/hugetlb_cgroup.h | 21 +++++ init/Kconfig | 10 ++ mm/hugetlb.c | 58 ++++++++++++- 7 files changed, 301 insertions(+), 3 deletions(-) create mode 100644 fs/hugetlbfs/hugetlb_cgroup.c create mode 100644 include/linux/hugetlb_cgroup.h
fs/hugetlbfs/Makefile +1 -0
@@ -5,3 +5,4 @@ obj-$(CONFIG_HUGETLBFS) += hugetlbfs.o hugetlbfs-objs := inode.o +hugetlbfs-$(CONFIG_CGROUP_HUGETLB_RES_CTLR) += hugetlb_cgroup.o
fs/hugetlbfs/hugetlb_cgroup.c new file +196 -0
196 lines not on this page · raw message
include/linux/cgroup_subsys.h +6 -0
@@ -72,3 +72,9 @@ SUBSYS(net_prio)#endif /* */ + +#ifdef CONFIG_CGROUP_HUGETLB_RES_CTLR +SUBSYS(hugetlb) +#endif + +/* */
include/linux/hugetlb.h +10 -2
@@ -4,6 +4,7 @@#include <linux/mm_types.h> #include <linux/fs.h> #include <linux/hugetlb_inline.h> +#include <linux/cgroup.h> struct ctl_table ;struct user_struct ;
@@ -68,7 +69,8 @@ int pmd_huge(pmd_t pmd);int pud_huge (pud_t pmd);void hugetlb_change_protection (struct vm_area_struct *vma,unsigned long address,unsigned long end,pgprot_t newprot); - +int register_hugetlb_cgroup_files (struct cgroup_subsys *ss, +struct cgroup *cgroup);#else /* !CONFIG_HUGETLB_PAGE */ static inline int PageHuge (struct page *page)
@@ -109,7 +111,11 @@ static inline void copy_huge_page(struct page *dst, struct page *src) }#define hugetlb_change_protection (vma, address, end, newprot) - +static inline int register_huge_cgroup_files (struct cgroup_subsys *ss, +struct cgroup *cgroup); +{ +return 0 ; +}#endif /* !CONFIG_HUGETLB_PAGE */ #define HUGETLB_ANON_FILE "anon_hugepage"
@@ -220,6 +226,8 @@ struct hstate {unsigned int nr_huge_pages_node[MAX_NUMNODES ];unsigned int free_huge_pages_node[MAX_NUMNODES ];unsigned int surplus_huge_pages_node[MAX_NUMNODES ]; +/* cgroup control files */ +struct cftype cgroup_limit_file;char name[HSTATE_NAME_LEN ]; };
include/linux/hugetlb_cgroup.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* Copyright IBM Corporation, 2012 +* Author Aneesh Kumar K.V <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2.1 of the GNU Lesser General Public License +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +*/ + +#ifndef _LINUX_HUGETLB_CGROUP_H +#define _LINUX_HUGETLB_CGROUP_H + +extern u64 hugetlb_cgroup_read (struct cgroup *cgroup,struct cftype *cft); +extern int hugetlb_cgroup_write (struct cgroup *cgroup,struct cftype *cft, +const char *buffer); +#endif
init/Kconfig +10 -0
@@ -673,6 +673,16 @@ config CGROUP_MEM_RES_CTLR This config option also selects MM_OWNER config option, which could in turn add some fork/exit overhead. +config CGROUP_HUGETLB_RES_CTLR + bool "HugeTLB Resource Controller for Control Groups" + depends on RESOURCE_COUNTERS && HUGETLBFS + help + Provides a simple cgroup Resource Controller for HugeTLB pages. + The controller limit is enforced during mmap(2) time, so that + application can fall back to allocation using smaller page size + if the cgroup resource limit prevented them from allocating HugeTLB + pages. + config CGROUP_MEM_RES_CTLR_SWAP bool "Memory Resource Controller Swap Extension" depends on CGROUP_MEM_RES_CTLR && SWAP
mm/hugetlb.c +57 -1
76 lines not on this page · raw message
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/9 hugetlbfs: Add usage and max usage files to hugetlb cgroup
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
--- fs/hugetlbfs/hugetlb_cgroup.c | 12 ++++++++++++ include/linux/hugetlb.h | 2 ++ include/linux/hugetlb_cgroup.h | 1 + mm/hugetlb.c | 21 +++++++++++++++++++++ 4 files changed, 36 insertions(+), 0 deletions(-)
fs/hugetlbfs/hugetlb_cgroup.c +12 -0
@@ -99,6 +99,18 @@ int hugetlb_cgroup_write(struct cgroup *cgroup, struct cftype *cft,return ret; } +int hugetlb_cgroup_reset (struct cgroup *cgroup,unsigned int event) +{ +int name, idx; +struct hugetlb_cgroup *h_cgroup =cgroup_to_hugetlbcgroup (cgroup); + + idx =MEMFILE_TYPE (event); + name =MEMFILE_ATTR (event); + +res_counter_reset_max (&h_cgroup->memhuge[idx]); +return 0 ; +} +static int hugetlbcgroup_can_attach (struct cgroup_subsys *ss,struct cgroup *new_cgrp,struct cgroup_taskset *set)
include/linux/hugetlb.h +2 -0
@@ -228,6 +228,8 @@ struct hstate {unsigned int surplus_huge_pages_node[MAX_NUMNODES ];/* cgroup control files */ struct cftype cgroup_limit_file; +struct cftype cgroup_usage_file; +struct cftype cgroup_max_usage_file;char name[HSTATE_NAME_LEN ]; };
include/linux/hugetlb_cgroup.h +1 -0
@@ -18,4 +18,5 @@extern u64 hugetlb_cgroup_read (struct cgroup *cgroup,struct cftype *cft);extern int hugetlb_cgroup_write (struct cgroup *cgroup,struct cftype *cft,const char *buffer); +extern int hugetlb_cgroup_reset (struct cgroup *cgroup,unsigned int event);#endif
mm/hugetlb.c +21 -0
@@ -1814,6 +1814,13 @@ int register_hugetlb_cgroup_files(struct cgroup_subsys *ss, ret =cgroup_add_file (cgroup, ss, &h->cgroup_limit_file);if (ret)return ret; + ret =cgroup_add_file (cgroup, ss, &h->cgroup_usage_file); +if (ret) +return ret; + ret =cgroup_add_file (cgroup, ss, &h->cgroup_max_usage_file); +if (ret) +return ret; + }return ret; }
@@ -1845,6 +1852,20 @@ static int hugetlb_cgroup_file_init(struct hstate *h, int idx) cft->read_u64 = hugetlb_cgroup_read; cft->write_string = hugetlb_cgroup_write; +/* Add the usage file */ + cft = &h->cgroup_usage_file; +snprintf (cft->name,MAX_CFTYPE_NAME ,"%s.usage_in_bytes" , buf); + cft->private =MEMFILE_PRIVATE (idx,RES_USAGE ); + cft->read_u64 = hugetlb_cgroup_read; + +/* Add the MAX usage file */ + cft = &h->cgroup_max_usage_file; +snprintf (cft->name,MAX_CFTYPE_NAME , +"%s.max_usage_in_bytes" , buf); + cft->private =MEMFILE_PRIVATE (idx,RES_MAX_USAGE ); + cft->trigger = hugetlb_cgroup_reset; + cft->read_u64 = hugetlb_cgroup_read; + return 0; }#else
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/9 hugetlbfs: Add new region handling functions.
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
These functions takes an extra argument and only merge regions if the data value matches. This help us to build regions with difference hugetlb cgroup values. Last patch in the series will merge this to existing region code, having this as separate allows us to add cgroup support shared and private mapping in separate patchset.
--- fs/hugetlbfs/hugetlb_cgroup.c | 127 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 127 insertions(+), 0 deletions(-)
fs/hugetlbfs/hugetlb_cgroup.c +127 -0
@@ -31,9 +31,136 @@ struct hugetlb_cgroup {struct res_counter memhuge[HUGE_MAX_HSTATE ]; }; +struct file_region_with_data { +struct list_head link; +long from; +long to; +unsigned long data; +}; +struct cgroup_subsys hugetlb_subsys__read_mostly ;struct hugetlb_cgroup *root_h_cgroup__read_mostly ; +/* +* A vairant of region_add that only merges regions only if data +* match. +*/ +static long region_chg_with_same (struct list_head *head, +long f,long t,unsigned long data) +{ +long chg =0 ; +struct file_region_with_data *rg, *nrg, *trg; + +/* Locate the region we are before or in. */ +list_for_each_entry (rg, head, link) +if (f <= rg->to) +break ; +/* +* If we are below the current region then a new region is required. +* Subtle, allocate a new region at the position but make it zero +* size such that we can guarantee to record the reservation. +*/ +if (&rg->link == head || t < rg->from) { + nrg =kmalloc (sizeof (*nrg),GFP_KERNEL ); +if (!nrg) +return -ENOMEM ; + nrg->from = f; + nrg->to = f; + nrg->data = data; +INIT_LIST_HEAD (&nrg->link); +list_add (&nrg->link, rg->link.prev); +return t - f; + } +/* +* f rg->from t rg->to +*/ +if (f < rg->from && data != rg->data) { +/* we need to allocate a new region */ + nrg =kmalloc (sizeof (*nrg),GFP_KERNEL ); +if (!nrg) +return -ENOMEM ; + nrg->from = f; + nrg->to = f; + nrg->data = data; +INIT_LIST_HEAD (&nrg->link); +list_add (&nrg->link, rg->link.prev); + } + +/* Round our left edge to the current segment if it encloses us. */ +if (f > rg->from) + f = rg->from; + chg = t - f; + +/* Check for and consume any regions we now overlap with. */ +list_for_each_entry_safe (rg, trg, rg->link.prev, link) { +if (&rg->link == head) +break ; +if (rg->from > t) +return chg; +/* +* rg->from f rg->to t +*/ +if (t > rg->to && data != rg->data) { +/* we need to allocate a new region */ + nrg =kmalloc (sizeof (*nrg),GFP_KERNEL ); +if (!nrg) +return -ENOMEM ; + nrg->from = rg->to; + nrg->to = rg->to; + nrg->data = data; +INIT_LIST_HEAD (&nrg->link); +list_add (&nrg->link, &rg->link); + } +/* +* update charge +*/ +if (rg->to > t) { + chg += rg->to - t; + t = rg->to; + } + chg -= rg->to - rg->from; + } +return chg; +} + +static void region_add_with_same (struct list_head *head, +long f,long t,unsigned long data) +{ +struct file_region_with_data *rg, *nrg, *trg; + +/* Locate the region we are before or in. */ +list_for_each_entry (rg, head, link) +if (f <= rg->to) +break ; + +list_for_each_entry_safe (rg, trg, rg->link.prev, link) { + +if (rg->from > t) +return ; +if (&rg->link == head) +return ; + +/*FIXME!! this can possibly delete few regions */ +/* We need to worry only if we match data */ +if (rg->data == data) { +if (f < rg->from) + rg->from = f; +if (t > rg->to) { +/* if we are the last entry */ +if (rg->link.next == head) { + rg->to = t; +break ; + }else { + nrg =list_entry (rg->link.next, +typeof (*nrg), link); + rg->to = nrg->from; + } + } + } + f = rg->to; + } +} +static inline struct hugetlb_cgroup *css_to_hugetlbcgroup (struct cgroup_subsys_state *s) {
Signature · 1 line
1.7.9
PATCH 4/9 hugetlbfs: Add controller support for shared mapping
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
HugeTLB controller is different from a memory controller in that we charge controller during mmap() time and not fault time. This make sure userspace can fallback to non-hugepage allocation when mmap fails during to controller limit.
For shared mapping we need to track the hugetlb cgroup along with the range. If two task in two different cgroup map the same area only the non-overlapping part should be charged to the second task. Hence we need to track the cgroup along with range. We always charge during mmap(2) and we do uncharge during truncate. The region list is tracked in the inode->i_mapping->private_list.
--- fs/hugetlbfs/hugetlb_cgroup.c | 114 ++++++++++++++++++++++++++++++++++++++++ fs/hugetlbfs/inode.c | 1 + include/linux/hugetlb_cgroup.h | 39 ++++++++++++++ mm/hugetlb.c | 84 ++++++++++++++++++++--------- 4 files changed, 212 insertions(+), 26 deletions(-)
fs/hugetlbfs/hugetlb_cgroup.c +114 -0
@@ -17,6 +17,7 @@#include <linux/slab.h> #include <linux/hugetlb.h> #include <linux/res_counter.h> +#include <linux/list.h> /* lifted from mem control */ #define MEMFILE_PRIVATE (x, val) (((x) << 16) | (val))
@@ -333,3 +334,116 @@ struct cgroup_subsys hugetlb_subsys = { .populate = hugetlbcgroup_populate, .subsys_id = hugetlb_subsys_id, }; + +long hugetlb_page_charge (struct list_head *head, +struct hstate *h,long f,long t) +{ +long chg; +int ret =0 , idx; +unsigned long csize; +struct hugetlb_cgroup *h_cg; +struct res_counter *fail_res; + +/* +* Get the task cgroup within rcu_readlock and also +* get cgroup reference to make sure cgroup destroy won't +* race with page_charge. We don't allow a cgroup destroy +* when the cgroup have some charge against it +*/ +rcu_read_lock (); + h_cg =task_hugetlbcgroup (current); +css_get (&h_cg->css); +rcu_read_unlock (); + + chg =region_chg_with_same (head, f, t, (unsigned long )h_cg); +if (chg <0 ) + goto err_out; + +if (hugetlb_cgroup_is_root (h_cg)) + goto err_out; + + csize = chg *huge_page_size (h); + idx = h - hstates; + ret =res_counter_charge (&h_cg->memhuge[idx], csize, &fail_res); + +err_out: +/* Now that we have charged we can drop cgroup reference */ +css_put (&h_cg->css); +if (!ret) +return chg; + +/* We don't worry about region_uncharge */ +return ret; +} + +void hugetlb_page_uncharge (struct list_head *head,int idx,long nr_pages) +{ +struct hugetlb_cgroup *h_cg; +unsigned long csize = nr_pages *PAGE_SIZE ; + +rcu_read_lock (); + h_cg =task_hugetlbcgroup (current); + +if (!hugetlb_cgroup_is_root (h_cg)) +res_counter_uncharge (&h_cg->memhuge[idx], csize); +rcu_read_unlock (); +/* +* We could ideally remove zero size regions from +* resv map hcg_regions here +*/ +return ; +} + +void hugetlb_commit_page_charge (struct list_head *head,long f,long t) +{ +struct hugetlb_cgroup *h_cg; + +rcu_read_lock (); + h_cg =task_hugetlbcgroup (current); +region_add_with_same (head, f, t, (unsigned long )h_cg); +rcu_read_unlock (); +return ; +} + +long hugetlb_truncate_cgroup (struct hstate *h, +struct list_head *head,long end) +{ +long chg =0 , csize; +int idx = h - hstates; +struct hugetlb_cgroup *h_cg; +struct file_region_with_data *rg, *trg; + +/* Locate the region we are either in or before. */ +list_for_each_entry (rg, head, link) +if (end <= rg->to) +break ; +if (&rg->link == head) +return 0 ; + +/* If we are in the middle of a region then adjust it. */ +if (end > rg->from) { + chg = rg->to - end; + rg->to = end; + h_cg = (struct hugetlb_cgroup *)rg->data; +if (!hugetlb_cgroup_is_root (h_cg)) { + csize = chg *huge_page_size (h); +res_counter_uncharge (&h_cg->memhuge[idx], csize); + } + rg =list_entry (rg->link.next,typeof (*rg), link); + } + +/* Drop any remaining regions. */ +list_for_each_entry_safe (rg, trg, rg->link.prev, link) { +if (&rg->link == head) +break ; + chg += rg->to - rg->from; + h_cg = (struct hugetlb_cgroup *)rg->data; +if (!hugetlb_cgroup_is_root (h_cg)) { + csize = (rg->to - rg->from) *huge_page_size (h); +res_counter_uncharge (&h_cg->memhuge[idx], csize); + } +list_del (&rg->link); +kfree (rg); + } +return chg; +}
fs/hugetlbfs/inode.c +1 -0
@@ -32,6 +32,7 @@#include <linux/security.h> #include <linux/magic.h> #include <linux/migrate.h> +#include <linux/hugetlb_cgroup.h> #include <asm/uaccess.h>
include/linux/hugetlb_cgroup.h +39 -0
@@ -15,8 +15,47 @@#ifndef _LINUX_HUGETLB_CGROUP_H#define _LINUX_HUGETLB_CGROUP_H +extern long region_add (struct list_head *head,long f,long t); +extern long region_chg (struct list_head *head,long f,long t); +extern long region_truncate (struct list_head *head,long end); +extern long region_count (struct list_head *head,long f,long t); + +#ifdef CONFIG_CGROUP_HUGETLB_RES_CTLR extern u64 hugetlb_cgroup_read (struct cgroup *cgroup,struct cftype *cft);extern int hugetlb_cgroup_write (struct cgroup *cgroup,struct cftype *cft,const char *buffer);extern int hugetlb_cgroup_reset (struct cgroup *cgroup,unsigned int event); +extern long hugetlb_page_charge (struct list_head *head, +struct hstate *h,long f,long t); +extern void hugetlb_page_uncharge (struct list_head *head, +int idx,long nr_pages); +extern void hugetlb_commit_page_charge (struct list_head *head,long f,long t); +extern long hugetlb_truncate_cgroup (struct hstate *h, +struct list_head *head,long from); +#else +static inline long hugetlb_page_charge (struct list_head *head, +struct hstate *h,long f,long t) +{ +return region_chg (head, f, t); +} + +static inline void hugetlb_page_uncharge (struct list_head *head, +int idx,long nr_pages) +{ +return ; +} + +static inline void hugetlb_commit_page_charge (struct list_head *head, +long f,long t) +{ +region_add (head, f, t); +return ; +} + +static inline long hugetlb_truncate_cgroup (struct hstate *h, +struct list_head *head,long from) +{ +return region_truncate (head, from); +} +#endif /* CONFIG_CGROUP_HUGETLB_RES_CTLR */ #endif
mm/hugetlb.c +58 -26
179 lines not on this page · raw message
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 5/9 hugetlbfs: Add controller support for private mapping
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
HugeTLB controller is different from a memory controller in that we charge controller during mmap() time and not fault time. This make sure userspace can fallback to non-hugepage allocation when mmap fails due to controller limit.
For private mapping we always charge/uncharge from the current task cgroup. Charging happens during mmap(2) and uncharge happens during the vm_operations->close when resv_map refcount reaches zero. The uncharge count is stored in struct resv_map. For child task after fork the charging happens during fault time in alloc_huge_page. We also need to make sure for private mapping each vma for hugeTLB mapping have struct resv_map allocated so that we can store the uncharge count in resv_map.
--- fs/hugetlbfs/hugetlb_cgroup.c | 50 ++++++++++++++++++++++++++++++++ include/linux/hugetlb.h | 7 ++++ include/linux/hugetlb_cgroup.h | 16 ++++++++++ mm/hugetlb.c | 62 ++++++++++++++++++++++++++++++++-------- 4 files changed, 123 insertions(+), 12 deletions(-)
fs/hugetlbfs/hugetlb_cgroup.c +50 -0
@@ -447,3 +447,53 @@ long hugetlb_truncate_cgroup(struct hstate *h, }return chg; } + +int hugetlb_priv_page_charge (struct resv_map *map,struct hstate *h,long chg) +{ +long csize; +int idx, ret; +struct hugetlb_cgroup *h_cg; +struct res_counter *fail_res; + +/* +* Get the task cgroup within rcu_readlock and also +* get cgroup reference to make sure cgroup destroy won't +* race with page_charge. We don't allow a cgroup destroy +* when the cgroup have some charge against it +*/ +rcu_read_lock (); + h_cg =task_hugetlbcgroup (current); +css_get (&h_cg->css); +rcu_read_unlock (); + +if (hugetlb_cgroup_is_root (h_cg)) { + ret = chg; + goto err_out; + } + + csize = chg *huge_page_size (h); + idx = h - hstates; + ret =res_counter_charge (&h_cg->memhuge[idx], csize, &fail_res); +if (!ret) { + map->nr_pages[idx] += chg <<huge_page_order (h); + ret = chg; + } +err_out: +css_put (&h_cg->css); +return ret; +} + +void hugetlb_priv_page_uncharge (struct resv_map *map,int idx,long nr_pages) +{ +struct hugetlb_cgroup *h_cg; +unsigned long csize = nr_pages *PAGE_SIZE ; + +rcu_read_lock (); + h_cg =task_hugetlbcgroup (current); +if (!hugetlb_cgroup_is_root (h_cg)) { +res_counter_uncharge (&h_cg->memhuge[idx], csize); + map->nr_pages[idx] -= nr_pages; + } +rcu_read_unlock (); +return ; +}
include/linux/hugetlb.h +7 -0
@@ -253,6 +253,12 @@ struct hstate *size_to_hstate(unsigned long size);#define HUGE_MAX_HSTATE 1#endif +struct resv_map { +struct kref refs; +long nr_pages[HUGE_MAX_HSTATE ]; +struct list_head regions; +}; +extern struct hstate hstates[HUGE_MAX_HSTATE ];extern unsigned int default_hstate_idx;
@@ -323,6 +329,7 @@ static inline unsigned hstate_index_to_shift(unsigned index)#else struct hstate {}; +struct resv_map {};#define alloc_huge_page_node (h, nid) NULL#define alloc_bootmem_huge_page (h) NULL#define hstate_file (f) NULL
include/linux/hugetlb_cgroup.h +16 -0
@@ -32,6 +32,10 @@ extern void hugetlb_page_uncharge(struct list_head *head,extern void hugetlb_commit_page_charge (struct list_head *head,long f,long t);extern long hugetlb_truncate_cgroup (struct hstate *h,struct list_head *head,long from); +extern int hugetlb_priv_page_charge (struct resv_map *map, +struct hstate *h,long chg); +extern void hugetlb_priv_page_uncharge (struct resv_map *map, +int idx,long nr_pages);#else static inline long hugetlb_page_charge (struct list_head *head,struct hstate *h,long f,long t)
@@ -57,5 +61,17 @@ static inline long hugetlb_truncate_cgroup(struct hstate *h, {return region_truncate (head, from); } + +static inline int hugetlb_priv_page_charge (struct resv_map *map, +struct hstate *h,long chg) +{ +return chg; +} + +static inline void hugetlb_priv_page_uncharge (struct resv_map *map, +int idx,long nr_pages) +{ +return ; +}#endif /* CONFIG_CGROUP_HUGETLB_RES_CTLR */ #endif
mm/hugetlb.c +50 -12
129 lines not on this page · raw message
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 6/9 hugetlbfs: Switch to new region APIs
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
Remove the old code which is not used
--- fs/hugetlbfs/Makefile | 2 +- fs/hugetlbfs/hugetlb_cgroup.c | 135 +-------------------------- fs/hugetlbfs/region.c | 202 ++++++++++++++++++++++++++++++++++++++++ include/linux/hugetlb_cgroup.h | 17 +++- mm/hugetlb.c | 163 +-------------------------------- 5 files changed, 222 insertions(+), 297 deletions(-) create mode 100644 fs/hugetlbfs/region.c
fs/hugetlbfs/Makefile +1 -1
@@ -4,5 +4,5 @@ obj-$(CONFIG_HUGETLBFS) += hugetlbfs.o -hugetlbfs-objs := inode.o +hugetlbfs-objs := inode.o region.o hugetlbfs-$(CONFIG_CGROUP_HUGETLB_RES_CTLR) += hugetlb_cgroup.o
fs/hugetlbfs/hugetlb_cgroup.c +5 -130
168 lines not on this page · raw message
fs/hugetlbfs/region.c new file +202 -0
202 lines not on this page · raw message
include/linux/hugetlb_cgroup.h +12 -5
@@ -15,8 +15,16 @@#ifndef _LINUX_HUGETLB_CGROUP_H#define _LINUX_HUGETLB_CGROUP_H -extern long region_add (struct list_head *head,long f,long t); -extern long region_chg (struct list_head *head,long f,long t); +struct file_region { +long from, to; +unsigned long data; +struct list_head link; +}; + +extern long region_chg (struct list_head *head,long f,long t, +unsigned long data); +extern void region_add (struct list_head *head,long f,long t, +unsigned long data);extern long region_truncate (struct list_head *head,long end);extern long region_count (struct list_head *head,long f,long t);
@@ -40,7 +48,7 @@ extern void hugetlb_priv_page_uncharge(struct resv_map *map,static inline long hugetlb_page_charge (struct list_head *head,struct hstate *h,long f,long t) { -return region_chg (head, f, t); +return region_chg (head, f, t,0 ); } static inline void hugetlb_page_uncharge(struct list_head *head,
@@ -52,8 +60,7 @@ static inline void hugetlb_page_uncharge(struct list_head *head,static inline void hugetlb_commit_page_charge (struct list_head *head,long f,long t) { -region_add (head, f, t); -return ; +return region_add (head, f, t,0 ); } static inline long hugetlb_truncate_cgroup(struct hstate *h,
mm/hugetlb.c +2 -161
181 lines not on this page · raw message
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 7/9 hugetlbfs: Add truncate region functions
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
This will later be used by the task migration patches.
--- fs/hugetlbfs/hugetlb_cgroup.c | 84 ++++++++++++++++++++++++++++++++++++++++ fs/hugetlbfs/region.c | 58 +++++++++++++++++++++++++++ include/linux/hugetlb_cgroup.h | 12 +++++- 3 files changed, 153 insertions(+), 1 deletions(-)
fs/hugetlbfs/hugetlb_cgroup.c +84 -0
@@ -323,6 +323,90 @@ long hugetlb_truncate_cgroup(struct hstate *h,return chg; } +long hugetlb_truncate_cgroup_range (struct hstate *h, +struct list_head *head,long from,long to) +{ +long chg =0 , csize; +int idx = h - hstates; +struct hugetlb_cgroup *h_cg; +struct file_region *rg, *trg; + +/* Locate the region we are either in or before. */ +list_for_each_entry (rg, head, link) +if (from <= rg->to) +break ; +if (&rg->link == head) +return 0 ; + +/* If we are in the middle of a region then adjust it. */ +if (from > rg->from) { +if (to < rg->to) { +struct file_region *nrg; +/* rg->from from to rg->to */ + nrg =kmalloc (sizeof (*nrg),GFP_KERNEL ); +/* +* If we fail to allocate we return the +* with the 0 charge . Later a complete +* truncate will reclaim the left over space +*/ +if (!nrg) +return 0 ; + nrg->from = to; + nrg->to = rg->to; + nrg->data = rg->data; +INIT_LIST_HEAD (&nrg->link); +list_add (&nrg->link, &rg->link); + +/* Adjust the rg entry */ + rg->to = from; + chg = to - from; + h_cg = (struct hugetlb_cgroup *)rg->data; +if (!hugetlb_cgroup_is_root (h_cg)) { + csize = chg *huge_page_size (h); +res_counter_uncharge (&h_cg->memhuge[idx], + csize); + } +return chg; + } + chg = rg->to - from; + rg->to = from; + h_cg = (struct hugetlb_cgroup *)rg->data; +if (!hugetlb_cgroup_is_root (h_cg)) { + csize = chg *huge_page_size (h); +res_counter_uncharge (&h_cg->memhuge[idx], csize); + } + rg =list_entry (rg->link.next,typeof (*rg), link); + } +/* Drop any remaining regions till to */ +list_for_each_entry_safe (rg, trg, rg->link.prev, link) { +if (rg->from >= to) +break ; +if (&rg->link == head) +break ; +if (rg->to > to) { +/* rg->from to rg->to */ + chg += to - rg->from; + rg->from = to; + h_cg = (struct hugetlb_cgroup *)rg->data; +if (!hugetlb_cgroup_is_root (h_cg)) { + csize = (to - rg->from) *huge_page_size (h); +res_counter_uncharge (&h_cg->memhuge[idx], + csize); + } +return chg; + } + chg += rg->to - rg->from; + h_cg = (struct hugetlb_cgroup *)rg->data; +if (!hugetlb_cgroup_is_root (h_cg)) { + csize = (rg->to - rg->from) *huge_page_size (h); +res_counter_uncharge (&h_cg->memhuge[idx], csize); + } +list_del (&rg->link); +kfree (rg); + } +return chg; +} +int hugetlb_priv_page_charge (struct resv_map *map,struct hstate *h,long chg) {long csize;
fs/hugetlbfs/region.c +58 -0
61 lines not on this page · raw message
include/linux/hugetlb_cgroup.h +11 -1
@@ -27,7 +27,7 @@ extern void region_add(struct list_head *head, long f, long t, unsigned long data);extern long region_truncate (struct list_head *head,long end);extern long region_count (struct list_head *head,long f,long t); - +extern long region_truncate_range (struct list_head *head,long from,long end);#ifdef CONFIG_CGROUP_HUGETLB_RES_CTLR extern u64 hugetlb_cgroup_read (struct cgroup *cgroup,struct cftype *cft); extern int hugetlb_cgroup_write(struct cgroup *cgroup, struct cftype *cft,
@@ -40,6 +40,9 @@ extern void hugetlb_page_uncharge(struct list_head *head,extern void hugetlb_commit_page_charge (struct list_head *head,long f,long t);extern long hugetlb_truncate_cgroup (struct hstate *h,struct list_head *head,long from); +extern long hugetlb_truncate_cgroup_range (struct hstate *h, +struct list_head *head, +long from,long end);extern int hugetlb_priv_page_charge (struct resv_map *map,struct hstate *h,long chg); extern void hugetlb_priv_page_uncharge(struct resv_map *map,
@@ -69,6 +72,13 @@ static inline long hugetlb_truncate_cgroup(struct hstate *h,return region_truncate (head, from); } +static inline long hugetlb_truncate_cgroup_range (struct hstate *h, +struct list_head *head, +long from,long end) +{ +return region_truncate_range (head, from, end); +} +static inline int hugetlb_priv_page_charge (struct resv_map *map,struct hstate *h,long chg) {
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 8/9 hugetlbfs: Add task migration support
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
This patch add task migration support to hugetlb cgroup. When task migrate we don't move charge across hugetlb cgroup.
--- fs/hugetlbfs/hugetlb_cgroup.c | 74 ---------------------- fs/hugetlbfs/region.c | 24 ------- include/linux/hugetlb.h | 1 - include/linux/hugetlb_cgroup.h | 17 ----- mm/hugetlb.c | 134 +++++++++++++++------------------------- 5 files changed, 49 insertions(+), 201 deletions(-)
fs/hugetlbfs/hugetlb_cgroup.c +0 -74
@@ -114,29 +114,6 @@ int hugetlb_cgroup_reset(struct cgroup *cgroup, unsigned int event)return 0 ; } -static int hugetlbcgroup_can_attach (struct cgroup_subsys *ss, -struct cgroup *new_cgrp, -struct cgroup_taskset *set) -{ -struct hugetlb_cgroup *h_cg; -struct task_struct *task =cgroup_taskset_first (set); -/* -* Make sure all the task in the set are in root cgroup -* We only allow move from root cgroup to other cgroup. -*/ -while (task !=NULL ) { -rcu_read_lock (); - h_cg =task_hugetlbcgroup (task); -if (!hugetlb_cgroup_is_root (h_cg)) { -rcu_read_unlock (); -return -EOPNOTSUPP ; - } -rcu_read_unlock (); - task =cgroup_taskset_next (set); - } -return 0 ; -} -/* * called from kernel/cgroup.c with cgroup_lock() held. */
@@ -202,7 +179,6 @@ static int hugetlbcgroup_populate(struct cgroup_subsys *ss,struct cgroup_subsys hugetlb_subsys = { .name ="hugetlb" , - .can_attach = hugetlbcgroup_can_attach, .create = hugetlbcgroup_create, .pre_destroy = hugetlbcgroup_pre_destroy, .destroy = hugetlbcgroup_destroy,
@@ -406,53 +382,3 @@ long hugetlb_truncate_cgroup_range(struct hstate *h, }return chg; } - -int hugetlb_priv_page_charge (struct resv_map *map,struct hstate *h,long chg) -{ -long csize; -int idx, ret; -struct hugetlb_cgroup *h_cg; -struct res_counter *fail_res; - -/* -* Get the task cgroup within rcu_readlock and also -* get cgroup reference to make sure cgroup destroy won't -* race with page_charge. We don't allow a cgroup destroy -* when the cgroup have some charge against it -*/ -rcu_read_lock (); - h_cg =task_hugetlbcgroup (current); -css_get (&h_cg->css); -rcu_read_unlock (); - -if (hugetlb_cgroup_is_root (h_cg)) { - ret = chg; - goto err_out; - } - - csize = chg *huge_page_size (h); - idx = h - hstates; - ret =res_counter_charge (&h_cg->memhuge[idx], csize, &fail_res); -if (!ret) { - map->nr_pages[idx] += chg <<huge_page_order (h); - ret = chg; - } -err_out: -css_put (&h_cg->css); -return ret; -} - -void hugetlb_priv_page_uncharge (struct resv_map *map,int idx,long nr_pages) -{ -struct hugetlb_cgroup *h_cg; -unsigned long csize = nr_pages *PAGE_SIZE ; - -rcu_read_lock (); - h_cg =task_hugetlbcgroup (current); -if (!hugetlb_cgroup_is_root (h_cg)) { -res_counter_uncharge (&h_cg->memhuge[idx], csize); - map->nr_pages[idx] -= nr_pages; - } -rcu_read_unlock (); -return ; -}
fs/hugetlbfs/region.c +0 -24
@@ -177,30 +177,6 @@ long region_truncate(struct list_head *head, long end)return chg; } -long region_count (struct list_head *head,long f,long t) -{ -struct file_region *rg; -long chg =0 ; - -/* Locate each segment we overlap with, and count that overlap. */ -list_for_each_entry (rg, head, link) { -int seg_from; -int seg_to; - -if (rg->to <= f) -continue ; -if (rg->from >= t) -break ; - - seg_from =max (rg->from, f); - seg_to =min (rg->to, t); - - chg += seg_to - seg_from; - } - -return chg; -} -long region_truncate_range (struct list_head *head,long from,long to) {long chg =0 ;
include/linux/hugetlb.h +0 -1
@@ -255,7 +255,6 @@ struct hstate *size_to_hstate(unsigned long size);struct resv_map {struct kref refs; -long nr_pages[HUGE_MAX_HSTATE ];struct list_head regions; };
include/linux/hugetlb_cgroup.h +0 -17
@@ -26,7 +26,6 @@ extern long region_chg(struct list_head *head, long f, long t,extern void region_add (struct list_head *head,long f,long t,unsigned long data);extern long region_truncate (struct list_head *head,long end); -extern long region_count (struct list_head *head,long f,long t);extern long region_truncate_range (struct list_head *head,long from,long end);#ifdef CONFIG_CGROUP_HUGETLB_RES_CTLR extern u64 hugetlb_cgroup_read (struct cgroup *cgroup,struct cftype *cft);
@@ -43,10 +42,6 @@ extern long hugetlb_truncate_cgroup(struct hstate *h,extern long hugetlb_truncate_cgroup_range (struct hstate *h,struct list_head *head,long from,long end); -extern int hugetlb_priv_page_charge (struct resv_map *map, -struct hstate *h,long chg); -extern void hugetlb_priv_page_uncharge (struct resv_map *map, -int idx,long nr_pages);#else static inline long hugetlb_page_charge (struct list_head *head,struct hstate *h,long f,long t)
@@ -78,17 +73,5 @@ static inline long hugetlb_truncate_cgroup_range(struct hstate *h, {return region_truncate_range (head, from, end); } - -static inline int hugetlb_priv_page_charge (struct resv_map *map, -struct hstate *h,long chg) -{ -return chg; -} - -static inline void hugetlb_priv_page_uncharge (struct resv_map *map, -int idx,long nr_pages) -{ -return ; -}#endif /* CONFIG_CGROUP_HUGETLB_RES_CTLR */ #endif
mm/hugetlb.c +49 -85
249 lines not on this page · raw message
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 9/9 hugetlbfs: Add HugeTLB controller documentation
In reply to Aneesh Kumar K.VFrom: "Aneesh Kumar K.V" <[email protected]>
--- Documentation/cgroups/hugetlb.txt | 54 +++++++++++++++++++++++++++++++++++++ 1 files changed, 54 insertions(+), 0 deletions(-) create mode 100644 Documentation/cgroups/hugetlb.txt
Documentation/cgroups/hugetlb.txt new file +54 -0
@@ -0,0 +1,54 @@ +HugeTLB controller +----------------- + +The HugetTLB controller is used to group tasks using cgroups and +limit the HugeTLB pages used by these groups of tasks. HugetTLB cgroup +enforce the limit during mmap(2). This enables application to fall back +to allocation using smaller page size if the cgroup resource limit prevented +them from allocating HugeTLB pages. + + +The HugetTLB controller supports multi-hierarchy groups and task migration +across cgroups. + +HugeTLB groups can be created by first mounting the cgroup filesystem. + +# mount -t cgroup -o hugetlb none /sys/fs/cgroup + +With the above step, the initial or the root HugeTLB cgroup becomes +visible at /sys/fs/cgroup. At bootup, this group includes all the tasks in +the system. /sys/fs/cgroup/tasks lists the tasks in this cgroup. HugeTLB +cgroup create seperate limit, usage and max_usage files for each huge page +size supported. An example listing is given below + +hugetlb.16GB.limit_in_bytes +hugetlb.16GB.max_usage_in_bytes +hugetlb.16GB.usage_in_bytes +hugetlb.16MB.limit_in_bytes +hugetlb.16MB.max_usage_in_bytes +hugetlb.16MB.usage_in_bytes + +/sys/fs/cgroup/hugetlb.<pagesize>.usage_in_bytes gives the HugeTLB usage +by this group which is essentially the total size HugeTLB pages obtained +by all the tasks in the system. + +New cgroup can be created under root HugeTLB cgroup /sys/fs/cgroup + +# cd /sys/fs/cgroup +# mkdir g1 +# echo $$ > g1/tasks + +The above steps create a new group g1 and move the current shell +process (bash) into it. 16MB HugeTLB pages consumed by this bash and its +children can be obtained from g1/hugetlb.16MB.usage_in_bytes and the same +is accumulated in /sys/fs/cgroup/hugetlb.16MB.usage_in_bytes. + +We can limit the usage of 16MB hugepage by a hugeTLB cgroup using +hugetlb.16MB.limit_in_bytes + +# echo 16M > /sys/fs/cgroup/g1/hugetlb.16MB.limit_in_bytes +# hugectl --heap=16M /root/heap +libhugetlbfs: WARNING: New heap segment map at 0x20000000000 failed: Cannot allocate memory +# echo -1 > /sys/fs/cgroup/g1/hugetlb.16MB.limit_in_bytes +# hugectl --heap=16M /root/heap +#
Signature · 8 lines
1.7.9 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>