Skip to content

UCT/CUDA_IPC: Support VMM with multiple memory allocations - #11283

Open
tomerg-nvidia wants to merge 3 commits into
openucx:masterfrom
tomerg-nvidia:multichunk-vmm-in-unpack
Open

tomerg-nvidia wants to merge 3 commits into
openucx:masterfrom
tomerg-nvidia:multichunk-vmm-in-unpack

Conversation

@tomerg-nvidia

@tomerg-nvidia tomerg-nvidia commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

What?

Add support for CUDA VMM allocations that span multiple underlying physical allocations (cuMemCreate chunks) in the cuda_ipc transport.

Why?

A user-visible VA range may be backed by several independently created and mapped VMM chunks. Currently, cuda_ipc only exports a single fabric handle per region, which covers just the first chunk. This means transfers involving multi-chunk VMM regions silently access only the first chunk's memory, producing incorrect results for offsets beyond the first chunk boundary.

How?

Introduce UCT_CUDA_IPC_KEY_HANDLE_TYPE_VMM_MULTI. During mkey_pack, cuda_ipc detects when the requested VMM range spans multiple allocation buffer IDs, discovers the chunks covering the expanded VA range, exports each chunk’s fabric handle, and writes the chunk descriptors into a GPU-resident metadata buffer. A small GPU-resident header points to that descriptor buffer, and the header fabric handle is packed in the rkey.
During rkey_unpack, the receiver imports the metadata header, performs a blocking DtoH copy of the header, imports the chunk-descriptor buffer, and performs another blocking DtoH copy to fetch all chunk handles. The VMM_MULTI memhandle open path then imports and maps the chunks into one contiguous local VA range, which is cached like other cuda_ipc mappings.

Notes

  1. Will be split into multiple PRs. This is just for completeness.
  2. Posting performance is better than splitting the operation into one transfer per chunk, but worse than a single-allocation buffer because the resulting mapped range still has higher cuMemcpyAsync overhead.
  3. A new cache is introduced for multi-allocation views, because now an allocation can map into multiple views but since the local buffer will be different these can't be merged

@tomerg-nvidia
tomerg-nvidia force-pushed the multichunk-vmm-in-unpack branch 2 times, most recently from 4920050 to 250e476 Compare March 23, 2026 09:48
@tomerg-nvidia
tomerg-nvidia marked this pull request as ready for review March 24, 2026 07:21
@tomerg-nvidia
tomerg-nvidia requested a review from brminich March 24, 2026 07:21
@brminich
brminich requested review from rakhmets and tvegas1 March 25, 2026 15:16
Comment thread test/mpi/test_cuda_common.c Resolved
Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.h Resolved
Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.h Resolved
Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_iface.c Outdated
uct_cuda_ipc_event_desc_t);
ucs_status_t status;

if (cuda_ipc_event->mapped_addr == NULL) {

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 integrate VMM_MULTI in the cuda_ipc cache, releasing some refcnt here? This could enable LRU as implemented in #11245, to avoid VA exhaustion on some platforms?

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, now the unpack doesn't import the handles, it happens in the memhandle open path so it could be cached. This does have some drawbacks though: I'm using the first chunk's buffer id for the cache, so it could be that the mapping later changes but we still get a cache hit

Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.c Resolved
Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.c Resolved
Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.c Outdated
}

status = UCT_CUDADRV_FUNC_LOG_ERR(
cuMemcpyHtoD(meta_dev_ptr, host_chunks, meta_size));

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.

do we need to use one of our internal stream?

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.

I don't have access to the internal streams in this function, as the iface is needed to get the stream which I don't have at unpack. I can maybe create a new async stream for this, what do you think?

Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.c Outdated
#if HAVE_CUDA_FABRIC
if (unpacked->super.super.ph.handle_type ==
UCT_CUDA_IPC_KEY_HANDLE_TYPE_VMM_MULTI) {
uct_cuda_ipc_vmm_contig_cleanup(unpacked);

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.

I have doubts about the performance of this solution.
We do not unmap other types of handles in rkey release for this reason. Since opening a handle exported from another process is a time-consuming operation. Probably we should do the same way for this new handle type.

Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_md.c Outdated
goto err_pop_ctx;
}

