[-next] drm/amdkfd: Fix NULL pointer dereference in svm_migrate_to_ram()

Message ID 20221025072822.129940-1-yang.lee@linux.alibaba.com
State New
Headers
Series [-next] drm/amdkfd: Fix NULL pointer dereference in svm_migrate_to_ram() |

Commit Message

Yang Li Oct. 25, 2022, 7:28 a.m. UTC
  ./drivers/gpu/drm/amd/amdkfd/kfd_migrate.c:985:58-62: ERROR: p is NULL but dereferenced.

Link: https://bugzilla.openanolis.cn/show_bug.cgi?id=2549
Reported-by: Abaci Robot <abaci@linux.alibaba.com>
Signed-off-by: Yang Li <yang.lee@linux.alibaba.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Felix Kuehling Oct. 25, 2022, 7:39 p.m. UTC | #1
Am 2022-10-25 um 03:28 schrieb Yang Li:
> ./drivers/gpu/drm/amd/amdkfd/kfd_migrate.c:985:58-62: ERROR: p is NULL but dereferenced.
>
> Link: https://bugzilla.openanolis.cn/show_bug.cgi?id=2549
> Reported-by: Abaci Robot <abaci@linux.alibaba.com>
> Signed-off-by: Yang Li <yang.lee@linux.alibaba.com>
> ---
>   drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> index cddf259875c0..405dd51521dc 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> @@ -981,7 +981,8 @@ static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)
>   out_mmput:
>   	mmput(mm);
>   
> -	pr_debug("CPU fault svms 0x%p address 0x%lx done\n", &p->svms, addr);
> +	if (p)
> +		pr_debug("CPU fault svms 0x%p address 0x%lx done\n", &p->svms, addr);

Thank you for catching and reporting this problem. I think the correct 
solution would be to move the pr_debug up before the kfd_unref_process 
call. That way you're sure that the pointer is initialized and that it 
represents a valid reference to the kfd_process structure.

Regards,
   Felix


>   
>   	return r ? VM_FAULT_SIGBUS : 0;
>   }
  

Patch

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
index cddf259875c0..405dd51521dc 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
@@ -981,7 +981,8 @@  static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)
 out_mmput:
 	mmput(mm);
 
-	pr_debug("CPU fault svms 0x%p address 0x%lx done\n", &p->svms, addr);
+	if (p)
+		pr_debug("CPU fault svms 0x%p address 0x%lx done\n", &p->svms, addr);
 
 	return r ? VM_FAULT_SIGBUS : 0;
 }