All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Zbigniew Kempczyński" <zbigniew.kempczynski@intel.com>
To: igt-dev@lists.freedesktop.org
Subject: [igt-dev] [PATCH i-g-t v4 3/3] tests/i915/gem_mmap_offset: Add new API test for gem_mmap_offset
Date: Tue, 26 Nov 2019 18:25:56 +0100	[thread overview]
Message-ID: <20191126172556.21805-4-zbigniew.kempczynski@intel.com> (raw)
In-Reply-To: <20191126172556.21805-1-zbigniew.kempczynski@intel.com>

From: Lukasz Kalamarz <lukasz.kalamarz@intel.com>

Few simple tests which tries to create / mmap buffer objects
using GEM_MMAP_OFFSET uAPI.

v2: change from WC -> WB (according to Chris review comment)
v3: add mmap-offset-close-race test

Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Signed-off-by: Zbigniew Kempczyński <zbigniew.kempczynski@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>
---
 tests/Makefile.sources       |   3 +
 tests/i915/gem_mmap_offset.c | 175 +++++++++++++++++++++++++++++++++++
 tests/meson.build            |   1 +
 3 files changed, 179 insertions(+)
 create mode 100644 tests/i915/gem_mmap_offset.c

diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index d86f9c26..af464aef 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -323,6 +323,9 @@ gem_mmap_SOURCES = i915/gem_mmap.c
 TESTS_progs += gem_mmap_gtt
 gem_mmap_gtt_SOURCES = i915/gem_mmap_gtt.c
 
+TESTS_progs += gem_mmap_offset
+gem_mmap_offset_SOURCES = i915/gem_mmap_offset.c
+
 TESTS_progs += gem_mmap_offset_exhaustion
 gem_mmap_offset_exhaustion_SOURCES = i915/gem_mmap_offset_exhaustion.c
 
