From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from d03relay04.boulder.ibm.com (d03relay04.boulder.ibm.com [9.17.195.106]) by e35.co.us.ibm.com (8.13.8/8.13.8) with ESMTP id m3THBjNI031663 for ; Tue, 29 Apr 2008 13:11:45 -0400 Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay04.boulder.ibm.com (8.13.8/8.13.8/NCO v8.7) with ESMTP id m3THBjcP126960 for ; Tue, 29 Apr 2008 11:11:45 -0600 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m3THBSDO020542 for ; Tue, 29 Apr 2008 11:11:30 -0600 Date: Tue, 29 Apr 2008 10:11:15 -0700 From: Nishanth Aravamudan Subject: Re: [RFC][PATCH] hugetlb: add information and interface in sysfs [Was Re: [RFC][PATCH 4/5] Documentation: add node files to sysfs ABI] Message-ID: <20080429171115.GD24967@us.ibm.com> References: <20080422051447.GI21993@wotan.suse.de> <20080422165602.GA29570@us.ibm.com> <20080423010259.GA17572@wotan.suse.de> <20080423183252.GA10548@us.ibm.com> <20080424071352.GB14543@wotan.suse.de> <20080427034942.GB12129@us.ibm.com> <20080427051029.GA22858@suse.de> <20080428172239.GA24169@us.ibm.com> <20080428172951.GA764@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20080428172951.GA764@suse.de> Sender: owner-linux-mm@kvack.org Return-Path: To: Greg KH Cc: Nick Piggin , Christoph Lameter , wli@holomorphy.com, agl@us.ibm.com, luick@cray.com, Lee.Schermerhorn@hp.com, linux-mm@kvack.org List-ID: On 28.04.2008 [10:29:51 -0700], Greg KH wrote: > On Mon, Apr 28, 2008 at 10:22:39AM -0700, Nishanth Aravamudan wrote: > > On 26.04.2008 [22:10:29 -0700], Greg KH wrote: > > > On Sat, Apr 26, 2008 at 08:49:42PM -0700, Nishanth Aravamudan wrote: > > > > > > > > [20:41:56]nacc@arkanoid:/sys/kernel/hugepages$ cat /sys/kernel/hugepages/hugepages-2MB/meminfo > > > > HugePages_Total: 0 > > > > HugePages_Free: 0 > > > > HugePages_Rsvd: 0 > > > > HugePages_Surp: 0 > > > > Hugepagesize: 2048 kB > > > > > > > > Greg, do you see any obvious violations of sysfs rules here? Well, beyond > > > > meminfo itself, I guess, but given our previous snapshot discussion, I left it > > > > simple and the same, rather than split it up. > > > > > > Yeah, I don't like that file. Why not just have 5 files, one for each > > > value? There isn't such a need for an immediate snapshot shere you > > > can't just read all 5 values from 5 files? > > > > Actually, we already have Total in nr_hugepages, so I only needed to add > > 3 files. The size is implicit in the directory name? > > Ah, good point. > > > > Also, why use a "units" here, just always use the lowest unit, and > > > userspace can convert from kB to GB if needed. > > > > Agreed, so I changed the name of the directory from > > > > hugepages-2M > > > > to > > > > hugpeages-2048 > > > > for instance. Userspace utilities can pretty-ize it :) > > Exactly, that would be much better. FWIW, here's the updated patch. Still needs more testing. include/linux/hugetlb.h | 9 +- mm/hugetlb.c | 322 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 256 insertions(+), 75 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 7aa22e7..cac63bd 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -3,6 +3,9 @@ #include #include +#include +#include +#include #ifdef CONFIG_HUGETLBFS struct hugetlbfs_config { @@ -69,10 +72,6 @@ static inline void set_file_hugepages(struct file *file) #ifdef CONFIG_HUGETLB_PAGE -#include -#include -#include - struct ctl_table; static inline int is_vm_hugetlb_page(struct vm_area_struct *vma) @@ -131,6 +130,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]; + const char *name; + struct kobject kobj; }; void __init huge_add_hstate(unsigned order); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index de03a14..1d94a85 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -624,6 +625,8 @@ static void __init hugetlb_init_hstate(struct hstate *h) break; } h->max_huge_pages = i; + h->name = kasprintf(GFP_KERNEL, "hugepages-%lu", + huge_page_size(h) / 1024); } static void __init hugetlb_init_hstates(void) @@ -662,77 +665,6 @@ static void __init report_hugepages(void) } } -static int __init hugetlb_init(void) -{ - BUILD_BUG_ON(HPAGE_SHIFT == 0); - - if (!size_to_hstate(HPAGE_SIZE)) { - huge_add_hstate(HUGETLB_PAGE_ORDER); - parsed_hstate->max_huge_pages = default_hstate_resv; - } - - hugetlb_init_hstates(); - - gather_bootmem_prealloc(); - - report_hugepages(); - - return 0; -} -module_init(hugetlb_init); - -/* Should be called on processing a hugepagesz=... option */ -void __init huge_add_hstate(unsigned order) -{ - struct hstate *h; - if (size_to_hstate(PAGE_SIZE << order)) { - printk("hugepagesz= specified twice, ignoring\n"); - return; - } - BUG_ON(max_hstate >= HUGE_MAX_HSTATE); - BUG_ON(order < HPAGE_SHIFT - PAGE_SHIFT); - h = &hstates[max_hstate++]; - h->order = order; - h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); - hugetlb_init_hstate(h); - parsed_hstate = h; -} - -static int __init hugetlb_setup(char *s) -{ - unsigned long *mhp; - - if (!max_hstate) - mhp = &default_hstate_resv; - else - mhp = &parsed_hstate->max_huge_pages; - - if (sscanf(s, "%lu", mhp) <= 0) - *mhp = 0; - - /* - * Global state is always initialized later in hugetlb_init. - * But we need to allocate >= MAX_ORDER hstates here early to still - * use the bootmem allocator. - */ - if (max_hstate > 0 && parsed_hstate->order >= MAX_ORDER) - hugetlb_init_hstate(parsed_hstate); - - return 1; -} -__setup("hugepages=", hugetlb_setup); - -static unsigned int cpuset_mems_nr(unsigned int *array) -{ - int node; - unsigned int nr = 0; - - for_each_node_mask(node, cpuset_current_mems_allowed) - nr += array[node]; - - return nr; -} - #ifdef CONFIG_SYSCTL #ifdef CONFIG_HIGHMEM static void try_to_free_low(struct hstate *h, unsigned long count) @@ -843,6 +775,254 @@ out: return ret; } +#ifdef CONFIG_SYSFS +#define to_hstate_attr(n) container_of(n, struct hstate_attribute, attr) +#define to_hstate(n) container_of(n, struct hstate, kobj) + +struct hstate_attribute { + struct attribute attr; + ssize_t (*show)(struct hstate *h, char *buf); + ssize_t (*store)(struct hstate *h, const char *buf, size_t count); +}; + +#define HSTATE_ATTR_RO(_name) \ + static struct hstate_attribute _name##_attr = __ATTR_RO(_name) + +#define HSTATE_ATTR(_name) \ + static struct hstate_attribute _name##_attr = \ + __ATTR(_name, 0644, _name##_show, _name##_store) + +static ssize_t nr_hugepages_show(struct hstate *h, char *buf) +{ + return sprintf(buf, "%lu\n", h->nr_huge_pages); +} +static ssize_t nr_hugepages_store(struct hstate *h, const char *buf, + size_t count) +{ + int tmp, err; + unsigned long input; + err = strict_strtoul(buf, 10, &input); + if (err) + return 0; + + h->max_huge_pages = set_max_huge_pages(h, input, &tmp); + max_huge_pages[h - hstates] = h->max_huge_pages; + return count; +} +HSTATE_ATTR(nr_hugepages); + +static ssize_t nr_overcommit_hugepages_show(struct hstate *h, char *buf) +{ + return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); +} +static ssize_t nr_overcommit_hugepages_store(struct hstate *h, const char *buf, + size_t count) +{ + unsigned long input; + int err; + + err = strict_strtoul(buf, 10, &input); + if (err) + return 0; + + spin_lock(&hugetlb_lock); + h->nr_overcommit_huge_pages = input; + sysctl_overcommit_huge_pages[h - hstates] = h->nr_overcommit_huge_pages; + spin_unlock(&hugetlb_lock); + return count; +} +HSTATE_ATTR(nr_overcommit_hugepages); + +static ssize_t free_hugepages_show(struct hstate *h, char *buf) +{ + return sprintf(buf, "%lu\n", h->free_huge_pages); +} +HSTATE_ATTR_RO(free_hugepages); + +static ssize_t resv_hugepages_show(struct hstate *h, char *buf) +{ + return sprintf(buf, "%lu\n", h->resv_huge_pages); +} +HSTATE_ATTR_RO(resv_hugepages); + +static ssize_t surplus_hugepages_show(struct hstate *h, char *buf) +{ + return sprintf(buf, "%lu\n", h->surplus_huge_pages); +} +HSTATE_ATTR_RO(surplus_hugepages); + +static struct kset *hstate_kset; + +static struct attribute *hstate_attrs[] = { + &nr_hugepages_attr.attr, + &nr_overcommit_hugepages_attr.attr, + &free_hugepages_attr.attr, + &resv_hugepages_attr.attr, + &surplus_hugepages_attr.attr, +}; + +static struct attribute_group hstate_attr_group = { + .attrs = hstate_attrs, +}; + +static ssize_t hstate_attr_show(struct kobject *kobj, + struct attribute *attr, + char *buf) +{ + struct hstate_attribute *attribute; + struct hstate *h; + int err; + + attribute = to_hstate_attr(attr); + h = to_hstate(kobj); + + if (!attribute->show) + return -EIO; + + err = attribute->show(h, buf); + + return err; +} + +static ssize_t hstate_attr_store(struct kobject *kobj, + struct attribute *attr, + const char *buf, size_t len) +{ + struct hstate_attribute *attribute; + struct hstate *h; + int err; + + attribute = to_hstate_attr(attr); + h = to_hstate(kobj); + + if (!attribute->store) + return -EIO; + + err = attribute->store(h, buf, len); + + return err; +} + +static struct sysfs_ops hstate_sysfs_ops = { + .show = hstate_attr_show, + .store = hstate_attr_store, +}; + +static struct kobj_type hstate_ktype = { + .sysfs_ops = &hstate_sysfs_ops, +}; + +static int __init hugetlb_sysfs_add_hstate(struct hstate *h) +{ + int err; + h->kobj.kset = hstate_kset; + err = kobject_init_and_add(&h->kobj, &hstate_ktype, NULL, h->name); + if (err) { + kobject_put(&h->kobj); + return err; + } + err = sysfs_create_group(&h->kobj, &hstate_attr_group); + if (err) + return err; + return 0; +} + +static void __init hugetlb_sysfs_init(void) +{ + struct hstate *h; + int err; + + hstate_kset = kset_create_and_add("hugepages", NULL, kernel_kobj); + if (!hstate_kset) + return; + + for_each_hstate(h) { + err = hugetlb_sysfs_add_hstate(h); + if (err) + printk(KERN_ERR "Hugetlb: Unable to add hstate %s", + h->name); + } +} +#else +static void __init hugetlb_sysfs_init(void) +{ +} +#endif + +static int __init hugetlb_init(void) +{ + BUILD_BUG_ON(HPAGE_SHIFT == 0); + + if (!size_to_hstate(HPAGE_SIZE)) { + huge_add_hstate(HUGETLB_PAGE_ORDER); + parsed_hstate->max_huge_pages = default_hstate_resv; + } + + hugetlb_init_hstates(); + + gather_bootmem_prealloc(); + + report_hugepages(); + + hugetlb_sysfs_init(); + + return 0; +} +module_init(hugetlb_init); + +/* Should be called on processing a hugepagesz=... option */ +void __init huge_add_hstate(unsigned order) +{ + struct hstate *h; + if (size_to_hstate(PAGE_SIZE << order)) { + printk("hugepagesz= specified twice, ignoring\n"); + return; + } + BUG_ON(max_hstate >= HUGE_MAX_HSTATE); + BUG_ON(order < HPAGE_SHIFT - PAGE_SHIFT); + h = &hstates[max_hstate++]; + h->order = order; + h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); + hugetlb_init_hstate(h); + parsed_hstate = h; +} + +static int __init hugetlb_setup(char *s) +{ + unsigned long *mhp; + + if (!max_hstate) + mhp = &default_hstate_resv; + else + mhp = &parsed_hstate->max_huge_pages; + + if (sscanf(s, "%lu", mhp) <= 0) + *mhp = 0; + + /* + * Global state is always initialized later in hugetlb_init. + * But we need to allocate >= MAX_ORDER hstates here early to still + * use the bootmem allocator. + */ + if (max_hstate > 0 && parsed_hstate->order >= MAX_ORDER) + hugetlb_init_hstate(parsed_hstate); + + return 1; +} +__setup("hugepages=", hugetlb_setup); + +static unsigned int cpuset_mems_nr(unsigned int *array) +{ + int node; + unsigned int nr = 0; + + for_each_node_mask(node, cpuset_current_mems_allowed) + nr += array[node]; + + return nr; +} + + int hugetlb_sysctl_handler(struct ctl_table *table, int write, struct file *file, void __user *buffer, size_t *length, loff_t *ppos) -- Nishanth Aravamudan IBM Linux Technology Center -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org