Skip to contentSkip to conversation

drm/amdgpu: unmap GART dma pages before free

PATCH Superseded Michail Tatas 2 participants 2 messages Sep 22

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: unmap GART dma pages before free

amdgpu_gart_table_ram_alloc() maps the GART dma pages and neither the error path nor amdgpu_gart_table_ram_free() unmaps those pages before freeing them, which could leave the device writing to freed memory.

Fix by unmapping the GART dma pages before freeing them.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +4 -0
index c4c21dbbbdbf..780fb01530e6 100644
@@ -209,6 +209,8 @@ int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
 error_resv:
 	amdgpu_bo_unreserve(bo);
 error:
+	dma_unmap_page(&adev->pdev->dev, sg_dma_address(sg->sgl), adev->gart.table_size,
+		       DMA_BIDIRECTIONAL);
 	amdgpu_bo_unref(&bo);
 	if (sg) {
 		sg_free_table(sg);
@@ -240,6 +242,8 @@ void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
 		amdgpu_bo_unreserve(adev->gart.bo);
 	}
 	amdgpu_bo_unref(&adev->gart.bo);
+	dma_unmap_page(&adev->pdev->dev, sg_dma_address(sg->sgl), adev->gart.table_size,
+		       DMA_BIDIRECTIONAL);
 	sg_free_table(sg);
 	kfree(sg);
 	p = virt_to_page(adev->gart.ptr);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/amdgpu: NULL pointer dereference in amdgpu_gart_table_ram_alloc error path --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/amdgpu: NULL pointer dereference in amdgpu_gart_table_ram_alloc error path --

commit cffcb1919743d55f9dc644f758abf99d4187be7c
Author: Michail Tatas <[email protected]>

drm/amdgpu: unmap GART dma pages before free

This commit unmaps the GART dma pages before freeing them to avoid leaving the device writing to freed memory.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +0 -0
index c4c21dbbbdbf8..780fb01530e69 100644

[ ... ]

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev) · line 213

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +2 -0
@@ -209,6 +209,8 @@ int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
 error_resv:
 	amdgpu_bo_unreserve(bo);
 error:
+	dma_unmap_page(&adev->pdev->dev, sg_dma_address(sg->sgl), adev->gart.table_size,
+		       DMA_BIDIRECTIONAL);

[Severity: High]
Can this cause a NULL pointer dereference during memory pressure?

If memory allocation fails earlier in amdgpu_gart_table_ram_alloc(), sg is set to NULL and execution jumps to the error label:

	sg = kmalloc_obj(*sg);
	if (!sg) {
		ret = -ENOMEM;
		goto error;
	}

The new dma_unmap_page() call unconditionally accesses sg->sgl at the error label. If the execution arrived here due to the allocation failure above, dereferencing sg->sgl will crash.

Additionally, if sg_alloc_table() fails, sg is allocated but sg->sgl may be uninitialized when accessed here.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +0 -0
 	amdgpu_bo_unref(&bo);
 	if (sg) {
 		sg_free_table(sg);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/arKhDFCSoPDNYaxX@michalis-linux?part=1