Skip to contentSkip to conversation

drm/{ast, mgag200}: Protect I/O regs against concurrent access

PATCH In review Thomas Zimmermann 3 participants 6 messages May 2, 2022 – May 4, 2022

Subsystems (4)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

cover drm/{ast, mgag200}: Protect I/O regs against concurrent access

Protect access to I/O registers in ast and mgag200 via lock. Commit- tail functions and get-modes operations use the same registers and can interfere with each other. This can result in failed mode-setting operations.

As both drivers use fully managed cleanup, the patchset adds a new helper that initializes a mutex with auto-cleanup.

Thomas Zimmermann (3):
drm: Add DRM-managed mutex_init()
drm/ast: Protect concurrent access to I/O registers with lock drm/mgag200: Protect concurrent access to I/O registers with lock

 drivers/gpu/drm/ast/ast_drv.h          |  1 +
 drivers/gpu/drm/ast/ast_main.c         |  4 +++
 drivers/gpu/drm/ast/ast_mode.c         | 48 ++++++++++++++++++++++++--
 drivers/gpu/drm/drm_managed.c          | 27 +++++++++++++++
 drivers/gpu/drm/mgag200/mgag200_drv.c  |  6 ++++
 drivers/gpu/drm/mgag200/mgag200_drv.h  |  1 +
 drivers/gpu/drm/mgag200/mgag200_mode.c | 14 ++++++++
 include/drm/drm_managed.h              |  3 ++
 8 files changed, 101 insertions(+), 3 deletions(-)

base-commit: 3ae2e00290c290713e21118220a817a24b44d39f
prerequisite-patch-id: c2b2f08f0eccc9f5df0c0da49fa1d36267deb11d
prerequisite-patch-id: c67e5d886a47b7d0266d81100837557fda34cb24

Signature · 1 line
2.36.0
author maintainer
#1 lore raw

PATCH 1/3 drm: Add DRM-managed mutex_init()

Add drmm_mutex_init(), a helper that provides managed mutex cleanup. The mutex will be destroyed with the final reference of the DRM device.

---
 drivers/gpu/drm/drm_managed.c | 27 +++++++++++++++++++++++++++
 include/drm/drm_managed.h     |  3 +++
 2 files changed, 30 insertions(+)
drivers/gpu/drm/drm_managed.c +27 -0
index 37d7db6223be6..4cf214de50c40 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_managed.h>
 
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 
@@ -262,3 +263,29 @@ void drmm_kfree(struct drm_device *dev, void *data)
 	free_dr(dr_match);
 }
 EXPORT_SYMBOL(drmm_kfree);
+
+static void drmm_mutex_release(struct drm_device *dev, void *res)
+{
+	struct mutex *lock = res;
+
+	mutex_destroy(lock);
+}
+
+/**
+ * drmm_mutex_init - &drm_device-managed mutex_init()
+ * @dev: DRM device
+ * @lock: lock to be initialized
+ *
+ * Returns:
+ * 0 on success, or a negative errno code otherwise.
+ *
+ * This is a &drm_device-managed version of mutex_init(). The initialized
+ * lock is automatically destroyed on the final drm_dev_put().
+ */
+int drmm_mutex_init(struct drm_device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+
+	return drmm_add_action_or_reset(dev, drmm_mutex_release, lock);
+}
+EXPORT_SYMBOL(drmm_mutex_init);
include/drm/drm_managed.h +3 -0
index b45c6fbf53ac4..359883942612e 100644
@@ -8,6 +8,7 @@
 #include <linux/types.h>
 
 struct drm_device;
+struct mutex;
 
 typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
 
@@ -104,4 +105,6 @@ char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
 
 void drmm_kfree(struct drm_device *dev, void *data);
 
+int drmm_mutex_init(struct drm_device *dev, struct mutex *lock);
+
 #endif
Signature · 1 line
2.36.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] drm: Add DRM-managed mutex_init()

Quoted 4 lines · Thomas Zimmermann

Add drmm_mutex_init(), a helper that provides managed mutex cleanup. The mutex will be destroyed with the final reference of the DRM device.

the other two driver patches.
-Daniel

Quoted 69 lines
---
 drivers/gpu/drm/drm_managed.c | 27 +++++++++++++++++++++++++++
 include/drm/drm_managed.h     |  3 +++
 2 files changed, 30 insertions(+)
drivers/gpu/drm/drm_managed.c +27 -0
index 37d7db6223be6..4cf214de50c40 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_managed.h>
 
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 
@@ -262,3 +263,29 @@ void drmm_kfree(struct drm_device *dev, void *data)
 	free_dr(dr_match);
 }
 EXPORT_SYMBOL(drmm_kfree);
