From patchwork Wed Feb 21 12:22:04 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: 129058 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp562110ljc; Wed, 21 Feb 2018 04:24:36 -0800 (PST) X-Google-Smtp-Source: AH8x227ZdyGtCj57qGCd12WCmuuEtvKG8YKCjEQRo0FdPCoJuSonVf+JCUai2YegT3+Xn0/LS4Jt X-Received: by 2002:a17:902:2006:: with SMTP id n6-v6mr2993541pla.149.1519215876728; Wed, 21 Feb 2018 04:24:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215876; cv=none; d=google.com; s=arc-20160816; b=pdGyPqqagiRZwUxYNG19+H7D0NFinXNFSDVF0OcsPWW+mo1bVhJwxGXTZccW8aJj62 nNrgyaV/f2F0tZVwFTOgEk7PmtmIJL++KkgA2JmneAwZvD++19WBja28Y10RTtvH0/yd ROHqB7vO7Q5sv53dBFBgp6l3HpTlHBkhHtD7oOzkaa0sobrTKdwVx1AML60jOxdd2asm I/U1JgdRBiHRTxaHS64vHy3sWY39gQUQ/w7gpk7USgsWMK+e0Iya0XlHtc5oTm96T13m M6nsBaAtnBKSunOOMEk4v5W+52MD/5IMoW3nUBs6zLutyp193o5ThAhmSWok7Jfcb95m nRsg== 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=7IiRlOmivD2W5GkXFDLmgEJyrb1YA6mfb29IOKdoa2w=; b=HrHEgRsAvPhFbEWkgjfmfXO0Ki1YsGYb4LgyEoaM533JP+xQmqoDJapXr0S617/+cS 7rvWPIyQ+Yl/zSnRfgyPVDtbXye2VQCL4Jh4jGLHtYeRBOWRlm6pLmrF/ryDHAwggV7w QSnuDWYbir2DPNJLfauNUDzWhw1nEJPO/MhpT2EnNUvpj4lyi8qUwVJ6gHCD9QnlGIMm lyGryhg7epC6KAzhdd4c185FtK84LmxwQT79bmB8l8VMWk25TbhOHxhUPoHNNnhAkmgL 6vqWTqCOpbsHT3sNIQ1vvjGAfqz8Z5+cWEzYLk/7qvGWh0RUiFVGNi5aGaN0vRAJGpdo c7qA== 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 u6-v6si9168144pld.31.2018.02.21.04.24.36; Wed, 21 Feb 2018 04:24:36 -0800 (PST) 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 S1754192AbeBUMYT (ORCPT + 28 others); Wed, 21 Feb 2018 07:24:19 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:5255 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752350AbeBUMXk (ORCPT ); Wed, 21 Feb 2018 07:23:40 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id D2DF01E0430C9; Wed, 21 Feb 2018 20:23:14 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:09 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 1/6] vfio/type1: Introduce iova list and add iommu aperture validity check Date: Wed, 21 Feb 2018 12:22:04 +0000 Message-ID: <20180221122209.9292-2-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-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 e30e29a..ac6772d 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 */ @@ -1192,6 +1199,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) { @@ -1202,6 +1352,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); @@ -1271,6 +1423,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); @@ -1304,8 +1475,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); @@ -1328,7 +1498,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; @@ -1337,6 +1509,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); @@ -1475,6 +1648,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); @@ -1517,6 +1691,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 Feb 21 12:22:05 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: 129054 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp561357ljc; Wed, 21 Feb 2018 04:23:43 -0800 (PST) X-Google-Smtp-Source: AH8x225uvN9SQe7cawLeMDGdnDdJadGSvr8Na7k18AV3b2NtT9ZdFk0XAry+l3mSE++5ny0w06oD X-Received: by 2002:a17:902:5a88:: with SMTP id r8-v6mr3096970pli.426.1519215823411; Wed, 21 Feb 2018 04:23:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215823; cv=none; d=google.com; s=arc-20160816; b=E5fom3KTqyQNg8mYCvRHcP3GqkRGUnSysFQSgJEapIPxkLmBTGGgMvDX1QuCuTvfMp NyfcbWlywWVdsLVa55V0VM+mR+vSHAlG0O/d3C5mzxux7IQSmuBfTZFA8jInT0LIHwjN eJsGZevu4l1oquIsWpGs+I0X4NigLm11vJxk7GTXA9SZUao8qHtcVDRxvzpCZ8hAbJjU WH52/+hqRz4aEpaCznvMiq82Onm8LjbTvsVrEn+d5qLLB+ztv/sks53kwEsdnbnWZOt3 ByY79uRY0PpQHlmP75xWkXq6qql3JpNQlbnqC2n8utCcYYUtKH58d5/j9NNkgW/dTBh+ 3VkQ== 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=9dLnHSI/Ga7Ae6ZFaQoVPEyvWPORngWl7jyok3jLxm8=; b=ZpxF3hdGJ8Eg98g6smHmcnmUwA7P6RwFgu94iP02RNgZbk4lKBPAwGCzCwPkuNkfwb pfj01LbuVVUr2zhsxw+xxpRKXeWbVLuLzRW+WbGrEtOZtopoYAA178eZuN+G+AMQQqiA g5MNveu1PYU1UjX0Ye3SBoDuB+3nUCG8xpcNCKCc2wsfQ9ewbbhQZq0q038qOqbm1cL4 KAbMzd8VC5nCmZlS82oLeUGy0UZp9bVrHCbvjiajB9yMI1KmyEjXTyxDw2ZbFQ8htsnV bijmcrwXSUFb/O+nhysctp1C6alGqpDwMyulZ/rBsTC5h27YfQYCwcJtg/x6jMgL2yrG FlPA== 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 p11si1240291pgs.537.2018.02.21.04.23.43; Wed, 21 Feb 2018 04:23:43 -0800 (PST) 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 S1754142AbeBUMXj (ORCPT + 28 others); Wed, 21 Feb 2018 07:23:39 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:5655 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752480AbeBUMXh (ORCPT ); Wed, 21 Feb 2018 07:23:37 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 687B967B5E818; Wed, 21 Feb 2018 20:23:20 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:12 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 2/6] vfio/type1: Check reserve region conflict and update iova list Date: Wed, 21 Feb 2018 12:22:05 +0000 Message-ID: <20180221122209.9292-3-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-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 ac6772d..66c57ee 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1301,6 +1301,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; @@ -1354,6 +1430,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); @@ -1432,6 +1509,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) @@ -1442,6 +1526,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); @@ -1502,6 +1590,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; @@ -1510,6 +1599,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 Feb 21 12:22:06 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: 129057 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp561901ljc; Wed, 21 Feb 2018 04:24:24 -0800 (PST) X-Google-Smtp-Source: AH8x226xaBT0Q3pFyVHYwv8cftb6xstH4KYjIHRnyunmuQ61EAem9rDwuTUP/lv0Tm0WsdhnzxAX X-Received: by 10.99.114.18 with SMTP id n18mr2573147pgc.169.1519215863899; Wed, 21 Feb 2018 04:24:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215863; cv=none; d=google.com; s=arc-20160816; b=OLiDrH5kL4NkwtF54QK0WecX7O+IybbBfx0ipqJ4rVZDHKuFEL9kmccxfTTc+UcDuB KNUGMCVrzLZjWKXAYwkKRWqzizKBmxaVv9xBJVoGYGTw2fKArF8UZcOR+LuLHaVjXzIx NA6lsHA3kuxq4sjLMzKkx6yN9f/6bhGK0TZZyi0BxxCcuNCkpLthBbSIit/m0Xr+CiiT 8kXx+yebYCvvzywOoB9vKLva+pC3+IDhNnL5YZMDmsFqSIAqSUObkImIPdvAYLcQHQP8 jj/BMyEGtAEQb9PAJEyPRbwduaiYgsOWGAcUUzIwP16NwST480Sgr9NrhzuJfyvRLLI5 Ul1Q== 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=5znM/8nt8qpmZWdPLgL0u0B9qJ80eVEnMlLKH3WlHio=; b=oleBA2k47j5eccV0p27VAdfPV/SveJCBK7kSnd5dh8QmusmWeyxp3Df/nZaNyySMD7 t3B1glOcNjqF7ZBYjeWZVT8q33lp5iDeY2hzSb2RtJspksouovjXZFtPMnlfT7p7zY79 /MUg1RHr7VBJYGMpB4IHcgAyEUgeA7+HA8aVcfFi/cKK383RbhSmsBTlFWs6ZKvmTXFm uczIXZhlHBntsVxtO+i2iSbJEo3rdfpQEzdA0G0ph99nkNvqKA0QF5d51+5JCMsyt1bJ qjs9jMaU4NAbMTTjJunT5IYzo6bnx8+QAm8KlM4XUnXTDvLr1Bk3+4qYe7TZTDAfqRX5 z3kg== 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 n129si1077655pga.260.2018.02.21.04.24.23; Wed, 21 Feb 2018 04:24:23 -0800 (PST) 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 S1754208AbeBUMYV (ORCPT + 28 others); Wed, 21 Feb 2018 07:24:21 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:5656 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754063AbeBUMXk (ORCPT ); Wed, 21 Feb 2018 07:23:40 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 7E3A31C04D506; Wed, 21 Feb 2018 20:23:25 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:16 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 3/6] vfio/type1: Update iova list on detach Date: Wed, 21 Feb 2018 12:22:06 +0000 Message-ID: <20180221122209.9292-4-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-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 66c57ee..a80884e 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1655,12 +1655,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); @@ -1683,6 +1759,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) @@ -1708,10 +1790,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 Feb 21 12:22:07 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: 129056 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp561655ljc; Wed, 21 Feb 2018 04:24:04 -0800 (PST) X-Google-Smtp-Source: AH8x227X2ixerfNoZP3Ujz0+vGTuDeJjkGbbDhEwfCNeEo/+vZBL1K286mtgFf+/1VBWjDsDaXlM X-Received: by 2002:a17:902:6c42:: with SMTP id h2-v6mr3052395pln.123.1519215844385; Wed, 21 Feb 2018 04:24:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215844; cv=none; d=google.com; s=arc-20160816; b=hc7cgv0skVHTV84b/bbOUrqKABT2vQNpuLZJVDj9iwPCFDjaLAsZ07MvVwzUO6+MJe 2aP32w2N/b65lyeAVIyKwES8rtaylK4on6mezz80aQjOzWkp2nZq18yC84UzjiNN0aTv VrHccCrJFYnEVFdzGlkm+EdPepzsOIKW9X6Khf/iC4DE48enPUrX3vHlYcHRc/ieZf3I 53Noh/rfLxcWNRIJKIh9GriTHJEHEVIqv84v4JMZtECvfSuI9sA2KD+G87gm9kORNXNv Y2W1jtINUvwKo2EtSqwiF9oqaIrVyqlM0haBVaHnpnpsQRxC5T9rl/1JfrRJCYUkFzl6 9JKw== 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=B4TbYMWouUpPNhPvS2T775D/7RO2IKBxc7zfA2upqDM=; b=UGPSDR1MauuUxsjviWIgfi+bi4EWa9O8veym3aEv+eY89YJPmkwfrHylS7kQRvpiWH hAqHfWlIVdCA7J0d/vpKXiY242vHb1u5jPCX4GnlCCeAlxnNzr7muDKQlzSKuNx84dQd xJ2j5ycMolhnj0/jpyreQiZToley7AkHE7du4BdIm4MSAC9E1bZ7Ycd4jZ6AAizDoP71 XD4YubxUER2CQEFskGLmXSLhxBEheAt9T7LAZSpqvBLryLCFKKRmituqmM7moLIAlXK3 Y1loZdPb4o0K097nN0f/eszD+WjI3nVpdHhCyuDXopdlJLNymTri284iRc1U25aeQdBD WkiQ== 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 s196si376254pgc.129.2018.02.21.04.24.04; Wed, 21 Feb 2018 04:24:04 -0800 (PST) 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 S1754166AbeBUMXq (ORCPT + 28 others); Wed, 21 Feb 2018 07:23:46 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:5657 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754124AbeBUMXj (ORCPT ); Wed, 21 Feb 2018 07:23:39 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 8DC3B1518CE04; Wed, 21 Feb 2018 20:23:25 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:19 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 4/6] vfio/type1: check dma map request is within a valid iova range Date: Wed, 21 Feb 2018 12:22:07 +0000 Message-ID: <20180221122209.9292-5-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-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 a80884e..3049393 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -970,6 +970,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) { @@ -1008,6 +1025,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 Feb 21 12:22:08 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: 129059 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp563059ljc; Wed, 21 Feb 2018 04:25:41 -0800 (PST) X-Google-Smtp-Source: AH8x226iMl7f3LU1hi1t/FD1eUjWQ6oKdZGGc6Xo+DrbFKsCXdfN66fNgGUxwho12ziP8qAp6bzT X-Received: by 10.98.67.157 with SMTP id l29mr3158312pfi.214.1519215941275; Wed, 21 Feb 2018 04:25:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215941; cv=none; d=google.com; s=arc-20160816; b=ZMX0TH1r0ZExHqmUp2K56sY/B66mnE06oCFD032JAoqEmlvD99K+Pa6L+O1SbEX7JE RC9+jxwFgB0H6Zfxk5Wp0FCKD2bb0hNMyx1hlPbuj3YxTxq9AECb7hNB8wHl+KOEBQ5M IeRSDAJuOVKr0KRBpTw6iausEdciDTyubBJ84du97plHNSZKPD7aAqCjshkhh4ZOGNVD r4BIHet3kEmYI4pZDaredqjgDo/kYtZaZmBqpbyhtBW/KWIYo5IodhQGCJ4rbgHR3MDg wfpkWYyXlhHEPBIJdwrqtQl5qxrqFEnHgv9Aqf1HsBkLueQWOoSnevy7kEDtN66zSk1f s3wA== 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=Wt435cJsSYXVjBb0lgq+QO58ijLOL7gqCA7O0vLObz4=; b=MfbmaOg1DQKG0M1DOUTOpe3OpmvfAadB7YLtj8vTzi5fyt0n8gkCJ83a4SGQi3CEun A1kW4RyW+bdepZoSek++1Ckq+xrlos6uxElVDvlo/Y1MFiycGvj6gB/dnFZuMyIx2QlK kyVNtc6ewl8uRr7zxzq2pt13mLjRwanKVD9r2TGdVe5S6moGDjPyfnocuVxVtwtU12TT FSz6KbwxhOwpWcv6RkJUZD0rLPtoX3Tf1plupiFKpY6FDRIlmU3GmHO9jCYWUWWYaWEj 4TNbzlqPjy6Cg+CNRsgMWidozqflyI0mQ608locVtEDFb3HbSNm+uYENTsimLzV0HtxH c8wg== 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 l6-v6si1466241pls.65.2018.02.21.04.25.41; Wed, 21 Feb 2018 04:25:41 -0800 (PST) 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 S1754096AbeBUMXg (ORCPT + 28 others); Wed, 21 Feb 2018 07:23:36 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:49318 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752455AbeBUMXe (ORCPT ); Wed, 21 Feb 2018 07:23:34 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 1F0058944EFC0; Wed, 21 Feb 2018 20:23:31 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:22 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 5/6] vfio/type1: Add IOVA range capability support Date: Wed, 21 Feb 2018 12:22:08 +0000 Message-ID: <20180221122209.9292-6-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-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 | 88 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 23 +++++++++++ 2 files changed, 111 insertions(+) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 3049393..c08adb5 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1917,6 +1917,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) { @@ -1938,6 +2000,8 @@ 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 }; + int ret; minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes); @@ -1951,6 +2015,30 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, 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; + } + minsz = offsetofend(struct vfio_iommu_type1_info, + cap_offset); + 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 c743721..46b49e9 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -589,7 +589,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 Feb 21 12:22:09 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: 129055 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp561478ljc; Wed, 21 Feb 2018 04:23:52 -0800 (PST) X-Google-Smtp-Source: AH8x227p9aovLT8DjRGNNl1cbmFL15oH4ANIAXVhSbkzuz3pLFyyS8RXvTEvMRCEXixCMlYrd8U6 X-Received: by 10.99.45.195 with SMTP id t186mr2622719pgt.127.1519215832027; Wed, 21 Feb 2018 04:23:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215832; cv=none; d=google.com; s=arc-20160816; b=NCNOgW9/UYUwCeDTC79kuaGc1TN9mKtWgH60ETY+8m6Wz0H+jauo3khBtjZCinXthM IJJAo+jYtXRN5rLaSs3JBLRPJ7yfAPODrpbD18pvFQ7Tj+R9fBZCkjiCnYfo//b5BFY2 8dGdYZOeRtEQDN7vhm9wznHyVd13vE2sSDk/A1AVz38kaFqQfRdxx6WmRVEktF2fAUtk xPkzyL1DX5pbi/Yk+UwNdQP1I8FiOvTpQecKG5xHy5nO3CHhTmKWmRJPS8xpSio+M2Rc PWEkt7KMVoPXeBH7S+Kr1m9NBJJ3jGU515kXxhOaIqxIK1tEm17na9lHJ7NMlGpHBEZM 4PlQ== 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=OrIfLE02AOc44nARBLwJJwAp5/aObvd7TaWoLWlztLE=; b=jTf6dEtYVwFYMy3ta86w94ytnoiyJ9pp6f87x6C1V4pIwtHakISIfHk+eGWNlw8Jcr hnWiqQxN0RMfF7NfWwITqIcIlY4Z5dh1HoiHjP5oqoZCfQt45shldk0tlQkpEt1WQ4bO ib7VI1B0nk1NESJ5ZS0MfoIx6REmc0WQ9CTpLumKxDFCPTKYbb3jshNH+KLwdGE9n5VC WFS9Swed0qhFkVvziBJYzcV/yq+aJPzWDsZFtCVsnJIKnmqagZ+TatEHjnQR5da/OQlq itQtNNSKl6ucJ3QabHK6k+OrJGtpjz9Uc++VwIYJwutDSxSJOM+roipS1x8I6h4sgQuM sMFg== 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 s196si376254pgc.129.2018.02.21.04.23.51; Wed, 21 Feb 2018 04:23:52 -0800 (PST) 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 S1754180AbeBUMXs (ORCPT + 28 others); Wed, 21 Feb 2018 07:23:48 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:49322 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754092AbeBUMXj (ORCPT ); Wed, 21 Feb 2018 07:23:39 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 2E54078A571A2; Wed, 21 Feb 2018 20:23:31 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:25 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 6/6] vfio/type1: remove duplicate retrieval of reserved regions. Date: Wed, 21 Feb 2018 12:22:09 +0000 Message-ID: <20180221122209.9292-7-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-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 c08adb5..56f68b1 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1192,15 +1192,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 @@ -1216,8 +1214,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; } @@ -1552,7 +1549,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);