Skip to content

Commit ca43226

Browse files
rbradfordalexandruag
authored andcommitted
volatile_memory: Only use slow path when needed
Where small objects are those objects that are less then the native data width for the platform. This ensure that volatile and alignment safe read/writes are used when updating structures that are sensitive to this such as virtio devices where the spec requires writes to be atomic. Fixes: cloud-hypervisor/cloud-hypervisor#1258 Fixes: #100 Signed-off-by: Rob Bradford <[email protected]>
1 parent 54c8934 commit ca43226

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 85.4,
2+
"coverage_score": 85.5,
33
"exclude_path": "mmap_windows.rs",
44
"crate_features": "backend-mmap,backend-atomic"
55
}

src/volatile_memory.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ mod copy_slice_impl {
10821082
}
10831083
}
10841084

1085-
pub(super) fn copy_slice(dst: &mut [u8], src: &[u8]) -> usize {
1085+
fn copy_slice_volatile(dst: &mut [u8], src: &[u8]) -> usize {
10861086
let total = min(src.len(), dst.len());
10871087
let mut left = total;
10881088

@@ -1110,6 +1110,17 @@ mod copy_slice_impl {
11101110

11111111
total
11121112
}
1113+
1114+
pub(super) fn copy_slice(dst: &mut [u8], src: &[u8]) -> usize {
1115+
let total = min(src.len(), dst.len());
1116+
if total <= size_of::<usize>() {
1117+
copy_slice_volatile(dst, src);
1118+
} else {
1119+
dst[..total].copy_from_slice(&src[..total]);
1120+
}
1121+
1122+
total
1123+
}
11131124
}
11141125

11151126
#[cfg(test)]

0 commit comments

Comments
 (0)