Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2532676lqb; Tue, 28 May 2024 02:58:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdHjv2n+iroEDmZNoZJlLxek4BzHfz8yhfYKi/tCSxi+WKUk1PyIBC9uOflPrqFQzqUJqYYE5ORHWCj8EtLXYhQ5i3PGtLiucM1nivbw== X-Google-Smtp-Source: AGHT+IG7OhKKg/Oa6cipb9mIfKtInJH97taPUNDJuMQKbNEAwpl/5KD+2elV173ShsNIxKROb9K/ X-Received: by 2002:a05:6214:223:b0:6ad:5f0f:6c33 with SMTP id 6a1803df08f44-6ad5f0f6d3amr101310646d6.57.1716890328975; Tue, 28 May 2024 02:58:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716890328; cv=pass; d=google.com; s=arc-20160816; b=YwyN+2tqglAPjb/foxrhjseqdQaSt7oX/VVHgoDyXhR/iKB7fC8qnvLiGpzaPUBraL 2310GkVT8WQ4d8A8kFiRv2lHgwT7z6m7qtsa7F91dAB6WedNIrr+T0AVcnA8x9QgZ4xD qabMOlFpYoxK1TPWnFFrvNHlwZeGihW+27HVFWQRXwkwKARWS5aV8MkRsXI6urYKt4z2 hCm3rUYWQaxsjJeWadY6hMuuK6/0yKM9aT9360yqDYoQrI8klS/2cz4ApXmJFhUSWYI8 E/g16DkvlEgRz4nDdFyOtdKoQwx1dBgfHvAV/cv00msYkEet3Wmqm8QzlA7uvhqjzgam geHg== 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=jKzY+IJTxktZ685R93VhJUbGF0rvTl3iTwtLazyIugI=; b=I+M90RwzaYyZgqQy0YRa62BWH2i2pTITKksL9wER2Z/cKgl0Ikr7uJPMu3u4ULEcc2 /AIEHFDY/dulWxPtgKr0neINVXt/XVUoOZamvBu5IxyAkNtE6UYXhwz5F/0ToUcN/o2C MW1C2o1oRNlgje3nEDa+WA9JG7qP6WEmymyF7o6dF0Azq8OCEsQPqHYHaEdq9rDyBbMO zr8m5bWdKCF+kRq+OYq7pMrEuQNEJWDVCQ7ZHbv9xOD3XRVTzxtZACPdOHqmytUfdvFk jH6rcWH0NAskeuegZYpaEv7bBainBXuRAVaJcbW8G14gzR6PWEvl3pWOyBCQ5tW8PpyW eb5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MxqgJFHn; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-192051-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192051-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6ac0fa6259asi97708136d6.314.2024.05.28.02.58.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 02:58:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192051-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MxqgJFHn; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-192051-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192051-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 912001C23A6C for ; Tue, 28 May 2024 09:58:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B11D516E86B; Tue, 28 May 2024 09:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MxqgJFHn" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (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 4FB7D16D9C8; Tue, 28 May 2024 09:55:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890156; cv=none; b=h+uZWPfWL0ap3YxOu++br/ZzS79HgV4qc42ajkSMUkUIbtkVnPFYDyivUeBBvjcrGDDWNK++m0AyacryMEsyIFElDWRPfEYcP+xDRf2VH9rSTD99OlsQAfMnYrjW6391TLTQm8EDuQJsTxGMrZfD2K8kDGktQGzCCWg1O9aTbxQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890156; c=relaxed/simple; bh=ggOF5UziCfkAXFWyu9AjZBbe749R0vOnMDQfmgr6Dec=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=osAY1S9zuKJcycRtRjaV16c/d9RltTIQSlyUTpSEraIPKmvdDIe+b87mjXjR/PxWpUZiz+Pem+8+kR6Mdn78WBDbRqQ2ssdJ4gn481xYgO54rA1d9TS10a/5KmpJVCoV9VvwQeGZQVoKkeLAOac2qGDvAeHUyJ3lF+PN5jcbSPU= 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=MxqgJFHn; arc=none smtp.client-ip=198.175.65.21 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=1716890156; x=1748426156; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ggOF5UziCfkAXFWyu9AjZBbe749R0vOnMDQfmgr6Dec=; b=MxqgJFHnLtBBREofd9QtLC4uJL4QWYz/YH142ejkWEhLDHKOVnZG4vE1 BmUbJaDlzBgiJMIdBoHLlNvqBSDeylM+FHpQOF8SwNanmQn+Hbtq8US73 fnwdshO0Vqy7sIORqi+j2BPwBoPWPa+Rx4lAmWZph5xAPghtZAvA7OxO7 cUWMAtgHNwkqtOuBE1SZb2ZJJAVHXU6qPeBLmDObSVM4RG4z0YlxAotgj GWCGux3+8AqulQYb1kzwU2R8dVjJiGIg3DPYrlbUD+pB1XVBrrft13u1f H1onWvrQ/MV67O0dByaBnVDTXwb+BISRVXt3aS/edu/7Gj7R+hZjocP/g w==; X-CSE-ConnectionGUID: 9DFki978SnqCXMdo8TLCwA== X-CSE-MsgGUID: Rm2i719qQtWVkxc5eNH+TA== X-IronPort-AV: E=McAfee;i="6600,9927,11085"; a="13172186" X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="13172186" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2024 02:55:41 -0700 X-CSE-ConnectionGUID: J1NSLEg5Q4us7sdnT0LpJA== X-CSE-MsgGUID: lwOD5fg7R4aE5sJChWCTtw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="39984750" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa004.jf.intel.com with ESMTP; 28 May 2024 02:55:34 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id AA54977C; Tue, 28 May 2024 12:55:26 +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: [PATCHv11 09/19] x86/tdx: Account shared memory Date: Tue, 28 May 2024 12:55:12 +0300 Message-ID: <20240528095522.509667-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> References: <20240528095522.509667-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