[RFC,5/5,v2] kselftests: Add dma-heap test

Message ID 1551819273-640-6-git-send-email-john.stultz@linaro.org
State New
Headers show
Series
  • DMA-BUF Heaps (destaging ION)
Related show

Commit Message

John Stultz March 5, 2019, 8:54 p.m.
Add very trivial allocation test for dma-heaps.

TODO: Need to actually do some validation on
the returned dma-buf.

Cc: Laura Abbott <labbott@redhat.com>
Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Liam Mark <lmark@codeaurora.org>
Cc: Brian Starkey <Brian.Starkey@arm.com>
Cc: Andrew F. Davis <afd@ti.com>
Cc: Chenbo Feng <fengc@google.com>
Cc: Alistair Strachan <astrachan@google.com>
Cc: dri-devel@lists.freedesktop.org
Signed-off-by: John Stultz <john.stultz@linaro.org>

---
v2: Switched to use reworked dma-heap apis
---
 tools/testing/selftests/dmabuf-heaps/Makefile      | 11 +++
 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 96 ++++++++++++++++++++++
 2 files changed, 107 insertions(+)
 create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile
 create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

-- 
2.7.4

Comments

Benjamin Gaignard March 6, 2019, 4:14 p.m. | #1
Le mar. 5 mars 2019 à 21:54, John Stultz <john.stultz@linaro.org> a écrit :
>

> Add very trivial allocation test for dma-heaps.

>

> TODO: Need to actually do some validation on

> the returned dma-buf.

>

> Cc: Laura Abbott <labbott@redhat.com>

> Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>

> Cc: Greg KH <gregkh@linuxfoundation.org>

> Cc: Sumit Semwal <sumit.semwal@linaro.org>

> Cc: Liam Mark <lmark@codeaurora.org>

> Cc: Brian Starkey <Brian.Starkey@arm.com>

> Cc: Andrew F. Davis <afd@ti.com>

> Cc: Chenbo Feng <fengc@google.com>

> Cc: Alistair Strachan <astrachan@google.com>

> Cc: dri-devel@lists.freedesktop.org

> Signed-off-by: John Stultz <john.stultz@linaro.org>

> ---

> v2: Switched to use reworked dma-heap apis

> ---

>  tools/testing/selftests/dmabuf-heaps/Makefile      | 11 +++

>  tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 96 ++++++++++++++++++++++

>  2 files changed, 107 insertions(+)

>  create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile

>  create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

>

> diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile

> new file mode 100644

> index 0000000..c414ad3

> --- /dev/null

> +++ b/tools/testing/selftests/dmabuf-heaps/Makefile

> @@ -0,0 +1,11 @@

> +# SPDX-License-Identifier: GPL-2.0

> +CFLAGS += -static -O3 -Wl,-no-as-needed -Wall

> +#LDLIBS += -lrt -lpthread -lm

> +

> +# these are all "safe" tests that don't modify

> +# system time or require escalated privileges

> +TEST_GEN_PROGS = dmabuf-heap

> +

> +

> +include ../lib.mk

> +

> diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> new file mode 100644

> index 0000000..06837a4

> --- /dev/null

> +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> @@ -0,0 +1,96 @@

> +// SPDX-License-Identifier: GPL-2.0

> +

> +#include <dirent.h>

> +#include <errno.h>

> +#include <fcntl.h>

> +#include <stdio.h>

> +#include <string.h>

> +#include <unistd.h>

> +#include <sys/ioctl.h>

> +#include <sys/mman.h>

> +#include <sys/types.h>

> +

> +#include "../../../../include/uapi/linux/dma-heap.h"

> +

> +#define DEVPATH "/dev/dma_heap"

> +

> +int dmabuf_heap_open(char *name)

> +{

> +       int ret, fd;

> +       char buf[256];

> +

> +       ret = sprintf(buf, "%s/%s", DEVPATH, name);

> +       if (ret < 0) {

> +               printf("sprintf failed!\n");

> +               return ret;

> +       }

> +

> +       fd = open(buf, O_RDWR);

> +       if (fd < 0)

> +               printf("open %s failed!\n", buf);

> +       return fd;

> +}

> +

> +int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd)

> +{

> +       struct dma_heap_allocation_data data = {

> +               .len = len,

> +               .flags = flags,

> +       };

> +       int ret;

> +

> +       if (dmabuf_fd == NULL)

> +               return -EINVAL;

> +

> +       ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);

> +       if (ret < 0)

> +               return ret;

> +       *dmabuf_fd = (int)data.fd;

> +       return ret;

> +}

> +

> +#define ONE_MEG (1024*1024)

> +

> +void do_test(char *heap_name)

> +{

> +       int heap_fd = -1, dmabuf_fd = -1;

> +       int ret;

> +

> +       printf("Testing heap: %s\n", heap_name);

> +

> +       heap_fd = dmabuf_heap_open(heap_name);

> +       if (heap_fd < 0)

> +               return;

> +

> +       printf("Allocating 1 MEG\n");

> +       ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);

> +       if (ret)

> +               goto out;

> +

> +       /* DO SOMETHING WITH THE DMABUF HERE? */


You can do a call to mmap and write a pattern in the buffer.

Benjamin
> +

> +out:

> +       if (dmabuf_fd >= 0)

> +               close(dmabuf_fd);

> +       if (heap_fd >= 0)

> +               close(heap_fd);

> +}

> +

> +

> +int main(void)

> +{

> +       DIR *d;

> +       struct dirent *dir;

> +

> +       d = opendir(DEVPATH);

> +       if (!d) {

> +               printf("No %s directory?\n", DEVPATH);

> +               return -1;

> +       }

> +

> +       while ((dir = readdir(d)) != NULL)

> +               do_test(dir->d_name);

> +

> +

> +       return 0;

> +}

> --

> 2.7.4

>



-- 
Benjamin Gaignard

Graphic Study Group

Linaro.org │ Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog
John Stultz March 6, 2019, 5:01 p.m. | #2
On Wed, Mar 6, 2019 at 8:14 AM Benjamin Gaignard
<benjamin.gaignard@linaro.org> wrote:
> Le mar. 5 mars 2019 à 21:54, John Stultz <john.stultz@linaro.org> a écrit :

> > +

> > +       printf("Allocating 1 MEG\n");

> > +       ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);

> > +       if (ret)

> > +               goto out;

> > +

> > +       /* DO SOMETHING WITH THE DMABUF HERE? */

>

> You can do a call to mmap and write a pattern in the buffer.


Yea. I can also do some invalid allocations to make sure things fail properly.

But I was talking a bit w/ Sumit about the lack of any general dmabuf
tests, and am curious if we need to have a importer device driver that
can validate its a real dmabuf and exercise more of the dmabuf ops.

thanks
-john
John Stultz March 6, 2019, 6:19 p.m. | #3
On Wed, Mar 6, 2019 at 10:15 AM Andrew F. Davis <afd@ti.com> wrote:
>

> On 3/6/19 10:14 AM, Benjamin Gaignard wrote:

> > Le mar. 5 mars 2019 à 21:54, John Stultz <john.stultz@linaro.org> a écrit :

> >>

> >> Add very trivial allocation test for dma-heaps.

> >>

> >> TODO: Need to actually do some validation on

> >> the returned dma-buf.

> >>

> >> Cc: Laura Abbott <labbott@redhat.com>

> >> Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>

> >> Cc: Greg KH <gregkh@linuxfoundation.org>

> >> Cc: Sumit Semwal <sumit.semwal@linaro.org>

> >> Cc: Liam Mark <lmark@codeaurora.org>

> >> Cc: Brian Starkey <Brian.Starkey@arm.com>

> >> Cc: Andrew F. Davis <afd@ti.com>

> >> Cc: Chenbo Feng <fengc@google.com>

> >> Cc: Alistair Strachan <astrachan@google.com>

> >> Cc: dri-devel@lists.freedesktop.org

