[igt-dev] [PATCH i-g-t] tests/gem_create: Test unaligned BO size update

Michał Winiarski
2019-03-26T16:33:10Z

The driver is supposed to update the size when it's trying to outsmart
userspace. Let's test it, and tweak the __gem_create in order to control
its input.

Signed-off-by: Michał Winiarski <michal.winiarski@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Janusz Krzysztofik <janusz.krzysztofik@intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
---
lib/ioctl_wrappers.c | 19 ++++++++----------
lib/ioctl_wrappers.h | 2 +-
tests/i915/gem_create.c | 35 +++++++++++++++++++++++++---------
tests/i915/gem_fd_exhaustion.c | 11 ++++++-----
4 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/lib/ioctl_wrappers.c b/lib/ioctl_wrappers.c
index a66eb4bc..bb387a27 100644
--- a/lib/ioctl_wrappers.c
+++ b/lib/ioctl_wrappers.c
@@ -541,21 +541,16 @@ uint32_t gem_create_stolen(int fd, uint64_t size)
return create.handle;
}

-int __gem_create(int fd, uint64_t size, uint32_t *handle)
+int __gem_create(int fd, struct drm_i915_gem_create *create)
{
- struct drm_i915_gem_create create = {
- .size = size,
- };
int err = 0;

- if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) == 0) {
- *handle = create.handle;
- } else {
+ if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, create) != 0) {
err = -errno;
igt_assume(err != 0);
}
-
errno = 0;
+
return err;
}

@@ -571,11 +566,13 @@ int __gem_create(int fd, uint64_t size, uint32_t *handle)
*/
uint32_t gem_create(int fd, uint64_t size)
{
- uint32_t handle;
+ struct drm_i915_gem_create create = {
+ .size = size,
+ };

- igt_assert_eq(__gem_create(fd, size, &handle), 0);
+ igt_assert_eq(__gem_create(fd, &create), 0);

- return handle;
+ return create.handle;
}

/**
diff --git a/lib/ioctl_wrappers.h b/lib/ioctl_wrappers.h
index ad93daff..8202374f 100644
--- a/lib/ioctl_wrappers.h
+++ b/lib/ioctl_wrappers.h
@@ -77,7 +77,7 @@ void gem_sync(int fd, uint32_t handle);
bool gem_create__has_stolen_support(int fd);
uint32_t __gem_create_stolen(int fd, uint64_t size);
uint32_t gem_create_stolen(int fd, uint64_t size);
-int __gem_create(int fd, uint64_t size, uint32_t *handle);
+int __gem_create(int fd, struct drm_i915_gem_create *create);
uint32_t gem_create(int fd, uint64_t size);
void gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
int __gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf);
diff --git a/tests/i915/gem_create.c b/tests/i915/gem_create.c
index 2a861ca8..057816a5 100644
--- a/tests/i915/gem_create.c
+++ b/tests/i915/gem_create.c
@@ -71,7 +71,7 @@ struct local_i915_gem_create_v2 {
uint32_t pad;
#define I915_CREATE_PLACEMENT_STOLEN (1<<0)
uint32_t flags;
-} create;
+} create_v2;

#define LOCAL_IOCTL_I915_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_CREATE, struct local_i915_gem_create_v2)

@@ -81,24 +81,26 @@ static void invalid_flag_test(int fd)

gem_require_stolen_support(fd);

- create.handle = 0;
- create.size = PAGE_SIZE;
- create.flags = ~I915_CREATE_PLACEMENT_STOLEN;
- ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
+ create_v2.handle = 0;
+ create_v2.size = PAGE_SIZE;
+ create_v2.flags = ~I915_CREATE_PLACEMENT_STOLEN;
+ ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create_v2);

igt_assert(ret <= 0);

- create.flags = ~0;
- ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create);
+ create_v2.flags = ~0;
+ ret = drmIoctl(fd, LOCAL_IOCTL_I915_GEM_CREATE, &create_v2);

igt_assert(ret <= 0);
}

static void invalid_size_test(int fd)
{
- uint32_t handle;
+ struct drm_i915_gem_create create = {
+ .size = 0,
+ };

- igt_assert_eq(__gem_create(fd, 0, &handle), -EINVAL);
+ igt_assert_eq(__gem_create(fd, &create), -EINVAL);
}

/*
@@ -209,6 +211,18 @@ static void always_clear(int i915, int timeout)
pthread_join(thread[i], NULL);
}

+static void size_update(int fd)
+{
+ int size_initial_nonaligned = 16;
+
+ struct drm_i915_gem_create create = {
+ .size = size_initial_nonaligned,
+ };
+
+ igt_assert_eq(__gem_create(fd, &create), 0);
+ igt_assert_neq(create.size, size_initial_nonaligned);
+}
+
igt_main
{
int fd = -1;
@@ -233,4 +247,7 @@ igt_main

igt_subtest("create-clear")
always_clear(fd, 30);
+
+ igt_subtest("create-size-update")
+ size_update(fd);
}
diff --git a/tests/i915/gem_fd_exhaustion.c b/tests/i915/gem_fd_exhaustion.c
index 559590b1..05800a5d 100644
--- a/tests/i915/gem_fd_exhaustion.c
+++ b/tests/i915/gem_fd_exhaustion.c
@@ -100,16 +100,17 @@ igt_simple_main
igt_drop_root();

for (int i = 0; ; i++) {
+ struct drm_i915_gem_create create = {
+ .size = 4096,
+ };
int leak = open("/dev/null", O_RDONLY);
- uint32_t handle;

- if (__gem_create(fd, 4096, &handle) == 0)
- gem_close(fd, handle);
+ if (__gem_create(fd, &create) == 0)
+ gem_close(fd, create.handle);

if (leak < 0) {
igt_info("fd exhaustion after %i rounds.\n", i);
- igt_assert(__gem_create(fd, 4096,
- &handle) < 0);
+ igt_assert(__gem_create(fd, &create) < 0);
break;
}
}
--
2.20.1