Skip to content
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

Merged
merged 5 commits into from
Dec 19, 2023
Merged

mm/vm: minor cleanups #183

merged 5 commits into from
Dec 19, 2023

Conversation

00xc
Copy link
Member

@00xc 00xc commented Dec 14, 2023

Minor cleanup and readability changes.

00xc added 5 commits December 14, 2023 15:04
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>
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);
Copy link
Member

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.

Copy link
Member Author

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 :)

@joergroedel joergroedel merged commit 7a85454 into coconut-svsm:main Dec 19, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants