Sanity test existing persistence and new exciting non-persistent context
behaviour.

Signed-off-by: Chris Wilson <ch...@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahti...@linux.intel.com>
Cc: Michał Winiarski <michal.winiar...@intel.com>
Cc: Jon Bloomfield <jon.bloomfi...@intel.com>
---
 lib/i915/gem_context.c           |  37 +++++
 lib/i915/gem_context.h           |   9 ++
 lib/ioctl_wrappers.c             |   1 +
 tests/Makefile.sources           |   3 +
 tests/i915/gem_ctx_persistence.c | 241 +++++++++++++++++++++++++++++++
 tests/meson.build                |   1 +
 6 files changed, 292 insertions(+)
 create mode 100644 tests/i915/gem_ctx_persistence.c

diff --git a/lib/i915/gem_context.c b/lib/i915/gem_context.c
index 83c5df961..1fae5191f 100644
--- a/lib/i915/gem_context.c
+++ b/lib/i915/gem_context.c
@@ -272,6 +272,43 @@ void gem_context_set_priority(int fd, uint32_t ctx_id, int 
prio)
        igt_assert_eq(__gem_context_set_priority(fd, ctx_id, prio), 0);
 }
 
+/**
+ * __gem_context_set_persistence:
+ * @i915: open i915 drm file descriptor
+ * @ctx: i915 context id
+ * @state: desired persistence
+ *
+ * Declare whether this context is allowed to persist after closing until
+ * its requests are complete (persistent=true) or if it should be
+ * immediately reaped on closing and its requests cancelled
+ * (persistent=false).
+ *
+ * Returns: An integer equal to zero for success and negative for failure
+ */
+int __gem_context_set_persistence(int i915, uint32_t ctx, bool state)
+{
+       struct drm_i915_gem_context_param p = {
+               .ctx_id = ctx,
+               .param = I915_CONTEXT_PARAM_PERSISTENCE,
+               .value = state,
+       };
+
+       return __gem_context_set_param(i915, &p);
+}
+
+/**
+ * __gem_context_set_persistence:
+ * @i915: open i915 drm file descriptor
+ * @ctx: i915 context id
+ * @state: desired persistence
+ *
+ * Like __gem_context_set_persistence(), except we assert on failure.
+ */
+void gem_context_set_persistence(int i915, uint32_t ctx, bool state)
+{
+       igt_assert_eq(__gem_context_set_persistence(i915, ctx, state), 0);
+}
+
 int
 __gem_context_clone(int i915,
                    uint32_t src, unsigned int share,
diff --git a/lib/i915/gem_context.h b/lib/i915/gem_context.h
index 8043c3401..6745c90c3 100644
--- a/lib/i915/gem_context.h
+++ b/lib/i915/gem_context.h
@@ -24,6 +24,11 @@
 #ifndef GEM_CONTEXT_H
 #define GEM_CONTEXT_H
 
+#include <stdbool.h>
+#include <stdint.h>
+
+struct drm_i915_gem_context_param;
+
 uint32_t gem_context_create(int fd);
 int __gem_context_create(int fd, uint32_t *ctx_id);
 void gem_context_destroy(int fd, uint32_t ctx_id);
@@ -58,6 +63,10 @@ int __gem_context_get_param(int fd, struct 
drm_i915_gem_context_param *p);
 int __gem_context_set_priority(int fd, uint32_t ctx, int prio);
 void gem_context_set_priority(int fd, uint32_t ctx, int prio);
 
+#define I915_CONTEXT_PARAM_PERSISTENCE 0xb
+int __gem_context_set_persistence(int i915, uint32_t ctx, bool state);
+void gem_context_set_persistence(int i915, uint32_t ctx, bool state);
+
 bool gem_context_has_engine(int fd, uint32_t ctx, uint64_t engine);
 
 #endif /* GEM_CONTEXT_H */
diff --git a/lib/ioctl_wrappers.c b/lib/ioctl_wrappers.c
index 280fdd624..628f8b830 100644
--- a/lib/ioctl_wrappers.c
+++ b/lib/ioctl_wrappers.c
@@ -445,6 +445,7 @@ int gem_wait(int fd, uint32_t handle, int64_t *timeout_ns)
        ret = 0;
        if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_WAIT, &wait))
                ret = -errno;