+
+static void drmm_mutex_release(struct drm_device *dev, void *res)
+{
+	struct mutex *lock = res;
+
+	mutex_destroy(lock);
+}
+
+/**
+ * drmm_mutex_init - &drm_device-managed mutex_init()
+ * @dev: DRM device
+ * @lock: lock to be initialized
+ *
+ * Returns:
+ * 0 on success, or a negative errno code otherwise.
+ *
+ * This is a &drm_device-managed version of mutex_init(). The initialized
+ * lock is automatically destroyed on the final drm_dev_put().
+ */
+int drmm_mutex_init(struct drm_device *dev, struct mutex *lock)
+{
+	mutex_init(lock);
+
+	return drmm_add_action_or_reset(dev, drmm_mutex_release, lock);
+}
+EXPORT_SYMBOL(drmm_mutex_init);
include/drm/drm_managed.h +3 -0
index b45c6fbf53ac4..359883942612e 100644
@@ -8,6 +8,7 @@
 #include <linux/types.h>
 
 struct drm_device;
+struct mutex;
 
 typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
 
@@ -104,4 +105,6 @@ char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
 
 void drmm_kfree(struct drm_device *dev, void *data);
 
+int drmm_mutex_init(struct drm_device *dev, struct mutex *lock);
+
 #endif
Signature · 1 line
2.36.0
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
author maintainer
#3 lore raw

PATCH 2/3 drm/ast: Protect concurrent access to I/O registers with lock

In reply to Thomas Zimmermann

Add a mutex lock to protect concurrent access to I/O registers against each other. This happens between invokataion of commit- tail functions and get-mode operations. Both with use the CRTC index register AST_IO_CRTC_PORT. Concurrent access can lead to failed mode-setting operations.

---
 drivers/gpu/drm/ast/ast_drv.h  |  1 +
 drivers/gpu/drm/ast/ast_main.c |  4 +++
 drivers/gpu/drm/ast/ast_mode.c | 48 +++++++++++++++++++++++++++++++---
 3 files changed, 50 insertions(+), 3 deletions(-)
drivers/gpu/drm/ast/ast_drv.h +1 -0
index a19315b2f7e56..43cedd767f8f1 100644
@@ -158,6 +158,7 @@ to_ast_sil164_connector(struct drm_connector *connector)
 struct ast_private {
 	struct drm_device base;
 
+	struct mutex ioregs_lock; /* Protects access to I/O registers in ioregs */
 	void __iomem *regs;
 	void __iomem *ioregs;
 	void __iomem *dp501_fw_buf;
drivers/gpu/drm/ast/ast_main.c +4 -0
index 22e9e2d3c71ab..5ad473aeaea2b 100644
@@ -420,6 +420,10 @@ struct ast_private *ast_device_create(const struct drm_driver *drv,
 
 	pci_set_drvdata(pdev, dev);
 
+	ret = drmm_mutex_init(dev, &ast->ioregs_lock);
+	if (ret)
+		return ERR_PTR(ret);
+
 	ast->regs = pcim_iomap(pdev, 1, 0);
 	if (!ast->regs)
 		return ERR_PTR(-EIO);
drivers/gpu/drm/ast/ast_mode.c +45 -3

112 lines not on this page · raw message

Signature · 1 line
2.36.0
author maintainer
#4 lore raw

PATCH 3/3 drm/mgag200: Protect concurrent access to I/O registers with lock

In reply to Thomas Zimmermann

Add a mutex lock to protect concurrent access to I/O registers against each other. This happens between invokataion of commit- tail functions and get-mode operations. Both with use the CRTC index registers MGA1064_GEN_IO_DATA and MGA1064_GEN_IO_CTL. Concurrent access can lead to failed mode-setting operations.

---
 drivers/gpu/drm/mgag200/mgag200_drv.c  |  6 ++++++
 drivers/gpu/drm/mgag200/mgag200_drv.h  |  1 +
 drivers/gpu/drm/mgag200/mgag200_mode.c | 14 ++++++++++++++
 3 files changed, 21 insertions(+)
drivers/gpu/drm/mgag200/mgag200_drv.c +6 -0
index 217844d71ab5c..08839460606fe 100644
@@ -14,6 +14,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_file.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_managed.h>
 #include <drm/drm_module.h>
 #include <drm/drm_pciids.h>
 
@@ -65,6 +66,11 @@ static int mgag200_regs_init(struct mga_device *mdev)
 	struct pci_dev *pdev = to_pci_dev(dev->dev);
 	u32 option, option2;
 	u8 crtcext3;
+	int ret;
+
+	ret = drmm_mutex_init(dev, &mdev->rmmio_lock);
+	if (ret)
+		return ret;
 
 	switch (mdev->type) {
 	case G200_PCI:
drivers/gpu/drm/mgag200/mgag200_drv.h +1 -0
index 4368112023f7c..18829eb8398a0 100644
@@ -213,6 +213,7 @@ struct mga_device {
 	struct drm_device		base;
 	unsigned long			flags;
 
+	struct mutex			rmmio_lock;
 	resource_size_t			rmmio_base;
 	resource_size_t			rmmio_size;
 	void __iomem			*rmmio;
drivers/gpu/drm/mgag200/mgag200_mode.c +14 -0
index 6e18d3bbd7207..abde7655477db 100644
@@ -881,6 +881,14 @@ mgag200_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
 		.y2 = fb->height,
 	};
 
+	/*
+	 * Concurrent operations could possibly trigger a call to
+	 * drm_connector_helper_funcs.get_modes by trying to read the
+	 * display modes. Protect access to I/O registers by acquiring
+	 * the I/O-register lock.
+	 */
+	mutex_lock(&mdev->rmmio_lock);
+
 	if (mdev->type == G200_WB || mdev->type == G200_EW3)
 		mgag200_g200wb_hold_bmc(mdev);
 
@@ -904,6 +912,8 @@ mgag200_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
 	mgag200_enable_display(mdev);
 
 	mgag200_handle_damage(mdev, fb, &fullscreen, &shadow_plane_state->data[0]);
+
+	mutex_unlock(&mdev->rmmio_lock);
 }
 
 static void
@@ -963,8 +973,12 @@ mgag200_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
 	if (!fb)
 		return;
 
+	mutex_lock(&mdev->rmmio_lock);
+
 	if (drm_atomic_helper_damage_merged(old_state, state, &damage))
 		mgag200_handle_damage(mdev, fb, &damage, &shadow_plane_state->data[0]);
+
+	mutex_unlock(&mdev->rmmio_lock);
 }
 
 static struct drm_crtc_state *
Signature · 1 line
2.36.0
reviewer Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] drm/mgag200: Protect concurrent access to I/O registers with lock

