AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Xiaogang.Chen <xiaogang.chen@amd.com>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH v5 3/3] drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver
Date: Sun, 22 Nov 2026 20:51:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-228336@kernel.org> (raw)
In-Reply-To: <20261122202653.274580-4-xiaogang.chen@amd.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/amdkfd: broken out_oom cleanup and THP DMA unmap in
  svm_migrate_copy_to_ram()
- [High] drm/amdkfd: svm_migrate_vram_busy() races with compound folio
  teardown
- [Medium] drm/amdkfd: uninitialized tail VRAM offsets in
  svm_migrate_copy_to_vram()
- [Medium] drm/amdkfd: CPU PMD fault 4K fallback in
  svm_migrate_copy_to_ram() fails folio split
--

commit 0bbf4e6ec6f1a6c7fb1fece90e6a6372e1d80249
Author: Xiaogang Chen <xiaogang.chen@amd.com>

drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver

This commit enables compound THP migration for device-private SVM ranges by
setting MIGRATE_VMA_SELECT_COMPOUND when the migration window is at least
SVM_HPAGE_PMD_NR pages and handling MIGRATE_PFN_COMPOUND entries in the
RAM-to-VRAM and VRAM-to-RAM copy paths.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> index 11cda92b88783..aeb74d812c6c8 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> @@ -340,6 +340,153 @@ static unsigned long svm_migrate_successful_pages(struct migrate_vma *migrate)
>  	return mpages;
>  }
>  
> +/*
> + * A reused svm_bo VRAM page may still be held by someone else, ex:
> + * (do_swap_page -> svm_migrate_to_ram). Migrating to VRAM re-initializes
> + * the page and resets its refcount, so the other's put_page() would
> + * free it mid-migration. Report whether any of the @nr VRAM pages starting
> + * at page offset @m of @cursor is still referenced. @cursor is a copy, so
> + * the range may cross VRAM blocks without moving the caller's cursor.
> + *
> + * Check the folio, not the page: a tail page of a live device THP always
> + * has a zero refcount while its folio is held.
> + */
> +static bool
> +svm_migrate_vram_busy(struct amdgpu_device *adev,
> +		      struct amdgpu_res_cursor cursor, u64 m, u64 nr)
> +{
> +	while (nr) {
> +		u64 chunk = cursor.size >> PAGE_SHIFT;
> +		struct page *page;
> +
> +		if (m >= chunk) {
> +			if (cursor.remaining <= cursor.size)
> +				return false;
> +			amdgpu_res_next(&cursor, cursor.size);
> +			m -= chunk;
> +			continue;
> +		}
> +
> +		page = pfn_to_page(svm_migrate_addr_to_pfn(adev,
> +					cursor.start + (m << PAGE_SHIFT)));
> +		if (folio_ref_count(page_folio(page)))
> +			return true;
> +		m++;
> +		nr--;
> +	}
> +
> +	return false;
> +}

[Severity: High]
Can svm_migrate_vram_busy() return false while a previous compound device
folio on the same svm_bo is still being torn down in
svm_migrate_folio_free()?

When a CPU fault finishes svm_migrate_to_ram() and drops its reference outside
prange->migrate_mutex, folio_put_testzero() zeroes the folio refcount before
svm_migrate_folio_free() clears the compound pages and drops the svm_bo
reference:

CPU0                                  CPU1
do_huge_pmd_device_private()
  svm_migrate_to_ram()
    ...
    mutex_unlock(&prange->migrate_mutex)
  folio_put(folio)
    folio_put_testzero(folio)
    /* refcount is now 0 */
                                      svm_migrate_to_vram()
                                        /* reuses prange->svm_bo */
                                        svm_migrate_copy_to_vram()
                                          svm_migrate_vram_busy() -> false
                                          svm_migrate_get_vram_page(..., order)
                                            zone_device_folio_init()
    free_zone_device_folio()
      svm_migrate_folio_free()
        svm_migrate_clear_compound_pages()
          /* tail pages have page_count == 0 */
          new_folio->pgmap = pgmap;  <-- corrupts live compound tail pages

Because svm_migrate_clear_compound_pages() only skips pages with a non-zero
page_count(page), and all tail pages of the newly initialized compound folio
have a page count of 0, the teardown loop wipes the new folio's compound
order, _nr_pages, and tail page compound_head pointers.

[ ... ]

> @@ -363,69 +510,251 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,
>  
>  	amdgpu_res_first(prange->ttm_res, ttm_res_offset,
>  			 npages << PAGE_SHIFT, &cursor);

