Skip to contentSkip to conversation

drm/amdgpu:resolv deadlock between reset and cs_ioctl

PATCH In review Monk Liu 2 participants 4 messages Sep 11, 2017 – Sep 12, 2017 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Monk Liu

author
#0 lore raw

PATCH 1/1 drm/amdgpu:resolv deadlock between reset and cs_ioctl

need to unreserve ttm bo before "cs_add_fence" and "entity_push_job" otherwise there will be deadlock between "recover_vram_from_shadow" and previous two routines on the ttm bo's resv lock.

Change-Id I9c18b677e474ce5b45a9cc24da3fa255d77b3d44
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 35 ++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +19 -16
index c1f1ae9..823ac12 100644
@@ -736,26 +736,12 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
 /**
  * cs_parser_fini() - clean parser states
  * @parser:	parser structure holding parsing context.
- * @error:	error number
- *
- * If error is set than unvalidate buffer, otherwise just free memory
- * used by parsing context.
  **/
-static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
+static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
 {
 	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
 	unsigned i;
 
-	if (!error) {
-		amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
-
-		ttm_eu_fence_buffer_objects(&parser->ticket,
-					    &parser->validated,
-					    parser->fence);
-	} else if (backoff) {
-		ttm_eu_backoff_reservation(&parser->ticket,
-					   &parser->validated);
-	}
 	fence_put(parser->fence);
 
 	if (parser->ctx)
@@ -1075,6 +1061,16 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	job->owner = p->filp;
 	job->fence_ctx = entity->fence_context;
 	p->fence = fence_get(&job->base.s_fence->finished);
+
+	/* hook sched fence to all BOs' reservation in validated list
+	 * and unreserve them.
+	 *
+	 * we unreserve at here is because otherwise
+	 * there'll be deadlock between ctx_add_fence/sched_entity_push_job
+	 * and gpu_reset routine's recover_bo_from_shadow on PD/PTEs' ttm bo lock
+	 */
+	ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
+
 	r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
 	if (r) {
 		fence_put(p->fence);
@@ -1095,6 +1091,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 {
 	struct amdgpu_device *adev = dev->dev_private;
 	union drm_amdgpu_cs *cs = data;
+	struct amdgpu_fpriv *fpriv;
 	struct amdgpu_cs_parser parser = {};
 	bool reserved_buffers = false;
 	int i, r;
@@ -1104,6 +1101,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 
 	parser.adev = adev;
 	parser.filp = filp;
+	fpriv = filp->driver_priv;
 
 	r = amdgpu_cs_parser_init(&parser, data);
 	if (r) {
@@ -1141,7 +1139,12 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 	r = amdgpu_cs_submit(&parser, cs);
 
 out:
-	amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
+	if (!r)
+		amdgpu_vm_move_pt_bos_in_lru(parser.adev, &fpriv->vm);
+	else if (reserved_buffers)
+		ttm_eu_backoff_reservation(&parser.ticket, &parser.validated);
+
+	amdgpu_cs_parser_fini(&parser);
 	return r;
 }
 
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1505120545-12805-1-git-send-email-Monk.Liu-5C7GfCeVMHo@public.gmane.org

#2 lore raw

Am 11.09.2017 um 11:02 schrieb Monk Liu:

Quoted 6 lines

need to unreserve ttm bo before "cs_add_fence" and "entity_push_job" otherwise there will be deadlock between "recover_vram_from_shadow" and previous two routines on the ttm bo's resv lock.

Change-Id I9c18b677e474ce5b45a9cc24da3fa255d77b3d44

Yeah, I was thinking about this as well. Problem is this won't work without further changes.

Quoted 39 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 35 ++++++++++++++++++----------------
  1 file changed, 19 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -15
index c1f1ae9..823ac12 100644
@@ -736,26 +736,12 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  /**
   * cs_parser_fini() - clean parser states
   * @parser:	parser structure holding parsing context.
- * @error:	error number
- *
- * If error is set than unvalidate buffer, otherwise just free memory
- * used by parsing context.
   **/
-static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
+static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
  {
  	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  	unsigned i;
 
-	if (!error) {
-		amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
-
-		ttm_eu_fence_buffer_objects(&parser->ticket,
-					    &parser->validated,
-					    parser->fence);
-	} else if (backoff) {
-		ttm_eu_backoff_reservation(&parser->ticket,
-					   &parser->validated);
-	}
  	fence_put(parser->fence);
 
  	if (parser->ctx)
@@ -1075,6 +1061,16 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	job->owner = p->filp;
  	job->fence_ctx = entity->fence_context;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1072

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +9 -0
  	p->fence = fence_get(&job->base.s_fence->finished);
+
+	/* hook sched fence to all BOs' reservation in validated list
+	 * and unreserve them.
+	 *
+	 * we unreserve at here is because otherwise
+	 * there'll be deadlock between ctx_add_fence/sched_entity_push_job
+	 * and gpu_reset routine's recover_bo_from_shadow on PD/PTEs' ttm bo lock
+	 */
+	ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

The order of job->base.s_fence and preventing concurrent CS was guaranteed by the VM PD being reserved, that is now no longer the case and we need a new lock for protection.

I suggest to add another lock to the context which we grab in amdgpu_ctx_get() and release in amdgpu_ctx_put().

Regards,
Christian.

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
+
  	r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
  	if (r) {
  		fence_put(p->fence);
@@ -1095,6 +1091,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  {
  	struct amdgpu_device *adev = dev->dev_private;
  	union drm_amdgpu_cs *cs = data;
+	struct amdgpu_fpriv *fpriv;
  	struct amdgpu_cs_parser parser = {};
  	bool reserved_buffers = false;
  	int i, r;
@@ -1104,6 +1101,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 
  	parser.adev = adev;
  	parser.filp = filp;
+	fpriv = filp->driver_priv;
 
  	r = amdgpu_cs_parser_init(&parser, data);
  	if (r) {
@@ -1141,7 +1139,12 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  	r = amdgpu_cs_submit(&parser, cs);
 
  out:

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 1150

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -1
-	amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
+	if (!r)
+		amdgpu_vm_move_pt_bos_in_lru(parser.adev, &fpriv->vm);
+	else if (reserved_buffers)
+		ttm_eu_backoff_reservation(&parser.ticket, &parser.validated);
+
+	amdgpu_cs_parser_fini(&parser);
  	return r;
  }
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): b035905d-c405-b555-20c8-2bc02a33db91-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

author
#4 lore raw
Quoted 1 line

The order of job->base.s_fence and preventing concurrent CS was guaranteed by the VM PD being reserved, that is now no longer the case and we need a new lock for protection.

Thought of this either, but since UMD/APP submits their jobs in one thread upon one ctx (if submits jobs within multiple thread upon one ctx that means the order is not important for UMD/app), so after we unreserved PD's resv lock We still only have one thread doing the cs_ioctl upon *current ctx*,

So how that not work now ? thanks !

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2017年9月11日 17:13
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu:resolv deadlock between reset and cs_ioctl

Am 11.09.2017 um 11:02 schrieb Monk Liu:

Quoted 6 lines

need to unreserve ttm bo before "cs_add_fence" and "entity_push_job" otherwise there will be deadlock between "recover_vram_from_shadow" and previous two routines on the ttm bo's resv lock.

Change-Id I9c18b677e474ce5b45a9cc24da3fa255d77b3d44

Yeah, I was thinking about this as well. Problem is this won't work without further changes.

Quoted 41 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 35 ++++++++++++++++++----------------
  1 file changed, 19 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -15
index c1f1ae9..823ac12 100644
@@ -736,26 +736,12 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
  /**
   * cs_parser_fini() - clean parser states
   * @parser:	parser structure holding parsing context.
- * @error:	error number
- *
- * If error is set than unvalidate buffer, otherwise just free memory
- * used by parsing context.
   **/
-static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, 
 int error, bool backoff)
+static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
  {
  	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
  	unsigned i;
 
-	if (!error) {
-		amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
-
-		ttm_eu_fence_buffer_objects(&parser->ticket,
-					    &parser->validated,
-					    parser->fence);
-	} else if (backoff) {
-		ttm_eu_backoff_reservation(&parser->ticket,
-					   &parser->validated);
-	}
  	fence_put(parser->fence);
 

if (parser->ctx)

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1075,6 +1061,16 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	job->owner = p->filp;
  	job->fence_ctx = entity->fence_context;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1072

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +9 -0
  	p->fence = fence_get(&job->base.s_fence->finished);
+
+	/* hook sched fence to all BOs' reservation in validated list
+	 * and unreserve them.
+	 *
+	 * we unreserve at here is because otherwise
+	 * there'll be deadlock between ctx_add_fence/sched_entity_push_job
+	 * and gpu_reset routine's recover_bo_from_shadow on PD/PTEs' ttm bo lock
+	 */
+	ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

The order of job->base.s_fence and preventing concurrent CS was guaranteed by the VM PD being reserved, that is now no longer the case and we need a new lock for protection.

I suggest to add another lock to the context which we grab in amdgpu_ctx_get() and release in amdgpu_ctx_put().

Regards,
Christian.

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
+
  	r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
  	if (r) {
  		fence_put(p->fence);
@@ -1095,6 +1091,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  {
  	struct amdgpu_device *adev = dev->dev_private;
  	union drm_amdgpu_cs *cs = data;
+	struct amdgpu_fpriv *fpriv;
  	struct amdgpu_cs_parser parser = {};
  	bool reserved_buffers = false;
  	int i, r;
@@ -1104,6 +1101,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void 
 *data, struct drm_file *filp)
 
  	parser.adev = adev;
  	parser.filp = filp;
+	fpriv = filp->driver_priv;
 
  	r = amdgpu_cs_parser_init(&parser, data);
  	if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1141,7 +1139,12 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  	r = amdgpu_cs_submit(&parser, cs);
 
  out:

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 1150

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -1
-	amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
+	if (!r)
+		amdgpu_vm_move_pt_bos_in_lru(parser.adev, &fpriv->vm);
+	else if (reserved_buffers)
+		ttm_eu_backoff_reservation(&parser.ticket, &parser.validated);
+
+	amdgpu_cs_parser_fini(&parser);
  	return r;
  }
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): BLUPR12MB0449CA127E5569CD411DEB3484690-7LeqcoF/[email protected]

#6 lore raw
Quoted 1 line

but since UMD/APP submits their jobs in one thread upon one ctx

That's correct for Mesa, but not necessary other user space stacks.

But that's actually irrelevant, IOCTLs must be thread save to the point that they at least don't crash or produce a hardware hang or something like this.

Aborting with a result that indicates concurrently access is forbidden (like -EBUSY) is ok, but hard to get right and so should usually be avoided as well.

Regards,
Christian.

Am 12.09.2017 um 05:47 schrieb Liu, Monk:

Quoted 106 lines
Quoted 1 line

The order of job->base.s_fence and preventing concurrent CS was guaranteed by the VM PD being reserved, that is now no longer the case and we need a new lock for protection.

Thought of this either, but since UMD/APP submits their jobs in one thread upon one ctx (if submits jobs within multiple thread upon one ctx that means the order is not important for UMD/app), so after we unreserved PD's resv lock We still only have one thread doing the cs_ioctl upon *current ctx*,

So how that not work now ? thanks !

BR Monk

-----Original Message-----
From: Christian König [mailto:[email protected]]
Sent: 2017年9月11日 17:13
To: Liu, Monk <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu:resolv deadlock between reset and cs_ioctl

Am 11.09.2017 um 11:02 schrieb Monk Liu:

Quoted 6 lines

need to unreserve ttm bo before "cs_add_fence" and "entity_push_job" otherwise there will be deadlock between "recover_vram_from_shadow" and previous two routines on the ttm bo's resv lock.

Change-Id I9c18b677e474ce5b45a9cc24da3fa255d77b3d44

Yeah, I was thinking about this as well. Problem is this won't work without further changes.

Quoted 41 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 35 ++++++++++++++++++----------------
   1 file changed, 19 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -15
index c1f1ae9..823ac12 100644
@@ -736,26 +736,12 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
   /**
    * cs_parser_fini() - clean parser states
    * @parser:	parser structure holding parsing context.
- * @error:	error number
- *
- * If error is set than unvalidate buffer, otherwise just free memory
- * used by parsing context.
    **/
-static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser,
 int error, bool backoff)
+static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
   {
   	struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
   	unsigned i;
 
-	if (!error) {
-		amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
-
-		ttm_eu_fence_buffer_objects(&parser->ticket,
-					    &parser->validated,
-					    parser->fence);
-	} else if (backoff) {
-		ttm_eu_backoff_reservation(&parser->ticket,
-					   &parser->validated);
-	}
   	fence_put(parser->fence);
 
   	if (parser->ctx)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1075,6 +1061,16 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
   	job->owner = p->filp;
   	job->fence_ctx = entity->fence_context;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1072

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +9 -0
   	p->fence = fence_get(&job->base.s_fence->finished);
+
+	/* hook sched fence to all BOs' reservation in validated list
+	 * and unreserve them.
+	 *
+	 * we unreserve at here is because otherwise
+	 * there'll be deadlock between ctx_add_fence/sched_entity_push_job
+	 * and gpu_reset routine's recover_bo_from_shadow on PD/PTEs' ttm bo lock
+	 */
+	ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);

The order of job->base.s_fence and preventing concurrent CS was guaranteed by the VM PD being reserved, that is now no longer the case and we need a new lock for protection.

I suggest to add another lock to the context which we grab in amdgpu_ctx_get() and release in amdgpu_ctx_put().

Regards,
Christian.

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
+
   	r = amdgpu_ctx_add_fence(p->ctx, ring, p->fence, &seq);
   	if (r) {
   		fence_put(p->fence);
@@ -1095,6 +1091,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
   {
   	struct amdgpu_device *adev = dev->dev_private;
   	union drm_amdgpu_cs *cs = data;
+	struct amdgpu_fpriv *fpriv;
   	struct amdgpu_cs_parser parser = {};
   	bool reserved_buffers = false;
   	int i, r;
@@ -1104,6 +1101,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void
 *data, struct drm_file *filp)
 
   	parser.adev = adev;
   	parser.filp = filp;
+	fpriv = filp->driver_priv;
 
   	r = amdgpu_cs_parser_init(&parser, data);
   	if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1141,7 +1139,12 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
   	r = amdgpu_cs_submit(&parser, cs);
 
Quoted 16 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 1150

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -1
   out:
-	amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
+	if (!r)
+		amdgpu_vm_move_pt_bos_in_lru(parser.adev, &fpriv->vm);
+	else if (reserved_buffers)
+		ttm_eu_backoff_reservation(&parser.ticket, &parser.validated);
+
+	amdgpu_cs_parser_fini(&parser);
   	return r;
   }
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx