Skip to contentSkip to conversation

selftests/dmabuf-heap: conform test to TAP format output

PATCH Superseded Muhammad Usama Anjum 2 participants 5 messages Feb 26, 2024 – Feb 27, 2024

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 selftests/dmabuf-heap: conform test to TAP format output

Conform the layout, informational and status messages to TAP. No functional change is intended other than the layout of output messages.

---
 .../selftests/dmabuf-heaps/dmabuf-heap.c      | 194 +++++++-----------
 1 file changed, 77 insertions(+), 117 deletions(-)
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +77 -117
index 890a8236a8ba7..6e538e346cb8f 100644
@@ -15,6 +15,7 @@
 #include <linux/dma-buf.h>
 #include <linux/dma-heap.h>
 #include <drm/drm.h>
+#include "../kselftest.h"
 
 #define DEVPATH "/dev/dma_heap"
 
@@ -90,14 +91,13 @@ static int dmabuf_heap_open(char *name)
 	char buf[256];
 
 	ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
-	if (ret < 0) {
-		printf("snprintf failed!\n");
-		return ret;
-	}
+	if (ret < 0)
+		ksft_exit_fail_msg("snprintf failed!\n");
 
 	fd = open(buf, O_RDWR);
 	if (fd < 0)
-		printf("open %s failed!\n", buf);
+		ksft_exit_fail_msg("open %s failed: %s\n", buf, strerror(errno));
+
 	return fd;
 }
 
@@ -140,7 +140,7 @@ static int dmabuf_sync(int fd, int start_stop)
 
 #define ONE_MEG (1024 * 1024)
 
-static int test_alloc_and_import(char *heap_name)
+static void test_alloc_and_import(char *heap_name)
 {
 	int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
 	uint32_t handle = 0;
@@ -148,16 +148,12 @@ static int test_alloc_and_import(char *heap_name)
 	int ret;
 
 	heap_fd = dmabuf_heap_open(heap_name);
-	if (heap_fd < 0)
-		return -1;
 
-	printf("  Testing allocation and importing:  ");
+	ksft_print_msg("Testing allocation and importing:\n");
 	ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-	if (ret) {
-		printf("FAIL (Allocation Failed!)\n");
-		ret = -1;
-		goto out;
-	}
+	if (ret)
+		ksft_exit_fail_msg("FAIL (Allocation Failed!)\n");
+
 	/* mmap and write a simple pattern */
 	p = mmap(NULL,
 		 ONE_MEG,
@@ -166,7 +162,7 @@ static int test_alloc_and_import(char *heap_name)
 		 dmabuf_fd,
 		 0);
 	if (p == MAP_FAILED) {
-		printf("FAIL (mmap() failed)\n");
+		ksft_print_msg("FAIL (mmap() failed)\n");
 		ret = -1;
 		goto out;
 	}
@@ -179,31 +175,28 @@ static int test_alloc_and_import(char *heap_name)
 	importer_fd = open_vgem();
 	if (importer_fd < 0) {
 		ret = importer_fd;
-		printf("(Could not open vgem - skipping):  ");
+		ksft_test_result_skip("Could not open vgem\n");
 	} else {
 		ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle);
-		if (ret < 0) {
-			printf("FAIL (Failed to import buffer)\n");
-			goto out;
-		}
+		ksft_test_result(ret >= 0, "Import buffer\n");
 	}
 
 	ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
 	if (ret < 0) {
-		printf("FAIL (DMA_BUF_SYNC_START failed!)\n");
+		ksft_print_msg("FAIL (DMA_BUF_SYNC_START failed!)\n");
 		goto out;
 	}
 
 	memset(p, 0xff, ONE_MEG);
 	ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
 	if (ret < 0) {
-		printf("FAIL (DMA_BUF_SYNC_END failed!)\n");
+		ksft_print_msg("FAIL (DMA_BUF_SYNC_END failed!)\n");
 		goto out;
 	}
 
 	close_handle(importer_fd, handle);
-	ret = 0;
-	printf(" OK\n");
+	ksft_test_result_pass("%s\n", __func__);
+	return;
 out:
 	if (p)
 		munmap(p, ONE_MEG);
