Skip to content

Commit 24f02bd

Browse files
committed
Kernel: Put Process's current directory in a SpinlockProtected
Also let's call it "current_directory" instead of "cwd" everywhere.
1 parent 71792e4 commit 24f02bd

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

Kernel/Process.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,20 @@ void Process::unprotect_data()
215215
});
216216
}
217217

218-
ErrorOr<NonnullRefPtr<Process>> Process::try_create(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
218+
ErrorOr<NonnullRefPtr<Process>> Process::try_create(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
219219
{
220220
auto space = TRY(Memory::AddressSpace::try_create(fork_parent ? &fork_parent->address_space() : nullptr));
221221
auto unveil_tree = UnveilNode { TRY(KString::try_create("/"sv)), UnveilMetadata(TRY(KString::try_create("/"sv))) };
222-
auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(move(name), uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty, move(unveil_tree))));
222+
auto process = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Process(move(name), uid, gid, ppid, is_kernel_process, move(current_directory), move(executable), tty, move(unveil_tree))));
223223
TRY(process->attach_resources(move(space), first_thread, fork_parent));
224224
return process;
225225
}
226226

227-
Process::Process(NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, UnveilNode unveil_tree)
227+
Process::Process(NonnullOwnPtr<KString> name, UserID uid, GroupID gid, ProcessID ppid, bool is_kernel_process, NonnullRefPtr<Custody> current_directory, RefPtr<Custody> executable, TTY* tty, UnveilNode unveil_tree)
228228
: m_name(move(name))
229229
, m_is_kernel_process(is_kernel_process)
230230
, m_executable(move(executable))
231-
, m_cwd(move(cwd))
231+
, m_current_directory(move(current_directory))
232232
, m_tty(tty)
233233
, m_unveiled_paths(move(unveil_tree))
234234
, m_wait_blocker_set(*this)
@@ -528,11 +528,13 @@ siginfo_t Process::wait_info() const
528528
return siginfo;
529529
}
530530

531-
Custody& Process::current_directory()
531+
NonnullRefPtr<Custody> Process::current_directory()
532532
{
533-
if (!m_cwd)
534-
m_cwd = VirtualFileSystem::the().root_custody();
535-
return *m_cwd;
533+
return m_current_directory.with([&](auto& current_directory) -> NonnullRefPtr<Custody> {
534+
if (!current_directory)
535+
current_directory = VirtualFileSystem::the().root_custody();
536+
return *current_directory;
537+
});
536538
}
537539

538540
ErrorOr<NonnullOwnPtr<KString>> Process::get_syscall_path_argument(Userspace<char const*> user_path, size_t path_length)
@@ -629,7 +631,6 @@ void Process::finalize()
629631
m_fds.with_exclusive([](auto& fds) { fds.clear(); });
630632
m_tty = nullptr;
631633
m_executable = nullptr;
632-
m_cwd = nullptr;
633634
m_arguments.clear();
634635
m_environment.clear();
635636

Kernel/Process.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class Process final
432432
u32 m_ticks_in_user_for_dead_children { 0 };
433433
u32 m_ticks_in_kernel_for_dead_children { 0 };
434434

435-
Custody& current_directory();
435+
NonnullRefPtr<Custody> current_directory();
436436
Custody* executable() { return m_executable.ptr(); }
437437
const Custody* executable() const { return m_executable.ptr(); }
438438

@@ -525,8 +525,8 @@ class Process final
525525
bool add_thread(Thread&);
526526
bool remove_thread(Thread&);
527527

