Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2459230rdb; Wed, 4 Oct 2023 01:34:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGr4jss3bMHsco45G7wCE9Cg8voURdZf1dkPwAfCIqn7DX8n6zELwtdcT8lcvYG42oWlR2p X-Received: by 2002:a05:6a21:271c:b0:132:79da:385a with SMTP id rm28-20020a056a21271c00b0013279da385amr1478530pzb.23.1696408463922; Wed, 04 Oct 2023 01:34:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696408463; cv=none; d=google.com; s=arc-20160816; b=wOGP3yIDWMrqmGNaKEsxdXZcLXiaxpQm8uDmmGYRZLafE8t1vSGjpjVbpVHDQpRRKp AgAEwJRy439Dxj6g5LsEchBo0LSzfKCUqCL6iZtBoMG6v1BfMYK7Cmj8dPHVJ0LQQ6Hx 1gLbvg8MB+gZkmXwIi85lDsat0gp95uGiPgVydpu0u0xtXnGy1DAOGXWfJua4FWsbzfi TMSpU8Uaiof3T6Dj80FOZe/SAJwbsTl8x3VO+5GhK/UzYDT72ZKuL+z6V7zOQ5L1RDD+ LeeGyFLi1yqSPbYNOpm50of6dpKT9RBvJqvDEo0ocVc5acJZK+ScC1Vfa9SNdcp+TjhV DykA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=wfmW3coigpVfgSMijltIl7QADja7SAFkOQQrbcYexQk=; fh=yjAjNrGMEkxcil/2eSrEfNQJUEqdzlG6B6mNhsODtdY=; b=zAxblODMjIHOSd/y3q1YGIOweqWEHH3DPI7EGmXcO2zUh2wTEAtSQZYbH9pzFzhriW s7mO0oifXdmccxBz8fIKQEzs5+9NdZg8/nTuPM7+uPG6wbUAe0GhfLN1pSDT3g2fvbkP 1npBeKse5U0ZtH5Bb1ZRk+MdVAP/9eplaFE+18EaiuncIcYX063eID+YQQMZs40PgHIl LFt4uq3IFfGxnsF+1KmlWi8bUL7nszGCH84LlHLxKR1g8Q6cV49mI/gmvIvyiXiXh0Jx VROFZ+CxZrmH6AY1p3CrTl1fkbeAFR4yO0FrHTG9oqJhV3gWs33hqIyLXPbaptnnwmSy rhuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bOT0lP9l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id ay6-20020a1709028b8600b001c45e1f034esi3075951plb.141.2023.10.04.01.34.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 01:34:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bOT0lP9l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id A08AD81A05B6; Wed, 4 Oct 2023 01:34:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241693AbjJDIeL (ORCPT + 99 others); Wed, 4 Oct 2023 04:34:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241688AbjJDIeJ (ORCPT ); Wed, 4 Oct 2023 04:34:09 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F13E6C1 for ; Wed, 4 Oct 2023 01:34:04 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-50573e85ee0so2148934e87.3 for ; Wed, 04 Oct 2023 01:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696408443; x=1697013243; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=wfmW3coigpVfgSMijltIl7QADja7SAFkOQQrbcYexQk=; b=bOT0lP9lDC2xqTqru/LCIAZnVwJIJWhZVa3WKIV+MlonLzZRsspeMiktA08pHAiIZe VVe0uki+9l2/J8f9+nh3ZAq6l5lv3v5cyc2QroPyU789pcrj7gJryZiLuZrKeDsDmVAm FPtmjbqMwSF0oWZe9tMW4uS59OVTWEXszvx+A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696408443; x=1697013243; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wfmW3coigpVfgSMijltIl7QADja7SAFkOQQrbcYexQk=; b=I4iUCTtieAPb0s7xJAi6Pzx2BMJB0QnvdlHZUsnlmfqkv1TLXbgSUVf8ND47HEafx3 h9DuSQJOdT27JRPdo4T2VrIIaBeAdbzwlmjAyV/+egLRPtQR2PbH5tQcFo7ywQK8KbPg mUfKYgRZz1O9x2qPdtLgahri3rMKgBiUoswCwobpNSBA85SI5/WIDkWroVrmI1sBKvf7 CpvkMij5ffS1LxDWGO6CPC6QFK6Y/NwBNEaXzGUwQZj2qnshSoJTpiX6iMizaSmgxvMb yysKqTqzoEbgwyApE3QozU4L78/EfRablITzDlLRgO2H7vkpeb+3Ey8dwST0l9Etz+km r6vQ== X-Gm-Message-State: AOJu0YyGh1vcZ9sZfKeVL3CpDVNOU+dCmwAsoDUqoBoCcQev13W5E933 11dXwlKxmvwLaGH/Y3shs3d9QjUKiKorc8Fg6UWPkA== X-Received: by 2002:a19:e015:0:b0:500:94c3:8e3b with SMTP id x21-20020a19e015000000b0050094c38e3bmr1344729lfg.57.1696408442607; Wed, 04 Oct 2023 01:34:02 -0700 (PDT) MIME-Version: 1.0 References: <20231004083226.1940055-1-wenst@chromium.org> In-Reply-To: <20231004083226.1940055-1-wenst@chromium.org> From: Chen-Yu Tsai Date: Wed, 4 Oct 2023 16:33:51 +0800 Message-ID: Subject: Re: [PATCH v2] drm/mediatek: Correctly free sg_table in gem prime vmap To: Chun-Kuang Hu , Philipp Zabel , David Airlie , Daniel Vetter , Matthias Brugger , AngeloGioacchino Del Regno Cc: dri-devel@lists.freedesktop.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 04 Oct 2023 01:34:19 -0700 (PDT) On Wed, Oct 4, 2023 at 4:32=E2=80=AFPM Chen-Yu Tsai wr= ote: > > 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: 3df64d7b0a4f ("drm/mediatek: Implement gem prime vmap/vunmap funct= ion") > Cc: > Signed-off-by: Chen-Yu Tsai > --- Changes since v1: - Move "out" shortcut label to after sg_free_table() and kfree() > 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. > > 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/med= iatek/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 =3D obj->size >> PAGE_SHIFT; > mtk_gem->pages =3D kcalloc(npages, sizeof(*mtk_gem->pages), GFP_K= ERNEL); > 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 *o= bj, struct iosys_map *map) > mtk_gem->kvaddr =3D 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 >