All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v2)
@ 2019-06-11 16:54 StDenis, Tom
       [not found] ` <20190611165444.17841-1-tom.stdenis-5C7GfCeVMHo@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: StDenis, Tom @ 2019-06-11 16:54 UTC (permalink / raw)
  To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW; +Cc: StDenis, Tom

(v2): Return 0 and set mem->mm_node to NULL.

Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
index 8aea2f21b202..c2730e5c1081 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
@@ -276,7 +276,7 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 	struct drm_mm_node *nodes;
 	enum drm_mm_insert_mode mode;
 	unsigned long lpfn, num_nodes, pages_per_node, pages_left;
-	uint64_t usage = 0, vis_usage = 0;
+	uint64_t vis_usage = 0;
 	unsigned i;
 	int r;
 
@@ -284,6 +284,14 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 	if (!lpfn)
 		lpfn = man->size;
 
+	/* bail out quickly if there's likely not enough VRAM for this BO */
+	atomic64_add(mem->num_pages << PAGE_SHIFT, &mgr->usage);
+	if (atomic64_read(&mgr->usage) > adev->gmc.mc_vram_size) {
+		atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
+		mem->mm_node = NULL;
+		return 0;
+	}
+
 	if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
 		pages_per_node = ~0ul;
 		num_nodes = 1;
@@ -300,8 +308,10 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 
 	nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
 			       GFP_KERNEL | __GFP_ZERO);
-	if (!nodes)
+	if (!nodes) {
+		atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
 		return -ENOMEM;
+	}
 
 	mode = DRM_MM_INSERT_BEST;
 	if (place->flags & TTM_PL_FLAG_TOPDOWN)
@@ -321,7 +331,6 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 		if (unlikely(r))
 			break;
 
-		usage += nodes[i].size << PAGE_SHIFT;
 		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
 		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
 		pages_left -= pages;
@@ -341,14 +350,12 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 		if (unlikely(r))
 			goto error;
 
-		usage += nodes[i].size << PAGE_SHIFT;
 		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
 		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
 		pages_left -= pages;
 	}
 	spin_unlock(&mgr->lock);
 
-	atomic64_add(usage, &mgr->usage);
 	atomic64_add(vis_usage, &mgr->vis_usage);
 
 	mem->mm_node = nodes;
@@ -359,6 +366,7 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
 	while (i--)
 		drm_mm_remove_node(&nodes[i]);
 	spin_unlock(&mgr->lock);
+	atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
 
 	kvfree(nodes);
 	return r == -ENOSPC ? 0 : r;
-- 
2.21.0

_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v2)
       [not found] ` <20190611165444.17841-1-tom.stdenis-5C7GfCeVMHo@public.gmane.org>
@ 2019-06-12  7:26   ` Michel Dänzer
       [not found]     ` <fd4c5152-2670-8ac5-e6cc-fff6f260f3ed-otUistvHUpPR7s880joybQ@public.gmane.org>
  2019-06-12  8:00   ` Christian König
  1 sibling, 1 reply; 4+ messages in thread
From: Michel Dänzer @ 2019-06-12  7:26 UTC (permalink / raw)
  To: StDenis, Tom; +Cc: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW

On 2019-06-11 6:54 p.m., StDenis, Tom wrote:
> (v2): Return 0 and set mem->mm_node to NULL.
> 
> Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
> 
> [...]
>  
> @@ -284,6 +284,14 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>  	if (!lpfn)
>  		lpfn = man->size;
>  
> +	/* bail out quickly if there's likely not enough VRAM for this BO */
> +	atomic64_add(mem->num_pages << PAGE_SHIFT, &mgr->usage);
> +	if (atomic64_read(&mgr->usage) > adev->gmc.mc_vram_size) {

Should probably use atomic64_add_return instead of atomic64_add +
atomic64_read.

Also, AFAICT this doesn't allow any VRAM overcommit, which seems a bit
conservative?


-- 
Earthling Michel Dänzer               |              https://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v2)
       [not found] ` <20190611165444.17841-1-tom.stdenis-5C7GfCeVMHo@public.gmane.org>
  2019-06-12  7:26   ` Michel Dänzer
@ 2019-06-12  8:00   ` Christian König
  1 sibling, 0 replies; 4+ messages in thread
From: Christian König @ 2019-06-12  8:00 UTC (permalink / raw)
  To: StDenis, Tom, amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW

Am 11.06.19 um 18:54 schrieb StDenis, Tom:
> (v2): Return 0 and set mem->mm_node to NULL.
>
> Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
> ---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 18 +++++++++++++-----
>   1 file changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
> index 8aea2f21b202..c2730e5c1081 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c
> @@ -276,7 +276,7 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>   	struct drm_mm_node *nodes;
>   	enum drm_mm_insert_mode mode;
>   	unsigned long lpfn, num_nodes, pages_per_node, pages_left;
> -	uint64_t usage = 0, vis_usage = 0;
> +	uint64_t vis_usage = 0;
>   	unsigned i;
>   	int r;
>   
> @@ -284,6 +284,14 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>   	if (!lpfn)
>   		lpfn = man->size;
>   
> +	/* bail out quickly if there's likely not enough VRAM for this BO */
> +	atomic64_add(mem->num_pages << PAGE_SHIFT, &mgr->usage);
> +	if (atomic64_read(&mgr->usage) > adev->gmc.mc_vram_size) {

You should use atomic64_add_return here instead of two operations. 
Otherwise using an atomic doesn't make to much sense.

Christian.

> +		atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
> +		mem->mm_node = NULL;
> +		return 0;
> +	}
> +
>   	if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
>   		pages_per_node = ~0ul;
>   		num_nodes = 1;
> @@ -300,8 +308,10 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>   
>   	nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
>   			       GFP_KERNEL | __GFP_ZERO);
> -	if (!nodes)
> +	if (!nodes) {
> +		atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
>   		return -ENOMEM;
> +	}
>   
>   	mode = DRM_MM_INSERT_BEST;
>   	if (place->flags & TTM_PL_FLAG_TOPDOWN)
> @@ -321,7 +331,6 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>   		if (unlikely(r))
>   			break;
>   
> -		usage += nodes[i].size << PAGE_SHIFT;
>   		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
>   		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
>   		pages_left -= pages;
> @@ -341,14 +350,12 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>   		if (unlikely(r))
>   			goto error;
>   
> -		usage += nodes[i].size << PAGE_SHIFT;
>   		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
>   		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
>   		pages_left -= pages;
>   	}
>   	spin_unlock(&mgr->lock);
>   
> -	atomic64_add(usage, &mgr->usage);
>   	atomic64_add(vis_usage, &mgr->vis_usage);
>   
>   	mem->mm_node = nodes;
> @@ -359,6 +366,7 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>   	while (i--)
>   		drm_mm_remove_node(&nodes[i]);
>   	spin_unlock(&mgr->lock);
> +	atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
>   
>   	kvfree(nodes);
>   	return r == -ENOSPC ? 0 : r;

_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v2)
       [not found]     ` <fd4c5152-2670-8ac5-e6cc-fff6f260f3ed-otUistvHUpPR7s880joybQ@public.gmane.org>
@ 2019-06-12  8:03       ` Christian König
  0 siblings, 0 replies; 4+ messages in thread
From: Christian König @ 2019-06-12  8:03 UTC (permalink / raw)
  To: Michel Dänzer, StDenis, Tom; +Cc: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW

Am 12.06.19 um 09:26 schrieb Michel Dänzer:
> On 2019-06-11 6:54 p.m., StDenis, Tom wrote:
>> (v2): Return 0 and set mem->mm_node to NULL.
>>
>> Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
>> ---
>>   drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 18 +++++++++++++-----
>>   1 file changed, 13 insertions(+), 5 deletions(-)
>>
>> [...]
>>   
>> @@ -284,6 +284,14 @@ static int amdgpu_vram_mgr_new(struct ttm_mem_type_manager *man,
>>   	if (!lpfn)
>>   		lpfn = man->size;
>>   
>> +	/* bail out quickly if there's likely not enough VRAM for this BO */
>> +	atomic64_add(mem->num_pages << PAGE_SHIFT, &mgr->usage);
>> +	if (atomic64_read(&mgr->usage) > adev->gmc.mc_vram_size) {
> Should probably use atomic64_add_return instead of atomic64_add +
> atomic64_read.
>
> Also, AFAICT this doesn't allow any VRAM overcommit, which seems a bit
> conservative?

This is the low level VRAM manager. Here we can't overcomit anyway and 
need to return early for TTM to evict things.

Christian.
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-06-12  8:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-11 16:54 [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v2) StDenis, Tom
     [not found] ` <20190611165444.17841-1-tom.stdenis-5C7GfCeVMHo@public.gmane.org>
2019-06-12  7:26   ` Michel Dänzer
     [not found]     ` <fd4c5152-2670-8ac5-e6cc-fff6f260f3ed-otUistvHUpPR7s880joybQ@public.gmane.org>
2019-06-12  8:03       ` Christian König
2019-06-12  8:00   ` Christian König

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.