-
Notifications
You must be signed in to change notification settings - Fork 158
disk_nvme: add specific test for DMA allocation failure handling #2206
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,3 +265,95 @@ impl DeviceTestMemory { | |
| self.allocator.clone() | ||
| } | ||
| } | ||
|
|
||
| /// Callbacks for the [`DeviceTestDmaClient`]. Tests supply these to customize the behaviour of the dma client. | ||
| pub trait DeviceTestDmaClientCallbacks: Sync + Send { | ||
| /// Called when the DMA client needs to allocate a new DMA buffer. | ||
| fn allocate_dma_buffer( | ||
| &self, | ||
| allocator: &PagePoolAllocator, | ||
| total_size: usize, | ||
| ) -> anyhow::Result<user_driver::memory::MemoryBlock>; | ||
|
|
||
| /// Called when the DMA client needs to attach pending buffers. | ||
| fn attach_pending_buffers( | ||
| &self, | ||
| inner: &PagePoolAllocator, | ||
|
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. nit: why the different name for the same type? |
||
| ) -> anyhow::Result<Vec<user_driver::memory::MemoryBlock>>; | ||
| } | ||
|
|
||
| /// A DMA client that uses a [`PagePoolAllocator`] as the backing. It can be customized through the use of | ||
| /// [`DeviceTestDmaClientCallbacks`] to modify its behaviour for testing purposes. | ||
| /// | ||
| /// # Example | ||
| /// ```rust | ||
| /// use std::sync::Arc; | ||
| /// use user_driver::DmaClient; | ||
| /// use user_driver_emulated_mock::DeviceTestDmaClient; | ||
| /// use page_pool_alloc::PagePoolAllocator; | ||
| /// | ||
| /// struct MyCallbacks; | ||
| /// impl user_driver_emulated_mock::DeviceTestDmaClientCallbacks for MyCallbacks { | ||
| /// fn allocate_dma_buffer( | ||
| /// &self, | ||
| /// allocator: &page_pool_alloc::PagePoolAllocator, | ||
| /// total_size: usize, | ||
| /// ) -> anyhow::Result<user_driver::memory::MemoryBlock> { | ||
| /// // Custom test logic here, for example: | ||
| /// anyhow::bail!("allocation failed for testing"); | ||
| /// } | ||
| /// | ||
| /// fn attach_pending_buffers( | ||
| /// &self, | ||
| /// allocator: &page_pool_alloc::PagePoolAllocator, | ||
| /// ) -> anyhow::Result<Vec<user_driver::memory::MemoryBlock>> { | ||
| /// // Custom test logic here, for example: | ||
| /// anyhow::bail!("attachment failed for testing"); | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// // Use the above in a test ... | ||
| /// fn test_dma_client() { | ||
| /// let pages = 1000; | ||
| /// let device_test_memory = user_driver_emulated_mock::DeviceTestMemory::new( | ||
| /// pages, | ||
| /// true, | ||
| /// "test_dma_client", | ||
| /// ); | ||
| /// let page_pool_allocator = device_test_memory.dma_client(); | ||
| /// let dma_client = DeviceTestDmaClient::new(page_pool_allocator, MyCallbacks); | ||
| /// | ||
| /// // Use dma_client in tests... | ||
| /// assert!(dma_client.allocate_dma_buffer(4096).is_err()); | ||
| /// } | ||
| /// ``` | ||
| #[derive(Inspect)] | ||
| #[inspect(transparent)] | ||
| pub struct DeviceTestDmaClient<C> | ||
| where | ||
| C: DeviceTestDmaClientCallbacks, | ||
| { | ||
| inner: Arc<PagePoolAllocator>, | ||
| #[inspect(skip)] | ||
| callbacks: C, | ||
| } | ||
|
|
||
| impl<C: DeviceTestDmaClientCallbacks> DeviceTestDmaClient<C> { | ||
| /// Creates a new [`DeviceTestDmaClient`] with the given inner allocator. | ||
| pub fn new(inner: Arc<PagePoolAllocator>, callbacks: C) -> Self { | ||
| Self { inner, callbacks } | ||
| } | ||
| } | ||
|
|
||
| impl<C: DeviceTestDmaClientCallbacks> DmaClient for DeviceTestDmaClient<C> { | ||
| fn allocate_dma_buffer( | ||
| &self, | ||
| total_size: usize, | ||
| ) -> anyhow::Result<user_driver::memory::MemoryBlock> { | ||
| self.callbacks.allocate_dma_buffer(&self.inner, total_size) | ||
| } | ||
|
|
||
| fn attach_pending_buffers(&self) -> anyhow::Result<Vec<user_driver::memory::MemoryBlock>> { | ||
| self.callbacks.attach_pending_buffers(&self.inner) | ||
| } | ||
| } | ||
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.