> >> Signed-off-by: John Stultz <john.stultz@linaro.org>

> >> ---

> >> v2: Switched to use reworked dma-heap apis

> >> ---

> >>  tools/testing/selftests/dmabuf-heaps/Makefile      | 11 +++

> >>  tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 96 ++++++++++++++++++++++

> >>  2 files changed, 107 insertions(+)

> >>  create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile

> >>  create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> >>

> >> diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile

> >> new file mode 100644

> >> index 0000000..c414ad3

> >> --- /dev/null

> >> +++ b/tools/testing/selftests/dmabuf-heaps/Makefile

> >> @@ -0,0 +1,11 @@

> >> +# SPDX-License-Identifier: GPL-2.0

> >> +CFLAGS += -static -O3 -Wl,-no-as-needed -Wall

> >> +#LDLIBS += -lrt -lpthread -lm

> >> +

> >> +# these are all "safe" tests that don't modify

> >> +# system time or require escalated privileges

> >> +TEST_GEN_PROGS = dmabuf-heap

> >> +

> >> +

> >> +include ../lib.mk

> >> +

> >> diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> >> new file mode 100644

> >> index 0000000..06837a4

> >> --- /dev/null

> >> +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> >> @@ -0,0 +1,96 @@

> >> +// SPDX-License-Identifier: GPL-2.0

> >> +

> >> +#include <dirent.h>

> >> +#include <errno.h>

> >> +#include <fcntl.h>

> >> +#include <stdio.h>

> >> +#include <string.h>

> >> +#include <unistd.h>

> >> +#include <sys/ioctl.h>

> >> +#include <sys/mman.h>

> >> +#include <sys/types.h>

> >> +

> >> +#include "../../../../include/uapi/linux/dma-heap.h"

> >> +

> >> +#define DEVPATH "/dev/dma_heap"

> >> +

> >> +int dmabuf_heap_open(char *name)

> >> +{

> >> +       int ret, fd;

> >> +       char buf[256];

> >> +

> >> +       ret = sprintf(buf, "%s/%s", DEVPATH, name);

> >> +       if (ret < 0) {

> >> +               printf("sprintf failed!\n");

> >> +               return ret;

> >> +       }

> >> +

> >> +       fd = open(buf, O_RDWR);

> >> +       if (fd < 0)

> >> +               printf("open %s failed!\n", buf);

> >> +       return fd;

> >> +}

> >> +

> >> +int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd)

> >> +{

> >> +       struct dma_heap_allocation_data data = {

> >> +               .len = len,

> >> +               .flags = flags,

> >> +       };

> >> +       int ret;

> >> +

> >> +       if (dmabuf_fd == NULL)

> >> +               return -EINVAL;

> >> +

> >> +       ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);

> >> +       if (ret < 0)

> >> +               return ret;

> >> +       *dmabuf_fd = (int)data.fd;

> >> +       return ret;

> >> +}

> >> +

> >> +#define ONE_MEG (1024*1024)

> >> +

> >> +void do_test(char *heap_name)

> >> +{

> >> +       int heap_fd = -1, dmabuf_fd = -1;

> >> +       int ret;

> >> +

> >> +       printf("Testing heap: %s\n", heap_name);

> >> +

> >> +       heap_fd = dmabuf_heap_open(heap_name);

> >> +       if (heap_fd < 0)

> >> +               return;

> >> +

> >> +       printf("Allocating 1 MEG\n");

> >> +       ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);

> >> +       if (ret)

> >> +               goto out;

> >> +

> >> +       /* DO SOMETHING WITH THE DMABUF HERE? */

> >

> > You can do a call to mmap and write a pattern in the buffer.

> >

>

> mmap is optional for DMA-BUFs, only attach/map are required. To test

> those we would need a dummy device, so a test kernel module may be

> needed to really exercise this.

>

> I have one I use for ION buffer testing, it consumes a DMA-BUF passed

> from userspace, attach/maps it to a dummy device then return the

