Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2122026rwd; Fri, 2 Jun 2023 05:18:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6j8OIxSa5km7XmfdsyehW8dJJIF7yaUb/CWx+oLt27W0rnIlqztmnn9IYHUSl6+EWaquW1 X-Received: by 2002:a05:6a00:148b:b0:64f:aea5:7b49 with SMTP id v11-20020a056a00148b00b0064faea57b49mr10446075pfu.17.1685708291256; Fri, 02 Jun 2023 05:18:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685708291; cv=none; d=google.com; s=arc-20160816; b=vS9DDiM3ufxhh0PntPJ/035qORnIiDAabXvneIfWoN39tHBm3wKJHbbDIu+G6DTdK7 3bWl0OVC1SkCUcVWdrc2BTejlKiP7IocakmDBmedaGbqHo4cYNKUrt4H8SoCQJ9T49Ip IwRv2pUhPM3pzSG+SPP0QNim54is9RQtUwO3QOP9Y0ozFaestuT7CHgVzqUlvpyq8MZm MIfbLjOx8N3WdprSxiiXDfYkW8Y8AoXvm9u51P9udmimFNY/A4U3sqeBxOHo31R6qLy9 oOTijL7+wAv5TpLc8772LfbqyGY5pf9to65p1n/WWlL8inVZ6gYMa7FgWk7qsrM5pop7 y69g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jBqXisssxSy08laXqA4j2X5BRqq3rkPi2c6BRIFiiB0=; b=Po22TDIe6Xk+ZcuQvZfx4eMuj/W5GJukibbDIOL0rD0sWqv/36bkdB+dZFnFmHI7Xg /464CU/+DzIVFIXkHdOku0ZLbjBSFWSp1VbIWyIBAaUlj8S1AK28UR1GMe9qc4W38LcQ ekdjHVndvj0/8oYumzTYlYjE9ZNuPHzF27+/64Kx5sR4Yatiqx8zCIj37deEHnRymBch erP8f1D/8rBaSkB9JtVp5aneP/MPgaLqRwz7O9cj7cWmyk0GWKTZR2up0kzsBMKIM6cl xN1DaBbd6AVtUREvnb6kY8uYFAUEGVqy4bnwm1OyeK+/A0BRRmgkswZ/moSbiW/v3d5W 8RYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KFqcnBdk; 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 l21-20020a637015000000b0053fb768afc8si927738pgc.748.2023.06.02.05.17.36; Fri, 02 Jun 2023 05:18:11 -0700 (PDT) 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=KFqcnBdk; 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 S235724AbjFBMM1 (ORCPT + 99 others); Fri, 2 Jun 2023 08:12:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235501AbjFBMMZ (ORCPT ); Fri, 2 Jun 2023 08:12:25 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEFAEE7 for ; Fri, 2 Jun 2023 05:11:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685707903; 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: in-reply-to:in-reply-to:references:references; bh=jBqXisssxSy08laXqA4j2X5BRqq3rkPi2c6BRIFiiB0=; b=KFqcnBdkwa6+ko4XDv7sAQtk/OJN4blu07FMGbvPN/wyvqtdVib8Fxzrg7WbSl9UsY+1pW 4W/50jYS9RwDzGX+jUZcUcyQyDmXcYbNZpz9UGoZaziMBuYylv5r8h5H5inxExz2eKLLnS 7FclH0fyuvBvLvH3PG9tnLXlwNi+4wQ= Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-518-zzyjqPU2PbSyqV8Pkow48A-1; Fri, 02 Jun 2023 08:11:42 -0400 X-MC-Unique: zzyjqPU2PbSyqV8Pkow48A-1 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-4f3bb9c5b2bso1489998e87.0 for ; Fri, 02 Jun 2023 05:11:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685707901; x=1688299901; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=jBqXisssxSy08laXqA4j2X5BRqq3rkPi2c6BRIFiiB0=; b=MsjD5/Y6sDOo3kTn5kmllvUbEM16IXCcNV+3WtS0TL3qJf40eupOuFtYcAkhxnh3xo nQ0S9aAhi4Y5T/pLWemZg5qZKVEdAs2BfQkzeAta8NGU6CHridgn/EflIIXeQmuqu8/P of7HY5hTMBpCKXBr6nLz7tkhGCmJf54Wr/3caG1vKKoaXh9IB+w+Pl4bTTEmktjrhdf+ CU1h3aeZnweHoIRlG53MvBWxaOaforlMyAKqBVFwcIpIBrzveKOmF/kvvWM33+lN7QLJ Q4svC+d+b3RWj+x872xaKYpSdbK8NbHITPwo5ysxf+ZWm5bc6v17sT/hzmFPmCJcfkeJ cM5g== X-Gm-Message-State: AC+VfDzx08kbX/984i6DBiI+f9Fd2MRHgV6o+8bart+OYAj26jYoUu9V bEoFDi9Rrz+cQVtsahGvZaoora1qP3qJtD8cG/TlGw7QM2MxDfg0bXJSOKXAUgRLKInbSeeEumF ByKyg+J3X67jEtVo9/TZtpHNT X-Received: by 2002:ac2:5d49:0:b0:4f3:b242:aa90 with SMTP id w9-20020ac25d49000000b004f3b242aa90mr1515708lfd.35.1685707901330; Fri, 02 Jun 2023 05:11:41 -0700 (PDT) X-Received: by 2002:ac2:5d49:0:b0:4f3:b242:aa90 with SMTP id w9-20020ac25d49000000b004f3b242aa90mr1515704lfd.35.1685707900929; Fri, 02 Jun 2023 05:11:40 -0700 (PDT) Received: from redhat.com ([2.55.4.169]) by smtp.gmail.com with ESMTPSA id n2-20020a5d4c42000000b00306415ac69asm1519978wrt.15.2023.06.02.05.11.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 05:11:40 -0700 (PDT) Date: Fri, 2 Jun 2023 08:11:36 -0400 From: "Michael S. Tsirkin" To: Nanyong Sun Cc: joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, jasowang@redhat.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, wangrong68@huawei.com Subject: Re: [PATCH v2] vhost/vdpa: Add MSI translation tables to iommu for software-managed MSI Message-ID: <20230602081114-mutt-send-email-mst@kernel.org> References: <20230207120843.1580403-1-sunnanyong@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230207120843.1580403-1-sunnanyong@huawei.com> X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 On Tue, Feb 07, 2023 at 08:08:43PM +0800, Nanyong Sun wrote: > From: Rong Wang > > Once enable iommu domain for one device, the MSI > translation tables have to be there for software-managed MSI. > Otherwise, platform with software-managed MSI without an > irq bypass function, can not get a correct memory write event > from pcie, will not get irqs. > The solution is to obtain the MSI phy base address from > iommu reserved region, and set it to iommu MSI cookie, > then translation tables will be created while request irq. OK this one seems to be going nowhere I untagged it. > Change log > ---------- > > v1->v2: > - add resv iotlb to avoid overlap mapping. > > Signed-off-by: Rong Wang > Signed-off-by: Nanyong Sun > --- > drivers/iommu/iommu.c | 1 + > drivers/vhost/vdpa.c | 59 ++++++++++++++++++++++++++++++++++++++++--- > 2 files changed, 57 insertions(+), 3 deletions(-) > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c > index 5f6a85aea501..af9c064ad8b2 100644 > --- a/drivers/iommu/iommu.c > +++ b/drivers/iommu/iommu.c > @@ -2623,6 +2623,7 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) > if (ops->get_resv_regions) > ops->get_resv_regions(dev, list); > } > +EXPORT_SYMBOL(iommu_get_resv_regions); > > /** > * iommu_put_resv_regions - release resered regions > diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c > index ec32f785dfde..a58979da8acd 100644 > --- a/drivers/vhost/vdpa.c > +++ b/drivers/vhost/vdpa.c > @@ -49,6 +49,7 @@ struct vhost_vdpa { > struct completion completion; > struct vdpa_device *vdpa; > struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS]; > + struct vhost_iotlb resv_iotlb; > struct device dev; > struct cdev cdev; > atomic_t opened; > @@ -216,6 +217,8 @@ static int vhost_vdpa_reset(struct vhost_vdpa *v) > > v->in_batch = 0; > > + vhost_iotlb_reset(&v->resv_iotlb); > + > return vdpa_reset(vdpa); > } > > @@ -1013,6 +1016,10 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, > msg->iova + msg->size - 1 > v->range.last) > return -EINVAL; > > + if (vhost_iotlb_itree_first(&v->resv_iotlb, msg->iova, > + msg->iova + msg->size - 1)) > + return -EINVAL; > + > if (vhost_iotlb_itree_first(iotlb, msg->iova, > msg->iova + msg->size - 1)) > return -EEXIST; > @@ -1103,6 +1110,45 @@ static ssize_t vhost_vdpa_chr_write_iter(struct kiocb *iocb, > return vhost_chr_write_iter(dev, from); > } > > +static int vhost_vdpa_resv_iommu_region(struct iommu_domain *domain, struct device *dma_dev, > + struct vhost_iotlb *resv_iotlb) > +{ > + struct list_head dev_resv_regions; > + phys_addr_t resv_msi_base = 0; > + struct iommu_resv_region *region; > + int ret = 0; > + bool with_sw_msi = false; > + bool with_hw_msi = false; > + > + INIT_LIST_HEAD(&dev_resv_regions); > + iommu_get_resv_regions(dma_dev, &dev_resv_regions); > + > + list_for_each_entry(region, &dev_resv_regions, list) { > + ret = vhost_iotlb_add_range_ctx(resv_iotlb, region->start, > + region->start + region->length - 1, > + 0, 0, NULL); > + if (ret) { > + vhost_iotlb_reset(resv_iotlb); > + break; > + } > + > + if (region->type == IOMMU_RESV_MSI) > + with_hw_msi = true; > + > + if (region->type == IOMMU_RESV_SW_MSI) { > + resv_msi_base = region->start; > + with_sw_msi = true; > + } > + } > + > + if (!ret && !with_hw_msi && with_sw_msi) > + ret = iommu_get_msi_cookie(domain, resv_msi_base); > + > + iommu_put_resv_regions(dma_dev, &dev_resv_regions); > + > + return ret; > +} > + > static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v) > { > struct vdpa_device *vdpa = v->vdpa; > @@ -1128,11 +1174,16 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v) > > ret = iommu_attach_device(v->domain, dma_dev); > if (ret) > - goto err_attach; > + goto err_alloc_domain; > > - return 0; > + ret = vhost_vdpa_resv_iommu_region(v->domain, dma_dev, &v->resv_iotlb); > + if (ret) > + goto err_attach_device; > > -err_attach: > + return 0; > +err_attach_device: > + iommu_detach_device(v->domain, dma_dev); > +err_alloc_domain: > iommu_domain_free(v->domain); > return ret; > } > @@ -1385,6 +1436,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa) > goto err; > } > > + vhost_iotlb_init(&v->resv_iotlb, 0, 0); > + > r = dev_set_name(&v->dev, "vhost-vdpa-%u", minor); > if (r) > goto err; > -- > 2.25.1