Repository navigation
[Bugfix] clean up partially initialized TENT RDMA contexts #3325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1a2f5b2
f7d193b
ceacf69
0cb9456
1ce338d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,6 +283,8 @@ static inline const std::string statusToString( | |
| return "DEVICE_ENABLED"; | ||
| case RdmaContext::DEVICE_DISABLED: | ||
| return "DEVICE_DISABLED"; | ||
| case RdmaContext::DEVICE_INITIALIZING: | ||
| return "DEVICE_INITIALIZING"; | ||
| case RdmaContext::DEVICE_PAUSED: | ||
| return "DEVICE_PAUSED"; | ||
| } | ||
|
|
@@ -302,7 +304,15 @@ RdmaContext::RdmaContext(RdmaTransport& transport) | |
| } | ||
|
|
||
| RdmaContext::~RdmaContext() { | ||
| if (status_ != DEVICE_UNINIT) disable(); | ||
| if (status_ == DEVICE_INITIALIZING) { | ||
| rollbackEnable(); | ||
| return; | ||
| } | ||
| if (status_ != DEVICE_UNINIT || native_context_ || native_pd_ || | ||
| event_fd_ >= 0 || !comp_channel_.empty() || !cq_list_.empty() || | ||
| notify_cq_) { | ||
| disable(); | ||
| } | ||
| } | ||
|
|
||
| int RdmaContext::construct(const std::string& device_name, | ||
|
|
@@ -320,34 +330,40 @@ int RdmaContext::construct(const std::string& device_name, | |
| } | ||
|
|
||
| int RdmaContext::enable() { | ||
| if (status_ != DEVICE_DISABLED) { | ||
| LOG(WARNING) << "RDMA context " << name() << " has been enabled"; | ||
| return 0; | ||
| DeviceStatus expected = DEVICE_DISABLED; | ||
| if (!status_.compare_exchange_strong(expected, DEVICE_INITIALIZING)) { | ||
| if (expected == DEVICE_ENABLED || expected == DEVICE_PAUSED) { | ||
| LOG(WARNING) << "RDMA context " << name() << " has been enabled"; | ||
| return 0; | ||
| } | ||
| LOG(ERROR) << "RDMA context " << name() << " cannot be enabled from " | ||
| << statusToString(expected); | ||
| return -1; | ||
| } | ||
| if (openDevice(device_name_, params_->device.port)) { | ||
| LOG(ERROR) << "Failed to open device [" << device_name_ << "] on port [" | ||
| << params_->device.port << "] with GID index [" | ||
| << params_->device.gid_index << "]"; | ||
| disable(); | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
|
|
||
| native_pd_ = verbs_.ibv_alloc_pd(native_context_); | ||
| if (!native_pd_) { | ||
| PLOG(ERROR) << "ibv_alloc_pd"; | ||
| disable(); | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
|
|
||
| event_fd_ = epoll_create1(0); | ||
| if (event_fd_ < 0) { | ||
| PLOG(ERROR) << "epoll_create1"; | ||
| disable(); | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
|
|
||
| if (joinNonblockingPollList(event_fd_, native_context_->async_fd)) { | ||
| disable(); | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
|
|
||
|
|
@@ -358,12 +374,12 @@ int RdmaContext::enable() { | |
| comp_channel_[i] = verbs_.ibv_create_comp_channel(native_context_); | ||
| if (!comp_channel_[i]) { | ||
| PLOG(ERROR) << "ibv_create_comp_channel"; | ||
| disable(); | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
|
|
||
| if (joinNonblockingPollList(event_fd_, comp_channel_[i]->fd)) { | ||
| disable(); | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
| } | ||
|
|
@@ -372,7 +388,7 @@ int RdmaContext::enable() { | |
| auto cq = new RdmaCQ(); | ||
| int ret = cq->construct(this, params_->device.max_cqe, i); | ||
| if (ret) { | ||
| disable(); | ||
| rollbackEnable(); | ||
| return ret; | ||
| } | ||
|
runzhech marked this conversation as resolved.
Resolved
|
||
| cq_list_.push_back(cq); | ||
|
|
@@ -384,7 +400,7 @@ int RdmaContext::enable() { | |
| params_->device.num_cq_list); | ||
| if (notify_ret) { | ||
| LOG(ERROR) << "Failed to create notification CQ for " << device_name_; | ||
| disable(); | ||
| rollbackEnable(); | ||
| return notify_ret; | ||
| } | ||
|
|
||
|
|
@@ -419,9 +435,7 @@ int RdmaContext::enable() { | |
| if (ret) { | ||
| PLOG(ERROR) << "Failed to query port " << params_->device.port << " on " | ||
| << device_name_; | ||
| if (verbs_.ibv_close_device(native_context_)) { | ||
| PLOG(ERROR) << "ibv_close_device"; | ||
| } | ||
| rollbackEnable(); | ||
| return -1; | ||
| } | ||
|
|
||
|
|
@@ -439,16 +453,34 @@ int RdmaContext::enable() { | |
| } | ||
|
|
||
| int RdmaContext::disable() { | ||
| if (status_ == DEVICE_UNINIT || status_ == DEVICE_DISABLED) { | ||
| if (status_ == DEVICE_INITIALIZING) { | ||
| LOG(ERROR) << "RDMA context " << name() | ||
| << " cannot be disabled while it is initializing"; | ||
| return -1; | ||
| } | ||
|
runzhech marked this conversation as resolved.
Resolved
|
||
| if (status_ == DEVICE_UNINIT && !native_context_ && !native_pd_ && | ||
| event_fd_ < 0 && comp_channel_.empty() && cq_list_.empty() && | ||
| !notify_cq_) { | ||
| LOG(WARNING) << "RDMA context " << name() << " has been deconstructed"; | ||
| return 0; | ||
| } | ||
| if (endpoint_store_->clear()) { | ||
| if (endpoint_store_ && endpoint_store_->clear()) { | ||
| LOG(ERROR) << "Failed to destroy all endpoints for context " << name() | ||
| << "; preserving CQ, PD and device resources for retry"; | ||
| return -1; | ||
| } | ||
|
|
||
| cleanupResources(); | ||
| status_ = DEVICE_DISABLED; | ||
| return 0; | ||
| } | ||
|
|
||
| void RdmaContext::rollbackEnable() { | ||
| cleanupResources(); | ||
| status_ = DEVICE_DISABLED; | ||
| } | ||
|
|
||
| void RdmaContext::cleanupResources() { | ||
| for (auto& entry : mr_set_) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mr_set_ is iterated and cleared here without holding mr_set_mutex_, while every other access site takes the lock. In the current call graph disable() runs during teardown (same thread, after workers are stopped), so this isn't a live race today — but the broken locking discipline could bite if teardown ordering ever changes. Consider holding mr_set_mutex_ around the iterate-and-clear to keep the invariant, or add a comment documenting the relies-on-teardown-ordering assumption |
||
| int ret = verbs_.ibv_dereg_mr(entry); | ||
| if (ret) PLOG(ERROR) << "ibv_dereg_mr"; | ||
|
|
@@ -486,9 +518,6 @@ int RdmaContext::disable() { | |
| PLOG(ERROR) << "ibv_close_device"; | ||
| native_context_ = nullptr; | ||
| } | ||
|
|
||
| status_ = DEVICE_DISABLED; | ||
| return 0; | ||
| } | ||
|
|
||
| int RdmaContext::pause() { | ||
|
|
@@ -505,7 +534,8 @@ int RdmaContext::resume() { | |
|
|
||
| RdmaContext::MemReg RdmaContext::registerMemReg(void* addr, size_t length, | ||
| int access) { | ||
| if (status_ == DEVICE_DISABLED || status_ == DEVICE_UNINIT) { | ||
| if (status_ == DEVICE_DISABLED || status_ == DEVICE_INITIALIZING || | ||
| status_ == DEVICE_UNINIT) { | ||
| LOG(FATAL) << "RDMA context " << name() << " not constructed"; | ||
|
runzhech marked this conversation as resolved.
Resolved
|
||
| return nullptr; | ||
| } | ||
|
|
@@ -583,7 +613,8 @@ RdmaContext::MemReg RdmaContext::registerMemReg(void* addr, size_t length, | |
| } | ||
|
|
||
| int RdmaContext::warmupMrRegistration(void* addr, size_t length) { | ||
| if (status_ == DEVICE_DISABLED || status_ == DEVICE_UNINIT) { | ||
| if (status_ == DEVICE_DISABLED || status_ == DEVICE_INITIALIZING || | ||
| status_ == DEVICE_UNINIT) { | ||
| LOG(FATAL) << "RDMA context " << name() << " not constructed"; | ||
| return -1; | ||
| } | ||
|
|
@@ -607,7 +638,8 @@ int RdmaContext::warmupMrRegistration(void* addr, size_t length) { | |
| } | ||
|
|
||
| int RdmaContext::unregisterMemReg(MemReg id) { | ||
| if (status_ == DEVICE_DISABLED || status_ == DEVICE_UNINIT) { | ||
| if (status_ == DEVICE_DISABLED || status_ == DEVICE_INITIALIZING || | ||
| status_ == DEVICE_UNINIT) { | ||
| LOG(FATAL) << "RDMA context " << name() << " not constructed"; | ||
| return -1; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment says "reverse construction order", but the actual cleanup isn't strictly reverse — cq_list_ is cleaned before notify_cq_ (constructed after it), and event_fd_ before comp_channel_ (also constructed after). Functionally the order is fine (no interdependency requiring strict reverse), but the comment misleads future readers. It's better to drop "reverse construction order" from the comment.