From: Jens Wiklander <jens.wiklander@linaro.org>
To: "T.J. Mercier" <tjmercier@google.com>
Cc: linux-kernel@vger.kernel.org, devicetree@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,
linux-mediatek@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>,
"Christian König" <christian.koenig@amd.com>,
"Sumit Garg" <sumit.garg@linaro.org>,
"Matthias Brugger" <matthias.bgg@gmail.com>,
"AngeloGioacchino Del Regno"
<angelogioacchino.delregno@collabora.com>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>
Subject: Re: [RFC PATCH 4/4] dma-buf: heaps: add Linaro restricted dmabuf heap support
Date: Wed, 4 Sep 2024 11:44:33 +0200 [thread overview]
Message-ID: <CAHUa44FYYFVQWf0DGUXNHoOVQEC0-HRyYa0386dHNjo4y1qSiQ@mail.gmail.com> (raw)
In-Reply-To: <CABdmKX2KzswmiDY4oWw69_rPWs8d_Cqp7OXouSeMQaYX1SDSmw@mail.gmail.com>
On Tue, Sep 3, 2024 at 7:50 PM T.J. Mercier <tjmercier@google.com> wrote:
>
> On Fri, Aug 30, 2024 at 12:04 AM Jens Wiklander
> <jens.wiklander@linaro.org> wrote:
> >
> > Add a Linaro restricted heap using the linaro,restricted-heap bindings
> > implemented based on the generic restricted heap.
> >
> > The bindings defines a range of physical restricted memory. The heap
> > manages this address range using genalloc. The allocated dma-buf file
> > descriptor can later be registered with the TEE subsystem for later use
> > via Trusted Applications in the secure world.
> >
> > Co-developed-by: Olivier Masse <olivier.masse@nxp.com>
> > Signed-off-by: Olivier Masse <olivier.masse@nxp.com>
> > Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
> > ---
> > drivers/dma-buf/heaps/Kconfig | 10 ++
> > drivers/dma-buf/heaps/Makefile | 1 +
> > .../dma-buf/heaps/restricted_heap_linaro.c | 165 ++++++++++++++++++
> > 3 files changed, 176 insertions(+)
> > create mode 100644 drivers/dma-buf/heaps/restricted_heap_linaro.c
> >
> > diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig
> > index 58903bc62ac8..82e2c5d09242 100644
> > --- a/drivers/dma-buf/heaps/Kconfig
> > +++ b/drivers/dma-buf/heaps/Kconfig
> > @@ -28,3 +28,13 @@ config DMABUF_HEAPS_RESTRICTED_MTK
> > help
> > Enable restricted dma-buf heaps for MediaTek platform. This heap is backed by
> > TEE client interfaces. If in doubt, say N.
> > +
> > +config DMABUF_HEAPS_RESTRICTED_LINARO
> > + bool "Linaro DMA-BUF Restricted Heap"
> > + depends on DMABUF_HEAPS_RESTRICTED
> > + help
> > + Choose this option to enable the Linaro restricted dma-buf heap.
> > + The restricted heap pools are defined according to the DT. Heaps
> > + are allocated in the pools using gen allocater.
> > + If in doubt, say N.
> > +
> > diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile
> > index 0028aa9d875f..66b2f67c47b5 100644
> > --- a/drivers/dma-buf/heaps/Makefile
> > +++ b/drivers/dma-buf/heaps/Makefile
> > @@ -2,4 +2,5 @@
> > obj-$(CONFIG_DMABUF_HEAPS_CMA) += cma_heap.o
> > obj-$(CONFIG_DMABUF_HEAPS_RESTRICTED) += restricted_heap.o
> > obj-$(CONFIG_DMABUF_HEAPS_RESTRICTED_MTK) += restricted_heap_mtk.o
> > +obj-$(CONFIG_DMABUF_HEAPS_RESTRICTED_LINARO) += restricted_heap_linaro.o
> > obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) += system_heap.o
> > diff --git a/drivers/dma-buf/heaps/restricted_heap_linaro.c b/drivers/dma-buf/heaps/restricted_heap_linaro.c
> > new file mode 100644
> > index 000000000000..4b08ed514023
> > --- /dev/null
> > +++ b/drivers/dma-buf/heaps/restricted_heap_linaro.c
> > @@ -0,0 +1,165 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * DMABUF secure heap exporter
> > + *
> > + * Copyright 2021 NXP.
> > + * Copyright 2024 Linaro Limited.
> > + */
> > +
> > +#define pr_fmt(fmt) "rheap_linaro: " fmt
> > +
> > +#include <linux/dma-buf.h>
> > +#include <linux/err.h>
> > +#include <linux/genalloc.h>
> > +#include <linux/module.h>
> > +#include <linux/of.h>
> > +#include <linux/of_fdt.h>
> > +#include <linux/of_reserved_mem.h>
> > +#include <linux/scatterlist.h>
> > +#include <linux/slab.h>
> > +
> > +#include "restricted_heap.h"
> > +
> > +#define MAX_HEAP_COUNT 2
>
> Are multiple supported because of what Cyrille mentioned here about permissions?
> https://lore.kernel.org/lkml/DBBPR04MB7514E006455AEA407041E4F788709@DBBPR04MB7514.eurprd04.prod.outlook.com/
Yes, I kept that as is.
>
> So this is just some arbitrary limit? I'd prefer to have some sort of
> documentation about this.
How about removing the limit and using dynamic allocation instead?
Thanks,
Jens
>
>
> > +#define HEAP_NAME_LEN 32
> > +
> > +struct resmem_restricted {
> > + phys_addr_t base;
> > + phys_addr_t size;
> > +
> > + char name[HEAP_NAME_LEN];
> > +
> > + bool no_map;
> > +};
> > +
> > +static struct resmem_restricted restricted_data[MAX_HEAP_COUNT] = {0};
> > +static unsigned int restricted_data_count;
> > +
> > +static int linaro_restricted_memory_allocate(struct restricted_heap *heap,
> > + struct restricted_buffer *buf)
> > +{
> > + struct gen_pool *pool = heap->priv_data;
> > + unsigned long pa;
> > + int ret;
> > +
> > + buf->size = ALIGN(buf->size, PAGE_SIZE);
> > + pa = gen_pool_alloc(pool, buf->size);
> > + if (!pa)
> > + return -ENOMEM;
> > +
> > + ret = sg_alloc_table(&buf->sg_table, 1, GFP_KERNEL);
> > + if (ret) {
> > + gen_pool_free(pool, pa, buf->size);
> > + return ret;
> > + }
> > +
> > + sg_set_page(buf->sg_table.sgl, phys_to_page(pa), buf->size, 0);
> > +
> > + return 0;
> > +}
> > +
> > +static void linaro_restricted_memory_free(struct restricted_heap *heap,
> > + struct restricted_buffer *buf)
> > +{
> > + struct gen_pool *pool = heap->priv_data;
> > + struct scatterlist *sg;
> > + unsigned int i;
> > +
> > + for_each_sg(buf->sg_table.sgl, sg, buf->sg_table.nents, i)
> > + gen_pool_free(pool, page_to_phys(sg_page(sg)), sg->length);
> > + sg_free_table(&buf->sg_table);
> > +}
> > +
> > +static const struct restricted_heap_ops linaro_restricted_heap_ops = {
> > + .alloc = linaro_restricted_memory_allocate,
> > + .free = linaro_restricted_memory_free,
> > +};
> > +
> > +static int add_heap(struct resmem_restricted *mem)
> > +{
> > + struct restricted_heap *heap;
> > + struct gen_pool *pool;
> > + int ret;
> > +
> > + if (mem->base == 0 || mem->size == 0) {
> > + pr_err("restricted_data base or size is not correct\n");
> > + return -EINVAL;
> > + }
> > +
> > + heap = kzalloc(sizeof(*heap), GFP_KERNEL);
> > + if (!heap)
> > + return -ENOMEM;
> > +
> > + pool = gen_pool_create(PAGE_SHIFT, -1);
> > + if (!pool) {
> > + ret = -ENOMEM;
> > + goto err_free_heap;
> > + }
> > +
> > + ret = gen_pool_add(pool, mem->base, mem->size, -1);
> > + if (ret)
> > + goto err_free_pool;
> > +
> > + heap->no_map = mem->no_map;
> > + heap->priv_data = pool;
> > + heap->name = mem->name;
> > + heap->ops = &linaro_restricted_heap_ops;
> > +
> > + ret = restricted_heap_add(heap);
> > + if (ret)
> > + goto err_free_pool;
> > +
> > + return 0;
> > +
> > +err_free_pool:
> > + gen_pool_destroy(pool);
> > +err_free_heap:
> > + kfree(heap);
> > +
> > + return ret;
> > +}
> > +
> > +static int __init rmem_restricted_heap_setup(struct reserved_mem *rmem)
> > +{
> > + size_t len = HEAP_NAME_LEN;
> > + const char *s;
> > + bool no_map;
> > +
> > + if (WARN_ONCE(restricted_data_count >= MAX_HEAP_COUNT,
> > + "Cannot handle more than %u restricted heaps\n",
> > + MAX_HEAP_COUNT))
> > + return -EINVAL;
> > +
> > + no_map = of_get_flat_dt_prop(rmem->fdt_node, "no-map", NULL);
> > + s = strchr(rmem->name, '@');
> > + if (s)
> > + len = umin(s - rmem->name + 1, len);
> > +
> > + restricted_data[restricted_data_count].base = rmem->base;
> > + restricted_data[restricted_data_count].size = rmem->size;
> > + restricted_data[restricted_data_count].no_map = no_map;
> > + strscpy(restricted_data[restricted_data_count].name, rmem->name, len);
> > +
> > + restricted_data_count++;
> > + return 0;
> > +}
> > +
> > +RESERVEDMEM_OF_DECLARE(linaro_restricted_heap, "linaro,restricted-heap",
> > + rmem_restricted_heap_setup);
> > +
> > +static int linaro_restricted_heap_init(void)
> > +{
> > + unsigned int i;
> > + int ret;
> > +
> > + for (i = 0; i < restricted_data_count; i++) {
> > + ret = add_heap(&restricted_data[i]);
> > + if (ret)
> > + return ret;
> > + }
> > + return 0;
> > +}
> > +
> > +module_init(linaro_restricted_heap_init);
> > +MODULE_DESCRIPTION("Linaro Restricted Heap Driver");
> > +MODULE_LICENSE("GPL");
> > --
> > 2.34.1
> >
next prev parent reply other threads:[~2024-09-04 10:17 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-30 7:03 [RFC PATCH 0/4] Linaro restricted heap Jens Wiklander
2024-08-30 7:03 ` [RFC PATCH 1/4] dma-buf: heaps: restricted_heap: add no_map attribute Jens Wiklander
2024-08-30 8:46 ` Christian König
2024-09-05 6:56 ` Jens Wiklander
2024-09-05 8:01 ` Christian König
2024-08-30 7:03 ` [RFC PATCH 2/4] tee: new ioctl to a register tee_shm from a dmabuf file descriptor Jens Wiklander
2024-09-03 17:49 ` T.J. Mercier
2024-09-04 9:49 ` Jens Wiklander
2024-08-30 7:03 ` [RFC PATCH 3/4] dt-bindings: reserved-memory: add linaro,restricted-heap Jens Wiklander
2024-08-30 8:20 ` Krzysztof Kozlowski
2024-08-30 8:42 ` Jens Wiklander
2024-08-30 7:03 ` [RFC PATCH 4/4] dma-buf: heaps: add Linaro restricted dmabuf heap support Jens Wiklander
2024-09-03 17:50 ` T.J. Mercier
2024-09-04 9:44 ` Jens Wiklander [this message]
2024-09-04 21:42 ` T.J. Mercier
2024-09-10 6:06 ` Jens Wiklander
2024-09-10 15:08 ` T.J. Mercier
2024-09-11 5:58 ` Jens Wiklander
2024-09-23 6:33 ` [RFC PATCH 0/4] Linaro restricted heap Dmitry Baryshkov
2024-09-24 18:13 ` Andrew Davis
2024-09-24 23:05 ` Dmitry Baryshkov
[not found] ` <e967e382-6cca-4dee-8333-39892d532f71@gmail.com>
2024-09-25 12:51 ` [Linaro-mm-sig] " Dmitry Baryshkov
[not found] ` <04caa788-19a6-4336-985c-4eb191c24438@amd.com>
[not found] ` <2f9a4abe-b2fc-4bc7-9926-1da2d38f5080@linaro.org>
2024-09-26 13:52 ` Sumit Garg
2024-09-26 14:02 ` Christian König
2024-09-27 6:16 ` Jens Wiklander
2024-09-27 19:50 ` Nicolas Dufresne
2024-09-30 6:47 ` Sumit Garg
2024-09-26 14:56 ` Andrew Davis
2024-09-25 7:58 ` Jens Wiklander
2024-09-25 7:15 ` Jens Wiklander
2024-09-25 11:41 ` Dmitry Baryshkov
2024-09-25 12:53 ` Jens Wiklander
2024-09-24 22:02 ` Daniel Stone
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=CAHUa44FYYFVQWf0DGUXNHoOVQEC0-HRyYa0386dHNjo4y1qSiQ@mail.gmail.com \
--to=jens.wiklander@linaro.org \
--cc=Brian.Starkey@arm.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=benjamin.gaignard@collabora.com \
--cc=christian.koenig@amd.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=jstultz@google.com \
--cc=krzk+dt@kernel.org \
--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=linux-mediatek@lists.infradead.org \
--cc=matthias.bgg@gmail.com \
--cc=olivier.masse@nxp.com \
--cc=op-tee@lists.trustedfirmware.org \
--cc=robh@kernel.org \
--cc=sumit.garg@linaro.org \
--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).