fs/proc/page: Refactoring to reduce code duplication.
Subsystems (3)
PATCH v2 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.
---
V2 : Use an enumeration to indicate the operation to be performed to avoid passing functions.
---
--- fs/proc/page.c | 166 +++++++++++++++++-------------------------------- 1 file changed, 58 insertions(+), 108 deletions(-)
fs/proc/page.c +58 -108
@@ -22,6 +22,14 @@#define KPMMASK (KPMSIZE - 1)#define KPMBITS (KPMSIZE * BITS_PER_BYTE) +enum kpage_operation { +KPAGE_FLAGS , +KPAGE_COUNT , +#ifdef CONFIG_MEMCG +KPAGE_CGROUP , +#endif +}; +static inline unsigned long get_max_dump_pfn (void ) {#ifdef CONFIG_SPARSEMEM
@@ -37,19 +45,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, +enum kpage_operation op) {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 +65,29 @@ 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) { +switch (op) { +case KPAGE_FLAGS : + info =stable_page_flags (ppage); +break ; +case KPAGE_COUNT : + info =folio_precise_page_mapcount (page_folio (ppage), ppage); +break ; +#ifdef CONFIG_MEMCG +case KPAGE_CGROUP : + info =page_cgroup_ino (ppage); +break ; +#endif +default : + info =0 ; +break ; + } + }else + info =0 ; + +if (put_user (info, out)) { ret = -EFAULT ;break ; }
@@ -89,17 +105,23 @@ static ssize_t kpagecount_read(struct file *file, char __user *buf,return ret; } +/* /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,KPAGE_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 +242,17 @@ u64 stable_page_flags(const struct page *page)#endif return u; -}; +} +/* /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) +size_t count,loff_t *ppos) { -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 (); - } - - *ppos += (char __user *)out - buf; -if (!ret) - ret = (char __user *)out - buf; -return ret; +return kpage_read (file, buf, count, ppos,KPAGE_FLAGS ); }static const struct proc_ops kpageflags_proc_ops = {
@@ -270,54 +262,12 @@ 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) +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; +return kpage_read (file, buf, count, ppos,KPAGE_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 82 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]>---
V2 : Use an enumeration to indicate the operation to be performed to avoid passing functions.
------ fs/proc/page.c | 166 +++++++++++++++++-------------------------------- 1 file changed, 58 insertions(+), 108 deletions(-)fs/proc/page.c +18
-20@@ -22,6 +22,14 @@#define KPMMASK (KPMSIZE - 1)#define KPMBITS (KPMSIZE * BITS_PER_BYTE) +enum kpage_operation { +KPAGE_FLAGS , +KPAGE_COUNT , +#ifdef CONFIG_MEMCG +KPAGE_CGROUP , +#endif +}; +static inline unsigned long get_max_dump_pfn (void ) {#ifdef CONFIG_SPARSEMEM @@ -37,19 +45,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, +enum kpage_operation op) {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 +65,29 @@ 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) { +switch (op) { +case KPAGE_FLAGS :
fs/proc/page.c static ssize_t kpagecount_read(struct file *file, char __user *buf, · line 82
Quoted 10 lines
fs/proc/page.c +10
-0+ info =stable_page_flags (ppage); +break ; +case KPAGE_COUNT : + info =folio_precise_page_mapcount (page_folio (ppage), ppage); +break ; +#ifdef CONFIG_MEMCG +case KPAGE_CGROUP : + info =page_cgroup_ino (ppage); +break ; +#endif
In general, LGTM.
I do wonder if we should just get rid of the two "#ifdef CONFIG_MEMCG" by adding a stub for page_cgroup_ino().
include/linux/memcontrol.h +5 -0
@@ -1788,6 +1788,11 @@ static inline void count_objcg_events(struct obj_cgroup *objcg, { } +static inline ino_t page_cgroup_ino (struct page *page) +{ +return 0 ; +} +#endif /* CONFIG_MEMCG */ #if defined(CONFIG_MEMCG ) && defined(CONFIG_ZSWAP )
Signature · 3 lines
Cheers, David / dhildenb
Quoted 108 lines · David Hildenbrand
Quoted 78 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]>---
V2 : Use an enumeration to indicate the operation to be performed to avoid passing functions.
---
---
fs/proc/page.c| 166 +++++++++++++++++--------------------------------1 file changed, 58 insertions(+), 108 deletions(-)fs/proc/page.c +7
-0@@ -22,6 +22,14 @@ #define KPMMASK (KPMSIZE - 1) #define KPMBITS (KPMSIZE * BITS_PER_BYTE) +enum kpage_operation { + KPAGE_FLAGS, + KPAGE_COUNT, +#ifdef CONFIG_MEMCG + KPAGE_CGROUP, +#endif +}; + static inline unsigned long get_max_dump_pfn(void) { #ifdef CONFIG_SPARSEMEM@@ -37,19 +45,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.
- */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, + enum kpage_operation op){
const unsigned longmax_dump_pfn=get_max_dump_pfn(); u64__user*out = (u64__user*)buf;
+ struct page *ppage;
unsigned long src = *ppos;
unsigned long pfn;
ssize_tret = 0;
+ u64 info;
pfn = src / KPMSIZE;
if (src & KPMMASK || count & KPMMASK)fs/proc/page.c +5
-13@@ -59,19 +65,29 @@ 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) { + switch (op) { + case KPAGE_FLAGS:fs/proc/page.c static ssize_t kpagecount_read(struct file *file, char __user *buf, · line 81
Quoted 10 lines
fs/proc/page.c +10
-0+ info = stable_page_flags(ppage); + break; + case KPAGE_COUNT: + info = folio_precise_page_mapcount(page_folio(ppage), ppage); + break; +#ifdef CONFIG_MEMCG + case KPAGE_CGROUP: + info = page_cgroup_ino(ppage); + break; +#endif In general, LGTM.
I do wonder if we should just get rid of the two "#ifdef
CONFIG_MEMCG" by adding a stub forpage_cgroup_ino().include/linux/memcontrol.h +0
-0@@ -1788,6 +1788,11 @@ static inline void count_objcg_events(struct obj_cgroup *objcg, { }include/linux/memcontrol.h +5
-0+static inline ino_t page_cgroup_ino (struct page *page) +{ + return 0; +} +
Quoted 5 lines
#endif /*
CONFIG_MEMCG*/
#ifdefined(CONFIG_MEMCG)&&defined(CONFIG_ZSWAP)
Agreed. I’ll add a stub for page_cgroup_ino() and remove the #ifdef CONFIG_MEMCG.
like this.
fs/proc/page.c +0 -0
@@ -25,9 +25,7 @@
enum kpage_operation {
KPAGE_FLAGS,
KPAGE_COUNT,
-#ifdef CONFIG_MEMCG
KPAGE_CGROUP,
-#endif
};
static inline unsigned long get_max_dump_pfn(void)
@@ -75,11 +73,9 @@ static ssize_t kpage_read(struct file *file, char __user *buf, case KPAGE_COUNT:
info = folio_precise_page_mapcount(page_folio(ppage), ppage); break;
-#ifdef CONFIG_MEMCG
case KPAGE_CGROUP:
info = page_cgroup_ino(ppage);
break;
-#endif
default:
info = 0;
break;
@@ -262,7 +258,6 @@ 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)
{
include/linux/memcontrol.h +0 -0
@@ -1794,6 +1794,10 @@ static inline void count_objcg_events(struct obj_cgroup *objcg,
{
}
+static inline ino_t page_cgroup_ino(struct page *page) +{
+ return 0;
+}
#endif /* CONFIG_MEMCG */
#if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)