Skip to content

Commit

Permalink
UCT/IB: Fix teardown path on memory handler callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tvegas1 committed Sep 25, 2024
1 parent 9b830f3 commit 0475192
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions src/uct/ib/base/ib_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ typedef struct {
void *address;
size_t length;
size_t first_mr_size;
int mr_idx;
const uct_md_mem_reg_params_t *params;
uint64_t access_flags;
struct ibv_mr **mrs;
Expand Down Expand Up @@ -288,30 +289,25 @@ void *uct_ib_md_mem_handle_thread_func(void *arg)
size_t chunk_size = ctx->md->config.mt_reg_chunk;
ucs_time_t UCS_V_UNUSED t0 = ucs_get_time();
void UCS_V_UNUSED *start = ctx->address;
int mr_idx = 0;
size_t length = ctx->first_mr_size;
int i;
ucs_status_t status;

while (ctx->length > 0) {
if (ctx->params != NULL) {
status = uct_ib_reg_mr(ctx->md, ctx->address, length, ctx->params,
ctx->access_flags, NULL, &ctx->mrs[mr_idx]);
ctx->access_flags, NULL,
&ctx->mrs[ctx->mr_idx]);
if (status != UCS_OK) {
goto err_dereg;
}
} else {
status = uct_ib_dereg_mr(ctx->mrs[mr_idx]);
if (status != UCS_OK) {
goto err;
}

ctx->mrs[mr_idx] = NULL;
} else {
(void)uct_ib_dereg_mr(ctx->mrs[ctx->mr_idx]);
}
ctx->address = UCS_PTR_BYTE_OFFSET(ctx->address, length);
ctx->length -= length;
length = ucs_min(ctx->length, chunk_size);
mr_idx++;
ctx->mr_idx++;
}

ucs_trace("%s %p..%p (first_mr_size %zu) took %f usec\n",
Expand All @@ -321,19 +317,10 @@ void *uct_ib_md_mem_handle_thread_func(void *arg)
return UCS_STATUS_PTR(UCS_OK);

err_dereg:
for (i = 0; i < mr_idx; i++) {
uct_ib_dereg_mr(ctx->mrs[i]);
ctx->mrs[i] = NULL;
while (ctx->mr_idx-- > 0) {
(void)uct_ib_dereg_mr(ctx->mrs[ctx->mr_idx]);
}

while (ctx->length > 0) {
ctx->mrs[mr_idx] = NULL;
ctx->length -= length;
length = ucs_min(ctx->length, chunk_size);
mr_idx++;
}

err:
return UCS_STATUS_PTR(status);
}

Expand Down Expand Up @@ -393,6 +380,7 @@ uct_ib_md_handle_mr_list_mt(uct_ib_md_t *md, void *address, size_t length,
ctx->params = params;
ctx->access_flags = access_flags;
ctx->mrs = &mrs[mr_idx];
ctx->mr_idx = 0;

/* First MR size can be different to align further MRs */
padding = ucs_padding((uintptr_t)ctx->address, chunk_size);
Expand All @@ -401,7 +389,6 @@ uct_ib_md_handle_mr_list_mt(uct_ib_md_t *md, void *address, size_t length,
ucs_assertv((ctx->address == address) || (padding == 0),
"thread_idx=%d address=%p padding=%zu",
thread_idx, address, padding);

ctx->length = (thread_num_mrs - 1) * chunk_size +
ctx->first_mr_size;
ctx->length = ucs_min(ctx->length, length - offset);
Expand All @@ -425,7 +412,6 @@ uct_ib_md_handle_mr_list_mt(uct_ib_md_t *md, void *address, size_t length,
ucs_error("pthread_create() failed: %m");
status = UCS_ERR_IO_ERROR;
thread_num = thread_idx;
mr_num = mr_idx;
break;
}

Expand All @@ -440,18 +426,17 @@ uct_ib_md_handle_mr_list_mt(uct_ib_md_t *md, void *address, size_t length,
}
}

ucs_free(ctxs);
pthread_attr_destroy(&attr);

if (status != UCS_OK) {
for (mr_idx = 0; mr_idx < mr_num; mr_idx++) {
if (mrs[mr_idx] != NULL) {
/* coverity[check_return] */
uct_ib_dereg_mr(mrs[mr_idx]);
if ((status != UCS_OK) && (params != NULL)) {
for (thread_idx = 0; thread_idx < thread_num; thread_idx++) {
for (mr_idx = 0; mr_idx < ctxs[thread_idx].mr_idx; mr_idx++) {
(void)uct_ib_dereg_mr(ctxs[thread_idx].mrs[mr_idx]);
}
}
}

ucs_free(ctxs);
pthread_attr_destroy(&attr);

return status;
}

Expand Down

0 comments on commit 0475192

Please sign in to comment.