@@ -214,32 +207,29 @@ static int test_alloc_and_import(char *heap_name)
 	if (heap_fd >= 0)
 		close(heap_fd);
 
-	return ret;
+	ksft_test_result_fail("%s\n", __func__);
 }
 
-static int test_alloc_zeroed(char *heap_name, size_t size)
+static void test_alloc_zeroed(char *heap_name, size_t size)
 {
 	int heap_fd = -1, dmabuf_fd[32];
 	int i, j, ret;
 	void *p = NULL;
 	char *c;
 
-	printf("  Testing alloced %ldk buffers are zeroed:  ", size / 1024);
+	ksft_print_msg("Testing alloced %ldk buffers are zeroed:\n", size / 1024);
 	heap_fd = dmabuf_heap_open(heap_name);
-	if (heap_fd < 0)
-		return -1;
 
 	/* Allocate and fill a bunch of buffers */
 	for (i = 0; i < 32; i++) {
 		ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
-		if (ret < 0) {
-			printf("FAIL (Allocation (%i) failed)\n", i);
-			goto out;
-		}
+		if (ret)
+			ksft_exit_fail_msg("FAIL (Allocation (%i) failed)\n", i);
+
 		/* mmap and fill with simple pattern */
 		p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
 		if (p == MAP_FAILED) {
-			printf("FAIL (mmap() failed!)\n");
+			ksft_print_msg("FAIL (mmap() failed!)\n");
 			ret = -1;
 			goto out;
 		}
@@ -255,15 +245,13 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
 	/* Allocate and validate all buffers are zeroed */
 	for (i = 0; i < 32; i++) {
 		ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
-		if (ret < 0) {
-			printf("FAIL (Allocation (%i) failed)\n", i);
-			goto out;
-		}
+		if (ret < 0)
+			ksft_exit_fail_msg("FAIL (Allocation (%i) failed)\n", i);
 
 		/* mmap and validate everything is zero */
 		p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
 		if (p == MAP_FAILED) {
-			printf("FAIL (mmap() failed!)\n");
+			ksft_print_msg("FAIL (mmap() failed!)\n");
 			ret = -1;
 			goto out;
 		}
@@ -271,7 +259,7 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
 		c = (char *)p;
 		for (j = 0; j < size; j++) {
 			if (c[j] != 0) {
-				printf("FAIL (Allocated buffer not zeroed @ %i)\n", j);
+				ksft_print_msg("FAIL (Allocated buffer not zeroed @ %i)\n", j);
 				break;
 			}
 		}
@@ -283,8 +271,8 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
 		close(dmabuf_fd[i]);
 
 	close(heap_fd);
-	printf("OK\n");
-	return 0;
+	ksft_test_result_pass("%s\n", __func__);
+	return;
 
 out:
 	while (i > 0) {
@@ -292,7 +280,7 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
 		i--;
 	}
 	close(heap_fd);
-	return ret;
+	ksft_test_result_fail("%s\n", __func__);
 }
 
 /* Test the ioctl version compatibility w/ a smaller structure then expected */
@@ -360,126 +348,98 @@ static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
 	return ret;
 }
 
-static int test_alloc_compat(char *heap_name)
+static void test_alloc_compat(char *heap_name)
 {
-	int heap_fd = -1, dmabuf_fd = -1;
-	int ret;
+	int ret, heap_fd = -1, dmabuf_fd = -1;
 
 	heap_fd = dmabuf_heap_open(heap_name);
-	if (heap_fd < 0)
-		return -1;
 
-	printf("  Testing (theoretical)older alloc compat:  ");
+	ksft_print_msg("Testing (theoretical)older alloc compat:\n");
 	ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-	if (ret) {
-		printf("FAIL (Older compat allocation failed!)\n");
-		ret = -1;
-		goto out;
-	}
-	close(dmabuf_fd);
-	printf("OK\n");
+	if (dmabuf_fd >= 0)
+		close(dmabuf_fd);
+	ksft_test_result(!ret, "dmabuf_heap_alloc_older\n");
 
-	printf("  Testing (theoretical)newer alloc compat:  ");
+	ksft_print_msg("Testing (theoretical)newer alloc compat:\n");
 	ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-	if (ret) {
-		printf("FAIL (Newer compat allocation failed!)\n");
-		ret = -1;
-		goto out;
-	}
-	printf("OK\n");
-out:
 	if (dmabuf_fd >= 0)
 		close(dmabuf_fd);
-	if (heap_fd >= 0)
-		close(heap_fd);
+	ksft_test_result(!ret, "dmabuf_heap_alloc_newer\n");
 
-	return ret;
+	close(heap_fd);
 }
 
-static int test_alloc_errors(char *heap_name)
+static void test_alloc_errors(char *heap_name)
 {
 	int heap_fd = -1, dmabuf_fd = -1;
 	int ret;
 
 	heap_fd = dmabuf_heap_open(heap_name);
-	if (heap_fd < 0)
-		return -1;
 
-	printf("  Testing expected error cases:  ");
+	ksft_print_msg("Testing expected error cases:\n");
 	ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
-	if (!ret) {
-		printf("FAIL (Did not see expected error (invalid fd)!)\n");
-		ret = -1;
-		goto out;
-	}
+	ksft_test_result(ret, "Error expected on invalid fd\n");
 
 	ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd);
-	if (!ret) {
-		printf("FAIL (Did not see expected error (invalid heap flags)!)\n");
-		ret = -1;
-		goto out;
-	}
+	ksft_test_result(ret, "Error expected on invalid heap flags\n");
 
 	ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG,
 					~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd);
-	if (!ret) {
-		printf("FAIL (Did not see expected error (invalid fd flags)!)\n");
-		ret = -1;
-		goto out;
-	}
+	ksft_test_result(ret, "Error expected on invalid heap flags\n");
 
-	printf("OK\n");
-	ret = 0;
-out:
 	if (dmabuf_fd >= 0)
 		close(dmabuf_fd);
 	if (heap_fd >= 0)
 		close(heap_fd);
+}
 
-	return ret;
+static int numer_of_heaps(void)
+{
+	DIR *d = opendir(DEVPATH);
+	struct dirent *dir;
+	int heaps = 0;
+
+	while ((dir = readdir(d))) {
+		if (!strncmp(dir->d_name, ".", 2))
+			continue;
+		if (!strncmp(dir->d_name, "..", 3))
+			continue;
+		heaps++;
+	}
+
+	return heaps;
 }
 
 int main(void)
 {
-	DIR *d;
 	struct dirent *dir;
-	int ret = -1;
+	DIR *d;
+
+	ksft_print_header();
 
 	d = opendir(DEVPATH);
 	if (!d) {
-		printf("No %s directory?\n", DEVPATH);
-		return -1;
+		ksft_print_msg("No %s directory?\n", DEVPATH);
+		return KSFT_SKIP;
 	}
 
-	while ((dir = readdir(d)) != NULL) {
+	ksft_set_plan(9 * numer_of_heaps());
+
+	while ((dir = readdir(d))) {
 		if (!strncmp(dir->d_name, ".", 2))
 			continue;
 		if (!strncmp(dir->d_name, "..", 3))
 			continue;
 
-		printf("Testing heap: %s\n", dir->d_name);
-		printf("=======================================\n");
-		ret = test_alloc_and_import(dir->d_name);
-		if (ret)
-			break;
-
-		ret = test_alloc_zeroed(dir->d_name, 4 * 1024);
-		if (ret)
-			break;
-
-		ret = test_alloc_zeroed(dir->d_name, ONE_MEG);
-		if (ret)
-			break;
-
-		ret = test_alloc_compat(dir->d_name);
-		if (ret)
-			break;
-
-		ret = test_alloc_errors(dir->d_name);
-		if (ret)
-			break;
+		ksft_print_msg("Testing heap: %s\n", dir->d_name);
+		ksft_print_msg("=======================================\n");
+		test_alloc_and_import(dir->d_name);
+		test_alloc_zeroed(dir->d_name, 4 * 1024);
+		test_alloc_zeroed(dir->d_name, ONE_MEG);
+		test_alloc_compat(dir->d_name);
+		test_alloc_errors(dir->d_name);
 	}
 	closedir(d);
 
-	return ret;
+	ksft_finished();
 }
Signature · 1 line
2.42.0
#1 lore raw

PATCH 2/2 selftests: dmabuf-heap: add config file for the test

The config fragment enlists all the config options needed for the test. This config is merged into the kernel's config on which this test is run.

---
 tools/testing/selftests/dmabuf-heaps/config | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 tools/testing/selftests/dmabuf-heaps/config
tools/testing/selftests/dmabuf-heaps/config new file +4 -0
new file mode 100644
index 0000000000000..34bd294836df0
@@ -0,0 +1,4 @@
+CONFIG_DMABUF_HEAPS=y
+CONFIG_DMABUF_HEAPS_SYSTEM=y
+CONFIG_DRM_VGEM=y
+
Signature · 1 line
2.42.0
reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] selftests: dmabuf-heap: add config file for the test

Quoted 22 lines · Muhammad Usama Anjum

The config fragment enlists all the config options needed for the test. This config is merged into the kernel's config on which this test is run.

---
 tools/testing/selftests/dmabuf-heaps/config | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 tools/testing/selftests/dmabuf-heaps/config
tools/testing/selftests/dmabuf-heaps/config new file +4 -1
new file mode 100644
index 0000000000000..34bd294836df0
@@ -0,0 +1,4 @@
+CONFIG_DMABUF_HEAPS=y
+CONFIG_DMABUF_HEAPS_SYSTEM=y
+CONFIG_DRM_VGEM=y
+
--
Quoted 1 line

2.42.0

reviewer
#3 lore raw
In reply to Muhammad Usama Anjum
Quoted 71 lines · Muhammad Usama Anjum

Conform the layout, informational and status messages to TAP. No functional change is intended other than the layout of output messages.

---
 .../selftests/dmabuf-heaps/dmabuf-heap.c      | 194 +++++++-----------
 1 file changed, 77 insertions(+), 117 deletions(-)
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +10 -14
index 890a8236a8ba7..6e538e346cb8f 100644
@@ -15,6 +15,7 @@
 #include <linux/dma-buf.h>
 #include <linux/dma-heap.h>
 #include <drm/drm.h>
+#include "../kselftest.h"
 
 #define DEVPATH "/dev/dma_heap"
 
@@ -90,14 +91,13 @@ static int dmabuf_heap_open(char *name)
        char buf[256];
 
        ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
-       if (ret < 0) {
-               printf("snprintf failed!\n");
-               return ret;
-       }
+       if (ret < 0)
+               ksft_exit_fail_msg("snprintf failed!\n");
 
        fd = open(buf, O_RDWR);
        if (fd < 0)
-               printf("open %s failed!\n", buf);
+               ksft_exit_fail_msg("open %s failed: %s\n", buf, strerror(errno));
+
        return fd;
 }
 
@@ -140,7 +140,7 @@ static int dmabuf_sync(int fd, int start_stop)
 
 #define ONE_MEG (1024 * 1024)
 
-static int test_alloc_and_import(char *heap_name)
+static void test_alloc_and_import(char *heap_name)
 {
        int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
        uint32_t handle = 0;
@@ -148,16 +148,12 @@ static int test_alloc_and_import(char *heap_name)
        int ret;
 
        heap_fd = dmabuf_heap_open(heap_name);
-       if (heap_fd < 0)
-               return -1;
 
-       printf("  Testing allocation and importing:  ");
+       ksft_print_msg("Testing allocation and importing:\n");
        ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-       if (ret) {
-               printf("FAIL (Allocation Failed!)\n");
-               ret = -1;
-               goto out;
-       }
+       if (ret)
+               ksft_exit_fail_msg("FAIL (Allocation Failed!)\n");
+
        /* mmap and write a simple pattern */
        p = mmap(NULL,
                 ONE_MEG,

tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c static int test_alloc_and_import(char *heap_name) · line 168

Quoted 8 lines
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +1 -1
@@ -166,7 +162,7 @@ static int test_alloc_and_import(char *heap_name)
                 dmabuf_fd,
                 0);
        if (p == MAP_FAILED) {
-               printf("FAIL (mmap() failed)\n");
+               ksft_print_msg("FAIL (mmap() failed)\n");
                ret = -1;
                goto out;
        }

I think you should just ksft_exit_fail_msg these too and get rid of out / not bother with manual cleanup if we're going to exit anyway.

Quoted 303 lines
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +66 -103
@@ -179,31 +175,28 @@ static int test_alloc_and_import(char *heap_name)
        importer_fd = open_vgem();
        if (importer_fd < 0) {
                ret = importer_fd;
-               printf("(Could not open vgem - skipping):  ");
+               ksft_test_result_skip("Could not open vgem\n");
        } else {
                ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle);
-               if (ret < 0) {
-                       printf("FAIL (Failed to import buffer)\n");
-                       goto out;
-               }
+               ksft_test_result(ret >= 0, "Import buffer\n");
        }
 
        ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
        if (ret < 0) {
-               printf("FAIL (DMA_BUF_SYNC_START failed!)\n");
+               ksft_print_msg("FAIL (DMA_BUF_SYNC_START failed!)\n");
                goto out;
        }
 
        memset(p, 0xff, ONE_MEG);
        ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
        if (ret < 0) {
-               printf("FAIL (DMA_BUF_SYNC_END failed!)\n");
+               ksft_print_msg("FAIL (DMA_BUF_SYNC_END failed!)\n");
                goto out;
        }
 
        close_handle(importer_fd, handle);
