Skip to contentSkip to conversation

nouveau/svm: fix + cleanup for nouveau_atomic_range_fault()

PATCH Applied David Hildenbrand 3 participants 7 messages Jan 24, 2025 – Feb 14, 2025

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

cover nouveau/svm: fix + cleanup for nouveau_atomic_range_fault()

One fix and a minor cleanup.

Only compile-tested due to lack of HW, so I'd be happy if someone with access to HW could test. But not sure how easy this is to trigger. Likely some concurrent MADV_DONTNEED on the PTE we just converted might be able to trigger it.

Cc Karol Herbst <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Alistair Popple <[email protected]>

David Hildenbrand (2):
nouveau/svm: fix missing folio unlock + put after
make_device_exclusive_range()
nouveau/svm: don't initialize ret in nouveau_atomic_range_fault()

 drivers/gpu/drm/nouveau/nouveau_svm.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/2 nouveau/svm: fix missing folio unlock + put after make_device_exclusive_range()

In case we have to retry the loop, we are missing to unlock+put the folio. In that case, we will keep failing make_device_exclusive_range() because we cannot grab the folio lock, and even return from the function with the folio locked and referenced, effectively never succeeding the make_device_exclusive_range().

While at it, convert the other unlock+put to use a folio as well.

This was found by code inspection.

Fixes 8f187163eb89 ("nouveau/svm: implement atomic SVM access")
---
 drivers/gpu/drm/nouveau/nouveau_svm.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +7 -2
index b4da82ddbb6b2..8ea98f06d39af 100644
@@ -590,6 +590,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 	unsigned long timeout =
 		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
 	struct mm_struct *mm = svmm->notifier.mm;
+	struct folio *folio;
 	struct page *page;
 	unsigned long start = args->p.addr;
 	unsigned long notifier_seq;
@@ -616,12 +617,16 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 			ret = -EINVAL;
 			goto out;
 		}
+		folio = page_folio(page);
 
 		mutex_lock(&svmm->mutex);
 		if (!mmu_interval_read_retry(&notifier->notifier,
 					     notifier_seq))
 			break;
 		mutex_unlock(&svmm->mutex);
+
+		folio_unlock(folio);
+		folio_put(folio);
 	}
 
 	/* Map the page on the GPU. */
@@ -637,8 +642,8 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 	ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args, size, NULL);
 	mutex_unlock(&svmm->mutex);
 
-	unlock_page(page);
-	put_page(page);
+	folio_unlock(folio);
+	folio_put(folio);
 
 out:
 	mmu_interval_notifier_remove(&notifier->notifier);
Signature · 1 line
2.47.1
#4 lore raw

PATCH 2/2 nouveau/svm: don't initialize ret in nouveau_atomic_range_fault()

In reply to David Hildenbrand

ret will be modified immediately afterwards.

---
 drivers/gpu/drm/nouveau/nouveau_svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +1 -1
index 8ea98f06d39af..2f8b8b978fc08 100644
@@ -594,7 +594,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 	struct page *page;
 	unsigned long start = args->p.addr;
 	unsigned long notifier_seq;
-	int ret = 0;
+	int ret;
 
 	ret = mmu_interval_notifier_insert(&notifier->notifier, mm,
 					args->p.addr, args->p.size,
Signature · 1 line
2.47.1
#6 lore raw
In reply to David Hildenbrand
Quoted 6 lines · David Hildenbrand

One fix and a minor cleanup.

Only compile-tested due to lack of HW, so I'd be happy if someone with access to HW could test. But not sure how easy this is to trigger. Likely some concurrent MADV_DONTNEED on the PTE we just converted might be able to trigger it.

I have this setup so will certainly test it and see if I can trigger the bug as well. Thanks for the fixes.

Quoted 18 lines
Cc Karol Herbst <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Alistair Popple <[email protected]>

David Hildenbrand (2):
nouveau/svm: fix missing folio unlock + put after
make_device_exclusive_range()
nouveau/svm: don't initialize ret in nouveau_atomic_range_fault()

 drivers/gpu/drm/nouveau/nouveau_svm.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.1
Reviewed-by Tested-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] nouveau/svm: fix missing folio unlock + put after make_device_exclusive_range()

In reply to David Hildenbrand
Quoted 9 lines · David Hildenbrand

In case we have to retry the loop, we are missing to unlock+put the folio. In that case, we will keep failing make_device_exclusive_range() because we cannot grab the folio lock, and even return from the function with the folio locked and referenced, effectively never succeeding the make_device_exclusive_range().

While at it, convert the other unlock+put to use a folio as well.

This was found by code inspection.

I couldn't (easily at least) trigger this condition from userspace, probably because the window is pretty tight between mmu_interval_read_begin() and mmu_interval_read_retry(). But I tested it by manually forcing at least one retry and it fixes the issue observed via inspection. There's no reason to think it would be entirely impossible to hit either, and the change looks good so please add:

Quoted 49 lines
Fixes 8f187163eb89 ("nouveau/svm: implement atomic SVM access")
---
 drivers/gpu/drm/nouveau/nouveau_svm.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +7 -2
index b4da82ddbb6b2..8ea98f06d39af 100644
@@ -590,6 +590,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 	unsigned long timeout =
 		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
 	struct mm_struct *mm = svmm->notifier.mm;
+	struct folio *folio;
 	struct page *page;
 	unsigned long start = args->p.addr;
 	unsigned long notifier_seq;
@@ -616,12 +617,16 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 			ret = -EINVAL;
 			goto out;
 		}
+		folio = page_folio(page);
 
 		mutex_lock(&svmm->mutex);
 		if (!mmu_interval_read_retry(&notifier->notifier,
 					     notifier_seq))
 			break;
 		mutex_unlock(&svmm->mutex);
+
+		folio_unlock(folio);
+		folio_put(folio);
 	}
 
 	/* Map the page on the GPU. */
@@ -637,8 +642,8 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 	ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args, size, NULL);
 	mutex_unlock(&svmm->mutex);
 
-	unlock_page(page);
-	put_page(page);
+	folio_unlock(folio);
+	folio_put(folio);
 
 out:
 	mmu_interval_notifier_remove(&notifier->notifier);
Signature · 1 line
2.47.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH v1 2/2] nouveau/svm: don't initialize ret in nouveau_atomic_range_fault()

In reply to David Hildenbrand
Quoted 1 line · David Hildenbrand

ret will be modified immediately afterwards.

Yep. Thanks for fixing.

Quoted 21 lines
---
 drivers/gpu/drm/nouveau/nouveau_svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_svm.c +1 -1
index 8ea98f06d39af..2f8b8b978fc08 100644
@@ -594,7 +594,7 @@ static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 	struct page *page;
 	unsigned long start = args->p.addr;
 	unsigned long notifier_seq;
-	int ret = 0;
+	int ret;
 
 	ret = mmu_interval_notifier_insert(&notifier->notifier, mm,
 					args->p.addr, args->p.size,
Signature · 1 line
2.47.1
maintainer Applied
#3 lore raw

Re: [PATCH v1 1/2] nouveau/svm: fix missing folio unlock + put after make_device_exclusive_range()

In reply to David Hildenbrand
Quoted 6 lines · David Hildenbrand

In case we have to retry the loop, we are missing to unlock+put the folio. In that case, we will keep failing make_device_exclusive_range() because we cannot grab the folio lock, and even return from the function with the folio locked and referenced, effectively never succeeding the make_device_exclusive_range().

Quoted 6 lines

While at it, convert the other unlock+put to use a folio as well.

This was found by code inspection.

Fixes 8f187163eb89 ("nouveau/svm: implement atomic SVM access")

Applied to drm-misc-fixes, thanks!