public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] drm/nova: Align GEM memory allocation to system page size
@ 2025-12-23 13:59 Brendan Shephard
  2025-12-23 20:11 ` Danilo Krummrich
  0 siblings, 1 reply; 3+ messages in thread
From: Brendan Shephard @ 2025-12-23 13:59 UTC (permalink / raw)
  To: acourbot, aliceryhl, dakr, joelagnelf, airlied
  Cc: rust-for-linux, 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

Changes in v5:
- Remove unnecessary variable declaration.
- Consolidate page_align call to use ok_or().and_then() for returned
  Result.

 drivers/gpu/drm/nova/gem.rs | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs
index a07e922e25ef..e1b57609b8f7 100644
--- a/drivers/gpu/drm/nova/gem.rs
+++ b/drivers/gpu/drm/nova/gem.rs
@@ -3,10 +3,7 @@
 use kernel::{
     drm,
     drm::{gem, gem::BaseObject},
-    page::{
-        page_align,
-        PAGE_SIZE, //
-    },
+    page::page_align,
     prelude::*,
     sync::aref::ARef,
 };
@@ -31,14 +28,12 @@ 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>>> {
-        // Check for 0 size or potential usize overflow before calling page_align
-        if size == 0 || size > usize::MAX - PAGE_SIZE + 1 {
+        if size == 0 {
             return Err(EINVAL);
         }
-
-        let aligned_size = page_align(size);
-
-        gem::Object::new(dev, aligned_size)
+        page_align(size)
+            .ok_or(EINVAL)
+            .and_then(|size| gem::Object::new(dev, size))
     }
 
     /// Look up a GEM object handle for a `File` and return an `ObjectRef` for it.

base-commit: b927546677c876e26eba308550207c2ddf812a43
prerequisite-patch-id: 7154ef17c3e40275152c90eff07bffcb5ef5e5cb
-- 
2.52.0


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

end of thread, other threads:[~2025-12-24  1:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-23 13:59 [PATCH v5] drm/nova: Align GEM memory allocation to system page size Brendan Shephard
2025-12-23 20:11 ` Danilo Krummrich
2025-12-24  1:58   ` Brendan Shephard

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