Re: [PATCH v4 5/6] drm/gem-shmem: Track folio accessed/dirty status in mmap

Thomas Zimmermann
2026-03-13T08:33:47Z

[-- Attachment #1: Type: text/plain, Size: 4004 bytes --]

Hi

Am 12.03.26 um 18:46 schrieb Biju Das:
> Hi Tommaso,
>
>> -----Original Message-----
>> From: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
>> Sent: 12 March 2026 17:37
>> Subject: Re: [PATCH v4 5/6] drm/gem-shmem: Track folio accessed/dirty status in mmap
>>
>> Hi Thomas,
>> Thanks for your patch.
>>
>> I'm working on DSI support for RZ/G3E from this morning rebasing on top of next-20260311 I'm seeing
>> that weston hang on my side:
>>
>> Reverting this patch fix the issue.
>> (git revert 28e3918179aa)

Just a guess, but maybe vm_file is NULL. The attached patch should
handle this. Could either of you please test?

Best regards
Thomas

>>
>> I'm wondering if anyone encountered this issue?
>> Thanks in advance.
>
> I am also seeing same issue on RZ/G3L with weston.
>
> Cheers,
> Biju
>
>
>
>> Kind Regards,
>> Tommaso
>>
>> On Fri, Feb 27, 2026 at 12:42:10PM +0100, Thomas Zimmermann wrote:
>>> Invoke folio_mark_accessed() in mmap page faults to add the folio to
>>> the memory manager's LRU list. Userspace invokes mmap to get the
>>> memory for software rendering. Compositors do the same when creating
>>> the final on-screen image, so keeping the pages in LRU makes sense.
>>> Avoids paging out graphics buffers when under memory pressure.
>>>
>>> In pfn_mkwrite, further invoke the folio_mark_dirty() to add the folio
>>> for writeback should the underlying file be paged out from system memory.
>>> This rarely happens in practice, yet it would corrupt the buffer content.
>>>
>>> This has little effect on a system's hardware-accelerated rendering,
>>> which only mmaps for an initial setup of textures, meshes, shaders, etc.
>>>
>>> v4:
>>> - test for VM_FAULT_NOPAGE before marking folio as accessed (Boris)
>>> - test page-array bounds in mkwrite handler (Boris)
>>> v3:
>>> - rewrite for VM_PFNMAP
>>> v2:
>>> - adapt to changes in drm_gem_shmem_try_mmap_pmd()
>>>
>>> Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
>>> Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
>>> ---
>>> drivers/gpu/drm/drm_gem_shmem_helper.c | 22 ++++++++++++++++++++++
>>> 1 file changed, 22 insertions(+)
>>>
>>> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> b/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> index cefa50eaf7a4..1ab2bbd3860c 100644
>>> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> @@ -598,6 +598,9 @@ static vm_fault_t drm_gem_shmem_fault(struct vm_fault *vmf)
>>> if (ret != VM_FAULT_NOPAGE)
>>> ret = vmf_insert_pfn(vma, vmf->address, pfn);
>>>
>>> + if (ret == VM_FAULT_NOPAGE)
>>> + folio_mark_accessed(folio);
>>> +
>>> out:
>>> dma_resv_unlock(obj->resv);
>>>
>>> @@ -638,10 +641,29 @@ static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
>>> drm_gem_vm_close(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]));
>>> +
>>> + return 0;
>>> +}
>>> +
>>> const struct vm_operations_struct drm_gem_shmem_vm_ops = {
>>> .fault = drm_gem_shmem_fault,
>>> .open = drm_gem_shmem_vm_open,
>>> .close = drm_gem_shmem_vm_close,
>>> + .pfn_mkwrite = drm_gem_shmem_pfn_mkwrite,
>>> };
>>> EXPORT_SYMBOL_GPL(drm_gem_shmem_vm_ops);
>>>
>>> --
>>> 2.52.0
>>>

--
--
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)

[-- Attachment #2: 0001-test-for-vm_file-before-updating-file-access-time.patch --]
[-- Type: text/x-patch, Size: 898 bytes --]

From e64346c6b3d946e4424066d888cb41fe60eb9d88 Mon Sep 17 00:00:00 2001
From: Thomas Zimmermann <tzimmermann@suse.de>
Date: Fri, 13 Mar 2026 09:29:26 +0100
Subject: [PATCH] test for vm_file before updating file access time

---
drivers/gpu/drm/drm_gem_shmem_helper.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index 4500deef4127..7fad72cc01dc 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -656,7 +656,8 @@ static vm_fault_t drm_gem_shmem_pfn_mkwrite(struct vm_fault *vmf)
if (drm_WARN_ON(obj->dev, !shmem->pages || page_offset >= num_pages))
return VM_FAULT_SIGBUS;

- file_update_time(vma->vm_file);
+ if (vma->vm_file)
+ file_update_time(vma->vm_file);

folio_mark_dirty(page_folio(shmem->pages[page_offset]));

--
2.53.0