XMem: physical page numbers freed are given out again before they pass the title's 21 bits
Page numbers only ever grew, and the title keeps them as 21-bit `number >> 4`: past ~124 GiB of allocations over a run, a number came back truncated, unknown to us or aliasing a live page. Numbers are still given in order while they fit; then the lowest run of freed ones is used, and an allocation with no such run fails (count 0) instead of handing out a number the title cannot keep. Port of avatarmanz/VIctory-7900XTX#316 (issue #233 there). (cherry picked from commit 0db908bf11b861dcafc69eca9ef78c9e53b41ff1)
This commit is contained in:
1 parent
3f054e4677
commit
108a897113
1 file changed
+74
-4
@@ -547,6 +547,48 @@ struct Pages {
|
|||||||
pages: HashMap<usize, Slice>,
|
pages: HashMap<usize, Slice>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The first page number given out; 0 stays unused.
|
||||||
|
const FIRST_PAGE: usize = 0x10_0000;
|
||||||
|
/// Numbers from here on do not survive the title's 21-bit `number >> 4`
|
||||||
|
/// ([`FRAMES`]).
|
||||||
|
const PAGE_LIMIT: usize = 1 << 25;
|
||||||
|
|
||||||
|
impl Pages {
|
||||||
|
/// The first of `n` numbers, [`FRAMES`] apart, for new pages: after the
|
||||||
|
/// last given while they fit the title's 21 bits (as before), then the
|
||||||
|
/// lowest run of `n` freed ones. `None` if there is no such run.
|
||||||
|
fn take(&mut self, n: usize) -> Option<usize> {
|
||||||
|
let len = n.checked_mul(FRAMES)?;
|
||||||
|
let end = self.next_page.checked_add(len);
|
||||||
|
if let Some(end) = end.filter(|&end| end <= PAGE_LIMIT) {
|
||||||
|
let first = self.next_page;
|
||||||
|
self.next_page = end;
|
||||||
|
return Some(first);
|
||||||
|
}
|
||||||
|
static REUSING: std::sync::Once = std::sync::Once::new();
|
||||||
|
REUSING.call_once(|| {
|
||||||
|
tracing::info!("page numbers reach the title's 21 bits: freed ones given again");
|
||||||
|
});
|
||||||
|
let mut used: Vec<usize> = self.pages.keys().copied().collect();
|
||||||
|
used.sort_unstable();
|
||||||
|
free_run(used, len)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The lowest `start` from [`FIRST_PAGE`] where `len` (a multiple of
|
||||||
|
/// [`FRAMES`]) numbers up to [`PAGE_LIMIT`] are all outside `used`
|
||||||
|
/// (sorted).
|
||||||
|
fn free_run(used: impl IntoIterator<Item = usize>, len: usize) -> Option<usize> {
|
||||||
|
let mut start = FIRST_PAGE;
|
||||||
|
for at in used.into_iter().chain([PAGE_LIMIT]) {
|
||||||
|
if at.checked_sub(start).is_some_and(|gap| gap >= len) {
|
||||||
|
return Some(start);
|
||||||
|
}
|
||||||
|
start = start.max(at.checked_add(FRAMES)?);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Where the title sees its pages: [`Physical::views`] the other way.
|
/// Where the title sees its pages: [`Physical::views`] the other way.
|
||||||
/// Locked apart from them, and only for a moment ([`PAGES`]).
|
/// Locked apart from them, and only for a moment ([`PAGES`]).
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -663,8 +705,8 @@ impl Drop for Held {
|
|||||||
/// where both are.
|
/// where both are.
|
||||||
static PAGES: LazyLock<RwLock<Pages>> = LazyLock::new(|| {
|
static PAGES: LazyLock<RwLock<Pages>> = LazyLock::new(|| {
|
||||||
RwLock::new(Pages {
|
RwLock::new(Pages {
|
||||||
// Page numbers are opaque to titles; 0 stays unused.
|
// Page numbers are opaque to titles.
|
||||||
next_page: 0x10_0000,
|
next_page: FIRST_PAGE,
|
||||||
pages: HashMap::new(),
|
pages: HashMap::new(),
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@@ -731,8 +773,14 @@ pub extern "system" fn XMemAllocatePhysicalPages(
|
|||||||
committed: (0..n.div_ceil(64)).map(|_| std::sync::atomic::AtomicU64::new(0)).collect(),
|
committed: (0..n.div_ceil(64)).map(|_| std::sync::atomic::AtomicU64::new(0)).collect(),
|
||||||
});
|
});
|
||||||
let mut table = page_table_mut();
|
let mut table = page_table_mut();
|
||||||
let first = table.next_page;
|
let Some(first) = table.take(n) else {
|
||||||
table.next_page += n * FRAMES;
|
drop(table);
|
||||||
|
// The title keeps 21 bits of a number (`FRAMES`).
|
||||||
|
tracing::warn!(n, "XMemAllocatePhysicalPages: out of page numbers");
|
||||||
|
// SAFETY: checked non-null. The section goes with `section`.
|
||||||
|
unsafe { count.write(0) };
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
for index in 0..n {
|
for index in 0..n {
|
||||||
let slice = Slice {
|
let slice = Slice {
|
||||||
section: section.clone(),
|
section: section.clone(),
|
||||||
@@ -2955,6 +3003,28 @@ mod tests {
|
|||||||
assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1);
|
assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn page_numbers_freed_are_given_again_past_the_title_s_21_bits() {
|
||||||
|
let mut table = Pages {
|
||||||
|
next_page: PAGE_LIMIT - 2 * FRAMES,
|
||||||
|
pages: HashMap::new(),
|
||||||
|
};
|
||||||
|
// The last numbers that fit, as before.
|
||||||
|
assert_eq!(table.take(2), Some(PAGE_LIMIT - 2 * FRAMES));
|
||||||
|
assert_eq!(table.next_page, PAGE_LIMIT);
|
||||||
|
// Then the lowest free run, still packed into 21 bits.
|
||||||
|
assert_eq!(table.take(3), Some(FIRST_PAGE));
|
||||||
|
assert_eq!(table.take(usize::MAX), None);
|
||||||
|
let f = |k: usize| FIRST_PAGE + k * FRAMES;
|
||||||
|
assert_eq!(free_run([f(0), f(1), f(3)], FRAMES), Some(f(2)));
|
||||||
|
assert_eq!(free_run([f(0), f(1), f(3)], 2 * FRAMES), Some(f(4)));
|
||||||
|
assert_eq!(free_run([], PAGE_LIMIT - FIRST_PAGE), Some(FIRST_PAGE));
|
||||||
|
assert_eq!(free_run([f(0)], PAGE_LIMIT - FIRST_PAGE), None);
|
||||||
|
assert_eq!(free_run([], PAGE_LIMIT - FIRST_PAGE + FRAMES), None);
|
||||||
|
let p = free_run([PAGE_LIMIT - FRAMES], FRAMES).unwrap();
|
||||||
|
assert_eq!((p >> 4 & 0x1f_ffff) << 4, p);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn physical_pages_map_alias_and_unmap() {
|
fn physical_pages_map_alias_and_unmap() {
|
||||||
let mut pages = [0usize; 3];
|
let mut pages = [0usize; 3];
|
||||||
|
|||||||
Reference in new issue
Block a user