1212
1313#include <ucs/sys/string.h>
1414#include <ucs/sys/module.h>
15+ #include <ucs/sys/sock.h>
1516#include <ucs/memory/memtype_cache.h>
17+ #include <ucs/type/init_once.h>
1618#include <sys/utsname.h>
1719#include <pthread.h>
1820
@@ -333,9 +335,8 @@ FILE* uct_rocm_base_load_kernel_config_file()
333335
334336int uct_rocm_base_file_contains_dmabuf_support (FILE * fp , const char kernel_opt1 [], const char kernel_opt2 [])
335337{
336- int dmabuf_supported = 0 ;
337- int found_opt1 = 0 ;
338- int found_opt2 = 0 ;
338+ int found_opt1 = 0 ;
339+ int found_opt2 = 0 ;
339340 char buf [256 ];
340341
341342 while (fgets (buf , sizeof (buf ), fp ) != NULL ) {
@@ -346,10 +347,10 @@ int uct_rocm_base_file_contains_dmabuf_support(FILE* fp, const char kernel_opt1[
346347 found_opt2 = 1 ;
347348 }
348349 if (found_opt1 && found_opt2 ) {
349- dmabuf_supported = 1 ;
350+ return 1 ;
350351 }
351352 }
352- return dmabuf_supported ;
353+ return 0 ;
353354}
354355
355356int uct_rocm_base_kernel_config_supports_dmabuf ()
@@ -388,16 +389,24 @@ int uct_rocm_base_kernel_symbols_supports_dmabuf()
388389
389390int uct_rocm_base_is_dmabuf_supported ()
390391{
391- #if HAVE_HSA_AMD_PORTABLE_EXPORT_DMABUF
392- if (uct_rocm_base_kernel_config_supports_dmabuf ()) {
393- return 1 ;
394- }
392+ static ucs_init_once_t init_once = UCS_INIT_ONCE_INITIALIZER ;
393+ static int dmabuf_supported = 0 ;
395394
396- ucs_trace ("no kernel conf file found or no support for dmabuf found, trying /proc/kallsyms fallback" );
397- return uct_rocm_base_kernel_symbols_supports_dmabuf ();
398- #else
399- return 0 ;
395+ UCS_INIT_ONCE (& init_once ) {
396+ #if HAVE_HSA_AMD_PORTABLE_EXPORT_DMABUF
397+ if (uct_rocm_base_kernel_config_supports_dmabuf ()) {
398+ dmabuf_supported = 1 ;
399+ } else {
400+ ucs_trace ("no kernel conf file found or no support for dmabuf "
401+ "found, trying /proc/kallsyms fallback" );
402+ dmabuf_supported = uct_rocm_base_kernel_symbols_supports_dmabuf ();
403+ }
400404#endif
405+ ucs_debug ("dmabuf is%s supported on ROCm" ,
406+ dmabuf_supported ? "" : " not" );
407+ }
408+
409+ return dmabuf_supported ;
401410}
402411
403412static void uct_rocm_base_dmabuf_export (const void * addr , const size_t length ,
@@ -414,8 +423,8 @@ static void uct_rocm_base_dmabuf_export(const void *addr, const size_t length,
414423 if (status != HSA_STATUS_SUCCESS ) {
415424 fd = UCT_DMABUF_FD_INVALID ;
416425 offset = 0 ;
417- ucs_warn ("failed to export dmabuf handle for addr %p / %zu" , addr ,
418- length );
426+ ucs_debug ("failed to export dmabuf handle for addr %p / %zu: 0x%x" ,
427+ addr , length , status );
419428 }
420429
421430 ucs_trace ("dmabuf export addr %p %lu to dmabuf fd %d offset %zu\n" ,
@@ -427,14 +436,13 @@ static void uct_rocm_base_dmabuf_export(const void *addr, const size_t length,
427436}
428437
429438ucs_status_t uct_rocm_base_mem_query (uct_md_h md , const void * addr ,
430- const size_t length ,
439+ size_t length , int have_dmabuf ,
431440 uct_md_mem_attr_v2_t * mem_attr_p )
432441{
433442 size_t dmabuf_offset = 0 ;
434- int is_exported = 0 ;
435443 ucs_memory_type_t mem_type = UCS_MEMORY_TYPE_HOST ;
436444 ucs_sys_device_t sys_dev = UCS_SYS_DEVICE_ID_UNKNOWN ;
437- int dmabuf_fd ;
445+ int dmabuf_fd = UCT_DMABUF_FD_INVALID ;
438446 hsa_status_t status ;
439447 hsa_device_type_t dev_type ;
440448 hsa_amd_pointer_type_t hsa_mem_type ;
@@ -447,7 +455,7 @@ ucs_status_t uct_rocm_base_mem_query(uct_md_h md, const void *addr,
447455 & base_size , & hsa_mem_type , & agent ,
448456 & dev_type );
449457 if (status != HSA_STATUS_SUCCESS ) {
450- return status ;
458+ return UCS_ERR_INVALID_ADDR ;
451459 }
452460
453461 if ((hsa_mem_type == HSA_EXT_POINTER_TYPE_HSA ) &&
@@ -458,6 +466,9 @@ ucs_status_t uct_rocm_base_mem_query(uct_md_h md, const void *addr,
458466 if (ucs_status != UCS_OK ) {
459467 sys_dev = UCS_SYS_DEVICE_ID_UNKNOWN ;
460468 }
469+ } else {
470+ base_addr = (void * )addr ;
471+ base_size = length ;
461472 }
462473
463474 if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_MEM_TYPE ) {
@@ -469,26 +480,30 @@ ucs_status_t uct_rocm_base_mem_query(uct_md_h md, const void *addr,
469480 }
470481
471482 if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_BASE_ADDRESS ) {
472- mem_attr_p -> base_address = ( void * ) addr ;
483+ mem_attr_p -> base_address = base_addr ;
473484 }
474485
475486 if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_ALLOC_LENGTH ) {
476- mem_attr_p -> alloc_length = length ;
487+ mem_attr_p -> alloc_length = base_size ;
477488 }
478489
479- if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_DMABUF_FD ) {
480- uct_rocm_base_dmabuf_export (addr , length , mem_type , & dmabuf_fd ,
481- & dmabuf_offset );
482- mem_attr_p -> dmabuf_fd = dmabuf_fd ;
483- is_exported = 1 ;
484- }
490+ if ((mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_DMABUF_FD ) ||
491+ (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_DMABUF_OFFSET )) {
492+ if (have_dmabuf ) {
493+ uct_rocm_base_dmabuf_export (base_addr , base_size , mem_type ,
494+ & dmabuf_fd , & dmabuf_offset );
495+ }
496+
497+ if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_DMABUF_FD ) {
498+ mem_attr_p -> dmabuf_fd = dmabuf_fd ;
499+ } else {
500+ ucs_close_fd (& dmabuf_fd );
501+ }
485502
486- if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_DMABUF_OFFSET ) {
487- if (!is_exported ) {
488- uct_rocm_base_dmabuf_export (addr , length , mem_type , & dmabuf_fd ,
489- & dmabuf_offset );
503+ if (mem_attr_p -> field_mask & UCT_MD_MEM_ATTR_V2_FIELD_DMABUF_OFFSET ) {
504+ mem_attr_p -> dmabuf_offset = dmabuf_offset +
505+ UCS_PTR_BYTE_DIFF (base_addr , addr );
490506 }
491- mem_attr_p -> dmabuf_offset = dmabuf_offset ;
492507 }
493508
494509 if (mem_type == UCS_MEMORY_TYPE_ROCM ) {
0 commit comments