mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Lance Yang <lance.yang@linux.dev>
Cc: akpm@linux-foundation.org, ziy@nvidia.com,
	baolin.wang@linux.alibaba.com, liam@infradead.org,
	nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com,
	baohua@kernel.org, usama.arif@linux.dev, kas@kernel.org,
	qi.zheng@linux.dev, hughd@google.com, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v2 1/2] mm: free unused page table allocated by do_set_pmd()
Date: Mon, 9 Nov 2026 15:25:40 +0100	[thread overview]
Message-ID: <2e3287d7-19d5-4441-8750-bd351df5574a@kernel.org> (raw)
In-Reply-To: <20261108091455.88759-1-lance.yang@linux.dev>

On 11/8/26 10:14, Lance Yang wrote:
> 
> On Fri, Nov 06, 2026 at 01:36:12PM +0100, David Hildenbrand (Arm) wrote:
>> do_set_pmd() can allocate vmf->prealloc_pte on architectures that need a
>> deposited page table, such as PowerPC with the hash MMU. If the PMD is
>> no longer empty when it takes the PMD lock, it returns VM_FAULT_FALLBACK
>> without consuming that table.
>>
>> Since collapse_pte_mapped_thp() uses mmap_read_lock(), a concurrent
>> userfaultfd operation can install a PTE table before taking the folio
>> lock. If that races with set_huge_pmd(), the unused preallocated table
>> is lost when its local vm_fault goes out of scope.
>>
>> Track whether do_set_pmd() allocated the table and free it after dropping
>> the PMD lock if it was not consumed. Preserve any preallocated table
>> supplied by the caller for PTE fallback. Successful PMD installation
>> consumes the table and clears the pointer.
>>
>> Returning without a preallocated table matches the existing behavior on
>> architectures that do not need a deposited table. All callers
>> already handle that case.
>>
>> Fixes: 1043173eb5eb ("mm/khugepaged: collapse_pte_mapped_thp() with mmap_read_lock()")
>> Cc: stable@vger.kernel.org
>> Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>> ---
>> mm/memory.c | 6 ++++++
>> 1 file changed, 6 insertions(+)
>>
>> diff --git a/mm/memory.c b/mm/memory.c
>> index becf53a2a0944..421064aef3cf0 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -5687,6 +5687,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
>> 	struct vm_area_struct *vma = vmf->vma;
>> 	bool write = vmf->flags & FAULT_FLAG_WRITE;
>> 	unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
>> +	bool pgtable_allocated = false;
>> 	pmd_t entry;
>> 	vm_fault_t ret = VM_FAULT_FALLBACK;
>>
>> @@ -5725,6 +5726,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
>> 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
>> 		if (!vmf->prealloc_pte)
>> 			return VM_FAULT_OOM;
>> +		pgtable_allocated = true;
>> 	}
>>
>> 	vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
>> @@ -5755,6 +5757,10 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa
>> 	count_vm_event(THP_FILE_MAPPED);
>> out:
>> 	spin_unlock(vmf->ptl);
>> +	if (pgtable_allocated && vmf->prealloc_pte) {
> 
> Took me a sec ... to see why we can't just check vmf->prealloc_pte here
> ...
> 
> Emm ... as it turns out, the caller might have supplied it for fallback :D
> 

Yeah, it's all ... complicated :)

-- 
Cheers,

David

  reply	other threads:[~2026-11-09 14:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 12:36 [PATCH v2 0/2] " David Hildenbrand (Arm)
2026-11-06 12:36 ` [PATCH v2 1/2] " David Hildenbrand (Arm)
2026-11-06 15:34   ` Usama Arif
2026-11-09 14:26     ` David Hildenbrand (Arm)
2026-11-08  9:14   ` Lance Yang
2026-11-09 14:25     ` David Hildenbrand (Arm) [this message]
2026-11-06 12:36 ` [PATCH v2 2/2] mm: don't allocate a page table under RCU in do_set_pmd() David Hildenbrand (Arm)
2026-11-06 15:56   ` Usama Arif
2026-11-09 14:27     ` David Hildenbrand (Arm)
2026-11-08  9:23   ` Lance Yang
2026-11-09 14:27 ` [PATCH v2 0/2] mm: free unused page table allocated by do_set_pmd() David Hildenbrand

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=2e3287d7-19d5-4441-8750-bd351df5574a@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=dev.jain@arm.com \
    --cc=hughd@google.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nico.pache@linux.dev \
    --cc=qi.zheng@linux.dev \
    --cc=ryan.roberts@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=ziy@nvidia.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

all inboxes | Powered by JetHome®