2020-07-30 21:38:15 +00:00
|
|
|
/*
|
2021-02-08 19:14:33 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-07-13 22:25:35 +00:00
|
|
|
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
2020-07-30 21:38:15 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
#include <AK/Singleton.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2022-07-13 22:25:35 +00:00
|
|
|
#include <Kernel/Memory/InodeVMObject.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
2020-12-22 06:21:58 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
static Singleton<SpinlockProtected<HashMap<GlobalFutexKey, NonnullLockRefPtr<FutexQueue>>>> s_global_futex_queues;
|
2022-07-13 22:25:35 +00:00
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
void Process::clear_futex_queues_on_exec()
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-07-13 22:25:35 +00:00
|
|
|
s_global_futex_queues->with([this](auto& queues) {
|
2022-08-23 15:58:05 +00:00
|
|
|
auto const* address_space = this->address_space().with([](auto& space) { return space.ptr(); });
|
2022-07-13 22:25:35 +00:00
|
|
|
queues.remove_all_matching([address_space](auto& futex_key, auto& futex_queue) {
|
|
|
|
if ((futex_key.raw.offset & futex_key_private_flag) == 0)
|
|
|
|
return false;
|
|
|
|
if (futex_key.private_.address_space != address_space)
|
|
|
|
return false;
|
|
|
|
bool did_wake_all;
|
|
|
|
futex_queue->wake_all(did_wake_all);
|
|
|
|
VERIFY(did_wake_all); // No one should be left behind...
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<GlobalFutexKey> Process::get_futex_key(FlatPtr user_address, bool shared)
|
|
|
|
{
|
|
|
|
if (user_address & 0b11) // user_address points to a u32, so must be 4byte aligned
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
auto range = Memory::VirtualRange { VirtualAddress(user_address), sizeof(u32) };
|
|
|
|
|
|
|
|
if (!Kernel::Memory::is_user_range(range))
|
|
|
|
return EFAULT;
|
|
|
|
|
|
|
|
if (!shared) { // If this is thread-shared, we can skip searching the matching region
|
|
|
|
return GlobalFutexKey {
|
|
|
|
.private_ = {
|
2022-08-23 15:58:05 +00:00
|
|
|
.address_space = this->address_space().with([](auto& space) { return space.ptr(); }),
|
2022-07-13 22:25:35 +00:00
|
|
|
.user_address = user_address | futex_key_private_flag,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 15:58:05 +00:00
|
|
|
return address_space().with([&](auto& space) -> ErrorOr<GlobalFutexKey> {
|
|
|
|
auto* matching_region = space->find_region_containing(range);
|
|
|
|
if (!matching_region)
|
|
|
|
return EFAULT;
|
|
|
|
|
|
|
|
// The user wants to share this futex, but if the address doesn't point to a shared resource, there's not
|
|
|
|
// much sharing to be done, so let's mark this as private
|
|
|
|
if (!matching_region->is_shared()) {
|
|
|
|
return GlobalFutexKey {
|
|
|
|
.private_ = {
|
|
|
|
.address_space = space.ptr(),
|
|
|
|
.user_address = user_address | futex_key_private_flag,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This address is backed by a shared VMObject, if it's an AnonymousVMObject, it can be shared between processes
|
|
|
|
// via forking, and shared regions that are cloned during a fork retain their original AnonymousVMObject.
|
|
|
|
// On the other hand, if it's a SharedInodeVMObject, it can be shared by two processes mapping the same file as
|
|
|
|
// MAP_SHARED, but since they are deduplicated based on the inode, in all cases the VMObject pointer should be
|
|
|
|
// a unique global identifier.
|
|
|
|
// NOTE: This assumes that a program will not unmap the only region keeping the vmobject alive while waiting on it,
|
|
|
|
// if it does, it will get stuck waiting forever until interrupted by a signal, but since that use case is defined as
|
|
|
|
// a programmer error, we are fine with it.
|
|
|
|
|
|
|
|
auto const& vmobject = matching_region->vmobject();
|
|
|
|
if (vmobject.is_inode())
|
|
|
|
VERIFY(vmobject.is_shared_inode());
|
2022-07-13 22:25:35 +00:00
|
|
|
|
|
|
|
return GlobalFutexKey {
|
2022-08-23 15:58:05 +00:00
|
|
|
.shared = {
|
|
|
|
.vmobject = &vmobject,
|
|
|
|
.offset = matching_region->offset_in_vmobject_from_vaddr(range.base()) }
|
2022-07-13 22:25:35 +00:00
|
|
|
};
|
2022-08-23 15:58:05 +00:00
|
|
|
});
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$futex(Userspace<Syscall::SC_futex_params const*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-09-05 15:51:37 +00:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
Thread::BlockTimeout timeout;
|
|
|
|
u32 cmd = params.futex_op & FUTEX_CMD_MASK;
|
2021-04-26 15:22:04 +00:00
|
|
|
|
|
|
|
bool use_realtime_clock = (params.futex_op & FUTEX_CLOCK_REALTIME) != 0;
|
|
|
|
if (use_realtime_clock && cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET) {
|
|
|
|
return ENOSYS;
|
|
|
|
}
|
|
|
|
|
2022-07-13 22:25:35 +00:00
|
|
|
bool shared = (params.futex_op & FUTEX_PRIVATE_FLAG) == 0;
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case FUTEX_WAIT:
|
|
|
|
case FUTEX_WAIT_BITSET:
|
|
|
|
case FUTEX_REQUEUE:
|
|
|
|
case FUTEX_CMP_REQUEUE: {
|
|
|
|
if (params.timeout) {
|
2021-09-06 20:22:14 +00:00
|
|
|
auto timeout_time = TRY(copy_time_from_user(params.timeout));
|
2020-12-22 06:21:58 +00:00
|
|
|
bool is_absolute = cmd != FUTEX_WAIT;
|
2021-04-26 15:22:04 +00:00
|
|
|
clockid_t clock_id = use_realtime_clock ? CLOCK_REALTIME_COARSE : CLOCK_MONOTONIC_COARSE;
|
2021-09-06 20:22:14 +00:00
|
|
|
timeout = Thread::BlockTimeout(is_absolute, &timeout_time, nullptr, clock_id);
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
if (cmd == FUTEX_WAIT_BITSET && params.val3 == FUTEX_BITSET_MATCH_ANY)
|
|
|
|
cmd = FUTEX_WAIT;
|
|
|
|
break;
|
|
|
|
case FUTEX_WAKE_BITSET:
|
|
|
|
if (params.val3 == FUTEX_BITSET_MATCH_ANY)
|
|
|
|
cmd = FUTEX_WAKE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
auto find_futex_queue = [&](GlobalFutexKey futex_key, bool create_if_not_found, bool* did_create = nullptr) -> ErrorOr<LockRefPtr<FutexQueue>> {
|
2021-07-06 18:48:48 +00:00
|
|
|
VERIFY(!create_if_not_found || did_create != nullptr);
|
2022-08-19 18:53:40 +00:00
|
|
|
return s_global_futex_queues->with([&](auto& queues) -> ErrorOr<LockRefPtr<FutexQueue>> {
|
2022-07-13 22:25:35 +00:00
|
|
|
auto it = queues.find(futex_key);
|
|
|
|
if (it != queues.end())
|
|
|
|
return it->value;
|
|
|
|
if (!create_if_not_found)
|
|
|
|
return nullptr;
|
2021-07-06 18:48:48 +00:00
|
|
|
*did_create = true;
|
2022-08-19 18:53:40 +00:00
|
|
|
auto futex_queue = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) FutexQueue));
|
2022-07-13 22:25:35 +00:00
|
|
|
auto result = TRY(queues.try_set(futex_key, futex_queue));
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
2020-12-22 06:21:58 +00:00
|
|
|
return futex_queue;
|
2022-07-13 22:25:35 +00:00
|
|
|
});
|
2020-12-22 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
2022-07-13 22:25:35 +00:00
|
|
|
auto remove_futex_queue = [&](GlobalFutexKey futex_key) {
|
|
|
|
return s_global_futex_queues->with([&](auto& queues) {
|
|
|
|
auto it = queues.find(futex_key);
|
|
|
|
if (it == queues.end())
|
|
|
|
return;
|
|
|
|
if (it->value->try_remove())
|
|
|
|
queues.remove(it);
|
|
|
|
});
|
2020-12-22 06:21:58 +00:00
|
|
|
};
|
|
|
|
|
2022-07-13 22:25:35 +00:00
|
|
|
auto do_wake = [&](FlatPtr user_address, u32 count, Optional<u32> const& bitmask) -> ErrorOr<int> {
|
2020-12-22 06:21:58 +00:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
2022-07-13 22:25:35 +00:00
|
|
|
auto futex_key = TRY(get_futex_key(user_address, shared));
|
|
|
|
auto futex_queue = TRY(find_futex_queue(futex_key, false));
|
2020-12-22 06:21:58 +00:00
|
|
|
if (!futex_queue)
|
|
|
|
return 0;
|
|
|
|
bool is_empty;
|
|
|
|
u32 woke_count = futex_queue->wake_n(count, bitmask, is_empty);
|
|
|
|
if (is_empty) {
|
|
|
|
// If there are no more waiters, we want to get rid of the futex!
|
2022-07-13 22:25:35 +00:00
|
|
|
remove_futex_queue(futex_key);
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
return (int)woke_count;
|
|
|
|
};
|
|
|
|
|
2021-08-16 21:29:25 +00:00
|
|
|
auto user_address = FlatPtr(params.userspace_address);
|
|
|
|
auto user_address2 = FlatPtr(params.userspace_address2);
|
|
|
|
|
2021-11-24 17:49:43 +00:00
|
|
|
auto do_wait = [&](u32 bitset) -> ErrorOr<FlatPtr> {
|
2021-07-06 18:48:48 +00:00
|
|
|
bool did_create;
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<FutexQueue> futex_queue;
|
2022-07-13 22:25:35 +00:00
|
|
|
auto futex_key = TRY(get_futex_key(user_address, shared));
|
2021-07-06 18:48:48 +00:00
|
|
|
do {
|
|
|
|
auto user_value = user_atomic_load_relaxed(params.userspace_address);
|
|
|
|
if (!user_value.has_value())
|
|
|
|
return EFAULT;
|
|
|
|
if (user_value.value() != params.val) {
|
|
|
|
dbgln_if(FUTEX_DEBUG, "futex wait: EAGAIN. user value: {:p} @ {:p} != val: {}", user_value.value(), params.userspace_address, params.val);
|
|
|
|
return EAGAIN;
|
|
|
|
}
|
|
|
|
atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
|
|
|
|
|
|
|
|
did_create = false;
|
2022-07-13 22:25:35 +00:00
|
|
|
futex_queue = TRY(find_futex_queue(futex_key, true, &did_create));
|
2021-07-06 18:48:48 +00:00
|
|
|
VERIFY(futex_queue);
|
|
|
|
// We need to try again if we didn't create this queue and the existing queue
|
|
|
|
// was removed before we were able to queue an imminent wait.
|
|
|
|
} while (!did_create && !futex_queue->queue_imminent_wait());
|
2020-12-22 06:21:58 +00:00
|
|
|
|
2021-07-06 20:18:26 +00:00
|
|
|
// We must not hold the lock before blocking. But we have a reference
|
2020-12-22 06:21:58 +00:00
|
|
|
// to the FutexQueue so that we can keep it alive.
|
|
|
|
|
|
|
|
Thread::BlockResult block_result = futex_queue->wait_on(timeout, bitset);
|
|
|
|
|
2021-07-06 18:48:48 +00:00
|
|
|
if (futex_queue->is_empty_and_no_imminent_waits()) {
|
2020-12-22 06:21:58 +00:00
|
|
|
// If there are no more waiters, we want to get rid of the futex!
|
2022-07-13 22:25:35 +00:00
|
|
|
remove_futex_queue(futex_key);
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
if (block_result == Thread::BlockResult::InterruptedByTimeout) {
|
2021-03-01 12:49:16 +00:00
|
|
|
return ETIMEDOUT;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-12-22 06:21:58 +00:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2021-11-24 17:49:43 +00:00
|
|
|
auto do_requeue = [&](Optional<u32> val3) -> ErrorOr<FlatPtr> {
|
2020-12-22 06:21:58 +00:00
|
|
|
auto user_value = user_atomic_load_relaxed(params.userspace_address);
|
|
|
|
if (!user_value.has_value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-12-22 06:21:58 +00:00
|
|
|
if (val3.has_value() && val3.value() != user_value.value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EAGAIN;
|
2020-12-22 06:21:58 +00:00
|
|
|
atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
|
|
|
|
|
2022-07-13 22:25:35 +00:00
|
|
|
auto futex_key = TRY(get_futex_key(user_address, shared));
|
|
|
|
auto futex_queue = TRY(find_futex_queue(futex_key, false));
|
|
|
|
if (!futex_queue)
|
|
|
|
return 0;
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<FutexQueue> target_futex_queue;
|
2022-07-13 22:25:35 +00:00
|
|
|
bool is_empty = false;
|
|
|
|
bool is_target_empty = false;
|
|
|
|
auto futex_key2 = TRY(get_futex_key(user_address2, shared));
|
|
|
|
auto woken_or_requeued = TRY(futex_queue->wake_n_requeue(
|
|
|
|
params.val, [&]() -> ErrorOr<FutexQueue*> {
|
|
|
|
// NOTE: futex_queue's lock is being held while this callback is called
|
|
|
|
// The reason we're doing this in a callback is that we don't want to always
|
|
|
|
// create a target queue, only if we actually have anything to move to it!
|
|
|
|
target_futex_queue = TRY(find_futex_queue(futex_key2, true));
|
|
|
|
return target_futex_queue.ptr();
|
|
|
|
},
|
|
|
|
params.val2, is_empty, is_target_empty));
|
|
|
|
if (is_empty)
|
|
|
|
remove_futex_queue(futex_key);
|
|
|
|
if (is_target_empty && target_futex_queue)
|
|
|
|
remove_futex_queue(futex_key2);
|
2020-12-22 06:21:58 +00:00
|
|
|
return woken_or_requeued;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FUTEX_WAIT:
|
|
|
|
return do_wait(0);
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
case FUTEX_WAKE:
|
2022-07-13 06:29:51 +00:00
|
|
|
return TRY(do_wake(user_address, params.val, {}));
|
2020-12-22 06:21:58 +00:00
|
|
|
|
|
|
|
case FUTEX_WAKE_OP: {
|
|
|
|
Optional<u32> oldval;
|
|
|
|
u32 op_arg = _FUTEX_OP_ARG(params.val3);
|
|
|
|
auto op = _FUTEX_OP(params.val3);
|
|
|
|
if (op & FUTEX_OP_ARG_SHIFT) {
|
|
|
|
op_arg = 1 << op_arg;
|
|
|
|
op &= FUTEX_OP_ARG_SHIFT;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-12-22 06:21:58 +00:00
|
|
|
atomic_thread_fence(AK::MemoryOrder::memory_order_release);
|
|
|
|
switch (op) {
|
|
|
|
case FUTEX_OP_SET:
|
|
|
|
oldval = user_atomic_exchange_relaxed(params.userspace_address2, op_arg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_ADD:
|
|
|
|
oldval = user_atomic_fetch_add_relaxed(params.userspace_address2, op_arg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_OR:
|
|
|
|
oldval = user_atomic_fetch_or_relaxed(params.userspace_address2, op_arg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_ANDN:
|
|
|
|
oldval = user_atomic_fetch_and_not_relaxed(params.userspace_address2, op_arg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_XOR:
|
|
|
|
oldval = user_atomic_fetch_xor_relaxed(params.userspace_address2, op_arg);
|
|
|
|
break;
|
|
|
|
default:
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
if (!oldval.has_value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-12-22 06:21:58 +00:00
|
|
|
atomic_thread_fence(AK::MemoryOrder::memory_order_acquire);
|
2022-07-13 06:29:51 +00:00
|
|
|
auto result = TRY(do_wake(user_address, params.val, {}));
|
2020-12-22 06:21:58 +00:00
|
|
|
if (params.val2 > 0) {
|
|
|
|
bool compare_result;
|
|
|
|
switch (_FUTEX_CMP(params.val3)) {
|
|
|
|
case FUTEX_OP_CMP_EQ:
|
|
|
|
compare_result = (oldval.value() == _FUTEX_CMP_ARG(params.val3));
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_CMP_NE:
|
|
|
|
compare_result = (oldval.value() != _FUTEX_CMP_ARG(params.val3));
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_CMP_LT:
|
|
|
|
compare_result = (oldval.value() < _FUTEX_CMP_ARG(params.val3));
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_CMP_LE:
|
|
|
|
compare_result = (oldval.value() <= _FUTEX_CMP_ARG(params.val3));
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_CMP_GT:
|
|
|
|
compare_result = (oldval.value() > _FUTEX_CMP_ARG(params.val3));
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_CMP_GE:
|
|
|
|
compare_result = (oldval.value() >= _FUTEX_CMP_ARG(params.val3));
|
|
|
|
break;
|
|
|
|
default:
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
if (compare_result)
|
2022-07-13 06:29:51 +00:00
|
|
|
result += TRY(do_wake(user_address2, params.val2, {}));
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
|
|
|
return result;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
case FUTEX_REQUEUE:
|
|
|
|
return do_requeue({});
|
|
|
|
|
|
|
|
case FUTEX_CMP_REQUEUE:
|
|
|
|
return do_requeue(params.val3);
|
|
|
|
|
|
|
|
case FUTEX_WAIT_BITSET:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(params.val3 != FUTEX_BITSET_MATCH_ANY); // we should have turned it into FUTEX_WAIT
|
2020-12-22 06:21:58 +00:00
|
|
|
if (params.val3 == 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-12-22 06:21:58 +00:00
|
|
|
return do_wait(params.val3);
|
|
|
|
|
|
|
|
case FUTEX_WAKE_BITSET:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(params.val3 != FUTEX_BITSET_MATCH_ANY); // we should have turned it into FUTEX_WAKE
|
2020-12-22 06:21:58 +00:00
|
|
|
if (params.val3 == 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2022-07-13 06:29:51 +00:00
|
|
|
return TRY(do_wake(user_address, params.val, params.val3));
|
2020-12-22 06:21:58 +00:00
|
|
|
}
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOSYS;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|