kernel: Add mechanism to replace specific object caches with the guarded heap.

This allows the guarded heap to be used in a much more targeted way,
if it's suspected specific object_caches have problems.

Enabled by default for KDEBUG. The new safemode/kernel option is
"guarded_heap_for_object_caches". This accepts a comma-separated
list of quoted strings, optionally with * at beginning and/or end
(but not middle.) Examples:

mbufs*   # matches caches starting with "mbufs"
*jumbo9*	# matches caches containing "jumbo9"
"*jumbo9 chunks","mbufs*" # matches caches ending with "jumbo9 chunks"
                          # or starting with "mbufs"

This should help with diagnosing #19973, among others, without needing
to compile custom versions of drivers or anything like that.

Unfortunately it is not possible to use this mechanism to selectively
replace parts of the slab malloc. The slab malloc free() relies on
the MemoryManager to figure out what cache an object belongs to, which
doesn't work if objects of some size classes aren't known to the
MemoryManager at all.
This commit is contained in:
Augustin Cavalier
2026-03-24 18:10:10 -04:00
parent 76df196f25
commit 7240f3762f
6 changed files with 276 additions and 54 deletions
+7 -3
View File
@@ -120,9 +120,6 @@
// space so it is quite easy to hit limits.
#define DEBUG_HEAPS_DEFAULT slab
// Replace the object cache with the chosen debug heap to force debug features.
#define USE_DEBUG_HEAPS_FOR_OBJECT_CACHE 0
// If 0, disables memory re-use by default in the guarded heap (can be overridden
// via "guarded_heap_options".) This means freed virtual memory is never reused and
// stays invalid, causing any access to crash. This is a magnitude more space
@@ -130,6 +127,13 @@
// to address space waste.
#define DEBUG_GUARDED_HEAP_MEMORY_REUSE_DEFAULT 1
// If DEBUG_HEAPS is enabled, allow the guarded heap to replace select object_cache(s)
// (but not slab malloc ones) specified via "guarded_heap_for_object_caches".
#define GUARDED_HEAP_CAN_REPLACE_OBJECT_CACHES 1
// Replace all object caches with the chosen debug heap to force debug features.
#define USE_DEBUG_HEAPS_FOR_ALL_OBJECT_CACHES 0
// Enables additional sanity checks in the slab allocator's memory manager.
#define DEBUG_SLAB_MEMORY_MANAGER_PARANOID_CHECKS 0