monarch_rdma/backend/ibverbs/memory_region.rs
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Registered memory regions returned by [`IbvDomainImpl::register_mr`].
10//!
11//! [`IbvMemoryRegionView`] is the cheap, cloneable handle peers use: the keys
12//! and addresses for a slice of registered memory, plus an `Arc<dyn IbvMemoryRegionKeepalive>`
13//! that keeps the backing registration's resources alive until the last clone
14//! of the view drops.
15//!
16//! [`IbvDomainImpl::register_mr`]: super::domain::IbvDomainImpl::register_mr
17
18use std::sync::Arc;
19
20use super::primitives::IbvMr;
21
22/// Guards the resources behind a registered MR, releasing them when the last
23/// [`IbvMemoryRegionView`] over it drops. Each implementor frees whatever it
24/// owns in its own `Drop`; the trait carries no methods and exists only to
25/// type-erase the guards so a view can hold any of them behind an
26/// `Arc<dyn IbvMemoryRegionKeepalive>`.
27pub(super) trait IbvMemoryRegionKeepalive: std::fmt::Debug + Send + Sync {}
28
29/// A standalone [`IbvMr`] guards its own registration: its `Drop` runs
30/// `ibv_dereg_mr` against the PD it owns.
31impl IbvMemoryRegionKeepalive for IbvMr {}
32
33/// A cloneable handle to a slice of registered memory: the keys and addresses
34/// a peer needs, plus an `Arc<dyn IbvMemoryRegionKeepalive>` keepalive.
35///
36/// Cheap to clone; every clone shares the same guard, so the backing
37/// registration stays alive (and registered) until the last clone drops.
38#[derive(Debug, Clone)]
39pub struct IbvMemoryRegionView {
40 /// Virtual address in the local process address space.
41 pub virtual_addr: usize,
42 /// RDMA address, possibly offset from the region's base MR address.
43 pub rdma_addr: usize,
44 pub size: usize,
45 pub lkey: u32,
46 pub rkey: u32,
47 /// Name of the RDMA device the view's protection domain is on.
48 pub device_name: String,
49 /// Keeps the backing registration alive for every clone of this view; the
50 /// last drop releases its resources. Never read directly.
51 pub(super) _guard: Arc<dyn IbvMemoryRegionKeepalive>,
52}
53
54impl IbvMemoryRegionView {
55 pub(super) fn new(
56 virtual_addr: usize,
57 rdma_addr: usize,
58 size: usize,
59 lkey: u32,
60 rkey: u32,
61 device_name: String,
62 guard: Arc<dyn IbvMemoryRegionKeepalive>,
63 ) -> Self {
64 Self {
65 virtual_addr,
66 rdma_addr,
67 size,
68 lkey,
69 rkey,
70 device_name,
71 _guard: guard,
72 }
73 }
74}