When allocating the PFN array fails, amdgpu_hmm_range_get_pages()
returns -ENOMEM before setting the notifier of the range, but
svm_range_validate_and_map() still checks the range, as it is not NULL,
before looking at the error. On navi48, setting SVM attributes with
failslab:

  KASAN: null-ptr-deref in range [0x0000000000000050-0x0000000000000057]
  RIP: 0010:amdgpu_hmm_range_valid+0x66/0xa0 [amdgpu]
   svm_range_validate_and_map+0xaef/0x4e40 [amdgpu]
   svm_range_set_attr+0x193e/0x4610 [amdgpu]
   kfd_ioctl_svm+0x137/0x320 [amdgpu]

Check the error first; it is only 0 when the pages were taken.

Signed-off-by: Jesse Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index a571fda5c713..0879e37faa71 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1895,11 +1895,12 @@ static int svm_range_validate_and_map(struct mm_struct 
*mm,
 
                svm_range_lock(prange);
 
-               /* Free backing memory of hmm_range if it was initialized
-                * Override return value to TRY AGAIN only if prior returns
-                * were successful
+               /*
+                * Retry if the pages were invalidated after they were taken.
+                * Only check that when taking them succeeded: a range can exist
+                * without a notifier, e.g. when allocating its PFN array 
failed.
                 */
-               if (range && !amdgpu_hmm_range_valid(range) && !r) {
+               if (!r && !amdgpu_hmm_range_valid(range)) {
                        pr_debug("hmm update the range, need validate again\n");
                        r = -EAGAIN;
                }
-- 
2.50.1

Reply via email to