Skip to contentSkip to conversation

drm/amdkfd: Fix mmap write lock not release

PATCH In review Philip Yang 4 participants 4 messages Sep 16, 2025 – Sep 18, 2025

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

PATCH 1/1 drm/amdkfd: Fix mmap write lock not release

If mmap write lock is taken while draining retry fault, mmap write lock is not released because svm_range_restore_pages calls mmap_read_unlock then returns. This causes deadlock and systen hang later because mmap read or write lock cannot be taken.

Downgrade mmap write lock to read lock if draining retry fault fix this bug.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
index 6604a37b304f..fb02ff9ae62a 100644
@@ -3043,6 +3043,8 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
 	if (svms->checkpoint_ts[gpuidx] != 0) {
 		if (amdgpu_ih_ts_after_or_equal(ts,  svms->checkpoint_ts[gpuidx])) {
 			pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
+			if (write_locked)
+				mmap_write_downgrade(mm);
 			r = -EAGAIN;
 			goto out_unlock_svms;
 		} else {
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Philip Yang
Sent: Tuesday, September 16, 2025 6:41 PM
To: [email protected]
Cc: Kuehling, Felix <[email protected]>; Yang, Philip <[email protected]>
Subject: [PATCH] drm/amdkfd: Fix mmap write lock not release

If mmap write lock is taken while draining retry fault, mmap write lock is not released because svm_range_restore_pages calls mmap_read_unlock then returns. This causes deadlock and systen hang later because mmap read or write lock cannot be taken.

Downgrade mmap write lock to read lock if draining retry fault fix this bug.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
index 6604a37b304f..fb02ff9ae62a 100644
@@ -3043,6 +3043,8 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
        if (svms->checkpoint_ts[gpuidx] != 0) {
                if (amdgpu_ih_ts_after_or_equal(ts,  svms->checkpoint_ts[gpuidx])) {
                        pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
+                       if (write_locked)
+                               mmap_write_downgrade(mm);
                        r = -EAGAIN;
                        goto out_unlock_svms;
                } else {

--
2.49.0

#2 lore raw
In reply to Philip Yang
Quoted 18 lines · Philip Yang

If mmap write lock is taken while draining retry fault, mmap write lock is not released because svm_range_restore_pages calls mmap_read_unlock then returns. This causes deadlock and systen hang later because mmap read or write lock cannot be taken.

Downgrade mmap write lock to read lock if draining retry fault fix this bug.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index 6604a37b304f..fb02ff9ae62a 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, · line 3047

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
@@ -3043,6 +3043,8 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
  	if (svms->checkpoint_ts[gpuidx] != 0) {
  		if (amdgpu_ih_ts_after_or_equal(ts,  svms->checkpoint_ts[gpuidx])) {
  			pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
+			if (write_locked)
+				mmap_write_downgrade(mm);

Is there unlock order issue? Now it holds svms->lock first, then mmap read lock after mmap_write_downgrade. The unlock should be mmap_read_unlock(mm), then mutex_unlock(&svms->lock).  "goto out_unlock_svms" does it in reverse order.

Regards

Xiaogang

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
  			r = -EAGAIN;
  			goto out_unlock_svms;
  		} else {
#3 lore raw
Quoted 16 lines · Xiaogang
Quoted 13 lines · Philip Yang

If mmap write lock is taken while draining retry fault, mmap write lock is not released because svm_range_restore_pages calls mmap_read_unlock then returns. This causes deadlock and systen hang later because mmap read or write lock cannot be taken.

Downgrade mmap write lock to read lock if draining retry fault fix this bug.

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 13 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, · line 3047

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -0
index 6604a37b304f..fb02ff9ae62a 100644
@@ -3043,6 +3043,8 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
  	if (svms->checkpoint_ts[gpuidx] != 0) {
  		if (amdgpu_ih_ts_after_or_equal(ts,  svms->checkpoint_ts[gpuidx])) {
  			pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
+			if (write_locked)
+				mmap_write_downgrade(mm);

Is there unlock order issue? Now it holds svms->lock first, then mmap read lock after mmap_write_downgrade. The unlock should be mmap_read_unlock(mm), then mutex_unlock(&svms->lock).  "goto out_unlock_svms" does it in reverse order.

downgrading write lock doesn't change lock order (not in two steps like up_write, down_read), we downgrade mmap write lock in normal path as well, but missing it in this error handling path.

Regards,

Philip

Quoted 7 lines

Regards

Xiaogang

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
  			r = -EAGAIN;
  			goto out_unlock_svms;
  		} else {