[igt-dev] [PATCH i-g-t v3 1/2] tests/dumb_buffer: Tests for creation and map

Ramalingam C ramalingam.c at intel.com
Thu Nov 28 18:09:37 UTC 2019


Dumb buffer creation and mapping are covered with test cases.

v2:
  Made as a generic test for dumb buffer creation [Chris]
v3:
  dumb_map is used [Chris]
  Added two tests for map.

Signed-off-by: Ramalingam C <ramalingam.c at intel.com>
cc: Chris Wilson <chris at chris-wilson.co.uk>
---
 tests/Makefile.am       |   2 +
 tests/Makefile.sources  |   1 +
 tests/gem_dumb_buffer.c | 317 ++++++++++++++++++++++++++++++++++++++++
 tests/meson.build       |   9 ++
 4 files changed, 329 insertions(+)
 create mode 100644 tests/gem_dumb_buffer.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7d71df8c7a2e..66e69558663a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -96,6 +96,8 @@ gem_close_race_LDADD = $(LDADD) -lpthread
 gem_ctx_thrash_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
 gem_ctx_thrash_LDADD = $(LDADD) -lpthread
 gem_ctx_sseu_LDADD = $(LDADD) $(top_builddir)/lib/libigt_perf.la
+gem_dumb_buffer_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
+gem_dumb_buffer_LDADD = $(LDADD) -lpthread -latomic
 gem_exec_balancer_LDADD = $(LDADD) $(top_builddir)/lib/libigt_perf.la
 gem_exec_capture_LDADD = $(LDADD) -lz
 gem_exec_parallel_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index 27801c89c46d..d02bbd0d981c 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -99,6 +99,7 @@ TESTS_progs = \
 	tools_test \
 	vgem_basic \
 	vgem_slow \
+	gem_dumb_buffer \
 	$(NULL)
 
 TESTS_progs += gem_bad_reloc
diff --git a/tests/gem_dumb_buffer.c b/tests/gem_dumb_buffer.c
new file mode 100644
index 000000000000..caf1adb1fadb
--- /dev/null
+++ b/tests/gem_dumb_buffer.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright © 2019 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Ramalingam C <ramalingam.c at intel.com>
+ *
+ */
+
+/** @file gem_dumb_buffer.c
+ *
+ * This is a test for the extended and old gem_create_dumb and gem_map_dumb
+ * ioctl, that includes allocation of object from stolen memory, shmem and
+ * local memory.
+ *
+ * The goal is to simply ensure that basics work and invalid input
+ * combinations are rejected.
+ */
+
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <getopt.h>
+#include <pthread.h>
+#include <stdatomic.h>
+
+#include <drm.h>
+
+#include "ioctl_wrappers.h"
+#include "intel_bufmgr.h"
+#include "intel_batchbuffer.h"
+#include "intel_io.h"
+#include "intel_chipset.h"
+#include "igt_aux.h"
+#include "drmtest.h"
+#include "drm.h"
+#include "i915_drm.h"
+#include "igt_kms.h"
+
+IGT_TEST_DESCRIPTION("This is a test for the extended & old gem_create_dumb and"
+		     " gem_map_dumb ioctl,"
+		     " that includes allocation of object from stolen memory,"
+		     " shmem and local memory.");
+
+#define CLEAR(s) memset(&s, 0, sizeof(s))
+#define PAGE_SIZE 4096
+
+static void invalid_dimensions_test(int fd)
+{
+	struct drm_mode_create_dumb create;
+
+	memset(&create, 0, sizeof(create));
+	create.width = 4032;
+	create.height = 2016;
+	create.bpp = 24;
+	do_ioctl_err(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create, EINVAL);
+
+	create.bpp = 32;
+	create.width = 0;
+	do_ioctl_err(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create, EINVAL);
+
+	create.width = 4032;
+	create.height = 0;
+	do_ioctl_err(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create, EINVAL);
+}
+
+static void valid_dumb_creation_test(int fd)
+{
+	struct drm_mode_create_dumb create = {
+		.width = 4032,
+		.height = 2016,
+		.bpp = 32,
+	};
+
+	do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+	gem_close(fd, create.handle);
+}
+
+static void valid_map(int fd)
+{
+	struct drm_mode_create_dumb create = {
+		.width = 4032,
+		.height = 2016,
+		.bpp = 32,
+	};
+	void *ptr;
+
+	do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+	ptr = kmstest_dumb_map_buffer(fd, create.handle, create.size,
+				      PROT_READ | PROT_WRITE);
+	munmap(ptr, create.size);
+	gem_close(fd, create.handle);
+}
+
+static void invalid_size_map(int fd)
+{
+	struct drm_mode_map_dumb arg = {};
+	struct drm_mode_create_dumb create = {
+		.width = 4032,
+		.height = 2016,
+		.bpp = 32,
+	};
+	void *ptr;
+
+	do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+
+	arg.handle = create.handle;
+	do_ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
+
+	ptr = mmap(NULL, create.size + 1, PROT_READ | PROT_WRITE,
+		   MAP_SHARED, fd, arg.offset);
+	igt_assert(ptr == MAP_FAILED);
+
+	gem_close(fd, create.handle);
+}
+
+/*
+ * Creating an dumb buffer with non-aligned size and trying to access it with an
+ * offset, which is greater than the requested size but smaller than the
+ * object's last page boundary. pwrite here must be successful.
+ */
+static void valid_nonaligned_size(int fd)
+{
+	struct drm_mode_create_dumb create = {
+		.width = 24,
+		.height = 24,
+		.bpp = 32,
+	};
+	char buf[PAGE_SIZE];
+
+	do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+
+	gem_write(fd, create.handle, PAGE_SIZE / 2, buf, PAGE_SIZE / 2);
+
+	gem_close(fd, create.handle);
+}
+
+/*
+ * Creating an object with non-aligned size and trying to access it with an
+ * offset, which is greater than the requested size and larger than the
+ * object's last page boundary. pwrite here must fail.
+ */
+static void invalid_nonaligned_size(int fd)
+{
+	struct drm_mode_create_dumb create = {
+		.width = 24,
+		.height = 24,
+		.bpp = 32,
+	};
+	char buf[PAGE_SIZE];
+
+	do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+	/* This should fail. Hence cannot use gem_write. */
+	igt_assert(__gem_write(fd, create.handle,
+			       create.size - (PAGE_SIZE / 2), buf, PAGE_SIZE));
+	gem_close(fd, create.handle);
+}
+
+static uint64_t atomic_compare_swap_u64(_Atomic(uint64_t) *ptr,
+					uint64_t oldval, uint64_t newval)
+{
+	atomic_compare_exchange_strong(ptr, &oldval, newval);
+	return oldval;
+}
+
+static uint64_t get_npages(_Atomic(uint64_t) *global, uint64_t npages)
+{
+	uint64_t try, old, max;
+
+	max = *global;
+	do {
+		old = max;
+		try = 1 + npages % ((max / 2) ? (max / 2) : 1);
+		max -= try;
+	} while ((max = atomic_compare_swap_u64(global, old, max)) != old);
+
+	return try;
+}
+
+struct thread_clear {
+	_Atomic(uint64_t) max;
+	int timeout;
+	int fd;
+};
+
+#define MAX_PAGE_TO_REQUEST	102400
+
+static void *thread_clear(void *data)
+{
+	struct thread_clear *arg = data;
+	unsigned long checked = 0;
+	int fd = arg->fd;
+	void *ptr;
+
+	igt_until_timeout(arg->timeout) {
+		struct drm_mode_create_dumb create = {};
+		uint64_t npages;
+
+		npages = random();
+		npages = npages << 32;
+		npages |= random();
+		npages = get_npages(&arg->max, npages);
+
+		for(uint64_t _npages = npages; npages > 0; npages -= _npages)
+		{
+			create.bpp = 32;
+			create.width = PAGE_SIZE / (create.bpp / 8);
+			_npages = npages <= MAX_PAGE_TO_REQUEST ? npages :
+				  MAX_PAGE_TO_REQUEST;
+			create.height = _npages;
+
+			do_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+
+			igt_assert(_npages == create.size / PAGE_SIZE);
+
+			ptr = kmstest_dumb_map_buffer(fd, create.handle,
+						      create.size,
+						      PROT_READ | PROT_WRITE);
+
+			for (uint64_t page = 0; page < _npages; page++) {
+				uint64_t x;
+				memcpy(&x, ptr + page * 4096 +
+				       page % (4096 - sizeof(x)), sizeof(x));
+				igt_assert_eq_u64(x, 0);
+			}
+
+			if (ptr)
+				munmap(ptr, create.size);
+
+			kmstest_dumb_destroy(fd, create.handle);
+			atomic_fetch_add(&arg->max, _npages);
+			checked += _npages;
+		}
+
+	}
+
+	return (void *)(uintptr_t)checked;
+}
+
+static void always_clear(int fd, int timeout)
+{
+	struct thread_clear arg = {
+		.fd = fd,
+		.timeout = timeout,
+		.max = intel_get_avail_ram_mb() << (20 - 12), /* in pages */
+	};
+	const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+	unsigned long checked;
+	pthread_t thread[ncpus];
+	void *result;
+
+	for (int i = 0; i < ncpus; i++)
+		pthread_create(&thread[i], NULL, thread_clear, &arg);
+
+	checked = 0;
+	for (int i = 0; i < ncpus; i++) {
+		pthread_join(thread[i], &result);
+		checked += (uintptr_t)result;
+	}
+	igt_info("Checked %'lu page allocations\n", checked);
+}
+
+igt_main
+{
+	int fd = -1;
+
+	igt_skip_on_simulation();
+
+	igt_fixture {
+		fd = drm_open_driver(DRIVER_ANY);
+	}
+
+	igt_subtest("invalid-bpp")
+		invalid_dimensions_test(fd);
+
+	igt_subtest("create-valid-dumb")
+		valid_dumb_creation_test(fd);
+
+	igt_subtest("create-valid-nonaligned")
+		valid_nonaligned_size(fd);
+
+	igt_subtest("create-invalid-nonaligned")
+		invalid_nonaligned_size(fd);
+
+	igt_subtest("map-valid")
+		valid_map(fd);
+
+	igt_subtest("map-invalid-size")
+		invalid_size_map(fd);
+
+	igt_subtest("create-clear")
+		always_clear(fd, 30);
+}
diff --git a/tests/meson.build b/tests/meson.build
index 755fc9e6f2d7..915a567d4a97 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -97,6 +97,7 @@ test_progs = [
 	'vc4_wait_seqno',
 	'vgem_basic',
 	'vgem_slow',
+	'gem_dumb_buffer'
 ]
 
 i915_progs = [
@@ -291,6 +292,14 @@ test_executables += executable('gem_create',
 	   install : true)
 test_list += 'gem_create'
 
+# test_executables += executable('gem_dumb_buffer',
+#	   join_paths('i915', 'gem_dumb_buffer.c'),
+#	   dependencies : test_deps + [ libatomic ],
+#	   install_dir : libexecdir,
+#	   install_rpath : libexecdir_rpathdir,
+#	   install : true)
+#test_list += 'gem_dumb_buffer'
+
 test_executables += executable('gem_ctx_sseu',
 	   join_paths('i915', 'gem_ctx_sseu.c'),
 	   dependencies : test_deps + [ lib_igt_perf ],
-- 
2.20.1



More information about the igt-dev mailing list