Repository navigation
UCS/UCT: Resolve optional rdma-core functions at module load - #12271
bwestheimer wants to merge 6 commits into
Conversation
|
🤖 Starting review — findings will be posted here when done. |
| check_ib_symbol_versions() { | ||
| local lib versions failed=no | ||
|
|
||
| for lib in $(find ${ucx_build_dir}/src/uct/ib -path '*/.libs/libuct_*.so') |
There was a problem hiding this comment.
if find matches nothing (e.g. IB is not built in the image) the loop never runs and the check passes silently. pls fail when no library was scanned, otherwise the guard can rot without anyone noticing.
There was a problem hiding this comment.
Done, the check now fails when IB is configured but no module was scanned.
| /* Optional: libibverbs exports it only since IBVERBS_1.16 */ | ||
| static ucs_typeof(ibv_query_port_speed) *uct_ib_iface_query_port_speed_func; | ||
|
|
||
| static void uct_ib_iface_query_port_speed_init() |
There was a problem hiding this comment.
why not resolve it lazily inside uct_ib_iface_query_port_speed_gbps() with UCS_INIT_ONCE, like uct_ib_mlx5dv_check_direct_nic() does in this same PR? then we don't need the extra init function and the requirement to call it before uct_ib_iface_set_num_paths().
There was a problem hiding this comment.
uct_ib_iface_query_port_speed_gbps() is called from uct_ib_iface_estimate_bandwidth() on every perf estimate, and UCS_INIT_ONCE always takes its mutex, so I kept the one-time init in the iface constructor. check_direct_nic is only called at MD open and GDA device scan.
|
|
||
| UCS_INIT_ONCE(&init_once) { | ||
| uct_ib_iface_query_port_speed_func = UCS_SYS_LIB_OPTIONAL_FUNC( | ||
| ibv_open_device, ibv_query_port_speed, "IBVERBS_1.16"); |
There was a problem hiding this comment.
how do we catch it if the version node string is not exactly the one libibverbs uses? the lookup would just return NULL and port speed becomes 0 with only a debug message.
There was a problem hiding this comment.
Done, ucs_sys_get_lib_sym() now warns when the versioned lookup fails but the library defines the symbol in another version, so a wrong version string fails gtest on images with newer rdma-core.
| #if HAVE_DECL_MLX5DV_REG_DMABUF_MR | ||
| uct_ib_mlx5dv_reg_dmabuf_mr_func = UCS_SYS_LIB_OPTIONAL_FUNC( | ||
| mlx5dv_query_device, mlx5dv_reg_dmabuf_mr, "MLX5_1.25"); | ||
| if (uct_ib_mlx5dv_reg_dmabuf_mr_func == NULL) { |
There was a problem hiding this comment.
pls drop this cross-nulling: uct_ib_mlx5_direct_nic_reg_mr() already checks the pointer, and here it also makes uct_ib_mlx5dv_check_direct_nic() report "not supported" for the gdaki caller (gdaki.c:1516), which does not use mlx5dv_reg_dmabuf_mr.
There was a problem hiding this comment.
Done, dropped. The registration path already checks the pointer.
| #if HAVE_DECL_MLX5DV_GET_DATA_DIRECT_SYSFS_PATH | ||
| /* Optional: libmlx5 exports it only since MLX5_1.25 */ | ||
| static ucs_typeof(mlx5dv_get_data_direct_sysfs_path) *get_sysfs_path_func; | ||
| static ucs_init_once_t init_once = UCS_INIT_ONCE_INITIALIZER; |
There was a problem hiding this comment.
minor: pls align the '=' of these definitions with the ones below (docs/CodeStyle.md - indent multiple consecutive assignments on the column).
|
Residual coverage gap: No test or CI step validates that Residual coverage gap: Residual coverage gap: The static-build branch of |
|
🤖 Starting review — findings will be posted here when done. |
| mlx5dv_query_device, mlx5dv_get_data_direct_sysfs_path, | ||
| "MLX5_1.25"); | ||
| #if HAVE_DECL_MLX5DV_REG_DMABUF_MR | ||
| uct_ib_mlx5dv_reg_dmabuf_mr_func = UCS_SYS_LIB_OPTIONAL_FUNC( |
There was a problem hiding this comment.
mlx5dv_reg_dmabuf_mr is resolved inside the direct-NIC detection init_once, so it is only set when enabled is true and the device has an active port, and only when HAVE_DECL_MLX5DV_GET_DATA_DIRECT_SYSFS_PATH is set. can we give it its own init_once next to uct_ib_mlx5_direct_nic_reg_mr instead? it keeps the resolution at the point of use and drops the nested #if.
Impact: today this is safe only because md->direct_nic_sys_dev stays UNKNOWN on every path that skips the UCS_INIT_ONCE. Any future caller of uct_ib_mlx5_direct_nic_reg_mr, or a build where only mlx5dv_reg_dmabuf_mr is declared, silently gets a NULL pointer. Root AGENTS.md asks to trace initialization through the owning lifecycle rather than attach it to an unrelated flow.
There was a problem hiding this comment.
uct_ib_mlx5_direct_nic_reg_mr() runs on every registration, and UCS_INIT_ONCE always takes its mutex, so I kept the lookup with the detection. Registration only uses the pointer after that same init set md->direct_nic_sys_dev, and without the sysfs path declaration the detection is compiled out.
| echo "==== Build with --enable-debug option ====" | ||
| ${WORKSPACE}/contrib/configure-devel --prefix=$ucx_inst --enable-debug --enable-examples | ||
| $MAKEP | ||
| check_ib_symbol_versions |
There was a problem hiding this comment.
does the build_debug CI image have rdma-core new enough for ibv_query_port_speed / mlx5dv_reg_dmabuf_mr / mlx5dv_get_data_direct_sysfs_path to be declared? if the build host is older, HAVE_DECL_* is 0, nothing references the new nodes and the check passes trivially.
Impact: the new guard provides value only on a build host whose rdma-core actually declares these symbols; otherwise it is a no-op and the regression it is meant to catch can land unnoticed.
There was a problem hiding this comment.
Yes. All three are declared on sles15sp7, sles16sp0, tencentos44, ctyunos2507, openeuler2203sp4/2403sp3, uos201060a and ubuntu2604, on x86_64 and aarch64. ubuntu2404, rhel100 and debian125/130 declare the two mlx5dv functions. Only ubuntu2204, rhel82 and rhel90 declare none.
| azure_log_error "readelf -V $lib failed" | ||
| failed=yes | ||
| elif echo "$versions" | \ | ||
| grep -E 'Name: (MLX5_1\.(2[5-9]|[3-9][0-9]|[1-9][0-9]{2,})|IBVERBS_1\.(1[5-9]|[2-9][0-9]|[1-9][0-9]{2,}))' |
There was a problem hiding this comment.
minor: the regex only covers MLX5_1.* and IBVERBS_1.*, so a future major bump (e.g. MLX5_2.0) would pass silently. maybe match every IBVERBS_/MLX5_ version need and allow-list the nodes present in rdma-core 50 instead.
Impact: maintainability; also the hardcoded 50 in the error message is only implied by the regex, so the two can drift.
There was a problem hiding this comment.
Done, the check now compares every MLX5_ and IBVERBS_ version node against MLX5_1.24 and IBVERBS_1.14 with sort -V, and the error message names the limit.
|
🤖 Starting review — findings will be posted here when done. |
| /* Optional: libibverbs exports it only since IBVERBS_1.16 */ | ||
| static ucs_typeof(ibv_query_port_speed) *uct_ib_iface_query_port_speed_func; | ||
|
|
||
| static void uct_ib_iface_query_port_speed_init() |
There was a problem hiding this comment.
Two different lazy-init patterns in one PR: ib_mlx5dv_md.c resolves its pointers with a function-local UCS_INIT_ONCE at the point of use, while ib_iface.c adds an #if/#else pair of init functions plus a call from UCS_CLASS_INIT_FUNC. The second form is ~15 lines longer and creates an ordering dependency between the constructor and uct_ib_iface_set_num_paths(). Why not put the UCS_INIT_ONCE directly in uct_ib_iface_query_port_speed_gbps(), like uct_ib_mlx5dv_check_direct_nic() does? It drops both init functions and the extra call from the constructor.
There was a problem hiding this comment.
Both lazy-init paths are gone. The functions are resolved once in the module constructors, before any interface or memory domain can use them, so there is a single pattern and no init-once state.
|
Residual coverage gap:
Residual coverage gap: the check validates |
|
🤖 CI Triage Agent — TL;DR: The gtest run passed 9029/9030 tests and failed only Full analysisSummary: Azure job "roce on worker 3" (build 143538) failed because Root cause: Evidence from the log: exactly one test failed, and it is the IB MD allocation test. For the Implicated commit: unknown — not attributable to the PR commits ( File: test/gtest/uct/test_md.cc:479-561 (test body); src/uct/ib/mlx5/dv/ib_mlx5dv_md.c:2155 ( Suggested fix:
Related: PR #12271 ( |
|
@bwestheimer As this is a risky change, we need to have a proper justification. |
|
@rakhmets can we unite code with what you did for nvml? |
|
@bwestheimer please look at Raul's PR #10680 and apply same method (extract common code) |
|
@shasson5 @yosefe Thanks. I looked at #10680 and will follow the same structure here. Planned change: Lookup: Scope: @tvegas1 I will take your four comments as part of the same update. |
IMO better extract the common code to a shared helper in this PR |
|
Thanks. I'll extract the common part into |
A library built against newer headers binds every function it calls to the symbol version that introduced it, so it fails to load where the dependency is older, even when the function is only used optionally. ucs_sys_get_lib_sym() finds the library that contains a given address with dladdr(), reopens it with RTLD_NOLOAD and returns the symbol only if that library defines it. UCS_SYS_LIB_OPTIONAL_FUNC() uses it in shared libraries and references the function directly in static archives, which have no dynamic symbols to look up. The check that the symbol is defined by the library itself, and not by one of its dependencies, is the one the module loader already used. It moves from module.c to lib.c as ucs_sys_dlsym_shallow(), which takes the log level so that the module loader keeps logging at the module log level. The function pointer table macros UCS_SYS_LIB_FUNC_PTR_DECLARE/_DEFINE, UCS_SYS_LIB_FUNC_LOAD and UCS_SYS_LIB_FUNC_LOAD_OPTIONAL, and ucs_sys_dlsym(), which logs a missing symbol, are shared by the users of optional library functions: NVML, gdr_copy and the IB modules. dlsym() returns the default version of the symbol, as the NVML lookup in cuda_nvml.c does. This is the version a direct call would bind unless the library adds a second version of the function with a different prototype.
The NVML function pointers are declared and loaded with the UCS table macros, typed from the nvml.h declarations, and looked up with ucs_sys_dlsym(), which replaces uct_cuda_nvml_dlsym(). NVML still opens its library by name and initializes on first use, so its behavior does not change.
gdr_get_attribute() is looked up with ucs_sys_get_lib_sym() in the GDRCopy library which provides gdr_open(), instead of the same dladdr(), RTLD_NOLOAD and ownership check written out in place. The lookup still runs in both shared and static builds, so the behavior does not change.
ibv_query_port_speed() is in libibverbs IBVERBS_1.16 (rdma-core 62), so libuct_ib built against rdma-core 62 or newer fails to load on hosts with older rdma-core and every IB transport is lost. Optional libibverbs functions are listed in UCT_IB_VERBS_OPTIONAL_FUNCS and looked up once in the module constructor, in the library which provides ibv_open_device. Port speed is unknown when the function is missing, as in a build without its declaration.
mlx5dv_get_data_direct_sysfs_path() and mlx5dv_reg_dmabuf_mr() are in libmlx5 MLX5_1.25 (rdma-core 54), so libuct_ib_mlx5 built against newer rdma-core fails to load on hosts with rdma-core 50, such as Ubuntu 24.04 inbox, and rc_mlx5, dc_mlx5, ud_mlx5 and the DevX memory domain are lost. Both functions are looked up once in the module constructor. Direct NIC is reported only when both are declared and found: once the IB device has a Direct NIC sibling, cuda_copy exports dmabufs mapped through the PCIe BAR, which work only when registered with mlx5dv_reg_dmabuf_mr(). When one of them is missing, the state of both is logged per device.
Fail the debug build if an IB module requires a libmlx5 or libibverbs version node newer than rdma-core 50 (MLX5_1.24, IBVERBS_1.14), so that a new direct call to a newer rdma-core function is caught. Such functions belong in the optional function tables.
faaebcd to
d20faa4
Compare
|
🤖 Starting review — findings will be posted here when done. |
| #ifdef UCX_SHARED_LIB | ||
| # define UCS_SYS_LIB_OPTIONAL_FUNC(_lib_addr, _func) \ | ||
| ((ucs_typeof(_func)*)ucs_sys_get_lib_sym((const void*)(_lib_addr), \ | ||
| #_func)) |
There was a problem hiding this comment.
pls use UCS_PP_MAKE_STRING(_func) here, like UCS_SYS_LIB_FUNC_LOAD() does below.
| #_func)) | |
| UCS_PP_MAKE_STRING(_func))) |
#_func keeps the unexpanded token, while ucs_typeof(_func) on the same line expands it, so for an aliased function (e.g. #define nvmlDeviceGetPciInfo nvmlDeviceGetPciInfo_v3) we would take the type of one symbol and look up another.
| sym = ucs_sys_dlsym_shallow(lib_info.dli_fname, dl, sym_name, | ||
| UCS_LOG_LEVEL_DEBUG); | ||
| if (sym == NULL) { | ||
| (void)dlerror(); |
There was a problem hiding this comment.
minor: the dlerror() return value is not used here, pls remove.
| ret = mlx5dv_get_data_direct_sysfs_path(ctx, sys_path, sizeof(sys_path)); | ||
| if ((UCT_IB_OPTIONAL_FUNC(mlx5dv_get_data_direct_sysfs_path) == NULL) || | ||
| (UCT_IB_OPTIONAL_FUNC(mlx5dv_reg_dmabuf_mr) == NULL)) { | ||
| ucs_debug("%s: mlx5dv_get_data_direct_sysfs_path() %s, " |
There was a problem hiding this comment.
minor: can we drop this message? ucs_sys_get_lib_sym() already logs at debug which symbol was missing when the module constructor loaded it, and out_not_supported logs the resulting decision, so this adds 7 lines without new information.
|
|
||
| get_attribute = (uct_gdr_get_attribute_func_t)dlsym(dl_handle, | ||
| "gdr_get_attribute"); | ||
| get_attribute = (ucs_typeof(gdr_get_attribute)*)ucs_sys_get_lib_sym( |
There was a problem hiding this comment.
can we use UCS_SYS_LIB_OPTIONAL_FUNC(gdr_open, gdr_get_attribute) here? It drops the cast and the symbol name string, and keeps one way of resolving optional functions.
| fi | ||
|
|
||
| for node in $(echo "$versions" | \ | ||
| grep -oE 'Name: (MLX5|IBVERBS)_[0-9][0-9.]*' | \ |
There was a problem hiding this comment.
the pattern covers only MLX5_/IBVERBS_, but the find above also picks up libuct_rdmacm.so and libuct_ib_efa.so, which need RDMACM_/EFA_ nodes from the same rdma-core - can we add them too, so the check matches the rule added to src/uct/ib/AGENTS.md?
|
Residual coverage gap: Residual coverage gap: the check inspects Residual coverage gap: Residual coverage gap: the |
|
I've force-pushed a reworked version based on the review. The branch is rebased on master and split into six commits. Changes: Common code, as discussed above: Two corrections to my earlier description: On the bot's point about falling back to Testing: Note: |
|
🤖 CI Triage Agent — TL;DR: The BlueField ASan gtest job failed on LeakSanitizer ("68024 byte(s) leaked in 17 allocation(s)") in Full analysisSummary: Root cause: The leak set is composed entirely of mlx5 DEVX resources and the buffers they reference:
So the allocation side of the DEVX API works, but the release side ( Implicated commit: 8360997 "UCT/IB/MLX5: Resolve the Direct NIC functions when the module loads" and b51c82f "UCS/SYS: Add lookup of an optional symbol in a loaded library" (Bryce Westheimer, PR #12271) File: src/uct/ib/mlx5/ib_mlx5.h:1224 ( Suggested fix: Audit the new optional-symbol table for the release entry points — Related: PR #12271 (#12271); test last modified by #12362, #11901, #12334, #12206. |
| void UCS_F_CTOR uct_ib_init() | ||
| { | ||
| UCS_MODULE_FRAMEWORK_DECLARE(uct_ib); | ||
| ssize_t i; | ||
|
|
||
| UCT_IB_VERBS_OPTIONAL_FUNCS(UCT_IB_OPTIONAL_FUNC_LOAD, ibv_open_device) |
There was a problem hiding this comment.
can we find the lib by soname instead, e.g. dlopen("libibverbs.so.1", RTLD_LAZY | RTLD_NOLOAD), like nvml does? then we don't need an anchor function, and a preloaded wrapper of ibv_open_device does not turn it off.
|
|
||
|
|
||
| /* Pointer to function @a _func, in a table of pointers named by @a _prefix */ | ||
| #define UCS_SYS_LIB_FUNC_PTR(_prefix, _func) UCS_PP_TOKENPASTE(_prefix, _func) |
There was a problem hiding this comment.
can we drop most of these layers? for 3 functions IMO it is simpler to keep only UCS_SYS_LIB_OPTIONAL_FUNC() here, and assign plain pointers in the ib/mlx5 ctors, e.g.
uct_ib_mlx5dv_reg_dmabuf_mr = UCS_SYS_LIB_OPTIONAL_FUNC(mlx5dv_query_device,
mlx5dv_reg_dmabuf_mr);then UCS_SYS_LIB_FUNC_PTR*, UCS_SYS_LIB_FUNC_LOAD*, UCT_IB_OPTIONAL_FUNC* and the two func tables are not needed, and nvml only needs ucs_sys_dlsym().
What?
Resolve the rdma-core functions that UCX uses only optionally when the IB modules load, instead of linking them directly, so UCX built against new rdma-core still loads with older libmlx5 and libibverbs:
mlx5dv_get_data_direct_sysfs_path()andmlx5dv_reg_dmabuf_mr()(libmlx5MLX5_1.25, rdma-core 54+), for Direct NIC.ibv_query_port_speed()(libibverbsIBVERBS_1.16, rdma-core 62+), for the port speed used in bandwidth estimation.When a function is missing, behavior is the same as a build without its declaration: Direct NIC is not supported and port speed is unknown. A CI check keeps future direct calls from bringing the problem back.
Why?
configure only checks the declarations, so a build against newer rdma-core requires the new symbol versions at load time. On a host with older rdma-core, for example Ubuntu 24.04 inbox rdma-core 50 (
MLX5_1.24,IBVERBS_1.14), the modules fail to load:The first drops rc_mlx5, dc_mlx5, ud_mlx5 and the DevX memory domain. The second drops every IB transport and rdmacm. Binary packages that are built on one host and run on many hit the first one today, for example on DGX OS 7 nodes.
How?
ucs_sys_get_lib_sym()finds the library that contains a function the caller already links withdladdr(), reopens it withRTLD_NOLOAD, and returns the optional symbol only if that library defines it. The ownership check is the module loader'sucs_module_dlsym_shallow(), moved tolib.casucs_sys_dlsym_shallow()and called by both.UCS_SYS_LIB_OPTIONAL_FUNC()uses the lookup in shared libraries and references the function directly in static archives, which have no dynamic symbols to look up.sys/lib.his not installed, so there is no public API change.UCS_SYS_LIB_FUNC_PTR_DECLARE/_DEFINE,UCS_SYS_LIB_FUNC_LOADfrom an opened library,UCS_SYS_LIB_FUNC_LOAD_OPTIONALfrom a linked one) anducs_sys_dlsym(), which logs a missing symbol, are shared by all users of optional library functions.nvml.h, anducs_sys_dlsym()replacesuct_cuda_nvml_dlsym(). NVML still opens its library by name and initializes on first use.gdr_get_attribute()is looked up withucs_sys_get_lib_sym()instead of its own copy of the samedladdr(),RTLD_NOLOADand ownership check.UCT_IB_VERBS_OPTIONAL_FUNCSand resolved inuct_ib_init(). Port speed is unknown whenibv_query_port_speed()is missing.UCT_IB_MLX5_OPTIONAL_FUNCSand resolved inuct_mlx5_init(). Direct NIC is reported only when both functions are declared and found, since cuda_copy exports a PCIe-mapped dmabuf once the device has a Direct NIC sibling, and that handle only works withmlx5dv_reg_dmabuf_mr(). When one is missing, the state of both is logged per device at debug level.libuct_iblinks libibverbs andlibuct_ib_mlx5links libmlx5.build_debugfails if an IB module requires a libmlx5 or libibverbs version node newer than rdma-core 50 (MLX5_1.24,IBVERBS_1.14). rdma-core 50 is the Ubuntu 24.04 inbox version and the oldest runtime this change targets. Binaries built against newer headers do not load on rdma-core older than 50 if they call a function fromMLX5_1.12..MLX5_1.24directly. The modules need at mostMLX5_1.11andIBVERBS_1.13today, so the limit could be lowered if you prefer a stricter check. The rule is noted insrc/uct/ib/AGENTS.md.Alternatives considered:
dlvsym()with the version node of each function: it binds exactly what a direct call would, but needs a version string per function and a configure check.dlsym()returns the default version, which is the same unless rdma-core adds a second version of the function with a different prototype.dlsym(RTLD_DEFAULT, ...): modules are loadedRTLD_LOCAL, so it only works when the application itself links libmlx5 or libibverbs.Known limits, in shared builds:
LD_PRELOADlibrary wraps it,dladdr()names that object and the optional feature is turned off, with a debug message.LD_PRELOADwrappers of those three functions do not see the calls.Testing
libuct_ibandlibuct_ib_mlx5fail to load and master fails the new CI check. With this change all IB modules load, the three missing functions are logged at debug level, and the check passes.ibv_query_port_speed()is called.test_sys.*(14/14),test_uct_ib_optional_func, and*test_md*:*test_uct_iface*pass on both runtimes, exceptib/test_md.sys_device, which fails the same way on master because soft-RoCE has no PCI device.HAVE_DECL_*is 0, the build is clean, and the new test is skipped.-Werror.MLX5_1.27,IBVERBS_1.17), release builds of master and this change:MLX5_1.25andIBVERBS_1.16, this change at mostMLX5_1.11andIBVERBS_1.13. Both load the same transports (rc/dc/ud_mlx5, gga_mlx5, rc/ud_verbs) and memory domains.mlx5dv_get_data_direct_sysfs_path()through the resolved pointer and reports "not supported" on CX-7, the same as master.ucx_perfteston one port, 3 runs each:tag_lat8 B (rc_mlx5 about 1.31 us, rc_verbs about 1.40 us) andtag_bw1 MiB (about 11.9 GB/s) are the same as master within run-to-run noise.cuda_nvml.cand the changed gdr_copy function were compile-checked with-Wall -Werroragainst minimal stand-in headers only.