drm/panthor: Protected mode support for Mali CSF GPUs
Subsystems (5)
v5 cover drm/panthor: Protected mode support for Mali CSF GPUs
Hi,
This is a patch series covering the support for protected mode execution in Mali Panthor CSF kernel driver.
It builds on the initial RFC posted by Florent Tomasin back in January of 2025.
The initial RFC can be found here:
https://lore.kernel.org/lkml/[email protected]/
The Mali CSF GPUs come with the support for protected mode execution at the HW level. This feature requires two main changes in the kernel driver:
1) Configure the GPU with a protected buffer. The system must provide a DMA heap from which the driver can get protected buffers.
It can be a carved-out memory or dynamically allocated protected memory region. Some system includes a trusted FW which is in charge of the protected memory. This problem is integration specific and the responsibility to allocate the required protected memory from the correct and system specific DMA heap is left to user space (Mesa). Panthor CSF driver must be handed the needed buffers via new IOCTLs.
2) Handle enter and exit of the GPU HW from normal to protected mode of execution. FW sends a request for protected mode entry to the kernel driver. The acknowledgment of that request is a scheduling decision. Effectively, protected mode execution should not overrule normal mode of execution. A fair distribution of execution time will guaranty the overall performance of the device, including the UI (usually executing in normal mode), will not regress when a protected mode job is submitted by an application.
Background
----------
Current Mali Panthor CSF driver does not allow a user space application to execute protected jobs on the GPU. This use case is quite common on end-user-device. A user may want to watch a video or render content that is under a "Digital Right Management" protection, or launch an application with user private data.
1) User-space:
In order for an application to execute protected jobs on a Mali CSF GPU the user space application must submit jobs to the GPU within a "protected regions" (range of commands to execute in protected mode).
Find here an example of a command buffer that contains protected commands:
```
<--- Normal mode ---><--- Protected mode ---><--- Normal mode --->
+-------------------------------------------------------------------------+
| ... | CMD_0 | ... | CMD_N | PROT_REGION | CMD_N+1 | ... | CMD_N+M | ... |
+-------------------------------------------------------------------------+
``` The PROT_REGION command acts as a barrier to notify the HW of upcoming protected jobs. It also defines the number of commands to execute in protected mode.
The Mesa definition of the opcode can be found here:
2) Kernel-space:
When loading the FW image, the Kernel driver must also load the data section of CSF FW that comes from the protected memory, in order to allow FW to execute in protected mode.
The FW uses the same protected memory buffer to execute protected jobs from all processes on the system. Note: This buffer must be provided by a user space process with CAP_SYS_MODULE though.
In addition, when a CSG (group) is created, it must have a protected suspend buffer. User space must provide this buffer on group creation if it want to execute protected jobs.
The required size of both these buffer types can be queried. See query type DRM_PANTHOR_DEV_QUERY_PROTM_INFO.
Design decisions
----------------
The earlier versions (RFC and v1) relied on in-kernel allocation of protected DMA-bufs, both for the required FW memory (system wide), and per group suspend buffers (per process). This is no longer needed.
Starting with v2, this is now changed and it is user space which is responsible for supplying these buffers.
Only a user space processes with elevated privileges (CAP_SYS_MODULE) is trusted to provide the system wide protected FW memory. This only needs to happen once per boot, and is needed before any non-privileged processes can make use of protected rendering.
The Mali Panthor CSF kernel driver will handle enter/exit of protected mode with a fair consideration of the job scheduling.
If the system integrator does not provide a protected DMA heap, the driver will not allow any protected mode execution.
Testing
-------
1) Platform and development environment
Any platform containing a Mali CSF type of GPU and a protected memory allocator that is based on DMA Heap can be used. For example, it can be a physical platform or a simulator such as Arm Total Compute FVPs platforms. Reference to the latter:
https://developer.arm.com/Tools%20and%20Software/Fixed%20Virtual%20Platforms/Total%20Compute%20FVPs
2) Mesa:
PanVK support can be found here:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40044
Previous versions
-----------------
v4: https://lore.kernel.org/lkml/[email protected]/
v3: https://lkml.org/lkml/2026/9/11/1187
v2: https://lkml.org/lkml/2026/7/12/369
v1: https://lkml.org/lkml/2026/5/5/1523
Highlevel changes
-----------------
Changes in v5:
- Main changes are to the "drm/panthor: Add support for entering and exiting protected mode" patch. Use an empty transtab for VMs requesting updates when in protected mode.
This allows us to keep non-protected groups on slot for immediate resume on protm exit.
- See each patch for detailed changes
Changes in v4:
- Main changes are to the "drm/panthor: Add support for entering and exiting protected mode" patch. A new approach to avoid uneccesary protm premption by non-running VMs.
This patch do need some splitting before final version. Will do that if
this approach gets the general approval.
- Split the old patch "drm/panthor: Expose protected rendering features" into multiple smaller patches.
- See each patch for detailed changes
Changes in v3:
- Dropped patch "drm/panthor: Explicit expansion of locked VM region"
- See each patch for detailed changes
Changes in v2:
- Conceptually similar to v1, but heavily reworked to address fault handling
- User space provides all protected memory buffers (no in-kernel dmabuf alloc)
Boris Brezillon (7):
drm/panthor: Pass drm_file instead of panthor_file drm/panthor: New function for FW section map flags drm/panthor: Keep size and VA in FW section obj drm/panthor: Refactor GEM usage flags assignment drm/panthor: Create kernel BO from existing GEM drm/panthor: Support FW init of protm drm/panthor: Protm support for groups
Florent Tomasin (2):
drm/panthor: Minor scheduler refactoring
drm/panthor: Add support for entering and exiting protected mode
Ketil Johnsen (4):
drm/panthor: De-duplicate FW memory section sync drm/panthor: Don't allocate protm_suspend_buf drm/panthor: Use a copy of suspend buffer sizes drm/panthor: Bump version to expose protm feature
drivers/gpu/drm/panthor/panthor_device.h | 8 + drivers/gpu/drm/panthor/panthor_drv.c | 64 ++- drivers/gpu/drm/panthor/panthor_fw.c | 347 ++++++++++-- drivers/gpu/drm/panthor/panthor_fw.h | 12 + drivers/gpu/drm/panthor/panthor_gem.c | 111 ++-- drivers/gpu/drm/panthor/panthor_gem.h | 9 +- drivers/gpu/drm/panthor/panthor_gpu.c | 49 +- drivers/gpu/drm/panthor/panthor_gpu.h | 4 + drivers/gpu/drm/panthor/panthor_mmu.c | 368 +++++++++---- drivers/gpu/drm/panthor/panthor_mmu.h | 11 +- drivers/gpu/drm/panthor/panthor_sched.c | 672 +++++++++++++++++++---- drivers/gpu/drm/panthor/panthor_sched.h | 39 +- include/uapi/drm/panthor_drm.h | 85 ++- 13 files changed, 1441 insertions(+), 338 deletions(-)
Signature · 1 line
2.43.0
PATCH v5 1/13 drm/panthor: De-duplicate FW memory section sync
Handle the sync to device of FW memory sections inside panthor_fw_init_section_mem() so that the callers do not have to.
This small improvement is also critical for protected FW sections, so we avoid issuing memory transactions to protected memory from CPU running in normal mode.
--- drivers/gpu/drm/panthor/panthor_fw.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +7 -16
@@ -449,6 +449,7 @@ static void panthor_fw_init_section_mem(struct panthor_device *ptdev, struct panthor_fw_section *section) {bool was_mapped = !!section->mem->kmap; +struct sg_table *sgt;int ret; if (!section->data.size &&
@@ -467,6 +468,12 @@ static void panthor_fw_init_section_mem(struct panthor_device *ptdev,if (!was_mapped)panthor_kernel_bo_vunmap (section->mem); + +/* 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 ); }/**
@@ -639,7 +646,6 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,if (section_size) {u32 cache_mode = hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_MASK ; -struct panthor_gem_object *bo;u32 vm_map_flags =0 ;u64 va = hdr.va.start;
@@ -676,14 +682,6 @@ 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); - -/* 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 )
@@ -738,17 +736,10 @@ panthor_reload_fw_sections(struct panthor_device *ptdev, bool full_reload)struct panthor_fw_section *section;list_for_each_entry (section, &ptdev->fw->sections, node) { -struct sg_table *sgt; -if (!full_reload && !(section->flags &CSF_FW_BINARY_IFACE_ENTRY_WR ))continue ;panthor_fw_init_section_mem (ptdev, section); - -/* 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 ); } }
Signature · 1 line
2.43.0
PATCH v5 2/13 drm/panthor: Minor scheduler refactoring
In reply to Ketil JohnsenFrom: Florent Tomasin <[email protected]>
Refactor parts of the group scheduling logic into new helper functions. This will simplify addition of the protected mode feature.
Remove redundant assignments of csg_slot.
---
v5:
- Actually handle error from csg_slot_prog_locked()
v4:
- Handle error from csg_slot_prog_locked()
v3:
- Use panthor_fw_csg_endpoint_req_update() (was rebase mistake)
- Function rename to tick_ctx_update_group_prio()
v2:
- Moved option to ding only the CSG doorbell to later patch
--- drivers/gpu/drm/panthor/panthor_sched.c | 135 +++++++++++++++--------- 1 file changed, 83 insertions(+), 52 deletions(-)
drivers/gpu/drm/panthor/panthor_sched.c +83 -52
@@ -2345,12 +2345,85 @@ tick_ctx_cleanup(struct panthor_scheduler *sched, } } +static void +tick_ctx_evict_group (struct panthor_scheduler *sched, +struct panthor_csg_slots_upd_ctx *upd_ctx, +struct panthor_group *group) +{ +struct panthor_device *ptdev = sched->ptdev; + +if (drm_WARN_ON (&ptdev->base, group->csg_id <0 )) +return ; + +csgs_upd_ctx_queue_reqs (ptdev, upd_ctx, group->csg_id, +group_can_run (group) ? +CSG_STATE_SUSPEND :CSG_STATE_TERMINATE , +CSG_STATE_MASK ); +} + +static void +tick_ctx_update_group_prio (struct panthor_scheduler *sched, +struct panthor_csg_slots_upd_ctx *upd_ctx, +struct panthor_group *group, +int new_csg_prio) +{ +struct panthor_device *ptdev = sched->ptdev; +struct panthor_fw_csg_iface *csg_iface; +struct panthor_csg_slot *csg_slot; + +if (group->csg_id <0 ) +return ; + + csg_iface =panthor_fw_get_csg_iface (ptdev, group->csg_id); + csg_slot = &sched->csg_slots[group->csg_id]; + +if (csg_slot->priority != new_csg_prio) { +panthor_fw_csg_endpoint_req_update (ptdev, csg_iface, +CSG_EP_REQ_PRIORITY (new_csg_prio), +CSG_EP_REQ_PRIORITY_MASK ); +csgs_upd_ctx_queue_reqs (ptdev, upd_ctx, group->csg_id, + csg_iface->output->ack ^CSG_ENDPOINT_CONFIG , +CSG_ENDPOINT_CONFIG ); + } +} + +static int +tick_ctx_schedule_group (struct panthor_scheduler *sched, +struct panthor_csg_slots_upd_ctx *upd_ctx, +struct panthor_group *group, +int csg_id,int csg_prio) +{ +struct panthor_device *ptdev = sched->ptdev; +struct panthor_fw_csg_iface *csg_iface = +panthor_fw_get_csg_iface (ptdev, csg_id); +int ret; + + ret =group_bind_locked (group, csg_id); +if (ret) +return ret; + + ret =csg_slot_prog_locked (ptdev, csg_id, csg_prio); +if (ret) { +group_unbind_locked (group); +return ret; + } + +csgs_upd_ctx_queue_reqs (ptdev, upd_ctx, csg_id, + group->state ==PANTHOR_CS_GROUP_SUSPENDED ? +CSG_STATE_RESUME :CSG_STATE_START , +CSG_STATE_MASK ); +csgs_upd_ctx_queue_reqs (ptdev, upd_ctx, csg_id, + csg_iface->output->ack ^CSG_ENDPOINT_CONFIG , +CSG_ENDPOINT_CONFIG ); + +return 0 ; +} +static void tick_ctx_apply (struct panthor_scheduler *sched,struct panthor_sched_tick_ctx *ctx) {struct panthor_group *group, *tmp;struct panthor_device *ptdev = sched->ptdev; -struct panthor_csg_slot *csg_slot;int prio, new_csg_prio =MAX_CSG_PRIO , i;u32 free_csg_slots =0 ;struct panthor_csg_slots_upd_ctx upd_ctx;
@@ -2360,44 +2433,13 @@ tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *cfor (prio =PANTHOR_CSG_PRIORITY_COUNT -1 ; prio >=0 ; prio--) {/* Suspend or terminate evicted groups. */ -list_for_each_entry (group, &ctx->old_groups[prio], run_node) { -bool term = !group_can_run (group); -int csg_id = group->csg_id; - -if (drm_WARN_ON (&ptdev->base, csg_id <0 )) -continue ; - - csg_slot = &sched->csg_slots[csg_id]; -csgs_upd_ctx_queue_reqs (ptdev, &upd_ctx, csg_id, - term ?CSG_STATE_TERMINATE :CSG_STATE_SUSPEND , -CSG_STATE_MASK ); - } +list_for_each_entry (group, &ctx->old_groups[prio], run_node) +tick_ctx_evict_group (sched, &upd_ctx, group);/* Update priorities on already running groups. */ -list_for_each_entry (group, &ctx->groups[prio], run_node) { -struct panthor_fw_csg_iface *csg_iface; -int csg_id = group->csg_id; - -if (csg_id <0 ) { - new_csg_prio--; -continue ; - } - - csg_slot = &sched->csg_slots[csg_id]; - csg_iface =panthor_fw_get_csg_iface (ptdev, csg_id); -if (csg_slot->priority == new_csg_prio) { - new_csg_prio--; -continue ; - } - -panthor_fw_csg_endpoint_req_update (ptdev, csg_iface, -CSG_EP_REQ_PRIORITY (new_csg_prio), -CSG_EP_REQ_PRIORITY_MASK ); -csgs_upd_ctx_queue_reqs (ptdev, &upd_ctx, csg_id, - csg_iface->output->ack ^CSG_ENDPOINT_CONFIG , -CSG_ENDPOINT_CONFIG ); - new_csg_prio--; - } +list_for_each_entry (group, &ctx->groups[prio], run_node) +tick_ctx_update_group_prio (sched, &upd_ctx, group, + new_csg_prio--); } ret =csgs_upd_ctx_apply_locked (ptdev, &upd_ctx);
@@ -2425,34 +2467,23 @@ tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *cfor (prio =PANTHOR_CSG_PRIORITY_COUNT -1 ; prio >=0 ; prio--) {list_for_each_entry (group, &ctx->groups[prio], run_node) {int csg_id = group->csg_id; -struct panthor_fw_csg_iface *csg_iface; +int csg_prio = new_csg_prio--; -if (csg_id >=0 ) { - new_csg_prio--; +if (csg_id >=0 )continue ; - } csg_id =ffs (free_csg_slots) -1 ;if (drm_WARN_ON (&ptdev->base, csg_id <0 ))break ; - csg_iface =panthor_fw_get_csg_iface (ptdev, csg_id); - csg_slot = &sched->csg_slots[csg_id]; - ret =group_bind_locked (group, csg_id); + ret =tick_ctx_schedule_group (sched, &upd_ctx, group, + csg_id, csg_prio);if (ret) {panthor_device_schedule_reset (ptdev); ctx->csg_upd_failed_mask |=BIT (csg_id);return ; } -csg_slot_prog_locked (ptdev, csg_id, new_csg_prio--); -csgs_upd_ctx_queue_reqs (ptdev, &upd_ctx, csg_id, - group->state ==PANTHOR_CS_GROUP_SUSPENDED ? -CSG_STATE_RESUME :CSG_STATE_START , -CSG_STATE_MASK ); -csgs_upd_ctx_queue_reqs (ptdev, &upd_ctx, csg_id, - csg_iface->output->ack ^CSG_ENDPOINT_CONFIG , -CSG_ENDPOINT_CONFIG ); free_csg_slots &= ~BIT (csg_id); } }
Signature · 1 line
2.43.0
PATCH v5 3/13 drm/panthor: Pass drm_file instead of panthor_file
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
Some sched helpers need info that are part of drm_file, and we will soon need to call drm_gem_object_lookup() from panthor_group_create(). Let's prepare for that by passing a drm_file instead of panthor_file to all current helpers taking a panthor_file, so we keep things consistent.
---
v5:
- Updated kernel doc
v4:
- No change
v3:
- Fixed incorrect parameter passed to panthor_gpu_show_fdinfo()
v2:
- First introduction of this patch in this patch set
--- drivers/gpu/drm/panthor/panthor_drv.c | 38 +++++++++++------------- drivers/gpu/drm/panthor/panthor_mmu.c | 17 ++++++----- drivers/gpu/drm/panthor/panthor_mmu.h | 6 ++-- drivers/gpu/drm/panthor/panthor_sched.c | 39 +++++++++++++++---------- drivers/gpu/drm/panthor/panthor_sched.h | 23 +++++++-------- 5 files changed, 63 insertions(+), 60 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +17 -21
@@ -1106,7 +1106,6 @@ static int panthor_ioctl_bo_mmap_offset(struct drm_device *ddev, void *data,static int panthor_ioctl_group_submit (struct drm_device *ddev,void *data,struct drm_file *file) { -struct panthor_file *pfile = file->driver_priv;struct drm_panthor_group_submit *args = data;struct drm_panthor_queue_submit *jobs_args;struct panthor_submit_ctx ctx;
@@ -1131,8 +1130,7 @@ static int panthor_ioctl_group_submit(struct drm_device *ddev, void *data,const struct drm_panthor_queue_submit *qsubmit = &jobs_args[i];struct drm_sched_job *job; - job =panthor_job_create (pfile, args->group_handle, qsubmit, - file->client_id); + job =panthor_job_create (file, args->group_handle, qsubmit);if (IS_ERR (job)) { ret =PTR_ERR (job); goto out_cleanup_submit_ctx;
@@ -1212,19 +1210,17 @@ static int panthor_ioctl_group_submit(struct drm_device *ddev, void *data,static int panthor_ioctl_group_destroy (struct drm_device *ddev,void *data,struct drm_file *file) { -struct panthor_file *pfile = file->driver_priv;struct drm_panthor_group_destroy *args = data;if (args->pad)return -EINVAL ; -return panthor_group_destroy (pfile, args->group_handle); +return panthor_group_destroy (file, args->group_handle); }static int panthor_ioctl_group_create (struct drm_device *ddev,void *data,struct drm_file *file) { -struct panthor_file *pfile = file->driver_priv;struct drm_panthor_group_create *args = data;struct drm_panthor_queue_create *queue_args;int ret;
@@ -1240,7 +1236,7 @@ static int panthor_ioctl_group_create(struct drm_device *ddev, void *data,if (ret) goto out; - ret =panthor_group_create (pfile, args, queue_args, file->client_id); + ret =panthor_group_create (file, args, queue_args);if (ret <0 ) goto out; args->group_handle = ret;
@@ -1254,10 +1250,9 @@ static int panthor_ioctl_group_create(struct drm_device *ddev, void *data,static int panthor_ioctl_group_get_state (struct drm_device *ddev,void *data,struct drm_file *file) { -struct panthor_file *pfile = file->driver_priv;struct drm_panthor_group_get_state *args = data; -return panthor_group_get_state (pfile, args); +return panthor_group_get_state (file, args); } static int panthor_ioctl_tiler_heap_create(struct drm_device *ddev, void *data,
@@ -1600,6 +1595,7 @@ panthor_open(struct drm_device *ddev, struct drm_file *file)if (!pfile)return -ENOMEM ; + file->driver_priv = pfile; pfile->ptdev = ptdev; pfile->user_mmio.offset =DRM_PANTHOR_USER_MMIO_OFFSET ;
@@ -1614,19 +1610,18 @@ panthor_open(struct drm_device *ddev, struct drm_file *file)#endif - ret =panthor_vm_pool_create (pfile); + ret =panthor_vm_pool_create (file);if (ret) goto err_free_file; - ret =panthor_group_pool_create (pfile); + ret =panthor_group_pool_create (file);if (ret) goto err_destroy_vm_pool; - file->driver_priv = pfile;return 0 ; err_destroy_vm_pool: -panthor_vm_pool_destroy (pfile); +panthor_vm_pool_destroy (file); err_free_file:kfree (pfile);
@@ -1638,8 +1633,8 @@ panthor_postclose(struct drm_device *ddev, struct drm_file *file) {struct panthor_file *pfile = file->driver_priv; -panthor_group_pool_destroy (pfile); -panthor_vm_pool_destroy (pfile); +panthor_group_pool_destroy (file); +panthor_vm_pool_destroy (file);kfree (pfile); }
@@ -1699,11 +1694,13 @@ static int panthor_mmap(struct file *filp, struct vm_area_struct *vma) }static void panthor_gpu_show_fdinfo (struct panthor_device *ptdev, -struct panthor_file *pfile, +struct drm_file *file,struct drm_printer *p) { +struct panthor_file *pfile = file->driver_priv; +if (ptdev->profile_mask &PANTHOR_DEVICE_PROFILING_ALL ) -panthor_fdinfo_gather_group_samples (pfile); +panthor_fdinfo_gather_group_samples (file);if (ptdev->profile_mask &PANTHOR_DEVICE_PROFILING_TIMESTAMP ) {#ifdef CONFIG_ARM_ARCH_TIMER
@@ -1723,11 +1720,10 @@ static void panthor_gpu_show_fdinfo(struct panthor_device *ptdev,static void panthor_show_internal_memory_stats (struct drm_printer *p,struct drm_file *file) {char *drv_name = file->minor->dev->driver->name; -struct panthor_file *pfile = file->driver_priv;struct drm_memory_stats stats = {0 }; -panthor_fdinfo_gather_group_mem_info (pfile, &stats); -panthor_vm_heaps_sizes (pfile, &stats); +panthor_fdinfo_gather_group_mem_info (file, &stats); +panthor_vm_heaps_sizes (file, &stats);drm_fdinfo_print_size (p, drv_name,"resident" ,"memory" , stats.resident);drm_fdinfo_print_size (p, drv_name,"active" ,"memory" , stats.active);
@@ -1738,7 +1734,7 @@ static void panthor_show_fdinfo(struct drm_printer *p, struct drm_file *file)struct drm_device *dev = file->minor->dev; struct panthor_device *ptdev = container_of(dev, struct panthor_device, base); -panthor_gpu_show_fdinfo (ptdev, file->driver_priv, p); +panthor_gpu_show_fdinfo (ptdev, file, p);panthor_show_internal_memory_stats (p, file);drm_show_memory_stats (p, file);
drivers/gpu/drm/panthor/panthor_mmu.c +10 -7
@@ -1676,15 +1676,16 @@ panthor_vm_pool_get_vm(struct panthor_vm_pool *pool, u32 handle)/** * panthor_vm_pool_destroy() - Destroy a VM pool. -* @pfile: File. +* @file: DRM file. * * Destroy all VMs in the pool, and release the pool resources. * * Note that VMs can outlive the pool they were created from if other * objects hold a reference to there VMs. */ -void panthor_vm_pool_destroy (struct panthor_file *pfile) +void panthor_vm_pool_destroy (struct drm_file *file) { +struct panthor_file *pfile = file->driver_priv;struct panthor_vm *vm;unsigned long i;
@@ -1702,12 +1703,13 @@ void panthor_vm_pool_destroy(struct panthor_file *pfile)/** * panthor_vm_pool_create() - Create a VM pool -* @pfile: File. +* @file: DRM file. * * Return: 0 on success, a negative error code otherwise. */ -int panthor_vm_pool_create (struct panthor_file *pfile) +int panthor_vm_pool_create (struct drm_file *file) { +struct panthor_file *pfile = file->driver_priv;struct panthor_gem_object *dummy;int ret;
@@ -1728,7 +1730,7 @@ int panthor_vm_pool_create(struct panthor_file *pfile)return 0 ; err_destroy_vm_pool: -panthor_vm_pool_destroy (pfile); +panthor_vm_pool_destroy (file);return ret; }
@@ -2141,14 +2143,15 @@ struct panthor_heap_pool *panthor_vm_get_heap_pool(struct panthor_vm *vm, bool c/** * panthor_vm_heaps_sizes() - Calculate size of all heap chunks across all * heaps over all the heap pools in a VM -* @pfile: File. +* @file: DRM file. * @stats: Memory stats to be updated. * * Calculate all heap chunk sizes in all heap pools bound to a VM. If the VM * is active, record the size as active as well. */ -void panthor_vm_heaps_sizes (struct panthor_file *pfile,struct drm_memory_stats *stats) +void panthor_vm_heaps_sizes (struct drm_file *file,struct drm_memory_stats *stats) { +struct panthor_file *pfile = file->driver_priv;struct panthor_vm *vm;unsigned long i;
drivers/gpu/drm/panthor/panthor_mmu.h +3 -3
@@ -38,7 +38,7 @@ int panthor_vm_as(struct panthor_vm *vm);struct panthor_heap_pool *panthor_vm_get_heap_pool (struct panthor_vm *vm,bool create); -void panthor_vm_heaps_sizes (struct panthor_file *pfile,struct drm_memory_stats *stats); +void panthor_vm_heaps_sizes (struct drm_file *file,struct drm_memory_stats *stats);struct panthor_vm *panthor_vm_get (struct panthor_vm *vm);void panthor_vm_put (struct panthor_vm *vm);
@@ -64,8 +64,8 @@ void panthor_vm_add_job_fence_to_bos_resvs(struct panthor_vm *vm,struct dma_resv *panthor_vm_resv (struct panthor_vm *vm);struct drm_gem_object *panthor_vm_root_gem (struct panthor_vm *vm); -void panthor_vm_pool_destroy (struct panthor_file *pfile); -int panthor_vm_pool_create (struct panthor_file *pfile); +void panthor_vm_pool_destroy (struct drm_file *file); +int panthor_vm_pool_create (struct drm_file *file);int panthor_vm_pool_create_vm (struct panthor_device *ptdev,struct panthor_vm_pool *pool,struct drm_panthor_vm_create *args);
drivers/gpu/drm/panthor/panthor_sched.c +23 -16
@@ -3117,8 +3117,9 @@ void panthor_sched_post_reset(struct panthor_device *ptdev, bool reset_failed) } } -void panthor_fdinfo_gather_group_samples (struct panthor_file *pfile) +void panthor_fdinfo_gather_group_samples (struct drm_file *file) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool = pfile->groups;struct panthor_group *group;unsigned long i;
@@ -3654,11 +3655,11 @@ static void add_group_kbo_sizes(struct panthor_device *ptdev,#define MAX_GROUPS_PER_POOL 128 -int panthor_group_create (struct panthor_file *pfile, +int panthor_group_create (struct drm_file *file,const struct drm_panthor_group_create *group_args, -const struct drm_panthor_queue_create *queue_args, -u64 drm_client_id) +const struct drm_panthor_queue_create *queue_args) { +struct panthor_file *pfile = file->driver_priv;struct panthor_device *ptdev = pfile->ptdev;struct panthor_group_pool *gpool = pfile->groups;struct panthor_scheduler *sched = ptdev->scheduler;
@@ -3756,7 +3757,8 @@ int panthor_group_create(struct panthor_file *pfile, goto err_put_group;for (i =0 ; i < group_args->queues.count; i++) { - group->queues[i] =group_create_queue (group, &queue_args[i], drm_client_id, gid, i); + group->queues[i] =group_create_queue (group, &queue_args[i], + file->client_id, gid, i);if (IS_ERR (group->queues[i])) { ret =PTR_ERR (group->queues[i]); group->queues[i] =NULL ;
@@ -3796,8 +3798,9 @@ int panthor_group_create(struct panthor_file *pfile,return ret; } -int panthor_group_destroy (struct panthor_file *pfile,u32 group_handle) +int panthor_group_destroy (struct drm_file *file,u32 group_handle) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool = pfile->groups;struct panthor_device *ptdev = pfile->ptdev;struct panthor_scheduler *sched = ptdev->scheduler;
@@ -3848,9 +3851,10 @@ static struct panthor_group *group_from_handle(struct panthor_group_pool *pool,return group; } -int panthor_group_get_state (struct panthor_file *pfile, +int panthor_group_get_state (struct drm_file *file,struct drm_panthor_group_get_state *get_state) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool = pfile->groups;struct panthor_device *ptdev = pfile->ptdev;struct panthor_scheduler *sched = ptdev->scheduler;
@@ -3881,8 +3885,9 @@ int panthor_group_get_state(struct panthor_file *pfile,return 0 ; } -int panthor_group_pool_create (struct panthor_file *pfile) +int panthor_group_pool_create (struct drm_file *file) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool; gpool =kzalloc_obj (*gpool);
@@ -3894,8 +3899,9 @@ int panthor_group_pool_create(struct panthor_file *pfile)return 0 ; } -void panthor_group_pool_destroy (struct panthor_file *pfile) +void panthor_group_pool_destroy (struct drm_file *file) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool = pfile->groups;struct panthor_group *group;unsigned long i;
@@ -3904,7 +3910,7 @@ void panthor_group_pool_destroy(struct panthor_file *pfile)return ;xa_for_each (&gpool->xa, i, group) -panthor_group_destroy (pfile, i); +panthor_group_destroy (file, i);xa_destroy (&gpool->xa);kfree (gpool);
@@ -3914,14 +3920,15 @@ void panthor_group_pool_destroy(struct panthor_file *pfile)/** * panthor_fdinfo_gather_group_mem_info() - Retrieve aggregate size of all private kernel BO's * belonging to all the groups owned by an open Panthor file -* @pfile: File. +* @file: DRM file. * @stats: Memory statistics to be updated. * */ void -panthor_fdinfo_gather_group_mem_info (struct panthor_file *pfile, +panthor_fdinfo_gather_group_mem_info (struct drm_file *file,struct drm_memory_stats *stats) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool = pfile->groups;struct panthor_group *group;unsigned long i;
@@ -3984,11 +3991,11 @@ struct panthor_vm *panthor_job_vm(struct drm_sched_job *sched_job) }struct drm_sched_job * -panthor_job_create (struct panthor_file *pfile, +panthor_job_create (struct drm_file *file,u16 group_handle, -const struct drm_panthor_queue_submit *qsubmit, -u64 drm_client_id) +const struct drm_panthor_queue_submit *qsubmit) { +struct panthor_file *pfile = file->driver_priv;struct panthor_group_pool *gpool = pfile->groups;struct panthor_job *job;u32 credits;
@@ -4059,7 +4066,7 @@ panthor_job_create(struct panthor_file *pfile, ret =drm_sched_job_init (&job->base, &job->group->queues[job->queue_idx]->entity, - credits, job->group, drm_client_id); + credits, job->group, file->client_id);if (ret) goto err_put_job;
drivers/gpu/drm/panthor/panthor_sched.h +10 -13
@@ -16,31 +16,28 @@ struct drm_panthor_queue_create;struct drm_panthor_group_get_state ;struct drm_panthor_queue_submit ;struct panthor_device ; -struct panthor_file ;struct panthor_group_pool ;struct panthor_job ; -int panthor_group_create (struct panthor_file *pfile, +int panthor_group_create (struct drm_file *file,const struct drm_panthor_group_create *group_args, -const struct drm_panthor_queue_create *queue_args, -u64 drm_client_id); -int panthor_group_destroy (struct panthor_file *pfile,u32 group_handle); -int panthor_group_get_state (struct panthor_file *pfile, +const struct drm_panthor_queue_create *queue_args); +int panthor_group_destroy (struct drm_file *file,u32 group_handle); +int panthor_group_get_state (struct drm_file *file,struct drm_panthor_group_get_state *get_state);struct drm_sched_job * -panthor_job_create (struct panthor_file *pfile, +panthor_job_create (struct drm_file *file,u16 group_handle, -const struct drm_panthor_queue_submit *qsubmit, -u64 drm_client_id); +const struct drm_panthor_queue_submit *qsubmit);struct drm_sched_job *panthor_job_get (struct drm_sched_job *job);struct panthor_vm *panthor_job_vm (struct drm_sched_job *sched_job);void panthor_job_put (struct drm_sched_job *job);void panthor_job_update_resvs (struct drm_exec *exec,struct drm_sched_job *job); -int panthor_group_pool_create (struct panthor_file *pfile); -void panthor_group_pool_destroy (struct panthor_file *pfile); -void panthor_fdinfo_gather_group_mem_info (struct panthor_file *pfile, +int panthor_group_pool_create (struct drm_file *file); +void panthor_group_pool_destroy (struct drm_file *file); +void panthor_fdinfo_gather_group_mem_info (struct drm_file *pfile,struct drm_memory_stats *stats);int panthor_sched_init (struct panthor_device *ptdev);
@@ -54,7 +51,7 @@ void panthor_sched_report_mmu_fault(struct panthor_device *ptdev);void panthor_sched_prepare_for_vm_destruction (struct panthor_device *ptdev);void panthor_sched_report_fw_events (struct panthor_device *ptdev,u32 events); -void panthor_fdinfo_gather_group_samples (struct panthor_file *pfile); +void panthor_fdinfo_gather_group_samples (struct drm_file *file);#ifdef CONFIG_DEBUG_FS void panthor_sched_debugfs_init (struct drm_minor *minor);
Signature · 1 line
2.43.0
PATCH v5 4/13 drm/panthor: Don't allocate protm_suspend_buf
In reply to Ketil JohnsenThe PROTM suspend buffer is only needed if the group is going to use PROTM in the first place, so let's not assume we need one until we're being asked to.
--- drivers/gpu/drm/panthor/panthor_sched.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/gpu/drm/panthor/panthor_sched.c +4 -10
@@ -3641,9 +3641,11 @@ static void add_group_kbo_sizes(struct panthor_device *ptdev,if (drm_WARN_ON (&ptdev->base, ptdev != group->ptdev))return ; - group->fdinfo.kbo_sizes += group->suspend_buf->obj->size; - group->fdinfo.kbo_sizes += group->protm_suspend_buf->obj->size; group->fdinfo.kbo_sizes += group->syncobjs->obj->size; + group->fdinfo.kbo_sizes += group->suspend_buf->obj->size; + +if (group->protm_suspend_buf) + group->fdinfo.kbo_sizes += group->protm_suspend_buf->obj->size;for (i =0 ; i < group->queue_count; i++) { queue = group->queues[i];
@@ -3724,14 +3726,6 @@ int panthor_group_create(struct drm_file *file, goto err_put_group; } - suspend_size = csg_iface->control->protm_suspend_size; - group->protm_suspend_buf =panthor_fw_alloc_suspend_buf_mem (ptdev, suspend_size); -if (IS_ERR (group->protm_suspend_buf)) { - ret =PTR_ERR (group->protm_suspend_buf); - group->protm_suspend_buf =NULL ; - goto err_put_group; - } - group->syncobjs = panthor_kernel_bo_create(ptdev, group->vm, group_args->queues.count * sizeof(struct panthor_syncobj_64b),
Signature · 1 line
2.43.0
PATCH v5 5/13 drm/panthor: Add support for entering and exiting protected mode
In reply to Ketil JohnsenFrom: Florent Tomasin <[email protected]>
This patch modifies the Panthor driver code to allow handling of the GPU HW protected mode enter and exit.
The logic added by this patch includes:
- the mechanisms needed for entering and exiting protected mode.
- the handling of protected mode IRQs and FW interactions.
- the scheduler changes needed to decide when to enter protected mode based on CSG scheduling.
- GPU fault handling during protected mode execution.
Note that the submission of protected mode jobs are done from the user space.
The following is a summary of how protected mode is entered and exited:
- When the GPU detects a protected mode job needs to be executed, an IRQ is sent to the CPU to notify the kernel driver that the job is blocked until the GPU has entered protected mode. The entering of protected mode is controlled by the kernel driver.
- The Mali Panthor CSF driver will schedule a tick and evaluate which CS in the CSG to schedule on slot needs protected mode. If the priority of the CSG is not sufficiently high, the protected mode job will not progress until the CSG is scheduled at top priority.
- The Panthor scheduler notifies the GPU that the blocked protected jobs will soon be able to progress.
- Once all CSG and CS slots are updated, the scheduler requests the GPU to enter protected mode and waits for it to be acknowledged.
- If successful, all protected mode jobs will resume execution while normal mode jobs block until the GPU exits protected mode, or the kernel driver rotates the CSGs and forces the GPU to exit protected mode.
- If unsuccessful, the scheduler will request a GPU reset.
- Faults during protected mode are reported GPU wide, and not as CSG/CS errors. We allow only one CSG to run in protected mode at a time so we know which CSG to blame for the fault.
- All faults during protected mode are handled with a GPU reset.
This sequence will continue so long the user space is submitting protected mode jobs.
---
v5:
- Use empty transtab trickery for "dormant" VMs during protm instead of the "delayed update" mask in previous version.
- Ditched "try block", no need to block protm in reclaim path.
- Split remap_evicted_vma() to avoid mixed cleanup approaches.
- Take protm.lock in panthor_sched_post_reset()
- Halt processing of async VM ops for VM executing in protm.
- Minor formating fixes.
- Minor kernel doc fixes.
v4:
- Do not pick any more groups if first group requests protm. Prevous versions allowed lower pri groups to stay on slot.
- Moved the rwsem for protm sync into scheduler.
- Added protm exit handling to start of scheduler tick.
- Scheduler will notify MMU code when it enters and exits protm.
- When in protected mode, MMU code will do "delayed updates" of VMs which are not "running". This avoids unnecessary preemptions and blocking during unrelated VM_BIND ops.
- Page table updates to FW VM and currently running protm VM can not proceed before protm is exited (preempted).
- Ensure ticking during protm (so we can evaluate preemption).
- Added special protm preempt code with preempt_protm_group().
- Poll for protm enter and exit without FW event wait queue.
Protm enter simply use read_poll_timeout().
Protm exit use a custom loop as this needs to be interruptible (long waits).
- Reversed check order in panthor_gpu_protm_fault_pending()
- Do not print error message in panthor_fw_protm_exit(), leave that to caller since it can some times be expected.
- Reintroduce disabling of GPU_IRQ_PROTM_FAULT while handling them.
- Reset protm_fault and re-enable GPU_IRQ_PROTM_FAULT after FW have re-started.
- Tweaks to tick_ctx_handle_protm_group(), take events_lock and ack everything!
- Use "protm block" with DEFINE_GUARD helpers (MMU code).
- Minor spelling/wording and naming changes.
v3:
- Rebase
- Required changes due to new IRQ handling (events_lock).
- Required changes due to memory reclaim (more places to sync with protm).
- Count number of enter and exits to and from protected mode.
- Added helper function wait_protm_enter() to check protm enter condition, using the counters mentioned above.
- Tweaks to timeouts.
- Removed disable/enable of GPU_IRQ_PROTM_FAULT. No longer recall what issue it was supposed to fix.
- protm_fault changed to atomic.
- Moved sync with protm out from panthor_vm_lock_region() and panthor_vm_unlock_region().
- Avoids special dealing with as.slots_lock and GPU reset.
- Block and unblock of protm in same function, so easier to see.
- We no longer need to add panthor_vm_expand_locked_region() (different patch).
- The downside is that we might block protm mode more than necessary.
- Added panthor_sched_protm_try_block() used by memory reclaim case.
- protm_pending_queues made atomic (following the same change for fatal_queues).
- Only clear bits from protm_pending_queues when we ACK CS_PROTM_PENDING.
- Clear any pending CS_PROTM_PENDING on slot reset.
- Ack all CS_PROTM_PENDING in tick_ctx_handle_protm_group(), no matter the value of protm_pending_queues.
- Inlined helper function panthor_sched_protm_enter().
v2:
- Heavily reworked, although conceptually similar to v1.
--- drivers/gpu/drm/panthor/panthor_device.h | 8 + drivers/gpu/drm/panthor/panthor_fw.c | 94 ++++- drivers/gpu/drm/panthor/panthor_fw.h | 4 + drivers/gpu/drm/panthor/panthor_gpu.c | 49 ++- drivers/gpu/drm/panthor/panthor_gpu.h | 4 + drivers/gpu/drm/panthor/panthor_mmu.c | 351 +++++++++++++----- drivers/gpu/drm/panthor/panthor_mmu.h | 5 + drivers/gpu/drm/panthor/panthor_sched.c | 438 +++++++++++++++++++++-- drivers/gpu/drm/panthor/panthor_sched.h | 16 + 9 files changed, 847 insertions(+), 122 deletions(-)
drivers/gpu/drm/panthor/panthor_device.h +8 -0
@@ -336,6 +336,14 @@ struct panthor_device {struct list_head node; } gems;#endif +/** @protm: Protected mode related data. */ +struct { +/** @protm.protm_enter_count: Number of times we entered protm. */ +atomic64_t protm_enter_count; + +/** @protm.protm_exit_count: Number of times we exited protm. */ +atomic64_t protm_exit_count; + } protm; }; struct panthor_gpu_usage {
drivers/gpu/drm/panthor/panthor_fw.c +93 -1
112 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_fw.h +4 -0
@@ -529,4 +529,8 @@ static inline int panthor_fw_resume(struct panthor_device *ptdev)int panthor_fw_init (struct panthor_device *ptdev);void panthor_fw_unplug (struct panthor_device *ptdev); +int panthor_fw_protm_enter (struct panthor_device *ptdev); +int panthor_fw_protm_exit (struct panthor_device *ptdev,u32 timeout_ms); +int panthor_fw_protm_exit_wait (struct panthor_device *ptdev,u32 timeout_ms); +#endif
drivers/gpu/drm/panthor/panthor_gpu.c +47 -2
82 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_gpu.h +4 -0
@@ -60,4 +60,8 @@ u64 panthor_gpu_get_cycle_count(struct panthor_device *ptdev);int panthor_gpu_coherency_init (struct panthor_device *ptdev); +u32 panthor_gpu_status (struct panthor_device *ptdev); + +bool panthor_gpu_protm_fault_pending (struct panthor_device *ptdev); +#endif
drivers/gpu/drm/panthor/panthor_mmu.c +255 -96
422 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_mmu.h +5 -0
@@ -106,6 +106,11 @@ void panthor_vm_update_resvs(struct panthor_vm *vm, struct drm_exec *exec,int panthor_mmu_pt_cache_init (void );void panthor_mmu_pt_cache_fini (void ); +bool panthor_vm_for_mcu (struct panthor_vm *vm); + +void panthor_vm_protm_enter (struct panthor_vm *vm); +void panthor_vm_protm_exit (struct panthor_vm *vm); +#ifdef CONFIG_DEBUG_FS void panthor_mmu_debugfs_init (struct drm_minor *minor);#endif
drivers/gpu/drm/panthor/panthor_sched.c +415 -23
613 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_sched.h +16 -0
@@ -53,6 +53,22 @@ void panthor_sched_report_fw_events(struct panthor_device *ptdev, u32 events);void panthor_fdinfo_gather_group_samples (struct drm_file *file); +enum protm_block_result { +/* GPU is currently, and will stay, in normal mode */ +PROTM_BLOCK_NORMAL , + +/* GPU might be in protected mode serving another vm */ +PROTM_BLOCK_DORMANT , + +/* GPU error */ +PROTM_BLOCK_ERROR +}; + +enum protm_block_result +panthor_sched_protm_block_timeout (struct panthor_device *ptdev, +struct panthor_vm *vm); +void panthor_sched_protm_unblock (struct panthor_device *ptdev); +#ifdef CONFIG_DEBUG_FS void panthor_sched_debugfs_init (struct drm_minor *minor);#endif
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 05/13] drm/panthor: Add support for entering and exiting protected mode
Thank you for your contribution! Sashiko AI review found 16 potential issues to consider: - [Critical] drm/panthor: wrong page order get_order(size) * 2 in alloc_pt() and free_pt() - [High]…
Thank you for your contribution! Sashiko AI review found 16 potential issues to consider:
- [Critical] drm/panthor: wrong page order get_order(size) * 2 in alloc_pt() and free_pt()
- [High] drm/panthor: missing sched->lock and CS slot reset in handle_protm_fault()
- [High] drm/panthor: deadlock in tick_ctx_apply() stopping VM under protm.lock
- [High] drm/panthor: race between handle_as_fault_locked() and dormant VM updates
- [High] drm/panthor: missing protm sync in panthor_vm_evict_bo_mappings_locked()
- [High] drm/panthor: protm_fault reset in panthor_gpu_soft_reset() skipped on v14
- [High] drm/panthor: signal in panthor_fw_protm_exit_wait() breaks VM teardown
- [High] drm/panthor: preempt_protm_group() ignores runnable groups bound to CSG slots
- [High] drm/panthor: missing active_group cleanup on unplug, suspend, and VM destroy
- [High] drm/panthor: stale protm_pending_queues across CS reset and early ack before resume
- [Medium] drm/panthor: skipped reset cleanup and active_group race in handle_protm_fault()
- [Medium] drm/panthor: missing rollback and return on panthor_fw_protm_enter() error
- [Medium] drm/panthor: race on GLB_PING between panthor_fw_protm_exit() and ping_work
- [Medium] drm/panthor: panthor_sched_protm_block_timeout() waits under protm.lock
- [Low] drm/panthor: panthor_vm_exec_op() mixes scoped_cond_guard() and goto
- [Low] drm/panthor: inverted comment for panthor_scheduler::protm.active_group
--
commit 692a26958a72ddf6fd6351559d386fc6e296f3d5
Author: Florent Tomasin <[email protected]>
drm/panthor: Add support for entering and exiting protected mode
This commit adds support for entering and exiting hardware protected mode across the Panthor firmware interface, GPU interrupt handler, MMU, and CSG scheduler.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_fw.c +0
-0
[ ... ]
Quoted 1 line
drivers/gpu/drm/panthor/panthor_fw.c +0
-0@@ -1476,6 +1481,93 @@ static void panthor_fw_ping_work(struct work_struct *work)
[ ... ]
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_fw.c +9
-0+int panthor_fw_protm_exit_wait (struct panthor_device *ptdev,u32 timeout_ms) +{ +unsigned long deadline = jiffies +msecs_to_jiffies (timeout_ms); + + do { + if (!(panthor_gpu_status(ptdev) & GPU_STATUS_PROTM_ACTIVE)) + return 0; + + if (signal_pending(current))
drivers/gpu/drm/panthor/panthor_fw.c static void panthor_fw_ping_work(struct work_struct *work) · line 1557
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_fw.c +10
-0+return -ERESTARTSYS ; + +usleep_range_state (100 ,200 ,TASK_INTERRUPTIBLE ); + }while (!time_after_eq (jiffies, deadline)); + +if (!(panthor_gpu_status (ptdev) &GPU_STATUS_PROTM_ACTIVE )) +return 0 ; + +return -ETIMEDOUT ; +}
[Severity: High]
Does returning -ERESTARTSYS here on a pending signal break non-restartable teardown paths such as panthor_vm_destroy() and panthor_kernel_bo_destroy()?
When a process is killed with SIGKILL while a group is in protected mode, signal_pending(current) remains true during exit teardown. In panthor_sched_protm_block_timeout(), the initial 5 ms panthor_fw_protm_exit_wait() immediately returns -ERESTARTSYS, which is treated as a timeout: it calls panthor_fw_protm_exit(), sends GLB_PING to preempt protected mode, and calls panthor_fw_protm_exit_wait() a second time, which again fails immediately without waiting for the GPU to exit protected mode. scoped_cond_guard(protm_block_timeout, ret = -ETIMEDOUT, vm) then turns that into -ETIMEDOUT and skips the unmap in panthor_vm_exec_op().
In panthor_vm_destroy(), panthor_sched_prepare_for_vm_destruction() only flushes tick_work once, and that first tick_work run only requests protected mode exit without clearing sched->protm.active_group or unbinding the group. The subsequent panthor_vm_unmap_range() therefore hits this path, fails with drm_WARN_ON(), and leaks the VM, its VMAs, page tables, and mapped BOs. In panthor_kernel_bo_destroy(), the failed unmap is ignored while the VA range and backing pages are freed with the page table mappings still present.
drivers/gpu/drm/panthor/panthor_fw.c static void panthor_fw_ping_work(struct work_struct *work) · line 1569
Quoted 12 lines
drivers/gpu/drm/panthor/panthor_fw.c +12
-0+ +int panthor_fw_protm_exit (struct panthor_device *ptdev,u32 timeout_ms) +{ +struct panthor_fw_global_iface *glb_iface = +panthor_fw_get_glb_iface (ptdev); + +/* Send PING request to force an exit */ +panthor_fw_toggle_reqs (glb_iface, req, ack,GLB_PING ); +panthor_fw_ring_doorbell (ptdev,CSF_GLB_DOORBELL_ID ); + +return panthor_fw_protm_exit_wait (ptdev, timeout_ms); +}
[Severity: Medium]
Can toggling GLB_PING here race with panthor_fw_ping_work() and trigger a spurious GPU reset?
CPU0 (reset.wq) CPU1panthor_fw_ping_work()
panthor_fw_toggle_reqs(..., GLB_PING)
panthor_fw_glb_wait_acks(..., GLB_PING, ...)
req = READ_ONCE(*req_ptr) & GLB_PING
/* MCU acks first ping: *ack_ptr == req */
panthor_fw_protm_exit()
panthor_fw_toggle_reqs(..., GLB_PING)
/* flips *req_ptr and MCU flips *ack_ptr */
wait_event_timeout(..., (*ack_ptr & GLB_PING) == req, ...)
--> times out (-ETIMEDOUT) panthor_device_schedule_reset(ptdev)
panthor_fw_wait_acks() snapshots the expected GLB_PING bit in req before sleeping on req_waitqueue. If panthor_fw_protm_exit() toggles GLB_PING again after the MCU acknowledges panthor_fw_ping_work()'s ping but before panthor_fw_ping_work() wakes up and re-reads *ack_ptr, the ack bit is flipped to the opposite polarity and panthor_fw_ping_work() times out and schedules a reset.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_gpu.c +0
-0
[ ... ]
drivers/gpu/drm/panthor/panthor_gpu.c int panthor_gpu_soft_reset(struct panthor_device *ptdev) · line 476
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_gpu.c +3
-0@@ -431,6 +468,9 @@ int panthor_gpu_soft_reset(struct panthor_device *ptdev)return -ETIMEDOUT ; } +atomic_set (&ptdev->gpu->protm_fault,0 ); +panthor_irq_enable_events (&ptdev->gpu->irq,GPU_IRQ_PROTM_FAULT ); + scoped_guard(spinlock, &ptdev->gpu->reqs_lock) { ptdev->gpu->pending_reqs =0 ; }
[Severity: High]
Does clearing protm_fault and re-enabling GPU_IRQ_PROTM_FAULT only in panthor_gpu_soft_reset() leave protected mode permanently broken after a fault on Arch v14 GPUs?
panthor_device_reset_work() calls panthor_hw_soft_reset(ptdev), which on Arch v14 dispatches to panthor_pwr_reset_soft() rather than panthor_gpu_soft_reset(). On those GPUs (and on v10-v13 if a pending reset is coalesced into a suspend/resume cycle where fast suspend skips panthor_hw_soft_reset()), ptdev->gpu->protm_fault stays 1 and GPU_IRQ_PROTM_FAULT stays masked forever, causing panthor_gpu_protm_fault_pending() to always return true and sched_process_protm_exit_event_locked() to ignore all subsequent GLB_PROTM_EXIT events.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0
[ ... ]
drivers/gpu/drm/panthor/panthor_mmu.c static void *alloc_pt(void *cookie, size_t size, gfp_t gfp) · line 562
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_mmu.c +2
-1@@ -475,9 +554,10 @@ static void *alloc_pt(void *cookie, size_t size, gfp_t gfp)drm_WARN_ON (&vm->ptdev->base, vm->op_ctx); p =alloc_pages_node (dev_to_node (vm->ptdev->base.dev), - gfp | __GFP_ZERO,get_order (size)); + gfp | __GFP_ZERO,get_order (size) *2 ); page = p ?page_address (p) :NULL ; vm->root_page_table = page; + vm->empty_transtab_offset = size;return page; }
[Severity: Critical]
Doesn't get_order(size) * 2 evaluate to 0 when size is SZ_4K, allocating only a single page while setting vm->empty_transtab_offset = size?
When size is PAGE_SIZE, get_order(size) is 0, so alloc_pages_node() allocates one page, and cfg->arm_lpae_s1_cfg.ttbr + vm->empty_transtab_offset in panthor_mmu_set_empty_pt() points one page past the allocation into unrelated physical memory that the GPU MMU then walks as its root page table.
In addition:
- free_pt() still calls free_pages((unsigned long)data, get_order(size)), which does not match a doubled allocation order (get_order(size) + 1).
- panthor_mmu_set_empty_pt() and handle_as_fault_locked() only write the lower 32 bits of AS_TRANSTAB via gpu_write() instead of gpu_write64(), which can truncate the address if ttbr + vm->empty_transtab_offset crosses a 4 GB boundary.
- On non-coherent devices (!ptdev->coherent), __arm_lpae_cfg_alloc() only DMA-maps/flushes the first size bytes of the root table, leaving the zeroed second half at empty_transtab_offset unflushed in CPU caches.
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_mmu.c +18
-0@@ -1847,6 +1927,79 @@ static void panthor_vm_unlock_region(struct panthor_vm *vm)mutex_unlock (&ptdev->mmu->as.slots_lock); } +static bool handle_as_fault_locked (struct panthor_device *ptdev,u32 as) +{ +struct panthor_mmu *mmu = ptdev->mmu; +struct panthor_vm *vm = mmu->as.slots[as].vm; +struct io_pgtable_cfg *cfg; +u64 actual_transtab; +u32 empty_transtab_low; +u32 cur_transtab_low; +int ret; + +lockdep_assert_held (&ptdev->mmu->as.slots_lock); + +if (!vm) +return false; + + cfg = &io_pgtable_ops_to_pgtable (vm->pgtbl_ops)->cfg; + actual_transtab = cfg->arm_lpae_s1_cfg.ttbr; + empty_transtab_low = (u32)(actual_transtab + vm->empty_transtab_offset);
drivers/gpu/drm/panthor/panthor_mmu.c static void panthor_vm_unlock_region(struct panthor_vm *vm) · line 1957
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+ cur_transtab_low =gpu_read (mmu->iomem,AS_TRANSTAB (as)); + +if (cur_transtab_low != empty_transtab_low) +return false; + +gpu_write (mmu->iomem,AS_TRANSTAB (as), (u32 )actual_transtab); + ret =as_send_cmd_and_wait (ptdev, as,AS_COMMAND_UPDATE ); + +return ret ? false : true; +}
[Severity: High]
Can handle_as_fault_locked() restore actual_transtab while a dormant VM's page tables are still being modified on another CPU?
CPU0 CPU1 (MMU IRQ thread)panthor_vm_exec_op(vm)
/* holds vm->op_lock and
* down_read(&sched->protm.lock) */
protm_block_timeout(vm)
panthor_mmu_set_empty_pt(vm)
/* sets empty_transtab_low,
* drops mmu->as.slots_lock */
panthor_vm_lock_region(vm)
/* AS_COMMAND_LOCK ignored in protm,
* drops mmu->as.slots_lock */
drm_gpuvm_sm_map() / sm_unmap()
/* modifying actual_transtab */
/* GPU finishes protm job, resumes
* normal CSG on as, faults on
* empty_transtab_low */
panthor_mmu_irq_handler()
mutex_lock(&mmu->as.slots_lock)
handle_as_fault_locked()
gpu_write(..., AS_TRANSTAB(as),
actual_transtab)
as_send_cmd_and_wait(...,
AS_COMMAND_UPDATE)
<-- MMU walks partially updated PTsHolding sched->protm.lock in read mode does not stop the GPU from completing its protected mode work and automatically resuming normal-mode CSGs on other slots. Because handle_as_fault_locked() only takes mmu->as.slots_lock and does not synchronize with vm->op_lock, it can switch AS_TRANSTAB(as) back to actual_transtab while the page table walk structures are being modified in place without an active hardware region lock.
Conversely, if the GPU already exited protected mode before panthor_sched_protm_block_timeout() ran (before tick_work() cleared sched->protm.active_group), panthor_mmu_set_empty_pt() installs empty_transtab_low while in normal mode; if that VM then enters protected mode before taking a normal-mode page fault, AS_TRANSTAB still points to the empty table and triggers a fatal GPU_IRQ_PROTM_FAULT.
[ ... ]
Quoted 7 lines
drivers/gpu/drm/panthor/panthor_mmu.c +3
-0@@ -2563,12 +2680,55 @@ static struct panthor_vma *select_evicted_vma(struct drm_gpuvm_bo *vm_bo,return first_evicted_vma; } +static int remap_evicted_vma_locked (struct panthor_vm *vm, +struct panthor_vma *evicted_vma, +struct panthor_vm_op_ctx *op_ctx)
drivers/gpu/drm/panthor/panthor_mmu.c static struct panthor_vma *select_evicted_vma(struct drm_gpuvm_bo *vm_bo, · line 2695
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +10
-0+{ +struct panthor_gem_object *bo = +to_panthor_bo (evicted_vma->base.gem.obj); +int ret; + +lockdep_assert_held (&vm->op_lock); + + scoped_cond_guard(protm_block_timeout, ret = -ETIMEDOUT, vm) + { + vm->op_ctx = op_ctx;
[Severity: High]
While remap_evicted_vma_locked() blocks protected mode here, what protects panthor_vm_evict_bo_mappings_locked() in the shrinker eviction path just above?
drivers/gpu/drm/panthor/panthor_mmu.c:panthor_vm_evict_bo_mappings_locked() {
...
ret = panthor_vm_lock_region(vm, va->va.addr, va->va.range);
if (ret)
break;
panthor_vm_unmap_pages(vm, va->va.addr, va->va.range);
panthor_vm_unlock_region(vm);
vma->evicted = true;
...
}A VM whose BOs are being evicted can still hold an active AS slot (vm->as.id >= 0), either on mmu->as.lru_list or bound to an idle CSG slot. If eviction runs while the GPU is in protected mode, AS_COMMAND_LOCK, the cache flush, and AS_COMMAND_UNLOCK are ignored by the MMU and panthor_mmu_set_empty_pt() is never called, leaving stale GPU TLB entries pointing to physical pages that panthor_gem_evict_locked() immediately frees. If eviction instead races with protected mode entry, the GPU can enter protected mode with AS_COMMAND_LOCK still pending on that AS.
[ ... ]
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-9@@ -2716,39 +2857,42 @@ panthor_vm_exec_op(struct panthor_vm *vm, struct panthor_vm_op_ctx *op,mutex_lock (&vm->op_lock); vm->op_ctx = op; - ret =panthor_vm_lock_region (vm, op->va.addr, op->va.range); -if (ret) - goto out; - -switch (op_type) { -case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP : { -const struct drm_gpuvm_map_req map_req = { - .map.va.addr = op->va.addr, - .map.va.range = op->va.range,
drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_exec_op(struct panthor_vm *vm, struct panthor_vm_op_ctx *op, · line 2863
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-6- .map.gem.obj = op->map.vm_bo->obj, - .map.gem.offset = op->map.bo_offset, - }; - -if (vm->unusable) { - ret = -EINVAL ; + scoped_cond_guard(protm_block_timeout, ret = -ETIMEDOUT, vm) { + ret =panthor_vm_lock_region (vm, op->va.addr, op->va.range); +if (ret) + goto out;
[Severity: Low]
This isn't a bug, but jumping out of scoped_cond_guard() with goto out mixes cleanup helpers with goto-based error handling in the same function; splitting out a locked helper like remap_evicted_vma_locked() would avoid mixing the two styles.
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_sched.c +0
-0@@ -307,6 +307,44 @@ struct panthor_scheduler {
[ ... ]
drivers/gpu/drm/panthor/panthor_sched.c struct panthor_scheduler { · line 348
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_sched.c +12
-0+/** +* @protm.active_group: The active protected group. +* +* We only allow one protected group to run at the same time, +* as it makes it easier to handle faults in protected mode. +* +* If the active_group member is set, then the scheduler +* believes the GPU is in protected mode. If not set, then the +* GPU is for sure not in normal mode. +*/ +struct panthor_group *active_group; + } protm; };
[Severity: Low]
The last sentence of this comment says "not in normal mode" where "in normal mode" (or "not in protected mode") was meant.
[ ... ]
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_sched.c +0
-0@@ -1983,6 +2083,66 @@ group_unbind_locked(struct panthor_group *group)return 0 ; }
drivers/gpu/drm/panthor/panthor_sched.c group_unbind_locked(struct panthor_group *group) · line 2095
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +10
-0+static void handle_protm_fault (struct panthor_device *ptdev) +{ +struct panthor_scheduler *sched = ptdev->scheduler; +u32 csg_id; +struct panthor_group *protm_group; + + protm_group = sched->protm.active_group; + +if (!protm_group || !panthor_gpu_protm_fault_pending (ptdev)) +return ;
[Severity: Medium]
What happens if a GPU reset is scheduled while in protected mode without GPU_IRQ_PROTM_FAULT being raised, such as when panthor_fw_protm_exit() times out during preemption in tick_work() or a protected job hits a queue timeout?
Because panthor_gpu_protm_fault_pending(ptdev) is false, handle_protm_fault() returns early without exiting protected mode or unbinding CSGs. panthor_sched_suspend() then issues CSG_STATE_SUSPEND while the GPU is still stuck in protected mode, timing out and terminating innocent normal-mode groups bound to the other CSG slots.
Conversely, when a fault raises both GPU_IRQ_PROTM_FAULT and GLB_PROTM_EXIT, sched_process_protm_exit_event_locked() increments protm_exit_count before returning. If a queued tick_work() runs before panthor_gpu_irq_handler() sets reset.pending, tick_work() sees protm_exit_count == protm_enter_count and clears sched->protm.active_group without checking panthor_gpu_protm_fault_pending(), so handle_protm_fault() sees !protm_group and never marks protm_group->fatal_queues.
Quoted 39 lines
drivers/gpu/drm/panthor/panthor_sched.c +39
-0+ +atomic_set (&protm_group->fatal_queues, +GENMASK (protm_group->queue_count -1 ,0 )); + +/* Different kinds of faults during protected mode can give different +* behavior/state. +* Case 1) The fault keeps the GPU in protected mode. +* In this case, the request to exit protected mode below will +* fail and we need to take some further action. +* Case 2) The fault do not keep the GPU in protected mode. +* In this case, the request to exit protected +* mode below will succeed, and we don't need to take any +* further action right here. +*/ +if (!panthor_fw_protm_exit (ptdev,500 )) +return ; + +/* GPU failed to exit protected mode. +* Mark all CSGs as suspended and unbind them, so that they are +* unaffected by the GPU reset itself. +* We can not suspend the groups in this case, because we are stuck +* in protected mode. That is also the reason it is safe to unbind +* without suspending first (the groups are already "suspended"). +* The failing protected group will be scheduled for termination. +*/ +for (csg_id =0 ; csg_id < sched->csg_slot_count; csg_id++) { +struct panthor_group *group = sched->csg_slots[csg_id].group; + +if (!group) +continue ; + +group_get (group); + + group->state =PANTHOR_CS_GROUP_SUSPENDED ; +group_unbind_locked (group); + +drm_WARN_ON (&group->ptdev->base, !list_empty (&group->run_node)); + +if (group_can_run (group)) {
drivers/gpu/drm/panthor/panthor_sched.c group_unbind_locked(struct panthor_group *group) · line 2144
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +10
-0+list_add (&group->run_node, + &sched->groups.idle[group->priority]); + }else { +list_del_init (&group->wait_node); +group_queue_work (group, term); + } + +group_put (group); + } +}
[Severity: High]
Isn't handle_protm_fault() calling group_unbind_locked() and modifying sched->groups.idle and group->wait_node without holding sched->lock?
panthor_sched_pre_reset() calls handle_protm_fault() while holding only sched->reset.lock, before panthor_sched_suspend() acquires sched->lock and before panthor_group_stop() stops the per-queue scheduler workers. This trips lockdep_assert_held(&ptdev->scheduler->lock) inside group_unbind_locked() and races with paths that only take sched->lock, such as queue_run_job() (which can read queue->doorbell_id == -1 after group_unbind_locked() clears it and perform an out-of-bounds MMIO write in panthor_fw_ring_doorbell()).
In addition, setting group->state = PANTHOR_CS_GROUP_SUSPENDED directly from PANTHOR_CS_GROUP_ACTIVE skips cs_slot_reset_locked() and queue_suspend_timeout(queue). When panthor_group_stop() then calls disable_delayed_work_sync(&queue->timeout.work) on the surviving groups, queue->timeout.remaining stays at MAX_SCHEDULE_TIMEOUT, so queue_resume_timeout() never restarts their job timeout timers after the reset.
[ ... ]
Quoted 32 lines
drivers/gpu/drm/panthor/panthor_sched.c +28
-0@@ -2292,6 +2474,55 @@ static void group_term_work(struct work_struct *work)group_put (group); } +enum protm_block_result +panthor_sched_protm_block_timeout (struct panthor_device *ptdev, +struct panthor_vm *vm) +{ +struct panthor_scheduler *sched = ptdev->scheduler; +int ret; + +if (!sched) +return PROTM_BLOCK_NORMAL ; + +down_read (&sched->protm.lock); + +if (!sched->protm.active_group) +return PROTM_BLOCK_NORMAL ;/* All good, we are in normal mode */ + +/* We will only exit protected mode for the passed vm and the MCU VM. +* Other VMs have no active CSG work, so MMU updates can proceed, +* although with special caution (delayed updates)! +*/ +if (sched->protm.active_group->vm != vm && !panthor_vm_for_mcu (vm)) +return PROTM_BLOCK_DORMANT ;/* Specified VM can proceed, with caution. */ + +/* Give GPU a chance to finish ongoing protm work by itself */ + ret =panthor_fw_protm_exit_wait (ptdev,5 ); +if (!ret) +return PROTM_BLOCK_NORMAL ;/* All good, we are back to normal mode */ + +/* We do not want this function to fail, because that can lead to
drivers/gpu/drm/panthor/panthor_sched.c static void group_term_work(struct work_struct *work) · line 2514
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +10
-0+* innocent processes failing. That is why we wait up to 2x the job +* timeout for GPU to exit protm here. This shall cover the worst case +* where protm work hangs and we must rely on job timeout to recover. +*/ + ret =panthor_fw_protm_exit (ptdev,JOB_TIMEOUT_MS <<1 ); +if (ret) +up_read (&sched->protm.lock); + +return ret ?PROTM_BLOCK_ERROR :PROTM_BLOCK_NORMAL ; +}
[Severity: Medium]
As reported by Boris Brezillon on v3 [1], waiting for protected mode exit here while holding sched->protm.lock in read mode still races with GPU reset.
If a thread is blocked in panthor_fw_protm_exit_wait() or panthor_fw_protm_exit() (for example from a synchronous VM_BIND, remap_evicted_vma_locked(), or a kernel BO map/unmap on the MCU VM) when panthor_device_reset_work() runs, panthor_hw_soft_reset() clears GPU_STATUS_PROTM_ACTIVE in GPU_STATUS before panthor_mmu_post_reset() and panthor_sched_post_reset() execute. panthor_fw_protm_exit_wait() then sees GPU_STATUS_PROTM_ACTIVE cleared and returns 0 (PROTM_BLOCK_NORMAL) mid-reset while vm->as.id is still set, allowing the caller to issue AS_COMMAND_LOCK, cache flushes, and AS_COMMAND_UNLOCK concurrently with panthor_hw_l2_power_on(), panthor_mmu_post_reset(), and panthor_fw_post_reset() before panthor_sched_post_reset() can take down_write(&sched->protm.lock).
[ ... ]
Quoted 28 lines
drivers/gpu/drm/panthor/panthor_sched.c +24
-0@@ -2419,6 +2650,41 @@ tick_ctx_schedule_group(struct panthor_scheduler *sched,return 0 ; } +static void +tick_ctx_handle_protm_group (struct panthor_scheduler *sched, +struct panthor_csg_slots_upd_ctx *upd_ctx, +struct panthor_group *group) +{ +struct panthor_device *ptdev = sched->ptdev; +struct panthor_fw_csg_iface *csg_iface = +panthor_fw_get_csg_iface (ptdev, group->csg_id); +u32 q; + +if (drm_WARN_ON (&ptdev->base, group->csg_id <0 )) +return ; + +guard (spinlock)(&sched->events_lock); + +for (q =0 ; q < group->queue_count; q++) { +struct panthor_fw_cs_iface *cs_iface = +panthor_fw_get_cs_iface (ptdev, group->csg_id, q); + +/* Ack any pending CS_PROTM_PENDING so it can run in protm */ +if ((cs_iface->output->ack ^ cs_iface->input->req) & +CS_PROTM_PENDING ) + panthor_fw_update_reqs(cs_iface, req, + cs_iface->output->ack,
drivers/gpu/drm/panthor/panthor_sched.c tick_ctx_schedule_group(struct panthor_scheduler *sched, · line 2686
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +10
-0+ CS_PROTM_PENDING); + } + +panthor_fw_toggle_reqs (csg_iface, doorbell_req, doorbell_ack, +GENMASK (group->queue_count -1 ,0 )); + +csgs_upd_ctx_ring_doorbell (upd_ctx, group->csg_id); + +atomic_set (&group->protm_pending_queues,0 ); +}
[Severity: High]
What happens if a group that raised CS_PROTM_PENDING is suspended or evicted before it can enter protected mode, and is later resumed as the highest-priority group?
When the group is suspended, cs_slot_reset_locked() acknowledges CS_PROTM_PENDING on cs_iface, but group->protm_pending_queues is never cleared (unlike group->tiler_oom in group_unbind_locked()). When the suspended group is later picked first, tick_ctx_apply() calls tick_ctx_handle_protm_group() before csgs_upd_ctx_apply_locked() sends CSG_STATE_RESUME to the firmware. Because the group has not been resumed yet, CS_PROTM_PENDING is not asserted on the newly bound CS slot when tick_ctx_handle_protm_group() checks it and clears group->protm_pending_queues. Once csgs_upd_ctx_apply_locked() resumes the group and tick_ctx_apply() immediately calls panthor_fw_protm_enter(), the firmware re-asserts CS_PROTM_PENDING unacknowledged and blocks the queue, causing panthor_fw_protm_enter() to time out and trigger a GPU reset.
Also, panthor_fw_get_csg_iface(ptdev, group->csg_id) at the top of tick_ctx_handle_protm_group() indexes ptdev->fw->iface.csgs[] with group->csg_id before checking group->csg_id < 0.
Quoted 11 lines
drivers/gpu/drm/panthor/panthor_sched.c +3
-0@@ -2488,6 +2754,9 @@ tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *c } } +if (ctx->protm_group) +tick_ctx_handle_protm_group (sched, &upd_ctx, ctx->protm_group); + ret =csgs_upd_ctx_apply_locked (ptdev, &upd_ctx);if (ret) {panthor_device_schedule_reset (ptdev);
drivers/gpu/drm/panthor/panthor_sched.c tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *c · line 2773
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +7
-0@@ -2495,6 +2764,23 @@ tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *creturn ; } +if (ctx->protm_group) { +down_write (&sched->protm.lock); + + sched->protm.active_group = ctx->protm_group; +group_get (sched->protm.active_group); + +panthor_vm_protm_enter (ctx->protm_group->vm);
[Severity: High]
Can calling panthor_vm_protm_enter() while holding sched->protm.lock in write mode ABBA deadlock with an asynchronous VM_BIND job on the same VM?
CPU0 (sched->wq) CPU1 (vm->sched.work_run_job)
---------------- -----------------------------down_write(&sched->protm.lock)
/* inside dma_fence_begin_signalling() */
down_read(&sched->protm.lock)
panthor_vm_protm_enter(vm)
panthor_vm_stop(vm)
drm_sched_stop(&vm->sched, NULL)
cancel_work_sync(&vm->sched.work_run_job)*** DEADLOCK ***
CPU0: tick_work() -> tick_ctx_apply() -> panthor_vm_protm_enter()
CPU1: drm_sched_run_job_work() -> panthor_vm_bind_run_job() ->
panthor_vm_exec_op() -> protm_block_timeout() ->
panthor_sched_protm_block_timeout()If vm->sched.work_run_job is already running when tick_ctx_apply() takes down_write(&sched->protm.lock), panthor_vm_bind_run_job() blocks on down_read(&sched->protm.lock) while drm_sched_stop() waits in cancel_work_sync(&vm->sched.work_run_job) for it to finish. Taking sched->protm.lock inside panthor_vm_bind_run_job() also sleeps inside a dma_fence_begin_signalling() critical section.
drivers/gpu/drm/panthor/panthor_sched.c tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *c · line 2782
Quoted 9 lines
drivers/gpu/drm/panthor/panthor_sched.c +9
-0+ + ret =panthor_fw_protm_enter (ptdev); +if (ret) { +panthor_device_schedule_reset (ptdev); + ctx->csg_upd_failed_mask =U32_MAX ; + } + +up_write (&sched->protm.lock); + }
[Severity: Medium]
If panthor_fw_protm_enter() fails here, doesn't this leave sched->protm.active_group set, its reference leaked, and the VM scheduler stopped even though the GPU never entered protected mode?
Unlike the other failure paths in tick_ctx_apply(), this branch also falls through instead of returning early, so ctx->groups[] and ctx->old_groups[] are drained while tick_work() sees ctx.csg_upd_failed_mask != 0 and jumps over the pm_runtime_get() block without acquiring sched->pm.has_ref for the newly bound CSG slots.
[ ... ]
Quoted 25 lines
drivers/gpu/drm/panthor/panthor_sched.c +21
-0@@ -2552,6 +2838,38 @@ tick_ctx_update_resched_target(struct panthor_scheduler *sched,return U64_MAX ; } +static bool preempt_protm_group (struct panthor_group *group,bool full_tick) +{ +struct panthor_device *ptdev = group->ptdev; +struct panthor_scheduler *sched = ptdev->scheduler; +bool groups_waiting; + +/* If the protm group can not run, then preempt! */ +if (!group_can_run (group)) +return true; + +/* If we have RT groups waiting and we are not RT, then preempt! */ + groups_waiting = + !list_empty (&sched->groups.runnable[PANTHOR_CSG_PRIORITY_RT ]); +if (groups_waiting && group->priority <PANTHOR_CSG_PRIORITY_RT ) +return true; + +/* The below conditions for preempting are only valid for full ticks */ +if (!full_tick) +return false; + + for (int pri = PANTHOR_CSG_PRIORITY_RT - 1;
drivers/gpu/drm/panthor/panthor_sched.c tick_ctx_update_resched_target(struct panthor_scheduler *sched, · line 2871
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +10
-0+ pri >= sched->protm.active_group->priority; pri--) { +if (!list_empty (&sched->groups.runnable[pri])) { + groups_waiting = true; +break ; + } + } + +return groups_waiting; + +}
[Severity: High]
Does preempt_protm_group() miss runnable normal-mode groups that are already bound to other CSG slots?
When a group enters protected mode, tick_work() still binds other runnable and idle groups to the remaining CSG slots, and tick_ctx_apply() removes those bound groups from sched->groups.runnable[]. While the GPU is in protected mode, the hardware blocks normal-mode queues on those other CSG slots from executing even though their per-queue drm_sched timeout timers are running. Because preempt_protm_group() only checks sched->groups.runnable[] and never checks sched->csg_slots[] for non-idle bound groups, it returns false on the next full tick when all runnable groups fit in the CSG slots, and tick_update_resched_target(sched, full_tick) stops the tick timer, starving the bound normal-mode groups until their jobs time out and trigger a GPU reset.
[ ... ]
drivers/gpu/drm/panthor/panthor_sched.c void panthor_sched_post_reset(struct panthor_device *ptdev, bool reset_failed) · line 3486
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +6
-0@@ -3094,6 +3478,13 @@ void panthor_sched_post_reset(struct panthor_device *ptdev, bool reset_failed)mutex_lock (&sched->reset.lock); +down_write (&sched->protm.lock); +if (sched->protm.active_group) { +group_put (sched->protm.active_group); + sched->protm.active_group =NULL ; + } +up_write (&sched->protm.lock);
[Severity: High]
What cleans up sched->protm.active_group and restarts its VM scheduler on reset, suspend, and device unplug?
Here in panthor_sched_post_reset(), clearing sched->protm.active_group does not call panthor_vm_protm_exit(sched->protm.active_group->vm), leaving the VM's drm_gpu_scheduler permanently stopped after a reset.
Neither panthor_sched_unplug() nor panthor_sched_suspend() exits protected mode or clears sched->protm.active_group either. If the device is unplugged while sched->protm.active_group is set, panthor_fw_unplug() -> panthor_kernel_bo_destroy() unmaps the vmap backing glb_iface and then calls panthor_vm_unmap_range(fw->vm, ...), which enters panthor_sched_protm_block_timeout() -> panthor_fw_protm_exit() and dereferences the already-unmapped glb_iface->input/output pointers, while also leaking the active_group reference. Likewise, if the device is suspended while sched->protm.active_group is set, active_group remains non-NULL while the GPU is powered off, so any VM operation on active_group->vm while suspended calls panthor_fw_protm_exit_wait() -> panthor_gpu_status() and reads MMIO with clocks disabled.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v5 6/13 drm/panthor: New function for FW section map flags
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
This refactors the VM map flags for FW sections into a new function called section_vm_map_flags().
This comes in handly for future handling of protected FW sections (type CSF_FW_BINARY_IFACE_ENTRY_PROT).
---
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_fw.c | 44 ++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +26 -18
@@ -543,6 +543,31 @@ panthor_fw_alloc_suspend_buf_mem(struct panthor_device *ptdev, size_t size) "FW suspend buffer"); } +static u32 section_vm_map_flags (const struct panthor_fw_section *section) +{ +u32 cache_mode = section->flags & +CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_MASK ; +u32 vm_map_flags =0 ; + +if (!(section->flags &CSF_FW_BINARY_IFACE_ENTRY_WR )) + vm_map_flags |=DRM_PANTHOR_VM_BIND_OP_MAP_READONLY ; + +if (!(section->flags &CSF_FW_BINARY_IFACE_ENTRY_EX )) + vm_map_flags |=DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC ; + +/* TODO: CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_*_COHERENT are mapped to +* non-cacheable for now. We might want to introduce a new +* IOMMU_xxx flag (or abuse IOMMU_MMIO, which maps to device +* memory and is currently not used by our driver) for +* AS_MEMATTR_AARCH64_SHARED memory, so we can take benefit +* of IO-coherent systems. +*/ +if (cache_mode !=CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_CACHED ) + vm_map_flags |=DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED ; + +return vm_map_flags; +} + static int panthor_fw_load_section_entry(struct panthor_device *ptdev, const struct firmware *fw, struct panthor_fw_binary_iter *iter,
@@ -645,26 +670,9 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev, }if (section_size) { -u32 cache_mode = hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_MASK ; -u32 vm_map_flags =0 ; +u32 vm_map_flags =section_vm_map_flags (section);u64 va = hdr.va.start; -if (!(hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_WR )) - vm_map_flags |=DRM_PANTHOR_VM_BIND_OP_MAP_READONLY ; - -if (!(hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_EX )) - vm_map_flags |=DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC ; - -/* TODO: CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_*_COHERENT are mapped to -* non-cacheable for now. We might want to introduce a new -* IOMMU_xxx flag (or abuse IOMMU_MMIO, which maps to device -* memory and is currently not used by our driver) for -* AS_MEMATTR_AARCH64_SHARED memory, so we can take benefit -* of IO-coherent systems. -*/ -if (cache_mode !=CSF_FW_BINARY_IFACE_ENTRY_CACHE_MODE_CACHED ) - vm_map_flags |=DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED ; - section->mem = panthor_kernel_bo_create(ptdev, panthor_fw_vm(ptdev), section_size, DRM_PANTHOR_BO_NO_MMAP,
Signature · 1 line
2.43.0
PATCH v5 7/13 drm/panthor: Keep size and VA in FW section obj
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
Keep a copy of the FW section size and VA in the panthor_fw_section object. We need this in the future when handling late init of protected memory for FW sections of type CSF_FW_BINARY_IFACE_ENTRY_PROT.
---
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_fw.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +15 -6
@@ -198,6 +198,12 @@ struct panthor_fw_section {/** @size: Size of @buf in bytes. */ size_t size; } data; + +/** @size: Section size. */ +size_t size; + +/** @va: Section VA. */ +u32 va; };#define CSF_MCU_SHARED_REGION_START 0x04000000ULL
@@ -669,18 +675,21 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev, section->name = name; } + section->size = section_size; + section->va = hdr.va.start; +if (section_size) {u32 vm_map_flags =section_vm_map_flags (section); -u64 va = hdr.va.start; - section->mem =panthor_kernel_bo_create (ptdev,panthor_fw_vm (ptdev), - section_size, -DRM_PANTHOR_BO_NO_MMAP , - vm_map_flags, va,"FW section" ); + section->mem =panthor_kernel_bo_create ( + ptdev,panthor_fw_vm (ptdev), section->size, +DRM_PANTHOR_BO_NO_MMAP , vm_map_flags, section->va, +"FW section" );if (IS_ERR (section->mem))return PTR_ERR (section->mem); -if (drm_WARN_ON (&ptdev->base, section->mem->va_node.start != hdr.va.start)) +if (drm_WARN_ON (&ptdev->base, + section->mem->va_node.start != section->va))return -EINVAL ;if (section->flags &CSF_FW_BINARY_IFACE_ENTRY_SHARED ) {
Signature · 1 line
2.43.0
PATCH v5 8/13 drm/panthor: Refactor GEM usage flags assignment
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
Changes panthor_gem_debugfs_set_usage_flags() to panthor_gem_debugfs_add_usage_flags(). This will come in handy in a later change when we split panthor_kernel_bo_create() to also handle protected memory.
---
v5:
- Minor formating fixes.
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_gem.c | 13 +++++++------ drivers/gpu/drm/panthor/panthor_gem.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +7 -6
@@ -74,14 +74,14 @@ static void panthor_gem_debugfs_bo_rm(struct panthor_gem_object *bo) mutex_unlock(&ptdev->gems.lock); } -static void panthor_gem_debugfs_set_usage_flags (struct panthor_gem_object *bo,u32 usage_flags) +static void panthor_gem_debugfs_add_usage_flags (struct panthor_gem_object *bo,u32 usage_flags) { - bo->debugfs.flags = usage_flags; -panthor_gem_debugfs_bo_add (bo); +atomic_or (usage_flags, &bo->debugfs.flags); }#else +static void panthor_gem_debugfs_bo_add (struct panthor_gem_object *bo) {}static void panthor_gem_debugfs_bo_rm (struct panthor_gem_object *bo) {} -static void panthor_gem_debugfs_set_usage_flags (struct panthor_gem_object *bo,u32 usage_flags) {} +static void panthor_gem_debugfs_add_usage_flags (struct panthor_gem_object *bo,u32 usage_flags) {}static void panthor_gem_debugfs_bo_init (struct panthor_gem_object *bo) {}#endif
@@ -1034,7 +1034,8 @@ panthor_gem_create(struct drm_device *dev, size_t size, uint32_t flags, bo->base.resv = bo->exclusive_vm_root_gem->resv; } -panthor_gem_debugfs_set_usage_flags (bo, usage_flags); +panthor_gem_debugfs_add_usage_flags (bo, usage_flags); +panthor_gem_debugfs_bo_add (bo);return bo; err_put:
@@ -1647,9 +1648,9 @@ static void panthor_gem_debugfs_bo_print(struct panthor_gem_object *bo,enum panthor_gem_reclaim_state reclaim_state = bo->reclaim_state;unsigned int refcount =kref_read (&bo->base.refcount);int reclaimed_count =atomic_read (&bo->reclaimed_count); +u32 gem_usage_flags =atomic_read (&bo->debugfs.flags);char creator_info[32 ] = {};size_t resident_size; -u32 gem_usage_flags = bo->debugfs.flags;u32 gem_state_flags =0 ;/* Skip BOs being destroyed. */
drivers/gpu/drm/panthor/panthor_gem.h +1 -1
@@ -62,7 +62,7 @@ struct panthor_gem_debugfs { } creator;/** @flags: Combination of panthor_debugfs_gem_usage_flags flags */ -u32 flags; +atomic_t flags; };/**
Signature · 1 line
2.43.0
PATCH v5 9/13 drm/panthor: Create kernel BO from existing GEM
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
Refactors/splits existing panthor_kernel_bo_create(). Adds panthor_kernel_bo_create_from_gem() so we can create kernel BOs from imported protected memory in future change.
---
v5:
- Fixed size type for panthor_kernel_bo_create_from_gem()
- Removed unused param from panthor_gem_create()
- Minor formating tweaks
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_gem.c | 100 +++++++++++++++++--------- drivers/gpu/drm/panthor/panthor_gem.h | 7 ++ 2 files changed, 73 insertions(+), 34 deletions(-)
drivers/gpu/drm/panthor/panthor_gem.c +66 -34
@@ -1001,7 +1001,7 @@ panthor_gem_alloc_object(u32 flags)static struct panthor_gem_object *panthor_gem_create (struct drm_device *dev,size_t size,uint32_t flags, -struct panthor_vm *exclusive_vm,u32 usage_flags) +struct panthor_vm *exclusive_vm) {struct panthor_gem_object *bo;int ret;
@@ -1034,7 +1034,6 @@ panthor_gem_create(struct drm_device *dev, size_t size, uint32_t flags, bo->base.resv = bo->exclusive_vm_root_gem->resv; } -panthor_gem_debugfs_add_usage_flags (bo, usage_flags);panthor_gem_debugfs_bo_add (bo);return bo;
@@ -1089,7 +1088,7 @@ panthor_gem_create_with_handle(struct drm_file *file,int ret;struct panthor_gem_object *bo; - bo =panthor_gem_create (ddev, *size, flags, exclusive_vm,0 ); + bo =panthor_gem_create (ddev, *size, flags, exclusive_vm);if (IS_ERR (bo))return PTR_ERR (bo);
@@ -1251,17 +1250,18 @@ panthor_gem_sync(struct drm_gem_object *obj, u32 type,void panthor_kernel_bo_destroy (struct panthor_kernel_bo *bo) {struct panthor_device *ptdev; +struct drm_gem_object *root_gem;struct panthor_vm *vm;if (IS_ERR_OR_NULL (bo))return ; ptdev = container_of(bo->obj->dev, struct panthor_device, base); + root_gem =to_panthor_bo (bo->obj)->exclusive_vm_root_gem; 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)); +drm_WARN_ON (bo->obj->dev, root_gem && (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);if (vm ==panthor_fw_vm (ptdev))
@@ -1272,46 +1272,34 @@ void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo) }/** -* panthor_kernel_bo_create() - Create and map a GEM object to a VM +* panthor_kernel_bo_create_from_gem() - Create kernel BO from existing GEM object * @ptdev: Device. * @vm: VM to map the GEM to. -* @size: Size of the buffer object. -* @bo_flags: Combination of drm_panthor_bo_flags flags. +* @bo: BO to use for our kernel BO. * @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 +* @vm_map_size: Size of the BO to map to the VM. * * 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) +panthor_kernel_bo_create_from_gem (struct panthor_device *ptdev, +struct panthor_vm *vm, +struct panthor_gem_object *bo, +u32 vm_map_flags,u64 gpu_va, +size_t vm_map_size) {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 ); -if (vm ==panthor_fw_vm (ptdev)) - debug_flags |=PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED ; - - bo =panthor_gem_create (&ptdev->base, size, bo_flags, vm, debug_flags); -if (IS_ERR (bo)) { - ret =PTR_ERR (bo); - goto err_free_kbo; - } - +drm_gem_object_get (&bo->base); kbo->obj = &bo->base;if (vm ==panthor_fw_vm (ptdev)) {
@@ -1320,24 +1308,26 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, goto err_put_obj; } -panthor_gem_kernel_bo_set_label (kbo, name); -/* 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); + vm_map_size =ALIGN (vm_map_size,panthor_vm_page_size (vm)); + ret =panthor_vm_alloc_va (vm, gpu_va, vm_map_size, &kbo->va_node);if (ret) goto err_unpin; - ret =panthor_vm_map_bo_range (vm, bo,0 , size, kbo->va_node.start, vm_map_flags); + ret =panthor_vm_map_bo_range (vm, bo,0 , vm_map_size, + kbo->va_node.start, vm_map_flags);if (ret) goto err_free_va; kbo->vm =panthor_vm_get (vm); +if (vm ==panthor_fw_vm (ptdev)) +panthor_gem_debugfs_add_usage_flags (bo,PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED ); +return kbo; err_free_va:
@@ -1349,12 +1339,54 @@ panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm, err_put_obj:drm_gem_object_put (&bo->base); - -err_free_kbo:kfree (kbo);return ERR_PTR (ret); } +/** +* panthor_kernel_bo_create() - Create and map a GEM object to a VM +* @ptdev: Device. +* @vm: VM to map the GEM to. +* @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 panthor_kernel_bo *kbo; +struct panthor_gem_object *bo; + +if (drm_WARN_ON (&ptdev->base, !vm)) +return ERR_PTR (-EINVAL ); + + bo =panthor_gem_create (&ptdev->base, size, bo_flags, vm); +if (IS_ERR (bo)) +return ERR_CAST (bo); + + kbo =panthor_kernel_bo_create_from_gem (ptdev, vm, bo, vm_map_flags, + gpu_va, size); +if (!IS_ERR (kbo)) { +panthor_gem_debugfs_add_usage_flags (bo,PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL ); +panthor_gem_kernel_bo_set_label (kbo, name); + } + +/* panthor_kernel_bo_create_from_gem() acquires a GEM ref if the import +* succeeds, so we can release it unconditionally here. +*/ +drm_gem_object_put (&bo->base); +return kbo; +} +/** * panthor_dummy_bo_create() - Create a Panthor BO meant to back sparse bindings. * @ptdev: Device.
@@ -1370,7 +1402,7 @@ panthor_dummy_bo_create(struct panthor_device *ptdev)* make sense to pre-populate this object's page array, nor to fall back on a BO size * of 4KiB. Sticking to a dummy object size of 2MiB lets us keep things simple for now. */ -return panthor_gem_create (&ptdev->base,SZ_2M ,DRM_PANTHOR_BO_NO_MMAP ,NULL ,0 ); +return panthor_gem_create (&ptdev->base,SZ_2M ,DRM_PANTHOR_BO_NO_MMAP ,NULL ); }static bool can_swap (void )
drivers/gpu/drm/panthor/panthor_gem.h +7 -0
@@ -318,6 +318,13 @@ panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo) } } +struct panthor_kernel_bo * +panthor_kernel_bo_create_from_gem (struct panthor_device *ptdev, +struct panthor_vm *vm, +struct panthor_gem_object *bo, +u32 vm_map_flags,u64 gpu_va, +size_t vm_map_size); +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,
Signature · 1 line
2.43.0
PATCH v5 10/13 drm/panthor: Use a copy of suspend buffer sizes
In reply to Ketil JohnsenStore the suspend buffer sizes (normal and protected) provided by FW after FW start. Unsafe to read directly from FW as these will return zero between FW load and FW start.
---
v5:
- First version of this patch in this patch set.
- This change was previously in a different patch in the set.
--- drivers/gpu/drm/panthor/panthor_fw.c | 25 +++++++++++++++++++++++++ drivers/gpu/drm/panthor/panthor_fw.h | 3 +++ drivers/gpu/drm/panthor/panthor_sched.c | 6 +++--- 3 files changed, 31 insertions(+), 3 deletions(-)
drivers/gpu/drm/panthor/panthor_fw.c +25 -0
@@ -255,6 +255,12 @@ struct panthor_fw {/** @iface: FW interfaces. */ struct panthor_fw_iface iface; +/** @suspend_size: Size of regular suspend buffer */ +u32 suspend_size; + +/** @protm_suspend_size: Size of protected suspend buffer size */ +u32 protm_suspend_size; +/** @watchdog: Collection of fields relating to the FW watchdog. */ struct {/** @ping_work: Delayed work used to ping the FW. */
@@ -549,6 +555,16 @@ panthor_fw_alloc_suspend_buf_mem(struct panthor_device *ptdev, size_t size) "FW suspend buffer"); } +u32 panthor_fw_suspend_size (struct panthor_device *ptdev) +{ +return ptdev->fw->suspend_size; +} + +u32 panthor_fw_protm_suspend_size (struct panthor_device *ptdev) +{ +return ptdev->fw->protm_suspend_size; +} +static u32 section_vm_map_flags (const struct panthor_fw_section *section) { u32 cache_mode = section->flags &
@@ -985,6 +1001,15 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev,return -EINVAL ; } +if (!csg_idx) { +/* We make a copy of these sizes, as csg_iface can read these +* as zero during full GPU reset, between FW sections being +* reloaded and FW fully booted. +*/ + ptdev->fw->suspend_size = csg_iface->control->suspend_size; + ptdev->fw->protm_suspend_size = csg_iface->control->protm_suspend_size; + } +if (csg_idx >0 ) {struct panthor_fw_csg_iface *first_csg_iface =panthor_fw_get_csg_iface (ptdev,0 );
drivers/gpu/drm/panthor/panthor_fw.h +3 -0
@@ -511,6 +511,9 @@ panthor_fw_alloc_queue_iface_mem(struct panthor_device *ptdev,struct panthor_kernel_bo *panthor_fw_alloc_suspend_buf_mem (struct panthor_device *ptdev,size_t size); +u32 panthor_fw_suspend_size (struct panthor_device *ptdev); +u32 panthor_fw_protm_suspend_size (struct panthor_device *ptdev); +struct panthor_vm *panthor_fw_vm (struct panthor_device *ptdev);void panthor_fw_pre_reset (struct panthor_device *ptdev,bool on_hang);
drivers/gpu/drm/panthor/panthor_sched.c +3 -3
@@ -4058,7 +4058,7 @@ int panthor_group_create(struct drm_file *file,struct panthor_scheduler *sched = ptdev->scheduler;struct panthor_fw_csg_iface *csg_iface =panthor_fw_get_csg_iface (ptdev,0 );struct panthor_group *group =NULL ; -u32 gid, i, suspend_size; +u32 gid, i;int ret;if (group_args->pad)
@@ -4109,8 +4109,8 @@ int panthor_group_create(struct drm_file *file, goto err_put_group; } - suspend_size = csg_iface->control->suspend_size; - group->suspend_buf =panthor_fw_alloc_suspend_buf_mem (ptdev, suspend_size); + group->suspend_buf = +panthor_fw_alloc_suspend_buf_mem (ptdev,panthor_fw_suspend_size (ptdev));if (IS_ERR (group->suspend_buf)) { ret =PTR_ERR (group->suspend_buf); group->suspend_buf =NULL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 10/13] drm/panthor: Use a copy of suspend buffer sizes
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/panthor: unused variable csg_iface in panthor_group_create() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/panthor: unused variable csg_iface in panthor_group_create() --
commit 71ef359cc5f25037d19672b0d70e07aa5a47adee
Author: Ketil Johnsen <[email protected]>
drm/panthor: Use a copy of suspend buffer sizes
This commit caches the regular and protected suspend buffer sizes from the firmware CSG control interface during initialization and uses the cached size when allocating the suspend buffer in panthor_group_create().
Quoted 14 lines
drivers/gpu/drm/panthor/panthor_sched.c +1
-1@@ -4058,7 +4058,7 @@ int panthor_group_create(struct drm_file *file,struct panthor_scheduler *sched = ptdev->scheduler;struct panthor_fw_csg_iface *csg_iface =panthor_fw_get_csg_iface (ptdev,0 );struct panthor_group *group =NULL ; -u32 gid, i, suspend_size; +u32 gid, i;int ret;if (group_args->pad)
drivers/gpu/drm/panthor/panthor_sched.c int panthor_group_create(struct drm_file *file, · line 4116
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +2
-2@@ -4109,8 +4109,8 @@ int panthor_group_create(struct drm_file *file, goto err_put_group; } - suspend_size = csg_iface->control->suspend_size; - group->suspend_buf =panthor_fw_alloc_suspend_buf_mem (ptdev, suspend_size); + group->suspend_buf = +panthor_fw_alloc_suspend_buf_mem (ptdev,panthor_fw_suspend_size (ptdev));if (IS_ERR (group->suspend_buf)) { ret =PTR_ERR (group->suspend_buf); group->suspend_buf =NULL ;
[Severity: Low]
Does replacing csg_iface->control->suspend_size with panthor_fw_suspend_size(ptdev) leave csg_iface unused in panthor_group_create()? That triggers a -Wunused-variable warning (and a build failure with CONFIG_WERROR=y) at this point in the series, as the csg_iface declaration is only removed in the later commit "drm/panthor:
Protm support for groups".
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH v5 11/13 drm/panthor: Support FW init of protm
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
The Mali GPU FW needs some protected memory when executing in protected mode. This FW memory section is assigned a VA during device init. A user space process with the needed privileges (CAP_SYS_MODULE) must provide a suitable memory buffer before the Mali GPU is capable of executing in protected mode.
Adds user query of FW protm state, min FW section size and min protm suspend buffer size.
Adds user initiated init of FW protm section.
---
v5:
- Ditched the lockless approach, now using a simpler mutex.
- Handle potential races reported by Sashiko.
- Do not write to protected FW section memory (during GPU reset).
- No longer mix guard() and goto error handling in panthor_fw_protm_init()
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_drv.c | 21 +++- drivers/gpu/drm/panthor/panthor_fw.c | 146 ++++++++++++++++++++++++-- drivers/gpu/drm/panthor/panthor_fw.h | 5 + include/uapi/drm/panthor_drm.h | 75 +++++++++++++ 4 files changed, 238 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +20 -1
@@ -177,11 +177,13 @@ panthor_get_uobj_array(const struct drm_panthor_obj_array *in, u32 min_stride, PANTHOR_UOBJ_DECL(struct drm_panthor_mmu_info, page_size_bitmap), \PANTHOR_UOBJ_DECL (struct drm_panthor_timestamp_info , current_timestamp), \PANTHOR_UOBJ_DECL (struct drm_panthor_group_priorities_info , pad), \ +PANTHOR_UOBJ_DECL (struct drm_panthor_protm_info , pad), \PANTHOR_UOBJ_DECL (struct drm_panthor_sync_op , timeline_value), \PANTHOR_UOBJ_DECL (struct drm_panthor_queue_submit , syncs), \PANTHOR_UOBJ_DECL (struct drm_panthor_queue_create , ringbuf_size), \PANTHOR_UOBJ_DECL (struct drm_panthor_vm_bind_op , syncs), \ - PANTHOR_UOBJ_DECL(struct drm_panthor_bo_sync_op, size)) +PANTHOR_UOBJ_DECL (struct drm_panthor_bo_sync_op , size), \ + PANTHOR_UOBJ_DECL(struct drm_panthor_protm_init, pad))/** * PANTHOR_UOBJ_SET() - Copy a kernel object to a user object.
@@ -934,6 +936,8 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct dstruct drm_panthor_dev_query *args = data;struct drm_panthor_timestamp_info timestamp_info;struct drm_panthor_group_priorities_info priorities_info; +struct drm_panthor_protm_info protm_info; +int ret;if (!args->pointer) {
@@ -958,6 +962,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d args->size =sizeof (ptdev->mmu_info);return 0 ; +case DRM_PANTHOR_DEV_QUERY_PROTM_INFO : + args->size =sizeof (protm_info); +return 0 ; +default :return -EINVAL ; }
@@ -991,6 +999,10 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct dcase DRM_PANTHOR_DEV_QUERY_MMU_INFO :return PANTHOR_UOBJ_SET (args->pointer, args->size, ptdev->mmu_info); +case DRM_PANTHOR_DEV_QUERY_PROTM_INFO : +panthor_fw_query_protm_info (file, &protm_info); +return PANTHOR_UOBJ_SET (args->pointer, args->size, protm_info); +default :return -EINVAL ; }
@@ -1584,6 +1596,12 @@ static int panthor_ioctl_bo_query_info(struct drm_device *ddev, void *data,return 0 ; } +static int panthor_ioctl_protm_init (struct drm_device *ddev,void *data, +struct drm_file *file) +{ +return panthor_fw_protm_init (file, data); +} +static int panthor_open (struct drm_device *ddev,struct drm_file *file) {
@@ -1660,6 +1678,7 @@ static const struct drm_ioctl_desc panthor_drm_driver_ioctls[] = {PANTHOR_IOCTL (SET_USER_MMIO_OFFSET , set_user_mmio_offset,DRM_RENDER_ALLOW ),PANTHOR_IOCTL (BO_SYNC , bo_sync,DRM_RENDER_ALLOW ),PANTHOR_IOCTL (BO_QUERY_INFO , bo_query_info,DRM_RENDER_ALLOW ), + PANTHOR_IOCTL(PROTM_INIT, protm_init, DRM_RENDER_ALLOW), };static int panthor_mmap (struct file *filp,struct vm_area_struct *vma)
drivers/gpu/drm/panthor/panthor_fw.c +138 -8
196 lines not on this page · raw message
drivers/gpu/drm/panthor/panthor_fw.h +5 -0
@@ -532,6 +532,11 @@ static inline int panthor_fw_resume(struct panthor_device *ptdev)int panthor_fw_init (struct panthor_device *ptdev);void panthor_fw_unplug (struct panthor_device *ptdev); +int panthor_fw_protm_init (struct drm_file *file, +struct drm_panthor_protm_init *args); +bool panthor_fw_protm_is_inited (struct panthor_device *ptdev); +void panthor_fw_query_protm_info (struct drm_file *file, +struct drm_panthor_protm_info *arg);int panthor_fw_protm_enter (struct panthor_device *ptdev);int panthor_fw_protm_exit (struct panthor_device *ptdev,u32 timeout_ms);int panthor_fw_protm_exit_wait (struct panthor_device *ptdev,u32 timeout_ms);
include/uapi/drm/panthor_drm.h +75 -0
105 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 11/13] drm/panthor: Support FW init of protm
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/panthor: NULL pointer dereference in panthor_fw_query_protm_info() - [Medium] drm/panthor: UAF…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] drm/panthor: NULL pointer dereference in panthor_fw_query_protm_info()
- [Medium] drm/panthor: UAF or NULL dereference of fw->vm outside drm_dev_enter()
- [Low] drm/panthor: missing kernel-doc for protm member in struct panthor_fw
- [Low] drm/panthor: NULL shared_section->mem in panthor_fw_load_section_entry()
--
commit 5ac4027cd6058f1c3359662cdf5433f1c22a832b
Author: Boris Brezillon <[email protected]>
drm/panthor: Support FW init of protm
This commit adds UAPI and firmware support to query protected mode information and initialize the protected firmware section from a userspace-supplied GEM buffer object.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_fw.c +0
-0
[ ... ]
Quoted 5 lines
drivers/gpu/drm/panthor/panthor_fw.c +1
-0@@ -267,6 +269,18 @@ struct panthor_fw {struct delayed_work ping_work; } watchdog; +struct {
drivers/gpu/drm/panthor/panthor_fw.c struct panthor_fw { · line 282
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_fw.c +10
-0+/** @protm.init_lock: Used to serialize protm initialization. */ +struct mutex init_lock; + +/** @protm.inited: set to true when protm.section is initialized */ +bool inited; + +/** @protm.section: The protected mode section. */ +struct panthor_fw_section *section; + + } protm;
[Severity: Low]
This isn't a bug, but struct panthor_fw has a kernel-doc comment block and protm is missing its top-level @protm description, which triggers a kernel-doc warning with W=1 builds.
[ ... ]
Quoted 27 lines
drivers/gpu/drm/panthor/panthor_fw.c +9
-7@@ -637,12 +659,6 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev,return -EINVAL ; } -if (hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_PROT ) { -drm_warn (&ptdev->base, -"Firmware protected mode entry is not supported, ignoring" ); -return 0 ; - } -if (hdr.va.start ==CSF_MCU_SHARED_REGION_START && !(hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_SHARED )) { drm_err(&ptdev->base,@@ -694,7 +710,22 @@ static int panthor_fw_load_section_entry(struct panthor_device *ptdev, section->size = section_size; section->va = hdr.va.start; -if (section_size) { +if (hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_PROT ) { +guard (mutex)(&ptdev->fw->protm.init_lock); + +if (ptdev->fw->protm.section) { +drm_err (&ptdev->base, +"Only one protected section supported\n" ); +return -EINVAL ; + } +
drivers/gpu/drm/panthor/panthor_fw.c static int panthor_fw_load_section_entry(struct panthor_device *ptdev, · line 731
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_fw.c +7
-0+ ptdev->fw->protm.section = section; + } + +/* Defer the section->mem creation if this is a protected entry. +* This will be populated when DRM_IOCTL_PANTHOR_PROTM_INIT is called. +*/ +if (section->size && !(hdr.flags &CSF_FW_BINARY_IFACE_ENTRY_PROT )) {u32 vm_map_flags =section_vm_map_flags (section); section->mem = panthor_kernel_bo_create(
[Severity: Low]
Can a malformed firmware binary cause a NULL pointer dereference of ptdev->fw->shared_section->mem in panthor_fw_init_ifaces()?
If an entry at CSF_MCU_SHARED_REGION_START has both CSF_FW_BINARY_IFACE_ENTRY_SHARED and CSF_FW_BINARY_IFACE_ENTRY_PROT set, panthor_fw_load_section_entry() skips allocating section->mem and still assigns ptdev->fw->shared_section = section, which panthor_fw_init_ifaces() then dereferences via panthor_kernel_bo_size(). Rejecting CSF_FW_BINARY_IFACE_ENTRY_PROT when CSF_FW_BINARY_IFACE_ENTRY_SHARED is set would prevent this.
[ ... ]
Quoted 28 lines
drivers/gpu/drm/panthor/panthor_fw.c +24
-0@@ -1610,6 +1641,101 @@ int panthor_fw_protm_exit(struct panthor_device *ptdev, u32 timeout_ms)return panthor_fw_protm_exit_wait (ptdev, timeout_ms); } +int panthor_fw_protm_init (struct drm_file *file, +struct drm_panthor_protm_init *args) +{ +struct panthor_file *pfile = file->driver_priv; +struct panthor_device *ptdev = pfile->ptdev; +ssize_t vm_pgsz =panthor_vm_page_size (ptdev->fw->vm); +struct panthor_fw_section *protm_section; +struct panthor_kernel_bo *mem; +struct drm_gem_object *obj; +u32 vm_map_flags; +int cookie, ret =0 ; + +if (!capable (CAP_SYS_MODULE )) +return -EPERM ; + +if (args->pad) +return -EINVAL ; + +mutex_lock (&ptdev->fw->protm.init_lock); + + protm_section = ptdev->fw->protm.section; + +if (!protm_section || !protm_section->size) { + ret = -EINVAL ;
drivers/gpu/drm/panthor/panthor_fw.c int panthor_fw_protm_exit(struct panthor_device *ptdev, u32 timeout_ms) · line 1677
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_fw.c +10
-0+ goto out_unlock; + } + +if (ptdev->fw->protm.inited) + goto out_unlock; + +if (!drm_dev_enter (&ptdev->base, &cookie)) { + ret = -ENODEV ; + goto out_unlock; + }
[Severity: Medium]
Can calling panthor_vm_page_size(ptdev->fw->vm) before drm_dev_enter() here (and without drm_dev_enter() in panthor_fw_query_protm_info() below) cause a use-after-free or NULL pointer dereference if the device is unplugged concurrently?
CPU0 CPU1drm_ioctl_kernel()
panthor_ioctl_protm_init()
panthor_fw_protm_init()
panthor_device_unplug()
drm_dev_unplug(&ptdev->base)
panthor_fw_unplug(ptdev)
panthor_vm_put(ptdev->fw->vm)
ptdev->fw->vm = NULL
panthor_vm_page_size(ptdev->fw->vm)
<-- NULL / UAF dereference
...
drm_dev_enter(&ptdev->base, &cookie)Evaluating panthor_vm_page_size(ptdev->fw->vm) only inside a drm_dev_enter() section in both functions would avoid racing with panthor_fw_unplug().
Quoted 50 lines
drivers/gpu/drm/panthor/panthor_fw.c +50
-0+ + obj =drm_gem_object_lookup (file, + args->fw_protected_sections_bo_handle); +if (!obj) { + ret = -ENOENT ; + goto out_dev_exit; + } + +if (obj->size <ALIGN (ptdev->fw->protm.section->size, vm_pgsz)) { + ret = -EINVAL ; + goto out_gem_put; + } + + vm_map_flags =section_vm_map_flags (protm_section); + + mem =panthor_kernel_bo_create_from_gem ( + ptdev,panthor_fw_vm (ptdev),to_panthor_bo (obj), vm_map_flags, + protm_section->va, protm_section->size); +if (IS_ERR (mem)) { + ret =PTR_ERR (mem); + goto out_gem_put; + } + + protm_section->mem = mem; + + ptdev->fw->protm.inited = true; + +out_gem_put: +drm_gem_object_put (obj); + +out_dev_exit: +drm_dev_exit (cookie); + +out_unlock: +mutex_unlock (&ptdev->fw->protm.init_lock); + +return ret; +} + +bool panthor_fw_protm_is_inited (struct panthor_device *ptdev) +{ +guard (mutex)(&ptdev->fw->protm.init_lock); +return ptdev->fw->protm.inited; +} + +void panthor_fw_query_protm_info (struct drm_file *file, +struct drm_panthor_protm_info *arg) +{ +struct panthor_file *pfile = file->driver_priv; +struct panthor_device *ptdev = pfile->ptdev;
drivers/gpu/drm/panthor/panthor_fw.c int panthor_fw_protm_exit(struct panthor_device *ptdev, u32 timeout_ms) · line 1737
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_fw.c +10
-0+ssize_t vm_pgsz =panthor_vm_page_size (ptdev->fw->vm); + +guard (mutex)(&ptdev->fw->protm.init_lock); + +memset (arg,0 ,sizeof (*arg)); + arg->state = ptdev->fw->protm.inited ?DRM_PANTHOR_PROTM_INITIALIZED :0 ; + arg->group_protected_suspend_buf_size = ptdev->fw->protm_suspend_size; + arg->fw_protected_sections_size =ALIGN (ptdev->fw->protm.section->size, + vm_pgsz); +}
[Severity: High]
Does panthor_fw_query_protm_info() dereference a NULL ptdev->fw->protm.section when the loaded firmware has no protected section?
Unlike panthor_fw_protm_init(), which checks whether protm_section is NULL, querying DRM_PANTHOR_DEV_QUERY_PROTM_INFO unconditionally reads ptdev->fw->protm.section->size, allowing any user with render node access to trigger a kernel NULL pointer dereference if the firmware binary lacks a non-empty CSF_FW_BINARY_IFACE_ENTRY_PROT entry.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
kernel test robot
build botRe: [PATCH v5 11/13] drm/panthor: Support FW init of protm
In reply to Ketil Johnsenkernel test robot noticed the following build warnings:
Hi Ketil,
kernel test robot noticed the following build warnings:
[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on next-20261113]
[cannot apply to linux-review/Ketil-Johnsen/drm-panthor-De-duplicate-FW-memory-section-sync/20261012-145929 linus/master v7.4-rc3] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Ketil-Johnsen/drm-panthor-De-duplicate-FW-memory-section-sync/20261116-122549 base: https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next patch link: https://lore.kernel.org/r/20261116112601.3483649-12-ketil.johnsen%40arm.com patch subject: [PATCH v5 11/13] drm/panthor: Support FW init of protm config: sparc64-randconfig-1000-20261116 (https://download.01.org/0day-ci/archive/20261117/[email protected]/config) compiler: sparc64-linux-gcc (GCC) 15.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261117/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
Warning: drivers/gpu/drm/panthor/panthor_fw.c:145 Excess struct member 'start' description in 'panthor_fw_binary_section_entry_hdr' (did you mean one of: 'va.start', 'data.start')
Warning: drivers/gpu/drm/panthor/panthor_fw.c:145 Excess struct member 'end' description in 'panthor_fw_binary_section_entry_hdr' (did you mean one of: 'va.end', 'data.end')
Warning: drivers/gpu/drm/panthor/panthor_fw.c:208 Excess struct member 'buf' description in 'panthor_fw_section' (did you mean one of: 'data.buf')
>> Warning: drivers/gpu/drm/panthor/panthor_fw.c:299 struct member 'protm' not described in 'panthor_fw' Warning: drivers/gpu/drm/panthor/panthor_fw.c:299 Excess struct member 'ping_work' description in 'panthor_fw' (did you mean one of: 'watchdog.ping_work') Warning: drivers/gpu/drm/panthor/panthor_fw.c:145 Excess struct member 'start' description in 'panthor_fw_binary_section_entry_hdr' (did you mean one of: 'va.start', 'data.start') Warning: drivers/gpu/drm/panthor/panthor_fw.c:145 Excess struct member 'end' description in 'panthor_fw_binary_section_entry_hdr' (did you mean one of: 'va.end', 'data.end') Warning: drivers/gpu/drm/panthor/panthor_fw.c:208 Excess struct member 'buf' description in 'panthor_fw_section' (did you mean one of: 'data.buf') >> Warning: drivers/gpu/drm/panthor/panthor_fw.c:299 struct member 'protm' not described in 'panthor_fw'
Warning: drivers/gpu/drm/panthor/panthor_fw.c:299 Excess struct member 'ping_work' description in 'panthor_fw' (did you mean one of: 'watchdog.ping_work')
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v5 12/13 drm/panthor: Protm support for groups
In reply to Ketil JohnsenFrom: Boris Brezillon <[email protected]>
Extends struct drm_panthor_group_create to allow user provided protm suspend buffer. This is needed for protm execution with this group.
Make use of the user supplied suspend buffer (import).
Allow PROT_REGION instruction for groups which provided protm suspend buffer.
---
v5:
- Removed unused #define
- Minor formating changes
- Moved caching of suspend buf sizes to a separate patch
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_sched.c | 50 +++++++++++++++++++++---- include/uapi/drm/panthor_drm.h | 10 ++++- 2 files changed, 51 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_sched.c +43 -7
@@ -1456,10 +1456,15 @@ cs_slot_process_protm_pending_event_locked(struct panthor_device *ptdev,if (!group)return ; -/* Do not allow user space work to switch into protected mode, as we -* do not fully support this quite yet. +/* Do not allow user space work to switch into protected mode if we +* do not support protected mode on this device. +* User space should query (and init) this support before attempting +* to use such GPU instructions. */ -atomic_or (BIT (cs_id), &group->fatal_queues); +if (group->protm_suspend_buf) +atomic_or (BIT (cs_id), &group->protm_pending_queues); +else +atomic_or (BIT (cs_id), &group->fatal_queues);sched_queue_delayed_work (sched, tick,0 ); }
@@ -4056,14 +4061,10 @@ int panthor_group_create(struct drm_file *file,struct panthor_device *ptdev = pfile->ptdev;struct panthor_group_pool *gpool = pfile->groups;struct panthor_scheduler *sched = ptdev->scheduler; -struct panthor_fw_csg_iface *csg_iface =panthor_fw_get_csg_iface (ptdev,0 );struct panthor_group *group =NULL ;u32 gid, i;int ret; -if (group_args->pad) -return -EINVAL ; -if (group_args->priority >=PANTHOR_CSG_PRIORITY_COUNT )return -EINVAL ;
@@ -4117,6 +4118,41 @@ int panthor_group_create(struct drm_file *file, goto err_put_group; } +if (group_args->protected_suspend_bo_handle) { +struct drm_gem_object *obj; + +/* Read a correct snapshot of the state */ +if (!panthor_fw_protm_is_inited (ptdev)) { + ret = -EINVAL ; + goto err_put_group; + } + + obj =drm_gem_object_lookup (file, group_args->protected_suspend_bo_handle); +if (!obj) { + ret = -ENOENT ; + goto err_put_group; + } + +if (obj->size <panthor_fw_protm_suspend_size (ptdev)) { +drm_gem_object_put (obj); + ret = -EINVAL ; + goto err_put_group; + } + + group->protm_suspend_buf = +panthor_kernel_bo_create_from_gem (ptdev,panthor_fw_vm (ptdev), +to_panthor_bo (obj), +DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC , +PANTHOR_VM_KERNEL_AUTO_VA , +panthor_fw_protm_suspend_size (ptdev)); +drm_gem_object_put (obj); +if (IS_ERR (group->protm_suspend_buf)) { + ret =PTR_ERR (group->protm_suspend_buf); + group->protm_suspend_buf =NULL ; + goto err_put_group; + } + } + group->syncobjs = panthor_kernel_bo_create(ptdev, group->vm, group_args->queues.count * sizeof(struct panthor_syncobj_64b),
include/uapi/drm/panthor_drm.h +8 -2
@@ -956,8 +956,14 @@ struct drm_panthor_group_create {/** @priority: Group priority (see enum drm_panthor_group_priority). */ __u8 priority; -/** @pad: Padding field, MBZ. */ -__u32 pad; +/** +* @protected_suspend_bo_handle: BO to use as a protected suspend buffer. +* +* This BO must have been allocated from a protected DMA-BUF heap and +* imported in panthor. It's size must be at least +* drm_panthor_protm_info::group_protected_suspend_buf_size. +*/ +__u32 protected_suspend_bo_handle;/** * @compute_core_mask: Mask encoding cores that can be used for compute jobs.
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 12/13] drm/panthor: Protm support for groups
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/panthor: missing import and exclusivity checks in panthor_group_create() - [High] drm/panthor…
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] drm/panthor: missing import and exclusivity checks in panthor_group_create()
- [High] drm/panthor: AB-BA deadlock between sched->protm.lock and vm->sched.work_run_job
- [High] drm/panthor: missing sched->lock in handle_protm_fault() during reset
- [High] drm/panthor: stale protm_pending_queues across group_unbind_locked()
- [Medium] drm/panthor: NULL deref and pin leak on unplug in panthor_group_create()
- [Low] drm/panthor: user GEM BO accounted as private kernel BO in add_group_kbo_sizes()
--
commit f5cdea11b7d0262429a726c34516cf02769fb1a4
Author: Boris Brezillon <[email protected]>
drm/panthor: Protm support for groups
This commit extends struct drm_panthor_group_create with a user-supplied protected suspend buffer handle, maps that buffer into the MCU firmware VM during group creation, and allows queues on groups with a protected suspend buffer to raise CS_PROTM_PENDING events.
Quoted 13 lines
drivers/gpu/drm/panthor/panthor_sched.c +3
-2@@ -1456,10 +1456,15 @@ cs_slot_process_protm_pending_event_locked(struct panthor_device *ptdev,if (!group)return ; -/* Do not allow user space work to switch into protected mode, as we -* do not fully support this quite yet. +/* Do not allow user space work to switch into protected mode if we +* do not support protected mode on this device. +* User space should query (and init) this support before attempting
drivers/gpu/drm/panthor/panthor_sched.c cs_slot_process_protm_pending_event_locked(struct panthor_device *ptdev, · line 1470
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +5
-1+* to use such GPU instructions. */ -atomic_or (BIT (cs_id), &group->fatal_queues); +if (group->protm_suspend_buf) +atomic_or (BIT (cs_id), &group->protm_pending_queues); +else +atomic_or (BIT (cs_id), &group->fatal_queues);sched_queue_delayed_work (sched, tick,0 ); }
[Severity: High]
Can setting group->protm_pending_queues here trigger an ABBA deadlock between sched->protm.lock and vm->sched.work_run_job when tick_ctx_apply() enters protected mode concurrently with an asynchronous VM_BIND job on the same VM?
CPU0 CPU1
---- ----down_write(&sched->protm.lock)
drm_sched_run_job_work()
...
down_read(&sched->protm.lock)cancel_work_sync(&vm->sched.work_run_job)
*** DEADLOCK ***
CPU0: tick_work() -> tick_ctx_apply() -> panthor_vm_protm_enter() ->
panthor_vm_stop() -> drm_sched_stop()
CPU1: drm_sched_run_job_work() -> panthor_vm_bind_run_job() ->
panthor_vm_exec_op() -> protm_block_timeout() ->
panthor_sched_protm_block_timeout()Once group->protm_pending_queues is set, tick_ctx_apply() sees a non-NULL ctx->protm_group, takes sched->protm.lock in write mode, and calls panthor_vm_protm_enter(), which stops the VM scheduler and waits for any in-flight work_run_job to complete. If work_run_job is already running on ptdev->mmu->vm.wq and reaches panthor_sched_protm_block_timeout(), both workqueues hang permanently.
[Severity: High]
Once groups can enter protected mode and populate sched->protm.active_group, does handle_protm_fault() access CSG slots and scheduler group lists without holding sched->lock?
drivers/gpu/drm/panthor/panthor_sched.c:handle_protm_fault() {
...
for (csg_id = 0; csg_id < sched->csg_slot_count; csg_id++) {
struct panthor_group *group = sched->csg_slots[csg_id].group;
...
group->state = PANTHOR_CS_GROUP_SUSPENDED;
group_unbind_locked(group);
drm_WARN_ON(&group->ptdev->base, !list_empty(&group->run_node));
if (group_can_run(group)) {
list_add(&group->run_node,
&sched->groups.idle[group->priority]);
} else {
list_del_init(&group->wait_node);
...
}CPU0 (reset work) CPU1 (queue work / timeout)panthor_sched_pre_reset()
mutex_lock(&sched->reset.lock)
handle_protm_fault()
queue_run_job()
mutex_lock(&sched->lock)
if (group->csg_id >= 0)
group_unbind_locked(group)
/* lockdep_assert_held(&sched->lock) */
group->csg_id = -1;
queue->doorbell_id = -1;
panthor_fw_ring_doorbell(ptdev, -1)
<-- out-of-bounds MMIO writepanthor_sched_pre_reset() calls handle_protm_fault() while holding only sched->reset.lock and before panthor_group_stop() stops the per-queue scheduler and timeout works. When a protected-mode fault leaves the GPU unable to exit protected mode, handle_protm_fault() triggers lockdep_assert_held(&ptdev->scheduler->lock) inside group_unbind_locked() and races with concurrent sched->lock holders such as queue_run_job() and queue_timedout_job(), causing an invalid doorbell write or corrupting sched->groups.idle and group->wait_node.
[Severity: High]
Does group_unbind_locked() leave stale bits in group->protm_pending_queues when a group is suspended and unbound before it gets to enter protected mode?
drivers/gpu/drm/panthor/panthor_sched.c:group_unbind_locked() {
...
/* Tiler OOM events will be re-issued next time the group is scheduled. */
atomic_set(&group->tiler_oom, 0);
if (cancel_work(&group->tiler_oom_work))
group_put(group);
}If a queue raises CS_PROTM_PENDING while another group is picked first on that tick, and this group is later suspended and unbound (for example due to slot rotation or preemption), cs_slot_reset_locked() resets the slot's CS_PROTM_PENDING request bit, but group_unbind_locked() only clears group->tiler_oom and leaves group->protm_pending_queues non-zero.
When the unbound group is later picked first from sched->groups.runnable[prio], tick_ctx_pick_groups_from_list() selects it as ctx->protm_group based on that stale mask. In tick_ctx_apply(), tick_ctx_handle_protm_group() then runs before csgs_upd_ctx_apply_locked() sends CSG_STATE_RESUME, so (cs_iface->output->ack ^ cs_iface->input->req) & CS_PROTM_PENDING is zero and goes unacknowledged while group->protm_pending_queues is cleared. csgs_upd_ctx_apply_locked() then resumes the group in normal mode (re-raising CS_PROTM_PENDING) and tick_ctx_apply() immediately calls panthor_fw_protm_enter(), which times out and triggers a GPU reset.
Clearing group->protm_pending_queues in group_unbind_locked() alongside group->tiler_oom would let the firmware re-issue CS_PROTM_PENDING once the group is resumed.
Quoted 1 line
drivers/gpu/drm/panthor/panthor_sched.c +0
-0@@ -4056,14 +4061,10 @@ int panthor_group_create(struct drm_file *file,
[ ... ]
Quoted 28 lines
drivers/gpu/drm/panthor/panthor_sched.c +24
-0@@ -4117,6 +4118,41 @@ int panthor_group_create(struct drm_file *file, goto err_put_group; } +if (group_args->protected_suspend_bo_handle) { +struct drm_gem_object *obj; + +/* Read a correct snapshot of the state */ +if (!panthor_fw_protm_is_inited (ptdev)) { + ret = -EINVAL ; + goto err_put_group; + } + + obj =drm_gem_object_lookup (file, group_args->protected_suspend_bo_handle); +if (!obj) { + ret = -ENOENT ; + goto err_put_group; + } + +if (obj->size <panthor_fw_protm_suspend_size (ptdev)) { +drm_gem_object_put (obj); + ret = -EINVAL ; + goto err_put_group; + } + + group->protm_suspend_buf = + panthor_kernel_bo_create_from_gem(ptdev, panthor_fw_vm(ptdev), + to_panthor_bo(obj),
drivers/gpu/drm/panthor/panthor_sched.c int panthor_group_create(struct drm_file *file, · line 4154
Quoted 10 lines
drivers/gpu/drm/panthor/panthor_sched.c +10
-0+DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC , +PANTHOR_VM_KERNEL_AUTO_VA , + panthor_fw_protm_suspend_size(ptdev)); +drm_gem_object_put (obj); +if (IS_ERR (group->protm_suspend_buf)) { + ret =PTR_ERR (group->protm_suspend_buf); + group->protm_suspend_buf =NULL ; + goto err_put_group; + } + }
[Severity: High]
Does panthor_group_create() verify that protected_suspend_bo_handle is an imported DMA-BUF and prevent it from being mapped elsewhere or shared across groups?
The UAPI comment for struct drm_panthor_group_create states that protected_suspend_bo_handle must be allocated from a protected DMA-BUF heap and imported into Panthor, but neither panthor_group_create() nor panthor_kernel_bo_create_from_gem() checks drm_gem_is_imported(obj). A user can pass a normal shmem BO created via DRM_IOCTL_PANTHOR_BO_CREATE (with flags == 0 and exclusive_vm_id == 0), mapping a CPU-mmappable buffer into panthor_fw_vm(ptdev) as the MCU's protected suspend buffer, or pass the same handle to multiple groups and user GPU VMs so multiple CSG slots alias the same suspend buffer in the firmware address space.
[Severity: Medium]
Can panthor_kernel_bo_create_from_gem() dereference a NULL vm pointer or leak a pin reference if panthor_fw_unplug() runs concurrently with or before group teardown?
CPU0 CPU1panthor_group_create()
panthor_fw_alloc_suspend_buf_mem()
panthor_device_unplug()
...
panthor_fw_unplug()
ptdev->fw->vm = NULL;
panthor_kernel_bo_create_from_gem(
ptdev, panthor_fw_vm(ptdev), ...)
/* vm is NULL */
if (vm == panthor_fw_vm(ptdev))
panthor_gem_pin(bo);
panthor_vm_page_size(vm)
<-- NULL pointer dereferencepanthor_group_create() does not enter a drm_dev_enter() critical section before reading panthor_fw_vm(ptdev), and unlike panthor_kernel_bo_create(), panthor_kernel_bo_create_from_gem() (added in commit "drm/panthor: Create kernel BO from existing GEM") does not check for !vm. If panthor_fw_unplug() clears ptdev->fw->vm first, vm == panthor_fw_vm(ptdev) evaluates NULL == NULL as true, pins bo, and crashes in panthor_vm_page_size(NULL).
In addition, panthor_kernel_bo_destroy() checks vm == panthor_fw_vm(ptdev) instead of panthor_vm_for_mcu(vm), so if a group with protm_suspend_buf is destroyed after panthor_fw_unplug() sets ptdev->fw->vm to NULL, panthor_gem_unpin() is skipped and the BO remains pinned.
[Severity: Low]
Should add_group_kbo_sizes() still add group->protm_suspend_buf->obj->size to group->fdinfo.kbo_sizes now that protm_suspend_buf is backed by a user-supplied GEM handle?
drivers/gpu/drm/panthor/panthor_sched.c:add_group_kbo_sizes() {
...
if (group->protm_suspend_buf)
group->fdinfo.kbo_sizes += group->protm_suspend_buf->obj->size;
}group->fdinfo.kbo_sizes tracks internal driver-owned private kernel BOs that are not exposed through a DRM handle. Because protected_suspend_bo_handle is a user GEM handle already accounted by drm_show_memory_stats(), counting it in kbo_sizes as well inflates panthor-resident-memory and panthor-active-memory in fdinfo.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v5 13/13 drm/panthor: Bump version to expose protm feature
In reply to Ketil JohnsenBumps version to expose previously added:
- DRM_IOCTL_PANTHOR_PROTM_INIT ioctl
- DRM_PANTHOR_DEV_QUERY_PROTM_INFO query
- drm_panthor_group_create::protected_suspend_bo_handle
---
v4:
- First version of this change as a separate commit
--- drivers/gpu/drm/panthor/panthor_drv.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/panthor/panthor_drv.c +4 -1
@@ -1800,6 +1800,9 @@ static void panthor_debugfs_init(struct drm_minor *minor)* - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags * - 1.9 - adds DRM_PANTHOR_DEV_QUERY_MMU_INFO query * - adds DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE flag +* - 1.10 - adds DRM_IOCTL_PANTHOR_PROTM_INIT ioctl +* - adds DRM_PANTHOR_DEV_QUERY_PROTM_INFO query +* - adds drm_panthor_group_create::protected_suspend_bo_handle */ static const struct drm_driver panthor_drm_driver = { .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1813,7 +1816,7 @@ static const struct drm_driver panthor_drm_driver = { .name = "panthor", .desc = "Panthor DRM driver", .major = 1, - .minor = 9, + .minor = 10, .gem_prime_import_sg_table = panthor_gem_prime_import_sg_table, .gem_prime_import = panthor_gem_prime_import,
Signature · 1 line
2.43.0