Received: by 2002:ab2:7104:0:b0:1f7:f6c3:9cb1 with SMTP id z4csp21592lql; Tue, 7 May 2024 08:45:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX8Fn4y4A99e0cso/28nySW+EDmDk3Durs9wwLvm2bnaDqUbicGyZxCT/pLN5EY8tbMKUbc32kyUo5s4uQqEuWuDJDS4c4kMZwBIwdEvA== X-Google-Smtp-Source: AGHT+IEfoSMTlNhXq783TRQtw/JPaXuQb8mwSZ/jU1sg69Rep5Rhl5Srgut8lDtHuLAeOmvajIGL X-Received: by 2002:a05:620a:c4e:b0:790:3f7b:42b1 with SMTP id af79cd13be357-792a6481e17mr498256485a.17.1715096746969; Tue, 07 May 2024 08:45:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715096746; cv=pass; d=google.com; s=arc-20160816; b=rUO8bvAFjpDckkvp22BwAt6L+4Wlvzyud/dtBv/OiUoYXW1XuruKKM5RsZr2J9ekyL RhFQPPaLMGRzeC3cUDYUkpp2qawT0Zi/hnZQMufKysrEIsxnwWpcKE/mLL10arPQP/6v e/Okh8/gxYD3lHEE0V+x9NYqRqqlqj/ZeNcpZn6oNbpvq8kc1VlXJFTYvvWYvLbES3qS tyS+jRVpqDX54RW7gjHU4D0284ds3f7MUdbsWR4jelGAURQL9tUSI2FeU14TzctWRX9l FD447At51tw9EpH8xL0V2tLK9C0SUfdhvMLLHtITzQljdqp4jjHudw0nmewTBftVsCIs xfrA== 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=+W1eHdUpPKTTAI4bHqjai+AwCzOOKEeOgSH970S6/XU=; fh=j1uWSsKok7OFro6C4trrims04JVv1Rml7bKO8Jubr9c=; b=jFl7FCwrwwsFYER6h8PHduobnCDg6YOUkVEB3xbmDO+tYR+fYCwMY4Wx7iBuNWi8gq 08magNJioR1eO5DabwR5YJFk2Ko4pBIhvBzdi9rpHLYJw/9PUtFgZoTMHv/5f4lT/ZiP 9xyvATY7R/M+cBVTVqhTH+atKsw8VbaTBf0KcyP1Wc8Rx3F8e4eyVdzR0DFVpn8hTjax PIMQxmCYwWc8lP2BAM6ZeZWmG9MkcJjlD41K8+tXYF/C2mq/Ep7K+b1SXe54R5xoKijA dqd9ozp6VJFp9N21MPTXyob+pKMY0QMmwYcZ8EZ/DDBoq2E/ztgHbb9tn951Ce6jhoUQ 48ug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="TJ1oC3/L"; 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-171712-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171712-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g2-20020a05620a108200b007929d88288csi3597366qkk.394.2024.05.07.08.45.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 08:45:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171712-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="TJ1oC3/L"; 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-171712-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171712-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 9D2AC1C21E2E for ; Tue, 7 May 2024 15:45:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 48BA716C6B5; Tue, 7 May 2024 15:45:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TJ1oC3/L" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 CEFB716ABC7 for ; Tue, 7 May 2024 15:45:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715096707; cv=none; b=uLnqcCi5Hjgq/PYsNCh7V+9k+GBMpaEFcztsxkr6wIDfOJ2V5in1DQuOKoVsn1AT9O679n8Bq5t8G2CYO/JQISS9p6GCmPZO0Y94zgi5uJo2jPoGJZ7V3+6aoKq9Az+Xbe8sa6Qm4B9Xsomp1FcJvTxPdqLxx5n3uzVUTGCA33U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715096707; c=relaxed/simple; bh=dt5lCI9BKhgSVWERuQVtqfoHcvj8FsffJiAncsN/ffs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XYcTpRLaQLfhj7IzcvSRAFyGoSxVv3l35wAqgKWDRmLUg49/ZGfTFAobn41N9+rhvTCrRNSoj5pCKQuBMbgLUOfrV9QYfw8YCtgrgKVgJSayRmdV1JAkPCoMWuAgGUXCuL0Z+PrCQhngdfLyy5PnHPmgF+9RZ8N5NuK3USPN/1E= 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=TJ1oC3/L; arc=none smtp.client-ip=170.10.129.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=1715096704; 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=+W1eHdUpPKTTAI4bHqjai+AwCzOOKEeOgSH970S6/XU=; b=TJ1oC3/LHAr9fbcC0WB95xMRO/WjzM8YA0NvpxNBmGdPm+339lWQZ3nifwvD7p/Cm51fy6 /hMcafS7clrgCQpsO/ktbGt/e1CS67Tp1ITKEYPlb9qNq6/7usF2NHdF1vD4MALms2b1pu P39W9Ztseanmtes/bb57Flh3Lj8iBQI= 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-207-N4yCkID0NjaGHl92wDsg6w-1; Tue, 07 May 2024 11:45:01 -0400 X-MC-Unique: N4yCkID0NjaGHl92wDsg6w-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 CC5A93C3D0D2; Tue, 7 May 2024 15:45:00 +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 8BF4BC0004E; Tue, 7 May 2024 15:45:00 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Isaku Yamahata , Xiaoyao Li , Binbin Wu Subject: [PATCH 1/7] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values Date: Tue, 7 May 2024 11:44:53 -0400 Message-ID: <20240507154459.3950778-2-pbonzini@redhat.com> In-Reply-To: <20240507154459.3950778-1-pbonzini@redhat.com> References: <20240507154459.3950778-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 From: Sean Christopherson 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 Signed-off-by: Isaku Yamahata Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com> Reviewed-by: Xiaoyao Li Reviewed-by: Binbin Wu Signed-off-by: Paolo Bonzini --- 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 658581d4ad68..38b498669ef9 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(u64)); + 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; -- 2.43.0