[PATCH v5 5/9] dma-buf: heaps: restricted_heap: Add private heap ops
Yong Wu
yong.wu at mediatek.com
Wed May 15 04:23:04 PDT 2024
Add "struct restricted_heap_ops". For the restricted memory, totally there
are two steps:
a) alloc: Allocate the buffer in kernel;
b) restrict_buf: Restrict/Protect/Secure that buffer.
The "alloc" is mandatory while "restrict_buf" is optional since it may
be part of "alloc".
Signed-off-by: Yong Wu <yong.wu at mediatek.com>
---
drivers/dma-buf/heaps/restricted_heap.c | 41 ++++++++++++++++++++++++-
drivers/dma-buf/heaps/restricted_heap.h | 12 ++++++++
2 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/drivers/dma-buf/heaps/restricted_heap.c b/drivers/dma-buf/heaps/restricted_heap.c
index c2ae19ba7d7e..8bb3c1876a69 100644
--- a/drivers/dma-buf/heaps/restricted_heap.c
+++ b/drivers/dma-buf/heaps/restricted_heap.c
@@ -12,10 +12,44 @@
#include "restricted_heap.h"
+static int
+restricted_heap_memory_allocate(struct restricted_heap *rheap, struct restricted_buffer *buf)
+{
+ const struct restricted_heap_ops *ops = rheap->ops;
+ int ret;
+
+ ret = ops->alloc(rheap, buf);
+ if (ret)
+ return ret;
+
+ if (ops->restrict_buf) {
+ ret = ops->restrict_buf(rheap, buf);
+ if (ret)
+ goto buf_free;
+ }
+ return 0;
+
+buf_free:
+ ops->free(rheap, buf);
+ return ret;
+}
+
+static void
+restricted_heap_memory_free(struct restricted_heap *rheap, struct restricted_buffer *buf)
+{
+ const struct restricted_heap_ops *ops = rheap->ops;
+
+ if (ops->unrestrict_buf)
+ ops->unrestrict_buf(rheap, buf);
+
+ ops->free(rheap, buf);
+}
+
static struct dma_buf *
restricted_heap_allocate(struct dma_heap *heap, unsigned long size,
unsigned long fd_flags, unsigned long heap_flags)
{
+ struct restricted_heap *rheap = dma_heap_get_drvdata(heap);
struct restricted_buffer *restricted_buf;
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
struct dma_buf *dmabuf;
@@ -28,6 +62,9 @@ restricted_heap_allocate(struct dma_heap *heap, unsigned long size,
restricted_buf->size = ALIGN(size, PAGE_SIZE);
restricted_buf->heap = heap;
+ ret = restricted_heap_memory_allocate(rheap, restricted_buf);
+ if (ret)
+ goto err_free_buf;
exp_info.exp_name = dma_heap_get_name(heap);
exp_info.size = restricted_buf->size;
exp_info.flags = fd_flags;
@@ -36,11 +73,13 @@ restricted_heap_allocate(struct dma_heap *heap, unsigned long size,
dmabuf = dma_buf_export(&exp_info);
if (IS_ERR(dmabuf)) {
ret = PTR_ERR(dmabuf);
- goto err_free_buf;
+ goto err_free_rstrd_mem;
}
return dmabuf;
+err_free_rstrd_mem:
+ restricted_heap_memory_free(rheap, restricted_buf);
err_free_buf:
kfree(restricted_buf);
return ERR_PTR(ret);
diff --git a/drivers/dma-buf/heaps/restricted_heap.h b/drivers/dma-buf/heaps/restricted_heap.h
index b448f77616ac..5783275d5714 100644
--- a/drivers/dma-buf/heaps/restricted_heap.h
+++ b/drivers/dma-buf/heaps/restricted_heap.h
@@ -15,6 +15,18 @@ struct restricted_buffer {
struct restricted_heap {
const char *name;
+
+ const struct restricted_heap_ops *ops;
+};
+
+struct restricted_heap_ops {
+ int (*heap_init)(struct restricted_heap *rheap);
+
+ int (*alloc)(struct restricted_heap *rheap, struct restricted_buffer *buf);
+ void (*free)(struct restricted_heap *rheap, struct restricted_buffer *buf);
+
+ int (*restrict_buf)(struct restricted_heap *rheap, struct restricted_buffer *buf);
+ void (*unrestrict_buf)(struct restricted_heap *rheap, struct restricted_buffer *buf);
};
int restricted_heap_add(struct restricted_heap *rheap);
--
2.25.1
More information about the Linux-mediatek
mailing list