Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1959507pxp; Thu, 10 Mar 2022 16:05:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzA759VHhpiingB6LfquYn2gs0JDgUpYmw5u31FF87QcemCtR03xJ7Hgu/chWwIQOXIcPOo X-Received: by 2002:a17:906:f85:b0:6d6:e97b:d276 with SMTP id q5-20020a1709060f8500b006d6e97bd276mr6418170ejj.431.1646957156633; Thu, 10 Mar 2022 16:05:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646957156; cv=none; d=google.com; s=arc-20160816; b=KO6ts/3yqVNRHkbdu8cbsAdmCWMiuxtvrPDUjvze3dvq3XZhxRkJz8EkCRWLjyv8E2 PJJAaEPyaSYcpudKetn7vrHDf1o0HObjfu5DAOIRI4ezSlLlyXhKl1SEnFeGXD+y0BE/ 7WzycRjS53Xq6YrjUINZEIwFTopgvlUIpnESgep+CWw/A1lCauXkqPHcuYEeof3VtRHe LrdSBPJ3Y0KHw8N8G67sHrU730jU0jGJ810/tIT9XUrR3ZHHiwBY8uPSp09euHbJWQFI wlwiTFfQSeAkd0v109gug7Nn7yUXUY+OhwLGmpfSizkItXof5t0vDC1pphg1b8K+McUG FgKg== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=fbxWpZEa6eY/UYN831/It4hPjXSSXrSY82CYIn9xG4k=; b=O4Fw1oYzRDC8pA02YPjJXTiKcAM33ZVtS+XMp8H3jUjQMNofzRh7SooE4415KE4Nis 8J/nsGH7YV+Dxsj09d8iNXkquoJx7rxi2DewDYf/siAHFK/y0g+LXcHIpBtgV4PD3NKO mahkdSxSVzHwoqIjpJMWcvM2cFSwfXsRe+isAUZI+cCPECu7y9QOhJHPULAbk+WMa1UJ cb/Jk615ybOktaeStoijJCFQjoIHPRHPy0VyDc+vV4juZ9HjZKNWDGXyb7YuN/vsxIE9 I07w7eDlVYbP/VuxgWB+YpOGCo7GvsQf7SK0Y20jEMslAiCDKUk7E+UhTJyx4y3bohHu TF1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Va2QgQuf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j11-20020a05640211cb00b00416a12da179si3678374edw.550.2022.03.10.16.05.34; Thu, 10 Mar 2022 16:05:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Va2QgQuf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S244041AbiCJQsk (ORCPT + 99 others); Thu, 10 Mar 2022 11:48:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244006AbiCJQr0 (ORCPT ); Thu, 10 Mar 2022 11:47:26 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43609194156 for ; Thu, 10 Mar 2022 08:46:06 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id i7-20020a170902cf0700b0015163eb319eso2960278plg.18 for ; Thu, 10 Mar 2022 08:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=fbxWpZEa6eY/UYN831/It4hPjXSSXrSY82CYIn9xG4k=; b=Va2QgQuftz6KeYn+NlohEVeWMRJNDL0ugIP1b+C9U3Y8BFpEEHWDfng5rES6HHZLOX vMH7Egye4Q+YUkyTrSZWRSrW7mxmJrFdlVfyWeiUgDEa41fqBHapehDrsTlJCG5ODnai EPQ04Bw60k1WvIyDg2ws80xj5/Ly0/4qnrFuXgx+Zre/9eqncifndEPIJj/8xKi0nBOg E1dlQr+uuFm4cRzpo49sDK6OoP/1CVNlVSV0LEBMJkZQD9ZbVedzSAA/YHkBEYHC5FUA JLAwbQsquD47DVpGdhkP34ch3hQ4bTVO3jkZyiNGoPrPgrsJpSH5joz2+mtrnOcmA1CO beHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=fbxWpZEa6eY/UYN831/It4hPjXSSXrSY82CYIn9xG4k=; b=TYFNQlFptjHL1CxAHOIhuSQgJfz8TED5emZTo9EVGn2Z3UbRBcaAN86OfvQ0Mkt4Wa GCPr9Gj86ALRWxjl+0KRjOqX60dTLvWrebt3uhL4zBHd32vKbPRCuNf7wF5UZGe39QRs qVs9MTECZYDdcs7zbwhkx9Pp9fShiPaZe9Gxbz9QyUEwmMt65uh+gWspIes9SScAY/8Q Ov85vOjx+i5v2JJ6wFwi6x4ZqHONXUPxJVO3HJCuRZIXUrDsZ/Km7ZF7DnTcHG2DFFbC g4ffzPklaDDoHFeHPbqbjDvlEPKo/nZrgNJrlSxwYrqVtPGExUw/4ZSx56NG21LelmjT rovA== X-Gm-Message-State: AOAM531o95EJ3pSPO7WC5CgHFLhvkawIqKR8WpEl7EV2dxACI82+AR+c RYN/fH4wu44YRLSopZY2sWco6OicHrSyats4er3sahy0YTfYaLBx6UD7WfOvY7JBzxHvFfM/Znq kbjIHMUvcBIJbyiyPIeog4eWSWK++se0aQNKOfRo3lLmW0K/7bdvnhpxIEK0YUAXVcIdSgdc4 X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:2d58:733f:1853:8e86]) (user=bgardon job=sendgmr) by 2002:a63:715:0:b0:380:cf1d:6765 with SMTP id 21-20020a630715000000b00380cf1d6765mr4732500pgh.577.1646930765503; Thu, 10 Mar 2022 08:46:05 -0800 (PST) Date: Thu, 10 Mar 2022 08:45:26 -0800 In-Reply-To: <20220310164532.1821490-1-bgardon@google.com> Message-Id: <20220310164532.1821490-8-bgardon@google.com> Mime-Version: 1.0 References: <20220310164532.1821490-1-bgardon@google.com> X-Mailer: git-send-email 2.35.1.616.g0bdcbb4464-goog Subject: [PATCH 07/13] selftests: KVM: Add NX huge pages test From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Jim Mattson , David Dunn , Jing Zhang , Junaid Shahid , Ben Gardon Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There's currently no test coverage of NX hugepages in KVM selftests, so add a basic test to ensure that the feature works as intended. Signed-off-by: Ben Gardon --- tools/testing/selftests/kvm/Makefile | 3 +- .../kvm/lib/x86_64/nx_huge_pages_guest.S | 45 +++++++ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 122 ++++++++++++++++++ .../kvm/x86_64/nx_huge_pages_test.sh | 25 ++++ 4 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/lib/x86_64/nx_huge_pages_guest.S create mode 100644 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c create mode 100755 tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 04099f453b59..6ee30c0df323 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -38,7 +38,7 @@ ifeq ($(ARCH),riscv) endif LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c -LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S +LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S lib/x86_64/nx_huge_pages_guest.S LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c LIBKVM_riscv = lib/riscv/processor.c lib/riscv/ucall.c @@ -56,6 +56,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test +TEST_GEN_PROGS_x86_64 += x86_64/nx_huge_pages_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id diff --git a/tools/testing/selftests/kvm/lib/x86_64/nx_huge_pages_guest.S b/tools/testing/selftests/kvm/lib/x86_64/nx_huge_pages_guest.S new file mode 100644 index 000000000000..09c66b9562a3 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/nx_huge_pages_guest.S @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * tools/testing/selftests/kvm/nx_huge_page_guest.S + * + * Copyright (C) 2022, Google LLC. + */ + +.include "kvm_util.h" + +#define HPAGE_SIZE (2*1024*1024) +#define PORT_SUCCESS 0x70 + +.global guest_code0 +.global guest_code1 + +.align HPAGE_SIZE +exit_vm: + mov $0x1,%edi + mov $0x2,%esi + mov a_string,%edx + mov $0x1,%ecx + xor %eax,%eax + jmp ucall + + +guest_code0: + mov data1, %eax + mov data2, %eax + jmp exit_vm + +.align HPAGE_SIZE +guest_code1: + mov data1, %eax + mov data2, %eax + jmp exit_vm +data1: +.quad 0 + +.align HPAGE_SIZE +data2: +.quad 0 +a_string: +.string "why does the ucall function take a string argument?" + + diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c new file mode 100644 index 000000000000..5cbcc777d0ab --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * tools/testing/selftests/kvm/nx_huge_page_test.c + * + * Usage: to be run via nx_huge_page_test.sh, which does the necessary + * environment setup and teardown + * + * Copyright (C) 2022, Google LLC. + */ + +#define _GNU_SOURCE + +#include +#include + +#include +#include "kvm_util.h" + +#define HPAGE_SLOT MEMSLOT(10) +#define HPAGE_PADDR_START (10*1024*1024) +#define HPAGE_SLOT_NPAGES (100*1024*1024/4096) + +/* Defined in nx_huge_page_guest.S */ +void guest_code0(void); +void guest_code1(void); + +static void run_guest_code(struct kvm_vm *vm, void (*guest_code)(void)) +{ + struct kvm_regs regs; + + vcpu_regs_get(vm, 0, ®s); + regs.rip = (uint64_t)guest_code; + vcpu_regs_set(vm, 0, ®s); + vcpu_run(vm, 0); +} + +static void check_2m_page_count(struct kvm_vm *vm, int expected_pages_2m) +{ + int actual_pages_2m; + + actual_pages_2m = vm_get_single_stat(vm, "pages_2m"); + + TEST_ASSERT(actual_pages_2m == expected_pages_2m, + "Unexpected 2m page count. Expected %d, got %d", + expected_pages_2m, actual_pages_2m); +} + +static void check_split_count(struct kvm_vm *vm, int expected_splits) +{ + int actual_splits; + + actual_splits = vm_get_single_stat(vm, "nx_lpage_splits"); + + TEST_ASSERT(actual_splits == expected_splits, + "Unexpected nx lpage split count. Expected %d, got %d", + expected_splits, actual_splits); +} + +int main(int argc, char **argv) +{ + struct kvm_vm *vm; + + vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR); + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB, + HPAGE_PADDR_START, HPAGE_SLOT, + HPAGE_SLOT_NPAGES, 0); + + kvm_vm_elf_load(vm, program_invocation_name, HPAGE_SLOT); + + vm_vcpu_add_default(vm, 0, guest_code0); + + check_2m_page_count(vm, 0); + check_split_count(vm, 0); + + /* + * Running guest_code0 will access data1 and data2. + * This should result in part of the huge page containing guest_code0, + * and part of the hugepage containing the ucall function being mapped + * at 4K. The huge pages containing data1 and data2 will be mapped + * at 2M. + */ + run_guest_code(vm, guest_code0); + check_2m_page_count(vm, 2); + check_split_count(vm, 2); + + /* + * guest_code1 is in the same huge page as data1, so it will cause + * that huge page to be remapped at 4k. + */ + run_guest_code(vm, guest_code1); + check_2m_page_count(vm, 1); + check_split_count(vm, 3); + + /* Run guest_code0 again to check that is has no effect. */ + run_guest_code(vm, guest_code0); + check_2m_page_count(vm, 1); + check_split_count(vm, 3); + + /* Give recovery thread time to run */ + sleep(3); + check_2m_page_count(vm, 1); + check_split_count(vm, 0); + + /* + * The split 2M pages should have been reclaimed, so run guest_code0 + * again to check that pages are mapped at 2M again. + */ + run_guest_code(vm, guest_code0); + check_2m_page_count(vm, 2); + check_split_count(vm, 2); + + /* Pages are once again split from running guest_code1. */ + run_guest_code(vm, guest_code1); + check_2m_page_count(vm, 1); + check_split_count(vm, 3); + + kvm_vm_free(vm); + + return 0; +} + diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh new file mode 100755 index 000000000000..a5f946fb0626 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only */ + +# tools/testing/selftests/kvm/nx_huge_page_test.sh +# Copyright (C) 2022, Google LLC. + +NX_HUGE_PAGES=$(cat /sys/module/kvm/parameters/nx_huge_pages) +NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio) +NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms) +HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages) + +echo 1 > /sys/module/kvm/parameters/nx_huge_pages +echo 1 > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo 2 > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo 200 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +./nx_huge_pages_test +RET=$? + +echo $NX_HUGE_PAGES > /sys/module/kvm/parameters/nx_huge_pages +echo $NX_HUGE_PAGES_RECOVERY_RATIO > /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio +echo $NX_HUGE_PAGES_RECOVERY_PERIOD > /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms +echo $HUGE_PAGES > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages + +exit $RET -- 2.35.1.616.g0bdcbb4464-goog