-       ret = 0;
-       printf(" OK\n");
+       ksft_test_result_pass("%s\n", __func__);
+       return;
 out:
        if (p)
                munmap(p, ONE_MEG);
@@ -214,32 +207,29 @@ static int test_alloc_and_import(char *heap_name)
        if (heap_fd >= 0)
                close(heap_fd);
 
-       return ret;
+       ksft_test_result_fail("%s\n", __func__);
 }
 
-static int test_alloc_zeroed(char *heap_name, size_t size)
+static void test_alloc_zeroed(char *heap_name, size_t size)
 {
        int heap_fd = -1, dmabuf_fd[32];
        int i, j, ret;
        void *p = NULL;
        char *c;
 
-       printf("  Testing alloced %ldk buffers are zeroed:  ", size / 1024);
+       ksft_print_msg("Testing alloced %ldk buffers are zeroed:\n", size / 1024);
        heap_fd = dmabuf_heap_open(heap_name);
-       if (heap_fd < 0)
-               return -1;
 
        /* Allocate and fill a bunch of buffers */
        for (i = 0; i < 32; i++) {
                ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
-               if (ret < 0) {
-                       printf("FAIL (Allocation (%i) failed)\n", i);
-                       goto out;
-               }
+               if (ret)
+                       ksft_exit_fail_msg("FAIL (Allocation (%i) failed)\n", i);
+
                /* mmap and fill with simple pattern */
                p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
                if (p == MAP_FAILED) {
-                       printf("FAIL (mmap() failed!)\n");
+                       ksft_print_msg("FAIL (mmap() failed!)\n");
                        ret = -1;
                        goto out;
                }
@@ -255,15 +245,13 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
        /* Allocate and validate all buffers are zeroed */
        for (i = 0; i < 32; i++) {
                ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
-               if (ret < 0) {
-                       printf("FAIL (Allocation (%i) failed)\n", i);
-                       goto out;
-               }
+               if (ret < 0)
+                       ksft_exit_fail_msg("FAIL (Allocation (%i) failed)\n", i);
 
                /* mmap and validate everything is zero */
                p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
                if (p == MAP_FAILED) {
-                       printf("FAIL (mmap() failed!)\n");
+                       ksft_print_msg("FAIL (mmap() failed!)\n");
                        ret = -1;
                        goto out;
                }
@@ -271,7 +259,7 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
                c = (char *)p;
                for (j = 0; j < size; j++) {
                        if (c[j] != 0) {
-                               printf("FAIL (Allocated buffer not zeroed @ %i)\n", j);
+                               ksft_print_msg("FAIL (Allocated buffer not zeroed @ %i)\n", j);
                                break;
                        }
                }
@@ -283,8 +271,8 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
                close(dmabuf_fd[i]);
 
        close(heap_fd);
-       printf("OK\n");
-       return 0;
+       ksft_test_result_pass("%s\n", __func__);
+       return;
 
 out:
        while (i > 0) {
@@ -292,7 +280,7 @@ static int test_alloc_zeroed(char *heap_name, size_t size)
                i--;
        }
        close(heap_fd);
-       return ret;
+       ksft_test_result_fail("%s\n", __func__);
 }
 
 /* Test the ioctl version compatibility w/ a smaller structure then expected */
@@ -360,126 +348,98 @@ static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
        return ret;
 }
 
-static int test_alloc_compat(char *heap_name)
+static void test_alloc_compat(char *heap_name)
 {
-       int heap_fd = -1, dmabuf_fd = -1;
-       int ret;
+       int ret, heap_fd = -1, dmabuf_fd = -1;
 
        heap_fd = dmabuf_heap_open(heap_name);
-       if (heap_fd < 0)
-               return -1;
 
-       printf("  Testing (theoretical)older alloc compat:  ");
+       ksft_print_msg("Testing (theoretical)older alloc compat:\n");
        ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-       if (ret) {
-               printf("FAIL (Older compat allocation failed!)\n");
-               ret = -1;
-               goto out;
-       }
-       close(dmabuf_fd);
-       printf("OK\n");
+       if (dmabuf_fd >= 0)
+               close(dmabuf_fd);
+       ksft_test_result(!ret, "dmabuf_heap_alloc_older\n");
 
-       printf("  Testing (theoretical)newer alloc compat:  ");
+       ksft_print_msg("Testing (theoretical)newer alloc compat:\n");
        ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-       if (ret) {
-               printf("FAIL (Newer compat allocation failed!)\n");
-               ret = -1;
-               goto out;
-       }
-       printf("OK\n");
-out:
        if (dmabuf_fd >= 0)
                close(dmabuf_fd);
-       if (heap_fd >= 0)
-               close(heap_fd);
+       ksft_test_result(!ret, "dmabuf_heap_alloc_newer\n");
 
-       return ret;
+       close(heap_fd);
 }
 
-static int test_alloc_errors(char *heap_name)
+static void test_alloc_errors(char *heap_name)
 {
        int heap_fd = -1, dmabuf_fd = -1;
        int ret;
 
        heap_fd = dmabuf_heap_open(heap_name);
-       if (heap_fd < 0)
-               return -1;
 
-       printf("  Testing expected error cases:  ");
+       ksft_print_msg("Testing expected error cases:\n");
        ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
-       if (!ret) {
-               printf("FAIL (Did not see expected error (invalid fd)!)\n");
-               ret = -1;
-               goto out;
-       }
+       ksft_test_result(ret, "Error expected on invalid fd\n");
 
        ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd);
-       if (!ret) {
-               printf("FAIL (Did not see expected error (invalid heap flags)!)\n");
-               ret = -1;
-               goto out;
-       }
+       ksft_test_result(ret, "Error expected on invalid heap flags\n");
 
        ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG,
                                        ~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd);
