Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp6411764pxb; Tue, 15 Feb 2022 01:42:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJw610sgR+a0FmFa1n4ghH+O7+iW+WPsYsce9Fj4OjtWhWlNmGOF4N0ZDmsvA4YgNPqQy8jy X-Received: by 2002:a17:906:729d:: with SMTP id b29mr2192305ejl.471.1644918176563; Tue, 15 Feb 2022 01:42:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644918176; cv=none; d=google.com; s=arc-20160816; b=x7QyyXYLrwN/rnXQ2S2sZ/vVYNmB1/hj/g3AZMBXYPpfkwrUZQ9SF9ZJWF+yTcJUFD 6t++PBI+RUt5wLwfWbRydL2fqKuzz0opHkOTQ/LRQC1uNODpPqTRekEB/LPYQutuQUro Ybs8H7i6Aw1+D7S3t/ksKtDmZl+gLw36WyskMYi+hfuO4BmGLJIJ1EhOfhEYLyDKFTgp SCB3OoBVZAkvUfW4hKqmLzdbBjnnzYQr9VvT88DtnBA7Vhf7tzZKsGY2N053BKR1Bg6c qNCcFOvf7syHiZket839kYYB6j9d/WpaJMYWx8Hxubiy7JQ9FrbIreGLO4amXNsrrCDx mazQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=qBRCXHWRsfZFbcnR2e9NrL7AfQRDhw152q44tl2KQ8s=; b=cDRSfK5WKfuNr1jTaA6jYV5oCuiHAyeBF8qFOn6CPTKcu66T4PkkMJUEefjsREIQTb Sap7U9Wpxd2MBPQDakvXu/dsForqd092LDaJ9Abq5Nu/PiWNdWkJdmjKh46ArCdCDnB0 pcPsx0uStDVPggqp/cfAEGswOU/Ei2/sbd2mm4dkDGy05ckb6+PZ1S31FSdkGlmq5j2u LyXVt688VASId4jN9hPF6a1O2xTXyKF/IdWnbIgnmBn+Xyhh9svLpANvjIQAZIkSVhY/ 45KjuJf2wD5pKMVEn9oSl7ihfIZhokGh8QOWeGz+bwQU3fwnjJI3sihfI5JyHWRaNJMd 272w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=F9Atbi4z; 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 sc1si17723770ejc.589.2022.02.15.01.42.34; Tue, 15 Feb 2022 01:42:56 -0800 (PST) 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=F9Atbi4z; 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 S235300AbiBOIR4 (ORCPT + 99 others); Tue, 15 Feb 2022 03:17:56 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234021AbiBOIRz (ORCPT ); Tue, 15 Feb 2022 03:17:55 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 622D2811BD for ; Tue, 15 Feb 2022 00:17:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644913065; 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=qBRCXHWRsfZFbcnR2e9NrL7AfQRDhw152q44tl2KQ8s=; b=F9Atbi4zQuMqCcCckciXZVqfwBz3Tm31ZwQ+qUBlUBMeG2tNw2p2vr7XlzrWjEAQ5masSO HrDtLlaFgGGhUE5YZd0wKUMs9Sl6kCzYkdgVvTs7WxvPN3JiI+7BXOWyO2l9s77l0yI5Zv Z0ZbMOJrihDxXlx+9hiU4Euzk8gEK+A= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-211-LYb5oqFCMcmpaYxTsif0ng-1; Tue, 15 Feb 2022 03:17:44 -0500 X-MC-Unique: LYb5oqFCMcmpaYxTsif0ng-1 Received: by mail-ed1-f71.google.com with SMTP id j10-20020a05640211ca00b004090fd8a936so7286862edw.23 for ; Tue, 15 Feb 2022 00:17:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=qBRCXHWRsfZFbcnR2e9NrL7AfQRDhw152q44tl2KQ8s=; b=mB7OmB+Ip5FumnUjbwzwq/ibM1DvjQ6z2gQQOJj/VuBbXVOQXOsgm0VAP4XX2CGBUz tUUe0agJ1ygF+4iRwK5cug2RYxCtY8z1Fm8Yzo5yRDPlZ3uPvtbd6KmuTcJ9/sN2byRf FngE9rTrXlgyamzCcD/gwqyxYn16+YHErO8PemZ4XSHA/Hh8WJ30jwm+usV9lVXVjSKC WOKe5fXhZWwOYKTZs/P3cU26N4QeggtFOAGZ/Ns/IZmC8GCO6owHnTknWzuVfw6NAsjC lMpmIus9Kx4o/kSdd+qSqb1llL6ErtqmqWqjKsdTi+96e0Ao6ib3046X/WHdqnLA7nTg xRyw== X-Gm-Message-State: AOAM530G0CmaErlL84cWYpdbuxsVDKocctAdYNKTlVzsrk5wTZWiMAHM JSG4zUl1Ac+c6Ivxmebft/6oNNwCvQuSsNKaXhPiEC3I0wIAPUVUw+B/LYmIdQMXlZMb48cwPCy fCn+Stx3Lgpu/BJxF7hiHLxLd X-Received: by 2002:a17:907:7da4:: with SMTP id oz36mr2014407ejc.59.1644913063047; Tue, 15 Feb 2022 00:17:43 -0800 (PST) X-Received: by 2002:a17:907:7da4:: with SMTP id oz36mr2014392ejc.59.1644913062759; Tue, 15 Feb 2022 00:17:42 -0800 (PST) Received: from ?IPV6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.googlemail.com with ESMTPSA id j6sm16739438edl.98.2022.02.15.00.17.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Feb 2022 00:17:42 -0800 (PST) Message-ID: <29e1edc7-9f9f-0663-997f-3416269b6a89@redhat.com> Date: Tue, 15 Feb 2022 09:17:30 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH 12/12] KVM: x86: do not unload MMU roots on all role changes Content-Language: en-US To: Sean Christopherson Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, vkuznets@redhat.com, mlevitsk@redhat.com, dmatlack@google.com References: <20220209170020.1775368-1-pbonzini@redhat.com> <20220209170020.1775368-13-pbonzini@redhat.com> <5f42d1ef-f6b7-c339-32b9-f4cf48c21841@redhat.com> From: Paolo Bonzini In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 On 2/14/22 20:24, Sean Christopherson wrote: > On Mon, Feb 14, 2022, Paolo Bonzini wrote: >> On 2/11/22 19:48, Sean Christopherson wrote: >>> On Wed, Feb 09, 2022, Paolo Bonzini wrote: >>>> - kvm_mmu_unload(vcpu); >>>> kvm_init_mmu(vcpu); >>>> + kvm_mmu_new_pgd(vcpu, vcpu->arch.cr3); >>> >>> This is too risky IMO, there are far more flows than just MOV CR0/CR4 that are >>> affected, e.g. SMM transitions, KVM_SET_SREG, etc... > > I'm not concerned about the TLB flush aspects so much as the addition of > kvm_mmu_new_pgd() in new paths. Okay, yeah those are more complex and the existing ones are broken too. >>>> - if ((cr0 ^ old_cr0) & KVM_MMU_CR0_ROLE_BITS) >>>> + if ((cr0 ^ old_cr0) & KVM_MMU_CR0_ROLE_BITS) { >>>> + /* Flush the TLB if CR0 is changed 1 -> 0. */ >>>> + if ((old_cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PG)) >>>> + kvm_mmu_unload(vcpu); >>> >>> Calling kvm_mmu_unload() instead of requesting a flush isn't coherent with respect >>> to the comment, or with SMEP handling. And the SMEP handling isn't coherent with >>> respect to the changelog. Please elaborate :-) >> >> Yep, will do (the CR0.PG=0 case is similar to the CR0.PCIDE=0 case below). > > Oh, you're freeing all roots to ensure a future MOV CR3 with NO_FLUSH and PCIDE=1 > can't reuse a stale root. That's necessary if and only if the MMU is shadowing > the guest, non-nested TDP MMUs just need to flush the guest's TLB. The same is > true for the PCIDE case, i.e. we could optimize that too, though the main motivation > would be to clarify why all roots are unloaded. Yes. Clarifying all this should be done before the big change to kvm_mmu_reset_context(). >> Using kvm_mmu_unload() avoids loading a cached root just to throw it away >> immediately after, > > The shadow paging case will throw it away, but not the non-nested TDP MMU case? Yes, the TDP case is okay since the role is the same. kvm_init_mmu() is enough. >> but I can change this to a new KVM_REQ_MMU_UPDATE_ROOT flag that does >> >> kvm_mmu_new_pgd(vcpu, vcpu->arch.cr3); > > I don't think that's necessary, I was just confused by the discrepancy. It may not be necessary but it is clearer IMO. Let me post a new patch. Paolo