2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-08-22 13:23:32 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-02 20:46:43 +00:00
|
|
|
#include <Kernel/Arch/SafeMem.h>
|
2021-10-14 21:53:48 +00:00
|
|
|
#include <Kernel/Arch/SmapDisabler.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
|
|
|
#include <Kernel/Memory/PhysicalPage.h>
|
2020-09-05 21:52:14 +00:00
|
|
|
#include <Kernel/Process.h>
|
2019-08-07 16:06:17 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<VMObject>> AnonymousVMObject::try_clone()
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
|
|
|
// We need to acquire our lock so we copy a sane state
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-25 00:27:43 +00:00
|
|
|
if (is_purgeable() && is_volatile()) {
|
|
|
|
// If this object is purgeable+volatile, create a new zero-filled purgeable+volatile
|
|
|
|
// object, effectively "pre-purging" it in the child process.
|
2021-09-05 14:13:10 +00:00
|
|
|
auto clone = TRY(try_create_purgeable_with_size(size(), AllocationStrategy::None));
|
2021-07-25 00:27:43 +00:00
|
|
|
clone->m_volatile = true;
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
// We're the parent. Since we're about to become COW we need to
|
|
|
|
// commit the number of pages that we need to potentially allocate
|
|
|
|
// so that the parent is still guaranteed to be able to have all
|
|
|
|
// non-volatile memory available.
|
2022-07-10 14:58:02 +00:00
|
|
|
size_t new_cow_pages_needed = 0;
|
|
|
|
for (auto const& page : m_physical_pages) {
|
|
|
|
if (!page->is_shared_zero_page())
|
|
|
|
++new_cow_pages_needed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_cow_pages_needed == 0)
|
|
|
|
return TRY(try_create_with_size(size(), AllocationStrategy::None));
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
dbgln_if(COMMIT_DEBUG, "Cloning {:p}, need {} committed cow pages", this, new_cow_pages_needed);
|
2021-03-09 20:17:30 +00:00
|
|
|
|
2022-07-14 12:27:22 +00:00
|
|
|
auto committed_pages = TRY(MM.commit_physical_pages(new_cow_pages_needed));
|
2021-05-28 10:52:30 +00:00
|
|
|
|
2021-07-30 11:49:41 +00:00
|
|
|
// Create or replace the committed cow pages. When cloning a previously
|
|
|
|
// cloned vmobject, we want to essentially "fork", leaving us and the
|
|
|
|
// new clone with one set of shared committed cow pages, and the original
|
|
|
|
// one would keep the one it still has. This ensures that the original
|
|
|
|
// one and this one, as well as the clone have sufficient resources
|
|
|
|
// to cow all pages as needed
|
2022-08-19 18:53:40 +00:00
|
|
|
auto new_shared_committed_cow_pages = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) SharedCommittedCowPages(move(committed_pages))));
|
2022-01-12 15:23:48 +00:00
|
|
|
auto new_physical_pages = TRY(this->try_clone_physical_pages());
|
2022-02-10 17:17:27 +00:00
|
|
|
auto clone = TRY(try_create_with_shared_cow(*this, *new_shared_committed_cow_pages, move(new_physical_pages)));
|
2021-08-04 20:58:58 +00:00
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
// Both original and clone become COW. So create a COW map for ourselves
|
|
|
|
// or reset all pages to be copied again if we were previously cloned
|
2022-02-10 17:17:27 +00:00
|
|
|
TRY(ensure_or_reset_cow_map());
|
|
|
|
|
|
|
|
m_shared_committed_cow_pages = move(new_shared_committed_cow_pages);
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-08-04 20:49:13 +00:00
|
|
|
if (m_unused_committed_pages.has_value() && !m_unused_committed_pages->is_empty()) {
|
|
|
|
// The parent vmobject didn't use up all committed pages. When
|
|
|
|
// cloning (fork) we will overcommit. For this purpose we drop all
|
|
|
|
// lazy-commit references and replace them with shared zero pages.
|
|
|
|
for (size_t i = 0; i < page_count(); i++) {
|
|
|
|
auto& page = clone->m_physical_pages[i];
|
|
|
|
if (page && page->is_lazy_committed_page()) {
|
|
|
|
page = MM.shared_zero_page();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<AnonymousVMObject>> AnonymousVMObject::try_create_with_size(size_t size, AllocationStrategy strategy)
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-08-04 20:49:13 +00:00
|
|
|
Optional<CommittedPhysicalPageSet> committed_pages;
|
|
|
|
if (strategy == AllocationStrategy::Reserve || strategy == AllocationStrategy::AllocateNow) {
|
2022-07-14 12:27:22 +00:00
|
|
|
committed_pages = TRY(MM.commit_physical_pages(ceil_div(size, static_cast<size_t>(PAGE_SIZE))));
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
2021-08-15 09:07:59 +00:00
|
|
|
|
2022-01-12 15:23:48 +00:00
|
|
|
auto new_physical_pages = TRY(VMObject::try_create_physical_pages(size));
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) AnonymousVMObject(move(new_physical_pages), strategy, move(committed_pages)));
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<AnonymousVMObject>> AnonymousVMObject::try_create_physically_contiguous_with_size(size_t size)
|
2021-07-25 16:37:11 +00:00
|
|
|
{
|
2022-07-14 12:27:22 +00:00
|
|
|
auto contiguous_physical_pages = TRY(MM.allocate_contiguous_physical_pages(size));
|
2021-08-15 09:07:59 +00:00
|
|
|
|
2023-01-28 20:12:17 +00:00
|
|
|
auto new_physical_pages = TRY(FixedArray<RefPtr<PhysicalPage>>::create(contiguous_physical_pages.span()));
|
2022-01-12 15:23:48 +00:00
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) AnonymousVMObject(move(new_physical_pages)));
|
2021-07-25 16:37:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<AnonymousVMObject>> AnonymousVMObject::try_create_purgeable_with_size(size_t size, AllocationStrategy strategy)
|
2021-07-24 23:46:44 +00:00
|
|
|
{
|
2021-08-04 20:49:13 +00:00
|
|
|
Optional<CommittedPhysicalPageSet> committed_pages;
|
|
|
|
if (strategy == AllocationStrategy::Reserve || strategy == AllocationStrategy::AllocateNow) {
|
2022-07-14 12:27:22 +00:00
|
|
|
committed_pages = TRY(MM.commit_physical_pages(ceil_div(size, static_cast<size_t>(PAGE_SIZE))));
|
2021-07-24 23:46:44 +00:00
|
|
|
}
|
2021-08-15 09:07:59 +00:00
|
|
|
|
2022-01-12 15:23:48 +00:00
|
|
|
auto new_physical_pages = TRY(VMObject::try_create_physical_pages(size));
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
auto vmobject = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) AnonymousVMObject(move(new_physical_pages), strategy, move(committed_pages))));
|
2021-07-24 23:46:44 +00:00
|
|
|
vmobject->m_purgeable = true;
|
2021-09-05 20:39:43 +00:00
|
|
|
return vmobject;
|
2021-07-24 23:46:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<AnonymousVMObject>> AnonymousVMObject::try_create_with_physical_pages(Span<NonnullRefPtr<PhysicalPage>> physical_pages)
|
2021-02-26 12:32:43 +00:00
|
|
|
{
|
2023-01-28 20:12:17 +00:00
|
|
|
auto new_physical_pages = TRY(FixedArray<RefPtr<PhysicalPage>>::create(physical_pages));
|
2022-08-19 18:53:40 +00:00
|
|
|
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) AnonymousVMObject(move(new_physical_pages)));
|
2021-02-26 12:32:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<AnonymousVMObject>> AnonymousVMObject::try_create_for_physical_range(PhysicalAddress paddr, size_t size)
|
2019-08-07 16:06:17 +00:00
|
|
|
{
|
2020-01-28 19:48:07 +00:00
|
|
|
if (paddr.offset(size) < paddr) {
|
2021-07-11 15:55:29 +00:00
|
|
|
dbgln("Shenanigans! try_create_for_physical_range({}, {}) would wrap around", paddr, size);
|
2021-08-15 09:07:59 +00:00
|
|
|
// Since we can't wrap around yet, let's pretend to OOM.
|
|
|
|
return ENOMEM;
|
2020-01-28 19:48:07 +00:00
|
|
|
}
|
2021-08-15 09:07:59 +00:00
|
|
|
|
2022-01-12 15:23:48 +00:00
|
|
|
auto new_physical_pages = TRY(VMObject::try_create_physical_pages(size));
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) AnonymousVMObject(paddr, move(new_physical_pages)));
|
2019-08-07 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
ErrorOr<NonnullLockRefPtr<AnonymousVMObject>> AnonymousVMObject::try_create_with_shared_cow(AnonymousVMObject const& other, NonnullLockRefPtr<SharedCommittedCowPages> shared_committed_cow_pages, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages)
|
2022-02-10 17:17:27 +00:00
|
|
|
{
|
2022-07-10 13:40:48 +00:00
|
|
|
auto weak_parent = TRY(other.try_make_weak_ptr<AnonymousVMObject>());
|
2022-08-19 18:53:40 +00:00
|
|
|
auto vmobject = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) AnonymousVMObject(move(weak_parent), move(shared_committed_cow_pages), move(new_physical_pages))));
|
2022-02-10 17:17:27 +00:00
|
|
|
|
|
|
|
TRY(vmobject->ensure_cow_map());
|
|
|
|
|
|
|
|
return vmobject;
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
AnonymousVMObject::AnonymousVMObject(FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages, AllocationStrategy strategy, Optional<CommittedPhysicalPageSet> committed_pages)
|
2022-01-12 15:23:48 +00:00
|
|
|
: VMObject(move(new_physical_pages))
|
2021-08-04 20:49:13 +00:00
|
|
|
, m_unused_committed_pages(move(committed_pages))
|
2019-08-07 16:06:17 +00:00
|
|
|
{
|
2020-09-05 21:52:14 +00:00
|
|
|
if (strategy == AllocationStrategy::AllocateNow) {
|
|
|
|
// Allocate all pages right now. We know we can get all because we committed the amount needed
|
2020-09-05 03:12:25 +00:00
|
|
|
for (size_t i = 0; i < page_count(); ++i)
|
2021-08-05 17:58:09 +00:00
|
|
|
physical_pages()[i] = m_unused_committed_pages->take_one();
|
2020-09-05 21:52:14 +00:00
|
|
|
} else {
|
|
|
|
auto& initial_page = (strategy == AllocationStrategy::Reserve) ? MM.lazy_committed_page() : MM.shared_zero_page();
|
|
|
|
for (size_t i = 0; i < page_count(); ++i)
|
|
|
|
physical_pages()[i] = initial_page;
|
2020-09-05 03:12:25 +00:00
|
|
|
}
|
2019-08-07 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
AnonymousVMObject::AnonymousVMObject(PhysicalAddress paddr, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages)
|
2022-01-12 15:23:48 +00:00
|
|
|
: VMObject(move(new_physical_pages))
|
2019-08-07 16:06:17 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(paddr.page_base() == paddr);
|
2019-08-07 18:12:50 +00:00
|
|
|
for (size_t i = 0; i < page_count(); ++i)
|
2021-07-17 11:30:47 +00:00
|
|
|
physical_pages()[i] = PhysicalPage::create(paddr.offset(i * PAGE_SIZE), MayReturnToFreeList::No);
|
2019-08-07 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
AnonymousVMObject::AnonymousVMObject(FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages)
|
2022-01-12 15:23:48 +00:00
|
|
|
: VMObject(move(new_physical_pages))
|
2021-02-26 12:32:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
AnonymousVMObject::AnonymousVMObject(LockWeakPtr<AnonymousVMObject> other, NonnullLockRefPtr<SharedCommittedCowPages> shared_committed_cow_pages, FixedArray<RefPtr<PhysicalPage>>&& new_physical_pages)
|
2022-01-12 15:23:48 +00:00
|
|
|
: VMObject(move(new_physical_pages))
|
2022-07-10 13:40:48 +00:00
|
|
|
, m_cow_parent(move(other))
|
2021-08-04 20:58:58 +00:00
|
|
|
, m_shared_committed_cow_pages(move(shared_committed_cow_pages))
|
2022-07-10 13:40:48 +00:00
|
|
|
, m_purgeable(m_cow_parent.strong_ref()->m_purgeable)
|
2019-08-07 16:06:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-07-10 13:40:48 +00:00
|
|
|
AnonymousVMObject::~AnonymousVMObject()
|
|
|
|
{
|
|
|
|
if (!m_shared_committed_cow_pages || m_shared_committed_cow_pages->is_empty())
|
|
|
|
return;
|
|
|
|
auto cow_parent = m_cow_parent.strong_ref();
|
|
|
|
if (!cow_parent)
|
|
|
|
return;
|
|
|
|
SpinlockLocker lock(cow_parent->m_lock);
|
|
|
|
if (cow_parent->m_shared_committed_cow_pages == m_shared_committed_cow_pages)
|
|
|
|
cow_parent->m_shared_committed_cow_pages.clear();
|
|
|
|
}
|
2019-08-07 16:06:17 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
size_t AnonymousVMObject::purge()
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
if (!is_purgeable() || !is_volatile())
|
|
|
|
return 0;
|
2021-07-23 00:40:16 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
size_t total_pages_purged = 0;
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
for (auto& page : m_physical_pages) {
|
|
|
|
VERIFY(page);
|
|
|
|
if (page->is_shared_zero_page())
|
2020-09-05 21:52:14 +00:00
|
|
|
continue;
|
2021-07-24 23:46:44 +00:00
|
|
|
page = MM.shared_zero_page();
|
|
|
|
++total_pages_purged;
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
m_was_purged = true;
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
for_each_region([](Region& region) {
|
|
|
|
region.remap();
|
|
|
|
});
|
|
|
|
|
|
|
|
return total_pages_purged;
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged)
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-07-24 23:46:44 +00:00
|
|
|
VERIFY(is_purgeable());
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker locker(m_lock);
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
was_purged = m_was_purged;
|
|
|
|
if (m_volatile == is_volatile)
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
if (is_volatile) {
|
|
|
|
// When a VMObject is made volatile, it gives up all of its committed memory.
|
|
|
|
// Any physical pages already allocated remain in the VMObject for now, but the kernel is free to take them at any moment.
|
|
|
|
for (auto& page : m_physical_pages) {
|
|
|
|
if (page && page->is_lazy_committed_page())
|
|
|
|
page = MM.shared_zero_page();
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:49:13 +00:00
|
|
|
m_unused_committed_pages = {};
|
2021-07-25 00:27:43 +00:00
|
|
|
m_shared_committed_cow_pages = nullptr;
|
|
|
|
|
2021-07-25 22:27:42 +00:00
|
|
|
if (!m_cow_map.is_null())
|
|
|
|
m_cow_map = {};
|
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
m_volatile = true;
|
|
|
|
m_was_purged = false;
|
2021-08-03 08:21:49 +00:00
|
|
|
|
|
|
|
for_each_region([&](auto& region) { region.remap(); });
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-07-24 23:46:44 +00:00
|
|
|
}
|
|
|
|
// When a VMObject is made non-volatile, we try to commit however many pages are not currently available.
|
|
|
|
// If that fails, we return false to indicate that memory allocation failed.
|
|
|
|
size_t committed_pages_needed = 0;
|
|
|
|
for (auto& page : m_physical_pages) {
|
|
|
|
VERIFY(page);
|
|
|
|
if (page->is_shared_zero_page())
|
|
|
|
++committed_pages_needed;
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
if (!committed_pages_needed) {
|
|
|
|
m_volatile = false;
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-07-24 23:46:44 +00:00
|
|
|
}
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2022-07-14 12:27:22 +00:00
|
|
|
m_unused_committed_pages = TRY(MM.commit_physical_pages(committed_pages_needed));
|
2020-09-05 21:52:14 +00:00
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
for (auto& page : m_physical_pages) {
|
|
|
|
if (page->is_shared_zero_page())
|
|
|
|
page = MM.lazy_committed_page();
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 23:46:44 +00:00
|
|
|
m_volatile = false;
|
|
|
|
m_was_purged = false;
|
2021-08-03 08:21:49 +00:00
|
|
|
for_each_region([&](auto& region) { region.remap(); });
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
NonnullRefPtr<PhysicalPage> AnonymousVMObject::allocate_committed_page(Badge<Region>)
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-08-04 20:49:13 +00:00
|
|
|
return m_unused_committed_pages->take_one();
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 17:17:27 +00:00
|
|
|
ErrorOr<void> AnonymousVMObject::ensure_cow_map()
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-03-04 09:05:38 +00:00
|
|
|
if (m_cow_map.is_null())
|
2022-12-20 16:34:13 +00:00
|
|
|
m_cow_map = TRY(Bitmap::create(page_count(), true));
|
2022-02-10 17:17:27 +00:00
|
|
|
return {};
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 17:17:27 +00:00
|
|
|
ErrorOr<void> AnonymousVMObject::ensure_or_reset_cow_map()
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-03-04 09:05:38 +00:00
|
|
|
if (m_cow_map.is_null())
|
2022-02-10 17:17:27 +00:00
|
|
|
TRY(ensure_cow_map());
|
2020-09-05 21:52:14 +00:00
|
|
|
else
|
2021-03-04 09:05:38 +00:00
|
|
|
m_cow_map.fill(true);
|
2022-02-10 17:17:27 +00:00
|
|
|
return {};
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AnonymousVMObject::should_cow(size_t page_index, bool is_shared) const
|
|
|
|
{
|
2021-12-28 18:54:05 +00:00
|
|
|
auto const& page = physical_pages()[page_index];
|
2020-09-05 21:52:14 +00:00
|
|
|
if (page && (page->is_shared_zero_page() || page->is_lazy_committed_page()))
|
|
|
|
return true;
|
|
|
|
if (is_shared)
|
|
|
|
return false;
|
2021-03-04 09:05:38 +00:00
|
|
|
return !m_cow_map.is_null() && m_cow_map.get(page_index);
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 17:17:27 +00:00
|
|
|
ErrorOr<void> AnonymousVMObject::set_should_cow(size_t page_index, bool cow)
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2022-02-10 17:17:27 +00:00
|
|
|
TRY(ensure_cow_map());
|
|
|
|
m_cow_map.set(page_index, cow);
|
|
|
|
return {};
|
2019-08-07 16:06:17 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
size_t AnonymousVMObject::cow_pages() const
|
2020-09-05 03:12:25 +00:00
|
|
|
{
|
2021-03-04 09:05:38 +00:00
|
|
|
if (m_cow_map.is_null())
|
2020-09-05 21:52:14 +00:00
|
|
|
return 0;
|
2021-03-04 09:05:38 +00:00
|
|
|
return m_cow_map.count_slow(true);
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PageFaultResponse AnonymousVMObject::handle_cow_fault(size_t page_index, VirtualAddress vaddr)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-24 23:46:44 +00:00
|
|
|
|
|
|
|
if (is_volatile()) {
|
|
|
|
// A COW fault in a volatile region? Userspace is writing to volatile memory, this is a bug. Crash.
|
|
|
|
dbgln("COW fault in volatile region, will crash.");
|
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
auto& page_slot = physical_pages()[page_index];
|
2021-07-24 23:46:44 +00:00
|
|
|
|
2021-08-05 13:33:26 +00:00
|
|
|
// If we were sharing committed COW pages with another process, and the other process
|
|
|
|
// has exhausted the supply, we can stop counting the shared pages.
|
|
|
|
if (m_shared_committed_cow_pages && m_shared_committed_cow_pages->is_empty())
|
|
|
|
m_shared_committed_cow_pages = nullptr;
|
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
if (page_slot->ref_count() == 1) {
|
2021-05-01 19:10:08 +00:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> It's a COW page but nobody is sharing it anymore. Remap r/w");
|
2022-02-10 17:17:27 +00:00
|
|
|
MUST(set_should_cow(page_index, false)); // If we received a COW fault, we already have a cow map allocated, so this is infallible
|
2021-08-05 15:14:13 +00:00
|
|
|
|
|
|
|
if (m_shared_committed_cow_pages) {
|
|
|
|
m_shared_committed_cow_pages->uncommit_one();
|
2021-08-05 17:26:04 +00:00
|
|
|
if (m_shared_committed_cow_pages->is_empty())
|
2021-08-05 15:14:13 +00:00
|
|
|
m_shared_committed_cow_pages = nullptr;
|
|
|
|
}
|
2020-09-05 21:52:14 +00:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
RefPtr<PhysicalPage> page;
|
2021-07-25 22:31:26 +00:00
|
|
|
if (m_shared_committed_cow_pages) {
|
2021-05-01 19:10:08 +00:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> It's a committed COW page and it's time to COW!");
|
2021-08-04 20:49:13 +00:00
|
|
|
page = m_shared_committed_cow_pages->take_one();
|
2020-09-05 21:52:14 +00:00
|
|
|
} else {
|
2021-05-01 19:10:08 +00:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> It's a COW page and it's time to COW!");
|
2022-07-14 12:27:22 +00:00
|
|
|
auto page_or_error = MM.allocate_physical_page(MemoryManager::ShouldZeroFill::No);
|
2022-01-28 14:36:53 +00:00
|
|
|
if (page_or_error.is_error()) {
|
2021-03-09 20:17:30 +00:00
|
|
|
dmesgln("MM: handle_cow_fault was unable to allocate a physical page");
|
2020-09-05 21:52:14 +00:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
}
|
2022-01-28 14:36:53 +00:00
|
|
|
page = page_or_error.release_value();
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> COW {} <- {}", page->paddr(), page_slot->paddr());
|
2020-09-05 21:52:14 +00:00
|
|
|
{
|
2021-08-22 10:57:58 +00:00
|
|
|
u8* dest_ptr = MM.quickmap_page(*page);
|
2020-09-05 21:52:14 +00:00
|
|
|
SmapDisabler disabler;
|
|
|
|
void* fault_at;
|
|
|
|
if (!safe_memcpy(dest_ptr, vaddr.as_ptr(), PAGE_SIZE, fault_at)) {
|
|
|
|
if ((u8*)fault_at >= dest_ptr && (u8*)fault_at <= dest_ptr + PAGE_SIZE)
|
2021-01-10 15:21:56 +00:00
|
|
|
dbgln(" >> COW: error copying page {}/{} to {}/{}: failed to write to page at {}",
|
|
|
|
page_slot->paddr(), vaddr, page->paddr(), VirtualAddress(dest_ptr), VirtualAddress(fault_at));
|
2020-09-05 21:52:14 +00:00
|
|
|
else if ((u8*)fault_at >= vaddr.as_ptr() && (u8*)fault_at <= vaddr.as_ptr() + PAGE_SIZE)
|
2021-01-10 15:21:56 +00:00
|
|
|
dbgln(" >> COW: error copying page {}/{} to {}/{}: failed to read from page at {}",
|
|
|
|
page_slot->paddr(), vaddr, page->paddr(), VirtualAddress(dest_ptr), VirtualAddress(fault_at));
|
2020-09-05 21:52:14 +00:00
|
|
|
else
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
2021-08-22 10:57:58 +00:00
|
|
|
MM.unquickmap_page();
|
2020-09-05 21:52:14 +00:00
|
|
|
}
|
|
|
|
page_slot = move(page);
|
2022-02-10 17:17:27 +00:00
|
|
|
MUST(set_should_cow(page_index, false)); // If we received a COW fault, we already have a cow map allocated, so this is infallible
|
2020-09-05 21:52:14 +00:00
|
|
|
return PageFaultResponse::Continue;
|
2020-09-05 03:12:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:49:13 +00:00
|
|
|
AnonymousVMObject::SharedCommittedCowPages::SharedCommittedCowPages(CommittedPhysicalPageSet&& committed_pages)
|
|
|
|
: m_committed_pages(move(committed_pages))
|
2021-07-24 23:46:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:15:15 +00:00
|
|
|
AnonymousVMObject::SharedCommittedCowPages::~SharedCommittedCowPages() = default;
|
2021-07-24 23:46:44 +00:00
|
|
|
|
2022-08-24 13:56:26 +00:00
|
|
|
NonnullRefPtr<PhysicalPage> AnonymousVMObject::SharedCommittedCowPages::take_one()
|
2021-07-24 23:46:44 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker locker(m_lock);
|
2021-08-04 20:49:13 +00:00
|
|
|
return m_committed_pages.take_one();
|
2021-07-24 23:46:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 15:14:13 +00:00
|
|
|
void AnonymousVMObject::SharedCommittedCowPages::uncommit_one()
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker locker(m_lock);
|
2021-08-05 15:14:13 +00:00
|
|
|
m_committed_pages.uncommit_one();
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|