[Feat] (Remote): give inproc T0 the server's own store and never an AHB

This commit is contained in:
2026-10-10 22:32:19 -04:00
parent 5f9c3d6947
commit 745f59bc3f
18 changed files with 302 additions and 30 deletions
+65 -4
View File
@@ -44,6 +44,7 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <mutex>
@@ -3011,11 +3012,26 @@ namespace MobileGL::MG_Remote::Client {
Bool ClientSession::AdoptT0() {
if (!m_adoptT0Asked) return false;
if (m_adoptT0Decision >= 0) return m_adoptT0Decision == 1;
if (Caps().HasCap(MG_Pipe::kCapAdoptT0)) {
const Bool peerIsThisProcess = m_transport != nullptr &&
Transport::AdoptT0::PeerIsThisProcess(m_transport->Role() == Transport::TransportRole::InProcess);
if (Caps().HasCap(MG_Pipe::kCapAdoptT0) && peerIsThisProcess &&
!Caps().HasCap(MG_Pipe::kCapAdoptInProcess)) {
// A server in this process offered the AHardwareBuffer arm. The stores are this
// process's memory already; an in-process session never allocates one.
m_adoptT0Decision = 0;
++m_t0Fallbacks;
MGLOG_W("MG_Remote client: Refuse{AdoptT0Unavailable, \"in process\"} - the server is this process "
"and published kCapAdoptT0 without kCapAdoptInProcess; the session runs T2");
} else if (Caps().HasCap(MG_Pipe::kCapAdoptT0)) {
m_adoptT0Decision = 1;
MGLOG_I("MG_Remote client: T0 - the server published kCapAdoptT0 (its POST self-test of the "
"sustained-lock pattern passed), so this session adopts persistent stores as "
"AHardwareBuffers the server imports");
if (Caps().HasCap(MG_Pipe::kCapAdoptInProcess)) {
MGLOG_I("MG_Remote client: T0 in process - the server is this process, so this session's "
"persistent maps are the server's own store mappings (no AHardwareBuffer)");
} else {
MGLOG_I("MG_Remote client: T0 - the server published kCapAdoptT0 (its POST self-test of the "
"sustained-lock pattern passed), so this session adopts persistent stores as "
"AHardwareBuffers the server imports");
}
} else {
m_adoptT0Decision = 0;
++m_t0Fallbacks;
@@ -3036,6 +3052,7 @@ namespace MobileGL::MG_Remote::Client {
void* ClientSession::AdoptPersistentT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 size,
const void* seed) {
using namespace Transport::AdoptT0;
if (Caps().HasCap(MG_Pipe::kCapAdoptInProcess)) return AdoptPersistentInProcess(handle, size, seed);
// Everything that can fail runs BEFORE the record is encoded, so the record never waits on
// a step that has not happened: either the Offer carries the handle or it says it does not.
HeldStore store;
@@ -3141,6 +3158,50 @@ namespace MobileGL::MG_Remote::Client {
return nullptr;
}
// T0 IN ONE PROCESS (kCapAdoptInProcess): the OK answer carries the address of the server's own
// mapping of the store, which becomes this buffer's persistent map - the memory both sides
// already share, as monolith's donation is. The server has drained the GPU's use of the store
// before answering and applies nothing of this client's until this call returns, so the
// shadow is copied in here, by the side that owns it.
void* ClientSession::AdoptPersistentInProcess(const MG_Pipe::MGPHandleOnly& handle, Uint64 size,
const void* seed) {
Uint64 base = 0;
Uint64 returned = 0;
Int32 status = Wire::ReplySink::kStatusError;
EmitAndWait(MG_Pipe::MGPWireOp::MapPersistent, &handle, sizeof(handle), nullptr, 0, &base, sizeof(base),
&status, &returned);
if (status == Wire::ReplySink::kStatusError) {
SessionFail(MGFatalFamily::ReplyError,
"MGPipe: Fatal{ReplyError, \"map_persistent\"} - the in-process T0 row answered ERROR, "
"which is not an acceptance answer");
}
if (status != Wire::ReplySink::kStatusOk) {
++m_t0Declined;
return nullptr;
}
if (returned != sizeof(base) || base == 0) {
SessionFail(MGFatalFamily::UnexpectedMapAccept,
"MGPipe: Fatal{UnexpectedMapAccept, \"map_persistent\"} - the server accepted an "
"in-process T0 store without the store's address");
}
void* ptr = reinterpret_cast<void*>(static_cast<std::uintptr_t>(base));
if (seed != nullptr) std::memcpy(ptr, seed, static_cast<SizeT>(size));
++m_t0Imported;
m_t0Bytes += size;
// No AHardwareBuffer to hold: the entry only marks the handle T0 (its release is a no-op).
const Uint64 key = T0Key(handle.Handle);
if (auto it = m_t0Stores.find(key); it != m_t0Stores.end()) {
Transport::AdoptT0::ReleaseHeld(it->second);
++m_t0Released;
}
m_t0Stores[key] = Transport::AdoptT0::HeldStore{nullptr, ptr, size};
MGLOG_D("MG_Remote client: T0 store {slot=%u, gen=%u} donated in process - %llu bytes; session total "
"%llu stores / %llu bytes",
handle.Handle.Slot, handle.Handle.Gen, static_cast<unsigned long long>(size),
static_cast<unsigned long long>(m_t0Imported), static_cast<unsigned long long>(m_t0Bytes));
return ptr;
}
Bool ClientSession::EmitSharedImage(const MG_Pipe::MGPSharedImageOp& op, int sendFd,
MG_Pipe::MGPSharedImageReply* reply, int* receivedFd) {
if (receivedFd != nullptr) *receivedFd = -1;
@@ -293,6 +293,7 @@ namespace MobileGL::MG_Remote::Client {
// server's inbox relies on), and wait for the answer. The held pointer on OK - the caller
// adopts it as the store's mapping - or null on DECLINED (the store stays T2).
void* AdoptPersistentT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 size, const void* seed);
void* AdoptPersistentInProcess(const MG_Pipe::MGPHandleOnly& handle, Uint64 size, const void* seed);
// shared_image (docs/Disaggregated/notes/anland/plan-ahb-dmabuf.md): emits `op` and waits
// for its answer. `sendFd` >= 0 (an Import) is queued on the aux socket for the record's
// seq BEFORE the record is published; `receivedFd` non-null (an Allocate) takes the
+3 -2
View File
@@ -116,9 +116,10 @@ namespace MobileGL::MG_Remote::Server {
return it->second;
}
Bool ServerVerbSink::OnMapPersistent(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status) {
Bool ServerVerbSink::OnMapPersistent(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status,
Uint64& inProcessBase) {
ServerSession* session = ServerSession::Active();
return session != nullptr && session->AdoptStoreT0(handle, seq, status);
return session != nullptr && session->AdoptStoreT0(handle, seq, status, inProcessBase);
}
Bool ServerVerbSink::OnFenceCreate(const MG_Pipe::MGPHandleOnly& desc) {
+2 -1
View File
@@ -156,7 +156,8 @@ namespace MobileGL::MG_Remote::Server {
// queries, present, the T0 map, the control records and shared images.
// P11 B2: T0's map_persistent - ServerSession::AdoptStoreT0 (it owns the aux socket).
Bool OnMapPersistent(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status) override;
Bool OnMapPersistent(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status,
Uint64& inProcessBase) override;
Bool OnFenceCreate(const MG_Pipe::MGPHandleOnly&) override;
Bool OnFenceDestroy(const MG_Pipe::MGPHandleOnly&) override;
Bool OnFenceStatus(const MG_Pipe::MGPHandleOnly&, Uint32&) override;
+49 -4
View File
@@ -30,6 +30,7 @@
#include <atomic>
#include <string>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <thread>
@@ -726,6 +727,7 @@ namespace MobileGL::MG_Remote::Server {
m_adoptT0Quiet = m_adoptAsk == Transport::kAdoptAskT0Default;
m_adoptT0Settled = false;
m_adoptT0 = false;
m_adoptT0InProcess = false;
m_t0Stores = m_t0Bytes = m_t0Declined = m_t0Refusals = 0;
m_adoptInbox.Clear();
m_adoptInbox.Attach(&transport);
@@ -966,6 +968,7 @@ namespace MobileGL::MG_Remote::Server {
// P11 B2: this session's T0 grant, never part of the process-wide m_capBits - it is a
// statement about THIS client's ask and this server's self-test.
if (m_adoptT0) callMask |= static_cast<Uint64>(MG_Pipe::kCapAdoptT0);
if (m_adoptT0InProcess) callMask |= static_cast<Uint64>(MG_Pipe::kCapAdoptInProcess);
const auto timerSupported = m_backend->GetBackendFunctions().GL.IsTimerQuerySupported;
if ((callMask & MG_Pipe::kCapTimerQuery) && (!timerSupported || !timerSupported()))
callMask &= ~static_cast<Uint64>(MG_Pipe::kCapTimerQuery);
@@ -1113,7 +1116,7 @@ namespace MobileGL::MG_Remote::Server {
m_adoptInbox.Clear();
m_adoptInbox.Attach(nullptr);
m_sharedImages.Clear();
m_adoptT0Asked = m_adoptT0Settled = m_adoptT0 = m_adoptT0Quiet = false;
m_adoptT0Asked = m_adoptT0Settled = m_adoptT0 = m_adoptT0Quiet = m_adoptT0InProcess = false;
m_adoptAsk = 2;
m_consumer.Detach();
*m_commands = Transport::RingConsumer();
@@ -1153,7 +1156,27 @@ namespace MobileGL::MG_Remote::Server {
const char* backendName = m_backend == nullptr ? "server"
: m_backend->GetBackendType() == BackendType::DirectVulkan ? "Magma" : "Espryt";
std::string why;
if (MG_Config::Ipc.AllowAdoptT0 == 0) {
// A CLIENT IN THIS PROCESS SHARES THE SERVER'S MEMORY ALREADY: its persistent maps are the
// stores' own mappings, donated as monolith donates them, and no AHardwareBuffer, import
// or POST is involved. A backend that cannot donate leaves the session on T2 - an
// in-process session never takes the AHardwareBuffer arm below.
const Bool inProcess = m_transport != nullptr &&
Transport::AdoptT0::PeerIsThisProcess(m_transport->Role() == Transport::TransportRole::InProcess);
if (inProcess) {
const MG_Pipe::MGPipeResourceOps* ops = MG_Pipe::MGPipeGetResourceOps();
if (MG_Config::Ipc.AllowAdoptT0 == 0) {
why = "this server's MOBILEGL_IPC_ALLOW_ADOPT_T0=0 disallows it";
} else if (ops == nullptr || ops->DonateInProcess == nullptr) {
why = std::string("the ") + backendName + " backend cannot hand its stores to a client in this process";
} else {
m_adoptT0 = true;
m_adoptT0InProcess = true;
MGLOG_I("MG_Remote server: T0 granted in process - the client is this process, so each "
"map_persistent hands it the %s store's own mapping (no AHardwareBuffer)",
backendName);
return;
}
} else if (MG_Config::Ipc.AllowAdoptT0 == 0) {
why = "this server's MOBILEGL_IPC_ALLOW_ADOPT_T0=0 disallows it";
} else if (!Transport::AdoptT0::PlatformHasAhb()) {
why = "no AHardwareBuffer on this platform";
@@ -1182,7 +1205,9 @@ namespace MobileGL::MG_Remote::Server {
if (!why.empty()) {
++m_t0Refusals;
const char* word = MG_Config::Ipc.AllowAdoptT0 == 0 ? "disallowed"
: !Transport::AdoptT0::PlatformHasAhb() ? "no AHardwareBuffer" : "POST";
: inProcess ? "in process"
: !Transport::AdoptT0::PlatformHasAhb() ? "no AHardwareBuffer"
: "POST";
if (m_adoptT0Quiet) {
// THE DEFAULT ASK (MOBILEGL_IPC_ADOPT_TIER unset) FALLS BACK QUIETLY: every host
// session would otherwise carry this line. Counted (T0Refusals, the totals line).
@@ -1202,9 +1227,29 @@ namespace MobileGL::MG_Remote::Server {
backendName);
}
Bool ServerSession::AdoptStoreT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status) {
Bool ServerSession::AdoptStoreT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status,
Uint64& inProcessBase) {
if (!m_adoptT0) return false;
status = Wire::ReplySink::kStatusDeclined;
inProcessBase = 0;
if (m_adoptT0InProcess) {
void* base = MG_Pipe::MGPipeApplyDonateInProcess(handle);
if (base == nullptr) {
++m_t0Declined;
MGLOG_W("MG_Remote server: T0 map_persistent {slot=%u, gen=%u} (record %llu) DECLINED - the "
"backend did not donate the store (its log names why); the store runs T2",
handle.Handle.Slot, handle.Handle.Gen, static_cast<unsigned long long>(seq));
return true;
}
inProcessBase = static_cast<Uint64>(reinterpret_cast<std::uintptr_t>(base));
++m_t0Stores;
status = Wire::ReplySink::kStatusOk;
MGLOG_D("MG_Remote server: T0 donated store {slot=%u, gen=%u} in process (record %llu); session "
"total %llu stores",
handle.Handle.Slot, handle.Handle.Gen, static_cast<unsigned long long>(seq),
static_cast<unsigned long long>(m_t0Stores));
return true;
}
int hop = -1;
Transport::AdoptT0::Offer offer{};
std::string why;
+7 -1
View File
@@ -320,7 +320,12 @@ namespace MobileGL::MG_Remote::Server {
// hang), read the AHB off its hop, and import it as `handle`'s store. False = this session is
// not T0 (the codec's T2 decline answers); true = handled and `status` is the answer (OK, or
// DECLINED with a named line).
Bool AdoptStoreT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status);
// A client in this process (inproc) takes no AHardwareBuffer: the backend donates the
// store's own mapping and `inProcessBase` carries its address in the OK answer.
Bool AdoptStoreT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status,
Uint64& inProcessBase);
// T0 granted for a client in this process: map_persistent donates (kCapAdoptInProcess).
Bool AdoptT0InProcess() const { return m_adoptT0InProcess; }
// The arm's proof for this session (B2): stores imported and their bytes, declined.
Uint64 T0StoresImported() const { return m_t0Stores; }
Uint64 T0BytesImported() const { return m_t0Bytes; }
@@ -426,6 +431,7 @@ namespace MobileGL::MG_Remote::Server {
Uint8 m_adoptAsk = 2;
Bool m_adoptT0Settled = false;
Bool m_adoptT0 = false;
Bool m_adoptT0InProcess = false;
AdoptInbox m_adoptInbox;
SharedImages::SessionHolder m_sharedImages;
Uint64 m_t0Stores = 0;
+12
View File
@@ -184,6 +184,18 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
return test != nullptr ? test->HasAhb() : RealHasAhb();
}
namespace {
std::atomic<bool> g_peerInAnotherProcessForTest{false};
} // namespace
bool PeerIsThisProcess(bool inProcessTransport) {
return inProcessTransport && !g_peerInAnotherProcessForTest.load(std::memory_order_acquire);
}
void SetPeerInAnotherProcessForTest(bool asIfCrossProcess) {
g_peerInAnotherProcessForTest.store(asIfCrossProcess, std::memory_order_release);
}
bool AllocateHeld(std::uint64_t size, const void* seed, HeldStore& out, std::string& why) {
const PlatformForTest* test = g_testPlatform.load(std::memory_order_acquire);
return test != nullptr ? test->AllocateHeld(size, seed, out, why) : RealAllocateHeld(size, seed, out, why);
+8
View File
@@ -56,6 +56,14 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
// True where AHardwareBuffer exists (Android). Everywhere else T0 is never granted.
bool PlatformHasAhb();
// True when a session's peer is this very process (an in-process transport). Such a session
// never takes this header's arm: its stores are this process's memory already, so T0 there is
// the backend handing over its own store (kCapAdoptInProcess) or nothing (T2).
bool PeerIsThisProcess(bool inProcessTransport);
// Test seam: the protocol tests run over an in-process transport and stand for a peer in
// another process while this is set.
void SetPeerInAnotherProcessForTest(bool asIfCrossProcess);
// ---- client half ----------------------------------------------------------------------
// A store the client allocated. `ahb` is an AHardwareBuffer*, `ptr` its CPU lock, held until
+4 -2
View File
@@ -1852,9 +1852,11 @@ namespace MobileGL::MG_Remote::Wire {
// it (Server/AdoptInbox.h says why that wait cannot hang the apply thread); it answers
// for itself or says "not T0", and everything below is the T2 arm, unchanged.
Int32 t0Status = ReplySink::kStatusDeclined;
Uint64 inProcessBase = 0;
if (m_verbs != nullptr &&
m_verbs->OnMapPersistent(*static_cast<const MGPHandleOnly*>(payload), seq, t0Status)) {
PostReply(op, seq, t0Status, nullptr, 0);
m_verbs->OnMapPersistent(*static_cast<const MGPHandleOnly*>(payload), seq, t0Status, inProcessBase)) {
if (inProcessBase != 0) PostReply(op, seq, t0Status, &inProcessBase, sizeof(inProcessBase));
else PostReply(op, seq, t0Status, nullptr, 0);
return true;
}
}