Skip to contentSkip to conversation

rust: page: add Rust version of PAGE_ALIGN

PATCH Superseded Alice Ryhl 3 participants 4 messages Oct 15, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

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

---
 rust/kernel/page.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/kernel/page.rs +5 -0
index 208a006d587c..90846e3fe829 100644
@@ -20,6 +20,11 @@
 /// A bitmask that gives the page containing a given address.
 pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
 
+/// Round up the given number to a multiple of `PAGE_SIZE`.
+pub fn page_align(addr: usize) -> usize {
+    (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,

Signature · 1 line
Alice Ryhl <[email protected]>
#1 lore raw
Quoted 15 lines · 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").

---
 rust/kernel/page.rs | 5 +++++
 1 file changed, 5 insertions(+)
rust/kernel/page.rs +0 -0
index 208a006d587c..90846e3fe829 100644

rust/kernel/page.rs line 24

Quoted 5 lines
rust/kernel/page.rs +2 -0
@@ -20,6 +20,11 @@
 /// A bitmask that gives the page containing a given address.
 pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
 
+/// Round up the given number to a multiple of `PAGE_SIZE`.
+pub fn page_align(addr: usize) -> usize {

Make it a const function? For example, there is a VMBUS_RING_SIZE() macro in C that calculates a const number for a vmbus ringbuffer size, so it will be useful.

rust/kernel/page.rs line 25

Quoted 1 line
rust/kernel/page.rs +1 -0
+    (addr + (PAGE_SIZE - 1)) & PAGE_MASK

I guess overflows are unexpected, i.e. the users should not pass a addr that > usize::MAX - PAGE_SIZE + 1?

Regards,
Boqun

Quoted 14 lines
rust/kernel/page.rs +2 -1
+}
+
 /// A pointer to a page that owns the page allocation.
 ///
 /// # Invariants
 
---

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

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
#2 lore raw
Quoted 19 lines · Boqun Feng
Quoted 18 lines · 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").

---
  rust/kernel/page.rs | 5 +++++
  1 file changed, 5 insertions(+)
rust/kernel/page.rs +0 -0
index 208a006d587c..90846e3fe829 100644
@@ -20,6 +20,11 @@
  /// A bitmask that gives the page containing a given address.
  pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
 
Quoted 6 lines

rust/kernel/page.rs line 24

Quoted 2 lines
rust/kernel/page.rs +2 -0
+/// Round up the given number to a multiple of `PAGE_SIZE`.
+pub fn page_align(addr: usize) -> usize {

Make it a const function? For example, there is a VMBUS_RING_SIZE() macro in C that calculates a const number for a vmbus ringbuffer size, so it will be useful.

Good idea.

Quoted 4 lines

rust/kernel/page.rs line 25

Quoted 1 line
rust/kernel/page.rs +1 -0
+    (addr + (PAGE_SIZE - 1)) & PAGE_MASK

I guess overflows are unexpected, i.e. the users should not pass a addr that > usize::MAX - PAGE_SIZE + 1?

Correct. If this wraps around to zero, that's incorrect. Note that the minus one is in brackets to only trigger overflow detection in the right cases.

I can mention this in the docs.

Alice

Reviewed-by
#3 lore raw
Quoted 26 lines · Alice Ryhl
Quoted 11 lines · Boqun Feng
Quoted 10 lines · 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").

---
  rust/kernel/page.rs | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 13 lines

rust/kernel/page.rs line 23

Quoted 9 lines
rust/kernel/page.rs +2 -0
index 208a006d587c..90846e3fe829 100644
@@ -20,6 +20,11 @@
  /// A bitmask that gives the page containing a given address.
  pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
+/// Round up the given number to a multiple of `PAGE_SIZE`.
+pub fn page_align(addr: usize) -> usize {

Make it a const function? For example, there is a VMBUS_RING_SIZE() macro in C that calculates a const number for a vmbus ringbuffer size, so it will be useful.

Quoted 9 lines

Good idea.

Quoted 4 lines

rust/kernel/page.rs line 25

Quoted 1 line
rust/kernel/page.rs +1 -0
+    (addr + (PAGE_SIZE - 1)) & PAGE_MASK

I guess overflows are unexpected, i.e. the users should not pass a addr that > usize::MAX - PAGE_SIZE + 1?

Correct. If this wraps around to zero, that's incorrect. Note that the minus one is in brackets to only trigger overflow detection in the right cases.

Make senses, with the const change and some docs on the overflow case, feel free to add:

Regards,
Boqun

Quoted 3 lines

I can mention this in the docs.

Alice