Skip to content

UCS/UCT: Resolve optional rdma-core functions at module load - #12271

Open
bwestheimer wants to merge 6 commits into
openucx:masterfrom
bwestheimer:bwestheimer/mlx5dv-optional-syms
Open

bwestheimer wants to merge 6 commits into
openucx:masterfrom
bwestheimer:bwestheimer/mlx5dv-optional-syms

Conversation

@bwestheimer

@bwestheimer bwestheimer commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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() and mlx5dv_reg_dmabuf_mr() (libmlx5 MLX5_1.25, rdma-core 54+), for Direct NIC.
  • ibv_query_port_speed() (libibverbs IBVERBS_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:

dlopen('.../libuct_ib_mlx5.so.0') failed: libmlx5.so.1: version `MLX5_1.25' not found
dlopen('.../libuct_ib.so.0') failed: libibverbs.so.1: version `IBVERBS_1.16' not found

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: ucs_sys_get_lib_sym() finds the library that contains a function the caller already links with dladdr(), reopens it with RTLD_NOLOAD, and returns the optional symbol only if that library defines it. The ownership check is the module loader's ucs_module_dlsym_shallow(), moved to lib.c as ucs_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.h is not installed, so there is no public API change.
  • UCS/SYS: the function-pointer table macros (UCS_SYS_LIB_FUNC_PTR_DECLARE/_DEFINE, UCS_SYS_LIB_FUNC_LOAD from an opened library, UCS_SYS_LIB_FUNC_LOAD_OPTIONAL from a linked one) and ucs_sys_dlsym(), which logs a missing symbol, are shared by all users of optional library functions.
  • UCT/CUDA: the NVML function pointers use the table macros, typed from nvml.h, and ucs_sys_dlsym() replaces uct_cuda_nvml_dlsym(). NVML still opens its library by name and initializes on first use.
  • UCT/CUDA/GDR_COPY: gdr_get_attribute() is looked up with ucs_sys_get_lib_sym() instead of its own copy of the same dladdr(), RTLD_NOLOAD and ownership check.
  • UCT/IB: optional libibverbs functions are listed in UCT_IB_VERBS_OPTIONAL_FUNCS and resolved in uct_ib_init(). Port speed is unknown when ibv_query_port_speed() is missing.
  • UCT/IB/MLX5: optional libmlx5 functions are listed in UCT_IB_MLX5_OPTIONAL_FUNCS and resolved in uct_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 with mlx5dv_reg_dmabuf_mr(). When one is missing, the state of both is logged per device at debug level.
  • Each pointer is written once by its module constructor, before any interface or memory domain exists, and is only read afterwards. The pointers stay valid because libuct_ib links libibverbs and libuct_ib_mlx5 links libmlx5.
  • BUILD/CI: build_debug fails 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 from MLX5_1.12..MLX5_1.24 directly. The modules need at most MLX5_1.11 and IBVERBS_1.13 today, so the limit could be lowered if you prefer a stricter check. The rule is noted in src/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 loaded RTLD_LOCAL, so it only works when the application itself links libmlx5 or libibverbs.
  • Weak symbols: the version requirement is dropped only if every reference is weak, and the result depends on the linker.
  • Building packages against rdma-core 50: a packaging workaround that turns Direct NIC and port speed off for that package.

Known limits, in shared builds:

  • If a non-PIE executable takes the address of the anchor function, or an LD_PRELOAD library wraps it, dladdr() names that object and the optional feature is turned off, with a debug message.
  • The optional functions are called through the resolved pointer, so LD_PRELOAD wrappers of those three functions do not see the calls.

Testing

  • Ubuntu 24.04 aarch64, rdma-core 66 headers, soft-RoCE, devel and release builds:
    • Inbox rdma-core 50 runtime: master's libuct_ib and libuct_ib_mlx5 fail 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.
    • rdma-core 66 runtime: all three functions resolve, and ibv_query_port_speed() is called.
    • gtest test_sys.* (14/14), test_uct_ib_optional_func, and *test_md*:*test_uct_iface* pass on both runtimes, except ib/test_md.sys_device, which fails the same way on master because soft-RoCE has no PCI device.
    • With rdma-core 50 headers, every HAVE_DECL_* is 0, the build is clean, and the new test is skipped.
    • Static archives reference the three functions directly.
    • Each commit builds with -Werror.
  • GH200, CX-7 InfiniBand, Ubuntu 24.04 with DOCA rdma-core 2607 (MLX5_1.27, IBVERBS_1.17), release builds of master and this change:
    • Master needs MLX5_1.25 and IBVERBS_1.16, this change at most MLX5_1.11 and IBVERBS_1.13. Both load the same transports (rc/dc/ud_mlx5, gga_mlx5, rc/ud_verbs) and memory domains.
    • All three functions resolve. The Direct NIC check calls mlx5dv_get_data_direct_sysfs_path() through the resolved pointer and reports "not supported" on CX-7, the same as master.
    • Loopback ucx_perftest on one port, 3 runs each: tag_lat 8 B (rc_mlx5 about 1.31 us, rc_verbs about 1.40 us) and tag_bw 1 MiB (about 11.9 GB/s) are the same as master within run-to-run noise.
  • cuda_nvml.c and the changed gdr_copy function were compile-checked with -Wall -Werror against minimal stand-in headers only.
  • Not run: Data Direct hardware, a 2-node run, CUDA/GDAKI builds and the NVML and gdr_copy tests. A run on mlx5 hardware with inbox rdma-core 50 is pending, and I'll add the results here.

@bwestheimer
bwestheimer requested a review from a team as a code owner November 6, 2026 20:44
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread buildlib/tools/builds.sh
check_ib_symbol_versions() {
local lib versions failed=no

for lib in $(find ${ucx_build_dir}/src/uct/ib -path '*/.libs/libuct_*.so')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the check now fails when IB is configured but no module was scanned.

Comment thread src/uct/ib/base/ib_iface.c Outdated
/* 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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/uct/ib/base/ib_iface.c Outdated

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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/uct/ib/mlx5/dv/ib_mlx5dv_md.c Outdated
#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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, dropped. The registration path already checks the pointer.

Comment thread src/uct/ib/mlx5/dv/ib_mlx5dv_md.c Outdated
#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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: pls align the '=' of these definitions with the ones below (docs/CodeStyle.md - indent multiple consecutive assignments on the column).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@svc-nvidia-pr-review

Copy link
Copy Markdown

Residual coverage gap: No test or CI step validates that "IBVERBS_1.16" / "MLX5_1.25" are the actual version nodes on a host with new rdma-core; a wrong string disables port-speed query and direct-NIC silently. The new check_ib_symbol_versions only proves the symbols are not linked, not that dlvsym() finds them.

Residual coverage gap: contrib/ibmock does not define ibv_query_port_speed and has no version script, so mock-based IB CI always exercises the NULL/fallback path, never the resolved path.

Residual coverage gap: The static-build branch of UCS_SYS_LIB_OPTIONAL_FUNC() (&_func) is untested and intentionally keeps the hard symbol-version dependency; no CI job covers it (there is no --disable-shared build).

@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/uct/ib/mlx5/dv/ib_mlx5dv_md.c Outdated
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(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread buildlib/tools/builds.sh
echo "==== Build with --enable-debug option ===="
${WORKSPACE}/contrib/configure-devel --prefix=$ucx_inst --enable-debug --enable-examples
$MAKEP
check_ib_symbol_versions

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread buildlib/tools/builds.sh Outdated
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,}))'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/uct/ib/base/ib_iface.c Outdated
/* 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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@svc-nvidia-pr-review

Copy link
Copy Markdown

src/ucs/sys/lib.c

ucs_sys_get_lib_sym() detects exactly the case where the library does define the symbol but not in the requested version node, and only warns — returning NULL. A vendor or backported libibverbs/libmlx5 (e.g. MOFED libmlx5 backporting mlx5dv_get_data_direct_sysfs_path under MLX5_1.24) can export a new function under an older version node; here we drop the feature (direct NIC / port-speed support) and warn although the symbol is there and usable. Can we use the dlsym() result in that case? A newly added function has only one definition, so the default version is the one we want.

buildlib/tools/builds.sh

Residual coverage gap: check_ib_symbol_versions runs only from build_debug. On a CI image with old rdma-core headers, HAVE_DECL_IBV_QUERY_PORT_SPEED / HAVE_DECL_MLX5DV_* are 0, the new code is compiled out, and the check passes trivially — so a passing CI run does not prove the new path was exercised.

buildlib/tools/builds.sh

Residual coverage gap: the check validates .gnu.version_r entries only. If UCX is linked against a libibverbs/libmlx5 built without a version script, a new symbol produces an unversioned undefined reference, which this check would not flag.

@svc-ucx

svc-ucx commented Nov 8, 2026

Copy link
Copy Markdown

🤖 CI Triage Agent — UCX PR (Tests roce on worker 3) · commit faaebcd6

TL;DR: The gtest run passed 9029/9030 tests and failed only ib/test_md.alloc/1 (device mlx5_1), which exercises IB on-device-memory (UCS_MEMORY_TYPE_RDMA/ibv_alloc_dm) allocation — a path untouched by PR #12271's optional-mlx5dv-symbol change; it looks like a device-memory resource/flakiness failure rather than a regression, and the fix is to re-run and harden the test's DM exhaustion handling (the retrieved log window doesn't include the assertion text, so grab the full gtest log to confirm).

Full analysis

Summary: Azure job "roce on worker 3" (build 143538) failed because make test in build-test/test/gtest exited non-zero: [ FAILED ] ib/test_md.alloc/1, where GetParam() = mlx5_1 (1 failed, 9029 passed).

Root cause: Evidence from the log: exactly one test failed, and it is the IB MD allocation test. For the ib component the only allocatable memory type is device memory — uct_ib_mlx5_devx_md_query() adds UCS_BIT(UCS_MEMORY_TYPE_RDMA) with max_alloc = dev_attr.max_dm_size (src/uct/ib/mlx5/dv/ib_mlx5dv_md.c:3210-3214), and uct_ib_mlx5_devx_device_mem_alloc() backs it with ibv_alloc_dm() + KSM/atomic-key registration (same file, line 2155+). The test does 300 random DM allocations and hard-fails (ASSERT_UCS_OK / EXPECT_LT(failures/iterations, 0.5) "Too many OUT_OF_RESOURCE failures") if an allocation returns anything other than UCS_ERR_NO_MEMORY or if more than half of them return NO_MEMORY (test/gtest/uct/test_md.cc:479-561). On-device memory is a scarce, device-global resource shared by all gtest workers running concurrently on that machine, which makes this test sensitive to contention on mlx5_1 (note that mlx5_1 is also the device whose verbs atomics/OOO-SL capabilities are reported as unavailable in the same log). Nothing in PR #12271 touches this path: the PR only resolves the optional symbols mlx5dv_get_data_direct_sysfs_path/mlx5dv_reg_dmabuf_mr at runtime (UCS_SYS_LIB_OPTIONAL_FUNC → ucs_sys_get_lib_sym(), src/ucs/sys/lib.c:63), used solely for Direct-NIC dmabuf MR registration (uct_ib_mlx5_direct_nic_reg_mr, ib_mlx5dv_md.c:809-855) — and all dmabuf/export tests (test_md_dmabuf, shm_ib*/test_ucp_mmap_export) passed in this very run. Caveat: the Azure task log is only retrievable as its last ~500 lines, so the gtest assertion line itself (file:line) is outside the retrieved window; this diagnosis is based on which test failed plus the source of that test and the DM alloc path.

Implicated commit: unknown — not attributable to the PR commits (865a1711, 3fe0df71, 90536f58, 56f99a55, Bryce Westheimer), which only affect Direct-NIC/dmabuf optional symbol lookup.

File: test/gtest/uct/test_md.cc:479-561 (test body); src/uct/ib/mlx5/dv/ib_mlx5dv_md.c:2155 (uct_ib_mlx5_devx_device_mem_alloc, ibv_alloc_dm path)

Suggested fix:

  1. Re-trigger the job; if ib/test_md.alloc/1 passes, treat it as unrelated flakiness and don't block the PR.
  2. To confirm the exact assertion, re-run just that case with the full log preserved, e.g. GTEST_FILTER='ib/test_md.alloc*' ./gtest --gtest_filter=... with UCX_LOG_LEVEL=debug, and look for either "Too many OUT_OF_RESOURCE failures" (DM exhaustion ⇒ environment/contention) or a non-NO_MEMORY status from uct_mem_alloc/uct_md_mkey_pack_v2 (real bug).
  3. Hardening (independent of this PR): in test_md.alloc, treat device memory (UCS_MEMORY_TYPE_RDMA) specially — skip the memory type when ibv_alloc_dm keeps returning UCS_ERR_NO_MEMORY (DM is a device-global resource shared by concurrently running gtest workers) instead of asserting on a >50% failure ratio, or bound max_size by the currently free DM rather than max_dm_size.

Related: PR #12271 (UCT/IB: Resolve optional rdma-core symbols at runtime) — the PR under test; no existing issue found tracking ib/test_md.alloc failures.

@shasson5

shasson5 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

@bwestheimer As this is a risky change, we need to have a proper justification.
As the slack issue is currently addressed in another way, this PR doesn't have practical usages AFAIU

@yosefe

yosefe commented Nov 8, 2026

Copy link
Copy Markdown
Member

@rakhmets can we unite code with what you did for nvml?

Comment thread src/uct/ib/mlx5/dv/ib_mlx5dv_md.c Resolved
Comment thread src/uct/ib/mlx5/dv/ib_mlx5dv_md.c Resolved
Comment thread src/uct/ib/mlx5/dv/ib_mlx5dv_md.c Resolved
Comment thread src/uct/ib/base/ib_iface.c Resolved
Comment thread src/uct/ib/base/ib_iface.c Resolved
@shasson5

Copy link
Copy Markdown
Contributor

@rakhmets can we unite code with what you did for nvml?

@rakhmets

@shasson5

Copy link
Copy Markdown
Contributor

@bwestheimer please look at Raul's PR #10680 and apply same method (extract common code)

@bwestheimer

Copy link
Copy Markdown
Contributor Author

@shasson5 @yosefe Thanks. I looked at #10680 and will follow the same structure here.

Planned change:
The three optional functions go into one table with their library, and they are resolved once when the IB module loads. Callers use a wrapper for each function. This replaces the two separate lazy inits and removes the ordering dependency in the iface constructor.

Lookup:
I will switch to plain dlsym(), as #10680 does, and drop dlvsym() and its configure check. The only case this gives up is rdma-core adding a second version of one of these functions with a different prototype, which I will note in the commit message.

Scope:
I would keep this PR to the IB code and leave moving the NVML lookup onto a shared helper for a follow-up, so that this fix stays small. Is that acceptable, or do you prefer the common code extracted in this PR?

@tvegas1 I will take your four comments as part of the same update.

@shasson5

Copy link
Copy Markdown
Contributor

I would keep this PR to the IB code and leave moving the NVML lookup onto a shared helper for a follow-up, so that this fix stays small. Is that acceptable, or do you prefer the common code extracted in this PR?

IMO better extract the common code to a shared helper in this PR

@bwestheimer

Copy link
Copy Markdown
Contributor Author

Thanks. I'll extract the common part into ucs/sys/lib.h in this PR. Plan:
The function-pointer table macros (declare, define, load) and the symbol lookup move to UCS. NVML, the IB modules and gdr_copy's gdr_get_attribute lookup will use them. NVML keeps loading its library by name and initializing on first use, and the IB modules keep resolving when they load, since they link their libraries.
Does that match what you had in mind, or did you also want NVML's wrapper and init handling shared?

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.
@bwestheimer
bwestheimer force-pushed the bwestheimer/mlx5dv-optional-syms branch from faaebcd to d20faa4 Compare November 20, 2026 17:46
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

@bwestheimer bwestheimer changed the title UCT/IB: Resolve optional rdma-core symbols at runtime UCS/UCT: Resolve optional rdma-core functions at module load Nov 20, 2026
Comment thread src/ucs/sys/lib.h
#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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use UCS_PP_MAKE_STRING(_func) here, like UCS_SYS_LIB_FUNC_LOAD() does below.

Suggested change
#_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.

Comment thread src/ucs/sys/lib.c
sym = ucs_sys_dlsym_shallow(lib_info.dli_fname, dl, sym_name,
UCS_LOG_LEVEL_DEBUG);
if (sym == NULL) {
(void)dlerror();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, "

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread buildlib/tools/builds.sh
fi

for node in $(echo "$versions" | \
grep -oE 'Name: (MLX5|IBVERBS)_[0-9][0-9.]*' | \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@svc-nvidia-pr-review

Copy link
Copy Markdown

Residual coverage gap: check_ib_symbol_versions runs only in build_debug, and on an image with old rdma-core headers the new code is compiled out, so a pass does not prove the optional path was exercised.

Residual coverage gap: the check inspects .gnu.version_r only; an unversioned libibverbs/libmlx5 would produce unversioned undefined refs that it cannot flag.

Residual coverage gap: contrib/ibmock does not define ibv_query_port_speed, so mock-based IB CI always exercises the NULL/fallback branch, never a resolved pointer.

Residual coverage gap: the !UCX_SHARED_LIB branch of UCS_SYS_LIB_OPTIONAL_FUNC() (&_func) is uncovered — there is no --disable-shared CI job.

@bwestheimer

Copy link
Copy Markdown
Contributor Author

I've force-pushed a reworked version based on the review. The branch is rebased on master and split into six commits.

Changes:
The optional functions are now resolved once when each module loads, as tvegas1 suggested, instead of lazily under UCS_INIT_ONCE. Direct NIC requires both mlx5dv functions again, the registration path is back to master's check, and the empty function and both init-once blocks are gone. The lookup uses dlsym() instead of dlvsym(), so there is no version string to maintain and no configure check for dlvsym(). The check that a symbol is defined by the library itself is the one the module loader already used: ucs_module_dlsym_shallow() moved from module.c to lib.c as ucs_sys_dlsym_shallow(), and the module loader calls it with its own log level, so its behavior does not change.

Common code, as discussed above:
The function-pointer table macros (UCS_SYS_LIB_FUNC_PTR_DECLARE/_DEFINE, UCS_SYS_LIB_FUNC_LOAD, UCS_SYS_LIB_FUNC_LOAD_OPTIONAL) and ucs_sys_dlsym() are now in ucs/sys/lib.h. NVML uses them for its pointers and lookup, gdr_copy uses ucs_sys_get_lib_sym() for gdr_get_attribute() instead of its own copy of the same steps, and the IB tables are built on them. NVML still opens its library by name and initializes on first use, and its wrappers are unchanged.

Two corrections to my earlier description:
There are two tables, one per module, rather than one shared table, because libuct_ib does not link libmlx5. Callers use a function through UCT_IB_OPTIONAL_FUNC(name) after a NULL check, or behind a capability that already requires it, as the Direct NIC registration path does.

On the bot's point about falling back to dlsym() when a symbol exists under another version:
The lookup now uses dlsym(), which returns the default version, so a vendor library that exports these functions under another version node is still found. The limit is the one noted in the description: if rdma-core ever adds a second version of one of these functions with a different prototype, the table entry has to be revisited.

Testing:
Ubuntu 24.04 aarch64 with rdma-core 66 headers and the inbox rdma-core 50 runtime: master's modules fail to load (IBVERBS_1.16, MLX5_1.25 not found) and fail the new CI check, while this change loads all IB modules, logs the three missing functions at debug level, and passes the check. gtest test_sys.*, the new test_uct_ib_optional_func, and the IB test_md/test_uct_iface tests pass on both the rdma-core 50 and 66 runtimes, except ib/test_md.sys_device on soft-RoCE, which fails the same way on master. Each commit builds with -Werror. The NVML and gdr_copy changes were only compile-checked here, since this machine has no CUDA, so CI is their first full build and test.
On GH200 with CX-7 (InfiniBand) and DOCA rdma-core 2607 (MLX5_1.27, IBVERBS_1.17), where master also loads, both builds load the same transports and memory domains, this change resolves all three functions, and the Direct NIC check gives the same result as master ("not supported" on CX-7). Loopback ucx_perftest tag_lat and tag_bw on rc_mlx5 and rc_verbs match master within run-to-run noise.

Note:
A run on mlx5 hardware with inbox rdma-core 50 is pending, and I'll add the results here.

@svc-ucx

svc-ucx commented Nov 21, 2026

Copy link
Copy Markdown

🤖 CI Triage Agent — UCX PR (AddressSanitizer BlueField on worker 3) · commit d20faa43

TL;DR: The BlueField ASan gtest job failed on LeakSanitizer ("68024 byte(s) leaked in 17 allocation(s)") in test_ucp_proto_mock_rcx_speed_change.rma_get; every direct leak is a small libmlx5 handle returned by a mlx5dv_devx_* create call (umem_reg / obj_create / alloc_uar) whose void-returning destroy counterparts are exactly the symbols this PR converts to module-load-resolved optional pointers, so a destroy symbol that fails to resolve silently no-ops and the DEVX objects are never released.

Full analysis

Summary: make test in the AddressSanitizer BlueField job exited 1 because LeakSanitizer reported 68 KB in 17 allocations at gtest exit, all allocated from test_ucp_proto_mock_rcx_speed_change_rma_get_Test::init() (test/gtest/ucp/test_ucp_proto_mock.cc:5116).

Root cause: The leak set is composed entirely of mlx5 DEVX resources and the buffers they reference:

  • Direct leak of 40 byte(s) × N — libmlx5 calloc inside mlx5dv_devx_umem_reg() called from uct_ib_mlx5_md_buf_alloc (src/uct/ib/mlx5/ib_mlx5.h:1192), via uct_ib_mlx5_devx_create_cq (ib_mlx5_dv.c:695/704), uct_ib_mlx5_add_page (ib_mlx5dv_md.c:1732), uct_rc_mlx5_devx_init_rx_common (rc_mlx5_devx.c:233) and uct_ib_mlx5_devx_create_qp (ib_mlx5_dv.c:290).
  • Indirect leak of 48 byte(s) — libmlx5 handle from mlx5dv_devx_obj_create (uct_ib_mlx5_devx_obj_create, ib_mlx5_dv.c:480).
  • Indirect leak of 152 byte(s) — libmlx5 handle from mlx5dv_devx_alloc_uar (ib_mlx5.c:601) allocated by uct_ib_mlx5_devx_check_uar (ib_mlx5.c:645), a function that unconditionally calls uct_ib_mlx5_devx_uar_cleanup() → mlx5dv_devx_free_uar() before returning. That UAR can only leak if the free call did nothing.
  • The remaining indirect leaks (64 KB WQ buffer from ucs_posix_memalign, dbrec page, stub-iface/ep structs) are the buffers/objects still referenced by those leaked libmlx5 handles.

So the allocation side of the DEVX API works, but the release side (mlx5dv_devx_umem_dereg, mlx5dv_devx_obj_destroy, mlx5dv_devx_free_uar — all void, so a missing call is completely silent) does not take effect. That matches exactly what the branch under test changes: bwestheimer/mlx5dv-optional-syms adds UCS/SYS: Add lookup of an optional symbol in a loaded library (b51c82f) and UCT/IB/MLX5: Resolve the Direct NIC functions when the module loads (8360997), i.e. these mlx5dv entry points are now called through pointers resolved at module load with a stub fallback. A destroy/free symbol missing from the resolution table (or resolved against the wrong handle, e.g. versioned mlx5dv_* symbols in libmlx5) degrades to a no-op stub and produces precisely this leak signature. (Secondary possibility worth ruling out: a genuine UCP lane leak on reconfiguration in the new speed-change test, which was touched repeatedly just before this build — #12206, #12334, #12362, #11901 — but that would not explain the leaked check_uar UAR, which is freed on a straight-line code path.)

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 (uct_ib_mlx5_md_buf_free → mlx5dv_devx_umem_dereg), src/uct/ib/mlx5/ib_mlx5.c:708 (uct_ib_mlx5_devx_uar_cleanup → mlx5dv_devx_free_uar), src/uct/ib/mlx5/dv/ib_mlx5_dv.c:443 (uct_ib_mlx5_devx_destroy_qp_common → mlx5dv_devx_obj_destroy)

Suggested fix: Audit the new optional-symbol table for the release entry points — mlx5dv_devx_umem_dereg, mlx5dv_devx_obj_destroy, mlx5dv_devx_free_uar (and mlx5dv_devx_obj_modify/destroy_cq paths) — and make sure each is resolved whenever its matching create symbol was resolved. Do not allow a void-returning destroy symbol to fall back to a silent stub: if the create symbol resolved but its destroy symbol did not, log an error and fail module/MD initialization (or keep the directly linked symbol as fallback), and add an assertion/ucs_warn in uct_ib_mlx5_md_buf_free()/uct_ib_mlx5_devx_uar_cleanup() when the resolved pointer is a stub. Locally reproducible with gtest --gtest_filter='*rcx_speed_change*rma_get*' under ASan on a DEVX-capable device; if the leak persists with the symbol table fixed, re-run the same filter on master to separate it from the UCP speed-change/lane-reconfiguration changes.

Related: PR #12271 (#12271); test last modified by #12362, #11901, #12334, #12206.

Comment thread src/uct/ib/base/ib_md.c
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/ucs/sys/lib.h


/* 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants