Skip to contentSkip to conversation

drm/kfd: fix ttm_bo_release warning

PATCH NAK Lang Yu 4 participants 15 messages Sep 23, 2021 – Sep 24, 2021

Subsystems (3)
lore
Share & tools
lore

Lang Yu

author
#0 lore raw

PATCH 1/1 drm/kfd: fix ttm_bo_release warning

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
index 2d6b2d77b738..7e693b064072 100644
@@ -1567,6 +1567,9 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
 	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
 		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+
 	ret = unreserve_bo_and_vms(&ctx, false, false);
 
 	/* Remove from VM internal data structures */
Signature · 1 line
2.25.1
maintainer NAK
#1 lore raw

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 27 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
@@ -1567,6 +1567,9 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
  		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

Where was the BO pinned in the first place?

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
  	ret = unreserve_bo_and_vms(&ctx, false, false);
 
  	/* Remove from VM internal data structures */
author
#2 lore raw

[AMD Official Use Only]

Quoted 42 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 17 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30

Review

Quoted 6 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
  1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

Quoted 7 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
  		mem->va + bo_size * (1 + mem->aql_queue));
 
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
  	ret = unreserve_bo_and_vms(&ctx, false, false);
 
  	/* Remove from VM internal data structures */
maintainer
#3 lore raw

Am 23.09.21 um 14:09 schrieb Yu, Lang:

Quoted 46 lines

[AMD Official Use Only]

Quoted 42 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 17 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30

Review

Quoted 6 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
   1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

Quoted 7 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
   	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
   		mem->va + bo_size * (1 + mem->aql_queue));
 
Quoted 6 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

What amdgpu_bo_unpin() does is the following:

        ttm_bo_unpin(&bo->tbo);
        if (bo->tbo.pin_count)
                return;
....

In other words we take further actions based on if the buffer us is still pinned or not after an unpin operation.

What you try to do here is unpinning the BO when it is pinned independent if somebody else or our code has pinned it before.

That can lead to all kind of problems and is clearly illegal.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Well then you need to figure out where that memory is freed again and place the unpin appropriately.

General rule of thumb is that calls to amdgpu_bo_pin/amdgpu_bo_unpin should be balanced.

Regards,
Christian.

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
   	ret = unreserve_bo_and_vms(&ctx, false, false);
 
   	/* Remove from VM internal data structures */
author
#4 lore raw

[AMD Official Use Only]

Quoted 70 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 8:24 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 14:09 schrieb Yu, Lang:

Quoted 37 lines

[AMD Official Use Only]

Quoted 33 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 22 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
   1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 4 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int

amdgpu_amdkfd_gpuvm_free_memory_of_gpu(

Quoted 9 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
   	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
   		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

What amdgpu_bo_unpin() does is the following:

        ttm_bo_unpin(&bo->tbo);
        if (bo->tbo.pin_count)
                return;
....

Quoted 5 lines

In other words we take further actions based on if the buffer us is still pinned or not after an unpin operation.

What you try to do here is unpinning the BO when it is pinned independent if somebody else or our code has pinned it before.

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

Regards,
Lang

Quoted 29 lines

That can lead to all kind of problems and is clearly illegal.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Well then you need to figure out where that memory is freed again and place the unpin appropriately.

General rule of thumb is that calls to amdgpu_bo_pin/amdgpu_bo_unpin should be balanced.

Regards,
Christian.

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
   	ret = unreserve_bo_and_vms(&ctx, false, false);
 
   	/* Remove from VM internal data structures */
maintainer
#5 lore raw

Am 23.09.21 um 16:24 schrieb Yu, Lang:

Quoted 75 lines

[AMD Official Use Only]

Quoted 69 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 8:24 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 14:09 schrieb Yu, Lang:

Quoted 37 lines

[AMD Official Use Only]

Quoted 33 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 22 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
    1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 4 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int

amdgpu_amdkfd_gpuvm_free_memory_of_gpu(

Quoted 9 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
    	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
    		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

What amdgpu_bo_unpin() does is the following:

        ttm_bo_unpin(&bo->tbo);
        if (bo->tbo.pin_count)
                return;
....

Quoted 5 lines

In other words we take further actions based on if the buffer us is still pinned or not after an unpin operation.

What you try to do here is unpinning the BO when it is pinned independent if somebody else or our code has pinned it before.

Quoted 7 lines

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Regards,
Christian.

Quoted 30 lines

Regards,
Lang

Quoted 27 lines

That can lead to all kind of problems and is clearly illegal.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Well then you need to figure out where that memory is freed again and place the unpin appropriately.

General rule of thumb is that calls to amdgpu_bo_pin/amdgpu_bo_unpin should be balanced.

Regards,
Christian.

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
    	ret = unreserve_bo_and_vms(&ctx, false, false);
 
    	/* Remove from VM internal data structures */
author
#6 lore raw

[AMD Official Use Only]

Quoted 86 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 10:52 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 16:24 schrieb Yu, Lang:

Quoted 77 lines

[AMD Official Use Only]

Quoted 69 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 8:24 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 14:09 schrieb Yu, Lang:

Quoted 37 lines

[AMD Official Use Only]

Quoted 33 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 22 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
    1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 4 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int

amdgpu_amdkfd_gpuvm_free_memory_of_gpu(

Quoted 9 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
    	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
    		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

What amdgpu_bo_unpin() does is the following:

        ttm_bo_unpin(&bo->tbo);
        if (bo->tbo.pin_count)
                return;
....

Quoted 5 lines

In other words we take further actions based on if the buffer us is still pinned or not after an unpin operation.

What you try to do here is unpinning the BO when it is pinned independent if somebody else or our code has pinned it before.

Hi Christian,

Quoted 8 lines
Quoted 5 lines

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Regards,
Lang

Quoted 33 lines

Regards,
Christian.

Quoted 30 lines

Regards,
Lang

Quoted 27 lines

That can lead to all kind of problems and is clearly illegal.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Well then you need to figure out where that memory is freed again and place the unpin appropriately.

General rule of thumb is that calls to amdgpu_bo_pin/amdgpu_bo_unpin should be balanced.

Regards,
Christian.

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
    	ret = unreserve_bo_and_vms(&ctx, false, false);
 
    	/* Remove from VM internal data structures */
maintainer
#7 lore raw

Am 24.09.21 um 07:35 schrieb Yu, Lang:

Quoted 92 lines

[AMD Official Use Only]

Quoted 91 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 10:52 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 16:24 schrieb Yu, Lang:

Quoted 82 lines

[AMD Official Use Only]

Quoted 69 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 8:24 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 14:09 schrieb Yu, Lang:

Quoted 37 lines

[AMD Official Use Only]

Quoted 33 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 22 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
     drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
     1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 4 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int

amdgpu_amdkfd_gpuvm_free_memory_of_gpu(

Quoted 9 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
     	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
     		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

What amdgpu_bo_unpin() does is the following:

        ttm_bo_unpin(&bo->tbo);
        if (bo->tbo.pin_count)
                return;
....

Quoted 5 lines

In other words we take further actions based on if the buffer us is still pinned or not after an unpin operation.

What you try to do here is unpinning the BO when it is pinned independent if somebody else or our code has pinned it before.

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

Quoted 4 lines
Quoted 2 lines

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Well not with a flag or something like that.

The knowledge which BO is pinned and needs to be unpinned should come from the control logic and not be papered over by some general handling. That's the background why we have removed the general handling for this from TTM in the first place.

In other words when you need to pin a BO because it is kmapped it should be unpinned when it is kunmapped and if you don't kunmap at all then there is something wrong with the code structure from a higher level point of view.

Regards,
Christian.

Quoted 36 lines

Regards,
Lang

Quoted 32 lines

Regards,
Christian.

Quoted 29 lines

Regards,
Lang

Quoted 27 lines

That can lead to all kind of problems and is clearly illegal.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Well then you need to figure out where that memory is freed again and place the unpin appropriately.

General rule of thumb is that calls to amdgpu_bo_pin/amdgpu_bo_unpin should be balanced.

Regards,
Christian.

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
     	ret = unreserve_bo_and_vms(&ctx, false, false);
 
     	/* Remove from VM internal data structures */
author
#8 lore raw

[AMD Official Use Only]

Quoted 109 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, September 24, 2021 1:42 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 24.09.21 um 07:35 schrieb Yu, Lang:

Quoted 43 lines

[AMD Official Use Only]

Quoted 42 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 10:52 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 16:24 schrieb Yu, Lang:

Quoted 33 lines

[AMD Official Use Only]

Quoted 32 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 8:24 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 14:09 schrieb Yu, Lang:

Quoted 23 lines

[AMD Official Use Only]

Quoted 19 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 8 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0

[amdgpu]

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 37 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
     drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
     1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 4 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int

amdgpu_amdkfd_gpuvm_free_memory_of_gpu(

Quoted 9 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
     	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
     		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

What amdgpu_bo_unpin() does is the following:

        ttm_bo_unpin(&bo->tbo);
        if (bo->tbo.pin_count)
                return;
....

Quoted 5 lines

In other words we take further actions based on if the buffer us is still pinned or not after an unpin operation.

What you try to do here is unpinning the BO when it is pinned independent if somebody else or our code has pinned it before.

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

Quoted 4 lines
Quoted 2 lines

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Well not with a flag or something like that.

Quoted 8 lines

The knowledge which BO is pinned and needs to be unpinned should come from the control logic and not be papered over by some general handling. That's the background why we have removed the general handling for this from TTM in the first place.

In other words when you need to pin a BO because it is kmapped it should be unpinned when it is kunmapped and if you don't kunmap at all then there is something wrong with the code structure from a higher level point of view.

Yes, this function "amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel" did a kmap,
but without a kunmap when the kfd process is destroyed. The flag actually indicates kmap/kunmap.

Regards,
Lang

Quoted 39 lines

Regards,
Christian.

Quoted 36 lines

Regards,
Lang

Quoted 32 lines

Regards,
Christian.

Quoted 29 lines

Regards,
Lang

Quoted 27 lines

That can lead to all kind of problems and is clearly illegal.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

Well then you need to figure out where that memory is freed again and place the unpin appropriately.

General rule of thumb is that calls to
amdgpu_bo_pin/amdgpu_bo_unpin should be balanced.

Regards,
Christian.

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
     	ret = unreserve_bo_and_vms(&ctx, false, false);
 
     	/* Remove from VM internal data structures */
maintainer
#9 lore raw

Am 24.09.21 um 07:50 schrieb Yu, Lang:

Quoted 20 lines

[AMD Official Use Only]

Quoted 19 lines

[SNIP]

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Well not with a flag or something like that.

The knowledge which BO is pinned and needs to be unpinned should come from the control logic and not be papered over by some general handling. That's the background why we have removed the general handling for this from TTM in the first place.

Quoted 5 lines
Quoted 3 lines

In other words when you need to pin a BO because it is kmapped it should be unpinned when it is kunmapped and if you don't kunmap at all then there is something wrong with the code structure from a higher level point of view.

Yes, this function "amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel" did a kmap,
but without a kunmap when the kfd process is destroyed. The flag actually indicates kmap/kunmap.

Well that's the wrong approach then. I mean you need to have the BO reference and the mapped pointer somewhere, don't you?

How do you clean those up?

Regards,
Christian.

author
#10 lore raw

[AMD Official Use Only]

Quoted 34 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, September 24, 2021 1:54 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 24.09.21 um 07:50 schrieb Yu, Lang:

Quoted 23 lines

[AMD Official Use Only]

Quoted 22 lines

[SNIP]

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Well not with a flag or something like that.

The knowledge which BO is pinned and needs to be unpinned should come from the control logic and not be papered over by some general handling. That's the background why we have removed the general handling for this from TTM in the first place.

In other words when you need to pin a BO because it is kmapped it should be unpinned when it is kunmapped and if you don't kunmap at all then there is something wrong with the code structure from a higher level

point of view.

Quoted 8 lines
Quoted 2 lines

Yes, this function "amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel" did a kmap, but without a kunmap when the kfd process is destroyed. The flag

actually indicates kmap/kunmap.

Well that's the wrong approach then. I mean you need to have the BO reference and the mapped pointer somewhere, don't you?

How do you clean those up?

They are respectively cleaned by " kfd_process_device_free_bos " and " kfd_process_destroy_pdds". Let me put the code here. Thanks!

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +3 -0
index ec028cf963f5..d65b3bf13fd8 100644
@@ -81,6 +81,7 @@ struct kgd_mem {
 
        bool aql_queue;
        bool is_imported;
+       bool is_mapped_to_kernel;
 };
 
 /* KFD Memory Eviction */
@@ -278,6 +279,8 @@ int amdgpu_amdkfd_gpuvm_sync_memory(
                struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
                struct kgd_mem *mem, void **kptr, uint64_t *size);
+void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_dev *kgd,
+               struct kgd_mem *mem);
 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
                                            struct dma_fence **ef);
 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +16 -0
index 2d6b2d77b738..45ccbe9f63ee 100644
@@ -1857,6 +1857,8 @@ int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
 
        amdgpu_bo_unreserve(bo);
 
+       mem->is_mapped_to_kernel = true;
+
        mutex_unlock(&mem->process_info->lock);
        return 0;
 
@@ -1870,6 +1872,20 @@ int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
        return ret;
 }
 
+void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_dev *kgd, struct kgd_mem *mem)
+{
+       struct amdgpu_bo *bo = mem->bo;
+
+       if (!mem->is_mapped_to_kernel)
+               return;
+
+       amdgpu_bo_reserve(bo, true);
+       amdgpu_bo_kunmap(bo);
+       amdgpu_bo_unpin(bo);
+       amdgpu_bo_unreserve(bo);
+       mem->is_mapped_to_kernel = false;
+}
+
 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
                                              struct kfd_vm_fault_info *mem)
 {
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
index 21ec8a18cad2..f5506b153aed 100644
@@ -941,6 +941,8 @@ static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
                                peer_pdd->dev->kgd, mem, peer_pdd->drm_priv);
                }
 
+               amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(pdd->dev->kgd, mem);
+
                amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem,
                                                       pdd->drm_priv, NULL);
                kfd_process_device_remove_obj_handle(pdd, id);

Regards,
Lang

Quoted 2 lines

Regards,
Christian.

#11 lore raw

Am 24.09.21 um 08:34 schrieb Yu, Lang:

Quoted 54 lines

[AMD Official Use Only]

Quoted 34 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, September 24, 2021 1:54 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 24.09.21 um 07:50 schrieb Yu, Lang:

Quoted 23 lines

[AMD Official Use Only]

Quoted 22 lines

[SNIP]

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process lifecycle. I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Well not with a flag or something like that.

The knowledge which BO is pinned and needs to be unpinned should come from the control logic and not be papered over by some general handling. That's the background why we have removed the general handling for this from TTM in the first place.

In other words when you need to pin a BO because it is kmapped it should be unpinned when it is kunmapped and if you don't kunmap at all then there is something wrong with the code structure from a higher level

point of view.

Quoted 8 lines
Quoted 2 lines

Yes, this function "amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel" did a kmap, but without a kunmap when the kfd process is destroyed. The flag

actually indicates kmap/kunmap.

Well that's the wrong approach then. I mean you need to have the BO reference and the mapped pointer somewhere, don't you?

How do you clean those up?

They are respectively cleaned by " kfd_process_device_free_bos " and " kfd_process_destroy_pdds". Let me put the code here. Thanks!

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
index ec028cf963f5..d65b3bf13fd8 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h struct kgd_mem { · line 84

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
@@ -81,6 +81,7 @@ struct kgd_mem {
 
         bool aql_queue;
         bool is_imported;
+       bool is_mapped_to_kernel;

Yeah, that is exactly what you absolutely should NOT do.

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int amdgpu_amdkfd_gpuvm_sync_memory( · line 281

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
  };
 
  /* KFD Memory Eviction */
@@ -278,6 +279,8 @@ int amdgpu_amdkfd_gpuvm_sync_memory(
                 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
  int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
                 struct kgd_mem *mem, void **kptr, uint64_t *size);

The real question is who is calling this function here?

Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -0
+void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_dev *kgd,
+               struct kgd_mem *mem);
  int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
                                             struct dma_fence **ef);
  int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +16 -0
index 2d6b2d77b738..45ccbe9f63ee 100644
@@ -1857,6 +1857,8 @@ int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
 
         amdgpu_bo_unreserve(bo);
 
+       mem->is_mapped_to_kernel = true;
+
         mutex_unlock(&mem->process_info->lock);
         return 0;
 
