linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Naoya Horiguchi <naoya.horiguchi@linux.dev>
To: linux-mm@kvack.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Matthew Wilcox <willy@infradead.org>,
	David Hildenbrand <david@redhat.com>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	Miaohe Lin <linmiaohe@huawei.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	Muchun Song <songmuchun@bytedance.com>,
	Naoya Horiguchi <naoya.horiguchi@nec.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v1 2/5] mm: kpageflags: distinguish thp and folio
Date: Tue, 10 Oct 2023 23:27:58 +0900	[thread overview]
Message-ID: <20231010142801.3780917-3-naoya.horiguchi@linux.dev> (raw)
In-Reply-To: <20231010142801.3780917-1-naoya.horiguchi@linux.dev>

From: Naoya Horiguchi <naoya.horiguchi@nec.com>

Currently a large folio is considered as thp in the output of
/proc/kpageflags because stable_page_flags() does not have resolution to
distinguish thp and large folio.  This is confusing and the readers of
/proc/kpageflags need additional checks in userspace, which is inefficient.

Check page order in stable_page_flags() to distinguish thp and large folio.
Although thp (or other types of compound page) is a special form of folio,
but the KPF_FOLIO means "folio" in narrower meaning (representing folios
in page cache), so KPF_FOLIO is not set on thp or hugetlb pages.  This is
because thp and hugetlb (and other compound pages) have their own KPF_*
flags, so those are already identifiable.

Thp and folio use some struct page's field of the first tail pages for
internal use.  There's no point to parse and show flag info based on tail
pages, so return immediately when finding thp/folio tail pages.

The output below shows how this patch changes the output of page-types.

Before patch:

  // anonymous thp
  voffset         offset  len     flags
  ...
  700000000       156c00  1       ___U_l_____Ma_bH______t_____________f_d_____1
  700000001       156c01  1       L__U_______Ma___T_____t_____________f_______1
  700000002       156c02  1fe     ___________Ma___T_____t_____________f_______1
                                                        ^
                                                    this 't' means thp
  // file thp
  700000000       15d600  1       __RUDl_____M__bH______t_____________f__I____1
  700000001       15d601  1       L__U_______M____T_____t_____________f_______1
  700000002       15d602  1fe     ___________M____T_____t_____________f_______1

  // large folio
  700000000       154f84  1       __RU_l_____M___H______t________P____f_____F_1
  700000001       154f85  1       ________W__M____T_____t_____________f_____F_1
  700000002       154f86  2       ___________M____T_____t_____________f_____F_1

After patch:

  // anonymous thp
  700000000       117000  1       ___U_l_____Ma_bH______t_____________f_d_____1
  700000001       117001  1ff     ________________T_____t_____________f_______1

  // file thp
  700000000       118400  1       __RUDl_____M__bH______t_____________f__I____1
  700000001       118401  1ff     ________________T_____t_____________f_______1

  // large folio
  700000000       148da4  1       __RU_l_____M___H___________f___P____f_____F_1
  700000001       148da5  3       ________________T__________f________f_____F_1
                                                             ^
                                                    this 'f' means folio

Signed-off-by: Naoya Horiguchi <naoya.horiguchi@nec.com>
---
 fs/proc/page.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/fs/proc/page.c b/fs/proc/page.c
index 195b077c0fac..78f675f791c1 100644
--- a/fs/proc/page.c
+++ b/fs/proc/page.c
@@ -154,11 +154,24 @@ u64 stable_page_flags(struct page *page)
 	else if (PageTransCompound(page)) {
 		struct page *head = compound_head(page);
 
-		if (PageLRU(head) || PageAnon(head))
-			u |= 1 << KPF_THP;
-		else if (is_huge_zero_page(head)) {
+		/*
+		 * We need to check PageLRU/PageAnon to make sure a given page
+		 * is a thp, not a huge zero page or a generic compound page
+		 * (allocated by drivers with __GFP_COMP).
+		 */
+		if (PageLRU(head) || PageAnon(head)) {
+			if (compound_order(head) == HPAGE_PMD_ORDER)
+				u |= 1 << KPF_THP;
+			else
+				u |= 1 << KPF_FOLIO;
+		} else if (is_huge_zero_page(head))
 			u |= 1 << KPF_ZERO_PAGE;
-			u |= 1 << KPF_THP;
+
+		if (PageHead(page))
+			u |= 1 << KPF_COMPOUND_HEAD;
+		if (PageTail(page)) {
+			u |= 1 << KPF_COMPOUND_TAIL;
+			return u;
 		}
 	} else if (is_zero_pfn(page_to_pfn(page)))
 		u |= 1 << KPF_ZERO_PAGE;
-- 
2.25.1



  parent reply	other threads:[~2023-10-10 14:33 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-10 14:27 [PATCH v1 0/5] mm, kpageflags: support folio and fix output for compound pages Naoya Horiguchi
2023-10-10 14:27 ` [PATCH v1 1/5] include/uapi/linux/kernel-page-flags.h: define KPF_FOLIO Naoya Horiguchi
2023-10-10 14:27 ` Naoya Horiguchi [this message]
2023-10-10 14:27 ` [PATCH v1 3/5] mm, kpageflags: separate code path for hugetlb pages Naoya Horiguchi
2023-10-10 14:28 ` [PATCH v1 4/5] mm, kpageflags: fix invalid output for PageSlab Naoya Horiguchi
2023-10-10 14:28 ` [PATCH v1 5/5] tools/mm/page-types.c: hide compound pages in non-raw mode Naoya Horiguchi
2023-10-12  8:33 ` [PATCH v1 0/5] mm, kpageflags: support folio and fix output for compound pages David Hildenbrand
2023-10-12 15:02   ` Naoya Horiguchi
2023-10-12 15:30     ` David Hildenbrand
2023-10-13  0:54       ` Naoya Horiguchi
2023-10-13  7:46         ` David Hildenbrand
2023-10-13 15:03       ` Matthew Wilcox
2023-10-16 10:13         ` David Hildenbrand
2023-10-16 11:36           ` Ryan Roberts
2023-10-18  5:25             ` Naoya Horiguchi

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=20231010142801.3780917-3-naoya.horiguchi@linux.dev \
    --to=naoya.horiguchi@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mike.kravetz@oracle.com \
    --cc=naoya.horiguchi@nec.com \
    --cc=songmuchun@bytedance.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