Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: sashiko-reviews@lists.linux.dev
Cc: Muchun Song <songmuchun@bytedance.com>,
	Oscar Salvador <osalvador@suse.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Miaohe Lin <linmiaohe@huawei.com>,
	David Hildenbrand <david@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Naoya Horiguchi <nao.horiguchi@gmail.com>,
	Joao Martins <joao.m.martins@oracle.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Ingo Molnar <mingo@kernel.org>, Lorenzo Stoakes <ljs@kernel.org>,
	linux-mm@kvack.org
Subject: Re: [PATCH v2 1/3] mm/hugetlb_vmemmap: fix lost HWPoison state during optimization
Date: Thu, 12 Nov 2026 10:59:08 +0800	[thread overview]
Message-ID: <1E848BF4-4AB4-4656-8523-FAC6DD8FDAEA@linux.dev> (raw)
In-Reply-To: <sashiko-outbox-209812@kernel.org>



> On Nov 12, 2026, at 06:38, sashiko-bot@kernel.org wrote:
> 
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] mm/memory-failure: unserialized unpoison_memory() races with
>  HVO head remap
> --
> 
> commit c0c1b7e9976626bf636152889fd9f5d88944d78b
> Author: Muchun Song <muchun.song@linux.dev>
> 
> mm/hugetlb_vmemmap: fix lost HWPoison state during optimization
> 
> This commit introduces vmemmap_remap_rwsem to serialize HVO head page
> remapping against HWPoison state updates in get_huge_page_for_hwpoison().
> 
>> diff --git a/mm/memory-failure.c b/mm/memory-failure.c
>> index 78f0a13349cb3..164a02866fd59 100644
>> --- a/mm/memory-failure.c
>> +++ b/mm/memory-failure.c
> 
> [ ... ]
> 
>> @@ -2038,6 +2039,7 @@ static int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
>> 	bool count_increased = false;
>> 	int ret, rc;
>> 
>> +	guard(vmemmap_remap)();
>> 	spin_lock_irq(&hugetlb_lock);
>> 	folio = page_folio(page);
>> 	if (!folio_test_hugetlb(folio)) {
> 
> [Severity: Medium]
> Does unpoison_memory() also need vmemmap_remap_rwsem when clearing
> PG_hwpoison and freeing folio->_hugetlb_hwpoison?

I think the following diff will fix this.

diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 164a02866fd5..abe92db14fbc 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -2814,6 +2814,7 @@ int unpoison_memory(unsigned long pfn)

        ghp = get_hwpoison_page(p, MF_UNPOISON);
        if (!ghp) {
+               guard(vmemmap_remap)();
                if (folio_test_hugetlb(folio)) {
                        huge = true;
                        count = folio_free_raw_hwp(folio, false);
@@ -2830,6 +2831,7 @@ int unpoison_memory(unsigned long pfn)
                                         pfn, &unpoison_rs);
                }
        } else {
+               guard(vmemmap_remap)();
                if (folio_test_hugetlb(folio)) {
                        huge = true;
                        count = folio_free_raw_hwp(folio, false);

> 
> If a newly initialized hugetlb folio is poisoned by a software-simulated
> memory failure before HVO acquires vmemmap_remap_rwsem, unpoison_memory()
> can race with the head page remap:
> 
> CPU0                                CPU1
> hugetlb_vmemmap_optimize_folio()
>  down_read(&vmemmap_remap_rwsem)
>  ...
>    vmemmap_optimize_pte()
>      vmemmap_dup_page_from_list(old_head, ...)
>                                    unpoison_memory()
>                                      get_hwpoison_page(..., MF_UNPOISON)
>                                      folio_free_raw_hwp(old_head, false)
>                                      folio_test_clear_hwpoison(old_head)
>      set_pte_at(..., new_head)
>      flush_tlb_all()
>                                    /* new_head retains PG_hwpoison and
>                                     * freed _hugetlb_hwpoison pointer */
> 
> In get_hwpoison_hugetlb_folio(), passing unpoison == true takes the
> folio_try_get(folio) branch even when !folio_test_hugetlb_migratable(folio),
> returning 0 on the frozen folio instead of -EBUSY:
> 
> mm/hugetlb.c:get_hwpoison_hugetlb_folio() {
>    ...
> 		if (folio_test_hugetlb_freed(folio))
> 			ret = 0;
> 		else if (folio_test_hugetlb_migratable(folio) || unpoison)
> 			ret = folio_try_get(folio);
> 		else
> 			ret = -EBUSY;
>    ...
> }
> 
> unpoison_memory() then sees ghp == 0 and calls folio_free_raw_hwp(folio,
> false), which skips the folio_test_hugetlb_vmemmap_optimized(folio) check
> used when move_flag is true and frees the struct raw_hwp_page entries from
> old_head.  Once CPU0 installs new_head and flushes the TLB, the folio still
> has PG_hwpoison set and folio->_hugetlb_hwpoison pointing to freed memory,
> leading to a use-after-free or double free when the list is later walked or
> freed.
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261111093747.50048-1-songmuchun@bytedance.com?part=1



  reply	other threads:[~2026-11-12  2:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  9:37 [PATCH v2 0/3] mm/hugetlb_vmemmap: Fix HVO races and deadlock Muchun Song
2026-11-11  9:37 ` [PATCH v2 1/3] mm/hugetlb_vmemmap: fix lost HWPoison state during optimization Muchun Song
2026-11-11 14:53   ` Jose A. Perez de Azpillaga
2026-11-11 17:46   ` Jiaqi Yan
2026-11-11 22:38   ` sashiko-bot
2026-11-12  2:59     ` Muchun Song [this message]
2026-11-12  5:35       ` Muchun Song
2026-11-11  9:37 ` [PATCH v2 2/3] mm/hugetlb_vmemmap: fix PMD split TLB flush race Muchun Song
2026-11-11 14:55   ` Jose A. Perez de Azpillaga
2026-11-11  9:37 ` [PATCH v2 3/3] mm/hugetlb_vmemmap: fix deadlock during PMD split Muchun Song
2026-11-11 14:56   ` Jose A. Perez de Azpillaga

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=1E848BF4-4AB4-4656-8523-FAC6DD8FDAEA@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=joao.m.martins@oracle.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mingo@kernel.org \
    --cc=nao.horiguchi@gmail.com \
    --cc=osalvador@suse.de \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=songmuchun@bytedance.com \
    /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