Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
110 changes: 100 additions & 10 deletions src/fs_dmabuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <algorithm>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -68,6 +69,14 @@ static fs_dmabuf_record* fs_record_from_pub(fs_dmabuf_t* buf) {
return rec;
}

static uint64_t fs_next_fence_seq(fs_context_t* ctx) {
uint64_t next = ++ctx->next_fence_seq;
if (next == 0u) {
next = ++ctx->next_fence_seq;
}
return next;
}

static size_t fs_page_size(void) {
long pg = sysconf(_SC_PAGESIZE);
return (pg > 0) ? static_cast<size_t>(pg) : 4096u;
Expand Down Expand Up @@ -102,8 +111,14 @@ static void fs_free_record_backing(fs_context_t* ctx, fs_dmabuf_record* rec) {
close(rec->pub.dmabuf_fd);
}
}
if (rec->last_fence.sync_file_fd >= 0 &&
rec->last_fence.sync_file_fd != rec->pub.dmabuf_fd) {
close(rec->last_fence.sync_file_fd);
rec->last_fence.sync_file_fd = -1;
}
rec->pub.host_ptr = nullptr;
rec->pub.dmabuf_fd = -1;
rec->pub.sync_file_fd = -1;
rec->pub.owner_dev = FS_DEV_NONE;
rec->magic = 0u;
std::free(rec);
Expand Down Expand Up @@ -573,7 +588,7 @@ fs_status_t fs_buffer_export_sync_file(fs_context_t* ctx,
#endif

*out_fence = fs_fence_init(rec->pub.owner_dev);
out_fence->seq = ++ctx->next_fence_seq;
out_fence->seq = fs_next_fence_seq(ctx);
out_fence->sync_file_fd = sync_fd;
out_fence->state = FS_FENCE_SIGNALED;
out_fence->signaled_at_us = fs_now_us();
Expand All @@ -594,9 +609,16 @@ fs_status_t fs_fence_signal(fs_fence_t* fence, int sync_file_fd) {
if (!fence) {
return FS_ERR_INVALID_ARG;
}
if (sync_file_fd < -1) {
return FS_ERR_INVALID_ARG;
}
if (fence->state == FS_FENCE_ERROR) {
return FS_ERR_FENCE;
}
if (fence->state == FS_FENCE_SIGNALED) {
/* Duplicate signal: reject cleanly without overwriting the original sync_file. */
return FS_ERR_FENCE;
}
fence->sync_file_fd = sync_file_fd;
fence->state = FS_FENCE_SIGNALED;
fence->signaled_at_us = fs_now_us();
Expand All @@ -607,6 +629,9 @@ fs_status_t fs_fence_wait(const fs_fence_t* fence, uint32_t timeout_ms) {
if (!fence) {
return FS_OK;
}
if (fence->sync_file_fd < -1) {
return FS_ERR_INVALID_ARG;
}
if (fence->state == FS_FENCE_ERROR) {
return FS_ERR_FENCE;
}
Expand All @@ -617,8 +642,11 @@ fs_status_t fs_fence_wait(const fs_fence_t* fence, uint32_t timeout_ms) {
pfd.events = POLLIN;
pfd.revents = 0;
int rc = 0;
const int poll_timeout = timeout_ms > static_cast<uint32_t>(INT_MAX)
? INT_MAX
: static_cast<int>(timeout_ms);
do {
rc = poll(&pfd, 1, static_cast<int>(timeout_ms));
rc = poll(&pfd, 1, poll_timeout);
} while (rc < 0 && errno == EINTR);
if (rc < 0) {
return FS_ERR_IO;
Expand Down Expand Up @@ -661,14 +689,20 @@ fs_status_t fs_buffer_alloc(fs_context_t* ctx,

const size_t pg = fs_page_size();
const size_t alloc_size = fs_align_up(size_bytes, pg);
if (alloc_size < size_bytes || alloc_size == 0u) {
return FS_ERR_OVERFLOW;
}

std::lock_guard<std::mutex> lock(ctx->mu);

/* Try buffer pool first unless FS_BUF_NO_POOL is requested */
if ((flags & FS_BUF_NO_POOL) == 0u) {
for (auto it = ctx->pooled_buffers.begin(); it != ctx->pooled_buffers.end(); ++it) {
fs_dmabuf_record* rec = *it;
if (rec->alloc_size >= alloc_size && rec->alloc_size <= alloc_size * 2u) {
const size_t alloc_limit = (alloc_size > static_cast<size_t>(-1) / 2u)
? static_cast<size_t>(-1)
: alloc_size * 2u;
if (rec->alloc_size >= alloc_size && rec->alloc_size <= alloc_limit) {
ctx->pooled_buffers.erase(it);
ctx->pooled_bytes -= rec->alloc_size;
ctx->pool_hits++;
Expand Down Expand Up @@ -846,6 +880,9 @@ fs_status_t fs_buffer_retain(fs_context_t* ctx, fs_dmabuf_t* buf) {
if (!rec || rec->owner_ctx != ctx || rec->in_pool) {
return FS_ERR_INVALID_ARG;
}
if (rec->pub.refcount == UINT32_MAX) {
return FS_ERR_OVERFLOW;
}
rec->pub.refcount++;
return FS_OK;
}
Expand Down Expand Up @@ -896,13 +933,24 @@ fs_status_t fs_buffer_handoff(fs_context_t* ctx,

const uint64_t t0 = fs_now_us();

/* Snapshot taken under lock before any mutation so rollback can restore
* the pre-handoff fence, not the overwritten one. */
fs_dev_t prev_owner = from_dev;
uint64_t prev_gen = 1u;
fs_fence_t prev_fence = fs_fence_init(from_dev);
bool have_prev = false;

{
std::lock_guard<std::mutex> lock(ctx->mu);
fs_dmabuf_record* rec = fs_record_from_pub(buf);
if (!rec || rec->owner_ctx != ctx || rec->in_pool) {
fs_set_error(ctx, "handoff called on invalid or released buffer");
return FS_ERR_INVALID_ARG;
}
prev_owner = rec->pub.owner_dev;
prev_gen = rec->pub.generation;
prev_fence = rec->last_fence;
have_prev = true;

/* Strict single-owner invariant: catches use-after-handoff and wrong owner */
if (rec->pub.owner_dev != from_dev) {
Expand Down Expand Up @@ -951,11 +999,29 @@ fs_status_t fs_buffer_handoff(fs_context_t* ctx,

/* Validate / advance explicit sync_file fence */
if (io_fence) {
if (io_fence->sync_file_fd < -1) {
fs_set_error(ctx, "invalid fence fd on buf %llu", static_cast<unsigned long long>(rec->pub.buf_id));
return FS_ERR_INVALID_ARG;
}
if (io_fence->state == FS_FENCE_ERROR) {
fs_set_error(ctx, "cannot handoff buf %llu with errored fence",
static_cast<unsigned long long>(rec->pub.buf_id));
return FS_ERR_FENCE;
}
/* Strict monotonic sync_file ordering: a pre-sequenced fence that is not
* strictly newer than the buffer's last fence is duplicate/stale and is
* rejected without touching ownership or generation. seq == 0 means the
* caller signaled without allocating a sequence (fs_fence_signal leaves
* seq untouched); such fences are upgraded to a fresh sequence below so
* they stay compatible with backend-produced signal fences. */
if (io_fence->state == FS_FENCE_SIGNALED && io_fence->seq != 0u &&
io_fence->seq <= rec->last_fence.seq) {
fs_set_error(ctx, "stale/duplicate fence on buf %llu: seq %llu <= last %llu",
static_cast<unsigned long long>(rec->pub.buf_id),
static_cast<unsigned long long>(io_fence->seq),
static_cast<unsigned long long>(rec->last_fence.seq));
return FS_ERR_FENCE;
}
if (io_fence->sync_file_fd >= 0) {
fs_status_t wst = fs_fence_wait(io_fence, 5000u);
if (wst != FS_OK) {
Expand All @@ -964,17 +1030,32 @@ fs_status_t fs_buffer_handoff(fs_context_t* ctx,
return wst;
}
}
if (io_fence->state == FS_FENCE_UNSIGNALED) {
if (io_fence->state == FS_FENCE_UNSIGNALED || io_fence->seq == 0u) {
io_fence->producer_dev = from_dev;
io_fence->seq = ++ctx->next_fence_seq;
io_fence->seq = fs_next_fence_seq(ctx);
io_fence->state = FS_FENCE_SIGNALED;
io_fence->signaled_at_us = fs_now_us();
} else if (io_fence->seq > ctx->next_fence_seq) {
ctx->next_fence_seq = io_fence->seq;
}
if (rec->last_fence.sync_file_fd >= 0 &&
rec->last_fence.sync_file_fd != io_fence->sync_file_fd) {
close(rec->last_fence.sync_file_fd);
}
rec->last_fence = *io_fence;
rec->pub.sync_file_fd = io_fence->sync_file_fd;
if (io_fence->sync_file_fd >= 0) {
int dup_fd = dup(io_fence->sync_file_fd);
if (dup_fd >= 0) {
rec->last_fence.sync_file_fd = dup_fd;
}
}
rec->pub.sync_file_fd = rec->last_fence.sync_file_fd;
} else {
if (rec->last_fence.sync_file_fd >= 0) {
close(rec->last_fence.sync_file_fd);
}
rec->last_fence = fs_fence_init(from_dev);
rec->last_fence.seq = ++ctx->next_fence_seq;
rec->last_fence.seq = fs_next_fence_seq(ctx);
rec->last_fence.state = FS_FENCE_SIGNALED;
rec->last_fence.signaled_at_us = fs_now_us();
rec->pub.sync_file_fd = -1;
Expand Down Expand Up @@ -1060,11 +1141,20 @@ fs_status_t fs_buffer_handoff(fs_context_t* ctx,
/* Ensure target device has zero-copy mapping (EDGETPU_MAP_DMABUF / VK_KHR_external_memory_fd) */
fs_status_t map_st = fs_dmabuf_ensure_mapped(ctx, buf, to_dev);
if (map_st != FS_OK) {
/* Roll back ownership atomically if backend import rejects the buffer */
/* Roll back ownership, generation, and fence atomically if backend import
* rejects the buffer. Use current-state guard so a concurrent successful
* handoff in the map window is not clobbered. */
std::lock_guard<std::mutex> lock(ctx->mu);
fs_dmabuf_record* rec = fs_record_from_pub(buf);
if (rec && rec->owner_ctx == ctx) {
rec->pub.owner_dev = from_dev;
if (have_prev && rec && rec->owner_ctx == ctx && rec->pub.owner_dev == to_dev &&
rec->pub.generation == prev_gen + 1u) {
if (rec->last_fence.sync_file_fd >= 0) {
close(rec->last_fence.sync_file_fd);
}
rec->pub.owner_dev = prev_owner;
rec->pub.generation = prev_gen;
rec->last_fence = prev_fence;
rec->pub.sync_file_fd = prev_fence.sync_file_fd;
}
}
return map_st;
Expand Down
Loading
Loading