Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1216827lql; Tue, 12 Mar 2024 10:21:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU+2LZ7ia9y30pWwu0M6FdLyU5KE6pfCoYPo9mNhqYgumiG11diA+chZm/jbJCoHCjQZdNTryu81jqVi9ETq6nIDoj/bJ1++OH0XGP4HA== X-Google-Smtp-Source: AGHT+IG/Kstu4yPE14nJQ/AdwoeaYEYxa/Bp1CU0S6Ni4IV2Klmi1/HgAc0NFma6AbRisIvYYTd7 X-Received: by 2002:a17:90b:3010:b0:29b:bcea:c17e with SMTP id hg16-20020a17090b301000b0029bbceac17emr2399053pjb.1.1710264092128; Tue, 12 Mar 2024 10:21:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710264092; cv=pass; d=google.com; s=arc-20160816; b=JCfFNFu7nUv5U/jgvxJt5sMdynesGgP7JNQ1t8SNJpG52lHRJvubh6XluypEF0jK+g KQJSH3z2aGFDqgpm6UOZiJBVuLbcedd/3GJgfk5UpORJ1AO4ovlBsHLJnR2WuWbpfbfy AU3GD8ETbOE8Wn+zTxLWK6U7h1VtEwo/NQZSv6jqxDQoeVHVB83KMTlbfRLafQGLrYkK mUB+VsP5bPikr+Fu/5mdrOf7+XN0gxxWYM7SPm9N5WwiznUMglNEL2LgEatANFOthvG0 MmITremaYf0y/pNqIv0olDmm7XmDgPYVsK2N36uvX8mjr142LVaFhBmPp1dE9DjbTY10 a5Pw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=WE8e1Ziotl3mZ18Tc3buOFiIUh2+sqQFXIcFrXQVlYE=; fh=ilVJmBFpPKn5KOwzWBt7x9JrTHAO2XZ+fbjeNkPYHQM=; b=uiOVcvV61WSpBmg1WqoLAhAWqFfZ6abPhc9u9bSrvT3ueegIL9Mx9M4aYEhEAwbhCh Q2cEmewUt2cmbEVRKwKg72981vNmm8s74Be9Wpm+teUceI5FyVq41QNvdrpAAl9m/5Lr Zl9u8j2uDUJfBHv/q9WxJPNwfg45zZxV2o3SIFFA7KZpudWE7tTOsu0Z9BL2uDLhbecA HC5ovndMUUbazSFy0dhiRIXCBW4QO4IAF3JaWIYDpLU7ilUnqk+yOiz9B/Wy/ZOfwN/1 uCBPII4/XWJIM6NopiOYVqXuHdGiqsW/bNZu5Lx39nINn6gE/7305vRAsmxwBK2WrAdj QeEg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Mdd623ZJ; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-100579-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100579-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id z8-20020a17090a1fc800b0029a84892664si7376468pjz.27.2024.03.12.10.21.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 10:21:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100579-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Mdd623ZJ; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-100579-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100579-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 0EF49B226DE for ; Tue, 12 Mar 2024 17:10:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF22013C9E0; Tue, 12 Mar 2024 17:08:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Mdd623ZJ" Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C3861386BC for ; Tue, 12 Mar 2024 17:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710263286; cv=none; b=UHdQ7TO9f2+NqZC2kmhz34vIiIWBrAk9DNa39j0dLhAP1nAH3LN/l4ih6G9GhNA+GgB6t4VkfUaJdnJdtdQOMWxCznJoK1gRdcQhxzjQZeankEoxEvo61xNd0kexzh1QOUQKvnoQ8F0E1Oy0AcsrfzXgEjD4HZsdbTPI4X2hl3o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710263286; c=relaxed/simple; bh=LCt3WWvGn4P8WKRrmZ370zHM2Olntl8aKMiYWPrEOpc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OqOGpkyafyihWIXcaOp6wj0Q0K8mkaT01sddqBwk/dhonxXnrCRxvJ1rFPbw90sto76ftikn5pNthwe+qovBiA7dIartPd5yNKOsUZiSlDMn0jjrqUG9u9cfFuxGFGOXrL8WOcVe6QFH6PIG+/jRCEKVvdduRKdAmZPOWcLrr3Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Mdd623ZJ; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5c670f70a37so5220266a12.2 for ; Tue, 12 Mar 2024 10:08:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710263284; x=1710868084; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WE8e1Ziotl3mZ18Tc3buOFiIUh2+sqQFXIcFrXQVlYE=; b=Mdd623ZJ8ozdBoWFRe/vQqSnLlmzGLbDodzEDOBX19UTcrKd+OgXfShY7GlxGT4RMv +sFVjEy6MV4eiYg9SzXgmTdgoBYEY30sO6NXqm+DlNr+vjC1C5koOVqSPQD2DfmHLZTP IQ03Af9ObWtSr6X3SiHkvLlYOhJVPVUESqOMzTuxsj8+RHXN5hOPeuFInzIv2S1rdwMO KH7JIv3ccdGVKTwnCsvJRjcyq2YnQSNdbae46eDDWMumtOjJwOxavTsfoSGRMqZKJwHt lwcihTYhkb3CPfl0MXCt5Aapw6+wIC8bj730pa5gaJYvC76M9EDncMO3u/gsxfkcXuWF V5pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710263284; x=1710868084; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WE8e1Ziotl3mZ18Tc3buOFiIUh2+sqQFXIcFrXQVlYE=; b=M/l/Gjle+6V8AmIBg+ZkhnOCDuca77YXCFlGgQC4PC2NBK5DPJD2g2X1mz5KG90zkt DMJaxfUAVwuNC8QfuX2HPOtwVTm1KbZzIZUNBEFmtHR3E9Kz9n6bwV4gH/nZHkdEJbeQ /pDuv1cVtkU+gAeBP9JjMfNP0M6o7wNnsWupGrOTxsrgbud272R5oHmZA0VVduuAxhHm LbJteyLqgmjHUzgPmpwGxpelF+OmTIa+NPA8MOD141bV07QL6O0y2cSaOGVhCqvxEDe5 FuEYMXdw+8HGOev9Cio0cpc4moPsgdhfSatvXwu5Ek38ClfMd8RJ5B1ugf21fFLWI1kx gZUw== X-Forwarded-Encrypted: i=1; AJvYcCWn+eQJwyzrHujMkID+CKCgh5UemxKlJdGpwVZICyPc7ojMBuzRoGfB2T+U+aB5ZY6OnnqVs0jFTkbrVk8N/0gX02IL0dcmNAsLq1Al X-Gm-Message-State: AOJu0YwS8hdfojflks4/+cFlG5X0QIWh/34P78gn/s2mXw/CZVbE9u9+ umuaNPWDeL/Yo6ehTA0Ve1mLRIXEP+HS1HEY+vTZxv3rJLnBfAkAS+ViDWaekLAUZ7q1sHBaI+s gxA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:e02:0:b0:5dc:6130:a914 with SMTP id d2-20020a630e02000000b005dc6130a914mr26681pgl.7.1710263284174; Tue, 12 Mar 2024 10:08:04 -0700 (PDT) Date: Tue, 12 Mar 2024 10:08:02 -0700 In-Reply-To: <5ee34382-b45b-2069-ea33-ef58acacaa79@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309010929.1403984-1-seanjc@google.com> <20240309010929.1403984-2-seanjc@google.com> <5ee34382-b45b-2069-ea33-ef58acacaa79@oracle.com> Message-ID: Subject: Re: [PATCH 1/5] KVM: x86: Remove VMX support for virtualizing guest MTRR memtypes From: Sean Christopherson To: Dongli Zhang Cc: Paolo Bonzini , Lai Jiangshan , "Paul E. McKenney" , Josh Triplett , kvm@vger.kernel.org, rcu@vger.kernel.org, linux-kernel@vger.kernel.org, Kevin Tian , Yan Zhao , Yiwei Zhang Content-Type: text/plain; charset="us-ascii" On Mon, Mar 11, 2024, Dongli Zhang wrote: > > > On 3/8/24 17:09, Sean Christopherson wrote: > > Remove KVM's support for virtualizing guest MTRR memtypes, as full MTRR > > adds no value, negatively impacts guest performance, and is a maintenance > > burden due to it's complexity and oddities. > > > > KVM's approach to virtualizating MTRRs make no sense, at all. KVM *only* > > honors guest MTRR memtypes if EPT is enabled *and* the guest has a device > > that may perform non-coherent DMA access. From a hardware virtualization > > perspective of guest MTRRs, there is _nothing_ special about EPT. Legacy > > shadowing paging doesn't magically account for guest MTRRs, nor does NPT. > > [snip] > > > > > -bool __kvm_mmu_honors_guest_mtrrs(bool vm_has_noncoherent_dma) > > +bool kvm_mmu_may_ignore_guest_pat(void) > > { > > /* > > - * If host MTRRs are ignored (shadow_memtype_mask is non-zero), and the > > - * VM has non-coherent DMA (DMA doesn't snoop CPU caches), KVM's ABI is > > - * to honor the memtype from the guest's MTRRs so that guest accesses > > - * to memory that is DMA'd aren't cached against the guest's wishes. > > - * > > - * Note, KVM may still ultimately ignore guest MTRRs for certain PFNs, > > - * e.g. KVM will force UC memtype for host MMIO. > > + * When EPT is enabled (shadow_memtype_mask is non-zero), and the VM > > + * has non-coherent DMA (DMA doesn't snoop CPU caches), KVM's ABI is to > > + * honor the memtype from the guest's PAT so that guest accesses to > > + * memory that is DMA'd aren't cached against the guest's wishes. As a > > + * result, KVM _may_ ignore guest PAT, whereas without non-coherent DMA, > > + * KVM _always_ ignores guest PAT (when EPT is enabled). > > */ > > - return vm_has_noncoherent_dma && shadow_memtype_mask; > > + return shadow_memtype_mask; > > } > > > > Any special reason to use the naming 'may_ignore_guest_pat', but not > 'may_honor_guest_pat'? Because which (after this series) is would either be misleading or outright wrong. If KVM returns true from the helper based solely on shadow_memtype_mask, then it's misleading because KVM will *always* honors guest PAT for such CPUs. I.e. that name would yield this misleading statement. If the CPU supports self-snoop, KVM may honor guest PAT. If KVM returns true iff self-snoop is NOT available (as proposed in this series), then it's outright wrong as KVM would return false, i.e. would make this incorrect statement: If the CPU supports self-snoop, KVM never honors guest PAT. As saying that KVM may not or cannot do something is saying that KVM will never do that thing. And because the EPT flag is "ignore guest PAT", not "honor guest PAT", but that's as much coincidence as it is anything else. > Since it is also controlled by other cases, e.g., kvm_arch_has_noncoherent_dma() > at vmx_get_mt_mask(), it can be 'may_honor_guest_pat' too? > > Therefore, why not directly use 'shadow_memtype_mask' (without the API), or some > naming like "ept_enabled_for_hardware". Again, after this series, KVM will *always* honor guest PAT for CPUs with self-snoop, i.e. KVM will *never* ignore guest PAT. But for CPUs without self-snoop (or with errata), KVM conditionally honors/ignores guest PAT. > Even with the code from PATCH 5/5, we still have high chance that VM has > non-coherent DMA? I don't follow. On CPUs with self-snoop, whether or not the VM has non-coherent DMA (from VFIO!) is irrelevant. If the CPU has self-snoop, then KVM can safely honor guest PAT at all times. > bool kvm_mmu_may_ignore_guest_pat(void) > { > /* > - * When EPT is enabled (shadow_memtype_mask is non-zero), and the VM > + * When EPT is enabled (shadow_memtype_mask is non-zero), the CPU does > + * not support self-snoop (or is affected by an erratum), and the VM > * has non-coherent DMA (DMA doesn't snoop CPU caches), KVM's ABI is to > * honor the memtype from the guest's PAT so that guest accesses to > * memory that is DMA'd aren't cached against the guest's wishes. As a > * result, KVM _may_ ignore guest PAT, whereas without non-coherent DMA, > - * KVM _always_ ignores guest PAT (when EPT is enabled). > + * KVM _always_ ignores or honors guest PAT, i.e. doesn't toggle SPTE > + * bits in response to non-coherent device (un)registration. > */ > - return shadow_memtype_mask; > + return !static_cpu_has(X86_FEATURE_SELFSNOOP) && shadow_memtype_mask; > } > > > Thank you very much! > > Dongli Zhang