mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "CK Hu (胡俊光)" <ck.hu@mediatek.com>
To: "p.zabel@pengutronix.de" <p.zabel@pengutronix.de>,
	"wenst@chromium.org" <wenst@chromium.org>,
	"airlied@gmail.com" <airlied@gmail.com>,
	"daniel@ffwll.ch" <daniel@ffwll.ch>,
	"chunkuang.hu@kernel.org" <chunkuang.hu@kernel.org>,
	"angelogioacchino.delregno@collabora.com" 
	<angelogioacchino.delregno@collabora.com>,
	"matthias.bgg@gmail.com" <matthias.bgg@gmail.com>
Cc: "dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-mediatek@lists.infradead.org" 
	<linux-mediatek@lists.infradead.org>,
	"linux-arm-kernel@lists.infradead.org" 
	<linux-arm-kernel@lists.infradead.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: Re: [PATCH v2] drm/mediatek: Correctly free sg_table in gem prime vmap
Date: Fri, 6 Oct 2023 10:09:49 +0000	[thread overview]
Message-ID: <d45d0357f53c8856aa03239066d092e5ed3bfdf0.camel@mediatek.com> (raw)
In-Reply-To: <20231004083226.1940055-1-wenst@chromium.org>

Hi, Chen-yu:

On Wed, 2023-10-04 at 16:32 +0800, Chen-Yu Tsai wrote:
>  	 
> External email : Please do not click links or open attachments until
> you have verified the sender or the content.
>  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.

Reviewed-by: CK Hu <ck.hu@mediatek.com>

> 
> Fixes: 3df64d7b0a4f ("drm/mediatek: Implement gem prime vmap/vunmap
> function")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
> ---
> Please merge for v6.6 fixes.
> 
> Also, I was wondering why the MediaTek DRM driver implements a lot of
> the GEM functionality itself, instead of using the GEM DMA helpers.
> From what I could tell, the code closely follows the DMA helpers,
> except
> that it vmaps the buffers only upon request.

The reason is that priv->dma_dev is different with drm_dev, so MediaTek
DRM driver have to implement its own function. Exynos DRM driver also
has this problem, so it's welcome anyone to simplify both DRM driver.

Regards,
CK

> 
>  drivers/gpu/drm/mediatek/mtk_drm_gem.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> index 9f364df52478..0e0a41b2f57f 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> @@ -239,6 +239,7 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object
> *obj, struct iosys_map *map)
>  	npages = obj->size >> PAGE_SHIFT;
>  	mtk_gem->pages = kcalloc(npages, sizeof(*mtk_gem->pages),
> GFP_KERNEL);
>  	if (!mtk_gem->pages) {
> +		sg_free_table(sgt);
>  		kfree(sgt);
>  		return -ENOMEM;
>  	}
> @@ -248,12 +249,15 @@ int mtk_drm_gem_prime_vmap(struct
> drm_gem_object *obj, struct iosys_map *map)
>  	mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
>  			       pgprot_writecombine(PAGE_KERNEL));
>  	if (!mtk_gem->kvaddr) {
> +		sg_free_table(sgt);
>  		kfree(sgt);
>  		kfree(mtk_gem->pages);
>  		return -ENOMEM;
>  	}
> -out:
> +	sg_free_table(sgt);
>  	kfree(sgt);
> +
> +out:
>  	iosys_map_set_vaddr(map, mtk_gem->kvaddr);
>  
>  	return 0;
> -- 
> 2.42.0.582.g8ccd20d70d-goog

      parent reply	other threads:[~2023-10-06 10:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-04  8:32 Chen-Yu Tsai
2023-10-04  8:33 ` Chen-Yu Tsai
2023-10-06 10:09 ` CK Hu (胡俊光) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d45d0357f53c8856aa03239066d092e5ed3bfdf0.camel@mediatek.com \
    --to=ck.hu@mediatek.com \
    --cc=airlied@gmail.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=chunkuang.hu@kernel.org \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=p.zabel@pengutronix.de \
    --cc=stable@vger.kernel.org \
    --cc=wenst@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®