public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] drm/nova: Align GEM memory allocation to system page size
@ 2025-12-15  8:34 Brendan Shephard
  2025-12-18 13:16 ` Alexandre Courbot
  2025-12-29 14:46 ` Danilo Krummrich
  0 siblings, 2 replies; 4+ messages in thread
From: Brendan Shephard @ 2025-12-15  8:34 UTC (permalink / raw)
  To: aliceryhl, joelagnelf, acourbot, 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 is important on systems where the
default page size is not 4k. Such as 16k or 64k aarch64 systems.

This change uses the updated page_align() function which returns
Option<usize> for overflow safety. (See "rust: Return Option from
page_align and ensure no usize overflow").

Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
---
Changes in v2:
- Updated to use the new page_align() Option<usize> return value
- Prerequisite patch:
  https://lore.kernel.org/rust-for-linux/20251204224006.353646-2-bshephar@bne-home.net/T/#u
- Link to v1: https://lore.kernel.org/rust-for-linux/98227EBD-92F7-40FC-A5A4-3FF3780FB2CB@bne-home.net/

Changes in v3:
- Add back missing semi-colon after ok_or().
- Reword commit message to be more concise.
- Link to v2: https://lore.kernel.org/rust-for-linux/20251208064405.573026-1-bshephar@bne-home.net/T/#u

Changes in v4:
- Add back the size == 0 check;
- Rebase on latest drm-next branch
- Link to v3: https://lore.kernel.org/rust-for-linux/20251208071810.653223-1-bshephar@bne-home.net/T/#u

 drivers/gpu/drm/nova/gem.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
index 2760ba4f3450..3a3a41e8d4bc 100644
--- a/drivers/gpu/drm/nova/gem.rs
+++ b/drivers/gpu/drm/nova/gem.rs
@@ -3,6 +3,7 @@
 use kernel::{
     drm,
     drm::{gem, gem::BaseObject},
+    page::page_align,
     prelude::*,
     sync::aref::ARef,
 };
@@ -27,11 +28,10 @@ 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 {
+        if size == 0 {
             return Err(EINVAL);
         }
+        let aligned_size = page_align(size).ok_or(EINVAL)?;
 
         gem::Object::new(dev, aligned_size)
     }

base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
-- 
2.51.1


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

* Re: [PATCH v4] drm/nova: Align GEM memory allocation to system page size
  2025-12-15  8:34 [PATCH v4] drm/nova: Align GEM memory allocation to system page size Brendan Shephard
@ 2025-12-18 13:16 ` Alexandre Courbot
  2025-12-23  6:10   ` Brendan Shephard
  2025-12-29 14:46 ` Danilo Krummrich
  1 sibling, 1 reply; 4+ messages in thread
From: Alexandre Courbot @ 2025-12-18 13:16 UTC (permalink / raw)
  To: Brendan Shephard, aliceryhl, joelagnelf, acourbot, airlied
  Cc: rust-for-linux, nouveau

On Mon Dec 15, 2025 at 5:34 PM JST, Brendan Shephard wrote:
> Use page::page_align for GEM object memory allocation to ensure the
> allocation is page aligned. This is important on systems where the
> default page size is not 4k. Such as 16k or 64k aarch64 systems.
>
> This change uses the updated page_align() function which returns
> Option<usize> for overflow safety. (See "rust: Return Option from
> page_align and ensure no usize overflow").
>
> Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> ---
> Changes in v2:
> - Updated to use the new page_align() Option<usize> return value
> - Prerequisite patch:
>   https://lore.kernel.org/rust-for-linux/20251204224006.353646-2-bshephar@bne-home.net/T/#u
> - Link to v1: https://lore.kernel.org/rust-for-linux/98227EBD-92F7-40FC-A5A4-3FF3780FB2CB@bne-home.net/
>
> Changes in v3:
> - Add back missing semi-colon after ok_or().
> - Reword commit message to be more concise.
> - Link to v2: https://lore.kernel.org/rust-for-linux/20251208064405.573026-1-bshephar@bne-home.net/T/#u
>
> Changes in v4:
> - Add back the size == 0 check;
> - Rebase on latest drm-next branch
> - Link to v3: https://lore.kernel.org/rust-for-linux/20251208071810.653223-1-bshephar@bne-home.net/T/#u
>
>  drivers/gpu/drm/nova/gem.rs | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
> index 2760ba4f3450..3a3a41e8d4bc 100644
> --- a/drivers/gpu/drm/nova/gem.rs
> +++ b/drivers/gpu/drm/nova/gem.rs
> @@ -3,6 +3,7 @@
>  use kernel::{
>      drm,
>      drm::{gem, gem::BaseObject},
> +    page::page_align,
>      prelude::*,
>      sync::aref::ARef,
>  };
> @@ -27,11 +28,10 @@ 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 {
> +        if size == 0 {
>              return Err(EINVAL);
>          }
> +        let aligned_size = page_align(size).ok_or(EINVAL)?;

nit, but it's a good practice to always leave an empty line before a
block of variable declarations.

>  
>          gem::Object::new(dev, aligned_size)

... or if you prefer to avoid the variable altogether:

    page_align(size)
        .ok_or(EINVAL)
        .and_then(|size| gem::Object::new(dev, size))


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

* Re: [PATCH v4] drm/nova: Align GEM memory allocation to system page size
  2025-12-18 13:16 ` Alexandre Courbot
@ 2025-12-23  6:10   ` Brendan Shephard
  0 siblings, 0 replies; 4+ messages in thread
From: Brendan Shephard @ 2025-12-23  6:10 UTC (permalink / raw)
  To: Alexandre Courbot; +Cc: aliceryhl, joelagnelf, airlied, rust-for-linux, nouveau

On Thu, Dec 18, 2025 at 10:16:48PM +0900, Alexandre Courbot wrote:
> On Mon Dec 15, 2025 at 5:34 PM JST, Brendan Shephard 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 {
> > +        if size == 0 {
> >              return Err(EINVAL);
> >          }
> > +        let aligned_size = page_align(size).ok_or(EINVAL)?;
> 
> nit, but it's a good practice to always leave an empty line before a
> block of variable declarations.
> 
> >  
> >          gem::Object::new(dev, aligned_size)
> 
> ... or if you prefer to avoid the variable altogether:
> 
>     page_align(size)
>         .ok_or(EINVAL)
>         .and_then(|size| gem::Object::new(dev, size))
> 

Sounds good, I'll use `and_then`. I like the idea of not unnecessarily
assigning variables just to use them once. I'll make that change, re-test
and send a new revision of this one.

Thanks!

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

* Re: [PATCH v4] drm/nova: Align GEM memory allocation to system page size
  2025-12-15  8:34 [PATCH v4] drm/nova: Align GEM memory allocation to system page size Brendan Shephard
  2025-12-18 13:16 ` Alexandre Courbot
@ 2025-12-29 14:46 ` Danilo Krummrich
  1 sibling, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2025-12-29 14:46 UTC (permalink / raw)
  To: Brendan Shephard
  Cc: aliceryhl, joelagnelf, acourbot, airlied, rust-for-linux, nouveau

On Mon Dec 15, 2025 at 9:34 AM CET, Brendan Shephard wrote:
> Use page::page_align for GEM object memory allocation to ensure the
> allocation is page aligned. This is important on systems where the
> default page size is not 4k. Such as 16k or 64k aarch64 systems.
>
> This change uses the updated page_align() function which returns
> Option<usize> for overflow safety. (See "rust: Return Option from
> page_align and ensure no usize overflow").
>
> Signed-off-by: Brendan Shephard <bshephar@bne-home.net>

Applied to drm-rust-next, thanks!

    [ Import page module only. - Danilo ]

For future submissions, please make sure to run scripts/get_maintainer.pl.

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

end of thread, other threads:[~2025-12-29 14:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-15  8:34 [PATCH v4] drm/nova: Align GEM memory allocation to system page size Brendan Shephard
2025-12-18 13:16 ` Alexandre Courbot
2025-12-23  6:10   ` Brendan Shephard
2025-12-29 14:46 ` Danilo Krummrich

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