Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp955254pxb; Wed, 3 Mar 2021 22:24:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxKOwSuTOTmQN5weJFMu6wWZOYbeB8jZexZlFoERNJOnEW9I6Ygya1EAqUd9vxYVZvm+Zj1 X-Received: by 2002:a50:da8b:: with SMTP id q11mr2729563edj.352.1614839085706; Wed, 03 Mar 2021 22:24:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614839085; cv=none; d=google.com; s=arc-20160816; b=TMJujPU1n8ueGf9FtWgtDUU/LGhhG0OVaIvoE231O9F8EBPZEWFIW/2yNF9fZjDuLD rnk2GA0IeN10W30wi/qmAPfL7VC42QQZYRukGzJ8M180KRShXIthHHXyG7GkH2AiGDsl /R/4IrzuzFUtb6SK0qxfVAv9z+gGJ/nXwtDa2Yx9UFaR8qTHy/Y4Lw5CqYKDKQ74x//3 IfgtflgIqQxhYr9vrDohm3Q2SXqW7CowkmPQmABxOVe6+TSKDxC/IvALy/ed1JupANyb mT/EVlS4St84TdyLNtBopojg0qy620ngWwbAlUt4hz4YIXYhlszUSkfTgA+MOwvyEqL/ 6tHA== 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=ZAuqWN0cChuub2Y163GSLbbg//vcDUUewwya/p9Rlzg=; b=w7yWp57MZri8OTiTPC5KK9l33V46ZqMxKfPi4NfF6qbCRAsy/pdSDDeP8EzZJIrIy5 VsTxA4DWQdbgnBcHhrfVml58sLbUIhwHR0ecOLCcsOT8qb8JtEyqbYlK/ypwb4HZME2F ql71NPj7gruaGwtRM7XnVkDvDEiDAxIh8zfGTKjyQ8hkuEgtH9CP74uVjEA3m1QuyFHY gP2trm67vHXRDQRRGQssNGA1au+NbMZUTXuTqgVzYJpnUITtaTU1VFbf83N71cvRYdoS GKjFVfBfahJBOq3QLjaAT+nRHS82J2JO9ckCeWLeJnEKqM8eAVJp397jWAAzspoTopIm biew== 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 co24si8508311edb.599.2021.03.03.22.24.23; Wed, 03 Mar 2021 22:24:45 -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 S1578729AbhCBP0x (ORCPT + 99 others); Tue, 2 Mar 2021 10:26:53 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:13842 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351008AbhCBNDj (ORCPT ); Tue, 2 Mar 2021 08:03:39 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4DqcZk4DfQz7sRT; Tue, 2 Mar 2021 20:56:30 +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; Tue, 2 Mar 2021 20:58:04 +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 v4 8/9] KVM: selftests: Adapt vm_userspace_mem_region_add to new helpers Date: Tue, 2 Mar 2021 20:57:50 +0800 Message-ID: <20210302125751.19080-9-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210302125751.19080-1-wangyanan55@huawei.com> References: <20210302125751.19080-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 Reviewed-by: Ben Gardon --- 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