status = uct_cuda_ipc_create_contig_mapping(unpacked,

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.

Is it possible to unify the flow with existing types?
The mapping is done in get/put function. And unmapping is done via the event callback.

@guy-ealey-morag

Copy link
Copy Markdown
Contributor

Make sure to update the copyright years at the top of all the changed files

@nicolasnoble

Copy link
Copy Markdown
Contributor

@tomerg-nvidia quick scope question. Does the multi-chunk VMM fix here benefit the IB transport, or is it scoped strictly to cuda_ipc / fabric memory? I'm hitting the same multi-chunk VMM failures on the IB side with ibv_reg_dmabuf_mr on cross-node weight transfers, want to know whether to follow this PR or if there's a separate piece tracking the IB path.

@openucx openucx deleted a comment from svc-nixl May 4, 2026
@tomerg-nvidia
tomerg-nvidia force-pushed the multichunk-vmm-in-unpack branch from e3b2860 to ce5fc24 Compare August 13, 2026 15:31
nicolasnoble added a commit to ai-dynamo/modelexpress that referenced this pull request Aug 14, 2026
The arena's docs presented UCX_CUDA_COPY_REG_WHOLE_ALLOC=off as the
complete deployment precondition and described multi-handle single-MR
registration as validated without naming a transport. Both are true only
on dmabuf/IB. On cuda_ipc a fabric handle names one cuMemCreate
allocation, so a single MR over a multi-allocation arena publishes an
rkey covering only the first chunk.

- scope the cuda_copy knob in both env tables and in vmm/README.md
- add a Transport support section and a cuda_ipc known limitation
- fix the register_arena docstring, which asserted validation directly
  above the fallback that same function performs
- point at openucx/ucx#11283 for the upstream cuda_ipc fix

Signed-off-by: Nicolas 'Pixel' Noble <nicolas@nobis-crew.org>
Comment thread src/uct/cuda/cuda_ipc/cuda_ipc_cache.c Outdated
return status;
}

