From patchwork Wed Oct 28 13:12:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 55684 Delivered-To: patches@linaro.org Received: by 10.112.61.134 with SMTP id p6csp112168lbr; Wed, 28 Oct 2015 06:12:57 -0700 (PDT) X-Received: by 10.180.108.13 with SMTP id hg13mr2836911wib.91.1446037977833; Wed, 28 Oct 2015 06:12:57 -0700 (PDT) Return-Path: Received: from mail-wi0-x22d.google.com (mail-wi0-x22d.google.com. [2a00:1450:400c:c05::22d]) by mx.google.com with ESMTPS id v184si5116197wmd.49.2015.10.28.06.12.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Oct 2015 06:12:57 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c05::22d as permitted sender) client-ip=2a00:1450:400c:c05::22d; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c05::22d as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro_org.20150623.gappssmtp.com Received: by wikq8 with SMTP id q8so252267824wik.1 for ; Wed, 28 Oct 2015 06:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=L1Fn2llcfRU+2ma2dYB2Eme//g4qOOp11ZWEqtcWLEw=; b=Udcu8+9Y/fBY8QHSm4KyZtmWkAL7wSUyW+TLXicByoWlLqZFKOKXczGNJRoM7I1IVw syVtH/LmxY56WkC+l8dB2Zdzu79klcpTVayKmCfRWZoNUMoXI1xGjBWO23wGKZIepoVH ursvxL9Xkkv0Q2hLA+0nthDjCTPWg7AKFDSzp+Jfmp3rLeAdxH+GKdY/xj1ibpzwv61t ojwNsxHorlxOBer5zfLU2AzmsJ4pdF6u4I9gNmCYHxtkTnOsbWXVB1X1W/FhpqEo50Oo x3EbGAvpx73dB53EL+3+oLcpwc0DLYjPjYbV9cpi2MFk34Go88iZSJ3Cf8ci5Nq+9iYD x6oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=L1Fn2llcfRU+2ma2dYB2Eme//g4qOOp11ZWEqtcWLEw=; b=bdbchHiAsftUirAlNZeX7iiKNPNdub9XY+Uloi0T7I8kE0VTthrJ+wzwpy855K4Rms X2NDW6ehiqkOTsgisx1wQSJhfePPRUeDg+xVdat9S8DVfrx8oN8VbWYkmPqusabUynNW nLhPjiEgU3Yfm0qGuA9KqhoNaVPzQ+IauTAPFWUzWnPsMh2jWSzD6/plG61e9rqZCIKt 9qpYbnbbtT9abUt+telEqjh2Es0zLsh35PD1LjPdqBLIgU5HOIntjqHOSleQgVGGlFZI 2zjLgoAJTEIlpJoJvU+3M8dkdNx44bf41VGJniTMSMgwn313VPzmHKO8lhRSZsf7FZdq ScUQ== X-Gm-Message-State: ALoCoQmWo6iIco8DDTWrxV0kCGtYT4ZylHYtNEa5dXP3CkgrBOW4oMpDLgxCbNb9sf/c8EL0g7Cs X-Received: by 10.180.211.109 with SMTP id nb13mr2709377wic.52.1446037977605; Wed, 28 Oct 2015 06:12:57 -0700 (PDT) Return-Path: Received: from new-host-2.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id bk2sm21177776wjc.3.2015.10.28.06.12.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 28 Oct 2015 06:12:56 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, alex.williamson@redhat.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, will.deacon@arm.com Cc: suravee.suthikulpanit@amd.com, christoffer.dall@linaro.org, linux-kernel@vger.kernel.org, patches@linaro.org Subject: [RFC] vfio/type1: handle case where IOMMU does not support PAGE_SIZE size Date: Wed, 28 Oct 2015 13:12:45 +0000 Message-Id: <1446037965-2341-1-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 Current vfio_pgsize_bitmap code hides the supported IOMMU page sizes smaller than PAGE_SIZE. As a result, in case the IOMMU does not support PAGE_SIZE page, the alignment check on map/unmap is done with larger page sizes, if any. This can fail although mapping could be done with pages smaller than PAGE_SIZE. vfio_pgsize_bitmap is modified to expose the IOMMU page sizes, supported by all domains, even those smaller than PAGE_SIZE. The alignment check on map is performed against PAGE_SIZE if the minimum IOMMU size is less than PAGE_SIZE or against the min page size greater than PAGE_SIZE. Signed-off-by: Eric Auger --- This was tested on AMD Seattle with 64kB page host. ARM MMU 401 currently expose 4kB, 2MB and 1GB page support. With a 64kB page host, the map/unmap check is done against 2MB. Some alignment check fail so VFIO_IOMMU_MAP_DMA fail while we could map using 4kB IOMMU page size. --- drivers/vfio/vfio_iommu_type1.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) -- 1.9.1 diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 57d8c37..13fb974 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -403,7 +403,7 @@ static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma) static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) { struct vfio_domain *domain; - unsigned long bitmap = PAGE_MASK; + unsigned long bitmap = ULONG_MAX; mutex_lock(&iommu->lock); list_for_each_entry(domain, &iommu->domain_list, next) @@ -416,20 +416,18 @@ static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu) static int vfio_dma_do_unmap(struct vfio_iommu *iommu, struct vfio_iommu_type1_dma_unmap *unmap) { - uint64_t mask; struct vfio_dma *dma; size_t unmapped = 0; int ret = 0; + unsigned int min_pagesz = __ffs(vfio_pgsize_bitmap(iommu)); + unsigned int requested_alignment = (min_pagesz < PAGE_SIZE) ? + PAGE_SIZE : min_pagesz; - mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1; - - if (unmap->iova & mask) + if (!IS_ALIGNED(unmap->iova, requested_alignment)) return -EINVAL; - if (!unmap->size || unmap->size & mask) + if (!unmap->size || !IS_ALIGNED(unmap->size, requested_alignment)) return -EINVAL; - WARN_ON(mask & PAGE_MASK); - mutex_lock(&iommu->lock); /* @@ -553,25 +551,24 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, size_t size = map->size; long npage; int ret = 0, prot = 0; - uint64_t mask; struct vfio_dma *dma; unsigned long pfn; + unsigned int min_pagesz = __ffs(vfio_pgsize_bitmap(iommu)); + unsigned int requested_alignment = (min_pagesz < PAGE_SIZE) ? + PAGE_SIZE : min_pagesz; /* Verify that none of our __u64 fields overflow */ if (map->size != size || map->vaddr != vaddr || map->iova != iova) return -EINVAL; - mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1; - - WARN_ON(mask & PAGE_MASK); - /* READ/WRITE from device perspective */ if (map->flags & VFIO_DMA_MAP_FLAG_WRITE) prot |= IOMMU_WRITE; if (map->flags & VFIO_DMA_MAP_FLAG_READ) prot |= IOMMU_READ; - if (!prot || !size || (size | iova | vaddr) & mask) + if (!prot || !size || + !IS_ALIGNED(size | iova | vaddr, requested_alignment)) return -EINVAL; /* Don't allow IOVA or virtual address wrap */