528-
Process(NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, UnveilNode unveil_tree);
529-
static ErrorOr<NonnullRefPtr<Process>> try_create(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
528+
Process(NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory, RefPtr<Custody> executable, TTY* tty, UnveilNode unveil_tree);
529+
static ErrorOr<NonnullRefPtr<Process>> try_create(RefPtr<Thread>& first_thread, NonnullOwnPtr<KString> name, UserID, GroupID, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> current_directory = nullptr, RefPtr<Custody> executable = nullptr, TTY* = nullptr, Process* fork_parent = nullptr);
530530
ErrorOr<void> attach_resources(NonnullOwnPtr<Memory::AddressSpace>&&, RefPtr<Thread>& first_thread, Process* fork_parent);
531531
static ProcessID allocate_pid();
532532

@@ -788,7 +788,8 @@ class Process final
788788
bool m_should_generate_coredump { false };
789789

790790
RefPtr<Custody> m_executable;
791-
RefPtr<Custody> m_cwd;
791+
792+
SpinlockProtected<RefPtr<Custody>> m_current_directory;
792793

793794
NonnullOwnPtrVector<KString> m_arguments;
794795
NonnullOwnPtrVector<KString> m_environment;

Kernel/ProcessSpecificExposed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ ErrorOr<void> Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder)
274274

275275
ErrorOr<void> Process::procfs_get_current_work_directory_link(KBufferBuilder& builder) const
276276
{
277-
return builder.append(TRY(const_cast<Process&>(*this).current_directory().try_serialize_absolute_path())->view());
277+
return builder.append(TRY(const_cast<Process&>(*this).current_directory()->try_serialize_absolute_path())->view());
278278
}
279279

280280
mode_t Process::binary_link_required_mode() const

Kernel/Syscalls/chdir.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ ErrorOr<FlatPtr> Process::sys$chdir(Userspace<const char*> user_path, size_t pat
1515
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
1616
TRY(require_promise(Pledge::rpath));
1717
auto path = TRY(get_syscall_path_argument(user_path, path_length));
18-
m_cwd = TRY(VirtualFileSystem::the().open_directory(path->view(), current_directory()));
19-
return 0;
18+
return m_current_directory.with([&](auto& current_directory) -> ErrorOr<FlatPtr> {
19+
current_directory = TRY(VirtualFileSystem::the().open_directory(path->view(), *current_directory));
20+
return 0;
21+
});
2022
}
2123

2224
ErrorOr<FlatPtr> Process::sys$fchdir(int fd)
@@ -28,7 +30,9 @@ ErrorOr<FlatPtr> Process::sys$fchdir(int fd)
2830
return ENOTDIR;
2931
if (!description->metadata().may_execute(*this))
3032
return EACCES;
31-
m_cwd = description->custody();
33+
m_current_directory.with([&](auto& current_directory) {
34+
current_directory = description->custody();
35+
});
3236
return 0;
3337
}
3438

@@ -40,7 +44,7 @@ ErrorOr<FlatPtr> Process::sys$getcwd(Userspace<char*> buffer, size_t size)
4044
if (size > NumericLimits<ssize_t>::max())
4145
return EINVAL;
4246

43-
auto path = TRY(current_directory().try_serialize_absolute_path());
47+
auto path = TRY(current_directory()->try_serialize_absolute_path());
4448
size_t ideal_size = path->length() + 1;
4549
auto size_to_copy = min(ideal_size, size);
4650
TRY(copy_to_user(buffer, path->characters(), size_to_copy));

Kernel/Syscalls/fork.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ErrorOr<FlatPtr> Process::sys$fork(RegisterState& regs)
1919
TRY(require_promise(Pledge::proc));
2020
RefPtr<Thread> child_first_thread;
2121
auto child_name = TRY(m_name->try_clone());
22-
auto child = TRY(Process::try_create(child_first_thread, move(child_name), uid(), gid(), pid(), m_is_kernel_process, m_cwd, m_executable, m_tty, this));
22+
auto child = TRY(Process::try_create(child_first_thread, move(child_name), uid(), gid(), pid(), m_is_kernel_process, current_directory(), m_executable, m_tty, this));
2323
child->m_veil_state = m_veil_state;
2424
child->m_unveiled_paths = TRY(m_unveiled_paths.deep_copy());
2525

0 commit comments

Comments
 (0)