Skip to content

[Store] Remove unreachable INVALID_BATCH_ID check after freeBatchID - #7

Open
stmatengss wants to merge 1 commit into
hemist/cxl-level-storefrom
fix/dead-code-mp-submit-transfer
Open

stmatengss wants to merge 1 commit into
hemist/cxl-level-storefrom
fix/dead-code-mp-submit-transfer

Conversation

@stmatengss

Copy link
Copy Markdown

Problem

In submitTransfer and mp_submitTransfer, after submission failure:

engine_.freeBatchID(batch_id);
return std::nullopt;
}

if (batch_id == INVALID_BATCH_ID) {  // ← unreachable dead code
    ...
}

batch_id is validated at function entry and cannot become INVALID_BATCH_ID between that guard and this check. The duplicate guard after the error-return path is never reached.

Fix

Remove both unreachable INVALID_BATCH_ID guards (one in submitTransfer, one in mp_submitTransfer).

Impact

  • No behavior change (guards were never triggered)
  • Eliminates misleading dead code

In submitTransfer and mp_submitTransfer, batch_id is validated once at
function entry. On submission failure the batch is freed and the function
returns immediately. A second INVALID_BATCH_ID guard placed after that
error path is unreachable dead code and should be removed.
@stmatengss
stmatengss requested review from XucSh and YiXR as code owners April 22, 2026 13:01
Bo-Vincent pushed a commit to Bo-Vincent/Mooncake that referenced this pull request Nov 2, 2026
…B contexts (kvcache-ai#5104)

* [TransferEngine] Fix URMA module lifecycle for multi-device UB contexts

urma_init()/urma_uninit() are process-global, but the UB transport paired
them wrongly: UrmaContext::deconstruct() called urma_uninit() once per
context, while UbTransport::init() called urma_init() once per transport and
~UbTransport() never released it.

With a single device the extra uninit() was harmless. With two or more devices
the first context destructor released the whole URMA module while sibling
contexts still held jetties, and the next teardown faulted:

  #0 urma_unbind_jetty ()
  openanolis#1 UrmaEndpoint::deconstruct()      urma_endpoint.cpp:680
  openanolis#2 UbSIEVEEndpointStore::destroy()  ub_context.cpp:145
  openanolis#3 UrmaContext::~UrmaContext()
  kvcache-ai#16 UbTransport::~UbTransport()

Reproduced on a Kunpeng 950 host with 10 URMA devices: any target or initiator
configured with two devices crashed on exit while the same single-device
configuration ran fine.

Let the transport own the module lifetime: drop the per-context uninit(),
record whether init() ran, make uninit() idempotent, and pair init() in the
destructor after the contexts are gone. install() failure paths already call
uninit(), so the guard keeps those from releasing the module twice.

* [TransferEngine] Do not dereference a null URMA context while logging

UbContext::doConstruct() reports a construction failure with

  LOG(INFO) << "failed construct context " << toString();

and UrmaContext::toString() calls getAsyncFd(), which dereferenced
urma_context_ unconditionally. Opening a device can fail - for example when no
configured device name matches, or the mock build is used on a host with real
URMA devices - so the failure was reported as a SIGSEGV instead of an error.

Return -1 when the context has not been created so the existing error path
survives long enough to log and return ERR_DEVICE_NOT_FOUND.

* [TransferEngine] Fix a data race in UrmaContext::retrieveRemoteSeg

retrieveRemoteSeg() is reached from every submitting thread through
UbWorkerPool::submitPostSend(), but it did its cache lookup, malloc and
push_back into remote_seg_list_ with no synchronisation. Two threads that both
miss the cache therefore push concurrently, and when the vector reallocates
both free the old buffer: AddressSanitizer reports

  ERROR: AddressSanitizer: attempting double-free
  openanolis#7 UrmaContext::retrieveRemoteSeg()   urma_endpoint.cpp:364
  openanolis#9 UbWorkerPool::submitPostSend()     ub_context.cpp:271

After the corruption a later malloc()/free() segfaults somewhere unrelated,
which is what a multi-initiator benchmark sees as a random SIGSEGV.

The same function also published the handle in remote_seg_list_ before the
import succeeded, so a failed urma_import_seg() left it for deconstruct() to
free a second time.

Guard the map and the lists with a shared_mutex: shared lock for the hot
lookup path, unique lock for publishing, with a second lookup so a thread that
lost the race drops its own import. Publish only after a successful import.

Verified with four concurrent initiators (2 read + 2 write, 2 devices,
64 KiB blocks) under ASan: before the fix 3 of 8 processes reported a
double-free, after it 8 of 8 are clean across repeated runs, and the
non-instrumented build finishes 5 mixed scenarios with no crash.

* ci: retrigger build (previous run was cancelled by a runner shutdown)

* [TransferEngine] Read the UB device NUMA node from ubcore sysfs

UbContext::socketId() built its path from /sys/class/infiniband/<dev>/device/numa_node.
URMA devices have no infiniband entry, so the lookup always failed and every
UbWorkerPool thread was pinned to node 0 via bindToSocket() regardless of where
the device actually sits.

The node is published two directories above the /sys/class/ubcore/<dev> link,
next to the ub_bus_controller directory, and is formatted as hex (0x01), so a
plain '>> int' would have truncated it to 0 even with the right path.

Resolve the link with realpath(), walk two levels up, and parse the value with
base 0 so the hex form is handled.

On a 4-node Kunpeng 950 (node 0: CPU 0-95, node 1: 96-191, node 2: 192-287,
node 3: 288-383) with udmac1d1e2, which reports numa=0x03:

  before: 3 worker threads Cpus_allowed_list=0-95
  after:  3 worker threads Cpus_allowed_list=288-383

Devices udmac0* report 0x01 and udmac1* report 0x03, so the two device groups
are now pinned to their own nodes instead of both landing on node 0.

* docs: correct the device-name comments in UbContext

The deviceName()/nicPath() comments were copied from the RDMA transport and
still said 'mlx5_3'. URMA devices are named after their ubcore entry
(udmac0d1e2, udmac1d1e6, ...) and the NIC path is '${server}:${port}@${device}',
for example 127.0.0.1:15479@udmac0d1e3.

* docs: point the mock URMA device path at ubcore sysfs

mock_urma.cpp advertised /sys/class/infiniband/mock_device, which does not
exist for any UB device. Nothing reads urma_device_t::path (listUBDevices()
builds /sys/class/ubcore/<name> from the device name), so this was dead data,
but it kept the RDMA spelling that misleads readers of the UB transport.

Verified the mock build still compiles and passes:
  cmake .. -DUSE_UB=ON -DURMA_LIBRARY= ...   # 'building ub_transport with Mock URMA library'
  ./ub_transport_test --metadata_server=P2PHANDSHAKE ...
  [  PASSED  ] 2 tests.

* docs: call the UB endpoint a device, not an RNIC

Two diagnostics in the UB worker pool reported problems on a 'local RNIC'.
UB has no NIC; the value logged next to the message is nicPath(), i.e.
<host>:<port>@<ubep device>, so 'local UB device' describes it accurately.

The '<Transport>: No available RNIC' messages in rdma/barex/ub are left
untouched: that sentence is deliberately shared across transports.

Both builds still compile and the mock UB test passes:
  make -j64 transfer_engine_bench        -> 0 errors
  make -j64 ub_transport_test (mock)     -> 0 errors
  ./ub_transport_test                    -> [  PASSED  ] 2 tests
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.

1 participant