static ucs_status_t uct_cuda_ipc_open_memhandle_vmm_multi(

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.

Observed cuda_ipc_cache.c:721 Assertion 'pgt_region != NULL' failed. when trying this PR, which could be related.

could put_region be calling cache_region_destroy / invalidate_regions on a mapping that still has refcount > 0 (same d_bptr + new buffer_id, or overlapping VMM_MULTI)? if so, can we keep in-use regions until last unmap, or at least assert on refcount when removing entries?

also, if we pack again with a wider range, vmm_multi_meta_cleanup rebuilds meta — is that safe for peers still holding the old rkey?

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.

Relevant not checking refcount: uct_cuda_ipc_cache_invalidate_regions , uct_cuda_ipc_cache_region_remove and uct_cuda_ipc_cache_region_destroy.

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.

Two cuda_ipc GETs use different VMM_MULTI rkeys made of 20MB chunks. They overlap, and the second ends after the first (30 chunks [0x3720000000, 0x3745800000) vs 2 chunks [0x3744400000, 0x3746c00000)).

The shared 20MB chunk is the same remote VA, so that chunk’s buffer_id should match; the cache still compares the sum of all chunk ids (30 vs 2), treats the second key as stale, destroys the first mapping without refcount check, and re-inserts only the shorter span. First GET completion then unmaps a d_bptr that is gone (cuda_ipc_cache.c:721).

I think ucp_rkey_pack/unpack with area that partly overlap is probably valid usage.

Could we cache the individual chunks with their buffer_id instead and adapt caching logic to check various buffer_id/add missing ones?

Factor the balancing mechanics of ucs_interval_tree_t into a reusable
intrusive red-black tree, ucs_rbtree_t, and port the interval tree onto
it. The caller performs its own descent and hands the attachment point to
ucs_rbtree_insert_at, so the ordering key stays in the embedding struct.
An optional augmentation callback recomputes data a node derives from its
subtrees, which the interval tree does not use and leaves NULL.

The only behavior that changes is node identity on removal: the successor
is relinked rather than having its interval copied into the removed node,
so the node object a caller names is the one removed. Interval tree nodes
are pool allocated and never referenced from outside, so this is not
observable here; it is what lets a later user hold node pointers across
unrelated removals.
Add ucs_interval_map_t, a map of half-open intervals to caller-owned
objects. Unlike ucs_interval_tree_t, which merges, it keeps
overlapping intervals distinct: inserting [0,4) and [2,6) yields two
entries, both findable.

It is a ucs_rbtree_t ordered by interval start and augmented with each
subtree's maximum end, so finding an interval that contains a given range
costs O(log n). Nodes are intrusive and caller-allocated, and removal
never relocates one, so removal by a held pointer needs no search.
A VMM buffer whose range spans more than one physical allocation could
not be exported: the packed key described only the allocation containing
its base, so a peer received a key covering less memory than was
registered.

Publish the chunk layout instead. At pack time the range is expanded to
whole allocations, their descriptors are written to a GPU buffer, and its
fabric handle travels in the key together with an inline description of
the layout. At unpack the peer fetches the descriptors, and at import it
reserves one contiguous address range and maps every chunk into its slot,
so the rest of the transport sees a single base pointer.

Imported views are cached per peer and indexed by remote range, which
lets a key nested inside an already-imported range reuse it instead of
re-importing. Views may overlap, so they are held in an interval map
rather than the page table. A view is verified per chunk on every hit,
by base address and buffer ID, so a recycled remote address is detected
rather than reused. Teardown waits for outstanding references, since an
operation in flight holds a pointer into the reservation.
@tomerg-nvidia
tomerg-nvidia force-pushed the multichunk-vmm-in-unpack branch from ce5fc24 to e4f418a Compare September 14, 2026 07:18
@svc-nvidia-pr-review

Copy link
Copy Markdown

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

}
#endif
goto err_free_rkey_handle;
err_pop_ctx:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blocker: err_pop_ctx is only reachable from inside #if HAVE_CUDA_FABRIC, so a non-fabric CUDA build (CU_MEM_HANDLE_TYPE_FABRIC not declared) fails with -Wunused-label, and BASE_CFLAGS has -Wall -Werror.

Suggested change
err_pop_ctx:
goto err_free_rkey_handle;
#if HAVE_CUDA_FABRIC
err_pop_ctx:
if (cuda_device != avail_cuda_device) {
uct_cuda_ctx_primary_pop_and_release(avail_cuda_device);
}
#endif

return 0;
}

for (i = 0; i < unpacked->num_chunks; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

uct_cuda_ipc_multi_cache_verify() runs on the data path — every put/get goes through uct_cuda_ipc_get_remote_address() -> uct_cuda_ipc_map_memhandle() -> _multi_cache_get() — and this loop is O(num_chunks) under the global cache write lock. docs/OptimizationStyle.md asks to keep such lookups O(1); can we validate staleness with a single value (e.g. a generation/id stored in the metadata) instead of comparing every chunk?

}

/* Peers may unpack any published metadata until memh deregistration. */
ucs_list_add_tail(&key->vmm_multi_list, &meta->list);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Each mkey_pack() for a range not contained in an already published meta allocates a new device buffer (rounded up to the allocation granularity), exports a fabric handle and does an HtoD copy, and the list is only released at mem_dereg(). What bounds the number of entries per lkey for an app that packs many different sub-ranges of the same registration?

