For deployments of systems with very tight memory and a system resource
manager process that can make well-founded decisions on trade-offs, a
way to tell panthor that all of a process' swapped-out GPU memory should
be swapped back in without touching every page manually is of interest.

Make it possible to do this by adding a new sysfs file, called
"mem_claim". Writing a TGID to it will cause panthor to search through
all panthor_files associated with that process, and bring all its
buffers back from swap.

Doing this requires the writer to have the CAP_SYS_RESOURCE capability,
even when operating on themselves.

Signed-off-by: Nicolas Frattaroli <[email protected]>
---
 Documentation/ABI/testing/sysfs-driver-panthor-mem | 17 ++++++++++++++
 drivers/gpu/drm/panthor/panthor_drv.c              | 26 ++++++++++++++++++++++
 drivers/gpu/drm/panthor/panthor_mmu.c              | 25 +++++++++++++++++++++
 drivers/gpu/drm/panthor/panthor_mmu.h              |  1 +
 4 files changed, 69 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-panthor-mem 
b/Documentation/ABI/testing/sysfs-driver-panthor-mem
index 6639394abed2..41e2c18f641d 100644
--- a/Documentation/ABI/testing/sysfs-driver-panthor-mem
+++ b/Documentation/ABI/testing/sysfs-driver-panthor-mem
@@ -15,3 +15,20 @@ Description:
                * -EPERM: insufficient permissions to run a reclaim on given 
TGID
                * -EINTR: interrupted by signal
                * -ESRCH: given TGID is not using panthor, and might not exist 
at all
+
+What:          /sys/bus/platform/drivers/panthor/.../mem_claim
+Date:          May 2026
+Contact:       Nicolas Frattaroli <[email protected]>
+Description:
+               (WO) Writing to this file will cause GPU memory for all panthor 
GPU
+               contexts associated with the TGID that's written to it to be 
brought
+               back from swap. The write completes when the operation has 
finished.
+
+               The writing process requires the CAP_SYS_RESOURCE capability.
+
+               Possible error codes:
+               * -ERANGE: given TGID is too large/small for the TGID type.
+               * -EINVAL: given TGID could not be parsed.
+               * -EPERM: insufficient permissions
+               * -EINTR: interrupted by signal
+               * -ESRCH: given TGID is not using panthor, and might not exist 
at all
diff --git a/drivers/gpu/drm/panthor/panthor_drv.c 
b/drivers/gpu/drm/panthor/panthor_drv.c
index 7d19b8785ea3..fb2172b0439c 100644
--- a/drivers/gpu/drm/panthor/panthor_drv.c
+++ b/drivers/gpu/drm/panthor/panthor_drv.c
@@ -1924,9 +1924,35 @@ static ssize_t mem_reclaim_store(struct device *dev,
 
 static DEVICE_ATTR_WO(mem_reclaim);
 
+static ssize_t mem_claim_store(struct device *dev, struct device_attribute 
*attr,
+                              const char *buf, size_t len)
+{
+       struct panthor_device *ptdev = dev_get_drvdata(dev);
+       pid_t tgid;
+       int ret;
+
+       ret = kstrtoint(buf, 0, &tgid);
+       if (ret)
+               return ret;
+
+       if (!capable(CAP_SYS_RESOURCE))
+               return -EPERM;
+
+       ret = panthor_run_on_pfiles_of_tgid(ptdev, tgid, 
panthor_mmu_force_claim);
+       if (ret < 0)
+               return ret;
+       else if (!ret)
+               return -ESRCH;
+
+       return len;
+}
+
+static DEVICE_ATTR_WO(mem_claim);
+
 static struct attribute *panthor_attrs[] = {
        &dev_attr_profiling.attr,
        &dev_attr_mem_reclaim.attr,
+       &dev_attr_mem_claim.attr,
        NULL,
 };
 
diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c 
b/drivers/gpu/drm/panthor/panthor_mmu.c
index e185787f5657..7130ba4a24da 100644
--- a/drivers/gpu/drm/panthor/panthor_mmu.c
+++ b/drivers/gpu/drm/panthor/panthor_mmu.c
@@ -3232,6 +3232,31 @@ void panthor_mmu_force_reclaim(struct panthor_file 
*pfile)
                freed, freed_sz, nr_to_scan, remaining);
 }
 
+/**
+ * panthor_mmu_force_claim - Swap in all VMs associated with a file
+ * @pfile: pointer to the &struct panthor_file whose memory to swap in
+ *
+ * Attempt to get all GPU memory of @pfile swapped back in.
+ */
+void panthor_mmu_force_claim(struct panthor_file *pfile)
+{
+       struct panthor_vm *vm;
+       unsigned long i;
+       int ret;
+
+       xa_for_each(&pfile->vms->xa, i, vm) {
+               struct dma_resv *resv = drm_gpuvm_resv(&vm->base);
+
+               dma_resv_lock(resv, NULL);
+               ret = drm_gpuvm_validate(&vm->base, NULL);
+               if (ret)
+                       drm_dbg(&vm->ptdev->base, "drm_gpuvm_validate failed: 
%pe\n",
+                               ERR_PTR(ret));
+
+               dma_resv_unlock(resv);
+       }
+}
+
 /**
  * panthor_mmu_unplug() - Unplug the MMU logic
  * @ptdev: Device.
diff --git a/drivers/gpu/drm/panthor/panthor_mmu.h 
b/drivers/gpu/drm/panthor/panthor_mmu.h
index 34adca4b4e95..460f83eb22a9 100644
--- a/drivers/gpu/drm/panthor/panthor_mmu.h
+++ b/drivers/gpu/drm/panthor/panthor_mmu.h
@@ -25,6 +25,7 @@ void panthor_mmu_post_reset(struct panthor_device *ptdev);
 void panthor_mmu_suspend(struct panthor_device *ptdev);
 void panthor_mmu_resume(struct panthor_device *ptdev);
 void panthor_mmu_force_reclaim(struct panthor_file *pfile);
+void panthor_mmu_force_claim(struct panthor_file *pfile);
 
 int panthor_vm_map_bo_range(struct panthor_vm *vm, struct panthor_gem_object 
*bo,
                            u64 offset, u64 size, u64 va, u32 flags);

-- 
2.54.0

Reply via email to