From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73D45C00140 for ; Tue, 26 Jul 2022 15:00:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D75108E0002; Tue, 26 Jul 2022 11:00:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D24928E0001; Tue, 26 Jul 2022 11:00:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BEBA68E0002; Tue, 26 Jul 2022 11:00:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AC7018E0001 for ; Tue, 26 Jul 2022 11:00:16 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 70A5212129B for ; Tue, 26 Jul 2022 15:00:16 +0000 (UTC) X-FDA: 79729561632.05.0A896A3 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by imf09.hostedemail.com (Postfix) with ESMTP id 7DB3E1400CD for ; Tue, 26 Jul 2022 15:00:15 +0000 (UTC) Received: from zn.tnic (p200300ea970f4ff2329c23fffea6a903.dip0.t-ipconnect.de [IPv6:2003:ea:970f:4ff2:329c:23ff:fea6:a903]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 9EC801EC0622; Tue, 26 Jul 2022 16:51:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1658847084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=NGpnmZ5jm4nrbHTpdSQDfmfUGgLHgWESSBLxxIVaizU=; b=dHkIi2g4b9g7z5bsQlPX2NowyqkPzJCGlMi4Ufh5hsv5Tfj5YHiwctLroQfN+dGTx85G+h Y92PR06Yd9CQXG5YcmHOCRurUVgl9eoGkpiaLVvTWuZzeuhHEeBgIFSgFWv/jh0Uw+Amz0 0zRyMO9OBx+eb+9HGQ3kDZx/AIN1LPA= Date: Tue, 26 Jul 2022 16:51:16 +0200 From: Borislav Petkov To: "Kirill A. Shutemov" Cc: Andy Lutomirski , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel , Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Varad Gautam , Dario Faggioli , Dave Hansen , Mike Rapoport , David Hildenbrand , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCHv7 14/14] x86/tdx: Add unaccepted memory support Message-ID: References: <20220614120231.48165-1-kirill.shutemov@linux.intel.com> <20220614120231.48165-15-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220614120231.48165-15-kirill.shutemov@linux.intel.com> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658847616; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NGpnmZ5jm4nrbHTpdSQDfmfUGgLHgWESSBLxxIVaizU=; b=3noMZkoJZRKh42vAC+mqzQ5C4ql2jsb067BVYwRGvIAvesyRvAKK8JTULybzX2A1T5Sp8X Nd6Ms7nhImPrWe0Q1QWuYMXi+uIYSScR2AgOwULtIhU5JtRFNW4VUSdWCS7JqX7iCkTuf/ NMOu2M8XGV03zpiKm/mh95j8VGxxO58= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=alien8.de header.s=dkim header.b=dHkIi2g4; dmarc=pass (policy=none) header.from=alien8.de; spf=pass (imf09.hostedemail.com: domain of bp@alien8.de designates 5.9.137.197 as permitted sender) smtp.mailfrom=bp@alien8.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658847616; a=rsa-sha256; cv=none; b=LCzfuTjjKe8YGUTRhSo0xJfqZmLWZXocv1NxzI/5fJsMkmSGsKzfiFo2u138XDiiByA0sS e8b5zXcbXwdf5S0hFxGE85QJ42NcVrEMLr+mPKbVQk8JPyBGBPMFtTOjxmZDv6YoPL7R7G Xaforw1D37CjLsVAUV6v9GrtfvLwO0s= X-Stat-Signature: rgs14gyfoxdii14ri9ybe4n6czzf7bf9 X-Rspamd-Queue-Id: 7DB3E1400CD X-Rspam-User: X-Rspamd-Server: rspam12 Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=alien8.de header.s=dkim header.b=dHkIi2g4; dmarc=pass (policy=none) header.from=alien8.de; spf=pass (imf09.hostedemail.com: domain of bp@alien8.de designates 5.9.137.197 as permitted sender) smtp.mailfrom=bp@alien8.de X-HE-Tag: 1658847615-819062 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Jun 14, 2022 at 03:02:31PM +0300, Kirill A. Shutemov wrote: > +static bool is_tdx_guest(void) > +{ > + static bool once; > + static bool is_tdx; > + > + if (!IS_ENABLED(CONFIG_INTEL_TDX_GUEST)) > + return false; > + > + if (!once) { > + u32 eax, sig[3]; > + > + cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, > + &sig[0], &sig[2], &sig[1]); > + is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig)); > + once = true; > + } > + > + return is_tdx; > +} early_tdx_detect() already calls this CPUID function. It assigns function pointers too. So why can't you assign an accept_memory() function pointer there and get rid of this sprinkled if (tdx) everywhere? > diff --git a/arch/x86/boot/compressed/tdx.c b/arch/x86/boot/compressed/tdx.c > index 918a7606f53c..8518a75e5dd5 100644 > --- a/arch/x86/boot/compressed/tdx.c > +++ b/arch/x86/boot/compressed/tdx.c > @@ -3,12 +3,15 @@ > #include "../cpuflags.h" > #include "../string.h" > #include "../io.h" > +#include "align.h" > #include "error.h" > +#include "pgtable_types.h" > > #include > #include > > #include > +#include > > /* Called from __tdx_hypercall() for unrecoverable failure */ > void __tdx_hypercall_failed(void) > @@ -75,3 +78,78 @@ void early_tdx_detect(void) > pio_ops.f_outb = tdx_outb; > pio_ops.f_outw = tdx_outw; > } > + > +static unsigned long try_accept_one(phys_addr_t start, unsigned long len, > + enum pg_level level) That's pretty much a copy of the same function in arch/x86/coco/tdx/tdx.c. Yeah, you need a tdx-shared.c which you include in both places just like it is done with sev-shared.c ... > +void tdx_accept_memory(phys_addr_t start, phys_addr_t end) That one too. > +{ > + /* > + * Notify the VMM about page mapping conversion. More info about ABI > + * can be found in TDX Guest-Host-Communication Interface (GHCI), > + * section "TDG.VP.VMCALL" > + */ > + if (_tdx_hypercall(TDVMCALL_MAP_GPA, start, end - start, 0, 0)) > + error("Accepting memory failed\n"); > + > + /* > + * For shared->private conversion, accept the page using > + * TDX_ACCEPT_PAGE TDX module call. > + */ > + while (start < end) { > + unsigned long len = end - start; > + unsigned long accept_size; > + > + /* > + * Try larger accepts first. It gives chance to VMM to keep > + * 1G/2M Secure EPT entries where possible and speeds up > + * process by cutting number of hypercalls (if successful). > + */ > + > + accept_size = try_accept_one(start, len, PG_LEVEL_1G); > + if (!accept_size) > + accept_size = try_accept_one(start, len, PG_LEVEL_2M); > + if (!accept_size) > + accept_size = try_accept_one(start, len, PG_LEVEL_4K); > + if (!accept_size) > + error("Accepting memory failed\n"); > + start += accept_size; This series of calls to try_accept_one() appear in at least three places. Please carve them out into a separate function can put it in tdx-shared.c. -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette