Kernel: Make PrivateInodeVMObject factory APIs OOM safe

This commit is contained in:
Brian Gianforcaro 2021-05-28 03:18:07 -07:00 committed by Andreas Kling
parent d947253c51
commit 65d5f81afc
Notes: sideshowbarker 2024-07-18 17:15:34 +09:00
4 changed files with 13 additions and 8 deletions

View File

@ -632,14 +632,16 @@ KResultOr<FlatPtr> Process::sys$mremap(Userspace<const Syscall::SC_mremap_params
auto old_offset = old_region->offset_in_vmobject();
NonnullRefPtr inode = static_cast<SharedInodeVMObject&>(old_region->vmobject()).inode();
auto new_vmobject = PrivateInodeVMObject::create_with_inode(inode);
if (!new_vmobject)
return ENOMEM;
// Unmap without deallocating the VM range since we're going to reuse it.
old_region->unmap(Region::ShouldDeallocateVirtualMemoryRange::No);
bool success = space().deallocate_region(*old_region);
VERIFY(success);
auto new_vmobject = PrivateInodeVMObject::create_with_inode(inode);
auto new_region_or_error = space().allocate_region_with_vmobject(range, new_vmobject, old_offset, old_name, old_prot, false);
auto new_region_or_error = space().allocate_region_with_vmobject(range, new_vmobject.release_nonnull(), old_offset, old_name, old_prot, false);
if (new_region_or_error.is_error())
return new_region_or_error.error().error();
auto& new_region = *new_region_or_error.value();

View File

@ -201,7 +201,10 @@ KResult Process::poke_user_data(Userspace<u32*> address, u32 data)
// If the region is shared, we change its vmobject to a PrivateInodeVMObject
// to prevent the write operation from changing any shared inode data
VERIFY(region->vmobject().is_shared_inode());
region->set_vmobject(PrivateInodeVMObject::create_with_inode(static_cast<SharedInodeVMObject&>(region->vmobject()).inode()));
auto vmobject = PrivateInodeVMObject::create_with_inode(static_cast<SharedInodeVMObject&>(region->vmobject()).inode());
if (!vmobject)
return ENOMEM;
region->set_vmobject(vmobject.release_nonnull());
region->set_shared(false);
}
const bool was_writable = region->is_writable();

View File

@ -9,14 +9,14 @@
namespace Kernel {
NonnullRefPtr<PrivateInodeVMObject> PrivateInodeVMObject::create_with_inode(Inode& inode)
RefPtr<PrivateInodeVMObject> PrivateInodeVMObject::create_with_inode(Inode& inode)
{
return adopt_ref(*new PrivateInodeVMObject(inode, inode.size()));
return adopt_ref_if_nonnull(new PrivateInodeVMObject(inode, inode.size()));
}
RefPtr<VMObject> PrivateInodeVMObject::clone()
{
return adopt_ref(*new PrivateInodeVMObject(*this));
return adopt_ref_if_nonnull(new PrivateInodeVMObject(*this));
}
PrivateInodeVMObject::PrivateInodeVMObject(Inode& inode, size_t size)

View File

@ -18,7 +18,7 @@ class PrivateInodeVMObject final : public InodeVMObject {
public:
virtual ~PrivateInodeVMObject() override;
static NonnullRefPtr<PrivateInodeVMObject> create_with_inode(Inode&);
static RefPtr<PrivateInodeVMObject> create_with_inode(Inode&);
virtual RefPtr<VMObject> clone() override;
private: