Kernel/MM: Set the correct memory type for mmap()s

Make the MemoryType used by mmap() configurable by
File::vmobject_and_memory_type_for_mmap overrides (previously called
vmobject_for_mmap).

All mmap()s were previously MemoryType::Normal.
This commit is contained in:
Sönke Holz 2025-01-10 18:53:29 +01:00
parent b92907f339
commit 0d11e70cfe
15 changed files with 53 additions and 25 deletions

View file

@ -33,13 +33,16 @@ DisplayConnector::DisplayConnector(size_t framebuffer_resource_size, bool enable
{
}
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> DisplayConnector::vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool)
ErrorOr<File::VMObjectAndMemoryType> DisplayConnector::vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool)
{
VERIFY(m_shared_framebuffer_vmobject);
if (offset != 0)
return Error::from_errno(ENOTSUP);
return *m_shared_framebuffer_vmobject;
return VMObjectAndMemoryType {
.vmobject = *m_shared_framebuffer_vmobject,
.memory_type = m_framebuffer_region->memory_type(),
};
}
ErrorOr<size_t> DisplayConnector::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t)

View file

@ -137,7 +137,7 @@ private:
virtual bool can_write(OpenFileDescription const&, u64) const final override { return true; }
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override final;
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override final;
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64&, bool) override final;
virtual ErrorOr<VMObjectAndMemoryType> vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64&, bool) override final;
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override final;
virtual StringView class_name() const override final { return "DisplayConnector"sv; }

View file

@ -38,7 +38,7 @@ ErrorOr<size_t> MemoryDevice::read(OpenFileDescription&, u64 offset, UserOrKerne
return length;
}
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> MemoryDevice::vmobject_for_mmap(Process&, Memory::VirtualRange const& range, u64& offset, bool)
ErrorOr<File::VMObjectAndMemoryType> MemoryDevice::vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const& range, u64& offset, bool)
{
auto viewed_address = PhysicalAddress(offset);
@ -58,7 +58,10 @@ ErrorOr<NonnullLockRefPtr<Memory::VMObject>> MemoryDevice::vmobject_for_mmap(Pro
}
offset = 0;
return TRY(Memory::AnonymousVMObject::try_create_for_physical_range(viewed_address, range.size()));
return VMObjectAndMemoryType {
.vmobject = TRY(Memory::AnonymousVMObject::try_create_for_physical_range(viewed_address, range.size())),
.memory_type = Memory::MemoryType::IO,
};
}
}

View file

@ -19,7 +19,7 @@ public:
static NonnullRefPtr<MemoryDevice> must_create();
~MemoryDevice();
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
virtual ErrorOr<VMObjectAndMemoryType> vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
private:
MemoryDevice();

View file

@ -71,7 +71,7 @@ ErrorOr<void> KCOVDevice::ioctl(OpenFileDescription&, unsigned request, Userspac
}
}
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> KCOVDevice::vmobject_for_mmap(Process& process, Memory::VirtualRange const&, u64&, bool)
ErrorOr<File::VMObjectAndMemoryType> KCOVDevice::vmobject_and_memory_type_for_mmap(Process& process, Memory::VirtualRange const&, u64&, bool)
{
auto* kcov_instance = process.kcov_instance();
VERIFY(kcov_instance != nullptr); // Should have happened on fd open()
@ -79,7 +79,10 @@ ErrorOr<NonnullLockRefPtr<Memory::VMObject>> KCOVDevice::vmobject_for_mmap(Proce
if (!kcov_instance->vmobject())
return ENOBUFS; // mmaped, before KCOV_SETBUFSIZE
return *kcov_instance->vmobject();
return VMObjectAndMemoryType {
.vmobject = *kcov_instance->vmobject(),
.memory_type = Memory::MemoryType::Normal,
};
}
}

View file

@ -19,7 +19,7 @@ public:
static void free_process();
// ^File
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
ErrorOr<VMObjectAndMemoryType> vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
virtual ErrorOr<NonnullRefPtr<OpenFileDescription>> open(int options) override;
protected:

View file

@ -17,12 +17,15 @@ AnonymousFile::AnonymousFile(NonnullLockRefPtr<Memory::AnonymousVMObject> vmobje
AnonymousFile::~AnonymousFile() = default;
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> AnonymousFile::vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool)
ErrorOr<File::VMObjectAndMemoryType> AnonymousFile::vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool)
{
if (offset != 0)
return EINVAL;
return m_vmobject;
return VMObjectAndMemoryType {
.vmobject = m_vmobject,
.memory_type = Memory::MemoryType::Normal,
};
}
ErrorOr<NonnullOwnPtr<KString>> AnonymousFile::pseudo_path(OpenFileDescription const&) const

View file

@ -20,7 +20,7 @@ public:
virtual ~AnonymousFile() override;
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
virtual ErrorOr<VMObjectAndMemoryType> vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
private:
virtual StringView class_name() const override { return "AnonymousFile"sv; }

View file

