From: Konstantin Khorenko <khorenko@virtuozzo.com>
To: Lance Yang <lance.yang@linux.dev>
Cc: "Alex Williamson" <alex@shazbot.org>,
"David Hildenbrand" <david@kernel.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Ryan Roberts" <ryan.roberts@arm.com>,
kvm@vger.kernel.org,
"Loïc Molinari" <loic.molinari@collabora.com>,
"Nico Pache" <nico.pache@linux.dev>, "Zi Yan" <ziy@nvidia.com>,
"Liam R. Howlett" <liam@infradead.org>,
dri-devel@lists.freedesktop.org,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Usama Arif" <usama.arif@linux.dev>,
"Barry Song" <baohua@kernel.org>, "Dev Jain" <dev.jain@arm.com>,
linux-mm@kvack.org, "Peter Xu" <peterx@redhat.com>,
linux-kernel@vger.kernel.org, "Jason Gunthorpe" <jgg@ziepe.ca>,
stable@vger.kernel.org, "Kiryl Shutsemau" <kas@kernel.org>
Subject: Re: [PATCH] mm/huge_memory: recheck a special PMD under the lock in copy_huge_pmd()
Date: Wed, 14 Oct 2026 17:27:24 +0200 [thread overview]
Message-ID: <85b319d2-ba91-45cf-9f68-4182df93c848@virtuozzo.com> (raw)
In-Reply-To: <58e8d79b-f7c3-4ded-9272-07c309917055@linux.dev>
On 10/14/26 15:19, Lance Yang wrote:
>
>
> On 2026/10/14 19:57, Konstantin Khorenko wrote:
> [...]
>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
>> index 1e5d68acf62a5..fcea98f0d8865 100644
>> --- a/mm/huge_memory.c
>> +++ b/mm/huge_memory.c
>> @@ -1965,13 +1965,22 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
>> src_ptl = pmd_lockptr(src_mm, src_pmd);
>> spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
>> /*
>> - * No need to recheck the pmd, it can't change with write
>> - * mmap lock held here.
>> - *
>> - * Meanwhile, making sure it's not a CoW VMA with writable
>> - * mapping, otherwise it means either the anon page wrongly
>> - * applied special bit, or we made the PRIVATE mapping be
>> - * able to wrongly write to the backend MMIO.
>> + * The mmap and VMA write locks of the source keep faults
>> + * out, but unmap_mapping_range() does not take the mmap
>> + * lock and may have cleared the pmd since the lockless
>> + * read. Let copy_pmd_range() skip it then.
>> + */
>> + pmd = pmdp_get(src_pmd);
>> + if (unlikely(!pmd_present(pmd) || !pmd_special(pmd))) {
>
> Why not just check pmd_none(pmd) here? We're holding the source mmap/VMA
> write locks, so the PMD either stays special or gets cleared, right?
Right: with the source mmap and VMA write locks held, only a zap can
change it, and both ways a zap takes leave the PMD none:
zap_huge_pmd() clears it, and a partial zap splits it in
__split_huge_pmd_locked(), which for a special VMA only clears it and
returns.
I tested the same condition as the lockless check that chose this
branch, so that the branch never copies anything it did not select,
but taking into account the above paragraph we can easily use pmd_none()
here, it even better matches the comment :), so I will switch to it in v3.
v2 is already out, with a second patch for the missing page table
deposit on ppc64 hash:
https://lore.kernel.org/r/20261014-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v2-0-e1548488fa68@virtuozzo.com
The pmd_none() change will go into v3.
Cheers, Konstantin
>> + spin_unlock(src_ptl);
>> + spin_unlock(dst_ptl);
>> + return -EAGAIN;
>> + }
>> + /*
>> + * Make sure it's not a CoW VMA with writable mapping,
>> + * otherwise it means either the anon page wrongly applied
>> + * special bit, or we made the PRIVATE mapping be able to
>> + * wrongly write to the backend MMIO.
>> */
>> VM_WARN_ON_ONCE(vma_is_cow_mapping(src_vma) && pmd_write(pmd));
>> goto set_pmd;
>>
>> ---
>> base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
>> change-id: 20261014-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-bdf7efb612c2
>>
>> Best regards,
>> --
>> Konstantin Khorenko <khorenko@virtuozzo.com>
>>
>
prev parent reply other threads:[~2026-10-14 15:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 11:57 [PATCH] mm/huge_memory: recheck a special PMD under the lock in copy_huge_pmd() Konstantin Khorenko
2026-10-14 12:15 ` sashiko-bot
2026-10-14 13:15 ` Konstantin Khorenko
2026-10-14 13:19 ` Lance Yang
2026-10-14 15:27 ` Konstantin Khorenko [this message]
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=85b319d2-ba91-45cf-9f68-4182df93c848@virtuozzo.com \
--to=khorenko@virtuozzo.com \
--cc=akpm@linux-foundation.org \
--cc=alex@shazbot.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=jgg@ziepe.ca \
--cc=kas@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=loic.molinari@collabora.com \
--cc=nico.pache@linux.dev \
--cc=peterx@redhat.com \
--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