Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3036182pxb; Sun, 28 Feb 2021 23:04:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJwp/wQoSkM/UvIIM6IcIlaq//g1GTvzr/CvFYPifpaWaiDrGffR5qPgD4tA4SNs9jOxNhk4 X-Received: by 2002:a17:906:789:: with SMTP id l9mr14307059ejc.161.1614582291488; Sun, 28 Feb 2021 23:04:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614582291; cv=none; d=google.com; s=arc-20160816; b=RoumyxxgzgTRFtDT2FS4JqpPSBQYCe8Y8rBpE7p6YoJp6+fv3QO7a68ittm5Nl96o+ Gw3XZn+nDKwSgp5lIpKpElPNk4K1aGin7Ubneu8LCeA1ugoDVYP6dci3VIkkXsLSiwad GX5BKx5p5SsE3eSXZm4RV/SHXvuuKEPONHmknz2NbSwASBrJQQOr9fEQ7svU0SgG8ZVB LynLL77m5RayFycOFvyYJjuLngdvH9+jgDj1g6sonrmj9G+Tqk18wV4Y//mYK4j+EZNK cI0UuLt7cUKKYJARLv1bmasUBtroSU1dOCy0uLXrMS7TjybSTL46sdqlzIoDKxPOSqdH E8kg== 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=IdXdKufQOgd1CKgjKZpkODmDw8etxy5kpJB08J0nAd4=; b=nKu9ugSZZINWBbel5gUJkmDp6aWUmlGFglKJTFik+wYrrZ+xFVzngyrRuAU+CN+CzV 9I+eQg4WCHfbqLvfSGRz1nfQVmqLgbQcXG4Oi4q0DEDXsXF6jpKRVTfa1pWy8KRK3p4h eCMGkBDqojfid38ztPZLQEThGxyWSImFli5Cc4viLJb6+6EfTTO7YJTFuCO0j9ELHhNN ndfdEbS/GMVOQVmFco05imHDNpw0QTdjA/enYE/9i+CP9YYZjvt9mhIpipa6DHp2FHT6 FMRhML8vwfM6p8+/V1PYybKA2HbDrKj4EKP3jUYfrxiuCeLFD8HTgiXyPmZziTXmu4rx AvHw== 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 g27si3735788ejc.32.2021.02.28.23.04.29; Sun, 28 Feb 2021 23:04:51 -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 S232278AbhCAHBy (ORCPT + 99 others); Mon, 1 Mar 2021 02:01:54 -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 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 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; -- 2.23.0