Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a3303c7
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
69e8a65
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
d7e89df
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
5826f89
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
5806e55
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
d335520
Graph cache: TF_GRAPH_CACHE=0 or off runs every step eagerly in any f…
jayleaton Oct 14, 2026
864fe25
Graph cache: effective settings have one digest for ranks to compare …
jayleaton Oct 16, 2026
7ff83d8
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
9af2ea2
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
7c9a26e
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
8ebb0be
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
e9e0056
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
0472cbf
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
9688398
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
72132f0
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
bf19238
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
beae7e8
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
602aad5
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
890dfc6
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
a3e2fb1
TP NCCL: all_reduce's doc says NCCL adds in its ring's order, so its …
jayleaton Oct 14, 2026
62a75ae
TP: keep Linux-only transport host tests off macOS builds
jayleaton Oct 15, 2026
e2504f6
TP: bootstrap compares the graph cache's effective settings, so a ran…
jayleaton Oct 16, 2026
5410eac
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
0d0bdb2
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
2cf61d3
Sessions: honor zero residency budgets and reserve uncapped requests
jayleaton Oct 15, 2026
dcdc30c
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
… with another region layout are refused

roce_guard.stripes computes each HCA's share of a post (its send stripe, the peer's receive range and its flag word)
and refuses with error.OutOfRegion any range past its slot, its flag area or the registered region, before
post_send. The verbs wire and the shm wire both post through it. The shm wire moves to roce_shm.zig and can stripe
like the verbs wire (Options.shm_stripes), with an optional tap that sees every post for tests. The connection record
carries the slot and registered length, so a peer with another layout is refused at connect; its magic is bumped.

Tests: stripes tile the slot over two HCAs and refuse a post past the slot, a third slot, an unpadded or empty post,
no HCA and a short region. On fake verbs a post past its slot reaches no verb, and a full slot stays inside the
region. Over the shm wire striped as two HCAs, 2,000 variable-length gathers stay inside slot and region, bit for bit.
  • Loading branch information
jayleaton committed Oct 16, 2026
commit 0472cbf9a2ea642520dd1ba5b90589cfaba1e472
60 changes: 12 additions & 48 deletions zig/src/fabric/tp/roce.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const collective = @import("collective.zig");
const Bootstrap = @import("bootstrap.zig").Bootstrap;
const sock = @import("sock.zig");
const rv = @import("roce_verbs.zig");
const shm = @import("roce_shm.zig");
const guard = @import("roce_guard.zig");
const HostRegister = @import("mailbox.zig").HostRegister;
const posix = std.posix;
const DevicePtr = collective.DevicePtr;
Expand Down Expand Up @@ -71,30 +73,20 @@ pub const Options = struct {
fast: u32 = 0,
/// TF_TP_ROCE_BLOCKS: the largest grid (config.roce_blocks)
blocks: u32 = 16,
};

/// The shm wire: the peer's region is mapped here; a post copies the slot, then stores the flag with release order.
const ShmWire = struct {
peer: []u8,
local: []u8,
slot_bytes: usize,

fn post(w: *ShmWire, slot: u32, seq: u32, padded: u32) void {
const recv = send_off + 2 * w.slot_bytes;
@memcpy(w.peer[recv + slot * w.slot_bytes ..][0..padded], w.local[send_off + slot * w.slot_bytes ..][0..padded]);
const flag: *std.atomic.Value(u32) = @ptrCast(@alignCast(w.peer[flag_off + slot * 2 * flag_stride ..].ptr));
flag.store(seq, .release);
}
/// The shm wire's stripes, as HCAs: its posts take the verbs wire's split and bounds.
shm_stripes: u32 = 1,
/// Tests: sees every shm post's stripes before the copy.
tap: ?*shm.PostTap = null,
};

