summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMaarten Lankhorst <maarten.lankhorst@ubuntu.com>2015-02-26 11:54:03 +0100
committerMaarten Lankhorst <dev@mblankhorst.nl>2015-03-13 20:26:33 +0100
commit5ea6f1c32628887c9df0c53bc8c199eb12633fec (patch)
treef466d5ed08d547ebb20da6ccc6fa1cbb2efc3785 /tests
parent7caa442e761ab5e48698c937aea9ce18f4522ecb (diff)
nouveau: make nouveau importing global buffers completely thread-safe, with tests
While I've closed off most races in a previous patch, a small race still existed where importing then unreffing cound cause an invalid bo. Add a test for this case. Racing sequence fixed: - thread 1 releases bo, refcount drops to zero, blocks on acquiring nvdev->lock. - thread 2 increases refcount to 1. - thread 2 decreases refcount to zero, blocks on acquiring nvdev->lock. At this point the 2 threads will clean up the same bo. Signed-off-by: Maarten Lankhorst <maarten.lankhorst@ubuntu.com> Reviewed-By: Emil Velikov <emil.l.velikov@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am4
-rw-r--r--tests/nouveau/.gitignore1
-rw-r--r--tests/nouveau/Makefile.am16
-rw-r--r--tests/nouveau/threaded.c156
4 files changed, 177 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d5269f94..94fcc080 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -31,6 +31,10 @@ check_PROGRAMS = \
dristat \
drmstat
+if HAVE_NOUVEAU
+SUBDIRS += nouveau
+endif
+
if HAVE_LIBUDEV
check_LTLIBRARIES = libdrmtest.la
diff --git a/tests/nouveau/.gitignore b/tests/nouveau/.gitignore
new file mode 100644
index 00000000..837bfb91
--- /dev/null
+++ b/tests/nouveau/.gitignore
@@ -0,0 +1 @@
+threaded
diff --git a/tests/nouveau/Makefile.am b/tests/nouveau/Makefile.am
new file mode 100644
index 00000000..c4f6e299
--- /dev/null
+++ b/tests/nouveau/Makefile.am
@@ -0,0 +1,16 @@
+AM_CPPFLAGS = \
+ -I$(top_srcdir)/include/drm \
+ -I$(top_srcdir)/nouveau \
+ -I$(top_srcdir)
+
+AM_CFLAGS = $(WARN_CFLAGS)
+
+LDADD = \
+ ../../nouveau/libdrm_nouveau.la \
+ ../../libdrm.la \
+ -ldl -lpthread
+
+TESTS = threaded
+
+check_PROGRAMS = $(TESTS)
+
diff --git a/tests/nouveau/threaded.c b/tests/nouveau/threaded.c
new file mode 100644
index 00000000..281af460
--- /dev/null
+++ b/tests/nouveau/threaded.c
@@ -0,0 +1,156 @@
+/*
+ * Copyright © 2015 Canonical Ltd. (Maarten Lankhorst)
+ *
+ * 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 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <sys/ioctl.h>
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <pthread.h>
+
+#include "xf86drm.h"
+#include "nouveau.h"
+
+static typeof(ioctl) *old_ioctl;
+static int failed;
+
+static int import_fd;
+
+int ioctl(int fd, unsigned long request, ...)
+{
+ va_list va;
+ int ret;
+ void *arg;
+
+ va_start(va, request);
+ arg = va_arg(va, void *);
+ ret = old_ioctl(fd, request, arg);
+ va_end(va);
+
+ if (ret < 0 && request == DRM_IOCTL_GEM_CLOSE && errno == EINVAL)
+ failed = 1;
+
+ return ret;
+}
+
+static void *
+openclose(void *dev)
+{
+ struct nouveau_device *nvdev = dev;
+ struct nouveau_bo *bo = NULL;
+ int i;
+
+ for (i = 0; i < 100000; ++i) {
+ if (!nouveau_bo_prime_handle_ref(nvdev, import_fd, &bo))
+ nouveau_bo_ref(NULL, &bo);
+ }
+ return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+ drmVersionPtr version;
+ const char *device = NULL;
+ int err, fd, fd2;
+ struct nouveau_device *nvdev, *nvdev2;
+ struct nouveau_bo *bo;
+ pthread_t t1, t2;
+
+ old_ioctl = dlsym(RTLD_NEXT, "ioctl");
+
+ if (argc < 2) {
+ fd = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
+ if (fd >= 0)
+ fd2 = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
+ } else {
+ device = argv[1];
+
+ fd = open(device, O_RDWR);
+ if (fd >= 0)
+ fd2 = open(device, O_RDWR);
+ else
+ fd2 = fd = -errno;
+ }
+
+ if (fd < 0) {
+ fprintf(stderr, "Opening nouveau render node failed with %i\n", fd);
+ return device ? -fd : 77;
+ }
+
+ if (fd2 < 0) {
+ fprintf(stderr, "Opening second nouveau render node failed with %i\n", -errno);
+ return errno;
+ }
+
+ version = drmGetVersion(fd);
+ if (version) {
+ printf("Version: %d.%d.%d\n", version->version_major,
+ version->version_minor, version->version_patchlevel);
+ printf(" Name: %s\n", version->name);
+ printf(" Date: %s\n", version->date);
+ printf(" Description: %s\n", version->desc);
+
+ drmFreeVersion(version);
+ }
+
+ err = nouveau_device_wrap(fd, 0, &nvdev);
+ if (!err)
+ err = nouveau_device_wrap(fd2, 0, &nvdev2);
+ if (err < 0)
+ return 1;
+
+ err = nouveau_bo_new(nvdev2, NOUVEAU_BO_GART, 0, 4096, NULL, &bo);
+ if (!err)
+ err = nouveau_bo_set_prime(bo, &import_fd);
+
+ if (!err) {
+ pthread_create(&t1, NULL, openclose, nvdev);
+ pthread_create(&t2, NULL, openclose, nvdev);
+ }
+
+ pthread_join(t1, NULL);
+ pthread_join(t2, NULL);
+
+ close(import_fd);
+ nouveau_bo_ref(NULL, &bo);
+
+ nouveau_device_del(&nvdev2);
+ nouveau_device_del(&nvdev);
+ if (device) {
+ close(fd2);
+ close(fd);
+ } else {
+ drmClose(fd2);
+ drmClose(fd);
+ }
+
+ if (failed)
+ fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed with EINVAL,\n"
+ "race in opening/closing bo is likely.\n");
+
+ return failed;
+}