Quoted 2 lines · Thomas Zimmermann

Add a mutex lock to protect concurrent access to I/O registers against each other. This happens between invokataion of commit-

Typo in commit message, invokataion => invocation

Quoted 5 lines

tail functions and get-mode operations. Both with use the CRTC index registers MGA1064_GEN_IO_DATA and MGA1064_GEN_IO_CTL. Concurrent access can lead to failed mode-setting operations.

There is a trivial conflict with https://lists.freedesktop.org/archives/dri-devel/2022-April/352966.html
But I will need to send a v2 for it anyway.

It looks good to me,

Quoted 83 lines
---
  drivers/gpu/drm/mgag200/mgag200_drv.c  |  6 ++++++
  drivers/gpu/drm/mgag200/mgag200_drv.h  |  1 +
  drivers/gpu/drm/mgag200/mgag200_mode.c | 14 ++++++++++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/mgag200/mgag200_drv.c +6 -0
index 217844d71ab5c..08839460606fe 100644
@@ -14,6 +14,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_file.h>
  #include <drm/drm_ioctl.h>
+#include <drm/drm_managed.h>
  #include <drm/drm_module.h>
  #include <drm/drm_pciids.h>
 
@@ -65,6 +66,11 @@ static int mgag200_regs_init(struct mga_device *mdev)
  	struct pci_dev *pdev = to_pci_dev(dev->dev);
  	u32 option, option2;
  	u8 crtcext3;
+	int ret;
+
+	ret = drmm_mutex_init(dev, &mdev->rmmio_lock);
+	if (ret)
+		return ret;
 
  	switch (mdev->type) {
  	case G200_PCI:
drivers/gpu/drm/mgag200/mgag200_drv.h +1 -0
index 4368112023f7c..18829eb8398a0 100644
@@ -213,6 +213,7 @@ struct mga_device {
  	struct drm_device		base;
  	unsigned long			flags;
 
+	struct mutex			rmmio_lock;
  	resource_size_t			rmmio_base;
  	resource_size_t			rmmio_size;
  	void __iomem			*rmmio;
drivers/gpu/drm/mgag200/mgag200_mode.c +14 -0
index 6e18d3bbd7207..abde7655477db 100644
@@ -881,6 +881,14 @@ mgag200_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
  		.y2 = fb->height,
  	};
 
+	/*
+	 * Concurrent operations could possibly trigger a call to
+	 * drm_connector_helper_funcs.get_modes by trying to read the
+	 * display modes. Protect access to I/O registers by acquiring
+	 * the I/O-register lock.
+	 */
+	mutex_lock(&mdev->rmmio_lock);
+
  	if (mdev->type == G200_WB || mdev->type == G200_EW3)
  		mgag200_g200wb_hold_bmc(mdev);
 
@@ -904,6 +912,8 @@ mgag200_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
  	mgag200_enable_display(mdev);
 
  	mgag200_handle_damage(mdev, fb, &fullscreen, &shadow_plane_state->data[0]);
+
+	mutex_unlock(&mdev->rmmio_lock);
  }
 
  static void
@@ -963,8 +973,12 @@ mgag200_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
  	if (!fb)
  		return;
 
+	mutex_lock(&mdev->rmmio_lock);
+
  	if (drm_atomic_helper_damage_merged(old_state, state, &damage))
  		mgag200_handle_damage(mdev, fb, &damage, &shadow_plane_state->data[0]);
+
+	mutex_unlock(&mdev->rmmio_lock);
  }
 
  static struct drm_crtc_state *