Skip to contentSkip to conversation

drm/amdgpu: Fix multi-level page table bugs for large BOs v3

PATCH In review Felix Kuehling 3 participants 4 messages Mar 29, 2017 – Mar 30, 2017 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix multi-level page table bugs for large BOs v3

Fix the start/end address calculation for address ranges that span multiple page directories in amdgpu_vm_alloc_levels.

Add error messages if page tables aren't found. Otherwise the page table update would just fail silently.

v2:
* Change WARN_ON to WARN_ON_ONCE
* Move masking of high address bits to caller
* Add range-check for "from" and "to"
v3:
* Replace WARN_ON_ONCE in get_pt with pr_err in caller

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +18 -6
index 818747f..c11d15e 100644
@@ -275,13 +275,18 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
 		memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
 	}
 
-	from = (saddr >> shift) % amdgpu_vm_num_entries(adev, level);
-	to = (eaddr >> shift) % amdgpu_vm_num_entries(adev, level);
+	from = saddr >> shift;
+	to = eaddr >> shift;
+	if (from >= amdgpu_vm_num_entries(adev, level) ||
+	    to >= amdgpu_vm_num_entries(adev, level))
+		return -EINVAL;
 
 	if (to > parent->last_entry_used)
 		parent->last_entry_used = to;
 
 	++level;
+	saddr = saddr & ((1 << shift) - 1);
+	eaddr = eaddr & ((1 << shift) - 1);
 
 	/* walk over the address space and allocate the page tables */
 	for (pt_idx = from; pt_idx <= to; ++pt_idx) {
@@ -312,8 +317,11 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
 		}
 
 		if (level < adev->vm_manager.num_level) {
-			r = amdgpu_vm_alloc_levels(adev, vm, entry, saddr,
-						   eaddr, level);
+			uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
+			uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
+				((1 << shift) - 1);
+			r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
+						   sub_eaddr, level);
 			if (r)
 				return r;
 		}
@@ -990,8 +998,10 @@ static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
 	/* initialize the variables */
 	addr = start;
 	pt = amdgpu_vm_get_pt(params, addr);
