Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1036457ioo; Thu, 26 May 2022 23:13:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyN7fymupgdWoyyNFKy5Ie12ydxW56VomCRx/9EO7UbOW5ml628JHpreE/xM5fK1Dg8O6Ea X-Received: by 2002:a05:6402:190a:b0:42b:60b:592b with SMTP id e10-20020a056402190a00b0042b060b592bmr40454149edz.127.1653631992358; Thu, 26 May 2022 23:13:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653631992; cv=none; d=google.com; s=arc-20160816; b=F0CQY+C5m/vyHAXPgRNC+HMGL8ezmwMK1/DI8h2VRTAHel/eK5B98hzZheo4vtDPzB qMmM0sa+o+DYqahMAPZBFdUp2Oldbrf9MbVUp1G+LFilgGgdpm2WIsyM5BB4EgV1EitV lA/dzYivrjA4W0K8hydpaRzDWI07w/6+3VRUBJC4CyJOt46Q7q3FhdC9iUfIyBBWVShn LiNFYMtIAFQH59rXzZLNt/F5KUxfm1MfJgkwfpfqR5OWooOksjXLhC6mxG6nRj50FIOD 01bitiSi7k70mvCJmXddtXwP0rSTH0xd/Vyt1ffmUmAJTHVX3ie2l0xz13gfavnJT27N ys/A== 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=nnvcwssuqNg42L7MdpU79nR8N7GTvFyXSrqknrZfDjo=; b=UVA7EAyN2wayi4pW8rH9AZoplkrpY3oLLLegxe8mzB9esrpxO/Hz1Y6it/Z+QUPnol GYvCjYr619MXwA36hncudfuk9FQaOSZAHIQZLXIWj1OZBhK1VL8MQlkHj8/azrLB3/Ph ksX9roV+XA/Wt9KEnn8ilt/+5CatwyDSYufRvLxm6mVE3n/1dIgbqAM2bwmfVYV/i+Bm w0W2o+XYbHDtEFCRIor5O9iuG18/L60kupjDW3VfnAPyyrpg4LUNApOmDY5+npaULJrz 6cIze814VwGykOYn0HSUJkYWcz+jYyaMqZa3ZfhvSxxvmzdSlgsQlC51HZprjI6LT2EA bZmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=WLdOu5EN; 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 o8-20020a170906974800b006feb850156bsi1185813ejy.978.2022.05.26.23.12.47; Thu, 26 May 2022 23:13:12 -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=WLdOu5EN; 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 S1347360AbiEZRyP (ORCPT + 99 others); Thu, 26 May 2022 13:54:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232984AbiEZRyN (ORCPT ); Thu, 26 May 2022 13:54:13 -0400 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 76D5E9EB44 for ; Thu, 26 May 2022 10:54:12 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id s10-20020a170902a50a00b00162359521c9so1423846plq.23 for ; Thu, 26 May 2022 10:54:12 -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=nnvcwssuqNg42L7MdpU79nR8N7GTvFyXSrqknrZfDjo=; b=WLdOu5EN26dmTPjq6cGXkXU2d+1gDe0PvlIKHs79XwihlBmMAWlRdWuFL0BS8xGQd0 bYOSF2WpSi/gjnHcgX34So716bgviOt5/JwG8w8E+ZCcNecoPtncMDxzeLDiJcvs7bmk av3k9IC+nGKEwlVT/AVCGHDKTUQNEFe+YcFHQKLqtyrepmqiZwwDYPRZMg7jei9RZ/Yy 91uIL8lhF4xIo+XmGAcMJLNuvbk5/v087WbQRcHnTuPpDe3K5gVAWl/sGNYTf2nvIz1M BrMJi2FzY0OA6M+e18xILX+OgNFphm+h/31Sv93GTbJUzmfVvSG18GP3qEGRzj4TZS2c IUeQ== 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=nnvcwssuqNg42L7MdpU79nR8N7GTvFyXSrqknrZfDjo=; b=MRd+kxReWPy46w2TikOf/0eIhoBrhV+RYJhi/wXE+jxPwlF+QbZJdKBf6EhXFPxLBT OTQfkMwlj1LtJHssgP4zA70gyn73mNi48GSD+7bzUKNfFLDZJvjtVn6H4+hKOQkOWXKF /4jCAhXvAIDD3IXu08nJjvMwxuAEhVxi4jcr21KTbnp1JcnAaB5+YVTDnsTmB5v6Uosc osvBXeVKDi2iWGNqx15tZGJSPJrOB3DqsqxjP1PvVW3XJEEigFVLPIlXTh6zL36ELisG 6nUnhPRlA8mHtrJAmQXjRmNvJhZuuUivBkQjkY3CdTCaXwlRVUDday1cT+yu9pnLMRc6 sI3A== X-Gm-Message-State: AOAM531jQ9NYIGPLzqONYRPJZolglQ3WZPxagvzJUWGlj6D6TyB9Blfl k2yASsV1NbEzARv91S2iuvJ290y/mrxz X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a17:903:22c4:b0:163:6700:d27b with SMTP id y4-20020a17090322c400b001636700d27bmr7031444plg.37.1653587651948; Thu, 26 May 2022 10:54:11 -0700 (PDT) Date: Thu, 26 May 2022 17:53:57 +0000 Message-Id: <20220526175408.399718-1-bgardon@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v8 00/11] 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] Ben Gardon (10): 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: Factor out calculation of pages needed for a VM 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 | 17 ++ 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 | 46 +++ .../selftests/kvm/kvm_binary_stats_test.c | 138 +++++---- tools/testing/selftests/kvm/lib/kvm_util.c | 223 ++++++++++++-- .../selftests/kvm/lib/kvm_util_internal.h | 5 + .../selftests/kvm/x86_64/nx_huge_pages_test.c | 271 ++++++++++++++++++ .../kvm/x86_64/nx_huge_pages_test.sh | 52 ++++ 16 files changed, 720 insertions(+), 97 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.124.g0e6072fb45-goog