-       if (!ret) {
-               printf("FAIL (Did not see expected error (invalid fd flags)!)\n");
-               ret = -1;
-               goto out;
-       }
+       ksft_test_result(ret, "Error expected on invalid heap flags\n");
 
-       printf("OK\n");
-       ret = 0;
-out:
        if (dmabuf_fd >= 0)
                close(dmabuf_fd);
        if (heap_fd >= 0)
                close(heap_fd);
+}
 
-       return ret;
+static int numer_of_heaps(void)
+{
+       DIR *d = opendir(DEVPATH);
+       struct dirent *dir;
+       int heaps = 0;
+
+       while ((dir = readdir(d))) {
+               if (!strncmp(dir->d_name, ".", 2))
+                       continue;
+               if (!strncmp(dir->d_name, "..", 3))
+                       continue;
+               heaps++;
+       }
+
+       return heaps;
 }
 
 int main(void)
 {
-       DIR *d;
        struct dirent *dir;
-       int ret = -1;
+       DIR *d;
+
+       ksft_print_header();
 
        d = opendir(DEVPATH);
        if (!d) {
-               printf("No %s directory?\n", DEVPATH);
-               return -1;
+               ksft_print_msg("No %s directory?\n", DEVPATH);
+               return KSFT_SKIP;
        }
 
-       while ((dir = readdir(d)) != NULL) {
+       ksft_set_plan(9 * numer_of_heaps());
+
+       while ((dir = readdir(d))) {
                if (!strncmp(dir->d_name, ".", 2))
                        continue;
                if (!strncmp(dir->d_name, "..", 3))
                        continue;
 
-               printf("Testing heap: %s\n", dir->d_name);
-               printf("=======================================\n");
-               ret = test_alloc_and_import(dir->d_name);
-               if (ret)
-                       break;
-
-               ret = test_alloc_zeroed(dir->d_name, 4 * 1024);
-               if (ret)
-                       break;
-
-               ret = test_alloc_zeroed(dir->d_name, ONE_MEG);
-               if (ret)
-                       break;
-
-               ret = test_alloc_compat(dir->d_name);
-               if (ret)
-                       break;
-
-               ret = test_alloc_errors(dir->d_name);
-               if (ret)
-                       break;
+               ksft_print_msg("Testing heap: %s\n", dir->d_name);
+               ksft_print_msg("=======================================\n");
+               test_alloc_and_import(dir->d_name);
+               test_alloc_zeroed(dir->d_name, 4 * 1024);
+               test_alloc_zeroed(dir->d_name, ONE_MEG);
+               test_alloc_compat(dir->d_name);
+               test_alloc_errors(dir->d_name);
        }
        closedir(d);
 
-       return ret;
+       ksft_finished();
 }
--

2.42.0

#4 lore raw
Quoted 76 lines · T.J. Mercier
Quoted 74 lines · Muhammad Usama Anjum

Conform the layout, informational and status messages to TAP. No functional change is intended other than the layout of output messages.

---
 .../selftests/dmabuf-heaps/dmabuf-heap.c      | 194 +++++++-----------
 1 file changed, 77 insertions(+), 117 deletions(-)
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +10 -14
index 890a8236a8ba7..6e538e346cb8f 100644
@@ -15,6 +15,7 @@
 #include <linux/dma-buf.h>
 #include <linux/dma-heap.h>
 #include <drm/drm.h>
+#include "../kselftest.h"
 
 #define DEVPATH "/dev/dma_heap"
 
@@ -90,14 +91,13 @@ static int dmabuf_heap_open(char *name)
        char buf[256];
 
        ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
-       if (ret < 0) {
-               printf("snprintf failed!\n");
-               return ret;
-       }
+       if (ret < 0)
+               ksft_exit_fail_msg("snprintf failed!\n");
 
        fd = open(buf, O_RDWR);
        if (fd < 0)
-               printf("open %s failed!\n", buf);
+               ksft_exit_fail_msg("open %s failed: %s\n", buf, strerror(errno));
+
        return fd;
 }
 
