[igt-dev] [PATCH i-g-t 3/3] tests/i915/gem_mmap_offset: remove local mmap calls

Zbigniew Kempczyński zbigniew.kempczynski at intel.com
Thu Dec 5 07:58:00 UTC 2019


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

Use gem_mman functions instead local versions.
Limit available memory for 'clear' test to run it on lmem correctly.

Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz at intel.com>
Signed-off-by: Zbigniew Kempczyński <zbigniew.kempczynski at intel.com>
Cc: Chris Wilson <chris at chris-wilson.co.uk>
Cc: Vanshidhar Konda <vanshidhar.r.konda at intel.com>
---
 tests/i915/gem_mmap_offset.c | 131 ++++++++++++++++++-----------------
 1 file changed, 68 insertions(+), 63 deletions(-)

diff --git a/tests/i915/gem_mmap_offset.c b/tests/i915/gem_mmap_offset.c
index 95e1e3e6..cd49e7d3 100644
--- a/tests/i915/gem_mmap_offset.c
+++ b/tests/i915/gem_mmap_offset.c
@@ -31,7 +31,7 @@
 #include "igt.h"
 #include "igt_x86.h"
 
-IGT_TEST_DESCRIPTION("Basic MMAP_OFFSET IOCTL tests for mem regions\n");
+IGT_TEST_DESCRIPTION("Basic MMAP_OFFSET IOCTL tests\n");
 
 static const struct mmap_offset {
 	const char *name;
@@ -63,28 +63,6 @@ static int mmap_offset_ioctl(int i915, struct drm_i915_gem_mmap_offset *arg)
 	return err;
 }
 
-static void *
-__mmap_offset(int i915, uint32_t handle, uint64_t offset, uint64_t size,
-	      unsigned int prot, uint64_t flags)
-{
-	struct drm_i915_gem_mmap_offset arg = {
-		.handle = handle,
-		.flags = flags,
-	};
-	void *ptr;
-
-	if (mmap_offset_ioctl(i915, &arg))
-		return NULL;
-
-	ptr = mmap64(0, size, prot, MAP_SHARED, i915, arg.offset + offset);
-	if (ptr == MAP_FAILED)
-		ptr = NULL;
-	else
-		errno = 0;
-
-	return ptr;
-}
-
 static void bad_object(int i915)
 {
 	uint32_t real_handle;
@@ -115,12 +93,19 @@ static void bad_object(int i915)
 
 static void bad_flags(int i915)
 {
+	uint64_t flags = I915_MMAP_OFFSET_UC;
 	struct drm_i915_gem_mmap_offset arg = {
 		.handle = gem_create(i915, 4096),
 		.flags = -1ull,
 	};
 
-	igt_assert_eq(mmap_offset_ioctl(i915, &arg), -EINVAL);
+	while (flags) {
+		igt_debug("Testing flags: %llx\n", arg.flags);
+		igt_assert_eq(mmap_offset_ioctl(i915, &arg), -EINVAL);
+		flags <<= 1;
+		arg.flags = flags;
+	}
+
 	gem_close(i915, arg.handle);
 }
 
@@ -147,24 +132,22 @@ static void basic_uaf(int i915)
 
 	for_each_mmap_offset_type(t) {
 		uint32_t handle = gem_create(i915, obj_size);
-		uint8_t *expected, *buf, *addr;
+		uint8_t *buf, *addr;
 
-		addr = __mmap_offset(i915, handle, 0, obj_size,
-				     PROT_READ | PROT_WRITE,
-				     t->type);
+		addr = __gem_mmap_offset(i915, handle, 0, obj_size,
+					 PROT_READ | PROT_WRITE,
+					 t->type);
 		if (!addr) {
 			gem_close(i915, handle);
 			continue;
 		}
 
-		expected = calloc(obj_size, sizeof(*expected));
+		buf = calloc(obj_size, sizeof(*buf));
 		gem_set_domain(i915, handle, t->domain, 0);
-		igt_assert_f(memcmp(addr, expected, obj_size) == 0,
+		igt_assert_f(memcmp(addr, buf, obj_size) == 0,
 			     "mmap(%s) not clear on gem_create()\n",
 			     t->name);
-		free(expected);
 
-		buf = calloc(obj_size, sizeof(*buf));
 		memset(buf + 1024, 0x01, 1024);
 		gem_write(i915, handle, 0, buf, obj_size);
 		gem_set_domain(i915, handle, t->domain, 0);
@@ -184,15 +167,16 @@ static void basic_uaf(int i915)
 		igt_assert_f(memcmp(buf, addr, obj_size) == 0,
 			     "mmap(%s) not resident after gem_close()\n",
 			     t->name);
-		free(buf);
 
-		igt_debug("Testing unmapping\n");
+		free(buf);
 		munmap(addr, obj_size);
 	}
 }
 
 static void isolation(int i915)
 {
+	int maps_supported = 0;
+
 	for_each_mmap_offset_type(t) {
 		struct drm_i915_gem_mmap_offset mmap_arg = {
 			.flags = t->type
@@ -218,10 +202,10 @@ static void isolation(int i915)
 		igt_assert_eq(mmap_offset_ioctl(B, &mmap_arg), 0);
 		offset_b = mmap_arg.offset;
 
-		igt_info("A[%s]: {fd:%d, handle:%d, offset:%"PRIx64"}\n",
-			 t->name, A, a, offset_a);
-		igt_info("B[%s]: {fd:%d, handle:%d, offset:%"PRIx64"}\n",
-			 t->name, B, b, offset_b);
+		igt_debug("A[%s]: {fd:%d, handle:%d, offset:%"PRIx64"}\n",
+			  t->name, A, a, offset_a);
+		igt_debug("B[%s]: {fd:%d, handle:%d, offset:%"PRIx64"}\n",
+			  t->name, B, b, offset_b);
 
 		errno = 0;
 		ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, i915, offset_a);
@@ -253,7 +237,11 @@ static void isolation(int i915)
 
 		ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);
 		igt_assert(ptr == MAP_FAILED);
+
+		maps_supported++;
 	}
+
+	igt_assert_f(maps_supported, "No mmap offset type found!\n");
 }
 
 static void pf_nonblock(int i915)
@@ -263,9 +251,9 @@ static void pf_nonblock(int i915)
 	for_each_mmap_offset_type(t) {
 		uint32_t *ptr;
 
-		ptr = __mmap_offset(i915, spin->handle, 0, 4096,
-				    PROT_READ | PROT_WRITE,
-				    t->type);
+		ptr = __gem_mmap_offset(i915, spin->handle, 0, 4096,
+					PROT_READ | PROT_WRITE,
+					t->type);
 		if (!ptr)
 			continue;
 
@@ -385,10 +373,13 @@ static void *thread_clear(void *data)
 		npages = get_npages(&arg->max, npages);
 		create.size = npages << 12;
 
+		igt_debug("pages: %lu, size: %llu\n", npages, create.size);
 		create_ioctl(i915, &create);
-		ptr = __mmap_offset(i915, create.handle, 0, create.size,
-				    PROT_READ | PROT_WRITE,
-				    t->type);
+		igt_assert(create.handle);
+		ptr = __gem_mmap_offset(i915, create.handle, 0, create.size,
+					PROT_READ | PROT_WRITE,
+					t->type);
+
 		/* No set-domains as we are being as naughty as possible */
 		for (uint64_t page = 0; ptr && page < npages; page++) {
 			uint64_t x[8] = {
@@ -418,12 +409,43 @@ static void *thread_clear(void *data)
 	return (void *)(uintptr_t)checked;
 }
 
+/*
+ * Depending on allocation region we have different memory constraints.
+ * Try to find reasonable limit to cover the test regardless the region.
+ */
+static uint64_t __get_memory_size_in_mb(int i915)
+{
+	uint64_t aperture_size = gem_available_aperture_size(i915);
+	uint32_t size = 1024*1024;
+	uint32_t *handles;
+	uint32_t num_handles;
+	int i = 0;
+
+	igt_debug("Aperture size: %zd\n", gem_available_aperture_size(i915));
+
+	num_handles = aperture_size / size;
+	handles = malloc(num_handles * sizeof(uint32_t));
+
+	for (i = 0; i < num_handles; i++) {
+		if (__gem_create(i915, size, &handles[i]))
+			break;
+	}
+	igt_debug("Created %d/%u handles of size %u\n", i, num_handles, size);
+	num_handles = i;
+
+	for (i = 0; i < num_handles; i++)
+		gem_close(i915, handles[i]);
+	free(handles);
+
+	return num_handles;
+}
+
 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 */
+		.max = __get_memory_size_in_mb(i915) / 2 << 8, /* in pages */
 	};
 	const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
 	unsigned long checked;
@@ -441,30 +463,13 @@ static void always_clear(int i915, int timeout)
 	igt_info("Checked %'lu page allocations\n", checked);
 }
 
-static int mmap_gtt_version(int i915)
-{
-	int gtt_version = -1;
-	struct drm_i915_getparam gp = {
-		.param = I915_PARAM_MMAP_GTT_VERSION,
-		.value = &gtt_version,
-	};
-	ioctl(i915, DRM_IOCTL_I915_GETPARAM, &gp);
-
-	return gtt_version;
-}
-
-static bool has_mmap_offset(int i915)
-{
-	return mmap_gtt_version(i915) >= 4;
-}
-
 igt_main
 {
 	int i915;
 
 	igt_fixture {
 		i915 = drm_open_driver(DRIVER_INTEL);
-		igt_require(has_mmap_offset(i915));
+		gem_require_mmap_offset(i915);
 	}
 
 	igt_describe("Verify mapping to invalid gem objects won't be created");
-- 
2.23.0



More information about the igt-dev mailing list