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 1/3] drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm driver
Date: Sun, 22 Nov 2026 20:49:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-228335@kernel.org> (raw)
In-Reply-To: <20261122202653.274580-2-xiaogang.chen@amd.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/amdkfd: race and tail refcount bug in
  svm_migrate_clear_compound_pages()
- [Medium] drm/amdkfd: svm_migrate_get_vram_page() calls page_pgmap() on
  stale tail page
- [Medium] drm/amdkfd: VRAM eviction thrashing and defeated fallback in
  svm_range_vram_node_new()
--

commit 19748270f4f63cf5e57c0c13c312409968fa180c
Author: Xiaogang Chen <xiaogang.chen@amd.com>

drm/amdkfd: Add awareness of THP of device and system RAM in kfd svm driver

This commit extends KFD SVM migration and buffer allocation helpers to
support PMD-sized compound folios for device-private VRAM and system RAM.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> index 1eb92ac6eb058..5ead98a604b1e 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> @@ -218,17 +218,45 @@ svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr)
>  	return (addr + adev->kfd.pgmap.range.start) >> PAGE_SHIFT;
>  }
>  
> +/*
> + * TTM can reuse a 2MB VRAM block that was previously a device THP.
> + * Reset unused pages so the next 4K init does not see leftover head/tail.
> + */
> +static void svm_migrate_clear_compound_pages(struct page *page, unsigned int nr,
> +					      struct dev_pagemap *pgmap)
> +{
> +	unsigned int i;
> +
> +	for (i = 0; i < nr; i++, page++) {
> +		struct folio *new_folio = (struct folio *)page;
> +
> +		if (page_count(page))
> +			continue;
> +
> +		page->flags.f &= ~0xffUL;
> +#ifdef NR_PAGES_IN_LARGE_FOLIO
> +		((struct folio *)(page - 1))->_nr_pages = 0;
> +#endif
> +		new_folio->mapping = NULL;
> +		new_folio->pgmap = pgmap;
> +		new_folio->share = 0;
> +		page->zone_device_data = NULL;
> +	}
> +}

[Severity: High]
Can svm_migrate_clear_compound_pages() skip clearing tail pages or corrupt
newly reallocated VRAM pages?

During a CPU fault on a device-private folio, do_huge_pmd_device_private()
and do_swap_page() hold an extra folio reference across
pgmap->ops->migrate_to_ram() and drop it via folio_put() after
svm_migrate_to_ram() has already released prange->migrate_mutex.  Because
folio_put_testzero() decrements the folio refcount to 0 before calling
free_zone_device_folio() -> svm_migrate_folio_free(), a concurrent
migration reusing the same svm_bo can reallocate those VRAM pages while
svm_migrate_clear_compound_pages() is running:

CPU0                                  CPU1
do_huge_pmd_device_private()
  folio_get(folio)
  svm_migrate_to_ram()
    ...
    mutex_unlock(&prange->migrate_mutex)
  folio_put(folio)
    folio_put_testzero(folio)
                                      svm_migrate_ram_to_vram()
                                        svm_migrate_copy_to_vram()
                                          page_ref_count(page) == 0
                                          svm_migrate_get_vram_page()
                                            zone_device_folio_init()
    free_zone_device_folio()
      svm_migrate_folio_free()
        svm_migrate_clear_compound_pages()
          page_count(page) == 0
          page->flags.f &= ~0xffUL
          page->zone_device_data = NULL  <-- clobbers reallocated folio

Even without the TOCTOU race between page_count(page) and
set_page_count(page, 1) inside zone_device_page_init(), page_count(page)
calls folio_ref_count(page_folio(page)) before new_folio->pgmap = pgmap
clears compound_head on tail pages.  If page[0] has already been
reallocated as a 4K page (setting page[0]._refcount = 1), page_folio() on
each remaining tail page (i = 1 .. nr - 1) follows the leftover
compound_head back to page[0], sees page_count() == 1, and skips clearing
all 511 unused tail pages.

In addition, in svm_migrate_copy_to_vram(), page_ref_count() reads
page->_refcount directly, which is 0 on tail pages even before the head
folio's final reference is put.  Reinitializing tail page 1 as a 4K page
wipes folio_order() and folio->_nr_pages on the still-live compound folio
before folio_put() runs, so free_zone_device_folio() leaks 512 pgmap->ref
references and hangs memunmap_pages() on device teardown.

