Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp961600iog; Mon, 13 Jun 2022 17:22:03 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tocYo5IvYrkXTik0AunXu7I63rTmHzoz5KXkUZ6X/0bHRqWDm5su64+F/Zu+Sek6M7xnxw X-Received: by 2002:a17:902:7449:b0:167:9520:d063 with SMTP id e9-20020a170902744900b001679520d063mr1525447plt.146.1655166122887; Mon, 13 Jun 2022 17:22:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655166122; cv=none; d=google.com; s=arc-20160816; b=zV2Mt8jysTQ1xqxl9bNIhtXkwOmHv8e5nyC59LUeDca0EfMUSTyJ6i1K6+X4STmUU4 PKeN7Dy3eI4LeYwlefbO0fhZYlwh5kWJQGFpNV7P+mZc5sPH5ummxr8E545pCbqY7K0K DyyvurFMDL1cYn9af1prv0XYtvcJzQFkqhVyBsbDj2PSsBQk7wKbG6wWSnFfaoN+Kkuu v3n5wUzkAoNfFQWwkUW2XQ7Yen+ti1UkmCA2FiqwWpHbBHqqicFQpjRDUbmAJ2uWQWCu klo2BJ07BamG4Fp0fCWrV16lFVe1dtBLK4kzmFPH2IoXQylT54fcIS50Kvm4uY+cvhgM vAEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=YnMbZcTpvqgPLdBSjHRJuKCSk759c13xui6B7R1t1uQ=; b=BN3Gz/YrwVn8cTs/OwHKF2fqlKFGvmSWK+jfoQYwU9UBt43uHSatGiUFj1TttuWJuu vBr70cerEyn67I5t6AnMkTue2Vb0dvBe80fjWfmvBEb+D8oQFekV2uRyB7ActCOjWUJQ hKaIVSfD68m9rR911cYiL1F3W/2MDDDry5y1jdOSOZ61lvZaKOGH2H04MThvpiKLVRol VKUtnXqsTzv0mqeQQ3S2dvb/UbsVo7gv47jBoSsTc/ZAiDoAx9oiLc5H6OwnQaUTB3AL K8jf62oKAjybI5QdC+yn2nihtJnkhh/DuvFe/NFJdZNp1k1GG0amHA6B6GY9ImXvV8fN X8rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UWHIwl07; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 18-20020a17090a199200b001dfcda58373si13358838pji.104.2022.06.13.17.21.34; Mon, 13 Jun 2022 17:22:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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=@google.com header.s=20210112 header.b=UWHIwl07; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241532AbiFNALO (ORCPT + 99 others); Mon, 13 Jun 2022 20:11:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231978AbiFNALN (ORCPT ); Mon, 13 Jun 2022 20:11:13 -0400 Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94C2D13D6A for ; Mon, 13 Jun 2022 17:11:12 -0700 (PDT) Received: by mail-il1-x129.google.com with SMTP id s1so5520503ilj.0 for ; Mon, 13 Jun 2022 17:11:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=YnMbZcTpvqgPLdBSjHRJuKCSk759c13xui6B7R1t1uQ=; b=UWHIwl07hGDvI0wfcK3zDLePqB084XvOAEXZtG9YLs7Qamrq8qBh/97Rs7I8Cpc6Ky vQgi1KwGM1tjod5so3pAToAuYd5zAYthYQXWsok7uctu87wFLHq13M7UvoL5nShoA9Bm bUbNKL0dnUsLf2GOoerH9ILMtqhbH0CsPPJwK5xrk3EJO5EFoDQiIxEK4nucWJkXnZQg inUnSfdvuk0EYJ+0dUgFndr1/5B2aMu8LQIwGRFKLF5ZmkAI14sj8bF+hPUlDoU44DMJ gve7YjirSW6+HkmN7TJQ4guEwIYIa2uwuzDe5Us9Ymi41zRtq/9G5/8yEFC1+zaEdAOS rdfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=YnMbZcTpvqgPLdBSjHRJuKCSk759c13xui6B7R1t1uQ=; b=nNl+N3ahb5E8dZZTmS26Cn5FmsbNKDjkYQohJUitwxr4SZYet00Cw402iIZJhY22x8 18f7oRslr5kCxDY2Lggvt+HIx8B53ddVANom4Wx4OFBG8JTiQvvM3/1pedD7jD8HNH3Y lBpA/NDKptrOCMaSumW6MrY5wm9tCYcs7ZXDsGCu5tcSoa7eMeC/7uwflI1RlvQ227HY /ELcbLmv2KiU9/TIYM8upGISSzA/kpszfuUa2VDu9qYc1ibK5OlEbSU2Y3EvQiCVWsn7 Co4mL/bgw8ckLeUoHZnxUDtkJpN6iyfsA5nGvabp+12m43mMRBS5inRLzKj3zFgE2reZ KjJQ== X-Gm-Message-State: AJIora/6tjIC/Fac2uvg24wKH2noYajVnVobLGlYjc/dKPVP9bqyj5UI Ln+S5cUV4Vk7Ygi5Bb79kJ6y77cYRh+lTHjftn/stA== X-Received: by 2002:a05:6e02:1a23:b0:2d3:82bb:4dae with SMTP id g3-20020a056e021a2300b002d382bb4daemr1432588ile.62.1655165471758; Mon, 13 Jun 2022 17:11:11 -0700 (PDT) MIME-Version: 1.0 References: <20210820155918.7518-1-brijesh.singh@amd.com> <20210820155918.7518-15-brijesh.singh@amd.com> In-Reply-To: <20210820155918.7518-15-brijesh.singh@amd.com> From: Alper Gun Date: Mon, 13 Jun 2022 17:10:58 -0700 Message-ID: Subject: Re: [PATCH Part2 v5 14/45] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled To: Ashish.Kalra@amd.com Cc: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , tony.luck@intel.com, Marc Orr , sathyanarayanan.kuppuswamy@linux.intel.com, Brijesh Singh Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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-crypto@vger.kernel.org Similar to the TMR page, sev_init_ex_buffer should be owned by firmware. Otherwise INIT_EX won't work with the SNP. Since v5 patches are prepared before INIT_EX work, I wanted to bring this to your attention. One difference from the TMR page, sev_init_ex_buffer has to be in the direct map. Firmware pages are removed from directmap in v5 patches. But the kernel reads sev_init_ex_buffer later to write into a persistent file. I have a version to make it work, if you're interested I can share. On Fri, Aug 20, 2021 at 9:00 AM Brijesh Singh wrote: > > The behavior and requirement for the SEV-legacy command is altered when > the SNP firmware is in the INIT state. See SEV-SNP firmware specification > for more details. > > Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region > when SNP is enabled to satify new requirements for the SNP. Continue > allocating a 1mb region for !SNP configuration. > > While at it, provide API that can be used by others to allocate a page > that can be used by the firmware. The immediate user for this API will > be the KVM driver. The KVM driver to need to allocate a firmware context > page during the guest creation. The context page need to be updated > by the firmware. See the SEV-SNP specification for further details. > > Signed-off-by: Brijesh Singh > --- > drivers/crypto/ccp/sev-dev.c | 169 ++++++++++++++++++++++++++++++++++- > include/linux/psp-sev.h | 11 +++ > 2 files changed, 176 insertions(+), 4 deletions(-) > > diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c > index 01edad9116f2..34dc358b13b9 100644 > --- a/drivers/crypto/ccp/sev-dev.c > +++ b/drivers/crypto/ccp/sev-dev.c > @@ -62,6 +62,14 @@ static int psp_timeout; > #define SEV_ES_TMR_SIZE (1024 * 1024) > static void *sev_es_tmr; > > +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ > +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) > + > +static size_t sev_es_tmr_size = SEV_ES_TMR_SIZE; > + > +static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret); > +static int sev_do_cmd(int cmd, void *data, int *psp_ret); > + > static inline bool sev_version_greater_or_equal(u8 maj, u8 min) > { > struct sev_device *sev = psp_master->sev_data; > @@ -159,6 +167,156 @@ static int sev_cmd_buffer_len(int cmd) > return 0; > } > > +static void snp_leak_pages(unsigned long pfn, unsigned int npages) > +{ > + WARN(1, "psc failed, pfn 0x%lx pages %d (leaking)\n", pfn, npages); > + while (npages--) { > + memory_failure(pfn, 0); > + dump_rmpentry(pfn); > + pfn++; > + } > +} > + > +static int snp_reclaim_pages(unsigned long pfn, unsigned int npages, bool locked) > +{ > + struct sev_data_snp_page_reclaim data; > + int ret, err, i, n = 0; > + > + for (i = 0; i < npages; i++) { > + memset(&data, 0, sizeof(data)); > + data.paddr = pfn << PAGE_SHIFT; > + > + if (locked) > + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); > + else > + ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); > + if (ret) > + goto cleanup; > + > + ret = rmp_make_shared(pfn, PG_LEVEL_4K); > + if (ret) > + goto cleanup; > + > + pfn++; > + n++; > + } > + > + return 0; > + > +cleanup: > + /* > + * If failed to reclaim the page then page is no longer safe to > + * be released, leak it. > + */ > + snp_leak_pages(pfn, npages - n); > + return ret; > +} > + > +static inline int rmp_make_firmware(unsigned long pfn, int level) > +{ > + return rmp_make_private(pfn, 0, level, 0, true); > +} > + > +static int snp_set_rmp_state(unsigned long paddr, unsigned int npages, bool to_fw, bool locked, > + bool need_reclaim) > +{ > + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; /* Cbit maybe set in the paddr */ > + int rc, n = 0, i; > + > + for (i = 0; i < npages; i++) { > + if (to_fw) > + rc = rmp_make_firmware(pfn, PG_LEVEL_4K); > + else > + rc = need_reclaim ? snp_reclaim_pages(pfn, 1, locked) : > + rmp_make_shared(pfn, PG_LEVEL_4K); > + if (rc) > + goto cleanup; > + > + pfn++; > + n++; > + } > + > + return 0; > + > +cleanup: > + /* Try unrolling the firmware state changes */ > + if (to_fw) { > + /* > + * Reclaim the pages which were already changed to the > + * firmware state. > + */ > + snp_reclaim_pages(paddr >> PAGE_SHIFT, n, locked); > + > + return rc; > + } > + > + /* > + * If failed to change the page state to shared, then its not safe > + * to release the page back to the system, leak it. > + */ > + snp_leak_pages(pfn, npages - n); > + > + return rc; > +} > + > +static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) > +{ > + unsigned long npages = 1ul << order, paddr; > + struct sev_device *sev; > + struct page *page; > + > + if (!psp_master || !psp_master->sev_data) > + return ERR_PTR(-EINVAL); > + > + page = alloc_pages(gfp_mask, order); > + if (!page) > + return NULL; > + > + /* If SEV-SNP is initialized then add the page in RMP table. */ > + sev = psp_master->sev_data; > + if (!sev->snp_inited) > + return page; > + > + paddr = __pa((unsigned long)page_address(page)); > + if (snp_set_rmp_state(paddr, npages, true, locked, false)) > + return NULL; > + > + return page; > +} > + > +void *snp_alloc_firmware_page(gfp_t gfp_mask) > +{ > + struct page *page; > + > + page = __snp_alloc_firmware_pages(gfp_mask, 0, false); > + > + return page ? page_address(page) : NULL; > +} > +EXPORT_SYMBOL_GPL(snp_alloc_firmware_page); > + > +static void __snp_free_firmware_pages(struct page *page, int order, bool locked) > +{ > + unsigned long paddr, npages = 1ul << order; > + > + if (!page) > + return; > + > + paddr = __pa((unsigned long)page_address(page)); > + if (snp_set_rmp_state(paddr, npages, false, locked, true)) > + return; > + > + __free_pages(page, order); > +} > + > +void snp_free_firmware_page(void *addr) > +{ > + if (!addr) > + return; > + > + __snp_free_firmware_pages(virt_to_page(addr), 0, false); > +} > +EXPORT_SYMBOL(snp_free_firmware_page); > + > static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) > { > struct psp_device *psp = psp_master; > @@ -281,7 +439,7 @@ static int __sev_platform_init_locked(int *error) > > data.flags |= SEV_INIT_FLAGS_SEV_ES; > data.tmr_address = tmr_pa; > - data.tmr_len = SEV_ES_TMR_SIZE; > + data.tmr_len = sev_es_tmr_size; > } > > rc = __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); > @@ -638,6 +796,8 @@ static int __sev_snp_init_locked(int *error) > sev->snp_inited = true; > dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); > > + sev_es_tmr_size = SEV_SNP_ES_TMR_SIZE; > + > return rc; > } > > @@ -1161,8 +1321,9 @@ static void sev_firmware_shutdown(struct sev_device *sev) > /* The TMR area was encrypted, flush it from the cache */ > wbinvd_on_all_cpus(); > > - free_pages((unsigned long)sev_es_tmr, > - get_order(SEV_ES_TMR_SIZE)); > + __snp_free_firmware_pages(virt_to_page(sev_es_tmr), > + get_order(sev_es_tmr_size), > + false); > sev_es_tmr = NULL; > } > > @@ -1233,7 +1394,7 @@ void sev_pci_init(void) > } > > /* Obtain the TMR memory area for SEV-ES use */ > - tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE)); > + tmr_page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(sev_es_tmr_size), false); > if (tmr_page) { > sev_es_tmr = page_address(tmr_page); > } else { > diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h > index f2105a8755f9..00bd684dc094 100644 > --- a/include/linux/psp-sev.h > +++ b/include/linux/psp-sev.h > @@ -12,6 +12,8 @@ > #ifndef __PSP_SEV_H__ > #define __PSP_SEV_H__ > > +#include > + > #include > > #ifdef CONFIG_X86 > @@ -919,6 +921,8 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error); > int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error); > > void *psp_copy_user_blob(u64 uaddr, u32 len); > +void *snp_alloc_firmware_page(gfp_t mask); > +void snp_free_firmware_page(void *addr); > > #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ > > @@ -960,6 +964,13 @@ static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *erro > return -ENODEV; > } > > +static inline void *snp_alloc_firmware_page(gfp_t mask) > +{ > + return NULL; > +} > + > +static inline void snp_free_firmware_page(void *addr) { } > + > #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ > > #endif /* __PSP_SEV_H__ */ > -- > 2.17.1 > >