linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: page: add Rust version of PAGE_ALIGN
@ 2024-10-16 11:34 Alice Ryhl
  2024-10-21 18:13 ` Miguel Ojeda
  2024-10-21 18:20 ` John Hubbard
  0 siblings, 2 replies; 15+ messages in thread
From: Alice Ryhl @ 2024-10-16 11:34 UTC (permalink / raw)
  To: Miguel Ojeda, Andrew Morton, Matthew Wilcox (Oracle)
  Cc: Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, linux-mm,
	rust-for-linux, linux-kernel, Alice Ryhl

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 fc6e66f4696b ("rust: add
abstraction for `struct page`").

Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Changes in v2:
- Make the function const.
- Address integer overflow in docs.
- Link to v1: https://lore.kernel.org/r/20241015-page-align-v1-1-68fbd8b6d10c@google.com
---
 rust/kernel/page.rs | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 208a006d587c..9ef01929e7d0 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -20,6 +20,15 @@
 /// A bitmask that gives the page containing a given address.
 pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
 
+/// Round up the given number to the next multiple of `PAGE_SIZE`.
+///
+/// It is incorrect to pass an address where the next multiple of `PAGE_SIZE` doesn't fit in a
+/// `usize`.
+pub const fn page_align(addr: usize) -> usize {
+    // Brackets around PAGE_SIZE-1 to avoid triggering overflow sanitizers in the wrong cases.
+    (addr + (PAGE_SIZE - 1)) & PAGE_MASK
+}
+
 /// A pointer to a page that owns the page allocation.
 ///
 /// # Invariants

---
base-commit: 8d8f785ceb21b9a0de11e05b811cc52d6fa79318
change-id: 20241015-page-align-7e5fa4c751be

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2024-10-21 19:52 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-16 11:34 [PATCH v2] rust: page: add Rust version of PAGE_ALIGN Alice Ryhl
2024-10-21 18:13 ` Miguel Ojeda
2024-10-21 18:20 ` John Hubbard
2024-10-21 18:29   ` Miguel Ojeda
2024-10-21 18:34     ` John Hubbard
2024-10-21 18:37       ` Miguel Ojeda
2024-10-21 18:41         ` John Hubbard
2024-10-21 18:59           ` Alice Ryhl
2024-10-21 19:08             ` John Hubbard
2024-10-21 19:26               ` Alice Ryhl
2024-10-21 19:34                 ` John Hubbard
2024-10-21 19:49                   ` Alice Ryhl
2024-10-21 19:51                     ` John Hubbard
2024-10-21 18:32   ` Alice Ryhl
2024-10-21 18:36     ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox