Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3201042pxf; Sun, 21 Mar 2021 23:45:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxdq115VY4KDrEHBbTdTyRWxTqpbD6TfZ0IWbKMcZK/Eg45lRzw8tjLPoWt/vAq1EQu3UWq X-Received: by 2002:a17:907:1614:: with SMTP id hb20mr17662930ejc.77.1616395505446; Sun, 21 Mar 2021 23:45:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616395505; cv=none; d=google.com; s=arc-20160816; b=gBnYIYbcVShyozlXL7mitYs//0Mc+yA5QwDLugy59dgiDoCUlKGDOs68b0ye5pdKA6 /vUH2ij2Xl6i6ce0VDLuoh41OJcHRU47N2lP/jSdwsn5/arIQx/lA5+4U773PPaviwtL 4hgV+UgLwl8GwvLBbzJDE96Ufsa2nCLRGz5W1fyrhh7OaPWv8sNwBw6UlJRCJpv0vnOs SVOoOjvb+HZOPIyj1GIN+jKiX5BPzc0q31gMNmdak5h0Jn3IvokLVTkNvLPDd0uO0ftJ 3jRDMQKrUWZtPIsu8YebVVLYbgH/NpBtA941e08TVrC7lXwDtUMBHU1bFJ3eK4bvkZIC Uqyw== 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=mz3vaun941gyhqHXTEYXn95JRLcW18SrGO3tPKCgatI=; b=b+JozdzUnMWL1MI3XINAARz/JWSneqwMuFazmtlMpGLKj0G9gCLEHUuvD5m9C/gL51 5B9/Ol5yVfN1cL3BS2URTqjnnhFKma6+ksLmzBIDzhsee+PYRnKoBZ/gh4iy58vY+7UB BCFsL8L1hCYrEF9UONvl7fNB/qcZqIfhlpGabVbQ2++jBN/Krb7Dl2IrbwETlygsibeV +IYXj2Z+EAdDqcWUagnioT9lSwt0+lD7L+TR+uVng8rvxrgX1zgt1+BD2s6v/JzQz/Fl 3+mEeTOBOhatKZe1YHvjH9LwG86u6Yt2bCiOVNnZ8lYBdfrDrsjc88syzzOo6Oj5DdpF YzsQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a3si10312599ejv.34.2021.03.21.23.44.43; Sun, 21 Mar 2021 23:45:05 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230012AbhCVGnR (ORCPT + 99 others); Mon, 22 Mar 2021 02:43:17 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:5103 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230010AbhCVGmr (ORCPT ); Mon, 22 Mar 2021 02:42:47 -0400 Received: from DGGEML401-HUB.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4F3lJ80TQmzYMsW; Mon, 22 Mar 2021 14:40:56 +0800 (CST) Received: from dggpemm500023.china.huawei.com (7.185.36.83) by DGGEML401-HUB.china.huawei.com (10.3.17.32) with Microsoft SMTP Server (TLS) id 14.3.498.0; Mon, 22 Mar 2021 14:42:42 +0800 Received: from [10.174.187.128] (10.174.187.128) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2106.2; Mon, 22 Mar 2021 14:42:41 +0800 Subject: Re: [RFC PATCH v4 5/9] KVM: selftests: Add a helper to get system configured THP page size To: Andrew Jones CC: , , , Paolo Bonzini , "Ben Gardon" , Sean Christopherson , "Vitaly Kuznetsov" , Peter Xu , Marc Zyngier , Ingo Molnar , Adrian Hunter , Jiri Olsa , "Arnaldo Carvalho de Melo" , Arnd Bergmann , Michael Kerrisk , Thomas Gleixner , , , References: <20210302125751.19080-1-wangyanan55@huawei.com> <20210302125751.19080-6-wangyanan55@huawei.com> <20210312113112.laarzqhkslroagha@kamzik.brq.redhat.com> From: "wangyanan (Y)" Message-ID: <46362177-c514-96fe-6117-4ab9716ee01d@huawei.com> Date: Mon, 22 Mar 2021 14:42:41 +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: <20210312113112.laarzqhkslroagha@kamzik.brq.redhat.com> 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: dggeme704-chm.china.huawei.com (10.1.199.100) To dggpemm500023.china.huawei.com (7.185.36.83) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Drew, Thanks for your attention to this series! On 2021/3/12 19:31, Andrew Jones wrote: > On Tue, Mar 02, 2021 at 08:57:47PM +0800, Yanan Wang wrote: >> If we want to have some tests about transparent hugepages, the system >> configured THP hugepage size should better be known by the tests, which >> can be used for kinds of alignment or guest memory accessing of vcpus... >> So it makes sense to add a helper to get the transparent hugepage size. >> >> With VM_MEM_SRC_ANONYMOUS_THP specified in vm_userspace_mem_region_add(), >> we now stat /sys/kernel/mm/transparent_hugepage to check whether THP is >> configured in the host kernel before madvise(). Based on this, we can also >> read file /sys/kernel/mm/transparent_hugepage/hpage_pmd_size to get THP >> hugepage size. >> >> Signed-off-by: Yanan Wang >> Reviewed-by: Ben Gardon >> --- >> .../testing/selftests/kvm/include/test_util.h | 2 ++ >> tools/testing/selftests/kvm/lib/test_util.c | 36 +++++++++++++++++++ >> 2 files changed, 38 insertions(+) >> >> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h >> index b7f41399f22c..ef24c76ba89a 100644 >> --- a/tools/testing/selftests/kvm/include/test_util.h >> +++ b/tools/testing/selftests/kvm/include/test_util.h >> @@ -78,6 +78,8 @@ struct vm_mem_backing_src_alias { >> enum vm_mem_backing_src_type type; >> }; >> >> +bool thp_configured(void); >> +size_t get_trans_hugepagesz(void); >> void backing_src_help(void); >> enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); >> >> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c >> index c7c0627c6842..f2d133f76c67 100644 >> --- a/tools/testing/selftests/kvm/lib/test_util.c >> +++ b/tools/testing/selftests/kvm/lib/test_util.c >> @@ -10,6 +10,7 @@ >> #include >> #include >> #include >> +#include >> #include "linux/kernel.h" >> >> #include "test_util.h" >> @@ -117,6 +118,41 @@ const struct vm_mem_backing_src_alias backing_src_aliases[] = { >> {"anonymous_hugetlb", VM_MEM_SRC_ANONYMOUS_HUGETLB,}, >> }; >> >> +bool thp_configured(void) >> +{ >> + int ret; >> + struct stat statbuf; >> + >> + ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf); >> + TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT), >> + "Error in stating /sys/kernel/mm/transparent_hugepage: %d", >> + errno); > TEST_ASSERT will already output errno's string. Is that not sufficient? If > not, I think extending TEST_ASSERT to output errno too would be fine. I think it's a good idea to output the errno together with it's string in TEST_ASSERT, it will explicitly indicate that the string is an error information and the errno is much easier to be used for debugging than the string. I will make this change a separate patch in next version and add your S-b tag. >> + >> + return ret == 0; >> +} >> + >> +size_t get_trans_hugepagesz(void) >> +{ >> + size_t size; >> + char buf[16]; >> + FILE *f; >> + >> + TEST_ASSERT(thp_configured(), "THP is not configured in host kernel"); >> + >> + f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r"); >> + TEST_ASSERT(f != NULL, >> + "Error in opening transparent_hugepage/hpage_pmd_size: %d", >> + errno); > Same comment as above. > >> + >> + if (fread(buf, sizeof(char), sizeof(buf), f) == 0) { >> + fclose(f); >> + TEST_FAIL("Unable to read transparent_hugepage/hpage_pmd_size"); >> + } >> + >> + size = strtoull(buf, NULL, 10); > fscanf with %lld? This makes senses. But it should be %ld corresponding to size_t. Thanks, Yanan. >> + return size; >> +} >> + >> void backing_src_help(void) >> { >> int i; >> -- >> 2.23.0 >> > Thanks, > drew > > .