From patchwork Thu Feb 15 09:45:00 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: 128400 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp1558418ljc; Thu, 15 Feb 2018 01:48:39 -0800 (PST) X-Google-Smtp-Source: AH8x225i0O60QRMYg3roB9n9Oz+Wt3g3ZBqgG8inHYaLAC7pCLSNatYzM1+ms5AYI9DawpqH3Gr9 X-Received: by 10.101.93.142 with SMTP id f14mr1694234pgt.82.1518688119530; Thu, 15 Feb 2018 01:48:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518688119; cv=none; d=google.com; s=arc-20160816; b=dxxdsmh25BZrN2mXQpgNwuprvTc18eaxP0CHYbDVeGw+xOCHwF8qNnXSMMePxXShFa 8qtuDCn5zR2mCGLUwTmUYLjR2ny4+dpHjReo9813wR4pZGyj2Kg+AQhxiChJuMQK081r crLChogakWvBryFKpkdFN4WqLMUTD++PIcOGIi8Eyqbzr+5cQbGl74v9Y/ZboFBGQKUy /AEWCbrFaRzhOrWt0y24KbSUzn00nGpfdgSZ3hIpcMQMwHa0gZmzXwNNfZp2tShhXY0u 6CjrS4jxLMiHaqEkljKfkg69Rk9qhaRtmZFczW/ihZnYOBvAkabDKuZ71mvJLuzfqo50 u9eA== 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=MSub6yClrvAfx30XTilWp+YqFOKVLWxuSzH++4RSZKQ=; b=NatzcBW7O7X6eMH5BhpdZZNfZctWydgH1GTvdSUN6eb6v5QSY7wTAsCv5EyxxPNaTM tE8+ENhwYxfPTtZOPkoGwzCnAAcOLp9YenZgZehDjIoTjli7xUC0/pbAtkKWuGnHW6mx cZEmRnXYrNHnJmi3qkS/eoY9DJ7ErJVVKgVfN/dn2nr2zMkb3c3xgnxiwQMtAJm0zUKV DIJoDH/p1JSsm8yDEcOF7IZjWdnkX8JrM8805I8qwGvBo/HSPS/hYwVYBagwGTBEfBhD +JQ/8YBqEDUfvRBYhzKhY43S8/SaowfJ/GDFmCHh/B5M+GgSWakmH+CkYn3tVOIvDCr3 FpXA== 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 x189si1096674pgb.216.2018.02.15.01.48.39; Thu, 15 Feb 2018 01:48:39 -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 S1755375AbeBOJse (ORCPT + 28 others); Thu, 15 Feb 2018 04:48:34 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:59926 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S966892AbeBOJrV (ORCPT ); Thu, 15 Feb 2018 04:47:21 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id CB1522DC571C7; Thu, 15 Feb 2018 17:47:07 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.361.1; Thu, 15 Feb 2018 17:46:58 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v3 2/6] vfio/type1: Check reserve region conflict and update iova list Date: Thu, 15 Feb 2018 09:45:00 +0000 Message-ID: <20180215094504.4972-3-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180215094504.4972-1-shameerali.kolothum.thodi@huawei.com> References: <20180215094504.4972-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 | 86 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 4726f55..4db87a9 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1303,6 +1303,72 @@ 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_insert_iova(n->start, start - 1, + &n->list); + if (!ret && end < n->end) + ret = vfio_insert_iova(end + 1, n->end, + &n->list); + if (ret) + return ret; + + list_del(&n->list); + kfree(n); + } + } + + if (list_empty(iova)) + return -EINVAL; + + return 0; +} + static int vfio_iommu_get_iova_copy(struct vfio_iommu *iommu, struct list_head *iova_copy) { @@ -1346,7 +1412,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 iova_copy; + struct list_head iova_copy, group_resv_regions; + struct iommu_resv_region *resv, *resv_next; struct vfio_iova *iova, *iova_next; mutex_lock(&iommu->lock); @@ -1426,6 +1493,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_detach; } + INIT_LIST_HEAD(&group_resv_regions); + 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 */ INIT_LIST_HEAD(&iova_copy); ret = vfio_iommu_get_iova_copy(iommu, &iova_copy); @@ -1437,6 +1512,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); @@ -1497,6 +1576,9 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, /* Delete the old one and insert new iova list */ vfio_iommu_insert_iova_copy(iommu, &iova_copy); + list_for_each_entry_safe(resv, resv_next, &group_resv_regions, list) + kfree(resv); + mutex_unlock(&iommu->lock); return 0; @@ -1507,6 +1589,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, iommu_domain_free(domain->domain); list_for_each_entry_safe(iova, iova_next, &iova_copy, list) kfree(iova); + list_for_each_entry_safe(resv, resv_next, &group_resv_regions, list) + kfree(resv); out_free: kfree(domain); kfree(group);