drm/vc4: Miscellaneous fixes and improvements
Subsystems (3)
Maíra Canal
reviewercover drm/vc4: Miscellaneous fixes and improvements
Hi,
This is a spin-off of the series "[PATCH 00/11] drm/vc4: Switch to DRM GPU scheduler" [1]. While I was writing the series, I noticed some small bugs in the driver code, so I compiled the fixes and code improvements in this series.
Patches #1 to #4 are bug-fixes related to PM reference unbalance, memory leaks, and race conditions. They will probably go through the drm-misc-fixes branch. Finally, patches #5 and #6 are minor clean-ups and common API adoption.
[1] https://lore.kernel.org/dri-devel/[email protected]/T/
Best regards,
- Maíra
---
Maíra Canal (6):
drm/vc4: Release runtime PM reference after binding V3D drm/vc4: Fix memory leak of BO array in hang state drm/vc4: Fix a memory leak in hang state error path drm/vc4: Protect madv read in vc4_gem_object_mmap() with madv_lock drm/vc4: Use devm_request_irq() for automatic cleanup drm/vc4: Clean-up UAPI header inclusion
drivers/gpu/drm/vc4/vc4_bo.c | 4 +++- drivers/gpu/drm/vc4/vc4_drv.c | 2 -- drivers/gpu/drm/vc4/vc4_gem.c | 20 +++++++++++--------- drivers/gpu/drm/vc4/vc4_irq.c | 33 ++++++++++++--------------------- drivers/gpu/drm/vc4/vc4_plane.c | 2 -- drivers/gpu/drm/vc4/vc4_render_cl.c | 1 - drivers/gpu/drm/vc4/vc4_v3d.c | 1 + drivers/gpu/drm/vc4/vc4_validate.c | 1 - 8 files changed, 27 insertions(+), 37 deletions(-)
---
base-commit: dac97a6bd6c4a24e60a147cb2cf750eddb7594a8
change-id: 20260330-vc4-misc-fixes-9b874f854fcf
Maíra Canal
reviewerPATCH 1/6 drm/vc4: Release runtime PM reference after binding V3D
The vc4_v3d_bind() function acquires a runtime PM reference via pm_runtime_resume_and_get() to access V3D registers during setup. However, this reference is never released after a successful bind. This prevents the device from ever runtime suspending, since the reference count never reaches zero.
Release the runtime PM reference by adding pm_runtime_put_autosuspend() after autosuspend is configured, allowing the device to runtime suspend after the delay.
--- drivers/gpu/drm/vc4/vc4_v3d.c | 1 + 1 file changed, 1 insertion(+)
drivers/gpu/drm/vc4/vc4_v3d.c +1 -0
@@ -481,6 +481,7 @@ static int vc4_v3d_bind(struct device *dev, struct device *master, void *data)pm_runtime_use_autosuspend (dev);pm_runtime_set_autosuspend_delay (dev,40 );/* a little over 2 frames. */ +pm_runtime_put_autosuspend (dev);return 0 ;
Signature · 1 line
2.53.0
Maíra Canal
reviewerPATCH 2/6 drm/vc4: Fix memory leak of BO array in hang state
In reply to Maíra CanalThe hang state's BO array is allocated separately with kzalloc() in vc4_save_hang_state() but never freed in vc4_free_hang_state(). Add the missing kfree() for the BO array before freeing the hang state struct.
--- drivers/gpu/drm/vc4/vc4_gem.c | 1 + 1 file changed, 1 insertion(+)
drivers/gpu/drm/vc4/vc4_gem.c +1 -0
@@ -62,6 +62,7 @@ vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)for (i =0 ; i < state->user_state.bo_count; i++)drm_gem_object_put (state->bo[i]); +kfree (state->bo);kfree (state); }
Signature · 1 line
2.53.0
Re: [PATCH 2/6] drm/vc4: Fix memory leak of BO array in hang state
Quoted 5 lines · Maíra Canal
The hang state's BO array is allocated separately with
kzalloc()invc4_save_hang_state()but never freed invc4_free_hang_state(). Add the missingkfree()for the BO array before freeing the hang state struct.Fixes 214613656b51 ("drm/vc4: Add an interface for capturing the GPU state after a hang.")
Quoted 18 lines
Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/vc4/vc4_gem.c | 1 + 1 file changed, 1 insertion(+)drivers/gpu/drm/vc4/vc4_gem.c +1
-0@@ -62,6 +62,7 @@ vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)for (i =0 ; i < state->user_state.bo_count; i++)drm_gem_object_put (state->bo[i]); +kfree (state->bo);kfree (state); }
Maíra Canal
reviewerPATCH 3/6 drm/vc4: Fix a memory leak in hang state error path
In reply to Maíra CanalWhen vc4_save_hang_state() encounters an early return condition, it returns without freeing the previously allocated kernel_state, leaking memory.
Add the missing kfree() calls by consolidating the early return paths into a single place.
--- drivers/gpu/drm/vc4/vc4_gem.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/gpu/drm/vc4/vc4_gem.c +10 -8
@@ -171,10 +171,8 @@ vc4_save_hang_state(struct drm_device *dev)spin_lock_irqsave (&vc4->job_lock, irqflags); exec[0 ] =vc4_first_bin_job (vc4); exec[1 ] =vc4_first_render_job (vc4); -if (!exec[0 ] && !exec[1 ]) { -spin_unlock_irqrestore (&vc4->job_lock, irqflags); -return ; - } +if (!exec[0 ] && !exec[1 ]) + goto err_free_state;/* Get the bos from both binner and renderer into hang state. */ state->bo_count =0 ;
@@ -191,10 +189,8 @@ vc4_save_hang_state(struct drm_device *dev) kernel_state->bo =kzalloc_objs (*kernel_state->bo, state->bo_count,GFP_ATOMIC ); -if (!kernel_state->bo) { -spin_unlock_irqrestore (&vc4->job_lock, irqflags); -return ; - } +if (!kernel_state->bo) + goto err_free_state; k =0 ;for (i =0 ; i <2 ; i++) {
@@ -286,6 +282,12 @@ vc4_save_hang_state(struct drm_device *dev) vc4->hang_state = kernel_state;spin_unlock_irqrestore (&vc4->job_lock, irqflags); } + +return ; + +err_free_state: +spin_unlock_irqrestore (&vc4->job_lock, irqflags); +kfree (kernel_state); } static void
Signature · 1 line
2.53.0
Re: [PATCH 3/6] drm/vc4: Fix a memory leak in hang state error path
Quoted 6 lines · Maíra Canal
When
vc4_save_hang_state()encounters an early return condition, it returns without freeing the previously allocatedkernel_state, leaking memory.Add the missing
kfree()calls by consolidating the early return paths into a single place.
Nice catch!
Quoted 51 lines
Fixes 214613656b51 ("drm/vc4: Add an interface for capturing the GPU state after a hang.")Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/vc4/vc4_gem.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)drivers/gpu/drm/vc4/vc4_gem.c +10
-8@@ -171,10 +171,8 @@ vc4_save_hang_state(struct drm_device *dev)spin_lock_irqsave (&vc4->job_lock, irqflags); exec[0 ] =vc4_first_bin_job (vc4); exec[1 ] =vc4_first_render_job (vc4); -if (!exec[0 ] && !exec[1 ]) { -spin_unlock_irqrestore (&vc4->job_lock, irqflags); -return ; - } +if (!exec[0 ] && !exec[1 ]) + goto err_free_state;/* Get the bos from both binner and renderer into hang state. */ state->bo_count =0 ;@@ -191,10 +189,8 @@ vc4_save_hang_state(struct drm_device *dev) kernel_state->bo =kzalloc_objs (*kernel_state->bo, state->bo_count,GFP_ATOMIC ); -if (!kernel_state->bo) { -spin_unlock_irqrestore (&vc4->job_lock, irqflags); -return ; - } +if (!kernel_state->bo) + goto err_free_state; k =0 ;for (i =0 ; i <2 ; i++) {@@ -286,6 +282,12 @@ vc4_save_hang_state(struct drm_device *dev) vc4->hang_state = kernel_state;spin_unlock_irqrestore (&vc4->job_lock, irqflags); } + +return ; + +err_free_state: +spin_unlock_irqrestore (&vc4->job_lock, irqflags); +kfree (kernel_state); } static void
Maíra Canal
reviewerPATCH 4/6 drm/vc4: Protect madv read in vc4_gem_object_mmap() with madv_lock
In reply to Maíra CanalThe mmap callback reads bo->madv without holding madv_lock, racing with concurrent DRM_IOCTL_VC4_GEM_MADVISE calls that modify the field under the same lock. Add the missing locking to prevent the data race.
--- drivers/gpu/drm/vc4/vc4_bo.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/gpu/drm/vc4/vc4_bo.c +3 -0
@@ -738,12 +738,15 @@ static int vc4_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_structreturn -EINVAL ; } +mutex_lock (&bo->madv_lock);if (bo->madv !=VC4_MADV_WILLNEED ) {DRM_DEBUG ("mmapping of %s BO not allowed\n" , bo->madv ==VC4_MADV_DONTNEED ?"purgeable" :"purged" ); +mutex_unlock (&bo->madv_lock);return -EINVAL ; } +mutex_unlock (&bo->madv_lock);return drm_gem_dma_mmap (&bo->base, vma); }
Signature · 1 line
2.53.0
Re: [PATCH 4/6] drm/vc4: Protect madv read in vc4_gem_object_mmap() with madv_lock
Quoted 5 lines · Maíra Canal
The mmap callback reads
bo->madvwithout holdingmadv_lock, racing with concurrentDRM_IOCTL_VC4_GEM_MADVISEcalls that modify the field under the same lock. Add the missing locking to prevent the data race.Fixes b9f19259b84d ("drm/vc4: Add the DRM_IOCTL_VC4_GEM_MADVISE ioctl")
Quoted 26 lines
Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/vc4/vc4_bo.c | 3 +++ 1 file changed, 3 insertions(+)drivers/gpu/drm/vc4/vc4_bo.c +3
-0@@ -738,12 +738,15 @@ static int vc4_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_structreturn -EINVAL ; } +mutex_lock (&bo->madv_lock);if (bo->madv !=VC4_MADV_WILLNEED ) {DRM_DEBUG ("mmapping of %s BO not allowed\n" , bo->madv ==VC4_MADV_DONTNEED ?"purgeable" :"purged" ); +mutex_unlock (&bo->madv_lock);return -EINVAL ; } +mutex_unlock (&bo->madv_lock);return drm_gem_dma_mmap (&bo->base, vma); }
Maíra Canal
reviewerPATCH 5/6 drm/vc4: Use devm_request_irq() for automatic cleanup
In reply to Maíra CanalSwitch from request_irq()/free_irq() to the device-managed alternative devm_request_irq(), letting device-managed resource cleanup handle IRQ teardown automatically.
While here, inline vc4_irq_prepare() into vc4_irq_install() since it's the only caller.
--- drivers/gpu/drm/vc4/vc4_irq.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-)
drivers/gpu/drm/vc4/vc4_irq.c +12 -21
@@ -47,7 +47,6 @@#include <linux/platform_device.h> -#include <drm/drm_drv.h> #include <drm/drm_print.h> #include "vc4_drv.h"
@@ -242,23 +241,6 @@ vc4_irq(int irq, void *arg)return status; } -static void -vc4_irq_prepare (struct drm_device *dev) -{ -struct vc4_dev *vc4 =to_vc4_dev (dev); - -if (!vc4->v3d) -return ; - -init_waitqueue_head (&vc4->job_wait_queue); -INIT_WORK (&vc4->overflow_mem_work, vc4_overflow_mem_work); - -/* Clear any pending interrupts someone might have left around -* for us. -*/ -V3D_WRITE (V3D_INTCTL ,V3D_DRIVER_IRQS ); -} -void vc4_irq_enable (struct drm_device *dev) {
@@ -307,12 +289,22 @@ int vc4_irq_install(struct drm_device *dev, int irq)if (WARN_ON_ONCE (vc4->gen >VC4_GEN_4 ))return -ENODEV ; +if (!vc4->v3d) +return -ENODEV ; +if (irq ==IRQ_NOTCONNECTED )return -ENOTCONN ; -vc4_irq_prepare (dev); +init_waitqueue_head (&vc4->job_wait_queue); +INIT_WORK (&vc4->overflow_mem_work, vc4_overflow_mem_work); - ret =request_irq (irq, vc4_irq,0 , dev->driver->name, dev); +/* Clear any pending interrupts someone might have left around +* for us. +*/ +V3D_WRITE (V3D_INTCTL ,V3D_DRIVER_IRQS ); + + ret =devm_request_irq (dev->dev, irq, vc4_irq,0 , +dev_name (dev->dev), dev);if (ret)return ret;
@@ -329,7 +321,6 @@ void vc4_irq_uninstall(struct drm_device *dev)return ;vc4_irq_disable (dev); -free_irq (vc4->irq, dev); }/** Reinitializes interrupt registers when a GPU reset is performed. */
Signature · 1 line
2.53.0
Re: [PATCH 5/6] drm/vc4: Use devm_request_irq() for automatic cleanup
Quoted 6 lines · Maíra Canal
Switch from request_irq()/free_irq() to the device-managed alternative
devm_request_irq(), letting device-managed resource cleanup handle IRQ teardown automatically.While here, inline
vc4_irq_prepare()intovc4_irq_install()since it's the only caller.
Quoted 76 lines
Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/vc4/vc4_irq.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-)drivers/gpu/drm/vc4/vc4_irq.c +12
-21@@ -47,7 +47,6 @@#include <linux/platform_device.h> -#include <drm/drm_drv.h> #include <drm/drm_print.h> #include "vc4_drv.h" @@ -242,23 +241,6 @@ vc4_irq(int irq, void *arg)return status; } -static void -vc4_irq_prepare (struct drm_device *dev) -{ -struct vc4_dev *vc4 =to_vc4_dev (dev); - -if (!vc4->v3d) -return ; - -init_waitqueue_head (&vc4->job_wait_queue); -INIT_WORK (&vc4->overflow_mem_work, vc4_overflow_mem_work); - -/* Clear any pending interrupts someone might have left around -* for us. -*/ -V3D_WRITE (V3D_INTCTL ,V3D_DRIVER_IRQS ); -} -void vc4_irq_enable (struct drm_device *dev) {@@ -307,12 +289,22 @@ int vc4_irq_install(struct drm_device *dev, int irq)if (WARN_ON_ONCE (vc4->gen >VC4_GEN_4 ))return -ENODEV ; +if (!vc4->v3d) +return -ENODEV ; +if (irq ==IRQ_NOTCONNECTED )return -ENOTCONN ; -vc4_irq_prepare (dev); +init_waitqueue_head (&vc4->job_wait_queue); +INIT_WORK (&vc4->overflow_mem_work, vc4_overflow_mem_work); - ret =request_irq (irq, vc4_irq,0 , dev->driver->name, dev); +/* Clear any pending interrupts someone might have left around +* for us. +*/ +V3D_WRITE (V3D_INTCTL ,V3D_DRIVER_IRQS ); + + ret =devm_request_irq (dev->dev, irq, vc4_irq,0 , +dev_name (dev->dev), dev);if (ret)return ret;@@ -329,7 +321,6 @@ void vc4_irq_uninstall(struct drm_device *dev)return ;vc4_irq_disable (dev); -free_irq (vc4->irq, dev); }/** Reinitializes interrupt registers when a GPU reset is performed. */
Maíra Canal
reviewerPATCH 6/6 drm/vc4: Clean-up UAPI header inclusion
In reply to Maíra Canal"uapi/drm/vc4_drm.h" is already included through "vc4_drv.h". Therefore, remove its direct inclusion from several files.
--- drivers/gpu/drm/vc4/vc4_bo.c | 1 - drivers/gpu/drm/vc4/vc4_drv.c | 2 -- drivers/gpu/drm/vc4/vc4_gem.c | 1 - drivers/gpu/drm/vc4/vc4_plane.c | 2 -- drivers/gpu/drm/vc4/vc4_render_cl.c | 1 - drivers/gpu/drm/vc4/vc4_validate.c | 1 - 6 files changed, 8 deletions(-)
drivers/gpu/drm/vc4/vc4_bo.c +0 -1
@@ -22,7 +22,6 @@#include <drm/drm_print.h> #include "vc4_drv.h" -#include "uapi/drm/vc4_drm.h" static const struct drm_gem_object_funcs vc4_gem_object_funcs;
drivers/gpu/drm/vc4/vc4_drv.c +0 -2
@@ -41,8 +41,6 @@#include <soc/bcm2835/raspberrypi-firmware.h> -#include "uapi/drm/vc4_drm.h" -#include "vc4_drv.h" #include "vc4_regs.h"
drivers/gpu/drm/vc4/vc4_gem.c +0 -1
@@ -33,7 +33,6 @@#include <drm/drm_print.h> #include <drm/drm_syncobj.h> -#include "uapi/drm/vc4_drm.h" #include "vc4_drv.h" #include "vc4_regs.h" #include "vc4_trace.h"
drivers/gpu/drm/vc4/vc4_plane.c +0 -2
@@ -26,8 +26,6 @@#include <drm/drm_gem_atomic_helper.h> #include <drm/drm_print.h> -#include "uapi/drm/vc4_drm.h" -#include "vc4_drv.h" #include "vc4_regs.h"
drivers/gpu/drm/vc4/vc4_render_cl.c +0 -1
@@ -37,7 +37,6 @@#include <drm/drm_print.h> -#include "uapi/drm/vc4_drm.h" #include "vc4_drv.h" #include "vc4_packet.h"
drivers/gpu/drm/vc4/vc4_validate.c +0 -1
@@ -45,7 +45,6 @@#include <drm/drm_print.h> -#include "uapi/drm/vc4_drm.h" #include "vc4_drv.h" #include "vc4_packet.h"
Signature · 1 line
2.53.0
Re: [PATCH 6/6] drm/vc4: Clean-up UAPI header inclusion
Quoted 2 lines · Maíra Canal
"
uapi/drm/vc4_drm.h" is already included through "vc4_drv.h". Therefore, remove its direct inclusion from several files.
Quoted 87 lines
Suggested-by Melissa Wen <[email protected]>Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/vc4/vc4_bo.c | 1 - drivers/gpu/drm/vc4/vc4_drv.c | 2 -- drivers/gpu/drm/vc4/vc4_gem.c | 1 - drivers/gpu/drm/vc4/vc4_plane.c | 2 -- drivers/gpu/drm/vc4/vc4_render_cl.c | 1 - drivers/gpu/drm/vc4/vc4_validate.c | 1 - 6 files changed, 8 deletions(-)drivers/gpu/drm/vc4/vc4_bo.c +0
-1@@ -22,7 +22,6 @@#include <drm/drm_print.h> #include "vc4_drv.h" -#include "uapi/drm/vc4_drm.h" static const struct drm_gem_object_funcs vc4_gem_object_funcs;drivers/gpu/drm/vc4/vc4_drv.c +0
-2@@ -41,8 +41,6 @@#include <soc/bcm2835/raspberrypi-firmware.h> -#include "uapi/drm/vc4_drm.h" -#include "vc4_drv.h" #include "vc4_regs.h" drivers/gpu/drm/vc4/vc4_gem.c +0
-1@@ -33,7 +33,6 @@#include <drm/drm_print.h> #include <drm/drm_syncobj.h> -#include "uapi/drm/vc4_drm.h" #include "vc4_drv.h" #include "vc4_regs.h" #include "vc4_trace.h" drivers/gpu/drm/vc4/vc4_plane.c +0
-2@@ -26,8 +26,6 @@#include <drm/drm_gem_atomic_helper.h> #include <drm/drm_print.h> -#include "uapi/drm/vc4_drm.h" -#include "vc4_drv.h" #include "vc4_regs.h" drivers/gpu/drm/vc4/vc4_render_cl.c +0
-1@@ -37,7 +37,6 @@#include <drm/drm_print.h> -#include "uapi/drm/vc4_drm.h" #include "vc4_drv.h" #include "vc4_packet.h" drivers/gpu/drm/vc4/vc4_validate.c +0
-1@@ -45,7 +45,6 @@#include <drm/drm_print.h> -#include "uapi/drm/vc4_drm.h" #include "vc4_drv.h" #include "vc4_packet.h"
Quoted 17 lines · Maíra Canal
Hi,
This is a spin-off of the series "[PATCH 00/11] drm/vc4: Switch to DRM GPU scheduler" [1]. While I was writing the series, I noticed some small bugs in the driver code, so I compiled the fixes and code improvements in this series.
Patches #1 to #4 are bug-fixes related to PM reference unbalance, memory leaks, and race conditions. They will probably go through the drm-misc-fixes branch. Finally, patches #5 and #6 are minor clean-ups and common API adoption.
[1] https://lore.kernel.org/dri-devel/[email protected]/T/
Best regards,
- Maíra
Quoted 8 lines
---
Maíra Canal (6):
drm/vc4: Release runtime PM reference after binding V3D drm/vc4: Fix memory leak of BO array in hang state drm/vc4: Fix a memory leak in hang state error path drm/vc4: Protect madv read invc4_gem_object_mmap()withmadv_lockdrm/vc4: Usedevm_request_irq()for automatic cleanup drm/vc4: Clean-up UAPI header inclusion
Applied to drm-misc-fixes:
[1/6] drm/vc4: Release runtime PM reference after binding V3D
commit: aaefbdde9abdc43699e110679c0e10972a5e1c59
[2/6] drm/vc4: Fix memory leak of BO array in hang state
commit: f4dfd6847b3e5d24e336bca6057485116d17aea4
[3/6] drm/vc4: Fix a memory leak in hang state error path
commit: 9525d169e5fd481538cf8c663cc5839e54f2e481
[4/6] drm/vc4: Protect madv read in vc4_gem_object_mmap() with madv_lock
commit: 338c56050d8e892604da97f67bfa8cc4015a955fApplied to drm-misc-next:
[5/6] drm/vc4: Use devm_request_irq() for automatic cleanup
commit: 19e2815babd4c51a51ebabfe68daa90eb26309c3
[6/6] drm/vc4: Clean-up UAPI header inclusion
commit: 4b9c36c83b34f710da9573291404f6a2246251c1Thanks for the reviews!
Best regards,
- Maíra