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

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Eyob94
Copy link

@Eyob94 Eyob94 commented Sep 8, 2024

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

Copy link
Owner

@quininer quininer left a comment

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.

/// Retrieve page size of the system
///
/// Used for alignment
unsafe fn page_size() -> usize {
Copy link
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
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

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