@@ -1870,6 +1872,20 @@ int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
         return ret;
  }
 
+void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_dev *kgd, struct kgd_mem *mem)
+{
+       struct amdgpu_bo *bo = mem->bo;
+
+       if (!mem->is_mapped_to_kernel)
+               return;
+
+       amdgpu_bo_reserve(bo, true);
+       amdgpu_bo_kunmap(bo);
+       amdgpu_bo_unpin(bo);
+       amdgpu_bo_unreserve(bo);
+       mem->is_mapped_to_kernel = false;
+}
+
  int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
                                               struct kfd_vm_fault_info *mem)
  {
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index 21ec8a18cad2..f5506b153aed 100644

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_device_free_bos(struct kfd_process_device *pdd) · line 945

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
@@ -941,6 +941,8 @@ static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
                                 peer_pdd->dev->kgd, mem, peer_pdd->drm_priv);
                 }
 
+               amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(pdd->dev->kgd, mem);
+

That's a general cleanup function for user space allocations and should not be abused for stuff like that.

Regards,
Christian.

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
                 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem,
                                                        pdd->drm_priv, NULL);
                 kfd_process_device_remove_obj_handle(pdd, id);

Regards,
Lang

Quoted 2 lines

Regards,
Christian.

author
#12 lore raw

[AMD Official Use Only]

Quoted 79 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Friday, September 24, 2021 2:37 PM
To: Yu, Lang <[email protected]>; Koenig, Christian <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 24.09.21 um 08:34 schrieb Yu, Lang:

Quoted 20 lines

[AMD Official Use Only]

Quoted 16 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, September 24, 2021 1:54 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 24.09.21 um 07:50 schrieb Yu, Lang:

Quoted 6 lines

[AMD Official Use Only]

Quoted 5 lines

[SNIP]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Hi Christian,

Thanks for your explanation and advice. I got your point. Actually, these BOs are allocated and pinned during a kfd process

lifecycle.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I will try to add a flag into struct kgd_mem to indicate which BO is pined and should be unpinned. Which will make amdgpu_bo_pin/amdgpu_bo_unpin calls balanced. Thanks!

That isn't to much better. The real solution would be to unpin them when the kfd process is destroyed.

Yes, will unpin them when the kfd process is destroyed.
But we should indicate which BO is pinned and should be unpinned. Right?

Well not with a flag or something like that.

The knowledge which BO is pinned and needs to be unpinned should come from the control logic and not be papered over by some general