>  static void
> -svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn)
> +svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn,
> +			   int order)
>  {
> -	struct page *page;
> +	struct page *page = pfn_to_page(pfn);
>  
> -	page = pfn_to_page(pfn);
> -	svm_range_bo_ref(prange->svm_bo);
> +	/* Init this pfn, not page_folio() — that follows a leftover tail to the old head. */
> +	zone_device_folio_init((struct folio *)page, page_pgmap(page), order);

[Severity: Medium]
Does page_pgmap(page) still call page_folio(page) on a leftover tail page
here?

include/linux/mmzone.h:page_pgmap() {
	VM_WARN_ON_ONCE_PAGE(!memdesc_is_zone_device(&page->flags), page);
	return page_folio(page)->pgmap;
}

Because page_pgmap(page) runs before zone_device_folio_init() clears any
stale compound_head on page, it still follows a leftover tail pointer to
the old head page.  If that old head page has since become a tail page of
another compound folio, its pgmap union slot holds an odd compound_head
value, passing a corrupted pgmap pointer into zone_device_page_init() and
percpu_ref_tryget_many().  Passing &prange->svm_bo->node->adev->kfd.pgmap
directly avoids reading page_pgmap(page) before the page is initialized.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> index a571fda5c713a..fc739af454edf 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
[ ... ]
> @@ -579,10 +588,20 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
>  	}
>  
>  	memset(&bp, 0, sizeof(bp));
> +	/*
> +	 * Keep bp.size equal to the prange size so it matches the
> +	 * amdgpu_amdkfd_reserve_mem_limit() reservation made by the caller.
> +	 * A 2MB byte_align makes the VRAM manager allocate 2MB-aligned 2MB
> +	 * buddy blocks and trim only the last one to the requested size, so
> +	 * every full 2MB chunk can still back a device THP.
> +	 */
>  	bp.size = prange->npages * PAGE_SIZE;
> +	if (SVM_THP_ENABLED && prange->npages >= SVM_HPAGE_PMD_NR)
> +		bp.byte_align = SVM_HPAGE_PMD_SIZE;
> +	else
> +		bp.byte_align = PAGE_SIZE;
>  	bp.bo_ptr_size = sizeof(struct svm_range_bo);
>  	bp.destroy = svm_range_bo_destroy;
> -	bp.byte_align = PAGE_SIZE;
>  	bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
>  	bp.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
>  	bp.flags |= clear ? AMDGPU_GEM_CREATE_VRAM_CLEARED : 0;
> @@ -594,9 +613,16 @@ svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
>  
>  	r = amdgpu_bo_create(node->adev, &bp, &bo);
>  	if (r) {
> -		pr_debug("failed %d to create bo\n", r);
> -		mmput(mm);
> -		goto create_bo_failed;
> +		if (bp.byte_align == SVM_HPAGE_PMD_SIZE) {
> +			/* fall back PAGE_SIZE granularity */
> +			bp.byte_align = PAGE_SIZE;
> +			r = amdgpu_bo_create(node->adev, &bp, &bo);
> +		}
> +		if (r) {
> +			pr_debug("failed %d to create bo\n", r);
> +			mmput(mm);
> +			goto create_bo_failed;
> +		}
>  	}

[Severity: Medium]
Does the first amdgpu_bo_create() call trigger TTM VRAM LRU eviction when
2MB blocks are fragmented, and is the PAGE_SIZE fallback a no-op when
bp.size is a multiple of 2MB?

When bp.byte_align is SVM_HPAGE_PMD_SIZE, amdgpu_vram_mgr_new() sets
min_block_size = 2MB and gpu_buddy_alloc_blocks() rounds the allocation up
to a 2MB multiple before trimming the last block.  If VRAM has enough 4K
pages but no 2MB-contiguous blocks, ttm_bo_validate() does not return to
the fallback immediately; its second pass calls ttm_bo_evict_alloc() and
evicts other live VRAM BOs trying to coalesce 2MB blocks.

Furthermore, when bp.size is an exact multiple of 2MB, resetting
bp.byte_align = PAGE_SIZE in the fallback still leaves min_block_size at
2MB (pages_per_block defaults to HPAGE_PMD_NR):

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c:amdgpu_vram_mgr_new() {
    ...
		if (tbo->page_alignment)
			min_block_size = (u64)tbo->page_alignment << PAGE_SHIFT;
		else if ((size >= (u64)pages_per_block << PAGE_SHIFT) &&
			 !(size & (((u64)pages_per_block << PAGE_SHIFT) - 1)))
			min_block_size = (u64)pages_per_block << PAGE_SHIFT;
		else
			min_block_size = PAGE_SIZE;
    ...
}

Because tbo->page_alignment is 1 when bp.byte_align is PAGE_SIZE, wait -
actually when tbo->page_alignment is 0 or when size is a 2MB multiple, the
fallback does not relax the 2MB requirement or repeats the eviction scan.

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

  reply	other threads:[~2026-11-22 20:50 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 [this message]
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
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-228335@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