drm/panthor: Add a GEM shrinker
Subsystems (3)
Boris Brezillon
maintainerv6 cover drm/panthor: Add a GEM shrinker
Hello,
This is an attempt at adding a GEM shrinker to panthor so the system can finally reclaim GPU memory.
This implementation is losely based on the MSM shrinker (which is why I added the MSM maintainers in Cc), and it's relying on the drm_gpuvm eviction/validation infrastructure.
I've only done very basic IGT-based [1] and chromium-based (opening a lot of tabs on Aquarium until the system starts reclaiming+swapping out GPU buffers) testing, but I'm posting this early so I can get preliminary feedback on the implementation. If someone knows about better tools/ways to test the shrinker, please let me know.
A few words about some design/implementation choices:
- No MADVISE support because I want to see if we can live with just transparent reclaim
- We considered basing this implementation on the generic shrinker work started by Dmitry [2], but
1. with the activeness/idleness tracking happening at the VM granularity, having per-BO LRUs would caused a lot of list_move()s that are not really needed (the VM as a whole become active/idle, we can track individual BOs)
2. Thomas Zimmermann recently suggested that we should have our own GEM implementation instead of trying to add this extra reclaim complexity to gem-shmem. There are some plans to create a gem-uma (Unified Memory Architecture) lib that would do more than gem-shmem but in a way that doesn't force all its users to pay the overhead (size overhead of the gem object, mostly) for features they don't use. Patch "Part ways with drm_gem_shmem_object" is showing what this component-based lib API could look like if it were to be extracted
- At the moment we only support swapout, but we could add an extra flag to specify when buffer content doesn't need to be preserved to avoid the swapout/swapin dance. First candidate for this DISCARD_ON_RECLAIM flag would probably be the tiler heap chunks.
- Reclaim uses _try_lock() all the way because of the various lock order inversions between the reclaim path and submission paths. That means we don't try very hard to reclaim hot GPU buffers, but the locking is such a mess that I don't really see a better option to be honest.
Changes in v2:
- No fundamental changes in this v2, since the feedback I got were more focused on bugs than the overall approach. Check the changelog in each patch for more details.
Changes in v3:
- Mostly fixes (see the changelog in each patch)
Changes in v4:
- Only fixes (see the changelog in each patch)
Changes in v5:
- Only fixes (see the changelog in each patch)
Changes in v6:
- Fix huge fault handling and various other things reported by Adrian and the review bot (see the changelog in each patch for more)
Regards,
Boris
[1]https://gitlab.freedesktop.org/bbrezillon/igt-gpu-tools/-/commit/fc76934a5579767d2aabe787d40e38a17c3f4ea4 [2]https://lkml.org/lkml/2024/1/5/665
Akash Goel (1):
drm/panthor: Add a GEM shrinker
Boris Brezillon (8):
drm/gem: Consider GEM object reclaimable if shrinking fails drm/panthor: Move panthor_gems_debugfs_init() to panthor_gem.c drm/panthor: Group panthor_kernel_bo_xxx() helpers drm/panthor: Don't call drm_gpuvm_bo_extobj_add() if the object is
private
drm/panthor: Part ways with drm_gem_shmem_object drm/panthor: Lazily allocate pages on mmap() drm/panthor: Split panthor_vm_prepare_map_op_ctx() to prepare for
reclaim
drm/panthor: Track the number of mmap on a BO
drivers/gpu/drm/drm_gem.c | 10 + drivers/gpu/drm/panthor/Kconfig | 1 - drivers/gpu/drm/panthor/panthor_device.c | 11 +- drivers/gpu/drm/panthor/panthor_device.h | 73 ++ drivers/gpu/drm/panthor/panthor_drv.c | 33 +- drivers/gpu/drm/panthor/panthor_fw.c | 16 +- drivers/gpu/drm/panthor/panthor_gem.c | 1453 ++++++++++++++++++---- drivers/gpu/drm/panthor/panthor_gem.h | 136 +- drivers/gpu/drm/panthor/panthor_mmu.c | 502 ++++++-- drivers/gpu/drm/panthor/panthor_mmu.h | 8 + drivers/gpu/drm/panthor/panthor_sched.c | 9 +- 11 files changed, 1911 insertions(+), 341 deletions(-)
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 1/9 drm/gem: Consider GEM object reclaimable if shrinking fails
If the object wasn't moved to a different LRU after the shrink callback returns, it means the buffer is still reclaimable. Update the remaining counter to reflect that.
v2:
- Collect R-b
v3:
- Collect R-b
v4:
- No changes
v5:
- No changes
v6:
- No changes
--- drivers/gpu/drm/drm_gem.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/gpu/drm/drm_gem.c +10 -0
@@ -1700,6 +1700,16 @@ drm_gem_lru_scan(struct drm_gem_lru *lru,*/ WARN_ON (obj->lru == &still_in_lru);WARN_ON (obj->lru == lru); + }else if (obj->lru == &still_in_lru) { +/* +* If the object wasn't moved and wasn't shrunk either, +* it's still remaining as reclaimable. Note that +* obj->lru is supposed to be checked with the LRU lock +* held for an accurate result, but we don't care about +* accuracy here. Worst thing that could happen is an +* extra scan. +*/ + *remaining += obj->size >>PAGE_SHIFT ; }dma_resv_unlock (obj->resv);
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 2/9 drm/panthor: Move panthor_gems_debugfs_init() to panthor_gem.c
In reply to Boris BrezillonThere's no reason for panthor_drv to know about panthor_gem.c internals, so let's move the GEM debugfs init logic to panthor_gem.c.
v2:
- Collect R-bs
v3:
-No changes
v4:
- No changes
v5:
- No changes
v6:
- No changes
--- drivers/gpu/drm/panthor/panthor_drv.c | 26 +----------------------- drivers/gpu/drm/panthor/panthor_gem.c | 29 +++++++++++++++++++++++++-- drivers/gpu/drm/panthor/panthor_gem.h | 3 +-- 3 files changed, 29 insertions(+), 29 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +1 -25
@@ -1756,34 +1756,10 @@ static const struct file_operations panthor_drm_driver_fops = { };#ifdef CONFIG_DEBUG_FS -static int panthor_gems_show (struct seq_file *m,void *data) -{ -struct drm_info_node *node = m->private; -struct drm_device *dev = node->minor->dev; - struct panthor_device *ptdev = container_of(dev, struct panthor_device, base); - -panthor_gem_debugfs_print_bos (ptdev, m); - -return 0 ; -} - -static struct drm_info_list panthor_debugfs_list[] = { - {"gems" , panthor_gems_show,0 ,NULL }, -}; - -static int panthor_gems_debugfs_init (struct drm_minor *minor) -{ -drm_debugfs_create_files (panthor_debugfs_list, -ARRAY_SIZE (panthor_debugfs_list), - minor->debugfs_root, minor); - -return 0 ; -} -static void panthor_debugfs_init (struct drm_minor *minor) {panthor_mmu_debugfs_init (minor); -panthor_gems_debugfs_init (minor); +panthor_gem_debugfs_init (minor); }#endif
drivers/gpu/drm/panthor/panthor_gem.c +27 -2
@@ -9,6 +9,8 @@#include <linux/err.h> #include <linux/slab.h> +#include <drm/drm_debugfs.h> +#include <drm/drm_file.h> #include <drm/drm_print.h> #include <drm/panthor_drm.h>
@@ -683,8 +685,8 @@ static void panthor_gem_debugfs_bo_print(struct panthor_gem_object *bo, totals->reclaimable += resident_size; } -void panthor_gem_debugfs_print_bos (struct panthor_device *ptdev, -struct seq_file *m) +static void panthor_gem_debugfs_print_bos (struct panthor_device *ptdev, +struct seq_file *m) {struct gem_size_totals totals = {0 };struct panthor_gem_object *bo;
@@ -704,4 +706,27 @@ void panthor_gem_debugfs_print_bos(struct panthor_device *ptdev,seq_printf (m,"Total size: %zd, Total resident: %zd, Total reclaimable: %zd\n" , totals.size, totals.resident, totals.reclaimable); } + +static int panthor_gem_show_bos (struct seq_file *m,void *data) +{ +struct drm_info_node *node = m->private; +struct drm_device *dev = node->minor->dev; +struct panthor_device *ptdev = + container_of(dev, struct panthor_device, base); + +panthor_gem_debugfs_print_bos (ptdev, m); + +return 0 ; +} + +static struct drm_info_list panthor_gem_debugfs_list[] = { + {"gems" , panthor_gem_show_bos,0 ,NULL }, +}; + +void panthor_gem_debugfs_init (struct drm_minor *minor) +{ +drm_debugfs_create_files (panthor_gem_debugfs_list, +ARRAY_SIZE (panthor_gem_debugfs_list), + minor->debugfs_root, minor); +}#endif
drivers/gpu/drm/panthor/panthor_gem.h +1 -2
@@ -203,8 +203,7 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,void panthor_kernel_bo_destroy (struct panthor_kernel_bo *bo);#ifdef CONFIG_DEBUG_FS -void panthor_gem_debugfs_print_bos (struct panthor_device *pfdev, -struct seq_file *m); +void panthor_gem_debugfs_init (struct drm_minor *minor);#endif #endif /* __PANTHOR_GEM_H__ */
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 3/9 drm/panthor: Group panthor_kernel_bo_xxx() helpers
In reply to Boris BrezillonMove all panthor_kernel_bo_xxx() helpers at the end of the file, just before the debugfs init logic. This will make further panthor_gem.c refactoring more readable.
v2:
- Collect R-bs
v3:
- No changes
v4:
- No changes
v5:
- No changes
v6:
- No changes
--- drivers/gpu/drm/panthor/panthor_gem.c | 212 +++++++++++++------------- 1 file changed, 106 insertions(+), 106 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +106 -106
@@ -132,112 +132,6 @@ static void panthor_gem_free_object(struct drm_gem_object *obj)drm_gem_object_put (vm_root_gem); } -/** -* panthor_kernel_bo_destroy() - Destroy a kernel buffer object -* @bo: Kernel buffer object to destroy. If NULL or an ERR_PTR(), the destruction -* is skipped. -*/ -void panthor_kernel_bo_destroy (struct panthor_kernel_bo *bo) -{ -struct panthor_vm *vm; - -if (IS_ERR_OR_NULL (bo)) -return ; - - vm = bo->vm; -panthor_kernel_bo_vunmap (bo); - -drm_WARN_ON (bo->obj->dev, -to_panthor_bo (bo->obj)->exclusive_vm_root_gem !=panthor_vm_root_gem (vm)); -panthor_vm_unmap_range (vm, bo->va_node.start, bo->va_node.size); -panthor_vm_free_va (vm, &bo->va_node); -drm_gem_object_put (bo->obj); -panthor_vm_put (vm); -kfree (bo); -} - -/** -* panthor_kernel_bo_create() - Create and map a GEM object to a VM -* @ptdev: Device. -* @vm: VM to map the GEM to. If NULL, the kernel object is not GPU mapped. -* @size: Size of the buffer object. -* @bo_flags: Combination of drm_panthor_bo_flags flags. -* @vm_map_flags: Combination of drm_panthor_vm_bind_op_flags (only those -* that are related to map operations). -* @gpu_va: GPU address assigned when mapping to the VM. -* If gpu_va == PANTHOR_VM_KERNEL_AUTO_VA, the virtual address will be -* automatically allocated. -* @name: Descriptive label of the BO's contents -* -* Return: A valid pointer in case of success, an ERR_PTR() otherwise. -*/ -struct panthor_kernel_bo * -panthor_kernel_bo_create (struct panthor_device *ptdev,struct panthor_vm *vm, -size_t size,u32 bo_flags,u32 vm_map_flags, -u64 gpu_va,const char *name) -{ -struct drm_gem_shmem_object *obj; -struct panthor_kernel_bo *kbo; -struct panthor_gem_object *bo; -u32 debug_flags =PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL ; -int ret; - -if (drm_WARN_ON (&ptdev->base, !vm)) -return ERR_PTR (-EINVAL ); - - kbo =kzalloc_obj (*kbo); -if (!kbo) -return ERR_PTR (-ENOMEM ); - - obj =drm_gem_shmem_create (&ptdev->base, size); -if (IS_ERR (obj)) { - ret =PTR_ERR (obj); - goto err_free_bo; - } - - bo =to_panthor_bo (&obj->base); - kbo->obj = &obj->base; - bo->flags = bo_flags; - bo->base.map_wc =should_map_wc (bo, vm); - bo->exclusive_vm_root_gem =panthor_vm_root_gem (vm); -drm_gem_object_get (bo->exclusive_vm_root_gem); - bo->base.base.resv = bo->exclusive_vm_root_gem->resv; - -if (vm ==panthor_fw_vm (ptdev)) - debug_flags |=PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED ; - -panthor_gem_kernel_bo_set_label (kbo, name); -panthor_gem_debugfs_set_usage_flags (to_panthor_bo (kbo->obj), debug_flags); - -/* The system and GPU MMU page size might differ, which becomes a -* problem for FW sections that need to be mapped at explicit address -* since our PAGE_SIZE alignment might cover a VA range that's -* expected to be used for another section. -* Make sure we never map more than we need. -*/ - size =ALIGN (size,panthor_vm_page_size (vm)); - ret =panthor_vm_alloc_va (vm, gpu_va, size, &kbo->va_node); -if (ret) - goto err_put_obj; - - ret =panthor_vm_map_bo_range (vm, bo,0 , size, kbo->va_node.start, vm_map_flags); -if (ret) - goto err_free_va; - - kbo->vm =panthor_vm_get (vm); -return kbo; - -err_free_va: -panthor_vm_free_va (vm, &kbo->va_node); - -err_put_obj: -drm_gem_object_put (&obj->base); - -err_free_bo: -kfree (kbo); -return ERR_PTR (ret); -} -static struct sg_table *panthor_gem_prime_map_dma_buf (struct dma_buf_attachment *attach,enum dma_data_direction dir)
@@ -603,6 +497,112 @@ panthor_gem_sync(struct drm_gem_object *obj, u32 type,return 0 ; } +/** +* panthor_kernel_bo_destroy() - Destroy a kernel buffer object +* @bo: Kernel buffer object to destroy. If NULL or an ERR_PTR(), the destruction +* is skipped. +*/ +void panthor_kernel_bo_destroy (struct panthor_kernel_bo *bo) +{ +struct panthor_vm *vm; + +if (IS_ERR_OR_NULL (bo)) +return ; + + vm = bo->vm; +panthor_kernel_bo_vunmap (bo); + +drm_WARN_ON (bo->obj->dev, +to_panthor_bo (bo->obj)->exclusive_vm_root_gem !=panthor_vm_root_gem (vm)); +panthor_vm_unmap_range (vm, bo->va_node.start, bo->va_node.size); +panthor_vm_free_va (vm, &bo->va_node); +drm_gem_object_put (bo->obj); +panthor_vm_put (vm); +kfree (bo); +} + +/** +* panthor_kernel_bo_create() - Create and map a GEM object to a VM +* @ptdev: Device. +* @vm: VM to map the GEM to. If NULL, the kernel object is not GPU mapped. +* @size: Size of the buffer object. +* @bo_flags: Combination of drm_panthor_bo_flags flags. +* @vm_map_flags: Combination of drm_panthor_vm_bind_op_flags (only those +* that are related to map operations). +* @gpu_va: GPU address assigned when mapping to the VM. +* If gpu_va == PANTHOR_VM_KERNEL_AUTO_VA, the virtual address will be +* automatically allocated. +* @name: Descriptive label of the BO's contents +* +* Return: A valid pointer in case of success, an ERR_PTR() otherwise. +*/ +struct panthor_kernel_bo * +panthor_kernel_bo_create (struct panthor_device *ptdev,struct panthor_vm *vm, +size_t size,u32 bo_flags,u32 vm_map_flags, +u64 gpu_va,const char *name) +{ +struct drm_gem_shmem_object *obj; +struct panthor_kernel_bo *kbo; +struct panthor_gem_object *bo; +u32 debug_flags =PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL ; +int ret; + +if (drm_WARN_ON (&ptdev->base, !vm)) +return ERR_PTR (-EINVAL ); + + kbo =kzalloc_obj (*kbo); +if (!kbo) +return ERR_PTR (-ENOMEM ); + + obj =drm_gem_shmem_create (&ptdev->base, size); +if (IS_ERR (obj)) { + ret =PTR_ERR (obj); + goto err_free_bo; + } + + bo =to_panthor_bo (&obj->base); + kbo->obj = &obj->base; + bo->flags = bo_flags; + bo->base.map_wc =should_map_wc (bo, vm); + bo->exclusive_vm_root_gem =panthor_vm_root_gem (vm); +drm_gem_object_get (bo->exclusive_vm_root_gem); + bo->base.base.resv = bo->exclusive_vm_root_gem->resv; + +if (vm ==panthor_fw_vm (ptdev)) + debug_flags |=PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED ; + +panthor_gem_kernel_bo_set_label (kbo, name); +panthor_gem_debugfs_set_usage_flags (to_panthor_bo (kbo->obj), debug_flags); + +/* The system and GPU MMU page size might differ, which becomes a +* problem for FW sections that need to be mapped at explicit address +* since our PAGE_SIZE alignment might cover a VA range that's +* expected to be used for another section. +* Make sure we never map more than we need. +*/ + size =ALIGN (size,panthor_vm_page_size (vm)); + ret =panthor_vm_alloc_va (vm, gpu_va, size, &kbo->va_node); +if (ret) + goto err_put_obj; + + ret =panthor_vm_map_bo_range (vm, bo,0 , size, kbo->va_node.start, vm_map_flags); +if (ret) + goto err_free_va; + + kbo->vm =panthor_vm_get (vm); +return kbo; + +err_free_va: +panthor_vm_free_va (vm, &kbo->va_node); + +err_put_obj: +drm_gem_object_put (&obj->base); + +err_free_bo: +kfree (kbo); +return ERR_PTR (ret); +} + #ifdef CONFIG_DEBUG_FS struct gem_size_totals { size_t size;
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 4/9 drm/panthor: Don't call drm_gpuvm_bo_extobj_add() if the object is private
In reply to Boris Brezillondrm_gpuvm_bo_extobj_add() is a NOP if the object is private, but it forces us to take/release the VM resv lock, so let's do that only when we know the object can be shared.
v3:
- New commit
v4:
- Collect R-bs
v5:
- No changes
v6:
- No changes
--- drivers/gpu/drm/panthor/panthor_mmu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/panthor/panthor_mmu.c +5 -3
@@ -1283,9 +1283,11 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, }/* Insert BO into the extobj list last, when we know nothing can fail. */ -dma_resv_lock (panthor_vm_resv (vm),NULL ); -drm_gpuvm_bo_extobj_add (op_ctx->map.vm_bo); -dma_resv_unlock (panthor_vm_resv (vm)); +if (bo->base.base.resv !=panthor_vm_resv (vm)) { +dma_resv_lock (panthor_vm_resv (vm),NULL ); +drm_gpuvm_bo_extobj_add (op_ctx->map.vm_bo); +dma_resv_unlock (panthor_vm_resv (vm)); + }return 0 ;
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 5/9 drm/panthor: Part ways with drm_gem_shmem_object
In reply to Boris BrezillonWhile drm_gem_shmem_object does most of the job we need it to do, the way sub-resources (pages, sgt, vmap) are handled and their lifetimes gets in the way of BO reclaim. There has been attempts to address that [1], but in the meantime, new gem_shmem users were introduced (accel drivers), and some of them manually free some of these resources. This makes things harder to control/sanitize/validate.
Thomas Zimmerman is not a huge fan of enforcing lifetimes of sub-resources and forcing gem_shmem users to go through new gem_shmem helpers when they need manual control of some sort, and I believe this is a dead end if we don't force users to follow some stricter rules through carefully designed helpers, because there will always be one user doing crazy things with gem_shmem_object internals, which ends up tripping out the common helpers when they are called.
The consensus we reached was that we would be better off forking gem_shmem in panthor. So here we are, parting ways with gem_shmem. The current transition tries to minimize the changes, but there are still some aspects that are different, the main one being that we no longer have a pages_use_count, and pages stays around until the GEM object is destroyed (or when evicted once we've added a shrinker). The sgt also no longer retains pages. This is losely based on how msm does things by the way.
If there's any interest in sharing code (probably with msm, since the panthor shrinker is going to be losely based on the msm implementation), we can always change gears and do that once we have everything working/merged.
[1]https://patchwork.kernel.org/project/dri-devel/patch/[email protected]/
v2:
- Fix refcounting
- Add a _locked suffix to a bunch of functions expecting the resv lock to be held
- Take the lock before releasing resources in panthor_gem_free_object()
v3:
- Use ERR_CAST() to fix an ERR-ptr deref
- Add missing resv_[un]lock() around a panthor_gem_backing_unpin_locked() call
v4:
- Fix an error path in panthor_gem_vmap_get_locked()
- Don't leave bo->base.pages with an ERR_PTR()
- Make panthor_gem_{pin,unpin}[_locked]() more consistent
- Don't fail in panthor_gem_dev_map_get_sgt_locked() if the pages are not allocated
v5:
- Add missing static specifier on our vm_ops
v6:
- Fix huge_fault handling
- s/uint32_t/u32/
- s/drm_dbg_kms/drm_dbg_driver/
- Collect R-bs
--- drivers/gpu/drm/panthor/Kconfig | 1 - drivers/gpu/drm/panthor/panthor_drv.c | 7 +- drivers/gpu/drm/panthor/panthor_fw.c | 16 +- drivers/gpu/drm/panthor/panthor_gem.c | 706 ++++++++++++++++++++---- drivers/gpu/drm/panthor/panthor_gem.h | 62 ++- drivers/gpu/drm/panthor/panthor_mmu.c | 48 +- drivers/gpu/drm/panthor/panthor_sched.c | 9 +- 7 files changed, 675 insertions(+), 174 deletions(-)
drivers/gpu/drm/panthor/Kconfig +0 -1
@@ -8,7 +8,6 @@ config DRM_PANTHOR depends on MMU select DEVFREQ_GOV_SIMPLE_ONDEMAND select DRM_EXEC - select DRM_GEM_SHMEM_HELPER select DRM_GPUVM select DRM_SCHED select IOMMU_IO_PGTABLE_LPAE
drivers/gpu/drm/panthor/panthor_drv.c +4 -3
@@ -21,6 +21,7 @@#include <drm/drm_debugfs.h> #include <drm/drm_drv.h> #include <drm/drm_exec.h> +#include <drm/drm_file.h> #include <drm/drm_ioctl.h> #include <drm/drm_print.h> #include <drm/drm_syncobj.h>
@@ -1578,7 +1579,7 @@ static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data, args->create_flags = bo->flags; args->extra_flags =0 ; -if (drm_gem_is_imported (&bo->base.base)) +if (drm_gem_is_imported (&bo->base)) args->extra_flags |=DRM_PANTHOR_BO_IS_IMPORTED ;drm_gem_object_put (obj);
@@ -1793,8 +1794,7 @@ static const struct drm_driver panthor_drm_driver = { .major = 1, .minor = 8, - .gem_create_object = panthor_gem_create_object, - .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, + .gem_prime_import_sg_table = panthor_gem_prime_import_sg_table, .gem_prime_import = panthor_gem_prime_import, #ifdef CONFIG_DEBUG_FS .debugfs_init = panthor_debugfs_init,
@@ -1944,3 +1944,4 @@ module_exit(panthor_exit);MODULE_AUTHOR ("Panthor Project Developers" );MODULE_DESCRIPTION ("Panthor DRM Driver" );MODULE_LICENSE ("Dual MIT/GPL" ); +MODULE_IMPORT_NS ("DMA_BUF" );
drivers/gpu/drm/panthor/panthor_fw.c +9 -7
@@ -628,7 +628,6 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,u32 cache_mode = hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_MASK ;struct panthor_gem_object *bo;u32 vm_map_flags =0 ; -struct sg_table *sgt;u64 va = hdr.va.start;if (!(hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_WR ))
@@ -666,11 +665,12 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,panthor_fw_init_section_mem (ptdev, section); bo =to_panthor_bo (section->mem->obj); - sgt =drm_gem_shmem_get_pages_sgt (&bo->base); -if (IS_ERR (sgt)) -return PTR_ERR (sgt); -dma_sync_sgtable_for_device (ptdev->base.dev, sgt,DMA_TO_DEVICE ); +/* An sgt should have been requested when the kernel BO was GPU-mapped. */ +if (drm_WARN_ON_ONCE (&ptdev->base, !bo->dmap.sgt)) +return -EINVAL ; + +dma_sync_sgtable_for_device (ptdev->base.dev, bo->dmap.sgt,DMA_TO_DEVICE ); }if (hdr.va.start ==CSF_MCU_SHARED_REGION_START )
@@ -730,8 +730,10 @@ panthor_reload_fw_sections(struct panthor_device *ptdev, bool full_reload)continue ;panthor_fw_init_section_mem (ptdev, section); - sgt =drm_gem_shmem_get_pages_sgt (&to_panthor_bo (section->mem->obj)->base); -if (!drm_WARN_ON (&ptdev->base,IS_ERR_OR_NULL (sgt))) + +/* An sgt should have been requested when the kernel BO was GPU-mapped. */ + sgt =to_panthor_bo (section->mem->obj)->dmap.sgt; +if (!drm_WARN_ON_ONCE (&ptdev->base, !sgt))dma_sync_sgtable_for_device (ptdev->base.dev, sgt,DMA_TO_DEVICE ); } }
drivers/gpu/drm/panthor/panthor_gem.c +586 -120
892 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_gem.h +52 -10
93 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_mmu.c +20 -28
121 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_sched.c +4 -5
@@ -3,7 +3,7 @@#include <drm/drm_drv.h> #include <drm/drm_exec.h> -#include <drm/drm_gem_shmem_helper.h> +#include <drm/drm_file.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> #include <drm/gpu_scheduler.h>
@@ -871,8 +871,7 @@ panthor_queue_get_syncwait_obj(struct panthor_group *group, struct panthor_queueint ret;if (queue->syncwait.kmap) { - bo =container_of (queue->syncwait.obj, - struct panthor_gem_object, base.base); + bo =to_panthor_bo (queue->syncwait.obj); goto out_sync; }
@@ -882,7 +881,7 @@ panthor_queue_get_syncwait_obj(struct panthor_group *group, struct panthor_queueif (drm_WARN_ON (&ptdev->base,IS_ERR_OR_NULL (bo))) goto err_put_syncwait_obj; - queue->syncwait.obj = &bo->base.base; + queue->syncwait.obj = &bo->base; ret =drm_gem_vmap (queue->syncwait.obj, &map);if (drm_WARN_ON (&ptdev->base, ret)) goto err_put_syncwait_obj;
@@ -896,7 +895,7 @@ panthor_queue_get_syncwait_obj(struct panthor_group *group, struct panthor_queue* panthor_gem_sync() is a NOP if map_wc=true, so no need to check * it here. */ - panthor_gem_sync(&bo->base.base, + panthor_gem_sync(&bo->base, DRM_PANTHOR_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE, queue->syncwait.offset, queue->syncwait.sync64 ?
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 6/9 drm/panthor: Lazily allocate pages on mmap()
In reply to Boris BrezillonDefer pages allocation until their first access.
v2:
- Don't deal with FAULT_FLAG_INTERRUPTIBLE
- Make sure bo->backing.pages is never an ERR_PTR()
- Drop a useless vm_fault_t local var
- Fix comment in panthor_gem_fault()
v3:
- Collect R-bs
v4:
- No changes
v5:
- No changes
v6:
- Fix huge_fault handling
--- drivers/gpu/drm/panthor/panthor_gem.c | 118 ++++++++++++++++---------- 1 file changed, 75 insertions(+), 43 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +75 -43
@@ -604,15 +604,6 @@ static int panthor_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vif (is_cow_mapping (vma->vm_flags))return -EINVAL ; -dma_resv_lock (obj->resv,NULL ); - ret =panthor_gem_backing_get_pages_locked (bo); -if (!ret) - ret =panthor_gem_prep_for_cpu_map_locked (bo); -dma_resv_unlock (obj->resv); - -if (ret) -return ret; -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))
@@ -653,35 +644,95 @@ static vm_fault_t insert_page(struct vm_fault *vmf, unsigned int order, struct preturn VM_FAULT_FALLBACK ; } +static vm_fault_t nonblocking_page_setup (struct vm_fault *vmf, +unsigned int order, +pgoff_t page_offset) +{ +struct vm_area_struct *vma = vmf->vma; +struct panthor_gem_object *bo =to_panthor_bo (vma->vm_private_data); +vm_fault_t ret; + +if (!dma_resv_trylock (bo->base.resv)) +return VM_FAULT_RETRY ; + +if (bo->backing.pages) + ret =insert_page (vmf, order, bo->backing.pages[page_offset]); +else + ret =VM_FAULT_RETRY ; + +dma_resv_unlock (bo->base.resv); +return ret; +} + +static vm_fault_t blocking_page_setup (struct vm_fault *vmf,unsigned int order, +struct panthor_gem_object *bo, +pgoff_t page_offset,bool mmap_lock_held) +{ +vm_fault_t ret; +int err; + + err =dma_resv_lock_interruptible (bo->base.resv,NULL ); +if (err) +return mmap_lock_held ?VM_FAULT_NOPAGE :VM_FAULT_RETRY ; + + err =panthor_gem_backing_get_pages_locked (bo); +if (!err) + err =panthor_gem_prep_for_cpu_map_locked (bo); + +if (err) { + ret = mmap_lock_held ?VM_FAULT_SIGBUS :VM_FAULT_RETRY ; + }else { +struct page *page = bo->backing.pages[page_offset]; + +if (mmap_lock_held) + ret =insert_page (vmf, order, page); +else + ret =VM_FAULT_RETRY ; + } + +dma_resv_unlock (bo->base.resv); + +return ret; +} +static vm_fault_t panthor_gem_any_fault (struct vm_fault *vmf,unsigned int order) {struct vm_area_struct *vma = vmf->vma; -struct drm_gem_object *obj = vma->vm_private_data;struct panthor_gem_object *bo =to_panthor_bo (vma->vm_private_data); -loff_t num_pages = obj->size >>PAGE_SHIFT ; -vm_fault_t ret; +loff_t num_pages = bo->base.size >>PAGE_SHIFT ;pgoff_t page_offset; +vm_fault_t ret;if (order && order !=PMD_ORDER )return VM_FAULT_FALLBACK ;/* Offset to faulty address in the VMA. */ page_offset = vmf->pgoff - vma->vm_pgoff; +if (page_offset >= num_pages) +return VM_FAULT_SIGBUS ; -dma_resv_lock (bo->base.resv,NULL ); + ret =nonblocking_page_setup (vmf, order, page_offset); +if (ret !=VM_FAULT_RETRY ) +return ret; -if (page_offset >= num_pages || -drm_WARN_ON_ONCE (obj->dev, !bo->backing.pages)) { - ret =VM_FAULT_SIGBUS ; - goto out; +/* Check if we're allowed to retry. */ +if (fault_flag_allow_retry_first (vmf->flags)) { +/* If we're allowed to retry but not wait here, return +* immediately, the wait will be done when the fault +* handler is called again, with the mmap_lock held. +*/ +if (vmf->flags &FAULT_FLAG_RETRY_NOWAIT ) +return VM_FAULT_RETRY ; + +/* Wait with the mmap lock released, if we're allowed to. */ +drm_gem_object_get (&bo->base); +mmap_read_unlock (vmf->vma->vm_mm); + ret =blocking_page_setup (vmf, order, bo, page_offset, false); +drm_gem_object_put (&bo->base); +return ret; } - ret =insert_page (vmf, order, bo->backing.pages[page_offset]); - - out: -dma_resv_unlock (bo->base.resv); - -return ret; +return blocking_page_setup (vmf, order, bo, page_offset, true); }static vm_fault_t panthor_gem_fault (struct vm_fault *vmf)
@@ -689,31 +740,12 @@ 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)); - -dma_resv_lock (bo->base.resv,NULL ); - -/* We should have already pinned the pages when the buffer was first -* mmap'd, vm_open() just grabs an additional reference for the new -* mm the vma is getting copied into (ie. on fork()). -*/ -drm_WARN_ON_ONCE (bo->base.dev, !bo->backing.pages); - -dma_resv_unlock (bo->base.resv); - -drm_gem_vm_open (vma); -} -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 = panthor_gem_vm_open, + .open = drm_gem_vm_open, .close = drm_gem_vm_close, };
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 7/9 drm/panthor: Split panthor_vm_prepare_map_op_ctx() to prepare for reclaim
In reply to Boris BrezillonWe're gonna need just the page table reservation logic when we restore evicted BO mappings, so let's prepare for that by extracting the op_ctx init and page table pre-allocation into separate helpers.
v2:
- Collect R-bs
v3:
- No changes
v4:
- No changes
v5:
- No changes
v6:
- No changes
--- drivers/gpu/drm/panthor/panthor_mmu.c | 68 ++++++++++++++++----------- 1 file changed, 41 insertions(+), 27 deletions(-)
drivers/gpu/drm/panthor/panthor_mmu.c +41 -27
@@ -1180,6 +1180,44 @@ panthor_vm_op_ctx_prealloc_vmas(struct panthor_vm_op_ctx *op_ctx)return 0 ; } +static void panthor_vm_init_op_ctx (struct panthor_vm_op_ctx *op_ctx, +u64 size,u64 va,u32 flags) +{ +memset (op_ctx,0 ,sizeof (*op_ctx)); + op_ctx->flags = flags; + op_ctx->va.range = size; + op_ctx->va.addr = va; +} + +static int panthor_vm_op_ctx_prealloc_pts (struct panthor_vm_op_ctx *op_ctx) +{ +u64 size = op_ctx->va.range; +u64 va = op_ctx->va.addr; +int ret; + +/* L1, L2 and L3 page tables. +* We could optimize L3 allocation by iterating over the sgt and merging +* 2M contiguous blocks, but it's simpler to over-provision and return +* the pages if they're not used. +*/ +u64 pt_count = ((ALIGN (va + size,1ull <<39 ) -ALIGN_DOWN (va,1ull <<39 )) >>39 ) + + ((ALIGN (va + size,1ull <<30 ) -ALIGN_DOWN (va,1ull <<30 )) >>30 ) + + ((ALIGN (va + size,1ull <<21 ) -ALIGN_DOWN (va,1ull <<21 )) >>21 ); + + op_ctx->rsvd_page_tables.pages =kzalloc_objs (*op_ctx->rsvd_page_tables.pages, + pt_count); +if (!op_ctx->rsvd_page_tables.pages) +return -ENOMEM ; + + ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, + op_ctx->rsvd_page_tables.pages); + op_ctx->rsvd_page_tables.count = ret; +if (ret != pt_count) +return -ENOMEM ; + +return 0 ; +} +#define PANTHOR_VM_BIND_OP_MAP_FLAGS \ (DRM_PANTHOR_VM_BIND_OP_MAP_READONLY | \ DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC | \
@@ -1195,7 +1233,6 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, {struct drm_gpuvm_bo *preallocated_vm_bo;struct sg_table *sgt =NULL ; -u64 pt_count;int ret;if (!bo)
@@ -1214,10 +1251,7 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, bo->exclusive_vm_root_gem != panthor_vm_root_gem(vm))return -EINVAL ; -memset (op_ctx,0 ,sizeof (*op_ctx)); - op_ctx->flags = flags; - op_ctx->va.range = size; - op_ctx->va.addr = va; +panthor_vm_init_op_ctx (op_ctx, size, va, flags); ret =panthor_vm_op_ctx_prealloc_vmas (op_ctx);if (ret)
@@ -1249,29 +1283,9 @@ static int panthor_vm_prepare_map_op_ctx(struct panthor_vm_op_ctx *op_ctx, op_ctx->map.vm_bo =drm_gpuvm_bo_obtain_prealloc (preallocated_vm_bo); op_ctx->map.bo_offset = offset; -/* L1, L2 and L3 page tables. -* We could optimize L3 allocation by iterating over the sgt and merging -* 2M contiguous blocks, but it's simpler to over-provision and return -* the pages if they're not used. -*/ - pt_count = ((ALIGN (va + size,1ull <<39 ) -ALIGN_DOWN (va,1ull <<39 )) >>39 ) + - ((ALIGN (va + size,1ull <<30 ) -ALIGN_DOWN (va,1ull <<30 )) >>30 ) + - ((ALIGN (va + size,1ull <<21 ) -ALIGN_DOWN (va,1ull <<21 )) >>21 ); - - op_ctx->rsvd_page_tables.pages =kzalloc_objs (*op_ctx->rsvd_page_tables.pages, - pt_count); -if (!op_ctx->rsvd_page_tables.pages) { - ret = -ENOMEM ; + ret =panthor_vm_op_ctx_prealloc_pts (op_ctx); +if (ret) goto err_cleanup; - } - - ret =kmem_cache_alloc_bulk (pt_cache,GFP_KERNEL , pt_count, - op_ctx->rsvd_page_tables.pages); - op_ctx->rsvd_page_tables.count = ret; -if (ret != pt_count) { - ret = -ENOMEM ; - goto err_cleanup; - }/* Insert BO into the extobj list last, when we know nothing can fail. */ if (bo->base.resv !=panthor_vm_resv (vm)) {
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 8/9 drm/panthor: Track the number of mmap on a BO
In reply to Boris BrezillonThis 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()
--- drivers/gpu/drm/panthor/panthor_gem.c | 42 +++++++++++++++++++++++++-- drivers/gpu/drm/panthor/panthor_gem.h | 3 ++ 2 files changed, 43 insertions(+), 2 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +40 -2
@@ -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 *vif (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 = {
drivers/gpu/drm/panthor/panthor_gem.h +3 -0
@@ -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; };/**
Signature · 1 line
2.53.0
Boris Brezillon
maintainerPATCH v6 9/9 drm/panthor: Add a GEM shrinker
In reply to Boris BrezillonFrom: Akash Goel <[email protected]>
This implementation is losely based on the MSM shrinker, and it's relying on the drm_gpuvm eviction/validation infrastructure.
Right now we only support swapout/eviction, but we could add an extra flag to specify when buffer content doesn't need to be preserved to avoid the swapout/swapin dance.
Locking is a bit of a nightmare, but using _trylock() all the way in the reclaim path seems to make lockdep happy. And yes, we might be missing opportunities to reclaim when the system is under heavy GPU load/heavy memory pressure/heavy GPU VM activity, but that's better than no reclaim at all.
v2:
- Move gpu_mapped_shared next to the mmapped LRU
- Add a bunch of missing is_[vm_bo,vma]_evicted() tests
- Only test mmap_count to check if a BO is mmaped
- Remove stale comment about shrinker not being a thing
- Allow pin_count to be non-zero in panthor_gem_swapin_locked()
- Fix panthor_gem_sync() to check for BO residency before doing the CPU sync
- Fix the value returned by panthor_gem_shrinker_count() in case some memory has been released
- Check drmm_mutex_init() ret code
- Explicitly mention that PANTHOR_GEM_UNRECLAIMABLE is the initial state of all BOs
v3:
- Make panthor_gem_try_evict() static
- Collect {A,R}-bs
v4:
- Update the reclaim_state in panthor_gem_mmap()
- Don't reclaim GPU-mapped BOs if can_block() returns false
- Skip evicited vm_bos in panthor_vm_update_bo_reclaim_lru_locked() to avoid spurious WARN_ON()s
- Explain why we have to do this select_evicted_vma/repopulate_evicted_vma dance
v5:
- Properly report the reclaimable size in panthor_gem_debugfs_print_bos()
- Check panthor_vm_lock_region() errors in panthor_vm_evict_bo_mappings_locked()
- Fix lock order inversion (dma_resv_wait_timeout() inside gpuva.lock)
v6:
- Don't remap if the unmapped VMA is evicted.
- Drop a stale comment in panthor_gem_dev_map_get_sgt_locked()
- s/PANTHOR_GEM_GPU_MAPPED_PRIVATE/PANTHOR_GEM_GPU_MAPPED_SINGLE_VM/
- s/PANTHOR_GEM_GPU_MAPPED_SHARED/PANTHOR_GEM_GPU_MAPPED_MULTI_VM/
- Just count the number of vm_bo to determine the reclaim state in is_gpu_mapped()
- Drop a redundant panthor_gem_backing_get_pages_locked() call in panthor_gem_swapin_locked()
- Add more comments to panthor_vm_evict_bo_mappings_locked() to convince Claude it's actually safe.
- Add a comment in panthor_gem_shrinker_count() to mention the race and hopefully clear Claude's concerns.
- Rework the "is-still-in-list" check we have in panthor_mmu_reclaim_priv_bos() to address Claude's concerns
- Don't call panthor_vm_unlock_region() if panthor_vm_lock_region() fails in remap_evicted_vma(). Was harmless, but confusing, as pointed out by Claude
- Fix can_block() to allow blocking on KSWAPD_RECLAIM-only in a kswapd context
- Fix a lockdep warning when the last ref on a BO is released in the shrinker path, where the resv lock can't be acquired other than with a try_lock()
--- drivers/gpu/drm/panthor/panthor_device.c | 11 +- drivers/gpu/drm/panthor/panthor_device.h | 73 ++++ drivers/gpu/drm/panthor/panthor_gem.c | 508 ++++++++++++++++++++++- drivers/gpu/drm/panthor/panthor_gem.h | 68 +++ drivers/gpu/drm/panthor/panthor_mmu.c | 390 +++++++++++++++-- drivers/gpu/drm/panthor/panthor_mmu.h | 8 + 6 files changed, 1014 insertions(+), 44 deletions(-)
drivers/gpu/drm/panthor/panthor_device.c +10 -1
@@ -2,6 +2,7 @@/* Copyright 2018 Marty E. Plummer <[email protected]> */ /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */ /* Copyright 2023 Collabora ltd. */ +/* Copyright 2025 ARM Limited. All rights reserved. */ #include <linux/clk.h> #include <linux/mm.h>
@@ -122,6 +123,7 @@ void panthor_device_unplug(struct panthor_device *ptdev)panthor_sched_unplug (ptdev);panthor_fw_unplug (ptdev);panthor_mmu_unplug (ptdev); +panthor_gem_shrinker_unplug (ptdev);panthor_gpu_unplug (ptdev);panthor_pwr_unplug (ptdev);
@@ -291,10 +293,14 @@ int panthor_device_init(struct panthor_device *ptdev)if (ret) goto err_unplug_gpu; - ret =panthor_mmu_init (ptdev); + ret =panthor_gem_shrinker_init (ptdev);if (ret) goto err_unplug_gpu; + ret =panthor_mmu_init (ptdev); +if (ret) + goto err_unplug_shrinker; + ret =panthor_fw_init (ptdev);if (ret) goto err_unplug_mmu;
@@ -326,6 +332,9 @@ int panthor_device_init(struct panthor_device *ptdev) err_unplug_mmu:panthor_mmu_unplug (ptdev); +err_unplug_shrinker: +panthor_gem_shrinker_unplug (ptdev); + err_unplug_gpu:panthor_gpu_unplug (ptdev);
drivers/gpu/drm/panthor/panthor_device.h +73 -0
85 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_gem.c +491 -17
689 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_gem.h +68 -0
92 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_mmu.c +364 -26
517 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_mmu.h +8 -0
@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 or MIT */ /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */ /* Copyright 2023 Collabora ltd. */ +/* Copyright 2025 ARM Limited. All rights reserved. */ #ifndef __PANTHOR_MMU_H__#define __PANTHOR_MMU_H__
@@ -46,6 +47,13 @@ struct panthor_vm *panthor_vm_create(struct panthor_device *ptdev, bool for_mcu,u64 kernel_auto_va_start, u64 kernel_auto_va_size); +void panthor_vm_update_bo_reclaim_lru_locked (struct panthor_gem_object *bo); +int panthor_vm_evict_bo_mappings_locked (struct panthor_gem_object *bo); +unsigned long +panthor_mmu_reclaim_priv_bos (struct panthor_device *ptdev, +unsigned int nr_to_scan,unsigned long *remaining, +bool (*shrink)(struct drm_gem_object *, +struct ww_acquire_ctx *));int panthor_vm_prepare_mapped_bos_resvs (struct drm_exec *exec,struct panthor_vm *vm,u32 slot_count);
Signature · 1 line
2.53.0
Steven Price
maintainerHi Boris,
Quoted 9 lines · Boris Brezillon
Hello,
This is an attempt at adding a GEM shrinker to panthor so the system can finally reclaim GPU memory.
This implementation is losely based on the MSM shrinker (which is why I added the MSM maintainers in Cc), and it's relying on the
drm_gpuvmeviction/validation infrastructure.
Quoted 5 lines
I've only done very basic IGT-based [1] and chromium-based (opening a lot of tabs on Aquarium until the system starts reclaiming+swapping out GPU buffers) testing, but I'm posting this early so I can get preliminary feedback on the implementation. If someone knows about better
tools/waysto test the shrinker, please let me know.
I did my own pretty basic testing (glmark with memhog) and managed to hit this:
[ 265.053172] ============================================
[ 265.053667] WARNING: possible recursive locking detected
[ 265.054159] 7.0.0-rc3-00694-gadfa5ca08767 #1 Not tainted
[ 265.054655] --------------------------------------------
[ 265.055143] glmark2-es2-drm/443 is trying to acquire lock:
[ 265.055651] ffff0001194011a8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: drm_gpuvm_bo_deferred_cleanup+0x100/0x2c0 [drm_gpuvm]
[ 265.056738]
[ 265.056738] but task is already holding lock:
[ 265.057278] ffff80008b7c79e8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: panthor_ioctl_group_submit+0x424/0x560 [panthor]
[ 265.058324]
[ 265.058324] other info that might help us debug this:
[ 265.058927] Possible unsafe locking scenario:
[ 265.058927]
[ 265.059475] CPU0
[ 265.059706] ----
[ 265.059939] lock(reservation_ww_class_mutex);
[ 265.060365] lock(reservation_ww_class_mutex);
[ 265.060788]
[ 265.060788] *** DEADLOCK ***
[ 265.060788]
[ 265.061338] May be due to missing lock nesting notation
[ 265.061338]
[ 265.061964] 3 locks held by glmark2-es2-drm/443:
[ 265.062395] #0: ffff80008493c458 (drm_unplug_srcu){.+.+}-{0:0}, at: drm_dev_enter+0x0/0x140
[ 265.063188] #1: ffff80008b7c79c0 (reservation_ww_class_acquire){+.+.}-{0:0}, at: panthor_ioctl_group_submit+0x424/0x560 [panthor]
[ 265.064288] #2: ffff80008b7c79e8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: panthor_ioctl_group_submit+0x424/0x560 [panthor]
[ 265.065370]
[ 265.065370] stack backtrace:
[ 265.065780] CPU: 4 UID: 1000 PID: 443 Comm: glmark2-es2-drm Not tainted 7.0.0-rc3-00694-gadfa5ca08767 #1 PREEMPT
[ 265.065787] Hardware name: Radxa ROCK 5B (DT)
[ 265.065791] Call trace:
[ 265.065793] show_stack+0x18/0x24 (C)
[ 265.065802] dump_stack_lvl+0x6c/0x94
[ 265.065810] dump_stack+0x1c/0x28
[ 265.065815] print_deadlock_bug+0x224/0x238
[ 265.065822] __lock_acquire+0xe54/0x1600
[ 265.065829] lock_acquire+0x3cc/0x420
[ 265.065834] __ww_mutex_lock.constprop.0+0x1fc/0x2c40
[ 265.065844] ww_mutex_lock+0x50/0x168
[ 265.065850] drm_gpuvm_bo_deferred_cleanup+0x100/0x2c0 [drm_gpuvm]
[ 265.065862] panthor_vm_cleanup_op_ctx+0x188/0x270 [panthor]
[ 265.065881] panthor_vm_bo_validate+0x404/0x758 [panthor]
[ 265.065898] drm_gpuvm_validate+0x28c/0xf50 [drm_gpuvm]
[ 265.065907] panthor_vm_prepare_mapped_bos_resvs+0x64/0x80 [panthor]
[ 265.065925] panthor_ioctl_group_submit+0x418/0x560 [panthor]
[ 265.065942] drm_ioctl_kernel+0x15c/0x2c0
[ 265.065947] drm_ioctl+0x56c/0xb1c
[ 265.065952] __arm64_sys_ioctl+0x124/0x1a4
[ 265.065961] invoke_syscall+0x70/0x260
[ 265.065967] el0_svc_common.constprop.0+0xac/0x230
[ 265.065972] do_el0_svc+0x40/0x58
[ 265.065976] el0_svc+0x4c/0x210
[ 265.065981] el0t_64_sync_handler+0xa0/0xe4
[ 265.065986] el0t_64_sync+0x198/0x19cThanks,
Steve
Quoted 81 lines
A few words about some design/implementation choices:
- No MADVISE support because I want to see if we can live with just transparent reclaim
- We considered basing this implementation on the generic shrinker work started by Dmitry [2], but
1. with the activeness/idleness tracking happening at the VM granularity, having per-BO LRUs would caused a lot of list_move()s that are not really needed (the VM as a whole become active/idle, we can track individual BOs)
2. Thomas Zimmermann recently suggested that we should have our own GEM implementation instead of trying to add this extra reclaim complexity to gem-shmem. There are some plans to create a gem-uma (Unified Memory Architecture) lib that would do more than gem-shmem but in a way that doesn't force all its users to pay the overhead (size overhead of the gem object, mostly) for features they don't use. Patch "Part ways withdrm_gem_shmem_object" is showing what this component-based lib API could look like if it were to be extracted
- At the moment we only support swapout, but we could add an extra flag to specify when buffer content doesn't need to be preserved to avoid the swapout/swapin dance. First candidate for thisDISCARD_ON_RECLAIMflag would probably be the tiler heap chunks.
- Reclaim uses_try_lock()all the way because of the various lock order inversions between the reclaim path and submission paths. That means we don't try very hard to reclaim hot GPU buffers, but the locking is such a mess that I don't really see a better option to be honest.Changes in v2:
- No fundamental changes in this v2, since the feedback I got were more focused on bugs than the overall approach. Check the changelog in each patch for more details.Changes in v3:
- Mostly fixes (see the changelog in each patch)Changes in v4:
- Only fixes (see the changelog in each patch)Changes in v5:
- Only fixes (see the changelog in each patch)Changes in v6:
- Fix huge fault handling and various other things reported by Adrian and the review bot (see the changelog in each patch for more)Regards,
Boris
[1]https://gitlab.freedesktop.org/bbrezillon/igt-gpu-tools/-/commit/fc76934a5579767d2aabe787d40e38a17c3f4ea4 [2]https://lkml.org/lkml/2024/1/5/665
Akash Goel (1):
drm/panthor: Add a GEM shrinkerBoris Brezillon (8):
drm/gem: Consider GEM object reclaimable if shrinking fails drm/panthor: Movepanthor_gems_debugfs_init()topanthor_gem.cdrm/panthor: Grouppanthor_kernel_bo_xxx()helpers drm/panthor: Don't calldrm_gpuvm_bo_extobj_add()if the object is
private
drm/panthor: Part ways withdrm_gem_shmem_objectdrm/panthor: Lazily allocate pages onmmap()drm/panthor: Splitpanthor_vm_prepare_map_op_ctx()to prepare for
reclaim
drm/panthor: Track the number of mmap on a BOdrivers/gpu/drm/drm_gem.c | 10 + drivers/gpu/drm/panthor/Kconfig | 1 - drivers/gpu/drm/panthor/panthor_device.c | 11 +- drivers/gpu/drm/panthor/panthor_device.h | 73 ++ drivers/gpu/drm/panthor/panthor_drv.c | 33 +- drivers/gpu/drm/panthor/panthor_fw.c | 16 +- drivers/gpu/drm/panthor/panthor_gem.c | 1453 ++++++++++++++++++---- drivers/gpu/drm/panthor/panthor_gem.h | 136 +- drivers/gpu/drm/panthor/panthor_mmu.c | 502 ++++++-- drivers/gpu/drm/panthor/panthor_mmu.h | 8 + drivers/gpu/drm/panthor/panthor_sched.c | 9 +- 11 files changed, 1911 insertions(+), 341 deletions(-)
Boris Brezillon
maintainerQuoted 66 lines · On Mon, 30 Mar 2026 11:39:00 +0100 Steven Price <[email protected]> wrote:
Hi Boris,
Quoted 9 lines · Boris Brezillon
Hello,
This is an attempt at adding a GEM shrinker to panthor so the system can finally reclaim GPU memory.
This implementation is losely based on the MSM shrinker (which is why I added the MSM maintainers in Cc), and it's relying on the
drm_gpuvmeviction/validation infrastructure.Quoted 5 lines
I've only done very basic IGT-based [1] and chromium-based (opening a lot of tabs on Aquarium until the system starts reclaiming+swapping out GPU buffers) testing, but I'm posting this early so I can get preliminary feedback on the implementation. If someone knows about better
tools/waysto test the shrinker, please let me know.I did my own pretty basic testing (glmark with memhog) and managed to hit this:
[ 265.053172] ============================================ [ 265.053667] WARNING: possible recursive locking detected [ 265.054159] 7.0.0-rc3-00694-gadfa5ca08767 #1 Not tainted [ 265.054655] -------------------------------------------- [ 265.055143] glmark2-es2-drm/443 is trying to acquire lock: [ 265.055651] ffff0001194011a8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: drm_gpuvm_bo_deferred_cleanup+0x100/0x2c0 [drm_gpuvm] [ 265.056738] [ 265.056738] but task is already holding lock: [ 265.057278] ffff80008b7c79e8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: panthor_ioctl_group_submit+0x424/0x560 [panthor] [ 265.058324] [ 265.058324] other info that might help us debug this: [ 265.058927] Possible unsafe locking scenario: [ 265.058927] [ 265.059475] CPU0 [ 265.059706] ---- [ 265.059939] lock(reservation_ww_class_mutex); [ 265.060365] lock(reservation_ww_class_mutex); [ 265.060788] [ 265.060788] *** DEADLOCK *** [ 265.060788] [ 265.061338] May be due to missing lock nesting notation [ 265.061338] [ 265.061964] 3 locks held by glmark2-es2-drm/443: [ 265.062395] #0: ffff80008493c458 (drm_unplug_srcu){.+.+}-{0:0}, at: drm_dev_enter+0x0/0x140 [ 265.063188] #1: ffff80008b7c79c0 (reservation_ww_class_acquire){+.+.}-{0:0}, at: panthor_ioctl_group_submit+0x424/0x560 [panthor] [ 265.064288] #2: ffff80008b7c79e8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: panthor_ioctl_group_submit+0x424/0x560 [panthor] [ 265.065370] [ 265.065370] stack backtrace: [ 265.065780] CPU: 4 UID: 1000 PID: 443 Comm: glmark2-es2-drm Not tainted 7.0.0-rc3-00694-gadfa5ca08767 #1 PREEMPT [ 265.065787] Hardware name: Radxa ROCK 5B (DT) [ 265.065791] Call trace: [ 265.065793] show_stack+0x18/0x24 (C) [ 265.065802] dump_stack_lvl+0x6c/0x94 [ 265.065810] dump_stack+0x1c/0x28 [ 265.065815] print_deadlock_bug+0x224/0x238 [ 265.065822] __lock_acquire+0xe54/0x1600 [ 265.065829] lock_acquire+0x3cc/0x420 [ 265.065834] __ww_mutex_lock.constprop.0+0x1fc/0x2c40 [ 265.065844] ww_mutex_lock+0x50/0x168 [ 265.065850] drm_gpuvm_bo_deferred_cleanup+0x100/0x2c0 [drm_gpuvm] [ 265.065862] panthor_vm_cleanup_op_ctx+0x188/0x270 [panthor] [ 265.065881] panthor_vm_bo_validate+0x404/0x758 [panthor] [ 265.065898] drm_gpuvm_validate+0x28c/0xf50 [drm_gpuvm] [ 265.065907] panthor_vm_prepare_mapped_bos_resvs+0x64/0x80 [panthor] [ 265.065925] panthor_ioctl_group_submit+0x418/0x560 [panthor] [ 265.065942] drm_ioctl_kernel+0x15c/0x2c0
Quoted 9 lines
[ 265.065947] drm_ioctl+0x56c/0xb1c [ 265.065952] __arm64_sys_ioctl+0x124/0x1a4 [ 265.065961] invoke_syscall+0x70/0x260 [ 265.065967] el0_svc_common.constprop.0+0xac/0x230 [ 265.065972] do_el0_svc+0x40/0x58 [ 265.065976] el0_svc+0x4c/0x210 [ 265.065981] el0t_64_sync_handler+0xa0/0xe4 [ 265.065986] el0t_64_sync+0x198/0x19c
Nice catch! I've fixed it by skipping the drm_gpuvm_bo_deferred_cleanup() call in panthor_vm_cleanup_op_ctx() when the operation is a VMA repopulation. In that case, the VM resv lock will be held (because the SUBMIT logic acquires it), and the very same lock is taken in drm_gpuvm_bo_deferred_cleanup(). We could have added a drm_gpuvm_bo_deferred_cleanup_locked() variant, but in practice, the VMA repopulation never calls drm_gpuvm_bo_put_deferred(), so there's nothing for us to cleanup, and the only reason we were going past the if (!bo_defer) test in drm_gpuvm_bo_deferred_cleanup() is because other threads can race with the VMA repopulation and queue vm_bos to the deferred cleanup list.
TLDR; this should be sorted out in v7, which I plan to post soon (I'd like to maximize the time this patch series spends in linux-next so we can detect issues early and fix them before it hits Linus' tree).