From 164c9617c360cd2766754dff5a4299c47be6e5cc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 16 Aug 2022 20:39:45 +0200 Subject: [PATCH] Kernel: Only lock file descriptor table once in sys$pipe() Instead of locking it twice, we now frontload all the work that doesn't touch the fd table, and then only lock it towards the end of the syscall. The benefit here is simplicity. The downside is that we do a bit of unnecessary work in the EMFILE error case, but we don't need to optimize that case anyway. --- Kernel/Syscalls/pipe.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Kernel/Syscalls/pipe.cpp b/Kernel/Syscalls/pipe.cpp index 6db456e731..e6b8e90879 100644 --- a/Kernel/Syscalls/pipe.cpp +++ b/Kernel/Syscalls/pipe.cpp @@ -21,15 +21,6 @@ ErrorOr Process::sys$pipe(Userspace pipefd, int flags) u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0; auto fifo = TRY(FIFO::try_create(uid())); - ScopedDescriptionAllocation reader_fd_allocation; - ScopedDescriptionAllocation writer_fd_allocation; - - TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr { - reader_fd_allocation = TRY(fds.allocate()); - writer_fd_allocation = TRY(fds.allocate()); - return {}; - })); - auto reader_description = TRY(fifo->open_direction(FIFO::Direction::Reader)); auto writer_description = TRY(fifo->open_direction(FIFO::Direction::Writer)); @@ -41,6 +32,9 @@ ErrorOr Process::sys$pipe(Userspace pipefd, int flags) } TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr { + auto reader_fd_allocation = TRY(fds.allocate()); + auto writer_fd_allocation = TRY(fds.allocate()); + fds[reader_fd_allocation.fd].set(move(reader_description), fd_flags); fds[writer_fd_allocation.fd].set(move(writer_description), fd_flags);