Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1020036rwb; Thu, 15 Dec 2022 05:37:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf493asGxEaa9JyoyWX/+pSBlxEIibwyZHQMvDISIHQut6jHWpecoQ98V6ZdBT+pASgZIhus X-Received: by 2002:a05:6a20:bb24:b0:af:7989:2579 with SMTP id fc36-20020a056a20bb2400b000af79892579mr5529276pzb.32.1671111420425; Thu, 15 Dec 2022 05:37:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671111420; cv=none; d=google.com; s=arc-20160816; b=jfprAHEWBxOC0MOklbTQxbVSaLYGpJsRzmSvFxxyc5fzrbP0q7siID7014ApA7IOhP 7AOCUISob46xPnMtZKbgK/Giy1QxXSIlv8QCC1JWcpSPC96z2GiyiljL58fNHPdaCHCE bioG7dFSfOpbMnu1Kmf42id9Lo1sRY9AVi5b7X6j/4CbaJoVyNXpqjRbTAHmVHFhTxz0 EoEreNWDukKg28JanSAuGl0PMLjyfmUhspgjSLWcCPr0Ymgv+daHpljZ9ZvKDHJGQl0C aP4ArVG6nJ8QdIcm1bcdwgI3mBRIRK2xkUvRRRFWLmcmYsnj8xDCsfC507byEVL74+N8 2zHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dkim-filter; bh=7vCuNFrK1OQpeQJVAk4kMKTCnLpf775i8aOo4DRS+9k=; b=Uu7ZG0p1XFSnr0HmybN820LpsFf7lz+XhdmBsho6twXLKlsYNTgBklxt+U3e77eWGX V0G2FSh7T7JdCK5Kp1K4zKO+DmsD6err/I70xrNtoeG5EiKwjVnwZ57fONkVP+iVSMzK vzDsSbK1yAJLXWdiNbdxG3AdtsIgjFlvQJ0dq58x0wY3EI+3jJuqgpoffEge+9h1pd/f mMh9GDfqLfo4oqEAGFncgTuqmChP+QTdfdln6fNRV54MgkEKm44BfugEHU0DAl0U7rYE 6oRNuJ6p9bxO1d1kSNWhmYHJJtX9bkoeR8dIRSfe/8nM1oBLM3NRbr7BpeOln0mx7S3l pR8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b="QZnI/Iyy"; 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=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r7-20020a63e507000000b00477f5addc5dsi2771876pgh.487.2022.12.15.05.36.41; Thu, 15 Dec 2022 05:37:00 -0800 (PST) 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=@linux.microsoft.com header.s=default header.b="QZnI/Iyy"; 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=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229488AbiLON3U (ORCPT + 99 others); Thu, 15 Dec 2022 08:29:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbiLON3T (ORCPT ); Thu, 15 Dec 2022 08:29:19 -0500 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6A36518B1C; Thu, 15 Dec 2022 05:29:18 -0800 (PST) Received: by linux.microsoft.com (Postfix, from userid 1112) id 0309520B92BA; Thu, 15 Dec 2022 05:29:18 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0309520B92BA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1671110958; bh=7vCuNFrK1OQpeQJVAk4kMKTCnLpf775i8aOo4DRS+9k=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QZnI/IyyKwaP/zHzNYXBCCMLXwZ6fWvbR3+FSmMN8vXx7vDA9nqW6jBuCd94pC8h5 AxMEb0GPQZDrKvtVpzQGSdOOMd57S+oHh7VfYSpHkUuhy2Pznc/RAL9npCWFUU+YVH Zwan7XglTQRg5zjvabjEK8IJ+GIpzc9ooNPxTQRI= Date: Thu, 15 Dec 2022 05:29:17 -0800 From: Jeremi Piotrowski To: Rijo Thomas Cc: Tom Lendacky , John Allen , Herbert Xu , "David S . Miller" , Sumit Semwal , Christian =?utf-8?B?S8O2bmln?= , linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Mythri PK , Jeshwanth , Devaraj Rangasamy , stable@vger.kernel.org, Jens Wiklander Subject: Re: [PATCH v2] crypto: ccp - Allocate TEE ring and cmd buffer using DMA APIs Message-ID: <20221215132917.GA11061@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net> References: <651349f55060767a9a51316c966c1e5daa57a644.1670919979.git.Rijo-john.Thomas@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <651349f55060767a9a51316c966c1e5daa57a644.1670919979.git.Rijo-john.Thomas@amd.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,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 On Tue, Dec 13, 2022 at 04:40:27PM +0530, Rijo Thomas wrote: > For AMD Secure Processor (ASP) to map and access TEE ring buffer, the > ring buffer address sent by host to ASP must be a real physical > address and the pages must be physically contiguous. > > In a virtualized environment though, when the driver is running in a > guest VM, the pages allocated by __get_free_pages() may not be > contiguous in the host (or machine) physical address space. Guests > will see a guest (or pseudo) physical address and not the actual host > (or machine) physical address. The TEE running on ASP cannot decipher > pseudo physical addresses. It needs host or machine physical address. > > To resolve this problem, use DMA APIs for allocating buffers that must > be shared with TEE. This will ensure that the pages are contiguous in > host (or machine) address space. If the DMA handle is an IOVA, > translate it into a physical address before sending it to ASP. > > This patch also exports two APIs (one for buffer allocation and > another to free the buffer). This API can be used by AMD-TEE driver to > share buffers with TEE. > > Fixes: 33960acccfbd ("crypto: ccp - add TEE support for Raven Ridge") > Cc: Tom Lendacky > Cc: stable@vger.kernel.org > Signed-off-by: Rijo Thomas > Co-developed-by: Jeshwanth > Signed-off-by: Jeshwanth > Reviewed-by: Devaraj Rangasamy > --- > v2: > * Removed references to dma_buffer. > * If psp_init() fails, clear reference to master device. > * Handle gfp flags within psp_tee_alloc_buffer() instead of passing it as > a function argument. > * Added comments within psp_tee_alloc_buffer() to serve as future > documentation. > > drivers/crypto/ccp/psp-dev.c | 13 ++-- > drivers/crypto/ccp/tee-dev.c | 124 +++++++++++++++++++++++------------ > drivers/crypto/ccp/tee-dev.h | 9 +-- > include/linux/psp-tee.h | 49 ++++++++++++++ > 4 files changed, 142 insertions(+), 53 deletions(-) > > diff --git a/drivers/crypto/ccp/psp-dev.c b/drivers/crypto/ccp/psp-dev.c > index c9c741ac8442..380f5caaa550 100644 > --- a/drivers/crypto/ccp/psp-dev.c > +++ b/drivers/crypto/ccp/psp-dev.c > @@ -161,13 +161,13 @@ int psp_dev_init(struct sp_device *sp) > goto e_err; > } > > - ret = psp_init(psp); > - if (ret) > - goto e_irq; > - > if (sp->set_psp_master_device) > sp->set_psp_master_device(sp); > > + ret = psp_init(psp); > + if (ret) > + goto e_clear; > + > /* Enable interrupt */ > iowrite32(-1, psp->io_regs + psp->vdata->inten_reg); > > @@ -175,7 +175,10 @@ int psp_dev_init(struct sp_device *sp) > > return 0; > > -e_irq: > +e_clear: > + if (sp->clear_psp_master_device) > + sp->clear_psp_master_device(sp); > + > sp_free_psp_irq(psp->sp, psp); > e_err: > sp->psp_data = NULL; > diff --git a/drivers/crypto/ccp/tee-dev.c b/drivers/crypto/ccp/tee-dev.c > index 5c9d47f3be37..5c43e6e166f1 100644 > --- a/drivers/crypto/ccp/tee-dev.c > +++ b/drivers/crypto/ccp/tee-dev.c > @@ -12,8 +12,9 @@ > #include > #include > #include > +#include > +#include > #include > -#include > #include > > #include "psp-dev.h" > @@ -21,25 +22,73 @@ > > static bool psp_dead; > > +struct psp_tee_buffer *psp_tee_alloc_buffer(unsigned long size) > +{ > + struct psp_device *psp = psp_get_master_device(); > + struct psp_tee_buffer *buf; > + struct iommu_domain *dom; > + > + if (!psp || !size) > + return NULL; > + > + buf = kzalloc(sizeof(*buf), GFP_KERNEL); > + if (!buf) > + return NULL; > + > + /* The pages allocated for PSP Trusted OS must be physically > + * contiguous in host (or machine) address space. Therefore, > + * use DMA API to allocate memory. > + */ > + > + buf->vaddr = dma_alloc_coherent(psp->dev, size, &buf->dma, > + GFP_KERNEL | __GFP_ZERO); dma_alloc_coherent memory is just as contiguous as __get_free_pages, and calling dma_alloc_coherent from a guest does not guarantee that the memory is contiguous in host memory either. The memory would look contiguous from the device point of view thanks to the IOMMU though (in both cases). So this is not about being contiguous but other properties that you might rely on (dma mask most likely, or coherent if you're not running this on x86?). Can you confirm why this fixes things and update the comment to reflect that. > + if (!buf->vaddr || !buf->dma) { > + kfree(buf); > + return NULL; > + } > + > + buf->size = size; > + > + /* Check whether IOMMU is present. If present, convert IOVA to > + * physical address. In the absence of IOMMU, the DMA address > + * is actually the physical address. > + */ > + > + dom = iommu_get_domain_for_dev(psp->dev); > + if (dom) > + buf->paddr = iommu_iova_to_phys(dom, buf->dma); > + else > + buf->paddr = buf->dma; This is confusing: you're storing GPA for the guest and HPA in case of the host, to pass to the device. Let's talk about the host case. a) the device is behind an IOMMU. The DMA API gives you an IOVA, and the device should be using the IOVA to access memory (because it's behind an IOMMU). b) the device is not behind an IOMMU. The DMA API gives you a PA, the device uses a PA. But in case a) you're extracting the PA, which means your device can bypass the IOMMU, in which case the system should not think that it is behind an IOMMU. So how does this work? Jeremi > + > + return buf; > +} > +EXPORT_SYMBOL(psp_tee_alloc_buffer); > +