Skip to content

Commit 90af0ca

Browse files
Luben Tuikovalexdeucher
authored andcommitted
drm/amdgpu: Protect the amdgpu_bo_list list with a mutex v2
Protect the struct amdgpu_bo_list with a mutex. This is used during command submission in order to avoid buffer object corruption as recorded in the link below. v2 (chk): Keep the mutex looked for the whole CS to avoid using the list from multiple CS threads at the same time. Suggested-by: Christian König <[email protected]> Cc: Alex Deucher <[email protected]> Cc: Andrey Grodzovsky <[email protected]> Cc: Vitaly Prosyak <[email protected]> Link: https://gitlab.freedesktop.org/drm/amd/-/issues/2048 Signed-off-by: Luben Tuikov <[email protected]> Signed-off-by: Christian König <[email protected]> Tested-by: Luben Tuikov <[email protected]> Reviewed-by: Alex Deucher <[email protected]> Signed-off-by: Alex Deucher <[email protected]> Cc: [email protected]
1 parent e1aadba commit 90af0ca

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu)
4040
{
4141
struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list,
4242
rhead);
43-
43+
mutex_destroy(&list->bo_list_mutex);
4444
kvfree(list);
4545
}
4646

@@ -136,6 +136,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
136136

137137
trace_amdgpu_cs_bo_status(list->num_entries, total_size);
138138

139+
mutex_init(&list->bo_list_mutex);
139140
*result = list;
140141
return 0;
141142

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ struct amdgpu_bo_list {
4747
struct amdgpu_bo *oa_obj;
4848
unsigned first_userptr;
4949
unsigned num_entries;
50+
51+
/* Protect access during command submission.
52+
*/
53+
struct mutex bo_list_mutex;
5054
};
5155

5256
int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
519519
return r;
520520
}
521521

522+
mutex_lock(&p->bo_list->bo_list_mutex);
523+
522524
/* One for TTM and one for the CS job */
523525
amdgpu_bo_list_for_each_entry(e, p->bo_list)
524526
e->tv.num_shared = 2;
@@ -651,6 +653,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
651653
kvfree(e->user_pages);
652654
e->user_pages = NULL;
653655
}
656+
mutex_unlock(&p->bo_list->bo_list_mutex);
654657
}
655658
return r;
656659
}
@@ -690,9 +693,11 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
690693
{
691694
unsigned i;
692695

693-
if (error && backoff)
696+
if (error && backoff) {
694697
ttm_eu_backoff_reservation(&parser->ticket,
695698
&parser->validated);
699+
mutex_unlock(&parser->bo_list->bo_list_mutex);
700+
}
696701

697702
for (i = 0; i < parser->num_post_deps; i++) {
698703
drm_syncobj_put(parser->post_deps[i].syncobj);
@@ -832,12 +837,16 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
832837
continue;
833838

834839
r = amdgpu_vm_bo_update(adev, bo_va, false);
835-
if (r)
840+
if (r) {
841+
mutex_unlock(&p->bo_list->bo_list_mutex);
836842
return r;
843+
}
837844

838845
r = amdgpu_sync_fence(&p->job->sync, bo_va->last_pt_update);
839-
if (r)
846+
if (r) {
847+
mutex_unlock(&p->bo_list->bo_list_mutex);
840848
return r;
849+
}
841850
}
842851

843852
r = amdgpu_vm_handle_moved(adev, vm);
@@ -1278,6 +1287,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
12781287

12791288
ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
12801289
mutex_unlock(&p->adev->notifier_lock);
1290+
mutex_unlock(&p->bo_list->bo_list_mutex);
12811291

12821292
return 0;
12831293

0 commit comments

Comments
 (0)