Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1205205pxj; Sat, 12 Jun 2021 01:24:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkpucn7rwQjnvkMb+ZOJEvdEK1kk/z5wc59g73i1wM76hh7HrCIe473hi01aEZQJ8MsUdA X-Received: by 2002:a05:6402:4cf:: with SMTP id n15mr7916814edw.162.1623486289588; Sat, 12 Jun 2021 01:24:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623486289; cv=none; d=google.com; s=arc-20160816; b=hNvKiOH1F0O6PnJ1NsEpdrrVeWk1CzvpTTRunfMvIcG4XUB42t7VhY0L9rnlp6keO/ SXve70p+6tkur1XOgeLLDkMutkZCqky3O5Hy+n2wK3tMSJu5/oCRgaP9QQBGHbYW5QAF YouGyvbXyrmpYdnxSn3JsdOCvKhFJ9rDUZV5ELA655B4u1gSK0A9dZPABvnzvqabPG3N KN+zM+i/hT2bUKURhr9xu64ycYZ8b4wOrmLEpmN9K4OmNOltHCLkwtNM1ORvokiptQU8 x9w8U5ZCf0Z9JXMS1+2jj6NrmnpsNS2z6Pxy2Ne6yD0OUGcAp+jVUla0ZCkfcNZj5K2i VStw== 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=Y2SpUEzOky83sMYBnEdAjJxCHRE/WrgH62z28bh+deo=; b=nk/7Wz6r2hYw2GiWQaeOyLAOP63dKaGPj9vQDLNyR5qmLbAk6ubNrYvptnx/xwnHb8 eJLf2HsLSNqpI4pIAA7aCzM89Rqk5iDAsincPLe67g84pFdygHrM3kizFo4kFnF4JUa0 yOeJe/l9WQF6riKt24fGx9HgEpqNLDJQi/g1bpFakUZWjsWX9kkvyAfX5NtRr/w5XlhL 5EZ7yDzdfV9cxbcx1N6FLUiFDa5lMeu3evHwP5I/GrmDaxXMfTMiid0fw+MkcycQGbJ/ 8Rb921jD+En0dnPbv0IScrEu5iimMT9YmTSQ56K86dVqJosImGq8ozRnAKCJ+oXjpApK ZD4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i6F7HLFg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h22si7572571ejl.384.2021.06.12.01.24.23; Sat, 12 Jun 2021 01:24:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i6F7HLFg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230474AbhFLIWu (ORCPT + 99 others); Sat, 12 Jun 2021 04:22:50 -0400 Received: from mail-lf1-f54.google.com ([209.85.167.54]:33513 "EHLO mail-lf1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229819AbhFLIWt (ORCPT ); Sat, 12 Jun 2021 04:22:49 -0400 Received: by mail-lf1-f54.google.com with SMTP id bp38so12271959lfb.0 for ; Sat, 12 Jun 2021 01:20:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Y2SpUEzOky83sMYBnEdAjJxCHRE/WrgH62z28bh+deo=; b=i6F7HLFg8E8xfiEjsnOSwSKOgq843AFv4vi8Lh8ZUieNfZqXbpIAuGmbfNRmdkzHWc wge+qCVyz0UVtAm8jQWQaLSyE5K/O1tzqVSDSQUzlkdLaUBBox+FlPZpUBGZJmWiNP8/ yfu63vXT8qHxVdAiKmJSsF0LAzIJ648thUndEQylNurKMqBnkescj0CioCbQW/v3XD0Y 1tlWRTYMw0ggYpvCcIH7YsAJ55gSfg1NcTtPyH9idknN3btNylxJmYUk2NHPswxR9I/Z bep/jf/ClilCaOAdh/WR35rIE9RfSpy6eXG42ZoIawDtEIdTAYX1BJI7940OjK+JDYbB xicA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Y2SpUEzOky83sMYBnEdAjJxCHRE/WrgH62z28bh+deo=; b=fQFMstMVhy3W3jnTdCZb24Z6WUTJ57WybiGAlQkpZp7UJ5Xke2RiPFKQp+pGmrB/2z VSty1ugU9LLkI+vWMt0bEo3BqKsn/yBKRkyOkkK84zUKB2Cj/K5uQH6AFr7+0YULtrBh G3b3fyphTR34m8Uz7XYhr0oxtfFQJpGTVQGfQEe0gBt/Czq4st8DszmieWiE6GCmFcPW noYRiEHMA+emxXHx7Cey53fIMOsdxZodTcBrdh0g0rkjM7du9kCeoSNmyOiHOQpdsEAM hl1kKmzB+8sTDzrwanuIRifTmR/eI9Zoa9f6gCbMuIwuotr579TRNCZaxxgUxEjlQbrK 2g2g== X-Gm-Message-State: AOAM531FP0FabhK5q8p3dOfUNI5wzVYzQMlKRv88RDgp/vSeB0GvwVUB U1KKUxzJNDAE4wj+lr6oudbOt2YZlUFUUTgh1uuv4Q== X-Received: by 2002:a19:8641:: with SMTP id i62mr5230170lfd.546.1623485989617; Sat, 12 Jun 2021 01:19:49 -0700 (PDT) MIME-Version: 1.0 References: <20210610210913.536081-1-tyhicks@linux.microsoft.com> <20210610210913.536081-7-tyhicks@linux.microsoft.com> <20210611130958.GR4910@sequoia> <20210611131619.GS4910@sequoia> In-Reply-To: <20210611131619.GS4910@sequoia> From: Sumit Garg Date: Sat, 12 Jun 2021 13:49:38 +0530 Message-ID: Subject: Re: [PATCH v4 6/8] tee: Support kernel shm registration without dma-buf backing To: Tyler Hicks Cc: Jens Wiklander , Allen Pais , Peter Huewe , Jarkko Sakkinen , Jason Gunthorpe , Vikas Gupta , Thirupathaiah Annapureddy , Pavel Tatashin , =?UTF-8?B?UmFmYcWCIE1pxYJlY2tp?= , op-tee@lists.trustedfirmware.org, linux-integrity , bcm-kernel-feedback-list@broadcom.com, linux-mips@vger.kernel.org, Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 11 Jun 2021 at 18:46, Tyler Hicks wrote: > > On 2021-06-11 08:10:01, Tyler Hicks wrote: > > On 2021-06-11 10:46:20, Sumit Garg wrote: > > > On Fri, 11 Jun 2021 at 02:39, Tyler Hicks wrote: > > > > > > > > Uncouple the registration of kernel shared memory buffers from the > > > > TEE_SHM_DMA_BUF flag. Drivers may wish to allocate multi-page contiguous > > > > shared memory regions but do not need them to be backed by a dma-buf > > > > when the memory region is only used by the driver. > > > > > > > > If the TEE implementation does not require shared memory to be > > > > registered, clear the flag prior to calling the corresponding pool alloc > > > > function. Update the OP-TEE driver to respect TEE_SHM_REGISTER, rather > > > > than TEE_SHM_DMA_BUF, when deciding whether to (un)register on > > > > alloc/free operations. > > > > > > > The AMD-TEE driver continues to ignore the > > > > TEE_SHM_REGISTER flag. > > > > > > > > > > That's the main point that no other TEE implementation would honour > > > TEE_SHM_REGISTER and I think it's just the incorrect usage of > > > TEE_SHM_REGISTER flag to suffice OP-TEE underlying implementation. > > > > > > > Allow callers of tee_shm_alloc_kernel_buf() to allocate and register a > > > > shared memory region without the backing of dma-buf. > > > > > > > > Signed-off-by: Tyler Hicks > > > > --- > > > > drivers/tee/optee/shm_pool.c | 5 ++--- > > > > drivers/tee/tee_shm.c | 13 +++++++++++-- > > > > 2 files changed, 13 insertions(+), 5 deletions(-) > > > > > > > > > > This patch is just mixing two separate approaches to TEE shared > > > memory. Have a look at alternative suggestions below. > > > > > > > diff --git a/drivers/tee/optee/shm_pool.c b/drivers/tee/optee/shm_pool.c > > > > index da06ce9b9313..6054343a29fb 100644 > > > > --- a/drivers/tee/optee/shm_pool.c > > > > +++ b/drivers/tee/optee/shm_pool.c > > > > @@ -27,7 +27,7 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, > > > > shm->paddr = page_to_phys(page); > > > > shm->size = PAGE_SIZE << order; > > > > > > > > - if (shm->flags & TEE_SHM_DMA_BUF) { > > > > + if (shm->flags & TEE_SHM_REGISTER) { > > > > > > Here you can just do following check instead: > > > > > > if (!(shm->flags & TEE_SHM_PRIV)) { > > > > This is a bug fix series that's intended to fix the current and older > > kernels. tee_shm_alloc_anon_kernel_buf()/TEE_SHM_PRIV is not present in > > older kernels and isn't required to fix these kexec/kdump bugs. Your > > suggestion feels like something that should be done in the allocator > > rewrite that Jens is working on to clean all of this up going forward. > > I want to add that I do fully agree with you that TEE_SHM_REGISTER is an > OP-TEE thing and not a TEE thing. Ideally, it wouldn't be defined in > tee_drv.h and would be completely private to the OP-TEE driver. > Likewise, I don't think that tee_shm_register() should exist (certainly > not at the TEE level) because it only works with OP-TEE. I think there is some confusion going on. tee_shm_register() is a standard interface that is listed in TEE client API specification as an alternative approach to tee_shm_alloc(). As I earlier mentioned, please read through "3.2.4. Shared Memory" in TEE Client API Specification. In the initial times, OP-TEE only supported tee_shm_alloc() approach but with the addition of dynamic shared memory feature it became possible to support tee_shm_register() as well but we had to add new capability as TEE_GEN_CAP_REG_MEM in order to maintain OP-TEE backwards compatibility. It can very well be the same case for AMD-TEE which currently only supports tee_shm_alloc() approach. The reason for confusion here seems to be that OP-TEE driver is providing a way to leverage dynamic shared memory approach in order to implement tee_shm_alloc() but that doesn't mean at TEE level we should intermix both approaches via using TEE_SHM_REGISTER to implement tee_shm_alloc(). > > That said, I think the first step is to fix the kexec/kdump bugs and the > second step is to clean up the code to remove the layering violation of > exposing shm registration from the TEE interfaces. > Doesn't the following patch sound suitable to be backported to a stable kernel? It has even less changes compared to your patch as well :). -Sumit ------------------------------------------- Subject: [PATCH] tee: Correct inappropriate usage of TEE_SHM_DMA_BUF flag Currently TEE_SHM_DMA_BUF flag has been inappropriately used to not register shared memory allocated for private usage by underlying TEE driver: OP-TEE in this case. So rather add a new flag as TEE_SHM_PRIV that can be utilized by underlying TEE drivers for private allocation and usage of shared memory. With this corrected, allow tee_shm_alloc_kernel_buf() to allocate a shared memory region without the backing of dma-buf. Signed-off-by: Sumit Garg --- drivers/tee/optee/call.c | 2 +- drivers/tee/optee/core.c | 3 ++- drivers/tee/optee/shm_pool.c | 8 ++++++-- drivers/tee/tee_shm.c | 2 +- include/linux/tee_drv.h | 1 + 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index 6132cc8d014c..faaa13c9172b 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -184,7 +184,7 @@ static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params, struct optee_msg_arg *ma; shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params), - TEE_SHM_MAPPED); + TEE_SHM_MAPPED | TEE_SHM_PRIV); if (IS_ERR(shm)) return shm; diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index ddb8f9ecf307..eac0e91ec559 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -277,7 +277,8 @@ static void optee_release(struct tee_context *ctx) if (!ctxdata) return; - shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED); + shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), + TEE_SHM_MAPPED | TEE_SHM_PRIV); if (!IS_ERR(shm)) { arg = tee_shm_get_va(shm, 0); /* diff --git a/drivers/tee/optee/shm_pool.c b/drivers/tee/optee/shm_pool.c index d767eebf30bd..3b4a3853a10f 100644 --- a/drivers/tee/optee/shm_pool.c +++ b/drivers/tee/optee/shm_pool.c @@ -27,7 +27,11 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, shm->paddr = page_to_phys(page); shm->size = PAGE_SIZE << order; - if (shm->flags & TEE_SHM_DMA_BUF) { + /* + * Shared memory private to the OP-TEE driver doesn't need + * to be registered with OP-TEE. + */ + if (!(shm->flags & TEE_SHM_PRIV)) { unsigned int nr_pages = 1 << order, i; struct page **pages; @@ -52,7 +56,7 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, static void pool_op_free(struct tee_shm_pool_mgr *poolm, struct tee_shm *shm) { - if (shm->flags & TEE_SHM_DMA_BUF) + if (!(shm->flags & TEE_SHM_PRIV)) optee_shm_unregister(shm->ctx, shm); free_pages((unsigned long)shm->kaddr, get_order(shm->size)); diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index c425ad80d6a6..f8b73e734094 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -207,7 +207,7 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc); */ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size) { - return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED); } EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 8990f7628387..3ebfea0781f1 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -27,6 +27,7 @@ #define TEE_SHM_USER_MAPPED BIT(4) /* Memory mapped in user space */ #define TEE_SHM_POOL BIT(5) /* Memory allocated from pool */ #define TEE_SHM_KERNEL_MAPPED BIT(6) /* Memory mapped in kernel space */ +#define TEE_SHM_PRIV BIT(7) /* Memory private to TEE driver */ struct device; struct tee_device;