Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp964929pxb; Thu, 25 Feb 2021 21:51:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwBbzYqQD+frh5NPWv4SXclyzrdlVyVKs6nhVSL8biiQt1YOEpXl1fbIov7YeLwueLB724G X-Received: by 2002:a17:907:3e04:: with SMTP id hp4mr1559597ejc.188.1614318709583; Thu, 25 Feb 2021 21:51:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614318709; cv=none; d=google.com; s=arc-20160816; b=xR+EFaZiyrVczm/eSy8lcd7evfqLV6JHeB66RrZvuV6liVF6TB5MPk6e4yalrcdF7i 0xOnT2U8iUxyG21oHANnUelMXfQF4zHMvccl/A3GbPXnDDR1Ajbu97Kppxvos0Y032qV cvEsivG8AQX4vJas64J3jBEOcpJzaK7i+gunEEOUQHA9FzLvXxArdHE3efoevDLcOid6 edxMAZ7k4yYtPojLZeBpM5uzU23NbMjekiuz9H4yUxG8yIkMQ1DLdXAj5Y3+X9UXUdgA PLgJJN1sSosJkONnmCjE+l/g5Hrq5bc6Og2CBqWdpNiHZqw+twzY2cBpmqHZHlw6AiAf xeiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=YbPXT8lhO+/L7Qxe6cttBNrzxQ4sfT5qD+tfDSou8dQ=; b=dS1i6+benBBM+foCNb1b6g9QfQrJPtbjsIGtvSgg5QOvFosu62Pvq3OuNVMwMr0ci1 hAdxzGLDXXJ9l3yGQPVku/Ut5/ea3LjqDxb+BjeROkp6Zj6RH+Kki7ZLh++/C8szxWMq 9OBo4P/JcBd6vBCg8tJUXxXQeXR8EQBIK/mDdfLIybMxWH8HFORVoTWWtNkw8Dcs4xXI p/d+nLZgC+B4peZSR4DsIaIOFxIrn4MmJK1rio56lEOUNXKRI2LOslOYMjSrTBT5F/UI I0PjImraBxm9mIQG3hChwPtfAnBdOxQFY5b5hkK39HrjE2KTUo3zQ1aRMtALeAiwl2y6 3eVQ== 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 k9si4992855eje.184.2021.02.25.21.51.27; Thu, 25 Feb 2021 21:51:49 -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 S229621AbhBZFta (ORCPT + 99 others); Fri, 26 Feb 2021 00:49:30 -0500 Received: from szxga01-in.huawei.com ([45.249.212.187]:2590 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbhBZFt2 (ORCPT ); Fri, 26 Feb 2021 00:49:28 -0500 Received: from dggeme711-chm.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4DmzD02MpYzWCW0; Fri, 26 Feb 2021 13:46:08 +0800 (CST) Received: from [10.174.187.128] (10.174.187.128) by dggeme711-chm.china.huawei.com (10.1.199.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2106.2; Fri, 26 Feb 2021 13:48:44 +0800 Subject: Re: [RFC PATCH v2 6/7] KVM: selftests: Adapt vm_userspace_mem_region_add to new helpers To: Ben Gardon CC: kvm , , LKML , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Arnaldo Carvalho de Melo , Ingo Molnar , Andrew Jones , Peter Xu , Marc Zyngier , , References: <20210225055940.18748-1-wangyanan55@huawei.com> <20210225055940.18748-7-wangyanan55@huawei.com> From: "wangyanan (Y)" Message-ID: Date: Fri, 26 Feb 2021 13:48:44 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.174.187.128] X-ClientProxiedBy: dggeme706-chm.china.huawei.com (10.1.199.102) To dggeme711-chm.china.huawei.com (10.1.199.107) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/2/26 7:44, Ben Gardon wrote: > On Wed, Feb 24, 2021 at 10:03 PM Yanan Wang wrote: >> 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()) { > This check relies on an unstated property of the backing src type > enums where VM_MEM_SRC_ANONYMOUS and VM_MEM_SRC_ANONYMOUS_THP are > declared first. > It would probably be more readable for folks if the check was explicit: > if ((src_type == VM_MEM_SRC_ANONYMOUS || src_type == > VM_MEM_SRC_ANONYMOUS_THP) && thp_configured()) { > Yes, this makes sense, I will fix it. Thanks, Yanan >> + 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 >> > .