1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Craton Software Company
//! Backend-agnostic tenant memory-pool abstraction shared across crates.
//!
//! The per-tenant GPU cap (roadmap feature #8, T39) needs
//! `tensor-wasm-tenant` to keep a tenant's cap aligned with a concrete
//! CUDA memory pool living in `tensor-wasm-mem`. But `tensor-wasm-tenant`
//! cannot depend on `tensor-wasm-mem`: `mem` already depends on `tenant`
//! (its `TensorWasmMemoryCreator::with_tenant_context` builder takes an
//! `Arc<TenantContext>` to drive `consume_gpu_bytes`), so a `tenant -> mem`
//! edge would close a dependency cycle.
//!
//! This module breaks that cycle by hoisting the *interface* into
//! `tensor-wasm-core` — a crate both `mem` and `tenant` already depend
//! on. `tensor-wasm-tenant` holds an
//! `Option<Arc<dyn DriverMemPool>>` and pushes the cap through
//! [`DriverMemPool::set_release_threshold`]; `tensor-wasm-mem`'s
//! `cuda_mem_pool::TenantMemPool` implements the trait. Neither crate
//! references the other's concrete types, so no cycle forms.
//!
//! NB: `set_release_threshold` sets `CU_MEMPOOL_ATTR_RELEASE_THRESHOLD`,
//! a memory-*retention* hint — NOT an allocation ceiling. The actual
//! per-tenant cap is enforced host-side in `TenantMemPool::allocate`
//! (see `docs/GPU-QUOTAS.md` and BUG-1 in
//! `docs/GPU-VALIDATION-2026-05-30.md`). The surface here is deliberately
//! minimal: keep the threshold aligned with the cap, and read it back for
//! honest reporting. The concrete `cuMemPool*` FFI, pool creation, the
//! host-side cap check, and allocation paths stay in `tensor-wasm-mem`
//! behind its cudarc gating.
use Arc;
/// Errors raised by [`DriverMemPool`] operations.
///
/// Owned here in `tensor-wasm-core` (moved out of
/// `tensor-wasm-mem::cuda_mem_pool`) so both the trait and its
/// implementors share one error type without either crate depending on
/// the other. The `String` payloads carry the `Debug`-formatted
/// underlying CUDA result so the operator alert path keeps the same
/// context it had when these variants lived in `mem`.
///
/// SECURITY (finding: `MemPoolError` Display leaks raw CUDA detail): the
/// raw CUDA `Debug` payload can contain host pointer addresses and paths,
/// so — mirroring [`crate::error::TensorWasmError`]'s opaque-`Display` +
/// [`inner()`](Self::inner) pattern — the `Create`/`SetAttribute`/`Device`
/// variants render an OPAQUE label via `Display` (no `{0}`) and surface
/// the inner string only via [`Debug`](std::fmt::Debug) or the explicit
/// [`inner()`](Self::inner) accessor. **Never expose `inner()` to a
/// tenant-facing surface** — it is for server-side operator logs only.
/// A tenant memory pool whose release threshold (a CUDA memory-retention
/// hint) can be aligned to a per-tenant cap.
///
/// Implemented by `tensor-wasm-mem`'s `cuda_mem_pool::TenantMemPool`
/// (backed by `cuMemPoolSetAttribute(CU_MEMPOOL_ATTR_RELEASE_THRESHOLD, ...)`)
/// and by test mocks. `tensor-wasm-tenant` holds one as an
/// `Arc<dyn DriverMemPool>` and never names the concrete type — that is
/// what keeps the `mem` <-> `tenant` dependency graph acyclic.
///
/// NB: `set_release_threshold` does NOT enforce the allocation cap —
/// `CU_MEMPOOL_ATTR_RELEASE_THRESHOLD` is a retention hint, not a ceiling.
/// The concrete `TenantMemPool` enforces the cap host-side in its
/// `allocate` method. The trait name retains "DriverMemPool" for
/// compatibility; read it as "the tenant's driver-backed pool". See
/// `docs/GPU-QUOTAS.md` (and BUG-1 in `docs/GPU-VALIDATION-2026-05-30.md`).
///
/// `Send + Sync` so the pool can be shared across the threads that drive
/// a tenant's allocations (the tenant context is itself shared behind an
/// `Arc`). `Debug` so `TenantContext` (which holds an
/// `Option<Arc<dyn DriverMemPool>>`) keeps its derived `Debug` impl;
/// the concrete `tensor-wasm-mem::TenantMemPool` already derives it and
/// test mocks can too.