From patchwork Tue Feb 27 21:24:43 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: 207544 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3045154dyb; Tue, 27 Feb 2024 16:16:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVavNCIVC5dFQkyo5NesDpXr0Z5JDojQG+AYkKdf8kskn6SxI6OU7NqCUz2a98oR5nAZkACvkcD26hhS/OqSoXTJaIFxg== X-Google-Smtp-Source: AGHT+IEGs3cYPLaoiIkbNPHEOZ8Y6lcESdZMBfSi1Ge3Lf3JxjTf0pRDE8Kw1WRRQLxkiR26s1RR X-Received: by 2002:ac2:5b1a:0:b0:512:eee2:d8d0 with SMTP id v26-20020ac25b1a000000b00512eee2d8d0mr6947144lfn.59.1709079402420; Tue, 27 Feb 2024 16:16:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709079402; cv=pass; d=google.com; s=arc-20160816; b=p0r9eoBjPM5VGv4jX0ki8Lcydx0LKoVZD6vZM5E5IzGeRWqTEVvl6QkjTkCDFYhN4Z HBsVfMhXhaOTH6SqaRz6JoiZT0IwpMDWPFDDqMiebmdd+wN4wJz1CdLfgN2nNtGl49/5 W+09maDlJ1ab+V5xi6paldzLV2lhrfW3RqSsDsGjaDhhF7TetJCQk3wXKCS1WtBv4pIv zYnzG3cOSPinyR7UQCdgtAWEl+X/+ZTWrAsz9wLpOOWGRk67dl5L5OuTtETVjd3PfWrp gDNnJKdfD1TBZYdmLKHRfCzWtoJzps2eNxXvFSb0dOR/wT5f5843qjFO9fPVEA8jVZ0o x5EA== 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84038-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id bn7-20020a170906c0c700b00a3e730bc98bsi1178312ejb.395.2024.02.27.16.16.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 16:16:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84038-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84038-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 am.mirrors.kernel.org (Postfix) with ESMTPS id F106A1F2CB29 for ; Tue, 27 Feb 2024 21:26:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B8A1E158D74; Tue, 27 Feb 2024 21:25:14 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792097805462222752 X-GMAIL-MSGID: 1792099643233024787 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; }