Re: [PATCH 2/3] drm/amdkfd: Fix the case that vm range is hole at svm_migrate_copy_to_vram

Felix Kuehling <[email protected]>
Newsgroups org.freedesktop.lists.amd-gfx
Organization AMD Inc.
Message-ID <[email protected]>
On 2026-08-17 09:59, Xiaogang.Chen wrote:
> From: Xiaogang Chen <[email protected]>
>
> When migration vm range is hole at cpu side(MIGRATE_PFN_MIGRATE set +
> MIGRATE_PFN_VALID unset) driver still allocates device pages. There is no
> dma map of src pages and migration. j is 0 and svm_migrate_copy_memory_gart()
> will return an uninitialized r. That can trigger out_free_vram_pages to drop
> all VRAM just set up.

Good catch. The first fix I would expect here, is that 
svm_migrate_copy_memory_gart should never return an uninitialized r. I'd 
defensively initialize r in svm_migrate_copy_memory_gart as well, even 
if you never plan to call it with npages = 0. With that fixed, the patch is

Reviewed-by: Felix Kuehling <[email protected]>


>
> Initialize r and only call the last svm_migrate_copy_memory_gart if j > 0.
>
> Current code postponed the last page to the final copy. This patch flushes on
> the last page when reach to the end of current drm_buddy_block; avoids another
> svm_migrate_copy_memory_gart.
>
> Signed-off-by: Xiaogang Chen <[email protected]>
> ---
>   drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 9 ++++++---
>   1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> index aea82da5f575..263dae49bb3e 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> @@ -284,7 +284,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,
>   	dma_addr_t *src;
>   	u64 *dst;
>   	u64 i, j;
> -	int r;
> +	int r = 0;
>   
>   	pr_debug("svms 0x%p [0x%lx 0x%lx 0x%llx]\n", prange->svms, prange->start,
>   		 prange->last, ttm_res_offset);
> @@ -310,6 +310,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,
>   					      DMA_BIDIRECTIONAL);
>   			r = dma_mapping_error(dev, src[i]);
>   			if (r) {
> +				src[i] = 0;
>   				dev_err(dev, "%s: fail %d dma_map_page\n",
>   					__func__, r);
>   				goto out_free_vram_pages;
> @@ -334,7 +335,8 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,
>   		pr_debug_ratelimited("dma mapping src to 0x%llx, pfn 0x%lx\n",
>   				     src[i] >> PAGE_SHIFT, page_to_pfn(spage));
>   
> -		if (j >= (cursor.size >> PAGE_SHIFT) - 1 && i < npages - 1) {
> +		/* accumulated j + 1 pages reach end of current drm_buddy_block */
> +		if (j + 1 >= (cursor.size >> PAGE_SHIFT)) {
>   			r = svm_migrate_copy_memory_gart(adev, src + i - j,
>   							 dst + i - j, j + 1,
>   							 FROM_RAM_TO_VRAM,
> @@ -348,7 +350,8 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,
>   		}
>   	}
>   
> -	r = svm_migrate_copy_memory_gart(adev, src + i - j, dst + i - j, j,
> +	if (j > 0)
> +		r = svm_migrate_copy_memory_gart(adev, src + i - j, dst + i - j, j,
>   					 FROM_RAM_TO_VRAM, mfence);
>   
>   out_free_vram_pages:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.