threaded.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright © 2015 Canonical Ltd. (Maarten Lankhorst)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <sys/ioctl.h>
  23. #include <dlfcn.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <pthread.h>
  29. #include "xf86drm.h"
  30. #include "nouveau.h"
  31. static __typeof__(ioctl) *old_ioctl;
  32. static int failed;
  33. static int import_fd;
  34. #if defined(__GLIBC__) || defined(__FreeBSD__)
  35. int ioctl(int fd, unsigned long request, ...)
  36. #else
  37. int ioctl(int fd, int request, ...)
  38. #endif
  39. {
  40. va_list va;
  41. int ret;
  42. void *arg;
  43. va_start(va, request);
  44. arg = va_arg(va, void *);
  45. ret = old_ioctl(fd, request, arg);
  46. va_end(va);
  47. if (ret < 0 && request == DRM_IOCTL_GEM_CLOSE && errno == EINVAL)
  48. failed = 1;
  49. return ret;
  50. }
  51. static void *
  52. openclose(void *dev)
  53. {
  54. struct nouveau_device *nvdev = dev;
  55. struct nouveau_bo *bo = NULL;
  56. int i;
  57. for (i = 0; i < 100000; ++i) {
  58. if (!nouveau_bo_prime_handle_ref(nvdev, import_fd, &bo))
  59. nouveau_bo_ref(NULL, &bo);
  60. }
  61. return NULL;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. drmVersionPtr version;
  66. const char *device = NULL;
  67. int err, fd, fd2;
  68. struct nouveau_device *nvdev, *nvdev2;
  69. struct nouveau_bo *bo;
  70. pthread_t t1, t2;
  71. old_ioctl = dlsym(RTLD_NEXT, "ioctl");
  72. if (argc < 2) {
  73. fd = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
  74. if (fd >= 0)
  75. fd2 = drmOpenWithType("nouveau", NULL, DRM_NODE_RENDER);
  76. } else {
  77. device = argv[1];
  78. fd = open(device, O_RDWR);
  79. if (fd >= 0)
  80. fd2 = open(device, O_RDWR);
  81. else
  82. fd2 = fd = -errno;
  83. }
  84. if (fd < 0) {
  85. fprintf(stderr, "Opening nouveau render node failed with %i\n", fd);
  86. return device ? -fd : 77;
  87. }
  88. if (fd2 < 0) {
  89. fprintf(stderr, "Opening second nouveau render node failed with %i\n", -errno);
  90. return errno;
  91. }
  92. version = drmGetVersion(fd);
  93. if (version) {
  94. printf("Version: %d.%d.%d\n", version->version_major,
  95. version->version_minor, version->version_patchlevel);
  96. printf(" Name: %s\n", version->name);
  97. printf(" Date: %s\n", version->date);
  98. printf(" Description: %s\n", version->desc);
  99. drmFreeVersion(version);
  100. }
  101. err = nouveau_device_wrap(fd, 0, &nvdev);
  102. if (!err)
  103. err = nouveau_device_wrap(fd2, 0, &nvdev2);
  104. if (err < 0)
  105. return 1;
  106. err = nouveau_bo_new(nvdev2, NOUVEAU_BO_GART, 0, 4096, NULL, &bo);
  107. if (!err)
  108. err = nouveau_bo_set_prime(bo, &import_fd);
  109. if (!err) {
  110. pthread_create(&t1, NULL, openclose, nvdev);
  111. pthread_create(&t2, NULL, openclose, nvdev);
  112. }
  113. pthread_join(t1, NULL);
  114. pthread_join(t2, NULL);
  115. close(import_fd);
  116. nouveau_bo_ref(NULL, &bo);
  117. nouveau_device_del(&nvdev2);
  118. nouveau_device_del(&nvdev);
  119. if (device) {
  120. close(fd2);
  121. close(fd);
  122. } else {
  123. drmClose(fd2);
  124. drmClose(fd);
  125. }
  126. if (failed)
  127. fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed with EINVAL,\n"
  128. "race in opening/closing bo is likely.\n");
  129. return failed;
  130. }