[ ... ]

> +		if (thp_dst) {
> +			dst[i] = cursor.start + (m << PAGE_SHIFT);
> +			svm_migrate_get_vram_page(prange, cur_dst_pfn,
> +						  SVM_HPAGE_PMD_ORDER);
> +
> +			migrate->dst[i] = migrate_pfn(cur_dst_pfn) |
> +					  MIGRATE_PFN_COMPOUND;
> +
> +			for (l = 1; l < k; l++)
> +				migrate->dst[i + l] = 0;
> +
> +			mpages++;

[ ... ]

> +				/* get dma address for following SVM_HPAGE_PMD_NR-1 pages
> +				 * since src pages are continuous their dma addresses
> +				 * are continuous too.
> +				 */
> +				for (l = 1; l < k; l++)
> +					src[i + l] = src[i] + l*PAGE_SIZE;
> +
> +				/* migrate the SVM_HPAGE_PMD_NR pages above */
> +				r = svm_migrate_copy_memory_gart(
> +						adev, src + i,
> +						dst + i, SVM_HPAGE_PMD_NR,
> +						FROM_RAM_TO_VRAM,
> +						mfence);

[Severity: Medium]
Does the thp_dst path leave dst[i + 1 .. i + k - 1] as 0 before calling
svm_migrate_copy_memory_gart()?

svm_migrate_copy_memory_gart() splits transfers larger than GTT_MAX_PAGES
(4 MiB) and advances vram += size on each chunk, dereferencing *vram for the
destination VRAM offset.  On architectures supported by CONFIG_HSA_AMD where
HPAGE_PMD_SIZE exceeds 4 MiB (such as arm64, ppc64, or loongarch with larger
base page sizes), every chunk after the first 4 MiB reads *vram == 0 and DMA
copies the remainder of the THP to VRAM offset 0.

[ ... ]

> @@ -762,7 +1155,58 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,

[ ... ]

> +			/*
> +			 * No THP destination (allocation failed or THP is
> +			 * disabled for the VMA): migrate the whole folio as
> +			 * SVM_HPAGE_PMD_NR 4K pages so migrate_vma_pages() finds
> +			 * a page in every slot after it splits the source.
> +			 */
> +			if (!dpage) {
> +				r = svm_migrate_compound_to_4k_ram(adev, migrate,
> +								   mfence, dst,
> +								   src, i, addr);
> +				if (r)
> +					goto out_oom;
> +				continue;
> +			}
> +		} else {
> +			k = 1;
> +			dpage = svm_migrate_get_sys_page(migrate->vma, addr, 0);
> +		}
> +
>  		if (!dpage) {
>  			pr_debug("failed get page svms 0x%p [0x%lx 0x%lx]\n",
>  				 prange->svms, prange->start, prange->last);
> @@ -770,7 +1214,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
>  			goto out_oom;
>  		}
>  
> -		dst[i] = dma_map_page(dev, dpage, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
> +		dst[i] = dma_map_page(dev, dpage, 0, PAGE_SIZE*k, DMA_BIDIRECTIONAL);
>  		r = dma_mapping_error(dev, dst[i]);
>  		if (r) {
>  			dev_err(adev->dev, "%s: fail %d dma_map_page\n", __func__, r);
> @@ -778,13 +1222,40 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
>  			goto out_oom;
>  		}
>  
> -		pr_debug_ratelimited("dma mapping dst to 0x%llx, pfn 0x%lx\n",
> -				     dst[i] >> PAGE_SHIFT, page_to_pfn(dpage));
> -
>  		migrate->dst[i] = migrate_pfn(page_to_pfn(dpage));
> -
>  		dpage = NULL;
> -		j++;
> +
> +		if (k > 1) {
> +			migrate->dst[i] |= MIGRATE_PFN_COMPOUND;
> +
> +			for (l = 1; l < k; l++) {
> +
> +				src[i + l] = src[i] + l*PAGE_SIZE;
> +				dst[i + l] = dst[i] + l*PAGE_SIZE;
> +				migrate->dst[i + l] = 0;
> +			}
> +
> +			/* migrate the SVM_HPAGE_PMD_NR pages above
> +			 * the migration is for 2MB THP
> +			 */
> +			r = svm_migrate_copy_memory_gart(
> +						adev, dst + i,
> +						src + i, SVM_HPAGE_PMD_NR,
> +						FROM_VRAM_TO_RAM,
> +						mfence);
> +
> +			/* mark head page dma mapping as THP, tail pages dma addr
> +			 * are set to 0 for following dma_unmap
> +			 */
> +			dst[i] |= SVM_RANGE_DMA_THP;
> +			for (l = 1; l < k; l++)
> +				dst[i + l] = 0;
> +
> +			if (r)
> +				goto out_oom;

[Severity: High]
As reported by Philip Yang on v3 [1], does jumping to out_oom here or from the
svm_migrate_compound_to_4k_ram() fallback leave migrate->dst[i .. i + k - 1]
populated?

In both paths dpage is already NULL and i has not been advanced yet, so the
while (i--) cleanup loop in out_oom only visits indices 0 .. i - 1:

drivers/gpu/drm/amd/amdkfd/kfd_migrate.c:svm_migrate_copy_to_ram() {
    ...
		/* release previous allocated sys pages and unmap dma address */
		while (i--) {

			if (dst[i]) {
				dma_unmap_page(dev, dst[i], PAGE_SIZE,
					       DMA_BIDIRECTIONAL);
				dst[i] = 0;
			}
    ...
}

Because svm_migrate_vma_to_ram() still calls migrate_vma_pages() and
migrate_vma_finalize() after svm_migrate_copy_to_ram() fails, any pages left
in migrate->dst[i .. i + SVM_HPAGE_PMD_NR - 1] are installed into the process
page tables without valid copied contents.  In addition, if an earlier
iteration mapped a 2MB THP and set SVM_RANGE_DMA_THP on dst[idx], the out_oom
loop passes the untagged address with SVM_RANGE_DMA_THP still set and size
PAGE_SIZE (instead of PAGE_SIZE * SVM_HPAGE_PMD_NR) to dma_unmap_page().

[ ... ]

> @@ -866,6 +1340,10 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,
>  	else
>  		migrate.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
>  
> +	if (SVM_THP_ENABLED && is_private_device &&
> +	    ((end - start) >> PAGE_SHIFT) >= SVM_HPAGE_PMD_NR)
> +		migrate.flags = migrate.flags | MIGRATE_VMA_SELECT_COMPOUND;

[Severity: Medium]
When a CPU huge-PMD fault (do_huge_pmd_device_private() ->
svm_migrate_to_ram()) sets migrate.fault_page and falls back to 4K system
pages in svm_migrate_compound_to_4k_ram(), won't migrate_vma_pages() always
fail to split the compound source folio?

do_huge_pmd_device_private() holds an extra reference on the faulting folio,
and migrate_vma_collect_huge_pmd() takes another reference when collecting it
with MIGRATE_PFN_COMPOUND.  When migrate->dst[i] is populated with 4K pages
instead of MIGRATE_PFN_COMPOUND, migrate_vma_split_unmapped_folio() calls
folio_split_unmapped(), which requires folio_ref_count(folio) == 1 and fails
with -EAGAIN, discarding all 512 allocated and SDMA-copied 4K pages.

[1] https://lore.kernel.org/r/ecc35307-3332-4bee-bdd9-db66948caac9@amd.com

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122202653.274580-1-xiaogang.chen@amd.com?part=3

  reply	other threads:[~2026-11-22 20:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 20:26 [PATCH v5 0/3] drm/amdkfd: Enable device private memory THP support in kfd svm driver Xiaogang.Chen
2026-11-22 20:26 ` [PATCH v5 1/3] drm/amdkfd: Add awareness of THP of device and system RAM " Xiaogang.Chen
2026-11-22 20:49   ` sashiko-bot
2026-11-24 20:32   ` Philip Yang
2026-11-30  7:31     ` Chen, Xiaogang
2026-11-27 21:34   ` Philip Yang
2026-11-30  7:36     ` Chen, Xiaogang
2026-11-30 22:06       ` Philip Yang
2026-11-22 20:26 ` [PATCH v5 2/3] drm/amdkfd: Do not migrate partial THP memory at CPU/GPU page fault handler Xiaogang.Chen
2026-11-22 20:42   ` sashiko-bot
2026-11-22 20:26 ` [PATCH v5 3/3] drm/amdkfd: Enable HMM device-private THP migration in KFD SVM driver Xiaogang.Chen
2026-11-22 20:51   ` sashiko-bot [this message]
2026-11-27 23:23   ` Philip Yang

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=sashiko-outbox-228336@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xiaogang.chen@amd.com \
    /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