From patchwork Tue Feb 27 18:16:08 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: 207395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2922059dyb; Tue, 27 Feb 2024 11:35:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV/xfsRdIryWkytxe8f4SCw7DGU6L0Isbnr1lfr2cMYvKbWtzJ7IXpn8kY+EjQWoYCk6nUF7IZ4NL3vTju5x9P3DFuoqw== X-Google-Smtp-Source: AGHT+IHa6qNIWnMi9MGOVy0++wiaZPKpJ+HULeZ0m7bt+5KLhXqycalcnoxpJVhNOx3ItDOreyb4 X-Received: by 2002:a17:902:d2c4:b0:1db:5213:222 with SMTP id n4-20020a170902d2c400b001db52130222mr13801119plc.5.1709062519784; Tue, 27 Feb 2024 11:35:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709062519; cv=pass; d=google.com; s=arc-20160816; b=cYEctokiMk/aQ58pNVvrwST47RNyC2lBM+T61uJ5Mg5lKSfqUnHnMy7yQTkjitcS8U YbIe1HcNHA9YA7tTzU6BUSF+Ac/Jal5qr4XcJh7doNiA1jAtrUEdZ5n5TFjZx5ktSFzM jOxakfTrNENZ3zrr99Yk7kyZV60h1NPX9cObF8u8lKCuO61rI0RucTbbn8SL6SMFB0qu vShRYwYXF2Se/sZqh5+b63us4PRQ0zwgHpN4G1HnQJXFt4Qx95gBPTqHbaHoU9QiX9mt Orbib7zbuoe0VuJS04djONW5gVXgu1wPdZo9AXuk486rHzxjwIetpGZHrV/qt8dkI5l+ gwMQ== 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=QbNVvvHkrGHh7BR7Qwt2J0OgUR8dAXMlMSzqos0/4mY=; fh=OvJRnOqsMTm9XoNmEwebcqh9Ud7yh1CTeKAP84ols98=; b=ZP8EYCbiDizwgMgHdycQCJ3jfL76Im5scmu1zckA6RL0iNKs8MeublGJnI+hjkf9lU hS/rpm0KHL4ZYlV6obXC78f2tygJTeis+aba0CPRvbaZozcLKptLXn27JDuY2L2k3Ck3 5KNXPWF1z4V68yLbRU75xt9xGTa0l58R1P/WdpqTVodQctKshLP+FhouIKuWiLSrOX1x 55qqtAqo928grb9WdTpy+ofK7qN+UWZi2v7oKo8pLvA+yPX+ooz/2yRL9zstD42Egplr Q4KnTUjpNOSwtVatWa8ijdAhbWYZGWG2QdeazxYBKS/WSqwIuoMq4+ZSU1kiru1eAREc cNAw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NvuYTpxm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83817-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83817-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e2-20020a17090301c200b001dc01351441si1853611plh.528.2024.02.27.11.35.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 11:35:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-83817-ouuuleilei=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=NvuYTpxm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-83817-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-83817-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 733A928EAC0 for ; Tue, 27 Feb 2024 18:19:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A668F1534E9; Tue, 27 Feb 2024 18:17:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NvuYTpxm" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 39225145FF9 for ; Tue, 27 Feb 2024 18:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709057824; cv=none; b=eGFKgv7lbL3y5eVZGDtpAF5PdFb6sszMpahsQ5TL2mHz2pYa/jY1+MpSe5pYpJUVdxCLNYS7cJL0g0g3njHPjOGgimjrwKkkT0DX/oiMxziYw6NNnMx0zyYNMnqcJHJl7Ib7R6aQEvoYuptlC9svPFmCqvpClP8jWLklQ6zR3Qc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709057824; c=relaxed/simple; bh=ttorKfrAsCfwHjNDg0dNsgpEDrTBddN8JkJtVUzul2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ILQXysfmMJzNk+VyP0d2UKa7M0t+zW25cxtvSFFcAiON4cDEIaL/3ZK/Apotj/eO+Kt8vmSfJxJFCIxUP4JzSxkpQ0C4D4N+26M91kMHEzRKTxM0Me21tQcTA9eq5+utorAiwjytEN63g10kI0INq3Jy9ggu7sa638TmCUK/X4k= 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=NvuYTpxm; arc=none smtp.client-ip=192.198.163.8 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=1709057821; x=1740593821; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ttorKfrAsCfwHjNDg0dNsgpEDrTBddN8JkJtVUzul2k=; b=NvuYTpxmDxBZlriaQQenUPBNvomz4hQD6aovLdNxLcYs0f6YB55sDUv0 sS/mjQRzZsTA58bOfD+wsEZm6RchzWv2WZ9x/1gCSuRAka+wvAC3Jj3pv nj+Wz8jrcA9iFhS2fzlQ79JINxVdi5/6PM5iWKw38Vj8QmMSA/3gMNv8N Cz6g3l/zRkMR+CfbryjOCEkYZoCcLlxTP6rXOky3NV9UNjmOq6Hqj9prz XvN6RKOrRu80ntDf7hOtxSi62ITLlK8kU+iOsz3n7hcJrcYsiw45JPDaf jhh68gbGAdrgXRmKQYuTiPxk67gzZ4xf1/6stRvZeLAwpNG8Lq8nN+2hy Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="20966127" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="20966127" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2024 10:16:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="937032733" X-IronPort-AV: E=Sophos;i="6.06,188,1705392000"; d="scan'208";a="937032733" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 27 Feb 2024 10:16:26 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id A06DF812; Tue, 27 Feb 2024 20:16:19 +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/16] x86/tdx: Account shared memory Date: Tue, 27 Feb 2024 20:16:08 +0200 Message-ID: <20240227181616.3032032-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240227181616.3032032-1-kirill.shutemov@linux.intel.com> References: <20240227181616.3032032-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: 1792081940456107046 X-GMAIL-MSGID: 1792081940456107046 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. Include a debugfs interface that allows for the check to be performed at any point. Signed-off-by: Kirill A. Shutemov --- arch/x86/coco/tdx/tdx.c | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 26fa47db5782..fd212c9bad89 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -5,6 +5,7 @@ #define pr_fmt(fmt) "tdx: " fmt #include +#include #include #include #include @@ -38,6 +39,13 @@ #define TDREPORT_SUBTYPE_0 0 +static atomic_long_t nr_shared; + +static inline bool pte_decrypted(pte_t pte) +{ + return cc_mkdec(pte_val(pte)) == pte_val(pte); +} + /* Called from __tdx_hypercall() for unrecoverable failure */ noinstr void __noreturn __tdx_hypercall_failed(void) { @@ -821,6 +829,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; } @@ -896,3 +909,59 @@ void __init tdx_early_init(void) pr_info("Guest detected\n"); } + +#ifdef CONFIG_DEBUG_FS +static int tdx_shared_memory_show(struct seq_file *m, void *p) +{ + unsigned long addr, end; + unsigned long found = 0; + + addr = PAGE_OFFSET; + end = PAGE_OFFSET + get_max_mapped(); + + while (addr < end) { + unsigned long size; + unsigned int level; + pte_t *pte; + + pte = lookup_address(addr, &level); + size = page_level_size(level); + + if (pte && pte_decrypted(*pte)) + found += size / PAGE_SIZE; + + addr += size; + + cond_resched(); + } + + seq_printf(m, "Number of shared pages in kernel page tables: %16lu\n", + found); + seq_printf(m, "Number of pages accounted as shared: %16ld\n", + atomic_long_read(&nr_shared)); + return 0; +} + +static int tdx_shared_memory_open(struct inode *inode, struct file *file) +{ + return single_open(file, tdx_shared_memory_show, NULL); +} + +static const struct file_operations tdx_shared_memory_fops = { + .open = tdx_shared_memory_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static __init int debug_tdx_shared_memory(void) +{ + if (!cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) + return 0; + + debugfs_create_file("tdx_shared_memory", 0400, arch_debugfs_dir, + NULL, &tdx_shared_memory_fops); + return 0; +} +fs_initcall(debug_tdx_shared_memory); +#endif