fs/proc/page: Refactoring to reduce code duplication.
Subsystems (2)
PATCH 1/1 fs/proc/page: Refactoring to reduce code duplication.
From: Liu Ye <[email protected]>
The function kpageflags_read and kpagecgroup_read is quite similar to kpagecount_read. Consider refactoring common code into a helper function to reduce code duplication.
--- fs/proc/page.c | 158 ++++++++++++++++--------------------------------- 1 file changed, 50 insertions(+), 108 deletions(-)
fs/proc/page.c +50 -108
@@ -37,19 +37,17 @@ static inline unsigned long get_max_dump_pfn(void)#endif } -/* /proc/kpagecount - an array exposing page mapcounts -* -* Each entry is a u64 representing the corresponding -* physical page mapcount. -*/ -static ssize_t kpagecount_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) +static ssize_t kpage_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos, +u64 (*get_page_info)(struct page *)) {const unsigned long max_dump_pfn =get_max_dump_pfn ();u64 __user *out = (u64 __user *)buf; +struct page *ppage;unsigned long src = *ppos;unsigned long pfn;ssize_t ret =0 ; +u64 info; pfn = src /KPMSIZE ;if (src &KPMMASK || count &KPMMASK )
@@ -59,19 +57,14 @@ static ssize_t kpagecount_read(struct file *file, char __user *buf, count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src);while (count >0 ) { -struct page *page; -u64 mapcount =0 ; - -/* -* TODO: ZONE_DEVICE support requires to identify -* memmaps that were actually initialized. -*/ - page =pfn_to_online_page (pfn); -if (page) - mapcount =folio_precise_page_mapcount (page_folio (page), - page); - -if (put_user (mapcount, out)) { + ppage =pfn_to_online_page (pfn); + +if (ppage) + info =get_page_info (ppage); +else + info =0 ; + +if (put_user (info, out)) { ret = -EFAULT ;break ; }
@@ -89,17 +82,28 @@ static ssize_t kpagecount_read(struct file *file, char __user *buf,return ret; } +static u64 get_page_count (struct page *page) +{ +return folio_precise_page_mapcount (page_folio (page), page); +} + +/* /proc/kpagecount - an array exposing page mapcounts +* +* Each entry is a u64 representing the corresponding +* physical page mapcount. +*/ +static ssize_t kpagecount_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos) +{ +return kpage_read (file, buf, count, ppos, get_page_count); +} +static const struct proc_ops kpagecount_proc_ops = { .proc_flags =PROC_ENTRY_PERMANENT , .proc_lseek = mem_lseek, .proc_read = kpagecount_read, }; -/* /proc/kpageflags - an array exposing page flags -* -* Each entry is a u64 representing the corresponding -* physical page flags. -*/ static inline u64 kpf_copy_bit (u64 kflags,int ubit,int kbit) {
@@ -220,47 +224,22 @@ u64 stable_page_flags(const struct page *page)#endif return u; -}; +} -static ssize_t kpageflags_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) +static u64 get_page_flags (struct page *page) { -const unsigned long max_dump_pfn =get_max_dump_pfn (); -u64 __user *out = (u64 __user *)buf; -unsigned long src = *ppos; -unsigned long pfn; -ssize_t ret =0 ; - - pfn = src /KPMSIZE ; -if (src &KPMMASK || count &KPMMASK ) -return -EINVAL ; -if (src >= max_dump_pfn *KPMSIZE ) -return 0 ; - count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src); - -while (count >0 ) { -/* -* TODO: ZONE_DEVICE support requires to identify -* memmaps that were actually initialized. -*/ -struct page *page =pfn_to_online_page (pfn); - -if (put_user (stable_page_flags (page), out)) { - ret = -EFAULT ; -break ; - } - - pfn++; - out++; - count -=KPMSIZE ; - -cond_resched (); - } +return stable_page_flags (page); +} - *ppos += (char __user *)out - buf; -if (!ret) - ret = (char __user *)out - buf; -return ret; +/* /proc/kpageflags - an array exposing page flags +* +* Each entry is a u64 representing the corresponding +* physical page flags. +*/ +static ssize_t kpageflags_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos) +{ +return kpage_read (file, buf, count, ppos, get_page_flags); }static const struct proc_ops kpageflags_proc_ops = {
@@ -270,54 +249,17 @@ static const struct proc_ops kpageflags_proc_ops = { };#ifdef CONFIG_MEMCG -static ssize_t kpagecgroup_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) -{ -const unsigned long max_dump_pfn =get_max_dump_pfn (); -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 ; -if (src &KPMMASK || count &KPMMASK ) -return -EINVAL ; -if (src >= max_dump_pfn *KPMSIZE ) -return 0 ; - count = min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src); - -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 ; - -cond_resched (); - } - - *ppos += (char __user *)out - buf; -if (!ret) - ret = (char __user *)out - buf; -return ret; +static u64 get_page_cgroup (struct page *page) +{ +return page_cgroup_ino (page); } +static ssize_t kpagecgroup_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos) +{ +return kpage_read (file, buf, count, ppos, get_page_cgroup); +}static const struct proc_ops kpagecgroup_proc_ops = { .proc_flags =PROC_ENTRY_PERMANENT , .proc_lseek = mem_lseek,
Signature · 1 line
2.25.1
Quoted 19 lines · Liu Ye
From: Liu Ye <[email protected]>
The function
kpageflags_readandkpagecgroup_readis quite similar tokpagecount_read. Consider refactoring common code into a helper function to reduce code duplication.Signed-off-by Liu Ye <[email protected]>--- fs/proc/page.c | 158 ++++++++++++++++--------------------------------- 1 file changed, 50 insertions(+), 108 deletions(-)fs/proc/page.c +0
-0@@ -37,19 +37,17 @@ static inline unsigned long get_max_dump_pfn(void)#endif }
fs/proc/page.c static inline unsigned long get_max_dump_pfn(void) · line 42
Quoted 10 lines
fs/proc/page.c +3
-7-/* /proc/kpagecount - an array exposing page mapcounts -* -* Each entry is a u64 representing the corresponding -* physical page mapcount. -*/ -static ssize_t kpagecount_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) +static ssize_t kpage_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos, +u64 (*get_page_info)(struct page *))
Can we just indicate using an enum which operation to perform, so we can avoid having+passing these functions?
Signature · 3 lines
Cheers, David / dhildenb
Quoted 23 lines · David Hildenbrand
Quoted 22 lines · Liu Ye
From: Liu Ye <[email protected]>
The function
kpageflags_readandkpagecgroup_readis quite similar tokpagecount_read. Consider refactoring common code into a helper function to reduce code duplication.Signed-off-by Liu Ye <[email protected]>---
fs/proc/page.c| 158 ++++++++++++++++---------------------------------1 file changed, 50 insertions(+), 108 deletions(-)fs/proc/page.c +0
-0@@ -37,19 +37,17 @@ static inline unsigned long get_max_dump_pfn(void) #endif }-/*
/proc/kpagecount- an array exposing page mapcounts
- *
- * Each entry is a u64 representing the corresponding
- * physical page mapcount.
Quoted 9 lines
fs/proc/page.c static inline unsigned long get_max_dump_pfn(void) · line 42
Quoted 6 lines
- */
fs/proc/page.c +3
-2-static ssize_t kpagecount_read (struct file *file,char __user *buf, - size_t count, loff_t *ppos) +static ssize_t kpage_read (struct file *file,char __user *buf, + size_t count, loff_t *ppos, + u64 (*get_page_info)(struct page *))Can we just indicate using an enum which operation to perform, so we can avoid having+passing these functions?
Like this? Good idea, I'll send a new patch later.
enum kpage_operation {
KPAGE_FLAGS,
KPAGE_COUNT,
KPAGE_CGROUP,
};
static u64 get_page_info(struct page *page, enum kpage_operation op) {
switch (op) {
case KPAGE_FLAGS:
return stable_page_flags(page);
case KPAGE_COUNT:
return page_count(page);
case KPAGE_CGROUP:
return page_cgroup_ino(page);
default:
return 0;
}
}
static ssize_t kpageflags_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
return kpage_read(file, buf, count, ppos, KPAGE_FLAGS); }
static ssize_t kpagecount_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
return kpage_read(file, buf, count, ppos, KPAGE_COUNT); }
static ssize_t kpagecgroup_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
return kpage_read(file, buf, count, ppos, KPAGE_CGROUP); }
static ssize_t kpage_read(struct file *file, char __user *buf, size_t count, loff_t *ppos, enum kpage_operation op)
{
...
info = get_page_info(ppage, op);
...
}
Quoted 42 lines · Liu Ye
Quoted 23 lines · David Hildenbrand
Quoted 22 lines · Liu Ye
From: Liu Ye <[email protected]>
The function
kpageflags_readandkpagecgroup_readis quite similar tokpagecount_read. Consider refactoring common code into a helper function to reduce code duplication.Signed-off-by Liu Ye <[email protected]>--- fs/proc/page.c | 158 ++++++++++++++++--------------------------------- 1 file changed, 50 insertions(+), 108 deletions(-)fs/proc/page.c +0
-3@@ -37,19 +37,17 @@ static inline unsigned long get_max_dump_pfn(void) #endif } -/* /proc/kpagecount - an array exposing page mapcounts -* -* Each entry is a u64 representing the corresponding -* physical page mapcount. Quoted 9 lines
fs/proc/page.c static inline unsigned long get_max_dump_pfn(void) · line 42
Quoted 6 lines
fs/proc/page.c +3
-3-*/ -static ssize_t kpagecount_read (struct file *file,char __user *buf, - size_t count, loff_t *ppos) +static ssize_t kpage_read (struct file *file,char __user *buf, + size_t count, loff_t *ppos, + u64 (*get_page_info)(struct page *))Can we just indicate using an enum which operation to perform, so we can avoid having+passing these functions?
Like this? Good idea, I'll send a new patch later.
enum
kpage_operation{
KPAGE_FLAGS,
KPAGE_COUNT,
KPAGE_CGROUP,
};
Quoted 13 lines
static u64
get_page_info(struct page *page, enum kpage_operation op){
switch (op) {
caseKPAGE_FLAGS:
returnstable_page_flags(page);
caseKPAGE_COUNT:
returnpage_count(page);
caseKPAGE_CGROUP:
returnpage_cgroup_ino(page);
default:
return 0;
}
}
Likely it's best to inline get_page_info() into kpage_read() to just get rid of it.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 58 lines · David Hildenbrand
Quoted 42 lines · Liu Ye
Quoted 23 lines · David Hildenbrand
Quoted 22 lines · Liu Ye
From: Liu Ye <[email protected]>
The function
kpageflags_readandkpagecgroup_readis quite similar tokpagecount_read. Consider refactoring common code into a helper function to reduce code duplication.Signed-off-by Liu Ye <[email protected]>---
fs/proc/page.c| 158 ++++++++++++++++---------------------------------1 file changed, 50 insertions(+), 108 deletions(-)fs/proc/page.c +0
-0@@ -37,19 +37,17 @@ static inline unsigned long get_max_dump_pfn(void) #endif }-/*
/proc/kpagecount- an array exposing page mapcounts
- *
- * Each entry is a u64 representing the corresponding
- * physical page mapcount.Quoted 9 lines
fs/proc/page.c static inline unsigned long get_max_dump_pfn(void) · line 42
Quoted 6 lines
- */
fs/proc/page.c +3
-2-static ssize_t kpagecount_read (struct file *file,char __user *buf, - size_t count, loff_t *ppos) +static ssize_t kpage_read (struct file *file,char __user *buf, + size_t count, loff_t *ppos, + u64 (*get_page_info)(struct page *))Can we just indicate using an enum which operation to perform, so we can avoid having+passing these functions?
Like this? Good idea, I'll send a new patch later.
enum
kpage_operation{
KPAGE_FLAGS,
KPAGE_COUNT,
KPAGE_CGROUP,
};Quoted 13 lines
static u64
get_page_info(struct page *page, enum kpage_operation op){
switch (op) {
caseKPAGE_FLAGS:
returnstable_page_flags(page);
caseKPAGE_COUNT:
returnpage_count(page);
caseKPAGE_CGROUP:
returnpage_cgroup_ino(page);
default:
return 0;
}
}
Quoted 3 lines
Likely it's best to inline
get_page_info()intokpage_read()to just get rid of it.
Thank you for your suggestion. I agree and will make the changes accordingly.
Thanks,
Liu Ye