kfd session: wait for freed VRAM before refusing an allocation

A KFD VRAM allocation is checked against the VRAM manager before KFD
sees it (vram_fits), so that a full manager refuses it instead of TTM
evicting the process's buffers across the link. The manager still
counts buffers that were freed but not yet released: every freed KFD
VRAM buffer is wiped on release (AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)
and TTM's delayed delete returns its memory only after that SDMA fill
retires, about 12 ms per GB on an R9700.

An LSE server short of memory trims its caches and allocates at once.
The trimmed buffers were still being wiped, so the check refused the
allocation with gigabytes about to come free: "KFD VRAM used 26.25 GB
... VRAM manager used 31.52 GB", where the 5.3 GB difference was the
trimmed caches, not memory held by the display or other clients (with
no KFD client the manager holds 63 MiB). Upstream TTM waits for such
buffers when it evicts (ttm_bo_evict_cb, bo->deleted), so on Linux the
same allocation succeeds.

When an allocation does not fit, count the freed buffers still on the
VRAM manager's LRU. If their release would make room, wait for it,
bounded by RT_KFD_PENDING_FREE_MS (the copy timeout, 5 s); otherwise
refuse at once as before. The refusal message now says how much of the
manager's use is freed buffers still being released.
This commit is contained in:
geramyloveless committed 2026-10-10 18:06:07 -07:00
1 parent e31d50c09f
commit 6b75868c1e
2 files changed
+174 -8

No files matched your search

+94 -8
View File
@@ -16,6 +16,7 @@
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/sysinfo.h>
#include <linux/delay.h>
#include <linux/dma-fence.h>
#include <linux/jiffies.h>
#include <uapi/linux/kfd_ioctl.h>
@@ -75,6 +76,11 @@
#ifndef RT_KFD_SETTLE_MS
#define RT_KFD_SETTLE_MS RT_KFD_COPY_TIMEOUT_MS
#endif
/* How long a VRAM allocation waits for freed buffers to leave the VRAM
* manager (vram_fits_settled) before it is refused. */
#ifndef RT_KFD_PENDING_FREE_MS
#define RT_KFD_PENDING_FREE_MS RT_KFD_COPY_TIMEOUT_MS
#endif
struct va_range {
struct va_range *next;
@@ -863,9 +869,50 @@ static int unmap_and_free(struct rt_kfd_session *s, uint64_t handle)
return (int)r;
}
/* VRAM still counted by the VRAM manager for buffers already freed: TTM's
* delayed delete (ttm_bo_release, bo->deleted) holds a freed buffer's memory
* until its fences retire, and every freed KFD VRAM buffer has one, the
* SDMA fill of AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE (measured at about
* 12 ms per GB on an R9700). Caller holds the LRU lock. A freed buffer that
* was pinned is on the manager's LRU too (ttm_bo_release moves it there). */
static uint64_t vram_pending_free_locked(struct ttm_resource_manager *man)
{
uint64_t bytes = 0;
for (unsigned int i = 0; i < TTM_MAX_BO_PRIORITY; i++) {
struct ttm_lru_item *lru;
list_for_each_entry(lru, &man->lru[i], link) {
struct ttm_resource *res;
if (!ttm_lru_item_is_res(lru))
continue;
res = ttm_lru_item_to_res(lru);
if (res->bo && res->bo->deleted)
bytes += res->size;
}
}
return bytes;
}
static uint64_t vram_pending_free(struct amdgpu_device *adev)
{
struct ttm_resource_manager *man = &adev->mman.vram_mgr.manager;
uint64_t bytes;
if (!man->size)
return 0;
spin_lock(&adev->mman.bdev.lru_lock);
bytes = vram_pending_free_locked(man);
spin_unlock(&adev->mman.bdev.lru_lock);
return bytes;
}
/* KFD reports why an allocation was refused only at debug level. Say what
* was asked and what KFD's own limits stood at: its VRAM accounting (used,
* pinned, available) and the system RAM its system limit derives from. */
* pinned, available), the VRAM manager's use and how much of it is freed
* buffers not yet released, and the system RAM its system limit derives
* from. */
static void report_alloc_failure(struct rt_kfd_session *s, uint64_t size,
enum rt_kfd_domain domain, long r)
{
@@ -875,7 +922,8 @@ static void report_alloc_failure(struct rt_kfd_session *s, uint64_t size,
memset(&info, 0, sizeof(info));
si_meminfo(&info);
pr_warn("kfd session: %s %llu bytes refused (%ld%s); KFD VRAM used %lld "
"pinned %lld available %zu of %llu; VRAM manager used %llu of %llu; RAM %llu\n",
"pinned %lld available %zu of %llu; VRAM manager used %llu of %llu "
"(%llu freed, still releasing); RAM %llu\n",
domain == RT_KFD_VRAM ? "VRAM" : "GTT", (unsigned long long)size, r,
r == -ENOSPC ? ": would evict, VRAM manager full" : "",
(long long)adev->kfd.vram_used[0], (long long)atomic64_read(&adev->vram_pin_size),
@@ -883,6 +931,7 @@ static void report_alloc_failure(struct rt_kfd_session *s, uint64_t size,
(unsigned long long)adev->gmc.real_vram_size,
(unsigned long long)adev->mman.vram_mgr.manager.usage,
(unsigned long long)adev->mman.vram_mgr.manager.size,
(unsigned long long)vram_pending_free(adev),
(unsigned long long)info.totalram * (info.mem_unit ? info.mem_unit : 1));
}
@@ -897,19 +946,56 @@ static void report_alloc_failure(struct rt_kfd_session *s, uint64_t size,
* a device behind a narrow, BAR-limited link that move is what hangs the
* copy engine and loses the device. Refusing here keeps an allocation that
* does not fit an allocation failure. A manager that reports no size (not
* initialized) is not checked. */
static bool vram_fits(struct amdgpu_device *adev, uint64_t size)
* initialized) is not checked.
*
* Buffers already freed count until TTM releases them (vram_pending_free);
* @pending, when not NULL, gets how much that is when the allocation does
* not fit as things stand, and @fits_after whether it would fit once they
* are released. */
static bool vram_fits(struct amdgpu_device *adev, uint64_t size, uint64_t *pending,
bool *fits_after)
{
struct ttm_resource_manager *man = &adev->mman.vram_mgr.manager;
uint64_t used;
uint64_t used, freed = 0;
bool fits;
if (!man->size)
return true;
spin_lock(&adev->mman.bdev.lru_lock);
used = man->usage;
spin_unlock(&adev->mman.bdev.lru_lock);
return used <= man->size && size <= man->size - used &&
fits = used <= man->size && size <= man->size - used &&
man->size - used - size >= RT_KFD_VRAM_HEADROOM;
if (!fits && pending)
freed = vram_pending_free_locked(man);
spin_unlock(&adev->mman.bdev.lru_lock);
if (!fits && pending) {
*pending = freed;
used -= freed < used ? freed : used;
*fits_after = used <= man->size && size <= man->size - used &&
man->size - used - size >= RT_KFD_VRAM_HEADROOM;
}
return fits;
}
/* vram_fits, after waiting for freed buffers it needs to leave the VRAM
* manager. A runtime that trims its caches when short of memory frees
* buffers and asks for the new one at once, before their wipe retires; the
* manager still counts them. Upstream TTM waits for such buffers when it
* evicts (ttm_bo_evict_cb: bo->deleted), so on Linux that allocation
* succeeds; here it would be refused. Wait while what is pending would make
* room, bounded by RT_KFD_PENDING_FREE_MS; never when it could not. */
static bool vram_fits_settled(struct amdgpu_device *adev, uint64_t size)
{
const unsigned long deadline = jiffies + msecs_to_jiffies(RT_KFD_PENDING_FREE_MS);
uint64_t pending = 0;
bool fits_after = false;
while (!vram_fits(adev, size, &pending, &fits_after)) {
if (!pending || !fits_after || time_after(jiffies, deadline))
return false;
msleep(1);
}
return true;
}
/* Caller holds s->lock and is inside the process. */
@@ -953,7 +1039,7 @@ static int bo_alloc_locked(struct rt_kfd_session *s, uint64_t size, uint64_t ali
/* libhsakmt's flags: device-local VRAM, or coherent non-paged system
* memory (fine-grained, shared with the host). Both executable, as
* code objects and signal kernels run from them. */
if (domain == RT_KFD_VRAM && !vram_fits(s->adev, size)) {
if (domain == RT_KFD_VRAM && !vram_fits_settled(s->adev, size)) {
report_alloc_failure(s, size, domain, -ENOSPC);
va_free(region, va);
kfree(bo);
+80
View File
@@ -32,6 +32,11 @@
#include "kfd_topology.h"
#include "kfd_session_fixture.h"
/* kfd_session.c's wait for freed VRAM, as the test script builds it. */
#ifndef RT_KFD_PENDING_FREE_MS
#define RT_KFD_PENDING_FREE_MS RT_KFD_COPY_TIMEOUT_MS
#endif
/* The context-save header of queue @set, read back through KFD's BO. */
struct queue_set {
struct rt_kfd_bo *ring, *meta;
@@ -63,6 +68,49 @@ static void make_queue(struct rt_kfd_session *s, struct queue_set *set, uint32_t
}
struct range_count { unsigned int runs; uint64_t bytes; };
/* A freed buffer TTM has not released yet (its wipe still running), and
* TTM's delayed delete releasing it after @delay_ms. */
struct pending_free {
struct amdgpu_device *adev;
struct ttm_buffer_object bo;
struct ttm_resource res;
unsigned int delay_ms;
};
static void pending_free_add(struct pending_free *p, struct amdgpu_device *adev, uint64_t size)
{
memset(p, 0, sizeof(*p));
p->adev = adev;
p->bo.deleted = true;
p->res.bo = &p->bo;
p->res.size = size;
ttm_lru_item_init(&p->res.lru, TTM_LRU_RESOURCE);
spin_lock(&adev->mman.bdev.lru_lock);
list_add_tail(&p->res.lru.link, &adev->mman.vram_mgr.manager.lru[0]);
spin_unlock(&adev->mman.bdev.lru_lock);
}
static void *pending_free_release(void *arg)
{
struct pending_free *p = arg;
struct ttm_resource_manager *man = &p->adev->mman.vram_mgr.manager;
usleep(p->delay_ms * 1000);
spin_lock(&p->adev->mman.bdev.lru_lock);
list_del_init(&p->res.lru.link);
man->usage -= p->res.size;
spin_unlock(&p->adev->mman.bdev.lru_lock);
return NULL;
}
static uint64_t monotonic_ms(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000 + (uint64_t)t.tv_nsec / 1000000;
}
static int count_range(void *arg, void *cpu, uint64_t bytes)
{
struct range_count *c = arg;
@@ -781,6 +829,8 @@ int main(void)
struct rt_kfd_bo *tight = NULL;
spin_lock_init(&adev->mman.bdev.lru_lock);
for (unsigned int i = 0; i < TTM_MAX_BO_PRIORITY; i++)
INIT_LIST_HEAD(&man->lru[i]);
man->size = TEST_VRAM_BYTES;
man->usage = TEST_VRAM_BYTES - (160ULL << 20);
assert(rt_kfd_bo_alloc(s, 64ULL << 20, 0, RT_KFD_VRAM, RT_KFD_PLACE_PRIVATE, &tight) == -ENOMEM);
@@ -788,6 +838,36 @@ int main(void)
assert(!rt_kfd_bo_alloc(s, 16ULL << 20, 0, RT_KFD_VRAM, RT_KFD_PLACE_PRIVATE, &tight) && tight);
assert(kgd_allocs == 3);
assert(!rt_kfd_bo_free(s, tight));
tight = NULL;
/* 64 MiB of that use is a buffer freed but not yet released
* (TTM's delayed delete waits for its wipe). */
{
struct pending_free freed;
pthread_t releaser;
uint64_t start;
pending_free_add(&freed, adev, 64ULL << 20);
/* Room even once it is released? No: refused at once. */
start = monotonic_ms();
assert(rt_kfd_bo_alloc(s, 1ULL << 30, 0, RT_KFD_VRAM, RT_KFD_PLACE_PRIVATE,
&tight) == -ENOMEM);
assert(!tight && kgd_allocs == 3 && monotonic_ms() - start < RT_KFD_PENDING_FREE_MS);
/* Room once it is released, which never comes: refused after
* the wait. */
start = monotonic_ms();
assert(rt_kfd_bo_alloc(s, 64ULL << 20, 0, RT_KFD_VRAM, RT_KFD_PLACE_PRIVATE,
&tight) == -ENOMEM);
assert(!tight && kgd_allocs == 3 && monotonic_ms() - start + 1 >= RT_KFD_PENDING_FREE_MS);
/* Released while the allocation waits: it succeeds. */
freed.delay_ms = 20;
assert(!pthread_create(&releaser, NULL, pending_free_release, &freed));
assert(!rt_kfd_bo_alloc(s, 64ULL << 20, 0, RT_KFD_VRAM, RT_KFD_PLACE_PRIVATE,
&tight) && tight);
assert(!pthread_join(releaser, NULL));
assert(kgd_allocs == 4 && list_empty(&man->lru[0]));
assert(!rt_kfd_bo_free(s, tight));
}
man->size = 0;
man->usage = 0;
}