From nobody Wed Dec 17 12:06:32 2025 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CE90E1E4B2; Tue, 18 Mar 2025 02:52:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742266361; cv=none; b=EUTkQ3dqMb2JmCUr6EmTYWlsOS/w6yL5Q9/m3pSQ718pXNCcddn3aMl/F7Uh08CG1crObjyKMUzK3RPCi3ax+QoDtetlCB9v9AccprCZ1STBmDWFslAmIiqYETzhrQbJPUxHFz5B27GlB5egeYd03zIuIdkixBiTCX7ylLWlQ0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742266361; c=relaxed/simple; bh=Vt95DbqeCyIlJ8MU6h+2HPbVsq8NDYSfMFUjXB8VsqU=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=EClNZ5L1Cq13nHFPmLFL0oAPUnAAt0gEKL59l7Q+/QyLTTg7Pk/d17apUe+hZbxOkJoLMjWH+fd+mIXelJ6A6oQGTSyVy5AZFiNAF2BH4HKI9/1GhDGKA38pqu9I3q3R72EZafkqEVKUtr5tntAIt+xMR5tDVuyjN9pAzmz+9wg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=h0YYfd/B; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="h0YYfd/B" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=twhKD HS2bhTthfmdF+zbTPqo8rT9kGcPJYWcAYK7wrg=; b=h0YYfd/BGw6VDCffz0AxD qRiU04m9ROEDQ84zETrC6KBUQ9uIVcaFYtwn/4W86Ij7XZoU9RVlGnMjtB2ZF7/+ BlPdxrlKcvzo4W8JGIKW4dsvU9uC/jCc3huzR7SSQd/6gyAVvbfzcdqpXUM/MJLQ bhCpOsOXk6QXNm8gCMhrMA= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g0-0 (Coremail) with SMTP id _____wD3WJfE39hnuBWKAA--.1949S2; Tue, 18 Mar 2025 10:51:50 +0800 (CST) From: Liu Ye 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, liuyerd@163.com, 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 Subject: [PATCH v3] fs/proc/page: Refactoring to reduce code duplication. Date: Tue, 18 Mar 2025 10:51:38 +0800 Message-Id: <20250318025138.170876-1-liuyerd@163.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wD3WJfE39hnuBWKAA--.1949S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxKr48XF43AF43tFWfurykXwb_yoWxXrW8pF s8GF4jyrs5W3s0kw1xJ398Zas8G3s3Aa1Yy3y7G34fZa47JrnakFySyFnYvFyxGryUZr1U ua909ry3CFWjyaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j089NUUUUU= X-CM-SenderInfo: 5olx5vlug6il2tof0z/xtbBMRAUTGfY3fg8AwAAsc Content-Type: text/plain; charset="utf-8" From: Liu Ye 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 --- 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, 57 insertions(+), 108 deletions(-) diff --git a/fs/proc/page.c b/fs/proc/page.c index a55f5acefa97..cbadbf9568a1 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) =20 +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 } =20 -/* /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 =3D get_max_dump_pfn(); u64 __user *out =3D (u64 __user *)buf; + struct page *ppage; unsigned long src =3D *ppos; unsigned long pfn; ssize_t ret =3D 0; + u64 info; =20 pfn =3D src / KPMSIZE; if (src & KPMMASK || count & KPMMASK) @@ -59,19 +63,27 @@ static ssize_t kpagecount_read(struct file *file, char = __user *buf, count =3D min_t(unsigned long, count, (max_dump_pfn * KPMSIZE) - src); =20 while (count > 0) { - struct page *page; - u64 mapcount =3D 0; - - /* - * TODO: ZONE_DEVICE support requires to identify - * memmaps that were actually initialized. - */ - page =3D pfn_to_online_page(pfn); - if (page) - mapcount =3D folio_precise_page_mapcount(page_folio(page), - page); - - if (put_user(mapcount, out)) { + ppage =3D pfn_to_online_page(pfn); + + if (ppage) { + switch (op) { + case KPAGE_FLAGS: + info =3D stable_page_flags(ppage); + break; + case KPAGE_COUNT: + info =3D folio_precise_page_mapcount(page_folio(ppage), ppage); + break; + case KPAGE_CGROUP: + info =3D page_cgroup_ino(ppage); + break; + default: + info =3D 0; + break; + } + } else + info =3D 0; + + if (put_user(info, out)) { ret =3D -EFAULT; break; } @@ -89,17 +101,23 @@ static ssize_t kpagecount_read(struct file *file, char= __user *buf, return ret; } =20 +/* /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 =3D { .proc_flags =3D PROC_ENTRY_PERMANENT, .proc_lseek =3D mem_lseek, .proc_read =3D kpagecount_read, }; =20 -/* /proc/kpageflags - an array exposing page flags - * - * Each entry is a u64 representing the corresponding - * physical page flags. - */ =20 static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit) { @@ -220,47 +238,17 @@ u64 stable_page_flags(const struct page *page) #endif =20 return u; -}; +} =20 +/* /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 =3D get_max_dump_pfn(); - u64 __user *out =3D (u64 __user *)buf; - unsigned long src =3D *ppos; - unsigned long pfn; - ssize_t ret =3D 0; - - pfn =3D src / KPMSIZE; - if (src & KPMMASK || count & KPMMASK) - return -EINVAL; - if (src >=3D max_dump_pfn * KPMSIZE) - return 0; - count =3D 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 =3D pfn_to_online_page(pfn); - - if (put_user(stable_page_flags(page), out)) { - ret =3D -EFAULT; - break; - } - - pfn++; - out++; - count -=3D KPMSIZE; - - cond_resched(); - } - - *ppos +=3D (char __user *)out - buf; - if (!ret) - ret =3D (char __user *)out - buf; - return ret; + return kpage_read(file, buf, count, ppos, KPAGE_FLAGS); } =20 static const struct proc_ops kpageflags_proc_ops =3D { @@ -271,53 +259,10 @@ static const struct proc_ops kpageflags_proc_ops =3D { =20 #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 =3D get_max_dump_pfn(); - u64 __user *out =3D (u64 __user *)buf; - struct page *ppage; - unsigned long src =3D *ppos; - unsigned long pfn; - ssize_t ret =3D 0; - u64 ino; - - pfn =3D src / KPMSIZE; - if (src & KPMMASK || count & KPMMASK) - return -EINVAL; - if (src >=3D max_dump_pfn * KPMSIZE) - return 0; - count =3D 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 =3D pfn_to_online_page(pfn); - - if (ppage) - ino =3D page_cgroup_ino(ppage); - else - ino =3D 0; - - if (put_user(ino, out)) { - ret =3D -EFAULT; - break; - } - - pfn++; - out++; - count -=3D KPMSIZE; - - cond_resched(); - } - - *ppos +=3D (char __user *)out - buf; - if (!ret) - ret =3D (char __user *)out - buf; - return ret; + return kpage_read(file, buf, count, ppos, KPAGE_CGROUP); } - static const struct proc_ops kpagecgroup_proc_ops =3D { .proc_flags =3D PROC_ENTRY_PERMANENT, .proc_lseek =3D mem_lseek, diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 6e74b8254d9b..df4d28c7e1a3 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1794,6 +1794,10 @@ static inline void count_objcg_events(struct obj_cgr= oup *objcg, { } =20 +static inline ino_t page_cgroup_ino(struct page *page) +{ + return 0; +} #endif /* CONFIG_MEMCG */ =20 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP) --=20 2.25.1