[02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
Message ID | 20240227232100.478238-3-pbonzini@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-84189-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3022888dyb; Tue, 27 Feb 2024 15:23:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU8AcUeSCxqX+xkgZsyt/JIr1dJmRKH508q0RpvyShCuxlQUWUhPsS78xdrNgXPoa1MwlR9miIGIS0mpoiv0xydSFE5qw== X-Google-Smtp-Source: AGHT+IGp96XOxNK6kPyBQ0E9y2GDvj45oOKPllrE0XaMrsEin62isWK1BjjY38OeMiNtt3Vbu757 X-Received: by 2002:a17:906:af97:b0:a43:f825:ef57 with SMTP id mj23-20020a170906af9700b00a43f825ef57mr88846ejb.49.1709076195214; Tue, 27 Feb 2024 15:23:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709076195; cv=pass; d=google.com; s=arc-20160816; b=Gi7iy9dLfpGObpWtWi4P56kTYZAKYnFbKg+IvxsB2JdKYaulWypgBCIpmpre2BWFZL p68fdJqvpO3dOwo6DHixKr6hzLbm/S/jhr0n6lU4VFMUYm9cjm1w4JlHQ6e+hF5x1FaB xOKpYjVwPSY3xzxu7XJKmF3M3cWvypA9hjFJnk72Unad5LD/krzPcIT88CVEwbbe7E8a BZX5FjHIQfQZzZ0EMPNgbXPi6MX2iJSVVjtPhj3owuX5F2MnHQowlr8arzCBkDlo8Wxn Cp0k5aHLG4uYL/sAfArHMxnAguoocLqrGM0+sctDpoLThvMVis8dVgKaXvtMGf/LVgy1 Fosg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=93wanb7lMzSD6iLqwpiKLZNE2V6bD9o5BsnwgstbG6o=; fh=miuXyzfw5j8qsOURKCmYrSiXirUlKyDkR37pPJ+5+/s=; b=MyY722HwIhvX6BwzzFy+2kZpUPo5d/E0vvYBdJUq42MvSRq8wgemb/SuHNGImAumJB VZAWvtbS/nTTwT1slQOZtAlgu+lMNv2Qvz5eANyXLbt6YP1BZMui/Gky+HVCtc0RkFQH QpiYRkUHZ1GOhbOliD6GfMrYlxoLV8KYNn/6zwPz/U8VEgIqnl8bxCuPh4QTHYHzgEan BBhoIgRS4qzPwcvqwS2R6nXL3YQpAp9bzCmeXVBY4zadQN61IrkzudcVd5ysULc4/SkD SONd1N//yZrl9ItKG+swhlUGmKl6G2rCzm2T4H+dJfnsdQcTw8m4mEx79R1zI8sFZwi0 8TDQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=g5uw9nWm; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-84189-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84189-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id lo23-20020a170906fa1700b00a43e1aa11a4si349751ejb.138.2024.02.27.15.23.15 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 15:23:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84189-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=g5uw9nWm; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-84189-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84189-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 205371F237C4 for <ouuuleilei@gmail.com>; Tue, 27 Feb 2024 23:22:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7495658ACC; Tue, 27 Feb 2024 23:21:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="g5uw9nWm" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F1FE55E49 for <linux-kernel@vger.kernel.org>; Tue, 27 Feb 2024 23:21:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076066; cv=none; b=rOEwc1Se+a9+/AD18MC3r5mh0Z/YwnOWsw1/3GbuAo/3on/XSkas30/aHBB5IPoJcjpd/7J+9yWwdYklCnRnPRrIAsXYHx1rdFkpid112GvFi5/H1CiDje/9xwTwwsLUQj2QSTdTDjzyvqwyIxZPx1WPDz0+DekYe1sVUvq4ynQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076066; c=relaxed/simple; bh=dpznaUsDnWvVOp1QFLo/tAdeu3xC5fk3IJDC5TFijK8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=r11ib/4rxkg+5tyUqMcDvpzdAVyPhY/JY3mfCpEdeMcEb9M05RfO6NwvOXXYgzsdfjrCuFxh/y3TQAwoB98Wq7MZIRXKz1TxZrqWu1ckakLE3uQCs9yiWXQOvT/KFZPkrA0q8pg6I7P6W5LNHjyjOtCsIa8AlAaWFr8xDCKoCYM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=g5uw9nWm; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709076063; 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=93wanb7lMzSD6iLqwpiKLZNE2V6bD9o5BsnwgstbG6o=; b=g5uw9nWmi/pq+Rte7M+clYlCesfu5sIVI2vCJDsKx8E99bzUxCJotHwqpu0GvHBCj/Yq7D lrjipJoY16SYzRWfF8DD+S7BanNESQteS+43mifrq3K7NziHzAmm9PLvS2cnpBeCykSQUe BUZ+7W9hhX/mSeLU7lYOPYztzRTRhPw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-178-nTa1wnusMXShVbAS9Sg2TQ-1; Tue, 27 Feb 2024 18:21:01 -0500 X-MC-Unique: nTa1wnusMXShVbAS9Sg2TQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4F3AD3869147; Tue, 27 Feb 2024 23:21:01 +0000 (UTC) Received: from virtlab511.virt.lab.eng.bos.redhat.com (virtlab511.virt.lab.eng.bos.redhat.com [10.19.152.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2254A5EDCE; Tue, 27 Feb 2024 23:21:01 +0000 (UTC) From: Paolo Bonzini <pbonzini@redhat.com> To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, isaku.yamahata@intel.com, thomas.lendacky@amd.com Subject: [PATCH 02/21] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values Date: Tue, 27 Feb 2024 18:20:41 -0500 Message-Id: <20240227232100.478238-3-pbonzini@redhat.com> In-Reply-To: <20240227232100.478238-1-pbonzini@redhat.com> References: <20240227232100.478238-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792096280442963442 X-GMAIL-MSGID: 1792096280442963442 |
Series |
TDX/SNP part 1 of n, for 6.9
|
|
Commit Message
Paolo Bonzini
Feb. 27, 2024, 11:20 p.m. UTC
From: Sean Christopherson <seanjc@google.com> Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in). Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> --- include/linux/kvm_types.h | 1 + virt/kvm/kvm_main.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-)
Comments
On 2/28/2024 7:20 AM, Paolo Bonzini wrote: > From: Sean Christopherson <seanjc@google.com> > > Add support to MMU caches for initializing a page with a custom 64-bit > value, e.g. to pre-fill an entire page table with non-zero PTE values. > The functionality will be used by x86 to support Intel's TDX, which needs > to set bit 63 in all non-present PTEs in order to prevent !PRESENT page > faults from getting reflected into the guest (Intel's EPT Violation #VE > architecture made the less than brilliant decision of having the per-PTE > behavior be opt-out instead of opt-in). > > Signed-off-by: Sean Christopherson <seanjc@google.com> > Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> > Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> > --- > include/linux/kvm_types.h | 1 + > virt/kvm/kvm_main.c | 16 ++++++++++++++-- > 2 files changed, 15 insertions(+), 2 deletions(-) > > diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h > index d93f6522b2c3..827ecc0b7e10 100644 > --- a/include/linux/kvm_types.h > +++ b/include/linux/kvm_types.h > @@ -86,6 +86,7 @@ struct gfn_to_pfn_cache { > struct kvm_mmu_memory_cache { > gfp_t gfp_zero; > gfp_t gfp_custom; > + u64 init_value; > struct kmem_cache *kmem_cache; > int capacity; > int nobjs; > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 9c99c9373a3e..c9828feb7a1c 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm) > static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc, > gfp_t gfp_flags) > { > + void *page; > + > gfp_flags |= mc->gfp_zero; > > if (mc->kmem_cache) > return kmem_cache_alloc(mc->kmem_cache, gfp_flags); > - else > - return (void *)__get_free_page(gfp_flags); > + > + page = (void *)__get_free_page(gfp_flags); > + if (page && mc->init_value) > + memset64(page, mc->init_value, PAGE_SIZE / sizeof(mc->init_value)); > + return page; > } > > int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, int min) > @@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, > if (WARN_ON_ONCE(!capacity)) > return -EIO; > > + /* > + * Custom init values can be used only for page allocations, > + * and obviously conflict with __GFP_ZERO. > + */ > + if (WARN_ON_ONCE(mc->init_value && (mc->kmem_cache || mc->gfp_zero))) > + return -EIO; > + > mc->objects = kvmalloc_array(capacity, sizeof(void *), gfp); > if (!mc->objects) > return -ENOMEM;
diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index d93f6522b2c3..827ecc0b7e10 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -86,6 +86,7 @@ struct gfn_to_pfn_cache { struct kvm_mmu_memory_cache { gfp_t gfp_zero; gfp_t gfp_custom; + u64 init_value; struct kmem_cache *kmem_cache; int capacity; int nobjs; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9c99c9373a3e..c9828feb7a1c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm) static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc, gfp_t gfp_flags) { + void *page; + gfp_flags |= mc->gfp_zero; if (mc->kmem_cache) return kmem_cache_alloc(mc->kmem_cache, gfp_flags); - else - return (void *)__get_free_page(gfp_flags); + + page = (void *)__get_free_page(gfp_flags); + if (page && mc->init_value) + memset64(page, mc->init_value, PAGE_SIZE / sizeof(mc->init_value)); + return page; } int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, int min) @@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, if (WARN_ON_ONCE(!capacity)) return -EIO; + /* + * Custom init values can be used only for page allocations, + * and obviously conflict with __GFP_ZERO. + */ + if (WARN_ON_ONCE(mc->init_value && (mc->kmem_cache || mc->gfp_zero))) + return -EIO; + mc->objects = kvmalloc_array(capacity, sizeof(void *), gfp); if (!mc->objects) return -ENOMEM;