Skip to contentSkip to conversation

drm: damage_helper: Fix race checking plane->state->fb

PATCH In review Sean Paul 3 participants 5 messages Sep 4, 2019 – Oct 8, 2019

Subsystems (3)
lore
Share & tools
lore

Sean Paul

author
#0 lore raw

PATCH 1/1 drm: damage_helper: Fix race checking plane->state->fb

From: Sean Paul <[email protected]>

Since the dirtyfb ioctl doesn't give us any hints as to which plane is scanning out the fb it's marking as damaged, we need to loop through planes to find it.

Currently we just reach into plane state and check, but that can race with another commit changing the fb out from under us. This patch locks the plane before checking the fb and will release the lock if the plane is not displaying the dirty fb.

Fixes b9fc5e01d1ce ("drm: Add helper to implement legacy dirtyfb")
Cc Rob Clark <[email protected]>
Cc Deepak Rawat <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Thomas Hellstrom <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Sean Paul <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc <[email protected]> # v5.0+
---
 drivers/gpu/drm/drm_damage_helper.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_damage_helper.c +7 -1
index 8230dac01a89..3a4126dc2520 100644
@@ -212,8 +212,14 @@ int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
 	drm_for_each_plane(plane, fb->dev) {
 		struct drm_plane_state *plane_state;
 
-		if (plane->state->fb != fb)
+		ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
+		if (ret)
+			goto out;
+
+		if (plane->state->fb != fb) {
+			drm_modeset_unlock(&plane->mutex);
 			continue;
+		}
 
 		plane_state = drm_atomic_get_plane_state(state, plane);
 		if (IS_ERR(plane_state)) {
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
#1 lore raw
Quoted 35 lines · Sean Paul

From: Sean Paul <[email protected]>

Since the dirtyfb ioctl doesn't give us any hints as to which plane is scanning out the fb it's marking as damaged, we need to loop through planes to find it.

Currently we just reach into plane state and check, but that can race with another commit changing the fb out from under us. This patch locks the plane before checking the fb and will release the lock if the plane is not displaying the dirty fb.

Fixes b9fc5e01d1ce ("drm: Add helper to implement legacy dirtyfb")
Cc Rob Clark <[email protected]>
Cc Deepak Rawat <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Thomas Hellstrom <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Sean Paul <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc <[email protected]> # v5.0+
---
 drivers/gpu/drm/drm_damage_helper.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_damage_helper.c +0 -0
index 8230dac01a89..3a4126dc2520 100644

drivers/gpu/drm/drm_damage_helper.c int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb, · line 216

Quoted 6 lines
drivers/gpu/drm/drm_damage_helper.c +2 -1
@@ -212,8 +212,14 @@ int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
        drm_for_each_plane(plane, fb->dev) {
                struct drm_plane_state *plane_state;
 
-               if (plane->state->fb != fb)
+               ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
+               if (ret)

I think for paranoid safety we should have a WARN_ON(ret == -EALREADY) here. It should be impossible, but if it's not for some oddball reason, we'll blow up.

With that: Reviewed-by: Daniel Vetter <[email protected]>

But please give this a spin with some workloads and the ww_mutex slowpath debugging enabled, just to makre sure. -Daniel

Quoted 12 lines
drivers/gpu/drm/drm_damage_helper.c +5 -1
+                       goto out;
+
+               if (plane->state->fb != fb) {
+                       drm_modeset_unlock(&plane->mutex);
                        continue;
+               }
 
                plane_state = drm_atomic_get_plane_state(state, plane);
                if (IS_ERR(plane_state)) {
--

Sean Paul, Software Engineer, Google / Chromium OS

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
author
#2 lore raw
Quoted 39 lines · Daniel Vetter
Quoted 38 lines · Sean Paul

From: Sean Paul <[email protected]>

Since the dirtyfb ioctl doesn't give us any hints as to which plane is scanning out the fb it's marking as damaged, we need to loop through planes to find it.

Currently we just reach into plane state and check, but that can race with another commit changing the fb out from under us. This patch locks the plane before checking the fb and will release the lock if the plane is not displaying the dirty fb.

Fixes b9fc5e01d1ce ("drm: Add helper to implement legacy dirtyfb")
Cc Rob Clark <[email protected]>
Cc Deepak Rawat <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Thomas Hellstrom <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Sean Paul <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc <[email protected]> # v5.0+
---
 drivers/gpu/drm/drm_damage_helper.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_damage_helper.c +0 -0
index 8230dac01a89..3a4126dc2520 100644
@@ -212,8 +212,14 @@ int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
        drm_for_each_plane(plane, fb->dev) {
                struct drm_plane_state *plane_state;
 
Quoted 7 lines

drivers/gpu/drm/drm_damage_helper.c int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb, · line 216

Quoted 3 lines
drivers/gpu/drm/drm_damage_helper.c +2 -1
-               if (plane->state->fb != fb)
+               ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
+               if (ret)

I think for paranoid safety we should have a WARN_ON(ret == -EALREADY) here. It should be impossible, but if it's not for some oddball reason, we'll blow up.

drm_modeset_lock eats EALREADY and returns 0 for that case, so I guess it depends _how_ paranoid you want to be here :-)

Quoted 5 lines

With that: Reviewed-by: Daniel Vetter <[email protected]>

But please give this a spin with some workloads and the ww_mutex slowpath debugging enabled, just to makre sure.

Ok, had a chance to run through some tests this morning with CONFIG_DEBUG_WW_MUTEX_SLOWPATH and things lgtm

Sean

Quoted 20 lines

-Daniel

Quoted 12 lines
drivers/gpu/drm/drm_damage_helper.c +5 -1
+                       goto out;
+
+               if (plane->state->fb != fb) {
+                       drm_modeset_unlock(&plane->mutex);
                        continue;
+               }
 
                plane_state = drm_atomic_get_plane_state(state, plane);
                if (IS_ERR(plane_state)) {
--

Sean Paul, Software Engineer, Google / Chromium OS

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
#3 lore raw
Quoted 48 lines · Sean Paul
Quoted 39 lines · Daniel Vetter
Quoted 38 lines · Sean Paul

From: Sean Paul <[email protected]>

Since the dirtyfb ioctl doesn't give us any hints as to which plane is scanning out the fb it's marking as damaged, we need to loop through planes to find it.

Currently we just reach into plane state and check, but that can race with another commit changing the fb out from under us. This patch locks the plane before checking the fb and will release the lock if the plane is not displaying the dirty fb.

Fixes b9fc5e01d1ce ("drm: Add helper to implement legacy dirtyfb")
Cc Rob Clark <[email protected]>
Cc Deepak Rawat <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Thomas Hellstrom <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Sean Paul <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc <[email protected]> # v5.0+
---
 drivers/gpu/drm/drm_damage_helper.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_damage_helper.c +0 -0
index 8230dac01a89..3a4126dc2520 100644
@@ -212,8 +212,14 @@ int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
        drm_for_each_plane(plane, fb->dev) {
                struct drm_plane_state *plane_state;
 
Quoted 7 lines

drivers/gpu/drm/drm_damage_helper.c int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb, · line 216

Quoted 3 lines
drivers/gpu/drm/drm_damage_helper.c +2 -1
-               if (plane->state->fb != fb)
+               ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
+               if (ret)

I think for paranoid safety we should have a WARN_ON(ret == -EALREADY) here. It should be impossible, but if it's not for some oddball reason, we'll blow up.

Quoted 2 lines

drm_modeset_lock eats EALREADY and returns 0 for that case, so I guess it depends _how_ paranoid you want to be here :-)

Ah silly me, r-b as-is then.
-Daniel

Quoted 35 lines
Quoted 6 lines

With that: Reviewed-by: Daniel Vetter <[email protected]>

But please give this a spin with some workloads and the ww_mutex slowpath debugging enabled, just to makre sure.

Ok, had a chance to run through some tests this morning with CONFIG_DEBUG_WW_MUTEX_SLOWPATH and things lgtm

Sean

Quoted 20 lines

-Daniel

Quoted 12 lines
drivers/gpu/drm/drm_damage_helper.c +5 -1
+                       goto out;
+
+               if (plane->state->fb != fb) {
+                       drm_modeset_unlock(&plane->mutex);
                        continue;
+               }
 
                plane_state = drm_atomic_get_plane_state(state, plane);
                if (IS_ERR(plane_state)) {
--

Sean Paul, Software Engineer, Google / Chromium OS

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
author
#4 lore raw
Quoted 49 lines · Daniel Vetter
Quoted 48 lines · Sean Paul
Quoted 46 lines · Daniel Vetter
Quoted 35 lines · Sean Paul

From: Sean Paul <[email protected]>

Since the dirtyfb ioctl doesn't give us any hints as to which plane is scanning out the fb it's marking as damaged, we need to loop through planes to find it.

Currently we just reach into plane state and check, but that can race with another commit changing the fb out from under us. This patch locks the plane before checking the fb and will release the lock if the plane is not displaying the dirty fb.

Fixes b9fc5e01d1ce ("drm: Add helper to implement legacy dirtyfb")
Cc Rob Clark <[email protected]>
Cc Deepak Rawat <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Thomas Hellstrom <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Sean Paul <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc <[email protected]> # v5.0+
---
 drivers/gpu/drm/drm_damage_helper.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_damage_helper.c +0 -0
index 8230dac01a89..3a4126dc2520 100644

drivers/gpu/drm/drm_damage_helper.c int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb, · line 216

Quoted 6 lines
drivers/gpu/drm/drm_damage_helper.c +2 -1
@@ -212,8 +212,14 @@ int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
        drm_for_each_plane(plane, fb->dev) {
                struct drm_plane_state *plane_state;
 
-               if (plane->state->fb != fb)
+               ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
+               if (ret)

I think for paranoid safety we should have a WARN_ON(ret == -EALREADY) here. It should be impossible, but if it's not for some oddball reason, we'll blow up.

Quoted 4 lines
Quoted 2 lines

drm_modeset_lock eats EALREADY and returns 0 for that case, so I guess it depends _how_ paranoid you want to be here :-)

Ah silly me, r-b as-is then.

Thanks, pushed to -misc-next

Sean

Quoted 42 lines

-Daniel

Quoted 35 lines
Quoted 6 lines

With that: Reviewed-by: Daniel Vetter <[email protected]>

But please give this a spin with some workloads and the ww_mutex slowpath debugging enabled, just to makre sure.

Ok, had a chance to run through some tests this morning with CONFIG_DEBUG_WW_MUTEX_SLOWPATH and things lgtm

Sean

Quoted 20 lines

-Daniel

Quoted 12 lines
drivers/gpu/drm/drm_damage_helper.c +5 -1
+                       goto out;
+
+               if (plane->state->fb != fb) {
+                       drm_modeset_unlock(&plane->mutex);
                        continue;
+               }
 
                plane_state = drm_atomic_get_plane_state(state, plane);
                if (IS_ERR(plane_state)) {
--

Sean Paul, Software Engineer, Google / Chromium OS

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 5 lines
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel