Skip to content

Commit dcc583c

Browse files
wensChun-Kuang Hu
authored andcommitted
drm/mediatek: Correctly free sg_table in gem prime vmap
The MediaTek DRM driver implements GEM PRIME vmap by fetching the sg_table for the object, iterating through the pages, and then vmapping them. In essence, unlike the GEM DMA helpers which vmap when the object is first created or imported, the MediaTek version does it on request. Unfortunately, the code never correctly frees the sg_table contents. This results in a kernel memory leak. On a Hayato device with a text console on the internal display, this results in the system running out of memory in a few days from all the console screen cursor updates. Add sg_free_table() to correctly free the contents of the sg_table. This was missing despite explicitly required by mtk_gem_prime_get_sg_table(). Also move the "out" shortcut label to after the kfree() call for the sg_table. Having sg_free_table() together with kfree() makes more sense. The shortcut is only used when the object already has a kernel address, in which case the pointer is NULL and kfree() does nothing. Hence this change causes no functional change. Fixes: 3df64d7 ("drm/mediatek: Implement gem prime vmap/vunmap function") Cc: <[email protected]> Signed-off-by: Chen-Yu Tsai <[email protected]> Reviewed-by: CK Hu <[email protected]> Link: https://patchwork.kernel.org/project/dri-devel/patch/[email protected]/ Signed-off-by: Chun-Kuang Hu <[email protected]>
1 parent 5872080 commit dcc583c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

drivers/gpu/drm/mediatek/mtk_drm_gem.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
239239
npages = obj->size >> PAGE_SHIFT;
240240
mtk_gem->pages = kcalloc(npages, sizeof(*mtk_gem->pages), GFP_KERNEL);
241241
if (!mtk_gem->pages) {
242+
sg_free_table(sgt);
242243
kfree(sgt);
243244
return -ENOMEM;
244245
}
@@ -248,12 +249,15 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
248249
mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
249250
pgprot_writecombine(PAGE_KERNEL));
250251
if (!mtk_gem->kvaddr) {
252+
sg_free_table(sgt);
251253
kfree(sgt);
252254
kfree(mtk_gem->pages);
253255
return -ENOMEM;
254256
}
255-
out:
257+
sg_free_table(sgt);
256258
kfree(sgt);
259+
260+
out:
257261
iosys_map_set_vaddr(map, mtk_gem->kvaddr);
258262

259263
return 0;

0 commit comments

Comments
 (0)