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 BECFCC433EF for ; Tue, 14 Jun 2022 00:11:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 532328D01F3; Mon, 13 Jun 2022 20:11:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E2068D01EE; Mon, 13 Jun 2022 20:11:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35BC18D01F3; Mon, 13 Jun 2022 20:11:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 27C188D01EE for ; Mon, 13 Jun 2022 20:11:13 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 06906344AA for ; Tue, 14 Jun 2022 00:11:13 +0000 (UTC) X-FDA: 79574911626.26.E048975 Received: from mail-il1-f177.google.com (mail-il1-f177.google.com [209.85.166.177]) by imf17.hostedemail.com (Postfix) with ESMTP id A9EC14008B for ; Tue, 14 Jun 2022 00:11:12 +0000 (UTC) Received: by mail-il1-f177.google.com with SMTP id a15so5454978ilq.12 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=eIG0dsBRnY+8HAkF4vLyBFvO3Uj7keoHrgOEUyOhV+G12i7WMtw6IbUQacwGqe1/YA 7vtVlErFK8VfoCNVFtV0z6n8VWrVBz0/WK8+aC0BOm8gB5JlihzjSelinz2k3qU9W5IB Lkk8edazjgfexAwoY/9A5Gb2aC7bWRMlOBEY9eYTs5bmR3subl4PZAhoTKqzyRsjbn0t ejs8SSqFVcFGxLupMWqhfxXA/ppmlNc69zzGuP4eyyPxX7Q3DoXO+OGfbdFbWEwbOVLf oFzAWmI2JCgsupfuVCzlma3nHZTtqM4D+YNEX0YlWjcq6famg6F6ScNJgNt0vN0rplKi yMKQ== X-Gm-Message-State: AJIora8b9Ysv8KUrsFpb1qESo6cDT8BLGtk0pUCgvDx+x4Ldaks0stnt AjfKrHlhNEMrA+gQSOGcYT7be/RjB79lf5FYZEJCNA== X-Google-Smtp-Source: AGRyM1vvmWcS6Uk3XLwlWmm2TBNmlDnvXez0ftCObxk7xELuMfQjgAyThxD2SMRX8sV2ytdMhECXy+3RnrepRJd58Ec= 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" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655165472; a=rsa-sha256; cv=none; b=p+g+Aa/2BSjAo4cVQuzocIv1Q0H7RPe+7tb81KYX5C2BIB5aWiZrkyAj9Z94BEyDikcGSN DN07sHanVj5rVTzHY+7/g6L0G1U1q0MuQxpSNcMMZ+mDrTH+606mDsVafGFeWOqIxep3os xMRT5C5J6IyEyR8P1Ap/IKs6KmToaUU= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UWHIwl07; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of alpergun@google.com designates 209.85.166.177 as permitted sender) smtp.mailfrom=alpergun@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655165472; 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=YnMbZcTpvqgPLdBSjHRJuKCSk759c13xui6B7R1t1uQ=; b=EHOepXsz0BPpumTM8sn+a8SfrPYUGttBZ3xmC3ju4PuMUbnC6Yu2QYrVHYJGuzKl+7gi7F yd7ZpWk+zEbom6dAp9Dju2vSjTzUVaqOev+HrCWxtGtwtOLI1PZB2b6skFegcbooHiqCi9 nE3PnWQuLzdR3FgRysiE7x5+0CvvkrM= X-Rspamd-Queue-Id: A9EC14008B X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UWHIwl07; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of alpergun@google.com designates 209.85.166.177 as permitted sender) smtp.mailfrom=alpergun@google.com X-Rspamd-Server: rspam06 X-Stat-Signature: sqdrcu343rfn1pibrbewnjmkp7t5ixgk X-HE-Tag: 1655165472-381833 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: 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 > >