From patchwork Fri Jan 12 16:45:27 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: 124376 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2272875qgn; Fri, 12 Jan 2018 08:51:11 -0800 (PST) X-Google-Smtp-Source: ACJfBotcF9QJuTtK73SvKIDToK4SKfTH2DzulHoUGIV1Av5xmIJcxdGVM12jXGK3d8I1GCh807kG X-Received: by 10.98.254.11 with SMTP id z11mr15649583pfh.48.1515775871457; Fri, 12 Jan 2018 08:51:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515775871; cv=none; d=google.com; s=arc-20160816; b=xpz2CnGxFqz84Ole7k4MDBDt0KNgowvaNEK8v8AyopOhmJg5EGj+uZ9rRK+XYWtrCp KUW12Hb1gTyv/TU71DoMlV2EUl/aV9hj1TOCI9Ga1e0QIrEhXLKgtJRvJAbxSv+fmkF+ y3je47sGZjr50ykzcg7KiA5OIYWZ1yJKQqJpHHBQtJzp9vPYDxTlXbbE5LZl2Am623kt 16Ni+lpxgOg8EJTI3NjRYFiC46E4/GeB2gHXOXQLuo6Wayn+d/riUDDCJoiSmac/tuif h5wiuVT1N+OXsCCitwG7jnNsxzoNOj+DIb1DvLgMs7n2kKFD1vdjggF9cbXdzZb32qDX p3YA== 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=2d/pRI42xNNZA7vX55s/FVSLLJY8hOF19dMHWzEhr+A=; b=t1XoGy3PHYm44rVZMar5dpimZ+arE/iTqALYqxANqsUFepPuqsK8DiYvi5qsjHSmSy /DP16jTDsbmnOHJL4k55GjDJqldnNPhdwNxesXdLWIFX1PXAG5yc/n84DXkrMCdC1Xg0 X6igcnNn5qlXc9n1d60DPRosHx5P9aSG7+w9T2TWKHYF+8Q2pwDEuiua3V3uhxy8sval OA2YMKUHTvUehLw/Y/ljVQoBwVuIF/dFWNYcH2i3WCVnpW9bZJboGUTTs3HcT01gTvO1 hAMqNuVuMmcQsYiU2LBiGeHAeGnThVQW/KbVpBhc9UQ7pMVF+nbPJS2z4IF3nrEXgAYu NUMA== 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 n189si15593837pfn.40.2018.01.12.08.51.11; Fri, 12 Jan 2018 08:51:11 -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 S965028AbeALQvJ (ORCPT + 28 others); Fri, 12 Jan 2018 11:51:09 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:57007 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S964853AbeALQsW (ORCPT ); Fri, 12 Jan 2018 11:48:22 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 05C835747A4AC; Sat, 13 Jan 2018 00:48:17 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.361.1; Sat, 13 Jan 2018 00:48:08 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [RFC v2 1/5] vfio/type1: Introduce iova list and add iommu aperture validity check Date: Fri, 12 Jan 2018 16:45:27 +0000 Message-ID: <20180112164531.93712-2-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180112164531.93712-1-shameerali.kolothum.thodi@huawei.com> References: <20180112164531.93712-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 is valid and doesn't conflict with any existing dma mappings during attach. Also update the iova list with new aperture window during attach/detach. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 177 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) -- 1.9.1 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index e30e29a..11cbd49 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -60,6 +60,7 @@ 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; + phys_addr_t start; + phys_addr_t end; +}; + /* * Guest RAM pinning working set or DMA target */ @@ -1192,6 +1199,123 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) return ret; } +static int vfio_insert_iova(phys_addr_t start, phys_addr_t end, + struct list_head *head) +{ + 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; +} + +/* + * Find whether a mem region overlaps with existing dma mappings + */ +static bool vfio_find_dma_overlap(struct vfio_iommu *iommu, + phys_addr_t start, phys_addr_t end) +{ + struct rb_node *n = rb_first(&iommu->dma_list); + + for (; n; n = rb_next(n)) { + struct vfio_dma *dma; + + dma = rb_entry(n, struct vfio_dma, node); + + if (end < dma->iova) + break; + if (start >= dma->iova + dma->size) + continue; + return true; + } + + return false; +} + +/* + * Check the new iommu aperture is a valid one + */ +static int vfio_iommu_valid_aperture(struct vfio_iommu *iommu, + phys_addr_t start, + phys_addr_t end) +{ + struct vfio_iova *first, *last; + struct list_head *iova = &iommu->iova_list; + + if (list_empty(iova)) + return 0; + + /* Check if new one is outside the current aperture */ + 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 -EINVAL; + + /* Check for any existing dma mappings outside the new start */ + if (start > first->start) { + if (vfio_find_dma_overlap(iommu, first->start, start - 1)) + return -EINVAL; + } + + /* Check for any existing dma mappings outside the new end */ + if (end < last->end) { + if (vfio_find_dma_overlap(iommu, end + 1, last->end)) + return -EINVAL; + } + + return 0; +} + +/* + * Adjust the iommu aperture window if new aperture is a valid one + */ +static int vfio_iommu_iova_aper_adjust(struct vfio_iommu *iommu, + phys_addr_t start, + phys_addr_t end) +{ + struct vfio_iova *node, *next; + struct list_head *iova = &iommu->iova_list; + + if (list_empty(iova)) + return vfio_insert_iova(start, end, iova); + + /* 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 int vfio_iommu_type1_attach_group(void *iommu_data, struct iommu_group *iommu_group) { @@ -1202,6 +1326,7 @@ 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; mutex_lock(&iommu->lock); @@ -1271,6 +1396,14 @@ 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); + + ret = vfio_iommu_valid_aperture(iommu, 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); @@ -1327,6 +1460,11 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_detach; } + ret = vfio_iommu_iova_aper_adjust(iommu, geo.aperture_start, + geo.aperture_end); + if (ret) + goto out_detach; + list_add(&domain->next, &iommu->domain_list); mutex_unlock(&iommu->lock); @@ -1392,6 +1530,35 @@ 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_iova_aper_refresh(struct vfio_iommu *iommu) +{ + struct vfio_domain *domain; + struct iommu_domain_geometry geo; + struct vfio_iova *node; + phys_addr_t start = 0; + phys_addr_t end = (phys_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 iova aperture limits */ + node = list_first_entry(&iommu->iova_list, struct vfio_iova, list); + node->start = start; + node = list_last_entry(&iommu->iova_list, struct vfio_iova, list); + node->end = end; +} + static void vfio_iommu_type1_detach_group(void *iommu_data, struct iommu_group *iommu_group) { @@ -1445,6 +1612,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, iommu_domain_free(domain->domain); list_del(&domain->next); kfree(domain); + vfio_iommu_iova_aper_refresh(iommu); } break; } @@ -1475,6 +1643,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); @@ -1502,6 +1671,7 @@ static void vfio_iommu_type1_release(void *iommu_data) { struct vfio_iommu *iommu = iommu_data; struct vfio_domain *domain, *domain_tmp; + struct vfio_iova *iova, *iova_tmp; if (iommu->external_domain) { vfio_release_domain(iommu->external_domain, true); @@ -1517,6 +1687,13 @@ static void vfio_iommu_type1_release(void *iommu_data) list_del(&domain->next); kfree(domain); } + + list_for_each_entry_safe(iova, iova_tmp, + &iommu->iova_list, list) { + list_del(&iova->list); + kfree(iova); + } + kfree(iommu); } From patchwork Fri Jan 12 16:45:28 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: 124371 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2270303qgn; Fri, 12 Jan 2018 08:48:24 -0800 (PST) X-Google-Smtp-Source: ACJfBouV5sudkQGnomb2kXDNdf5AWDpeT9IEO3LhmUI+Boo0W8+67hAE+SD/dfy4r/DK8xybv2qT X-Received: by 10.159.240.139 with SMTP id p11mr26151730plr.92.1515775704842; Fri, 12 Jan 2018 08:48:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515775704; cv=none; d=google.com; s=arc-20160816; b=02lhYLy9Kxx0GgeDh/onk9hkrpcFbQ3/7G8Io4Wt4JMwHlE5FFSlmvSlN/dDH838Yy hqtzzjbC959PluF3/C0B+RHPC9JMAd3z3rDLJ1rPa976egmivBk1q4g6gpcd4fwJisrD OIRaxmcgHPGcCkYYizjR0YZVSp+r5ms2VqtpF/fFw4uVbMREn3Yd2iJXK51/6dr3g6x+ NZR21GxZsOzUEBBfSj9lzxJLWt+WHRKOHk8/2I9KHS/f4VIoJ7oHjXKgewuE/CI+l0mP 4KI+OOGU52svavcjp2KnkC9CJPppqlRmvo+xZHTKwcDk1F+wPhoBNXwI7VHUuvC1+Q0/ F5dg== 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=EBciyJPDOv6PURCHG03ZDyrov7Q2EG4aQAyNn1AhqtU=; b=CDw4QHLmijsxgKN1+r8LaxpgQlTB7+7v2nKRdYYRM7asXnNd2KjO6zS2WfEFMuIDl5 jOEZrsOWWEMqaotkC72yYZ2SKssnDRcRdZK7MOkk/MWtYl8NWywzhGbdZ2wHdyEyosuA 7WGRpvXwujrYN3lvCzvhGdihvWDjJTogzypUI2u+9KIlmNyN6fs65Ccr3BoqLenuLwsI /5nLbyY3E0fDh9AGEkUcJgKfmWiqQkkEBJ9r70inlMlEQlcsRmxplvAwcXHKzYZdL5rc xa23Nxe6S5M9uXWJnkK6Yjq6eSFpN3Zg7IArTUYbtfTuDuvLsLffohkXT/tsXjC9nqqW LUfA== 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 m27si3287866pgn.667.2018.01.12.08.48.24; Fri, 12 Jan 2018 08:48:24 -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 S964921AbeALQsX (ORCPT + 28 others); Fri, 12 Jan 2018 11:48:23 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:57008 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S964828AbeALQsV (ORCPT ); Fri, 12 Jan 2018 11:48:21 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 16024B0730D7E; Sat, 13 Jan 2018 00:48:17 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.361.1; Sat, 13 Jan 2018 00:48:11 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [RFC v2 2/5] vfio/type1: Check reserve region conflict and update iova list Date: Fri, 12 Jan 2018 16:45:28 +0000 Message-ID: <20180112164531.93712-3-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180112164531.93712-1-shameerali.kolothum.thodi@huawei.com> References: <20180112164531.93712-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 | 161 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 2 deletions(-) -- 1.9.1 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 11cbd49..7609070 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1199,6 +1200,20 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) return ret; } +static int vfio_resv_cmp(void *priv, struct list_head *a, struct list_head *b) +{ + struct iommu_resv_region *ra, *rb; + + ra = container_of(a, struct iommu_resv_region, list); + rb = container_of(b, struct iommu_resv_region, list); + + if (ra->start < rb->start) + return -1; + if (ra->start > rb->start) + return 1; + return 0; +} + static int vfio_insert_iova(phys_addr_t start, phys_addr_t end, struct list_head *head) { @@ -1274,6 +1289,24 @@ static int vfio_iommu_valid_aperture(struct vfio_iommu *iommu, } /* + * Check reserved region conflicts with existing dma mappings + */ +static int vfio_iommu_resv_region_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_overlap(iommu, region->start, + region->start + region->length - 1)) + return -EINVAL; + } + + return 0; +} + +/* * Adjust the iommu aperture window if new aperture is a valid one */ static int vfio_iommu_iova_aper_adjust(struct vfio_iommu *iommu, @@ -1316,6 +1349,51 @@ static int vfio_iommu_iova_aper_adjust(struct vfio_iommu *iommu, return 0; } +/* + * Check and update iova region list in case a reserved region + * overlaps the iommu iova range + */ +static int vfio_iommu_iova_resv_adjust(struct vfio_iommu *iommu, + struct list_head *resv_regions) +{ + struct iommu_resv_region *resv; + struct list_head *iova = &iommu->iova_list; + 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) { + phys_addr_t a, b; + int ret = 0; + + a = n->start; + b = n->end; + /* No overlap */ + if ((start > b) || (end < a)) + continue; + /* Split the current node and create holes */ + if (start > a) + ret = vfio_insert_iova(a, start - 1, &n->list); + if (!ret && end < b) + ret = vfio_insert_iova(end + 1, b, &n->list); + if (ret) + return ret; + + list_del(&n->list); + kfree(n); + } + } + + if (list_empty(iova)) + return -EINVAL; + + return 0; +} + static int vfio_iommu_type1_attach_group(void *iommu_data, struct iommu_group *iommu_group) { @@ -1327,6 +1405,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, bool resv_msi, msi_remap; phys_addr_t resv_msi_base; struct iommu_domain_geometry geo; + struct list_head group_resv_regions; + struct iommu_resv_region *resv, *resv_next; mutex_lock(&iommu->lock); @@ -1404,6 +1484,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (ret) goto out_detach; + INIT_LIST_HEAD(&group_resv_regions); + iommu_get_group_resv_regions(iommu_group, &group_resv_regions); + list_sort(NULL, &group_resv_regions, vfio_resv_cmp); + + ret = vfio_iommu_resv_region_conflict(iommu, &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); @@ -1434,11 +1522,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, d->prot == domain->prot) { iommu_detach_group(domain->domain, iommu_group); if (!iommu_attach_group(d->domain, iommu_group)) { + ret = vfio_iommu_iova_resv_adjust(iommu, + &group_resv_regions); + if (!ret) + goto out_domain; + 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); @@ -1465,8 +1557,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (ret) goto out_detach; + ret = vfio_iommu_iova_resv_adjust(iommu, &group_resv_regions); + if (ret) + goto out_detach; + list_add(&domain->next, &iommu->domain_list); +done: + list_for_each_entry_safe(resv, resv_next, &group_resv_regions, list) + kfree(resv); mutex_unlock(&iommu->lock); return 0; @@ -1475,6 +1574,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, iommu_detach_group(domain->domain, iommu_group); out_domain: iommu_domain_free(domain->domain); + list_for_each_entry_safe(resv, resv_next, &group_resv_regions, list) + kfree(resv); out_free: kfree(domain); kfree(group); @@ -1559,6 +1660,60 @@ static void vfio_iommu_iova_aper_refresh(struct vfio_iommu *iommu) 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 void vfio_iommu_iova_resv_refresh(struct vfio_iommu *iommu) +{ + struct vfio_domain *d; + struct vfio_group *g; + struct vfio_iova *node, *tmp; + struct iommu_resv_region *resv, *resv_next; + struct list_head resv_regions; + phys_addr_t start, end; + + INIT_LIST_HEAD(&resv_regions); + + 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; + + list_sort(NULL, &resv_regions, vfio_resv_cmp); + + node = list_first_entry(&iommu->iova_list, struct vfio_iova, list); + start = node->start; + node = list_last_entry(&iommu->iova_list, struct vfio_iova, list); + end = node->end; + + /* purge the iova list and create new one */ + list_for_each_entry_safe(node, tmp, &iommu->iova_list, list) { + list_del(&node->list); + kfree(node); + } + + if (vfio_iommu_iova_aper_adjust(iommu, start, end)) { + pr_warn("%s: Failed to update iova aperture. VFIO DMA map request may fail\n", + __func__); + goto done; + } + + /* adjust the iova with current reserved regions */ + if (vfio_iommu_iova_resv_adjust(iommu, &resv_regions)) + pr_warn("%s: Failed to update iova list with reserve regions. VFIO DMA map request may fail\n", + __func__); +done: + list_for_each_entry_safe(resv, resv_next, &resv_regions, list) + kfree(resv); +} + static void vfio_iommu_type1_detach_group(void *iommu_data, struct iommu_group *iommu_group) { @@ -1617,6 +1772,8 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, break; } + vfio_iommu_iova_resv_refresh(iommu); + detach_group_done: mutex_unlock(&iommu->lock); } From patchwork Fri Jan 12 16:45:29 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: 124374 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2270722qgn; Fri, 12 Jan 2018 08:48:52 -0800 (PST) X-Google-Smtp-Source: ACJfBou9lxVk5d+ll6WdxacPw53uU9gHCkS/uqM+1Q4K6TEUpXEEA8hCO0/6e7mSOsSs23MZnKBv X-Received: by 10.98.157.211 with SMTP id a80mr14244019pfk.242.1515775732720; Fri, 12 Jan 2018 08:48:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515775732; cv=none; d=google.com; s=arc-20160816; b=UiS7OH9YgMUEFNybF6+Vh0EzL1G27wwjYFILo//rPP46wM0C9KEKoZMlbf6kEUZkpB aIPHu5PPD/Bl/o5NIIHkifo6dSPiDczJccO3s5ls8AzTtczt1CRjWfHpTEMK84itOe27 Edr6MFDQ+V4yHSROf169XXesJjHS8PhLxl+JvhmWqPwMDdy1wGGWZnYSjvDP0E9j2fzW q2DwDoDIlDAdcIuxT8eYezIv2J8c/TEraaMQmZA2fbRNRlNALOoYTXWKJF+USwLL2fag XfcwPe73VvdpG1X6qblJLG5jtpC5ESqu1VNlYbPfp0lbq3wwJp5RhP0ZAjCORmbds2WX 1f+A== 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=kaCkxPGw4scAKQY3QqzzIHMZcdUh1WNCAm2ziInWafk=; b=BVjuV9kxdknTMJ3KIBVxABG+4s3uaLXd67JXZRv7YK73vKOk4mG52dsJcHROAM6ZR+ pkcXId4zEVtFy5LwtoU4ov3Mmz9ifUByd2ampCJGst19KqN9WLgcpVbNueI/1b41uGZd pMA1BiOTVyU2gqyd7E3iECvMskzrnCbkkSWD4HEsvbVAz0/ybXGDunFMBcgKeYC7RdOj cLhTcJruAXCt6HNOKkp7Sl8peqF3lNqjDclNEN6HBASJPD4bhOmxJmhtvZXYeEomF650 lWybk9WUjn4mfqwO1plRg7l0ACF7vpSPg/XvCnXNWMo3X2SqYyqYbvFTXaU5hC2DS4tt royg== 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 m10si8520400pll.714.2018.01.12.08.48.52; Fri, 12 Jan 2018 08:48: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 S965036AbeALQsu (ORCPT + 28 others); Fri, 12 Jan 2018 11:48:50 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:48536 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S964828AbeALQsq (ORCPT ); Fri, 12 Jan 2018 11:48:46 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 3EBD37303D566; Sat, 13 Jan 2018 00:48:24 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.361.1; Sat, 13 Jan 2018 00:48:15 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [RFC v2 3/5] vfio/type1: check dma map request is within a valid iova range Date: Fri, 12 Jan 2018 16:45:29 +0000 Message-ID: <20180112164531.93712-4-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180112164531.93712-1-shameerali.kolothum.thodi@huawei.com> References: <20180112164531.93712-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(+) -- 1.9.1 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 7609070..47ea490 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -971,6 +971,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, + phys_addr_t start, phys_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) { @@ -1009,6 +1026,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 Fri Jan 12 16:45:30 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: 124373 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2270625qgn; Fri, 12 Jan 2018 08:48:47 -0800 (PST) X-Google-Smtp-Source: ACJfBos9YkPSuBa6edzD5d/kE1ym3Wsj69NcuDCS3B45qatOk9hd4tegI2dw7zZT8hiCT982pmYg X-Received: by 10.99.186.87 with SMTP id l23mr11597871pgu.83.1515775727027; Fri, 12 Jan 2018 08:48:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515775727; cv=none; d=google.com; s=arc-20160816; b=ELmiNLOOjUzsuQLYDKPBNzllNnaZ8OI1eD64Jioe3Kt2/fA1L/ElJqpr7ZDBteEtHx P+2pU74/03Lo/ADCaBwCnSzPr4hw33LFinPCRozfilPPQVwCb9N8nnLAQaOFPLEWiqk2 cLas72k6AlC5rg+v2vBoLDE/5MjLp1xISkKgIgBZ0JNufMQPLLmmg+WLJbPyOngTdc4V +M7JFvKa4BMlgcBUOVLmznCfXqpuZ94owgXuq6QA85qzWDmt0GoD/UMt/Km5ueTTSLWW SjR5xd87xvLP80GmfaJumvh8lI9oU/erzW4+KniWrdEPEue15cqakFQuCdpA+DgHw3jS EXVQ== 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=w1uvBAsxO/IrZWpeXJxATHBWibyeHOMEAWWLndSHzCg=; b=vAtw0UTb/o4zrk8UjxjrKVbllXKg9c08qmKVJkbyqjKG4xrZ9thtce8qbvRQYrndRD Fs4zJxHbyH2vInmo3zZ+/duzEjh5Ylcp9/q3LKE6Do7sHegvLMV1N3AeHC5g+rDqA2rg geon5c2/ozp6MDfafN18vIyETiE6VcnOXeofYpWzurDoo92wyiX05P5YdN7BkSz3NHnQ ocrv7j38Ln3LiWIlPnGM0V0Yhnwq/XwPQxFKoUwLJPezT2aCE1VRjWxySy5cfD8RI3WN Y159Hh/ZkwnFmNwCwu7BVfNarEu0g0NTKTg/vDiFw0pMpCuiycswKr/YuK5aIKtVnBUL NXzA== 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 91si3501865ply.410.2018.01.12.08.48.46; Fri, 12 Jan 2018 08:48:47 -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 S965004AbeALQsp (ORCPT + 28 others); Fri, 12 Jan 2018 11:48:45 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:48539 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S964828AbeALQsn (ORCPT ); Fri, 12 Jan 2018 11:48:43 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 43C95CBA57363; Sat, 13 Jan 2018 00:48:24 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.361.1; Sat, 13 Jan 2018 00:48:17 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [RFC v2 4/5] vfio/type1: Add IOVA range capability support Date: Fri, 12 Jan 2018 16:45:30 +0000 Message-ID: <20180112164531.93712-5-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180112164531.93712-1-shameerali.kolothum.thodi@huawei.com> References: <20180112164531.93712-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 | 91 +++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 23 +++++++++++ 2 files changed, 114 insertions(+) -- 1.9.1 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 47ea490..dc6ed85 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1893,6 +1893,67 @@ static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu) return ret; } +static int vfio_add_iova_cap(struct vfio_info_cap *caps, void *cap_type, + size_t size) +{ + struct vfio_info_cap_header *header; + struct vfio_iommu_type1_info_cap_iova *iova_cap, *iova = cap_type; + + header = vfio_info_cap_add(caps, size, + VFIO_IOMMU_TYPE1_INFO_CAP_IOVA, 1); + if (IS_ERR(header)) + return PTR_ERR(header); + + iova_cap = container_of(header, + struct vfio_iommu_type1_info_cap_iova, header); + iova_cap->nr_iovas = iova->nr_iovas; + memcpy(iova_cap->iova_ranges, iova->iova_ranges, + iova->nr_iovas * sizeof(*iova->iova_ranges)); + return 0; +} + +static int vfio_build_iommu_iova_caps(struct vfio_iommu *iommu, + struct vfio_info_cap *caps) +{ + struct vfio_iommu_type1_info_cap_iova *iova_cap; + 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(*iova_cap) + (iovas * sizeof(*iova_cap->iova_ranges)); + + iova_cap = kzalloc(size, GFP_KERNEL); + if (!iova_cap) { + ret = -ENOMEM; + goto out_unlock; + } + + iova_cap->nr_iovas = iovas; + + list_for_each_entry(iova, &iommu->iova_list, list) { + iova_cap->iova_ranges[i].start = iova->start; + iova_cap->iova_ranges[i].end = iova->end; + i++; + } + + ret = vfio_add_iova_cap(caps, iova_cap, size); + + kfree(iova_cap); +out_unlock: + mutex_unlock(&iommu->lock); + return ret; +} + static long vfio_iommu_type1_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { @@ -1914,6 +1975,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); @@ -1927,6 +1990,34 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, info.iova_pgsizes = vfio_pgsize_bitmap(iommu); + if (info.argsz == minsz) + goto done; + + ret = vfio_build_iommu_iova_caps(iommu, &caps); + if (ret) + return ret; + + if (caps.size) { + info.flags |= VFIO_IOMMU_INFO_CAPS; + minsz = offsetofend(struct vfio_iommu_type1_info, + cap_offset); + if (info.argsz < sizeof(info) + caps.size) { + info.argsz = sizeof(info) + caps.size; + info.cap_offset = 0; + } 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); + } +done: 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 e3301db..8671448 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -517,7 +517,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 1 + +struct vfio_iova_range { + __u64 start; + __u64 end; +}; + +struct vfio_iommu_type1_info_cap_iova { + 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 Fri Jan 12 16:45:31 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: 124372 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2270503qgn; Fri, 12 Jan 2018 08:48:37 -0800 (PST) X-Google-Smtp-Source: ACJfBotFxlhCxkZZ9KHx6vEqeYJsKXtiswtgPG6+9IT8RqjH+ngP0B/LY3A/NVtSJLasUZyOYIj1 X-Received: by 10.101.65.136 with SMTP id a8mr20769887pgq.363.1515775717862; Fri, 12 Jan 2018 08:48:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515775717; cv=none; d=google.com; s=arc-20160816; b=bKar6M1AgTcMFj9TT51tiZxrAK+9Bxbi+CMsHSBT5p7bzmOjN8wTOu2zkJb+QSVm22 7DF0fu6Y4oqCVCl+Ul6TpihTHDeg/grl/Jb2KdR9Rwdfxt5hidp6aj5CllLXw41PHA22 Qr21LpmNkfNiUmrVwRm1cbqu62MfZEQQcBK1PXL8nmof6pl9m3LbVUMm6ZnQ0TVpTAym SAdFfjPvzKbZmkrwZJ1L22I6RdDVnyfH4sPM7H/4H/RVX0YzBxYF/qFd6p8q0gSp0LvK dgx7QhLV+qDFMwC4sYamEGq135KG7RWZ5TzHZXmxJz1wB7rfsrCHoVfShmi2d3QX1Bwd cOAg== 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=WtB1RB2ALQVbHjF8k0DcJitd+jT72JEMFDHKJpnvF8k=; b=LlhIrdOyEj/1+fSAoULy0IgQOuffF8zDGz8NlOBCZLKteBvR45lVZRwwk/K2L+TJt9 4UymIbKGpwTla604I9LDNjq2u2q8HbMTBXmz8jMwx4AUxSprcMnyr7l43tQ8gY2Owvli LbvHOLED8aVmKiZgs+xdMvAw7phUzF0hI4xtRoVBJx8AjWYaeUdEu4CqKlP7fs4Nn1I9 oeG62o8JUTniKsikGhNvqtmD/v9lQ7za4wIOAZ9AKg9I0a7YpoXvEN3kD5dqTAO/CmNj 9M3hkujA9p0NaMhoFs0rAXiPCNLagStasxPwFAuNw2ospDXshpHziPqdrNJgwKKXk6f3 Bt5A== 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 26si15558860pfo.269.2018.01.12.08.48.37; Fri, 12 Jan 2018 08:48:37 -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 S964977AbeALQsf (ORCPT + 28 others); Fri, 12 Jan 2018 11:48:35 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:57092 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S964828AbeALQsc (ORCPT ); Fri, 12 Jan 2018 11:48:32 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 28E0B6778BD91; Sat, 13 Jan 2018 00:48:27 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.361.1; Sat, 13 Jan 2018 00:48:20 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [RFC v2 5/5] vfio/type1: remove duplicate retrieval of reserved regions. Date: Fri, 12 Jan 2018 16:45:31 +0000 Message-ID: <20180112164531.93712-6-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180112164531.93712-1-shameerali.kolothum.thodi@huawei.com> References: <20180112164531.93712-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(-) -- 1.9.1 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index dc6ed85..b5cf21d 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1193,15 +1193,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 @@ -1217,8 +1215,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; } @@ -1514,7 +1511,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);