Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2740127pxj; Mon, 31 May 2021 09:32:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLESUol+g9VLzhRodesqQi0T/eKi1WjBCuBumnfvG0cIK/NsYGF5IRAmusSspCHHehIsdX X-Received: by 2002:a92:c243:: with SMTP id k3mr18428970ilo.81.1622478754367; Mon, 31 May 2021 09:32:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622478754; cv=none; d=google.com; s=arc-20160816; b=ZvKcniFi+Xlz29CPpTmSDywyIfw7YHtZe035I2MLDSV7NFYmTVpQOsv+BecwvisTmv sajbHUE7ihkNzltWk6SEZj8cthqXeBOGz7JmmvXNFfWkeabZx/FK0GyG2YJvJpj+Bb9r hxbjsLj9pu0oV8XRsCRloo91jSxp0Z21mBMFO8JXX7XzWVtphnqnKjHrhDt/TVyGKgd8 Ewzx78gHYTLzuHjLBMT0V7RtioSuc+l1vVtAyKnzV7vACD+vhOV+V8/KDf1PXL6lPl22 kWkUWtBOcnyryJeahJOmOw5B/Txn6rz61uvTlC6/+nujqGb9yFhJYfxh4a7Pi6kkrFkL 8vew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=aWY2hDbKydpxrqddzbut/S9WY3I5QbBxklA0P+MAEEk=; b=iP614gE67G2Q3h839junWgxL5xTN8yEF7gFXDikGcsAcXwR+dXNihSBKTxmax7Kvpj bWUhieSiqA2hEYlVLBMVu7QtdmEEV2P0Ur7dvOC1uwg+blHf5PcCDbGgR8Wd6nwcesno rDPLZOfnGZlb5LKF/ZrPdgyVEMweXfufm3Q/AHyQ4nu27P6u34sGjlinQV2WDkwOCdxL +772YQ6LTpeZCSocTuHynlOgZ0vbBy7MtSYvM8Nry9Cys0YMUUu08baGSLOaCrdHBUiU lTxklIvxFUQt4rrMSGL2i5P6k9S3UgXhVykNsTTjMjSP/0fSZHet25SA5KgCN/77waN5 gYeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rUFKNSeW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w12si14417524jad.49.2021.05.31.09.32.18; Mon, 31 May 2021 09:32:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rUFKNSeW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232939AbhEaQdT (ORCPT + 99 others); Mon, 31 May 2021 12:33:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:40790 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233821AbhEaOrj (ORCPT ); Mon, 31 May 2021 10:47:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AA86E60FE8; Mon, 31 May 2021 13:56:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469381; bh=+2DSCjZV3TyGT9kJq28+bHORX3ytUeD85NtTPmhdBK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rUFKNSeWxVyZvgQOx2omXZhmowha1sanDG9SY6j5+nrM2lb0uuMekE3nxBeNa1rkz 11ORWNgyu/H6W7vGbcT40oOFXvlldLrjqWSoQVUM5SQToKEL4WojzU5lV7vur8wMa2 48TCs7Kf2oNPzXDwtjotVPIJ3DKWAlw7P39oJftk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Venkatesh Srinivas , David Matlack , Andrew Jones , Peter Xu , Paolo Bonzini Subject: [PATCH 5.12 131/296] KVM: selftests: Fix 32-bit truncation of vm_get_max_gfn() Date: Mon, 31 May 2021 15:13:06 +0200 Message-Id: <20210531130708.302150806@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Matlack commit ef4c9f4f654622fa15b7a94a9bd1f19e76bb7feb upstream. vm_get_max_gfn() casts vm->max_gfn from a uint64_t to an unsigned int, which causes the upper 32-bits of the max_gfn to get truncated. Nobody noticed until now likely because vm_get_max_gfn() is only used as a mechanism to create a memslot in an unused region of the guest physical address space (the top), and the top of the 32-bit physical address space was always good enough. This fix reveals a bug in memslot_modification_stress_test which was trying to create a dummy memslot past the end of guest physical memory. Fix that by moving the dummy memslot lower. Fixes: 52200d0d944e ("KVM: selftests: Remove duplicate guest mode handling") Reviewed-by: Venkatesh Srinivas Signed-off-by: David Matlack Message-Id: <20210521173828.1180619-1-dmatlack@google.com> Reviewed-by: Andrew Jones Reviewed-by: Peter Xu Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- tools/testing/selftests/kvm/include/kvm_util.h | 2 - tools/testing/selftests/kvm/lib/kvm_util.c | 2 - tools/testing/selftests/kvm/lib/perf_test_util.c | 4 +- tools/testing/selftests/kvm/memslot_modification_stress_test.c | 18 ++++++---- 4 files changed, 16 insertions(+), 10 deletions(-) --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -295,7 +295,7 @@ bool vm_is_unrestricted_guest(struct kvm unsigned int vm_get_page_size(struct kvm_vm *vm); unsigned int vm_get_page_shift(struct kvm_vm *vm); -unsigned int vm_get_max_gfn(struct kvm_vm *vm); +uint64_t vm_get_max_gfn(struct kvm_vm *vm); int vm_get_fd(struct kvm_vm *vm); unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size); --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1969,7 +1969,7 @@ unsigned int vm_get_page_shift(struct kv return vm->page_shift; } -unsigned int vm_get_max_gfn(struct kvm_vm *vm) +uint64_t vm_get_max_gfn(struct kvm_vm *vm) { return vm->max_gfn; } --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -2,6 +2,7 @@ /* * Copyright (C) 2020, Google LLC. */ +#include #include "kvm_util.h" #include "perf_test_util.h" @@ -80,7 +81,8 @@ struct kvm_vm *perf_test_create_vm(enum */ TEST_ASSERT(guest_num_pages < vm_get_max_gfn(vm), "Requested more guest memory than address space allows.\n" - " guest pages: %lx max gfn: %x vcpus: %d wss: %lx]\n", + " guest pages: %" PRIx64 " max gfn: %" PRIx64 + " vcpus: %d wss: %" PRIx64 "]\n", guest_num_pages, vm_get_max_gfn(vm), vcpus, vcpu_memory_bytes); --- a/tools/testing/selftests/kvm/memslot_modification_stress_test.c +++ b/tools/testing/selftests/kvm/memslot_modification_stress_test.c @@ -71,14 +71,22 @@ struct memslot_antagonist_args { }; static void add_remove_memslot(struct kvm_vm *vm, useconds_t delay, - uint64_t nr_modifications, uint64_t gpa) + uint64_t nr_modifications) { + const uint64_t pages = 1; + uint64_t gpa; int i; + /* + * Add the dummy memslot just below the perf_test_util memslot, which is + * at the top of the guest physical address space. + */ + gpa = guest_test_phys_mem - pages * vm_get_page_size(vm); + for (i = 0; i < nr_modifications; i++) { usleep(delay); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, - DUMMY_MEMSLOT_INDEX, 1, 0); + DUMMY_MEMSLOT_INDEX, pages, 0); vm_mem_region_delete(vm, DUMMY_MEMSLOT_INDEX); } @@ -120,11 +128,7 @@ static void run_test(enum vm_guest_mode pr_info("Started all vCPUs\n"); add_remove_memslot(vm, p->memslot_modification_delay, - p->nr_memslot_modifications, - guest_test_phys_mem + - (guest_percpu_mem_size * nr_vcpus) + - perf_test_args.host_page_size + - perf_test_args.guest_page_size); + p->nr_memslot_modifications); run_vcpus = false;