const Wire = union(WireKind) {
verbs: rv.VerbsWire,
shm: ShmWire,
shm: shm.ShmWire,

fn post(w: *Wire, slot: u32, seq: u32, padded: u32) !void {
switch (w.*) {
.verbs => |*v| try v.post(slot, seq, padded),
.shm => |*s| s.post(slot, seq, padded),
.shm => |*s| try s.post(slot, seq, padded),
}
}

Expand All @@ -108,7 +100,7 @@ const Wire = union(WireKind) {
fn hcas(w: *const Wire) u32 {
return switch (w.*) {
.verbs => |v| v.n_hca,
.shm => 1,
.shm => |s| s.stripes,
};
}
};
Expand Down Expand Up @@ -204,9 +196,10 @@ pub const Roce = struct {
r.wire = .{ .verbs = try rv.VerbsWire.open(o.hcas, r.region, .{ .send = send_off, .recv = send_off + 2 * r.slot_bytes, .flags = flag_off, .slot = r.slot_bytes }, o.traffic_class) };
},
.shm => {
r.mapping = try sharedPair(b, len);
r.mapping = try shm.sharedPair(b, len);
r.region = @alignCast(r.mapping[r.me * len ..][0..len]);
r.wire = .{ .shm = .{ .peer = r.mapping[(1 - r.me) * len ..][0..len], .local = r.region, .slot_bytes = r.slot_bytes } };
const layout: guard.Layout = .{ .send = send_off, .recv = send_off + 2 * r.slot_bytes, .flags = flag_off, .slot = r.slot_bytes, .len = len };
r.wire = .{ .shm = .{ .peer = r.mapping[(1 - r.me) * len ..][0..len], .layout = layout, .local = r.region, .stripes = o.shm_stripes, .tap = o.tap } };
},
}
return true;
Expand Down Expand Up @@ -422,35 +415,6 @@ pub const Roce = struct {
}
};

/// One shared mapping holding both ranks' regions (rank 0 creates and sizes the file before anyone learns its name).
fn sharedPair(b: *Bootstrap, len: usize) ![]align(page) u8 {
var name: [64]u8 = undefined;
var name_len: u8 = 0;
var fd: std.c.fd_t = -1;
defer if (fd >= 0) {
_ = std.c.close(fd);
};
if (b.rank == 0) blk: {
const s = std.fmt.bufPrint(&name, "/dev/shm/tf-tp-roce-{d}-{x}", .{ std.c.getpid(), sock.nowNs() }) catch break :blk;
name[s.len] = 0;
fd = std.c.open(name[0..s.len :0], .{ .ACCMODE = .RDWR, .CREAT = true, .EXCL = true, .CLOEXEC = true }, @as(std.c.mode_t, 0o600));
if (fd >= 0 and std.c.ftruncate(fd, @intCast(2 * len)) == 0) name_len = @intCast(s.len);
}
// an empty name tells the other rank that rank 0 failed (it must not wait for a name that never comes)
try b.broadcast(std.mem.asBytes(&name_len), std.mem.asBytes(&name_len));
if (name_len == 0) return error.SharedMemoryUnavailable;
try b.broadcast(name[0..name_len], name[0..name_len]);
name[name_len] = 0;
if (b.rank != 0) {
fd = std.c.open(name[0..name_len :0], .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0));
if (fd < 0) return error.SharedMemoryUnavailable;
}
const m = try posix.mmap(null, 2 * len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED }, fd, 0);
try b.agree("mapped", "RoCE shm wire");
if (b.rank == 0) _ = std.c.unlink(name[0..name_len :0]);
return m;
}

