From patchwork Tue Oct 31 19:14:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10035269 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 458B8602B9 for ; Tue, 31 Oct 2017 19:16:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 37F2128AA7 for ; Tue, 31 Oct 2017 19:16:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2CE2C28AFB; Tue, 31 Oct 2017 19:16:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A6EA128AF6 for ; Tue, 31 Oct 2017 19:16:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933077AbdJaTQc (ORCPT ); Tue, 31 Oct 2017 15:16:32 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:40882 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933083AbdJaTQU (ORCPT ); Tue, 31 Oct 2017 15:16:20 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0BF1A1596; Tue, 31 Oct 2017 12:16:20 -0700 (PDT) Received: from e106794-lin.cambridge.arm.com (e106794-lin.cambridge.arm.com [10.1.210.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id CE3403F3E1; Tue, 31 Oct 2017 12:16:18 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org Cc: will.deacon@arm.com, robin.murphy@arm.com, lorenzo.pieralisi@arm.com, marc.zyngier@arm.com, punit.agrawal@arm.com Subject: [PATCH v3 kvmtool 11/11] vfio: check reserved regions before mapping DMA Date: Tue, 31 Oct 2017 19:14:49 +0000 Message-Id: <20171031191449.1950-12-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171031191449.1950-1-jean-philippe.brucker@arm.com> References: <20171031191449.1950-1-jean-philippe.brucker@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use the new reserved_regions API to ensure that RAM doesn't overlap any reserved region. This prevents for instance from mapping an MSI doorbell into the guest IPA space. For the moment we reject any overlapping. In the future, we might carve reserved regions out of the guest physical space. Signed-off-by: Jean-Philippe Brucker --- vfio/core.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/vfio/core.c b/vfio/core.c index 7e1ba7896df2..10b67f6d6343 100644 --- a/vfio/core.c +++ b/vfio/core.c @@ -84,6 +84,37 @@ void vfio_unmap_region(struct kvm *kvm, struct vfio_region *region) munmap(region->host_addr, region->info.size); } +static int vfio_configure_reserved_regions(struct kvm *kvm, + struct vfio_group *group) +{ + FILE *file; + int ret = 0; + char type[9]; + char filename[PATH_MAX]; + unsigned long long start, end; + + snprintf(filename, PATH_MAX, IOMMU_GROUP_DIR "/%lu/reserved_regions", + group->id); + + /* reserved_regions might not be present on older systems */ + if (access(filename, F_OK)) + return 0; + + file = fopen(filename, "r"); + if (!file) + return -errno; + + while (fscanf(file, "0x%llx 0x%llx %8s\n", &start, &end, type) == 3) { + ret = kvm__reserve_mem(kvm, start, end - start + 1); + if (ret) + break; + } + + fclose(file); + + return ret; +} + static int vfio_configure_device(struct kvm *kvm, struct vfio_group *group, const char *dirpath, const char *name) { @@ -196,6 +227,10 @@ static int vfio_configure_iommu_groups(struct kvm *kvm) return ret; } + ret = vfio_configure_reserved_regions(kvm, group); + if (ret) + return ret; + if (closedir(dir)) pr_warning("Failed to close IOMMU group %s", dirpath); }