Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp259727rdf; Tue, 21 Nov 2023 01:42:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwHzCMD731YktHJp5Ovb7ZjdPs4U8Wui9uLGKa8jrjoT6P+aw2NR7nSfAWfES2dmvwOgQL X-Received: by 2002:a17:90b:1b10:b0:274:7db1:f50f with SMTP id nu16-20020a17090b1b1000b002747db1f50fmr3281855pjb.15.1700559763718; Tue, 21 Nov 2023 01:42:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700559763; cv=none; d=google.com; s=arc-20160816; b=uR/KfpLcU8qOq/W+9h5dHsoXAKKsV4VTjs+uM4gUfRaLQf1qOJ80HIhhQqakMXrRtT c6zrWLzP06hw2st7MAA2M6ZvxWLC8R7mqlsMTr9E98qadlcBs2b+St8OnRqZLOgSHSAR OFYou0Rjd1Xd9dsY5HvcLsLSLZm4jTLrrF+2HHT2QtSdoOEa7C/jDdoTVyqBvTkTA7Fe evq7e7sbuMaS5rR5gfNl7yOkDeOhgpk2eudeiIbLq8Gl4Vc3r4u5g56onY/MF4smTFni TeLcnJdFQesqCgXTk1Q5878T12Oqs8VFz/B+jl1QENtEUTMpAe8AM7eQnz66CSwwJBNt GpqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/IRJgpv/XJy3Ec3akiam3PgN8zEXQBWOzaD2anw9Mwc=; fh=f4SnCP18GcvdWICWlZ/o9/0mE+7famEFm5zezDEmU9w=; b=TO9g5Ztd+MJCtfO/dcZ7+2KHqQzLTdK79qPAUOf84iRarSdFT/cLxPXGRdexKXMxX3 7nrQ9W8vTd1Ml59M/SP4DlS7/9Rpe6eY9c35kFKTejA4ga6gtwwvR9H/LpEm/VaU2osL jKptmBGra/YB47Lco5Z1fvd4R/JaF6lhp6loE3BXICQnp5dKF0L3M0CfxdmjPc2QZePd 2iAKVLWPtqwOoFydqYA0IuI+2EZ6ceMAvyGLcaMvuNoP4ZwZGuk13WwCEK34/xIb2qnd JivRXkpUaaMxFfewURgWqSCZivSmkqmQqzCt1C4CN0qA7uV/gUOzn4siTHyioIP+G3JL jDIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IaSpGEpZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id mh11-20020a17090b4acb00b002800b13adb7si10482473pjb.72.2023.11.21.01.42.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 01:42:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IaSpGEpZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 3C83F80E879A; Tue, 21 Nov 2023 01:42:42 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231982AbjKUJmd (ORCPT + 99 others); Tue, 21 Nov 2023 04:42:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231955AbjKUJmc (ORCPT ); Tue, 21 Nov 2023 04:42:32 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B784490 for ; Tue, 21 Nov 2023 01:42:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700559748; x=1732095748; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=gKDb5RgVL1eT3zD06y02XsLI88vWKvuYieg0wRPL9PM=; b=IaSpGEpZFwzTQzblpjc0arK4wHT4Frw0cH7syVboMrSkyQknArEp5s5Q XPk5FBnoQbZVFjLHNR8J6XFCm9BszexFj2/dCQSbdCwHGU7Fcv5jraS4Z yN9Sontd68c5hn6yVA8s8zrMkLDqNfudyFo4Y24HETr517j7xWq7LWisk ywknTUis1ShZH2BrGrnBI3ZONqgEflkWT51b0fN5YbNTmVRdG0gDLKl/s ByskiVBigi5nZXI7UO3aTtR1e3UybkaS8uTCisFcK/rKsifl7PGdUy1Wd eKoqpIY+335tcLV/QQ1xFxfizSyEFL0oL+viazp7/JdePZuIj+qJGtZCX A==; X-IronPort-AV: E=McAfee;i="6600,9927,10900"; a="376839376" X-IronPort-AV: E=Sophos;i="6.04,215,1695711600"; d="scan'208";a="376839376" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 01:42:28 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10900"; a="766584619" X-IronPort-AV: E=Sophos;i="6.04,215,1695711600"; d="scan'208";a="766584619" Received: from ikosarev-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.40.84]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 01:42:22 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id 0860910A36E; Tue, 21 Nov 2023 12:42:20 +0300 (+03) Date: Tue, 21 Nov 2023 12:42:19 +0300 From: "kirill.shutemov@linux.intel.com" To: "Huang, Kai" Cc: "tglx@linutronix.de" , "mingo@redhat.com" , "x86@kernel.org" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "Edgecombe, Rick P" , "Reshetova, Elena" , "Nakajima, Jun" , "rafael@kernel.org" , "peterz@infradead.org" , "linux-kernel@vger.kernel.org" , "sathyanarayanan.kuppuswamy@linux.intel.com" , "Hunter, Adrian" , "thomas.lendacky@amd.com" , "ashish.kalra@amd.com" , "kexec@lists.infradead.org" , "seanjc@google.com" , "bhe@redhat.com" , "linux-coco@lists.linux.dev" Subject: Re: [PATCHv3 09/14] x86/tdx: Account shared memory Message-ID: <20231121094219.x5ez6ohzc773viul@box.shutemov.name> References: <20231115120044.8034-1-kirill.shutemov@linux.intel.com> <20231115120044.8034-10-kirill.shutemov@linux.intel.com> <07e7619d098ba3579f642a97b644f256884ed0a2.camel@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <07e7619d098ba3579f642a97b644f256884ed0a2.camel@intel.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 21 Nov 2023 01:42:42 -0800 (PST) On Tue, Nov 21, 2023 at 02:47:29AM +0000, Huang, Kai wrote: > > > +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) > > { > > @@ -820,6 +828,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; > > } > > > > @@ -895,3 +908,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 unshared pages in kernel page tables: %16lu\n", > > + found); > > + seq_printf(m, "Number of pages accounted as unshared: %16ld\n", > > + atomic_long_read(&nr_shared)); > > unshared -> shared? Right. > Btw, I am not quite sure what's the purpose of reporting number of shared pages > in both kernel page table and that the kernel is accounting? > > IIUC, there might be slight chance that the former is different from the latter > (i.e., when user reads this while the kernel is converting pages > simultaneously), but in most of the time the user should see they are the same. > > I can see it might be helpful to report @nr_shared to the user, but how can > reporting both help the user? It is critical to unshared *all* pages on kexec or the second kernel will crash at some point on accessing shared page as private. This is the sanity check: if number of shared pages in page tables is less than what we expected, we've lost shared bit somewhere. And kexec will likely be a disaster. Ability to trigger the check a any point can help to correlate the leak with activity. > That being said, I think perhaps you can separate the /sysfs part as a separate > patch because it's not a mandatory part of this series but a nice to have. Then > the /sysfs part can be reviewed separately. Okay, makes sense. -- Kiryl Shutsemau / Kirill A. Shutemov