Skip to contentSkip to conversation

drm/amdkfd: Use huge page size to check split svm range alignment

PATCH Superseded Xiaogang.Chen 2 participants 4 messages Nov 18, 2025 – Nov 20, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 drm/amdkfd: Use huge page size to check split svm range alignment

From: Xiaogang Chen <[email protected]>

Fixes 7ef6b2d4b7e5 (drm/amdkfd: remap unaligned svm ranges that have split)

When split svm ranges that have been mapped using huge page should use huge page size(2MB) to check split range alignment, not prange->granularity that means migration granularity.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 63 ++++++++++++++++++++++++++--
 1 file changed, 59 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +59 -4
index 521c14c7a789..7bb94555e5f9 100644
@@ -1145,12 +1145,39 @@ svm_range_split_tail(struct svm_range *prange, uint64_t new_last,
 		     struct list_head *insert_list, struct list_head *remap_list)
 {
 	struct svm_range *tail = NULL;
+	unsigned long start_align = ALIGN(prange->start, 512);
+	unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
 	int r = svm_range_split(prange, prange->start, new_last, &tail);
+	bool huge_page_mapping = (last_align_down > start_align) &&
+				 (last_align_down - start_align) >= 512;
 
 	if (!r) {
 		list_add(&tail->list, insert_list);
-		if (!IS_ALIGNED(new_last + 1, 1UL << prange->granularity))
-			list_add(&tail->update_list, remap_list);
+		/* original prange has huge page mapping */
+		if (huge_page_mapping) {
+
+			/* tail->start is inside huge page mapping but not 2MB aligned
+			 * or tail size is smaller than 512 pages
+			 */
+			if (tail->start >= start_align && tail->start <= last_align_down &&
+				(!IS_ALIGNED(tail->start, 512) ||
+				(tail->last - tail->start) < 512)) {
+
+				list_add(&tail->update_list, remap_list);
+				return 0;
+			}
+
+			/* tail->last is inside huge page mapping but not 2MB aligned
+			 * or tail size is smaller than 512 pages
+			 */
+			if (tail->last >= start_align && tail->last <= last_align_down &&
+				(!IS_ALIGNED(tail->last, 512) ||
+				(tail->last - tail->start) < 512)) {
+
+				list_add(&tail->update_list, remap_list);
+				return 0;
+			}
+		}
 	}
 	return r;
 }
