From patchwork Mon Feb 26 08:26:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 206460 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1952519dyb; Mon, 26 Feb 2024 01:11:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU+XCVgWExm+nFIJVZuTkbXehXS8f1mEDHBKo8sUnbMNFaEnVLHbbBhPYcw+G6/Ugf42yn33F5tesATpoz0Wr/wmMj1wA== X-Google-Smtp-Source: AGHT+IFBhWLAa1uSbazjVQJ5OaDVw9UqHx5zRv9xfx3nB1RAAqRf+2xNTPdYBCqMWT9FZ1Ba5Ie9 X-Received: by 2002:a17:902:cccd:b0:1dc:affe:692c with SMTP id z13-20020a170902cccd00b001dcaffe692cmr363234ple.29.1708938666156; Mon, 26 Feb 2024 01:11:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708938666; cv=pass; d=google.com; s=arc-20160816; b=N9q00T4rQrdKrzPeN1ejNZXV2H4NueLgIVmpIjwQY3ey5MxQE5hTPLuyKe4KqNLUaY I+XXkDlBB/ahlGutGqIT2GlpelkDUXU7BWj+pa97gzPSHfDPoDEhpf9odcQunIRXIqTA USuWbWIyCBZ7OcKsG0wlLFkNaoUSTQuUZGKjEh6UirUjXp+feAJJuI7AMr0snIaAop8E HmnIYfkn4g7vpMwRTHCXIvTuNqDVLJEqoFOOhnhg9u+r/DO821bMUDjgayyquTtDHeHV UnhXWROhw0afZhzfFagGt8rrv0yLgDBilWijd4m0hRCEPgO9vSGQLi79otkHdVhaFlzB Ttow== 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=mWk0X0atclBFdwsepUTeYE5IOvJDDg/T5oMu5foELGo=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=hLIThkQPCVGrkVe0/sMLlN9MKD/QnJ8OI2IVQw3H9Fgd3KXvRSMbyAnqICggllHlmR OFS8F33ZVzeTZ8y3U4zTX9TKeD2Pi8LNWzAjt7wsfewHyCUJZX5jh7h5lEraGImcf+ow 0ePea6+aA6aFgAnobSdy71rAnWLQXsr234hlpSEJ/59xYuPvTvX4/ObiUMVvVieReRlC hZ4LfZfB6yaSd1tYRKKOx2z43ZwY8p3BhW9XmG5RplPdshQn4qqHjsGdWmkmaEngtVOH HRXiIviJOW1LvNqYwFfEQf12h0aSVzmxUUJKq5+vyLQqbPv9YWW/ae7JBgoJEsa1BxGv CV+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Mn9Rlq5/"; 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-80837-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80837-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id w16-20020a170902e89000b001dca886a144si811129plg.377.2024.02.26.01.11.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 01:11:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80837-ouuuleilei=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=@intel.com header.s=Intel header.b="Mn9Rlq5/"; 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-80837-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80837-ouuuleilei=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 26564B24DB5 for ; Mon, 26 Feb 2024 08:53:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 364816E5E8; Mon, 26 Feb 2024 08:28:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Mn9Rlq5/" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (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 C4E29692E8; Mon, 26 Feb 2024 08:28:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936115; cv=none; b=d9L+eKTP792pbFzCaiJvLGa7VDjEY7rBotYwv4VJ5HHwoMYPVRI4OYqXoVVYE9G0q0Urk50ypNSe+QSsfW3NTVPJ3EgIfA8O/kQV5cWaQkPnxd682v1ro3UK92iTMNKuiwKWucf9rOxxdZyVepnnyrROmQGMg2HeDYEWaNkJq6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936115; c=relaxed/simple; bh=WRkO1YUitiN/S3sFKUXbIqDdVI858msvJkdcbzWDebk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hR4goN0BYE5c81ecOatLcbffezkfgxYA2MOqWLk1NR07hVxVZSZH77MAmQhAfGPGuTiySbI9e5XHbl1ao6lcq7pgIbbahIlI+nPISvCWXqgTKS4nvm+nzt6OB90v0UfQpKCWFenM076UlvF3uFNIP8xc/3atQykrH/M23vqqIlw= 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=Mn9Rlq5/; arc=none smtp.client-ip=198.175.65.19 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=1708936114; x=1740472114; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WRkO1YUitiN/S3sFKUXbIqDdVI858msvJkdcbzWDebk=; b=Mn9Rlq5/8AJ2Nv56ZkMlKpMEvr3k1OefYg5jM0bXaxR7oTyFYHe/G7Si vzbngtkR3zODUo6tTHLDtCsn2EIvR7IqKJbuLiidSgZ+8TLstoa3SlYNa zJGNJTtaQuBZj+IlOwF0JhpQmJopF8aaxt9W1HZxEoBRF4MvSbanefEuM 2IM4XYlTvrUBzC8qgsKygjhlflLVr1Tifl7yHxNvhLixE4YREKLw0KbeT gsAzHpjYxq5hsGjtgs1bqQyaT0vtxH2Chz1nIxIlisdoi+pqU7dCDiJyT zzXfkRy0Oc9VRM3AGOhnGCa45RLmHjCKGIzPWq+9+Kur+gO5f/qC10lN5 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069485" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069485" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:33 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272476" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:33 -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 075/130] KVM: TDX: Extend memory measurement with initial guest memory Date: Mon, 26 Feb 2024 00:26:17 -0800 Message-Id: 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791952070717868904 X-GMAIL-MSGID: 1791952070717868904 From: Isaku Yamahata TDX allows to extned memory measurement with the initial memory. Define new subcommand, KVM_TDX_EXTEND_MEMORY, of VM-scoped KVM_MEMORY_ENCRYPT_OP. it extends memory measurement of the TDX guest. The memory region must be populated with KVM_MEMORY_MAPPING command. Signed-off-by: Isaku Yamahata --- v19: - newly added - Split KVM_TDX_INIT_MEM_REGION into only extension function --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 4000a2e087a8..34167404020c 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -572,6 +572,7 @@ enum kvm_tdx_cmd_id { KVM_TDX_CAPABILITIES = 0, KVM_TDX_INIT_VM, KVM_TDX_INIT_VCPU, + KVM_TDX_EXTEND_MEMORY, KVM_TDX_CMD_NR_MAX, }; diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 8cf6e5dab3e9..3cfba63a7762 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1339,6 +1339,67 @@ void tdx_flush_tlb_current(struct kvm_vcpu *vcpu) tdx_track(vcpu->kvm); } +static int tdx_extend_memory(struct kvm *kvm, struct kvm_tdx_cmd *cmd) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); + struct kvm_memory_mapping mapping; + struct tdx_module_args out; + bool extended = false; + int idx, ret = 0; + gpa_t gpa; + u64 err; + int i; + + /* Once TD is finalized, the initial guest memory is fixed. */ + if (is_td_finalized(kvm_tdx)) + return -EINVAL; + + if (cmd->flags) + return -EINVAL; + + if (copy_from_user(&mapping, (void __user *)cmd->data, sizeof(mapping))) + return -EFAULT; + + /* Sanity check */ + if (mapping.source || !mapping.nr_pages || + mapping.nr_pages & GENMASK_ULL(63, 63 - PAGE_SHIFT) || + mapping.base_gfn + (mapping.nr_pages << PAGE_SHIFT) <= mapping.base_gfn || + !kvm_is_private_gpa(kvm, mapping.base_gfn) || + !kvm_is_private_gpa(kvm, mapping.base_gfn + (mapping.nr_pages << PAGE_SHIFT))) + return -EINVAL; + + idx = srcu_read_lock(&kvm->srcu); + while (mapping.nr_pages) { + if (signal_pending(current)) { + ret = -ERESTARTSYS; + break; + } + + if (need_resched()) + cond_resched(); + + gpa = gfn_to_gpa(mapping.base_gfn); + for (i = 0; i < PAGE_SIZE; i += TDX_EXTENDMR_CHUNKSIZE) { + err = tdh_mr_extend(kvm_tdx->tdr_pa, gpa + i, &out); + if (err) { + ret = -EIO; + break; + } + } + mapping.base_gfn++; + mapping.nr_pages--; + extended = true; + } + srcu_read_unlock(&kvm->srcu, idx); + + if (extended && mapping.nr_pages > 0) + ret = -EAGAIN; + if (copy_to_user((void __user *)cmd->data, &mapping, sizeof(mapping))) + ret = -EFAULT; + + return ret; +} + int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_tdx_cmd tdx_cmd; @@ -1358,6 +1419,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) case KVM_TDX_INIT_VM: r = tdx_td_init(kvm, &tdx_cmd); break; + case KVM_TDX_EXTEND_MEMORY: + r = tdx_extend_memory(kvm, &tdx_cmd); + break; default: r = -EINVAL; goto out;