randomkang commented on code in PR #3144:
URL: https://github.com/apache/brpc/pull/3144#discussion_r4513109376


##########
src/brpc/rdma/rdma_endpoint.cpp:
##########
@@ -1133,26 +1148,94 @@ int RdmaEndpoint::DoPostRecv(void* block, size_t 
block_size) {
     return 0;
 }
 
+int RdmaEndpoint::DoPostRecvGDR(void* block, size_t block_size, uint32_t lkey) 
{
+    ibv_recv_wr wr;
+    memset(&wr, 0, sizeof(wr));
+    ibv_sge sge;
+    sge.addr = (uint64_t)block;
+    sge.length = block_size;
+    sge.lkey = lkey;
+    wr.num_sge = 1;
+    wr.sg_list = &sge;
+    //LOG(INFO) << "POST recv: addr=0x" << std::hex << sge.addr
+    //      << std::dec  << " length=0x" << sge.length
+    //      << " lkey=0x" << sge.lkey;
+    //LOG(INFO) << block << " " << _device_allocator->get_lkey();
+    ibv_recv_wr* bad = NULL;
+    int err = ibv_post_recv(_resource->qp, &wr, &bad);
+    if (err != 0) {
+        LOG(WARNING) << "Fail to ibv_post_recv: " << berror(err);
+        return -1;
+    }
+    return 0;
+}
+
 int RdmaEndpoint::PostRecv(uint32_t num, bool zerocopy) {
     // We do the post repeatedly from the _rbuf[_rq_received].
     while (num > 0) {
+#if BRPC_WITH_GDR
+        uint32_t lkey = 0;
+#endif  // if BRPC_WITH_GDR
         if (zerocopy) {
             _rbuf[_rq_received].clear();
-            butil::IOBufAsZeroCopyOutputStream os(&_rbuf[_rq_received],
-                    g_rdma_recv_block_size + IOBUF_BLOCK_HEADER_LEN);
-            int size = 0;
-            if (!os.Next(&_rbuf_data[_rq_received], &size)) {
-                // Memory is not enough for preparing a block
-                PLOG(WARNING) << "Fail to allocate rbuf";
-                return -1;
-            } else {
-                CHECK_EQ(static_cast<uint32_t>(size), g_rdma_recv_block_size);
+
+#if BRPC_WITH_GDR
+            if (_use_gdr) {
+                butil::gdr::BlockPoolAllocator* device_allocator = 
butil::gdr::BlockPoolAllocators::GetInstance()->get_gpu_allocator();
+                void* device_ptr = 
device_allocator->AllocateRaw(g_gdr_recv_block_size);
+                if (device_ptr == nullptr) {
+                    // GPU pool exhausted; abort this post, the caller will 
retry.
+                    PLOG(WARNING) << "Fail to allocate gpu rbuf";
+                    return -1;
+                }
+                auto deleter = [device_allocator](void* data) { 
device_allocator->DeallocateRaw(data); };
+                lkey = device_allocator->get_lkey(device_ptr);
+                // Tag the block as GPU memory so that IOBuf::is_gpu_memory()
+                // works deterministically. The lkey is carried via `data_meta'
+                // so the recv path can locate the correct memory region key.
+                if (_rbuf[_rq_received].append_user_data_gpu(
+                        device_ptr, g_gdr_recv_block_size, deleter, lkey) != 
0) {
+                    // Failed to attach the GPU block to the IOBuf (e.g. the
+                    // IOBuf metadata block allocation failed). Release the
+                    // device block back to the pool and abort this post; the
+                    // caller will retry. Publishing _rbuf_data without a
+                    // successful append would leave the posted block ownerless
+                    // and silently drop the receive on completion.
+                    PLOG(WARNING) << "Fail to append gpu rbuf to IOBuf";
+                    device_allocator->DeallocateRaw(device_ptr);
+                    return -1;
+                }
+                _rbuf_data[_rq_received] = device_ptr;
+            } else
+#endif  // if BRPC_WITH_GDR
+            {
+                butil::IOBufAsZeroCopyOutputStream os(&_rbuf[_rq_received],
+                        g_rdma_recv_block_size + IOBUF_BLOCK_HEADER_LEN);
+                int size = 0;
+                if (!os.Next(&_rbuf_data[_rq_received], &size)) {
+                    // Memory is not enough for preparing a block
+                    PLOG(WARNING) << "Fail to allocate rbuf";
+                    return -1;
+                } else {
+                    CHECK_EQ(static_cast<uint32_t>(size), 
g_rdma_recv_block_size);
+                }
             }
         }
-        if (DoPostRecv(_rbuf_data[_rq_received], g_rdma_recv_block_size) < 0) {
-            _rbuf[_rq_received].clear();
-            return -1;
+#if BRPC_WITH_GDR
+        if (_use_gdr) {
+            if (DoPostRecvGDR(_rbuf_data[_rq_received], g_gdr_recv_block_size, 
lkey) < 0) {

Review Comment:
   Because the rdma_recv_zerocopy is true at default, this issue has not been 
exposed. When wc.byte_len is 0, the zerocopy also should be true when gdr is 
open. Code has been updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to