Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1890457pxb; Fri, 24 Sep 2021 14:32:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRAxou2uOWW5yoAWAQw1a3Wt3M3hbbi2J79LjwJVHNfwO/8MHaXzIdrRzLVruDRq1cGuAY X-Received: by 2002:a6b:6a13:: with SMTP id x19mr11147345iog.111.1632519176626; Fri, 24 Sep 2021 14:32:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632519176; cv=none; d=google.com; s=arc-20160816; b=yo4eGwDtEmTDKuBQTCNb5lLVB4jGHvLnBlqFleHVzGeuFTGkRk90ShAHHKN9zv4xsm S8tVdMzL3WDSDB6yJ/A0oaKVwcFadtHoy3JEkAfe6kH7V4MzEQegRy3hxmcxzQsdMhwP FEvUo7T0YZSRMqNO6xhz2PYQ4E53mcw890Q0llB8GgQ0+iFI4qKfZR/bxBH53fnYSJYp WqgCiX5dBp+CYL1VJ5Slfngbmp+f0woyW2/ehumPrC+5F5XCMimSt+fuvxTziEqiGfB0 UJcaxxdvbjxVvslj+hwjLM8Qme22/oXyPdIr5gxsViBMKCwQ8cxcETsp32r34b1exocF FE+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GTogtUZapQH1IOFeY6BrBepqa21A5XNaqz5dVvZ4Lo0=; b=MLdIRP5saxolGXhy+e3m/DWvkpcm2mwQNa/6Dvje4qZO5LsBkATrYJzCnswd/EQOjj Bodd3cRXsUMPMTpCo0Vg2PSDoTVV9s8Op68eRdkqWhJMHAsPF5oW3m2AbOUcKOTsQQ8I zDPOGRwhT/AouieB+uvLlWQQa43Q0QWSAd/ZiOkLUjAMhXCjCC3r/PxGbZdCBN+73njh guVWABVcBt0Ra9FMTDIerXSElcIYCR1fvWFld+BBjRK3Vy/MUaB8ujZ1Xf33mSdvOI3W UN97hMyF5D9heYIXPlotpLtlNbnf6bkH2Osc8Nb5jsvnGI9awNaMi4fvwdeDXAZEU0fd JJbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aeEsRNit; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m82si10602093ioa.74.2021.09.24.14.32.44; Fri, 24 Sep 2021 14:32:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aeEsRNit; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231430AbhIXQen (ORCPT + 99 others); Fri, 24 Sep 2021 12:34:43 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23240 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347574AbhIXQdq (ORCPT ); Fri, 24 Sep 2021 12:33:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632501132; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GTogtUZapQH1IOFeY6BrBepqa21A5XNaqz5dVvZ4Lo0=; b=aeEsRNitNDSAfNCZ1tkUVqNKethH3IfuTKeP0ham54oA4mX5sy/ZRW5gwwYs+YVUYNsnGZ W9TQ1S9vrlQtQMAfq9S+nZr/7ZSCPKIpLFbUaNZL1A9TuheOisgQsjPc6MW0UMhSnY9EZy 2/cxfdWlnUiU4mqL5KRdJn8lOb06UTI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-366-9Bwbwqb_M-6SREC2P4MDCg-1; Fri, 24 Sep 2021 12:32:06 -0400 X-MC-Unique: 9Bwbwqb_M-6SREC2P4MDCg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AF19E10168E7; Fri, 24 Sep 2021 16:32:03 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4F4EF60E1C; Fri, 24 Sep 2021 16:32:03 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: dmatlack@google.com, seanjc@google.com Subject: [PATCH v3 18/31] KVM: MMU: mark page dirty in make_spte Date: Fri, 24 Sep 2021 12:31:39 -0400 Message-Id: <20210924163152.289027-19-pbonzini@redhat.com> In-Reply-To: <20210924163152.289027-1-pbonzini@redhat.com> References: <20210924163152.289027-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This simplifies set_spte, which we want to remove, and unifies code between the shadow MMU and the TDP MMU. The warning will be added back later to make_spte as well. There is a small disadvantage in the TDP MMU; it may unnecessarily mark a page as dirty twice if two vCPUs end up mapping the same page twice. However, this is a very small cost for a case that is already rare. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 3 --- arch/x86/kvm/mmu/spte.c | 3 +++ arch/x86/kvm/mmu/tdp_mmu.c | 21 +-------------------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 8b6dc276935f..5a757953b98b 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2688,9 +2688,6 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, ret = make_spte(vcpu, pte_access, level, gfn, pfn, *sptep, speculative, can_unsync, host_writable, sp_ad_disabled(sp), &spte); - if (spte & PT_WRITABLE_MASK) - kvm_vcpu_mark_page_dirty(vcpu, gfn); - if (*sptep == spte) ret |= SET_SPTE_SPURIOUS; else if (mmu_spte_update(sptep, spte)) diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index a33c581aabd6..66be9452ded1 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -179,6 +179,9 @@ int make_spte(struct kvm_vcpu *vcpu, unsigned int pte_access, int level, "spte = 0x%llx, level = %d, rsvd bits = 0x%llx", spte, level, get_rsvd_bits(&vcpu->arch.mmu->shadow_zero_check, spte, level)); + if (spte & PT_WRITABLE_MASK) + kvm_vcpu_mark_page_dirty(vcpu, gfn); + *new_spte = spte; return ret; } diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 3bf85a8c7d15..b41b6f5ea82b 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -542,26 +542,7 @@ static inline bool tdp_mmu_map_set_spte_atomic(struct kvm_vcpu *vcpu, struct tdp_iter *iter, u64 new_spte) { - struct kvm *kvm = vcpu->kvm; - - if (!tdp_mmu_set_spte_atomic_no_dirty_log(kvm, iter, new_spte)) - return false; - - /* - * Use kvm_vcpu_gfn_to_memslot() instead of going through - * handle_changed_spte_dirty_log() to leverage vcpu->last_used_slot. - */ - if (is_writable_pte(new_spte)) { - struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, iter->gfn); - - if (slot && kvm_slot_dirty_track_enabled(slot)) { - /* Enforced by kvm_mmu_hugepage_adjust. */ - WARN_ON_ONCE(iter->level > PG_LEVEL_4K); - mark_page_dirty_in_slot(kvm, slot, iter->gfn); - } - } - - return true; + return tdp_mmu_set_spte_atomic_no_dirty_log(vcpu->kvm, iter, new_spte); } static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm, -- 2.27.0