-	if (!pt)
+	if (!pt) {
+		pr_err("PT not found, aborting update_ptes\n");
 		return;
+	}
 
 	if (params->shadow) {
 		if (!pt->shadow)
@@ -1015,8 +1025,10 @@ static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
 	/* walk over the address space and update the page tables */
 	while (addr < end) {
 		pt = amdgpu_vm_get_pt(params, addr);
-		if (!pt)
+		if (!pt) {
+			pr_err("PT not found, aborting update_ptes\n");
 			return;
+		}
 
 		if (params->shadow) {
 			if (!pt->shadow)
Signature · 6 lines
1.9.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1490808171-21714-1-git-send-email-Felix.Kuehling-5C7GfCeVMHo@public.gmane.org

#2 lore raw

Am 29.03.2017 um 19:22 schrieb Felix Kuehling:

Quoted 6 lines

Fix the start/end address calculation for address ranges that span multiple page directories in amdgpu_vm_alloc_levels.

Add error messages if page tables aren't found. Otherwise the page table update would just fail silently.

Quoted 8 lines

v2:
* Change WARN_ON to WARN_ON_ONCE
* Move masking of high address bits to caller
* Add range-check for "from" and "to"
v3:
* Replace WARN_ON_ONCE in get_pt with pr_err in caller

Mhm, I still don't like that we modify saddr/eaddr here because I wanted to resolve the recursion sooner or later.

But anyway that is work for a future patch, this one is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 57 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++++++++++++++++++------
  1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +15 -5
index 818747f..c11d15e 100644
@@ -275,13 +275,18 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  		memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
  	}
 
-	from = (saddr >> shift) % amdgpu_vm_num_entries(adev, level);
-	to = (eaddr >> shift) % amdgpu_vm_num_entries(adev, level);
+	from = saddr >> shift;
+	to = eaddr >> shift;
+	if (from >= amdgpu_vm_num_entries(adev, level) ||
+	    to >= amdgpu_vm_num_entries(adev, level))
+		return -EINVAL;
 
  	if (to > parent->last_entry_used)
  		parent->last_entry_used = to;
 
  	++level;
+	saddr = saddr & ((1 << shift) - 1);
+	eaddr = eaddr & ((1 << shift) - 1);
 
  	/* walk over the address space and allocate the page tables */
  	for (pt_idx = from; pt_idx <= to; ++pt_idx) {
@@ -312,8 +317,11 @@ static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  		}
 
  		if (level < adev->vm_manager.num_level) {
-			r = amdgpu_vm_alloc_levels(adev, vm, entry, saddr,
-						   eaddr, level);
+			uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
+			uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
+				((1 << shift) - 1);
+			r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
+						   sub_eaddr, level);
  			if (r)
  				return r;
  		}
@@ -990,8 +998,10 @@ static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  	/* initialize the variables */
  	addr = start;
  	pt = amdgpu_vm_get_pt(params, addr);
-	if (!pt)
+	if (!pt) {
+		pr_err("PT not found, aborting update_ptes\n");
  		return;
+	}
 
  	if (params->shadow) {
  		if (!pt->shadow)
@@ -1015,8 +1025,10 @@ static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  	/* walk over the address space and update the page tables */

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, · line 1034

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -1
  	while (addr < end) {
  		pt = amdgpu_vm_get_pt(params, addr);
-		if (!pt)
+		if (!pt) {
+			pr_err("PT not found, aborting update_ptes\n");
  			return;
+		}
 
  		if (params->shadow) {
  			if (!pt->shadow)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 3beac896-9cb5-de7f-a94e-2a0f4621b537-5C7GfCeVMHo@public.gmane.org

#4 lore raw
Quoted 14 lines · Christian König

Am 29.03.2017 um 19:22 schrieb Felix Kuehling:

Quoted 13 lines

Fix the start/end address calculation for address ranges that span multiple page directories in amdgpu_vm_alloc_levels.

Add error messages if page tables aren't found. Otherwise the page table update would just fail silently.

v2:
* Change WARN_ON to WARN_ON_ONCE
* Move masking of high address bits to caller
* Add range-check for "from" and "to"
v3:
* Replace WARN_ON_ONCE in get_pt with pr_err in caller

Quoted 7 lines
Quoted 1 line

Mhm, I still don't like that we modify saddr/eaddr here because I wanted to resolve the recursion sooner or later.

But anyway that is work for a future patch, this one is Reviewed-by:
Christian König <[email protected]>.

Thank you, I appreciate it. I realize it's probably not perfect. But I'm just looking at the intention of the original code and trying to make it work correctly for KFD so we can continue integrating amdgpu changes (and important fixes, such as VM fault handling) into the KFD branch. That's also why I'm a bit in a rush. This was actually blocking KFD.

We take a different approach from hybrid. We do a nightly git-merge from amd-staging-4.9 and running it through our automated pre-submission tests. If the merge doesn't have conflicts that process is completely automated (except for someone pressing the Submit button in the end). If the testing fails, we have to block integrations until the problem is fixed upstream. So I'm fixing the problem upstream. ;)

Regards,
Felix

Quoted 61 lines

Regards,
Christian.

Quoted 57 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++++++++++++++++++------
  1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +15 -4
index 818747f..c11d15e 100644
@@ -275,13 +275,18 @@ static int amdgpu_vm_alloc_levels(struct
 amdgpu_device *adev,
          memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
      }
  -    from = (saddr >> shift) % amdgpu_vm_num_entries(adev, level);
-    to = (eaddr >> shift) % amdgpu_vm_num_entries(adev, level);
+    from = saddr >> shift;
+    to = eaddr >> shift;
+    if (from >= amdgpu_vm_num_entries(adev, level) ||
+        to >= amdgpu_vm_num_entries(adev, level))
+        return -EINVAL;
        if (to > parent->last_entry_used)
          parent->last_entry_used = to;
        ++level;
+    saddr = saddr & ((1 << shift) - 1);
+    eaddr = eaddr & ((1 << shift) - 1);
        /* walk over the address space and allocate the page tables */
      for (pt_idx = from; pt_idx <= to; ++pt_idx) {
@@ -312,8 +317,11 @@ static int amdgpu_vm_alloc_levels(struct
 amdgpu_device *adev,
          }
            if (level < adev->vm_manager.num_level) {
-            r = amdgpu_vm_alloc_levels(adev, vm, entry, saddr,
-                           eaddr, level);
+            uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
+            uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
+                ((1 << shift) - 1);
+            r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
+                           sub_eaddr, level);
              if (r)
                  return r;
          }
@@ -990,8 +998,10 @@ static void amdgpu_vm_update_ptes(struct
 amdgpu_pte_update_params *params,
      /* initialize the variables */
      addr = start;
      pt = amdgpu_vm_get_pt(params, addr);
-    if (!pt)
+    if (!pt) {
+        pr_err("PT not found, aborting update_ptes\n");
          return;
+    }
        if (params->shadow) {
          if (!pt->shadow)
@@ -1015,8 +1025,10 @@ static void amdgpu_vm_update_ptes(struct
 amdgpu_pte_update_params *params,
      /* walk over the address space and update the page tables */
      while (addr < end) {
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, · line 1007

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -1
          pt = amdgpu_vm_get_pt(params, addr);
-        if (!pt)
+        if (!pt) {
+            pr_err("PT not found, aborting update_ptes\n");
              return;
+        }
            if (params->shadow) {
              if (!pt->shadow)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 7ea76115-90cc-3153-a410-d39bc291f697-5C7GfCeVMHo@public.gmane.org

Reviewed-by Junwei Zhang
#6 lore raw
Quoted 20 lines · Felix Kuehling
Quoted 19 lines · Christian König

Am 29.03.2017 um 19:22 schrieb Felix Kuehling:

Quoted 6 lines

Fix the start/end address calculation for address ranges that span multiple page directories in amdgpu_vm_alloc_levels.

Add error messages if page tables aren't found. Otherwise the page table update would just fail silently.

Quoted 8 lines

v2:
* Change WARN_ON to WARN_ON_ONCE
* Move masking of high address bits to caller
* Add range-check for "from" and "to"
v3:
* Replace WARN_ON_ONCE in get_pt with pr_err in caller

Mhm, I still don't like that we modify saddr/eaddr here because I wanted to resolve the recursion sooner or later.

Quoted 8 lines
Quoted 2 lines

But anyway that is work for a future patch, this one is Reviewed-by:
Christian König <[email protected]>.

Thank you, I appreciate it. I realize it's probably not perfect. But I'm just looking at the intention of the original code and trying to make it work correctly for KFD so we can continue integrating amdgpu changes (and important fixes, such as VM fault handling) into the KFD branch. That's also why I'm a bit in a rush. This was actually blocking KFD.

Anyway, let's make it work for all of us.
It will become better in coding, I believe. :)

Quoted 8 lines

We take a different approach from hybrid. We do a nightly git-merge from amd-staging-4.9 and running it through our automated pre-submission tests. If the merge doesn't have conflicts that process is completely automated (except for someone pressing the Submit button in the end). If the testing fails, we have to block integrations until the problem is fixed upstream. So I'm fixing the problem upstream. ;)

Quoted 75 lines

Regards,
Felix

Quoted 71 lines

Regards,
Christian.

Quoted 65 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++++++++++++++++++------
   1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +18 -5
index 818747f..c11d15e 100644
@@ -275,13 +275,18 @@ static int amdgpu_vm_alloc_levels(struct
 amdgpu_device *adev,
           memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
       }
   -    from = (saddr >> shift) % amdgpu_vm_num_entries(adev, level);
-    to = (eaddr >> shift) % amdgpu_vm_num_entries(adev, level);
+    from = saddr >> shift;
+    to = eaddr >> shift;
+    if (from >= amdgpu_vm_num_entries(adev, level) ||
+        to >= amdgpu_vm_num_entries(adev, level))
+        return -EINVAL;
         if (to > parent->last_entry_used)
           parent->last_entry_used = to;
         ++level;
+    saddr = saddr & ((1 << shift) - 1);
+    eaddr = eaddr & ((1 << shift) - 1);
         /* walk over the address space and allocate the page tables */
       for (pt_idx = from; pt_idx <= to; ++pt_idx) {
@@ -312,8 +317,11 @@ static int amdgpu_vm_alloc_levels(struct
 amdgpu_device *adev,
           }
             if (level < adev->vm_manager.num_level) {
-            r = amdgpu_vm_alloc_levels(adev, vm, entry, saddr,
-                           eaddr, level);
+            uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
+            uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
+                ((1 << shift) - 1);
+            r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
+                           sub_eaddr, level);
               if (r)
                   return r;
           }
@@ -990,8 +998,10 @@ static void amdgpu_vm_update_ptes(struct
 amdgpu_pte_update_params *params,
       /* initialize the variables */
       addr = start;
       pt = amdgpu_vm_get_pt(params, addr);
-    if (!pt)
+    if (!pt) {
+        pr_err("PT not found, aborting update_ptes\n");
           return;
+    }
         if (params->shadow) {
           if (!pt->shadow)
@@ -1015,8 +1025,10 @@ static void amdgpu_vm_update_ptes(struct
 amdgpu_pte_update_params *params,
       /* walk over the address space and update the page tables */
       while (addr < end) {
           pt = amdgpu_vm_get_pt(params, addr);
-        if (!pt)
+        if (!pt) {
+            pr_err("PT not found, aborting update_ptes\n");
               return;
+        }
             if (params->shadow) {
               if (!pt->shadow)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx