Skip to content

Commit 0d2e3cb

Browse files
committed
TL/UCP: various memh fixes
1 parent 56a3874 commit 0d2e3cb

2 files changed

Lines changed: 39 additions & 15 deletions

File tree

src/components/tl/ucp/tl_ucp_context.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,7 @@ ucc_status_t ucc_tl_ucp_mem_map_offload_import(ucc_tl_ucp_context_t *ctx,
716716
if (strncmp(tl_h->tl_name, name, UCC_MEM_MAP_TL_NAME_LEN - 1) == 0) {
717717
break;
718718
}
719-
/* this is not the index, skip this section of buffer if exists */
720-
offset += *packed_size;
719+
offset += UCC_MEM_MAP_TL_NAME_LEN + sizeof(size_t) + *packed_size;
721720
}
722721
if (i == l_memh->num_tls) {
723722
tl_error(ctx->super.super.lib, "unable to find TL UCP in memory handle");
@@ -767,6 +766,7 @@ ucc_status_t ucc_tl_ucp_mem_map(const ucc_base_context_t *context, ucc_mem_map_m
767766
if (UCC_OK != ucc_status) {
768767
tl_error(ctx->super.super.lib, "failed to import memory handle");
769768
ucc_free(m_data);
769+
tl_h->tl_data = NULL;
770770
return ucc_status;
771771
}
772772
}
@@ -785,6 +785,9 @@ ucc_status_t ucc_tl_ucp_mem_unmap(const ucc_base_context_t *context, ucc_mem_map
785785
}
786786

787787
data = (ucc_tl_ucp_memh_data_t *)memh->tl_data;
788+
if (!data) {
789+
return UCC_OK;
790+
}
788791
if (mode == UCC_MEM_MAP_MODE_EXPORT || mode == UCC_MEM_MAP_MODE_EXPORT_OFFLOAD) {
789792
status = ucp_mem_unmap(ctx->worker.ucp_context, data->rinfo.mem_h);
790793
if (status != UCS_OK) {
@@ -802,7 +805,16 @@ ucc_status_t ucc_tl_ucp_mem_unmap(const ucc_base_context_t *context, ucc_mem_map
802805
data->rinfo.packed_key = NULL;
803806
}
804807
} else if (mode == UCC_MEM_MAP_MODE_IMPORT || mode == UCC_MEM_MAP_MODE_IMPORT_OFFLOAD) {
805-
// need to free rkeys (data->rkey) , packed memh (data->packed_memh)
808+
ucc_status_t ret = UCC_OK;
809+
if (mode == UCC_MEM_MAP_MODE_IMPORT_OFFLOAD && data->rinfo.mem_h) {
810+
ucs_status_t ucs_st = ucp_mem_unmap(ctx->worker.ucp_context,
811+
data->rinfo.mem_h);
812+
if (ucs_st != UCS_OK) {
813+
tl_error(ctx->super.super.lib,
814+
"ucp_mem_unmap (import offload) failed: %d", ucs_st);
815+
ret = ucs_status_to_ucc_status(ucs_st);
816+
}
817+
}
806818
if (data->packed_memh) {
807819
ucp_memh_buffer_release(data->packed_memh, NULL);
808820
}
@@ -812,6 +824,11 @@ ucc_status_t ucc_tl_ucp_mem_unmap(const ucc_base_context_t *context, ucc_mem_map
812824
if (data->rkey) {
813825
ucp_rkey_destroy(data->rkey);
814826
}
827+
if (ret != UCC_OK) {
828+
ucc_free(data);
829+
memh->tl_data = NULL;
830+
return ret;
831+
}
815832
} else {
816833
ucc_error("Unknown mem map mode entered: %d", mode);
817834
return UCC_ERR_INVALID_PARAM;
@@ -872,7 +889,7 @@ ucc_status_t ucc_tl_ucp_memh_pack(const ucc_base_context_t *context,
872889
*
873890
* packed_key_size | packed_memh_size | packed_key | packed_memh
874891
*/
875-
packed_buffer = ucc_malloc(sizeof(size_t) * 2 + data->packed_memh_len +
892+
packed_buffer = ucc_malloc(UCC_TL_UCP_MEMH_TL_HEADER_SIZE + data->packed_memh_len +
876893
data->rinfo.packed_key_len,
877894
"packed buffer");
878895
if (!packed_buffer) {
@@ -899,9 +916,11 @@ ucc_status_t ucc_tl_ucp_memh_pack(const ucc_base_context_t *context,
899916

900917
failed_alloc_buffer:
901918
ucp_rkey_buffer_release(data->rinfo.packed_key);
919+
data->rinfo.packed_key = NULL;
902920
failed_rkey_pack:
903921
if (data->packed_memh) {
904922
ucp_memh_buffer_release(data->packed_memh, NULL);
923+
data->packed_memh = NULL;
905924
}
906925
return ucc_status;
907926
}

src/core/ucc_context.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
#include "components/tl/ucc_tl.h"
1212
#include "components/topo/ucc_sysinfo.h"
1313
#include "utils/ucc_malloc.h"
14+
#include "utils/ucc_math.h"
1415
#include "utils/ucc_log.h"
1516
#include "utils/ucc_list.h"
1617
#include "utils/ucc_string.h"
1718
#include "utils/ucc_debug.h"
1819
#include "ucc_progress_queue.h"
1920

20-
_Static_assert(UCC_MEM_MAP_TL_NAME_LEN == sizeof(size_t),
21-
"UCC_MEM_MAP_TL_NAME_LEN must equal sizeof(size_t) for pack layout");
22-
2321
static uint32_t ucc_context_seq_num = 0;
2422
static ucc_config_field_t ucc_context_config_table[] = {
2523
{"ESTIMATED_NUM_EPS", "0",
@@ -1246,6 +1244,7 @@ ucc_status_t ucc_mem_map_import(ucc_context_h context,
12461244

12471245
if (ctx->n_tl_ctx == 0) {
12481246
ucc_debug("No TL contexts available for import");
1247+
local_memh->tl_h = NULL;
12491248
local_memh->mode = mode;
12501249
local_memh->context = ctx;
12511250
*memh_size = 0;
@@ -1328,7 +1327,7 @@ ucc_status_t ucc_mem_map_export(ucc_context_h context,
13281327
int tls;
13291328
int tlh_index;
13301329

1331-
if (!params) {
1330+
if (mode != UCC_MEM_MAP_MODE_EXPORT_OFFLOAD && !params) {
13321331
ucc_error("params cannot be NULL");
13331332
return UCC_ERR_INVALID_PARAM;
13341333
}
@@ -1433,8 +1432,8 @@ ucc_status_t ucc_mem_map_export(ucc_context_h context,
14331432
if (mode == UCC_MEM_MAP_MODE_EXPORT) {
14341433
ucc_free(local_memh->tl_h);
14351434
ucc_free(local_memh);
1435+
*memh = NULL;
14361436
}
1437-
*memh = NULL;
14381437
*memh_size = 0;
14391438
return UCC_OK;
14401439
}
@@ -1478,14 +1477,20 @@ ucc_status_t ucc_mem_map_export(ucc_context_h context,
14781477
}
14791478
}
14801479
} else {
1481-
exported_memh->tl_h = local_memh->tl_h;
1480+
exported_memh->tl_h = (ucc_mem_map_tl_t *)ucc_calloc(
1481+
packed_tls, sizeof(ucc_mem_map_tl_t), "packed tl memh");
1482+
if (!exported_memh->tl_h) {
1483+
ucc_error("failed to allocate handle for exported buffers' tl handles");
1484+
status = UCC_ERR_NO_MEMORY;
1485+
ucc_free(exported_memh);
1486+
goto failed_pack;
1487+
}
14821488

1483-
for (i = 0; i < local_memh->num_tls; i++) {
1489+
for (i = 0, offset = 0, tls = 0; i < (int)local_memh->num_tls; i++) {
14841490
if (!local_memh->tl_h[i].packed_size) {
14851491
ucc_free(packed_buffers[i]);
14861492
continue;
14871493
}
1488-
/* these are already packed in order */
14891494
strncpy(PTR_OFFSET(exported_memh->pack_buffer, offset),
14901495
local_memh->tl_h[i].tl_name, UCC_MEM_MAP_TL_NAME_LEN);
14911496
offset += UCC_MEM_MAP_TL_NAME_LEN;
@@ -1496,6 +1501,8 @@ ucc_status_t ucc_mem_map_export(ucc_context_h context,
14961501
packed_buffers[i], local_memh->tl_h[i].packed_size);
14971502
ucc_free(packed_buffers[i]);
14981503
offset += local_memh->tl_h[i].packed_size;
1504+
memcpy(&exported_memh->tl_h[tls++], &local_memh->tl_h[i],
1505+
sizeof(ucc_mem_map_tl_t));
14991506
}
15001507
}
15011508

@@ -1506,9 +1513,7 @@ ucc_status_t ucc_mem_map_export(ucc_context_h context,
15061513
exported_memh->num_tls = packed_tls;
15071514
*memh = exported_memh;
15081515
*memh_size = sizeof(ucc_mem_map_memh_t) + offset;
1509-
if (mode == UCC_MEM_MAP_MODE_EXPORT) {
1510-
ucc_free(local_memh->tl_h);
1511-
}
1516+
ucc_free(local_memh->tl_h);
15121517
ucc_free(local_memh);
15131518
ucc_free(packed_buffers);
15141519
return UCC_OK;

0 commit comments

Comments
 (0)