From 12d6ecf0df54dc8752f7ccd1f430d74f45fcbd9b Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Fri, 3 Jan 2025 12:40:13 -0500 Subject: [PATCH] kernel/fs: Allocate FD tables separately instead of in one malloc(). This way, we significantly increase the FD table sizes that can be allocated without needing a "raw" allocation: previously an FD table size of 512 would've been too large (on x86_64), while now, tables of up to size 1024 will fit (so long as the largest block allocator size is 8192, anyway.) --- src/system/kernel/fs/vfs.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/system/kernel/fs/vfs.cpp b/src/system/kernel/fs/vfs.cpp index fd75bbb357..c69d3967ae 100644 --- a/src/system/kernel/fs/vfs.cpp +++ b/src/system/kernel/fs/vfs.cpp @@ -5046,17 +5046,22 @@ vfs_resize_fd_table(struct io_context* context, uint32 newSize) select_info** oldSelectInfos = context->select_infos; uint8* oldCloseOnExecTable = context->fds_close_on_exec; - // allocate new tables + // allocate new tables (separately to reduce the chances of needing a raw allocation) file_descriptor** newFDs = (file_descriptor**)malloc( - sizeof(struct file_descriptor*) * newSize - + sizeof(struct select_infos**) * newSize - + newCloseOnExitBitmapSize); - if (newFDs == NULL) + sizeof(struct file_descriptor*) * newSize); + select_info** newSelectInfos = (select_info**)malloc( + + sizeof(select_info**) * newSize); + uint8* newCloseOnExecTable = (uint8*)malloc(newCloseOnExitBitmapSize); + if (newFDs == NULL || newSelectInfos == NULL || newCloseOnExecTable == NULL) { + free(newFDs); + free(newSelectInfos); + free(newCloseOnExecTable); return B_NO_MEMORY; + } context->fds = newFDs; - context->select_infos = (select_info**)(context->fds + newSize); - context->fds_close_on_exec = (uint8*)(context->select_infos + newSize); + context->select_infos = newSelectInfos; + context->fds_close_on_exec = newCloseOnExecTable; context->table_size = newSize; if (oldSize != 0) { @@ -5079,6 +5084,8 @@ vfs_resize_fd_table(struct io_context* context, uint32 newSize) } free(oldFDs); + free(oldSelectInfos); + free(oldCloseOnExecTable); return B_OK; }