1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/*
* Copyright © 2013 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.
*
* Authors:
* Chris Wilson <chris@chris-wilson.co.uk>
*
*/
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include "drm.h"
#include "i915/gem.h"
#include "i915/gem_create.h"
#include "i915/gem_mman.h"
#include "igt.h"
#include "igt_aux.h"
#include "igt_device_scan.h"
/**
* TEST: gem close race
* Description: Test try to race gem_close against workload submission.
* Category: Core
* Mega feature: General Core features
* Sub-category: CMD submission
* Functionality: buffer management
* Feature: synchronization
* Test category: GEM_Legacy
*
* SUBTEST: basic-process
* Description: Basic workload submission.
*
* SUBTEST: basic-threads
* Description: Share buffer handle across different drm fd's and trying to
* race gem_close against continuous workload with minimum timeout.
*
* SUBTEST: contexts
* Description: Share buffer handle across different drm fd's and trying to
* race gem_close against continuous workload in other contexts.
*
* SUBTEST: gem-close-race
* Description: Share buffer handle across different drm fd's and trying to
* race of gem_close against continuous workload.
*
* SUBTEST: multigpu-basic-process
* Description: Basic workload submission on multi-GPU machine.
* Sub-category: MultiGPU
* Functionality: buffer management on MultiGPU
* Feature: multigpu, synchronization
*
* SUBTEST: multigpu-basic-threads
* Description: Run basic-threads race on multi-GPU machine.
* Sub-category: MultiGPU
* Functionality: buffer management on MultiGPU
* Feature: multigpu, synchronization
*
* SUBTEST: process-exit
* Description: Test try to race gem_close against submission of continuous workload.
*/
#define OBJECT_SIZE (256 * 1024)
#define COPY_BLT_CMD (2<<29|0x53<<22|0x6)
#define BLT_WRITE_ALPHA (1<<21)
#define BLT_WRITE_RGB (1<<20)
IGT_TEST_DESCRIPTION("Test try to race gem_close against workload submission.");
static uint32_t devid;
static bool has_64bit_relocations;
static bool has_softpin;
static uint64_t exec_addr;
static uint64_t data_addr;
static void selfcopy(int fd, uint32_t ctx, uint32_t handle, int loops)
{
struct drm_i915_gem_relocation_entry reloc[2];
struct drm_i915_gem_exec_object2 gem_exec[2];
struct drm_i915_gem_execbuffer2 execbuf;
struct drm_i915_gem_create create;
uint32_t buf[16], *b = buf;
int err;
memset(reloc, 0, sizeof(reloc));
*b = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
if (has_64bit_relocations)
*b += 2;
b++;
*b++ = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
*b++ = 0;
*b++ = 1 << 16 | 1024;
reloc[0].offset = (b - buf) * sizeof(*b);
reloc[0].target_handle = handle;
reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
reloc[0].presumed_offset = data_addr;
*b++ = data_addr;
if (has_64bit_relocations)
*b++ = CANONICAL(data_addr) >> 32;
*b++ = 512 << 16;
*b++ = 4*1024;
reloc[1].offset = (b - buf) * sizeof(*b);
reloc[1].target_handle = handle;
reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
reloc[1].write_domain = 0;
reloc[1].presumed_offset = data_addr;
*b++ = data_addr;
if (has_64bit_relocations)
*b++ = CANONICAL(data_addr) >> 32;
*b++ = MI_BATCH_BUFFER_END;
*b++ = 0;
memset(gem_exec, 0, sizeof(gem_exec));
gem_exec[0].handle = handle;
memset(&create, 0, sizeof(create));
create.handle = 0;
create.size = 4096;
drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
gem_exec[1].handle = create.handle;
gem_exec[1].offset = CANONICAL(exec_addr);
gem_exec[0].offset = CANONICAL(data_addr);
if (has_softpin) {
gem_exec[1].flags |= EXEC_OBJECT_PINNED | EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
gem_exec[0].flags |= EXEC_OBJECT_PINNED | EXEC_OBJECT_WRITE |
EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
} else {
gem_exec[1].relocation_count = 2;
gem_exec[1].relocs_ptr = to_user_pointer(reloc);
}
memset(&execbuf, 0, sizeof(execbuf));
execbuf.flags |= I915_EXEC_NO_RELOC;
execbuf.buffers_ptr = to_user_pointer(gem_exec);
execbuf.buffer_count = 2;
execbuf.batch_len = (b - buf) * sizeof(*b);
if (HAS_BLT_RING(devid))
execbuf.flags |= I915_EXEC_BLT;
execbuf.rsvd1 = ctx;
err = __gem_write(fd, create.handle, 0, buf, sizeof(buf));
if (err == -EOPNOTSUPP) {
void *ptr;
ptr = __gem_mmap__device_coherent(fd, create.handle, 0, sizeof(buf), PROT_WRITE);
if (!ptr) {
err = errno;
} else {
memcpy(ptr, buf, sizeof(buf));
gem_munmap(ptr, sizeof(buf));
}
}
if (!err)
while (loops-- && __gem_execbuf(fd, &execbuf) == 0)
;
drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
}
static uint32_t load(int fd)
{
uint32_t handle;
handle = gem_create(fd, OBJECT_SIZE);
if (handle == 0)
return 0;
selfcopy(fd, 0, handle, 100);
return handle;
}
static void process(int fd, int child)
{
uint32_t handle;
fd = drm_reopen_driver(fd);
handle = load(fd);
if ((child & 63) == 63)
gem_read(fd, handle, 0, &handle, sizeof(handle));
/* leave fd to be closed by process termination */
}
struct crashme {
int fd;
} crashme;
static void crashme_now(int sig)
{
close(crashme.fd);
}
#define usec(x) (1000*(x))
#define msec(x) usec(1000*(x))
static void thread(int fd, struct drm_gem_open name,
int timeout, unsigned int flags)
#define CONTEXTS 0x1
{
struct sigevent sev;
struct sigaction act;
struct itimerspec its;
uint32_t *history;
#define N_HISTORY (256)
timer_t timer;
history = malloc(sizeof(*history) * N_HISTORY);
igt_assert(history);
memset(&act, 0, sizeof(act));
act.sa_handler = crashme_now;
igt_assert(sigaction(SIGRTMIN, &act, NULL) == 0);
memset(&sev, 0, sizeof(sev));
sev.sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID;
sev.sigev_notify_thread_id = gettid();
sev.sigev_signo = SIGRTMIN;
igt_assert(timer_create(CLOCK_MONOTONIC, &sev, &timer) == 0);
igt_until_timeout(timeout) {
unsigned int n = 0;
memset(history, 0, sizeof(*history) * N_HISTORY);
crashme.fd = drm_reopen_driver(fd);
memset(&its, 0, sizeof(its));
its.it_value.tv_nsec = msec(1) + (rand() % msec(150));
igt_assert(timer_settime(timer, 0, &its, NULL) == 0);
do {
uint32_t ctx = 0;
if (drmIoctl(crashme.fd,
DRM_IOCTL_GEM_OPEN,
&name))
break;
if (flags & CONTEXTS)
__gem_context_create(crashme.fd, &ctx);
selfcopy(crashme.fd, ctx, name.handle, 1);
ctx = history[n % N_HISTORY];
if (ctx)
drmIoctl(crashme.fd,
DRM_IOCTL_GEM_CLOSE,
&ctx);
history[n % N_HISTORY] = name.handle;
n++;
} while (1);
/* leave fd to be closed by process termination */
}
timer_delete(timer);
free(history);
}
static void multigpu_threads(int timeout, unsigned int flags, int gpu_count)
{
int size = sysconf(_SC_NPROCESSORS_ONLN);
size /= gpu_count;
if (size < 1)
size = 1;
igt_multi_fork(gpu, gpu_count) {
struct drm_gem_open name;
int fd = __drm_open_driver_another(gpu, DRIVER_INTEL);
igt_assert_lt(0, fd);
igt_fork(child, size)
thread(fd, name, timeout, flags);
igt_waitchildren();
gem_quiescent_gpu(fd);
drm_close_driver(fd);
}
igt_waitchildren();
}
static void threads(int timeout, unsigned int flags)
{
struct drm_gem_open name;
int fd;
fd = drm_open_driver(DRIVER_INTEL);
name.name = gem_flink(fd, gem_create(fd, OBJECT_SIZE));
igt_fork(child, sysconf(_SC_NPROCESSORS_ONLN))
thread(fd, name, timeout, flags);
igt_waitchildren();
gem_quiescent_gpu(fd);
drm_close_driver(fd);
}
igt_main
{
int gpu_count;
igt_fixture {
int fd;
fd = drm_open_driver(DRIVER_INTEL);
igt_require_gem(fd);
devid = intel_get_drm_devid(fd);
has_64bit_relocations = intel_gen(devid) >= 8;
has_softpin = !gem_has_relocations(fd);
exec_addr = gem_detect_safe_start_offset(fd);
data_addr = gem_detect_safe_alignment(fd);
exec_addr = max_t(exec_addr, exec_addr, data_addr);
data_addr += exec_addr;
gpu_count = igt_device_filter_count();
igt_fork_hang_detector(fd);
drm_close_driver(fd);
}
igt_describe("Basic workload submission.");
igt_subtest("basic-process") {
int fd = drm_open_driver(DRIVER_INTEL);
igt_fork(child, 1)
process(fd, child);
igt_waitchildren();
gem_quiescent_gpu(fd);
drm_close_driver(fd);
}
igt_describe("Basic workload submission on multi-GPU machine.");
igt_subtest("multigpu-basic-process") {
igt_require(gpu_count > 1);
igt_multi_fork(child, gpu_count) {
int fd = __drm_open_driver_another(child, DRIVER_INTEL);
igt_assert(fd > 0);
process(fd, child);
gem_quiescent_gpu(fd);
drm_close_driver(fd);
}
igt_waitchildren();
}
igt_describe("Share buffer handle across different drm fd's and trying to race "
" gem_close against continuous workload with minimum timeout.");
igt_subtest("basic-threads")
threads(1, 0);
igt_describe("Run basic-threads race on multi-GPU machine.");
igt_subtest("multigpu-basic-threads") {
igt_require(gpu_count > 1);
multigpu_threads(1, 0, gpu_count);
}
igt_describe("Test try to race gem_close against submission of continuous"
" workload.");
igt_subtest("process-exit") {
int fd = drm_open_driver(DRIVER_INTEL);
igt_fork(child, 768)
process(fd, child);
igt_waitchildren();
gem_quiescent_gpu(fd);
drm_close_driver(fd);
}
igt_describe("Share buffer handle across different drm fd's and trying to race"
" gem_close against continuous workload in other contexts.");
igt_subtest("contexts")
threads(30, CONTEXTS);
igt_describe("Share buffer handle across different drm fd's and trying to race of"
" gem_close against continuous workload.");
igt_subtest("gem-close-race")
threads(150, 0);
igt_fixture
igt_stop_hang_detector();
}
|