Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp5123234iog; Wed, 22 Jun 2022 12:29:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ujGjL6tKpaC6L8Hef3O6m66VSR9GnFITXwkem3r/8D4UDP5DEiIIWPKQxzmymWDiOX8A1A X-Received: by 2002:aa7:88c9:0:b0:525:2aae:a94e with SMTP id k9-20020aa788c9000000b005252aaea94emr14456527pff.63.1655926196500; Wed, 22 Jun 2022 12:29:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655926196; cv=none; d=google.com; s=arc-20160816; b=rZ7TiTmRyuryZXlcWS3/trW+LP0ZlSvNH8T3TOdr3PpR8w6tAKC51mHb/T1iJrA5kk bx7m+MchPOZAE2DNpU4fAkAQtDgvFn8azFHHhGZtrLkd9F6Nx69WOEbolqnd3J181G5O 2QmEmlwjY9azyzGL7pGqUCAE8/zDKSsYigXknECcxYvtPujcebzcVnreGnDpNjFzQuvG rErVe8ZIRtvCwZ6OCaHGt37WkDOyWRKbOWq+cDGegCzb1s9/0OrLnnPU7qanp5qIAmrb a5Falug2L12Mj5cfxoMzJ1Eg2tlNrx+EhthzE55igqDjKXkXBtdqW7vaiQydVPpGAgKp NzrA== 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=F73ZQSpJXAcM6JUMGTWxTim9jEMxT3dE24kf7MSiY/U=; b=WxAjer5OrRWraUINEMlAg/UNe/a2p7Hb0zf4yn4WbEhk+DFq1ZndwHvvaXU0BjAcPW r6mo0xLOcdWIL1KUGJZmY9K/EnVfmPuceY5Uao6c57+G144vRM9K+RItsU9Ar9CxfM0h QPf/xOZUPkOTFEAZFTjMjQyNR2k8owow8yjgTjG7ZqmBQLQVs2++p5/pcwO8MSSNYyNs Ga02MrAKNZs6EtpDtGQnjnvrTLPqaul/3kOIMEMRorrJzzwQ+hdi4Nx7bcMIUGZK7QrY Z+RemuaEGwV+8hRvINhlwwAupeIE5GgVs8HaJivLI0gExiVt1XoozS6YACRR5JwIPS8n c5oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CiNaHmDm; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g10-20020a63f40a000000b00408c34d632dsi23483716pgi.157.2022.06.22.12.29.44; Wed, 22 Jun 2022 12:29:56 -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=@redhat.com header.s=mimecast20190719 header.b=CiNaHmDm; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357100AbiFVT2o (ORCPT + 99 others); Wed, 22 Jun 2022 15:28:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357400AbiFVT11 (ORCPT ); Wed, 22 Jun 2022 15:27:27 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A27E43DDF2 for ; Wed, 22 Jun 2022 12:27:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655926040; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F73ZQSpJXAcM6JUMGTWxTim9jEMxT3dE24kf7MSiY/U=; b=CiNaHmDmF17tKQI8Vh+wOZ80vly3nBWCi6NulS92g4kWYL1Vz8ucyhXzUTRM8SryaZ2rBU hIBjoU6rpPpuD1dSYuEQMQNCU64JHZakFtWCHdspD1Eql+HZtKzwBiEA9z4v6RcAuDHHQ+ Txk+z39MJBIfGraggWQsbswsAOrZ1UA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-88-MEOckTHDNae0HiQ4MeKbbw-1; Wed, 22 Jun 2022 15:27:15 -0400 X-MC-Unique: MEOckTHDNae0HiQ4MeKbbw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AC726294EDC2; Wed, 22 Jun 2022 19:27:14 +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 562ED1121314; Wed, 22 Jun 2022 19:27:14 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: maz@kernel.org, anup@brainfault.org, seanjc@google.com, bgardon@google.com, peterx@redhat.com, maciej.szmigiero@oracle.com, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, pfeiner@google.com, jiangshanlai@gmail.com, dmatlack@google.com Subject: [PATCH v7 09/23] KVM: x86/mmu: Move guest PT write-protection to account_shadowed() Date: Wed, 22 Jun 2022 15:26:56 -0400 Message-Id: <20220622192710.2547152-10-pbonzini@redhat.com> In-Reply-To: <20220622192710.2547152-1-pbonzini@redhat.com> References: <20220622192710.2547152-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: David Matlack Move the code that write-protects newly-shadowed guest page tables into account_shadowed(). This avoids a extra gfn-to-memslot lookup and is a more logical place for this code to live. But most importantly, this reduces kvm_mmu_alloc_shadow_page()'s reliance on having a struct kvm_vcpu pointer, which will be necessary when creating new shadow pages during VM ioctls for eager page splitting. Note, it is safe to drop the role.level == PG_LEVEL_4K check since account_shadowed() returns early if role.level > PG_LEVEL_4K. No functional change intended. Reviewed-by: Sean Christopherson Signed-off-by: David Matlack Message-Id: <20220516232138.1783324-10-dmatlack@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index bd45364bf465..2602c3642f23 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -766,6 +766,9 @@ static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) KVM_PAGE_TRACK_WRITE); kvm_mmu_gfn_disallow_lpage(slot, gfn); + + if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn, PG_LEVEL_4K)) + kvm_flush_remote_tlbs_with_address(kvm, gfn, 1); } void account_huge_nx_page(struct kvm *kvm, struct kvm_mmu_page *sp) @@ -2072,11 +2075,8 @@ static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm_vcpu *vcpu, sp->gfn = gfn; sp->role = role; hlist_add_head(&sp->hash_link, sp_list); - if (sp_has_gptes(sp)) { + if (sp_has_gptes(sp)) account_shadowed(vcpu->kvm, sp); - if (role.level == PG_LEVEL_4K && kvm_vcpu_write_protect_gfn(vcpu, gfn)) - kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, 1); - } return sp; } -- 2.31.1