@@ -140,7 +140,7 @@ static int dmabuf_sync(int fd, int start_stop)
 
 #define ONE_MEG (1024 * 1024)
 
-static int test_alloc_and_import(char *heap_name)
+static void test_alloc_and_import(char *heap_name)
 {
        int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
        uint32_t handle = 0;
@@ -148,16 +148,12 @@ static int test_alloc_and_import(char *heap_name)
        int ret;
 
        heap_fd = dmabuf_heap_open(heap_name);
-       if (heap_fd < 0)
-               return -1;
 
-       printf("  Testing allocation and importing:  ");
+       ksft_print_msg("Testing allocation and importing:\n");
        ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
-       if (ret) {
-               printf("FAIL (Allocation Failed!)\n");
-               ret = -1;
-               goto out;
-       }
+       if (ret)
+               ksft_exit_fail_msg("FAIL (Allocation Failed!)\n");
+
        /* mmap and write a simple pattern */
        p = mmap(NULL,
                 ONE_MEG,
@@ -166,7 +162,7 @@ static int test_alloc_and_import(char *heap_name)
                 dmabuf_fd,
                 0);
        if (p == MAP_FAILED) {
Quoted 8 lines

tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c static int test_alloc_and_import(char *heap_name) · line 168

Quoted 5 lines
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c +1 -1
-               printf("FAIL (mmap() failed)\n");
+               ksft_print_msg("FAIL (mmap() failed)\n");
                ret = -1;
                goto out;
        }

I think you should just ksft_exit_fail_msg these too and get rid of out / not bother with manual cleanup if we're going to exit anyway.

Not sure how I missed this. I'll send a v2.