dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Maxime Ripard <maxime@cerno.tech>
To: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	Maxime Ripard <maxime@cerno.tech>,
	Daniel Vetter <daniel.vetter@intel.com>,
	David Airlie <airlied@linux.ie>
Cc: dri-devel@lists.freedesktop.org
Subject: [PATCH v2 14/68] drm/vc4: hvs: Protect device resources after removal
Date: Wed, 22 Jun 2022 16:31:15 +0200	[thread overview]
Message-ID: <20220622143209.600298-15-maxime@cerno.tech> (raw)
In-Reply-To: <20220622143209.600298-1-maxime@cerno.tech>

Whenever the device and driver are unbound, the main device and all the
subdevices will be removed by calling their unbind() method.

However, the DRM device itself will only be freed when the last user will
have closed it.

It means that there is a time window where the device and its resources
aren't there anymore, but the userspace can still call into our driver.

Fortunately, the DRM framework provides the drm_dev_enter() and
drm_dev_exit() functions to make sure our underlying device is still there
for the section protected by those calls. Let's add them to the HVS driver.

Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/gpu/drm/vc4/vc4_hvs.c | 99 ++++++++++++++++++++++++++++++++---
 1 file changed, 93 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/vc4/vc4_hvs.c b/drivers/gpu/drm/vc4/vc4_hvs.c
index ba2c8e5a9b64..c02243bcb61f 100644
--- a/drivers/gpu/drm/vc4/vc4_hvs.c
+++ b/drivers/gpu/drm/vc4/vc4_hvs.c
@@ -25,6 +25,7 @@
 #include <linux/platform_device.h>
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_drv.h>
 #include <drm/drm_vblank.h>
 
 #include "vc4_drv.h"
