From: "Carlos López" <clopez@suse.de>
To: kvm@vger.kernel.org, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org,
linux-riscv@lists.infradead.org, seanjc@google.com,
pbonzini@redhat.com, akpm@linux-foundation.org, david@kernel.org,
peterx@redhat.com, ljs@kernel.org, imbrenda@linux.ibm.com,
borntraeger@linux.ibm.com, frankja@linux.ibm.com,
"Carlos López" <clopez@suse.de>, "Zi Yan" <ziy@nvidia.com>,
"Baolin Wang" <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <liam@infradead.org>,
"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>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Jann Horn" <jannh@google.com>,
"Pedro Falcato" <pfalcato@suse.de>
Subject: [PATCH v3 4/9] mm: keep writable entries writable on protection changes
Date: Thu, 12 Nov 2026 15:20:33 +0100 [thread overview]
Message-ID: <20261112142039.907090-6-clopez@suse.de> (raw)
In-Reply-To: <20261112142039.907090-2-clopez@suse.de>
change_protection() applies vma->vm_page_prot, which lacks the write bit
for CoW and write-notified mappings. MM_CP_TRY_CHANGE_WRITABLE makes
entries writable again if a write fault would only set the write bit.
In shared mappings, it relies on the dirty bit for this, as a clean
entry might still need a write fault to notify the FS.
However, this misses clean entries that were already writable. For
example, a uffd-wp unprotect makes shmem PTEs read-only if they were
mapped by a read fault before uffd-wp was registered, so the next write
to each of them faults.
Entries that were writable before the change can stay writable, as
anything that relies on write faults write-protects the entry first, and
MM_CP_TRY_CHANGE_WRITABLE is only set if the VMA stays writable. So
check the old entry before the shared and private rules.
Note: writable anonymous entries are always exclusive and thus already
kept writable, so in private mappings this only affects PFNs that
drivers map writable into them.
Signed-off-by: Carlos López <clopez@suse.de>
---
mm/huge_memory.c | 22 ++++++++++++++++------
mm/mprotect.c | 11 +++++++++++
2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 080b718eb7de..69f4485c277e 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2269,7 +2269,8 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
}
static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
- unsigned long addr, pmd_t pmd)
+ unsigned long addr, pmd_t oldpmd,
+ pmd_t pmd)
{
struct page *page;
@@ -2288,6 +2289,10 @@ static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
if (userfaultfd_huge_pmd_wp(vma, pmd))
return false;
+ /* Writes did not need a fault before the change either. */
+ if (pmd_write(oldpmd))
+ return true;
+
if (!(vma->vm_flags & VM_SHARED)) {
/* See can_change_pte_writable(). */
page = vm_normal_page_pmd(vma, addr, pmd);
@@ -2317,7 +2322,7 @@ vm_fault_t do_huge_pmd_uffd_rwp(struct vm_fault *vmf)
pmd = pmd_mkyoung(pmd);
if (!pmd_write(pmd) &&
vma_wants_manual_pte_write_upgrade(vma) &&
- can_change_pmd_writable(vma, vmf->address, pmd))
+ can_change_pmd_writable(vma, vmf->address, vmf->orig_pmd, pmd))
pmd = pmd_mkwrite(pmd, vma);
set_pmd_at(vma->vm_mm, vmf->address & HPAGE_PMD_MASK,
vmf->pmd, pmd);
@@ -2354,7 +2359,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
*/
writable = pmd_write(pmd);
if (!writable && vma_wants_manual_pte_write_upgrade(vma) &&
- can_change_pmd_writable(vma, vmf->address, pmd))
+ can_change_pmd_writable(vma, vmf->address, old_pmd, pmd))
writable = true;
folio = vm_normal_folio_pmd(vma, haddr, pmd);
@@ -2820,7 +2825,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
/* See change_pte_range(). */
if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) &&
- can_change_pmd_writable(vma, addr, entry))
+ can_change_pmd_writable(vma, addr, oldpmd, entry))
entry = pmd_mkwrite(entry, vma);
ret = HPAGE_PMD_NR;
@@ -2835,7 +2840,8 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
static inline bool can_change_pud_writable(struct vm_area_struct *vma,
- unsigned long addr, pud_t pud)
+ unsigned long addr, pud_t oldpud,
+ pud_t pud)
{
struct page *page;
@@ -2846,6 +2852,10 @@ static inline bool can_change_pud_writable(struct vm_area_struct *vma,
if (vma_soft_dirty_enabled(vma) && !pud_soft_dirty(pud))
return false;
+ /* Writes did not need a fault before the change either. */
+ if (pud_write(oldpud))
+ return true;
+
if (!(vma->vm_flags & VM_SHARED)) {
/* See can_change_pte_writable(). */
page = vm_normal_page_pud(vma, addr, pud);
@@ -2896,7 +2906,7 @@ int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
entry = pud_modify(oldpud, newprot);
/* See change_pte_range(). */
if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pud_write(entry) &&
- can_change_pud_writable(vma, addr, entry))
+ can_change_pud_writable(vma, addr, oldpud, entry))
entry = pud_mkwrite(entry);
set_pud_at(mm, addr, pudp, entry);
tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index e18d4e12fc34..84bcfe0281d3 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -194,6 +194,17 @@ static __always_inline void set_write_prot_commit_flush_ptes(struct vm_area_stru
{
bool set_write;
+ /*
+ * Writes did not need a fault before the change either. All PTEs in
+ * the batch share the write bit.
+ */
+ if (pte_write(oldpte)) {
+ set_write = maybe_change_pte_writable(vma, ptent);
+ prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, nr_ptes,
+ /* idx = */ 0, set_write, tlb);
+ return;
+ }
+
if (vma_test(vma, VMA_SHARED_BIT)) {
set_write = can_change_shared_pte_writable(vma, ptent);
prot_commit_flush_ptes(vma, addr, ptep, oldpte, ptent, nr_ptes,
--
2.51.0
next prev parent reply other threads:[~2026-11-12 14:25 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 14:20 [PATCH v3 0/9] KVM: don't zap secondary MMU on permission upgrade Carlos López
2026-11-12 14:20 ` [PATCH v3 1/9] mm: fix eager write-bit restore for write-notified shared mappings Carlos López
2026-11-12 14:20 ` [PATCH v3 2/9] riscv: mm: add pud_soft_dirty() Carlos López
2026-11-12 14:20 ` [PATCH v3 3/9] mm/huge_memory: honor MM_CP_TRY_CHANGE_WRITABLE in change_huge_pud() Carlos López
2026-11-12 14:20 ` Carlos López [this message]
2026-11-12 14:20 ` [PATCH v3 5/9] mm/mprotect: add MM_CP_PERM_UPGRADE Carlos López
2026-11-12 14:20 ` [PATCH v3 6/9] mm/mmu_notifier: add MMU_NOTIFIER_RANGE_PERM_UPGRADE Carlos López
2026-11-12 14:20 ` [PATCH v3 7/9] KVM: pass the mmu_notifier event and flags to kvm_gfn_range Carlos López
2026-11-12 14:20 ` [PATCH v3 8/9] KVM: x86/mmu: don't zap secondary MMU on permission upgrade Carlos López
2026-11-12 14:20 ` [PATCH v3 9/9] KVM: s390: don't unmap the gmap " Carlos López
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=20261112142039.907090-6-clopez@suse.de \
--to=clopez@suse.de \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=borntraeger@linux.ibm.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=frankja@linux.ibm.com \
--cc=imbrenda@linux.ibm.com \
--cc=jannh@google.com \
--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=linux-riscv@lists.infradead.org \
--cc=linux-s390@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=nico.pache@linux.dev \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=pfalcato@suse.de \
--cc=ryan.roberts@arm.com \
--cc=seanjc@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@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®