Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1132932imi; Fri, 22 Jul 2022 18:26:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ueP8uWLUQmcyzzhKSlmCp3zdWVIt7DrWr/KkDI7YbgNApzBrVlQ0ukQABeKuC+STyolYhz X-Received: by 2002:a05:6402:524d:b0:43a:72fe:76b7 with SMTP id t13-20020a056402524d00b0043a72fe76b7mr2262433edd.398.1658539617268; Fri, 22 Jul 2022 18:26:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658539617; cv=none; d=google.com; s=arc-20160816; b=omablgU0QdubMdykaOCvgDxaQUnLOzA57q6srpFkzRcKRT3AUhzMPdwp+YM94r/Ie5 99aYFQplm1ycYteCTXiseEo4ayZnodZFywqJuQ/dafgVQEgPNmvXX2tCCd4gElVJIqm3 kkkkdy5j+TQ/AiJhWclWGINjpbrbnFaMvnez+qZ62Mj/fWZj/YSALWYGrumFsKjghaYO wpAfia8v/RUfyoQR5rNp9y/heXpGanmZCh1BDJAqtFuBO9fZiscOIaQlUL6q8RN+VjwP OMkiNSy8C+dl9zRLDytn/H37AYPMA/KNrUK1krafhRMiMtIkdGPewWiDAVnF7z4Pe0jZ YYTw== 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:reply-to:dkim-signature; bh=btEsTIYySxPVQ4hIglgkyTpjS/v7UG71CQDMcqqOrRE=; b=xc7gAqeGpniD/8FHAKJrQ6UixI0OfizkQVMZpSQEPgdgAPAwHrZAGTm8pUPX14Ya/s v2/7F90mkSLAhqZWUOSEXop3JUZnG5ILeGxp+4QvsIBDgvgWWVQJCYa5PX3KKgAAwh7S INVEBl+6g+p/967xt0lD8IFoQOJnlAQC6Xk4EZ8TKPk6yY8dURzImxR4B5O1wo1qlfUF lOdjQeaQ0b6kGhKxvsJ1bpMOmVEvs/0PZ2bJeg9liyVrTXG2BEYASt2s4OnHOf3IxyWF ESkCK7/BTw4qCT+1jQ36kVXkbv4uTVGUMH0Q6085d+KjBu6P7R80Lqym/YOjdpUdUngw o4bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=drLQt2LD; 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 z6-20020a056402274600b00435c0205537si8742050edd.29.2022.07.22.18.26.33; Fri, 22 Jul 2022 18:26:57 -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=drLQt2LD; 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 S237019AbiGWBYA (ORCPT + 99 others); Fri, 22 Jul 2022 21:24:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236563AbiGWBXr (ORCPT ); Fri, 22 Jul 2022 21:23:47 -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 8C05B97A11 for ; Fri, 22 Jul 2022 18:23:41 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id k11-20020a170902ce0b00b0016a15fe2627so3473762plg.22 for ; Fri, 22 Jul 2022 18:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=btEsTIYySxPVQ4hIglgkyTpjS/v7UG71CQDMcqqOrRE=; b=drLQt2LDSl7jNOcsHF3VkSAntBfgOcE3aoog0eZYYNxZW9kHPgZFN+oRzHmfyaACAk UrfxUyFA9DbBL14mdrQ+kuaJxOnT4gPmMCWin5z2MS9M06j8dkxe2BaekZrj5X1zFZqh BaV5P20RK38wOFTUh2q3D1r7Ac2RddSEJJyL17p6hvzPb6eGYo1CxKBQKJj9D0IfHv7s IeY8qOtkJtZFE3ukbxZgTu3600E/onAUSfzX85nah37URUu/xX+gHeFolVBvzqG6qft6 iHWglqluCmhGRmMMfgs30X8YvQ260GV6ZmGftkJKgZdAbE22Ogl2fj1tUDJE+0KpgE6e XELw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=btEsTIYySxPVQ4hIglgkyTpjS/v7UG71CQDMcqqOrRE=; b=kUImT28Fc0iXjzIc9kRpkJILDzcwWBuCeedijwIXHzGpR6ipAALN0Up7zgyiPQkca3 E2xG8Xwh3W3ZO2kXQropfRpyVA+u5ZFc5UVuIGHuWp04o2V4KnV1KNHo0E7TdWy5tghq fqAk5eQCqED7K4KR8CoxuB8jIkEeE2pk5SyU1D1Eq/wW4fOKFjOVyf/1MEMP9VPRjK9k YETom0htNsE8xJlH3mNoOuFsf6k0HVUcd3BS9/DDh0lyYyWVwIxCSHgj63Yl1lIss2+w DLbBF1pZsbWADUcaEEq12KfYHWd7vej4TDtgMuqv7fqDGIxhL983sltNQvus/qOSDRaQ G5ZQ== X-Gm-Message-State: AJIora+tHcBkYxIgowHhLV58pHEkIuSVvHxDyPWUKzNCOImBLHuJj9L7 ep2faKnVyW1nzOjuniPPcVHT0WuuPAQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:b92:b0:52a:e60d:dfbb with SMTP id g18-20020a056a000b9200b0052ae60ddfbbmr2602461pfj.72.1658539420974; Fri, 22 Jul 2022 18:23:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Sat, 23 Jul 2022 01:23:25 +0000 In-Reply-To: <20220723012325.1715714-1-seanjc@google.com> Message-Id: <20220723012325.1715714-7-seanjc@google.com> Mime-Version: 1.0 References: <20220723012325.1715714-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 6/6] KVM: x86/mmu: explicitly check nx_hugepage in disallowed_hugepage_adjust() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed , Mingwei Zhang , 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,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 From: Mingwei Zhang Explicitly check if a NX huge page is disallowed when determining if a page fault needs to be forced to use a smaller sized page. KVM incorrectly assumes that the NX huge page mitigation is the only scenario where KVM will create a shadow page instead of a huge page. Any scenario that causes KVM to zap leaf SPTEs may result in having a SP that can be made huge without violating the NX huge page mitigation. E.g. disabling of dirty logging, zapping from mmu_notifier due to page migration, guest MTRR changes that affect the viability of a huge page, etc... Fixes: b8e8c8303ff2 ("kvm: mmu: ITLB_MULTIHIT mitigation") Reviewed-by: Ben Gardon Signed-off-by: Mingwei Zhang [sean: add barrier comments, use spte_to_sp()] Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 17 +++++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index ed3cfb31853b..97980528bf4a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3092,6 +3092,19 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_ cur_level == fault->goal_level && is_shadow_present_pte(spte) && !is_large_pte(spte)) { + u64 page_mask; + + /* + * Ensure nx_huge_page_disallowed is read after checking for a + * present shadow page. A different vCPU may be concurrently + * installing the shadow page if mmu_lock is held for read. + * Pairs with the smp_wmb() in kvm_tdp_mmu_map(). + */ + smp_rmb(); + + if (!spte_to_sp(spte)->nx_huge_page_disallowed) + return; + /* * A small SPTE exists for this pfn, but FNAME(fetch) * and __direct_map would like to create a large PTE @@ -3099,8 +3112,8 @@ void disallowed_hugepage_adjust(struct kvm_page_fault *fault, u64 spte, int cur_ * patching back for them into pfn the next 9 bits of * the address. */ - u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) - - KVM_PAGES_PER_HPAGE(cur_level - 1); + page_mask = KVM_PAGES_PER_HPAGE(cur_level) - + KVM_PAGES_PER_HPAGE(cur_level - 1); fault->pfn |= fault->gfn & page_mask; fault->goal_level--; } diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index fea22dc481a0..313092d4931a 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1194,6 +1194,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) tdp_mmu_init_child_sp(sp, &iter); sp->nx_huge_page_disallowed = fault->huge_page_disallowed; + /* + * Ensure nx_huge_page_disallowed is visible before the + * SP is marked present, as mmu_lock is held for read. + * Pairs with the smp_rmb() in disallowed_hugepage_adjust(). + */ + smp_wmb(); if (tdp_mmu_link_sp(kvm, &iter, sp, true)) { tdp_mmu_free_sp(sp); -- 2.37.1.359.gd136c6c3e2-goog