> physical address of the first page of the buffer for validation. Might

> be a good test, but dummy devices don't always have the proper dma

> attributes set like a real device does, so it may also fail for some

> otherwise valid buffers.


Cool! Do you mind sharing that? I might try to rework and integrate it
into this patchset?

thanks
-john
Liam Mark March 13, 2019, 8:23 p.m. | #4
On Tue, 5 Mar 2019, John Stultz wrote:

> Add very trivial allocation test for dma-heaps.

> 

> TODO: Need to actually do some validation on

> the returned dma-buf.

> 

> Cc: Laura Abbott <labbott@redhat.com>

> Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>

> Cc: Greg KH <gregkh@linuxfoundation.org>

> Cc: Sumit Semwal <sumit.semwal@linaro.org>

> Cc: Liam Mark <lmark@codeaurora.org>

> Cc: Brian Starkey <Brian.Starkey@arm.com>

> Cc: Andrew F. Davis <afd@ti.com>

> Cc: Chenbo Feng <fengc@google.com>

> Cc: Alistair Strachan <astrachan@google.com>

> Cc: dri-devel@lists.freedesktop.org

> Signed-off-by: John Stultz <john.stultz@linaro.org>

> ---

> v2: Switched to use reworked dma-heap apis

> ---

>  tools/testing/selftests/dmabuf-heaps/Makefile      | 11 +++

>  tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 96 ++++++++++++++++++++++

>  2 files changed, 107 insertions(+)

>  create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile

>  create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> 

> diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile

> new file mode 100644

> index 0000000..c414ad3

> --- /dev/null

> +++ b/tools/testing/selftests/dmabuf-heaps/Makefile

> @@ -0,0 +1,11 @@

> +# SPDX-License-Identifier: GPL-2.0

> +CFLAGS += -static -O3 -Wl,-no-as-needed -Wall

> +#LDLIBS += -lrt -lpthread -lm

> +

> +# these are all "safe" tests that don't modify

> +# system time or require escalated privileges

> +TEST_GEN_PROGS = dmabuf-heap

> +

> +

> +include ../lib.mk

> +

> diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> new file mode 100644

> index 0000000..06837a4

> --- /dev/null

> +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c

> @@ -0,0 +1,96 @@

> +// SPDX-License-Identifier: GPL-2.0

> +

> +#include <dirent.h>

> +#include <errno.h>

> +#include <fcntl.h>

> +#include <stdio.h>

> +#include <string.h>

> +#include <unistd.h>

> +#include <sys/ioctl.h>

> +#include <sys/mman.h>

> +#include <sys/types.h>

> +

> +#include "../../../../include/uapi/linux/dma-heap.h"

> +

> +#define DEVPATH "/dev/dma_heap"

> +

> +int dmabuf_heap_open(char *name)

> +{

> +	int ret, fd;

> +	char buf[256];

> +

> +	ret = sprintf(buf, "%s/%s", DEVPATH, name);

> +	if (ret < 0) {

> +		printf("sprintf failed!\n");

> +		return ret;

> +	}

> +

> +	fd = open(buf, O_RDWR);

> +	if (fd < 0)

> +		printf("open %s failed!\n", buf);

> +	return fd;

> +}

> +

> +int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd)

> +{

> +	struct dma_heap_allocation_data data = {

> +		.len = len,

> +		.flags = flags,

> +	};

> +	int ret;

> +

> +	if (dmabuf_fd == NULL)

> +		return -EINVAL;

> +

> +	ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);

> +	if (ret < 0)

> +		return ret;

> +	*dmabuf_fd = (int)data.fd;

> +	return ret;

> +}

> +

> +#define ONE_MEG (1024*1024)

> +

> +void do_test(char *heap_name)

> +{

> +	int heap_fd = -1, dmabuf_fd = -1;

> +	int ret;

> +

> +	printf("Testing heap: %s\n", heap_name);

> +

> +	heap_fd = dmabuf_heap_open(heap_name);

> +	if (heap_fd < 0)

> +		return;

> +

> +	printf("Allocating 1 MEG\n");

> +	ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);


