[igt-dev] [PATCH i-g-t] tests/dumb_buffer: Tests for creation

Ramalingam C
2019-11-15T15:39:34Z

Tests are developed for dumb buffer creation.

Signed-off-by: Ramalingam C <ramalingam.c@intel.com>
cc: Chris Wilson <chris@chris-wilson.co.uk>
---
tests/Makefile.am | 2 +
tests/Makefile.sources | 3 +
tests/i915/gem_dumb_buffer.c | 266 +++++++++++++++++++++++++++++++++++
tests/meson.build | 8 ++
4 files changed, 279 insertions(+)
create mode 100644 tests/i915/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 abf1e2fc14a3..438f71ef88cf 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -172,6 +172,9 @@ gem_ctx_switch_SOURCES = i915/gem_ctx_switch.c
TESTS_progs += gem_ctx_thrash
gem_ctx_thrash_SOURCES = i915/gem_ctx_thrash.c

+TESTS_progs += gem_dumb_buffer
+gem_dumb_buffer_SOURCES = i915/gem_dumb_buffer.c
+
TESTS_progs += gem_double_irq_loop
gem_double_irq_loop_SOURCES = i915/gem_double_irq_loop.c

diff --git a/tests/i915/gem_dumb_buffer.c b/tests/i915/gem_dumb_buffer.c
new file mode 100644
index 000000000000..d6a8ea5a8a6a
--- /dev/null
+++ b/tests/i915/gem_dumb_buffer.c
@@ -0,0 +1,266 @@
+/*
+ * Copyright © 2015 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@intel.com>
+ *
+ */
+
+/** @file gem_dumb_buffer.c
+ *
+ * This is a test for the extended and old gem_create_dumb and gem_mmap_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"
+
+IGT_TEST_DESCRIPTION("This is a test for the extended & old gem_create_dumb and"
+ " gem_mmap_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);
+}
+
+/*
+ * 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 -= try;
+ } while ((max = atomic_compare_swap_u64(global, old, max)) != old);
+
+ return try;
+}
+
+struct thread_clear {
+ _Atomic(uint64_t) max;
+ int timeout;
+ int i915;
+};
+
+static void *thread_clear(void *data)
+{
+ struct thread_clear *arg = data;
+ unsigned long checked = 0;
+ int i915 = arg->i915;
+
+ igt_until_timeout(arg->timeout) {
+ struct drm_mode_create_dumb create = {};
+ uint64_t npages;
+ uint32_t stride;
+
+ 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);
+ stride = create.width * DIV_ROUND_UP(create.bpp, 8);
+ _npages = npages <= (((unsigned int)~0U) / stride) ?
+ npages : (((unsigned int)~0U) / stride);
+ create.height = _npages;
+
+ do_ioctl(i915, DRM_IOCTL_MODE_CREATE_DUMB, &create);
+
+ igt_assert(_npages == create.size / PAGE_SIZE);
+
+ for (uint64_t page = 0; page < _npages; page++) {
+ uint64_t x;
+
+ gem_read(i915, create.handle,
+ page * 4096 +
+ (page % (4096 - sizeof(x))),
+ &x, sizeof(x));
+ igt_assert_eq_u64(x, 0);
+ }
+ gem_close(i915, create.handle);
+ checked += _npages;
+ }
+
+ atomic_fetch_add(&arg->max, npages);
+ }
+
+ return (void *)(uintptr_t)checked;
+}
+
+static void always_clear(int i915, int timeout)
+{
+ struct thread_clear arg = {
+ .i915 = i915,
+ .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_INTEL);
+ }
+
+ 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("create-clear")
+ always_clear(fd, 30);
+}
diff --git a/tests/meson.build b/tests/meson.build
index 98f2db55584d..de5a2708d3f5 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -289,6 +289,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