Kernel+LibC: Make all SharedBuffers purgeable (default: non-volatile)

This patch makes SharedBuffer use a PurgeableVMObject as its underlying
memory object.

A new syscall is added to control the volatile flag of a SharedBuffer.
This commit is contained in:
Andreas Kling 2019-12-09 20:06:47 +01:00
parent 65229a4082
commit 0317ca5ccc
Notes: sideshowbarker 2024-07-19 10:54:28 +09:00
6 changed files with 50 additions and 5 deletions

View file

@ -2909,6 +2909,28 @@ int Process::sys$get_shared_buffer_size(int shared_buffer_id)
return shared_buffer.size();
}
int Process::sys$set_shared_buffer_volatile(int shared_buffer_id, bool state)
{
LOCKER(shared_buffers().lock());
auto it = shared_buffers().resource().find(shared_buffer_id);
if (it == shared_buffers().resource().end())
return -EINVAL;
auto& shared_buffer = *(*it).value;
if (!shared_buffer.is_shared_with(m_pid))
return -EPERM;
#ifdef SHARED_BUFFER_DEBUG
kprintf("%s(%u): Set shared buffer %d volatile: %u\n", name().characters(), pid(), shared_buffer_id, state);
#endif
if (!state) {
bool was_purged = shared_buffer.vmobject().was_purged();
shared_buffer.vmobject().set_volatile(state);
shared_buffer.vmobject().set_was_purged(false);
return was_purged ? 1 : 0;
}
shared_buffer.vmobject().set_volatile(true);
return 0;
}
void Process::terminate_due_to_signal(u8 signal)
{
ASSERT_INTERRUPTS_DISABLED();

View file

@ -219,6 +219,7 @@ public:
int sys$release_shared_buffer(int shared_buffer_id);
int sys$seal_shared_buffer(int shared_buffer_id);
int sys$get_shared_buffer_size(int shared_buffer_id);
int sys$set_shared_buffer_volatile(int shared_buffer_id, bool);
int sys$halt();
int sys$reboot();
int sys$set_process_icon(int icon_id);

View file

@ -1,8 +1,8 @@
#pragma once
#include <AK/OwnPtr.h>
#include <Kernel/VM/AnonymousVMObject.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/PurgeableVMObject.h>
struct SharedBuffer {
private:
@ -20,7 +20,7 @@ private:
public:
SharedBuffer(int id, int size)
: m_shared_buffer_id(id)
, m_vmobject(AnonymousVMObject::create_with_size(size))
, m_vmobject(PurgeableVMObject::create_with_size(size))
{
#ifdef SHARED_BUFFER_DEBUG
dbgprintf("Created shared buffer %d of size %d\n", m_shared_buffer_id, size);
@ -44,12 +44,14 @@ public:
size_t size() const { return m_vmobject->size(); }
void destroy_if_unused();
void seal();
PurgeableVMObject& vmobject() { return m_vmobject; }
const PurgeableVMObject& vmobject() const { return m_vmobject; }
int id() const { return m_shared_buffer_id; }
int m_shared_buffer_id { -1 };
bool m_writable { true };
bool m_global { false };
NonnullRefPtr<AnonymousVMObject> m_vmobject;
NonnullRefPtr<PurgeableVMObject> m_vmobject;
Vector<Reference, 2> m_refs;
unsigned m_total_refs { 0 };
};

View file

@ -146,7 +146,8 @@ typedef u32 socklen_t;
__ENUMERATE_SYSCALL(set_thread_name) \
__ENUMERATE_SYSCALL(get_thread_name) \
__ENUMERATE_SYSCALL(madvise) \
__ENUMERATE_SYSCALL(purge)
__ENUMERATE_SYSCALL(purge) \
__ENUMERATE_SYSCALL(set_shared_buffer_volatile)
namespace Syscall {

View file

@ -1,4 +1,5 @@
#include <AK/kmalloc.h>
#include <Kernel/Syscall.h>
#include <SharedBuffer.h>
#include <stdio.h>
#include <unistd.h>
@ -64,3 +65,19 @@ void SharedBuffer::seal()
ASSERT_NOT_REACHED();
}
}
void SharedBuffer::set_volatile()
{
u32 rc = syscall(SC_set_shared_buffer_volatile, m_shared_buffer_id, true);
ASSERT(rc == 0);
}
bool SharedBuffer::set_nonvolatile()
{
u32 rc = syscall(SC_set_shared_buffer_volatile, m_shared_buffer_id, false);
if (rc == 0)
return true;
if (rc == 1)
return false;
ASSERT_NOT_REACHED();
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <AK/RefPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
class SharedBuffer : public RefCounted<SharedBuffer> {
public:
@ -15,6 +15,8 @@ public:
int size() const { return m_size; }
void* data() { return m_data; }
const void* data() const { return m_data; }
void set_volatile();
[[nodiscard]] bool set_nonvolatile();
private:
SharedBuffer(int shared_buffer_id, int size, void*);