fn pin(cpu: u32) void {
var set: std.os.linux.cpu_set_t = @splat(0);
set[cpu / @bitSizeOf(usize)] |= @as(usize, 1) << @intCast(cpu % @bitSizeOf(usize));
Expand Down
96 changes: 96 additions & 0 deletions zig/src/fabric/tp/roce_fake.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//! Fake verbs for host tests of VerbsWire: each QP's posted writes counted from the requests, completions that lag.
const std = @import("std");
const verbs = @import("verbs");
const abi = verbs.abi;
const rv = @import("roce_verbs.zig");

pub const slot: u64 = 1 << 20;
pub const send_off = 8192;

/// What the HCAs hold (posted, not completed); every count comes from the work requests themselves.
pub const Fake = struct {
pub var region: []const u8 = &.{};
pub var lag: u32 = 1;
pub var polls: u64 = 0;
pub var queue: [2][512]u64 = undefined;
pub var head: [2]u32 = .{ 0, 0 };
pub var len: [2]u32 = .{ 0, 0 };
pub var inflight: u64 = 0;
pub var max_inflight: u64 = 0;
pub var max_write: u64 = 0;
pub var outside: u64 = 0;
pub var bytes: u64 = 0;
pub var posts: u64 = 0;

pub fn reset(l: u32) void {
lag = l;
polls = 0;
head = .{ 0, 0 };
len = .{ 0, 0 };
inflight = 0;
max_inflight = 0;
max_write = 0;
outside = 0;
bytes = 0;
posts = 0;
}

pub fn postSend(qp: *abi.Qp, wr: *abi.SendWr, _: *?*abi.SendWr) callconv(.c) c_int {
const q = qp.qp_num;
posts += 1;
var sum: u64 = 0;
var at: ?*abi.SendWr = wr;
while (at) |w| : (at = w.next) {
if (w.send_flags & abi.send_inline == 0) {
const sge = w.sg_list[0];
const lo = @intFromPtr(region.ptr);
if (sge.addr < lo + send_off or sge.addr + sge.length > lo + send_off + 2 * slot) outside += 1;
max_write = @max(max_write, sge.length);
sum += sge.length;
}
if (w.send_flags & abi.send_signaled != 0) {
queue[q][(head[q] + len[q]) % 512] = sum;
len[q] += 1;
inflight += sum;
bytes += sum;
max_inflight = @max(max_inflight, inflight);
sum = 0;
}
}
return 0;
}

/// One completion every `lag` polls of a QP's CQ: the HCA finishes writes far slower than they are posted.
pub fn pollCq(cq: *abi.Cq, _: c_int, wc: [*]abi.Wc) callconv(.c) c_int {
const q = cq.handle;
polls += 1;
if (polls % lag != 0 or len[q] == 0) return 0;
inflight -= queue[q][head[q]];
head[q] = (head[q] + 1) % 512;
len[q] -= 1;
wc[0] = std.mem.zeroes(abi.Wc);
wc[0].status = abi.wc_success;
return 1;
}
};

/// Two fake HCAs behind one VerbsWire, connected to a peer whose region has this rank's layout.
pub const Hw = struct {
ctx: [2]abi.Context = undefined,
qp: [2]abi.Qp = undefined,
cq: [2]abi.Cq = undefined,
mr: [2]abi.Mr = undefined,

pub fn wire(hw: *Hw, region: []u8) rv.VerbsWire {
var w: rv.VerbsWire = .{ .v = undefined, .traffic_class = 0, .recv_off = send_off + 2 * slot, .flag_off = 4096, .send_off = send_off, .slot_bytes = slot, .region = region };
for (0..2) |i| {
hw.ctx[i].ops = .{ ._before_poll = @splat(null), .poll_cq = Fake.pollCq, ._before_post = @splat(null), .post_send = Fake.postSend, .post_recv = undefined, ._after = @splat(null) };
hw.qp[i].qp_num = @intCast(i);
hw.cq[i].handle = @intCast(i);
hw.mr[i].lkey = 7;
w.hcas[i] = .{ .ctx = &hw.ctx[i], .pd = undefined, .mr = &hw.mr[i], .cq = &hw.cq[i], .qp = &hw.qp[i], .gid_index = 0, .gid = @splat(0), .mtu = 0, .lid = 0 };
}
w.n_hca = 2;
return w;
}
};
63 changes: 63 additions & 0 deletions zig/src/fabric/tp/roce_guard.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Every RoCE post's bounds before the verb: each HCA's stripe inside its slot and the registered region.
const std = @import("std");

pub const max_hcas = 2;
pub const flag_stride = 128;

pub const Error = error{OutOfRegion};

/// Both ranks' region layout (roce.zig's): byte offsets and the registered length every post must stay inside.
pub const Layout = struct { send: u64, recv: u64, flags: u64, slot: u64, len: u64 };

/// One HCA's share of a post: its data stripe in this rank's send slot and the peer's receive slot, and its flag word.
pub const Stripe = struct { local: u64, remote: u64, len: u32, flag: u64 };

/// Slot `slot`'s first `padded` bytes striped over `n_hca` HCAs; error.OutOfRegion before any verb past a slot or MR.
pub fn stripes(l: Layout, n_hca: u32, slot: u32, padded: u32, out: *[max_hcas]Stripe) Error!void {
if (n_hca == 0 or n_hca > max_hcas or slot > 1) return error.OutOfRegion;
if (padded == 0 or padded % 16 != 0 or padded > l.slot) return error.OutOfRegion;
const packs = padded / 16;
var offset: u64 = 0;
for (out[0..n_hca], 0..) |*o, i| {
var stripe = packs / n_hca;
if (i < packs % n_hca) stripe += 1;
o.* = .{ .local = l.send + slot * l.slot + offset, .remote = l.recv + slot * l.slot + offset, .len = stripe * 16, .flag = l.flags + (@as(u64, slot) * max_hcas + i) * flag_stride };
if (!within(o.local, o.len, l.send + slot * l.slot, l.slot) or !within(o.remote, o.len, l.recv + slot * l.slot, l.slot)) return error.OutOfRegion;
if (!within(o.flag, 4, l.flags, l.send - l.flags) or !within(o.local, o.len, 0, l.len) or !within(o.remote, o.len, 0, l.len)) return error.OutOfRegion;
offset += o.len;
}
}

fn within(at: u64, len: u64, lo: u64, size: u64) bool {
return at >= lo and at + len <= lo + size;
}

/// roce.zig's layout for a 64 KiB slot: ctrl 4 KiB, flags 4 KiB, send [2][slot], recv [2][slot].
fn testLayout() Layout {
const slot: u64 = 64 << 10;
return .{ .send = 8192, .recv = 8192 + 2 * slot, .flags = 4096, .slot = slot, .len = 8192 + 4 * slot };
}

test "a post striped over two HCAs: the stripes tile the slot's first bytes, each flag on its own word" {
const l = testLayout();
var s: [max_hcas]Stripe = undefined;
try stripes(l, 2, 1, 48, &s);
try std.testing.expectEqual(Stripe{ .local = l.send + l.slot, .remote = l.recv + l.slot, .len = 32, .flag = l.flags + 2 * flag_stride }, s[0]);
try std.testing.expectEqual(Stripe{ .local = l.send + l.slot + 32, .remote = l.recv + l.slot + 32, .len = 16, .flag = l.flags + 3 * flag_stride }, s[1]);
try stripes(l, 2, 1, @intCast(l.slot), &s);
try std.testing.expectEqual(l.recv + 2 * l.slot, s[1].remote + s[1].len);
}

test "a post past its slot, a third slot, an unpadded or empty post, or no HCA is refused" {
const l = testLayout();
var s: [max_hcas]Stripe = undefined;
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 0, @intCast(l.slot + 16), &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 2, 16, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 0, 24, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 0, 0, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 0, 0, 16, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 3, 0, 16, &s));
var short = l;
short.len -= 16;
try std.testing.expectError(error.OutOfRegion, stripes(short, 2, 1, @intCast(l.slot), &s));
}
20 changes: 20 additions & 0 deletions zig/src/fabric/tp/roce_post_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! VerbsWire.post on fake verbs: a post that would leave its slot or region is refused before any verb.
const std = @import("std");
const fake = @import("roce_fake.zig");
const Roce = @import("roce.zig").Roce;
const Fake = fake.Fake;

