From: Sumit Garg <sumit.garg@kernel.org>
To: Jens Wiklander <jens.wiklander@linaro.org>
Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
op-tee@lists.trustedfirmware.org,
linux-arm-kernel@lists.infradead.org,
"Olivier Masse" <olivier.masse@nxp.com>,
"Thierry Reding" <thierry.reding@gmail.com>,
"Yong Wu" <yong.wu@mediatek.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Benjamin Gaignard" <benjamin.gaignard@collabora.com>,
"Brian Starkey" <Brian.Starkey@arm.com>,
"John Stultz" <jstultz@google.com>,
"T . J . Mercier" <tjmercier@google.com>,
"Christian König" <christian.koenig@amd.com>,
"Matthias Brugger" <matthias.bgg@gmail.com>,
"AngeloGioacchino Del Regno"
<angelogioacchino.delregno@collabora.com>,
azarrabi@qti.qualcomm.com,
"Simona Vetter" <simona.vetter@ffwll.ch>,
"Daniel Stone" <daniel@fooishbar.org>,
"Rouven Czerwinski" <rouven.czerwinski@linaro.org>
Subject: Re: [PATCH v9 6/9] tee: add tee_shm_alloc_dma_mem()
Date: Mon, 26 May 2025 15:03:21 +0530 [thread overview]
Message-ID: <aDQ1YR5jbcOFctty@sumit-X1> (raw)
In-Reply-To: <CAHUa44E4-z7-7DQjyGXYRiZDf6mRBKYZDuxWFGy+3JxtWO1ocg@mail.gmail.com>
On Mon, May 26, 2025 at 11:21:47AM +0200, Jens Wiklander wrote:
> On Mon, May 26, 2025 at 9:22 AM Sumit Garg <sumit.garg@kernel.org> wrote:
> >
> > On Tue, May 20, 2025 at 05:16:49PM +0200, Jens Wiklander wrote:
> > > Add tee_shm_alloc_dma_mem() to allocate DMA memory. The memory is
> > > represented by a tee_shm object using the new flag TEE_SHM_DMA_MEM to
> > > identify it as DMA memory. The allocated memory will later be lent to
> > > the TEE to be used as protected memory.
> > >
> > > Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
> > > ---
> > > drivers/tee/tee_shm.c | 74 ++++++++++++++++++++++++++++++++++++++--
> > > include/linux/tee_core.h | 5 +++
> > > 2 files changed, 77 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c
> > > index e1ed52ee0a16..92a6a35e1a1e 100644
> > > --- a/drivers/tee/tee_shm.c
> > > +++ b/drivers/tee/tee_shm.c
> > > @@ -5,6 +5,8 @@
> > > #include <linux/anon_inodes.h>
> > > #include <linux/device.h>
> > > #include <linux/dma-buf.h>
> > > +#include <linux/dma-mapping.h>
> > > +#include <linux/highmem.h>
> > > #include <linux/idr.h>
> > > #include <linux/io.h>
> > > #include <linux/mm.h>
> > > @@ -13,9 +15,14 @@
> > > #include <linux/tee_core.h>
> > > #include <linux/uaccess.h>
> > > #include <linux/uio.h>
> > > -#include <linux/highmem.h>
> > > #include "tee_private.h"
> > >
> > > +struct tee_shm_dma_mem {
> > > + struct tee_shm shm;
> > > + dma_addr_t dma_addr;
> > > + struct page *page;
> > > +};
> > > +
> > > static void shm_put_kernel_pages(struct page **pages, size_t page_count)
> > > {
> > > size_t n;
> > > @@ -49,7 +56,14 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
> > > struct tee_shm *parent_shm = NULL;
> > > void *p = shm;
> > >
> > > - if (shm->flags & TEE_SHM_DMA_BUF) {
> > > + if (shm->flags & TEE_SHM_DMA_MEM) {
> > > + struct tee_shm_dma_mem *dma_mem;
> > > +
> > > + dma_mem = container_of(shm, struct tee_shm_dma_mem, shm);
> > > + p = dma_mem;
> > > + dma_free_pages(&teedev->dev, shm->size, dma_mem->page,
> > > + dma_mem->dma_addr, DMA_BIDIRECTIONAL);
> >
> > Although the kernel bot already found a randconfig issue, it looks like
> > we need to add Kconfig dependencies like HAS_DMA, DMA_CMA etc.
> >
> > Also, I was thinking if we should rather add a new TEE subsystem
> > specific Kconfig option like: TEE_DMABUF_HEAPS which can then be used to
> > select whatever dependency is needed as well as act as a gating Kconfig
> > for relevant features.
>
> You mean something like this?
>
> --- a/drivers/tee/Kconfig
> +++ b/drivers/tee/Kconfig
> @@ -13,6 +13,14 @@ menuconfig TEE
>
> if TEE
>
> +config TEE_DMABUF_HEAPS
> + bool
> + depends on HAS_DMA && DMABUF_HEAPS
Yeah this looks fine to me but needs to be tested if DMA_CMA is a
dependency here too.
> +
> +config TEE_STATIC_PROTMEM_POOL
> + bool
> + depends on HAS_IOMEM && TEE_DMABUF_HEAPS
The static and dynamic protected memory pools should get auto enabled if
TEE_DMABUF_HEAPS is enabled since they are pre-requisite to provide the
protected heaps support. Something like:
+config TEE_STATIC_PROTMEM_POOL
+ bool
+ default y if TEE_DMABUF_HEAPS
+ depends on HAS_IOMEM
-Sumit
> +
>
> Cheers,
> Jens
>
> >
> > -Sumit
> >
> > > + } else if (shm->flags & TEE_SHM_DMA_BUF) {
> > > struct tee_shm_dmabuf_ref *ref;
> > >
> > > ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
> > > @@ -306,6 +320,62 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
> > > }
> > > EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
> > >
> > > +/**
> > > + * tee_shm_alloc_dma_mem() - Allocate DMA memory as shared memory object
> > > + * @ctx: Context that allocates the shared memory
> > > + * @page_count: Number of pages
> > > + *
> > > + * The allocated memory is expected to be lent (made inaccessible to the
> > > + * kernel) to the TEE while it's used and returned (accessible to the
> > > + * kernel again) before it's freed.
> > > + *
> > > + * This function should normally only be used internally in the TEE
> > > + * drivers.
> > > + *
> > > + * @returns a pointer to 'struct tee_shm'
> > > + */
> > > +struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx,
> > > + size_t page_count)
> > > +{
> > > + struct tee_device *teedev = ctx->teedev;
> > > + struct tee_shm_dma_mem *dma_mem;
> > > + dma_addr_t dma_addr;
> > > + struct page *page;
> > > +
> > > + if (!tee_device_get(teedev))
> > > + return ERR_PTR(-EINVAL);
> > > +
> > > + page = dma_alloc_pages(&teedev->dev, page_count * PAGE_SIZE,
> > > + &dma_addr, DMA_BIDIRECTIONAL, GFP_KERNEL);
> > > + if (!page)
> > > + goto err_put_teedev;
> > > +
> > > + dma_mem = kzalloc(sizeof(*dma_mem), GFP_KERNEL);
> > > + if (!dma_mem)
> > > + goto err_free_pages;
> > > +
> > > + refcount_set(&dma_mem->shm.refcount, 1);
> > > + dma_mem->shm.ctx = ctx;
> > > + dma_mem->shm.paddr = page_to_phys(page);
> > > + dma_mem->dma_addr = dma_addr;
> > > + dma_mem->page = page;
> > > + dma_mem->shm.size = page_count * PAGE_SIZE;
> > > + dma_mem->shm.flags = TEE_SHM_DMA_MEM;
> > > +
> > > + teedev_ctx_get(ctx);
> > > +
> > > + return &dma_mem->shm;
> > > +
> > > +err_free_pages:
> > > + dma_free_pages(&teedev->dev, page_count * PAGE_SIZE, page, dma_addr,
> > > + DMA_BIDIRECTIONAL);
> > > +err_put_teedev:
> > > + tee_device_put(teedev);
> > > +
> > > + return ERR_PTR(-ENOMEM);
> > > +}
> > > +EXPORT_SYMBOL_GPL(tee_shm_alloc_dma_mem);
> > > +
> > > int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
> > > int (*shm_register)(struct tee_context *ctx,
> > > struct tee_shm *shm,
> > > diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h
> > > index 02c07f661349..925690e1020b 100644
> > > --- a/include/linux/tee_core.h
> > > +++ b/include/linux/tee_core.h
> > > @@ -29,6 +29,8 @@
> > > #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */
> > > #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */
> > > #define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */
> > > +#define TEE_SHM_DMA_MEM BIT(5) /* Memory allocated with */
> > > + /* dma_alloc_pages() */
> > >
> > > #define TEE_DEVICE_FLAG_REGISTERED 0x1
> > > #define TEE_MAX_DEV_NAME_LEN 32
> > > @@ -310,6 +312,9 @@ void *tee_get_drvdata(struct tee_device *teedev);
> > > */
> > > struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
> > >
> > > +struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx,
> > > + size_t page_count);
> > > +
> > > int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
> > > int (*shm_register)(struct tee_context *ctx,
> > > struct tee_shm *shm,
> > > --
> > > 2.43.0
> > >
next prev parent reply other threads:[~2025-05-26 9:39 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-20 15:16 [PATCH v9 0/9] TEE subsystem for protected dma-buf allocations Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 1/9] optee: sync secure world ABI headers Jens Wiklander
2025-05-23 9:00 ` Sumit Garg
2025-05-20 15:16 ` [PATCH v9 2/9] dma-buf: dma-heap: export declared functions Jens Wiklander
2025-05-21 7:13 ` Christian König
2025-05-22 6:56 ` Jens Wiklander
2025-05-22 11:52 ` Christian König
2025-05-22 12:36 ` Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 3/9] tee: implement protected DMA-heap Jens Wiklander
2025-05-23 13:03 ` Sumit Garg
2025-05-26 7:11 ` Jens Wiklander
2025-05-30 2:13 ` Amirreza Zarrabi
2025-06-02 16:00 ` Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 4/9] tee: refactor params_from_user() Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 5/9] tee: new ioctl to a register tee_shm from a dmabuf file descriptor Jens Wiklander
2025-05-23 13:31 ` Sumit Garg
2025-05-26 8:34 ` Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 6/9] tee: add tee_shm_alloc_dma_mem() Jens Wiklander
2025-05-22 16:49 ` kernel test robot
2025-05-26 7:22 ` Sumit Garg
2025-05-26 9:21 ` Jens Wiklander
2025-05-26 9:33 ` Sumit Garg [this message]
2025-05-27 14:21 ` Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 7/9] optee: support protected memory allocation Jens Wiklander
2025-05-26 7:33 ` Sumit Garg
2025-05-27 14:32 ` Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 8/9] optee: FF-A: dynamic " Jens Wiklander
2025-05-26 8:09 ` Sumit Garg
2025-05-27 15:07 ` Jens Wiklander
2025-05-20 15:16 ` [PATCH v9 9/9] optee: smc abi: " Jens Wiklander
2025-05-26 8:13 ` Sumit Garg
2025-05-27 14:43 ` Jens Wiklander
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aDQ1YR5jbcOFctty@sumit-X1 \
--to=sumit.garg@kernel.org \
--cc=Brian.Starkey@arm.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=azarrabi@qti.qualcomm.com \
--cc=benjamin.gaignard@collabora.com \
--cc=christian.koenig@amd.com \
--cc=daniel@fooishbar.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=jens.wiklander@linaro.org \
--cc=jstultz@google.com \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=matthias.bgg@gmail.com \
--cc=olivier.masse@nxp.com \
--cc=op-tee@lists.trustedfirmware.org \
--cc=rouven.czerwinski@linaro.org \
--cc=simona.vetter@ffwll.ch \
--cc=sumit.semwal@linaro.org \
--cc=thierry.reding@gmail.com \
--cc=tjmercier@google.com \
--cc=yong.wu@mediatek.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).