Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp182928pxb; Wed, 24 Feb 2021 22:54:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJxhfZzRKqOOQfA5UXF1z4rtjKev6J+t9ZATuR+6tARNxwfqwb0Y0neHmvBbDbYs/ATMlkun X-Received: by 2002:a50:ee92:: with SMTP id f18mr1393763edr.373.1614236052073; Wed, 24 Feb 2021 22:54:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614236052; cv=none; d=google.com; s=arc-20160816; b=iWTzc1U7MjBAACzBLljxNrpKjuMkmDNtHKku3RxVWRHGeHrjassWTyl7qGiUKsWy8N cT6j8DpvcHTXe73QiUulSiB/symOTEUNXqQiz5IJp8/YxTDowilyRTgLYUG2euIAzojb 1Y9ouhdyn5Ujw8wHSfi+CTrS6An48qw0D11/itMvFYm5L0oeyX23a7p3elBKyRVNED8f XhahHK7hksn+MoNOdYCF5nGZbxx/hwjDGtpZib4nYqLlnjOzk3BCS7bPv0o8h1Wt1fB+ 8F7PNA6lrId/dLQJp+TdPD1+c4gYi/S6SJ+OvhMnbjWHJ3mQEUfqurQERS+LZnS3TUnS kbEQ== 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=hjbXnZnLExLKvF4UjCGL5ZI4yvQmuVdFo64PXr3pb08=; b=vWC5QtpQHiX/EmVyvptC5M15x9amkEficPJMwqdaSFgcu6hhQw0IfiemonYfk+369I TWe7uG2U+GL0BhhgyuHNHnbive0TNdvAa4+6FClAL32DxYH55jWRKE/7yLIbJmOejwre Ack6unPuw9wJ2xIl0Svf4NpUeoe0JPhn5Ax9zKX1HyokH6WN3YubbNZIACGJNK7Coq5v 42IU+/nmmRrU+9ZzRknZW+VLXkcEI/qa9AvLrk2XLS4y1TxQFNDjNjakjybLoG5GNrld QiS+8EoPA1FNPpRx3QrEk+xcJDC+2Ma8AlQVtBfo9g4oG4ndthgR0RJ6wTQsZZ6a8Wf3 b2gg== 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 s11si2639858edr.584.2021.02.24.22.53.49; Wed, 24 Feb 2021 22:54:12 -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 S235536AbhBYGDn (ORCPT + 99 others); Thu, 25 Feb 2021 01:03:43 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:13085 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235334AbhBYGAp (ORCPT ); Thu, 25 Feb 2021 01:00:45 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DmMXZ0JFwz16CZv; Thu, 25 Feb 2021 13:58:22 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.498.0; Thu, 25 Feb 2021 13:59:50 +0800 From: Yanan Wang To: , , CC: Paolo Bonzini , Ben Gardon , "Sean Christopherson" , Vitaly Kuznetsov , Arnaldo Carvalho de Melo , Ingo Molnar , Andrew Jones , Peter Xu , Marc Zyngier , , , "Yanan Wang" Subject: [RFC PATCH v2 6/7] KVM: selftests: Adapt vm_userspace_mem_region_add to new helpers Date: Thu, 25 Feb 2021 13:59:39 +0800 Message-ID: <20210225055940.18748-7-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210225055940.18748-1-wangyanan55@huawei.com> References: <20210225055940.18748-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 | 27 +++++++--------------- 1 file changed, 8 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..0105fbfed036 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,12 @@ 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_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.19.1