drm/amdkfd: Use last + 1 of vm range to check 2MB huge page alignment
Subsystems (4)
PATCH 1/1 drm/amdkfd: Use last + 1 of vm range to check 2MB huge page alignment
SVM ranges use inclusive page indices: prange->last is the last page in the range. The split-remap logic introduced by commit 448ee45353ef ("drm/amdkfd: Use huge page size to check split svm range alignment") uses ALIGN_DOWN(prange->last, 512) to determine whether the original range can contain a 2MB huge-page mapping.
That aligns the last page itself down. Thus a range ending one page before the next 2MB boundary is classified as if the final 2MB block did not exist. When such a range is split inside that final block, the split head or tail can be left off the remap list even though it was derived from an original range that may have PMD mappings.
Use prange->last + 1 as the exclusive upper bound when computing the original range's last 2MB-aligned boundary. For the head-side remap check, compare the inclusive head->last directly against that exclusive aligned boundary; this catches a split head ending one page before the boundary while preserving the existing alignment check.
Xiaogang Chen identified this cause and posted the candidate fix in the regression thread. With the culprit change active and the local revert not applied, the unchanged C/HSA reproducer completes 10/10 runs with this change on an RX 7600 XT.
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -4
@@ -1144,7 +1144,7 @@ static int svm_range_split_tail(struct svm_range *prange, uint64_t new_last, struct list_head *insert_list, struct list_head *remap_list) { - unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); + unsigned long last_align_down = ALIGN_DOWN(prange->last + 1, 512);unsigned long start_align =ALIGN (prange->start,512 );bool huge_page_mapping = last_align_down > start_align;struct svm_range *tail =NULL ;
@@ -1168,7 +1168,7 @@ static int svm_range_split_head(struct svm_range *prange, uint64_t new_start, struct list_head *insert_list, struct list_head *remap_list) { - unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); + unsigned long last_align_down = ALIGN_DOWN(prange->last + 1, 512);unsigned long start_align =ALIGN (prange->start,512 );bool huge_page_mapping = last_align_down > start_align;struct svm_range *head =NULL ;
@@ -1181,8 +1181,8 @@ svm_range_split_head(struct svm_range *prange, uint64_t new_start,list_add (&head->list, insert_list); -if (huge_page_mapping && head->last +1 > start_align && - head->last +1 < last_align_down && (!IS_ALIGNED (head->last,512 ))) +if (huge_page_mapping && head->last > start_align && + head->last < last_align_down && (!IS_ALIGNED (head->last,512 )))list_add (&head->update_list, remap_list);return 0 ;
base-commit: 2c7d5b0a5ec0fc713a7f350806553643e87e6f43
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Incorrect boundary and alignment checks in `svm_range_split_head` cause missing GPU page table…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Incorrect boundary and alignment checks in svm_range_split_head cause missing GPU page table remaps for shattered huge pages, and unnecessary remaps for perfectly aligned ranges. --
commit 91761d4de2f6b8a545a87df1ae810dd4280f1f59
Author: Gerhard Schwanzer <[email protected]>
drm/amdkfd: Use last + 1 of vm range to check 2MB huge page alignment
This patch updates the split-remap logic to use the exclusive upper bound when computing the last 2MB-aligned boundary of the original range. It also modifies the split head check to compare the inclusive head->last directly against the exclusive aligned boundary.
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0
[ ... ]
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_head(struct svm_range *prange, uint64_t new_start, · line 1186
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2
-2@@ -1181,8 +1181,8 @@ svm_range_split_head(struct svm_range *prange, uint64_t new_start,list_add (&head->list, insert_list); -if (huge_page_mapping && head->last +1 > start_align && - head->last +1 < last_align_down && (!IS_ALIGNED (head->last,512 ))) +if (huge_page_mapping && head->last > start_align && + head->last < last_align_down && (!IS_ALIGNED (head->last,512 )))list_add (&head->update_list, remap_list);
[Severity: High]
Does removing the + 1 offsets from head->last in these boundary checks introduce unnecessary remaps for aligned ranges, while failing to fix the shattered huge pages?
Because head->last is an inclusive page index, its aligned boundary values are 511, 1023, etc. If a split head perfectly aligns with a 2MB page (for example, ending at 511), head->last is 511. The !IS_ALIGNED(511, 512) check is true.
Before this patch, head->last + 1 < last_align_down (512 < 512) evaluated to false, safely preventing a remap. By removing the + 1, this evaluates 511 < 512 as true, causing perfectly aligned ranges to be unnecessarily remapped.
Additionally, if a split head shatters a 2MB page by ending exactly one page into the next block (for example, ending at 512), !IS_ALIGNED(512, 512) is false. This incorrectly skips adding the shattered range to the remap list, leaving stale 2MB mappings in the hardware.
Should the code use the exclusive bound head->last + 1 for all of these comparisons, including the IS_ALIGNED check?
Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 drm/amdkfd: Use exclusive bounds for SVM split alignment checks
In reply to Gerhard SchwanzerSVM ranges use inclusive page indices: prange->last is the last page in the range. The split-remap logic introduced by commit 448ee45353ef ("drm/amdkfd: Use huge page size to check split svm range alignment") uses ALIGN_DOWN(prange->last, 512) to determine whether the original range can contain a 2MB huge-page mapping.
That aligns the last page itself down. Thus a range ending one page before the next 2MB boundary is classified as if the final 2MB block did not exist. When such a range is split inside that final block, the split head or tail can be left off the remap list even though it was derived from an original range that may have PMD mappings.
Use prange->last + 1 as the exclusive upper bound when computing the original range's last 2MB-aligned boundary. Then use the actual split boundary for the head and tail alignment checks: tail->start for a tail split, and new_start for a head split. new_start is equivalent to head->last + 1 and directly names the exclusive end of the split head.
Using head->last for the head-side check can both remap a head that ends exactly one page before a 2MB boundary and miss a head whose split boundary is one page after such a boundary. Philip Yang pointed out in the review of the original change that this condition should use head->last + 1 or new_start.
Xiaogang Chen identified the inclusive-last cause and posted the candidate fix in the regression thread. With the culprit change active and the local revert not applied, the unchanged C/HSA reproducer completes 10/10 runs with this change on an RX 7600 XT.
---
Changes in v2:
- Use new_start for the head-side split-boundary checks, matching Philip Yang's original v4 review guidance and avoiding aligned-head false positives / unaligned-head false negatives.
- Keep the tail-side last + 1 exclusive-bound fix from Xiaogang's public candidate, which fixes the reproduced RX 7600 XT SDMA0 fault.
drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -4
@@ -1144,7 +1144,7 @@ static int svm_range_split_tail(struct svm_range *prange, uint64_t new_last, struct list_head *insert_list, struct list_head *remap_list) { - unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); + unsigned long last_align_down = ALIGN_DOWN(prange->last + 1, 512);unsigned long start_align =ALIGN (prange->start,512 );bool huge_page_mapping = last_align_down > start_align;struct svm_range *tail =NULL ;
@@ -1168,7 +1168,7 @@ static int svm_range_split_head(struct svm_range *prange, uint64_t new_start, struct list_head *insert_list, struct list_head *remap_list) { - unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); + unsigned long last_align_down = ALIGN_DOWN(prange->last + 1, 512);unsigned long start_align =ALIGN (prange->start,512 );bool huge_page_mapping = last_align_down > start_align;struct svm_range *head =NULL ;
@@ -1181,8 +1181,8 @@ svm_range_split_head(struct svm_range *prange, uint64_t new_start,list_add (&head->list, insert_list); -if (huge_page_mapping && head->last +1 > start_align && - head->last +1 < last_align_down && (!IS_ALIGNED (head->last,512 ))) +if (huge_page_mapping && new_start > start_align && + new_start < last_align_down && !IS_ALIGNED (new_start,512 ))list_add (&head->update_list, remap_list);return 0 ;
base-commit: 2c7d5b0a5ec0fc713a7f350806553643e87e6f43
Signature · 1 line
2.54.0
Re: [PATCH v2] drm/amdkfd: Use exclusive bounds for SVM split alignment checks
Quoted 30 lines · Gerhard Schwanzer
SVM ranges use inclusive page indices:
prange->lastis the last page in the range. The split-remap logic introduced by commit448ee45353ef("drm/amdkfd: Use huge page size to check split svm range alignment") usesALIGN_DOWN(prange->last, 512)to determine whether the original range can contain a 2MB huge-page mapping.That aligns the last page itself down. Thus a range ending one page before the next 2MB boundary is classified as if the final 2MB block did not exist. When such a range is split inside that final block, the split head or tail can be left off the remap list even though it was derived from an original range that may have PMD mappings.
Use
prange->last+ 1 as the exclusive upper bound when computing the original range's last 2MB-aligned boundary. Then use the actual split boundary for the head and tail alignment checks:tail->startfor a tail split, andnew_startfor a head split.new_startis equivalent tohead->last+ 1 and directly names the exclusive end of the split head.Using
head->lastfor the head-side check can both remap a head that ends exactly one page before a 2MB boundary and miss a head whose split boundary is one page after such a boundary. Philip Yang pointed out in the review of the original change that this condition should usehead->last+ 1 ornew_start.Xiaogang Chen identified the inclusive-last cause and posted the candidate fix in the regression thread. With the culprit change active and the local revert not applied, the unchanged C/HSA reproducer completes 10/10 runs with this change on an RX 7600 XT.
Quoted 7 lines
Fixes 448ee45353ef ("drm/amdkfd: Use huge page size to check split svm range alignment")Link https://lore.kernel.org/stable/IA1PR12MB85172F7FE9157C092EDA46A0E3112@IA1PR12MB8517.namprd12.prod.outlook.com/ open hereSuggested-by Xiaogang Chen <[email protected]>Signed-off-by Gerhard Schwanzer <[email protected]>
Sorry, I missed this patch earlier.
Applied. thanks.
Alex
Quoted 50 lines
---
Changes in v2:
- Usenew_startfor the head-side split-boundary checks, matching Philip Yang's original v4 review guidance and avoiding aligned-head false positives / unaligned-head false negatives.
- Keep the tail-side last + 1 exclusive-bound fix from Xiaogang's public candidate, which fixes the reproduced RX 7600 XT SDMA0 fault.drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4
-4@@ -1144,7 +1144,7 @@ static int svm_range_split_tail(struct svm_range *prange, uint64_t new_last, struct list_head *insert_list, struct list_head *remap_list) { - unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); + unsigned long last_align_down = ALIGN_DOWN(prange->last + 1, 512);unsigned long start_align =ALIGN (prange->start,512 );bool huge_page_mapping = last_align_down > start_align;struct svm_range *tail =NULL ;@@ -1168,7 +1168,7 @@ static int svm_range_split_head(struct svm_range *prange, uint64_t new_start, struct list_head *insert_list, struct list_head *remap_list) { - unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); + unsigned long last_align_down = ALIGN_DOWN(prange->last + 1, 512);unsigned long start_align =ALIGN (prange->start,512 );bool huge_page_mapping = last_align_down > start_align;struct svm_range *head =NULL ;@@ -1181,8 +1181,8 @@ svm_range_split_head(struct svm_range *prange, uint64_t new_start,list_add (&head->list, insert_list); -if (huge_page_mapping && head->last +1 > start_align && - head->last +1 < last_align_down && (!IS_ALIGNED (head->last,512 ))) +if (huge_page_mapping && new_start > start_align && + new_start < last_align_down && !IS_ALIGNED (new_start,512 ))list_add (&head->update_list, remap_list);return 0 ;base-commit:
2c7d5b0a5ec0fc713a7f350806553643e87e6f43--
2.54.0