Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp874549iog; Mon, 13 Jun 2022 15:04:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHSnCt9NBygUdHoa+Nibr7HZA4UYKH9o74gJ4fFzC+mYZbVcyJJDpsM4j1fp4XeT6XhpO7 X-Received: by 2002:a05:6a00:230d:b0:4f6:ec4f:35ff with SMTP id h13-20020a056a00230d00b004f6ec4f35ffmr1298483pfh.53.1655157866131; Mon, 13 Jun 2022 15:04:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655157866; cv=none; d=google.com; s=arc-20160816; b=tvRBdAJISd9DgvlmcpO7hrzbTrm3slbLSS+GY8RQ7Y1JXptJaszF3g90vXQnRVn8Iw 9qDyHJq0ycavwReKpY7o9gwLQzi5YwsDeHtHu30JuEmRJoo78lE7HU3YqtZlDVAXJk9T Q+hCeSr1cL8uvHTH3gCg0zqTfSih1t1f+DnVtt/84eBdEfpA2SsQjeLbV6/YivP05282 yoBm6WUyfpm+oIJv1F8MHJnHCE+vnjS4H2GLOpS0S34poUt5geA9I9xXpHmxRLkKTtGu 8aIvniuhydynulMXk1MrDMH/7I6HV0M9/A97zHdIoXE5qpaVIPICuJmDpAYZgVYsph9T Rwew== 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 :dkim-signature; bh=BdnFH6BzKbOFo4fS+klZ+MzOcoinhP5p7LWOzL4Vzwo=; b=NFSZXE1iXeQ+FScVsmK+/cnPBsgz71a7FTPwmIrFbjgdysTqxHbKgUgyXFfrjNfq54 vLxxBDwQDjYxJ8EZ5nbDMb7xt4FBHei01khd1rYdLVO3KxSDgJgCN032AdqDSy/LKyp4 DUOmDz4Am+1sXolr1+Yj+aMugpAv+VjK3s99MzXz0MRXG6MsM4AQg7AAZmuypwLkp4MI EmAibUj9+ZBkqnXdeHkgBuR0i7E5gQWFnaarq1Ad3YZ0aC937fwz7SiaNAyTvZN6GB1k hKV2ondcKRrAkYgiSJijnfgMF16xtW9GwkLNOAVsHZgMauIAOGpuNGMK3iMVvwrDrlB5 WHVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=C4NYruo8; 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 68-20020a630347000000b003fbb3ea3390si11018355pgd.189.2022.06.13.15.04.13; Mon, 13 Jun 2022 15:04:26 -0700 (PDT) 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=C4NYruo8; 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 S1352432AbiFMVbf (ORCPT + 99 others); Mon, 13 Jun 2022 17:31:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353113AbiFMV3S (ORCPT ); Mon, 13 Jun 2022 17:29:18 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5A571ED for ; Mon, 13 Jun 2022 14:25:26 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 78-20020a630051000000b003fe25580679so3932276pga.9 for ; Mon, 13 Jun 2022 14:25:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=BdnFH6BzKbOFo4fS+klZ+MzOcoinhP5p7LWOzL4Vzwo=; b=C4NYruo82VP57N6JKjXb2HLUBgGIsvjU6QUitx2vEKxCQiuwajCqpxyA6ZzCgZafMm RUUkyzdJ1CYt8xrKrDLEBRUYQ2ieoARlrNbfRAL8/zZCUws1BLC3s4D4ScGft4Vn0Eba HY+7TTG4mFYmHot18GeIW5AJz2e2UnWzGkAgdN08BMLG/fI3WwbJ+IaJ0HrUutjjUufJ esffVoEtQXmDXTopCFqg3ETMhsjiVUVs8H04RssK7tlwMPz9XbFy/nVJyi5/tlnre5vD eWFjoM62scHZy2eH9NdLTryX7jts6cBEeotzAm1VvCYwXva6IBsreAhC2fEWIyRmng53 PjZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=BdnFH6BzKbOFo4fS+klZ+MzOcoinhP5p7LWOzL4Vzwo=; b=pXihcuVaKDxG5hTqkVbxzIpfdbXWjvmhJlK5QUrxNOzxjhNrGs4TYuDNZv5uQQm2v/ f1fc6VOY3TKnWOSofMSj4swczvfBw7oDKiQvNURuMIhVxLrwg0a6YdRs1eM1XbbnZAuS eTP38ceYCWTs4wWUyedYrMsWPeRqa3ZUSxYzJTLAyJgccWq4zVSZPBeI2UqU82oKxL5J 4qyDjpbie1OIvSd2mw/vp4ZuDL1c7kYr+k0WSoXE3VWBVG8edkmsLzTM9XlrTzUw8/8c c6MdgCDBfcuSdXLnTEzwg41h0/8pflhdXeSWtX7pqHC1AtEl9Zg2x9WIY4q8moVgyoLe IsjQ== X-Gm-Message-State: AJIora/b8uP6aMKm3hSRPR06xdQ2lYE8dVRy2v56fjQHbq5Al7k8/aKD 5XXoQvKVes15noonYz1k9v/3hI4eysRl X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a17:902:e5cc:b0:167:5526:ea68 with SMTP id u12-20020a170902e5cc00b001675526ea68mr974312plf.133.1655155526214; Mon, 13 Jun 2022 14:25:26 -0700 (PDT) Date: Mon, 13 Jun 2022 21:25:13 +0000 Message-Id: <20220613212523.3436117-1-bgardon@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH v9 00/10] KVM: x86: Add a cap to disable NX hugepages on a VM From: Ben Gardon To: kvm@vger.kernel.org, Paolo Bonzini Cc: linux-kernel@vger.kernel.org, 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=ham 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 Given the high cost of NX hugepages in terms of TLB performance, it may be desirable to disable the mitigation on a per-VM basis. In the case of public cloud providers with many VMs on a single host, some VMs may be more trusted than others. In order to maximize performance on critical VMs, while still providing some protection to the host from iTLB Multihit, allow the mitigation to be selectively disabled. Disabling NX hugepages on a VM is relatively straightforward, but I took this as an opportunity to add some NX hugepages test coverage and clean up selftests infrastructure a bit. This series was tested with the new selftest and the rest of the KVM selftests on an Intel Haswell machine. Changelog: v1->v2: Dropped the complicated memslot refactor in favor of Ricardo Koller's patch with a similar effect. Incorporated David Dunn's feedback and reviewed by tag: shortened waits to speed up test. v2->v3: Incorporated a suggestion from David on how to build the NX huge pages test. Fixed a build breakage identified by David. Dropped the per-vm nx_huge_pages field in favor of simply checking the global + per-VM disable override. Documented the new capability Separated out the commit to test disabling NX huge pages Removed permission check when checking if the disable NX capability is supported. Added test coverage for the permission check. v3->v4: Collected RB's from Jing and David Modified stat collection to reduce a memory allocation [David] Incorporated various improvments to the NX test [David] Changed the NX disable test to run by default [David] Removed some now unnecessary commits Dropped the code to dump KVM stats from the binary stats test, and factor out parts of the existing test to library functions instead. [David, Jing, Sean] Dropped the improvement to a debugging log message as it's no longer relevant to this series. v4->v5: Incorporated cleanup suggestions from David and Sean Added a patch with style fixes for the binary stats test from Sean Added a restriction that NX huge pages can only be disabled before vCPUs are created [Sean] v5->v6: Scooped up David's RBs Added a magic token to skip nx_huge_pages_test when not run via wrapper script [Sean] Made the call to nx_huge_pages_test in the wrapper script more robust [Sean] Incorportated various nits and comment / documentation suggestions from Sean. Improved negative testing of NX disable without reboot permissions. [Sean] v6->v7: Collected Peter Xu's Reviewed-by tags Added stats metadata caching to kvm_util Misc NX test fixups v7->v8: Spell out descriptors in library function names [Sean] Reorganize stat descriptor size calculation [Sean] Addded a get_stats_descriptor helper [Sean] Remove misleading comment about error reporting in read_stat_data() [Sean] Use unsigned size_t for input to pread [Sean] Clean up read_stat_data() [Sean] Add nx_huge_pages_test to .gitignore [Sean] Fix organization of get_vm_stat() functions. [Sean] Clean up #defines in NX huge pages test [Sean] Add flag parsing and reclaim period flag to NX test [Sean] Don't reduce hugepage allocation for NX test [Sean] Fix error check when disabling NX huge pages [Sean] Don't leave reboot permissions on test binary when executing as root [Sean] v8->v9: Rebased on top of Sean's giant selftests refactor series Ben Gardon (9): KVM: selftests: Remove dynamic memory allocation for stats header KVM: selftests: Read binary stats header in lib KVM: selftests: Read binary stats desc in lib KVM: selftests: Read binary stat data in lib KVM: selftests: Add NX huge pages test KVM: x86: Fix errant brace in KVM capability handling KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis KVM: selftests: Test disabling NX hugepages on a VM KVM: selftests: Cache binary stats metadata for duration of test Sean Christopherson (1): KVM: selftests: Clean up coding style in binary stats test Documentation/virt/kvm/api.rst | 16 ++ arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/mmu/mmu_internal.h | 7 +- arch/x86/kvm/mmu/spte.c | 7 +- arch/x86/kvm/mmu/spte.h | 3 +- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/x86.c | 32 ++- include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 10 + .../selftests/kvm/include/kvm_util_base.h | 59 ++++ .../selftests/kvm/kvm_binary_stats_test.c | 138 +++++---- tools/testing/selftests/kvm/lib/kvm_util.c | 116 ++++++++ .../selftests/kvm/x86_64/nx_huge_pages_test.c | 269 ++++++++++++++++++ .../kvm/x86_64/nx_huge_pages_test.sh | 52 ++++ 15 files changed, 635 insertions(+), 80 deletions(-) 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 -- 2.36.1.476.g0c4daa206d-goog