Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1176404rbb; Mon, 26 Feb 2024 00:43:30 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUNkX0cGIyIqFujWTVhPYMD8sCO0nyNoHKf9mEofTPidACW+flpPFxbnfQd6DeZxlwPPEy9Vmo8q1xm4HRl6tpmvp9/h79E9cE/7dyPwQ== X-Google-Smtp-Source: AGHT+IGwowS0Ka1RUHaKelaSC08RwNrxPaTHKU03zjIFrhSlAEDWQ2uVfmyM5W1cAAsrNo8yCDrn X-Received: by 2002:a05:620a:1a29:b0:787:8115:23f4 with SMTP id bk41-20020a05620a1a2900b00787811523f4mr9401552qkb.61.1708937010276; Mon, 26 Feb 2024 00:43:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937010; cv=pass; d=google.com; s=arc-20160816; b=obNihfTWfQIAflRlfuIurkVbzaZ52LEWnFIcUi7OfQeuciQTuzQ8psaRoNNfuA4/ec jiayxzU8sV4tOEhnez/nD4LkR+Jpd3yQiMr+bHwnSroanC+ZeDiot08jS8DHtHZAJK+J wIo39J6SfLYTKMIgHZgsj3MhUBxrKqiiEKVfj38bNlSeTUj+//6fbSxRItrHggsBFPTW Xa3gpvYnbrAJQjPsK5mf6EehKREcYP5nTZwd2fa3/xRlh57du/vjEqeozXwxi/ovGW38 sFjha4M9AnYXToL5vD12uT4rwytdLAsu/v82/NThEsAck4GvZMJQTzSc+ry8o2sePKrd vjoA== 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=1LhR850R+1AFmNIYJ/9PTwXfWbbIzj9dx+07ROQ15S0=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=QsGWwHIVkHFEoG6D3z5tzaPUULrWrIPJtLLAeJM1fIgor0mY7bZp2gjuwgcKpf0uQ4 CiThVE8ZXS72wB83YXj1zA7pIYfge5WHXdV8J3+ZI6OLQTe62B4YLAJY6t57IsHj9F+Y 6/ILHDupU/nbfXxFoU5w+FFzI8/kE2m8nzmGLIiWNu/TZZBGMJF63c6bFmKnKbguALY3 yCfPoeUtllHwfLNYJnBoNLHi4r/2UwjlBDroYy0tPFCRBYAlkUE2dnm+FBP51YA66iGq bUiTu0kU/QmpmMiBIgFZQBtzwCXu0xJ/zslTSIeV1H2q8KX0TxYOFZk/idTKDcq249bW TpqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HoRbKJ3G; 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-80810-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80810-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v24-20020a05620a091800b00785d5fb869asi4581812qkv.208.2024.02.26.00.43.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:43:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80810-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HoRbKJ3G; 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-80810-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80810-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id EFF3A1C215BC for ; Mon, 26 Feb 2024 08:43:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0B2555FBB4; Mon, 26 Feb 2024 08:28:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HoRbKJ3G" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (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 3B3A5208AD; Mon, 26 Feb 2024 08:28:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936096; cv=none; b=cOkcmMdvfKn7+diOmqEWPvhTZBvxAEAxVoO17o1KDn6IuktEo9mWkKFstQzB8DLadJJ5/K12p78tcOX/9GRwNVISXm2ffgd54wf9DidfVW4g5DI9IXWDqzAhdjcSssMVYl8Z7G1RCW+DgK4ONM7XXaxIQ1GUBKqoLELYDjjSH58= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936096; c=relaxed/simple; bh=X5tL+FTdFYIo/iu/ZRdr5tfelqekqbOSzf0LKCUZinM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R2/YdGBUkH50UA6m1BYvaF0JBHsLGEtQFNus/vYTrFBeoLDFe4GW51bEJEIVDpktxXgqAleIHM9z0KQ1FNWT42rBCc87VgBXYSsPrIbZx4RS2nwouevshCLAcwstLdziHCvBx6Ixr2g2FE3aMjOuJixoUYOkySB46AxLwEMvEe8= 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=HoRbKJ3G; arc=none smtp.client-ip=192.198.163.13 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=1708936093; x=1740472093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X5tL+FTdFYIo/iu/ZRdr5tfelqekqbOSzf0LKCUZinM=; b=HoRbKJ3GiHQhNmG6aAF5deRzleDhO17EooWcUO7f5nZTrsa3E5IyWJBc a8xXDLRNI+Wxs/uMNrBBWZv+qDmJ9bbxPhh9fwRKzXybOvuHOucYESVhr RpJ4CRXqkjuh2fvqIod4s/pSCvkizcGJhMAUCts0C0rkdyrkhsQXYrNV6 sJA5wyvVLyNbp1fzJsnONkK/gdvyapbA3YGcaNWD2vdiU8V+2a1V65dnp LLj1tjOeSAVUctwCygyDLH/CfXvlz8ltuTZGj3nj8HHcNmpPERxPQebfa y+m7bxFbZhC/SEVmMAhTVo2rziMp9ucGdOD6tFT7nyt5jIk/TZkNWuyan g==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="6155368" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6155368" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="6615787" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:09 -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 v19 048/130] KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values Date: Mon, 26 Feb 2024 00:25:50 -0800 Message-Id: <9c392612eac4f3c489ad12dd4a4d505cf10d36dc.1708933498.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 de38f308738e..d399009ef1d7 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