Skip to content

librdmacm: Add Confidential Computing (CoCo) support for rsocket - #1826

Open
eagleidirect wants to merge 5 commits into
linux-rdma:masterfrom
eagleidirect:rsocket-coco
Open

eagleidirect wants to merge 5 commits into
linux-rdma:masterfrom
eagleidirect:rsocket-coco

Conversation

@eagleidirect

@eagleidirect eagleidirect commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Enable librdmacm and rsocket (SOCK_STREAM and SOCK_DGRAM) to operate in Confidential Computing (CoCo) guests using the ibv_alloc_buf(), ibv_reg_buf_mr(), and parent domain APIs from #1748:

  1. librdmacm: Add CoCo parent domain support to cma_device — cache IBV_DEVICE_CC_DMA_BOUNCE and a single lazily allocated coco_pd on struct cma_device alongside cma_dev->pd, and expose private opt-in helpers ucma_set_coco_pd() and ucma_create_cq().
  2. librdmacm/rsocket: Fix teardown and init order in rs_free/ds_free_qp — keep cm_id->pd alive until buffers are freed, fix qp->dest.ah leak and errno preservation, order rs_create_cq() before ds_init_bufs(), and guard rs_create_cq() against NULL cm_id->verbs/cm_id->pd.
  3. librdmacm/rsocket: Allocate datagram send buffer per ds_qp — allocate qp->sbuf per ds_qp so registration PD matches allocation PD, and move smsg_free into a private index stack synchronized under rs->cq_lock.
  4. librdmacm/rsocket: Use ibv_alloc_buf() and ibv_reg_buf_mr() for buffers — switch stream and datagram data buffers to PD-backed ibv_alloc_buf() / ibv_reg_buf_mr() / ibv_free_buf(), keeping page-size rounding for MADV_DONTFORK.
  5. librdmacm/rsocket: Enable CoCo parent domain via librdmacm — opt rsocket cm_ids into librdmacm's shared coco_pd via ucma_set_coco_pd() and ucma_create_cq().

cc: @shefty @jpirko @jakemoroni @10000TB

Comment thread librdmacm/rsocket.c Resolved
Comment thread librdmacm/rsocket.c
free(rs->rmsg);

if (rs->index >= 0)
rs_remove(rs);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This also seems a net positive change, to prevent new accesses after buf freeed, and before rs_remove. Was this done as a general improvement together as part of CoCo support, or is there a CoCo specific reason for this ordering change ?

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: in general, calling rs_remove() and rdma_destroy_qp() before deregistering MRs and freeing sbuf/rbuf prevents hardware or concurrent fd lookups from touching unmapped buffers. For CoCo specifically (commit 4), cm_id->pd becomes the CoCo parent PD backing both the QP/CQ and the ibv_buf allocations (sbuf_ibv/rbuf_ibv), so destroying the QP/CQ first, then freeing the MRs/ibv_bufs, and finally calling ibv_dealloc_pd(cm_id->pd) before rdma_destroy_id() ensures all child objects are released before the parent PD and its dmabuf_heap are torn down.

@eagleidirect

Copy link
Copy Markdown
Contributor Author

Tested end-to-end (rstream, udpong, and LD_PRELOAD=librspreload.so) in CoCo and non-CoCo VMs alongside #1766 (note: this PR only uses the generic libibverbs CoCo APIs from #1748 and does not depend on #1766 to build or merge).

cc @shefty @jpirko

@jakemoroni

Copy link
Copy Markdown
Contributor

FWIW, there is a patch series that I believe will effectively prevent RDMA CM from working with untrusted NICs in a CVM, which I think would break rsocket. Can you take a look and see what the impact would be?

@eagleidirect

Copy link
Copy Markdown
Contributor Author

Thanks for flagging this, @jakemoroni! I looked through Yishai's V2 kernel series and companion PR #1844.

For cc_dma_bounce devices that leave ib_device::adversarial_mode = 0 (like irdma in #1766 today), kverbs_provider stays true, so rdma_cm and rsocket (SOCK_STREAM and SOCK_DGRAM) continue working as-is.

For devices that set adversarial_mode = 1 (mlx5 in a CC guest), you're right: RDMA core clears kverbs_provider so kernel ULPs (MAD, IB CM, rdma_cm) don't bind to the untrusted device, which makes librdmacm device/address resolution fail with ENODEV and breaks rsocket on mlx5 in a CVM. PR #1844 also rejects IBV_QPT_UD and ibv_create_ah(), ruling out SOCK_DGRAM at the verbs layer.

All of RoCE/IB rsocket SOCK_STREAM's RC data-path verbs (IBV_QPT_RC, RDMA_WRITE, RDMA_WRITE_WITH_IMM, IBV_SEND_INLINE, 0-SGE ibv_post_recv, and ibv_poll_cq) are still supported by #1844, though. So if we want SOCK_STREAM to work on adversarial_mode devices in a follow-up, rsocket could fall back to an out-of-band TCP handshake to transition the RC QP via uverbs (plus bounds-checking wc.imm_data and target_sgl in shared memory).

@shefty shefty left a comment

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 only reviewed 2 of the patches. My main concern is that this series tries to add CoCo support only to rsockets without the rdma_cm itself being CoCo aware. Yet, it's the rdma_cm which is calling through to CoCo verbs.

I don't think the rdma_cm needs full support (so pieces of the implementation can be deferred), but it should have enough to support rsocket's needs.

I haven't thought through all the pieces, but my general idea is to store the parent_domain with the ucma_device. For the cm_id, an opt-in model is preferrable. For example, use rdma_set_option() to change the cm_id->pd to the parent_domain. This should be set prior to using the cm_id->pd.

The CoCo requirement could be stored as a flag on the ucma_device. We may need to expose a new private ucma_ function for rsockets to use.

Comment thread librdmacm/rsocket.c Outdated

if (qp->cm_id) {
if (qp->dest.ah)
ibv_destroy_ah(qp->dest.ah);

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.

This looks like it solves a real leak

Comment thread librdmacm/rsocket.c
rdma_destroy_qp(qp->cm_id);
} else if (qp->cm_id->recv_cq) {
ibv_destroy_cq(qp->cm_id->recv_cq);
ibv_destroy_comp_channel(qp->cm_id->recv_cq_channel);

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 don't believe this path can be taken, but I'm okay keeping it.

Comment thread librdmacm/rsocket.c
rdma_destroy_qp(rs->cm_id);
} else if (rs->cm_id->recv_cq) {
ibv_destroy_cq(rs->cm_id->recv_cq);
ibv_destroy_comp_channel(rs->cm_id->recv_cq_channel);

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.

As above, I don't think this path can be taken, but I'm okay with it.

Comment thread librdmacm/rsocket.c
ibv_destroy_comp_channel(rs->cm_id->recv_cq_channel);
}
}

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.

The reordering of the destroy path is fine. But it shouldn't have an impact on security. Deregistering a MR should invalidate the lkey/rkey, resulting in any access to the MR failing.

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.

Good point. Updated the commit message in v2 to drop the security rationale and focus on keeping cm_id->pd alive until after ibv_free_buf().

Comment thread librdmacm/rsocket.c Outdated
if (!coco_parent_pd)
goto err1;
cq_attr.parent_domain = coco_parent_pd;
cm_id->recv_cq = ibv_cq_ex_to_cq(ibv_create_cq_ex(cm_id->verbs, &cq_attr));

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 a couple concerns about this flow. First, this queries the device for CC_DMA_BOUNCE for every rsocket. Second, it allocates a parent domain for each rsocket.

The coco_parent_pd would be better paired next to the actual pd. That means relocating it to umca_device, which would fix both concerns.

And that raises the issue whether the approach shouldn't start with adding CoCo support to the rdma_cm itself.

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.

Updated in v2: moved cc_dma_bounce and a single lazily allocated coco_pd onto struct cma_device alongside cma_dev->pd (commit 1), so the device is queried once and one shared parent domain is reused across rsockets.

Comment thread librdmacm/rsocket.c Outdated
if (coco_parent_pd) {
cm_id->pd = coco_parent_pd;
rs->opts |= RS_OPT_COCO_PD;
}

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.

For example, changes like this where the PD is swapped out, so that rdma_create_qp works builds in a hidden and possibly fragile dependency.

The rdma_cm 'core' needs to use the parent_domain, but it's unaware that it's actually occuring.

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.

Updated in v2: removed the direct cm_id->pd swap from rsocket.c. rsocket now opts in via private ucma_set_coco_pd() and ucma_create_cq() helpers in cma.c, so librdmacm owns the coco_pd lifecycle and propagates the opt-in to accepted child IDs.

In Confidential Computing (CoCo) guests whose RDMA device reports
IBV_DEVICE_CC_DMA_BOUNCE, hardware queues and data buffers must be
allocated from an unprotected parent domain created with
IBV_PARENT_DOMAIN_INIT_ATTR_ALLOW_CC_UNPROTECTED_ALLOC. Having internal
consumers such as rsocket query the device and allocate a parent domain
per connection adds repeated device queries, duplicates parent domains,
and leaves librdmacm unaware of the substituted cm_id->pd.

Cache IBV_DEVICE_CC_DMA_BOUNCE and a single lazily allocated coco_pd on
struct cma_device alongside cma_dev->pd, releasing coco_pd before pd
when the device refcount drops to zero or the device is removed. If a
provider does not support ALLOW_CC_UNPROTECTED_ALLOC (EOPNOTSUPP or
EINVAL), clear the cached flag and fall back to cma_dev->pd.

Add private helpers ucma_set_coco_pd() and ucma_create_cq() so callers
can opt an rdma_cm_id into the shared CoCo parent domain (propagated to
accepted child IDs) and create extended CQs bound to coco_pd while
leaving non-CoCo and regular rdma_cm paths unchanged.

Signed-off-by: Ying Xiao <eagleshaw@google.com>
Upcoming ibv_free_buf() buffer teardown requires the buffer's
allocation PD (cm_id->pd) to remain valid until all buffers are
released, and rs_free()/ds_free_qp() currently deregister MRs and free
buffers before destroying QPs/CQs and removing the rsocket from lookup
tables. In addition, ds_free_qp() leaks qp->dest.ah and clobbers errno
when unwinding ds_create_qp() failures, ds_create_qp() initializes
buffers before creating the CQ, and rs_create_cq() dereferences
cm_id->verbs without checking for a non-RDMA rdma_bind_addr().

