Skip to contentSkip to conversation

drm/nouveau: Simplify nouveau_cli_work

PATCH Superseded Tvrtko Ursulin 5 participants 17 messages Jun 12 – Jun 22

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

PATCH 1/1 drm/nouveau: Simplify nouveau_cli_work

I don't claim this fixes anything but the code certainly was a bit roundabout and this at least cleans it up.

The only semantic difference is releasing the fence reference after the work callback is called. Which kind of make sense anyway.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
---
 drivers/gpu/drm/nouveau/nouveau_drm.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -17
index 42a81166f3a9..7de174537fc7 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
 		return nouveau_platform_name(to_platform_device(dev->dev));
 }
 
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-	unsigned long flags;
-	bool ret = true;
-
-	dma_fence_lock_irqsave(fence, flags);
-	if (!dma_fence_is_signaled_locked(fence))
-		ret = false;
-	dma_fence_unlock_irqrestore(fence, flags);
-
-	if (ret == true)
-		dma_fence_put(fence);
-	return ret;
-}
-
 static void
 nouveau_cli_work(struct work_struct *w)
 {
@@ -176,9 +160,10 @@ nouveau_cli_work(struct work_struct *w)
 	struct nouveau_cli_work *work, *wtmp;
 	mutex_lock(&cli->lock);
 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		if (!work->fence || dma_fence_is_signaled(work->fence)) {
 			list_del(&work->head);
 			work->func(work);
+			dma_fence_put(work->fence);
 		}
 	}
 	mutex_unlock(&cli->lock);
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] The patch introduces a use-after-free vulnerability by reading `work->fence` after calling…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] The patch introduces a use-after-free vulnerability by reading work->fence after calling work->func(work), which frees the work structure. --

commit 5f500d0384ad49806cf724c1cd7f76a580d6bc42
Author: Tvrtko Ursulin <[email protected]>

drm/nouveau: Simplify nouveau_cli_work

This patch simplifies the nouveau_cli_work function by removing the nouveau_cli_work_ready wrapper. As part of this cleanup, the dma_fence_put() call is moved to execute after the work callback has been invoked.

Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
index e16f59b00f6fa..a3e38a30fbdab 100644

[ ... ]

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 9 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -1
@@ -176,9 +160,10 @@ nouveau_cli_work(struct work_struct *w)
 	struct nouveau_cli_work *work, *wtmp;
 	mutex_lock(&cli->lock);
 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		if (!work->fence || dma_fence_is_signaled(work->fence)) {
 			list_del(&work->head);
 			work->func(work);
+			dma_fence_put(work->fence);

[Severity: Critical]
Does moving dma_fence_put() after work->func() introduce a use-after-free here?

Looking at the callback implementation in nouveau_gem_object_delete_work() (in drivers/gpu/drm/nouveau/nouveau_gem.c), the work structure is freed:

nouveau_gem_object_delete_work(...)

{
	...
	nouveau_gem_object_delete(work->vma);
	kfree(work);
}

If the callback frees the work structure, accessing work->fence immediately afterwards in dma_fence_put() would read from freed memory.

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
 		}
 	}
 	mutex_unlock(&cli->lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Tvrtko Ursulin
Quoted 43 lines · Tvrtko Ursulin

I don't claim this fixes anything but the code certainly was a bit roundabout and this at least cleans it up.

The only semantic difference is releasing the fence reference after the work callback is called. Which kind of make sense anyway.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>

---
 drivers/gpu/drm/nouveau/nouveau_drm.c | 19 ++-----------------

 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
index 42a81166f3a9..7de174537fc7 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
  		return nouveau_platform_name(to_platform_device(dev->dev));
  }

drivers/gpu/drm/nouveau/nouveau_drm.c +0 -16
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-	unsigned long flags;
-	bool ret = true;
-
-	dma_fence_lock_irqsave(fence, flags);
-	if (!dma_fence_is_signaled_locked(fence))
-		ret = false;
-	dma_fence_unlock_irqrestore(fence, flags);
-
-	if (ret == true)
-		dma_fence_put(fence);
-	return ret;
-}
-

 static void
 nouveau_cli_work(struct work_struct *w)
 {

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -1
@@ -176,9 +160,10 @@ nouveau_cli_work(struct work_struct *w)
  	struct nouveau_cli_work *work, *wtmp;
  	mutex_lock(&cli->lock);
  	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		if (!work->fence || dma_fence_is_signaled(work->fence)) {
  			list_del(&work->head);
  			work->func(work);
+			dma_fence_put(work->fence);

Dave had addressed a UAF with that patch.

So if this would fix it, then the claim would be that work->func(work) was the user of that fence, and not some other party elsewhere.

I don't know whether that's the case.

P.

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
  		}
  	}
  	mutex_unlock(&cli->lock);
#3 lore raw
Quoted 55 lines · Philipp Stanner
Quoted 43 lines · Tvrtko Ursulin

I don't claim this fixes anything but the code certainly was a bit roundabout and this at least cleans it up.

The only semantic difference is releasing the fence reference after the work callback is called. Which kind of make sense anyway.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
---
  drivers/gpu/drm/nouveau/nouveau_drm.c | 19 ++-----------------
  1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -16
index 42a81166f3a9..7de174537fc7 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
  		return nouveau_platform_name(to_platform_device(dev->dev));
  }
 
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-	unsigned long flags;
-	bool ret = true;
-
-	dma_fence_lock_irqsave(fence, flags);
-	if (!dma_fence_is_signaled_locked(fence))
-		ret = false;
-	dma_fence_unlock_irqrestore(fence, flags);
-
-	if (ret == true)
-		dma_fence_put(fence);
-	return ret;
-}
-
  static void
  nouveau_cli_work(struct work_struct *w)
  {

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -1
@@ -176,9 +160,10 @@ nouveau_cli_work(struct work_struct *w)
  	struct nouveau_cli_work *work, *wtmp;
  	mutex_lock(&cli->lock);
  	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		if (!work->fence || dma_fence_is_signaled(work->fence)) {
  			list_del(&work->head);
  			work->func(work);
+			dma_fence_put(work->fence);

Dave had addressed a UAF with that patch.

Quoted 4 lines

So if this would fix it, then the claim would be that work->func(work) was the user of that fence, and not some other party elsewhere.

I don't know whether that's the case.

Me neither. I say in the commit I don't claim to fix anything. But it is more logical and avoids the silly games with ret and all.

What I missed though is that work->func(work) frees the work.

So patch needs to cache the fence... v2 incoming..

Regards,

Tvrtko

Quoted 7 lines

P.

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
  		}
  	}
  	mutex_unlock(&cli->lock);
#4 lore raw
Quoted 59 lines · Tvrtko Ursulin
Quoted 57 lines · Philipp Stanner
Quoted 42 lines · Tvrtko Ursulin

I don't claim this fixes anything but the code certainly was a bit roundabout and this at least cleans it up.

The only semantic difference is releasing the fence reference after the work callback is called. Which kind of make sense anyway.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>

---
  drivers/gpu/drm/nouveau/nouveau_drm.c | 19 ++-----------------

  1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -16
index 42a81166f3a9..7de174537fc7 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
           return nouveau_platform_name(to_platform_device(dev->dev));
   }
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-    unsigned long flags;
-    bool ret = true;
-
-    dma_fence_lock_irqsave(fence, flags);
-    if (!dma_fence_is_signaled_locked(fence))
-        ret = false;
-    dma_fence_unlock_irqrestore(fence, flags);
-
-    if (ret == true)
-        dma_fence_put(fence);
-    return ret;
-}
-

  static void
  nouveau_cli_work(struct work_struct *w)
  {

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -1
@@ -176,9 +160,10 @@ nouveau_cli_work(struct work_struct *w)
       struct nouveau_cli_work *work, *wtmp;
       mutex_lock(&cli->lock);
       list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-        if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+        if (!work->fence || dma_fence_is_signaled(work->fence)) {
               list_del(&work->head);
               work->func(work);
+            dma_fence_put(work->fence);

Dave had addressed a UAF with that patch.

So if this would fix it, then the claim would be that work->func(work) was the user of that fence, and not some other party elsewhere.

Quoted 7 lines
Quoted 1 line

I don't know whether that's the case.

Me neither. I say in the commit I don't claim to fix anything. But it is more logical and avoids the silly games with ret and all.

What I missed though is that work->func(work) frees the work.

So patch needs to cache the fence... v2 incoming..

Ah sorry and I forgot the lock cycling hack we discussed.. ie. the whole point of the race. Nevermind then, confusing.

Regards,

Tvrtko

#5 lore raw

PATCH v2 1/1 drm/nouveau: Simplify nouveau_cli_work

In reply to Tvrtko Ursulin

I don't claim this fixes anything but the code certainly was a bit roundabout and this at least cleans it up.

The only semantic difference is releasing the fence reference after the work callback is called. Which kind of make sense anyway.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>

---
v2:
* Don't dereference work after work->func(work) frees it.
---
Not even compile tested!

---
 drivers/gpu/drm/nouveau/nouveau_drm.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +4 -17
index 42a81166f3a9..e81ec261689a 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
 		return nouveau_platform_name(to_platform_device(dev->dev));
 }
 
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-	unsigned long flags;
-	bool ret = true;
-
-	dma_fence_lock_irqsave(fence, flags);
-	if (!dma_fence_is_signaled_locked(fence))
-		ret = false;
-	dma_fence_unlock_irqrestore(fence, flags);
-
-	if (ret == true)
-		dma_fence_put(fence);
-	return ret;
-}
-
 static void
 nouveau_cli_work(struct work_struct *w)
 {
@@ -176,9 +160,12 @@ nouveau_cli_work(struct work_struct *w)
 	struct nouveau_cli_work *work, *wtmp;
 	mutex_lock(&cli->lock);
 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
 			list_del(&work->head);
 			work->func(work);
+			dma_fence_put(fence);
 		}
 	}
 	mutex_unlock(&cli->lock);
Signature · 1 line
2.54.0
#6 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Replacing the locked fence check (`nouveau_cli_work_ready`) with the lockless…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Replacing the locked fence check (nouveau_cli_work_ready) with the lockless dma_fence_is_signaled() removes critical synchronization with dma_fence_signal(), introducing a race condition that leads to a Use-After-Free of struct nouveau_cli_work. --

commit f14e589461298a55cf7c0a8d50dd778a913dd4fb
Author: Tvrtko Ursulin <[email protected]>

drm/nouveau: Simplify nouveau_cli_work

This commit simplifies nouveau_cli_work by removing the nouveau_cli_work_ready helper and replacing it with a direct call to dma_fence_is_signaled. It also changes the fence reference release to happen after the work callback is invoked.

Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
index e16f59b00f6fa..a6432eede6137 100644

[ ... ]

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 165

Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +3 -1
@@ -176,9 +160,12 @@ nouveau_cli_work(struct work_struct *w)
 	struct nouveau_cli_work *work, *wtmp;
 	mutex_lock(&cli->lock);
 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {

[Severity: Critical]
Can this lockless check introduce a race condition leading to a use-after-free of the work structure?

The original nouveau_cli_work_ready() acquired the fence spinlock before checking if the fence was signaled, synchronizing with dma_fence_signal().

By using the lockless dma_fence_is_signaled(), this code can observe the signaled bit while dma_fence_signal() is still iterating over its local callback list.

If work->func(work) then deallocates the work structure (for instance, via kfree in nouveau_gem_object_delete_work()), could the signaling CPU subsequently trigger a use-after-free when it invokes the callback (nouveau_cli_work_fence)?

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +1 -0
 			list_del(&work->head);
 			work->func(work);
+			dma_fence_put(fence);
 		}
 	}
 	mutex_unlock(&cli->lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw

PATCH v3 1/1 drm/nouveau: Simplify nouveau_cli_work

In reply to Tvrtko Ursulin

Lets simplify and make clearer the fence lock cycling workaround in nouveau_cli_work().

The reason for the workaround is that the worker processes the list of pending work items (and so fence callbacks) opportunisticaly, while dma_fence_is_signaled() returns true as soon as a the fence is signaled. It is therefore not allowed for the opportunistic processing to free the work item, since the fence callback processing can still be in the process of dereferencing work->cb.

This wasn't very clear in the code so clarify it with a nice comment.

In the process we replace the somewhat interleaved and not very readable nouveau_cli_work_ready() helper with a direct dma_fence_is_signaled, which is even faster since it removes the lock cycling from the unsignaled fence path.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>

---
v2:
* Don't dereference work after work->func(work) frees it.

v3:
* Put back and explain lock cycling.

---
 drivers/gpu/drm/nouveau/nouveau_drm.c | 34 +++++++++++++--------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +17 -17
index 42a81166f3a9..7ea95ab960a0 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
 		return nouveau_platform_name(to_platform_device(dev->dev));
 }
 
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-	unsigned long flags;
-	bool ret = true;
-
-	dma_fence_lock_irqsave(fence, flags);
-	if (!dma_fence_is_signaled_locked(fence))
-		ret = false;
-	dma_fence_unlock_irqrestore(fence, flags);
-
-	if (ret == true)
-		dma_fence_put(fence);
-	return ret;
-}
-
 static void
 nouveau_cli_work(struct work_struct *w)
 {
@@ -176,9 +160,25 @@ nouveau_cli_work(struct work_struct *w)
 	struct nouveau_cli_work *work, *wtmp;
 	mutex_lock(&cli->lock);
 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
+			if (fence) {
+				unsigned long flags;
+
+				/*
+				 * Because fence can still be in the process of
+				 * processing the callback list, and the
+				 * callback references the work we are about to
+				 * free, we need to sync with the callback
+				 * processing before freeing the work.
+				 */
+				dma_fence_lock_irqsave(fence, flags);
+				dma_fence_unlock_irqrestore(fence, flags);
+			}
 			list_del(&work->head);
 			work->func(work);
+			dma_fence_put(fence);
 		}
 	}
 	mutex_unlock(&cli->lock);
Signature · 1 line
2.54.0
#8 lore raw
Quoted 73 lines · Tvrtko Ursulin

Lets simplify and make clearer the fence lock cycling workaround in nouveau_cli_work().

The reason for the workaround is that the worker processes the list of pending work items (and so fence callbacks) opportunisticaly, while dma_fence_is_signaled() returns true as soon as a the fence is signaled. It is therefore not allowed for the opportunistic processing to free the work item, since the fence callback processing can still be in the process of dereferencing work->cb.

This wasn't very clear in the code so clarify it with a nice comment.

In the process we replace the somewhat interleaved and not very readable nouveau_cli_work_ready() helper with a direct dma_fence_is_signaled, which is even faster since it removes the lock cycling from the unsignaled fence path.

Cc Philipp Stanner <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>

---
v2:
 * Don't dereference work after work->func(work) frees it.

v3:
 * Put back and explain lock cycling.
---
 drivers/gpu/drm/nouveau/nouveau_drm.c | 34 +++++++++++++--------------

 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
index 42a81166f3a9..7ea95ab960a0 100644
@@ -153,22 +153,6 @@ nouveau_name(struct drm_device *dev)
  		return nouveau_platform_name(to_platform_device(dev->dev));
  }

drivers/gpu/drm/nouveau/nouveau_drm.c +0 -16
-static inline bool
-nouveau_cli_work_ready(struct dma_fence *fence)
-{
-	unsigned long flags;
-	bool ret = true;
-
-	dma_fence_lock_irqsave(fence, flags);
-	if (!dma_fence_is_signaled_locked(fence))
-		ret = false;
-	dma_fence_unlock_irqrestore(fence, flags);
-
-	if (ret == true)
-		dma_fence_put(fence);
-	return ret;
-}
-

 static void
 nouveau_cli_work(struct work_struct *w)
 {

drivers/gpu/drm/nouveau/nouveau_drm.c +9 -1
@@ -176,9 +160,25 @@ nouveau_cli_work(struct work_struct *w)
  	struct nouveau_cli_work *work, *wtmp;
  	mutex_lock(&cli->lock);
  	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
+			if (fence) {
+				unsigned long flags;
+
+				/*
+				 * Because fence can still be in the process of
+				 * processing the callback list, and the

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 181

Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +8 -0
+				 * callback references the work we are about to
+				 * free, we need to sync with the callback
+				 * processing before freeing the work.
+				 */
+				dma_fence_lock_irqsave(fence, flags);
+				dma_fence_unlock_irqrestore(fence, flags);
+			}
  			list_del(&work->head);
  			work->func(work);
+			dma_fence_put(fence);

This is semantically exactly the same fix that was introduced by Dave in c8a5d5ea3ba6a and which you are reverting here again, basically.

It's no improvement IMO, just a differently looking work around the fact that the dma_fence framework is not properly synchronized.

I suggest you participate here [1] and here [2] and share your opinion. At least *this* problem would disappear of only dma_fence_is_signaled() were locking properly.

P.

[1] https://lore.kernel.org/dri-devel/[email protected]/
[2] https://lore.kernel.org/dri-devel/[email protected]/

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0 -0
  		}
  	}
  	mutex_unlock(&cli->lock);
maintainer Applied
#9 lore raw
In reply to Tvrtko Ursulin
Quoted 1 line · Tvrtko Ursulin

[PATCH v3] drm/nouveau: Simplify nouveau_cli_work

Applied, thanks!

Branch: drm-misc-next
Tree: https://gitlab.freedesktop.org/drm/misc/kernel.git

[1/1] drm/nouveau: Simplify nouveau_cli_work

      commit: a80d12eafad1

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.

maintainer
#10 lore raw
In reply to Tvrtko Ursulin

(Cc: Alice, Boris, Christian, Dave, Gary)

Quoted 10 lines · Tvrtko Ursulin
drivers/gpu/drm/nouveau/nouveau_drm.c +5 -1
@@ -176,9 +160,25 @@ nouveau_cli_work(struct work_struct *w)
 	struct nouveau_cli_work *work, *wtmp;
 	mutex_lock(&cli->lock);
 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
+			if (fence) {
+				unsigned long flags;

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 177

Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +10 -0
+
+				/*
+				 * Because fence can still be in the process of
+				 * processing the callback list, and the
+				 * callback references the work we are about to
+				 * free, we need to sync with the callback
+				 * processing before freeing the work.
+				 */
+				dma_fence_lock_irqsave(fence, flags);
+				dma_fence_unlock_irqrestore(fence, flags);

This does not ensure that there are no more dma_fence_ops callbacks into the driver. For instance, a concurrent dma_fence_is_signaled() might read the signaled flag as false before we do this lock dance and then still enter ops->signaled().

Luckily, for this specific code here in nouveau it should be enough to ensure that all dma_fence_cb callbacks are finished, which this hack does achieve.

The reason I call it hack is that it fundamentally relies on dma_fence implementation details and not a specified API contract.

The closest thing to an API contract for this is the RCU protection that was recently added. So, the proper approach would be to run this work with queue_rcu_work() and then call flush_rcu_work().

In this context it is worth noting that in this case it is even more complicated. Since the work struct is shared per CLI the single RCU barrier in flush_rcu_work() is not enough, i.e. there is no per fence callback grace period guarantee.

To me this still seems to be a bit of a footgun (although we could probably improve the nouveau code to make it a bit simpler).

That said, the patch does make things a bit clearer, so I picked it up -- thanks!

Thanks,
Danilo

Quoted 9 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -0
+			}
 			list_del(&work->head);
 			work->func(work);
+			dma_fence_put(fence);
 		}
 	}
 	mutex_unlock(&cli->lock);
Signature · 1 line
2.54.0
#11 lore raw
Quoted 12 lines · Danilo Krummrich

(Cc: Alice, Boris, Christian, Dave, Gary)

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 9 lines · Tvrtko Ursulin
drivers/gpu/drm/nouveau/nouveau_drm.c +4 -1
@@ -176,9 +160,25 @@ nouveau_cli_work(struct work_struct *w)
  	struct nouveau_cli_work *work, *wtmp;
  	mutex_lock(&cli->lock);
  	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
+			if (fence) {

btw I don't think that these two lines read more beautifully than the removed nouveau_cli_work_ready().

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +8 -0
+				unsigned long flags;
+
+				/*
+				 * Because fence can still be in the process of
+				 * processing the callback list, and the
+				 * callback references the work we are about to
+				 * free, we need to sync with the callback
+				 * processing before freeing the work.
Quoted 8 lines

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 177

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +3 -0
+				 */
+				dma_fence_lock_irqsave(fence, flags);
+				dma_fence_unlock_irqrestore(fence, flags);

This does not ensure that there are no more dma_fence_ops callbacks into the driver. For instance, a concurrent dma_fence_is_signaled() might read the signaled flag as false before we do this lock dance and then still enter ops->signaled().

The issue Dave addressed was a UAF within Nouveau itself, where the driver put() its own fence while its own callbacks were still executing.

Tvrtko's patch does not address the RCU issues you mention, nor does it address any other fence related issue except for adding the comment that should have been added back then with the fix in nouveau_cli_work_ready().

It's literally the same. Take the lock manually to work around the fact that the bit is protected inconsistently, setting is locked, reading just randomly races around.

Quoted 16 lines

Luckily, for this specific code here in nouveau it should be enough to ensure that all dma_fence_cb callbacks are finished, which this hack does achieve.

The reason I call it hack is that it fundamentally relies on dma_fence implementation details and not a specified API contract.

The closest thing to an API contract for this is the RCU protection that was recently added. So, the proper approach would be to run this work with queue_rcu_work() and then call flush_rcu_work().

In this context it is worth noting that in this case it is even more complicated. Since the work struct is shared per CLI the single RCU barrier in flush_rcu_work() is not enough, i.e. there is no per fence callback grace period guarantee.

Quoted 5 lines

To me this still seems to be a bit of a footgun (although we could probably improve the nouveau code to make it a bit simpler).

That said, the patch does make things a bit clearer, so I picked it up -- thanks!

I disagree that this is an improvement and am unhappy with you merging it. It merely moves Dave's fix from one place to another and causes unnecessary churn in the git log.

Adding the comment into Dave's fix until someone can address the (literally) fundemantal problem in dma_fence would have been my preferred approach.

P.

#12 lore raw
Quoted 38 lines · Philipp Stanner
Quoted 12 lines · Danilo Krummrich

(Cc: Alice, Boris, Christian, Dave, Gary)

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 9 lines · Tvrtko Ursulin
drivers/gpu/drm/nouveau/nouveau_drm.c +4 -1
@@ -176,9 +160,25 @@ nouveau_cli_work(struct work_struct *w)
  	struct nouveau_cli_work *work, *wtmp;
  	mutex_lock(&cli->lock);
  	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
+			if (fence) {

btw I don't think that these two lines read more beautifully than the removed nouveau_cli_work_ready().

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +8 -0
+				unsigned long flags;
+
+				/*
+				 * Because fence can still be in the process of
+				 * processing the callback list, and the
+				 * callback references the work we are about to
+				 * free, we need to sync with the callback
+				 * processing before freeing the work.
Quoted 8 lines

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 177

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +3 -0
+				 */
+				dma_fence_lock_irqsave(fence, flags);
+				dma_fence_unlock_irqrestore(fence, flags);

This does not ensure that there are no more dma_fence_ops callbacks into the driver. For instance, a concurrent dma_fence_is_signaled() might read the signaled flag as false before we do this lock dance and then still enter ops->signaled().

The issue Dave addressed was a UAF within Nouveau itself, where the driver put() its own fence while its own callbacks were still executing.

Quoted 8 lines

Tvrtko's patch does not address the RCU issues you mention, nor does it address any other fence related issue except for adding the comment that should have been added back then with the fix in nouveau_cli_work_ready().

It's literally the same. Take the lock manually to work around the fact that the bit is protected inconsistently, setting is locked, reading just randomly races around.

FWIW for me the sandwiched sampling of the signaled status inside a lock was quite misleading, when the real problem is nouveau freeing a fence callback underneath the dma-fence APIs feet. Hence I think clearly splitting the steps is clearer.

Alternative could be having a dma_fence_remove_callback() variant which is safe to call for the nouveau use case. Then the sequence could be:

if (dma_fence_is_signaled(work->fence)) {
	dma_fence_remove_callback_safe(work->fence, work->cb);
	kfree(work);
}

(Signatures invented purely for the purpose of illustrating the logic flow and freeing the callback gotcha.)

And as an extra bonus the patch removed the pointless lock cycling of all the unsignaled fences on the list.

So in my view discussion about the semantics of the signaled status when observed from the outside is orthogonal. Ie. whether seeing a signaled fence status means fence has been signaled (what we have now), or whether also all listeners have been notified (what you argue for).

For the latter one could argue that it is not required to "block" all listeners until all have been notified. Let the first one go and carry one notifying the rest kind of makes sense.

Regards,

Tvrtko

Quoted 32 lines
Quoted 16 lines

Luckily, for this specific code here in nouveau it should be enough to ensure that all dma_fence_cb callbacks are finished, which this hack does achieve.

The reason I call it hack is that it fundamentally relies on dma_fence implementation details and not a specified API contract.

The closest thing to an API contract for this is the RCU protection that was recently added. So, the proper approach would be to run this work with queue_rcu_work() and then call flush_rcu_work().

In this context it is worth noting that in this case it is even more complicated. Since the work struct is shared per CLI the single RCU barrier in flush_rcu_work() is not enough, i.e. there is no per fence callback grace period guarantee.

Quoted 5 lines

To me this still seems to be a bit of a footgun (although we could probably improve the nouveau code to make it a bit simpler).

That said, the patch does make things a bit clearer, so I picked it up -- thanks!

I disagree that this is an improvement and am unhappy with you merging it. It merely moves Dave's fix from one place to another and causes unnecessary churn in the git log.

Adding the comment into Dave's fix until someone can address the (literally) fundemantal problem in dma_fence would have been my preferred approach.

P.

#13 lore raw
In reply to Philipp Stanner
Quoted 34 lines · Philipp Stanner
Quoted 12 lines · Danilo Krummrich

(Cc: Alice, Boris, Christian, Dave, Gary)

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 166

Quoted 9 lines · Tvrtko Ursulin
drivers/gpu/drm/nouveau/nouveau_drm.c +4 -1
@@ -176,9 +160,25 @@ nouveau_cli_work(struct work_struct *w)
  	struct nouveau_cli_work *work, *wtmp;
  	mutex_lock(&cli->lock);
  	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
-		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
+		struct dma_fence *fence = work->fence;
+
+		if (!fence || dma_fence_is_signaled(fence)) {
+			if (fence) {

btw I don't think that these two lines read more beautifully than the removed nouveau_cli_work_ready().

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +8 -0
+				unsigned long flags;
+
+				/*
+				 * Because fence can still be in the process of
+				 * processing the callback list, and the
+				 * callback references the work we are about to
+				 * free, we need to sync with the callback
+				 * processing before freeing the work.
Quoted 8 lines

drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_cli_work(struct work_struct *w) · line 177

Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +3 -0
+				 */
+				dma_fence_lock_irqsave(fence, flags);
+				dma_fence_unlock_irqrestore(fence, flags);

This does not ensure that there are no more dma_fence_ops callbacks into the driver. For instance, a concurrent dma_fence_is_signaled() might read the signaled flag as false before we do this lock dance and then still enter ops->signaled().

Quoted 3 lines

The issue Dave addressed was a UAF within Nouveau itself, where the driver put() its own fence while its own callbacks were still executing.

Yeah that's exactly the reason why I have been rejecting the idea that drivers install callbacks on their own fences.

Maybe an idea how to generally tackle this for all drivers: Never let the dma-fence framework call dma_fence_signal(), that should only the driver do by itself.

This way we don't run into the issue in the first place that the driver needs to install some callbacks *and* the driver also knows when the installed callbacks are finished because that is when dma_fence_signal() returns.

Does that sound valid to you guys? If yes I will be sending out patches to do this.

Regards,
Christian.

Quoted 42 lines

Tvrtko's patch does not address the RCU issues you mention, nor does it address any other fence related issue except for adding the comment that should have been added back then with the fix in nouveau_cli_work_ready().

It's literally the same. Take the lock manually to work around the fact that the bit is protected inconsistently, setting is locked, reading just randomly races around.

Quoted 16 lines

Luckily, for this specific code here in nouveau it should be enough to ensure that all dma_fence_cb callbacks are finished, which this hack does achieve.

The reason I call it hack is that it fundamentally relies on dma_fence implementation details and not a specified API contract.

The closest thing to an API contract for this is the RCU protection that was recently added. So, the proper approach would be to run this work with queue_rcu_work() and then call flush_rcu_work().

In this context it is worth noting that in this case it is even more complicated. Since the work struct is shared per CLI the single RCU barrier in flush_rcu_work() is not enough, i.e. there is no per fence callback grace period guarantee.

Quoted 5 lines

To me this still seems to be a bit of a footgun (although we could probably improve the nouveau code to make it a bit simpler).

That said, the patch does make things a bit clearer, so I picked it up -- thanks!

I disagree that this is an improvement and am unhappy with you merging it. It merely moves Dave's fix from one place to another and causes unnecessary churn in the git log.

Adding the comment into Dave's fix until someone can address the (literally) fundemantal problem in dma_fence would have been my preferred approach.

P.

#14 lore raw

(+Cc Daniel)

Quoted 8 lines · Christian König
Quoted 4 lines · Philipp Stanner

The issue Dave addressed was a UAF within Nouveau itself, where the driver put() its own fence while its own callbacks were still executing.

Yeah that's exactly the reason why I have been rejecting the idea that drivers install callbacks on their own fences.

I remember we had that discussion when I discovered that signaled fences can temporarily remain on nouveau's "unsignaled fences" list and wanted to do the removal automatically via callback.

Quoted 4 lines

Maybe an idea how to generally tackle this for all drivers: Never let the dma-fence framework call dma_fence_signal(), that should only the driver do by itself.

About the framework I'm not sure, but what we do enforce strictly in Rust is that only the creator of a fence can signal it. We achieve that by two fence types, of which only one implements a signal() method

I remember seeing some parties signaling the GPU scheduler's fences, so such things definitely happen. And since you likely can't enforce it in C, you would still have to take the possibility of a third party signalling your fence into account.

Quoted 8 lines

This way we don't run into the issue in the first place that the driver needs to install some callbacks *and* the driver also knows when the installed callbacks are finished because that is when dma_fence_signal() returns.

Does that sound valid to you guys? If yes I will be sending out patches to do this.

Hm. I suppose that would be an improvement?

Maybe let me ask differently, too: why is it that the framework is signaling fences in the first place? If I understood you correctly last time that is partially related to the locking problem in the callbacks.

P.

Quoted 3 lines

Regards,
Christian.

maintainer
#15 lore raw
In reply to Christian König
Quoted 10 lines · Christian König

Maybe an idea how to generally tackle this for all drivers: Never let the dma-fence framework call dma_fence_signal(), that should only the driver do by itself.

This way we don't run into the issue in the first place that the driver needs to install some callbacks *and* the driver also knows when the installed callbacks are finished because that is when dma_fence_signal() returns.

Does that sound valid to you guys? If yes I will be sending out patches to do this.

I think that's a good idea, as it would get us rid of having to rely on API internals to synchronize struct dma_fence_cb callbacks without doing the full RCU dance, which can be quite cumbersome (as described in the nouveau case).

It still leaves us with the full RCU dance for struct dma_fence_ops callbacks, but that's a different topic.

Thanks,
Danilo

maintainer
#16 lore raw
In reply to Philipp Stanner
Quoted 3 lines · Philipp Stanner

Tvrtko's patch does not address the RCU issues you mention, nor does it address any other fence related issue except for adding the comment that should have been added back then with the fix in nouveau_cli_work_ready().

I described exactly that, with the goal to discuss a proper solution rather than arguing about which hack is the better one.