Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2183478rbb; Tue, 27 Feb 2024 13:26:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWc3m8soy6aEZTMuJeopS5N7b8fepA1ji7o0jM8vPzwc3mvcVa9kY/cplx0fjSasgo0ClmkmhyF/JUtVTX7h7zmcI5anmu8lkDmmJr8oA== X-Google-Smtp-Source: AGHT+IFL9q/Mja+y4zujUc1T0Umvk9TivBNF80qVMbk6KoYu7y3YBvsfY3arW/Lkk/i67YE3yyMu X-Received: by 2002:a05:6a20:d80e:b0:19c:7560:e7d6 with SMTP id iv14-20020a056a20d80e00b0019c7560e7d6mr3950800pzb.1.1709069200526; Tue, 27 Feb 2024 13:26:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709069200; cv=pass; d=google.com; s=arc-20160816; b=WqY3cIgof+wS41rAymwFJMNG11iO72zYke2MLHqsyr+h2rXVIdepJO0eAjZutSVTiP WU3C9zsLOHHTDQjHsBVbXDZdfErDVEgefL3I3upSUMAsOJKMNy09/lHlLOcnUzZmoyP9 qbEUbVwVoHSl1QAnqqwodWkko1p2P+1X4merZJQDYb4UHEbCA/dZKHyEhlDgM3cjUumT 8tjTusQdoFQb/QGvgf3oEgvlNPyxYO8eDz87fE+nzfeTlrdaCFoRJWsZ8MPFR1AJq/uN lBL6dFJbufNOI0oB2tbLPE9NGsUjp+dKP+Of8t+dKotJ5kfQaZUYOp8uK7s4e3cieas9 9m4w== 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=lMlTrFTrr7klW2adN5JqkbNkQ8V2FIXm7+yroDer/Yc=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=xBgnXYVgmV0ivP5c6lsZgtQc99IAXtg9SwE3qHm7S8Ur5JQDPpUKGMkdiTbEsrQu6W xix3SYVwi87Cr9wRuAupKJ6axrZjgWksCgrpImNOj7mF1J/uJFSkB4AlNzxmkBH3nsWY LrOq6ASa/AfbKQvqPH7k1iWDdfMaNZf0tWCNAuustcvIDpuiKYf9nasZiES8OeaPTI/y FlqirxhRzJ43hwTGAh1Mh8FOzUYr07KU/RMYPAwMe/yiiX6ZGyjTLMOPh3OwX+ll5QrY i9/xumLJk2XcGjwk6bJeXaF8dD/zNadhUmhccSHdYP4s+pJTUToo+BAO4bKzWAfDLJg7 l2Zg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=B261yzQ5; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-84038-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84038-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id x3-20020a63aa43000000b005dc528a5317si6070846pgo.50.2024.02.27.13.26.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 13:26:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84038-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=B261yzQ5; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-84038-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84038-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 4E870291007 for ; Tue, 27 Feb 2024 21:26:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1A071586D0; Tue, 27 Feb 2024 21:25:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="B261yzQ5" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.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 9B50B155A4D for ; Tue, 27 Feb 2024 21:25:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709069112; cv=none; b=BdZLPtrObkg94wyWO3691haR8b4Q2bKCYnMbRerkqCmcMbSqfvieMqJ4APtJkAc+LLm+t+725OdZfCTSAm9Bt6chrpKw3liPJF4pdCoLAXRQE9BwfK1C6pdUntyaUNjSXD6wN6ksw1/wNKt/sfhgvkFwH+gQgv2Yc42Z3Bi3XaI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709069112; c=relaxed/simple; bh=W80pqKoVc1vqxX/XGuHFDF0b+nJhSWWU10+X3joqYlo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RjaSMki9uQJeMQeeq92o3wnhZrWY9m29tAsDV2NNcybJmALF8XC/3QSHM+Gb2I5xGuELXVJBoPFMn0kT6WgdI5dF5TTHJvwOIywFrVTH5QTqRqwBItHkOVPf9LoH4fjU8u9KvDZY5ZNNir3/WN6NVRT6lHxcRXaJINamtZV+6uo= 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=B261yzQ5; arc=none smtp.client-ip=198.175.65.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709069111; x=1740605111; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=W80pqKoVc1vqxX/XGuHFDF0b+nJhSWWU10+X3joqYlo=; b=B261yzQ5U92MZnUe7LjCcfeDrkpZNJ1l29hSOsPgXAczD1BJBv+yKjGY amLVrVb0iK2C5I21yDoDvCalA/PuO07CB1V/vxcWsjZwl9STayGgKAnDG Y0lHjFd00kNgq0ISUWyxhJ8NU2BMPActVRrr72FHOPCpKoHnHFTQtkvl1 02ACYUHu6Tij3/kIp68PCOZqkZ1WfE62z0yp4kFvi2C7YV95hGefDU3b4 TkXQIghfTaHeA+8eLP065tCZV6CyK9F+3U77Byh6qX6zN/SOgSJJxiqMZ vx1TXSSRz1RN839LgBi90mHxJ/CWK95sEGT2RE8P0JHHuIiKp/+kXpMV+ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="3567009" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="3567009" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 13:25:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="937032910" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="937032910" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 27 Feb 2024 13:25:02 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 48969812; Tue, 27 Feb 2024 23:24:56 +0200 (EET) 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" Subject: [PATCHv8 08/17] x86/tdx: Account shared memory Date: Tue, 27 Feb 2024 23:24:43 +0200 Message-ID: <20240227212452.3228893-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227212452.3228893-1-kirill.shutemov@linux.intel.com> References: <20240227212452.3228893-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 --- 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