Reorder rs_free() and ds_free_qp() to destroy the QP (or standalone CQ
and completion channel) and qp->dest.ah before deregistering MRs and
freeing buffers, move rs_remove() and rs_free_iomappings() ahead of
buffer teardown, and call rdma_destroy_id() last. Preserve errno across
ds_free_qp() on ds_create_qp() error paths, create the CQ before
ds_init_bufs() in ds_create_qp() to match rs_create_ep(), and return
ENODEV from rs_create_cq() when cm_id->verbs or cm_id->pd is NULL.

Fixes: e6e93ed ("rsocket: Add datagram support")
Signed-off-by: Ying Xiao <eagleshaw@google.com>
A single SOCK_DGRAM rsocket can create multiple ds_qp instances across
different local source addresses or RDMA devices (rs->qp_list), each
with its own qp->cm_id->pd. Unlike qp->rbuf, which is allocated per
ds_qp, sbuf was previously allocated once on struct rsocket and
registered across every ds_qp.

Because ibv_reg_buf_mr() requires the registration PD to match the
allocation PD (pd == buf->pd), a single shared send buffer cannot be
registered across multiple ds_qp instances with distinct PDs.

Move sbuf into the SOCK_STREAM union of struct rsocket and allocate
qp->sbuf per struct ds_qp in ds_init_bufs(), matching qp->rbuf. Keep
the send slot freelist (qp->smsg_free) in a private uint16_t index
stack instead of an intrusive pointer list inside qp->sbuf so raw guest
pointers are never stored in DMA-accessible/unencrypted send buffers,
synchronize slot pop/push under rs->cq_lock, bounds-check completion
slot indices, and return the slot if ds_post_send() fails.

Signed-off-by: Ying Xiao <eagleshaw@google.com>
In Confidential Computing (CoCo) guests, private memory allocated via
forksafe_alloc() is encrypted and cannot be DMA-accessed by the RDMA
device; buffers must instead be allocated through the protection
domain's buffer allocator (such as an unprotected DMA-buf heap on a
CoCo parent domain).

Replace forksafe_alloc() and rdma_reg_msgs()/rdma_reg_write() with
ibv_alloc_buf(), ibv_reg_buf_mr(), and ibv_free_buf() for stream and
datagram rsocket buffers. Keep page-size length rounding in
rs_alloc_buf() (while registering the logical length with
ibv_reg_buf_mr()) so MADV_DONTFORK fork-safety from
commit b2fedc4 ("rsocket: Make sure that the allocated memory is
aligned") is preserved on providers using libibverbs' default
alloc_buf(). Zero rs->target_buffer_list explicitly, reject zero-length
allocations, guard page rounding against overflow, and preserve errno
on allocation failure.

Signed-off-by: Ying Xiao <eagleshaw@google.com>
In Confidential Computing (CoCo) guests whose RDMA device reports
IBV_DEVICE_CC_DMA_BOUNCE, opt rsocket cm_ids into librdmacm's shared
per-device CoCo parent domain via ucma_set_coco_pd() in rsocket()
(SOCK_STREAM) and ds_create_qp() (SOCK_DGRAM), and create CQs via
ucma_create_cq() in rs_create_cq().

Because cma_device caches both the CC_DMA_BOUNCE capability and the
shared coco_pd, rsocket does not need to query the device or allocate a
parent domain per socket, and cm_id->pd is managed directly by
librdmacm across active connections, accepted child sockets, and
datagram QPs.

Signed-off-by: Ying Xiao <eagleshaw@google.com>
@eagleidirect eagleidirect changed the title librdmacm/rsocket: Add Confidential Computing (CoCo) support llibrdmacm: Add Confidential Computing (CoCo) support for rsocket Nov 22, 2026
@eagleidirect eagleidirect changed the title llibrdmacm: Add Confidential Computing (CoCo) support for rsocket librdmacm: Add Confidential Computing (CoCo) support for rsocket Nov 22, 2026
@eagleidirect

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @shefty. I reworked the series to make librdmacm CoCo-aware as you suggested:

  • Cached cc_dma_bounce and a lazily allocated shared coco_pd on struct cma_device alongside cma_dev->pd, releasing coco_pd before pd on teardown.
  • Added private opt-in helpers (ucma_set_coco_pd() and ucma_create_cq()), with accepted child IDs inheriting the opt-in and providers returning EOPNOTSUPP/EINVAL falling back to cma_dev->pd. rsocket no longer queries the device per socket or swaps cm_id->pd directly.
  • Dropped the security claim from the destroy-order commit message, preserved page-size rounding for MADV_DONTFORK in rs_alloc_buf(), and moved SOCK_DGRAM's send freelist out of DMA-accessible qp->sbuf into a private index stack.

Re-tested on both CoCo (irdma) and non-CoCo hosts. PTAL when you have a chance.

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.

4 participants