Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp956574pxb; Wed, 3 Mar 2021 22:27:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJzkI0ukvCYgUNdddO3u75/vP8ydQuGXljpmc9STEGIdFueVco1pPd7xr9vohj5boDPK0Ypm X-Received: by 2002:a17:906:228d:: with SMTP id p13mr2596088eja.412.1614839264629; Wed, 03 Mar 2021 22:27:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614839264; cv=none; d=google.com; s=arc-20160816; b=gKQZ/GyHa10yB8jeBS4nb/gS4bgVoCdxQ6KJ6uCIDZNb/iickFB0sr+p3y3xgaJKMG gsKrTJxszjLUlOmc/+lOPUyEGHEiHMDIyuXyWxb33XO0uF156mZPuvuH5I+wmM9ESXqB 504fT/Gm2wwARFOQDzdQlezwwuZAikdAmWLiF+ndqf98IL/pCze93bfvvSmQhkl5o0t8 AtnZ+sOvEU5wCuft5DWb8cZirgUv9e96TJ/lbYjVk7SV8fVNpinoj26j7s767Df/NJAC BDdLvFPHV/D7uo09lAls8wzY+kiWJDtocrc5hzljJV4nqpsrbdJ9/LNkKAHsEobhvgox WW/g== 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=+UyiUAsoaeRWDLXAFptCuxxZ6qNJGWeQf9uJB1DX0+c=; b=HNWYUhToJC4/2Zemnl4ocgUrE52/h/VYybbYb5jHFrflT+2QRwPw1UEvgHmOg/Q8YK 9lM2x9S1bKC/dVPnDhUdRcA1FBZGVv2ZAxOSQP6xoLEKAWWiUhJRVOtgTYVfYESN+xJb RpGcJozEFem5Njzl8maLhqllbqqoAYRu3e8TBpe16R9UIZYsc7QuYCAzK832mpQ9aeC6 MfQ3WcsL4eVDPALE2Bi8ldsbjGFYpQgIGSZdFLpREIKF8C5Lt2xBBzAMjGDyKz/JDCYb BSzOj4cVEk5AspJSjuEunOzrXs7cR9TCCV7I/k+rzo9FEmJNSZ1JL53Hqs2+q3LtVex1 N9jQ== 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 i7si20544118ejo.67.2021.03.03.22.27.22; Wed, 03 Mar 2021 22:27:44 -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 S1379122AbhCBPgh (ORCPT + 99 others); Tue, 2 Mar 2021 10:36:37 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:13838 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350927AbhCBM7O (ORCPT ); Tue, 2 Mar 2021 07:59:14 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4DqcZk2wKFz7sRB; 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:01 +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 6/9] KVM: selftests: Add a helper to get system default hugetlb page size Date: Tue, 2 Mar 2021 20:57:48 +0800 Message-ID: <20210302125751.19080-7-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 If HUGETLB is configured in the host kernel, then we can know the system default hugetlb page size through *cat /proc/meminfo*. Otherwise, we will not see the information of hugetlb pages in file /proc/meminfo if it's not configured. So add a helper to determine whether HUGETLB is configured and then get the default page size by reading /proc/meminfo. This helper can be useful when a program wants to use the default hugetlb pages of the system and doesn't know the default page size. Signed-off-by: Yanan Wang --- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/test_util.c | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index ef24c76ba89a..e087174eefe5 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -80,6 +80,7 @@ struct vm_mem_backing_src_alias { bool thp_configured(void); size_t get_trans_hugepagesz(void); +size_t get_def_hugetlb_pagesz(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 f2d133f76c67..80d68dbd72d2 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -153,6 +153,33 @@ size_t get_trans_hugepagesz(void) return size; } +size_t get_def_hugetlb_pagesz(void) +{ + char buf[64]; + const char *tag = "Hugepagesize:"; + FILE *f; + + f = fopen("/proc/meminfo", "r"); + TEST_ASSERT(f != NULL, "Error in opening /proc/meminfo: %d", errno); + + while (fgets(buf, sizeof(buf), f) != NULL) { + if (strstr(buf, tag) == buf) { + fclose(f); + return strtoull(buf + strlen(tag), NULL, 10) << 10; + } + } + + if (feof(f)) { + fclose(f); + TEST_FAIL("HUGETLB is not configured in host kernel"); + } else { + fclose(f); + TEST_FAIL("Error in reading /proc/meminfo: %d", errno); + } + + return 0; +} + void backing_src_help(void) { int i; -- 2.23.0