Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp182550pxb; Wed, 24 Feb 2021 22:53:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJytB0pfn1kQHS4y0XdRUJiMwJguxRBeuOdilTMveX575kwlfCk4yHv5fjDPPk+pQi1e8q1Z X-Received: by 2002:a17:906:c401:: with SMTP id u1mr1332349ejz.200.1614236003553; Wed, 24 Feb 2021 22:53:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614236003; cv=none; d=google.com; s=arc-20160816; b=VknBxxp4VZtsitEfzzx00LhShFVSI++IMPAcfxyJso/2pg/nsazFWdimN/JfqSxWNX SPpgJHtCz4WJ3vzwHR62Exru6Cc1lRnSRZ7UIJSvgdwZpALr52qFnxBDpTB1orBjnq9G x7VM590B8NUwV/uuYDTNH8ybrSrup6IopMUmLSAlzqcRuSPYi4qwIWiH7tx+VLyL/uyz p3YQXMWQ3KYZc1wfbQ88mX4QidK/60e1/s3kwou+EC1HVkgb3TT3yLFtdXiFg8Lq9rVG B6hkctx6NGfZ+3qH3l7RKPZ3pNa2LKREjjaqqm1/gxgbrY73ETtrhOI58F5l0EhX3KsJ ViPA== 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=aZOogMQJfBhkVbf0pqB8hQQgk4IPJ6h5Jt1RX8z4KMU=; b=XZpCNVXIi6LyIFKTnqgqMmssiTSkqxCNe0mH2Cg/Y2Mn2KnggoPKLKiS4Za8pLAg2E HvpYCNyu1pKhBzTo/3mfDxx0hG9W4ME4PtNSsmeZ8t5O2bKEMVs2Wub2k0Nn8HtuQkvz vlGLkJp5M17l676EX11OKAlesSTLKmKCngDhSUK3jKxIqAlKJsRowN1DrPxNUDSKwSyE 1GSNxcnHDP+GBEsewo/uEFGr+8ouEUJNcOnW2jSevH2ULhdyBsNbJxYBA7b1VkhPsXgQ /vLwAjoqqGf0+yLzqa7s3rfsQO6vFfLCAfv+4c4omENY2qk58/KxKDI2BJ5pG86VTz5j 7Ebg== 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 dk8si2563602edb.444.2021.02.24.22.53.00; Wed, 24 Feb 2021 22:53:23 -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 S235418AbhBYGBC (ORCPT + 99 others); Thu, 25 Feb 2021 01:01:02 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:12204 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235049AbhBYGAh (ORCPT ); Thu, 25 Feb 2021 01:00:37 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DmMWz4q96zlPwg; Thu, 25 Feb 2021 13:57:51 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.498.0; Thu, 25 Feb 2021 13:59:47 +0800 From: Yanan Wang To: , , CC: Paolo Bonzini , Ben Gardon , "Sean Christopherson" , Vitaly Kuznetsov , Arnaldo Carvalho de Melo , Ingo Molnar , Andrew Jones , Peter Xu , Marc Zyngier , , , "Yanan Wang" Subject: [RFC PATCH v2 4/7] KVM: selftests: Add a helper to get system configured THP page size Date: Thu, 25 Feb 2021 13:59:37 +0800 Message-ID: <20210225055940.18748-5-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210225055940.18748-1-wangyanan55@huawei.com> References: <20210225055940.18748-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 --- .../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.19.1