handling.

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines

That's the background why we have removed the general handling for this from TTM in the first place.

In other words when you need to pin a BO because it is kmapped it should be unpinned when it is kunmapped and if you don't kunmap at all then there is something wrong with the code structure from a higher level

point of view.

Quoted 3 lines

Yes, this function "amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel" did a kmap, but without a kunmap when the kfd process is destroyed. The flag

Quoted 7 lines
Quoted 6 lines

actually indicates kmap/kunmap.

Well that's the wrong approach then. I mean you need to have the BO reference and the mapped pointer somewhere, don't you?

How do you clean those up?

They are respectively cleaned by " kfd_process_device_free_bos " and "

kfd_process_destroy_pdds".

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h struct kgd_mem { · line 84

Quoted 12 lines

Let me put the code here. Thanks!

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
index ec028cf963f5..d65b3bf13fd8 100644
@@ -81,6 +81,7 @@ struct kgd_mem {
 
         bool aql_queue;
         bool is_imported;
+       bool is_mapped_to_kernel;

Yeah, that is exactly what you absolutely should NOT do.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
  };
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int amdgpu_amdkfd_gpuvm_sync_memory( · line 281

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
  /* KFD Memory Eviction */
@@ -278,6 +279,8 @@ int amdgpu_amdkfd_gpuvm_sync_memory(
                 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
  int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
                 struct kgd_mem *mem, void **kptr, uint64_t *size);

The real question is who is calling this function here?

Currently there are 3 places called function "amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel" to kmap a BO.

1, kmap a ptr for pdd->qpd->cwsr_kaddr
Call stack:
amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel
kfd_process_alloc_gpuvm
kfd_process_device_init_cwsr_dgpu
kfd_process_device_init_vm
kfd_ioctl_acquire_vm

2, kmap a ptr for pdd->qpd->ib_kaddr
Call stack:
amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel
kfd_process_alloc_gpuvm
kfd_process_device_reserve_ib_mem
kfd_process_device_init_vm
kfd_ioctl_acquire_vm

3, kmap a ptr for p->signal_page->kernel_address
Call stack:
amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel
kfd_ioctl_create_event

The problem is these kmaped BOs were not kunmaped properly when the kfd process is destroyed.

Regards,
Lang

Quoted 71 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int amdgpu_amdkfd_gpuvm_sync_memory( · line 282

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
+void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_dev

*kgd,

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
+               struct kgd_mem *mem);
  int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
                                             struct dma_fence **ef);
  int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd, diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index 2d6b2d77b738..45ccbe9f63ee 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
@@ -1857,6 +1857,8 @@ int
 amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
 
         amdgpu_bo_unreserve(bo);
 
+       mem->is_mapped_to_kernel = true;
+
         mutex_unlock(&mem->process_info->lock);
         return 0;
@@ -1870,6 +1872,20 @@ int

amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_device_free_bos(struct

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +13 -0
         return ret;
  }
 
+void amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(struct kgd_dev
+*kgd, struct kgd_mem *mem) {
+       struct amdgpu_bo *bo = mem->bo;
+
+       if (!mem->is_mapped_to_kernel)
+               return;
+
+       amdgpu_bo_reserve(bo, true);
+       amdgpu_bo_kunmap(bo);
+       amdgpu_bo_unpin(bo);
+       amdgpu_bo_unreserve(bo);
+       mem->is_mapped_to_kernel = false; }
+
  int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
                                               struct kfd_vm_fault_info *mem)
  {
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index 21ec8a18cad2..f5506b153aed 100644
@@ -941,6 +941,8 @@ static void kfd_process_device_free_bos(struct

kfd_process_device *pdd)

drivers/gpu/drm/amd/amdkfd/kfd_process.c static void kfd_process_device_free_bos(struct kfd_process_device *pdd) · line 945

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
                                 peer_pdd->dev->kgd, mem, peer_pdd->drm_priv);
                 }
 
