-
Notifications
You must be signed in to change notification settings - Fork 121
DMA hint calculation in OpenHCL bootshim and fallback mem allocator for NVMe #1190
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
Open
yupavlen-ms
wants to merge
18
commits into
microsoft:main
Choose a base branch
from
yupavlen-ms:dma_self_hint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e56fecc
Thinking where to add it
yupavlen-ms 4391f69
Improved syntax
yupavlen-ms ac04936
More experiments
yupavlen-ms b45f919
Added table lookup (incomplete table yet)
yupavlen-ms 3d744e2
Minor styling
yupavlen-ms 5670edf
Add unit test
yupavlen-ms 536e7bc
More tests
yupavlen-ms 936d262
Replace float point with integers
yupavlen-ms 3b37398
Make it more compact
yupavlen-ms 989c2c9
Expand the table
yupavlen-ms 3ad7040
Some progress to propagate fallback flag uphill but not quite done yet
yupavlen-ms 0f12275
Simplify some return values
yupavlen-ms 0d796ba
Closer to final code
yupavlen-ms b5a2ba1
Validated fallback allocator for NVMe, more tracing
yupavlen-ms e4e7ffc
Move fallback logic to dma clients
yupavlen-ms 727fd7d
Tracking and inspecting improvements
yupavlen-ms 09efefb
Inform openhcl about self hint and print it
yupavlen-ms 1c34003
Swapped one row
yupavlen-ms 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
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 |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
//! Calculate DMA hint value if not provided by host. | ||
|
||
use super::PartitionInfo; | ||
use igvm_defs::{MemoryMapEntryType, PAGE_SIZE_4K}; | ||
|
||
/// Lookup table for DMA hint calculation. | ||
/// Using tuples instead of structs to keep it readable. | ||
/// Let's keep the table sorted by VP count, then by assigned memory. | ||
/// Using u16 to keep the memory req short. | ||
/// Max VTL2 memory known today is 24838 MiB. | ||
/// (vp_count, vtl2_memory_mb, dma_hint_mb) | ||
const LOOKUP_TABLE: &[(u16, u16, u16)] = &[ | ||
(2, 96, 2), | ||
(2, 98, 4), | ||
(2, 100, 4), | ||
(2, 104, 4), | ||
(4, 108, 2), | ||
(4, 110, 6), | ||
(4, 112, 6), | ||
(4, 118, 8), | ||
(4, 130, 12), | ||
(8, 140, 4), | ||
(8, 148, 10), | ||
(8, 170, 20), | ||
(8, 176, 20), | ||
(16, 234, 12), | ||
(16, 256, 20), // There is another configuration with '18'. | ||
(16, 268, 38), | ||
(16, 282, 54), | ||
(24, 420, 66), | ||
(32, 404, 22), | ||
(32, 516, 36), | ||
(32, 538, 74), // There is another configuration with '52'. | ||
(48, 558, 32), | ||
(48, 718, 52), | ||
(48, 730, 52), | ||
(48, 746, 78), | ||
(64, 712, 42), | ||
(64, 924, 68), | ||
(64, 938, 68), | ||
(96, 1030, 64), | ||
(96, 1042, 114), // Can be '64'. | ||
(96, 1058, 114), // Can be '106'. | ||
(96, 1340, 102), | ||
(96, 1358, 104), | ||
(96, 1382, 120), | ||
(112, 1566, 288), | ||
(128, 1342, 84), | ||
(128, 1360, 84), | ||
(896, 12912, 0), // (516) Needs to be validated as the vNIC number is unknown. | ||
]; | ||
|
||
/// Round up to next 2MiB. | ||
fn round_up_to_2mb(pages_4k: u64) -> u64 { | ||
(pages_4k + 511) & !(511) | ||
} | ||
|
||
/// Returns calculated DMA hint value, in 4k pages. | ||
pub fn vtl2_calculate_dma_hint(vp_count: usize, storage: &PartitionInfo) -> u64 { | ||
let mut dma_hint_4k = 0; | ||
let mem_size = storage | ||
.vtl2_ram | ||
.iter() | ||
.filter(|m| m.mem_type == MemoryMapEntryType::VTL2_PROTECTABLE) | ||
.map(|e| e.range.len()) | ||
.sum::<u64>(); | ||
// Sanity check for the calculated memory size. | ||
if mem_size > 0 && mem_size < 0xFFFFFFFF00000 { | ||
let mem_size_mb = (mem_size / 1048576) as u32; | ||
|
||
let mut min_vtl2_memory_mb = 65535; | ||
let mut max_vtl2_memory_mb = 0; | ||
|
||
// To avoid using floats, scale ratios to 1:1000. | ||
let mut min_ratio_1000th = 100000; | ||
let mut max_ratio_1000th = 1000; | ||
|
||
let mut min_vp_count: u16 = 1; | ||
let mut max_vp_count = vp_count as u16; | ||
|
||
for (vp_lookup, vtl2_memory_mb, dma_hint_mb) in LOOKUP_TABLE { | ||
match (*vp_lookup).cmp(&(vp_count as u16)) { | ||
core::cmp::Ordering::Less => { | ||
// Find nearest. | ||
min_vp_count = min_vp_count.max(*vp_lookup); | ||
} | ||
core::cmp::Ordering::Equal => { | ||
if *vtl2_memory_mb == mem_size_mb as u16 { | ||
// Found exact match. | ||
dma_hint_4k = *dma_hint_mb as u64 * 1048576 / PAGE_SIZE_4K; | ||
max_vtl2_memory_mb = *vtl2_memory_mb; | ||
break; | ||
} else { | ||
// Prepare for possible extrapolation. | ||
min_vtl2_memory_mb = min_vtl2_memory_mb.min(*vtl2_memory_mb); | ||
max_vtl2_memory_mb = max_vtl2_memory_mb.max(*vtl2_memory_mb); | ||
min_ratio_1000th = min_ratio_1000th | ||
.min(*vtl2_memory_mb as u32 * 1000 / *dma_hint_mb as u32); | ||
max_ratio_1000th = max_ratio_1000th | ||
.max(*vtl2_memory_mb as u32 * 1000 / *dma_hint_mb as u32); | ||
} | ||
} | ||
core::cmp::Ordering::Greater => { | ||
// Find nearest. | ||
max_vp_count = max_vp_count.min(*vp_lookup); | ||
} | ||
} | ||
} | ||
|
||
// It is possible there were no matching entries in the lookup table. | ||
// (i.e. unexpected VP count). | ||
if max_vtl2_memory_mb == 0 { | ||
LOOKUP_TABLE | ||
.iter() | ||
.filter(|(vp_lookup, _, _)| { | ||
*vp_lookup == min_vp_count || *vp_lookup == max_vp_count | ||
}) | ||
.for_each(|(_, vtl2_memory_mb, dma_hint_mb)| { | ||
min_vtl2_memory_mb = min_vtl2_memory_mb.min(*vtl2_memory_mb); | ||
max_vtl2_memory_mb = max_vtl2_memory_mb.max(*vtl2_memory_mb); | ||
min_ratio_1000th = | ||
min_ratio_1000th.min(*vtl2_memory_mb as u32 * 1000 / *dma_hint_mb as u32); | ||
max_ratio_1000th = | ||
max_ratio_1000th.max(*vtl2_memory_mb as u32 * 1000 / *dma_hint_mb as u32); | ||
}); | ||
} | ||
|
||
if dma_hint_4k == 0 { | ||
// Didn't find an exact match for vp_count, try to extrapolate. | ||
dma_hint_4k = (mem_size_mb as u64 * 1000u64 * (1048576u64 / PAGE_SIZE_4K)) | ||
/ ((min_ratio_1000th + max_ratio_1000th) as u64 / 2u64); | ||
|
||
// And then round up to 2MiB. | ||
dma_hint_4k = round_up_to_2mb(dma_hint_4k); | ||
} | ||
} | ||
|
||
dma_hint_4k | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::MemoryRange; | ||
use crate::host_params::MemoryEntry; | ||
use test_with_tracing::test; | ||
|
||
#[test] | ||
fn test_vtl2_calculate_dma_hint() { | ||
let mut storage = PartitionInfo::new(); | ||
|
||
storage.vtl2_ram.clear(); | ||
storage.vtl2_ram.push(MemoryEntry { | ||
range: MemoryRange::new(0x0..0x6200000), | ||
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE, | ||
vnode: 0, | ||
}); | ||
assert_eq!(vtl2_calculate_dma_hint(2, &storage), 1024); | ||
|
||
storage.vtl2_ram.clear(); | ||
storage.vtl2_ram.push(MemoryEntry { | ||
range: MemoryRange::new(0x0..0x6E00000), | ||
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE, | ||
vnode: 0, | ||
}); | ||
assert_eq!(vtl2_calculate_dma_hint(4, &storage), 1536); | ||
|
||
// Test VP count higher than max from LOOKUP_TABLE. | ||
storage.vtl2_ram.clear(); | ||
storage.vtl2_ram.push(MemoryEntry { | ||
range: MemoryRange::new(0x0..0x7000000), | ||
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE, | ||
vnode: 0, | ||
}); | ||
assert_eq!(vtl2_calculate_dma_hint(112, &storage), 5632); | ||
|
||
// Test unusual VP count. | ||
storage.vtl2_ram.clear(); | ||
storage.vtl2_ram.push(MemoryEntry { | ||
range: MemoryRange::new(0x0..0x6000000), | ||
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE, | ||
vnode: 0, | ||
}); | ||
assert_eq!(vtl2_calculate_dma_hint(52, &storage), 2048); | ||
|
||
storage.vtl2_ram.clear(); | ||
storage.vtl2_ram.push(MemoryEntry { | ||
range: MemoryRange::new(0x0..0x8000000), | ||
mem_type: MemoryMapEntryType::VTL2_PROTECTABLE, | ||
vnode: 0, | ||
}); | ||
assert_eq!(vtl2_calculate_dma_hint(52, &storage), 2560); | ||
} | ||
} |
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.
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.
I'd probably want some more rationale on what we should do if we don't match one of these lookup tables exactly.