Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/asahi/lib/agx_bg_eot.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache,
&cache->pool, bin->binary, bin->info.binary_size, 128, &res->bo);
}
free(bin->binary);

/* Out of memory (agx_pool_fail): do not cache a 0 address */
if (!res->ptr) {
ralloc_free(res);
return NULL;
}

return res;
}

Expand Down
31 changes: 21 additions & 10 deletions src/asahi/lib/agx_scratch.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,9 @@ agx_scratch_get_bucket(uint32_t dwords)
1 + util_logbase2_ceil(DIV_ROUND_UP(dwords, AGX_SPILL_UNIT_DWORDS)));
}

static void
static bool
agx_scratch_realloc(struct agx_scratch *scratch)
{
if (scratch->buf)
agx_bo_unreference(scratch->dev, scratch->buf);

struct spill_size size = agx_scratch_get_spill_size(scratch->size_dwords);

if (scratch->dev->debug & AGX_DBG_SCRATCH)
Expand Down Expand Up @@ -111,8 +108,13 @@ agx_scratch_realloc(struct agx_scratch *scratch)
#ifdef SCRATCH_DEBUG
flags = AGX_BO_WRITEBACK;
#endif
scratch->buf = agx_bo_create(scratch->dev, total_alloc, block_size_bytes,
flags, "Scratch");
struct agx_bo *buf = agx_bo_create(scratch->dev, total_alloc,
block_size_bytes, flags, "Scratch");
if (!buf)
return false;

agx_bo_unreference(scratch->dev, scratch->buf);
scratch->buf = buf;
void *map = agx_bo_map(scratch->buf);
memset(map, 0, blocks_off);

Expand Down Expand Up @@ -181,16 +183,21 @@ agx_scratch_realloc(struct agx_scratch *scratch)
if (scratch->dev->debug & AGX_DBG_SCRATCH)
fprintf(stderr, "New Scratch @ 0x%" PRIx64 " (size: 0x%zx)\n",
scratch->buf->va->addr, scratch->buf->size);
return true;
}

