Skip to content

Commit ee27c88

Browse files
Mazin Rezkgregkh
Mazin Rezk
authored andcommitted
drm/amd/display: Clear dm_state for fast updates
commit fde9f39 upstream. This patch fixes a race condition that causes a use-after-free during amdgpu_dm_atomic_commit_tail. This can occur when 2 non-blocking commits are requested and the second one finishes before the first. Essentially, this bug occurs when the following sequence of events happens: 1. Non-blocking commit gregkh#1 is requested w/ a new dm_state gregkh#1 and is deferred to the workqueue. 2. Non-blocking commit gregkh#2 is requested w/ a new dm_state gregkh#2 and is deferred to the workqueue. 3. Commit gregkh#2 starts before commit gregkh#1, dm_state gregkh#1 is used in the commit_tail and commit gregkh#2 completes, freeing dm_state gregkh#1. 4. Commit gregkh#1 starts after commit gregkh#2 completes, uses the freed dm_state 1 and dereferences a freelist pointer while setting the context. Since this bug has only been spotted with fast commits, this patch fixes the bug by clearing the dm_state instead of using the old dc_state for fast updates. In addition, since dm_state is only used for its dc_state and amdgpu_dm_atomic_commit_tail will retain the dc_state if none is found, removing the dm_state should not have any consequences in fast updates. This use-after-free bug has existed for a while now, but only caused a noticeable issue starting from 5.7-rc1 due to 3202fa6 ("slub: relocate freelist pointer to middle of object") moving the freelist pointer from dm_state->base (which was unused) to dm_state->context (which is dereferenced). Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=207383 Fixes: bd200d1 ("drm/amd/display: Don't replace the dc_state for fast updates") Reported-by: Duncan <[email protected]> Signed-off-by: Mazin Rezk <[email protected]> Reviewed-by: Nicholas Kazlauskas <[email protected]> Signed-off-by: Alex Deucher <[email protected]> Cc: [email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent f53aaf8 commit ee27c88

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8489,20 +8489,38 @@ static int amdgpu_dm_atomic_check(struct drm_device *dev,
84898489
* the same resource. If we have a new DC context as part of
84908490
* the DM atomic state from validation we need to free it and
84918491
* retain the existing one instead.
8492+
*
8493+
* Furthermore, since the DM atomic state only contains the DC
8494+
* context and can safely be annulled, we can free the state
8495+
* and clear the associated private object now to free
8496+
* some memory and avoid a possible use-after-free later.
84928497
*/
8493-
struct dm_atomic_state *new_dm_state, *old_dm_state;
84948498

8495-
new_dm_state = dm_atomic_get_new_state(state);
8496-
old_dm_state = dm_atomic_get_old_state(state);
8499+
for (i = 0; i < state->num_private_objs; i++) {
8500+
struct drm_private_obj *obj = state->private_objs[i].ptr;
84978501

8498-
if (new_dm_state && old_dm_state) {
8499-
if (new_dm_state->context)
8500-
dc_release_state(new_dm_state->context);
8502+
if (obj->funcs == adev->dm.atomic_obj.funcs) {
8503+
int j = state->num_private_objs-1;
85018504

8502-
new_dm_state->context = old_dm_state->context;
8505+
dm_atomic_destroy_state(obj,
8506+
state->private_objs[i].state);
8507+
8508+
/* If i is not at the end of the array then the
8509+
* last element needs to be moved to where i was
8510+
* before the array can safely be truncated.
8511+
*/
8512+
if (i != j)
8513+
state->private_objs[i] =
8514+
state->private_objs[j];
85038515

8504-
if (old_dm_state->context)
8505-
dc_retain_state(old_dm_state->context);
8516+
state->private_objs[j].ptr = NULL;
8517+
state->private_objs[j].state = NULL;
8518+
state->private_objs[j].old_state = NULL;
8519+
state->private_objs[j].new_state = NULL;
8520+
8521+
state->num_private_objs = j;
8522+
break;
8523+
}
85068524
}
85078525
}
85088526

0 commit comments

Comments
 (0)