Jelajahi Sumber

Kernel: Fix deadlock in ~Memory::Region()

First off: unregister the region from MemoryManager before unmapping it.
The order of operations here was a bit strange, presumably to avoid a
situation where a fault would happen while unmapping, and the fault
handler would find the MemoryManager region list in an invalid state.

Unregistering it before unmapping sidesteps the whole problem, and
allows us to easily fix another problem: a deadlock could occur due
to inconsistent acquisition order (PageDirectory must come before MM.)
Andreas Kling 3 tahun lalu
induk
melakukan
08b4d8f0de
1 mengubah file dengan 4 tambahan dan 6 penghapusan
  1. 4 6
      Kernel/Memory/Region.cpp

+ 4 - 6
Kernel/Memory/Region.cpp

@@ -40,16 +40,14 @@ Region::~Region()
 {
     m_vmobject->remove_region(*this);
 
-    // Make sure we disable interrupts so we don't get interrupted between unmapping and unregistering.
-    // Unmapping the region will give the VM back to the VirtualRangeAllocator, so an interrupt handler would
-    // find the address<->region mappings in an invalid state there.
-    ScopedSpinLock lock(s_mm_lock);
+    MM.unregister_region(*this);
+
     if (m_page_directory) {
+        ScopedSpinLock page_lock(m_page_directory->get_lock());
+        ScopedSpinLock lock(s_mm_lock);
         unmap(ShouldDeallocateVirtualRange::Yes);
         VERIFY(!m_page_directory);
     }
-
-    MM.unregister_region(*this);
 }
 
 OwnPtr<Region> Region::clone()