status = uct_cuda_ipc_mkey_pack_vmm_multi_chunk(memh, key, address,
length, &meta);
if (status == UCS_OK) {
packed->ph.handle_type =

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 this break wire compatibility? An older peer that gets UCT_CUDA_IPC_KEY_HANDLE_TYPE_VMM_MULTI falls into the default: case of uct_cuda_ipc_open_memhandle() and logs ucs_error("unsupported key handle type") for every rkey before declaring the peer unreachable. Is there any negotiation, or should the packer keep the old VMM type when the new type cannot be understood?

#if HAVE_CUDA_FABRIC
uct_cuda_ipc_multi_cache_purge(cache, close_handles);
#endif
ucs_list_head_init(&cache->lru_list);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

uct_cuda_ipc_cache_purge() and uct_cuda_ipc_multi_cache_purge() already decrement num_regions/total_size and unlink every region from lru_list, so this reset has no distinct state transition to perform. Per the root AGENTS.md rule on repeated reset logic, pls drop it.

ucs_interval_tree_find_overlap(ucs_rbtree_node_t *rb_node,
ucs_interval_tree_range_t range)
{
ucs_interval_node_t *node = ucs_interval_tree_node(rb_node);

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: ucs_derived_of() is applied to rb_node before the NULL check below. It happens to work because super is at offset 0, but pls move the conversion after the check so the code does not rely on that.

/**
* @brief Whether the tree holds no nodes
*/
static UCS_F_ALWAYS_INLINE int ucs_rbtree_is_empty(const ucs_rbtree_t *tree)

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: ucs_rbtree_is_empty() has no callers; pls remove it until something needs it.

rkey_a.super.pid = getpid() + 100000;
rkey_b.super.pid = rkey_a.super.pid;
rkey_request.super.pid = rkey_a.super.pid;
unpacked_a.super = rkey_a;

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: these consecutive assignments are split into two alignment columns; pls align them all to the longest LHS (unpacked_request.super.pid_ns) per docs/CodeStyle.md.

@svc-nvidia-pr-review

Copy link
Copy Markdown

This PR adds roughly 2,600 lines across two independent changes: a generic ucs_rbtree core + interval_tree refactor + new ucs_interval_map, and the cuda_ipc multi-chunk VMM feature. REVIEW.md caps a PR at 500 added lines, and requires refactoring to be in a separate PR with no functional changes. Please split into at least:

  1. UCS/DATASTRUCT: Extract generic red-black tree from interval_tree (+ shared rbtree_check.h, no functional change).
  2. UCS/DATASTRUCT: Add interval map (+ test_interval_map.cc).
  3. UCT/CUDA_IPC: Support multi-chunk VMM registrations.

The unrelated fixes bundled in (pgtable cleanup leak in uct_cuda_ipc_create_cache, release_empty_rkey test, whitespace/typo touch-ups) should also move out of the feature PR.

src/ucs/AGENTS.md (datastruct bullet): pls add rbtree and interval_map to the datastruct/ list, so the new containers are discoverable from the subtree guide.

Coverage gap: ucs_rbtree_remove()'s successor-splice path is only exercised indirectly through test_interval_map.random_stress and interval_tree pops. That looks sufficient for the invariants, but there is no direct test that a caller-held node pointer stays valid across a two-child removal — which is the stated reason for relinking instead of copying keys.

Coverage gap: uct_cuda_ipc_multi_cache_purge() with refcount > 0 (the ucs_warn + unindexed-but-live path) is not covered; that is also the path where a still referenced view can outlive its cache.

Coverage gap: the multi-chunk data path itself is only covered by test/mpi (VMM_Multi allocator), which does not run in the gtest CI matrix. Is there a fabric-capable CI job that exercises it?

@tomerg-nvidia tomerg-nvidia added the WIP-DNM Work in progress / Do not review label Sep 14, 2026
@svc-ucx

svc-ucx commented Sep 14, 2026

Copy link
Copy Markdown

🤖 CI Triage Agent — UCX PR (Coverity coverity devel on coverity_rh7) · commit e4f418a5

TL;DR: The Coverity gate failed (exit code 4) on 4 new defects introduced by this PR's own commit — 3 × CHECKED_RETURN for unchecked cuMemUnmap/cuMemRelease/cuMemAddressFree in the new cuda_vmm_mem_buffer::cleanup(), plus 1 × FORWARD_NULL for the new release_empty_rkey test passing a zero-initialized (NULL-handle) rkey bundle into uct_rkey_release. Fix by checking the CUDA driver return values in cleanup() and not routing a NULL handle through the generic uct_rkey_release function pointer.

Full analysis

Summary: coverity devel on coverity_rh7 — the build and cov-build/cov-analyze succeeded, but cov-format-errors reported nerrors=4, so the stage exited with return 4 and Azure logged ##[error]Coverity found 4 issues.

Root cause: All 4 defects are in files last modified by the PR commit itself (e4f418a5):

  • test/gtest/uct/cuda/cuda_vmm_mem_buffer.h:167, :171, :175 — the new multi-chunk unwind path cleanup() calls cuMemUnmap(), cuMemRelease() and cuMemAddressFree() and discards their CUresult. Coverity's CHECKED_RETURN flags this because the same APIs are checked elsewhere in the tree (e.g. src/uct/cuda/cuda_ipc/cuda_ipc_vmm_multi.c:71,120,199,206,208, cuda_copy_md.c:286,289,545 all check _result == CUDA_SUCCESS), so the unchecked calls are statistical outliers.
  • test/gtest/uct/cuda/test_cuda_ipc_md.cc:330 — the new release_empty_rkey test does uct_rkey_bundle_t bundle = {}; (so bundle.handle == NULL) and passes &bundle to uct_rkey_release(). uct_md.c:380 calls component->rkey_release(component, rkey_ob->rkey, rkey_ob->handle); with --enable-fnptr --fnptr-models enabled in this job, Coverity resolves that pointer to uct_posix_rkey_release → uct_posix_mem_detach_common, which directly dereferences the handle (src/uct/sm/mm/posix/mm_posix.c:487), producing an explicit-NULL-dereference report.

There is no infrastructure/timeout problem: compilation emitted 748/748 units successfully and analysis completed in 00:23:45 with continuous progress.

Implicated commit: [REDACTED:Hex High Entropy String] — Tomer Gilad, "UCT/CUDA_IPC: Support VMM with multiple memory allocations" (PR #11283)

File: test/gtest/uct/cuda/cuda_vmm_mem_buffer.h:164-181 (defects at 167, 171, 175) and test/gtest/uct/cuda/test_cuda_ipc_md.cc:326-331 (defect at 330)

Suggested fix:

  1. In cuda_vmm_mem_buffer::cleanup(), consume the return values instead of discarding them — matching the EXPECT_EQ(CUDA_SUCCESS, ...) style already used in test_cuda_ipc_md.cc (e.g. lines 64, 67, 79), or capture and log:
    for (size_t i = 0; i < m_num_mapped; ++i) {
        CUresult ret = cuMemUnmap(m_ptr + (i * m_chunk_size), m_chunk_size);
        if (ret != CUDA_SUCCESS) {
            UCS_TEST_MESSAGE << "cuMemUnmap() failed: " << ret;
        }
    }
    and likewise for cuMemRelease() and cuMemAddressFree(). (A bare (void) cast will not silence Coverity's CHECKED_RETURN.)
  2. For release_empty_rkey, avoid pushing a NULL handle through the generic component function pointer. Either call the cuda_ipc release path directly (so Coverity resolves a concrete callee that tolerates a NULL handle) or set bundle.handle to a valid non-NULL handle for the release check. If the NULL-handle release is deliberately part of the contract being tested, keep the test but add an explicit guard/ucs_assert (or a documented Coverity suppression) so the analyzer can't reach the posix dereference model.
  3. Re-run the coverity devel job after the fixes; the stage passes only when nerrors == 0.

Related: PR #11283 (the failing PR); prior work on the same files: #11877, #11461, #11183; related rkey-release change #11288.

🛡️ This comment had 1 potential secret(s) redacted (Hex High Entropy String). See request_id c6d1c055-62f7-492c-b5c6-f7229d0d74c9 in the triage console for the audit trail.

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

WIP-DNM Work in progress / Do not review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants