Open
pid add #4992
life-hanAdd on Sep 1
pid add #4992
Open
life-hanAdd on Sep 1
Sep 1

描述

变更类型

请选择本次引入的变更类型:

关联的Issue

测试

已完成的测试用例和场景:
1.
2.

补充的UT用例:

文档更新

合入检查

likedislike
Merge blocked
Llife-han
Sep 1 Created pull request, commit a6a67901
atomgit-bot
atomgit-bot
Sep 1 Comments:

变更摘要

本 PR(pid add)为 RoCE 远端内存管理引入进程 PID 维度,以区分同一网卡(EndpointDesc)上来自不同远端进程的内存导入。核心改动位于 experimental/base_comm/comm_mem/roce_mem.cc 与 experimental/base_comm/comm_mem/roce_mem.h:在 RoceRegedMemMgr::GetMemDesc 生成的内存描述符末尾追加本进程 getpid();GetParamsFromMemDesc 新增 uint32_t& pid 出参并从描述符末尾解析 PID;同时在头文件中新增 RemoteRdmaMemKey 结构体及其相等比较与 FNV-1a 哈希函数,将 remoteRdmaRmaBufferMgrs_ 的键由 EndpointDesc 改为 (EndpointDesc, pid) 组合键,使 MemoryImport/MemoryUnimport 能区分同一网卡上的不同远端进程。

主要改动

  • 内存描述符追加并解析 PID: GetMemDesc 通过 getpid() 将本进程 PID 以字节形式追加到 tempLocalMemDesc 尾部;GetParamsFromMemDesc 增加 uint32_t& pid 参数,从描述符末尾提取 PID,并将长度校验与 EndpointDesc 提取偏移统一调整为 tailLen = sizeof(EndpointDesc) + sizeof(uint32_t)。
  • 新增组合键 RemoteRdmaMemKey: 头文件中定义包含 EndpointDesc endpointDesc 与 uint32_t pid 的结构体,提供基于 std::memcmp 的 operator==,并实现 64/32 位适配的 FNV-1a 哈希仿函数 RemoteRdmaMemKeyHash。
  • 远端内存管理器改以组合键索引: remoteRdmaRmaBufferMgrs_ 由 std::unordered_map<EndpointDesc, ...> 改为 std::unordered_map<RemoteRdmaMemKey, std::unique_ptr<RemoteRdmaRmaBufferMgr>, RemoteRdmaMemKeyHash>;MemoryImport 与 MemoryUnimport 均以 RemoteRdmaMemKey{endpointDesc, remotePid} 进行查找、插入与删除,MemoryImport 中新增打印远端 PID 的 HCCL_INFO 日志。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 1 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 1 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 1 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
experimental ❌ (0/2)(You can also ask: 欧阳茜, 施秉辰, 胡林康, 林叶峰, 杨邵华) ❌ (0/1)(You can also ask: 程祥乐, 颜业峰, 文学敏)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