void
/* False when the larger buffer could not be allocated: the old one stays, too
* small for the caller's shader. */
bool
agx_scratch_alloc(struct agx_scratch *scratch, unsigned dwords,
size_t subgroups)
{
bool realloc = false;
const uint32_t old_dwords = scratch->size_dwords;
const uint32_t old_subgroups = scratch->subgroups;

if (!dwords)
return;
return true;

assert(dwords <= AGX_MAX_SCRATCH_DWORDS && "Scratch size too large");

Expand All @@ -209,9 +216,13 @@ agx_scratch_alloc(struct agx_scratch *scratch, unsigned dwords,
realloc = true;
}

if (realloc) {
agx_scratch_realloc(scratch);
if (realloc && !agx_scratch_realloc(scratch)) {
scratch->size_dwords = old_dwords;
scratch->subgroups = old_subgroups;
return false;
}

return true;
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/asahi/lib/agx_scratch.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ void agx_scratch_debug_pre(struct agx_scratch *scratch);
void agx_scratch_debug_post(struct agx_scratch *scratch);

uint32_t agx_scratch_get_bucket(uint32_t dwords);
void agx_scratch_alloc(struct agx_scratch *scratch, uint32_t dwords,
bool agx_scratch_alloc(struct agx_scratch *scratch, uint32_t dwords,
size_t subgroups);
61 changes: 32 additions & 29 deletions src/asahi/lib/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ agx_pool_init(struct agx_pool *pool, struct agx_device *dev, const char *label,
pool->create_flags = create_flags;
pool->label = label;
pool->bos = UTIL_DYNARRAY_INIT;
pool->host = UTIL_DYNARRAY_INIT;

if (prealloc)
agx_pool_alloc_backing(pool, POOL_SLAB_SIZE);
Expand All @@ -55,6 +56,31 @@ agx_pool_cleanup(struct agx_pool *pool)
}

util_dynarray_fini(&pool->bos);

util_dynarray_foreach(&pool->host, void *, mem) {
free(*mem);
}

util_dynarray_fini(&pool->host);
}

/* Out of GPU memory: mark the pool failed and return zeroed host memory of sz
* bytes, so the caller can finish what it is recording without a check at
* every allocation. Its GPU address is 0 and it has no BO. The owner drops
* whatever it recorded since the failure (an OpenGL batch) or treats a 0
* address as the error (a long-lived cache), so the GPU never reads it.
* Host allocation failure is fatal, as everywhere else in Mesa.
*/
void *
agx_pool_fail(struct agx_pool *pool, size_t sz)
{
void *mem = calloc(1, MAX2(sz, 1));
if (!mem)
abort();

util_dynarray_append(&pool->host, mem);
pool->failed = true;
return mem;
}

struct agx_ptr
Expand All @@ -73,33 +99,10 @@ agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz,
pool, ALIGN_POT(MAX2(POOL_SLAB_SIZE, sz), 16384));

if (unlikely(fresh == NULL)) {
/* BO creation fails once the kernel has failed this VM (a GPU
* recovery blamed our work: the context already reports a reset).
* Recycle a backing that fits rather than dereferencing NULL: what
* is written here is never executed on a failed VM, and the
* application sees the reset through GL_ARB_robustness and rebuilds
* its context.
*
* On a healthy VM the failure is real memory exhaustion, and a
* submitted batch that has not completed may still read any of our
* backings. Recycling one would corrupt it, so abort instead.
*/
if (agx_device_vm_failed(pool->dev)) {
util_dynarray_foreach(&pool->bos, struct agx_bo *, it) {
if ((*it)->size >= sz) {
fresh = *it;
break;
}
}
}

if (fresh == NULL) {
fprintf(stderr, "agx: pool %s cannot allocate %zu bytes\n",
pool->label ? pool->label : "(unnamed)", sz);
abort();
}

pool->transient_bo = fresh;
if (out_bo)
*out_bo = NULL;

return (struct agx_ptr){.cpu = agx_pool_fail(pool, sz), .gpu = 0};
}

bo = fresh;
Expand Down Expand Up @@ -135,7 +138,7 @@ agx_pool_upload_aligned_with_bo(struct agx_pool *pool, const void *data,
struct agx_ptr transfer =
agx_pool_alloc_aligned_with_bo(pool, sz, alignment, out_bo);
memcpy(transfer.cpu, data, sz);
agx_bo_assert_range(*out_bo,
transfer.gpu - (*out_bo)->va->addr, sz);
if (*out_bo)
agx_bo_assert_range(*out_bo, transfer.gpu - (*out_bo)->va->addr, sz);
return transfer.gpu;
}
10 changes: 10 additions & 0 deletions src/asahi/lib/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ struct agx_pool {

/* Label for pool allocations */
const char *label;

/* Set when an allocation could not get GPU memory (agx_pool_fail), or by
* the owner when its own GPU allocation failed. The owner must not let the
* GPU see anything recorded since. Cleared only by the owner. */
bool failed;

/* Host memory handed out after a failure, freed by agx_pool_cleanup */
struct util_dynarray host;
};

void agx_pool_init(struct agx_pool *pool, struct agx_device *dev,
const char *label, unsigned create_flags, bool prealloc);

void agx_pool_cleanup(struct agx_pool *pool);

void *agx_pool_fail(struct agx_pool *pool, size_t sz);

struct agx_ptr agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz,
unsigned alignment,
struct agx_bo **bo);
Expand Down
45 changes: 44 additions & 1 deletion src/asahi/lib/tests/test-agx-bo-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
#include <stdlib.h>
#include <string.h>

#include "util/os_time.h"
#include "agx_bo.h"
#include "agx_device.h"
#include "util/os_time.h"
#include "pool.h"

#define MIB (1024ull * 1024ull)

Expand All @@ -37,6 +38,7 @@ struct mock_state {
unsigned mmap_calls;
unsigned munmap_calls;
bool alloc_va;
bool alloc_fail;
};

