-
Notifications
You must be signed in to change notification settings - Fork 5
feat(sync): futex support #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
67f47ba
feat(sync): add futex address-based wait/wake primitive
cursoragent b8b6e94
test(sync): add futex unit tests (8 tests, all passing)
cursoragent 5c2703b
fix(sync): add missing privilege docstrings to futex.h
cursoragent 5e3ac68
fix(sync): fix futex timeout test and race in futex_wait
cursoragent 0ad24d0
fix(sync): address bugbot findings in futex_wait
cursoragent 5dcdf5c
fix(sync): use uint32_t for wake count comparison
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| #include "sync/futex.h" | ||
| #include "sync/spinlock.h" | ||
| #include "sched/sched.h" | ||
| #include "sched/task.h" | ||
| #include "mm/uaccess.h" | ||
| #include "mm/vma.h" | ||
| #include "common/hash.h" | ||
| #include "common/string.h" | ||
| #include "clock/clock.h" | ||
| #include "timer/timer.h" | ||
|
|
||
| namespace sync { | ||
|
|
||
| constexpr uint32_t WAKE_BATCH_SIZE = 16; | ||
|
|
||
| __PRIVILEGED_BSS static futex_bucket g_futex_table[FUTEX_BUCKET_COUNT]; | ||
|
|
||
| __PRIVILEGED_CODE void futex_init() { | ||
| for (uint32_t i = 0; i < FUTEX_BUCKET_COUNT; i++) { | ||
| g_futex_table[i].lock = SPINLOCK_INIT; | ||
| g_futex_table[i].waiters.init(); | ||
| } | ||
| } | ||
|
|
||
| __PRIVILEGED_CODE static uint32_t futex_hash(mm::mm_context* mm, uintptr_t addr) { | ||
| uint64_t h = hash::combine(hash::ptr(mm), hash::u64(addr)); | ||
| return static_cast<uint32_t>(h) & FUTEX_BUCKET_MASK; | ||
| } | ||
|
|
||
| __PRIVILEGED_CODE int32_t futex_wait(uintptr_t uaddr, uint32_t expected, | ||
| uint64_t timeout_ns) { | ||
| sched::task* self = sched::current(); | ||
| mm::mm_context* mm = self->exec.mm_ctx; | ||
| if (uaddr & 0x3) return -22; // EINVAL | ||
|
|
||
| // Read the value before taking the bucket lock. copy_from_user | ||
| // acquires mm_ctx->lock (a sleeping mutex) so it must not be called | ||
| // under a spinlock. This also faults in the page so the re-read | ||
| // under the spinlock below is safe. | ||
| uint32_t pre_val; | ||
| if (mm) { | ||
| int32_t rc = mm::uaccess::copy_from_user( | ||
| &pre_val, reinterpret_cast<const void*>(uaddr), sizeof(uint32_t)); | ||
| if (rc != 0) return -14; // EFAULT | ||
| } else { | ||
| string::memcpy(&pre_val, reinterpret_cast<const void*>(uaddr), | ||
| sizeof(uint32_t)); | ||
| } | ||
|
|
||
| // Early exit: if the value already changed, no need to lock the bucket. | ||
| if (pre_val != expected) return -11; // EAGAIN | ||
|
|
||
| uint32_t idx = futex_hash(mm, uaddr); | ||
| futex_bucket* bucket = &g_futex_table[idx]; | ||
|
|
||
| futex_waiter waiter; | ||
| waiter.task = self; | ||
| waiter.mm = mm; | ||
| waiter.addr = uaddr; | ||
| waiter.link = {}; | ||
|
|
||
| irq_state irq = spin_lock_irqsave(bucket->lock); | ||
|
|
||
| // Re-read the futex word under the bucket lock. The page is already | ||
| // validated/faulted by the copy_from_user above, so a direct read | ||
| // is safe here. This atomic check-and-enqueue prevents lost wakeups. | ||
| uint32_t current_val; | ||
| string::memcpy(¤t_val, reinterpret_cast<const void*>(uaddr), | ||
| sizeof(uint32_t)); | ||
|
|
||
| if (current_val != expected) { | ||
| spin_unlock_irqrestore(bucket->lock, irq); | ||
| return -11; // EAGAIN | ||
| } | ||
|
|
||
| self->state = sched::TASK_STATE_BLOCKED; | ||
| bucket->waiters.push_back(&waiter); | ||
|
|
||
| if (timeout_ns > 0) { | ||
| uint64_t deadline = clock::now_ns() + timeout_ns; | ||
| timer::schedule_sleep(self, deadline); | ||
| } | ||
|
|
||
| spin_unlock_irqrestore(bucket->lock, irq); | ||
| sched::yield(); | ||
|
|
||
| // Cancel any outstanding timer to prevent spurious wakes of future | ||
| // blocking operations if we were woken by futex_wake before timeout. | ||
| timer::cancel_sleep(self); | ||
|
|
||
| // Remove self from bucket if still linked (timeout or kill wakeup). | ||
| bool was_linked = false; | ||
| irq = spin_lock_irqsave(bucket->lock); | ||
| if (waiter.link.is_linked()) { | ||
| bucket->waiters.remove(&waiter); | ||
| was_linked = true; | ||
| } | ||
| spin_unlock_irqrestore(bucket->lock, irq); | ||
|
|
||
| if (sched::is_kill_pending()) return -4; // EINTR | ||
| if (was_linked) return -110; // ETIMEDOUT | ||
| return 0; | ||
FlareCoding marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| __PRIVILEGED_CODE int32_t futex_wake(uintptr_t uaddr, uint32_t count) { | ||
| sched::task* self = sched::current(); | ||
| mm::mm_context* mm = self->exec.mm_ctx; | ||
| if (uaddr & 0x3) return -22; // EINVAL | ||
| if (count == 0) return 0; | ||
|
|
||
| uint32_t idx = futex_hash(mm, uaddr); | ||
| futex_bucket* bucket = &g_futex_table[idx]; | ||
|
|
||
| uint32_t total_woken = 0; | ||
|
|
||
| for (;;) { | ||
| sched::task* batch[WAKE_BATCH_SIZE]; | ||
| uint32_t n = 0; | ||
| bool done = false; | ||
|
|
||
| irq_state irq = spin_lock_irqsave(bucket->lock); | ||
|
|
||
| auto it = bucket->waiters.begin(); | ||
| auto end = bucket->waiters.end(); | ||
| while (it != end && n < WAKE_BATCH_SIZE) { | ||
| futex_waiter& w = *it; | ||
| ++it; // advance before removal | ||
| if (w.mm == mm && w.addr == uaddr) { | ||
| bucket->waiters.remove(&w); | ||
| batch[n++] = w.task; | ||
| if (total_woken + n >= count) { | ||
| done = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (n == 0) done = true; | ||
| spin_unlock_irqrestore(bucket->lock, irq); | ||
|
|
||
| for (uint32_t i = 0; i < n; i++) { | ||
| sched::wake(batch[i]); | ||
| } | ||
| total_woken += n; | ||
|
|
||
| if (done) break; | ||
| } | ||
|
|
||
| return static_cast<int32_t>(total_woken); | ||
| } | ||
|
|
||
| __PRIVILEGED_CODE int32_t futex_wake_all(uintptr_t uaddr) { | ||
| sched::task* self = sched::current(); | ||
| mm::mm_context* mm = self->exec.mm_ctx; | ||
| if (uaddr & 0x3) return -22; // EINVAL | ||
|
|
||
| uint32_t idx = futex_hash(mm, uaddr); | ||
| futex_bucket* bucket = &g_futex_table[idx]; | ||
|
|
||
| uint32_t total_woken = 0; | ||
|
|
||
| for (;;) { | ||
| sched::task* batch[WAKE_BATCH_SIZE]; | ||
| uint32_t n = 0; | ||
|
|
||
| irq_state irq = spin_lock_irqsave(bucket->lock); | ||
|
|
||
| auto it = bucket->waiters.begin(); | ||
| auto end = bucket->waiters.end(); | ||
| while (it != end && n < WAKE_BATCH_SIZE) { | ||
| futex_waiter& w = *it; | ||
| ++it; | ||
| if (w.mm == mm && w.addr == uaddr) { | ||
| bucket->waiters.remove(&w); | ||
| batch[n++] = w.task; | ||
| } | ||
| } | ||
|
|
||
| bool drained = (n == 0); | ||
| spin_unlock_irqrestore(bucket->lock, irq); | ||
|
|
||
| for (uint32_t i = 0; i < n; i++) { | ||
| sched::wake(batch[i]); | ||
| } | ||
| total_woken += n; | ||
|
|
||
| if (drained) break; | ||
| } | ||
|
|
||
| return static_cast<int32_t>(total_woken); | ||
| } | ||
|
|
||
| } // namespace sync | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #ifndef STELLUX_SYNC_FUTEX_H | ||
| #define STELLUX_SYNC_FUTEX_H | ||
|
|
||
| #include "common/types.h" | ||
| #include "common/list.h" | ||
| #include "sync/spinlock.h" | ||
|
|
||
| namespace sched { struct task; } | ||
| namespace mm { struct mm_context; } | ||
|
|
||
| namespace sync { | ||
|
|
||
| struct futex_waiter { | ||
| sched::task* task; | ||
| mm::mm_context* mm; | ||
| uintptr_t addr; | ||
| list::node link; | ||
| }; | ||
|
|
||
| struct futex_bucket { | ||
| spinlock lock; | ||
| list::head<futex_waiter, &futex_waiter::link> waiters; | ||
| }; | ||
|
|
||
| constexpr uint32_t FUTEX_BUCKET_COUNT = 256; | ||
| constexpr uint32_t FUTEX_BUCKET_MASK = FUTEX_BUCKET_COUNT - 1; | ||
|
|
||
| /** | ||
| * Initialize the futex hash table. Call once during boot after sched::init(). | ||
| * @note Privilege: **required** | ||
| */ | ||
| __PRIVILEGED_CODE void futex_init(); | ||
|
|
||
| /** | ||
| * Block if *uaddr == expected. timeout_ns=0 means wait indefinitely. | ||
| * Returns 0 on wake, -EAGAIN on mismatch, -ETIMEDOUT, -EINTR, -EFAULT. | ||
| * @note Privilege: **required** | ||
| */ | ||
| __PRIVILEGED_CODE int32_t futex_wait(uintptr_t uaddr, uint32_t expected, | ||
| uint64_t timeout_ns); | ||
|
|
||
| /** | ||
| * Wake up to count threads waiting on uaddr. Returns number woken. | ||
| * @note Privilege: **required** | ||
| */ | ||
| __PRIVILEGED_CODE int32_t futex_wake(uintptr_t uaddr, uint32_t count); | ||
|
|
||
| /** | ||
| * Wake all threads waiting on uaddr. Returns number woken. | ||
| * @note Privilege: **required** | ||
| */ | ||
| __PRIVILEGED_CODE int32_t futex_wake_all(uintptr_t uaddr); | ||
|
|
||
| } // namespace sync | ||
|
|
||
| #endif // STELLUX_SYNC_FUTEX_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include "syscall/handlers/sys_futex.h" | ||
| #include "sync/futex.h" | ||
|
|
||
| DEFINE_SYSCALL3(futex_wait, uaddr, expected, timeout_ns) { | ||
| return sync::futex_wait( | ||
| static_cast<uintptr_t>(uaddr), | ||
| static_cast<uint32_t>(expected), | ||
| timeout_ns); | ||
| } | ||
|
|
||
| DEFINE_SYSCALL2(futex_wake, uaddr, count) { | ||
| return sync::futex_wake( | ||
| static_cast<uintptr_t>(uaddr), | ||
| static_cast<uint32_t>(count)); | ||
| } | ||
|
|
||
| DEFINE_SYSCALL1(futex_wake_all, uaddr) { | ||
| return sync::futex_wake_all(static_cast<uintptr_t>(uaddr)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #ifndef STELLUX_SYSCALL_HANDLERS_SYS_FUTEX_H | ||
| #define STELLUX_SYSCALL_HANDLERS_SYS_FUTEX_H | ||
|
|
||
| #include "syscall/syscall_table.h" | ||
|
|
||
| DECLARE_SYSCALL(futex_wait); | ||
| DECLARE_SYSCALL(futex_wake); | ||
| DECLARE_SYSCALL(futex_wake_all); | ||
|
|
||
| #endif // STELLUX_SYSCALL_HANDLERS_SYS_FUTEX_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.