anv/allocator: Return a null state for zero-size allocations
[mesa.git] / src / intel / vulkan / anv_allocator.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <limits.h>
28 #include <assert.h>
29 #include <linux/futex.h>
30 #include <linux/memfd.h>
31 #include <sys/time.h>
32 #include <sys/mman.h>
33 #include <sys/syscall.h>
34
35 #include "anv_private.h"
36
37 #include "util/hash_table.h"
38
39 #ifdef HAVE_VALGRIND
40 #define VG_NOACCESS_READ(__ptr) ({ \
41 VALGRIND_MAKE_MEM_DEFINED((__ptr), sizeof(*(__ptr))); \
42 __typeof(*(__ptr)) __val = *(__ptr); \
43 VALGRIND_MAKE_MEM_NOACCESS((__ptr), sizeof(*(__ptr)));\
44 __val; \
45 })
46 #define VG_NOACCESS_WRITE(__ptr, __val) ({ \
47 VALGRIND_MAKE_MEM_UNDEFINED((__ptr), sizeof(*(__ptr))); \
48 *(__ptr) = (__val); \
49 VALGRIND_MAKE_MEM_NOACCESS((__ptr), sizeof(*(__ptr))); \
50 })
51 #else
52 #define VG_NOACCESS_READ(__ptr) (*(__ptr))
53 #define VG_NOACCESS_WRITE(__ptr, __val) (*(__ptr) = (__val))
54 #endif
55
56 /* Design goals:
57 *
58 * - Lock free (except when resizing underlying bos)
59 *
60 * - Constant time allocation with typically only one atomic
61 *
62 * - Multiple allocation sizes without fragmentation
63 *
64 * - Can grow while keeping addresses and offset of contents stable
65 *
66 * - All allocations within one bo so we can point one of the
67 * STATE_BASE_ADDRESS pointers at it.
68 *
69 * The overall design is a two-level allocator: top level is a fixed size, big
70 * block (8k) allocator, which operates out of a bo. Allocation is done by
71 * either pulling a block from the free list or growing the used range of the
72 * bo. Growing the range may run out of space in the bo which we then need to
73 * grow. Growing the bo is tricky in a multi-threaded, lockless environment:
74 * we need to keep all pointers and contents in the old map valid. GEM bos in
75 * general can't grow, but we use a trick: we create a memfd and use ftruncate
76 * to grow it as necessary. We mmap the new size and then create a gem bo for
77 * it using the new gem userptr ioctl. Without heavy-handed locking around
78 * our allocation fast-path, there isn't really a way to munmap the old mmap,
79 * so we just keep it around until garbage collection time. While the block
80 * allocator is lockless for normal operations, we block other threads trying
81 * to allocate while we're growing the map. It sholdn't happen often, and
82 * growing is fast anyway.
83 *
84 * At the next level we can use various sub-allocators. The state pool is a
85 * pool of smaller, fixed size objects, which operates much like the block
86 * pool. It uses a free list for freeing objects, but when it runs out of
87 * space it just allocates a new block from the block pool. This allocator is
88 * intended for longer lived state objects such as SURFACE_STATE and most
89 * other persistent state objects in the API. We may need to track more info
90 * with these object and a pointer back to the CPU object (eg VkImage). In
91 * those cases we just allocate a slightly bigger object and put the extra
92 * state after the GPU state object.
93 *
94 * The state stream allocator works similar to how the i965 DRI driver streams
95 * all its state. Even with Vulkan, we need to emit transient state (whether
96 * surface state base or dynamic state base), and for that we can just get a
97 * block and fill it up. These cases are local to a command buffer and the
98 * sub-allocator need not be thread safe. The streaming allocator gets a new
99 * block when it runs out of space and chains them together so they can be
100 * easily freed.
101 */
102
103 /* Allocations are always at least 64 byte aligned, so 1 is an invalid value.
104 * We use it to indicate the free list is empty. */
105 #define EMPTY 1
106
107 struct anv_mmap_cleanup {
108 void *map;
109 size_t size;
110 uint32_t gem_handle;
111 };
112
113 #define ANV_MMAP_CLEANUP_INIT ((struct anv_mmap_cleanup){0})
114
115 static inline long
116 sys_futex(void *addr1, int op, int val1,
117 struct timespec *timeout, void *addr2, int val3)
118 {
119 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
120 }
121
122 static inline int
123 futex_wake(uint32_t *addr, int count)
124 {
125 return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
126 }
127
128 static inline int
129 futex_wait(uint32_t *addr, int32_t value)
130 {
131 return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
132 }
133
134 static inline int
135 memfd_create(const char *name, unsigned int flags)
136 {
137 return syscall(SYS_memfd_create, name, flags);
138 }
139
140 static inline uint32_t
141 ilog2_round_up(uint32_t value)
142 {
143 assert(value != 0);
144 return 32 - __builtin_clz(value - 1);
145 }
146
147 static inline uint32_t
148 round_to_power_of_two(uint32_t value)
149 {
150 return 1 << ilog2_round_up(value);
151 }
152
153 static bool
154 anv_free_list_pop(union anv_free_list *list, void **map, int32_t *offset)
155 {
156 union anv_free_list current, new, old;
157
158 current.u64 = list->u64;
159 while (current.offset != EMPTY) {
160 /* We have to add a memory barrier here so that the list head (and
161 * offset) gets read before we read the map pointer. This way we
162 * know that the map pointer is valid for the given offset at the
163 * point where we read it.
164 */
165 __sync_synchronize();
166
167 int32_t *next_ptr = *map + current.offset;
168 new.offset = VG_NOACCESS_READ(next_ptr);
169 new.count = current.count + 1;
170 old.u64 = __sync_val_compare_and_swap(&list->u64, current.u64, new.u64);
171 if (old.u64 == current.u64) {
172 *offset = current.offset;
173 return true;
174 }
175 current = old;
176 }
177
178 return false;
179 }
180
181 static void
182 anv_free_list_push(union anv_free_list *list, void *map, int32_t offset)
183 {
184 union anv_free_list current, old, new;
185 int32_t *next_ptr = map + offset;
186
187 old = *list;
188 do {
189 current = old;
190 VG_NOACCESS_WRITE(next_ptr, current.offset);
191 new.offset = offset;
192 new.count = current.count + 1;
193 old.u64 = __sync_val_compare_and_swap(&list->u64, current.u64, new.u64);
194 } while (old.u64 != current.u64);
195 }
196
197 /* All pointers in the ptr_free_list are assumed to be page-aligned. This
198 * means that the bottom 12 bits should all be zero.
199 */
200 #define PFL_COUNT(x) ((uintptr_t)(x) & 0xfff)
201 #define PFL_PTR(x) ((void *)((uintptr_t)(x) & ~(uintptr_t)0xfff))
202 #define PFL_PACK(ptr, count) ({ \
203 (void *)(((uintptr_t)(ptr) & ~(uintptr_t)0xfff) | ((count) & 0xfff)); \
204 })
205
206 static bool
207 anv_ptr_free_list_pop(void **list, void **elem)
208 {
209 void *current = *list;
210 while (PFL_PTR(current) != NULL) {
211 void **next_ptr = PFL_PTR(current);
212 void *new_ptr = VG_NOACCESS_READ(next_ptr);
213 unsigned new_count = PFL_COUNT(current) + 1;
214 void *new = PFL_PACK(new_ptr, new_count);
215 void *old = __sync_val_compare_and_swap(list, current, new);
216 if (old == current) {
217 *elem = PFL_PTR(current);
218 return true;
219 }
220 current = old;
221 }
222
223 return false;
224 }
225
226 static void
227 anv_ptr_free_list_push(void **list, void *elem)
228 {
229 void *old, *current;
230 void **next_ptr = elem;
231
232 /* The pointer-based free list requires that the pointer be
233 * page-aligned. This is because we use the bottom 12 bits of the
234 * pointer to store a counter to solve the ABA concurrency problem.
235 */
236 assert(((uintptr_t)elem & 0xfff) == 0);
237
238 old = *list;
239 do {
240 current = old;
241 VG_NOACCESS_WRITE(next_ptr, PFL_PTR(current));
242 unsigned new_count = PFL_COUNT(current) + 1;
243 void *new = PFL_PACK(elem, new_count);
244 old = __sync_val_compare_and_swap(list, current, new);
245 } while (old != current);
246 }
247
248 static uint32_t
249 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state);
250
251 VkResult
252 anv_block_pool_init(struct anv_block_pool *pool,
253 struct anv_device *device, uint32_t block_size)
254 {
255 VkResult result;
256
257 assert(util_is_power_of_two(block_size));
258
259 pool->device = device;
260 anv_bo_init(&pool->bo, 0, 0);
261 pool->block_size = block_size;
262 pool->free_list = ANV_FREE_LIST_EMPTY;
263 pool->back_free_list = ANV_FREE_LIST_EMPTY;
264
265 pool->fd = memfd_create("block pool", MFD_CLOEXEC);
266 if (pool->fd == -1)
267 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
268
269 /* Just make it 2GB up-front. The Linux kernel won't actually back it
270 * with pages until we either map and fault on one of them or we use
271 * userptr and send a chunk of it off to the GPU.
272 */
273 if (ftruncate(pool->fd, BLOCK_POOL_MEMFD_SIZE) == -1) {
274 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
275 goto fail_fd;
276 }
277
278 if (!u_vector_init(&pool->mmap_cleanups,
279 round_to_power_of_two(sizeof(struct anv_mmap_cleanup)),
280 128)) {
281 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
282 goto fail_fd;
283 }
284
285 pool->state.next = 0;
286 pool->state.end = 0;
287 pool->back_state.next = 0;
288 pool->back_state.end = 0;
289
290 /* Immediately grow the pool so we'll have a backing bo. */
291 pool->state.end = anv_block_pool_grow(pool, &pool->state);
292
293 return VK_SUCCESS;
294
295 fail_fd:
296 close(pool->fd);
297
298 return result;
299 }
300
301 void
302 anv_block_pool_finish(struct anv_block_pool *pool)
303 {
304 struct anv_mmap_cleanup *cleanup;
305
306 u_vector_foreach(cleanup, &pool->mmap_cleanups) {
307 if (cleanup->map)
308 munmap(cleanup->map, cleanup->size);
309 if (cleanup->gem_handle)
310 anv_gem_close(pool->device, cleanup->gem_handle);
311 }
312
313 u_vector_finish(&pool->mmap_cleanups);
314
315 close(pool->fd);
316 }
317
318 #define PAGE_SIZE 4096
319
320 /** Grows and re-centers the block pool.
321 *
322 * We grow the block pool in one or both directions in such a way that the
323 * following conditions are met:
324 *
325 * 1) The size of the entire pool is always a power of two.
326 *
327 * 2) The pool only grows on both ends. Neither end can get
328 * shortened.
329 *
330 * 3) At the end of the allocation, we have about twice as much space
331 * allocated for each end as we have used. This way the pool doesn't
332 * grow too far in one direction or the other.
333 *
334 * 4) If the _alloc_back() has never been called, then the back portion of
335 * the pool retains a size of zero. (This makes it easier for users of
336 * the block pool that only want a one-sided pool.)
337 *
338 * 5) We have enough space allocated for at least one more block in
339 * whichever side `state` points to.
340 *
341 * 6) The center of the pool is always aligned to both the block_size of
342 * the pool and a 4K CPU page.
343 */
344 static uint32_t
345 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
346 {
347 size_t size;
348 void *map;
349 uint32_t gem_handle;
350 struct anv_mmap_cleanup *cleanup;
351
352 pthread_mutex_lock(&pool->device->mutex);
353
354 assert(state == &pool->state || state == &pool->back_state);
355
356 /* Gather a little usage information on the pool. Since we may have
357 * threadsd waiting in queue to get some storage while we resize, it's
358 * actually possible that total_used will be larger than old_size. In
359 * particular, block_pool_alloc() increments state->next prior to
360 * calling block_pool_grow, so this ensures that we get enough space for
361 * which ever side tries to grow the pool.
362 *
363 * We align to a page size because it makes it easier to do our
364 * calculations later in such a way that we state page-aigned.
365 */
366 uint32_t back_used = align_u32(pool->back_state.next, PAGE_SIZE);
367 uint32_t front_used = align_u32(pool->state.next, PAGE_SIZE);
368 uint32_t total_used = front_used + back_used;
369
370 assert(state == &pool->state || back_used > 0);
371
372 size_t old_size = pool->bo.size;
373
374 if (old_size != 0 &&
375 back_used * 2 <= pool->center_bo_offset &&
376 front_used * 2 <= (old_size - pool->center_bo_offset)) {
377 /* If we're in this case then this isn't the firsta allocation and we
378 * already have enough space on both sides to hold double what we
379 * have allocated. There's nothing for us to do.
380 */
381 goto done;
382 }
383
384 if (old_size == 0) {
385 /* This is the first allocation */
386 size = MAX2(32 * pool->block_size, PAGE_SIZE);
387 } else {
388 size = old_size * 2;
389 }
390
391 /* We can't have a block pool bigger than 1GB because we use signed
392 * 32-bit offsets in the free list and we don't want overflow. We
393 * should never need a block pool bigger than 1GB anyway.
394 */
395 assert(size <= (1u << 31));
396
397 /* We compute a new center_bo_offset such that, when we double the size
398 * of the pool, we maintain the ratio of how much is used by each side.
399 * This way things should remain more-or-less balanced.
400 */
401 uint32_t center_bo_offset;
402 if (back_used == 0) {
403 /* If we're in this case then we have never called alloc_back(). In
404 * this case, we want keep the offset at 0 to make things as simple
405 * as possible for users that don't care about back allocations.
406 */
407 center_bo_offset = 0;
408 } else {
409 /* Try to "center" the allocation based on how much is currently in
410 * use on each side of the center line.
411 */
412 center_bo_offset = ((uint64_t)size * back_used) / total_used;
413
414 /* Align down to a multiple of both the block size and page size */
415 uint32_t granularity = MAX2(pool->block_size, PAGE_SIZE);
416 assert(util_is_power_of_two(granularity));
417 center_bo_offset &= ~(granularity - 1);
418
419 assert(center_bo_offset >= back_used);
420
421 /* Make sure we don't shrink the back end of the pool */
422 if (center_bo_offset < pool->back_state.end)
423 center_bo_offset = pool->back_state.end;
424
425 /* Make sure that we don't shrink the front end of the pool */
426 if (size - center_bo_offset < pool->state.end)
427 center_bo_offset = size - pool->state.end;
428 }
429
430 assert(center_bo_offset % pool->block_size == 0);
431 assert(center_bo_offset % PAGE_SIZE == 0);
432
433 /* Assert that we only ever grow the pool */
434 assert(center_bo_offset >= pool->back_state.end);
435 assert(size - center_bo_offset >= pool->state.end);
436
437 cleanup = u_vector_add(&pool->mmap_cleanups);
438 if (!cleanup)
439 goto fail;
440 *cleanup = ANV_MMAP_CLEANUP_INIT;
441
442 /* Just leak the old map until we destroy the pool. We can't munmap it
443 * without races or imposing locking on the block allocate fast path. On
444 * the whole the leaked maps adds up to less than the size of the
445 * current map. MAP_POPULATE seems like the right thing to do, but we
446 * should try to get some numbers.
447 */
448 map = mmap(NULL, size, PROT_READ | PROT_WRITE,
449 MAP_SHARED | MAP_POPULATE, pool->fd,
450 BLOCK_POOL_MEMFD_CENTER - center_bo_offset);
451 cleanup->map = map;
452 cleanup->size = size;
453
454 if (map == MAP_FAILED)
455 goto fail;
456
457 gem_handle = anv_gem_userptr(pool->device, map, size);
458 if (gem_handle == 0)
459 goto fail;
460 cleanup->gem_handle = gem_handle;
461
462 #if 0
463 /* Regular objects are created I915_CACHING_CACHED on LLC platforms and
464 * I915_CACHING_NONE on non-LLC platforms. However, userptr objects are
465 * always created as I915_CACHING_CACHED, which on non-LLC means
466 * snooped. That can be useful but comes with a bit of overheard. Since
467 * we're eplicitly clflushing and don't want the overhead we need to turn
468 * it off. */
469 if (!pool->device->info.has_llc) {
470 anv_gem_set_caching(pool->device, gem_handle, I915_CACHING_NONE);
471 anv_gem_set_domain(pool->device, gem_handle,
472 I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
473 }
474 #endif
475
476 /* Now that we successfull allocated everything, we can write the new
477 * values back into pool. */
478 pool->map = map + center_bo_offset;
479 pool->center_bo_offset = center_bo_offset;
480
481 /* For block pool BOs we have to be a bit careful about where we place them
482 * in the GTT. There are two documented workarounds for state base address
483 * placement : Wa32bitGeneralStateOffset and Wa32bitInstructionBaseOffset
484 * which state that those two base addresses do not support 48-bit
485 * addresses and need to be placed in the bottom 32-bit range.
486 * Unfortunately, this is not quite accurate.
487 *
488 * The real problem is that we always set the size of our state pools in
489 * STATE_BASE_ADDRESS to 0xfffff (the maximum) even though the BO is most
490 * likely significantly smaller. We do this because we do not no at the
491 * time we emit STATE_BASE_ADDRESS whether or not we will need to expand
492 * the pool during command buffer building so we don't actually have a
493 * valid final size. If the address + size, as seen by STATE_BASE_ADDRESS
494 * overflows 48 bits, the GPU appears to treat all accesses to the buffer
495 * as being out of bounds and returns zero. For dynamic state, this
496 * usually just leads to rendering corruptions, but shaders that are all
497 * zero hang the GPU immediately.
498 *
499 * The easiest solution to do is exactly what the bogus workarounds say to
500 * do: restrict these buffers to 32-bit addresses. We could also pin the
501 * BO to some particular location of our choosing, but that's significantly
502 * more work than just not setting a flag. So, we explicitly DO NOT set
503 * the EXEC_OBJECT_SUPPORTS_48B_ADDRESS flag and the kernel does all of the
504 * hard work for us.
505 */
506 anv_bo_init(&pool->bo, gem_handle, size);
507 pool->bo.map = map;
508
509 if (pool->device->instance->physicalDevice.has_exec_async)
510 pool->bo.flags |= EXEC_OBJECT_ASYNC;
511
512 done:
513 pthread_mutex_unlock(&pool->device->mutex);
514
515 /* Return the appropreate new size. This function never actually
516 * updates state->next. Instead, we let the caller do that because it
517 * needs to do so in order to maintain its concurrency model.
518 */
519 if (state == &pool->state) {
520 return pool->bo.size - pool->center_bo_offset;
521 } else {
522 assert(pool->center_bo_offset > 0);
523 return pool->center_bo_offset;
524 }
525
526 fail:
527 pthread_mutex_unlock(&pool->device->mutex);
528
529 return 0;
530 }
531
532 static uint32_t
533 anv_block_pool_alloc_new(struct anv_block_pool *pool,
534 struct anv_block_state *pool_state)
535 {
536 struct anv_block_state state, old, new;
537
538 while (1) {
539 state.u64 = __sync_fetch_and_add(&pool_state->u64, pool->block_size);
540 if (state.next < state.end) {
541 assert(pool->map);
542 return state.next;
543 } else if (state.next == state.end) {
544 /* We allocated the first block outside the pool, we have to grow it.
545 * pool_state->next acts a mutex: threads who try to allocate now will
546 * get block indexes above the current limit and hit futex_wait
547 * below. */
548 new.next = state.next + pool->block_size;
549 new.end = anv_block_pool_grow(pool, pool_state);
550 assert(new.end >= new.next && new.end % pool->block_size == 0);
551 old.u64 = __sync_lock_test_and_set(&pool_state->u64, new.u64);
552 if (old.next != state.next)
553 futex_wake(&pool_state->end, INT_MAX);
554 return state.next;
555 } else {
556 futex_wait(&pool_state->end, state.end);
557 continue;
558 }
559 }
560 }
561
562 int32_t
563 anv_block_pool_alloc(struct anv_block_pool *pool)
564 {
565 int32_t offset;
566
567 /* Try free list first. */
568 if (anv_free_list_pop(&pool->free_list, &pool->map, &offset)) {
569 assert(offset >= 0);
570 assert(pool->map);
571 return offset;
572 }
573
574 return anv_block_pool_alloc_new(pool, &pool->state);
575 }
576
577 /* Allocates a block out of the back of the block pool.
578 *
579 * This will allocated a block earlier than the "start" of the block pool.
580 * The offsets returned from this function will be negative but will still
581 * be correct relative to the block pool's map pointer.
582 *
583 * If you ever use anv_block_pool_alloc_back, then you will have to do
584 * gymnastics with the block pool's BO when doing relocations.
585 */
586 int32_t
587 anv_block_pool_alloc_back(struct anv_block_pool *pool)
588 {
589 int32_t offset;
590
591 /* Try free list first. */
592 if (anv_free_list_pop(&pool->back_free_list, &pool->map, &offset)) {
593 assert(offset < 0);
594 assert(pool->map);
595 return offset;
596 }
597
598 offset = anv_block_pool_alloc_new(pool, &pool->back_state);
599
600 /* The offset we get out of anv_block_pool_alloc_new() is actually the
601 * number of bytes downwards from the middle to the end of the block.
602 * We need to turn it into a (negative) offset from the middle to the
603 * start of the block.
604 */
605 assert(offset >= 0);
606 return -(offset + pool->block_size);
607 }
608
609 void
610 anv_block_pool_free(struct anv_block_pool *pool, int32_t offset)
611 {
612 if (offset < 0) {
613 anv_free_list_push(&pool->back_free_list, pool->map, offset);
614 } else {
615 anv_free_list_push(&pool->free_list, pool->map, offset);
616 }
617 }
618
619 static void
620 anv_fixed_size_state_pool_init(struct anv_fixed_size_state_pool *pool,
621 size_t state_size)
622 {
623 /* At least a cache line and must divide the block size. */
624 assert(state_size >= 64 && util_is_power_of_two(state_size));
625
626 pool->state_size = state_size;
627 pool->free_list = ANV_FREE_LIST_EMPTY;
628 pool->block.next = 0;
629 pool->block.end = 0;
630 }
631
632 static uint32_t
633 anv_fixed_size_state_pool_alloc(struct anv_fixed_size_state_pool *pool,
634 struct anv_block_pool *block_pool)
635 {
636 int32_t offset;
637 struct anv_block_state block, old, new;
638
639 /* Try free list first. */
640 if (anv_free_list_pop(&pool->free_list, &block_pool->map, &offset)) {
641 assert(offset >= 0);
642 return offset;
643 }
644
645 /* If free list was empty (or somebody raced us and took the items) we
646 * allocate a new item from the end of the block */
647 restart:
648 block.u64 = __sync_fetch_and_add(&pool->block.u64, pool->state_size);
649
650 if (block.next < block.end) {
651 return block.next;
652 } else if (block.next == block.end) {
653 offset = anv_block_pool_alloc(block_pool);
654 new.next = offset + pool->state_size;
655 new.end = offset + block_pool->block_size;
656 old.u64 = __sync_lock_test_and_set(&pool->block.u64, new.u64);
657 if (old.next != block.next)
658 futex_wake(&pool->block.end, INT_MAX);
659 return offset;
660 } else {
661 futex_wait(&pool->block.end, block.end);
662 goto restart;
663 }
664 }
665
666 static void
667 anv_fixed_size_state_pool_free(struct anv_fixed_size_state_pool *pool,
668 struct anv_block_pool *block_pool,
669 uint32_t offset)
670 {
671 anv_free_list_push(&pool->free_list, block_pool->map, offset);
672 }
673
674 void
675 anv_state_pool_init(struct anv_state_pool *pool,
676 struct anv_block_pool *block_pool)
677 {
678 pool->block_pool = block_pool;
679 for (unsigned i = 0; i < ANV_STATE_BUCKETS; i++) {
680 size_t size = 1 << (ANV_MIN_STATE_SIZE_LOG2 + i);
681 anv_fixed_size_state_pool_init(&pool->buckets[i], size);
682 }
683 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
684 }
685
686 void
687 anv_state_pool_finish(struct anv_state_pool *pool)
688 {
689 VG(VALGRIND_DESTROY_MEMPOOL(pool));
690 }
691
692 static struct anv_state
693 anv_state_pool_alloc_no_vg(struct anv_state_pool *pool,
694 size_t size, size_t align)
695 {
696 unsigned size_log2 = ilog2_round_up(size < align ? align : size);
697 assert(size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
698 if (size_log2 < ANV_MIN_STATE_SIZE_LOG2)
699 size_log2 = ANV_MIN_STATE_SIZE_LOG2;
700 unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
701
702 struct anv_state state;
703 state.alloc_size = 1 << size_log2;
704 state.offset = anv_fixed_size_state_pool_alloc(&pool->buckets[bucket],
705 pool->block_pool);
706 state.map = pool->block_pool->map + state.offset;
707 return state;
708 }
709
710 struct anv_state
711 anv_state_pool_alloc(struct anv_state_pool *pool, size_t size, size_t align)
712 {
713 if (size == 0)
714 return ANV_STATE_NULL;
715
716 struct anv_state state = anv_state_pool_alloc_no_vg(pool, size, align);
717 VG(VALGRIND_MEMPOOL_ALLOC(pool, state.map, size));
718 return state;
719 }
720
721 static void
722 anv_state_pool_free_no_vg(struct anv_state_pool *pool, struct anv_state state)
723 {
724 assert(util_is_power_of_two(state.alloc_size));
725 unsigned size_log2 = ilog2_round_up(state.alloc_size);
726 assert(size_log2 >= ANV_MIN_STATE_SIZE_LOG2 &&
727 size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
728 unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
729
730 anv_fixed_size_state_pool_free(&pool->buckets[bucket],
731 pool->block_pool, state.offset);
732 }
733
734 void
735 anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state)
736 {
737 if (state.alloc_size == 0)
738 return;
739
740 VG(VALGRIND_MEMPOOL_FREE(pool, state.map));
741 anv_state_pool_free_no_vg(pool, state);
742 }
743
744 #define NULL_BLOCK 1
745 struct anv_state_stream_block {
746 /* The next block */
747 struct anv_state_stream_block *next;
748
749 /* The offset into the block pool at which this block starts */
750 uint32_t offset;
751
752 #ifdef HAVE_VALGRIND
753 /* A pointer to the first user-allocated thing in this block. This is
754 * what valgrind sees as the start of the block.
755 */
756 void *_vg_ptr;
757 #endif
758 };
759
760 /* The state stream allocator is a one-shot, single threaded allocator for
761 * variable sized blocks. We use it for allocating dynamic state.
762 */
763 void
764 anv_state_stream_init(struct anv_state_stream *stream,
765 struct anv_block_pool *block_pool)
766 {
767 stream->block_pool = block_pool;
768 stream->block = NULL;
769
770 /* Ensure that next + whatever > end. This way the first call to
771 * state_stream_alloc fetches a new block.
772 */
773 stream->next = 1;
774 stream->end = 0;
775
776 VG(VALGRIND_CREATE_MEMPOOL(stream, 0, false));
777 }
778
779 void
780 anv_state_stream_finish(struct anv_state_stream *stream)
781 {
782 VG(const uint32_t block_size = stream->block_pool->block_size);
783
784 struct anv_state_stream_block *next = stream->block;
785 while (next != NULL) {
786 struct anv_state_stream_block sb = VG_NOACCESS_READ(next);
787 VG(VALGRIND_MEMPOOL_FREE(stream, sb._vg_ptr));
788 VG(VALGRIND_MAKE_MEM_UNDEFINED(next, block_size));
789 anv_block_pool_free(stream->block_pool, sb.offset);
790 next = sb.next;
791 }
792
793 VG(VALGRIND_DESTROY_MEMPOOL(stream));
794 }
795
796 struct anv_state
797 anv_state_stream_alloc(struct anv_state_stream *stream,
798 uint32_t size, uint32_t alignment)
799 {
800 if (size == 0)
801 return ANV_STATE_NULL;
802
803 struct anv_state_stream_block *sb = stream->block;
804
805 struct anv_state state;
806
807 state.offset = align_u32(stream->next, alignment);
808 if (state.offset + size > stream->end) {
809 uint32_t block = anv_block_pool_alloc(stream->block_pool);
810 sb = stream->block_pool->map + block;
811
812 VG(VALGRIND_MAKE_MEM_UNDEFINED(sb, sizeof(*sb)));
813 sb->next = stream->block;
814 sb->offset = block;
815 VG(sb->_vg_ptr = NULL);
816 VG(VALGRIND_MAKE_MEM_NOACCESS(sb, stream->block_pool->block_size));
817
818 stream->block = sb;
819 stream->start = block;
820 stream->next = block + sizeof(*sb);
821 stream->end = block + stream->block_pool->block_size;
822
823 state.offset = align_u32(stream->next, alignment);
824 assert(state.offset + size <= stream->end);
825 }
826
827 assert(state.offset > stream->start);
828 state.map = (void *)sb + (state.offset - stream->start);
829 state.alloc_size = size;
830
831 #ifdef HAVE_VALGRIND
832 void *vg_ptr = VG_NOACCESS_READ(&sb->_vg_ptr);
833 if (vg_ptr == NULL) {
834 vg_ptr = state.map;
835 VG_NOACCESS_WRITE(&sb->_vg_ptr, vg_ptr);
836 VALGRIND_MEMPOOL_ALLOC(stream, vg_ptr, size);
837 } else {
838 void *state_end = state.map + state.alloc_size;
839 /* This only updates the mempool. The newly allocated chunk is still
840 * marked as NOACCESS. */
841 VALGRIND_MEMPOOL_CHANGE(stream, vg_ptr, vg_ptr, state_end - vg_ptr);
842 /* Mark the newly allocated chunk as undefined */
843 VALGRIND_MAKE_MEM_UNDEFINED(state.map, state.alloc_size);
844 }
845 #endif
846
847 stream->next = state.offset + size;
848
849 return state;
850 }
851
852 struct bo_pool_bo_link {
853 struct bo_pool_bo_link *next;
854 struct anv_bo bo;
855 };
856
857 void
858 anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device)
859 {
860 pool->device = device;
861 memset(pool->free_list, 0, sizeof(pool->free_list));
862
863 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
864 }
865
866 void
867 anv_bo_pool_finish(struct anv_bo_pool *pool)
868 {
869 for (unsigned i = 0; i < ARRAY_SIZE(pool->free_list); i++) {
870 struct bo_pool_bo_link *link = PFL_PTR(pool->free_list[i]);
871 while (link != NULL) {
872 struct bo_pool_bo_link link_copy = VG_NOACCESS_READ(link);
873
874 anv_gem_munmap(link_copy.bo.map, link_copy.bo.size);
875 anv_gem_close(pool->device, link_copy.bo.gem_handle);
876 link = link_copy.next;
877 }
878 }
879
880 VG(VALGRIND_DESTROY_MEMPOOL(pool));
881 }
882
883 VkResult
884 anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo, uint32_t size)
885 {
886 VkResult result;
887
888 const unsigned size_log2 = size < 4096 ? 12 : ilog2_round_up(size);
889 const unsigned pow2_size = 1 << size_log2;
890 const unsigned bucket = size_log2 - 12;
891 assert(bucket < ARRAY_SIZE(pool->free_list));
892
893 void *next_free_void;
894 if (anv_ptr_free_list_pop(&pool->free_list[bucket], &next_free_void)) {
895 struct bo_pool_bo_link *next_free = next_free_void;
896 *bo = VG_NOACCESS_READ(&next_free->bo);
897 assert(bo->gem_handle);
898 assert(bo->map == next_free);
899 assert(size <= bo->size);
900
901 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
902
903 return VK_SUCCESS;
904 }
905
906 struct anv_bo new_bo;
907
908 result = anv_bo_init_new(&new_bo, pool->device, pow2_size);
909 if (result != VK_SUCCESS)
910 return result;
911
912 assert(new_bo.size == pow2_size);
913
914 new_bo.map = anv_gem_mmap(pool->device, new_bo.gem_handle, 0, pow2_size, 0);
915 if (new_bo.map == MAP_FAILED) {
916 anv_gem_close(pool->device, new_bo.gem_handle);
917 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
918 }
919
920 *bo = new_bo;
921
922 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
923
924 return VK_SUCCESS;
925 }
926
927 void
928 anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo_in)
929 {
930 /* Make a copy in case the anv_bo happens to be storred in the BO */
931 struct anv_bo bo = *bo_in;
932
933 VG(VALGRIND_MEMPOOL_FREE(pool, bo.map));
934
935 struct bo_pool_bo_link *link = bo.map;
936 VG_NOACCESS_WRITE(&link->bo, bo);
937
938 assert(util_is_power_of_two(bo.size));
939 const unsigned size_log2 = ilog2_round_up(bo.size);
940 const unsigned bucket = size_log2 - 12;
941 assert(bucket < ARRAY_SIZE(pool->free_list));
942
943 anv_ptr_free_list_push(&pool->free_list[bucket], link);
944 }
945
946 // Scratch pool
947
948 void
949 anv_scratch_pool_init(struct anv_device *device, struct anv_scratch_pool *pool)
950 {
951 memset(pool, 0, sizeof(*pool));
952 }
953
954 void
955 anv_scratch_pool_finish(struct anv_device *device, struct anv_scratch_pool *pool)
956 {
957 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
958 for (unsigned i = 0; i < 16; i++) {
959 struct anv_scratch_bo *bo = &pool->bos[i][s];
960 if (bo->exists > 0)
961 anv_gem_close(device, bo->bo.gem_handle);
962 }
963 }
964 }
965
966 struct anv_bo *
967 anv_scratch_pool_alloc(struct anv_device *device, struct anv_scratch_pool *pool,
968 gl_shader_stage stage, unsigned per_thread_scratch)
969 {
970 if (per_thread_scratch == 0)
971 return NULL;
972
973 unsigned scratch_size_log2 = ffs(per_thread_scratch / 2048);
974 assert(scratch_size_log2 < 16);
975
976 struct anv_scratch_bo *bo = &pool->bos[scratch_size_log2][stage];
977
978 /* We can use "exists" to shortcut and ignore the critical section */
979 if (bo->exists)
980 return &bo->bo;
981
982 pthread_mutex_lock(&device->mutex);
983
984 __sync_synchronize();
985 if (bo->exists)
986 return &bo->bo;
987
988 const struct anv_physical_device *physical_device =
989 &device->instance->physicalDevice;
990 const struct gen_device_info *devinfo = &physical_device->info;
991
992 /* WaCSScratchSize:hsw
993 *
994 * Haswell's scratch space address calculation appears to be sparse
995 * rather than tightly packed. The Thread ID has bits indicating which
996 * subslice, EU within a subslice, and thread within an EU it is.
997 * There's a maximum of two slices and two subslices, so these can be
998 * stored with a single bit. Even though there are only 10 EUs per
999 * subslice, this is stored in 4 bits, so there's an effective maximum
1000 * value of 16 EUs. Similarly, although there are only 7 threads per EU,
1001 * this is stored in a 3 bit number, giving an effective maximum value
1002 * of 8 threads per EU.
1003 *
1004 * This means that we need to use 16 * 8 instead of 10 * 7 for the
1005 * number of threads per subslice.
1006 */
1007 const unsigned subslices = MAX2(physical_device->subslice_total, 1);
1008 const unsigned scratch_ids_per_subslice =
1009 device->info.is_haswell ? 16 * 8 : devinfo->max_cs_threads;
1010
1011 uint32_t max_threads[] = {
1012 [MESA_SHADER_VERTEX] = devinfo->max_vs_threads,
1013 [MESA_SHADER_TESS_CTRL] = devinfo->max_tcs_threads,
1014 [MESA_SHADER_TESS_EVAL] = devinfo->max_tes_threads,
1015 [MESA_SHADER_GEOMETRY] = devinfo->max_gs_threads,
1016 [MESA_SHADER_FRAGMENT] = devinfo->max_wm_threads,
1017 [MESA_SHADER_COMPUTE] = scratch_ids_per_subslice * subslices,
1018 };
1019
1020 uint32_t size = per_thread_scratch * max_threads[stage];
1021
1022 anv_bo_init_new(&bo->bo, device, size);
1023
1024 /* Even though the Scratch base pointers in 3DSTATE_*S are 64 bits, they
1025 * are still relative to the general state base address. When we emit
1026 * STATE_BASE_ADDRESS, we set general state base address to 0 and the size
1027 * to the maximum (1 page under 4GB). This allows us to just place the
1028 * scratch buffers anywhere we wish in the bottom 32 bits of address space
1029 * and just set the scratch base pointer in 3DSTATE_*S using a relocation.
1030 * However, in order to do so, we need to ensure that the kernel does not
1031 * place the scratch BO above the 32-bit boundary.
1032 *
1033 * NOTE: Technically, it can't go "anywhere" because the top page is off
1034 * limits. However, when EXEC_OBJECT_SUPPORTS_48B_ADDRESS is set, the
1035 * kernel allocates space using
1036 *
1037 * end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
1038 *
1039 * so nothing will ever touch the top page.
1040 */
1041 bo->bo.flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
1042
1043 /* Set the exists last because it may be read by other threads */
1044 __sync_synchronize();
1045 bo->exists = true;
1046
1047 pthread_mutex_unlock(&device->mutex);
1048
1049 return &bo->bo;
1050 }
1051
1052 struct anv_cached_bo {
1053 struct anv_bo bo;
1054
1055 uint32_t refcount;
1056 };
1057
1058 VkResult
1059 anv_bo_cache_init(struct anv_bo_cache *cache)
1060 {
1061 cache->bo_map = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1062 _mesa_key_pointer_equal);
1063 if (!cache->bo_map)
1064 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1065
1066 if (pthread_mutex_init(&cache->mutex, NULL)) {
1067 _mesa_hash_table_destroy(cache->bo_map, NULL);
1068 return vk_errorf(VK_ERROR_OUT_OF_HOST_MEMORY,
1069 "pthread_mutex_init failed: %m");
1070 }
1071
1072 return VK_SUCCESS;
1073 }
1074
1075 void
1076 anv_bo_cache_finish(struct anv_bo_cache *cache)
1077 {
1078 _mesa_hash_table_destroy(cache->bo_map, NULL);
1079 pthread_mutex_destroy(&cache->mutex);
1080 }
1081
1082 static struct anv_cached_bo *
1083 anv_bo_cache_lookup_locked(struct anv_bo_cache *cache, uint32_t gem_handle)
1084 {
1085 struct hash_entry *entry =
1086 _mesa_hash_table_search(cache->bo_map,
1087 (const void *)(uintptr_t)gem_handle);
1088 if (!entry)
1089 return NULL;
1090
1091 struct anv_cached_bo *bo = (struct anv_cached_bo *)entry->data;
1092 assert(bo->bo.gem_handle == gem_handle);
1093
1094 return bo;
1095 }
1096
1097 static struct anv_bo *
1098 anv_bo_cache_lookup(struct anv_bo_cache *cache, uint32_t gem_handle)
1099 {
1100 pthread_mutex_lock(&cache->mutex);
1101
1102 struct anv_cached_bo *bo = anv_bo_cache_lookup_locked(cache, gem_handle);
1103
1104 pthread_mutex_unlock(&cache->mutex);
1105
1106 return bo ? &bo->bo : NULL;
1107 }
1108
1109 VkResult
1110 anv_bo_cache_alloc(struct anv_device *device,
1111 struct anv_bo_cache *cache,
1112 uint64_t size, struct anv_bo **bo_out)
1113 {
1114 struct anv_cached_bo *bo =
1115 vk_alloc(&device->alloc, sizeof(struct anv_cached_bo), 8,
1116 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1117 if (!bo)
1118 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1119
1120 bo->refcount = 1;
1121
1122 /* The kernel is going to give us whole pages anyway */
1123 size = align_u64(size, 4096);
1124
1125 VkResult result = anv_bo_init_new(&bo->bo, device, size);
1126 if (result != VK_SUCCESS) {
1127 vk_free(&device->alloc, bo);
1128 return result;
1129 }
1130
1131 assert(bo->bo.gem_handle);
1132
1133 pthread_mutex_lock(&cache->mutex);
1134
1135 _mesa_hash_table_insert(cache->bo_map,
1136 (void *)(uintptr_t)bo->bo.gem_handle, bo);
1137
1138 pthread_mutex_unlock(&cache->mutex);
1139
1140 *bo_out = &bo->bo;
1141
1142 return VK_SUCCESS;
1143 }
1144
1145 VkResult
1146 anv_bo_cache_import(struct anv_device *device,
1147 struct anv_bo_cache *cache,
1148 int fd, uint64_t size, struct anv_bo **bo_out)
1149 {
1150 pthread_mutex_lock(&cache->mutex);
1151
1152 /* The kernel is going to give us whole pages anyway */
1153 size = align_u64(size, 4096);
1154
1155 uint32_t gem_handle = anv_gem_fd_to_handle(device, fd);
1156 if (!gem_handle) {
1157 pthread_mutex_unlock(&cache->mutex);
1158 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
1159 }
1160
1161 struct anv_cached_bo *bo = anv_bo_cache_lookup_locked(cache, gem_handle);
1162 if (bo) {
1163 if (bo->bo.size != size) {
1164 pthread_mutex_unlock(&cache->mutex);
1165 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
1166 }
1167 __sync_fetch_and_add(&bo->refcount, 1);
1168 } else {
1169 /* For security purposes, we reject BO imports where the size does not
1170 * match exactly. This prevents a malicious client from passing a
1171 * buffer to a trusted client, lying about the size, and telling the
1172 * trusted client to try and texture from an image that goes
1173 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
1174 * in the trusted client. The trusted client can protect itself against
1175 * this sort of attack but only if it can trust the buffer size.
1176 */
1177 off_t import_size = lseek(fd, 0, SEEK_END);
1178 if (import_size == (off_t)-1 || import_size != size) {
1179 anv_gem_close(device, gem_handle);
1180 pthread_mutex_unlock(&cache->mutex);
1181 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
1182 }
1183
1184 bo = vk_alloc(&device->alloc, sizeof(struct anv_cached_bo), 8,
1185 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1186 if (!bo) {
1187 anv_gem_close(device, gem_handle);
1188 pthread_mutex_unlock(&cache->mutex);
1189 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1190 }
1191
1192 bo->refcount = 1;
1193
1194 anv_bo_init(&bo->bo, gem_handle, size);
1195
1196 if (device->instance->physicalDevice.supports_48bit_addresses)
1197 bo->bo.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
1198
1199 if (device->instance->physicalDevice.has_exec_async)
1200 bo->bo.flags |= EXEC_OBJECT_ASYNC;
1201
1202 _mesa_hash_table_insert(cache->bo_map, (void *)(uintptr_t)gem_handle, bo);
1203 }
1204
1205 pthread_mutex_unlock(&cache->mutex);
1206
1207 /* From the Vulkan spec:
1208 *
1209 * "Importing memory from a file descriptor transfers ownership of
1210 * the file descriptor from the application to the Vulkan
1211 * implementation. The application must not perform any operations on
1212 * the file descriptor after a successful import."
1213 *
1214 * If the import fails, we leave the file descriptor open.
1215 */
1216 close(fd);
1217
1218 *bo_out = &bo->bo;
1219
1220 return VK_SUCCESS;
1221 }
1222
1223 VkResult
1224 anv_bo_cache_export(struct anv_device *device,
1225 struct anv_bo_cache *cache,
1226 struct anv_bo *bo_in, int *fd_out)
1227 {
1228 assert(anv_bo_cache_lookup(cache, bo_in->gem_handle) == bo_in);
1229 struct anv_cached_bo *bo = (struct anv_cached_bo *)bo_in;
1230
1231 int fd = anv_gem_handle_to_fd(device, bo->bo.gem_handle);
1232 if (fd < 0)
1233 return vk_error(VK_ERROR_TOO_MANY_OBJECTS);
1234
1235 *fd_out = fd;
1236
1237 return VK_SUCCESS;
1238 }
1239
1240 static bool
1241 atomic_dec_not_one(uint32_t *counter)
1242 {
1243 uint32_t old, val;
1244
1245 val = *counter;
1246 while (1) {
1247 if (val == 1)
1248 return false;
1249
1250 old = __sync_val_compare_and_swap(counter, val, val - 1);
1251 if (old == val)
1252 return true;
1253
1254 val = old;
1255 }
1256 }
1257
1258 void
1259 anv_bo_cache_release(struct anv_device *device,
1260 struct anv_bo_cache *cache,
1261 struct anv_bo *bo_in)
1262 {
1263 assert(anv_bo_cache_lookup(cache, bo_in->gem_handle) == bo_in);
1264 struct anv_cached_bo *bo = (struct anv_cached_bo *)bo_in;
1265
1266 /* Try to decrement the counter but don't go below one. If this succeeds
1267 * then the refcount has been decremented and we are not the last
1268 * reference.
1269 */
1270 if (atomic_dec_not_one(&bo->refcount))
1271 return;
1272
1273 pthread_mutex_lock(&cache->mutex);
1274
1275 /* We are probably the last reference since our attempt to decrement above
1276 * failed. However, we can't actually know until we are inside the mutex.
1277 * Otherwise, someone could import the BO between the decrement and our
1278 * taking the mutex.
1279 */
1280 if (unlikely(__sync_sub_and_fetch(&bo->refcount, 1) > 0)) {
1281 /* Turns out we're not the last reference. Unlock and bail. */
1282 pthread_mutex_unlock(&cache->mutex);
1283 return;
1284 }
1285
1286 struct hash_entry *entry =
1287 _mesa_hash_table_search(cache->bo_map,
1288 (const void *)(uintptr_t)bo->bo.gem_handle);
1289 assert(entry);
1290 _mesa_hash_table_remove(cache->bo_map, entry);
1291
1292 if (bo->bo.map)
1293 anv_gem_munmap(bo->bo.map, bo->bo.size);
1294
1295 anv_gem_close(device, bo->bo.gem_handle);
1296
1297 /* Don't unlock until we've actually closed the BO. The whole point of
1298 * the BO cache is to ensure that we correctly handle races with creating
1299 * and releasing GEM handles and we don't want to let someone import the BO
1300 * again between mutex unlock and closing the GEM handle.
1301 */
1302 pthread_mutex_unlock(&cache->mutex);
1303
1304 vk_free(&device->alloc, bo);
1305 }