From patchwork Wed Feb 17 17:27:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12091953 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C64BDC43381 for ; Wed, 17 Feb 2021 17:31:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9393264DCF for ; Wed, 17 Feb 2021 17:31:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233571AbhBQRbY (ORCPT ); Wed, 17 Feb 2021 12:31:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232222AbhBQRbN (ORCPT ); Wed, 17 Feb 2021 12:31:13 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1F8DC061574 for ; Wed, 17 Feb 2021 09:30:33 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 194so12809858ybl.5 for ; Wed, 17 Feb 2021 09:30:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=jHfqonvlMKl1wsgB/F2W/qoXTxBiDKQ8+KEhAtz6vqY=; b=PzcnGWb+9YlawHHBzCy0ixjJsg6CAcCZLbYZJPCjxkNLvmnH4MzE58JTFFCBzx1IoZ IeMKw+kpNLMNSMaqCBQd6PH+TUwrSLCXGcqfEvUXmNgdEPa21PmkkBqpLDwA/tC2uRGT 1K66VUnyqGxYkxZ5qtIR5idf035Rwo2ROgzhEX8FdTuLBSqXUhuA23b+JqhrIkpiXtyz S1SG6kOWbk7rby4JMKYwTC4ABRaFmev+e8uYejP3K2HVjawu/km+bOD3WdGemy3jWIwG azGtQzru9zBdL4aO3SIeZMTEF6qz4z9vbHtTSyyQle9J+BMqBV2d9BhXclosuYX3JFcQ 8pgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=jHfqonvlMKl1wsgB/F2W/qoXTxBiDKQ8+KEhAtz6vqY=; b=dzszYmk/Rvw6krRkJgsEn5FVnmY1XmfvQC91w0rOc1RaWZ3b8WU2aFz3OiFc53jcq+ vrjl1ebke9hfBj+YAf3lpPFfoweloW6ccx6Qs6MFnz5eeqPIF8EallqUeN3lbpXV4PEU LCqnJkv3PwXyOV0gt0Ec21tCaGgE/EKfw+H7B07+wDQuvK7emDxgVtkHpDuAL35/RdCe kzztOtMDPOp0D/OExIiE/1tR+1OKS2dWESjqAPke5Wr5Zja8XfQVIl773SqofY0ngHs9 07TV2bvQLnb9BwiGF4+VGBvJ1TXUwRbIyd50AXY3hrVLnmGV2ICX4gf08ALPkq4acn1E qXtw== X-Gm-Message-State: AOAM53158gOSFt5jzi+RQJ7dBR+H9NbXFmw/UgMYiI69XMhEY+d8p03x 7wnNPCVxs6qeVXk7Ic+WoVLFsrLM69hc7TkECDTzJKaZMP1RIUcV/w1S8IA9K3X36tShh+QT7/J jqrY1HahNR7WzPWtEmGZCLswJLGVwEA5Nj02mT6XbFpJ7G6UJy1bTvebdWRlZ7GKsosGy X-Google-Smtp-Source: ABdhPJykAOfKVkSiVgyo1aPX35HYdsLlFd9CbcWO7jIpaPUMALw7DAoA8kQIytI/+7X/9Sh/PjZB7o9j2mN6JY1f Sender: "aaronlewis via sendgmr" X-Received: from aaronlewis1.sea.corp.google.com ([2620:15c:100:202:95fa:4161:5a32:e12]) (user=aaronlewis job=sendgmr) by 2002:a25:af52:: with SMTP id c18mr761621ybj.196.1613583032910; Wed, 17 Feb 2021 09:30:32 -0800 (PST) Date: Wed, 17 Feb 2021 09:27:31 -0800 Message-Id: <20210217172730.1521644-1-aaronlewis@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog Subject: [PATCH v2] selftests: kvm: Mmap the entire vcpu mmap area From: Aaron Lewis To: kvm@vger.kernel.org Cc: seanjc@google.com, Aaron Lewis , Steve Rutherford , Andrew Jones Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The vcpu mmap area may consist of more than just the kvm_run struct. Allocate enough space for the entire vcpu mmap area. Without this, on x86, the PIO page, for example, will be missing. This is problematic when dealing with an unhandled exception from the guest as the exception vector will be incorrectly reported as 0x0. Co-developed-by: Steve Rutherford Signed-off-by: Steve Rutherford Signed-off-by: Aaron Lewis Reviewed-by: Andrew Jones Reviewed-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/kvm_util.c | 66 +++++++++++----------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index fa5a90e6c6f0..a2874e366d0f 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -486,6 +486,37 @@ struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid) return NULL; } +/* + * VCPU mmap Size + * + * Input Args: None + * + * Output Args: None + * + * Return: + * Size of VCPU state + * + * Returns the size of the structure pointed to by the return value + * of vcpu_state(). + */ +static int vcpu_mmap_sz(void) +{ + int dev_fd, ret; + + dev_fd = open(KVM_DEV_PATH, O_RDONLY); + if (dev_fd < 0) + exit(KSFT_SKIP); + + ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL); + TEST_ASSERT(ret >= sizeof(struct kvm_run), + "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i", + __func__, ret, errno); + + close(dev_fd); + + return ret; +} + /* * VM VCPU Remove * @@ -509,7 +540,7 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct vcpu *vcpu) vcpu->dirty_gfns = NULL; } - ret = munmap(vcpu->state, sizeof(*vcpu->state)); + ret = munmap(vcpu->state, vcpu_mmap_sz()); TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i " "errno: %i", ret, errno); close(vcpu->fd); @@ -909,37 +940,6 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot) __vm_mem_region_delete(vm, memslot2region(vm, slot)); } -/* - * VCPU mmap Size - * - * Input Args: None - * - * Output Args: None - * - * Return: - * Size of VCPU state - * - * Returns the size of the structure pointed to by the return value - * of vcpu_state(). - */ -static int vcpu_mmap_sz(void) -{ - int dev_fd, ret; - - dev_fd = open(KVM_DEV_PATH, O_RDONLY); - if (dev_fd < 0) - exit(KSFT_SKIP); - - ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL); - TEST_ASSERT(ret >= sizeof(struct kvm_run), - "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i", - __func__, ret, errno); - - close(dev_fd); - - return ret; -} - /* * VM VCPU Add * @@ -978,7 +978,7 @@ void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid) TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size " "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi", vcpu_mmap_sz(), sizeof(*vcpu->state)); - vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state), + vcpu->state = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(), PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0); TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, " "vcpu id: %u errno: %i", vcpuid, errno);