static struct mock_state mock;
Expand All @@ -46,6 +48,9 @@ mock_bo_alloc(struct agx_device *dev, size_t size, size_t align,
enum agx_bo_flags flags)
{
mock.alloc_calls++;
if (mock.alloc_fail)
return NULL;

const uint32_t handle = ++mock.next_handle;

simple_mtx_lock(&dev->bo_map_lock);
Expand Down Expand Up @@ -734,6 +739,43 @@ test_mmap_offset_cache_propagation(void)
finish_mock_device(&dev);
}

/* Out of GPU memory, a pool hands out zeroed host memory at GPU address 0
* with no BO and marks itself failed, instead of aborting; it allocates BOs
* again once memory is back. */
static void
test_pool_alloc_failure(void)
{
struct agx_device dev;
init_mock_device(&dev);
mock.alloc_va = true;

struct agx_pool pool;
agx_pool_init(&pool, &dev, "test", 0, false);

struct agx_bo *bo = NULL;
struct agx_ptr ok = agx_pool_alloc_aligned_with_bo(&pool, 64, 64, &bo);
assert(bo && ok.gpu && !pool.failed);

const size_t big = 1024 * 1024;
mock.alloc_fail = true;
struct agx_ptr sink = agx_pool_alloc_aligned_with_bo(&pool, big, 64, &bo);
assert(pool.failed && !bo && sink.cpu && sink.gpu == 0);
for (size_t i = 0; i < big; ++i)
assert(((const uint8_t *)sink.cpu)[i] == 0);
memset(sink.cpu, 0xff, big);

static uint8_t data[1024 * 1024];
assert(agx_pool_upload_aligned(&pool, data, sizeof(data), 64) == 0);

mock.alloc_fail = false;
pool.failed = false;
struct agx_ptr again = agx_pool_alloc_aligned_with_bo(&pool, big, 64, &bo);
assert(bo && again.gpu && !pool.failed);

agx_pool_cleanup(&pool);
finish_mock_device(&dev);
}

int
main(void)
{
Expand All @@ -747,6 +789,7 @@ main(void)
test_create_checked_evicts_cache_before_refusing();
test_large_buckets_flag_isolation_and_cross_pot_reuse();
test_mmap_offset_cache_propagation();
test_pool_alloc_failure();

printf("agx bo cache: ok\n");
return 0;
Expand Down
6 changes: 5 additions & 1 deletion src/asahi/vulkan/hk_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,11 @@ hk_reserve_scratch_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
unsigned preamble_size = (s->b.info.preamble_scratch_size > 0) ? 1 : 0;

/* Note: this uses the hardware stage, not the software stage */
hk_device_alloc_scratch(dev, s->b.info.stage, max_scratch_size);
if (!hk_device_alloc_scratch(dev, s->b.info.stage, max_scratch_size)) {
vk_command_buffer_set_error(&cmd->vk, VK_ERROR_OUT_OF_DEVICE_MEMORY);
return;
}

perf_debug(cmd, "Reserving %u (%u) bytes of scratch for stage %s",
s->b.info.scratch_size, s->b.info.preamble_scratch_size,
_mesa_shader_stage_to_abbrev(s->b.info.stage));
Expand Down
5 changes: 3 additions & 2 deletions src/asahi/vulkan/hk_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,13 +699,14 @@ hk_device_scratch_locked(struct hk_device *dev, mesa_shader_stage stage)
}
}

static inline void
static inline bool
hk_device_alloc_scratch(struct hk_device *dev, mesa_shader_stage stage,
unsigned size)
{
simple_mtx_lock(&dev->scratch.lock);
agx_scratch_alloc(hk_device_scratch_locked(dev, stage), size, 0);
bool ok = agx_scratch_alloc(hk_device_scratch_locked(dev, stage), size, 0);
simple_mtx_unlock(&dev->scratch.lock);
return ok;
}

/* frg-codegen v4: device-lifetime cache of single-attachment G17P BG/EOT
Expand Down
Loading