Skip to content
Open
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
7 changes: 7 additions & 0 deletions libopenbuenos/src/ob_buffer_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ size_t compute_slab_size(size_t requested) {
while (s < requested) s <<= 1u;
return s;
}
// Page-align without wrapping. requested > SIZE_MAX-4095 makes the old
// (requested+4095) sum collapse to a 0-byte slab that posix_memalign accepts.
if (requested > (SIZE_MAX - 4095u)) return 0u;
return (requested + 4095u) & ~static_cast<size_t>(4095u);
}

Expand Down Expand Up @@ -311,6 +314,10 @@ ob_status_t ob_buffer_alloc(ob_context_t* ctx,
*out_buf = nullptr;

const size_t slab_size = ob::compute_slab_size(size_bytes);
if (slab_size < size_bytes) {
ob::set_last_error(ctx, "buffer size overflow during slab rounding");
return OB_ERR_OVERFLOW;
}
// Always map bidirectional when pooled so any future caller can reuse the slab safely.
const uint32_t map_flags =
(flags & OB_BUF_NO_POOL)
Expand Down
14 changes: 13 additions & 1 deletion libopenbuenos/src/ob_dgc0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,20 @@ ob_status_t parse_dgc0_buffer(const uint8_t* data, size_t size,
dws.i32_vector_field(tp, 0, &dims);
uint8_t dt = dws.field_u8(tp, 1, 2);
uint64_t bytes = darwinn_dtype_bytes(dt);
bool scratch_overflow = false;
for (int32_t d : dims) {
if (d > 0) bytes *= static_cast<uint64_t>(d);
if (d <= 0) continue;
const uint64_t factor = static_cast<uint64_t>(d);
if (factor != 0u && bytes > (UINT64_MAX / factor)) {
scratch_overflow = true;
break;
}
bytes *= factor;
}
if (scratch_overflow) {
out->dive_scratch_size = 0u;
if (err) *err = "DGC0 scratchpad dimension product overflows";
return OB_ERR_OVERFLOW;
}
if (bytes > out->dive_scratch_size) out->dive_scratch_size = bytes;
}
Expand Down
42 changes: 34 additions & 8 deletions libopenbuenos/src/ob_worker_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ inline void cpu_yield_hint() {
#endif
}

uint32_t ceil_div_u32(uint32_t n, uint32_t d) {
if (d == 0u) return 0u;
const uint32_t q = n / d;
if ((n % d) == 0u || q == UINT32_MAX) return q;
return q + 1u;
}

// Publish the next unclaimed index without wrapping next_item back into [0, total).
// fetch_add(step) on a uint32 cursor does that once s+step exceeds 2^32, and the
// following worker then reissues ranges that already ran.
uint32_t claim_work_start(std::atomic<uint32_t>& next_item, uint32_t step, uint32_t total) {
uint32_t s = next_item.load(std::memory_order_relaxed);
for (;;) {
if (s >= total) return s;
const uint32_t n = (step == 0u || step >= total || s > total - step) ? total : (s + step);
if (next_item.compare_exchange_weak(s, n, std::memory_order_relaxed,
std::memory_order_relaxed)) {
return s;
}
}
}

uint32_t claim_work_end(uint32_t s, uint32_t step, uint32_t total) {
if (s >= total) return s;
if (step == 0u || step >= total || s > total - step) return total;
return s + step;
}

struct WorkerLaunchArg {
WorkerPool* pool = nullptr;
uint32_t worker_idx = 0u;
Expand Down Expand Up @@ -115,10 +143,9 @@ void* worker_thread_main(void* raw) {

if (fn != nullptr) {
for (;;) {
const uint32_t s = p->next_item.fetch_add(step, std::memory_order_relaxed);
const uint32_t s = claim_work_start(p->next_item, step, total);
if (s >= total) break;
const uint32_t e = (s + step < total) ? (s + step) : total;
fn(arg, s, e, tid);
fn(arg, s, claim_work_end(s, step, total), tid);
}
if (p->workers_remaining.fetch_sub(1u, std::memory_order_acq_rel) == 1u) {
pthread_mutex_lock(&p->mu);
Expand Down Expand Up @@ -196,7 +223,7 @@ void WorkerPool::parallel_for(uint32_t total, uint32_t min_chunk, TaskFn fn, voi
}
}

const uint32_t max_useful = (total + min_chunk - 1u) / min_chunk;
const uint32_t max_useful = ceil_div_u32(total, min_chunk);
const uint32_t active_threads = std::min<uint32_t>(num_threads, max_useful);
if (active_threads <= 1u) {
fn(arg, 0u, total, 0u);
Expand All @@ -206,7 +233,7 @@ void WorkerPool::parallel_for(uint32_t total, uint32_t min_chunk, TaskFn fn, voi
const uint32_t active_workers = active_threads - 1u;
// Work-stealing granularity (~4 chunks/thread) balances heterogeneous ARM64 big+mid cores.
const uint32_t target_chunks = active_threads * 4u;
const uint32_t step = std::max<uint32_t>(min_chunk, (total + target_chunks - 1u) / target_chunks);
const uint32_t step = std::max<uint32_t>(min_chunk, ceil_div_u32(total, target_chunks));

pthread_mutex_lock(&mu);
cur_fn = fn;
Expand All @@ -222,10 +249,9 @@ void WorkerPool::parallel_for(uint32_t total, uint32_t min_chunk, TaskFn fn, voi

// Main thread participates as tid=0
for (;;) {
const uint32_t s = next_item.fetch_add(step, std::memory_order_relaxed);
const uint32_t s = claim_work_start(next_item, step, total);
if (s >= total) break;
const uint32_t e = (s + step < total) ? (s + step) : total;
fn(arg, s, e, 0u);
fn(arg, s, claim_work_end(s, step, total), 0u);
}

// Brief bounded yield spin (~25 us) before sleeping on done_cv to avoid futex wakeup jitter
Expand Down
Loading
Loading