From patchwork Mon Jun 18 18:42:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10472565 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 311006029B for ; Mon, 18 Jun 2018 18:43:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 214C52040D for ; Mon, 18 Jun 2018 18:43:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 163DE27D16; Mon, 18 Jun 2018 18:43:50 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, 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 997572040D for ; Mon, 18 Jun 2018 18:43:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936020AbeFRSnq (ORCPT ); Mon, 18 Jun 2018 14:43:46 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:39290 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935995AbeFRSnd (ORCPT ); Mon, 18 Jun 2018 14:43:33 -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 B54DE15B2; Mon, 18 Jun 2018 11:43:33 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.210.39]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3E37B3F557; Mon, 18 Jun 2018 11:43:32 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org Cc: kvmarm@lists.cs.columbia.edu, will.deacon@arm.com, robin.murphy@arm.com, lorenzo.pieralisi@arm.com, marc.zyngier@arm.com, punit.agrawal@arm.com, alex.williamson@redhat.com Subject: [PATCH v6 kvmtool 13/13] vfio: check reserved regions before mapping DMA Date: Mon, 18 Jun 2018 19:42:11 +0100 Message-Id: <20180618184211.43904-14-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180618184211.43904-1-jean-philippe.brucker@arm.com> References: <20180618184211.43904-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. Reviewed-by: Punit Agrawal Signed-off-by: Jean-Philippe Brucker --- vfio/core.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/vfio/core.c b/vfio/core.c index a4a257a70..17b5b0cfc 100644 --- a/vfio/core.c +++ b/vfio/core.c @@ -379,6 +379,51 @@ static int vfio_unmap_mem_bank(struct kvm *kvm, struct kvm_mem_bank *bank, void return 0; } +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_groups(struct kvm *kvm) +{ + int ret; + struct vfio_group *group; + + list_for_each_entry(group, &vfio_groups, list) { + ret = vfio_configure_reserved_regions(kvm, group); + if (ret) + return ret; + } + + return 0; +} + static struct vfio_group *vfio_group_create(struct kvm *kvm, unsigned long id) { int ret; @@ -597,6 +642,10 @@ static int vfio__init(struct kvm *kvm) if (ret) return ret; + ret = vfio_configure_groups(kvm); + if (ret) + return ret; + ret = vfio_configure_devices(kvm); if (ret) return ret;