From 60fee365f5670c1e13cc259ab1a026b16b0c5d44 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 9 Sep 2021 10:32:16 -0400 Subject: [PATCH] kernel/vm: Allow locking kernel space if allocating page_protections for userspace. Some applications may request per-page protections for an especially large area (e.g. multiple GB), which leads to allocating a rather large page protections area (e.g. 512KB), which we cannot allocate without locking the kernel space to get new slabs. We only need to avoid locking the kernel space if the area in question is in the kernel space, as in that case, kernel space will already be read-locked by the current thread. Fixes #16898. Change-Id: If52413a594da66edfc2821811d959085a2c3c78e Reviewed-on: https://review.haiku-os.org/c/haiku/+/4436 Reviewed-by: waddlesplash --- src/system/kernel/vm/VMArea.cpp | 7 ++----- src/system/kernel/vm/vm.cpp | 7 +++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/system/kernel/vm/VMArea.cpp b/src/system/kernel/vm/VMArea.cpp index 7f9d8650f4..e55e6812f9 100644 --- a/src/system/kernel/vm/VMArea.cpp +++ b/src/system/kernel/vm/VMArea.cpp @@ -48,11 +48,8 @@ VMArea::VMArea(VMAddressSpace* addressSpace, uint32 wiring, uint32 protection) VMArea::~VMArea() { - const uint32 flags = HEAP_DONT_WAIT_FOR_MEMORY - | HEAP_DONT_LOCK_KERNEL_SPACE; - // TODO: This might be stricter than necessary. - - free_etc(page_protections, flags); + free_etc(page_protections, address_space == VMAddressSpace::Kernel() + ? HEAP_DONT_WAIT_FOR_MEMORY | HEAP_DONT_LOCK_KERNEL_SPACE : 0); } diff --git a/src/system/kernel/vm/vm.cpp b/src/system/kernel/vm/vm.cpp index 05e259b9c0..665ecb8f33 100644 --- a/src/system/kernel/vm/vm.cpp +++ b/src/system/kernel/vm/vm.cpp @@ -465,7 +465,8 @@ allocate_area_page_protections(VMArea* area) // so we use 4 bits per page. size_t bytes = (area->Size() / B_PAGE_SIZE + 1) / 2; area->page_protections = (uint8*)malloc_etc(bytes, - HEAP_DONT_LOCK_KERNEL_SPACE); + area->address_space == VMAddressSpace::Kernel() + ? HEAP_DONT_LOCK_KERNEL_SPACE : 0); if (area->page_protections == NULL) return B_NO_MEMORY; @@ -2634,7 +2635,9 @@ vm_copy_area(team_id team, const char* name, void** _address, if (source->page_protections != NULL) { size_t bytes = (source->Size() / B_PAGE_SIZE + 1) / 2; targetPageProtections = (uint8*)malloc_etc(bytes, - HEAP_DONT_LOCK_KERNEL_SPACE); + (source->address_space == VMAddressSpace::Kernel() + || targetAddressSpace == VMAddressSpace::Kernel()) + ? HEAP_DONT_LOCK_KERNEL_SPACE : 0); if (targetPageProtections == NULL) return B_NO_MEMORY;