Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp190970rdb; Mon, 22 Jan 2024 17:06:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6OZVVl4OxADTgZO+0Z22ldSvb5xCwrckI9yX+rfddvNnKYMZKBjU4E9D2JfZN+rAjzMdD X-Received: by 2002:a05:6a20:9c8e:b0:19a:21b8:ab68 with SMTP id mj14-20020a056a209c8e00b0019a21b8ab68mr6312521pzb.64.1705971997058; Mon, 22 Jan 2024 17:06:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705971997; cv=pass; d=google.com; s=arc-20160816; b=UmAjMJ85OM61m/Ofu/ml1x9ROSynW0ElTBK+yw/NmQKkOW0I7IpO3ueXPPrvoDpPDt emOAyzqM9r8QO8pTY9G5q+K9Lvi70WACvkJUZyEbOIAsNocB12DiqRESpA0aTH+Bmzof yG82pPVXhACWMWNEe/K6dfZ5PTaTOtll1D8Xa18zcOiXznJjXGhgkwF7RDpTx2UKF/Sk 9aFShS423E0BBoxnykDu4YDnMvddmQlDyPPGhpM5peJmP9LEj0VWkCAEylhhudXdy2p3 hXo1md0IBBUsC+w1AWqBD7GHRgLvCjkjLuG/VLHrWbnCfRnvbNlXNRc7Yoix6Bd7jjRw fyPg== 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=bbkdqfiqOuOOgmw6FXzRE5fdr5vNrsegSiTQfALP8R0=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=c1mVRd9tv+5BZ0wfACj0EVMC0Tpcnm6ZHtRAolCVfevdsro59CmnAY3H/lHvbjJYJd 6RhIs6kaMjlEW0waEdurBegxx8A4CUbvx9xWQNMRqU3Ghn77A0DV314jjQfUzomlBbyX xH/5goWMTuaOr7XIlHolmocLoIxULeTK9pOAUTzPxKQnM2zrJle8WQEZxHDt9Dr4YnYR yCl/aTdx9VqyCFgMR7ErPEnvDQb4AX1NHVZqZouPUVqAMMb40GqkPQyu9wCcEedVs8OQ 4IicxllhD3qVDVZZi7Yd8E9CLg3gkPtsDkg12GKuhqRn6YcylRQ1Uw1VW4NU+8MY194+ y88g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ThgubnJG; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34344-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34344-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id v5-20020a632f05000000b005cd82a478f6si8741293pgv.759.2024.01.22.17.06.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 17:06:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34344-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ThgubnJG; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34344-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34344-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 3C520B20838 for ; Tue, 23 Jan 2024 00:39:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 80D015F843; Mon, 22 Jan 2024 23:55:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ThgubnJG" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (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 3367B5DF37; Mon, 22 Jan 2024 23:55:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967726; cv=none; b=ktdvyuAhTC6laO01I88MeRA5aFR51xlmybCy07DzfYsUfD5VtrRbw1suhDGqvnoTRlEfeQyq0dV2bh5wPR6rpgwRlhuKktorJhKbpnB4PM/Tzy/5UaMQ3SmUrp5AAcXmVedsoe4pnVzmJHBu+Eag2w2spgIiTtJ3q52CfS+qtKs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967726; c=relaxed/simple; bh=X12gjPfxFcHpNF/9VdlNSg3UYlpyaeeatnJUN8+oq0M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BEj4wQjMlOgXAq+3I8BT9cD0Pdiy3hOl6qqSWoBO9OCiUzWtLE7Sd7nGq+trT4c3lHlcBNNWrt/g//6XPyTduvU//ViRj8VdHj51Kf9Ul9+9He5BtCsOJHnsfDGekF+ZThU1YFXD4Par89iwu5C2t1fs8uYH355slvlyFSbzMEw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ThgubnJG; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705967724; x=1737503724; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X12gjPfxFcHpNF/9VdlNSg3UYlpyaeeatnJUN8+oq0M=; b=ThgubnJGXBXxDFyGlD6Cu9YCW1H1IdikQpiJ+f2DvgoWBymP4ELeCOhN e619DnARUOb6SY8a8wDUNBHyyfwOb6su+4xt38Q9VTVJ3PoX0Wp+n5LpQ QsktpLQ+di1RcMYGweIY4B8BAtlS53qyY/bLzioo7J6Tz7fWIlc/M0pPH LmVjuAX6z2eE2CaVFqMlw5hEMMMO+aW6lkSFXVnpl0Z/nLK5OuRRbLwmp 30qsf0mVh4VQ72RiwC7h3I1lQRCDSQrWhy3AxXqA1QhCT30Mda263oKh0 jRlkSv8M3QhQqDexaCFsRYODMKcFsnjVZRGCwaE5Nqxc0v0dQZfU8picK A==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="1243870" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="1243870" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="819888642" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="819888642" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:23 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v18 035/121] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values Date: Mon, 22 Jan 2024 15:53:11 -0800 Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 9d1f7835d8c1..60c8d5c9eab9 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -94,6 +94,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 48f2086a627f..0a0f3b131ea2 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(sizeof(void *), capacity, gfp); if (!mc->objects) return -ENOMEM; -- 2.25.1