Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1571618pxb; Sun, 22 Aug 2021 22:17:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzU4qTloknG0hfnzVE5O5oxYtk7Ua0RjX0wYridz6HcIl1+cWtKFt5VP8e/4LKGzWmWe2N0 X-Received: by 2002:a05:6e02:1250:: with SMTP id j16mr22010395ilq.215.1629695849317; Sun, 22 Aug 2021 22:17:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629695849; cv=none; d=google.com; s=arc-20160816; b=elW/qESqyQUve64ZdSHL1Vx2ExaKTJrhX9Mm0R/gsxynUub/viZQ0FOhzBqRekCF4F +bouDkPpwgqiysD0rQjihxCyHMEsDy+1iVPlmng7FqLH4NfVfePtTJ3U5eA9A2BGlat3 9n1c2+zrYG7sj1iauirXNL9FikaDOnhwFqLqJNYSPYeQxV/rWqpP85BfzXn0DmgUkaB+ YPCqAcu9Huxl1EH0lCHQZc9FtNO8RXGixk4ptK8s5AHU02Afqvke01jRuwDqBCaKm1Ci eEmEulRQIKr6XnvauNnTxFUB7IIUk5U0F7LLSNaXvLosLcbQuNlGydCtSUPjyYy1KFDe 3AVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :reply-to:dkim-signature; bh=SZp9jopn6L2gGcjaBST5d963oFablXCgT4VwmaYX6c8=; b=a6AuYI/468HL8YmzQ1jPUc3Cq13K8DDZ/eVDT2F5VVkUiGH6gtyA2+nDEHvLsJIAtI D+mhOjyjvyz/u8zjEH98e+J9uR04G32lGpKtUkETZd8sTpN49Cno7xF/LG3AhNPiUuBl yIEtDgI4SqCGRudELto9ZDTeDgLutN2QUOfzv6DPgUZxu4Q2VTDs9xGmJKS3CkD/JqNG aGyGIk9s8CytlisgBxkat/KwxjzorCoU9a75djNRPWxCGFcUiIhndUpnyjcDrrTOi27W CWa4BTSWIpwytxrrvGqFe1FR7V03WB29iA5KBqlhdHRuSMeRWEXINmLsYQ2nqho/80Dr KyFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=BWDOf9o4; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u15si13374813ilq.38.2021.08.22.22.17.17; Sun, 22 Aug 2021 22:17:29 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=BWDOf9o4; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232915AbhHWFRS (ORCPT + 99 others); Mon, 23 Aug 2021 01:17:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231267AbhHWFRM (ORCPT ); Mon, 23 Aug 2021 01:17:12 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3133C061575 for ; Sun, 22 Aug 2021 22:16:30 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id a62-20020a254d410000b0290592f360b0ccso15442760ybb.14 for ; Sun, 22 Aug 2021 22:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:message-id:mime-version:subject:from:to:cc; bh=SZp9jopn6L2gGcjaBST5d963oFablXCgT4VwmaYX6c8=; b=BWDOf9o4rquaek14MDqqotPhxfN7ahfaqMx6tz5jr8+1ICDTokXwzaWj9OiWvGGlCY xhpf17kFVXBYtpSAPmuHEzf8x0lpZylcyF/q96KBfzZOdcd+rnm+AnGXTqFvzCNdv1nx lcQRiwvQrD9I1j42OQo3g2Wfc7/jWA8TPQzhZFgtDg3tNEpOtdgao/VPZ4Q7US7woJvr JIXVPexNEV9hSe9N3zGuTM4TbsYWIrafPJnq/+dbaafxrjVxhSOsn3O1KNpXRTQ7X+zz s1VBxLXXdRvPBRGp1nfHT3OSPatiwwllLizCMmrMv6a/yhg+hSnhUldaNCHO5KFLQXj0 tm5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:message-id:mime-version:subject :from:to:cc; bh=SZp9jopn6L2gGcjaBST5d963oFablXCgT4VwmaYX6c8=; b=ufxHC9u7KBS4PayBj0cE+aRYBjQ2PsWwLubP4pXHhIh3EhuiMbHkev26rKstzTDIqZ 4EnlNVVe5hvyHWzHdaojU4kZhgA7MDKP/E+y8ZTE0kpnH/LoskGVnrEzB/BufHiQnGa0 DxdAAlT/Oa6cS0LRV8fu0raja6MHtzdhzBBniVZYEcTVGjxD5dX0INxme+P1bcmM2hCg ez+dF9nOn++BvuKDGgp5Zd/jjc9xy/I5CdqJIuUk0ffAADBVBSiNs/B9xzm00ID2eDzd 4VDWVXuc7lPcH049fPg5+QUIXjRBNqwCXXPM24b3n2fE+neN1GQiA2fsLfWswlGaggj+ WalA== X-Gm-Message-State: AOAM532c5JAjdZ9bZzowrRCgdYljelNSCHVrXIrsgG3uyS3VrAlT57rJ qleAOIgT4Ok8GefAiEGTUNMYdthco+Xk X-Received: from mizhang-super.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1071]) (user=mizhang job=sendgmr) by 2002:a25:da89:: with SMTP id n131mr41861469ybf.255.1629695789605; Sun, 22 Aug 2021 22:16:29 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 23 Aug 2021 05:16:22 +0000 Message-Id: <20210823051622.312890-1-mizhang@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.rc2.250.ged5fa647cd-goog Subject: [PATCH] selftests: KVM: use dirty logging to check if page stats work correctly From: Mingwei Zhang To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Christopherson , David Matlack , Jing Zhang , Peter Xu , Ben Gardon , Mingwei Zhang Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When dirty logging is enabled, KVM splits the all hugepage mapping in NPT/EPT into the smallest 4K size. This property could be used to check if the page stats metrics work properly in KVM mmu. At the same time, this logic might be used the other way around: using page stats to verify if dirty logging really splits all huge pages. So add page stats checking in dirty logging performance selftest. In particular, add checks in three locations: - just after vm is created; - after populating memory into vm but before enabling dirty logging; - after turning off dirty logging. Tested using commands: - ./dirty_log_perf_test -s anonymous_hugetlb_1gb - ./dirty_log_perf_test -s anonymous_thp Cc: Sean Christopherson Cc: David Matlack Cc: Jing Zhang Cc: Peter Xu Suggested-by: Ben Gardon Signed-off-by: Mingwei Zhang --- .../selftests/kvm/dirty_log_perf_test.c | 30 +++++++++++++++++++ .../testing/selftests/kvm/include/test_util.h | 1 + .../selftests/kvm/lib/perf_test_util.c | 3 ++ tools/testing/selftests/kvm/lib/test_util.c | 29 ++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index 3c30d0045d8d..e190f6860166 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -19,6 +19,10 @@ #include "perf_test_util.h" #include "guest_modes.h" +#ifdef __x86_64__ +#include "processor.h" +#endif + /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ #define TEST_HOST_LOOP_N 2UL @@ -166,6 +170,14 @@ static void run_test(enum vm_guest_mode mode, void *arg) vm = perf_test_create_vm(mode, nr_vcpus, guest_percpu_mem_size, p->slots, p->backing_src); +#ifdef __x86_64__ + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_4K) == 0, + "4K page is non zero"); + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_2M) == 0, + "2M page is non zero"); + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_1G) == 0, + "1G page is non zero"); +#endif perf_test_args.wr_fract = p->wr_fract; guest_num_pages = (nr_vcpus * guest_percpu_mem_size) >> vm_get_page_shift(vm); @@ -211,6 +223,16 @@ static void run_test(enum vm_guest_mode mode, void *arg) pr_info("Populate memory time: %ld.%.9lds\n", ts_diff.tv_sec, ts_diff.tv_nsec); +#ifdef __x86_64__ + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_4K) != 0, + "4K page is zero"); + if (p->backing_src == VM_MEM_SRC_ANONYMOUS_THP) + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_2M) != 0, + "2M page is zero"); + if (p->backing_src == VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB) + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_1G) != 0, + "1G page is zero"); +#endif /* Enable dirty logging */ clock_gettime(CLOCK_MONOTONIC, &start); enable_dirty_logging(vm, p->slots); @@ -256,6 +278,14 @@ static void run_test(enum vm_guest_mode mode, void *arg) iteration, ts_diff.tv_sec, ts_diff.tv_nsec); } } +#ifdef __x86_64__ + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_4K) != 0, + "4K page is zero after dirty logging"); + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_2M) == 0, + "2M page is non-zero after dirty logging"); + TEST_ASSERT(get_page_stats(X86_PAGE_SIZE_1G) == 0, + "1G page is non-zero after dirty logging"); +#endif /* Disable dirty logging */ clock_gettime(CLOCK_MONOTONIC, &start); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index d79be15dd3d2..dca5fcf7aa87 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -102,6 +102,7 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i); size_t get_backing_src_pagesz(uint32_t i); void backing_src_help(void); enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); +size_t get_page_stats(uint32_t page_level); /* * Whether or not the given source type is shared memory (as opposed to diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c index 0ef80dbdc116..c2c532990fb0 100644 --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -96,6 +96,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int vcpus, #ifdef __s390x__ /* Align to 1M (segment size) */ guest_test_phys_mem &= ~((1 << 20) - 1); +#elif __x86_64__ + /* Align to 1G (segment size) to allow hugepage mapping. */ + guest_test_phys_mem &= ~((1 << 30) - 1); #endif pr_info("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem); diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index af1031fed97f..07eb6b5c125e 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -15,6 +15,13 @@ #include "linux/kernel.h" #include "test_util.h" +#include "processor.h" + +static const char * const pagestat_filepaths[] = { + "/sys/kernel/debug/kvm/pages_4k", + "/sys/kernel/debug/kvm/pages_2m", + "/sys/kernel/debug/kvm/pages_1g", +}; /* * Parses "[0-9]+[kmgt]?". @@ -141,6 +148,28 @@ size_t get_trans_hugepagesz(void) return size; } +#ifdef __x86_64__ +size_t get_stats_from_file(const char *path) +{ + size_t value; + FILE *f; + + f = fopen(path, "r"); + TEST_ASSERT(f != NULL, "Error in opening file: %s\n", path); + + fscanf(f, "%ld", &value); + fclose(f); + + return value; +} + +size_t get_page_stats(uint32_t page_level) +{ + TEST_ASSERT(page_level <= X86_PAGE_SIZE_1G, "page type error."); + return get_stats_from_file(pagestat_filepaths[page_level]); +} +#endif + size_t get_def_hugetlb_pagesz(void) { char buf[64]; -- 2.33.0.rc2.250.ged5fa647cd-goog