[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <3ea20eb4-2a88-40bd-a453-bf2ff5d52d03@ryhl.io>
Date: Tue, 15 Oct 2024 20:44:22 +0200
From: Alice Ryhl <alice@...l.io>
To: Boqun Feng <boqun.feng@...il.com>, Alice Ryhl <aliceryhl@...gle.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Andrew Morton
<akpm@...ux-foundation.org>, "Matthew Wilcox (Oracle)"
<willy@...radead.org>, Alex Gaynor <alex.gaynor@...il.com>,
Gary Guo <gary@...yguo.net>, Björn Roy Baron
<bjorn3_gh@...tonmail.com>, Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>,
linux-mm@...ck.org, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] rust: page: add Rust version of PAGE_ALIGN
On 10/15/24 8:33 PM, Boqun Feng wrote:
> On Tue, Oct 15, 2024 at 02:28:28PM +0000, Alice Ryhl wrote:
>> 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`").
>>
>> Signed-off-by: Alice Ryhl <aliceryhl@...gle.com>
>> ---
>> rust/kernel/page.rs | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
>> index 208a006d587c..90846e3fe829 100644
>> --- a/rust/kernel/page.rs
>> +++ b/rust/kernel/page.rs
>> @@ -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.
Good idea.
>> + (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
Powered by blists - more mailing lists