From fc5256fd28c80eceef85758a63260c1ef0cf189e Mon Sep 17 00:00:00 2001 From: Sarbojit2019 <52527887+SarbojitAMD@users.noreply.github.com> Date: Thu, 13 Feb 2020 14:22:11 +0530 Subject: [PATCH] ihipEnablePeerAccess return error if peer is not accessible (#1858) hipDeviceEnablePeerAccess returns success and adds peer into the list even if it is not accessible which creates problem in hipMalloc when it tries to share the ptr to peer device. Proposed change is to check the access status before updating the peer list and update only when it can access the peer. --- src/hip_peer.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/hip_peer.cpp b/src/hip_peer.cpp index 7781af1dbe..8fd66a52bb 100644 --- a/src/hip_peer.cpp +++ b/src/hip_peer.cpp @@ -128,17 +128,24 @@ hipError_t ihipEnablePeerAccess(TlsData* tls, hipCtx_t peerCtx, unsigned int fla if (thisCtx == peerCtx) { err = hipErrorInvalidDevice; // Can't enable peer access to self. } else if ((thisCtx != NULL) && (peerCtx != NULL)) { - LockedAccessor_CtxCrit_t peerCrit(peerCtx->criticalData()); - // Add thisCtx to peerCtx's access list so that new allocations on peer will be made - // visible to this device: - bool isNewPeer = peerCrit->addPeerWatcher(peerCtx, thisCtx); - if (isNewPeer) { - tprintf(DB_MEM, "device=%s can now see all memory allocated on peer=%s\n", - thisCtx->toString().c_str(), peerCtx->toString().c_str()); - am_memtracker_update_peers(peerCtx->getDevice()->_acc, peerCrit->peerCnt(), - peerCrit->peerAgents()); + + int canAccess = 0; + if ((hipSuccess != ihipDeviceCanAccessPeer(&canAccess,thisCtx,peerCtx)) || (canAccess == 0)){ + tprintf(DB_MEM, "device=%s can't access peer=%s\n",thisCtx->toString().c_str(), peerCtx->toString().c_str()); + err = hipErrorInvalidDevice; } else { - err = hipErrorPeerAccessAlreadyEnabled; + LockedAccessor_CtxCrit_t peerCrit(peerCtx->criticalData()); + // Add thisCtx to peerCtx's access list so that new allocations on peer will be made + // visible to this device: + bool isNewPeer = peerCrit->addPeerWatcher(peerCtx, thisCtx); + if (isNewPeer) { + tprintf(DB_MEM, "device=%s can now see all memory allocated on peer=%s\n", + thisCtx->toString().c_str(), peerCtx->toString().c_str()); + am_memtracker_update_peers(peerCtx->getDevice()->_acc, peerCrit->peerCnt(), + peerCrit->peerAgents()); + } else { + err = hipErrorPeerAccessAlreadyEnabled; + } } } else { err = hipErrorInvalidDevice;