rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
@ 2025-11-21  4:04 bshephar
  2025-11-25  7:41 ` bshephar
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: bshephar @ 2025-11-21  4:04 UTC (permalink / raw)
  To: dakr, acourbot, joelagnelf, jhubbard, airlied
  Cc: rust-for-linux, nouveau, brendan.shephard

Use page::page_align for GEM object memory allocation to ensure the
allocation is page aligned. This ensures that the allocation is page
aligned with the system in cases where 4096 is not the default.
For example on 16k or 64k aarch64 systems this allocation should be
aligned accordingly.

Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
---
 drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
index 2760ba4f3450..a07e922e25ef 100644
--- a/drivers/gpu/drm/nova/gem.rs
+++ b/drivers/gpu/drm/nova/gem.rs
@@ -3,6 +3,10 @@
 use kernel::{
     drm,
     drm::{gem, gem::BaseObject},
+    page::{
+        page_align,
+        PAGE_SIZE, //
+    },
     prelude::*,
     sync::aref::ARef,
 };
@@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
 impl NovaObject {
     /// Create a new DRM GEM object.
     pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
-        let aligned_size = size.next_multiple_of(1 << 12);
-
-        if size == 0 || size > aligned_size {
+        // Check for 0 size or potential usize overflow before calling page_align
+        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
             return Err(EINVAL);
         }

+        let aligned_size = page_align(size);
+
         gem::Object::new(dev, aligned_size)
     }

--

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-21  4:04 [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size bshephar
@ 2025-11-25  7:41 ` bshephar
  2025-11-25  9:23 ` Alice Ryhl
  2025-11-25 14:28 ` Alexandre Courbot
  2 siblings, 0 replies; 17+ messages in thread
From: bshephar @ 2025-11-25  7:41 UTC (permalink / raw)
  To: dakr, acourbot, joelagnelf, jhubbard, airlied
  Cc: rust-for-linux, nouveau, brendan.shephard

Should also note that it’s currently possible to overflow usize::MAX here which causes a panic. This patch is also addressing
that by moving the overflow check to before the page alignment. If a request from user-space is made for a size that is
Within one PAGE_SIZE of usize::MAX, then it will panic when we try to use size.next_multiple_of().

So even if we want to keep the 4k page alignments regardless of the underlying CPU architecture, we should at least do the
Overflow check, and use page_align() instead of next_multiple_of().

