Fixes for vc4 hotplug rework
Subsystems (3)
cover Fixes for vc4 hotplug rework
Hi,
I've found a few potential issues left after the hotplug rework.
In vc4_hdmi.c we're missing two mutex_unlock() calls when the device is unplugged.
vc4_crtc and vc4_plane seem to miss some drm_dev_enter()/drm_dev_exit() calls to protect against resource access after the device/driver is unbound, but the DRM potentially isn't freed yet and userspace can still call into the driver.
Danilo Krummrich (3):
drm/vc4: hdmi: unlock mutex when device is unplugged drm/vc4: plane: protect device resources after removal drm/vc4: crtc: protect device resources after removal
drivers/gpu/drm/vc4/vc4_crtc.c | 41 ++++++++++++++++++++++++++++++++- drivers/gpu/drm/vc4/vc4_drv.h | 1 + drivers/gpu/drm/vc4/vc4_hdmi.c | 7 ++++-- drivers/gpu/drm/vc4/vc4_plane.c | 25 ++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-)
base-commit: 8ba9249396bef37cb68be9e8dee7847f1737db9d
Signature · 1 line
2.37.2
PATCH 1/3 drm/vc4: hdmi: unlock mutex when device is unplugged
In vc4_hdmi_encoder_{pre,post}_crtc_enable() commit cd00ed5187bf ("drm/vc4: hdmi: Protect device resources after removal") missed to unlock the mutex before returning due to drm_dev_enter() indicating the device being unplugged.
--- drivers/gpu/drm/vc4/vc4_hdmi.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/vc4/vc4_hdmi.c +5 -2
@@ -1425,7 +1425,7 @@ static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,mutex_lock (&vc4_hdmi->mutex);if (!drm_dev_enter (drm, &idx)) -return ; + goto out;if (vc4_hdmi->variant->csc_setup) vc4_hdmi->variant->csc_setup (vc4_hdmi, conn_state, mode);
@@ -1436,6 +1436,7 @@ static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder *encoder,drm_dev_exit (idx); +out:mutex_unlock (&vc4_hdmi->mutex); }
@@ -1455,7 +1456,7 @@ static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,mutex_lock (&vc4_hdmi->mutex);if (!drm_dev_enter (drm, &idx)) -return ; + goto out;spin_lock_irqsave (&vc4_hdmi->hw_lock, flags);
@@ -1516,6 +1517,8 @@ static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder *encoder,vc4_hdmi_enable_scrambling (encoder);drm_dev_exit (idx); + +out:mutex_unlock (&vc4_hdmi->mutex); }
Signature · 1 line
2.37.2
PATCH 2/3 drm/vc4: plane: protect device resources after removal
In reply to Danilo Krummrich(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.
However, the DRM device isn't freed as long as the last user closed it, hence userspace can still call into the driver.
Therefore protect the critical sections which are accessing those resources with drm_dev_enter() and drm_dev_exit().
--- drivers/gpu/drm/vc4/vc4_drv.h | 1 + drivers/gpu/drm/vc4/vc4_plane.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+)
drivers/gpu/drm/vc4/vc4_drv.h +1 -0
@@ -341,6 +341,7 @@ struct vc4_hvs {struct vc4_plane {struct drm_plane base; +struct drm_device *dev; }; static inline struct vc4_plane *
drivers/gpu/drm/vc4/vc4_plane.c +25 -0
@@ -19,6 +19,7 @@#include <drm/drm_atomic_helper.h> #include <drm/drm_atomic_uapi.h> #include <drm/drm_blend.h> +#include <drm/drm_drv.h> #include <drm/drm_fb_dma_helper.h> #include <drm/drm_fourcc.h> #include <drm/drm_framebuffer.h>
@@ -1218,14 +1219,22 @@ static void vc4_plane_atomic_update(struct drm_plane *plane,u32 vc4_plane_write_dlist (struct drm_plane *plane,u32 __iomem *dlist) {struct vc4_plane_state *vc4_state =to_vc4_plane_state (plane->state); +struct vc4_plane *vc4_plane =to_vc4_plane (plane);int i; +int idx; vc4_state->hw_dlist = dlist; +if (!drm_dev_enter (vc4_plane->dev, &idx)) + goto out; +/* Can't memcpy_toio() because it needs to be 32-bit writes. */ for (i =0 ; i < vc4_state->dlist_count; i++)writel (vc4_state->dlist[i], &dlist[i]); +drm_dev_exit (idx); + +out:return vc4_state->dlist_count; }
@@ -1243,8 +1252,10 @@ u32 vc4_plane_dlist_size(const struct drm_plane_state *state)void vc4_plane_async_set_fb (struct drm_plane *plane,struct drm_framebuffer *fb) {struct vc4_plane_state *vc4_state =to_vc4_plane_state (plane->state); +struct vc4_plane *vc4_plane =to_vc4_plane (plane);struct drm_gem_dma_object *bo =drm_fb_dma_get_gem_obj (fb,0 );uint32_t addr; +int idx;/* We're skipping the address adjustment for negative origin, * because this is only called on the primary plane.
@@ -1252,12 +1263,17 @@ void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)WARN_ON_ONCE (plane->state->crtc_x <0 || plane->state->crtc_y <0 ); addr = bo->dma_addr + fb->offsets[0 ]; +if (!drm_dev_enter (vc4_plane->dev, &idx)) +return ; +/* Write the new address into the hardware immediately. The * scanout will start from this address as soon as the FIFO * needs to refill with pixels. */ writel (addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]); +drm_dev_exit (idx); +/* Also update the CPU-side dlist copy, so that any later * atomic updates that don't do a new modeset on our plane * also use our updated address.
@@ -1271,6 +1287,8 @@ static void vc4_plane_atomic_async_update(struct drm_plane *plane,struct drm_plane_state *new_plane_state =drm_atomic_get_new_plane_state (state, plane);struct vc4_plane_state *vc4_state, *new_vc4_state; +struct vc4_plane *vc4_plane =to_vc4_plane (plane); +int idx;swap (plane->state->fb, new_plane_state->fb); plane->state->crtc_x = new_plane_state->crtc_x;
@@ -1323,6 +1341,9 @@ static void vc4_plane_atomic_async_update(struct drm_plane *plane, vc4_state->dlist[vc4_state->ptr0_offset] = new_vc4_state->dlist[vc4_state->ptr0_offset]; +if (!drm_dev_enter (vc4_plane->dev, &idx)) +return ; +/* Note that we can't just call vc4_plane_write_dlist() * because that would smash the context data that the HVS is * currently using.
@@ -1333,6 +1354,8 @@ static void vc4_plane_atomic_async_update(struct drm_plane *plane, &vc4_state->hw_dlist[vc4_state->pos2_offset]);writel (vc4_state->dlist[vc4_state->ptr0_offset], &vc4_state->hw_dlist[vc4_state->ptr0_offset]); + +drm_dev_exit (idx); } static int vc4_plane_atomic_async_check(struct drm_plane *plane,
@@ -1521,6 +1544,8 @@ struct drm_plane *vc4_plane_init(struct drm_device *dev, modifiers, type, NULL);if (IS_ERR (vc4_plane))return ERR_CAST (vc4_plane); + + vc4_plane->dev = dev; plane = &vc4_plane->base;if (vc4->is_vc5)
Signature · 1 line
2.37.2
Re: [PATCH drm-misc-next 2/3] drm/vc4: plane: protect device resources after removal
Hi,
Quoted 8 lines · Danilo Krummrich
(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.
However, the DRM device isn't freed as long as the last user closed it, hence userspace can still call into the driver.
Therefore protect the critical sections which are accessing those resources with
drm_dev_enter()anddrm_dev_exit().
Ah good catch, thanks
Quoted 12 lines
Fixes 9872c7a31921 ("drm/vc4: plane: Switch to drmm_universal_plane_alloc()")Signed-off-by Danilo Krummrich <[email protected]>--- drivers/gpu/drm/vc4/vc4_drv.h | 1 + drivers/gpu/drm/vc4/vc4_plane.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+)drivers/gpu/drm/vc4/vc4_drv.h +0
-0
drivers/gpu/drm/vc4/vc4_drv.h struct vc4_hvs { · line 344
Quoted 4 lines
drivers/gpu/drm/vc4/vc4_drv.h +1
-0@@ -341,6 +341,7 @@ struct vc4_hvs {struct vc4_plane {struct drm_plane base; +struct drm_device *dev;
That pointer already exists in struct drm_plane
Looks good otherwise
Maxime
Re: [PATCH drm-misc-next 2/3] drm/vc4: plane: protect device resources after removal
Hi Maxime,
Quoted 27 lines · Maxime Ripard
Hi,
Quoted 8 lines · Danilo Krummrich
(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.
However, the DRM device isn't freed as long as the last user closed it, hence userspace can still call into the driver.
Therefore protect the critical sections which are accessing those resources with
drm_dev_enter()anddrm_dev_exit().Ah good catch, thanks
Quoted 13 lines
Fixes 9872c7a31921 ("drm/vc4: plane: Switch to drmm_universal_plane_alloc()")Signed-off-by Danilo Krummrich <[email protected]>--- drivers/gpu/drm/vc4/vc4_drv.h | 1 + drivers/gpu/drm/vc4/vc4_plane.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+)drivers/gpu/drm/vc4/vc4_drv.h +0
-0@@ -341,6 +341,7 @@ struct vc4_hvs {
Quoted 5 lines
drivers/gpu/drm/vc4/vc4_drv.h struct vc4_hvs { · line 344
Quoted 3 lines
drivers/gpu/drm/vc4/vc4_drv.h +1
-0struct vc4_plane {struct drm_plane base; +struct drm_device *dev;That pointer already exists in struct
drm_plane
Oops, I've sent a v2. Also addressing your comment from the other patch.
- Danilo
Quoted 4 lines
Looks good otherwise
Maxime
PATCH 3/3 drm/vc4: crtc: protect device resources after removal
In reply to Danilo Krummrich(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.
However, the DRM device isn't freed as long as the last user closed it, hence userspace can still call into the driver.
Therefore protect the critical sections which are accessing those resources with drm_dev_enter() and drm_dev_exit().
--- drivers/gpu/drm/vc4/vc4_crtc.c | 41 +++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-)
drivers/gpu/drm/vc4/vc4_crtc.c +40 -1
@@ -39,6 +39,7 @@#include <drm/drm_atomic_uapi.h> #include <drm/drm_fb_dma_helper.h> #include <drm/drm_framebuffer.h> +#include <drm/drm_drv.h> #include <drm/drm_print.h> #include <drm/drm_probe_helper.h> #include <drm/drm_vblank.h>
@@ -295,10 +296,17 @@ struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,static void vc4_crtc_pixelvalve_reset (struct drm_crtc *crtc) {struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc); +struct drm_device *dev = crtc->dev; +int idx; + +if (!drm_dev_enter (dev, &idx)) +return ;/* The PV needs to be disabled before it can be flushed */ CRTC_WRITE (PV_CONTROL ,CRTC_READ (PV_CONTROL ) & ~PV_CONTROL_EN );CRTC_WRITE (PV_CONTROL ,CRTC_READ (PV_CONTROL ) |PV_CONTROL_FIFO_CLR ); + +drm_dev_exit (idx); } static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encoder,
@@ -321,6 +329,10 @@ static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encodeu32 format = is_dsi1 ?PV_CONTROL_FORMAT_DSIV_24 :PV_CONTROL_FORMAT_24 ;u8 ppc = pv_data->pixels_per_clock;bool debug_dump_regs = false; +int idx; + +if (!drm_dev_enter (dev, &idx)) +return ;if (debug_dump_regs) {struct drm_printer p =drm_info_printer (&vc4_crtc->pdev->dev);
@@ -410,6 +422,8 @@ static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encode drm_crtc_index(crtc));drm_print_regset32 (&p, &vc4_crtc->regset); } + +drm_dev_exit (idx); }static void require_hvs_enabled (struct drm_device *dev)
@@ -430,13 +444,18 @@ static int vc4_crtc_disable(struct drm_crtc *crtc,struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc);struct drm_device *dev = crtc->dev;struct vc4_dev *vc4 =to_vc4_dev (dev); -int ret; +int idx, ret; + +if (!drm_dev_enter (dev, &idx)) +return -ENODEV ;CRTC_WRITE (PV_V_CONTROL ,CRTC_READ (PV_V_CONTROL ) & ~PV_VCONTROL_VIDEN ); ret =wait_for (!(CRTC_READ (PV_V_CONTROL ) &PV_VCONTROL_VIDEN ),1 );WARN_ONCE (ret,"Timeout waiting for !PV_VCONTROL_VIDEN\n" ); +drm_dev_exit (idx); +/* * This delay is needed to avoid to get a pixel stuck in an * unflushable FIFO between the pixelvalve and the HDMI
@@ -588,6 +607,7 @@ static void vc4_crtc_atomic_enable(struct drm_crtc *crtc,struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc);struct drm_encoder *encoder =vc4_get_crtc_encoder (crtc, new_state);struct vc4_encoder *vc4_encoder =to_vc4_encoder (encoder); +int idx;drm_dbg (dev,"Enabling CRTC %s (%u) connected to Encoder %s (%u)" , crtc->name, crtc->base.id, encoder->name, encoder->base.id);
@@ -606,6 +626,9 @@ static void vc4_crtc_atomic_enable(struct drm_crtc *crtc,vc4_crtc_config_pv (crtc, encoder, state); +if (!drm_dev_enter (dev, &idx)) +return ; +CRTC_WRITE (PV_CONTROL ,CRTC_READ (PV_CONTROL ) |PV_CONTROL_EN );if (vc4_encoder->pre_crtc_enable)
@@ -617,6 +640,8 @@ static void vc4_crtc_atomic_enable(struct drm_crtc *crtc,CRTC_WRITE (PV_V_CONTROL ,CRTC_READ (PV_V_CONTROL ) |PV_VCONTROL_VIDEN ); +drm_dev_exit (idx); +if (vc4_encoder->post_crtc_enable) vc4_encoder->post_crtc_enable (encoder, state); }
@@ -711,17 +736,31 @@ static int vc4_crtc_atomic_check(struct drm_crtc *crtc,static int vc4_enable_vblank (struct drm_crtc *crtc) {struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc); +struct drm_device *dev = crtc->dev; +int idx; + +if (!drm_dev_enter (dev, &idx)) +return -ENODEV ;CRTC_WRITE (PV_INTEN ,PV_INT_VFP_START ); +drm_dev_exit (idx); +return 0 ; }static void vc4_disable_vblank (struct drm_crtc *crtc) {struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc); +struct drm_device *dev = crtc->dev; +int idx; + +if (!drm_dev_enter (dev, &idx)) +return ;CRTC_WRITE (PV_INTEN ,0 ); + +drm_dev_exit (idx); }static void vc4_crtc_handle_page_flip (struct vc4_crtc *vc4_crtc)
Signature · 1 line
2.37.2
Re: [PATCH drm-misc-next 3/3] drm/vc4: crtc: protect device resources after removal
Hi,
Quoted 72 lines · Danilo Krummrich
(Hardware) resources which are bound to the driver and device lifecycle must not be accessed after the device and driver are unbound.
However, the DRM device isn't freed as long as the last user closed it, hence userspace can still call into the driver.
Therefore protect the critical sections which are accessing those resources with
drm_dev_enter()anddrm_dev_exit().Fixes 7cc4214c27cf ("drm/vc4: crtc: Switch to drmm_kzalloc")Signed-off-by Danilo Krummrich <[email protected]>--- drivers/gpu/drm/vc4/vc4_crtc.c | 41 +++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-)drivers/gpu/drm/vc4/vc4_crtc.c +16
-1@@ -39,6 +39,7 @@#include <drm/drm_atomic_uapi.h> #include <drm/drm_fb_dma_helper.h> #include <drm/drm_framebuffer.h> +#include <drm/drm_drv.h> #include <drm/drm_print.h> #include <drm/drm_probe_helper.h> #include <drm/drm_vblank.h> @@ -295,10 +296,17 @@ struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,static void vc4_crtc_pixelvalve_reset (struct drm_crtc *crtc) {struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc); +struct drm_device *dev = crtc->dev; +int idx; + +if (!drm_dev_enter (dev, &idx)) +return ;/* The PV needs to be disabled before it can be flushed */ CRTC_WRITE (PV_CONTROL ,CRTC_READ (PV_CONTROL ) & ~PV_CONTROL_EN );CRTC_WRITE (PV_CONTROL ,CRTC_READ (PV_CONTROL ) |PV_CONTROL_FIFO_CLR ); + +drm_dev_exit (idx); } static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encoder,@@ -321,6 +329,10 @@ static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encodeu32 format = is_dsi1 ?PV_CONTROL_FORMAT_DSIV_24 :PV_CONTROL_FORMAT_24 ;u8 ppc = pv_data->pixels_per_clock;bool debug_dump_regs = false; +int idx; + +if (!drm_dev_enter (dev, &idx)) +return ;if (debug_dump_regs) {struct drm_printer p =drm_info_printer (&vc4_crtc->pdev->dev);@@ -410,6 +422,8 @@ static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encode drm_crtc_index(crtc));drm_print_regset32 (&p, &vc4_crtc->regset); } + +drm_dev_exit (idx); }static void require_hvs_enabled (struct drm_device *dev)@@ -430,13 +444,18 @@ static int vc4_crtc_disable(struct drm_crtc *crtc,struct vc4_crtc *vc4_crtc =to_vc4_crtc (crtc);struct drm_device *dev = crtc->dev;struct vc4_dev *vc4 =to_vc4_dev (dev); -int ret; +int idx, ret; +
drivers/gpu/drm/vc4/vc4_crtc.c static int vc4_crtc_disable(struct drm_crtc *crtc, · line 458
Quoted 10 lines
drivers/gpu/drm/vc4/vc4_crtc.c +4
-0+if (!drm_dev_enter (dev, &idx)) +return -ENODEV ;CRTC_WRITE (PV_V_CONTROL ,CRTC_READ (PV_V_CONTROL ) & ~PV_VCONTROL_VIDEN ); ret =wait_for (!(CRTC_READ (PV_V_CONTROL ) &PV_VCONTROL_VIDEN ),1 );WARN_ONCE (ret,"Timeout waiting for !PV_VCONTROL_VIDEN\n" ); +drm_dev_exit (idx); +
I think this would be easier to follow if we were protecting the entire function with our lock.
Having locks taken in the middle of the function is harder to identify whether or not one particular function is safe or not.
The same applies to the plane patch
Maxime