Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1912308lqe; Tue, 9 Apr 2024 04:32:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXPoGHvBOBDzgEL74mqmCxoOJflukOV8qjYAhdB5T2GsSkmqMHZQADdH0371GpwFHP8k0Xncb0J/Tc0+Ig9dk9E/WW2QjuCQHa8OTvHFA== X-Google-Smtp-Source: AGHT+IHU5EIMRE7pZ6bHAiD9PEmbZDq51gylEeIBx1ogiSrMilRIXSia5/hYAnxyPNp07h7iwPH0 X-Received: by 2002:a17:903:41d0:b0:1e2:194a:3d22 with SMTP id u16-20020a17090341d000b001e2194a3d22mr12843411ple.32.1712662330366; Tue, 09 Apr 2024 04:32:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712662330; cv=pass; d=google.com; s=arc-20160816; b=Vi4FvQ1d8YNQqJwEmzjAmHecDrpGuw095E6e8dW/jp6yWexBXm2RPbXqNlMosxcTf9 0zBpOZxp5Ym8kPh3byOngNpIXyrC2dzVSEf0p4RMfxzb1QzvV5x5pDUgZp8Epg32Unzr HX4xRQ07c61klZoDoObop52nleav8mgtRCwzfDhnu3kzuAA4DqZ4x1G2XCgrtaLwcbv8 nmL6oX6HoVpoN0zGASmXokgn5sVAKXhuAj8/7aV0MTc6v+QpXwpMBtFfAbCOo/AmmHD1 KA5yeq0ShlZa4kCTavm0ru/sHpTArohEV+rQjMiP0WQFBu6xSxgs8tiMtjc4LbHCOeA4 sHAg== 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=f2w7jBf/0FOU+6cXOCEvPmY/mQ3d7coyq6RNzb1Txow=; fh=Yw2S7s1ZcF7+jfnU9Zy13ghDfPt/cQWqfRpyK6FQAJQ=; b=s2flMMeFtOIkk7VMpqScHJehV+A/lFxd7kQchV0UjHCwyImlx6mZhJYdpKr+QuJMHv r1yXWYxESutqDTZ2nAik00y8pE8kYWNIEZqlthga93M9n06NkMuP1pUKCfvl/JuKeYPF MddOxn3AVkuudtSteVLur7cFwvruhMSA7+fZvt4db40CQdJeKTsy9IiQiegbMdZZVI3/ V076pX3e3F8WTt0EdkdYx/JdCM+VA8vuhCpTUOifk9brdo3YAsBekLW6oMiX3sKz2WLj 8OTWhkm40ps8rUC03o9qEmxA2JpQI8D1f4EXL9GPwyoTnQZDo57Hppm8zyAFTYkuRbjW eXjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZWR0zNhC; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-136755-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136755-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id z7-20020a170902ccc700b001e2c8a0e01asi1922431ple.29.2024.04.09.04.32.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 04:32:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-136755-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZWR0zNhC; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-136755-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136755-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D8BE4286F44 for ; Tue, 9 Apr 2024 11:31:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E20EF12D212; Tue, 9 Apr 2024 11:30:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ZWR0zNhC" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (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 2FDA312BF39 for ; Tue, 9 Apr 2024 11:30:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712662231; cv=none; b=F732JzlFBmVz2ZKDC0dw9QQQ0FGbQ+pWdImiQV0LuMWmOyqrkR58cbCaDWMg9mLfr40MXQjXssipT8Pu6VPJHGtTE5QUPsMESMSPH48dfEui54inho61aWOcvpLWZ/b/eQUgXcPgmFTc1RJvSRoJ6lSqB3mDb0nzOQhXn+IJ20k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712662231; c=relaxed/simple; bh=ggOF5UziCfkAXFWyu9AjZBbe749R0vOnMDQfmgr6Dec=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u5JurtpeBqVRJqOFeyvteP22O2LuXOjV/BH7uGbbI8csueHWLvRvWvwNeKfjEfqv6m6w0OlhUo/Zg3tIROKPAfp12AW7U6CicICDSUqs4chLZEJ1DubdpBsut9kYGJSOlZkERD8eAi7CoOOMTS3pH45MBtXYVNUYR8EZ2L3mbA8= 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=ZWR0zNhC; arc=none smtp.client-ip=198.175.65.9 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712662231; x=1744198231; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ggOF5UziCfkAXFWyu9AjZBbe749R0vOnMDQfmgr6Dec=; b=ZWR0zNhC5BzSBqZjqUIKXaeoWCBmO8sy6lUEHUFXDXl89FzRCT0mOVxj z3ac0Q0kvQ5nHEp98cj1umg/ItNq7KZl9IZLqLutA8V6HYLZMwvIv3imv eZ3PA3jiD4tkYrRki1aF788ljfEljdL5JuN56IjxjiDVCdrLE+UFK0VAX YD+m/D22l1eBjBV29loFvB86JMaSPZ78MzSNrmQjkrV+QC6GG4xwPfY0Y bRp97TStf6rSFXA9fvuLR4Ild294MEyYXP2YI+6n2NmRKumXT97hYUZvm hkxqmndK3n2v2TSGTICrap47Qzo54BqPUMxf23DQTVNytBn6cqrjjGpDU Q==; X-CSE-ConnectionGUID: /bkCHEP7T0aYLepr1hzMig== X-CSE-MsgGUID: c6rlSuOoQ+ykKly7jOOj/Q== X-IronPort-AV: E=McAfee;i="6600,9927,11038"; a="30460393" X-IronPort-AV: E=Sophos;i="6.07,189,1708416000"; d="scan'208";a="30460393" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 04:30:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,11038"; a="937093333" X-IronPort-AV: E=Sophos;i="6.07,189,1708416000"; d="scan'208";a="937093333" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 09 Apr 2024 04:30:24 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id CD2B92E9; Tue, 9 Apr 2024 14:30:17 +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" , Baoquan He , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Tao Liu Subject: [PATCHv10 08/18] x86/tdx: Account shared memory Date: Tue, 9 Apr 2024 14:30:00 +0300 Message-ID: <20240409113010.465412-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240409113010.465412-1-kirill.shutemov@linux.intel.com> References: <20240409113010.465412-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. 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; } -- 2.43.0