All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: have hva_to_pfn_remapped write-upgrade PTEs
@ 2026-07-29  1:42 Sergio Lopez
  2026-07-29 17:17 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Sergio Lopez @ 2026-07-29  1:42 UTC (permalink / raw)
  To: Paolo Bonzini, kvm, linux-kernel; +Cc: Sergio Lopez

After 28e39181 ("drm/gem-shmem: Track folio accessed/dirty status in
mmap") was merged, a guest write to an unpopulated PTE from a mapping
backed by a DRM GEM BO triggers a VM exit with EFAULT, with
hva_to_pfn_remapped setting p_pfn to KVM_PFN_ERR_RO_FAULT.

This happens because that commit implements pfn_mkwrite for
drm_gem_shmem_vm_ops. With that function present, vma_wants_writenotify
returns true in vma_set_page_prot, clearing VM_SHARED and leading to the
entry to be installed as read-only. This is done on purpose so the
fault handler gets nofitied when the entry is going to be written.

In KVM, hva_to_pfn_remapped calls to fixup_user_fault to trigger the
fault handler but, as seen above, this one might install a read-only PTE
even with FAULT_FLAG_WRITE present in fault_flags. The check a the end
of hvf_to_pfn_remapped notices that the entry is not writable despite
this being a write fault and sets p_pfn to KVM_PFN_ERR_RO_FAULT.

To address this issue, have hva_to_pfn_remapped issue a second
fixup_user_fault call when needed for write-upgrading the PTE.

Signed-off-by: Sergio Lopez <slp@redhat.com>
---
 virt/kvm/kvm_main.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 45e784462ec6..a398f4677854 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2958,6 +2958,8 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
 		 * not call the fault handler, so do it here.
 		 */
 		bool unlocked = false;
+		bool write_upgrade = false;
+do_write_upgrade:
 		r = fixup_user_fault(current->mm, kfp->hva,
 				     (write_fault ? FAULT_FLAG_WRITE : 0),
 				     &unlocked);
@@ -2969,6 +2971,17 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
 		r = follow_pfnmap_start(&args);
 		if (r)
 			return r;
+
+		if (write_fault && !args.writable && !write_upgrade) {
+			/*
+			 * VM_PFNMAP fault handlers may install read-only PTEs
+			 * via vmf_insert_pfn(), deferring the write upgrade to
+			 * a second fault. Trigger that upgrade now.
+			 */
+			write_upgrade = true;
+			follow_pfnmap_end(&args);
+			goto do_write_upgrade;
+		}
 	}
 
 	if (write_fault && !args.writable) {
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-29 17:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-29  1:42 [PATCH] KVM: have hva_to_pfn_remapped write-upgrade PTEs Sergio Lopez
2026-07-29 17:17 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.