Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp742220imn; Tue, 26 Jul 2022 08:08:20 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sy3rqJd4lpaxgrEZ85kIvn/APgf5bvcWnRj6Wep1NBOfW/fh4+5Oy/3+3DU7f5zqpnDyqA X-Received: by 2002:a17:907:2c78:b0:72b:64f5:11ea with SMTP id ib24-20020a1709072c7800b0072b64f511eamr14782611ejc.68.1658848100063; Tue, 26 Jul 2022 08:08:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658848100; cv=none; d=google.com; s=arc-20160816; b=Q+7leYhIcdIzkRyPdt16XZSrUTmYWkUkHlaKabAZ1Oeot2qdHVIIFMSOjT0pIodrou UBYUiU9PIRXb9rauU7ij80s8MNZLjNTypH+kmDVVXPKfANsVH+rGmkGsm0eJhJnqarL+ ATZxVVUi1W7lvKdVCTvhtG6AftcqR5vtDMVSZrJ9GnCqPKrXs14IvJmlH43f7rC3wt4y 49aDNC4AxwiO7qIQD80dj71AkLGYmN/wxPUURgylE3+4JujH8TiXHzR4lrGylwN/H0ku Q5Z//beo7IL3jDizNxvsC7mbDFrS8x9mkLUDRw/4SBSlX43loRTUUx/U21ZjirP+O5wF cIsA== 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=NGpnmZ5jm4nrbHTpdSQDfmfUGgLHgWESSBLxxIVaizU=; b=0DzjTXVemyUAJsiO/TWEEseVWsx84bWpvGHUkoapfsIgYFwJaZY11TwTeYLfaoKzCo yi8Vo37wZX8BZ6wr1HFbBjykYVuRpz7DIASVlfjedofyO7mjeNdobllgnaPxDMs7yrp8 t2JN8KaHGNk4+TJXPEqUdy1zQm9FBPPCo5MjCreRxj9cmE577AUR301h8R9aPhuhoLu9 54sSBCCaow+s2/AVaOCn9u4L98HXmeHy9ksnfy+dmnjUYKx8UknOlEhOtuT1YWXtW+bo r1xfUDPyuJpPBsj4azbM6CRVrWnaHtfhcntwxB3AB7Hw2iRtDtrmK8vre/Wlqo+76gzh uZVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=dHkIi2g4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw17-20020a170906479100b0072ed9eda8e6si16904189ejc.985.2022.07.26.08.07.54; Tue, 26 Jul 2022 08:08:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=dHkIi2g4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239453AbiGZOvd (ORCPT + 99 others); Tue, 26 Jul 2022 10:51:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233523AbiGZOvc (ORCPT ); Tue, 26 Jul 2022 10:51:32 -0400 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23C3B15802; Tue, 26 Jul 2022 07:51:30 -0700 (PDT) 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> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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 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