From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DEF3AC433F5 for ; Sat, 23 Apr 2022 03:48:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231573AbiDWDvQ (ORCPT ); Fri, 22 Apr 2022 23:51:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232670AbiDWDvI (ORCPT ); Fri, 22 Apr 2022 23:51:08 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AFFC1BD5C2 for ; Fri, 22 Apr 2022 20:48:13 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id j17-20020a62b611000000b004fa6338bd77so6549489pff.10 for ; Fri, 22 Apr 2022 20:48:13 -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=UpDwvHE3te50hhADXZwRX35cXLlp2sOCZQHjdNcE0rc=; b=COJtGw4Wavj/9yPBesyN/SgXH/HIWMsbDBmf9ECoXEaMXvPup15jFuJN/PKGgWnlrB jXQrLxE14lurQzFajjzsr6+93rpNVy0pIwYNdg8JIDm7VJdeWKnfDokdHbFVGSsHe+gq eF20CJ8Xb1pTUI6pAPJPUM2864n/FJqbcrJ0YL6xAH5iQ+PvmulL5BGd3XB/HtKO6VhW e/DLavN3/K0BJ4walqhBRroYuU43zEWA3Pyp2XgG7MNM/htDDPvXuw+CNjngMubCA+ND 3tuYU6togv1tI/lvupH9gh2cO0JI1qNgVAmez5RGIc2Yz1u2xisxETb4wB3qCtqDONdi iBTw== 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=UpDwvHE3te50hhADXZwRX35cXLlp2sOCZQHjdNcE0rc=; b=7ssOyZMKq6LrVvtClPgjFmAQQfkaikpCE1BnWRTpRm94OrVLbyneIoOvJoakckgvA2 nCo3K4YCOZgCV8/dJKO20kq5Nd+AWi8cyBQNjld3UuKSoroNxW1dvX2w6yCLedOrtGl3 S7we8jhfodGrjvlZfon9u+Welz7G1VImEW+YpgWmDepLbId7FmBeSANbQWHfLd7AAd1i Gkvc0iGVZNZQvLn6/tYIVHa+CSjoxUqoDE+H/jm7VhtGpfyCxHTxWeCKTAiy2uxwUbaZ FokJxMOsfCjC499FqFDVIoq/IEmwrnX1k7vtNYb5djM13wUF5eB2m97l+CNMtFFyP15f FSkw== X-Gm-Message-State: AOAM531UDxPuDDLX1SEeQWnvMrmUjC9RHzAehTlElaCJP0d9nYqZZs7I OD0qQYmSi3LC4E0z0gfAs5Lq5Y4Mumc= X-Google-Smtp-Source: ABdhPJyXteO9JqYc3L2JBj7BFHCj5A5q1lxAeJctmp3IloEpb2fcn+tkkeSHQ58pOpEy2m1zzPRxNbsLstQ= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:902:c14a:b0:15b:9c29:935a with SMTP id 10-20020a170902c14a00b0015b9c29935amr7612387plj.2.1650685692822; Fri, 22 Apr 2022 20:48:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Sat, 23 Apr 2022 03:47:41 +0000 In-Reply-To: <20220423034752.1161007-1-seanjc@google.com> Message-Id: <20220423034752.1161007-2-seanjc@google.com> Mime-Version: 1.0 References: <20220423034752.1161007-1-seanjc@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH 01/12] KVM: x86/mmu: Don't treat fully writable SPTEs as volatile (modulo A/D) From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , David Matlack , Venkatesh Srinivas , Chao Peng Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Don't treat SPTEs that are truly writable, i.e. writable in hardware, as being volatile (unless they're volatile for other reasons, e.g. A/D bits). KVM _sets_ the WRITABLE bit out of mmu_lock, but never _clears_ the bit out of mmu_lock, so if the WRITABLE bit is set, it cannot magically get cleared just because the SPTE is MMU-writable. Rename the wrapper of MMU-writable to be more literal, the previous name of spte_can_locklessly_be_made_writable() is wrong and misleading. Fixes: c7ba5b48cc8d ("KVM: MMU: fast path of handling guest page fault") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 17 +++++++++-------- arch/x86/kvm/mmu/spte.h | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 904f0faff218..612316768e8e 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -481,13 +481,15 @@ static bool spte_has_volatile_bits(u64 spte) * also, it can help us to get a stable is_writable_pte() * to ensure tlb flush is not missed. */ - if (spte_can_locklessly_be_made_writable(spte) || - is_access_track_spte(spte)) + if (!is_writable_pte(spte) && is_mmu_writable_spte(spte)) + return true; + + if (is_access_track_spte(spte)) return true; if (spte_ad_enabled(spte)) { - if ((spte & shadow_accessed_mask) == 0 || - (is_writable_pte(spte) && (spte & shadow_dirty_mask) == 0)) + if (!(spte & shadow_accessed_mask) || + (is_writable_pte(spte) && !(spte & shadow_dirty_mask))) return true; } @@ -554,7 +556,7 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte) * we always atomically update it, see the comments in * spte_has_volatile_bits(). */ - if (spte_can_locklessly_be_made_writable(old_spte) && + if (is_mmu_writable_spte(old_spte) && !is_writable_pte(new_spte)) flush = true; @@ -1192,7 +1194,7 @@ static bool spte_write_protect(u64 *sptep, bool pt_protect) u64 spte = *sptep; if (!is_writable_pte(spte) && - !(pt_protect && spte_can_locklessly_be_made_writable(spte))) + !(pt_protect && is_mmu_writable_spte(spte))) return false; rmap_printk("spte %p %llx\n", sptep, *sptep); @@ -3171,8 +3173,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) * be removed in the fast path only if the SPTE was * write-protected for dirty-logging or access tracking. */ - if (fault->write && - spte_can_locklessly_be_made_writable(spte)) { + if (fault->write && is_mmu_writable_spte(spte)) { new_spte |= PT_WRITABLE_MASK; /* diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index ad8ce3c5d083..570699682f6d 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -398,7 +398,7 @@ static inline void check_spte_writable_invariants(u64 spte) "kvm: Writable SPTE is not MMU-writable: %llx", spte); } -static inline bool spte_can_locklessly_be_made_writable(u64 spte) +static inline bool is_mmu_writable_spte(u64 spte) { return spte & shadow_mmu_writable_mask; } -- 2.36.0.rc2.479.g8af0fa9b8e-goog