> On 21 Nov 2025, at 2:04 pm, bshephar@bne-home.net wrote:
> 
> Use page::page_align for GEM object memory allocation to ensure the
> allocation is page aligned. This ensures that the allocation is page
> aligned with the system in cases where 4096 is not the default.
> For example on 16k or 64k aarch64 systems this allocation should be
> aligned accordingly.
> 
> Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> ---
> drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> index 2760ba4f3450..a07e922e25ef 100644
> --- a/drivers/gpu/drm/nova/gem.rs
> +++ b/drivers/gpu/drm/nova/gem.rs
> @@ -3,6 +3,10 @@
> use kernel::{
>     drm,
>     drm::{gem, gem::BaseObject},
> +    page::{
> +        page_align,
> +        PAGE_SIZE, //
> +    },
>     prelude::*,
>     sync::aref::ARef,
> };
> @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
> impl NovaObject {
>     /// Create a new DRM GEM object.
>     pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> -        let aligned_size = size.next_multiple_of(1 << 12);
> -
> -        if size == 0 || size > aligned_size {
> +        // Check for 0 size or potential usize overflow before calling page_align
> +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
>             return Err(EINVAL);
>         }
> 
> +        let aligned_size = page_align(size);
> +
>         gem::Object::new(dev, aligned_size)
>     }
> 
> --


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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-21  4:04 [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size bshephar
  2025-11-25  7:41 ` bshephar
@ 2025-11-25  9:23 ` Alice Ryhl
  2025-11-26  5:49   ` Brendan Shephard
  2025-11-25 14:28 ` Alexandre Courbot
  2 siblings, 1 reply; 17+ messages in thread
From: Alice Ryhl @ 2025-11-25  9:23 UTC (permalink / raw)
  To: bshephar
  Cc: dakr, acourbot, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard

On Fri, Nov 21, 2025 at 02:04:28PM +1000, bshephar@bne-home.net wrote:
> Use page::page_align for GEM object memory allocation to ensure the
> allocation is page aligned. This ensures that the allocation is page
> aligned with the system in cases where 4096 is not the default.
> For example on 16k or 64k aarch64 systems this allocation should be
> aligned accordingly.
> 
> Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> ---
>  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> index 2760ba4f3450..a07e922e25ef 100644
> --- a/drivers/gpu/drm/nova/gem.rs
> +++ b/drivers/gpu/drm/nova/gem.rs
> @@ -3,6 +3,10 @@
>  use kernel::{
>      drm,
>      drm::{gem, gem::BaseObject},
> +    page::{
> +        page_align,
> +        PAGE_SIZE, //
> +    },
>      prelude::*,
>      sync::aref::ARef,
>  };
> @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
>  impl NovaObject {
>      /// Create a new DRM GEM object.
>      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> -        let aligned_size = size.next_multiple_of(1 << 12);
> -
> -        if size == 0 || size > aligned_size {
> +        // Check for 0 size or potential usize overflow before calling page_align
> +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {

Maybe this should use isize::MAX as the maximum size instead? That's a
pretty common maximum size for allocations in Rust and big enough for
everyone.

Alice

>              return Err(EINVAL);
>          }
> 
> +        let aligned_size = page_align(size);
> +
>          gem::Object::new(dev, aligned_size)
>      }
> 
> --

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-21  4:04 [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size bshephar
  2025-11-25  7:41 ` bshephar
  2025-11-25  9:23 ` Alice Ryhl
@ 2025-11-25 14:28 ` Alexandre Courbot
  2025-11-25 14:41   ` Alice Ryhl
  2 siblings, 1 reply; 17+ messages in thread
From: Alexandre Courbot @ 2025-11-25 14:28 UTC (permalink / raw)
  To: bshephar, dakr, acourbot, joelagnelf, jhubbard, airlied
  Cc: rust-for-linux, nouveau, brendan.shephard

On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> Use page::page_align for GEM object memory allocation to ensure the
> allocation is page aligned. This ensures that the allocation is page
> aligned with the system in cases where 4096 is not the default.
> For example on 16k or 64k aarch64 systems this allocation should be
> aligned accordingly.
>
> Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> ---
>  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> index 2760ba4f3450..a07e922e25ef 100644
> --- a/drivers/gpu/drm/nova/gem.rs
> +++ b/drivers/gpu/drm/nova/gem.rs
> @@ -3,6 +3,10 @@
>  use kernel::{
>      drm,
>      drm::{gem, gem::BaseObject},
> +    page::{
> +        page_align,
> +        PAGE_SIZE, //
> +    },
>      prelude::*,
>      sync::aref::ARef,
>  };
> @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
>  impl NovaObject {
>      /// Create a new DRM GEM object.
>      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> -        let aligned_size = size.next_multiple_of(1 << 12);
> -
> -        if size == 0 || size > aligned_size {
> +        // Check for 0 size or potential usize overflow before calling page_align
> +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {

`PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
I'll admit it looks better as a placeholder. :) But the actual alignment
will eventually be provided elsewhere.

>              return Err(EINVAL);
>          }
>
> +        let aligned_size = page_align(size);

`page_align` won't panic on overflow, but it will still return an
invalid size. This is a job for `kernel::ptr::Alignment`, which let's
you return an error when an overflow occurs.

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-25 14:28 ` Alexandre Courbot
@ 2025-11-25 14:41   ` Alice Ryhl
  2025-11-25 14:55     ` Alexandre Courbot
  0 siblings, 1 reply; 17+ messages in thread
From: Alice Ryhl @ 2025-11-25 14:41 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard

On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> > Use page::page_align for GEM object memory allocation to ensure the
> > allocation is page aligned. This ensures that the allocation is page
> > aligned with the system in cases where 4096 is not the default.
> > For example on 16k or 64k aarch64 systems this allocation should be
> > aligned accordingly.
> >
> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> > ---
> >  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
> >  1 file changed, 8 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> > index 2760ba4f3450..a07e922e25ef 100644
> > --- a/drivers/gpu/drm/nova/gem.rs
> > +++ b/drivers/gpu/drm/nova/gem.rs
> > @@ -3,6 +3,10 @@
> >  use kernel::{
> >      drm,
> >      drm::{gem, gem::BaseObject},
> > +    page::{
> > +        page_align,
> > +        PAGE_SIZE, //
> > +    },
> >      prelude::*,
> >      sync::aref::ARef,
> >  };
> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
> >  impl NovaObject {
> >      /// Create a new DRM GEM object.
> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> > -        let aligned_size = size.next_multiple_of(1 << 12);
> > -
> > -        if size == 0 || size > aligned_size {
> > +        // Check for 0 size or potential usize overflow before calling page_align
> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
>
> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
> I'll admit it looks better as a placeholder. :) But the actual alignment
> will eventually be provided elsewhere.

What about kernels with 16k pages?

> >              return Err(EINVAL);
> >          }
> >
> > +        let aligned_size = page_align(size);
>
> `page_align` won't panic on overflow, but it will still return an
> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> you return an error when an overflow occurs.

The Rust implementation of page_align() is implemented as (addr +
(PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
if the appropriate config options are enabled.

Alice

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-25 14:41   ` Alice Ryhl
@ 2025-11-25 14:55     ` Alexandre Courbot
  2025-11-25 14:59       ` Alice Ryhl
  2025-11-26  6:05       ` Brendan Shephard
  0 siblings, 2 replies; 17+ messages in thread
From: Alexandre Courbot @ 2025-11-25 14:55 UTC (permalink / raw)
  To: Alice Ryhl, Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
>> > Use page::page_align for GEM object memory allocation to ensure the
>> > allocation is page aligned. This ensures that the allocation is page
>> > aligned with the system in cases where 4096 is not the default.
>> > For example on 16k or 64k aarch64 systems this allocation should be
>> > aligned accordingly.
>> >
>> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
>> > ---
>> >  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
>> >  1 file changed, 8 insertions(+), 3 deletions(-)
>> >
>> > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
>> > index 2760ba4f3450..a07e922e25ef 100644
>> > --- a/drivers/gpu/drm/nova/gem.rs
>> > +++ b/drivers/gpu/drm/nova/gem.rs
>> > @@ -3,6 +3,10 @@
>> >  use kernel::{
>> >      drm,
>> >      drm::{gem, gem::BaseObject},
>> > +    page::{
>> > +        page_align,
>> > +        PAGE_SIZE, //
>> > +    },
>> >      prelude::*,
>> >      sync::aref::ARef,
>> >  };
>> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
>> >  impl NovaObject {
>> >      /// Create a new DRM GEM object.
>> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
>> > -        let aligned_size = size.next_multiple_of(1 << 12);
>> > -
>> > -        if size == 0 || size > aligned_size {
>> > +        // Check for 0 size or potential usize overflow before calling page_align
>> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
>>
>> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
>> I'll admit it looks better as a placeholder. :) But the actual alignment
>> will eventually be provided elsewhere.
>
> What about kernels with 16k pages?

The actual alignment should IIUC be a mix of the GPU and kernel's
requirements (GPU can also use a different page size). So no matter what
we pick right now, it won't be great but you are right that PAGE_SIZE
will at least accomodate the kernel.

>
>> >              return Err(EINVAL);
>> >          }
>> >
>> > +        let aligned_size = page_align(size);
>>
>> `page_align` won't panic on overflow, but it will still return an
>> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
>> you return an error when an overflow occurs.
>
> The Rust implementation of page_align() is implemented as (addr +
> (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> if the appropriate config options are enabled.

That's right, I skimmed its code too fast. ^_^; All the more reason to
use `Alignment`.

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-25 14:55     ` Alexandre Courbot
@ 2025-11-25 14:59       ` Alice Ryhl
  2025-11-26  0:31         ` Alexandre Courbot
  2025-11-26  6:05       ` Brendan Shephard
  1 sibling, 1 reply; 17+ messages in thread
From: Alice Ryhl @ 2025-11-25 14:59 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >>
> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> >> > Use page::page_align for GEM object memory allocation to ensure the
> >> > allocation is page aligned. This ensures that the allocation is page
> >> > aligned with the system in cases where 4096 is not the default.
> >> > For example on 16k or 64k aarch64 systems this allocation should be
> >> > aligned accordingly.
> >> >
> >> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> >> > ---
> >> >  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
> >> >  1 file changed, 8 insertions(+), 3 deletions(-)
> >> >
> >> > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> >> > index 2760ba4f3450..a07e922e25ef 100644
> >> > --- a/drivers/gpu/drm/nova/gem.rs
> >> > +++ b/drivers/gpu/drm/nova/gem.rs
> >> > @@ -3,6 +3,10 @@
> >> >  use kernel::{
> >> >      drm,
> >> >      drm::{gem, gem::BaseObject},
> >> > +    page::{
> >> > +        page_align,
> >> > +        PAGE_SIZE, //
> >> > +    },
> >> >      prelude::*,
> >> >      sync::aref::ARef,
> >> >  };
> >> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
> >> >  impl NovaObject {
> >> >      /// Create a new DRM GEM object.
> >> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> >> > -        let aligned_size = size.next_multiple_of(1 << 12);
> >> > -
> >> > -        if size == 0 || size > aligned_size {
> >> > +        // Check for 0 size or potential usize overflow before calling page_align
> >> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
> >>
> >> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
> >> I'll admit it looks better as a placeholder. :) But the actual alignment
> >> will eventually be provided elsewhere.
> >
> > What about kernels with 16k pages?
>
> The actual alignment should IIUC be a mix of the GPU and kernel's
> requirements (GPU can also use a different page size). So no matter what
> we pick right now, it won't be great but you are right that PAGE_SIZE
> will at least accomodate the kernel.

In that case, is PAGE_SIZE not the wrong constant? What's the actually
correct constant here?

> >> >              return Err(EINVAL);
> >> >          }
> >> >
> >> > +        let aligned_size = page_align(size);
> >>
> >> `page_align` won't panic on overflow, but it will still return an
> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> >> you return an error when an overflow occurs.
> >
> > The Rust implementation of page_align() is implemented as (addr +
> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> > if the appropriate config options are enabled.
>
> That's right, I skimmed its code too fast. ^_^; All the more reason to
> use `Alignment`.

Alignment stores values that are powers of two, not multiples of PAGE_SIZE.

Alice

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-25 14:59       ` Alice Ryhl
@ 2025-11-26  0:31         ` Alexandre Courbot
  2025-11-26  9:54           ` Alice Ryhl
  0 siblings, 1 reply; 17+ messages in thread
From: Alexandre Courbot @ 2025-11-26  0:31 UTC (permalink / raw)
  To: Alice Ryhl, Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
> On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>>
>> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
>> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>> >>
>> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
>> >> > Use page::page_align for GEM object memory allocation to ensure the
>> >> > allocation is page aligned. This ensures that the allocation is page
>> >> > aligned with the system in cases where 4096 is not the default.
>> >> > For example on 16k or 64k aarch64 systems this allocation should be
>> >> > aligned accordingly.
>> >> >
>> >> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
>> >> > ---
>> >> >  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
>> >> >  1 file changed, 8 insertions(+), 3 deletions(-)
>> >> >
>> >> > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
>> >> > index 2760ba4f3450..a07e922e25ef 100644
>> >> > --- a/drivers/gpu/drm/nova/gem.rs
>> >> > +++ b/drivers/gpu/drm/nova/gem.rs
>> >> > @@ -3,6 +3,10 @@
>> >> >  use kernel::{
>> >> >      drm,
>> >> >      drm::{gem, gem::BaseObject},
>> >> > +    page::{
>> >> > +        page_align,
>> >> > +        PAGE_SIZE, //
>> >> > +    },
>> >> >      prelude::*,
>> >> >      sync::aref::ARef,
>> >> >  };
>> >> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
>> >> >  impl NovaObject {
>> >> >      /// Create a new DRM GEM object.
>> >> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
>> >> > -        let aligned_size = size.next_multiple_of(1 << 12);
>> >> > -
>> >> > -        if size == 0 || size > aligned_size {
>> >> > +        // Check for 0 size or potential usize overflow before calling page_align
>> >> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
>> >>
>> >> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
>> >> I'll admit it looks better as a placeholder. :) But the actual alignment
>> >> will eventually be provided elsewhere.
>> >
>> > What about kernels with 16k pages?
>>
>> The actual alignment should IIUC be a mix of the GPU and kernel's
>> requirements (GPU can also use a different page size). So no matter what
>> we pick right now, it won't be great but you are right that PAGE_SIZE
>> will at least accomodate the kernel.
>
> In that case, is PAGE_SIZE not the wrong constant? What's the actually
> correct constant here?
>
>> >> >              return Err(EINVAL);
>> >> >          }
>> >> >
>> >> > +        let aligned_size = page_align(size);
>> >>
>> >> `page_align` won't panic on overflow, but it will still return an
>> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
>> >> you return an error when an overflow occurs.
>> >
>> > The Rust implementation of page_align() is implemented as (addr +
>> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
>> > if the appropriate config options are enabled.
>>
>> That's right, I skimmed its code too fast. ^_^; All the more reason to
>> use `Alignment`.
>
> Alignment stores values that are powers of two, not multiples of PAGE_SIZE.

Isn't PAGE_SIZE always a power of two though?

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-25  9:23 ` Alice Ryhl
@ 2025-11-26  5:49   ` Brendan Shephard
  2025-11-26  9:53     ` Alice Ryhl
  0 siblings, 1 reply; 17+ messages in thread
From: Brendan Shephard @ 2025-11-26  5:49 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: dakr, acourbot, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard

On Tue, Nov 25, 2025 at 09:23:59AM +0000, Alice Ryhl wrote:
> On Fri, Nov 21, 2025 at 02:04:28PM +1000, bshephar@bne-home.net wrote:
> >  impl NovaObject {
> >      /// Create a new DRM GEM object.
> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> > -        let aligned_size = size.next_multiple_of(1 << 12);
> > -
> > -        if size == 0 || size > aligned_size {
> > +        // Check for 0 size or potential usize overflow before calling page_align
> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
> 
> Maybe this should use isize::MAX as the maximum size instead? That's a
> pretty common maximum size for allocations in Rust and big enough for
> everyone.
> 
> Alice

Thanks for the review Alice. I used usize here because the page_align()
function specifically mentions that the provided value should not
overflow a ['usize'].
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/page.rs#n30

I don't think that alone needs to be the deciding factor, but it's worth
expressing why I made that decision to begin with. Happy to hear your
thoughts, and if you still feel isize::MAX is more appropriate given
this information.

Brendan




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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-25 14:55     ` Alexandre Courbot
  2025-11-25 14:59       ` Alice Ryhl
@ 2025-11-26  6:05       ` Brendan Shephard
  1 sibling, 0 replies; 17+ messages in thread
From: Brendan Shephard @ 2025-11-26  6:05 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Alice Ryhl, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Tue, Nov 25, 2025 at 11:55:08PM +0900, Alexandre Courbot wrote:
> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> >> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
> >> >  impl NovaObject {
> >> >      /// Create a new DRM GEM object.
> >> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> >> > -        let aligned_size = size.next_multiple_of(1 << 12);
> >> > -
> >> > -        if size == 0 || size > aligned_size {
> >> > +        // Check for 0 size or potential usize overflow before calling page_align
> >> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
> >>
> >> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
> >> I'll admit it looks better as a placeholder. :) But the actual alignment
> >> will eventually be provided elsewhere.
> >
> > What about kernels with 16k pages?
> 
> The actual alignment should IIUC be a mix of the GPU and kernel's
> requirements (GPU can also use a different page size). So no matter what
> we pick right now, it won't be great but you are right that PAGE_SIZE
> will at least accomodate the kernel.
> 

So, maybe what we realistically should be doing is aligning to the
larger page size when comparing system and GPU page sizes?

> >
> >> >              return Err(EINVAL);
> >> >          }
> >> >
> >> > +        let aligned_size = page_align(size);
> >>
> >> `page_align` won't panic on overflow, but it will still return an
> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> >> you return an error when an overflow occurs.
> >
> > The Rust implementation of page_align() is implemented as (addr +
> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> > if the appropriate config options are enabled.
> 
> That's right, I skimmed its code too fast. ^_^; All the more reason to
> use `Alignment`.

We would still need to ensure the value is a multiple of PAGE_SIZE
though right? Like if the user requests a size that is _not_ a multiple
of 2, then we would want to align the value to a PAGE_SIZE. Which is
what the existing logic does, it's just always rounding to the next
multiple of 4096. Maybe I'm missing something about Alignment and I need
to spend some more time looking at it as an alternative here.

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26  5:49   ` Brendan Shephard
@ 2025-11-26  9:53     ` Alice Ryhl
  0 siblings, 0 replies; 17+ messages in thread
From: Alice Ryhl @ 2025-11-26  9:53 UTC (permalink / raw)
  To: Brendan Shephard
  Cc: dakr, acourbot, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard

On Wed, Nov 26, 2025 at 03:49:26PM +1000, Brendan Shephard wrote:
> On Tue, Nov 25, 2025 at 09:23:59AM +0000, Alice Ryhl wrote:
> > On Fri, Nov 21, 2025 at 02:04:28PM +1000, bshephar@bne-home.net wrote:
> > >  impl NovaObject {
> > >      /// Create a new DRM GEM object.
> > >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> > > -        let aligned_size = size.next_multiple_of(1 << 12);
> > > -
> > > -        if size == 0 || size > aligned_size {
> > > +        // Check for 0 size or potential usize overflow before calling page_align
> > > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
> > 
> > Maybe this should use isize::MAX as the maximum size instead? That's a
> > pretty common maximum size for allocations in Rust and big enough for
> > everyone.
> > 
> > Alice
> 
> Thanks for the review Alice. I used usize here because the page_align()
> function specifically mentions that the provided value should not
> overflow a ['usize'].
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/page.rs#n30
> 
> I don't think that alone needs to be the deciding factor, but it's worth
> expressing why I made that decision to begin with. Happy to hear your
> thoughts, and if you still feel isize::MAX is more appropriate given
> this information.

I know that you picked this particular limit to avoid integer overflow.

I think picking a lower limit makes sense because isize::MAX is a
relatively standard choice for maximum allocation sizes. I think it is a
nice choice because it means you can always compute 2*x for any index or
size x without risk of overflow.

But it's up to the nova folks, not me.

Alice

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26  0:31         ` Alexandre Courbot
@ 2025-11-26  9:54           ` Alice Ryhl
  2025-11-26 13:22             ` Alexandre Courbot
  0 siblings, 1 reply; 17+ messages in thread
From: Alice Ryhl @ 2025-11-26  9:54 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Wed, Nov 26, 2025 at 09:31:46AM +0900, Alexandre Courbot wrote:
> On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
> > On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >>
> >> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> >> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >>
> >> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> >> >> > Use page::page_align for GEM object memory allocation to ensure the
> >> >> > allocation is page aligned. This ensures that the allocation is page
> >> >> > aligned with the system in cases where 4096 is not the default.
> >> >> > For example on 16k or 64k aarch64 systems this allocation should be
> >> >> > aligned accordingly.
> >> >> >
> >> >> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> >> >> > ---
> >> >> >  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
> >> >> >  1 file changed, 8 insertions(+), 3 deletions(-)
> >> >> >
> >> >> > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> >> >> > index 2760ba4f3450..a07e922e25ef 100644
> >> >> > --- a/drivers/gpu/drm/nova/gem.rs
> >> >> > +++ b/drivers/gpu/drm/nova/gem.rs
> >> >> > @@ -3,6 +3,10 @@
> >> >> >  use kernel::{
> >> >> >      drm,
> >> >> >      drm::{gem, gem::BaseObject},
> >> >> > +    page::{
> >> >> > +        page_align,
> >> >> > +        PAGE_SIZE, //
> >> >> > +    },
> >> >> >      prelude::*,
> >> >> >      sync::aref::ARef,
> >> >> >  };
> >> >> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
> >> >> >  impl NovaObject {
> >> >> >      /// Create a new DRM GEM object.
> >> >> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
> >> >> > -        let aligned_size = size.next_multiple_of(1 << 12);
> >> >> > -
> >> >> > -        if size == 0 || size > aligned_size {
> >> >> > +        // Check for 0 size or potential usize overflow before calling page_align
> >> >> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
> >> >>
> >> >> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
> >> >> I'll admit it looks better as a placeholder. :) But the actual alignment
> >> >> will eventually be provided elsewhere.
> >> >
> >> > What about kernels with 16k pages?
> >>
> >> The actual alignment should IIUC be a mix of the GPU and kernel's
> >> requirements (GPU can also use a different page size). So no matter what
> >> we pick right now, it won't be great but you are right that PAGE_SIZE
> >> will at least accomodate the kernel.
> >
> > In that case, is PAGE_SIZE not the wrong constant? What's the actually
> > correct constant here?
> >
> >> >> >              return Err(EINVAL);
> >> >> >          }
> >> >> >
> >> >> > +        let aligned_size = page_align(size);
> >> >>
> >> >> `page_align` won't panic on overflow, but it will still return an
> >> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> >> >> you return an error when an overflow occurs.
> >> >
> >> > The Rust implementation of page_align() is implemented as (addr +
> >> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> >> > if the appropriate config options are enabled.
> >>
> >> That's right, I skimmed its code too fast. ^_^; All the more reason to
> >> use `Alignment`.
> >
> > Alignment stores values that are powers of two, not multiples of PAGE_SIZE.
> 
> Isn't PAGE_SIZE always a power of two though?

Yes it is. Maybe you can elaborate on how you wanted to use Alignment?
It sounds like you have something different in mind than what I thought.

Alice

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26  9:54           ` Alice Ryhl
@ 2025-11-26 13:22             ` Alexandre Courbot
  2025-11-26 13:36               ` Alice Ryhl
  0 siblings, 1 reply; 17+ messages in thread
From: Alexandre Courbot @ 2025-11-26 13:22 UTC (permalink / raw)
  To: Alice Ryhl, Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Wed Nov 26, 2025 at 6:54 PM JST, Alice Ryhl wrote:
> On Wed, Nov 26, 2025 at 09:31:46AM +0900, Alexandre Courbot wrote:
>> On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
>> > On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>> >>
>> >> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
>> >> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>> >> >>
>> >> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
>> >> >> > Use page::page_align for GEM object memory allocation to ensure the
>> >> >> > allocation is page aligned. This ensures that the allocation is page
>> >> >> > aligned with the system in cases where 4096 is not the default.
>> >> >> > For example on 16k or 64k aarch64 systems this allocation should be
>> >> >> > aligned accordingly.
>> >> >> >
>> >> >> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
>> >> >> > ---
>> >> >> >  drivers/gpu/drm/nova/gem.rs | 11 ++++++++---
>> >> >> >  1 file changed, 8 insertions(+), 3 deletions(-)
>> >> >> >
>> >> >> > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
>> >> >> > index 2760ba4f3450..a07e922e25ef 100644
>> >> >> > --- a/drivers/gpu/drm/nova/gem.rs
>> >> >> > +++ b/drivers/gpu/drm/nova/gem.rs
>> >> >> > @@ -3,6 +3,10 @@
>> >> >> >  use kernel::{
>> >> >> >      drm,
>> >> >> >      drm::{gem, gem::BaseObject},
>> >> >> > +    page::{
>> >> >> > +        page_align,
>> >> >> > +        PAGE_SIZE, //
>> >> >> > +    },
>> >> >> >      prelude::*,
>> >> >> >      sync::aref::ARef,
>> >> >> >  };
>> >> >> > @@ -27,12 +31,13 @@ fn new(_dev: &NovaDevice, _size: usize) -> impl PinInit<Self, Error> {
>> >> >> >  impl NovaObject {
>> >> >> >      /// Create a new DRM GEM object.
>> >> >> >      pub(crate) fn new(dev: &NovaDevice, size: usize) -> Result<ARef<gem::Object<Self>>> {
>> >> >> > -        let aligned_size = size.next_multiple_of(1 << 12);
>> >> >> > -
>> >> >> > -        if size == 0 || size > aligned_size {
>> >> >> > +        // Check for 0 size or potential usize overflow before calling page_align
>> >> >> > +        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
>> >> >>
>> >> >> `PAGE_SIZE` here is no more correct than the hardcoded `1 << 12` - well,
>> >> >> I'll admit it looks better as a placeholder. :) But the actual alignment
>> >> >> will eventually be provided elsewhere.
>> >> >
>> >> > What about kernels with 16k pages?
>> >>
>> >> The actual alignment should IIUC be a mix of the GPU and kernel's
>> >> requirements (GPU can also use a different page size). So no matter what
>> >> we pick right now, it won't be great but you are right that PAGE_SIZE
>> >> will at least accomodate the kernel.
>> >
>> > In that case, is PAGE_SIZE not the wrong constant? What's the actually
>> > correct constant here?
>> >
>> >> >> >              return Err(EINVAL);
>> >> >> >          }
>> >> >> >
>> >> >> > +        let aligned_size = page_align(size);
>> >> >>
>> >> >> `page_align` won't panic on overflow, but it will still return an
>> >> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
>> >> >> you return an error when an overflow occurs.
>> >> >
>> >> > The Rust implementation of page_align() is implemented as (addr +
>> >> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
>> >> > if the appropriate config options are enabled.
>> >>
>> >> That's right, I skimmed its code too fast. ^_^; All the more reason to
>> >> use `Alignment`.
>> >
>> > Alignment stores values that are powers of two, not multiples of PAGE_SIZE.
>> 
>> Isn't PAGE_SIZE always a power of two though?
>
> Yes it is. Maybe you can elaborate on how you wanted to use Alignment?
> It sounds like you have something different in mind than what I thought.

I thought we could just do something like this:

    use kernel::ptr::{Alignable, Alignment};

    let aligned_size = size
        .align_up(Alignment::new::<PAGE_SIZE>())
        .ok_or(EOVERFLOW)?;

(maybe we could also have that `Alignment<PAGE_SIZE>` stored as a const
in `page.rs` for convenience, as it might be used often)

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26 13:22             ` Alexandre Courbot
@ 2025-11-26 13:36               ` Alice Ryhl
  2025-11-26 14:00                 ` Alexandre Courbot
  0 siblings, 1 reply; 17+ messages in thread
From: Alice Ryhl @ 2025-11-26 13:36 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Wed, Nov 26, 2025 at 10:22:14PM +0900, Alexandre Courbot wrote:
> On Wed Nov 26, 2025 at 6:54 PM JST, Alice Ryhl wrote:
> > On Wed, Nov 26, 2025 at 09:31:46AM +0900, Alexandre Courbot wrote:
> >> On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
> >> > On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >>
> >> >> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> >> >> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >> >>
> >> >> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> >> >> >> >              return Err(EINVAL);
> >> >> >> >          }
> >> >> >> >
> >> >> >> > +        let aligned_size = page_align(size);
> >> >> >>
> >> >> >> `page_align` won't panic on overflow, but it will still return an
> >> >> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> >> >> >> you return an error when an overflow occurs.
> >> >> >
> >> >> > The Rust implementation of page_align() is implemented as (addr +
> >> >> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> >> >> > if the appropriate config options are enabled.
> >> >>
> >> >> That's right, I skimmed its code too fast. ^_^; All the more reason to
> >> >> use `Alignment`.
> >> >
> >> > Alignment stores values that are powers of two, not multiples of PAGE_SIZE.
> >> 
> >> Isn't PAGE_SIZE always a power of two though?
> >
> > Yes it is. Maybe you can elaborate on how you wanted to use Alignment?
> > It sounds like you have something different in mind than what I thought.
> 
> I thought we could just do something like this:
> 
>     use kernel::ptr::{Alignable, Alignment};
> 
>     let aligned_size = size
>         .align_up(Alignment::new::<PAGE_SIZE>())
>         .ok_or(EOVERFLOW)?;
> 
> (maybe we could also have that `Alignment<PAGE_SIZE>` stored as a const
> in `page.rs` for convenience, as it might be used often)

If you're trying to round up a number to the next multiple of PAGE_SIZE,
then you should use page_align() because that's exactly what the
function does.

If you think there is something wrong with the design of page_align(),
change it instead of reimplemtning it.

Alice

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26 13:36               ` Alice Ryhl
@ 2025-11-26 14:00                 ` Alexandre Courbot
  2025-11-26 16:24                   ` Alice Ryhl
  2025-11-26 21:14                   ` Brendan Shephard
  0 siblings, 2 replies; 17+ messages in thread
From: Alexandre Courbot @ 2025-11-26 14:00 UTC (permalink / raw)
  To: Alice Ryhl, Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Wed Nov 26, 2025 at 10:36 PM JST, Alice Ryhl wrote:
> On Wed, Nov 26, 2025 at 10:22:14PM +0900, Alexandre Courbot wrote:
>> On Wed Nov 26, 2025 at 6:54 PM JST, Alice Ryhl wrote:
>> > On Wed, Nov 26, 2025 at 09:31:46AM +0900, Alexandre Courbot wrote:
>> >> On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
>> >> > On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>> >> >>
>> >> >> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
>> >> >> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>> >> >> >>
>> >> >> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
>> >> >> >> >              return Err(EINVAL);
>> >> >> >> >          }
>> >> >> >> >
>> >> >> >> > +        let aligned_size = page_align(size);
>> >> >> >>
>> >> >> >> `page_align` won't panic on overflow, but it will still return an
>> >> >> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
>> >> >> >> you return an error when an overflow occurs.
>> >> >> >
>> >> >> > The Rust implementation of page_align() is implemented as (addr +
>> >> >> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
>> >> >> > if the appropriate config options are enabled.
>> >> >>
>> >> >> That's right, I skimmed its code too fast. ^_^; All the more reason to
>> >> >> use `Alignment`.
>> >> >
>> >> > Alignment stores values that are powers of two, not multiples of PAGE_SIZE.
>> >> 
>> >> Isn't PAGE_SIZE always a power of two though?
>> >
>> > Yes it is. Maybe you can elaborate on how you wanted to use Alignment?
>> > It sounds like you have something different in mind than what I thought.
>> 
>> I thought we could just do something like this:
>> 
>>     use kernel::ptr::{Alignable, Alignment};
>> 
>>     let aligned_size = size
>>         .align_up(Alignment::new::<PAGE_SIZE>())
>>         .ok_or(EOVERFLOW)?;
>> 
>> (maybe we could also have that `Alignment<PAGE_SIZE>` stored as a const
>> in `page.rs` for convenience, as it might be used often)
>
> If you're trying to round up a number to the next multiple of PAGE_SIZE,
> then you should use page_align() because that's exactly what the
> function does.
>
> If you think there is something wrong with the design of page_align(),
> change it instead of reimplemtning it.

In that case I would suggest that `page_align` returns an `Option`
instead of potentially panicking. Does that sound reasonable? I cannot
find any user of it in the Rust code for now.

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26 14:00                 ` Alexandre Courbot
@ 2025-11-26 16:24                   ` Alice Ryhl
  2025-11-26 21:14                   ` Brendan Shephard
  1 sibling, 0 replies; 17+ messages in thread
From: Alice Ryhl @ 2025-11-26 16:24 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: bshephar, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Wed, Nov 26, 2025 at 3:00 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> On Wed Nov 26, 2025 at 10:36 PM JST, Alice Ryhl wrote:
> > On Wed, Nov 26, 2025 at 10:22:14PM +0900, Alexandre Courbot wrote:
> >> On Wed Nov 26, 2025 at 6:54 PM JST, Alice Ryhl wrote:
> >> > On Wed, Nov 26, 2025 at 09:31:46AM +0900, Alexandre Courbot wrote:
> >> >> On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
> >> >> > On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >> >>
> >> >> >> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> >> >> >> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >> >> >>
> >> >> >> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> >> >> >> >> >              return Err(EINVAL);
> >> >> >> >> >          }
> >> >> >> >> >
> >> >> >> >> > +        let aligned_size = page_align(size);
> >> >> >> >>
> >> >> >> >> `page_align` won't panic on overflow, but it will still return an
> >> >> >> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> >> >> >> >> you return an error when an overflow occurs.
> >> >> >> >
> >> >> >> > The Rust implementation of page_align() is implemented as (addr +
> >> >> >> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> >> >> >> > if the appropriate config options are enabled.
> >> >> >>
> >> >> >> That's right, I skimmed its code too fast. ^_^; All the more reason to
> >> >> >> use `Alignment`.
> >> >> >
> >> >> > Alignment stores values that are powers of two, not multiples of PAGE_SIZE.
> >> >>
> >> >> Isn't PAGE_SIZE always a power of two though?
> >> >
> >> > Yes it is. Maybe you can elaborate on how you wanted to use Alignment?
> >> > It sounds like you have something different in mind than what I thought.
> >>
> >> I thought we could just do something like this:
> >>
> >>     use kernel::ptr::{Alignable, Alignment};
> >>
> >>     let aligned_size = size
> >>         .align_up(Alignment::new::<PAGE_SIZE>())
> >>         .ok_or(EOVERFLOW)?;
> >>
> >> (maybe we could also have that `Alignment<PAGE_SIZE>` stored as a const
> >> in `page.rs` for convenience, as it might be used often)
> >
> > If you're trying to round up a number to the next multiple of PAGE_SIZE,
> > then you should use page_align() because that's exactly what the
> > function does.
> >
> > If you think there is something wrong with the design of page_align(),
> > change it instead of reimplemtning it.
>
> In that case I would suggest that `page_align` returns an `Option`
> instead of potentially panicking. Does that sound reasonable? I cannot
> find any user of it in the Rust code for now.

That sounds reasonable enough to me.

Alice

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

* Re: [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
  2025-11-26 14:00                 ` Alexandre Courbot
  2025-11-26 16:24                   ` Alice Ryhl
@ 2025-11-26 21:14                   ` Brendan Shephard
  1 sibling, 0 replies; 17+ messages in thread
From: Brendan Shephard @ 2025-11-26 21:14 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Alice Ryhl, dakr, joelagnelf, jhubbard, airlied, rust-for-linux,
	nouveau, brendan.shephard, Nouveau

On Wed, Nov 26, 2025 at 11:00:00PM +0900, Alexandre Courbot wrote:
> On Wed Nov 26, 2025 at 10:36 PM JST, Alice Ryhl wrote:
> > On Wed, Nov 26, 2025 at 10:22:14PM +0900, Alexandre Courbot wrote:
> >> On Wed Nov 26, 2025 at 6:54 PM JST, Alice Ryhl wrote:
> >> > On Wed, Nov 26, 2025 at 09:31:46AM +0900, Alexandre Courbot wrote:
> >> >> On Tue Nov 25, 2025 at 11:59 PM JST, Alice Ryhl wrote:
> >> >> > On Tue, Nov 25, 2025 at 3:55 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >> >>
> >> >> >> On Tue Nov 25, 2025 at 11:41 PM JST, Alice Ryhl wrote:
> >> >> >> > On Tue, Nov 25, 2025 at 3:29 PM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >> >> >> >>
> >> >> >> >> On Fri Nov 21, 2025 at 1:04 PM JST, bshephar wrote:
> >> >> >> >> >              return Err(EINVAL);
> >> >> >> >> >          }
> >> >> >> >> >
> >> >> >> >> > +        let aligned_size = page_align(size);
> >> >> >> >>
> >> >> >> >> `page_align` won't panic on overflow, but it will still return an
> >> >> >> >> invalid size. This is a job for `kernel::ptr::Alignment`, which let's
> >> >> >> >> you return an error when an overflow occurs.
> >> >> >> >
> >> >> >> > The Rust implementation of page_align() is implemented as (addr +
> >> >> >> > (PAGE_SIZE - 1)) & PAGE_MASK, which definitely will panic on overflow
> >> >> >> > if the appropriate config options are enabled.
> >> >> >>
> >> >> >> That's right, I skimmed its code too fast. ^_^; All the more reason to
> >> >> >> use `Alignment`.
> >> >> >
> >> >> > Alignment stores values that are powers of two, not multiples of PAGE_SIZE.
> >> >> 
> >> >> Isn't PAGE_SIZE always a power of two though?
> >> >
> >> > Yes it is. Maybe you can elaborate on how you wanted to use Alignment?
> >> > It sounds like you have something different in mind than what I thought.
> >> 
> >> I thought we could just do something like this:
> >> 
> >>     use kernel::ptr::{Alignable, Alignment};
> >> 
> >>     let aligned_size = size
> >>         .align_up(Alignment::new::<PAGE_SIZE>())
> >>         .ok_or(EOVERFLOW)?;
> >> 
> >> (maybe we could also have that `Alignment<PAGE_SIZE>` stored as a const
> >> in `page.rs` for convenience, as it might be used often)
> >
> > If you're trying to round up a number to the next multiple of PAGE_SIZE,
> > then you should use page_align() because that's exactly what the
> > function does.
> >
> > If you think there is something wrong with the design of page_align(),
> > change it instead of reimplemtning it.
> 
> In that case I would suggest that `page_align` returns an `Option`
> instead of potentially panicking. Does that sound reasonable? I cannot
> find any user of it in the Rust code for now.
> 

This sounds reasonable, and I did wonder when I read the comment on
page_align() whether it should just implement that check within the
function rather than relying on callers. But, I think changing the
signature of that function is probably something that should be done
separately to this change. I can't see anyone using it atm either, but
there could be unmerged branches or pending reviews that are using it.
If that's the path we want to go down, and it sounds completely
reasonable, I'll send that separately before posting a v2 of this patch
to use it.

Happy to take your guidance on that to satisfy the logistics of the
kernels ML driven patch approach.

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

end of thread, other threads:[~2025-11-26 21:14 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-21  4:04 [PATCH 1/1] drm: nova: Align GEM memory allocation to system page size bshephar
2025-11-25  7:41 ` bshephar
2025-11-25  9:23 ` Alice Ryhl
2025-11-26  5:49   ` Brendan Shephard
2025-11-26  9:53     ` Alice Ryhl
2025-11-25 14:28 ` Alexandre Courbot
2025-11-25 14:41   ` Alice Ryhl
2025-11-25 14:55     ` Alexandre Courbot
2025-11-25 14:59       ` Alice Ryhl
2025-11-26  0:31         ` Alexandre Courbot
2025-11-26  9:54           ` Alice Ryhl
2025-11-26 13:22             ` Alexandre Courbot
2025-11-26 13:36               ` Alice Ryhl
2025-11-26 14:00                 ` Alexandre Courbot
2025-11-26 16:24                   ` Alice Ryhl
2025-11-26 21:14                   ` Brendan Shephard
2025-11-26  6:05       ` Brendan Shephard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).