+       errno = 0;
 
        if (timeout_ns)
                *timeout_ns = wait.timeout_ns;
diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index 41a10c653..f361dc030 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -152,6 +152,9 @@ gem_ctx_isolation_SOURCES = i915/gem_ctx_isolation.c
 TESTS_progs += gem_ctx_param
 gem_ctx_param_SOURCES = i915/gem_ctx_param.c
 
+TESTS_progs += gem_ctx_persistence
+gem_ctx_persistence_SOURCES = i915/gem_ctx_ersistence.c
+
 TESTS_progs += gem_ctx_shared
 gem_ctx_shared_SOURCES = i915/gem_ctx_shared.c
 
diff --git a/tests/i915/gem_ctx_persistence.c b/tests/i915/gem_ctx_persistence.c
new file mode 100644
index 000000000..ec6add135
--- /dev/null
+++ b/tests/i915/gem_ctx_persistence.c
@@ -0,0 +1,241 @@
+/*
+ * 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 <errno.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "drmtest.h" /* gem_quiescent_gpu()! */
+#include "i915/gem_context.h"
+#include "i915/gem_submission.h"
+#include "igt_dummyload.h"
+#include "ioctl_wrappers.h" /* gem_wait()! */
+#include "sw_sync.h"
+
+static bool has_persistence(int i915)
+{
+       struct drm_i915_gem_context_param p = {
+               .param = I915_CONTEXT_PARAM_PERSISTENCE,
+       };
+       uint64_t saved;
+
+       if (__gem_context_get_param(i915, &p))
+               return false;
+
+       saved = p.value;
+       p.value = 0;
+       if (__gem_context_set_param(i915, &p))
+               return false;
+
+       p.value = saved;
+       return __gem_context_set_param(i915, &p) == 0;
+}
+
+static void test_idempotent(int i915)
+{
+       struct drm_i915_gem_context_param p = {
+               .param = I915_CONTEXT_PARAM_PERSISTENCE,
+       };
+       int expected;
+
+       gem_context_get_param(i915, &p);
+       expected = !!p.value;
+
+       expected = !expected;
+       p.value = expected;
+       gem_context_set_param(i915, &p);
+       gem_context_get_param(i915, &p);
+       igt_assert_eq(p.value, expected);
+
+       expected = !expected; /* and restores */
+       p.value = expected;
+       gem_context_set_param(i915, &p);
+       gem_context_get_param(i915, &p);
+       igt_assert_eq(p.value, expected);
+}
+
+static void test_persistence(int i915)
+{
+       igt_spin_t *spin;
+       int64_t timeout;
+       uint32_t ctx;
+
+       ctx = gem_context_create(i915);
+       gem_context_set_persistence(i915, ctx, true);
+
+       spin = igt_spin_new(i915, ctx, .flags = IGT_SPIN_FENCE_OUT);
+       gem_context_destroy(i915, ctx);
+
+       timeout = NSEC_PER_SEC / 5;
+       igt_assert_eq(gem_wait(i915, spin->handle, &timeout), -ETIME);
+
+       igt_spin_end(spin);
+
+       timeout = NSEC_PER_SEC / 5;
+       igt_assert_eq(gem_wait(i915, spin->handle, &timeout), 0);
+       igt_assert_eq(sync_fence_status(spin->out_fence), 1);
+
+       igt_spin_free(i915, spin);
+       gem_quiescent_gpu(i915);
+}
+
+static void test_nonpersistent_cleanup(int i915)
+{
+       int64_t timeout = NSEC_PER_SEC / 5;
+       igt_spin_t *spin;
+       uint32_t ctx;
+
+       ctx = gem_context_create(i915);
+       gem_context_set_persistence(i915, ctx, false);
+
+       spin = igt_spin_new(i915, ctx, .flags = IGT_SPIN_FENCE_OUT);
+       gem_context_destroy(i915, ctx);
+
+       igt_assert_eq(gem_wait(i915, spin->handle, &timeout), 0);
+       igt_assert_eq(sync_fence_status(spin->out_fence), -EIO);
+
+       igt_spin_free(i915, spin);
+       gem_quiescent_gpu(i915);
+}
+
+static void test_nonpersistent_hostile(int i915)
+{
+       int64_t timeout = NSEC_PER_SEC / 2;
+       igt_spin_t *spin;
+       uint32_t ctx;
+
+       ctx = gem_context_create(i915);
+       gem_context_set_persistence(i915, ctx, false);
+
+       spin = igt_spin_new(i915, ctx, .flags = IGT_SPIN_NO_PREEMPTION);
+       gem_context_destroy(i915, ctx);
+
+       igt_assert_eq(gem_wait(i915, spin->handle, &timeout), 0);
+       igt_assert_eq(sync_fence_status(spin->out_fence), -EIO);
+
+       igt_spin_free(i915, spin);
+       gem_quiescent_gpu(i915);
+}
+
+static void sendfd(int socket, int fd)
+{
+       struct iovec io = { .iov_base = (char *)"ABC", .iov_len = 3 };
+       struct msghdr msg = {};
+       char buf[CMSG_SPACE(sizeof(fd))];
+       struct cmsghdr *cmsg;
+
+       msg.msg_iov = &io;
+       msg.msg_iovlen = 1;
+       msg.msg_control = buf;
+       msg.msg_controllen = sizeof(buf);
+
+       cmsg = CMSG_FIRSTHDR(&msg);
+       cmsg->cmsg_level = SOL_SOCKET;
+       cmsg->cmsg_type = SCM_RIGHTS;
+       cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
+
+       *(int *)CMSG_DATA(cmsg) = fd;
+       msg.msg_controllen = cmsg->cmsg_len;
+
+       igt_assert(sendmsg(socket, &msg, 0) != -1);
+}
+
+static int recvfd(int socket)
+{
+       char m_buffer[256], c_buffer[256];
+       struct iovec io = {
+               .iov_base = m_buffer,
+               .iov_len = sizeof(m_buffer),
+       };
+       struct msghdr msg = {
+               .msg_iov = &io,
+               .msg_iovlen = 1,
+               .msg_control = c_buffer,
+               .msg_controllen = sizeof(c_buffer),
+       };
+
+       igt_assert(recvmsg(socket, &msg, 0) != -1);
+       return *(int *)CMSG_DATA(CMSG_FIRSTHDR(&msg));
+}
+
+static void test_nonpersistent_process(int i915)
+{
+       int fence, sv[2];
+
+       igt_require(socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) == 0);
+
+       igt_fork(child, 1) {
+               igt_spin_t *spin;
+
+               i915 = gem_reopen_driver(i915);
+               gem_context_set_persistence(i915, 0, false);
+               spin = igt_spin_new(i915, .flags = IGT_SPIN_FENCE_OUT);
+
+               sendfd(sv[0], spin->out_fence);
+       }
+       igt_waitchildren();
+
+       fence = recvfd(sv[1]);
+       close(sv[0]);
+       close(sv[1]);
+
+       igt_assert_eq(sync_fence_wait(fence, MSEC_PER_SEC / 5), 0);
+       igt_assert_eq(sync_fence_status(fence), -EIO);
+       close(fence);
+
+       gem_quiescent_gpu(i915);
+}
+
+igt_main
+{
+       int i915;
+
+       igt_fixture {
+               i915 = drm_open_driver(DRIVER_INTEL);
+               igt_require_gem(i915);
+
+               igt_require(has_persistence(i915));
+       }
+
+       igt_subtest_group {
+               igt_subtest("idempotent")
+                       test_idempotent(i915);
+
+               igt_subtest("persistence")
+                       test_persistence(i915);
+
+               igt_subtest("cleanup")
+                       test_nonpersistent_cleanup(i915);
+
+               igt_subtest("hostile")
+                       test_nonpersistent_hostile(i915);
+
+               igt_subtest("process")
+                       test_nonpersistent_process(i915);
+       }
+
+       igt_fixture {
+               close(i915);
+       }
+}
diff --git a/tests/meson.build b/tests/meson.build
index ff3ba6d78..e60acd836 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -118,6 +118,7 @@ i915_progs = [
        'gem_ctx_exec',
        'gem_ctx_isolation',
        'gem_ctx_param',
+       'gem_ctx_persistence',
        'gem_ctx_shared',
        'gem_ctx_switch',
        'gem_ctx_thrash',
-- 
2.23.0.rc1

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reply via email to