drm/amdkfd: Check ownership of device pages for DMA mapping
Subsystems (3)
Felix Kuehling
maintainerPATCH v3 1/1 drm/amdkfd: Check ownership of device pages for DMA mapping
This issue was raised independently in two contexts: pages from DAX devices and pages from other GPU hives or inherited from other processed. This patch addresses both concerns together along with Sashiko comments raised for one of the previous reviews:
https://sashiko.dev/#/patchset/20261103-kfd-svm-foreign-vram-20261103-v1-1-d6282053cad3@gmail.com?part=1
Before treating a page as a zone device page belonging to the SVM range, check that it actually belongs to that range based on the zone_device_data. Non-private pages belonging to other devices or ranges can be treated like system memory for the purpose of DMA mapping.
---
v2: Addressed code review comments from Sashiko
v3: Optimized to avoid mutex lock/unlock in inner loop
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +21 -1
@@ -164,6 +164,7 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange,enum dma_data_direction dir =DMA_BIDIRECTIONAL ;dma_addr_t *addr = prange->dma_addr[gpuidx];struct device *dev = adev->dev; +void *zone_device_data;struct page *page;int i, r;
@@ -174,13 +175,22 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, prange->dma_addr[gpuidx] = addr; } +mutex_lock (&prange->lock); + zone_device_data = prange->ttm_res ? prange->svm_bo :NULL ; +mutex_unlock (&prange->lock); +/* prange->svm_bo may change after this. If that happens, it implies +* some kind of invalidation that would be detected by an HMM range +* sequence number change before the page tables are updated +*/ + addr += offset;for (i =0 ; i < npages; i++) {if (svm_is_valid_dma_mapping_addr (dev, addr[i]))dma_unmap_page (dev, addr[i],PAGE_SIZE , dir); page =hmm_pfn_to_page (hmm_pfns[i]); -if (is_zone_device_page (page)) { +if (zone_device_data &&is_zone_device_page (page) && + page->zone_device_data == zone_device_data) {struct amdgpu_device *bo_adev = prange->svm_bo->node->adev; addr[i] = (hmm_pfns[i] << PAGE_SHIFT) +
@@ -189,6 +199,16 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, addr[i] |=SVM_RANGE_VRAM_DOMAIN ;pr_debug_ratelimited ("vram address: 0x%llx\n" , addr[i]);continue ; + }else if (is_device_private_page (page)) { +/* Device private memory from another driver or range. +* This should never happen in practice but it's +* theoretically possible after a process forks, +* creates a new KFD context and, maps the same page +* after the parent got a new COW page. +*/ +pr_debug ("Trying to map device private page from another device or range\n" ); + addr[i] =0 ; +return -EINVAL ; } addr[i] =dma_map_page (dev, page,0 ,PAGE_SIZE , dir); r =dma_mapping_error (dev, addr[i]);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdkfd: same-hive device-private pages fail in svm_range_dma_map_dev() - [Medium] drm/amdkfd…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/amdkfd: same-hive device-private pages fail in svm_range_dma_map_dev()
- [Medium] drm/amdkfd: dma_map_page() called on zone_device pages in svm_range_dma_map_dev()
- [Low] drm/amdkfd: missing Fixes trailer for commit 1d5dbfe6c06a --
commit d01d79723c2d218e5005d3a6b9a8c0c534d177d0
Author: Felix Kuehling <[email protected]>
Quoted 8 lines
drm/amdkfd: Check ownership of device pages for DMA mapping
This issue was raised independently in two contexts: pages from DAX devices and pages from other GPU hives or inherited from other processed. This patch addresses both concerns together along with Sashiko comments raised for one of the previous reviews:
https://sashiko.dev/#/patchset/20261103-kfd-svm-foreign-vram-20261103-v1-1-d6282053cad3@gmail.com?part=1
Quoted 8 lines
Before treating a page as a zone device page belonging to the SVM range, check that it actually belongs to that range based on the
zone_device_data. Non-private pages belonging to other devices or ranges can be treated like system memory for the purpose of DMA mapping.
[Severity: Low]
This fixes the unconditional is_zone_device_page(page) ownership assumption in svm_range_dma_map_dev(). Should this include a Fixes tag for 1d5dbfe6c06a ("drm/amdkfd: classify and map mixed svm range pages in GPU")?
Quoted 41 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +12
-1@@ -164,6 +164,7 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange,enum dma_data_direction dir =DMA_BIDIRECTIONAL ;dma_addr_t *addr = prange->dma_addr[gpuidx];struct device *dev = adev->dev; +void *zone_device_data;struct page *page;int i, r;@@ -174,13 +175,22 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, prange->dma_addr[gpuidx] = addr; } +mutex_lock (&prange->lock); + zone_device_data = prange->ttm_res ? prange->svm_bo :NULL ; +mutex_unlock (&prange->lock); +/* prange->svm_bo may change after this. If that happens, it implies +* some kind of invalidation that would be detected by an HMM range +* sequence number change before the page tables are updated +*/ + addr += offset;for (i =0 ; i < npages; i++) {if (svm_is_valid_dma_mapping_addr (dev, addr[i]))dma_unmap_page (dev, addr[i],PAGE_SIZE , dir); page =hmm_pfn_to_page (hmm_pfns[i]); -if (is_zone_device_page (page)) { +if (zone_device_data &&is_zone_device_page (page) && + page->zone_device_data == zone_device_data) {struct amdgpu_device *bo_adev = prange->svm_bo->node->adev; addr[i] = (hmm_pfns[i] << PAGE_SHIFT) +@@ -189,6 +199,16 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, addr[i] |=SVM_RANGE_VRAM_DOMAIN ;pr_debug_ratelimited ("vram address: 0x%llx\n" , addr[i]);continue ; + }else if (is_device_private_page (page)) {
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, · line 212
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9
-0+/* Device private memory from another driver or range. +* This should never happen in practice but it's +* theoretically possible after a process forks, +* creates a new KFD context and, maps the same page +* after the parent got a new COW page. +*/ +pr_debug ("Trying to map device private page from another device or range\n" ); + addr[i] =0 ; +return -EINVAL ; }
[Severity: High]
Can this return -EINVAL on device-private pages inherited across fork() or moved via mremap() on the same GPU or XGMI hive?
svm_range_validate_and_map() computes dev_private_owner via kfd_svm_page_owner(), which returns the device-wide or hive-wide SVM_ADEV_PGMAP_OWNER(adev) even when prange->ttm_res is NULL or prange->svm_bo does not own the underlying pages. When pgmap->owner matches range->dev_private_owner, hmm_vma_handle_pte() skips hmm_pte_need_fault() and returns the existing device-private PFN without faulting it back to system RAM, even on a writable COW VMA after fork():
mm/hmm.c:hmm_vma_handle_pte() {
...
if (softleaf_is_device_private(entry) &&
page_pgmap(softleaf_to_page(entry))->owner ==
range->dev_private_owner) {
cpu_flags = HMM_PFN_VALID;
if (softleaf_is_device_private_write(entry))
cpu_flags |= HMM_PFN_WRITE;
new_pfn_flags = softleaf_to_pfn(entry) | cpu_flags;
goto out;
}
...
}Because page->zone_device_data still points to the parent or source range's svm_bo, page->zone_device_data == zone_device_data is false, so svm_range_dma_map_dev() fails with -EINVAL. When reached from svm_range_restore_work(), returning -EINVAL causes the worker to reschedule itself every 1ms indefinitely while user queues remain suspended.
Passing NULL for dev_private_owner in svm_range_validate_and_map() when prange->ttm_res is NULL (or when foreign same-hive pages are detected) would let hmm_range_fault() migrate those pages back to system memory.
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, · line 213
Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0addr[i] =dma_map_page (dev, page,0 ,PAGE_SIZE , dir);
[Severity: Medium]
Can falling through to dma_map_page() for non-private ZONE_DEVICE pages (such as DAX pages or MEMORY_DEVICE_COHERENT pages from another range or hive) fail when CONFIG_DMA_API_DEBUG is enabled?
dma_map_page() calls dma_map_page_attrs(), which rejects all ZONE_DEVICE pages under CONFIG_DMA_API_DEBUG:
kernel/dma/mapping.c:dma_map_page_attrs() {
...
if (IS_ENABLED(CONFIG_DMA_API_DEBUG) &&
WARN_ON_ONCE(is_zone_device_page(page)))
return DMA_MAPPING_ERROR;
...
}This triggers a WARN_ON_ONCE() and causes svm_range_dma_map_dev() to fail with -ENOMEM. Using dma_map_phys() (or pci_p2pdma_map_segment() as appropriate) avoids passing a ZONE_DEVICE struct page to dma_map_page().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 6 lines · Felix Kuehling
This issue was raised independently in two contexts: pages from DAX devices and pages from other GPU hives or inherited from other processed. This patch addresses both concerns together along with Sashiko comments raised for one of the previous reviews:
https://sashiko.dev/#/patchset/20261103-kfd-svm-foreign-vram-20261103-v1-1-d6282053cad3@gmail.com?part=1
Quoted 8 lines
Before treating a page as a zone device page belonging to the SVM range, check that it actually belongs to that range based on the
zone_device_data. Non-private pages belonging to other devices or ranges can be treated like system memory for the purpose of DMA mapping.
Quoted 62 lines
---
v2: Addressed code review comments from Sashiko
v3: Optimized to avoid mutex lock/unlock in inner loop--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdkfd/kfd_svm.c +21
-1@@ -164,6 +164,7 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange,enum dma_data_direction dir =DMA_BIDIRECTIONAL ;dma_addr_t *addr = prange->dma_addr[gpuidx];struct device *dev = adev->dev; +void *zone_device_data;struct page *page;int i, r;@@ -174,13 +175,22 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, prange->dma_addr[gpuidx] = addr; } +mutex_lock (&prange->lock); + zone_device_data = prange->ttm_res ? prange->svm_bo :NULL ; +mutex_unlock (&prange->lock); +/* prange->svm_bo may change after this. If that happens, it implies +* some kind of invalidation that would be detected by an HMM range +* sequence number change before the page tables are updated +*/ + addr += offset;for (i =0 ; i < npages; i++) {if (svm_is_valid_dma_mapping_addr (dev, addr[i]))dma_unmap_page (dev, addr[i],PAGE_SIZE , dir); page =hmm_pfn_to_page (hmm_pfns[i]); -if (is_zone_device_page (page)) { +if (zone_device_data &&is_zone_device_page (page) && + page->zone_device_data == zone_device_data) {struct amdgpu_device *bo_adev = prange->svm_bo->node->adev; addr[i] = (hmm_pfns[i] << PAGE_SHIFT) +@@ -189,6 +199,16 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange, addr[i] |=SVM_RANGE_VRAM_DOMAIN ;pr_debug_ratelimited ("vram address: 0x%llx\n" , addr[i]);continue ; + }else if (is_device_private_page (page)) { +/* Device private memory from another driver or range. +* This should never happen in practice but it's +* theoretically possible after a process forks, +* creates a new KFD context and, maps the same page +* after the parent got a new COW page. +*/ +pr_debug ("Trying to map device private page from another device or range\n" ); + addr[i] =0 ; +return -EINVAL ; } addr[i] =dma_map_page (dev, page,0 ,PAGE_SIZE , dir); r =dma_mapping_error (dev, addr[i]);