Skip to contentSkip to conversation

nouveau/fence: handle cross device fences properly.

PATCH In review Dave Airlie 3 participants 6 messages Jan 7, 2025 – Jan 8, 2025

Subsystems (3)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

PATCH 1/1 nouveau/fence: handle cross device fences properly.

From: Dave Airlie <[email protected]>

If we have two nouveau controlled devices and one passes a dma-fence to the other, when we hit the sync path it can cause the second device to try and put a sync wait in it's pushbuf for the seqno of the context on the first device.

Since fence contexts are vmm bound, check the if vmm's match between both users, this should ensure that fence seqnos don't get used wrongly on incorrect channels.

This seems to happen fairly spuriously and I found it tracking down a multi-card regression report, that seems to work by luck before this.

---
 drivers/gpu/drm/nouveau/nouveau_fence.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +2 -1
index ee5e9d40c166f..5743c82f4094b 100644
@@ -370,7 +370,8 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
 
 				rcu_read_lock();
 				prev = rcu_dereference(f->channel);
-				if (prev && (prev == chan ||
+				if (prev && (prev->vmm == chan->vmm) &&
+				    (prev == chan ||
 					     fctx->sync(f, prev, chan) == 0))
 					must_wait = false;
 				rcu_read_unlock();
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Dave Airlie

From: Dave Airlie <[email protected]>

If we have two nouveau controlled devices and one passes a dma-fence to the other, when we hit the sync path it can cause the second device to try and put a sync wait in it's pushbuf for the seqno of the context on the first device.

Since fence contexts are vmm bound, check the if vmm's match between both users, this should ensure that fence seqnos don't get used wrongly on incorrect channels.

Quoted 5 lines

This seems to happen fairly spuriously and I found it tracking down a multi-card regression report, that seems to work by luck before this.

Quoted 18 lines
---
  drivers/gpu/drm/nouveau/nouveau_fence.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +2 -1
index ee5e9d40c166f..5743c82f4094b 100644
@@ -370,7 +370,8 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
 
  				rcu_read_lock();
  				prev = rcu_dereference(f->channel);
-				if (prev && (prev == chan ||
+				if (prev && (prev->vmm == chan->vmm) &&
+				    (prev == chan ||
  					     fctx->sync(f, prev, chan) == 0))
  					must_wait = false;
  				rcu_read_unlock();
maintainer
#2 lore raw
In reply to Dave Airlie
Quoted 10 lines · Dave Airlie

From: Dave Airlie <[email protected]>

If we have two nouveau controlled devices and one passes a dma-fence to the other, when we hit the sync path it can cause the second device to try and put a sync wait in it's pushbuf for the seqno of the context on the first device.

Since fence contexts are vmm bound, check the if vmm's match between both users, this should ensure that fence seqnos don't get used wrongly on incorrect channels.

The fence sequence number is global, i.e. per device, hence checking the vmm context seems too restrictive.

Wouldn't it be better to ensure that prev->cli->drm == chan->cli->drm?

This way we can still optimize where dependencies are between different applications, but on the same device.

Quoted 15 lines

This seems to happen fairly spuriously and I found it tracking down a multi-card regression report, that seems to work by luck before this.

---
 drivers/gpu/drm/nouveau/nouveau_fence.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +0 -0
index ee5e9d40c166f..5743c82f4094b 100644

drivers/gpu/drm/nouveau/nouveau_fence.c nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, · line 374

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_fence.c +2 -1
@@ -370,7 +370,8 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
 
 				rcu_read_lock();
 				prev = rcu_dereference(f->channel);
-				if (prev && (prev == chan ||
+				if (prev && (prev->vmm == chan->vmm) &&
+				    (prev == chan ||

Maybe better break it down a bit, e.g.

bool local = prev && (prev->... == chan->...);

if (local && ...) {
...
}

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_fence.c +0 -0
 					     fctx->sync(f, prev, chan) == 0))
 					must_wait = false;
 				rcu_read_unlock();
Signature · 1 line
2.43.0
author maintainer
#3 lore raw
Quoted 9 lines · Danilo Krummrich
Quoted 7 lines · Dave Airlie

From: Dave Airlie <[email protected]>

If we have two nouveau controlled devices and one passes a dma-fence to the other, when we hit the sync path it can cause the second device to try and put a sync wait in it's pushbuf for the seqno of the context on the first device.

Quoted 8 lines
Quoted 3 lines

Since fence contexts are vmm bound, check the if vmm's match between both users, this should ensure that fence seqnos don't get used wrongly on incorrect channels.

The fence sequence number is global, i.e. per device, hence checking the vmm context seems too restrictive.

Wouldn't it be better to ensure that prev->cli->drm == chan->cli->drm?

Can you prove that? I thought the same and I've gone around a few times yesterday/today and convinced myself what I wrote is right.

dma_fence_init gets passed the seqno which comes from fctx->sequence, which is nouveau_fence_chan, which gets allocated for each channel.

So we should hit this path if we have 2 userspace submits, one with say graphics, the one with copy engine contexts, otherwise we should wait on the CPU.

Quoted 17 lines
Quoted 8 lines
 drivers/gpu/drm/nouveau/nouveau_fence.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +0 -0
index ee5e9d40c166f..5743c82f4094b 100644

drivers/gpu/drm/nouveau/nouveau_fence.c nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, · line 374

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_fence.c +2 -1
@@ -370,7 +370,8 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
 
                              rcu_read_lock();
                              prev = rcu_dereference(f->channel);
-                             if (prev && (prev == chan ||
+                             if (prev && (prev->vmm == chan->vmm) &&
+                                 (prev == chan ||

Maybe better break it down a bit, e.g.

Quoted 5 lines

bool local = prev && (prev->... == chan->...);

if (local && ...) {
...
}

I'll update that once we resolve the above.

Dave.

#4 lore raw
Quoted 12 lines · Dave Airlie
Quoted 11 lines · Danilo Krummrich
Quoted 10 lines · Dave Airlie

From: Dave Airlie <[email protected]>

If we have two nouveau controlled devices and one passes a dma-fence to the other, when we hit the sync path it can cause the second device to try and put a sync wait in it's pushbuf for the seqno of the context on the first device.

Since fence contexts are vmm bound, check the if vmm's match between both users, this should ensure that fence seqnos don't get used wrongly on incorrect channels.

Quoted 6 lines
Quoted 4 lines

The fence sequence number is global, i.e. per device, hence checking the vmm context seems too restrictive.

Wouldn't it be better to ensure that prev->cli->drm == chan->cli->drm?

Can you prove that? I thought the same and I've gone around a few times yesterday/today and convinced myself what I wrote is right.

I think Danilo is right.  Using the VMM would prevent synchronisation between clients on the same device, which was one of the intended purposes.

Quoted 3 lines

dma_fence_init gets passed the seqno which comes from fctx->sequence, which is nouveau_fence_chan, which gets allocated for each channel.

All this code is really old and horrible, especially after not receiving much attention through many many DRM changes over the years.  But - all channels share the semaphore buffer, each with their own (fixed, based on channel id) offset.  There are indeed per-channel GPU VA mappings of the buffer in the fctx, but they all point at the same underlying memory.

The "new" exec submission path doesn't use nouveau_fence_sync() at all.  This isn't the worst idea in the world, given various shortcomings in how it's currently implemented, but I've never felt confident *something* wouldn't regress by removing its use in the older paths (or buffer moves).

Quoted 29 lines

So we should hit this path if we have 2 userspace submits, one with say graphics, the one with copy engine contexts, otherwise we should wait on the CPU.

Quoted 16 lines
Quoted 8 lines
  drivers/gpu/drm/nouveau/nouveau_fence.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +0 -0
index ee5e9d40c166f..5743c82f4094b 100644

drivers/gpu/drm/nouveau/nouveau_fence.c nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, · line 374

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_fence.c +2 -1
@@ -370,7 +370,8 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
 
                               rcu_read_lock();
                               prev = rcu_dereference(f->channel);
-                             if (prev && (prev == chan ||
+                             if (prev && (prev->vmm == chan->vmm) &&
+                                 (prev == chan ||

Maybe better break it down a bit, e.g.

Quoted 5 lines

bool local = prev && (prev->... == chan->...);

if (local && ...) {
...
}

I'll update that once we resolve the above.

Dave.

maintainer
#5 lore raw
In reply to Dave Airlie
Quoted 14 lines · Dave Airlie
Quoted 13 lines · Danilo Krummrich
Quoted 10 lines · Dave Airlie

From: Dave Airlie <[email protected]>

If we have two nouveau controlled devices and one passes a dma-fence to the other, when we hit the sync path it can cause the second device to try and put a sync wait in it's pushbuf for the seqno of the context on the first device.

Since fence contexts are vmm bound, check the if vmm's match between both users, this should ensure that fence seqnos don't get used wrongly on incorrect channels.

Quoted 7 lines
Quoted 4 lines

The fence sequence number is global, i.e. per device, hence checking the vmm context seems too restrictive.

Wouldn't it be better to ensure that prev->cli->drm == chan->cli->drm?

Can you prove that? I thought the same and I've gone around a few times yesterday/today and convinced myself what I wrote is right.

Honestly, I thought you were implying that by the commit summary and message, but that's more the how you found this.

With that bias grep made me end up at pre-nv84 code, where this is actually still the case (see nv17_fence_sync()).

But of course for later GPUs it's a per fence-context / channel seqno; can't know what the firmware scheduler puts first.

I think we should change the commit message to "handle cross cli fences properly" (channels of the same cli share the cli's vmm) and clarify in the commit message that not only cross device cases are affected.

I'd also put that the problem is that (for nv84 and later) we otherwise take the channel ID of the fence' channel and add it on top of the fence-context vma address of the target channel, which (if they have different VMMs) makes us end up with a wrong synchronization point [1].

Cross device could even be worse with very old GPUs, since ->sync() just assumes the same fence-context type between the channels.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/nouveau/nv84_fence.c#n100

Quoted 34 lines

dma_fence_init gets passed the seqno which comes from fctx->sequence, which is nouveau_fence_chan, which gets allocated for each channel.

So we should hit this path if we have 2 userspace submits, one with say graphics, the one with copy engine contexts, otherwise we should wait on the CPU.

Quoted 17 lines
Quoted 8 lines
 drivers/gpu/drm/nouveau/nouveau_fence.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +0 -0
index ee5e9d40c166f..5743c82f4094b 100644

drivers/gpu/drm/nouveau/nouveau_fence.c nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, · line 374

Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_fence.c +2 -1
@@ -370,7 +370,8 @@ nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
 
                              rcu_read_lock();
                              prev = rcu_dereference(f->channel);
-                             if (prev && (prev == chan ||
+                             if (prev && (prev->vmm == chan->vmm) &&
+                                 (prev == chan ||

Maybe better break it down a bit, e.g.

Quoted 5 lines

bool local = prev && (prev->... == chan->...);

if (local && ...) {
...
}

I'll update that once we resolve the above.

Dave.