From: Liu Ye <liuyerd@163.com>
To: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev,
shakeel.butt@linux.dev, muchun.song@linux.dev
Cc: akpm@linux-foundation.org, willy@infradead.org, david@redhat.com,
svetly.todorov@memverge.com, vbabka@suse.cz,
ran.xiaokai@zte.com.cn, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org,
linux-mm@kvack.org, Liu Ye <liuye@kylinos.cn>
Subject: Re: [PATCH v4] fs/proc/page: Refactoring to reduce code duplication.
Date: Mon, 24 Mar 2025 11:25:08 +0800 [thread overview]
Message-ID: <c21dbc6b-3f1b-4015-9aee-44979ef0233e@163.com> (raw)
In-Reply-To: <20250318063226.223284-1-liuyerd@163.com>
Friendly ping.
在 2025/3/18 14:32, Liu Ye 写道:
> From: Liu Ye <liuye@kylinos.cn>
>
> 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.
>
> Signed-off-by: Liu Ye <liuye@kylinos.cn>
>
> ---
> V4 : Update code remake patch.
> V3 : Add a stub for page_cgroup_ino and remove the #ifdef CONFIG_MEMCG.
> V2 : Use an enumeration to indicate the operation to be performed
> to avoid passing functions.
> ---
> ---
> fs/proc/page.c | 161 +++++++++++++------------------------
> include/linux/memcontrol.h | 4 +
> 2 files changed, 58 insertions(+), 107 deletions(-)
>
> diff --git a/fs/proc/page.c b/fs/proc/page.c
> index 23fc771100ae..999af26c7298 100644
> --- a/fs/proc/page.c
> +++ b/fs/proc/page.c
> @@ -22,6 +22,12 @@
> #define KPMMASK (KPMSIZE - 1)
> #define KPMBITS (KPMSIZE * BITS_PER_BYTE)
>
> +enum kpage_operation {
> + KPAGE_FLAGS,
> + KPAGE_COUNT,
> + KPAGE_CGROUP,
> +};
> +
> static inline unsigned long get_max_dump_pfn(void)
> {
> #ifdef CONFIG_SPARSEMEM
> @@ -37,19 +43,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 *page;
> unsigned long src = *ppos;
> unsigned long pfn;
> ssize_t ret = 0;
> + u64 info;
>
> pfn = src / KPMSIZE;
> if (src & KPMMASK || count & KPMMASK)
> @@ -59,24 +63,34 @@ 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) {
> - struct folio *folio = page_folio(page);
>
> - if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT))
> - mapcount = folio_precise_page_mapcount(folio, page);
> - else
> - mapcount = folio_average_page_mapcount(folio);
> - }
> -
> - if (put_user(mapcount, out)) {
> + if (page) {
> + switch (op) {
> + case KPAGE_FLAGS:
> + info = stable_page_flags(page);
> + break;
> + case KPAGE_COUNT:
> + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT))
> + info = folio_precise_page_mapcount(page_folio(page), page);
> + else
> + info = folio_average_page_mapcount(page_folio(page));
> + break;
> + case KPAGE_CGROUP:
> + info = page_cgroup_ino(page);
> + break;
> + default:
> + info = 0;
> + break;
> + }
> + } else
> + info = 0;
> +
> + if (put_user(info, out)) {
> ret = -EFAULT;
> break;
> }
> @@ -94,17 +108,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)
> {
> @@ -225,47 +245,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 = {
> @@ -276,53 +266,10 @@ 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,
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index 53364526d877..5264d148bdd9 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -1793,6 +1793,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)
next prev parent reply other threads:[~2025-03-24 3:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-18 6:32 [PATCH v4] fs/proc/page: Refactoring to reduce code duplication Liu Ye
2025-03-18 8:44 ` David Hildenbrand
2025-03-24 3:25 ` Liu Ye [this message]
2025-03-24 11:55 ` David Hildenbrand
2025-04-07 1:39 ` Liu Ye
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c21dbc6b-3f1b-4015-9aee-44979ef0233e@163.com \
--to=liuyerd@163.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=david@redhat.com \
--cc=hannes@cmpxchg.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=liuye@kylinos.cn \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=ran.xiaokai@zte.com.cn \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=svetly.todorov@memverge.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).