Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp955153pxb; Wed, 3 Mar 2021 22:24:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJzSOqtiOU+AItk+qN8qAGKSRQ5uhvZzFdB6uyWINWJuF7Zk1BNU81F0drQtu5C2U1muyBUj X-Received: by 2002:a17:906:70d4:: with SMTP id g20mr2576942ejk.361.1614839072049; Wed, 03 Mar 2021 22:24:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614839072; cv=none; d=google.com; s=arc-20160816; b=O/6OKnxeWmmCbbHGW5mSAAjet+0V55VK17r11eA+9Vw3D0u3uEOEHdcDv/NaMe98x9 QrD5j87D8i82265+5tNEPezdBPg2Tyb1uIqRZDApGlluSDi4Wop2kVmjVMJdUiRG1R2m lSiECWcYfToj2b2yyoA7b/Sg1Vgoey52DcmUfffGrF6Oz4o58CWqbBXSlwetNueNEvjJ 4RnVVJRiql2P4hpv61oAJJogofDCBw2txaUqgg/s0EAySjuun2fE1F1vTmMgxbgYxScn 8eALmYCkytH4xqYVS7LmY6cfEoFwN/VYUwxKxBaWeL47vz/oXVi5ha+3GWnBcxU8qAWn 0tlw== 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=gbsaP+KnAkBlqQEj96ZGT0TBH704l2LtaC4CQOMAkFk=; b=R7Mx/kHNnXR+HHL+DAj7tzgn8Xcka4Jva4mJV/T/g3YTCH/Nyozbi7MIqMLo3bE3t/ tSXTorN+Us0htRzlwUSCsaVEObqb3y3cHFHupWfBLEBmrmIgwopwneav686ysJfHcq03 x6FvukCPVnsmCb85WwS/pZLa4gUpMsrezPEyWxgWIHn9ypd7CmlOVtGn8gPVyamnqoCy 7KkBpYYpBYK09A6ReVSuTLvozgzpCqFT9gkoax5XPW2DS2emPliCnKmSZxpcsQmdcWYN zI3UXsohMDWZTibWsGHivpq0I93pumz9WYpNiWwlpCR99w8pNNvRmEsj3EHQzaabKwQy ivBg== 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 ca3si17476335ejb.561.2021.03.03.22.24.09; Wed, 03 Mar 2021 22:24:32 -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 S1578686AbhCBP0b (ORCPT + 99 others); Tue, 2 Mar 2021 10:26:31 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:13841 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351010AbhCBNDj (ORCPT ); Tue, 2 Mar 2021 08:03:39 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4DqcZk3MVKz7sRM; 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:00 +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 5/9] KVM: selftests: Add a helper to get system configured THP page size Date: Tue, 2 Mar 2021 20:57:47 +0800 Message-ID: <20210302125751.19080-6-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 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); + + 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