-
Notifications
You must be signed in to change notification settings - Fork 49
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
mm/vm: minor cleanups #183
Conversation
Use a strongly typed page size in the memory mapping interfaces Signed-off-by: Carlos López <carlos.lopez@suse.com>
Signed-off-by: Carlos López <carlos.lopez@suse.com>
Clean up control flow in VMR::handle_page_fault() to reduce indentation and make code easier to read. Signed-off-by: Carlos López <carlos.lopez@suse.com>
Signed-off-by: Carlos López <carlos.lopez@suse.com>
Clean up control flow in VMFileMapping::handle_page_fault() to reduce indentation and make code easier to read. Signed-off-by: Carlos López <carlos.lopez@suse.com>
src/mm/vm/mapping/file_mapping.rs
Outdated
if write { | ||
if let Some(write_copy) = self.write_copy.as_mut() { | ||
// This is a writeable region with copy-on-write access. The | ||
// page fault will have occurred because the page has not yet | ||
// been allocated. Allocate a page and copy the readonly source | ||
// page into the new writeable page. | ||
let offset_aligned = offset & !(page_size - 1); | ||
let offset_aligned = offset & !(page_size_bytes - 1); |
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.
What about adding PageSize::mask()
?
I mean something like this:
diff --git a/src/mm/vm/mapping/file_mapping.rs b/src/mm/vm/mapping/file_mapping.rs
index 51540aa..a687442 100644
--- a/src/mm/vm/mapping/file_mapping.rs
+++ b/src/mm/vm/mapping/file_mapping.rs
@@ -222,7 +222,6 @@ impl VirtualMapping for VMFileMapping {
write: bool,
) -> Result<VMPageFaultResolution, SvsmError> {
let page_size = self.page_size();
- let page_size_bytes = usize::from(page_size);
if !write {
return Err(SvsmError::Mem);
@@ -236,7 +235,7 @@ impl VirtualMapping for VMFileMapping {
// page fault will have occurred because the page has not yet
// been allocated. Allocate a page and copy the readonly source
// page into the new writeable page.
- let offset_aligned = offset & !(page_size_bytes - 1);
+ let offset_aligned = offset & page_size.mask();
write_copy.get_alloc_mut().alloc_page(offset_aligned)?;
let paddr_new_page = write_copy.map(offset_aligned).ok_or(SvsmError::Mem)?;
copy_page(vmr, &self.file, offset_aligned, paddr_new_page, page_size)?;
diff --git a/src/types.rs b/src/types.rs
index 770b213..f805cb9 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -17,6 +17,12 @@ pub enum PageSize {
Huge,
}
+impl PageSize {
+ pub fn mask(self) -> usize {
+ !(usize::from(self) - 1)
+ }
+}
+
impl From<PageSize> for usize {
fn from(psize: PageSize) -> Self {
match psize {
We can do also in another PR if you prefer to keep this simple.
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.
Probably best left for a separate PR :)
Minor cleanup and readability changes.