From patchwork Thu Nov 24 06:23:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cindy Lu X-Patchwork-Id: 25328 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3220904wrr; Wed, 23 Nov 2022 22:26:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf5IwYjFOoD9+cqErwUzcoQNafzr+yItUWk7e95sDFHY6mA0qpSRbFdNIedJvkR6YmkbcNmR X-Received: by 2002:a63:5a02:0:b0:477:15c9:423b with SMTP id o2-20020a635a02000000b0047715c9423bmr28397567pgb.374.1669271167215; Wed, 23 Nov 2022 22:26:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669271167; cv=none; d=google.com; s=arc-20160816; b=x3LmCyCCCCi24zW2bupzolbwSIQI21Hp+XztGzg4PHDQnY+RhK1Tom4NFF0gYSFJFE 4NjW4eBQs0l4KaaZWcMk7rJxc1hDu0UiCCFl6+ng1NhRS10fyrCvccBxLuxxRY5p/ANR wp6CkpOaAVLZocrs9SHU9CfoA4W/lGK2MsVW74v3ZFljuNBt7Cvay7pTVFLgjXor32qh pLiPRk1iJqrgDbRZP7KIC13kCJJz4c83LROwUxRAdlDgIOMqXZFLRmREuZYGF5EF6jrF lMoS8I4dyWvBk0LEvul/xLBIweN4cW8CZEl04GMqfAVmUXEoyDMGeKz490kH3dHYtIB3 tlWQ== 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 :message-id:date:subject:to:from:dkim-signature; bh=bH5r1DTPkPYQR0htLjtLo5pkCkq5AEtL+jOtp/FRJ5o=; b=YOeOBjr6HYzZhyM4U5pr96ip6WmYR9r2Vi/cwIs93Ee/Yxn/cuRnv1e+hm9xPgA7hF d1J3gs8pb0C03O/Zcm3r0ydUWFElFYHveHWiOTZnmqYGyK94JJTd2mdsYCvOoXMsIDoG 46Kpl6DMyOTv64R8wP9Vverk8zp8m2EZGvc18c505VmjCJv0PlgDfrPiPejO4bd2f3pa YulYR3iinkP5TFgANAcNebr+Hn9+/mgrLPmFCtV6ztbOGaOBdCbciVRYTOMFTRGxnf6x euV122WSW+pVxFIg4UljllPEhehrZHge42U/rsqs0NfjPH/ScNsoYArsJ8Do0cbOcr4f GOWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Xv7RT1Bl; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s6-20020a170902ea0600b00176d22a068csi204245plg.515.2022.11.23.22.25.53; Wed, 23 Nov 2022 22:26:07 -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=@redhat.com header.s=mimecast20190719 header.b=Xv7RT1Bl; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229507AbiKXGYU (ORCPT + 99 others); Thu, 24 Nov 2022 01:24:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229463AbiKXGYS (ORCPT ); Thu, 24 Nov 2022 01:24:18 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E22F391F1 for ; Wed, 23 Nov 2022 22:23:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669270999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=bH5r1DTPkPYQR0htLjtLo5pkCkq5AEtL+jOtp/FRJ5o=; b=Xv7RT1BlubWiT7yo30Bqn6HwUV5ZUpZ1N4k4EY4EzP3b6mUpZvxbg58v0cy/jgvb3uUOxR 607Go4kD7ZnvNFbjrZPxrVsJCzmJRKxtpX2c8tLjgZ/vUakHGIVDFe9OL3N+hirkhMGD0R dugXRCQ74CoFD/WuW5ipaax6Kx5FrUc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-299-SqTPytWqMUmF7_aVyzjx5A-1; Thu, 24 Nov 2022 01:23:15 -0500 X-MC-Unique: SqTPytWqMUmF7_aVyzjx5A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7811E1C06ECB; Thu, 24 Nov 2022 06:23:15 +0000 (UTC) Received: from server.redhat.com (ovpn-12-152.pek2.redhat.com [10.72.12.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2EB3B40C206B; Thu, 24 Nov 2022 06:23:12 +0000 (UTC) From: Cindy Lu To: mst@redhat.com, jasowang@redhat.com, lulu@redhat.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH] vhost_vdpa: fix the crash in unmap a large memory Date: Thu, 24 Nov 2022 14:23:09 +0800 Message-Id: <20221124062309.2081720-1-lulu@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750357683575385439?= X-GMAIL-MSGID: =?utf-8?q?1750357683575385439?= While testing in vIOMMU, sometimes guest will unmap very large memory, which will cause the crash. To fix this,Move the iommu_unmap to vhost_vdpa_pa_unmap/vhost_vdpa_va_unmap and only unmap the memory that saved in iotlb. Signed-off-by: Cindy Lu --- drivers/vhost/vdpa.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 166044642fd5..c392979702cf 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -692,6 +692,8 @@ static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, struct vhost_iotlb_map *map; struct page *page; unsigned long pfn, pinned; + struct vdpa_device *vdpa = v->vdpa; + const struct vdpa_config_ops *ops = vdpa->config; while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) { pinned = PFN_DOWN(map->size); @@ -703,6 +705,8 @@ static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, unpin_user_page(page); } atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm); + if ((ops->dma_map == NULL) && (ops->set_map == NULL)) + iommu_unmap(v->domain, map->start, map->size); vhost_iotlb_map_free(iotlb, map); } } @@ -713,11 +717,15 @@ static void vhost_vdpa_va_unmap(struct vhost_vdpa *v, { struct vhost_iotlb_map *map; struct vdpa_map_file *map_file; + struct vdpa_device *vdpa = v->vdpa; + const struct vdpa_config_ops *ops = vdpa->config; while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) { map_file = (struct vdpa_map_file *)map->opaque; fput(map_file->file); kfree(map_file); + if ((ops->dma_map == NULL) && (ops->set_map == NULL)) + iommu_unmap(v->domain, map->start, map->size); vhost_iotlb_map_free(iotlb, map); } } @@ -805,8 +813,6 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, } else if (ops->set_map) { if (!v->in_batch) ops->set_map(vdpa, asid, iotlb); - } else { - iommu_unmap(v->domain, iova, size); } /* If we are in the middle of batch processing, delay the free