Skip to content

Commit 3b16dc3

Browse files
committed
fix aligned chunk address calc in disjoint pool
1 parent 6c85540 commit 3b16dc3

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/pool/pool_disjoint.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// Temporary solution for disabling memory poisoning. This is needed because
1111
// AddressSanitizer does not support memory poisoning for GPU allocations.
1212
// More info: https://github.com/oneapi-src/unified-memory-framework/issues/634
13+
14+
// TODO - add a param to disjoint pool to disable memory poisoning
1315
#ifndef POISON_MEMORY
1416
#undef __SANITIZE_ADDRESS__
1517
#endif
@@ -94,9 +96,6 @@ static slab_t *create_slab(bucket_t *bucket) {
9496
goto free_slab_chunks;
9597
}
9698

97-
// TODO
98-
// ASSERT_IS_ALIGNED((uintptr_t)slab->mem_ptr, bucket->size);
99-
10099
// raw allocation is not available for user so mark it as inaccessible
101100
utils_annotate_memory_inaccessible(slab->mem_ptr, slab->slab_size);
102101

@@ -178,7 +177,8 @@ static void slab_free_chunk(slab_t *slab, void *ptr) {
178177
// Even if the pointer p was previously aligned, it's still inside the
179178
// corresponding chunk, so we get the correct index here.
180179
size_t chunk_idx =
181-
((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) / slab->bucket->size;
180+
(size_t)floor((double)((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) /
181+
slab->bucket->size);
182182

183183
// Make sure that the chunk was allocated
184184
assert(slab->chunks[chunk_idx] && "double free detected");
@@ -738,8 +738,6 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
738738
}
739739
}
740740

741-
utils_mutex_unlock(&bucket->bucket_lock);
742-
743741
if (disjoint_pool->params.pool_trace > 2) {
744742
LOG_DEBUG("Allocated %8zu %s bytes aligned at %zu from %s -> %p", size,
745743
disjoint_pool->params.name, alignment,
@@ -749,6 +747,8 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
749747
void *aligned_ptr = (void *)ALIGN_UP_SAFE((size_t)ptr, alignment);
750748
VALGRIND_DO_MEMPOOL_ALLOC(disjoint_pool, aligned_ptr, size);
751749
utils_annotate_memory_undefined(aligned_ptr, size);
750+
751+
utils_mutex_unlock(&bucket->bucket_lock);
752752
return aligned_ptr;
753753
}
754754

@@ -804,10 +804,20 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
804804

805805
bucket_t *bucket = slab->bucket;
806806

807-
VALGRIND_DO_MEMPOOL_FREE(pool, ptr);
808807
utils_mutex_lock(&bucket->bucket_lock);
809808

810-
utils_annotate_memory_inaccessible(ptr, bucket->size);
809+
// TODO valgrind
810+
VALGRIND_DO_MEMPOOL_FREE(pool, ptr);
811+
812+
// Get the unalgined pointer
813+
// NOTE: the base pointer slab->mem_ptr needn't to be aligned to bucket size
814+
size_t chunk_idx =
815+
floor((double)((uintptr_t)ptr - (uintptr_t)slab->mem_ptr) /
816+
slab->bucket->size);
817+
void *unaligned_ptr =
818+
(void *)((uintptr_t)slab->mem_ptr + chunk_idx * slab->bucket->size);
819+
820+
utils_annotate_memory_inaccessible(unaligned_ptr, bucket->size);
811821
bucket_free_chunk(bucket, ptr, slab, &to_pool);
812822

813823
if (disjoint_pool->params.pool_trace > 1) {

src/pool/pool_disjoint_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <assert.h>
1212
#include <ctype.h>
1313
#include <errno.h>
14+
#include <math.h>
1415
#include <stdbool.h>
1516
#include <stdlib.h>
1617
#include <string.h>

0 commit comments

Comments
 (0)