AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Kuehling, Felix" <Felix.Kuehling-5C7GfCeVMHo@public.gmane.org>
To: "StDenis, Tom" <Tom.StDenis-5C7GfCeVMHo@public.gmane.org>,
	"amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org"
	<amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org>
Subject: Re: [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v3)
Date: Wed, 12 Jun 2019 18:02:31 +0000	[thread overview]
Message-ID: <977f7a62-9d5c-ded8-8c03-7950e095bde3@amd.com> (raw)
In-Reply-To: <20190612110829.4699-1-tom.stdenis-5C7GfCeVMHo@public.gmane.org>

On 2019-06-12 4:08, StDenis, Tom wrote:
> (v2): Return 0 and set mem->mm_node to NULL.
> (v3): Use atomic64_add_return instead.
>
> Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
> ---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 17 ++++++++++++-----
>   1 file changed, 12 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..c963ad86072e 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,13 @@ 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 */
> +	if (atomic64_add_return(mem->num_pages << PAGE_SHIFT, &mgr->usage) > adev->gmc.mc_vram_size) {
> +		atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);

On 32-bit systems mem->num_pages is only 32-bit (unsigned long) and the 
<< PAGE_SHIFT may overflow. You probably want to cast this to u64 
explicitly before the shift.

Regards,
   Felix


> +		mem->mm_node = NULL;
> +		return 0;
> +	}
> +
>   	if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
>   		pages_per_node = ~0ul;
>   		num_nodes = 1;
> @@ -300,8 +307,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 +330,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 +349,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 +365,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

      parent reply	other threads:[~2019-06-12 18:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-12 11:08 [PATCH] drm/amd/amdgpu: Bail out of BO node creation if not enough VRAM (v3) StDenis, Tom
     [not found] ` <20190612110829.4699-1-tom.stdenis-5C7GfCeVMHo@public.gmane.org>
2019-06-12 11:57   ` Christian König
2019-06-12 18:02   ` Kuehling, Felix [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=977f7a62-9d5c-ded8-8c03-7950e095bde3@amd.com \
    --to=felix.kuehling-5c7gfcevmho@public.gmane.org \
    --cc=Tom.StDenis-5C7GfCeVMHo@public.gmane.org \
    --cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox