Skip to content

Commit 0d22836

Browse files
committed
Add lock to umfMemoryTrackerAdd() and umfMemoryTrackerRemove()
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent b79de25 commit 0d22836

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/provider/provider_tracking.c

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct umf_memory_tracker_t {
3737
// when one memory pool acts as a memory provider
3838
// for another memory pool (nested memory pooling).
3939
critnib *alloc_segments_map[MAX_LEVELS_OF_ALLOC_SEGMENT_MAP];
40-
utils_mutex_t splitMergeMutex;
40+
utils_mutex_t mutex;
4141
};
4242

4343
typedef struct tracker_alloc_info_t {
@@ -179,6 +179,11 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
179179
int level = 0;
180180
int found = 0;
181181

182+
int ret = utils_mutex_lock(&hTracker->mutex);
183+
if (ret) {
184+
return UMF_RESULT_ERROR_UNKNOWN;
185+
}
186+
182187
// Find the most nested (in the highest level) entry
183188
// in the critnib maps that contains the given 'ptr' pointer.
184189
do {
@@ -197,7 +202,8 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
197202
// TODO: we need to support an arbitrary amount of layers in the future
198203
LOG_ERR("tracker level is too high, ptr=%p, size=%zu", ptr,
199204
size);
200-
return UMF_RESULT_ERROR_OUT_OF_RESOURCES;
205+
umf_result = UMF_RESULT_ERROR_OUT_OF_RESOURCES;
206+
goto err_unlock;
201207
}
202208
if (((uintptr_t)ptr + size) > (rkey + rsize)) {
203209
LOG_ERR(
@@ -206,7 +212,8 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
206212
"that exceeds the parent value (pool=%p, ptr=%p, size=%zu)",
207213
(void *)pool, ptr, size, (void *)rvalue->pool, (void *)rkey,
208214
(size_t)rsize);
209-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
215+
umf_result = UMF_RESULT_ERROR_INVALID_ARGUMENT;
216+
goto err_unlock;
210217
}
211218
parent_key = rkey;
212219
parent_value = rvalue;
@@ -217,10 +224,15 @@ static umf_result_t umfMemoryTrackerAdd(umf_memory_tracker_handle_t hTracker,
217224
umf_result = umfMemoryTrackerAddAtLevel(hTracker, level, pool, ptr, size,
218225
parent_key, parent_value);
219226
if (umf_result != UMF_RESULT_SUCCESS) {
220-
return umf_result;
227+
goto err_unlock;
221228
}
222229

223-
return UMF_RESULT_SUCCESS;
230+
umf_result = UMF_RESULT_SUCCESS;
231+
232+
err_unlock:
233+
utils_mutex_unlock(&hTracker->mutex);
234+
235+
return umf_result;
224236
}
225237

226238
static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
@@ -232,17 +244,23 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
232244
// Every umfMemoryTrackerAdd(..., ptr, ...) should have a corresponding
233245
// umfMemoryTrackerRemove call with the same ptr value.
234246

247+
umf_result_t umf_result = UMF_RESULT_ERROR_UNKNOWN;
235248
tracker_alloc_info_t *parent_value = NULL;
236249
uintptr_t parent_key = 0;
237250
int level = 0;
238251

252+
int ret = utils_mutex_lock(&hTracker->mutex);
253+
if (ret) {
254+
return UMF_RESULT_ERROR_UNKNOWN;
255+
}
256+
239257
// Find the most nested (on the highest level) entry in the map
240258
// with the `ptr` key and with no children - only such entry can be removed.
241259
tracker_alloc_info_t *value = get_most_nested_alloc_segment(
242260
hTracker, ptr, &level, &parent_key, &parent_value, 1 /* no_children */);
243261
if (!value) {
244262
LOG_ERR("pointer %p not found in the alloc_segments_map", ptr);
245-
return UMF_RESULT_ERROR_UNKNOWN;
263+
goto err_unlock;
246264
}
247265

248266
assert(level < MAX_LEVELS_OF_ALLOC_SEGMENT_MAP);
@@ -264,7 +282,12 @@ static umf_result_t umfMemoryTrackerRemove(umf_memory_tracker_handle_t hTracker,
264282

265283
umf_ba_free(hTracker->alloc_info_allocator, value);
266284

267-
return UMF_RESULT_SUCCESS;
285+
umf_result = UMF_RESULT_SUCCESS;
286+
287+
err_unlock:
288+
utils_mutex_unlock(&hTracker->mutex);
289+
290+
return umf_result;
268291
}
269292

270293
umf_memory_pool_handle_t umfMemoryTrackerGetPool(const void *ptr) {
@@ -404,7 +427,7 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
404427
splitValue->size = firstSize;
405428
splitValue->n_children = 0;
406429

407-
int r = utils_mutex_lock(&provider->hTracker->splitMergeMutex);
430+
int r = utils_mutex_lock(&provider->hTracker->mutex);
408431
if (r) {
409432
goto err_lock;
410433
}
@@ -470,7 +493,7 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
470493

471494
// free the original value
472495
umf_ba_free(provider->hTracker->alloc_info_allocator, value);
473-
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
496+
utils_mutex_unlock(&provider->hTracker->mutex);
474497

475498
LOG_DEBUG(
476499
"split memory region (level=%i): ptr=%p, totalSize=%zu, firstSize=%zu",
@@ -479,7 +502,7 @@ static umf_result_t trackingAllocationSplit(void *hProvider, void *ptr,
479502
return UMF_RESULT_SUCCESS;
480503

481504
err:
482-
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
505+
utils_mutex_unlock(&provider->hTracker->mutex);
483506
err_lock:
484507
umf_ba_free(provider->hTracker->alloc_info_allocator, splitValue);
485508

@@ -511,7 +534,7 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
511534
int lowLevel = -2;
512535
int highLevel = -1;
513536

514-
int r = utils_mutex_lock(&provider->hTracker->splitMergeMutex);
537+
int r = utils_mutex_lock(&provider->hTracker->mutex);
515538
if (r) {
516539
goto err_lock;
517540
}
@@ -578,7 +601,7 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
578601

579602
umf_ba_free(provider->hTracker->alloc_info_allocator, erasedhighValue);
580603

581-
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
604+
utils_mutex_unlock(&provider->hTracker->mutex);
582605

583606
LOG_DEBUG("merged memory regions (level=%i): lowPtr=%p (child=%zu), "
584607
"highPtr=%p (child=%zu), totalSize=%zu",
@@ -593,7 +616,7 @@ static umf_result_t trackingAllocationMerge(void *hProvider, void *lowPtr,
593616
assert(0);
594617

595618
not_merged:
596-
utils_mutex_unlock(&provider->hTracker->splitMergeMutex);
619+
utils_mutex_unlock(&provider->hTracker->mutex);
597620

598621
err_lock:
599622
umf_ba_free(provider->hTracker->alloc_info_allocator, mergedValue);
@@ -1077,7 +1100,7 @@ umf_memory_tracker_handle_t umfMemoryTrackerCreate(void) {
10771100

10781101
handle->alloc_info_allocator = alloc_info_allocator;
10791102

1080-
void *mutex_ptr = utils_mutex_init(&handle->splitMergeMutex);
1103+
void *mutex_ptr = utils_mutex_init(&handle->mutex);
10811104
if (!mutex_ptr) {
10821105
goto err_destroy_alloc_info_allocator;
10831106
}
@@ -1101,7 +1124,7 @@ umf_memory_tracker_handle_t umfMemoryTrackerCreate(void) {
11011124
critnib_delete(handle->alloc_segments_map[j]);
11021125
}
11031126
}
1104-
utils_mutex_destroy_not_free(&handle->splitMergeMutex);
1127+
utils_mutex_destroy_not_free(&handle->mutex);
11051128
err_destroy_alloc_info_allocator:
11061129
umf_ba_destroy(alloc_info_allocator);
11071130
err_free_handle:
@@ -1134,7 +1157,7 @@ void umfMemoryTrackerDestroy(umf_memory_tracker_handle_t handle) {
11341157
handle->alloc_segments_map[i] = NULL;
11351158
}
11361159
}
1137-
utils_mutex_destroy_not_free(&handle->splitMergeMutex);
1160+
utils_mutex_destroy_not_free(&handle->mutex);
11381161
umf_ba_destroy(handle->alloc_info_allocator);
11391162
handle->alloc_info_allocator = NULL;
11401163
umf_ba_global_free(handle);

0 commit comments

Comments
 (0)