kpagecount_read(), kpageflags_read(), and kpagecgroup_read() have duplicate code. This patch moves it into a common function. Signed-off-by: Toshiki Fukasawa <t-fukasawa@xxxxxxxxxxxxx> --- fs/proc/page.c | 133 +++++++++++++++++++++------------------------------------ 1 file changed, 48 insertions(+), 85 deletions(-) diff --git a/fs/proc/page.c b/fs/proc/page.c index 7c952ee..a49b638 100644 --- a/fs/proc/page.c +++ b/fs/proc/page.c @@ -21,20 +21,19 @@ #define KPMMASK (KPMSIZE - 1) #define KPMBITS (KPMSIZE * BITS_PER_BYTE) -/* /proc/kpagecount - an array exposing page counts - * - * Each entry is a u64 representing the corresponding - * physical page count. +typedef u64 (*read_page_data_fn_t)(struct page *page); + +/* + * This is general function to read various data on pages. */ -static ssize_t kpagecount_read(struct file *file, char __user *buf, - size_t count, loff_t *ppos) +static ssize_t kpage_common_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos, read_page_data_fn_t read_fn) { u64 __user *out = (u64 __user *)buf; struct page *ppage; unsigned long src = *ppos; unsigned long pfn; ssize_t ret = 0; - u64 pcount; pfn = src / KPMSIZE; count = min_t(size_t, count, (max_pfn * KPMSIZE) - src); @@ -48,12 +47,7 @@ static ssize_t kpagecount_read(struct file *file, char __user *buf, */ ppage = pfn_to_online_page(pfn); - if (!ppage || PageSlab(ppage) || page_has_type(ppage)) - pcount = 0; - else - pcount = page_mapcount(ppage); - - if (put_user(pcount, out)) { + if (put_user(read_fn(ppage), out)) { ret = -EFAULT; break; } @@ -71,6 +65,30 @@ static ssize_t kpagecount_read(struct file *file, char __user *buf, return ret; } +/* /proc/kpagecount - an array exposing page counts + * + * Each entry is a u64 representing the corresponding + * physical page count. + */ + +static u64 page_count_data(struct page *page) +{ + u64 pcount; + + if (!page || PageSlab(page) || page_has_type(page)) + pcount = 0; + else + pcount = page_mapcount(page); + + return pcount; +} + +static ssize_t kpagecount_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + return kpage_common_read(file, buf, count, ppos, page_count_data); +} + static const struct file_operations proc_kpagecount_operations = { .llseek = mem_lseek, .read = kpagecount_read, @@ -203,43 +221,15 @@ u64 stable_page_flags(struct page *page) return u; }; +static u64 page_flags_data(struct page *page) +{ + return stable_page_flags(page); +} + static ssize_t kpageflags_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { - u64 __user *out = (u64 __user *)buf; - struct page *ppage; - unsigned long src = *ppos; - unsigned long pfn; - ssize_t ret = 0; - - pfn = src / KPMSIZE; - count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); - if (src & KPMMASK || count & KPMMASK) - return -EINVAL; - - while (count > 0) { - /* - * TODO: ZONE_DEVICE support requires to identify - * memmaps that were actually initialized. - */ - ppage = pfn_to_online_page(pfn); - - if (put_user(stable_page_flags(ppage), out)) { - ret = -EFAULT; - break; - } - - pfn++; - out++; - count -= KPMSIZE; - - cond_resched(); - } - - *ppos += (char __user *)out - buf; - if (!ret) - ret = (char __user *)out - buf; - return ret; + return kpage_common_read(file, buf, count, ppos, page_flags_data); } static const struct file_operations proc_kpageflags_operations = { @@ -248,49 +238,22 @@ static ssize_t kpageflags_read(struct file *file, char __user *buf, }; #ifdef CONFIG_MEMCG -static ssize_t kpagecgroup_read(struct file *file, char __user *buf, - size_t count, loff_t *ppos) +static u64 page_cgroup_data(struct page *page) { - u64 __user *out = (u64 __user *)buf; - struct page *ppage; - unsigned long src = *ppos; - unsigned long pfn; - ssize_t ret = 0; u64 ino; - pfn = src / KPMSIZE; - count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src); - if (src & KPMMASK || count & KPMMASK) - return -EINVAL; - - while (count > 0) { - /* - * TODO: ZONE_DEVICE support requires to identify - * memmaps that were actually initialized. - */ - ppage = pfn_to_online_page(pfn); - - if (ppage) - ino = page_cgroup_ino(ppage); - else - ino = 0; - - if (put_user(ino, out)) { - ret = -EFAULT; - break; - } - - pfn++; - out++; - count -= KPMSIZE; + if (page) + ino = page_cgroup_ino(page); + else + ino = 0; - cond_resched(); - } + return ino; +} - *ppos += (char __user *)out - buf; - if (!ret) - ret = (char __user *)out - buf; - return ret; +static ssize_t kpagecgroup_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + return kpage_common_read(file, buf, count, ppos, page_cgroup_data); } static const struct file_operations proc_kpagecgroup_operations = { -- 1.8.3.1