@@ -66,11 +67,15 @@ static const struct debugfs_reg32 hvs_regs[] = {
 
 void vc4_hvs_dump_state(struct vc4_hvs *hvs)
 {
+	struct drm_device *drm = &hvs->vc4->base;
 	struct drm_printer p = drm_info_printer(&hvs->pdev->dev);
-	int i;
+	int idx, i;
 
 	drm_print_regset32(&p, &hvs->regset);
 
+	if (!drm_dev_enter(drm, &idx))
+		return;
+
 	DRM_INFO("HVS ctx:\n");
 	for (i = 0; i < 64; i += 4) {
 		DRM_INFO("0x%08x (%s): 0x%08x 0x%08x 0x%08x 0x%08x\n",
@@ -80,6 +85,8 @@ void vc4_hvs_dump_state(struct vc4_hvs *hvs)
 			 readl((u32 __iomem *)hvs->dlist + i + 2),
 			 readl((u32 __iomem *)hvs->dlist + i + 3));
 	}
+
+	drm_dev_exit(idx);
 }
 
 static int vc4_hvs_debugfs_underrun(struct seq_file *m, void *data)
@@ -135,6 +142,11 @@ static int vc4_hvs_upload_linear_kernel(struct vc4_hvs *hvs,
 	int ret, i;
 	u32 __iomem *dst_kernel;
 
+	/*
+	 * NOTE: We don't need a call to drm_dev_enter()/drm_dev_exit()
+	 * here since that function is only called from vc4_hvs_bind().
+	 */
+
 	ret = drm_mm_insert_node(&hvs->dlist_mm, space, VC4_KERNEL_DWORDS);
 	if (ret) {
 		DRM_ERROR("Failed to allocate space for filter kernel: %d\n",
@@ -159,10 +171,15 @@ static int vc4_hvs_upload_linear_kernel(struct vc4_hvs *hvs,
 static void vc4_hvs_lut_load(struct vc4_hvs *hvs,
 			     struct vc4_crtc *vc4_crtc)
 {
+	struct drm_device *drm = &hvs->vc4->base;
 	struct drm_crtc *crtc = &vc4_crtc->base;
 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
+	int idx;
 	u32 i;
 
+	if (!drm_dev_enter(drm, &idx))
+		return;
+
 	/* The LUT memory is laid out with each HVS channel in order,
 	 * each of which takes 256 writes for R, 256 for G, then 256
 	 * for B.
@@ -177,6 +194,8 @@ static void vc4_hvs_lut_load(struct vc4_hvs *hvs,
 		HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_g[i]);
 	for (i = 0; i < crtc->gamma_size; i++)
 		HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_b[i]);
+
+	drm_dev_exit(idx);
 }
 
 static void vc4_hvs_update_gamma_lut(struct vc4_hvs *hvs,
@@ -198,7 +217,12 @@ static void vc4_hvs_update_gamma_lut(struct vc4_hvs *hvs,
 
 u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo)
 {
+	struct drm_device *drm = &hvs->vc4->base;
 	u8 field = 0;
+	int idx;
+
+	if (!drm_dev_enter(drm, &idx))
+		return 0;
 
 	switch (fifo) {
 	case 0:
@@ -215,6 +239,7 @@ u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo)
 		break;
 	}
 
+	drm_dev_exit(idx);
 	return field;
 }
 
@@ -227,6 +252,12 @@ int vc4_hvs_get_fifo_from_output(struct vc4_hvs *hvs, unsigned int output)
 	if (!vc4->is_vc5)
 		return output;
 
+	/*
+	 * NOTE: We should probably use drm_dev_enter()/drm_dev_exit()
+	 * here, but this function is only used during the DRM device
+	 * initialization, so we should be fine.
+	 */
+
 	switch (output) {
 	case 0:
 		return 0;
@@ -275,12 +306,17 @@ static int vc4_hvs_init_channel(struct vc4_hvs *hvs, struct drm_crtc *crtc,
 				struct drm_display_mode *mode, bool oneshot)
 {
 	struct vc4_dev *vc4 = hvs->vc4;
+	struct drm_device *drm = &vc4->base;
 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
 	struct vc4_crtc_state *vc4_crtc_state = to_vc4_crtc_state(crtc->state);
 	unsigned int chan = vc4_crtc_state->assigned_channel;
 	bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
 	u32 dispbkgndx;
 	u32 dispctrl;
+	int idx;
+
+	if (!drm_dev_enter(drm, &idx))
+		return -ENODEV;
 
 	HVS_WRITE(SCALER_DISPCTRLX(chan), 0);
 	HVS_WRITE(SCALER_DISPCTRLX(chan), SCALER_DISPCTRLX_RESET);
@@ -322,13 +358,21 @@ static int vc4_hvs_init_channel(struct vc4_hvs *hvs, struct drm_crtc *crtc,
 	 */
 	vc4_hvs_lut_load(hvs, vc4_crtc);
 
+	drm_dev_exit(idx);
+
 	return 0;
 }
 
 void vc4_hvs_stop_channel(struct vc4_hvs *hvs, unsigned int chan)
 {
+	struct drm_device *drm = &hvs->vc4->base;
+	int idx;
+
+	if (!drm_dev_enter(drm, &idx))
+		return;
+
 	if (HVS_READ(SCALER_DISPCTRLX(chan)) & SCALER_DISPCTRLX_ENABLE)
-		return;
+		goto out;
 
 	HVS_WRITE(SCALER_DISPCTRLX(chan),
 		  HVS_READ(SCALER_DISPCTRLX(chan)) | SCALER_DISPCTRLX_RESET);
@@ -345,6 +389,9 @@ void vc4_hvs_stop_channel(struct vc4_hvs *hvs, unsigned int chan)
 	WARN_ON_ONCE((HVS_READ(SCALER_DISPSTATX(chan)) &
 		      (SCALER_DISPSTATX_FULL | SCALER_DISPSTATX_EMPTY)) !=
 		     SCALER_DISPSTATX_EMPTY);
+
+out:
+	drm_dev_exit(idx);
 }
 
 int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
@@ -386,9 +433,15 @@ static void vc4_hvs_install_dlist(struct drm_crtc *crtc)
 	struct vc4_dev *vc4 = to_vc4_dev(dev);
 	struct vc4_hvs *hvs = vc4->hvs;
 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx))
+		return;
 
 	HVS_WRITE(SCALER_DISPLISTX(vc4_state->assigned_channel),
 		  vc4_state->mm.start);
+
+	drm_dev_exit(idx);
 }
 
 static void vc4_hvs_update_dlist(struct drm_crtc *crtc)
@@ -473,6 +526,12 @@ void vc4_hvs_atomic_flush(struct drm_crtc *crtc,
 	bool enable_bg_fill = false;
 	u32 __iomem *dlist_start = vc4->hvs->dlist + vc4_state->mm.start;
 	u32 __iomem *dlist_next = dlist_start;
+	int idx;
+
+	if (!drm_dev_enter(dev, &idx)) {
+		vc4_crtc_send_vblank(crtc);
+		return;
+	}
 
 	if (debug_dump_regs) {
 		DRM_INFO("CRTC %d HVS before:\n", drm_crtc_index(crtc));
@@ -543,26 +602,44 @@ void vc4_hvs_atomic_flush(struct drm_crtc *crtc,
 		DRM_INFO("CRTC %d HVS after:\n", drm_crtc_index(crtc));
 		vc4_hvs_dump_state(hvs);
 	}
+
+	drm_dev_exit(idx);
 }
 
 void vc4_hvs_mask_underrun(struct vc4_hvs *hvs, int channel)
 {
-	u32 dispctrl = HVS_READ(SCALER_DISPCTRL);
+	struct drm_device *drm = &hvs->vc4->base;
+	u32 dispctrl;
+	int idx;
 
+	if (!drm_dev_enter(drm, &idx))
+		return;
+
+	dispctrl = HVS_READ(SCALER_DISPCTRL);
 	dispctrl &= ~SCALER_DISPCTRL_DSPEISLUR(channel);
 
 	HVS_WRITE(SCALER_DISPCTRL, dispctrl);
+
+	drm_dev_exit(idx);
 }
 
 void vc4_hvs_unmask_underrun(struct vc4_hvs *hvs, int channel)
 {
-	u32 dispctrl = HVS_READ(SCALER_DISPCTRL);
+	struct drm_device *drm = &hvs->vc4->base;
+	u32 dispctrl;
+	int idx;
 
+	if (!drm_dev_enter(drm, &idx))
+		return;
+
+	dispctrl = HVS_READ(SCALER_DISPCTRL);
 	dispctrl |= SCALER_DISPCTRL_DSPEISLUR(channel);
 
 	HVS_WRITE(SCALER_DISPSTAT,
 		  SCALER_DISPSTAT_EUFLOW(channel));
 	HVS_WRITE(SCALER_DISPCTRL, dispctrl);
+
+	drm_dev_exit(idx);
 }
 
 static void vc4_hvs_report_underrun(struct drm_device *dev)
@@ -583,6 +660,17 @@ static irqreturn_t vc4_hvs_irq_handler(int irq, void *data)
 	u32 control;
 	u32 status;
 
+	/*
+	 * NOTE: We don't need to protect the register access using
+	 * drm_dev_enter() there because the interrupt handler lifetime
+	 * is tied to the device itself, and not to the DRM device.
+	 *
+	 * So when the device will be gone, one of the first thing we
+	 * will be doing will be to unregister the interrupt handler,
+	 * and then unregister the DRM device. drm_dev_enter() would
+	 * thus always succeed if we are here.
+	 */
+
 	status = HVS_READ(SCALER_DISPSTAT);
 	control = HVS_READ(SCALER_DISPCTRL);
 
@@ -615,10 +703,9 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
 	u32 dispctrl;
 	u32 reg;
 
-	hvs = devm_kzalloc(&pdev->dev, sizeof(*hvs), GFP_KERNEL);
+	hvs = drmm_kzalloc(drm, sizeof(*hvs), GFP_KERNEL);
 	if (!hvs)
 		return -ENOMEM;
-
 	hvs->vc4 = vc4;
 	hvs->pdev = pdev;
 
-- 
2.36.1


  parent reply	other threads:[~2022-06-22 14:33 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-22 14:31 [PATCH v2 00/68] drm/vc4: Fix hotplug for vc4 Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 01/68] drm/mipi-dsi: Detach devices when removing the host Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 02/68] drm/crtc: Introduce drmm_crtc_init_with_planes Maxime Ripard
2022-06-22 18:37   ` kernel test robot
2022-06-28  8:54   ` Thomas Zimmermann
2022-06-22 14:31 ` [PATCH v2 03/68] drm/encoder: Introduce drmm_encoder_init Maxime Ripard
2022-06-22 19:38   ` kernel test robot
2022-06-28  8:55   ` Thomas Zimmermann
2022-06-22 14:31 ` [PATCH v2 04/68] drm/connector: Reorder headers Maxime Ripard
2022-06-24 19:21   ` Sam Ravnborg
2022-06-22 14:31 ` [PATCH v2 05/68] drm/connector: Mention the cleanup after drm_connector_init Maxime Ripard
2022-06-24 19:25   ` Sam Ravnborg
2022-06-22 14:31 ` [PATCH v2 06/68] drm/connector: Clarify when drm_connector_unregister is needed Maxime Ripard
2022-06-24 19:25   ` Sam Ravnborg
2022-06-22 14:31 ` [PATCH v2 07/68] drm/connector: Introduce drmm_connector_init Maxime Ripard
2022-06-24 19:26   ` Sam Ravnborg
2022-06-22 14:31 ` [PATCH v2 08/68] drm/connector: Introduce drmm_connector_init_with_ddc Maxime Ripard
2022-06-24 19:26   ` Sam Ravnborg
2022-06-28  9:00   ` Thomas Zimmermann
2022-06-28 14:05     ` Jani Nikula
2022-06-22 14:31 ` [PATCH v2 09/68] drm/bridge: panel: Introduce drmm_panel_bridge_add Maxime Ripard
2022-06-24 19:27   ` Sam Ravnborg
2022-06-22 14:31 ` [PATCH v2 10/68] drm/bridge: panel: Introduce drmm_of_get_bridge Maxime Ripard
2022-06-24 19:31   ` Sam Ravnborg
2022-06-22 14:31 ` [PATCH v2 11/68] drm/vc4: drv: Call component_unbind_all() Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 12/68] drm/vc4: drv: Use drm_dev_unplug Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 13/68] drm/vc4: crtc: Create vblank reporting function Maxime Ripard
2022-06-22 14:31 ` Maxime Ripard [this message]
2022-06-22 14:31 ` [PATCH v2 15/68] drm/vc4: hvs: Remove planes currently allocated before taking down Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 16/68] drm/vc4: plane: Take possible_crtcs as an argument Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 17/68] drm/vc4: crtc: Remove manual plane removal on error Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 18/68] drm/vc4: plane: Switch to drmm_universal_plane_alloc() Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 19/68] drm/vc4: crtc: Move debugfs_name to crtc_data Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 20/68] drm/vc4: crtc: Switch to drmm_kzalloc Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 21/68] drm/vc4: crtc: Switch to DRM-managed CRTC initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 22/68] drm/vc4: dpi: Remove vc4_dev dpi pointer Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 23/68] drm/vc4: dpi: Embed DRM structures into the private structure Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 24/68] drm/vc4: dpi: Switch to drmm_kzalloc Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 25/68] drm/vc4: dpi: Return an error if we can't enable our clock Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 26/68] drm/vc4: dpi: Remove unnecessary drm_of_panel_bridge_remove call Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 27/68] drm/vc4: dpi: Add action to disable the clock Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 28/68] drm/vc4: dpi: Switch to DRM-managed encoder initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 29/68] drm/vc4: dpi: Switch to drmm_of_get_bridge Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 30/68] drm/vc4: dpi: Protect device resources Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 31/68] drm/vc4: dsi: Embed DRM structures into the private structure Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 32/68] drm/vc4: dsi: Switch to DRM-managed encoder initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 33/68] drm/vc4: dsi: Switch to drmm_of_get_bridge Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 34/68] drm/vc4: dsi: Fix the driver structure lifetime Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 35/68] drm/vc4: dsi: Switch to devm_pm_runtime_enable Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 36/68] drm/vc4: hdmi: Switch to drmm_kzalloc Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 37/68] drm/vc4: hdmi: Remove call to drm_connector_unregister() Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 38/68] drm/vc4: hdmi: Switch to DRM-managed encoder initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 39/68] drm/vc4: hdmi: Switch to DRM-managed connector initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 40/68] drm/vc4: hdmi: Switch to device-managed ALSA initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 41/68] drm/vc4: hdmi: Switch to device-managed CEC initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 42/68] drm/vc4: hdmi: Use a device-managed action for DDC Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 43/68] drm/vc4: hdmi: Switch to DRM-managed kfree to build regsets Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 44/68] drm/vc4: hdmi: Use devm to register hotplug interrupts Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 45/68] drm/vc4: hdmi: Move audio structure offset checks Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 46/68] drm/vc4: hdmi: Protect device resources after removal Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 47/68] drm/vc4: hdmi: Switch to devm_pm_runtime_enable Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 48/68] drm/vc4: txp: Remove vc4_dev txp pointer Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 49/68] drm/vc4: txp: Remove duplicate regset Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 50/68] drm/vc4: txp: Switch to drmm_kzalloc Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 51/68] drm/vc4: txp: Remove call to drm_connector_unregister() Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 52/68] drm/vc4: txp: Protect device resources Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 53/68] drm/vc4: vec: Remove vc4_dev vec pointer Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 54/68] drm/vc4: vec: Embed DRM structures into the private structure Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 55/68] drm/vc4: vec: Switch to drmm_kzalloc Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 56/68] drm/vc4: vec: Remove call to drm_connector_unregister() Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 57/68] drm/vc4: vec: Switch to DRM-managed encoder initialization Maxime Ripard
2022-06-22 14:31 ` [PATCH v2 58/68] drm/vc4: vec: Switch to DRM-managed connector initialization Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 59/68] drm/vc4: vec: Protect device resources after removal Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 60/68] drm/vc4: vec: Switch to devm_pm_runtime_enable Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 61/68] drm/vc4: debugfs: Protect device resources Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 62/68] drm/vc4: debugfs: Return an error on failure Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 63/68] drm/vc4: debugfs: Simplify debugfs registration Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 64/68] drm/vc4: Switch to drmm_mutex_init Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 65/68] drm/vc4: perfmon: Add missing mutex_destroy Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 66/68] drm/vc4: v3d: Stop disabling interrupts Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 67/68] drm/vc4: v3d: Rework the runtime_pm setup Maxime Ripard
2022-06-22 14:32 ` [PATCH v2 68/68] drm/vc4: v3d: Switch to devm_pm_runtime_enable Maxime Ripard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220622143209.600298-15-maxime@cerno.tech \
    --to=maxime@cerno.tech \
    --cc=airlied@linux.ie \
    --cc=daniel.vetter@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox