bshephar at bne-home.net
2025-Nov-21 04:20 UTC
[PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
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 at 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)
}
--
bshephar at bne-home.net
2025-Nov-25 07:49 UTC
[PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
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 at 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 at 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) > } > > --
Alice Ryhl
2025-Nov-25 09:23 UTC
[PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
On Fri, Nov 21, 2025 at 02:04:28PM +1000, bshephar at 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 at 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) > } > > --
Alexandre Courbot
2025-Nov-25 14:28 UTC
[PATCH 1/1] drm: nova: Align GEM memory allocation to system page size
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 at 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.