linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shivank Garg <shivankg@amd.com>
To: Luiz Capitulino <luizcap@redhat.com>,
	david@redhat.com, willy@infradead.org, akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, sj@kernel.org
Subject: Re: [PATCH v2 4/4] fs: stable_page_flags(): use snapshot_page()
Date: Tue, 8 Jul 2025 11:25:26 +0530	[thread overview]
Message-ID: <880b9e00-23d5-4c03-8269-8e2f9a50f358@amd.com> (raw)
In-Reply-To: <42122b9988871469f1311d71a608629f40c0c55d.1751914235.git.luizcap@redhat.com>



On 7/8/2025 12:20 AM, Luiz Capitulino wrote:
> A race condition is possible in stable_page_flags() where user-space is
> reading /proc/kpageflags concurrently to a folio split. This may lead to
> oopses or BUG_ON()s being triggered.
> 
> To fix this, this commit uses snapshot_page() in stable_page_flags() so
> that stable_page_flags() works with a stable page and folio snapshots
> instead.
> 
> Note that stable_page_flags() makes use of some functions that require
> the original page or folio pointer to work properly (eg.
> is_free_budy_page() and folio_test_idle()). Since those functions can't
> be used on the page snapshot, we replace their usage with flags that
> were set by snapshot_page() for this purpose.
> 
> Signed-off-by: Luiz Capitulino <luizcap@redhat.com>
> ---
>  fs/proc/page.c | 29 +++++++++++++----------------
>  1 file changed, 13 insertions(+), 16 deletions(-)
> 
> diff --git a/fs/proc/page.c b/fs/proc/page.c
> index 936f8bbe5a6f..cb58f066eb31 100644
> --- a/fs/proc/page.c
> +++ b/fs/proc/page.c
> @@ -147,6 +147,7 @@ static inline u64 kpf_copy_bit(u64 kflags, int ubit, int kbit)
>  u64 stable_page_flags(const struct page *page)
>  {
>  	const struct folio *folio;
> +	struct page_snapshot ps;
>  	unsigned long k;
>  	unsigned long mapping;
>  	bool is_anon;
> @@ -158,7 +159,9 @@ u64 stable_page_flags(const struct page *page)
>  	 */
>  	if (!page)
>  		return 1 << KPF_NOPAGE;
> -	folio = page_folio(page);
> +
> +	snapshot_page(&ps, page);
> +	folio = &ps.folio_snapshot;
>  
>  	k = folio->flags;
>  	mapping = (unsigned long)folio->mapping;
> @@ -167,7 +170,7 @@ u64 stable_page_flags(const struct page *page)
>  	/*
>  	 * pseudo flags for the well known (anonymous) memory mapped pages
>  	 */
> -	if (page_mapped(page))
> +	if (folio_mapped(folio))
>  		u |= 1 << KPF_MMAP;
>  	if (is_anon) {
>  		u |= 1 << KPF_ANON;
> @@ -179,7 +182,7 @@ u64 stable_page_flags(const struct page *page)
>  	 * compound pages: export both head/tail info
>  	 * they together define a compound page's start/end pos and order
>  	 */
> -	if (page == &folio->page)
> +	if (ps.idx == 0)
>  		u |= kpf_copy_bit(k, KPF_COMPOUND_HEAD, PG_head);
>  	else
>  		u |= 1 << KPF_COMPOUND_TAIL;
> @@ -189,25 +192,19 @@ u64 stable_page_flags(const struct page *page)
>  	         folio_test_large_rmappable(folio)) {
>  		/* Note: we indicate any THPs here, not just PMD-sized ones */
>  		u |= 1 << KPF_THP;
> -	} else if (is_huge_zero_folio(folio)) {
> +	} else if (is_huge_zero_pfn(ps.pfn)) {
>  		u |= 1 << KPF_ZERO_PAGE;
>  		u |= 1 << KPF_THP;
> -	} else if (is_zero_folio(folio)) {
> +	} else if (is_zero_pfn(ps.pfn)) {
>  		u |= 1 << KPF_ZERO_PAGE;
>  	}
>  
> -	/*
> -	 * Caveats on high order pages: PG_buddy and PG_slab will only be set
> -	 * on the head page.
> -	 */
> -	if (PageBuddy(page))
> -		u |= 1 << KPF_BUDDY;
> -	else if (page_count(page) == 0 && is_free_buddy_page(page))
> +	if (ps.flags & PAGE_SNAPSHOT_PG_FREE)
>  		u |= 1 << KPF_BUDDY;
>  
> -	if (PageOffline(page))
> +	if (folio_test_offline(folio))
>  		u |= 1 << KPF_OFFLINE;
> -	if (PageTable(page))
> +	if (folio_test_pgtable(folio))
>  		u |= 1 << KPF_PGTABLE;
>  	if (folio_test_slab(folio))
>  		u |= 1 << KPF_SLAB;
> @@ -215,7 +212,7 @@ u64 stable_page_flags(const struct page *page)
>  #if defined(CONFIG_PAGE_IDLE_FLAG) && defined(CONFIG_64BIT)
>  	u |= kpf_copy_bit(k, KPF_IDLE,          PG_idle);
>  #else
> -	if (folio_test_idle(folio))
> +	if (ps.flags & PAGE_SNAPSHOT_PG_IDLE)
>  		u |= 1 << KPF_IDLE;
>  #endif
>  
> @@ -241,7 +238,7 @@ u64 stable_page_flags(const struct page *page)
>  	if (u & (1 << KPF_HUGE))
>  		u |= kpf_copy_bit(k, KPF_HWPOISON,	PG_hwpoison);
>  	else
> -		u |= kpf_copy_bit(page->flags, KPF_HWPOISON,	PG_hwpoison);
> +		u |= kpf_copy_bit(ps.page_snapshot.flags, KPF_HWPOISON, PG_hwpoison);
>  #endif
>  
>  	u |= kpf_copy_bit(k, KPF_RESERVED,	PG_reserved);
LGTM

Reviewed-by: Shivank Garg <shivankg@amd.com>

  reply	other threads:[~2025-07-08  5:55 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-07 18:50 [PATCH v2 0/4] mm: introduce snapshot_page() Luiz Capitulino
2025-07-07 18:50 ` [PATCH v2 1/4] mm/memory: introduce is_huge_zero_pfn() and use it in vm_normal_page_pmd() Luiz Capitulino
2025-07-07 19:37   ` Shivank Garg
2025-07-07 18:50 ` [PATCH v2 2/4] mm/util: introduce snapshot_page() Luiz Capitulino
2025-07-08  5:49   ` Shivank Garg
2025-07-08 16:59     ` Luiz Capitulino
2025-07-14 13:16       ` Luiz Capitulino
2025-07-14 13:43         ` Shivank Garg
2025-07-11 11:56   ` David Hildenbrand
2025-07-11 12:59     ` Luiz Capitulino
2025-07-07 18:50 ` [PATCH v2 3/4] proc: kpagecount: use snapshot_page() Luiz Capitulino
2025-07-08  5:51   ` Shivank Garg
2025-07-07 18:50 ` [PATCH v2 4/4] fs: stable_page_flags(): " Luiz Capitulino
2025-07-08  5:55   ` Shivank Garg [this message]
2025-07-09  8:14 ` BUG: KASAN: stack-out-of-bounds in snapshot_page during gup_test test case Harry Yoo
2025-07-09 12:51   ` Luiz Capitulino
2025-07-10  3:32     ` Luiz Capitulino

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=880b9e00-23d5-4c03-8269-8e2f9a50f358@amd.com \
    --to=shivankg@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luizcap@redhat.com \
    --cc=sj@kernel.org \
    --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).