drm/sysfs: Grab lock for edid/modes_show
diff mbox

Message ID 1443775171-31600-1-git-send-email-daniel.vetter@ffwll.ch
State New
Headers show

Commit Message

Daniel Vetter Oct. 2, 2015, 8:39 a.m. UTC
We chase pointers/lists without taking the locks protecting them,
which isn't that good.

Fix it.

v2: Actually unlock properly, spotted by Julia.

Cc: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
---
 drivers/gpu/drm/drm_sysfs.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Julia Lawall Oct. 2, 2015, 3:02 p.m. UTC | #1
On Fri, 2 Oct 2015, Daniel Vetter wrote:

> We chase pointers/lists without taking the locks protecting them,
> which isn't that good.
>
> Fix it.
>
> v2: Actually unlock properly, spotted by Julia.

The unlock is still on top of the unlock label?

julia

>
> Cc: Julia Lawall <julia.lawall@lip6.fr>
> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
> ---
>  drivers/gpu/drm/drm_sysfs.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_sysfs.c b/drivers/gpu/drm/drm_sysfs.c
> index 7506de0a75b4..9bffa63fe849 100644
> --- a/drivers/gpu/drm/drm_sysfs.c
> +++ b/drivers/gpu/drm/drm_sysfs.c
> @@ -261,23 +261,29 @@ static ssize_t edid_show(struct file *filp, struct kobject *kobj,
>  	struct drm_connector *connector = to_drm_connector(connector_dev);
>  	unsigned char *edid;
>  	size_t size;
> +	ssize_t ret = 0;
>
> +	mutex_lock(&connector->dev->mode_config.mutex);
>  	if (!connector->edid_blob_ptr)
> -		return 0;
> +		goto unlock;
>
>  	edid = connector->edid_blob_ptr->data;
>  	size = connector->edid_blob_ptr->length;
>  	if (!edid)
> -		return 0;
> +		goto unlock;
>
>  	if (off >= size)
> -		return 0;
> +		goto unlock;
>
>  	if (off + count > size)
>  		count = size - off;
>  	memcpy(buf, edid + off, count);
>
> -	return count;
> +	ret = count;
> +	mutex_unlock(&connector->dev->mode_config.mutex);
> +unlock:
> +
> +	return ret;
>  }
>
>  static ssize_t modes_show(struct device *device,
> @@ -288,10 +294,12 @@ static ssize_t modes_show(struct device *device,
>  	struct drm_display_mode *mode;
>  	int written = 0;
>
> +	mutex_lock(&connector->dev->mode_config.mutex);
>  	list_for_each_entry(mode, &connector->modes, head) {
>  		written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
>  				    mode->name);
>  	}
> +	mutex_unlock(&connector->dev->mode_config.mutex);
>
>  	return written;
>  }
> --
> 2.5.1
>
>

Patch
diff mbox

diff --git a/drivers/gpu/drm/drm_sysfs.c b/drivers/gpu/drm/drm_sysfs.c
index 7506de0a75b4..9bffa63fe849 100644
--- a/drivers/gpu/drm/drm_sysfs.c
+++ b/drivers/gpu/drm/drm_sysfs.c
@@ -261,23 +261,29 @@  static ssize_t edid_show(struct file *filp, struct kobject *kobj,
 	struct drm_connector *connector = to_drm_connector(connector_dev);
 	unsigned char *edid;
 	size_t size;
+	ssize_t ret = 0;
 
+	mutex_lock(&connector->dev->mode_config.mutex);
 	if (!connector->edid_blob_ptr)
-		return 0;
+		goto unlock;
 
 	edid = connector->edid_blob_ptr->data;
 	size = connector->edid_blob_ptr->length;
 	if (!edid)
-		return 0;
+		goto unlock;
 
 	if (off >= size)
-		return 0;
+		goto unlock;
 
 	if (off + count > size)
 		count = size - off;
 	memcpy(buf, edid + off, count);
 
-	return count;
+	ret = count;
+	mutex_unlock(&connector->dev->mode_config.mutex);
+unlock:
+
+	return ret;
 }
 
 static ssize_t modes_show(struct device *device,
@@ -288,10 +294,12 @@  static ssize_t modes_show(struct device *device,
 	struct drm_display_mode *mode;
 	int written = 0;
 
+	mutex_lock(&connector->dev->mode_config.mutex);
 	list_for_each_entry(mode, &connector->modes, head) {
 		written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
 				    mode->name);
 	}
+	mutex_unlock(&connector->dev->mode_config.mutex);
 
 	return written;
 }