drm: Block fb changes for async plane updates
Subsystems (2)
PATCH 1/1 drm: Block fb changes for async plane updates
The behavior of drm_atomic_helper_cleanup_planes differs depending on whether the commit was an asynchronous update or not.
For a typical (non-async) atomic commit prepare_fb is called on the new_plane_state and cleanup_fb is called on the old_plane_state.
However, async commits are performed in place and don't swap the state for the plane. The call to prepare_fb happens on the new_plane_state and the call to cleanup_fb is also called on the new_plane_state in this case (since the state hasn't swapped).
This behavior can lead to use-after-free or unpin of an active fb.
Consider the following sequence of events for interleaving fbs:
- Async update, fb1 prepare, fb1 cleanup_fb
- Async update, fb2 prepare, fb2 cleanup_fb
- Non-async update, fb1 prepare, fb2 cleanup_fb
- Async update, fb2 cleanup_fb -> double cleanup, use-after-free
This situation has been observed in practice for a double buffered cursor when closing an X client. The non-async update occurs because the new_plane_state->crtc != old_plane_state->crtc which forces the non-async path to occur.
The simplest fix for this is to block fb updates in drm_atomic_helper_async_check. This guarantees that the framebuffer will have previously been prepared and any subsequent async updates will always call prepare and cleanup_fb like the non-async atomic commit path would.
--- drivers/gpu/drm/drm_atomic_helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_atomic_helper.c +2 -1
@@ -1599,7 +1599,8 @@ int drm_atomic_helper_async_check(struct drm_device *dev,return -EINVAL ;if (!new_plane_state->crtc || - old_plane_state->crtc != new_plane_state->crtc) + old_plane_state->crtc != new_plane_state->crtc || + old_plane_state->fb != new_plane_state->fb)return -EINVAL ; funcs = plane->helper_private;
Signature · 6 lines
2.17.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
As far as we discussed this internally looks good to me, but obviously we need to wait for some feedback from non AMD people.
Andrey
Quoted 45 lines · Nicholas Kazlauskas
The behavior of
drm_atomic_helper_cleanup_planesdiffers depending on whether the commit was an asynchronous update or not.For a typical (non-async) atomic commit
prepare_fbis called on thenew_plane_stateandcleanup_fbis called on theold_plane_state.However, async commits are performed in place and don't swap the state for the plane. The call to
prepare_fbhappens on thenew_plane_stateand the call tocleanup_fbis also called on thenew_plane_statein this case (since the state hasn't swapped).This behavior can lead to use-after-free or unpin of an active fb.
Consider the following sequence of events for interleaving fbs:
- Async update, fb1 prepare, fb1
cleanup_fb
- Async update, fb2 prepare, fb2cleanup_fb
- Non-async update, fb1 prepare, fb2cleanup_fb
- Async update, fb2cleanup_fb-> double cleanup, use-after-freeThis situation has been observed in practice for a double buffered cursor when closing an X client. The non-async update occurs because the
new_plane_state->crtc!=old_plane_state->crtcwhich forces the non-async path to occur.The simplest fix for this is to block fb updates in
drm_atomic_helper_async_check. This guarantees that the framebuffer will have previously been prepared and any subsequent async updates will always call prepare andcleanup_fblike the non-async atomic commit path would.Cc Michel Dänzer <[email protected]>Cc Daniel Vetter <[email protected]>Cc Andrey Grodzovsky <[email protected]>Cc Harry Wentland <[email protected]>Signed-off-by Nicholas Kazlauskas <[email protected]>--- drivers/gpu/drm/drm_atomic_helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_atomic_helper.c +0
-0
drivers/gpu/drm/drm_atomic_helper.c int drm_atomic_helper_async_check(struct drm_device *dev, · line 1606
Quoted 9 lines
drivers/gpu/drm/drm_atomic_helper.c +2
-1@@ -1599,7 +1599,8 @@ int drm_atomic_helper_async_check(struct drm_device *dev,return -EINVAL ;if (!new_plane_state->crtc || - old_plane_state->crtc != new_plane_state->crtc) + old_plane_state->crtc != new_plane_state->crtc || + old_plane_state->fb != new_plane_state->fb)return -EINVAL ; funcs = plane->helper_private;
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 11 lines · Nicholas Kazlauskas
The behavior of
drm_atomic_helper_cleanup_planesdiffers depending on whether the commit was an asynchronous update or not.For a typical (non-async) atomic commit
prepare_fbis called on thenew_plane_stateandcleanup_fbis called on theold_plane_state.However, async commits are performed in place and don't swap the state for the plane. The call to
prepare_fbhappens on thenew_plane_stateand the call tocleanup_fbis also called on thenew_plane_statein this case (since the state hasn't swapped).
Quoted 8 lines
This behavior can lead to use-after-free or unpin of an active fb.
Consider the following sequence of events for interleaving fbs:
- Async update, fb1 prepare, fb1
cleanup_fb
- Async update, fb2 prepare, fb2cleanup_fb
- Non-async update, fb1 prepare, fb2cleanup_fb
- Async update, fb2cleanup_fb-> double cleanup, use-after-free
I think I see your bug, but I'm completely lost in your description above.
I think this is ok as a short-term gap, but imo better if it's a separate if condition with a FIXME comment.
Long-term we want to fix this, and I think simplest way to do that is if we expect drivers to store the old fb in the new_plane_state (and check that with a WARN_ON like the others). I think that should work.
We probably also need some locking on top, to prevent races with the cleanup_fb calls done by non-blocking commits, to make sure those clean up the right fb.
-Daniel
Quoted 37 lines
This situation has been observed in practice for a double buffered cursor when closing an X client. The non-async update occurs because the
new_plane_state->crtc!=old_plane_state->crtcwhich forces the non-async path to occur.The simplest fix for this is to block fb updates in
drm_atomic_helper_async_check. This guarantees that the framebuffer will have previously been prepared and any subsequent async updates will always call prepare andcleanup_fblike the non-async atomic commit path would.Cc Michel Dänzer <[email protected]>Cc Daniel Vetter <[email protected]>Cc Andrey Grodzovsky <[email protected]>Cc Harry Wentland <[email protected]>Signed-off-by Nicholas Kazlauskas <[email protected]>--- drivers/gpu/drm/drm_atomic_helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_atomic_helper.c +2
-1@@ -1599,7 +1599,8 @@ int drm_atomic_helper_async_check(struct drm_device *dev,return -EINVAL ;if (!new_plane_state->crtc || - old_plane_state->crtc != new_plane_state->crtc) + old_plane_state->crtc != new_plane_state->crtc || + old_plane_state->fb != new_plane_state->fb)return -EINVAL ; funcs = plane->helper_private;Signature · 1 line
2.17.1
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 26 lines · Daniel Vetter
Quoted 11 lines · Nicholas Kazlauskas
The behavior of
drm_atomic_helper_cleanup_planesdiffers depending on whether the commit was an asynchronous update or not.For a typical (non-async) atomic commit
prepare_fbis called on thenew_plane_stateandcleanup_fbis called on theold_plane_state.However, async commits are performed in place and don't swap the state for the plane. The call to
prepare_fbhappens on thenew_plane_stateand the call tocleanup_fbis also called on thenew_plane_statein this case (since the state hasn't swapped).Quoted 8 lines
This behavior can lead to use-after-free or unpin of an active fb.
Consider the following sequence of events for interleaving fbs:
- Async update, fb1 prepare, fb1
cleanup_fb
- Async update, fb2 prepare, fb2cleanup_fb
- Non-async update, fb1 prepare, fb2cleanup_fb
- Async update, fb2cleanup_fb-> double cleanup, use-after-freeI think I see your bug, but I'm completely lost in your description above.
I think this is ok as a short-term gap, but imo better if it's a separate if condition with a FIXME comment.
Quoted 8 lines
Long-term we want to fix this, and I think simplest way to do that is if we expect drivers to store the old fb in the
new_plane_state(and check that with aWARN_ONlike the others). I think that should work.We probably also need some locking on top, to prevent races with the
cleanup_fbcalls done by non-blocking commits, to make sure those clean up the right fb.
-Daniel
Hi Daniel,
The description is supposed to be saying that the wrong fb is being cleaned up because in state updates to the plane don't swap the state pointer - which is required by the cleanup planes helper function.
But putting that aside, I think what you suggest here would work best for "fixing" the problem. Storing the old fb pointer in the new state looks kind of odd, but as long as it's documented and there's the WARN_ON in the helper I think it's fine. I think I would prefer this over a solution that blocks fb changes in the helper altogether.
I don't think any additional drm level locking is necessary here. The race with cleanup_fb calls is already something you have to worry about when dealing with async commits even if the fb hasn't changed. Most drivers have their own internal locks or ref-counting that can handle this.
So to summarize, I think I can post a new patch series that addresses this problem by fixing existing async commit usage in vc4 and amdgpu for framebuffer changes. The last patch in the series could be the one that adds a WARN_ON in the drm_atomic_helper_async_commit helper along with a comment indicating that the old fb should be in the new plane state.
Does this sound reasonable to you?
Nicholas Kazlauskas
Quoted 34 lines
Quoted 34 lines
This situation has been observed in practice for a double buffered cursor when closing an X client. The non-async update occurs because the
new_plane_state->crtc!=old_plane_state->crtcwhich forces the non-async path to occur.The simplest fix for this is to block fb updates in
drm_atomic_helper_async_check. This guarantees that the framebuffer will have previously been prepared and any subsequent async updates will always call prepare andcleanup_fblike the non-async atomic commit path would.Cc Michel Dänzer <[email protected]>Cc Daniel Vetter <[email protected]>Cc Andrey Grodzovsky <[email protected]>Cc Harry Wentland <[email protected]>Signed-off-by Nicholas Kazlauskas <[email protected]>--- drivers/gpu/drm/drm_atomic_helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_atomic_helper.c +2
-1@@ -1599,7 +1599,8 @@ int drm_atomic_helper_async_check(struct drm_device *dev,return -EINVAL ;if (!new_plane_state->crtc || - old_plane_state->crtc != new_plane_state->crtc) + old_plane_state->crtc != new_plane_state->crtc || + old_plane_state->fb != new_plane_state->fb)return -EINVAL ;
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/drm_atomic_helper.c +0
-0funcs = plane->helper_private;Signature · 1 line
2.17.1
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 36 lines · Nicholas
Quoted 26 lines · Daniel Vetter
Quoted 11 lines · Nicholas Kazlauskas
The behavior of
drm_atomic_helper_cleanup_planesdiffers depending on whether the commit was an asynchronous update or not.For a typical (non-async) atomic commit
prepare_fbis called on thenew_plane_stateandcleanup_fbis called on theold_plane_state.However, async commits are performed in place and don't swap the state for the plane. The call to
prepare_fbhappens on thenew_plane_stateand the call tocleanup_fbis also called on thenew_plane_statein this case (since the state hasn't swapped).Quoted 8 lines
This behavior can lead to use-after-free or unpin of an active fb.
Consider the following sequence of events for interleaving fbs:
- Async update, fb1 prepare, fb1
cleanup_fb
- Async update, fb2 prepare, fb2cleanup_fb
- Non-async update, fb1 prepare, fb2cleanup_fb
- Async update, fb2cleanup_fb-> double cleanup, use-after-freeI think I see your bug, but I'm completely lost in your description above.
I think this is ok as a short-term gap, but imo better if it's a separate if condition with a FIXME comment.
Quoted 8 lines
Long-term we want to fix this, and I think simplest way to do that is if we expect drivers to store the old fb in the
new_plane_state(and check that with aWARN_ONlike the others). I think that should work.We probably also need some locking on top, to prevent races with the
cleanup_fbcalls done by non-blocking commits, to make sure those clean up the right fb.
-Daniel
Quoted 5 lines
Hi Daniel,
The description is supposed to be saying that the wrong fb is being cleaned up because in state updates to the plane don't swap the state pointer - which is required by the cleanup planes helper function.
Yeah I inferred that, but the actual commit message isn't really clear on this, and the example you've listed only confused me. We need good commit messages, so that half a year down the road we still know what exactly we've been thinking :-) Especially in an area that's really tricky, like synchronization of the nonblocking atomic updates against normal updates (and each another), which has seen plenty of bugs in the past.
Quoted 11 lines
But putting that aside, I think what you suggest here would work best for "fixing" the problem. Storing the old fb pointer in the new state looks kind of odd, but as long as it's documented and there's the
WARN_ONin the helper I think it's fine. I think I would prefer this over a solution that blocks fb changes in the helper altogether.I don't think any additional drm level locking is necessary here. The race with
cleanup_fbcalls is already something you have to worry about when dealing with async commits even if the fb hasn't changed. Most drivers have their own internal locks or ref-counting that can handle this.
Quoted 7 lines
So to summarize, I think I can post a new patch series that addresses this problem by fixing existing async commit usage in vc4 and amdgpu for framebuffer changes. The last patch in the series could be the one that adds a
WARN_ONin thedrm_atomic_helper_async_commithelper along with a comment indicating that the old fb should be in the new plane state.Does this sound reasonable to you?
Let's please do the the minimal bugfixe first (probably cc: stable), and figure out how to properly fix things up long-term.
-Daniel
Quoted 43 lines
Nicholas Kazlauskas
Quoted 39 lines
Quoted 38 lines
This situation has been observed in practice for a double buffered cursor when closing an X client. The non-async update occurs because the
new_plane_state->crtc!=old_plane_state->crtcwhich forces the non-async path to occur.The simplest fix for this is to block fb updates in
drm_atomic_helper_async_check. This guarantees that the framebuffer will have previously been prepared and any subsequent async updates will always call prepare andcleanup_fblike the non-async atomic commit path would.Cc Michel Dänzer <[email protected]>Cc Daniel Vetter <[email protected]>Cc Andrey Grodzovsky <[email protected]>Cc Harry Wentland <[email protected]>Signed-off-by Nicholas Kazlauskas <[email protected]>--- drivers/gpu/drm/drm_atomic_helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_atomic_helper.c +2
-1@@ -1599,7 +1599,8 @@ int drm_atomic_helper_async_check(struct drm_device *dev,return -EINVAL ;if (!new_plane_state->crtc || - old_plane_state->crtc != new_plane_state->crtc) + old_plane_state->crtc != new_plane_state->crtc || + old_plane_state->fb != new_plane_state->fb)return -EINVAL ; funcs = plane->helper_private;Signature · 1 line
2.17.1
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel