Skip to contentSkip to conversation

drm/shmem_helper: Make sure PMD entries get the writeable upgrade

PATCH In review Boris Brezillon 5 participants 6 messages Mar 20 – Apr 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/shmem_helper: Make sure PMD entries get the writeable upgrade

Unlike PTEs which are automatically upgraded to writeable entries if .pfn_mkwrite() returns 0, the PMD upgrades go through .huge_fault(), and we currently pretend to have handled the make-writeable request even though we only ever map things read-only. Make sure we pass the proper "write" info to vmf_insert_pfn_pmd() in that case.

This also means we have to record the mkwrite event in the .huge_fault() path now. Move the dirty tracking logic to a drm_gem_shmem_record_mkwrite() helper so it can also be called from drm_gem_shmem_pfn_mkwrite().

Note that this wasn't a problem before commit 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap"), because the pgprot were not lowered to read-only before this commit (see the vma_wants_writenotify() in vma_set_page_prot()).

Fixes 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc Biju Das <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tommaso Merciai <[email protected]>

---

This patch is based on drm-tip [2], because that's the only branch that has both [1] and the dirty tracking changes that live in drm-misc-next.

Also added the THP maintainers in Cc, so I can hopefully get some feedback on the fix. For instance, I'm still unsure drm_gem_shmem_pfn_mkwrite() is race-free (do we need some locking there? should we call folio_mark_dirty_lock()? should we call the fault handler directly from there and have all the dirty tracking in this .[huge_]fault path?).

[1]https://yhbt.net/lore/dri-devel/[email protected]/ [2]https://gitlab.freedesktop.org/drm/tip

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 46 ++++++++++++++++++--------
 1 file changed, 32 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +32 -14
index 2062ca607833..545933c7f712 100644
@@ -554,6 +554,21 @@ int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
 }
 EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
 
+static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct drm_gem_object *obj = vma->vm_private_data;
+	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
+	loff_t num_pages = obj->size >> PAGE_SHIFT;
+	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
+
+	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
+		return;
+
+	file_update_time(vma->vm_file);
+	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
+}
+
 static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
 				 unsigned long pfn)
 {
@@ -566,8 +581,23 @@ 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;
-			return vmf_insert_pfn_pmd(vmf, pfn, false);
+
+			/* 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;
 		}
 #endif
 	}
@@ -655,19 +685,7 @@ static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
 
 static vm_fault_t drm_gem_shmem_pfn_mkwrite(struct vm_fault *vmf)
 {
-	struct vm_area_struct *vma = vmf->vma;
-	struct drm_gem_object *obj = vma->vm_private_data;
-	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	loff_t num_pages = obj->size >> PAGE_SHIFT;
-	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
-
-	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
-		return VM_FAULT_SIGBUS;
-
-	file_update_time(vma->vm_file);
-
-	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
-
+	drm_gem_shmem_record_mkwrite(vmf);
 	return 0;
 }
 
Signature · 1 line
2.53.0
Tested-by on v1 1/1
#1 lore raw

Hi Boris,
Thanks for your patch.

Quoted 21 lines · Boris Brezillon

Unlike PTEs which are automatically upgraded to writeable entries if .pfn_mkwrite() returns 0, the PMD upgrades go through .huge_fault(), and we currently pretend to have handled the make-writeable request even though we only ever map things read-only. Make sure we pass the proper "write" info to vmf_insert_pfn_pmd() in that case.

This also means we have to record the mkwrite event in the .huge_fault() path now. Move the dirty tracking logic to a drm_gem_shmem_record_mkwrite() helper so it can also be called from drm_gem_shmem_pfn_mkwrite().

Note that this wasn't a problem before commit 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap"), because the pgprot were not lowered to read-only before this commit (see the vma_wants_writenotify() in vma_set_page_prot()).

Fixes 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc Biju Das <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tommaso Merciai <[email protected]>
Quoted 5 lines

---

This patch is based on drm-tip [2], because that's the only branch that has both [1] and the dirty tracking changes that live in drm-misc-next.

Tested on RZ/G3E, this fix the issue on my side.
Thanks for your work.

Kind Regards,
Tommaso

Quoted 89 lines

Also added the THP maintainers in Cc, so I can hopefully get some feedback on the fix. For instance, I'm still unsure drm_gem_shmem_pfn_mkwrite() is race-free (do we need some locking there? should we call folio_mark_dirty_lock()? should we call the fault handler directly from there and have all the dirty tracking in this .[huge_]fault path?).

[1]https://yhbt.net/lore/dri-devel/[email protected]/ [2]https://gitlab.freedesktop.org/drm/tip

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 46 ++++++++++++++++++--------
 1 file changed, 32 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +32 -14
index 2062ca607833..545933c7f712 100644
@@ -554,6 +554,21 @@ int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
 }
 EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
 
+static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct drm_gem_object *obj = vma->vm_private_data;
+	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
+	loff_t num_pages = obj->size >> PAGE_SHIFT;
+	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
+
+	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
+		return;
+
+	file_update_time(vma->vm_file);
+	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
+}
+
 static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
 				 unsigned long pfn)
 {
@@ -566,8 +581,23 @@ 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;
-			return vmf_insert_pfn_pmd(vmf, pfn, false);
+
+			/* 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;
 		}
 #endif
 	}
@@ -655,19 +685,7 @@ static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
 
 static vm_fault_t drm_gem_shmem_pfn_mkwrite(struct vm_fault *vmf)
 {
-	struct vm_area_struct *vma = vmf->vma;
-	struct drm_gem_object *obj = vma->vm_private_data;
-	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	loff_t num_pages = obj->size >> PAGE_SHIFT;
-	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
-
-	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
-		return VM_FAULT_SIGBUS;
-
-	file_update_time(vma->vm_file);
-
-	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
-
+	drm_gem_shmem_record_mkwrite(vmf);
 	return 0;
 }
 
Signature · 1 line
2.53.0
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Boris Brezillon

Am 20.03.26 um 16:19 schrieb Boris Brezillon:

Quoted 16 lines

Unlike PTEs which are automatically upgraded to writeable entries if .pfn_mkwrite() returns 0, the PMD upgrades go through .huge_fault(), and we currently pretend to have handled the make-writeable request even though we only ever map things read-only. Make sure we pass the proper "write" info to vmf_insert_pfn_pmd() in that case.

This also means we have to record the mkwrite event in the .huge_fault() path now. Move the dirty tracking logic to a drm_gem_shmem_record_mkwrite() helper so it can also be called from drm_gem_shmem_pfn_mkwrite().

Note that this wasn't a problem before commit 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap"), because the pgprot were not lowered to read-only before this commit (see the vma_wants_writenotify() in vma_set_page_prot()).

Quoted 5 lines
Fixes 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc Biju Das <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tommaso Merciai <[email protected]>
Quoted 91 lines

---

This patch is based on drm-tip [2], because that's the only branch that has both [1] and the dirty tracking changes that live in drm-misc-next.

Also added the THP maintainers in Cc, so I can hopefully get some feedback on the fix. For instance, I'm still unsure drm_gem_shmem_pfn_mkwrite() is race-free (do we need some locking there? should we call folio_mark_dirty_lock()? should we call the fault handler directly from there and have all the dirty tracking in this .[huge_]fault path?).

[1]https://yhbt.net/lore/dri-devel/[email protected]/ [2]https://gitlab.freedesktop.org/drm/tip

---
  drivers/gpu/drm/drm_gem_shmem_helper.c | 46 ++++++++++++++++++--------
  1 file changed, 32 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +32 -14
index 2062ca607833..545933c7f712 100644
@@ -554,6 +554,21 @@ int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
  }
  EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
 
+static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct drm_gem_object *obj = vma->vm_private_data;
+	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
+	loff_t num_pages = obj->size >> PAGE_SHIFT;
+	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
+
+	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
+		return;
+
+	file_update_time(vma->vm_file);
+	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
+}
+
  static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
  				 unsigned long pfn)
  {
@@ -566,8 +581,23 @@ 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;
-			return vmf_insert_pfn_pmd(vmf, pfn, false);
+
+			/* 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;
  		}
  #endif
  	}
@@ -655,19 +685,7 @@ static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
 
  static vm_fault_t drm_gem_shmem_pfn_mkwrite(struct vm_fault *vmf)
  {
-	struct vm_area_struct *vma = vmf->vma;
-	struct drm_gem_object *obj = vma->vm_private_data;
-	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	loff_t num_pages = obj->size >> PAGE_SHIFT;
-	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
-
-	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
-		return VM_FAULT_SIGBUS;
-
-	file_update_time(vma->vm_file);
-
-	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
-
+	drm_gem_shmem_record_mkwrite(vmf);
  	return 0;
  }
 
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
Tested-by on v1 1/1
#3 lore raw
In reply to Boris Brezillon

Hi Boris Brezillon,

Quoted 19 lines

-----Original Message-----
From: Boris Brezillon <[email protected]>
Sent: 20 March 2026 15:19
Subject: [PATCH] drm/shmem_helper: Make sure PMD entries get the writeable upgrade

Unlike PTEs which are automatically upgraded to writeable entries if
.pfn_mkwrite() returns 0, the PMD upgrades go through .huge_fault(), and we currently pretend to have handled the make-writeable request even though we only ever map things read-only. Make sure we pass the proper "write" info to vmf_insert_pfn_pmd() in that case.

This also means we have to record the mkwrite event in the .huge_fault() path now. Move the dirty tracking logic to a
drm_gem_shmem_record_mkwrite() helper so it can also be called from drm_gem_shmem_pfn_mkwrite().

Note that this wasn't a problem before commit 28e3918179aa
("drm/gem-shmem: Track folio accessed/dirty status in mmap"), because the pgprot were not lowered to read-only before this commit (see the
vma_wants_writenotify() in vma_set_page_prot()).

Quoted 5 lines
Fixes 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc Biju Das <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tommaso Merciai <[email protected]>

Thanks, weston is now back on RZ/G3L SMARC EVK with mesa 24.0.7 Panfrost

Cheers,
Biju

Quoted 89 lines

---

This patch is based on drm-tip [2], because that's the only branch that has both [1] and the dirty tracking changes that live in drm-misc-next.

Also added the THP maintainers in Cc, so I can hopefully get some feedback on the fix. For instance, I'm still unsure
drm_gem_shmem_pfn_mkwrite() is race-free (do we need some locking there? should we call folio_mark_dirty_lock()? should we call the fault handler directly from there and have all the dirty tracking in this .[huge_]fault path?).

[1]https://yhbt.net/lore/dri-devel/[email protected]/ [2]https://gitlab.freedesktop.org/drm/tip

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 46 ++++++++++++++++++--------
 1 file changed, 32 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +32 -15
index 2062ca607833..545933c7f712 100644
@@ -554,6 +554,21 @@ int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,  }
 EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
 
+static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf) {
+	struct vm_area_struct *vma = vmf->vma;
+	struct drm_gem_object *obj = vma->vm_private_data;
+	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
+	loff_t num_pages = obj->size >> PAGE_SHIFT;
+	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset
+within VMA */
+
+	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
+		return;
+
+	file_update_time(vma->vm_file);
+	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
+}
+
 static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
 				 unsigned long pfn)
 {
@@ -566,8 +581,23 @@ 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;
-			return vmf_insert_pfn_pmd(vmf, pfn, false);
+
+			/* 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;
 		}
 #endif
 	}
@@ -655,19 +685,7 @@ static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
 
 static vm_fault_t drm_gem_shmem_pfn_mkwrite(struct vm_fault *vmf)  {
-	struct vm_area_struct *vma = vmf->vma;
-	struct drm_gem_object *obj = vma->vm_private_data;
-	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	loff_t num_pages = obj->size >> PAGE_SHIFT;
-	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
-
-	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
-		return VM_FAULT_SIGBUS;
-
-	file_update_time(vma->vm_file);
-
-	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
-
+	drm_gem_shmem_record_mkwrite(vmf);
 	return 0;
 }
 
--

2.53.0

Reviewed-by on v1 1/1
#4 lore raw
In reply to Boris Brezillon

Hi Boris,

Quoted 51 lines · Boris Brezillon

Unlike PTEs which are automatically upgraded to writeable entries if .pfn_mkwrite() returns 0, the PMD upgrades go through .huge_fault(), and we currently pretend to have handled the make-writeable request even though we only ever map things read-only. Make sure we pass the proper "write" info to vmf_insert_pfn_pmd() in that case.

This also means we have to record the mkwrite event in the .huge_fault() path now. Move the dirty tracking logic to a drm_gem_shmem_record_mkwrite() helper so it can also be called from drm_gem_shmem_pfn_mkwrite().

Note that this wasn't a problem before commit 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap"), because the pgprot were not lowered to read-only before this commit (see the vma_wants_writenotify() in vma_set_page_prot()).

Fixes 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc Biju Das <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tommaso Merciai <[email protected]>

---

This patch is based on drm-tip [2], because that's the only branch that has both [1] and the dirty tracking changes that live in drm-misc-next.

Also added the THP maintainers in Cc, so I can hopefully get some feedback on the fix. For instance, I'm still unsure drm_gem_shmem_pfn_mkwrite() is race-free (do we need some locking there? should we call folio_mark_dirty_lock()? should we call the fault handler directly from there and have all the dirty tracking in this .[huge_]fault path?).

[1]https://yhbt.net/lore/dri-devel/[email protected]/ [2]https://gitlab.freedesktop.org/drm/tip

---
  drivers/gpu/drm/drm_gem_shmem_helper.c | 46 ++++++++++++++++++--------
  1 file changed, 32 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +3 -0
index 2062ca607833..545933c7f712 100644
@@ -554,6 +554,21 @@ int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
  }
  EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
 
+static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf)
+{
+	struct vm_area_struct *vma = vmf->vma;

drivers/gpu/drm/drm_gem_shmem_helper.c int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, · line 569

Quoted 10 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +10 -0
+	struct drm_gem_object *obj = vma->vm_private_data;
+	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
+	loff_t num_pages = obj->size >> PAGE_SHIFT;
+	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
+
+	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
+		return;
+
+	file_update_time(vma->vm_file);
+	folio_mark_dirty(page_folio(shmem->pages[page_offset]));

Unless we're sure the folio can't be truncated by another CPU, maybe we should use folio_mark_dirty_lock() here. This is what's done for pages (not PFNs) in mm/memory.c. Let's wait and see how it goes without locking for now.

Regards,
Loïc

Quoted 51 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +19 -14
+}
+
  static vm_fault_t try_insert_pfn(struct vm_fault *vmf, unsigned int order,
  				 unsigned long pfn)
  {
@@ -566,8 +581,23 @@ 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;
-			return vmf_insert_pfn_pmd(vmf, pfn, false);
+
+			/* 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;
  		}
  #endif
  	}
@@ -655,19 +685,7 @@ static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
 
  static vm_fault_t drm_gem_shmem_pfn_mkwrite(struct vm_fault *vmf)
  {
-	struct vm_area_struct *vma = vmf->vma;
-	struct drm_gem_object *obj = vma->vm_private_data;
-	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	loff_t num_pages = obj->size >> PAGE_SHIFT;
-	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
-
-	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
-		return VM_FAULT_SIGBUS;
-
-	file_update_time(vma->vm_file);
-
-	folio_mark_dirty(page_folio(shmem->pages[page_offset]));
-
+	drm_gem_shmem_record_mkwrite(vmf);
  	return 0;
  }
 
#5 lore raw
Quoted 51 lines · On Fri, 3 Apr 2026 09:57:53 +0200 Loïc Molinari <[email protected]>…

Hi Boris,

Quoted 48 lines · Boris Brezillon

Unlike PTEs which are automatically upgraded to writeable entries if .pfn_mkwrite() returns 0, the PMD upgrades go through .huge_fault(), and we currently pretend to have handled the make-writeable request even though we only ever map things read-only. Make sure we pass the proper "write" info to vmf_insert_pfn_pmd() in that case.

This also means we have to record the mkwrite event in the .huge_fault() path now. Move the dirty tracking logic to a drm_gem_shmem_record_mkwrite() helper so it can also be called from drm_gem_shmem_pfn_mkwrite().

Note that this wasn't a problem before commit 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap"), because the pgprot were not lowered to read-only before this commit (see the vma_wants_writenotify() in vma_set_page_prot()).

Fixes 28e3918179aa ("drm/gem-shmem: Track folio accessed/dirty status in mmap")
Cc Biju Das <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc Tommaso Merciai <[email protected]>

---

This patch is based on drm-tip [2], because that's the only branch that has both [1] and the dirty tracking changes that live in drm-misc-next.

Also added the THP maintainers in Cc, so I can hopefully get some feedback on the fix. For instance, I'm still unsure drm_gem_shmem_pfn_mkwrite() is race-free (do we need some locking there? should we call folio_mark_dirty_lock()? should we call the fault handler directly from there and have all the dirty tracking in this .[huge_]fault path?).

[1]https://yhbt.net/lore/dri-devel/[email protected]/ [2]https://gitlab.freedesktop.org/drm/tip

---
  drivers/gpu/drm/drm_gem_shmem_helper.c | 46 ++++++++++++++++++--------
  1 file changed, 32 insertions(+), 14 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
index 2062ca607833..545933c7f712 100644
@@ -554,6 +554,21 @@ int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
  }
  EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
 
Quoted 10 lines

drivers/gpu/drm/drm_gem_shmem_helper.c int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, · line 566

Quoted 10 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +10 -0
+static void drm_gem_shmem_record_mkwrite(struct vm_fault *vmf)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct drm_gem_object *obj = vma->vm_private_data;
+	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
+	loff_t num_pages = obj->size >> PAGE_SHIFT;
+	pgoff_t page_offset = vmf->pgoff - vma->vm_pgoff; /* page offset within VMA */
+
+	if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
+		return;

For full transparency, I'd like to mention the review bot complained [1] about us not propagating the error to .pfn_mkwrite() as was done before this patch. In practice, I don't think it matters much: if the pages are gone and .pfn_mkwrite() is called, we're in trouble anyway, because a read-only PTE pointing to this missing page exists already, and it won't be removed if we return an error, it just won't be updated to read-write.

Quoted 6 lines

drivers/gpu/drm/drm_gem_shmem_helper.c int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, · line 569

Quoted 3 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +3 -0
+
+	file_update_time(vma->vm_file);
+	folio_mark_dirty(page_folio(shmem->pages[page_offset]));  

Unless we're sure the folio can't be truncated by another CPU, maybe we should use folio_mark_dirty_lock() here.

In practice, we control when the file is truncated (drm_gem_shmem_purge_locked()), and before we do that, we make sure to kill all the CPU mappings (drm_vma_node_unmap() called before shmem_truncate_range()). So I'd say we're good WRT this particular race.

Quoted 3 lines

This is what's done for pages
(not PFNs) in mm/memory.c. Let's wait and see how it goes without locking for now.

I agree, let's see how it goes and revisit later if needed.

Quoted 2 lines

Thanks for the review. The patch has been queued to drm-misc-next-fixes.

Regards,

Boris

[1]https://lore.gitlab.freedesktop.org/drm-ai-reviews/review-patch1-20260320151914.586945-1-boris.brezillon@collabora.com/