Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3037487pxb; Sun, 28 Feb 2021 23:07:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJw3aU/vcVEQMz+B4KBClNBW6354Z7hWe5Uo2MGwEPrV7M+fbQ73nacNrLbyKPYvqm8cEq+O X-Received: by 2002:a17:906:8a65:: with SMTP id hy5mr8711722ejc.250.1614582424511; Sun, 28 Feb 2021 23:07:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614582424; cv=none; d=google.com; s=arc-20160816; b=bTDUPwRWAlabTzwSHB0P41YBsjp2Cdknovk1NiqtRrgJD540fbXMNcOKd3hHNYQRtc ctAIDJ1y4FpA4+stzVeLZfQZWrg1ZReEhfZvN9omW17O9UhV4zCL/XADmlsrN+E4zt5N AoNFy6vS/vJfiPupbU1CKZxrsIUHNHYfPR9T0cdlBNveeatjqfBz4JISI1SiPcb0G8WE UxCuwUrWCHZRkubRHd/2mRyjxSq/busNMDbdB7aC91TFe0hWf6LOLoxiES6cNpIW7s2W 6jzyNkkkdKaqiKukir8enXxzcgt0RHCGyitDohDcoV3LqRPUuvVfSNLf6laqXaJYzidi txEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=GRdyeRObhhFKsZY29OHfCmil+B3fLMkypUGySF74EeM=; b=kKEQ6UVu05IJJSAT8cm6bhGpLcC01cj5Av8b9JjB/P+qsoXMiueOrGAAXtqg0cCZoC Svm/yN1DVZ7pQtIhgUoonsR9WtB6HT+RWRryuvDAMjG8mlIGd3N+6xNbZJWwo/VpcdRz 0fCFTk7Ee5qRTMKI4g+paBNlXojcHpEfmNA+mRtiCJTbgqMPebMjfCCX55JECwj8Ej4+ 0seuyuWXJ/WDtcGT2YDYqciqd9zAY6VUQrUkdn9ZJi/jB248gOsotjyuYhOzh5nDEb2s UOeqo7v9aS3ohATKAh9AEas+HwTKjU9HUYsdMy3Nnjno0zDKV/q+PLttlB7vL8Po8WG5 L1pg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n22si10950140ejk.360.2021.02.28.23.06.42; Sun, 28 Feb 2021 23:07:04 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232311AbhCAHCy (ORCPT + 99 others); Mon, 1 Mar 2021 02:02:54 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:13026 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229882AbhCAHBR (ORCPT ); Mon, 1 Mar 2021 02:01:17 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Dprfx4tPJzMgNW; Mon, 1 Mar 2021 14:57:29 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.498.0; Mon, 1 Mar 2021 14:59:28 +0800 From: Yanan Wang To: , , CC: Paolo Bonzini , Ben Gardon , "Sean Christopherson" , Vitaly Kuznetsov , Andrew Jones , Peter Xu , Marc Zyngier , Ingo Molnar , Adrian Hunter , Jiri Olsa , "Arnaldo Carvalho de Melo" , Arnd Bergmann , Michael Kerrisk , Thomas Gleixner , , , , Yanan Wang Subject: [RFC PATCH v3 7/8] KVM: selftests: Adapt vm_userspace_mem_region_add to new helpers Date: Mon, 1 Mar 2021 14:59:15 +0800 Message-ID: <20210301065916.11484-8-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210301065916.11484-1-wangyanan55@huawei.com> References: <20210301065916.11484-1-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.174.187.128] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With VM_MEM_SRC_ANONYMOUS_THP specified in vm_userspace_mem_region_add(), we have to get the transparent hugepage size for HVA alignment. With the new helpers, we can use get_backing_src_pagesz() to check whether THP is configured and then get the exact configured hugepage size. As different architectures may have different THP page sizes configured, this can get the accurate THP page sizes on any platform. Signed-off-by: Yanan Wang --- tools/testing/selftests/kvm/lib/kvm_util.c | 28 +++++++--------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index b91c8e3a7ee1..b29402f9f00c 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -18,7 +18,6 @@ #include #include -#define KVM_UTIL_PGS_PER_HUGEPG 512 #define KVM_UTIL_MIN_PFN 2 /* Aligns x up to the next multiple of size. Size must be a power of 2. */ @@ -686,7 +685,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, { int ret; struct userspace_mem_region *region; - size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size; + size_t backing_src_pagesz = get_backing_src_pagesz(src_type); size_t alignment; TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages, @@ -748,7 +747,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, #endif if (src_type == VM_MEM_SRC_ANONYMOUS_THP) - alignment = max(huge_page_size, alignment); + alignment = max(backing_src_pagesz, alignment); /* Add enough memory to align up if necessary */ if (alignment > 1) @@ -767,22 +766,13 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->host_mem = align(region->mmap_start, alignment); /* As needed perform madvise */ - if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) { - struct stat statbuf; - - ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf); - TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT), - "stat /sys/kernel/mm/transparent_hugepage"); - - TEST_ASSERT(ret == 0 || src_type != VM_MEM_SRC_ANONYMOUS_THP, - "VM_MEM_SRC_ANONYMOUS_THP requires THP to be configured in the host kernel"); - - if (ret == 0) { - ret = madvise(region->host_mem, npages * vm->page_size, - src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); - TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %x", - region->host_mem, npages * vm->page_size, src_type); - } + if ((src_type == VM_MEM_SRC_ANONYMOUS || + src_type == VM_MEM_SRC_ANONYMOUS_THP) && thp_configured()) { + ret = madvise(region->host_mem, npages * vm->page_size, + src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); + TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %s", + region->host_mem, npages * vm->page_size, + vm_mem_backing_src_alias(src_type)->name); } region->unused_phy_pages = sparsebit_alloc(); -- 2.23.0