hanjf, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
92 messages are collapsed here。 See more
严正行
严正行CollaboratorsSep 25Start code review1
experimental/base_comm/comm_mem/roce_mem.h
@@ -28,0 +31,4 @@
31+ */
32+struct RemoteRdmaMemKey {
33+ EndpointDesc endpointDesc{};
34+ uint32_t pid{0};
严正行
严正行Sep 25Comments:

[MEDIUM] pid 作为远端进程唯一标识存在复用漂移风险,重启进程可能误拒导入或误删他人内存

第 34 行:uint32_t pid{0};

问题分析:RemoteRdmaMemKey 以 (endpointDesc, pid) 区分远端进程,pid 取自导出方 getpid()(roce_mem.cc:146)。Linux pid 是可复用的滚动编号:远端进程异常退出且未走 MemoryUnimport 时,本端 remoteRdmaRmaBufferMgrs_ 残留 key=(ep, pid) 条目;若新进程重启后拿到相同 pid 且 mmap 到与旧进程相同的地址区间(同二进制多次运行 VA 高度稳定),MemoryImport 中 Add(tempKey, ...) 会命中残留条目返回 HCCL_E_AGAIN,合法导入被误拒;反之框架延迟补发的 Unimport 会把新进程刚导入的同地址条目误删。容器/PID namespace 场景下不同远端容器进程 pid 撞号的概率进一步放大。

修复建议:

  1. 优先方案:导出侧改用进程生命周期内单调唯一且跨重启不重复的标识,如 pid + 进程启动时间戳(读 /proc/self/stat 第 22 字段),或直接复用通信域已有的全局唯一 ctx/comm id;
  2. 过渡方案:至少在 MemoryImport 的 Add 返回 false(HCCL_E_AGAIN 路径)时校验已存条目的 rkey 与本次 dto.rkey 是否一致(ExchangeRdmaBufferDto 含 rkey 字段),不一致说明是 pid 复用后的残留条目,应先清理旧条目再重新 Add,并在日志中告警;
  3. 若维持现状,须在 RemoteRdmaMemKey 的注释中明确记录「pid 复用场景下条目可能误命中」的边界假设,供部署评估。
likedislike
严正行
严正行CollaboratorsSep 25Start code review1
experimental/base_comm/comm_mem/roce_mem.cc
@@ -172,30 +179,30 @@
172- sizeof(EndpointDesc));
179+ tailLen);
173180 return HCCL_E_INTERNAL;
174181 }
182+ 
183+ // 从memDesc末尾提取pid
184+ if (memcpy_s(&pid, sizeof(uint32_t), description + descLen - sizeof(uint32_t), sizeof(uint32_t)) != EOK) {
185+ HCCL_ERROR(
186+ "[RoceRegedMemMgr][GetParamsFromMemDesc] [%s] pid copy error. aim size:[%llu]", __func__, sizeof(uint32_t));
187+ return HCCL_E_INTERNAL;
188+ }
189+ 
175190 // 从memDesc末尾提取EndpointDesc
176- if (memcpy_s(
177- &endpointDesc, sizeof(EndpointDesc), description + descLen - sizeof(EndpointDesc), sizeof(EndpointDesc))
178- != EOK) {
191+ if (memcpy_s(&endpointDesc, sizeof(EndpointDesc), description + descLen - tailLen, sizeof(EndpointDesc)) != EOK) {
179192 HCCL_ERROR(
180193 "[RoceRegedMemMgr][GetParamsFromMemDesc] [%s] endpointDesc copy error. aim size:[%llu]", __func__,
181194 sizeof(EndpointDesc));
@@ -185,7 +198,7 @@ HcclResult RoceRegedMemMgr::GetParamsFromMemDesc(
185198 // 反序列化
186199 std::vector<char> tempDesc{};
187200 tempDesc.resize(TRANSPORT_EMD_ESC_SIZE);
188- tempDesc.assign(description, description + descLen - sizeof(EndpointDesc));
201+ tempDesc.assign(description, description + descLen - tailLen);
189202 Hccl::BinaryStream remoteRdmaRmaBufferStream(tempDesc);
190203 dto.Deserialize(remoteRdmaRmaBufferStream);
191204 return HCCL_SUCCESS;
@@ -194,27 +207,30 @@ HcclResult RoceRegedMemMgr::GetParamsFromMemDesc(
194207HcclResult RoceRegedMemMgr::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)
严正行
严正行Sep 25Comments:

[MEDIUM] key 维度扩展、加锁、空 Mgr 清理等行为变更无任何 UT 覆盖

第 207 行:HcclResult RoceRegedMemMgr::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)

问题分析:本 PR 引入三处行为变更——①remoteRdmaRmaBufferMgrs_ 的 key 从 EndpointDesc 扩展为 (EndpointDesc, pid);②MemoryImport/MemoryUnimport 新增 memMtx_ 互斥保护;③MemoryUnimport 在 Del 后对空 RemoteRdmaRmaBufferMgr 做 erase 清理(roce_mem.cc:273-275)。但 git diff 显示 test/ 目录零变更,PR 模板「补充的UT用例」栏也是空的。experimental/base_comm 参与编译链接(cpu_roce_endpoint.cc 直接构造 RoceRegedMemMgr),上述逻辑的正确性(尤其 Import/Unimport 的 key 对称性与清理时机)完全依赖人工验证,后续重构无回归防护。

修复建议:补充 UT 用例(建议放 test/ut/framework/next/comms/endpoints/reged_mems/ 下,仿 ut_roce_mem_test.cc 的 mockcpp 模式,对 experimental 版另建文件):

  1. 同 endpointDesc、不同 pid 的两次 MemoryImport,断言 remoteRdmaRmaBufferMgrs_ 生成两个独立 Mgr 条目、互不影响 Add/Del;
  2. MemoryUnimport 把某 Mgr 的最后一个条目删空后,断言该 remoteKey 从 map 中被 erase(size 变化用 #define private public 方式或新增计数接口断言);
  3. descLen 边界:descLen < sizeof(EndpointDesc)+sizeof(uint32_t) 时 GetParamsFromMemDesc 返回 HCCL_E_INTERNAL;
  4. memDesc 末 4 字节 pid 与 EndpointDesc 的拼接顺序往返一致性(GetMemDesc 生成 → GetParamsFromMemDesc 解析还原同一 pid 与 endpointDesc)。
likedislike
LeewisCollaborators
16 days ago Comments:

您好,当前 PR 已超过 30 天未更新。如暂无合入需要,请及时关闭 PR;如仍有需要,请及时更新处理并推动检视合入。若 5 天内仍无更新,该 PR 将被自动关闭。

likedislike
LLeewisCollaborators
16 days ago Added label: stale
LLeewisCollaborators
11 days ago Removed label: stale