From patchwork Mon Mar 1 06:59:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yanan Wang X-Patchwork-Id: 388929 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3832C433E0 for ; Mon, 1 Mar 2021 07:01:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8AEA560234 for ; Mon, 1 Mar 2021 07:01:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232213AbhCAHBV (ORCPT ); Mon, 1 Mar 2021 02:01:21 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:13027 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232089AbhCAHBR (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 4Dprfx5LJCzMgP1; 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:25 +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 5/8] KVM: selftests: Add a helper to get system configured THP page size Date: Mon, 1 Mar 2021 14:59:13 +0800 Message-ID: <20210301065916.11484-6-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 X-Originating-IP: [10.174.187.128] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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. Reviewed-by: Ben Gardon Signed-off-by: Yanan Wang --- .../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); + + 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); + + 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); + return size; +} + void backing_src_help(void) { int i;