From patchwork Thu Mar 15 16:35:03 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: 131823 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1699264edg; Thu, 15 Mar 2018 09:39:46 -0700 (PDT) X-Google-Smtp-Source: AG47ELua9pASKR3Cx9i+kbxlTTTHGLNMfqWV9QOlQhvW5rJQxepG/alNqYG/DDWX0qG33HecvvtE X-Received: by 2002:a17:902:b691:: with SMTP id c17-v6mr8872906pls.308.1521131986277; Thu, 15 Mar 2018 09:39:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131986; cv=none; d=google.com; s=arc-20160816; b=eGaoLjcWah36iaTo7x29rhxl0seDC0VEyvrG0PFK4cou8QBpmTrwxDJz6OfpR1sPvx h3rlDzDxZbT4qnEwVdTstVi1gG3OgEpx175bYP9yGrqMibo0G3gHiiZZqGGb+q7L+xq0 mgAox2xvqD2ynG9hKNi9TgqKzbfVltUZL4hMNS1/V29tTdqju9NF3Tgl1YxW1PVIQYiC iIHLDNCKzDRiEBZGJBDGNAPMgmq3pjHpYGHy1Q1NbciV6kO4nM1sfhMeQUnBnU36AOhF LUVx/kzwOIvOyJeaAZYREp1ef4piPREkvkm2OE46M3s+PCY90asowEiU57ppvoX0DDdG FSbA== 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=MlHNfiKsptG9q+j6sk0X+VOqzUS8B+TFMV2d2p1cE+A=; b=Phr1kNMOGmvnQzEGN20nX7+cL/kr6Yfd21JNGEuWKD4cnLDz5FliXXIFddey3C3t14 vlmSmqkBhaR8z5kDBIJN4ZkO/JfnJk256eieGLU9z9GuzkD5We1NONhOVjk4LE0FU7TI q9lGIMSyQrSkADLgnQ6HyrOEtJYQlWq+y5FMHqO4qc1NYuGvlPjTV87jUa/u0VtGXCTG hQIS5hHwKrbkgasQNu+HWrCoKiF1YhkcDwbtBXDe/i0BgIWPA/XefgAmCkCiueegg5rh /R9UpMuL359vAEFTPR8Z6RyCdMpxbMwEzBt75cJchdoLC+V1sFUFQ7bcuXMdjSWO/5aG yZNw== 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 s63si1852300pgs.169.2018.03.15.09.39.46; Thu, 15 Mar 2018 09:39:46 -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 S932311AbeCOQjn (ORCPT + 28 others); Thu, 15 Mar 2018 12:39:43 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:6649 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751546AbeCOQiM (ORCPT ); Thu, 15 Mar 2018 12:38:12 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 90347B1B38EDA; Fri, 16 Mar 2018 00:37:54 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:37:45 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v5 1/7] vfio/type1: Introduce iova list and add iommu aperture validity check Date: Thu, 15 Mar 2018 16:35:03 +0000 Message-ID: <20180315163509.17740-2-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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 45657e2..1123c74 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 */ @@ -1204,6 +1211,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) { @@ -1214,6 +1364,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); @@ -1283,6 +1435,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); @@ -1316,8 +1487,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); @@ -1340,7 +1510,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; @@ -1349,6 +1521,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); @@ -1487,6 +1660,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); @@ -1529,6 +1703,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 Thu Mar 15 16:35: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: 131817 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1697517edg; Thu, 15 Mar 2018 09:38:16 -0700 (PDT) X-Google-Smtp-Source: AG47ELsiQohzSfKOzxtgGWZNSzbQU3/NHYBbobPlBV7LI1GjeSxUxKvOIfAH+W9neFjso66/sCK4 X-Received: by 10.101.75.202 with SMTP id p10mr4793099pgr.339.1521131896759; Thu, 15 Mar 2018 09:38:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131896; cv=none; d=google.com; s=arc-20160816; b=SsubwQt1wLUZ/DcDeMjs92+aXJbt1jE272d72P7GhxunC9EBObohOJUgb+yo4QmeF6 gDqoPYq6HOLd4Jjs1f4Vwd7nw7ZdZwXjUYDI5yl7BXqIiKsT4SwB9uWyqWwenz1rA01p fNrZZXi0qYq7DtYLP4ExE/Qw3CYml74lVbRXYRW7HBv4gTVW2/6IbQI4QCL23PU7cLWD gciEzCoyuvbyTqwdFSWziwcjDTgAB+rQuj1LO+5wRRWdaZB0hMVV1oxIkrPPkrS3sqta bxax3K44fbikKT5WZUTv+c0vJzWXT4EDHqhxxcevJkGITFTUAY8vySLjMxb2O9DoSkUG 0tpg== 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=5VEDN5QIoL7AYLxgPRKMBRuPBsuHugRzTUy5j5up3s0=; b=f+xw7nXNJEl+HMg+V6iJc43NyCRA9sVTYgVoJUA9ygbLO8Zujh5CoWF0S4AU7CBz3u tVTr6qlzh+xMM6F9Fr2a7UghEGTGlb4tU+Mk0TJYMGxqIsmKtzeBkxnXOLVkArrnIYuE O64XN85PBtVVdIg0XYsOzjKcxvlqhE6rkxuzviyS9G9vLsLS9ivAz2K3mtH9L/o834v2 G8/+7EbtIgFW4rgq6AGaTaqSKSIeSac1iSAnwqJrACBcyrjQR6XCCL82KF1aDKRI/9CL +1xd4sRBE2xlEG7AQWg34vCzikSdQuF2eO6XwEaXxgMJo++emwx6RG/nk7dzn9UvqXu7 9Xfg== 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 i5-v6si4301438plr.393.2018.03.15.09.38.16; Thu, 15 Mar 2018 09:38: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 S1752186AbeCOQiM (ORCPT + 28 others); Thu, 15 Mar 2018 12:38:12 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:6648 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751982AbeCOQiJ (ORCPT ); Thu, 15 Mar 2018 12:38:09 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 7FB443D26B2EE; Fri, 16 Mar 2018 00:37:54 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:37:48 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v5 2/7] vfio/type1: Check reserve region conflict and update iova list Date: Thu, 15 Mar 2018 16:35:04 +0000 Message-ID: <20180315163509.17740-3-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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 1123c74..cfe2bb2 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1313,6 +1313,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; @@ -1366,6 +1442,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); @@ -1444,6 +1521,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) @@ -1454,6 +1538,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); @@ -1514,6 +1602,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; @@ -1522,6 +1611,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 Thu Mar 15 16:35: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: 131820 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1698079edg; Thu, 15 Mar 2018 09:38:45 -0700 (PDT) X-Google-Smtp-Source: AG47ELt16ZkMsZm4mEp3jMCVyyBlCfFlkOp8z8xQzGSJks3AKEmRfagDYmnhsuphwUkUss7SikhF X-Received: by 2002:a17:902:6b4b:: with SMTP id g11-v6mr8619495plt.273.1521131925774; Thu, 15 Mar 2018 09:38:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131925; cv=none; d=google.com; s=arc-20160816; b=mcj4btH3VJbROaIloYvDeww1PzntCUnr6/EB7tl1dvJRiE6yExJs2trD12f/4jtQOt N7qxlmcgDND48LdWb7hRGrYuB7uXMD5NA2YJ6b0XwuJcxvjOwYvg/mWgtDORkDHLAI5Y 3abliwHBpdZgStI2ovVc5QaEIHOcy6YxR3Qm+RLDxNvf1l1aml/l1ZpM2PSIx8NvdNj4 kc+CrRex9sVb5oBYKPFL04V8O9u9LUXqGjgB52wHzuL7KKZc5q8tycQ9icNiUfWyLolc m4gvxSYpJ8hTekn3hiVFnBO9eVE1BjcQ2M8qx+hZ97hnT1IUatMyJG3VL4W83J+Sq7nI xKTg== 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=J525HwImsqwJxkjLqx5vezbJxVH3kMPVSQ9OH1Bel0s=; b=PDXsmzS1bZUmKJftWdVYpf16bjz8SjOYQLUAK6LlnhTq3LVVcVAm+OCeQrxT/NS/+s Fc00P+hx4MsZrgjxklnFnN2L1D1idga0r4TU3hgcHka/3sOoEDZzVxf1UgrJgRsjCFgl jtsxs3KhZ5lYkOS2d0r/qAhxvzEfOUQ17MabH5Vzf35eWQimh2w+makk0ijAdOTxiAR+ B1JlX8Zh6vhTgKBr54aOHstf/xaN5o7qztgnlcqQ2G3MOOcJDdQts/qLGiaX1JPGa09F KKwmZq1ZSwYhLn+wz2BbHclO3CtaxhWLHAS44I4VVbTxB4Z9BIQgNoaGbmOHulYLjVv/ ey0A== 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 x3-v6si4323415plv.81.2018.03.15.09.38.45; Thu, 15 Mar 2018 09:38:45 -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 S1752357AbeCOQi0 (ORCPT + 28 others); Thu, 15 Mar 2018 12:38:26 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:39694 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752163AbeCOQiN (ORCPT ); Thu, 15 Mar 2018 12:38:13 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 61BA431831A30; Fri, 16 Mar 2018 00:37:59 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:37:51 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v5 3/7] vfio/type1: Update iova list on detach Date: Thu, 15 Mar 2018 16:35:05 +0000 Message-ID: <20180315163509.17740-4-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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 cfe2bb2..25e6920 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1667,12 +1667,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); @@ -1695,6 +1771,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) @@ -1720,10 +1802,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 Thu Mar 15 16:35: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: 131821 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1698307edg; Thu, 15 Mar 2018 09:38:59 -0700 (PDT) X-Google-Smtp-Source: AG47ELuCg6CR+VShbvmPSZLVLGkHwi9pqQeJQedJn+9owcphyVmQ+gTvnkan7PXsRRtzeFmmPbt5 X-Received: by 2002:a17:902:69ce:: with SMTP id m14-v6mr7812505pln.358.1521131939402; Thu, 15 Mar 2018 09:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131939; cv=none; d=google.com; s=arc-20160816; b=ScuQfdXhAERHifPHHILBzmLUfZQtGgdutYQQXCU1WWwo84YU956raXFOoip2JE27uB /ilYO5l7ndbAAZbTrG5euE5unQhuoZd9kkXGcKBQrw6iqaSFxNVNVTPLOpS/cg4tciZi sVa73lu1yn/xSWpYg+sQznUmtuC84EiBjwBFKLyXBanx7+5BhpvIu7d3vzjiqUO0OxYH jJ7MX1La7mQJyvEzUXw1L+E10mMdQeA13inq/roma52+DSZpQNrZ5KanP8APq4CSyajp NgvQDBP3l6EApg02TrerBc2YDpVOjcHw70HScxQqqror6rVKoSCsKzhwxj4caAnfFQgj uUAA== 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=c4bEcsrt7QIT/XX1gs7dW/M+sYF/aSEifXn0VsAwbBA=; b=HqUB3BwjmcjBNaqTt1XlQtjUgusA74MUC2pOFk6A/HYAxHmg07TuKyLsHxo+exhnwY X9GAYpB8Ih9/dV/pf4bWynukVfCQgo4xuF7yMG0LmMkqHxbZS116T6AzBOf97wydm1ZU Pb82F04qqp3VgS3omaJKiuoakdlYDAg1Clh7aeAhV1HGXfzqfd0XfQAkRXsc4/6VZnQ/ ahkOEYs5pQhR7/qFgRYEyYncH0xSztul+lRuOeHYcT/+NyeAM3TtKI5wdVKf/lX5G7Wt /XgYl1V645lxpYB6FbK/GfscrA4LNwtCPvhi3YDBME6vfUi1FoU4eAlxNcsOsa+ETIym 74Tw== 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 b10si3674019pgn.140.2018.03.15.09.38.59; Thu, 15 Mar 2018 09:38:59 -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 S1752333AbeCOQiY (ORCPT + 28 others); Thu, 15 Mar 2018 12:38:24 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:39687 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752158AbeCOQiN (ORCPT ); Thu, 15 Mar 2018 12:38:13 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 538006AB55BD0; Fri, 16 Mar 2018 00:37:59 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:37:54 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v5 4/7] vfio/type1: check dma map request is within a valid iova range Date: Thu, 15 Mar 2018 16:35:06 +0000 Message-ID: <20180315163509.17740-5-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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 25e6920..d59db31 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -982,6 +982,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) { @@ -1020,6 +1037,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 Thu Mar 15 16:35: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: 131818 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1697546edg; Thu, 15 Mar 2018 09:38:18 -0700 (PDT) X-Google-Smtp-Source: AG47ELvrRqD8rgj0cOVdhkzbvGjnriHosBpv3NOSr2XO7CxnQ4jfrTJMJ5CpinDJgJyr0UaBcusW X-Received: by 10.99.119.133 with SMTP id s127mr7134615pgc.441.1521131898281; Thu, 15 Mar 2018 09:38:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131898; cv=none; d=google.com; s=arc-20160816; b=bEYsysJcP5p9bz98KMF8lTaCi/h1KmkrmBsXK2xGEjLBwXM/+Zk+9RZKqYJhECySSo U1i0SOyXpWV0RQHzA5moYfXjbKWtZqQKzefArCPhsEcvLjaDWmktsBASCQwkU6mDlNPp UOGnVdIKuAOciFSGRdfCc1EGkhfClhPqV4omclXDQMLoCBbf3b0f8AhoUvzFkxORbeTt xa9k5DVrMVXO/w9/7M8wnluoYlNW4TgoJGY4YociQttOWlqtiimgNmJ04pmINdWx6+bu w1I+LdZ7KoNz+p+BxrtaLZJEwlrwveVfnP4pI8B2kKA3BUI+CvZwpD3pnXAnLh/JilDD T0UQ== 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=A5w9DfhpVdF10FFaF1cLqEJfo/FKads5hJjPdOUYD74=; b=fUt0sM+wDYhER5wrMyDKKc/hx9+qRDSB2g39V/z23Ii3YooYQOXJibEeFogUxvfFZn DUiLdtMWShNB3DYCpeAAFpZau/max3wOf7gt+TRy6bueStSPqISqsw5T8vp9AkiNpCwT kTAQMGjJq0h1POs2f8uTFyFoy6jteLOeQogXhUvL2lt/QMTek78So03ufb+UwdrMoyxt b46AJn2a4SQvUJcaVH2mqixir4Y2T0WjrHSUhO2j1EEQru4R9/J5qlQp4Eov3LXXD0NE NK3SSaCEIsDxQxQyHejAA87qZhijwCvi0H1m3y0lnSTzV3czDGjyDPvxyduT6RPnK8UQ fb1A== 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 e3-v6si4289825plb.100.2018.03.15.09.38.17; Thu, 15 Mar 2018 09:38:18 -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 S1752252AbeCOQiO (ORCPT + 28 others); Thu, 15 Mar 2018 12:38:14 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:35071 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752084AbeCOQiJ (ORCPT ); Thu, 15 Mar 2018 12:38:09 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id A5ED5565304CE; Fri, 16 Mar 2018 00:38:04 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:37:57 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v5 5/7] vfio/type1: Add IOVA range capability support Date: Thu, 15 Mar 2018 16:35:07 +0000 Message-ID: <20180315163509.17740-6-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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 d59db31..90f195d 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1929,6 +1929,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) { @@ -1950,19 +2012,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 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 Thu Mar 15 16:35: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: 131819 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1697897edg; Thu, 15 Mar 2018 09:38:36 -0700 (PDT) X-Google-Smtp-Source: AG47ELuzk6e5rR4h5YkzuMa2CbD1xuqvH+vw/F3BP2u2mqfhVCx85MKrg6dNK3f3Mn+7vGvXQb1c X-Received: by 10.99.127.27 with SMTP id a27mr7104354pgd.18.1521131916644; Thu, 15 Mar 2018 09:38:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131916; cv=none; d=google.com; s=arc-20160816; b=LRTb1CyXACBmBzNeeI7ByNGNAP1YAa0hZX1zCMi+6K/hGKoeNghfshgWQ9J6stop+2 heL1T6TOqB2mE1kr4Vb/ku0cetXGJlutoL+thEZNh/1a1mU1KK43X94RGxjFBIdTmZV5 dJ7BaPb4fuH5MdcctmzitNqhkeOu8yfh4HKdot9IS/ZOaB6neW9sGyb1f5O8X4xeZiIJ pPb5GHs6mK7ittcqv5WAn+MDEZjc5VmtDjm41meWWXKIT6LxuStUHAghQfMvrND0anK6 HYcZ9rKIAh1cS95ETZOfRpG9yPg+uKXWLKQOXw9Rvosw8kxTfex3PQ11K90KSlca/vf7 nt9Q== 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=8qYA8102MR1n5XzIH3SBzoouueIW1D3HmR00QjMzMgI=; b=fgPghsfHa/1VIXQaJ/YJ/sAB4vDaU4IKYxH71++xj3HgWJ67QYOBvGRk7YqghGyzaj 1Guz33RM/6xyN9NZeZ12RGG89tTjMpMPz/jzK0grvEojXprRQms8Ey4hIv6JaIC7YJ1i UiuB/TJeoavGh52L9WcBi4RoCRgC9RvKSYLCHaiHhVZQn3KPLQIHvdQ+Gl/RCPg2nQlB CE5gZfSD2BxhdShEuxBC//QvvLOE5/+5KdnOPevxMmL0HFJTXwkZnJKl0tOy3/H31bEZ LyMUSXjm/c2IaN300G3jISdP0StVWqSUAKH+A8veP2KH/MHYADrOSWYok67mCvrhq9jF W2dA== 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 x3-v6si4323415plv.81.2018.03.15.09.38.36; Thu, 15 Mar 2018 09:38:36 -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 S1752402AbeCOQi1 (ORCPT + 28 others); Thu, 15 Mar 2018 12:38:27 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:39780 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751829AbeCOQiX (ORCPT ); Thu, 15 Mar 2018 12:38:23 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 8DD0AB058D20B; Fri, 16 Mar 2018 00:38:09 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:38:00 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum Subject: [PATCH v5 6/7] vfio/type1: remove duplicate retrieval of reserved regions Date: Thu, 15 Mar 2018 16:35:08 +0000 Message-ID: <20180315163509.17740-7-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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 90f195d..65c13e7 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1204,15 +1204,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 @@ -1228,8 +1226,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; } @@ -1564,7 +1561,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); From patchwork Thu Mar 15 16:35: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: 131822 Delivered-To: patch@linaro.org Received: by 10.80.210.197 with SMTP id q5csp1698415edg; Thu, 15 Mar 2018 09:39:05 -0700 (PDT) X-Google-Smtp-Source: AG47ELu/ekELcGo+uHv4QMmpy2KhRMGpOLgZSht4HbJ5sSET5EuLqk40dT1bsE4NChdrJ2Ms9lSy X-Received: by 10.98.17.218 with SMTP id 87mr6556445pfr.160.1521131945116; Thu, 15 Mar 2018 09:39:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521131945; cv=none; d=google.com; s=arc-20160816; b=A4dGSNgqFrycmtvxvOkVfENA5TWyJPRr1G0ny+cf/NW0ZwlUOA/kQc6UhjnmvuMxjH pVbMk1uNw1Wcii1eEgWgtI1QAT9pvn4fTkZe06ax0hJWVJ37bZK2jPbw0N/XO/9qloeg PupqIrm+siiSGtqz0zBzLQPzL1OkErRQ83Q4pSNWAnZ+C0f34M0c9yD1fmG9PQCEzgp+ 8mPpP0XRU+F6YO4J13ur52pfeIcQEf96d7NogJRQZ3md6lTCULQTwtIfTHEZstOzR775 fB7f+sHdYcqFD40E7muH4IqnCOEOUdkXveOUHQjYuz9YUEVqGYjyPbYV6p/pxK+ZPMiK cIxA== 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=ufrWkm6JmbhfagCdX0iY9Oqw++NyGc4fz21T45wh/PI=; b=gUisQy2LPY5asvx6QsalC37/XFMTxIfKnrJvd7dffkmP/EvZXtCH7WoM9GyIkWHrMu DGm5TzOLnJXFH2kFEoS2pFai5hau2NayaE2eHPP7Yb5kO/pDRbGlPVj8lHZfUk9oDQw+ H69vUGjQd4flXp2h25+lFWmN7dWJH9uHaMxx0lh6nsFT5fvv4sv0mVeW7pJqTNwIkFyd f+wHlBJREBXUiU8wpPBQ87WIE+FvLtJ6OIqoJ+qCgbh3cWCEuuzB0IIu1/U8IFWvKS2p wZYcB2Z1zVozxY8PhI5E0fpoF8TM1VOiqXGj/KsQu2pb0GmbAEG0/ndRqv5ZQUmVXk+o 1D5g== 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 b10si3674019pgn.140.2018.03.15.09.39.04; Thu, 15 Mar 2018 09:39:05 -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 S1752471AbeCOQi7 (ORCPT + 28 others); Thu, 15 Mar 2018 12:38:59 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:39776 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752261AbeCOQiY (ORCPT ); Thu, 15 Mar 2018 12:38:24 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 7E21E4575D12F; Fri, 16 Mar 2018 00:38:09 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.361.1; Fri, 16 Mar 2018 00:38:04 +0800 From: Shameer Kolothum To: , , CC: , , , , , , Shameer Kolothum , Robin Murphy , Joerg Roedel Subject: [PATCH v5 7/7] iommu/dma: Move PCI window region reservation back into dma specific path. Date: Thu, 15 Mar 2018 16:35:09 +0000 Message-ID: <20180315163509.17740-8-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180315163509.17740-1-shameerali.kolothum.thodi@huawei.com> References: <20180315163509.17740-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: Robin Murphy Cc: Joerg Roedel Signed-off-by: Shameer Kolothum --- drivers/iommu/dma-iommu.c | 54 ++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) -- 2.7.4 Acked-by: Robin Murphy 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;