Skip to content

Commit 6e86292

Browse files
Darksonnojeda
authored andcommitted
rust: page: add Rust version of PAGE_ALIGN
This is a useful for helper for working with indices into buffers that consist of several pages. I forgot to include it when I added PAGE_SIZE and PAGE_MASK for the same purpose in commit fc6e66f ("rust: add abstraction for `struct page`"). Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Added intra-doc links, formatted comment and replaced "Brackets" with "Parentheses" as discussed in the list. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent d4d7c05 commit 6e86292

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

rust/kernel/page.rs

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ pub const PAGE_SIZE: usize = bindings::PAGE_SIZE;
2020
/// A bitmask that gives the page containing a given address.
2121
pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
2222

23+
/// Round up the given number to the next multiple of [`PAGE_SIZE`].
24+
///
25+
/// It is incorrect to pass an address where the next multiple of [`PAGE_SIZE`] doesn't fit in a
26+
/// [`usize`].
27+
pub const fn page_align(addr: usize) -> usize {
28+
// Parentheses around `PAGE_SIZE - 1` to avoid triggering overflow sanitizers in the wrong
29+
// cases.
30+
(addr + (PAGE_SIZE - 1)) & PAGE_MASK
31+
}
32+
2333
/// A pointer to a page that owns the page allocation.
2434
///
2535
/// # Invariants

0 commit comments

Comments
 (0)