@ -35,7 +35,7 @@ ErrorOr<void> File::ioctl(OpenFileDescription&, unsigned, Userspace<void*>)
return ENOTTY;
}
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> File::vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64&, bool)
ErrorOr<File::VMObjectAndMemoryType> File::vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64&, bool)
{
return ENODEV;
}

View file

@ -64,7 +64,7 @@ public:
// - Can be overridden in subclasses to implement arbitrary functionality.
// - Subclasses should take care to validate incoming addresses before dereferencing.
//
// vmobject_for_mmap()
// vmobject_and_memory_type_for_mmap()
//
// - Optional. If unimplemented, mmap() on this File will fail with -ENODEV.
// - Called by mmap() when userspace wants to memory-map this File somewhere.
@ -90,9 +90,14 @@ public:
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) = 0;
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) = 0;
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg);
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
virtual ErrorOr<struct stat> stat() const { return EBADF; }
struct VMObjectAndMemoryType {
NonnullLockRefPtr<Memory::VMObject> vmobject;
Memory::MemoryType memory_type;
};
virtual ErrorOr<VMObjectAndMemoryType> vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
// Although this might be better described "name" or "description", these terms already have other meanings.
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const = 0;

View file

@ -81,11 +81,18 @@ ErrorOr<void> InodeFile::ioctl(OpenFileDescription& description, unsigned reques
}
}
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> InodeFile::vmobject_for_mmap(Process&, Memory::VirtualRange const& range, u64& offset, bool shared)
ErrorOr<File::VMObjectAndMemoryType> InodeFile::vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const& range, u64& offset, bool shared)
{
if (shared)
return TRY(Memory::SharedInodeVMObject::try_create_with_inode_and_range(inode(), offset, range.size()));
return TRY(Memory::PrivateInodeVMObject::try_create_with_inode_and_range(inode(), offset, range.size()));
if (shared) {
return VMObjectAndMemoryType {
.vmobject = TRY(Memory::SharedInodeVMObject::try_create_with_inode_and_range(inode(), offset, range.size())),
.memory_type = Memory::MemoryType::Normal,
};
}
return VMObjectAndMemoryType {
.vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode_and_range(inode(), offset, range.size())),
.memory_type = Memory::MemoryType::Normal,
};
}
ErrorOr<NonnullOwnPtr<KString>> InodeFile::pseudo_path(OpenFileDescription const&) const

View file

@ -33,7 +33,7 @@ public:
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override;
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override;
virtual ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
virtual ErrorOr<VMObjectAndMemoryType> vmobject_and_memory_type_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared) override;
virtual ErrorOr<struct stat> stat() const override { return inode().metadata().stat(); }
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) const override;

View file

@ -379,9 +379,9 @@ InodeMetadata OpenFileDescription::metadata() const
return {};
}
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> OpenFileDescription::vmobject_for_mmap(Process& process, Memory::VirtualRange const& range, u64& offset, bool shared)
ErrorOr<File::VMObjectAndMemoryType> OpenFileDescription::vmobject_for_mmap(Process& process, Memory::VirtualRange const& range, u64& offset, bool shared)
{
return m_file->vmobject_for_mmap(process, range, offset, shared);
return m_file->vmobject_and_memory_type_for_mmap(process, range, offset, shared);
}
ErrorOr<void> OpenFileDescription::truncate(u64 length)

View file

@ -95,7 +95,7 @@ public:
RefPtr<Custody> custody();
RefPtr<Custody const> custody() const;
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
ErrorOr<File::VMObjectAndMemoryType> vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64& offset, bool shared);
bool is_blocking() const;
void set_blocking(bool b);

View file

@ -146,6 +146,7 @@ ErrorOr<FlatPtr> Process::sys$mmap(Userspace<Syscall::SC_mmap_params const*> use
RefPtr<OpenFileDescription> description;
LockRefPtr<Memory::VMObject> vmobject;
u64 used_offset = 0;
auto memory_type = Memory::MemoryType::Normal;
if (map_anonymous) {
auto strategy = map_noreserve ? AllocationStrategy::None : AllocationStrategy::Reserve;
@ -174,7 +175,9 @@ ErrorOr<FlatPtr> Process::sys$mmap(Userspace<Syscall::SC_mmap_params const*> use
if (description->inode())
TRY(validate_inode_mmap_prot(prot, description->is_readable(), description->is_writable(), map_shared));
vmobject = TRY(description->vmobject_for_mmap(*this, requested_range, used_offset, map_shared));
auto vmobject_and_memory_type = TRY(description->vmobject_for_mmap(*this, requested_range, used_offset, map_shared));
vmobject = vmobject_and_memory_type.vmobject;
memory_type = vmobject_and_memory_type.memory_type;
}
return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
@ -191,7 +194,8 @@ ErrorOr<FlatPtr> Process::sys$mmap(Userspace<Syscall::SC_mmap_params const*> use
used_offset,
{},
prot,
map_shared));
map_shared,
memory_type));
if (!region)
return ENOMEM;