test "a post past its slot is refused before any verb; a full slot posts inside the region" {
const region = try std.testing.allocator.alignedAlloc(u8, .fromByteUnits(4096), Roce.regionBytes(fake.slot));
defer std.testing.allocator.free(region);
Fake.region = region;
Fake.reset(1);
var hw: fake.Hw = .{};
var w = hw.wire(region);
try std.testing.expectError(error.OutOfRegion, w.post(1, 1, fake.slot + 16));
try std.testing.expectError(error.OutOfRegion, w.post(2, 1, 16));
try std.testing.expectEqual(@as(u64, 0), Fake.posts);
try w.post(1, 1, fake.slot);
try std.testing.expectEqual(@as(u64, 2), Fake.posts);
try std.testing.expectEqual(@as(u64, 0), Fake.outside);
}
63 changes: 63 additions & 0 deletions zig/src/fabric/tp/roce_shm.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! The RoCE protocol's shm wire: both ranks' regions in one shared mapping, posts through the guard's stripes.
const std = @import("std");
const Bootstrap = @import("bootstrap.zig").Bootstrap;
const sock = @import("sock.zig");
const guard = @import("roce_guard.zig");
const posix = std.posix;
const page = std.heap.page_size_min;

/// A test's view of each post: slot, padded bytes and the stripes the wire is about to move.
pub const PostTap = struct {
ctx: *anyopaque,
seen: *const fn (ctx: *anyopaque, l: guard.Layout, slot: u32, padded: u32, s: []const guard.Stripe) void,
};

/// The peer's region is mapped here; a post copies each stripe, then stores its flag with release order.
pub const ShmWire = struct {
peer: []u8,
layout: guard.Layout,
local: []u8,
/// Stripes a post makes, as HCAs would: the verbs wire's split, flags and bounds on one host.
stripes: u32 = 1,
tap: ?*PostTap = null,

pub fn post(w: *ShmWire, slot: u32, seq: u32, padded: u32) guard.Error!void {
var plan: [guard.max_hcas]guard.Stripe = undefined;
try guard.stripes(w.layout, w.stripes, slot, padded, &plan);
if (w.tap) |t| t.seen(t.ctx, w.layout, slot, padded, plan[0..w.stripes]);
for (plan[0..w.stripes]) |s| {
@memcpy(w.peer[s.remote..][0..s.len], w.local[s.local..][0..s.len]);
const flag: *std.atomic.Value(u32) = @ptrCast(@alignCast(w.peer[s.flag..].ptr));
flag.store(seq, .release);
}
}
};

/// One shared mapping holding both ranks' regions (rank 0 creates and sizes the file before anyone learns its name).
pub fn sharedPair(b: *Bootstrap, len: usize) ![]align(page) u8 {
var name: [64]u8 = undefined;
var name_len: u8 = 0;
var fd: std.c.fd_t = -1;
defer if (fd >= 0) {
_ = std.c.close(fd);
};
if (b.rank == 0) blk: {
const s = std.fmt.bufPrint(&name, "/dev/shm/tf-tp-roce-{d}-{x}", .{ std.c.getpid(), sock.nowNs() }) catch break :blk;
name[s.len] = 0;
fd = std.c.open(name[0..s.len :0], .{ .ACCMODE = .RDWR, .CREAT = true, .EXCL = true, .CLOEXEC = true }, @as(std.c.mode_t, 0o600));
if (fd >= 0 and std.c.ftruncate(fd, @intCast(2 * len)) == 0) name_len = @intCast(s.len);
}
// an empty name tells the other rank that rank 0 failed (it must not wait for a name that never comes)
try b.broadcast(std.mem.asBytes(&name_len), std.mem.asBytes(&name_len));
if (name_len == 0) return error.SharedMemoryUnavailable;
try b.broadcast(name[0..name_len], name[0..name_len]);
name[name_len] = 0;
if (b.rank != 0) {
fd = std.c.open(name[0..name_len :0], .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0));
if (fd < 0) return error.SharedMemoryUnavailable;
}
const m = try posix.mmap(null, 2 * len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED }, fd, 0);
try b.agree("mapped", "RoCE shm wire");
if (b.rank == 0) _ = std.c.unlink(name[0..name_len :0]);
return m;
}
Loading