Just be aware that some CMA heaps may already have all their memory 
allocated by the client, so you may see intermittent failures depending on 
when you run the test and failrues may
be more common when run on certain platform.

> +	if (ret)

> +		goto out;

> +

> +	/* DO SOMETHING WITH THE DMABUF HERE? */

> +

> +out:

> +	if (dmabuf_fd >= 0)

> +		close(dmabuf_fd);

> +	if (heap_fd >= 0)

> +		close(heap_fd);

> +}

> +

> +

> +int main(void)

> +{

> +	DIR *d;

> +	struct dirent *dir;

> +

> +	d = opendir(DEVPATH);

> +	if (!d) {

> +		printf("No %s directory?\n", DEVPATH);

> +		return -1;

> +	}

> +

> +	while ((dir = readdir(d)) != NULL)

> +		do_test(dir->d_name);

> +

> +

> +	return 0;

> +}

> -- 

> 2.7.4

> 

> 


Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
John Stultz March 15, 2019, 8:13 p.m. | #5
On Fri, Mar 15, 2019 at 1:07 PM Laura Abbott <labbott@redhat.com> wrote:
>

> On 3/6/19 9:01 AM, John Stultz wrote:

> > On Wed, Mar 6, 2019 at 8:14 AM Benjamin Gaignard

> > <benjamin.gaignard@linaro.org> wrote:

> >> Le mar. 5 mars 2019 à 21:54, John Stultz <john.stultz@linaro.org> a écrit :

> >>> +

> >>> +       printf("Allocating 1 MEG\n");

> >>> +       ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);

> >>> +       if (ret)

> >>> +               goto out;

> >>> +

> >>> +       /* DO SOMETHING WITH THE DMABUF HERE? */

> >>

> >> You can do a call to mmap and write a pattern in the buffer.

> >

> > Yea. I can also do some invalid allocations to make sure things fail properly.

> >

> > But I was talking a bit w/ Sumit about the lack of any general dmabuf

> > tests, and am curious if we need to have a importer device driver that

> > can validate its a real dmabuf and exercise more of the dmabuf ops.

> >

> > thanks

> > -john

> >

>

> There's the vgem driver in drm. I did some work to clean that

> up so it could take an import af33a9190d02 ("drm/vgem: Enable dmabuf import

> interfaces") . I mostly used it for private tests and never ended

> up upstreaming any of the tests.


Thanks for the poitner, I'll check that out as well!  Also, if you
still have them around, I'd be interested in checking out the tests to
try to get something integrated into kselftest.

Talking with Brian yesterday, there was some thought that we should
try to put together some sort of example dmabuf pipeline that isn't
hardware dependent and can be used to demonstrate the usage model as
well as validate the frameworks and maybe even benchmark some of the
ideas floating around right now.  So suggestions here would be
welcome!

thanks
-john
Laura Abbott March 15, 2019, 8:49 p.m. | #6
On 3/15/19 1:13 PM, John Stultz wrote:
> On Fri, Mar 15, 2019 at 1:07 PM Laura Abbott <labbott@redhat.com> wrote:

>>

>> On 3/6/19 9:01 AM, John Stultz wrote:

>>> On Wed, Mar 6, 2019 at 8:14 AM Benjamin Gaignard

>>> <benjamin.gaignard@linaro.org> wrote:

>>>> Le mar. 5 mars 2019 à 21:54, John Stultz <john.stultz@linaro.org> a écrit :

>>>>> +

>>>>> +       printf("Allocating 1 MEG\n");

>>>>> +       ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);

>>>>> +       if (ret)

>>>>> +               goto out;

>>>>> +

>>>>> +       /* DO SOMETHING WITH THE DMABUF HERE? */

>>>>

>>>> You can do a call to mmap and write a pattern in the buffer.

>>>

>>> Yea. I can also do some invalid allocations to make sure things fail properly.

>>>

>>> But I was talking a bit w/ Sumit about the lack of any general dmabuf

