-
Notifications
You must be signed in to change notification settings - Fork 163
nvme_driver: update driver to use Arc / Weak semantics to enforce unique ownership for namespaces by nsid
#2657
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
Merged
mattkur
merged 31 commits into
microsoft:main
from
gurasinghMS:handle-controller-namespaces-leaving
Jan 26, 2026
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f8eabd1
first pass
gurasinghMS 8ee51ca
no longer using strong ref counting
gurasinghMS edee1bc
Added a first pass on the strong/weak pointer approach
gurasinghMS 1d6fe56
weak pointer strong pointer approach
gurasinghMS 7175d6c
Now implemented a thin wrapper around Arc<Namespace> so as to make re…
gurasinghMS 5259989
v1 implementation with the is strong and is weak stuff
gurasinghMS ddeb8dd
More minor cleanup
gurasinghMS 2fb91b0
Uncommenting prep steps
gurasinghMS db97283
Update vm/devices/storage/disk_nvme/nvme_driver/src/driver.rs
gurasinghMS f4229c2
Update vm/devices/storage/disk_nvme/nvme_driver/src/driver.rs
gurasinghMS 0af769f
Update vm/devices/storage/disk_nvme/nvme_driver/src/driver.rs
gurasinghMS 127b0db
uncomment test for disk removal to verify changes in CI
gurasinghMS b18dfce
Remove unused import
gurasinghMS ed094d7
Fixing clippy related issues
gurasinghMS 22f9912
More clippy issues
gurasinghMS 6172ca3
Another clippy fix
gurasinghMS a6059c5
Now using the deref trait instead of rewriting all the functions
gurasinghMS 48f59ce
remove changes to prep steps
gurasinghMS 593a118
Minor comment changes
gurasinghMS ec20c27
Merge branch 'main' into handle-controller-namespaces-leaving
gurasinghMS 8ae9b20
doc build fixes
gurasinghMS 7647119
Forgot to save some changes
gurasinghMS b357f1c
Added more debugging lines to verify namespace recreation
gurasinghMS a46466e
Merge branch 'main' into handle-controller-namespaces-leaving
gurasinghMS 5cdb667
Fixed debug output issue
gurasinghMS 98b4064
Merge branch 'main' into handle-controller-namespaces-leaving
gurasinghMS 2e70d85
Increasing the retries for finding device along with longer sleep
gurasinghMS 1ff6ed4
Added a log line with debugging in storvsp as wellg
gurasinghMS 2f0af48
Increase retries to find a disk and reduce total number of iterations
gurasinghMS 7be1e20
Merge branch 'main' into handle-controller-namespaces-leaving
mattkur 9e52f8c
Merge branch 'main' into handle-controller-namespaces-leaving
alandau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,11 @@ | |
|
|
||
| use super::spec; | ||
| use crate::NVME_PAGE_SHIFT; | ||
| use crate::Namespace; | ||
| use crate::NamespaceError; | ||
| use crate::NamespaceHandle; | ||
| use crate::RequestError; | ||
| use crate::driver::save_restore::IoQueueSavedState; | ||
| use crate::namespace::Namespace; | ||
| use crate::queue_pair::AdminAerHandler; | ||
| use crate::queue_pair::Issuer; | ||
| use crate::queue_pair::MAX_CQ_ENTRIES; | ||
|
|
@@ -35,6 +36,7 @@ use std::mem::ManuallyDrop; | |
| use std::ops::Deref; | ||
| use std::sync::Arc; | ||
| use std::sync::OnceLock; | ||
| use std::sync::Weak; | ||
| use task_control::AsyncRun; | ||
| use task_control::InspectTask; | ||
| use task_control::TaskControl; | ||
|
|
@@ -76,15 +78,40 @@ pub struct NvmeDriver<D: DeviceBacking> { | |
| rescan_notifiers: Arc<RwLock<HashMap<u32, mesh::Sender<()>>>>, | ||
| /// NVMe namespaces associated with this driver. Mapping nsid to NamespaceHandle. | ||
| #[inspect(skip)] | ||
| namespaces: HashMap<u32, NamespaceHandle>, | ||
| namespaces: HashMap<u32, WeakOrStrong<Namespace>>, | ||
| /// Keeps the controller connected (CC.EN==1) while servicing. | ||
| nvme_keepalive: bool, | ||
| bounce_buffer: bool, | ||
| } | ||
|
|
||
| struct NamespaceHandle { | ||
| namespace: Arc<Namespace>, | ||
| in_use: bool, | ||
| /// A container that can hold either a weak or strong reference to a value. | ||
| /// | ||
| /// During normal operation, the driver ONLY stores weak references. After restore | ||
| /// strong references are temporarily held until the StorageController retrieves them. | ||
| /// Once retrieved, the strong reference is downgraded to a weak one, resuming | ||
| /// normal behavior. | ||
| enum WeakOrStrong<T> { | ||
| Weak(Weak<T>), | ||
| Strong(Arc<T>), | ||
| } | ||
|
|
||
| impl<T> WeakOrStrong<T> { | ||
| /// Returns a strong reference to the underlying value when possible. | ||
| /// Implicitly downgrades Strong to Weak when this function is invoked. | ||
| pub fn get_arc(&mut self) -> Option<Arc<T>> { | ||
| match self { | ||
| WeakOrStrong::Strong(arc) => { | ||
| let strong = arc.clone(); | ||
| *self = WeakOrStrong::Weak(Arc::downgrade(arc)); | ||
| Some(strong) | ||
| } | ||
| WeakOrStrong::Weak(weak) => weak.upgrade(), | ||
| } | ||
| } | ||
|
|
||
| pub fn is_weak(&self) -> bool { | ||
| matches!(self, WeakOrStrong::Weak(_)) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Inspect)] | ||
|
|
@@ -576,19 +603,23 @@ impl<D: DeviceBacking> NvmeDriver<D> { | |
| } | ||
|
|
||
| /// Gets the namespace with namespace ID `nsid`. | ||
| pub async fn namespace(&mut self, nsid: u32) -> Result<Arc<Namespace>, NamespaceError> { | ||
| if let Some(handle) = self.namespaces.get_mut(&nsid) { | ||
| // After reboot ns will be present but unused. | ||
| if !handle.in_use { | ||
| handle.in_use = true; | ||
| return Ok(handle.namespace.clone()); | ||
| } | ||
| pub async fn namespace(&mut self, nsid: u32) -> Result<NamespaceHandle, NamespaceError> { | ||
| if let Some(namespace) = self.namespaces.get_mut(&nsid) { | ||
|
Contributor
Author
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. I wonder if there is a more idiomatic way to do this ..... it looks a little off to me. |
||
| // After restore we will have a strong ref -> downgrade and return. | ||
| // If we have a weak ref, make sure it is not upgradeable (that means we have a duplicate somewhere). | ||
| let is_weak = namespace.is_weak(); // This value will change after invoking get_arc(). | ||
| let namespace = namespace.get_arc(); | ||
| if let Some(namespace) = namespace { | ||
| if is_weak && namespace.check_active().is_ok() { | ||
| return Err(NamespaceError::Duplicate(nsid)); | ||
| } | ||
|
|
||
| // Prevent multiple references to the same Namespace. | ||
| // Allowing this could lead to undefined behavior if multiple components | ||
| // concurrently read or write to the same namespace. To avoid this, | ||
| // return an error if the namespace is already requested. | ||
| return Err(NamespaceError::DuplicateRequest { nsid }); | ||
| tracing::debug!( | ||
| "reusing existing namespace nsid={}. This should only happen after restore.", | ||
| nsid | ||
| ); | ||
| return Ok(NamespaceHandle::new(namespace)); | ||
| } | ||
| } | ||
|
|
||
| let (send, recv) = mesh::channel::<()>(); | ||
|
|
@@ -603,18 +634,13 @@ impl<D: DeviceBacking> NvmeDriver<D> { | |
| ) | ||
| .await?, | ||
| ); | ||
| self.namespaces.insert( | ||
| nsid, | ||
| NamespaceHandle { | ||
| namespace: namespace.clone(), | ||
| in_use: true, | ||
| }, | ||
| ); | ||
| self.namespaces | ||
| .insert(nsid, WeakOrStrong::Weak(Arc::downgrade(&namespace))); | ||
|
|
||
| // Append the sender to the list of notifiers for this nsid. | ||
| let mut notifiers = self.rescan_notifiers.write(); | ||
| notifiers.insert(nsid, send); | ||
| Ok(namespace) | ||
| Ok(NamespaceHandle::new(namespace)) | ||
| } | ||
|
|
||
| /// Returns the number of CPUs that are in fallback mode (that are using a | ||
|
|
@@ -655,13 +681,19 @@ impl<D: DeviceBacking> NvmeDriver<D> { | |
| "saving namespaces", | ||
| ); | ||
| let mut saved_namespaces = vec![]; | ||
| for (nsid, handle) in self.namespaces.iter() { | ||
| saved_namespaces.push(handle.namespace.save().with_context(|| { | ||
| format!( | ||
| "failed to save namespace nsid {} device {}", | ||
| nsid, self.device_id | ||
| ) | ||
| })?); | ||
| for (nsid, namespace) in self.namespaces.iter_mut() { | ||
| let is_weak = namespace.is_weak(); // This value will change after invoking get_arc(). | ||
| if let Some(ns) = namespace.get_arc() | ||
| && ns.check_active().is_ok() | ||
| && is_weak | ||
| { | ||
| saved_namespaces.push(ns.save().with_context(|| { | ||
| format!( | ||
| "failed to save namespace nsid {} device {}", | ||
| nsid, self.device_id | ||
| ) | ||
| })?); | ||
| } | ||
| } | ||
| Ok(NvmeDriverSavedState { | ||
| identify_ctrl: spec::IdentifyController::read_from_bytes( | ||
|
|
@@ -951,17 +983,14 @@ impl<D: DeviceBacking> NvmeDriver<D> { | |
| let (send, recv) = mesh::channel::<()>(); | ||
| this.namespaces.insert( | ||
| ns.nsid, | ||
| NamespaceHandle { | ||
| namespace: Arc::new(Namespace::restore( | ||
| &driver, | ||
| admin.issuer().clone(), | ||
| recv, | ||
| this.identify.clone().unwrap(), | ||
| &this.io_issuers, | ||
| ns, | ||
| )?), | ||
| in_use: false, | ||
| }, | ||
| WeakOrStrong::Strong(Arc::new(Namespace::restore( | ||
| &driver, | ||
| admin.issuer().clone(), | ||
| recv, | ||
| this.identify.clone().unwrap(), | ||
| &this.io_issuers, | ||
| ns, | ||
| )?)), | ||
| ); | ||
| this.rescan_notifiers.write().insert(ns.nsid, send); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.