1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:48:12 +00:00

Kernel: Introduce the new ProcFS design

The new ProcFS design consists of two main parts:
1. The representative ProcFS class, which is derived from the FS class.
The ProcFS and its inodes are much more lean - merely 3 classes to
represent the common type of inodes - regular files, symbolic links and
directories. They're backed by a ProcFSExposedComponent object, which
is responsible for the functional operation behind the scenes.
2. The backend of the ProcFS - the ProcFSComponentsRegistrar class
and all derived classes from the ProcFSExposedComponent class. These
together form the entire backend and handle all the functions you can
expect from the ProcFS.

The ProcFSExposedComponent derived classes split to 3 types in the
manner of lifetime in the kernel:
1. Persistent objects - this category includes all basic objects, like
the root folder, /proc/bus folder, main blob files in the root folders,
etc. These objects are persistent and cannot die ever.
2. Semi-persistent objects - this category includes all PID folders,
and subdirectories to the PID folders. It also includes exposed objects
like the unveil JSON'ed blob. These object are persistent as long as the
the responsible process they represent is still alive.
3. Dynamic objects - this category includes files in the subdirectories
of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially,
these objects are always created dynamically and when no longer in need
after being used, they're deallocated.
Nevertheless, the new allocated backend objects and inodes try to use
the same InodeIndex if possible - this might change only when a thread
dies and a new thread is born with a new thread stack, or when a file
descriptor is closed and a new one within the same file descriptor
number is opened. This is needed to actually be able to do something
useful with these objects.

The new design assures that many ProcFS instances can be used at once,
with one backend for usage for all instances.
This commit is contained in:
Liav A 2021-06-12 04:23:58 +03:00 committed by Andreas Kling
parent 1baa05d6b2
commit 12b6e69150
12 changed files with 2294 additions and 1774 deletions

View file

@ -24,6 +24,7 @@
#include <Kernel/PerformanceEventBuffer.h>
#include <Kernel/PerformanceManager.h>
#include <Kernel/Process.h>
#include <Kernel/ProcessExposed.h>
#include <Kernel/RTC.h>
#include <Kernel/Sections.h>
#include <Kernel/StdLib.h>
@ -129,6 +130,15 @@ void Process::kill_all_threads()
});
}
void Process::register_new(Process& process)
{
// Note: this is essentially the same like process->ref()
RefPtr<Process> new_process = process;
ScopedSpinLock lock(g_processes_lock);
g_processes->prepend(process);
ProcFSComponentsRegistrar::the().register_new_process(process);
}
RefPtr<Process> Process::create_user_process(RefPtr<Thread>& first_thread, const String& path, uid_t uid, gid_t gid, ProcessID parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
{
auto parts = path.split('/');
@ -166,11 +176,7 @@ RefPtr<Process> Process::create_user_process(RefPtr<Thread>& first_thread, const
return {};
}
{
process->ref();
ScopedSpinLock lock(g_processes_lock);
g_processes->prepend(*process);
}
register_new(*process);
error = 0;
return process;
}
@ -189,9 +195,7 @@ RefPtr<Process> Process::create_kernel_process(RefPtr<Thread>& first_thread, Str
#endif
if (process->pid() != 0) {
process->ref();
ScopedSpinLock lock(g_processes_lock);
g_processes->prepend(*process);
register_new(*process);
}
ScopedSpinLock lock(g_scheduler_lock);
@ -391,6 +395,7 @@ RefPtr<Process> Process::from_pid(ProcessID pid)
RefPtr<FileDescription> Process::file_description(int fd) const
{
ScopedSpinLock lock(m_fds_lock);
if (fd < 0)
return nullptr;
if (static_cast<size_t>(fd) < m_fds.size())
@ -400,6 +405,7 @@ RefPtr<FileDescription> Process::file_description(int fd) const
int Process::fd_flags(int fd) const
{
ScopedSpinLock lock(m_fds_lock);
if (fd < 0)
return -1;
if (static_cast<size_t>(fd) < m_fds.size())
@ -409,9 +415,10 @@ int Process::fd_flags(int fd) const
int Process::number_of_open_file_descriptors() const
{
ScopedSpinLock lock(m_fds_lock);
int count = 0;
for (auto& description : m_fds) {
if (description)
for (size_t index = 0; index < m_fds.size(); index++) {
if (m_fds[index].is_valid())
++count;
}
return count;
@ -419,6 +426,7 @@ int Process::number_of_open_file_descriptors() const
int Process::alloc_fd(int first_candidate_fd)
{
ScopedSpinLock lock(m_fds_lock);
for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
if (!m_fds[i])
return i;
@ -535,6 +543,12 @@ void Process::finalize()
m_arguments.clear();
m_environment.clear();
// Note: We need to remove the references from the ProcFS registrar
// If we don't do it here, we can't drop the object later, and we can't
// do this from the destructor because the state of the object doesn't
// allow us to take references anymore.
ProcFSComponentsRegistrar::the().unregister_process(*this);
m_dead = true;
{
@ -676,14 +690,24 @@ RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_d
void Process::FileDescriptionAndFlags::clear()
{
// FIXME: Verify Process::m_fds_lock is locked!
m_description = nullptr;
m_flags = 0;
m_global_procfs_inode_index = 0;
}
void Process::FileDescriptionAndFlags::refresh_inode_index()
{
// FIXME: Verify Process::m_fds_lock is locked!
m_global_procfs_inode_index = ProcFSComponentsRegistrar::the().allocate_inode_index();
}
void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& description, u32 flags)
{
// FIXME: Verify Process::m_fds_lock is locked!
m_description = move(description);
m_flags = flags;
m_global_procfs_inode_index = ProcFSComponentsRegistrar::the().allocate_inode_index();
}
Custody& Process::root_directory()