From patchwork Wed Feb 21 12:22:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shameerali Kolothum Thodi X-Patchwork-Id: 129057 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp561901ljc; Wed, 21 Feb 2018 04:24:24 -0800 (PST) X-Google-Smtp-Source: AH8x226xaBT0Q3pFyVHYwv8cftb6xstH4KYjIHRnyunmuQ61EAem9rDwuTUP/lv0Tm0WsdhnzxAX X-Received: by 10.99.114.18 with SMTP id n18mr2573147pgc.169.1519215863899; Wed, 21 Feb 2018 04:24:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519215863; cv=none; d=google.com; s=arc-20160816; b=OLiDrH5kL4NkwtF54QK0WecX7O+IybbBfx0ipqJ4rVZDHKuFEL9kmccxfTTc+UcDuB KNUGMCVrzLZjWKXAYwkKRWqzizKBmxaVv9xBJVoGYGTw2fKArF8UZcOR+LuLHaVjXzIx NA6lsHA3kuxq4sjLMzKkx6yN9f/6bhGK0TZZyi0BxxCcuNCkpLthBbSIit/m0Xr+CiiT 8kXx+yebYCvvzywOoB9vKLva+pC3+IDhNnL5YZMDmsFqSIAqSUObkImIPdvAYLcQHQP8 jj/BMyEGtAEQb9PAJEyPRbwduaiYgsOWGAcUUzIwP16NwST480Sgr9NrhzuJfyvRLLI5 Ul1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=5znM/8nt8qpmZWdPLgL0u0B9qJ80eVEnMlLKH3WlHio=; b=oleBA2k47j5eccV0p27VAdfPV/SveJCBK7kSnd5dh8QmusmWeyxp3Df/nZaNyySMD7 t3B1glOcNjqF7ZBYjeWZVT8q33lp5iDeY2hzSb2RtJspksouovjXZFtPMnlfT7p7zY79 /MUg1RHr7VBJYGMpB4IHcgAyEUgeA7+HA8aVcfFi/cKK383RbhSmsBTlFWs6ZKvmTXFm uczIXZhlHBntsVxtO+i2iSbJEo3rdfpQEzdA0G0ph99nkNvqKA0QF5d51+5JCMsyt1bJ qjs9jMaU4NAbMTTjJunT5IYzo6bnx8+QAm8KlM4XUnXTDvLr1Bk3+4qYe7TZTDAfqRX5 z3kg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n129si1077655pga.260.2018.02.21.04.24.23; Wed, 21 Feb 2018 04:24:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754208AbeBUMYV (ORCPT + 28 others); Wed, 21 Feb 2018 07:24:21 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:5656 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754063AbeBUMXk (ORCPT ); Wed, 21 Feb 2018 07:23:40 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 7E3A31C04D506; Wed, 21 Feb 2018 20:23:25 +0800 (CST) Received: from S00345302A-PC.china.huawei.com (10.202.227.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.361.1; Wed, 21 Feb 2018 20:23:16 +0800 From: Shameer Kolothum To: , , CC: , , , , , Shameer Kolothum Subject: [PATCH v4 3/6] vfio/type1: Update iova list on detach Date: Wed, 21 Feb 2018 12:22:06 +0000 Message-ID: <20180221122209.9292-4-shameerali.kolothum.thodi@huawei.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> References: <20180221122209.9292-1-shameerali.kolothum.thodi@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.202.227.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Get a copy of iova list on _group_detach and try to update the list. On success replace the current one with the copy. Leave the list as it is if update fails. Signed-off-by: Shameer Kolothum --- drivers/vfio/vfio_iommu_type1.c | 91 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) -- 2.7.4 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 66c57ee..a80884e 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1655,12 +1655,88 @@ static void vfio_sanity_check_pfn_list(struct vfio_iommu *iommu) WARN_ON(iommu->notifier.head); } +/* + * Called when a domain is removed in detach. It is possible that + * the removed domain decided the iova aperture window. Modify the + * iova aperture with the smallest window among existing domains. + */ +static void vfio_iommu_aper_expand(struct vfio_iommu *iommu, + struct list_head *iova_copy) +{ + struct vfio_domain *domain; + struct iommu_domain_geometry geo; + struct vfio_iova *node; + dma_addr_t start = 0; + dma_addr_t end = (dma_addr_t)~0; + + list_for_each_entry(domain, &iommu->domain_list, next) { + iommu_domain_get_attr(domain->domain, DOMAIN_ATTR_GEOMETRY, + &geo); + if (geo.aperture_start > start) + start = geo.aperture_start; + if (geo.aperture_end < end) + end = geo.aperture_end; + } + + /* Modify aperture limits. The new aper is either same or bigger */ + node = list_first_entry(iova_copy, struct vfio_iova, list); + node->start = start; + node = list_last_entry(iova_copy, struct vfio_iova, list); + node->end = end; +} + +/* + * Called when a group is detached. The reserved regions for that + * group can be part of valid iova now. But since reserved regions + * may be duplicated among groups, populate the iova valid regions + * list again. + */ +static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu, + struct list_head *iova_copy) +{ + struct vfio_domain *d; + struct vfio_group *g; + struct vfio_iova *node; + dma_addr_t start, end; + LIST_HEAD(resv_regions); + int ret; + + list_for_each_entry(d, &iommu->domain_list, next) { + list_for_each_entry(g, &d->group_list, next) + iommu_get_group_resv_regions(g->iommu_group, + &resv_regions); + } + + if (list_empty(&resv_regions)) + return 0; + + node = list_first_entry(iova_copy, struct vfio_iova, list); + start = node->start; + node = list_last_entry(iova_copy, struct vfio_iova, list); + end = node->end; + + /* purge the iova list and create new one */ + vfio_iommu_iova_free(iova_copy); + + ret = vfio_iommu_aper_resize(iova_copy, start, end); + if (ret) + goto done; + + /* Exclude current reserved regions from iova ranges */ + ret = vfio_iommu_resv_exclude(iova_copy, &resv_regions); +done: + vfio_iommu_resv_free(&resv_regions); + return ret; +} + static void vfio_iommu_type1_detach_group(void *iommu_data, struct iommu_group *iommu_group) { struct vfio_iommu *iommu = iommu_data; struct vfio_domain *domain; struct vfio_group *group; + bool iova_copy_fail; + LIST_HEAD(iova_copy); mutex_lock(&iommu->lock); @@ -1683,6 +1759,12 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, } } + /* + * Get a copy of iova list. If success, use copy to update the + * list and to replace the current one. + */ + iova_copy_fail = !!vfio_iommu_iova_get_copy(iommu, &iova_copy); + list_for_each_entry(domain, &iommu->domain_list, next) { group = find_iommu_group(domain, iommu_group); if (!group) @@ -1708,10 +1790,19 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, iommu_domain_free(domain->domain); list_del(&domain->next); kfree(domain); + if (!iova_copy_fail && !list_empty(&iommu->domain_list)) + vfio_iommu_aper_expand(iommu, &iova_copy); } break; } + if (!iova_copy_fail && !list_empty(&iommu->domain_list)) { + if (!vfio_iommu_resv_refresh(iommu, &iova_copy)) + vfio_iommu_iova_insert_copy(iommu, &iova_copy); + else + vfio_iommu_iova_free(&iova_copy); + } + detach_group_done: mutex_unlock(&iommu->lock); }