diff --git a/tests/i915/gem_mmap_offset.c b/tests/i915/gem_mmap_offset.c
new file mode 100644
index 00000000..d11f7f22
--- /dev/null
+++ b/tests/i915/gem_mmap_offset.c
@@ -0,0 +1,175 @@
+/*
+ * 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.
+ */
+
+#include "igt.h"
+#include <errno.h>
+#include <stdatomic.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include "drm.h"
+
+IGT_TEST_DESCRIPTION("Basic MMAP_OFFSET IOCTL tests for mem regions\n");
+
+static int mmap_offset_ioctl(int fd, struct drm_i915_gem_mmap_offset *arg)
+{
+	int err = 0;
+
+	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET, arg))
+		err = -errno;
+
+	errno = 0;
+	return err;
+}
+
+static void mmap_offset_bad_object(int fd)
+{
+	uint32_t real_handle;
+	uint32_t handles[20];
+	int i = 0;
+
+	real_handle = gem_create(fd, 4096);
+
+	handles[i++] = 0xdeadbeef;
+	for (int bit = 0; bit < 16; bit++)
+		handles[i++] = real_handle | (1 << (bit + 16));
+	handles[i] = real_handle + 1;
+
+	for (; i >= 0; i--) {
+		struct drm_i915_gem_mmap_offset arg = {
+			.handle = handles[i],
+			.flags = I915_MMAP_OFFSET_WB,
+		};
+
+		igt_debug("Trying MMAP IOCTL with handle %x\n",
+			  handles[i]);
+		igt_assert_eq(mmap_offset_ioctl(fd, &arg),
+			      -ENOENT);
+	}
+
+	gem_close(fd, real_handle);
+}
+
+static void mmap_offset_basic(int fd)
+{
+	uint64_t flags;
+	uint32_t handle, obj_size;
+	uint8_t *expected, *buf, *addr;
+
+	obj_size = 4096;
+	handle = gem_create(fd, obj_size);
+	flags = I915_MMAP_OFFSET_WB;
+	addr = __gem_mmap_offset(fd, handle, 0, obj_size,
+				 PROT_READ | PROT_WRITE,
+				 flags);
+	igt_assert(addr);
+
+	igt_debug("Testing contents of newly created object.\n");
+	expected = calloc(obj_size, sizeof(*expected));
+	igt_assert_eq(memcmp(addr, expected, obj_size), 0);
+	free(expected);
+
+	igt_debug("Testing coherency of writes and mmap reads.\n");
+	buf = calloc(obj_size, sizeof(*buf));
+	memset(buf + 1024, 0x01, 1024);
+	gem_write(fd, handle, 0, buf, obj_size);
+
+	igt_debug("Testing that mapping stays after close\n");
+	gem_close(fd, handle);
+	igt_assert_eq(memcmp(buf, addr, obj_size), 0);
+	free(buf);
+
+	igt_debug("Testing unmapping\n");
+	munmap(addr, obj_size);
+}
+
+static void mmap_offset_close_race(int i915)
+{
+	const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+	_Atomic uint32_t *handles;
+	size_t len = ALIGN((ncpus + 1) * sizeof(uint32_t), 4096);
+
+	handles = mmap64(0, len, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
+	igt_assert(handles != MAP_FAILED);
+
+	igt_fork(child, ncpus + 1) {
+		do {
+			struct drm_i915_gem_mmap_offset mmap_arg = {};
+			const int i = 1 + random() % ncpus;
+			uint32_t old;
+
+			mmap_arg.handle = gem_create(i915, 4096);
+			mmap_arg.flags = I915_MMAP_OFFSET_WB;
+			old = atomic_exchange(&handles[i], mmap_arg.handle);
+			ioctl(i915, DRM_IOCTL_GEM_CLOSE, &old);
+
+			if (ioctl(i915,
+				  DRM_IOCTL_I915_GEM_MMAP_OFFSET,
+				  &mmap_arg) != -1) {
+				void *ptr;
+
+				ptr = mmap64(0, 4096,
+					     PROT_WRITE, MAP_SHARED, i915,
+					     mmap_arg.offset);
+				if (ptr != MAP_FAILED) {
+					*(volatile uint32_t *)ptr = 0;
+					munmap(ptr, 4096);
+				}
+			}
+
+		} while (!READ_ONCE(handles[0]));
+	}
+
+	sleep(20);
+	handles[0] = 1;
+	igt_waitchildren();
+
+	for (int i = 1; i <= ncpus; i++)
+		ioctl(i915, DRM_IOCTL_GEM_CLOSE, handles[i]);
+	munmap(handles, len);
+}
+
+igt_main
+{
+	int fd;
+
+	igt_fixture {
+		fd = drm_open_driver(DRIVER_INTEL);
+		gem_require_mmap_offset(fd);
+	}
+
+	igt_describe("Verify mapping to invalid gem objects won't be created");
+	igt_subtest_f("mmap-offset-bad-object")
+			mmap_offset_bad_object(fd);
+
+	igt_describe("Check buffer object mapping persists after gem_close");
+	igt_subtest_f("mmap-offset-basic")
+			mmap_offset_basic(fd);
+
+	igt_describe("Check race between close and mmap offset between threads");
+	igt_subtest_f("mmap-offset-close-race")
+			mmap_offset_close_race(fd);
+
+	igt_fixture {
+		close(fd);
+	}
+}
diff --git a/tests/meson.build b/tests/meson.build
index a909d271..ad55f697 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -174,6 +174,7 @@ i915_progs = [
 	'gem_media_vme',
 	'gem_mmap',
 	'gem_mmap_gtt',
+	'gem_mmap_offset',
 	'gem_mmap_offset_exhaustion',
 	'gem_mmap_wc',
 	'gem_partial_pwrite_pread',
-- 
2.23.0

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

  parent reply	other threads:[~2019-11-26 17:26 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-26 17:25 [igt-dev] [PATCH i-g-t v4 0/3] Add GEM_MMAP_OFFSET support in IGT Zbigniew Kempczyński
2019-11-26 17:25 ` [igt-dev] [PATCH i-g-t v4 1/3] include/drm-uapi/i915_drm: Add defs for mmap_offset Zbigniew Kempczyński
2019-11-26 17:25 ` [igt-dev] [PATCH i-g-t v4 2/3] lib/i915/gem_mman: add mmap_offset support Zbigniew Kempczyński
2019-11-28 14:38   ` Chris Wilson
2019-11-28 14:43   ` Chris Wilson
2019-11-26 17:25 ` Zbigniew Kempczyński [this message]
2019-11-28 12:55   ` [PATCH i-g-t] tests/i915/gem_mmap_offset: Add new API test for gem_mmap_offset Chris Wilson
2019-11-28 12:55     ` [igt-dev] " Chris Wilson
2019-11-28 12:55     ` [Intel-gfx] " Chris Wilson
2019-11-28 12:59     ` Chris Wilson
2019-11-28 12:59       ` [igt-dev] " Chris Wilson
2019-11-28 12:59       ` [Intel-gfx] " Chris Wilson
2019-11-28 13:38     ` [igt-dev] " Petri Latvala
2019-11-28 13:38       ` Petri Latvala
2019-11-28 13:38       ` [Intel-gfx] " Petri Latvala
2019-11-28 13:05   ` Chris Wilson
2019-11-28 13:05     ` [igt-dev] " Chris Wilson
2019-11-28 13:05     ` [Intel-gfx] " Chris Wilson
2019-11-28 13:10   ` Chris Wilson
2019-11-28 13:10     ` [igt-dev] " Chris Wilson
2019-11-28 13:10     ` [Intel-gfx] " Chris Wilson
2019-11-26 18:24 ` [igt-dev] ✓ Fi.CI.BAT: success for Add GEM_MMAP_OFFSET support in IGT. (rev4) Patchwork
2019-11-26 18:26   ` Chris Wilson
2019-11-27  5:22 ` [igt-dev] ✗ Fi.CI.IGT: failure " Patchwork
2019-11-28 13:05 ` [igt-dev] ✗ GitLab.Pipeline: warning for Add GEM_MMAP_OFFSET support in IGT. (rev5) Patchwork
2019-11-28 13:20 ` [igt-dev] ✓ Fi.CI.BAT: success " Patchwork
2019-11-28 13:43 ` [igt-dev] ✓ Fi.CI.BAT: success for Add GEM_MMAP_OFFSET support in IGT. (rev7) Patchwork
2019-11-29 19:26 ` [igt-dev] ✗ Fi.CI.IGT: failure for Add GEM_MMAP_OFFSET support in IGT. (rev5) Patchwork
2019-11-29 19:35 ` [igt-dev] ✗ Fi.CI.IGT: failure for Add GEM_MMAP_OFFSET support in IGT. (rev7) Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191126172556.21805-4-zbigniew.kempczynski@intel.com \
    --to=zbigniew.kempczynski@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.