The title's writes into guarded blocks go in beside the guard: its memcpy, memmove and memset (VICTORY_COPY_BESIDE=1) and single stores that fault (VICTORY_STORE_BESIDE=1); both off
Most of what faults is the runtime's memcpy (a third by time) and single stores into objects that lie in memory some copy covers: flags, reference counts, links of free lists. The bytes are put in through our own view of the page, the block told as written at every view by a bit that the next asking takes: no protection changes, for the write or for the next taking. Copies: the title's imports are ours and look at the blocks' bits. Stores: the instruction that faulted is decoded and done in the handler. Too many to a block, or code that fills blocks: opened as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0578afffbb
commit
44c69a8fee
8 files changed
+1229
-16
No files matched your search
+258
-14
@@ -791,19 +791,36 @@ impl Bits {
|
||||
unsafe { piece.as_ref() }
|
||||
}
|
||||
|
||||
/// One caller at a time (under the guards' lock).
|
||||
fn set(&self, block: usize) {
|
||||
use std::sync::atomic::Ordering::{Relaxed, Release};
|
||||
use std::sync::atomic::Ordering::{AcqRel, Acquire, SeqCst};
|
||||
let word = block / 64;
|
||||
let Some(place) = self.0.get(word / Bits::WORDS) else {
|
||||
return;
|
||||
};
|
||||
if place.load(Relaxed).is_null() {
|
||||
if place.load(Acquire).is_null() {
|
||||
let piece: Box<BitsPiece> = Box::new([const { std::sync::atomic::AtomicU64::new(0) }; Bits::WORDS]);
|
||||
place.store(Box::into_raw(piece), Release);
|
||||
let piece = Box::into_raw(piece);
|
||||
if place.compare_exchange(std::ptr::null_mut(), piece, AcqRel, Acquire).is_err() {
|
||||
// SAFETY: made just now, and another thread's is there.
|
||||
drop(unsafe { Box::from_raw(piece) });
|
||||
}
|
||||
}
|
||||
if let Some(piece) = self.piece(word) {
|
||||
piece[word % Bits::WORDS].fetch_or(1 << (block % 64), Release);
|
||||
piece[word % Bits::WORDS].fetch_or(1 << (block % 64), SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sixty-four of them.
|
||||
fn word(&self, word: usize) -> u64 {
|
||||
self.piece(word).map_or(0, |piece| piece[word % Bits::WORDS].load(std::sync::atomic::Ordering::SeqCst))
|
||||
}
|
||||
|
||||
/// Whether it was set, and is no more.
|
||||
fn take(&self, block: usize) -> bool {
|
||||
self.any(block, block + 1) && {
|
||||
let (word, bit) = (block / 64, 1 << (block % 64));
|
||||
self.piece(word)
|
||||
.is_some_and(|piece| piece[word % Bits::WORDS].fetch_and(!bit, std::sync::atomic::Ordering::SeqCst) & bit != 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -883,12 +900,93 @@ static PUBLISHED: Bits = Bits::new();
|
||||
/// no view.
|
||||
static PROTECTED: Bits = Bits::new();
|
||||
|
||||
/// The blocks the title's bytes were put in beside their guards
|
||||
/// ([`put_beside`]) and that no asking has told as written since. Such a
|
||||
/// block is as it was else: read-only, clean, and an asking that finds its
|
||||
/// bit here takes the bit and tells it as written.
|
||||
///
|
||||
/// The bit is set when the bytes are in, at every view of the page, and
|
||||
/// [`PUBLISHED`]'s is cleared after that. So an asking either finds the bit
|
||||
/// or came before the title's thread went on from its write; and who takes
|
||||
/// the bit reads the bytes after.
|
||||
static BESIDE: Bits = Bits::new();
|
||||
|
||||
/// How much was put into a block beside its guard since it was last told as
|
||||
/// written (`crate::titlewrite::MOST`), by a few bits of its number.
|
||||
static PUT: [std::sync::atomic::AtomicU32; 1 << 13] = [const { std::sync::atomic::AtomicU32::new(0) }; 1 << 13];
|
||||
|
||||
fn put_count(block: usize) -> &'static std::sync::atomic::AtomicU32 {
|
||||
&PUT[block % PUT.len()]
|
||||
}
|
||||
|
||||
/// Whether [`put_beside`] put it there.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub(crate) enum Beside {
|
||||
Done,
|
||||
/// The block has had its share.
|
||||
Full,
|
||||
/// Not guarded, or there is no other way to its page, or the runtime is
|
||||
/// changing a mapping.
|
||||
Cannot,
|
||||
}
|
||||
|
||||
/// Whether a block of `[at, at + len)` is (or is about to be) read-only by
|
||||
/// our hand. `len` is not 0.
|
||||
#[inline]
|
||||
pub(crate) fn guards_any(at: usize, len: usize) -> bool {
|
||||
PROTECTED.any(at / BLOCK, (at.saturating_add(len) - 1) / BLOCK + 1)
|
||||
}
|
||||
|
||||
/// The title's bytes for a guarded block, put in by `put` at our own view of
|
||||
/// the same page: it is given what to add to an address of the block (as by
|
||||
/// `wrapping_add`). `weight`: how much of the block's share it is.
|
||||
pub(crate) fn put_beside(block: usize, weight: u32, put: impl FnOnce(usize)) -> Beside {
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
if !light() || !view_guard() || !PROTECTED.any(block, block + 1) {
|
||||
return Beside::Cannot;
|
||||
}
|
||||
let count = put_count(block);
|
||||
if count.load(Relaxed) + weight > crate::titlewrite::MOST {
|
||||
return Beside::Full;
|
||||
}
|
||||
// No mapping changes until all is told: a view of the page that came
|
||||
// after the look at them would not be.
|
||||
let Some(_reading) = title_memory_reading_now() else {
|
||||
return Beside::Cannot;
|
||||
};
|
||||
let Some((page, shown)) = crate::inplace::beside_page(block * BLOCK) else {
|
||||
return Beside::Cannot;
|
||||
};
|
||||
put(page.wrapping_sub(block * BLOCK));
|
||||
count.fetch_add(weight, Relaxed);
|
||||
for view in shown.iter().filter(|&&view| view != 0) {
|
||||
BESIDE.set(view / BLOCK);
|
||||
}
|
||||
for view in shown.iter().filter(|&&view| view != 0) {
|
||||
PUBLISHED.clear(view / BLOCK);
|
||||
}
|
||||
Beside::Done
|
||||
}
|
||||
|
||||
/// The title is about to write block by block around `at`: as by a fault.
|
||||
pub(crate) fn opened_for_writing(at: usize) {
|
||||
let mut g = guards();
|
||||
if open(&mut g, at, at + 1) != 0 {
|
||||
g.opened(at / BLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
/// An asking made blocks of `pages` clean and noted them in the journal.
|
||||
fn publish(pages: &[usize]) {
|
||||
let g = guards();
|
||||
for page in pages {
|
||||
if g.clean.contains(*page) || g.held.contains(*page) {
|
||||
PUBLISHED.set(*page);
|
||||
for &page in pages {
|
||||
if g.clean.contains(page) || g.held.contains(page) {
|
||||
PUBLISHED.set(page);
|
||||
// (Put there since: whoever did clears the bit after this is
|
||||
// set, or its own bit is found here.)
|
||||
if BESIDE.any(page, page + 1) {
|
||||
PUBLISHED.clear(page);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1240,6 +1338,7 @@ impl Guards {
|
||||
self.opened.clear();
|
||||
}
|
||||
}
|
||||
put_count(block).store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
self.opened.insert(block, std::time::Instant::now());
|
||||
}
|
||||
}
|
||||
@@ -1434,7 +1533,7 @@ fn guard(start: usize, end: usize) -> Option<Vec<usize>> {
|
||||
// takes the block out of the set, and it is looked at as any other.
|
||||
let unsure: Vec<usize> = {
|
||||
let g = guards_timed();
|
||||
if g.clean.range(first..last).count() == last - first {
|
||||
if g.clean.range(first..last).count() == last - first && !BESIDE.any(first, last) {
|
||||
// Nothing written, mapped or new since the range was last taken.
|
||||
#[cfg(feature = "diagnostics")]
|
||||
tally(3, 1);
|
||||
@@ -1471,7 +1570,7 @@ fn guard(start: usize, end: usize) -> Option<Vec<usize>> {
|
||||
// clean.
|
||||
let mut written = Vec::new();
|
||||
for word in first / 64..last.div_ceil(64) {
|
||||
let known = g.clean.word(word) | g.absent.word(word);
|
||||
let known = g.clean.word(word) & !BESIDE.word(word) | g.absent.word(word);
|
||||
written.extend(BitsOf(!known & in_word(word * 64, first, last)).map(|bit| word * 64 + bit));
|
||||
}
|
||||
let mut run: Option<(usize, usize)> = None;
|
||||
@@ -1486,12 +1585,20 @@ fn guard(start: usize, end: usize) -> Option<Vec<usize>> {
|
||||
drop(g);
|
||||
g = guards_timed();
|
||||
}
|
||||
// Told as written by this, and read by the caller after.
|
||||
if BESIDE.take(block) {
|
||||
put_count(block).store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
if g.protected.contains(block) {
|
||||
// Read-only by way of another view of its page, or not clean
|
||||
// because one could not be made so.
|
||||
// because one could not be made so. (Or clean, and bytes were put
|
||||
// in beside the guard: it stays.)
|
||||
if let Some((from, to)) = run.take() {
|
||||
protect(&mut g, from, to);
|
||||
}
|
||||
if g.clean.contains(block) {
|
||||
continue;
|
||||
}
|
||||
if cover(&mut g, block, block + 1) {
|
||||
g.clean.insert(block);
|
||||
}
|
||||
@@ -2330,9 +2437,11 @@ pub(crate) fn install() {
|
||||
// A fault of our own copies (`crate::safecopy`): whatever it is, if
|
||||
// it is not a block we guard, the copy fails and goes on from there.
|
||||
let ours = crate::safecopy::failing_exit(r.ExceptionAddress as usize);
|
||||
let fail = |exit: usize| {
|
||||
// SAFETY: as above.
|
||||
let context = unsafe { (*info).ContextRecord };
|
||||
let fail = move |exit: usize| {
|
||||
// SAFETY: the context the system passes, to go on from.
|
||||
unsafe { (*(*info).ContextRecord).Rip = exit as u64 };
|
||||
unsafe { (*context).Rip = exit as u64 };
|
||||
EXCEPTION_CONTINUE_EXECUTION
|
||||
};
|
||||
if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS
|
||||
@@ -2353,6 +2462,13 @@ pub(crate) fn install() {
|
||||
}
|
||||
let at = r.ExceptionInformation[1];
|
||||
let block = at / BLOCK;
|
||||
if ours.is_none()
|
||||
&& PROTECTED.any(block, block + 1)
|
||||
// SAFETY: the context the system passes, to go on from.
|
||||
&& crate::titlewrite::store(unsafe { &mut *context }, at)
|
||||
{
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
// Another thread may have opened the block since this one faulted
|
||||
// (then nothing waits for the lock), and a copy may have guarded it
|
||||
// again since that (then it is ours again, not read-only for some
|
||||
@@ -2380,6 +2496,7 @@ pub(crate) fn install() {
|
||||
let ns = waited.as_nanos().min(u128::from(u32::MAX)) as usize;
|
||||
let held = (began.elapsed() - waited).as_nanos() as u64;
|
||||
crate::faultbook::note(crate::faultbook::What::Fault, block, opened << 32 | ns, held);
|
||||
crate::faultbook::note(crate::faultbook::What::Site, r.ExceptionAddress as usize, block, 0);
|
||||
}
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
@@ -3308,6 +3425,116 @@ mod tests {
|
||||
drop_views(section, &[one, two], BLOCK);
|
||||
}
|
||||
|
||||
/// What is put in beside a guard is told at every view, once, and the
|
||||
/// block stays read-only; until it has had its share.
|
||||
#[test]
|
||||
fn what_the_title_puts_in_beside_a_guard_is_told() {
|
||||
let (section, one, two) = two_views(BLOCK);
|
||||
crate::title_memory_section(section, BLOCK as u64, true);
|
||||
let page = |view: usize| view / PAGE;
|
||||
for view in [one, two] {
|
||||
assert_eq!(guard(view, view + BLOCK), Some(vec![page(view)]));
|
||||
}
|
||||
assert!(writes(one, one + BLOCK, true).is_some() && writes(two, two + BLOCK, true).is_some());
|
||||
put_count(one / BLOCK).store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
let put = |byte: u8| loop {
|
||||
// SAFETY: a byte of the section's only page.
|
||||
let done = put_beside(one / BLOCK, 1, |delta| unsafe { ((one + 100).wrapping_add(delta) as *mut u8).write(byte) });
|
||||
// (Other tests hold changes of mapping.)
|
||||
if done != Beside::Cannot {
|
||||
return done;
|
||||
}
|
||||
};
|
||||
for round in 1..4 {
|
||||
assert_eq!(put(round), Beside::Done);
|
||||
// SAFETY: the view the title reads it by.
|
||||
assert_eq!(unsafe { ((two + 100) as *const u8).read_volatile() }, round);
|
||||
assert!(!writable(one) && !writable(two) && !published(one, one + BLOCK) && !published(two, two + BLOCK));
|
||||
for view in [two, one] {
|
||||
assert_eq!(guard(view, view + BLOCK), Some(vec![page(view)]));
|
||||
assert_eq!(guard(view, view + BLOCK), Some(Vec::new()));
|
||||
}
|
||||
}
|
||||
for _ in 0..crate::titlewrite::MOST {
|
||||
assert_eq!(put(9), Beside::Done);
|
||||
}
|
||||
assert_eq!(put(9), Beside::Full);
|
||||
// A refresh between gives it its share again.
|
||||
assert_eq!(guard(one, one + BLOCK), Some(vec![page(one)]));
|
||||
assert_eq!(put(9), Beside::Done);
|
||||
// Opened by a fault, it is told by that.
|
||||
assert_eq!(guard(one, one + BLOCK), Some(vec![page(one)]));
|
||||
// SAFETY: a view, opened by the handler.
|
||||
unsafe { ((one + 8) as *mut u8).write(1) };
|
||||
assert_eq!(put_beside(one / BLOCK, 1, |_| panic!("it is not guarded")), Beside::Cannot);
|
||||
assert_eq!(guard(one, one + BLOCK), Some(vec![page(one)]));
|
||||
assert_eq!(guard(one, one + BLOCK), Some(Vec::new()));
|
||||
drop_views(section, &[one, two], BLOCK);
|
||||
crate::title_memory_section_gone(section);
|
||||
}
|
||||
|
||||
/// Stores that fault, and copies, as the title makes them: what is there
|
||||
/// after, the block's guard and what the next asking says.
|
||||
#[test]
|
||||
fn stores_and_copies_of_the_title_leave_the_guard() {
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
unsafe extern "system-unwind" fn real(to: *mut u8, from: *const u8, n: usize) -> *mut u8 {
|
||||
// SAFETY: by the caller.
|
||||
unsafe { std::ptr::copy(from, to, n) };
|
||||
to
|
||||
}
|
||||
const SIZE: usize = 2 * BLOCK;
|
||||
let (section, one, two) = two_views(SIZE);
|
||||
crate::title_memory_section(section, SIZE as u64, true);
|
||||
let pages = |view: usize| vec![view / PAGE, view / PAGE + 1];
|
||||
assert_eq!(guard(one, one + SIZE), Some(pages(one)));
|
||||
assert_eq!(guard(two, two + SIZE), Some(pages(two)));
|
||||
let copy = crate::titlewrite::import("memcpy", real as *const () as usize);
|
||||
crate::titlewrite::ON_IN_TEST.set(true);
|
||||
let copy = if copy == real as *const () as usize {
|
||||
crate::titlewrite::copy_for_test(real as *const () as usize)
|
||||
} else {
|
||||
copy
|
||||
};
|
||||
// SAFETY: of that signature.
|
||||
let copy: unsafe extern "system-unwind" fn(*mut u8, *const u8, usize) -> *mut u8 = unsafe { std::mem::transmute(copy) };
|
||||
// No change of mapping meanwhile but the other tests' moments of it:
|
||||
// a store that meets one opens the block, as before.
|
||||
let mut kept = 0;
|
||||
let quiet = || {
|
||||
while title_memory_reading_now().is_none() {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
};
|
||||
for round in 1..200u32 {
|
||||
quiet();
|
||||
// SAFETY (all): words of the view.
|
||||
unsafe { ((one + 64) as *mut u32).write_volatile(round) };
|
||||
let was = unsafe { AtomicU32::from_ptr((two + 64) as *mut u32) }.fetch_sub(1, Ordering::SeqCst);
|
||||
assert_eq!((was, unsafe { ((one + 64) as *const u32).read_volatile() }), (round, round - 1));
|
||||
let bytes = vec![round as u8; 3000 + round as usize];
|
||||
// Over the end of the first block.
|
||||
assert_eq!(unsafe { copy((one + BLOCK - 1000) as *mut u8, bytes.as_ptr(), bytes.len()) } as usize, one + BLOCK - 1000);
|
||||
let there = unsafe { std::slice::from_raw_parts((two + BLOCK - 1000) as *const u8, bytes.len()) };
|
||||
assert!(there == bytes && unsafe { ((two + BLOCK + 2000 + round as usize) as *const u8).read_volatile() } != round as u8);
|
||||
kept += usize::from(!writable(one) && !writable(two));
|
||||
for view in [one, two] {
|
||||
assert_eq!(guard(view, view + SIZE), Some(pages(view)), "round {round}");
|
||||
assert_eq!(guard(view, view + SIZE), Some(Vec::new()), "round {round}");
|
||||
}
|
||||
}
|
||||
assert!(kept > 10, "the guard stayed {kept} times: {:?}", crate::titlewrite::DONE);
|
||||
// Many stores to a block: it is opened.
|
||||
quiet();
|
||||
for k in 0..20 {
|
||||
unsafe { ((one + 128 + k * 4) as *mut u32).write_volatile(7) };
|
||||
}
|
||||
assert!(writable(one));
|
||||
crate::titlewrite::ON_IN_TEST.set(false);
|
||||
drop_views(section, &[one, two], SIZE);
|
||||
crate::title_memory_section_gone(section);
|
||||
}
|
||||
|
||||
/// The set of blocks against another of the same.
|
||||
#[test]
|
||||
fn blocks_are_a_set() {
|
||||
@@ -3604,6 +3831,7 @@ mod many_askers_tests {
|
||||
};
|
||||
let by_bits = std::sync::atomic::AtomicUsize::new(0);
|
||||
let past = std::sync::atomic::AtomicUsize::new(0);
|
||||
let title_beside = std::sync::atomic::AtomicUsize::new(0);
|
||||
std::thread::scope(|s| {
|
||||
if VIEWS > 1 {
|
||||
s.spawn(move || {
|
||||
@@ -3635,7 +3863,7 @@ mod many_askers_tests {
|
||||
});
|
||||
}
|
||||
for t in 0..THREADS {
|
||||
let (by_bits, past) = (&by_bits, &past);
|
||||
let (by_bits, past, title_beside) = (&by_bits, &past, &title_beside);
|
||||
s.spawn(move || {
|
||||
super::REST_IN_TEST.set(rest);
|
||||
let mut last = [[0u64; PAGES]; VIEWS];
|
||||
@@ -3657,6 +3885,21 @@ mod many_askers_tests {
|
||||
let word = through + page * PAGE + t * 8;
|
||||
// SAFETY (both): as above.
|
||||
// (Waited for: other tests hold changes of mapping.)
|
||||
// (Some as a copy of the title's, or a store of it, does.)
|
||||
if next(3) == 0
|
||||
&& super::put_beside(word / PAGE, 1, |delta| unsafe {
|
||||
(word.wrapping_add(delta) as *mut u64).write_volatile(3);
|
||||
}) == super::Beside::Done
|
||||
{
|
||||
title_beside.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let (now, written) = writes(at + from * PAGE, at + to * PAGE, true).expect("a view");
|
||||
let number = written.iter().find(|w| w.0 == at / PAGE + page).map_or(0, |w| w.1);
|
||||
assert!(number > last[page], "page {page}, beside: {number} is not after {}", last[page]);
|
||||
for l in &mut last[from..to] {
|
||||
*l = now;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let reading = (next(3) == 0).then(super::title_memory_reading);
|
||||
let beside = reading.as_ref().and_then(|_| crate::inplace::beside(word, 8));
|
||||
if let Some(to) = beside {
|
||||
@@ -3694,6 +3937,7 @@ mod many_askers_tests {
|
||||
});
|
||||
assert!(by_bits.into_inner() > 0, "the bits answered none");
|
||||
assert!(past.into_inner() > 0, "nothing was put past the guards");
|
||||
assert!(title_beside.into_inner() > 0, "nothing of the title's was put in beside the guards");
|
||||
// (The view stays: at its address other tests' memory would be told
|
||||
// of these writes.)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user