Message ID | 20221220094014.1128207-1-zyytlz.wz@163.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp2870008wrn; Tue, 20 Dec 2022 01:43:27 -0800 (PST) X-Google-Smtp-Source: AA0mqf51oNf1H4S/zZN+YNBq2A7MCxBLz2dVJtUrtxUn/Vjzd99oTLxMWiBy9xag8Iq2sKmPEsFt X-Received: by 2002:a05:6402:1cc3:b0:45c:834b:eb44 with SMTP id ds3-20020a0564021cc300b0045c834beb44mr60498807edb.15.1671529406868; Tue, 20 Dec 2022 01:43:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671529406; cv=none; d=google.com; s=arc-20160816; b=px0AoJdKzUEGFCG3D8EEd8P20Z+6jvZJi1dzXCndvlPTL8pV4RId3+Qdm528REwFlm nNuRA7di2ZatTaCKrK+WcIs+Vm2y99tpgE7kj2e8yysC+B6OAlOvqzV30yfKamI0SAub 1Zlnr2koNEzOQ6Pg1y2c3Ot8Vhr5dZmr0fqZ8tGmwdP/xQaPHi+FyE9NrBcmFr20DycL 0tTwBJHJ7ozpdeepu4vySYDfhKtBao9AxpcB/P4WB2wSy7QVKG2esrVF6pOT9tR4oxsz Bfode+eTEFHTq+XXBY9JRublEfsStZCV46FpXvkZmLSn5OQ5SDfQrW4VTgms44lnXCn6 CnTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5U/Q0IzBncgGJ65mZDk0e6NuKaOGAn75Xs2OHH9bGHk=; b=0AwArj+/iXcSzmXzb9cqrw739tFZbz87t93n7lG9UX7LG3Kzmrrn7vt+OdtEdscgPF 67SCSlK8aduGS2O2hCiUj8YEUyYWHZeAJOZGYPkmbn2zpinBkD/a5FhAaS47DMAw7dg8 2GKPJX0Wdo6b0uGledwtfpqfgaQUI/mC0w6wqUY4cFSFT70GInipQJ0VBD4SO/i7cyVz 1vVsFVEnVavw4md/a7/RhkJLLVd+gcOYAb5CJsJbj2/WXTlu4Z3pFSTbeHJaVvY9vzif c/OwuPtw/oofp2MQqBHH6j4fKMQ14Cxl/ozuTtsmhyapyzZq3e9ZxUkCTT3zVcW2jwIE RKbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=SrkBw11w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho38-20020a1709070ea600b007baa6e22742si12911977ejc.570.2022.12.20.01.43.03; Tue, 20 Dec 2022 01:43:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=SrkBw11w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233479AbiLTJlv (ORCPT <rfc822;abdi.embedded@gmail.com> + 99 others); Tue, 20 Dec 2022 04:41:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233569AbiLTJlZ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 20 Dec 2022 04:41:25 -0500 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 731F817599 for <linux-kernel@vger.kernel.org>; Tue, 20 Dec 2022 01:41:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=5U/Q0 IzBncgGJ65mZDk0e6NuKaOGAn75Xs2OHH9bGHk=; b=SrkBw11w8K6FHxt1r/Cva GQWOb75ACnz7P5xM98IkqXOjfmO8KduGo4Pv8Xi7FjygF1zJFdFsOK1cgwI4gtKh IJ3JRQ+xud5UYjcCa/m1uk+LeX95NeT/jesrY9D1/ctk2Px2r0+2yJ7uTM6P29q3 vrWyHZx9Z+ZeDdw9r7kMUo= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g0-3 (Coremail) with SMTP id _____wB3j3r_gqFjgjtjAA--.34322S2; Tue, 20 Dec 2022 17:40:15 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: zhi.a.wang@intel.com Cc: 1002992920@qq.com, airlied@gmail.com, airlied@linux.ie, alex000young@gmail.com, dri-devel@lists.freedesktop.org, gregkh@linuxfoundation.org, hackerzheng666@gmail.com, intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, joonas.lahtinen@linux.intel.com, linux-kernel@vger.kernel.org, security@kernel.org, tvrtko.ursulin@linux.intel.com, zhenyuw@linux.intel.com, zyytlz.wz@163.com Subject: [PATCH v5] drm/i915/gvt: fix double free bug in split_2MB_gtt_entry Date: Tue, 20 Dec 2022 17:40:14 +0800 Message-Id: <20221220094014.1128207-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <11728bc1-7b59-1623-b517-d1a0d57eb275@intel.com> References: <11728bc1-7b59-1623-b517-d1a0d57eb275@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wB3j3r_gqFjgjtjAA--.34322S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7AFW8WFy7XF4DtF17trWxtFb_yoW5JF43pF W8XF4YyF48ZF1Ivw47uF18AFy3Z3W3Xa48WrZ7Ka4Ykrs0qF1qkr90yFW5Xr97uFWDAw4f Cr4DJrW3Ca4jvaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRomh7UUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/xtbBzgbdU2I0W3z3ogAAsi X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747798198327243367?= X-GMAIL-MSGID: =?utf-8?q?1752725619325570901?= |
Series |
[v5] drm/i915/gvt: fix double free bug in split_2MB_gtt_entry
|
|
Commit Message
Zheng Wang
Dec. 20, 2022, 9:40 a.m. UTC
If intel_gvt_dma_map_guest_page failed, it will call
ppgtt_invalidate_spt, which will finally free the spt. But the
caller function ppgtt_populate_spt_by_guest_entry does not notice
that, it will free spt again in its error path.
Fix this by undoing the mapping of DMA address and freeing sub_spt.
Besides, leave the handle of spt destroy to caller function instead of
callee function when error occurs.
Fixes: b901b252b6cf ("drm/i915/gvt: Add 2M huge gtt support")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
v5:
- remove unnecessary switch-case code for there is only one particular case,
correct the unmap target from parent_spt to sub_spt.add more details in
commit message. All suggested by Zhenyu
v4:
- fix by undo the mapping of DMA address and free sub_spt suggested by Zhi
v3:
- correct spelling mistake and remove unused variable suggested by Greg
v2: https://lore.kernel.org/all/20221006165845.1735393-1-zyytlz.wz@163.com/
v1: https://lore.kernel.org/all/20220928033340.1063949-1-zyytlz.wz@163.com/
---
drivers/gpu/drm/i915/gvt/gtt.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
Comments
On 2022.12.20 17:40:14 +0800, Zheng Wang wrote: > If intel_gvt_dma_map_guest_page failed, it will call > ppgtt_invalidate_spt, which will finally free the spt. But the > caller function ppgtt_populate_spt_by_guest_entry does not notice > that, it will free spt again in its error path. indent > > Fix this by undoing the mapping of DMA address and freeing sub_spt. > Besides, leave the handle of spt destroy to caller function instead of > callee function when error occurs. > > Fixes: b901b252b6cf ("drm/i915/gvt: Add 2M huge gtt support") > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > --- > v5: > - remove unnecessary switch-case code for there is only one particular case, > correct the unmap target from parent_spt to sub_spt.add more details in > commit message. All suggested by Zhenyu > > v4: > - fix by undo the mapping of DMA address and free sub_spt suggested by Zhi > > v3: > - correct spelling mistake and remove unused variable suggested by Greg > > v2: https://lore.kernel.org/all/20221006165845.1735393-1-zyytlz.wz@163.com/ > > v1: https://lore.kernel.org/all/20220928033340.1063949-1-zyytlz.wz@163.com/ > --- > drivers/gpu/drm/i915/gvt/gtt.c | 18 ++++++++++++++---- > 1 file changed, 14 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c > index 51e5e8fb505b..4d478a59eb7d 100644 > --- a/drivers/gpu/drm/i915/gvt/gtt.c > +++ b/drivers/gpu/drm/i915/gvt/gtt.c > @@ -1209,10 +1209,8 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, > for_each_shadow_entry(sub_spt, &sub_se, sub_index) { > ret = intel_gvt_dma_map_guest_page(vgpu, start_gfn + sub_index, > PAGE_SIZE, &dma_addr); > - if (ret) { > - ppgtt_invalidate_spt(spt); > - return ret; > - } > + if (ret) > + goto err; > sub_se.val64 = se->val64; > > /* Copy the PAT field from PDE. */ > @@ -1231,6 +1229,18 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, > ops->set_pfn(se, sub_spt->shadow_page.mfn); > ppgtt_set_shadow_entry(spt, se, index); > return 0; > +err: > + /* Undone the existing mappings of DMA addr. */ We need a verb here for Undo. > + for_each_present_shadow_entry(sub_spt, &sub_se, sub_index) { > + gvt_vdbg_mm("invalidate 4K entry\n"); > + ppgtt_invalidate_pte(sub_spt, &sub_se); > + } > + /* Release the new allocated spt. */ > + trace_spt_change(sub_spt->vgpu->id, "release", sub_spt, > + sub_spt->guest_page.gfn, sub_spt->shadow_page.type); > + ppgtt_free_spt(sub_spt); > + sub_spt = NULL; Not need to reset local variable that has no use then. I'll handle these trivial fixes during the merge. Reviewed-by: Zhenyu Wang <zhenyuw@linux.intel.com> thanks > + return ret; > } > > static int split_64KB_gtt_entry(struct intel_vgpu *vgpu, > -- > 2.25.1 >
Zhenyu Wang <zhenyuw@linux.intel.com> 于2022年12月21日周三 11:01写道: > > On 2022.12.20 17:40:14 +0800, Zheng Wang wrote: > > If intel_gvt_dma_map_guest_page failed, it will call > > ppgtt_invalidate_spt, which will finally free the spt. But the > > caller function ppgtt_populate_spt_by_guest_entry does not notice > > that, it will free spt again in its error path. > > indent Yeap :) > > + if (ret) > > + goto err; > > sub_se.val64 = se->val64; > > > > /* Copy the PAT field from PDE. */ > > @@ -1231,6 +1229,18 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, > > ops->set_pfn(se, sub_spt->shadow_page.mfn); > > ppgtt_set_shadow_entry(spt, se, index); > > return 0; > > +err: > > + /* Undone the existing mappings of DMA addr. */ > > We need a verb here for Undo. Get it. > > > + for_each_present_shadow_entry(sub_spt, &sub_se, sub_index) { > > + gvt_vdbg_mm("invalidate 4K entry\n"); > > + ppgtt_invalidate_pte(sub_spt, &sub_se); > > + } > > + /* Release the new allocated spt. */ > > + trace_spt_change(sub_spt->vgpu->id, "release", sub_spt, > > + sub_spt->guest_page.gfn, sub_spt->shadow_page.type); > > + ppgtt_free_spt(sub_spt); > > + sub_spt = NULL; > > Not need to reset local variable that has no use then. > > I'll handle these trivial fixes during the merge. > Very thanks for that. Best regards, Zheng Wang
diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c index 51e5e8fb505b..4d478a59eb7d 100644 --- a/drivers/gpu/drm/i915/gvt/gtt.c +++ b/drivers/gpu/drm/i915/gvt/gtt.c @@ -1209,10 +1209,8 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, for_each_shadow_entry(sub_spt, &sub_se, sub_index) { ret = intel_gvt_dma_map_guest_page(vgpu, start_gfn + sub_index, PAGE_SIZE, &dma_addr); - if (ret) { - ppgtt_invalidate_spt(spt); - return ret; - } + if (ret) + goto err; sub_se.val64 = se->val64; /* Copy the PAT field from PDE. */ @@ -1231,6 +1229,18 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, ops->set_pfn(se, sub_spt->shadow_page.mfn); ppgtt_set_shadow_entry(spt, se, index); return 0; +err: + /* Undone the existing mappings of DMA addr. */ + for_each_present_shadow_entry(sub_spt, &sub_se, sub_index) { + gvt_vdbg_mm("invalidate 4K entry\n"); + ppgtt_invalidate_pte(sub_spt, &sub_se); + } + /* Release the new allocated spt. */ + trace_spt_change(sub_spt->vgpu->id, "release", sub_spt, + sub_spt->guest_page.gfn, sub_spt->shadow_page.type); + ppgtt_free_spt(sub_spt); + sub_spt = NULL; + return ret; } static int split_64KB_gtt_entry(struct intel_vgpu *vgpu,