+
+ amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(pdd->dev->kgd, mem);
+

That's a general cleanup function for user space allocations and should not be abused for stuff like that.

Regards,
Christian.

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
                 amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem,
                                                        pdd->drm_priv, NULL);
                 kfd_process_device_remove_obj_handle(pdd, id);

Regards,
Lang

Quoted 2 lines

Regards,
Christian.

maintainer
#13 lore raw
In reply to Yu, Lang
Quoted 53 lines · Lang

[AMD Official Use Only]

Quoted 42 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 17 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30

Review

Quoted 6 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
   1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

Quoted 7 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
   	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
   		mem->va + bo_size * (1 + mem->aql_queue));
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);

These two allocations are created by the kernel mode driver. There is another case where a user-allocated BO can get pinned because we need to kmap it (in kfd_ioctl_create_event).

Regards,
  Felix

Quoted 8 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
   	ret = unreserve_bo_and_vms(&ctx, false, false);
 
   	/* Remove from VM internal data structures */
author
#14 lore raw

[AMD Official Use Only]

Quoted 73 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Friday, September 24, 2021 12:21 AM
To: Yu, Lang <[email protected]>; Koenig, Christian <[email protected]>; [email protected]
Cc: Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Quoted 37 lines · Lang

[AMD Official Use Only]

Quoted 33 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Thursday, September 23, 2021 7:40 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Christian K nig <[email protected]>; Huang, Ray <[email protected]>
Subject: Re: [PATCH] drm/kfd: fix ttm_bo_release warning

Am 23.09.21 um 11:44 schrieb Lang Yu:

Quoted 22 lines

If a BO is pinned, unpin it before freeing it.

Call Trace:
	ttm_bo_put+0x30/0x50 [ttm]
	amdgpu_bo_unref+0x1e/0x30 [amdgpu]
	amdgpu_gem_object_free+0x34/0x50 [amdgpu]
	drm_gem_object_free+0x1d/0x30 [drm]
	amdgpu_amdkfd_gpuvm_free_memory_of_gpu+0x31f/0x3a0 [amdgpu]
	kfd_process_device_free_bos+0xa3/0xf0 [amdgpu]
	kfd_process_wq_release+0x224/0x2e0 [amdgpu]
	process_one_work+0x220/0x3c0
	worker_thread+0x4d/0x3f0
	kthread+0x114/0x150
	process_one_work+0x3c0/0x3c0
	kthread_park+0x90/0x90
	ret_from_fork+0x22/0x30
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 +++
   1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 4 lines

index 2d6b2d77b738..7e693b064072 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -1567,6 +1567,9 @@ int

amdgpu_amdkfd_gpuvm_free_memory_of_gpu(

Quoted 10 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1572

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -0
   	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
   		mem->va + bo_size * (1 + mem->aql_queue));
 
+	if (mem->bo->tbo.pin_count)
+		amdgpu_bo_unpin(mem->bo);
+

NAK, using mem->bo->tbo.pin_count like this is illegal.

I didn't get your point. I referred to function-"void amdgpu_bo_unpin(struct amdgpu_bo *bo)", which uses it like this.

Quoted 8 lines
Quoted 1 line

Where was the BO pinned in the first place?

I found two places:

	ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
				      &kaddr);

	ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
				      KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
Quoted 6 lines

These two allocations are created by the kernel mode driver. There is another case where a user-allocated BO can get pinned because we need to kmap it (in kfd_ioctl_create_event).

Regards,
  Felix

Yes, these BOs will not be freed until a kfd process is destroyed. I will make a v2 patch, please help review. Thanks!

Regards,
Lang

Quoted 9 lines
Quoted 9 lines

Regards,
Lang

Quoted 5 lines

Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
   	ret = unreserve_bo_and_vms(&ctx, false, false);
 
   	/* Remove from VM internal data structures */