Fix crash in MemoryManager::PerformMaintenance()

sFreeAreaCount wasn't decremented after removing an area from
sFreeAreas, thus causing the loop to continue until enountering and
crashing on a NULL pointer after removing the last area. Introduce
helper methods _PushFreeArea() and _PopFreeArea() to ensure this cannot
easily happen again.

Fixes ticket #8972.
This commit is contained in:
Ingo Weinhold
2012-09-11 22:31:19 +02:00
parent 8040911a25
commit 1bd0748253
2 changed files with 30 additions and 13 deletions
+7 -13
View File
@@ -22,7 +22,6 @@
#include "kernel_debug_config.h" #include "kernel_debug_config.h"
#include "ObjectCache.h" #include "ObjectCache.h"
#include "slab_private.h"
//#define TRACE_MEMORY_MANAGER //#define TRACE_MEMORY_MANAGER
@@ -849,15 +848,13 @@ MemoryManager::PerformMaintenance()
if (_AllocateArea(0, area) != B_OK) if (_AllocateArea(0, area) != B_OK)
return; return;
_push(sFreeAreas, area); _PushFreeArea(area);
if (++sFreeAreaCount > 2) if (sFreeAreaCount > 2)
sMaintenanceNeeded = true; sMaintenanceNeeded = true;
} else { } else {
// free until we only have two free ones // free until we only have two free ones
while (sFreeAreaCount > 2) { while (sFreeAreaCount > 2)
Area* area = _pop(sFreeAreas); _FreeArea(_PopFreeArea(), true, 0);
_FreeArea(area, true, 0);
}
if (sFreeAreaCount == 0) if (sFreeAreaCount == 0)
sMaintenanceNeeded = true; sMaintenanceNeeded = true;
@@ -956,8 +953,7 @@ MemoryManager::_AllocateChunks(size_t chunkSize, uint32 chunkCount,
return B_OK; return B_OK;
if (sFreeAreas != NULL) { if (sFreeAreas != NULL) {
_AddArea(_pop(sFreeAreas)); _AddArea(_PopFreeArea());
sFreeAreaCount--;
_RequestMaintenance(); _RequestMaintenance();
_GetChunks(metaChunkList, chunkSize, chunkCount, _metaChunk, _chunk); _GetChunks(metaChunkList, chunkSize, chunkCount, _metaChunk, _chunk);
@@ -1409,16 +1405,14 @@ MemoryManager::_FreeArea(Area* area, bool areaRemoved, uint32 flags)
// We want to keep one or two free areas as a reserve. // We want to keep one or two free areas as a reserve.
if (sFreeAreaCount <= 1) { if (sFreeAreaCount <= 1) {
_push(sFreeAreas, area); _PushFreeArea(area);
sFreeAreaCount++;
return; return;
} }
if (area->vmArea == NULL || (flags & CACHE_DONT_LOCK_KERNEL_SPACE) != 0) { if (area->vmArea == NULL || (flags & CACHE_DONT_LOCK_KERNEL_SPACE) != 0) {
// This is either early in the boot process or we aren't allowed to // This is either early in the boot process or we aren't allowed to
// delete the area now. // delete the area now.
_push(sFreeAreas, area); _PushFreeArea(area);
sFreeAreaCount++;
_RequestMaintenance(); _RequestMaintenance();
return; return;
} }
+23
View File
@@ -15,6 +15,7 @@
#include <util/OpenHashTable.h> #include <util/OpenHashTable.h>
#include "slab_debug.h" #include "slab_debug.h"
#include "slab_private.h"
class AbstractTraceEntryWithStackTrace; class AbstractTraceEntryWithStackTrace;
@@ -161,6 +162,9 @@ private:
static void _PrepareMetaChunk(MetaChunk* metaChunk, static void _PrepareMetaChunk(MetaChunk* metaChunk,
size_t chunkSize); size_t chunkSize);
static void _PushFreeArea(Area* area);
static Area* _PopFreeArea();
static void _AddArea(Area* area); static void _AddArea(Area* area);
static status_t _AllocateArea(uint32 flags, Area*& _area); static status_t _AllocateArea(uint32 flags, Area*& _area);
static void _FreeArea(Area* area, bool areaRemoved, static void _FreeArea(Area* area, bool areaRemoved,
@@ -235,6 +239,25 @@ MemoryManager::MaintenanceNeeded()
} }
/*static*/ inline void
MemoryManager::_PushFreeArea(Area* area)
{
_push(sFreeAreas, area);
sFreeAreaCount++;
}
/*static*/ inline MemoryManager::Area*
MemoryManager::_PopFreeArea()
{
if (sFreeAreaCount == 0)
return NULL;
sFreeAreaCount--;
return _pop(sFreeAreas);
}
/*static*/ inline addr_t /*static*/ inline addr_t
MemoryManager::_AreaBaseAddressForAddress(addr_t address) MemoryManager::_AreaBaseAddressForAddress(addr_t address)
{ {