Skip to contentSkip to conversation

drm/nouveau/gsp: Preserve BAR2 flush mapping on kmap failure

PATCH New Ruoyu Wang 2 participants 2 messages Jul 9

Subsystems (3)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 drm/nouveau/gsp: Preserve BAR2 flush mapping on kmap failure

r535_bar_bar2_init() warns if the FB zero object cannot be wrapped or mapped, but then continues and maps bar->flushFBZero through nvkm_kmap().

If the object was not created, nvkm_kmap() dereferences NULL. If nvkm_kmap() returns NULL, assigning the result to bar->flushBAR2 clears the existing physical-mode flush mapping and later r535_bar_flush() can dereference NULL after the generic BAR2 init wrapper marks BAR2 active.

Keep the physical-mode flush mapping unless the BAR2 flush mapping was successfully acquired. In fini, call nvkm_done() only for an acquired BAR2 flush mapping, because nvkm_done() is not NULL-safe.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 5bf0257136a2 ("drm/nouveau/mmu/r535: initial support")
---
 .../gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c  | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c +10 -4
index fae08ac3b18c7..eb3a7ddfa3355 100644
@@ -71,8 +71,9 @@ r535_bar_bar2_fini(struct nvkm_bar *bar)
 	struct nvkm_vmm *vmm = gf100_bar(bar)->bar[0].vmm;
 	struct nvkm_gsp *gsp = bar->subdev.device->gsp;
 
+	if (bar->flushFBZero && bar->flushBAR2 != bar->flushBAR2PhysMode)
+		nvkm_done(bar->flushFBZero);
 	bar->flushBAR2 = bar->flushBAR2PhysMode;
-	nvkm_done(bar->flushFBZero);
 
 	WARN_ON(r535_bar_bar2_update_pde(gsp, vmm->func->page[0].shift, 0));
 }
@@ -86,6 +87,7 @@ r535_bar_bar2_init(struct nvkm_bar *bar)
 	struct nvkm_memory *pdb = vmm->pd->pt[0]->memory;
 	u32 pdb_offset = vmm->pd->pt[0]->base;
 	u32 pdbe_lo, pdbe_hi;
+	void __iomem *flush;
 	u64 pdbe;
 
 	nvkm_kmap(pdb);
@@ -106,12 +108,16 @@ r535_bar_bar2_init(struct nvkm_bar *bar)
 			ret = nvkm_memory_kmap(fbZero, &bar->flushFBZero);
 			nvkm_memory_unref(&fbZero);
 		}
-		WARN_ON(ret);
+		if (WARN_ON(ret))
+			return;
 	}
 
+	flush = nvkm_kmap(bar->flushFBZero);
+	if (WARN_ON(!flush))
+		return;
+
+	bar->flushBAR2 = flush;
 	bar->bar2 = true;
-	bar->flushBAR2 = nvkm_kmap(bar->flushFBZero);
-	WARN_ON(!bar->flushBAR2);
 }
 
 static void
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unbalanced acquire/release refcount leak when nvkm_kmap() returns NULL --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unbalanced acquire/release refcount leak when nvkm_kmap() returns NULL --

--- Patch [1]: [PATCH] drm/nouveau/gsp: Preserve BAR2 flush mapping on kmap failure --- commit 31b13bcd1196c2d2aeb9a843ab22642741567833
Author: Ruoyu Wang <[email protected]>

drm/nouveau/gsp: Preserve BAR2 flush mapping on kmap failure

This patch prevents a NULL pointer dereference in r535_bar_bar2_init() by checking the return value of nvkm_kmap() before assignment. It also preserves the physical-mode flush mapping unless the BAR2 flush mapping was successfully acquired.

Quoted 4 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c +0 -0
index 9cd68f8622d36..b7b44baf836cb 100644

[ ... ]

drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c r535_bar_bar2_fini(struct nvkm_bar *bar) · line 76

Quoted 7 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c +2 -0
@@ -71,8 +71,9 @@ r535_bar_bar2_fini(struct nvkm_bar *bar)
 	struct nvkm_vmm *vmm = gf100_bar(bar)->bar[0].vmm;
 	struct nvkm_gsp *gsp = bar->subdev.device->gsp;
 
+	if (bar->flushFBZero && bar->flushBAR2 != bar->flushBAR2PhysMode)
+		nvkm_done(bar->flushFBZero);
 	bar->flushBAR2 = bar->flushBAR2PhysMode;

[Severity: Medium]
Will this skip the necessary cleanup if nvkm_kmap() failed?

Because the failure path in r535_bar_bar2_init() below doesn't call nvkm_done(), and this condition now skips nvkm_done() if the flush pointer wasn't updated, this appears to cement a reference leak of the mapping.

Quoted 4 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c +0 -1
-	nvkm_done(bar->flushFBZero);
 
 	WARN_ON(r535_bar_bar2_update_pde(gsp, vmm->func->page[0].shift, 0));
 }

[ ... ]

drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c r535_bar_bar2_init(struct nvkm_bar *bar) · line 117

Quoted 12 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c +5 -1
@@ -106,12 +108,16 @@ r535_bar_bar2_init(struct nvkm_bar *bar)
 			ret = nvkm_memory_kmap(fbZero, &bar->flushFBZero);
 			nvkm_memory_unref(&fbZero);
 		}
-		WARN_ON(ret);
+		if (WARN_ON(ret))
+			return;
 	}
 
+	flush = nvkm_kmap(bar->flushFBZero);
+	if (WARN_ON(!flush))
+		return;

[Severity: Medium]
Does this early return cause a refcount leak?

In the Nouveau driver, nvkm_kmap(o) expands to (o)->func->acquire(o). The acquire implementation always increments the mapping reference count and removes the object from the LRU list, even if it returns a NULL mapping.

If nvkm_kmap() fails to map and returns NULL, shouldn't nvkm_done() still be called to decrement the refcount and unpin the object before returning?

Quoted 8 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/bar.c +2 -2
+
+	bar->flushBAR2 = flush;
 	bar->bar2 = true;
-	bar->flushBAR2 = nvkm_kmap(bar->flushFBZero);
-	WARN_ON(!bar->flushBAR2);
 }
 
 static void
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1