drm/panthor: Track the number of mmap on a BO

This will be used to order things by reclaimability.

v2:
- Fix refcounting

v3:
- Fix refcounting (again)

v4:
- Collect R-b

v5:
- Collect R-b

v6:
- Warn on is_imported() (not supposed to happen since mmap is
  redirected to the dmabuf layer) instead of adding a conditional
  in panthor_gem_vm_open()

v7:
- No changes

Reviewed-by: Steven Price <steven.price@arm.com>
Reviewed-by: Liviu Dudau <liviu.dudau@arm.com>
Link: https://patch.msgid.link/20260401134854.2275433-10-boris.brezillon@collabora.com
Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
This commit is contained in:
Boris Brezillon 2026-04-01 15:48:53 +02:00
parent 1013bf5365
commit 3218aae545
2 changed files with 43 additions and 2 deletions

View File

@ -488,6 +488,7 @@ static void panthor_gem_print_info(struct drm_printer *p, unsigned int indent,
drm_printf_indent(p, indent, "vmap_use_count=%u\n",
refcount_read(&bo->cmap.vaddr_use_count));
drm_printf_indent(p, indent, "vaddr=%p\n", bo->cmap.vaddr);
drm_printf_indent(p, indent, "mmap_count=%u\n", refcount_read(&bo->cmap.mmap_count));
}
static int panthor_gem_pin_locked(struct drm_gem_object *obj)
@ -604,6 +605,13 @@ static int panthor_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *v
if (is_cow_mapping(vma->vm_flags))
return -EINVAL;
if (!refcount_inc_not_zero(&bo->cmap.mmap_count)) {
dma_resv_lock(obj->resv, NULL);
if (!refcount_inc_not_zero(&bo->cmap.mmap_count))
refcount_set(&bo->cmap.mmap_count, 1);
dma_resv_unlock(obj->resv);
}
vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
if (should_map_wc(bo))
@ -740,13 +748,43 @@ static vm_fault_t panthor_gem_fault(struct vm_fault *vmf)
return panthor_gem_any_fault(vmf, 0);
}
static void panthor_gem_vm_open(struct vm_area_struct *vma)
{
struct panthor_gem_object *bo = to_panthor_bo(vma->vm_private_data);
drm_WARN_ON(bo->base.dev, drm_gem_is_imported(&bo->base));
drm_WARN_ON(bo->base.dev, !refcount_inc_not_zero(&bo->cmap.mmap_count));
drm_gem_vm_open(vma);
}
static void panthor_gem_vm_close(struct vm_area_struct *vma)
{
struct panthor_gem_object *bo = to_panthor_bo(vma->vm_private_data);
if (drm_gem_is_imported(&bo->base))
goto out;
if (refcount_dec_not_one(&bo->cmap.mmap_count))
goto out;
dma_resv_lock(bo->base.resv, NULL);
if (refcount_dec_and_test(&bo->cmap.mmap_count)) {
/* Nothing to do, pages are reclaimed lazily. */
}
dma_resv_unlock(bo->base.resv);
out:
drm_gem_object_put(&bo->base);
}
static const struct vm_operations_struct panthor_gem_vm_ops = {
.fault = panthor_gem_fault,
#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP
.huge_fault = panthor_gem_any_fault,
#endif
.open = drm_gem_vm_open,
.close = drm_gem_vm_close,
.open = panthor_gem_vm_open,
.close = panthor_gem_vm_close,
};
static const struct drm_gem_object_funcs panthor_gem_funcs = {

View File

@ -80,6 +80,9 @@ struct panthor_gem_cpu_map {
/** @vaddr_use_count: Number of active vmap() requests on this GEM */
refcount_t vaddr_use_count;
/** @mmap_count: Number of active mmap() requests on this GEM */
refcount_t mmap_count;
};
/**