>>> tests, and am curious if we need to have a importer device driver that

>>> can validate its a real dmabuf and exercise more of the dmabuf ops.

>>>

>>> thanks

>>> -john

>>>

>>

>> There's the vgem driver in drm. I did some work to clean that

>> up so it could take an import af33a9190d02 ("drm/vgem: Enable dmabuf import

>> interfaces") . I mostly used it for private tests and never ended

>> up upstreaming any of the tests.

> 

> Thanks for the poitner, I'll check that out as well!  Also, if you

> still have them around, I'd be interested in checking out the tests to

> try to get something integrated into kselftest.

> 

> Talking with Brian yesterday, there was some thought that we should

> try to put together some sort of example dmabuf pipeline that isn't

> hardware dependent and can be used to demonstrate the usage model as

> well as validate the frameworks and maybe even benchmark some of the

> ideas floating around right now.  So suggestions here would be

> welcome!

> 


So the existing ion selftest (tools/testing/selftests/android/ion)
does make use of the import to do some very simple tests.
I can't seem to find the more complex tests I had though
they may have been lost during my last machine move :(

I do think building off of vgem would be a good first step
for a testing pipeline, although I worry we wouldn't be
able to measure caching effects without a real device since
setting up coherency testing otherwise seems error prone.

Thanks,
Laura

> thanks

> -john

>

Patch

diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile
new file mode 100644
index 0000000..c414ad3
--- /dev/null
+++ b/tools/testing/selftests/dmabuf-heaps/Makefile
@@ -0,0 +1,11 @@ 
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -static -O3 -Wl,-no-as-needed -Wall
+#LDLIBS += -lrt -lpthread -lm
+
+# these are all "safe" tests that don't modify
+# system time or require escalated privileges
+TEST_GEN_PROGS = dmabuf-heap
+
+
+include ../lib.mk
+
diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
new file mode 100644
index 0000000..06837a4
--- /dev/null
+++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
@@ -0,0 +1,96 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "../../../../include/uapi/linux/dma-heap.h"
+
+#define DEVPATH "/dev/dma_heap"
+
+int dmabuf_heap_open(char *name)
+{
+	int ret, fd;
+	char buf[256];
+
+	ret = sprintf(buf, "%s/%s", DEVPATH, name);
+	if (ret < 0) {
+		printf("sprintf failed!\n");
+		return ret;
+	}
+
+	fd = open(buf, O_RDWR);
+	if (fd < 0)
+		printf("open %s failed!\n", buf);
+	return fd;
+}
+
+int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd)
+{
+	struct dma_heap_allocation_data data = {
+		.len = len,
+		.flags = flags,
+	};
+	int ret;
+
+	if (dmabuf_fd == NULL)
+		return -EINVAL;
+
+	ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);
+	if (ret < 0)
+		return ret;
+	*dmabuf_fd = (int)data.fd;
+	return ret;
+}
+
+#define ONE_MEG (1024*1024)
+
+void do_test(char *heap_name)
+{
+	int heap_fd = -1, dmabuf_fd = -1;
+	int ret;
+
+	printf("Testing heap: %s\n", heap_name);
+
+	heap_fd = dmabuf_heap_open(heap_name);
+	if (heap_fd < 0)
+		return;
+
+	printf("Allocating 1 MEG\n");
+	ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
+	if (ret)
+		goto out;
+
+	/* DO SOMETHING WITH THE DMABUF HERE? */
+
+out:
+	if (dmabuf_fd >= 0)
+		close(dmabuf_fd);
+	if (heap_fd >= 0)
+		close(heap_fd);
+}
+
+
+int main(void)
+{
+	DIR *d;
+	struct dirent *dir;
+
+	d = opendir(DEVPATH);
+	if (!d) {
+		printf("No %s directory?\n", DEVPATH);
+		return -1;
+	}
+
+	while ((dir = readdir(d)) != NULL)
+		do_test(dir->d_name);
+
+
+	return 0;
+}