From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Alex Williamson <alex@shazbot.org>,
bcm-kernel-feedback-list@broadcom.com,
Boris Brezillon <boris.brezillon@collabora.com>,
Christian Koenig <christian.koenig@amd.com>,
David Hildenbrand <david@kernel.org>,
dri-devel@lists.freedesktop.org, Fei Li <fei1.li@intel.com>,
Huang Rui <ray.huang@amd.com>,
linux-mm@kvack.org, linux-s390@vger.kernel.org,
Michal Hocko <mhocko@suse.com>, Peter Xu <peterx@redhat.com>,
Sergio Lopez <slp@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Thomas Zimmermann <tzimmermann@suse.de>,
stable@vger.kernel.org
Subject: [PATCH v2 2/6] drm/shmem_helper: use vmf_insert_pfn_mkwrite()
Date: Tue, 4 Aug 2026 14:05:24 +0200 [thread overview]
Message-ID: <20260804120529.1730187-3-pbonzini@redhat.com> (raw)
In-Reply-To: <20260804120529.1730187-1-pbonzini@redhat.com>
This ensures that KVM or VFIO correctly see a writable PTE when
they request one. Otherwise, a guest write to an unpopulated
PTE from a mapping backed by a DRM GEM BO triggers a VM exit
with EFAULT.
The code actually is simpler, because the same logic already
applied to the hugepage mapping case using vmf_insert_pfn_pmd().
Reported-by: Sergio Lopez <slp@redhat.com>
Link: https://lore.kernel.org/kvm/20260729072044.25796-1-slp@redhat.com/
Tested-by: Sergio Lopez <slp@redhat.com>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Fixes: 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc: stable@vger.kernel.org
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
drivers/gpu/drm/drm_gem_shmem_helper.c | 38 ++++++++++++++------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index c989459eb215..c81be3e97317 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -589,11 +589,25 @@ static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf)
folio_mark_dirty(page_folio(shmem->pages[page_offset]));
}
+/*
+ * Because the vm_ops have a .pfn_mkwrite() callback, vma_set_page_prot()
+ * has cleared the write bit from vma->vm_page_prot. vmf_insert_pfn()
+ * would install a read-only entry even for a write fault, relying on a
+ * second fault to reach .pfn_mkwrite() and upgrade it, but that second
+ * fault never happens for fixup_user_fault() callers that directly
+ * walk the page tables with follow_pfnmap_start(). To ensure that
+ * they don't see the read-only entry, pass FAULT_FLAG_WRITE info down
+ * to install a writable entry right away. Because .pfn_mkwrite() is
+ * not invoked, record the write afterwards.
+ */
static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
unsigned long pfn)
{
+ bool write = vmf->flags & FAULT_FLAG_WRITE;
+ vm_fault_t ret = VM_FAULT_FALLBACK;
+
if (!order) {
- return vmf_insert_pfn(vmf->vma, vmf->address, pfn);
+ ret = vmf_insert_pfn_mkwrite(vmf->vma, vmf->address, pfn, write);
#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
} else if (order == PMD_ORDER) {
unsigned long paddr = pfn << PAGE_SHIFT;
@@ -601,27 +615,15 @@ static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
if (aligned &&
folio_test_pmd_mappable(page_folio(pfn_to_page(pfn)))) {
- vm_fault_t ret;
-
pfn &= PMD_MASK >> PAGE_SHIFT;
-
- /* Unlike PTEs which are automatically upgraded to
- * writeable entries, the PMD upgrades go through
- * .huge_fault(). Make sure we pass the "write" info
- * along in that case.
- * This also means we have to record the write fault
- * here, instead of in .pfn_mkwrite().
- */
- ret = vmf_insert_pfn_pmd(vmf, pfn,
- vmf->flags & FAULT_FLAG_WRITE);
- if (ret == VM_FAULT_NOPAGE && (vmf->flags & FAULT_FLAG_WRITE))
- drm_gem_shmem_record_mkwrite(vmf);
-
- return ret;
+ ret = vmf_insert_pfn_pmd(vmf, pfn, write);
}
#endif
}
- return VM_FAULT_FALLBACK;
+
+ if (ret == VM_FAULT_NOPAGE && write)
+ drm_gem_shmem_record_mkwrite(vmf);
+ return ret;
}
static vm_fault_t drm_gem_shmem_any_fault(struct vm_fault *vmf, unsigned int order)
--
2.55.0
next prev parent reply other threads:[~2026-08-04 14:32 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-04 12:05 [PATCH v2 0/6] mm, drm: fix interaction of .pfn_mkwrite() with fixup_user_fault() Paolo Bonzini
2026-08-04 12:05 ` [PATCH v2 1/6] mm: export vmf_insert_pfn_prot_mkwrite(), change variants to inline Paolo Bonzini
2026-08-04 12:05 ` Paolo Bonzini [this message]
2026-08-04 14:15 ` [PATCH v2 2/6] drm/shmem_helper: use vmf_insert_pfn_mkwrite() Boris Brezillon
2026-08-04 14:18 ` Boris Brezillon
2026-08-04 14:34 ` Paolo Bonzini
2026-08-04 14:42 ` Boris Brezillon
2026-08-05 6:08 ` Paolo Bonzini
2026-08-05 8:34 ` Boris Brezillon
2026-08-04 12:05 ` [PATCH v2 3/6] drm/ttm, drm/vmwgfx: directly create writable PTEs when mkwrite is in use Paolo Bonzini
2026-08-06 23:32 ` Peter Xu
2026-08-04 12:05 ` [PATCH v2 4/6] kvm: apply VM_READ/VM_WRITE checks to all VMA types Paolo Bonzini
2026-08-04 21:15 ` Sean Christopherson
2026-08-04 12:05 ` [PATCH v2 5/6] mm: pull writability check to follow_pfnmap_start() Paolo Bonzini
2026-08-04 12:05 ` [PATCH v2 6/6] kvm: return -EFAULT for writes to !VM_WRITE IO mappings Paolo Bonzini
2026-08-04 21:08 ` Sean Christopherson
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=20260804120529.1730187-3-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=alex@shazbot.org \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=boris.brezillon@collabora.com \
--cc=christian.koenig@amd.com \
--cc=david@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=fei1.li@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-s390@vger.kernel.org \
--cc=mhocko@suse.com \
--cc=peterx@redhat.com \
--cc=ray.huang@amd.com \
--cc=seanjc@google.com \
--cc=slp@redhat.com \
--cc=stable@vger.kernel.org \
--cc=tzimmermann@suse.de \
/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