Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1709624rwb; Tue, 29 Nov 2022 18:14:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf6dIO4IyFTwHYZ0xxqu61eSOnOSgPDwUJW/ysb6n5GV7YC0sItLwRka4gx+k/QfbyjZIoIP X-Received: by 2002:a17:906:9718:b0:7bf:1090:ded6 with SMTP id k24-20020a170906971800b007bf1090ded6mr12289330ejx.577.1669774486812; Tue, 29 Nov 2022 18:14:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669774486; cv=none; d=google.com; s=arc-20160816; b=KvDfX/XE3jKKcBMUa9SSh6iaSWym1PJB79bS9VcX3Iq1zp/ugo+2yzD+CuqCHY9wj5 TsG7Edan9F1zfgJKfnAvo75ROuy+R3w3Q3/yoiJRfiCl0Z40YGFpvTGpA9Pn7Mf4ZHMc kuLxlh7OE+ZZAhL9uhXFKTEwQV7iVK9oHluq7qKfajIw3FynGv3vY6VRD0GgCHdoP0K+ Pvj/ENTChMOZOrcdvIOa8ijBg2tHFenqiP5MD5awXADwIB2WZOtxBw0PwDhlNyXchoVn JxfRgDh6NxCSN0VUvO7L6K06JAmW1/CwwUmsVtZKu+JHcm5A6eK4q2LFyM1age1mYOwn 6Tvg== 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=rNO7g8bYXcgKYmsyunc7q9RriXvgw7+u6mtPM1gx+ic=; b=BqUeKQQS3/8m7JLetB+yQsDnqBKRjUIuuMhDazhvxNQoqAkclh7riZZorh/CFLlwa0 BprWxLjMGzzeVLq4KbfPGxdDgCmBZMUIw3S/YeJmOOM17bFVXQKw4ffkA/BS/FshxcDV /ksvPP7qzo4s+Ek5qSS7SEpSyU1bHvdWNP/99s8oVLKQaBUzXFW5yUfsVNso/3V/jeA5 ilYEP0za38KxftBgeTrYbEpfJruYVrOZl+1hiS8RXukIhgwWZC7vRE065on55q+IUp2p ZGWYqwPMXUtOd8gEB4kGNUVwqz3Jw4xOvyA7HrxMi18yCYnhWusxymEeUR6UunnWARZ2 SoRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Zzsvmblw; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o9-20020a170906974900b0078d484e0e79si212308ejy.481.2022.11.29.18.14.26; Tue, 29 Nov 2022 18:14:46 -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=Zzsvmblw; 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 S231438AbiK3BlF (ORCPT + 85 others); Tue, 29 Nov 2022 20:41:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230101AbiK3BlD (ORCPT ); Tue, 29 Nov 2022 20:41:03 -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 C4ECE720AF for ; Tue, 29 Nov 2022 17:40:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669772407; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rNO7g8bYXcgKYmsyunc7q9RriXvgw7+u6mtPM1gx+ic=; b=Zzsvmblwls+I90vlUxSN4NX90573P46ZsH7eKe8+V12upWNFpx8FmoPOmb5X6gjBuvf/jM X52vFNkDgyk9mFhcmlzPSmCCspai0RVG7rXvUK5uvdSK+PLf9FvXwLAgNRW5lRZ7KqdFlY dw0mnTdB9h71k8CPyLbWe1Gfpmh5WLE= Received: from mail-vs1-f71.google.com (mail-vs1-f71.google.com [209.85.217.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-483-S0u_W4yPOJW5RrvwI2R71A-1; Tue, 29 Nov 2022 20:40:06 -0500 X-MC-Unique: S0u_W4yPOJW5RrvwI2R71A-1 Received: by mail-vs1-f71.google.com with SMTP id k17-20020a056102005100b003b09dba645fso3568229vsp.9 for ; Tue, 29 Nov 2022 17:40:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=rNO7g8bYXcgKYmsyunc7q9RriXvgw7+u6mtPM1gx+ic=; b=ExaFu7G7ZVnf6cyKdJq4YTdcKUqZWY6eJM38vNw9p3e5oFvspz7IJVECXw9WrWrSSs +Em3t0X2wktR6V8kuxMd9aPGltZYSBBwW/4Kcp+8FmaeJb74L+eQFulm76n0hjIsSGX3 n5TcqjqK4M7hDQJ+iTP+JM5vKVNVrisJ09Im2bEcGop8Rw0ALEcEslyaohETHUxErEo2 JsIBwMibcP89NylWmJ7wywhteTb+kyn/ZY7Sx4rwLHOVFBm5UkDwduNBwRGSo5v4oh3/ ZFjWKF+q9zz1iF+sEBtGH20W13TgpNOfcxWoQ3WPsvy1SXhcHKfT4r6s0OPgYVMxJ89U NMoA== X-Gm-Message-State: ANoB5pmfoovl8VJxvJtvvWhAZEVBc2J2GIWoPaxE2LtOLJvqe1zp3b2p +ytCqW5t2Moy9z38tKK4Q2OvF/mTJgSyCUYnZWlGLpEK1hhVUHZ/7evZeXJ7KE0jictmrHrZ/jv jmepA/0a6niSBKbLBxgcJqJ7UG04Z24SAEqpvasUc X-Received: by 2002:a05:6102:3354:b0:3a9:8207:bb1a with SMTP id j20-20020a056102335400b003a98207bb1amr24634196vse.58.1669772405269; Tue, 29 Nov 2022 17:40:05 -0800 (PST) X-Received: by 2002:a05:6102:3354:b0:3a9:8207:bb1a with SMTP id j20-20020a056102335400b003a98207bb1amr24634184vse.58.1669772404971; Tue, 29 Nov 2022 17:40:04 -0800 (PST) MIME-Version: 1.0 References: <20221125023045.2158413-1-lulu@redhat.com> In-Reply-To: From: Cindy Lu Date: Wed, 30 Nov 2022 09:39:27 +0800 Message-ID: Subject: Re: [PATCH v3] vhost_vdpa: fix the crash in unmap a large memory To: Jason Wang Cc: mst@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=unavailable 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 On Tue, 29 Nov 2022 at 11:04, Jason Wang wrote: > > On Fri, Nov 25, 2022 at 3:38 PM Cindy Lu wrote: > > > > / and > > > > > > On Fri, 25 Nov 2022 at 15:17, Jason Wang wrote: > > > > > > On Fri, Nov 25, 2022 at 10:31 AM Cindy Lu wrote: > > > > > > > > While testing in vIOMMU, sometimes guest will unmap very large memo= ry, > > > > 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. > > > > > > > > Call Trace: > > > > [ 647.820144] ------------[ cut here ]------------ > > > > [ 647.820848] kernel BUG at drivers/iommu/intel/iommu.c:1174! > > > > [ 647.821486] invalid opcode: 0000 [#1] PREEMPT SMP PTI > > > > [ 647.822082] CPU: 10 PID: 1181 Comm: qemu-system-x86 Not tainted = 6.0.0-rc1home_lulu_2452_lulu7_vhost+ #62 > > > > [ 647.823139] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), = BIOS rel-1.15.0-29-g6a62e0cb0dfe-prebuilt.qem4 > > > > [ 647.824365] RIP: 0010:domain_unmap+0x48/0x110 > > > > [ 647.825424] Code: 48 89 fb 8d 4c f6 1e 39 c1 0f 4f c8 83 e9 0c 8= 3 f9 3f 7f 18 48 89 e8 48 d3 e8 48 85 c0 75 59 > > > > [ 647.828064] RSP: 0018:ffffae5340c0bbf0 EFLAGS: 00010202 > > > > [ 647.828973] RAX: 0000000000000001 RBX: ffff921793d10540 RCX: 000= 000000000001b > > > > [ 647.830083] RDX: 00000000080000ff RSI: 0000000000000001 RDI: fff= f921793d10540 > > > > [ 647.831214] RBP: 0000000007fc0100 R08: ffffae5340c0bcd0 R09: 000= 0000000000003 > > > > [ 647.832388] R10: 0000007fc0100000 R11: 0000000000100000 R12: 000= 00000080000ff > > > > [ 647.833668] R13: ffffae5340c0bcd0 R14: ffff921793d10590 R15: 000= 0008000100000 > > > > [ 647.834782] FS: 00007f772ec90640(0000) GS:ffff921ce7a80000(0000= ) knlGS:0000000000000000 > > > > [ 647.836004] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > > > > [ 647.836990] CR2: 00007f02c27a3a20 CR3: 0000000101b0c006 CR4: 000= 0000000372ee0 > > > > [ 647.838107] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 000= 0000000000000 > > > > [ 647.839283] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 000= 0000000000400 > > > > [ 647.840666] Call Trace: > > > > [ 647.841437] > > > > [ 647.842107] intel_iommu_unmap_pages+0x93/0x140 > > > > [ 647.843112] __iommu_unmap+0x91/0x1b0 > > > > [ 647.844003] iommu_unmap+0x6a/0x95 > > > > [ 647.844885] vhost_vdpa_unmap+0x1de/0x1f0 [vhost_vdpa] > > > > [ 647.845985] vhost_vdpa_process_iotlb_msg+0xf0/0x90b [vhost_vdpa= ] > > > > [ 647.847235] ? _raw_spin_unlock+0x15/0x30 > > > > [ 647.848181] ? _copy_from_iter+0x8c/0x580 > > > > [ 647.849137] vhost_chr_write_iter+0xb3/0x430 [vhost] > > > > [ 647.850126] vfs_write+0x1e4/0x3a0 > > > > [ 647.850897] ksys_write+0x53/0xd0 > > > > [ 647.851688] do_syscall_64+0x3a/0x90 > > > > [ 647.852508] entry_SYSCALL_64_after_hwframe+0x63/0xcd > > > > [ 647.853457] RIP: 0033:0x7f7734ef9f4f > > > > [ 647.854408] Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 29 7= 6 f8 ff 48 8b 54 24 18 48 8b 74 24 10 41 89 c8 > > > > [ 647.857217] RSP: 002b:00007f772ec8f040 EFLAGS: 00000293 ORIG_RAX= : 0000000000000001 > > > > [ 647.858486] RAX: ffffffffffffffda RBX: 00000000fef00000 RCX: 000= 07f7734ef9f4f > > > > [ 647.859713] RDX: 0000000000000048 RSI: 00007f772ec8f090 RDI: 000= 0000000000010 > > > > [ 647.860942] RBP: 00007f772ec8f1a0 R08: 0000000000000000 R09: 000= 0000000000000 > > > > [ 647.862206] R10: 0000000000000001 R11: 0000000000000293 R12: 000= 0000000000010 > > > > [ 647.863446] R13: 0000000000000002 R14: 0000000000000000 R15: fff= fffff01100000 > > > > [ 647.864692] > > > > [ 647.865458] Modules linked in: rpcsec_gss_krb5 auth_rpcgss nfsv4= dns_resolver nfs lockd grace fscache netfs v] > > > > [ 647.874688] ---[ end trace 0000000000000000 ]--- > > > > [ 647.876013] RIP: 0010:domain_unmap+0x48/0x110 > > > > [ 647.878306] Code: 48 89 fb 8d 4c f6 1e 39 c1 0f 4f c8 83 e9 0c 8= 3 f9 3f 7f 18 48 89 e8 48 d3 e8 48 85 c0 75 59 > > > > [ 647.884581] RSP: 0018:ffffae5340c0bbf0 EFLAGS: 00010202 > > > > [ 647.886308] RAX: 0000000000000001 RBX: ffff921793d10540 RCX: 000= 000000000001b > > > > [ 647.888775] RDX: 00000000080000ff RSI: 0000000000000001 RDI: fff= f921793d10540 > > > > [ 647.890295] RBP: 0000000007fc0100 R08: ffffae5340c0bcd0 R09: 000= 0000000000003 > > > > [ 647.891660] R10: 0000007fc0100000 R11: 0000000000100000 R12: 000= 00000080000ff > > > > [ 647.893019] R13: ffffae5340c0bcd0 R14: ffff921793d10590 R15: 000= 0008000100000 > > > > [ 647.894506] FS: 00007f772ec90640(0000) GS:ffff921ce7a80000(0000= ) knlGS:0000000000000000 > > > > [ 647.895963] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > > > > [ 647.897348] CR2: 00007f02c27a3a20 CR3: 0000000101b0c006 CR4: 000= 0000000372ee0 > > > > [ 647.898719] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 000= 0000000000000 > > > > > > > > Cc: stable@vger.kernel.org > > > > Fixes: 4c8cf31885f6 ("vhost: introduce vDPA-based backend") > > > > 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..e5a07751bf45 100644 > > > > --- a/drivers/vhost/vdpa.c > > > > +++ b/drivers/vhost/vdpa.c > > > > @@ -692,6 +692,8 @@ static void vhost_vdpa_pa_unmap(struct vhost_vd= pa *v, > > > > struct vhost_iotlb_map *map; > > > > struct page *page; > > > > unsigned long pfn, pinned; > > > > + struct vdpa_device *vdpa =3D v->vdpa; > > > > + const struct vdpa_config_ops *ops =3D vdpa->config; > > > > > > > > while ((map =3D vhost_iotlb_itree_first(iotlb, start, last)= ) !=3D NULL) { > > > > pinned =3D PFN_DOWN(map->size); > > > > @@ -703,6 +705,8 @@ static void vhost_vdpa_pa_unmap(struct vhost_vd= pa *v, > > > > unpin_user_page(page); > > > > } > > > > atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_= vm); > > > > + if ((ops->dma_map =3D=3D NULL) && (ops->set_map =3D= =3D NULL)) > > > > + iommu_unmap(v->domain, map->start, map->siz= e); > > > > > > I think we'd better move the ops->dma_unmap() here as well as iommu_u= nmap()? > > > > > > > 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 =3D v->vdpa; > > > > + const struct vdpa_config_ops *ops =3D vdpa->config; > > > > > > > > while ((map =3D vhost_iotlb_itree_first(iotlb, start, last)= ) !=3D NULL) { > > > > map_file =3D (struct vdpa_map_file *)map->opaque; > > > > fput(map_file->file); > > > > kfree(map_file); > > > > + if (ops->set_map =3D=3D NULL) > > > > + iommu_unmap(v->domain, map->start, map->siz= e); > > > > > > Need to check where we have dma_unmap() and call that if it exists? > > > > > > Thanks > > > > > Hi Jason=EF=BC=8C > > I think these functions are called in vhost_vdpa_unmap, > > Do you want to separate the function in vhost_vdpa_unmap > > and move it to vhost_vdpa_va_unmap and vhost_vdpa_pa_unmap? I > > I meant dma_map()/dma_unmap() should be functional equivalent to > iommu_map/unmap(). That means we should unmap exactly what is mapped > before (vDPA parent may call iommu_unmap in its own dma_unmap() if it > needs). If we move the iommu_unmap() from vhost_vdpa_unmap() to > vhost_vdpa_{pa|va}_umap, we should move dma_unmap() as well. > > Thanks > Got it.thanks Jason, I will change this part Thanks Cindy > > thanks > > cindy > > > > > > 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 f= ree > > > > -- > > > > 2.34.3 > > > > > > > > > >