Skip to content

Commit efa04fe

Browse files
johnstultz-worksumitsemwal
authored andcommitted
dma-buf: heaps: Add system heap to dmabuf heaps
This patch adds system heap to the dma-buf heaps framework. This allows applications to get a page-allocator backed dma-buf for non-contiguous memory. This code is an evolution of the Android ION implementation, so thanks to its original authors and maintainters: Rebecca Schultz Zavin, Colin Cross, Laura Abbott, and others! Cc: Laura Abbott <labbott@redhat.com> Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org> Cc: Sumit Semwal <sumit.semwal@linaro.org> Cc: Liam Mark <lmark@codeaurora.org> Cc: Pratik Patel <pratikp@codeaurora.org> Cc: Brian Starkey <Brian.Starkey@arm.com> Cc: Vincent Donnefort <Vincent.Donnefort@arm.com> Cc: Sudipto Paul <Sudipto.Paul@arm.com> Cc: Andrew F. Davis <afd@ti.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Chenbo Feng <fengc@google.com> Cc: Alistair Strachan <astrachan@google.com> Cc: Hridya Valsaraju <hridya@google.com> Cc: Sandeep Patil <sspatil@google.com> Cc: Hillf Danton <hdanton@sina.com> Cc: Dave Airlie <airlied@gmail.com> Cc: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard <benjamin.gaignard@linaro.org> Reviewed-by: Brian Starkey <brian.starkey@arm.com> Acked-by: Sandeep Patil <sspatil@android.com> Acked-by: Laura Abbott <labbott@redhat.com> Tested-by: Ayan Kumar Halder <ayan.halder@arm.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org> Link: https://patchwork.freedesktop.org/patch/msgid/20191203172641.66642-4-john.stultz@linaro.org
1 parent 5248eb1 commit efa04fe

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎drivers/dma-buf/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ menuconfig DMABUF_HEAPS
5353
allows userspace to allocate dma-bufs that can be shared
5454
between drivers.
5555

56+
source "drivers/dma-buf/heaps/Kconfig"
57+
5658
endmenu

‎drivers/dma-buf/heaps/Kconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
config DMABUF_HEAPS_SYSTEM
2+
bool "DMA-BUF System Heap"
3+
depends on DMABUF_HEAPS
4+
help
5+
Choose this option to enable the system dmabuf heap. The system heap
6+
is backed by pages from the buddy allocator. If in doubt, say Y.

‎drivers/dma-buf/heaps/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-y += heap-helpers.o
3+
obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) += system_heap.o
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* DMABUF System heap exporter
4+
*
5+
* Copyright (C) 2011 Google, Inc.
6+
* Copyright (C) 2019 Linaro Ltd.
7+
*/
8+
9+
#include <linux/dma-buf.h>
10+
#include <linux/dma-mapping.h>
11+
#include <linux/dma-heap.h>
12+
#include <linux/err.h>
13+
#include <linux/highmem.h>
14+
#include <linux/mm.h>
15+
#include <linux/module.h>
16+
#include <linux/scatterlist.h>
17+
#include <linux/slab.h>
18+
#include <linux/sched/signal.h>
19+
#include <asm/page.h>
20+
21+
#include "heap-helpers.h"
22+
23+
struct dma_heap *sys_heap;
24+
25+
static void system_heap_free(struct heap_helper_buffer *buffer)
26+
{
27+
pgoff_t pg;
28+
29+
for (pg = 0; pg < buffer->pagecount; pg++)
30+
__free_page(buffer->pages[pg]);
31+
kfree(buffer->pages);
32+
kfree(buffer);
33+
}
34+
35+
static int system_heap_allocate(struct dma_heap *heap,
36+
unsigned long len,
37+
unsigned long fd_flags,
38+
unsigned long heap_flags)
39+
{
40+
struct heap_helper_buffer *helper_buffer;
41+
struct dma_buf *dmabuf;
42+
int ret = -ENOMEM;
43+
pgoff_t pg;
44+
45+
helper_buffer = kzalloc(sizeof(*helper_buffer), GFP_KERNEL);
46+
if (!helper_buffer)
47+
return -ENOMEM;
48+
49+
init_heap_helper_buffer(helper_buffer, system_heap_free);
50+
helper_buffer->heap = heap;
51+
helper_buffer->size = len;
52+
53+
helper_buffer->pagecount = len / PAGE_SIZE;
54+
helper_buffer->pages = kmalloc_array(helper_buffer->pagecount,
55+
sizeof(*helper_buffer->pages),
56+
GFP_KERNEL);
57+
if (!helper_buffer->pages) {
58+
ret = -ENOMEM;
59+
goto err0;
60+
}
61+
62+
for (pg = 0; pg < helper_buffer->pagecount; pg++) {
63+
/*
64+
* Avoid trying to allocate memory if the process
65+
* has been killed by by SIGKILL
66+
*/
67+
if (fatal_signal_pending(current))
68+
goto err1;
69+
70+
helper_buffer->pages[pg] = alloc_page(GFP_KERNEL | __GFP_ZERO);
71+
if (!helper_buffer->pages[pg])
72+
goto err1;
73+
}
74+
75+
/* create the dmabuf */
76+
dmabuf = heap_helper_export_dmabuf(helper_buffer, fd_flags);
77+
if (IS_ERR(dmabuf)) {
78+
ret = PTR_ERR(dmabuf);
79+
goto err1;
80+
}
81+
82+
helper_buffer->dmabuf = dmabuf;
83+
84+
ret = dma_buf_fd(dmabuf, fd_flags);
85+
if (ret < 0) {
86+
dma_buf_put(dmabuf);
87+
/* just return, as put will call release and that will free */
88+
return ret;
89+
}
90+
91+
return ret;
92+
93+
err1:
94+
while (pg > 0)
95+
__free_page(helper_buffer->pages[--pg]);
96+
kfree(helper_buffer->pages);
97+
err0:
98+
kfree(helper_buffer);
99+
100+
return ret;
101+
}
102+
103+
static const struct dma_heap_ops system_heap_ops = {
104+
.allocate = system_heap_allocate,
105+
};
106+
107+
static int system_heap_create(void)
108+
{
109+
struct dma_heap_export_info exp_info;
110+
int ret = 0;
111+
112+
exp_info.name = "system_heap";
113+
exp_info.ops = &system_heap_ops;
114+
exp_info.priv = NULL;
115+
116+
sys_heap = dma_heap_add(&exp_info);
117+
if (IS_ERR(sys_heap))
118+
ret = PTR_ERR(sys_heap);
119+
120+
return ret;
121+
}
122+
module_init(system_heap_create);
123+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)