Skip to content

Add memory page size alignment for madvise, mlock and munlock - #19

Open
Eyob94 wants to merge 3 commits into
quininer:masterfrom
Eyob94:master
Open

Add memory page size alignment for madvise, mlock and munlock#19
Eyob94 wants to merge 3 commits into
quininer:masterfrom
Eyob94:master

Conversation

@Eyob94

@Eyob94 Eyob94 commented Sep 8, 2024

Copy link
Copy Markdown

Improved memory alignment in mlock and munlock functions(including madvise) to ensure proper page boundary handling. This adjustment aligns memory regions to the system's page size across unix systems

@quininer quininer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mlock now implicitly requires user to guarantee address alignment, and I agree that changing it is good.

Comment thread src/mlock.rs
/// Retrieve page size of the system
///
/// Used for alignment
unsafe fn page_size() -> usize {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the page size cache in alloc mod.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, shouldn't be doing syscalls everytime we mlock/munlock. I've initialized the alloc_init() function and returned the PAGE_SIZE from the alloc module instead

Comment thread src/mlock.rs Outdated
// start address of the page obtained from masked the value of the page size
// with the memory address
//
let start_addr = (addr as usize) & !(page_size - 1);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be put into a separate function and some tests added.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored it out to a separate function, and added a few tests to check whether it returns the right start and end page offsets

@Eyob94
Eyob94 requested a review from quininer September 11, 2024 12:45
Comment thread src/alloc/mod.rs
static ALLOC_INIT: Once = Once::new();
static mut PAGE_SIZE: usize = 0;
pub(crate) static ALLOC_INIT: Once = Once::new();
pub(crate) static mut PAGE_SIZE: usize = 0;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ALLOC_INIT should probably be called PAGE_SIZE_INIT and put in a separate module

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can move it to an init module or something like that. Are you thinking of adding anything else?

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.

2 participants