From: "Chen, Xiaogang" <xiaogang.chen@amd.com>
To: Philip Yang <yangp@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: Mon, 30 Nov 2026 01:36:34 -0600 [thread overview]
Message-ID: <a0d0ebca-ce1c-4434-b42c-a29a8a5f6ca8@amd.com> (raw)
In-Reply-To: <7affc853-ec24-4620-a883-b344f4ded38e@amd.com>
On 11/27/2026 3:34 PM, Philip Yang wrote:
>
>
> On 2026-11-22 15:26, Xiaogang.Chen wrote:
>> From: Xiaogang Chen<xiaogang.chen@amd.com>
>>
>> Extend kfd/svm functions to allocate HPAGE_PMD_SIZE based device
>> memory by
>> buddy allocator, each drm_buddy_block is HPAGE_PMD_SIZE aligned; and to
>> allocate THP system ram by vma_alloc_folio.
>>
>> Introduce SVM_RANGE_DMA_THP flag that indicates dma map of THP. THP dma
>> addresss will use this flag.
>>
>> Add dev_pagemap_ops->folio_split callback that is called by
>> folio_split when
>> core MM splits device memory folio.
>>
>> Reset compound folio of device memory after it got released from core
>> HMM.
>> So, TTM can reuse this VRAM as 4K pages on next range.
>>
>> These are preparations for following support for (THP) migration of zone
>> device-private memory, no function change.
>>
>> Signed-off-by: Xiaogang Chen<xiaogang.chen@amd.com>
>> ---
>> drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 112 ++++++++++++++++++++---
>> drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 39 ++++++--
>> drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 18 ++++
>> 3 files changed, 152 insertions(+), 17 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
>> b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
>> index 1eb92ac6eb05..5ead98a604b1 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;
>> + }
>> +}
>> +
> This is not needed. Commit 12b2285bf3d1 ("mm/zone_device: reinitialize
> large zone device private folios") already makes zone_device_page_init()
> clear any leftover head/tail state when a page is handed out again.
> svm_migrate_get_vram_page() calls zone_device_folio_init() for every VRAM
> page it hands out, so the page is reinitialized when it is reused, as
> a 4K
> page or as part of a new THP. There is no need to clear the compound
> pages
> when the folio is freed.
no, it is needed. That part is easy to get confused.
svm_migrate_clear_compound_pages() runs because freeing the device THP
only releases the head folio. The other 511 struct pages stay linked to
that head, and TTM can hand those same VRAM pages out later as ordinary
4K pages.
free_zone_device_folio() clears folio->mapping on the head and calls
folio_free. It does not walk the tails. Each tail still has its
compound-tail marker, so page_folio() follows it back to the old head,
and page_pgmap() is implemented as page_folio(page)->pgmap. The tail's
own pgmap slot held the head pointer, not the dev_pagemap.
Look at folio definition: pgmap and compound_info are the same word. A
tail page has bit 0 set there, so page_folio() follows it to the old
head, and page_pgmap() is page_folio(page)->pgmap. Writing the
dev_pagemap pointer clears that bit. After that, page_folio() returns
the page itself and page_pgmap() reads the pointer just stored. The next
zone_device_folio_init() does this again for the one page it is given,
but a former tail can be initialized after the old head has already been
reused. Without new_folio->pgmap = pgmap at free time, page_pgmap() on
that tail still follows the live head.
And page->flags.f &= ~0xffUL is required too. PG_head is in the head
page's flags, and the folio order is in the low byte of the second
page's flags (folio->_flags_1). The next init clears those bits only on
the page it receives. Until the head itself is allocated again,
folio_test_large() on it is still true.
svm_migrate_clear_compound_pages walks every page of the freed folio,
drops the head/tail flag bits and _nr_pages, and stores the real pgmap
on each page. After that, each page is a standalone zone-device page and
the next order-0 init does not follow the old THP.
Another reason to have svm_migrate_clear_compound_pages: after kfd
driver publishes device pages to core MM. These pages are under core MM
management. So, not only kfd driver use them, other components can use
them too. At the time device pages got released by HMM to kfd driver,
before kfd driver reuse them these pages or folios need be in clean
state. So, re-users(not only kfd driver) can use them without worrying
their stale states.
svm_migrate_clear_compound_pages does mimic zone_device_folio_init,
but it is on purpose.
>> 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);
>> page->zone_device_data = prange->svm_bo;
> Please use folio_set_zone_device_data(page_folio(page), prange->svm_bo).
> The result is the same, since page is always the head page here, but it
> adds a sanity check and matches how other drivers do it.
I also have a version for coherent device THP support.
folio_set_zone_device_data only allows private device memory because
currently only AMD supports coherent device memory. It is why I set
page->zone_device_data directly here. It does not affect anything but
will apply to coherent case.
>> - zone_device_page_init(page, page_pgmap(page), 0);
>> + svm_range_bo_ref(prange->svm_bo);
>> }
>> +
>> static void
>> svm_migrate_put_vram_page(struct amdgpu_device *adev, unsigned long
>> addr)
>> {
>> @@ -249,11 +277,45 @@ svm_migrate_addr(struct amdgpu_device *adev,
>> struct page *page)
>> }
>> static struct page *
>> -svm_migrate_get_sys_page(struct vm_area_struct *vma, unsigned long
>> addr)
>> +svm_migrate_get_sys_page(struct vm_area_struct *vma, unsigned long
>> addr,
>> + unsigned long order)
>> {
>> struct page *page;
>> - page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
>> + if (order) {
>> +#ifdef CONFIG_TRANSPARENT_HUGEPAGE
> The caller only passes order > 0 on the THP compound page path, so this
> #ifdef is not needed.
It is defensive. if migrate->src[i] & MIGRATE_PFN_COMPOUND is true,
CONFIG_TRANSPARENT_HUGEPAGE should have been defined by user on kernel
setting and hardware support THP. If HMM provides MIGRATE_PFN_COMPOUND
folio, but kernel does not enable THP or hardware does not support THP
driver return null for such request.
>> + struct folio *folio;
>> + gfp_t gfp;
>> +
>> + /*
>> + * Honour MADV_NOHUGEPAGE and prctl(PR_SET_THP_DISABLE). The
>> + * sysfs THP policy (thp_vma_allowable_orders()) and
>> + * vma_thp_gfp_mask() are not exported to modules.
>> + */
>> + if (vma_thp_disabled(vma, vma->vm_flags, false))
>> + return NULL;
>> +
>> + /*
>> + * Used by every VRAM to RAM migration (CPU/GPU fault,
>> + * prefetch, TTM eviction), and the caller falls back to 4K
>> + * pages on failure. Fail fast like the default "madvise"
>> + * defrag policy: no direct reclaim or compaction unless the
>> + * VMA asked for huge pages. Eviction in particular runs under
>> + * memory pressure, where compaction stalls are most costly.
>> + */
>> + gfp = GFP_TRANSHUGE_LIGHT;
>> + if (vma->vm_flags & VM_HUGEPAGE)
>> + gfp |= __GFP_DIRECT_RECLAIM;
>> +
> I think this mixes up the madvise THP policy with THP page migration.
> They are separate settings. GFP_TRANSHUGE_LIGHT/GFP_TRANSHUGE are used
> internally by core mm (THP fault, khugepaged, NUMA migration), not by
> drivers. For the migration destination, please use GFP_HIGHUSER, the same
> flag other drivers use in their migrate-to-RAM paths.
GFP_TRANSHUGE_LIGHT is used mostly by core MM. Here we allocate THP. The
idea is: not allocate sys THP when system has memory pressure. If it
happens fail quickly and driver falls back to normal size page
allocation by alloc_page_vma(GFP_HIGHUSER,...), there we use GFP_HIGHUSER.
The kernel comments on GFP_TRANSHUGE_LIGH:
* %GFP_TRANSHUGE and %GFP_TRANSHUGE_LIGHT are used for THP
allocations. They
* are compound allocations that will generally fail quickly if memory
is not
* available and will not wake kswapd/kcompactd on failure. The _LIGHT
* version does not attempt reclaim/compaction at all and is by default
used
* in page fault path, while the non-light is used by khugepaged.
If we let kernel do its best to allocate THP that may trigger
kswapd/kcopactd and reclaim under memory pressure. That take much longer
time to allocate, specially this sys ram allocation is also used by cpu
page fault recovery and ttm eviction.
So, allocate sys THP when there is no memory pressure(no reclaim). If
kernel has to do reclaim GFP_TRANSHUGE_LIGHT let kernel fail THP
allocation immediately, then driver uses GFP_HIGHUSER to allocate normal
size pages. It is for better performance.
>
>> + folio = vma_alloc_folio(gfp, order, vma, addr);
>> + page = folio ? folio_page(folio, 0) : NULL;
>> +#else
>> + return NULL;
>> +#endif
>> + } else {
>> + page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
>> + }
>> +
>> if (page)
>> lock_page(page);
>> @@ -267,8 +329,13 @@ static unsigned long
>> svm_migrate_successful_pages(struct migrate_vma *migrate)
>> for (i = 0; i < migrate->npages; i++) {
>> if (migrate->dst[i] & MIGRATE_PFN_VALID &&
>> - migrate->src[i] & MIGRATE_PFN_MIGRATE)
>> - mpages++;
>> + migrate->src[i] & MIGRATE_PFN_MIGRATE) {
> Wrong indentation change.
ok,
>> +
>> + if (migrate->dst[i] & MIGRATE_PFN_COMPOUND)
>> + mpages += SVM_HPAGE_PMD_NR;
> For a compound page, the remaining dst entries are zero, so skip them:
>
> i += HPAGE_PMD_NR - 1;
>
>> + else
>> + mpages++;
>> + }
>> }
>> return mpages;
>> }
>> @@ -312,7 +379,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node,
>> struct svm_range *prange,
>> migrate->dst[i] = 0;
>> spage = NULL;
>> } else {
>> - svm_migrate_get_vram_page(prange, migrate->dst[i]);
>> + svm_migrate_get_vram_page(prange, migrate->dst[i], 0);
>> migrate->dst[i] = migrate_pfn(migrate->dst[i]);
>> mpages++;
>> }
>> @@ -626,10 +693,17 @@ svm_migrate_ram_to_vram(struct svm_range
>> *prange, uint32_t best_loc,
>> return r < 0 ? r : 0;
>> }
>> +/* folio can be compound folio or single page */
>> static void svm_migrate_folio_free(struct folio *folio)
>> {
>> struct page *page = &folio->page;
>> struct svm_range_bo *svm_bo = page->zone_device_data;
>> + struct dev_pagemap *pgmap = folio->pgmap;
>> + unsigned int order = folio_order(folio);
>> +
>> + /* So TTM can reuse this VRAM as 4K pages on the next range. */
>> + if (order)
>> + svm_migrate_clear_compound_pages(page, 1U << order, pgmap);
> As above, please remove this.
As explain above we need svm_migrate_clear_compound_pages.
>
>> if (svm_bo) {
>> pr_debug_ratelimited("ref: %d\n", kref_read(&svm_bo->kref));
>> @@ -688,7 +762,7 @@ svm_migrate_copy_to_ram(struct amdgpu_device
>> *adev, struct svm_range *prange,
>> j = 0;
>> }
>> - dpage = svm_migrate_get_sys_page(migrate->vma, addr);
>> + 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);
>> @@ -1091,9 +1165,25 @@ static vm_fault_t svm_migrate_to_ram(struct
>> vm_fault *vmf)
>> return r ? VM_FAULT_SIGBUS : 0;
>> }
>> +static void svm_migrate_folio_split(struct folio *head, struct
>> folio *tail)
>> +{
>> + struct svm_range_bo *svm_bo;
>> +
>> + if (tail == NULL)
>> + return;
>> +
>> + tail->pgmap = head->pgmap;
>> + tail->mapping = head->mapping;
>> +
>> + svm_bo = folio_zone_device_data(head);
>> + folio_set_zone_device_data(tail, svm_bo);
>> + svm_range_bo_ref(svm_bo);
>> +}
>> +
>> static const struct dev_pagemap_ops svm_migrate_pgmap_ops = {
>> .folio_free = svm_migrate_folio_free,
>> .migrate_to_ram = svm_migrate_to_ram,
>> + .folio_split = svm_migrate_folio_split,
>> };
>> /* Each VRAM page uses sizeof(struct page) on system memory */
>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> index 421d3f65fcf1..35cebdad5651 100644
>> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> @@ -246,7 +246,16 @@ void svm_range_dma_unmap_dev(struct device *dev,
>> dma_addr_t *dma_addr,
>> if (!svm_is_valid_dma_mapping_addr(dev, dma_addr[i]))
>> continue;
>> pr_debug_ratelimited("unmap 0x%llx\n", dma_addr[i] >>
>> PAGE_SHIFT);
>> - dma_unmap_page(dev, dma_addr[i], PAGE_SIZE, dir);
>> +
>> + /* dma unmap of THP */
>> + if (dma_addr[i] & SVM_RANGE_DMA_THP) {
>> + dma_addr[i] &= ~SVM_RANGE_DMA_THP;
>> + dma_unmap_page(dev, dma_addr[i],
>> PAGE_SIZE*SVM_HPAGE_PMD_NR,
>> + DMA_BIDIRECTIONAL);
> Clear the entry and skip the rest of the THP here:
>
> dma_addr[i] = 0;
> i += HPAGE_PMD_NR - 1;
>
ok
Regards
Xiaogang
>
>> + } else {
>> + dma_unmap_page(dev, dma_addr[i], PAGE_SIZE, dir);
> dma_addr[i] = 0;
>> + }
>> +
>> dma_addr[i] = 0;
> Then this line can be removed.
>
> Regards,
> Philip
>> }
>> }
>> @@ -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;
>> + }
>> }
>> svm_bo = to_svm_range_bo(bo);
>> @@ -957,6 +983,7 @@ svm_range_copy_array(void *psrc, size_t size,
>> uint64_t num_elements,
>> *vram_pages = 0;
>> for (i = 0; i < num_elements; i++) {
>> dma_addr_t *temp;
>> +
>> temp = (dma_addr_t *)dst + i;
>> *temp = *((dma_addr_t *)src + i);
>> if (*temp&SVM_RANGE_VRAM_DOMAIN)
>> @@ -1675,7 +1702,7 @@ static int svm_range_reserve_bos(struct
>> svm_validate_context *ctx, bool intr)
>> uint32_t gpuidx;
>> int r;
>> - drm_exec_init(&ctx->exec, intr ? DRM_EXEC_INTERRUPTIBLE_WAIT:
>> 0, 0);
>> + drm_exec_init(&ctx->exec, intr ? DRM_EXEC_INTERRUPTIBLE_WAIT :
>> 0, 0);
>> drm_exec_until_all_locked(&ctx->exec) {
>> for_each_set_bit(gpuidx, ctx->bitmap, MAX_GPU_INSTANCE) {
>> pdd = kfd_process_device_from_gpuidx(ctx->process,
>> gpuidx);
>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
>> b/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
>> index 38744e716f74..8767a784e679 100644
>> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
>> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.h
>> @@ -35,6 +35,24 @@
>> #include "kfd_priv.h"
>> #define SVM_RANGE_VRAM_DOMAIN (1UL << 0)
>> +#define SVM_RANGE_DMA_THP (1UL << 1)
>> +
>> +/*
>> + * Size of a PMD-mapped device or system THP. HPAGE_PMD_* expand to
>> + * BUILD_BUG() without CONFIG_TRANSPARENT_HUGEPAGE, so the SVM code
>> uses
>> + * these instead. Without THP, SVM_THP_ENABLED is false, no compound
>> + * migration is requested, and HMM never reports
>> MIGRATE_PFN_COMPOUND, so
>> + * the THP paths still compile but are never taken.
>> + */
>> +#ifdef CONFIG_TRANSPARENT_HUGEPAGE
>> +#define SVM_HPAGE_PMD_ORDER HPAGE_PMD_ORDER
>> +#else
>> +#define SVM_HPAGE_PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
>> +#endif
>> +#define SVM_HPAGE_PMD_NR (1UL << SVM_HPAGE_PMD_ORDER)
>> +#define SVM_HPAGE_PMD_SIZE (PAGE_SIZE << SVM_HPAGE_PMD_ORDER)
>> +#define SVM_THP_ENABLED IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)
>> +
>> #define SVM_ADEV_PGMAP_OWNER(adev)\
>> ((adev)->hive ? (void *)(adev)->hive : (void *)(adev))
>
next prev parent reply other threads:[~2026-11-30 7:37 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 [this message]
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=a0d0ebca-ce1c-4434-b42c-a29a8a5f6ca8@amd.com \
--to=xiaogang.chen@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=yangp@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 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.