From patchwork Fri Jun 14 09:58:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A . Shutemov" X-Patchwork-Id: 13698472 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (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 E5FDD195FE5; Fri, 14 Jun 2024 09:59:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718359166; cv=none; b=XF8ECXQYL4kXJYoJuCF/KbzC/4GN9JZK4S2l1zpHmsNkMP4NveDXWq61k8zeZ+LqTHFRaxFyg6hc6Ls1rsZRNkBGNqOmPfzPdv50bVrZufppcD9N25kA78vl23qR1o7LCi7UwLNsaPA1fBqkVv5Nfvx7/YIc2Hfwn9Jga/DRFzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718359166; c=relaxed/simple; bh=vMAUzycpMKArP020PRsibNB/a1B3MTtv6nj6DDJJbg4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vFIOjCYumU4k12JuoHfMmL9H8pl85rfIsEQLTaqsf6rLKVCj4hZqECG6JdxyIf3RUQ6/VRoyxDPC6RdImPDEYdQBiGGHjwK7oQ/vwKv1WcNJOsGq+iVVhIB6jnKfPe7ZBTOVx7xgMwTJPm2UDlnC7ZCR98LIjGnqGyyhbRtrKmU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=TKo1scMc; arc=none smtp.client-ip=192.198.163.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TKo1scMc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718359164; x=1749895164; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vMAUzycpMKArP020PRsibNB/a1B3MTtv6nj6DDJJbg4=; b=TKo1scMcC9dCQ5aLaTpcw1tFYO+D2KIQwJVD+o6VPohTylCgCNKiK2+U JcKycP1FXPH2/MMqgiDVrsjSNMRN97eF+29fcgsJO1qKp6YnrS5/v7sda zBbHcyfBd2+X//JtAI7vAIq1/YQkB/8mhWEWLLIk/W/ETtvMlOqEAPgLe xAEK73tJEzDFfYsPjnixeisQyaIRtXZZXJLf3n6mB2cnybNVDggBKjbgv Tv6feY9kL5T0O7Dx29inv1KnmqccbW4P7Y2VRzr775cAMB5v3Kna2jia/ uZMwRaOF1Czms6IkcUmXTu5tv1lrgLMgt6WwDaO6YlH94VxETqDK4c6Ln Q==; X-CSE-ConnectionGUID: 1PAp0eKgQfmk3F4bCCsiog== X-CSE-MsgGUID: Zb6TuuBlQlmIfbcVjZJCWg== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="12072383" X-IronPort-AV: E=Sophos;i="6.08,237,1712646000"; d="scan'208";a="12072383" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jun 2024 02:59:23 -0700 X-CSE-ConnectionGUID: yg2GzZ0nR0mCYXIueUV0tQ== X-CSE-MsgGUID: CghLlVdVThaOPXPOp4nOSw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,237,1712646000"; d="scan'208";a="44995849" Received: from black.fi.intel.com ([10.237.72.28]) by fmviesa004.fm.intel.com with ESMTP; 14 Jun 2024 02:59:17 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id EBD162A3B; Fri, 14 Jun 2024 12:59:08 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , "Kalra, Ashish" , Sean Christopherson , "Huang, Kai" , Ard Biesheuvel , Baoquan He , "H. Peter Anvin" , "Kirill A. Shutemov" , "K. Y. Srinivasan" , Haiyang Zhang , kexec@lists.infradead.org, linux-hyperv@vger.kernel.org, linux-acpi@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Tao Liu Subject: [PATCHv12 09/19] x86/tdx: Account shared memory Date: Fri, 14 Jun 2024 12:58:54 +0300 Message-ID: <20240614095904.1345461-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240614095904.1345461-1-kirill.shutemov@linux.intel.com> References: <20240614095904.1345461-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The kernel will convert all shared memory back to private during kexec. The direct mapping page tables will provide information on which memory is shared. It is extremely important to convert all shared memory. If a page is missed, it will cause the second kernel to crash when it accesses it. Keep track of the number of shared pages. This will allow for cross-checking against the shared information in the direct mapping and reporting if the shared bit is lost. Memory conversion is slow and does not happen often. Global atomic is not going to be a bottleneck. Signed-off-by: Kirill A. Shutemov Reviewed-by: Kai Huang Tested-by: Tao Liu --- arch/x86/coco/tdx/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 26fa47db5782..979891e97d83 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -38,6 +38,8 @@ #define TDREPORT_SUBTYPE_0 0 +static atomic_long_t nr_shared; + /* Called from __tdx_hypercall() for unrecoverable failure */ noinstr void __noreturn __tdx_hypercall_failed(void) { @@ -821,6 +823,11 @@ static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages, if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc)) return -EIO; + if (enc) + atomic_long_sub(numpages, &nr_shared); + else + atomic_long_add(numpages, &nr_shared); + return 0; }