Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4186397pxf; Tue, 30 Mar 2021 01:11:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTBjkWdKUlRfu/MyYBmZoxLuFnWYR6IlHFx3u42IGkkkax9pJz9OxyspzHfbZUlJyytox4 X-Received: by 2002:a17:906:9386:: with SMTP id l6mr31795638ejx.455.1617091868380; Tue, 30 Mar 2021 01:11:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617091868; cv=none; d=google.com; s=arc-20160816; b=TqCeBz6AMbkD5xdT4ghZrs67VjbVtYGQvTGid2wY7lvwyMF0WiQDrbQIpz1UUIFggv 6IuJsyOxRZKxdC0Y14TB1rEvr5c3uR+7Haj4P9L4A8YLnl2xweHccp3LGv1YnEVF8pIH hVUi5JhV0BQ7enNThpBPU4Dh879+DuzSYSRwtSewAtK9I5P145HQmC9ALmXl3Obxvr43 LmUfGZI2QTMJkc8lulaTNfdZx3Qf2A57O0JTdWAmxN3Y6H8HMASTwoiGnrL2CANYdP6G 88QQe61exW/rb0Z7uWsS+9Q9mz+tLzgG5+xmFW3ZN4iP2aTLS2kHda1GNZYIEL+7Kuq5 X7ZQ== 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=O6vapICC/b3UK8esJsqa+evF3cLuoVMc2dv1OyXY1w4=; b=0bEnadUx9gxQnjJCCHlH0vx1vmoScBFUYdmT41U2AJ0k3J2yH6DXYcz0BT8tU9MFHS 7gwZhFK/dRP+uU6W171gwpDekdiHUH0q6h6rfNiOi7iUK+QmqEUn5pqqFFlrKQlUp1rf kVBlQqr/qyskm2LhnGuXIrRXg3kIrw9Gh50LevBtnGQUCBiVGEBopNFWAC6Tb5FPzbEz mUPHJkTSwMYgFm7vVTLQw2bufjDT/EIqJhQGNpd4FnSzieujZ+nDQGkO8xTcK6yOjAIc FaqXLogP6b/qDrb0Ag2moWHpVp129/Uf5TaVIUS3JNAw4fhT11ejA1lAAxsuh7+JLFoW AtIw== 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 dr9si15623913ejc.436.2021.03.30.01.10.45; Tue, 30 Mar 2021 01:11:08 -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 S231688AbhC3IKA (ORCPT + 99 others); Tue, 30 Mar 2021 04:10:00 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:14199 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231509AbhC3IJW (ORCPT ); Tue, 30 Mar 2021 04:09:22 -0400 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4F8hqQ5ZTSzmcCx; Tue, 30 Mar 2021 16:06:42 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.498.0; Tue, 30 Mar 2021 16:09:09 +0800 From: Yanan Wang To: Paolo Bonzini , Andrew Jones , , , CC: Ben Gardon , Sean Christopherson , Vitaly Kuznetsov , Peter Xu , "Ingo Molnar" , Adrian Hunter , Jiri Olsa , Arnaldo Carvalho de Melo , Arnd Bergmann , Michael Kerrisk , Thomas Gleixner , , , Yanan Wang Subject: [PATCH v6 07/10] KVM: selftests: Add a helper to get system default hugetlb page size Date: Tue, 30 Mar 2021 16:08:53 +0800 Message-ID: <20210330080856.14940-8-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210330080856.14940-1-wangyanan55@huawei.com> References: <20210330080856.14940-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 Reviewed-by: Andrew Jones --- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/test_util.c | 25 +++++++++++++++++++ 2 files changed, 26 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 efc1a7782de0..665724ccab97 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -146,6 +146,31 @@ 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"); + + 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)) + TEST_FAIL("HUGETLB is not configured in host kernel"); + else + TEST_FAIL("Error in reading /proc/meminfo"); + + fclose(f); + return 0; +} + void backing_src_help(void) { int i; -- 2.19.1