malloc: Fix DEBUG_REDZONE for contigmalloc()

When free() was adapted to support allocations originating from
contigmalloc(), redzone(9) support was not included.  redzone(9)
involves adjusting the pointer to freed memory before looking up the
slab cookie, so it's not straightforward to make contigmalloc() opt out
of redzone support.

Thus, augment contigmalloc() to support redzone.

Reported by:	glebius
Tested by:	dhw
MFC after:	2 weeks
Fixes:		9e6544dd6e ("malloc(9): extend contigmalloc(9) by a "slab cookie"")
This commit is contained in:
Mark Johnston
2025-03-23 09:42:40 -04:00
parent 5748163568
commit 74361d693a
+22 -2
View File
@@ -477,11 +477,18 @@ contigmalloc_size(uma_slab_t slab)
}
void *
contigmalloc(unsigned long size, struct malloc_type *type, int flags,
contigmalloc(unsigned long osize, struct malloc_type *type, int flags,
vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
vm_paddr_t boundary)
{
void *ret;
unsigned long size;
#ifdef DEBUG_REDZONE
size = redzone_size_ntor(osize);
#else
size = osize;
#endif
ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment,
boundary, VM_MEMATTR_DEFAULT);
@@ -489,16 +496,26 @@ contigmalloc(unsigned long size, struct malloc_type *type, int flags,
/* Use low bits unused for slab pointers. */
vsetzoneslab((uintptr_t)ret, NULL, CONTIG_MALLOC_SLAB(size));
malloc_type_allocated(type, round_page(size));
#ifdef DEBUG_REDZONE
ret = redzone_setup(ret, osize);
#endif
}
return (ret);
}
void *
contigmalloc_domainset(unsigned long size, struct malloc_type *type,
contigmalloc_domainset(unsigned long osize, struct malloc_type *type,
struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high,
unsigned long alignment, vm_paddr_t boundary)
{
void *ret;
unsigned long size;
#ifdef DEBUG_REDZONE
size = redzone_size_ntor(osize);
#else
size = osize;
#endif
ret = (void *)kmem_alloc_contig_domainset(ds, size, flags, low, high,
alignment, boundary, VM_MEMATTR_DEFAULT);
@@ -506,6 +523,9 @@ contigmalloc_domainset(unsigned long size, struct malloc_type *type,
/* Use low bits unused for slab pointers. */
vsetzoneslab((uintptr_t)ret, NULL, CONTIG_MALLOC_SLAB(size));
malloc_type_allocated(type, round_page(size));
#ifdef DEBUG_REDZONE
ret = redzone_setup(ret, osize);
#endif
}
return (ret);
}