Skip to contentSkip to conversation

nova-core: Memory management infrastructure (v6)

RFC PATCH Superseded Joel Fernandes 9 participants 72 messages Jan 20 – Feb 6

Subsystems (13)

v6 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Joel Fernandes

author reviewer
#0 lore raw

v6 cover nova-core: Memory management infrastructure (v6)

This series is rebased on drm-rust-kernel/drm-rust-next and provides memory management infrastructure for the nova-core GPU driver. It combines several previous series and provides a foundation for nova GPU memory management including page tables, virtual memory management, and BAR mapping. All these are critical nova-core features.

The series includes:
- A Rust module (CList) to interface with C circular linked lists, required for iterating over buddy allocator blocks.
- Movement of the DRM buddy allocator up to drivers/gpu/ level, renamed to GPU buddy.
- Rust bindings for the GPU buddy allocator.
- PRAMIN aperture support for direct VRAM access.
- Page table types for MMU v2 and v3 formats.
- Virtual Memory Manager (VMM) for GPU virtual address space management.
- BAR1 user interface for mapping access GPU via virtual memory.
- Selftests for PRAMIN and BAR1 user interface (disabled by default).

Changes from v5 to v6:
- Rebased on drm-rust-kernel/drm-rust-next
- Added page table types and page table walker infrastructure
- Added Virtual Memory Manager (VMM)
- Added BAR1 user interface
- Added TLB flush support
- Added GpuMm memory manager
- Extended to 26 patches from 6 (full mm infrastructure now included)

The git tree with all patches can be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git (tag: nova-mm-v6-20260120)

Link to v5: https://lore.kernel.org/all/[email protected]/

Previous series that are combined:
- v4 (clist + buddy): https://lore.kernel.org/all/[email protected]/
- v3 (clist only): https://lore.kernel.org/all/[email protected]/
- v2 (clist only): https://lore.kernel.org/all/[email protected]/
- clist RFC (original with buddy): https://lore.kernel.org/all/[email protected]/
- DRM buddy move: https://lore.kernel.org/all/[email protected]/
- PRAMIN series: https://lore.kernel.org/all/[email protected]/

Joel Fernandes (26):
rust: clist: Add support to interface with C linked lists
gpu: Move DRM buddy allocator one level up
rust: gpu: Add GPU buddy allocator bindings
nova-core: mm: Select GPU_BUDDY for VRAM allocation
nova-core: mm: Add support to use PRAMIN windows to write to VRAM
docs: gpu: nova-core: Document the PRAMIN aperture mechanism
nova-core: Add BAR1 aperture type and size constant
nova-core: gsp: Add BAR1 PDE base accessors
nova-core: mm: Add common memory management types
nova-core: mm: Add common types for all page table formats
nova-core: mm: Add MMU v2 page table types
nova-core: mm: Add MMU v3 page table types
nova-core: mm: Add unified page table entry wrapper enums
nova-core: mm: Add TLB flush support
nova-core: mm: Add GpuMm centralized memory manager
nova-core: mm: Add page table walker for MMU v2
nova-core: mm: Add Virtual Memory Manager
nova-core: mm: Add virtual address range tracking to VMM
nova-core: mm: Add BAR1 user interface
nova-core: gsp: Return GspStaticInfo and FbLayout from boot()
nova-core: mm: Add memory management self-tests
nova-core: mm: Add PRAMIN aperture self-tests
nova-core: gsp: Extract usable FB region from GSP
nova-core: fb: Add usable_vram field to FbLayout
nova-core: mm: Use usable VRAM region for buddy allocator
nova-core: mm: Add BarUser to struct Gpu and create at boot

 Documentation/gpu/drm-mm.rst                  |   10 +-
 Documentation/gpu/nova/core/pramin.rst        |  125 ++
 Documentation/gpu/nova/index.rst              |    1 +
 MAINTAINERS                                   |    7 +
 drivers/gpu/Kconfig                           |   13 +
 drivers/gpu/Makefile                          |    2 +
 drivers/gpu/buddy.c                           | 1310 +++++++++++++++++
 drivers/gpu/drm/Kconfig                       |    1 +
 drivers/gpu/drm/Kconfig.debug                 |    4 +-
 drivers/gpu/drm/amd/amdgpu/Kconfig            |    1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |    2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |   12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  |   80 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h  |   20 +-
 drivers/gpu/drm/drm_buddy.c                   | 1284 +---------------
 drivers/gpu/drm/i915/Kconfig                  |    1 +
 drivers/gpu/drm/i915/i915_scatterlist.c       |   10 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c |   55 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |    6 +-
 .../drm/i915/selftests/intel_memory_region.c  |   20 +-
 drivers/gpu/drm/tests/Makefile                |    1 -
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |    5 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  |   18 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |    4 +-
 drivers/gpu/drm/xe/Kconfig                    |    1 +
 drivers/gpu/drm/xe/xe_res_cursor.h            |   34 +-
 drivers/gpu/drm/xe/xe_svm.c                   |   12 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c          |   73 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h    |    4 +-
 drivers/gpu/nova-core/Kconfig                 |   22 +
 drivers/gpu/nova-core/driver.rs               |    9 +-
 drivers/gpu/nova-core/fb.rs                   |   23 +-
 drivers/gpu/nova-core/gpu.rs                  |  140 +-
 drivers/gpu/nova-core/gsp/boot.rs             |   22 +-
 drivers/gpu/nova-core/gsp/commands.rs         |   18 +-
 drivers/gpu/nova-core/gsp/fw/commands.rs      |   38 +
 drivers/gpu/nova-core/mm/bar_user.rs          |  336 +++++
 drivers/gpu/nova-core/mm/mod.rs               |  209 +++
 drivers/gpu/nova-core/mm/pagetable/mod.rs     |  377 +++++
 drivers/gpu/nova-core/mm/pagetable/ver2.rs    |  184 +++
 drivers/gpu/nova-core/mm/pagetable/ver3.rs    |  286 ++++
 drivers/gpu/nova-core/mm/pagetable/walk.rs    |  285 ++++
 drivers/gpu/nova-core/mm/pramin.rs            |  404 +++++
 drivers/gpu/nova-core/mm/tlb.rs               |   79 +
 drivers/gpu/nova-core/mm/vmm.rs               |  247 ++++
 drivers/gpu/nova-core/nova_core.rs            |    1 +
 drivers/gpu/nova-core/regs.rs                 |   38 +
 drivers/gpu/tests/Makefile                    |    3 +
 .../gpu_buddy_test.c}                         |  390 ++---
 drivers/gpu/tests/gpu_random.c                |   48 +
 drivers/gpu/tests/gpu_random.h                |   28 +
 drivers/video/Kconfig                         |    2 +
 include/drm/drm_buddy.h                       |  163 +-
 include/linux/gpu_buddy.h                     |  177 +++
 rust/bindings/bindings_helper.h               |   11 +
 rust/helpers/gpu.c                            |   23 +
 rust/helpers/helpers.c                        |    2 +
 rust/helpers/list.c                           |   12 +
 rust/kernel/clist.rs                          |  357 +++++
 rust/kernel/gpu/buddy.rs                      |  538 +++++++
 rust/kernel/gpu/mod.rs                        |    5 +
 rust/kernel/lib.rs                            |    3 +
 62 files changed, 5788 insertions(+), 1808 deletions(-)
 create mode 100644 Documentation/gpu/nova/core/pramin.rst
 create mode 100644 drivers/gpu/Kconfig
 create mode 100644 drivers/gpu/buddy.c
 create mode 100644 drivers/gpu/nova-core/mm/bar_user.rs
 create mode 100644 drivers/gpu/nova-core/mm/mod.rs
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/mod.rs
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/ver2.rs
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/ver3.rs
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/walk.rs
 create mode 100644 drivers/gpu/nova-core/mm/pramin.rs
 create mode 100644 drivers/gpu/nova-core/mm/tlb.rs
 create mode 100644 drivers/gpu/nova-core/mm/vmm.rs
 create mode 100644 drivers/gpu/tests/Makefile
 rename drivers/gpu/{drm/tests/drm_buddy_test.c => tests/gpu_buddy_test.c} (68%)
 create mode 100644 drivers/gpu/tests/gpu_random.c
 create mode 100644 drivers/gpu/tests/gpu_random.h
 create mode 100644 include/linux/gpu_buddy.h
 create mode 100644 rust/helpers/gpu.c
 create mode 100644 rust/helpers/list.c
 create mode 100644 rust/kernel/clist.rs
 create mode 100644 rust/kernel/gpu/buddy.rs
 create mode 100644 rust/kernel/gpu/mod.rs

base-commit: 6ea52b6d8f33ae627f4dcf43b12b6e713a8b9331

Signature · 1 line
2.34.1
author reviewer
#1 lore raw

RFC PATCH v6 1/26 rust: clist: Add support to interface with C linked lists

Add a new module clist for working with C's doubly circular linked lists. Provide low-level iteration over list nodes.

Typed iteration over actual items is provided with a clist_create macro to assist in creation of the Clist type.

---
 MAINTAINERS            |   7 +
 rust/helpers/helpers.c |   1 +
 rust/helpers/list.c    |  12 ++
 rust/kernel/clist.rs   | 357 +++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs     |   1 +
 5 files changed, 378 insertions(+)
 create mode 100644 rust/helpers/list.c
 create mode 100644 rust/kernel/clist.rs
MAINTAINERS +7 -0
index 0d044a58cbfe..b76988c38045 100644
@@ -22936,6 +22936,13 @@ F:	rust/kernel/init.rs
 F:	rust/pin-init/
 K:	\bpin-init\b|pin_init\b|PinInit
 
+RUST TO C LIST INTERFACES
+M:	Joel Fernandes <[email protected]>
+M:	Alexandre Courbot <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	rust/kernel/clist.rs
+
 RXRPC SOCKETS (AF_RXRPC)
 M:	David Howells <[email protected]>
 M:	Marc Dionne <[email protected]>
rust/helpers/helpers.c +1 -0
index 79c72762ad9c..634fa2386bbb 100644
@@ -32,6 +32,7 @@
 #include "io.c"
 #include "jump_label.c"
 #include "kunit.c"
+#include "list.c"
 #include "maple_tree.c"
 #include "mm.c"
 #include "mutex.c"
rust/helpers/list.c new file +12 -0
new file mode 100644
index 000000000000..6044979c7a2e
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Helpers for C Circular doubly linked list implementation.
+ */
+
+#include <linux/list.h>
+
+void rust_helper_list_add_tail(struct list_head *new, struct list_head *head)
+{
+	list_add_tail(new, head);
+}
rust/kernel/clist.rs new file +357 -0

357 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index f812cf120042..cd7e6a1055b0 100644
@@ -75,6 +75,7 @@
 pub mod bug;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clist;
 pub mod clk;
 #[cfg(CONFIG_CONFIGFS_FS)]
 pub mod configfs;
Signature · 1 line
2.34.1
reviewer
#2 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

Quoted 5 lines · Joel Fernandes

Add a new module clist for working with C's doubly circular linked lists. Provide low-level iteration over list nodes.

Typed iteration over actual items is provided with a clist_create macro to assist in creation of the Clist type.

This should read "CList".

---

I was quite dubious about the patch just from the title (everybody knows how easy a linked list is in Rust), but it turns out it is not as concerning as I expected, mostly due to the read-only nature of the particular implementation (a lot of the safety comments would be much more difficult to justify, say, if it's mutable). That said, still a lot of feedbacks below.

I think something like is okay in the short term. However, there's an growing interest in getting our Rust list API improved, so it could be ideal if eventually the Rust list can be capable of handling FFI lists, too.

Quoted 162 lines
---
 MAINTAINERS            |   7 +
 rust/helpers/helpers.c |   1 +
 rust/helpers/list.c    |  12 ++
 rust/kernel/clist.rs   | 357 +++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs     |   1 +
 5 files changed, 378 insertions(+)
 create mode 100644 rust/helpers/list.c
 create mode 100644 rust/kernel/clist.rs
MAINTAINERS +7 -0
index 0d044a58cbfe..b76988c38045 100644
@@ -22936,6 +22936,13 @@ F:	rust/kernel/init.rs
 F:	rust/pin-init/
 K:	\bpin-init\b|pin_init\b|PinInit
 
+RUST TO C LIST INTERFACES
+M:	Joel Fernandes <[email protected]>
+M:	Alexandre Courbot <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	rust/kernel/clist.rs
+
 RXRPC SOCKETS (AF_RXRPC)
 M:	David Howells <[email protected]>
 M:	Marc Dionne <[email protected]>
rust/helpers/helpers.c +1 -0
index 79c72762ad9c..634fa2386bbb 100644
@@ -32,6 +32,7 @@
 #include "io.c"
 #include "jump_label.c"
 #include "kunit.c"
+#include "list.c"
 #include "maple_tree.c"
 #include "mm.c"
 #include "mutex.c"
rust/helpers/list.c new file +12 -0
new file mode 100644
index 000000000000..6044979c7a2e
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Helpers for C Circular doubly linked list implementation.
+ */
+
+#include <linux/list.h>
+
+void rust_helper_list_add_tail(struct list_head *new, struct list_head *head)
+{
+	list_add_tail(new, head);
+}
rust/kernel/clist.rs new file +96 -0
new file mode 100644
index 000000000000..91754ae721b9
@@ -0,0 +1,357 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! A C doubly circular intrusive linked list interface for rust code.
+//!
+//! # Examples
+//!
+//! ```
+//! use kernel::{
+//!     bindings,
+//!     clist::init_list_head,
+//!     clist_create,
+//!     types::Opaque, //
+//! };
+//! # // Create test list with values (0, 10, 20) - normally done by C code but it is
+//! # // emulated here for doctests using the C bindings.
+//! # use core::mem::MaybeUninit;
+//! #
+//! # /// C struct with embedded `list_head` (typically will be allocated by C code).
+//! # #[repr(C)]
+//! # pub(crate) struct SampleItemC {
+//! #     pub value: i32,
+//! #     pub link: bindings::list_head,
+//! # }
+//! #
+//! # let mut head = MaybeUninit::<bindings::list_head>::uninit();
+//! #
+//! # let head = head.as_mut_ptr();
+//! # // SAFETY: head and all the items are test objects allocated in this scope.
+//! # unsafe { init_list_head(head) };
+//! #
+//! # let mut items = [
+//! #     MaybeUninit::<SampleItemC>::uninit(),
+//! #     MaybeUninit::<SampleItemC>::uninit(),
+//! #     MaybeUninit::<SampleItemC>::uninit(),
+//! # ];
+//! #
+//! # for (i, item) in items.iter_mut().enumerate() {
+//! #     let ptr = item.as_mut_ptr();
+//! #     // SAFETY: pointers are to allocated test objects with a list_head field.
+//! #     unsafe {
+//! #         (*ptr).value = i as i32 * 10;
+//! #         // addr_of_mut!() computes address of link directly as link is uninitialized.
+//! #         init_list_head(core::ptr::addr_of_mut!((*ptr).link));
+//! #         bindings::list_add_tail(&mut (*ptr).link, head);
+//! #     }
+//! # }
+//!
+//! // Rust wrapper for the C struct.
+//! // The list item struct in this example is defined in C code as:
+//! //   struct SampleItemC {
+//! //       int value;
+//! //       struct list_head link;
+//! //   };
+//! //
+//! #[repr(transparent)]
+//! pub(crate) struct Item(Opaque<SampleItemC>);
+//!
+//! impl Item {
+//!     pub(crate) fn value(&self) -> i32 {
+//!         // SAFETY: [`Item`] has same layout as [`SampleItemC`].
+//!         unsafe { (*self.0.get()).value }
+//!     }
+//! }
+//!
+//! // Create typed [`CList`] from sentinel head.
+//! // SAFETY: head is valid, items are [`SampleItemC`] with embedded `link` field.
+//! let list = unsafe { clist_create!(head, Item, SampleItemC, link) };
+//!
+//! // Iterate directly over typed items.
+//! let mut found_0 = false;
+//! let mut found_10 = false;
+//! let mut found_20 = false;
+//!
+//! for item in list.iter() {
+//!     let val = item.value();
+//!     if val == 0 { found_0 = true; }
+//!     if val == 10 { found_10 = true; }
+//!     if val == 20 { found_20 = true; }
+//! }
+//!
+//! assert!(found_0 && found_10 && found_20);
+//! ```
+
+use core::{
+    iter::FusedIterator,
+    marker::PhantomData, //
+};
+
+use crate::{
+    bindings,
+    types::Opaque, //
+};
+
+use pin_init::PinInit;
+
+/// Initialize a `list_head` object to point to itself.

rust/kernel/clist.rs line 106

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+///
+/// # Safety
+///
+/// `list` must be a valid pointer to a `list_head` object.
+#[inline]
+pub unsafe fn init_list_head(list: *mut bindings::list_head) {
+    // SAFETY: Caller guarantees `list` is a valid pointer to a `list_head`.
+    unsafe {
+        (*list).next = list;
+        (*list).prev = list;

This needs to be an atomic write or it'll depart from the C implementation.

rust/kernel/clist.rs line 108

Quoted 2 lines
rust/kernel/clist.rs new file +2 -0
+    }
+}

I don't think we want to publicly expose this! I've not found a user in the subsequent patch, too.

Alice suggested to move this to bindings in v3 which I think is a good idea. Also, even though it's against Rust name convention, for bindings we should use the exact name as C (so INIT_LIST_HEAD).

Quoted 48 lines
rust/kernel/clist.rs new file +48 -0
+
+/// Wraps a `list_head` object for use in intrusive linked lists.
+///
+/// # Invariants
+///
+/// - [`CListHead`] represents an allocated and valid `list_head` structure.
+/// - Once a [`CListHead`] is created in Rust, it will not be modified by non-Rust code.
+/// - All `list_head` for individual items are not modified for the lifetime of [`CListHead`].
+#[repr(transparent)]
+pub struct CListHead(Opaque<bindings::list_head>);
+
+impl CListHead {
+    /// Create a `&CListHead` reference from a raw `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::list_head) -> &'a Self {
+        // SAFETY:
+        // - [`CListHead`] has same layout as `list_head`.
+        // - `ptr` is valid and unmodified for 'a.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Get the raw `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.0.get()
+    }
+
+    /// Get the next [`CListHead`] in the list.
+    #[inline]
+    pub fn next(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - `self.as_raw()` is valid per type invariants.
+        // - The `next` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).next) }
+    }
+
+    /// Get the previous [`CListHead`] in the list.
+    #[inline]
+    pub fn prev(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - self.as_raw() is valid per type invariants.

rust/kernel/clist.rs line 166

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+        // - The `prev` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).prev) }
+    }
+
+    /// Check if this node is linked in a list (not isolated).
+    #[inline]
+    pub fn is_linked(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next != raw && (*raw).prev != raw }

While is this checking both prev and next? list_empty is just READ_ONCE(head->next) == head.

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+    }
+
+    /// Fallible pin-initializer that initializes and then calls user closure.
+    ///
+    /// Initializes the list head first, then passes `&CListHead` to the closure.
+    /// This hides the raw FFI pointer from the user.

rust/kernel/clist.rs line 182

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+    pub fn try_init<E>(
+        init_func: impl FnOnce(&CListHead) -> Result<(), E>,
+    ) -> impl PinInit<Self, E> {
+        // SAFETY: init_list_head initializes the list_head to point to itself.
+        // After initialization, we create a reference to pass to the closure.
+        unsafe {
+            pin_init::pin_init_from_closure(move |slot: *mut Self| {
+                init_list_head(slot.cast());
+                // SAFETY: slot is now initialized, safe to create reference.
+                init_func(&*slot)

Why is this callback necessary? The user can just create the list head and then reference it later? I don't see what this specifically gains over just doing

    fn new() -> impl PinInit<Self>;

and have user-side

    list <- CListHead::new(),
    _: {
        do_want_ever(&list)
    }
Quoted 5 lines
rust/kernel/clist.rs new file +5 -0
+            })
+        }
+    }
+}
+

rust/kernel/clist.rs line 197

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+// SAFETY: [`CListHead`] can be sent to any thread.
+unsafe impl Send for CListHead {}
+
+// SAFETY: [`CListHead`] can be shared among threads as it is not modified
+// by non-Rust code per type invariants.
+unsafe impl Sync for CListHead {}
+
+impl PartialEq for CListHead {
+    fn eq(&self, other: &Self) -> bool {
+        self.as_raw() == other.as_raw()

Or just core::ptr::eq(self, other)

Quoted 23 lines
rust/kernel/clist.rs new file +23 -0
+    }
+}
+
+impl Eq for CListHead {}
+
+/// Low-level iterator over `list_head` nodes.
+///
+/// An iterator used to iterate over a C intrusive linked list (`list_head`). Caller has to
+/// perform conversion of returned [`CListHead`] to an item (using `container_of` macro or similar).
+///
+/// # Invariants
+///
+/// [`CListHeadIter`] is iterating over an allocated, initialized and valid list.
+struct CListHeadIter<'a> {
+    current_head: &'a CListHead,
+    list_head: &'a CListHead,
+}
+
+impl<'a> Iterator for CListHeadIter<'a> {
+    type Item = &'a CListHead;
+
+    #[inline]
+    fn next(&mut self) -> Option<Self::Item> {

rust/kernel/clist.rs line 230

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+        // Advance to next node.
+        let next = self.current_head.next();
+
+        // Check if we've circled back to the sentinel head.
+        if next == self.list_head {
+            None
+        } else {
+            self.current_head = next;
+            Some(self.current_head)
+        }

I think this could match the C iterator behaviour. When the iterator is created, a next is done first, and then subsequently you only need to check if current_head is list_head.

This is slightly better because the condition check does not need to dereference a pointer.

Quoted 13 lines
rust/kernel/clist.rs new file +13 -0
+    }
+}
+
+impl<'a> FusedIterator for CListHeadIter<'a> {}
+
+/// A typed C linked list with a sentinel head.
+///
+/// A sentinel head represents the entire linked list and can be used for
+/// iteration over items of type `T`, it is not associated with a specific item.
+///
+/// The const generic `OFFSET` specifies the byte offset of the `list_head` field within
+/// the struct that `T` wraps.
+///

rust/kernel/clist.rs line 253

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+/// # Invariants
+///
+/// - `head` is an allocated and valid C `list_head` structure that is the list's sentinel.
+/// - `OFFSET` is the byte offset of the `list_head` field within the struct that `T` wraps.
+/// - All the list's `list_head` nodes are allocated and have valid next/prev pointers.
+/// - The underlying `list_head` (and entire list) is not modified for the lifetime `'a`.
+pub struct CList<'a, T, const OFFSET: usize> {
+    head: &'a CListHead,
+    _phantom: PhantomData<&'a T>,
+}

Is there a reason that this is not

#[repr(transparent)]
struct CList(CListHead)

? We typically want to avoid putting reference inside the struct if it can be on the outside. This allows &self to be a single level of reference, not too.

It also means that you can just write &CList<_> in many cases, and doesn't need CList<'_, T> (plus all the benefits of a reference).

Quoted 22 lines
rust/kernel/clist.rs new file +22 -0
+
+impl<'a, T, const OFFSET: usize> CList<'a, T, OFFSET> {
+    /// Create a typed [`CList`] from a raw sentinel `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure
+    ///   representing a list sentinel.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    /// - The list must contain items where the `list_head` field is at byte offset `OFFSET`.
+    /// - `T` must be `#[repr(transparent)]` over the C struct.
+    #[inline]
+    pub unsafe fn from_raw(ptr: *mut bindings::list_head) -> Self {
+        Self {
+            // SAFETY: Caller guarantees `ptr` is a valid, sentinel `list_head` object.
+            head: unsafe { CListHead::from_raw(ptr) },
+            _phantom: PhantomData,
+        }
+    }
+
+    /// Get the raw sentinel `list_head` pointer.
+    #[inline]

rust/kernel/clist.rs line 285

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.head.as_raw()
+    }
+
+    /// Check if the list is empty.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next == raw }

self.head.is_linked()?

Quoted 46 lines
rust/kernel/clist.rs new file +46 -0
+    }
+
+    /// Create an iterator over typed items.
+    #[inline]
+    pub fn iter(&self) -> CListIter<'a, T, OFFSET> {
+        CListIter {
+            head_iter: CListHeadIter {
+                current_head: self.head,
+                list_head: self.head,
+            },
+            _phantom: PhantomData,
+        }
+    }
+}
+
+/// High-level iterator over typed list items.
+pub struct CListIter<'a, T, const OFFSET: usize> {
+    head_iter: CListHeadIter<'a>,
+    _phantom: PhantomData<&'a T>,
+}
+
+impl<'a, T, const OFFSET: usize> Iterator for CListIter<'a, T, OFFSET> {
+    type Item = &'a T;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        let head = self.head_iter.next()?;
+
+        // Convert to item using OFFSET.
+        // SAFETY: `item_ptr` calculation from `OFFSET` (calculated using offset_of!)
+        // is valid per invariants.
+        Some(unsafe { &*head.as_raw().byte_sub(OFFSET).cast::<T>() })
+    }
+}
+
+impl<'a, T, const OFFSET: usize> FusedIterator for CListIter<'a, T, OFFSET> {}
+
+/// Create a C doubly-circular linked list interface [`CList`] from a raw `list_head` pointer.
+///
+/// This macro creates a [`CList<T, OFFSET>`] that can iterate over items of type `$rust_type`
+/// linked via the `$field` field in the underlying C struct `$c_type`.
+///
+/// # Arguments
+///
+/// - `$head`: Raw pointer to the sentinel `list_head` object (`*mut bindings::list_head`).
+/// - `$rust_type`: Each item's rust wrapper type.
+/// - `$c_type`: Each item's C struct type that contains the embedded `list_head`.

rust/kernel/clist.rs line 341

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+/// - `$field`: The name of the `list_head` field within the C struct.
+///
+/// # Safety
+///
+/// The caller must ensure:
+/// - `$head` is a valid, initialized sentinel `list_head` pointing to a list that remains
+///   unmodified for the lifetime of the rust [`CList`].
+/// - The list contains items of type `$c_type` linked via an embedded `$field`.
+/// - `$rust_type` is `#[repr(transparent)]` over `$c_type` or has compatible layout.
+/// - The macro is called from an unsafe block.

This is not a safe requirement, probably lift it up and say "This is an unsafe macro.".

rust/kernel/clist.rs line 351

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+///
+/// # Examples
+///
+/// Refer to the examples in the [`crate::clist`] module documentation.
+#[macro_export]
+macro_rules! clist_create {
+    ($head:expr, $rust_type:ty, $c_type:ty, $($field:tt).+) => {{
+        // Compile-time check that field path is a list_head.
+        let _: fn(*const $c_type) -> *const $crate::bindings::list_head =
+            |p| ::core::ptr::addr_of!((*p).$($field).+);

&raw const is preferred now.

Quoted 11 lines
rust/kernel/clist.rs new file +6 -0
+
+        // Calculate offset and create `CList`.
+        const OFFSET: usize = ::core::mem::offset_of!($c_type, $($field).+);
+        $crate::clist::CList::<$rust_type, OFFSET>::from_raw($head)
+    }};
+}
rust/kernel/lib.rs +0 -0
index f812cf120042..cd7e6a1055b0 100644

rust/kernel/lib.rs line 78

Quoted 4 lines
rust/kernel/lib.rs +1 -0
@@ -75,6 +75,7 @@
 pub mod bug;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clist;

Can we keep this pub(crate)?

Best,
Gary

Quoted 3 lines
rust/kernel/lib.rs +0 -0
 pub mod clk;
 #[cfg(CONFIG_CONFIGFS_FS)]
 pub mod configfs;
author reviewer
#3 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

Hello, Gary,

Quoted 8 lines · Gary Guo
Quoted 5 lines · Joel Fernandes

Add a new module clist for working with C's doubly circular linked lists. Provide low-level iteration over list nodes.

Typed iteration over actual items is provided with a clist_create macro to assist in creation of the Clist type.

This should read "CList".

Sure, will fix.

Quoted 6 lines

I was quite dubious about the patch just from the title (everybody knows how easy a linked list is in Rust), but it turns out it is not as concerning as I expected, mostly due to the read-only nature of the particular implementation (a lot of the safety comments would be much more difficult to justify, say, if it's mutable). That said, still a lot of feedbacks below.

Sure, the reason for requiring this is interfacing with lists coming from C code. I'd see a future where we may want it mutable too (example, Rust code adding elements to the existing). At which point, the invariants/safety reasoning may change.

Quoted 3 lines

I think something like is okay in the short term. However, there's an growing interest in getting our Rust list API improved, so it could be ideal if eventually the Rust list can be capable of handling FFI lists, too.

Yeah we looked into that, if you see old threads, the conclusion was it is not a good fit for existing rust list abstractions. TLDR; it does not fit into their ownership/borrowing model.

[...]

Quoted 6 lines
Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+
+/// Initialize a `list_head` object to point to itself.
+///
+/// # Safety
+///
+/// `list` must be a valid pointer to a `list_head` object.
Quoted 8 lines

rust/kernel/clist.rs line 106

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+#[inline]
+pub unsafe fn init_list_head(list: *mut bindings::list_head) {
+    // SAFETY: Caller guarantees `list` is a valid pointer to a `list_head`.
+    unsafe {
+        (*list).next = list;
+        (*list).prev = list;

This needs to be an atomic write or it'll depart from the C implementation.

I am curious what you mean by atomic write, can you define it? Does rust compiler have load/store fusing, invented stores, etc, like C does? Sorry I am only familiar with these concepts on C. Could you provide example of a race condition in Rust that can happen?

Also I did this addition based on feedback from past review:
https://lore.kernel.org/all/[email protected]/

There was some concerns around pointless function call overhead when the rust implementation is already quite intertwined with internals of the C linked list implementation. I do agree with that point of view too.

Also see my other reply to Zhi on this helper topic, lets discuss there too, if that's Ok.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    }
+}

I don't think we want to publicly expose this! I've not found a user in the subsequent patch, too.

There are 2 users:

    pub fn try_init<E>(

and the self-tests:

//! # let head = head.as_mut_ptr();
//! # // SAFETY: head and all the items are test objects allocated in [..] //! # unsafe { init_list_head(head) };
//! #

Quoted 54 lines
Quoted 54 lines
rust/kernel/clist.rs new file +53 -0
+
+/// Wraps a `list_head` object for use in intrusive linked lists.
+///
+/// # Invariants
+///
+/// - [`CListHead`] represents an allocated and valid `list_head` structure.
+/// - Once a [`CListHead`] is created in Rust, it will not be modified by non-Rust code.
+/// - All `list_head` for individual items are not modified for the lifetime of [`CListHead`].
+#[repr(transparent)]
+pub struct CListHead(Opaque<bindings::list_head>);
+
+impl CListHead {
+    /// Create a `&CListHead` reference from a raw `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::list_head) -> &'a Self {
+        // SAFETY:
+        // - [`CListHead`] has same layout as `list_head`.
+        // - `ptr` is valid and unmodified for 'a.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Get the raw `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.0.get()
+    }
+
+    /// Get the next [`CListHead`] in the list.
+    #[inline]
+    pub fn next(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - `self.as_raw()` is valid per type invariants.
+        // - The `next` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).next) }
+    }
+
+    /// Get the previous [`CListHead`] in the list.
+    #[inline]
+    pub fn prev(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - self.as_raw() is valid per type invariants.
+        // - The `prev` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).prev) }
+    }
+
+    /// Check if this node is linked in a list (not isolated).
Quoted 8 lines

rust/kernel/clist.rs line 166

Quoted 5 lines
rust/kernel/clist.rs new file +5 -0
+    #[inline]
+    pub fn is_linked(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next != raw && (*raw).prev != raw }

While is this checking both prev and next? list_empty is just READ_ONCE(head->next) == head.

Sure, I can optimize to just check ->next, that makes sense. Will do.

Quoted 22 lines
Quoted 7 lines
rust/kernel/clist.rs new file +6 -0
+    }
+
+    /// Fallible pin-initializer that initializes and then calls user closure.
+    ///
+    /// Initializes the list head first, then passes `&CListHead` to the closure.
+    /// This hides the raw FFI pointer from the user.

rust/kernel/clist.rs line 182

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+    pub fn try_init<E>(
+        init_func: impl FnOnce(&CListHead) -> Result<(), E>,
+    ) -> impl PinInit<Self, E> {
+        // SAFETY: init_list_head initializes the list_head to point to itself.
+        // After initialization, we create a reference to pass to the closure.
+        unsafe {
+            pin_init::pin_init_from_closure(move |slot: *mut Self| {
+                init_list_head(slot.cast());
+                // SAFETY: slot is now initialized, safe to create reference.
+                init_func(&*slot)

Why is this callback necessary? The user can just create the list head and then reference it later? I don't see what this specifically gains over just doing

Quoted 8 lines
    fn new() -> impl PinInit<Self>;

and have user-side

    list <- CListHead::new(),
    _: {
        do_want_ever(&list)
    }

The list initialization can fail, see the GPU buddy patch:

        // Create pin-initializer that initializes list and allocates blocks.
        let init = try_pin_init!(AllocatedBlocks {
            list <- CListHead::try_init(|list| {
                // Lock while allocating to serialize with concurrent frees.
                let guard = buddy_arc.lock();

                // SAFETY: guard provides exclusive access, list is initialized.
                to_result(unsafe {
                    bindings::gpu_buddy_alloc_blocks(
                        guard.as_raw(),
                        params.start_range_address,
                        params.end_range_address,
                        params.size_bytes,
                        params.min_block_size_bytes,
                        list.as_raw(),
                        params.buddy_flags.as_raw(),
                    )
                })
            }),
            buddy: Arc::clone(&buddy_arc),
            flags: params.buddy_flags,
        });
Quoted 11 lines
Quoted 11 lines
rust/kernel/clist.rs new file +9 -0
+            })
+        }
+    }
+}
+
+// SAFETY: [`CListHead`] can be sent to any thread.
+unsafe impl Send for CListHead {}
+
+// SAFETY: [`CListHead`] can be shared among threads as it is not modified
Quoted 8 lines

rust/kernel/clist.rs line 197

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+// by non-Rust code per type invariants.
+unsafe impl Sync for CListHead {}
+
+impl PartialEq for CListHead {
+    fn eq(&self, other: &Self) -> bool {
+        self.as_raw() == other.as_raw()

Or just core::ptr::eq(self, other)

Sure, will fix.

Quoted 39 lines
Quoted 24 lines
rust/kernel/clist.rs new file +23 -0
+    }
+}
+
+impl Eq for CListHead {}
+
+/// Low-level iterator over `list_head` nodes.
+///
+/// An iterator used to iterate over a C intrusive linked list (`list_head`). Caller has to
+/// perform conversion of returned [`CListHead`] to an item (using `container_of` macro or similar).
+///
+/// # Invariants
+///
+/// [`CListHeadIter`] is iterating over an allocated, initialized and valid list.
+struct CListHeadIter<'a> {
+    current_head: &'a CListHead,
+    list_head: &'a CListHead,
+}
+
+impl<'a> Iterator for CListHeadIter<'a> {
+    type Item = &'a CListHead;
+
+    #[inline]
+    fn next(&mut self) -> Option<Self::Item> {

rust/kernel/clist.rs line 230

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+        // Advance to next node.
+        let next = self.current_head.next();
+
+        // Check if we've circled back to the sentinel head.
+        if next == self.list_head {
+            None
+        } else {
+            self.current_head = next;
+            Some(self.current_head)
+        }

I think this could match the C iterator behaviour. When the iterator is created, a next is done first, and then subsequently you only need to check if current_head is list_head.

Quoted 2 lines

This is slightly better because the condition check does not need to dereference a pointer.

Sure, I can change it to that.

Quoted 23 lines
Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+impl<'a> FusedIterator for CListHeadIter<'a> {}
+
+/// A typed C linked list with a sentinel head.
+///
+/// A sentinel head represents the entire linked list and can be used for
+/// iteration over items of type `T`, it is not associated with a specific item.
+///
+/// The const generic `OFFSET` specifies the byte offset of the `list_head` field within
+/// the struct that `T` wraps.
+///

rust/kernel/clist.rs line 253

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+/// # Invariants
+///
+/// - `head` is an allocated and valid C `list_head` structure that is the list's sentinel.
+/// - `OFFSET` is the byte offset of the `list_head` field within the struct that `T` wraps.
+/// - All the list's `list_head` nodes are allocated and have valid next/prev pointers.
+/// - The underlying `list_head` (and entire list) is not modified for the lifetime `'a`.
+pub struct CList<'a, T, const OFFSET: usize> {
+    head: &'a CListHead,
+    _phantom: PhantomData<&'a T>,
+}

Is there a reason that this is not

Quoted 8 lines

#[repr(transparent)]
struct CList(CListHead)

? We typically want to avoid putting reference inside the struct if it can be on the outside. This allows &self to be a single level of reference, not too.

It also means that you can just write &CList<_> in many cases, and doesn't need CList<'_, T> (plus all the benefits of a reference).

Sure! Will change to this. I am guessing you mean the following, but please let me know if you meant something else:

pub struct CList<T, const OFFSET: usize>(
CListHead,
PhantomData<T>,
);

I don't see any issues with my code using that, at the moment. Will let you know how it goes.

Quoted 25 lines
Quoted 25 lines
rust/kernel/clist.rs new file +25 -0
+impl<'a, T, const OFFSET: usize> CList<'a, T, OFFSET> {
+    /// Create a typed [`CList`] from a raw sentinel `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure
+    ///   representing a list sentinel.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    /// - The list must contain items where the `list_head` field is at byte offset `OFFSET`.
+    /// - `T` must be `#[repr(transparent)]` over the C struct.
+    #[inline]
+    pub unsafe fn from_raw(ptr: *mut bindings::list_head) -> Self {
+        Self {
+            // SAFETY: Caller guarantees `ptr` is a valid, sentinel `list_head` object.
+            head: unsafe { CListHead::from_raw(ptr) },
+            _phantom: PhantomData,
+        }
+    }
+
+    /// Get the raw sentinel `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.head.as_raw()
+    }
+
Quoted 8 lines

rust/kernel/clist.rs line 285

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+    /// Check if the list is empty.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next == raw }

self.head.is_linked()?

I'd considered is_linked() to be something that makes sense to call only on ClistHead objects that belong to a particular "item" node, not a sentinel node, so that was deliberate.

Though, I am Ok with doing it the way you are suggesting too (self.head.is_linked()), since it is functionally equivalent.

Quoted 51 lines
Quoted 51 lines
rust/kernel/clist.rs new file +51 -0
+    }
+
+    /// Create an iterator over typed items.
+    #[inline]
+    pub fn iter(&self) -> CListIter<'a, T, OFFSET> {
+        CListIter {
+            head_iter: CListHeadIter {
+                current_head: self.head,
+                list_head: self.head,
+            },
+            _phantom: PhantomData,
+        }
+    }
+}
+
+/// High-level iterator over typed list items.
+pub struct CListIter<'a, T, const OFFSET: usize> {
+    head_iter: CListHeadIter<'a>,
+    _phantom: PhantomData<&'a T>,
+}
+
+impl<'a, T, const OFFSET: usize> Iterator for CListIter<'a, T, OFFSET> {
+    type Item = &'a T;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        let head = self.head_iter.next()?;
+
+        // Convert to item using OFFSET.
+        // SAFETY: `item_ptr` calculation from `OFFSET` (calculated using offset_of!)
+        // is valid per invariants.
+        Some(unsafe { &*head.as_raw().byte_sub(OFFSET).cast::<T>() })
+    }
+}
+
+impl<'a, T, const OFFSET: usize> FusedIterator for CListIter<'a, T, OFFSET> {}
+
+/// Create a C doubly-circular linked list interface [`CList`] from a raw `list_head` pointer.
+///
+/// This macro creates a [`CList<T, OFFSET>`] that can iterate over items of type `$rust_type`
+/// linked via the `$field` field in the underlying C struct `$c_type`.
+///
+/// # Arguments
+///
+/// - `$head`: Raw pointer to the sentinel `list_head` object (`*mut bindings::list_head`).
+/// - `$rust_type`: Each item's rust wrapper type.
+/// - `$c_type`: Each item's C struct type that contains the embedded `list_head`.
+/// - `$field`: The name of the `list_head` field within the C struct.
+///
+/// # Safety
+///
+/// The caller must ensure:
Quoted 8 lines

rust/kernel/clist.rs line 341

Quoted 5 lines
rust/kernel/clist.rs new file +5 -0
+/// - `$head` is a valid, initialized sentinel `list_head` pointing to a list that remains
+///   unmodified for the lifetime of the rust [`CList`].
+/// - The list contains items of type `$c_type` linked via an embedded `$field`.
+/// - `$rust_type` is `#[repr(transparent)]` over `$c_type` or has compatible layout.
+/// - The macro is called from an unsafe block.

This is not a safe requirement, probably lift it up and say "This is an unsafe macro.".

Sure, so like this then:
/// This is an unsafe macro. The caller must ensure:
/// - $head is a valid, initialized sentinel list_head...

Quoted 4 lines
Quoted 4 lines
rust/kernel/clist.rs new file +4 -0
+///
+/// # Examples
+///
+/// Refer to the examples in the [`crate::clist`] module documentation.
Quoted 8 lines

rust/kernel/clist.rs line 351

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+#[macro_export]
+macro_rules! clist_create {
+    ($head:expr, $rust_type:ty, $c_type:ty, $($field:tt).+) => {{
+        // Compile-time check that field path is a list_head.
+        let _: fn(*const $c_type) -> *const $crate::bindings::list_head =
+            |p| ::core::ptr::addr_of!((*p).$($field).+);

&raw const is preferred now.

Sure, will fix.

Quoted 10 lines
Quoted 10 lines
rust/kernel/clist.rs new file +6 -0
+
+        // Calculate offset and create `CList`.
+        const OFFSET: usize = ::core::mem::offset_of!($c_type, $($field).+);
+        $crate::clist::CList::<$rust_type, OFFSET>::from_raw($head)
+    }};
+}
rust/kernel/lib.rs +0 -1
index f812cf120042..cd7e6a1055b0 100644
--- a/rust/kernel/lib.rs
Quoted 8 lines

rust/kernel/lib.rs line 78

Quoted 6 lines

+++ b/rust/kernel/lib.rs

rust/kernel/lib.rs +1 -0
@@ -75,6 +75,7 @@
 pub mod bug;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clist;

Can we keep this pub(crate)?

Yes, will do.

Signature · 1 line
Joel Fernandes
reviewer
#4 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

Quoted 26 lines · Joel Fernandes

Hello, Gary,

Quoted 8 lines · Gary Guo
Quoted 5 lines · Joel Fernandes

Add a new module clist for working with C's doubly circular linked lists. Provide low-level iteration over list nodes.

Typed iteration over actual items is provided with a clist_create macro to assist in creation of the Clist type.

This should read "CList".

Sure, will fix.

Quoted 6 lines

I was quite dubious about the patch just from the title (everybody knows how easy a linked list is in Rust), but it turns out it is not as concerning as I expected, mostly due to the read-only nature of the particular implementation (a lot of the safety comments would be much more difficult to justify, say, if it's mutable). That said, still a lot of feedbacks below.

Sure, the reason for requiring this is interfacing with lists coming from C code. I'd see a future where we may want it mutable too (example, Rust code adding elements to the existing). At which point, the invariants/safety reasoning may change.

Quoted 7 lines
Quoted 3 lines

I think something like is okay in the short term. However, there's an growing interest in getting our Rust list API improved, so it could be ideal if eventually the Rust list can be capable of handling FFI lists, too.

Yeah we looked into that, if you see old threads, the conclusion was it is not a good fit for existing rust list abstractions. TLDR; it does not fit into their ownership/borrowing model.

Definitely not with the existing one that we have, as it handles only Arc. But the existing abstraction is also not good enough if you want to insert Box...

Quoted 17 lines

[...]

Quoted 6 lines
Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+
+/// Initialize a `list_head` object to point to itself.
+///
+/// # Safety
+///
+/// `list` must be a valid pointer to a `list_head` object.
Quoted 8 lines

rust/kernel/clist.rs line 106

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+#[inline]
+pub unsafe fn init_list_head(list: *mut bindings::list_head) {
+    // SAFETY: Caller guarantees `list` is a valid pointer to a `list_head`.
+    unsafe {
+        (*list).next = list;
+        (*list).prev = list;

This needs to be an atomic write or it'll depart from the C implementation.

Quoted 4 lines

I am curious what you mean by atomic write, can you define it? Does rust compiler have load/store fusing, invented stores, etc, like C does? Sorry I am only familiar with these concepts on C. Could you provide example of a race condition in Rust that can happen?

Oh yes, this would definitely happen. It's down to LLVM to compile anyway. If you create a reference, there'll be even more freedom to do these.

Quoted 7 lines

Also I did this addition based on feedback from past review:
https://lore.kernel.org/all/[email protected]/

There was some concerns around pointless function call overhead when the rust implementation is already quite intertwined with internals of the C linked list implementation. I do agree with that point of view too.

Overall our practice is to not duplicate code. Even ERR_PTR is calling into helpers.

For performance, it's a valid concern. However Alice and I have series out there that enable you to inline the helpers. I'd say unless there's an absolute need, we should do the helpers. Especially with caveats like WRITE_ONCE in this case.

Quoted 7 lines

Also see my other reply to Zhi on this helper topic, lets discuss there too, if that's Ok.

Quoted 3 lines
Quoted 2 lines
(quoted diff) +2 -0
+    }
+}
Quoted 8 lines
Quoted 2 lines

I don't think we want to publicly expose this! I've not found a user in the subsequent patch, too.

There are 2 users:

    pub fn try_init<E>(

and the self-tests:

This is not really a public user. It's hidden in the doc test too, you could initialize using try_init too.

Quoted 69 lines

//! # let head = head.as_mut_ptr();
//! # // SAFETY: head and all the items are test objects allocated in [..] //! # unsafe { init_list_head(head) };
//! #

Quoted 54 lines
Quoted 54 lines
rust/kernel/clist.rs new file +53 -0
+
+/// Wraps a `list_head` object for use in intrusive linked lists.
+///
+/// # Invariants
+///
+/// - [`CListHead`] represents an allocated and valid `list_head` structure.
+/// - Once a [`CListHead`] is created in Rust, it will not be modified by non-Rust code.
+/// - All `list_head` for individual items are not modified for the lifetime of [`CListHead`].
+#[repr(transparent)]
+pub struct CListHead(Opaque<bindings::list_head>);
+
+impl CListHead {
+    /// Create a `&CListHead` reference from a raw `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::list_head) -> &'a Self {
+        // SAFETY:
+        // - [`CListHead`] has same layout as `list_head`.
+        // - `ptr` is valid and unmodified for 'a.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Get the raw `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.0.get()
+    }
+
+    /// Get the next [`CListHead`] in the list.
+    #[inline]
+    pub fn next(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - `self.as_raw()` is valid per type invariants.
+        // - The `next` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).next) }
+    }
+
+    /// Get the previous [`CListHead`] in the list.
+    #[inline]
+    pub fn prev(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - self.as_raw() is valid per type invariants.
+        // - The `prev` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).prev) }
+    }
+
+    /// Check if this node is linked in a list (not isolated).
Quoted 8 lines

rust/kernel/clist.rs line 166

Quoted 5 lines
rust/kernel/clist.rs new file +5 -0
+    #[inline]
+    pub fn is_linked(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next != raw && (*raw).prev != raw }

While is this checking both prev and next? list_empty is just READ_ONCE(head->next) == head.

Quoted 2 lines

Sure, I can optimize to just check ->next, that makes sense. Will do.

The important part is to make sure we don't deviate from C implementation. A copy is already not good, and difference is worse.

Quoted 47 lines
Quoted 22 lines
Quoted 7 lines
rust/kernel/clist.rs new file +6 -0
+    }
+
+    /// Fallible pin-initializer that initializes and then calls user closure.
+    ///
+    /// Initializes the list head first, then passes `&CListHead` to the closure.
+    /// This hides the raw FFI pointer from the user.

rust/kernel/clist.rs line 182

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+    pub fn try_init<E>(
+        init_func: impl FnOnce(&CListHead) -> Result<(), E>,
+    ) -> impl PinInit<Self, E> {
+        // SAFETY: init_list_head initializes the list_head to point to itself.
+        // After initialization, we create a reference to pass to the closure.
+        unsafe {
+            pin_init::pin_init_from_closure(move |slot: *mut Self| {
+                init_list_head(slot.cast());
+                // SAFETY: slot is now initialized, safe to create reference.
+                init_func(&*slot)

Why is this callback necessary? The user can just create the list head and then reference it later? I don't see what this specifically gains over just doing

Quoted 8 lines
    fn new() -> impl PinInit<Self>;

and have user-side

    list <- CListHead::new(),
    _: {
        do_want_ever(&list)
    }

The list initialization can fail, see the GPU buddy patch:

        // Create pin-initializer that initializes list and allocates blocks.
        let init = try_pin_init!(AllocatedBlocks {
            list <- CListHead::try_init(|list| {
                // Lock while allocating to serialize with concurrent frees.
                let guard = buddy_arc.lock();

                // SAFETY: guard provides exclusive access, list is initialized.
                to_result(unsafe {
                    bindings::gpu_buddy_alloc_blocks(
                        guard.as_raw(),
                        params.start_range_address,
                        params.end_range_address,
                        params.size_bytes,
                        params.min_block_size_bytes,
Quoted 8 lines
                        list.as_raw(),
                        params.buddy_flags.as_raw(),
                    )
                })
            }),
            buddy: Arc::clone(&buddy_arc),
            flags: params.buddy_flags,
        });

The list initialization doesn't fail? It's the subsequent action you did that failed.

You can put failing things in the _: { ... } block too.

Quoted 101 lines
Quoted 12 lines
Quoted 12 lines
rust/kernel/clist.rs new file +9 -0
+            })
+        }
+    }
+}
+
+// SAFETY: [`CListHead`] can be sent to any thread.
+unsafe impl Send for CListHead {}
+
+// SAFETY: [`CListHead`] can be shared among threads as it is not modified
Quoted 8 lines

rust/kernel/clist.rs line 197

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+// by non-Rust code per type invariants.
+unsafe impl Sync for CListHead {}
+
+impl PartialEq for CListHead {
+    fn eq(&self, other: &Self) -> bool {
+        self.as_raw() == other.as_raw()

Or just core::ptr::eq(self, other)

Sure, will fix.

Quoted 39 lines
Quoted 24 lines
rust/kernel/clist.rs new file +23 -0
+    }
+}
+
+impl Eq for CListHead {}
+
+/// Low-level iterator over `list_head` nodes.
+///
+/// An iterator used to iterate over a C intrusive linked list (`list_head`). Caller has to
+/// perform conversion of returned [`CListHead`] to an item (using `container_of` macro or similar).
+///
+/// # Invariants
+///
+/// [`CListHeadIter`] is iterating over an allocated, initialized and valid list.
+struct CListHeadIter<'a> {
+    current_head: &'a CListHead,
+    list_head: &'a CListHead,
+}
+
+impl<'a> Iterator for CListHeadIter<'a> {
+    type Item = &'a CListHead;
+
+    #[inline]
+    fn next(&mut self) -> Option<Self::Item> {

rust/kernel/clist.rs line 230

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+        // Advance to next node.
+        let next = self.current_head.next();
+
+        // Check if we've circled back to the sentinel head.
+        if next == self.list_head {
+            None
+        } else {
+            self.current_head = next;
+            Some(self.current_head)
+        }

I think this could match the C iterator behaviour. When the iterator is created, a next is done first, and then subsequently you only need to check if current_head is list_head.

Quoted 2 lines

This is slightly better because the condition check does not need to dereference a pointer.

Sure, I can change it to that.

Quoted 23 lines
Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+impl<'a> FusedIterator for CListHeadIter<'a> {}
+
+/// A typed C linked list with a sentinel head.
+///
+/// A sentinel head represents the entire linked list and can be used for
+/// iteration over items of type `T`, it is not associated with a specific item.
+///
+/// The const generic `OFFSET` specifies the byte offset of the `list_head` field within
+/// the struct that `T` wraps.
+///

rust/kernel/clist.rs line 253

Quoted 10 lines
rust/kernel/clist.rs new file +10 -0
+/// # Invariants
+///
+/// - `head` is an allocated and valid C `list_head` structure that is the list's sentinel.
+/// - `OFFSET` is the byte offset of the `list_head` field within the struct that `T` wraps.
+/// - All the list's `list_head` nodes are allocated and have valid next/prev pointers.
+/// - The underlying `list_head` (and entire list) is not modified for the lifetime `'a`.
+pub struct CList<'a, T, const OFFSET: usize> {
+    head: &'a CListHead,
+    _phantom: PhantomData<&'a T>,
+}

Is there a reason that this is not

Quoted 8 lines

#[repr(transparent)]
struct CList(CListHead)

? We typically want to avoid putting reference inside the struct if it can be on the outside. This allows &self to be a single level of reference, not too.

It also means that you can just write &CList<_> in many cases, and doesn't need CList<'_, T> (plus all the benefits of a reference).

Sure! Will change to this. I am guessing you mean the following, but please let me know if you meant something else:

Quoted 7 lines

pub struct CList<T, const OFFSET: usize>(
CListHead,
PhantomData<T>,
);

I don't see any issues with my code using that, at the moment. Will let you know how it goes.

Yes, with #[repr(transparent)].

Quoted 101 lines
Quoted 25 lines
Quoted 25 lines
rust/kernel/clist.rs new file +25 -0
+impl<'a, T, const OFFSET: usize> CList<'a, T, OFFSET> {
+    /// Create a typed [`CList`] from a raw sentinel `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure
+    ///   representing a list sentinel.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    /// - The list must contain items where the `list_head` field is at byte offset `OFFSET`.
+    /// - `T` must be `#[repr(transparent)]` over the C struct.
+    #[inline]
+    pub unsafe fn from_raw(ptr: *mut bindings::list_head) -> Self {
+        Self {
+            // SAFETY: Caller guarantees `ptr` is a valid, sentinel `list_head` object.
+            head: unsafe { CListHead::from_raw(ptr) },
+            _phantom: PhantomData,
+        }
+    }
+
+    /// Get the raw sentinel `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.head.as_raw()
+    }
+
Quoted 8 lines

rust/kernel/clist.rs line 285

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+    /// Check if the list is empty.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next == raw }

self.head.is_linked()?

I'd considered is_linked() to be something that makes sense to call only on ClistHead objects that belong to a particular "item" node, not a sentinel node, so that was deliberate.

Though, I am Ok with doing it the way you are suggesting too (self.head.is_linked()), since it is functionally equivalent.

Quoted 51 lines
Quoted 51 lines
rust/kernel/clist.rs new file +51 -0
+    }
+
+    /// Create an iterator over typed items.
+    #[inline]
+    pub fn iter(&self) -> CListIter<'a, T, OFFSET> {
+        CListIter {
+            head_iter: CListHeadIter {
+                current_head: self.head,
+                list_head: self.head,
+            },
+            _phantom: PhantomData,
+        }
+    }
+}
+
+/// High-level iterator over typed list items.
+pub struct CListIter<'a, T, const OFFSET: usize> {
+    head_iter: CListHeadIter<'a>,
+    _phantom: PhantomData<&'a T>,
+}
+
+impl<'a, T, const OFFSET: usize> Iterator for CListIter<'a, T, OFFSET> {
+    type Item = &'a T;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        let head = self.head_iter.next()?;
+
+        // Convert to item using OFFSET.
+        // SAFETY: `item_ptr` calculation from `OFFSET` (calculated using offset_of!)
+        // is valid per invariants.
+        Some(unsafe { &*head.as_raw().byte_sub(OFFSET).cast::<T>() })
+    }
+}
+
+impl<'a, T, const OFFSET: usize> FusedIterator for CListIter<'a, T, OFFSET> {}
+
+/// Create a C doubly-circular linked list interface [`CList`] from a raw `list_head` pointer.
+///
+/// This macro creates a [`CList<T, OFFSET>`] that can iterate over items of type `$rust_type`
+/// linked via the `$field` field in the underlying C struct `$c_type`.
+///
+/// # Arguments
+///
+/// - `$head`: Raw pointer to the sentinel `list_head` object (`*mut bindings::list_head`).
+/// - `$rust_type`: Each item's rust wrapper type.
+/// - `$c_type`: Each item's C struct type that contains the embedded `list_head`.
+/// - `$field`: The name of the `list_head` field within the C struct.
+///
+/// # Safety
+///
+/// The caller must ensure:
Quoted 8 lines

rust/kernel/clist.rs line 341

Quoted 5 lines
rust/kernel/clist.rs new file +5 -0
+/// - `$head` is a valid, initialized sentinel `list_head` pointing to a list that remains
+///   unmodified for the lifetime of the rust [`CList`].
+/// - The list contains items of type `$c_type` linked via an embedded `$field`.
+/// - `$rust_type` is `#[repr(transparent)]` over `$c_type` or has compatible layout.
+/// - The macro is called from an unsafe block.

This is not a safe requirement, probably lift it up and say "This is an unsafe macro.".

Quoted 3 lines

Sure, so like this then:
/// This is an unsafe macro. The caller must ensure:
/// - $head is a valid, initialized sentinel list_head...

Yes.

Best,
Gary

Quoted 36 lines
Quoted 13 lines

rust/kernel/clist.rs line 351

Quoted 11 lines
rust/kernel/clist.rs new file +10 -0
+///
+/// # Examples
+///
+/// Refer to the examples in the [`crate::clist`] module documentation.
+#[macro_export]
+macro_rules! clist_create {
+    ($head:expr, $rust_type:ty, $c_type:ty, $($field:tt).+) => {{
+        // Compile-time check that field path is a list_head.
+        let _: fn(*const $c_type) -> *const $crate::bindings::list_head =
+            |p| ::core::ptr::addr_of!((*p).$($field).+);

&raw const is preferred now.

Sure, will fix.

Quoted 10 lines
Quoted 10 lines
rust/kernel/clist.rs new file +6 -0
+
+        // Calculate offset and create `CList`.
+        const OFFSET: usize = ::core::mem::offset_of!($c_type, $($field).+);
+        $crate::clist::CList::<$rust_type, OFFSET>::from_raw($head)
+    }};
+}
rust/kernel/lib.rs +0 -1
index f812cf120042..cd7e6a1055b0 100644
--- a/rust/kernel/lib.rs
Quoted 8 lines

rust/kernel/lib.rs line 78

Quoted 6 lines

+++ b/rust/kernel/lib.rs

rust/kernel/lib.rs +1 -0
@@ -75,6 +75,7 @@
 pub mod bug;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clist;

Can we keep this pub(crate)?

Yes, will do.

author reviewer
#5 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

Quoted 12 lines · Gary Guo
Quoted 5 lines

There are 2 users:

    pub fn try_init<E>(

and the self-tests:

This is not really a public user. It's hidden in the doc test too, you could initialize using try_init too.

Quoted 4 lines

//! # let head = head.as_mut_ptr();
//! # // SAFETY: head and all the items are test objects allocated in [..] //! # unsafe { init_list_head(head) };
//! #

True, but if we initialize purely within try_init() without using a helper, does that not defeat the argument of adding a separate INIT_LIST_HEAD helper such that we don't deviate from the C side?

Regarding your other comment about the try_init block itself, I will take a look at your suggestion and see if I can simplify.

Signature · 1 line
Joel Fernandes
author reviewer
#6 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

In reply to Gary Guo
Quoted 15 lines · Gary Guo
Quoted 15 lines

[...]

Quoted 14 lines

rust/kernel/clist.rs line 106

Quoted 12 lines
rust/kernel/clist.rs new file +12 -0
+
+/// Initialize a `list_head` object to point to itself.
+///
+/// # Safety
+///
+/// `list` must be a valid pointer to a `list_head` object.
+#[inline]
+pub unsafe fn init_list_head(list: *mut bindings::list_head) {
+    // SAFETY: Caller guarantees `list` is a valid pointer to a `list_head`.
+    unsafe {
+        (*list).next = list;
+        (*list).prev = list;

This needs to be an atomic write or it'll depart from the C implementation.

Quoted 8 lines
Quoted 4 lines

I am curious what you mean by atomic write, can you define it? Does rust compiler have load/store fusing, invented stores, etc, like C does? Sorry I am only familiar with these concepts on C. Could you provide example of a race condition in Rust that can happen?

Oh yes, this would definitely happen. It's down to LLVM to compile anyway. If you create a reference, there'll be even more freedom to do these.

Ok.

Quoted 7 lines
Quoted 6 lines

Also I did this addition based on feedback from past review:
https://lore.kernel.org/all/[email protected]/

There was some concerns around pointless function call overhead when the rust implementation is already quite intertwined with internals of the C linked list implementation. I do agree with that point of view too.

Quoted 6 lines

Overall our practice is to not duplicate code. Even ERR_PTR is calling into helpers.

For performance, it's a valid concern. However Alice and I have series out there that enable you to inline the helpers. I'd say unless there's an absolute need, we should do the helpers. Especially with caveats like WRITE_ONCE in this case.

Sounds good, so I will then go back to adding a INIT_LIST_HEAD C helper for the next spin. I agree with the suggestion and now that we are inlining helpers, there seems little point in adding a separate rust function to do the same.

Signature · 1 line
Joel Fernandes
author reviewer
#7 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

In reply to Gary Guo
Quoted 38 lines · Gary Guo
Quoted 29 lines
Quoted 4 lines

Why is this callback necessary? The user can just create the list head and then reference it later? I don't see what this specifically gains over just doing

Quoted 8 lines
    fn new() -> impl PinInit<Self>;

and have user-side

    list <- CListHead::new(),
    _: {
        do_want_ever(&list)
    }

The list initialization can fail, see the GPU buddy patch:

        // Create pin-initializer that initializes list and allocates blocks.
        let init = try_pin_init!(AllocatedBlocks {
            list <- CListHead::try_init(|list| {
                // Lock while allocating to serialize with concurrent frees.
                let guard = buddy_arc.lock();

                // SAFETY: guard provides exclusive access, list is initialized.
                to_result(unsafe {
                    bindings::gpu_buddy_alloc_blocks(
                        guard.as_raw(),
                        params.start_range_address,
                        params.end_range_address,
                        params.size_bytes,
                        params.min_block_size_bytes,
Quoted 8 lines
                        list.as_raw(),
                        params.buddy_flags.as_raw(),
                    )
                })
            }),
            buddy: Arc::clone(&buddy_arc),
            flags: params.buddy_flags,
        });
Quoted 4 lines

The list initialization doesn't fail? It's the subsequent action you did that failed.

You can put failing things in the _: { ... } block too.

This worked out well, thanks for the suggestion! I've updated the code to use CListHead::new() with the failable allocation in a _: { ... } block:

        let init = try_pin_init!(AllocatedBlocks {
            buddy: Arc::clone(&buddy_arc),
            list <- CListHead::new(),
            flags: params.buddy_flags,
            _: {
                // Lock while allocating to serialize with concurrent frees.
                let guard = buddy.lock();

                // SAFETY: `guard` provides exclusive access to the buddy allocator.
                to_result(unsafe {
                    bindings::gpu_buddy_alloc_blocks(
                        guard.as_raw(),
                        params.start_range_address,
                        params.end_range_address,
                        params.size_bytes,
                        params.min_block_size_bytes,
                        list.as_raw(),
                        params.buddy_flags.as_raw(),
                    )
                })?
            }
        });

I'll remove the try_init() method from CListHead since new() is sufficient.

Signature · 1 line
Joel Fernandes
#8 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

In reply to Joel Fernandes
Quoted 8 lines · On Tue, 20 Jan 2026 15:42:38 -0500 Joel Fernandes <[email protected]> wrote:

Add a new module clist for working with C's doubly circular linked lists. Provide low-level iteration over list nodes.

Typed iteration over actual items is provided with a clist_create macro to assist in creation of the Clist type.

---

snip

Quoted 9 lines
rust/kernel/clist.rs new file +8 -0
+/// Initialize a `list_head` object to point to itself.
+///
+/// # Safety
+///
+/// `list` must be a valid pointer to a `list_head` object.
+#[inline]
+pub unsafe fn init_list_head(list: *mut bindings::list_head) {
+    // SAFETY: Caller guarantees `list` is a valid pointer to a

list_head.

rust/kernel/clist.rs line 109

Quoted 6 lines
rust/kernel/clist.rs new file +6 -0
+    unsafe {
+        (*list).next = list;
+        (*list).prev = list;
+    }
+}
+

Might be better to have a C helper? since INIT_LIST_HEAD() has WRITE_ONCE() for memory ordering. This one seems not equal to it.

Z.

Quoted 276 lines
rust/kernel/clist.rs new file +5 -0
+/// Wraps a `list_head` object for use in intrusive linked lists.
+///
+/// # Invariants
+///
+/// - [`CListHead`] represents an allocated and valid `list_head`

structure. +/// - Once a [CListHead] is created in Rust, it will not be modified by non-Rust code. +/// - All list_head for individual items are not modified for the lifetime of [CListHead].

rust/kernel/clist.rs new file +8 -0
+#[repr(transparent)] +pub struct CListHead(Opaque<bindings::list_head>);
+
+impl CListHead {
+    /// Create a `&CListHead` reference from a raw `list_head` pointer.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized

list_head structure.

rust/kernel/clist.rs new file +3 -0
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::list_head) -> &'a

Self {

rust/kernel/clist.rs new file +41 -0
+        // SAFETY:
+        // - [`CListHead`] has same layout as `list_head`.
+        // - `ptr` is valid and unmodified for 'a.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Get the raw `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.0.get()
+    }
+
+    /// Get the next [`CListHead`] in the list.
+    #[inline]
+    pub fn next(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - `self.as_raw()` is valid per type invariants.
+        // - The `next` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).next) }
+    }
+
+    /// Get the previous [`CListHead`] in the list.
+    #[inline]
+    pub fn prev(&self) -> &Self {
+        let raw = self.as_raw();
+        // SAFETY:
+        // - self.as_raw() is valid per type invariants.
+        // - The `prev` pointer is guaranteed to be non-NULL.
+        unsafe { Self::from_raw((*raw).prev) }
+    }
+
+    /// Check if this node is linked in a list (not isolated).
+    #[inline]
+    pub fn is_linked(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next != raw && (*raw).prev != raw }
+    }
+
+    /// Fallible pin-initializer that initializes and then calls user

closure.

(quoted diff) +2 -0
+    ///
+    /// Initializes the list head first, then passes `&CListHead` to

the closure.

rust/kernel/clist.rs new file +5 -0
+    /// This hides the raw FFI pointer from the user.
+    pub fn try_init<E>(
+        init_func: impl FnOnce(&CListHead) -> Result<(), E>,
+    ) -> impl PinInit<Self, E> {
+        // SAFETY: init_list_head initializes the list_head to point to

itself.
+ // After initialization, we create a reference to pass to the
closure.

rust/kernel/clist.rs new file +4 -0
+        unsafe {
+            pin_init::pin_init_from_closure(move |slot: *mut Self| {
+                init_list_head(slot.cast());
+                // SAFETY: slot is now initialized, safe to create

reference.

rust/kernel/clist.rs new file +10 -0
+                init_func(&*slot)
+            })
+        }
+    }
+}
+
+// SAFETY: [`CListHead`] can be sent to any thread.
+unsafe impl Send for CListHead {}
+
+// SAFETY: [`CListHead`] can be shared among threads as it is not

modified +// by non-Rust code per type invariants.

rust/kernel/clist.rs new file +13 -0
+unsafe impl Sync for CListHead {}
+
+impl PartialEq for CListHead {
+    fn eq(&self, other: &Self) -> bool {
+        self.as_raw() == other.as_raw()
+    }
+}
+
+impl Eq for CListHead {}
+
+/// Low-level iterator over `list_head` nodes.
+///
+/// An iterator used to iterate over a C intrusive linked list

(list_head). Caller has to +/// perform conversion of returned [CListHead] to an item (using container_of macro or similar). +///

rust/kernel/clist.rs new file +3 -0
+/// # Invariants
+///
+/// [`CListHeadIter`] is iterating over an allocated, initialized and

valid list. +struct CListHeadIter<'a> {

rust/kernel/clist.rs new file +27 -0
+    current_head: &'a CListHead,
+    list_head: &'a CListHead,
+}
+
+impl<'a> Iterator for CListHeadIter<'a> {
+    type Item = &'a CListHead;
+
+    #[inline]
+    fn next(&mut self) -> Option<Self::Item> {
+        // Advance to next node.
+        let next = self.current_head.next();
+
+        // Check if we've circled back to the sentinel head.
+        if next == self.list_head {
+            None
+        } else {
+            self.current_head = next;
+            Some(self.current_head)
+        }
+    }
+}
+
+impl<'a> FusedIterator for CListHeadIter<'a> {}
+
+/// A typed C linked list with a sentinel head.
+///
+/// A sentinel head represents the entire linked list and can be used

for +/// iteration over items of type T, it is not associated with a specific item. +///
+/// The const generic OFFSET specifies the byte offset of the list_head field within +/// the struct that T wraps.

rust/kernel/clist.rs new file +4 -0
+///
+/// # Invariants
+///
+/// - `head` is an allocated and valid C `list_head` structure that is

the list's sentinel. +/// - OFFSET is the byte offset of the list_head field within the struct that T wraps. +/// - All the list's list_head nodes are allocated and have valid next/prev pointers. +/// - The underlying list_head (and entire list) is not modified for the lifetime 'a. +pub struct CList<'a, T, const OFFSET:
usize> {

rust/kernel/clist.rs new file +6 -0
+    head: &'a CListHead,
+    _phantom: PhantomData<&'a T>,
+}
+
+impl<'a, T, const OFFSET: usize> CList<'a, T, OFFSET> {
+    /// Create a typed [`CList`] from a raw sentinel `list_head`

pointer.

rust/kernel/clist.rs new file +4 -0
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to an allocated and initialized

list_head structure

rust/kernel/clist.rs new file +3 -0
+    ///   representing a list sentinel.
+    /// - `ptr` must remain valid and unmodified for the lifetime `'a`.
+    /// - The list must contain items where the `list_head` field is at

byte offset OFFSET.

rust/kernel/clist.rs new file +5 -0
+    /// - `T` must be `#[repr(transparent)]` over the C struct.
+    #[inline]
+    pub unsafe fn from_raw(ptr: *mut bindings::list_head) -> Self {
+        Self {
+            // SAFETY: Caller guarantees `ptr` is a valid, sentinel

list_head object.

rust/kernel/clist.rs new file +46 -0
+            head: unsafe { CListHead::from_raw(ptr) },
+            _phantom: PhantomData,
+        }
+    }
+
+    /// Get the raw sentinel `list_head` pointer.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::list_head {
+        self.head.as_raw()
+    }
+
+    /// Check if the list is empty.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        let raw = self.as_raw();
+        // SAFETY: self.as_raw() is valid per type invariants.
+        unsafe { (*raw).next == raw }
+    }
+
+    /// Create an iterator over typed items.
+    #[inline]
+    pub fn iter(&self) -> CListIter<'a, T, OFFSET> {
+        CListIter {
+            head_iter: CListHeadIter {
+                current_head: self.head,
+                list_head: self.head,
+            },
+            _phantom: PhantomData,
+        }
+    }
+}
+
+/// High-level iterator over typed list items.
+pub struct CListIter<'a, T, const OFFSET: usize> {
+    head_iter: CListHeadIter<'a>,
+    _phantom: PhantomData<&'a T>,
+}
+
+impl<'a, T, const OFFSET: usize> Iterator for CListIter<'a, T, OFFSET> {
+    type Item = &'a T;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        let head = self.head_iter.next()?;
+
+        // Convert to item using OFFSET.
+        // SAFETY: `item_ptr` calculation from `OFFSET` (calculated

using offset_of!)

rust/kernel/clist.rs new file +6 -0
+        // is valid per invariants.
+        Some(unsafe { &*head.as_raw().byte_sub(OFFSET).cast::<T>() })
+    }
+}
+
+impl<'a, T, const OFFSET: usize> FusedIterator for CListIter<'a, T,

OFFSET> {} +
+/// Create a C doubly-circular linked list interface [CList] from a raw list_head pointer. +///
+/// This macro creates a [CList<T, OFFSET>] that can iterate over items of type $rust_type +/// linked via the $field field in the underlying C struct $c_type. +///

rust/kernel/clist.rs new file +3 -0
+/// # Arguments
+///
+/// - `$head`: Raw pointer to the sentinel `list_head` object (`*mut

bindings::list_head). +/// - $rust_type: Each item's rust wrapper type. +/// - $c_type: Each item's C struct type that contains the embedded list_head. +/// - $field: The name of the list_head` field within the C struct. +///

rust/kernel/clist.rs new file +4 -0
+/// # Safety
+///
+/// The caller must ensure:
+/// - `$head` is a valid, initialized sentinel `list_head` pointing to

a list that remains +/// unmodified for the lifetime of the rust
[CList]. +/// - The list contains items of type $c_type linked via an embedded $field. +/// - $rust_type is #[repr(transparent)] over $c_type or has compatible layout. +/// - The macro is called from an unsafe block. +///

rust/kernel/clist.rs new file +8 -0
+/// # Examples
+///
+/// Refer to the examples in the [`crate::clist`] module documentation.
+#[macro_export]
+macro_rules! clist_create {
+    ($head:expr, $rust_type:ty, $c_type:ty, $($field:tt).+) => {{
+        // Compile-time check that field path is a list_head.
+        let _: fn(*const $c_type) -> *const $crate::bindings::list_head

=

rust/kernel/clist.rs new file +4 -0
+            |p| ::core::ptr::addr_of!((*p).$($field).+);
+
+        // Calculate offset and create `CList`.
+        const OFFSET: usize = ::core::mem::offset_of!($c_type,

$($field).+);

rust/kernel/clist.rs new file +3 -0
+        $crate::clist::CList::<$rust_type, OFFSET>::from_raw($head)
+    }};
+}
rust/kernel/lib.rs +1 -0
index f812cf120042..cd7e6a1055b0 100644
@@ -75,6 +75,7 @@
 pub mod bug;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clist;
 pub mod clk;
 #[cfg(CONFIG_CONFIGFS_FS)]
 pub mod configfs;
#9 lore raw

Re: [PATCH RFC v6 01/26] rust: clist: Add support to interface with C linked lists

Quoted 11 lines · Zhi Wang
Quoted 11 lines
rust/kernel/clist.rs new file +8 -0
+/// Initialize a `list_head` object to point to itself.
+///
+/// # Safety
+///
+/// `list` must be a valid pointer to a `list_head` object.
+#[inline]
+pub unsafe fn init_list_head(list: *mut bindings::list_head) {
+    // SAFETY: Caller guarantees `list` is a valid pointer to a

list_head.
+ unsafe {

Quoted 8 lines

rust/kernel/clist.rs line 109

Quoted 5 lines
rust/kernel/clist.rs new file +5 -0
+        (*list).next = list;
+        (*list).prev = list;
+    }
+}
+

Might be better to have a C helper? since INIT_LIST_HEAD() has WRITE_ONCE() for memory ordering. This one seems not equal to it.

WRITE_ONCE() is not really about CPU memory ordering though, it is about compiler optimizations. On the C side, I think it is needed in case of list_for_each_entry_rcu(), to avoid the case of invented stores or store fusing, but here we are not doing RCU-based iteration.

Anyway, if we want to future proof that, I am Ok with adding the helper back (which I actually initially had but feedback from past review was to just inline it into rust).

But I am not sure if we have this issue for the Rust compiler, like we do for C. Rust does not allow raw pointers to be concurrently read/written using plain accesses, so should be already protected due to the borrow checker and compiler itself right?

Adding some interested folks as well to CC for the topic of _ONCE, +Boqun +Paul.

--
Joel Fernandes

author reviewer
#10 lore raw

RFC PATCH v6 2/26 gpu: Move DRM buddy allocator one level up

In reply to Joel Fernandes

Move the DRM buddy allocator one level up so that it can be used by GPU drivers (example, nova-core) that have usecases other than DRM (such as VFIO vGPU support). Modify the API, structures and Kconfigs to use "gpu_buddy" terminology. Adapt the drivers and tests to use the new API.

The commit cannot be split due to bisectability, however no functional change is intended. Verified by running K-UNIT tests and build tested various configurations.

---
 Documentation/gpu/drm-mm.rst                  |   10 +-
 drivers/gpu/Kconfig                           |   13 +
 drivers/gpu/Makefile                          |    2 +
 drivers/gpu/buddy.c                           | 1310 +++++++++++++++++
 drivers/gpu/drm/Kconfig                       |    1 +
 drivers/gpu/drm/Kconfig.debug                 |    4 +-
 drivers/gpu/drm/amd/amdgpu/Kconfig            |    1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |    2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |   12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  |   80 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h  |   20 +-
 drivers/gpu/drm/drm_buddy.c                   | 1284 +---------------
 drivers/gpu/drm/i915/Kconfig                  |    1 +
 drivers/gpu/drm/i915/i915_scatterlist.c       |   10 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c |   55 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |    6 +-
 .../drm/i915/selftests/intel_memory_region.c  |   20 +-
 drivers/gpu/drm/tests/Makefile                |    1 -
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |    5 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  |   18 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |    4 +-
 drivers/gpu/drm/xe/Kconfig                    |    1 +
 drivers/gpu/drm/xe/xe_res_cursor.h            |   34 +-
 drivers/gpu/drm/xe/xe_svm.c                   |   12 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c          |   73 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h    |    4 +-
 drivers/gpu/tests/Makefile                    |    3 +
 .../gpu_buddy_test.c}                         |  390 ++---
 drivers/gpu/tests/gpu_random.c                |   48 +
 drivers/gpu/tests/gpu_random.h                |   28 +
 drivers/video/Kconfig                         |    2 +
 include/drm/drm_buddy.h                       |  163 +-
 include/linux/gpu_buddy.h                     |  177 +++
 33 files changed, 1995 insertions(+), 1799 deletions(-)
 create mode 100644 drivers/gpu/Kconfig
 create mode 100644 drivers/gpu/buddy.c
 create mode 100644 drivers/gpu/tests/Makefile
 rename drivers/gpu/{drm/tests/drm_buddy_test.c => tests/gpu_buddy_test.c} (68%)
 create mode 100644 drivers/gpu/tests/gpu_random.c
 create mode 100644 drivers/gpu/tests/gpu_random.h
 create mode 100644 include/linux/gpu_buddy.h
Documentation/gpu/drm-mm.rst +8 -2
index d55751cad67c..8e0d31230b29 100644
@@ -509,8 +509,14 @@ DRM GPUVM Function References
 DRM Buddy Allocator
 ===================
 
-DRM Buddy Function References
------------------------------
+Buddy Allocator Function References (GPU buddy)
+-----------------------------------------------
+
+.. kernel-doc:: drivers/gpu/buddy.c
+   :export:
+
+DRM Buddy Specific Logging Function References
+----------------------------------------------
 
 .. kernel-doc:: drivers/gpu/drm/drm_buddy.c
    :export:
drivers/gpu/Kconfig new file +13 -0
new file mode 100644
index 000000000000..22dd29cd50b5
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config GPU_BUDDY
+	bool
+	help
+	  A page based buddy allocator for GPU memory.
+
+config GPU_BUDDY_KUNIT_TEST
+	tristate "KUnit tests for GPU buddy allocator" if !KUNIT_ALL_TESTS
+	depends on GPU_BUDDY && KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  KUnit tests for the GPU buddy allocator.
drivers/gpu/Makefile +2 -0
index 36a54d456630..5063caccabdf 100644
@@ -6,3 +6,5 @@ obj-y			+= host1x/ drm/ vga/
 obj-$(CONFIG_IMX_IPUV3_CORE)	+= ipu-v3/
 obj-$(CONFIG_TRACE_GPU_MEM)		+= trace/
 obj-$(CONFIG_NOVA_CORE)		+= nova-core/
+obj-$(CONFIG_GPU_BUDDY)		+= buddy.o
+obj-y				+= tests/
drivers/gpu/buddy.c new file +1310 -0

1,310 lines not on this page · raw message

drivers/gpu/drm/Kconfig +1 -0
index 7e6bc0b3a589..0475defb37f0 100644
@@ -220,6 +220,7 @@ config DRM_GPUSVM
 config DRM_BUDDY
 	tristate
 	depends on DRM
+	select GPU_BUDDY
 	help
 	  A page based buddy allocator
 
drivers/gpu/drm/Kconfig.debug +3 -1
index 05dc43c0b8c5..1f4c408c7920 100644
@@ -71,6 +71,7 @@ config DRM_KUNIT_TEST
 	select DRM_KUNIT_TEST_HELPERS
 	select DRM_LIB_RANDOM
 	select DRM_SYSFB_HELPER
+	select GPU_BUDDY
 	select PRIME_NUMBERS
 	default KUNIT_ALL_TESTS
 	help
@@ -88,10 +89,11 @@ config DRM_TTM_KUNIT_TEST
 	tristate "KUnit tests for TTM" if !KUNIT_ALL_TESTS
 	default n
 	depends on DRM && KUNIT && MMU && (UML || COMPILE_TEST)
-	select DRM_TTM
 	select DRM_BUDDY
+	select DRM_TTM
 	select DRM_EXPORT_FOR_TESTS if m
 	select DRM_KUNIT_TEST_HELPERS
+	select GPU_BUDDY
 	default KUNIT_ALL_TESTS
 	help
 	  Enables unit tests for TTM, a GPU memory manager subsystem used
drivers/gpu/drm/amd/amdgpu/Kconfig +1 -0
index 7f515be5185d..bb131543e1d9 100644
@@ -23,6 +23,7 @@ config DRM_AMDGPU
 	select CRC16
 	select BACKLIGHT_CLASS_DEVICE
 	select INTERVAL_TREE
+	select GPU_BUDDY
 	select DRM_BUDDY
 	select DRM_SUBALLOC_HELPER
 	select DRM_EXEC
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
index 2a6cf7963dde..e0bd8a68877f 100644
@@ -5654,7 +5654,7 @@ int amdgpu_ras_add_critical_region(struct amdgpu_device *adev,
 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
 	struct amdgpu_vram_mgr_resource *vres;
 	struct ras_critical_region *region;
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	int ret = 0;
 
 	if (!bo || !bo->tbo.resource)
drivers/gpu/drm/amd/amdgpu/amdgpu_res_cursor.h +6 -6
index be2e56ce1355..8908d9e08a30 100644
@@ -55,7 +55,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 				    uint64_t start, uint64_t size,
 				    struct amdgpu_res_cursor *cur)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct list_head *head, *next;
 	struct drm_mm_node *node;
 
@@ -71,7 +71,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 		head = &to_amdgpu_vram_mgr_resource(res)->blocks;
 
 		block = list_first_entry_or_null(head,
-						 struct drm_buddy_block,
+						 struct gpu_buddy_block,
 						 link);
 		if (!block)
 			goto fallback;
@@ -81,7 +81,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 
 			next = block->link.next;
 			if (next != head)
-				block = list_entry(next, struct drm_buddy_block, link);
+				block = list_entry(next, struct gpu_buddy_block, link);
 		}
 
 		cur->start = amdgpu_vram_mgr_block_start(block) + start;
@@ -125,7 +125,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
  */
 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 	struct drm_mm_node *node;
 	struct list_head *next;
 
@@ -146,7 +146,7 @@ static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
 		block = cur->node;
 
 		next = block->link.next;
-		block = list_entry(next, struct drm_buddy_block, link);
+		block = list_entry(next, struct gpu_buddy_block, link);
 
 		cur->node = block;
 		cur->start = amdgpu_vram_mgr_block_start(block);
@@ -175,7 +175,7 @@ static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
  */
 static inline bool amdgpu_res_cleared(struct amdgpu_res_cursor *cur)
 {
-	struct drm_buddy_block *block;
+	struct gpu_buddy_block *block;
 
 	switch (cur->mem_type) {
 	case TTM_PL_VRAM:
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +41 -39

260 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h +10 -10
index 5f5fd9a911c2..429a21a2e9b2 100644
@@ -24,11 +24,11 @@
 #ifndef __AMDGPU_VRAM_MGR_H__
 #define __AMDGPU_VRAM_MGR_H__
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 
 struct amdgpu_vram_mgr {
 	struct ttm_resource_manager manager;
-	struct drm_buddy mm;
+	struct gpu_buddy mm;
 	/* protects access to buffer objects */
 	struct mutex lock;
 	struct list_head reservations_pending;
@@ -57,19 +57,19 @@ struct amdgpu_vram_mgr_resource {
 	struct amdgpu_vres_task task;
 };
 
-static inline u64 amdgpu_vram_mgr_block_start(struct drm_buddy_block *block)
+static inline u64 amdgpu_vram_mgr_block_start(struct gpu_buddy_block *block)
 {
-	return drm_buddy_block_offset(block);
+	return gpu_buddy_block_offset(block);
 }
 
-static inline u64 amdgpu_vram_mgr_block_size(struct drm_buddy_block *block)
+static inline u64 amdgpu_vram_mgr_block_size(struct gpu_buddy_block *block)
 {
-	return (u64)PAGE_SIZE << drm_buddy_block_order(block);
+	return (u64)PAGE_SIZE << gpu_buddy_block_order(block);
 }
 
-static inline bool amdgpu_vram_mgr_is_cleared(struct drm_buddy_block *block)
+static inline bool amdgpu_vram_mgr_is_cleared(struct gpu_buddy_block *block)
 {
-	return drm_buddy_block_is_clear(block);
+	return gpu_buddy_block_is_clear(block);
 }
 
 static inline struct amdgpu_vram_mgr_resource *
@@ -82,8 +82,8 @@ static inline void amdgpu_vram_mgr_set_cleared(struct ttm_resource *res)
 {
 	struct amdgpu_vram_mgr_resource *ares = to_amdgpu_vram_mgr_resource(res);
 
-	WARN_ON(ares->flags & DRM_BUDDY_CLEARED);
-	ares->flags |= DRM_BUDDY_CLEARED;
+	WARN_ON(ares->flags & GPU_BUDDY_CLEARED);
+	ares->flags |= GPU_BUDDY_CLEARED;
 }
 
 int amdgpu_vram_mgr_query_address_block_info(struct amdgpu_vram_mgr *mgr,
drivers/gpu/drm/drm_buddy.c +17 -1267

1,259 lines not on this page · raw message

drivers/gpu/drm/i915/Kconfig +1 -0
index 5e939004b646..859aeca87c19 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/i915_scatterlist.c +5 -5
index 4d830740946d..6a34dae13769 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +28 -27

55 lines not on this page · raw message

drivers/gpu/drm/i915/i915_ttm_buddy_manager.h +3 -3
index d64620712830..4a92dcf09766 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/selftests/intel_memory_region.c +10 -10
index 7b856b5090f9..8307390943a2 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/tests/Makefile +0 -1
index 87d5d5f9332a..d2e2e3d8349a 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +3 -2
index 2eda87882e65..ffa12473077c 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/ttm/tests/ttm_mock_manager.c +9 -9
index dd395229e388..294d56d9067e 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/ttm/tests/ttm_mock_manager.h +2 -2
index e4c95f86a467..08710756fd8e 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/Kconfig +1 -0
index 4b288eb3f5b0..982ef754742e 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_res_cursor.h +17 -17
index 4e00008b7081..5f4ab08c0686 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_svm.c +6 -6
index f97e0af6a9b0..2b7e266f9bdd 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +37 -36

73 lines not on this page · raw message

drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +2 -2
index a71e14818ec2..9106da056b49 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/Makefile new file +3 -0
new file mode 100644
index 000000000000..31a5ff44cb4e

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/gpu_buddy_test.c renamed from drivers/gpu/drm/tests/drm_buddy_test.c +195 -195

390 lines not on this page · raw message

drivers/gpu/tests/gpu_random.c new file +48 -0

48 lines not on this page · raw message

drivers/gpu/tests/gpu_random.h new file +28 -0
new file mode 100644
index 000000000000..b68cf3448264

Lines of this file are not shown. View the raw message.

drivers/video/Kconfig +2 -0
index d51777df12d1..6ae1383b0e2e 100644

Lines of this file are not shown. View the raw message.

include/drm/drm_buddy.h +5 -158

163 lines not on this page · raw message

include/linux/gpu_buddy.h new file +177 -0

177 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

maintainer Reviewed-by on v6 2/26
#11 lore raw

Re: [PATCH RFC v6 02/26] gpu: Move DRM buddy allocator one level up

Quoted 11 lines · Joel Fernandes

Move the DRM buddy allocator one level up so that it can be used by GPU drivers (example, nova-core) that have usecases other than DRM (such as VFIO vGPU support). Modify the API, structures and Kconfigs to use "gpu_buddy" terminology. Adapt the drivers and tests to use the new API.

The commit cannot be split due to bisectability, however no functional change is intended. Verified by running K-UNIT tests and build tested various configurations.

I suggested this and think it's a good idea.

Quoted 5,528 lines
---
 Documentation/gpu/drm-mm.rst                  |   10 +-
 drivers/gpu/Kconfig                           |   13 +
 drivers/gpu/Makefile                          |    2 +
 drivers/gpu/buddy.c                           | 1310 +++++++++++++++++
 drivers/gpu/drm/Kconfig                       |    1 +
 drivers/gpu/drm/Kconfig.debug                 |    4 +-
 drivers/gpu/drm/amd/amdgpu/Kconfig            |    1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c       |    2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |   12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  |   80 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h  |   20 +-
 drivers/gpu/drm/drm_buddy.c                   | 1284 +---------------
 drivers/gpu/drm/i915/Kconfig                  |    1 +
 drivers/gpu/drm/i915/i915_scatterlist.c       |   10 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c |   55 +-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |    6 +-
 .../drm/i915/selftests/intel_memory_region.c  |   20 +-
 drivers/gpu/drm/tests/Makefile                |    1 -
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  |    5 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  |   18 +-
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |    4 +-
 drivers/gpu/drm/xe/Kconfig                    |    1 +
 drivers/gpu/drm/xe/xe_res_cursor.h            |   34 +-
 drivers/gpu/drm/xe/xe_svm.c                   |   12 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c          |   73 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h    |    4 +-
 drivers/gpu/tests/Makefile                    |    3 +
 .../gpu_buddy_test.c}                         |  390 ++---
 drivers/gpu/tests/gpu_random.c                |   48 +
 drivers/gpu/tests/gpu_random.h                |   28 +
 drivers/video/Kconfig                         |    2 +
 include/drm/drm_buddy.h                       |  163 +-
 include/linux/gpu_buddy.h                     |  177 +++
 33 files changed, 1995 insertions(+), 1799 deletions(-)
 create mode 100644 drivers/gpu/Kconfig
 create mode 100644 drivers/gpu/buddy.c
 create mode 100644 drivers/gpu/tests/Makefile
 rename drivers/gpu/{drm/tests/drm_buddy_test.c => tests/gpu_buddy_test.c} (68%)
 create mode 100644 drivers/gpu/tests/gpu_random.c
 create mode 100644 drivers/gpu/tests/gpu_random.h
 create mode 100644 include/linux/gpu_buddy.h
Documentation/gpu/drm-mm.rst +8 -2
index d55751cad67c..8e0d31230b29 100644
@@ -509,8 +509,14 @@ DRM GPUVM Function References
 DRM Buddy Allocator
 ===================
 
-DRM Buddy Function References
------------------------------
+Buddy Allocator Function References (GPU buddy)
+-----------------------------------------------
+
+.. kernel-doc:: drivers/gpu/buddy.c
+   :export:
+
+DRM Buddy Specific Logging Function References
+----------------------------------------------
 
 .. kernel-doc:: drivers/gpu/drm/drm_buddy.c
    :export:
drivers/gpu/Kconfig new file +13 -0
new file mode 100644
index 000000000000..22dd29cd50b5
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config GPU_BUDDY
+       bool
+       help
+         A page based buddy allocator for GPU memory.
+
+config GPU_BUDDY_KUNIT_TEST
+       tristate "KUnit tests for GPU buddy allocator" if !KUNIT_ALL_TESTS
+       depends on GPU_BUDDY && KUNIT
+       default KUNIT_ALL_TESTS
+       help
+         KUnit tests for the GPU buddy allocator.
drivers/gpu/Makefile +2 -0
index 36a54d456630..5063caccabdf 100644
@@ -6,3 +6,5 @@ obj-y                   += host1x/ drm/ vga/
 obj-$(CONFIG_IMX_IPUV3_CORE)   += ipu-v3/
 obj-$(CONFIG_TRACE_GPU_MEM)            += trace/
 obj-$(CONFIG_NOVA_CORE)                += nova-core/
+obj-$(CONFIG_GPU_BUDDY)                += buddy.o
+obj-y                          += tests/
drivers/gpu/buddy.c new file +1310 -0
new file mode 100644
index 000000000000..1347c0436617
@@ -0,0 +1,1310 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include <kunit/test-bug.h>
+
+#include <linux/export.h>
+#include <linux/gpu_buddy.h>
+#include <linux/kmemleak.h>
+#include <linux/module.h>
+#include <linux/sizes.h>
+
+static struct kmem_cache *slab_blocks;
+
+static struct gpu_buddy_block *gpu_block_alloc(struct gpu_buddy *mm,
+                                              struct gpu_buddy_block *parent,
+                                              unsigned int order,
+                                              u64 offset)
+{
+       struct gpu_buddy_block *block;
+
+       BUG_ON(order > GPU_BUDDY_MAX_ORDER);
+
+       block = kmem_cache_zalloc(slab_blocks, GFP_KERNEL);
+       if (!block)
+               return NULL;
+
+       block->header = offset;
+       block->header |= order;
+       block->parent = parent;
+
+       RB_CLEAR_NODE(&block->rb);
+
+       BUG_ON(block->header & GPU_BUDDY_HEADER_UNUSED);
+       return block;
+}
+
+static void gpu_block_free(struct gpu_buddy *mm,
+                          struct gpu_buddy_block *block)
+{
+       kmem_cache_free(slab_blocks, block);
+}
+
+static enum gpu_buddy_free_tree
+get_block_tree(struct gpu_buddy_block *block)
+{
+       return gpu_buddy_block_is_clear(block) ?
+              GPU_BUDDY_CLEAR_TREE : GPU_BUDDY_DIRTY_TREE;
+}
+
+static struct gpu_buddy_block *
+rbtree_get_free_block(const struct rb_node *node)
+{
+       return node ? rb_entry(node, struct gpu_buddy_block, rb) : NULL;
+}
+
+static struct gpu_buddy_block *
+rbtree_last_free_block(struct rb_root *root)
+{
+       return rbtree_get_free_block(rb_last(root));
+}
+
+static bool rbtree_is_empty(struct rb_root *root)
+{
+       return RB_EMPTY_ROOT(root);
+}
+
+static bool gpu_buddy_block_offset_less(const struct gpu_buddy_block *block,
+                                       const struct gpu_buddy_block *node)
+{
+       return gpu_buddy_block_offset(block) < gpu_buddy_block_offset(node);
+}
+
+static bool rbtree_block_offset_less(struct rb_node *block,
+                                    const struct rb_node *node)
+{
+       return gpu_buddy_block_offset_less(rbtree_get_free_block(block),
+                                          rbtree_get_free_block(node));
+}
+
+static void rbtree_insert(struct gpu_buddy *mm,
+                         struct gpu_buddy_block *block,
+                         enum gpu_buddy_free_tree tree)
+{
+       rb_add(&block->rb,
+              &mm->free_trees[tree][gpu_buddy_block_order(block)],
+              rbtree_block_offset_less);
+}
+
+static void rbtree_remove(struct gpu_buddy *mm,
+                         struct gpu_buddy_block *block)
+{
+       unsigned int order = gpu_buddy_block_order(block);
+       enum gpu_buddy_free_tree tree;
+       struct rb_root *root;
+
+       tree = get_block_tree(block);
+       root = &mm->free_trees[tree][order];
+
+       rb_erase(&block->rb, root);
+       RB_CLEAR_NODE(&block->rb);
+}
+
+static void clear_reset(struct gpu_buddy_block *block)
+{
+       block->header &= ~GPU_BUDDY_HEADER_CLEAR;
+}
+
+static void mark_cleared(struct gpu_buddy_block *block)
+{
+       block->header |= GPU_BUDDY_HEADER_CLEAR;
+}
+
+static void mark_allocated(struct gpu_buddy *mm,
+                          struct gpu_buddy_block *block)
+{
+       block->header &= ~GPU_BUDDY_HEADER_STATE;
+       block->header |= GPU_BUDDY_ALLOCATED;
+
+       rbtree_remove(mm, block);
+}
+
+static void mark_free(struct gpu_buddy *mm,
+                     struct gpu_buddy_block *block)
+{
+       enum gpu_buddy_free_tree tree;
+
+       block->header &= ~GPU_BUDDY_HEADER_STATE;
+       block->header |= GPU_BUDDY_FREE;
+
+       tree = get_block_tree(block);
+       rbtree_insert(mm, block, tree);
+}
+
+static void mark_split(struct gpu_buddy *mm,
+                      struct gpu_buddy_block *block)
+{
+       block->header &= ~GPU_BUDDY_HEADER_STATE;
+       block->header |= GPU_BUDDY_SPLIT;
+
+       rbtree_remove(mm, block);
+}
+
+static inline bool overlaps(u64 s1, u64 e1, u64 s2, u64 e2)
+{
+       return s1 <= e2 && e1 >= s2;
+}
+
+static inline bool contains(u64 s1, u64 e1, u64 s2, u64 e2)
+{
+       return s1 <= s2 && e1 >= e2;
+}
+
+static struct gpu_buddy_block *
+__get_buddy(struct gpu_buddy_block *block)
+{
+       struct gpu_buddy_block *parent;
+
+       parent = block->parent;
+       if (!parent)
+               return NULL;
+
+       if (parent->left == block)
+               return parent->right;
+
+       return parent->left;
+}
+
+static unsigned int __gpu_buddy_free(struct gpu_buddy *mm,
+                                    struct gpu_buddy_block *block,
+                                    bool force_merge)
+{
+       struct gpu_buddy_block *parent;
+       unsigned int order;
+
+       while ((parent = block->parent)) {
+               struct gpu_buddy_block *buddy;
+
+               buddy = __get_buddy(block);
+
+               if (!gpu_buddy_block_is_free(buddy))
+                       break;
+
+               if (!force_merge) {
+                       /*
+                        * Check the block and its buddy clear state and exit
+                        * the loop if they both have the dissimilar state.
+                        */
+                       if (gpu_buddy_block_is_clear(block) !=
+                           gpu_buddy_block_is_clear(buddy))
+                               break;
+
+                       if (gpu_buddy_block_is_clear(block))
+                               mark_cleared(parent);
+               }
+
+               rbtree_remove(mm, buddy);
+               if (force_merge && gpu_buddy_block_is_clear(buddy))
+                       mm->clear_avail -= gpu_buddy_block_size(mm, buddy);
+
+               gpu_block_free(mm, block);
+               gpu_block_free(mm, buddy);
+
+               block = parent;
+       }
+
+       order = gpu_buddy_block_order(block);
+       mark_free(mm, block);
+
+       return order;
+}
+
+static int __force_merge(struct gpu_buddy *mm,
+                        u64 start,
+                        u64 end,
+                        unsigned int min_order)
+{
+       unsigned int tree, order;
+       int i;
+
+       if (!min_order)
+               return -ENOMEM;
+
+       if (min_order > mm->max_order)
+               return -EINVAL;
+
+       for_each_free_tree(tree) {
+               for (i = min_order - 1; i >= 0; i--) {
+                       struct rb_node *iter = rb_last(&mm->free_trees[tree][i]);
+
+                       while (iter) {
+                               struct gpu_buddy_block *block, *buddy;
+                               u64 block_start, block_end;
+
+                               block = rbtree_get_free_block(iter);
+                               iter = rb_prev(iter);
+
+                               if (!block || !block->parent)
+                                       continue;
+
+                               block_start = gpu_buddy_block_offset(block);
+                               block_end = block_start + gpu_buddy_block_size(mm, block) - 1;
+
+                               if (!contains(start, end, block_start, block_end))
+                                       continue;
+
+                               buddy = __get_buddy(block);
+                               if (!gpu_buddy_block_is_free(buddy))
+                                       continue;
+
+                               WARN_ON(gpu_buddy_block_is_clear(block) ==
+                                       gpu_buddy_block_is_clear(buddy));
+
+                               /*
+                                * Advance to the next node when the current node is the buddy,
+                                * as freeing the block will also remove its buddy from the tree.
+                                */
+                               if (iter == &buddy->rb)
+                                       iter = rb_prev(iter);
+
+                               rbtree_remove(mm, block);
+                               if (gpu_buddy_block_is_clear(block))
+                                       mm->clear_avail -= gpu_buddy_block_size(mm, block);
+
+                               order = __gpu_buddy_free(mm, block, true);
+                               if (order >= min_order)
+                                       return 0;
+                       }
+               }
+       }
+
+       return -ENOMEM;
+}
+
+/**
+ * gpu_buddy_init - init memory manager
+ *
+ * @mm: GPU buddy manager to initialize
+ * @size: size in bytes to manage
+ * @chunk_size: minimum page size in bytes for our allocations
+ *
+ * Initializes the memory manager and its resources.
+ *
+ * Returns:
+ * 0 on success, error code on failure.
+ */
+int gpu_buddy_init(struct gpu_buddy *mm, u64 size, u64 chunk_size)
+{
+       unsigned int i, j, root_count = 0;
+       u64 offset = 0;
+
+       if (size < chunk_size)
+               return -EINVAL;
+
+       if (chunk_size < SZ_4K)
+               return -EINVAL;
+
+       if (!is_power_of_2(chunk_size))
+               return -EINVAL;
+
+       size = round_down(size, chunk_size);
+
+       mm->size = size;
+       mm->avail = size;
+       mm->clear_avail = 0;
+       mm->chunk_size = chunk_size;
+       mm->max_order = ilog2(size) - ilog2(chunk_size);
+
+       BUG_ON(mm->max_order > GPU_BUDDY_MAX_ORDER);
+
+       mm->free_trees = kmalloc_array(GPU_BUDDY_MAX_FREE_TREES,
+                                      sizeof(*mm->free_trees),
+                                      GFP_KERNEL);
+       if (!mm->free_trees)
+               return -ENOMEM;
+
+       for_each_free_tree(i) {
+               mm->free_trees[i] = kmalloc_array(mm->max_order + 1,
+                                                 sizeof(struct rb_root),
+                                                 GFP_KERNEL);
+               if (!mm->free_trees[i])
+                       goto out_free_tree;
+
+               for (j = 0; j <= mm->max_order; ++j)
+                       mm->free_trees[i][j] = RB_ROOT;
+       }
+
+       mm->n_roots = hweight64(size);
+
+       mm->roots = kmalloc_array(mm->n_roots,
+                                 sizeof(struct gpu_buddy_block *),
+                                 GFP_KERNEL);
+       if (!mm->roots)
+               goto out_free_tree;
+
+       /*
+        * Split into power-of-two blocks, in case we are given a size that is
+        * not itself a power-of-two.
+        */
+       do {
+               struct gpu_buddy_block *root;
+               unsigned int order;
+               u64 root_size;
+
+               order = ilog2(size) - ilog2(chunk_size);
+               root_size = chunk_size << order;
+
+               root = gpu_block_alloc(mm, NULL, order, offset);
+               if (!root)
+                       goto out_free_roots;
+
+               mark_free(mm, root);
+
+               BUG_ON(root_count > mm->max_order);
+               BUG_ON(gpu_buddy_block_size(mm, root) < chunk_size);
+
+               mm->roots[root_count] = root;
+
+               offset += root_size;
+               size -= root_size;
+               root_count++;
+       } while (size);
+
+       return 0;
+
+out_free_roots:
+       while (root_count--)
+               gpu_block_free(mm, mm->roots[root_count]);
+       kfree(mm->roots);
+out_free_tree:
+       while (i--)
+               kfree(mm->free_trees[i]);
+       kfree(mm->free_trees);
+       return -ENOMEM;
+}
+EXPORT_SYMBOL(gpu_buddy_init);
+
+/**
+ * gpu_buddy_fini - tear down the memory manager
+ *
+ * @mm: GPU buddy manager to free
+ *
+ * Cleanup memory manager resources and the freetree
+ */
+void gpu_buddy_fini(struct gpu_buddy *mm)
+{
+       u64 root_size, size, start;
+       unsigned int order;
+       int i;
+
+       size = mm->size;
+
+       for (i = 0; i < mm->n_roots; ++i) {
+               order = ilog2(size) - ilog2(mm->chunk_size);
+               start = gpu_buddy_block_offset(mm->roots[i]);
+               __force_merge(mm, start, start + size, order);
+
+               if (WARN_ON(!gpu_buddy_block_is_free(mm->roots[i])))
+                       kunit_fail_current_test("buddy_fini() root");
+
+               gpu_block_free(mm, mm->roots[i]);
+
+               root_size = mm->chunk_size << order;
+               size -= root_size;
+       }
+
+       WARN_ON(mm->avail != mm->size);
+
+       for_each_free_tree(i)
+               kfree(mm->free_trees[i]);
+       kfree(mm->roots);
+}
+EXPORT_SYMBOL(gpu_buddy_fini);
+
+static int split_block(struct gpu_buddy *mm,
+                      struct gpu_buddy_block *block)
+{
+       unsigned int block_order = gpu_buddy_block_order(block) - 1;
+       u64 offset = gpu_buddy_block_offset(block);
+
+       BUG_ON(!gpu_buddy_block_is_free(block));
+       BUG_ON(!gpu_buddy_block_order(block));
+
+       block->left = gpu_block_alloc(mm, block, block_order, offset);
+       if (!block->left)
+               return -ENOMEM;
+
+       block->right = gpu_block_alloc(mm, block, block_order,
+                                      offset + (mm->chunk_size << block_order));
+       if (!block->right) {
+               gpu_block_free(mm, block->left);
+               return -ENOMEM;
+       }
+
+       mark_split(mm, block);
+
+       if (gpu_buddy_block_is_clear(block)) {
+               mark_cleared(block->left);
+               mark_cleared(block->right);
+               clear_reset(block);
+       }
+
+       mark_free(mm, block->left);
+       mark_free(mm, block->right);
+
+       return 0;
+}
+
+/**
+ * gpu_get_buddy - get buddy address
+ *
+ * @block: GPU buddy block
+ *
+ * Returns the corresponding buddy block for @block, or NULL
+ * if this is a root block and can't be merged further.
+ * Requires some kind of locking to protect against
+ * any concurrent allocate and free operations.
+ */
+struct gpu_buddy_block *
+gpu_get_buddy(struct gpu_buddy_block *block)
+{
+       return __get_buddy(block);
+}
+EXPORT_SYMBOL(gpu_get_buddy);
+
+/**
+ * gpu_buddy_reset_clear - reset blocks clear state
+ *
+ * @mm: GPU buddy manager
+ * @is_clear: blocks clear state
+ *
+ * Reset the clear state based on @is_clear value for each block
+ * in the freetree.
+ */
+void gpu_buddy_reset_clear(struct gpu_buddy *mm, bool is_clear)
+{
+       enum gpu_buddy_free_tree src_tree, dst_tree;
+       u64 root_size, size, start;
+       unsigned int order;
+       int i;
+
+       size = mm->size;
+       for (i = 0; i < mm->n_roots; ++i) {
+               order = ilog2(size) - ilog2(mm->chunk_size);
+               start = gpu_buddy_block_offset(mm->roots[i]);
+               __force_merge(mm, start, start + size, order);
+
+               root_size = mm->chunk_size << order;
+               size -= root_size;
+       }
+
+       src_tree = is_clear ? GPU_BUDDY_DIRTY_TREE : GPU_BUDDY_CLEAR_TREE;
+       dst_tree = is_clear ? GPU_BUDDY_CLEAR_TREE : GPU_BUDDY_DIRTY_TREE;
+
+       for (i = 0; i <= mm->max_order; ++i) {
+               struct rb_root *root = &mm->free_trees[src_tree][i];
+               struct gpu_buddy_block *block, *tmp;
+
+               rbtree_postorder_for_each_entry_safe(block, tmp, root, rb) {
+                       rbtree_remove(mm, block);
+                       if (is_clear) {
+                               mark_cleared(block);
+                               mm->clear_avail += gpu_buddy_block_size(mm, block);
+                       } else {
+                               clear_reset(block);
+                               mm->clear_avail -= gpu_buddy_block_size(mm, block);
+                       }
+
+                       rbtree_insert(mm, block, dst_tree);
+               }
+       }
+}
+EXPORT_SYMBOL(gpu_buddy_reset_clear);
+
+/**
+ * gpu_buddy_free_block - free a block
+ *
+ * @mm: GPU buddy manager
+ * @block: block to be freed
+ */
+void gpu_buddy_free_block(struct gpu_buddy *mm,
+                         struct gpu_buddy_block *block)
+{
+       BUG_ON(!gpu_buddy_block_is_allocated(block));
+       mm->avail += gpu_buddy_block_size(mm, block);
+       if (gpu_buddy_block_is_clear(block))
+               mm->clear_avail += gpu_buddy_block_size(mm, block);
+
+       __gpu_buddy_free(mm, block, false);
+}
+EXPORT_SYMBOL(gpu_buddy_free_block);
+
+static void __gpu_buddy_free_list(struct gpu_buddy *mm,
+                                 struct list_head *objects,
+                                 bool mark_clear,
+                                 bool mark_dirty)
+{
+       struct gpu_buddy_block *block, *on;
+
+       WARN_ON(mark_dirty && mark_clear);
+
+       list_for_each_entry_safe(block, on, objects, link) {
+               if (mark_clear)
+                       mark_cleared(block);
+               else if (mark_dirty)
+                       clear_reset(block);
+               gpu_buddy_free_block(mm, block);
+               cond_resched();
+       }
+       INIT_LIST_HEAD(objects);
+}
+
+static void gpu_buddy_free_list_internal(struct gpu_buddy *mm,
+                                        struct list_head *objects)
+{
+       /*
+        * Don't touch the clear/dirty bit, since allocation is still internal
+        * at this point. For example we might have just failed part of the
+        * allocation.
+        */
+       __gpu_buddy_free_list(mm, objects, false, false);
+}
+
+/**
+ * gpu_buddy_free_list - free blocks
+ *
+ * @mm: GPU buddy manager
+ * @objects: input list head to free blocks
+ * @flags: optional flags like GPU_BUDDY_CLEARED
+ */
+void gpu_buddy_free_list(struct gpu_buddy *mm,
+                        struct list_head *objects,
+                        unsigned int flags)
+{
+       bool mark_clear = flags & GPU_BUDDY_CLEARED;
+
+       __gpu_buddy_free_list(mm, objects, mark_clear, !mark_clear);
+}
+EXPORT_SYMBOL(gpu_buddy_free_list);
+
+static bool block_incompatible(struct gpu_buddy_block *block, unsigned int flags)
+{
+       bool needs_clear = flags & GPU_BUDDY_CLEAR_ALLOCATION;
+
+       return needs_clear != gpu_buddy_block_is_clear(block);
+}
+
+static struct gpu_buddy_block *
+__alloc_range_bias(struct gpu_buddy *mm,
+                  u64 start, u64 end,
+                  unsigned int order,
+                  unsigned long flags,
+                  bool fallback)
+{
+       u64 req_size = mm->chunk_size << order;
+       struct gpu_buddy_block *block;
+       struct gpu_buddy_block *buddy;
+       LIST_HEAD(dfs);
+       int err;
+       int i;
+
+       end = end - 1;
+
+       for (i = 0; i < mm->n_roots; ++i)
+               list_add_tail(&mm->roots[i]->tmp_link, &dfs);
+
+       do {
+               u64 block_start;
+               u64 block_end;
+
+               block = list_first_entry_or_null(&dfs,
+                                                struct gpu_buddy_block,
+                                                tmp_link);
+               if (!block)
+                       break;
+
+               list_del(&block->tmp_link);
+
+               if (gpu_buddy_block_order(block) < order)
+                       continue;
+
+               block_start = gpu_buddy_block_offset(block);
+               block_end = block_start + gpu_buddy_block_size(mm, block) - 1;
+
+               if (!overlaps(start, end, block_start, block_end))
+                       continue;
+
+               if (gpu_buddy_block_is_allocated(block))
+                       continue;
+
+               if (block_start < start || block_end > end) {
+                       u64 adjusted_start = max(block_start, start);
+                       u64 adjusted_end = min(block_end, end);
+
+                       if (round_down(adjusted_end + 1, req_size) <=
+                           round_up(adjusted_start, req_size))
+                               continue;
+               }
+
+               if (!fallback && block_incompatible(block, flags))
+                       continue;
+
+               if (contains(start, end, block_start, block_end) &&
+                   order == gpu_buddy_block_order(block)) {
+                       /*
+                        * Find the free block within the range.
+                        */
+                       if (gpu_buddy_block_is_free(block))
+                               return block;
+
+                       continue;
+               }
+
+               if (!gpu_buddy_block_is_split(block)) {
+                       err = split_block(mm, block);
+                       if (unlikely(err))
+                               goto err_undo;
+               }
+
+               list_add(&block->right->tmp_link, &dfs);
+               list_add(&block->left->tmp_link, &dfs);
+       } while (1);
+
+       return ERR_PTR(-ENOSPC);
+
+err_undo:
+       /*
+        * We really don't want to leave around a bunch of split blocks, since
+        * bigger is better, so make sure we merge everything back before we
+        * free the allocated blocks.
+        */
+       buddy = __get_buddy(block);
+       if (buddy &&
+           (gpu_buddy_block_is_free(block) &&
+            gpu_buddy_block_is_free(buddy)))
+               __gpu_buddy_free(mm, block, false);
+       return ERR_PTR(err);
+}
+
+static struct gpu_buddy_block *
+__gpu_buddy_alloc_range_bias(struct gpu_buddy *mm,
+                            u64 start, u64 end,
+                            unsigned int order,
+                            unsigned long flags)
+{
+       struct gpu_buddy_block *block;
+       bool fallback = false;
+
+       block = __alloc_range_bias(mm, start, end, order,
+                                  flags, fallback);
+       if (IS_ERR(block))
+               return __alloc_range_bias(mm, start, end, order,
+                                         flags, !fallback);
+
+       return block;
+}
+
+static struct gpu_buddy_block *
+get_maxblock(struct gpu_buddy *mm,
+            unsigned int order,
+            enum gpu_buddy_free_tree tree)
+{
+       struct gpu_buddy_block *max_block = NULL, *block = NULL;
+       struct rb_root *root;
+       unsigned int i;
+
+       for (i = order; i <= mm->max_order; ++i) {
+               root = &mm->free_trees[tree][i];
+               block = rbtree_last_free_block(root);
+               if (!block)
+                       continue;
+
+               if (!max_block) {
+                       max_block = block;
+                       continue;
+               }
+
+               if (gpu_buddy_block_offset(block) >
+                   gpu_buddy_block_offset(max_block)) {
+                       max_block = block;
+               }
+       }
+
+       return max_block;
+}
+
+static struct gpu_buddy_block *
+alloc_from_freetree(struct gpu_buddy *mm,
+                   unsigned int order,
+                   unsigned long flags)
+{
+       struct gpu_buddy_block *block = NULL;
+       struct rb_root *root;
+       enum gpu_buddy_free_tree tree;
+       unsigned int tmp;
+       int err;
+
+       tree = (flags & GPU_BUDDY_CLEAR_ALLOCATION) ?
+               GPU_BUDDY_CLEAR_TREE : GPU_BUDDY_DIRTY_TREE;
+
+       if (flags & GPU_BUDDY_TOPDOWN_ALLOCATION) {
+               block = get_maxblock(mm, order, tree);
+               if (block)
+                       /* Store the obtained block order */
+                       tmp = gpu_buddy_block_order(block);
+       } else {
+               for (tmp = order; tmp <= mm->max_order; ++tmp) {
+                       /* Get RB tree root for this order and tree */
+                       root = &mm->free_trees[tree][tmp];
+                       block = rbtree_last_free_block(root);
+                       if (block)
+                               break;
+               }
+       }
+
+       if (!block) {
+               /* Try allocating from the other tree */
+               tree = (tree == GPU_BUDDY_CLEAR_TREE) ?
+                       GPU_BUDDY_DIRTY_TREE : GPU_BUDDY_CLEAR_TREE;
+
+               for (tmp = order; tmp <= mm->max_order; ++tmp) {
+                       root = &mm->free_trees[tree][tmp];
+                       block = rbtree_last_free_block(root);
+                       if (block)
+                               break;
+               }
+
+               if (!block)
+                       return ERR_PTR(-ENOSPC);
+       }
+
+       BUG_ON(!gpu_buddy_block_is_free(block));
+
+       while (tmp != order) {
+               err = split_block(mm, block);
+               if (unlikely(err))
+                       goto err_undo;
+
+               block = block->right;
+               tmp--;
+       }
+       return block;
+
+err_undo:
+       if (tmp != order)
+               __gpu_buddy_free(mm, block, false);
+       return ERR_PTR(err);
+}
+
+static int __alloc_range(struct gpu_buddy *mm,
+                        struct list_head *dfs,
+                        u64 start, u64 size,
+                        struct list_head *blocks,
+                        u64 *total_allocated_on_err)
+{
+       struct gpu_buddy_block *block;
+       struct gpu_buddy_block *buddy;
+       u64 total_allocated = 0;
+       LIST_HEAD(allocated);
+       u64 end;
+       int err;
+
+       end = start + size - 1;
+
+       do {
+               u64 block_start;
+               u64 block_end;
+
+               block = list_first_entry_or_null(dfs,
+                                                struct gpu_buddy_block,
+                                                tmp_link);
+               if (!block)
+                       break;
+
+               list_del(&block->tmp_link);
+
+               block_start = gpu_buddy_block_offset(block);
+               block_end = block_start + gpu_buddy_block_size(mm, block) - 1;
+
+               if (!overlaps(start, end, block_start, block_end))
+                       continue;
+
+               if (gpu_buddy_block_is_allocated(block)) {
+                       err = -ENOSPC;
+                       goto err_free;
+               }
+
+               if (contains(start, end, block_start, block_end)) {
+                       if (gpu_buddy_block_is_free(block)) {
+                               mark_allocated(mm, block);
+                               total_allocated += gpu_buddy_block_size(mm, block);
+                               mm->avail -= gpu_buddy_block_size(mm, block);
+                               if (gpu_buddy_block_is_clear(block))
+                                       mm->clear_avail -= gpu_buddy_block_size(mm, block);
+                               list_add_tail(&block->link, &allocated);
+                               continue;
+                       } else if (!mm->clear_avail) {
+                               err = -ENOSPC;
+                               goto err_free;
+                       }
+               }
+
+               if (!gpu_buddy_block_is_split(block)) {
+                       err = split_block(mm, block);
+                       if (unlikely(err))
+                               goto err_undo;
+               }
+
+               list_add(&block->right->tmp_link, dfs);
+               list_add(&block->left->tmp_link, dfs);
+       } while (1);
+
+       if (total_allocated < size) {
+               err = -ENOSPC;
+               goto err_free;
+       }
+
+       list_splice_tail(&allocated, blocks);
+
+       return 0;
+
+err_undo:
+       /*
+        * We really don't want to leave around a bunch of split blocks, since
+        * bigger is better, so make sure we merge everything back before we
+        * free the allocated blocks.
+        */
+       buddy = __get_buddy(block);
+       if (buddy &&
+           (gpu_buddy_block_is_free(block) &&
+            gpu_buddy_block_is_free(buddy)))
+               __gpu_buddy_free(mm, block, false);
+
+err_free:
+       if (err == -ENOSPC && total_allocated_on_err) {
+               list_splice_tail(&allocated, blocks);
+               *total_allocated_on_err = total_allocated;
+       } else {
+               gpu_buddy_free_list_internal(mm, &allocated);
+       }
+
+       return err;
+}
+
+static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
+                                  u64 start,
+                                  u64 size,
+                                  u64 *total_allocated_on_err,
+                                  struct list_head *blocks)
+{
+       LIST_HEAD(dfs);
+       int i;
+
+       for (i = 0; i < mm->n_roots; ++i)
+               list_add_tail(&mm->roots[i]->tmp_link, &dfs);
+
+       return __alloc_range(mm, &dfs, start, size,
+                            blocks, total_allocated_on_err);
+}
+
+static int __alloc_contig_try_harder(struct gpu_buddy *mm,
+                                    u64 size,
+                                    u64 min_block_size,
+                                    struct list_head *blocks)
+{
+       u64 rhs_offset, lhs_offset, lhs_size, filled;
+       struct gpu_buddy_block *block;
+       unsigned int tree, order;
+       LIST_HEAD(blocks_lhs);
+       unsigned long pages;
+       u64 modify_size;
+       int err;
+
+       modify_size = rounddown_pow_of_two(size);
+       pages = modify_size >> ilog2(mm->chunk_size);
+       order = fls(pages) - 1;
+       if (order == 0)
+               return -ENOSPC;
+
+       for_each_free_tree(tree) {
+               struct rb_root *root;
+               struct rb_node *iter;
+
+               root = &mm->free_trees[tree][order];
+               if (rbtree_is_empty(root))
+                       continue;
+
+               iter = rb_last(root);
+               while (iter) {
+                       block = rbtree_get_free_block(iter);
+
+                       /* Allocate blocks traversing RHS */
+                       rhs_offset = gpu_buddy_block_offset(block);
+                       err =  __gpu_buddy_alloc_range(mm, rhs_offset, size,
+                                                      &filled, blocks);
+                       if (!err || err != -ENOSPC)
+                               return err;
+
+                       lhs_size = max((size - filled), min_block_size);
+                       if (!IS_ALIGNED(lhs_size, min_block_size))
+                               lhs_size = round_up(lhs_size, min_block_size);
+
+                       /* Allocate blocks traversing LHS */
+                       lhs_offset = gpu_buddy_block_offset(block) - lhs_size;
+                       err =  __gpu_buddy_alloc_range(mm, lhs_offset, lhs_size,
+                                                      NULL, &blocks_lhs);
+                       if (!err) {
+                               list_splice(&blocks_lhs, blocks);
+                               return 0;
+                       } else if (err != -ENOSPC) {
+                               gpu_buddy_free_list_internal(mm, blocks);
+                               return err;
+                       }
+                       /* Free blocks for the next iteration */
+                       gpu_buddy_free_list_internal(mm, blocks);
+
+                       iter = rb_prev(iter);
+               }
+       }
+
+       return -ENOSPC;
+}
+
+/**
+ * gpu_buddy_block_trim - free unused pages
+ *
+ * @mm: GPU buddy manager
+ * @start: start address to begin the trimming.
+ * @new_size: original size requested
+ * @blocks: Input and output list of allocated blocks.
+ * MUST contain single block as input to be trimmed.
+ * On success will contain the newly allocated blocks
+ * making up the @new_size. Blocks always appear in
+ * ascending order
+ *
+ * For contiguous allocation, we round up the size to the nearest
+ * power of two value, drivers consume *actual* size, so remaining
+ * portions are unused and can be optionally freed with this function
+ *
+ * Returns:
+ * 0 on success, error code on failure.
+ */
+int gpu_buddy_block_trim(struct gpu_buddy *mm,
+                        u64 *start,
+                        u64 new_size,
+                        struct list_head *blocks)
+{
+       struct gpu_buddy_block *parent;
+       struct gpu_buddy_block *block;
+       u64 block_start, block_end;
+       LIST_HEAD(dfs);
+       u64 new_start;
+       int err;
+
+       if (!list_is_singular(blocks))
+               return -EINVAL;
+
+       block = list_first_entry(blocks,
+                                struct gpu_buddy_block,
+                                link);
+
+       block_start = gpu_buddy_block_offset(block);
+       block_end = block_start + gpu_buddy_block_size(mm, block);
+
+       if (WARN_ON(!gpu_buddy_block_is_allocated(block)))
+               return -EINVAL;
+
+       if (new_size > gpu_buddy_block_size(mm, block))
+               return -EINVAL;
+
+       if (!new_size || !IS_ALIGNED(new_size, mm->chunk_size))
+               return -EINVAL;
+
+       if (new_size == gpu_buddy_block_size(mm, block))
+               return 0;
+
+       new_start = block_start;
+       if (start) {
+               new_start = *start;
+
+               if (new_start < block_start)
+                       return -EINVAL;
+
+               if (!IS_ALIGNED(new_start, mm->chunk_size))
+                       return -EINVAL;
+
+               if (range_overflows(new_start, new_size, block_end))
+                       return -EINVAL;
+       }
+
+       list_del(&block->link);
+       mark_free(mm, block);
+       mm->avail += gpu_buddy_block_size(mm, block);
+       if (gpu_buddy_block_is_clear(block))
+               mm->clear_avail += gpu_buddy_block_size(mm, block);
+
+       /* Prevent recursively freeing this node */
+       parent = block->parent;
+       block->parent = NULL;
+
+       list_add(&block->tmp_link, &dfs);
+       err =  __alloc_range(mm, &dfs, new_start, new_size, blocks, NULL);
+       if (err) {
+               mark_allocated(mm, block);
+               mm->avail -= gpu_buddy_block_size(mm, block);
+               if (gpu_buddy_block_is_clear(block))
+                       mm->clear_avail -= gpu_buddy_block_size(mm, block);
+               list_add(&block->link, blocks);
+       }
+
+       block->parent = parent;
+       return err;
+}
+EXPORT_SYMBOL(gpu_buddy_block_trim);
+
+static struct gpu_buddy_block *
+__gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
+                        u64 start, u64 end,
+                        unsigned int order,
+                        unsigned long flags)
+{
+       if (flags & GPU_BUDDY_RANGE_ALLOCATION)
+               /* Allocate traversing within the range */
+               return  __gpu_buddy_alloc_range_bias(mm, start, end,
+                                                    order, flags);
+       else
+               /* Allocate from freetree */
+               return alloc_from_freetree(mm, order, flags);
+}
+
+/**
+ * gpu_buddy_alloc_blocks - allocate power-of-two blocks
+ *
+ * @mm: GPU buddy manager to allocate from
+ * @start: start of the allowed range for this block
+ * @end: end of the allowed range for this block
+ * @size: size of the allocation in bytes
+ * @min_block_size: alignment of the allocation
+ * @blocks: output list head to add allocated blocks
+ * @flags: GPU_BUDDY_*_ALLOCATION flags
+ *
+ * alloc_range_bias() called on range limitations, which traverses
+ * the tree and returns the desired block.
+ *
+ * alloc_from_freetree() called when *no* range restrictions
+ * are enforced, which picks the block from the freetree.
+ *
+ * Returns:
+ * 0 on success, error code on failure.
+ */
+int gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
+                          u64 start, u64 end, u64 size,
+                          u64 min_block_size,
+                          struct list_head *blocks,
+                          unsigned long flags)
+{
+       struct gpu_buddy_block *block = NULL;
+       u64 original_size, original_min_size;
+       unsigned int min_order, order;
+       LIST_HEAD(allocated);
+       unsigned long pages;
+       int err;
+
+       if (size < mm->chunk_size)
+               return -EINVAL;
+
+       if (min_block_size < mm->chunk_size)
+               return -EINVAL;
+
+       if (!is_power_of_2(min_block_size))
+               return -EINVAL;
+
+       if (!IS_ALIGNED(start | end | size, mm->chunk_size))
+               return -EINVAL;
+
+       if (end > mm->size)
+               return -EINVAL;
+
+       if (range_overflows(start, size, mm->size))
+               return -EINVAL;
+
+       /* Actual range allocation */
+       if (start + size == end) {
+               if (!IS_ALIGNED(start | end, min_block_size))
+                       return -EINVAL;
+
+               return __gpu_buddy_alloc_range(mm, start, size, NULL, blocks);
+       }
+
+       original_size = size;
+       original_min_size = min_block_size;
+
+       /* Roundup the size to power of 2 */
+       if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) {
+               size = roundup_pow_of_two(size);
+               min_block_size = size;
+       /* Align size value to min_block_size */
+       } else if (!IS_ALIGNED(size, min_block_size)) {
+               size = round_up(size, min_block_size);
+       }
+
+       pages = size >> ilog2(mm->chunk_size);
+       order = fls(pages) - 1;
+       min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
+
+       do {
+               order = min(order, (unsigned int)fls(pages) - 1);
+               BUG_ON(order > mm->max_order);
+               BUG_ON(order < min_order);
+
+               do {
+                       block = __gpu_buddy_alloc_blocks(mm, start,
+                                                        end,
+                                                        order,
+                                                        flags);
+                       if (!IS_ERR(block))
+                               break;
+
+                       if (order-- == min_order) {
+                               /* Try allocation through force merge method */
+                               if (mm->clear_avail &&
+                                   !__force_merge(mm, start, end, min_order)) {
+                                       block = __gpu_buddy_alloc_blocks(mm, start,
+                                                                        end,
+                                                                        min_order,
+                                                                        flags);
+                                       if (!IS_ERR(block)) {
+                                               order = min_order;
+                                               break;
+                                       }
+                               }
+
+                               /*
+                                * Try contiguous block allocation through
+                                * try harder method.
+                                */
+                               if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION &&
+                                   !(flags & GPU_BUDDY_RANGE_ALLOCATION))
+                                       return __alloc_contig_try_harder(mm,
+                                                                        original_size,
+                                                                        original_min_size,
+                                                                        blocks);
+                               err = -ENOSPC;
+                               goto err_free;
+                       }
+               } while (1);
+
+               mark_allocated(mm, block);
+               mm->avail -= gpu_buddy_block_size(mm, block);
+               if (gpu_buddy_block_is_clear(block))
+                       mm->clear_avail -= gpu_buddy_block_size(mm, block);
+               kmemleak_update_trace(block);
+               list_add_tail(&block->link, &allocated);
+
+               pages -= BIT(order);
+
+               if (!pages)
+                       break;
+       } while (1);
+
+       /* Trim the allocated block to the required size */
+       if (!(flags & GPU_BUDDY_TRIM_DISABLE) &&
+           original_size != size) {
+               struct list_head *trim_list;
+               LIST_HEAD(temp);
+               u64 trim_size;
+
+               trim_list = &allocated;
+               trim_size = original_size;
+
+               if (!list_is_singular(&allocated)) {
+                       block = list_last_entry(&allocated, typeof(*block), link);
+                       list_move(&block->link, &temp);
+                       trim_list = &temp;
+                       trim_size = gpu_buddy_block_size(mm, block) -
+                               (size - original_size);
+               }
+
+               gpu_buddy_block_trim(mm,
+                                    NULL,
+                                    trim_size,
+                                    trim_list);
+
+               if (!list_empty(&temp))
+                       list_splice_tail(trim_list, &allocated);
+       }
+
+       list_splice_tail(&allocated, blocks);
+       return 0;
+
+err_free:
+       gpu_buddy_free_list_internal(mm, &allocated);
+       return err;
+}
+EXPORT_SYMBOL(gpu_buddy_alloc_blocks);
+
+/**
+ * gpu_buddy_block_print - print block information
+ *
+ * @mm: GPU buddy manager
+ * @block: GPU buddy block
+ */
+void gpu_buddy_block_print(struct gpu_buddy *mm,
+                          struct gpu_buddy_block *block)
+{
+       u64 start = gpu_buddy_block_offset(block);
+       u64 size = gpu_buddy_block_size(mm, block);
+
+       pr_info("%#018llx-%#018llx: %llu\n", start, start + size, size);
+}
+EXPORT_SYMBOL(gpu_buddy_block_print);
+
+/**
+ * gpu_buddy_print - print allocator state
+ *
+ * @mm: GPU buddy manager
+ */
+void gpu_buddy_print(struct gpu_buddy *mm)
+{
+       int order;
+
+       pr_info("chunk_size: %lluKiB, total: %lluMiB, free: %lluMiB, clear_free: %lluMiB\n",
+               mm->chunk_size >> 10, mm->size >> 20, mm->avail >> 20, mm->clear_avail >> 20);
+
+       for (order = mm->max_order; order >= 0; order--) {
+               struct gpu_buddy_block *block, *tmp;
+               struct rb_root *root;
+               u64 count = 0, free;
+               unsigned int tree;
+
+               for_each_free_tree(tree) {
+                       root = &mm->free_trees[tree][order];
+
+                       rbtree_postorder_for_each_entry_safe(block, tmp, root, rb) {
+                               BUG_ON(!gpu_buddy_block_is_free(block));
+                               count++;
+                       }
+               }
+
+               free = count * (mm->chunk_size << order);
+               if (free < SZ_1M)
+                       pr_info("order-%2d free: %8llu KiB, blocks: %llu\n",
+                               order, free >> 10, count);
+               else
+                       pr_info("order-%2d free: %8llu MiB, blocks: %llu\n",
+                               order, free >> 20, count);
+       }
+}
+EXPORT_SYMBOL(gpu_buddy_print);
+
+static void gpu_buddy_module_exit(void)
+{
+       kmem_cache_destroy(slab_blocks);
+}
+
+static int __init gpu_buddy_module_init(void)
+{
+       slab_blocks = KMEM_CACHE(gpu_buddy_block, 0);
+       if (!slab_blocks)
+               return -ENOMEM;
+
+       return 0;
+}
+
+module_init(gpu_buddy_module_init);
+module_exit(gpu_buddy_module_exit);
+
+MODULE_DESCRIPTION("GPU Buddy Allocator");
+MODULE_LICENSE("Dual MIT/GPL");
drivers/gpu/drm/Kconfig +1 -0
index 7e6bc0b3a589..0475defb37f0 100644
@@ -220,6 +220,7 @@ config DRM_GPUSVM
 config DRM_BUDDY
        tristate
        depends on DRM
+       select GPU_BUDDY
        help
          A page based buddy allocator
 
drivers/gpu/drm/Kconfig.debug +3 -1
index 05dc43c0b8c5..1f4c408c7920 100644
@@ -71,6 +71,7 @@ config DRM_KUNIT_TEST
        select DRM_KUNIT_TEST_HELPERS
        select DRM_LIB_RANDOM
        select DRM_SYSFB_HELPER
+       select GPU_BUDDY
        select PRIME_NUMBERS
        default KUNIT_ALL_TESTS
        help
@@ -88,10 +89,11 @@ config DRM_TTM_KUNIT_TEST
        tristate "KUnit tests for TTM" if !KUNIT_ALL_TESTS
        default n
        depends on DRM && KUNIT && MMU && (UML || COMPILE_TEST)
-       select DRM_TTM
        select DRM_BUDDY
+       select DRM_TTM
        select DRM_EXPORT_FOR_TESTS if m
        select DRM_KUNIT_TEST_HELPERS
+       select GPU_BUDDY
        default KUNIT_ALL_TESTS
        help
          Enables unit tests for TTM, a GPU memory manager subsystem used
drivers/gpu/drm/amd/amdgpu/Kconfig +1 -0
index 7f515be5185d..bb131543e1d9 100644
@@ -23,6 +23,7 @@ config DRM_AMDGPU
        select CRC16
        select BACKLIGHT_CLASS_DEVICE
        select INTERVAL_TREE
+       select GPU_BUDDY
        select DRM_BUDDY
        select DRM_SUBALLOC_HELPER
        select DRM_EXEC
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
index 2a6cf7963dde..e0bd8a68877f 100644
@@ -5654,7 +5654,7 @@ int amdgpu_ras_add_critical_region(struct amdgpu_device *adev,
        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
        struct amdgpu_vram_mgr_resource *vres;
        struct ras_critical_region *region;
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        int ret = 0;
 
        if (!bo || !bo->tbo.resource)
drivers/gpu/drm/amd/amdgpu/amdgpu_res_cursor.h +6 -6
index be2e56ce1355..8908d9e08a30 100644
@@ -55,7 +55,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
                                    uint64_t start, uint64_t size,
                                    struct amdgpu_res_cursor *cur)
 {
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        struct list_head *head, *next;
        struct drm_mm_node *node;
 
@@ -71,7 +71,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
                head = &to_amdgpu_vram_mgr_resource(res)->blocks;
 
                block = list_first_entry_or_null(head,
-                                                struct drm_buddy_block,
+                                                struct gpu_buddy_block,
                                                 link);
                if (!block)
                        goto fallback;
@@ -81,7 +81,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 
                        next = block->link.next;
                        if (next != head)
-                               block = list_entry(next, struct drm_buddy_block, link);
+                               block = list_entry(next, struct gpu_buddy_block, link);
                }
 
                cur->start = amdgpu_vram_mgr_block_start(block) + start;
@@ -125,7 +125,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
  */
 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
 {
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        struct drm_mm_node *node;
        struct list_head *next;
 
@@ -146,7 +146,7 @@ static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
                block = cur->node;
 
                next = block->link.next;
-               block = list_entry(next, struct drm_buddy_block, link);
+               block = list_entry(next, struct gpu_buddy_block, link);
 
                cur->node = block;
                cur->start = amdgpu_vram_mgr_block_start(block);
@@ -175,7 +175,7 @@ static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
  */
 static inline bool amdgpu_res_cleared(struct amdgpu_res_cursor *cur)
 {
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
 
        switch (cur->mem_type) {
        case TTM_PL_VRAM:
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +41 -39
index 9d934c07fa6b..6c06a9c9b13f 100644
@@ -23,6 +23,8 @@
  */
 
 #include <linux/dma-mapping.h>
+
+#include <drm/drm_buddy.h>
 #include <drm/ttm/ttm_range_manager.h>
 #include <drm/drm_drv.h>
 
@@ -52,15 +54,15 @@ to_amdgpu_device(struct amdgpu_vram_mgr *mgr)
        return container_of(mgr, struct amdgpu_device, mman.vram_mgr);
 }
 
-static inline struct drm_buddy_block *
+static inline struct gpu_buddy_block *
 amdgpu_vram_mgr_first_block(struct list_head *list)
 {
-       return list_first_entry_or_null(list, struct drm_buddy_block, link);
+       return list_first_entry_or_null(list, struct gpu_buddy_block, link);
 }
 
 static inline bool amdgpu_is_vram_mgr_blocks_contiguous(struct list_head *head)
 {
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        u64 start, size;
 
        block = amdgpu_vram_mgr_first_block(head);
@@ -71,7 +73,7 @@ static inline bool amdgpu_is_vram_mgr_blocks_contiguous(struct list_head *head)
                start = amdgpu_vram_mgr_block_start(block);
                size = amdgpu_vram_mgr_block_size(block);
 
-               block = list_entry(block->link.next, struct drm_buddy_block, link);
+               block = list_entry(block->link.next, struct gpu_buddy_block, link);
                if (start + size != amdgpu_vram_mgr_block_start(block))
                        return false;
        }
@@ -81,7 +83,7 @@ static inline bool amdgpu_is_vram_mgr_blocks_contiguous(struct list_head *head)
 
 static inline u64 amdgpu_vram_mgr_blocks_size(struct list_head *head)
 {
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        u64 size = 0;
 
        list_for_each_entry(block, head, link)
@@ -254,7 +256,7 @@ const struct attribute_group amdgpu_vram_mgr_attr_group = {
  * Calculate how many bytes of the DRM BUDDY block are inside visible VRAM
  */
 static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
-                                   struct drm_buddy_block *block)
+                                   struct gpu_buddy_block *block)
 {
        u64 start = amdgpu_vram_mgr_block_start(block);
        u64 end = start + amdgpu_vram_mgr_block_size(block);
@@ -279,7 +281,7 @@ u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo)
        struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
        struct ttm_resource *res = bo->tbo.resource;
        struct amdgpu_vram_mgr_resource *vres = to_amdgpu_vram_mgr_resource(res);
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        u64 usage = 0;
 
        if (amdgpu_gmc_vram_full_visible(&adev->gmc))
@@ -299,15 +301,15 @@ static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man)
 {
        struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
        struct amdgpu_device *adev = to_amdgpu_device(mgr);
-       struct drm_buddy *mm = &mgr->mm;
+       struct gpu_buddy *mm = &mgr->mm;
        struct amdgpu_vram_reservation *rsv, *temp;
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        uint64_t vis_usage;
 
        list_for_each_entry_safe(rsv, temp, &mgr->reservations_pending, blocks) {
-               if (drm_buddy_alloc_blocks(mm, rsv->start, rsv->start + rsv->size,
+               if (gpu_buddy_alloc_blocks(mm, rsv->start, rsv->start + rsv->size,
                                           rsv->size, mm->chunk_size, &rsv->allocated,
-                                          DRM_BUDDY_RANGE_ALLOCATION))
+                                          GPU_BUDDY_RANGE_ALLOCATION))
                        continue;
 
                block = amdgpu_vram_mgr_first_block(&rsv->allocated);
@@ -403,7 +405,7 @@ int amdgpu_vram_mgr_query_address_block_info(struct amdgpu_vram_mgr *mgr,
                        uint64_t address, struct amdgpu_vram_block_info *info)
 {
        struct amdgpu_vram_mgr_resource *vres;
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
        u64 start, size;
        int ret = -ENOENT;
 
@@ -450,8 +452,8 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
        struct amdgpu_vram_mgr_resource *vres;
        u64 size, remaining_size, lpfn, fpfn;
        unsigned int adjust_dcc_size = 0;
-       struct drm_buddy *mm = &mgr->mm;
-       struct drm_buddy_block *block;
+       struct gpu_buddy *mm = &mgr->mm;
+       struct gpu_buddy_block *block;
        unsigned long pages_per_block;
        int r;
 
@@ -493,17 +495,17 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
        INIT_LIST_HEAD(&vres->blocks);
 
        if (place->flags & TTM_PL_FLAG_TOPDOWN)
-               vres->flags |= DRM_BUDDY_TOPDOWN_ALLOCATION;
+               vres->flags |= GPU_BUDDY_TOPDOWN_ALLOCATION;
 
        if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
-               vres->flags |= DRM_BUDDY_CONTIGUOUS_ALLOCATION;
+               vres->flags |= GPU_BUDDY_CONTIGUOUS_ALLOCATION;
 
        if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED)
-               vres->flags |= DRM_BUDDY_CLEAR_ALLOCATION;
+               vres->flags |= GPU_BUDDY_CLEAR_ALLOCATION;
 
        if (fpfn || lpfn != mgr->mm.size)
                /* Allocate blocks in desired range */
-               vres->flags |= DRM_BUDDY_RANGE_ALLOCATION;
+               vres->flags |= GPU_BUDDY_RANGE_ALLOCATION;
 
        if (bo->flags & AMDGPU_GEM_CREATE_GFX12_DCC &&
            adev->gmc.gmc_funcs->get_dcc_alignment)
@@ -516,7 +518,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
                dcc_size = roundup_pow_of_two(vres->base.size + adjust_dcc_size);
                remaining_size = (u64)dcc_size;
 
-               vres->flags |= DRM_BUDDY_TRIM_DISABLE;
+               vres->flags |= GPU_BUDDY_TRIM_DISABLE;
        }
 
        mutex_lock(&mgr->lock);
@@ -536,7 +538,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 
                BUG_ON(min_block_size < mm->chunk_size);
 
-               r = drm_buddy_alloc_blocks(mm, fpfn,
+               r = gpu_buddy_alloc_blocks(mm, fpfn,
                                           lpfn,
                                           size,
                                           min_block_size,
@@ -545,7 +547,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 
                if (unlikely(r == -ENOSPC) && pages_per_block == ~0ul &&
                    !(place->flags & TTM_PL_FLAG_CONTIGUOUS)) {
-                       vres->flags &= ~DRM_BUDDY_CONTIGUOUS_ALLOCATION;
+                       vres->flags &= ~GPU_BUDDY_CONTIGUOUS_ALLOCATION;
                        pages_per_block = max_t(u32, 2UL << (20UL - PAGE_SHIFT),
                                                tbo->page_alignment);
 
@@ -566,7 +568,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
        list_add_tail(&vres->vres_node, &mgr->allocated_vres_list);
 
        if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS && adjust_dcc_size) {
-               struct drm_buddy_block *dcc_block;
+               struct gpu_buddy_block *dcc_block;
                unsigned long dcc_start;
                u64 trim_start;
 
@@ -576,7 +578,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
                        roundup((unsigned long)amdgpu_vram_mgr_block_start(dcc_block),
                                adjust_dcc_size);
                trim_start = (u64)dcc_start;
-               drm_buddy_block_trim(mm, &trim_start,
+               gpu_buddy_block_trim(mm, &trim_start,
                                     (u64)vres->base.size,
                                     &vres->blocks);
        }
@@ -614,7 +616,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
        return 0;
 
 error_free_blocks:
-       drm_buddy_free_list(mm, &vres->blocks, 0);
+       gpu_buddy_free_list(mm, &vres->blocks, 0);
        mutex_unlock(&mgr->lock);
 error_fini:
        ttm_resource_fini(man, &vres->base);
@@ -637,8 +639,8 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
        struct amdgpu_vram_mgr_resource *vres = to_amdgpu_vram_mgr_resource(res);
        struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
        struct amdgpu_device *adev = to_amdgpu_device(mgr);
-       struct drm_buddy *mm = &mgr->mm;
-       struct drm_buddy_block *block;
+       struct gpu_buddy *mm = &mgr->mm;
+       struct gpu_buddy_block *block;
        uint64_t vis_usage = 0;
 
        mutex_lock(&mgr->lock);
@@ -649,7 +651,7 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
        list_for_each_entry(block, &vres->blocks, link)
                vis_usage += amdgpu_vram_mgr_vis_size(adev, block);
 
-       drm_buddy_free_list(mm, &vres->blocks, vres->flags);
+       gpu_buddy_free_list(mm, &vres->blocks, vres->flags);
        amdgpu_vram_mgr_do_reserve(man);
        mutex_unlock(&mgr->lock);
 
@@ -688,7 +690,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
        if (!*sgt)
                return -ENOMEM;
 
-       /* Determine the number of DRM_BUDDY blocks to export */
+       /* Determine the number of GPU_BUDDY blocks to export */
        amdgpu_res_first(res, offset, length, &cursor);
        while (cursor.remaining) {
                num_entries++;
@@ -704,10 +706,10 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
                sg->length = 0;
 
        /*
-        * Walk down DRM_BUDDY blocks to populate scatterlist nodes
-        * @note: Use iterator api to get first the DRM_BUDDY block
+        * Walk down GPU_BUDDY blocks to populate scatterlist nodes
+        * @note: Use iterator api to get first the GPU_BUDDY block
         * and the number of bytes from it. Access the following
-        * DRM_BUDDY block(s) if more buffer needs to exported
+        * GPU_BUDDY block(s) if more buffer needs to exported
         */
        amdgpu_res_first(res, offset, length, &cursor);
        for_each_sgtable_sg((*sgt), sg, i) {
@@ -792,10 +794,10 @@ uint64_t amdgpu_vram_mgr_vis_usage(struct amdgpu_vram_mgr *mgr)
 void amdgpu_vram_mgr_clear_reset_blocks(struct amdgpu_device *adev)
 {
        struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
-       struct drm_buddy *mm = &mgr->mm;
+       struct gpu_buddy *mm = &mgr->mm;
 
        mutex_lock(&mgr->lock);
-       drm_buddy_reset_clear(mm, false);
+       gpu_buddy_reset_clear(mm, false);
        mutex_unlock(&mgr->lock);
 }
 
@@ -815,7 +817,7 @@ static bool amdgpu_vram_mgr_intersects(struct ttm_resource_manager *man,
                                       size_t size)
 {
        struct amdgpu_vram_mgr_resource *mgr = to_amdgpu_vram_mgr_resource(res);
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
 
        /* Check each drm buddy block individually */
        list_for_each_entry(block, &mgr->blocks, link) {
@@ -848,7 +850,7 @@ static bool amdgpu_vram_mgr_compatible(struct ttm_resource_manager *man,
                                       size_t size)
 {
        struct amdgpu_vram_mgr_resource *mgr = to_amdgpu_vram_mgr_resource(res);
-       struct drm_buddy_block *block;
+       struct gpu_buddy_block *block;
 
        /* Check each drm buddy block individually */
        list_for_each_entry(block, &mgr->blocks, link) {
@@ -877,7 +879,7 @@ static void amdgpu_vram_mgr_debug(struct ttm_resource_manager *man,
                                  struct drm_printer *printer)
 {
        struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
-       struct drm_buddy *mm = &mgr->mm;
+       struct gpu_buddy *mm = &mgr->mm;
        struct amdgpu_vram_reservation *rsv;
 
        drm_printf(printer, "  vis usage:%llu\n",
@@ -930,7 +932,7 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
        mgr->default_page_size = PAGE_SIZE;
 
        man->func = &amdgpu_vram_mgr_func;
-       err = drm_buddy_init(&mgr->mm, man->size, PAGE_SIZE);
+       err = gpu_buddy_init(&mgr->mm, man->size, PAGE_SIZE);
        if (err)
                return err;
 
@@ -965,11 +967,11 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
                kfree(rsv);
 
        list_for_each_entry_safe(rsv, temp, &mgr->reserved_pages, blocks) {
-               drm_buddy_free_list(&mgr->mm, &rsv->allocated, 0);
+               gpu_buddy_free_list(&mgr->mm, &rsv->allocated, 0);
                kfree(rsv);
        }
        if (!adev->gmc.is_app_apu)
-               drm_buddy_fini(&mgr->mm);
+               gpu_buddy_fini(&mgr->mm);
        mutex_unlock(&mgr->lock);
 
        ttm_resource_manager_cleanup(man);
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.h +10 -10
index 5f5fd9a911c2..429a21a2e9b2 100644
@@ -24,11 +24,11 @@
 #ifndef __AMDGPU_VRAM_MGR_H__
 #define __AMDGPU_VRAM_MGR_H__
 
-#include <drm/drm_buddy.h>
+#include <linux/gpu_buddy.h>
 
 struct amdgpu_vram_mgr {
        struct ttm_resource_manager manager;
-       struct drm_buddy mm;
+       struct gpu_buddy mm;
        /* protects access to buffer objects */
        struct mutex lock;
        struct list_head reservations_pending;
@@ -57,19 +57,19 @@ struct amdgpu_vram_mgr_resource {
        struct amdgpu_vres_task task;
 };
 
-static inline u64 amdgpu_vram_mgr_block_start(struct drm_buddy_block *block)
+static inline u64 amdgpu_vram_mgr_block_start(struct gpu_buddy_block *block)
 {
-       return drm_buddy_block_offset(block);
+       return gpu_buddy_block_offset(block);
 }
 
-static inline u64 amdgpu_vram_mgr_block_size(struct drm_buddy_block *block)
+static inline u64 amdgpu_vram_mgr_block_size(struct gpu_buddy_block *block)
 {
-       return (u64)PAGE_SIZE << drm_buddy_block_order(block);
+       return (u64)PAGE_SIZE << gpu_buddy_block_order(block);
 }
 
-static inline bool amdgpu_vram_mgr_is_cleared(struct drm_buddy_block *block)
+static inline bool amdgpu_vram_mgr_is_cleared(struct gpu_buddy_block *block)
 {
-       return drm_buddy_block_is_clear(block);
+       return gpu_buddy_block_is_clear(block);
 }
 
 static inline struct amdgpu_vram_mgr_resource *
@@ -82,8 +82,8 @@ static inline void amdgpu_vram_mgr_set_cleared(struct ttm_resource *res)
 {
        struct amdgpu_vram_mgr_resource *ares = to_amdgpu_vram_mgr_resource(res);
 
-       WARN_ON(ares->flags & DRM_BUDDY_CLEARED);
-       ares->flags |= DRM_BUDDY_CLEARED;
+       WARN_ON(ares->flags & GPU_BUDDY_CLEARED);
+       ares->flags |= GPU_BUDDY_CLEARED;
 }
 
 int amdgpu_vram_mgr_query_address_block_info(struct amdgpu_vram_mgr *mgr,
drivers/gpu/drm/drm_buddy.c +17 -1267
index 2f279b46bd2c..188b36054e59 100644
@@ -3,1262 +3,25 @@
  * Copyright © 2021 Intel Corporation
  */
 
-#include <kunit/test-bug.h>
-
 #include <linux/export.h>
-#include <linux/kmemleak.h>
 #include <linux/module.h>
 #include <linux/sizes.h>
 
 #include <drm/drm_buddy.h>
 #include <drm/drm_print.h>
 
-enum drm_buddy_free_tree {
-       DRM_BUDDY_CLEAR_TREE = 0,
-       DRM_BUDDY_DIRTY_TREE,
-       DRM_BUDDY_MAX_FREE_TREES,
-};
-
-static struct kmem_cache *slab_blocks;
-
-#define for_each_free_tree(tree) \
-       for ((tree) = 0; (tree) < DRM_BUDDY_MAX_FREE_TREES; (tree)++)
-
-static struct drm_buddy_block *drm_block_alloc(struct drm_buddy *mm,
-                                              struct drm_buddy_block *parent,
-                                              unsigned int order,
-                                              u64 offset)
-{
-       struct drm_buddy_block *block;
-
-       BUG_ON(order > DRM_BUDDY_MAX_ORDER);
-
-       block = kmem_cache_zalloc(slab_blocks, GFP_KERNEL);
-       if (!block)
-               return NULL;
-
-       block->header = offset;
-       block->header |= order;
-       block->parent = parent;
-
-       RB_CLEAR_NODE(&block->rb);
-
-       BUG_ON(block->header & DRM_BUDDY_HEADER_UNUSED);
-       return block;
-}
-
-static void drm_block_free(struct drm_buddy *mm,
-                          struct drm_buddy_block *block)
-{
-       kmem_cache_free(slab_blocks, block);
-}
-
-static enum drm_buddy_free_tree
-get_block_tree(struct drm_buddy_block *block)
-{
-       return drm_buddy_block_is_clear(block) ?
-              DRM_BUDDY_CLEAR_TREE : DRM_BUDDY_DIRTY_TREE;
-}
-
-static struct drm_buddy_block *
-rbtree_get_free_block(const struct rb_node *node)
-{
-       return node ? rb_entry(node, struct drm_buddy_block, rb) : NULL;
-}
-
-static struct drm_buddy_block *
-rbtree_last_free_block(struct rb_root *root)
-{
-       return rbtree_get_free_block(rb_last(root));
-}
-
-static bool rbtree_is_empty(struct rb_root *root)
-{
-       return RB_EMPTY_ROOT(root);
-}
-
-static bool drm_buddy_block_offset_less(const struct drm_buddy_block *block,
-                                       const struct drm_buddy_block *node)
-{
-       return drm_buddy_block_offset(block) < drm_buddy_block_offset(node);
-}
-
-static bool rbtree_block_offset_less(struct rb_node *block,
-                                    const struct rb_node *node)
-{
-       return drm_buddy_block_offset_less(rbtree_get_free_block(block),
-                                          rbtree_get_free_block(node));
-}
-
-static void rbtree_insert(struct drm_buddy *mm,
-                         struct drm_buddy_block *block,
-                         enum drm_buddy_free_tree tree)
-{
-       rb_add(&block->rb,
-              &mm->free_trees[tree][drm_buddy_block_order(block)],
-              rbtree_block_offset_less);
-}
-
-static void rbtree_remove(struct drm_buddy *mm,
-                         struct drm_buddy_block *block)
-{
-       unsigned int order = drm_buddy_block_order(block);
-       enum drm_buddy_free_tree tree;
-       struct rb_root *root;
-
-       tree = get_block_tree(block);
-       root = &mm->free_trees[tree][order];
-
-       rb_erase(&block->rb, root);
-       RB_CLEAR_NODE(&block->rb);
-}
-
-static void clear_reset(struct drm_buddy_block *block)
-{
-       block->header &= ~DRM_BUDDY_HEADER_CLEAR;
-}
-
-static void mark_cleared(struct drm_buddy_block *block)
-{
-       block->header |= DRM_BUDDY_HEADER_CLEAR;
-}
-
-static void mark_allocated(struct drm_buddy *mm,
-                          struct drm_buddy_block *block)
-{
-       block->header &= ~DRM_BUDDY_HEADER_STATE;
-       block->header |= DRM_BUDDY_ALLOCATED;
-
-       rbtree_remove(mm, block);
-}
-
-static void mark_free(struct drm_buddy *mm,
-                     struct drm_buddy_block *block)
-{
-       enum drm_buddy_free_tree tree;
-
-       block->header &= ~DRM_BUDDY_HEADER_STATE;
-       block->header |= DRM_BUDDY_FREE;
-
-       tree = get_block_tree(block);
-       rbtree_insert(mm, block, tree);
-}
-
-static void mark_split(struct drm_buddy *mm,
-                      struct drm_buddy_block *block)
-{
-       block->header &= ~DRM_BUDDY_HEADER_STATE;
-       block->header |= DRM_BUDDY_SPLIT;
-
-       rbtree_remove(mm, block);
-}
-
-static inline bool overlaps(u64 s1, u64 e1, u64 s2, u64 e2)
-{
-       return s1 <= e2 && e1 >= s2;
-}
-
-static inline bool contains(u64 s1, u64 e1, u64 s2, u64 e2)
-{
-       return s1 <= s2 && e1 >= e2;
-}
-
-static struct drm_buddy_block *
-__get_buddy(struct drm_buddy_block *block)
-{
-       struct drm_buddy_block *parent;
-
-       parent = block->parent;
-       if (!parent)
-               return NULL;
-
-       if (parent->left == block)
-               return parent->right;
-
-       return parent->left;
-}
-
-static unsigned int __drm_buddy_free(struct drm_buddy *mm,
-                                    struct drm_buddy_block *block,
-                                    bool force_merge)
-{
-       struct drm_buddy_block *parent;
-       unsigned int order;
-
-       while ((parent = block->parent)) {
-               struct drm_buddy_block *buddy;
-
-               buddy = __get_buddy(block);
-
-               if (!drm_buddy_block_is_free(buddy))
-                       break;
-
-               if (!force_merge) {
-                       /*
-                        * Check the block and its buddy clear state and exit
-                        * the loop if they both have the dissimilar state.
-                        */
-                       if (drm_buddy_block_is_clear(block) !=
-                           drm_buddy_block_is_clear(buddy))
-                               break;
-
-                       if (drm_buddy_block_is_clear(block))
-                               mark_cleared(parent);
-               }
-
-               rbtree_remove(mm, buddy);
-               if (force_merge && drm_buddy_block_is_clear(buddy))
-                       mm->clear_avail -= drm_buddy_block_size(mm, buddy);
-
-               drm_block_free(mm, block);
-               drm_block_free(mm, buddy);
-
-               block = parent;
-       }
-
-       order = drm_buddy_block_order(block);
-       mark_free(mm, block);
-
-       return order;
-}
-
-static int __force_merge(struct drm_buddy *mm,
-                        u64 start,
-                        u64 end,
-                        unsigned int min_order)
-{
-       unsigned int tree, order;
-       int i;
-
-       if (!min_order)
-               return -ENOMEM;
-
-       if (min_order > mm->max_order)
-               return -EINVAL;
-
-       for_each_free_tree(tree) {
-               for (i = min_order - 1; i >= 0; i--) {
-                       struct rb_node *iter = rb_last(&mm->free_trees[tree][i]);
-
-                       while (iter) {
-                               struct drm_buddy_block *block, *buddy;
-                               u64 block_start, block_end;
-
-                               block = rbtree_get_free_block(iter);
-                               iter = rb_prev(iter);
-
-                               if (!block || !block->parent)
-                                       continue;
-
-                               block_start = drm_buddy_block_offset(block);
-                               block_end = block_start + drm_buddy_block_size(mm, block) - 1;
-
-                               if (!contains(start, end, block_start, block_end))
-                                       continue;
-
-                               buddy = __get_buddy(block);
-                               if (!drm_buddy_block_is_free(buddy))
-                                       continue;
-
-                               WARN_ON(drm_buddy_block_is_clear(block) ==
-                                       drm_buddy_block_is_clear(buddy));
-
-                               /*
-                                * Advance to the next node when the current node is the buddy,
-                                * as freeing the block will also remove its buddy from the tree.
-                                */
-                               if (iter == &buddy->rb)
-                                       iter = rb_prev(iter);
-
-                               rbtree_remove(mm, block);
-                               if (drm_buddy_block_is_clear(block))
-                                       mm->clear_avail -= drm_buddy_block_size(mm, block);
-
-                               order = __drm_buddy_free(mm, block, true);
-                               if (order >= min_order)
-                                       return 0;
-                       }
-               }
-       }
-
-       return -ENOMEM;
-}
-
-/**
- * drm_buddy_init - init memory manager
- *
- * @mm: DRM buddy manager to initialize
- * @size: size in bytes to manage
- * @chunk_size: minimum page size in bytes for our allocations
- *
- * Initializes the memory manager and its resources.
- *
- * Returns:
- * 0 on success, error code on failure.
- */
-int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size)
-{
-       unsigned int i, j, root_count = 0;
-       u64 offset = 0;
-
-       if (size < chunk_size)
-               return -EINVAL;
-
-       if (chunk_size < SZ_4K)
-               return -EINVAL;
-
-       if (!is_power_of_2(chunk_size))
-               return -EINVAL;
-
-       size = round_down(size, chunk_size);
-
-       mm->size = size;
-       mm->avail = size;
-       mm->clear_avail = 0;
-       mm->chunk_size = chunk_size;
-       mm->max_order = ilog2(size) - ilog2(chunk_size);
-
-       BUG_ON(mm->max_order > DRM_BUDDY_MAX_ORDER);
-
-       mm->free_trees = kmalloc_array(DRM_BUDDY_MAX_FREE_TREES,
-                                      sizeof(*mm->free_trees),
-                                      GFP_KERNEL);
-       if (!mm->free_trees)
-               return -ENOMEM;
-
-       for_each_free_tree(i) {
-               mm->free_trees[i] = kmalloc_array(mm->max_order + 1,
-                                                 sizeof(struct rb_root),
-                                                 GFP_KERNEL);
-               if (!mm->free_trees[i])
-                       goto out_free_tree;
-
-               for (j = 0; j <= mm->max_order; ++j)
-                       mm->free_trees[i][j] = RB_ROOT;
-       }
-
-       mm->n_roots = hweight64(size);
-
-       mm->roots = kmalloc_array(mm->n_roots,
-                                 sizeof(struct drm_buddy_block *),
-                                 GFP_KERNEL);
-       if (!mm->roots)
-               goto out_free_tree;
-
-       /*
-        * Split into power-of-two blocks, in case we are given a size that is
-        * not itself a power-of-two.
-        */
-       do {
-               struct drm_buddy_block *root;
-               unsigned int order;
-               u64 root_size;
-
-               order = ilog2(size) - ilog2(chunk_size);
-               root_size = chunk_size << order;
-
-               root = drm_block_alloc(mm, NULL, order, offset);
-               if (!root)
-                       goto out_free_roots;
-
-               mark_free(mm, root);
-
-               BUG_ON(root_count > mm->max_order);
-               BUG_ON(drm_buddy_block_size(mm, root) < chunk_size);
-
-               mm->roots[root_count] = root;
-
-               offset += root_size;
-               size -= root_size;
-               root_count++;
-       } while (size);
-
-       return 0;
-
-out_free_roots:
-       while (root_count--)
-               drm_block_free(mm, mm->roots[root_count]);
-       kfree(mm->roots);
-out_free_tree:
-       while (i--)
-               kfree(mm->free_trees[i]);
-       kfree(mm->free_trees);
-       return -ENOMEM;
-}
-EXPORT_SYMBOL(drm_buddy_init);
-
-/**
- * drm_buddy_fini - tear down the memory manager
- *
- * @mm: DRM buddy manager to free
- *
- * Cleanup memory manager resources and the freetree
- */
-void drm_buddy_fini(struct drm_buddy *mm)
-{
-       u64 root_size, size, start;
-       unsigned int order;
-       int i;
-
-       size = mm->size;
-
-       for (i = 0; i < mm->n_roots; ++i) {
-               order = ilog2(size) - ilog2(mm->chunk_size);
-               start = drm_buddy_block_offset(mm->roots[i]);
-               __force_merge(mm, start, start + size, order);
-
-               if (WARN_ON(!drm_buddy_block_is_free(mm->roots[i])))
-                       kunit_fail_current_test("buddy_fini() root");
-
-               drm_block_free(mm, mm->roots[i]);
-
-               root_size = mm->chunk_size << order;
-               size -= root_size;
-       }
-
-       WARN_ON(mm->avail != mm->size);
-
-       for_each_free_tree(i)
-               kfree(mm->free_trees[i]);
-       kfree(mm->roots);
-}
-EXPORT_SYMBOL(drm_buddy_fini);
-
-static int split_block(struct drm_buddy *mm,
-                      struct drm_buddy_block *block)
-{
-       unsigned int block_order = drm_buddy_block_order(block) - 1;
-       u64 offset = drm_buddy_block_offset(block);
-
-       BUG_ON(!drm_buddy_block_is_free(block));
-       BUG_ON(!drm_buddy_block_order(block));
-
-       block->left = drm_block_alloc(mm, block, block_order, offset);
-       if (!block->left)
-               return -ENOMEM;
-
-       block->right = drm_block_alloc(mm, block, block_order,
-                                      offset + (mm->chunk_size << block_order));
-       if (!block->right) {
-               drm_block_free(mm, block->left);
-               return -ENOMEM;
-       }
-
-       mark_split(mm, block);
-
-       if (drm_buddy_block_is_clear(block)) {
-               mark_cleared(block->left);
-               mark_cleared(block->right);
-               clear_reset(block);
-       }
-
-       mark_free(mm, block->left);
-       mark_free(mm, block->right);
-
-       return 0;
-}
-
-/**
- * drm_get_buddy - get buddy address
- *
- * @block: DRM buddy block
- *
- * Returns the corresponding buddy block for @block, or NULL
- * if this is a root block and can't be merged further.
- * Requires some kind of locking to protect against
- * any concurrent allocate and free operations.
- */
-struct drm_buddy_block *
-drm_get_buddy(struct drm_buddy_block *block)
-{
-       return __get_buddy(block);
-}
-EXPORT_SYMBOL(drm_get_buddy);
-
-/**
- * drm_buddy_reset_clear - reset blocks clear state
- *
- * @mm: DRM buddy manager
- * @is_clear: blocks clear state
- *
- * Reset the clear state based on @is_clear value for each block
- * in the freetree.
- */
-void drm_buddy_reset_clear(struct drm_buddy *mm, bool is_clear)
-{
-       enum drm_buddy_free_tree src_tree, dst_tree;
-       u64 root_size, size, start;
-       unsigned int order;
-       int i;
-
-       size = mm->size;
-       for (i = 0; i < mm->n_roots; ++i) {
-               order = ilog2(size) - ilog2(mm->chunk_size);
-               start = drm_buddy_block_offset(mm->roots[i]);
-               __force_merge(mm, start, start + size, order);
-
-               root_size = mm->chunk_size << order;
-               size -= root_size;
-       }
-
-       src_tree = is_clear ? DRM_BUDDY_DIRTY_TREE : DRM_BUDDY_CLEAR_TREE;
-       dst_tree = is_clear ? DRM_BUDDY_CLEAR_TREE : DRM_BUDDY_DIRTY_TREE;
-
-       for (i = 0; i <= mm->max_order; ++i) {
-               struct rb_root *root = &mm->free_trees[src_tree][i];
-               struct drm_buddy_block *block, *tmp;
-
-               rbtree_postorder_for_each_entry_safe(block, tmp, root, rb) {
-                       rbtree_remove(mm, block);
-                       if (is_clear) {
-                               mark_cleared(block);
-                               mm->clear_avail += drm_buddy_block_size(mm, block);
-                       } else {
-                               clear_reset(block);
-                               mm->clear_avail -= drm_buddy_block_size(mm, block);
-                       }
-
-                       rbtree_insert(mm, block, dst_tree);
-               }
-       }
-}
-EXPORT_SYMBOL(drm_buddy_reset_clear);
-
-/**
- * drm_buddy_free_block - free a block
- *
- * @mm: DRM buddy manager
- * @block: block to be freed
- */
-void drm_buddy_free_block(struct drm_buddy *mm,
-                         struct drm_buddy_block *block)
-{
-       BUG_ON(!drm_buddy_block_is_allocated(block));
-       mm->avail += drm_buddy_block_size(mm, block);
-       if (drm_buddy_block_is_clear(block))
-               mm->clear_avail += drm_buddy_block_size(mm, block);
-
-       __drm_buddy_free(mm, block, false);
-}
-EXPORT_SYMBOL(drm_buddy_free_block);
-
-static void __drm_buddy_free_list(struct drm_buddy *mm,
-                                 struct list_head *objects,
-                                 bool mark_clear,
-                                 bool mark_dirty)
-{
-       struct drm_buddy_block *block, *on;
-
-       WARN_ON(mark_dirty && mark_clear);
-
-       list_for_each_entry_safe(block, on, objects, link) {
-               if (mark_clear)
-                       mark_cleared(block);
-               else if (mark_dirty)
-                       clear_reset(block);
-               drm_buddy_free_block(mm, block);
-               cond_resched();
-       }
-       INIT_LIST_HEAD(objects);
-}
-
-static void drm_buddy_free_list_internal(struct drm_buddy *mm,
-                                        struct list_head *objects)
-{
-       /*
-        * Don't touch the clear/dirty bit, since allocation is still internal
-        * at this point. For example we might have just failed part of the
-        * allocation.
-        */
-       __drm_buddy_free_list(mm, objects, false, false);
-}
-
-/**
- * drm_buddy_free_list - free blocks
- *
- * @mm: DRM buddy manager
- * @objects: input list head to free blocks
- * @flags: optional flags like DRM_BUDDY_CLEARED
- */
-void drm_buddy_free_list(struct drm_buddy *mm,
-                        struct list_head *objects,
-                        unsigned int flags)
-{
-       bool mark_clear = flags & DRM_BUDDY_CLEARED;
-
-       __drm_buddy_free_list(mm, objects, mark_clear, !mark_clear);
-}
-EXPORT_SYMBOL(drm_buddy_free_list);
-
-static bool block_incompatible(struct drm_buddy_block *block, unsigned int flags)
-{
-       bool needs_clear = flags & DRM_BUDDY_CLEAR_ALLOCATION;
-
-       return needs_clear != drm_buddy_block_is_clear(block);
-}
-
-static struct drm_buddy_block *
-__alloc_range_bias(struct drm_buddy *mm,
-                  u64 start, u64 end,
-                  unsigned int order,
-                  unsigned long flags,
-                  bool fallback)
-{
-       u64 req_size = mm->chunk_size << order;
-       struct drm_buddy_block *block;
-       struct drm_buddy_block *buddy;
-       LIST_HEAD(dfs);
-       int err;
-       int i;
-
-       end = end - 1;
-
-       for (i = 0; i < mm->n_roots; ++i)
-               list_add_tail(&mm->roots[i]->tmp_link, &dfs);
-
-       do {
-               u64 block_start;
-               u64 block_end;
-
-               block = list_first_entry_or_null(&dfs,
-                                                struct drm_buddy_block,
-                                                tmp_link);
-               if (!block)
-                       break;
-
-               list_del(&block->tmp_link);
-
-               if (drm_buddy_block_order(block) < order)
-                       continue;
-
-               block_start = drm_buddy_block_offset(block);
-               block_end = block_start + drm_buddy_block_size(mm, block) - 1;
-
-               if (!overlaps(start, end, block_start, block_end))
-                       continue;
-
-               if (drm_buddy_block_is_allocated(block))
-                       continue;
-
-               if (block_start < start || block_end > end) {
-                       u64 adjusted_start = max(block_start, start);
-                       u64 adjusted_end = min(block_end, end);
-
-                       if (round_down(adjusted_end + 1, req_size) <=
-                           round_up(adjusted_start, req_size))
-                               continue;
-               }
-
-               if (!fallback && block_incompatible(block, flags))
-                       continue;
-
-               if (contains(start, end, block_start, block_end) &&
-                   order == drm_buddy_block_order(block)) {
-                       /*
-                        * Find the free block within the range.
-                        */
-                       if (drm_buddy_block_is_free(block))
-                               return block;
-
-                       continue;
-               }
-
-               if (!drm_buddy_block_is_split(block)) {
-                       err = split_block(mm, block);
-                       if (unlikely(err))
-                               goto err_undo;
-               }
-
-               list_add(&block->right->tmp_link, &dfs);
-               list_add(&block->left->tmp_link, &dfs);
-       } while (1);
-
-       return ERR_PTR(-ENOSPC);
-
-err_undo:
-       /*
-        * We really don't want to leave around a bunch of split blocks, since
-        * bigger is better, so make sure we merge everything back before we
-        * free the allocated blocks.
-        */
-       buddy = __get_buddy(block);
-       if (buddy &&
-           (drm_buddy_block_is_free(block) &&
-            drm_buddy_block_is_free(buddy)))
-               __drm_buddy_free(mm, block, false);
-       return ERR_PTR(err);
-}
-
-static struct drm_buddy_block *
-__drm_buddy_alloc_range_bias(struct drm_buddy *mm,
-                            u64 start, u64 end,
-                            unsigned int order,
-                            unsigned long flags)
-{
-       struct drm_buddy_block *block;
-       bool fallback = false;
-
-       block = __alloc_range_bias(mm, start, end, order,
-                                  flags, fallback);
-       if (IS_ERR(block))
-               return __alloc_range_bias(mm, start, end, order,
-                                         flags, !fallback);
-
-       return block;
-}
-
-static struct drm_buddy_block *
-get_maxblock(struct drm_buddy *mm,
-            unsigned int order,
-            enum drm_buddy_free_tree tree)
-{
-       struct drm_buddy_block *max_block = NULL, *block = NULL;
-       struct rb_root *root;
-       unsigned int i;
-
-       for (i = order; i <= mm->max_order; ++i) {
-               root = &mm->free_trees[tree][i];
-               block = rbtree_last_free_block(root);
-               if (!block)
-                       continue;
-
-               if (!max_block) {
-                       max_block = block;
-                       continue;
-               }
-
-               if (drm_buddy_block_offset(block) >
-                   drm_buddy_block_offset(max_block)) {
-                       max_block = block;
-               }
-       }
-
-       return max_block;
-}
-
-static struct drm_buddy_block *
-alloc_from_freetree(struct drm_buddy *mm,
-                   unsigned int order,
-                   unsigned long flags)
-{
-       struct drm_buddy_block *block = NULL;
-       struct rb_root *root;
-       enum drm_buddy_free_tree tree;
-       unsigned int tmp;
-       int err;
-
-       tree = (flags & DRM_BUDDY_CLEAR_ALLOCATION) ?
-               DRM_BUDDY_CLEAR_TREE : DRM_BUDDY_DIRTY_TREE;
-
-       if (flags & DRM_BUDDY_TOPDOWN_ALLOCATION) {
-               block = get_maxblock(mm, order, tree);
-               if (block)
-                       /* Store the obtained block order */
-                       tmp = drm_buddy_block_order(block);
-       } else {
-               for (tmp = order; tmp <= mm->max_order; ++tmp) {
-                       /* Get RB tree root for this order and tree */
-                       root = &mm->free_trees[tree][tmp];
-                       block = rbtree_last_free_block(root);
-                       if (block)
-                               break;
-               }
-       }
-
-       if (!block) {
-               /* Try allocating from the other tree */
-               tree = (tree == DRM_BUDDY_CLEAR_TREE) ?
-                       DRM_BUDDY_DIRTY_TREE : DRM_BUDDY_CLEAR_TREE;
-
-               for (tmp = order; tmp <= mm->max_order; ++tmp) {
-                       root = &mm->free_trees[tree][tmp];
-                       block = rbtree_last_free_block(root);
-                       if (block)
-                               break;
-               }
-
-               if (!block)
-                       return ERR_PTR(-ENOSPC);
-       }
-
-       BUG_ON(!drm_buddy_block_is_free(block));
-
-       while (tmp != order) {
-               err = split_block(mm, block);
-               if (unlikely(err))
-                       goto err_undo;
-
-               block = block->right;
-               tmp--;
-       }
-       return block;
-
-err_undo:
-       if (tmp != order)
-               __drm_buddy_free(mm, block, false);
-       return ERR_PTR(err);
-}
-
-static int __alloc_range(struct drm_buddy *mm,
-                        struct list_head *dfs,
-                        u64 start, u64 size,
-                        struct list_head *blocks,
-                        u64 *total_allocated_on_err)
-{
-       struct drm_buddy_block *block;
-       struct drm_buddy_block *buddy;
-       u64 total_allocated = 0;
-       LIST_HEAD(allocated);
-       u64 end;
-       int err;
-
-       end = start + size - 1;
-
-       do {
-               u64 block_start;
-               u64 block_end;
-
-               block = list_first_entry_or_null(dfs,
-                                                struct drm_buddy_block,
-                                                tmp_link);
-               if (!block)
-                       break;
-
-               list_del(&block->tmp_link);
-
-               block_start = drm_buddy_block_offset(block);
-               block_end = block_start + drm_buddy_block_size(mm, block) - 1;
-
-               if (!overlaps(start, end, block_start, block_end))
-                       continue;
-
-               if (drm_buddy_block_is_allocated(block)) {
-                       err = -ENOSPC;
-                       goto err_free;
-               }
-
-               if (contains(start, end, block_start, block_end)) {
-                       if (drm_buddy_block_is_free(block)) {
-                               mark_allocated(mm, block);
-                               total_allocated += drm_buddy_block_size(mm, block);
-                               mm->avail -= drm_buddy_block_size(mm, block);
-                               if (drm_buddy_block_is_clear(block))
-                                       mm->clear_avail -= drm_buddy_block_size(mm, block);
-                               list_add_tail(&block->link, &allocated);
-                               continue;
-                       } else if (!mm->clear_avail) {
-                               err = -ENOSPC;
-                               goto err_free;
-                       }
-               }
-
-               if (!drm_buddy_block_is_split(block)) {
-                       err = split_block(mm, block);
-                       if (unlikely(err))
-                               goto err_undo;
-               }
-
-               list_add(&block->right->tmp_link, dfs);
-               list_add(&block->left->tmp_link, dfs);
-       } while (1);
-
-       if (total_allocated < size) {
-               err = -ENOSPC;
-               goto err_free;
-       }
-
-       list_splice_tail(&allocated, blocks);
-
-       return 0;
-
-err_undo:
-       /*
-        * We really don't want to leave around a bunch of split blocks, since
-        * bigger is better, so make sure we merge everything back before we
-        * free the allocated blocks.
-        */
-       buddy = __get_buddy(block);
-       if (buddy &&
-           (drm_buddy_block_is_free(block) &&
-            drm_buddy_block_is_free(buddy)))
-               __drm_buddy_free(mm, block, false);
-
-err_free:
-       if (err == -ENOSPC && total_allocated_on_err) {
-               list_splice_tail(&allocated, blocks);
-               *total_allocated_on_err = total_allocated;
-       } else {
-               drm_buddy_free_list_internal(mm, &allocated);
-       }
-
-       return err;
-}
-
-static int __drm_buddy_alloc_range(struct drm_buddy *mm,
-                                  u64 start,
-                                  u64 size,
-                                  u64 *total_allocated_on_err,
-                                  struct list_head *blocks)
-{
-       LIST_HEAD(dfs);
-       int i;
-
-       for (i = 0; i < mm->n_roots; ++i)
-               list_add_tail(&mm->roots[i]->tmp_link, &dfs);
-
-       return __alloc_range(mm, &dfs, start, size,
-                            blocks, total_allocated_on_err);
-}
-
-static int __alloc_contig_try_harder(struct drm_buddy *mm,
-                                    u64 size,
-                                    u64 min_block_size,
-                                    struct list_head *blocks)
-{
-       u64 rhs_offset, lhs_offset, lhs_size, filled;
-       struct drm_buddy_block *block;
-       unsigned int tree, order;
-       LIST_HEAD(blocks_lhs);
-       unsigned long pages;
-       u64 modify_size;
-       int err;
-
-       modify_size = rounddown_pow_of_two(size);
-       pages = modify_size >> ilog2(mm->chunk_size);
-       order = fls(pages) - 1;
-       if (order == 0)
-               return -ENOSPC;
-
-       for_each_free_tree(tree) {
-               struct rb_root *root;
-               struct rb_node *iter;
-
-               root = &mm->free_trees[tree][order];
-               if (rbtree_is_empty(root))
-                       continue;
-
-               iter = rb_last(root);
-               while (iter) {
-                       block = rbtree_get_free_block(iter);
-
-                       /* Allocate blocks traversing RHS */
-                       rhs_offset = drm_buddy_block_offset(block);
-                       err =  __drm_buddy_alloc_range(mm, rhs_offset, size,
-                                                      &filled, blocks);
-                       if (!err || err != -ENOSPC)
-                               return err;
-
-                       lhs_size = max((size - filled), min_block_size);
-                       if (!IS_ALIGNED(lhs_size, min_block_size))
-                               lhs_size = round_up(lhs_size, min_block_size);
-
-                       /* Allocate blocks traversing LHS */
-                       lhs_offset = drm_buddy_block_offset(block) - lhs_size;
-                       err =  __drm_buddy_alloc_range(mm, lhs_offset, lhs_size,
-                                                      NULL, &blocks_lhs);
-                       if (!err) {
-                               list_splice(&blocks_lhs, blocks);
-                               return 0;
-                       } else if (err != -ENOSPC) {
-                               drm_buddy_free_list_internal(mm, blocks);
-                               return err;
-                       }
-                       /* Free blocks for the next iteration */
-                       drm_buddy_free_list_internal(mm, blocks);
-
-                       iter = rb_prev(iter);
-               }
-       }
-
-       return -ENOSPC;
-}
-
-/**
- * drm_buddy_block_trim - free unused pages
- *
- * @mm: DRM buddy manager
- * @start: start address to begin the trimming.
- * @new_size: original size requested
- * @blocks: Input and output list of allocated blocks.
- * MUST contain single block as input to be trimmed.
- * On success will contain the newly allocated blocks
- * making up the @new_size. Blocks always appear in
- * ascending order
- *
- * For contiguous allocation, we round up the size to the nearest
- * power of two value, drivers consume *actual* size, so remaining
- * portions are unused and can be optionally freed with this function
- *
- * Returns:
- * 0 on success, error code on failure.
- */
-int drm_buddy_block_trim(struct drm_buddy *mm,
-                        u64 *start,
-                        u64 new_size,
-                        struct list_head *blocks)
-{
-       struct drm_buddy_block *parent;
-       struct drm_buddy_block *block;
-       u64 block_start, block_end;
-       LIST_HEAD(dfs);
-       u64 new_start;
-       int err;
-
-       if (!list_is_singular(blocks))
-               return -EINVAL;
-
-       block = list_first_entry(blocks,
-                                struct drm_buddy_block,
-                                link);
-
-       block_start = drm_buddy_block_offset(block);
-       block_end = block_start + drm_buddy_block_size(mm, block);
-
-       if (WARN_ON(!drm_buddy_block_is_allocated(block)))
-               return -EINVAL;
-
-       if (new_size > drm_buddy_block_size(mm, block))
-               return -EINVAL;
-
-       if (!new_size || !IS_ALIGNED(new_size, mm->chunk_size))
-               return -EINVAL;
-
-       if (new_size == drm_buddy_block_size(mm, block))
-               return 0;
-
-       new_start = block_start;
-       if (start) {
-               new_start = *start;
-
-               if (new_start < block_start)
-                       return -EINVAL;
-
-               if (!IS_ALIGNED(new_start, mm->chunk_size))
-                       return -EINVAL;
-
-               if (range_overflows(new_start, new_size, block_end))
-                       return -EINVAL;
-       }
-
-       list_del(&block->link);
-       mark_free(mm, block);
-       mm->avail += drm_buddy_block_size(mm, block);
-       if (drm_buddy_block_is_clear(block))
-               mm->clear_avail += drm_buddy_block_size(mm, block);
-
-       /* Prevent recursively freeing this node */
-       parent = block->parent;
-       block->parent = NULL;
-
-       list_add(&block->tmp_link, &dfs);
-       err =  __alloc_range(mm, &dfs, new_start, new_size, blocks, NULL);
-       if (err) {
-               mark_allocated(mm, block);
-               mm->avail -= drm_buddy_block_size(mm, block);
-               if (drm_buddy_block_is_clear(block))
-                       mm->clear_avail -= drm_buddy_block_size(mm, block);
-               list_add(&block->link, blocks);
-       }
-
-       block->parent = parent;
-       return err;
-}
-EXPORT_SYMBOL(drm_buddy_block_trim);
-
-static struct drm_buddy_block *
-__drm_buddy_alloc_blocks(struct drm_buddy *mm,
-                        u64 start, u64 end,
-                        unsigned int order,
-                        unsigned long flags)
-{
-       if (flags & DRM_BUDDY_RANGE_ALLOCATION)
-               /* Allocate traversing within the range */
-               return  __drm_buddy_alloc_range_bias(mm, start, end,
-                                                    order, flags);
-       else
-               /* Allocate from freetree */
-               return alloc_from_freetree(mm, order, flags);
-}
-
-/**
- * drm_buddy_alloc_blocks - allocate power-of-two blocks
- *
- * @mm: DRM buddy manager to allocate from
- * @start: start of the allowed range for this block
- * @end: end of the allowed range for this block
- * @size: size of the allocation in bytes
- * @min_block_size: alignment of the allocation
- * @blocks: output list head to add allocated blocks
- * @flags: DRM_BUDDY_*_ALLOCATION flags
- *
- * alloc_range_bias() called on range limitations, which traverses
- * the tree and returns the desired block.
- *
- * alloc_from_freetree() called when *no* range restrictions
- * are enforced, which picks the block from the freetree.
- *
- * Returns:
- * 0 on success, error code on failure.
- */
-int drm_buddy_alloc_blocks(struct drm_buddy *mm,
-                          u64 start, u64 end, u64 size,
-                          u64 min_block_size,
-                          struct list_head *blocks,
-                          unsigned long flags)
-{
-       struct drm_buddy_block *block = NULL;
-       u64 original_size, original_min_size;
-       unsigned int min_order, order;
-       LIST_HEAD(allocated);
-       unsigned long pages;
-       int err;
-
-       if (size < mm->chunk_size)
-               return -EINVAL;
-
-       if (min_block_size < mm->chunk_size)
-               return -EINVAL;
-
-       if (!is_power_of_2(min_block_size))
-               return -EINVAL;
-
-       if (!IS_ALIGNED(start | end | size, mm->chunk_size))
-               return -EINVAL;
-
-       if (end > mm->size)
-               return -EINVAL;
-
-       if (range_overflows(start, size, mm->size))
-               return -EINVAL;
-
-       /* Actual range allocation */
-       if (start + size == end) {
-               if (!IS_ALIGNED(start | end, min_block_size))
-                       return -EINVAL;
-
-               return __drm_buddy_alloc_range(mm, start, size, NULL, blocks);
-       }
-
-       original_size = size;
-       original_min_size = min_block_size;
-
-       /* Roundup the size to power of 2 */
-       if (flags & DRM_BUDDY_CONTIGUOUS_ALLOCATION) {
-               size = roundup_pow_of_two(size);
-               min_block_size = size;
-       /* Align size value to min_block_size */
-       } else if (!IS_ALIGNED(size, min_block_size)) {
-               size = round_up(size, min_block_size);
-       }
-
-       pages = size >> ilog2(mm->chunk_size);
-       order = fls(pages) - 1;
-       min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
-
-       do {
-               order = min(order, (unsigned int)fls(pages) - 1);
-               BUG_ON(order > mm->max_order);
-               BUG_ON(order < min_order);
-
-               do {
-                       block = __drm_buddy_alloc_blocks(mm, start,
-                                                        end,
-                                                        order,
-                                                        flags);
-                       if (!IS_ERR(block))
-                               break;
-
-                       if (order-- == min_order) {
-                               /* Try allocation through force merge method */
-                               if (mm->clear_avail &&
-                                   !__force_merge(mm, start, end, min_order)) {
-                                       block = __drm_buddy_alloc_blocks(mm, start,
-                                                                        end,
-                                                                        min_order,
-                                                                        flags);
-                                       if (!IS_ERR(block)) {
-                                               order = min_order;
-                                               break;
-                                       }
-                               }
-
-                               /*
-                                * Try contiguous block allocation through
-                                * try harder method.
-                                */
-                               if (flags & DRM_BUDDY_CONTIGUOUS_ALLOCATION &&
-                                   !(flags & DRM_BUDDY_RANGE_ALLOCATION))
-                                       return __alloc_contig_try_harder(mm,
-                                                                        original_size,
-                                                                        original_min_size,
-                                                                        blocks);
-                               err = -ENOSPC;
-                               goto err_free;
-                       }
-               } while (1);
-
-               mark_allocated(mm, block);
-               mm->avail -= drm_buddy_block_size(mm, block);
-               if (drm_buddy_block_is_clear(block))
-                       mm->clear_avail -= drm_buddy_block_size(mm, block);
-               kmemleak_update_trace(block);
-               list_add_tail(&block->link, &allocated);
-
-               pages -= BIT(order);
-
-               if (!pages)
-                       break;
-       } while (1);
-
-       /* Trim the allocated block to the required size */
-       if (!(flags & DRM_BUDDY_TRIM_DISABLE) &&
-           original_size != size) {
-               struct list_head *trim_list;
-               LIST_HEAD(temp);
-               u64 trim_size;
-
-               trim_list = &allocated;
-               trim_size = original_size;
-
-               if (!list_is_singular(&allocated)) {
-                       block = list_last_entry(&allocated, typeof(*block), link);
-                       list_move(&block->link, &temp);
-                       trim_list = &temp;
-                       trim_size = drm_buddy_block_size(mm, block) -
-                               (size - original_size);
-               }
-
-               drm_buddy_block_trim(mm,
-                                    NULL,
-                                    trim_size,
-                                    trim_list);
-
-               if (!list_empty(&temp))
-                       list_splice_tail(trim_list, &allocated);
-       }
-
-       list_splice_tail(&allocated, blocks);
-       return 0;
-
-err_free:
-       drm_buddy_free_list_internal(mm, &allocated);
-       return err;
-}
-EXPORT_SYMBOL(drm_buddy_alloc_blocks);
-
 /**
  * drm_buddy_block_print - print block information
  *
- * @mm: DRM buddy manager
- * @block: DRM buddy block
+ * @mm: GPU buddy manager
+ * @block: GPU buddy block
  * @p: DRM printer to use
  */
-void drm_buddy_block_print(struct drm_buddy *mm,
-                          struct drm_buddy_block *block,
+void drm_buddy_block_print(struct gpu_buddy *mm, struct gpu_buddy_block *block,
                           struct drm_printer *p)
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/Kconfig +1 -0
index 5e939004b646..859aeca87c19 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/i915_scatterlist.c +5 -5
index 4d830740946d..6a34dae13769 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +28 -27
index d5c6e6605086..f43d7f2771ad 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/i915_ttm_buddy_manager.h +3 -3
index d64620712830..4a92dcf09766 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/i915/selftests/intel_memory_region.c +10 -10
index 7b856b5090f9..8307390943a2 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/tests/Makefile +0 -1
index 87d5d5f9332a..d2e2e3d8349a 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +3 -2
index 2eda87882e65..ffa12473077c 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/ttm/tests/ttm_mock_manager.c +9 -9
index dd395229e388..294d56d9067e 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/ttm/tests/ttm_mock_manager.h +2 -2
index e4c95f86a467..08710756fd8e 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/Kconfig +1 -0
index 4b288eb3f5b0..982ef754742e 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_res_cursor.h +17 -17
index 4e00008b7081..5f4ab08c0686 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_svm.c +6 -6
index f97e0af6a9b0..2b7e266f9bdd 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +37 -36
index 9f70802fce92..8192957261e8 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/drm/xe/xe_ttm_vram_mgr_types.h +2 -2
index a71e14818ec2..9106da056b49 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/Makefile new file +3 -0
new file mode 100644
index 000000000000..31a5ff44cb4e

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/gpu_buddy_test.c renamed from drivers/gpu/drm/tests/drm_buddy_test.c +195 -195
similarity index 68%
rename from drivers/gpu/drm/tests/drm_buddy_test.c
rename to drivers/gpu/tests/gpu_buddy_test.c
index 5f40b5343bd8..dcd4741a905d 100644

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/gpu_random.c new file +48 -0
new file mode 100644
index 000000000000..54f1f6a3a6c1

Lines of this file are not shown. View the raw message.

drivers/gpu/tests/gpu_random.h new file +28 -0
new file mode 100644
index 000000000000..b68cf3448264

Lines of this file are not shown. View the raw message.

drivers/video/Kconfig +2 -0
index d51777df12d1..6ae1383b0e2e 100644

Lines of this file are not shown. View the raw message.

include/drm/drm_buddy.h +5 -158
index b909fa8f810a..3054369bebff 100644

Lines of this file are not shown. View the raw message.

include/linux/gpu_buddy.h new file +177 -1
new file mode 100644
index 000000000000..3e4bd11ccb71

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

2.34.1

Part of the diff is not shown here. View the raw message.

author reviewer
#12 lore raw

Re: [PATCH RFC v6 02/26] gpu: Move DRM buddy allocator one level up

Quoted 11 lines · Dave Airlie
Quoted 10 lines · Joel Fernandes

Move the DRM buddy allocator one level up so that it can be used by GPU drivers (example, nova-core) that have usecases other than DRM (such as VFIO vGPU support). Modify the API, structures and Kconfigs to use "gpu_buddy" terminology. Adapt the drivers and tests to use the new API.

The commit cannot be split due to bisectability, however no functional change is intended. Verified by running K-UNIT tests and build tested various configurations.

Quoted 5 lines
Quoted 1 line

I suggested this and think it's a good idea.

Thanks, Dave!

--
Joel Fernandes

maintainer
#13 lore raw

Re: [PATCH RFC v6 02/26] gpu: Move DRM buddy allocator one level up

Quoted 15 lines · Joel Fernandes
Quoted 11 lines · Dave Airlie
Quoted 10 lines · Joel Fernandes

Move the DRM buddy allocator one level up so that it can be used by GPU drivers (example, nova-core) that have usecases other than DRM (such as VFIO vGPU support). Modify the API, structures and Kconfigs to use "gpu_buddy" terminology. Adapt the drivers and tests to use the new API.

The commit cannot be split due to bisectability, however no functional change is intended. Verified by running K-UNIT tests and build tested various configurations.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

I suggested this and think it's a good idea.

Thanks, Dave!

I'm going to apply this to drm-misc-next today but I'll move some of it around.

Dave.

author reviewer
#14 lore raw

RFC PATCH v6 3/26 rust: gpu: Add GPU buddy allocator bindings

In reply to Joel Fernandes

Add safe Rust abstractions over the Linux kernel's GPU buddy allocator for physical memory management. The GPU buddy allocator implements a binary buddy system useful for GPU physical memory allocation. nova-core will use it for physical memory allocation.

---
 rust/bindings/bindings_helper.h |  11 +
 rust/helpers/gpu.c              |  23 ++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/gpu/buddy.rs        | 538 ++++++++++++++++++++++++++++++++
 rust/kernel/gpu/mod.rs          |   5 +
 rust/kernel/lib.rs              |   2 +
 6 files changed, 580 insertions(+)
 create mode 100644 rust/helpers/gpu.c
 create mode 100644 rust/kernel/gpu/buddy.rs
 create mode 100644 rust/kernel/gpu/mod.rs
rust/bindings/bindings_helper.h +11 -0
index a067038b4b42..940b854a1f93 100644
@@ -29,6 +29,7 @@
 #include <linux/hrtimer_types.h>
 
 #include <linux/acpi.h>
+#include <linux/gpu_buddy.h>
 #include <drm/drm_device.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_file.h>
@@ -144,6 +145,16 @@ const vm_flags_t RUST_CONST_HELPER_VM_MIXEDMAP = VM_MIXEDMAP;
 const vm_flags_t RUST_CONST_HELPER_VM_HUGEPAGE = VM_HUGEPAGE;
 const vm_flags_t RUST_CONST_HELPER_VM_NOHUGEPAGE = VM_NOHUGEPAGE;
 
+#if IS_ENABLED(CONFIG_GPU_BUDDY)
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_RANGE_ALLOCATION = GPU_BUDDY_RANGE_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_TOPDOWN_ALLOCATION = GPU_BUDDY_TOPDOWN_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_CONTIGUOUS_ALLOCATION =
+								GPU_BUDDY_CONTIGUOUS_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_CLEAR_ALLOCATION = GPU_BUDDY_CLEAR_ALLOCATION;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_CLEARED = GPU_BUDDY_CLEARED;
+const unsigned long RUST_CONST_HELPER_GPU_BUDDY_TRIM_DISABLE = GPU_BUDDY_TRIM_DISABLE;
+#endif
+
 #if IS_ENABLED(CONFIG_ANDROID_BINDER_IPC_RUST)
 #include "../../drivers/android/binder/rust_binder.h"
 #include "../../drivers/android/binder/rust_binder_events.h"
rust/helpers/gpu.c new file +23 -0
new file mode 100644
index 000000000000..38b1a4e6bef8
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/gpu_buddy.h>
+
+#ifdef CONFIG_GPU_BUDDY
+
+__rust_helper u64 rust_helper_gpu_buddy_block_offset(const struct gpu_buddy_block *block)
+{
+	return gpu_buddy_block_offset(block);
+}
+
+__rust_helper unsigned int rust_helper_gpu_buddy_block_order(struct gpu_buddy_block *block)
+{
+	return gpu_buddy_block_order(block);
+}
+
+__rust_helper u64 rust_helper_gpu_buddy_block_size(struct gpu_buddy *mm,
+						   struct gpu_buddy_block *block)
+{
+	return gpu_buddy_block_size(mm, block);
+}
+
+#endif /* CONFIG_GPU_BUDDY */
rust/helpers/helpers.c +1 -0
index 634fa2386bbb..6db7c4c25afa 100644
@@ -29,6 +29,7 @@
 #include "err.c"
 #include "irq.c"
 #include "fs.c"
+#include "gpu.c"
 #include "io.c"
 #include "jump_label.c"
 #include "kunit.c"
rust/kernel/gpu/buddy.rs new file +538 -0

538 lines not on this page · raw message

rust/kernel/gpu/mod.rs new file +5 -0
new file mode 100644
index 000000000000..8f25e6367edc
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! GPU subsystem abstractions.
+
+pub mod buddy;
rust/kernel/lib.rs +2 -0
index cd7e6a1055b0..d754d777f8ff 100644
@@ -98,6 +98,8 @@
 pub mod firmware;
 pub mod fmt;
 pub mod fs;
+#[cfg(CONFIG_GPU_BUDDY)]
+pub mod gpu;
 #[cfg(CONFIG_I2C = "y")]
 pub mod i2c;
 pub mod id_pool;
Signature · 1 line
2.34.1
maintainer
#15 lore raw

Re: [PATCH RFC v6 03/26] rust: gpu: Add GPU buddy allocator bindings

Quoted 67 lines
rust/kernel/gpu/buddy.rs new file +67 -0
+///
+/// These flags control the allocation behavior of the buddy allocator.
+#[derive(Clone, Copy, Default, PartialEq, Eq)]
+pub struct BuddyFlags(usize);
+
+impl BuddyFlags {
+    /// Range-based allocation from start to end addresses.
+    pub const RANGE_ALLOCATION: usize = bindings::GPU_BUDDY_RANGE_ALLOCATION;
+
+    /// Allocate from top of address space downward.
+    pub const TOPDOWN_ALLOCATION: usize = bindings::GPU_BUDDY_TOPDOWN_ALLOCATION;
+
+    /// Allocate physically contiguous blocks.
+    pub const CONTIGUOUS_ALLOCATION: usize = bindings::GPU_BUDDY_CONTIGUOUS_ALLOCATION;
+
+    /// Request allocation from the cleared (zeroed) memory. The zero'ing is not
+    /// done by the allocator, but by the caller before freeing old blocks.
+    pub const CLEAR_ALLOCATION: usize = bindings::GPU_BUDDY_CLEAR_ALLOCATION;
+
+    /// Disable trimming of partially used blocks.
+    pub const TRIM_DISABLE: usize = bindings::GPU_BUDDY_TRIM_DISABLE;
+
+    /// Mark blocks as cleared (zeroed) when freeing. When set during free,
+    /// indicates that the caller has already zeroed the memory.
+    pub const CLEARED: usize = bindings::GPU_BUDDY_CLEARED;
+
+    /// Create [`BuddyFlags`] from a raw value with validation.
+    ///
+    /// Use `|` operator to combine flags if needed, before calling this method.
+    pub fn try_new(flags: usize) -> Result<Self> {
+        // Flags must not exceed u32::MAX to satisfy the GPU buddy allocator C API.
+        if flags > u32::MAX as usize {
+            return Err(EINVAL);
+        }
+
+        // `TOPDOWN_ALLOCATION` only works without `RANGE_ALLOCATION`. When both are
+        // set, `TOPDOWN_ALLOCATION` is silently ignored by the allocator. Reject this.
+        if (flags & Self::RANGE_ALLOCATION) != 0 && (flags & Self::TOPDOWN_ALLOCATION) != 0 {
+            return Err(EINVAL);
+        }
+
+        Ok(Self(flags))
+    }
+
+    /// Get raw value of the flags.
+    pub(crate) fn as_raw(self) -> usize {
+        self.0
+    }
+}
+
+/// Parameters for creating a GPU buddy allocator.
+#[derive(Clone, Copy)]
+pub struct GpuBuddyParams {
+    /// Base offset in bytes where the managed memory region starts.
+    /// Allocations will be offset by this value.
+    pub base_offset_bytes: u64,
+    /// Total physical memory size managed by the allocator in bytes.
+    pub physical_memory_size_bytes: u64,
+    /// Minimum allocation unit / chunk size in bytes, must be >= 4KB.
+    pub chunk_size_bytes: u64,
+}
+
+/// Parameters for allocating blocks from a GPU buddy allocator.
+#[derive(Clone, Copy)]
+pub struct GpuBuddyAllocParams {
+    /// Start of allocation range in bytes. Use 0 for beginning.
+    pub start_range_address: u64,

rust/kernel/gpu/buddy.rs line 233

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+    /// End of allocation range in bytes. Use 0 for entire range.
+    pub end_range_address: u64,
+    /// Total size to allocate in bytes.
+    pub size_bytes: u64,
+    /// Minimum block size for fragmented allocations in bytes.
+    pub min_block_size_bytes: u64,
+    /// Buddy allocator behavior flags.
+    pub buddy_flags: BuddyFlags,
+}
+

(not a full review)

Any reason these two need Clone, Copy? I'm not seeing a use case for that, maybe we should pass them as non-mutable references, but I don't think there is any point in passing them by value ever.

Dave.

author reviewer
#16 lore raw

Re: [PATCH RFC v6 03/26] rust: gpu: Add GPU buddy allocator bindings

Quoted 80 lines · Dave Airlie
Quoted 67 lines
rust/kernel/gpu/buddy.rs new file +67 -0
+///
+/// These flags control the allocation behavior of the buddy allocator.
+#[derive(Clone, Copy, Default, PartialEq, Eq)]
+pub struct BuddyFlags(usize);
+
+impl BuddyFlags {
+    /// Range-based allocation from start to end addresses.
+    pub const RANGE_ALLOCATION: usize = bindings::GPU_BUDDY_RANGE_ALLOCATION;
+
+    /// Allocate from top of address space downward.
+    pub const TOPDOWN_ALLOCATION: usize = bindings::GPU_BUDDY_TOPDOWN_ALLOCATION;
+
+    /// Allocate physically contiguous blocks.
+    pub const CONTIGUOUS_ALLOCATION: usize = bindings::GPU_BUDDY_CONTIGUOUS_ALLOCATION;
+
+    /// Request allocation from the cleared (zeroed) memory. The zero'ing is not
+    /// done by the allocator, but by the caller before freeing old blocks.
+    pub const CLEAR_ALLOCATION: usize = bindings::GPU_BUDDY_CLEAR_ALLOCATION;
+
+    /// Disable trimming of partially used blocks.
+    pub const TRIM_DISABLE: usize = bindings::GPU_BUDDY_TRIM_DISABLE;
+
+    /// Mark blocks as cleared (zeroed) when freeing. When set during free,
+    /// indicates that the caller has already zeroed the memory.
+    pub const CLEARED: usize = bindings::GPU_BUDDY_CLEARED;
+
+    /// Create [`BuddyFlags`] from a raw value with validation.
+    ///
+    /// Use `|` operator to combine flags if needed, before calling this method.
+    pub fn try_new(flags: usize) -> Result<Self> {
+        // Flags must not exceed u32::MAX to satisfy the GPU buddy allocator C API.
+        if flags > u32::MAX as usize {
+            return Err(EINVAL);
+        }
+
+        // `TOPDOWN_ALLOCATION` only works without `RANGE_ALLOCATION`. When both are
+        // set, `TOPDOWN_ALLOCATION` is silently ignored by the allocator. Reject this.
+        if (flags & Self::RANGE_ALLOCATION) != 0 && (flags & Self::TOPDOWN_ALLOCATION) != 0 {
+            return Err(EINVAL);
+        }
+
+        Ok(Self(flags))
+    }
+
+    /// Get raw value of the flags.
+    pub(crate) fn as_raw(self) -> usize {
+        self.0
+    }
+}
+
+/// Parameters for creating a GPU buddy allocator.
+#[derive(Clone, Copy)]
+pub struct GpuBuddyParams {
+    /// Base offset in bytes where the managed memory region starts.
+    /// Allocations will be offset by this value.
+    pub base_offset_bytes: u64,
+    /// Total physical memory size managed by the allocator in bytes.
+    pub physical_memory_size_bytes: u64,
+    /// Minimum allocation unit / chunk size in bytes, must be >= 4KB.
+    pub chunk_size_bytes: u64,
+}
+
+/// Parameters for allocating blocks from a GPU buddy allocator.
+#[derive(Clone, Copy)]
+pub struct GpuBuddyAllocParams {
+    /// Start of allocation range in bytes. Use 0 for beginning.
+    pub start_range_address: u64,

rust/kernel/gpu/buddy.rs line 233

Quoted 10 lines
rust/kernel/gpu/buddy.rs new file +10 -0
+    /// End of allocation range in bytes. Use 0 for entire range.
+    pub end_range_address: u64,
+    /// Total size to allocate in bytes.
+    pub size_bytes: u64,
+    /// Minimum block size for fragmented allocations in bytes.
+    pub min_block_size_bytes: u64,
+    /// Buddy allocator behavior flags.
+    pub buddy_flags: BuddyFlags,
+}
+

(not a full review)

Quoted 3 lines

Any reason these two need Clone, Copy? I'm not seeing a use case for that, maybe we should pass them as non-mutable references, but I don't think there is any point in passing them by value ever.

Yes, one reason I did that is because the doctests reuse the same params. But I could also just pass by reference as you suggest. It might remove some mem copies in the doctests. I will make this change then, thanks!

--
Joel Fernandes

author reviewer
#17 lore raw

RFC PATCH v6 4/26 nova-core: mm: Select GPU_BUDDY for VRAM allocation

In reply to Joel Fernandes

Select the GPU_BUDDY allocator config option, which provides the buddy allocator bindings needed for VRAM page allocation in nova-core's memory management subsystem.

---
 drivers/gpu/nova-core/Kconfig | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/nova-core/Kconfig +1 -0
index 527920f9c4d3..809485167aff 100644
@@ -5,6 +5,7 @@ config NOVA_CORE
 	depends on RUST
 	select RUST_FW_LOADER_ABSTRACTIONS
 	select AUXILIARY_BUS
+	select GPU_BUDDY
 	default n
 	help
 	  Choose this if you want to build the Nova Core driver for Nvidia
Signature · 1 line
2.34.1
author reviewer
#18 lore raw

RFC PATCH v6 5/26 nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Joel Fernandes

PRAMIN apertures are a crucial mechanism to direct read/write to VRAM. Add support for the same.

---
 drivers/gpu/nova-core/mm/mod.rs    |   5 +
 drivers/gpu/nova-core/mm/pramin.rs | 244 +++++++++++++++++++++++++++++
 drivers/gpu/nova-core/nova_core.rs |   1 +
 drivers/gpu/nova-core/regs.rs      |   5 +
 4 files changed, 255 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/mod.rs
 create mode 100644 drivers/gpu/nova-core/mm/pramin.rs
drivers/gpu/nova-core/mm/mod.rs new file +5 -0
new file mode 100644
index 000000000000..7a5dd4220c67
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Memory management subsystems for nova-core.
+
+pub(crate) mod pramin;
drivers/gpu/nova-core/mm/pramin.rs new file +244 -0

244 lines not on this page · raw message

drivers/gpu/nova-core/nova_core.rs +1 -0
index c1121e7c64c5..3de00db3279e 100644
@@ -13,6 +13,7 @@
 mod gfw;
 mod gpu;
 mod gsp;
+mod mm;
 mod num;
 mod regs;
 mod sbuffer;
drivers/gpu/nova-core/regs.rs +5 -0
index 82cc6c0790e5..c8b8fbdcf608 100644
@@ -96,6 +96,11 @@ fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result {
     31:16   frts_err_code as u16;
 });
 
+register!(NV_PBUS_BAR0_WINDOW @ 0x00001700, "BAR0 window control for PRAMIN access" {
+    25:24   target as u8, "Target memory (0=VRAM, 1=SYS_MEM_COH, 2=SYS_MEM_NONCOH)";
+    23:0    window_base as u32, "Window base address (bits 39:16 of FB addr)";
+});
+
 // PFB
 
 // The following two registers together hold the physical system memory address that is used by the
Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 3 lines · On Tue, 20 Jan 2026 15:42:42 -0500 Joel Fernandes <[email protected]> wrote:

PRAMIN apertures are a crucial mechanism to direct read/write to VRAM. Add support for the same.

I went through the code, this seems not designed for multiple users. As this is used for writting PTEs for page tables, can you shed some light about the plan of how we should handle the concurrency of writting multiple page table PTEs, e.g. when two GPU memory mapping in two different GPU page tables are procceding concurrently, this could happen when people creating vGPUs concurrently.

Z.

Quoted 311 lines
---
 drivers/gpu/nova-core/mm/mod.rs    |   5 +
 drivers/gpu/nova-core/mm/pramin.rs | 244 +++++++++++++++++++++++++++++
 drivers/gpu/nova-core/nova_core.rs |   1 +
 drivers/gpu/nova-core/regs.rs      |   5 +
 4 files changed, 255 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/mod.rs
 create mode 100644 drivers/gpu/nova-core/mm/pramin.rs
(quoted diff) +0 -0

b/drivers/gpu/nova-core/mm/mod.rs new file mode 100644 index 000000000000..7a5dd4220c67

drivers/gpu/nova-core/mm/mod.rs new file +5 -0
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Memory management subsystems for nova-core.
+
+pub(crate) mod pramin;
(quoted diff) +0 -0

b/drivers/gpu/nova-core/mm/pramin.rs new file mode 100644 index 000000000000..6a7ea2dc7d77

drivers/gpu/nova-core/mm/pramin.rs new file +5 -0
@@ -0,0 +1,244 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Direct VRAM access through the PRAMIN aperture.
+//!
+//! PRAMIN provides a 1MB sliding window into VRAM through BAR0,

allowing the CPU to access +//! video memory directly. The [Window] type automatically repositions the window when +//! accessing different VRAM regions and restores the original position on drop. This allows

drivers/gpu/nova-core/mm/pramin.rs new file +2 -0
+//! to reuse the same window for multiple accesses in the same window.
+//! +//! The PRAMIN aperture is a 1MB region at BAR0 + 0x700000 for all

GPUs. The window base is +//! controlled by the NV_PBUS_BAR0_WINDOW register and must be 64KB aligned. +//!

drivers/gpu/nova-core/mm/pramin.rs new file +80 -0
+//! # Examples
+//!
+//! ## Basic read/write
+//!
+//! ```no_run
+//! use crate::driver::Bar0;
+//! use crate::mm::pramin;
+//! use kernel::devres::Devres;
+//! use kernel::sync::Arc;
+//!
+//! fn example(devres_bar: Arc<Devres<Bar0>>) -> Result<()> {
+//!     let mut pram_win = pramin::Window::new(devres_bar)?;
+//!
+//!     // Write and read back.
+//!     pram_win.try_write32(0x100, 0xDEADBEEF)?;
+//!     let val = pram_win.try_read32(0x100)?;
+//!     assert_eq!(val, 0xDEADBEEF);
+//!
+//!     Ok(())
+//!     // Original window position restored on drop.
+//! }
+//! ```
+//!
+//! ## Auto-repositioning across VRAM regions
+//!
+//! ```no_run
+//! use crate::driver::Bar0;
+//! use crate::mm::pramin;
+//! use kernel::devres::Devres;
+//! use kernel::sync::Arc;
+//!
+//! fn example(devres_bar: Arc<Devres<Bar0>>) -> Result<()> {
+//!     let mut pram_win = pramin::Window::new(devres_bar)?;
+//!
+//!     // Access first 1MB region.
+//!     pram_win.try_write32(0x100, 0x11111111)?;
+//!
+//!     // Access at 2MB - window auto-repositions.
+//!     pram_win.try_write32(0x200000, 0x22222222)?;
+//!
+//!     // Back to first region - window repositions again.
+//!     let val = pram_win.try_read32(0x100)?;
+//!     assert_eq!(val, 0x11111111);
+//!
+//!     Ok(())
+//! }
+//! ```
+
+#![allow(unused)]
+
+use crate::{
+    driver::Bar0,
+    regs, //
+};
+
+use kernel::bits::genmask_u64;
+use kernel::devres::Devres;
+use kernel::prelude::*;
+use kernel::ptr::{
+    Alignable,
+    Alignment, //
+};
+use kernel::sizes::{
+    SZ_1M,
+    SZ_64K, //
+};
+use kernel::sync::Arc;
+
+/// PRAMIN aperture base offset in BAR0.
+const PRAMIN_BASE: usize = 0x700000;
+
+/// PRAMIN aperture size (1MB).
+const PRAMIN_SIZE: usize = SZ_1M;
+
+/// 64KB alignment for window base.
+const WINDOW_ALIGN: Alignment = Alignment::new::<SZ_64K>();
+
+/// Maximum addressable VRAM offset (40-bit address space).
+///
+/// The `NV_PBUS_BAR0_WINDOW` register has a 24-bit `window_base` field

(bits 23:0) that stores +/// bits [39:16] of the target VRAM address. This limits the addressable space to 2^40 bytes. +///

drivers/gpu/nova-core/mm/pramin.rs new file +7 -0
+/// CAST: On 64-bit systems, this fits in usize.
+const MAX_VRAM_OFFSET: usize = genmask_u64(0..=39) as usize;
+
+/// Generate a PRAMIN read accessor.
+macro_rules! define_pramin_read {
+    ($name:ident, $ty:ty) => {
+        #[doc = concat!("Read a `", stringify!($ty), "` from VRAM at

the given offset.")]
+ pub(crate) fn $name(&mut self, vram_offset: usize) ->
Result<$ty> {

drivers/gpu/nova-core/mm/pramin.rs new file +3 -0
+            // Compute window parameters without bar reference.
+            let (bar_offset, new_base) =
+                self.compute_window(vram_offset,

::core::mem::size_of::<$ty>())?; +

drivers/gpu/nova-core/mm/pramin.rs new file +15 -0
+            // Update window base if needed and perform read.
+            let bar = self.bar.try_access().ok_or(ENODEV)?;
+            if let Some(base) = new_base {
+                Self::write_window_base(&bar, base);
+                self.current_base = base;
+            }
+            bar.$name(bar_offset)
+        }
+    };
+}
+
+/// Generate a PRAMIN write accessor.
+macro_rules! define_pramin_write {
+    ($name:ident, $ty:ty) => {
+        #[doc = concat!("Write a `", stringify!($ty), "` to VRAM at the

given offset.")]

drivers/gpu/nova-core/mm/pramin.rs new file +4 -1
+        pub(crate) fn $name(&mut self, vram_offset: usize, value: $ty)
-> Result {
+            // Compute window parameters without bar reference.
+            let (bar_offset, new_base) =
+                self.compute_window(vram_offset,

::core::mem::size_of::<$ty>())?; +

drivers/gpu/nova-core/mm/pramin.rs new file +14 -0
+            // Update window base if needed and perform write.
+            let bar = self.bar.try_access().ok_or(ENODEV)?;
+            if let Some(base) = new_base {
+                Self::write_window_base(&bar, base);
+                self.current_base = base;
+            }
+            bar.$name(value, bar_offset)
+        }
+    };
+}
+
+/// PRAMIN window for direct VRAM access.
+///
+/// The window auto-repositions when accessing VRAM offsets outside the

current 1MB range. +/// Original window position is saved on creation and restored on drop. +pub(crate) struct Window {

drivers/gpu/nova-core/mm/pramin.rs new file +32 -0
+    bar: Arc<Devres<Bar0>>,
+    saved_base: usize,
+    current_base: usize,
+}
+
+impl Window {
+    /// Create a new PRAMIN window accessor.
+    ///
+    /// Saves the current window position for restoration on drop.
+    pub(crate) fn new(bar: Arc<Devres<Bar0>>) -> Result<Self> {
+        let bar_access = bar.try_access().ok_or(ENODEV)?;
+        let saved_base = Self::try_read_window_base(&bar_access)?;
+
+        Ok(Self {
+            bar,
+            saved_base,
+            current_base: saved_base,
+        })
+    }
+
+    /// Read the current window base from the BAR0_WINDOW register.
+    fn try_read_window_base(bar: &Bar0) -> Result<usize> {
+        let reg = regs::NV_PBUS_BAR0_WINDOW::read(bar);
+        let base = u64::from(reg.window_base());
+        let shifted = base.checked_shl(16).ok_or(EOVERFLOW)?;
+        shifted.try_into().map_err(|_| EOVERFLOW)
+    }
+
+    /// Write a new window base to the BAR0_WINDOW register.
+    fn write_window_base(bar: &Bar0, base: usize) {
+        // CAST:
+        // - We have guaranteed that the base is within the addressable

range (40-bits).
+ // - After >> 16, a 40-bit aligned base becomes 24 bits, which
fits in u32.

drivers/gpu/nova-core/mm/pramin.rs new file +10 -0
+        regs::NV_PBUS_BAR0_WINDOW::default()
+            .set_window_base((base >> 16) as u32)
+            .write(bar);
+    }
+
+    /// Compute window parameters for a VRAM access.
+    ///
+    /// Returns (bar_offset, new_base) where:
+    /// - bar_offset: The BAR0 offset to use for the access
+    /// - new_base: Some(base) if window needs repositioning, None

otherwise

drivers/gpu/nova-core/mm/pramin.rs new file +6 -0
+    fn compute_window(
+        &self,
+        vram_offset: usize,
+        access_size: usize,
+    ) -> Result<(usize, Option<usize>)> {
+        // Validate VRAM offset is within addressable range (40-bit

address space).
+ let end_offset =
vram_offset.checked_add(access_size).ok_or(EINVAL)?;

drivers/gpu/nova-core/mm/pramin.rs new file +52 -0
+        if end_offset > MAX_VRAM_OFFSET + 1 {
+            return Err(EINVAL);
+        }
+
+        // Calculate which 64KB-aligned base we need.
+        let needed_base = vram_offset.align_down(WINDOW_ALIGN);
+
+        // Calculate offset within the window.
+        let offset_in_window = vram_offset - needed_base;
+
+        // Check if access fits in 1MB window from this base.
+        if offset_in_window + access_size > PRAMIN_SIZE {
+            return Err(EINVAL);
+        }
+
+        // Return bar offset and whether window needs repositioning.
+        let new_base = if self.current_base != needed_base {
+            Some(needed_base)
+        } else {
+            None
+        };
+
+        Ok((PRAMIN_BASE + offset_in_window, new_base))
+    }
+
+    define_pramin_read!(try_read8, u8);
+    define_pramin_read!(try_read16, u16);
+    define_pramin_read!(try_read32, u32);
+    define_pramin_read!(try_read64, u64);
+
+    define_pramin_write!(try_write8, u8);
+    define_pramin_write!(try_write16, u16);
+    define_pramin_write!(try_write32, u32);
+    define_pramin_write!(try_write64, u64);
+}
+
+impl Drop for Window {
+    fn drop(&mut self) {
+        // Restore the original window base if it changed.
+        if self.current_base != self.saved_base {
+            if let Some(bar) = self.bar.try_access() {
+                Self::write_window_base(&bar, self.saved_base);
+            }
+        }
+    }
+}
+
+// SAFETY: `Window` requires `&mut self` for all accessors.
+unsafe impl Send for Window {}
+
+// SAFETY: `Window` requires `&mut self` for all accessors.
+unsafe impl Sync for Window {}
(quoted diff) +0 -0

b/drivers/gpu/nova-core/nova_core.rs index c1121e7c64c5..3de00db3279e 100644 --- a/drivers/gpu/nova-core/nova_core.rs
+++ b/drivers/gpu/nova-core/nova_core.rs

drivers/gpu/nova-core/nova_core.rs +1 -0
@@ -13,6 +13,7 @@
 mod gfw;
 mod gpu;
 mod gsp;
+mod mm;
 mod num;
 mod regs;
 mod sbuffer;
(quoted diff) +0 -0

b/drivers/gpu/nova-core/regs.rs index 82cc6c0790e5..c8b8fbdcf608 100644

drivers/gpu/nova-core/regs.rs +4 -0
@@ -96,6 +96,11 @@ fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) ->
 kernel::fmt::Result { 31:16   frts_err_code as u16;
 });
 
+register!(NV_PBUS_BAR0_WINDOW @ 0x00001700, "BAR0 window control for
 PRAMIN access" {
+    25:24   target as u8, "Target memory (0=VRAM, 1=SYS_MEM_COH,
 2=SYS_MEM_NONCOH)";
+    23:0    window_base as u32, "Window base address (bits 39:16 of FB
 addr)"; +});
+
 // PFB
 
 // The following two registers together hold the physical system memory

address that is used by the

author reviewer
#20 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Hello, Zhi,

Quoted 7 lines · Zhi Wang
Quoted 3 lines · On Tue, 20 Jan 2026 15:42:42 -0500 Joel Fernandes <[email protected]> wrote:

PRAMIN apertures are a crucial mechanism to direct read/write to VRAM. Add support for the same.

Quoted 6 lines

I went through the code, this seems not designed for multiple users. As this is used for writting PTEs for page tables, can you shed some light about the plan of how we should handle the concurrency of writting multiple page table PTEs, e.g. when two GPU memory mapping in two different GPU page tables are procceding concurrently, this could happen when people creating vGPUs concurrently.

Good question. Currently, BarUser::map() requires a mutable reference to both the BarUser and the GpuMm.

pub(crate) fn map<'a>(
&'a mut self,
mm: &'a mut GpuMm,

GpuMm is owned by the struct Gpu, so from a Rust standpoint, it is already handled since it is not possible to manipulate the Page table hierarchy (Page directories and last level Page table).

But yes, we have to look into concurrency once we have channels, and users other than bar where have multiple users of the same address space doing mapping/unmapping.

I think we can incrementally build on this series to add support for the same, it is not something this series directly addresses since I have spend majority of my time last several months making translation *work* which is itself no east task. This series is just preliminary based on work from last several months and to make BAR1 work. For instance, I kept PRAMIN simple based on feedback that we don't want to over complicate without fully understanding all the requirements. There is also additional requirements for locking design that have implications with DMA fencing etc, for instance.

Anyway thinking out loud, I am thinking for handling concurrency at the page table entry level (if we ever need it), we could use per-PT spinlocks similar to the Linux kernel. But lets plan on how to do this properly and based on actual requirements.

Signature · 1 line
Joel Fernandes
author reviewer
#21 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 9 lines · Joel Fernandes

I think we can incrementally build on this series to add support for the same, it is not something this series directly addresses since I have spend majority of my time last several months making translation *work* which is itself no east task. This series is just preliminary based on work from last several months and to make BAR1 work. For instance, I kept PRAMIN simple based on feedback that we don't want to over complicate without fully understanding all the requirements. There is also additional requirements for locking design that have implications with DMA fencing etc, for instance.

Quoted 4 lines

Anyway thinking out loud, I am thinking for handling concurrency at the page table entry level (if we ever need it), we could use per-PT spinlocks similar to the Linux kernel. But lets plan on how to do this properly and based on actual requirements.

Thanks for the discussion on concurrency, Zhi.

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

No atomic compare-and-swap on VRAM is needed because the PRAMIN lock serializes access. Please let me know if you had a different scenario in mind, but I think this covers it.

Zhi, feel free to use v6 though for any testing you are doing while I rework the locking.

Signature · 1 line
Joel Fernandes
#22 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 38 lines · On Thu, 22 Jan 2026 18:16:00 -0500 Joel Fernandes <[email protected]> wrote:
Quoted 10 lines · Joel Fernandes

I think we can incrementally build on this series to add support for the same, it is not something this series directly addresses since I have spend majority of my time last several months making translation *work* which is itself no east task. This series is just preliminary based on work from last several months and to make BAR1 work. For instance, I kept PRAMIN simple based on feedback that we don't want to over complicate without fully understanding all the requirements. There is also additional requirements for locking design that have implications with DMA fencing etc, for instance.

Quoted 4 lines

Anyway thinking out loud, I am thinking for handling concurrency at the page table entry level (if we ever need it), we could use per-PT spinlocks similar to the Linux kernel. But lets plan on how to do this properly and based on actual requirements.

Thanks for the discussion on concurrency, Zhi.

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

Quoted 7 lines

No atomic compare-and-swap on VRAM is needed because the PRAMIN lock serializes access. Please let me know if you had a different scenario in mind, but I think this covers it.

Zhi, feel free to use v6 though for any testing you are doing while I rework the locking.

Hi Joel:

Thanks so much for the work and the discussion. It is super important efforts for me to move on for the vGPU work. :)

As we discussed, the concurrency matters most when booting multiple vGPUs. At that time, the concurrency happens at:

1) Allocating GPU memory chunks
2) Reserving GPU channels
3) Mapping GPU memory to BAR1 page table

We basically need kinda protection there. E.g. Guard/Access on immutable references, which is backed by the mutex. I believe there shouldn't be a non-sleepible path reaching those. This should be fine.

I can see you are thinking of fine-granularity locking scheme, which I think is the right direction to go. I agreed with the above two locks.

For 1), I can recall that you mentioned there is some lock protection already there.

For 2), We can think of it when reaching there.

However for 3), We need to have one there as well beside the above two locks. Have you already had one in the GPU VA allocator?

If yes, the above two locks should be good enough so far. IMO.

Z.

author reviewer
#23 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 2 lines · Zhi Wang

Thanks so much for the work and the discussion. It is super important efforts for me to move on for the vGPU work. :)

Great!

Quoted 6 lines

As we discussed, the concurrency matters most when booting multiple vGPUs. At that time, the concurrency happens at:

1) Allocating GPU memory chunks
2) Reserving GPU channels
3) Mapping GPU memory to BAR1 page table

Yes all these are already covered from a concurrency PoV by the v6.

Quoted 2 lines

I can see you are thinking of fine-granularity locking scheme, which I think is the right direction to go. I agreed with the above two locks.

Cool!

Quoted 2 lines

However for 3), We need to have one there as well beside the above two locks. Have you already had one in the GPU VA allocator?

Currently for mapping Bar pages, you need a mutable reference to BarUser. For the future, when we have multiple channels sharing the same va space, it will still be protected because the va space allocator (virt_buddy) already has internal locking. And for each map_page, it is protected as well. So I believe that should also be covered. Thanks for checking.

Quoted 1 line

If yes, the above two locks should be good enough so far. IMO.

Ok, thanks for checking.

--
Joel Fernandes

maintainer
#24 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Joel Fernandes
Quoted 11 lines · Joel Fernandes

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Quoted 8 lines

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

This won't work unfortunately.

We have to separate allocations and modifications of the page tabe. Or in other words, we must not allocate new PDEs or PTEs while holding the lock protecting the page table from modifications.

Once we have VM_BIND in nova-drm, we will have the situation that userspace passes jobs to modify the GPUs virtual address space and hence the page tables.

Such a jobs has mainly three stages.

(1) The submit stage.

This is where the job is initialized, dependencies are set up and the driver has to pre-allocate all kinds of structures that are required throughout the subsequent stages of the job.

(2) The run stage.

This is the stage where the job is staged for execution and its DMA fence has been made public (i.e. it is accessible by userspace).

This is the stage where we are in the DMA fence signalling critical section, hence we can't do any non-atomic allocations, since otherwise we could deadlock in MMU notifier callbacks for instance.

This is the stage where the page table is actually modified. Hence, we can't acquire any locks that might be held elsewhere while doing non-atomic allocations. Also note that this is transitive, e.g. if you take lock A and somewhere else a lock B is taked while A is already held and we do non-atomic allocations while holding B, then A can't be held in the DMA fence signalling critical path either.

It is also worth noting that this is the stage where we know the exact operations we have to execute based on the VM_BIND request from userspace.

For instance, in the submit stage we may only know that userspace wants that we map a BO with a certain offset in the GPUs virtual address space at [0x0, 0x1000000]. What we don't know is what exact operations this does require, i.e. "What do we have to unmap first?", "Are there any overlapping mappings that we have to truncate?", etc.

So, we have to consider this when we pre-allocate in the submit stage.

(3) The cleanup stage.

This is where the job has been signaled and hence left the DMA fence signalling critical section.

In this stage the job is cleaned up, which includes freeing data that is not required anymore, such as PTEs and PDEs.

author reviewer
#25 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 21 lines · Danilo Krummrich
Quoted 11 lines · Joel Fernandes

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Quoted 8 lines

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

Quoted 5 lines

This won't work unfortunately.

We have to separate allocations and modifications of the page tabe. Or in other words, we must not allocate new PDEs or PTEs while holding the lock protecting the page table from modifications.

I will go over these concerns, just to clarify - do you mean forbidding *any* lock or do you mean only forbidding non-atomic locks? I believe we can avoid non-atomic locks completely - actually I just wrote a patch before I read this email to do just. If we are to forbid any locking at all, that might require some careful redesign to handle the above race afaics.

Quoted 3 lines

Once we have VM_BIND in nova-drm, we will have the situation that userspace passes jobs to modify the GPUs virtual address space and hence the page tables.

Thanks for listing all the concerns below, this is very valuable. I will go over all these and all cases before posting the v7 now that I have this.

--
Joel Fernandes

Quoted 42 lines

Such a jobs has mainly three stages.

(1) The submit stage.

This is where the job is initialized, dependencies are set up and the driver has to pre-allocate all kinds of structures that are required throughout the subsequent stages of the job.

(2) The run stage.

This is the stage where the job is staged for execution and its DMA fence has been made public (i.e. it is accessible by userspace).

This is the stage where we are in the DMA fence signalling critical section, hence we can't do any non-atomic allocations, since otherwise we could deadlock in MMU notifier callbacks for instance.

This is the stage where the page table is actually modified. Hence, we can't acquire any locks that might be held elsewhere while doing non-atomic allocations. Also note that this is transitive, e.g. if you take lock A and somewhere else a lock B is taked while A is already held and we do non-atomic allocations while holding B, then A can't be held in the DMA fence signalling critical path either.

It is also worth noting that this is the stage where we know the exact operations we have to execute based on the VM_BIND request from userspace.

For instance, in the submit stage we may only know that userspace wants that we map a BO with a certain offset in the GPUs virtual address space at [0x0, 0x1000000]. What we don't know is what exact operations this does require, i.e. "What do we have to unmap first?", "Are there any overlapping mappings that we have to truncate?", etc.

So, we have to consider this when we pre-allocate in the submit stage.

(3) The cleanup stage.

This is where the job has been signaled and hence left the DMA fence signalling critical section.

In this stage the job is cleaned up, which includes freeing data that is not required anymore, such as PTEs and PDEs.

maintainer
#26 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 6 lines · Joel Fernandes

I will go over these concerns, just to clarify - do you mean forbidding *any* lock or do you mean only forbidding non-atomic locks? I believe we can avoid non-atomic locks completely - actually I just wrote a patch before I read this email to do just. If we are to forbid any locking at all, that might require some careful redesign to handle the above race afaics.

It's not about the locks themselves, sleeping locks are fine too. It's about holding locks that are held elsewhere when doing memory allocations that can call back into MMU notifiers or the shrinker.

I.e. if in the fence signalling critical path you wait for a mutex that is held elsewhere while allocating memory and the memory allocation calls back into the shrinker, you may end up waiting for your own DMA fence to be signaled, which causes a deadlock.

#27 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 11 lines · Danilo Krummrich
Quoted 6 lines · Joel Fernandes

I will go over these concerns, just to clarify - do you mean forbidding *any* lock or do you mean only forbidding non-atomic locks? I believe we can avoid non-atomic locks completely - actually I just wrote a patch before I read this email to do just. If we are to forbid any locking at all, that might require some careful redesign to handle the above race afaics.

It's not about the locks themselves, sleeping locks are fine too. It's about holding locks that are held elsewhere when doing memory allocations that can call back into MMU notifiers or the shrinker.

If you look at core kernel mm, you'll find a similar constraint: avoid holding any locks while allocating--unless you are in the reclaim code itself.

Especially when dealing with page tables.

So this is looking familiar to me and I agree with the constraint, fwiw.

Quoted 5 lines

I.e. if in the fence signalling critical path you wait for a mutex that is held elsewhere while allocating memory and the memory allocation calls back into the shrinker, you may end up waiting for your own DMA fence to be signaled, which causes a deadlock.

Right, and the list of pitfalls such as this is basically limited only by your imagination--it's long. :)

thanks,

Signature · 1 line
John Hubbard
author reviewer
#28 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 13 lines · John Hubbard
Quoted 11 lines · Danilo Krummrich
Quoted 6 lines · Joel Fernandes

I will go over these concerns, just to clarify - do you mean forbidding *any* lock or do you mean only forbidding non-atomic locks? I believe we can avoid non-atomic locks completely - actually I just wrote a patch before I read this email to do just. If we are to forbid any locking at all, that might require some careful redesign to handle the above race afaics.

It's not about the locks themselves, sleeping locks are fine too. It's about holding locks that are held elsewhere when doing memory allocations that can call back into MMU notifiers or the shrinker.

Quoted 7 lines

If you look at core kernel mm, you'll find a similar constraint: avoid holding any locks while allocating--unless you are in the reclaim code itself.

Especially when dealing with page tables.

So this is looking familiar to me and I agree with the constraint, fwiw.

Right, so similar to core kernel mm, we need to separate PT allocation from the lock needed for PT writing. Essentially never allocating PT pages in the dma-fence critical paths. We already have separate locks for both these (buddy versus vmm), so it should be doable with some adjustments. I will study the dma-fence further and revise patches. Thanks.
--
Joel Fernandes

author reviewer
#29 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Danilo Krummrich

Hi Danilo,

Quoted 9 lines · Danilo Krummrich
Quoted 6 lines · Joel Fernandes

I will go over these concerns, just to clarify - do you mean forbidding *any* lock or do you mean only forbidding non-atomic locks? I believe we can avoid non-atomic locks completely - actually I just wrote a patch before I read this email to do just. If we are to forbid any locking at all, that might require some careful redesign to handle the above race afaics.

It's not about the locks themselves, sleeping locks are fine too.

Ah, so in your last email when you meant "non-atomic", you mean an allocation that cause memory reclamation etc, right? I got confused by "non-atomic" because I thought you were referring to acquiring a sleeping lock in a non-atomic context (I also work on CPU scheduling/RCU, so the word atomic sometimes means different things to me - my fault not yours :P).

I believe we may have to use "try lock" on a mutex if have to use these in the future, in a path that cannot wait (such as a page fault handler), but yes I agree with you we can use mutexes for these, with a combination of try_lock + bottom half deferrals. additional comment [1].

Coming to the dma-fence deadlocks you mention, this sounds very similar to my experiences with reclaim-deadlocks when I worked on the Ashmem Android driver. Deja-vu :-D. The issue there was the memory shrinker would take a lock in the ashmem driver during reclaim, which is a disaster if the lock was already held and a memory allocation request triggered reclaim. I believe the DMA fence usecase is also similar based on your description.

It's about

Quoted 7 lines

holding locks that are held elsewhere when doing memory allocations that can call back into MMU notifiers or the shrinker.

I.e. if in the fence signalling critical path you wait for a mutex that is held elsewhere while allocating memory and the memory allocation calls back into the shrinker, you may end up waiting for your own DMA fence to be signaled, which causes a deadlock.

Got it, I will send the next day or so studying the DMA fence architecture but I mostly got the idea now. We need to be careful with reclaim locking as you stressed. I will analyze all the requirements to properly address this. I will reach out if I have any questions. Thanks for sharing your knowledge in this!

--
Joel Fernandes

[1]
I can confirm for completeness, that both Nouveau and OpenRM use mutexes for PT/VMM related locking. In interrupt contexts, OpenRM does a "try lock" AFAICS on its mutex. This is similar to how Linux kernel mm page fault handling acquires mmap_sem (via try-locking).
The linux kernel does have per-PT spinlocks to handle the "2 paths try to install a PDE/PTE race", but I don't think we need that at the moment for our usecases as we can keep it simple and rely on the VMM mutex, we can perhaps add that in later if needed (or use more finer grained block-level locking), but let me know if anyone disagrees with that.

author reviewer
#30 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Danilo Krummrich

Hi, Danilo, all,

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. Please let me know if I missed something below. At the moment, off the top I identified that we are doing GFP_KERNEL memory allocations inside GPU buddy allocator during map/unmap. I will work on solutions for that. Thanks.

All deadlock scenarios
----------------------
The gist is, in the DMA fence signaling critical path we cannot acquire resources (locks or memory allocation etc) that are already acquired when a fence is being waited on to be signaled. So we have to careful which resources we acquire, and also we need to be careful which paths in the driver we do any memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

1. deadlock scenario 1: allocator deadlock (no locking needed to trigger it)

Fence Signal start (A) -> Alloc -> MMU notifier/Shrinker (B) -> Fence Wait (A)

ABA deadlock.

2. deadlock scenario 2: Same as 1, but ABBA scenario (2 CPUs).

CPU 0: Fence Signal start (A) -> Alloc (B)

CPU 1: Alloc -> MMU notifier or Shrinker (B) -> Fence Wait (A)

3. deadlock scenario 3: When locking: ABBA (and similarly) deadlock but locking.

CPU 0: Fence Signal start (A) -> Lock (B)

CPU 1: Lock (B) -> Fence Wait (A)

4. deadlock scenario 4: Same as scenario 3, but the fence wait comes from allocation path.

rule: We cannot try to acquire locks in the DMA fence signaling critical path if those locks were already acquire in paths that do reclaimable memory allocations.

CPU 0: Fence Signal (A) -> Lock (B)

CPU 1: Lock (B) -> Alloc -> Fence Wait (A)

5. deadlock scenario 5: Transitive locking:

rule: We cannot try to acquire locks in the DMA fence signaling critical path that are transitively waiting on the same DMA fence.

Fence Signal (A) -> Lock (B)

Lock (B) -> Lock(C)

Lock (C) -> Alloc -> Fence Wait (A)

ABBCCA deadlock.

--
Joel Fernandes

Quoted 72 lines · Danilo Krummrich
Quoted 11 lines · Joel Fernandes

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Quoted 8 lines

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

This won't work unfortunately.

We have to separate allocations and modifications of the page tabe. Or in other words, we must not allocate new PDEs or PTEs while holding the lock protecting the page table from modifications.

Once we have VM_BIND in nova-drm, we will have the situation that userspace passes jobs to modify the GPUs virtual address space and hence the page tables.

Such a jobs has mainly three stages.

(1) The submit stage.

This is where the job is initialized, dependencies are set up and the driver has to pre-allocate all kinds of structures that are required throughout the subsequent stages of the job.

(2) The run stage.

This is the stage where the job is staged for execution and its DMA fence has been made public (i.e. it is accessible by userspace).

This is the stage where we are in the DMA fence signalling critical section, hence we can't do any non-atomic allocations, since otherwise we could deadlock in MMU notifier callbacks for instance.

This is the stage where the page table is actually modified. Hence, we can't acquire any locks that might be held elsewhere while doing non-atomic allocations. Also note that this is transitive, e.g. if you take lock A and somewhere else a lock B is taked while A is already held and we do non-atomic allocations while holding B, then A can't be held in the DMA fence signalling critical path either.

It is also worth noting that this is the stage where we know the exact operations we have to execute based on the VM_BIND request from userspace.

For instance, in the submit stage we may only know that userspace wants that we map a BO with a certain offset in the GPUs virtual address space at [0x0, 0x1000000]. What we don't know is what exact operations this does require, i.e. "What do we have to unmap first?", "Are there any overlapping mappings that we have to truncate?", etc.

So, we have to consider this when we pre-allocate in the submit stage.

(3) The cleanup stage.

This is where the job has been signaled and hence left the DMA fence signalling critical section.

In this stage the job is cleaned up, which includes freeing data that is not required anymore, such as PTEs and PDEs.

Signature · 1 line
Joel Fernandes
#31 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 8 lines · Joel Fernandes

Hi, Danilo, all,

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. Please let me know if I missed something below. At the moment, off the top I identified that we are doing GFP_KERNEL memory allocations inside GPU buddy allocator during map/unmap. I will work on solutions for that. Thanks.

Quoted 8 lines

All deadlock scenarios
----------------------
The gist is, in the DMA fence signaling critical path we cannot acquire resources (locks or memory allocation etc) that are already acquired when a fence is being waited on to be signaled. So we have to careful which resources we acquire, and also we need to be careful which paths in the driver we do any memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

thanks,

Signature · 1 line
John Hubbard
author reviewer
#32 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 9 lines · John Hubbard
Quoted 5 lines · Joel Fernandes

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. [...]
memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

Not allocations but we are acquiring locks during unmap. My understanding is (at least some) unmaps have to also be done in the dma fence signaling critical path (the run stage), but Danilo/you can correct me if I am wrong on that. We cannot avoid all locking but those same locks cannot be held in any other paths which do a memory allocation (as mentioned in one of the deadlock scenarios), that is probably the main thing to check for unmap.

Thanks,

Signature · 1 line
Joel Fernandes
#33 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 11 lines · Joel Fernandes
Quoted 9 lines · John Hubbard
Quoted 5 lines · Joel Fernandes

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. [...]
memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

Quoted 7 lines

Not allocations but we are acquiring locks during unmap. My understanding is (at least some) unmaps have to also be done in the dma fence signaling critical path (the run stage), but Danilo/you can correct me if I am wrong on that. We cannot avoid all locking but those same locks cannot be held in any other paths which do a memory allocation (as mentioned in one of the deadlock scenarios), that is probably the main thing to check for unmap.

Right, OK we are on the same page now: no allocations happening on unmap, but it can still deadlock, because the driver is typically going to use a single lock to protect calls both map and unmap-related calls to the buddy allocator.

For the deadlock above, I think a good way to break that deadlock is to not allow taking that lock in a fence signaling calling path.

So during an unmap, instead of "lock, unmap/free, unlock" it should move the item to a deferred-free list, which is processed separately. Of course, this is a little complex, because the allocation and reclaim has to be aware of such lists if they get large.

thanks,

Signature · 1 line
John Hubbard
author reviewer
#34 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 20 lines · John Hubbard
Quoted 11 lines · Joel Fernandes
Quoted 9 lines · John Hubbard
Quoted 5 lines · Joel Fernandes

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. [...]
memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

Quoted 7 lines

Not allocations but we are acquiring locks during unmap. My understanding is (at least some) unmaps have to also be done in the dma fence signaling critical path (the run stage), but Danilo/you can correct me if I am wrong on that. We cannot avoid all locking but those same locks cannot be held in any other paths which do a memory allocation (as mentioned in one of the deadlock scenarios), that is probably the main thing to check for unmap.

Quoted 4 lines

Right, OK we are on the same page now: no allocations happening on unmap, but it can still deadlock, because the driver is typically going to use a single lock to protect calls both map and unmap-related calls to the buddy allocator.

Yes exactly!

Quoted 8 lines

For the deadlock above, I think a good way to break that deadlock is to not allow taking that lock in a fence signaling calling path.

So during an unmap, instead of "lock, unmap/free, unlock" it should move the item to a deferred-free list, which is processed separately. Of course, this is a little complex, because the allocation and reclaim has to be aware of such lists if they get large.

Yes, also avoiding GFP_KERNEL allocations while holding any of these mm locks (whichever we take during map). The GPU buddy actually does GFP_KERNEL allocations internally which is problematic.

Some solutions / next steps:

1. allocating (VRAM and system memory) outside mm locks just before acquiring them.

2. pre-allocating both VRAM and system memory needed, before the DMA fence critical paths (The issue is also to figure out how much memory to pre-allocate for the page table pages based on the VM_BIND request. I think we can analyze the page tables in the submit stage to make an estimate).

3. Unfortunately, I am using gpu-buddy when allocating a VA range in the Vmm (called virt_buddy), which itself does GFP_KERNEL memory allocations in the allocate path. I am not sure what do yet about this. ISTR the maple tree also has similar issues.

4. Using non-reclaimable memory allocations where pre-allocation or pre-allocated memory pools is not possible (I'd like to avoid this #4 so we don't fail allocations when memory is scarce).

Will work on these issues for the v7. Thanks,

--
Joel Fernandes

maintainer
#35 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 57 lines · Joel Fernandes
Quoted 20 lines · John Hubbard
Quoted 11 lines · Joel Fernandes
Quoted 9 lines · John Hubbard
Quoted 5 lines · Joel Fernandes

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. [...]
memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

Quoted 7 lines

Not allocations but we are acquiring locks during unmap. My understanding is (at least some) unmaps have to also be done in the dma fence signaling critical path (the run stage), but Danilo/you can correct me if I am wrong on that. We cannot avoid all locking but those same locks cannot be held in any other paths which do a memory allocation (as mentioned in one of the deadlock scenarios), that is probably the main thing to check for unmap.

Quoted 4 lines

Right, OK we are on the same page now: no allocations happening on unmap, but it can still deadlock, because the driver is typically going to use a single lock to protect calls both map and unmap-related calls to the buddy allocator.

Yes exactly!

Quoted 8 lines

For the deadlock above, I think a good way to break that deadlock is to not allow taking that lock in a fence signaling calling path.

So during an unmap, instead of "lock, unmap/free, unlock" it should move the item to a deferred-free list, which is processed separately. Of course, this is a little complex, because the allocation and reclaim has to be aware of such lists if they get large.

Yes, also avoiding GFP_KERNEL allocations while holding any of these mm locks (whichever we take during map). The GPU buddy actually does GFP_KERNEL allocations internally which is problematic.

Some solutions / next steps:

1. allocating (VRAM and system memory) outside mm locks just before acquiring them.

2. pre-allocating both VRAM and system memory needed, before the DMA fence critical paths (The issue is also to figure out how much memory to pre-allocate for the page table pages based on the VM_BIND request. I think we can analyze the page tables in the submit stage to make an estimate).

3. Unfortunately, I am using gpu-buddy when allocating a VA range in the Vmm (called virt_buddy), which itself does GFP_KERNEL memory allocations in the allocate path. I am not sure what do yet about this. ISTR the maple tree also has similar issues.

Quoted 5 lines

4. Using non-reclaimable memory allocations where pre-allocation or pre-allocated memory pools is not possible (I'd like to avoid this #4 so we don't fail allocations when memory is scarce).

Will work on these issues for the v7. Thanks,

The way this works on nouveau at least (and I haven't yet read the nova code in depth).

Is we have 4 stages of vmm page table mgmt.

ref - locked with a ref lock - can allocate/free memory - just makes sure the page tables exist and are reference counted
map - locked with a map lock - cannot allocate memory - fill in the PTEs in the page table
unmap - locked with a map lock - cannot allocate memory - removes entries in PTEs
unref - locked with a ref lock - can allocate/free memory - just drops references and frees (not sure if it ever merges).

So maps and unmaps can be in fence signalling paths, but unrefs are done in free job from a workqueue.

Dave.

Quoted 4 lines

--
Joel Fernandes

#36 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 22 lines · Dave Airlie
Quoted 6 lines · Joel Fernandes
Quoted 4 lines · John Hubbard
Quoted 3 lines · Joel Fernandes
Quoted 2 lines · John Hubbard
Quoted 1 line · Joel Fernandes

[...]

Will work on these issues for the v7. Thanks,

The way this works on nouveau at least (and I haven't yet read the nova code in depth).

Is we have 4 stages of vmm page table mgmt.

ref - locked with a ref lock - can allocate/free memory - just makes sure the page tables exist and are reference counted
map - locked with a map lock - cannot allocate memory - fill in the PTEs in the page table
unmap - locked with a map lock - cannot allocate memory - removes entries in PTEs
unref - locked with a ref lock - can allocate/free memory - just drops references and frees (not sure if it ever merges).

Quoted 3 lines

So maps and unmaps can be in fence signalling paths, but unrefs are done in free job from a workqueue.

Nice! Thanks Dave, I guess this is one time we really should have taken a peek at nouveau for inspiration after all. :)

thanks,

Signature · 1 line
John Hubbard
author reviewer
#37 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 27 lines · John Hubbard
Quoted 22 lines · Dave Airlie
Quoted 7 lines · Joel Fernandes
Quoted 4 lines · John Hubbard
Quoted 3 lines · Joel Fernandes
Quoted 2 lines · John Hubbard
Quoted 2 lines · Joel Fernandes
Quoted 1 line

[...]

Will work on these issues for the v7. Thanks,

The way this works on nouveau at least (and I haven't yet read the nova code in depth).

Is we have 4 stages of vmm page table mgmt.

ref - locked with a ref lock - can allocate/free memory - just makes sure the page tables exist and are reference counted
map - locked with a map lock - cannot allocate memory - fill in the PTEs in the page table
unmap - locked with a map lock - cannot allocate memory - removes entries in PTEs
unref - locked with a ref lock - can allocate/free memory - just drops references and frees (not sure if it ever merges).

Quoted 5 lines
Quoted 3 lines

So maps and unmaps can be in fence signalling paths, but unrefs are done in free job from a workqueue.

Nice! Thanks Dave

Indeed, thanks Dave and John.

Quoted 2 lines

, I guess this is one time we really should have taken a peek at nouveau for inspiration after all. :)

I have actually been referring to Nouveau, OpenRM and also the core kernel mm code for my research in this area. These all have been a great reference in this respect :). Thanks,

Joel Fernandes

Quoted 5 lines

thanks,
--
John Hubbard

author reviewer
#38 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Dave Airlie

Hi Dave,

Quoted 71 lines · Dave Airlie
Quoted 57 lines · Joel Fernandes
Quoted 24 lines · John Hubbard
Quoted 11 lines · Joel Fernandes
Quoted 9 lines · John Hubbard
Quoted 5 lines · Joel Fernandes

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. [...]
memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

Quoted 7 lines

Not allocations but we are acquiring locks during unmap. My understanding is (at least some) unmaps have to also be done in the dma fence signaling critical path (the run stage), but Danilo/you can correct me if I am wrong on that. We cannot avoid all locking but those same locks cannot be held in any other paths which do a memory allocation (as mentioned in one of the deadlock scenarios), that is probably the main thing to check for unmap.

Quoted 4 lines

Right, OK we are on the same page now: no allocations happening on unmap, but it can still deadlock, because the driver is typically going to use a single lock to protect calls both map and unmap-related calls to the buddy allocator.

Yes exactly!

Quoted 8 lines

For the deadlock above, I think a good way to break that deadlock is to not allow taking that lock in a fence signaling calling path.

So during an unmap, instead of "lock, unmap/free, unlock" it should move the item to a deferred-free list, which is processed separately. Of course, this is a little complex, because the allocation and reclaim has to be aware of such lists if they get large.

Yes, also avoiding GFP_KERNEL allocations while holding any of these mm locks (whichever we take during map). The GPU buddy actually does GFP_KERNEL allocations internally which is problematic.

Some solutions / next steps:

1. allocating (VRAM and system memory) outside mm locks just before acquiring them.

2. pre-allocating both VRAM and system memory needed, before the DMA fence critical paths (The issue is also to figure out how much memory to pre-allocate for the page table pages based on the VM_BIND request. I think we can analyze the page tables in the submit stage to make an estimate).

3. Unfortunately, I am using gpu-buddy when allocating a VA range in the Vmm (called virt_buddy), which itself does GFP_KERNEL memory allocations in the allocate path. I am not sure what do yet about this. ISTR the maple tree also has similar issues.

Quoted 5 lines

4. Using non-reclaimable memory allocations where pre-allocation or pre-allocated memory pools is not possible (I'd like to avoid this #4 so we don't fail allocations when memory is scarce).

Will work on these issues for the v7. Thanks,

The way this works on nouveau at least (and I haven't yet read the nova code in depth).

Is we have 4 stages of vmm page table mgmt.

Quoted 8 lines

ref - locked with a ref lock - can allocate/free memory - just makes sure the page tables exist and are reference counted
map - locked with a map lock - cannot allocate memory - fill in the PTEs in the page table
unmap - locked with a map lock - cannot allocate memory - removes entries in PTEs
unref - locked with a ref lock - can allocate/free memory - just drops references and frees (not sure if it ever merges).

Thanks for sharing this, yes this is similar to what I am coming up with.

One thing is OpenRM (and the Linux kernel) have finer grained locking.

But I think we can keep it simple initially like we do for Nouveau with additional complexity progressively added.

Joel Fernandes

Quoted 9 lines

So maps and unmaps can be in fence signalling paths, but unrefs are done in free job from a workqueue.

Dave.

Quoted 4 lines

--
Joel Fernandes

maintainer
#39 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Dave Airlie
Quoted 69 lines · Dave Airlie
Quoted 57 lines · Joel Fernandes
Quoted 20 lines · John Hubbard
Quoted 11 lines · Joel Fernandes
Quoted 9 lines · John Hubbard
Quoted 5 lines · Joel Fernandes

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. [...]
memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

unmap? Are you seeing any allocations happening during unmap? I don't immediately see any, but that sounds surprising.

Quoted 7 lines

Not allocations but we are acquiring locks during unmap. My understanding is (at least some) unmaps have to also be done in the dma fence signaling critical path (the run stage), but Danilo/you can correct me if I am wrong on that. We cannot avoid all locking but those same locks cannot be held in any other paths which do a memory allocation (as mentioned in one of the deadlock scenarios), that is probably the main thing to check for unmap.

Quoted 4 lines

Right, OK we are on the same page now: no allocations happening on unmap, but it can still deadlock, because the driver is typically going to use a single lock to protect calls both map and unmap-related calls to the buddy allocator.

Yes exactly!

Quoted 8 lines

For the deadlock above, I think a good way to break that deadlock is to not allow taking that lock in a fence signaling calling path.

So during an unmap, instead of "lock, unmap/free, unlock" it should move the item to a deferred-free list, which is processed separately. Of course, this is a little complex, because the allocation and reclaim has to be aware of such lists if they get large.

Yes, also avoiding GFP_KERNEL allocations while holding any of these mm locks (whichever we take during map). The GPU buddy actually does GFP_KERNEL allocations internally which is problematic.

Some solutions / next steps:

1. allocating (VRAM and system memory) outside mm locks just before acquiring them.

2. pre-allocating both VRAM and system memory needed, before the DMA fence critical paths (The issue is also to figure out how much memory to pre-allocate for the page table pages based on the VM_BIND request. I think we can analyze the page tables in the submit stage to make an estimate).

3. Unfortunately, I am using gpu-buddy when allocating a VA range in the Vmm (called virt_buddy), which itself does GFP_KERNEL memory allocations in the allocate path. I am not sure what do yet about this. ISTR the maple tree also has similar issues.

Quoted 5 lines

4. Using non-reclaimable memory allocations where pre-allocation or pre-allocated memory pools is not possible (I'd like to avoid this #4 so we don't fail allocations when memory is scarce).

Will work on these issues for the v7. Thanks,

The way this works on nouveau at least (and I haven't yet read the nova code in depth).

Is we have 4 stages of vmm page table mgmt.

Quoted 8 lines

ref - locked with a ref lock - can allocate/free memory - just makes sure the page tables exist and are reference counted
map - locked with a map lock - cannot allocate memory - fill in the PTEs in the page table
unmap - locked with a map lock - cannot allocate memory - removes entries in PTEs
unref - locked with a ref lock - can allocate/free memory - just drops references and frees (not sure if it ever merges).

On amdgpu VM page tables are allocated and PTEs filled outside of the fence critical path.

Only invalidating PTEs to signal that a shader needs to be taken off the HW are inside the fence critical path and here no memory allocation is needed.

Keep in mind that you not only need to avoid having memory allocations inside the critical path, but also not take locks under which memory is allocated.

Simona added some dma_fence_begin_signalling() and dma_fence_end_signalling() helpers to add lockdep annotations to the fence signaling path. Those have proven to be extremely useful since they allow lockdep to point out mistakes immediately and not just after hours of running on a test system.

Regards,
Christian.

Quoted 9 lines

So maps and unmaps can be in fence signalling paths, but unrefs are done in free job from a workqueue.

Dave.

Quoted 4 lines

--
Joel Fernandes

author reviewer
#40 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 3 lines · Christian König
Quoted 2 lines · Dave Airlie
Quoted 1 line · Joel Fernandes

On 1/29/2026 10:38 PM, John Hubbard wrote:

[...]

Quoted 45 lines
Quoted 36 lines
Quoted 25 lines
Quoted 7 lines

For the deadlock above, I think a good way to break that deadlock is to not allow taking that lock in a fence signaling calling path.

So during an unmap, instead of "lock, unmap/free, unlock" it should move the item to a deferred-free list, which is processed separately. Of course, this is a little complex, because the allocation and reclaim has to be aware of such lists if they get large.

Yes, also avoiding GFP_KERNEL allocations while holding any of these mm locks (whichever we take during map). The GPU buddy actually does GFP_KERNEL allocations internally which is problematic.

Some solutions / next steps:

1. allocating (VRAM and system memory) outside mm locks just before acquiring them.

2. pre-allocating both VRAM and system memory needed, before the DMA fence critical paths (The issue is also to figure out how much memory to pre-allocate for the page table pages based on the VM_BIND request. I think we can analyze the page tables in the submit stage to make an estimate).

3. Unfortunately, I am using gpu-buddy when allocating a VA range in the Vmm (called virt_buddy), which itself does GFP_KERNEL memory allocations in the allocate path. I am not sure what do yet about this. ISTR the maple tree also has similar issues.

Quoted 5 lines

4. Using non-reclaimable memory allocations where pre-allocation or pre-allocated memory pools is not possible (I'd like to avoid this #4 so we don't fail allocations when memory is scarce).

Will work on these issues for the v7. Thanks,

The way this works on nouveau at least (and I haven't yet read the nova code in depth).

Is we have 4 stages of vmm page table mgmt.

Quoted 8 lines

ref - locked with a ref lock - can allocate/free memory - just makes sure the page tables exist and are reference counted
map - locked with a map lock - cannot allocate memory - fill in the PTEs in the page table
unmap - locked with a map lock - cannot allocate memory - removes entries in PTEs
unref - locked with a ref lock - can allocate/free memory - just drops references and frees (not sure if it ever merges).

Quoted 1 line

On amdgpu VM page tables are allocated and PTEs filled outside of the fence critical path.

Does that really work for async VM_BIND? If we're missing anything in nova-core related to the timing of when the allocate and update of the page tables, it would be good to know.

My understanding you have to write the PTEs at the run stage of the job in question otherwise you may not know how to map? Are you saying amdgpu writes it during the run stage but somehow before fence signaling?

Quoted 4 lines

Only invalidating PTEs to signal that a shader needs to be taken off the HW are inside the fence critical path and here no memory allocation is needed.

Keep in mind that you not only need to avoid having memory allocations inside the critical path, but also not take locks under which memory is allocated.

Yes, this part I was clear from Danilo's email and clear about the various deadlocks. See my analysis where what you mention is in the cases I covered:
https://lore.kernel.org/all/[email protected]/

Quoted 2 lines

Simona added some dma_fence_begin_signalling() and dma_fence_end_signalling() helpers to add lockdep annotations to the fence signaling path. Those have proven to be extremely useful since they allow lockdep to point out mistakes immediately and not just after hours of running on a test system.

Yeah, I looked. Nice! Thanks,

--
Joel Fernandes

reviewer
#41 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

In reply to Joel Fernandes
Quoted 8 lines · Joel Fernandes

Hi, Danilo, all,

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. Please let me know if I missed something below. At the moment, off the top I identified that we are doing GFP_KERNEL memory allocations inside GPU buddy allocator during map/unmap. I will work on solutions for that. Thanks.

Quoted 8 lines

All deadlock scenarios
----------------------
The gist is, in the DMA fence signaling critical path we cannot acquire resources (locks or memory allocation etc) that are already acquired when a fence is being waited on to be signaled. So we have to careful which resources we acquire, and also we need to be careful which paths in the driver we do any memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

When thinking about deadlocks it usually helps if you think without detailed scenarios (which would be hard to enumerate and easy to miss), but rather in terms of relative order of resource acquisition. All resources that you wait on would need to form a partial order. Any violation could result in deadlocks. This is also how lockdep checks.

So to me all cases you listed are all the same...

Best,
Gary

Quoted 119 lines

1. deadlock scenario 1: allocator deadlock (no locking needed to trigger it)

Fence Signal start (A) -> Alloc -> MMU notifier/Shrinker (B) -> Fence Wait (A)

ABA deadlock.

2. deadlock scenario 2: Same as 1, but ABBA scenario (2 CPUs).

CPU 0: Fence Signal start (A) -> Alloc (B)

CPU 1: Alloc -> MMU notifier or Shrinker (B) -> Fence Wait (A)

3. deadlock scenario 3: When locking: ABBA (and similarly) deadlock but locking.

CPU 0: Fence Signal start (A) -> Lock (B)

CPU 1: Lock (B) -> Fence Wait (A)

4. deadlock scenario 4: Same as scenario 3, but the fence wait comes from allocation path.

rule: We cannot try to acquire locks in the DMA fence signaling critical path if those locks were already acquire in paths that do reclaimable memory allocations.

CPU 0: Fence Signal (A) -> Lock (B)

CPU 1: Lock (B) -> Alloc -> Fence Wait (A)

5. deadlock scenario 5: Transitive locking:

rule: We cannot try to acquire locks in the DMA fence signaling critical path that are transitively waiting on the same DMA fence.

Fence Signal (A) -> Lock (B)

Lock (B) -> Lock(C)

Lock (C) -> Alloc -> Fence Wait (A)

ABBCCA deadlock.

--
Joel Fernandes

Quoted 72 lines · Danilo Krummrich
Quoted 11 lines · Joel Fernandes

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Quoted 8 lines

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

This won't work unfortunately.

We have to separate allocations and modifications of the page tabe. Or in other words, we must not allocate new PDEs or PTEs while holding the lock protecting the page table from modifications.

Once we have VM_BIND in nova-drm, we will have the situation that userspace passes jobs to modify the GPUs virtual address space and hence the page tables.

Such a jobs has mainly three stages.

(1) The submit stage.

This is where the job is initialized, dependencies are set up and the driver has to pre-allocate all kinds of structures that are required throughout the subsequent stages of the job.

(2) The run stage.

This is the stage where the job is staged for execution and its DMA fence has been made public (i.e. it is accessible by userspace).

This is the stage where we are in the DMA fence signalling critical section, hence we can't do any non-atomic allocations, since otherwise we could deadlock in MMU notifier callbacks for instance.

This is the stage where the page table is actually modified. Hence, we can't acquire any locks that might be held elsewhere while doing non-atomic allocations. Also note that this is transitive, e.g. if you take lock A and somewhere else a lock B is taked while A is already held and we do non-atomic allocations while holding B, then A can't be held in the DMA fence signalling critical path either.

It is also worth noting that this is the stage where we know the exact operations we have to execute based on the VM_BIND request from userspace.

For instance, in the submit stage we may only know that userspace wants that we map a BO with a certain offset in the GPUs virtual address space at [0x0, 0x1000000]. What we don't know is what exact operations this does require, i.e. "What do we have to unmap first?", "Are there any overlapping mappings that we have to truncate?", etc.

So, we have to consider this when we pre-allocate in the submit stage.

(3) The cleanup stage.

This is where the job has been signaled and hence left the DMA fence signalling critical section.

In this stage the job is cleaned up, which includes freeing data that is not required anymore, such as PTEs and PDEs.

author reviewer
#42 lore raw

Re: [PATCH RFC v6 05/26] nova-core: mm: Add support to use PRAMIN windows to write to VRAM

Quoted 20 lines · Gary Guo
Quoted 8 lines · Joel Fernandes

Hi, Danilo, all,

Based on the below discussion and research, I came up with some deadlock scenarios that we need to handle in the v6 series of these patches. Please let me know if I missed something below. At the moment, off the top I identified that we are doing GFP_KERNEL memory allocations inside GPU buddy allocator during map/unmap. I will work on solutions for that. Thanks.

Quoted 8 lines

All deadlock scenarios
----------------------
The gist is, in the DMA fence signaling critical path we cannot acquire resources (locks or memory allocation etc) that are already acquired when a fence is being waited on to be signaled. So we have to careful which resources we acquire, and also we need to be careful which paths in the driver we do any memory allocations under locks that we need in the dma-fence signaling critical path (when doing the virtual memory map/unmap)

Quoted 7 lines

When thinking about deadlocks it usually helps if you think without detailed scenarios (which would be hard to enumerate and easy to miss), but rather in terms of relative order of resource acquisition. All resources that you wait on would need to form a partial order. Any violation could result in deadlocks. This is also how lockdep checks.

So to me all cases you listed are all the same...

Hmm, I am quite familiar with lockdep internals, but I don’t see how all cases are the same one when there are different resources being acquired (locks versus memory allocation, for instance). I think it helps to visualize different cases based on different scenarios for a complete understanding of issues and mild repetition is a good thing IMO - the goal is to not miss anything. But agreed on that is how lockdep works. Lockdep just needs those relationships in its graph to know that ordering enough to flag issues. Speaking of lockdep, I have not checked but we should probably add support for fence signal/wait and resource dependencies, to catch any potential issues as well.

Thanks for taking a look,

--
Joel Fernandes

Quoted 124 lines

Best,
Gary

Quoted 119 lines

1. deadlock scenario 1: allocator deadlock (no locking needed to trigger it)

Fence Signal start (A) -> Alloc -> MMU notifier/Shrinker (B) -> Fence Wait (A)

ABA deadlock.

2. deadlock scenario 2: Same as 1, but ABBA scenario (2 CPUs).

CPU 0: Fence Signal start (A) -> Alloc (B)

CPU 1: Alloc -> MMU notifier or Shrinker (B) -> Fence Wait (A)

3. deadlock scenario 3: When locking: ABBA (and similarly) deadlock but locking.

CPU 0: Fence Signal start (A) -> Lock (B)

CPU 1: Lock (B) -> Fence Wait (A)

4. deadlock scenario 4: Same as scenario 3, but the fence wait comes from allocation path.

rule: We cannot try to acquire locks in the DMA fence signaling critical path if those locks were already acquire in paths that do reclaimable memory allocations.

CPU 0: Fence Signal (A) -> Lock (B)

CPU 1: Lock (B) -> Alloc -> Fence Wait (A)

5. deadlock scenario 5: Transitive locking:

rule: We cannot try to acquire locks in the DMA fence signaling critical path that are transitively waiting on the same DMA fence.

Fence Signal (A) -> Lock (B)

Lock (B) -> Lock(C)

Lock (C) -> Alloc -> Fence Wait (A)

ABBCCA deadlock.

--
Joel Fernandes

Quoted 73 lines · Danilo Krummrich
Quoted 11 lines · Joel Fernandes

My plan is to make TLB and PRAMIN use immutable references in their function calls and then implement internal locking. I've already done this for the GPU buddy functions, so it should be doable, and we'll keep it consistent. As a result, we will have finer-grain locking on the memory management objects instead of requiring to globally lock a common GpuMm object. I'll plan on doing this for v7.

Also, the PTE allocation race you mentioned is already handled by PRAMIN serialization. Since threads must hold the PRAMIN lock to write page table entries, concurrent writers are not possible:

Quoted 8 lines

Thread A: acquire PRAMIN lock
Thread A: read PDE (via PRAMIN) -> NULL
Thread A: alloc PT page, write PDE
Thread A: release PRAMIN lock

Thread B: acquire PRAMIN lock
Thread B: read PDE (via PRAMIN) -> sees A's pointer Thread B: uses existing PT page, no allocation needed

This won't work unfortunately.

We have to separate allocations and modifications of the page tabe. Or in other words, we must not allocate new PDEs or PTEs while holding the lock protecting the page table from modifications.

Once we have VM_BIND in nova-drm, we will have the situation that userspace passes jobs to modify the GPUs virtual address space and hence the page tables.

Such a jobs has mainly three stages.

(1) The submit stage.

This is where the job is initialized, dependencies are set up and the driver has to pre-allocate all kinds of structures that are required throughout the subsequent stages of the job.

(2) The run stage.

This is the stage where the job is staged for execution and its DMA fence has been made public (i.e. it is accessible by userspace).

This is the stage where we are in the DMA fence signalling critical section, hence we can't do any non-atomic allocations, since otherwise we could deadlock in MMU notifier callbacks for instance.

This is the stage where the page table is actually modified. Hence, we can't acquire any locks that might be held elsewhere while doing non-atomic allocations. Also note that this is transitive, e.g. if you take lock A and somewhere else a lock B is taked while A is already held and we do non-atomic allocations while holding B, then A can't be held in the DMA fence signalling critical path either.

It is also worth noting that this is the stage where we know the exact operations we have to execute based on the VM_BIND request from userspace.

For instance, in the submit stage we may only know that userspace wants that we map a BO with a certain offset in the GPUs virtual address space at [0x0, 0x1000000]. What we don't know is what exact operations this does require, i.e. "What do we have to unmap first?", "Are there any overlapping mappings that we have to truncate?", etc.

So, we have to consider this when we pre-allocate in the submit stage.

(3) The cleanup stage.

This is where the job has been signaled and hence left the DMA fence signalling critical section.

In this stage the job is cleaned up, which includes freeing data that is not required anymore, such as PTEs and PDEs.

Signature · 5 lines
Joel Fernandes

-- 
-- 
Joel Fernandes
author reviewer
#43 lore raw

RFC PATCH v6 6/26 docs: gpu: nova-core: Document the PRAMIN aperture mechanism

In reply to Joel Fernandes

Add documentation for the PRAMIN aperture mechanism used by nova-core for direct VRAM access.

Nova only uses TARGET=VID_MEM for VRAM access. The SYS_MEM target values are documented for completeness but not used by the driver.

---
 Documentation/gpu/nova/core/pramin.rst | 125 +++++++++++++++++++++++++
 Documentation/gpu/nova/index.rst       |   1 +
 2 files changed, 126 insertions(+)
 create mode 100644 Documentation/gpu/nova/core/pramin.rst
Documentation/gpu/nova/core/pramin.rst new file +125 -0
new file mode 100644
index 000000000000..55ec9d920629
@@ -0,0 +1,125 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+=========================
+PRAMIN aperture mechanism
+=========================
+
+.. note::
+   The following description is approximate and current as of the Ampere family.
+   It may change for future generations and is intended to assist in understanding
+   the driver code.
+
+Introduction
+============
+
+PRAMIN is a hardware aperture mechanism that provides CPU access to GPU Video RAM (VRAM) before
+the GPU's Memory Management Unit (MMU) and page tables are initialized. This 1MB sliding window,
+located at a fixed offset within BAR0, is essential for setting up page tables and other critical
+GPU data structures without relying on the GPU's MMU.
+
+Architecture Overview
+=====================
+
+The PRAMIN aperture mechanism is logically implemented by the GPU's PBUS (PCIe Bus Controller Unit)
+and provides a CPU-accessible window into VRAM through the PCIe interface::
+
+    +-----------------+    PCIe     +------------------------------+
+    |      CPU        |<----------->|           GPU                |
+    +-----------------+             |                              |
+                                    |  +----------------------+    |
+                                    |  |       PBUS           |    |
+                                    |  |  (Bus Controller)    |    |
+                                    |  |                      |    |
+                                    |  |  +--------------+<------------ (window starts at
+                                    |  |  |   PRAMIN     |    |    |     BAR0 + 0x700000)
+                                    |  |  |   Window     |    |    |
+                                    |  |  |   (1MB)      |    |    |
+                                    |  |  +--------------+    |    |
+                                    |  |         |            |    |
+                                    |  +---------|------------+    |
+                                    |            |                 |
+                                    |            v                 |
+                                    |  +----------------------+<------------ (Program PRAMIN to any
+                                    |  |       VRAM           |    |    64KB-aligned VRAM boundary)
+                                    |  |    (Several GBs)     |    |
+                                    |  |                      |    |
+                                    |  |  FB[0x000000000000]  |    |
+                                    |  |          ...         |    |
+                                    |  |  FB[0x7FFFFFFFFFF]   |    |
+                                    |  +----------------------+    |
+                                    +------------------------------+
+
+PBUS (PCIe Bus Controller) is responsible for, among other things, handling MMIO
+accesses to the BAR registers.
+
+PRAMIN Window Operation
+=======================
+
+The PRAMIN window provides a 1MB sliding aperture that can be repositioned over
+the entire VRAM address space using the ``NV_PBUS_BAR0_WINDOW`` register.
+
+Window Control Mechanism
+-------------------------
+
+The window position is controlled via the PBUS ``BAR0_WINDOW`` register::
+
+    NV_PBUS_BAR0_WINDOW Register (0x1700):
+    +-------+--------+--------------------------------------+
+    | 31:26 | 25:24  |               23:0                   |
+    | RSVD  | TARGET |            BASE_ADDR                 |
+    |       |        |        (bits 39:16 of VRAM address)  |
+    +-------+--------+--------------------------------------+
+
+    BASE_ADDR field (bits 23:0):
+    - Contains bits [39:16] of the target VRAM address
+    - Provides 40-bit (1TB) address space coverage
+    - Must be programmed with 64KB-aligned addresses
+
+    TARGET field (bits 25:24):
+    - 0x0: VRAM (Video Memory)
+    - 0x1: SYS_MEM_COH (Coherent System Memory)
+    - 0x2: SYS_MEM_NONCOH (Non-coherent System Memory)
+    - 0x3: Reserved
+
+    .. note::
+       Nova only uses TARGET=VRAM (0x0) for video memory access. The SYS_MEM
+       target values are documented here for hardware completeness but are
+       not used by the driver.
+
+64KB Alignment Requirement
+---------------------------
+
+The PRAMIN window must be aligned to 64KB boundaries in VRAM. This is enforced
+by the ``BASE_ADDR`` field representing bits [39:16] of the target address::
+
+    VRAM Address Calculation:
+    actual_vram_addr = (BASE_ADDR << 16) + pramin_offset
+    Where:
+    - BASE_ADDR: 24-bit value from NV_PBUS_BAR0_WINDOW[23:0]
+    - pramin_offset: 20-bit offset within the PRAMIN window [0x00000-0xFFFFF]
+
+    Example Window Positioning:
+    +---------------------------------------------------------+
+    |                    VRAM Space                           |
+    |                                                         |
+    |  0x000000000  +-----------------+ <-- 64KB aligned      |
+    |               | PRAMIN Window   |                       |
+    |               |    (1MB)        |                       |
+    |  0x0000FFFFF  +-----------------+                       |
+    |                                                         |
+    |       |              ^                                  |
+    |       |              | Window can slide                 |
+    |       v              | to any 64KB-aligned boundary     |
+    |                                                         |
+    |  0x123400000  +-----------------+ <-- 64KB aligned      |
+    |               | PRAMIN Window   |                       |
+    |               |    (1MB)        |                       |
+    |  0x1234FFFFF  +-----------------+                       |
+    |                                                         |
+    |                       ...                               |
+    |                                                         |
+    |  0x7FFFF0000  +-----------------+ <-- 64KB aligned      |
+    |               | PRAMIN Window   |                       |
+    |               |    (1MB)        |                       |
+    |  0x7FFFFFFFF  +-----------------+                       |
+    +---------------------------------------------------------+
Documentation/gpu/nova/index.rst +1 -0
index e39cb3163581..b8254b1ffe2a 100644
@@ -32,3 +32,4 @@ vGPU manager VFIO driver and the nova-drm driver.
    core/devinit
    core/fwsec
    core/falcon
+   core/pramin
Signature · 1 line
2.34.1
author reviewer
#44 lore raw

RFC PATCH v6 7/26 nova-core: Add BAR1 aperture type and size constant

In reply to Joel Fernandes

Add BAR1_SIZE constant and Bar1 type alias for the 256MB BAR1 aperture. These are prerequisites for BAR1 memory access functionality.

---
 drivers/gpu/nova-core/driver.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/driver.rs +6 -1
index 5a4cc047bcfc..d8b2e967ba4c 100644
@@ -13,7 +13,10 @@
         Vendor, //
     },
     prelude::*,
-    sizes::SZ_16M,
+    sizes::{
+        SZ_16M,
+        SZ_256M, //
+    },
     sync::Arc, //
 };
 
@@ -28,6 +31,7 @@ pub(crate) struct NovaCore {
 }
 
 const BAR0_SIZE: usize = SZ_16M;
+pub(crate) const BAR1_SIZE: usize = SZ_256M;
 
 // For now we only support Ampere which can use up to 47-bit DMA addresses.
 //
@@ -38,6 +42,7 @@ pub(crate) struct NovaCore {
 const GPU_DMA_BITS: u32 = 47;
 
 pub(crate) type Bar0 = pci::Bar<BAR0_SIZE>;
+pub(crate) type Bar1 = pci::Bar<BAR1_SIZE>;
 
 kernel::pci_device_table!(
     PCI_TABLE,
Signature · 1 line
2.34.1
author reviewer
#45 lore raw

RFC PATCH v6 8/26 nova-core: gsp: Add BAR1 PDE base accessors

In reply to Joel Fernandes

Add accessor methods to GspStaticConfigInfo for retrieving the BAR1 Page Directory Entry base addresses from GSP-RM firmware.

These addresses point to the root page tables for BAR1 virtual memory spaces. The page tables are set up by GSP-RM during GPU initialization.

---
 drivers/gpu/nova-core/driver.rs          | 1 +
 drivers/gpu/nova-core/gsp/commands.rs    | 8 ++++++++
 drivers/gpu/nova-core/gsp/fw/commands.rs | 8 ++++++++
 3 files changed, 17 insertions(+)
drivers/gpu/nova-core/driver.rs +1 -0
index d8b2e967ba4c..f30ffa45cf13 100644
@@ -42,6 +42,7 @@ pub(crate) struct NovaCore {
 const GPU_DMA_BITS: u32 = 47;
 
 pub(crate) type Bar0 = pci::Bar<BAR0_SIZE>;
+#[expect(dead_code)]
 pub(crate) type Bar1 = pci::Bar<BAR1_SIZE>;
 
 kernel::pci_device_table!(
drivers/gpu/nova-core/gsp/commands.rs +8 -0
index c8430a076269..7b5025cba106 100644
@@ -189,6 +189,7 @@ fn init(&self) -> impl Init<Self::Command, Self::InitError> {
 /// The reply from the GSP to the [`GetGspInfo`] command.
 pub(crate) struct GetGspStaticInfoReply {
     gpu_name: [u8; 64],
+    bar1_pde_base: u64,
 }
 
 impl MessageFromGsp for GetGspStaticInfoReply {
@@ -202,6 +203,7 @@ fn read(
     ) -> Result<Self, Self::InitError> {
         Ok(GetGspStaticInfoReply {
             gpu_name: msg.gpu_name_str(),
+            bar1_pde_base: msg.bar1_pde_base(),
         })
     }
 }
@@ -228,6 +230,12 @@ pub(crate) fn gpu_name(&self) -> core::result::Result<&str, GpuNameError> {
             .to_str()
             .map_err(GpuNameError::InvalidUtf8)
     }
+
+    /// Returns the BAR1 Page Directory Entry base address.
+    #[expect(dead_code)]
+    pub(crate) fn bar1_pde_base(&self) -> u64 {
+        self.bar1_pde_base
+    }
 }
 
 /// Send the [`GetGspInfo`] command and awaits for its reply.
drivers/gpu/nova-core/gsp/fw/commands.rs +8 -0
index 21be44199693..f069f4092911 100644
@@ -114,6 +114,14 @@ impl GspStaticConfigInfo {
     pub(crate) fn gpu_name_str(&self) -> [u8; 64] {
         self.0.gpuNameString
     }
+
+    /// Returns the BAR1 Page Directory Entry base address.
+    ///
+    /// This is the root page table address for BAR1 virtual memory,
+    /// set up by GSP-RM firmware.
+    pub(crate) fn bar1_pde_base(&self) -> u64 {
+        self.0.bar1PdeBase
+    }
 }
 
 // SAFETY: Padding is explicit and will not contain uninitialized data.
Signature · 1 line
2.34.1
author reviewer
#46 lore raw

RFC PATCH v6 9/26 nova-core: mm: Add common memory management types

In reply to Joel Fernandes

Add foundational types for GPU memory management. These types are used throughout the nova memory management subsystem for page table operations, address translation, and memory allocation.

---
 drivers/gpu/nova-core/mm/mod.rs | 147 ++++++++++++++++++++++++++++++++
 1 file changed, 147 insertions(+)
drivers/gpu/nova-core/mm/mod.rs +147 -0
index 7a5dd4220c67..b57016d453ce 100644
@@ -2,4 +2,151 @@
 
 //! Memory management subsystems for nova-core.
 
+#![expect(dead_code)]
+
 pub(crate) mod pramin;
+
+use kernel::sizes::SZ_4K;
+
+/// Page size in bytes (4 KiB).
+pub(crate) const PAGE_SIZE: usize = SZ_4K;
+
+bitfield! {
+    pub(crate) struct VramAddress(u64), "Physical VRAM address in GPU video memory" {
+        11:0    offset          as u64, "Offset within 4KB page";
+        63:12   frame_number    as u64 => Pfn, "Physical frame number";
+    }
+}
+
+impl VramAddress {
+    /// Create a new VRAM address from a raw value.
+    pub(crate) const fn new(addr: u64) -> Self {
+        Self(addr)
+    }
+
+    /// Get the raw address value as `usize` (useful for MMIO offsets).
+    pub(crate) const fn raw(&self) -> usize {
+        self.0 as usize
+    }
+
+    /// Get the raw address value as `u64`.
+    pub(crate) const fn raw_u64(&self) -> u64 {
+        self.0
+    }
+}
+
+impl From<Pfn> for VramAddress {
+    fn from(pfn: Pfn) -> Self {
+        Self::default().set_frame_number(pfn)
+    }
+}
+
+bitfield! {
+    pub(crate) struct VirtualAddress(u64), "Virtual address in GPU address space" {
+        11:0    offset          as u64, "Offset within 4KB page";
+        20:12   l4_index        as u64, "Level 4 index (PTE)";
+        29:21   l3_index        as u64, "Level 3 index (Dual PDE)";
+        38:30   l2_index        as u64, "Level 2 index";
+        47:39   l1_index        as u64, "Level 1 index";
+        56:48   l0_index        as u64, "Level 0 index (PDB)";
+        63:12   frame_number    as u64 => Vfn, "Virtual frame number";
+    }
+}
+
+impl VirtualAddress {
+    /// Create a new virtual address from a raw value.
+    #[expect(dead_code)]
+    pub(crate) const fn new(addr: u64) -> Self {
+        Self(addr)
+    }
+
+    /// Get the page table index for a given level.
+    pub(crate) fn level_index(&self, level: u64) -> u64 {
+        match level {
+            0 => self.l0_index(),
+            1 => self.l1_index(),
+            2 => self.l2_index(),
+            3 => self.l3_index(),
+            4 => self.l4_index(),
+            _ => 0,
+        }
+    }
+}
+
+impl From<Vfn> for VirtualAddress {
+    fn from(vfn: Vfn) -> Self {
+        Self::default().set_frame_number(vfn)
+    }
+}
+
+/// Physical Frame Number.
+///
+/// Represents a physical page in VRAM.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub(crate) struct Pfn(u64);
+
+impl Pfn {
+    /// Create a new PFN from a frame number.
+    pub(crate) const fn new(frame_number: u64) -> Self {
+        Self(frame_number)
+    }
+
+    /// Get the raw frame number.
+    pub(crate) const fn raw(self) -> u64 {
+        self.0
+    }
+}
+
+impl From<VramAddress> for Pfn {
+    fn from(addr: VramAddress) -> Self {
+        addr.frame_number()
+    }
+}
+
+impl From<u64> for Pfn {
+    fn from(val: u64) -> Self {
+        Self(val)
+    }
+}
+
+impl From<Pfn> for u64 {
+    fn from(pfn: Pfn) -> Self {
+        pfn.0
+    }
+}
+
+/// Virtual Frame Number.
+///
+/// Represents a virtual page in GPU address space.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub(crate) struct Vfn(u64);
+
+impl Vfn {
+    /// Create a new VFN from a frame number.
+    pub(crate) const fn new(frame_number: u64) -> Self {
+        Self(frame_number)
+    }
+
+    /// Get the raw frame number.
+    pub(crate) const fn raw(self) -> u64 {
+        self.0
+    }
+}
+
+impl From<VirtualAddress> for Vfn {
+    fn from(addr: VirtualAddress) -> Self {
+        addr.frame_number()
+    }
+}
+
+impl From<u64> for Vfn {
+    fn from(val: u64) -> Self {
+        Self(val)
+    }
+}
+
+impl From<Vfn> for u64 {
+    fn from(vfn: Vfn) -> Self {
+        vfn.0
+    }
+}
Signature · 1 line
2.34.1
author reviewer
#47 lore raw

RFC PATCH v6 10/26 nova-core: mm: Add common types for all page table formats

In reply to Joel Fernandes

Add common page table types shared between MMU v2 and v3. These types are hardware-agnostic and used by both MMU versions.

---
 drivers/gpu/nova-core/mm/mod.rs           |   1 +
 drivers/gpu/nova-core/mm/pagetable/mod.rs | 168 ++++++++++++++++++++++
 2 files changed, 169 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/mod.rs
drivers/gpu/nova-core/mm/mod.rs +1 -0
index b57016d453ce..6015fc8753bc 100644
@@ -4,6 +4,7 @@
 
 #![expect(dead_code)]
 
+pub(crate) mod pagetable;
 pub(crate) mod pramin;
 
 use kernel::sizes::SZ_4K;
drivers/gpu/nova-core/mm/pagetable/mod.rs new file +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#48 lore raw

RFC PATCH v6 11/26 nova-core: mm: Add MMU v2 page table types

In reply to Joel Fernandes

Add page table entry and directory structures for MMU version 2 used by Turing/Ampere/Ada GPUs.

---
 drivers/gpu/nova-core/mm/pagetable/mod.rs  |   1 +
 drivers/gpu/nova-core/mm/pagetable/ver2.rs | 184 +++++++++++++++++++++
 2 files changed, 185 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/ver2.rs
drivers/gpu/nova-core/mm/pagetable/mod.rs +1 -0
index bb3a37cc6ca0..787755e89a5b 100644
@@ -7,6 +7,7 @@
 //! - Memory aperture types for PDEs and PTEs
 
 #![expect(dead_code)]
+pub(crate) mod ver2;
 
 use crate::gpu::Architecture;
 
drivers/gpu/nova-core/mm/pagetable/ver2.rs new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#49 lore raw

RFC PATCH v6 12/26 nova-core: mm: Add MMU v3 page table types

In reply to Joel Fernandes

Add page table entry and directory structures for MMU version 3 used by Hopper and later GPUs.

---
 drivers/gpu/nova-core/mm/pagetable/mod.rs  |   1 +
 drivers/gpu/nova-core/mm/pagetable/ver3.rs | 286 +++++++++++++++++++++
 2 files changed, 287 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/ver3.rs
drivers/gpu/nova-core/mm/pagetable/mod.rs +1 -0
index 787755e89a5b..3b1324add844 100644
@@ -8,6 +8,7 @@
 
 #![expect(dead_code)]
 pub(crate) mod ver2;
+pub(crate) mod ver3;
 
 use crate::gpu::Architecture;
 
drivers/gpu/nova-core/mm/pagetable/ver3.rs new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#50 lore raw

RFC PATCH v6 13/26 nova-core: mm: Add unified page table entry wrapper enums

In reply to Joel Fernandes

Add unified Pte, Pde, and DualPde wrapper enums that abstract over MMU v2 and v3 page table entry formats. These enums allow the page table walker and VMM to work with both MMU versions.

Each unified type:
- Takes MmuVersion parameter in constructors
- Wraps both ver2 and ver3 variants
- Delegates method calls to the appropriate variant

This enables version-agnostic page table operations while keeping version-specific implementation details encapsulated in the ver2 and ver3 modules.

---
 drivers/gpu/nova-core/mm/pagetable/mod.rs | 194 ++++++++++++++++++++++
 1 file changed, 194 insertions(+)
drivers/gpu/nova-core/mm/pagetable/mod.rs +194 -0
index 3b1324add844..72bc7cda8df6 100644
@@ -10,6 +10,10 @@
 pub(crate) mod ver2;
 pub(crate) mod ver3;
 
+use super::{
+    Pfn,
+    VramAddress, //
+};
 use crate::gpu::Architecture;
 
 /// MMU version enumeration.
@@ -168,3 +172,193 @@ fn from(val: AperturePde) -> Self {
         val as u8
     }
 }
+
+/// Unified Page Table Entry wrapper for both MMU v2 and v3 `PTE`
+/// types, allowing the walker to work with either format.
+#[derive(Debug, Clone, Copy)]
+pub(crate) enum Pte {
+    /// MMU v2 `PTE` (Turing/Ampere/Ada).
+    V2(ver2::Pte),
+    /// MMU v3 `PTE` (Hopper+).
+    V3(ver3::Pte),
+}
+
+impl Pte {
+    /// Create a `PTE` from a raw `u64` value for the given MMU version.
+    pub(crate) fn new(version: MmuVersion, val: u64) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::Pte::new(val)),
+            MmuVersion::V3 => Self::V3(ver3::Pte::new(val)),
+        }
+    }
+
+    /// Create an invalid `PTE` for the given MMU version.
+    pub(crate) fn invalid(version: MmuVersion) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::Pte::invalid()),
+            MmuVersion::V3 => Self::V3(ver3::Pte::invalid()),
+        }
+    }
+
+    /// Create a valid `PTE` for video memory.
+    pub(crate) fn new_vram(version: MmuVersion, pfn: Pfn, writable: bool) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::Pte::new_vram(pfn, writable)),
+            MmuVersion::V3 => Self::V3(ver3::Pte::new_vram(pfn, writable)),
+        }
+    }
+
+    /// Check if this `PTE` is valid.
+    pub(crate) fn is_valid(&self) -> bool {
+        match self {
+            Self::V2(p) => p.valid(),
+            Self::V3(p) => p.valid(),
+        }
+    }
+
+    /// Get the physical frame number.
+    pub(crate) fn frame_number(&self) -> Pfn {
+        match self {
+            Self::V2(p) => p.frame_number(),
+            Self::V3(p) => p.frame_number(),
+        }
+    }
+
+    /// Get the raw `u64` value.
+    pub(crate) fn raw_u64(&self) -> u64 {
+        match self {
+            Self::V2(p) => p.raw_u64(),
+            Self::V3(p) => p.raw_u64(),
+        }
+    }
+}
+
+impl Default for Pte {
+    fn default() -> Self {
+        Self::V2(ver2::Pte::default())
+    }
+}
+
+/// Unified Page Directory Entry wrapper for both MMU v2 and v3 `PDE`.
+#[derive(Debug, Clone, Copy)]
+pub(crate) enum Pde {
+    /// MMU v2 `PDE` (Turing/Ampere/Ada).
+    V2(ver2::Pde),
+    /// MMU v3 `PDE` (Hopper+).
+    V3(ver3::Pde),
+}
+
+impl Pde {
+    /// Create a `PDE` from a raw `u64` value for the given MMU version.
+    pub(crate) fn new(version: MmuVersion, val: u64) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::Pde::new(val)),
+            MmuVersion::V3 => Self::V3(ver3::Pde::new(val)),
+        }
+    }
+
+    /// Create a valid `PDE` pointing to a page table in video memory.
+    pub(crate) fn new_vram(version: MmuVersion, table_pfn: Pfn) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::Pde::new_vram(table_pfn)),
+            MmuVersion::V3 => Self::V3(ver3::Pde::new_vram(table_pfn)),
+        }
+    }
+
+    /// Create an invalid `PDE` for the given MMU version.
+    pub(crate) fn invalid(version: MmuVersion) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::Pde::invalid()),
+            MmuVersion::V3 => Self::V3(ver3::Pde::invalid()),
+        }
+    }
+
+    /// Check if this `PDE` is valid.
+    pub(crate) fn is_valid(&self) -> bool {
+        match self {
+            Self::V2(p) => p.is_valid(),
+            Self::V3(p) => p.is_valid(),
+        }
+    }
+
+    /// Get the VRAM address of the page table.
+    pub(crate) fn table_vram_address(&self) -> VramAddress {
+        match self {
+            Self::V2(p) => p.table_vram_address(),
+            Self::V3(p) => p.table_vram_address(),
+        }
+    }
+
+    /// Get the raw `u64` value.
+    pub(crate) fn raw_u64(&self) -> u64 {
+        match self {
+            Self::V2(p) => p.raw_u64(),
+            Self::V3(p) => p.raw_u64(),
+        }
+    }
+}
+
+impl Default for Pde {
+    fn default() -> Self {
+        Self::V2(ver2::Pde::default())
+    }
+}
+
+/// Unified Dual Page Directory Entry wrapper for both MMU v2 and v3 [`DualPde`].
+#[derive(Debug, Clone, Copy)]
+pub(crate) enum DualPde {
+    /// MMU v2 [`DualPde`] (Turing/Ampere/Ada).
+    V2(ver2::DualPde),
+    /// MMU v3 [`DualPde`] (Hopper+).
+    V3(ver3::DualPde),
+}
+
+impl DualPde {
+    /// Create a [`DualPde`] from raw 128-bit value (two `u64`s) for the given MMU version.
+    pub(crate) fn new(version: MmuVersion, big: u64, small: u64) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::DualPde::new(big, small)),
+            MmuVersion::V3 => Self::V3(ver3::DualPde::new(big, small)),
+        }
+    }
+
+    /// Create a [`DualPde`] with only the small page table pointer set.
+    pub(crate) fn new_small(version: MmuVersion, table_pfn: Pfn) -> Self {
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::DualPde::new_small(table_pfn)),
+            MmuVersion::V3 => Self::V3(ver3::DualPde::new_small(table_pfn)),
+        }
+    }
+
+    /// Check if the small page table pointer is valid.
+    pub(crate) fn has_small(&self) -> bool {
+        match self {
+            Self::V2(d) => d.has_small(),
+            Self::V3(d) => d.has_small(),
+        }
+    }
+
+    /// Get the small page table VRAM address.
+    pub(crate) fn small_vram_address(&self) -> VramAddress {
+        match self {
+            Self::V2(d) => d.small.table_vram_address(),
+            Self::V3(d) => d.small.table_vram_address(),
+        }
+    }
+
+    /// Get the raw `u64` value of the big PDE.
+    pub(crate) fn big_raw_u64(&self) -> u64 {
+        match self {
+            Self::V2(d) => d.big.raw_u64(),
+            Self::V3(d) => d.big.raw_u64(),
+        }
+    }
+
+    /// Get the raw `u64` value of the small PDE.
+    pub(crate) fn small_raw_u64(&self) -> u64 {
+        match self {
+            Self::V2(d) => d.small.raw_u64(),
+            Self::V3(d) => d.small.raw_u64(),
+        }
+    }
+}
Signature · 1 line
2.34.1
#51 lore raw

Re: [PATCH RFC v6 13/26] nova-core: mm: Add unified page table entry wrapper enums

Quoted 4 lines · On Tue, 20 Jan 2026 15:42:50 -0500 Joel Fernandes <[email protected]> wrote:

Add unified Pte, Pde, and DualPde wrapper enums that abstract over MMU v2 and v3 page table entry formats. These enums allow the page table walker and VMM to work with both MMU versions.

snip

Quoted 20 lines
(quoted diff) +2 -0
+impl DualPde {
+    /// Create a [`DualPde`] from raw 128-bit value (two `u64`s) for

the given MMU version.
+ pub(crate) fn new(version: MmuVersion, big: u64, small: u64) ->
Self {

drivers/gpu/nova-core/mm/pagetable/mod.rs +8 -0
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::DualPde::new(big, small)),
+            MmuVersion::V3 => Self::V3(ver3::DualPde::new(big, small)),
+        }
+    }
+
+    /// Create a [`DualPde`] with only the small page table pointer set.
+    pub(crate) fn new_small(version: MmuVersion, table_pfn: Pfn) ->

Self {

(quoted diff) +2 -0
+        match version {
+            MmuVersion::V2 =>

Self::V2(ver2::DualPde::new_small(table_pfn)),
+ MmuVersion::V3 =>
Self::V3(ver3::DualPde::new_small(table_pfn)),

drivers/gpu/nova-core/mm/pagetable/mod.rs +1 -0
+        }

drivers/gpu/nova-core/mm/pagetable/mod.rs fn from(val: AperturePde) -> Self { · line 340

Quoted 10 lines
drivers/gpu/nova-core/mm/pagetable/mod.rs +10 -0
+    }
+
+    /// Check if the small page table pointer is valid.
+    pub(crate) fn has_small(&self) -> bool {
+        match self {
+            Self::V2(d) => d.has_small(),
+            Self::V3(d) => d.has_small(),
+        }
+    }
+

Should we also have a has_big here as well?

Z.

Quoted 24 lines
drivers/gpu/nova-core/mm/pagetable/mod.rs +24 -0
+    /// Get the small page table VRAM address.
+    pub(crate) fn small_vram_address(&self) -> VramAddress {
+        match self {
+            Self::V2(d) => d.small.table_vram_address(),
+            Self::V3(d) => d.small.table_vram_address(),
+        }
+    }
+
+    /// Get the raw `u64` value of the big PDE.
+    pub(crate) fn big_raw_u64(&self) -> u64 {
+        match self {
+            Self::V2(d) => d.big.raw_u64(),
+            Self::V3(d) => d.big.raw_u64(),
+        }
+    }
+
+    /// Get the raw `u64` value of the small PDE.
+    pub(crate) fn small_raw_u64(&self) -> u64 {
+        match self {
+            Self::V2(d) => d.small.raw_u64(),
+            Self::V3(d) => d.small.raw_u64(),
+        }
+    }
+}
author reviewer
#52 lore raw

Re: [PATCH RFC v6 13/26] nova-core: mm: Add unified page table entry wrapper enums

Quoted 33 lines · Zhi Wang
Quoted 4 lines · On Tue, 20 Jan 2026 15:42:50 -0500 Joel Fernandes <[email protected]> wrote:

Add unified Pte, Pde, and DualPde wrapper enums that abstract over MMU v2 and v3 page table entry formats. These enums allow the page table walker and VMM to work with both MMU versions.

snip

Quoted 24 lines
(quoted diff) +2 -0
+impl DualPde {
+    /// Create a [`DualPde`] from raw 128-bit value (two `u64`s) for

the given MMU version.
+ pub(crate) fn new(version: MmuVersion, big: u64, small: u64) ->
Self {

drivers/gpu/nova-core/mm/pagetable/mod.rs +8 -0
+        match version {
+            MmuVersion::V2 => Self::V2(ver2::DualPde::new(big, small)),
+            MmuVersion::V3 => Self::V3(ver3::DualPde::new(big, small)),
+        }
+    }
+
+    /// Create a [`DualPde`] with only the small page table pointer set.
+    pub(crate) fn new_small(version: MmuVersion, table_pfn: Pfn) ->

Self {

(quoted diff) +2 -0
+        match version {
+            MmuVersion::V2 =>

Self::V2(ver2::DualPde::new_small(table_pfn)),
+ MmuVersion::V3 =>
Self::V3(ver3::DualPde::new_small(table_pfn)),

drivers/gpu/nova-core/mm/pagetable/mod.rs +5 -0
+        }
+    }
+
+    /// Check if the small page table pointer is valid.
+    pub(crate) fn has_small(&self) -> bool {
Quoted 8 lines

drivers/gpu/nova-core/mm/pagetable/mod.rs fn from(val: AperturePde) -> Self { · line 340

Quoted 6 lines
drivers/gpu/nova-core/mm/pagetable/mod.rs +6 -0
+        match self {
+            Self::V2(d) => d.has_small(),
+            Self::V3(d) => d.has_small(),
+        }
+    }
+

Should we also have a has_big here as well?

Good catch, I will add that in, thanks.

--
Joel Fernandes

author reviewer
#53 lore raw

RFC PATCH v6 14/26 nova-core: mm: Add TLB flush support

In reply to Joel Fernandes

Add TLB (Translation Lookaside Buffer) flush support for GPU MMU.

After modifying page table entries, the GPU's TLB must be invalidated to ensure the new mappings take effect. The Tlb struct provides flush functionality through BAR0 registers.

The flush operation writes the page directory base address and triggers an invalidation, polling for completion with a 2 second timeout matching the Nouveau driver.

---
 drivers/gpu/nova-core/mm/mod.rs |  1 +
 drivers/gpu/nova-core/mm/tlb.rs | 79 +++++++++++++++++++++++++++++++++
 drivers/gpu/nova-core/regs.rs   | 33 ++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/tlb.rs
drivers/gpu/nova-core/mm/mod.rs +1 -0
index 6015fc8753bc..39635f2d0156 100644
@@ -6,6 +6,7 @@
 
 pub(crate) mod pagetable;
 pub(crate) mod pramin;
+pub(crate) mod tlb;
 
 use kernel::sizes::SZ_4K;
 
drivers/gpu/nova-core/mm/tlb.rs new file +79 -0

79 lines not on this page · raw message

drivers/gpu/nova-core/regs.rs +33 -0
index c8b8fbdcf608..e722ef837e11 100644
@@ -414,3 +414,36 @@ pub(crate) mod ga100 {
         0:0     display_disabled as bool;
     });
 }
+
+// MMU TLB
+
+register!(NV_TLB_FLUSH_PDB_LO @ 0x00b830a0, "TLB flush register: PDB address bits [39:8]" {
+    31:0    pdb_lo as u32, "PDB address bits [39:8]";
+});
+
+impl NV_TLB_FLUSH_PDB_LO {
+    /// Create a register value from a PDB address.
+    ///
+    /// Extracts bits [39:8] of the address and shifts it right by 8 bits.
+    pub(crate) fn from_pdb_addr(addr: u64) -> Self {
+        Self::default().set_pdb_lo(((addr >> 8) & 0xFFFF_FFFF) as u32)
+    }
+}
+
+register!(NV_TLB_FLUSH_PDB_HI @ 0x00b830a4, "TLB flush register: PDB address bits [47:40]" {
+    7:0     pdb_hi as u8, "PDB address bits [47:40]";
+});
+
+impl NV_TLB_FLUSH_PDB_HI {
+    /// Create a register value from a PDB address.
+    ///
+    /// Extracts bits [47:40] of the address and shifts it right by 40 bits.
+    pub(crate) fn from_pdb_addr(addr: u64) -> Self {
+        Self::default().set_pdb_hi(((addr >> 40) & 0xFF) as u8)
+    }
+}
+
+register!(NV_TLB_FLUSH_CTRL @ 0x00b830b0, "TLB flush control register" {
+    0:0     page_all as bool, "Invalidate all pages";
+    31:31   enable as bool, "Enable/trigger flush (clears when flush completes)";
+});
Signature · 1 line
2.34.1
#54 lore raw

Re: [PATCH RFC v6 14/26] nova-core: mm: Add TLB flush support

Quoted 2 lines · On Tue, 20 Jan 2026 15:42:51 -0500 Joel Fernandes <[email protected]> wrote:

Add TLB (Translation Lookaside Buffer) flush support for GPU MMU.

The same concern as in PATCH 5, guess we need to think of concurrency for TLB flush.

Quoted 162 lines

After modifying page table entries, the GPU's TLB must be invalidated to ensure the new mappings take effect. The Tlb struct provides flush functionality through BAR0 registers.

The flush operation writes the page directory base address and triggers an invalidation, polling for completion with a 2 second timeout matching the Nouveau driver.

---
 drivers/gpu/nova-core/mm/mod.rs |  1 +
 drivers/gpu/nova-core/mm/tlb.rs | 79 +++++++++++++++++++++++++++++++++
 drivers/gpu/nova-core/regs.rs   | 33 ++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/tlb.rs
(quoted diff) +0 -0

b/drivers/gpu/nova-core/mm/mod.rs index 6015fc8753bc..39635f2d0156 100644

drivers/gpu/nova-core/mm/mod.rs +1 -0
@@ -6,6 +6,7 @@
 
 pub(crate) mod pagetable;
 pub(crate) mod pramin;
+pub(crate) mod tlb;
 
 use kernel::sizes::SZ_4K;
 
(quoted diff) +0 -0

b/drivers/gpu/nova-core/mm/tlb.rs new file mode 100644 index 000000000000..8b2ee620da18

drivers/gpu/nova-core/mm/tlb.rs new file +14 -0
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! TLB (Translation Lookaside Buffer) flush support for GPU MMU.
+//!
+//! After modifying page table entries, the GPU's TLB must be flushed to
+//! ensure the new mappings take effect. This module provides TLB flush
+//! functionality for virtual memory managers.
+//!
+//! # Example
+//!
+//! ```ignore
+//! use crate::mm::tlb::Tlb;
+//!
+//! fn page_table_update(tlb: &Tlb, pdb_addr: VramAddress) ->

Result<()> { +//! // ... modify page tables ...

drivers/gpu/nova-core/mm/tlb.rs new file +38 -0
+//!
+//!     // Flush TLB to make changes visible (polls for completion).
+//!     tlb.flush(pdb_addr)?;
+//!
+//!     Ok(())
+//! }
+//! ```
+
+#![allow(dead_code)]
+
+use kernel::{
+    devres::Devres,
+    io::poll::read_poll_timeout,
+    prelude::*,
+    sync::Arc,
+    time::Delta, //
+};
+
+use crate::{
+    driver::Bar0,
+    mm::VramAddress,
+    regs, //
+};
+
+/// TLB manager for GPU translation buffer operations.
+pub(crate) struct Tlb {
+    bar: Arc<Devres<Bar0>>,
+}
+
+impl Tlb {
+    /// Create a new TLB manager.
+    pub(super) fn new(bar: Arc<Devres<Bar0>>) -> Self {
+        Self { bar }
+    }
+
+    /// Flush the GPU TLB for a specific page directory base.
+    ///
+    /// This invalidates all TLB entries associated with the given PDB

address.
+ /// Must be called after modifying page table entries to ensure the
GPU sees

drivers/gpu/nova-core/mm/tlb.rs new file +6 -0
+    /// the updated mappings.
+    pub(crate) fn flush(&self, pdb_addr: VramAddress) -> Result {
+        let bar = self.bar.try_access().ok_or(ENODEV)?;
+
+        // Write PDB address.
+

regs::NV_TLB_FLUSH_PDB_LO::from_pdb_addr(pdb_addr.raw_u64()).write(&*bar); + regs::NV_TLB_FLUSH_PDB_HI::from_pdb_addr(pdb_addr.raw_u64()).write(&*bar);

drivers/gpu/nova-core/mm/tlb.rs new file +18 -0
+
+        // Trigger flush: invalidate all pages and enable.
+        regs::NV_TLB_FLUSH_CTRL::default()
+            .set_page_all(true)
+            .set_enable(true)
+            .write(&*bar);
+
+        // Poll for completion - enable bit clears when flush is done.
+        read_poll_timeout(
+            || Ok(regs::NV_TLB_FLUSH_CTRL::read(&*bar)),
+            |ctrl| !ctrl.enable(),
+            Delta::ZERO,
+            Delta::from_secs(2),
+        )?;
+
+        Ok(())
+    }
+}
(quoted diff) +0 -0

b/drivers/gpu/nova-core/regs.rs index c8b8fbdcf608..e722ef837e11 100644

drivers/gpu/nova-core/regs.rs +4 -0
@@ -414,3 +414,36 @@ pub(crate) mod ga100 {
         0:0     display_disabled as bool;
     });
 }
+
+// MMU TLB
+
+register!(NV_TLB_FLUSH_PDB_LO @ 0x00b830a0, "TLB flush register: PDB

address bits [39:8]" {

drivers/gpu/nova-core/regs.rs +7 -0
+    31:0    pdb_lo as u32, "PDB address bits [39:8]";
+});
+
+impl NV_TLB_FLUSH_PDB_LO {
+    /// Create a register value from a PDB address.
+    ///
+    /// Extracts bits [39:8] of the address and shifts it right by 8

bits.

drivers/gpu/nova-core/regs.rs +6 -0
+    pub(crate) fn from_pdb_addr(addr: u64) -> Self {
+        Self::default().set_pdb_lo(((addr >> 8) & 0xFFFF_FFFF) as u32)
+    }
+}
+
+register!(NV_TLB_FLUSH_PDB_HI @ 0x00b830a4, "TLB flush register: PDB

address bits [47:40]" {

drivers/gpu/nova-core/regs.rs +7 -0
+    7:0     pdb_hi as u8, "PDB address bits [47:40]";
+});
+
+impl NV_TLB_FLUSH_PDB_HI {
+    /// Create a register value from a PDB address.
+    ///
+    /// Extracts bits [47:40] of the address and shifts it right by 40

bits.

drivers/gpu/nova-core/regs.rs +8 -0
+    pub(crate) fn from_pdb_addr(addr: u64) -> Self {
+        Self::default().set_pdb_hi(((addr >> 40) & 0xFF) as u8)
+    }
+}
+
+register!(NV_TLB_FLUSH_CTRL @ 0x00b830b0, "TLB flush control register" {
+    0:0     page_all as bool, "Invalidate all pages";
+    31:31   enable as bool, "Enable/trigger flush (clears when flush

completes)"; +});

author reviewer
#55 lore raw

Re: [PATCH RFC v6 14/26] nova-core: mm: Add TLB flush support

Hello, Zhi,

Quoted 7 lines · Zhi Wang
Quoted 2 lines · On Tue, 20 Jan 2026 15:42:51 -0500 Joel Fernandes <[email protected]> wrote:

Add TLB (Translation Lookaside Buffer) flush support for GPU MMU.

The same concern as in PATCH 5, guess we need to think of concurrency for TLB flush.

Will change:
pub(crate) fn flush(&self, pdb_addr: VramAddress)

to:
pub(crate) fn flush(&mut self, pdb_addr: VramAddress)

and also changing in mm/mod.rs:

    pub(crate) fn tlb(&self) -> &Tlb {
to:
    pub(crate) fn tlb(&mut self) -> &mut Tlb.

Since TLB operations modify registers, that does make sense to me.

For the buddy allocator, however, I am locking internally so I left it as is:

    /// Access the [`GpuBuddy`] allocator.
    pub(crate) fn buddy(&self) -> &GpuBuddy {
        &self.buddy
    }
Signature · 1 line
Joel Fernandes
author reviewer
#56 lore raw

RFC PATCH v6 15/26 nova-core: mm: Add GpuMm centralized memory manager

In reply to Joel Fernandes

Introduce GpuMm as the centralized GPU memory manager that owns:
- Buddy allocator for VRAM allocation.
- PRAMIN window for direct VRAM access.
- TLB manager for translation buffer operations.

This provides clean ownership model where GpuMm provides accessor methods for its components that can be used for memory management operations.

---
 drivers/gpu/nova-core/gpu.rs    | 14 +++++++++
 drivers/gpu/nova-core/mm/mod.rs | 55 ++++++++++++++++++++++++++++++++-
 2 files changed, 68 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gpu.rs +14 -0
index 9b042ef1a308..572e6d4502bc 100644
@@ -4,8 +4,10 @@
     device,
     devres::Devres,
     fmt,
+    gpu::buddy::GpuBuddyParams,
     pci,
     prelude::*,
+    sizes::{SZ_1M, SZ_4K},
     sync::Arc, //
 };
 
@@ -19,6 +21,7 @@
     fb::SysmemFlush,
     gfw,
     gsp::Gsp,
+    mm::GpuMm,
     regs,
 };
 
@@ -249,6 +252,8 @@ pub(crate) struct Gpu {
     gsp_falcon: Falcon<GspFalcon>,
     /// SEC2 falcon instance, used for GSP boot up and cleanup.
     sec2_falcon: Falcon<Sec2Falcon>,
+    /// GPU memory manager owning memory management resources.
+    mm: GpuMm,
     /// GSP runtime data. Temporarily an empty placeholder.
     #[pin]
     gsp: Gsp,
@@ -281,6 +286,15 @@ pub(crate) fn new<'a>(
 
             sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset)?,
 
+            // Create GPU memory manager owning memory management resources.
+            // This will be initialized with the usable VRAM region from GSP in a later
+            // patch. For now, we use a placeholder of 1MB.
+            mm: GpuMm::new(devres_bar.clone(), GpuBuddyParams {
+                base_offset_bytes: 0,
+                physical_memory_size_bytes: SZ_1M as u64,
+                chunk_size_bytes: SZ_4K as u64,
+            })?,
+
             gsp <- Gsp::new(pdev),
 
             _: { gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)? },
drivers/gpu/nova-core/mm/mod.rs +54 -1

61 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#57 lore raw

RFC PATCH v6 16/26 nova-core: mm: Add page table walker for MMU v2

In reply to Joel Fernandes

Add the page table walker implementation that traverses the 5-level page table hierarchy (PDB -> L1 -> L2 -> L3 -> L4) to resolve virtual addresses to physical addresses or find PTE locations.

The walker provides:
- walk_to_pte_lookup(): Walk existing page tables (no allocation)
- Helper functions for reading/writing PDEs and PTEs via PRAMIN

Uses GpuMm API for centralized access to PRAMIN window.

---
 drivers/gpu/nova-core/mm/pagetable/mod.rs  |  13 +
 drivers/gpu/nova-core/mm/pagetable/walk.rs | 285 +++++++++++++++++++++
 2 files changed, 298 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/pagetable/walk.rs
drivers/gpu/nova-core/mm/pagetable/mod.rs +13 -0
index 72bc7cda8df6..4c77d4953fbd 100644
@@ -9,12 +9,25 @@
 #![expect(dead_code)]
 pub(crate) mod ver2;
 pub(crate) mod ver3;
+pub(crate) mod walk;
 
 use super::{
+    GpuMm,
     Pfn,
     VramAddress, //
 };
 use crate::gpu::Architecture;
+use kernel::prelude::*;
+
+/// Trait for allocating page tables during page table walks.
+///
+/// Implementors must allocate a zeroed 4KB page table in VRAM and
+/// ensure the allocation persists for the lifetime of the address
+/// space and the lifetime of the implementor.
+pub(crate) trait PageTableAllocator {
+    /// Allocate a zeroed page table and return its VRAM address.
+    fn alloc_page_table(&mut self, mm: &mut GpuMm) -> Result<VramAddress>;
+}
 
 /// MMU version enumeration.
 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
drivers/gpu/nova-core/mm/pagetable/walk.rs new file +285 -0

285 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#58 lore raw

RFC PATCH v6 17/26 nova-core: mm: Add Virtual Memory Manager

In reply to Joel Fernandes

Add the Virtual Memory Manager (VMM) for GPU address space management. The VMM provides high-level page mapping and unmapping operations for BAR1 address spaces.

The VMM provides mapping, unmapping, lookup, and page table allocations. Uses GpuMm for access to buddy allocator, PRAMIN, and TLB. Extends the page table walker with walk_to_pte_allocate() for on-demand page table creation.

---
 drivers/gpu/nova-core/mm/mod.rs |   1 +
 drivers/gpu/nova-core/mm/vmm.rs | 204 ++++++++++++++++++++++++++++++++
 2 files changed, 205 insertions(+)
 create mode 100644 drivers/gpu/nova-core/mm/vmm.rs
drivers/gpu/nova-core/mm/mod.rs +1 -0
index 56c72bf51431..53d726eb7296 100644
@@ -7,6 +7,7 @@
 pub(crate) mod pagetable;
 pub(crate) mod pramin;
 pub(crate) mod tlb;
+pub(crate) mod vmm;
 
 use kernel::{
     devres::Devres,
drivers/gpu/nova-core/mm/vmm.rs new file +204 -0

204 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#59 lore raw

RFC PATCH v6 18/26 nova-core: mm: Add virtual address range tracking to VMM

In reply to Joel Fernandes

Extend the Virtual Memory Manager with optional virtual address range tracking using a buddy allocator. This enables BarUser to allocate contiguous virtual ranges for BAR1 mappings.

---
 drivers/gpu/nova-core/mm/vmm.rs | 49 +++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 3 deletions(-)
drivers/gpu/nova-core/mm/vmm.rs +46 -3
index a5b4af9053a0..0ab80b84e55a 100644
@@ -32,7 +32,9 @@
     gpu::buddy::{
         AllocatedBlocks,
         BuddyFlags,
-        GpuBuddyAllocParams, //
+        GpuBuddy,
+        GpuBuddyAllocParams,
+        GpuBuddyParams, //
     },
     prelude::*,
     sizes::SZ_4K,
@@ -60,29 +62,48 @@
 /// Virtual Memory Manager for a GPU address space.
 ///
 /// Each [`Vmm`] instance manages a single address space identified by its Page
-/// Directory Base (`PDB`) address. The [`Vmm`] is used for BAR1 and BAR2 mappings.
+/// Directory Base (`PDB`) address. The [`Vmm`] is used for Channel, BAR1 and BAR2 mappings.
 ///
 /// The [`Vmm`] tracks all page table allocations made during mapping operations
 /// to ensure they remain valid for the lifetime of the address space.
+///
+/// It tracks virtual address allocations via a buddy allocator.
 pub(crate) struct Vmm {
     pdb_addr: VramAddress,
     mmu_version: MmuVersion,
     /// Page table allocations that must persist for the lifetime of mappings.
     page_table_allocs: KVec<Arc<AllocatedBlocks>>,
+    /// Buddy allocator for virtual address range tracking.
+    virt_buddy: GpuBuddy,
 }
 
 impl Vmm {
     /// Create a new [`Vmm`] for the given Page Directory Base address.
-    pub(crate) fn new(pdb_addr: VramAddress, mmu_version: MmuVersion) -> Result<Self> {
+    ///
+    /// The [`Vmm`] will manage a virtual address space of `va_size` bytes using
+    /// a buddy allocator. This enables [`Vmm::alloc_vfn_range()`] for allocating
+    /// contiguous virtual ranges.
+    pub(crate) fn new(
+        pdb_addr: VramAddress,
+        mmu_version: MmuVersion,
+        va_size: u64,
+    ) -> Result<Self> {
         // Only MMU v2 is supported for now.
         if mmu_version != MmuVersion::V2 {
             return Err(ENOTSUPP);
         }
 
+        let virt_buddy = GpuBuddy::new(GpuBuddyParams {
+            base_offset_bytes: 0,
+            physical_memory_size_bytes: va_size,
+            chunk_size_bytes: SZ_4K as u64,
+        })?;
+
         Ok(Self {
             pdb_addr,
             mmu_version,
             page_table_allocs: KVec::new(),
+            virt_buddy,
         })
     }
 
@@ -96,6 +117,28 @@ pub(crate) fn mmu_version(&self) -> MmuVersion {
         self.mmu_version
     }
 
+    /// Allocate a contiguous virtual frame number range.
+    ///
+    /// Returns an [`Arc<AllocatedBlocks>`] representing the allocated range.
+    /// The allocation is automatically freed when the [`Arc`] is dropped.
+    pub(crate) fn alloc_vfn_range(&self, num_pages: usize) -> Result<(Vfn, Arc<AllocatedBlocks>)> {
+        let params = GpuBuddyAllocParams {
+            start_range_address: 0,
+            end_range_address: 0,
+            size_bytes: num_pages.checked_mul(PAGE_SIZE).ok_or(EOVERFLOW)? as u64,
+            min_block_size_bytes: SZ_4K as u64,
+            buddy_flags: BuddyFlags::try_new(BuddyFlags::CONTIGUOUS_ALLOCATION)?,
+        };
+
+        let alloc = self.virt_buddy.alloc_blocks(params)?;
+
+        // Get the starting offset from the first (and only, due to CONTIGUOUS) block.
+        let offset = alloc.iter().next().ok_or(ENOMEM)?.offset();
+        let vfn = Vfn::new(offset / PAGE_SIZE as u64);
+
+        Ok((vfn, alloc))
+    }
+
     /// Allocate a new page table, zero it, and track the allocation.
     ///
     /// This method ensures page table allocations persist for the lifetime of
Signature · 1 line
2.34.1
author reviewer
#60 lore raw

RFC PATCH v6 19/26 nova-core: mm: Add BAR1 user interface

In reply to Joel Fernandes

Add the BAR1 user interface for CPU access to GPU video memory through the BAR1 aperture.

---
 drivers/gpu/nova-core/driver.rs      |   1 -
 drivers/gpu/nova-core/mm/bar_user.rs | 195 +++++++++++++++++++++++++++
 drivers/gpu/nova-core/mm/mod.rs      |   1 +
 3 files changed, 196 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/nova-core/mm/bar_user.rs
drivers/gpu/nova-core/driver.rs +0 -1
index f30ffa45cf13..d8b2e967ba4c 100644
@@ -42,7 +42,6 @@ pub(crate) struct NovaCore {
 const GPU_DMA_BITS: u32 = 47;
 
 pub(crate) type Bar0 = pci::Bar<BAR0_SIZE>;
-#[expect(dead_code)]
 pub(crate) type Bar1 = pci::Bar<BAR1_SIZE>;
 
 kernel::pci_device_table!(
drivers/gpu/nova-core/mm/bar_user.rs new file +195 -0

195 lines not on this page · raw message

drivers/gpu/nova-core/mm/mod.rs +1 -0
index 53d726eb7296..449c2dea3e07 100644
@@ -4,6 +4,7 @@
 
 #![expect(dead_code)]
 
+pub(crate) mod bar_user;
 pub(crate) mod pagetable;
 pub(crate) mod pramin;
 pub(crate) mod tlb;
Signature · 1 line
2.34.1
author reviewer
#61 lore raw

RFC PATCH v6 20/26 nova-core: gsp: Return GspStaticInfo and FbLayout from boot()

In reply to Joel Fernandes

Refactor the GSP boot function to return the GspStaticInfo and FbLayout.

This enables access required for memory management initialization to:
- bar1_pde_base: BAR1 page directory base.
- bar2_pde_base: BAR2 page directory base.
- usable memory regions in vidmem.

---
 drivers/gpu/nova-core/gpu.rs      |  9 +++++++--
 drivers/gpu/nova-core/gsp/boot.rs | 15 ++++++++++++---
 2 files changed, 19 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/gpu.rs +7 -2
index 572e6d4502bc..91ec7f7910e9 100644
@@ -20,7 +20,10 @@
     },
     fb::SysmemFlush,
     gfw,
-    gsp::Gsp,
+    gsp::{
+        commands::GetGspStaticInfoReply,
+        Gsp, //
+    },
     mm::GpuMm,
     regs,
 };
@@ -257,6 +260,8 @@ pub(crate) struct Gpu {
     /// GSP runtime data. Temporarily an empty placeholder.
     #[pin]
     gsp: Gsp,
+    /// Static GPU information from GSP.
+    gsp_static_info: GetGspStaticInfoReply,
 }
 
 impl Gpu {
@@ -297,7 +302,7 @@ pub(crate) fn new<'a>(
 
             gsp <- Gsp::new(pdev),
 
-            _: { gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)? },
+            gsp_static_info: { gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)?.0 },
 
             bar: devres_bar,
         })
drivers/gpu/nova-core/gsp/boot.rs +12 -3
index 581b412554dc..75f949bc4864 100644
@@ -32,7 +32,10 @@
     },
     gpu::Chipset,
     gsp::{
-        commands,
+        commands::{
+            self,
+            GetGspStaticInfoReply, //
+        },
         sequencer::{
             GspSequencer,
             GspSequencerParams, //
@@ -127,6 +130,12 @@ fn run_fwsec_frts(
     /// structures that the GSP will use at runtime.
     ///
     /// Upon return, the GSP is up and running, and its runtime object given as return value.
+    ///
+    /// Returns a tuple containing:
+    /// - [`GetGspStaticInfoReply`]: Static GPU information from GSP, including the BAR1 page
+    ///   directory base address needed for memory management.
+    /// - [`FbLayout`]: Frame buffer layout computed during boot, containing memory regions
+    ///   required for [`GpuMm`] initialization.
     pub(crate) fn boot(
         mut self: Pin<&mut Self>,
         pdev: &pci::Device<device::Bound>,
@@ -134,7 +143,7 @@ pub(crate) fn boot(
         chipset: Chipset,
         gsp_falcon: &Falcon<Gsp>,
         sec2_falcon: &Falcon<Sec2>,
-    ) -> Result {
+    ) -> Result<(GetGspStaticInfoReply, FbLayout)> {
         let dev = pdev.as_ref();
 
         let bios = Vbios::new(dev, bar)?;
@@ -243,6 +252,6 @@ pub(crate) fn boot(
             Err(e) => dev_warn!(pdev.as_ref(), "GPU name unavailable: {:?}\n", e),
         }
 
-        Ok(())
+        Ok((info, fb_layout))
     }
 }
Signature · 1 line
2.34.1
author reviewer
#62 lore raw

RFC PATCH v6 21/26 nova-core: mm: Add memory management self-tests

In reply to Joel Fernandes

Add comprehensive self-tests for the MM subsystem that run during driver probe when CONFIG_NOVA_MM_SELFTESTS is enabled (default disabled). These result in testing the Vmm, buddy, bar1 and pramin all of which should function correctly for the tests to pass.

---
 drivers/gpu/nova-core/Kconfig         |  10 ++
 drivers/gpu/nova-core/driver.rs       |   2 +
 drivers/gpu/nova-core/gpu.rs          |  43 ++++++++
 drivers/gpu/nova-core/gsp/commands.rs |   1 -
 drivers/gpu/nova-core/mm/bar_user.rs  | 141 ++++++++++++++++++++++++++
 5 files changed, 196 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/Kconfig +10 -0
index 809485167aff..257bca5aa0ef 100644
@@ -15,3 +15,13 @@ config NOVA_CORE
 	  This driver is work in progress and may not be functional.
 
 	  If M is selected, the module will be called nova_core.
+
+config NOVA_MM_SELFTESTS
+	bool "Memory management self-tests"
+	depends on NOVA_CORE
+	help
+	  Enable self-tests for the memory management subsystem. When enabled,
+	  tests are run during GPU probe to verify page table walking and
+	  BAR1 virtual memory mapping functionality.
+
+	  This is a testing option and is default-disabled.
drivers/gpu/nova-core/driver.rs +2 -0
index d8b2e967ba4c..7d0d09939835 100644
@@ -92,6 +92,8 @@ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, E
 
             Ok(try_pin_init!(Self {
                 gpu <- Gpu::new(pdev, bar.clone(), bar.access(pdev.as_ref())?),
+                // Run optional GPU selftests.
+                _: { gpu.run_selftests(pdev)? },
                 _reg <- auxiliary::Registration::new(
                     pdev.as_ref(),
                     c"nova-drm",
drivers/gpu/nova-core/gpu.rs +43 -0

47 lines not on this page · raw message

drivers/gpu/nova-core/gsp/commands.rs +0 -1
index 7b5025cba106..311f65f8367b 100644
@@ -232,7 +232,6 @@ pub(crate) fn gpu_name(&self) -> core::result::Result<&str, GpuNameError> {
     }
 
     /// Returns the BAR1 Page Directory Entry base address.
-    #[expect(dead_code)]
     pub(crate) fn bar1_pde_base(&self) -> u64 {
         self.bar1_pde_base
     }
drivers/gpu/nova-core/mm/bar_user.rs +141 -0

144 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#63 lore raw

RFC PATCH v6 22/26 nova-core: mm: Add PRAMIN aperture self-tests

In reply to Joel Fernandes

Add self-tests for the PRAMIN aperture mechanism to verify correct operation during GPU probe. The tests validate various alignment requirements and corner cases.

The tests are default disabled and behind CONFIG_NOVA_PRAMIN_SELFTESTS When enabled, tests run after GSP boot during probe.

---
 drivers/gpu/nova-core/Kconfig      |  11 ++
 drivers/gpu/nova-core/gpu.rs       |  14 +++
 drivers/gpu/nova-core/mm/pramin.rs | 160 +++++++++++++++++++++++++++++
 3 files changed, 185 insertions(+)
drivers/gpu/nova-core/Kconfig +11 -0
index 257bca5aa0ef..cbdbc1fb02b2 100644
@@ -25,3 +25,14 @@ config NOVA_MM_SELFTESTS
 	  BAR1 virtual memory mapping functionality.
 
 	  This is a testing option and is default-disabled.
+
+config NOVA_PRAMIN_SELFTESTS
+	bool "PRAMIN self-tests"
+	depends on NOVA_CORE
+	default n
+	help
+	  Enable self-tests for the PRAMIN aperture mechanism. When enabled,
+	  basic tests are run during GPU probe after GSP boot to
+	  verify PRAMIN functionality.
+
+	  This is a testing option and is default-disabled.
drivers/gpu/nova-core/gpu.rs +14 -0
index 938828508f2c..a1bcf6679e2a 100644
@@ -324,10 +324,24 @@ pub(crate) fn run_selftests(
         mut self: Pin<&mut Self>,
         pdev: &pci::Device<device::Bound>,
     ) -> Result {
+        self.as_mut().run_pramin_selftest(pdev)?;
         self.as_mut().run_mm_selftest(pdev)?;
         Ok(())
     }
 
+    fn run_pramin_selftest(self: Pin<&mut Self>, pdev: &pci::Device<device::Bound>) -> Result {
+        #[cfg(CONFIG_NOVA_PRAMIN_SELFTESTS)]
+        {
+            use crate::mm::pagetable::MmuVersion;
+
+            let mmu_version = MmuVersion::from(self.spec.chipset.arch());
+            crate::mm::pramin::run_self_test(pdev.as_ref(), self.bar.clone(), mmu_version)?;
+        }
+
+        let _ = pdev; // Suppress unused warning when selftests disabled.
+        Ok(())
+    }
+
     fn run_mm_selftest(mut self: Pin<&mut Self>, pdev: &pci::Device<device::Bound>) -> Result {
         #[cfg(CONFIG_NOVA_MM_SELFTESTS)]
         {
drivers/gpu/nova-core/mm/pramin.rs +160 -0

163 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#64 lore raw

RFC PATCH v6 23/26 nova-core: gsp: Extract usable FB region from GSP

In reply to Joel Fernandes

Add first_usable_fb_region() to GspStaticConfigInfo to extract the first usable FB region from GSP's fbRegionInfoParams. Usable regions are those that are not reserved or protected.

The extracted region is stored in GetGspStaticInfoReply and exposed via usable_fb_region() API for use by the memory subsystem.

---
 drivers/gpu/nova-core/gsp/commands.rs    | 13 +++++++++-
 drivers/gpu/nova-core/gsp/fw/commands.rs | 30 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/commands.rs +12 -1
index 311f65f8367b..d619cf294b9c 100644
@@ -186,10 +186,13 @@ fn init(&self) -> impl Init<Self::Command, Self::InitError> {
     }
 }
 
-/// The reply from the GSP to the [`GetGspInfo`] command.
+/// The reply from the GSP to the [`GetGspStaticInfo`] command.
 pub(crate) struct GetGspStaticInfoReply {
     gpu_name: [u8; 64],
     bar1_pde_base: u64,
+    /// First usable FB region (base, size) for memory allocation.
+    #[expect(dead_code)]
+    usable_fb_region: Option<(u64, u64)>,
 }
 
 impl MessageFromGsp for GetGspStaticInfoReply {
@@ -204,6 +207,7 @@ fn read(
         Ok(GetGspStaticInfoReply {
             gpu_name: msg.gpu_name_str(),
             bar1_pde_base: msg.bar1_pde_base(),
+            usable_fb_region: msg.first_usable_fb_region(),
         })
     }
 }
@@ -235,6 +239,13 @@ pub(crate) fn gpu_name(&self) -> core::result::Result<&str, GpuNameError> {
     pub(crate) fn bar1_pde_base(&self) -> u64 {
         self.bar1_pde_base
     }
+
+    /// Returns the usable FB region (base, size) for driver allocation which is
+    /// already retrieved from the GSP.
+    #[expect(dead_code)]
+    pub(crate) fn usable_fb_region(&self) -> Option<(u64, u64)> {
+        self.usable_fb_region
+    }
 }
 
 /// Send the [`GetGspInfo`] command and awaits for its reply.
drivers/gpu/nova-core/gsp/fw/commands.rs +30 -0
index f069f4092911..cc1cf4bd52ea 100644
@@ -122,6 +122,36 @@ impl GspStaticConfigInfo {
     pub(crate) fn bar1_pde_base(&self) -> u64 {
         self.0.bar1PdeBase
     }
+
+    /// Extract the first usable FB region from GSP firmware data.
+    ///
+    /// Returns the first region suitable for driver memory allocation as a base,size tuple.
+    /// Usable regions are those that:
+    /// - Are not reserved for firmware internal use.
+    /// - Are not protected (hardware-enforced access restrictions).
+    /// - Support compression (can use GPU memory compression for bandwidth).
+    /// - Support ISO (isochronous memory for display requiring guaranteed bandwidth).
+    pub(crate) fn first_usable_fb_region(&self) -> Option<(u64, u64)> {
+        let fb_info = &self.0.fbRegionInfoParams;
+        for i in 0..fb_info.numFBRegions as usize {
+            if let Some(reg) = fb_info.fbRegion.get(i) {
+                // Skip malformed regions where limit < base.
+                if reg.limit < reg.base {
+                    continue;
+                }
+                // Filter: not reserved, not protected, supports compression and ISO.
+                if reg.reserved == 0
+                    && reg.bProtected == 0
+                    && reg.supportCompressed != 0
+                    && reg.supportISO != 0
+                {
+                    let size = reg.limit - reg.base + 1;
+                    return Some((reg.base, size));
+                }
+            }
+        }
+        None
+    }
 }
 
 // SAFETY: Padding is explicit and will not contain uninitialized data.
Signature · 1 line
2.34.1
author reviewer
#65 lore raw

RFC PATCH v6 24/26 nova-core: fb: Add usable_vram field to FbLayout

In reply to Joel Fernandes

Add usable_vram field to FbLayout to store the usable VRAM region for driver allocations. This is populated after GSP boot with the region extracted from GSP's fbRegionInfoParams.

FbLayout is now a two-phase structure:
1. new() computes firmware layout from hardware
2. set_usable_vram() populates usable region from GSP

The new usable_vram field represents the actual usable VRAM region (~23.7GB on a 24GB GPU GA102 Ampere GPU).

---
 drivers/gpu/nova-core/fb.rs | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/fb.rs +22 -1
index c62abcaed547..779447952b19 100644
@@ -97,6 +97,10 @@ pub(crate) fn unregister(&self, bar: &Bar0) {
 /// Layout of the GPU framebuffer memory.
 ///
 /// Contains ranges of GPU memory reserved for a given purpose during the GSP boot process.
+///
+/// This structure is populated in 2 steps:
+/// 1. [`FbLayout::new()`] computes firmware layout from hardware.
+/// 2. [`FbLayout::set_usable_vram()`] populates usable region from GSP response.
 #[derive(Debug)]
 pub(crate) struct FbLayout {
     /// Range of the framebuffer. Starts at `0`.
@@ -111,10 +115,14 @@ pub(crate) struct FbLayout {
     pub(crate) elf: Range<u64>,
     /// WPR2 heap.
     pub(crate) wpr2_heap: Range<u64>,
-    /// WPR2 region range, starting with an instance of `GspFwWprMeta`.
+    /// WPR2 region range, starting with an instance of [`GspFwWprMeta`].
     pub(crate) wpr2: Range<u64>,
+    /// Non-WPR heap carved before WPR2, used by GSP firmware.
     pub(crate) heap: Range<u64>,
     pub(crate) vf_partition_count: u8,
+    /// Usable VRAM region for driver allocations (from GSP `fbRegionInfoParams`).
+    /// Initially [`None`], populated after GSP boot with usable region info.
+    pub(crate) usable_vram: Option<Range<u64>>,
 }
 
 impl FbLayout {
@@ -212,6 +220,19 @@ pub(crate) fn new(chipset: Chipset, bar: &Bar0, gsp_fw: &GspFirmware) -> Result<
             wpr2,
             heap,
             vf_partition_count: 0,
+            usable_vram: None,
         })
     }
+
+    /// Set the usable VRAM region from GSP response.
+    ///
+    /// Called after GSP boot with the first usable region extracted from
+    /// GSP's `fbRegionInfoParams`. Usable regions are those that:
+    /// - Are not reserved for firmware internal use.
+    /// - Are not protected (hardware-enforced access restrictions).
+    /// - Support compression (can use GPU memory compression for bandwidth).
+    /// - Support ISO (isochronous memory for display requiring guaranteed bandwidth).
+    pub(crate) fn set_usable_vram(&mut self, base: u64, size: u64) {
+        self.usable_vram = Some(base..base.saturating_add(size));
+    }
 }
Signature · 1 line
2.34.1
author reviewer
#66 lore raw

RFC PATCH v6 25/26 nova-core: mm: Use usable VRAM region for buddy allocator

In reply to Joel Fernandes

The buddy allocator manages the actual usable VRAM. On my GA102 Ampere with 24GB video memory, that is ~23.7GB on a 24GB GPU enabling proper GPU memory allocation for driver use.

---
 drivers/gpu/nova-core/gpu.rs          | 62 ++++++++++++++++++++++-----
 drivers/gpu/nova-core/gsp/boot.rs     |  7 ++-
 drivers/gpu/nova-core/gsp/commands.rs |  2 -
 3 files changed, 57 insertions(+), 14 deletions(-)
drivers/gpu/nova-core/gpu.rs +51 -11
index a1bcf6679e2a..dd05ad23f763 100644
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
+use core::cell::Cell;
+
 use kernel::{
     device,
     devres::Devres,
@@ -7,7 +9,7 @@
     gpu::buddy::GpuBuddyParams,
     pci,
     prelude::*,
-    sizes::{SZ_1M, SZ_4K},
+    sizes::SZ_4K,
     sync::Arc, //
 };
 
@@ -28,6 +30,13 @@
     regs,
 };
 
+/// Parameters extracted from GSP boot for initializing memory subsystems.
+#[derive(Clone, Copy)]
+struct BootParams {
+    usable_vram_start: u64,
+    usable_vram_size: u64,
+}
+
 macro_rules! define_chipset {
     ({ $($variant:ident = $value:expr),* $(,)* }) =>
     {
@@ -270,6 +279,13 @@ pub(crate) fn new<'a>(
         devres_bar: Arc<Devres<Bar0>>,
         bar: &'a Bar0,
     ) -> impl PinInit<Self, Error> + 'a {
+        // Cell to share boot parameters between GSP boot and subsequent initializations.
+        // Contains usable VRAM region from FbLayout and BAR1 PDE base from GSP info.
+        let boot_params: Cell<BootParams> = Cell::new(BootParams {
+            usable_vram_start: 0,
+            usable_vram_size: 0,
+        });
+
         try_pin_init!(Self {
             spec: Spec::new(pdev.as_ref(), bar).inspect(|spec| {
                 dev_info!(pdev.as_ref(),"NVIDIA ({})\n", spec);
@@ -291,18 +307,42 @@ pub(crate) fn new<'a>(
 
             sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset)?,
 
-            // Create GPU memory manager owning memory management resources.
-            // This will be initialized with the usable VRAM region from GSP in a later
-            // patch. For now, we use a placeholder of 1MB.
-            mm: GpuMm::new(devres_bar.clone(), GpuBuddyParams {
-                base_offset_bytes: 0,
-                physical_memory_size_bytes: SZ_1M as u64,
-                chunk_size_bytes: SZ_4K as u64,
-            })?,
-
             gsp <- Gsp::new(pdev),
 
-            gsp_static_info: { gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)?.0 },
+            // Boot GSP and extract usable VRAM region for buddy allocator.
+            gsp_static_info: {
+                let (info, fb_layout) = gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)?;
+
+                let usable_vram = fb_layout.usable_vram.as_ref().ok_or_else(|| {
+                    dev_err!(pdev.as_ref(), "No usable FB regions found from GSP\n");
+                    ENODEV
+                })?;
+
+                dev_info!(
+                    pdev.as_ref(),
+                    "Using FB region: {:#x}..{:#x}\n",
+                    usable_vram.start,
+                    usable_vram.end
+                );
+
+                boot_params.set(BootParams {
+                    usable_vram_start: usable_vram.start,
+                    usable_vram_size: usable_vram.end - usable_vram.start,
+                });
+
+                info
+            },
+
+            // Create GPU memory manager owning memory management resources.
+            // Uses the usable VRAM region from GSP for buddy allocator.
+            mm: {
+                let params = boot_params.get();
+                GpuMm::new(devres_bar.clone(), GpuBuddyParams {
+                    base_offset_bytes: params.usable_vram_start,
+                    physical_memory_size_bytes: params.usable_vram_size,
+                    chunk_size_bytes: SZ_4K as u64,
+                })?
+            },
 
             bar: devres_bar,
         })
drivers/gpu/nova-core/gsp/boot.rs +6 -1
index 75f949bc4864..a034e2e80a4b 100644
@@ -150,7 +150,7 @@ pub(crate) fn boot(
 
         let gsp_fw = KBox::pin_init(GspFirmware::new(dev, chipset, FIRMWARE_VERSION), GFP_KERNEL)?;
 
-        let fb_layout = FbLayout::new(chipset, bar, &gsp_fw)?;
+        let mut fb_layout = FbLayout::new(chipset, bar, &gsp_fw)?;
         dev_dbg!(dev, "{:#x?}\n", fb_layout);
 
         Self::run_fwsec_frts(dev, gsp_falcon, bar, &bios, &fb_layout)?;
@@ -252,6 +252,11 @@ pub(crate) fn boot(
             Err(e) => dev_warn!(pdev.as_ref(), "GPU name unavailable: {:?}\n", e),
         }
 
+        // Populate usable VRAM from GSP response.
+        if let Some((base, size)) = info.usable_fb_region() {
+            fb_layout.set_usable_vram(base, size);
+        }
+
         Ok((info, fb_layout))
     }
 }
drivers/gpu/nova-core/gsp/commands.rs +0 -2
index d619cf294b9c..4a7eda512789 100644
@@ -191,7 +191,6 @@ pub(crate) struct GetGspStaticInfoReply {
     gpu_name: [u8; 64],
     bar1_pde_base: u64,
     /// First usable FB region (base, size) for memory allocation.
-    #[expect(dead_code)]
     usable_fb_region: Option<(u64, u64)>,
 }
 
@@ -242,7 +241,6 @@ pub(crate) fn bar1_pde_base(&self) -> u64 {
 
     /// Returns the usable FB region (base, size) for driver allocation which is
     /// already retrieved from the GSP.
-    #[expect(dead_code)]
     pub(crate) fn usable_fb_region(&self) -> Option<(u64, u64)> {
         self.usable_fb_region
     }
Signature · 1 line
2.34.1
author reviewer
#67 lore raw

RFC PATCH v6 26/26 nova-core: mm: Add BarUser to struct Gpu and create at boot

In reply to Joel Fernandes

Add a BarUser field to struct Gpu and eagerly create it during GPU initialization. The BarUser provides the BAR1 user interface for CPU access to GPU virtual memory through the GPU's MMU.

The BarUser is initialized using BAR1 PDE base address from GSP static info, MMU version and BAR1 size obtained from platform device.

---
 drivers/gpu/nova-core/gpu.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gpu.rs +21 -1
index dd05ad23f763..15d8d42ecfa8 100644
@@ -26,7 +26,12 @@
         commands::GetGspStaticInfoReply,
         Gsp, //
     },
-    mm::GpuMm,
+    mm::{
+        bar_user::BarUser,
+        pagetable::MmuVersion,
+        GpuMm,
+        VramAddress, //
+    },
     regs,
 };
 
@@ -35,6 +40,7 @@
 struct BootParams {
     usable_vram_start: u64,
     usable_vram_size: u64,
+    bar1_pde_base: u64,
 }
 
 macro_rules! define_chipset {
@@ -271,6 +277,8 @@ pub(crate) struct Gpu {
     gsp: Gsp,
     /// Static GPU information from GSP.
     gsp_static_info: GetGspStaticInfoReply,
+    /// BAR1 user interface for CPU access to GPU virtual memory.
+    bar_user: BarUser,
 }
 
 impl Gpu {
@@ -284,6 +292,7 @@ pub(crate) fn new<'a>(
         let boot_params: Cell<BootParams> = Cell::new(BootParams {
             usable_vram_start: 0,
             usable_vram_size: 0,
+            bar1_pde_base: 0,
         });
 
         try_pin_init!(Self {
@@ -328,6 +337,7 @@ pub(crate) fn new<'a>(
                 boot_params.set(BootParams {
                     usable_vram_start: usable_vram.start,
                     usable_vram_size: usable_vram.end - usable_vram.start,
+                    bar1_pde_base: info.bar1_pde_base(),
                 });
 
                 info
@@ -344,6 +354,16 @@ pub(crate) fn new<'a>(
                 })?
             },
 
+            // Create BAR1 user interface for CPU access to GPU virtual memory.
+            // Uses the BAR1 PDE base from GSP and full BAR1 size for VA space.
+            bar_user: {
+                let params = boot_params.get();
+                let pdb_addr = VramAddress::new(params.bar1_pde_base);
+                let mmu_version = MmuVersion::from(spec.chipset.arch());
+                let bar1_size = pdev.resource_len(1)?;
+                BarUser::new(pdb_addr, mmu_version, bar1_size)?
+            },
+
             bar: devres_bar,
         })
     }
Signature · 1 line
2.34.1
#68 lore raw

✗ Fi.CI.BUILD: failure for nova-core: Memory management infrastructure (v6)

In reply to Joel Fernandes
== Series Details ==

== Series Details ==

Series: nova-core: Memory management infrastructure (v6)
URL : https://patchwork.freedesktop.org/series/160380/
State : failure

== Summary ==

Error: patch https://patchwork.freedesktop.org/api/1.0/series/160380/revisions/1/mbox/ not applied
Applying: rust: clist: Add support to interface with C linked lists
Applying: gpu: Move DRM buddy allocator one level up
Using index info to reconstruct a base tree...
M Documentation/gpu/drm-mm.rst
M drivers/gpu/drm/Kconfig
M drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
M drivers/gpu/drm/drm_buddy.c
M drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c
M drivers/gpu/drm/xe/xe_svm.c
M drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
Falling back to patching base and 3-way merge...
Auto-merging drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
Auto-merging drivers/gpu/drm/xe/xe_svm.c
CONFLICT (content): Merge conflict in drivers/gpu/drm/xe/xe_svm.c
Auto-merging drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c
Auto-merging drivers/gpu/drm/drm_buddy.c
CONFLICT (content): Merge conflict in drivers/gpu/drm/drm_buddy.c
Auto-merging drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c
Auto-merging drivers/gpu/drm/Kconfig
Auto-merging Documentation/gpu/drm-mm.rst
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0002 gpu: Move DRM buddy allocator one level up
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Build failed, no error log produced

maintainer
#69 lore raw
In reply to Joel Fernandes
Quoted 5 lines · Joel Fernandes

This series is rebased on drm-rust-kernel/drm-rust-next and provides memory management infrastructure for the nova-core GPU driver. It combines several previous series and provides a foundation for nova GPU memory management including page tables, virtual memory management, and BAR mapping. All these are critical nova-core features.

Thanks for this work, I will go through the series soon. (Although it would also be nice to have what I mention below addressed first.)

Quoted 25 lines

The series includes:
- A Rust module (CList) to interface with C circular linked lists, required for iterating over buddy allocator blocks.
- Movement of the DRM buddy allocator up to drivers/gpu/ level, renamed to GPU buddy.
- Rust bindings for the GPU buddy allocator.
- PRAMIN aperture support for direct VRAM access.
- Page table types for MMU v2 and v3 formats.
- Virtual Memory Manager (VMM) for GPU virtual address space management.
- BAR1 user interface for mapping access GPU via virtual memory.
- Selftests for PRAMIN and BAR1 user interface (disabled by default).

Changes from v5 to v6:
- Rebased on drm-rust-kernel/drm-rust-next
- Added page table types and page table walker infrastructure
- Added Virtual Memory Manager (VMM)
- Added BAR1 user interface
- Added TLB flush support
- Added GpuMm memory manager
- Extended to 26 patches from 6 (full mm infrastructure now included)

The git tree with all patches can be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git (tag: nova-mm-v6-20260120)

Link to v5: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Previous series that are combined:
- v4 (clist + buddy): https://lore.kernel.org/all/[email protected]/
- v3 (clist only): https://lore.kernel.org/all/[email protected]/
- v2 (clist only): https://lore.kernel.org/all/[email protected]/
- clist RFC (original with buddy): https://lore.kernel.org/all/[email protected]/
- DRM buddy move: https://lore.kernel.org/all/[email protected]/
- PRAMIN series: https://lore.kernel.org/all/[email protected]/

I'm not overly happy with this version history. I understand that you are building things on top of each other, but going back and forth with adding and removing features from a series is confusing and makes it hard to keep track of things.

(In the worst case it may even result in reviewers skipping over it leaving you with no progress eventually.)

I.e. you stared with a CList and DRM buddy RFC, then DRM buddy disappeared for a few versions and came back eventually. Then, in the next version, the PRAMIN stuff came back in, which also had a predecessor series already and now you added lots of MM stuff on top of it.

The whole version history is about what features and patches were added and removed to/from the series, rather than about what actually changed design wise and code wise between the iterations (which is the important part for reviewers and maintainers).

I also think it is confusing that a lot of the patches in this series have never been posted before, yet they are labeled as v6 of this RFC.

Hence, please separate the features from each other in separate patch series, with their own proper version history and changelog. In order to account for the dependencies, you can just mention them in the cover letter and add a link to the other related patch series, which should be sufficient for people interested in the full picture.

I think the most clean approach would probably be a split with CList, DRM buddy and Nova MM stuff.

And just to clarify, in the end I do not care too much about whether it's all in a single series or split up, but going back and forth with combining things that once have been separate and have a separate history doesn't work out well.

author reviewer
#70 lore raw
Quoted 9 lines · Danilo Krummrich
Quoted 5 lines · Joel Fernandes

This series is rebased on drm-rust-kernel/drm-rust-next and provides memory management infrastructure for the nova-core GPU driver. It combines several previous series and provides a foundation for nova GPU memory management including page tables, virtual memory management, and BAR mapping. All these are critical nova-core features.

Thanks for this work, I will go through the series soon. (Although it would also be nice to have what I mention below addressed first.)

Thanks, I appreciate that.

Quoted 16 lines

I'm not overly happy with this version history. I understand that you are building things on top of each other, but going back and forth with adding and removing features from a series is confusing and makes it hard to keep track of things.

(In the worst case it may even result in reviewers skipping over it leaving you with no progress eventually.)

[...]

Hence, please separate the features from each other in separate patch series, with their own proper version history and changelog. In order to account for the dependencies, you can just mention them in the cover letter and add a link to the other related patch series, which should be sufficient for people interested in the full picture.

Quoted 6 lines

I think the most clean approach would probably be a split with CList, DRM buddy and Nova MM stuff.

And just to clarify, in the end I do not care too much about whether it's all in a single series or split up, but going back and forth with combining things that once have been separate and have a separate history doesn't work out well.

I understand the concern, and I appreciate you taking the time to explain. Let me provide some context on how we ended up here, as it may help clarify the situation.

1. This is a multi-month undertaking with many interdependencies. It is
difficult to predict which patches will come to exist, the optimal order, how to split, which series first, or what pieces are missing. This is similar to the evolution of nova
itself - complex interdependencies make it hard to predict what will be
needed. Rather than waiting months for a perfect plan before posting
anything, I chose to iterate publicly.

2. The decision to move GPU buddy out of DRM came later in the process [1]. This significantly changed the scope, requiring a much larger patch to handle the buddy infrastructure that everything else depends on.

3. The decision to separate buddy from the CList series came from wanting to make progress on CList independently [2]. That effort alone took almost a month with several rewrites based on feedback from others.

4. There was some back and forth on whether to post code with users or code that could potentially be used. This influenced the decision to combine things into the same series to demonstrate working functionality.

5. The memory management code only became functional around v3. Page table walking turned out to be tricky, and I did not have a proper user at that time. Eventually I realized BAR1 is a strong use case for page table translation, so I added support for that.

Regarding splitting the series: that makes sense, I will split into CList, GPU buddy, and Nova MM as you suggest. You make a fair point about the versioning
too - labeling new patches (even though most are old) as v6 is confusing. One question: what version numbers should each split series use? CList was at v3 before being combined,
and similar story for GPU buddy and Nova MM. Should I continue from the last
version number they were posted with, or continue from v6?

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

--
Joel Fernandes

maintainer
#71 lore raw
Quoted 1 line · Joel Fernandes

I will split into CList, GPU buddy, and Nova MM as you suggest.

Thanks, together with a proper changelog this will help a lot.

Quoted 4 lines

One question: what version numbers should each split series use? CList was at v3 before being combined, and similar story for GPU buddy and Nova MM. Should I continue from the last version number they were posted with, or continue from v6?

I'd say from the last version is probably best. Maybe you also want to move out of the RFC stage for some of them.

Thanks,
Danilo