From patchwork Wed Apr 18 11:40:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133612 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5775901ljb; Wed, 18 Apr 2018 04:43:53 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+x8Os4IDnQ0pH5NQPpclRLmWzVmjXK8wIRMg1zTr1VY7NhPSgRUxbhRXDQ03x4QugPIWij X-Received: by 10.99.191.12 with SMTP id v12mr1504126pgf.54.1524051833757; Wed, 18 Apr 2018 04:43:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051833; cv=none; d=google.com; s=arc-20160816; b=p9w1Iju6P0Tk72jsMp3QVLtiHISiZNlD+cQvaOO5eTKkwgM5bxkLOIF3WY1tN3oueo qi+stnYUA6MXlEYY8ROmO8g+IZkopsDAV/gvZO1N/lh2eD16yHcc299SeJwVLga8JLQe W3B31hSPBDLS3IVAToHgy0oJhPsReSSzJE5cefvPBvrX4Ao0NBJEly09oeqc7kUHrqgU a7QogLK+fX81HI+BmSm+iF7ulBv4YDm8RZuF7zkbi8XpztXVhaSdlPXVu8Toy+QiHx50 kaGSpnXc6Z5BBITa9Uy81RioP2YZ+Y9aLRg7f1ohZb71rteHkElDpvVkt6xkIDl/Lry0 aB/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=cJbgjUlJHQAXl1eY14IkIp+m+j8j/SRga3qqMxP3B9Y=; b=O8ZstHKJ+RIeD+b9AbAFy5Y6ESWwRne5lBkvp591LHQdTjZugcBsEJyuwsiFEzIP5M tSkSooKgrXd0c+Ehrnwpuq2AuzkVM4+YmoHHNLhe48PvVKVtKEGIF/SWapaTnsbRj+Rf xmo+7v/p+bO9lQ1QdOepVHj0d2RN9GOxquoGL8hOzsZeGRg2VsnOg2H3qqxNwEAmkUki ECIpPO2V9K08h1JS/MeugvmtfcFCR+dnw0t7Q4dRdWk1CyZHH5iyoBMm6HriQO+oVFOf L2Pq3JJSrOP9YIWnSy1cygiqZxmQJAmt2KJOn/2ruRU+AxbmwdMdwu5BXMnyB7tEWeIN SzmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 91-v6si1084665ply.33.2018.04.18.04.43.53; Wed, 18 Apr 2018 04:43:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754014AbeDRLnt (ORCPT + 29 others); Wed, 18 Apr 2018 07:43:49 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42219 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753919AbeDRLnp (ORCPT ); Wed, 18 Apr 2018 07:43:45 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 1C1C0CEBE5155; Wed, 18 Apr 2018 19:43:41 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:36 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v6 1/7] vfio/type1: Introduce iova list and add iommu aperture validity check Date: Wed, 18 Apr 2018 12:40:39 +0100 Message-ID: <20180418114045.7968-2-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This introduces an iova list that is valid for dma mappings. Make sure the new iommu aperture window doesn't conflict with the current one or with any existing dma mappings during attach. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 183 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 180 insertions(+), 3 deletions(-) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 5c212bf..775946d 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -60,6 +60,7 @@ MODULE_PARM_DESC(disable_hugepages, struct vfio_iommu { struct list_head domain_list; + struct list_head iova_list; struct vfio_domain *external_domain; /* domain for external user */ struct mutex lock; struct rb_root dma_list; @@ -92,6 +93,12 @@ struct vfio_group { struct list_head next; }; +struct vfio_iova { + struct list_head list; + dma_addr_t start; + dma_addr_t end; +}; + /* * Guest RAM pinning working set or DMA target */ @@ -1313,6 +1320,149 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) return ret; } +/* + * This is a helper function to insert an address range to iova list. + * The list starts with a single entry corresponding to the IOMMU + * domain geometry to which the device group is attached. The list + * aperture gets modified when a new domain is added to the container + * if the new aperture doesn't conflict with the current one or with + * any existing dma mappings. The list is also modified to exclude + * any reserved regions associated with the device group. + */ +static int vfio_iommu_iova_insert(struct list_head *head, + dma_addr_t start, dma_addr_t end) +{ + struct vfio_iova *region; + + region = kmalloc(sizeof(*region), GFP_KERNEL); + if (!region) + return -ENOMEM; + + INIT_LIST_HEAD(®ion->list); + region->start = start; + region->end = end; + + list_add_tail(®ion->list, head); + return 0; +} + +/* + * Check the new iommu aperture conflicts with existing aper or with any + * existing dma mappings. + */ +static bool vfio_iommu_aper_conflict(struct vfio_iommu *iommu, + dma_addr_t start, dma_addr_t end) +{ + struct vfio_iova *first, *last; + struct list_head *iova = &iommu->iova_list; + + if (list_empty(iova)) + return false; + + /* Disjoint sets, return conflict */ + first = list_first_entry(iova, struct vfio_iova, list); + last = list_last_entry(iova, struct vfio_iova, list); + if ((start > last->end) || (end < first->start)) + return true; + + /* Check for any existing dma mappings outside the new start */ + if (start > first->start) { + if (vfio_find_dma(iommu, first->start, start - first->start)) + return true; + } + + /* Check for any existing dma mappings outside the new end */ + if (end < last->end) { + if (vfio_find_dma(iommu, end + 1, last->end - end)) + return true; + } + + return false; +} + +/* + * Resize iommu iova aperture window. This is called only if the new + * aperture has no conflict with existing aperture and dma mappings. + */ +static int vfio_iommu_aper_resize(struct list_head *iova, + dma_addr_t start, + dma_addr_t end) +{ + struct vfio_iova *node, *next; + + if (list_empty(iova)) + return vfio_iommu_iova_insert(iova, start, end); + + /* Adjust iova list start */ + list_for_each_entry_safe(node, next, iova, list) { + if (start < node->start) + break; + if ((start >= node->start) && (start < node->end)) { + node->start = start; + break; + } + /* Delete nodes before new start */ + list_del(&node->list); + kfree(node); + } + + /* Adjust iova list end */ + list_for_each_entry_safe(node, next, iova, list) { + if (end > node->end) + continue; + if ((end > node->start) && (end <= node->end)) { + node->end = end; + continue; + } + /* Delete nodes after new end */ + list_del(&node->list); + kfree(node); + } + + return 0; +} + +static void vfio_iommu_iova_free(struct list_head *iova) +{ + struct vfio_iova *n, *next; + + list_for_each_entry_safe(n, next, iova, list) { + list_del(&n->list); + kfree(n); + } +} + +static int vfio_iommu_iova_get_copy(struct vfio_iommu *iommu, + struct list_head *iova_copy) +{ + + struct list_head *iova = &iommu->iova_list; + struct vfio_iova *n; + int ret; + + list_for_each_entry(n, iova, list) { + ret = vfio_iommu_iova_insert(iova_copy, n->start, n->end); + if (ret) + goto out_free; + } + + return 0; + +out_free: + vfio_iommu_iova_free(iova_copy); + return ret; +} + +static void vfio_iommu_iova_insert_copy(struct vfio_iommu *iommu, + struct list_head *iova_copy) +{ + struct list_head *iova = &iommu->iova_list; + + vfio_iommu_iova_free(iova); + + list_splice_tail(iova_copy, iova); +} + static int vfio_iommu_type1_attach_group(void *iommu_data, struct iommu_group *iommu_group) { @@ -1323,6 +1473,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, int ret; bool resv_msi, msi_remap; phys_addr_t resv_msi_base; + struct iommu_domain_geometry geo; + LIST_HEAD(iova_copy); mutex_lock(&iommu->lock); @@ -1392,6 +1544,25 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (ret) goto out_domain; + /* Get aperture info */ + iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, &geo); + + if (vfio_iommu_aper_conflict(iommu, geo.aperture_start, + geo.aperture_end)) { + ret = -EINVAL; + goto out_detach; + } + + /* Get a copy of the current iova list and work on it */ + ret = vfio_iommu_iova_get_copy(iommu, &iova_copy); + if (ret) + goto out_detach; + + ret = vfio_iommu_aper_resize(&iova_copy, geo.aperture_start, + geo.aperture_end); + if (ret) + goto out_detach; + resv_msi = vfio_iommu_has_sw_msi(iommu_group, &resv_msi_base); INIT_LIST_HEAD(&domain->group_list); @@ -1425,8 +1596,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, list_add(&group->next, &d->group_list); iommu_domain_free(domain->domain); kfree(domain); - mutex_unlock(&iommu->lock); - return 0; + goto done; } ret = iommu_attach_group(domain->domain, iommu_group); @@ -1449,7 +1619,9 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, } list_add(&domain->next, &iommu->domain_list); - +done: + /* Delete the old one and insert new iova list */ + vfio_iommu_iova_insert_copy(iommu, &iova_copy); mutex_unlock(&iommu->lock); return 0; @@ -1458,6 +1630,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, iommu_detach_group(domain->domain, iommu_group); out_domain: iommu_domain_free(domain->domain); + vfio_iommu_iova_free(&iova_copy); out_free: kfree(domain); kfree(group); @@ -1596,6 +1769,7 @@ static void *vfio_iommu_type1_open(unsigned long arg) } INIT_LIST_HEAD(&iommu->domain_list); + INIT_LIST_HEAD(&iommu->iova_list); iommu->dma_list = RB_ROOT; mutex_init(&iommu->lock); BLOCKING_INIT_NOTIFIER_HEAD(&iommu->notifier); @@ -1638,6 +1812,9 @@ static void vfio_iommu_type1_release(void *iommu_data) list_del(&domain->next); kfree(domain); } + + vfio_iommu_iova_free(&iommu->iova_list); + kfree(iommu); } From patchwork Wed Apr 18 11:40:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133613 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5775931ljb; Wed, 18 Apr 2018 04:43:56 -0700 (PDT) X-Google-Smtp-Source: AIpwx49vPqN0s/hrPytW4uA0A+I3leM5fy5KxMYeX2IxihPJSovimbqPOzxAhEfLdAVWH4JcRScJ X-Received: by 2002:a17:902:28a6:: with SMTP id f35-v6mr1805604plb.204.1524051836027; Wed, 18 Apr 2018 04:43:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051836; cv=none; d=google.com; s=arc-20160816; b=ZorOnMMrkFduszHZDARcYx+aIp40jxo1Hp8rJp0ynRC2gjsKSNBcUIzS0IVRtgq0mF SKqdq5t7T/eJAcmYBr4f5AxGJIgWsevCq8QQlLRRK1Pur2l27+VZMcCu86CZGtCdkZZ0 5r1fymg94zFhEO7aasomiUY1gqoqYVpmAAPYp4XqTAL+ibM2W3SIP7NuJ/yCret2rJHL bYsTe2sJ/un3bbESJw8TXvU0wioxBFnp5dNT509RQCl58xBKEFWb5ZiAICOUpKUN0OJa Iz/RtSvoTFNsLzB4j7lKXPsQaLBkiALUmrSE7uooqOZmegz6OwCxEHgWn2aF44h08pcb 9LIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=uFKoFyV4lDM3oCNFaGkWQudDxU66OozteUP72VdthPA=; b=nLijKiB84qfgnIsuhJfr4X+kMeB3JT9w4aAz0t90u550EF2mjLpyVnkbPL0mTx2P0l Eh6uYiT2Au7lA8+BLmYmhEiNauxc+7zAkvIVekPZwpCwwz/NPuHIh8bi3jebqzOB//Ky 9b5sMq0Jsz7Nbq0Y3Lm/5chTTYHAiQPQrWR5yYoHI+0sPxpE27DF89SnqnmDDz6gJWbD m8XaHi1KNL2NZNQc6dSinHidQg+1yKGyacXeYvzGLJSOcywd1/7i9jQZ9c6iRMCXaGxL ybcK51Ok0mXiHCKWDJIucy0aPUooDYUIUw+mIoErHWwbVEXYtbu9TVRnPfoA6/8BgeXq vQJQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 91-v6si1084665ply.33.2018.04.18.04.43.55; Wed, 18 Apr 2018 04:43:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754030AbeDRLnv (ORCPT + 29 others); Wed, 18 Apr 2018 07:43:51 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42251 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753982AbeDRLnt (ORCPT ); Wed, 18 Apr 2018 07:43:49 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 21CE1564814C0; Wed, 18 Apr 2018 19:43:46 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:39 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v6 2/7] vfio/type1: Check reserve region conflict and update iova list Date: Wed, 18 Apr 2018 12:40:40 +0100 Message-ID: <20180418114045.7968-3-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This retrieves the reserved regions associated with dev group and checks for conflicts with any existing dma mappings. Also update the iova list excluding the reserved regions. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 90 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 775946d..a0a79e1 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1422,6 +1422,82 @@ static int vfio_iommu_aper_resize(struct list_head *iova, return 0; } +/* + * Check reserved region conflicts with existing dma mappings + */ +static bool vfio_iommu_resv_conflict(struct vfio_iommu *iommu, + struct list_head *resv_regions) +{ + struct iommu_resv_region *region; + + /* Check for conflict with existing dma mappings */ + list_for_each_entry(region, resv_regions, list) { + if (vfio_find_dma(iommu, region->start, region->length)) + return true; + } + + return false; +} + +/* + * Check iova region overlap with reserved regions and + * exclude them from the iommu iova range + */ +static int vfio_iommu_resv_exclude(struct list_head *iova, + struct list_head *resv_regions) +{ + struct iommu_resv_region *resv; + struct vfio_iova *n, *next; + + list_for_each_entry(resv, resv_regions, list) { + phys_addr_t start, end; + + start = resv->start; + end = resv->start + resv->length - 1; + + list_for_each_entry_safe(n, next, iova, list) { + int ret = 0; + + /* No overlap */ + if ((start > n->end) || (end < n->start)) + continue; + /* + * Insert a new node if current node overlaps with the + * reserve region to exlude that from valid iova range. + * Note that, new node is inserted before the current + * node and finally the current node is deleted keeping + * the list updated and sorted. + */ + if (start > n->start) + ret = vfio_iommu_iova_insert(&n->list, + n->start, start - 1); + if (!ret && end < n->end) + ret = vfio_iommu_iova_insert(&n->list, + end + 1, n->end); + if (ret) + return ret; + + list_del(&n->list); + kfree(n); + } + } + + if (list_empty(iova)) + return -EINVAL; + + return 0; +} + +static void vfio_iommu_resv_free(struct list_head *resv_regions) +{ + struct iommu_resv_region *n, *next; + + list_for_each_entry_safe(n, next, resv_regions, list) { + list_del(&n->list); + kfree(n); + } +} + static void vfio_iommu_iova_free(struct list_head *iova) { struct vfio_iova *n, *next; @@ -1475,6 +1551,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, phys_addr_t resv_msi_base; struct iommu_domain_geometry geo; LIST_HEAD(iova_copy); + LIST_HEAD(group_resv_regions); mutex_lock(&iommu->lock); @@ -1553,6 +1630,13 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_detach; } + iommu_get_group_resv_regions(iommu_group, &group_resv_regions); + + if (vfio_iommu_resv_conflict(iommu, &group_resv_regions)) { + ret = -EINVAL; + goto out_detach; + } + /* Get a copy of the current iova list and work on it */ ret = vfio_iommu_iova_get_copy(iommu, &iova_copy); if (ret) @@ -1563,6 +1647,10 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (ret) goto out_detach; + ret = vfio_iommu_resv_exclude(&iova_copy, &group_resv_regions); + if (ret) + goto out_detach; + resv_msi = vfio_iommu_has_sw_msi(iommu_group, &resv_msi_base); INIT_LIST_HEAD(&domain->group_list); @@ -1623,6 +1711,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, /* Delete the old one and insert new iova list */ vfio_iommu_iova_insert_copy(iommu, &iova_copy); mutex_unlock(&iommu->lock); + vfio_iommu_resv_free(&group_resv_regions); return 0; @@ -1631,6 +1720,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, out_domain: iommu_domain_free(domain->domain); vfio_iommu_iova_free(&iova_copy); + vfio_iommu_resv_free(&group_resv_regions); out_free: kfree(domain); kfree(group); From patchwork Wed Apr 18 11:40:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133617 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5776959ljb; Wed, 18 Apr 2018 04:45:05 -0700 (PDT) X-Google-Smtp-Source: AIpwx48+rBXphWpfn2mvikJgrJJWPMuxRq1pVJHTC3IRMk938P7rPVowkeGGt0WHrUj34MgbxVFR X-Received: by 10.99.115.4 with SMTP id o4mr1465754pgc.404.1524051905006; Wed, 18 Apr 2018 04:45:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051904; cv=none; d=google.com; s=arc-20160816; b=Df2XSIOuG2bfvliczVDi736QevGKEgdvy7ttvBAZ7jzgz9gBkenRRy3QZc4n5SJndz iqZ340KMP0zZaZOs2UucaI6vUL+f+TJXhfrrJYVqmyfPD7GJOLdW5belCtfnsZlh/Lw1 GtAXKI5dM91PDT2zfEm8wBcPy238b/FMhYwxVhXuB8dmMr6vdCzj/DklfZZGibYngaFX bYn0a3kO4S9+D/xZA7nq1QVeFo6FEERIFN8UB5gETmuGqQiDhRsaRaDQHgQCylNDKqC/ TrA0XFLAHqXcFu/hFeH2d0YDfZ1UqITGTbNM0BR6qNZm+YJH31o60nPtoic8AQXptqIT 0+aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=SYjanaawzcGEgWdgcA2R3Zvz6NW0+3MkJBe1QScEMcA=; b=UWEQkzRCXMi2jIZ8BGvrJntcJmKnLs7iprbqPlj73O8NbCabQB7v1tIgsYUkQ/hLrM NtnOmWV3fMxVl9v2IgLyv94tSyX8NpVtiXNWrOXogVLxj9wXOzzTlSFw5mKyIQgfBMol ChqqQQWEl9m1I8DP0+mDy3IoXZShi7Vkl17e0yJnV74LS6Uo3QRiNgmCK34J519x6Fcl sD3z2H7NHH+zvBTUmtI1L7SW24r8a3cnXuuckx7RAlW2ncidI2F0U93SZfpvRx8L9Nd2 o/2ub49sSzy6sv4REBXAtEf/BVxPBGuCUd7MKqoNzKk76OoUxkQDk1nVgy37wqAlUI5p gS1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z18si893782pgc.99.2018.04.18.04.45.04; Wed, 18 Apr 2018 04:45:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753993AbeDRLpC (ORCPT + 29 others); Wed, 18 Apr 2018 07:45:02 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42309 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753996AbeDRLny (ORCPT ); Wed, 18 Apr 2018 07:43:54 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 2F48A6C32EAE9; Wed, 18 Apr 2018 19:43:51 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:42 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v6 3/7] vfio/type1: Update iova list on detach Date: Wed, 18 Apr 2018 12:40:41 +0100 Message-ID: <20180418114045.7968-4-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Get a copy of iova list on _group_detach and try to update the list. On success replace the current one with the copy. Leave the list as it is if update fails. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 91 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index a0a79e1..6fd6841 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1776,12 +1776,88 @@ static void vfio_sanity_check_pfn_list(struct vfio_iommu *iommu) WARN_ON(iommu->notifier.head); } +/* + * Called when a domain is removed in detach. It is possible that + * the removed domain decided the iova aperture window. Modify the + * iova aperture with the smallest window among existing domains. + */ +static void vfio_iommu_aper_expand(struct vfio_iommu *iommu, + struct list_head *iova_copy) +{ + struct vfio_domain *domain; + struct iommu_domain_geometry geo; + struct vfio_iova *node; + dma_addr_t start = 0; + dma_addr_t end = (dma_addr_t)~0; + + list_for_each_entry(domain, &iommu->domain_list, next) { + iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, + &geo); + if (geo.aperture_start > start) + start = geo.aperture_start; + if (geo.aperture_end < end) + end = geo.aperture_end; + } + + /* Modify aperture limits. The new aper is either same or bigger */ + node = list_first_entry(iova_copy, struct vfio_iova, list); + node->start = start; + node = list_last_entry(iova_copy, struct vfio_iova, list); + node->end = end; +} + +/* + * Called when a group is detached. The reserved regions for that + * group can be part of valid iova now. But since reserved regions + * may be duplicated among groups, populate the iova valid regions + * list again. + */ +static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu, + struct list_head *iova_copy) +{ + struct vfio_domain *d; + struct vfio_group *g; + struct vfio_iova *node; + dma_addr_t start, end; + LIST_HEAD(resv_regions); + int ret; + + list_for_each_entry(d, &iommu->domain_list, next) { + list_for_each_entry(g, &d->group_list, next) + iommu_get_group_resv_regions(g->iommu_group, + &resv_regions); + } + + if (list_empty(&resv_regions)) + return 0; + + node = list_first_entry(iova_copy, struct vfio_iova, list); + start = node->start; + node = list_last_entry(iova_copy, struct vfio_iova, list); + end = node->end; + + /* purge the iova list and create new one */ + vfio_iommu_iova_free(iova_copy); + + ret = vfio_iommu_aper_resize(iova_copy, start, end); + if (ret) + goto done; + + /* Exclude current reserved regions from iova ranges */ + ret = vfio_iommu_resv_exclude(iova_copy, &resv_regions); +done: + vfio_iommu_resv_free(&resv_regions); + return ret; +} + static void vfio_iommu_type1_detach_group(void *iommu_data, struct iommu_group *iommu_group) { struct vfio_iommu *iommu = iommu_data; struct vfio_domain *domain; struct vfio_group *group; + bool iova_copy_fail; + LIST_HEAD(iova_copy); mutex_lock(&iommu->lock); @@ -1804,6 +1880,12 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, } } + /* + * Get a copy of iova list. If success, use copy to update the + * list and to replace the current one. + */ + iova_copy_fail = !!vfio_iommu_iova_get_copy(iommu, &iova_copy); + list_for_each_entry(domain, &iommu->domain_list, next) { group = find_iommu_group(domain, iommu_group); if (!group) @@ -1829,10 +1911,19 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, iommu_domain_free(domain->domain); list_del(&domain->next); kfree(domain); + if (!iova_copy_fail && !list_empty(&iommu->domain_list)) + vfio_iommu_aper_expand(iommu, &iova_copy); } break; } + if (!iova_copy_fail && !list_empty(&iommu->domain_list)) { + if (!vfio_iommu_resv_refresh(iommu, &iova_copy)) + vfio_iommu_iova_insert_copy(iommu, &iova_copy); + else + vfio_iommu_iova_free(&iova_copy); + } + detach_group_done: mutex_unlock(&iommu->lock); } From patchwork Wed Apr 18 11:40:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133618 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5777179ljb; Wed, 18 Apr 2018 04:45:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/x5CLKyTfU3MIDD4XHgAoVuw6PE0Jb+Vj8xMKn5iWPzFF0Vl/KzR9WFwvptrjZh9fTLKjs X-Received: by 2002:a17:902:b28:: with SMTP id 37-v6mr1760967plq.207.1524051922339; Wed, 18 Apr 2018 04:45:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051922; cv=none; d=google.com; s=arc-20160816; b=tI3rUyz1rxoeQjRDmCu+M9c1CKSTK9VTen0SwPezDW0YhJOpxH0eOWd7VhZobzmxKF S3ptabn1SVYjCjrU+7wp62nhpmgxgS3RIDQvTxshmcp9z1PyYHxtZYB6yK1OHiNfLeEe 3LU8IoBxZfG4rMq49mEDOoiYKIu53vhP0pt+wqV8hTm6K/JF7N1CQ1Co7O3S5j0e7lDu heb466++pxptDY9MrJajMAXyN+P5BVuQD/YA7AQqsRqgw/UVD6OUT29CPsJOkroYnORM c3yQynf1IJxv8Idsxim5mkij6vLMLbc3M8D/k7SwzG0hxTvHRzTeDNZdWiQ2TgwJaA3A Maog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=yv6c/8aiMpLlRtXrCgrgePvKTAsKrdqn2OzaUV5hnmU=; b=0Xg9EPCMbZK2dU0VQ/ZfMb2320F6MLNXzDPC+G1usUWeaHJwHWojOFu/hfhJ3gt0+k YnS7FT+fhgLp8PMzhtAyy7GtTQM4NaJRowFCttlzS6k5TjmQAAlpnm7k1HxQGjL/ZJ6l aa5oJrz9XUEhzzFPFiIZnoxKZXteGEUEh4jlcwGmdaBSllRcUC0YcXqvnl3IH+D7qK2u NPinPJ6YWt9Ocru4tC+9Hmj//Ctg3WoUzjB+Y6dN06go4BjWlt8dlnUrbHQnIZWIVm9F GgwTPApjbAN2BxRnag/nQUB7PBp5+muwCYnFlsCYxDWT1GSxlpi5AKrAqJop1+Cb1HlR EJCg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r9si951394pgv.119.2018.04.18.04.45.22; Wed, 18 Apr 2018 04:45:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753847AbeDRLpB (ORCPT + 29 others); Wed, 18 Apr 2018 07:45:01 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42314 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754032AbeDRLnz (ORCPT ); Wed, 18 Apr 2018 07:43:55 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 3F5FD38666FCB; Wed, 18 Apr 2018 19:43:51 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:45 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum , Joerg Roedel Subject: [PATCH v6 4/7] iommu/dma: Move PCI window region reservation back into dma specific path. Date: Wed, 18 Apr 2018 12:40:42 +0100 Message-ID: <20180418114045.7968-5-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This pretty much reverts commit 273df9635385 ("iommu/dma: Make PCI window reservation generic") by moving the PCI window region reservation back into the dma specific path so that these regions doesn't get exposed via the IOMMU API interface. With this change, the vfio interface will report only iommu specific reserved regions to the user space. Cc: Joerg Roedel Signed-off-by: Shameer Kolothum Reviewed-by: Robin Murphy --- drivers/iommu/dma-iommu.c | 54 ++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) -- 2.7.4 diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index f05f3cf..ddcbbdb 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -167,40 +167,16 @@ EXPORT_SYMBOL(iommu_put_dma_cookie); * @list: Reserved region list from iommu_get_resv_regions() * * IOMMU drivers can use this to implement their .get_resv_regions callback - * for general non-IOMMU-specific reservations. Currently, this covers host - * bridge windows for PCI devices and GICv3 ITS region reservation on ACPI - * based ARM platforms that may require HW MSI reservation. + * for general non-IOMMU-specific reservations. Currently, this covers GICv3 + * ITS region reservation on ACPI based ARM platforms that may require HW MSI + * reservation. */ void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list) { - struct pci_host_bridge *bridge; - struct resource_entry *window; - - if (!is_of_node(dev->iommu_fwspec->iommu_fwnode) && - iort_iommu_msi_get_resv_regions(dev, list) < 0) - return; - - if (!dev_is_pci(dev)) - return; - - bridge = pci_find_host_bridge(to_pci_dev(dev)->bus); - resource_list_for_each_entry(window, &bridge->windows) { - struct iommu_resv_region *region; - phys_addr_t start; - size_t length; - - if (resource_type(window->res) != IORESOURCE_MEM) - continue; - start = window->res->start - window->offset; - length = window->res->end - window->res->start + 1; - region = iommu_alloc_resv_region(start, length, 0, - IOMMU_RESV_RESERVED); - if (!region) - return; + if (!is_of_node(dev->iommu_fwspec->iommu_fwnode)) + iort_iommu_msi_get_resv_regions(dev, list); - list_add_tail(®ion->list, list); - } } EXPORT_SYMBOL(iommu_dma_get_resv_regions); @@ -229,6 +205,23 @@ static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie, return 0; } +static void iova_reserve_pci_windows(struct pci_dev *dev, + struct iova_domain *iovad) +{ + struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus); + struct resource_entry *window; + unsigned long lo, hi; + + resource_list_for_each_entry(window, &bridge->windows) { + if (resource_type(window->res) != IORESOURCE_MEM) + continue; + + lo = iova_pfn(iovad, window->res->start - window->offset); + hi = iova_pfn(iovad, window->res->end - window->offset); + reserve_iova(iovad, lo, hi); + } +} + static int iova_reserve_iommu_regions(struct device *dev, struct iommu_domain *domain) { @@ -238,6 +231,9 @@ static int iova_reserve_iommu_regions(struct device *dev, LIST_HEAD(resv_regions); int ret = 0; + if (dev_is_pci(dev)) + iova_reserve_pci_windows(to_pci_dev(dev), iovad); + iommu_get_resv_regions(dev, &resv_regions); list_for_each_entry(region, &resv_regions, list) { unsigned long lo, hi; From patchwork Wed Apr 18 11:40:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133615 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5776294ljb; Wed, 18 Apr 2018 04:44:16 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+1wvwIRlWtjfr4bH8bks8Az8ToJvbJq/avF0WmZemk51HcrXWyTYpOiMysPO7xCdglz87F X-Received: by 10.101.77.145 with SMTP id p17mr1484014pgq.275.1524051856371; Wed, 18 Apr 2018 04:44:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051856; cv=none; d=google.com; s=arc-20160816; b=P7zxDs9C6tER2jTUQwPuwFAjQxeQmImVr2OM9fYQq4iawamEaOSWM9jKRVpVDq/FA4 tmcXnz4fMYSfUALQ0OG0szp+n/MFSNQ1M1mYMO4enLacpbKD0UiGmL57CsUw2So/JBwD jhIxmxiZk862VpcvpCuOzUrFTTUExPWojXRu5gDONrzaReHuPTOktYR/nAeitIVMTZUq 7XT9cXHvVIhjj0AQvUAzzMmwsbjcgwWvW9hIROS9x844ZI821iMNVeSO6HO4onjZTSxz 7+6lTB+1lTgnlpr8W7FXU7e98RHHiQodPcz9oCFx6ZUJ4+m8WEkICvUbdTbyw3EQ7a8W itgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=UM2lyCnUqJxsXLV3vRdUsI1uIb3I4qlUopzMyO1KhyQ=; b=mevi4qkh7cez2CJI24u5pO1iYCy/kcdZ/s5wqy2jadnqQ2dFmEC2kMZHDSCkBCYmPU da25P67r9CnjIJQdwncn/DUe5FgKbuy2VsgDOynQcJ5D+kRKEjNEPgeQGOkciZayOAig b05PG28imnp1czQUM+q5PSbSq+LcrzAiUFI3HRwhfIuSNCwdTOLR7vwUISP7yVeTvKjr U0ITxK3nH4w9Mf//4PIE9bidGKYZ19SGnyQAG7uQbNxpPMHFVY6AH3JfQEZSV9nTNz/3 xl8Yk3Ued1wW+h4LMyiAAnL9RgQNRoWgKPXZzS1bNbVQ7gkLEzac3IyIFVz7dQkbjLrt +viw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f3-v6si1092917plf.446.2018.04.18.04.44.16; Wed, 18 Apr 2018 04:44:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754096AbeDRLoN (ORCPT + 29 others); Wed, 18 Apr 2018 07:44:13 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:7179 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754070AbeDRLoK (ORCPT ); Wed, 18 Apr 2018 07:44:10 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 64398AB964606; Wed, 18 Apr 2018 19:43:56 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:49 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v6 5/7] vfio/type1: check dma map request is within a valid iova range Date: Wed, 18 Apr 2018 12:40:43 +0100 Message-ID: <20180418114045.7968-6-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This checks and rejects any dma map request outside valid iova range. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 6fd6841..bf33281 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1091,6 +1091,23 @@ static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma, return ret; } +/* + * Check dma map request is within a valid iova range + */ +static bool vfio_iommu_iova_dma_valid(struct vfio_iommu *iommu, + dma_addr_t start, dma_addr_t end) +{ + struct list_head *iova = &iommu->iova_list; + struct vfio_iova *node; + + list_for_each_entry(node, iova, list) { + if ((start >= node->start) && (end <= node->end)) + return true; + } + + return false; +} + static int vfio_dma_do_map(struct vfio_iommu *iommu, struct vfio_iommu_type1_dma_map *map) { @@ -1129,6 +1146,11 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto out_unlock; } + if (!vfio_iommu_iova_dma_valid(iommu, iova, iova + size - 1)) { + ret = -EINVAL; + goto out_unlock; + } + dma = kzalloc(sizeof(*dma), GFP_KERNEL); if (!dma) { ret = -ENOMEM; From patchwork Wed Apr 18 11:40:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133616 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5776604ljb; Wed, 18 Apr 2018 04:44:40 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+cHn3V1OIml0frAlv+e0OdqAX05v9yX7RdolP2IyFUroqPFumktDcBkTETdSo+q0QEm6Ud X-Received: by 10.98.96.195 with SMTP id u186mr1680368pfb.81.1524051880429; Wed, 18 Apr 2018 04:44:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051880; cv=none; d=google.com; s=arc-20160816; b=J7aOwHfX51LaNxQp4zY2OksMYVTaRru417Caa9qWQOrbGZqwaRkMzYfWXZ8ezUT4a+ jHVWExzXtnORuyBsCR36EVggl4jc9SsdkXH0sZoeG2aJsOIqZuITi5C0J6/5Qonei0s+ jCdNBkAGjQWErCcUfqRRgetCSrQvl0Ja+axklRlnLU5de3d6lDIfWS1Q+qPxJRbSPIfi +dhwFzpI4bSs/53RluemsViWUKffMy/YPNkeqc2vktGrPNWkbYJFG96fyigs7F9uKwNm SVGJL7dLSh/VtnyouPOin7VcmOayy/F/fMFZv9fYu39+JCKEPbEjA1C2vNjnwQ+Kt1ml qzUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=VVfLB1PyUpDTfX9VWnM/vPcho9VkcOBzTJYS5CtqkUA=; b=nSieLMuHXxLZ1p4PR3C2OWKRkn3QhK0TvVI5sDZgwP0hEPQDpnseVxu8DJ8PfCJ2mP AmbaU0bEsR9Hpd6zZQoqDfYwUwfphgBEraPn+84jgUvAYH4kbZgQf9A4F4O/rB/ETBuR T4EHVDpEemdLyqZ6C4iufDtUfmrRi6q2TjSm7VPFgifPHwB847mHIZ0GvDiAOqa104mt ISz0HRQs7sn3SqVurWCpDjPMROd1N3j3Uml7cBqDfoQCmE02u10TxsPiY1VUxzOsrScJ HKwxaqc1jN1fUPDIAGW6fSHGdIECIOVRnHNM4ZXVdaxARgwzKisSQcnxnoFTNJEi5EG+ Dcbw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f3-v6si1092917plf.446.2018.04.18.04.44.40; Wed, 18 Apr 2018 04:44:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754068AbeDRLoI (ORCPT + 29 others); Wed, 18 Apr 2018 07:44:08 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42344 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753250AbeDRLoF (ORCPT ); Wed, 18 Apr 2018 07:44:05 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 4EF8D27A3F74B; Wed, 18 Apr 2018 19:44:01 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:52 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v6 6/7] vfio/type1: Add IOVA range capability support Date: Wed, 18 Apr 2018 12:40:44 +0100 Message-ID: <20180418114045.7968-7-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This allows the user-space to retrieve the supported IOVA range(s), excluding any reserved regions. The implementation is based on capability chains, added to VFIO_IOMMU_GET_INFO ioctl. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 96 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 23 ++++++++++ 2 files changed, 119 insertions(+) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index bf33281..44d0f13d 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2038,6 +2038,68 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu) return ret; } +static int vfio_iommu_iova_add_cap(struct vfio_info_cap *caps, + struct vfio_iommu_type1_info_cap_iova_range *cap_iovas, + size_t size) +{ + struct vfio_info_cap_header *header; + struct vfio_iommu_type1_info_cap_iova_range *iova_cap; + + header = vfio_info_cap_add(caps, size, + VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE, 1); + if (IS_ERR(header)) + return PTR_ERR(header); + + iova_cap = container_of(header, + struct vfio_iommu_type1_info_cap_iova_range, header); + iova_cap->nr_iovas = cap_iovas->nr_iovas; + memcpy(iova_cap->iova_ranges, cap_iovas->iova_ranges, + cap_iovas->nr_iovas * sizeof(*cap_iovas->iova_ranges)); + return 0; +} + +static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu, + struct vfio_info_cap *caps) +{ + struct vfio_iommu_type1_info_cap_iova_range *cap_iovas; + struct vfio_iova *iova; + size_t size; + int iovas = 0, i = 0, ret; + + mutex_lock(&iommu->lock); + + list_for_each_entry(iova, &iommu->iova_list, list) + iovas++; + + if (!iovas) { + ret = -EINVAL; + goto out_unlock; + } + + size = sizeof(*cap_iovas) + (iovas * sizeof(*cap_iovas->iova_ranges)); + + cap_iovas = kzalloc(size, GFP_KERNEL); + if (!cap_iovas) { + ret = -ENOMEM; + goto out_unlock; + } + + cap_iovas->nr_iovas = iovas; + + list_for_each_entry(iova, &iommu->iova_list, list) { + cap_iovas->iova_ranges[i].start = iova->start; + cap_iovas->iova_ranges[i].end = iova->end; + i++; + } + + ret = vfio_iommu_iova_add_cap(caps, cap_iovas, size); + + kfree(cap_iovas); +out_unlock: + mutex_unlock(&iommu->lock); + return ret; +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { @@ -2059,19 +2121,53 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, } } else if (cmd == VFIO_IOMMU_GET_INFO) { struct vfio_iommu_type1_info info; + struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; + unsigned long capsz; + int ret; minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes); + /* For backward compatibility, cannot require this */ + capsz = offsetofend(struct vfio_iommu_type1_info, cap_offset); + if (copy_from_user(&info, (void __user *)arg, minsz)) return -EFAULT; if (info.argsz < minsz) return -EINVAL; + if (info.argsz >= capsz) { + minsz = capsz; + info.cap_offset = 0; /* output, no-recopy necessary */ + } + info.flags = VFIO_IOMMU_INFO_PGSIZES; info.iova_pgsizes = vfio_pgsize_bitmap(iommu); + ret = vfio_iommu_iova_build_caps(iommu, &caps); + if (ret) + return ret; + + if (caps.size) { + info.flags |= VFIO_IOMMU_INFO_CAPS; + + if (info.argsz < sizeof(info) + caps.size) { + info.argsz = sizeof(info) + caps.size; + } else { + vfio_info_cap_shift(&caps, sizeof(info)); + if (copy_to_user((void __user *)arg + + sizeof(info), caps.buf, + caps.size)) { + kfree(caps.buf); + return -EFAULT; + } + info.cap_offset = sizeof(info); + } + + kfree(caps.buf); + } + return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 1aa7b82..f1bf6b7 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -616,7 +616,30 @@ struct vfio_iommu_type1_info { __u32 argsz; __u32 flags; #define VFIO_IOMMU_INFO_PGSIZES (1 << 0) /* supported page sizes info */ +#define VFIO_IOMMU_INFO_CAPS (1 << 1) /* Info supports caps */ __u64 iova_pgsizes; /* Bitmap of supported page sizes */ + __u32 cap_offset; /* Offset within info struct of first cap */ +}; + +/* + * The IOVA capability allows to report the valid IOVA range(s) + * excluding any reserved regions associated with dev group. Any dma + * map attempt outside the valid iova range will return error. + * + * The structures below define version 1 of this capability. + */ +#define VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE 1 + +struct vfio_iova_range { + __u64 start; + __u64 end; +}; + +struct vfio_iommu_type1_info_cap_iova_range { + struct vfio_info_cap_header header; + __u32 nr_iovas; + __u32 reserved; + struct vfio_iova_range iova_ranges[]; }; #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12) From patchwork Wed Apr 18 11:40:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 133614 Delivered-To: patch@linaro.org Received: by 10.46.84.18 with SMTP id i18csp5776249ljb; Wed, 18 Apr 2018 04:44:14 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+cVEE2gHhVePtnKCrWdXAZjmNhO+Uml1nyhwY+UynER5bOmuVvfcCSXC8jL1UD0doLp3z0 X-Received: by 10.99.95.210 with SMTP id t201mr1505900pgb.315.1524051854134; Wed, 18 Apr 2018 04:44:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524051854; cv=none; d=google.com; s=arc-20160816; b=P4zra1XlWZ/E0AYVulQ4aHTcitJ+1SiHAsY6x3d0AaYlCfz9HaIT1CIdUqLlVCMINH TNdJrFUe/rWOQ/DQhvGT1PTUXmpDYPMJ0vBGHV+lFGOx0Qj/J4wLTHzw7RQkoEM5A+MY lJH2jdYgRuUd48WO01c9bnaWds4TMxgHBZ/kbdgC6nz8r5l5DX9eSn06DwdY3RWMUkB7 2NK/+de7SFFYrNLjtAjZi2HBX7qw35hwOPHvmn+N8p1DlsTp46P4xcZoZJgsCAytojMc s+IY/NJlK8ussKruViy9O+htJisTtYMlw4mEdONGos9BWVObXxRhVpjRjer6Va3Pg+Sh /bhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=0nNGlb+6cBOrRH02UmRQ/EYDC54UAiNj6weVrcAeIfw=; b=GjMsZIC8wHpoYde7KNQmLH3TgxNDoiVIIItcieaSUk/lHB79xB0Q1PftOtwx9z9WXY kZgOtjsl88b330ImkR25gRZPzcKslV6B2wpRug8MOaz0dfjY/NV9h6Hwe/ag6nh71sPr 0vo/EBiyFGsNomcnRsQFV+CCFZh6N6sUl5HA/BkuRX2uGT/rKzDf697KF1WJ93Lk1uSq W3IXN7JTRbs/t+nF4TfIkZ8aHfWiaAZlX7SbMNttChakteg8vjCx1KMmoOx7EeB8A/dm wgp11sUNDIgCg3pmcxH+GiKIzDcRO3wKJ/pBggDypT9+rJ7h10bOJVze5NMMiTbmgRgs P2UQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v31-v6si1118845plg.157.2018.04.18.04.44.13; Wed, 18 Apr 2018 04:44:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754082AbeDRLoK (ORCPT + 29 others); Wed, 18 Apr 2018 07:44:10 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42340 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753566AbeDRLoF (ORCPT ); Wed, 18 Apr 2018 07:44:05 -0400 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 5F95137BB648A; Wed, 18 Apr 2018 19:44:01 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Wed, 18 Apr 2018 19:43:55 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v6 7/7] vfio/type1: remove duplicate retrieval of reserved regions Date: Wed, 18 Apr 2018 12:40:45 +0100 Message-ID: <20180418114045.7968-8-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> References: <20180418114045.7968-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As we now already have the reserved regions list, just pass that into vfio_iommu_has_sw_msi() fn. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 44d0f13d..13c631a 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1313,15 +1313,13 @@ static struct vfio_group *find_iommu_group(struct vfio_domain *domain, return NULL; } -static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) +static bool vfio_iommu_has_sw_msi(struct list_head *group_resv_regions, + phys_addr_t *base) { - struct list_head group_resv_regions; - struct iommu_resv_region *region, *next; + struct iommu_resv_region *region; bool ret = false; - INIT_LIST_HEAD(&group_resv_regions); - iommu_get_group_resv_regions(group, &group_resv_regions); - list_for_each_entry(region, &group_resv_regions, list) { + list_for_each_entry(region, group_resv_regions, list) { /* * The presence of any 'real' MSI regions should take * precedence over the software-managed one if the @@ -1337,8 +1335,7 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) ret = true; } } - list_for_each_entry_safe(region, next, &group_resv_regions, list) - kfree(region); + return ret; } @@ -1673,7 +1670,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (ret) goto out_detach; - resv_msi = vfio_iommu_has_sw_msi(iommu_group, &resv_msi_base); + resv_msi = vfio_iommu_has_sw_msi(&group_resv_regions, &resv_msi_base); INIT_LIST_HEAD(&domain->group_list); list_add(&group->next, &domain->group_list);