@@ -1160,13 +1187,41 @@ svm_range_split_head(struct svm_range *prange, uint64_t new_start,
 		     struct list_head *insert_list, struct list_head *remap_list)
 {
 	struct svm_range *head = NULL;
+	unsigned long start_align = ALIGN(prange->start, 512);
+	unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
 	int r = svm_range_split(prange, new_start, prange->last, &head);
+	bool huge_page_mapping = (last_align_down >= start_align) &&
+				 (last_align_down - start_align) >= 512;
 
 	if (!r) {
 		list_add(&head->list, insert_list);
-		if (!IS_ALIGNED(new_start, 1UL << prange->granularity))
-			list_add(&head->update_list, remap_list);
+
+		/* original prange has huge page mapping */
+		if (huge_page_mapping) {
+			/* head->start is inside huge page mapping but not 2MB aligned
+			 * or head size is smaller than 512 pages
+			 */
+			if (head->start >= start_align && head->start <= last_align_down &&
+				(!IS_ALIGNED(head->start, 512) ||
+				(head->last - head->start) < 512)) {
+
+				list_add(&head->update_list, remap_list);
+				return 0;
+			}
+
+			/* head->last is inside huge page mapping but not 2MB aligned
+			 * or head size is smaller than 512 pages
+			 */
+			if (head->last >= start_align && head->last <= last_align_down &&
+				(!IS_ALIGNED(head->last, 512) ||
+				(head->last - head->start) < 512)) {
+
+				list_add(&head->update_list, remap_list);
+				return 0;
+			}
+		}
 	}
+
 	return r;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 18 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

Fixes 7ef6b2d4b7e5 (drm/amdkfd: remap unaligned svm ranges that have split)

When split svm ranges that have been mapped using huge page should use huge page size(2MB) to check split range alignment, not prange->granularity that means migration granularity.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 63 ++++++++++++++++++++++++++--
  1 file changed, 59 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index 521c14c7a789..7bb94555e5f9 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_tail(struct svm_range *prange, uint64_t new_last, · line 1150

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -1145,12 +1145,39 @@ svm_range_split_tail(struct svm_range *prange, uint64_t new_last,
  		     struct list_head *insert_list, struct list_head *remap_list)
  {
  	struct svm_range *tail = NULL;
+	unsigned long start_align = ALIGN(prange->start, 512);
+	unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
  	int r = svm_range_split(prange, prange->start, new_last, &tail);

move this function call after variable definition, easier to read.

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+	bool huge_page_mapping = (last_align_down > start_align) &&
+				 (last_align_down - start_align) >= 512;

= 512 is redundant because start, last already aligned to 512

  	if (!r) {
  		list_add(&tail->list, insert_list);

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_tail(struct svm_range *prange, uint64_t new_last, · line 1163

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -2
-		if (!IS_ALIGNED(new_last + 1, 1UL << prange->granularity))
-			list_add(&tail->update_list, remap_list);
+		/* original prange has huge page mapping */
+		if (huge_page_mapping) {
+
+			/* tail->start is inside huge page mapping but not 2MB aligned
+			 * or tail size is smaller than 512 pages
+			 */
+			if (tail->start >= start_align && tail->start <= last_align_down &&
+				(!IS_ALIGNED(tail->start, 512) ||

split from the tail, new range is the last part of prange, don't need check size

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_tail(struct svm_range *prange, uint64_t new_last, · line 1172

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9 -0
+				(tail->last - tail->start) < 512)) {
+
+				list_add(&tail->update_list, remap_list);
+				return 0;
+			}
+
+			/* tail->last is inside huge page mapping but not 2MB aligned
+			 * or tail size is smaller than 512 pages
+			 */

tail->last is the original prange->last, impossible inside prange.

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_tail(struct svm_range *prange, uint64_t new_last, · line 1181

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -0
+			if (tail->last >= start_align && tail->last <= last_align_down &&
+				(!IS_ALIGNED(tail->last, 512) ||
+				(tail->last - tail->start) < 512)) {
+
+				list_add(&tail->update_list, remap_list);
+				return 0;
+			}
+		}
  	}

to refactor the conditions

	unsigned long start_align = ALIGN(prange->start, 512);
	unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
	bool huge_page_mapping = last_align_down > start_align;

int r = svm_range_split(prange, prange->start, new_last, &tail);
if (r || !huge_page_mapping) return r; if (tail->start > start_align && tail->start < last_align_down &&

	    (!IS_ALIGNED(tail->start, 512))
		list_add(&tail->update_list, remap_list);

return 0;
}

do the similar change for below split head.

Regards,
Philip

@@ -1160,13 +1187,41 @@ svm_range_split_head(struct svm_range *prange, uint64_t new_start,
struct list_head *insert_list, struct list_head *remap_list)
{
struct svm_range *head = NULL;
+ unsigned long start_align = ALIGN(prange->start, 512);
+ unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
int r = svm_range_split(prange, new_start, prange->last, &head);
+ bool huge_page_mapping = (last_align_down >= start_align) &&
+ (last_align_down - start_align) >= 512;

if (!r) {
list_add(&head->list, insert_list);
- if (!IS_ALIGNED(new_start, 1UL << prange->granularity))
- list_add(&head->update_list, remap_list);
+
+ /* original prange has huge page mapping */
+ if (huge_page_mapping) {
+ /* head->start is inside huge page mapping but not 2MB aligned
+ * or head size is smaller than 512 pages
+ */
+ if (head->start >= start_align && head->start <= last_align_down &&
+ (!IS_ALIGNED(head->start, 512) ||
+ (head->last - head->start) < 512)) {
+
+ list_add(&head->update_list, remap_list);
+ return 0;
+ }
+
+ /* head->last is inside huge page mapping but not 2MB aligned
+ * or head size is smaller than 512 pages
+ */
+ if (head->last >= start_align && head->last <= last_align_down &&
+ (!IS_ALIGNED(head->last, 512) ||
+ (head->last - head->start) < 512)) {
+
+ list_add(&head->update_list, remap_list);
+ return 0;
+ }
+ }
}
+
return r;
}

#2 lore raw

resend, to fix the code format issue.

Quoted 68 lines · Philip Yang
Quoted 18 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

Fixes 7ef6b2d4b7e5 (drm/amdkfd: remap unaligned svm ranges that have split)

When split svm ranges that have been mapped using huge page should use huge page size(2MB) to check split range alignment, not prange->granularity that means migration granularity.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 63 ++++++++++++++++++++++++++--

  1 file changed, 59 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index 521c14c7a789..7bb94555e5f9 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_tail(struct svm_range · line 1151

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -1145,12 +1145,39 @@ svm_range_split_tail(struct svm_range 
 *prange, uint64_t new_last,
                struct list_head *insert_list, struct list_head *remap_list)
   {
       struct svm_range *tail = NULL;
+    unsigned long start_align = ALIGN(prange->start, 512);
+    unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
       int r = svm_range_split(prange, prange->start, new_last, &tail);

move this function call after variable definition, easier to read.

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+    bool huge_page_mapping = (last_align_down > start_align) &&
+                 (last_align_down - start_align) >= 512;

= 512 is redundant because start, last already aligned to 512 if (!r) {
          list_add(&tail->list, insert_list);
-        if (!IS_ALIGNED(new_last + 1, 1UL << prange->granularity)) -            list_add(&tail->update_list, remap_list);
+        /* original prange has huge page mapping */
+        if (huge_page_mapping) {

Quoted 8 lines

+
+            /* tail->start is inside huge page mapping but not 2MB aligned
+             * or tail size is smaller than 512 pages
+             */
+            if (tail->start >= start_align && tail->start <= last_align_down &&
+                (!IS_ALIGNED(tail->start, 512) ||

split from the tail, new range is the last part of prange, don't need check size

Quoted 10 lines

+                (tail->last - tail->start) < 512)) {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
+
+                list_add(&tail->update_list, remap_list);
+                return 0;
+            }
+
+            /* tail->last is inside huge page mapping but not 2MB 

aligned
+             * or tail size is smaller than 512 pages +             */

tail->last is the original prange->last, impossible inside prange.

Quoted 10 lines

+            if (tail->last >= start_align && tail->last <= last_align_down &&
+                (!IS_ALIGNED(tail->last, 512) || +                (tail->last - tail->start) < 512)) { +
+                list_add(&tail->update_list, remap_list); +                return 0;
+            }
+        }
      }

Quoted 1 line

to refactor the conditions

    unsigned long start_align = ALIGN(prange->start, 512); unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); bool huge_page_mapping = last_align_down > start_align; int r;

    r = svm_range_split(prange, prange->start, new_last, &tail);

    if (r || !huge_page_mapping)
        return r;

    if (tail->start > start_align && tail->start < last_align_down && (!IS_ALIGNED(tail->start, 512)) list_add(&tail->update_list, remap_list);

    return 0;

Quoted 56 lines

do the similar change for below split head.

Regards,
Philip

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -2
@@ -1160,13 +1187,41 @@ svm_range_split_head(struct svm_range *prange, 
 uint64_t new_start,
               struct list_head *insert_list, struct list_head *remap_list)
  {
      struct svm_range *head = NULL;
+    unsigned long start_align = ALIGN(prange->start, 512);
+    unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
      int r = svm_range_split(prange, new_start, prange->last, &head);
+    bool huge_page_mapping = (last_align_down >= start_align) &&
+                 (last_align_down - start_align) >= 512;
 
      if (!r) {
          list_add(&head->list, insert_list);
-        if (!IS_ALIGNED(new_start, 1UL << prange->granularity))
-            list_add(&head->update_list, remap_list);
+
+        /* original prange has huge page mapping */
+        if (huge_page_mapping) {
+            /* head->start is inside huge page mapping but not 2MB 

aligned
+             * or head size is smaller than 512 pages +             */
+            if (head->start >= start_align && head->start <= last_align_down &&
+                (!IS_ALIGNED(head->start, 512) || +                (head->last - head->start) < 512)) {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
+
+                list_add(&head->update_list, remap_list);
+                return 0;
+            }
+
+            /* head->last is inside huge page mapping but not 2MB 

aligned
+             * or head size is smaller than 512 pages +             */
+            if (head->last >= start_align && head->last <= last_align_down &&
+                (!IS_ALIGNED(head->last, 512) || +                (head->last - head->start) < 512)) { +
+                list_add(&head->update_list, remap_list); +                return 0;
+            }
+        }
     }
+
     return r;
 }

#3 lore raw
Quoted 81 lines · Philip Yang

resend, to fix the code format issue.

Quoted 69 lines · Philip Yang
Quoted 19 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

Fixes 7ef6b2d4b7e5 (drm/amdkfd: remap unaligned svm ranges that have split)

When split svm ranges that have been mapped using huge page should use huge page size(2MB) to check split range alignment, not prange->granularity that means migration granularity.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 63
++++++++++++++++++++++++++--

  1 file changed, 59 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index 521c14c7a789..7bb94555e5f9 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_tail(struct svm_range · line 1151

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -1145,12 +1145,39 @@ svm_range_split_tail(struct svm_range 
 *prange, uint64_t new_last,
                struct list_head *insert_list, struct list_head *remap_list)
   {
       struct svm_range *tail = NULL;
+    unsigned long start_align = ALIGN(prange->start, 512);
+    unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
       int r = svm_range_split(prange, prange->start, new_last, &tail);

move this function call after variable definition, easier to read.

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
+    bool huge_page_mapping = (last_align_down > start_align) &&
+                 (last_align_down - start_align) >= 512;

= 512 is redundant because start, last already aligned to 512 if (!r) {
          list_add(&tail->list, insert_list);
-        if (!IS_ALIGNED(new_last + 1, 1UL << prange->granularity)) -            list_add(&tail->update_list, remap_list);
+        /* original prange has huge page mapping */
+        if (huge_page_mapping) {

Quoted 8 lines

+
+            /* tail->start is inside huge page mapping but not 2MB aligned
+             * or tail size is smaller than 512 pages
+             */
+            if (tail->start >= start_align && tail->start <= last_align_down &&
+                (!IS_ALIGNED(tail->start, 512) ||

split from the tail, new range is the last part of prange, don't need check size

Quoted 10 lines

+                (tail->last - tail->start) < 512)) {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
+
+                list_add(&tail->update_list, remap_list);
+                return 0;
+            }
+
+            /* tail->last is inside huge page mapping but not 2MB 

aligned
+             * or tail size is smaller than 512 pages +             */

tail->last is the original prange->last, impossible inside prange.

Quoted 10 lines

+            if (tail->last >= start_align && tail->last <= last_align_down &&
+                (!IS_ALIGNED(tail->last, 512) || +                (tail->last - tail->start) < 512)) { +
+                list_add(&tail->update_list, remap_list); +                return 0;
+            }
+        }
      }

Quoted 1 line

to refactor the conditions

    unsigned long start_align = ALIGN(prange->start, 512); unsigned long last_align_down = ALIGN_DOWN(prange->last, 512); bool huge_page_mapping = last_align_down > start_align; int r;

    r = svm_range_split(prange, prange->start, new_last, &tail);

Quoted 8 lines

    if (r || !huge_page_mapping)
        return r;

    if (tail->start > start_align && tail->start < last_align_down && (!IS_ALIGNED(tail->start, 512)) list_add(&tail->update_list, remap_list);

    return 0;

ok, that makes code concise. Thanks.

Still need add tail to insert list:

list_add(&tail->list, insert_list);

Regards

Xiaogang

Quoted 57 lines
Quoted 56 lines

do the similar change for below split head.

Regards,
Philip

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -2
@@ -1160,13 +1187,41 @@ svm_range_split_head(struct svm_range 
 *prange, uint64_t new_start,
               struct list_head *insert_list, struct list_head *remap_list)
  {
      struct svm_range *head = NULL;
+    unsigned long start_align = ALIGN(prange->start, 512);
+    unsigned long last_align_down = ALIGN_DOWN(prange->last, 512);
      int r = svm_range_split(prange, new_start, prange->last, &head);
+    bool huge_page_mapping = (last_align_down >= start_align) &&
+                 (last_align_down - start_align) >= 512;
 
      if (!r) {
          list_add(&head->list, insert_list);
-        if (!IS_ALIGNED(new_start, 1UL << prange->granularity))
-            list_add(&head->update_list, remap_list);
+
+        /* original prange has huge page mapping */
+        if (huge_page_mapping) {
+            /* head->start is inside huge page mapping but not 2MB 

aligned
+             * or head size is smaller than 512 pages +             */
+            if (head->start >= start_align && head->start <= last_align_down &&
+                (!IS_ALIGNED(head->start, 512) || +                (head->last - head->start) < 512)) {

drivers/gpu/drm/amd/amdkfd/kfd_svm.c +6 -0
+
+                list_add(&head->update_list, remap_list);
+                return 0;
+            }
+
+            /* head->last is inside huge page mapping but not 2MB 

aligned
+             * or head size is smaller than 512 pages +             */
+            if (head->last >= start_align && head->last <= last_align_down &&
+                (!IS_ALIGNED(head->last, 512) || +                (head->last - head->start) < 512)) { +
+                list_add(&head->update_list, remap_list); +                return 0;
+            }
+        }
     }
+
     return r;
 }