mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>,
	Alex Williamson <alex@shazbot.org>, Peter Xu <peterx@redhat.com>,
	Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Nico Pache <nico.pache@linux.dev>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
	Usama Arif <usama.arif@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>, Wei Liu <wei.liu@kernel.org>,
	Roman Kisel <romank@linux.microsoft.com>,
	Saurabh Sengar <ssengar@linux.microsoft.com>,
	Naman Jain <namjain@linux.microsoft.com>,
	Michael Kelley <mhklinux@outlook.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Eliav Farber <farbere@amazon.com>,
	stable@vger.kernel.org,
	"Jose A. Perez de Azpillaga" <azpijr@gmail.com>
Subject: Re: [PATCH 2/3] mm/memory: do not report OOM if insert_pfn() races a huge fault
Date: Mon, 16 Nov 2026 21:10:05 +0100	[thread overview]
Message-ID: <b58451d6-e90d-4aa3-afb4-13666953dd1b@kernel.org> (raw)
In-Reply-To: <avs6WWOurjIN-trm@gremlin>

On 11/16/26 18:21, Lorenzo Stoakes (ARM) wrote:
> On Mon, Nov 16, 2026 at 04:59:35PM +0100, David Hildenbrand (Arm) wrote:
>> On 11/15/26 21:58, Lorenzo Stoakes (ARM) wrote:
>>> From: Eliav Farber <farbere@amazon.com>
>>>
>>> A driver that implements both ->fault and ->huge_fault must anticipate
>>> concurrent use of both.
>>>
>>> This can happen, for example, if a user calls prctl(PR_SET_THP_DISABLE)
>>> concurrent with faulting, as that only holds the mmap write lock and thus
>>> does not exclude faulting threads stabilised by VMA lock.
>>>
>>> This is not currently safely handled by insert_pfn(), however, which
>>> obtains its PTE entry via get_locked_pte(), and treats a NULL return value
>>> as an allocation failure, returning VM_FAULT_OOM.
>>>
>>> The issue is that get_locked_pte() also returns NULL if the PMD entry is
>>> not a page table which can happen if a concurrent huge fault installs a
>>> huge PMD entry between __handle_mm_fault() observing the PMD entry to be
>>> empty and insert_pfn() being invoked.
>>>
>>> This can result in VM_FAULT_OOM being reported incorrectly.
>>>
>>> Address this by proactively checking whether a page table allocation
>>> actually failed, and returning VM_FAULT_NOPAGE if the NULL indicates that
>>> we were raced instead.
>>>
>>> The same assumption is made in insert_page(), however only DAX implements
>>> ->huge_fault and can reach it, and it serialises faults of different orders
>>> with its own mapping entry lock, so it does not require a change.
>>>
>>> Target the commit introducing the first driver able to do this.
>>>
>>> Link: https://lore.kernel.org/all/20261112051440.16140-3-farbere@amazon.com/
>>> Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
>>> Cc: stable@vger.kernel.org
>>> Suggested-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
>>> Signed-off-by: Eliav Farber <farbere@amazon.com>
>>> [ ljs: rewrote commit message, trimmed comment ]
>>> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>>> ---
>>>  mm/memory.c | 11 +++++++++--
>>>  1 file changed, 9 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/mm/memory.c b/mm/memory.c
>>> index e03110906aa8..cff39eccfd42 100644
>>> --- a/mm/memory.c
>>> +++ b/mm/memory.c
>>> @@ -2866,10 +2866,17 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr,
>>>  	struct mm_struct *mm = vma->vm_mm;
>>>  	pte_t *pte, entry;
>>>  	spinlock_t *ptl;
>>> +	pmd_t *pmd;
>>>
>>> -	pte = get_locked_pte(mm, addr, &ptl);
>>> -	if (!pte)
>>> +	pmd = walk_to_pmd(mm, addr);
>>> +	if (!pmd)
>>>  		return VM_FAULT_OOM;
>>> +	if (pte_alloc(mm, pmd))
>>> +		return VM_FAULT_OOM;
>>> +	/* We may have raced a huge fault, if so let the fault be retried. */
>>> +	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
>>> +	if (!pte)
>>> +		return VM_FAULT_NOPAGE;
>>
>> Same question, what if we would stumble over a PUD entry, how would we handle
>> that today? I'd assume we'd also want to return VM_FAULT_NOPAGE instead of
>> VM_FAULT_OOM.
> 
> It can't for similar reasons.
> 
> Actually changing this would require changing walk_to_pmd() a fair bit so it can
> distinguish a failure to allocate from that, and I'm not sure it's worth doing
> that at this point, better as follow-up?

There are only two users in mm/memory.c right?

I'd just make the whole thing less shaky right away, but whatever you prefer!

-- 
Cheers,

David

  reply	other threads:[~2026-11-16 20:10 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 20:58 [PATCH 0/3] mm: fix races between huge and non-huge faults in PFN mappings Lorenzo Stoakes (ARM)
2026-11-15 20:58 ` [PATCH 1/3] mm/memory: remove racy huge PMD assertion from walk_to_pmd() Lorenzo Stoakes (ARM)
2026-11-16 10:35   ` Jose A. Perez de Azpillaga
2026-11-16 15:56   ` David Hildenbrand (Arm)
2026-11-16 17:10     ` Lorenzo Stoakes (ARM)
2026-11-16 20:06       ` David Hildenbrand (Arm)
2026-11-16 17:40   ` Gregory Price (Meta)
2026-11-15 20:58 ` [PATCH 2/3] mm/memory: do not report OOM if insert_pfn() races a huge fault Lorenzo Stoakes (ARM)
2026-11-16 10:36   ` Jose A. Perez de Azpillaga
2026-11-16 15:59   ` David Hildenbrand (Arm)
2026-11-16 17:21     ` Lorenzo Stoakes (ARM)
2026-11-16 20:10       ` David Hildenbrand (Arm) [this message]
2026-11-16 18:13   ` Gregory Price (Meta)
2026-11-15 20:58 ` [PATCH 3/3] mm/huge_memory: only compare PFNs of huge entries on huge fault insert Lorenzo Stoakes (ARM)
2026-11-16 10:42   ` Jose A. Perez de Azpillaga
2026-11-16 16:01   ` David Hildenbrand (Arm)
2026-11-16 16:55     ` Lorenzo Stoakes (ARM)
2026-11-16 20:09       ` David Hildenbrand (Arm)
2026-11-16 18:22   ` Gregory Price (Meta)

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=b58451d6-e90d-4aa3-afb4-13666953dd1b@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alex@shazbot.org \
    --cc=azpijr@gmail.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=dev.jain@arm.com \
    --cc=farbere@amazon.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=ljs@kernel.org \
    --cc=mhklinux@outlook.com \
    --cc=mhocko@suse.com \
    --cc=namjain@linux.microsoft.com \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=romank@linux.microsoft.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=ssengar@linux.microsoft.com \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=wei.liu@kernel.org \
    --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®